diff options
author | Clément Bœsch <clement@stupeflix.com> | 2016-09-21 16:39:16 +0300 |
---|---|---|
committer | James Almer <jamrial@gmail.com> | 2016-09-24 20:25:53 +0300 |
commit | 5ef19590802f000299e418143fc2301e3f43affe (patch) | |
tree | 327fdd757e3aadc3ddf1a04ceb55364d47d31dac /ffmpeg.c | |
parent | 159aa1275e5d9c94edc313a51d65220e887d790b (diff) |
ffmpeg: switch to the new BSF API
This commit is initially largely based on commit 4426540 from Anton
Khirnov <anton@khirnov.net> and two following fixes (80fb19b and
fe7b21c) which were previously skipped respectively in 98e3153, c9ee36e,
and 7fe7cdc.
mpeg4-bsf-unpack-bframes FATE reference is updated because the bsf
filter now actually fixes the extradata (mpeg4_unpack_bframes_init()
changing one byte is now honored on the output extradata).
The FATE references for remove_extra change because the packet flags
were wrong and the keyframes weren't marked, causing the bsf relying on
these proprieties to not actually work as intended.
The following was fixed by James Almer:
The filter option arguments are now also parsed correctly.
A hack to propagate extradata changed by bitstream filters after the
first av_bsf_receive_packet() call is added to maintain the current
behavior. This was previously done by av_bitstream_filter_filter() and
is needed for the aac_adtstoasc bsf.
The exit_on_error was not being checked anymore, and led to an exit
error in the last frame of h264_mp4toannexb test. Restoring this
behaviour prevents erroring out. The test is still changed as a result
due to the badly filtered frame now not being written after the failure.
Signed-off-by: Clément Bœsch <u@pkh.me>
Signed-off-by: James Almer <jamrial@gmail.com>
Diffstat (limited to 'ffmpeg.c')
-rw-r--r-- | ffmpeg.c | 154 |
1 files changed, 117 insertions, 37 deletions
@@ -502,18 +502,15 @@ static void ffmpeg_cleanup(int ret) } for (i = 0; i < nb_output_streams; i++) { OutputStream *ost = output_streams[i]; - AVBitStreamFilterContext *bsfc; if (!ost) continue; - bsfc = ost->bitstream_filters; - while (bsfc) { - AVBitStreamFilterContext *next = bsfc->next; - av_bitstream_filter_close(bsfc); - bsfc = next; - } - ost->bitstream_filters = NULL; + for (j = 0; j < ost->nb_bitstream_filters; j++) + av_bsf_free(&ost->bsf_ctx[j]); + av_freep(&ost->bsf_ctx); + av_freep(&ost->bsf_extradata_updated); + av_frame_free(&ost->filtered_frame); av_frame_free(&ost->last_frame); @@ -633,11 +630,9 @@ static void close_all_output_streams(OutputStream *ost, OSTFinished this_stream, } } -static void write_frame(AVFormatContext *s, AVPacket *pkt, OutputStream *ost) +static void write_packet(AVFormatContext *s, AVPacket *pkt, OutputStream *ost) { AVStream *st = ost->st; - AVBitStreamFilterContext *bsfc = ost->bitstream_filters; - AVCodecContext *avctx = ost->encoding_needed ? ost->enc_ctx : ost->st->codec; int ret; if ((st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && video_sync_method == VSYNC_DROP) || @@ -680,26 +675,6 @@ static void write_frame(AVFormatContext *s, AVPacket *pkt, OutputStream *ost) } } - if (bsfc) - av_packet_split_side_data(pkt); - - if ((ret = av_apply_bitstream_filters(avctx, pkt, bsfc)) < 0) { - print_error("", ret); - if (exit_on_error) - exit_program(1); - } - if (pkt->size == 0 && pkt->side_data_elems == 0) - return; - if (!st->codecpar->extradata_size && avctx->extradata_size) { - st->codecpar->extradata = av_mallocz(avctx->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE); - if (!st->codecpar->extradata) { - av_log(NULL, AV_LOG_ERROR, "Could not allocate extradata buffer to copy parser data.\n"); - exit_program(1); - } - st->codecpar->extradata_size = avctx->extradata_size; - memcpy(st->codecpar->extradata, avctx->extradata, avctx->extradata_size); - } - if (!(s->oformat->flags & AVFMT_NOTIMESTAMPS)) { if (pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE && @@ -772,6 +747,68 @@ static void close_output_stream(OutputStream *ost) } } +static void output_packet(AVFormatContext *s, AVPacket *pkt, OutputStream *ost) +{ + int ret = 0; + + /* apply the output bitstream filters, if any */ + if (ost->nb_bitstream_filters) { + int idx; + + ret = av_bsf_send_packet(ost->bsf_ctx[0], pkt); + if (ret < 0) + goto finish; + + idx = 1; + while (idx) { + /* get a packet from the previous filter up the chain */ + ret = av_bsf_receive_packet(ost->bsf_ctx[idx - 1], pkt); + /* HACK! - aac_adtstoasc updates extradata after filtering the first frame when + * the api states this shouldn't happen after init(). Propagate it here to the + * muxer and to the next filters in the chain to workaround this. + * TODO/FIXME - Make aac_adtstoasc use new packet side data instead of changing + * par_out->extradata and adapt muxers accordingly to get rid of this. */ + if (!(ost->bsf_extradata_updated[idx - 1] & 1)) { + ret = avcodec_parameters_copy(ost->st->codecpar, ost->bsf_ctx[idx - 1]->par_out); + if (ret < 0) + goto finish; + ost->bsf_extradata_updated[idx - 1] |= 1; + } + if (ret == AVERROR(EAGAIN)) { + ret = 0; + idx--; + continue; + } else if (ret < 0) + goto finish; + + /* send it to the next filter down the chain or to the muxer */ + if (idx < ost->nb_bitstream_filters) { + /* HACK/FIXME! - See above */ + if (!(ost->bsf_extradata_updated[idx] & 2)) { + ret = avcodec_parameters_copy(ost->bsf_ctx[idx]->par_out, ost->bsf_ctx[idx - 1]->par_out); + if (ret < 0) + goto finish; + ost->bsf_extradata_updated[idx] |= 2; + } + ret = av_bsf_send_packet(ost->bsf_ctx[idx], pkt); + if (ret < 0) + goto finish; + idx++; + } else + write_packet(s, pkt, ost); + } + } else + write_packet(s, pkt, ost); + +finish: + if (ret < 0 && ret != AVERROR_EOF) { + av_log(NULL, AV_LOG_ERROR, "Error applying bitstream filters to an output " + "packet for stream #%d:%d.\n", ost->file_index, ost->index); + if(exit_on_error) + exit_program(1); + } +} + static int check_recording_time(OutputStream *ost) { OutputFile *of = output_files[ost->file_index]; @@ -830,7 +867,7 @@ static void do_audio_out(AVFormatContext *s, OutputStream *ost, av_ts2str(pkt.dts), av_ts2timestr(pkt.dts, &ost->st->time_base)); } - write_frame(s, &pkt, ost); + output_packet(s, &pkt, ost); } } @@ -914,7 +951,7 @@ static void do_subtitle_out(AVFormatContext *s, pkt.pts += 90 * sub->end_display_time; } pkt.dts = pkt.pts; - write_frame(s, &pkt, ost); + output_packet(s, &pkt, ost); } } @@ -1095,7 +1132,7 @@ static void do_video_out(AVFormatContext *s, pkt.pts = av_rescale_q(in_picture->pts, enc->time_base, ost->st->time_base); pkt.flags |= AV_PKT_FLAG_KEY; - write_frame(s, &pkt, ost); + output_packet(s, &pkt, ost); } else #endif { @@ -1194,7 +1231,7 @@ static void do_video_out(AVFormatContext *s, } frame_size = pkt.size; - write_frame(s, &pkt, ost); + output_packet(s, &pkt, ost); /* if two pass, output log */ if (ost->logfile && enc->stats_out) { @@ -1756,7 +1793,7 @@ static void flush_encoders(void) } av_packet_rescale_ts(&pkt, enc->time_base, ost->st->time_base); pkt_size = pkt.size; - write_frame(os, &pkt, ost); + output_packet(os, &pkt, ost); if (ost->enc_ctx->codec_type == AVMEDIA_TYPE_VIDEO && vstats_filename) { do_video_stats(ost, pkt_size); } @@ -1898,7 +1935,7 @@ static void do_streamcopy(InputStream *ist, OutputStream *ost, const AVPacket *p } #endif - write_frame(of->ctx, &opkt, ost); + output_packet(of->ctx, &opkt, ost); } int guess_input_channel_layout(InputStream *ist) @@ -2591,6 +2628,42 @@ static int compare_int64(const void *a, const void *b) return FFDIFFSIGN(*(const int64_t *)a, *(const int64_t *)b); } +static int init_output_bsfs(OutputStream *ost) +{ + AVBSFContext *ctx; + int i, ret; + + if (!ost->nb_bitstream_filters) + return 0; + + for (i = 0; i < ost->nb_bitstream_filters; i++) { + ctx = ost->bsf_ctx[i]; + + ret = avcodec_parameters_copy(ctx->par_in, + i ? ost->bsf_ctx[i - 1]->par_out : ost->st->codecpar); + if (ret < 0) + return ret; + + ctx->time_base_in = i ? ost->bsf_ctx[i - 1]->time_base_out : ost->st->time_base; + + ret = av_bsf_init(ctx); + if (ret < 0) { + av_log(NULL, AV_LOG_ERROR, "Error initializing bistream filter: %s\n", + ost->bsf_ctx[i]->filter->name); + return ret; + } + } + + ctx = ost->bsf_ctx[ost->nb_bitstream_filters - 1]; + ret = avcodec_parameters_copy(ost->st->codecpar, ctx->par_out); + if (ret < 0) + return ret; + + ost->st->time_base = ctx->time_base_out; + + return 0; +} + static int init_output_stream(OutputStream *ost, char *error, int error_len) { int ret = 0; @@ -2696,6 +2769,13 @@ static int init_output_stream(OutputStream *ost, char *error, int error_len) return ret; } + /* initialize bitstream filters for the output stream + * needs to be done here, because the codec id for streamcopy is not + * known until now */ + ret = init_output_bsfs(ost); + if (ret < 0) + return ret; + return ret; } |