History

lavfi/src_movie: switch to new decoding API

Anton Khirnov committed 6 years ago

Commit 8918a3d1 parent ce0f5391 file · +81 −90Browse files

Unified · Split

libavfilter/src_movie.c+81−90
@@ -51,7 +51,6 @@ · 50 unmodified lines
5151 typedef struct MovieStream {
5252 AVStream *st;
5353 AVCodecContext *codec_ctx;
54− int done;
5554 int64_t discontinuity_threshold;
5655 int64_t last_pts;
5756 } MovieStream;
@@ -70,8 +69,6 @@ typedef struct MovieContext { · 12 unmodified lines
7069 int64_t ts_offset;
7170
7271 AVFormatContext *format_ctx;
73− int eof;
74− AVPacket pkt;
7572
7673 int max_stream_index; /**< max stream # actually used for output */
7774 MovieStream *st; /**< array of all streams, one per output */
@@ -172,7 +169,6 @@ static int open_stream(AVFilterContext *ctx, MovieStream *st) · 94 unmodified lines
172169 if (ret < 0)
173170 return ret;
174171
175− st->codec_ctx->refcounted_frames = 1;
176172 st->codec_ctx->thread_count = ff_filter_get_nb_threads(ctx);
177173
178174 if ((ret = avcodec_open2(st->codec_ctx, codec, NULL)) < 0) {
@@ -344,7 +340,6 @@ static av_cold void movie_uninit(AVFilterContext *ctx) · 165 unmodified lines
344340 }
345341 av_freep(&movie->st);
346342 av_freep(&movie->out_index);
347− av_packet_unref(&movie->pkt);
348343 if (movie->format_ctx)
349344 avformat_close_input(&movie->format_ctx);
350345 }
@@ -451,103 +446,69 @@ static int rewind_file(AVFilterContext *ctx) · 100 unmodified lines
451446
452447 for (i = 0; i < ctx->nb_outputs; i++) {
453448 avcodec_flush_buffers(movie->st[i].codec_ctx);
454− movie->st[i].done = 0;
455449 }
456− movie->eof = 0;
457450 return 0;
458451 }
459452
453+static int movie_decode_packet(AVFilterContext *ctx)
454+{
455+ MovieContext *movie = ctx->priv;
456+ AVPacket pkt = { 0 };
457+ int pkt_out_id, ret;
458+
459+ /* read a new packet from input stream */
460+ ret = av_read_frame(movie->format_ctx, &pkt);
461+ if (ret == AVERROR_EOF) {
462+ /* EOF -> set all decoders for flushing */
463+ for (int i = 0; i < ctx->nb_outputs; i++) {
464+ ret = avcodec_send_packet(movie->st[i].codec_ctx, NULL);
465+ if (ret < 0 && ret != AVERROR_EOF)
466+ return ret;
467+ }
468+
469+ return 0;
470+ } else if (ret < 0)
471+ return ret;
472+
473+ /* send the packet to its decoder, if any */
474+ pkt_out_id = pkt.stream_index > movie->max_stream_index ? -1 :
475+ movie->out_index[pkt.stream_index];
476+ if (pkt_out_id >= 0)
477+ ret = avcodec_send_packet(movie->st[pkt_out_id].codec_ctx, &pkt);
478+ av_packet_unref(&pkt);
479+
480+ return ret;
481+}
482+
460483 /**
461484 * Try to push a frame to the requested output.
462485 *
463486 * @param ctx filter context
464487 * @param out_id number of output where a frame is wanted;
465− * if the frame is read from file, used to set the return value;
466− * if the codec is being flushed, flush the corresponding stream
467− * @return 1 if a frame was pushed on the requested output,
468− * 0 if another attempt is possible,
469− * <0 AVERROR code
488+ * @return 0 if a frame was pushed on the requested output,
489+ * AVERROR(EAGAIN) if the decoder requires more input
490+ * AVERROR(EOF) if the decoder has been completely flushed
491+ * <0 AVERROR code
470492 */
471493 static int movie_push_frame(AVFilterContext *ctx, unsigned out_id)
472494 {
473− MovieContext *movie = ctx->priv;
474− AVPacket *pkt = &movie->pkt;
475− enum AVMediaType frame_type;
476− MovieStream *st;
477− int ret, got_frame = 0, pkt_out_id;
478− AVFilterLink *outlink;
495+ MovieContext *movie = ctx->priv;
496+ MovieStream *st = &movie->st[out_id];
497+ AVFilterLink *outlink = ctx->outputs[out_id];
479498 AVFrame *frame;
480−
481− if (!pkt->size) {
482− if (movie->eof) {
483− if (movie->st[out_id].done) {
484− if (movie->loop_count != 1) {
485− ret = rewind_file(ctx);
486− if (ret < 0)
487− return ret;
488− movie->loop_count -= movie->loop_count > 1;
489− av_log(ctx, AV_LOG_VERBOSE, "Stream finished, looping.\n");
490− return 0; /* retry */
491− }
492− return AVERROR_EOF;
493− }
494− pkt->stream_index = movie->st[out_id].st->index;
495− /* packet is already ready for flushing */
496− } else {
497− ret = av_read_frame(movie->format_ctx, pkt);
498− if (ret < 0) {
499− if (ret == AVERROR_EOF) {
500− movie->eof = 1;
501− return 0; /* start flushing */
502− }
503− return ret;
504− }
505− }
506− }
507−
508− pkt_out_id = pkt->stream_index > movie->max_stream_index ? -1 :
509− movie->out_index[pkt->stream_index];
510− if (pkt_out_id < 0) {
511− av_packet_unref(pkt);
512− return 0;
513− }
514− st = &movie->st[pkt_out_id];
515− outlink = ctx->outputs[pkt_out_id];
499+ int ret;
516500
517501 frame = av_frame_alloc();
518502 if (!frame)
519503 return AVERROR(ENOMEM);
520504
521− frame_type = st->st->codecpar->codec_type;
522− switch (frame_type) {
523− case AVMEDIA_TYPE_VIDEO:
524− ret = avcodec_decode_video2(st->codec_ctx, frame, &got_frame, pkt);
525− break;
526− case AVMEDIA_TYPE_AUDIO:
527− ret = avcodec_decode_audio4(st->codec_ctx, frame, &got_frame, pkt);
528− break;
529− default:
530− ret = AVERROR(ENOSYS);
531− break;
532− }
505+ ret = avcodec_receive_frame(st->codec_ctx, frame);
533506 if (ret < 0) {
534− av_log(ctx, AV_LOG_WARNING, "Decode error: %s\n", av_err2str(ret));
535− av_frame_free(&frame);
536− av_packet_unref(pkt);
537− return 0;
538− }
539− if (!ret || st->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
540− ret = pkt->size;
541−
542− pkt->data += ret;
543− pkt->size -= ret;
544− if (pkt->size <= 0)
545− av_packet_unref(pkt);
546− if (!got_frame) {
547− if (!ret)
548− st->done = 1;
507+ if (ret != AVERROR_EOF && ret != AVERROR(EAGAIN))
508+ av_log(ctx, AV_LOG_WARNING, "Decode error: %s\n", av_err2str(ret));
509+
549510 av_frame_free(&frame);
550− return 0;
511+ return ret;
551512 }
552513
553514 frame->pts = frame->best_effort_timestamp;
@@ -558,7 +519,7 @@ static int movie_push_frame(AVFilterContext *ctx, unsigned out_id) · 4 unmodified lines
558519 if (st->last_pts != AV_NOPTS_VALUE) {
559520 int64_t diff = frame->pts - st->last_pts;
560521 if (diff < 0 || diff > st->discontinuity_threshold) {
561− av_log(ctx, AV_LOG_VERBOSE, "Discontinuity in stream:%d diff:%"PRId64"\n", pkt_out_id, diff);
522+ av_log(ctx, AV_LOG_VERBOSE, "Discontinuity in stream:%d diff:%"PRId64"\n", out_id, diff);
562523 movie->ts_offset += av_rescale_q_rnd(-diff, outlink->time_base, AV_TIME_BASE_Q, AV_ROUND_UP);
563524 frame->pts -= diff;
564525 }
@@ -567,7 +528,8 @@ static int movie_push_frame(AVFilterContext *ctx, unsigned out_id) · 2 unmodified lines
567528 st->last_pts = frame->pts;
568529 }
569530 ff_dlog(ctx, "movie_push_frame(): file:'%s' %s\n", movie->file_name,
570− describe_frame_to_str((char[1024]){0}, 1024, frame, frame_type, outlink));
531+ describe_frame_to_str((char[1024]){0}, 1024, frame,
532+ st->st->codecpar->codec_type, outlink));
571533
572534 if (st->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
573535 if (frame->format != outlink->format) {
@@ -583,19 +545,49 @@ static int movie_push_frame(AVFilterContext *ctx, unsigned out_id) · 9 unmodified lines
583545
584546 if (ret < 0)
585547 return ret;
586− return pkt_out_id == out_id;
548+ return 0;
587549 }
588550
589551 static int movie_request_frame(AVFilterLink *outlink)
590552 {
591553 AVFilterContext *ctx = outlink->src;
554+ MovieContext *movie = ctx->priv;
592555 unsigned out_id = FF_OUTLINK_IDX(outlink);
593− int ret;
594556
595557 while (1) {
596− ret = movie_push_frame(ctx, out_id);
597− if (ret)
598− return FFMIN(ret, 0);
558+ int got_eagain = 0, got_eof = 0;
559+ int ret = 0;
560+
561+ /* check all decoders for available output */
562+ for (int i = 0; i < ctx->nb_outputs; i++) {
563+ ret = movie_push_frame(ctx, i);
564+ if (ret == AVERROR(EAGAIN))
565+ got_eagain++;
566+ else if (ret == AVERROR_EOF)
567+ got_eof++;
568+ else if (ret < 0)
569+ return ret;
570+ else if (i == out_id)
571+ return 0;
572+ }
573+
574+ if (got_eagain) {
575+ /* all decoders require more input -> read a new packet */
576+ ret = movie_decode_packet(ctx);
577+ if (ret < 0)
578+ return ret;
579+ } else if (got_eof) {
580+ /* all decoders flushed */
581+ if (movie->loop_count != 1) {
582+ ret = rewind_file(ctx);
583+ if (ret < 0)
584+ return ret;
585+ movie->loop_count -= movie->loop_count > 1;
586+ av_log(ctx, AV_LOG_VERBOSE, "Stream finished, looping.\n");
587+ continue;
588+ }
589+ return AVERROR_EOF;
590+ }
599591 }
600592 }
601593
@@ -619,7 +611,6 @@ static int process_command(AVFilterContext *ctx, const char *cmd, const char *ar · 17 unmodified lines
619611
620612 for (i = 0; i < ctx->nb_outputs; i++) {
621613 avcodec_flush_buffers(movie->st[i].codec_ctx);
622− movie->st[i].done = 0;
623614 }
624615 return ret;
625616 } else if (!strcmp(cmd, "get_duration")) {

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