blob: d0d019837235be0d22729098f79a95fa67973216
1 | #include <linux/kernel.h> |
2 | #include <linux/types.h> |
3 | #include <linux/vmalloc.h> |
4 | #include <linux/mm.h> |
5 | #include <linux/string.h> |
6 | |
7 | #include "aml_h264_parser.h" |
8 | #include "../utils/get_bits.h" |
9 | #include "../utils/put_bits.h" |
10 | #include "../utils/golomb.h" |
11 | #include "../utils/common.h" |
12 | #include "utils.h" |
13 | |
14 | #define MAX_DELAYED_PIC_COUNT (16) |
15 | #define MAX_LOG2_MAX_FRAME_NUM (12 + 4) |
16 | #define MIN_LOG2_MAX_FRAME_NUM (4) |
17 | #define MAX_SPS_COUNT (32) |
18 | #define EXTENDED_SAR (255) |
19 | |
20 | static const struct rational h264_pixel_aspect[17] = { |
21 | { 0, 1 }, |
22 | { 1, 1 }, |
23 | { 12, 11 }, |
24 | { 10, 11 }, |
25 | { 16, 11 }, |
26 | { 40, 33 }, |
27 | { 24, 11 }, |
28 | { 20, 11 }, |
29 | { 32, 11 }, |
30 | { 80, 33 }, |
31 | { 18, 11 }, |
32 | { 15, 11 }, |
33 | { 64, 33 }, |
34 | { 160, 99 }, |
35 | { 4, 3 }, |
36 | { 3, 2 }, |
37 | { 2, 1 }, |
38 | }; |
39 | |
40 | /* maximum number of MBs in the DPB for a given level */ |
41 | static const int level_max_dpb_mbs[][2] = { |
42 | { 10, 396 }, |
43 | { 11, 900 }, |
44 | { 12, 2376 }, |
45 | { 13, 2376 }, |
46 | { 20, 2376 }, |
47 | { 21, 4752 }, |
48 | { 22, 8100 }, |
49 | { 30, 8100 }, |
50 | { 31, 18000 }, |
51 | { 32, 20480 }, |
52 | { 40, 32768 }, |
53 | { 41, 32768 }, |
54 | { 42, 34816 }, |
55 | { 50, 110400 }, |
56 | { 51, 184320 }, |
57 | { 52, 184320 }, |
58 | }; |
59 | |
60 | static const u8 default_scaling4[2][16] = { |
61 | { 6, 13, 20, 28, 13, 20, 28, 32, |
62 | 20, 28, 32, 37, 28, 32, 37, 42}, |
63 | { 10, 14, 20, 24, 14, 20, 24, 27, |
64 | 20, 24, 27, 30, 24, 27, 30, 34 } |
65 | }; |
66 | |
67 | static const u8 default_scaling8[2][64] = { |
68 | { 6, 10, 13, 16, 18, 23, 25, 27, |
69 | 10, 11, 16, 18, 23, 25, 27, 29, |
70 | 13, 16, 18, 23, 25, 27, 29, 31, |
71 | 16, 18, 23, 25, 27, 29, 31, 33, |
72 | 18, 23, 25, 27, 29, 31, 33, 36, |
73 | 23, 25, 27, 29, 31, 33, 36, 38, |
74 | 25, 27, 29, 31, 33, 36, 38, 40, |
75 | 27, 29, 31, 33, 36, 38, 40, 42 }, |
76 | { 9, 13, 15, 17, 19, 21, 22, 24, |
77 | 13, 13, 17, 19, 21, 22, 24, 25, |
78 | 15, 17, 19, 21, 22, 24, 25, 27, |
79 | 17, 19, 21, 22, 24, 25, 27, 28, |
80 | 19, 21, 22, 24, 25, 27, 28, 30, |
81 | 21, 22, 24, 25, 27, 28, 30, 32, |
82 | 22, 24, 25, 27, 28, 30, 32, 33, |
83 | 24, 25, 27, 28, 30, 32, 33, 35 } |
84 | }; |
85 | |
86 | extern const u8 ff_zigzag_scan[16 + 1]; |
87 | extern const u8 ff_zigzag_direct[64]; |
88 | |
89 | static int decode_scaling_list(struct get_bits_context *gb, |
90 | u8 *factors, int size, |
91 | const u8 *jvt_list, |
92 | const u8 *fallback_list) |
93 | { |
94 | int i, last = 8, next = 8; |
95 | const u8 *scan = size == 16 ? ff_zigzag_scan : ff_zigzag_direct; |
96 | |
97 | if (!get_bits1(gb)) /* matrix not written, we use the predicted one */ |
98 | memcpy(factors, fallback_list, size * sizeof(u8)); |
99 | else |
100 | for (i = 0; i < size; i++) { |
101 | if (next) { |
102 | int v = get_se_golomb(gb); |
103 | /*if (v < -128 || v > 127) { //JM19 has not check. |
104 | pr_err( "delta scale %d is invalid\n", v); |
105 | return -1; |
106 | }*/ |
107 | next = (last + v) & 0xff; |
108 | } |
109 | if (!i && !next) { /* matrix not written, we use the preset one */ |
110 | memcpy(factors, jvt_list, size * sizeof(u8)); |
111 | break; |
112 | } |
113 | last = factors[scan[i]] = next ? next : last; |
114 | } |
115 | return 0; |
116 | } |
117 | |
118 | /* returns non zero if the provided SPS scaling matrix has been filled */ |
119 | static int decode_scaling_matrices(struct get_bits_context *gb, |
120 | const struct h264_SPS_t *sps, |
121 | const struct h264_PPS_t *pps, int is_sps, |
122 | u8(*scaling_matrix4)[16], |
123 | u8(*scaling_matrix8)[64]) |
124 | { |
125 | int ret = 0; |
126 | int fallback_sps = !is_sps && sps->scaling_matrix_present; |
127 | const u8 *fallback[4] = { |
128 | fallback_sps ? sps->scaling_matrix4[0] : default_scaling4[0], |
129 | fallback_sps ? sps->scaling_matrix4[3] : default_scaling4[1], |
130 | fallback_sps ? sps->scaling_matrix8[0] : default_scaling8[0], |
131 | fallback_sps ? sps->scaling_matrix8[3] : default_scaling8[1] |
132 | }; |
133 | |
134 | if (get_bits1(gb)) { |
135 | ret |= decode_scaling_list(gb, scaling_matrix4[0], 16, default_scaling4[0], fallback[0]); // Intra, Y |
136 | ret |= decode_scaling_list(gb, scaling_matrix4[1], 16, default_scaling4[0], scaling_matrix4[0]); // Intra, Cr |
137 | ret |= decode_scaling_list(gb, scaling_matrix4[2], 16, default_scaling4[0], scaling_matrix4[1]); // Intra, Cb |
138 | ret |= decode_scaling_list(gb, scaling_matrix4[3], 16, default_scaling4[1], fallback[1]); // Inter, Y |
139 | ret |= decode_scaling_list(gb, scaling_matrix4[4], 16, default_scaling4[1], scaling_matrix4[3]); // Inter, Cr |
140 | ret |= decode_scaling_list(gb, scaling_matrix4[5], 16, default_scaling4[1], scaling_matrix4[4]); // Inter, Cb |
141 | if (is_sps || pps->transform_8x8_mode) { |
142 | ret |= decode_scaling_list(gb, scaling_matrix8[0], 64, default_scaling8[0], fallback[2]); // Intra, Y |
143 | ret |= decode_scaling_list(gb, scaling_matrix8[3], 64, default_scaling8[1], fallback[3]); // Inter, Y |
144 | if (sps->chroma_format_idc == 3) { |
145 | ret |= decode_scaling_list(gb, scaling_matrix8[1], 64, default_scaling8[0], scaling_matrix8[0]); // Intra, Cr |
146 | ret |= decode_scaling_list(gb, scaling_matrix8[4], 64, default_scaling8[1], scaling_matrix8[3]); // Inter, Cr |
147 | ret |= decode_scaling_list(gb, scaling_matrix8[2], 64, default_scaling8[0], scaling_matrix8[1]); // Intra, Cb |
148 | ret |= decode_scaling_list(gb, scaling_matrix8[5], 64, default_scaling8[1], scaling_matrix8[4]); // Inter, Cb |
149 | } |
150 | } |
151 | if (!ret) |
152 | ret = is_sps; |
153 | } |
154 | |
155 | return ret; |
156 | } |
157 | |
158 | static int decode_hrd_parameters(struct get_bits_context *gb, |
159 | struct h264_SPS_t *sps) |
160 | { |
161 | int cpb_count, i; |
162 | |
163 | cpb_count = get_ue_golomb_31(gb) + 1; |
164 | if (cpb_count > 32U) { |
165 | v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, |
166 | "cpb_count %d invalid\n", cpb_count); |
167 | return -1; |
168 | } |
169 | |
170 | get_bits(gb, 4); /* bit_rate_scale */ |
171 | get_bits(gb, 4); /* cpb_size_scale */ |
172 | for (i = 0; i < cpb_count; i++) { |
173 | get_ue_golomb_long(gb); /* bit_rate_value_minus1 */ |
174 | get_ue_golomb_long(gb); /* cpb_size_value_minus1 */ |
175 | get_bits1(gb); /* cbr_flag */ |
176 | } |
177 | |
178 | sps->initial_cpb_removal_delay_length = get_bits(gb, 5) + 1; |
179 | sps->cpb_removal_delay_length = get_bits(gb, 5) + 1; |
180 | sps->dpb_output_delay_length = get_bits(gb, 5) + 1; |
181 | sps->time_offset_length = get_bits(gb, 5); |
182 | sps->cpb_cnt = cpb_count; |
183 | |
184 | return 0; |
185 | } |
186 | |
187 | static int decode_vui_parameters(struct get_bits_context *gb, struct h264_SPS_t *sps) |
188 | { |
189 | int aspect_ratio_info_present_flag; |
190 | u32 aspect_ratio_idc; |
191 | |
192 | aspect_ratio_info_present_flag = get_bits1(gb); |
193 | |
194 | if (aspect_ratio_info_present_flag) { |
195 | aspect_ratio_idc = get_bits(gb, 8); |
196 | if (aspect_ratio_idc == EXTENDED_SAR) { |
197 | sps->sar.num = get_bits(gb, 16); |
198 | sps->sar.den = get_bits(gb, 16); |
199 | } else if (aspect_ratio_idc < ARRAY_SIZE(h264_pixel_aspect)) { |
200 | sps->sar = h264_pixel_aspect[aspect_ratio_idc]; |
201 | } else { |
202 | return -1; |
203 | } |
204 | } else { |
205 | sps->sar.num = |
206 | sps->sar.den = 0; |
207 | } |
208 | |
209 | if (get_bits1(gb)) /* overscan_info_present_flag */ |
210 | get_bits1(gb); /* overscan_appropriate_flag */ |
211 | |
212 | sps->video_signal_type_present_flag = get_bits1(gb); |
213 | if (sps->video_signal_type_present_flag) { |
214 | get_bits(gb, 3); /* video_format */ |
215 | sps->full_range = get_bits1(gb); /* video_full_range_flag */ |
216 | |
217 | sps->colour_description_present_flag = get_bits1(gb); |
218 | if (sps->colour_description_present_flag) { |
219 | sps->color_primaries = get_bits(gb, 8); /* colour_primaries */ |
220 | sps->color_trc = get_bits(gb, 8); /* transfer_characteristics */ |
221 | sps->colorspace = get_bits(gb, 8); /* matrix_coefficients */ |
222 | |
223 | // Set invalid values to "unspecified" |
224 | if (!av_color_primaries_name(sps->color_primaries)) |
225 | sps->color_primaries = AVCOL_PRI_UNSPECIFIED; |
226 | if (!av_color_transfer_name(sps->color_trc)) |
227 | sps->color_trc = AVCOL_TRC_UNSPECIFIED; |
228 | if (!av_color_space_name(sps->colorspace)) |
229 | sps->colorspace = AVCOL_SPC_UNSPECIFIED; |
230 | } |
231 | } |
232 | |
233 | /* chroma_location_info_present_flag */ |
234 | if (get_bits1(gb)) { |
235 | /* chroma_sample_location_type_top_field */ |
236 | //avctx->chroma_sample_location = get_ue_golomb(gb) + 1; |
237 | get_ue_golomb(gb); /* chroma_sample_location_type_bottom_field */ |
238 | } |
239 | |
240 | if (show_bits1(gb) && get_bits_left(gb) < 10) { |
241 | v4l_dbg(0, V4L_DEBUG_CODEC_PARSER, "Truncated VUI\n"); |
242 | return 0; |
243 | } |
244 | |
245 | sps->timing_info_present_flag = get_bits1(gb); |
246 | if (sps->timing_info_present_flag) { |
247 | unsigned num_units_in_tick = get_bits_long(gb, 32); |
248 | unsigned time_scale = get_bits_long(gb, 32); |
249 | if (!num_units_in_tick || !time_scale) { |
250 | v4l_dbg(0, V4L_DEBUG_CODEC_PARSER, |
251 | "time_scale/num_units_in_tick invalid or unsupported (%u/%u)\n", |
252 | time_scale, num_units_in_tick); |
253 | sps->timing_info_present_flag = 0; |
254 | } else { |
255 | sps->num_units_in_tick = num_units_in_tick; |
256 | sps->time_scale = time_scale; |
257 | } |
258 | sps->fixed_frame_rate_flag = get_bits1(gb); |
259 | } |
260 | |
261 | sps->nal_hrd_parameters_present_flag = get_bits1(gb); |
262 | if (sps->nal_hrd_parameters_present_flag) |
263 | if (decode_hrd_parameters(gb, sps) < 0) |
264 | return -1; |
265 | sps->vcl_hrd_parameters_present_flag = get_bits1(gb); |
266 | if (sps->vcl_hrd_parameters_present_flag) |
267 | if (decode_hrd_parameters(gb, sps) < 0) |
268 | return -1; |
269 | if (sps->nal_hrd_parameters_present_flag || |
270 | sps->vcl_hrd_parameters_present_flag) |
271 | get_bits1(gb); /* low_delay_hrd_flag */ |
272 | sps->pic_struct_present_flag = get_bits1(gb); |
273 | if (!get_bits_left(gb)) |
274 | return 0; |
275 | sps->bitstream_restriction_flag = get_bits1(gb); |
276 | if (sps->bitstream_restriction_flag) { |
277 | get_bits1(gb); /* motion_vectors_over_pic_boundaries_flag */ |
278 | get_ue_golomb(gb); /* max_bytes_per_pic_denom */ |
279 | get_ue_golomb(gb); /* max_bits_per_mb_denom */ |
280 | get_ue_golomb(gb); /* log2_max_mv_length_horizontal */ |
281 | get_ue_golomb(gb); /* log2_max_mv_length_vertical */ |
282 | sps->num_reorder_frames = get_ue_golomb(gb); |
283 | sps->max_dec_frame_buffering = get_ue_golomb(gb); /*max_dec_frame_buffering*/ |
284 | |
285 | if (get_bits_left(gb) < 0) { |
286 | sps->num_reorder_frames = 0; |
287 | sps->bitstream_restriction_flag = 0; |
288 | } |
289 | |
290 | if (sps->num_reorder_frames > 16U |
291 | /* max_dec_frame_buffering || max_dec_frame_buffering > 16 */) { |
292 | v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, |
293 | "Clipping illegal num_reorder_frames %d\n", |
294 | sps->num_reorder_frames); |
295 | sps->num_reorder_frames = 16; |
296 | return -1; |
297 | } |
298 | } |
299 | |
300 | return 0; |
301 | } |
302 | |
303 | static int aml_h264_parser_sps(struct get_bits_context *gb, struct h264_SPS_t *sps) |
304 | { |
305 | int ret; |
306 | u32 sps_id; |
307 | int profile_idc, level_idc, constraint_set_flags = 0; |
308 | int i, log2_max_frame_num_minus4; |
309 | |
310 | profile_idc = get_bits(gb, 8); |
311 | constraint_set_flags |= get_bits1(gb) << 0; // constraint_set0_flag |
312 | constraint_set_flags |= get_bits1(gb) << 1; // constraint_set1_flag |
313 | constraint_set_flags |= get_bits1(gb) << 2; // constraint_set2_flag |
314 | constraint_set_flags |= get_bits1(gb) << 3; // constraint_set3_flag |
315 | constraint_set_flags |= get_bits1(gb) << 4; // constraint_set4_flag |
316 | constraint_set_flags |= get_bits1(gb) << 5; // constraint_set5_flag |
317 | skip_bits(gb, 2); // reserved_zero_2bits |
318 | level_idc = get_bits(gb, 8); |
319 | sps_id = get_ue_golomb_31(gb); |
320 | |
321 | if (sps_id >= MAX_SPS_COUNT) { |
322 | v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, |
323 | "sps_id %u out of range\n", sps_id); |
324 | goto fail; |
325 | } |
326 | |
327 | sps->sps_id = sps_id; |
328 | sps->time_offset_length = 24; |
329 | sps->profile_idc = profile_idc; |
330 | sps->constraint_set_flags = constraint_set_flags; |
331 | sps->level_idc = level_idc; |
332 | sps->full_range = -1; |
333 | |
334 | memset(sps->scaling_matrix4, 16, sizeof(sps->scaling_matrix4)); |
335 | memset(sps->scaling_matrix8, 16, sizeof(sps->scaling_matrix8)); |
336 | sps->scaling_matrix_present = 0; |
337 | sps->colorspace = 2; //AVCOL_SPC_UNSPECIFIED |
338 | |
339 | if (sps->profile_idc == 100 || // High profile |
340 | sps->profile_idc == 110 || // High10 profile |
341 | sps->profile_idc == 122 || // High422 profile |
342 | sps->profile_idc == 244 || // High444 Predictive profile |
343 | sps->profile_idc == 44 || // Cavlc444 profile |
344 | sps->profile_idc == 83 || // Scalable Constrained High profile (SVC) |
345 | sps->profile_idc == 86 || // Scalable High Intra profile (SVC) |
346 | sps->profile_idc == 118 || // Stereo High profile (MVC) |
347 | sps->profile_idc == 128 || // Multiview High profile (MVC) |
348 | sps->profile_idc == 138 || // Multiview Depth High profile (MVCD) |
349 | sps->profile_idc == 144) { // old High444 profile |
350 | sps->chroma_format_idc = get_ue_golomb_31(gb); |
351 | |
352 | if (sps->chroma_format_idc > 3U) { |
353 | v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, |
354 | "chroma_format_idc %u\n", sps->chroma_format_idc); |
355 | goto fail; |
356 | } else if (sps->chroma_format_idc == 3) { |
357 | sps->residual_color_transform_flag = get_bits1(gb); |
358 | if (sps->residual_color_transform_flag) { |
359 | v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, |
360 | "separate color planes are not supported\n"); |
361 | goto fail; |
362 | } |
363 | } |
364 | |
365 | sps->bit_depth_luma = get_ue_golomb(gb) + 8; |
366 | sps->bit_depth_chroma = get_ue_golomb(gb) + 8; |
367 | if (sps->bit_depth_chroma != sps->bit_depth_luma) { |
368 | v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, |
369 | "Different chroma and luma bit depth\n"); |
370 | goto fail; |
371 | } |
372 | |
373 | if (sps->bit_depth_luma < 8 || sps->bit_depth_luma > 14 || |
374 | sps->bit_depth_chroma < 8 || sps->bit_depth_chroma > 14) { |
375 | v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, |
376 | "illegal bit depth value (%d, %d)\n", |
377 | sps->bit_depth_luma, sps->bit_depth_chroma); |
378 | goto fail; |
379 | } |
380 | |
381 | sps->transform_bypass = get_bits1(gb); |
382 | ret = decode_scaling_matrices(gb, sps, NULL, 1, |
383 | sps->scaling_matrix4, sps->scaling_matrix8); |
384 | if (ret < 0) |
385 | goto fail; |
386 | sps->scaling_matrix_present |= ret; |
387 | } else { |
388 | sps->chroma_format_idc = 1; |
389 | sps->bit_depth_luma = 8; |
390 | sps->bit_depth_chroma = 8; |
391 | } |
392 | |
393 | log2_max_frame_num_minus4 = get_ue_golomb(gb); |
394 | if (log2_max_frame_num_minus4 < MIN_LOG2_MAX_FRAME_NUM - 4 || |
395 | log2_max_frame_num_minus4 > MAX_LOG2_MAX_FRAME_NUM - 4) { |
396 | v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, |
397 | "log2_max_frame_num_minus4 out of range (0-12): %d\n", |
398 | log2_max_frame_num_minus4); |
399 | goto fail; |
400 | } |
401 | sps->log2_max_frame_num = log2_max_frame_num_minus4 + 4; |
402 | |
403 | sps->poc_type = get_ue_golomb_31(gb); |
404 | if (sps->poc_type == 0) { // FIXME #define |
405 | u32 t = get_ue_golomb(gb); |
406 | if (t > 12) { |
407 | v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, |
408 | "log2_max_poc_lsb (%d) is out of range\n", t); |
409 | goto fail; |
410 | } |
411 | sps->log2_max_poc_lsb = t + 4; |
412 | } else if (sps->poc_type == 1) { // FIXME #define |
413 | sps->delta_pic_order_always_zero_flag = get_bits1(gb); |
414 | sps->offset_for_non_ref_pic = get_se_golomb_long(gb); |
415 | sps->offset_for_top_to_bottom_field = get_se_golomb_long(gb); |
416 | |
417 | sps->poc_cycle_length = get_ue_golomb(gb); |
418 | if ((u32)sps->poc_cycle_length >= ARRAY_SIZE(sps->offset_for_ref_frame)) { |
419 | v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, |
420 | "poc_cycle_length overflow %d\n", sps->poc_cycle_length); |
421 | goto fail; |
422 | } |
423 | |
424 | for (i = 0; i < sps->poc_cycle_length; i++) |
425 | sps->offset_for_ref_frame[i] = get_se_golomb_long(gb); |
426 | } else if (sps->poc_type != 2) { |
427 | v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, |
428 | "illegal POC type %d\n", sps->poc_type); |
429 | goto fail; |
430 | } |
431 | |
432 | sps->ref_frame_count = get_ue_golomb_31(gb); |
433 | if (sps->ref_frame_count > MAX_DELAYED_PIC_COUNT) { |
434 | v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, |
435 | "too many reference frames %d\n", sps->ref_frame_count); |
436 | goto fail; |
437 | } |
438 | sps->gaps_in_frame_num_allowed_flag = get_bits1(gb); |
439 | sps->mb_width = get_ue_golomb(gb) + 1; |
440 | sps->mb_height = get_ue_golomb(gb) + 1; |
441 | |
442 | sps->frame_mbs_only_flag = get_bits1(gb); |
443 | |
444 | if (sps->mb_height >= INT_MAX / 2U) { |
445 | v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "height overflow\n"); |
446 | goto fail; |
447 | } |
448 | sps->mb_height *= 2 - sps->frame_mbs_only_flag; |
449 | |
450 | if (!sps->frame_mbs_only_flag) |
451 | sps->mb_aff = get_bits1(gb); |
452 | else |
453 | sps->mb_aff = 0; |
454 | |
455 | if ((u32)sps->mb_width >= INT_MAX / 16 || |
456 | (u32)sps->mb_height >= INT_MAX / 16) { |
457 | v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, |
458 | "mb_width/height overflow\n"); |
459 | goto fail; |
460 | } |
461 | |
462 | sps->direct_8x8_inference_flag = get_bits1(gb); |
463 | |
464 | sps->crop = get_bits1(gb); |
465 | if (sps->crop) { |
466 | u32 crop_left = get_ue_golomb(gb); |
467 | u32 crop_right = get_ue_golomb(gb); |
468 | u32 crop_top = get_ue_golomb(gb); |
469 | u32 crop_bottom = get_ue_golomb(gb); |
470 | int width = 16 * sps->mb_width; |
471 | int height = 16 * sps->mb_height; |
472 | int vsub = (sps->chroma_format_idc == 1) ? 1 : 0; |
473 | int hsub = (sps->chroma_format_idc == 1 || sps->chroma_format_idc == 2) ? 1 : 0; |
474 | int step_x = 1 << hsub; |
475 | int step_y = (2 - sps->frame_mbs_only_flag) << vsub; |
476 | |
477 | if (crop_left > (u32)INT_MAX / 4 / step_x || |
478 | crop_right > (u32)INT_MAX / 4 / step_x || |
479 | crop_top > (u32)INT_MAX / 4 / step_y || |
480 | crop_bottom > (u32)INT_MAX / 4 / step_y || |
481 | (crop_left + crop_right ) * step_x >= width || |
482 | (crop_top + crop_bottom) * step_y >= height) { |
483 | v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, |
484 | "crop values invalid %u %u %u %u / %d %d\n", |
485 | crop_left, crop_right, crop_top, crop_bottom, width, height); |
486 | goto fail; |
487 | } |
488 | |
489 | sps->crop_left = crop_left * step_x; |
490 | sps->crop_right = crop_right * step_x; |
491 | sps->crop_top = crop_top * step_y; |
492 | sps->crop_bottom = crop_bottom * step_y; |
493 | } else { |
494 | sps->crop_left = |
495 | sps->crop_right = |
496 | sps->crop_top = |
497 | sps->crop_bottom = |
498 | sps->crop = 0; |
499 | } |
500 | |
501 | sps->vui_parameters_present_flag = get_bits1(gb); |
502 | if (sps->vui_parameters_present_flag) { |
503 | int ret = decode_vui_parameters(gb, sps); |
504 | if (ret < 0) |
505 | goto fail; |
506 | } |
507 | |
508 | if (get_bits_left(gb) < 0) { |
509 | v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, |
510 | "Overread %s by %d bits\n", |
511 | sps->vui_parameters_present_flag ? "VUI" : "SPS", -get_bits_left(gb)); |
512 | /*goto out;*/ |
513 | } |
514 | |
515 | #if 0 |
516 | /* if the maximum delay is not stored in the SPS, derive it based on the level */ |
517 | if (!sps->bitstream_restriction_flag && sps->ref_frame_count) { |
518 | sps->num_reorder_frames = MAX_DELAYED_PIC_COUNT - 1; |
519 | for (i = 0; i < ARRAY_SIZE(level_max_dpb_mbs); i++) { |
520 | if (level_max_dpb_mbs[i][0] == sps->level_idc) { |
521 | sps->num_reorder_frames = |
522 | MIN(level_max_dpb_mbs[i][1] / (sps->mb_width * sps->mb_height), |
523 | sps->num_reorder_frames); |
524 | break; |
525 | } |
526 | } |
527 | } |
528 | #endif |
529 | |
530 | sps->num_reorder_frames = MAX_DELAYED_PIC_COUNT - 1; |
531 | for (i = 0; i < ARRAY_SIZE(level_max_dpb_mbs); i++) { |
532 | if (level_max_dpb_mbs[i][0] == sps->level_idc) { |
533 | sps->num_reorder_frames = |
534 | MIN(level_max_dpb_mbs[i][1] / (sps->mb_width * sps->mb_height), |
535 | sps->num_reorder_frames); |
536 | sps->num_reorder_frames += 1; |
537 | if (sps->max_dec_frame_buffering > sps->num_reorder_frames) |
538 | sps->num_reorder_frames = sps->max_dec_frame_buffering; |
539 | break; |
540 | } |
541 | } |
542 | |
543 | if ((sps->bitstream_restriction_flag) && |
544 | (sps->max_dec_frame_buffering < |
545 | sps->num_reorder_frames)) { |
546 | sps->num_reorder_frames = sps->max_dec_frame_buffering; |
547 | v4l_dbg(0, V4L_DEBUG_CODEC_PARSER, |
548 | "set reorder_pic_num to %d\n", |
549 | sps->num_reorder_frames); |
550 | } |
551 | |
552 | if (!sps->sar.den) |
553 | sps->sar.den = 1; |
554 | /*out:*/ |
555 | if (1) { |
556 | static const char csp[4][5] = { "Gray", "420", "422", "444" }; |
557 | v4l_dbg(0, V4L_DEBUG_CODEC_PARSER, |
558 | "sps:%u profile:%d/%d poc:%d ref:%d %dx%d %s %s crop:%u/%u/%u/%u %s %s %d/%d b%d reo:%d\n", |
559 | sps_id, sps->profile_idc, sps->level_idc, |
560 | sps->poc_type, |
561 | sps->ref_frame_count, |
562 | sps->mb_width, sps->mb_height, |
563 | sps->frame_mbs_only_flag ? "FRM" : (sps->mb_aff ? "MB-AFF" : "PIC-AFF"), |
564 | sps->direct_8x8_inference_flag ? "8B8" : "", |
565 | sps->crop_left, sps->crop_right, |
566 | sps->crop_top, sps->crop_bottom, |
567 | sps->vui_parameters_present_flag ? "VUI" : "", |
568 | csp[sps->chroma_format_idc], |
569 | sps->timing_info_present_flag ? sps->num_units_in_tick : 0, |
570 | sps->timing_info_present_flag ? sps->time_scale : 0, |
571 | sps->bit_depth_luma, |
572 | sps->bitstream_restriction_flag ? sps->num_reorder_frames : -1); |
573 | } |
574 | |
575 | return 0; |
576 | |
577 | fail: |
578 | return -1; |
579 | } |
580 | |
581 | static const char *h264_nal_type_name[32] = { |
582 | "Unspecified 0", //H264_NAL_UNSPECIFIED |
583 | "Coded slice of a non-IDR picture", // H264_NAL_SLICE |
584 | "Coded slice data partition A", // H264_NAL_DPA |
585 | "Coded slice data partition B", // H264_NAL_DPB |
586 | "Coded slice data partition C", // H264_NAL_DPC |
587 | "IDR", // H264_NAL_IDR_SLICE |
588 | "SEI", // H264_NAL_SEI |
589 | "SPS", // H264_NAL_SPS |
590 | "PPS", // H264_NAL_PPS |
591 | "AUD", // H264_NAL_AUD |
592 | "End of sequence", // H264_NAL_END_SEQUENCE |
593 | "End of stream", // H264_NAL_END_STREAM |
594 | "Filler data", // H264_NAL_FILLER_DATA |
595 | "SPS extension", // H264_NAL_SPS_EXT |
596 | "Prefix", // H264_NAL_PREFIX |
597 | "Subset SPS", // H264_NAL_SUB_SPS |
598 | "Depth parameter set", // H264_NAL_DPS |
599 | "Reserved 17", // H264_NAL_RESERVED17 |
600 | "Reserved 18", // H264_NAL_RESERVED18 |
601 | "Auxiliary coded picture without partitioning", // H264_NAL_AUXILIARY_SLICE |
602 | "Slice extension", // H264_NAL_EXTEN_SLICE |
603 | "Slice extension for a depth view or a 3D-AVC texture view", // H264_NAL_DEPTH_EXTEN_SLICE |
604 | "Reserved 22", // H264_NAL_RESERVED22 |
605 | "Reserved 23", // H264_NAL_RESERVED23 |
606 | "Unspecified 24", // H264_NAL_UNSPECIFIED24 |
607 | "Unspecified 25", // H264_NAL_UNSPECIFIED25 |
608 | "Unspecified 26", // H264_NAL_UNSPECIFIED26 |
609 | "Unspecified 27", // H264_NAL_UNSPECIFIED27 |
610 | "Unspecified 28", // H264_NAL_UNSPECIFIED28 |
611 | "Unspecified 29", // H264_NAL_UNSPECIFIED29 |
612 | "Unspecified 30", // H264_NAL_UNSPECIFIED30 |
613 | "Unspecified 31", // H264_NAL_UNSPECIFIED31 |
614 | }; |
615 | |
616 | static const char *h264_nal_unit_name(int nal_type) |
617 | { |
618 | return h264_nal_type_name[nal_type]; |
619 | } |
620 | |
621 | static int decode_extradata_ps(u8 *data, int size, struct h264_param_sets *ps) |
622 | { |
623 | int ret = 0; |
624 | struct get_bits_context gb; |
625 | u32 src_len, rbsp_size = 0; |
626 | u8 *rbsp_buf = NULL; |
627 | int ref_idc, nalu_pos; |
628 | u32 nal_type; |
629 | u8 *p = data; |
630 | u32 len = size; |
631 | |
632 | nalu_pos = find_start_code(p, len); |
633 | if (nalu_pos < 0) |
634 | return -1; |
635 | |
636 | src_len = calc_nal_len(p + nalu_pos, size - nalu_pos); |
637 | rbsp_buf = nal_unit_extract_rbsp(p + nalu_pos, src_len, &rbsp_size); |
638 | if (rbsp_buf == NULL) |
639 | return -ENOMEM; |
640 | |
641 | ret = init_get_bits8(&gb, rbsp_buf, rbsp_size); |
642 | if (ret < 0) |
643 | goto out; |
644 | |
645 | if (get_bits1(&gb) != 0) { |
646 | ret = -1; |
647 | v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, |
648 | "invalid h264 data,return!\n"); |
649 | goto out; |
650 | } |
651 | |
652 | ref_idc = get_bits(&gb, 2); |
653 | nal_type = get_bits(&gb, 5); |
654 | |
655 | v4l_dbg(0, V4L_DEBUG_CODEC_PARSER, |
656 | "nal_unit_type: %d(%s), nal_ref_idc: %d\n", |
657 | nal_type, h264_nal_unit_name(nal_type), ref_idc); |
658 | |
659 | switch (nal_type) { |
660 | case H264_NAL_SPS: |
661 | ret = aml_h264_parser_sps(&gb, &ps->sps); |
662 | if (ret < 0) |
663 | goto out; |
664 | ps->sps_parsed = true; |
665 | break; |
666 | /*case H264_NAL_PPS: |
667 | ret = ff_h264_decode_picture_parameter_set(&gb, &ps->pps, rbsp_size); |
668 | if (ret < 0) |
669 | goto fail; |
670 | ps->pps_parsed = true; |
671 | break;*/ |
672 | default: |
673 | v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, |
674 | "Unsupport parser nal type (%s).\n", |
675 | h264_nal_unit_name(nal_type)); |
676 | break; |
677 | } |
678 | |
679 | out: |
680 | vfree(rbsp_buf); |
681 | |
682 | return ret; |
683 | } |
684 | |
685 | int h264_decode_extradata_ps(u8 *buf, int size, struct h264_param_sets *ps) |
686 | { |
687 | int ret = 0, i = 0, j = 0; |
688 | u8 *p = buf; |
689 | int len = size; |
690 | |
691 | for (i = 4; i < size; i++) { |
692 | j = find_start_code(p, len); |
693 | if (j > 0) { |
694 | len = size - (p - buf); |
695 | ret = decode_extradata_ps(p, len, ps); |
696 | if (ret) { |
697 | v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, |
698 | "parse extra data failed. err: %d\n", ret); |
699 | return ret; |
700 | } |
701 | |
702 | if (ps->sps_parsed) |
703 | break; |
704 | |
705 | p += j; |
706 | } |
707 | p++; |
708 | } |
709 | |
710 | return ret; |
711 | } |
712 | |
713 | |
714 |