Showing the changes to libavfilter (1 of 1 file). Show all
Unified · Split
| @@ -45,6 +45,7 @@ · 44 unmodified lines | ||
| 45 | 45 | |
| 46 | 46 | #include "audio.h" |
| 47 | 47 | #include "avfilter.h" |
| 48 | +#include "filters.h" | |
| 48 | 49 | #include "formats.h" |
| 49 | 50 | #include "internal.h" |
| 50 | 51 | #include "video.h" |
| @@ -54,6 +55,8 @@ typedef struct MovieStream { · 3 unmodified lines | ||
| 54 | 55 | AVCodecContext *codec_ctx; |
| 55 | 56 | int64_t discontinuity_threshold; |
| 56 | 57 | int64_t last_pts; |
| 58 | + AVFrame *frame; | |
| 59 | + int eof; | |
| 57 | 60 | } MovieStream; |
| 58 | 61 | |
| 59 | 62 | typedef struct MovieContext { |
| @@ -70,8 +73,10 @@ typedef struct MovieContext { · 10 unmodified lines | ||
| 70 | 73 | int64_t ts_offset; |
| 71 | 74 | int dec_threads; |
| 72 | 75 | |
| 76 | + AVPacket *pkt; | |
| 73 | 77 | AVFormatContext *format_ctx; |
| 74 | 78 | |
| 79 | + int eof; | |
| 75 | 80 | int max_stream_index; /**< max stream # actually used for output */ |
| 76 | 81 | MovieStream *st; /**< array of all streams, one per output */ |
| 77 | 82 | int *out_index; /**< stream number -> output number map, or -1 */ |
| @@ -99,7 +104,6 @@ static const AVOption movie_options[]= { · 21 unmodified lines | ||
| 99 | 104 | }; |
| 100 | 105 | |
| 101 | 106 | static int movie_config_output_props(AVFilterLink *outlink); |
| 102 | −static int movie_request_frame(AVFilterLink *outlink); | |
| 103 | 107 | |
| 104 | 108 | static AVStream *find_stream(void *log, AVFormatContext *avf, const char *spec) |
| 105 | 109 | { |
| @@ -330,6 +334,9 @@ static av_cold int movie_common_init(AVFilterContext *ctx) · 224 unmodified lines | ||
| 330 | 334 | for (i = 0; i < movie->format_ctx->nb_streams; i++) |
| 331 | 335 | movie->format_ctx->streams[i]->discard = AVDISCARD_ALL; |
| 332 | 336 | |
| 337 | + movie->pkt = av_packet_alloc(); | |
| 338 | + if (!movie->pkt) | |
| 339 | + return AVERROR(ENOMEM); | |
| 333 | 340 | movie->st = av_calloc(nb_streams, sizeof(*movie->st)); |
| 334 | 341 | if (!movie->st) |
| 335 | 342 | return AVERROR(ENOMEM); |
| @@ -347,6 +354,10 @@ static av_cold int movie_common_init(AVFilterContext *ctx) · 11 unmodified lines | ||
| 347 | 354 | movie->max_stream_index = FFMAX(movie->max_stream_index, st->index); |
| 348 | 355 | movie->st[i].discontinuity_threshold = |
| 349 | 356 | 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); | |
| 350 | 361 | } |
| 351 | 362 | if (av_strtok(NULL, "+", &cursor)) |
| 352 | 363 | return AVERROR_BUG; |
| @@ -365,7 +376,6 @@ static av_cold int movie_common_init(AVFilterContext *ctx) · 12 unmodified lines | ||
| 365 | 376 | if (!pad.name) |
| 366 | 377 | return AVERROR(ENOMEM); |
| 367 | 378 | pad.config_props = movie_config_output_props; |
| 368 | − pad.request_frame = movie_request_frame; | |
| 369 | 379 | if ((ret = ff_append_outpad_free_name(ctx, &pad)) < 0) |
| 370 | 380 | return ret; |
| 371 | 381 | 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 | ||
| 394 | 404 | for (i = 0; i < ctx->nb_outputs; i++) { |
| 395 | 405 | if (movie->st[i].st) |
| 396 | 406 | avcodec_free_context(&movie->st[i].codec_ctx); |
| 407 | + av_frame_free(&movie->st[i].frame); | |
| 397 | 408 | } |
| 409 | + av_packet_free(&movie->pkt); | |
| 398 | 410 | av_freep(&movie->st); |
| 399 | 411 | av_freep(&movie->out_index); |
| 400 | 412 | if (movie->format_ctx) |
| @@ -460,32 +472,6 @@ static int movie_config_output_props(AVFilterLink *outlink) · 59 unmodified lines | ||
| 460 | 472 | return 0; |
| 461 | 473 | } |
| 462 | 474 | |
| 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 | − | |
| 489 | 475 | static int rewind_file(AVFilterContext *ctx) |
| 490 | 476 | { |
| 491 | 477 | MovieContext *movie = ctx->priv; |
| @@ -507,147 +493,138 @@ static int rewind_file(AVFilterContext *ctx) · 15 unmodified lines | ||
| 507 | 493 | return 0; |
| 508 | 494 | } |
| 509 | 495 | |
| 510 | −static int movie_decode_packet(AVFilterContext *ctx) | |
| 496 | +static int flush_decoder(AVFilterContext *ctx, int i) | |
| 511 | 497 | { |
| 512 | 498 | 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; | |
| 529 | 500 | |
| 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); | |
| 540 | 502 | } |
| 541 | 503 | |
| 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) | |
| 553 | 505 | { |
| 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; | |
| 571 | 519 | } |
| 572 | 520 | |
| 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 | + } | |
| 584 | 544 | } |
| 585 | 545 | } |
| 546 | + st->last_pts = frame->pts; | |
| 586 | 547 | } |
| 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; | |
| 602 | 553 | } |
| 603 | − ret = ff_filter_frame(outlink, frame); | |
| 604 | 554 | |
| 605 | − if (ret < 0) | |
| 606 | − return ret; | |
| 607 | 555 | return 0; |
| 608 | 556 | } |
| 609 | 557 | |
| 610 | −static int movie_request_frame(AVFilterLink *outlink) | |
| 558 | +static int activate(AVFilterContext *ctx) | |
| 611 | 559 | { |
| 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; | |
| 615 | 562 | |
| 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 | + } | |
| 619 | 567 | |
| 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; | |
| 631 | 590 | } |
| 591 | + } else { | |
| 592 | + int nb_eofs = 0; | |
| 632 | 593 | |
| 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); | |
| 636 | 604 | if (ret < 0) |
| 637 | 605 | 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 | + } | |
| 647 | 619 | } |
| 648 | − return AVERROR_EOF; | |
| 649 | 620 | } |
| 621 | + | |
| 622 | + if (nb_eofs < ctx->nb_outputs) | |
| 623 | + ff_filter_set_ready(ctx, 100); | |
| 624 | + return 0; | |
| 650 | 625 | } |
| 626 | + | |
| 627 | + return FFERROR_NOT_READY; | |
| 651 | 628 | } |
| 652 | 629 | |
| 653 | 630 | static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, |
| @@ -702,6 +679,7 @@ const AVFilter ff_avsrc_movie = { · 48 unmodified lines | ||
| 702 | 679 | .priv_size = sizeof(MovieContext), |
| 703 | 680 | .priv_class = &movie_class, |
| 704 | 681 | .init = movie_common_init, |
| 682 | + .activate = activate, | |
| 705 | 683 | .uninit = movie_uninit, |
| 706 | 684 | FILTER_QUERY_FUNC(movie_query_formats), |
| 707 | 685 | |
| @@ -721,6 +699,7 @@ const AVFilter ff_avsrc_amovie = { · 13 unmodified lines | ||
| 721 | 699 | .priv_class = &movie_class, |
| 722 | 700 | .priv_size = sizeof(MovieContext), |
| 723 | 701 | .init = movie_common_init, |
| 702 | + .activate = activate, | |
| 724 | 703 | .uninit = movie_uninit, |
| 725 | 704 | FILTER_QUERY_FUNC(movie_query_formats), |
| 726 | 705 | |