summaryrefslogtreecommitdiff
path: root/apps/codecs/libmusepack/mpc_bits_reader.c
blob: 58b3f37e08670e857497caeef14a4987d77b222b (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
/*
  Copyright (c) 2007-2009, The Musepack Development Team
  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are
  met:

  * Redistributions of source code must retain the above copyright
  notice, this list of conditions and the following disclaimer.

  * Redistributions in binary form must reproduce the above
  copyright notice, this list of conditions and the following
  disclaimer in the documentation and/or other materials provided
  with the distribution.

  * Neither the name of the The Musepack Development Team nor the
  names of its contributors may be used to endorse or promote
  products derived from this software without specific prior
  written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "mpcdec.h"
#include "internal.h"
#include "huffman.h"
#include "mpc_bits_reader.h"

const mpc_uint32_t Cnk[MAX_ENUM / 2][MAX_ENUM] ICONST_ATTR_MPC_BITSREADER =
{
    {0, 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},
    {0, 0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105, 120, 136, 153, 171, 190, 210, 231, 253, 276, 300, 325, 351, 378, 406, 435, 465},
    {0, 0, 0, 1, 4, 10, 20, 35, 56, 84, 120, 165, 220, 286, 364, 455, 560, 680, 816, 969, 1140, 1330, 1540, 1771, 2024, 2300, 2600, 2925, 3276, 3654, 4060, 4495},
    {0, 0, 0, 0, 1, 5, 15, 35, 70, 126, 210, 330, 495, 715, 1001, 1365, 1820, 2380, 3060, 3876, 4845, 5985, 7315, 8855, 10626, 12650, 14950, 17550, 20475, 23751, 27405, 31465},
    {0, 0, 0, 0, 0, 1, 6, 21, 56, 126, 252, 462, 792, 1287, 2002, 3003, 4368, 6188, 8568, 11628, 15504, 20349, 26334, 33649, 42504, 53130, 65780, 80730, 98280, 118755, 142506, 169911},
    {0, 0, 0, 0, 0, 0, 1, 7, 28, 84, 210, 462, 924, 1716, 3003, 5005, 8008, 12376, 18564, 27132, 38760, 54264, 74613, 100947, 134596, 177100, 230230, 296010, 376740, 475020, 593775, 736281},
    {0, 0, 0, 0, 0, 0, 0, 1, 8, 36, 120, 330, 792, 1716, 3432, 6435, 11440, 19448, 31824, 50388, 77520, 116280, 170544, 245157, 346104, 480700, 657800, 888030, 1184040, 1560780, 2035800, 2629575},
    {0, 0, 0, 0, 0, 0, 0, 0, 1, 9, 45, 165, 495, 1287, 3003, 6435, 12870, 24310, 43758, 75582, 125970, 203490, 319770, 490314, 735471, 1081575, 1562275, 2220075, 3108105, 4292145, 5852925, 7888725},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 10, 55, 220, 715, 2002, 5005, 11440, 24310, 48620, 92378, 167960, 293930, 497420, 817190, 1307504, 2042975, 3124550, 4686825, 6906900, 10015005, 14307150, 20160075},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 11, 66, 286, 1001, 3003, 8008, 19448, 43758, 92378, 184756, 352716, 646646, 1144066, 1961256, 3268760, 5311735, 8436285, 13123110, 20030010, 30045015, 44352165},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 12, 78, 364, 1365, 4368, 12376, 31824, 75582, 167960, 352716, 705432, 1352078, 2496144, 4457400, 7726160, 13037895, 21474180, 34597290, 54627300, 84672315},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 13, 91, 455, 1820, 6188, 18564, 50388, 125970, 293930, 646646, 1352078, 2704156, 5200300, 9657700, 17383860, 30421755, 51895935, 86493225, 141120525},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 14, 105, 560, 2380, 8568, 27132, 77520, 203490, 497420, 1144066, 2496144, 5200300, 10400600, 20058300, 37442160, 67863915, 119759850, 206253075},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 15, 120, 680, 3060, 11628, 38760, 116280, 319770, 817190, 1961256, 4457400, 9657700, 20058300, 40116600, 77558760, 145422675, 265182525},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 16, 136, 816, 3876, 15504, 54264, 170544, 490314, 1307504, 3268760, 7726160, 17383860, 37442160, 77558760, 155117520, 300540195},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 17, 153, 969, 4845, 20349, 74613, 245157, 735471, 2042975, 5311735, 13037895, 30421755, 67863915, 145422675, 300540195}
};

const mpc_uint8_t Cnk_len[MAX_ENUM / 2][MAX_ENUM] ICONST_ATTR_MPC_BITSREADER =
{
    {0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5},
    {0, 0, 2, 3, 4, 4, 5, 5, 6, 6, 6, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9},
    {0, 0, 0, 2, 4, 5, 6, 6, 7, 7, 8, 8, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 13, 13},
    {0, 0, 0, 0, 3, 4, 6, 7, 7, 8, 9, 9, 10, 10, 11, 11, 12, 12, 12, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 15, 16},
    {0, 0, 0, 0, 0, 3, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 13, 14, 14, 14, 15, 15, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18},
    {0, 0, 0, 0, 0, 0, 3, 5, 7, 8, 9, 10, 11, 12, 13, 13, 14, 15, 15, 16, 16, 17, 17, 18, 18, 18, 19, 19, 19, 20, 20, 20},
    {0, 0, 0, 0, 0, 0, 0, 3, 6, 7, 9, 10, 11, 12, 13, 14, 15, 15, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 21, 22, 22},
    {0, 0, 0, 0, 0, 0, 0, 0, 4, 6, 8, 9, 11, 12, 13, 14, 15, 16, 17, 17, 18, 19, 19, 20, 21, 21, 22, 22, 23, 23, 23, 24},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 6, 8, 10, 11, 13, 14, 15, 16, 17, 18, 19, 19, 20, 21, 21, 22, 23, 23, 24, 24, 25, 25},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 7, 9, 10, 12, 13, 15, 16, 17, 18, 19, 20, 21, 21, 22, 23, 24, 24, 25, 25, 26, 26},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 7, 9, 11, 13, 14, 15, 17, 18, 19, 20, 21, 22, 23, 23, 24, 25, 26, 26, 27, 27},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 7, 9, 11, 13, 15, 16, 17, 19, 20, 21, 22, 23, 24, 25, 25, 26, 27, 28, 28},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 7, 10, 12, 14, 15, 17, 18, 19, 21, 22, 23, 24, 25, 26, 27, 27, 28, 29},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 7, 10, 12, 14, 16, 17, 19, 20, 21, 23, 24, 25, 26, 27, 28, 28, 29},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 8, 10, 12, 14, 16, 18, 19, 21, 22, 23, 25, 26, 27, 28, 29, 30},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 8, 10, 13, 15, 17, 18, 20, 21, 23, 24, 25, 27, 28, 29, 30}

};

const mpc_uint32_t Cnk_lost[MAX_ENUM / 2][MAX_ENUM] ICONST_ATTR_MPC_BITSREADER =
{
    {0, 0, 1, 0, 3, 2, 1, 0, 7, 6, 5, 4, 3, 2, 1, 0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0},
    {0, 0, 1, 2, 6, 1, 11, 4, 28, 19, 9, 62, 50, 37, 23, 8, 120, 103, 85, 66, 46, 25, 3, 236, 212, 187, 161, 134, 106, 77, 47, 16},
    {0, 0, 0, 0, 6, 12, 29, 8, 44, 8, 91, 36, 226, 148, 57, 464, 344, 208, 55, 908, 718, 508, 277, 24, 1796, 1496, 1171, 820, 442, 36, 3697, 3232},
    {0, 0, 0, 0, 3, 1, 29, 58, 2, 46, 182, 17, 309, 23, 683, 228, 1716, 1036, 220, 3347, 2207, 877, 7529, 5758, 3734, 1434, 15218, 12293, 9017, 5363, 1303, 29576},
    {0, 0, 0, 0, 0, 2, 11, 8, 2, 4, 50, 232, 761, 46, 1093, 3824, 2004, 7816, 4756, 880, 12419, 6434, 31887, 23032, 12406, 65292, 50342, 32792, 12317, 119638, 92233, 60768},
    {0, 0, 0, 0, 0, 0, 1, 4, 44, 46, 50, 100, 332, 1093, 3187, 184, 4008, 14204, 5636, 26776, 11272, 56459, 30125, 127548, 85044, 31914, 228278, 147548, 49268, 454801, 312295, 142384},
    {0, 0, 0, 0, 0, 0, 0, 0, 28, 8, 182, 232, 332, 664, 1757, 4944, 13320, 944, 15148, 53552, 14792, 91600, 16987, 178184, 43588, 390776, 160546, 913112, 536372, 61352, 1564729, 828448},
    {0, 0, 0, 0, 0, 0, 0, 0, 7, 19, 91, 17, 761, 1093, 1757, 3514, 8458, 21778, 55490, 5102, 58654, 204518, 33974, 313105, 1015577, 534877, 1974229, 1086199, 4096463, 2535683, 499883, 6258916},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 9, 36, 309, 46, 3187, 4944, 8458, 16916, 38694, 94184, 230358, 26868, 231386, 789648, 54177, 1069754, 3701783, 1481708, 6762211, 2470066, 13394357, 5505632},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 62, 226, 23, 1093, 184, 13320, 21778, 38694, 77388, 171572, 401930, 953086, 135896, 925544, 3076873, 8340931, 3654106, 13524422, 3509417, 22756699, 2596624},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 50, 148, 683, 3824, 4008, 944, 55490, 94184, 171572, 343144, 745074, 1698160, 3931208, 662448, 3739321, 12080252, 32511574, 12481564, 49545413, 5193248},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 37, 57, 228, 2004, 14204, 15148, 5102, 230358, 401930, 745074, 1490148, 3188308, 7119516, 16170572, 3132677, 15212929, 47724503, 127314931, 42642616},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 23, 464, 1716, 7816, 5636, 53552, 58654, 26868, 953086, 1698160, 3188308, 6376616, 13496132, 29666704, 66353813, 14457878, 62182381, 189497312},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 8, 344, 1036, 4756, 26776, 14792, 204518, 231386, 135896, 3931208, 7119516, 13496132, 26992264, 56658968, 123012781, 3252931, 65435312},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 120, 208, 220, 880, 11272, 91600, 33974, 789648, 925544, 662448, 16170572, 29666704, 56658968, 113317936, 236330717, 508019104},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 103, 55, 3347, 12419, 56459, 16987, 313105, 54177, 3076873, 3739321, 3132677, 66353813, 123012781, 236330717}
};

static const mpc_uint8_t log2[32] ICONST_ATTR_MPC_BITSREADER =
{ 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6};

static const mpc_uint8_t log2_lost[32] ICONST_ATTR_MPC_BITSREADER =
{ 0, 1, 0, 3, 2, 1, 0, 7, 6, 5, 4, 3, 2, 1, 0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 31};

mpc_int32_t mpc_bits_golomb_dec(mpc_bits_reader * r, const mpc_uint_t k)
{
    unsigned int l = 0;
    unsigned int code = r->buff[0] & ((1 << r->count) - 1);

    while( code == 0 ) {
        l += r->count;
        r->buff++;
        code = r->buff[0];
        r->count = 8;
    }

    while( ((1 << (r->count - 1)) & code) == 0 ) {
        l++;
        r->count--;
    }
    r->count--;

    while( r->count < k ) {
        r->buff++;
        r->count += 8;
        code = (code << 8) | r->buff[0];
    }

    r->count -= k;

    return (l << k) | ((code >> r->count) & ((1 << k) - 1));
}

mpc_uint32_t mpc_bits_log_dec(mpc_bits_reader * r, mpc_uint_t max)
{
    mpc_uint32_t value = 0;
    if (log2[max - 1] > 1)
        value = mpc_bits_read(r, log2[max - 1] - 1);
    if (value >= log2_lost[max - 1])
        value = ((value << 1) | mpc_bits_read(r, 1)) - log2_lost[max - 1];
    return value;
}

unsigned int mpc_bits_get_size(mpc_bits_reader * r, mpc_uint64_t * p_size)
{
    unsigned char tmp;
    mpc_uint64_t size = 0;
    unsigned int ret = 0;

    do {
        tmp = mpc_bits_read(r, 8);
        size = (size << 7) | (tmp & 0x7F);
        ret++;
    } while((tmp & 0x80));

    *p_size = size;
    return ret;
}

int mpc_bits_get_block(mpc_bits_reader * r, mpc_block * p_block)
{
    int size = 2;

    p_block->size = 0;
    p_block->key[0] = mpc_bits_read(r, 8);
    p_block->key[1] = mpc_bits_read(r, 8);

    size += mpc_bits_get_size(r, &(p_block->size));

    if (p_block->size >= (mpc_uint64_t)size) // check if the block size doesn't conflict with the header size
        p_block->size -= (mpc_uint64_t)size;

    return size;
}



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 }