FFmpeg coverage


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