From patchwork Thu Jan 23 16:08:27 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 17490 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 7E4CD44BAAD for ; Thu, 23 Jan 2020 18:08:49 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 5E96C68B069; Thu, 23 Jan 2020 18:08:49 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f66.google.com (mail-wr1-f66.google.com [209.85.221.66]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id E580568ADBE for ; Thu, 23 Jan 2020 18:08:42 +0200 (EET) Received: by mail-wr1-f66.google.com with SMTP id q6so3675912wro.9 for ; Thu, 23 Jan 2020 08:08:42 -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:mime-version :content-transfer-encoding; bh=7Ga8KCXNHdPJV/jTqjs1/iayjm5BLq+ocuWzcw/A5vM=; b=pXZRmGk/3vIlru0CkNdPeUZp8KMbGc2b2/0OCj5N0z7ew+NG/0+Q3dWcqZvMOjgYrX PKEVzowHb29LjHMc3PToLip0HgIX/raoao8YGB8Q/ra1/BU4xYABse62Ev4LJi5oa+Qf z9igck6rKPEKGJ0xH8AuYW3UXjXEvOPYu+RysCl2rnCrbXoR2xSsNHtlhuhfrE9cDRTc chw4TwBXae3EyRolORRsIeY3s4Xo2IrrKv7UE9PA/IktIWtAMk7Rciej0lq4v8L35lHt KTFr41KE7N4lAJvjSxW98LIhlQJbOF6aNPJnRl8dK/7c5hb0X7cfDisTJ85NP8GPrT25 vHJw== 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:mime-version :content-transfer-encoding; bh=7Ga8KCXNHdPJV/jTqjs1/iayjm5BLq+ocuWzcw/A5vM=; b=nluyWAPAhsXLwFnTbmb6jX415v75rC9ZHXJUTHEL+LN7iT9qRBX3xvfpiYak027MvR JPtaQMjhVekQUQuEBu9+LCdfMgXwwCNJREwubK2BB21g3YHghMIXg4nv3XRUBNMNYlS3 iTpZI/8gbr4THutqsOiRd7cHGPyzVbgJL016Kj6RV4HyzxkQHgRFyHZc4u3Jpcm9aTIM RoUb3CUYeAWlnwpOgnvizlifY9iSthjiAP0nPvcarKtYkPQMX339SNwtamM7f72n8jAV jDnKPXsiiOlBuYaVUXe7Ulpho7Bp2VlzYzsxOcsXfhSCaSd6rinldr4g1sgVJheN8WuR /H3A== X-Gm-Message-State: APjAAAWVCEtE8pUaEKMFXdzFqtFBOe95c1xIZPu7zslLHK7qa0nmyZYP iBFnDKkzqQg+btEyzfU2j1/jgfrO X-Google-Smtp-Source: APXvYqxFU9dB8OKk63LnG8FvOGsPoGAsXtzedDTRk4SUtsxZbatJjnm3OABuGk75UkjN3y7tYbj43g== X-Received: by 2002:a5d:4386:: with SMTP id i6mr18126680wrq.63.1579795721955; Thu, 23 Jan 2020 08:08:41 -0800 (PST) Received: from sblaptop.fritz.box (ipbcc08bbf.dynamic.kabel-deutschland.de. [188.192.139.191]) by smtp.gmail.com with ESMTPSA id o187sm3678419wme.36.2020.01.23.08.08.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Jan 2020 08:08:41 -0800 (PST) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Thu, 23 Jan 2020 17:08:27 +0100 Message-Id: <20200123160832.2020-1-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 1/6] avformat/matroskaenc: Check for reformatting errors 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" This is needed especially for AV1: If a reformatting error happens (e.g. if the length field of an OBU contained in the current packet indicates that said OBU extends beyond the current packet), the data pointer is still NULL, yet the size is unchanged, so that writing the data leads to a segmentation fault. Signed-off-by: Andreas Rheinhardt --- libavformat/matroskaenc.c | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/libavformat/matroskaenc.c b/libavformat/matroskaenc.c index 953421435d..a72bcb0ba1 100644 --- a/libavformat/matroskaenc.c +++ b/libavformat/matroskaenc.c @@ -2079,14 +2079,14 @@ fail: return ret; } -static void mkv_write_block(AVFormatContext *s, AVIOContext *pb, - uint32_t blockid, AVPacket *pkt, int keyframe) +static int mkv_write_block(AVFormatContext *s, AVIOContext *pb, + uint32_t blockid, AVPacket *pkt, int keyframe) { MatroskaMuxContext *mkv = s->priv_data; AVCodecParameters *par = s->streams[pkt->stream_index]->codecpar; mkv_track *track = &mkv->tracks[pkt->stream_index]; uint8_t *data = NULL, *side_data = NULL; - int offset = 0, size = pkt->size, side_data_size = 0; + int err = 0, offset = 0, size = pkt->size, side_data_size = 0; int64_t ts = track->write_dts ? pkt->dts : pkt->pts; uint64_t additional_id = 0; int64_t discard_padding = 0; @@ -2105,22 +2105,24 @@ static void mkv_write_block(AVFormatContext *s, AVIOContext *pb, mkv->cluster_pos, track_number, keyframe != 0); if (par->codec_id == AV_CODEC_ID_H264 && par->extradata_size > 0 && (AV_RB24(par->extradata) == 1 || AV_RB32(par->extradata) == 1)) - ff_avc_parse_nal_units_buf(pkt->data, &data, &size); + err = ff_avc_parse_nal_units_buf(pkt->data, &data, &size); else if (par->codec_id == AV_CODEC_ID_HEVC && par->extradata_size > 6 && (AV_RB24(par->extradata) == 1 || AV_RB32(par->extradata) == 1)) /* extradata is Annex B, assume the bitstream is too and convert it */ - ff_hevc_annexb2mp4_buf(pkt->data, &data, &size, 0, NULL); + err = ff_hevc_annexb2mp4_buf(pkt->data, &data, &size, 0, NULL); else if (par->codec_id == AV_CODEC_ID_AV1) - ff_av1_filter_obus_buf(pkt->data, &data, &size); + err = ff_av1_filter_obus_buf(pkt->data, &data, &size); else if (par->codec_id == AV_CODEC_ID_WAVPACK) { - int ret = mkv_strip_wavpack(pkt->data, &data, &size); - if (ret < 0) { - av_log(s, AV_LOG_ERROR, "Error stripping a WavPack packet.\n"); - return; - } + err = mkv_strip_wavpack(pkt->data, &data, &size); } else data = pkt->data; + if (err < 0) { + av_log(s, AV_LOG_ERROR, "Error when reformatting data of " + "a packet from stream %d.\n", pkt->stream_index); + return err; + } + if (par->codec_id == AV_CODEC_ID_PRORES && size >= 8) { /* Matroska specification requires to remove the first QuickTime atom */ @@ -2184,6 +2186,8 @@ static void mkv_write_block(AVFormatContext *s, AVIOContext *pb, if ((side_data_size && additional_id == 1) || discard_padding) { end_ebml_master(pb, block_group); } + + return 0; } static int mkv_write_vtt_blocks(AVFormatContext *s, AVIOContext *pb, AVPacket *pkt) @@ -2389,7 +2393,9 @@ static int mkv_write_packet_internal(AVFormatContext *s, AVPacket *pkt, int add_ relative_packet_pos = avio_tell(pb); if (par->codec_type != AVMEDIA_TYPE_SUBTITLE) { - mkv_write_block(s, pb, MATROSKA_ID_SIMPLEBLOCK, pkt, keyframe); + ret = mkv_write_block(s, pb, MATROSKA_ID_SIMPLEBLOCK, pkt, keyframe); + if (ret < 0) + return ret; if ((s->pb->seekable & AVIO_SEEKABLE_NORMAL) && (par->codec_type == AVMEDIA_TYPE_VIDEO && keyframe || add_cue)) { ret = mkv_add_cuepoint(mkv->cues, pkt->stream_index, tracknum, ts, mkv->cluster_pos, relative_packet_pos, -1); if (ret < 0) return ret; From patchwork Thu Jan 23 16:08:28 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 17491 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 864E544BAAD for ; Thu, 23 Jan 2020 18:08:54 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 7210768B3B6; Thu, 23 Jan 2020 18:08:54 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f66.google.com (mail-wm1-f66.google.com [209.85.128.66]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 8C8DF68B098 for ; Thu, 23 Jan 2020 18:08:47 +0200 (EET) Received: by mail-wm1-f66.google.com with SMTP id m24so3144477wmc.3 for ; Thu, 23 Jan 2020 08:08:47 -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=X9PtV1im8aCCDpx2VM1I62YgpLqb7J6kjbfv5lHdS4I=; b=AeJB/mCcfoXwyjn9alXoDZ379UCv+fAf6ozIkTEdc2ZBjg/zaCcmzAEtXP+x7O7t6x u+Gk60MWf+7ryCpKDTvOKO3iSr1s7B+VixoEQ7ogzq7+7bPdt70XjEAJNpPU8coTrro9 CuyGcVcbB0oSgxSrZzwGUtmVK7vGLX8kS/6vgEx+ADnceGDGiXaaFXkSdKuAzcDS1caJ iJiInOXpqV2yTyBmY7lKawrDHe0TDN7haPmJmdWIvuDM7lYh3YhzLtcL5OA/2c21sv+A pUIm7t93cHD1IqwrPicJ8Mz6DJYiE4dGmHuIFwBewiifpreoJs6Tk3eOikpDXYyF2p64 8vjA== 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=X9PtV1im8aCCDpx2VM1I62YgpLqb7J6kjbfv5lHdS4I=; b=hQ/JOI+XqeRZF2w6rT4scYg6iSxu+Cu8cP19koQDDP6Cd98JJ0PYNltSG+297DLVxM uPkvYKji7cJN5/q3nVLzDciBKVmf3sdQxHBBUqXOkS/j9TcAowZ/C2ps7LHrEqp6/OJD P2f2MZFroUWuvHS0iakYCnH9FiOxQodlbzFJKjUwuP+VPLqfxWLJQybgYikA4Dg1v4NQ akfJVKGTZpZgCynOoFFKWRluxb7vlqeaMs5zpnisXvTOr+G0EU8ww278EEeEW6KkIGp8 8F3nHlNkNqqpebGDAH9OH0Ez9+YyX3+fU7mwEwkg8I5eemsSxT6N/pA9dAxSuVscNyla PTzQ== X-Gm-Message-State: APjAAAUceAaHUfJY4CEQhFsZ7uRgBPdf4PfcDoGXV/cZvMp7z6uXcA0p GP40MCoDy/nXwmge1+bb1Dwxq0fo X-Google-Smtp-Source: APXvYqyZjAL5thH0XC8Fq0xd/dvTjLpf7EUC0fTrUwX+yR45m743udi1k74u9lpPgE6fwUR3lLlUiQ== X-Received: by 2002:a1c:80d4:: with SMTP id b203mr5029901wmd.102.1579795726836; Thu, 23 Jan 2020 08:08:46 -0800 (PST) Received: from sblaptop.fritz.box (ipbcc08bbf.dynamic.kabel-deutschland.de. [188.192.139.191]) by smtp.gmail.com with ESMTPSA id o187sm3678419wme.36.2020.01.23.08.08.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Jan 2020 08:08:46 -0800 (PST) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Thu, 23 Jan 2020 17:08:28 +0100 Message-Id: <20200123160832.2020-2-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200123160832.2020-1-andreas.rheinhardt@gmail.com> References: <20200123160832.2020-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/6] avformat/matroskaenc: Cosmetics 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" Add {, } in situations like if () ... else if () /* Comment */ ... else ... Signed-off-by: Andreas Rheinhardt --- libavformat/matroskaenc.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/libavformat/matroskaenc.c b/libavformat/matroskaenc.c index a72bcb0ba1..20bad95262 100644 --- a/libavformat/matroskaenc.c +++ b/libavformat/matroskaenc.c @@ -2103,16 +2103,17 @@ static int mkv_write_block(AVFormatContext *s, AVIOContext *pb, "at offset %" PRId64 ". TrackNumber %d, keyframe %d\n", pkt->size, pkt->pts, pkt->dts, pkt->duration, avio_tell(pb), mkv->cluster_pos, track_number, keyframe != 0); + if (par->codec_id == AV_CODEC_ID_H264 && par->extradata_size > 0 && - (AV_RB24(par->extradata) == 1 || AV_RB32(par->extradata) == 1)) + (AV_RB24(par->extradata) == 1 || AV_RB32(par->extradata) == 1)) { err = ff_avc_parse_nal_units_buf(pkt->data, &data, &size); - else if (par->codec_id == AV_CODEC_ID_HEVC && par->extradata_size > 6 && - (AV_RB24(par->extradata) == 1 || AV_RB32(par->extradata) == 1)) + } else if (par->codec_id == AV_CODEC_ID_HEVC && par->extradata_size > 6 && + (AV_RB24(par->extradata) == 1 || AV_RB32(par->extradata) == 1)) { /* extradata is Annex B, assume the bitstream is too and convert it */ err = ff_hevc_annexb2mp4_buf(pkt->data, &data, &size, 0, NULL); - else if (par->codec_id == AV_CODEC_ID_AV1) + } else if (par->codec_id == AV_CODEC_ID_AV1) { err = ff_av1_filter_obus_buf(pkt->data, &data, &size); - else if (par->codec_id == AV_CODEC_ID_WAVPACK) { + } else if (par->codec_id == AV_CODEC_ID_WAVPACK) { err = mkv_strip_wavpack(pkt->data, &data, &size); } else data = pkt->data; @@ -2133,7 +2134,6 @@ static int mkv_write_block(AVFormatContext *s, AVIOContext *pb, side_data = av_packet_get_side_data(pkt, AV_PKT_DATA_SKIP_SAMPLES, &side_data_size); - if (side_data && side_data_size >= 10) { discard_padding = av_rescale_q(AV_RL32(side_data + 4), (AVRational){1, par->sample_rate}, From patchwork Thu Jan 23 16:08:29 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 17492 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 6933844BAAD for ; Thu, 23 Jan 2020 18:08:56 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 5800468B43F; Thu, 23 Jan 2020 18:08:56 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f68.google.com (mail-wr1-f68.google.com [209.85.221.68]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 5D0BB68B098 for ; Thu, 23 Jan 2020 18:08:48 +0200 (EET) Received: by mail-wr1-f68.google.com with SMTP id w15so3720637wru.4 for ; Thu, 23 Jan 2020 08:08:48 -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=T2M+A50ragfZ3zMkXykZ1DbKxwm0td2wVCemQVeu0IA=; b=bbGya995jckyOl5UjVPyI8rergP6npE6Fr2G/k4rNzBqEtlhhgB3y7sVZd5en0m1Cp 2QezswTkmZ0oibiI1ETVS4f/kbU/6BR73+wvUXE0nM2Ke7PA3gDOeQTqc1mAVBxFMa2a Y+Geeb9ZeG2MXOdBc9724Pb5nB47EhFvIIgnd95kAOo+hf+YiKCyf8h8tLNw/XOsjd07 G+p7AvOIjh9vZgt3WEoSeXRpsoQVyDGXl4sj5wvPWejSd4VYTR5vjVsKLHUPgTYCM11l 0l70UDEnRF+JJ2Fg9WcHLPA9wNTgtilzePD4xWLurWUqOjExIeS1MPQeCRsPUMBFugDS n74w== 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=T2M+A50ragfZ3zMkXykZ1DbKxwm0td2wVCemQVeu0IA=; b=YOSpHKwingnwVjoNexRVAF2QOZqojeIBirKzWWQuhuKoNxlbrVedLlenQ+Ef2Cp9yn 0VhBhQv6qFRHd1VFBiW72paM49WB2j81DwF3M/Qzn/xrFZsbS9m+tS4NU9PbtoGMySOH INfvyrlaotL/YiPogUk2RXKa+M4qS4jHU956ah71cv24ujSF/srpWpYMnngRtaE20rD3 6PgrS/ueoMeE8djeck6Hod/X+3zeMcIJvfzVXGxX+D0OmB/LMR3OjYd86iMF2JBhs8lw S15LL5OHlyszTe0G2+10YVqZCSylK3tHHE6KHeSN4jr1ysgN5jdWuOrXfW4FCE835LG8 38sQ== X-Gm-Message-State: APjAAAWzwo5xHeTXUZFapfFvSyxA6IVU6YFQHDUgDw4W1hxColQYC0zU aVhD+i255NDJbTf71JOgurvXyxji X-Google-Smtp-Source: APXvYqxIz4rv3kkhnVo7OIFnLI9celwa5zoucKBZpEG1L27JGhXQLJ3TLaQ0S5xmHmTUQnQkCxJ7mA== X-Received: by 2002:adf:90e7:: with SMTP id i94mr17510366wri.47.1579795727687; Thu, 23 Jan 2020 08:08:47 -0800 (PST) Received: from sblaptop.fritz.box (ipbcc08bbf.dynamic.kabel-deutschland.de. [188.192.139.191]) by smtp.gmail.com with ESMTPSA id o187sm3678419wme.36.2020.01.23.08.08.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Jan 2020 08:08:47 -0800 (PST) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Thu, 23 Jan 2020 17:08:29 +0100 Message-Id: <20200123160832.2020-3-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200123160832.2020-1-andreas.rheinhardt@gmail.com> References: <20200123160832.2020-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 3/6] avformat/hevc: Fix potential leak in case of ff_hevc_annexb2mp4_buf failure 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" ff_hevc_annexb2mp4_buf() could indicate an error, yet leave cleaning after itself to the caller, so that a caller could not simply return the error, but had to free the buffer first. (Given that all current callers have set filter_ps = 0, this error can currently not be triggered.) Signed-off-by: Andreas Rheinhardt --- libavformat/hevc.c | 6 ++++++ libavformat/hevc.h | 8 ++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/libavformat/hevc.c b/libavformat/hevc.c index c7c4be3441..db931d2017 100644 --- a/libavformat/hevc.c +++ b/libavformat/hevc.c @@ -25,6 +25,7 @@ #include "libavutil/intreadwrite.h" #include "avc.h" #include "avio.h" +#include "avio_internal.h" #include "hevc.h" #define MAX_SPATIAL_SEGMENTATION 4096 // max. value of u(12) field @@ -1054,6 +1055,11 @@ int ff_hevc_annexb2mp4_buf(const uint8_t *buf_in, uint8_t **buf_out, return ret; ret = ff_hevc_annexb2mp4(pb, buf_in, *size, filter_ps, ps_count); + if (ret < 0) { + ffio_free_dyn_buf(&pb); + return ret; + } + *size = avio_close_dyn_buf(pb, buf_out); return ret; diff --git a/libavformat/hevc.h b/libavformat/hevc.h index 796eaf40b1..1e355cd34a 100644 --- a/libavformat/hevc.h +++ b/libavformat/hevc.h @@ -60,13 +60,13 @@ int ff_hevc_annexb2mp4(AVIOContext *pb, const uint8_t *buf_in, * If filter_ps is non-zero, any HEVC parameter sets found in the input will be * discarded, and *ps_count will be set to the number of discarded PS NAL units. * - * On output, *size holds the size (in bytes) of the output data buffer. + * On success, *size holds the size (in bytes) of the output data buffer. * * @param buf_in address of the buffer holding the input data * @param size address of the variable holding the size (in bytes) of the input - * buffer (on input) and of the output buffer (on output) - * @param buf_out address of the variable holding the address of the output - * buffer + * buffer (on input) and of the output buffer (on success) + * @param buf_out on success, address of the variable holding the address of + * the output buffer * @param filter_ps whether to write parameter set NAL units to the output (0) * or to discard them (non-zero) * @param ps_count address of the variable where the number of discarded From patchwork Thu Jan 23 16:08:30 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 17493 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 22E6544BAAD for ; Thu, 23 Jan 2020 18:08:57 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 12F3A68B494; Thu, 23 Jan 2020 18:08:57 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f67.google.com (mail-wm1-f67.google.com [209.85.128.67]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 3406968B0E2 for ; Thu, 23 Jan 2020 18:08:49 +0200 (EET) Received: by mail-wm1-f67.google.com with SMTP id p17so3116839wma.1 for ; Thu, 23 Jan 2020 08:08:49 -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=zYQMlqUGHZZIbGiWJMdD7gV48aJEdRxv/J0A39DxS/w=; b=FVslWbSF6Q/iuuLcYUnK/X9m6ZKTW+dSSzsNCDRQQZG22xAq5g35CUlgH7Q0bVHZX0 JeYB+2peW45Y8Ea27MxTDD8WSZzBSVwVXwrEFIeOolLFL4LdFvcj7SZlKlmDjjua2IJ6 j9mvMW3RP+FOe6K5pcM9OidBNCOkffKvZyxgkstbVVJubtWWbS4dMWPREs1fzsAYRfIa Lbfm0EZXcQ9ZCoJgR1DexHx3jZbM9nnGYsvQAQK8vSORkXeFTe1XDC7U/kkAfb9KUVm1 Ocx7hKKbb9b57hvmyPgZWcsqPBeFrg3vdSvg6Fly3uO4xgkVzhitXUXGxxdD70mh6fzm FSzw== 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=zYQMlqUGHZZIbGiWJMdD7gV48aJEdRxv/J0A39DxS/w=; b=Tl2QxlHsz/mc2lApUky4w0XW74eY/TAiP21D30ec8VC4lVy+c2euMDLIe4F2pphegv wYkn6LkHFdNuypl7Wyy+/fhJGw4NrtMUb5PoEiLN5bvpbMZURIOcdQHBP2T4g3VaXmVR amgmKYawVW7OXbmqy+u+4+ndDJPHelHET8wpnB8kBPbjrtMMvSEDcZr7kkzv6uCcj+NX K9+nG/S3oEbzy1Mus94bndGWhQNKiIDfzy642YjsSnPKx+DwZrYCQqXHPiarRYh4mL1Z jTHR0yimw8HNYrVNYHTcg1VUKiRVxFeS9MlqWZaangrMQLQYsE7yRbm27SYNIlZD41YG TyAg== X-Gm-Message-State: APjAAAXxteRVZxYkisuRIlHpu8nhGgxVmYjxMa/MQXhKyiJH7Lnu1WY8 oLQo4k+7iIWd8u76Nz0XG9LQRhKM X-Google-Smtp-Source: APXvYqy8Zke7xwM4wS4EY4FM1tET3VMYsZN1MPcfhzBiEEbgseJMmaLNE3LI8KeukSxiBSl8Rjfd5w== X-Received: by 2002:a05:600c:24d1:: with SMTP id 17mr4903329wmu.188.1579795728502; Thu, 23 Jan 2020 08:08:48 -0800 (PST) Received: from sblaptop.fritz.box (ipbcc08bbf.dynamic.kabel-deutschland.de. [188.192.139.191]) by smtp.gmail.com with ESMTPSA id o187sm3678419wme.36.2020.01.23.08.08.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Jan 2020 08:08:48 -0800 (PST) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Thu, 23 Jan 2020 17:08:30 +0100 Message-Id: <20200123160832.2020-4-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200123160832.2020-1-andreas.rheinhardt@gmail.com> References: <20200123160832.2020-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 4/6] avformat/av1, hevc: Make *_buf-functions return 0 on success 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" The output size is already returned via a pointer argument, so there is no need to return it via the ordinary return value as well. The rationale behind this is to not poison the return value on success. It also unifies the behaviour of the *_buf-functions for AVC, AV1 and HEVC. Signed-off-by: Andreas Rheinhardt --- libavformat/av1.c | 2 +- libavformat/av1.h | 4 ++-- libavformat/hevc.c | 2 +- libavformat/hevc.h | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/libavformat/av1.c b/libavformat/av1.c index 5ad9222900..4ff4bffddf 100644 --- a/libavformat/av1.c +++ b/libavformat/av1.c @@ -76,7 +76,7 @@ int ff_av1_filter_obus_buf(const uint8_t *buf, uint8_t **out, int *size) av_freep(out); *size = avio_close_dyn_buf(pb, out); - return ret; + return 0; } static inline void uvlc(GetBitContext *gb) diff --git a/libavformat/av1.h b/libavformat/av1.h index 9354889afe..0578435376 100644 --- a/libavformat/av1.h +++ b/libavformat/av1.h @@ -66,8 +66,8 @@ int ff_av1_filter_obus(AVIOContext *pb, const uint8_t *buf, int size); * @param size size of the input data buffer. The size of the resulting output data buffer will be written here * - * @return the amount of bytes written in case of success, a negative AVERROR - * code in case of failure. On failure, out and size are unchanged + * @return 0 in case of success, a negative AVERROR code in case of failure. + * On failure, out and size are unchanged */ int ff_av1_filter_obus_buf(const uint8_t *buf, uint8_t **out, int *size); diff --git a/libavformat/hevc.c b/libavformat/hevc.c index db931d2017..f621cb2f19 100644 --- a/libavformat/hevc.c +++ b/libavformat/hevc.c @@ -1062,7 +1062,7 @@ int ff_hevc_annexb2mp4_buf(const uint8_t *buf_in, uint8_t **buf_out, *size = avio_close_dyn_buf(pb, buf_out); - return ret; + return 0; } int ff_isom_write_hvcc(AVIOContext *pb, const uint8_t *data, diff --git a/libavformat/hevc.h b/libavformat/hevc.h index 1e355cd34a..bb144397c0 100644 --- a/libavformat/hevc.h +++ b/libavformat/hevc.h @@ -71,8 +71,8 @@ int ff_hevc_annexb2mp4(AVIOContext *pb, const uint8_t *buf_in, * or to discard them (non-zero) * @param ps_count address of the variable where the number of discarded * parameter set NAL units shall be written, may be NULL - * @return the amount (in bytes) of data written in case of success, a negative - * value corresponding to an AVERROR code in case of failure + * @return 0 in case of success, a negative value corresponding to an AVERROR + * code in case of failure */ int ff_hevc_annexb2mp4_buf(const uint8_t *buf_in, uint8_t **buf_out, int *size, int filter_ps, int *ps_count); From patchwork Thu Jan 23 16:08:31 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 17494 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 0079344BAAD for ; Thu, 23 Jan 2020 18:08:58 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id DE30568B49A; Thu, 23 Jan 2020 18:08:57 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f66.google.com (mail-wr1-f66.google.com [209.85.221.66]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id E8BEE68B319 for ; Thu, 23 Jan 2020 18:08:49 +0200 (EET) Received: by mail-wr1-f66.google.com with SMTP id c14so3692618wrn.7 for ; Thu, 23 Jan 2020 08:08:49 -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=I6R8A5mwx1J2WWtMuFft7UjGxpUu5Rc49vVVkwLtmtE=; b=A9MbTTaxBaabUlfrFPuyO7+/ZZy7/mjUVXvepz4OSEc1EIBtx4LuC6zYYNGLyLU0p9 e7gTPyT2LuN7yNOj5QEuunRsj5odj/gkTjjUSjBT9vVQR8+w9Ey81BSxmhaL2+PwavhS DvqRphv5UhIPQ2Cs1H3xBbVpnv6W4viglvzOEpd1nr1AkuVIDLLYsJnyyOLQWygkAFW9 1y3SGkKuXg4snnVUc8xM9lS77fiselfIhhGaOQ8YhaPr7eZDWMePL9xaIhyFHO/qJv6T 9bq42JE4pl16e8xc/wPug4J/JPOKtOyVj1DeSTUUlCMbuFFIT0MNDsU86vlVaFHyd/8p jyFQ== 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=I6R8A5mwx1J2WWtMuFft7UjGxpUu5Rc49vVVkwLtmtE=; b=U4k8AEnNkQHjeBtogdcpEz4nQ2VweZmz7i2O2h4ouMfI4CnLdeksB35A3gYHQniSbJ H3BjoQY8vpJAvwum+uwaXnkS2VMXBDnD03uTvA0rOAfxKy+GjjIv2YHg7KyxWlU3VMRz qmdzaxc9ECYPMGJPiDA+ZcXW3gPYU4FGXjVafolkfqPTeicJaoEq0U3j6Buw9LJZz887 BrauQTwSEHF2XcQLWJaNN0+TF4U6ot8JI/b5kIPddDJtXt+nhxGaf8O88ZwCqYqrjoJ7 lIF+RgsSR8CTFd5BvXuQFLD0GkMbWIWSPfBYvKXIqbblqwhrWwlmHaR1IHQeUHF+AldQ Z7aA== X-Gm-Message-State: APjAAAXuVPE6Ts0muD5LxeShZl7sE1jCj4Mht4lGlPryJKpgZ/jaG+om l9Cy/3oc3CkGDUejB11Z9kJU7cn1 X-Google-Smtp-Source: APXvYqy8rjxMImdfzEs2CVScAI9ELnfhUJIpgRe6FePW1BG2WZ+J/oHxqCZfGThlRys2Z/yQDr0gsA== X-Received: by 2002:adf:ffc5:: with SMTP id x5mr18800865wrs.92.1579795729271; Thu, 23 Jan 2020 08:08:49 -0800 (PST) Received: from sblaptop.fritz.box (ipbcc08bbf.dynamic.kabel-deutschland.de. [188.192.139.191]) by smtp.gmail.com with ESMTPSA id o187sm3678419wme.36.2020.01.23.08.08.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Jan 2020 08:08:48 -0800 (PST) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Thu, 23 Jan 2020 17:08:31 +0100 Message-Id: <20200123160832.2020-5-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200123160832.2020-1-andreas.rheinhardt@gmail.com> References: <20200123160832.2020-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 5/6] avformat/movenc: Check for reformatting errors when writing hint tracks 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" If this is not done, the avio_write() calls will cause segfaults immediately afterwards on error. Signed-off-by: Andreas Rheinhardt --- libavformat/movenc.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/libavformat/movenc.c b/libavformat/movenc.c index fb44ee2c71..b5e06de3d5 100644 --- a/libavformat/movenc.c +++ b/libavformat/movenc.c @@ -5461,8 +5461,10 @@ int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt) /* from x264 or from bytestream H.264 */ /* NAL reformatting needed */ if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams) { - ff_avc_parse_nal_units_buf(pkt->data, &reformatted_data, - &size); + ret = ff_avc_parse_nal_units_buf(pkt->data, &reformatted_data, + &size); + if (ret < 0) + return ret; avio_write(pb, reformatted_data, size); } else { if (trk->cenc.aes_ctr) { @@ -5479,14 +5481,19 @@ int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt) (AV_RB24(trk->vos_data) == 1 || AV_RB32(trk->vos_data) == 1)) { /* extradata is Annex B, assume the bitstream is too and convert it */ if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams) { - ff_hevc_annexb2mp4_buf(pkt->data, &reformatted_data, &size, 0, NULL); + ret = ff_hevc_annexb2mp4_buf(pkt->data, &reformatted_data, + &size, 0, NULL); + if (ret < 0) + return ret; avio_write(pb, reformatted_data, size); } else { size = ff_hevc_annexb2mp4(pb, pkt->data, pkt->size, 0, NULL); } } else if (par->codec_id == AV_CODEC_ID_AV1) { if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams) { - ff_av1_filter_obus_buf(pkt->data, &reformatted_data, &size); + ret = ff_av1_filter_obus_buf(pkt->data, &reformatted_data, &size); + if (ret < 0) + return ret; avio_write(pb, reformatted_data, size); } else { size = ff_av1_filter_obus(pb, pkt->data, pkt->size); From patchwork Thu Jan 23 16:08:32 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 17495 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 3F89744BAAD for ; Thu, 23 Jan 2020 18:08:59 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 2310268B49E; Thu, 23 Jan 2020 18:08:59 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f67.google.com (mail-wr1-f67.google.com [209.85.221.67]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id F1F2968AFD0 for ; Thu, 23 Jan 2020 18:08:50 +0200 (EET) Received: by mail-wr1-f67.google.com with SMTP id z3so3722222wru.3 for ; Thu, 23 Jan 2020 08:08:50 -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=NNOMAO2j92evoDimxXraroux7FNEw8Ryp4mz+eHXCLE=; b=RIur4a08ZctaJK6YbzQF+SzVkULKm0rJ1Yf9gtHN59jeTAl7S881SEw8RPXvCzNlxV 1Mk1411Cmi9syvWO4dPjtYd8VkkBGC69c8sVOle+3dgx5STHKKh80Yx627poEM+t6p9Z HwIrkkC+lyk6Tay9QjJgRJGHwiEKq7urAX99UV2KX3qMKCpBhcUQ/pUfBrEnKnivRATf DPnQHCV7eIvz3z5vUk0zoxBkVsbWlKAdHLX/Jrs7Bbz03JqizcQIy1LDJjzQ3VJRNVcv 4QSAnN/TVRa9tOP+P9HzLiPHD41f/7tTv6cDP21us6FgCb1/Fo7YLRrZkzRmLsMgcETc gnKg== 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=NNOMAO2j92evoDimxXraroux7FNEw8Ryp4mz+eHXCLE=; b=LNDw/oySNNmB65OvNfPhFDJq02NPp4ABVxo7voVvED59cxmh4CIMH/vTND2tr25igb KBcs3Hr+82Kbf0zsEFWM9qpIjCeMIdc5/5kW9MIgvarCJvnEtR/yMi0AsHsWe7M6p2uR s4BljUa02cwTPOEY1qM5hQyc4t1dbDGZ/O3y7AkBpenKolDkBF3cB36frRBwHEBouWbu P6FeAChX7YGmBmQ83L0/0lDDJb6IVIOczYqlMxalta8xvmzXmN9ATfskdHBsNsHdtisY Q9DyVayqgyBjZHcrPMJmEdQVeOSV5cVa9tIf1pauDtTmIJIP592KGElnTUuhLLYIkMZc D8dg== X-Gm-Message-State: APjAAAVHIFkbo59RHT3hFeRU7VFd2kUTSvNmNHxv4MWkNSdAonxZMkfA ciYYXRBVU6AWBN8ODHTeqySPjn7Z X-Google-Smtp-Source: APXvYqzQ7tLyWeg4jr+q1YLbQ+poTHdRLHztyoiS2+u0QCOn71KoAD4nkZKk9UqwQtpUIkqH8LvWUA== X-Received: by 2002:a5d:50cf:: with SMTP id f15mr17437452wrt.381.1579795730225; Thu, 23 Jan 2020 08:08:50 -0800 (PST) Received: from sblaptop.fritz.box (ipbcc08bbf.dynamic.kabel-deutschland.de. [188.192.139.191]) by smtp.gmail.com with ESMTPSA id o187sm3678419wme.36.2020.01.23.08.08.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Jan 2020 08:08:49 -0800 (PST) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Thu, 23 Jan 2020 17:08:32 +0100 Message-Id: <20200123160832.2020-6-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200123160832.2020-1-andreas.rheinhardt@gmail.com> References: <20200123160832.2020-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 6/6] avformat/av1: Improve filtering AV1 OBUs 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" Both ISOBMFF as well as Matroska require certain OBUs to be stripped before muxing them. There are two functions for this purpose; one writes directly into an AVIOContext, the other returns a freshly allocated buffer with the undesired units stripped away. The latter one actually relies on the former by means of a dynamic buffer. This has several drawbacks: The underlying buffer might have to be reallocated multiple times; the buffer will eventually be overallocated; the data will not be directly copied into the final buffer, but rather first in the write buffer (in chunks of 1024 byte) and then written in these chunks. Moreover, the API for dynamic buffers is defective wrt error checking and as a consequence, the earlier code would indicate a length of -AV_INPUT_BUFFER_PADDING_SIZE on allocation failure, but it would not return an error; there would also be no error in case the arbitrary limit of INT_MAX/2 that is currently imposed on dynamic buffers is hit. This commit changes this: The buffer is now parsed twice, once to get the precise length which will then be allocated; and once to actually write the data. For a 22.7mb/s file with average framesize 113 kB this improved the time for the calls to ff_av1_filter_obus_buf() when writing Matroska from 753662 decicycles to 313319 decicycles (based upon 50 runs a 2048 frames each); for another 1.5mb/s file (with average framesize of 7.3 kB) it improved from 79270 decicycles to 34539 decicycles (based upon 50 runs a 4096 frames). Signed-off-by: Andreas Rheinhardt --- libavformat/av1.c | 32 +++++++++++++++++++++----------- libavformat/av1.h | 4 ++-- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/libavformat/av1.c b/libavformat/av1.c index 4ff4bffddf..80c049f62f 100644 --- a/libavformat/av1.c +++ b/libavformat/av1.c @@ -19,6 +19,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +#include "libavutil/avassert.h" #include "libavutil/mem.h" #include "libavcodec/av1.h" #include "libavcodec/av1_parse.h" @@ -48,7 +49,8 @@ int ff_av1_filter_obus(AVIOContext *pb, const uint8_t *buf, int size) case AV1_OBU_PADDING: break; default: - avio_write(pb, buf, len); + if (pb) + avio_write(pb, buf, len); size += len; break; } @@ -58,23 +60,31 @@ int ff_av1_filter_obus(AVIOContext *pb, const uint8_t *buf, int size) return size; } -int ff_av1_filter_obus_buf(const uint8_t *buf, uint8_t **out, int *size) +int ff_av1_filter_obus_buf(const uint8_t *in, uint8_t **out, int *size) { - AVIOContext *pb; - int ret; + AVIOContext pb; + uint8_t *buf; + int len, ret; - ret = avio_open_dyn_buf(&pb); - if (ret < 0) - return ret; - - ret = ff_av1_filter_obus(pb, buf, *size); + len = ret = ff_av1_filter_obus(NULL, in, *size); if (ret < 0) { - ffio_free_dyn_buf(&pb); return ret; } + buf = av_malloc((size_t)len + AV_INPUT_BUFFER_PADDING_SIZE); + if (!buf) + return AVERROR(ENOMEM); + + ffio_init_context(&pb, buf, len, 1, NULL, NULL, NULL, NULL); + + ret = ff_av1_filter_obus(&pb, in, *size); + av_assert1(ret == len); + + memset(buf + len, 0, AV_INPUT_BUFFER_PADDING_SIZE); + av_freep(out); - *size = avio_close_dyn_buf(pb, out); + *out = buf; + *size = len; return 0; } diff --git a/libavformat/av1.h b/libavformat/av1.h index 0578435376..acba12612c 100644 --- a/libavformat/av1.h +++ b/libavformat/av1.h @@ -61,7 +61,7 @@ int ff_av1_filter_obus(AVIOContext *pb, const uint8_t *buf, int size); * * @param pb pointer to the AVIOContext where the filtered bitstream shall be * written - * @param buf input data buffer + * @param in input data buffer * @param out pointer to pointer that will hold the allocated data buffer * @param size size of the input data buffer. The size of the resulting output data buffer will be written here @@ -69,7 +69,7 @@ int ff_av1_filter_obus(AVIOContext *pb, const uint8_t *buf, int size); * @return 0 in case of success, a negative AVERROR code in case of failure. * On failure, out and size are unchanged */ -int ff_av1_filter_obus_buf(const uint8_t *buf, uint8_t **out, int *size); +int ff_av1_filter_obus_buf(const uint8_t *in, uint8_t **out, int *size); /** * Parses a Sequence Header from the the provided buffer. From patchwork Fri Jan 24 22:48:28 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 17521 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 98C6F449962 for ; Sat, 25 Jan 2020 00:48:51 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 4C96F68AEBC; Sat, 25 Jan 2020 00:48:51 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f68.google.com (mail-wm1-f68.google.com [209.85.128.68]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 61AFA68A101 for ; Sat, 25 Jan 2020 00:48:44 +0200 (EET) Received: by mail-wm1-f68.google.com with SMTP id q9so985830wmj.5 for ; Fri, 24 Jan 2020 14:48:44 -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=YchjTbmUZaVi0ULNqTgHZF2wR6w+RpluCcQyUJqKBqk=; b=CcLOEMPpQYG2f6aQcUGZ7SSPLT6ptaCUJxyidwa6Ikam3NNj5itrS58JVwZBi0/Zc9 B6HEPp5/vie/j7LllQ3Cvaij4jtMC47zCWhwTi5o6nj4CZVCVXQNsyXohHxaWh9EfVb3 P3Uj5tCu2URBihLOPOBnCYSXi/UqkDXBeTKo9fk9voP02eumTh8ioCsDFAcIAC7OhnNi dxfhsswpoJ5Q+LHs796ax9Om6jjZNQMiI0bJTBRKypLVuIq5oeaaqd/tuEYzZHVMs1uO R2j4gR4gHAj23O2mf8m6TQ+y3tGckY3NtkzvyTN7psOz2gQyqlOoCNn9Db3zW2lbbc0H SPWA== 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=YchjTbmUZaVi0ULNqTgHZF2wR6w+RpluCcQyUJqKBqk=; b=T6j65VMXI9F8zJJEDWVs4iWLQEgjeJ4fZtwPC0vgCtVVAXc0Cxlla84qbXh38f5r55 3VLamm9Hle0tk//G45YpuVVQka8YprhPR3FNVJtk2abFezT34I0wzERpp/iZlwQ7dZoR 5XTaYcQjNsEc/WK4JOlxtNxLIBd3wDJmE9iqU0EHNkL+zWkQw16wYu+hp5AqPyjTRoz3 8p33W1S5Byrti1frlpL6ZWl9W7f0Vc3HM7UE3BGk/0j/+TGwKuC2Co0bn9u8EhXFbD07 iTcYMX19cRBTrCkv531RbRtzBGIVV/z+SpzBZL5vv34rVNT+qfSw1n8iJ13UIv9sRYV6 VSHQ== X-Gm-Message-State: APjAAAWgYmVGgI68h8IFhV9oXuqaU1FzWz1QfpOG+ucxjG24NUN6dwgB LLSo9Y6rJDICxSGD0BEF06xur3x8 X-Google-Smtp-Source: APXvYqyN/3Q+/Fiv4pD340UA6GsjuWdwErTsL4go/SrEpMXVqbOnjW2ju/wiAJ2usePJanEFG/q58Q== X-Received: by 2002:a05:600c:2215:: with SMTP id z21mr1246374wml.55.1579906123615; Fri, 24 Jan 2020 14:48:43 -0800 (PST) Received: from sblaptop.fritz.box (ipbcc08bbf.dynamic.kabel-deutschland.de. [188.192.139.191]) by smtp.gmail.com with ESMTPSA id z25sm8977594wmf.14.2020.01.24.14.48.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Jan 2020 14:48:43 -0800 (PST) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Fri, 24 Jan 2020 23:48:28 +0100 Message-Id: <20200124224833.17579-1-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200123160832.2020-1-andreas.rheinhardt@gmail.com> References: <20200123160832.2020-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 07/12] avformat/av1, avc, hevc: Remove av_freep() 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" ff_av1_filter_obus_buf() and ff_avc_parse_nal_units_buf() both have a pointer-to-pointer parameter which they use to pass a newly allocated buffer to the caller. And both functions freed what this pointer points to before overwriting it. But no caller of these functions used this feature, but some had to initialize the pointer just because of this. So remove it and update the documentation of ff_av1_filter_obus_buf() wrt this fact. ff_hevc_annexb2mp4_buf in contrast did not free the pointer. This has been documented, too. Signed-off-by: Andreas Rheinhardt --- libavformat/av1.c | 1 - libavformat/av1.h | 1 + libavformat/avc.c | 3 +-- libavformat/hevc.h | 1 + 4 files changed, 3 insertions(+), 3 deletions(-) diff --git a/libavformat/av1.c b/libavformat/av1.c index 80c049f62f..07b399efcc 100644 --- a/libavformat/av1.c +++ b/libavformat/av1.c @@ -82,7 +82,6 @@ int ff_av1_filter_obus_buf(const uint8_t *in, uint8_t **out, int *size) memset(buf + len, 0, AV_INPUT_BUFFER_PADDING_SIZE); - av_freep(out); *out = buf; *size = len; diff --git a/libavformat/av1.h b/libavformat/av1.h index acba12612c..52d0814e86 100644 --- a/libavformat/av1.h +++ b/libavformat/av1.h @@ -68,6 +68,7 @@ int ff_av1_filter_obus(AVIOContext *pb, const uint8_t *buf, int size); * * @return 0 in case of success, a negative AVERROR code in case of failure. * On failure, out and size are unchanged + * @note *out will be treated as unintialized on input and will not be freed. */ int ff_av1_filter_obus_buf(const uint8_t *in, uint8_t **out, int *size); diff --git a/libavformat/avc.c b/libavformat/avc.c index e4e82e1864..cd15ac3cdb 100644 --- a/libavformat/avc.c +++ b/libavformat/avc.c @@ -101,7 +101,6 @@ int ff_avc_parse_nal_units_buf(const uint8_t *buf_in, uint8_t **buf, int *size) ff_avc_parse_nal_units(pb, buf_in, *size); - av_freep(buf); *size = avio_close_dyn_buf(pb, buf); return 0; } @@ -109,7 +108,7 @@ int ff_avc_parse_nal_units_buf(const uint8_t *buf_in, uint8_t **buf, int *size) int ff_isom_write_avcc(AVIOContext *pb, const uint8_t *data, int len) { AVIOContext *sps_pb = NULL, *pps_pb = NULL, *sps_ext_pb = NULL; - uint8_t *buf = NULL, *end, *start = NULL; + uint8_t *buf, *end, *start; uint8_t *sps, *pps, *sps_ext; uint32_t sps_size = 0, pps_size = 0, sps_ext_size = 0; int ret, nb_sps = 0, nb_pps = 0, nb_sps_ext = 0; diff --git a/libavformat/hevc.h b/libavformat/hevc.h index bb144397c0..0f56325c1c 100644 --- a/libavformat/hevc.h +++ b/libavformat/hevc.h @@ -73,6 +73,7 @@ int ff_hevc_annexb2mp4(AVIOContext *pb, const uint8_t *buf_in, * parameter set NAL units shall be written, may be NULL * @return 0 in case of success, a negative value corresponding to an AVERROR * code in case of failure + * @note *buf_out will be treated as uninitialized on input and won't be freed. */ int ff_hevc_annexb2mp4_buf(const uint8_t *buf_in, uint8_t **buf_out, int *size, int filter_ps, int *ps_count); From patchwork Fri Jan 24 22:48:29 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 17522 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 632F2449962 for ; Sat, 25 Jan 2020 00:48:55 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 5053468AF61; Sat, 25 Jan 2020 00:48:55 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f66.google.com (mail-wr1-f66.google.com [209.85.221.66]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 3AB9368AD7F for ; Sat, 25 Jan 2020 00:48:49 +0200 (EET) Received: by mail-wr1-f66.google.com with SMTP id z7so3897308wrl.13 for ; Fri, 24 Jan 2020 14:48:49 -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=1KmDrd6R3nZALx/RJ/gkkrNVe4/xKmWj6XMc4khPWRo=; b=RvnUrJpagGbeT6Lu2BVTEdvUyI4E0gqSdJwnOwluEW+mBP281L/vGwY/rJyt6RnlmM GUmNQl6sT8AFHfjGRmTUlYO3osU7d3y84JjCm5qv7Xu4ZfAvrHKLZ/D5XsUyBzpjjMVJ kC959t83onQ8zYuO7jo/af4dWTtVRIJYNfrqCtm99O0svo9ZDMeNrij8RrQs247VuL7Z wpdrkGw4wCjc+rXA9aNKdvO9KKcCmiWs2AzYfx1E9WHe+URsw3BQBEdfbVyBNtTEe8E7 /E0+28oW2wc89mSwt49VYU3gREWxWY8A+HXit8ugxH/AKOP95Wk71u2bMXxSZX2zW5bL z1Yg== 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=1KmDrd6R3nZALx/RJ/gkkrNVe4/xKmWj6XMc4khPWRo=; b=Y2dRSj4LO/GPRQ1PEy8PchaZWHEF4ym5wtd8cPhbe6Pxmc+OAzO1NG1m6/w4pfPi9+ R4KmucPOCuDK2wvSe9J/tKVBh25UCDJLr/L6BdLRi284CPZi3YrkOp60UNMO39uDelg2 WpOTaU1tw1E84S1FtzmqbpoQpxNJlEPt1aUSkvwkPKFOREgA3g0D07zgJ8xU6V61D5eM eTjJxHhGere8k0LkfUU7UMe1qF7nCCWeUZ/ztxGbPFNpU33K49ZJo1bM95b7/JmJ6n3u avViJdr6urajFjGj8cuWqabhTMZXqIuQmx9kmAL3ZcanDDc/dAu5edcQzQG7+hZIH1ps u0ZA== X-Gm-Message-State: APjAAAXpKUYubPkzhTapFOfjsS4WnGfzfUrlc/gLDQSFoHYZtS9O8ogv T03slch65Z2RSZ25IYtHNSmNfrCo X-Google-Smtp-Source: APXvYqxDh+2C5T2xHnpBSSn69OZ431m4EeL0TbJzFffk+i/23QbmrWwj4sjdLj7QXL3qio6mFo1uGw== X-Received: by 2002:adf:e683:: with SMTP id r3mr7176350wrm.38.1579906128469; Fri, 24 Jan 2020 14:48:48 -0800 (PST) Received: from sblaptop.fritz.box (ipbcc08bbf.dynamic.kabel-deutschland.de. [188.192.139.191]) by smtp.gmail.com with ESMTPSA id z25sm8977594wmf.14.2020.01.24.14.48.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Jan 2020 14:48:48 -0800 (PST) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Fri, 24 Jan 2020 23:48:29 +0100 Message-Id: <20200124224833.17579-2-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200123160832.2020-1-andreas.rheinhardt@gmail.com> References: <20200123160832.2020-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 08/12] avformat/avc, hevc: Check the allocations implicit in dynamic buffers 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" Dynamic buffers involve implicit allocations that are currently usually unchecked; ff_avc_parse_nal_units_buf() and ff_hevc_annexb2mp4buf() are no exceptions to this. So add checks for them. Signed-off-by: Andreas Rheinhardt --- One might argue that this should also check for whether the size returned by ff_hevc_annexb2mp4() resp. ff_avc_parse_nal_units() equals the size of the buffer (dynamic buffers have an implicit INT_MAX/2 allocation limit and if it is hit, no further writes are performed, but the already written data is not discarded); given that I prefer to drop this limit (and replace it by INT_MAX) later I have not added such a check here. (In case of an unchecked allocation failure in which the returned buffer is NULL, the returned size is -AV_INPUT_BUFFER_PADDING_SIZE. This is something that put_ebml_num() (the int is converted to uint64_t for it) doesn't like at all (it should assert, yet it actually runs into an infinite loop in ebml_num_size()).) libavformat/avc.c | 3 +++ libavformat/hevc.c | 2 ++ 2 files changed, 5 insertions(+) diff --git a/libavformat/avc.c b/libavformat/avc.c index cd15ac3cdb..aef5d3c35d 100644 --- a/libavformat/avc.c +++ b/libavformat/avc.c @@ -102,6 +102,9 @@ int ff_avc_parse_nal_units_buf(const uint8_t *buf_in, uint8_t **buf, int *size) ff_avc_parse_nal_units(pb, buf_in, *size); *size = avio_close_dyn_buf(pb, buf); + if (!*buf) + return AVERROR(ENOMEM); + return 0; } diff --git a/libavformat/hevc.c b/libavformat/hevc.c index f621cb2f19..a4e53bc4ab 100644 --- a/libavformat/hevc.c +++ b/libavformat/hevc.c @@ -1061,6 +1061,8 @@ int ff_hevc_annexb2mp4_buf(const uint8_t *buf_in, uint8_t **buf_out, } *size = avio_close_dyn_buf(pb, buf_out); + if (!*buf_out) + return AVERROR(ENOMEM); return 0; } From patchwork Fri Jan 24 22:48:30 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 17523 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 99DC9449962 for ; Sat, 25 Jan 2020 00:48:57 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 8682568B060; Sat, 25 Jan 2020 00:48:57 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f66.google.com (mail-wr1-f66.google.com [209.85.221.66]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id EE47668AECA for ; Sat, 25 Jan 2020 00:48:49 +0200 (EET) Received: by mail-wr1-f66.google.com with SMTP id c9so3909756wrw.8 for ; Fri, 24 Jan 2020 14:48:49 -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=KZEDvG0pzYpq/lSYNwgv0llmP7cYe4rusSZ4Iua5B6M=; b=UCqmYlEFBmb9BGrtKyg6eP3ipFCV93HRfeal+GGZ9o8hpT2UOJl05oDmBJDNN0BO0U yJCBrTHNsKdNk0/fHqJAp66u9KPplpqBftRWccP+31wkNajNzlkcb+8HfrH2b4is7bAX HeKrRc3TGJCNixosx8XzGGG/mrdcyEKayZbkU9eEuUidwirgCKAVKIbVMFyShgw0vV8v naMkYTYysxofifiEdyhahSIkkA6efhBWDwVYDu/FdFYy4Jm9sMHqZr1wR6c16nKE3RW5 rTnDzGfplM+aTHgKZk49Gs6f2xCfpRcHoNQBGBQKwP0htFyRugk3dEC3rZFaTHXayc50 OdPQ== 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=KZEDvG0pzYpq/lSYNwgv0llmP7cYe4rusSZ4Iua5B6M=; b=OoqlwJ1WI655Uh1R/LG8YH41PkNs/J8rlh0HL0aNAxjoQWNFU4eDZn1hAFPpVItS5V BwoPrlLvXy79cAd9el+yjcF8Vju+s1YuKdSvnWtndEKKlvBjMiHmlS7NnPByTT55xQnh Zu+ioQkpHA825r3WOgRmPjjNzvJnwL1MWznYjNMSxF4sKefSEGOM2bPwU1jR8W/8gPEZ 2zXmW4c9UlsW0x0n5fUP2GnTKIy6s3MAC3cGysQw2GKEy1K7qF2XfIvAgwasRuhL5mIu 8kcxc6o4tUZb9QsI0bSDXQrKe7VOYG/SAbL+XLWF1xidizSc0+7nlXGSMmxdMamNsTt7 JWbg== X-Gm-Message-State: APjAAAWr2jQzn+3i+rZiDt/9ilFarvxIs2+fIB1gH+KhTsEbbg/mHv3i HjFxERU5lNo17/9LNP3IYEujnp+h X-Google-Smtp-Source: APXvYqy6BrdObODgbv993tOuJ1htk+KL++4M6Q84IsCg5odLlvrCh5wzlD9WJKpRY7E0bys+oFnshg== X-Received: by 2002:a5d:6987:: with SMTP id g7mr6545001wru.422.1579906129291; Fri, 24 Jan 2020 14:48:49 -0800 (PST) Received: from sblaptop.fritz.box (ipbcc08bbf.dynamic.kabel-deutschland.de. [188.192.139.191]) by smtp.gmail.com with ESMTPSA id z25sm8977594wmf.14.2020.01.24.14.48.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Jan 2020 14:48:48 -0800 (PST) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Fri, 24 Jan 2020 23:48:30 +0100 Message-Id: <20200124224833.17579-3-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200123160832.2020-1-andreas.rheinhardt@gmail.com> References: <20200123160832.2020-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 09/12] avformat/hevc: Defer initializations in ff_isom_write_hvcc() 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" Saves initialization of an HEVCDecoderConfigurationRecord when the data is already in ISOBMFF-format or if it is plainly invalid. Signed-off-by: Andreas Rheinhardt --- libavformat/hevc.c | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/libavformat/hevc.c b/libavformat/hevc.c index a4e53bc4ab..616e9ed49a 100644 --- a/libavformat/hevc.c +++ b/libavformat/hevc.c @@ -1070,29 +1070,27 @@ int ff_hevc_annexb2mp4_buf(const uint8_t *buf_in, uint8_t **buf_out, int ff_isom_write_hvcc(AVIOContext *pb, const uint8_t *data, int size, int ps_array_completeness) { - int ret = 0; - uint8_t *buf, *end, *start = NULL; HEVCDecoderConfigurationRecord hvcc; - - hvcc_init(&hvcc); + uint8_t *buf, *end, *start; + int ret; if (size < 6) { /* We can't write a valid hvcC from the provided data */ - ret = AVERROR_INVALIDDATA; - goto end; + return AVERROR_INVALIDDATA; } else if (*data == 1) { /* Data is already hvcC-formatted */ avio_write(pb, data, size); - goto end; + return 0; } else if (!(AV_RB24(data) == 1 || AV_RB32(data) == 1)) { /* Not a valid Annex B start code prefix */ - ret = AVERROR_INVALIDDATA; - goto end; + return AVERROR_INVALIDDATA; } ret = ff_avc_parse_nal_units_buf(data, &start, &size); if (ret < 0) - goto end; + return ret; + + hvcc_init(&hvcc); buf = start; end = start + size; From patchwork Fri Jan 24 22:48:31 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 17524 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 63B69449962 for ; Sat, 25 Jan 2020 00:48:58 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 4B51D68B07D; Sat, 25 Jan 2020 00:48:58 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f67.google.com (mail-wr1-f67.google.com [209.85.221.67]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id DE41A68AFBC for ; Sat, 25 Jan 2020 00:48:50 +0200 (EET) Received: by mail-wr1-f67.google.com with SMTP id c9so3909785wrw.8 for ; Fri, 24 Jan 2020 14:48:50 -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=r8BoYBpc/fg/HIJ4gTtOJXNpZzNXZlSnea0gtt2Wek4=; b=YYjGXwnKxLezV3pwVAc2eXfp3VvV9pRBxxfRAZx+nD19y+yayCCY4cCqaqq+bLRZSc Gi2AAga9mYTGb0CnfKTNOHlGKzWP+auF32TgRKMyiVRjpCTARe0qZa1tEMwJwr5tDbOw /WZGEfqEYlzHyzogw0utLHnFPRE+EC77y76FqG0zb1VIZqbPa3ny6hfSMWtzWutF0Ysy 5m6f/G5Y5E6ooWDmdHskfG43nslkZofeDkeOLOE6bL88aWDbILcc6NHzpfVkFtfJKN4I WOxB7L3vRFXonO7VQp7o0OswuchwaJg6+PIPu3PspOjQaagmC37S22INmEgz1TPfxIet f++g== 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=r8BoYBpc/fg/HIJ4gTtOJXNpZzNXZlSnea0gtt2Wek4=; b=TecsQPqQTMbWy6yXxAz+ydxxj4U3wSVWPnvT+Ep+oMuFYgz1vm71OzljLi4AwEsK0H 26BBMBVaSlQYswcAvhY0LpYSnTEFSZakqTfjSl+CK51y4Es5lR/haHBs02i/42uon9h5 M6moB3D2T7bPX5K+znvD5v9JQ8WDyKConVIiZdSErYzeZvsAw4+d/qYYtydHc+l9v3Sz ck9vtGmhfc7NgJxhiB4Jbqv7agVn5aVSb+cH5eQaqSn36bfdh1qVSGas/41n9ZJyB7qO ItTyRXNbeOk6k9FNw7rGQJUPZmEsccCoyGtwnzNtDLKlJ0VJn301bN9EyY1OnW9tvHSu 9MtA== X-Gm-Message-State: APjAAAVvdRrfSqgu2t4XTBAy7F0BJHmJ4gtnFVz/k5La0MrVLgmsf3ug wfR1zYM44P8AWmMeSr9IVOAacQqq X-Google-Smtp-Source: APXvYqxTEUFJf8bcz3D695JgD9Z3XcIXAA0qMH0K4yy1TvOu5qEUbdHuqp1pEnzzxHN9QM9EhXlsQA== X-Received: by 2002:adf:f98c:: with SMTP id f12mr6313606wrr.138.1579906130191; Fri, 24 Jan 2020 14:48:50 -0800 (PST) Received: from sblaptop.fritz.box (ipbcc08bbf.dynamic.kabel-deutschland.de. [188.192.139.191]) by smtp.gmail.com with ESMTPSA id z25sm8977594wmf.14.2020.01.24.14.48.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Jan 2020 14:48:49 -0800 (PST) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Fri, 24 Jan 2020 23:48:31 +0100 Message-Id: <20200124224833.17579-4-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200123160832.2020-1-andreas.rheinhardt@gmail.com> References: <20200123160832.2020-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 10/12] avformat/av1: Fix nits in the documentation of ff_av1_filter_obus_buf() 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" Signed-off-by: Andreas Rheinhardt --- libavformat/av1.h | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/libavformat/av1.h b/libavformat/av1.h index 52d0814e86..6cc3fcaad2 100644 --- a/libavformat/av1.h +++ b/libavformat/av1.h @@ -59,15 +59,13 @@ int ff_av1_filter_obus(AVIOContext *pb, const uint8_t *buf, int size); * Filter out AV1 OBUs not meant to be present in ISOBMFF sample data and write * the resulting bitstream to a newly allocated data buffer. * - * @param pb pointer to the AVIOContext where the filtered bitstream shall be - * written * @param in input data buffer * @param out pointer to pointer that will hold the allocated data buffer * @param size size of the input data buffer. The size of the resulting output - data buffer will be written here + * data buffer will be written here * * @return 0 in case of success, a negative AVERROR code in case of failure. - * On failure, out and size are unchanged + * On failure, *out and *size are unchanged * @note *out will be treated as unintialized on input and will not be freed. */ int ff_av1_filter_obus_buf(const uint8_t *in, uint8_t **out, int *size); From patchwork Fri Jan 24 22:48:32 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 17525 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 6C3B5449962 for ; Sat, 25 Jan 2020 00:48:59 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 57FD068AF17; Sat, 25 Jan 2020 00:48:59 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f67.google.com (mail-wr1-f67.google.com [209.85.221.67]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 48C0768AFD7 for ; Sat, 25 Jan 2020 00:48:52 +0200 (EET) Received: by mail-wr1-f67.google.com with SMTP id q6so3893287wro.9 for ; Fri, 24 Jan 2020 14:48:52 -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=2A8KKkro2JC1Djp528xnb3VkPSfJxWnjODbQKXsAXO4=; b=Qsh6FlSorG8y8kytbgiUsaCsfuWNH/6YJwzTZgUf7LwLigQm9qM8lACOB9DeJNJ6+G fPMrUY5bCW/a2JYBsSS8GBMbpDP+2ifXtp+w2odcKeMtd1SXPbdbNFk2U/SKHUA5l7xL sg07yO/bSUEg6oBuQeQqJnb6piMTxE7n4tt+0GJ+OS0GZsav5Wdmt5tdPbff/mgCp3iv LvhuglTMtp8Fv5xzWLsyuDrYAkCyCPr4Pztm6TW++xiI0O2FY1wc63GStp5cWjgf0lRW xxzUQpzv9VAtee1G7RgVJ7v8tH7AbQ9hAnjha+Ldvg+YQFUK9wnIh//ah1MDyQmE9iX+ qKZg== 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=2A8KKkro2JC1Djp528xnb3VkPSfJxWnjODbQKXsAXO4=; b=eH6ZFhqlcmXrzPyJ6Tsy/dAhQJPwSf5u/DbBanwvzA1u8DdkkdD2O4q1ZZb5biax4n jnuQ+QhM3oHmAfJNZjb8QYk4bfdQNHZol7rJ1sxrv6JDDGwyq4NgCffIY4Z5OXbsKuDT QWJBNSx/kDiDAhCmSypM+NpJ74alQt+W5M39uBE/8m7UCHEB0wY/AAtx5I9M5ga0Iy+3 IUaiu5K+L4oset8oWslWiRayC7Z0b9nFUd2ZidzetsLxdbDdtjM5ZclDEZgsZVLDCc6V tdAxuEDfPcLDKJFZerBar4GWWg78oVJzY3l7nlKFvaRhV85jxeF1/g1RzNN9o3hzhwt5 x9Ag== X-Gm-Message-State: APjAAAXOMgCDScGd2eORjzF5gFtRCo6uuaUtKWZC3ZewbprHR32GbuAQ iPqb/im1T0aDiQkM8UUy/ZBiFP2j X-Google-Smtp-Source: APXvYqwteOU0GpG4JEwITVb5X8WNv1l9FZrnUumA8apoLb++55KD9VdwVBdWwYdow9see1VGlmVANA== X-Received: by 2002:a5d:4983:: with SMTP id r3mr6365506wrq.134.1579906131221; Fri, 24 Jan 2020 14:48:51 -0800 (PST) Received: from sblaptop.fritz.box (ipbcc08bbf.dynamic.kabel-deutschland.de. [188.192.139.191]) by smtp.gmail.com with ESMTPSA id z25sm8977594wmf.14.2020.01.24.14.48.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Jan 2020 14:48:50 -0800 (PST) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Fri, 24 Jan 2020 23:48:32 +0100 Message-Id: <20200124224833.17579-5-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200123160832.2020-1-andreas.rheinhardt@gmail.com> References: <20200123160832.2020-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 11/12] avformat/hevc: Clarify documentation of filter_ps 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" and use it to avoid unnecessary initializations and frees in ff_hevc_annexb2mp4() and ff_hevc_annexb2mp4_buf(): Even if the caller does not want to strip HEVC parameter sets away when converting from Annex B to mp4, a pointer has been initialized to NULL and later freed while still being NULL despite having not been used at all. The reason for this is that there is still a jump to the end of the function in order to set a variable (returned via a pointer argument) containing the number of parameter set NAL units stripped away to zero. But if the caller does not want to strip these parameter sets away, it is pointless to set this variable. Document this behaviour and remove the jump. Also document that said number won't be set on error (in case of ff_hevc_annexb2mp4_buf(), this was actually the behaviour anyway) and remove the jump to the end on error, too; this also allows to avoid initializing the pointer mentioned above. Signed-off-by: Andreas Rheinhardt --- libavformat/hevc.c | 8 +++----- libavformat/hevc.h | 14 ++++++++------ 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/libavformat/hevc.c b/libavformat/hevc.c index 616e9ed49a..b07897d9a5 100644 --- a/libavformat/hevc.c +++ b/libavformat/hevc.c @@ -1000,16 +1000,15 @@ int ff_hevc_annexb2mp4(AVIOContext *pb, const uint8_t *buf_in, int size, int filter_ps, int *ps_count) { int num_ps = 0, ret = 0; - uint8_t *buf, *end, *start = NULL; + uint8_t *buf, *end, *start; if (!filter_ps) { - ret = ff_avc_parse_nal_units(pb, buf_in, size); - goto end; + return ff_avc_parse_nal_units(pb, buf_in, size); } ret = ff_avc_parse_nal_units_buf(buf_in, &start, &size); if (ret < 0) - goto end; + return ret; ret = 0; buf = start; @@ -1037,7 +1036,6 @@ int ff_hevc_annexb2mp4(AVIOContext *pb, const uint8_t *buf_in, buf += len; } -end: av_free(start); if (ps_count) *ps_count = num_ps; diff --git a/libavformat/hevc.h b/libavformat/hevc.h index 0f56325c1c..3ff4614022 100644 --- a/libavformat/hevc.h +++ b/libavformat/hevc.h @@ -42,9 +42,10 @@ * @param buf_in address of the buffer holding the input data * @param size size (in bytes) of the input buffer * @param filter_ps whether to write parameter set NAL units to the output (0) - * or to discard them (non-zero) - * @param ps_count address of the variable where the number of discarded - * parameter set NAL units shall be written, may be NULL + * or to count but discard them (non-zero) + * @param ps_count if filter_ps is nonzero, address of the variable where the + * number of discarded parameter set NAL units shall be written + * on success; otherwise ignored; may be NULL * @return the amount (in bytes) of data written in case of success, a negative * value corresponding to an AVERROR code in case of failure */ @@ -68,9 +69,10 @@ int ff_hevc_annexb2mp4(AVIOContext *pb, const uint8_t *buf_in, * @param buf_out on success, address of the variable holding the address of * the output buffer * @param filter_ps whether to write parameter set NAL units to the output (0) - * or to discard them (non-zero) - * @param ps_count address of the variable where the number of discarded - * parameter set NAL units shall be written, may be NULL + * or to count but discard them (non-zero) + * @param ps_count if filter_ps is nonzero, address of the variable where the + * number of discarded parameter set NAL units shall be written + * on success; otherwise ignored; may be NULL * @return 0 in case of success, a negative value corresponding to an AVERROR * code in case of failure * @note *buf_out will be treated as uninitialized on input and won't be freed. From patchwork Fri Jan 24 22:48:33 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 17526 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 7391A449962 for ; Sat, 25 Jan 2020 00:49:00 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 5EE3668B0BE; Sat, 25 Jan 2020 00:49:00 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f66.google.com (mail-wr1-f66.google.com [209.85.221.66]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id D1F2268B06D for ; Sat, 25 Jan 2020 00:48:52 +0200 (EET) Received: by mail-wr1-f66.google.com with SMTP id q6so3893307wro.9 for ; Fri, 24 Jan 2020 14:48:52 -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=sczkhvkESylyMVdYwbfdAK+2vxqhZV8kVx7OuXQggzs=; b=XACh7w4O77P1yDyd8B3ewsXMRmjqR7k5zFGQfcLHKtRtHlaW2QJ4wleLWeTBr0BUZb euxyyX1JImgxjemtyn9EG3bF6RhURVK+I5AnpqOLn+iVrgbivOo2pHmR1C3y6aaJ17Y+ OADAwuL7qWzpigoHJB0TcnO8ezRcbY97JEKEvQvO1CyP3l51RIg5mZS1xw9IdKY+ovvb CP/S9J2/QyHfQAU386SidoB/nQk6h6i3BYgq2F1U6pUWfLb7teuZv82MQAgdhVlJaqeW 8vyK5O0Al6MiKEmvJMopRWi5wpjlGXPJ40Srq3KZESMbUNVkmN6+dOx+UECdg1QzPiEi 4VxQ== 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=sczkhvkESylyMVdYwbfdAK+2vxqhZV8kVx7OuXQggzs=; b=kId9J+fBnHrlRoA7CwXxV0nZkbGpuvc1CPovSMG7kYpOsW/CQ12M8+r5UBlCzhtLYG g9VIr0fq03N9bJvgRYorVHFpNPLOMp2VgeEehwwTqVcjAZ3olX7fj2jvtgDaLDHh7b/C n1sbePIZIpBpfDmkd0LWythX2mexPvjxdHMj6QaWwpkdC05uhnl261hUog6BAqi5DAR6 cAjnIRVXBgTV+k4QUDybilfHmk2p4YSPP8juZi4xwogXRXJXZsoVfZUJ9P5bDR6mR0iG MUTGn+6rz2wOenPy9pD8wTVmztS8xZpTqDkDbOlkFDK8PWIwk/8E6lqZBfz/hAhSxx9q /GRw== X-Gm-Message-State: APjAAAUguJNLTtNwi7lZ99n+xagX/Il7a0mAR63xl9fPaG2o+RRoDh2t SxZwz/1awdq4Vj/PTMlDXBdmTo/G X-Google-Smtp-Source: APXvYqxf4SV1dju213ZMxoUfoZMmESKXhWZNAJJHQn4XLw4BAtvMgFoneYTRy9hxRhzKX0Viqjdq1Q== X-Received: by 2002:adf:814c:: with SMTP id 70mr7344604wrm.157.1579906132067; Fri, 24 Jan 2020 14:48:52 -0800 (PST) Received: from sblaptop.fritz.box (ipbcc08bbf.dynamic.kabel-deutschland.de. [188.192.139.191]) by smtp.gmail.com with ESMTPSA id z25sm8977594wmf.14.2020.01.24.14.48.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Jan 2020 14:48:51 -0800 (PST) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Fri, 24 Jan 2020 23:48:33 +0100 Message-Id: <20200124224833.17579-6-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200123160832.2020-1-andreas.rheinhardt@gmail.com> References: <20200123160832.2020-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 12/12] avformat/av1: Avoid allocation + copying when filtering OBUs 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" Certain types of OBUs are stripped away before muxing into Matroska and ISOBMFF; there are two functions to do this: One that outputs by directly writing in an AVIOContext and one that returns a freshly allocated buffer with the units not stripped away copied into it. The latter option is bad for performance, especially when the input does already not contain any of the units intended to be stripped away (this covers typical remuxing scenarios). Therefore this commit changes this by avoiding allocating and copying when possible; it is possible if the OBUs to be retained are consecutively in the input buffer (without an OBU to be discarded between them). In this case, the caller receives the offset as well as the length of the part of the buffer that contains the units to be kept. This also avoids copying when e.g. the only unit to be discarded is a temporal delimiter at the front. For a 22.7mb/s file with average framesize 113 kB this improved the time for the calls to ff_av1_filter_obus_buf() when writing Matroska from 313319 decicycles to 2368 decicycles; for another file with 1.5mb/s (average framesize 7.3 kB) it improved from 34539 decicycles to 1922 decicyles. For these files the only units that needed to be stripped away were temporal unit delimiters at the front. Signed-off-by: Andreas Rheinhardt --- If it is desired, I can add a commit to switch ff_mov_write_packet() to not use a pointer just for reformatted_data (that is of course initialized to NULL), but to replace it by a data buffer that gets initialized to pkt->data and modified so that data + offset always points to the current data. (This is possible now that the av_freep() have been removed from the reformatting functions.) libavformat/av1.c | 50 ++++++++++++++++++++++++++++++++------- libavformat/av1.h | 13 ++++++---- libavformat/matroskaenc.c | 2 +- libavformat/movenc.c | 11 +++++---- 4 files changed, 58 insertions(+), 18 deletions(-) diff --git a/libavformat/av1.c b/libavformat/av1.c index 07b399efcc..fef8e96f8d 100644 --- a/libavformat/av1.c +++ b/libavformat/av1.c @@ -29,13 +29,20 @@ #include "avio.h" #include "avio_internal.h" -int ff_av1_filter_obus(AVIOContext *pb, const uint8_t *buf, int size) +static int av1_filter_obus(AVIOContext *pb, const uint8_t *buf, + int size, int *offset) { - const uint8_t *end = buf + size; + const uint8_t *start = buf, *end = buf + size; int64_t obu_size; - int start_pos, type, temporal_id, spatial_id; - - size = 0; + int off, start_pos, type, temporal_id, spatial_id; + enum { + START_NOT_FOUND, + START_FOUND, + END_FOUND, + OFFSET_IMPOSSIBLE, + } state = START_NOT_FOUND; + + off = size = 0; while (buf < end) { int len = parse_obu_header(buf, end - buf, &obu_size, &start_pos, &type, &temporal_id, &spatial_id); @@ -47,8 +54,16 @@ int ff_av1_filter_obus(AVIOContext *pb, const uint8_t *buf, int size) case AV1_OBU_REDUNDANT_FRAME_HEADER: case AV1_OBU_TILE_LIST: case AV1_OBU_PADDING: + if (state == START_FOUND) + state = END_FOUND; break; default: + if (state == START_NOT_FOUND) { + off = buf - start; + state = START_FOUND; + } else if (state == END_FOUND) { + state = OFFSET_IMPOSSIBLE; + } if (pb) avio_write(pb, buf, len); size += len; @@ -57,19 +72,35 @@ int ff_av1_filter_obus(AVIOContext *pb, const uint8_t *buf, int size) buf += len; } + if (offset) + *offset = state != OFFSET_IMPOSSIBLE ? off : -1; + return size; } -int ff_av1_filter_obus_buf(const uint8_t *in, uint8_t **out, int *size) +int ff_av1_filter_obus(AVIOContext *pb, const uint8_t *buf, int size) +{ + return av1_filter_obus(pb, buf, size, NULL); +} + +int ff_av1_filter_obus_buf(const uint8_t *in, uint8_t **out, + int *size, int *offset) { AVIOContext pb; uint8_t *buf; - int len, ret; + int len, off, ret; - len = ret = ff_av1_filter_obus(NULL, in, *size); + len = ret = av1_filter_obus(NULL, in, *size, &off); if (ret < 0) { return ret; } + if (off >= 0) { + *out = (uint8_t *)in; + *size = len; + *offset = off; + + return 0; + } buf = av_malloc((size_t)len + AV_INPUT_BUFFER_PADDING_SIZE); if (!buf) @@ -77,13 +108,14 @@ int ff_av1_filter_obus_buf(const uint8_t *in, uint8_t **out, int *size) ffio_init_context(&pb, buf, len, 1, NULL, NULL, NULL, NULL); - ret = ff_av1_filter_obus(&pb, in, *size); + ret = av1_filter_obus(&pb, in, *size, NULL); av_assert1(ret == len); memset(buf + len, 0, AV_INPUT_BUFFER_PADDING_SIZE); *out = buf; *size = len; + *offset = 0; return 0; } diff --git a/libavformat/av1.h b/libavformat/av1.h index 6cc3fcaad2..dd5b47dc25 100644 --- a/libavformat/av1.h +++ b/libavformat/av1.h @@ -56,19 +56,24 @@ typedef struct AV1SequenceParameters { int ff_av1_filter_obus(AVIOContext *pb, const uint8_t *buf, int size); /** - * Filter out AV1 OBUs not meant to be present in ISOBMFF sample data and write - * the resulting bitstream to a newly allocated data buffer. + * Filter out AV1 OBUs not meant to be present in ISOBMFF sample data and return + * the result in a data buffer, avoiding allocations and copies if possible. * * @param in input data buffer - * @param out pointer to pointer that will hold the allocated data buffer + * @param out pointer to pointer for the returned buffer. In case of success, + * it is independently allocated if and only if `*out` differs from in. * @param size size of the input data buffer. The size of the resulting output * data buffer will be written here + * @param offset offset of the returned data inside `*out`: It runs from + * `*out + offset` (inclusive) to `*out + offset + size` + * (exclusive); is zero if `*out` is independently allocated. * * @return 0 in case of success, a negative AVERROR code in case of failure. * On failure, *out and *size are unchanged * @note *out will be treated as unintialized on input and will not be freed. */ -int ff_av1_filter_obus_buf(const uint8_t *in, uint8_t **out, int *size); +int ff_av1_filter_obus_buf(const uint8_t *in, uint8_t **out, + int *size, int *offset); /** * Parses a Sequence Header from the the provided buffer. diff --git a/libavformat/matroskaenc.c b/libavformat/matroskaenc.c index 20bad95262..618f07c769 100644 --- a/libavformat/matroskaenc.c +++ b/libavformat/matroskaenc.c @@ -2112,7 +2112,7 @@ static int mkv_write_block(AVFormatContext *s, AVIOContext *pb, /* extradata is Annex B, assume the bitstream is too and convert it */ err = ff_hevc_annexb2mp4_buf(pkt->data, &data, &size, 0, NULL); } else if (par->codec_id == AV_CODEC_ID_AV1) { - err = ff_av1_filter_obus_buf(pkt->data, &data, &size); + err = ff_av1_filter_obus_buf(pkt->data, &data, &size, &offset); } else if (par->codec_id == AV_CODEC_ID_WAVPACK) { err = mkv_strip_wavpack(pkt->data, &data, &size); } else diff --git a/libavformat/movenc.c b/libavformat/movenc.c index b5e06de3d5..f715f911f3 100644 --- a/libavformat/movenc.c +++ b/libavformat/movenc.c @@ -5374,7 +5374,7 @@ int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt) AVCodecParameters *par = trk->par; AVProducerReferenceTime *prft; unsigned int samples_in_chunk = 0; - int size = pkt->size, ret = 0; + int size = pkt->size, ret = 0, offset = 0; int prft_size; uint8_t *reformatted_data = NULL; @@ -5491,7 +5491,8 @@ int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt) } } else if (par->codec_id == AV_CODEC_ID_AV1) { if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams) { - ret = ff_av1_filter_obus_buf(pkt->data, &reformatted_data, &size); + ret = ff_av1_filter_obus_buf(pkt->data, &reformatted_data, + &size, &offset); if (ret < 0) return ret; avio_write(pb, reformatted_data, size); @@ -5667,12 +5668,14 @@ int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt) if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams) ff_mov_add_hinted_packet(s, pkt, trk->hint_track, trk->entry, - reformatted_data, size); + reformatted_data ? reformatted_data + offset + : NULL, size); end: err: - av_free(reformatted_data); + if (pkt->data != reformatted_data) + av_free(reformatted_data); return ret; }