3 * Copyright (C) 2000-2003 Michel Lespinasse <walken@zoy.org>
4 * Copyright (C) 2003 Peter Gubanov <peter@elecard.net.ru>
5 * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
7 * This file is part of mpeg2dec, a free MPEG-2 video stream decoder.
8 * See http://libmpeg2.sourceforge.net/ for updates.
10 * mpeg2dec is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * mpeg2dec is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "xxmc-config.h"
30 #include "attributes.h"
31 #include "mpeg2_internal.h"
33 extern mpeg2_mc_t mpeg2_mc;
34 extern void (* mpeg2_cpu_state_save) (cpu_state_t * state);
35 extern void (* mpeg2_cpu_state_restore) (cpu_state_t * state);
39 static inline int get_motion_delta( mpeg2_decoder_t * const decoder,
42 #define bit_buf (decoder->bitstream_buf)
43 #define bits (decoder->bitstream_bits)
44 #define bit_ptr (decoder->bitstream_ptr)
50 if( bit_buf & 0x80000000 )
52 DUMPBITS (bit_buf, bits, 1);
55 else if (bit_buf >= 0x0c000000)
57 tab = MV_4 + UBITS (bit_buf, 4);
58 delta = (tab->delta << f_code) + 1;
59 bits += tab->len + f_code + 1;
62 sign = SBITS (bit_buf, 1);
66 delta += UBITS (bit_buf, f_code);
68 return (delta ^ sign) - sign;
72 tab = MV_10 + UBITS (bit_buf, 10);
73 delta = (tab->delta << f_code) + 1;
77 sign = SBITS (bit_buf, 1);
82 NEEDBITS (bit_buf, bits, bit_ptr);
83 delta += UBITS (bit_buf, f_code);
84 DUMPBITS (bit_buf, bits, f_code);
87 return (delta ^ sign) - sign;
94 static inline int bound_motion_vector (const int vector, const int f_code)
96 return ((int32_t)vector << (27 - f_code)) >> (27 - f_code);
99 static inline int get_dmv (mpeg2_decoder_t * const decoder)
101 #define bit_buf (decoder->bitstream_buf)
102 #define bits (decoder->bitstream_bits)
103 #define bit_ptr (decoder->bitstream_ptr)
107 tab = DMV_2 + UBITS (bit_buf, 2);
108 DUMPBITS (bit_buf, bits, tab->len);
115 #define MOTION_420(table,ref,motion_x,motion_y,size,y) \
116 pos_x = 2 * decoder->offset + motion_x; \
117 pos_y = 2 * decoder->v_offset + motion_y + 2 * y; \
118 if (unlikely (pos_x > decoder->limit_x)) { \
119 pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x; \
120 motion_x = pos_x - 2 * decoder->offset; \
122 if (unlikely (pos_y > decoder->limit_y_ ## size)) { \
123 pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y_ ## size; \
124 motion_y = pos_y - 2 * decoder->v_offset - 2 * y; \
126 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \
127 table[xy_half] (decoder->dest[0] + y * decoder->stride + decoder->offset, \
128 ref[0] + (pos_x >> 1) + (pos_y >> 1) * decoder->stride, \
129 decoder->stride, size); \
130 motion_x /= 2; motion_y /= 2; \
131 xy_half = ((motion_y & 1) << 1) | (motion_x & 1); \
132 offset = (((decoder->offset + motion_x) >> 1) + \
133 ((((decoder->v_offset + motion_y) >> 1) + y/2) * \
134 decoder->uv_stride)); \
135 table[4+xy_half] (decoder->dest[1] + y/2 * decoder->uv_stride + \
136 (decoder->offset >> 1), ref[1] + offset, \
137 decoder->uv_stride, size/2); \
138 table[4+xy_half] (decoder->dest[2] + y/2 * decoder->uv_stride + \
139 (decoder->offset >> 1), ref[2] + offset, \
140 decoder->uv_stride, size/2)
142 #define MOTION_FIELD_420(table,ref,motion_x,motion_y,dest_field,op,src_field) \
143 pos_x = 2 * decoder->offset + motion_x; \
144 pos_y = decoder->v_offset + motion_y; \
145 if (unlikely (pos_x > decoder->limit_x)) { \
146 pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x; \
147 motion_x = pos_x - 2 * decoder->offset; \
149 if (unlikely (pos_y > decoder->limit_y)) { \
150 pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y; \
151 motion_y = pos_y - decoder->v_offset; \
153 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \
154 table[xy_half] (decoder->dest[0] + dest_field * decoder->stride + \
156 (ref[0] + (pos_x >> 1) + \
157 ((pos_y op) + src_field) * decoder->stride), \
158 2 * decoder->stride, 8); \
159 motion_x /= 2; motion_y /= 2; \
160 xy_half = ((motion_y & 1) << 1) | (motion_x & 1); \
161 offset = (((decoder->offset + motion_x) >> 1) + \
162 (((decoder->v_offset >> 1) + (motion_y op) + src_field) * \
163 decoder->uv_stride)); \
164 table[4+xy_half] (decoder->dest[1] + dest_field * decoder->uv_stride + \
165 (decoder->offset >> 1), ref[1] + offset, \
166 2 * decoder->uv_stride, 4); \
167 table[4+xy_half] (decoder->dest[2] + dest_field * decoder->uv_stride + \
168 (decoder->offset >> 1), ref[2] + offset, \
169 2 * decoder->uv_stride, 4)
171 #define MOTION_DMV_420(table,ref,motion_x,motion_y) \
172 pos_x = 2 * decoder->offset + motion_x; \
173 pos_y = decoder->v_offset + motion_y; \
174 if (unlikely (pos_x > decoder->limit_x)) { \
175 pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x; \
176 motion_x = pos_x - 2 * decoder->offset; \
178 if (unlikely (pos_y > decoder->limit_y)) { \
179 pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y; \
180 motion_y = pos_y - decoder->v_offset; \
182 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \
183 offset = (pos_x >> 1) + (pos_y & ~1) * decoder->stride; \
184 table[xy_half] (decoder->dest[0] + decoder->offset, \
185 ref[0] + offset, 2 * decoder->stride, 8); \
186 table[xy_half] (decoder->dest[0] + decoder->stride + decoder->offset, \
187 ref[0] + decoder->stride + offset, \
188 2 * decoder->stride, 8); \
189 motion_x /= 2; motion_y /= 2; \
190 xy_half = ((motion_y & 1) << 1) | (motion_x & 1); \
191 offset = (((decoder->offset + motion_x) >> 1) + \
192 (((decoder->v_offset >> 1) + (motion_y & ~1)) * \
193 decoder->uv_stride)); \
194 table[4+xy_half] (decoder->dest[1] + (decoder->offset >> 1), \
195 ref[1] + offset, 2 * decoder->uv_stride, 4); \
196 table[4+xy_half] (decoder->dest[1] + decoder->uv_stride + \
197 (decoder->offset >> 1), \
198 ref[1] + decoder->uv_stride + offset, \
199 2 * decoder->uv_stride, 4); \
200 table[4+xy_half] (decoder->dest[2] + (decoder->offset >> 1), \
201 ref[2] + offset, 2 * decoder->uv_stride, 4); \
202 table[4+xy_half] (decoder->dest[2] + decoder->uv_stride + \
203 (decoder->offset >> 1), \
204 ref[2] + decoder->uv_stride + offset, \
205 2 * decoder->uv_stride, 4)
207 #define MOTION_ZERO_420(table,ref) \
208 table[0] (decoder->dest[0] + decoder->offset, \
209 (ref[0] + decoder->offset + \
210 decoder->v_offset * decoder->stride), decoder->stride, 16); \
211 offset = ((decoder->offset >> 1) + \
212 (decoder->v_offset >> 1) * decoder->uv_stride); \
213 table[4] (decoder->dest[1] + (decoder->offset >> 1), \
214 ref[1] + offset, decoder->uv_stride, 8); \
215 table[4] (decoder->dest[2] + (decoder->offset >> 1), \
216 ref[2] + offset, decoder->uv_stride, 8)
218 #define MOTION_422(table,ref,motion_x,motion_y,size,y) \
219 pos_x = 2 * decoder->offset + motion_x; \
220 pos_y = 2 * decoder->v_offset + motion_y + 2 * y; \
221 if (unlikely (pos_x > decoder->limit_x)) { \
222 pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x; \
223 motion_x = pos_x - 2 * decoder->offset; \
225 if (unlikely (pos_y > decoder->limit_y_ ## size)) { \
226 pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y_ ## size; \
227 motion_y = pos_y - 2 * decoder->v_offset - 2 * y; \
229 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \
230 offset = (pos_x >> 1) + (pos_y >> 1) * decoder->stride; \
231 table[xy_half] (decoder->dest[0] + y * decoder->stride + decoder->offset, \
232 ref[0] + offset, decoder->stride, size); \
233 offset = (offset + (motion_x & (motion_x < 0))) >> 1; \
235 xy_half = ((pos_y & 1) << 1) | (motion_x & 1); \
236 table[4+xy_half] (decoder->dest[1] + y * decoder->uv_stride + \
237 (decoder->offset >> 1), ref[1] + offset, \
238 decoder->uv_stride, size); \
239 table[4+xy_half] (decoder->dest[2] + y * decoder->uv_stride + \
240 (decoder->offset >> 1), ref[2] + offset, \
241 decoder->uv_stride, size)
243 #define MOTION_FIELD_422(table,ref,motion_x,motion_y,dest_field,op,src_field) \
244 pos_x = 2 * decoder->offset + motion_x; \
245 pos_y = decoder->v_offset + motion_y; \
246 if (unlikely (pos_x > decoder->limit_x)) { \
247 pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x; \
248 motion_x = pos_x - 2 * decoder->offset; \
250 if (unlikely (pos_y > decoder->limit_y)) { \
251 pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y; \
252 motion_y = pos_y - decoder->v_offset; \
254 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \
255 offset = (pos_x >> 1) + ((pos_y op) + src_field) * decoder->stride; \
256 table[xy_half] (decoder->dest[0] + dest_field * decoder->stride + \
257 decoder->offset, ref[0] + offset, \
258 2 * decoder->stride, 8); \
259 offset = (offset + (motion_x & (motion_x < 0))) >> 1; \
261 xy_half = ((pos_y & 1) << 1) | (motion_x & 1); \
262 table[4+xy_half] (decoder->dest[1] + dest_field * decoder->uv_stride + \
263 (decoder->offset >> 1), ref[1] + offset, \
264 2 * decoder->uv_stride, 8); \
265 table[4+xy_half] (decoder->dest[2] + dest_field * decoder->uv_stride + \
266 (decoder->offset >> 1), ref[2] + offset, \
267 2 * decoder->uv_stride, 8)
269 #define MOTION_DMV_422(table,ref,motion_x,motion_y) \
270 pos_x = 2 * decoder->offset + motion_x; \
271 pos_y = decoder->v_offset + motion_y; \
272 if (unlikely (pos_x > decoder->limit_x)) { \
273 pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x; \
274 motion_x = pos_x - 2 * decoder->offset; \
276 if (unlikely (pos_y > decoder->limit_y)) { \
277 pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y; \
278 motion_y = pos_y - decoder->v_offset; \
280 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \
281 offset = (pos_x >> 1) + (pos_y & ~1) * decoder->stride; \
282 table[xy_half] (decoder->dest[0] + decoder->offset, \
283 ref[0] + offset, 2 * decoder->stride, 8); \
284 table[xy_half] (decoder->dest[0] + decoder->stride + decoder->offset, \
285 ref[0] + decoder->stride + offset, \
286 2 * decoder->stride, 8); \
287 offset = (offset + (motion_x & (motion_x < 0))) >> 1; \
289 xy_half = ((pos_y & 1) << 1) | (motion_x & 1); \
290 table[4+xy_half] (decoder->dest[1] + (decoder->offset >> 1), \
291 ref[1] + offset, 2 * decoder->uv_stride, 8); \
292 table[4+xy_half] (decoder->dest[1] + decoder->uv_stride + \
293 (decoder->offset >> 1), \
294 ref[1] + decoder->uv_stride + offset, \
295 2 * decoder->uv_stride, 8); \
296 table[4+xy_half] (decoder->dest[2] + (decoder->offset >> 1), \
297 ref[2] + offset, 2 * decoder->uv_stride, 8); \
298 table[4+xy_half] (decoder->dest[2] + decoder->uv_stride + \
299 (decoder->offset >> 1), \
300 ref[2] + decoder->uv_stride + offset, \
301 2 * decoder->uv_stride, 8)
303 #define MOTION_ZERO_422(table,ref) \
304 offset = decoder->offset + decoder->v_offset * decoder->stride; \
305 table[0] (decoder->dest[0] + decoder->offset, \
306 ref[0] + offset, decoder->stride, 16); \
308 table[4] (decoder->dest[1] + (decoder->offset >> 1), \
309 ref[1] + offset, decoder->uv_stride, 16); \
310 table[4] (decoder->dest[2] + (decoder->offset >> 1), \
311 ref[2] + offset, decoder->uv_stride, 16)
313 #define MOTION_444(table,ref,motion_x,motion_y,size,y) \
314 pos_x = 2 * decoder->offset + motion_x; \
315 pos_y = 2 * decoder->v_offset + motion_y + 2 * y; \
316 if (unlikely (pos_x > decoder->limit_x)) { \
317 pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x; \
318 motion_x = pos_x - 2 * decoder->offset; \
320 if (unlikely (pos_y > decoder->limit_y_ ## size)) { \
321 pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y_ ## size; \
322 motion_y = pos_y - 2 * decoder->v_offset - 2 * y; \
324 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \
325 offset = (pos_x >> 1) + (pos_y >> 1) * decoder->stride; \
326 table[xy_half] (decoder->dest[0] + y * decoder->stride + decoder->offset, \
327 ref[0] + offset, decoder->stride, size); \
328 table[xy_half] (decoder->dest[1] + y * decoder->stride + decoder->offset, \
329 ref[1] + offset, decoder->stride, size); \
330 table[xy_half] (decoder->dest[2] + y * decoder->stride + decoder->offset, \
331 ref[2] + offset, decoder->stride, size)
333 #define MOTION_FIELD_444(table,ref,motion_x,motion_y,dest_field,op,src_field) \
334 pos_x = 2 * decoder->offset + motion_x; \
335 pos_y = decoder->v_offset + motion_y; \
336 if (unlikely (pos_x > decoder->limit_x)) { \
337 pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x; \
338 motion_x = pos_x - 2 * decoder->offset; \
340 if (unlikely (pos_y > decoder->limit_y)) { \
341 pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y; \
342 motion_y = pos_y - decoder->v_offset; \
344 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \
345 offset = (pos_x >> 1) + ((pos_y op) + src_field) * decoder->stride; \
346 table[xy_half] (decoder->dest[0] + dest_field * decoder->stride + \
347 decoder->offset, ref[0] + offset, \
348 2 * decoder->stride, 8); \
349 table[xy_half] (decoder->dest[1] + dest_field * decoder->stride + \
350 decoder->offset, ref[1] + offset, \
351 2 * decoder->stride, 8); \
352 table[xy_half] (decoder->dest[2] + dest_field * decoder->stride + \
353 decoder->offset, ref[2] + offset, \
354 2 * decoder->stride, 8)
356 #define MOTION_DMV_444(table,ref,motion_x,motion_y) \
357 pos_x = 2 * decoder->offset + motion_x; \
358 pos_y = decoder->v_offset + motion_y; \
359 if (unlikely (pos_x > decoder->limit_x)) { \
360 pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x; \
361 motion_x = pos_x - 2 * decoder->offset; \
363 if (unlikely (pos_y > decoder->limit_y)) { \
364 pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y; \
365 motion_y = pos_y - decoder->v_offset; \
367 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \
368 offset = (pos_x >> 1) + (pos_y & ~1) * decoder->stride; \
369 table[xy_half] (decoder->dest[0] + decoder->offset, \
370 ref[0] + offset, 2 * decoder->stride, 8); \
371 table[xy_half] (decoder->dest[0] + decoder->stride + decoder->offset, \
372 ref[0] + decoder->stride + offset, \
373 2 * decoder->stride, 8); \
374 table[xy_half] (decoder->dest[1] + decoder->offset, \
375 ref[1] + offset, 2 * decoder->stride, 8); \
376 table[xy_half] (decoder->dest[1] + decoder->stride + decoder->offset, \
377 ref[1] + decoder->stride + offset, \
378 2 * decoder->stride, 8); \
379 table[xy_half] (decoder->dest[2] + decoder->offset, \
380 ref[2] + offset, 2 * decoder->stride, 8); \
381 table[xy_half] (decoder->dest[2] + decoder->stride + decoder->offset, \
382 ref[2] + decoder->stride + offset, \
383 2 * decoder->stride, 8)
385 #define MOTION_ZERO_444(table,ref) \
386 offset = decoder->offset + decoder->v_offset * decoder->stride; \
387 table[0] (decoder->dest[0] + decoder->offset, \
388 ref[0] + offset, decoder->stride, 16); \
389 table[4] (decoder->dest[1] + decoder->offset, \
390 ref[1] + offset, decoder->stride, 16); \
391 table[4] (decoder->dest[2] + (decoder->offset >> 1), \
392 ref[2] + offset, decoder->stride, 16)
394 #define bit_buf (decoder->bitstream_buf)
395 #define bits (decoder->bitstream_bits)
396 #define bit_ptr (decoder->bitstream_ptr)
398 static void motion_mp1 (mpeg2_decoder_t * const decoder,
399 motion_t * const motion,
400 mpeg2_mc_fct * const * const table)
402 int motion_x, motion_y;
403 unsigned int pos_x, pos_y, xy_half, offset;
405 NEEDBITS (bit_buf, bits, bit_ptr);
406 motion_x = (motion->pmv[0][0] +
407 (get_motion_delta( decoder,
408 motion->f_code[0] ) << motion->f_code[1]));
409 motion_x = bound_motion_vector( motion_x,
410 motion->f_code[0] + motion->f_code[1]);
411 motion->pmv[0][0] = motion_x;
413 NEEDBITS (bit_buf, bits, bit_ptr);
414 motion_y = (motion->pmv[0][1] +
415 (get_motion_delta( decoder,
416 motion->f_code[0]) << motion->f_code[1]));
417 motion_y = bound_motion_vector( motion_y,
418 motion->f_code[0] + motion->f_code[1]);
419 motion->pmv[0][1] = motion_y;
420 MOTION_420 (table, motion->ref[0], motion_x, motion_y, 16, 0);
423 #define MOTION_FUNCTIONS(FORMAT,MOTION,MOTION_FIELD,MOTION_DMV,MOTION_ZERO) \
425 static void motion_fr_frame_##FORMAT (mpeg2_decoder_t * const decoder, \
426 motion_t * const motion, \
427 mpeg2_mc_fct * const * const table) \
429 int motion_x, motion_y; \
430 unsigned int pos_x, pos_y, xy_half, offset; \
432 NEEDBITS (bit_buf, bits, bit_ptr); \
433 motion_x = motion->pmv[0][0] + get_motion_delta (decoder, \
434 motion->f_code[0]); \
435 motion_x = bound_motion_vector (motion_x, motion->f_code[0]); \
436 motion->pmv[1][0] = motion->pmv[0][0] = motion_x; \
438 NEEDBITS (bit_buf, bits, bit_ptr); \
439 motion_y = motion->pmv[0][1] + get_motion_delta (decoder, \
440 motion->f_code[1]); \
441 motion_y = bound_motion_vector (motion_y, motion->f_code[1]); \
442 motion->pmv[1][1] = motion->pmv[0][1] = motion_y; \
444 MOTION (table, motion->ref[0], motion_x, motion_y, 16, 0); \
447 static void motion_fr_field_##FORMAT (mpeg2_decoder_t * const decoder, \
448 motion_t * const motion, \
449 mpeg2_mc_fct * const * const table) \
451 int motion_x, motion_y, field; \
452 unsigned int pos_x, pos_y, xy_half, offset; \
454 NEEDBITS (bit_buf, bits, bit_ptr); \
455 field = UBITS (bit_buf, 1); \
456 DUMPBITS (bit_buf, bits, 1); \
458 motion_x = motion->pmv[0][0] + get_motion_delta (decoder, \
459 motion->f_code[0]); \
460 motion_x = bound_motion_vector (motion_x, motion->f_code[0]); \
461 motion->pmv[0][0] = motion_x; \
463 NEEDBITS (bit_buf, bits, bit_ptr); \
464 motion_y = ((motion->pmv[0][1] >> 1) + \
465 get_motion_delta (decoder, motion->f_code[1])); \
466 /* motion_y = bound_motion_vector (motion_y, motion->f_code[1]); */ \
467 motion->pmv[0][1] = motion_y << 1; \
469 MOTION_FIELD (table, motion->ref[0], motion_x, motion_y, 0, & ~1, field); \
471 NEEDBITS (bit_buf, bits, bit_ptr); \
472 field = UBITS (bit_buf, 1); \
473 DUMPBITS (bit_buf, bits, 1); \
475 motion_x = motion->pmv[1][0] + get_motion_delta (decoder, \
476 motion->f_code[0]); \
477 motion_x = bound_motion_vector (motion_x, motion->f_code[0]); \
478 motion->pmv[1][0] = motion_x; \
480 NEEDBITS (bit_buf, bits, bit_ptr); \
481 motion_y = ((motion->pmv[1][1] >> 1) + \
482 get_motion_delta (decoder, motion->f_code[1])); \
483 /* motion_y = bound_motion_vector (motion_y, motion->f_code[1]); */ \
484 motion->pmv[1][1] = motion_y << 1; \
486 MOTION_FIELD (table, motion->ref[0], motion_x, motion_y, 1, & ~1, field); \
489 static void motion_fr_dmv_##FORMAT (mpeg2_decoder_t * const decoder, \
490 motion_t * const motion, \
491 mpeg2_mc_fct * const * const table) \
493 int motion_x, motion_y, dmv_x, dmv_y, m, other_x, other_y; \
494 unsigned int pos_x, pos_y, xy_half, offset; \
496 NEEDBITS (bit_buf, bits, bit_ptr); \
497 motion_x = motion->pmv[0][0] + get_motion_delta (decoder, \
498 motion->f_code[0]); \
499 motion_x = bound_motion_vector (motion_x, motion->f_code[0]); \
500 motion->pmv[1][0] = motion->pmv[0][0] = motion_x; \
501 NEEDBITS (bit_buf, bits, bit_ptr); \
502 dmv_x = get_dmv (decoder); \
504 motion_y = ((motion->pmv[0][1] >> 1) + \
505 get_motion_delta (decoder, motion->f_code[1])); \
506 /* motion_y = bound_motion_vector (motion_y, motion->f_code[1]); */ \
507 motion->pmv[1][1] = motion->pmv[0][1] = motion_y << 1; \
508 dmv_y = get_dmv (decoder); \
510 m = decoder->top_field_first ? 1 : 3; \
511 other_x = ((motion_x * m + (motion_x > 0)) >> 1) + dmv_x; \
512 other_y = ((motion_y * m + (motion_y > 0)) >> 1) + dmv_y - 1; \
513 MOTION_FIELD (mpeg2_mc.put, motion->ref[0], other_x, other_y, 0, | 1, 0); \
515 m = decoder->top_field_first ? 3 : 1; \
516 other_x = ((motion_x * m + (motion_x > 0)) >> 1) + dmv_x; \
517 other_y = ((motion_y * m + (motion_y > 0)) >> 1) + dmv_y + 1; \
518 MOTION_FIELD (mpeg2_mc.put, motion->ref[0], other_x, other_y, 1, & ~1, 0);\
520 MOTION_DMV (mpeg2_mc.avg, motion->ref[0], motion_x, motion_y); \
523 static void motion_reuse_##FORMAT (mpeg2_decoder_t * const decoder, \
524 motion_t * const motion, \
525 mpeg2_mc_fct * const * const table) \
527 int motion_x, motion_y; \
528 unsigned int pos_x, pos_y, xy_half, offset; \
530 motion_x = motion->pmv[0][0]; \
531 motion_y = motion->pmv[0][1]; \
533 MOTION (table, motion->ref[0], motion_x, motion_y, 16, 0); \
536 static void motion_zero_##FORMAT (mpeg2_decoder_t * const decoder, \
537 motion_t * const motion, \
538 mpeg2_mc_fct * const * const table) \
540 unsigned int offset; \
542 motion->pmv[0][0] = motion->pmv[0][1] = 0; \
543 motion->pmv[1][0] = motion->pmv[1][1] = 0; \
545 MOTION_ZERO (table, motion->ref[0]); \
548 static void motion_fi_field_##FORMAT (mpeg2_decoder_t * const decoder, \
549 motion_t * const motion, \
550 mpeg2_mc_fct * const * const table) \
552 int motion_x, motion_y; \
553 uint8_t ** ref_field; \
554 unsigned int pos_x, pos_y, xy_half, offset; \
556 NEEDBITS (bit_buf, bits, bit_ptr); \
557 ref_field = motion->ref2[UBITS (bit_buf, 1)]; \
558 DUMPBITS (bit_buf, bits, 1); \
560 motion_x = motion->pmv[0][0] + get_motion_delta (decoder, \
561 motion->f_code[0]); \
562 motion_x = bound_motion_vector (motion_x, motion->f_code[0]); \
563 motion->pmv[1][0] = motion->pmv[0][0] = motion_x; \
565 NEEDBITS (bit_buf, bits, bit_ptr); \
566 motion_y = motion->pmv[0][1] + get_motion_delta (decoder, \
567 motion->f_code[1]); \
568 motion_y = bound_motion_vector (motion_y, motion->f_code[1]); \
569 motion->pmv[1][1] = motion->pmv[0][1] = motion_y; \
571 MOTION (table, ref_field, motion_x, motion_y, 16, 0); \
574 static void motion_fi_16x8_##FORMAT (mpeg2_decoder_t * const decoder, \
575 motion_t * const motion, \
576 mpeg2_mc_fct * const * const table) \
578 int motion_x, motion_y; \
579 uint8_t ** ref_field; \
580 unsigned int pos_x, pos_y, xy_half, offset; \
582 NEEDBITS (bit_buf, bits, bit_ptr); \
583 ref_field = motion->ref2[UBITS (bit_buf, 1)]; \
584 DUMPBITS (bit_buf, bits, 1); \
586 motion_x = motion->pmv[0][0] + get_motion_delta (decoder, \
587 motion->f_code[0]); \
588 motion_x = bound_motion_vector (motion_x, motion->f_code[0]); \
589 motion->pmv[0][0] = motion_x; \
591 NEEDBITS (bit_buf, bits, bit_ptr); \
592 motion_y = motion->pmv[0][1] + get_motion_delta (decoder, \
593 motion->f_code[1]); \
594 motion_y = bound_motion_vector (motion_y, motion->f_code[1]); \
595 motion->pmv[0][1] = motion_y; \
597 MOTION (table, ref_field, motion_x, motion_y, 8, 0); \
599 NEEDBITS (bit_buf, bits, bit_ptr); \
600 ref_field = motion->ref2[UBITS (bit_buf, 1)]; \
601 DUMPBITS (bit_buf, bits, 1); \
603 motion_x = motion->pmv[1][0] + get_motion_delta (decoder, \
604 motion->f_code[0]); \
605 motion_x = bound_motion_vector (motion_x, motion->f_code[0]); \
606 motion->pmv[1][0] = motion_x; \
608 NEEDBITS (bit_buf, bits, bit_ptr); \
609 motion_y = motion->pmv[1][1] + get_motion_delta (decoder, \
610 motion->f_code[1]); \
611 motion_y = bound_motion_vector (motion_y, motion->f_code[1]); \
612 motion->pmv[1][1] = motion_y; \
614 MOTION (table, ref_field, motion_x, motion_y, 8, 8); \
617 static void motion_fi_dmv_##FORMAT (mpeg2_decoder_t * const decoder, \
618 motion_t * const motion, \
619 mpeg2_mc_fct * const * const table) \
621 int motion_x, motion_y, other_x, other_y; \
622 unsigned int pos_x, pos_y, xy_half, offset; \
624 NEEDBITS (bit_buf, bits, bit_ptr); \
625 motion_x = motion->pmv[0][0] + get_motion_delta (decoder, \
626 motion->f_code[0]); \
627 motion_x = bound_motion_vector (motion_x, motion->f_code[0]); \
628 motion->pmv[1][0] = motion->pmv[0][0] = motion_x; \
629 NEEDBITS (bit_buf, bits, bit_ptr); \
630 other_x = ((motion_x + (motion_x > 0)) >> 1) + get_dmv (decoder); \
632 motion_y = motion->pmv[0][1] + get_motion_delta (decoder, \
633 motion->f_code[1]); \
634 motion_y = bound_motion_vector (motion_y, motion->f_code[1]); \
635 motion->pmv[1][1] = motion->pmv[0][1] = motion_y; \
636 other_y = (((motion_y + (motion_y > 0)) >> 1) + get_dmv (decoder) + \
637 decoder->dmv_offset); \
639 MOTION (mpeg2_mc.put, motion->ref[0], motion_x, motion_y, 16, 0); \
640 MOTION (mpeg2_mc.avg, motion->ref[1], other_x, other_y, 16, 0); \
643 MOTION_FUNCTIONS (420, MOTION_420, MOTION_FIELD_420, MOTION_DMV_420,
645 MOTION_FUNCTIONS (422, MOTION_422, MOTION_FIELD_422, MOTION_DMV_422,
647 MOTION_FUNCTIONS (444, MOTION_444, MOTION_FIELD_444, MOTION_DMV_444,
650 /* like motion_frame, but parsing without actual motion compensation */
651 static void motion_fr_conceal (mpeg2_decoder_t * const decoder)
655 NEEDBITS (bit_buf, bits, bit_ptr);
656 tmp = (decoder->f_motion.pmv[0][0] +
657 get_motion_delta (decoder, decoder->f_motion.f_code[0]));
658 tmp = bound_motion_vector (tmp, decoder->f_motion.f_code[0]);
659 decoder->f_motion.pmv[1][0] = decoder->f_motion.pmv[0][0] = tmp;
661 NEEDBITS (bit_buf, bits, bit_ptr);
662 tmp = (decoder->f_motion.pmv[0][1] +
663 get_motion_delta (decoder, decoder->f_motion.f_code[1]));
664 tmp = bound_motion_vector (tmp, decoder->f_motion.f_code[1]);
665 decoder->f_motion.pmv[1][1] = decoder->f_motion.pmv[0][1] = tmp;
667 DUMPBITS (bit_buf, bits, 1); /* remove marker_bit */
670 static void motion_fi_conceal (mpeg2_decoder_t * const decoder)
674 NEEDBITS (bit_buf, bits, bit_ptr);
675 DUMPBITS (bit_buf, bits, 1); /* remove field_select */
677 tmp = (decoder->f_motion.pmv[0][0] +
678 get_motion_delta (decoder, decoder->f_motion.f_code[0]));
679 tmp = bound_motion_vector (tmp, decoder->f_motion.f_code[0]);
680 decoder->f_motion.pmv[1][0] = decoder->f_motion.pmv[0][0] = tmp;
682 NEEDBITS (bit_buf, bits, bit_ptr);
683 tmp = (decoder->f_motion.pmv[0][1] +
684 get_motion_delta (decoder, decoder->f_motion.f_code[1]));
685 tmp = bound_motion_vector (tmp, decoder->f_motion.f_code[1]);
686 decoder->f_motion.pmv[1][1] = decoder->f_motion.pmv[0][1] = tmp;
688 DUMPBITS (bit_buf, bits, 1); /* remove marker_bit */
695 #define MOTION_CALL(routine,direction) \
697 if ((direction) & MACROBLOCK_MOTION_FORWARD) \
698 routine (decoder, &(decoder->f_motion), mpeg2_mc.put); \
699 if ((direction) & MACROBLOCK_MOTION_BACKWARD) \
700 routine (decoder, &(decoder->b_motion), \
701 ((direction) & MACROBLOCK_MOTION_FORWARD ? \
702 mpeg2_mc.avg : mpeg2_mc.put)); \
705 #define NEXT_MACROBLOCK \
707 decoder->offset += 16; \
708 if (decoder->offset == decoder->width) { \
709 do { /* just so we can use the break statement */ \
710 if (decoder->convert) { \
711 decoder->convert (decoder->convert_id, decoder->dest, \
712 decoder->v_offset); \
713 if (decoder->coding_type == B_TYPE) \
716 decoder->dest[0] += decoder->slice_stride; \
717 decoder->dest[1] += decoder->slice_uv_stride; \
718 decoder->dest[2] += decoder->slice_uv_stride; \
720 decoder->v_offset += 16; \
721 if (decoder->v_offset > decoder->limit_y) { \
722 if (mpeg2_cpu_state_restore) \
723 mpeg2_cpu_state_restore (&cpu_state); \
726 decoder->offset = 0; \
730 void mpeg2_init_fbuf (mpeg2_decoder_t * decoder, uint8_t * current_fbuf[3],
731 uint8_t * forward_fbuf[3], uint8_t * backward_fbuf[3])
733 int offset, stride, height, bottom_field;
735 stride = decoder->stride_frame;
736 bottom_field = (decoder->picture_structure == BOTTOM_FIELD);
737 offset = bottom_field ? stride : 0;
738 height = decoder->height;
740 decoder->picture_dest[0] = current_fbuf[0] + offset;
741 decoder->picture_dest[1] = current_fbuf[1] + (offset >> 1);
742 decoder->picture_dest[2] = current_fbuf[2] + (offset >> 1);
744 decoder->f_motion.ref[0][0] = forward_fbuf[0] + offset;
745 decoder->f_motion.ref[0][1] = forward_fbuf[1] + (offset >> 1);
746 decoder->f_motion.ref[0][2] = forward_fbuf[2] + (offset >> 1);
748 decoder->b_motion.ref[0][0] = backward_fbuf[0] + offset;
749 decoder->b_motion.ref[0][1] = backward_fbuf[1] + (offset >> 1);
750 decoder->b_motion.ref[0][2] = backward_fbuf[2] + (offset >> 1);
752 if( decoder->picture_structure != FRAME_PICTURE )
754 decoder->dmv_offset = bottom_field ? 1 : -1;
755 decoder->f_motion.ref2[0] = decoder->f_motion.ref[bottom_field];
756 decoder->f_motion.ref2[1] = decoder->f_motion.ref[!bottom_field];
757 decoder->b_motion.ref2[0] = decoder->b_motion.ref[bottom_field];
758 decoder->b_motion.ref2[1] = decoder->b_motion.ref[!bottom_field];
759 offset = stride - offset;
761 if( decoder->second_field && (decoder->coding_type != B_TYPE) )
762 forward_fbuf = current_fbuf;
764 decoder->f_motion.ref[1][0] = forward_fbuf[0] + offset;
765 decoder->f_motion.ref[1][1] = forward_fbuf[1] + (offset >> 1);
766 decoder->f_motion.ref[1][2] = forward_fbuf[2] + (offset >> 1);
768 decoder->b_motion.ref[1][0] = backward_fbuf[0] + offset;
769 decoder->b_motion.ref[1][1] = backward_fbuf[1] + (offset >> 1);
770 decoder->b_motion.ref[1][2] = backward_fbuf[2] + (offset >> 1);
776 decoder->stride = stride;
777 decoder->uv_stride = stride >> 1;
778 decoder->slice_stride = 16 * stride;
779 decoder->slice_uv_stride =
780 decoder->slice_stride >> (2 - decoder->chroma_format);
781 decoder->limit_x = 2 * decoder->width - 32;
782 decoder->limit_y_16 = 2 * height - 32;
783 decoder->limit_y_8 = 2 * height - 16;
784 decoder->limit_y = height - 16;
788 decoder->motion_parser[0] = motion_zero_420;
789 decoder->motion_parser[MC_FRAME] = motion_mp1;
790 decoder->motion_parser[4] = motion_reuse_420;
792 else if( decoder->picture_structure == FRAME_PICTURE )
794 if (decoder->chroma_format == 0)
796 decoder->motion_parser[0] = motion_zero_420;
797 decoder->motion_parser[MC_FIELD] = motion_fr_field_420;
798 decoder->motion_parser[MC_FRAME] = motion_fr_frame_420;
799 decoder->motion_parser[MC_DMV] = motion_fr_dmv_420;
800 decoder->motion_parser[4] = motion_reuse_420;
802 else if (decoder->chroma_format == 1)
804 decoder->motion_parser[0] = motion_zero_422;
805 decoder->motion_parser[MC_FIELD] = motion_fr_field_422;
806 decoder->motion_parser[MC_FRAME] = motion_fr_frame_422;
807 decoder->motion_parser[MC_DMV] = motion_fr_dmv_422;
808 decoder->motion_parser[4] = motion_reuse_422;
811 decoder->motion_parser[0] = motion_zero_444;
812 decoder->motion_parser[MC_FIELD] = motion_fr_field_444;
813 decoder->motion_parser[MC_FRAME] = motion_fr_frame_444;
814 decoder->motion_parser[MC_DMV] = motion_fr_dmv_444;
815 decoder->motion_parser[4] = motion_reuse_444;
820 if (decoder->chroma_format == 0)
822 decoder->motion_parser[0] = motion_zero_420;
823 decoder->motion_parser[MC_FIELD] = motion_fi_field_420;
824 decoder->motion_parser[MC_16X8] = motion_fi_16x8_420;
825 decoder->motion_parser[MC_DMV] = motion_fi_dmv_420;
826 decoder->motion_parser[4] = motion_reuse_420;
828 else if (decoder->chroma_format == 1)
830 decoder->motion_parser[0] = motion_zero_422;
831 decoder->motion_parser[MC_FIELD] = motion_fi_field_422;
832 decoder->motion_parser[MC_16X8] = motion_fi_16x8_422;
833 decoder->motion_parser[MC_DMV] = motion_fi_dmv_422;
834 decoder->motion_parser[4] = motion_reuse_422;
838 decoder->motion_parser[0] = motion_zero_444;
839 decoder->motion_parser[MC_FIELD] = motion_fi_field_444;
840 decoder->motion_parser[MC_16X8] = motion_fi_16x8_444;
841 decoder->motion_parser[MC_DMV] = motion_fi_dmv_444;
842 decoder->motion_parser[4] = motion_reuse_444;