From patchwork Mon Mar 2 04:44:37 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 17996 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 D9878449CCD for ; Mon, 2 Mar 2020 06:45:07 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id BD44B68B1DC; Mon, 2 Mar 2020 06:45:07 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f52.google.com (mail-wm1-f52.google.com [209.85.128.52]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 9162F68B14F for ; Mon, 2 Mar 2020 06:45:00 +0200 (EET) Received: by mail-wm1-f52.google.com with SMTP id z12so9553909wmi.4 for ; Sun, 01 Mar 2020 20:45:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=LZEqpZ9hYxLfJ+RYnOk4LuNRrtlK52y5nSlou9Isy8Q=; b=d8a4JQN1mrl2v2mwzD9/ucYNPr9PMPlJV8UbE75p9x6n1AjJPKQSALSJDrMunMq2Kp +2bX9rQQlTtrAuxvPVwx3Wdevk3XdL7HY/RoI7qR3mKI3LWZn99o1wa394PlvLvnLgIa v6dtem+gJpEV3DoOcOMSG1TKGXEwl1tU1JnINpcCyYxoWJEamWgDWDxqfYOkluuumU08 i7ccP6bFmstzuj3+wHwonzQR/sckrliS0FLD4b1Ca7HKAaSX9xNm4S+YkYEGWq0EPvJY 2o4FekCU3df2wwahMl6xn3Ewnt1+kU4wGLObejukxkRV68FbHs65j4Y8H6peSQ1IsC/z vc8A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=LZEqpZ9hYxLfJ+RYnOk4LuNRrtlK52y5nSlou9Isy8Q=; b=SjHrIi7iEUADPww9Dlu9/3asomY2RP7D7QwCrV2B8rvcEpLAsrc3HJ1nfeWD/4Kese Aj44kS6hNenSThcd/xtTxt7nG6mnHv1xcr5iaOALc7qMLymLwgNkpUWX2DgKkLZFQY6c 5/8I7vUvnFLqEs9QmalSob8quFAfrUHVyklaq3cVFHuPw5H7reY/YGtnlJMIZ3t2Sng7 c4WxZEIvnzMWUaqDLsKu7MY1s3agL7MGf/BLETJ3xIjpWttEZPYOmZfinQo1hlfmVSPi Z9pJQ7BHBuKXdXHHvpgQ5rEdtabR+ZNAHjdqRrxFRObavzTCksknr3SHfXs+0xYywar6 01vA== X-Gm-Message-State: APjAAAXtPfzlw1r27Nf+7sUfdZ26ENiF0UrGTJEbB/5nJnLUjCfoengn pv/bhkUjuMuv5nJ5+t/4PyKacb81 X-Google-Smtp-Source: APXvYqyW70K3//dzbqtFowKdeXiselIz60Ks6l/TIrp31I6Twmo8BkZaI+R6s0srd5O0AmVZ2vDJ1w== X-Received: by 2002:a1c:a746:: with SMTP id q67mr16816640wme.1.1583124299660; Sun, 01 Mar 2020 20:44:59 -0800 (PST) Received: from sblaptop.fritz.box (ipbcc1ab4b.dynamic.kabel-deutschland.de. [188.193.171.75]) by smtp.gmail.com with ESMTPSA id s22sm13130229wmc.16.2020.03.01.20.44.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 01 Mar 2020 20:44:59 -0800 (PST) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Mon, 2 Mar 2020 05:44:37 +0100 Message-Id: <20200302044445.14842-5-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200302043520.14165-1-andreas.rheinhardt@gmail.com> References: <20200302043520.14165-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 09/17] avformat/webm_chunk: Use API functions for child muxer 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: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" instead of calling the write_header/packet/trailer functions directly via the function pointers. Also, use distinct AVStreams for the child AVFormatContext (up until now the two AVFormatContexts shared their AVStreams because allocating their own was deemed too onerous). Using the function pointers directly meant that the Matroska muxer's init-function was never called, because init-functions were only introduced a few months after webm_chunk has been added and no one thought of/bothered to adapt webm_chunk for this (when the init-function was added in b287d7ea, the code setting the timebase was moved to it, so that the timebases were no longer set to ms-precision when using the webm_chunk muxer; this has been fixed after some time in 42a635dd by setting the timebases direcly (instead of calling the init-function)). And when 982a98a0 added a deinit-function for the Matroska muxer, it introduced memleaks in webm_chunk, because the child muxer's internal structures were no longer freed when calling write_trailer directly. (Given that the init function has never ever been called, the child muxer has never ever been properly initialized, so that the deinit-function was not called when freeing the child context.) This commit stops calling the function pointers directly and instead uses the standard API functions for muxers. This fixes the above mentioned memleaks. (Memleaks are still possible on error. This will be fixed in a future commit that adds a deinit-function to webm_chunk itself.) Signed-off-by: Andreas Rheinhardt --- libavformat/webm_chunk.c | 59 ++++++++++++++++++++++++++-------------- 1 file changed, 39 insertions(+), 20 deletions(-) diff --git a/libavformat/webm_chunk.c b/libavformat/webm_chunk.c index 08d825449e..3c00c562e5 100644 --- a/libavformat/webm_chunk.c +++ b/libavformat/webm_chunk.c @@ -61,6 +61,8 @@ static int chunk_mux_init(AVFormatContext *s) { WebMChunkContext *wc = s->priv_data; AVFormatContext *oc; + AVStream *st, *ost = s->streams[0]; + AVDictionary *dict = NULL; int ret; ret = avformat_alloc_output_context2(&wc->avf, wc->oformat, NULL, NULL); @@ -72,17 +74,42 @@ static int chunk_mux_init(AVFormatContext *s) oc->max_delay = s->max_delay; oc->flags = s->flags; oc->strict_std_compliance = s->strict_std_compliance; + oc->avoid_negative_ts = s->avoid_negative_ts; av_dict_copy(&oc->metadata, s->metadata, 0); - *(const AVClass**)oc->priv_data = oc->oformat->priv_class; - av_opt_set_defaults(oc->priv_data); - av_opt_set_int(oc->priv_data, "dash", 1, 0); - av_opt_set_int(oc->priv_data, "cluster_time_limit", wc->chunk_duration, 0); - av_opt_set_int(oc->priv_data, "live", 1, 0); + if (!(st = avformat_new_stream(oc, NULL))) + return AVERROR(ENOMEM); + + if ((ret = avcodec_parameters_copy(st->codecpar, ost->codecpar)) < 0 || + (ret = av_dict_copy(&st->metadata, ost->metadata, 0)) < 0) + return ret; + + st->sample_aspect_ratio = ost->sample_aspect_ratio; + st->disposition = ost->disposition; + avpriv_set_pts_info(st, ost->pts_wrap_bits, ost->time_base.num, + ost->time_base.den); + + av_dict_set_int(&dict, "dash", 1, 0); + av_dict_set_int(&dict, "cluster_time_limit", wc->chunk_duration, 0); + av_dict_set_int(&dict, "live", 1, 0); + + ret = avformat_init_output(oc, &dict); + av_dict_free(&dict); + if (ret < 0) + return ret; + + // Copy the timing info back to the original stream + // so that the timestamps of the packets are directly usable + avpriv_set_pts_info(ost, st->pts_wrap_bits, st->time_base.num, + st->time_base.den); - oc->streams = s->streams; - oc->nb_streams = s->nb_streams; + // This ensures that the timestamps will already be properly shifted + // when the packets arrive here, so we don't need to shift again. + s->avoid_negative_ts = oc->avoid_negative_ts; + s->internal->avoid_negative_ts_use_pts = + oc->internal->avoid_negative_ts_use_pts; + oc->avoid_negative_ts = 0; return 0; } @@ -119,7 +146,6 @@ static int webm_chunk_write_header(AVFormatContext *s) WebMChunkContext *wc = s->priv_data; AVFormatContext *oc = NULL; int ret; - int i; AVDictionary *options = NULL; char oc_filename[MAX_FILENAME_SIZE]; char *oc_url; @@ -152,14 +178,10 @@ static int webm_chunk_write_header(AVFormatContext *s) return ret; oc->pb->seekable = 0; - ret = oc->oformat->write_header(oc); + ret = avformat_write_header(oc, NULL); ff_format_io_close(s, &oc->pb); if (ret < 0) return ret; - for (i = 0; i < s->nb_streams; i++) { - // ms precision is the de-facto standard timescale for mkv files. - avpriv_set_pts_info(s->streams[i], 64, 1, 1000); - } return 0; } @@ -192,7 +214,7 @@ static int chunk_end(AVFormatContext *s, int flush) if (flush) // Flush the cluster in WebM muxer. - oc->oformat->write_packet(oc, NULL); + av_write_frame(oc, NULL); buffer_size = avio_close_dyn_buf(oc->pb, &buffer); oc->pb = NULL; ret = get_chunk_filename(s, 0, filename); @@ -239,9 +261,8 @@ static int webm_chunk_write_packet(AVFormatContext *s, AVPacket *pkt) } } - ret = oc->oformat->write_packet(oc, pkt); - - return ret; + // We only have one stream, so use the non-interleaving av_write_frame. + return av_write_frame(oc, pkt); } static int webm_chunk_write_trailer(AVFormatContext *s) @@ -255,11 +276,9 @@ static int webm_chunk_write_trailer(AVFormatContext *s) if (ret < 0) goto fail; } - oc->oformat->write_trailer(oc); + av_write_trailer(oc); ret = chunk_end(s, 0); fail: - oc->streams = NULL; - oc->nb_streams = 0; avformat_free_context(oc); return ret; }