Parent Directory
|
Revision Log
Revision 1.21 - (view) (download)
1 : | Isibaar | 1.1 | /************************************************************************** |
2 : | * | ||
3 : | edgomez | 1.12 | * XVID MPEG-4 VIDEO CODEC |
4 : | * - Decoder main module - | ||
5 : | Isibaar | 1.1 | * |
6 : | edgomez | 1.12 | * This program is an implementation of a part of one or more MPEG-4 |
7 : | * Video tools as specified in ISO/IEC 14496-2 standard. Those intending | ||
8 : | * to use this software module in hardware or software products are | ||
9 : | * advised that its use may infringe existing patents or copyrights, and | ||
10 : | * any such use would be at such party's own risk. The original | ||
11 : | * developer of this software module and his/her company, and subsequent | ||
12 : | * editors and their companies, will have no liability for use of this | ||
13 : | * software or modifications or derivatives thereof. | ||
14 : | * | ||
15 : | * This program is free software; you can redistribute it and/or modify | ||
16 : | * it under the terms of the GNU General Public License as published by | ||
17 : | * the Free Software Foundation; either version 2 of the License, or | ||
18 : | * (at your option) any later version. | ||
19 : | * | ||
20 : | * This program is distributed in the hope that it will be useful, | ||
21 : | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
22 : | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
23 : | * GNU General Public License for more details. | ||
24 : | * | ||
25 : | * You should have received a copy of the GNU General Public License | ||
26 : | * along with this program; if not, write to the Free Software | ||
27 : | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
28 : | Isibaar | 1.1 | * |
29 : | *************************************************************************/ | ||
30 : | |||
31 : | /************************************************************************** | ||
32 : | * | ||
33 : | edgomez | 1.12 | * History: |
34 : | Isibaar | 1.1 | * |
35 : | suxen_drol | 1.21 | * 22.06.2002 added primative N_VOP support |
36 : | * #define BFRAMES_DEC now enables Minchenm's bframe decoder | ||
37 : | chenm001 | 1.17 | * 08.05.2002 add low_delay support for B_VOP decode |
38 : | * MinChen <chenm001@163.com> | ||
39 : | * 05.05.2002 fix some B-frame decode problem | ||
40 : | chenm001 | 1.14 | * 02.05.2002 add B-frame decode support(have some problem); |
41 : | * MinChen <chenm001@163.com> | ||
42 : | chenm001 | 1.11 | * 22.04.2002 add some B-frame decode support; chenm001 <chenm001@163.com> |
43 : | h | 1.8 | * 29.03.2002 interlacing fix - compensated block wasn't being used when |
44 : | * reconstructing blocks, thus artifacts | ||
45 : | * interlacing speedup - used transfers to re-interlace | ||
46 : | * interlaced decoding should be as fast as progressive now | ||
47 : | h | 1.5 | * 26.03.2002 interlacing support - moved transfers outside decode loop |
48 : | edgomez | 1.12 | * 26.12.2001 decoder_mbinter: dequant/idct moved within if(coded) block |
49 : | * 22.12.2001 lock based interpolation | ||
50 : | * 01.12.2001 inital version; (c)2001 peter ross <pross@cs.rmit.edu.au> | ||
51 : | * | ||
52 : | suxen_drol | 1.21 | * $Id: decoder.c,v 1.20 2002/06/14 13:21:13 Isibaar Exp $ |
53 : | Isibaar | 1.1 | * |
54 : | *************************************************************************/ | ||
55 : | |||
56 : | #include <stdlib.h> | ||
57 : | edgomez | 1.12 | #include <string.h> |
58 : | Isibaar | 1.1 | |
59 : | #include "xvid.h" | ||
60 : | #include "portab.h" | ||
61 : | |||
62 : | #include "decoder.h" | ||
63 : | #include "bitstream/bitstream.h" | ||
64 : | #include "bitstream/mbcoding.h" | ||
65 : | |||
66 : | #include "quant/quant_h263.h" | ||
67 : | #include "quant/quant_mpeg4.h" | ||
68 : | #include "dct/idct.h" | ||
69 : | #include "dct/fdct.h" | ||
70 : | #include "utils/mem_transfer.h" | ||
71 : | #include "image/interpolate8x8.h" | ||
72 : | |||
73 : | #include "bitstream/mbcoding.h" | ||
74 : | #include "prediction/mbprediction.h" | ||
75 : | #include "utils/timer.h" | ||
76 : | #include "utils/emms.h" | ||
77 : | |||
78 : | #include "image/image.h" | ||
79 : | #include "image/colorspace.h" | ||
80 : | Isibaar | 1.3 | #include "utils/mem_align.h" |
81 : | Isibaar | 1.1 | |
82 : | edgomez | 1.19 | int |
83 : | decoder_create(XVID_DEC_PARAM * param) | ||
84 : | Isibaar | 1.1 | { |
85 : | edgomez | 1.19 | DECODER *dec; |
86 : | Isibaar | 1.1 | |
87 : | Isibaar | 1.4 | dec = xvid_malloc(sizeof(DECODER), CACHE_LINE); |
88 : | edgomez | 1.19 | if (dec == NULL) { |
89 : | Isibaar | 1.1 | return XVID_ERR_MEMORY; |
90 : | } | ||
91 : | param->handle = dec; | ||
92 : | |||
93 : | dec->width = param->width; | ||
94 : | dec->height = param->height; | ||
95 : | |||
96 : | dec->mb_width = (dec->width + 15) / 16; | ||
97 : | dec->mb_height = (dec->height + 15) / 16; | ||
98 : | |||
99 : | dec->edged_width = 16 * dec->mb_width + 2 * EDGE_SIZE; | ||
100 : | dec->edged_height = 16 * dec->mb_height + 2 * EDGE_SIZE; | ||
101 : | Isibaar | 1.20 | dec->low_delay = 0; |
102 : | edgomez | 1.19 | |
103 : | if (image_create(&dec->cur, dec->edged_width, dec->edged_height)) { | ||
104 : | Isibaar | 1.3 | xvid_free(dec); |
105 : | Isibaar | 1.1 | return XVID_ERR_MEMORY; |
106 : | } | ||
107 : | |||
108 : | edgomez | 1.19 | if (image_create(&dec->refn[0], dec->edged_width, dec->edged_height)) { |
109 : | Isibaar | 1.1 | image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
110 : | Isibaar | 1.3 | xvid_free(dec); |
111 : | Isibaar | 1.1 | return XVID_ERR_MEMORY; |
112 : | } | ||
113 : | chenm001 | 1.11 | // add by chenm001 <chenm001@163.com> |
114 : | // for support B-frame to reference last 2 frame | ||
115 : | edgomez | 1.19 | if (image_create(&dec->refn[1], dec->edged_width, dec->edged_height)) { |
116 : | chenm001 | 1.11 | image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
117 : | image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height); | ||
118 : | xvid_free(dec); | ||
119 : | return XVID_ERR_MEMORY; | ||
120 : | } | ||
121 : | edgomez | 1.19 | if (image_create(&dec->refn[2], dec->edged_width, dec->edged_height)) { |
122 : | chenm001 | 1.14 | image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
123 : | image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height); | ||
124 : | image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height); | ||
125 : | xvid_free(dec); | ||
126 : | return XVID_ERR_MEMORY; | ||
127 : | } | ||
128 : | Isibaar | 1.1 | |
129 : | edgomez | 1.19 | dec->mbs = |
130 : | xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height, | ||
131 : | CACHE_LINE); | ||
132 : | if (dec->mbs == NULL) { | ||
133 : | Isibaar | 1.1 | image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
134 : | chenm001 | 1.14 | image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height); |
135 : | image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height); | ||
136 : | image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height); | ||
137 : | xvid_free(dec); | ||
138 : | return XVID_ERR_MEMORY; | ||
139 : | } | ||
140 : | Isibaar | 1.20 | |
141 : | memset(dec->mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height); | ||
142 : | |||
143 : | chenm001 | 1.14 | // add by chenm001 <chenm001@163.com> |
144 : | // for skip MB flag | ||
145 : | edgomez | 1.19 | dec->last_mbs = |
146 : | xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height, | ||
147 : | CACHE_LINE); | ||
148 : | if (dec->last_mbs == NULL) { | ||
149 : | chenm001 | 1.14 | xvid_free(dec->mbs); |
150 : | image_destroy(&dec->cur, dec->edged_width, dec->edged_height); | ||
151 : | image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height); | ||
152 : | image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height); | ||
153 : | image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height); | ||
154 : | Isibaar | 1.3 | xvid_free(dec); |
155 : | Isibaar | 1.1 | return XVID_ERR_MEMORY; |
156 : | } | ||
157 : | Isibaar | 1.20 | |
158 : | memset(dec->last_mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height); | ||
159 : | Isibaar | 1.1 | |
160 : | init_timer(); | ||
161 : | |||
162 : | chenm001 | 1.14 | // add by chenm001 <chenm001@163.com> |
163 : | // for support B-frame to save reference frame's time | ||
164 : | dec->frames = -1; | ||
165 : | dec->time = dec->time_base = dec->last_time_base = 0; | ||
166 : | edgomez | 1.19 | |
167 : | Isibaar | 1.1 | return XVID_ERR_OK; |
168 : | } | ||
169 : | |||
170 : | |||
171 : | edgomez | 1.19 | int |
172 : | decoder_destroy(DECODER * dec) | ||
173 : | Isibaar | 1.1 | { |
174 : | chenm001 | 1.14 | xvid_free(dec->last_mbs); |
175 : | Isibaar | 1.3 | xvid_free(dec->mbs); |
176 : | chenm001 | 1.11 | image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height); |
177 : | chenm001 | 1.14 | image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height); |
178 : | image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height); | ||
179 : | Isibaar | 1.1 | image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
180 : | Isibaar | 1.3 | xvid_free(dec); |
181 : | Isibaar | 1.1 | |
182 : | write_timer(); | ||
183 : | return XVID_ERR_OK; | ||
184 : | } | ||
185 : | |||
186 : | |||
187 : | |||
188 : | edgomez | 1.19 | static const int32_t dquant_table[4] = { |
189 : | Isibaar | 1.1 | -1, -2, 1, 2 |
190 : | }; | ||
191 : | |||
192 : | |||
193 : | chenm001 | 1.14 | |
194 : | |||
195 : | Isibaar | 1.1 | // decode an intra macroblock |
196 : | |||
197 : | edgomez | 1.19 | void |
198 : | decoder_mbintra(DECODER * dec, | ||
199 : | MACROBLOCK * pMB, | ||
200 : | const uint32_t x_pos, | ||
201 : | const uint32_t y_pos, | ||
202 : | const uint32_t acpred_flag, | ||
203 : | const uint32_t cbp, | ||
204 : | Bitstream * bs, | ||
205 : | const uint32_t quant, | ||
206 : | const uint32_t intra_dc_threshold) | ||
207 : | Isibaar | 1.1 | { |
208 : | edgomez | 1.7 | |
209 : | DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE); | ||
210 : | edgomez | 1.19 | DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE); |
211 : | edgomez | 1.7 | |
212 : | h | 1.8 | uint32_t stride = dec->edged_width; |
213 : | uint32_t stride2 = stride / 2; | ||
214 : | uint32_t next_block = stride * 8; | ||
215 : | h | 1.5 | uint32_t i; |
216 : | uint32_t iQuant = pMB->quant; | ||
217 : | uint8_t *pY_Cur, *pU_Cur, *pV_Cur; | ||
218 : | |||
219 : | edgomez | 1.7 | pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4); |
220 : | h | 1.8 | pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3); |
221 : | pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3); | ||
222 : | edgomez | 1.7 | |
223 : | edgomez | 1.19 | memset(block, 0, 6 * 64 * sizeof(int16_t)); // clear |
224 : | h | 1.5 | |
225 : | edgomez | 1.19 | for (i = 0; i < 6; i++) { |
226 : | h | 1.5 | uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4); |
227 : | Isibaar | 1.1 | int16_t predictors[8]; |
228 : | int start_coeff; | ||
229 : | |||
230 : | start_timer(); | ||
231 : | edgomez | 1.19 | predict_acdc(dec->mbs, x_pos, y_pos, dec->mb_width, i, &block[i * 64], |
232 : | iQuant, iDcScaler, predictors); | ||
233 : | if (!acpred_flag) { | ||
234 : | h | 1.5 | pMB->acpred_directions[i] = 0; |
235 : | Isibaar | 1.1 | } |
236 : | stop_prediction_timer(); | ||
237 : | |||
238 : | edgomez | 1.19 | if (quant < intra_dc_threshold) { |
239 : | Isibaar | 1.1 | int dc_size; |
240 : | int dc_dif; | ||
241 : | |||
242 : | edgomez | 1.19 | dc_size = i < 4 ? get_dc_size_lum(bs) : get_dc_size_chrom(bs); |
243 : | dc_dif = dc_size ? get_dc_dif(bs, dc_size) : 0; | ||
244 : | Isibaar | 1.1 | |
245 : | edgomez | 1.19 | if (dc_size > 8) { |
246 : | BitstreamSkip(bs, 1); // marker | ||
247 : | Isibaar | 1.1 | } |
248 : | edgomez | 1.19 | |
249 : | block[i * 64 + 0] = dc_dif; | ||
250 : | Isibaar | 1.1 | start_coeff = 1; |
251 : | edgomez | 1.19 | } else { |
252 : | Isibaar | 1.1 | start_coeff = 0; |
253 : | } | ||
254 : | |||
255 : | start_timer(); | ||
256 : | edgomez | 1.19 | if (cbp & (1 << (5 - i))) // coded |
257 : | Isibaar | 1.1 | { |
258 : | edgomez | 1.19 | get_intra_block(bs, &block[i * 64], pMB->acpred_directions[i], |
259 : | start_coeff); | ||
260 : | Isibaar | 1.1 | } |
261 : | stop_coding_timer(); | ||
262 : | |||
263 : | start_timer(); | ||
264 : | edgomez | 1.19 | add_acdc(pMB, i, &block[i * 64], iDcScaler, predictors); |
265 : | Isibaar | 1.1 | stop_prediction_timer(); |
266 : | |||
267 : | start_timer(); | ||
268 : | edgomez | 1.19 | if (dec->quant_type == 0) { |
269 : | dequant_intra(&data[i * 64], &block[i * 64], iQuant, iDcScaler); | ||
270 : | } else { | ||
271 : | dequant4_intra(&data[i * 64], &block[i * 64], iQuant, iDcScaler); | ||
272 : | Isibaar | 1.1 | } |
273 : | stop_iquant_timer(); | ||
274 : | |||
275 : | start_timer(); | ||
276 : | edgomez | 1.19 | idct(&data[i * 64]); |
277 : | Isibaar | 1.1 | stop_idct_timer(); |
278 : | h | 1.5 | } |
279 : | Isibaar | 1.1 | |
280 : | edgomez | 1.19 | if (dec->interlacing && pMB->field_dct) { |
281 : | h | 1.8 | next_block = stride; |
282 : | stride *= 2; | ||
283 : | Isibaar | 1.1 | } |
284 : | h | 1.5 | |
285 : | start_timer(); | ||
286 : | edgomez | 1.19 | transfer_16to8copy(pY_Cur, &data[0 * 64], stride); |
287 : | transfer_16to8copy(pY_Cur + 8, &data[1 * 64], stride); | ||
288 : | transfer_16to8copy(pY_Cur + next_block, &data[2 * 64], stride); | ||
289 : | transfer_16to8copy(pY_Cur + 8 + next_block, &data[3 * 64], stride); | ||
290 : | transfer_16to8copy(pU_Cur, &data[4 * 64], stride2); | ||
291 : | transfer_16to8copy(pV_Cur, &data[5 * 64], stride2); | ||
292 : | h | 1.5 | stop_transfer_timer(); |
293 : | Isibaar | 1.1 | } |
294 : | |||
295 : | |||
296 : | |||
297 : | |||
298 : | |||
299 : | #define SIGN(X) (((X)>0)?1:-1) | ||
300 : | #define ABS(X) (((X)>0)?(X):-(X)) | ||
301 : | static const uint32_t roundtab[16] = | ||
302 : | edgomez | 1.19 | { 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2 }; |
303 : | Isibaar | 1.1 | |
304 : | |||
305 : | // decode an inter macroblock | ||
306 : | |||
307 : | edgomez | 1.19 | void |
308 : | decoder_mbinter(DECODER * dec, | ||
309 : | const MACROBLOCK * pMB, | ||
310 : | const uint32_t x_pos, | ||
311 : | const uint32_t y_pos, | ||
312 : | const uint32_t acpred_flag, | ||
313 : | const uint32_t cbp, | ||
314 : | Bitstream * bs, | ||
315 : | const uint32_t quant, | ||
316 : | const uint32_t rounding) | ||
317 : | Isibaar | 1.1 | { |
318 : | edgomez | 1.7 | |
319 : | edgomez | 1.19 | DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE); |
320 : | edgomez | 1.7 | DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE); |
321 : | h | 1.5 | |
322 : | h | 1.8 | uint32_t stride = dec->edged_width; |
323 : | uint32_t stride2 = stride / 2; | ||
324 : | uint32_t next_block = stride * 8; | ||
325 : | edgomez | 1.7 | uint32_t i; |
326 : | uint32_t iQuant = pMB->quant; | ||
327 : | h | 1.5 | uint8_t *pY_Cur, *pU_Cur, *pV_Cur; |
328 : | Isibaar | 1.1 | int uv_dx, uv_dy; |
329 : | |||
330 : | edgomez | 1.7 | pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4); |
331 : | h | 1.8 | pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3); |
332 : | pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3); | ||
333 : | h | 1.5 | |
334 : | edgomez | 1.19 | if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) { |
335 : | h | 1.5 | uv_dx = pMB->mvs[0].x; |
336 : | uv_dy = pMB->mvs[0].y; | ||
337 : | Isibaar | 1.1 | |
338 : | uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2; | ||
339 : | uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2; | ||
340 : | edgomez | 1.19 | } else { |
341 : | Isibaar | 1.1 | int sum; |
342 : | edgomez | 1.19 | |
343 : | h | 1.5 | sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x; |
344 : | edgomez | 1.19 | uv_dx = |
345 : | (sum == | ||
346 : | 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + | ||
347 : | (ABS(sum) / 16) * 2)); | ||
348 : | Isibaar | 1.1 | |
349 : | h | 1.5 | sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y; |
350 : | edgomez | 1.19 | uv_dy = |
351 : | (sum == | ||
352 : | 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + | ||
353 : | (ABS(sum) / 16) * 2)); | ||
354 : | Isibaar | 1.1 | } |
355 : | |||
356 : | start_timer(); | ||
357 : | edgomez | 1.19 | interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16 * x_pos, 16 * y_pos, |
358 : | pMB->mvs[0].x, pMB->mvs[0].y, stride, rounding); | ||
359 : | interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16 * x_pos + 8, | ||
360 : | 16 * y_pos, pMB->mvs[1].x, pMB->mvs[1].y, stride, | ||
361 : | rounding); | ||
362 : | interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16 * x_pos, | ||
363 : | 16 * y_pos + 8, pMB->mvs[2].x, pMB->mvs[2].y, stride, | ||
364 : | rounding); | ||
365 : | interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16 * x_pos + 8, | ||
366 : | 16 * y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride, | ||
367 : | rounding); | ||
368 : | interpolate8x8_switch(dec->cur.u, dec->refn[0].u, 8 * x_pos, 8 * y_pos, | ||
369 : | uv_dx, uv_dy, stride2, rounding); | ||
370 : | interpolate8x8_switch(dec->cur.v, dec->refn[0].v, 8 * x_pos, 8 * y_pos, | ||
371 : | uv_dx, uv_dy, stride2, rounding); | ||
372 : | Isibaar | 1.1 | stop_comp_timer(); |
373 : | |||
374 : | edgomez | 1.19 | for (i = 0; i < 6; i++) { |
375 : | if (cbp & (1 << (5 - i))) // coded | ||
376 : | Isibaar | 1.1 | { |
377 : | edgomez | 1.19 | memset(&block[i * 64], 0, 64 * sizeof(int16_t)); // clear |
378 : | Isibaar | 1.1 | |
379 : | start_timer(); | ||
380 : | edgomez | 1.19 | get_inter_block(bs, &block[i * 64]); |
381 : | Isibaar | 1.1 | stop_coding_timer(); |
382 : | |||
383 : | start_timer(); | ||
384 : | edgomez | 1.19 | if (dec->quant_type == 0) { |
385 : | dequant_inter(&data[i * 64], &block[i * 64], iQuant); | ||
386 : | } else { | ||
387 : | dequant4_inter(&data[i * 64], &block[i * 64], iQuant); | ||
388 : | Isibaar | 1.1 | } |
389 : | stop_iquant_timer(); | ||
390 : | |||
391 : | start_timer(); | ||
392 : | edgomez | 1.19 | idct(&data[i * 64]); |
393 : | Isibaar | 1.1 | stop_idct_timer(); |
394 : | h | 1.5 | } |
395 : | } | ||
396 : | Isibaar | 1.1 | |
397 : | edgomez | 1.19 | if (dec->interlacing && pMB->field_dct) { |
398 : | h | 1.8 | next_block = stride; |
399 : | stride *= 2; | ||
400 : | Isibaar | 1.1 | } |
401 : | h | 1.5 | |
402 : | start_timer(); | ||
403 : | if (cbp & 32) | ||
404 : | edgomez | 1.19 | transfer_16to8add(pY_Cur, &data[0 * 64], stride); |
405 : | h | 1.5 | if (cbp & 16) |
406 : | edgomez | 1.19 | transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride); |
407 : | h | 1.5 | if (cbp & 8) |
408 : | edgomez | 1.19 | transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride); |
409 : | h | 1.5 | if (cbp & 4) |
410 : | edgomez | 1.19 | transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride); |
411 : | h | 1.5 | if (cbp & 2) |
412 : | edgomez | 1.19 | transfer_16to8add(pU_Cur, &data[4 * 64], stride2); |
413 : | h | 1.5 | if (cbp & 1) |
414 : | edgomez | 1.19 | transfer_16to8add(pV_Cur, &data[5 * 64], stride2); |
415 : | h | 1.5 | stop_transfer_timer(); |
416 : | Isibaar | 1.1 | } |
417 : | |||
418 : | |||
419 : | edgomez | 1.19 | void |
420 : | decoder_iframe(DECODER * dec, | ||
421 : | Bitstream * bs, | ||
422 : | int quant, | ||
423 : | int intra_dc_threshold) | ||
424 : | Isibaar | 1.1 | { |
425 : | edgomez | 1.7 | |
426 : | Isibaar | 1.1 | uint32_t x, y; |
427 : | |||
428 : | edgomez | 1.19 | for (y = 0; y < dec->mb_height; y++) { |
429 : | for (x = 0; x < dec->mb_width; x++) { | ||
430 : | MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x]; | ||
431 : | Isibaar | 1.1 | |
432 : | uint32_t mcbpc; | ||
433 : | uint32_t cbpc; | ||
434 : | uint32_t acpred_flag; | ||
435 : | uint32_t cbpy; | ||
436 : | uint32_t cbp; | ||
437 : | |||
438 : | mcbpc = get_mcbpc_intra(bs); | ||
439 : | mb->mode = mcbpc & 7; | ||
440 : | cbpc = (mcbpc >> 4); | ||
441 : | |||
442 : | acpred_flag = BitstreamGetBit(bs); | ||
443 : | |||
444 : | edgomez | 1.19 | if (mb->mode == MODE_STUFFING) { |
445 : | Isibaar | 1.1 | DEBUG("-- STUFFING ?"); |
446 : | continue; | ||
447 : | } | ||
448 : | |||
449 : | cbpy = get_cbpy(bs, 1); | ||
450 : | cbp = (cbpy << 2) | cbpc; | ||
451 : | |||
452 : | edgomez | 1.19 | if (mb->mode == MODE_INTRA_Q) { |
453 : | quant += dquant_table[BitstreamGetBits(bs, 2)]; | ||
454 : | if (quant > 31) { | ||
455 : | Isibaar | 1.1 | quant = 31; |
456 : | edgomez | 1.19 | } else if (quant < 1) { |
457 : | Isibaar | 1.1 | quant = 1; |
458 : | } | ||
459 : | } | ||
460 : | mb->quant = quant; | ||
461 : | h | 1.5 | |
462 : | edgomez | 1.19 | if (dec->interlacing) { |
463 : | h | 1.5 | mb->field_dct = BitstreamGetBit(bs); |
464 : | DEBUG1("deci: field_dct: ", mb->field_dct); | ||
465 : | } | ||
466 : | Isibaar | 1.1 | |
467 : | edgomez | 1.19 | decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant, |
468 : | intra_dc_threshold); | ||
469 : | Isibaar | 1.1 | } |
470 : | } | ||
471 : | edgomez | 1.7 | |
472 : | Isibaar | 1.1 | } |
473 : | |||
474 : | |||
475 : | edgomez | 1.19 | void |
476 : | get_motion_vector(DECODER * dec, | ||
477 : | Bitstream * bs, | ||
478 : | int x, | ||
479 : | int y, | ||
480 : | int k, | ||
481 : | VECTOR * mv, | ||
482 : | int fcode) | ||
483 : | Isibaar | 1.1 | { |
484 : | edgomez | 1.7 | |
485 : | Isibaar | 1.1 | int scale_fac = 1 << (fcode - 1); |
486 : | int high = (32 * scale_fac) - 1; | ||
487 : | int low = ((-32) * scale_fac); | ||
488 : | int range = (64 * scale_fac); | ||
489 : | |||
490 : | VECTOR pmv[4]; | ||
491 : | edgomez | 1.13 | int32_t psad[4]; |
492 : | Isibaar | 1.1 | |
493 : | int mv_x, mv_y; | ||
494 : | int pmv_x, pmv_y; | ||
495 : | |||
496 : | |||
497 : | get_pmvdata(dec->mbs, x, y, dec->mb_width, k, pmv, psad); | ||
498 : | |||
499 : | pmv_x = pmv[0].x; | ||
500 : | pmv_y = pmv[0].y; | ||
501 : | |||
502 : | mv_x = get_mv(bs, fcode); | ||
503 : | mv_y = get_mv(bs, fcode); | ||
504 : | edgomez | 1.19 | |
505 : | Isibaar | 1.1 | mv_x += pmv_x; |
506 : | mv_y += pmv_y; | ||
507 : | |||
508 : | edgomez | 1.19 | if (mv_x < low) { |
509 : | Isibaar | 1.1 | mv_x += range; |
510 : | edgomez | 1.19 | } else if (mv_x > high) { |
511 : | Isibaar | 1.1 | mv_x -= range; |
512 : | } | ||
513 : | |||
514 : | edgomez | 1.19 | if (mv_y < low) { |
515 : | Isibaar | 1.1 | mv_y += range; |
516 : | edgomez | 1.19 | } else if (mv_y > high) { |
517 : | Isibaar | 1.1 | mv_y -= range; |
518 : | } | ||
519 : | |||
520 : | mv->x = mv_x; | ||
521 : | mv->y = mv_y; | ||
522 : | |||
523 : | } | ||
524 : | |||
525 : | |||
526 : | edgomez | 1.19 | void |
527 : | decoder_pframe(DECODER * dec, | ||
528 : | Bitstream * bs, | ||
529 : | int rounding, | ||
530 : | int quant, | ||
531 : | int fcode, | ||
532 : | int intra_dc_threshold) | ||
533 : | Isibaar | 1.1 | { |
534 : | edgomez | 1.7 | |
535 : | Isibaar | 1.1 | uint32_t x, y; |
536 : | |||
537 : | start_timer(); | ||
538 : | edgomez | 1.19 | image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, |
539 : | dec->width, dec->height, dec->interlacing); | ||
540 : | Isibaar | 1.1 | stop_edges_timer(); |
541 : | |||
542 : | edgomez | 1.19 | for (y = 0; y < dec->mb_height; y++) { |
543 : | for (x = 0; x < dec->mb_width; x++) { | ||
544 : | MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x]; | ||
545 : | Isibaar | 1.1 | |
546 : | edgomez | 1.19 | //if (!(dec->mb_skip[y*dec->mb_width + x]=BitstreamGetBit(bs))) // not_coded |
547 : | if (!(BitstreamGetBit(bs))) // not_coded | ||
548 : | Isibaar | 1.1 | { |
549 : | uint32_t mcbpc; | ||
550 : | uint32_t cbpc; | ||
551 : | uint32_t acpred_flag; | ||
552 : | uint32_t cbpy; | ||
553 : | uint32_t cbp; | ||
554 : | uint32_t intra; | ||
555 : | |||
556 : | mcbpc = get_mcbpc_inter(bs); | ||
557 : | mb->mode = mcbpc & 7; | ||
558 : | cbpc = (mcbpc >> 4); | ||
559 : | edgomez | 1.2 | acpred_flag = 0; |
560 : | Isibaar | 1.1 | |
561 : | intra = (mb->mode == MODE_INTRA || mb->mode == MODE_INTRA_Q); | ||
562 : | edgomez | 1.19 | |
563 : | if (intra) { | ||
564 : | Isibaar | 1.1 | acpred_flag = BitstreamGetBit(bs); |
565 : | } | ||
566 : | |||
567 : | edgomez | 1.19 | if (mb->mode == MODE_STUFFING) { |
568 : | Isibaar | 1.1 | DEBUG("-- STUFFING ?"); |
569 : | continue; | ||
570 : | } | ||
571 : | |||
572 : | cbpy = get_cbpy(bs, intra); | ||
573 : | cbp = (cbpy << 2) | cbpc; | ||
574 : | |||
575 : | edgomez | 1.19 | if (mb->mode == MODE_INTER_Q || mb->mode == MODE_INTRA_Q) { |
576 : | quant += dquant_table[BitstreamGetBits(bs, 2)]; | ||
577 : | if (quant > 31) { | ||
578 : | Isibaar | 1.1 | quant = 31; |
579 : | edgomez | 1.19 | } else if (mb->quant < 1) { |
580 : | Isibaar | 1.1 | quant = 1; |
581 : | } | ||
582 : | } | ||
583 : | mb->quant = quant; | ||
584 : | h | 1.5 | |
585 : | edgomez | 1.19 | if (dec->interlacing) { |
586 : | h | 1.5 | mb->field_dct = BitstreamGetBit(bs); |
587 : | DEBUG1("decp: field_dct: ", mb->field_dct); | ||
588 : | |||
589 : | edgomez | 1.19 | if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) { |
590 : | h | 1.5 | mb->field_pred = BitstreamGetBit(bs); |
591 : | DEBUG1("decp: field_pred: ", mb->field_pred); | ||
592 : | |||
593 : | edgomez | 1.19 | if (mb->field_pred) { |
594 : | h | 1.5 | mb->field_for_top = BitstreamGetBit(bs); |
595 : | DEBUG1("decp: field_for_top: ", mb->field_for_top); | ||
596 : | mb->field_for_bot = BitstreamGetBit(bs); | ||
597 : | DEBUG1("decp: field_for_bot: ", mb->field_for_bot); | ||
598 : | } | ||
599 : | } | ||
600 : | } | ||
601 : | |||
602 : | edgomez | 1.19 | if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) { |
603 : | if (dec->interlacing && mb->field_pred) { | ||
604 : | get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], | ||
605 : | fcode); | ||
606 : | get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1], | ||
607 : | fcode); | ||
608 : | } else { | ||
609 : | get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], | ||
610 : | fcode); | ||
611 : | mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = | ||
612 : | mb->mvs[0].x; | ||
613 : | mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = | ||
614 : | mb->mvs[0].y; | ||
615 : | h | 1.5 | } |
616 : | edgomez | 1.19 | } else if (mb->mode == |
617 : | MODE_INTER4V /* || mb->mode == MODE_INTER4V_Q */ ) { | ||
618 : | Isibaar | 1.1 | get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode); |
619 : | get_motion_vector(dec, bs, x, y, 1, &mb->mvs[1], fcode); | ||
620 : | get_motion_vector(dec, bs, x, y, 2, &mb->mvs[2], fcode); | ||
621 : | get_motion_vector(dec, bs, x, y, 3, &mb->mvs[3], fcode); | ||
622 : | edgomez | 1.19 | } else // MODE_INTRA, MODE_INTRA_Q |
623 : | Isibaar | 1.1 | { |
624 : | edgomez | 1.19 | mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = |
625 : | 0; | ||
626 : | mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = | ||
627 : | 0; | ||
628 : | decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant, | ||
629 : | intra_dc_threshold); | ||
630 : | Isibaar | 1.1 | continue; |
631 : | } | ||
632 : | |||
633 : | edgomez | 1.19 | decoder_mbinter(dec, mb, x, y, acpred_flag, cbp, bs, quant, |
634 : | rounding); | ||
635 : | } else // not coded | ||
636 : | Isibaar | 1.1 | { |
637 : | chenm001 | 1.15 | //DEBUG2("P-frame MB at (X,Y)=",x,y); |
638 : | chenm001 | 1.14 | mb->mode = MODE_NOT_CODED; |
639 : | Isibaar | 1.1 | mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0; |
640 : | mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = 0; | ||
641 : | edgomez | 1.19 | |
642 : | Isibaar | 1.1 | // copy macroblock directly from ref to cur |
643 : | |||
644 : | start_timer(); | ||
645 : | |||
646 : | edgomez | 1.19 | transfer8x8_copy(dec->cur.y + (16 * y) * dec->edged_width + |
647 : | (16 * x), | ||
648 : | dec->refn[0].y + (16 * y) * dec->edged_width + | ||
649 : | (16 * x), dec->edged_width); | ||
650 : | |||
651 : | transfer8x8_copy(dec->cur.y + (16 * y) * dec->edged_width + | ||
652 : | (16 * x + 8), | ||
653 : | dec->refn[0].y + (16 * y) * dec->edged_width + | ||
654 : | (16 * x + 8), dec->edged_width); | ||
655 : | |||
656 : | transfer8x8_copy(dec->cur.y + (16 * y + 8) * dec->edged_width + | ||
657 : | (16 * x), | ||
658 : | dec->refn[0].y + (16 * y + | ||
659 : | 8) * dec->edged_width + | ||
660 : | (16 * x), dec->edged_width); | ||
661 : | |||
662 : | transfer8x8_copy(dec->cur.y + (16 * y + 8) * dec->edged_width + | ||
663 : | (16 * x + 8), | ||
664 : | dec->refn[0].y + (16 * y + | ||
665 : | 8) * dec->edged_width + | ||
666 : | (16 * x + 8), dec->edged_width); | ||
667 : | |||
668 : | transfer8x8_copy(dec->cur.u + (8 * y) * dec->edged_width / 2 + | ||
669 : | (8 * x), | ||
670 : | dec->refn[0].u + | ||
671 : | (8 * y) * dec->edged_width / 2 + (8 * x), | ||
672 : | dec->edged_width / 2); | ||
673 : | |||
674 : | transfer8x8_copy(dec->cur.v + (8 * y) * dec->edged_width / 2 + | ||
675 : | (8 * x), | ||
676 : | dec->refn[0].v + | ||
677 : | (8 * y) * dec->edged_width / 2 + (8 * x), | ||
678 : | dec->edged_width / 2); | ||
679 : | Isibaar | 1.1 | |
680 : | stop_transfer_timer(); | ||
681 : | } | ||
682 : | } | ||
683 : | } | ||
684 : | } | ||
685 : | |||
686 : | chenm001 | 1.14 | |
687 : | // add by MinChen <chenm001@163.com> | ||
688 : | // decode B-frame motion vector | ||
689 : | edgomez | 1.19 | void |
690 : | get_b_motion_vector(DECODER * dec, | ||
691 : | Bitstream * bs, | ||
692 : | int x, | ||
693 : | int y, | ||
694 : | VECTOR * mv, | ||
695 : | int fcode, | ||
696 : | const VECTOR pmv) | ||
697 : | chenm001 | 1.14 | { |
698 : | int scale_fac = 1 << (fcode - 1); | ||
699 : | int high = (32 * scale_fac) - 1; | ||
700 : | int low = ((-32) * scale_fac); | ||
701 : | int range = (64 * scale_fac); | ||
702 : | |||
703 : | int mv_x, mv_y; | ||
704 : | int pmv_x, pmv_y; | ||
705 : | |||
706 : | pmv_x = pmv.x; | ||
707 : | pmv_y = pmv.y; | ||
708 : | |||
709 : | mv_x = get_mv(bs, fcode); | ||
710 : | mv_y = get_mv(bs, fcode); | ||
711 : | edgomez | 1.19 | |
712 : | chenm001 | 1.14 | mv_x += pmv_x; |
713 : | mv_y += pmv_y; | ||
714 : | |||
715 : | edgomez | 1.19 | if (mv_x < low) { |
716 : | chenm001 | 1.14 | mv_x += range; |
717 : | edgomez | 1.19 | } else if (mv_x > high) { |
718 : | chenm001 | 1.14 | mv_x -= range; |
719 : | } | ||
720 : | |||
721 : | edgomez | 1.19 | if (mv_y < low) { |
722 : | chenm001 | 1.14 | mv_y += range; |
723 : | edgomez | 1.19 | } else if (mv_y > high) { |
724 : | chenm001 | 1.14 | mv_y -= range; |
725 : | } | ||
726 : | |||
727 : | mv->x = mv_x; | ||
728 : | mv->y = mv_y; | ||
729 : | } | ||
730 : | |||
731 : | |||
732 : | // add by MinChen <chenm001@163.com> | ||
733 : | // decode an B-frame forward & backward inter macroblock | ||
734 : | edgomez | 1.19 | void |
735 : | decoder_bf_mbinter(DECODER * dec, | ||
736 : | const MACROBLOCK * pMB, | ||
737 : | const uint32_t x_pos, | ||
738 : | const uint32_t y_pos, | ||
739 : | const uint32_t cbp, | ||
740 : | Bitstream * bs, | ||
741 : | const uint32_t quant, | ||
742 : | const uint8_t ref) | ||
743 : | chenm001 | 1.14 | { |
744 : | |||
745 : | edgomez | 1.19 | DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE); |
746 : | chenm001 | 1.14 | DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE); |
747 : | |||
748 : | uint32_t stride = dec->edged_width; | ||
749 : | uint32_t stride2 = stride / 2; | ||
750 : | uint32_t next_block = stride * 8; | ||
751 : | uint32_t i; | ||
752 : | uint32_t iQuant = pMB->quant; | ||
753 : | uint8_t *pY_Cur, *pU_Cur, *pV_Cur; | ||
754 : | int uv_dx, uv_dy; | ||
755 : | |||
756 : | pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4); | ||
757 : | pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3); | ||
758 : | pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3); | ||
759 : | |||
760 : | edgomez | 1.19 | if (!(pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)) { |
761 : | chenm001 | 1.14 | uv_dx = pMB->mvs[0].x; |
762 : | uv_dy = pMB->mvs[0].y; | ||
763 : | |||
764 : | uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2; | ||
765 : | uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2; | ||
766 : | edgomez | 1.19 | } else { |
767 : | chenm001 | 1.14 | int sum; |
768 : | edgomez | 1.19 | |
769 : | chenm001 | 1.14 | sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x; |
770 : | edgomez | 1.19 | uv_dx = |
771 : | (sum == | ||
772 : | 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + | ||
773 : | (ABS(sum) / 16) * 2)); | ||
774 : | chenm001 | 1.14 | |
775 : | sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y; | ||
776 : | edgomez | 1.19 | uv_dy = |
777 : | (sum == | ||
778 : | 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + | ||
779 : | (ABS(sum) / 16) * 2)); | ||
780 : | chenm001 | 1.14 | } |
781 : | |||
782 : | start_timer(); | ||
783 : | edgomez | 1.19 | interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16 * x_pos, 16 * y_pos, |
784 : | pMB->mvs[0].x, pMB->mvs[0].y, stride, 0); | ||
785 : | interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16 * x_pos + 8, | ||
786 : | 16 * y_pos, pMB->mvs[1].x, pMB->mvs[1].y, stride, 0); | ||
787 : | interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16 * x_pos, | ||
788 : | 16 * y_pos + 8, pMB->mvs[2].x, pMB->mvs[2].y, stride, | ||
789 : | 0); | ||
790 : | interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16 * x_pos + 8, | ||
791 : | 16 * y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride, | ||
792 : | 0); | ||
793 : | interpolate8x8_switch(dec->cur.u, dec->refn[ref].u, 8 * x_pos, 8 * y_pos, | ||
794 : | uv_dx, uv_dy, stride2, 0); | ||
795 : | interpolate8x8_switch(dec->cur.v, dec->refn[ref].v, 8 * x_pos, 8 * y_pos, | ||
796 : | uv_dx, uv_dy, stride2, 0); | ||
797 : | chenm001 | 1.14 | stop_comp_timer(); |
798 : | |||
799 : | edgomez | 1.19 | for (i = 0; i < 6; i++) { |
800 : | if (cbp & (1 << (5 - i))) // coded | ||
801 : | chenm001 | 1.14 | { |
802 : | edgomez | 1.19 | memset(&block[i * 64], 0, 64 * sizeof(int16_t)); // clear |
803 : | chenm001 | 1.14 | |
804 : | start_timer(); | ||
805 : | edgomez | 1.19 | get_inter_block(bs, &block[i * 64]); |
806 : | chenm001 | 1.14 | stop_coding_timer(); |
807 : | |||
808 : | start_timer(); | ||
809 : | edgomez | 1.19 | if (dec->quant_type == 0) { |
810 : | dequant_inter(&data[i * 64], &block[i * 64], iQuant); | ||
811 : | } else { | ||
812 : | dequant4_inter(&data[i * 64], &block[i * 64], iQuant); | ||
813 : | chenm001 | 1.14 | } |
814 : | stop_iquant_timer(); | ||
815 : | |||
816 : | start_timer(); | ||
817 : | edgomez | 1.19 | idct(&data[i * 64]); |
818 : | chenm001 | 1.14 | stop_idct_timer(); |
819 : | } | ||
820 : | } | ||
821 : | |||
822 : | edgomez | 1.19 | if (dec->interlacing && pMB->field_dct) { |
823 : | chenm001 | 1.14 | next_block = stride; |
824 : | stride *= 2; | ||
825 : | } | ||
826 : | |||
827 : | start_timer(); | ||
828 : | if (cbp & 32) | ||
829 : | edgomez | 1.19 | transfer_16to8add(pY_Cur, &data[0 * 64], stride); |
830 : | chenm001 | 1.14 | if (cbp & 16) |
831 : | edgomez | 1.19 | transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride); |
832 : | chenm001 | 1.14 | if (cbp & 8) |
833 : | edgomez | 1.19 | transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride); |
834 : | chenm001 | 1.14 | if (cbp & 4) |
835 : | edgomez | 1.19 | transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride); |
836 : | chenm001 | 1.14 | if (cbp & 2) |
837 : | edgomez | 1.19 | transfer_16to8add(pU_Cur, &data[4 * 64], stride2); |
838 : | chenm001 | 1.14 | if (cbp & 1) |
839 : | edgomez | 1.19 | transfer_16to8add(pV_Cur, &data[5 * 64], stride2); |
840 : | chenm001 | 1.14 | stop_transfer_timer(); |
841 : | } | ||
842 : | |||
843 : | |||
844 : | // add by MinChen <chenm001@163.com> | ||
845 : | // decode an B-frame direct & inter macroblock | ||
846 : | edgomez | 1.19 | void |
847 : | decoder_bf_interpolate_mbinter(DECODER * dec, | ||
848 : | IMAGE forward, | ||
849 : | IMAGE backward, | ||
850 : | const MACROBLOCK * pMB, | ||
851 : | const uint32_t x_pos, | ||
852 : | const uint32_t y_pos, | ||
853 : | const uint32_t cbp, | ||
854 : | Bitstream * bs) | ||
855 : | chenm001 | 1.14 | { |
856 : | |||
857 : | edgomez | 1.19 | DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE); |
858 : | chenm001 | 1.14 | DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE); |
859 : | |||
860 : | edgomez | 1.19 | uint32_t stride = dec->edged_width; |
861 : | uint32_t stride2 = stride / 2; | ||
862 : | uint32_t next_block = stride * 8; | ||
863 : | uint32_t iQuant = pMB->quant; | ||
864 : | int uv_dx, uv_dy; | ||
865 : | int b_uv_dx, b_uv_dy; | ||
866 : | uint32_t i; | ||
867 : | uint8_t *pY_Cur, *pU_Cur, *pV_Cur; | ||
868 : | chenm001 | 1.15 | |
869 : | pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4); | ||
870 : | pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3); | ||
871 : | pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3); | ||
872 : | chenm001 | 1.14 | |
873 : | edgomez | 1.19 | if ((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)) { |
874 : | chenm001 | 1.14 | uv_dx = pMB->mvs[0].x; |
875 : | uv_dy = pMB->mvs[0].y; | ||
876 : | |||
877 : | uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2; | ||
878 : | uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2; | ||
879 : | |||
880 : | b_uv_dx = pMB->b_mvs[0].x; | ||
881 : | b_uv_dy = pMB->b_mvs[0].y; | ||
882 : | |||
883 : | b_uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2; | ||
884 : | b_uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2; | ||
885 : | edgomez | 1.19 | } else { |
886 : | chenm001 | 1.14 | int sum; |
887 : | edgomez | 1.19 | |
888 : | chenm001 | 1.14 | sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x; |
889 : | edgomez | 1.19 | uv_dx = |
890 : | (sum == | ||
891 : | 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + | ||
892 : | (ABS(sum) / 16) * 2)); | ||
893 : | chenm001 | 1.14 | |
894 : | sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y; | ||
895 : | edgomez | 1.19 | uv_dy = |
896 : | (sum == | ||
897 : | 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + | ||
898 : | (ABS(sum) / 16) * 2)); | ||
899 : | |||
900 : | sum = | ||
901 : | pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x + | ||
902 : | pMB->b_mvs[3].x; | ||
903 : | b_uv_dx = | ||
904 : | (sum == | ||
905 : | 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + | ||
906 : | (ABS(sum) / 16) * 2)); | ||
907 : | |||
908 : | sum = | ||
909 : | pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y + | ||
910 : | pMB->b_mvs[3].y; | ||
911 : | b_uv_dy = | ||
912 : | (sum == | ||
913 : | 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + | ||
914 : | (ABS(sum) / 16) * 2)); | ||
915 : | } | ||
916 : | chenm001 | 1.14 | |
917 : | |||
918 : | start_timer(); | ||
919 : | edgomez | 1.19 | interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos, 16 * y_pos, |
920 : | pMB->mvs[0].x, pMB->mvs[0].y, stride, 0); | ||
921 : | interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8, 16 * y_pos, | ||
922 : | pMB->mvs[1].x, pMB->mvs[1].y, stride, 0); | ||
923 : | interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos, 16 * y_pos + 8, | ||
924 : | pMB->mvs[2].x, pMB->mvs[2].y, stride, 0); | ||
925 : | interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8, | ||
926 : | 16 * y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride, | ||
927 : | 0); | ||
928 : | interpolate8x8_switch(dec->cur.u, forward.u, 8 * x_pos, 8 * y_pos, uv_dx, | ||
929 : | uv_dy, stride2, 0); | ||
930 : | interpolate8x8_switch(dec->cur.v, forward.v, 8 * x_pos, 8 * y_pos, uv_dx, | ||
931 : | uv_dy, stride2, 0); | ||
932 : | |||
933 : | |||
934 : | interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos, 16 * y_pos, | ||
935 : | pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0); | ||
936 : | interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos + 8, | ||
937 : | 16 * y_pos, pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride, | ||
938 : | 0); | ||
939 : | interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos, | ||
940 : | 16 * y_pos + 8, pMB->b_mvs[2].x, pMB->b_mvs[2].y, | ||
941 : | stride, 0); | ||
942 : | interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos + 8, | ||
943 : | 16 * y_pos + 8, pMB->b_mvs[3].x, pMB->b_mvs[3].y, | ||
944 : | stride, 0); | ||
945 : | interpolate8x8_switch(dec->refn[2].u, backward.u, 8 * x_pos, 8 * y_pos, | ||
946 : | b_uv_dx, b_uv_dy, stride2, 0); | ||
947 : | interpolate8x8_switch(dec->refn[2].v, backward.v, 8 * x_pos, 8 * y_pos, | ||
948 : | b_uv_dx, b_uv_dy, stride2, 0); | ||
949 : | |||
950 : | interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16 * x_pos, 16 * y_pos, | ||
951 : | stride); | ||
952 : | interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16 * x_pos + 8, 16 * y_pos, | ||
953 : | stride); | ||
954 : | interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16 * x_pos, 16 * y_pos + 8, | ||
955 : | stride); | ||
956 : | interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16 * x_pos + 8, | ||
957 : | 16 * y_pos + 8, stride); | ||
958 : | interpolate8x8_c(dec->cur.u, dec->refn[2].u, 8 * x_pos, 8 * y_pos, | ||
959 : | stride2); | ||
960 : | interpolate8x8_c(dec->cur.v, dec->refn[2].v, 8 * x_pos, 8 * y_pos, | ||
961 : | stride2); | ||
962 : | chenm001 | 1.14 | |
963 : | stop_comp_timer(); | ||
964 : | edgomez | 1.19 | |
965 : | for (i = 0; i < 6; i++) { | ||
966 : | if (cbp & (1 << (5 - i))) // coded | ||
967 : | chenm001 | 1.15 | { |
968 : | edgomez | 1.19 | memset(&block[i * 64], 0, 64 * sizeof(int16_t)); // clear |
969 : | chenm001 | 1.14 | |
970 : | chenm001 | 1.15 | start_timer(); |
971 : | edgomez | 1.19 | get_inter_block(bs, &block[i * 64]); |
972 : | chenm001 | 1.15 | stop_coding_timer(); |
973 : | |||
974 : | start_timer(); | ||
975 : | edgomez | 1.19 | if (dec->quant_type == 0) { |
976 : | dequant_inter(&data[i * 64], &block[i * 64], iQuant); | ||
977 : | } else { | ||
978 : | dequant4_inter(&data[i * 64], &block[i * 64], iQuant); | ||
979 : | chenm001 | 1.15 | } |
980 : | stop_iquant_timer(); | ||
981 : | |||
982 : | start_timer(); | ||
983 : | edgomez | 1.19 | idct(&data[i * 64]); |
984 : | chenm001 | 1.15 | stop_idct_timer(); |
985 : | } | ||
986 : | } | ||
987 : | |||
988 : | edgomez | 1.19 | if (dec->interlacing && pMB->field_dct) { |
989 : | chenm001 | 1.15 | next_block = stride; |
990 : | stride *= 2; | ||
991 : | } | ||
992 : | |||
993 : | start_timer(); | ||
994 : | if (cbp & 32) | ||
995 : | edgomez | 1.19 | transfer_16to8add(pY_Cur, &data[0 * 64], stride); |
996 : | chenm001 | 1.15 | if (cbp & 16) |
997 : | edgomez | 1.19 | transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride); |
998 : | chenm001 | 1.15 | if (cbp & 8) |
999 : | edgomez | 1.19 | transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride); |
1000 : | chenm001 | 1.15 | if (cbp & 4) |
1001 : | edgomez | 1.19 | transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride); |
1002 : | chenm001 | 1.15 | if (cbp & 2) |
1003 : | edgomez | 1.19 | transfer_16to8add(pU_Cur, &data[4 * 64], stride2); |
1004 : | chenm001 | 1.15 | if (cbp & 1) |
1005 : | edgomez | 1.19 | transfer_16to8add(pV_Cur, &data[5 * 64], stride2); |
1006 : | chenm001 | 1.15 | stop_transfer_timer(); |
1007 : | chenm001 | 1.14 | } |
1008 : | |||
1009 : | |||
1010 : | // add by MinChen <chenm001@163.com> | ||
1011 : | // for decode B-frame dbquant | ||
1012 : | edgomez | 1.19 | int32_t __inline |
1013 : | get_dbquant(Bitstream * bs) | ||
1014 : | chenm001 | 1.14 | { |
1015 : | edgomez | 1.19 | if (!BitstreamGetBit(bs)) // '0' |
1016 : | return (0); | ||
1017 : | chenm001 | 1.14 | else if (!BitstreamGetBit(bs)) // '10' |
1018 : | edgomez | 1.19 | return (-2); |
1019 : | chenm001 | 1.14 | else |
1020 : | edgomez | 1.19 | return (2); // '11' |
1021 : | chenm001 | 1.14 | } |
1022 : | |||
1023 : | // add by MinChen <chenm001@163.com> | ||
1024 : | // for decode B-frame mb_type | ||
1025 : | // bit ret_value | ||
1026 : | // 1 0 | ||
1027 : | // 01 1 | ||
1028 : | // 001 2 | ||
1029 : | // 0001 3 | ||
1030 : | edgomez | 1.19 | int32_t __inline |
1031 : | get_mbtype(Bitstream * bs) | ||
1032 : | chenm001 | 1.14 | { |
1033 : | chenm001 | 1.15 | int32_t mb_type; |
1034 : | edgomez | 1.19 | |
1035 : | for (mb_type = 0; mb_type <= 3; mb_type++) { | ||
1036 : | if (BitstreamGetBit(bs)) | ||
1037 : | chenm001 | 1.15 | break; |
1038 : | } | ||
1039 : | |||
1040 : | edgomez | 1.19 | if (mb_type <= 3) |
1041 : | return (mb_type); | ||
1042 : | chenm001 | 1.14 | else |
1043 : | edgomez | 1.19 | return (-1); |
1044 : | chenm001 | 1.14 | } |
1045 : | |||
1046 : | edgomez | 1.19 | void |
1047 : | decoder_bframe(DECODER * dec, | ||
1048 : | Bitstream * bs, | ||
1049 : | int quant, | ||
1050 : | int fcode_forward, | ||
1051 : | int fcode_backward) | ||
1052 : | chenm001 | 1.14 | { |
1053 : | |||
1054 : | edgomez | 1.19 | uint32_t x, y; |
1055 : | VECTOR mv, zeromv; | ||
1056 : | chenm001 | 1.14 | |
1057 : | start_timer(); | ||
1058 : | edgomez | 1.19 | image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, |
1059 : | dec->width, dec->height, dec->interlacing); | ||
1060 : | chenm001 | 1.14 | //image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height, dec->width, dec->height, dec->interlacing); |
1061 : | stop_edges_timer(); | ||
1062 : | |||
1063 : | chenm001 | 1.15 | |
1064 : | edgomez | 1.19 | for (y = 0; y < dec->mb_height; y++) { |
1065 : | chenm001 | 1.15 | // Initialize Pred Motion Vector |
1066 : | dec->p_fmv.x = dec->p_fmv.y = dec->p_bmv.x = dec->p_bmv.y = 0; | ||
1067 : | edgomez | 1.19 | for (x = 0; x < dec->mb_width; x++) { |
1068 : | MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x]; | ||
1069 : | MACROBLOCK *last_mb = &dec->last_mbs[y * dec->mb_width + x]; | ||
1070 : | chenm001 | 1.14 | |
1071 : | edgomez | 1.19 | mb->mvs[0].x = mb->mvs[0].y = zeromv.x = zeromv.y = mv.x = mv.y = |
1072 : | 0; | ||
1073 : | chenm001 | 1.15 | |
1074 : | chenm001 | 1.14 | // the last P_VOP is skip macroblock ? |
1075 : | edgomez | 1.19 | if (last_mb->mode == MODE_NOT_CODED) { |
1076 : | chenm001 | 1.15 | //DEBUG2("Skip MB in B-frame at (X,Y)=!",x,y); |
1077 : | mb->mb_type = MODE_FORWARD; | ||
1078 : | mb->cbp = 0; | ||
1079 : | mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x; | ||
1080 : | mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y; | ||
1081 : | mb->quant = 8; | ||
1082 : | chenm001 | 1.14 | |
1083 : | chenm001 | 1.15 | decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 1); |
1084 : | chenm001 | 1.14 | continue; |
1085 : | } | ||
1086 : | chenm001 | 1.15 | //t=BitstreamShowBits(bs,32); |
1087 : | |||
1088 : | edgomez | 1.19 | if (!BitstreamGetBit(bs)) { // modb=='0' |
1089 : | const uint8_t modb2 = BitstreamGetBit(bs); | ||
1090 : | chenm001 | 1.14 | |
1091 : | mb->mb_type = get_mbtype(bs); | ||
1092 : | edgomez | 1.19 | |
1093 : | if (!modb2) { // modb=='00' | ||
1094 : | mb->cbp = BitstreamGetBits(bs, 6); | ||
1095 : | chenm001 | 1.14 | } else { |
1096 : | mb->cbp = 0; | ||
1097 : | } | ||
1098 : | edgomez | 1.19 | if (mb->mb_type && mb->cbp) { |
1099 : | chenm001 | 1.14 | quant += get_dbquant(bs); |
1100 : | |||
1101 : | edgomez | 1.19 | if (quant > 31) { |
1102 : | chenm001 | 1.14 | quant = 31; |
1103 : | edgomez | 1.19 | } else if (mb->quant < 1) { |
1104 : | chenm001 | 1.14 | quant = 1; |
1105 : | } | ||
1106 : | chenm001 | 1.15 | } else { |
1107 : | quant = 8; | ||
1108 : | chenm001 | 1.14 | } |
1109 : | mb->quant = quant; | ||
1110 : | } else { | ||
1111 : | mb->mb_type = MODE_DIRECT_NONE_MV; | ||
1112 : | edgomez | 1.19 | mb->cbp = 0; |
1113 : | chenm001 | 1.14 | } |
1114 : | |||
1115 : | chenm001 | 1.15 | mb->mode = MODE_INTER; |
1116 : | chenm001 | 1.14 | //DEBUG1("Switch bm_type=",mb->mb_type); |
1117 : | |||
1118 : | edgomez | 1.19 | switch (mb->mb_type) { |
1119 : | chenm001 | 1.14 | case MODE_DIRECT: |
1120 : | get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], 1, zeromv); | ||
1121 : | |||
1122 : | case MODE_DIRECT_NONE_MV: | ||
1123 : | edgomez | 1.19 | { // Because this file is a C file not C++ so I use '{' to define var |
1124 : | const int64_t TRB = dec->time_pp - dec->time_bp, TRD = | ||
1125 : | dec->time_pp; | ||
1126 : | chenm001 | 1.14 | int i; |
1127 : | edgomez | 1.19 | |
1128 : | for (i = 0; i < 4; i++) { | ||
1129 : | mb->mvs[i].x = | ||
1130 : | (int32_t) ((TRB * last_mb->mvs[i].x) / TRD + | ||
1131 : | mb->mvs[0].x); | ||
1132 : | mb->b_mvs[i].x = | ||
1133 : | (int32_t) ((mb->mvs[0].x == | ||
1134 : | 0) ? ((TRB - | ||
1135 : | TRD) * last_mb->mvs[i].x) / | ||
1136 : | TRD : mb->mvs[i].x - last_mb->mvs[i].x); | ||
1137 : | mb->mvs[i].y = | ||
1138 : | (int32_t) ((TRB * last_mb->mvs[i].y) / TRD + | ||
1139 : | mb->mvs[0].y); | ||
1140 : | mb->b_mvs[i].y = | ||
1141 : | (int32_t) ((mb->mvs[0].y == | ||
1142 : | 0) ? ((TRB - | ||
1143 : | TRD) * last_mb->mvs[i].y) / | ||
1144 : | TRD : mb->mvs[i].y - last_mb->mvs[i].y); | ||
1145 : | chenm001 | 1.14 | } |
1146 : | //DEBUG("B-frame Direct!\n"); | ||
1147 : | } | ||
1148 : | mb->mode = MODE_INTER4V; | ||
1149 : | edgomez | 1.19 | decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0], |
1150 : | mb, x, y, mb->cbp, bs); | ||
1151 : | chenm001 | 1.14 | break; |
1152 : | |||
1153 : | case MODE_INTERPOLATE: | ||
1154 : | edgomez | 1.19 | get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward, |
1155 : | dec->p_fmv); | ||
1156 : | dec->p_fmv.x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = | ||
1157 : | mb->mvs[0].x; | ||
1158 : | dec->p_fmv.y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = | ||
1159 : | mb->mvs[0].y; | ||
1160 : | |||
1161 : | get_b_motion_vector(dec, bs, x, y, &mb->b_mvs[0], | ||
1162 : | fcode_backward, dec->p_bmv); | ||
1163 : | dec->p_bmv.x = mb->b_mvs[1].x = mb->b_mvs[2].x = | ||
1164 : | mb->b_mvs[3].x = mb->b_mvs[0].x; | ||
1165 : | dec->p_bmv.y = mb->b_mvs[1].y = mb->b_mvs[2].y = | ||
1166 : | mb->b_mvs[3].y = mb->b_mvs[0].y; | ||
1167 : | chenm001 | 1.14 | |
1168 : | edgomez | 1.19 | decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0], |
1169 : | mb, x, y, mb->cbp, bs); | ||
1170 : | chenm001 | 1.14 | //DEBUG("B-frame Bidir!\n"); |
1171 : | break; | ||
1172 : | |||
1173 : | case MODE_BACKWARD: | ||
1174 : | edgomez | 1.19 | get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_backward, |
1175 : | dec->p_bmv); | ||
1176 : | dec->p_bmv.x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = | ||
1177 : | mb->mvs[0].x; | ||
1178 : | dec->p_bmv.y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = | ||
1179 : | mb->mvs[0].y; | ||
1180 : | chenm001 | 1.15 | |
1181 : | chenm001 | 1.14 | decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 0); |
1182 : | //DEBUG("B-frame Backward!\n"); | ||
1183 : | break; | ||
1184 : | |||
1185 : | case MODE_FORWARD: | ||
1186 : | edgomez | 1.19 | get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward, |
1187 : | dec->p_fmv); | ||
1188 : | dec->p_fmv.x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = | ||
1189 : | mb->mvs[0].x; | ||
1190 : | dec->p_fmv.y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = | ||
1191 : | mb->mvs[0].y; | ||
1192 : | chenm001 | 1.15 | |
1193 : | chenm001 | 1.14 | decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 1); |
1194 : | chenm001 | 1.15 | //DEBUG("B-frame Forward!\n"); |
1195 : | chenm001 | 1.14 | break; |
1196 : | |||
1197 : | default: | ||
1198 : | edgomez | 1.19 | DEBUG1("Not support B-frame mb_type =", mb->mb_type); |
1199 : | chenm001 | 1.14 | } |
1200 : | |||
1201 : | edgomez | 1.19 | } // end of FOR |
1202 : | chenm001 | 1.14 | } |
1203 : | } | ||
1204 : | |||
1205 : | // swap two MACROBLOCK array | ||
1206 : | edgomez | 1.19 | void |
1207 : | mb_swap(MACROBLOCK ** mb1, | ||
1208 : | MACROBLOCK ** mb2) | ||
1209 : | chenm001 | 1.14 | { |
1210 : | edgomez | 1.19 | MACROBLOCK *temp = *mb1; |
1211 : | |||
1212 : | *mb1 = *mb2; | ||
1213 : | *mb2 = temp; | ||
1214 : | chenm001 | 1.14 | } |
1215 : | |||
1216 : | edgomez | 1.19 | int |
1217 : | decoder_decode(DECODER * dec, | ||
1218 : | XVID_DEC_FRAME * frame) | ||
1219 : | Isibaar | 1.1 | { |
1220 : | edgomez | 1.7 | |
1221 : | Isibaar | 1.1 | Bitstream bs; |
1222 : | uint32_t rounding; | ||
1223 : | uint32_t quant; | ||
1224 : | chenm001 | 1.14 | uint32_t fcode_forward; |
1225 : | uint32_t fcode_backward; | ||
1226 : | Isibaar | 1.1 | uint32_t intra_dc_threshold; |
1227 : | chenm001 | 1.11 | uint32_t vop_type; |
1228 : | Isibaar | 1.1 | |
1229 : | start_global_timer(); | ||
1230 : | edgomez | 1.19 | |
1231 : | Isibaar | 1.1 | BitstreamInit(&bs, frame->bitstream, frame->length); |
1232 : | |||
1233 : | chenm001 | 1.11 | // add by chenm001 <chenm001@163.com> |
1234 : | // for support B-frame to reference last 2 frame | ||
1235 : | Isibaar | 1.18 | dec->frames++; |
1236 : | edgomez | 1.19 | vop_type = |
1237 : | BitstreamReadHeaders(&bs, dec, &rounding, &quant, &fcode_forward, | ||
1238 : | &fcode_backward, &intra_dc_threshold); | ||
1239 : | |||
1240 : | dec->p_bmv.x = dec->p_bmv.y = dec->p_fmv.y = dec->p_fmv.y = 0; // init pred vector to 0 | ||
1241 : | |||
1242 : | switch (vop_type) { | ||
1243 : | case P_VOP: | ||
1244 : | decoder_pframe(dec, &bs, rounding, quant, fcode_forward, | ||
1245 : | intra_dc_threshold); | ||
1246 : | DEBUG1("P_VOP Time=", dec->time); | ||
1247 : | Isibaar | 1.1 | break; |
1248 : | |||
1249 : | edgomez | 1.19 | case I_VOP: |
1250 : | Isibaar | 1.1 | decoder_iframe(dec, &bs, quant, intra_dc_threshold); |
1251 : | edgomez | 1.19 | DEBUG1("I_VOP Time=", dec->time); |
1252 : | Isibaar | 1.1 | break; |
1253 : | |||
1254 : | edgomez | 1.19 | case B_VOP: |
1255 : | suxen_drol | 1.21 | #ifdef BFRAMES_DEC |
1256 : | edgomez | 1.19 | if (dec->time_pp > dec->time_bp) { |
1257 : | DEBUG1("B_VOP Time=", dec->time); | ||
1258 : | chenm001 | 1.14 | decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward); |
1259 : | } else { | ||
1260 : | DEBUG("broken B-frame!"); | ||
1261 : | } | ||
1262 : | suxen_drol | 1.21 | #else |
1263 : | image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height); | ||
1264 : | Isibaar | 1.18 | #endif |
1265 : | Isibaar | 1.1 | break; |
1266 : | edgomez | 1.19 | |
1267 : | case N_VOP: // vop not coded | ||
1268 : | suxen_drol | 1.21 | // when low_delay==0, N_VOP's should interpolate between the past and future frames |
1269 : | image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height); | ||
1270 : | Isibaar | 1.1 | break; |
1271 : | |||
1272 : | edgomez | 1.19 | default: |
1273 : | Isibaar | 1.1 | return XVID_ERR_FAIL; |
1274 : | } | ||
1275 : | |||
1276 : | frame->length = BitstreamPos(&bs) / 8; | ||
1277 : | |||
1278 : | suxen_drol | 1.21 | #ifdef BFRAMES_DEC |
1279 : | chenm001 | 1.17 | // test if no B_VOP |
1280 : | edgomez | 1.19 | if (dec->low_delay) { |
1281 : | Isibaar | 1.18 | #endif |
1282 : | edgomez | 1.19 | image_output(&dec->cur, dec->width, dec->height, dec->edged_width, |
1283 : | frame->image, frame->stride, frame->colorspace); | ||
1284 : | suxen_drol | 1.21 | #ifdef BFRAMES_DEC |
1285 : | chenm001 | 1.17 | } else { |
1286 : | edgomez | 1.19 | if (dec->frames >= 1) { |
1287 : | chenm001 | 1.17 | start_timer(); |
1288 : | edgomez | 1.19 | if ((vop_type == I_VOP || vop_type == P_VOP)) { |
1289 : | image_output(&dec->refn[0], dec->width, dec->height, | ||
1290 : | dec->edged_width, frame->image, frame->stride, | ||
1291 : | frame->colorspace); | ||
1292 : | chenm001 | 1.17 | } else if (vop_type == B_VOP) { |
1293 : | edgomez | 1.19 | image_output(&dec->cur, dec->width, dec->height, |
1294 : | dec->edged_width, frame->image, frame->stride, | ||
1295 : | frame->colorspace); | ||
1296 : | chenm001 | 1.17 | } |
1297 : | stop_conv_timer(); | ||
1298 : | chenm001 | 1.14 | } |
1299 : | } | ||
1300 : | Isibaar | 1.18 | #endif |
1301 : | |||
1302 : | edgomez | 1.19 | if (vop_type == I_VOP || vop_type == P_VOP) { |
1303 : | chenm001 | 1.14 | image_swap(&dec->refn[0], &dec->refn[1]); |
1304 : | image_swap(&dec->cur, &dec->refn[0]); | ||
1305 : | // swap MACROBLOCK | ||
1306 : | edgomez | 1.19 | if (dec->low_delay && vop_type == P_VOP) |
1307 : | chenm001 | 1.15 | mb_swap(&dec->mbs, &dec->last_mbs); |
1308 : | chenm001 | 1.14 | } |
1309 : | |||
1310 : | Isibaar | 1.1 | emms(); |
1311 : | |||
1312 : | stop_global_timer(); | ||
1313 : | |||
1314 : | return XVID_ERR_OK; | ||
1315 : | } |
No admin address has been configured | ViewVC Help |
Powered by ViewVC 1.0.4 |