FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/svq1dec.c
Date: 2024-03-28 04:31:58
Exec Total Coverage
Lines: 297 356 83.4%
Functions: 12 14 85.7%
Branches: 171 229 74.7%

Line Branch Exec Source
1 /*
2 * SVQ1 decoder
3 * ported to MPlayer by Arpi <arpi@thot.banki.hu>
4 * ported to libavcodec by Nick Kurshev <nickols_k@mail.ru>
5 *
6 * Copyright (c) 2002 The Xine project
7 * Copyright (c) 2002 The FFmpeg project
8 *
9 * SVQ1 Encoder (c) 2004 Mike Melanson <melanson@pcisys.net>
10 *
11 * This file is part of FFmpeg.
12 *
13 * FFmpeg is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 2.1 of the License, or (at your option) any later version.
17 *
18 * FFmpeg is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
22 *
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with FFmpeg; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 */
27
28 /**
29 * @file
30 * Sorenson Vector Quantizer #1 (SVQ1) video codec.
31 * For more information of the SVQ1 algorithm, visit:
32 * http://www.pcisys.net/~melanson/codecs/
33 */
34
35 #include "libavutil/crc.h"
36 #include "libavutil/thread.h"
37
38 #include "avcodec.h"
39 #include "codec_internal.h"
40 #include "decode.h"
41 #include "get_bits.h"
42 #include "h263data.h"
43 #include "hpeldsp.h"
44 #include "mathops.h"
45 #include "svq1.h"
46
47 #define SVQ1_BLOCK_TYPE_VLC_BITS 3
48 static VLCElem svq1_block_type[8];
49 static VLCElem svq1_motion_component[176];
50 static const VLCElem *svq1_intra_multistage[6];
51 static const VLCElem *svq1_inter_multistage[6];
52 static VLCElem svq1_intra_mean[632];
53 static VLCElem svq1_inter_mean[1434];
54
55 /* motion vector (prediction) */
56 typedef struct svq1_pmv_s {
57 int x;
58 int y;
59 } svq1_pmv;
60
61 typedef struct SVQ1Context {
62 HpelDSPContext hdsp;
63 GetBitContext gb;
64 AVFrame *prev;
65
66 uint8_t *pkt_swapped;
67 int pkt_swapped_allocated;
68
69 svq1_pmv *pmv;
70 int pmv_allocated;
71
72 int width;
73 int height;
74 int frame_code;
75 int nonref; // 1 if the current frame won't be referenced
76
77 int last_tempref;
78 } SVQ1Context;
79
80 static const uint8_t string_table[256] = {
81 0x00, 0xD5, 0x7F, 0xAA, 0xFE, 0x2B, 0x81, 0x54,
82 0x29, 0xFC, 0x56, 0x83, 0xD7, 0x02, 0xA8, 0x7D,
83 0x52, 0x87, 0x2D, 0xF8, 0xAC, 0x79, 0xD3, 0x06,
84 0x7B, 0xAE, 0x04, 0xD1, 0x85, 0x50, 0xFA, 0x2F,
85 0xA4, 0x71, 0xDB, 0x0E, 0x5A, 0x8F, 0x25, 0xF0,
86 0x8D, 0x58, 0xF2, 0x27, 0x73, 0xA6, 0x0C, 0xD9,
87 0xF6, 0x23, 0x89, 0x5C, 0x08, 0xDD, 0x77, 0xA2,
88 0xDF, 0x0A, 0xA0, 0x75, 0x21, 0xF4, 0x5E, 0x8B,
89 0x9D, 0x48, 0xE2, 0x37, 0x63, 0xB6, 0x1C, 0xC9,
90 0xB4, 0x61, 0xCB, 0x1E, 0x4A, 0x9F, 0x35, 0xE0,
91 0xCF, 0x1A, 0xB0, 0x65, 0x31, 0xE4, 0x4E, 0x9B,
92 0xE6, 0x33, 0x99, 0x4C, 0x18, 0xCD, 0x67, 0xB2,
93 0x39, 0xEC, 0x46, 0x93, 0xC7, 0x12, 0xB8, 0x6D,
94 0x10, 0xC5, 0x6F, 0xBA, 0xEE, 0x3B, 0x91, 0x44,
95 0x6B, 0xBE, 0x14, 0xC1, 0x95, 0x40, 0xEA, 0x3F,
96 0x42, 0x97, 0x3D, 0xE8, 0xBC, 0x69, 0xC3, 0x16,
97 0xEF, 0x3A, 0x90, 0x45, 0x11, 0xC4, 0x6E, 0xBB,
98 0xC6, 0x13, 0xB9, 0x6C, 0x38, 0xED, 0x47, 0x92,
99 0xBD, 0x68, 0xC2, 0x17, 0x43, 0x96, 0x3C, 0xE9,
100 0x94, 0x41, 0xEB, 0x3E, 0x6A, 0xBF, 0x15, 0xC0,
101 0x4B, 0x9E, 0x34, 0xE1, 0xB5, 0x60, 0xCA, 0x1F,
102 0x62, 0xB7, 0x1D, 0xC8, 0x9C, 0x49, 0xE3, 0x36,
103 0x19, 0xCC, 0x66, 0xB3, 0xE7, 0x32, 0x98, 0x4D,
104 0x30, 0xE5, 0x4F, 0x9A, 0xCE, 0x1B, 0xB1, 0x64,
105 0x72, 0xA7, 0x0D, 0xD8, 0x8C, 0x59, 0xF3, 0x26,
106 0x5B, 0x8E, 0x24, 0xF1, 0xA5, 0x70, 0xDA, 0x0F,
107 0x20, 0xF5, 0x5F, 0x8A, 0xDE, 0x0B, 0xA1, 0x74,
108 0x09, 0xDC, 0x76, 0xA3, 0xF7, 0x22, 0x88, 0x5D,
109 0xD6, 0x03, 0xA9, 0x7C, 0x28, 0xFD, 0x57, 0x82,
110 0xFF, 0x2A, 0x80, 0x55, 0x01, 0xD4, 0x7E, 0xAB,
111 0x84, 0x51, 0xFB, 0x2E, 0x7A, 0xAF, 0x05, 0xD0,
112 0xAD, 0x78, 0xD2, 0x07, 0x53, 0x86, 0x2C, 0xF9
113 };
114
115 #define SVQ1_PROCESS_VECTOR() \
116 for (; level > 0; i++) { \
117 /* process next depth */ \
118 if (i == m) { \
119 m = n; \
120 if (--level == 0) \
121 break; \
122 } \
123 /* divide block if next bit set */ \
124 if (!get_bits1(bitbuf)) \
125 break; \
126 /* add child nodes */ \
127 list[n++] = list[i]; \
128 list[n++] = list[i] + (((level & 1) ? pitch : 1) << ((level >> 1) + 1));\
129 }
130
131 #define SVQ1_ADD_CODEBOOK() \
132 /* add codebook entries to vector */ \
133 for (j = 0; j < stages; j++) { \
134 n3 = codebook[entries[j]] ^ 0x80808080; \
135 n1 += (n3 & 0xFF00FF00) >> 8; \
136 n2 += n3 & 0x00FF00FF; \
137 } \
138 \
139 /* clip to [0..255] */ \
140 if (n1 & 0xFF00FF00) { \
141 n3 = (n1 >> 15 & 0x00010001 | 0x01000100) - 0x00010001; \
142 n1 += 0x7F007F00; \
143 n1 |= (~n1 >> 15 & 0x00010001 | 0x01000100) - 0x00010001; \
144 n1 &= n3 & 0x00FF00FF; \
145 } \
146 \
147 if (n2 & 0xFF00FF00) { \
148 n3 = (n2 >> 15 & 0x00010001 | 0x01000100) - 0x00010001; \
149 n2 += 0x7F007F00; \
150 n2 |= (~n2 >> 15 & 0x00010001 | 0x01000100) - 0x00010001; \
151 n2 &= n3 & 0x00FF00FF; \
152 }
153
154 #define SVQ1_CALC_CODEBOOK_ENTRIES(cbook) \
155 codebook = (const uint32_t *)cbook[level]; \
156 if (stages > 0) \
157 bit_cache = get_bits(bitbuf, 4 * stages); \
158 /* calculate codebook entries for this vector */ \
159 for (j = 0; j < stages; j++) { \
160 entries[j] = (((bit_cache >> (4 * (stages - j - 1))) & 0xF) + \
161 16 * j) << (level + 1); \
162 } \
163 mean -= stages * 128; \
164 n4 = (mean << 16) + mean;
165
166 12347 static int svq1_decode_block_intra(GetBitContext *bitbuf, uint8_t *pixels,
167 ptrdiff_t pitch)
168 {
169 uint32_t bit_cache;
170 uint8_t *list[63];
171 uint32_t *dst;
172 const uint32_t *codebook;
173 int entries[6];
174 int i, j, m, n;
175 int stages;
176 unsigned mean;
177 unsigned x, y, width, height, level;
178 uint32_t n1, n2, n3, n4;
179
180 /* initialize list for breadth first processing of vectors */
181 12347 list[0] = pixels;
182
183 /* recursively process vector */
184
2/2
✓ Branch 0 taken 243721 times.
✓ Branch 1 taken 12347 times.
256068 for (i = 0, m = 1, n = 1, level = 5; i < n; i++) {
185
10/10
✓ Branch 0 taken 58383 times.
✓ Branch 1 taken 261280 times.
✓ Branch 2 taken 11196 times.
✓ Branch 3 taken 47187 times.
✓ Branch 5 taken 77093 times.
✓ Branch 6 taken 231374 times.
✓ Branch 7 taken 139080 times.
✓ Branch 8 taken 92294 times.
✓ Branch 9 taken 319663 times.
✓ Branch 10 taken 155432 times.
475095 SVQ1_PROCESS_VECTOR();
186
187 /* destination address and vector size */
188 243721 dst = (uint32_t *)list[i];
189 243721 width = 1 << ((4 + level) / 2);
190 243721 height = 1 << ((3 + level) / 2);
191
192 /* get number of stages (-1 skips vector, 0 for mean only) */
193 243721 stages = get_vlc2(bitbuf, svq1_intra_multistage[level], 4, 2) - 1;
194
195
2/2
✓ Branch 0 taken 371 times.
✓ Branch 1 taken 243350 times.
243721 if (stages == -1) {
196
2/2
✓ Branch 0 taken 4122 times.
✓ Branch 1 taken 371 times.
4493 for (y = 0; y < height; y++)
197 4122 memset(&dst[y * (pitch / 4)], 0, width);
198 371 continue; /* skip vector */
199 }
200
201
3/4
✓ Branch 0 taken 183606 times.
✓ Branch 1 taken 59744 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 183606 times.
243350 if ((stages > 0 && level >= 4)) {
202 ff_dlog(NULL,
203 "Error (svq1_decode_block_intra): invalid vector: stages=%i level=%i\n",
204 stages, level);
205 return AVERROR_INVALIDDATA; /* invalid vector */
206 }
207
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 243350 times.
243350 av_assert0(stages >= 0);
208
209 243350 mean = get_vlc2(bitbuf, svq1_intra_mean, 8, 3);
210
211
2/2
✓ Branch 0 taken 59744 times.
✓ Branch 1 taken 183606 times.
243350 if (stages == 0) {
212
2/2
✓ Branch 0 taken 158400 times.
✓ Branch 1 taken 59744 times.
218144 for (y = 0; y < height; y++)
213 158400 memset(&dst[y * (pitch / 4)], mean, width);
214 } else {
215
3/4
✓ Branch 0 taken 183606 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 949117 times.
✓ Branch 4 taken 183606 times.
1132723 SVQ1_CALC_CODEBOOK_ENTRIES(ff_svq1_intra_codebooks);
216
217
2/2
✓ Branch 0 taken 498450 times.
✓ Branch 1 taken 183606 times.
682056 for (y = 0; y < height; y++) {
218
2/2
✓ Branch 0 taken 580842 times.
✓ Branch 1 taken 498450 times.
1079292 for (x = 0; x < width / 4; x++, codebook++) {
219 580842 n1 = n4;
220 580842 n2 = n4;
221
6/6
✓ Branch 0 taken 2927940 times.
✓ Branch 1 taken 580842 times.
✓ Branch 2 taken 221 times.
✓ Branch 3 taken 580621 times.
✓ Branch 4 taken 350 times.
✓ Branch 5 taken 580492 times.
3508782 SVQ1_ADD_CODEBOOK()
222 /* store result */
223 580842 dst[x] = n1 << 8 | n2;
224 }
225 498450 dst += pitch / 4;
226 }
227 }
228 }
229
230 12347 return 0;
231 }
232
233 65737 static int svq1_decode_block_non_intra(GetBitContext *bitbuf, uint8_t *pixels,
234 ptrdiff_t pitch, int buggy)
235 {
236 uint32_t bit_cache;
237 uint8_t *list[63];
238 uint32_t *dst;
239 const uint32_t *codebook;
240 int entries[6];
241 int i, j, m, n;
242 int stages;
243 unsigned mean;
244 int x, y, width, height, level;
245 uint32_t n1, n2, n3, n4;
246
247 /* initialize list for breadth first processing of vectors */
248 65737 list[0] = pixels;
249
250 /* recursively process vector */
251
2/2
✓ Branch 0 taken 1234047 times.
✓ Branch 1 taken 65737 times.
1299784 for (i = 0, m = 1, n = 1, level = 5; i < n; i++) {
252
10/10
✓ Branch 0 taken 311020 times.
✓ Branch 1 taken 1259002 times.
✓ Branch 2 taken 60529 times.
✓ Branch 3 taken 250491 times.
✓ Branch 5 taken 341183 times.
✓ Branch 6 taken 1168310 times.
✓ Branch 7 taken 713751 times.
✓ Branch 8 taken 454559 times.
✓ Branch 9 taken 1570022 times.
✓ Branch 10 taken 832335 times.
2402357 SVQ1_PROCESS_VECTOR();
253
254 /* destination address and vector size */
255 1234047 dst = (uint32_t *)list[i];
256 1234047 width = 1 << ((4 + level) / 2);
257 1234047 height = 1 << ((3 + level) / 2);
258
259 /* get number of stages (-1 skips vector, 0 for mean only) */
260 1234047 stages = get_vlc2(bitbuf, svq1_inter_multistage[level], 3, 2) - 1;
261
262
2/2
✓ Branch 0 taken 29883 times.
✓ Branch 1 taken 1204164 times.
1234047 if (stages == -1)
263 29883 continue; /* skip vector */
264
265
3/4
✓ Branch 0 taken 466312 times.
✓ Branch 1 taken 737852 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 466312 times.
1204164 if ((stages > 0 && level >= 4)) {
266 ff_dlog(NULL,
267 "Error (svq1_decode_block_non_intra): invalid vector: stages=%i level=%i\n",
268 stages, level);
269 return AVERROR_INVALIDDATA; /* invalid vector */
270 }
271
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1204164 times.
1204164 av_assert0(stages >= 0);
272
273 1204164 mean = get_vlc2(bitbuf, svq1_inter_mean, 9, 3) - 256;
274
275
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1204164 times.
1204164 if (buggy) {
276 if (mean == -128)
277 mean = 128;
278 else if (mean == 128)
279 mean = -128;
280 }
281
282
4/4
✓ Branch 0 taken 466312 times.
✓ Branch 1 taken 737852 times.
✓ Branch 3 taken 2167843 times.
✓ Branch 4 taken 1204164 times.
3372007 SVQ1_CALC_CODEBOOK_ENTRIES(ff_svq1_inter_codebooks);
283
284
2/2
✓ Branch 0 taken 3229280 times.
✓ Branch 1 taken 1204164 times.
4433444 for (y = 0; y < height; y++) {
285
2/2
✓ Branch 0 taken 4011448 times.
✓ Branch 1 taken 3229280 times.
7240728 for (x = 0; x < width / 4; x++) {
286 4011448 n3 = dst[x];
287 /* add mean value to vector */
288 4011448 n1 = n4 + ((n3 & 0xFF00FF00) >> 8);
289 4011448 n2 = n4 + (n3 & 0x00FF00FF);
290
6/6
✓ Branch 0 taken 5356192 times.
✓ Branch 1 taken 4011448 times.
✓ Branch 2 taken 8297 times.
✓ Branch 3 taken 4003151 times.
✓ Branch 4 taken 8590 times.
✓ Branch 5 taken 4002858 times.
9367640 SVQ1_ADD_CODEBOOK()
291 /* store result */
292 4011448 dst[x] = n1 << 8 | n2;
293
2/2
✓ Branch 0 taken 3641176 times.
✓ Branch 1 taken 370272 times.
4011448 if (codebook != NULL)
294 3641176 codebook++;
295 }
296 3229280 dst += pitch / 4;
297 }
298 }
299 65737 return 0;
300 }
301
302 79711 static int svq1_decode_motion_vector(GetBitContext *bitbuf, svq1_pmv *mv,
303 svq1_pmv **pmv)
304 {
305 int diff;
306 int i;
307
308
2/2
✓ Branch 0 taken 159422 times.
✓ Branch 1 taken 79711 times.
239133 for (i = 0; i < 2; i++) {
309 /* get motion code */
310 159422 diff = get_vlc2(bitbuf, svq1_motion_component, 7, 2);
311
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 159422 times.
159422 if (diff < 0)
312 return AVERROR_INVALIDDATA;
313
2/2
✓ Branch 0 taken 79034 times.
✓ Branch 1 taken 80388 times.
159422 else if (diff) {
314
2/2
✓ Branch 1 taken 34296 times.
✓ Branch 2 taken 44738 times.
79034 if (get_bits1(bitbuf))
315 34296 diff = -diff;
316 }
317
318 /* add median of motion vector predictors and clip result */
319
2/2
✓ Branch 0 taken 79711 times.
✓ Branch 1 taken 79711 times.
159422 if (i == 1)
320 79711 mv->y = sign_extend(diff + mid_pred(pmv[0]->y, pmv[1]->y, pmv[2]->y), 6);
321 else
322 79711 mv->x = sign_extend(diff + mid_pred(pmv[0]->x, pmv[1]->x, pmv[2]->x), 6);
323 }
324
325 79711 return 0;
326 }
327
328 7688 static void svq1_skip_block(uint8_t *current, uint8_t *previous,
329 ptrdiff_t pitch, int x, int y)
330 {
331 uint8_t *src;
332 uint8_t *dst;
333 int i;
334
335 7688 src = &previous[x + y * pitch];
336 7688 dst = current;
337
338
2/2
✓ Branch 0 taken 123008 times.
✓ Branch 1 taken 7688 times.
130696 for (i = 0; i < 16; i++) {
339 123008 memcpy(dst, src, 16);
340 123008 src += pitch;
341 123008 dst += pitch;
342 }
343 7688 }
344
345 61079 static int svq1_motion_inter_block(HpelDSPContext *hdsp, GetBitContext *bitbuf,
346 uint8_t *current, uint8_t *previous,
347 ptrdiff_t pitch, svq1_pmv *motion, int x, int y,
348 int width, int height)
349 {
350 uint8_t *src;
351 uint8_t *dst;
352 svq1_pmv mv;
353 svq1_pmv *pmv[3];
354 int result;
355
356 /* predict and decode motion vector */
357 61079 pmv[0] = &motion[0];
358
2/2
✓ Branch 0 taken 5028 times.
✓ Branch 1 taken 56051 times.
61079 if (y == 0) {
359 5028 pmv[1] =
360 5028 pmv[2] = pmv[0];
361 } else {
362 56051 pmv[1] = &motion[x / 8 + 2];
363 56051 pmv[2] = &motion[x / 8 + 4];
364 }
365
366 61079 result = svq1_decode_motion_vector(bitbuf, &mv, pmv);
367
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 61079 times.
61079 if (result)
368 return result;
369
370 61079 motion[0].x =
371 61079 motion[x / 8 + 2].x =
372 61079 motion[x / 8 + 3].x = mv.x;
373 61079 motion[0].y =
374 61079 motion[x / 8 + 2].y =
375 61079 motion[x / 8 + 3].y = mv.y;
376
377 61079 mv.x = av_clip(mv.x, -2 * x, 2 * (width - x - 16));
378 61079 mv.y = av_clip(mv.y, -2 * y, 2 * (height - y - 16));
379
380 61079 src = &previous[(x + (mv.x >> 1)) + (y + (mv.y >> 1)) * pitch];
381 61079 dst = current;
382
383 61079 hdsp->put_pixels_tab[0][(mv.y & 1) << 1 | (mv.x & 1)](dst, src, pitch, 16);
384
385 61079 return 0;
386 }
387
388 4658 static int svq1_motion_inter_4v_block(HpelDSPContext *hdsp, GetBitContext *bitbuf,
389 uint8_t *current, uint8_t *previous,
390 ptrdiff_t pitch, svq1_pmv *motion, int x, int y,
391 int width, int height)
392 {
393 uint8_t *src;
394 uint8_t *dst;
395 svq1_pmv mv;
396 svq1_pmv *pmv[4];
397 int i, result;
398
399 /* predict and decode motion vector (0) */
400 4658 pmv[0] = &motion[0];
401
2/2
✓ Branch 0 taken 731 times.
✓ Branch 1 taken 3927 times.
4658 if (y == 0) {
402 731 pmv[1] =
403 731 pmv[2] = pmv[0];
404 } else {
405 3927 pmv[1] = &motion[(x / 8) + 2];
406 3927 pmv[2] = &motion[(x / 8) + 4];
407 }
408
409 4658 result = svq1_decode_motion_vector(bitbuf, &mv, pmv);
410
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4658 times.
4658 if (result)
411 return result;
412
413 /* predict and decode motion vector (1) */
414 4658 pmv[0] = &mv;
415
2/2
✓ Branch 0 taken 731 times.
✓ Branch 1 taken 3927 times.
4658 if (y == 0) {
416 731 pmv[1] =
417 731 pmv[2] = pmv[0];
418 } else {
419 3927 pmv[1] = &motion[(x / 8) + 3];
420 }
421 4658 result = svq1_decode_motion_vector(bitbuf, &motion[0], pmv);
422
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4658 times.
4658 if (result)
423 return result;
424
425 /* predict and decode motion vector (2) */
426 4658 pmv[1] = &motion[0];
427 4658 pmv[2] = &motion[(x / 8) + 1];
428
429 4658 result = svq1_decode_motion_vector(bitbuf, &motion[(x / 8) + 2], pmv);
430
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4658 times.
4658 if (result)
431 return result;
432
433 /* predict and decode motion vector (3) */
434 4658 pmv[2] = &motion[(x / 8) + 2];
435 4658 pmv[3] = &motion[(x / 8) + 3];
436
437 4658 result = svq1_decode_motion_vector(bitbuf, pmv[3], pmv);
438
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4658 times.
4658 if (result)
439 return result;
440
441 /* form predictions */
442
2/2
✓ Branch 0 taken 18632 times.
✓ Branch 1 taken 4658 times.
23290 for (i = 0; i < 4; i++) {
443 18632 int mvx = pmv[i]->x + (i & 1) * 16;
444 18632 int mvy = pmv[i]->y + (i >> 1) * 16;
445
446 // FIXME: clipping or padding?
447 18632 mvx = av_clip(mvx, -2 * x, 2 * (width - x - 8));
448 18632 mvy = av_clip(mvy, -2 * y, 2 * (height - y - 8));
449
450 18632 src = &previous[(x + (mvx >> 1)) + (y + (mvy >> 1)) * pitch];
451 18632 dst = current;
452
453 18632 hdsp->put_pixels_tab[1][((mvy & 1) << 1) | (mvx & 1)](dst, src, pitch, 8);
454
455 /* select next block */
456
2/2
✓ Branch 0 taken 9316 times.
✓ Branch 1 taken 9316 times.
18632 if (i & 1)
457 9316 current += 8 * (pitch - 1);
458 else
459 9316 current += 8;
460 }
461
462 4658 return 0;
463 }
464
465 77239 static int svq1_decode_delta_block(AVCodecContext *avctx, HpelDSPContext *hdsp,
466 GetBitContext *bitbuf,
467 uint8_t *current, uint8_t *previous,
468 ptrdiff_t pitch, svq1_pmv *motion, int x, int y,
469 int width, int height, int buggy)
470 {
471 uint32_t block_type;
472 77239 int result = 0;
473
474 /* get block type */
475 77239 block_type = get_vlc2(bitbuf, svq1_block_type,
476 SVQ1_BLOCK_TYPE_VLC_BITS, 1);
477
478 /* reset motion vectors */
479
4/4
✓ Branch 0 taken 69551 times.
✓ Branch 1 taken 7688 times.
✓ Branch 2 taken 3814 times.
✓ Branch 3 taken 65737 times.
77239 if (block_type == SVQ1_BLOCK_SKIP || block_type == SVQ1_BLOCK_INTRA) {
480 11502 motion[0].x =
481 11502 motion[0].y =
482 11502 motion[x / 8 + 2].x =
483 11502 motion[x / 8 + 2].y =
484 11502 motion[x / 8 + 3].x =
485 11502 motion[x / 8 + 3].y = 0;
486 }
487
488
4/5
✓ Branch 0 taken 7688 times.
✓ Branch 1 taken 61079 times.
✓ Branch 2 taken 4658 times.
✓ Branch 3 taken 3814 times.
✗ Branch 4 not taken.
77239 switch (block_type) {
489 7688 case SVQ1_BLOCK_SKIP:
490 7688 svq1_skip_block(current, previous, pitch, x, y);
491 7688 break;
492
493 61079 case SVQ1_BLOCK_INTER:
494 61079 result = svq1_motion_inter_block(hdsp, bitbuf, current, previous,
495 pitch, motion, x, y, width, height);
496
497
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 61079 times.
61079 if (result != 0) {
498 ff_dlog(avctx, "Error in svq1_motion_inter_block %i\n", result);
499 break;
500 }
501 61079 result = svq1_decode_block_non_intra(bitbuf, current, pitch, buggy);
502 61079 break;
503
504 4658 case SVQ1_BLOCK_INTER_4V:
505 4658 result = svq1_motion_inter_4v_block(hdsp, bitbuf, current, previous,
506 pitch, motion, x, y, width, height);
507
508
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4658 times.
4658 if (result != 0) {
509 ff_dlog(avctx, "Error in svq1_motion_inter_4v_block %i\n", result);
510 break;
511 }
512 4658 result = svq1_decode_block_non_intra(bitbuf, current, pitch, buggy);
513 4658 break;
514
515 3814 case SVQ1_BLOCK_INTRA:
516 3814 result = svq1_decode_block_intra(bitbuf, current, pitch);
517 3814 break;
518 }
519
520 77239 return result;
521 }
522
523 static void svq1_parse_string(GetBitContext *bitbuf, uint8_t out[257])
524 {
525 uint8_t seed;
526 int i;
527
528 out[0] = get_bits(bitbuf, 8);
529 seed = string_table[out[0]];
530
531 for (i = 1; i <= out[0]; i++) {
532 out[i] = get_bits(bitbuf, 8) ^ seed;
533 seed = string_table[out[i] ^ seed];
534 }
535 out[i] = 0;
536 }
537
538 363 static int svq1_decode_frame_header(AVCodecContext *avctx, AVFrame *frame, int * buggy)
539 {
540 363 SVQ1Context *s = avctx->priv_data;
541 363 GetBitContext *bitbuf = &s->gb;
542 int frame_size_code;
543 363 int width = s->width;
544 363 int height = s->height;
545 int tempref;
546
547 363 tempref = get_bits(bitbuf, 8); /* temporal_reference */
548
5/6
✓ Branch 0 taken 202 times.
✓ Branch 1 taken 161 times.
✓ Branch 2 taken 196 times.
✓ Branch 3 taken 6 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 196 times.
363 *buggy = tempref == 0 && s->last_tempref == 0 && avctx->extradata_size == 0;
549 363 s->last_tempref = tempref;
550
551 /* frame type */
552 363 s->nonref = 0;
553
2/4
✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 327 times.
✗ Branch 4 not taken.
363 switch (get_bits(bitbuf, 2)) {
554 36 case 0:
555 36 frame->pict_type = AV_PICTURE_TYPE_I;
556 36 break;
557 case 2:
558 s->nonref = 1;
559 327 case 1:
560 327 frame->pict_type = AV_PICTURE_TYPE_P;
561 327 break;
562 default:
563 av_log(avctx, AV_LOG_ERROR, "Invalid frame type.\n");
564 return AVERROR_INVALIDDATA;
565 }
566
567
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 327 times.
363 if (frame->pict_type == AV_PICTURE_TYPE_I) {
568 /* unknown fields */
569
3/4
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 35 times.
36 if (s->frame_code == 0x50 || s->frame_code == 0x60) {
570 1 int csum = get_bits(bitbuf, 16);
571
572 1 csum = av_bswap16(av_crc(av_crc_get_table(AV_CRC_16_CCITT), av_bswap16(csum), bitbuf->buffer, bitbuf->size_in_bits >> 3));
573
574 ff_dlog(avctx, "%s checksum (%02x) for packet data\n",
575 (csum == 0) ? "correct" : "incorrect", csum);
576 }
577
578
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
36 if ((s->frame_code ^ 0x10) >= 0x50) {
579 uint8_t msg[257];
580
581 svq1_parse_string(bitbuf, msg);
582
583 av_log(avctx, AV_LOG_INFO,
584 "embedded message:\n%s\n", ((char *)msg) + 1);
585 }
586
587 36 skip_bits(bitbuf, 2);
588 36 skip_bits(bitbuf, 2);
589 36 skip_bits1(bitbuf);
590
591 /* load frame size */
592 36 frame_size_code = get_bits(bitbuf, 3);
593
594
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 30 times.
36 if (frame_size_code == 7) {
595 /* load width, height (12 bits each) */
596 6 width = get_bits(bitbuf, 12);
597 6 height = get_bits(bitbuf, 12);
598
599
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
6 if (!width || !height)
600 return AVERROR_INVALIDDATA;
601 } else {
602 /* get width, height from table */
603 30 width = ff_svq1_frame_size_table[frame_size_code][0];
604 30 height = ff_svq1_frame_size_table[frame_size_code][1];
605 }
606 }
607
608 /* unknown fields */
609
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 363 times.
363 if (get_bits1(bitbuf)) {
610 skip_bits1(bitbuf); /* use packet checksum if (1) */
611 skip_bits1(bitbuf); /* component checksums after image data if (1) */
612
613 if (get_bits(bitbuf, 2) != 0)
614 return AVERROR_INVALIDDATA;
615 }
616
617
2/2
✓ Branch 1 taken 163 times.
✓ Branch 2 taken 200 times.
363 if (get_bits1(bitbuf)) {
618 163 skip_bits1(bitbuf);
619 163 skip_bits(bitbuf, 4);
620 163 skip_bits1(bitbuf);
621 163 skip_bits(bitbuf, 2);
622
623
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 163 times.
163 if (skip_1stop_8data_bits(bitbuf) < 0)
624 return AVERROR_INVALIDDATA;
625 }
626
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 363 times.
363 if (get_bits_left(bitbuf) <= 0)
627 return AVERROR_INVALIDDATA;
628
629 363 s->width = width;
630 363 s->height = height;
631 363 return 0;
632 }
633
634 363 static int svq1_decode_frame(AVCodecContext *avctx, AVFrame *cur,
635 int *got_frame, AVPacket *avpkt)
636 {
637 363 const uint8_t *buf = avpkt->data;
638 363 int buf_size = avpkt->size;
639 363 SVQ1Context *s = avctx->priv_data;
640 uint8_t *current;
641 int result, i, x, y, width, height, buggy;
642 int ret;
643
644 /* initialize bit buffer */
645 363 ret = init_get_bits8(&s->gb, buf, buf_size);
646
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 363 times.
363 if (ret < 0)
647 return ret;
648
649 /* decode frame header */
650 363 s->frame_code = get_bits(&s->gb, 22);
651
652
2/4
✓ Branch 0 taken 363 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 363 times.
363 if ((s->frame_code & ~0x70) || !(s->frame_code & 0x60))
653 return AVERROR_INVALIDDATA;
654
655 /* swap some header bytes (why?) */
656
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 352 times.
363 if (s->frame_code != 0x20) {
657 uint32_t *src;
658
659
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if (buf_size < 9 * 4) {
660 av_log(avctx, AV_LOG_ERROR, "Input packet too small\n");
661 return AVERROR_INVALIDDATA;
662 }
663
664 11 av_fast_padded_malloc(&s->pkt_swapped,
665 11 &s->pkt_swapped_allocated,
666 buf_size);
667
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if (!s->pkt_swapped)
668 return AVERROR(ENOMEM);
669
670 11 memcpy(s->pkt_swapped, buf, buf_size);
671 11 buf = s->pkt_swapped;
672 11 init_get_bits(&s->gb, buf, buf_size * 8);
673 11 skip_bits(&s->gb, 22);
674
675 11 src = (uint32_t *)(s->pkt_swapped + 4);
676
677
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 11 times.
55 for (i = 0; i < 4; i++)
678 44 src[i] = ((src[i] << 16) | (src[i] >> 16)) ^ src[7 - i];
679 }
680
681 363 result = svq1_decode_frame_header(avctx, cur, &buggy);
682
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 363 times.
363 if (result != 0) {
683 ff_dlog(avctx, "Error in svq1_decode_frame_header %i\n", result);
684 return result;
685 }
686
687 363 result = ff_set_dimensions(avctx, s->width, s->height);
688
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 363 times.
363 if (result < 0)
689 return result;
690
691
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 363 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
363 if ((avctx->skip_frame >= AVDISCARD_NONREF && s->nonref) ||
692
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 363 times.
363 (avctx->skip_frame >= AVDISCARD_NONKEY &&
693 cur->pict_type != AV_PICTURE_TYPE_I) ||
694
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 363 times.
363 avctx->skip_frame >= AVDISCARD_ALL)
695 return buf_size;
696
697 363 result = ff_get_buffer(avctx, cur, s->nonref ? 0 : AV_GET_BUFFER_FLAG_REF);
698
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 363 times.
363 if (result < 0)
699 return result;
700
701 363 av_fast_padded_malloc(&s->pmv, &s->pmv_allocated, (FFALIGN(s->width, 16) / 8 + 3) * sizeof(*s->pmv));
702
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 363 times.
363 if (!s->pmv)
703 return AVERROR(ENOMEM);
704
705 /* decode y, u and v components */
706
2/2
✓ Branch 0 taken 1089 times.
✓ Branch 1 taken 363 times.
1452 for (i = 0; i < 3; i++) {
707 1089 int linesize = cur->linesize[i];
708
2/2
✓ Branch 0 taken 363 times.
✓ Branch 1 taken 726 times.
1089 if (i == 0) {
709 363 width = FFALIGN(s->width, 16);
710 363 height = FFALIGN(s->height, 16);
711 } else {
712
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 726 times.
726 if (avctx->flags & AV_CODEC_FLAG_GRAY)
713 break;
714 726 width = FFALIGN(s->width / 4, 16);
715 726 height = FFALIGN(s->height / 4, 16);
716 }
717
718 1089 current = cur->data[i];
719
720
2/2
✓ Branch 0 taken 108 times.
✓ Branch 1 taken 981 times.
1089 if (cur->pict_type == AV_PICTURE_TYPE_I) {
721 /* keyframe */
722
2/2
✓ Branch 0 taken 643 times.
✓ Branch 1 taken 108 times.
751 for (y = 0; y < height; y += 16) {
723
2/2
✓ Branch 0 taken 8533 times.
✓ Branch 1 taken 643 times.
9176 for (x = 0; x < width; x += 16) {
724 8533 result = svq1_decode_block_intra(&s->gb, &current[x],
725 linesize);
726
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8533 times.
8533 if (result) {
727 av_log(avctx, AV_LOG_ERROR,
728 "Error in svq1_decode_block %i (keyframe)\n",
729 result);
730 return result;
731 }
732 }
733 643 current += 16 * linesize;
734 }
735 } else {
736 /* delta frame */
737 981 uint8_t *previous = s->prev->data[i];
738
1/2
✓ Branch 0 taken 981 times.
✗ Branch 1 not taken.
981 if (!previous ||
739
2/4
✓ Branch 0 taken 981 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 981 times.
981 s->prev->width != s->width || s->prev->height != s->height) {
740 av_log(avctx, AV_LOG_ERROR, "Missing reference frame.\n");
741 return AVERROR_INVALIDDATA;
742 }
743
744 981 memset(s->pmv, 0, ((width / 8) + 3) * sizeof(svq1_pmv));
745
746
2/2
✓ Branch 0 taken 5829 times.
✓ Branch 1 taken 981 times.
6810 for (y = 0; y < height; y += 16) {
747
2/2
✓ Branch 0 taken 77239 times.
✓ Branch 1 taken 5829 times.
83068 for (x = 0; x < width; x += 16) {
748 77239 result = svq1_decode_delta_block(avctx, &s->hdsp,
749 &s->gb, &current[x],
750 previous, linesize,
751 s->pmv, x, y, width, height, buggy);
752
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 77239 times.
77239 if (result != 0) {
753 ff_dlog(avctx,
754 "Error in svq1_decode_delta_block %i\n",
755 result);
756 return result;
757 }
758 }
759
760 5829 s->pmv[0].x =
761 5829 s->pmv[0].y = 0;
762
763 5829 current += 16 * linesize;
764 }
765 }
766 }
767
768
1/2
✓ Branch 0 taken 363 times.
✗ Branch 1 not taken.
363 if (!s->nonref) {
769 363 result = av_frame_replace(s->prev, cur);
770
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 363 times.
363 if (result < 0)
771 return result;
772 }
773
774 363 *got_frame = 1;
775 363 result = buf_size;
776
777 363 return result;
778 }
779
780 9 static av_cold void svq1_static_init(void)
781 {
782 static VLCElem table[196];
783 9 VLCInitState state = VLC_INIT_STATE(table);
784
785 9 VLC_INIT_STATIC_TABLE(svq1_block_type, SVQ1_BLOCK_TYPE_VLC_BITS, 4,
786 &ff_svq1_block_type_vlc[0][1], 2, 1,
787 &ff_svq1_block_type_vlc[0][0], 2, 1, 0);
788
789 9 VLC_INIT_STATIC_TABLE(svq1_motion_component, 7, 33,
790 &ff_mvtab[0][1], 2, 1,
791 &ff_mvtab[0][0], 2, 1, 0);
792
793
2/2
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 9 times.
63 for (int i = 0; i < 6; i++) {
794 54 svq1_intra_multistage[i] =
795 54 ff_vlc_init_tables(&state, 4, 8,
796 54 &ff_svq1_intra_multistage_vlc[i][0][1], 2, 1,
797 54 &ff_svq1_intra_multistage_vlc[i][0][0], 2, 1, 0);
798 54 svq1_inter_multistage[i] =
799 54 ff_vlc_init_tables(&state, 3, 8,
800 54 &ff_svq1_inter_multistage_vlc[i][0][1], 2, 1,
801 54 &ff_svq1_inter_multistage_vlc[i][0][0], 2, 1, 0);
802 }
803
804 9 VLC_INIT_STATIC_TABLE(svq1_intra_mean, 8, 256,
805 &ff_svq1_intra_mean_vlc[0][1], 4, 2,
806 &ff_svq1_intra_mean_vlc[0][0], 4, 2, 0);
807
808 9 VLC_INIT_STATIC_TABLE(svq1_inter_mean, 9, 512,
809 &ff_svq1_inter_mean_vlc[0][1], 4, 2,
810 &ff_svq1_inter_mean_vlc[0][0], 4, 2, 0);
811 9 }
812
813 15 static av_cold int svq1_decode_init(AVCodecContext *avctx)
814 {
815 static AVOnce init_static_once = AV_ONCE_INIT;
816 15 SVQ1Context *s = avctx->priv_data;
817
818 15 s->prev = av_frame_alloc();
819
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (!s->prev)
820 return AVERROR(ENOMEM);
821
822 15 s->width = avctx->width + 3 & ~3;
823 15 s->height = avctx->height + 3 & ~3;
824 15 avctx->pix_fmt = AV_PIX_FMT_YUV410P;
825
826 15 ff_hpeldsp_init(&s->hdsp, avctx->flags);
827
828 15 ff_thread_once(&init_static_once, svq1_static_init);
829
830 15 s->last_tempref = 0xFF;
831
832 15 return 0;
833 }
834
835 15 static av_cold int svq1_decode_end(AVCodecContext *avctx)
836 {
837 15 SVQ1Context *s = avctx->priv_data;
838
839 15 av_frame_free(&s->prev);
840 15 av_freep(&s->pkt_swapped);
841 15 s->pkt_swapped_allocated = 0;
842 15 av_freep(&s->pmv);
843 15 s->pmv_allocated = 0;
844
845 15 return 0;
846 }
847
848 static void svq1_flush(AVCodecContext *avctx)
849 {
850 SVQ1Context *s = avctx->priv_data;
851
852 av_frame_unref(s->prev);
853 }
854
855 const FFCodec ff_svq1_decoder = {
856 .p.name = "svq1",
857 CODEC_LONG_NAME("Sorenson Vector Quantizer 1 / Sorenson Video 1 / SVQ1"),
858 .p.type = AVMEDIA_TYPE_VIDEO,
859 .p.id = AV_CODEC_ID_SVQ1,
860 .priv_data_size = sizeof(SVQ1Context),
861 .init = svq1_decode_init,
862 .close = svq1_decode_end,
863 FF_CODEC_DECODE_CB(svq1_decode_frame),
864 .p.capabilities = AV_CODEC_CAP_DR1,
865 .flush = svq1_flush,
866 };
867