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
7171
7272 AVFormatContext *format_ctx;
7373 int eof;
74− AVPacket pkt, pkt0;
74+ AVPacket pkt;
7575
7676 int max_stream_index; /**< max stream # actually used for output */
7777 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
493493 pkt->stream_index = movie->st[out_id].st->index;
494494 /* packet is already ready for flushing */
495495 } else {
496− ret = av_read_frame(movie->format_ctx, &movie->pkt0);
496+ ret = av_read_frame(movie->format_ctx, pkt);
497497 if (ret < 0) {
498− *pkt = movie->pkt0;
499498 if (ret == AVERROR_EOF) {
500499 movie->eof = 1;
501500 return 0; /* start flushing */
502501 }
503502 return ret;
504503 }
505− *pkt = movie->pkt0;
506504 }
507505 }
508506
509507 pkt_out_id = pkt->stream_index > movie->max_stream_index ? -1 :
510508 movie->out_index[pkt->stream_index];
511509 if (pkt_out_id < 0) {
512− av_packet_unref(&movie->pkt0);
513− pkt->size = 0; /* ready for next run */
514− pkt->data = NULL;
510+ av_packet_unref(pkt);
515511 return 0;
516512 }
517513 st = &movie->st[pkt_out_id];
@@ -536,9 +532,7 @@ static int movie_push_frame(AVFilterContext *ctx, unsigned out_id) · 18 unmodified lines
536532 if (ret < 0) {
537533 av_log(ctx, AV_LOG_WARNING, "Decode error: %s\n", av_err2str(ret));
538534 av_frame_free(&frame);
539− av_packet_unref(&movie->pkt0);
540− movie->pkt.size = 0;
541− movie->pkt.data = NULL;
535+ av_packet_unref(pkt);
542536 return 0;
543537 }
544538 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
546540
547541 pkt->data += ret;
548542 pkt->size -= ret;
549− if (pkt->size <= 0) {
550− av_packet_unref(&movie->pkt0);
551− pkt->size = 0; /* ready for next run */
552− pkt->data = NULL;
553− }
543+ if (pkt->size <= 0)
544+ av_packet_unref(pkt);
554545 if (!got_frame) {
555546 if (!ret)
556547 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