summaryrefslogtreecommitdiff
path: root/apps/plugins/mpegplayer/slice.c
blob: 926333d5d07595ffc6fa8f8f36bf8918a13753b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524

 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2007 Nicolas Pennequin, Dan Everton, Matthias Mohr
 *
 * All files in this archive are subject to the GNU General Public License.
 * See the file COPYING in the source tree root for full license agreement.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#ifdef DEBUG

#include <stdio.h>
#include <string.h>
#include "gwps.h"
#ifdef __PCTOOL__
#define DEBUGF printf
#else
#include "debug.h"
#endif

#define PARSE_FAIL_UNCLOSED_COND     1
#define PARSE_FAIL_INVALID_CHAR      2
#define PARSE_FAIL_COND_SYNTAX_ERROR 3

#if defined(SIMULATOR) || defined(__PCTOOL__)
extern bool debug_wps;
extern int wps_verbose_level;
#endif

static char *next_str(bool next) {
    return next ? "next " : "";
}

static void dump_wps_tokens(struct wps_data *data)
{
    struct wps_token *token;
    snprintf(buf, sizeof(buf), "conditional option, next cond: %d",
                         token->value.i);
                break;

            case WPS_TOKEN_CONDITIONAL_END:
                snprintf(buf, sizeof(buf), "conditional end");
                indent--;
                break;

#ifdef HAVE_LCD_BITMAP
            case WPS_TOKEN_IMAGE_PRELOAD:
                snprintf(buf, sizeof(buf), "preload image");
                break;

            case WPS_TOKEN_IMAGE_PRELOAD_DISPLAY:
                snprintf(buf, sizeof(buf), "display preloaded image %d",
                         token->value.i);
                break;

            case WPS_TOKEN_IMAGE_DISPLAY:
                snprintf(buf, sizeof(buf), "display image");
                break;
#endif

#ifdef HAS_BUTTON_HOLD
            case WPS_TOKEN_MAIN_HOLD:
                snprintf(buf, sizeof(buf), "mode hold");
                break;
#endif

#ifdef HAS_REMOTE_BUTTON_HOLD
            case WPS_TOKEN_REMOTE_HOLD:
                snprintf(buf, sizeof(buf), "mode remote hold");
                break;
#endif

            case WPS_TOKEN_REPEAT_MODE:
                snprintf(buf, sizeof(buf), "mode repeat");
                break;

            case WPS_TOKEN_PLAYBACK_STATUS:
                snprintf(buf, sizeof(buf), "mode playback");
                break;

            case WPS_TOKEN_RTC_DAY_OF_MONTH:
                snprintf(buf, sizeof(buf), "rtc: day of month (01..31)");
                break;
            case WPS_TOKEN_RTC_DAY_OF_MONTH_BLANK_PADDED:
                snprintf(buf, sizeof(buf),
                         "rtc: day of month, blank padded ( 1..31)");
                break;
            case WPS_TOKEN_RTC_HOUR_24_ZERO_PADDED:
                snprintf(buf, sizeof(buf), "rtc: hour (00..23)");
                break;
            case WPS_TOKEN_RTC_HOUR_24:
                snprintf(buf, sizeof(buf), "rtc: hour ( 0..23)");
                break;
            case WPS_TOKEN_RTC_HOUR_12_ZERO_PADDED:
                snprintf(buf, sizeof(buf), "rtc: hour (01..12)");
                break;
            case WPS_TOKEN_RTC_HOUR_12:
                snprintf(buf, sizeof(buf), "rtc: hour ( 1..12)");
                break;
            case WPS_TOKEN_RTC_MONTH:
                snprintf(buf, sizeof(buf), "rtc: month (01..12)");
                break;
            case WPS_TOKEN_RTC_MINUTE:
                snprintf(buf, sizeof(buf), "rtc: minute (00..59)");
                break;
            case WPS_TOKEN_RTC_SECOND:
                snprintf(buf, sizeof(buf), "rtc: second (00..59)");
                break;
            case WPS_TOKEN_RTC_YEAR_2_DIGITS:
                snprintf(buf, sizeof(buf),
                         "rtc: last two digits of year (00..99)");
                break;
            case WPS_TOKEN_RTC_YEAR_4_DIGITS:
                snprintf(buf, sizeof(buf), "rtc: year (1970...)");
                break;
            case WPS_TOKEN_RTC_AM_PM_UPPER:
                snprintf(buf, sizeof(buf),
                         "rtc: upper case AM or PM indicator");
                break;
            case WPS_TOKEN_RTC_AM_PM_LOWER:
                snprintf(buf, sizeof(buf),
                         "rtc: lower case am or pm indicator");
                break;
            case WPS_TOKEN_RTC_WEEKDAY_NAME:
                snprintf(buf, sizeof(buf),
                         "rtc: abbreviated weekday name (Sun..Sat)");
                break;
            case WPS_TOKEN_RTC_MONTH_NAME:
                snprintf(buf, sizeof(buf),
                         "rtc: abbreviated month name (Jan..Dec)");
                break;
            case WPS_TOKEN_RTC_DAY_OF_WEEK_START_MON:
                snprintf(buf, sizeof(buf),
                         "rtc: day of week (1..7); 1 is Monday");
                break;
            case WPS_TOKEN_RTC_DAY_OF_WEEK_START_SUN:
                snprintf(buf, sizeof(buf),
                         "rtc: day of week (0..6); 0 is Sunday");
                break;

#if (CONFIG_CODEC == SWCODEC)
            case WPS_TOKEN_CROSSFADE:
                snprintf(buf, sizeof(buf), "crossfade");
                break;

            case WPS_TOKEN_REPLAYGAIN:
                snprintf(buf, sizeof(buf), "replaygain");
                break;
#endif

#ifdef HAVE_LCD_BITMAP
            case WPS_TOKEN_IMAGE_BACKDROP:
                snprintf(buf, sizeof(buf), "backdrop image");
                break;

            case WPS_TOKEN_IMAGE_PROGRESS_BAR:
                snprintf(buf, sizeof(buf), "progressbar bitmap");
                break;

            case WPS_TOKEN_PEAKMETER:
                snprintf(buf, sizeof(buf), "peakmeter");
                break;
#endif

            case WPS_TOKEN_PROGRESSBAR:
                snprintf(buf, sizeof(buf), "progressbar");
                break;

#ifdef HAVE_LCD_CHARCELLS
            case WPS_TOKEN_PLAYER_PROGRESSBAR:
                snprintf(buf, sizeof(buf), "full line progressbar");
                break;
#endif

            case WPS_TOKEN_TRACK_TIME_ELAPSED:
                snprintf(buf, sizeof(buf), "time elapsed in track");
                break;

            case WPS_TOKEN_TRACK_ELAPSED_PERCENT:
                snprintf(buf, sizeof(buf), "played percentage of track");
                break;

            case WPS_TOKEN_PLAYLIST_ENTRIES:
                snprintf(buf, sizeof(buf), "number of entries in playlist");
                break;

            case WPS_TOKEN_PLAYLIST_NAME:
                snprintf(buf, sizeof(buf), "playlist name");
                break;

            case WPS_TOKEN_PLAYLIST_POSITION:
                snprintf(buf, sizeof(buf), "position in playlist");
                break;

            case WPS_TOKEN_TRACK_TIME_REMAINING:
                snprintf(buf, sizeof(buf), "time remaining in track");
                break;

            case WPS_TOKEN_PLAYLIST_SHUFFLE:
                snprintf(buf, sizeof(buf), "playlist shuffle mode");
                break;

            case WPS_TOKEN_TRACK_LENGTH:
                snprintf(buf, sizeof(buf), "track length");
                break;

            case WPS_TOKEN_VOLUME:
                snprintf(buf, sizeof(buf), "volume");
                break;

            case WPS_TOKEN_METADATA_ARTIST:
                snprintf(buf, sizeof(buf), "%strack artist",
                         next_str(next));
                break;

            case WPS_TOKEN_METADATA_COMPOSER:
                snprintf(buf, sizeof(buf), "%strack composer",
                         next_str(next));
                break;

            case WPS_TOKEN_METADATA_ALBUM:
                snprintf(buf, sizeof(buf), "%strack album",
                         next_str(next));
                break;

            case WPS_TOKEN_METADATA_GENRE:
                snprintf(buf, sizeof(buf), "%strack genre",
                         next_str(next));
                break;

            case WPS_TOKEN_METADATA_TRACK_NUMBER:
                snprintf(buf, sizeof(buf), "%strack number",
                         next_str(next));
                break;

            case WPS_TOKEN_METADATA_TRACK_TITLE:
                snprintf(buf, sizeof(buf), "%strack title",
                         next_str(next));
                break;

            case WPS_TOKEN_METADATA_VERSION:
                snprintf(buf, sizeof(buf), "%strack ID3 version",
                         next_str(next));
                break;

            case WPS_TOKEN_METADATA_YEAR:
                snprintf(buf, sizeof(buf), "%strack year", next_str(next));
                break;

            case WPS_TOKEN_BATTERY_PERCENT:
                snprintf(buf, sizeof(buf), "battery percentage");
                break;

            case WPS_TOKEN_BATTERY_VOLTS:
                snprintf(buf, sizeof(buf), "battery voltage");
                break;

            case WPS_TOKEN_BATTERY_TIME:
                snprintf(buf, sizeof(buf), "battery time left");
                break;

            case WPS_TOKEN_BATTERY_CHARGER_CONNECTED:
                snprintf(buf, sizeof(buf), "battery charger connected");
                break;

            case WPS_TOKEN_BATTERY_CHARGING:
                snprintf(buf, sizeof(buf), "battery charging");
                break;

            case WPS_TOKEN_FILE_BITRATE:
                snprintf(buf, sizeof(buf), "%sfile bitrate", next_str(next));
                break;

            case WPS_TOKEN_FILE_CODEC:
                snprintf(buf, sizeof(buf), "%sfile codec", next_str(next));
                break;

            case WPS_TOKEN_FILE_FREQUENCY:
                snprintf(buf, sizeof(buf), "%sfile audio frequency in Hz",
                         next_str(next));
                break;

            case WPS_TOKEN_FILE_FREQUENCY_KHZ:
                snprintf(buf, sizeof(buf), "%sfile audio frequency in KHz",
                         next_str(next));
                break;

            case WPS_TOKEN_FILE_NAME:
                snprintf(buf, sizeof(buf), "%sfile name", next_str(next));
                break;

            case WPS_TOKEN_FILE_NAME_WITH_EXTENSION:
                snprintf(buf, sizeof(buf), "%sfile name with extension",
                         next_str(next));
                break;

            case WPS_TOKEN_FILE_PATH:
                snprintf(buf, sizeof(buf), "%sfile path", next_str(next));
                break;

            case WPS_TOKEN_FILE_SIZE:
                snprintf(buf, sizeof(buf), "%sfile size", next_str(next));
                break;

            case WPS_TOKEN_FILE_VBR:
                snprintf(buf, sizeof(buf), "%sfile is vbr", next_str(next));
                break;

            case WPS_TOKEN_FILE_DIRECTORY:
                snprintf(buf, sizeof(buf), "%sfile directory, level: %d",
                         next_str(next), token->value.i);
                break;

            default:
                snprintf(buf, sizeof(buf), "FIXME (code: %d)",
                         token->type);
                break;
        }

        if (wps_verbose_level > 2)
        {
            for(j = 0; j < indent; j++) {
                DEBUGF("\t");
            }

            DEBUGF("[%3d] = (%2d) %s\n", i, token->type, buf);
        }
    }

    if (wps_verbose_level > 0)
    {
        DEBUGF("\n");
        DEBUGF("Number of string tokens: %d\n", num_string_tokens);
        DEBUGF("\n");
    }
}

static void print_line_info(struct wps_data *data)
{
    int i, j;
    struct wps_line *line;
    struct wps_subline *subline;

    if (wps_verbose_level > 0)
    {
        DEBUGF("Number of lines   : %d\n", data->num_lines);
        DEBUGF("Number of sublines: %d\n", data->num_sublines);
        DEBUGF("Number of tokens  : %d\n", data->num_tokens);
        DEBUGF("\n");
    }

    if (wps_verbose_level > 1)
    {
        for (i = 0, line = data->lines; i < data->num_lines; i++,line++)
        {
            DEBUGF("Line %2d (num_sublines=%d, first_subline=%d)\n",
                   i, line->num_sublines, line->first_subline_idx);

            for (j = 0, subline = data->sublines + line->first_subline_idx;
                 j < line->num_sublines; j++, subline++)
            {
                DEBUGF("    Subline %d: first_token=%3d, last_token=%3d",
                       j, subline->first_token_idx,
                       wps_last_token_index(data, i, j));

                if (subline->line_type & WPS_REFRESH_SCROLL)
                    DEBUGF(", scrolled");
                else if (subline->line_type & WPS_REFRESH_PLAYER_PROGRESS)
                    DEBUGF(", progressbar");
                else if (subline->line_type & WPS_REFRESH_PEAK_METER)
                    DEBUGF(", peakmeter");

                DEBUGF("\n");
            }
        }

        DEBUGF("\n");
    }
}

static void print_wps_strings(struct wps_data *data)
{
    int i, len, total_len = 0, buf_used = 0;

    if (wps_verbose_level > 1) DEBUGF("Strings:\n");
    for (i = 0; i < data->num_strings; i++)
    {
        len = strlen(data->strings[i]);
        total_len += len;
        buf_used += len + 1;
        if (wps_verbose_level > 1)
            DEBUGF("%2d: (%2d) '%s'\n", i, len, data->strings[i]);
    }
    if (wps_verbose_level > 1) DEBUGF("\n");

    if (wps_verbose_level > 0)
    {
        DEBUGF("Number of unique strings: %d (max: %d)\n",
               data->num_strings, WPS_MAX_STRINGS);
        DEBUGF("Total string length: %d\n", total_len);
        DEBUGF("String buffer used: %d out of %d bytes\n",
               buf_used, STRING_BUFFER_SIZE);
        DEBUGF("\n");
    }
}

#ifdef HAVE_LCD_BITMAP
static void print_img_cond_indexes(struct wps_data *data)
{
    DEBUGF("Image conditional indexes:\n");
    int i;
    for (i = 0; i < MAX_IMAGES; i++)
    {
        if (data->img[i].cond_index)
            DEBUGF("%2d: %d\n", i, data->img[i].cond_index);
    }
    DEBUGF("\n");
}
#endif /*HAVE_LCD_BITMAP */

