From 16fa513392adbdd786776d12258f39ad81149a19 Mon Sep 17 00:00:00 2001 From: James Almer Date: Tue, 26 Nov 2019 11:58:16 -0300 Subject: [PATCH] avformat/avc: write the missing bits in the AVC Decoder Configuration Box Signed-off-by: James Almer --- libavformat/avc.c | 37 ++++++++++++++++++++++++++++++++----- libavformat/avc.h | 1 + 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/libavformat/avc.c b/libavformat/avc.c index 7f9d81825b..a3516a2fb7 100644 --- a/libavformat/avc.c +++ b/libavformat/avc.c @@ -108,11 +108,11 @@ int ff_avc_parse_nal_units_buf(const uint8_t *buf_in, uint8_t **buf, int *size) int ff_isom_write_avcc(AVIOContext *pb, const uint8_t *data, int len) { - AVIOContext *sps_pb = NULL, *pps_pb = NULL; + AVIOContext *sps_pb = NULL, *pps_pb = NULL, *sps_ext_pb = NULL; uint8_t *buf = NULL, *end, *start = NULL; - uint8_t *sps, *pps; - uint32_t sps_size = 0, pps_size = 0; - int ret, nb_sps = 0, nb_pps = 0; + uint8_t *sps, *pps, *sps_ext; + uint32_t sps_size = 0, pps_size = 0, sps_ext_size = 0; + int ret, nb_sps = 0, nb_pps = 0, nb_sps_ext = 0; if (len <= 6) return AVERROR_INVALIDDATA; @@ -134,6 +134,9 @@ int ff_isom_write_avcc(AVIOContext *pb, const uint8_t *data, int len) if (ret < 0) goto fail; ret = avio_open_dyn_buf(&pps_pb); + if (ret < 0) + goto fail; + ret = avio_open_dyn_buf(&sps_ext_pb); if (ret < 0) goto fail; @@ -161,12 +164,21 @@ int ff_isom_write_avcc(AVIOContext *pb, const uint8_t *data, int len) } avio_wb16(pps_pb, size); avio_write(pps_pb, buf, size); + } else if (nal_type == 13) { /* SPS_EXT */ + nb_sps_ext++; + if (size > UINT16_MAX || nb_sps_ext >= 256) { + ret = AVERROR_INVALIDDATA; + goto fail; + } + avio_wb16(sps_ext_pb, size); + avio_write(sps_ext_pb, buf, size); } buf += size; } sps_size = avio_get_dyn_buf(sps_pb, &sps); pps_size = avio_get_dyn_buf(pps_pb, &pps); + sps_ext_size = avio_get_dyn_buf(sps_ext_pb, &sps_ext); if (sps_size < 6 || !pps_size) { ret = AVERROR_INVALIDDATA; @@ -184,9 +196,23 @@ int ff_isom_write_avcc(AVIOContext *pb, const uint8_t *data, int len) avio_w8(pb, nb_pps); /* number of pps */ avio_write(pb, pps, pps_size); + if (sps[3] != 66 && sps[3] != 77 && sps[3] != 88) { + H264SequenceParameterSet *seq = ff_avc_decode_sps(sps, sps_size); + if (!seq) + goto fail; + avio_w8(pb, 0xfc | seq->chroma_format_idc); /* 6 bits reserved (111111) + chroma_format_idc */ + avio_w8(pb, 0xf8 | (seq->bit_depth_luma - 8)); /* 5 bits reserved (11111) + bit_depth_luma_minus8 */ + avio_w8(pb, 0xf8 | (seq->bit_depth_chroma - 8)); /* 5 bits reserved (11111) + bit_depth_chroma_minus8 */ + avio_w8(pb, nb_sps_ext); /* number of sps ext */ + if (nb_sps_ext) + avio_write(pb, sps_ext, sps_ext_size); + av_free(seq); + } + fail: ffio_free_dyn_buf(&sps_pb); ffio_free_dyn_buf(&pps_pb); + ffio_free_dyn_buf(&sps_ext_pb); av_free(start); return ret; @@ -348,7 +374,7 @@ H264SequenceParameterSet *ff_avc_decode_sps(const uint8_t *buf, int buf_size) skip_bits1(&gb); // separate_colour_plane_flag } sps->bit_depth_luma = get_ue_golomb(&gb) + 8; - get_ue_golomb(&gb); // bit_depth_chroma_minus8 + sps->bit_depth_chroma = get_ue_golomb(&gb) + 8; skip_bits1(&gb); // qpprime_y_zero_transform_bypass_flag if (get_bits1(&gb)) { // seq_scaling_matrix_present_flag for (i = 0; i < ((sps->chroma_format_idc != 3) ? 8 : 12); i++) { @@ -369,6 +395,7 @@ H264SequenceParameterSet *ff_avc_decode_sps(const uint8_t *buf, int buf_size) } else { sps->chroma_format_idc = 1; sps->bit_depth_luma = 8; + sps->bit_depth_chroma = 8; } get_ue_golomb(&gb); // log2_max_frame_num_minus4 diff --git a/libavformat/avc.h b/libavformat/avc.h index a79bf9b2db..5286d19d89 100644 --- a/libavformat/avc.h +++ b/libavformat/avc.h @@ -43,6 +43,7 @@ typedef struct { uint8_t constraint_set_flags; uint8_t chroma_format_idc; uint8_t bit_depth_luma; + uint8_t bit_depth_chroma; uint8_t frame_mbs_only_flag; AVRational sar; } H264SequenceParameterSet;