FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpeg12.c
Date: 2022-12-09 07:38:14
Exec Total Coverage
Lines: 129 138 93.5%
Functions: 6 6 100.0%
Branches: 69 78 88.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 "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 271 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 271 VLCElem table[680] = { 0 };
73 271 VLC vlc = { .table = table, .table_allocated = static_size };
74
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 271 times.
271 av_assert0(static_size <= FF_ARRAY_ELEMS(table));
75 271 init_vlc(&vlc, TEX_VLC_BITS, n + 2, &table_vlc[0][1], 4, 2, &table_vlc[0][0], 4, 2, INIT_VLC_USE_NEW_STATIC | flags);
76
77
2/2
✓ Branch 0 taken 183434 times.
✓ Branch 1 taken 271 times.
183705 for (i = 0; i < vlc.table_size; i++) {
78 183434 int code = vlc.table[i].sym;
79 183434 int len = vlc.table[i].len;
80 int level, run;
81
82
2/2
✓ Branch 0 taken 5636 times.
✓ Branch 1 taken 177798 times.
183434 if (len == 0) { // illegal code
83 5636 run = 65;
84 5636 level = MAX_LEVEL;
85
2/2
✓ Branch 0 taken 1745 times.
✓ Branch 1 taken 176053 times.
177798 } else if (len<0) { //more bits needed
86 1745 run = 0;
87 1745 level = code;
88 } else {
89
2/2
✓ Branch 0 taken 2168 times.
✓ Branch 1 taken 173885 times.
176053 if (code == n) { //esc
90 2168 run = 65;
91 2168 level = 0;
92
2/2
✓ Branch 0 taken 21152 times.
✓ Branch 1 taken 152733 times.
173885 } else if (code == n + 1) { //eob
93 21152 run = 0;
94 21152 level = 127;
95 } else {
96 152733 run = table_run [code] + 1;
97 152733 level = table_level[code];
98 }
99 }
100 183434 rl_vlc[i].len = len;
101 183434 rl_vlc[i].level = level;
102 183434 rl_vlc[i].run = run;
103 }
104 271 }
105
106 403733 void ff_mpeg1_clean_buffers(MpegEncContext *s)
107 {
108 403733 s->last_dc[0] = 1 << (7 + s->intra_dc_precision);
109 403733 s->last_dc[1] = s->last_dc[0];
110 403733 s->last_dc[2] = s->last_dc[0];
111 403733 memset(s->last_mv, 0, sizeof(s->last_mv));
112 403733 }
113
114
115 /******************************************/
116 /* decoding */
117
118 VLC ff_mv_vlc;
119
120 VLC ff_dc_lum_vlc;
121 VLC ff_dc_chroma_vlc;
122
123 VLC ff_mbincr_vlc;
124 VLC ff_mb_ptype_vlc;
125 VLC ff_mb_btype_vlc;
126 VLC ff_mb_pat_vlc;
127
128 RL_VLC_ELEM ff_mpeg1_rl_vlc[680];
129 RL_VLC_ELEM ff_mpeg2_rl_vlc[674];
130
131 130 static av_cold void mpeg12_init_vlcs(void)
132 {
133 130 INIT_VLC_STATIC(&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, 512);
136 130 INIT_VLC_STATIC(&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, 514);
139 130 INIT_VLC_STATIC(&ff_mv_vlc, MV_VLC_BITS, 17,
140 &ff_mpeg12_mbMotionVectorTable[0][1], 2, 1,
141 &ff_mpeg12_mbMotionVectorTable[0][0], 2, 1, 266);
142 130 INIT_VLC_STATIC(&ff_mbincr_vlc, MBINCR_VLC_BITS, 36,
143 &ff_mpeg12_mbAddrIncrTable[0][1], 2, 1,
144 &ff_mpeg12_mbAddrIncrTable[0][0], 2, 1, 538);
145 130 INIT_VLC_STATIC(&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, 512);
148
149 130 INIT_VLC_STATIC(&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, 64);
152 130 INIT_VLC_STATIC(&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, 64);
155
156 130 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 130 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 130 }
163
164 285 av_cold void ff_mpeg12_init_vlcs(void)
165 {
166 static AVOnce init_static_once = AV_ONCE_INIT;
167 285 ff_thread_once(&init_static_once, mpeg12_init_vlcs);
168 285 }
169
170 #if FF_API_FLAG_TRUNCATED
171 /**
172 * Find the end of the current frame in the bitstream.
173 * @return the position of the first byte of the next frame, or -1
174 */
175 44736 int ff_mpeg1_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size, AVCodecParserContext *s)
176 {
177 int i;
178 44736 uint32_t state = pc->state;
179
180 /* EOF considered as end of frame */
181
2/2
✓ Branch 0 taken 316 times.
✓ Branch 1 taken 44420 times.
44736 if (buf_size == 0)
182 316 return 0;
183
184 /*
185 0 frame start -> 1/4
186 1 first_SEQEXT -> 0/2
187 2 first field start -> 3/0
188 3 second_SEQEXT -> 2/0
189 4 searching end
190 */
191
192
2/2
✓ Branch 0 taken 221065 times.
✓ Branch 1 taken 38413 times.
259478 for (i = 0; i < buf_size; i++) {
193 av_assert1(pc->frame_start_found >= 0 && pc->frame_start_found <= 4);
194
2/2
✓ Branch 0 taken 22777 times.
✓ Branch 1 taken 198288 times.
221065 if (pc->frame_start_found & 1) {
195
4/4
✓ Branch 0 taken 8789 times.
✓ Branch 1 taken 13988 times.
✓ Branch 2 taken 1795 times.
✓ Branch 3 taken 6994 times.
22777 if (state == EXT_START_CODE && (buf[i] & 0xF0) != 0x80)
196 1795 pc->frame_start_found--;
197
2/2
✓ Branch 0 taken 6994 times.
✓ Branch 1 taken 13988 times.
20982 else if (state == EXT_START_CODE + 2) {
198
2/2
✓ Branch 0 taken 3986 times.
✓ Branch 1 taken 3008 times.
6994 if ((buf[i] & 3) == 3)
199 3986 pc->frame_start_found = 0;
200 else
201 3008 pc->frame_start_found = (pc->frame_start_found + 1) & 3;
202 }
203 22777 state++;
204 } else {
205 198288 i = avpriv_find_start_code(buf + i, buf + buf_size, &state) - buf - 1;
206
6/6
✓ Branch 0 taken 22278 times.
✓ Branch 1 taken 176010 times.
✓ Branch 2 taken 15851 times.
✓ Branch 3 taken 6427 times.
✓ Branch 4 taken 6438 times.
✓ Branch 5 taken 9413 times.
198288 if (pc->frame_start_found == 0 && state >= SLICE_MIN_START_CODE && state <= SLICE_MAX_START_CODE) {
207 6438 i++;
208 6438 pc->frame_start_found = 4;
209 }
210
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 198278 times.
198288 if (state == SEQ_END_CODE) {
211 10 pc->frame_start_found = 0;
212 10 pc->state=-1;
213 10 return i+1;
214 }
215
3/4
✓ Branch 0 taken 27674 times.
✓ Branch 1 taken 170604 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 27674 times.
198278 if (pc->frame_start_found == 2 && state == SEQ_START_CODE)
216 pc->frame_start_found = 0;
217
4/4
✓ Branch 0 taken 43514 times.
✓ Branch 1 taken 154764 times.
✓ Branch 2 taken 8789 times.
✓ Branch 3 taken 34725 times.
198278 if (pc->frame_start_found < 4 && state == EXT_START_CODE)
218 8789 pc->frame_start_found++;
219
4/4
✓ Branch 0 taken 154764 times.
✓ Branch 1 taken 43514 times.
✓ Branch 2 taken 121063 times.
✓ Branch 3 taken 33701 times.
198278 if (pc->frame_start_found == 4 && (state & 0xFFFFFF00) == 0x100) {
220
4/4
✓ Branch 0 taken 115466 times.
✓ Branch 1 taken 5597 times.
✓ Branch 2 taken 400 times.
✓ Branch 3 taken 115066 times.
121063 if (state < SLICE_MIN_START_CODE || state > SLICE_MAX_START_CODE) {
221 5997 pc->frame_start_found = 0;
222 5997 pc->state = -1;
223 5997 return i - 3;
224 }
225 }
226
5/6
✓ Branch 0 taken 9100 times.
✓ Branch 1 taken 183181 times.
✓ Branch 2 taken 9100 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6427 times.
✓ Branch 5 taken 2673 times.
192281 if (pc->frame_start_found == 0 && s && state == PICTURE_START_CODE) {
227 6427 ff_fetch_timestamp(s, i - 3, 1, i > 3);
228 }
229 }
230 }
231 38413 pc->state = state;
232 38413 return END_NOT_FOUND;
233 }
234 #endif
235
236 #define MAX_INDEX (64 - 1)
237
238 176526 int ff_mpeg1_decode_block_intra(GetBitContext *gb,
239 const uint16_t *quant_matrix,
240 const uint8_t *scantable, int last_dc[3],
241 int16_t *block, int index, int qscale)
242 {
243 176526 int dc, diff, i = 0, component;
244
245 /* DC coefficient */
246 176526 component = index <= 3 ? 0 : index - 4 + 1;
247
248 176526 diff = decode_dc(gb, component);
249
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 176526 times.
176526 if (diff >= 0xffff)
250 return AVERROR_INVALIDDATA;
251
252 176526 dc = last_dc[component];
253 176526 dc += diff;
254 176526 last_dc[component] = dc;
255
256 176526 block[0] = dc * quant_matrix[0];
257
258 {
259 176526 OPEN_READER(re, gb);
260 176526 UPDATE_CACHE(re, gb);
261
2/2
✓ Branch 0 taken 36372 times.
✓ Branch 1 taken 140154 times.
176526 if (((int32_t)GET_CACHE(re, gb)) <= (int32_t)0xBFFFFFFF)
262 36372 goto end;
263
264 /* now quantify & encode AC coefficients */
265 1334388 while (1) {
266 int level, run, j;
267
268
2/2
✓ Branch 1 taken 139393 times.
✓ Branch 2 taken 1335149 times.
1474542 GET_RL_VLC(level, run, re, gb, ff_mpeg1_rl_vlc,
269 TEX_VLC_BITS, 2, 0);
270
271
2/2
✓ Branch 0 taken 1469278 times.
✓ Branch 1 taken 5264 times.
1474542 if (level != 0) {
272 1469278 i += run;
273
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1469278 times.
1469278 if (i > MAX_INDEX)
274 break;
275
276 1469278 j = scantable[i];
277 1469278 level = (level * qscale * quant_matrix[j]) >> 4;
278 1469278 level = (level - 1) | 1;
279 1469278 level = (level ^ SHOW_SBITS(re, gb, 1)) -
280 1469278 SHOW_SBITS(re, gb, 1);
281 1469278 SKIP_BITS(re, gb, 1);
282 } else {
283 /* escape */
284 5264 run = SHOW_UBITS(re, gb, 6) + 1;
285 5264 LAST_SKIP_BITS(re, gb, 6);
286 5264 UPDATE_CACHE(re, gb);
287 5264 level = SHOW_SBITS(re, gb, 8);
288 5264 SKIP_BITS(re, gb, 8);
289
290
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5264 times.
5264 if (level == -128) {
291 level = SHOW_UBITS(re, gb, 8) - 256;
292 SKIP_BITS(re, gb, 8);
293
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5264 times.
5264 } else if (level == 0) {
294 level = SHOW_UBITS(re, gb, 8);
295 SKIP_BITS(re, gb, 8);
296 }
297
298 5264 i += run;
299
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5264 times.
5264 if (i > MAX_INDEX)
300 break;
301
302 5264 j = scantable[i];
303
2/2
✓ Branch 0 taken 2967 times.
✓ Branch 1 taken 2297 times.
5264 if (level < 0) {
304 2967 level = -level;
305 2967 level = (level * qscale * quant_matrix[j]) >> 4;
306 2967 level = (level - 1) | 1;
307 2967 level = -level;
308 } else {
309 2297 level = (level * qscale * quant_matrix[j]) >> 4;
310 2297 level = (level - 1) | 1;
311 }
312 }
313
314 1474542 block[j] = level;
315
2/2
✓ Branch 0 taken 140154 times.
✓ Branch 1 taken 1334388 times.
1474542 if (((int32_t)GET_CACHE(re, gb)) <= (int32_t)0xBFFFFFFF)
316 140154 break;
317
318 1334388 UPDATE_CACHE(re, gb);
319 }
320 176526 end:
321 176526 LAST_SKIP_BITS(re, gb, 2);
322 176526 CLOSE_READER(re, gb);
323 }
324
325
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 176526 times.
176526 if (i > MAX_INDEX)
326 i = AVERROR_INVALIDDATA;
327
328 176526 return i;
329 }
330