void print_debug_info(struct wps_data *data, int fail, int line)
{
#if defined(SIMULATOR) || defined(__PCTOOL__)
    if (debug_wps && wps_verbose_level)
    {
        dump_wps_tokens(data);
        print_wps_strings(data);
        print_line_info(data);
#ifdef HAVE_LCD_BITMAP
        if (wps_verbose_level > 2) print_img_cond_indexes(data);
#endif
    }
#endif /* SIMULATOR */

    if (data->num_tokens >= WPS_MAX_TOKENS - 1) {
        DEBUGF("Warning: Max number of tokens was reached (%d)\n",
               WPS_MAX_TOKENS - 1);
    }

    if (fail)
    {
        DEBUGF("Failed parsing on line %d : ", line);
        switch (fail)
        {
            case PARSE_FAIL_UNCLOSED_COND:
                DEBUGF("Unclosed conditional");
                break;

            case PARSE_FAIL_INVALID_CHAR:
                DEBUGF("Invalid conditional char (not in an open conditional)");
                break;

            case PARSE_FAIL_COND_SYNTAX_ERROR:
                DEBUGF("Conditional syntax error");
                break;
        }
        DEBUGF("\n");
    }
}

#endif /* DEBUG */
href='#n2271'>2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898
/*
 * slice.c
 * Copyright (C) 2000-2003 Michel Lespinasse <walken@zoy.org>
 * Copyright (C) 2003      Peter Gubanov <peter@elecard.net.ru>
 * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
 *
 * This file is part of mpeg2dec, a free MPEG-2 video stream decoder.
 * See http://libmpeg2.sourceforge.net/ for updates.
 *
 * mpeg2dec is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * mpeg2dec is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * $Id$
 * libmpeg2 sync history:
 * 2008-07-01 - CVS revision 1.55
 */

#include "plugin.h"

#include "mpeg2dec_config.h"

#include "mpeg2.h"
#include "attributes.h"
#include "mpeg2_internal.h"

#include "vlc.h"

static inline int get_macroblock_modes (mpeg2_decoder_t * const decoder)
{
#define bit_buf (decoder->bitstream_buf)
#define bits (decoder->bitstream_bits)
#define bit_ptr (decoder->bitstream_ptr)

    int macroblock_modes;
    const MBtab * tab;

    switch (decoder->coding_type)
    {
    case I_TYPE:
        tab = MB_I + UBITS (bit_buf, 1);
        DUMPBITS (bit_buf, bits, tab->len);
        macroblock_modes = tab->modes;

        if (!(decoder->frame_pred_frame_dct) &&
            decoder->picture_structure == FRAME_PICTURE)
        {
            macroblock_modes |= UBITS (bit_buf, 1) * DCT_TYPE_INTERLACED;
            DUMPBITS (bit_buf, bits, 1);
        }

        return macroblock_modes;

    case P_TYPE:
        tab = MB_P + UBITS (bit_buf, 5);
        DUMPBITS (bit_buf, bits, tab->len);
        macroblock_modes = tab->modes;

        if (decoder->picture_structure != FRAME_PICTURE)
        {
            if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
            {
                macroblock_modes |= UBITS (bit_buf, 2) << MOTION_TYPE_SHIFT;
                DUMPBITS (bit_buf, bits, 2);
            }

            return macroblock_modes | MACROBLOCK_MOTION_FORWARD;
        }
        else if (decoder->frame_pred_frame_dct)
        {
            if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
                macroblock_modes |= MC_FRAME << MOTION_TYPE_SHIFT;

            return macroblock_modes | MACROBLOCK_MOTION_FORWARD;
        }
        else
        {
            if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
            {
                macroblock_modes |= UBITS (bit_buf, 2) << MOTION_TYPE_SHIFT;
                DUMPBITS (bit_buf, bits, 2);
            }

            if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN))
            {
                macroblock_modes |= UBITS (bit_buf, 1) * DCT_TYPE_INTERLACED;
                DUMPBITS (bit_buf, bits, 1);
            }

            return macroblock_modes | MACROBLOCK_MOTION_FORWARD;
        }

    case B_TYPE:
        tab = MB_B + UBITS (bit_buf, 6);
        DUMPBITS (bit_buf, bits, tab->len);
        macroblock_modes = tab->modes;

        if (decoder->picture_structure != FRAME_PICTURE)
        {
            if (! (macroblock_modes & MACROBLOCK_INTRA))
            {
                macroblock_modes |= UBITS (bit_buf, 2) << MOTION_TYPE_SHIFT;
                DUMPBITS (bit_buf, bits, 2);
            }

            return macroblock_modes;
        }
        else if (decoder->frame_pred_frame_dct)
        {
            /* if (! (macroblock_modes & MACROBLOCK_INTRA)) */
            macroblock_modes |= MC_FRAME << MOTION_TYPE_SHIFT;
            return macroblock_modes;
        }
        else
        {
            if (macroblock_modes & MACROBLOCK_INTRA)
                goto intra;

            macroblock_modes |= UBITS (bit_buf, 2) << MOTION_TYPE_SHIFT;
            DUMPBITS (bit_buf, bits, 2);

            if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN))
            {
            intra:
                macroblock_modes |= UBITS (bit_buf, 1) * DCT_TYPE_INTERLACED;
                DUMPBITS (bit_buf, bits, 1);
            }
            return macroblock_modes;
        }

    case D_TYPE:
        DUMPBITS (bit_buf, bits, 1);
        return MACROBLOCK_INTRA;

    default:
        return 0;
    }
#undef bit_buf
#undef bits
#undef bit_ptr
}

static inline void get_quantizer_scale (mpeg2_decoder_t * const decoder)
{
#define bit_buf (decoder->bitstream_buf)
#define bits (decoder->bitstream_bits)
#define bit_ptr (decoder->bitstream_ptr)

    int quantizer_scale_code;

    quantizer_scale_code = UBITS (bit_buf, 5);
    DUMPBITS (bit_buf, bits, 5);

    decoder->quantizer_matrix[0] =
        decoder->quantizer_prescale[0][quantizer_scale_code];

    decoder->quantizer_matrix[1] =
        decoder->quantizer_prescale[1][quantizer_scale_code];

    decoder->quantizer_matrix[2] =
        decoder->chroma_quantizer[0][quantizer_scale_code];

    decoder->quantizer_matrix[3] =
        decoder->chroma_quantizer[1][quantizer_scale_code];
#undef bit_buf
#undef bits
#undef bit_ptr
}

static inline int get_motion_delta (mpeg2_decoder_t * const decoder,
                                    const int f_code)
{
#define bit_buf (decoder->bitstream_buf)
#define bits (decoder->bitstream_bits)
#define bit_ptr (decoder->bitstream_ptr)

    int delta;
    int sign;
    const MVtab * tab;

    if (bit_buf & 0x80000000)
    {
        DUMPBITS (bit_buf, bits, 1);
        return 0;
    }
    else if (bit_buf >= 0x0c000000)
    {
        tab = MV_4 + UBITS (bit_buf, 4);
        delta = (tab->delta << f_code) + 1;
        bits += tab->len + f_code + 1;
        bit_buf <<= tab->len;

        sign = SBITS (bit_buf, 1);
        bit_buf <<= 1;

        if (f_code)
            delta += UBITS (bit_buf, f_code);
        bit_buf <<= f_code;

        return (delta ^ sign) - sign;
    }
    else
    {
        tab = MV_10 + UBITS (bit_buf, 10);
        delta = (tab->delta << f_code) + 1;
        bits += tab->len + 1;
        bit_buf <<= tab->len;

        sign = SBITS (bit_buf, 1);
        bit_buf <<= 1;

        if (f_code)
        {
            NEEDBITS (bit_buf, bits, bit_ptr);
            delta += UBITS (bit_buf, f_code);
            DUMPBITS (bit_buf, bits, f_code);
        }

        return (delta ^ sign) - sign;

    }
#undef bit_buf
#undef bits
#undef bit_ptr
}

static inline int bound_motion_vector (const int vector, const int f_code)
{
    return ((int32_t)vector << (27 - f_code)) >> (27 - f_code);
}

static inline int get_dmv (mpeg2_decoder_t * const decoder)
{
#define bit_buf (decoder->bitstream_buf)
#define bits (decoder->bitstream_bits)
#define bit_ptr (decoder->bitstream_ptr)

    const DMVtab * tab;

    tab = DMV_2 + UBITS (bit_buf, 2);
    DUMPBITS (bit_buf, bits, tab->len);
    return tab->dmv;

#undef bit_buf
#undef bits
#undef bit_ptr
}

static inline int get_coded_block_pattern (mpeg2_decoder_t * const decoder)
{
#define bit_buf (decoder->bitstream_buf)
#define bits (decoder->bitstream_bits)
#define bit_ptr (decoder->bitstream_ptr)

    const CBPtab * tab;

    NEEDBITS (bit_buf, bits, bit_ptr);

    if (bit_buf >= 0x20000000)
    {
        tab = CBP_7 + (UBITS (bit_buf, 7) - 16);
        DUMPBITS (bit_buf, bits, tab->len);
        return tab->cbp;
    }
    else
    {
        tab = CBP_9 + UBITS (bit_buf, 9);
        DUMPBITS (bit_buf, bits, tab->len);
        return tab->cbp;
    }

#undef bit_buf
#undef bits
#undef bit_ptr
}

static inline int get_luma_dc_dct_diff (mpeg2_decoder_t * const decoder)
{
#define bit_buf (decoder->bitstream_buf)
#define bits (decoder->bitstream_bits)
#define bit_ptr (decoder->bitstream_ptr)

    const DCtab * tab;
    int size;
    int dc_diff;

    if (bit_buf < 0xf8000000)
    {
        tab = DC_lum_5 + UBITS (bit_buf, 5);
        size = tab->size;

        if (size)
        {
            bits += tab->len + size;
            bit_buf <<= tab->len;
            dc_diff =
                UBITS (bit_buf, size) - UBITS (SBITS (~bit_buf, 1), size);
            bit_buf <<= size;
            return dc_diff << decoder->intra_dc_precision;
        }
        else
        {
            DUMPBITS (bit_buf, bits, 3);
            return 0;
        }
    }
    else
    {
        tab = DC_long + (UBITS (bit_buf, 9) - 0x1e0);
        size = tab->size;
        DUMPBITS (bit_buf, bits, tab->len);
        NEEDBITS (bit_buf, bits, bit_ptr);
        dc_diff = UBITS (bit_buf, size) - UBITS (SBITS (~bit_buf, 1), size);
        DUMPBITS (bit_buf, bits, size);
        return dc_diff << decoder->intra_dc_precision;
    }

#undef bit_buf
#undef bits
#undef bit_ptr
}

#if MPEG2_COLOR
static inline int get_chroma_dc_dct_diff (mpeg2_decoder_t * const decoder)
{
#define bit_buf (decoder->bitstream_buf)
#define bits (decoder->bitstream_bits)
#define bit_ptr (decoder->bitstream_ptr)

    const DCtab * tab;
    int size;
    int dc_diff;

    if (bit_buf < 0xf8000000)
    {
        tab = DC_chrom_5 + UBITS (bit_buf, 5);
        size = tab->size;

        if (size)
        {
            bits += tab->len + size;
            bit_buf <<= tab->len;
            dc_diff =
                UBITS (bit_buf, size) - UBITS (SBITS (~bit_buf, 1), size);
            bit_buf <<= size;
            return dc_diff << decoder->intra_dc_precision;
        }
        else
        {
            DUMPBITS (bit_buf, bits, 2);
            return 0;
        }
    }
    else
    {
        tab = DC_long + (UBITS (bit_buf, 10) - 0x3e0);
        size = tab->size;
        DUMPBITS (bit_buf, bits, tab->len + 1);
        NEEDBITS (bit_buf, bits, bit_ptr);
        dc_diff = UBITS (bit_buf, size) - UBITS (SBITS (~bit_buf, 1), size);
        DUMPBITS (bit_buf, bits, size);
        return dc_diff << decoder->intra_dc_precision;
    }

#undef bit_buf
#undef bits
#undef bit_ptr
}
#endif /* MPEG2_COLOR */

#define SATURATE(val) \
    do {                                        \
        val <<= 4;                              \
        if (unlikely (val != (int16_t) val))    \
            val = (SBITS (val, 1) ^ 2047) << 4; \
    } while (0)

static void get_intra_block_B14 (mpeg2_decoder_t * const decoder,
                                 const uint16_t * const quant_matrix)
{
    uint32_t bit_buf = decoder->bitstream_buf;
    int bits = decoder->bitstream_bits;
    const uint8_t * bit_ptr = decoder->bitstream_ptr;
    const uint8_t * const scan = decoder->scan;
    int16_t * const dest = decoder->DCTblock;
    int mismatch = ~dest[0];
    int i = 0;
    int j;
    int val;
    const DCTtab * tab;

    NEEDBITS (bit_buf, bits, bit_ptr);

    while (1)
    {
        if (bit_buf >= 0x28000000)
        {
            tab = DCT_B14AC_5 + (UBITS (bit_buf, 5) - 5);

            i += tab->run;
            if (i >= 64)
                break;        /* end of block */

        normal_code:
            j = scan[i];
            bit_buf <<= tab->len;
            bits += tab->len + 1;
            val = (tab->level * quant_matrix[j]) >> 4;

            /* if (bitstream_get (1)) val = -val; */
            val = (val ^ SBITS (bit_buf, 1)) - SBITS (bit_buf, 1);

            SATURATE (val);
            dest[j] = val;
            mismatch ^= val;

            bit_buf <<= 1;
            NEEDBITS (bit_buf, bits, bit_ptr);

            continue;
        }
        else if (bit_buf >= 0x04000000)
        {
            tab = DCT_B14_8 + (UBITS (bit_buf, 8) - 4);

            i += tab->run;
            if (i < 64)
                goto normal_code;

            /* escape code */

            i += UBITS (bit_buf << 6, 6) - 64;
            if (i >= 64)
                break;        /* illegal, check needed to avoid buffer overflow */

            j = scan[i];

            DUMPBITS (bit_buf, bits, 12);
            NEEDBITS (bit_buf, bits, bit_ptr);
            val = (SBITS (bit_buf, 12) * quant_matrix[j]) / 16;

            SATURATE (val);
            dest[j] = val;
            mismatch ^= val;

            DUMPBITS (bit_buf, bits, 12);
            NEEDBITS (bit_buf, bits, bit_ptr);

            continue;
        }
        else if (bit_buf >= 0x02000000)
        {
            tab = DCT_B14_10 + (UBITS (bit_buf, 10) - 8);
            i += tab->run;
            if (i < 64)
                goto normal_code;
        }
        else if (bit_buf >= 0x00800000)
        {
            tab = DCT_13 + (UBITS (bit_buf, 13) - 16);
            i += tab->run;
            if (i < 64)
                goto normal_code;
        }
        else if (bit_buf >= 0x00200000)
        {
            tab = DCT_15 + (UBITS (bit_buf, 15) - 16);
            i += tab->run;
            if (i < 64)
                goto normal_code;
        }
        else
        {
            tab = DCT_16 + UBITS (bit_buf, 16);
            bit_buf <<= 16;
            GETWORD (bit_buf, bits + 16, bit_ptr);
            i += tab->run;
            if (i < 64)
                goto normal_code;
        }
        break;        /* illegal, check needed to avoid buffer overflow */
    }

    dest[63] ^= mismatch & 16;
    DUMPBITS (bit_buf, bits, tab->len); /* dump end of block code */
    decoder->bitstream_buf = bit_buf;
    decoder->bitstream_bits = bits;
    decoder->bitstream_ptr = bit_ptr;
}

