From patchwork Sat Apr 25 18:55:53 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marton Balint X-Patchwork-Id: 19238 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 470F144AB30 for ; Sat, 25 Apr 2020 21:56:08 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 1D21868C4DA; Sat, 25 Apr 2020 21:56:08 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from iq.passwd.hu (iq.passwd.hu [217.27.212.140]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 7335A68BEEC for ; Sat, 25 Apr 2020 21:56:02 +0300 (EEST) Received: from localhost (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id 554F2E3DF6; Sat, 25 Apr 2020 20:56:02 +0200 (CEST) X-Virus-Scanned: amavisd-new at passwd.hu Received: from iq.passwd.hu ([127.0.0.1]) by localhost (iq.passwd.hu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 6tRP2NMLsyCg; Sat, 25 Apr 2020 20:56:00 +0200 (CEST) Received: from bluegene.passwd.hu (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id 34C9BE366A; Sat, 25 Apr 2020 20:56:00 +0200 (CEST) From: Marton Balint To: ffmpeg-devel@ffmpeg.org Date: Sat, 25 Apr 2020 20:55:53 +0200 Message-Id: <20200425185555.19111-1-cus@passwd.hu> X-Mailer: git-send-email 2.16.4 Subject: [FFmpeg-devel] [PATCH 1/3] fftools/ffmpeg: use a bsf list instead of individual bsfs X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Marton Balint MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Unfortunately the parsing part cannot use the API because it is supporting shorthand options and the API is not. Signed-off-by: Marton Balint --- fftools/ffmpeg.c | 72 ++++++++++++++-------------------------------------- fftools/ffmpeg.h | 3 +-- fftools/ffmpeg_opt.c | 31 ++++++++++++++-------- 3 files changed, 40 insertions(+), 66 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index d896b14a14..e46575481b 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -558,9 +558,7 @@ static void ffmpeg_cleanup(int ret) if (!ost) continue; - for (j = 0; j < ost->nb_bitstream_filters; j++) - av_bsf_free(&ost->bsf_ctx[j]); - av_freep(&ost->bsf_ctx); + av_bsf_free(&ost->bsf_ctx); av_frame_free(&ost->filtered_frame); av_frame_free(&ost->last_frame); @@ -859,40 +857,15 @@ static void output_packet(OutputFile *of, AVPacket *pkt, { 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], eof ? NULL : pkt); + /* apply the output bitstream filters */ + if (ost->bsf_ctx) { + ret = av_bsf_send_packet(ost->bsf_ctx, eof ? NULL : pkt); if (ret < 0) goto finish; - - eof = 0; - 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); - if (ret == AVERROR(EAGAIN)) { - ret = 0; - idx--; - continue; - } else if (ret == AVERROR_EOF) { - eof = 1; - } 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) { - ret = av_bsf_send_packet(ost->bsf_ctx[idx], eof ? NULL : pkt); - if (ret < 0) - goto finish; - idx++; - eof = 0; - } else if (eof) - goto finish; - else - write_packet(of, pkt, ost, 0); - } + while ((ret = av_bsf_receive_packet(ost->bsf_ctx, pkt)) >= 0) + write_packet(of, pkt, ost, 0); + if (ret == AVERROR(EAGAIN)) + ret = 0; } else if (!eof) write_packet(of, pkt, ost, 0); @@ -3015,35 +2988,28 @@ static int check_init_output_file(OutputFile *of, int file_index) static int init_output_bsfs(OutputStream *ost) { - AVBSFContext *ctx; + AVBSFContext *ctx = ost->bsf_ctx; int i, ret; - if (!ost->nb_bitstream_filters) + if (!ctx) 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; + ret = avcodec_parameters_copy(ctx->par_in, 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; + ctx->time_base_in = ost->st->time_base; - ret = av_bsf_init(ctx); - if (ret < 0) { - av_log(NULL, AV_LOG_ERROR, "Error initializing bitstream filter: %s\n", - ost->bsf_ctx[i]->filter->name); - return ret; - } + ret = av_bsf_init(ctx); + if (ret < 0) { + av_log(NULL, AV_LOG_ERROR, "Error initializing bitstream filter: %s\n", + ctx->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; diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index fbaae15377..f2e30300c1 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -459,8 +459,7 @@ typedef struct OutputStream { AVRational mux_timebase; AVRational enc_timebase; - int nb_bitstream_filters; - AVBSFContext **bsf_ctx; + AVBSFContext *bsf_ctx; AVCodecContext *enc_ctx; AVCodecParameters *ref_par; /* associated input codec parameters with encoders options applied */ diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c index 680f0f1dfb..b52aa28626 100644 --- a/fftools/ffmpeg_opt.c +++ b/fftools/ffmpeg_opt.c @@ -1416,6 +1416,7 @@ static OutputStream *new_output_stream(OptionsContext *o, AVFormatContext *oc, e { OutputStream *ost; AVStream *st = avformat_new_stream(oc, NULL); + AVBSFList *bsf_list = NULL; int idx = oc->nb_streams - 1, ret = 0; const char *bsfs = NULL, *time_base = NULL; char *next, *codec_tag = NULL; @@ -1538,6 +1539,7 @@ static OutputStream *new_output_stream(OptionsContext *o, AVFormatContext *oc, e while (bsfs && *bsfs) { const AVBitStreamFilter *filter; char *bsf, *bsf_options_str, *bsf_name; + AVBSFContext *bsf_ctx; bsf = av_get_token(&bsfs, ","); if (!bsf) @@ -1552,38 +1554,45 @@ static OutputStream *new_output_stream(OptionsContext *o, AVFormatContext *oc, e exit_program(1); } - ost->bsf_ctx = av_realloc_array(ost->bsf_ctx, - ost->nb_bitstream_filters + 1, - sizeof(*ost->bsf_ctx)); - if (!ost->bsf_ctx) - exit_program(1); - - ret = av_bsf_alloc(filter, &ost->bsf_ctx[ost->nb_bitstream_filters]); + ret = av_bsf_alloc(filter, &bsf_ctx); if (ret < 0) { av_log(NULL, AV_LOG_ERROR, "Error allocating a bitstream filter context\n"); exit_program(1); } - ost->nb_bitstream_filters++; - if (bsf_options_str && filter->priv_class) { - const AVOption *opt = av_opt_next(ost->bsf_ctx[ost->nb_bitstream_filters-1]->priv_data, NULL); + const AVOption *opt = av_opt_next(bsf_ctx->priv_data, NULL); const char * shorthand[2] = {NULL}; if (opt) shorthand[0] = opt->name; - ret = av_opt_set_from_string(ost->bsf_ctx[ost->nb_bitstream_filters-1]->priv_data, bsf_options_str, shorthand, "=", ":"); + ret = av_opt_set_from_string(bsf_ctx->priv_data, bsf_options_str, shorthand, "=", ":"); if (ret < 0) { av_log(NULL, AV_LOG_ERROR, "Error parsing options for bitstream filter %s\n", bsf_name); exit_program(1); } } + + if (!bsf_list) + bsf_list = av_bsf_list_alloc(); + if (!bsf_list || av_bsf_list_append(bsf_list, bsf_ctx) < 0) { + av_log(NULL, AV_LOG_ERROR, "Failed to allocate or append to bsf list\n"); + exit_program(1); + } + av_freep(&bsf); if (*bsfs) bsfs++; } + if (bsf_list) { + ret = av_bsf_list_finalize(&bsf_list, &ost->bsf_ctx); + if (ret < 0) { + av_log(NULL, AV_LOG_ERROR, "Failed to finalize bsf list\n"); + exit_program(1); + } + } MATCH_PER_STREAM_OPT(codec_tags, str, codec_tag, oc, st); if (codec_tag) { From patchwork Sat Apr 25 18:55:54 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marton Balint X-Patchwork-Id: 19239 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 1005B44AB30 for ; Sat, 25 Apr 2020 21:56:12 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id EEAFB68C4D9; Sat, 25 Apr 2020 21:56:11 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from iq.passwd.hu (iq.passwd.hu [217.27.212.140]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 3F01B68BEEC for ; Sat, 25 Apr 2020 21:56:05 +0300 (EEST) Received: from localhost (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id 1ED25E41A6; Sat, 25 Apr 2020 20:56:05 +0200 (CEST) X-Virus-Scanned: amavisd-new at passwd.hu Received: from iq.passwd.hu ([127.0.0.1]) by localhost (iq.passwd.hu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id XwTEFPKNboFi; Sat, 25 Apr 2020 20:56:03 +0200 (CEST) Received: from bluegene.passwd.hu (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id 9F79FE366A; Sat, 25 Apr 2020 20:56:03 +0200 (CEST) From: Marton Balint To: ffmpeg-devel@ffmpeg.org Date: Sat, 25 Apr 2020 20:55:54 +0200 Message-Id: <20200425185555.19111-2-cus@passwd.hu> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20200425185555.19111-1-cus@passwd.hu> References: <20200425185555.19111-1-cus@passwd.hu> Subject: [FFmpeg-devel] [PATCH 2/3] avcodec/bsf: support shorthand options for av_bsf_list_parse_str X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Marton Balint MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Or maybe we should just drop supporting shorthand options instead? Signed-off-by: Marton Balint --- libavcodec/bsf.c | 53 ++++++++++++++++++++++++++--------------------------- 1 file changed, 26 insertions(+), 27 deletions(-) diff --git a/libavcodec/bsf.c b/libavcodec/bsf.c index d3a9db57f7..85b63a5c9a 100644 --- a/libavcodec/bsf.c +++ b/libavcodec/bsf.c @@ -431,7 +431,7 @@ int av_bsf_list_append(AVBSFList *lst, AVBSFContext *bsf) return av_dynarray_add_nofree(&lst->bsfs, &lst->nb_bsfs, bsf); } -int av_bsf_list_append2(AVBSFList *lst, const char *bsf_name, AVDictionary ** options) +static int bsf_list_append_internal(AVBSFList *lst, const char *bsf_name, const char *options, AVDictionary ** options_dict) { int ret; const AVBitStreamFilter *filter; @@ -445,8 +445,20 @@ int av_bsf_list_append2(AVBSFList *lst, const char *bsf_name, AVDictionary ** op if (ret < 0) return ret; - if (options) { - ret = av_opt_set_dict2(bsf, options, AV_OPT_SEARCH_CHILDREN); + if (options && filter->priv_class) { + const AVOption *opt = av_opt_next(bsf->priv_data, NULL); + const char * shorthand[2] = {NULL}; + + if (opt) + shorthand[0] = opt->name; + + ret = av_opt_set_from_string(bsf->priv_data, options, shorthand, "=", ":"); + if (ret < 0) + goto end; + } + + if (options_dict) { + ret = av_opt_set_dict2(bsf, options_dict, AV_OPT_SEARCH_CHILDREN); if (ret < 0) goto end; } @@ -460,6 +472,11 @@ end: return ret; } +int av_bsf_list_append2(AVBSFList *lst, const char *bsf_name, AVDictionary ** options) +{ + return bsf_list_append_internal(lst, bsf_name, NULL, options); +} + int av_bsf_list_finalize(AVBSFList **lst, AVBSFContext **bsf) { int ret = 0; @@ -486,33 +503,15 @@ end: return ret; } -static int bsf_parse_single(const char *str, AVBSFList *bsf_lst) +static int bsf_parse_single(char *str, AVBSFList *bsf_lst) { - char *bsf_name, *bsf_options_str, *buf; - AVDictionary *bsf_options = NULL; - int ret = 0; + char *bsf_name, *bsf_options_str; - if (!(buf = av_strdup(str))) - return AVERROR(ENOMEM); - - bsf_name = av_strtok(buf, "=", &bsf_options_str); - if (!bsf_name) { - ret = AVERROR(EINVAL); - goto end; - } - - if (bsf_options_str) { - ret = av_dict_parse_string(&bsf_options, bsf_options_str, "=", ":", 0); - if (ret < 0) - goto end; - } - - ret = av_bsf_list_append2(bsf_lst, bsf_name, &bsf_options); + bsf_name = av_strtok(str, "=", &bsf_options_str); + if (!bsf_name) + return AVERROR(EINVAL); -end: - av_dict_free(&bsf_options); - av_free(buf); - return ret; + return bsf_list_append_internal(bsf_lst, bsf_name, bsf_options_str, NULL); } int av_bsf_list_parse_str(const char *str, AVBSFContext **bsf_lst) From patchwork Sat Apr 25 18:55:55 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marton Balint X-Patchwork-Id: 19240 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 3C57E44AB30 for ; Sat, 25 Apr 2020 21:56:16 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 20F0D68C4EB; Sat, 25 Apr 2020 21:56:16 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from iq.passwd.hu (iq.passwd.hu [217.27.212.140]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 51DA068C4E9 for ; Sat, 25 Apr 2020 21:56:10 +0300 (EEST) Received: from localhost (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id 0F2C3E3DF6; Sat, 25 Apr 2020 20:56:10 +0200 (CEST) X-Virus-Scanned: amavisd-new at passwd.hu Received: from iq.passwd.hu ([127.0.0.1]) by localhost (iq.passwd.hu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id PqNPOabvrlL1; Sat, 25 Apr 2020 20:56:07 +0200 (CEST) Received: from bluegene.passwd.hu (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id A0896E366A; Sat, 25 Apr 2020 20:56:07 +0200 (CEST) From: Marton Balint To: ffmpeg-devel@ffmpeg.org Date: Sat, 25 Apr 2020 20:55:55 +0200 Message-Id: <20200425185555.19111-3-cus@passwd.hu> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20200425185555.19111-1-cus@passwd.hu> References: <20200425185555.19111-1-cus@passwd.hu> Subject: [FFmpeg-devel] [PATCH 3/3] fftools/ffmpeg_opt: use av_bsf_list_parse_str for parsing bsf lists X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Marton Balint MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: Marton Balint --- fftools/ffmpeg_opt.c | 57 +++------------------------------------------------- 1 file changed, 3 insertions(+), 54 deletions(-) diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c index b52aa28626..dc42fb19d6 100644 --- a/fftools/ffmpeg_opt.c +++ b/fftools/ffmpeg_opt.c @@ -1416,7 +1416,6 @@ static OutputStream *new_output_stream(OptionsContext *o, AVFormatContext *oc, e { OutputStream *ost; AVStream *st = avformat_new_stream(oc, NULL); - AVBSFList *bsf_list = NULL; int idx = oc->nb_streams - 1, ret = 0; const char *bsfs = NULL, *time_base = NULL; char *next, *codec_tag = NULL; @@ -1536,60 +1535,10 @@ static OutputStream *new_output_stream(OptionsContext *o, AVFormatContext *oc, e MATCH_PER_STREAM_OPT(copy_prior_start, i, ost->copy_prior_start, oc ,st); MATCH_PER_STREAM_OPT(bitstream_filters, str, bsfs, oc, st); - while (bsfs && *bsfs) { - const AVBitStreamFilter *filter; - char *bsf, *bsf_options_str, *bsf_name; - AVBSFContext *bsf_ctx; - - bsf = av_get_token(&bsfs, ","); - if (!bsf) - exit_program(1); - bsf_name = av_strtok(bsf, "=", &bsf_options_str); - if (!bsf_name) - exit_program(1); - - filter = av_bsf_get_by_name(bsf_name); - if (!filter) { - av_log(NULL, AV_LOG_FATAL, "Unknown bitstream filter %s\n", bsf_name); - exit_program(1); - } - - ret = av_bsf_alloc(filter, &bsf_ctx); - if (ret < 0) { - av_log(NULL, AV_LOG_ERROR, "Error allocating a bitstream filter context\n"); - exit_program(1); - } - - if (bsf_options_str && filter->priv_class) { - const AVOption *opt = av_opt_next(bsf_ctx->priv_data, NULL); - const char * shorthand[2] = {NULL}; - - if (opt) - shorthand[0] = opt->name; - - ret = av_opt_set_from_string(bsf_ctx->priv_data, bsf_options_str, shorthand, "=", ":"); - if (ret < 0) { - av_log(NULL, AV_LOG_ERROR, "Error parsing options for bitstream filter %s\n", bsf_name); - exit_program(1); - } - } - - if (!bsf_list) - bsf_list = av_bsf_list_alloc(); - if (!bsf_list || av_bsf_list_append(bsf_list, bsf_ctx) < 0) { - av_log(NULL, AV_LOG_ERROR, "Failed to allocate or append to bsf list\n"); - exit_program(1); - } - - av_freep(&bsf); - - if (*bsfs) - bsfs++; - } - if (bsf_list) { - ret = av_bsf_list_finalize(&bsf_list, &ost->bsf_ctx); + if (bsfs && *bsfs) { + ret = av_bsf_list_parse_str(bsfs, &ost->bsf_ctx); if (ret < 0) { - av_log(NULL, AV_LOG_ERROR, "Failed to finalize bsf list\n"); + av_log(NULL, AV_LOG_ERROR, "Error parsing bitstream filter sequence '%s': %s\n", bsfs, av_err2str(ret)); exit_program(1); } }