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
Unified · Split
| @@ -3539,7 +3539,7 @@ libwebp_encoder_deps="libwebp" · 3538 unmodified lines | ||
| 3539 | 3539 | libwebp_anim_encoder_deps="libwebp" |
| 3540 | 3540 | libx262_encoder_deps="libx262" |
| 3541 | 3541 | libx264_encoder_deps="libx264" |
| 3542 | −libx264_encoder_select="atsc_a53" | |
| 3542 | +libx264_encoder_select="atsc_a53 golomb" | |
| 3543 | 3543 | libx264rgb_encoder_deps="libx264" |
| 3544 | 3544 | libx264rgb_encoder_select="libx264_encoder" |
| 3545 | 3545 | libx265_encoder_deps="libx265" |
| @@ -39,6 +39,7 @@ · 38 unmodified lines | ||
| 39 | 39 | #include "packet_internal.h" |
| 40 | 40 | #include "atsc_a53.h" |
| 41 | 41 | #include "sei.h" |
| 42 | +#include "golomb.h" | |
| 42 | 43 | |
| 43 | 44 | #include <x264.h> |
| 44 | 45 | #include <float.h> |
| @@ -848,6 +849,144 @@ static int convert_pix_fmt(enum AVPixelFormat pix_fmt) · 803 unmodified lines | ||
| 848 | 849 | return 0; |
| 849 | 850 | } |
| 850 | 851 | |
| 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 | + | |
| 851 | 990 | #define PARSE_X264_OPT(name, var)\ |
| 852 | 991 | if (x4->var && x264_param_parse(&x4->params, name, x4->var) < 0) {\ |
| 853 | 992 | 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 | ||
| 1295 | 1434 | return AVERROR_EXTERNAL; |
| 1296 | 1435 | |
| 1297 | 1436 | 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; | |
| 1322 | 1440 | } |
| 1323 | 1441 | |
| 1324 | 1442 | cpb_props = ff_encode_add_cpb_side_data(avctx); |