static void get_intra_block_B15 (mpeg2_decoder_t * const decoder,
                                 const uint16_t * const quant_matrix)
{
    uint32_t bit_buf = decoder->bitstream_buf;
    int bits = decoder->bitstream_bits;
    const uint8_t * bit_ptr = decoder->bitstream_ptr;
    const uint8_t * const scan = decoder->scan;
    int16_t * const dest = decoder->DCTblock;
    int mismatch = ~dest[0];
    int i = 0;
    int j;
    int val;
    const DCTtab * tab;

    NEEDBITS (bit_buf, bits, bit_ptr);

    while (1)
    {
        if (bit_buf >= 0x04000000)
        {
            tab = DCT_B15_8 + (UBITS (bit_buf, 8) - 4);

            i += tab->run;

            if (i < 64)
            {
            normal_code:
                j = scan[i];
                bit_buf <<= tab->len;
                bits += tab->len + 1;
                val = (tab->level * quant_matrix[j]) >> 4;

                /* if (bitstream_get (1)) val = -val; */
                val = (val ^ SBITS (bit_buf, 1)) - SBITS (bit_buf, 1);

                SATURATE (val);
                dest[j] = val;
                mismatch ^= val;

                bit_buf <<= 1;
                NEEDBITS (bit_buf, bits, bit_ptr);

                continue;
            }
            else
            {
                /* end of block. I commented out this code because if we */
                /* dont exit here we will still exit at the later test :) */

                /* if (i >= 128) break;        */        /* end of block */

                /* escape code */

                i += UBITS (bit_buf << 6, 6) - 64;
                if (i >= 64)
                    break;        /* illegal, check against buffer overflow */

                j = scan[i];

                DUMPBITS (bit_buf, bits, 12);
                NEEDBITS (bit_buf, bits, bit_ptr);
                val = (SBITS (bit_buf, 12) * quant_matrix[j]) / 16;

                SATURATE (val);
                dest[j] = val;
                mismatch ^= val;

                DUMPBITS (bit_buf, bits, 12);
                NEEDBITS (bit_buf, bits, bit_ptr);

                continue;
            }
        }
        else if (bit_buf >= 0x02000000)
        {
            tab = DCT_B15_10 + (UBITS (bit_buf, 10) - 8);
            i += tab->run;
            if (i < 64)
                goto normal_code;
        }
        else if (bit_buf >= 0x00800000)
        {
            tab = DCT_13 + (UBITS (bit_buf, 13) - 16);
            i += tab->run;
            if (i < 64)
                goto normal_code;
        }
        else if (bit_buf >= 0x00200000)
        {
            tab = DCT_15 + (UBITS (bit_buf, 15) - 16);
            i += tab->run;
            if (i < 64)
                goto normal_code;
        }
        else
        {
            tab = DCT_16 + UBITS (bit_buf, 16);
            bit_buf <<= 16;
            GETWORD (bit_buf, bits + 16, bit_ptr);
            i += tab->run;
            if (i < 64)
                goto normal_code;
        }
        break;        /* illegal, check needed to avoid buffer overflow */
    }

    dest[63] ^= mismatch & 16;
    DUMPBITS (bit_buf, bits, tab->len); /* dump end of block code */
    decoder->bitstream_buf = bit_buf;
    decoder->bitstream_bits = bits;
    decoder->bitstream_ptr = bit_ptr;
}

static int get_non_intra_block (mpeg2_decoder_t * const decoder,
                                const uint16_t * const quant_matrix)
{
    uint32_t bit_buf = decoder->bitstream_buf;
    int bits = decoder->bitstream_bits;
    const uint8_t * bit_ptr = decoder->bitstream_ptr;
    const uint8_t * const scan = decoder->scan;
    int16_t * const dest = decoder->DCTblock;
    int mismatch = -1;
    int i = -1;
    int j;
    int val;
    const DCTtab * tab;

    NEEDBITS (bit_buf, bits, bit_ptr);

    if (bit_buf >= 0x28000000)
    {
        tab = DCT_B14DC_5 + (UBITS (bit_buf, 5) - 5);
        goto entry_1;
    }
    else
    {
        goto entry_2;
    }

    while (1)
    {
        if (bit_buf >= 0x28000000)
        {
            tab = DCT_B14AC_5 + (UBITS (bit_buf, 5) - 5);

        entry_1:
            i += tab->run;
            if (i >= 64)
                break;        /* end of block */

        normal_code:
            j = scan[i];
            bit_buf <<= tab->len;
            bits += tab->len + 1;
            val = ((2 * tab->level + 1) * quant_matrix[j]) >> 5;

            /* if (bitstream_get (1)) val = -val; */
            val = (val ^ SBITS (bit_buf, 1)) - SBITS (bit_buf, 1);

            SATURATE (val);
            dest[j] = val;
            mismatch ^= val;

            bit_buf <<= 1;
            NEEDBITS (bit_buf, bits, bit_ptr);

            continue;
        }

    entry_2:
        if (bit_buf >= 0x04000000)
        {
            tab = DCT_B14_8 + (UBITS (bit_buf, 8) - 4);

            i += tab->run;
            if (i < 64)
                goto normal_code;

            /* escape code */

            i += UBITS (bit_buf << 6, 6) - 64;
            if (i >= 64)
                break;        /* illegal, check needed to avoid buffer overflow */

            j = scan[i];

            DUMPBITS (bit_buf, bits, 12);
            NEEDBITS (bit_buf, bits, bit_ptr);
            val = 2 * (SBITS (bit_buf, 12) + SBITS (bit_buf, 1)) + 1;
            val = (val * quant_matrix[j]) / 32;

            SATURATE (val);
            dest[j] = val;
            mismatch ^= val;

            DUMPBITS (bit_buf, bits, 12);
            NEEDBITS (bit_buf, bits, bit_ptr);

            continue;
        }
        else if (bit_buf >= 0x02000000)
        {
            tab = DCT_B14_10 + (UBITS (bit_buf, 10) - 8);
            i += tab->run;
            if (i < 64)
                goto normal_code;
        }
        else if (bit_buf >= 0x00800000)
        {
            tab = DCT_13 + (UBITS (bit_buf, 13) - 16);
            i += tab->run;
            if (i < 64)
                goto normal_code;
        }
        else if (bit_buf >= 0x00200000)
        {
            tab = DCT_15 + (UBITS (bit_buf, 15) - 16);
            i += tab->run;
            if (i < 64)
                goto normal_code;
        }
        else
        {
            tab = DCT_16 + UBITS (bit_buf, 16);
            bit_buf <<= 16;
            GETWORD (bit_buf, bits + 16, bit_ptr);
            i += tab->run;
            if (i < 64)
                goto normal_code;
        }
        break;        /* illegal, check needed to avoid buffer overflow */
    }

    dest[63] ^= mismatch & 16;
    DUMPBITS (bit_buf, bits, tab->len); /* dump end of block code */
    decoder->bitstream_buf = bit_buf;
    decoder->bitstream_bits = bits;
    decoder->bitstream_ptr = bit_ptr;
    return i;
}

static void get_mpeg1_intra_block (mpeg2_decoder_t * const decoder)
{
    uint32_t bit_buf = decoder->bitstream_buf;
    int bits = decoder->bitstream_bits;
    const uint8_t * bit_ptr = decoder->bitstream_ptr;
    const uint8_t * const scan = decoder->scan;
    const uint16_t * const quant_matrix = decoder->quantizer_matrix[0];
    int16_t * const dest = decoder->DCTblock;
    int i = 0;
    int j;
    int val;
    const DCTtab * tab;

    NEEDBITS (bit_buf, bits, bit_ptr);

    while (1)
    {
        if (bit_buf >= 0x28000000)
        {
            tab = DCT_B14AC_5 + (UBITS (bit_buf, 5) - 5);

            i += tab->run;
            if (i >= 64)
                break;        /* end of block */

        normal_code:
            j = scan[i];
            bit_buf <<= tab->len;
            bits += tab->len + 1;
            val = (tab->level * quant_matrix[j]) >> 4;

            /* oddification */
            val = (val - 1) | 1;

            /* if (bitstream_get (1)) val = -val; */
            val = (val ^ SBITS (bit_buf, 1)) - SBITS (bit_buf, 1);

            SATURATE (val);
            dest[j] = val;

            bit_buf <<= 1;
            NEEDBITS (bit_buf, bits, bit_ptr);

            continue;
        }
        else if (bit_buf >= 0x04000000)
        {
            tab = DCT_B14_8 + (UBITS (bit_buf, 8) - 4);

            i += tab->run;
            if (i < 64)
                goto normal_code;

            /* escape code */

            i += UBITS (bit_buf << 6, 6) - 64;
            if (i >= 64)
                break;        /* illegal, check needed to avoid buffer overflow */

            j = scan[i];

            DUMPBITS (bit_buf, bits, 12);
            NEEDBITS (bit_buf, bits, bit_ptr);
            val = SBITS (bit_buf, 8);

            if (! (val & 0x7f))
            {
                DUMPBITS (bit_buf, bits, 8);
                val = UBITS (bit_buf, 8) + 2 * val;
            }

            val = (val * quant_matrix[j]) / 16;

            /* oddification */
            val = (val + ~SBITS (val, 1)) | 1;

            SATURATE (val);
            dest[j] = val;

            DUMPBITS (bit_buf, bits, 8);
            NEEDBITS (bit_buf, bits, bit_ptr);

            continue;
        }
        else if (bit_buf >= 0x02000000)
        {
            tab = DCT_B14_10 + (UBITS (bit_buf, 10) - 8);
            i += tab->run;
            if (i < 64)
                goto normal_code;
        }
        else if (bit_buf >= 0x00800000)
        {
            tab = DCT_13 + (UBITS (bit_buf, 13) - 16);
            i += tab->run;
            if (i < 64)
                goto normal_code;
        }
        else if (bit_buf >= 0x00200000)
        {
            tab = DCT_15 + (UBITS (bit_buf, 15) - 16);
            i += tab->run;
            if (i < 64)
                goto normal_code;
        }
        else
        {
            tab = DCT_16 + UBITS (bit_buf, 16);
            bit_buf <<= 16;
            GETWORD (bit_buf, bits + 16, bit_ptr);
            i += tab->run;
            if (i < 64)
                goto normal_code;
        }
        break;        /* illegal, check needed to avoid buffer overflow */
    }

    DUMPBITS (bit_buf, bits, tab->len); /* dump end of block code */
    decoder->bitstream_buf = bit_buf;
    decoder->bitstream_bits = bits;
    decoder->bitstream_ptr = bit_ptr;
}

static int get_mpeg1_non_intra_block (mpeg2_decoder_t * const decoder)
{
    uint32_t bit_buf = decoder->bitstream_buf;
    int bits = decoder->bitstream_bits;
    const uint8_t * bit_ptr = decoder->bitstream_ptr;
    const uint8_t * const scan = decoder->scan;
    const uint16_t * const quant_matrix = decoder->quantizer_matrix[1];
    int16_t * const dest = decoder->DCTblock;
    int i = -1;
    int j;
    int val;
    const DCTtab * tab;

    NEEDBITS (bit_buf, bits, bit_ptr);
    if (bit_buf >= 0x28000000)
    {
        tab = DCT_B14DC_5 + (UBITS (bit_buf, 5) - 5);
        goto entry_1;
    }
    else
    {
        goto entry_2;
    }

    while (1)
    {
        if (bit_buf >= 0x28000000)
        {
            tab = DCT_B14AC_5 + (UBITS (bit_buf, 5) - 5);

        entry_1:
            i += tab->run;
            if (i >= 64)
                break;        /* end of block */

        normal_code:
            j = scan[i];
            bit_buf <<= tab->len;
            bits += tab->len + 1;
            val = ((2 * tab->level + 1) * quant_matrix[j]) >> 5;

            /* oddification */
            val = (val - 1) | 1;

            /* if (bitstream_get (1)) val = -val; */
            val = (val ^ SBITS (bit_buf, 1)) - SBITS (bit_buf, 1);

            SATURATE (val);
            dest[j] = val;

            bit_buf <<= 1;
            NEEDBITS (bit_buf, bits, bit_ptr);

            continue;
        }

    entry_2:
        if (bit_buf >= 0x04000000)
        {
            tab = DCT_B14_8 + (UBITS (bit_buf, 8) - 4);

            i += tab->run;
            if (i < 64)
                goto normal_code;

            /* escape code */

            i += UBITS (bit_buf << 6, 6) - 64;
            if (i >= 64)
                break;        /* illegal, check needed to avoid buffer overflow */

            j = scan[i];

            DUMPBITS (bit_buf, bits, 12);
            NEEDBITS (bit_buf, bits, bit_ptr);
            val = SBITS (bit_buf, 8);

            if (! (val & 0x7f))
            {
                DUMPBITS (bit_buf, bits, 8);
                val = UBITS (bit_buf, 8) + 2 * val;
            }

            val = 2 * (val + SBITS (val, 1)) + 1;
            val = (val * quant_matrix[j]) / 32;

            /* oddification */
            val = (val + ~SBITS (val, 1)) | 1;

            SATURATE (val);
            dest[j] = val;

            DUMPBITS (bit_buf, bits, 8);
            NEEDBITS (bit_buf, bits, bit_ptr);

            continue;

        }
        else if (bit_buf >= 0x02000000)
        {
            tab = DCT_B14_10 + (UBITS (bit_buf, 10) - 8);
            i += tab->run;
            if (i < 64)
                goto normal_code;
        }
        else if (bit_buf >= 0x00800000)
        {
            tab = DCT_13 + (UBITS (bit_buf, 13) - 16);
            i += tab->run;
            if (i < 64)
                goto normal_code;
        }
        else if (bit_buf >= 0x00200000)
        {
            tab = DCT_15 + (UBITS (bit_buf, 15) - 16);
            i += tab->run;
            if (i < 64)
                goto normal_code;
        }
        else
        {
            tab = DCT_16 + UBITS (bit_buf, 16);
            bit_buf <<= 16;
            GETWORD (bit_buf, bits + 16, bit_ptr);
            i += tab->run;
            if (i < 64)
                goto normal_code;
        }
        break;        /* illegal, check needed to avoid buffer overflow */
    }

    DUMPBITS (bit_buf, bits, tab->len); /* dump end of block code */
    decoder->bitstream_buf = bit_buf;
    decoder->bitstream_bits = bits;
    decoder->bitstream_ptr = bit_ptr;
    return i;
}

