1 /******************************************************************************
2 *
3 * Copyright (C) 2021 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *****************************************************************************
18 * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
19 */
20
21 /*****************************************************************************/
22 /* */
23 /* File Name : imvcd_nalu_parser.h */
24 /* */
25 /* Description : Functions for MVC NALU parsing */
26 /* */
27 /*****************************************************************************/
28 #include <string.h>
29
30 #include "ih264_typedefs.h"
31 #include "iv.h"
32 #include "imvcd.h"
33 #include "ih264d_error_handler.h"
34 #include "ih264d_bitstrm.h"
35 #include "ih264d_defs.h"
36 #include "ih264d_nal.h"
37 #include "ih264d_parse_cavlc.h"
38 #include "ih264d_structs.h"
39 #include "ih264d_vui.h"
40 #include "imvcd_defs.h"
41 #include "imvcd_slice_functions.h"
42 #include "imvcd_structs.h"
43 #include "imvcd_utils.h"
44
imvcd_nalu_mvc_ext_parser(mvc_dec_ctxt_t * ps_mvcd_ctxt,dec_bit_stream_t * ps_bitstrm)45 static WORD32 imvcd_nalu_mvc_ext_parser(mvc_dec_ctxt_t *ps_mvcd_ctxt, dec_bit_stream_t *ps_bitstrm)
46 {
47 nalu_mvc_ext_t *ps_nalu_mvc_ext =
48 &ps_mvcd_ctxt->as_nalu_mvc_ext[ps_mvcd_ctxt->u2_num_views_decoded];
49
50 ps_nalu_mvc_ext->u1_non_idr_flag = ih264d_get_bit_h264(ps_bitstrm);
51 ps_nalu_mvc_ext->u1_priority_id = ih264d_get_bits_h264(ps_bitstrm, 6);
52 ps_nalu_mvc_ext->u2_view_id = ih264d_get_bits_h264(ps_bitstrm, 10);
53
54 if((ps_nalu_mvc_ext->u2_view_id >= MAX_NUM_VIEWS) ||
55 (ps_nalu_mvc_ext->u2_view_id >= ps_mvcd_ctxt->u2_num_views))
56 {
57 return IVD_INVALID_BITSTREAM;
58 }
59
60 ps_nalu_mvc_ext->u1_temporal_id = ih264d_get_bits_h264(ps_bitstrm, 3);
61 ps_nalu_mvc_ext->u1_anchor_pic_flag = ih264d_get_bit_h264(ps_bitstrm);
62 ps_nalu_mvc_ext->u1_inter_view_flag = ih264d_get_bit_h264(ps_bitstrm);
63
64 if(0 == ih264d_get_bit_h264(ps_bitstrm))
65 {
66 return IVD_INVALID_BITSTREAM;
67 }
68
69 return OK;
70 }
71
imvcd_parse_subset_sps(mvc_dec_ctxt_t * ps_mvcd_ctxt,dec_bit_stream_t * ps_bitstrm)72 static WORD32 imvcd_parse_subset_sps(mvc_dec_ctxt_t *ps_mvcd_ctxt, dec_bit_stream_t *ps_bitstrm)
73 {
74 subset_sps_t *ps_subset_sps = NULL;
75 dec_seq_params_t *ps_sps = NULL;
76
77 WORD32 i, j, k;
78 UWORD8 u1_profile_idc;
79 UWORD8 au1_constraint_set_flags[6];
80 UWORD8 u1_level_idc;
81 UWORD8 u1_seq_parameter_set_id;
82 UWORD32 *pu4_bitstrm_buf = ps_bitstrm->pu4_buffer;
83 UWORD32 *pu4_bitstrm_ofst = &ps_bitstrm->u4_ofst;
84 UWORD32 u4_temp;
85 UWORD16 u2_num_views_m1;
86 UWORD8 u1_num_refs;
87 UWORD8 u1_num_level_values_signalled_m1;
88 UWORD8 u2_num_ops_m1;
89
90 dec_struct_t *ps_view_ctxt = &ps_mvcd_ctxt->s_view_dec_ctxt;
91
92 WORD32 i4_error_code = OK;
93
94 if(0 == ps_mvcd_ctxt->u1_num_sps)
95 {
96 return ERROR_INV_SPS_PPS_T;
97 }
98
99 for(i = 0; i < MAX_NUM_SEQ_PARAMS; i++)
100 {
101 if(ps_view_ctxt->ps_sps[i].u1_is_valid)
102 {
103 ps_sps = &ps_view_ctxt->ps_sps[i];
104
105 break;
106 }
107 }
108
109 if(NULL == ps_sps)
110 {
111 return ERROR_INV_SPS_PPS_T;
112 }
113
114 u1_profile_idc = ih264d_get_bits_h264(ps_bitstrm, 8);
115
116 for(i = 0; i < 6; i++)
117 {
118 au1_constraint_set_flags[i] = ih264d_get_bit_h264(ps_bitstrm);
119 }
120
121 if((u1_profile_idc != MULTIVIEW_HIGH_PROFILE_IDC) || (au1_constraint_set_flags[1] == 1))
122 {
123 return ERROR_FEATURE_UNAVAIL;
124 }
125
126 /*****************************************************/
127 /* Read reserved_zero_2bits (2 bits) */
128 /*****************************************************/
129 ih264d_get_bits_h264(ps_bitstrm, 2);
130
131 u1_level_idc = ih264d_get_bits_h264(ps_bitstrm, 8);
132
133 u4_temp = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
134 u1_seq_parameter_set_id = u4_temp;
135
136 if(u4_temp & MASK_ERR_SEQ_SET_ID)
137 {
138 return ERROR_INV_SPS_PPS_T;
139 }
140
141 if(ps_sps->u1_level_idc != u1_level_idc)
142 {
143 ps_view_ctxt->u1_res_changed = 1;
144
145 return IVD_RES_CHANGED;
146 }
147
148 ps_subset_sps = &ps_mvcd_ctxt->as_subset_sps[u1_seq_parameter_set_id];
149
150 /* Accounting for the idiocy in 'ih264d_parse_pps' */
151 ps_subset_sps->s_sps_data.u1_profile_idc = HIGH_PROFILE_IDC;
152 ps_subset_sps->s_sps_data.u1_level_idc = u1_level_idc;
153 ps_subset_sps->s_sps_data.u1_seq_parameter_set_id = u1_seq_parameter_set_id;
154
155 ps_subset_sps->s_sps_data.i4_chroma_format_idc = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
156
157 if(ps_subset_sps->s_sps_data.i4_chroma_format_idc != 1)
158 {
159 return ERROR_FEATURE_UNAVAIL;
160 }
161
162 ps_subset_sps->s_sps_data.i4_bit_depth_luma_minus8 =
163 ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
164
165 if(ps_subset_sps->s_sps_data.i4_bit_depth_luma_minus8 != 0)
166 {
167 return ERROR_FEATURE_UNAVAIL;
168 }
169
170 ps_subset_sps->s_sps_data.i4_bit_depth_chroma_minus8 =
171 ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
172
173 if(ps_subset_sps->s_sps_data.i4_bit_depth_chroma_minus8 != 0)
174 {
175 return ERROR_FEATURE_UNAVAIL;
176 }
177
178 ps_subset_sps->s_sps_data.i4_qpprime_y_zero_transform_bypass_flag =
179 ih264d_get_bit_h264(ps_bitstrm);
180
181 if(ps_subset_sps->s_sps_data.i4_qpprime_y_zero_transform_bypass_flag != 0)
182 {
183 return ERROR_INV_SPS_PPS_T;
184 }
185
186 ps_subset_sps->s_sps_data.i4_seq_scaling_matrix_present_flag = ih264d_get_bit_h264(ps_bitstrm);
187
188 if(ps_subset_sps->s_sps_data.i4_seq_scaling_matrix_present_flag)
189 {
190 for(i = 0; i < 8; i++)
191 {
192 ps_subset_sps->s_sps_data.u1_seq_scaling_list_present_flag[i] =
193 ih264d_get_bit_h264(ps_bitstrm);
194 }
195 }
196
197 u4_temp = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
198
199 if(u4_temp > (MAX_BITS_IN_FRAME_NUM - 4))
200 {
201 return ERROR_INV_SPS_PPS_T;
202 }
203
204 ps_subset_sps->s_sps_data.u1_bits_in_frm_num = 4 + u4_temp;
205
206 ps_subset_sps->s_sps_data.u2_u4_max_pic_num_minus1 =
207 (1 << (ps_subset_sps->s_sps_data.u1_bits_in_frm_num)) - 1;
208
209 ps_subset_sps->s_sps_data.u1_pic_order_cnt_type = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
210
211 if(ps_subset_sps->s_sps_data.u1_pic_order_cnt_type > MAX_PIC_ORDER_CNT_TYPE)
212 {
213 return ERROR_INV_POC_TYPE_T;
214 }
215
216 ps_subset_sps->s_sps_data.u1_num_ref_frames_in_pic_order_cnt_cycle = 1;
217
218 if(ps_subset_sps->s_sps_data.u1_pic_order_cnt_type == 0)
219 {
220 ps_subset_sps->s_sps_data.u1_log2_max_pic_order_cnt_lsb_minus =
221 4 + ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
222
223 if(ps_subset_sps->s_sps_data.u1_log2_max_pic_order_cnt_lsb_minus > MAX_BITS_IN_POC_LSB)
224 {
225 return ERROR_INV_SPS_PPS_T;
226 }
227
228 ps_subset_sps->s_sps_data.i4_max_pic_order_cntLsb =
229 (1 << ps_subset_sps->s_sps_data.u1_log2_max_pic_order_cnt_lsb_minus);
230 }
231 else if(ps_subset_sps->s_sps_data.u1_pic_order_cnt_type == 1)
232 {
233 ps_subset_sps->s_sps_data.u1_delta_pic_order_always_zero_flag =
234 ih264d_get_bit_h264(ps_bitstrm);
235
236 ps_subset_sps->s_sps_data.i4_ofst_for_non_ref_pic =
237 ih264d_sev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
238
239 ps_subset_sps->s_sps_data.i4_ofst_for_top_to_bottom_field =
240 ih264d_sev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
241
242 ps_subset_sps->s_sps_data.u1_num_ref_frames_in_pic_order_cnt_cycle =
243 ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
244
245 if(ps_subset_sps->s_sps_data.u1_num_ref_frames_in_pic_order_cnt_cycle > MVC_MAX_REF_PICS)
246 {
247 return ERROR_INV_SPS_PPS_T;
248 }
249
250 for(i = 0; i < ps_subset_sps->s_sps_data.u1_num_ref_frames_in_pic_order_cnt_cycle; i++)
251 {
252 ps_subset_sps->s_sps_data.i4_ofst_for_ref_frame[i] =
253 ih264d_sev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
254 }
255 }
256
257 ps_subset_sps->s_sps_data.u1_num_ref_frames = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
258
259 if((ps_subset_sps->s_sps_data.u1_num_ref_frames > MVC_MAX_REF_PICS))
260 {
261 return ERROR_NUM_REF;
262 }
263
264 ps_subset_sps->s_sps_data.u1_gaps_in_frame_num_value_allowed_flag =
265 ih264d_get_bit_h264(ps_bitstrm);
266
267 ps_subset_sps->s_sps_data.u2_frm_wd_in_mbs = 1 + ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
268
269 if(ps_subset_sps->s_sps_data.u2_frm_wd_in_mbs > (H264_MAX_FRAME_WIDTH >> 4))
270 {
271 return IVD_STREAM_WIDTH_HEIGHT_NOT_SUPPORTED;
272 }
273
274 ps_subset_sps->s_sps_data.u2_frm_ht_in_mbs = 1 + ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
275
276 if(ps_subset_sps->s_sps_data.u2_frm_ht_in_mbs > (H264_MAX_FRAME_HEIGHT >> 4))
277 {
278 return IVD_STREAM_WIDTH_HEIGHT_NOT_SUPPORTED;
279 }
280
281 ps_subset_sps->s_sps_data.u2_max_mb_addr =
282 ps_subset_sps->s_sps_data.u2_frm_wd_in_mbs * ps_subset_sps->s_sps_data.u2_frm_ht_in_mbs - 1;
283
284 ps_subset_sps->s_sps_data.u2_total_num_of_mbs = ps_subset_sps->s_sps_data.u2_max_mb_addr + 1;
285
286 ps_subset_sps->s_sps_data.u1_frame_mbs_only_flag = ih264d_get_bit_h264(ps_bitstrm);
287
288 if(!ps_subset_sps->s_sps_data.u1_frame_mbs_only_flag)
289 {
290 return ERROR_FEATURE_UNAVAIL;
291 }
292
293 ps_subset_sps->s_sps_data.u1_mb_aff_flag = 0;
294
295 ps_subset_sps->s_sps_data.u1_direct_8x8_inference_flag = ih264d_get_bit_h264(ps_bitstrm);
296
297 /* Frame cropping flag */
298 u4_temp = ih264d_get_bit_h264(ps_bitstrm);
299
300 if(u4_temp)
301 {
302 ps_subset_sps->s_disp_offsets.u2_left_offset =
303 ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
304 ps_subset_sps->s_disp_offsets.u2_right_offset =
305 ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
306 ps_subset_sps->s_disp_offsets.u2_top_offset = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
307 ps_subset_sps->s_disp_offsets.u2_bottom_offset =
308 ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
309 }
310 else
311 {
312 memset(&ps_subset_sps->s_disp_offsets, 0, sizeof(ps_subset_sps->s_disp_offsets));
313 }
314
315 if(ps_sps->u2_frm_wd_in_mbs != ps_subset_sps->s_sps_data.u2_frm_wd_in_mbs)
316 {
317 ps_view_ctxt->u1_res_changed = 1;
318
319 return IVD_RES_CHANGED;
320 }
321
322 if(ps_sps->u2_frm_ht_in_mbs != ps_subset_sps->s_sps_data.u2_frm_ht_in_mbs)
323 {
324 ps_view_ctxt->u1_res_changed = 1;
325
326 return IVD_RES_CHANGED;
327 }
328
329 if(ps_view_ctxt->u2_disp_width != (ps_subset_sps->s_sps_data.u2_frm_wd_in_mbs * MB_SIZE -
330 ps_subset_sps->s_disp_offsets.u2_left_offset -
331 ps_subset_sps->s_disp_offsets.u2_right_offset))
332 {
333 ps_view_ctxt->u1_res_changed = 1;
334
335 return IVD_RES_CHANGED;
336 }
337
338 if(ps_view_ctxt->u2_disp_height != (ps_subset_sps->s_sps_data.u2_frm_ht_in_mbs * MB_SIZE -
339 ps_subset_sps->s_disp_offsets.u2_top_offset -
340 ps_subset_sps->s_disp_offsets.u2_bottom_offset))
341 {
342 ps_view_ctxt->u1_res_changed = 1;
343
344 return IVD_RES_CHANGED;
345 }
346
347 ps_subset_sps->s_sps_data.u1_vui_parameters_present_flag = ih264d_get_bit_h264(ps_bitstrm);
348
349 if(ps_subset_sps->s_sps_data.u1_vui_parameters_present_flag)
350 {
351 i4_error_code = ih264d_parse_vui_parametres(&ps_subset_sps->s_sps_data.s_vui, ps_bitstrm);
352
353 if(i4_error_code != OK)
354 {
355 return i4_error_code;
356 }
357
358 if(ps_subset_sps->s_sps_data.s_vui.u1_bitstream_restriction_flag &&
359 (ps_sps->s_vui.u4_num_reorder_frames !=
360 ps_subset_sps->s_sps_data.s_vui.u4_num_reorder_frames))
361 {
362 ps_view_ctxt->u1_res_changed = 1;
363
364 return IVD_RES_CHANGED;
365 }
366 }
367
368 if(ih264d_get_bit_h264(ps_bitstrm) != 1)
369 {
370 return ERROR_INV_SPS_PPS_T;
371 }
372
373 u2_num_views_m1 = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
374
375 if(u2_num_views_m1 >= MAX_NUM_VIEWS)
376 {
377 return ERROR_INVALID_SEQ_PARAM;
378 }
379
380 ps_subset_sps->s_sps_mvc_ext.u2_num_views = 1 + u2_num_views_m1;
381
382 if(ps_view_ctxt->i4_decode_header)
383 {
384 ps_mvcd_ctxt->u2_num_views = MAX(ps_mvcd_ctxt->u2_num_views, 1 + u2_num_views_m1);
385 }
386 else if(ps_mvcd_ctxt->u2_num_views != (1 + u2_num_views_m1))
387 {
388 return ERROR_INVALID_SEQ_PARAM;
389 }
390
391 for(i = 0; i <= u2_num_views_m1; i++)
392 {
393 ps_subset_sps->s_sps_mvc_ext.au2_view_ids[i] =
394 ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
395 }
396
397 for(i = 0; i < 2; i++)
398 {
399 ps_subset_sps->s_sps_mvc_ext.as_anchor_ref_data[i][0].u1_num_refs = 0;
400 ps_subset_sps->s_sps_mvc_ext.as_non_anchor_ref_data[i][0].u1_num_refs = 0;
401 }
402
403 for(i = 1; i <= u2_num_views_m1; i++)
404 {
405 u1_num_refs = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
406 ps_subset_sps->s_sps_mvc_ext.as_anchor_ref_data[0][i].u1_num_refs = u1_num_refs;
407
408 if(u1_num_refs > MAX_NUM_IVP_REFS)
409 {
410 return ERROR_INVALID_SEQ_PARAM;
411 }
412
413 for(j = 0; j < u1_num_refs; j++)
414 {
415 ps_subset_sps->s_sps_mvc_ext.as_anchor_ref_data[0][i].au2_ref_view_ids[j] =
416 ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
417 }
418
419 u1_num_refs = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
420 ps_subset_sps->s_sps_mvc_ext.as_anchor_ref_data[1][i].u1_num_refs = u1_num_refs;
421
422 if(u1_num_refs > MAX_NUM_IVP_REFS)
423 {
424 return ERROR_INVALID_SEQ_PARAM;
425 }
426
427 for(j = 0; j < u1_num_refs; j++)
428 {
429 ps_subset_sps->s_sps_mvc_ext.as_anchor_ref_data[1][i].au2_ref_view_ids[j] =
430 ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
431 }
432 }
433
434 for(i = 1; i <= u2_num_views_m1; i++)
435 {
436 u1_num_refs = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
437 ps_subset_sps->s_sps_mvc_ext.as_non_anchor_ref_data[0][i].u1_num_refs = u1_num_refs;
438
439 if(u1_num_refs > MAX_NUM_IVP_REFS)
440 {
441 return ERROR_INVALID_SEQ_PARAM;
442 }
443
444 for(j = 0; j < u1_num_refs; j++)
445 {
446 ps_subset_sps->s_sps_mvc_ext.as_non_anchor_ref_data[0][i].au2_ref_view_ids[j] =
447 ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
448 }
449
450 u1_num_refs = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
451 ps_subset_sps->s_sps_mvc_ext.as_non_anchor_ref_data[1][i].u1_num_refs = u1_num_refs;
452
453 if(u1_num_refs > MAX_NUM_IVP_REFS)
454 {
455 return ERROR_INVALID_SEQ_PARAM;
456 }
457
458 for(j = 0; j < u1_num_refs; j++)
459 {
460 ps_subset_sps->s_sps_mvc_ext.as_non_anchor_ref_data[1][i].au2_ref_view_ids[j] =
461 ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
462 }
463 }
464
465 u1_num_level_values_signalled_m1 = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
466 ps_subset_sps->s_sps_mvc_ext.u1_num_level_values_signalled =
467 u1_num_level_values_signalled_m1 + 1;
468
469 if(u1_num_level_values_signalled_m1 >= MAX_NUM_LEVEL_VALUES_SIGNALLED)
470 {
471 return ERROR_INVALID_SEQ_PARAM;
472 }
473
474 for(i = 0; i <= u1_num_level_values_signalled_m1; i++)
475 {
476 ps_subset_sps->s_sps_mvc_ext.as_mvc_level_info[i].u4_level_idc =
477 ih264d_get_bits_h264(ps_bitstrm, 8);
478
479 u2_num_ops_m1 = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
480 ps_subset_sps->s_sps_mvc_ext.as_mvc_level_info->as_mvc_op_data[i].u2_num_ops =
481 1 + u2_num_ops_m1;
482
483 if(u2_num_ops_m1 >= MAX_NUM_OPERATING_POINTS)
484 {
485 return ERROR_INVALID_SEQ_PARAM;
486 }
487
488 for(j = 0; j <= u2_num_ops_m1; j++)
489 {
490 UWORD16 u2_num_target_views_m1;
491
492 ps_subset_sps->s_sps_mvc_ext.as_mvc_level_info->as_mvc_op_data[j].u1_temporal_id =
493 ih264d_get_bits_h264(ps_bitstrm, 3);
494
495 u2_num_target_views_m1 = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
496
497 ps_subset_sps->s_sps_mvc_ext.as_mvc_level_info->as_mvc_op_data[j].u2_num_target_views =
498 1 + u2_num_target_views_m1;
499
500 if(u2_num_target_views_m1 >= MAX_NUM_VIEWS)
501 {
502 return ERROR_INVALID_SEQ_PARAM;
503 }
504
505 for(k = 0; k <= u2_num_target_views_m1; k++)
506 {
507 ps_subset_sps->s_sps_mvc_ext.as_mvc_level_info->as_mvc_op_data[j]
508 .au2_target_view_ids[k] = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
509 }
510
511 ps_subset_sps->s_sps_mvc_ext.as_mvc_level_info->as_mvc_op_data[j].u2_num_views =
512 (UWORD16) 1 + ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
513 }
514 }
515
516 ps_subset_sps->u1_mvc_vui_parameters_present_flag = ih264d_get_bit_h264(ps_bitstrm);
517
518 if(ps_subset_sps->u1_mvc_vui_parameters_present_flag)
519 {
520 return ERROR_INV_SPS_PPS_T;
521 }
522
523 /* In case bitstream read has exceeded the filled size, then
524 return an error */
525 if(EXCEED_OFFSET(ps_bitstrm))
526 {
527 return ERROR_INV_SPS_PPS_T;
528 }
529
530 ps_subset_sps->s_sps_data.u1_is_valid = 1;
531
532 /* This ensures PPS has valid data in SPS array for reference */
533 ps_view_ctxt->ps_sps[ps_subset_sps->s_sps_data.u1_seq_parameter_set_id] =
534 ps_subset_sps->s_sps_data;
535
536 ps_mvcd_ctxt->u1_num_subset_sps++;
537
538 return OK;
539 }
540
541 /* This function removes emulation byte "0x03" from bitstream(EBSP to RBSP).
542 It also converts bytestream format into 32 bit little - endian format. */
imvcd_transform_nalu(dec_bit_stream_t * ps_bitstrm,UWORD8 * pu1_nal_unit,UWORD32 u4_numbytes_in_nal_unit)543 static WORD32 imvcd_transform_nalu(dec_bit_stream_t *ps_bitstrm, UWORD8 *pu1_nal_unit,
544 UWORD32 u4_numbytes_in_nal_unit)
545 {
546 UWORD32 ui4_word;
547 UWORD8 u1_cur_byte;
548
549 static const UWORD32 u4_num_bytes_in_word = sizeof(ui4_word) / sizeof(u1_cur_byte);
550 UWORD32 u4_num_bytes_in_rbsp = 0;
551 WORD32 i = 0, j;
552 WORD8 c_count = 0;
553 UWORD32 *puc_bitstream_buffer = (UWORD32 *) pu1_nal_unit;
554 UWORD8 u1_nal_header_size = 1;
555 UWORD8 u1_num_bytes_copied = 0;
556
557 ps_bitstrm->pu4_buffer = puc_bitstream_buffer;
558
559 ui4_word = *pu1_nal_unit++;
560 u1_num_bytes_copied++;
561
562 if((NAL_UNIT_TYPE(ui4_word) == PREFIX_NAL) ||
563 (NAL_UNIT_TYPE(ui4_word) == CODED_SLICE_EXTENSION))
564 {
565 u1_nal_header_size += 3;
566 }
567
568 for(j = 0; j < 2; j++)
569 {
570 u1_cur_byte = *pu1_nal_unit++;
571
572 ui4_word = ((ui4_word << 8) | u1_cur_byte);
573 u1_num_bytes_copied++;
574
575 c_count++;
576 u4_num_bytes_in_rbsp++;
577
578 if(u1_cur_byte != 0x00)
579 {
580 c_count = 0;
581 }
582 }
583
584 if(u4_numbytes_in_nal_unit > 2)
585 {
586 i = ((u4_numbytes_in_nal_unit - 3));
587 }
588
589 for(; i > 8; i -= 4)
590 {
591 // loop 0
592 u1_cur_byte = *pu1_nal_unit++;
593
594 if(c_count == NUM_OF_ZERO_BYTES_BEFORE_START_CODE &&
595 u1_cur_byte == EMULATION_PREVENTION_BYTE)
596 {
597 c_count = 0;
598 u1_cur_byte = *pu1_nal_unit++;
599 i--;
600 }
601
602 ui4_word = ((ui4_word << 8) | u1_cur_byte);
603 u1_num_bytes_copied++;
604 if(u4_num_bytes_in_word == u1_num_bytes_copied)
605 {
606 *puc_bitstream_buffer = ui4_word;
607 puc_bitstream_buffer++;
608 u1_num_bytes_copied = 0;
609 }
610
611 c_count++;
612 if(u1_cur_byte != 0x00) c_count = 0;
613
614 // loop 1
615 u1_cur_byte = *pu1_nal_unit++;
616
617 if(c_count == NUM_OF_ZERO_BYTES_BEFORE_START_CODE &&
618 u1_cur_byte == EMULATION_PREVENTION_BYTE)
619 {
620 c_count = 0;
621 u1_cur_byte = *pu1_nal_unit++;
622 i--;
623 }
624 ui4_word = ((ui4_word << 8) | u1_cur_byte);
625 u1_num_bytes_copied++;
626 if(u4_num_bytes_in_word == u1_num_bytes_copied)
627 {
628 *puc_bitstream_buffer = ui4_word;
629 puc_bitstream_buffer++;
630 u1_num_bytes_copied = 0;
631 }
632
633 c_count++;
634 if(u1_cur_byte != 0x00) c_count = 0;
635
636 // loop 2
637 u1_cur_byte = *pu1_nal_unit++;
638
639 if(c_count == NUM_OF_ZERO_BYTES_BEFORE_START_CODE &&
640 u1_cur_byte == EMULATION_PREVENTION_BYTE)
641 {
642 c_count = 0;
643 u1_cur_byte = *pu1_nal_unit++;
644 i--;
645 }
646
647 ui4_word = ((ui4_word << 8) | u1_cur_byte);
648 u1_num_bytes_copied++;
649 if(u4_num_bytes_in_word == u1_num_bytes_copied)
650 {
651 *puc_bitstream_buffer = ui4_word;
652 puc_bitstream_buffer++;
653 u1_num_bytes_copied = 0;
654 }
655
656 c_count++;
657 if(u1_cur_byte != 0x00) c_count = 0;
658
659 // loop 3
660 u1_cur_byte = *pu1_nal_unit++;
661
662 if(c_count == NUM_OF_ZERO_BYTES_BEFORE_START_CODE &&
663 u1_cur_byte == EMULATION_PREVENTION_BYTE)
664 {
665 c_count = 0;
666 u1_cur_byte = *pu1_nal_unit++;
667 i--;
668 }
669
670 ui4_word = ((ui4_word << 8) | u1_cur_byte);
671 u1_num_bytes_copied++;
672 if(u4_num_bytes_in_word == u1_num_bytes_copied)
673 {
674 *puc_bitstream_buffer = ui4_word;
675 puc_bitstream_buffer++;
676 u1_num_bytes_copied = 0;
677 }
678
679 c_count++;
680 if(u1_cur_byte != 0x00) c_count = 0;
681
682 u4_num_bytes_in_rbsp += 4;
683 }
684
685 for(; i > 0; i--)
686 {
687 u1_cur_byte = *pu1_nal_unit++;
688
689 if(c_count == NUM_OF_ZERO_BYTES_BEFORE_START_CODE &&
690 u1_cur_byte == EMULATION_PREVENTION_BYTE)
691 {
692 c_count = 0;
693 i--;
694 u1_cur_byte = *pu1_nal_unit++;
695 }
696
697 ui4_word = ((ui4_word << 8) | u1_cur_byte);
698 u4_num_bytes_in_rbsp++;
699
700 if((u4_num_bytes_in_rbsp & 0x03) == 0x03)
701 {
702 *puc_bitstream_buffer = ui4_word;
703 puc_bitstream_buffer++;
704 }
705 c_count++;
706 if(u1_cur_byte != 0x00) c_count = 0;
707 }
708
709 *puc_bitstream_buffer = (ui4_word << ((3 - (((u4_num_bytes_in_rbsp << 30) >> 30))) << 3));
710 ps_bitstrm->u4_ofst = 0;
711 ps_bitstrm->u4_max_ofst = ((u4_num_bytes_in_rbsp + u1_nal_header_size) << 3);
712
713 return (u4_num_bytes_in_rbsp);
714 }
715
imvcd_nalu_parser(mvc_dec_ctxt_t * ps_mvcd_ctxt,UWORD8 * pu1_bitstream_buf,UWORD32 i4_nalu_length)716 WORD32 imvcd_nalu_parser(mvc_dec_ctxt_t *ps_mvcd_ctxt, UWORD8 *pu1_bitstream_buf,
717 UWORD32 i4_nalu_length)
718 {
719 AVC_EXT_NALU_ID_T e_nalu_id;
720
721 UWORD8 u1_first_byte;
722
723 dec_struct_t *ps_view_ctxt = &ps_mvcd_ctxt->s_view_dec_ctxt;
724 dec_bit_stream_t *ps_bitstrm = ps_view_ctxt->ps_bitstrm;
725
726 WORD32 i4_error_code = NOT_OK;
727
728 if((NULL != pu1_bitstream_buf) && (i4_nalu_length > 0))
729 {
730 imvcd_transform_nalu(ps_bitstrm, pu1_bitstream_buf, i4_nalu_length);
731
732 u1_first_byte = ih264d_get_bits_h264(ps_bitstrm, 8);
733
734 if(NAL_FORBIDDEN_BIT(u1_first_byte))
735 {
736 return NOT_OK;
737 }
738
739 e_nalu_id = NAL_UNIT_TYPE(u1_first_byte);
740 ps_view_ctxt->u1_nal_unit_type = e_nalu_id;
741
742 // if any other nal unit other than slice nal is encountered in between a
743 // frame break out of loop without consuming header
744 if((ps_view_ctxt->u4_slice_start_code_found == 1) &&
745 (ps_view_ctxt->u1_pic_decode_done != 1) && is_slice_nalu_type(e_nalu_id))
746 {
747 return ERROR_INCOMPLETE_FRAME;
748 }
749
750 switch(e_nalu_id)
751 {
752 case PREFIX_NAL:
753 {
754 if(!ps_view_ctxt->i4_decode_header)
755 {
756 if(1 == ih264d_get_bit_h264(ps_bitstrm))
757 {
758 return IVD_INVALID_BITSTREAM;
759 }
760
761 i4_error_code = imvcd_nalu_mvc_ext_parser(ps_mvcd_ctxt, ps_bitstrm);
762
763 if(i4_error_code != OK)
764 {
765 return i4_error_code;
766 }
767 }
768
769 break;
770 }
771 case SUBSET_SPS:
772 {
773 ih264d_rbsp_to_sodb(ps_view_ctxt->ps_bitstrm);
774
775 i4_error_code = imvcd_parse_subset_sps(ps_mvcd_ctxt, ps_bitstrm);
776
777 if(OK != i4_error_code)
778 {
779 return i4_error_code;
780 }
781
782 ps_view_ctxt->i4_header_decoded |= 1 << SUBSET_SPS;
783
784 break;
785 }
786 case SLICE_NON_IDR:
787 case SLICE_IDR:
788 {
789 if(!ps_view_ctxt->i4_decode_header)
790 {
791 if(is_header_decoded(ps_view_ctxt->i4_header_decoded, SPS) &&
792 is_header_decoded(ps_view_ctxt->i4_header_decoded, PPS))
793 {
794 nalu_mvc_ext_t *ps_nalu_mvc_ext = imvcd_get_cur_nalu_mvc_ext(ps_mvcd_ctxt);
795
796 ps_view_ctxt->u4_slice_start_code_found = 1;
797
798 if((0 == ps_mvcd_ctxt->u2_num_views_decoded) &&
799 !ps_nalu_mvc_ext->u1_inter_view_flag)
800 {
801 ps_nalu_mvc_ext->u1_inter_view_flag = 1;
802 }
803
804 ih264d_rbsp_to_sodb(ps_view_ctxt->ps_bitstrm);
805
806 i4_error_code = imvcd_parse_decode_slice(ps_mvcd_ctxt);
807
808 if(i4_error_code != OK)
809 {
810 return i4_error_code;
811 }
812 }
813 else
814 {
815 return IVD_INVALID_BITSTREAM;
816 }
817 }
818
819 break;
820 }
821 case CODED_SLICE_EXTENSION:
822 {
823 if(!ps_view_ctxt->i4_decode_header)
824 {
825 if(is_header_decoded(ps_view_ctxt->i4_header_decoded, SPS) &&
826 is_header_decoded(ps_view_ctxt->i4_header_decoded, PPS) &&
827 is_header_decoded(ps_view_ctxt->i4_header_decoded, SUBSET_SPS))
828 {
829 ps_view_ctxt->u4_slice_start_code_found = 1;
830
831 if(1 == ih264d_get_bit_h264(ps_bitstrm))
832 {
833 return IVD_INVALID_BITSTREAM;
834 }
835
836 i4_error_code = imvcd_nalu_mvc_ext_parser(ps_mvcd_ctxt, ps_bitstrm);
837
838 if(i4_error_code != OK)
839 {
840 return i4_error_code;
841 }
842
843 ih264d_rbsp_to_sodb(ps_view_ctxt->ps_bitstrm);
844
845 i4_error_code = imvcd_parse_decode_slice(ps_mvcd_ctxt);
846
847 if(i4_error_code != OK)
848 {
849 return i4_error_code;
850 }
851 }
852 else
853 {
854 return IVD_INVALID_BITSTREAM;
855 }
856 }
857
858 break;
859 }
860 default:
861 {
862 i4_error_code = ERROR_UNKNOWN_NAL;
863
864 break;
865 }
866 }
867 }
868
869 return i4_error_code;
870 }
871