FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpeg12.c
Date: 2024-12-12 01:08:13
Exec Total Coverage
Lines: 97 102 95.1%
Functions: 5 5 100.0%
Branches: 28 32 87.5%

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