static inline void slice_intra_DCT (mpeg2_decoder_t * const decoder,
                                    const int cc,
                                    uint8_t * const dest, const int stride)
{
#define bit_buf (decoder->bitstream_buf)
#define bits (decoder->bitstream_bits)
#define bit_ptr (decoder->bitstream_ptr)

    NEEDBITS (bit_buf, bits, bit_ptr);
    /* Get the intra DC coefficient and inverse quantize it */
    if (cc == 0)
    {
        decoder->dc_dct_pred[0] += get_luma_dc_dct_diff (decoder);
        decoder->DCTblock[0] = decoder->dc_dct_pred[0];

    }
#if MPEG2_COLOR
    else
    {
        decoder->dc_dct_pred[cc] += get_chroma_dc_dct_diff (decoder);
        decoder->DCTblock[0] = decoder->dc_dct_pred[cc];
    }
#endif

    if (decoder->mpeg1)
    {
        if (decoder->coding_type != D_TYPE)
            get_mpeg1_intra_block (decoder);
    }
    else if (decoder->intra_vlc_format)
    {
        get_intra_block_B15 (decoder, decoder->quantizer_matrix[cc ? 2 : 0]);
    }
    else
    {
        get_intra_block_B14 (decoder, decoder->quantizer_matrix[cc ? 2 : 0]);
    }

    mpeg2_idct_copy (decoder->DCTblock, dest, stride);

#undef bit_buf
#undef bits
#undef bit_ptr
}

static inline void slice_non_intra_DCT (mpeg2_decoder_t * const decoder,
                                        const int cc,
                                        uint8_t * const dest, const int stride)
{
    int last;

    if (decoder->mpeg1)
    {
        last = get_mpeg1_non_intra_block (decoder);
    }
    else
    {
        last = get_non_intra_block (decoder,
                                    decoder->quantizer_matrix[cc ? 3 : 1]);
    }

    mpeg2_idct_add (last, decoder->DCTblock, dest, stride);
}

#if !MPEG2_COLOR
static void skip_mpeg1_intra_block (mpeg2_decoder_t * const decoder)
{
    uint32_t bit_buf = decoder->bitstream_buf;
    int bits = decoder->bitstream_bits;
    const uint8_t * bit_ptr = decoder->bitstream_ptr;
    int i = 0;
    const DCTtab * tab;

    NEEDBITS (bit_buf, bits, bit_ptr);

    while (1)
    {
        if (bit_buf >= 0x28000000)
        {
            tab = DCT_B14AC_5 + (UBITS (bit_buf, 5) - 5);

            i += tab->run;
            if (i >= 64)
                break;        /* end of block */

        normal_code:
            bit_buf <<= tab->len + 1;
            bits += tab->len + 1;
            NEEDBITS (bit_buf, bits, bit_ptr);
            continue;
        }
        else if (bit_buf >= 0x04000000)
        {
            tab = DCT_B14_8 + (UBITS (bit_buf, 8) - 4);

            i += tab->run;
            if (i < 64)
                goto normal_code;

            /* escape code */

            i += UBITS (bit_buf << 6, 6) - 64;
            if (i >= 64)
                break;        /* illegal, check needed to avoid buffer overflow */

            DUMPBITS (bit_buf, bits, 12);
            NEEDBITS (bit_buf, bits, bit_ptr);

            if (!(SBITS (bit_buf, 8) & 0x7f))
                DUMPBITS (bit_buf, bits, 8);

            DUMPBITS (bit_buf, bits, 8);
            NEEDBITS (bit_buf, bits, bit_ptr);

            continue;
        }
        else if (bit_buf >= 0x02000000)
        {
            tab = DCT_B14_10 + (UBITS (bit_buf, 10) - 8);
            i += tab->run;
            if (i < 64)
                goto normal_code;
        }
        else if (bit_buf >= 0x00800000)
        {
            tab = DCT_13 + (UBITS (bit_buf, 13) - 16);
            i += tab->run;
            if (i < 64)
                goto normal_code;
        }
        else if (bit_buf >= 0x00200000)
        {
            tab = DCT_15 + (UBITS (bit_buf, 15) - 16);
            i += tab->run;
            if (i < 64)
                goto normal_code;
        }
        else
        {
            tab = DCT_16 + UBITS (bit_buf, 16);
            bit_buf <<= 16;
            GETWORD (bit_buf, bits + 16, bit_ptr);
            i += tab->run;
            if (i < 64)
                goto normal_code;
        }
        break;        /* illegal, check needed to avoid buffer overflow */
    }

    DUMPBITS (bit_buf, bits, 2);        /* dump end of block code */
    decoder->bitstream_buf = bit_buf;
    decoder->bitstream_bits = bits;
    decoder->bitstream_ptr = bit_ptr;
}

static void skip_intra_block_B14 (mpeg2_decoder_t * const decoder)
{
    uint32_t bit_buf = decoder->bitstream_buf;
    int bits = decoder->bitstream_bits;
    const uint8_t * bit_ptr = decoder->bitstream_ptr;
    int i = 0;
    const DCTtab * tab;

    NEEDBITS (bit_buf, bits, bit_ptr);

    while (1)
    {
        if (bit_buf >= 0x28000000)
        {
            tab = DCT_B14AC_5 + (UBITS (bit_buf, 5) - 5);

            i += tab->run;
            if (i >= 64)
                break;        /* end of block */

        normal_code:
            bit_buf <<= tab->len + 1;
            bits += tab->len + 1;
            NEEDBITS (bit_buf, bits, bit_ptr);
            continue;
        }
        else if (bit_buf >= 0x04000000)
        {
            tab = DCT_B14_8 + (UBITS (bit_buf, 8) - 4);

            i += tab->run;
            if (i < 64)
                goto normal_code;

            /* escape code */

            i += UBITS (bit_buf << 6, 6) - 64;
            if (i >= 64)
                break;        /* illegal, check needed to avoid buffer overflow */

            DUMPBITS (bit_buf, bits, 12); /* Can't dump more than 16 atm */
            NEEDBITS (bit_buf, bits, bit_ptr);
            DUMPBITS (bit_buf, bits, 12);
            NEEDBITS (bit_buf, bits, bit_ptr);
            continue;
        }
        else if (bit_buf >= 0x02000000)
        {
            tab = DCT_B14_10 + (UBITS (bit_buf, 10) - 8);
            i += tab->run;
            if (i < 64)
                goto normal_code;
        }
        else if (bit_buf >= 0x00800000)
        {
            tab = DCT_13 + (UBITS (bit_buf, 13) - 16);
            i += tab->run;
            if (i < 64)
                goto normal_code;
        }
        else if (bit_buf >= 0x00200000)
        {
            tab = DCT_15 + (UBITS (bit_buf, 15) - 16);
            i += tab->run;
            if (i < 64)
                goto normal_code;
        }
        else
        {
            tab = DCT_16 + UBITS (bit_buf, 16);
            bit_buf <<= 16;
            GETWORD (bit_buf, bits + 16, bit_ptr);
            i += tab->run;
            if (i < 64)
                goto normal_code;
        }
        break;        /* illegal, check needed to avoid buffer overflow */
    }

    DUMPBITS (bit_buf, bits, 2);        /* dump end of block code */
    decoder->bitstream_buf = bit_buf;
    decoder->bitstream_bits = bits;
    decoder->bitstream_ptr = bit_ptr;
}

static void skip_intra_block_B15 (mpeg2_decoder_t * const decoder)
{
    uint32_t bit_buf = decoder->bitstream_buf;
    int bits = decoder->bitstream_bits;
    const uint8_t * bit_ptr = decoder->bitstream_ptr;
    int i = 0;
    const DCTtab * tab;

    NEEDBITS (bit_buf, bits, bit_ptr);

    while (1)
    {
        if (bit_buf >= 0x04000000)
        {
            tab = DCT_B15_8 + (UBITS (bit_buf, 8) - 4);

            i += tab->run;

            if (i < 64)
            {
            normal_code:
                bit_buf <<= tab->len + 1;
                bits += tab->len + 1;
                NEEDBITS (bit_buf, bits, bit_ptr);
                continue;
            }
            else
            {
                /* end of block. I commented out this code because if we */
                /* dont exit here we will still exit at the later test :) */

                /* if (i >= 128) break;        */        /* end of block */

                /* escape code */

                i += UBITS (bit_buf << 6, 6) - 64;
                if (i >= 64)
                    break;        /* illegal, check against buffer overflow */

                DUMPBITS (bit_buf, bits, 12); /* Can't dump more than 16 atm */
                NEEDBITS (bit_buf, bits, bit_ptr);
                DUMPBITS (bit_buf, bits, 12);
                NEEDBITS (bit_buf, bits, bit_ptr);
                continue;
            }
        }
        else if (bit_buf >= 0x02000000)
        {
            tab = DCT_B15_10 + (UBITS (bit_buf, 10) - 8);
            i += tab->run;
            if (i < 64)
                goto normal_code;
        }
        else if (bit_buf >= 0x00800000)
        {
            tab = DCT_13 + (UBITS (bit_buf, 13) - 16);
            i += tab->run;
            if (i < 64)
                goto normal_code;
        }
        else if (bit_buf >= 0x00200000)
        {
            tab = DCT_15 + (UBITS (bit_buf, 15) - 16);
            i += tab->run;
            if (i < 64)
                goto normal_code;
        }
        else
        {
            tab = DCT_16 + UBITS (bit_buf, 16);
            bit_buf <<= 16;
            GETWORD (bit_buf, bits + 16, bit_ptr);
            i += tab->run;
            if (i < 64)
                goto normal_code;
        }
        break;        /* illegal, check needed to avoid buffer overflow */
    }

    DUMPBITS (bit_buf, bits, 4);        /* dump end of block code */
    decoder->bitstream_buf = bit_buf;
    decoder->bitstream_bits = bits;
    decoder->bitstream_ptr = bit_ptr;
}

static void skip_non_intra_block (mpeg2_decoder_t * const decoder)
{
    uint32_t bit_buf = decoder->bitstream_buf;
    int bits = decoder->bitstream_bits;
    const uint8_t * bit_ptr = decoder->bitstream_ptr;
    int i = -1;
    const DCTtab * tab;

    NEEDBITS (bit_buf, bits, bit_ptr);

    if (bit_buf >= 0x28000000)
    {
        tab = DCT_B14DC_5 + (UBITS (bit_buf, 5) - 5);
        goto entry_1;
    }
    else
    {
        goto entry_2;
    }

    while (1)
    {
        if (bit_buf >= 0x28000000)
        {
            tab = DCT_B14AC_5 + (UBITS (bit_buf, 5) - 5);

        entry_1:
            i += tab->run;
            if (i >= 64)
                break;        /* end of block */

        normal_code:
            bit_buf <<= tab->len + 1;
            bits += tab->len + 1;
            NEEDBITS (bit_buf, bits, bit_ptr);

            continue;
        }

    entry_2:
        if (bit_buf >= 0x04000000)
        {
            tab = DCT_B14_8 + (UBITS (bit_buf, 8) - 4);

            i += tab->run;
            if (i < 64)
                goto normal_code;

            /* escape code */

            i += UBITS (bit_buf << 6, 6) - 64;
            if (i >= 64)
                break;        /* illegal, check needed to avoid buffer overflow */

            if (decoder->mpeg1)
            {    
                DUMPBITS (bit_buf, bits, 12);
                NEEDBITS (bit_buf, bits, bit_ptr);

                if (!(SBITS (bit_buf, 8) & 0x7f))
                    DUMPBITS (bit_buf, bits, 8);

                DUMPBITS (bit_buf, bits, 8);
            }
            else
            {
                DUMPBITS (bit_buf, bits, 12); 
                NEEDBITS (bit_buf, bits, bit_ptr);
                DUMPBITS (bit_buf, bits, 12);
            }

            NEEDBITS (bit_buf, bits, bit_ptr);
            continue;
        }
        else if (bit_buf >= 0x02000000)
        {
            tab = DCT_B14_10 + (UBITS (bit_buf, 10) - 8);
            i += tab->run;
            if (i < 64)
                goto normal_code;
        }
        else if (bit_buf >= 0x00800000)
        {
            tab = DCT_13 + (UBITS (bit_buf, 13) - 16);
            i += tab->run;
            if (i < 64)
                goto normal_code;
        }
        else if (bit_buf >= 0x00200000)
        {
            tab = DCT_15 + (UBITS (bit_buf, 15) - 16);
            i += tab->run;
            if (i < 64)
                goto normal_code;
        }
        else
        {
            tab = DCT_16 + UBITS (bit_buf, 16);
            bit_buf <<= 16;
            GETWORD (bit_buf, bits + 16, bit_ptr);
            i += tab->run;
            if (i < 64)
                goto normal_code;
        }
        break;        /* illegal, check needed to avoid buffer overflow */
    }

    DUMPBITS (bit_buf, bits, 2);        /* dump end of block code */
    decoder->bitstream_buf = bit_buf;
    decoder->bitstream_bits = bits;
    decoder->bitstream_ptr = bit_ptr;
}

static void skip_chroma_dc_dct_diff (mpeg2_decoder_t * const decoder)
{
#define bit_buf (decoder->bitstream_buf)
#define bits (decoder->bitstream_bits)
#define bit_ptr (decoder->bitstream_ptr)

    const DCtab * tab;
    int size;

    if (bit_buf < 0xf8000000)
    {
        tab = DC_chrom_5 + UBITS (bit_buf, 5);
        size = tab->size;

        if (size)
        {
            bits += tab->len + size;
            bit_buf <<= tab->len;
            bit_buf <<= size;
        }
        else
        {
            DUMPBITS (bit_buf, bits, 2);
        }
    }
    else
    {
        tab = DC_long + (UBITS (bit_buf, 10) - 0x3e0);
        size = tab->size;
        DUMPBITS (bit_buf, bits, tab->len + 1);
        NEEDBITS (bit_buf, bits, bit_ptr);
        DUMPBITS (bit_buf, bits, size);
    }

#undef bit_buf
#undef bits
#undef bit_ptr
}

static void skip_chroma_non_intra (mpeg2_decoder_t * const decoder,
                                   uint32_t coded_block_pattern)
{
    static const uint32_t cbp_mask[3] =
    {
        0x00000030,
        0xc0000030,
        0xfc000030,
    };

    uint32_t cbp = coded_block_pattern &
                    cbp_mask[MIN((unsigned)decoder->chroma_format, 2u)];

    while (cbp)
    {
        skip_non_intra_block (decoder);
        cbp &= (cbp - 1);
    }
}

static void skip_chroma_intra (mpeg2_decoder_t * const decoder)
{
#define bit_buf (decoder->bitstream_buf)
#define bits (decoder->bitstream_bits)
#define bit_ptr (decoder->bitstream_ptr)
    int i = 2 << decoder->chroma_format;

    if ((unsigned)i > 8)
        i = 8;

    while (i-- > 0)
    {
        NEEDBITS (bit_buf, bits, bit_ptr);

        skip_chroma_dc_dct_diff (decoder);

        if (decoder->mpeg1)
        {
            if (decoder->coding_type != D_TYPE)
                skip_mpeg1_intra_block (decoder);
        }
        else if (decoder->intra_vlc_format)
        {
            skip_intra_block_B15 (decoder);
        }
        else
        {
            skip_intra_block_B14 (decoder);
        }
    }

    if (decoder->chroma_format == 0 && decoder->coding_type == D_TYPE)
    {
        NEEDBITS (bit_buf, bits, bit_ptr);
        DUMPBITS (bit_buf, bits, 1);
    }

#undef bit_buf
#undef bits
#undef bit_ptr
}
#endif /* !MPEG2_COLOR */

