Commit 16fa5133 authored by James Almer's avatar James Almer

avformat/avc: write the missing bits in the AVC Decoder Configuration Box

Signed-off-by: 's avatarJames Almer <jamrial@gmail.com>
parent 4d97b2ad
...@@ -108,11 +108,11 @@ int ff_avc_parse_nal_units_buf(const uint8_t *buf_in, uint8_t **buf, int *size) ...@@ -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) 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 *buf = NULL, *end, *start = NULL;
uint8_t *sps, *pps; uint8_t *sps, *pps, *sps_ext;
uint32_t sps_size = 0, pps_size = 0; uint32_t sps_size = 0, pps_size = 0, sps_ext_size = 0;
int ret, nb_sps = 0, nb_pps = 0; int ret, nb_sps = 0, nb_pps = 0, nb_sps_ext = 0;
if (len <= 6) if (len <= 6)
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
...@@ -134,6 +134,9 @@ int ff_isom_write_avcc(AVIOContext *pb, const uint8_t *data, int len) ...@@ -134,6 +134,9 @@ int ff_isom_write_avcc(AVIOContext *pb, const uint8_t *data, int len)
if (ret < 0) if (ret < 0)
goto fail; goto fail;
ret = avio_open_dyn_buf(&pps_pb); ret = avio_open_dyn_buf(&pps_pb);
if (ret < 0)
goto fail;
ret = avio_open_dyn_buf(&sps_ext_pb);
if (ret < 0) if (ret < 0)
goto fail; goto fail;
...@@ -161,12 +164,21 @@ int ff_isom_write_avcc(AVIOContext *pb, const uint8_t *data, int len) ...@@ -161,12 +164,21 @@ int ff_isom_write_avcc(AVIOContext *pb, const uint8_t *data, int len)
} }
avio_wb16(pps_pb, size); avio_wb16(pps_pb, size);
avio_write(pps_pb, buf, 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; buf += size;
} }
sps_size = avio_get_dyn_buf(sps_pb, &sps); sps_size = avio_get_dyn_buf(sps_pb, &sps);
pps_size = avio_get_dyn_buf(pps_pb, &pps); 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) { if (sps_size < 6 || !pps_size) {
ret = AVERROR_INVALIDDATA; ret = AVERROR_INVALIDDATA;
...@@ -184,9 +196,23 @@ int ff_isom_write_avcc(AVIOContext *pb, const uint8_t *data, int len) ...@@ -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_w8(pb, nb_pps); /* number of pps */
avio_write(pb, pps, pps_size); 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: fail:
ffio_free_dyn_buf(&sps_pb); ffio_free_dyn_buf(&sps_pb);
ffio_free_dyn_buf(&pps_pb); ffio_free_dyn_buf(&pps_pb);
ffio_free_dyn_buf(&sps_ext_pb);
av_free(start); av_free(start);
return ret; return ret;
...@@ -348,7 +374,7 @@ H264SequenceParameterSet *ff_avc_decode_sps(const uint8_t *buf, int buf_size) ...@@ -348,7 +374,7 @@ H264SequenceParameterSet *ff_avc_decode_sps(const uint8_t *buf, int buf_size)
skip_bits1(&gb); // separate_colour_plane_flag skip_bits1(&gb); // separate_colour_plane_flag
} }
sps->bit_depth_luma = get_ue_golomb(&gb) + 8; 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 skip_bits1(&gb); // qpprime_y_zero_transform_bypass_flag
if (get_bits1(&gb)) { // seq_scaling_matrix_present_flag if (get_bits1(&gb)) { // seq_scaling_matrix_present_flag
for (i = 0; i < ((sps->chroma_format_idc != 3) ? 8 : 12); i++) { 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) ...@@ -369,6 +395,7 @@ H264SequenceParameterSet *ff_avc_decode_sps(const uint8_t *buf, int buf_size)
} else { } else {
sps->chroma_format_idc = 1; sps->chroma_format_idc = 1;
sps->bit_depth_luma = 8; sps->bit_depth_luma = 8;
sps->bit_depth_chroma = 8;
} }
get_ue_golomb(&gb); // log2_max_frame_num_minus4 get_ue_golomb(&gb); // log2_max_frame_num_minus4
......
...@@ -43,6 +43,7 @@ typedef struct { ...@@ -43,6 +43,7 @@ typedef struct {
uint8_t constraint_set_flags; uint8_t constraint_set_flags;
uint8_t chroma_format_idc; uint8_t chroma_format_idc;
uint8_t bit_depth_luma; uint8_t bit_depth_luma;
uint8_t bit_depth_chroma;
uint8_t frame_mbs_only_flag; uint8_t frame_mbs_only_flag;
AVRational sar; AVRational sar;
} H264SequenceParameterSet; } H264SequenceParameterSet;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment