1 /******************************************************************************
2  *
3  * Copyright (C) 2015 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 /*  File Name         : mpeg2dec_api_utils.c                                 */
23 /*                                                                           */
24 /*                                                                           */
25 /*  Description       : This file defines the API interface for MPEG2 Decoder*/
26 /*                                                                           */
27 /*  List of Functions : <List the functions defined in this file>            */
28 /*                                                                           */
29 /*  Issues / Problems : None                                                 */
30 /*                                                                           */
31 /*  Revision History  :                                                      */
32 /*                                                                           */
33 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
34 /*         17 09 2007  Rajendra C Y       Creation                           */
35 /*                                                                           */
36 /*****************************************************************************/
37 
38 /*****************************************************************************/
39 /* File Includes                                                             */
40 /*****************************************************************************/
41 /* System include files */
42 
43 #include <stddef.h>
44 #include <stdio.h>
45 #include <string.h>
46 
47 /* User include files */
48 #include "iv_datatypedef.h"
49 #include "iv.h"
50 #include "ivd.h"
51 #include "ithread.h"
52 
53 #include "impeg2_job_queue.h"
54 #include "impeg2_macros.h"
55 #include "impeg2_buf_mgr.h"
56 #include "impeg2_disp_mgr.h"
57 #include "impeg2_defs.h"
58 #include "impeg2_platform_macros.h"
59 #include "impeg2_inter_pred.h"
60 #include "impeg2_idct.h"
61 #include "impeg2_format_conv.h"
62 #include "impeg2_mem_func.h"
63 
64 #include "impeg2d.h"
65 #include "impeg2d_api.h"
66 #include "impeg2d_bitstream.h"
67 #include "impeg2d_debug.h"
68 #include "impeg2d_structs.h"
69 #include "impeg2d_mc.h"
70 #include "impeg2d_pic_proc.h"
71 #include "impeg2d_dec_hdr.h"
72 
73 void impeg2d_next_start_code(dec_state_t *ps_dec);
74 void impeg2d_next_code(dec_state_t *ps_dec, UWORD32 u4_start_code_val);
75 
76 /*****************************************************************************/
77 /*                                                                           */
78 /*  Function Name : impeg2d_dec_hdr                                      */
79 /*                                                                           */
80 /*  Description   :                                                          */
81 /*  Inputs        :                                                          */
82 /*  Globals       :                                                          */
83 /*  Processing    :                                                          */
84 /*  Outputs       :                                                          */
85 /*  Returns       :                                                          */
86 /*                                                                           */
87 /*  Issues        :                                                          */
88 /*                                                                           */
89 /*  Revision History:                                                        */
90 /*                                                                           */
91 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
92 /*         17 09 2007  Rajendra C Y          Draft                           */
93 /*                                                                           */
94 /*****************************************************************************/
impeg2d_dec_hdr(void * pv_dec,impeg2d_video_decode_ip_t * ps_ip,impeg2d_video_decode_op_t * ps_op)95 void impeg2d_dec_hdr(void *pv_dec,impeg2d_video_decode_ip_t *ps_ip,
96                  impeg2d_video_decode_op_t *ps_op)
97 {
98 
99     UWORD32 u4_bits_read;
100     dec_state_t *ps_dec;
101 
102     ps_dec = (dec_state_t *)pv_dec;
103     ps_op->s_ivd_video_decode_op_t.u4_error_code = 0;
104 
105     impeg2d_bit_stream_init(&(ps_dec->s_bit_stream),ps_ip->s_ivd_video_decode_ip_t.pv_stream_buffer,
106         ps_ip->s_ivd_video_decode_ip_t.u4_num_Bytes);
107 
108     {
109         {
110             IMPEG2D_ERROR_CODES_T e_error;
111             e_error = impeg2d_process_video_header(ps_dec);
112             if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
113             {
114                 ps_op->s_ivd_video_decode_op_t.u4_error_code    = e_error;
115 
116                 u4_bits_read     = impeg2d_bit_stream_num_bits_read(&ps_dec->s_bit_stream);
117 
118                 ps_op->s_ivd_video_decode_op_t.u4_num_bytes_consumed = u4_bits_read>> 3;
119                 if(ps_op->s_ivd_video_decode_op_t.u4_num_bytes_consumed > ps_ip->s_ivd_video_decode_ip_t.u4_num_Bytes)
120                 {
121                     ps_op->s_ivd_video_decode_op_t.u4_num_bytes_consumed = ps_ip->s_ivd_video_decode_ip_t.u4_num_Bytes;
122                 }
123                 if(ps_op->s_ivd_video_decode_op_t.u4_error_code == 0)
124                     ps_op->s_ivd_video_decode_op_t.u4_error_code = e_error;
125 
126 
127                 impeg2d_next_code(ps_dec, SEQUENCE_HEADER_CODE);
128                 return;
129             }
130         }
131         ps_op->s_ivd_video_decode_op_t.u4_pic_ht = ps_dec->u2_vertical_size;
132         ps_op->s_ivd_video_decode_op_t.u4_pic_wd = ps_dec->u2_horizontal_size;
133 
134         ps_op->s_ivd_video_decode_op_t.e_pic_type            = IV_NA_FRAME;
135         ps_op->s_ivd_video_decode_op_t.u4_error_code        = IV_SUCCESS;
136 
137         u4_bits_read     = impeg2d_bit_stream_num_bits_read(&ps_dec->s_bit_stream);
138         ps_op->s_ivd_video_decode_op_t.u4_num_bytes_consumed = u4_bits_read>> 3;
139         if(ps_op->s_ivd_video_decode_op_t.u4_num_bytes_consumed > ps_ip->s_ivd_video_decode_ip_t.u4_num_Bytes)
140         {
141             ps_op->s_ivd_video_decode_op_t.u4_num_bytes_consumed = ps_ip->s_ivd_video_decode_ip_t.u4_num_Bytes;
142         }
143         ps_op->s_ivd_video_decode_op_t.u4_frame_decoded_flag = 0;
144         /* MOD */
145         ps_dec->u2_header_done = 1;
146 
147     }
148 }
149 
150 /*****************************************************************************/
151 /*                                                                           */
152 /*  Function Name : impeg2d_dec_frm                                         */
153 /*                                                                           */
154 /*  Description   :                                                          */
155 /*  Inputs        :                                                          */
156 /*  Globals       :                                                          */
157 /*  Processing    :                                                          */
158 /*  Outputs       :                                                          */
159 /*  Returns       :                                                          */
160 /*                                                                           */
161 /*  Issues        :                                                          */
162 /*                                                                           */
163 /*  Revision History:                                                        */
164 /*                                                                           */
165 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
166 /*         17 09 2007  Rajendra C Y          Draft                           */
167 /*                                                                           */
168 /*****************************************************************************/
impeg2d_dec_frm(void * pv_dec,impeg2d_video_decode_ip_t * ps_ip,impeg2d_video_decode_op_t * ps_op)169 void impeg2d_dec_frm(void *pv_dec,impeg2d_video_decode_ip_t *ps_ip,
170                  impeg2d_video_decode_op_t *ps_op)
171 {
172 
173 
174     stream_t *ps_stream;
175     UWORD32 u4_size = ps_ip->s_ivd_video_decode_ip_t.u4_num_Bytes;
176 
177     dec_state_t *ps_dec;
178 
179     ps_dec = (dec_state_t *)pv_dec;
180     ps_op->s_ivd_video_decode_op_t.u4_error_code = 0;
181 
182     IMPEG2D_FRM_NUM_SET();
183 
184     ps_dec->pu1_inp_bits_buf = ps_ip->s_ivd_video_decode_ip_t.pv_stream_buffer;
185     ps_dec->u4_num_inp_bytes = u4_size;
186     ps_stream  = &ps_dec->s_bit_stream;
187 
188 
189     impeg2d_bit_stream_init(ps_stream,ps_ip->s_ivd_video_decode_ip_t.pv_stream_buffer,u4_size);
190 
191     /* @ */ /* Updating the bufferID */
192 
193     ps_dec->u4_xdmBufID     = ps_ip->s_ivd_video_decode_ip_t.u4_ts;
194 
195     {
196         IMPEG2D_ERROR_CODES_T e_error;
197         /* Process the Bitstream */
198         e_error = impeg2d_process_video_bit_stream(ps_dec);
199         if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
200         {
201             ps_op->s_ivd_video_decode_op_t.u4_error_code    = e_error;
202 
203             if ((IMPEG2D_ERROR_CODES_T) IVD_RES_CHANGED == e_error)
204             {
205                 ps_op->s_ivd_video_decode_op_t.u4_num_bytes_consumed = 0;
206                 ps_dec->u2_header_done = 0;
207             }
208             else if (IMPEG2D_UNSUPPORTED_DIMENSIONS == e_error)
209             {
210                 ps_op->s_ivd_video_decode_op_t.u4_num_bytes_consumed = 0;
211                 ps_dec->u2_header_done = 0;
212 
213                 ps_op->s_ivd_video_decode_op_t.u4_pic_ht = ps_dec->u2_reinit_max_height;
214                 ps_op->s_ivd_video_decode_op_t.u4_pic_wd = ps_dec->u2_reinit_max_width;
215             }
216             else
217             {
218                 if(ps_dec->i4_num_cores > 1)
219                     ps_op->s_ivd_video_decode_op_t.u4_num_bytes_consumed = ps_dec->i4_bytes_consumed;
220                 else
221                 {
222                     ps_op->s_ivd_video_decode_op_t.u4_num_bytes_consumed = (ps_dec->s_bit_stream.u4_offset + 7) >> 3;
223                     ps_op->s_ivd_video_decode_op_t.u4_num_bytes_consumed -= ((size_t)ps_dec->s_bit_stream.pv_bs_buf & 3);
224                 }
225 
226                 if(ps_op->s_ivd_video_decode_op_t.u4_num_bytes_consumed
227                                 > ps_ip->s_ivd_video_decode_ip_t.u4_num_Bytes)
228                 {
229                     ps_op->s_ivd_video_decode_op_t.u4_num_bytes_consumed =
230                                     ps_ip->s_ivd_video_decode_ip_t.u4_num_Bytes;
231                 }
232 
233                 impeg2d_next_start_code(ps_dec);
234             }
235 
236             if(ps_op->s_ivd_video_decode_op_t.u4_error_code == 0)
237             {
238                 ps_op->s_ivd_video_decode_op_t.u4_error_code = e_error;
239             }
240 
241             return;
242         }
243     }
244     /**************************************************************************/
245     /* Remove the bytes left till next start code is encountered              */
246     /**************************************************************************/
247     ps_op->s_ivd_video_decode_op_t.u4_error_code  = IV_SUCCESS;
248 
249     if(ps_dec->i4_num_cores > 1)
250         ps_op->s_ivd_video_decode_op_t.u4_num_bytes_consumed = ps_dec->i4_bytes_consumed;
251     else
252     {
253         ps_op->s_ivd_video_decode_op_t.u4_num_bytes_consumed = (ps_dec->s_bit_stream.u4_offset + 7) >> 3;
254         ps_op->s_ivd_video_decode_op_t.u4_num_bytes_consumed -= ((size_t)ps_dec->s_bit_stream.pv_bs_buf & 3);
255     }
256     if(ps_op->s_ivd_video_decode_op_t.u4_num_bytes_consumed > ps_ip->s_ivd_video_decode_ip_t.u4_num_Bytes)
257     {
258         ps_op->s_ivd_video_decode_op_t.u4_num_bytes_consumed = ps_ip->s_ivd_video_decode_ip_t.u4_num_Bytes;
259     }
260     ps_op->s_ivd_video_decode_op_t.u4_pic_ht = ps_dec->u2_vertical_size;
261     ps_op->s_ivd_video_decode_op_t.u4_pic_wd = ps_dec->u2_horizontal_size;
262 
263         switch(ps_dec->e_pic_type)
264         {
265         case I_PIC :
266             ps_op->s_ivd_video_decode_op_t.e_pic_type = IV_I_FRAME;
267             break;
268 
269         case P_PIC:
270             ps_op->s_ivd_video_decode_op_t.e_pic_type = IV_P_FRAME;
271             break;
272 
273         case B_PIC:
274             ps_op->s_ivd_video_decode_op_t.e_pic_type = IV_B_FRAME;
275             break;
276 
277         case D_PIC:
278             ps_op->s_ivd_video_decode_op_t.e_pic_type = IV_I_FRAME;
279             break;
280 
281         default :
282             ps_op->s_ivd_video_decode_op_t.e_pic_type = IV_FRAMETYPE_DEFAULT;
283             break;
284         }
285 
286         ps_op->s_ivd_video_decode_op_t.u4_frame_decoded_flag = ps_dec->i4_frame_decoded;
287         ps_op->s_ivd_video_decode_op_t.u4_new_seq = 0;
288         ps_op->s_ivd_video_decode_op_t.u4_error_code = ps_dec->u4_error_code;
289 
290 
291 }
292