1 /*
2 * MPEG-1/2 decoder
3 * Copyright (c) 2000, 2001 Fabrice Bellard
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 /**
24 * @file
25 * MPEG-1/2 decoder
26 */
27
28 #define UNCHECKED_BITSTREAM_READER 1
29
33
45
47
49 { 3, 5 }, // 0x01 MB_INTRA
50 { 1, 2 }, // 0x02 MB_PAT
51 { 1, 3 }, // 0x08 MB_FOR
52 { 1, 1 }, // 0x0A MB_FOR|MB_PAT
53 { 1, 6 }, // 0x11 MB_QUANT|MB_INTRA
54 { 1, 5 }, // 0x12 MB_QUANT|MB_PAT
55 { 2, 5 }, // 0x1A MB_QUANT|MB_FOR|MB_PAT
56 };
57
59 { 3, 5 }, // 0x01 MB_INTRA
60 { 2, 3 }, // 0x04 MB_BACK
61 { 3, 3 }, // 0x06 MB_BACK|MB_PAT
62 { 2, 4 }, // 0x08 MB_FOR
63 { 3, 4 }, // 0x0A MB_FOR|MB_PAT
64 { 2, 2 }, // 0x0C MB_FOR|MB_BACK
65 { 3, 2 }, // 0x0E MB_FOR|MB_BACK|MB_PAT
66 { 1, 6 }, // 0x11 MB_QUANT|MB_INTRA
67 { 2, 6 }, // 0x16 MB_QUANT|MB_BACK|MB_PAT
68 { 3, 6 }, // 0x1A MB_QUANT|MB_FOR|MB_PAT
69 { 2, 5 }, // 0x1E MB_QUANT|MB_FOR|MB_BACK|MB_PAT
70 };
71
72 #define INIT_2D_VLC_RL(rl, static_size)\
73 {\
74 static RL_VLC_ELEM rl_vlc_table[static_size];\
75 INIT_VLC_STATIC(&rl.vlc, TEX_VLC_BITS, rl.n + 2,\
76 &rl.table_vlc[0][1], 4, 2,\
77 &rl.table_vlc[0][0], 4, 2, static_size);\
78 \
79 rl.rl_vlc[0] = rl_vlc_table;\
80 init_2d_vlc_rl(&rl);\
81 }
82
84 {
85 int i;
86
91
92 if (len == 0) { // illegal code
93 run = 65;
95 } else if (len<0) { //more bits needed
96 run = 0;
97 level = code;
98 } else {
99 if (code == rl->
n) {
//esc
100 run = 65;
101 level = 0;
102 }
else if (code == rl->
n+1) {
//eob
103 run = 0;
104 level = 127;
105 } else {
108 }
109 }
113 }
114 }
115
117 {
118
121
122 }
123
125 {
130 }
131
132
133 /******************************************/
134 /* decoding */
135
137
140
145
147 {
148 static int done = 0;
149
150 if (!done) {
151 done = 1;
152
168
177
180 }
181 }
182
183 /**
184 * Find the end of the current frame in the bitstream.
185 * @return the position of the first byte of the next frame, or -1
186 */
188 {
189 int i;
191
192 /* EOF considered as end of frame */
193 if (buf_size == 0)
194 return 0;
195
196 /*
197 0 frame start -> 1/4
198 1 first_SEQEXT -> 0/2
199 2 first field start -> 3/0
200 3 second_SEQEXT -> 2/0
201 4 searching end
202 */
203
204 for (i = 0; i < buf_size; i++) {
210 if ((buf[i] & 3) == 3)
212 else
214 }
215 state++;
216 } else {
219 i++;
221 }
225 return i+1;
226 }
235 return i - 3;
236 }
237 }
240 }
241 }
242 }
245 }
246