History

avfilter/src_movie: Remove unnecessary secondary AVPacket

The movie and amovie filters currently use two packets. One of the two, pkt0, is the owner of the returned packet; it is also the destination packet for av_read_frame(). The other one pkt is initially (i.e. after av_read_frame()) a copy of pkt0; copy means that the contents of both are absolutely the same: They both point to the same AVBufferRef and the same side data. This violation of the refcounted packet API is only possible because pkt is not considered to own its data. Only pkt0 is ever unreferenced. The reason for pkt's existence seems to be historic: The API used for decoding audio (namely avcodec_decode_audio4()) could consume frames partially, i.e. it could return multiple frames for one packet and therefore it returned how much of the input buffer had been consumed. The caller was then supposed to update the packet's data and size pointer to reflect this and call avcodec_decode_audio4() again with the updated packet to get the next frame. But before the introduction of refcounted AVPackets where knowledge and responsibility about what to free lies with the underlying AVBuffer such a procedure required a spare packet (or one would need to record the original data and size fields separately to restore them before freeing the packet; notice that this code has been written when AVPackets still had a destruct field). But these times are long gone, so just remove the secondary AVPacket.

Andreas Rheinhardt committed 6 years ago

Commit 41e00581 parent c97d91e1 file · +6 −15Browse files

Showing the changes to libavfilter (1 of 1 file). Show all

Unified · Split

libavfilter/src_movie.c+6−15
@@ -71,7 +71,7 @@ typedef struct MovieContext { · 70 unmodified lines
71 71
72 AVFormatContext *format_ctx;72 AVFormatContext *format_ctx;
73 int eof;73 int eof;
74 AVPacket pkt, pkt0;74 AVPacket pkt;
75 75
76 int max_stream_index; /**< max stream # actually used for output */76 int max_stream_index; /**< max stream # actually used for output */
77 MovieStream *st; /**< array of all streams, one per output */77 MovieStream *st; /**< array of all streams, one per output */
@@ -493,25 +493,21 @@ static int movie_push_frame(AVFilterContext *ctx, unsigned out_id) · 415 unmodified lines
493 pkt->stream_index = movie->st[out_id].st->index;493 pkt->stream_index = movie->st[out_id].st->index;
494 /* packet is already ready for flushing */494 /* packet is already ready for flushing */
495 } else {495 } else {
496 ret = av_read_frame(movie->format_ctx, &movie->pkt0);496 ret = av_read_frame(movie->format_ctx, pkt);
497 if (ret < 0) {497 if (ret < 0) {
498 *pkt = movie->pkt0;
499 if (ret == AVERROR_EOF) {498 if (ret == AVERROR_EOF) {
500 movie->eof = 1;499 movie->eof = 1;
501 return 0; /* start flushing */500 return 0; /* start flushing */
502 }501 }
503 return ret;502 return ret;
504 }503 }
505 *pkt = movie->pkt0;
506 }504 }
507 }505 }
508 506
509 pkt_out_id = pkt->stream_index > movie->max_stream_index ? -1 :507 pkt_out_id = pkt->stream_index > movie->max_stream_index ? -1 :
510 movie->out_index[pkt->stream_index];508 movie->out_index[pkt->stream_index];
511 if (pkt_out_id < 0) {509 if (pkt_out_id < 0) {
512 av_packet_unref(&movie->pkt0);510 av_packet_unref(pkt);
513 pkt->size = 0; /* ready for next run */
514 pkt->data = NULL;
515 return 0;511 return 0;
516 }512 }
517 st = &movie->st[pkt_out_id];513 st = &movie->st[pkt_out_id];
@@ -536,9 +532,7 @@ static int movie_push_frame(AVFilterContext *ctx, unsigned out_id) · 18 unmodified lines
536 if (ret < 0) {532 if (ret < 0) {
537 av_log(ctx, AV_LOG_WARNING, "Decode error: %s\n", av_err2str(ret));533 av_log(ctx, AV_LOG_WARNING, "Decode error: %s\n", av_err2str(ret));
538 av_frame_free(&frame);534 av_frame_free(&frame);
539 av_packet_unref(&movie->pkt0);535 av_packet_unref(pkt);
540 movie->pkt.size = 0;
541 movie->pkt.data = NULL;
542 return 0;536 return 0;
543 }537 }
544 if (!ret || st->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)538 if (!ret || st->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
@@ -546,11 +540,8 @@ static int movie_push_frame(AVFilterContext *ctx, unsigned out_id) · 1 unmodified line
546 540
547 pkt->data += ret;541 pkt->data += ret;
548 pkt->size -= ret;542 pkt->size -= ret;
549 if (pkt->size <= 0) {543 if (pkt->size <= 0)
550 av_packet_unref(&movie->pkt0);544 av_packet_unref(pkt);
551 pkt->size = 0; /* ready for next run */
552 pkt->data = NULL;
553 }
554 if (!got_frame) {545 if (!got_frame) {
555 if (!ret)546 if (!ret)
556 st->done = 1;547 st->done = 1;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t