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