Line | Branch | Exec | Source |
---|---|---|---|
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 | |||
30 | #include "libavutil/attributes.h" | ||
31 | #include "libavutil/avassert.h" | ||
32 | #include "libavutil/thread.h" | ||
33 | |||
34 | #include "avcodec.h" | ||
35 | #include "mpegvideo.h" | ||
36 | #include "mpeg12.h" | ||
37 | #include "mpeg12codecs.h" | ||
38 | #include "mpeg12data.h" | ||
39 | #include "mpeg12dec.h" | ||
40 | #include "rl.h" | ||
41 | #include "startcode.h" | ||
42 | |||
43 | static const uint8_t table_mb_ptype[7][2] = { | ||
44 | { 3, 5 }, // 0x01 MB_INTRA | ||
45 | { 1, 2 }, // 0x02 MB_PAT | ||
46 | { 1, 3 }, // 0x08 MB_FOR | ||
47 | { 1, 1 }, // 0x0A MB_FOR|MB_PAT | ||
48 | { 1, 6 }, // 0x11 MB_QUANT|MB_INTRA | ||
49 | { 1, 5 }, // 0x12 MB_QUANT|MB_PAT | ||
50 | { 2, 5 }, // 0x1A MB_QUANT|MB_FOR|MB_PAT | ||
51 | }; | ||
52 | |||
53 | static const uint8_t table_mb_btype[11][2] = { | ||
54 | { 3, 5 }, // 0x01 MB_INTRA | ||
55 | { 2, 3 }, // 0x04 MB_BACK | ||
56 | { 3, 3 }, // 0x06 MB_BACK|MB_PAT | ||
57 | { 2, 4 }, // 0x08 MB_FOR | ||
58 | { 3, 4 }, // 0x0A MB_FOR|MB_PAT | ||
59 | { 2, 2 }, // 0x0C MB_FOR|MB_BACK | ||
60 | { 3, 2 }, // 0x0E MB_FOR|MB_BACK|MB_PAT | ||
61 | { 1, 6 }, // 0x11 MB_QUANT|MB_INTRA | ||
62 | { 2, 6 }, // 0x16 MB_QUANT|MB_BACK|MB_PAT | ||
63 | { 3, 6 }, // 0x1A MB_QUANT|MB_FOR|MB_PAT | ||
64 | { 2, 5 }, // 0x1E MB_QUANT|MB_FOR|MB_BACK|MB_PAT | ||
65 | }; | ||
66 | |||
67 | 285 | av_cold void ff_init_2d_vlc_rl(const uint16_t table_vlc[][2], RL_VLC_ELEM rl_vlc[], | |
68 | const int8_t table_run[], const uint8_t table_level[], | ||
69 | int n, unsigned static_size, int flags) | ||
70 | { | ||
71 | int i; | ||
72 | 285 | VLCElem table[680] = { 0 }; | |
73 | 285 | VLC vlc = { .table = table, .table_allocated = static_size }; | |
74 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 285 times.
|
285 | av_assert0(static_size <= FF_ARRAY_ELEMS(table)); |
75 | 285 | vlc_init(&vlc, TEX_VLC_BITS, n + 2, &table_vlc[0][1], 4, 2, &table_vlc[0][0], 4, 2, VLC_INIT_USE_STATIC | flags); | |
76 | |||
77 |
2/2✓ Branch 0 taken 192912 times.
✓ Branch 1 taken 285 times.
|
193197 | for (i = 0; i < vlc.table_size; i++) { |
78 | 192912 | int code = vlc.table[i].sym; | |
79 | 192912 | int len = vlc.table[i].len; | |
80 | int level, run; | ||
81 | |||
82 |
2/2✓ Branch 0 taken 5930 times.
✓ Branch 1 taken 186982 times.
|
192912 | if (len == 0) { // illegal code |
83 | 5930 | run = 65; | |
84 | 5930 | level = MAX_LEVEL; | |
85 |
2/2✓ Branch 0 taken 1836 times.
✓ Branch 1 taken 185146 times.
|
186982 | } else if (len<0) { //more bits needed |
86 | 1836 | run = 0; | |
87 | 1836 | level = code; | |
88 | } else { | ||
89 |
2/2✓ Branch 0 taken 2280 times.
✓ Branch 1 taken 182866 times.
|
185146 | if (code == n) { //esc |
90 | 2280 | run = 65; | |
91 | 2280 | level = 0; | |
92 |
2/2✓ Branch 0 taken 22272 times.
✓ Branch 1 taken 160594 times.
|
182866 | } else if (code == n + 1) { //eob |
93 | 22272 | run = 0; | |
94 | 22272 | level = 127; | |
95 | } else { | ||
96 | 160594 | run = table_run [code] + 1; | |
97 | 160594 | level = table_level[code]; | |
98 | } | ||
99 | } | ||
100 | 192912 | rl_vlc[i].len = len; | |
101 | 192912 | rl_vlc[i].level = level; | |
102 | 192912 | rl_vlc[i].run = run; | |
103 | } | ||
104 | 285 | } | |
105 | |||
106 | 420382 | void ff_mpeg1_clean_buffers(MpegEncContext *s) | |
107 | { | ||
108 | 420382 | s->last_dc[0] = 1 << (7 + s->intra_dc_precision); | |
109 | 420382 | s->last_dc[1] = s->last_dc[0]; | |
110 | 420382 | s->last_dc[2] = s->last_dc[0]; | |
111 | 420382 | memset(s->last_mv, 0, sizeof(s->last_mv)); | |
112 | 420382 | } | |
113 | |||
114 | |||
115 | /******************************************/ | ||
116 | /* decoding */ | ||
117 | |||
118 | VLCElem ff_mv_vlc[266]; | ||
119 | |||
120 | VLCElem ff_dc_lum_vlc[512]; | ||
121 | VLCElem ff_dc_chroma_vlc[514]; | ||
122 | |||
123 | VLCElem ff_mbincr_vlc[538]; | ||
124 | VLCElem ff_mb_ptype_vlc[64]; | ||
125 | VLCElem ff_mb_btype_vlc[64]; | ||
126 | VLCElem ff_mb_pat_vlc[512]; | ||
127 | |||
128 | RL_VLC_ELEM ff_mpeg1_rl_vlc[680]; | ||
129 | RL_VLC_ELEM ff_mpeg2_rl_vlc[674]; | ||
130 | |||
131 | 137 | static av_cold void mpeg12_init_vlcs(void) | |
132 | { | ||
133 | 137 | VLC_INIT_STATIC_TABLE(ff_dc_lum_vlc, DC_VLC_BITS, 12, | |
134 | ff_mpeg12_vlc_dc_lum_bits, 1, 1, | ||
135 | ff_mpeg12_vlc_dc_lum_code, 2, 2, 0); | ||
136 | 137 | VLC_INIT_STATIC_TABLE(ff_dc_chroma_vlc, DC_VLC_BITS, 12, | |
137 | ff_mpeg12_vlc_dc_chroma_bits, 1, 1, | ||
138 | ff_mpeg12_vlc_dc_chroma_code, 2, 2, 0); | ||
139 | 137 | VLC_INIT_STATIC_TABLE(ff_mv_vlc, MV_VLC_BITS, 17, | |
140 | &ff_mpeg12_mbMotionVectorTable[0][1], 2, 1, | ||
141 | &ff_mpeg12_mbMotionVectorTable[0][0], 2, 1, 0); | ||
142 | 137 | VLC_INIT_STATIC_TABLE(ff_mbincr_vlc, MBINCR_VLC_BITS, 36, | |
143 | &ff_mpeg12_mbAddrIncrTable[0][1], 2, 1, | ||
144 | &ff_mpeg12_mbAddrIncrTable[0][0], 2, 1, 0); | ||
145 | 137 | VLC_INIT_STATIC_TABLE(ff_mb_pat_vlc, MB_PAT_VLC_BITS, 64, | |
146 | &ff_mpeg12_mbPatTable[0][1], 2, 1, | ||
147 | &ff_mpeg12_mbPatTable[0][0], 2, 1, 0); | ||
148 | |||
149 | 137 | VLC_INIT_STATIC_TABLE(ff_mb_ptype_vlc, MB_PTYPE_VLC_BITS, 7, | |
150 | &table_mb_ptype[0][1], 2, 1, | ||
151 | &table_mb_ptype[0][0], 2, 1, 0); | ||
152 | 137 | VLC_INIT_STATIC_TABLE(ff_mb_btype_vlc, MB_BTYPE_VLC_BITS, 11, | |
153 | &table_mb_btype[0][1], 2, 1, | ||
154 | &table_mb_btype[0][0], 2, 1, 0); | ||
155 | |||
156 | 137 | ff_init_2d_vlc_rl(ff_mpeg1_vlc_table, ff_mpeg1_rl_vlc, ff_mpeg12_run, | |
157 | ff_mpeg12_level, MPEG12_RL_NB_ELEMS, | ||
158 | FF_ARRAY_ELEMS(ff_mpeg1_rl_vlc), 0); | ||
159 | 137 | ff_init_2d_vlc_rl(ff_mpeg2_vlc_table, ff_mpeg2_rl_vlc, ff_mpeg12_run, | |
160 | ff_mpeg12_level, MPEG12_RL_NB_ELEMS, | ||
161 | FF_ARRAY_ELEMS(ff_mpeg2_rl_vlc), 0); | ||
162 | 137 | } | |
163 | |||
164 | 299 | av_cold void ff_mpeg12_init_vlcs(void) | |
165 | { | ||
166 | static AVOnce init_static_once = AV_ONCE_INIT; | ||
167 | 299 | ff_thread_once(&init_static_once, mpeg12_init_vlcs); | |
168 | 299 | } | |
169 | |||
170 | #define MAX_INDEX (64 - 1) | ||
171 | |||
172 | 176424 | int ff_mpeg1_decode_block_intra(GetBitContext *gb, | |
173 | const uint16_t *quant_matrix, | ||
174 | const uint8_t *scantable, int last_dc[3], | ||
175 | int16_t *block, int index, int qscale) | ||
176 | { | ||
177 | 176424 | int dc, diff, i = 0, component; | |
178 | |||
179 | /* DC coefficient */ | ||
180 | 176424 | component = index <= 3 ? 0 : index - 4 + 1; | |
181 | |||
182 | 176424 | diff = decode_dc(gb, component); | |
183 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 176424 times.
|
176424 | if (diff >= 0xffff) |
184 | ✗ | return AVERROR_INVALIDDATA; | |
185 | |||
186 | 176424 | dc = last_dc[component]; | |
187 | 176424 | dc += diff; | |
188 | 176424 | last_dc[component] = dc; | |
189 | |||
190 | 176424 | block[0] = dc * quant_matrix[0]; | |
191 | |||
192 | { | ||
193 | 176424 | OPEN_READER(re, gb); | |
194 | 176424 | UPDATE_CACHE(re, gb); | |
195 |
2/2✓ Branch 0 taken 36367 times.
✓ Branch 1 taken 140057 times.
|
176424 | if (((int32_t)GET_CACHE(re, gb)) <= (int32_t)0xBFFFFFFF) |
196 | 36367 | goto end; | |
197 | |||
198 | /* now quantify & encode AC coefficients */ | ||
199 | 1334003 | while (1) { | |
200 | int level, run, j; | ||
201 | |||
202 |
2/2✓ Branch 1 taken 139306 times.
✓ Branch 2 taken 1334754 times.
|
1474060 | GET_RL_VLC(level, run, re, gb, ff_mpeg1_rl_vlc, |
203 | TEX_VLC_BITS, 2, 0); | ||
204 | |||
205 |
2/2✓ Branch 0 taken 1468795 times.
✓ Branch 1 taken 5265 times.
|
1474060 | if (level != 0) { |
206 | 1468795 | i += run; | |
207 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1468795 times.
|
1468795 | if (i > MAX_INDEX) |
208 | ✗ | break; | |
209 | |||
210 | 1468795 | j = scantable[i]; | |
211 | 1468795 | level = (level * qscale * quant_matrix[j]) >> 4; | |
212 | 1468795 | level = (level - 1) | 1; | |
213 | 1468795 | level = (level ^ SHOW_SBITS(re, gb, 1)) - | |
214 | 1468795 | SHOW_SBITS(re, gb, 1); | |
215 | 1468795 | SKIP_BITS(re, gb, 1); | |
216 | } else { | ||
217 | /* escape */ | ||
218 | 5265 | run = SHOW_UBITS(re, gb, 6) + 1; | |
219 | 5265 | LAST_SKIP_BITS(re, gb, 6); | |
220 | 5265 | UPDATE_CACHE(re, gb); | |
221 | 5265 | level = SHOW_SBITS(re, gb, 8); | |
222 | 5265 | SKIP_BITS(re, gb, 8); | |
223 | |||
224 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5265 times.
|
5265 | if (level == -128) { |
225 | ✗ | level = SHOW_UBITS(re, gb, 8) - 256; | |
226 | ✗ | SKIP_BITS(re, gb, 8); | |
227 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5265 times.
|
5265 | } else if (level == 0) { |
228 | ✗ | level = SHOW_UBITS(re, gb, 8); | |
229 | ✗ | SKIP_BITS(re, gb, 8); | |
230 | } | ||
231 | |||
232 | 5265 | i += run; | |
233 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5265 times.
|
5265 | if (i > MAX_INDEX) |
234 | ✗ | break; | |
235 | |||
236 | 5265 | j = scantable[i]; | |
237 |
2/2✓ Branch 0 taken 2964 times.
✓ Branch 1 taken 2301 times.
|
5265 | if (level < 0) { |
238 | 2964 | level = -level; | |
239 | 2964 | level = (level * qscale * quant_matrix[j]) >> 4; | |
240 | 2964 | level = (level - 1) | 1; | |
241 | 2964 | level = -level; | |
242 | } else { | ||
243 | 2301 | level = (level * qscale * quant_matrix[j]) >> 4; | |
244 | 2301 | level = (level - 1) | 1; | |
245 | } | ||
246 | } | ||
247 | |||
248 | 1474060 | block[j] = level; | |
249 |
2/2✓ Branch 0 taken 140057 times.
✓ Branch 1 taken 1334003 times.
|
1474060 | if (((int32_t)GET_CACHE(re, gb)) <= (int32_t)0xBFFFFFFF) |
250 | 140057 | break; | |
251 | |||
252 | 1334003 | UPDATE_CACHE(re, gb); | |
253 | } | ||
254 | 176424 | end: | |
255 | 176424 | LAST_SKIP_BITS(re, gb, 2); | |
256 | 176424 | CLOSE_READER(re, gb); | |
257 | } | ||
258 | |||
259 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 176424 times.
|
176424 | if (i > MAX_INDEX) |
260 | ✗ | i = AVERROR_INVALIDDATA; | |
261 | |||
262 | 176424 | return i; | |
263 | } | ||
264 |