1 gst-ffmpeg: h264: set parameters from SPS whenever it changes
3 Fixes a crash in the fuzzed sample sample_varPAR.avi_s26638 with
4 alternating bit depths.
6 Upstream-Status: Backport
8 Signed-off-by: Yue Tao <yue.tao@windriver.com>
10 diff --git a/gst-libs/ext/libav/libavcodec/h264.c.old b/gst-libs/ext/libav/libavcodec/h264.c
11 index 3621f41..718906a 100644
12 --- a/gst-libs/ext/libav/libavcodec/h264.c.old
13 +++ b/gst-libs/ext/libav/libavcodec/h264.c
14 @@ -2491,6 +2491,34 @@ int ff_h264_get_profile(SPS *sps)
18 +static int h264_set_parameter_from_sps(H264Context *h)
20 + MpegEncContext *s = &h->s;
21 + AVCodecContext * avctx= s->avctx;
23 + if (s->flags& CODEC_FLAG_LOW_DELAY ||
24 + (h->sps.bitstream_restriction_flag && !h->sps.num_reorder_frames))
27 + if(avctx->has_b_frames < 2)
28 + avctx->has_b_frames= !s->low_delay;
30 + if (avctx->bits_per_raw_sample != h->sps.bit_depth_luma) {
31 + if (h->sps.bit_depth_luma >= 8 && h->sps.bit_depth_luma <= 10) {
32 + avctx->bits_per_raw_sample = h->sps.bit_depth_luma;
33 + h->pixel_shift = h->sps.bit_depth_luma > 8;
35 + ff_h264dsp_init(&h->h264dsp, h->sps.bit_depth_luma);
36 + ff_h264_pred_init(&h->hpc, s->codec_id, h->sps.bit_depth_luma);
37 + dsputil_init(&s->dsp, s->avctx);
39 + av_log(avctx, AV_LOG_DEBUG, "Unsupported bit depth: %d\n", h->sps.bit_depth_luma);
47 * decodes a slice header.
48 * This will also call MPV_common_init() and frame_start() as needed.
49 @@ -2505,7 +2533,7 @@ static int decode_slice_header(H264Context *h, H264Context *h0){
50 MpegEncContext * const s0 = &h0->s;
51 unsigned int first_mb_in_slice;
53 - int num_ref_idx_active_override_flag;
54 + int num_ref_idx_active_override_flag, ret;
55 unsigned int slice_type, tmp, i, j;
56 int default_ref_list_done = 0;
57 int last_pic_structure;
58 @@ -2569,7 +2597,17 @@ static int decode_slice_header(H264Context *h, H264Context *h0){
59 av_log(h->s.avctx, AV_LOG_ERROR, "non-existing SPS %u referenced\n", h->pps.sps_id);
62 - h->sps = *h0->sps_buffers[h->pps.sps_id];
64 + if (h->pps.sps_id != h->current_sps_id ||
65 + h0->sps_buffers[h->pps.sps_id]->new) {
66 + h0->sps_buffers[h->pps.sps_id]->new = 0;
68 + h->current_sps_id = h->pps.sps_id;
69 + h->sps = *h0->sps_buffers[h->pps.sps_id];
71 + if ((ret = h264_set_parameter_from_sps(h)) < 0)
75 s->avctx->profile = ff_h264_get_profile(&h->sps);
76 s->avctx->level = h->sps.level_idc;
77 @@ -3811,26 +3811,8 @@ static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size){
79 init_get_bits(&s->gb, ptr, bit_length);
80 ff_h264_decode_seq_parameter_set(h);
82 - if (s->flags& CODEC_FLAG_LOW_DELAY ||
83 - (h->sps.bitstream_restriction_flag && !h->sps.num_reorder_frames))
86 - if(avctx->has_b_frames < 2)
87 - avctx->has_b_frames= !s->low_delay;
89 - if (avctx->bits_per_raw_sample != h->sps.bit_depth_luma) {
90 - if (h->sps.bit_depth_luma >= 8 && h->sps.bit_depth_luma <= 10) {
91 - avctx->bits_per_raw_sample = h->sps.bit_depth_luma;
92 - h->pixel_shift = h->sps.bit_depth_luma > 8;
94 - ff_h264dsp_init(&h->h264dsp, h->sps.bit_depth_luma);
95 - ff_h264_pred_init(&h->hpc, s->codec_id, h->sps.bit_depth_luma);
96 - dsputil_init(&s->dsp, s->avctx);
98 - av_log(avctx, AV_LOG_DEBUG, "Unsupported bit depth: %d\n", h->sps.bit_depth_luma);
101 + if (h264_set_parameter_from_sps(h) < 0) {
106 diff --git a/gst-libs/ext/libav/libavcodec/h264.h.old b/gst-libs/ext/libav/libavcodec/h264.h
107 index e3cc815..b77ad98 100644
108 --- a/gst-libs/ext/libav/libavcodec/h264.h.old
109 +++ b/gst-libs/ext/libav/libavcodec/h264.h
110 @@ -202,6 +202,7 @@ typedef struct SPS{
111 int bit_depth_chroma; ///< bit_depth_chroma_minus8 + 8
112 int residual_color_transform_flag; ///< residual_colour_transform_flag
113 int constraint_set_flags; ///< constraint_set[0-3]_flag
114 + int new; ///< flag to keep track if the decoder context needs re-init due to changed SPS
118 @@ -333,6 +334,7 @@ typedef struct H264Context{
122 + unsigned current_sps_id; ///< id of the current SPS
123 SPS sps; ///< current sps
126 diff --git a/gst-libs/ext/libav/libavcodec/h264_ps.c.old b/gst-libs/ext/libav/libavcodec/h264_ps.c
127 index 7491807..0929098 100644
128 --- a/gst-libs/ext/libav/libavcodec/h264_ps.c.old
129 +++ b/gst-libs/ext/libav/libavcodec/h264_ps.c
130 @@ -438,10 +438,13 @@ int ff_h264_decode_seq_parameter_set(H264Context *h){
131 sps->timing_info_present_flag ? sps->time_scale : 0
136 av_free(h->sps_buffers[sps_id]);
137 - h->sps_buffers[sps_id]= sps;
139 + h->sps_buffers[sps_id] = sps;
141 + h->current_sps_id = sps_id;