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 F40CC49EE2 for ; Wed, 13 Mar 2024 13:37:43 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 25C7A68C094; Wed, 13 Mar 2024 15:37:40 +0200 (EET) Received: from out203-205-251-82.mail.qq.com (out203-205-251-82.mail.qq.com [203.205.251.82]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id DE9BE68C094 for ; Wed, 13 Mar 2024 15:37:31 +0200 (EET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foxmail.com; s=s201512; t=1710337042; bh=zclG1rJQfE//jx1k89mq8Sted7yFJX6Z6WHzSyKJntw=; h=From:To:Cc:Subject:Date; b=dFFfRxfdFSP0Ib30VNXm/U1dzMlhNIiWAweupEAdI9usYWDW08VmZ6ZcU+Rxp5Kot 6E6hh4VfaKbIs33PX0H5QhAcae8rgkyKrhPDMfZ9/pjjiZkY5VoFXQiRksReJVR+m8 /C1cqTofRLr0IDN4P7lqpe7Xb4FsBWMr+J8mhVl0= Received: from localhost.localdomain ([119.147.10.206]) by newxmesmtplogicsvrszc5-0.qq.com (NewEsmtp) with SMTP id 95590038; Wed, 13 Mar 2024 21:37:21 +0800 X-QQ-mid: xmsmtpt1710337041t2oig940a Message-ID: X-QQ-XMAILINFO: MZ4XH1L85f1r4hh+kWfuftpsbfjR3p2mXLxj26mY+LZCUF/rfXhb3UC90OqvYu Q+bOljob7aJqrUsb4uvdetD8n4uqmCQ56jxHsUdNGJgs9pwAT6kVRF8m154LVb6GAPnGK9BA6zon TmrLYLX2Fp+2qafqaJ4WZFJAok/7JuoY9OPkzKi2MbQ9jwW7X9hGplK27gl2ZxvEgzUz4vDnPuU6 7bi7YX7DPDeLW2MrGUQPA/I0+B65LpIXCdDqfg29W1dVDJcgXrAXcrV32BLokMCkLbSXR8Qp83zg SzZH/0WbSHnRQbo7fJPfK7vk5BehEz24AQGI/ALzCe+SiNLjyDTNIuEZVgVkaHL43xsunjmRF6WI lNfrX7V1REBp7ZvzOABGUxb9FIoCQwWbsKuKcW2+K+/uH764Q0GApD0wpU8I4eSeQlbK10Z3K+ES 60OlXlP5aV1g4mPPetncVgXXvR7O8I3u7x0fE1aHNrL/tWLZAF8g5V35j/yfTb+Jq4syShxMiVkw wy69hzdATx1C3DnMCod/pkoHTY9ZXqrsMhtThBkIyGTFL6PIZ9xKkOCydziGuGEnhvZfzcc0QWKM eBMA2gaKfEbSUjR7pB/CaCN0ygcihoIbWv0XITXHDKsJigACTGxb8+070zb894IzOw2stwa3Fx0f CAfE8XSSzf3cR4XvbtS+v7CJ8gTWUcLwmbwIVMgKT5xDYofA4I18aK80FtCFIqeOdJ9rvr8CCHSP iip6HpxOE+/AkWkkl6LdlsmBa2/pTuBPTH8KCmJvyKfNuywxcJ+YG+Cck2tPL5sCbYpJG2ppYdaC tRVeHL7tSThblhZ2CHf7Wfa7nqBosOEFyEZ4g3+K8IaU3Bk2nzgX7hpaPMDWywu/82WVOLLkRNl5 132tbB9hy+Nm/CWwuoeufXhlUM8ProoFJUagYguqc5X4NseFAmTMokbM0miEc7zR4b/owtZhwlr+ lxYzt2e7x4UuxM0BDpG3SHcF7rWvLFp8n8kjUBW6uCU98WxJLynQ== X-QQ-XMRINFO: NI4Ajvh11aEj8Xl/2s1/T8w= From: Zhao Zhili To: ffmpeg-devel@ffmpeg.org Date: Wed, 13 Mar 2024 21:36:42 +0800 X-OQ-MSGID: <20240313133642.70767-1-quinkblack@foxmail.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v5] 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 Cc: Zhao Zhili Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: From: Zhao Zhili --- v5: Decode chroma_format_idc directly instead of ff_h264_decode_seq_parameter_set v4: Fix missing SEI in set_avcc_extradata v3: Remove unnecessary inclusion libavcodec/libx264.c | 162 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 138 insertions(+), 24 deletions(-) diff --git a/libavcodec/libx264.c b/libavcodec/libx264.c index 10d646bd76..64cefb5fb0 100644 --- a/libavcodec/libx264.c +++ b/libavcodec/libx264.c @@ -38,6 +38,7 @@ #include "packet_internal.h" #include "atsc_a53.h" #include "sei.h" +#include "golomb.h" #include #include @@ -865,6 +866,140 @@ 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; + int chroma_format_idc; + int bit_depth_luma_minus8, bit_depth_chroma_minus8; + + /* It's not possible to have emulation prevention byte before + * bit_depth_chroma_minus8 due to the range of sps id, chroma_format_idc + * and so on. So we can read directly without need to escape emulation + * prevention byte. + * + * +4 to skip until sps id. + */ + init_get_bits8(&gbc, sps + 4, sps_nal->i_payload - 4 - 4); + // Skip sps id + get_ue_golomb_31(&gbc); + chroma_format_idc = get_ue_golomb_31(&gbc); + if (chroma_format_idc == 3) + skip_bits1(&gbc); + bit_depth_luma_minus8 = get_ue_golomb_31(&gbc); + bit_depth_chroma_minus8 = get_ue_golomb_31(&gbc); + + *p++ = 0xFC | chroma_format_idc; + *p++ = 0xF8 | bit_depth_luma_minus8; + *p++ = 0xF8 | bit_depth_chroma_minus8; + *p++ = 0; + } + 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 +1368,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); -- 2.25.1 _______________________________________________ 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".