blob: 8963afd845445ce8c109f4f24926a8eb9cdf904d
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 | pr_err("cpb_count %d invalid\n", cpb_count); |
166 | return -1; |
167 | } |
168 | |
169 | get_bits(gb, 4); /* bit_rate_scale */ |
170 | get_bits(gb, 4); /* cpb_size_scale */ |
171 | for (i = 0; i < cpb_count; i++) { |
172 | get_ue_golomb_long(gb); /* bit_rate_value_minus1 */ |
173 | get_ue_golomb_long(gb); /* cpb_size_value_minus1 */ |
174 | get_bits1(gb); /* cbr_flag */ |
175 | } |
176 | |
177 | sps->initial_cpb_removal_delay_length = get_bits(gb, 5) + 1; |
178 | sps->cpb_removal_delay_length = get_bits(gb, 5) + 1; |
179 | sps->dpb_output_delay_length = get_bits(gb, 5) + 1; |
180 | sps->time_offset_length = get_bits(gb, 5); |
181 | sps->cpb_cnt = cpb_count; |
182 | |
183 | return 0; |
184 | } |
185 | |
186 | static int decode_vui_parameters(struct get_bits_context *gb, struct h264_SPS_t *sps) |
187 | { |
188 | int aspect_ratio_info_present_flag; |
189 | u32 aspect_ratio_idc; |
190 | |
191 | aspect_ratio_info_present_flag = get_bits1(gb); |
192 | |
193 | if (aspect_ratio_info_present_flag) { |
194 | aspect_ratio_idc = get_bits(gb, 8); |
195 | if (aspect_ratio_idc == EXTENDED_SAR) { |
196 | sps->sar.num = get_bits(gb, 16); |
197 | sps->sar.den = get_bits(gb, 16); |
198 | } else if (aspect_ratio_idc < ARRAY_SIZE(h264_pixel_aspect)) { |
199 | sps->sar = h264_pixel_aspect[aspect_ratio_idc]; |
200 | } else { |
201 | return -1; |
202 | } |
203 | } else { |
204 | sps->sar.num = |
205 | sps->sar.den = 0; |
206 | } |
207 | |
208 | if (get_bits1(gb)) /* overscan_info_present_flag */ |
209 | get_bits1(gb); /* overscan_appropriate_flag */ |
210 | |
211 | sps->video_signal_type_present_flag = get_bits1(gb); |
212 | if (sps->video_signal_type_present_flag) { |
213 | get_bits(gb, 3); /* video_format */ |
214 | sps->full_range = get_bits1(gb); /* video_full_range_flag */ |
215 | |
216 | sps->colour_description_present_flag = get_bits1(gb); |
217 | if (sps->colour_description_present_flag) { |
218 | sps->color_primaries = get_bits(gb, 8); /* colour_primaries */ |
219 | sps->color_trc = get_bits(gb, 8); /* transfer_characteristics */ |
220 | sps->colorspace = get_bits(gb, 8); /* matrix_coefficients */ |
221 | |
222 | // Set invalid values to "unspecified" |
223 | if (!av_color_primaries_name(sps->color_primaries)) |
224 | sps->color_primaries = AVCOL_PRI_UNSPECIFIED; |
225 | if (!av_color_transfer_name(sps->color_trc)) |
226 | sps->color_trc = AVCOL_TRC_UNSPECIFIED; |
227 | if (!av_color_space_name(sps->colorspace)) |
228 | sps->colorspace = AVCOL_SPC_UNSPECIFIED; |
229 | } |
230 | } |
231 | |
232 | /* chroma_location_info_present_flag */ |
233 | if (get_bits1(gb)) { |
234 | /* chroma_sample_location_type_top_field */ |
235 | //avctx->chroma_sample_location = get_ue_golomb(gb) + 1; |
236 | get_ue_golomb(gb); /* chroma_sample_location_type_bottom_field */ |
237 | } |
238 | |
239 | if (show_bits1(gb) && get_bits_left(gb) < 10) { |
240 | pr_info("Truncated VUI\n"); |
241 | return 0; |
242 | } |
243 | |
244 | sps->timing_info_present_flag = get_bits1(gb); |
245 | if (sps->timing_info_present_flag) { |
246 | unsigned num_units_in_tick = get_bits_long(gb, 32); |
247 | unsigned time_scale = get_bits_long(gb, 32); |
248 | if (!num_units_in_tick || !time_scale) { |
249 | pr_info("time_scale/num_units_in_tick invalid or unsupported (%u/%u)\n", |
250 | time_scale, num_units_in_tick); |
251 | sps->timing_info_present_flag = 0; |
252 | } else { |
253 | sps->num_units_in_tick = num_units_in_tick; |
254 | sps->time_scale = time_scale; |
255 | } |
256 | sps->fixed_frame_rate_flag = get_bits1(gb); |
257 | } |
258 | |
259 | sps->nal_hrd_parameters_present_flag = get_bits1(gb); |
260 | if (sps->nal_hrd_parameters_present_flag) |
261 | if (decode_hrd_parameters(gb, sps) < 0) |
262 | return -1; |
263 | sps->vcl_hrd_parameters_present_flag = get_bits1(gb); |
264 | if (sps->vcl_hrd_parameters_present_flag) |
265 | if (decode_hrd_parameters(gb, sps) < 0) |
266 | return -1; |
267 | if (sps->nal_hrd_parameters_present_flag || |
268 | sps->vcl_hrd_parameters_present_flag) |
269 | get_bits1(gb); /* low_delay_hrd_flag */ |
270 | sps->pic_struct_present_flag = get_bits1(gb); |
271 | if (!get_bits_left(gb)) |
272 | return 0; |
273 | sps->bitstream_restriction_flag = get_bits1(gb); |
274 | if (sps->bitstream_restriction_flag) { |
275 | get_bits1(gb); /* motion_vectors_over_pic_boundaries_flag */ |
276 | get_ue_golomb(gb); /* max_bytes_per_pic_denom */ |
277 | get_ue_golomb(gb); /* max_bits_per_mb_denom */ |
278 | get_ue_golomb(gb); /* log2_max_mv_length_horizontal */ |
279 | get_ue_golomb(gb); /* log2_max_mv_length_vertical */ |
280 | sps->num_reorder_frames = get_ue_golomb(gb); |
281 | sps->max_dec_frame_buffering = get_ue_golomb(gb); /*max_dec_frame_buffering*/ |
282 | |
283 | if (get_bits_left(gb) < 0) { |
284 | sps->num_reorder_frames = 0; |
285 | sps->bitstream_restriction_flag = 0; |
286 | } |
287 | |
288 | if (sps->num_reorder_frames > 16U |
289 | /* max_dec_frame_buffering || max_dec_frame_buffering > 16 */) { |
290 | pr_info("Clipping illegal num_reorder_frames %d\n", |
291 | sps->num_reorder_frames); |
292 | sps->num_reorder_frames = 16; |
293 | return -1; |
294 | } |
295 | } |
296 | |
297 | return 0; |
298 | } |
299 | |
300 | static int aml_h264_parser_sps(struct get_bits_context *gb, struct h264_SPS_t *sps) |
301 | { |
302 | int ret; |
303 | u32 sps_id; |
304 | int profile_idc, level_idc, constraint_set_flags = 0; |
305 | int i, log2_max_frame_num_minus4; |
306 | |
307 | profile_idc = get_bits(gb, 8); |
308 | constraint_set_flags |= get_bits1(gb) << 0; // constraint_set0_flag |
309 | constraint_set_flags |= get_bits1(gb) << 1; // constraint_set1_flag |
310 | constraint_set_flags |= get_bits1(gb) << 2; // constraint_set2_flag |
311 | constraint_set_flags |= get_bits1(gb) << 3; // constraint_set3_flag |
312 | constraint_set_flags |= get_bits1(gb) << 4; // constraint_set4_flag |
313 | constraint_set_flags |= get_bits1(gb) << 5; // constraint_set5_flag |
314 | skip_bits(gb, 2); // reserved_zero_2bits |
315 | level_idc = get_bits(gb, 8); |
316 | sps_id = get_ue_golomb_31(gb); |
317 | |
318 | if (sps_id >= MAX_SPS_COUNT) { |
319 | pr_info( "sps_id %u out of range\n", sps_id); |
320 | goto fail; |
321 | } |
322 | |
323 | sps->sps_id = sps_id; |
324 | sps->time_offset_length = 24; |
325 | sps->profile_idc = profile_idc; |
326 | sps->constraint_set_flags = constraint_set_flags; |
327 | sps->level_idc = level_idc; |
328 | sps->full_range = -1; |
329 | |
330 | memset(sps->scaling_matrix4, 16, sizeof(sps->scaling_matrix4)); |
331 | memset(sps->scaling_matrix8, 16, sizeof(sps->scaling_matrix8)); |
332 | sps->scaling_matrix_present = 0; |
333 | sps->colorspace = 2; //AVCOL_SPC_UNSPECIFIED |
334 | |
335 | if (sps->profile_idc == 100 || // High profile |
336 | sps->profile_idc == 110 || // High10 profile |
337 | sps->profile_idc == 122 || // High422 profile |
338 | sps->profile_idc == 244 || // High444 Predictive profile |
339 | sps->profile_idc == 44 || // Cavlc444 profile |
340 | sps->profile_idc == 83 || // Scalable Constrained High profile (SVC) |
341 | sps->profile_idc == 86 || // Scalable High Intra profile (SVC) |
342 | sps->profile_idc == 118 || // Stereo High profile (MVC) |
343 | sps->profile_idc == 128 || // Multiview High profile (MVC) |
344 | sps->profile_idc == 138 || // Multiview Depth High profile (MVCD) |
345 | sps->profile_idc == 144) { // old High444 profile |
346 | sps->chroma_format_idc = get_ue_golomb_31(gb); |
347 | |
348 | if (sps->chroma_format_idc > 3U) { |
349 | pr_err("chroma_format_idc %u\n", sps->chroma_format_idc); |
350 | goto fail; |
351 | } else if (sps->chroma_format_idc == 3) { |
352 | sps->residual_color_transform_flag = get_bits1(gb); |
353 | if (sps->residual_color_transform_flag) { |
354 | pr_info( "separate color planes are not supported\n"); |
355 | goto fail; |
356 | } |
357 | } |
358 | |
359 | sps->bit_depth_luma = get_ue_golomb(gb) + 8; |
360 | sps->bit_depth_chroma = get_ue_golomb(gb) + 8; |
361 | if (sps->bit_depth_chroma != sps->bit_depth_luma) { |
362 | pr_err("Different chroma and luma bit depth\n"); |
363 | goto fail; |
364 | } |
365 | |
366 | if (sps->bit_depth_luma < 8 || sps->bit_depth_luma > 14 || |
367 | sps->bit_depth_chroma < 8 || sps->bit_depth_chroma > 14) { |
368 | pr_info("illegal bit depth value (%d, %d)\n", |
369 | sps->bit_depth_luma, sps->bit_depth_chroma); |
370 | goto fail; |
371 | } |
372 | |
373 | sps->transform_bypass = get_bits1(gb); |
374 | ret = decode_scaling_matrices(gb, sps, NULL, 1, |
375 | sps->scaling_matrix4, sps->scaling_matrix8); |
376 | if (ret < 0) |
377 | goto fail; |
378 | sps->scaling_matrix_present |= ret; |
379 | } else { |
380 | sps->chroma_format_idc = 1; |
381 | sps->bit_depth_luma = 8; |
382 | sps->bit_depth_chroma = 8; |
383 | } |
384 | |
385 | log2_max_frame_num_minus4 = get_ue_golomb(gb); |
386 | if (log2_max_frame_num_minus4 < MIN_LOG2_MAX_FRAME_NUM - 4 || |
387 | log2_max_frame_num_minus4 > MAX_LOG2_MAX_FRAME_NUM - 4) { |
388 | pr_info( |
389 | "log2_max_frame_num_minus4 out of range (0-12): %d\n", |
390 | log2_max_frame_num_minus4); |
391 | goto fail; |
392 | } |
393 | sps->log2_max_frame_num = log2_max_frame_num_minus4 + 4; |
394 | |
395 | sps->poc_type = get_ue_golomb_31(gb); |
396 | if (sps->poc_type == 0) { // FIXME #define |
397 | u32 t = get_ue_golomb(gb); |
398 | if (t > 12) { |
399 | pr_info( "log2_max_poc_lsb (%d) is out of range\n", t); |
400 | goto fail; |
401 | } |
402 | sps->log2_max_poc_lsb = t + 4; |
403 | } else if (sps->poc_type == 1) { // FIXME #define |
404 | sps->delta_pic_order_always_zero_flag = get_bits1(gb); |
405 | sps->offset_for_non_ref_pic = get_se_golomb_long(gb); |
406 | sps->offset_for_top_to_bottom_field = get_se_golomb_long(gb); |
407 | |
408 | sps->poc_cycle_length = get_ue_golomb(gb); |
409 | if ((u32)sps->poc_cycle_length >= ARRAY_SIZE(sps->offset_for_ref_frame)) { |
410 | pr_info("poc_cycle_length overflow %d\n", sps->poc_cycle_length); |
411 | goto fail; |
412 | } |
413 | |
414 | for (i = 0; i < sps->poc_cycle_length; i++) |
415 | sps->offset_for_ref_frame[i] = get_se_golomb_long(gb); |
416 | } else if (sps->poc_type != 2) { |
417 | pr_info( "illegal POC type %d\n", sps->poc_type); |
418 | goto fail; |
419 | } |
420 | |
421 | sps->ref_frame_count = get_ue_golomb_31(gb); |
422 | if (sps->ref_frame_count > MAX_DELAYED_PIC_COUNT) { |
423 | pr_info("too many reference frames %d\n", sps->ref_frame_count); |
424 | goto fail; |
425 | } |
426 | sps->gaps_in_frame_num_allowed_flag = get_bits1(gb); |
427 | sps->mb_width = get_ue_golomb(gb) + 1; |
428 | sps->mb_height = get_ue_golomb(gb) + 1; |
429 | |
430 | sps->frame_mbs_only_flag = get_bits1(gb); |
431 | |
432 | if (sps->mb_height >= INT_MAX / 2U) { |
433 | pr_info("height overflow\n"); |
434 | goto fail; |
435 | } |
436 | sps->mb_height *= 2 - sps->frame_mbs_only_flag; |
437 | |
438 | if (!sps->frame_mbs_only_flag) |
439 | sps->mb_aff = get_bits1(gb); |
440 | else |
441 | sps->mb_aff = 0; |
442 | |
443 | if ((u32)sps->mb_width >= INT_MAX / 16 || |
444 | (u32)sps->mb_height >= INT_MAX / 16) { |
445 | pr_info( "mb_width/height overflow\n"); |
446 | goto fail; |
447 | } |
448 | |
449 | sps->direct_8x8_inference_flag = get_bits1(gb); |
450 | |
451 | sps->crop = get_bits1(gb); |
452 | if (sps->crop) { |
453 | u32 crop_left = get_ue_golomb(gb); |
454 | u32 crop_right = get_ue_golomb(gb); |
455 | u32 crop_top = get_ue_golomb(gb); |
456 | u32 crop_bottom = get_ue_golomb(gb); |
457 | int width = 16 * sps->mb_width; |
458 | int height = 16 * sps->mb_height; |
459 | int vsub = (sps->chroma_format_idc == 1) ? 1 : 0; |
460 | int hsub = (sps->chroma_format_idc == 1 || sps->chroma_format_idc == 2) ? 1 : 0; |
461 | int step_x = 1 << hsub; |
462 | int step_y = (2 - sps->frame_mbs_only_flag) << vsub; |
463 | |
464 | if (crop_left > (u32)INT_MAX / 4 / step_x || |
465 | crop_right > (u32)INT_MAX / 4 / step_x || |
466 | crop_top > (u32)INT_MAX / 4 / step_y || |
467 | crop_bottom > (u32)INT_MAX / 4 / step_y || |
468 | (crop_left + crop_right ) * step_x >= width || |
469 | (crop_top + crop_bottom) * step_y >= height) { |
470 | pr_info( "crop values invalid %u %u %u %u / %d %d\n", crop_left, crop_right, crop_top, crop_bottom, width, height); |
471 | goto fail; |
472 | } |
473 | |
474 | sps->crop_left = crop_left * step_x; |
475 | sps->crop_right = crop_right * step_x; |
476 | sps->crop_top = crop_top * step_y; |
477 | sps->crop_bottom = crop_bottom * step_y; |
478 | } else { |
479 | sps->crop_left = |
480 | sps->crop_right = |
481 | sps->crop_top = |
482 | sps->crop_bottom = |
483 | sps->crop = 0; |
484 | } |
485 | |
486 | sps->vui_parameters_present_flag = get_bits1(gb); |
487 | if (sps->vui_parameters_present_flag) { |
488 | int ret = decode_vui_parameters(gb, sps); |
489 | if (ret < 0) |
490 | goto fail; |
491 | } |
492 | |
493 | if (get_bits_left(gb) < 0) { |
494 | pr_info("Overread %s by %d bits\n", sps->vui_parameters_present_flag ? "VUI" : "SPS", -get_bits_left(gb)); |
495 | goto out; |
496 | } |
497 | |
498 | #if 0 |
499 | /* if the maximum delay is not stored in the SPS, derive it based on the level */ |
500 | if (!sps->bitstream_restriction_flag && sps->ref_frame_count) { |
501 | sps->num_reorder_frames = MAX_DELAYED_PIC_COUNT - 1; |
502 | for (i = 0; i < ARRAY_SIZE(level_max_dpb_mbs); i++) { |
503 | if (level_max_dpb_mbs[i][0] == sps->level_idc) { |
504 | sps->num_reorder_frames = |
505 | MIN(level_max_dpb_mbs[i][1] / (sps->mb_width * sps->mb_height), |
506 | sps->num_reorder_frames); |
507 | break; |
508 | } |
509 | } |
510 | } |
511 | #endif |
512 | |
513 | sps->num_reorder_frames = MAX_DELAYED_PIC_COUNT - 1; |
514 | for (i = 0; i < ARRAY_SIZE(level_max_dpb_mbs); i++) { |
515 | if (level_max_dpb_mbs[i][0] == sps->level_idc) { |
516 | sps->num_reorder_frames = |
517 | MIN(level_max_dpb_mbs[i][1] / (sps->mb_width * sps->mb_height), |
518 | sps->num_reorder_frames); |
519 | sps->num_reorder_frames += 1; |
520 | if (sps->max_dec_frame_buffering > sps->num_reorder_frames) |
521 | sps->num_reorder_frames = sps->max_dec_frame_buffering; |
522 | break; |
523 | } |
524 | } |
525 | |
526 | if ((sps->bitstream_restriction_flag) && |
527 | (sps->max_dec_frame_buffering < |
528 | sps->num_reorder_frames)) { |
529 | sps->num_reorder_frames = sps->max_dec_frame_buffering; |
530 | pr_info("set reorder_pic_num to %d\n", |
531 | sps->num_reorder_frames); |
532 | } |
533 | |
534 | if (!sps->sar.den) |
535 | sps->sar.den = 1; |
536 | out: |
537 | if (1) { |
538 | static const char csp[4][5] = { "Gray", "420", "422", "444" }; |
539 | pr_info("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", |
540 | sps_id, sps->profile_idc, sps->level_idc, |
541 | sps->poc_type, |
542 | sps->ref_frame_count, |
543 | sps->mb_width, sps->mb_height, |
544 | sps->frame_mbs_only_flag ? "FRM" : (sps->mb_aff ? "MB-AFF" : "PIC-AFF"), |
545 | sps->direct_8x8_inference_flag ? "8B8" : "", |
546 | sps->crop_left, sps->crop_right, |
547 | sps->crop_top, sps->crop_bottom, |
548 | sps->vui_parameters_present_flag ? "VUI" : "", |
549 | csp[sps->chroma_format_idc], |
550 | sps->timing_info_present_flag ? sps->num_units_in_tick : 0, |
551 | sps->timing_info_present_flag ? sps->time_scale : 0, |
552 | sps->bit_depth_luma, |
553 | sps->bitstream_restriction_flag ? sps->num_reorder_frames : -1); |
554 | } |
555 | |
556 | return 0; |
557 | |
558 | fail: |
559 | return -1; |
560 | } |
561 | |
562 | static const char *h264_nal_type_name[32] = { |
563 | "Unspecified 0", //H264_NAL_UNSPECIFIED |
564 | "Coded slice of a non-IDR picture", // H264_NAL_SLICE |
565 | "Coded slice data partition A", // H264_NAL_DPA |
566 | "Coded slice data partition B", // H264_NAL_DPB |
567 | "Coded slice data partition C", // H264_NAL_DPC |
568 | "IDR", // H264_NAL_IDR_SLICE |
569 | "SEI", // H264_NAL_SEI |
570 | "SPS", // H264_NAL_SPS |
571 | "PPS", // H264_NAL_PPS |
572 | "AUD", // H264_NAL_AUD |
573 | "End of sequence", // H264_NAL_END_SEQUENCE |
574 | "End of stream", // H264_NAL_END_STREAM |
575 | "Filler data", // H264_NAL_FILLER_DATA |
576 | "SPS extension", // H264_NAL_SPS_EXT |
577 | "Prefix", // H264_NAL_PREFIX |
578 | "Subset SPS", // H264_NAL_SUB_SPS |
579 | "Depth parameter set", // H264_NAL_DPS |
580 | "Reserved 17", // H264_NAL_RESERVED17 |
581 | "Reserved 18", // H264_NAL_RESERVED18 |
582 | "Auxiliary coded picture without partitioning", // H264_NAL_AUXILIARY_SLICE |
583 | "Slice extension", // H264_NAL_EXTEN_SLICE |
584 | "Slice extension for a depth view or a 3D-AVC texture view", // H264_NAL_DEPTH_EXTEN_SLICE |
585 | "Reserved 22", // H264_NAL_RESERVED22 |
586 | "Reserved 23", // H264_NAL_RESERVED23 |
587 | "Unspecified 24", // H264_NAL_UNSPECIFIED24 |
588 | "Unspecified 25", // H264_NAL_UNSPECIFIED25 |
589 | "Unspecified 26", // H264_NAL_UNSPECIFIED26 |
590 | "Unspecified 27", // H264_NAL_UNSPECIFIED27 |
591 | "Unspecified 28", // H264_NAL_UNSPECIFIED28 |
592 | "Unspecified 29", // H264_NAL_UNSPECIFIED29 |
593 | "Unspecified 30", // H264_NAL_UNSPECIFIED30 |
594 | "Unspecified 31", // H264_NAL_UNSPECIFIED31 |
595 | }; |
596 | |
597 | static const char *h264_nal_unit_name(int nal_type) |
598 | { |
599 | return h264_nal_type_name[nal_type]; |
600 | } |
601 | |
602 | static int decode_extradata_ps(u8 *data, int size, struct h264_param_sets *ps) |
603 | { |
604 | int ret = 0; |
605 | struct get_bits_context gb; |
606 | u32 src_len, rbsp_size = 0; |
607 | u8 *rbsp_buf = NULL; |
608 | int ref_idc, nalu_pos; |
609 | u32 nal_type; |
610 | u8 *p = data; |
611 | u32 len = size; |
612 | |
613 | nalu_pos = find_start_code(p, len); |
614 | if (nalu_pos < 0) |
615 | return -1; |
616 | |
617 | src_len = calc_nal_len(p + nalu_pos, size - nalu_pos); |
618 | rbsp_buf = nal_unit_extract_rbsp(p + nalu_pos, src_len, &rbsp_size); |
619 | if (rbsp_buf == NULL) |
620 | return -ENOMEM; |
621 | |
622 | ret = init_get_bits8(&gb, rbsp_buf, rbsp_size); |
623 | if (ret < 0) |
624 | goto out; |
625 | |
626 | if (get_bits1(&gb) != 0) { |
627 | pr_err("invalid h264 data,return!\n"); |
628 | goto out; |
629 | } |
630 | |
631 | ref_idc = get_bits(&gb, 2); |
632 | nal_type = get_bits(&gb, 5); |
633 | |
634 | pr_info("nal_unit_type: %d(%s), nal_ref_idc: %d\n", |
635 | nal_type, h264_nal_unit_name(nal_type), ref_idc); |
636 | |
637 | switch (nal_type) { |
638 | case H264_NAL_SPS: |
639 | ret = aml_h264_parser_sps(&gb, &ps->sps); |
640 | if (ret < 0) |
641 | goto out; |
642 | ps->sps_parsed = true; |
643 | break; |
644 | /*case H264_NAL_PPS: |
645 | ret = ff_h264_decode_picture_parameter_set(&gb, &ps->pps, rbsp_size); |
646 | if (ret < 0) |
647 | goto fail; |
648 | ps->pps_parsed = true; |
649 | break;*/ |
650 | default: |
651 | pr_err("Unsupport parser nal type (%s).\n", |
652 | h264_nal_unit_name(nal_type)); |
653 | break; |
654 | } |
655 | |
656 | out: |
657 | vfree(rbsp_buf); |
658 | |
659 | return ret; |
660 | } |
661 | |
662 | int h264_decode_extradata_ps(u8 *buf, int size, struct h264_param_sets *ps) |
663 | { |
664 | int ret = 0, i = 0, j = 0; |
665 | u8 *p = buf; |
666 | int len = size; |
667 | |
668 | for (i = 4; i < size; i++) { |
669 | j = find_start_code(p, len); |
670 | if (j > 0) { |
671 | len = size - (p - buf); |
672 | ret = decode_extradata_ps(p, len, ps); |
673 | if (ret) { |
674 | pr_err("parse extra data failed. err: %d\n", ret); |
675 | return ret; |
676 | } |
677 | p += j; |
678 | } |
679 | p++; |
680 | } |
681 | |
682 | return ret; |
683 | } |
684 | |
685 | |
686 |