From patchwork Wed Mar 20 02:19:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Niedermayer X-Patchwork-Id: 47259 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:8ba5:b0:1a3:31a3:7958 with SMTP id m37csp122778pzh; Tue, 19 Mar 2024 19:19:58 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCXGUrqCv5QKIfhd6dH+dsoBIZnr/xbbLzEdxOQw/c07Ws3h3WXi1NdM7C2NvMH3re7oY+RnQ2KBOcp0vnmERvk1R1LnA1QqOCZ35g== X-Google-Smtp-Source: AGHT+IFGYSEyRbg11gKFiYnrmEy2vwDMsuHbBAByI872IcLOtC5jDbSZqgILpslrbKSYBHHKi8Hh X-Received: by 2002:a17:906:ef03:b0:a46:c01d:b595 with SMTP id f3-20020a170906ef0300b00a46c01db595mr5906671ejs.53.1710901198346; Tue, 19 Mar 2024 19:19:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1710901198; cv=none; d=google.com; s=arc-20160816; b=KSfH+JZRDikl7/dnrPalNa6zpoZarSaUtXnmtkGWM89O37kUVTr6f4XjLZ4jaN8TTc JWi6M7P2TAV1axMHI7757wAWfmRC0CC+J49YT5ckxuWw70O69gYikIakmgdJFyHV2YCF +fQdc7GkhcAY48GBkRxCfMST5A8pwdJZIuwyu2fn0hbMqKeEqMvzxV7TfV4tOD+kH3rX N92RSl7MI/WdgQUd9Gsbtmf4UdQlM4gAXdvczYaIN/I47nAIUM6rAwmSS7IbC1LatJr4 24lIBt0wny+/nHQHweF40yoRRzj4C8UAZMdBpEbd4SrZ9bms2m1Kwqq1wX/xmYFPY1Zm 0AsA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:references:in-reply-to:message-id:date :to:from:dkim-signature:delivered-to; bh=ztUUIUx4GRhSEYuMqzDagOSPkq/7kCVpJ278n/1xfMQ=; fh=e5zN9xSzcxLA6bGo3lF+CqTbY/oLwzApV03EO/RBfgQ=; b=VYOwXVRzd2Kj3fY/9c6fVRu5gesHp7BsMhyR7R5It7ZZnD1qyXiSw5ZqwIZczWEXJY IVk7aCRNpaTptbM1rdpVt0EP8INQYGThlDAmSh4RoiMRLo4kaInZFr/+G3X2uVOgzu0F 6kmuRzbTvO5UYLQwBXNkd9OOLQEuTucyy4Cc3nS3uAOHeE6EHeWQBmv4vXehmHwTX45m nVHwP4omzaJVd9lSZuFnwvHXiaNTss5xjrlzvs6hgZIz3DsoMpReJaxxAPu2zseXpx78 WV3H9cWFdU+7vYZByxlmu7BRRgh9tm7oY0rwBGCtFwBCJVtFyJSXj5sNUF0EEQpRV57J scCg==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@niedermayer.cc header.s=gm1 header.b=HKNFgydw; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id e19-20020a170906249300b00a4674a2bad3si5782318ejb.195.2024.03.19.19.19.57; Tue, 19 Mar 2024 19:19:58 -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=@niedermayer.cc header.s=gm1 header.b=HKNFgydw; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id CA95368D43A; Wed, 20 Mar 2024 04:19:37 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from relay9-d.mail.gandi.net (relay9-d.mail.gandi.net [217.70.183.199]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 090E768D4B4 for ; Wed, 20 Mar 2024 04:19:29 +0200 (EET) Received: by mail.gandi.net (Postfix) with ESMTPSA id 2B963FF802 for ; Wed, 20 Mar 2024 02:19:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=niedermayer.cc; s=gm1; t=1710901169; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:in-reply-to:in-reply-to:references:references; bh=L8IDizKTZVoSqgFc6Rop/cH/WZ7ei0bS1RkSme4lfE4=; b=HKNFgydwIuW4ETyr8wr1QJlc+emVkawlXgXUIjcG2KJK7vIAABJyL8qKQtNP/uswyegRyl 2bNvwE+yHnF095GiOHjpU578I0GwK6GfCi7pi+AZuoLr3gN/35DyKx9fSgIXGNgMwctXNf /20FMxQWCt3JI4WY3cBa/q9doua/aBfJITLzE1PttL/yxucnlOiRf8SzGXprnnP9ebItlV SAP0ByxMtjtyuZFzDAgaGYafCk9QzG5jMa+6+wklIE/gLfEBi+zpFLg1LJhogSaIsPe6mw qu+d8GW3c6uOYb+WyP9T+OKAsWs1hbzpN0gyMJm2L7HWGn9lsRSoiQmiUUxxrg== From: Michael Niedermayer To: FFmpeg development discussions and patches Date: Wed, 20 Mar 2024 03:19:26 +0100 Message-Id: <20240320021926.3759-3-michael@niedermayer.cc> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20240320021926.3759-1-michael@niedermayer.cc> References: <20240320021926.3759-1-michael@niedermayer.cc> X-GND-Sasl: michael@niedermayer.cc Subject: [FFmpeg-devel] [PATCH 3/3] Revert "avcodec/h264_mp4toannexb_bsf: fix missing PS before IDR frames" 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 MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: KrmGbm2wU9fp This reverts commit d3aa0cd16f5e952bc346b7c74b4dcba95151a63a. Fixes: out of array write Fixes: 64407/clusterfuzz-testcase-minimized-ffmpeg_BSF_H264_MP4TOANNEXB_fuzzer-4966763443650560 The bsf code performs 2 iterations, the first counts how much space is needed than allocates and the 2nd pass copies into teh allocated space The reverted code reallocates sps/pps in the first pass in a data dependant way that leaves the 2nd pass in a different state then the first Found-by: continuous fuzzing process https://github.com/google/oss-fuzz/tree/master/projects/ffmpeg Signed-off-by: Michael Niedermayer --- libavcodec/bsf/h264_mp4toannexb.c | 83 +++---------------------------- tests/fate/h264.mak | 5 -- 2 files changed, 6 insertions(+), 82 deletions(-) diff --git a/libavcodec/bsf/h264_mp4toannexb.c b/libavcodec/bsf/h264_mp4toannexb.c index 120241c892..b99de39ce9 100644 --- a/libavcodec/bsf/h264_mp4toannexb.c +++ b/libavcodec/bsf/h264_mp4toannexb.c @@ -36,8 +36,6 @@ typedef struct H264BSFContext { uint8_t *pps; int sps_size; int pps_size; - unsigned sps_buf_size; - unsigned pps_buf_size; uint8_t length_size; uint8_t new_idr; uint8_t idr_sps_seen; @@ -133,33 +131,16 @@ pps: memset(out + total_size, 0, padding); if (pps_offset) { - uint8_t *sps; - + s->sps = out; s->sps_size = pps_offset; - sps = av_fast_realloc(s->sps, &s->sps_buf_size, s->sps_size); - if (!sps) { - av_free(out); - return AVERROR(ENOMEM); - } - s->sps = sps; - memcpy(s->sps, out, s->sps_size); } else { av_log(ctx, AV_LOG_WARNING, "Warning: SPS NALU missing or invalid. " "The resulting stream may not play.\n"); } if (pps_offset < total_size) { - uint8_t *pps; - + s->pps = out + pps_offset; s->pps_size = total_size - pps_offset; - pps = av_fast_realloc(s->pps, &s->pps_buf_size, s->pps_size); - if (!pps) { - av_freep(&s->sps); - av_free(out); - return AVERROR(ENOMEM); - } - s->pps = pps; - memcpy(s->pps, out + pps_offset, s->pps_size); } else { av_log(ctx, AV_LOG_WARNING, "Warning: PPS NALU missing or invalid. " @@ -179,35 +160,6 @@ pps: return 0; } -static int h264_mp4toannexb_save_ps(uint8_t **dst, int *dst_size, - unsigned *dst_buf_size, - const uint8_t *nal, uint32_t nal_size, - int first) -{ - static const uint8_t nalu_header[4] = { 0, 0, 0, 1 }; - const int start_code_size = sizeof(nalu_header); - uint8_t *ptr; - uint32_t size; - - if (first) - size = 0; - else - size = *dst_size; - - ptr = av_fast_realloc(*dst, dst_buf_size, size + nal_size + start_code_size); - if (!ptr) - return AVERROR(ENOMEM); - - memcpy(ptr + size, nalu_header, start_code_size); - size += start_code_size; - memcpy(ptr + size, nal, nal_size); - size += nal_size; - - *dst = ptr; - *dst_size = size; - return 0; -} - static int h264_mp4toannexb_init(AVBSFContext *ctx) { int extra_size = ctx->par_in->extradata_size; @@ -268,9 +220,6 @@ static int h264_mp4toannexb_filter(AVBSFContext *ctx, AVPacket *opkt) if (j) \ av_log(__VA_ARGS__) for (int j = 0; j < 2; j++) { - int sps_count = 0; - int pps_count = 0; - buf = in->data; new_idr = s->new_idr; sps_seen = s->idr_sps_seen; @@ -301,18 +250,8 @@ static int h264_mp4toannexb_filter(AVBSFContext *ctx, AVPacket *opkt) if (unit_type == H264_NAL_SPS) { sps_seen = new_idr = 1; - if (!j) { - h264_mp4toannexb_save_ps(&s->sps, &s->sps_size, &s->sps_buf_size, - buf, nal_size, !sps_count); - sps_count++; - } } else if (unit_type == H264_NAL_PPS) { pps_seen = new_idr = 1; - if (!j) { - h264_mp4toannexb_save_ps(&s->pps, &s->pps_size, &s->pps_buf_size, - buf, nal_size, !pps_count); - pps_count++; - } /* if SPS has not been seen yet, prepend the AVCC one to PPS */ if (!sps_seen) { if (!s->sps_size) { @@ -332,10 +271,9 @@ static int h264_mp4toannexb_filter(AVBSFContext *ctx, AVPacket *opkt) /* prepend only to the first type 5 NAL unit of an IDR picture, if no sps/pps are already present */ if (new_idr && unit_type == H264_NAL_IDR_SLICE && !sps_seen && !pps_seen) { - if (s->sps_size) - count_or_copy(&out, &out_size, s->sps, s->sps_size, PS_OUT_OF_BAND, j); - if (s->pps_size) - count_or_copy(&out, &out_size, s->pps, s->pps_size, PS_OUT_OF_BAND, j); + if (ctx->par_out->extradata) + count_or_copy(&out, &out_size, ctx->par_out->extradata, + ctx->par_out->extradata_size, PS_OUT_OF_BAND, j); new_idr = 0; /* if only SPS has been seen, also insert PPS */ } else if (new_idr && unit_type == H264_NAL_IDR_SLICE && sps_seen && !pps_seen) { @@ -351,7 +289,7 @@ static int h264_mp4toannexb_filter(AVBSFContext *ctx, AVPacket *opkt) else ps = PS_NONE; count_or_copy(&out, &out_size, buf, nal_size, ps, j); - if (unit_type == H264_NAL_SLICE) { + if (!new_idr && unit_type == H264_NAL_SLICE) { new_idr = 1; sps_seen = 0; pps_seen = 0; @@ -391,14 +329,6 @@ fail: return ret; } -static void h264_mp4toannexb_close(AVBSFContext *ctx) -{ - H264BSFContext *s = ctx->priv_data; - - av_freep(&s->sps); - av_freep(&s->pps); -} - static void h264_mp4toannexb_flush(AVBSFContext *ctx) { H264BSFContext *s = ctx->priv_data; @@ -418,6 +348,5 @@ const FFBitStreamFilter ff_h264_mp4toannexb_bsf = { .priv_data_size = sizeof(H264BSFContext), .init = h264_mp4toannexb_init, .filter = h264_mp4toannexb_filter, - .close = h264_mp4toannexb_close, .flush = h264_mp4toannexb_flush, }; diff --git a/tests/fate/h264.mak b/tests/fate/h264.mak index 674054560b..d0c57eabe9 100644 --- a/tests/fate/h264.mak +++ b/tests/fate/h264.mak @@ -227,7 +227,6 @@ FATE_H264-$(call FRAMECRC, MOV, H264) += fate-h264-twofields-packet FATE_H264-$(call DEMMUX, MOV, H264, H264_MP4TOANNEXB_BSF SCALE_FILTER) += fate-h264-bsf-mp4toannexb-new-extradata FATE_H264-$(call DEMMUX, MOV, H264, H264_MP4TOANNEXB_BSF) += fate-h264-bsf-mp4toannexb \ - fate-h264-bsf-mp4toannexb-2 \ fate-h264_mp4toannexb_ticket5927 \ fate-h264_mp4toannexb_ticket5927_2 \ @@ -432,10 +431,6 @@ fate-h264-conformance-sva_nl1_b: CMD = framecrc -i $(TARGET_SAM fate-h264-conformance-sva_nl2_e: CMD = framecrc -i $(TARGET_SAMPLES)/h264-conformance/SVA_NL2_E.264 fate-h264-bsf-mp4toannexb: CMD = md5 -i $(TARGET_SAMPLES)/h264/interlaced_crop.mp4 -c:v copy -f h264 -# First IDR is prefixed by SPS/PPS -fate-h264-bsf-mp4toannexb-2: CMD = md5 -i $(TARGET_SAMPLES)/h264/ps_prefix_first_idr.mp4 -c:v copy -f h264 -fate-h264-bsf-mp4toannexb-2: CMP = oneline -fate-h264-bsf-mp4toannexb-2: REF = cffcfa6a2d0b58c9de1f5785f099f41d fate-h264-bsf-mp4toannexb-new-extradata: CMD = stream_remux mov $(TARGET_SAMPLES)/h264/extradata-reload-multi-stsd.mov "" h264 "-map 0:v" fate-h264_mp4toannexb_ticket5927: CMD = transcode "mp4" $(TARGET_SAMPLES)/h264/thezerotheorem-cut.mp4 \ h264 "-c:v copy -bsf:v h264_mp4toannexb -an" "-c:v copy"