avformat/mux: use av_packet_alloc() to allocate packets

Signed-off-by: James Almer <jamrial@gmail.com>
This commit is contained in:
James Almer
2021-01-30 15:04:24 -03:00
parent 2995a1f294
commit 9066969713
4 changed files with 32 additions and 20 deletions

View File

@@ -92,6 +92,11 @@ struct AVFormatInternal {
*/ */
struct PacketList *parse_queue; struct PacketList *parse_queue;
struct PacketList *parse_queue_end; struct PacketList *parse_queue_end;
/**
* Used to hold temporary packets.
*/
AVPacket *pkt;
/** /**
* Remaining size available for raw_packet_buffer, in bytes. * Remaining size available for raw_packet_buffer, in bytes.
*/ */

View File

@@ -1211,7 +1211,7 @@ static int write_packets_common(AVFormatContext *s, AVPacket *pkt, int interleav
int av_write_frame(AVFormatContext *s, AVPacket *in) int av_write_frame(AVFormatContext *s, AVPacket *in)
{ {
AVPacket local_pkt, *pkt = &local_pkt; AVPacket *pkt = s->internal->pkt;
int ret; int ret;
if (!in) { if (!in) {
@@ -1232,6 +1232,7 @@ int av_write_frame(AVFormatContext *s, AVPacket *in)
* The following avoids copying in's data unnecessarily. * The following avoids copying in's data unnecessarily.
* Copying side data is unavoidable as a bitstream filter * Copying side data is unavoidable as a bitstream filter
* may change it, e.g. free it on errors. */ * may change it, e.g. free it on errors. */
av_packet_unref(pkt);
pkt->buf = NULL; pkt->buf = NULL;
pkt->data = in->data; pkt->data = in->data;
pkt->size = in->size; pkt->size = in->size;
@@ -1273,14 +1274,14 @@ int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt)
int av_write_trailer(AVFormatContext *s) int av_write_trailer(AVFormatContext *s)
{ {
int i, ret1, ret = 0; int i, ret1, ret = 0;
AVPacket pkt = {0}; AVPacket *pkt = s->internal->pkt;
av_init_packet(&pkt);
av_packet_unref(pkt);
for (i = 0; i < s->nb_streams; i++) { for (i = 0; i < s->nb_streams; i++) {
if (s->streams[i]->internal->bsfc) { if (s->streams[i]->internal->bsfc) {
ret1 = write_packets_from_bsfs(s, s->streams[i], &pkt, 1/*interleaved*/); ret1 = write_packets_from_bsfs(s, s->streams[i], pkt, 1/*interleaved*/);
if (ret1 < 0) if (ret1 < 0)
av_packet_unref(&pkt); av_packet_unref(pkt);
if (ret >= 0) if (ret >= 0)
ret = ret1; ret = ret1;
} }
@@ -1354,7 +1355,7 @@ static void uncoded_frame_free(void *unused, uint8_t *data)
static int write_uncoded_frame_internal(AVFormatContext *s, int stream_index, static int write_uncoded_frame_internal(AVFormatContext *s, int stream_index,
AVFrame *frame, int interleaved) AVFrame *frame, int interleaved)
{ {
AVPacket pkt, *pktp; AVPacket *pkt = s->internal->pkt;
av_assert0(s->oformat); av_assert0(s->oformat);
if (!s->oformat->write_uncoded_frame) { if (!s->oformat->write_uncoded_frame) {
@@ -1363,18 +1364,17 @@ static int write_uncoded_frame_internal(AVFormatContext *s, int stream_index,
} }
if (!frame) { if (!frame) {
pktp = NULL; pkt = NULL;
} else { } else {
size_t bufsize = sizeof(frame) + AV_INPUT_BUFFER_PADDING_SIZE; size_t bufsize = sizeof(frame) + AV_INPUT_BUFFER_PADDING_SIZE;
AVFrame **framep = av_mallocz(bufsize); AVFrame **framep = av_mallocz(bufsize);
if (!framep) if (!framep)
goto fail; goto fail;
pktp = &pkt; av_packet_unref(pkt);
av_init_packet(&pkt); pkt->buf = av_buffer_create((void *)framep, bufsize,
pkt.buf = av_buffer_create((void *)framep, bufsize,
uncoded_frame_free, NULL, 0); uncoded_frame_free, NULL, 0);
if (!pkt.buf) { if (!pkt->buf) {
av_free(framep); av_free(framep);
fail: fail:
av_frame_free(&frame); av_frame_free(&frame);
@@ -1382,17 +1382,17 @@ static int write_uncoded_frame_internal(AVFormatContext *s, int stream_index,
} }
*framep = frame; *framep = frame;
pkt.data = (void *)framep; pkt->data = (void *)framep;
pkt.size = sizeof(frame); pkt->size = sizeof(frame);
pkt.pts = pkt->pts =
pkt.dts = frame->pts; pkt->dts = frame->pts;
pkt.duration = frame->pkt_duration; pkt->duration = frame->pkt_duration;
pkt.stream_index = stream_index; pkt->stream_index = stream_index;
pkt.flags |= AV_PKT_FLAG_UNCODED_FRAME; pkt->flags |= AV_PKT_FLAG_UNCODED_FRAME;
} }
return interleaved ? av_interleaved_write_frame(s, pktp) : return interleaved ? av_interleaved_write_frame(s, pkt) :
av_write_frame(s, pktp); av_write_frame(s, pkt);
} }
int av_write_uncoded_frame(AVFormatContext *s, int stream_index, int av_write_uncoded_frame(AVFormatContext *s, int stream_index,

View File

@@ -220,6 +220,12 @@ AVFormatContext *avformat_alloc_context(void)
av_free(ic); av_free(ic);
return NULL; return NULL;
} }
internal->pkt = av_packet_alloc();
if (!internal->pkt) {
av_free(internal);
av_free(ic);
return NULL;
}
avformat_get_context_defaults(ic); avformat_get_context_defaults(ic);
ic->internal = internal; ic->internal = internal;
ic->internal->offset = AV_NOPTS_VALUE; ic->internal->offset = AV_NOPTS_VALUE;

View File

@@ -4451,6 +4451,7 @@ void avformat_free_context(AVFormatContext *s)
av_freep(&s->chapters); av_freep(&s->chapters);
av_dict_free(&s->metadata); av_dict_free(&s->metadata);
av_dict_free(&s->internal->id3v2_meta); av_dict_free(&s->internal->id3v2_meta);
av_packet_free(&s->internal->pkt);
av_freep(&s->streams); av_freep(&s->streams);
flush_packet_queue(s); flush_packet_queue(s);
av_freep(&s->internal); av_freep(&s->internal);