History

avfilter/src_movie: switch to activate

Paul B Mahol committed 3 years ago

Commit 5fe6a0e1 parent 3546f701 file · +122 −143Browse files

Unified · Split

libavfilter/src_movie.c+122−143
@@ -45,6 +45,7 @@ · 44 unmodified lines
4545
4646 #include "audio.h"
4747 #include "avfilter.h"
48+#include "filters.h"
4849 #include "formats.h"
4950 #include "internal.h"
5051 #include "video.h"
@@ -54,6 +55,8 @@ typedef struct MovieStream { · 3 unmodified lines
5455 AVCodecContext *codec_ctx;
5556 int64_t discontinuity_threshold;
5657 int64_t last_pts;
58+ AVFrame *frame;
59+ int eof;
5760 } MovieStream;
5861
5962 typedef struct MovieContext {
@@ -70,8 +73,10 @@ typedef struct MovieContext { · 10 unmodified lines
7073 int64_t ts_offset;
7174 int dec_threads;
7275
76+ AVPacket *pkt;
7377 AVFormatContext *format_ctx;
7478
79+ int eof;
7580 int max_stream_index; /**< max stream # actually used for output */
7681 MovieStream *st; /**< array of all streams, one per output */
7782 int *out_index; /**< stream number -> output number map, or -1 */
@@ -99,7 +104,6 @@ static const AVOption movie_options[]= { · 21 unmodified lines
99104 };
100105
101106 static int movie_config_output_props(AVFilterLink *outlink);
102−static int movie_request_frame(AVFilterLink *outlink);
103107
104108 static AVStream *find_stream(void *log, AVFormatContext *avf, const char *spec)
105109 {
@@ -330,6 +334,9 @@ static av_cold int movie_common_init(AVFilterContext *ctx) · 224 unmodified lines
330334 for (i = 0; i < movie->format_ctx->nb_streams; i++)
331335 movie->format_ctx->streams[i]->discard = AVDISCARD_ALL;
332336
337+ movie->pkt = av_packet_alloc();
338+ if (!movie->pkt)
339+ return AVERROR(ENOMEM);
333340 movie->st = av_calloc(nb_streams, sizeof(*movie->st));
334341 if (!movie->st)
335342 return AVERROR(ENOMEM);
@@ -347,6 +354,10 @@ static av_cold int movie_common_init(AVFilterContext *ctx) · 11 unmodified lines
347354 movie->max_stream_index = FFMAX(movie->max_stream_index, st->index);
348355 movie->st[i].discontinuity_threshold =
349356 av_rescale_q(movie->discontinuity_threshold, AV_TIME_BASE_Q, st->time_base);
357+
358+ movie->st[i].frame = av_frame_alloc();
359+ if (!movie->st[i].frame)
360+ return AVERROR(ENOMEM);
350361 }
351362 if (av_strtok(NULL, "+", &cursor))
352363 return AVERROR_BUG;
@@ -365,7 +376,6 @@ static av_cold int movie_common_init(AVFilterContext *ctx) · 12 unmodified lines
365376 if (!pad.name)
366377 return AVERROR(ENOMEM);
367378 pad.config_props = movie_config_output_props;
368− pad.request_frame = movie_request_frame;
369379 if ((ret = ff_append_outpad_free_name(ctx, &pad)) < 0)
370380 return ret;
371381 if ( movie->st[i].st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
@@ -394,7 +404,9 @@ static av_cold void movie_uninit(AVFilterContext *ctx) · 22 unmodified lines
394404 for (i = 0; i < ctx->nb_outputs; i++) {
395405 if (movie->st[i].st)
396406 avcodec_free_context(&movie->st[i].codec_ctx);
407+ av_frame_free(&movie->st[i].frame);
397408 }
409+ av_packet_free(&movie->pkt);
398410 av_freep(&movie->st);
399411 av_freep(&movie->out_index);
400412 if (movie->format_ctx)
@@ -460,32 +472,6 @@ static int movie_config_output_props(AVFilterLink *outlink) · 59 unmodified lines
460472 return 0;
461473 }
462474
463−static char *describe_frame_to_str(char *dst, size_t dst_size,
464− AVFrame *frame, enum AVMediaType frame_type,
465− AVFilterLink *link)
466−{
467− switch (frame_type) {
468− case AVMEDIA_TYPE_VIDEO:
469− snprintf(dst, dst_size,
470− "video pts:%s time:%s size:%dx%d aspect:%d/%d",
471− av_ts2str(frame->pts), av_ts2timestr(frame->pts, &link->time_base),
472− frame->width, frame->height,
473− frame->sample_aspect_ratio.num,
474− frame->sample_aspect_ratio.den);
475− break;
476− case AVMEDIA_TYPE_AUDIO:
477− snprintf(dst, dst_size,
478− "audio pts:%s time:%s samples:%d",
479− av_ts2str(frame->pts), av_ts2timestr(frame->pts, &link->time_base),
480− frame->nb_samples);
481− break;
482− default:
483− snprintf(dst, dst_size, "%s BUG", av_get_media_type_string(frame_type));
484− break;
485− }
486− return dst;
487−}
488−
489475 static int rewind_file(AVFilterContext *ctx)
490476 {
491477 MovieContext *movie = ctx->priv;
@@ -507,147 +493,138 @@ static int rewind_file(AVFilterContext *ctx) · 15 unmodified lines
507493 return 0;
508494 }
509495
510−static int movie_decode_packet(AVFilterContext *ctx)
496+static int flush_decoder(AVFilterContext *ctx, int i)
511497 {
512498 MovieContext *movie = ctx->priv;
513− AVPacket pkt = { 0 };
514− int pkt_out_id, ret;
515−
516− /* read a new packet from input stream */
517− ret = av_read_frame(movie->format_ctx, &pkt);
518− if (ret == AVERROR_EOF) {
519− /* EOF -> set all decoders for flushing */
520− for (int i = 0; i < ctx->nb_outputs; i++) {
521− ret = avcodec_send_packet(movie->st[i].codec_ctx, NULL);
522− if (ret < 0 && ret != AVERROR_EOF)
523− return ret;
524− }
525−
526− return 0;
527− } else if (ret < 0)
528− return ret;
499+ AVCodecContext *dec = movie->st[i].codec_ctx;
529500
530− /* send the packet to its decoder, if any */
531− pkt_out_id = pkt.stream_index > movie->max_stream_index ? -1 :
532− movie->out_index[pkt.stream_index];
533− if (pkt_out_id >= 0) {
534− pkt.opaque = ctx->outputs[pkt_out_id];
535− ret = avcodec_send_packet(movie->st[pkt_out_id].codec_ctx, &pkt);
536− }
537− av_packet_unref(&pkt);
538−
539− return ret;
501+ return avcodec_send_packet(dec, NULL);
540502 }
541503
542−/**
543− * Try to push a frame to the requested output.
544− *
545− * @param ctx filter context
546− * @param out_id number of output where a frame is wanted;
547− * @return 0 if a frame was pushed on the requested output,
548− * AVERROR(EAGAIN) if the decoder requires more input
549− * AVERROR(EOF) if the decoder has been completely flushed
550− * <0 AVERROR code
551− */
552−static int movie_push_frame(AVFilterContext *ctx, unsigned out_id)
504+static int decode_packet(AVFilterContext *ctx, int i)
553505 {
554− MovieContext *movie = ctx->priv;
555− MovieStream *st = &movie->st[out_id];
556− AVFilterLink *outlink = ctx->outputs[out_id];
557− AVFrame *frame;
558− int ret;
559−
560− frame = av_frame_alloc();
561− if (!frame)
562− return AVERROR(ENOMEM);
563−
564− ret = avcodec_receive_frame(st->codec_ctx, frame);
565− if (ret < 0) {
566− if (ret != AVERROR_EOF && ret != AVERROR(EAGAIN))
567− av_log(ctx, AV_LOG_WARNING, "Decode error: %s\n", av_err2str(ret));
568−
569− av_frame_free(&frame);
570− return ret;
506+ AVFilterLink *outlink = ctx->outputs[i];
507+ MovieContext *movie = ctx->priv;
508+ MovieStream *st = &movie->st[i];
509+ AVCodecContext *dec = movie->st[i].codec_ctx;
510+ AVFrame *frame = movie->st[i].frame;
511+ AVPacket *pkt = movie->pkt;
512+ int ret = 0;
513+
514+ // submit the packet to the decoder
515+ if (!movie->eof) {
516+ ret = avcodec_send_packet(dec, pkt);
517+ if (ret < 0)
518+ return ret;
571519 }
572520
573− frame->pts = frame->best_effort_timestamp;
574− if (frame->pts != AV_NOPTS_VALUE) {
575− if (movie->ts_offset)
576− frame->pts += av_rescale_q_rnd(movie->ts_offset, AV_TIME_BASE_Q, outlink->time_base, AV_ROUND_UP);
577− if (st->discontinuity_threshold) {
578− if (st->last_pts != AV_NOPTS_VALUE) {
579− int64_t diff = frame->pts - st->last_pts;
580− if (diff < 0 || diff > st->discontinuity_threshold) {
581− av_log(ctx, AV_LOG_VERBOSE, "Discontinuity in stream:%d diff:%"PRId64"\n", out_id, diff);
582− movie->ts_offset += av_rescale_q_rnd(-diff, outlink->time_base, AV_TIME_BASE_Q, AV_ROUND_UP);
583− frame->pts -= diff;
521+ // get all the available frames from the decoder
522+ if (ret >= 0) {
523+ ret = avcodec_receive_frame(dec, frame);
524+ if (ret < 0) {
525+ // those two return values are special and mean there is no output
526+ // frame available, but there were no errors during decoding
527+ if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN))
528+ return 0;
529+ return ret;
530+ }
531+
532+ frame->pts = frame->best_effort_timestamp;
533+ if (frame->pts != AV_NOPTS_VALUE) {
534+ if (movie->ts_offset)
535+ frame->pts += av_rescale_q_rnd(movie->ts_offset, AV_TIME_BASE_Q, outlink->time_base, AV_ROUND_UP);
536+ if (st->discontinuity_threshold) {
537+ if (st->last_pts != AV_NOPTS_VALUE) {
538+ int64_t diff = frame->pts - st->last_pts;
539+ if (diff < 0 || diff > st->discontinuity_threshold) {
540+ av_log(ctx, AV_LOG_VERBOSE, "Discontinuity in stream:%d diff:%"PRId64"\n", i, diff);
541+ movie->ts_offset += av_rescale_q_rnd(-diff, outlink->time_base, AV_TIME_BASE_Q, AV_ROUND_UP);
542+ frame->pts -= diff;
543+ }
584544 }
585545 }
546+ st->last_pts = frame->pts;
586547 }
587− st->last_pts = frame->pts;
588− }
589− ff_dlog(ctx, "movie_push_frame(): file:'%s' %s\n", movie->file_name,
590− describe_frame_to_str((char[1024]){0}, 1024, frame,
591− st->st->codecpar->codec_type, outlink));
592−
593− if (st->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
594− if (frame->format != outlink->format) {
595− av_log(ctx, AV_LOG_ERROR, "Format changed %s -> %s, discarding frame\n",
596− av_get_pix_fmt_name(outlink->format),
597− av_get_pix_fmt_name(frame->format)
598− );
599− av_frame_free(&frame);
600− return 0;
601− }
548+ ret = ff_filter_frame(outlink, av_frame_clone(frame));
549+ if (ret < 0)
550+ return ret;
551+ if (ret == 0)
552+ return 1;
602553 }
603− ret = ff_filter_frame(outlink, frame);
604554
605− if (ret < 0)
606− return ret;
607555 return 0;
608556 }
609557
610−static int movie_request_frame(AVFilterLink *outlink)
558+static int activate(AVFilterContext *ctx)
611559 {
612− AVFilterContext *ctx = outlink->src;
613− MovieContext *movie = ctx->priv;
614− unsigned out_id = FF_OUTLINK_IDX(outlink);
560+ MovieContext *movie = ctx->priv;
561+ int wanted = 0, ret;
615562
616− while (1) {
617− int got_eagain = 0, got_eof = 0;
618− int ret = 0;
563+ for (int i = 0; i < ctx->nb_outputs; i++) {
564+ if (ff_outlink_frame_wanted(ctx->outputs[i]))
565+ wanted++;
566+ }
619567
620− /* check all decoders for available output */
621− for (int i = 0; i < ctx->nb_outputs; i++) {
622− ret = movie_push_frame(ctx, i);
623− if (ret == AVERROR(EAGAIN))
624− got_eagain++;
625− else if (ret == AVERROR_EOF)
626− got_eof++;
627− else if (ret < 0)
628− return ret;
629− else if (i == out_id)
630− return 0;
568+ if (wanted == 0)
569+ return FFERROR_NOT_READY;
570+
571+ if (!movie->eof) {
572+ ret = av_read_frame(movie->format_ctx, movie->pkt);
573+ if (ret < 0) {
574+ movie->eof = 1;
575+ for (int i = 0; i < ctx->nb_outputs; i++)
576+ flush_decoder(ctx, i);
577+ ff_filter_set_ready(ctx, 100);
578+ return 0;
579+ } else {
580+ int pkt_out_id = movie->pkt->stream_index > movie->max_stream_index ? -1 :
581+ movie->out_index[movie->pkt->stream_index];
582+
583+ if (pkt_out_id >= 0) {
584+ movie->pkt->opaque = ctx->outputs[pkt_out_id];
585+ ret = decode_packet(ctx, pkt_out_id);
586+ }
587+ av_packet_unref(movie->pkt);
588+ ff_filter_set_ready(ctx, 100);
589+ return (ret <= 0) ? ret : 0;
631590 }
591+ } else {
592+ int nb_eofs = 0;
632593
633− if (got_eagain) {
634− /* all decoders require more input -> read a new packet */
635− ret = movie_decode_packet(ctx);
594+ for (int i = 0; i < ctx->nb_outputs; i++) {
595+ if (!movie->st[i].eof) {
596+ ret = decode_packet(ctx, i);
597+ if (ret <= 0)
598+ movie->st[i].eof = 1;
599+ }
600+ nb_eofs += movie->st[i].eof == 1;
601+ }
602+ if (nb_eofs == ctx->nb_outputs && movie->loop_count != 1) {
603+ ret = rewind_file(ctx);
636604 if (ret < 0)
637605 return ret;
638− } else if (got_eof) {
639− /* all decoders flushed */
640− if (movie->loop_count != 1) {
641− ret = rewind_file(ctx);
642− if (ret < 0)
643− return ret;
644− movie->loop_count -= movie->loop_count > 1;
645− av_log(ctx, AV_LOG_VERBOSE, "Stream finished, looping.\n");
646− continue;
606+ movie->loop_count -= movie->loop_count > 1;
607+ av_log(ctx, AV_LOG_VERBOSE, "Stream finished, looping.\n");
608+ ff_filter_set_ready(ctx, 100);
609+ for (int i = 0; i < ctx->nb_outputs; i++)
610+ movie->st[i].eof = 0;
611+ movie->eof = 0;
612+ return 0;
613+ } else {
614+ for (int i = 0; i < ctx->nb_outputs; i++) {
615+ if (movie->st[i].eof) {
616+ ff_outlink_set_status(ctx->outputs[i], AVERROR_EOF, movie->st[i].last_pts);
617+ nb_eofs++;
618+ }
647619 }
648− return AVERROR_EOF;
649620 }
621+
622+ if (nb_eofs < ctx->nb_outputs)
623+ ff_filter_set_ready(ctx, 100);
624+ return 0;
650625 }
626+
627+ return FFERROR_NOT_READY;
651628 }
652629
653630 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
@@ -702,6 +679,7 @@ const AVFilter ff_avsrc_movie = { · 48 unmodified lines
702679 .priv_size = sizeof(MovieContext),
703680 .priv_class = &movie_class,
704681 .init = movie_common_init,
682+ .activate = activate,
705683 .uninit = movie_uninit,
706684 FILTER_QUERY_FUNC(movie_query_formats),
707685
@@ -721,6 +699,7 @@ const AVFilter ff_avsrc_amovie = { · 13 unmodified lines
721699 .priv_class = &movie_class,
722700 .priv_size = sizeof(MovieContext),
723701 .init = movie_common_init,
702+ .activate = activate,
724703 .uninit = movie_uninit,
725704 FILTER_QUERY_FUNC(movie_query_formats),
726705

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