From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTP id 355DC49467 for ; Tue, 12 Mar 2024 15:17:43 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 2FC9168D0AA; Tue, 12 Mar 2024 17:17:41 +0200 (EET) Received: from mail-pj1-f41.google.com (mail-pj1-f41.google.com [209.85.216.41]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 197FC68C10F for ; Tue, 12 Mar 2024 17:17:35 +0200 (EET) Received: by mail-pj1-f41.google.com with SMTP id 98e67ed59e1d1-29bf9bd1907so1701231a91.1 for ; Tue, 12 Mar 2024 08:17:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1710256652; x=1710861452; darn=ffmpeg.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:to:subject:user-agent:mime-version:date:message-id:from :to:cc:subject:date:message-id:reply-to; bh=nSg2e/ZlOyfwqJ7TIz+2+ocEPUhnFuWQ+trTgGiYv1Q=; b=XFibuWnjP3Gx7qPugly5bv3ivlN1mN5JkwkwGaMfdJ6LFRaWngBuhsvTFR5oqNjYAO FAKQh+LV3BlPowZkMXhVL1h91cBTkgHh90zd8a8EzsFGa6/1HjbKAY6Ds6uOKvy8Tk5Y P+niqd3koqbFYx3vesaZMiqFTzbHLu7vAVk5ACCH1WpRHQF1mivSDrREDMPWujNKNWls RGKBg7o74SJlSLMvsBhtj37/rjFH9Ak402PhOJf1bFL7E6m7TeImkzfEKOqCS50oR0Ik PyVSGFjYtDYnwGPJEIhgxDsZ1KJ5lkKYSmYxvasuE7y5NZrp74gF3H0RqanaQOab/Lus poXw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710256652; x=1710861452; h=content-transfer-encoding:in-reply-to:from:content-language :references:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=nSg2e/ZlOyfwqJ7TIz+2+ocEPUhnFuWQ+trTgGiYv1Q=; b=O+8B+P8iIL+ZHB/Wx1W2/evC600idMMbWxS4WU8LNjQ1s6RaPanmRwdAOgGzJTHDxj lAk6MZtqCBSFn5UutihQGjmj0jp/doIYsFwpn3Uc6EXW6GPKGci4buoNIxTi/d/krLqN nQsgOo7PsZ0cNgYHUCMoMeVPyTi/QLD51l5nh6eYA0QOU8OYieOwe5Se8SaVi732DRQ+ Dqv8aq5cQjZtRo6YhQ6ErYgPsibxs5FOTB4LmQpC/dPkND/1oHGuOym2rpfui+OugK7s D2fZa80SKACVrjphAVAMsYEnEhznN2HVdg9qVXakt9hbgCISKP3RmtoNJh2olbbmfrqa Ww3A== X-Gm-Message-State: AOJu0YyrOgVms00qPNT8p7/DVJKBo9pjzk1iIQry9FuPqx3GXbkQ/Y30 Vlxtgt94PZZbFDLhN+ARez8FqMRavSdrhETVjpcGh9o5LfJTCVNSiQy3Ahnz X-Google-Smtp-Source: AGHT+IEduo4weIHXGk5CvDqecJNVjtmotioDQyRkI9+lLgXsW0S/exJD47v2Xrc3VNqOk3HcgylxAQ== X-Received: by 2002:a17:90b:378a:b0:29c:5708:b922 with SMTP id mz10-20020a17090b378a00b0029c5708b922mr563741pjb.26.1710256651914; Tue, 12 Mar 2024 08:17:31 -0700 (PDT) Received: from [192.168.0.10] ([190.194.169.124]) by smtp.gmail.com with ESMTPSA id w35-20020a17090a6ba600b0029bc319f7c9sm6637906pjj.39.2024.03.12.08.17.30 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 12 Mar 2024 08:17:31 -0700 (PDT) Message-ID: <85515dba-47a0-4974-a95e-debd9be97bc1@gmail.com> Date: Tue, 12 Mar 2024 12:17:34 -0300 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird To: ffmpeg-devel@ffmpeg.org References: Content-Language: en-US From: James Almer In-Reply-To: Subject: Re: [FFmpeg-devel] [PATCH v4] avcodec/libx264: fix extradata when config annexb=0 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 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="us-ascii"; Format="flowed" Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: On 3/12/2024 12:00 PM, Andreas Rheinhardt wrote: > Zhao Zhili: >> From: Zhao Zhili >> >> --- >> v4: Fix missing SEI in set_avcc_extradata >> v3: Remove unnecessary inclusion >> >> configure | 2 +- >> libavcodec/libx264.c | 153 ++++++++++++++++++++++++++++++++++++------- >> 2 files changed, 130 insertions(+), 25 deletions(-) >> >> diff --git a/configure b/configure >> index db7dc89755..24cb897d28 100755 >> --- a/configure >> +++ b/configure >> @@ -3491,7 +3491,7 @@ libwebp_encoder_deps="libwebp" >> libwebp_anim_encoder_deps="libwebp" >> libx262_encoder_deps="libx262" >> libx264_encoder_deps="libx264" >> -libx264_encoder_select="atsc_a53" >> +libx264_encoder_select="atsc_a53 h264parse" >> libx264rgb_encoder_deps="libx264" >> libx264rgb_encoder_select="libx264_encoder" >> libx265_encoder_deps="libx265" >> diff --git a/libavcodec/libx264.c b/libavcodec/libx264.c >> index 10d646bd76..e7d16997d2 100644 >> --- a/libavcodec/libx264.c >> +++ b/libavcodec/libx264.c >> @@ -34,6 +34,7 @@ >> #include "avcodec.h" >> #include "codec_internal.h" >> #include "encode.h" >> +#include "h264_ps.h" >> #include "internal.h" >> #include "packet_internal.h" >> #include "atsc_a53.h" >> @@ -865,6 +866,131 @@ static int convert_pix_fmt(enum AVPixelFormat pix_fmt) >> return 0; >> } >> >> +static int save_sei(AVCodecContext *avctx, x264_nal_t *nal) >> +{ >> + X264Context *x4 = avctx->priv_data; >> + >> + av_log(avctx, AV_LOG_INFO, "%s\n", nal->p_payload + 25); >> + x4->sei_size = nal->i_payload; >> + x4->sei = av_malloc(x4->sei_size); >> + if (!x4->sei) >> + return AVERROR(ENOMEM); >> + >> + memcpy(x4->sei, nal->p_payload, nal->i_payload); >> + >> + return 0; >> +} >> + >> +static int set_avcc_extradata(AVCodecContext *avctx, x264_nal_t *nal, int nnal) >> +{ >> + X264Context *x4 = avctx->priv_data; >> + x264_nal_t *sps_nal = NULL; >> + x264_nal_t *pps_nal = NULL; >> + uint8_t *p, *sps; >> + int ret; >> + >> + /* We know it's in the order of SPS/PPS/SEI, but it's not documented in x264 API. >> + * The x264 param i_sps_id implies there is a single pair of SPS/PPS. >> + */ >> + for (int i = 0; i < nnal; i++) { >> + switch (nal[i].i_type) { >> + case NAL_SPS: >> + sps_nal = &nal[i]; >> + break; >> + case NAL_PPS: >> + pps_nal = &nal[i]; >> + break; >> + case NAL_SEI: >> + ret = save_sei(avctx, &nal[i]); >> + if (ret < 0) >> + return ret; >> + break; >> + } >> + } >> + if (!sps_nal || !pps_nal) >> + return AVERROR_EXTERNAL; >> + >> + avctx->extradata_size = sps_nal->i_payload + pps_nal->i_payload + 7; >> + avctx->extradata = av_mallocz(avctx->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE); >> + if (!avctx->extradata) >> + return AVERROR(ENOMEM); >> + >> + // Now create AVCDecoderConfigurationRecord >> + p = avctx->extradata; >> + // Skip size part >> + sps = sps_nal->p_payload + 4; >> + *p++ = 1; // version >> + *p++ = sps[1]; // AVCProfileIndication >> + *p++ = sps[2]; // profile_compatibility >> + *p++ = sps[3]; // AVCLevelIndication >> + *p++ = 0xFF; >> + *p++ = 0xE0 | 0x01; // 3 bits reserved (111) + 5 bits number of sps >> + memcpy(p, sps_nal->p_payload + 2, sps_nal->i_payload - 2); >> + // Make sps has AV_INPUT_BUFFER_PADDING_SIZE padding, so it can be used >> + // with GetBitContext >> + sps = p + 2; >> + p += sps_nal->i_payload - 2; >> + *p++ = 1; >> + memcpy(p, pps_nal->p_payload + 2, pps_nal->i_payload - 2); >> + p += pps_nal->i_payload - 2; >> + >> + if (sps[3] != 66 && sps[3] != 77 && sps[3] != 88) { >> + GetBitContext gbc; >> + H264ParamSets ps = { 0 }; >> + >> + init_get_bits8(&gbc, sps, sps_nal->i_payload - 4); >> + skip_bits(&gbc, 8); >> + ret = ff_h264_decode_seq_parameter_set(&gbc, avctx, &ps, 1); > > ff_h264_decode_seq_parameter_set() expects to read from a GetBitContext > whose buffer has already been stripped of 0x03 escape bytes. Your buffer > hasn't and therefore it is possible for this function to return an error > even when the input is fine. Why would a buffer created by passing annexb=0 have the escape bytes? Is it not specific to that encapsulation, to prevent parsing image data as a start code? > >> + if (ret < 0) >> + return ret; >> + >> + ps.sps = ps.sps_list[x4->params.i_sps_id]; >> + *p++ = 0xFC | ps.sps->chroma_format_idc; >> + *p++ = 0xF8 | (ps.sps->bit_depth_luma - 8); >> + *p++ = 0xF8 | (ps.sps->bit_depth_chroma - 8); >> + *p++ = 0; >> + ff_h264_ps_uninit(&ps); >> + } >> + av_assert0(avctx->extradata + avctx->extradata_size >= p); >> + avctx->extradata_size = p - avctx->extradata; >> + >> + return 0; >> +} >> + >> +static int set_extradata(AVCodecContext *avctx) >> +{ >> + X264Context *x4 = avctx->priv_data; >> + x264_nal_t *nal; >> + uint8_t *p; >> + int nnal, s; >> + >> + s = x264_encoder_headers(x4->enc, &nal, &nnal); >> + if (s < 0) >> + return AVERROR_EXTERNAL; >> + >> + if (!x4->params.b_annexb) >> + return set_avcc_extradata(avctx, nal, nnal); >> + >> + avctx->extradata = p = av_mallocz(s + AV_INPUT_BUFFER_PADDING_SIZE); >> + if (!p) >> + return AVERROR(ENOMEM); >> + >> + for (int i = 0; i < nnal; i++) { >> + /* Don't put the SEI in extradata. */ >> + if (nal[i].i_type == NAL_SEI) { >> + s = save_sei(avctx, &nal[i]); >> + if (s < 0) >> + return s; >> + continue; >> + } >> + memcpy(p, nal[i].p_payload, nal[i].i_payload); >> + p += nal[i].i_payload; >> + } >> + avctx->extradata_size = p - avctx->extradata; >> + >> + return 0; >> +} >> + >> #define PARSE_X264_OPT(name, var)\ >> if (x4->var && x264_param_parse(&x4->params, name, x4->var) < 0) {\ >> av_log(avctx, AV_LOG_ERROR, "Error parsing option '%s' with value '%s'.\n", name, x4->var);\ >> @@ -1233,30 +1359,9 @@ FF_ENABLE_DEPRECATION_WARNINGS >> return AVERROR_EXTERNAL; >> >> if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) { >> - x264_nal_t *nal; >> - uint8_t *p; >> - int nnal, s, i; >> - >> - s = x264_encoder_headers(x4->enc, &nal, &nnal); >> - avctx->extradata = p = av_mallocz(s + AV_INPUT_BUFFER_PADDING_SIZE); >> - if (!p) >> - return AVERROR(ENOMEM); >> - >> - for (i = 0; i < nnal; i++) { >> - /* Don't put the SEI in extradata. */ >> - if (nal[i].i_type == NAL_SEI) { >> - av_log(avctx, AV_LOG_INFO, "%s\n", nal[i].p_payload+25); >> - x4->sei_size = nal[i].i_payload; >> - x4->sei = av_malloc(x4->sei_size); >> - if (!x4->sei) >> - return AVERROR(ENOMEM); >> - memcpy(x4->sei, nal[i].p_payload, nal[i].i_payload); >> - continue; >> - } >> - memcpy(p, nal[i].p_payload, nal[i].i_payload); >> - p += nal[i].i_payload; >> - } >> - avctx->extradata_size = p - avctx->extradata; >> + ret = set_extradata(avctx); >> + if (ret < 0) >> + return ret; >> } >> >> cpb_props = ff_encode_add_cpb_side_data(avctx); > > _______________________________________________ > ffmpeg-devel mailing list > ffmpeg-devel@ffmpeg.org > https://ffmpeg.org/mailman/listinfo/ffmpeg-devel > > To unsubscribe, visit link above, or email > ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe". _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".