Showing the changes to libavfilter (1 of 1 file). Show all
Unified · Split
| @@ -45,6 +45,7 @@ · 44 unmodified lines | |||
| 45 | 45 | ||
| 46 | #include "audio.h" | 46 | #include "audio.h" |
| 47 | #include "avfilter.h" | 47 | #include "avfilter.h" |
| 48 | #include "filters.h" | ||
| 48 | #include "formats.h" | 49 | #include "formats.h" |
| 49 | #include "internal.h" | 50 | #include "internal.h" |
| 50 | #include "video.h" | 51 | #include "video.h" |
| @@ -54,6 +55,8 @@ typedef struct MovieStream { · 3 unmodified lines | |||
| 54 | AVCodecContext *codec_ctx; | 55 | AVCodecContext *codec_ctx; |
| 55 | int64_t discontinuity_threshold; | 56 | int64_t discontinuity_threshold; |
| 56 | int64_t last_pts; | 57 | int64_t last_pts; |
| 58 | AVFrame *frame; | ||
| 59 | int eof; | ||
| 57 | } MovieStream; | 60 | } MovieStream; |
| 58 | 61 | ||
| 59 | typedef struct MovieContext { | 62 | typedef struct MovieContext { |
| @@ -70,8 +73,10 @@ typedef struct MovieContext { · 10 unmodified lines | |||
| 70 | int64_t ts_offset; | 73 | int64_t ts_offset; |
| 71 | int dec_threads; | 74 | int dec_threads; |
| 72 | 75 | ||
| 76 | AVPacket *pkt; | ||
| 73 | AVFormatContext *format_ctx; | 77 | AVFormatContext *format_ctx; |
| 74 | 78 | ||
| 79 | int eof; | ||
| 75 | int max_stream_index; /**< max stream # actually used for output */ | 80 | int max_stream_index; /**< max stream # actually used for output */ |
| 76 | MovieStream *st; /**< array of all streams, one per output */ | 81 | MovieStream *st; /**< array of all streams, one per output */ |
| 77 | int *out_index; /**< stream number -> output number map, or -1 */ | 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 | static int movie_config_output_props(AVFilterLink *outlink); | 106 | static int movie_config_output_props(AVFilterLink *outlink); |
| 102 | static int movie_request_frame(AVFilterLink *outlink); | ||
| 103 | 107 | ||
| 104 | static AVStream *find_stream(void *log, AVFormatContext *avf, const char *spec) | 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 | for (i = 0; i < movie->format_ctx->nb_streams; i++) | 334 | for (i = 0; i < movie->format_ctx->nb_streams; i++) |
| 331 | movie->format_ctx->streams[i]->discard = AVDISCARD_ALL; | 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 | movie->st = av_calloc(nb_streams, sizeof(*movie->st)); | 340 | movie->st = av_calloc(nb_streams, sizeof(*movie->st)); |
| 334 | if (!movie->st) | 341 | if (!movie->st) |
| 335 | return AVERROR(ENOMEM); | 342 | return AVERROR(ENOMEM); |
| @@ -347,6 +354,10 @@ static av_cold int movie_common_init(AVFilterContext *ctx) · 11 unmodified lines | |||
| 347 | movie->max_stream_index = FFMAX(movie->max_stream_index, st->index); | 354 | movie->max_stream_index = FFMAX(movie->max_stream_index, st->index); |
| 348 | movie->st[i].discontinuity_threshold = | 355 | movie->st[i].discontinuity_threshold = |
| 349 | av_rescale_q(movie->discontinuity_threshold, AV_TIME_BASE_Q, st->time_base); | 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 | if (av_strtok(NULL, "+", &cursor)) | 362 | if (av_strtok(NULL, "+", &cursor)) |
| 352 | return AVERROR_BUG; | 363 | return AVERROR_BUG; |
| @@ -365,7 +376,6 @@ static av_cold int movie_common_init(AVFilterContext *ctx) · 12 unmodified lines | |||
| 365 | if (!pad.name) | 376 | if (!pad.name) |
| 366 | return AVERROR(ENOMEM); | 377 | return AVERROR(ENOMEM); |
| 367 | pad.config_props = movie_config_output_props; | 378 | pad.config_props = movie_config_output_props; |
| 368 | pad.request_frame = movie_request_frame; | ||
| 369 | if ((ret = ff_append_outpad_free_name(ctx, &pad)) < 0) | 379 | if ((ret = ff_append_outpad_free_name(ctx, &pad)) < 0) |
| 370 | return ret; | 380 | return ret; |
| 371 | if ( movie->st[i].st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && | 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 | for (i = 0; i < ctx->nb_outputs; i++) { | 404 | for (i = 0; i < ctx->nb_outputs; i++) { |
| 395 | if (movie->st[i].st) | 405 | if (movie->st[i].st) |
| 396 | avcodec_free_context(&movie->st[i].codec_ctx); | 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 | av_freep(&movie->st); | 410 | av_freep(&movie->st); |
| 399 | av_freep(&movie->out_index); | 411 | av_freep(&movie->out_index); |
| 400 | if (movie->format_ctx) | 412 | if (movie->format_ctx) |
| @@ -460,32 +472,6 @@ static int movie_config_output_props(AVFilterLink *outlink) · 59 unmodified lines | |||
| 460 | return 0; | 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 | static int rewind_file(AVFilterContext *ctx) | 475 | static int rewind_file(AVFilterContext *ctx) |
| 490 | { | 476 | { |
| 491 | MovieContext *movie = ctx->priv; | 477 | MovieContext *movie = ctx->priv; |
| @@ -507,147 +493,138 @@ static int rewind_file(AVFilterContext *ctx) · 15 unmodified lines | |||
| 507 | return 0; | 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 | MovieContext *movie = ctx->priv; | 498 | MovieContext *movie = ctx->priv; |
| 513 | AVPacket pkt = { 0 }; | 499 | AVCodecContext *dec = movie->st[i].codec_ctx; |
| 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; | ||
| 529 | 500 | ||
| 530 | /* send the packet to its decoder, if any */ | 501 | return avcodec_send_packet(dec, NULL); |
| 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; | ||
| 540 | } | 502 | } |
| 541 | 503 | ||
| 542 | /** | 504 | static int decode_packet(AVFilterContext *ctx, int i) |
| 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) | ||
| 553 | { | 505 | { |
| 554 | MovieContext *movie = ctx->priv; | 506 | AVFilterLink *outlink = ctx->outputs[i]; |
| 555 | MovieStream *st = &movie->st[out_id]; | 507 | MovieContext *movie = ctx->priv; |
| 556 | AVFilterLink *outlink = ctx->outputs[out_id]; | 508 | MovieStream *st = &movie->st[i]; |
| 557 | AVFrame *frame; | 509 | AVCodecContext *dec = movie->st[i].codec_ctx; |
| 558 | int ret; | 510 | AVFrame *frame = movie->st[i].frame; |
| 559 | 511 | AVPacket *pkt = movie->pkt; | |
| 560 | frame = av_frame_alloc(); | 512 | int ret = 0; |
| 561 | if (!frame) | 513 | |
| 562 | return AVERROR(ENOMEM); | 514 | // submit the packet to the decoder |
| 563 | 515 | if (!movie->eof) { | |
| 564 | ret = avcodec_receive_frame(st->codec_ctx, frame); | 516 | ret = avcodec_send_packet(dec, pkt); |
| 565 | if (ret < 0) { | 517 | if (ret < 0) |
| 566 | if (ret != AVERROR_EOF && ret != AVERROR(EAGAIN)) | 518 | return ret; |
| 567 | av_log(ctx, AV_LOG_WARNING, "Decode error: %s\n", av_err2str(ret)); | ||
| 568 | |||
| 569 | av_frame_free(&frame); | ||
| 570 | return ret; | ||
| 571 | } | 519 | } |
| 572 | 520 | ||
| 573 | frame->pts = frame->best_effort_timestamp; | 521 | // get all the available frames from the decoder |
| 574 | if (frame->pts != AV_NOPTS_VALUE) { | 522 | if (ret >= 0) { |
| 575 | if (movie->ts_offset) | 523 | ret = avcodec_receive_frame(dec, frame); |
| 576 | frame->pts += av_rescale_q_rnd(movie->ts_offset, AV_TIME_BASE_Q, outlink->time_base, AV_ROUND_UP); | 524 | if (ret < 0) { |
| 577 | if (st->discontinuity_threshold) { | 525 | // those two return values are special and mean there is no output |
| 578 | if (st->last_pts != AV_NOPTS_VALUE) { | 526 | // frame available, but there were no errors during decoding |
| 579 | int64_t diff = frame->pts - st->last_pts; | 527 | if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN)) |
| 580 | if (diff < 0 || diff > st->discontinuity_threshold) { | 528 | return 0; |
| 581 | av_log(ctx, AV_LOG_VERBOSE, "Discontinuity in stream:%d diff:%"PRId64"\n", out_id, diff); | 529 | return ret; |
| 582 | movie->ts_offset += av_rescale_q_rnd(-diff, outlink->time_base, AV_TIME_BASE_Q, AV_ROUND_UP); | 530 | } |
| 583 | frame->pts -= diff; | 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; | 548 | ret = ff_filter_frame(outlink, av_frame_clone(frame)); |
| 588 | } | 549 | if (ret < 0) |
| 589 | ff_dlog(ctx, "movie_push_frame(): file:'%s' %s\n", movie->file_name, | 550 | return ret; |
| 590 | describe_frame_to_str((char[1024]){0}, 1024, frame, | 551 | if (ret == 0) |
| 591 | st->st->codecpar->codec_type, outlink)); | 552 | return 1; |
| 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 | } | ||
| 602 | } | 553 | } |
| 603 | ret = ff_filter_frame(outlink, frame); | ||
| 604 | 554 | ||
| 605 | if (ret < 0) | ||
| 606 | return ret; | ||
| 607 | return 0; | 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; | 560 | MovieContext *movie = ctx->priv; |
| 613 | MovieContext *movie = ctx->priv; | 561 | int wanted = 0, ret; |
| 614 | unsigned out_id = FF_OUTLINK_IDX(outlink); | ||
| 615 | 562 | ||
| 616 | while (1) { | 563 | for (int i = 0; i < ctx->nb_outputs; i++) { |
| 617 | int got_eagain = 0, got_eof = 0; | 564 | if (ff_outlink_frame_wanted(ctx->outputs[i])) |
| 618 | int ret = 0; | 565 | wanted++; |
| 566 | } | ||
| 619 | 567 | ||
| 620 | /* check all decoders for available output */ | 568 | if (wanted == 0) |
| 621 | for (int i = 0; i < ctx->nb_outputs; i++) { | 569 | return FFERROR_NOT_READY; |
| 622 | ret = movie_push_frame(ctx, i); | 570 | |
| 623 | if (ret == AVERROR(EAGAIN)) | 571 | if (!movie->eof) { |
| 624 | got_eagain++; | 572 | ret = av_read_frame(movie->format_ctx, movie->pkt); |
| 625 | else if (ret == AVERROR_EOF) | 573 | if (ret < 0) { |
| 626 | got_eof++; | 574 | movie->eof = 1; |
| 627 | else if (ret < 0) | 575 | for (int i = 0; i < ctx->nb_outputs; i++) |
| 628 | return ret; | 576 | flush_decoder(ctx, i); |
| 629 | else if (i == out_id) | 577 | ff_filter_set_ready(ctx, 100); |
| 630 | return 0; | 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) { | 594 | for (int i = 0; i < ctx->nb_outputs; i++) { |
| 634 | /* all decoders require more input -> read a new packet */ | 595 | if (!movie->st[i].eof) { |
| 635 | ret = movie_decode_packet(ctx); | 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 | if (ret < 0) | 604 | if (ret < 0) |
| 637 | return ret; | 605 | return ret; |
| 638 | } else if (got_eof) { | 606 | movie->loop_count -= movie->loop_count > 1; |
| 639 | /* all decoders flushed */ | 607 | av_log(ctx, AV_LOG_VERBOSE, "Stream finished, looping.\n"); |
| 640 | if (movie->loop_count != 1) { | 608 | ff_filter_set_ready(ctx, 100); |
| 641 | ret = rewind_file(ctx); | 609 | for (int i = 0; i < ctx->nb_outputs; i++) |
| 642 | if (ret < 0) | 610 | movie->st[i].eof = 0; |
| 643 | return ret; | 611 | movie->eof = 0; |
| 644 | movie->loop_count -= movie->loop_count > 1; | 612 | return 0; |
| 645 | av_log(ctx, AV_LOG_VERBOSE, "Stream finished, looping.\n"); | 613 | } else { |
| 646 | continue; | 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 | static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, | 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 | .priv_size = sizeof(MovieContext), | 679 | .priv_size = sizeof(MovieContext), |
| 703 | .priv_class = &movie_class, | 680 | .priv_class = &movie_class, |
| 704 | .init = movie_common_init, | 681 | .init = movie_common_init, |
| 682 | .activate = activate, | ||
| 705 | .uninit = movie_uninit, | 683 | .uninit = movie_uninit, |
| 706 | FILTER_QUERY_FUNC(movie_query_formats), | 684 | FILTER_QUERY_FUNC(movie_query_formats), |
| 707 | 685 | ||
| @@ -721,6 +699,7 @@ const AVFilter ff_avsrc_amovie = { · 13 unmodified lines | |||
| 721 | .priv_class = &movie_class, | 699 | .priv_class = &movie_class, |
| 722 | .priv_size = sizeof(MovieContext), | 700 | .priv_size = sizeof(MovieContext), |
| 723 | .init = movie_common_init, | 701 | .init = movie_common_init, |
| 702 | .activate = activate, | ||
| 724 | .uninit = movie_uninit, | 703 | .uninit = movie_uninit, |
| 725 | FILTER_QUERY_FUNC(movie_query_formats), | 704 | FILTER_QUERY_FUNC(movie_query_formats), |
| 726 | 705 | ||