Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * H.26L/H.264/AVC/JVT/14496-10/... parameter set decoding | ||
3 | * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at> | ||
4 | * | ||
5 | * This file is part of FFmpeg. | ||
6 | * | ||
7 | * FFmpeg is free software; you can redistribute it and/or | ||
8 | * modify it under the terms of the GNU Lesser General Public | ||
9 | * License as published by the Free Software Foundation; either | ||
10 | * version 2.1 of the License, or (at your option) any later version. | ||
11 | * | ||
12 | * FFmpeg is distributed in the hope that it will be useful, | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
15 | * Lesser General Public License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU Lesser General Public | ||
18 | * License along with FFmpeg; if not, write to the Free Software | ||
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
20 | */ | ||
21 | |||
22 | /** | ||
23 | * @file | ||
24 | * H.264 / AVC / MPEG-4 part10 parameter set decoding. | ||
25 | * @author Michael Niedermayer <michaelni@gmx.at> | ||
26 | */ | ||
27 | |||
28 | #include <inttypes.h> | ||
29 | |||
30 | #include "libavutil/imgutils.h" | ||
31 | #include "mathops.h" | ||
32 | #include "avcodec.h" | ||
33 | #include "h264data.h" | ||
34 | #include "h2645_vui.h" | ||
35 | #include "h264_ps.h" | ||
36 | #include "golomb.h" | ||
37 | |||
38 | #define MIN_LOG2_MAX_FRAME_NUM 4 | ||
39 | |||
40 | static const uint8_t default_scaling4[2][16] = { | ||
41 | { 6, 13, 20, 28, 13, 20, 28, 32, | ||
42 | 20, 28, 32, 37, 28, 32, 37, 42 }, | ||
43 | { 10, 14, 20, 24, 14, 20, 24, 27, | ||
44 | 20, 24, 27, 30, 24, 27, 30, 34 } | ||
45 | }; | ||
46 | |||
47 | static const uint8_t default_scaling8[2][64] = { | ||
48 | { 6, 10, 13, 16, 18, 23, 25, 27, | ||
49 | 10, 11, 16, 18, 23, 25, 27, 29, | ||
50 | 13, 16, 18, 23, 25, 27, 29, 31, | ||
51 | 16, 18, 23, 25, 27, 29, 31, 33, | ||
52 | 18, 23, 25, 27, 29, 31, 33, 36, | ||
53 | 23, 25, 27, 29, 31, 33, 36, 38, | ||
54 | 25, 27, 29, 31, 33, 36, 38, 40, | ||
55 | 27, 29, 31, 33, 36, 38, 40, 42 }, | ||
56 | { 9, 13, 15, 17, 19, 21, 22, 24, | ||
57 | 13, 13, 17, 19, 21, 22, 24, 25, | ||
58 | 15, 17, 19, 21, 22, 24, 25, 27, | ||
59 | 17, 19, 21, 22, 24, 25, 27, 28, | ||
60 | 19, 21, 22, 24, 25, 27, 28, 30, | ||
61 | 21, 22, 24, 25, 27, 28, 30, 32, | ||
62 | 22, 24, 25, 27, 28, 30, 32, 33, | ||
63 | 24, 25, 27, 28, 30, 32, 33, 35 } | ||
64 | }; | ||
65 | |||
66 | /* maximum number of MBs in the DPB for a given level */ | ||
67 | static const int level_max_dpb_mbs[][2] = { | ||
68 | { 10, 396 }, | ||
69 | { 11, 900 }, | ||
70 | { 12, 2376 }, | ||
71 | { 13, 2376 }, | ||
72 | { 20, 2376 }, | ||
73 | { 21, 4752 }, | ||
74 | { 22, 8100 }, | ||
75 | { 30, 8100 }, | ||
76 | { 31, 18000 }, | ||
77 | { 32, 20480 }, | ||
78 | { 40, 32768 }, | ||
79 | { 41, 32768 }, | ||
80 | { 42, 34816 }, | ||
81 | { 50, 110400 }, | ||
82 | { 51, 184320 }, | ||
83 | { 52, 184320 }, | ||
84 | }; | ||
85 | |||
86 | 14997 | static void remove_pps(H264ParamSets *s, int id) | |
87 | { | ||
88 | 14997 | av_buffer_unref(&s->pps_list[id]); | |
89 | 14997 | } | |
90 | |||
91 | 1052 | static void remove_sps(H264ParamSets *s, int id) | |
92 | { | ||
93 | #if 0 | ||
94 | int i; | ||
95 | if (s->sps_list[id]) { | ||
96 | /* drop all PPS that depend on this SPS */ | ||
97 | for (i = 0; i < FF_ARRAY_ELEMS(s->pps_list); i++) | ||
98 | if (s->pps_list[i] && ((PPS*)s->pps_list[i]->data)->sps_id == id) | ||
99 | remove_pps(s, i); | ||
100 | } | ||
101 | #endif | ||
102 | 1052 | av_buffer_unref(&s->sps_list[id]); | |
103 | 1052 | } | |
104 | |||
105 | 138 | static inline int decode_hrd_parameters(GetBitContext *gb, void *logctx, | |
106 | SPS *sps) | ||
107 | { | ||
108 | int cpb_count, i; | ||
109 | 138 | cpb_count = get_ue_golomb_31(gb) + 1; | |
110 | |||
111 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 138 times.
|
138 | if (cpb_count > 32U) { |
112 | ✗ | av_log(logctx, AV_LOG_ERROR, "cpb_count %d invalid\n", cpb_count); | |
113 | ✗ | return AVERROR_INVALIDDATA; | |
114 | } | ||
115 | |||
116 | 138 | get_bits(gb, 4); /* bit_rate_scale */ | |
117 | 138 | get_bits(gb, 4); /* cpb_size_scale */ | |
118 |
2/2✓ Branch 0 taken 138 times.
✓ Branch 1 taken 138 times.
|
276 | for (i = 0; i < cpb_count; i++) { |
119 | 138 | get_ue_golomb_long(gb); /* bit_rate_value_minus1 */ | |
120 | 138 | get_ue_golomb_long(gb); /* cpb_size_value_minus1 */ | |
121 | 138 | get_bits1(gb); /* cbr_flag */ | |
122 | } | ||
123 | 138 | sps->initial_cpb_removal_delay_length = get_bits(gb, 5) + 1; | |
124 | 138 | sps->cpb_removal_delay_length = get_bits(gb, 5) + 1; | |
125 | 138 | sps->dpb_output_delay_length = get_bits(gb, 5) + 1; | |
126 | 138 | sps->time_offset_length = get_bits(gb, 5); | |
127 | 138 | sps->cpb_cnt = cpb_count; | |
128 | 138 | return 0; | |
129 | } | ||
130 | |||
131 | 815 | static inline int decode_vui_parameters(GetBitContext *gb, void *logctx, | |
132 | SPS *sps) | ||
133 | { | ||
134 | 815 | ff_h2645_decode_common_vui_params(gb, &sps->vui, logctx); | |
135 | |||
136 |
3/4✓ Branch 1 taken 739 times.
✓ Branch 2 taken 76 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 739 times.
|
815 | if (show_bits1(gb) && get_bits_left(gb) < 10) { |
137 | ✗ | av_log(logctx, AV_LOG_WARNING, "Truncated VUI (%d)\n", get_bits_left(gb)); | |
138 | ✗ | return 0; | |
139 | } | ||
140 | |||
141 | 815 | sps->timing_info_present_flag = get_bits1(gb); | |
142 |
2/2✓ Branch 0 taken 739 times.
✓ Branch 1 taken 76 times.
|
815 | if (sps->timing_info_present_flag) { |
143 | 739 | unsigned num_units_in_tick = get_bits_long(gb, 32); | |
144 | 739 | unsigned time_scale = get_bits_long(gb, 32); | |
145 |
2/4✓ Branch 0 taken 739 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 739 times.
|
739 | if (!num_units_in_tick || !time_scale) { |
146 | ✗ | av_log(logctx, AV_LOG_ERROR, | |
147 | "time_scale/num_units_in_tick invalid or unsupported (%u/%u)\n", | ||
148 | time_scale, num_units_in_tick); | ||
149 | ✗ | sps->timing_info_present_flag = 0; | |
150 | } else { | ||
151 | 739 | sps->num_units_in_tick = num_units_in_tick; | |
152 | 739 | sps->time_scale = time_scale; | |
153 | } | ||
154 | 739 | sps->fixed_frame_rate_flag = get_bits1(gb); | |
155 | } | ||
156 | |||
157 | 815 | sps->nal_hrd_parameters_present_flag = get_bits1(gb); | |
158 |
2/2✓ Branch 0 taken 102 times.
✓ Branch 1 taken 713 times.
|
815 | if (sps->nal_hrd_parameters_present_flag) |
159 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 102 times.
|
102 | if (decode_hrd_parameters(gb, logctx, sps) < 0) |
160 | ✗ | return AVERROR_INVALIDDATA; | |
161 | 815 | sps->vcl_hrd_parameters_present_flag = get_bits1(gb); | |
162 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 779 times.
|
815 | if (sps->vcl_hrd_parameters_present_flag) |
163 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 36 times.
|
36 | if (decode_hrd_parameters(gb, logctx, sps) < 0) |
164 | ✗ | return AVERROR_INVALIDDATA; | |
165 |
2/2✓ Branch 0 taken 713 times.
✓ Branch 1 taken 102 times.
|
815 | if (sps->nal_hrd_parameters_present_flag || |
166 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 713 times.
|
713 | sps->vcl_hrd_parameters_present_flag) |
167 | 102 | get_bits1(gb); /* low_delay_hrd_flag */ | |
168 | 815 | sps->pic_struct_present_flag = get_bits1(gb); | |
169 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 815 times.
|
815 | if (!get_bits_left(gb)) |
170 | ✗ | return 0; | |
171 | 815 | sps->bitstream_restriction_flag = get_bits1(gb); | |
172 |
2/2✓ Branch 0 taken 662 times.
✓ Branch 1 taken 153 times.
|
815 | if (sps->bitstream_restriction_flag) { |
173 | 662 | get_bits1(gb); /* motion_vectors_over_pic_boundaries_flag */ | |
174 | 662 | get_ue_golomb_31(gb); /* max_bytes_per_pic_denom */ | |
175 | 662 | get_ue_golomb_31(gb); /* max_bits_per_mb_denom */ | |
176 | 662 | get_ue_golomb_31(gb); /* log2_max_mv_length_horizontal */ | |
177 | 662 | get_ue_golomb_31(gb); /* log2_max_mv_length_vertical */ | |
178 | 662 | sps->num_reorder_frames = get_ue_golomb_31(gb); | |
179 | 662 | get_ue_golomb_31(gb); /*max_dec_frame_buffering*/ | |
180 | |||
181 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 660 times.
|
662 | if (get_bits_left(gb) < 0) { |
182 | 2 | sps->num_reorder_frames = 0; | |
183 | 2 | sps->bitstream_restriction_flag = 0; | |
184 | } | ||
185 | |||
186 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 662 times.
|
662 | if (sps->num_reorder_frames > 16U |
187 | /* max_dec_frame_buffering || max_dec_frame_buffering > 16 */) { | ||
188 | ✗ | av_log(logctx, AV_LOG_ERROR, | |
189 | "Clipping illegal num_reorder_frames %d\n", | ||
190 | sps->num_reorder_frames); | ||
191 | ✗ | sps->num_reorder_frames = 16; | |
192 | ✗ | return AVERROR_INVALIDDATA; | |
193 | } | ||
194 | } | ||
195 | |||
196 | 815 | return 0; | |
197 | } | ||
198 | |||
199 | 2320 | static int decode_scaling_list(GetBitContext *gb, uint8_t *factors, int size, | |
200 | const uint8_t *jvt_list, | ||
201 | const uint8_t *fallback_list) | ||
202 | { | ||
203 | 2320 | int i, last = 8, next = 8; | |
204 |
2/2✓ Branch 0 taken 1740 times.
✓ Branch 1 taken 580 times.
|
2320 | const uint8_t *scan = size == 16 ? ff_zigzag_scan : ff_zigzag_direct; |
205 |
2/2✓ Branch 1 taken 1140 times.
✓ Branch 2 taken 1180 times.
|
2320 | if (!get_bits1(gb)) /* matrix not written, we use the predicted one */ |
206 | 1140 | memcpy(factors, fallback_list, size * sizeof(uint8_t)); | |
207 | else | ||
208 |
2/2✓ Branch 0 taken 30664 times.
✓ Branch 1 taken 1076 times.
|
31740 | for (i = 0; i < size; i++) { |
209 |
2/2✓ Branch 0 taken 28656 times.
✓ Branch 1 taken 2008 times.
|
30664 | if (next) { |
210 | 28656 | int v = get_se_golomb(gb); | |
211 |
2/4✓ Branch 0 taken 28656 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 28656 times.
|
28656 | if (v < -128 || v > 127) { |
212 | ✗ | av_log(NULL, AV_LOG_ERROR, "delta scale %d is invalid\n", v); | |
213 | ✗ | return AVERROR_INVALIDDATA; | |
214 | } | ||
215 | 28656 | next = (last + v) & 0xff; | |
216 | } | ||
217 |
4/4✓ Branch 0 taken 1180 times.
✓ Branch 1 taken 29484 times.
✓ Branch 2 taken 104 times.
✓ Branch 3 taken 1076 times.
|
30664 | if (!i && !next) { /* matrix not written, we use the preset one */ |
218 | 104 | memcpy(factors, jvt_list, size * sizeof(uint8_t)); | |
219 | 104 | break; | |
220 | } | ||
221 |
2/2✓ Branch 0 taken 28406 times.
✓ Branch 1 taken 2154 times.
|
30560 | last = factors[scan[i]] = next ? next : last; |
222 | } | ||
223 | 2320 | return 0; | |
224 | } | ||
225 | |||
226 | /* returns non zero if the provided SPS scaling matrix has been filled */ | ||
227 | 1562 | static int decode_scaling_matrices(GetBitContext *gb, const SPS *sps, | |
228 | const PPS *pps, int is_sps, | ||
229 | uint8_t(*scaling_matrix4)[16], | ||
230 | uint8_t(*scaling_matrix8)[64]) | ||
231 | { | ||
232 |
4/4✓ Branch 0 taken 718 times.
✓ Branch 1 taken 844 times.
✓ Branch 2 taken 260 times.
✓ Branch 3 taken 458 times.
|
1562 | int fallback_sps = !is_sps && sps->scaling_matrix_present; |
233 | 6248 | const uint8_t *fallback[4] = { | |
234 |
2/2✓ Branch 0 taken 260 times.
✓ Branch 1 taken 1302 times.
|
1562 | fallback_sps ? sps->scaling_matrix4[0] : default_scaling4[0], |
235 |
2/2✓ Branch 0 taken 260 times.
✓ Branch 1 taken 1302 times.
|
1562 | fallback_sps ? sps->scaling_matrix4[3] : default_scaling4[1], |
236 |
2/2✓ Branch 0 taken 260 times.
✓ Branch 1 taken 1302 times.
|
1562 | fallback_sps ? sps->scaling_matrix8[0] : default_scaling8[0], |
237 |
2/2✓ Branch 0 taken 260 times.
✓ Branch 1 taken 1302 times.
|
1562 | fallback_sps ? sps->scaling_matrix8[3] : default_scaling8[1] |
238 | }; | ||
239 | 1562 | int ret = 0; | |
240 |
2/2✓ Branch 1 taken 290 times.
✓ Branch 2 taken 1272 times.
|
1562 | if (get_bits1(gb)) { |
241 | 290 | ret |= decode_scaling_list(gb, scaling_matrix4[0], 16, default_scaling4[0], fallback[0]); // Intra, Y | |
242 | 290 | ret |= decode_scaling_list(gb, scaling_matrix4[1], 16, default_scaling4[0], scaling_matrix4[0]); // Intra, Cr | |
243 | 290 | ret |= decode_scaling_list(gb, scaling_matrix4[2], 16, default_scaling4[0], scaling_matrix4[1]); // Intra, Cb | |
244 | 290 | ret |= decode_scaling_list(gb, scaling_matrix4[3], 16, default_scaling4[1], fallback[1]); // Inter, Y | |
245 | 290 | ret |= decode_scaling_list(gb, scaling_matrix4[4], 16, default_scaling4[1], scaling_matrix4[3]); // Inter, Cr | |
246 | 290 | ret |= decode_scaling_list(gb, scaling_matrix4[5], 16, default_scaling4[1], scaling_matrix4[4]); // Inter, Cb | |
247 |
3/4✓ Branch 0 taken 145 times.
✓ Branch 1 taken 145 times.
✓ Branch 2 taken 145 times.
✗ Branch 3 not taken.
|
290 | if (is_sps || pps->transform_8x8_mode) { |
248 | 290 | ret |= decode_scaling_list(gb, scaling_matrix8[0], 64, default_scaling8[0], fallback[2]); // Intra, Y | |
249 | 290 | ret |= decode_scaling_list(gb, scaling_matrix8[3], 64, default_scaling8[1], fallback[3]); // Inter, Y | |
250 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 290 times.
|
290 | if (sps->chroma_format_idc == 3) { |
251 | ✗ | ret |= decode_scaling_list(gb, scaling_matrix8[1], 64, default_scaling8[0], scaling_matrix8[0]); // Intra, Cr | |
252 | ✗ | ret |= decode_scaling_list(gb, scaling_matrix8[4], 64, default_scaling8[1], scaling_matrix8[3]); // Inter, Cr | |
253 | ✗ | ret |= decode_scaling_list(gb, scaling_matrix8[2], 64, default_scaling8[0], scaling_matrix8[1]); // Intra, Cb | |
254 | ✗ | ret |= decode_scaling_list(gb, scaling_matrix8[5], 64, default_scaling8[1], scaling_matrix8[4]); // Inter, Cb | |
255 | } | ||
256 | } | ||
257 |
1/2✓ Branch 0 taken 290 times.
✗ Branch 1 not taken.
|
290 | if (!ret) |
258 | 290 | ret = is_sps; | |
259 | } | ||
260 | |||
261 | 1562 | return ret; | |
262 | } | ||
263 | |||
264 | 1002 | void ff_h264_ps_uninit(H264ParamSets *ps) | |
265 | { | ||
266 | int i; | ||
267 | |||
268 |
2/2✓ Branch 0 taken 32064 times.
✓ Branch 1 taken 1002 times.
|
33066 | for (i = 0; i < MAX_SPS_COUNT; i++) |
269 | 32064 | av_buffer_unref(&ps->sps_list[i]); | |
270 | |||
271 |
2/2✓ Branch 0 taken 256512 times.
✓ Branch 1 taken 1002 times.
|
257514 | for (i = 0; i < MAX_PPS_COUNT; i++) |
272 | 256512 | av_buffer_unref(&ps->pps_list[i]); | |
273 | |||
274 | 1002 | av_buffer_unref(&ps->pps_ref); | |
275 | |||
276 | 1002 | ps->pps = NULL; | |
277 | 1002 | ps->sps = NULL; | |
278 | 1002 | } | |
279 | |||
280 | 2441 | int ff_h264_decode_seq_parameter_set(GetBitContext *gb, AVCodecContext *avctx, | |
281 | H264ParamSets *ps, int ignore_truncation) | ||
282 | { | ||
283 | AVBufferRef *sps_buf; | ||
284 | 2441 | int profile_idc, level_idc, constraint_set_flags = 0; | |
285 | unsigned int sps_id; | ||
286 | int i, log2_max_frame_num_minus4; | ||
287 | SPS *sps; | ||
288 | int ret; | ||
289 | |||
290 | 2441 | sps_buf = av_buffer_allocz(sizeof(*sps)); | |
291 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2441 times.
|
2441 | if (!sps_buf) |
292 | ✗ | return AVERROR(ENOMEM); | |
293 | 2441 | sps = (SPS*)sps_buf->data; | |
294 | |||
295 | 2441 | sps->data_size = gb->buffer_end - gb->buffer; | |
296 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2441 times.
|
2441 | if (sps->data_size > sizeof(sps->data)) { |
297 | ✗ | av_log(avctx, AV_LOG_DEBUG, "Truncating likely oversized SPS\n"); | |
298 | ✗ | sps->data_size = sizeof(sps->data); | |
299 | } | ||
300 | 2441 | memcpy(sps->data, gb->buffer, sps->data_size); | |
301 | |||
302 | // Re-add the removed stop bit (may be used by hwaccels). | ||
303 |
3/4✓ Branch 0 taken 981 times.
✓ Branch 1 taken 1460 times.
✓ Branch 2 taken 981 times.
✗ Branch 3 not taken.
|
2441 | if (!(gb->size_in_bits & 7) && sps->data_size < sizeof(sps->data)) |
304 | 981 | sps->data[sps->data_size++] = 0x80; | |
305 | |||
306 | 2441 | profile_idc = get_bits(gb, 8); | |
307 | 2441 | constraint_set_flags |= get_bits1(gb) << 0; // constraint_set0_flag | |
308 | 2441 | constraint_set_flags |= get_bits1(gb) << 1; // constraint_set1_flag | |
309 | 2441 | constraint_set_flags |= get_bits1(gb) << 2; // constraint_set2_flag | |
310 | 2441 | constraint_set_flags |= get_bits1(gb) << 3; // constraint_set3_flag | |
311 | 2441 | constraint_set_flags |= get_bits1(gb) << 4; // constraint_set4_flag | |
312 | 2441 | constraint_set_flags |= get_bits1(gb) << 5; // constraint_set5_flag | |
313 | 2441 | skip_bits(gb, 2); // reserved_zero_2bits | |
314 | 2441 | level_idc = get_bits(gb, 8); | |
315 | 2441 | sps_id = get_ue_golomb_31(gb); | |
316 | |||
317 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2441 times.
|
2441 | if (sps_id >= MAX_SPS_COUNT) { |
318 | ✗ | av_log(avctx, AV_LOG_ERROR, "sps_id %u out of range\n", sps_id); | |
319 | ✗ | goto fail; | |
320 | } | ||
321 | |||
322 | 2441 | sps->sps_id = sps_id; | |
323 | 2441 | sps->time_offset_length = 24; | |
324 | 2441 | sps->profile_idc = profile_idc; | |
325 | 2441 | sps->constraint_set_flags = constraint_set_flags; | |
326 | 2441 | sps->level_idc = level_idc; | |
327 | 2441 | sps->vui.video_full_range_flag = -1; | |
328 | |||
329 | 2441 | memset(sps->scaling_matrix4, 16, sizeof(sps->scaling_matrix4)); | |
330 | 2441 | memset(sps->scaling_matrix8, 16, sizeof(sps->scaling_matrix8)); | |
331 | 2441 | sps->scaling_matrix_present = 0; | |
332 | 2441 | sps->vui.matrix_coeffs = AVCOL_SPC_UNSPECIFIED; | |
333 | |||
334 |
2/2✓ Branch 0 taken 1833 times.
✓ Branch 1 taken 608 times.
|
2441 | if (sps->profile_idc == 100 || // High profile |
335 |
2/2✓ Branch 0 taken 1785 times.
✓ Branch 1 taken 48 times.
|
1833 | sps->profile_idc == 110 || // High10 profile |
336 |
2/2✓ Branch 0 taken 1727 times.
✓ Branch 1 taken 58 times.
|
1785 | sps->profile_idc == 122 || // High422 profile |
337 |
2/2✓ Branch 0 taken 1597 times.
✓ Branch 1 taken 130 times.
|
1727 | sps->profile_idc == 244 || // High444 Predictive profile |
338 |
1/2✓ Branch 0 taken 1597 times.
✗ Branch 1 not taken.
|
1597 | sps->profile_idc == 44 || // Cavlc444 profile |
339 |
1/2✓ Branch 0 taken 1597 times.
✗ Branch 1 not taken.
|
1597 | sps->profile_idc == 83 || // Scalable Constrained High profile (SVC) |
340 |
1/2✓ Branch 0 taken 1597 times.
✗ Branch 1 not taken.
|
1597 | sps->profile_idc == 86 || // Scalable High Intra profile (SVC) |
341 |
1/2✓ Branch 0 taken 1597 times.
✗ Branch 1 not taken.
|
1597 | sps->profile_idc == 118 || // Stereo High profile (MVC) |
342 |
1/2✓ Branch 0 taken 1597 times.
✗ Branch 1 not taken.
|
1597 | sps->profile_idc == 128 || // Multiview High profile (MVC) |
343 |
1/2✓ Branch 0 taken 1597 times.
✗ Branch 1 not taken.
|
1597 | sps->profile_idc == 138 || // Multiview Depth High profile (MVCD) |
344 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1597 times.
|
1597 | sps->profile_idc == 144) { // old High444 profile |
345 | 844 | sps->chroma_format_idc = get_ue_golomb_31(gb); | |
346 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 844 times.
|
844 | if (sps->chroma_format_idc > 3U) { |
347 | ✗ | avpriv_request_sample(avctx, "chroma_format_idc %u", | |
348 | sps->chroma_format_idc); | ||
349 | ✗ | goto fail; | |
350 |
2/2✓ Branch 0 taken 126 times.
✓ Branch 1 taken 718 times.
|
844 | } else if (sps->chroma_format_idc == 3) { |
351 | 126 | sps->residual_color_transform_flag = get_bits1(gb); | |
352 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 126 times.
|
126 | if (sps->residual_color_transform_flag) { |
353 | ✗ | av_log(avctx, AV_LOG_ERROR, "separate color planes are not supported\n"); | |
354 | ✗ | goto fail; | |
355 | } | ||
356 | } | ||
357 | 844 | sps->bit_depth_luma = get_ue_golomb_31(gb) + 8; | |
358 | 844 | sps->bit_depth_chroma = get_ue_golomb_31(gb) + 8; | |
359 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 844 times.
|
844 | if (sps->bit_depth_chroma != sps->bit_depth_luma) { |
360 | ✗ | avpriv_request_sample(avctx, | |
361 | "Different chroma and luma bit depth"); | ||
362 | ✗ | goto fail; | |
363 | } | ||
364 |
2/4✓ Branch 0 taken 844 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 844 times.
✗ Branch 3 not taken.
|
844 | if (sps->bit_depth_luma < 8 || sps->bit_depth_luma > 14 || |
365 |
2/4✓ Branch 0 taken 844 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 844 times.
|
844 | sps->bit_depth_chroma < 8 || sps->bit_depth_chroma > 14) { |
366 | ✗ | av_log(avctx, AV_LOG_ERROR, "illegal bit depth value (%d, %d)\n", | |
367 | sps->bit_depth_luma, sps->bit_depth_chroma); | ||
368 | ✗ | goto fail; | |
369 | } | ||
370 | 844 | sps->transform_bypass = get_bits1(gb); | |
371 | 844 | ret = decode_scaling_matrices(gb, sps, NULL, 1, | |
372 | 844 | sps->scaling_matrix4, sps->scaling_matrix8); | |
373 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 844 times.
|
844 | if (ret < 0) |
374 | ✗ | goto fail; | |
375 | 844 | sps->scaling_matrix_present |= ret; | |
376 | } else { | ||
377 | 1597 | sps->chroma_format_idc = 1; | |
378 | 1597 | sps->bit_depth_luma = 8; | |
379 | 1597 | sps->bit_depth_chroma = 8; | |
380 | } | ||
381 | |||
382 | 2441 | log2_max_frame_num_minus4 = get_ue_golomb_31(gb); | |
383 |
2/4✓ Branch 0 taken 2441 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2441 times.
|
2441 | if (log2_max_frame_num_minus4 < MIN_LOG2_MAX_FRAME_NUM - 4 || |
384 | log2_max_frame_num_minus4 > MAX_LOG2_MAX_FRAME_NUM - 4) { | ||
385 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
386 | "log2_max_frame_num_minus4 out of range (0-12): %d\n", | ||
387 | log2_max_frame_num_minus4); | ||
388 | ✗ | goto fail; | |
389 | } | ||
390 | 2441 | sps->log2_max_frame_num = log2_max_frame_num_minus4 + 4; | |
391 | |||
392 | 2441 | sps->poc_type = get_ue_golomb_31(gb); | |
393 | |||
394 |
2/2✓ Branch 0 taken 1233 times.
✓ Branch 1 taken 1208 times.
|
2441 | if (sps->poc_type == 0) { // FIXME #define |
395 | 1233 | unsigned t = get_ue_golomb_31(gb); | |
396 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1233 times.
|
1233 | if (t>12) { |
397 | ✗ | av_log(avctx, AV_LOG_ERROR, "log2_max_poc_lsb (%d) is out of range\n", t); | |
398 | ✗ | goto fail; | |
399 | } | ||
400 | 1233 | sps->log2_max_poc_lsb = t + 4; | |
401 |
2/2✓ Branch 0 taken 76 times.
✓ Branch 1 taken 1132 times.
|
1208 | } else if (sps->poc_type == 1) { // FIXME #define |
402 | 76 | sps->delta_pic_order_always_zero_flag = get_bits1(gb); | |
403 | 76 | sps->offset_for_non_ref_pic = get_se_golomb_long(gb); | |
404 | 76 | sps->offset_for_top_to_bottom_field = get_se_golomb_long(gb); | |
405 | |||
406 |
1/2✓ Branch 0 taken 76 times.
✗ Branch 1 not taken.
|
76 | if ( sps->offset_for_non_ref_pic == INT32_MIN |
407 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 76 times.
|
76 | || sps->offset_for_top_to_bottom_field == INT32_MIN |
408 | ) { | ||
409 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
410 | "offset_for_non_ref_pic or offset_for_top_to_bottom_field is out of range\n"); | ||
411 | ✗ | goto fail; | |
412 | } | ||
413 | |||
414 | 76 | sps->poc_cycle_length = get_ue_golomb(gb); | |
415 | |||
416 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 76 times.
|
76 | if ((unsigned)sps->poc_cycle_length >= |
417 | FF_ARRAY_ELEMS(sps->offset_for_ref_frame)) { | ||
418 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
419 | "poc_cycle_length overflow %d\n", sps->poc_cycle_length); | ||
420 | ✗ | goto fail; | |
421 | } | ||
422 | |||
423 |
2/2✓ Branch 0 taken 100 times.
✓ Branch 1 taken 76 times.
|
176 | for (i = 0; i < sps->poc_cycle_length; i++) { |
424 | 100 | sps->offset_for_ref_frame[i] = get_se_golomb_long(gb); | |
425 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 100 times.
|
100 | if (sps->offset_for_ref_frame[i] == INT32_MIN) { |
426 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
427 | "offset_for_ref_frame is out of range\n"); | ||
428 | ✗ | goto fail; | |
429 | } | ||
430 | } | ||
431 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1132 times.
|
1132 | } else if (sps->poc_type != 2) { |
432 | ✗ | av_log(avctx, AV_LOG_ERROR, "illegal POC type %d\n", sps->poc_type); | |
433 | ✗ | goto fail; | |
434 | } | ||
435 | |||
436 | 2441 | sps->ref_frame_count = get_ue_golomb_31(gb); | |
437 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2441 times.
|
2441 | if (avctx->codec_tag == MKTAG('S', 'M', 'V', '2')) |
438 | ✗ | sps->ref_frame_count = FFMAX(2, sps->ref_frame_count); | |
439 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2441 times.
|
2441 | if (sps->ref_frame_count > H264_MAX_DPB_FRAMES) { |
440 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
441 | "too many reference frames %d\n", sps->ref_frame_count); | ||
442 | ✗ | goto fail; | |
443 | } | ||
444 | 2441 | sps->gaps_in_frame_num_allowed_flag = get_bits1(gb); | |
445 | 2441 | sps->mb_width = get_ue_golomb(gb) + 1; | |
446 | 2441 | sps->mb_height = get_ue_golomb(gb) + 1; | |
447 | |||
448 | 2441 | sps->frame_mbs_only_flag = get_bits1(gb); | |
449 | |||
450 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2441 times.
|
2441 | if (sps->mb_height >= INT_MAX / 2U) { |
451 | ✗ | av_log(avctx, AV_LOG_ERROR, "height overflow\n"); | |
452 | ✗ | goto fail; | |
453 | } | ||
454 | 2441 | sps->mb_height *= 2 - sps->frame_mbs_only_flag; | |
455 | |||
456 |
2/2✓ Branch 0 taken 521 times.
✓ Branch 1 taken 1920 times.
|
2441 | if (!sps->frame_mbs_only_flag) |
457 | 521 | sps->mb_aff = get_bits1(gb); | |
458 | else | ||
459 | 1920 | sps->mb_aff = 0; | |
460 | |||
461 |
1/2✓ Branch 0 taken 2441 times.
✗ Branch 1 not taken.
|
2441 | if ((unsigned)sps->mb_width >= INT_MAX / 16 || |
462 |
2/4✓ Branch 0 taken 2441 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2441 times.
|
4882 | (unsigned)sps->mb_height >= INT_MAX / 16 || |
463 | 2441 | av_image_check_size(16 * sps->mb_width, | |
464 | 2441 | 16 * sps->mb_height, 0, avctx)) { | |
465 | ✗ | av_log(avctx, AV_LOG_ERROR, "mb_width/height overflow\n"); | |
466 | ✗ | goto fail; | |
467 | } | ||
468 | |||
469 | 2441 | sps->direct_8x8_inference_flag = get_bits1(gb); | |
470 | |||
471 | 2441 | sps->crop = get_bits1(gb); | |
472 |
2/2✓ Branch 0 taken 342 times.
✓ Branch 1 taken 2099 times.
|
2441 | if (sps->crop) { |
473 | 342 | unsigned int crop_left = get_ue_golomb(gb); | |
474 | 342 | unsigned int crop_right = get_ue_golomb(gb); | |
475 | 342 | unsigned int crop_top = get_ue_golomb(gb); | |
476 | 342 | unsigned int crop_bottom = get_ue_golomb(gb); | |
477 | 342 | int width = 16 * sps->mb_width; | |
478 | 342 | int height = 16 * sps->mb_height; | |
479 | |||
480 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 342 times.
|
342 | if (avctx->flags2 & AV_CODEC_FLAG2_IGNORE_CROP) { |
481 | ✗ | av_log(avctx, AV_LOG_DEBUG, "discarding sps cropping, original " | |
482 | "values are l:%d r:%d t:%d b:%d\n", | ||
483 | crop_left, crop_right, crop_top, crop_bottom); | ||
484 | |||
485 | ✗ | sps->crop_left = | |
486 | ✗ | sps->crop_right = | |
487 | ✗ | sps->crop_top = | |
488 | ✗ | sps->crop_bottom = 0; | |
489 | } else { | ||
490 | 342 | int vsub = (sps->chroma_format_idc == 1) ? 1 : 0; | |
491 | 684 | int hsub = (sps->chroma_format_idc == 1 || | |
492 |
4/4✓ Branch 0 taken 66 times.
✓ Branch 1 taken 276 times.
✓ Branch 2 taken 30 times.
✓ Branch 3 taken 36 times.
|
342 | sps->chroma_format_idc == 2) ? 1 : 0; |
493 | 342 | int step_x = 1 << hsub; | |
494 | 342 | int step_y = (2 - sps->frame_mbs_only_flag) << vsub; | |
495 | |||
496 |
1/2✓ Branch 0 taken 342 times.
✗ Branch 1 not taken.
|
342 | if (crop_left > (unsigned)INT_MAX / 4 / step_x || |
497 |
1/2✓ Branch 0 taken 342 times.
✗ Branch 1 not taken.
|
342 | crop_right > (unsigned)INT_MAX / 4 / step_x || |
498 |
1/2✓ Branch 0 taken 342 times.
✗ Branch 1 not taken.
|
342 | crop_top > (unsigned)INT_MAX / 4 / step_y || |
499 |
1/2✓ Branch 0 taken 342 times.
✗ Branch 1 not taken.
|
342 | crop_bottom> (unsigned)INT_MAX / 4 / step_y || |
500 |
1/2✓ Branch 0 taken 342 times.
✗ Branch 1 not taken.
|
342 | (crop_left + crop_right ) * step_x >= width || |
501 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 342 times.
|
342 | (crop_top + crop_bottom) * step_y >= height |
502 | ) { | ||
503 | ✗ | av_log(avctx, AV_LOG_ERROR, "crop values invalid %d %d %d %d / %d %d\n", crop_left, crop_right, crop_top, crop_bottom, width, height); | |
504 | ✗ | goto fail; | |
505 | } | ||
506 | |||
507 | 342 | sps->crop_left = crop_left * step_x; | |
508 | 342 | sps->crop_right = crop_right * step_x; | |
509 | 342 | sps->crop_top = crop_top * step_y; | |
510 | 342 | sps->crop_bottom = crop_bottom * step_y; | |
511 | } | ||
512 | } else { | ||
513 | 2099 | sps->crop_left = | |
514 | 2099 | sps->crop_right = | |
515 | 2099 | sps->crop_top = | |
516 | 2099 | sps->crop_bottom = | |
517 | 2099 | sps->crop = 0; | |
518 | } | ||
519 | |||
520 | 2441 | sps->vui_parameters_present_flag = get_bits1(gb); | |
521 |
2/2✓ Branch 0 taken 815 times.
✓ Branch 1 taken 1626 times.
|
2441 | if (sps->vui_parameters_present_flag) { |
522 | 815 | int ret = decode_vui_parameters(gb, avctx, sps); | |
523 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 815 times.
|
815 | if (ret < 0) |
524 | ✗ | goto fail; | |
525 | } | ||
526 | |||
527 |
2/2✓ Branch 1 taken 5 times.
✓ Branch 2 taken 2436 times.
|
2441 | if (get_bits_left(gb) < 0) { |
528 | 10 | av_log_once(avctx, ignore_truncation ? AV_LOG_WARNING : AV_LOG_ERROR, AV_LOG_DEBUG, | |
529 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | &ps->overread_warning_printed[sps->vui_parameters_present_flag], |
530 |
1/2✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
|
5 | "Overread %s by %d bits\n", sps->vui_parameters_present_flag ? "VUI" : "SPS", -get_bits_left(gb)); |
531 |
1/2✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 | if (!ignore_truncation) |
532 | 5 | goto fail; | |
533 | } | ||
534 | |||
535 | /* if the maximum delay is not stored in the SPS, derive it based on the | ||
536 | * level */ | ||
537 |
2/2✓ Branch 0 taken 1776 times.
✓ Branch 1 taken 660 times.
|
2436 | if (!sps->bitstream_restriction_flag && |
538 |
3/4✓ Branch 0 taken 59 times.
✓ Branch 1 taken 1717 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 59 times.
|
1776 | (sps->ref_frame_count || avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT)) { |
539 | 1717 | sps->num_reorder_frames = H264_MAX_DPB_FRAMES - 1; | |
540 |
1/2✓ Branch 0 taken 9444 times.
✗ Branch 1 not taken.
|
9444 | for (i = 0; i < FF_ARRAY_ELEMS(level_max_dpb_mbs); i++) { |
541 |
2/2✓ Branch 0 taken 1717 times.
✓ Branch 1 taken 7727 times.
|
9444 | if (level_max_dpb_mbs[i][0] == sps->level_idc) { |
542 | 1717 | sps->num_reorder_frames = FFMIN(level_max_dpb_mbs[i][1] / (sps->mb_width * sps->mb_height), | |
543 | sps->num_reorder_frames); | ||
544 | 1717 | break; | |
545 | } | ||
546 | } | ||
547 | } | ||
548 | |||
549 |
2/2✓ Branch 0 taken 1626 times.
✓ Branch 1 taken 810 times.
|
2436 | if (!sps->vui.sar.den) |
550 | 1626 | sps->vui.sar.den = 1; | |
551 | |||
552 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2436 times.
|
2436 | if (avctx->debug & FF_DEBUG_PICT_INFO) { |
553 | static const char csp[4][5] = { "Gray", "420", "422", "444" }; | ||
554 | ✗ | av_log(avctx, AV_LOG_DEBUG, | |
555 | "sps:%u profile:%d/%d poc:%d ref:%d %dx%d %s %s crop:%u/%u/%u/%u %s %s %"PRId32"/%"PRId32" b%d reo:%d\n", | ||
556 | sps_id, sps->profile_idc, sps->level_idc, | ||
557 | sps->poc_type, | ||
558 | sps->ref_frame_count, | ||
559 | sps->mb_width, sps->mb_height, | ||
560 | ✗ | sps->frame_mbs_only_flag ? "FRM" : (sps->mb_aff ? "MB-AFF" : "PIC-AFF"), | |
561 | ✗ | sps->direct_8x8_inference_flag ? "8B8" : "", | |
562 | sps->crop_left, sps->crop_right, | ||
563 | sps->crop_top, sps->crop_bottom, | ||
564 | ✗ | sps->vui_parameters_present_flag ? "VUI" : "", | |
565 | ✗ | csp[sps->chroma_format_idc], | |
566 | ✗ | sps->timing_info_present_flag ? sps->num_units_in_tick : 0, | |
567 | ✗ | sps->timing_info_present_flag ? sps->time_scale : 0, | |
568 | sps->bit_depth_luma, | ||
569 | ✗ | sps->bitstream_restriction_flag ? sps->num_reorder_frames : -1 | |
570 | ); | ||
571 | } | ||
572 | |||
573 | /* check if this is a repeat of an already parsed SPS, then keep the | ||
574 | * original one. | ||
575 | * otherwise drop all PPSes that depend on it */ | ||
576 |
2/2✓ Branch 0 taken 1453 times.
✓ Branch 1 taken 983 times.
|
2436 | if (ps->sps_list[sps_id] && |
577 |
2/2✓ Branch 0 taken 1384 times.
✓ Branch 1 taken 69 times.
|
1453 | !memcmp(ps->sps_list[sps_id]->data, sps_buf->data, sps_buf->size)) { |
578 | 1384 | av_buffer_unref(&sps_buf); | |
579 | } else { | ||
580 | 1052 | remove_sps(ps, sps_id); | |
581 | 1052 | ps->sps_list[sps_id] = sps_buf; | |
582 | } | ||
583 | |||
584 | 2436 | return 0; | |
585 | |||
586 | 5 | fail: | |
587 | 5 | av_buffer_unref(&sps_buf); | |
588 | 5 | return AVERROR_INVALIDDATA; | |
589 | } | ||
590 | |||
591 | 705 | static void init_dequant8_coeff_table(PPS *pps, const SPS *sps) | |
592 | { | ||
593 | int i, j, q, x; | ||
594 | 705 | const int max_qp = 51 + 6 * (sps->bit_depth_luma - 8); | |
595 | |||
596 |
2/2✓ Branch 0 taken 4230 times.
✓ Branch 1 taken 705 times.
|
4935 | for (i = 0; i < 6; i++) { |
597 | 4230 | pps->dequant8_coeff[i] = pps->dequant8_buffer[i]; | |
598 |
2/2✓ Branch 0 taken 4862 times.
✓ Branch 1 taken 1236 times.
|
6098 | for (j = 0; j < i; j++) |
599 |
2/2✓ Branch 0 taken 2994 times.
✓ Branch 1 taken 1868 times.
|
4862 | if (!memcmp(pps->scaling_matrix8[j], pps->scaling_matrix8[i], |
600 | 64 * sizeof(uint8_t))) { | ||
601 | 2994 | pps->dequant8_coeff[i] = pps->dequant8_buffer[j]; | |
602 | 2994 | break; | |
603 | } | ||
604 |
2/2✓ Branch 0 taken 2994 times.
✓ Branch 1 taken 1236 times.
|
4230 | if (j < i) |
605 | 2994 | continue; | |
606 | |||
607 |
2/2✓ Branch 0 taken 65598 times.
✓ Branch 1 taken 1236 times.
|
66834 | for (q = 0; q < max_qp + 1; q++) { |
608 | 65598 | int shift = ff_h264_quant_div6[q]; | |
609 | 65598 | int idx = ff_h264_quant_rem6[q]; | |
610 |
2/2✓ Branch 0 taken 4198272 times.
✓ Branch 1 taken 65598 times.
|
4263870 | for (x = 0; x < 64; x++) |
611 | 4198272 | pps->dequant8_coeff[i][q][(x >> 3) | ((x & 7) << 3)] = | |
612 | 4198272 | ((uint32_t)ff_h264_dequant8_coeff_init[idx][ff_h264_dequant8_coeff_init_scan[((x >> 1) & 12) | (x & 3)]] * | |
613 | 4198272 | pps->scaling_matrix8[i][x]) << shift; | |
614 | } | ||
615 | } | ||
616 | 705 | } | |
617 | |||
618 | 14997 | static void init_dequant4_coeff_table(PPS *pps, const SPS *sps) | |
619 | { | ||
620 | int i, j, q, x; | ||
621 | 14997 | const int max_qp = 51 + 6 * (sps->bit_depth_luma - 8); | |
622 |
2/2✓ Branch 0 taken 89982 times.
✓ Branch 1 taken 14997 times.
|
104979 | for (i = 0; i < 6; i++) { |
623 | 89982 | pps->dequant4_coeff[i] = pps->dequant4_buffer[i]; | |
624 |
2/2✓ Branch 0 taken 78351 times.
✓ Branch 1 taken 15817 times.
|
94168 | for (j = 0; j < i; j++) |
625 |
2/2✓ Branch 0 taken 74165 times.
✓ Branch 1 taken 4186 times.
|
78351 | if (!memcmp(pps->scaling_matrix4[j], pps->scaling_matrix4[i], |
626 | 16 * sizeof(uint8_t))) { | ||
627 | 74165 | pps->dequant4_coeff[i] = pps->dequant4_buffer[j]; | |
628 | 74165 | break; | |
629 | } | ||
630 |
2/2✓ Branch 0 taken 74165 times.
✓ Branch 1 taken 15817 times.
|
89982 | if (j < i) |
631 | 74165 | continue; | |
632 | |||
633 |
2/2✓ Branch 0 taken 824176 times.
✓ Branch 1 taken 15817 times.
|
839993 | for (q = 0; q < max_qp + 1; q++) { |
634 | 824176 | int shift = ff_h264_quant_div6[q] + 2; | |
635 | 824176 | int idx = ff_h264_quant_rem6[q]; | |
636 |
2/2✓ Branch 0 taken 13186816 times.
✓ Branch 1 taken 824176 times.
|
14010992 | for (x = 0; x < 16; x++) |
637 | 13186816 | pps->dequant4_coeff[i][q][(x >> 2) | ((x << 2) & 0xF)] = | |
638 | 13186816 | ((uint32_t)ff_h264_dequant4_coeff_init[idx][(x & 1) + ((x >> 2) & 1)] * | |
639 | 13186816 | pps->scaling_matrix4[i][x]) << shift; | |
640 | } | ||
641 | } | ||
642 | 14997 | } | |
643 | |||
644 | 14997 | static void init_dequant_tables(PPS *pps, const SPS *sps) | |
645 | { | ||
646 | int i, x; | ||
647 | 14997 | init_dequant4_coeff_table(pps, sps); | |
648 | 14997 | memset(pps->dequant8_coeff, 0, sizeof(pps->dequant8_coeff)); | |
649 | |||
650 |
2/2✓ Branch 0 taken 705 times.
✓ Branch 1 taken 14292 times.
|
14997 | if (pps->transform_8x8_mode) |
651 | 705 | init_dequant8_coeff_table(pps, sps); | |
652 |
2/2✓ Branch 0 taken 22 times.
✓ Branch 1 taken 14975 times.
|
14997 | if (sps->transform_bypass) { |
653 |
2/2✓ Branch 0 taken 132 times.
✓ Branch 1 taken 22 times.
|
154 | for (i = 0; i < 6; i++) |
654 |
2/2✓ Branch 0 taken 2112 times.
✓ Branch 1 taken 132 times.
|
2244 | for (x = 0; x < 16; x++) |
655 | 2112 | pps->dequant4_coeff[i][0][x] = 1 << 6; | |
656 |
2/2✓ Branch 0 taken 21 times.
✓ Branch 1 taken 1 times.
|
22 | if (pps->transform_8x8_mode) |
657 |
2/2✓ Branch 0 taken 126 times.
✓ Branch 1 taken 21 times.
|
147 | for (i = 0; i < 6; i++) |
658 |
2/2✓ Branch 0 taken 8064 times.
✓ Branch 1 taken 126 times.
|
8190 | for (x = 0; x < 64; x++) |
659 | 8064 | pps->dequant8_coeff[i][0][x] = 1 << 6; | |
660 | } | ||
661 | 14997 | } | |
662 | |||
663 | 29994 | static void build_qp_table(PPS *pps, int t, int index, const int depth) | |
664 | { | ||
665 | int i; | ||
666 | 29994 | const int max_qp = 51 + 6 * (depth - 8); | |
667 |
2/2✓ Branch 0 taken 1562664 times.
✓ Branch 1 taken 29994 times.
|
1592658 | for (i = 0; i < max_qp + 1; i++) |
668 | 1562664 | pps->chroma_qp_table[t][i] = | |
669 | 1562664 | ff_h264_chroma_qp[depth - 8][av_clip(i + index, 0, max_qp)]; | |
670 | 29994 | } | |
671 | |||
672 | 718 | static int more_rbsp_data_in_pps(const SPS *sps, void *logctx) | |
673 | { | ||
674 | 718 | int profile_idc = sps->profile_idc; | |
675 | |||
676 |
5/6✓ Branch 0 taken 708 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 705 times.
✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 705 times.
|
718 | if ((profile_idc == 66 || profile_idc == 77 || |
677 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
|
13 | profile_idc == 88) && (sps->constraint_set_flags & 7)) { |
678 | ✗ | av_log(logctx, AV_LOG_VERBOSE, | |
679 | "Current profile doesn't provide more RBSP data in PPS, skipping\n"); | ||
680 | ✗ | return 0; | |
681 | } | ||
682 | |||
683 | 718 | return 1; | |
684 | } | ||
685 | |||
686 | 15011 | static void pps_free(void *opaque, uint8_t *data) | |
687 | { | ||
688 | 15011 | PPS *pps = (PPS*)data; | |
689 | |||
690 | 15011 | av_buffer_unref(&pps->sps_ref); | |
691 | |||
692 | 15011 | av_freep(&data); | |
693 | 15011 | } | |
694 | |||
695 | 15011 | int ff_h264_decode_picture_parameter_set(GetBitContext *gb, AVCodecContext *avctx, | |
696 | H264ParamSets *ps, int bit_length) | ||
697 | { | ||
698 | AVBufferRef *pps_buf; | ||
699 | const SPS *sps; | ||
700 | 15011 | unsigned int pps_id = get_ue_golomb(gb); | |
701 | PPS *pps; | ||
702 | int qp_bd_offset; | ||
703 | int bits_left; | ||
704 | int ret; | ||
705 | |||
706 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 15011 times.
|
15011 | if (pps_id >= MAX_PPS_COUNT) { |
707 | ✗ | av_log(avctx, AV_LOG_ERROR, "pps_id %u out of range\n", pps_id); | |
708 | ✗ | return AVERROR_INVALIDDATA; | |
709 | } | ||
710 | |||
711 | 15011 | pps = av_mallocz(sizeof(*pps)); | |
712 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 15011 times.
|
15011 | if (!pps) |
713 | ✗ | return AVERROR(ENOMEM); | |
714 | 15011 | pps_buf = av_buffer_create((uint8_t*)pps, sizeof(*pps), | |
715 | pps_free, NULL, 0); | ||
716 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 15011 times.
|
15011 | if (!pps_buf) { |
717 | ✗ | av_freep(&pps); | |
718 | ✗ | return AVERROR(ENOMEM); | |
719 | } | ||
720 | |||
721 | 15011 | pps->data_size = gb->buffer_end - gb->buffer; | |
722 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 15011 times.
|
15011 | if (pps->data_size > sizeof(pps->data)) { |
723 | ✗ | av_log(avctx, AV_LOG_DEBUG, "Truncating likely oversized PPS " | |
724 | "(%"SIZE_SPECIFIER" > %"SIZE_SPECIFIER")\n", | ||
725 | ✗ | pps->data_size, sizeof(pps->data)); | |
726 | ✗ | pps->data_size = sizeof(pps->data); | |
727 | } | ||
728 | 15011 | memcpy(pps->data, gb->buffer, pps->data_size); | |
729 | |||
730 | // Re-add the removed stop bit (may be used by hwaccels). | ||
731 |
3/4✓ Branch 0 taken 10359 times.
✓ Branch 1 taken 4652 times.
✓ Branch 2 taken 10359 times.
✗ Branch 3 not taken.
|
15011 | if (!(bit_length & 7) && pps->data_size < sizeof(pps->data)) |
732 | 10359 | pps->data[pps->data_size++] = 0x80; | |
733 | |||
734 | 15011 | pps->sps_id = get_ue_golomb_31(gb); | |
735 |
1/2✓ Branch 0 taken 15011 times.
✗ Branch 1 not taken.
|
15011 | if ((unsigned)pps->sps_id >= MAX_SPS_COUNT || |
736 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 15011 times.
|
15011 | !ps->sps_list[pps->sps_id]) { |
737 | ✗ | av_log(avctx, AV_LOG_ERROR, "sps_id %u out of range\n", pps->sps_id); | |
738 | ✗ | ret = AVERROR_INVALIDDATA; | |
739 | ✗ | goto fail; | |
740 | } | ||
741 | 15011 | pps->sps_ref = av_buffer_ref(ps->sps_list[pps->sps_id]); | |
742 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 15011 times.
|
15011 | if (!pps->sps_ref) { |
743 | ✗ | ret = AVERROR(ENOMEM); | |
744 | ✗ | goto fail; | |
745 | } | ||
746 | 15011 | pps->sps = (const SPS*)pps->sps_ref->data; | |
747 | 15011 | sps = pps->sps; | |
748 | |||
749 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 15011 times.
|
15011 | if (sps->bit_depth_luma > 14) { |
750 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
751 | "Invalid luma bit depth=%d\n", | ||
752 | ✗ | sps->bit_depth_luma); | |
753 | ✗ | ret = AVERROR_INVALIDDATA; | |
754 | ✗ | goto fail; | |
755 |
2/4✓ Branch 0 taken 15011 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 15011 times.
|
15011 | } else if (sps->bit_depth_luma == 11 || sps->bit_depth_luma == 13) { |
756 | ✗ | avpriv_report_missing_feature(avctx, | |
757 | "Unimplemented luma bit depth=%d", | ||
758 | ✗ | sps->bit_depth_luma); | |
759 | ✗ | ret = AVERROR_PATCHWELCOME; | |
760 | ✗ | goto fail; | |
761 | } | ||
762 | |||
763 | 15011 | pps->cabac = get_bits1(gb); | |
764 | 15011 | pps->pic_order_present = get_bits1(gb); | |
765 | 15011 | pps->slice_group_count = get_ue_golomb(gb) + 1; | |
766 |
2/2✓ Branch 0 taken 14 times.
✓ Branch 1 taken 14997 times.
|
15011 | if (pps->slice_group_count > 1) { |
767 | 14 | pps->mb_slice_group_map_type = get_ue_golomb(gb); | |
768 | 14 | avpriv_report_missing_feature(avctx, "FMO"); | |
769 | 14 | ret = AVERROR_PATCHWELCOME; | |
770 | 14 | goto fail; | |
771 | } | ||
772 | 14997 | pps->ref_count[0] = get_ue_golomb(gb) + 1; | |
773 | 14997 | pps->ref_count[1] = get_ue_golomb(gb) + 1; | |
774 |
2/4✓ Branch 0 taken 14997 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 14997 times.
|
14997 | if (pps->ref_count[0] - 1 > 32 - 1 || pps->ref_count[1] - 1 > 32 - 1) { |
775 | ✗ | av_log(avctx, AV_LOG_ERROR, "reference overflow (pps)\n"); | |
776 | ✗ | ret = AVERROR_INVALIDDATA; | |
777 | ✗ | goto fail; | |
778 | } | ||
779 | |||
780 | 14997 | qp_bd_offset = 6 * (sps->bit_depth_luma - 8); | |
781 | |||
782 | 14997 | pps->weighted_pred = get_bits1(gb); | |
783 | 14997 | pps->weighted_bipred_idc = get_bits(gb, 2); | |
784 | 14997 | pps->init_qp = get_se_golomb(gb) + 26U + qp_bd_offset; | |
785 | 14997 | pps->init_qs = get_se_golomb(gb) + 26U + qp_bd_offset; | |
786 | 14997 | pps->chroma_qp_index_offset[0] = get_se_golomb(gb); | |
787 |
2/4✓ Branch 0 taken 14997 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 14997 times.
|
14997 | if (pps->chroma_qp_index_offset[0] < -12 || pps->chroma_qp_index_offset[0] > 12) { |
788 | ✗ | ret = AVERROR_INVALIDDATA; | |
789 | ✗ | goto fail; | |
790 | } | ||
791 | |||
792 | 14997 | pps->deblocking_filter_parameters_present = get_bits1(gb); | |
793 | 14997 | pps->constrained_intra_pred = get_bits1(gb); | |
794 | 14997 | pps->redundant_pic_cnt_present = get_bits1(gb); | |
795 | |||
796 | 14997 | pps->transform_8x8_mode = 0; | |
797 | 14997 | memcpy(pps->scaling_matrix4, sps->scaling_matrix4, | |
798 | sizeof(pps->scaling_matrix4)); | ||
799 | 14997 | memcpy(pps->scaling_matrix8, sps->scaling_matrix8, | |
800 | sizeof(pps->scaling_matrix8)); | ||
801 | |||
802 | 14997 | bits_left = bit_length - get_bits_count(gb); | |
803 |
3/4✓ Branch 0 taken 718 times.
✓ Branch 1 taken 14279 times.
✓ Branch 3 taken 718 times.
✗ Branch 4 not taken.
|
14997 | if (bits_left > 0 && more_rbsp_data_in_pps(sps, avctx)) { |
804 | 718 | pps->transform_8x8_mode = get_bits1(gb); | |
805 | 718 | ret = decode_scaling_matrices(gb, sps, pps, 0, | |
806 | 718 | pps->scaling_matrix4, pps->scaling_matrix8); | |
807 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 718 times.
|
718 | if (ret < 0) |
808 | ✗ | goto fail; | |
809 | // second_chroma_qp_index_offset | ||
810 | 718 | pps->chroma_qp_index_offset[1] = get_se_golomb(gb); | |
811 |
2/4✓ Branch 0 taken 718 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 718 times.
|
718 | if (pps->chroma_qp_index_offset[1] < -12 || pps->chroma_qp_index_offset[1] > 12) { |
812 | ✗ | ret = AVERROR_INVALIDDATA; | |
813 | ✗ | goto fail; | |
814 | } | ||
815 | } else { | ||
816 | 14279 | pps->chroma_qp_index_offset[1] = pps->chroma_qp_index_offset[0]; | |
817 | } | ||
818 | |||
819 | 14997 | build_qp_table(pps, 0, pps->chroma_qp_index_offset[0], | |
820 | 14997 | sps->bit_depth_luma); | |
821 | 14997 | build_qp_table(pps, 1, pps->chroma_qp_index_offset[1], | |
822 | 14997 | sps->bit_depth_luma); | |
823 | |||
824 | 14997 | init_dequant_tables(pps, sps); | |
825 | |||
826 |
2/2✓ Branch 0 taken 76 times.
✓ Branch 1 taken 14921 times.
|
14997 | if (pps->chroma_qp_index_offset[0] != pps->chroma_qp_index_offset[1]) |
827 | 76 | pps->chroma_qp_diff = 1; | |
828 | |||
829 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 14997 times.
|
14997 | if (avctx->debug & FF_DEBUG_PICT_INFO) { |
830 | ✗ | av_log(avctx, AV_LOG_DEBUG, | |
831 | "pps:%u sps:%u %s slice_groups:%d ref:%u/%u %s qp:%d/%d/%d/%d %s %s %s %s\n", | ||
832 | ✗ | pps_id, pps->sps_id, | |
833 | ✗ | pps->cabac ? "CABAC" : "CAVLC", | |
834 | ✗ | pps->slice_group_count, | |
835 | ✗ | pps->ref_count[0], pps->ref_count[1], | |
836 | ✗ | pps->weighted_pred ? "weighted" : "", | |
837 | ✗ | pps->init_qp, pps->init_qs, pps->chroma_qp_index_offset[0], pps->chroma_qp_index_offset[1], | |
838 | ✗ | pps->deblocking_filter_parameters_present ? "LPAR" : "", | |
839 | ✗ | pps->constrained_intra_pred ? "CONSTR" : "", | |
840 | ✗ | pps->redundant_pic_cnt_present ? "REDU" : "", | |
841 | ✗ | pps->transform_8x8_mode ? "8x8DCT" : ""); | |
842 | } | ||
843 | |||
844 | 14997 | remove_pps(ps, pps_id); | |
845 | 14997 | ps->pps_list[pps_id] = pps_buf; | |
846 | |||
847 | 14997 | return 0; | |
848 | |||
849 | 14 | fail: | |
850 | 14 | av_buffer_unref(&pps_buf); | |
851 | 14 | return ret; | |
852 | } | ||
853 |