avformat/movenc: use av_packet_alloc() to allocate packets

Signed-off-by: James Almer <jamrial@gmail.com>
This commit is contained in:
James Almer 2021-01-29 11:13:18 -03:00
parent 2e80435a9a
commit ecdad29b67
3 changed files with 77 additions and 47 deletions

View File

@ -365,7 +365,7 @@ static int mov_write_ac3_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *trac
} }
struct eac3_info { struct eac3_info {
AVPacket pkt; AVPacket *pkt;
uint8_t ec3_done; uint8_t ec3_done;
uint8_t num_blocks; uint8_t num_blocks;
@ -407,6 +407,9 @@ static int handle_eac3(MOVMuxContext *mov, AVPacket *pkt, MOVTrack *track)
return AVERROR(ENOMEM); return AVERROR(ENOMEM);
info = track->eac3_priv; info = track->eac3_priv;
if (!info->pkt && !(info->pkt = av_packet_alloc()))
return AVERROR(ENOMEM);
if (avpriv_ac3_parse_header(&hdr, pkt->data, pkt->size) < 0) { if (avpriv_ac3_parse_header(&hdr, pkt->data, pkt->size) < 0) {
/* drop the packets until we see a good one */ /* drop the packets until we see a good one */
if (!track->entry) { if (!track->entry) {
@ -511,20 +514,20 @@ concatenate:
} }
if (!info->num_blocks) { if (!info->num_blocks) {
ret = av_packet_ref(&info->pkt, pkt); ret = av_packet_ref(info->pkt, pkt);
if (!ret) if (!ret)
info->num_blocks = num_blocks; info->num_blocks = num_blocks;
goto end; goto end;
} else { } else {
if ((ret = av_grow_packet(&info->pkt, pkt->size)) < 0) if ((ret = av_grow_packet(info->pkt, pkt->size)) < 0)
goto end; goto end;
memcpy(info->pkt.data + info->pkt.size - pkt->size, pkt->data, pkt->size); memcpy(info->pkt->data + info->pkt->size - pkt->size, pkt->data, pkt->size);
info->num_blocks += num_blocks; info->num_blocks += num_blocks;
info->pkt.duration += pkt->duration; info->pkt->duration += pkt->duration;
if (info->num_blocks != 6) if (info->num_blocks != 6)
goto end; goto end;
av_packet_unref(pkt); av_packet_unref(pkt);
av_packet_move_ref(pkt, &info->pkt); av_packet_move_ref(pkt, info->pkt);
info->num_blocks = 0; info->num_blocks = 0;
} }
ret = pkt->size; ret = pkt->size;
@ -3733,7 +3736,7 @@ static int mov_write_covr(AVIOContext *pb, AVFormatContext *s)
for (i = 0; i < s->nb_streams; i++) { for (i = 0; i < s->nb_streams; i++) {
MOVTrack *trk = &mov->tracks[i]; MOVTrack *trk = &mov->tracks[i];
if (!is_cover_image(trk->st) || trk->cover_image.size <= 0) if (!is_cover_image(trk->st) || trk->cover_image->size <= 0)
continue; continue;
if (!pos) { if (!pos) {
@ -3741,11 +3744,11 @@ static int mov_write_covr(AVIOContext *pb, AVFormatContext *s)
avio_wb32(pb, 0); avio_wb32(pb, 0);
ffio_wfourcc(pb, "covr"); ffio_wfourcc(pb, "covr");
} }
avio_wb32(pb, 16 + trk->cover_image.size); avio_wb32(pb, 16 + trk->cover_image->size);
ffio_wfourcc(pb, "data"); ffio_wfourcc(pb, "data");
avio_wb32(pb, trk->tag); avio_wb32(pb, trk->tag);
avio_wb32(pb , 0); avio_wb32(pb , 0);
avio_write(pb, trk->cover_image.data, trk->cover_image.size); avio_write(pb, trk->cover_image->data, trk->cover_image->size);
} }
return pos ? update_size(pb, pos) : 0; return pos ? update_size(pb, pos) : 0;
@ -5969,20 +5972,20 @@ static int mov_write_single_packet(AVFormatContext *s, AVPacket *pkt)
static int mov_write_subtitle_end_packet(AVFormatContext *s, static int mov_write_subtitle_end_packet(AVFormatContext *s,
int stream_index, int stream_index,
int64_t dts) { int64_t dts) {
AVPacket end; MOVMuxContext *mov = s->priv_data;
AVPacket *end = mov->pkt;
uint8_t data[2] = {0}; uint8_t data[2] = {0};
int ret; int ret;
av_init_packet(&end); end->size = sizeof(data);
end.size = sizeof(data); end->data = data;
end.data = data; end->pts = dts;
end.pts = dts; end->dts = dts;
end.dts = dts; end->duration = 0;
end.duration = 0; end->stream_index = stream_index;
end.stream_index = stream_index;
ret = mov_write_single_packet(s, &end); ret = mov_write_single_packet(s, end);
av_packet_unref(&end); av_packet_unref(end);
return ret; return ret;
} }
@ -6009,7 +6012,7 @@ static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
return 0; return 0;
} }
if ((ret = av_packet_ref(&trk->cover_image, pkt)) < 0) if ((ret = av_packet_ref(trk->cover_image, pkt)) < 0)
return ret; return ret;
return 0; return 0;
@ -6095,7 +6098,7 @@ static int mov_create_chapter_track(AVFormatContext *s, int tracknum)
MOVMuxContext *mov = s->priv_data; MOVMuxContext *mov = s->priv_data;
MOVTrack *track = &mov->tracks[tracknum]; MOVTrack *track = &mov->tracks[tracknum];
AVPacket pkt = { .stream_index = tracknum, .flags = AV_PKT_FLAG_KEY }; AVPacket *pkt = mov->pkt;
int i, len; int i, len;
track->mode = mov->mode; track->mode = mov->mode;
@ -6157,13 +6160,16 @@ static int mov_create_chapter_track(AVFormatContext *s, int tracknum)
} }
#endif #endif
pkt->stream_index = tracknum;
pkt->flags = AV_PKT_FLAG_KEY;
for (i = 0; i < s->nb_chapters; i++) { for (i = 0; i < s->nb_chapters; i++) {
AVChapter *c = s->chapters[i]; AVChapter *c = s->chapters[i];
AVDictionaryEntry *t; AVDictionaryEntry *t;
int64_t end = av_rescale_q(c->end, c->time_base, (AVRational){1,MOV_TIMESCALE}); int64_t end = av_rescale_q(c->end, c->time_base, (AVRational){1,MOV_TIMESCALE});
pkt.pts = pkt.dts = av_rescale_q(c->start, c->time_base, (AVRational){1,MOV_TIMESCALE}); pkt->pts = pkt->dts = av_rescale_q(c->start, c->time_base, (AVRational){1,MOV_TIMESCALE});
pkt.duration = end - pkt.dts; pkt->duration = end - pkt->dts;
if ((t = av_dict_get(c->metadata, "title", NULL, 0))) { if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
static const char encd[12] = { static const char encd[12] = {
@ -6171,18 +6177,22 @@ static int mov_create_chapter_track(AVFormatContext *s, int tracknum)
'e', 'n', 'c', 'd', 'e', 'n', 'c', 'd',
0x00, 0x00, 0x01, 0x00 }; 0x00, 0x00, 0x01, 0x00 };
len = strlen(t->value); len = strlen(t->value);
pkt.size = len + 2 + 12; pkt->size = len + 2 + 12;
pkt.data = av_malloc(pkt.size); pkt->data = av_malloc(pkt->size);
if (!pkt.data) if (!pkt->data) {
av_packet_unref(pkt);
return AVERROR(ENOMEM); return AVERROR(ENOMEM);
AV_WB16(pkt.data, len); }
memcpy(pkt.data + 2, t->value, len); AV_WB16(pkt->data, len);
memcpy(pkt.data + len + 2, encd, sizeof(encd)); memcpy(pkt->data + 2, t->value, len);
ff_mov_write_packet(s, &pkt); memcpy(pkt->data + len + 2, encd, sizeof(encd));
av_freep(&pkt.data); ff_mov_write_packet(s, pkt);
av_freep(&pkt->data);
} }
} }
av_packet_unref(mov->pkt);
return 0; return 0;
} }
@ -6202,9 +6212,9 @@ static int mov_create_timecode_track(AVFormatContext *s, int index, int src_inde
MOVTrack *track = &mov->tracks[index]; MOVTrack *track = &mov->tracks[index];
AVStream *src_st = s->streams[src_index]; AVStream *src_st = s->streams[src_index];
uint8_t data[4]; uint8_t data[4];
AVPacket pkt = { .data = data, .stream_index = index, AVPacket *pkt = mov->pkt;
.flags = AV_PKT_FLAG_KEY, .size = 4 };
AVRational rate = find_fps(s, src_st); AVRational rate = find_fps(s, src_st);
int ret;
/* tmcd track based on video stream */ /* tmcd track based on video stream */
track->mode = mov->mode; track->mode = mov->mode;
@ -6226,8 +6236,14 @@ static int mov_create_timecode_track(AVFormatContext *s, int index, int src_inde
track->st->avg_frame_rate = av_inv_q(rate); track->st->avg_frame_rate = av_inv_q(rate);
/* the tmcd track just contains one packet with the frame number */ /* the tmcd track just contains one packet with the frame number */
AV_WB32(pkt.data, tc.start); pkt->data = data;
return ff_mov_write_packet(s, &pkt); pkt->stream_index = index;
pkt->flags = AV_PKT_FLAG_KEY;
pkt->size = 4;
AV_WB32(pkt->data, tc.start);
ret = ff_mov_write_packet(s, pkt);
av_packet_unref(pkt);
return ret;
} }
/* /*
@ -6288,6 +6304,8 @@ static void mov_free(AVFormatContext *s)
MOVMuxContext *mov = s->priv_data; MOVMuxContext *mov = s->priv_data;
int i; int i;
av_packet_free(&mov->pkt);
if (!mov->tracks) if (!mov->tracks)
return; return;
@ -6302,11 +6320,11 @@ static void mov_free(AVFormatContext *s)
av_freep(&mov->tracks[i].par); av_freep(&mov->tracks[i].par);
av_freep(&mov->tracks[i].cluster); av_freep(&mov->tracks[i].cluster);
av_freep(&mov->tracks[i].frag_info); av_freep(&mov->tracks[i].frag_info);
av_packet_unref(&mov->tracks[i].cover_image); av_packet_free(&mov->tracks[i].cover_image);
if (mov->tracks[i].eac3_priv) { if (mov->tracks[i].eac3_priv) {
struct eac3_info *info = mov->tracks[i].eac3_priv; struct eac3_info *info = mov->tracks[i].eac3_priv;
av_packet_unref(&info->pkt); av_packet_free(&info->pkt);
av_freep(&mov->tracks[i].eac3_priv); av_freep(&mov->tracks[i].eac3_priv);
} }
if (mov->tracks[i].vos_len) if (mov->tracks[i].vos_len)
@ -6537,6 +6555,10 @@ static int mov_init(AVFormatContext *s)
mov->nb_streams += mov->nb_meta_tmcd; mov->nb_streams += mov->nb_meta_tmcd;
} }
mov->pkt = av_packet_alloc();
if (!mov->pkt)
return AVERROR(ENOMEM);
// Reserve an extra stream for chapters for the case where chapters // Reserve an extra stream for chapters for the case where chapters
// are written in the trailer // are written in the trailer
mov->tracks = av_mallocz_array((mov->nb_streams + 1), sizeof(*mov->tracks)); mov->tracks = av_mallocz_array((mov->nb_streams + 1), sizeof(*mov->tracks));
@ -6644,6 +6666,11 @@ static int mov_init(AVFormatContext *s)
av_log(s, AV_LOG_ERROR, "VP8 muxing is currently not supported.\n"); av_log(s, AV_LOG_ERROR, "VP8 muxing is currently not supported.\n");
return AVERROR_PATCHWELCOME; return AVERROR_PATCHWELCOME;
} }
if (is_cover_image(st)) {
track->cover_image = av_packet_alloc();
if (!track->cover_image)
return AVERROR(ENOMEM);
}
} else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) { } else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
track->timescale = st->codecpar->sample_rate; track->timescale = st->codecpar->sample_rate;
if (!st->codecpar->frame_size && !av_get_bits_per_sample(st->codecpar->codec_id)) { if (!st->codecpar->frame_size && !av_get_bits_per_sample(st->codecpar->codec_id)) {

View File

@ -134,7 +134,7 @@ typedef struct MOVTrack {
uint32_t default_size; uint32_t default_size;
HintSampleQueue sample_queue; HintSampleQueue sample_queue;
AVPacket cover_image; AVPacket *cover_image;
AVIOContext *mdat_buf; AVIOContext *mdat_buf;
int64_t data_offset; int64_t data_offset;
@ -215,6 +215,8 @@ typedef struct MOVMuxContext {
int per_stream_grouping; int per_stream_grouping;
AVFormatContext *fc; AVFormatContext *fc;
AVPacket *pkt;
int use_editlist; int use_editlist;
float gamma; float gamma;

View File

@ -408,7 +408,7 @@ int ff_mov_add_hinted_packet(AVFormatContext *s, AVPacket *pkt,
uint8_t *buf = NULL; uint8_t *buf = NULL;
int size; int size;
AVIOContext *hintbuf = NULL; AVIOContext *hintbuf = NULL;
AVPacket hint_pkt; AVPacket *hint_pkt = mov->pkt;
int ret = 0, count; int ret = 0, count;
if (!rtp_ctx) if (!rtp_ctx)
@ -437,21 +437,22 @@ int ff_mov_add_hinted_packet(AVFormatContext *s, AVPacket *pkt,
/* Open a buffer for writing the hint */ /* Open a buffer for writing the hint */
if ((ret = avio_open_dyn_buf(&hintbuf)) < 0) if ((ret = avio_open_dyn_buf(&hintbuf)) < 0)
goto done; goto done;
av_init_packet(&hint_pkt); av_packet_unref(hint_pkt);
count = write_hint_packets(hintbuf, buf, size, trk, &hint_pkt.dts); count = write_hint_packets(hintbuf, buf, size, trk, &hint_pkt->dts);
av_freep(&buf); av_freep(&buf);
/* Write the hint data into the hint track */ /* Write the hint data into the hint track */
hint_pkt.size = size = avio_close_dyn_buf(hintbuf, &buf); hint_pkt->size = size = avio_close_dyn_buf(hintbuf, &buf);
hint_pkt.data = buf; hint_pkt->data = buf;
hint_pkt.pts = hint_pkt.dts; hint_pkt->pts = hint_pkt->dts;
hint_pkt.stream_index = track_index; hint_pkt->stream_index = track_index;
if (pkt->flags & AV_PKT_FLAG_KEY) if (pkt->flags & AV_PKT_FLAG_KEY)
hint_pkt.flags |= AV_PKT_FLAG_KEY; hint_pkt->flags |= AV_PKT_FLAG_KEY;
if (count > 0) if (count > 0)
ff_mov_write_packet(s, &hint_pkt); ff_mov_write_packet(s, hint_pkt);
done: done:
av_free(buf); av_free(buf);
av_packet_unref(hint_pkt);
sample_queue_retain(&trk->sample_queue); sample_queue_retain(&trk->sample_queue);
return ret; return ret;
} }