From patchwork Fri Aug 5 15:20:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pierre-Anthony Lemieux X-Patchwork-Id: 37148 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:92a9:b0:8d:7f3b:94fc with SMTP id q41csp260973pzg; Fri, 5 Aug 2022 08:21:15 -0700 (PDT) X-Google-Smtp-Source: AA6agR7IVkiW7Kn6UXRXdAUNUxe2FiY+1nb0JPJMZJtmo7PwyQXj2pg0/tU3+4q5z30jWEoKyfvA X-Received: by 2002:a05:6402:1909:b0:43d:d495:5eff with SMTP id e9-20020a056402190900b0043dd4955effmr6944499edz.397.1659712875127; Fri, 05 Aug 2022 08:21:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659712875; cv=none; d=google.com; s=arc-20160816; b=xke/Knq8RJY3FEHHdKWjF+HnGjym4sameH9c8aa5oISrxRazkS7wcCp9lV1AKvE5Gl xhLtbutGeoigwat4WmREKXYDxfCVkbdy0uVb05m1YXqAaps8k3HCdquJtkIRHM8LWfTt rvKcwTA++VzK6sYArGNLuuzuCcYtB2tMlLz0M2dmN4n7el8yKwq7HMOeNNbRJkBp/rne HTputKisX/qcsy5Zh/b+b+7DsF0oa/IIMPf6yfLMtfgykDeBF3MMT/1AramG9rKyJjUJ qjXtwJxI9Nj93SkvnhbPdpZliUSVlTh8nTV/GawIRDl9VN/foYHe8Y5oDPQ1sAJKakmD WB4Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:mime-version:message-id:date:to:from :delivered-to; bh=Y3PqjLCIBdPz9kS/uSKVysEwUUwIbKu5LxEB0bG4J60=; b=cbrCgZ7clL6BReJg/WPRAjgdeMQUXz6S6tK1tqv+UZBRn+r6fu3LgczjYvkYiss2zX +FpvusaV7jk1rwXueL7CPqWlreAyUG4mPwvT0nU/hfyMT0LHDEBaBJt/NG4uktTJRZSA kfoD9NuIY7eAcEZNNSGNIeN75HdwJZk6QvrBfU/bAu7648ljczDUAPoMcECoHML7Zcjz gMZg9yA/AaObqZHZ80yeE9uoA6/Dq/SdT+qyHpozvRH3Q3eqIzKJw0QGXv2laYtF53zP TwB53RmewiykwtP/qASeMcGuriZfUx3ucuu7VLseNTuOdFsIhMPC30tKISNnQH6MwNUY 86kQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id hg2-20020a1709072cc200b00730947701dbsi2390177ejc.485.2022.08.05.08.21.07; Fri, 05 Aug 2022 08:21:15 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id E642A68B5AE; Fri, 5 Aug 2022 18:21:03 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pf1-f177.google.com (mail-pf1-f177.google.com [209.85.210.177]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id E2D6C689CDD for ; Fri, 5 Aug 2022 18:20:57 +0300 (EEST) Received: by mail-pf1-f177.google.com with SMTP id k14so514534pfh.0 for ; Fri, 05 Aug 2022 08:20:57 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=j2L46O6xKFMqnCARimdLglBzXN0GAG7VdeB+A1i3G4g=; b=g3pRHH1ix/PbPpPF2Y8b1mHstm334iLciAM3LKinqeMWHVQoyF9jaEg1FaOZfSL6m2 IbNLOzKld6qtesq9PMLG80u4sw27bgcTClCVoEdiRAMTzF7aVVd8ioYfokIJDpT1vNME QK+pk+HDoX5ftj+3MTsK+QxmdyJwDBCsB/XqvcyRIRnZn5aiJFcmE+iv+A9kfpFp4hGY DwotjBptiMv1HoqeWAYgyPqePzottge+8NyGjbBLhna//mloZUVFHz3zQvD575xWznP9 +aJ0GDl6b+SRKLfxZCFJYgBgEzhZi1JuagamR54rHYz/kik4JCTmueb0MXFRwYZh+eRI OfPQ== X-Gm-Message-State: ACgBeo238c4hSR4Q3hm8z5OYubPXYhgpmmGpYZot89VH4Xv3zSCvlZIF 352k9PPp/A3z8SP87KpGlUn3RxSxYOA= X-Received: by 2002:a63:4741:0:b0:41b:ba42:9b2e with SMTP id w1-20020a634741000000b0041bba429b2emr6121212pgk.209.1659712855454; Fri, 05 Aug 2022 08:20:55 -0700 (PDT) Received: from localhost (76-14-89-2.sf-cable.astound.net. [76.14.89.2]) by smtp.gmail.com with ESMTPSA id g64-20020a625243000000b0052d6ad246a4sm3229978pfb.144.2022.08.05.08.20.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Aug 2022 08:20:55 -0700 (PDT) Received: by localhost (sSMTP sendmail emulation); Fri, 05 Aug 2022 08:20:33 -0700 From: pal@sandflow.com To: ffmpeg-devel@ffmpeg.org Date: Fri, 5 Aug 2022 08:20:31 -0700 Message-Id: <20220805152032.1382-1-pal@sandflow.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v6 1/2] avformat: refactor ff_stream_encode_params_copy() to stream_params_copy() X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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: Pierre-Anthony Lemieux Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: yB5hE5wiFKtT From: Pierre-Anthony Lemieux Addresses http://ffmpeg.org/pipermail/ffmpeg-devel/2022-August/299685.html --- libavformat/avformat.c | 68 ++++++++++++++++++++++++++++++++++++++++ libavformat/fifo.c | 8 ++--- libavformat/internal.h | 12 +++++++ libavformat/mux.h | 9 ------ libavformat/mux_utils.c | 28 ----------------- libavformat/segment.c | 6 ++-- libavformat/tee.c | 7 ++--- libavformat/webm_chunk.c | 6 ++-- 8 files changed, 89 insertions(+), 55 deletions(-) diff --git a/libavformat/avformat.c b/libavformat/avformat.c index 30d6ea6a49..64c9d71a24 100644 --- a/libavformat/avformat.c +++ b/libavformat/avformat.c @@ -235,6 +235,74 @@ int ff_stream_side_data_copy(AVStream *dst, const AVStream *src) return 0; } +/** + * Copy all stream parameters from source to destination stream, with the + * exception of: + * * the index field, which is usually set by avformat_new_stream() + * * the attached_pic field, if attached_pic.size is 0 or less + * + * @param dst pointer to destination AVStream + * @param src pointer to source AVStream + * @return >=0 on success, AVERROR code on error + */ +static int stream_params_copy(AVStream *dst, const AVStream *src) +{ + int ret; + + dst->id = src->id; + dst->time_base = src->time_base; + dst->start_time = src->start_time; + dst->duration = src->duration; + dst->nb_frames = src->nb_frames; + dst->disposition = src->disposition; + dst->discard = src->discard; + dst->sample_aspect_ratio = src->sample_aspect_ratio; + dst->avg_frame_rate = src->avg_frame_rate; + dst->event_flags = src->event_flags; + dst->r_frame_rate = src->r_frame_rate; + dst->pts_wrap_bits = src->pts_wrap_bits; + + av_dict_free(&dst->metadata); + ret = av_dict_copy(&dst->metadata, src->metadata, 0); + if (ret < 0) + return ret; + + ret = avcodec_parameters_copy(dst->codecpar, src->codecpar); + if (ret < 0) + return ret; + + ret = ff_stream_side_data_copy(dst, src); + if (ret < 0) + return ret; + + av_packet_unref(&dst->attached_pic); + if (src->attached_pic.size > 0) { + ret = av_packet_ref(&dst->attached_pic, &src->attached_pic); + if (ret < 0) + return ret; + } + + return 0; +} + +AVStream *ff_stream_clone(AVFormatContext *dst_ctx, const AVStream *src) +{ + AVStream *st; + int ret; + + st = avformat_new_stream(dst_ctx, NULL); + if (!st) + return NULL; + + ret = stream_params_copy(st, src); + if (ret < 0) { + ff_remove_stream(dst_ctx, st); + return NULL; + } + + return st; +} + AVProgram *av_new_program(AVFormatContext *ac, int id) { AVProgram *program = NULL; diff --git a/libavformat/fifo.c b/libavformat/fifo.c index ead2bdc5cf..55d413b952 100644 --- a/libavformat/fifo.c +++ b/libavformat/fifo.c @@ -505,13 +505,11 @@ static int fifo_mux_init(AVFormatContext *avf, const AVOutputFormat *oformat, avf2->flags = avf->flags; for (i = 0; i < avf->nb_streams; ++i) { - AVStream *st = avformat_new_stream(avf2, NULL); + AVStream *st = NULL; + + st = ff_stream_clone(avf2, avf->streams[i]); if (!st) return AVERROR(ENOMEM); - - ret = ff_stream_encode_params_copy(st, avf->streams[i]); - if (ret < 0) - return ret; } return 0; diff --git a/libavformat/internal.h b/libavformat/internal.h index b6b8fbf56f..6dd763950f 100644 --- a/libavformat/internal.h +++ b/libavformat/internal.h @@ -625,6 +625,18 @@ enum AVCodecID ff_get_pcm_codec_id(int bps, int flt, int be, int sflags); */ int ff_stream_side_data_copy(AVStream *dst, const AVStream *src); +/** + * Create a new stream and copy to it all parameters from a source stream, + * with the exception of: + * * the index field, which is set when the new stream is created + * * the attached_pic field, if attached_pic.size is 0 or less + * + * @param dst_ctx pointer to the context in which the new stream is created + * @param src pointer to source AVStream + * @return pointer to the new stream or NULL on error + */ +AVStream *ff_stream_clone(AVFormatContext *dst_ctx, const AVStream *src); + /** * Wrap ffurl_move() and log if error happens. * diff --git a/libavformat/mux.h b/libavformat/mux.h index c01da82194..1bfcaf795f 100644 --- a/libavformat/mux.h +++ b/libavformat/mux.h @@ -113,15 +113,6 @@ int ff_format_shift_data(AVFormatContext *s, int64_t read_start, int shift_size) */ int ff_format_output_open(AVFormatContext *s, const char *url, AVDictionary **options); -/** - * Copy encoding parameters from source to destination stream - * - * @param dst pointer to destination AVStream - * @param src pointer to source AVStream - * @return >=0 on success, AVERROR code on error - */ -int ff_stream_encode_params_copy(AVStream *dst, const AVStream *src); - /** * Parse creation_time in AVFormatContext metadata if exists and warn if the * parsing fails. diff --git a/libavformat/mux_utils.c b/libavformat/mux_utils.c index eb8ea3d560..2fa2ab5b0f 100644 --- a/libavformat/mux_utils.c +++ b/libavformat/mux_utils.c @@ -121,34 +121,6 @@ int ff_format_output_open(AVFormatContext *s, const char *url, AVDictionary **op return 0; } -int ff_stream_encode_params_copy(AVStream *dst, const AVStream *src) -{ - int ret; - - dst->id = src->id; - dst->time_base = src->time_base; - dst->nb_frames = src->nb_frames; - dst->disposition = src->disposition; - dst->sample_aspect_ratio = src->sample_aspect_ratio; - dst->avg_frame_rate = src->avg_frame_rate; - dst->r_frame_rate = src->r_frame_rate; - - av_dict_free(&dst->metadata); - ret = av_dict_copy(&dst->metadata, src->metadata, 0); - if (ret < 0) - return ret; - - ret = avcodec_parameters_copy(dst->codecpar, src->codecpar); - if (ret < 0) - return ret; - - ret = ff_stream_side_data_copy(dst, src); - if (ret < 0) - return ret; - - return 0; -} - int ff_parse_creation_time_metadata(AVFormatContext *s, int64_t *timestamp, int return_seconds) { AVDictionaryEntry *entry; diff --git a/libavformat/segment.c b/libavformat/segment.c index fa435d9f32..fc1810e074 100644 --- a/libavformat/segment.c +++ b/libavformat/segment.c @@ -167,11 +167,9 @@ static int segment_mux_init(AVFormatContext *s) AVStream *st, *ist = s->streams[i]; AVCodecParameters *ipar = ist->codecpar, *opar; - if (!(st = avformat_new_stream(oc, NULL))) + st = ff_stream_clone(oc, ist); + if (! st) return AVERROR(ENOMEM); - ret = ff_stream_encode_params_copy(st, ist); - if (ret < 0) - return ret; opar = st->codecpar; if (!oc->oformat->codec_tag || av_codec_get_id (oc->oformat->codec_tag, ipar->codec_tag) == opar->codec_id || diff --git a/libavformat/tee.c b/libavformat/tee.c index f1f2a9d2a5..acbd3dfb84 100644 --- a/libavformat/tee.c +++ b/libavformat/tee.c @@ -284,14 +284,11 @@ static int open_slave(AVFormatContext *avf, char *slave, TeeSlave *tee_slave) } tee_slave->stream_map[i] = stream_count++; - if (!(st2 = avformat_new_stream(avf2, NULL))) { + st2 = ff_stream_clone(avf2, st); + if (! st2) { ret = AVERROR(ENOMEM); goto end; } - - ret = ff_stream_encode_params_copy(st2, st); - if (ret < 0) - goto end; } ret = ff_format_output_open(avf2, filename, &options); diff --git a/libavformat/webm_chunk.c b/libavformat/webm_chunk.c index d69db3a004..9e71a1209d 100644 --- a/libavformat/webm_chunk.c +++ b/libavformat/webm_chunk.c @@ -91,12 +91,10 @@ static int webm_chunk_init(AVFormatContext *s) if ((ret = av_dict_copy(&oc->metadata, s->metadata, 0)) < 0) return ret; - if (!(st = avformat_new_stream(oc, NULL))) + st = ff_stream_clone(oc, ost); + if (!st) return AVERROR(ENOMEM); - if ((ret = ff_stream_encode_params_copy(st, ost)) < 0) - return ret; - if (wc->http_method) if ((ret = av_dict_set(&dict, "method", wc->http_method, 0)) < 0) return ret;