FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpeg12.c
Date: 2026-01-23 19:11:46
Exec Total Coverage
Lines: 89 94 94.7%
Functions: 4 4 100.0%
Branches: 27 30 90.0%

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