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
51typedef struct MovieStream {51typedef struct MovieStream {
52 AVStream *st;52 AVStream *st;
53 AVCodecContext *codec_ctx;53 AVCodecContext *codec_ctx;
54 int done;
55 int64_t discontinuity_threshold;54 int64_t discontinuity_threshold;
56 int64_t last_pts;55 int64_t last_pts;
57} MovieStream;56} MovieStream;
@@ -70,8 +69,6 @@ typedef struct MovieContext { · 12 unmodified lines
70 int64_t ts_offset;69 int64_t ts_offset;
71 70
72 AVFormatContext *format_ctx;71 AVFormatContext *format_ctx;
73 int eof;
74 AVPacket pkt;
75 72
76 int max_stream_index; /**< max stream # actually used for output */73 int max_stream_index; /**< max stream # actually used for output */
77 MovieStream *st; /**< array of all streams, one per output */74 MovieStream *st; /**< array of all streams, one per output */
@@ -172,7 +169,6 @@ static int open_stream(AVFilterContext *ctx, MovieStream *st) · 94 unmodified lines
172 if (ret < 0)169 if (ret < 0)
173 return ret;170 return ret;
174 171
175 st->codec_ctx->refcounted_frames = 1;
176 st->codec_ctx->thread_count = ff_filter_get_nb_threads(ctx);172 st->codec_ctx->thread_count = ff_filter_get_nb_threads(ctx);
177 173
178 if ((ret = avcodec_open2(st->codec_ctx, codec, NULL)) < 0) {174 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
344 }340 }
345 av_freep(&movie->st);341 av_freep(&movie->st);
346 av_freep(&movie->out_index);342 av_freep(&movie->out_index);
347 av_packet_unref(&movie->pkt);
348 if (movie->format_ctx)343 if (movie->format_ctx)
349 avformat_close_input(&movie->format_ctx);344 avformat_close_input(&movie->format_ctx);
350}345}
@@ -451,103 +446,69 @@ static int rewind_file(AVFilterContext *ctx) · 100 unmodified lines
451 446
452 for (i = 0; i < ctx->nb_outputs; i++) {447 for (i = 0; i < ctx->nb_outputs; i++) {
453 avcodec_flush_buffers(movie->st[i].codec_ctx);448 avcodec_flush_buffers(movie->st[i].codec_ctx);
454 movie->st[i].done = 0;
455 }449 }
456 movie->eof = 0;
457 return 0;450 return 0;
458}451}
459 452
453static 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
460/**483/**
461 * Try to push a frame to the requested output.484 * Try to push a frame to the requested output.
462 *485 *
463 * @param ctx filter context486 * @param ctx filter context
464 * @param out_id number of output where a frame is wanted;487 * @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;488 * @return 0 if a frame was pushed on the requested output,
466 * if the codec is being flushed, flush the corresponding stream489 * AVERROR(EAGAIN) if the decoder requires more input
467 * @return 1 if a frame was pushed on the requested output,490 * AVERROR(EOF) if the decoder has been completely flushed
468 * 0 if another attempt is possible,491 * <0 AVERROR code
469 * <0 AVERROR code
470 */492 */
471static int movie_push_frame(AVFilterContext *ctx, unsigned out_id)493static int movie_push_frame(AVFilterContext *ctx, unsigned out_id)
472{494{
473 MovieContext *movie = ctx->priv;495 MovieContext *movie = ctx->priv;
474 AVPacket *pkt = &movie->pkt;496 MovieStream *st = &movie->st[out_id];
475 enum AVMediaType frame_type;497 AVFilterLink *outlink = ctx->outputs[out_id];
476 MovieStream *st;
477 int ret, got_frame = 0, pkt_out_id;
478 AVFilterLink *outlink;
479 AVFrame *frame;498 AVFrame *frame;
480 499 int ret;
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];
516 500
517 frame = av_frame_alloc();501 frame = av_frame_alloc();
518 if (!frame)502 if (!frame)
519 return AVERROR(ENOMEM);503 return AVERROR(ENOMEM);
520 504
521 frame_type = st->st->codecpar->codec_type;505 ret = avcodec_receive_frame(st->codec_ctx, frame);
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 }
533 if (ret < 0) {506 if (ret < 0) {
534 av_log(ctx, AV_LOG_WARNING, "Decode error: %s\n", av_err2str(ret));507 if (ret != AVERROR_EOF && ret != AVERROR(EAGAIN))
535 av_frame_free(&frame);508 av_log(ctx, AV_LOG_WARNING, "Decode error: %s\n", av_err2str(ret));
536 av_packet_unref(pkt);509
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;
549 av_frame_free(&frame);510 av_frame_free(&frame);
550 return 0;511 return ret;
551 }512 }
552 513
553 frame->pts = frame->best_effort_timestamp;514 frame->pts = frame->best_effort_timestamp;
@@ -558,7 +519,7 @@ static int movie_push_frame(AVFilterContext *ctx, unsigned out_id) · 4 unmodified lines
558 if (st->last_pts != AV_NOPTS_VALUE) {519 if (st->last_pts != AV_NOPTS_VALUE) {
559 int64_t diff = frame->pts - st->last_pts;520 int64_t diff = frame->pts - st->last_pts;
560 if (diff < 0 || diff > st->discontinuity_threshold) {521 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);
562 movie->ts_offset += av_rescale_q_rnd(-diff, outlink->time_base, AV_TIME_BASE_Q, AV_ROUND_UP);523 movie->ts_offset += av_rescale_q_rnd(-diff, outlink->time_base, AV_TIME_BASE_Q, AV_ROUND_UP);
563 frame->pts -= diff;524 frame->pts -= diff;
564 }525 }
@@ -567,7 +528,8 @@ static int movie_push_frame(AVFilterContext *ctx, unsigned out_id) · 2 unmodified lines
567 st->last_pts = frame->pts;528 st->last_pts = frame->pts;
568 }529 }
569 ff_dlog(ctx, "movie_push_frame(): file:'%s' %s\n", movie->file_name,530 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));
571 533
572 if (st->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {534 if (st->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
573 if (frame->format != outlink->format) {535 if (frame->format != outlink->format) {
@@ -583,19 +545,49 @@ static int movie_push_frame(AVFilterContext *ctx, unsigned out_id) · 9 unmodified lines
583 545
584 if (ret < 0)546 if (ret < 0)
585 return ret;547 return ret;
586 return pkt_out_id == out_id;548 return 0;
587}549}
588 550
589static int movie_request_frame(AVFilterLink *outlink)551static int movie_request_frame(AVFilterLink *outlink)
590{552{
591 AVFilterContext *ctx = outlink->src;553 AVFilterContext *ctx = outlink->src;
554 MovieContext *movie = ctx->priv;
592 unsigned out_id = FF_OUTLINK_IDX(outlink);555 unsigned out_id = FF_OUTLINK_IDX(outlink);
593 int ret;
594 556
595 while (1) {557 while (1) {
596 ret = movie_push_frame(ctx, out_id);558 int got_eagain = 0, got_eof = 0;
597 if (ret)559 int ret = 0;
598 return FFMIN(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 }
599 }591 }
600}592}
601 593
@@ -619,7 +611,6 @@ static int process_command(AVFilterContext *ctx, const char *cmd, const char *ar · 17 unmodified lines
619 611
620 for (i = 0; i < ctx->nb_outputs; i++) {612 for (i = 0; i < ctx->nb_outputs; i++) {
621 avcodec_flush_buffers(movie->st[i].codec_ctx);613 avcodec_flush_buffers(movie->st[i].codec_ctx);
622 movie->st[i].done = 0;
623 }614 }
624 return ret;615 return ret;
625 } else if (!strcmp(cmd, "get_duration")) {616 } 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