From patchwork Mon Aug 23 13:16:52 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 29736 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6602:2a4a:0:0:0:0 with SMTP id k10csp2072316iov; Mon, 23 Aug 2021 06:18:04 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwzudhB6s9s6+ohSo7aJBbA28wo3GXAt8OU44B1E8YXSUuPAM2Ns6UYjmlCZVlYrW7A+Oby X-Received: by 2002:aa7:d319:: with SMTP id p25mr37670385edq.197.1629724684287; Mon, 23 Aug 2021 06:18:04 -0700 (PDT) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id hr6si16144912ejc.711.2021.08.23.06.18.03; Mon, 23 Aug 2021 06:18:04 -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; dkim=neutral (body hash did not verify) header.i=@outlook.com header.s=selector1 header.b=aKsmlx1U; arc=fail (body hash mismatch); 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; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=outlook.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 8920268A581; Mon, 23 Aug 2021 16:17:25 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from EUR04-HE1-obe.outbound.protection.outlook.com (unknown [40.92.73.63]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 82E6568A4FE for ; Mon, 23 Aug 2021 16:17:18 +0300 (EEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=gkilZYCk8xt4hOKpVdvBCJ34QEWnplC86jiDiAUi8xSXiFzyNRILOKmL1Zvo5SFwyAGFcuFoBO0Hr/hSPn9ZEjEzo8/LZ1vj3bdMdG+DD3JaqClgFajNwqY5xMcha/PdPpq1urjEkBdRbnCCQQW14OWJfguwrInfGx61GnTkhovN5r2gKjBbk4lIL/x7JVA0oXq/u0IadXyw/wgUT44F3kLq5gsgeTBcXCuSLMrJHOAPq4Qbd83gOJlewBxcWaPIYJj/X55m+tFXXXFlns/0rL+CRIi60XJcNZm6NQjryb9rqsGV80F7BSVtDegqgEHB1z/sEm3ghJYZCM/L+092mQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=RCsrEr2ro/PfrtlyzM/lFnC4lp9eG++lmUN4zB/aYt0=; b=fn+EZueg0V/XYEt0LDRC7TxUZFs7aqvry5Pg1S/l0YIBS1hC8jGAzNrw/q3oFuGYJ2EYOOVZm7dnXWkmeOsPzq21F2ZhsL4anx+hdFPLzK1s1yb4zaXOMzNMRPwFWYVLzFWeuvVh/NGIVLZaX9yD1BVQn2/sKXYrVBqeX/Gktoj9h3Yg+yxT/Yz8Ojdlcz91xHPg99i+Lpw59tBWieCpxyq6kf3x/E5Npc/MG53d3DLjPna7lw/zhLHYNH09ofH7IapYUbFL6Uf8Bb9P/rjFzFY1o9ErFbFSw1XQ2s5AiSyHaUP05H32mLQJGRoBSpPIFM4tLUNcK3sJ1Uht2k0ITw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=RCsrEr2ro/PfrtlyzM/lFnC4lp9eG++lmUN4zB/aYt0=; b=aKsmlx1U5qH9gVgvNAro8skmSjVclVx17tDbL0rWrUP57WZGHKq8ldfE5okQxmNixHypyJDCaTrKqll8XW2QKhHL1bSh5dor5lv3NH1X3H3S4bNhALEwZoAVWx+sVdXdMR6t3tiWvDCz6J4Gj72rzwR54S3hIUiTehKpZq4ey7rQqdq447/xwKDaKLKSeLc1H/V0YKkZt5oEzsuGgw+OKJhMCz2Q+Icg7g2qv0yCeiciKIPFfTRMYBOUdvdB7prwHpwkXv7tzYLr7KMAYDToNDB9siP/rx1JfqIgufHZhh4HvGVdD99meKvOxP7L1+KoopfIvpG8C0MLRJAENCKo7g== Received: from AM7PR03MB6660.eurprd03.prod.outlook.com (2603:10a6:20b:1c1::22) by AM6PR03MB5250.eurprd03.prod.outlook.com (2603:10a6:20b:c6::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4436.22; Mon, 23 Aug 2021 13:17:09 +0000 Received: from AM7PR03MB6660.eurprd03.prod.outlook.com ([fe80::5574:1fd:cce2:d590]) by AM7PR03MB6660.eurprd03.prod.outlook.com ([fe80::5574:1fd:cce2:d590%5]) with mapi id 15.20.4436.024; Mon, 23 Aug 2021 13:17:09 +0000 From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Mon, 23 Aug 2021 15:16:52 +0200 Message-ID: X-Mailer: git-send-email 2.30.2 In-Reply-To: References: X-TMN: [xhFDsUU4VxPeYpgo91UrIaclMXL35e2d] X-ClientProxiedBy: AM4PR0202CA0012.eurprd02.prod.outlook.com (2603:10a6:200:89::22) To AM7PR03MB6660.eurprd03.prod.outlook.com (2603:10a6:20b:1c1::22) X-Microsoft-Original-Message-ID: <20210823131654.1837818-5-andreas.rheinhardt@outlook.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sblaptop.fritz.box (188.192.142.38) by AM4PR0202CA0012.eurprd02.prod.outlook.com (2603:10a6:200:89::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4436.19 via Frontend Transport; Mon, 23 Aug 2021 13:17:07 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: b0d9963d-e6d3-4f06-2829-08d966384f1c X-MS-Exchange-SLBlob-MailProps: gjx25WM8ZNV5xbDn+Qw+qkBWqS1ZoOmTGRzj25nc2cBs3kbzb1OZED0QBu1sNg6130Xv7nbQizCViViK6L2dQcjtFw6uslyCYLsZ3pyoKyqXUo1joNtmGyka5RCMGa9B1HuWrA6KErZ7rYMGR6FOy+QRmXGiv6J+XkDvgDruFvyQbdy+X/rdeOWuQWbvFz/ynvh1o0yCl1pAI7aOA6xuqvx3679JN/SkOjLM9le6CL1LwzsF6/R76vDS8LN2X//Vp6an6ROkaU5pcXkRX7QCn1qVWBjBgxBH7k1O0EwzgcLHg1jVBKg3bw36hyGTfVsMs4/qURMVaRoAaYRpatprH5CZeuuHHQus1TTeijXH7h65p6JNtuo/T1560k4RYnZG68k+5yoEMIDplSJdriGfkCq7+SLQsub7gIVb4o7eEs7F8T3bMsODgIof9edgYpaZYc1tSum9kytCo35WtoKAMf+5KOb9uDJgmAh/E/E84UBXz05IJ/iTnWZ9bpcqI4iiPkMcpv4qPxMe9gvQ10cVg1a+7AaRZVDlsacjayWWC4qu2rRF8faBAoqxA58QFyo2M7PFrlz/07+GJDEFbB+adc2jQNE+L+nXeQ7KlyObCUsZN8n1oqkPr3JC0XUoqKXX4ES3faTaSkYH9454mUhOU+Y0Ydt3HLKczkowRJmcBN0Zafk/ovXOubneWOhVDeImHja/lRpz+SNW4X9PvHzAObYxqFMtTeer0PUPCFkPrV4= X-MS-TrafficTypeDiagnostic: AM6PR03MB5250: X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: GDcBmJR/tXlT1OR9p4EjXAVbhclmISTbvZT29O2mvaIuo+SYHxBtGRllKyPulxBFQBXHCXSNtwa+I7wGG+oUrjfVjpl/nrKcCdDX7qMBLsZK508KYtUhsxQXmOYPA4s7Bqi6bdht2G0qZOduul3W8pR5qLkoXhAnzlU5b1Qb2s1nq/kd3HiUNn3gxT+EGr22OkzlEUHyrtcfGA1Po7BbwoiZFIiPrAJoEKfEc38KuRH1XV/nCG4Ov7SiwB/AqzfZnaQRMCmVRSJn5QJ1qCtGK8YNV+bcph6ZPh+jGKDldXVopcseDzn7NqgAl3TBxOqpCHs0R/PyXkacOXwGgKDNP985Zf/TqRXmfBo2+F/lRotL/KdNER22ozkIqurQXnCxt+FFkhsvb+Vpa0AmbYOAYA4pKmAEF+eM8mqV03WAKkAmR9unWbWYU92+iA0QhjJI X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 0/WhHhoBLboaqcMniZ0Zapv+zmZahZxOeAPqWjmLPWsYJRfDDruLGh9+oengfRH231M4TbMVPUyNxy9avT82gBK8Zo1fX43Bpnpv+ttev9Ih90WysWYy1BXOEB9WRZwTu/vIwgB6/EL/QhK/HPSS8g== X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: b0d9963d-e6d3-4f06-2829-08d966384f1c X-MS-Exchange-CrossTenant-AuthSource: AM7PR03MB6660.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 23 Aug 2021 13:17:08.3761 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM6PR03MB5250 Subject: [FFmpeg-devel] [PATCH 6/8] avformat/flac_picture: Try to reuse buffer for attached picture 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: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: lMbObj3qBHFR Signed-off-by: Andreas Rheinhardt --- libavformat/flac_picture.c | 15 +++++++++++++-- libavformat/flac_picture.h | 14 +++++++++++++- libavformat/flacdec.c | 2 +- libavformat/oggparsevorbis.c | 4 ++-- 4 files changed, 29 insertions(+), 6 deletions(-) diff --git a/libavformat/flac_picture.c b/libavformat/flac_picture.c index 96e14f76c9..e7660a7db1 100644 --- a/libavformat/flac_picture.c +++ b/libavformat/flac_picture.c @@ -29,12 +29,13 @@ #define MAX_TRUNC_PICTURE_SIZE (500 * 1024 * 1024) -int ff_flac_parse_picture(AVFormatContext *s, uint8_t *buf, int buf_size, int truncate_workaround) +int ff_flac_parse_picture(AVFormatContext *s, uint8_t **bufp, int buf_size, + int truncate_workaround) { const CodecMime *mime = ff_id3v2_mime_tags; enum AVCodecID id = AV_CODEC_ID_NONE; AVBufferRef *data = NULL; - uint8_t mimetype[64], *desc = NULL; + uint8_t mimetype[64], *desc = NULL, *buf = *bufp; GetByteContext g; AVStream *st; int width, height, ret = 0; @@ -142,6 +143,15 @@ int ff_flac_parse_picture(AVFormatContext *s, uint8_t *buf, int buf_size, int tr goto fail; } } + if (trunclen == 0 && len >= buf_size - (buf_size >> 4)) { + data = av_buffer_create(buf, buf_size + AV_INPUT_BUFFER_PADDING_SIZE, + av_buffer_default_free, NULL, 0); + if (!data) + RETURN_ERROR(AVERROR(ENOMEM)); + *bufp = NULL; + data->data += bytestream2_tell(&g); + data->size = len + AV_INPUT_BUFFER_PADDING_SIZE; + } else { if (!(data = av_buffer_alloc(len + AV_INPUT_BUFFER_PADDING_SIZE))) { RETURN_ERROR(AVERROR(ENOMEM)); } @@ -155,6 +165,7 @@ int ff_flac_parse_picture(AVFormatContext *s, uint8_t *buf, int buf_size, int tr if (avio_read(s->pb, data->data + len - trunclen, trunclen) < trunclen) RETURN_ERROR(AVERROR_INVALIDDATA); } + } memset(data->data + len, 0, AV_INPUT_BUFFER_PADDING_SIZE); if (AV_RB64(data->data) == PNGSIG) diff --git a/libavformat/flac_picture.h b/libavformat/flac_picture.h index 61fd0c8806..db074e531d 100644 --- a/libavformat/flac_picture.h +++ b/libavformat/flac_picture.h @@ -26,6 +26,18 @@ #define RETURN_ERROR(code) do { ret = (code); goto fail; } while (0) -int ff_flac_parse_picture(AVFormatContext *s, uint8_t *buf, int buf_size, int truncate_workaround); +/** + * Parse a FLAC METADATA_BLOCK_PICTURE + * + * @param s AVFormatContext for logging and the attached picture stream. + * @param buf `*buf` points to the actual data which must be padded by + * AV_INPUT_BUFFER_PADDING_SIZE bytes not counted in buf_size. + * This function may take ownership of `*buf` and reset it. + * @param buf_size size of `*buf` (excluding padding) + * @param truncate_workaround If set, additional data may be read from s->pb if + * truncation has been detected. + */ +int ff_flac_parse_picture(AVFormatContext *s, uint8_t **buf, int buf_size, + int truncate_workaround); #endif /* AVFORMAT_FLAC_PICTURE_H */ diff --git a/libavformat/flacdec.c b/libavformat/flacdec.c index bfa464c508..2060b48c69 100644 --- a/libavformat/flacdec.c +++ b/libavformat/flacdec.c @@ -145,7 +145,7 @@ static int flac_read_header(AVFormatContext *s) } av_freep(&buffer); } else if (metadata_type == FLAC_METADATA_TYPE_PICTURE) { - ret = ff_flac_parse_picture(s, buffer, metadata_size, 1); + ret = ff_flac_parse_picture(s, &buffer, metadata_size, 1); av_freep(&buffer); if (ret < 0) { av_log(s, AV_LOG_ERROR, "Error parsing attached picture.\n"); diff --git a/libavformat/oggparsevorbis.c b/libavformat/oggparsevorbis.c index 463fb61672..74d387f437 100644 --- a/libavformat/oggparsevorbis.c +++ b/libavformat/oggparsevorbis.c @@ -117,7 +117,7 @@ static int vorbis_parse_single_comment(AVFormatContext *as, AVDictionary **m, * recommended way of embedding cover art within VorbisComments." */ if (!av_strcasecmp(t, "METADATA_BLOCK_PICTURE") && parse_picture) { - int ret, len = AV_BASE64_DECODE_SIZE(vl); + int ret, len = AV_BASE64_DECODE_SIZE(vl) + AV_INPUT_BUFFER_PADDING_SIZE; uint8_t *pict = av_malloc(len); if (!pict) { @@ -126,7 +126,7 @@ static int vorbis_parse_single_comment(AVFormatContext *as, AVDictionary **m, } ret = av_base64_decode(pict, v, len); if (ret > 0) - ret = ff_flac_parse_picture(as, pict, ret, 0); + ret = ff_flac_parse_picture(as, &pict, ret, 0); av_freep(&pict); if (ret < 0) { av_log(as, AV_LOG_WARNING, "Failed to parse cover art block.\n");