History

avcodec/libx264: fix extradata when config annexb=0

AVCodecContext extradata should be an AVCDecoderConfigurationRecord when bitstream format is avcc. Simply concatenating the NALUs output by x264_encoder_headers does not form a standard AVCDecoderConfigurationRecord. The following cmd generates broken file before the patch: ffmpeg -i foo.mp4 -c:v libx264 -x264-params annexb=0 bar.mp4

Zhao Zhili committed 2 years ago

Commit 52297781 parent c7751632 files · +143 −25Browse files

Showing the changes to libavcodec (1 of 2 files). Show all

Unified · Split

libavcodec/libx264.c+142−24
@@ -39,6 +39,7 @@ · 38 unmodified lines
3939 #include "packet_internal.h"
4040 #include "atsc_a53.h"
4141 #include "sei.h"
42+#include "golomb.h"
4243
4344 #include <x264.h>
4445 #include <float.h>
@@ -848,6 +849,144 @@ static int convert_pix_fmt(enum AVPixelFormat pix_fmt) · 803 unmodified lines
848849 return 0;
849850 }
850851
852+static int save_sei(AVCodecContext *avctx, x264_nal_t *nal)
853+{
854+ X264Context *x4 = avctx->priv_data;
855+
856+ av_log(avctx, AV_LOG_INFO, "%s\n", nal->p_payload + 25);
857+ x4->sei_size = nal->i_payload;
858+ x4->sei = av_malloc(x4->sei_size);
859+ if (!x4->sei)
860+ return AVERROR(ENOMEM);
861+
862+ memcpy(x4->sei, nal->p_payload, nal->i_payload);
863+
864+ return 0;
865+}
866+
867+#if CONFIG_LIBX264_ENCODER
868+static int set_avcc_extradata(AVCodecContext *avctx, x264_nal_t *nal, int nnal)
869+{
870+ X264Context *x4 = avctx->priv_data;
871+ x264_nal_t *sps_nal = NULL;
872+ x264_nal_t *pps_nal = NULL;
873+ uint8_t *p, *sps;
874+ int ret;
875+
876+ /* We know it's in the order of SPS/PPS/SEI, but it's not documented in x264 API.
877+ * The x264 param i_sps_id implies there is a single pair of SPS/PPS.
878+ */
879+ for (int i = 0; i < nnal; i++) {
880+ switch (nal[i].i_type) {
881+ case NAL_SPS:
882+ sps_nal = &nal[i];
883+ break;
884+ case NAL_PPS:
885+ pps_nal = &nal[i];
886+ break;
887+ case NAL_SEI:
888+ ret = save_sei(avctx, &nal[i]);
889+ if (ret < 0)
890+ return ret;
891+ break;
892+ }
893+ }
894+ if (!sps_nal || !pps_nal)
895+ return AVERROR_EXTERNAL;
896+
897+ avctx->extradata_size = sps_nal->i_payload + pps_nal->i_payload + 7;
898+ avctx->extradata = av_mallocz(avctx->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
899+ if (!avctx->extradata)
900+ return AVERROR(ENOMEM);
901+
902+ // Now create AVCDecoderConfigurationRecord
903+ p = avctx->extradata;
904+ // Skip size part
905+ sps = sps_nal->p_payload + 4;
906+ *p++ = 1; // version
907+ *p++ = sps[1]; // AVCProfileIndication
908+ *p++ = sps[2]; // profile_compatibility
909+ *p++ = sps[3]; // AVCLevelIndication
910+ *p++ = 0xFF;
911+ *p++ = 0xE0 | 0x01; // 3 bits reserved (111) + 5 bits number of sps
912+ memcpy(p, sps_nal->p_payload + 2, sps_nal->i_payload - 2);
913+ // Make sps has AV_INPUT_BUFFER_PADDING_SIZE padding, so it can be used
914+ // with GetBitContext
915+ sps = p + 2;
916+ p += sps_nal->i_payload - 2;
917+ *p++ = 1;
918+ memcpy(p, pps_nal->p_payload + 2, pps_nal->i_payload - 2);
919+ p += pps_nal->i_payload - 2;
920+
921+ if (sps[3] != 66 && sps[3] != 77 && sps[3] != 88) {
922+ GetBitContext gbc;
923+ int chroma_format_idc;
924+ int bit_depth_luma_minus8, bit_depth_chroma_minus8;
925+
926+ /* It's not possible to have emulation prevention byte before
927+ * bit_depth_chroma_minus8 due to the range of sps id, chroma_format_idc
928+ * and so on. So we can read directly without need to escape emulation
929+ * prevention byte.
930+ *
931+ * +4 to skip until sps id.
932+ */
933+ init_get_bits8(&gbc, sps + 4, sps_nal->i_payload - 4 - 4);
934+ // Skip sps id
935+ get_ue_golomb_31(&gbc);
936+ chroma_format_idc = get_ue_golomb_31(&gbc);
937+ if (chroma_format_idc == 3)
938+ skip_bits1(&gbc);
939+ bit_depth_luma_minus8 = get_ue_golomb_31(&gbc);
940+ bit_depth_chroma_minus8 = get_ue_golomb_31(&gbc);
941+
942+ *p++ = 0xFC | chroma_format_idc;
943+ *p++ = 0xF8 | bit_depth_luma_minus8;
944+ *p++ = 0xF8 | bit_depth_chroma_minus8;
945+ *p++ = 0;
946+ }
947+ av_assert2(avctx->extradata + avctx->extradata_size >= p);
948+ avctx->extradata_size = p - avctx->extradata;
949+
950+ return 0;
951+}
952+#endif
953+
954+static int set_extradata(AVCodecContext *avctx)
955+{
956+ X264Context *x4 = avctx->priv_data;
957+ x264_nal_t *nal;
958+ uint8_t *p;
959+ int nnal, s;
960+
961+ s = x264_encoder_headers(x4->enc, &nal, &nnal);
962+ if (s < 0)
963+ return AVERROR_EXTERNAL;
964+
965+#if CONFIG_LIBX264_ENCODER
966+ if (!x4->params.b_annexb)
967+ return set_avcc_extradata(avctx, nal, nnal);
968+#endif
969+
970+ avctx->extradata = p = av_mallocz(s + AV_INPUT_BUFFER_PADDING_SIZE);
971+ if (!p)
972+ return AVERROR(ENOMEM);
973+
974+ for (int i = 0; i < nnal; i++) {
975+ /* Don't put the SEI in extradata. */
976+ if (nal[i].i_type == NAL_SEI) {
977+ s = save_sei(avctx, &nal[i]);
978+ if (s < 0)
979+ return s;
980+ continue;
981+ }
982+ memcpy(p, nal[i].p_payload, nal[i].i_payload);
983+ p += nal[i].i_payload;
984+ }
985+ avctx->extradata_size = p - avctx->extradata;
986+
987+ return 0;
988+}
989+
851990 #define PARSE_X264_OPT(name, var)\
852991 if (x4->var && x264_param_parse(&x4->params, name, x4->var) < 0) {\
853992 av_log(avctx, AV_LOG_ERROR, "Error parsing option '%s' with value '%s'.\n", name, x4->var);\
@@ -1295,30 +1434,9 @@ FF_ENABLE_DEPRECATION_WARNINGS · 441 unmodified lines
12951434 return AVERROR_EXTERNAL;
12961435
12971436 if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
1298− x264_nal_t *nal;
1299− uint8_t *p;
1300− int nnal, s, i;
1301−
1302− s = x264_encoder_headers(x4->enc, &nal, &nnal);
1303− avctx->extradata = p = av_mallocz(s + AV_INPUT_BUFFER_PADDING_SIZE);
1304− if (!p)
1305− return AVERROR(ENOMEM);
1306−
1307− for (i = 0; i < nnal; i++) {
1308− /* Don't put the SEI in extradata. */
1309− if (nal[i].i_type == NAL_SEI) {
1310− av_log(avctx, AV_LOG_INFO, "%s\n", nal[i].p_payload+25);
1311− x4->sei_size = nal[i].i_payload;
1312− x4->sei = av_malloc(x4->sei_size);
1313− if (!x4->sei)
1314− return AVERROR(ENOMEM);
1315− memcpy(x4->sei, nal[i].p_payload, nal[i].i_payload);
1316− continue;
1317− }
1318− memcpy(p, nal[i].p_payload, nal[i].i_payload);
1319− p += nal[i].i_payload;
1320− }
1321− avctx->extradata_size = p - avctx->extradata;
1437+ ret = set_extradata(avctx);
1438+ if (ret < 0)
1439+ return ret;
13221440 }
13231441
13241442 cpb_props = ff_encode_add_cpb_side_data(avctx);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t