#define MOTION_420(table, ref, motion_x, motion_y, size, y) \
    pos_x = 2 * decoder->offset + motion_x;                                   \
    pos_y = 2 * decoder->v_offset + motion_y + 2 * y;                         \
                                                                              \
    if (unlikely (pos_x > decoder->limit_x))                                  \
    {                                                                         \
        pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x;                      \
        motion_x = pos_x - 2 * decoder->offset;                               \
    }                                                                         \
                                                                              \
    if (unlikely (pos_y > decoder->limit_y_ ## size))                         \
    {                                                                         \
        pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y_ ## size;             \
        motion_y = pos_y - 2 * decoder->v_offset - 2 * y;                     \
    }                                                                         \
                                                                              \
    xy_half = ((pos_y & 1) << 1) | (pos_x & 1);                               \
    table[xy_half] (decoder->dest[0] + y * decoder->stride + decoder->offset, \
                    ref[0] + (pos_x >> 1) + (pos_y >> 1) * decoder->stride,   \
                    decoder->stride, size);                                   \
                                                                              \
    if (MPEG2_COLOR)                                                          \
    {                                                                         \
    motion_x /= 2;                                                            \
    motion_y /= 2;                                                            \
    xy_half = ((motion_y & 1) << 1) | (motion_x & 1);                         \
    offset = ((decoder->offset + motion_x) >> 1) +                            \
                ((((decoder->v_offset + motion_y) >> 1) + y/2) *              \
                    decoder->uv_stride);                                      \
                                                                              \
    table[4+xy_half] (decoder->dest[1] + y/2 * decoder->uv_stride +           \
                      (decoder->offset >> 1), ref[1] + offset,                \
                      decoder->uv_stride, size/2);                            \
    table[4+xy_half] (decoder->dest[2] + y/2 * decoder->uv_stride +           \
                      (decoder->offset >> 1), ref[2] + offset,                \
                      decoder->uv_stride, size/2);                            \
    }

#define MOTION_FIELD_420(table, ref, motion_x, motion_y, \
                         dest_field, op, src_field)      \
    pos_x = 2 * decoder->offset + motion_x;                                   \
    pos_y = decoder->v_offset + motion_y;                                     \
                                                                              \
    if (unlikely (pos_x > decoder->limit_x))                                  \
    {                                                                         \
        pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x;                      \
        motion_x = pos_x - 2 * decoder->offset;                               \
    }                                                                         \
                                                                              \
    if (unlikely (pos_y > decoder->limit_y))                                  \
    {                                                                         \
        pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y;                      \
        motion_y = pos_y - decoder->v_offset;                                 \
    }                                                                         \
                                                                              \
    xy_half = ((pos_y & 1) << 1) | (pos_x & 1);                               \
    table[xy_half] (decoder->dest[0] + dest_field * decoder->stride +         \
                    decoder->offset,                                          \
                    (ref[0] + (pos_x >> 1) +                                  \
                        ((pos_y op) + src_field) * decoder->stride),          \
                    2 * decoder->stride, 8);                                  \
                                                                              \
    if (MPEG2_COLOR)                                                          \
    {                                                                         \
    motion_x /= 2;                                                            \
    motion_y /= 2;                                                            \
    xy_half = ((motion_y & 1) << 1) | (motion_x & 1);                         \
    offset = ((decoder->offset + motion_x) >> 1) +                            \
              (((decoder->v_offset >> 1) + (motion_y op) + src_field) *       \
                    decoder->uv_stride);                                      \
                                                                              \
    table[4+xy_half] (decoder->dest[1] + dest_field * decoder->uv_stride +    \
                      (decoder->offset >> 1), ref[1] + offset,                \
                      2 * decoder->uv_stride, 4);                             \
    table[4+xy_half] (decoder->dest[2] + dest_field * decoder->uv_stride +    \
                      (decoder->offset >> 1), ref[2] + offset,                \
                      2 * decoder->uv_stride, 4);                             \
    }

#define MOTION_DMV_420(table, ref, motion_x, motion_y) \
    pos_x = 2 * decoder->offset + motion_x;                                   \
    pos_y = decoder->v_offset + motion_y;                                     \
                                                                              \
    if (unlikely (pos_x > decoder->limit_x))                                  \
    {                                                                         \
        pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x;                      \
        motion_x = pos_x - 2 * decoder->offset;                               \
    }                                                                         \
                                                                              \
    if (unlikely (pos_y > decoder->limit_y))                                  \
    {                                                                         \
        pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y;                      \
        motion_y = pos_y - decoder->v_offset;                                 \
    }                                                                         \
                                                                              \
    xy_half = ((pos_y & 1) << 1) | (pos_x & 1);                               \
    offset = (pos_x >> 1) + (pos_y & ~1) * decoder->stride;                   \
    table[xy_half] (decoder->dest[0] + decoder->offset,                       \
                    ref[0] + offset, 2 * decoder->stride, 8);                 \
    table[xy_half] (decoder->dest[0] + decoder->stride + decoder->offset,     \
                    ref[0] + decoder->stride + offset,                        \
                    2 * decoder->stride, 8);                                  \
                                                                              \
    if (MPEG2_COLOR)                                                          \
    {                                                                         \
    motion_x /= 2;                                                            \
    motion_y /= 2;                                                            \
    xy_half = ((motion_y & 1) << 1) | (motion_x & 1);                         \
    offset = ((decoder->offset + motion_x) >> 1) +                            \
                (((decoder->v_offset >> 1) + (motion_y & ~1)) *               \
                    decoder->uv_stride);                                      \
                                                                              \
    table[4+xy_half] (decoder->dest[1] + (decoder->offset >> 1),              \
                      ref[1] + offset, 2 * decoder->uv_stride, 4);            \
    table[4+xy_half] (decoder->dest[1] + decoder->uv_stride +                 \
                      (decoder->offset >> 1),                                 \
                      ref[1] + decoder->uv_stride + offset,                   \
                      2 * decoder->uv_stride, 4);                             \
    table[4+xy_half] (decoder->dest[2] + (decoder->offset >> 1),              \
                      ref[2] + offset, 2 * decoder->uv_stride, 4);            \
    table[4+xy_half] (decoder->dest[2] + decoder->uv_stride +                 \
                      (decoder->offset >> 1),                                 \
                      ref[2] + decoder->uv_stride + offset,                   \
                      2 * decoder->uv_stride, 4);                             \
    }

#define MOTION_ZERO_420(table, ref) \
    table[0] (decoder->dest[0] + decoder->offset,                             \
              (ref[0] + decoder->offset +                                     \
               decoder->v_offset * decoder->stride), decoder->stride, 16);    \
                                                                              \
    if (MPEG2_COLOR)                                                          \
    {                                                                         \
    offset = ((decoder->offset >> 1) +                                        \
              (decoder->v_offset >> 1) * decoder->uv_stride);                 \
                                                                              \
    table[4] (decoder->dest[1] + (decoder->offset >> 1),                      \
              ref[1] + offset, decoder->uv_stride, 8);                        \
    table[4] (decoder->dest[2] + (decoder->offset >> 1),                      \
              ref[2] + offset, decoder->uv_stride, 8);                        \
    }

#define MOTION_422(table, ref, motion_x, motion_y, size, y) \
    pos_x = 2 * decoder->offset + motion_x;                                   \
    pos_y = 2 * decoder->v_offset + motion_y + 2 * y;                         \
                                                                              \
    if (unlikely (pos_x > decoder->limit_x))                                  \
    {                                                                         \
        pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x;                      \
        motion_x = pos_x - 2 * decoder->offset;                               \
    }                                                                         \
                                                                              \
    if (unlikely (pos_y > decoder->limit_y_ ## size))                         \
    {                                                                         \
        pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y_ ## size;             \
        motion_y = pos_y - 2 * decoder->v_offset - 2 * y;                     \
    }                                                                         \
                                                                              \
    xy_half = ((pos_y & 1) << 1) | (pos_x & 1);                               \
    offset = (pos_x >> 1) + (pos_y >> 1) * decoder->stride;                   \
                                                                              \
    table[xy_half] (decoder->dest[0] + y * decoder->stride + decoder->offset, \
                    ref[0] + offset, decoder->stride, size);                  \
                                                                              \
    if (MPEG2_COLOR)                                                          \
    {                                                                         \
    offset = (offset + (motion_x & (motion_x < 0))) >> 1;                     \
    motion_x /= 2;                                                            \
    xy_half = ((pos_y & 1) << 1) | (motion_x & 1);                            \
                                                                              \
    table[4+xy_half] (decoder->dest[1] + y * decoder->uv_stride +             \
                      (decoder->offset >> 1), ref[1] + offset,                \
                      decoder->uv_stride, size);                              \
    table[4+xy_half] (decoder->dest[2] + y * decoder->uv_stride +             \
                      (decoder->offset >> 1), ref[2] + offset,                \
                      decoder->uv_stride, size);                              \
    }

#define MOTION_FIELD_422(table, ref, motion_x, motion_y, \
                         dest_field, op, src_field)      \
    pos_x = 2 * decoder->offset + motion_x;                                   \
    pos_y = decoder->v_offset + motion_y;                                     \
                                                                              \
    if (unlikely (pos_x > decoder->limit_x))                                  \
    {                                                                         \
        pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x;                      \
        motion_x = pos_x - 2 * decoder->offset;                               \
    }                                                                         \
                                                                              \
    if (unlikely (pos_y > decoder->limit_y))                                  \
    {                                                                         \
        pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y;                      \
        motion_y = pos_y - decoder->v_offset;                                 \
    }                                                                         \
                                                                              \
    xy_half = ((pos_y & 1) << 1) | (pos_x & 1);                               \
    offset = (pos_x >> 1) + ((pos_y op) + src_field) * decoder->stride;       \
                                                                              \
    table[xy_half] (decoder->dest[0] + dest_field * decoder->stride +         \
                    decoder->offset, ref[0] + offset,                         \
                    2 * decoder->stride, 8);                                  \
                                                                              \
    if (MPEG2_COLOR)                                                          \
    {                                                                         \
    offset = (offset + (motion_x & (motion_x < 0))) >> 1;                     \
    motion_x /= 2;                                                            \
    xy_half = ((pos_y & 1) << 1) | (motion_x & 1);                            \
                                                                              \
    table[4+xy_half] (decoder->dest[1] + dest_field * decoder->uv_stride +    \
                      (decoder->offset >> 1), ref[1] + offset,                \
                      2 * decoder->uv_stride, 8);                             \
    table[4+xy_half] (decoder->dest[2] + dest_field * decoder->uv_stride +    \
                      (decoder->offset >> 1), ref[2] + offset,                \
                      2 * decoder->uv_stride, 8);                             \
    }

#define MOTION_DMV_422(table, ref, motion_x, motion_y) \
    pos_x = 2 * decoder->offset + motion_x;                                   \
    pos_y = decoder->v_offset + motion_y;                                     \
                                                                              \
    if (unlikely (pos_x > decoder->limit_x))                                  \
    {                                                                         \
        pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x;                      \
        motion_x = pos_x - 2 * decoder->offset;                               \
    }                                                                         \
                                                                              \
    if (unlikely (pos_y > decoder->limit_y))                                  \
    {                                                                         \
        pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y;                      \
        motion_y = pos_y - decoder->v_offset;                                 \
    }                                                                         \
                                                                              \
    xy_half = ((pos_y & 1) << 1) | (pos_x & 1);                               \
    offset = (pos_x >> 1) + (pos_y & ~1) * decoder->stride;                   \
                                                                              \
    table[xy_half] (decoder->dest[0] + decoder->offset,                       \
                    ref[0] + offset, 2 * decoder->stride, 8);                 \
    table[xy_half] (decoder->dest[0] + decoder->stride + decoder->offset,     \
                    ref[0] + decoder->stride + offset,                        \
                    2 * decoder->stride, 8);                                  \
                                                                              \
    if (MPEG2_COLOR)                                                          \
    {                                                                         \
    offset = (offset + (motion_x & (motion_x < 0))) >> 1;                     \
    motion_x /= 2;                                                            \
    xy_half = ((pos_y & 1) << 1) | (motion_x & 1);                            \
                                                                              \
    table[4+xy_half] (decoder->dest[1] + (decoder->offset >> 1),              \
                      ref[1] + offset, 2 * decoder->uv_stride, 8);            \
    table[4+xy_half] (decoder->dest[1] + decoder->uv_stride +                 \
                      (decoder->offset >> 1),                                 \
                      ref[1] + decoder->uv_stride + offset,                   \
                      2 * decoder->uv_stride, 8);                             \
    table[4+xy_half] (decoder->dest[2] + (decoder->offset >> 1),              \
                      ref[2] + offset, 2 * decoder->uv_stride, 8);            \
    table[4+xy_half] (decoder->dest[2] + decoder->uv_stride +                 \
                      (decoder->offset >> 1),                                 \
                      ref[2] + decoder->uv_stride + offset,                   \
                      2 * decoder->uv_stride, 8);                             \
    }

#define MOTION_ZERO_422(table, ref) \
    offset = decoder->offset + decoder->v_offset * decoder->stride;           \
    table[0] (decoder->dest[0] + decoder->offset,                             \
              ref[0] + offset, decoder->stride, 16);                          \
                                                                              \
    if (MPEG2_COLOR)                                                          \
    {                                                                         \
    offset >>= 1;                                                             \
    table[4] (decoder->dest[1] + (decoder->offset >> 1),                      \
              ref[1] + offset, decoder->uv_stride, 16);                       \
    table[4] (decoder->dest[2] + (decoder->offset >> 1),                      \
              ref[2] + offset, decoder->uv_stride, 16);                       \
    }

#define MOTION_444(table, ref, motion_x, motion_y, size, y) \
    pos_x = 2 * decoder->offset + motion_x;                                   \
    pos_y = 2 * decoder->v_offset + motion_y + 2 * y;                         \
                                                                              \
    if (unlikely (pos_x > decoder->limit_x))                                  \
    {                                                                         \
        pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x;                      \
        motion_x = pos_x - 2 * decoder->offset;                               \
    }                                                                         \
                                                                              \
    if (unlikely (pos_y > decoder->limit_y_ ## size))                         \
    {                                                                         \
        pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y_ ## size;             \
        motion_y = pos_y - 2 * decoder->v_offset - 2 * y;                     \
    }                                                                         \
                                                                              \
    xy_half = ((pos_y & 1) << 1) | (pos_x & 1);                               \
    offset = (pos_x >> 1) + (pos_y >> 1) * decoder->stride;                   \
                                                                              \
    table[xy_half] (decoder->dest[0] + y * decoder->stride + decoder->offset, \
                    ref[0] + offset, decoder->stride, size);                  \
                                                                              \
    if (MPEG2_COLOR)                                                          \
    {                                                                         \
    table[xy_half] (decoder->dest[1] + y * decoder->stride + decoder->offset, \
                    ref[1] + offset, decoder->stride, size);                  \
    table[xy_half] (decoder->dest[2] + y * decoder->stride + decoder->offset, \
                    ref[2] + offset, decoder->stride, size);                  \
    }

#define MOTION_FIELD_444(table, ref, motion_x, motion_y, \
                         dest_field, op, src_field)      \
    pos_x = 2 * decoder->offset + motion_x;                                   \
    pos_y = decoder->v_offset + motion_y;                                     \
                                                                              \
    if (unlikely (pos_x > decoder->limit_x))                                  \
    {                                                                         \
        pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x;                      \
        motion_x = pos_x - 2 * decoder->offset;                               \
    }                                                                         \
                                                                              \
    if (unlikely (pos_y > decoder->limit_y))                                  \
    {                                                                         \
        pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y;                      \
        motion_y = pos_y - decoder->v_offset;                                 \
    }                                                                         \
                                                                              \
    xy_half = ((pos_y & 1) << 1) | (pos_x & 1);                               \
    offset = (pos_x >> 1) + ((pos_y op) + src_field) * decoder->stride;       \
                                                                              \
    table[xy_half] (decoder->dest[0] + dest_field * decoder->stride +         \
                    decoder->offset, ref[0] + offset,                         \
                    2 * decoder->stride, 8);                                  \
                                                                              \
    if (MPEG2_COLOR)                                                          \
    {                                                                         \
    table[xy_half] (decoder->dest[1] + dest_field * decoder->stride +         \
                    decoder->offset, ref[1] + offset,                         \
                    2 * decoder->stride, 8);                                  \
    table[xy_half] (decoder->dest[2] + dest_field * decoder->stride +         \
                    decoder->offset, ref[2] + offset,                         \
                    2 * decoder->stride, 8);                                  \
    }

#define MOTION_DMV_444(table, ref, motion_x, motion_y) \
    pos_x = 2 * decoder->offset + motion_x;                                   \
    pos_y = decoder->v_offset + motion_y;                                     \
                                                                              \
    if (unlikely (pos_x > decoder->limit_x))                                  \
    {                                                                         \
        pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x;                      \
        motion_x = pos_x - 2 * decoder->offset;                               \
    }                                                                         \
                                                                              \
    if (unlikely (pos_y > decoder->limit_y))                                  \
    {                                                                         \
        pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y;                      \
        motion_y = pos_y - decoder->v_offset;                                 \
    }                                                                         \
                                                                              \
    xy_half = ((pos_y & 1) << 1) | (pos_x & 1);                               \
    offset = (pos_x >> 1) + (pos_y & ~1) * decoder->stride;                   \
                                                                              \
    table[xy_half] (decoder->dest[0] + decoder->offset,                       \
                    ref[0] + offset, 2 * decoder->stride, 8);                 \
    table[xy_half] (decoder->dest[0] + decoder->stride + decoder->offset,     \
                    ref[0] + decoder->stride + offset,                        \
                    2 * decoder->stride, 8);                                  \
                                                                              \
    if (MPEG2_COLOR)                                                          \
    {                                                                         \
    table[xy_half] (decoder->dest[1] + decoder->offset,                       \
                    ref[1] + offset, 2 * decoder->stride, 8);                 \
    table[xy_half] (decoder->dest[1] + decoder->stride + decoder->offset,     \
                    ref[1] + decoder->stride + offset,                        \
                    2 * decoder->stride, 8);                                  \
    table[xy_half] (decoder->dest[2] + decoder->offset,                       \
                    ref[2] + offset, 2 * decoder->stride, 8);                 \
    table[xy_half] (decoder->dest[2] + decoder->stride + decoder->offset,     \
                    ref[2] + decoder->stride + offset,                        \
                    2 * decoder->stride, 8);                                  \
    }

#define MOTION_ZERO_444(table, ref) \
    offset = decoder->offset + decoder->v_offset * decoder->stride;           \
                                                                              \
    table[0] (decoder->dest[0] + decoder->offset,                             \
              ref[0] + offset, decoder->stride, 16);                          \
                                                                              \
    if (MPEG2_COLOR)                                                          \
    {                                                                         \
    table[4] (decoder->dest[1] + decoder->offset,                             \
              ref[1] + offset, decoder->stride, 16);                          \
    table[4] (decoder->dest[2] + decoder->offset,                             \
              ref[2] + offset, decoder->stride, 16);                          \
    }

#define bit_buf (decoder->bitstream_buf)
#define bits (decoder->bitstream_bits)
#define bit_ptr (decoder->bitstream_ptr)

static void motion_mp1 (mpeg2_decoder_t * const decoder,
                        motion_t * const motion,
                        mpeg2_mc_fct * const * const table)
{
    int motion_x, motion_y;
    unsigned int pos_x, pos_y, xy_half, offset;

    NEEDBITS (bit_buf, bits, bit_ptr);
    motion_x = motion->pmv[0][0] +
                (get_motion_delta (decoder,
                                   motion->f_code[0]) << motion->f_code[1]);
    motion_x = bound_motion_vector (motion_x,
                                    motion->f_code[0] + motion->f_code[1]);
    motion->pmv[0][0] = motion_x;

    NEEDBITS (bit_buf, bits, bit_ptr);
    motion_y = motion->pmv[0][1] +
                (get_motion_delta (decoder,
                                   motion->f_code[0]) << motion->f_code[1]);
    motion_y = bound_motion_vector (motion_y,
                                    motion->f_code[0] + motion->f_code[1]);
    motion->pmv[0][1] = motion_y;

    MOTION_420 (table, motion->ref[0], motion_x, motion_y, 16, 0);
}

#define MOTION_FUNCTIONS(FORMAT, MOTION, MOTION_FIELD, \
                         MOTION_DMV, MOTION_ZERO)      \
                                                                              \
static void motion_fr_frame_##FORMAT (mpeg2_decoder_t * const decoder,        \
                                      motion_t * const motion,                \
                                      mpeg2_mc_fct * const * const table)     \
{                                                                             \
    int motion_x, motion_y;                                                   \
    unsigned int pos_x, pos_y, xy_half, offset;                               \
                                                                              \
    NEEDBITS (bit_buf, bits, bit_ptr);                                        \
    motion_x = motion->pmv[0][0] + get_motion_delta (decoder,                 \
                                                     motion->f_code[0]);      \
    motion_x = bound_motion_vector (motion_x, motion->f_code[0]);             \
    motion->pmv[1][0] = motion->pmv[0][0] = motion_x;                         \
                                                                              \
    NEEDBITS (bit_buf, bits, bit_ptr);                                        \
    motion_y = motion->pmv[0][1] + get_motion_delta (decoder,                 \
                                                     motion->f_code[1]);      \
    motion_y = bound_motion_vector (motion_y, motion->f_code[1]);             \
    motion->pmv[1][1] = motion->pmv[0][1] = motion_y;                         \
                                                                              \
    MOTION (table, motion->ref[0], motion_x, motion_y, 16, 0);                \
}                                                                             \
                                                                              \
static void motion_fr_field_##FORMAT (mpeg2_decoder_t * const decoder,        \
                                      motion_t * const motion,                \
                                      mpeg2_mc_fct * const * const table)     \
{                                                                             \
    int motion_x, motion_y, field;                                            \
    unsigned int pos_x, pos_y, xy_half, offset;                               \
                                                                              \
    NEEDBITS (bit_buf, bits, bit_ptr);                                        \
    field = UBITS (bit_buf, 1);                                               \
    DUMPBITS (bit_buf, bits, 1);                                              \
                                                                              \
    motion_x = motion->pmv[0][0] + get_motion_delta (decoder,                 \
                                                     motion->f_code[0]);      \
    motion_x = bound_motion_vector (motion_x, motion->f_code[0]);             \
    motion->pmv[0][0] = motion_x;                                             \
                                                                              \
    NEEDBITS (bit_buf, bits, bit_ptr);                                        \
    motion_y = ((motion->pmv[0][1] >> 1) +                                    \
                get_motion_delta (decoder, motion->f_code[1]));               \
    /* motion_y = bound_motion_vector (motion_y, motion->f_code[1]); */       \
    motion->pmv[0][1] = motion_y << 1;                                        \
                                                                              \
    MOTION_FIELD (table, motion->ref[0], motion_x, motion_y, 0, & ~1, field); \
                                                                              \
    NEEDBITS (bit_buf, bits, bit_ptr);                                        \
    field = UBITS (bit_buf, 1);                                               \
    DUMPBITS (bit_buf, bits, 1);                                              \
                                                                              \
    motion_x = motion->pmv[1][0] + get_motion_delta (decoder,                 \
                                                     motion->f_code[0]);      \
    motion_x = bound_motion_vector (motion_x, motion->f_code[0]);             \
    motion->pmv[1][0] = motion_x;                                             \
                                                                              \
    NEEDBITS (bit_buf, bits, bit_ptr);                                        \
    motion_y = ((motion->pmv[1][1] >> 1) +                                    \
                get_motion_delta (decoder, motion->f_code[1]));               \
    /* motion_y = bound_motion_vector (motion_y, motion->f_code[1]); */       \
    motion->pmv[1][1] = motion_y << 1;                                        \
                                                                              \
    MOTION_FIELD (table, motion->ref[0], motion_x, motion_y, 1, & ~1, field); \
}                                                                             \
                                                                              \
