FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpeg12.c
Date: 2024-03-28 14:59:00
Exec Total Coverage
Lines: 98 104 94.2%
Functions: 5 5 100.0%
Branches: 29 34 85.3%

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 287 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 287 VLCElem table[680] = { 0 };
73 287 VLC vlc = { .table = table, .table_allocated = static_size };
74
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 287 times.
287 av_assert0(static_size <= FF_ARRAY_ELEMS(table));
75 287 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 194266 times.
✓ Branch 1 taken 287 times.
194553 for (i = 0; i < vlc.table_size; i++) {
78 194266 int code = vlc.table[i].sym;
79 194266 int len = vlc.table[i].len;
80 int level, run;
81
82
2/2
✓ Branch 0 taken 5972 times.
✓ Branch 1 taken 188294 times.
194266 if (len == 0) { // illegal code
83 5972 run = 65;
84 5972 level = MAX_LEVEL;
85
2/2
✓ Branch 0 taken 1849 times.
✓ Branch 1 taken 186445 times.
188294 } else if (len<0) { //more bits needed
86 1849 run = 0;
87 1849 level = code;
88 } else {
89
2/2
✓ Branch 0 taken 2296 times.
✓ Branch 1 taken 184149 times.
186445 if (code == n) { //esc
90 2296 run = 65;
91 2296 level = 0;
92
2/2
✓ Branch 0 taken 22432 times.
✓ Branch 1 taken 161717 times.
184149 } else if (code == n + 1) { //eob
93 22432 run = 0;
94 22432 level = 127;
95 } else {
96 161717 run = table_run [code] + 1;
97 161717 level = table_level[code];
98 }
99 }
100 194266 rl_vlc[i].len = len;
101 194266 rl_vlc[i].level = level;
102 194266 rl_vlc[i].run = run;
103 }
104 287 }
105
106 422690 void ff_mpeg1_clean_buffers(MpegEncContext *s)
107 {
108 422690 s->last_dc[0] = 1 << (7 + s->intra_dc_precision);
109 422690 s->last_dc[1] = s->last_dc[0];
110 422690 s->last_dc[2] = s->last_dc[0];
111 422690 memset(s->last_mv, 0, sizeof(s->last_mv));
112 422690 }
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 138 static av_cold void mpeg12_init_vlcs(void)
132 {
133 138 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 138 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 138 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 138 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 138 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 138 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 138 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 138 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 138 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 138 }
163
164 300 av_cold void ff_mpeg12_init_vlcs(void)
165 {
166 static AVOnce init_static_once = AV_ONCE_INIT;
167 300 ff_thread_once(&init_static_once, mpeg12_init_vlcs);
168 300 }
169
170 #define MAX_INDEX (64 - 1)
171
172 176781 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 176781 int dc, diff, i = 0, component;
178
179 /* DC coefficient */
180 176781 component = index <= 3 ? 0 : index - 4 + 1;
181
182 176781 diff = decode_dc(gb, component);
183
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 176781 times.
176781 if (diff >= 0xffff)
184 return AVERROR_INVALIDDATA;
185
186 176781 dc = last_dc[component];
187 176781 dc += diff;
188 176781 last_dc[component] = dc;
189
190 176781 block[0] = dc * quant_matrix[0];
191
192 {
193 176781 OPEN_READER(re, gb);
194 176781 UPDATE_CACHE(re, gb);
195
2/2
✓ Branch 0 taken 36412 times.
✓ Branch 1 taken 140369 times.
176781 if (((int32_t)GET_CACHE(re, gb)) <= (int32_t)0xBFFFFFFF)
196 36412 goto end;
197
198 /* now quantify & encode AC coefficients */
199 1336749 while (1) {
200 int level, run, j;
201
202
2/2
✓ Branch 1 taken 139671 times.
✓ Branch 2 taken 1337447 times.
1477118 GET_RL_VLC(level, run, re, gb, ff_mpeg1_rl_vlc,
203 TEX_VLC_BITS, 2, 0);
204
205
2/2
✓ Branch 0 taken 1471817 times.
✓ Branch 1 taken 5301 times.
1477118 if (level != 0) {
206 1471817 i += run;
207
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1471816 times.
1471817 if (i > MAX_INDEX)
208 1 break;
209
210 1471816 j = scantable[i];
211 1471816 level = (level * qscale * quant_matrix[j]) >> 4;
212 1471816 level = (level - 1) | 1;
213 1471816 level = (level ^ SHOW_SBITS(re, gb, 1)) -
214 1471816 SHOW_SBITS(re, gb, 1);
215 1471816 SKIP_BITS(re, gb, 1);
216 } else {
217 /* escape */
218 5301 run = SHOW_UBITS(re, gb, 6) + 1;
219 5301 LAST_SKIP_BITS(re, gb, 6);
220 5301 UPDATE_CACHE(re, gb);
221 5301 level = SHOW_SBITS(re, gb, 8);
222 5301 SKIP_BITS(re, gb, 8);
223
224
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5301 times.
5301 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 5301 times.
5301 } else if (level == 0) {
228 level = SHOW_UBITS(re, gb, 8);
229 SKIP_BITS(re, gb, 8);
230 }
231
232 5301 i += run;
233
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5301 times.
5301 if (i > MAX_INDEX)
234 break;
235
236 5301 j = scantable[i];
237
2/2
✓ Branch 0 taken 2985 times.
✓ Branch 1 taken 2316 times.
5301 if (level < 0) {
238 2985 level = -level;
239 2985 level = (level * qscale * quant_matrix[j]) >> 4;
240 2985 level = (level - 1) | 1;
241 2985 level = -level;
242 } else {
243 2316 level = (level * qscale * quant_matrix[j]) >> 4;
244 2316 level = (level - 1) | 1;
245 }
246 }
247
248 1477117 block[j] = level;
249
2/2
✓ Branch 0 taken 140368 times.
✓ Branch 1 taken 1336749 times.
1477117 if (((int32_t)GET_CACHE(re, gb)) <= (int32_t)0xBFFFFFFF)
250 140368 break;
251
252 1336749 UPDATE_CACHE(re, gb);
253 }
254 176781 end:
255 176781 LAST_SKIP_BITS(re, gb, 2);
256 176781 CLOSE_READER(re, gb);
257 }
258
259
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 176780 times.
176781 if (i > MAX_INDEX)
260 1 i = AVERROR_INVALIDDATA;
261
262 176781 return i;
263 }
264