/src/libmpeg2/decoder/impeg2d_decoder.c
Line | Count | Source (jump to first uncovered line) |
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 | | /*****************************************************************************/ |
95 | | void impeg2d_dec_hdr(void *pv_dec,impeg2d_video_decode_ip_t *ps_ip, |
96 | | impeg2d_video_decode_op_t *ps_op) |
97 | 19.6k | { |
98 | | |
99 | 19.6k | UWORD32 u4_bits_read; |
100 | 19.6k | dec_state_t *ps_dec; |
101 | 19.6k | UWORD32 u4_size = ps_ip->s_ivd_video_decode_ip_t.u4_num_Bytes; |
102 | | |
103 | 19.6k | ps_dec = (dec_state_t *)pv_dec; |
104 | 19.6k | ps_op->s_ivd_video_decode_op_t.u4_error_code = 0; |
105 | 19.6k | if (u4_size > MAX_BITSTREAM_BUFFER_SIZE) |
106 | 0 | { |
107 | 0 | u4_size = MAX_BITSTREAM_BUFFER_SIZE; |
108 | 0 | } |
109 | | |
110 | 19.6k | memcpy(ps_dec->pu1_input_buffer, ps_ip->s_ivd_video_decode_ip_t.pv_stream_buffer, u4_size); |
111 | | |
112 | 19.6k | impeg2d_bit_stream_init(&(ps_dec->s_bit_stream), ps_dec->pu1_input_buffer, |
113 | 19.6k | u4_size); |
114 | | |
115 | 19.6k | { |
116 | 19.6k | { |
117 | 19.6k | IMPEG2D_ERROR_CODES_T e_error; |
118 | 19.6k | e_error = impeg2d_process_video_header(ps_dec); |
119 | 19.6k | if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error) |
120 | 10.3k | { |
121 | 10.3k | ps_op->s_ivd_video_decode_op_t.u4_error_code = e_error; |
122 | | |
123 | 10.3k | u4_bits_read = impeg2d_bit_stream_num_bits_read(&ps_dec->s_bit_stream); |
124 | | |
125 | 10.3k | ps_op->s_ivd_video_decode_op_t.u4_num_bytes_consumed = u4_bits_read>> 3; |
126 | 10.3k | if(ps_op->s_ivd_video_decode_op_t.u4_num_bytes_consumed > ps_ip->s_ivd_video_decode_ip_t.u4_num_Bytes) |
127 | 1.11k | { |
128 | 1.11k | ps_op->s_ivd_video_decode_op_t.u4_num_bytes_consumed = ps_ip->s_ivd_video_decode_ip_t.u4_num_Bytes; |
129 | 1.11k | } |
130 | 10.3k | if(ps_op->s_ivd_video_decode_op_t.u4_error_code == 0) |
131 | 0 | ps_op->s_ivd_video_decode_op_t.u4_error_code = e_error; |
132 | | |
133 | 10.3k | if (IMPEG2D_UNSUPPORTED_DIMENSIONS == e_error) |
134 | 7.08k | { |
135 | 7.08k | ps_op->s_ivd_video_decode_op_t.u4_num_bytes_consumed = 0; |
136 | 7.08k | ps_dec->u2_header_done = 0; |
137 | | |
138 | 7.08k | ps_op->s_ivd_video_decode_op_t.u4_pic_ht = ps_dec->u2_reinit_max_height; |
139 | 7.08k | ps_op->s_ivd_video_decode_op_t.u4_pic_wd = ps_dec->u2_reinit_max_width; |
140 | 7.08k | } |
141 | 10.3k | impeg2d_next_code(ps_dec, SEQUENCE_HEADER_CODE); |
142 | 10.3k | return; |
143 | 10.3k | } |
144 | 19.6k | } |
145 | 9.31k | ps_op->s_ivd_video_decode_op_t.u4_pic_ht = ps_dec->u2_vertical_size; |
146 | 9.31k | ps_op->s_ivd_video_decode_op_t.u4_pic_wd = ps_dec->u2_horizontal_size; |
147 | | |
148 | 9.31k | ps_op->s_ivd_video_decode_op_t.e_pic_type = IV_NA_FRAME; |
149 | 9.31k | ps_op->s_ivd_video_decode_op_t.u4_error_code = IV_SUCCESS; |
150 | | |
151 | 9.31k | u4_bits_read = impeg2d_bit_stream_num_bits_read(&ps_dec->s_bit_stream); |
152 | 9.31k | ps_op->s_ivd_video_decode_op_t.u4_num_bytes_consumed = u4_bits_read>> 3; |
153 | 9.31k | if(ps_op->s_ivd_video_decode_op_t.u4_num_bytes_consumed > ps_ip->s_ivd_video_decode_ip_t.u4_num_Bytes) |
154 | 2.69k | { |
155 | 2.69k | ps_op->s_ivd_video_decode_op_t.u4_num_bytes_consumed = ps_ip->s_ivd_video_decode_ip_t.u4_num_Bytes; |
156 | 2.69k | } |
157 | 9.31k | ps_op->s_ivd_video_decode_op_t.u4_frame_decoded_flag = 0; |
158 | | |
159 | | /* Set the stride */ |
160 | 9.31k | if (0 == ps_dec->u4_frm_buf_stride) |
161 | 9.31k | { |
162 | 9.31k | ps_dec->u4_frm_buf_stride = ps_dec->u2_horizontal_size; |
163 | 9.31k | } |
164 | | /* MOD */ |
165 | 9.31k | ps_dec->u2_header_done = 1; |
166 | | |
167 | 9.31k | } |
168 | 9.31k | } |
169 | | |
170 | | /*****************************************************************************/ |
171 | | /* */ |
172 | | /* Function Name : impeg2d_dec_frm */ |
173 | | /* */ |
174 | | /* Description : */ |
175 | | /* Inputs : */ |
176 | | /* Globals : */ |
177 | | /* Processing : */ |
178 | | /* Outputs : */ |
179 | | /* Returns : */ |
180 | | /* */ |
181 | | /* Issues : */ |
182 | | /* */ |
183 | | /* Revision History: */ |
184 | | /* */ |
185 | | /* DD MM YYYY Author(s) Changes (Describe the changes made) */ |
186 | | /* 17 09 2007 Rajendra C Y Draft */ |
187 | | /* */ |
188 | | /*****************************************************************************/ |
189 | | void impeg2d_dec_frm(void *pv_dec,impeg2d_video_decode_ip_t *ps_ip, |
190 | | impeg2d_video_decode_op_t *ps_op) |
191 | 41.5k | { |
192 | | |
193 | | |
194 | 41.5k | stream_t *ps_stream; |
195 | 41.5k | UWORD32 u4_size = ps_ip->s_ivd_video_decode_ip_t.u4_num_Bytes; |
196 | | |
197 | 41.5k | dec_state_t *ps_dec; |
198 | | |
199 | 41.5k | ps_dec = (dec_state_t *)pv_dec; |
200 | 41.5k | ps_op->s_ivd_video_decode_op_t.u4_error_code = 0; |
201 | 41.5k | ps_dec->i4_bytes_consumed = 0; |
202 | 41.5k | ps_op->s_ivd_video_decode_op_t.u4_num_bytes_consumed = 0; |
203 | | |
204 | 41.5k | IMPEG2D_FRM_NUM_SET(); |
205 | 41.5k | if (u4_size > MAX_BITSTREAM_BUFFER_SIZE) |
206 | 0 | { |
207 | 0 | u4_size = MAX_BITSTREAM_BUFFER_SIZE; |
208 | 0 | } |
209 | | |
210 | 41.5k | memcpy(ps_dec->pu1_input_buffer, ps_ip->s_ivd_video_decode_ip_t.pv_stream_buffer, u4_size); |
211 | | |
212 | 41.5k | ps_dec->pu1_inp_bits_buf = ps_dec->pu1_input_buffer; |
213 | | |
214 | 41.5k | ps_dec->u4_num_inp_bytes = u4_size; |
215 | 41.5k | ps_stream = &ps_dec->s_bit_stream; |
216 | | |
217 | 41.5k | impeg2d_bit_stream_init(ps_stream, ps_dec->pu1_input_buffer, u4_size); |
218 | | |
219 | | /* @ */ /* Updating the bufferID */ |
220 | | |
221 | 41.5k | ps_dec->u4_xdmBufID = ps_ip->s_ivd_video_decode_ip_t.u4_ts; |
222 | | |
223 | 41.5k | { |
224 | 41.5k | IMPEG2D_ERROR_CODES_T e_error; |
225 | | /* Process the Bitstream */ |
226 | 41.5k | e_error = impeg2d_process_video_bit_stream(ps_dec); |
227 | 41.5k | if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error) |
228 | 15.6k | { |
229 | 15.6k | ps_op->s_ivd_video_decode_op_t.u4_error_code = e_error; |
230 | | |
231 | 15.6k | if ((IMPEG2D_ERROR_CODES_T) IVD_RES_CHANGED == e_error) |
232 | 1.80k | { |
233 | 1.80k | ps_op->s_ivd_video_decode_op_t.u4_num_bytes_consumed = ps_dec->i4_bytes_consumed; |
234 | 1.80k | ps_dec->u2_header_done = 0; |
235 | 1.80k | } |
236 | 13.8k | else if (IMPEG2D_UNSUPPORTED_DIMENSIONS == e_error) |
237 | 2.56k | { |
238 | 2.56k | ps_op->s_ivd_video_decode_op_t.u4_num_bytes_consumed = 0; |
239 | 2.56k | ps_dec->u2_header_done = 0; |
240 | | |
241 | 2.56k | ps_op->s_ivd_video_decode_op_t.u4_pic_ht = ps_dec->u2_reinit_max_height; |
242 | 2.56k | ps_op->s_ivd_video_decode_op_t.u4_pic_wd = ps_dec->u2_reinit_max_width; |
243 | 2.56k | } |
244 | 11.2k | else |
245 | 11.2k | { |
246 | 11.2k | if(ps_dec->i4_num_cores > 1 && 0 != ps_dec->i4_bytes_consumed) |
247 | 3.33k | { |
248 | | /* If the number of bytes consumed has been updated by |
249 | | * get_slice_pos function, then use that. Else, the bytes consumed is |
250 | | * calculated from the offset. The bytes consumed for multi-thread runs |
251 | | * is updated only into ps_dec->i4_bytes_consumed if the get_slice_pos |
252 | | * function has been called. If that function has not run, then we have |
253 | | * encountered an error but still have to consume the bytes in header |
254 | | * decode, etc. |
255 | | */ |
256 | 3.33k | ps_op->s_ivd_video_decode_op_t.u4_num_bytes_consumed = ps_dec->i4_bytes_consumed; |
257 | 3.33k | } |
258 | 7.95k | else |
259 | 7.95k | { |
260 | 7.95k | ps_op->s_ivd_video_decode_op_t.u4_num_bytes_consumed = (ps_dec->s_bit_stream.u4_offset + 7) >> 3; |
261 | 7.95k | ps_op->s_ivd_video_decode_op_t.u4_num_bytes_consumed -= ((size_t)ps_dec->s_bit_stream.pv_bs_buf & 3); |
262 | 7.95k | } |
263 | | |
264 | 11.2k | if(ps_op->s_ivd_video_decode_op_t.u4_num_bytes_consumed |
265 | 11.2k | > ps_ip->s_ivd_video_decode_ip_t.u4_num_Bytes) |
266 | 1.02k | { |
267 | 1.02k | ps_op->s_ivd_video_decode_op_t.u4_num_bytes_consumed = |
268 | 1.02k | ps_ip->s_ivd_video_decode_ip_t.u4_num_Bytes; |
269 | 1.02k | } |
270 | | |
271 | 11.2k | impeg2d_next_start_code(ps_dec); |
272 | 11.2k | } |
273 | | |
274 | 15.6k | if(ps_op->s_ivd_video_decode_op_t.u4_error_code == 0) |
275 | 0 | { |
276 | 0 | ps_op->s_ivd_video_decode_op_t.u4_error_code = e_error; |
277 | 0 | } |
278 | | |
279 | 15.6k | return; |
280 | 15.6k | } |
281 | 41.5k | } |
282 | | /**************************************************************************/ |
283 | | /* Remove the bytes left till next start code is encountered */ |
284 | | /**************************************************************************/ |
285 | 25.8k | ps_op->s_ivd_video_decode_op_t.u4_error_code = IV_SUCCESS; |
286 | | |
287 | 25.8k | if(ps_dec->i4_num_cores > 1 && 0 != ps_dec->i4_bytes_consumed) |
288 | 24.2k | { |
289 | | /* If the number of bytes consumed has been updated by |
290 | | * get_slice_pos function, then use that. Else, the bytes consumed is |
291 | | * calculated from the offset. The bytes consumed for multi-thread runs |
292 | | * is updated only into ps_dec->i4_bytes_consumed if the get_slice_pos |
293 | | * function has been called. If that function has not run, then we have |
294 | | * encountered an error but still have to consume the bytes in header |
295 | | * decode, etc. |
296 | | */ |
297 | 24.2k | ps_op->s_ivd_video_decode_op_t.u4_num_bytes_consumed = ps_dec->i4_bytes_consumed; |
298 | 24.2k | } |
299 | 1.64k | else |
300 | 1.64k | { |
301 | 1.64k | ps_op->s_ivd_video_decode_op_t.u4_num_bytes_consumed = (ps_dec->s_bit_stream.u4_offset + 7) >> 3; |
302 | 1.64k | ps_op->s_ivd_video_decode_op_t.u4_num_bytes_consumed -= ((size_t)ps_dec->s_bit_stream.pv_bs_buf & 3); |
303 | 1.64k | } |
304 | 25.8k | if(ps_op->s_ivd_video_decode_op_t.u4_num_bytes_consumed > ps_ip->s_ivd_video_decode_ip_t.u4_num_Bytes) |
305 | 958 | { |
306 | 958 | ps_op->s_ivd_video_decode_op_t.u4_num_bytes_consumed = ps_ip->s_ivd_video_decode_ip_t.u4_num_Bytes; |
307 | 958 | } |
308 | 25.8k | ps_op->s_ivd_video_decode_op_t.u4_pic_ht = ps_dec->u2_vertical_size; |
309 | 25.8k | ps_op->s_ivd_video_decode_op_t.u4_pic_wd = ps_dec->u2_horizontal_size; |
310 | | |
311 | 25.8k | switch(ps_dec->e_pic_type) |
312 | 25.8k | { |
313 | 6.09k | case I_PIC : |
314 | 6.09k | ps_op->s_ivd_video_decode_op_t.e_pic_type = IV_I_FRAME; |
315 | 6.09k | break; |
316 | | |
317 | 4.87k | case P_PIC: |
318 | 4.87k | ps_op->s_ivd_video_decode_op_t.e_pic_type = IV_P_FRAME; |
319 | 4.87k | break; |
320 | | |
321 | 5.69k | case B_PIC: |
322 | 5.69k | ps_op->s_ivd_video_decode_op_t.e_pic_type = IV_B_FRAME; |
323 | 5.69k | break; |
324 | | |
325 | 9.01k | case D_PIC: |
326 | 9.01k | ps_op->s_ivd_video_decode_op_t.e_pic_type = IV_I_FRAME; |
327 | 9.01k | break; |
328 | | |
329 | 195 | default : |
330 | 195 | ps_op->s_ivd_video_decode_op_t.e_pic_type = IV_FRAMETYPE_DEFAULT; |
331 | 195 | break; |
332 | 25.8k | } |
333 | | |
334 | 25.8k | ps_op->s_ivd_video_decode_op_t.u4_frame_decoded_flag = ps_dec->i4_frame_decoded; |
335 | 25.8k | ps_op->s_ivd_video_decode_op_t.u4_new_seq = 0; |
336 | 25.8k | ps_op->s_ivd_video_decode_op_t.u4_error_code = ps_dec->u4_error_code; |
337 | | |
338 | | |
339 | 25.8k | } |