static void motion_fr_dmv_##FORMAT (mpeg2_decoder_t * const decoder,          \
                                    motion_t * const motion,                  \
                                    mpeg2_mc_fct * const * const table)       \
{                                                                             \
    int motion_x, motion_y, dmv_x, dmv_y, m, other_x, other_y;                \
    unsigned int pos_x, pos_y, xy_half, offset;                               \
                                                                              \
    (void)table;                                                              \
    NEEDBITS (bit_buf, bits, bit_ptr);                                        \
    motion_x = motion->pmv[0][0] + get_motion_delta (decoder,                 \
                                                     motion->f_code[0]);      \
    motion_x = bound_motion_vector (motion_x, motion->f_code[0]);             \
    motion->pmv[1][0] = motion->pmv[0][0] = motion_x;                         \
    NEEDBITS (bit_buf, bits, bit_ptr);                                        \
    dmv_x = get_dmv (decoder);                                                \
                                                                              \
    motion_y = ((motion->pmv[0][1] >> 1) +                                    \
                get_motion_delta (decoder, motion->f_code[1]));               \
    /* motion_y = bound_motion_vector (motion_y, motion->f_code[1]); */       \
    motion->pmv[1][1] = motion->pmv[0][1] = motion_y << 1;                    \
    dmv_y = get_dmv (decoder);                                                \
                                                                              \
    m = decoder->top_field_first ? 1 : 3;                                     \
    other_x = ((motion_x * m + (motion_x > 0)) >> 1) + dmv_x;                 \
    other_y = ((motion_y * m + (motion_y > 0)) >> 1) + dmv_y - 1;             \
    MOTION_FIELD (mpeg2_mc.put, motion->ref[0], other_x, other_y, 0, | 1, 0); \
                                                                              \
    m = decoder->top_field_first ? 3 : 1;                                     \
    other_x = ((motion_x * m + (motion_x > 0)) >> 1) + dmv_x;                 \
    other_y = ((motion_y * m + (motion_y > 0)) >> 1) + dmv_y + 1;             \
    MOTION_FIELD (mpeg2_mc.put, motion->ref[0], other_x, other_y, 1, & ~1, 0);\
                                                                              \
    MOTION_DMV (mpeg2_mc.avg, motion->ref[0], motion_x, motion_y);            \
}                                                                             \
                                                                              \
static void motion_reuse_##FORMAT (mpeg2_decoder_t * const decoder,           \
                                   motion_t * const motion,                   \
                                   mpeg2_mc_fct * const * const table)        \
{                                                                             \
    int motion_x, motion_y;                                                   \
    unsigned int pos_x, pos_y, xy_half, offset;                               \
                                                                              \
    motion_x = motion->pmv[0][0];                                             \
    motion_y = motion->pmv[0][1];                                             \
                                                                              \
    MOTION (table, motion->ref[0], motion_x, motion_y, 16, 0);                \
}                                                                             \
                                                                              \
static void motion_zero_##FORMAT (mpeg2_decoder_t * const decoder,            \
                                  motion_t * const motion,                    \
                                  mpeg2_mc_fct * const * const table)         \
{                                                                             \
    unsigned int offset;                                                      \
                                                                              \
    motion->pmv[0][0] = motion->pmv[0][1] = 0;                                \
    motion->pmv[1][0] = motion->pmv[1][1] = 0;                                \
                                                                              \
    MOTION_ZERO (table, motion->ref[0]);                                      \
}                                                                             \
                                                                              \
static void motion_fi_field_##FORMAT (mpeg2_decoder_t * const decoder,        \
                                      motion_t * const motion,                \
                                      mpeg2_mc_fct * const * const table)     \
{                                                                             \
    int motion_x, motion_y;                                                   \
    uint8_t ** ref_field;                                                     \
    unsigned int pos_x, pos_y, xy_half, offset;                               \
                                                                              \
    NEEDBITS (bit_buf, bits, bit_ptr);                                        \
    ref_field = motion->ref2[UBITS (bit_buf, 1)];                             \
    DUMPBITS (bit_buf, bits, 1);                                              \
                                                                              \
    motion_x = motion->pmv[0][0] + get_motion_delta (decoder,                 \
                                                     motion->f_code[0]);      \
    motion_x = bound_motion_vector (motion_x, motion->f_code[0]);             \
    motion->pmv[1][0] = motion->pmv[0][0] = motion_x;                         \
                                                                              \
    NEEDBITS (bit_buf, bits, bit_ptr);                                        \
    motion_y = motion->pmv[0][1] + get_motion_delta (decoder,                 \
                                                     motion->f_code[1]);      \
    motion_y = bound_motion_vector (motion_y, motion->f_code[1]);             \
    motion->pmv[1][1] = motion->pmv[0][1] = motion_y;                         \
                                                                              \
    MOTION (table, ref_field, motion_x, motion_y, 16, 0);                     \
}                                                                             \
                                                                              \
