avformat/lrcenc: Avoid allocations for writing packet data

Reviewed-by: Paul B Mahol <onemda@gmail.com>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
This commit is contained in:
Andreas Rheinhardt 2021-10-01 22:11:17 +02:00
parent 1307089523
commit e110076d8c

View File

@ -84,33 +84,25 @@ static int lrc_write_header(AVFormatContext *s)
static int lrc_write_packet(AVFormatContext *s, AVPacket *pkt) static int lrc_write_packet(AVFormatContext *s, AVPacket *pkt)
{ {
if(pkt->pts != AV_NOPTS_VALUE) { if(pkt->pts != AV_NOPTS_VALUE) {
char *data = av_malloc(pkt->size + 1); const uint8_t *line = pkt->data;
char *line; const uint8_t *end = pkt->data + pkt->size;
char *delim;
if(!data) { while (end > line && (end[-1] == '\n' || end[-1] == '\r'))
return AVERROR(ENOMEM); end--;
} if (line != end) {
memcpy(data, pkt->data, pkt->size); while (line[0] == '\n' || line[0] == '\r')
data[pkt->size] = '\0'; line++; // Skip first empty lines
for(delim = data + pkt->size - 1;
delim >= data && (delim[0] == '\n' || delim[0] == '\r'); delim--) {
delim[0] = '\0'; // Strip last empty lines
}
line = data;
while(line[0] == '\n' || line[0] == '\r') {
line++; // Skip first empty lines
} }
while(line) { while(line) {
delim = strchr(line, '\n'); const uint8_t *next_line = memchr(line, '\n', end - line);
if(delim) { size_t size = end - line;
if(delim > line && delim[-1] == '\r') {
delim[-1] = '\0'; if (next_line) {
} size = next_line - line;
delim[0] = '\0'; if (next_line > line && next_line[-1] == '\r')
delim++; size--;
next_line++;
} }
if(line[0] == '[') { if(line[0] == '[') {
av_log(s, AV_LOG_WARNING, av_log(s, AV_LOG_WARNING,
@ -130,10 +122,10 @@ static int lrc_write_packet(AVFormatContext *s, AVPacket *pkt)
((-pkt->pts) / 100) % 60, ((-pkt->pts) / 100) % 60,
(-pkt->pts) % 100); (-pkt->pts) % 100);
} }
avio_printf(s->pb, "%s\n", line); avio_write(s->pb, line, size);
line = delim; avio_w8(s->pb, '\n');
line = next_line;
} }
av_free(data);
} }
return 0; return 0;
} }