X-Git-Url: https://git.piment-noir.org/?p=deb_ffmpeg.git;a=blobdiff_plain;f=ffmpeg%2Fffmpeg.c;h=eef774bda7c73b1e52f1e3aeb00e4c4b580f79e3;hp=ee8039cce8115db98e7df9d0bcca2ee92b670ef1;hb=f6fa7814ccfe3e76514b36cf04f5cd3cb657c8cf;hpb=2ba45a602cbfa7b771effba9b11bb4245c21bc00 diff --git a/ffmpeg/ffmpeg.c b/ffmpeg/ffmpeg.c index ee8039c..eef774b 100644 --- a/ffmpeg/ffmpeg.c +++ b/ffmpeg/ffmpeg.c @@ -475,6 +475,7 @@ static void ffmpeg_cleanup(int ret) } ost->bitstream_filters = NULL; av_frame_free(&ost->filtered_frame); + av_frame_free(&ost->last_frame); av_parser_close(ost->parser); @@ -622,7 +623,11 @@ static void write_frame(AVFormatContext *s, AVPacket *pkt, OutputStream *ost) while (bsfc) { AVPacket new_pkt = *pkt; - int a = av_bitstream_filter_filter(bsfc, avctx, NULL, + AVDictionaryEntry *bsf_arg = av_dict_get(ost->bsf_args, + bsfc->filter->name, + NULL, 0); + int a = av_bitstream_filter_filter(bsfc, avctx, + bsf_arg ? bsf_arg->value : NULL, &new_pkt.data, &new_pkt.size, pkt->data, pkt->size, pkt->flags & AV_PKT_FLAG_KEY); @@ -659,6 +664,17 @@ static void write_frame(AVFormatContext *s, AVPacket *pkt, OutputStream *ost) } if (!(s->oformat->flags & AVFMT_NOTIMESTAMPS)) { + if (pkt->dts != AV_NOPTS_VALUE && + pkt->pts != AV_NOPTS_VALUE && + pkt->dts > pkt->pts) { + av_log(s, AV_LOG_WARNING, "Invalid DTS: %"PRId64" PTS: %"PRId64" in output stream %d:%d, replacing by guess\n", + pkt->dts, pkt->pts, + ost->file_index, ost->st->index); + pkt->pts = + pkt->dts = pkt->pts + pkt->dts + ost->last_mux_dts + 1 + - FFMIN3(pkt->pts, pkt->dts, ost->last_mux_dts + 1) + - FFMAX3(pkt->pts, pkt->dts, ost->last_mux_dts + 1); + } if( (avctx->codec_type == AVMEDIA_TYPE_AUDIO || avctx->codec_type == AVMEDIA_TYPE_VIDEO) && pkt->dts != AV_NOPTS_VALUE && @@ -681,15 +697,6 @@ static void write_frame(AVFormatContext *s, AVPacket *pkt, OutputStream *ost) pkt->dts = max; } } - if (pkt->dts != AV_NOPTS_VALUE && - pkt->pts != AV_NOPTS_VALUE && - pkt->dts > pkt->pts) { - av_log(s, AV_LOG_WARNING, "Invalid DTS: %"PRId64" PTS: %"PRId64" in output stream %d:%d\n", - pkt->dts, pkt->pts, - ost->file_index, ost->st->index); - pkt->pts = AV_NOPTS_VALUE; - pkt->dts = AV_NOPTS_VALUE; - } } ost->last_mux_dts = pkt->dts; @@ -872,14 +879,14 @@ static void do_subtitle_out(AVFormatContext *s, static void do_video_out(AVFormatContext *s, OutputStream *ost, - AVFrame *in_picture) + AVFrame *next_picture) { int ret, format_video_sync; AVPacket pkt; AVCodecContext *enc = ost->enc_ctx; AVCodecContext *mux_enc = ost->st->codec; - int nb_frames, i; - double sync_ipts, delta; + int nb_frames, nb0_frames, i; + double sync_ipts, delta, delta0; double duration = 0; int frame_size = 0; InputStream *ist = NULL; @@ -890,10 +897,20 @@ static void do_video_out(AVFormatContext *s, if(ist && ist->st->start_time != AV_NOPTS_VALUE && ist->st->first_dts != AV_NOPTS_VALUE && ost->frame_rate.num) duration = 1/(av_q2d(ost->frame_rate) * av_q2d(enc->time_base)); - sync_ipts = in_picture->pts; - delta = sync_ipts - ost->sync_opts + duration; + if (!ost->filters_script && + !ost->filters && + next_picture && + ist && + lrintf(av_frame_get_pkt_duration(next_picture) * av_q2d(ist->st->time_base) / av_q2d(enc->time_base)) > 0) { + duration = lrintf(av_frame_get_pkt_duration(next_picture) * av_q2d(ist->st->time_base) / av_q2d(enc->time_base)); + } + + sync_ipts = next_picture->pts; + delta0 = sync_ipts - ost->sync_opts; + delta = delta0 + duration; /* by default, we output a single frame */ + nb0_frames = 0; nb_frames = 1; format_video_sync = video_sync_method; @@ -913,19 +930,34 @@ static void do_video_out(AVFormatContext *s, } } + if (delta0 < 0 && + delta > 0 && + format_video_sync != VSYNC_PASSTHROUGH && + format_video_sync != VSYNC_DROP) { + double cor = FFMIN(-delta0, duration); + av_log(NULL, AV_LOG_WARNING, "Past duration %f too large\n", -delta0); + sync_ipts += cor; + duration -= cor; + delta0 += cor; + } + switch (format_video_sync) { case VSYNC_VSCFR: if (ost->frame_number == 0 && delta - duration >= 0.5) { av_log(NULL, AV_LOG_DEBUG, "Not duplicating %d initial frames\n", (int)lrintf(delta - duration)); delta = duration; + delta0 = 0; ost->sync_opts = lrint(sync_ipts); } case VSYNC_CFR: // FIXME set to 0.5 after we fix some dts/pts bugs like in avidec.c if (delta < -1.1) nb_frames = 0; - else if (delta > 1.1) + else if (delta > 1.1) { nb_frames = lrintf(delta); + if (delta0 > 1.1) + nb0_frames = lrintf(delta0 - 0.6); + } break; case VSYNC_VFR: if (delta <= -0.6) @@ -942,28 +974,36 @@ static void do_video_out(AVFormatContext *s, } nb_frames = FFMIN(nb_frames, ost->max_frames - ost->frame_number); - if (nb_frames == 0) { + nb0_frames = FFMIN(nb0_frames, nb_frames); + if (nb0_frames == 0 && ost->last_droped) { nb_frames_drop++; av_log(NULL, AV_LOG_VERBOSE, "*** dropping frame %d from stream %d at ts %"PRId64"\n", - ost->frame_number, ost->st->index, in_picture->pts); - return; - } else if (nb_frames > 1) { + ost->frame_number, ost->st->index, ost->last_frame->pts); + } + if (nb_frames > (nb0_frames && ost->last_droped) + (nb_frames > nb0_frames)) { if (nb_frames > dts_error_threshold * 30) { av_log(NULL, AV_LOG_ERROR, "%d frame duplication too large, skipping\n", nb_frames - 1); nb_frames_drop++; return; } - nb_frames_dup += nb_frames - 1; + nb_frames_dup += nb_frames - (nb0_frames && ost->last_droped) - (nb_frames > nb0_frames); av_log(NULL, AV_LOG_VERBOSE, "*** %d dup!\n", nb_frames - 1); } + ost->last_droped = nb_frames == nb0_frames; /* duplicates frame if needed */ for (i = 0; i < nb_frames; i++) { + AVFrame *in_picture; av_init_packet(&pkt); pkt.data = NULL; pkt.size = 0; + if (i < nb0_frames && ost->last_frame) { + in_picture = ost->last_frame; + } else + in_picture = next_picture; + in_picture->pts = ost->sync_opts; #if 1 @@ -978,10 +1018,8 @@ static void do_video_out(AVFormatContext *s, /* raw pictures are written as AVPicture structure to avoid any copies. We support temporarily the older method. */ - mux_enc->coded_frame->interlaced_frame = in_picture->interlaced_frame; - mux_enc->coded_frame->top_field_first = in_picture->top_field_first; - if (mux_enc->coded_frame->interlaced_frame) - mux_enc->field_order = mux_enc->coded_frame->top_field_first ? AV_FIELD_TB:AV_FIELD_BT; + if (in_picture->interlaced_frame) + mux_enc->field_order = in_picture->top_field_first ? AV_FIELD_TB:AV_FIELD_BT; else mux_enc->field_order = AV_FIELD_PROGRESSIVE; pkt.data = (uint8_t *)in_picture; @@ -1007,8 +1045,7 @@ static void do_video_out(AVFormatContext *s, mux_enc->field_order = AV_FIELD_PROGRESSIVE; in_picture->quality = enc->global_quality; - if (!enc->me_threshold) - in_picture->pict_type = 0; + in_picture->pict_type = 0; pts_time = in_picture->pts != AV_NOPTS_VALUE ? in_picture->pts * av_q2d(enc->time_base) : NAN; @@ -1102,6 +1139,11 @@ static void do_video_out(AVFormatContext *s, if (vstats_filename && frame_size) do_video_stats(ost, frame_size); } + + if (!ost->last_frame) + ost->last_frame = av_frame_alloc(); + av_frame_unref(ost->last_frame); + av_frame_ref(ost->last_frame, next_picture); } static double psnr(double d) @@ -1270,7 +1312,6 @@ static void print_final_stats(int64_t total_size) if (data_size && total_size>0 && total_size >= data_size) percent = 100.0 * (total_size - data_size) / data_size; - av_log(NULL, AV_LOG_INFO, "\n"); av_log(NULL, AV_LOG_INFO, "video:%1.0fkB audio:%1.0fkB subtitle:%1.0fkB other streams:%1.0fkB global headers:%1.0fkB muxing overhead: ", video_size / 1024.0, audio_size / 1024.0, @@ -1461,6 +1502,8 @@ static void print_report(int is_last_report, int64_t timer_start, int64_t cur_ti if (av_stream_get_end_pts(ost->st) != AV_NOPTS_VALUE) pts = FFMAX(pts, av_rescale_q(av_stream_get_end_pts(ost->st), ost->st->time_base, AV_TIME_BASE_Q)); + if (is_last_report) + nb_frames_drop += ost->last_droped; } secs = pts / AV_TIME_BASE; @@ -1496,10 +1539,11 @@ static void print_report(int is_last_report, int64_t timer_start, int64_t cur_ti av_bprintf(&buf_script, "drop_frames=%d\n", nb_frames_drop); if (print_stats || is_last_report) { + const char end = is_last_report ? '\n' : '\r'; if (print_stats==1 && AV_LOG_INFO > av_log_get_level()) { - fprintf(stderr, "%s \r", buf); + fprintf(stderr, "%s %c", buf, end); } else - av_log(NULL, AV_LOG_INFO, "%s \r", buf); + av_log(NULL, AV_LOG_INFO, "%s %c", buf, end); fflush(stderr); } @@ -1906,6 +1950,20 @@ static int decode_video(InputStream *ist, AVPacket *pkt, int *got_output) if (*got_output || ret<0 || pkt->size) decode_error_stat[ret<0] ++; + if (*got_output && ret >= 0) { + if (ist->dec_ctx->width != decoded_frame->width || + ist->dec_ctx->height != decoded_frame->height || + ist->dec_ctx->pix_fmt != decoded_frame->format) { + av_log(NULL, AV_LOG_DEBUG, "Frame parameters mismatch context %d,%d,%d != %d,%d,%d\n", + decoded_frame->width, + decoded_frame->height, + decoded_frame->format, + ist->dec_ctx->width, + ist->dec_ctx->height, + ist->dec_ctx->pix_fmt); + } + } + if (!*got_output || ret < 0) { if (!pkt->size) { for (i = 0; i < ist->nb_filters; i++) @@ -2122,11 +2180,11 @@ static int process_input_packet(InputStream *ist, const AVPacket *pkt) ret = decode_video (ist, &avpkt, &got_output); if (avpkt.duration) { duration = av_rescale_q(avpkt.duration, ist->st->time_base, AV_TIME_BASE_Q); - } else if(ist->dec_ctx->time_base.num != 0 && ist->dec_ctx->time_base.den != 0) { + } else if(ist->dec_ctx->framerate.num != 0 && ist->dec_ctx->framerate.den != 0) { int ticks= av_stream_get_parser(ist->st) ? av_stream_get_parser(ist->st)->repeat_pict+1 : ist->dec_ctx->ticks_per_frame; duration = ((int64_t)AV_TIME_BASE * - ist->dec_ctx->time_base.num * ticks) / - ist->dec_ctx->time_base.den; + ist->dec_ctx->framerate.den * ticks) / + ist->dec_ctx->framerate.num / ist->dec_ctx->ticks_per_frame; } else duration = 0; @@ -2181,11 +2239,11 @@ static int process_input_packet(InputStream *ist, const AVPacket *pkt) ist->next_dts = av_rescale_q(next_dts + 1, av_inv_q(ist->framerate), time_base_q); } else if (pkt->duration) { ist->next_dts += av_rescale_q(pkt->duration, ist->st->time_base, AV_TIME_BASE_Q); - } else if(ist->dec_ctx->time_base.num != 0) { + } else if(ist->dec_ctx->framerate.num != 0) { int ticks= av_stream_get_parser(ist->st) ? av_stream_get_parser(ist->st)->repeat_pict + 1 : ist->dec_ctx->ticks_per_frame; ist->next_dts += ((int64_t)AV_TIME_BASE * - ist->dec_ctx->time_base.num * ticks) / - ist->dec_ctx->time_base.den; + ist->dec_ctx->framerate.den * ticks) / + ist->dec_ctx->framerate.num / ist->dec_ctx->ticks_per_frame; } break; } @@ -2621,6 +2679,26 @@ static int transcode_init(void) av_reduce(&enc_ctx->time_base.num, &enc_ctx->time_base.den, enc_ctx->time_base.num, enc_ctx->time_base.den, INT_MAX); + if (ist->st->nb_side_data) { + ost->st->side_data = av_realloc_array(NULL, ist->st->nb_side_data, + sizeof(*ist->st->side_data)); + if (!ost->st->side_data) + return AVERROR(ENOMEM); + + for (j = 0; j < ist->st->nb_side_data; j++) { + const AVPacketSideData *sd_src = &ist->st->side_data[j]; + AVPacketSideData *sd_dst = &ost->st->side_data[j]; + + sd_dst->data = av_malloc(sd_src->size); + if (!sd_dst->data) + return AVERROR(ENOMEM); + memcpy(sd_dst->data, sd_src->data, sd_src->size); + sd_dst->size = sd_src->size; + sd_dst->type = sd_src->type; + ost->st->nb_side_data++; + } + } + ost->parser = av_parser_init(enc_ctx->codec_id); switch (enc_ctx->codec_type) { @@ -2635,7 +2713,10 @@ static int transcode_init(void) enc_ctx->frame_size = dec_ctx->frame_size; enc_ctx->audio_service_type = dec_ctx->audio_service_type; enc_ctx->block_align = dec_ctx->block_align; + enc_ctx->initial_padding = dec_ctx->delay; +#if FF_API_AUDIOENC_DELAY enc_ctx->delay = dec_ctx->delay; +#endif if((enc_ctx->block_align == 1 || enc_ctx->block_align == 1152 || enc_ctx->block_align == 576) && enc_ctx->codec_id == AV_CODEC_ID_MP3) enc_ctx->block_align= 0; if(enc_ctx->codec_id == AV_CODEC_ID_AC3) @@ -2882,10 +2963,11 @@ static int transcode_init(void) av_log(NULL, AV_LOG_WARNING, "The bitrate parameter is set too low." " It takes bits/s as argument, not kbits/s\n"); } else { - if (av_opt_set_dict(ost->enc_ctx, &ost->encoder_opts) < 0) { + ret = av_opt_set_dict(ost->enc_ctx, &ost->encoder_opts); + if (ret < 0) { av_log(NULL, AV_LOG_FATAL, "Error setting up codec context options.\n"); - exit_program(1); + return ret; } } @@ -3085,9 +3167,9 @@ static OutputStream *choose_output(void) OutputStream *ost = output_streams[i]; int64_t opts = av_rescale_q(ost->st->cur_dts, ost->st->time_base, AV_TIME_BASE_Q); - if (!ost->unavailable && !ost->finished && opts < opts_min) { + if (!ost->finished && opts < opts_min) { opts_min = opts; - ost_min = ost; + ost_min = ost->unavailable ? NULL : ost; } } return ost_min; @@ -3465,13 +3547,14 @@ static int process_input(int file_index) if (pkt.dts != AV_NOPTS_VALUE) pkt.dts *= ist->ts_scale; - if (pkt.dts != AV_NOPTS_VALUE && ist->next_dts == AV_NOPTS_VALUE && !copy_ts + if ((ist->dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO || + ist->dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) && + pkt.dts != AV_NOPTS_VALUE && ist->next_dts == AV_NOPTS_VALUE && !copy_ts && (is->iformat->flags & AVFMT_TS_DISCONT) && ifile->last_ts != AV_NOPTS_VALUE) { int64_t pkt_dts = av_rescale_q(pkt.dts, ist->st->time_base, AV_TIME_BASE_Q); int64_t delta = pkt_dts - ifile->last_ts; - if(delta < -1LL*dts_delta_threshold*AV_TIME_BASE || - (delta > 1LL*dts_delta_threshold*AV_TIME_BASE && - ist->dec_ctx->codec_type != AVMEDIA_TYPE_SUBTITLE)){ + if (delta < -1LL*dts_delta_threshold*AV_TIME_BASE || + delta > 1LL*dts_delta_threshold*AV_TIME_BASE){ ifile->ts_offset -= delta; av_log(NULL, AV_LOG_DEBUG, "Inter stream timestamp discontinuity %"PRId64", new offset= %"PRId64"\n", @@ -3482,14 +3565,15 @@ static int process_input(int file_index) } } - if (pkt.dts != AV_NOPTS_VALUE && ist->next_dts != AV_NOPTS_VALUE && + if ((ist->dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO || + ist->dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) && + pkt.dts != AV_NOPTS_VALUE && ist->next_dts != AV_NOPTS_VALUE && !copy_ts) { int64_t pkt_dts = av_rescale_q(pkt.dts, ist->st->time_base, AV_TIME_BASE_Q); int64_t delta = pkt_dts - ist->next_dts; if (is->iformat->flags & AVFMT_TS_DISCONT) { if (delta < -1LL*dts_delta_threshold*AV_TIME_BASE || - (delta > 1LL*dts_delta_threshold*AV_TIME_BASE && - ist->dec_ctx->codec_type != AVMEDIA_TYPE_SUBTITLE) || + delta > 1LL*dts_delta_threshold*AV_TIME_BASE || pkt_dts + AV_TIME_BASE/10 < FFMAX(ist->pts, ist->dts)) { ifile->ts_offset -= delta; av_log(NULL, AV_LOG_DEBUG, @@ -3501,7 +3585,7 @@ static int process_input(int file_index) } } else { if ( delta < -1LL*dts_error_threshold*AV_TIME_BASE || - (delta > 1LL*dts_error_threshold*AV_TIME_BASE && ist->dec_ctx->codec_type != AVMEDIA_TYPE_SUBTITLE)) { + delta > 1LL*dts_error_threshold*AV_TIME_BASE) { av_log(NULL, AV_LOG_WARNING, "DTS %"PRId64", next:%"PRId64" st:%d invalid dropping\n", pkt.dts, ist->next_dts, pkt.stream_index); pkt.dts = AV_NOPTS_VALUE; } @@ -3509,7 +3593,7 @@ static int process_input(int file_index) int64_t pkt_pts = av_rescale_q(pkt.pts, ist->st->time_base, AV_TIME_BASE_Q); delta = pkt_pts - ist->next_dts; if ( delta < -1LL*dts_error_threshold*AV_TIME_BASE || - (delta > 1LL*dts_error_threshold*AV_TIME_BASE && ist->dec_ctx->codec_type != AVMEDIA_TYPE_SUBTITLE)) { + delta > 1LL*dts_error_threshold*AV_TIME_BASE) { av_log(NULL, AV_LOG_WARNING, "PTS %"PRId64", next:%"PRId64" invalid dropping st:%d\n", pkt.pts, ist->next_dts, pkt.stream_index); pkt.pts = AV_NOPTS_VALUE; } @@ -3752,6 +3836,7 @@ static int transcode(void) av_dict_free(&ost->encoder_opts); av_dict_free(&ost->swr_opts); av_dict_free(&ost->resample_opts); + av_dict_free(&ost->bsf_args); } } } @@ -3773,7 +3858,7 @@ static int64_t getutime(void) GetProcessTimes(proc, &c, &e, &k, &u); return ((int64_t) u.dwHighDateTime << 32 | u.dwLowDateTime) / 10; #else - return av_gettime(); + return av_gettime_relative(); #endif }