static void motion_fi_16x8_##FORMAT (mpeg2_decoder_t * const decoder,         \
                                     motion_t * const motion,                 \
                                     mpeg2_mc_fct * const * const table)      \
{                                                                             \
    int motion_x, motion_y;                                                   \
    uint8_t ** ref_field;                                                     \
    unsigned int pos_x, pos_y, xy_half, offset;                               \
                                                                              \
    NEEDBITS (bit_buf, bits, bit_ptr);                                        \
    ref_field = motion->ref2[UBITS (bit_buf, 1)];                             \
    DUMPBITS (bit_buf, bits, 1);                                              \
                                                                              \
    motion_x = motion->pmv[0][0] + get_motion_delta (decoder,                 \
                                                     motion->f_code[0]);      \
    motion_x = bound_motion_vector (motion_x, motion->f_code[0]);             \
    motion->pmv[0][0] = motion_x;                                             \
                                                                              \
    NEEDBITS (bit_buf, bits, bit_ptr);                                        \
    motion_y = motion->pmv[0][1] + get_motion_delta (decoder,                 \
                                                     motion->f_code[1]);      \
    motion_y = bound_motion_vector (motion_y, motion->f_code[1]);             \
    motion->pmv[0][1] = motion_y;                                             \
                                                                              \
    MOTION (table, ref_field, motion_x, motion_y, 8, 0);                      \
                                                                              \
    NEEDBITS (bit_buf, bits, bit_ptr);                                        \
    ref_field = motion->ref2[UBITS (bit_buf, 1)];                             \
    DUMPBITS (bit_buf, bits, 1);                                              \
                                                                              \
    motion_x = motion->pmv[1][0] + get_motion_delta (decoder,                 \
                                                     motion->f_code[0]);      \
    motion_x = bound_motion_vector (motion_x, motion->f_code[0]);             \
    motion->pmv[1][0] = motion_x;                                             \
                                                                              \
    NEEDBITS (bit_buf, bits, bit_ptr);                                        \
    motion_y = motion->pmv[1][1] + get_motion_delta (decoder,                 \
                                                     motion->f_code[1]);      \
    motion_y = bound_motion_vector (motion_y, motion->f_code[1]);             \
    motion->pmv[1][1] = motion_y;                                             \
                                                                              \
    MOTION (table, ref_field, motion_x, motion_y, 8, 8);                      \
}                                                                             \
                                                                              \
static void motion_fi_dmv_##FORMAT (mpeg2_decoder_t * const decoder,          \
                                    motion_t * const motion,                  \
                                    mpeg2_mc_fct * const * const table)       \
{                                                                             \
    int motion_x, motion_y, other_x, other_y;                                 \
    unsigned int pos_x, pos_y, xy_half, offset;                               \
                                                                              \
    (void)table;                                                              \
    NEEDBITS (bit_buf, bits, bit_ptr);                                        \
    motion_x = motion->pmv[0][0] + get_motion_delta (decoder,                 \
                                                     motion->f_code[0]);      \
    motion_x = bound_motion_vector (motion_x, motion->f_code[0]);             \
    motion->pmv[1][0] = motion->pmv[0][0] = motion_x;                         \
    NEEDBITS (bit_buf, bits, bit_ptr);                                        \
    other_x = ((motion_x + (motion_x > 0)) >> 1) + get_dmv (decoder);         \
                                                                              \
    motion_y = motion->pmv[0][1] + get_motion_delta (decoder,                 \
                                                     motion->f_code[1]);      \
    motion_y = bound_motion_vector (motion_y, motion->f_code[1]);             \
    motion->pmv[1][1] = motion->pmv[0][1] = motion_y;                         \
    other_y = (((motion_y + (motion_y > 0)) >> 1) + get_dmv (decoder) +       \
               decoder->dmv_offset);                                          \
                                                                              \
    MOTION (mpeg2_mc.put, motion->ref[0], motion_x, motion_y, 16, 0);         \
    MOTION (mpeg2_mc.avg, motion->ref[1], other_x, other_y, 16, 0);           \
}                                                                             \

MOTION_FUNCTIONS (420, MOTION_420, MOTION_FIELD_420, MOTION_DMV_420,
                  MOTION_ZERO_420)
MOTION_FUNCTIONS (422, MOTION_422, MOTION_FIELD_422, MOTION_DMV_422,
                  MOTION_ZERO_422)
MOTION_FUNCTIONS (444, MOTION_444, MOTION_FIELD_444, MOTION_DMV_444,
                  MOTION_ZERO_444)

/* like motion_frame, but parsing without actual motion compensation */
static void motion_fr_conceal (mpeg2_decoder_t * const decoder)
{
    int tmp;

    NEEDBITS (bit_buf, bits, bit_ptr);
    tmp = (decoder->f_motion.pmv[0][0] +
           get_motion_delta (decoder, decoder->f_motion.f_code[0]));
    tmp = bound_motion_vector (tmp, decoder->f_motion.f_code[0]);
    decoder->f_motion.pmv[1][0] = decoder->f_motion.pmv[0][0] = tmp;

    NEEDBITS (bit_buf, bits, bit_ptr);
    tmp = (decoder->f_motion.pmv[0][1] +
           get_motion_delta (decoder, decoder->f_motion.f_code[1]));
    tmp = bound_motion_vector (tmp, decoder->f_motion.f_code[1]);
    decoder->f_motion.pmv[1][1] = decoder->f_motion.pmv[0][1] = tmp;

    DUMPBITS (bit_buf, bits, 1); /* remove marker_bit */
}

static void motion_fi_conceal (mpeg2_decoder_t * const decoder)
{
    int tmp;

    NEEDBITS (bit_buf, bits, bit_ptr);
    DUMPBITS (bit_buf, bits, 1); /* remove field_select */

    tmp = decoder->f_motion.pmv[0][0] +
            get_motion_delta (decoder, decoder->f_motion.f_code[0]);
    tmp = bound_motion_vector (tmp, decoder->f_motion.f_code[0]);

    decoder->f_motion.pmv[1][0] =
    decoder->f_motion.pmv[0][0] = tmp;

    NEEDBITS (bit_buf, bits, bit_ptr);

    tmp = (decoder->f_motion.pmv[0][1] +
            get_motion_delta (decoder, decoder->f_motion.f_code[1]));
    tmp = bound_motion_vector (tmp, decoder->f_motion.f_code[1]);

    decoder->f_motion.pmv[1][1] =
    decoder->f_motion.pmv[0][1] = tmp;

    DUMPBITS (bit_buf, bits, 1); /* remove marker_bit */
}

#undef bit_buf
#undef bits
#undef bit_ptr

#define MOTION_CALL(routine, direction) \
do {                                                         \
    if ((direction) & MACROBLOCK_MOTION_FORWARD)             \
        routine (decoder, &decoder->f_motion, mpeg2_mc.put); \
                                                             \
    if ((direction) & MACROBLOCK_MOTION_BACKWARD)            \
    {                                                        \
        routine (decoder, &decoder->b_motion,                \
                 ((direction) & MACROBLOCK_MOTION_FORWARD ?  \
                    mpeg2_mc.avg : mpeg2_mc.put));           \
    }                                                        \
} while (0)

#define NEXT_MACROBLOCK \
do {                                                                  \
    decoder->offset += 16;                                            \
                                                                      \
    if (decoder->offset == decoder->width)                            \
    {                                                                 \
        do { /* just so we can use the break statement */             \
            if (decoder->convert)                                     \
            {                                                         \
                decoder->convert (decoder->convert_id, decoder->dest, \
                                  decoder->v_offset);                 \
                if (decoder->coding_type == B_TYPE)                   \
                    break;                                            \
            }                                                         \
                                                                      \
            decoder->dest[0] += decoder->slice_stride;                \
            if (MPEG2_COLOR)                                          \
            {                                                         \
            decoder->dest[1] += decoder->slice_uv_stride;             \
            decoder->dest[2] += decoder->slice_uv_stride;             \
            }                                                         \
        } while (0);                                                  \
                                                                      \
        decoder->v_offset += 16;                                      \
                                                                      \
        if (decoder->v_offset > decoder->limit_y)                     \
            return;                                                   \
                                                                      \
        decoder->offset = 0;                                          \
    }                                                                 \
} while (0)

void mpeg2_init_fbuf (mpeg2_decoder_t * decoder,
                      uint8_t * current_fbuf[MPEG2_COMPONENTS],
                      uint8_t * forward_fbuf[MPEG2_COMPONENTS],
                      uint8_t * backward_fbuf[MPEG2_COMPONENTS])
{
    int offset, stride, height, bottom_field;

    stride = decoder->stride_frame;
    bottom_field = (decoder->picture_structure == BOTTOM_FIELD);
    offset = bottom_field ? stride : 0;
    height = decoder->height;

    decoder->picture_dest[0] = current_fbuf[0] + offset;
#if MPEG2_COLOR
    decoder->picture_dest[1] = current_fbuf[1] + (offset >> 1);
    decoder->picture_dest[2] = current_fbuf[2] + (offset >> 1);
#endif

    decoder->f_motion.ref[0][0] = forward_fbuf[0] + offset;
#if MPEG2_COLOR
    decoder->f_motion.ref[0][1] = forward_fbuf[1] + (offset >> 1);
    decoder->f_motion.ref[0][2] = forward_fbuf[2] + (offset >> 1);
#endif

    decoder->b_motion.ref[0][0] = backward_fbuf[0] + offset;
#if MPEG2_COLOR
    decoder->b_motion.ref[0][1] = backward_fbuf[1] + (offset >> 1);
    decoder->b_motion.ref[0][2] = backward_fbuf[2] + (offset >> 1);
#endif

    if (decoder->picture_structure != FRAME_PICTURE)
    {
        decoder->dmv_offset = bottom_field ? 1 : -1;
        decoder->f_motion.ref2[0] = decoder->f_motion.ref[bottom_field];
        decoder->f_motion.ref2[1] = decoder->f_motion.ref[!bottom_field];
        decoder->b_motion.ref2[0] = decoder->b_motion.ref[bottom_field];
        decoder->b_motion.ref2[1] = decoder->b_motion.ref[!bottom_field];
        offset = stride - offset;

        if (decoder->second_field && (decoder->coding_type != B_TYPE))
            forward_fbuf = current_fbuf;

        decoder->f_motion.ref[1][0] = forward_fbuf[0] + offset;
#if MPEG2_COLOR
        decoder->f_motion.ref[1][1] = forward_fbuf[1] + (offset >> 1);
        decoder->f_motion.ref[1][2] = forward_fbuf[2] + (offset >> 1);
#endif
        decoder->b_motion.ref[1][0] = backward_fbuf[0] + offset;
#if MPEG2_COLOR
        decoder->b_motion.ref[1][1] = backward_fbuf[1] + (offset >> 1);
        decoder->b_motion.ref[1][2] = backward_fbuf[2] + (offset >> 1);
#endif
        stride <<= 1;
        height >>= 1;
    }

    decoder->stride = stride;
    decoder->slice_stride = 16 * stride;
#if MPEG2_COLOR
    decoder->uv_stride = stride >> 1;
    decoder->slice_uv_stride =
        decoder->slice_stride >> (2 - decoder->chroma_format);
#endif
    decoder->limit_x = 2 * decoder->width - 32;
    decoder->limit_y_16 = 2 * height - 32;
    decoder->limit_y_8 = 2 * height - 16;
    decoder->limit_y = height - 16;

    if (decoder->mpeg1)
    {
        decoder->motion_parser[0] = motion_zero_420;
        decoder->motion_parser[MC_FRAME] = motion_mp1;
        decoder->motion_parser[4] = motion_reuse_420;
    }
    else if (decoder->picture_structure == FRAME_PICTURE)
    {
        if (decoder->chroma_format == 0)
        {
            decoder->motion_parser[0] = motion_zero_420;
            decoder->motion_parser[MC_FIELD] = motion_fr_field_420;
            decoder->motion_parser[MC_FRAME] = motion_fr_frame_420;
            decoder->motion_parser[MC_DMV] = motion_fr_dmv_420;
            decoder->motion_parser[4] = motion_reuse_420;
        }
        else if (decoder->chroma_format == 1)
        {
            decoder->motion_parser[0] = motion_zero_422;
            decoder->motion_parser[MC_FIELD] = motion_fr_field_422;
            decoder->motion_parser[MC_FRAME] = motion_fr_frame_422;
            decoder->motion_parser[MC_DMV] = motion_fr_dmv_422;
            decoder->motion_parser[4] = motion_reuse_422;
        }
        else
        {
            decoder->motion_parser[0] = motion_zero_444;
            decoder->motion_parser[MC_FIELD] = motion_fr_field_444;
            decoder->motion_parser[MC_FRAME] = motion_fr_frame_444;
            decoder->motion_parser[MC_DMV] = motion_fr_dmv_444;
            decoder->motion_parser[4] = motion_reuse_444;
        }
    }
    else
    {
        if (decoder->chroma_format == 0)
        {
            decoder->motion_parser[0] = motion_zero_420;
            decoder->motion_parser[MC_FIELD] = motion_fi_field_420;
            decoder->motion_parser[MC_16X8] = motion_fi_16x8_420;
            decoder->motion_parser[MC_DMV] = motion_fi_dmv_420;
            decoder->motion_parser[4] = motion_reuse_420;
        }
        else if (decoder->chroma_format == 1)
        {
            decoder->motion_parser[0] = motion_zero_422;
            decoder->motion_parser[MC_FIELD] = motion_fi_field_422;
            decoder->motion_parser[MC_16X8] = motion_fi_16x8_422;
            decoder->motion_parser[MC_DMV] = motion_fi_dmv_422;
            decoder->motion_parser[4] = motion_reuse_422;
        }
        else
        {
            decoder->motion_parser[0] = motion_zero_444;
            decoder->motion_parser[MC_FIELD] = motion_fi_field_444;
            decoder->motion_parser[MC_16X8] = motion_fi_16x8_444;
            decoder->motion_parser[MC_DMV] = motion_fi_dmv_444;
            decoder->motion_parser[4] = motion_reuse_444;
        }
    }
}

