xref: /aosp_15_r20/external/libavc/decoder/mvc/imvcd_nalu_parser.c (revision 495ae853bb871d1e5a258cb02c2cc13cde8ddb9a)
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         UWORD32 u1_log2_max_pic_order_cnt_lsb_minus4 =
221             ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
222 
223         if(u1_log2_max_pic_order_cnt_lsb_minus4 > (MAX_BITS_IN_POC_LSB - 4))
224         {
225             return ERROR_INV_SPS_PPS_T;
226         }
227 
228         ps_subset_sps->s_sps_data.u1_log2_max_pic_order_cnt_lsb_minus =
229             4 + u1_log2_max_pic_order_cnt_lsb_minus4;
230 
231         ps_subset_sps->s_sps_data.i4_max_pic_order_cntLsb =
232             (1 << ps_subset_sps->s_sps_data.u1_log2_max_pic_order_cnt_lsb_minus);
233     }
234     else if(ps_subset_sps->s_sps_data.u1_pic_order_cnt_type == 1)
235     {
236         ps_subset_sps->s_sps_data.u1_delta_pic_order_always_zero_flag =
237             ih264d_get_bit_h264(ps_bitstrm);
238 
239         ps_subset_sps->s_sps_data.i4_ofst_for_non_ref_pic =
240             ih264d_sev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
241 
242         ps_subset_sps->s_sps_data.i4_ofst_for_top_to_bottom_field =
243             ih264d_sev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
244 
245         ps_subset_sps->s_sps_data.u1_num_ref_frames_in_pic_order_cnt_cycle =
246             ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
247 
248         if(ps_subset_sps->s_sps_data.u1_num_ref_frames_in_pic_order_cnt_cycle > MVC_MAX_REF_PICS)
249         {
250             return ERROR_INV_SPS_PPS_T;
251         }
252 
253         for(i = 0; i < ps_subset_sps->s_sps_data.u1_num_ref_frames_in_pic_order_cnt_cycle; i++)
254         {
255             ps_subset_sps->s_sps_data.i4_ofst_for_ref_frame[i] =
256                 ih264d_sev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
257         }
258     }
259 
260     ps_subset_sps->s_sps_data.u1_num_ref_frames = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
261 
262     if((ps_subset_sps->s_sps_data.u1_num_ref_frames > MVC_MAX_REF_PICS))
263     {
264         return ERROR_NUM_REF;
265     }
266 
267     ps_subset_sps->s_sps_data.u1_gaps_in_frame_num_value_allowed_flag =
268         ih264d_get_bit_h264(ps_bitstrm);
269 
270     ps_subset_sps->s_sps_data.u2_frm_wd_in_mbs = 1 + ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
271 
272     if(ps_subset_sps->s_sps_data.u2_frm_wd_in_mbs > (H264_MAX_FRAME_WIDTH >> 4))
273     {
274         return IVD_STREAM_WIDTH_HEIGHT_NOT_SUPPORTED;
275     }
276 
277     ps_subset_sps->s_sps_data.u2_frm_ht_in_mbs = 1 + ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
278 
279     if(ps_subset_sps->s_sps_data.u2_frm_ht_in_mbs > (H264_MAX_FRAME_HEIGHT >> 4))
280     {
281         return IVD_STREAM_WIDTH_HEIGHT_NOT_SUPPORTED;
282     }
283 
284     ps_subset_sps->s_sps_data.u2_max_mb_addr =
285         ps_subset_sps->s_sps_data.u2_frm_wd_in_mbs * ps_subset_sps->s_sps_data.u2_frm_ht_in_mbs - 1;
286 
287     ps_subset_sps->s_sps_data.u2_total_num_of_mbs = ps_subset_sps->s_sps_data.u2_max_mb_addr + 1;
288 
289     ps_subset_sps->s_sps_data.u1_frame_mbs_only_flag = ih264d_get_bit_h264(ps_bitstrm);
290 
291     if(!ps_subset_sps->s_sps_data.u1_frame_mbs_only_flag)
292     {
293         return ERROR_FEATURE_UNAVAIL;
294     }
295 
296     ps_subset_sps->s_sps_data.u1_mb_aff_flag = 0;
297 
298     ps_subset_sps->s_sps_data.u1_direct_8x8_inference_flag = ih264d_get_bit_h264(ps_bitstrm);
299 
300     /* Frame cropping flag */
301     u4_temp = ih264d_get_bit_h264(ps_bitstrm);
302 
303     if(u4_temp)
304     {
305         ps_subset_sps->s_disp_offsets.u2_left_offset =
306             ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
307         ps_subset_sps->s_disp_offsets.u2_right_offset =
308             ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
309         ps_subset_sps->s_disp_offsets.u2_top_offset = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
310         ps_subset_sps->s_disp_offsets.u2_bottom_offset =
311             ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
312     }
313     else
314     {
315         memset(&ps_subset_sps->s_disp_offsets, 0, sizeof(ps_subset_sps->s_disp_offsets));
316     }
317 
318     if(ps_sps->u2_frm_wd_in_mbs != ps_subset_sps->s_sps_data.u2_frm_wd_in_mbs)
319     {
320         ps_view_ctxt->u1_res_changed = 1;
321 
322         return IVD_RES_CHANGED;
323     }
324 
325     if(ps_sps->u2_frm_ht_in_mbs != ps_subset_sps->s_sps_data.u2_frm_ht_in_mbs)
326     {
327         ps_view_ctxt->u1_res_changed = 1;
328 
329         return IVD_RES_CHANGED;
330     }
331 
332     if(ps_view_ctxt->u2_disp_width != (ps_subset_sps->s_sps_data.u2_frm_wd_in_mbs * MB_SIZE -
333                                        ps_subset_sps->s_disp_offsets.u2_left_offset -
334                                        ps_subset_sps->s_disp_offsets.u2_right_offset))
335     {
336         ps_view_ctxt->u1_res_changed = 1;
337 
338         return IVD_RES_CHANGED;
339     }
340 
341     if(ps_view_ctxt->u2_disp_height != (ps_subset_sps->s_sps_data.u2_frm_ht_in_mbs * MB_SIZE -
342                                         ps_subset_sps->s_disp_offsets.u2_top_offset -
343                                         ps_subset_sps->s_disp_offsets.u2_bottom_offset))
344     {
345         ps_view_ctxt->u1_res_changed = 1;
346 
347         return IVD_RES_CHANGED;
348     }
349 
350     ps_subset_sps->s_sps_data.u1_vui_parameters_present_flag = ih264d_get_bit_h264(ps_bitstrm);
351 
352     if(ps_subset_sps->s_sps_data.u1_vui_parameters_present_flag)
353     {
354         i4_error_code = ih264d_parse_vui_parametres(&ps_subset_sps->s_sps_data.s_vui, ps_bitstrm);
355 
356         if(i4_error_code != OK)
357         {
358             return i4_error_code;
359         }
360 
361         if(ps_subset_sps->s_sps_data.s_vui.u1_bitstream_restriction_flag &&
362            (ps_sps->s_vui.u4_num_reorder_frames !=
363             ps_subset_sps->s_sps_data.s_vui.u4_num_reorder_frames))
364         {
365             ps_view_ctxt->u1_res_changed = 1;
366 
367             return IVD_RES_CHANGED;
368         }
369     }
370 
371     if(ih264d_get_bit_h264(ps_bitstrm) != 1)
372     {
373         return ERROR_INV_SPS_PPS_T;
374     }
375 
376     u2_num_views_m1 = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
377 
378     if(u2_num_views_m1 >= MAX_NUM_VIEWS)
379     {
380         return ERROR_INVALID_SEQ_PARAM;
381     }
382 
383     ps_subset_sps->s_sps_mvc_ext.u2_num_views = 1 + u2_num_views_m1;
384 
385     if(ps_view_ctxt->i4_decode_header)
386     {
387         ps_mvcd_ctxt->u2_num_views = MAX(ps_mvcd_ctxt->u2_num_views, 1 + u2_num_views_m1);
388     }
389     else if(ps_mvcd_ctxt->u2_num_views != (1 + u2_num_views_m1))
390     {
391         return ERROR_INVALID_SEQ_PARAM;
392     }
393 
394     for(i = 0; i <= u2_num_views_m1; i++)
395     {
396         ps_subset_sps->s_sps_mvc_ext.au2_view_ids[i] =
397             ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
398     }
399 
400     for(i = 0; i < 2; i++)
401     {
402         ps_subset_sps->s_sps_mvc_ext.as_anchor_ref_data[i][0].u1_num_refs = 0;
403         ps_subset_sps->s_sps_mvc_ext.as_non_anchor_ref_data[i][0].u1_num_refs = 0;
404     }
405 
406     for(i = 1; i <= u2_num_views_m1; i++)
407     {
408         u1_num_refs = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
409         ps_subset_sps->s_sps_mvc_ext.as_anchor_ref_data[0][i].u1_num_refs = u1_num_refs;
410 
411         if(u1_num_refs > MAX_NUM_IVP_REFS)
412         {
413             return ERROR_INVALID_SEQ_PARAM;
414         }
415 
416         for(j = 0; j < u1_num_refs; j++)
417         {
418             ps_subset_sps->s_sps_mvc_ext.as_anchor_ref_data[0][i].au2_ref_view_ids[j] =
419                 ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
420         }
421 
422         u1_num_refs = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
423         ps_subset_sps->s_sps_mvc_ext.as_anchor_ref_data[1][i].u1_num_refs = u1_num_refs;
424 
425         if(u1_num_refs > MAX_NUM_IVP_REFS)
426         {
427             return ERROR_INVALID_SEQ_PARAM;
428         }
429 
430         for(j = 0; j < u1_num_refs; j++)
431         {
432             ps_subset_sps->s_sps_mvc_ext.as_anchor_ref_data[1][i].au2_ref_view_ids[j] =
433                 ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
434         }
435     }
436 
437     for(i = 1; i <= u2_num_views_m1; i++)
438     {
439         u1_num_refs = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
440         ps_subset_sps->s_sps_mvc_ext.as_non_anchor_ref_data[0][i].u1_num_refs = u1_num_refs;
441 
442         if(u1_num_refs > MAX_NUM_IVP_REFS)
443         {
444             return ERROR_INVALID_SEQ_PARAM;
445         }
446 
447         for(j = 0; j < u1_num_refs; j++)
448         {
449             ps_subset_sps->s_sps_mvc_ext.as_non_anchor_ref_data[0][i].au2_ref_view_ids[j] =
450                 ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
451         }
452 
453         u1_num_refs = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
454         ps_subset_sps->s_sps_mvc_ext.as_non_anchor_ref_data[1][i].u1_num_refs = u1_num_refs;
455 
456         if(u1_num_refs > MAX_NUM_IVP_REFS)
457         {
458             return ERROR_INVALID_SEQ_PARAM;
459         }
460 
461         for(j = 0; j < u1_num_refs; j++)
462         {
463             ps_subset_sps->s_sps_mvc_ext.as_non_anchor_ref_data[1][i].au2_ref_view_ids[j] =
464                 ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
465         }
466     }
467 
468     u1_num_level_values_signalled_m1 = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
469     ps_subset_sps->s_sps_mvc_ext.u1_num_level_values_signalled =
470         u1_num_level_values_signalled_m1 + 1;
471 
472     if(u1_num_level_values_signalled_m1 >= MAX_NUM_LEVEL_VALUES_SIGNALLED)
473     {
474         return ERROR_INVALID_SEQ_PARAM;
475     }
476 
477     for(i = 0; i <= u1_num_level_values_signalled_m1; i++)
478     {
479         ps_subset_sps->s_sps_mvc_ext.as_mvc_level_info[i].u4_level_idc =
480             ih264d_get_bits_h264(ps_bitstrm, 8);
481 
482         u2_num_ops_m1 = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
483         ps_subset_sps->s_sps_mvc_ext.as_mvc_level_info->as_mvc_op_data[i].u2_num_ops =
484             1 + u2_num_ops_m1;
485 
486         if(u2_num_ops_m1 >= MAX_NUM_OPERATING_POINTS)
487         {
488             return ERROR_INVALID_SEQ_PARAM;
489         }
490 
491         for(j = 0; j <= u2_num_ops_m1; j++)
492         {
493             UWORD16 u2_num_target_views_m1;
494 
495             ps_subset_sps->s_sps_mvc_ext.as_mvc_level_info->as_mvc_op_data[j].u1_temporal_id =
496                 ih264d_get_bits_h264(ps_bitstrm, 3);
497 
498             u2_num_target_views_m1 = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
499 
500             ps_subset_sps->s_sps_mvc_ext.as_mvc_level_info->as_mvc_op_data[j].u2_num_target_views =
501                 1 + u2_num_target_views_m1;
502 
503             if(u2_num_target_views_m1 >= MAX_NUM_VIEWS)
504             {
505                 return ERROR_INVALID_SEQ_PARAM;
506             }
507 
508             for(k = 0; k <= u2_num_target_views_m1; k++)
509             {
510                 ps_subset_sps->s_sps_mvc_ext.as_mvc_level_info->as_mvc_op_data[j]
511                     .au2_target_view_ids[k] = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
512             }
513 
514             ps_subset_sps->s_sps_mvc_ext.as_mvc_level_info->as_mvc_op_data[j].u2_num_views =
515                 (UWORD16) 1 + ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
516         }
517     }
518 
519     ps_subset_sps->u1_mvc_vui_parameters_present_flag = ih264d_get_bit_h264(ps_bitstrm);
520 
521     if(ps_subset_sps->u1_mvc_vui_parameters_present_flag)
522     {
523         return ERROR_INV_SPS_PPS_T;
524     }
525 
526     /* In case bitstream read has exceeded the filled size, then
527      return an error */
528     if(EXCEED_OFFSET(ps_bitstrm))
529     {
530         return ERROR_INV_SPS_PPS_T;
531     }
532 
533     ps_subset_sps->s_sps_data.u1_is_valid = 1;
534 
535     /* This ensures PPS has valid data in SPS array for reference */
536     ps_view_ctxt->ps_sps[ps_subset_sps->s_sps_data.u1_seq_parameter_set_id] =
537         ps_subset_sps->s_sps_data;
538 
539     ps_mvcd_ctxt->u1_num_subset_sps++;
540 
541     return OK;
542 }
543 
544 /* This function removes emulation byte "0x03" from bitstream(EBSP to RBSP).
545    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)546 static WORD32 imvcd_transform_nalu(dec_bit_stream_t *ps_bitstrm, UWORD8 *pu1_nal_unit,
547                                    UWORD32 u4_numbytes_in_nal_unit)
548 {
549     UWORD32 ui4_word;
550     UWORD8 u1_cur_byte;
551 
552     static const UWORD32 u4_num_bytes_in_word = sizeof(ui4_word) / sizeof(u1_cur_byte);
553     UWORD32 u4_num_bytes_in_rbsp = 0;
554     WORD32 i = 0, j;
555     WORD8 c_count = 0;
556     UWORD32 *puc_bitstream_buffer = (UWORD32 *) pu1_nal_unit;
557     UWORD8 u1_nal_header_size = 1;
558     UWORD8 u1_num_bytes_copied = 0;
559 
560     ps_bitstrm->pu4_buffer = puc_bitstream_buffer;
561 
562     ui4_word = *pu1_nal_unit++;
563     u1_num_bytes_copied++;
564 
565     if((NAL_UNIT_TYPE(ui4_word) == PREFIX_NAL) ||
566        (NAL_UNIT_TYPE(ui4_word) == CODED_SLICE_EXTENSION))
567     {
568         u1_nal_header_size += 3;
569     }
570 
571     for(j = 0; j < 2; j++)
572     {
573         u1_cur_byte = *pu1_nal_unit++;
574 
575         ui4_word = ((ui4_word << 8) | u1_cur_byte);
576         u1_num_bytes_copied++;
577 
578         c_count++;
579         u4_num_bytes_in_rbsp++;
580 
581         if(u1_cur_byte != 0x00)
582         {
583             c_count = 0;
584         }
585     }
586 
587     if(u4_numbytes_in_nal_unit > 2)
588     {
589         i = ((u4_numbytes_in_nal_unit - 3));
590     }
591 
592     for(; i > 8; i -= 4)
593     {
594         // loop 0
595         u1_cur_byte = *pu1_nal_unit++;
596 
597         if(c_count == NUM_OF_ZERO_BYTES_BEFORE_START_CODE &&
598            u1_cur_byte == EMULATION_PREVENTION_BYTE)
599         {
600             c_count = 0;
601             u1_cur_byte = *pu1_nal_unit++;
602             i--;
603         }
604 
605         ui4_word = ((ui4_word << 8) | u1_cur_byte);
606         u1_num_bytes_copied++;
607         if(u4_num_bytes_in_word == u1_num_bytes_copied)
608         {
609             *puc_bitstream_buffer = ui4_word;
610             puc_bitstream_buffer++;
611             u1_num_bytes_copied = 0;
612         }
613 
614         c_count++;
615         if(u1_cur_byte != 0x00) c_count = 0;
616 
617         // loop 1
618         u1_cur_byte = *pu1_nal_unit++;
619 
620         if(c_count == NUM_OF_ZERO_BYTES_BEFORE_START_CODE &&
621            u1_cur_byte == EMULATION_PREVENTION_BYTE)
622         {
623             c_count = 0;
624             u1_cur_byte = *pu1_nal_unit++;
625             i--;
626         }
627         ui4_word = ((ui4_word << 8) | u1_cur_byte);
628         u1_num_bytes_copied++;
629         if(u4_num_bytes_in_word == u1_num_bytes_copied)
630         {
631             *puc_bitstream_buffer = ui4_word;
632             puc_bitstream_buffer++;
633             u1_num_bytes_copied = 0;
634         }
635 
636         c_count++;
637         if(u1_cur_byte != 0x00) c_count = 0;
638 
639         // loop 2
640         u1_cur_byte = *pu1_nal_unit++;
641 
642         if(c_count == NUM_OF_ZERO_BYTES_BEFORE_START_CODE &&
643            u1_cur_byte == EMULATION_PREVENTION_BYTE)
644         {
645             c_count = 0;
646             u1_cur_byte = *pu1_nal_unit++;
647             i--;
648         }
649 
650         ui4_word = ((ui4_word << 8) | u1_cur_byte);
651         u1_num_bytes_copied++;
652         if(u4_num_bytes_in_word == u1_num_bytes_copied)
653         {
654             *puc_bitstream_buffer = ui4_word;
655             puc_bitstream_buffer++;
656             u1_num_bytes_copied = 0;
657         }
658 
659         c_count++;
660         if(u1_cur_byte != 0x00) c_count = 0;
661 
662         // loop 3
663         u1_cur_byte = *pu1_nal_unit++;
664 
665         if(c_count == NUM_OF_ZERO_BYTES_BEFORE_START_CODE &&
666            u1_cur_byte == EMULATION_PREVENTION_BYTE)
667         {
668             c_count = 0;
669             u1_cur_byte = *pu1_nal_unit++;
670             i--;
671         }
672 
673         ui4_word = ((ui4_word << 8) | u1_cur_byte);
674         u1_num_bytes_copied++;
675         if(u4_num_bytes_in_word == u1_num_bytes_copied)
676         {
677             *puc_bitstream_buffer = ui4_word;
678             puc_bitstream_buffer++;
679             u1_num_bytes_copied = 0;
680         }
681 
682         c_count++;
683         if(u1_cur_byte != 0x00) c_count = 0;
684 
685         u4_num_bytes_in_rbsp += 4;
686     }
687 
688     for(; i > 0; i--)
689     {
690         u1_cur_byte = *pu1_nal_unit++;
691 
692         if(c_count == NUM_OF_ZERO_BYTES_BEFORE_START_CODE &&
693            u1_cur_byte == EMULATION_PREVENTION_BYTE)
694         {
695             c_count = 0;
696             i--;
697             u1_cur_byte = *pu1_nal_unit++;
698         }
699 
700         ui4_word = ((ui4_word << 8) | u1_cur_byte);
701         u4_num_bytes_in_rbsp++;
702 
703         if((u4_num_bytes_in_rbsp & 0x03) == 0x03)
704         {
705             *puc_bitstream_buffer = ui4_word;
706             puc_bitstream_buffer++;
707         }
708         c_count++;
709         if(u1_cur_byte != 0x00) c_count = 0;
710     }
711 
712     *puc_bitstream_buffer = (ui4_word << ((3 - (((u4_num_bytes_in_rbsp << 30) >> 30))) << 3));
713     ps_bitstrm->u4_ofst = 0;
714     ps_bitstrm->u4_max_ofst = ((u4_num_bytes_in_rbsp + u1_nal_header_size) << 3);
715 
716     return (u4_num_bytes_in_rbsp);
717 }
718 
imvcd_nalu_parser(mvc_dec_ctxt_t * ps_mvcd_ctxt,UWORD8 * pu1_bitstream_buf,UWORD32 i4_nalu_length)719 WORD32 imvcd_nalu_parser(mvc_dec_ctxt_t *ps_mvcd_ctxt, UWORD8 *pu1_bitstream_buf,
720                          UWORD32 i4_nalu_length)
721 {
722     AVC_EXT_NALU_ID_T e_nalu_id;
723 
724     UWORD8 u1_first_byte;
725 
726     dec_struct_t *ps_view_ctxt = &ps_mvcd_ctxt->s_view_dec_ctxt;
727     dec_bit_stream_t *ps_bitstrm = ps_view_ctxt->ps_bitstrm;
728 
729     WORD32 i4_error_code = NOT_OK;
730 
731     if((NULL != pu1_bitstream_buf) && (i4_nalu_length > 0))
732     {
733         imvcd_transform_nalu(ps_bitstrm, pu1_bitstream_buf, i4_nalu_length);
734 
735         u1_first_byte = ih264d_get_bits_h264(ps_bitstrm, 8);
736 
737         if(NAL_FORBIDDEN_BIT(u1_first_byte))
738         {
739             return NOT_OK;
740         }
741 
742         e_nalu_id = NAL_UNIT_TYPE(u1_first_byte);
743         ps_view_ctxt->u1_nal_unit_type = e_nalu_id;
744 
745         // if any other nal unit other than slice nal is encountered in between a
746         // frame break out of loop without consuming header
747         if((ps_view_ctxt->u4_slice_start_code_found == 1) &&
748            (ps_view_ctxt->u1_pic_decode_done != 1) && is_slice_nalu_type(e_nalu_id))
749         {
750             return ERROR_INCOMPLETE_FRAME;
751         }
752 
753         switch(e_nalu_id)
754         {
755             case PREFIX_NAL:
756             {
757                 if(!ps_view_ctxt->i4_decode_header)
758                 {
759                     if(1 == ih264d_get_bit_h264(ps_bitstrm))
760                     {
761                         return IVD_INVALID_BITSTREAM;
762                     }
763 
764                     i4_error_code = imvcd_nalu_mvc_ext_parser(ps_mvcd_ctxt, ps_bitstrm);
765 
766                     if(i4_error_code != OK)
767                     {
768                         return i4_error_code;
769                     }
770                 }
771 
772                 break;
773             }
774             case SUBSET_SPS:
775             {
776                 ih264d_rbsp_to_sodb(ps_view_ctxt->ps_bitstrm);
777 
778                 i4_error_code = imvcd_parse_subset_sps(ps_mvcd_ctxt, ps_bitstrm);
779 
780                 if(OK != i4_error_code)
781                 {
782                     return i4_error_code;
783                 }
784 
785                 ps_view_ctxt->i4_header_decoded |= 1 << SUBSET_SPS;
786 
787                 break;
788             }
789             case SLICE_NON_IDR:
790             case SLICE_IDR:
791             {
792                 if(!ps_view_ctxt->i4_decode_header)
793                 {
794                     if(is_header_decoded(ps_view_ctxt->i4_header_decoded, SPS) &&
795                        is_header_decoded(ps_view_ctxt->i4_header_decoded, PPS))
796                     {
797                         nalu_mvc_ext_t *ps_nalu_mvc_ext = imvcd_get_cur_nalu_mvc_ext(ps_mvcd_ctxt);
798 
799                         ps_view_ctxt->u4_slice_start_code_found = 1;
800 
801                         if((0 == ps_mvcd_ctxt->u2_num_views_decoded) &&
802                            !ps_nalu_mvc_ext->u1_inter_view_flag)
803                         {
804                             ps_nalu_mvc_ext->u1_inter_view_flag = 1;
805                         }
806 
807                         ih264d_rbsp_to_sodb(ps_view_ctxt->ps_bitstrm);
808 
809                         i4_error_code = imvcd_parse_decode_slice(ps_mvcd_ctxt);
810 
811                         if(i4_error_code != OK)
812                         {
813                             return i4_error_code;
814                         }
815                     }
816                     else
817                     {
818                         return IVD_INVALID_BITSTREAM;
819                     }
820                 }
821 
822                 break;
823             }
824             case CODED_SLICE_EXTENSION:
825             {
826                 if(!ps_view_ctxt->i4_decode_header)
827                 {
828                     if(is_header_decoded(ps_view_ctxt->i4_header_decoded, SPS) &&
829                        is_header_decoded(ps_view_ctxt->i4_header_decoded, PPS) &&
830                        is_header_decoded(ps_view_ctxt->i4_header_decoded, SUBSET_SPS))
831                     {
832                         ps_view_ctxt->u4_slice_start_code_found = 1;
833 
834                         if(1 == ih264d_get_bit_h264(ps_bitstrm))
835                         {
836                             return IVD_INVALID_BITSTREAM;
837                         }
838 
839                         i4_error_code = imvcd_nalu_mvc_ext_parser(ps_mvcd_ctxt, ps_bitstrm);
840 
841                         if(i4_error_code != OK)
842                         {
843                             return i4_error_code;
844                         }
845 
846                         ih264d_rbsp_to_sodb(ps_view_ctxt->ps_bitstrm);
847 
848                         i4_error_code = imvcd_parse_decode_slice(ps_mvcd_ctxt);
849 
850                         if(i4_error_code != OK)
851                         {
852                             return i4_error_code;
853                         }
854                     }
855                     else
856                     {
857                         return IVD_INVALID_BITSTREAM;
858                     }
859                 }
860 
861                 break;
862             }
863             default:
864             {
865                 i4_error_code = ERROR_UNKNOWN_NAL;
866 
867                 break;
868             }
869         }
870     }
871 
872     return i4_error_code;
873 }
874