static inline int slice_init (mpeg2_decoder_t * const decoder, int code)
{
#define bit_buf (decoder->bitstream_buf)
#define bits (decoder->bitstream_bits)
#define bit_ptr (decoder->bitstream_ptr)

    int offset;
    const MBAtab * mba;

#if MPEG2_COLOR
    decoder->dc_dct_pred[0] = decoder->dc_dct_pred[1] =
        decoder->dc_dct_pred[2] = 16384;
#else
    decoder->dc_dct_pred[0] = 16384;
#endif

    decoder->f_motion.pmv[0][0] = decoder->f_motion.pmv[0][1] = 0;
    decoder->f_motion.pmv[1][0] = decoder->f_motion.pmv[1][1] = 0;
    decoder->b_motion.pmv[0][0] = decoder->b_motion.pmv[0][1] = 0;
    decoder->b_motion.pmv[1][0] = decoder->b_motion.pmv[1][1] = 0;

    if (decoder->vertical_position_extension)
    {
        code += UBITS (bit_buf, 3) << 7;
        DUMPBITS (bit_buf, bits, 3);
    }

    decoder->v_offset = (code - 1) * 16;
    offset = 0;

    if (!(decoder->convert) || decoder->coding_type != B_TYPE)
    {
        offset = (code - 1) * decoder->slice_stride;
    }

    decoder->dest[0] = decoder->picture_dest[0] + offset;
#if MPEG2_COLOR
    offset >>= (2 - decoder->chroma_format);
    decoder->dest[1] = decoder->picture_dest[1] + offset;
    decoder->dest[2] = decoder->picture_dest[2] + offset;
#endif

    get_quantizer_scale (decoder);

    /* ignore intra_slice and all the extra data */
    while (bit_buf & 0x80000000)
    {
        DUMPBITS (bit_buf, bits, 9);
        NEEDBITS (bit_buf, bits, bit_ptr);
    }

    /* decode initial macroblock address increment */
    offset = 0;
    while (1)
    {
        if (bit_buf >= 0x08000000)
        {
            mba = MBA_5 + (UBITS (bit_buf, 6) - 2);
            break;
        }
        else if (bit_buf >= 0x01800000)
        {
            mba = MBA_11 + (UBITS (bit_buf, 12) - 24);
            break;
        }
        else
        {
            switch (UBITS (bit_buf, 12))
            {
            case 8:                /* macroblock_escape */
                offset += 33;
                DUMPBITS (bit_buf, bits, 11);
                NEEDBITS (bit_buf, bits, bit_ptr);
                continue;
            case 15:        /* macroblock_stuffing (MPEG1 only) */
                bit_buf &= 0xfffff;
                DUMPBITS (bit_buf, bits, 11);
                NEEDBITS (bit_buf, bits, bit_ptr);
                continue;
            default:        /* error */
                return 1;
            }
        }
    }

    DUMPBITS (bit_buf, bits, mba->len + 1);
    decoder->offset = (offset + mba->mba) << 4;

    while (decoder->offset - decoder->width >= 0)
    {
        decoder->offset -= decoder->width;

        if (!(decoder->convert) || decoder->coding_type != B_TYPE)
        {
            decoder->dest[0] += decoder->slice_stride;
#if MPEG2_COLOR
            decoder->dest[1] += decoder->slice_uv_stride;
            decoder->dest[2] += decoder->slice_uv_stride;
#endif
        }

        decoder->v_offset += 16;
    }

    if (decoder->v_offset > decoder->limit_y)
        return 1;

    return 0;

#undef bit_buf
#undef bits
#undef bit_ptr
}

void mpeg2_slice (mpeg2_decoder_t * const decoder, const int code,
                  const uint8_t * const buffer)
{
#define bit_buf (decoder->bitstream_buf)
#define bits (decoder->bitstream_bits)
#define bit_ptr (decoder->bitstream_ptr)

    bitstream_init (decoder, buffer);

    if (slice_init (decoder, code))
        return;

    while (1)
    {
        int macroblock_modes;
        int mba_inc;
        const MBAtab * mba;

        NEEDBITS (bit_buf, bits, bit_ptr);

        macroblock_modes = get_macroblock_modes (decoder);

        /* maybe integrate MACROBLOCK_QUANT test into get_macroblock_modes ? */
        if (macroblock_modes & MACROBLOCK_QUANT)
            get_quantizer_scale (decoder);

        if (macroblock_modes & MACROBLOCK_INTRA)
        {
            int DCT_offset, DCT_stride;
            int offset;
            uint8_t * dest_y;

            if (decoder->concealment_motion_vectors)
            {
                if (decoder->picture_structure == FRAME_PICTURE)
                    motion_fr_conceal (decoder);
                else
                    motion_fi_conceal (decoder);
            }
            else
            {
                decoder->f_motion.pmv[0][0] = decoder->f_motion.pmv[0][1] = 0;
                decoder->f_motion.pmv[1][0] = decoder->f_motion.pmv[1][1] = 0;
                decoder->b_motion.pmv[0][0] = decoder->b_motion.pmv[0][1] = 0;
                decoder->b_motion.pmv[1][0] = decoder->b_motion.pmv[1][1] = 0;
            }

            if (macroblock_modes & DCT_TYPE_INTERLACED)
            {
                DCT_offset = decoder->stride;
                DCT_stride = decoder->stride * 2;
            }
            else
            {
                DCT_offset = decoder->stride * 8;
                DCT_stride = decoder->stride;
            }

            offset = decoder->offset;
            dest_y = decoder->dest[0] + offset;
            slice_intra_DCT (decoder, 0, dest_y, DCT_stride);
            slice_intra_DCT (decoder, 0, dest_y + 8, DCT_stride);
            slice_intra_DCT (decoder, 0, dest_y + DCT_offset, DCT_stride);
            slice_intra_DCT (decoder, 0, dest_y + DCT_offset + 8, DCT_stride);

#if MPEG2_COLOR
            if (likely (decoder->chroma_format == 0))
            {
                slice_intra_DCT (decoder, 1, decoder->dest[1] + (offset >> 1),
                                 decoder->uv_stride);
                slice_intra_DCT (decoder, 2, decoder->dest[2] + (offset >> 1),
                                 decoder->uv_stride);

                if (decoder->coding_type == D_TYPE)
                {
                    NEEDBITS (bit_buf, bits, bit_ptr);
                    DUMPBITS (bit_buf, bits, 1);
                }
            }
            else if (likely (decoder->chroma_format == 1))
            {
                uint8_t * dest_u = decoder->dest[1] + (offset >> 1);
                uint8_t * dest_v = decoder->dest[2] + (offset >> 1);

                DCT_stride >>= 1;
                DCT_offset >>= 1;

                slice_intra_DCT (decoder, 1, dest_u, DCT_stride);
                slice_intra_DCT (decoder, 2, dest_v, DCT_stride);
                slice_intra_DCT (decoder, 1, dest_u + DCT_offset, DCT_stride);
                slice_intra_DCT (decoder, 2, dest_v + DCT_offset, DCT_stride);
            }
            else
            {
                uint8_t * dest_u = decoder->dest[1] + offset;
                uint8_t * dest_v = decoder->dest[2] + offset;

                slice_intra_DCT (decoder, 1, dest_u, DCT_stride);
                slice_intra_DCT (decoder, 2, dest_v, DCT_stride);
                slice_intra_DCT (decoder, 1, dest_u + DCT_offset, DCT_stride);
                slice_intra_DCT (decoder, 2, dest_v + DCT_offset, DCT_stride);
                slice_intra_DCT (decoder, 1, dest_u + 8, DCT_stride);
                slice_intra_DCT (decoder, 2, dest_v + 8, DCT_stride);
                slice_intra_DCT (decoder, 1, dest_u + DCT_offset + 8,
                                 DCT_stride);
                slice_intra_DCT (decoder, 2, dest_v + DCT_offset + 8,
                                 DCT_stride);
            }
#else
            skip_chroma_intra(decoder);
#endif /* MPEG2_COLOR */
        }
        else
        {
            motion_parser_t * parser;

            parser =
                decoder->motion_parser[macroblock_modes >> MOTION_TYPE_SHIFT];
            MOTION_CALL (parser, macroblock_modes);

            if (macroblock_modes & MACROBLOCK_PATTERN)
            {
                int coded_block_pattern;
                int DCT_offset, DCT_stride;

                if (macroblock_modes & DCT_TYPE_INTERLACED)
                {
                    DCT_offset = decoder->stride;
                    DCT_stride = decoder->stride * 2;
                }
                else
                {
                    DCT_offset = decoder->stride * 8;
                    DCT_stride = decoder->stride;
                }

                coded_block_pattern = get_coded_block_pattern (decoder);

                if (likely (decoder->chroma_format == 0))
                {
                    int offset = decoder->offset;
                    uint8_t * dest_y = decoder->dest[0] + offset;

                    if (coded_block_pattern & 1)
                        slice_non_intra_DCT (decoder, 0, dest_y, DCT_stride);

                    if (coded_block_pattern & 2)
                        slice_non_intra_DCT (decoder, 0, dest_y + 8,
                                             DCT_stride);

                    if (coded_block_pattern & 4)
                        slice_non_intra_DCT (decoder, 0, dest_y + DCT_offset,
                                             DCT_stride);

                    if (coded_block_pattern & 8)
                        slice_non_intra_DCT (decoder, 0,
                                             dest_y + DCT_offset + 8,
                                             DCT_stride);
#if MPEG2_COLOR
                    if (coded_block_pattern & 16)
                        slice_non_intra_DCT (decoder, 1,
                                             decoder->dest[1] + (offset >> 1),
                                             decoder->uv_stride);

                    if (coded_block_pattern & 32)
                        slice_non_intra_DCT (decoder, 2,
                                             decoder->dest[2] + (offset >> 1),
                                             decoder->uv_stride);
#endif /* MPEG2_COLOR */
                }
                else if (likely (decoder->chroma_format == 1))
                {
                    int offset;
                    uint8_t * dest_y;

                    coded_block_pattern |= bit_buf & (3 << 30);
                    DUMPBITS (bit_buf, bits, 2);

                    offset = decoder->offset;
                    dest_y = decoder->dest[0] + offset;

                    if (coded_block_pattern & 1)
                        slice_non_intra_DCT (decoder, 0, dest_y, DCT_stride);

                    if (coded_block_pattern & 2)
                        slice_non_intra_DCT (decoder, 0, dest_y + 8,
                                             DCT_stride);

                    if (coded_block_pattern & 4)
                        slice_non_intra_DCT (decoder, 0, dest_y + DCT_offset,
                                             DCT_stride);

                    if (coded_block_pattern & 8)
                        slice_non_intra_DCT (decoder, 0,
                                             dest_y + DCT_offset + 8,
                                             DCT_stride);
#if MPEG2_COLOR
                    DCT_stride >>= 1;
                    DCT_offset = (DCT_offset + offset) >> 1;

                    if (coded_block_pattern & 16)
                        slice_non_intra_DCT (decoder, 1,
                                             decoder->dest[1] + (offset >> 1),
                                             DCT_stride);

                    if (coded_block_pattern & 32)
                        slice_non_intra_DCT (decoder, 2,
                                             decoder->dest[2] + (offset >> 1),
                                             DCT_stride);

                    if (coded_block_pattern & (2 << 30))
                        slice_non_intra_DCT (decoder, 1,
                                             decoder->dest[1] + DCT_offset,
                                             DCT_stride);

                    if (coded_block_pattern & (1 << 30))
                        slice_non_intra_DCT (decoder, 2,
                                             decoder->dest[2] + DCT_offset,
                                             DCT_stride);
#endif /* MPEG2_COLOR */
                }
                else
                {
                    int offset = decoder->offset;
                    uint8_t * dest_y = decoder->dest[0] + offset;
#if MPEG2_COLOR
                    uint8_t * dest_u = decoder->dest[1] + offset;
                    uint8_t * dest_v = decoder->dest[2] + offset;
#endif
                    coded_block_pattern |= bit_buf & (63 << 26);
                    DUMPBITS (bit_buf, bits, 6);

                    if (coded_block_pattern & 1)
                        slice_non_intra_DCT (decoder, 0, dest_y, DCT_stride);

                    if (coded_block_pattern & 2)
                        slice_non_intra_DCT (decoder, 0, dest_y + 8,
                                             DCT_stride);

                    if (coded_block_pattern & 4)
                        slice_non_intra_DCT (decoder, 0, dest_y + DCT_offset,
                                             DCT_stride);

                    if (coded_block_pattern & 8)
                        slice_non_intra_DCT (decoder, 0,
                                             dest_y + DCT_offset + 8,
                                             DCT_stride);
#if MPEG2_COLOR
                    if (coded_block_pattern & 16)
                        slice_non_intra_DCT (decoder, 1, dest_u, DCT_stride);

                    if (coded_block_pattern & 32)
                        slice_non_intra_DCT (decoder, 2, dest_v, DCT_stride);

                    if (coded_block_pattern & (32 << 26))
                        slice_non_intra_DCT (decoder, 1, dest_u + DCT_offset,
                                             DCT_stride);

                    if (coded_block_pattern & (16 << 26))
                        slice_non_intra_DCT (decoder, 2, dest_v + DCT_offset,
                                             DCT_stride);

                    if (coded_block_pattern & (8 << 26))
                        slice_non_intra_DCT (decoder, 1, dest_u + 8,
                                             DCT_stride);

                    if (coded_block_pattern & (4 << 26))
                        slice_non_intra_DCT (decoder, 2, dest_v + 8,
                                             DCT_stride);

                    if (coded_block_pattern & (2 << 26))
                        slice_non_intra_DCT (decoder, 1,
                                             dest_u + DCT_offset + 8,
                                             DCT_stride);

                    if (coded_block_pattern & (1 << 26))
                        slice_non_intra_DCT (decoder, 2,
                                             dest_v + DCT_offset + 8,
                                             DCT_stride);
#endif /* MPEG2_COLOR */
                }
#if !MPEG2_COLOR
                skip_chroma_non_intra(decoder, coded_block_pattern);
#endif
            }

#if MPEG2_COLOR
            decoder->dc_dct_pred[0] = decoder->dc_dct_pred[1] =
                decoder->dc_dct_pred[2] = 16384;
#else
            decoder->dc_dct_pred[0] = 16384;
#endif
        }

        NEXT_MACROBLOCK;

        NEEDBITS (bit_buf, bits, bit_ptr);
        mba_inc = 0;

        while (1)
        {
            if (bit_buf >= 0x10000000)
            {
                mba = MBA_5 + (UBITS (bit_buf, 5) - 2);
                break;
            }
            else if (bit_buf >= 0x03000000)
            {
                mba = MBA_11 + (UBITS (bit_buf, 11) - 24);
                break;
            }
            else
            {
                switch (UBITS (bit_buf, 11))
                {
                case 8:                /* macroblock_escape */
                    mba_inc += 33;
                    /* pass through */
                case 15:        /* macroblock_stuffing (MPEG1 only) */
                    DUMPBITS (bit_buf, bits, 11);
                    NEEDBITS (bit_buf, bits, bit_ptr);
                    continue;
                default:        /* end of slice, or error */
                    return;
                }
            }
        }

        DUMPBITS (bit_buf, bits, mba->len);
        mba_inc += mba->mba;

        if (mba_inc)
        {
#if MPEG2_COLOR
            decoder->dc_dct_pred[0] = decoder->dc_dct_pred[1] =
                decoder->dc_dct_pred[2] = 16384;
#else
            decoder->dc_dct_pred[0] = 16384;
#endif
            if (decoder->coding_type == P_TYPE)
            {
                do
                {
                    MOTION_CALL (decoder->motion_parser[0],
                                 MACROBLOCK_MOTION_FORWARD);
                    NEXT_MACROBLOCK;
                }
                while (--mba_inc);
            }
            else
            {
                do
                {
                    MOTION_CALL (decoder->motion_parser[4], macroblock_modes);
                    NEXT_MACROBLOCK;
                }
                while (--mba_inc);
            }
        }
    }

#undef bit_buf
#undef bits
#undef bit_ptr
}