FFmpeg  4.4.9
svq1dec.c
Go to the documentation of this file.
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 "get_bits.h"
40 #include "h263.h"
41 #include "hpeldsp.h"
42 #include "internal.h"
43 #include "mathops.h"
44 #include "svq1.h"
45 
46 #define SVQ1_BLOCK_TYPE_VLC_BITS 3
53 
54 /* motion vector (prediction) */
55 typedef struct svq1_pmv_s {
56  int x;
57  int y;
58 } svq1_pmv;
59 
60 typedef struct SVQ1Context {
64 
67 
70 
71  int width;
72  int height;
74  int nonref; // 1 if the current frame won't be referenced
75 } SVQ1Context;
76 
77 static const uint8_t string_table[256] = {
78  0x00, 0xD5, 0x7F, 0xAA, 0xFE, 0x2B, 0x81, 0x54,
79  0x29, 0xFC, 0x56, 0x83, 0xD7, 0x02, 0xA8, 0x7D,
80  0x52, 0x87, 0x2D, 0xF8, 0xAC, 0x79, 0xD3, 0x06,
81  0x7B, 0xAE, 0x04, 0xD1, 0x85, 0x50, 0xFA, 0x2F,
82  0xA4, 0x71, 0xDB, 0x0E, 0x5A, 0x8F, 0x25, 0xF0,
83  0x8D, 0x58, 0xF2, 0x27, 0x73, 0xA6, 0x0C, 0xD9,
84  0xF6, 0x23, 0x89, 0x5C, 0x08, 0xDD, 0x77, 0xA2,
85  0xDF, 0x0A, 0xA0, 0x75, 0x21, 0xF4, 0x5E, 0x8B,
86  0x9D, 0x48, 0xE2, 0x37, 0x63, 0xB6, 0x1C, 0xC9,
87  0xB4, 0x61, 0xCB, 0x1E, 0x4A, 0x9F, 0x35, 0xE0,
88  0xCF, 0x1A, 0xB0, 0x65, 0x31, 0xE4, 0x4E, 0x9B,
89  0xE6, 0x33, 0x99, 0x4C, 0x18, 0xCD, 0x67, 0xB2,
90  0x39, 0xEC, 0x46, 0x93, 0xC7, 0x12, 0xB8, 0x6D,
91  0x10, 0xC5, 0x6F, 0xBA, 0xEE, 0x3B, 0x91, 0x44,
92  0x6B, 0xBE, 0x14, 0xC1, 0x95, 0x40, 0xEA, 0x3F,
93  0x42, 0x97, 0x3D, 0xE8, 0xBC, 0x69, 0xC3, 0x16,
94  0xEF, 0x3A, 0x90, 0x45, 0x11, 0xC4, 0x6E, 0xBB,
95  0xC6, 0x13, 0xB9, 0x6C, 0x38, 0xED, 0x47, 0x92,
96  0xBD, 0x68, 0xC2, 0x17, 0x43, 0x96, 0x3C, 0xE9,
97  0x94, 0x41, 0xEB, 0x3E, 0x6A, 0xBF, 0x15, 0xC0,
98  0x4B, 0x9E, 0x34, 0xE1, 0xB5, 0x60, 0xCA, 0x1F,
99  0x62, 0xB7, 0x1D, 0xC8, 0x9C, 0x49, 0xE3, 0x36,
100  0x19, 0xCC, 0x66, 0xB3, 0xE7, 0x32, 0x98, 0x4D,
101  0x30, 0xE5, 0x4F, 0x9A, 0xCE, 0x1B, 0xB1, 0x64,
102  0x72, 0xA7, 0x0D, 0xD8, 0x8C, 0x59, 0xF3, 0x26,
103  0x5B, 0x8E, 0x24, 0xF1, 0xA5, 0x70, 0xDA, 0x0F,
104  0x20, 0xF5, 0x5F, 0x8A, 0xDE, 0x0B, 0xA1, 0x74,
105  0x09, 0xDC, 0x76, 0xA3, 0xF7, 0x22, 0x88, 0x5D,
106  0xD6, 0x03, 0xA9, 0x7C, 0x28, 0xFD, 0x57, 0x82,
107  0xFF, 0x2A, 0x80, 0x55, 0x01, 0xD4, 0x7E, 0xAB,
108  0x84, 0x51, 0xFB, 0x2E, 0x7A, 0xAF, 0x05, 0xD0,
109  0xAD, 0x78, 0xD2, 0x07, 0x53, 0x86, 0x2C, 0xF9
110 };
111 
112 #define SVQ1_PROCESS_VECTOR() \
113  for (; level > 0; i++) { \
114  /* process next depth */ \
115  if (i == m) { \
116  m = n; \
117  if (--level == 0) \
118  break; \
119  } \
120  /* divide block if next bit set */ \
121  if (!get_bits1(bitbuf)) \
122  break; \
123  /* add child nodes */ \
124  list[n++] = list[i]; \
125  list[n++] = list[i] + (((level & 1) ? pitch : 1) << ((level >> 1) + 1));\
126  }
127 
128 #define SVQ1_ADD_CODEBOOK() \
129  /* add codebook entries to vector */ \
130  for (j = 0; j < stages; j++) { \
131  n3 = codebook[entries[j]] ^ 0x80808080; \
132  n1 += (n3 & 0xFF00FF00) >> 8; \
133  n2 += n3 & 0x00FF00FF; \
134  } \
135  \
136  /* clip to [0..255] */ \
137  if (n1 & 0xFF00FF00) { \
138  n3 = (n1 >> 15 & 0x00010001 | 0x01000100) - 0x00010001; \
139  n1 += 0x7F007F00; \
140  n1 |= (~n1 >> 15 & 0x00010001 | 0x01000100) - 0x00010001; \
141  n1 &= n3 & 0x00FF00FF; \
142  } \
143  \
144  if (n2 & 0xFF00FF00) { \
145  n3 = (n2 >> 15 & 0x00010001 | 0x01000100) - 0x00010001; \
146  n2 += 0x7F007F00; \
147  n2 |= (~n2 >> 15 & 0x00010001 | 0x01000100) - 0x00010001; \
148  n2 &= n3 & 0x00FF00FF; \
149  }
150 
151 #define SVQ1_CALC_CODEBOOK_ENTRIES(cbook) \
152  codebook = (const uint32_t *)cbook[level]; \
153  if (stages > 0) \
154  bit_cache = get_bits(bitbuf, 4 * stages); \
155  /* calculate codebook entries for this vector */ \
156  for (j = 0; j < stages; j++) { \
157  entries[j] = (((bit_cache >> (4 * (stages - j - 1))) & 0xF) + \
158  16 * j) << (level + 1); \
159  } \
160  mean -= stages * 128; \
161  n4 = (mean << 16) + mean;
162 
163 static int svq1_decode_block_intra(GetBitContext *bitbuf, uint8_t *pixels,
164  ptrdiff_t pitch)
165 {
166  uint32_t bit_cache;
167  uint8_t *list[63];
168  uint32_t *dst;
169  const uint32_t *codebook;
170  int entries[6];
171  int i, j, m, n;
172  int stages;
173  unsigned mean;
174  unsigned x, y, width, height, level;
175  uint32_t n1, n2, n3, n4;
176 
177  /* initialize list for breadth first processing of vectors */
178  list[0] = pixels;
179 
180  /* recursively process vector */
181  for (i = 0, m = 1, n = 1, level = 5; i < n; i++) {
183 
184  /* destination address and vector size */
185  dst = (uint32_t *)list[i];
186  width = 1 << ((4 + level) / 2);
187  height = 1 << ((3 + level) / 2);
188 
189  /* get number of stages (-1 skips vector, 0 for mean only) */
190  stages = get_vlc2(bitbuf, svq1_intra_multistage[level].table, 3, 3) - 1;
191 
192  if (stages == -1) {
193  for (y = 0; y < height; y++)
194  memset(&dst[y * (pitch / 4)], 0, width);
195  continue; /* skip vector */
196  }
197 
198  if ((stages > 0 && level >= 4)) {
199  ff_dlog(NULL,
200  "Error (svq1_decode_block_intra): invalid vector: stages=%i level=%i\n",
201  stages, level);
202  return AVERROR_INVALIDDATA; /* invalid vector */
203  }
204  av_assert0(stages >= 0);
205 
206  mean = get_vlc2(bitbuf, svq1_intra_mean.table, 8, 3);
207 
208  if (stages == 0) {
209  for (y = 0; y < height; y++)
210  memset(&dst[y * (pitch / 4)], mean, width);
211  } else {
213 
214  for (y = 0; y < height; y++) {
215  for (x = 0; x < width / 4; x++, codebook++) {
216  n1 = n4;
217  n2 = n4;
219  /* store result */
220  dst[x] = n1 << 8 | n2;
221  }
222  dst += pitch / 4;
223  }
224  }
225  }
226 
227  return 0;
228 }
229 
231  ptrdiff_t pitch)
232 {
233  uint32_t bit_cache;
234  uint8_t *list[63];
235  uint32_t *dst;
236  const uint32_t *codebook;
237  int entries[6];
238  int i, j, m, n;
239  int stages;
240  unsigned mean;
241  int x, y, width, height, level;
242  uint32_t n1, n2, n3, n4;
243 
244  /* initialize list for breadth first processing of vectors */
245  list[0] = pixels;
246 
247  /* recursively process vector */
248  for (i = 0, m = 1, n = 1, level = 5; i < n; i++) {
250 
251  /* destination address and vector size */
252  dst = (uint32_t *)list[i];
253  width = 1 << ((4 + level) / 2);
254  height = 1 << ((3 + level) / 2);
255 
256  /* get number of stages (-1 skips vector, 0 for mean only) */
257  stages = get_vlc2(bitbuf, svq1_inter_multistage[level].table, 3, 2) - 1;
258 
259  if (stages == -1)
260  continue; /* skip vector */
261 
262  if ((stages > 0 && level >= 4)) {
263  ff_dlog(NULL,
264  "Error (svq1_decode_block_non_intra): invalid vector: stages=%i level=%i\n",
265  stages, level);
266  return AVERROR_INVALIDDATA; /* invalid vector */
267  }
268  av_assert0(stages >= 0);
269 
270  mean = get_vlc2(bitbuf, svq1_inter_mean.table, 9, 3) - 256;
271 
273 
274  for (y = 0; y < height; y++) {
275  for (x = 0; x < width / 4; x++) {
276  n3 = dst[x];
277  /* add mean value to vector */
278  n1 = n4 + ((n3 & 0xFF00FF00) >> 8);
279  n2 = n4 + (n3 & 0x00FF00FF);
281  /* store result */
282  dst[x] = n1 << 8 | n2;
283  if (codebook != NULL)
284  codebook++;
285  }
286  dst += pitch / 4;
287  }
288  }
289  return 0;
290 }
291 
293  svq1_pmv **pmv)
294 {
295  int diff;
296  int i;
297 
298  for (i = 0; i < 2; i++) {
299  /* get motion code */
300  diff = get_vlc2(bitbuf, svq1_motion_component.table, 7, 2);
301  if (diff < 0)
302  return AVERROR_INVALIDDATA;
303  else if (diff) {
304  if (get_bits1(bitbuf))
305  diff = -diff;
306  }
307 
308  /* add median of motion vector predictors and clip result */
309  if (i == 1)
310  mv->y = sign_extend(diff + mid_pred(pmv[0]->y, pmv[1]->y, pmv[2]->y), 6);
311  else
312  mv->x = sign_extend(diff + mid_pred(pmv[0]->x, pmv[1]->x, pmv[2]->x), 6);
313  }
314 
315  return 0;
316 }
317 
318 static void svq1_skip_block(uint8_t *current, uint8_t *previous,
319  ptrdiff_t pitch, int x, int y)
320 {
321  uint8_t *src;
322  uint8_t *dst;
323  int i;
324 
325  src = &previous[x + y * pitch];
326  dst = current;
327 
328  for (i = 0; i < 16; i++) {
329  memcpy(dst, src, 16);
330  src += pitch;
331  dst += pitch;
332  }
333 }
334 
336  uint8_t *current, uint8_t *previous,
337  ptrdiff_t pitch, svq1_pmv *motion, int x, int y,
338  int width, int height)
339 {
340  uint8_t *src;
341  uint8_t *dst;
342  svq1_pmv mv;
343  svq1_pmv *pmv[3];
344  int result;
345 
346  /* predict and decode motion vector */
347  pmv[0] = &motion[0];
348  if (y == 0) {
349  pmv[1] =
350  pmv[2] = pmv[0];
351  } else {
352  pmv[1] = &motion[x / 8 + 2];
353  pmv[2] = &motion[x / 8 + 4];
354  }
355 
356  result = svq1_decode_motion_vector(bitbuf, &mv, pmv);
357  if (result)
358  return result;
359 
360  motion[0].x =
361  motion[x / 8 + 2].x =
362  motion[x / 8 + 3].x = mv.x;
363  motion[0].y =
364  motion[x / 8 + 2].y =
365  motion[x / 8 + 3].y = mv.y;
366 
367  mv.x = av_clip(mv.x, -2 * x, 2 * (width - x - 16));
368  mv.y = av_clip(mv.y, -2 * y, 2 * (height - y - 16));
369 
370  src = &previous[(x + (mv.x >> 1)) + (y + (mv.y >> 1)) * pitch];
371  dst = current;
372 
373  hdsp->put_pixels_tab[0][(mv.y & 1) << 1 | (mv.x & 1)](dst, src, pitch, 16);
374 
375  return 0;
376 }
377 
379  uint8_t *current, uint8_t *previous,
380  ptrdiff_t pitch, svq1_pmv *motion, int x, int y,
381  int width, int height)
382 {
383  uint8_t *src;
384  uint8_t *dst;
385  svq1_pmv mv;
386  svq1_pmv *pmv[4];
387  int i, result;
388 
389  /* predict and decode motion vector (0) */
390  pmv[0] = &motion[0];
391  if (y == 0) {
392  pmv[1] =
393  pmv[2] = pmv[0];
394  } else {
395  pmv[1] = &motion[(x / 8) + 2];
396  pmv[2] = &motion[(x / 8) + 4];
397  }
398 
399  result = svq1_decode_motion_vector(bitbuf, &mv, pmv);
400  if (result)
401  return result;
402 
403  /* predict and decode motion vector (1) */
404  pmv[0] = &mv;
405  if (y == 0) {
406  pmv[1] =
407  pmv[2] = pmv[0];
408  } else {
409  pmv[1] = &motion[(x / 8) + 3];
410  }
411  result = svq1_decode_motion_vector(bitbuf, &motion[0], pmv);
412  if (result)
413  return result;
414 
415  /* predict and decode motion vector (2) */
416  pmv[1] = &motion[0];
417  pmv[2] = &motion[(x / 8) + 1];
418 
419  result = svq1_decode_motion_vector(bitbuf, &motion[(x / 8) + 2], pmv);
420  if (result)
421  return result;
422 
423  /* predict and decode motion vector (3) */
424  pmv[2] = &motion[(x / 8) + 2];
425  pmv[3] = &motion[(x / 8) + 3];
426 
427  result = svq1_decode_motion_vector(bitbuf, pmv[3], pmv);
428  if (result)
429  return result;
430 
431  /* form predictions */
432  for (i = 0; i < 4; i++) {
433  int mvx = pmv[i]->x + (i & 1) * 16;
434  int mvy = pmv[i]->y + (i >> 1) * 16;
435 
436  // FIXME: clipping or padding?
437  mvx = av_clip(mvx, -2 * x, 2 * (width - x - 8));
438  mvy = av_clip(mvy, -2 * y, 2 * (height - y - 8));
439 
440  src = &previous[(x + (mvx >> 1)) + (y + (mvy >> 1)) * pitch];
441  dst = current;
442 
443  hdsp->put_pixels_tab[1][((mvy & 1) << 1) | (mvx & 1)](dst, src, pitch, 8);
444 
445  /* select next block */
446  if (i & 1)
447  current += 8 * (pitch - 1);
448  else
449  current += 8;
450  }
451 
452  return 0;
453 }
454 
456  GetBitContext *bitbuf,
457  uint8_t *current, uint8_t *previous,
458  ptrdiff_t pitch, svq1_pmv *motion, int x, int y,
459  int width, int height)
460 {
461  uint32_t block_type;
462  int result = 0;
463 
464  /* get block type */
465  block_type = get_vlc2(bitbuf, svq1_block_type.table,
467 
468  /* reset motion vectors */
469  if (block_type == SVQ1_BLOCK_SKIP || block_type == SVQ1_BLOCK_INTRA) {
470  motion[0].x =
471  motion[0].y =
472  motion[x / 8 + 2].x =
473  motion[x / 8 + 2].y =
474  motion[x / 8 + 3].x =
475  motion[x / 8 + 3].y = 0;
476  }
477 
478  switch (block_type) {
479  case SVQ1_BLOCK_SKIP:
480  svq1_skip_block(current, previous, pitch, x, y);
481  break;
482 
483  case SVQ1_BLOCK_INTER:
484  result = svq1_motion_inter_block(hdsp, bitbuf, current, previous,
485  pitch, motion, x, y, width, height);
486 
487  if (result != 0) {
488  ff_dlog(avctx, "Error in svq1_motion_inter_block %i\n", result);
489  break;
490  }
491  result = svq1_decode_block_non_intra(bitbuf, current, pitch);
492  break;
493 
494  case SVQ1_BLOCK_INTER_4V:
495  result = svq1_motion_inter_4v_block(hdsp, bitbuf, current, previous,
496  pitch, motion, x, y, width, height);
497 
498  if (result != 0) {
499  ff_dlog(avctx, "Error in svq1_motion_inter_4v_block %i\n", result);
500  break;
501  }
502  result = svq1_decode_block_non_intra(bitbuf, current, pitch);
503  break;
504 
505  case SVQ1_BLOCK_INTRA:
506  result = svq1_decode_block_intra(bitbuf, current, pitch);
507  break;
508  }
509 
510  return result;
511 }
512 
513 static void svq1_parse_string(GetBitContext *bitbuf, uint8_t out[257])
514 {
515  uint8_t seed;
516  int i;
517 
518  out[0] = get_bits(bitbuf, 8);
519  seed = string_table[out[0]];
520 
521  for (i = 1; i <= out[0]; i++) {
522  out[i] = get_bits(bitbuf, 8) ^ seed;
523  seed = string_table[out[i] ^ seed];
524  }
525  out[i] = 0;
526 }
527 
529 {
530  SVQ1Context *s = avctx->priv_data;
531  GetBitContext *bitbuf = &s->gb;
532  int frame_size_code;
533  int width = s->width;
534  int height = s->height;
535 
536  skip_bits(bitbuf, 8); /* temporal_reference */
537 
538  /* frame type */
539  s->nonref = 0;
540  switch (get_bits(bitbuf, 2)) {
541  case 0:
543  break;
544  case 2:
545  s->nonref = 1;
546  case 1:
548  break;
549  default:
550  av_log(avctx, AV_LOG_ERROR, "Invalid frame type.\n");
551  return AVERROR_INVALIDDATA;
552  }
553 
555  /* unknown fields */
556  if (s->frame_code == 0x50 || s->frame_code == 0x60) {
557  int csum = get_bits(bitbuf, 16);
558 
559  csum = av_bswap16(av_crc(av_crc_get_table(AV_CRC_16_CCITT), av_bswap16(csum), bitbuf->buffer, bitbuf->size_in_bits >> 3));
560 
561  ff_dlog(avctx, "%s checksum (%02x) for packet data\n",
562  (csum == 0) ? "correct" : "incorrect", csum);
563  }
564 
565  if ((s->frame_code ^ 0x10) >= 0x50) {
566  uint8_t msg[257];
567 
568  svq1_parse_string(bitbuf, msg);
569 
570  av_log(avctx, AV_LOG_INFO,
571  "embedded message:\n%s\n", ((char *)msg) + 1);
572  }
573 
574  skip_bits(bitbuf, 2);
575  skip_bits(bitbuf, 2);
576  skip_bits1(bitbuf);
577 
578  /* load frame size */
579  frame_size_code = get_bits(bitbuf, 3);
580 
581  if (frame_size_code == 7) {
582  /* load width, height (12 bits each) */
583  width = get_bits(bitbuf, 12);
584  height = get_bits(bitbuf, 12);
585 
586  if (!width || !height)
587  return AVERROR_INVALIDDATA;
588  } else {
589  /* get width, height from table */
590  width = ff_svq1_frame_size_table[frame_size_code][0];
591  height = ff_svq1_frame_size_table[frame_size_code][1];
592  }
593  }
594 
595  /* unknown fields */
596  if (get_bits1(bitbuf)) {
597  skip_bits1(bitbuf); /* use packet checksum if (1) */
598  skip_bits1(bitbuf); /* component checksums after image data if (1) */
599 
600  if (get_bits(bitbuf, 2) != 0)
601  return AVERROR_INVALIDDATA;
602  }
603 
604  if (get_bits1(bitbuf)) {
605  skip_bits1(bitbuf);
606  skip_bits(bitbuf, 4);
607  skip_bits1(bitbuf);
608  skip_bits(bitbuf, 2);
609 
610  if (skip_1stop_8data_bits(bitbuf) < 0)
611  return AVERROR_INVALIDDATA;
612  }
613  if (get_bits_left(bitbuf) <= 0)
614  return AVERROR_INVALIDDATA;
615 
616  s->width = width;
617  s->height = height;
618  return 0;
619 }
620 
621 static int svq1_decode_frame(AVCodecContext *avctx, void *data,
622  int *got_frame, AVPacket *avpkt)
623 {
624  const uint8_t *buf = avpkt->data;
625  int buf_size = avpkt->size;
626  SVQ1Context *s = avctx->priv_data;
627  AVFrame *cur = data;
628  uint8_t *current;
629  int result, i, x, y, width, height;
630  int ret;
631 
632  /* initialize bit buffer */
633  ret = init_get_bits8(&s->gb, buf, buf_size);
634  if (ret < 0)
635  return ret;
636 
637  /* decode frame header */
638  s->frame_code = get_bits(&s->gb, 22);
639 
640  if ((s->frame_code & ~0x70) || !(s->frame_code & 0x60))
641  return AVERROR_INVALIDDATA;
642 
643  /* swap some header bytes (why?) */
644  if (s->frame_code != 0x20) {
645  uint32_t *src;
646 
647  if (buf_size < 9 * 4) {
648  av_log(avctx, AV_LOG_ERROR, "Input packet too small\n");
649  return AVERROR_INVALIDDATA;
650  }
651 
652  av_fast_padded_malloc(&s->pkt_swapped,
653  &s->pkt_swapped_allocated,
654  buf_size);
655  if (!s->pkt_swapped)
656  return AVERROR(ENOMEM);
657 
658  memcpy(s->pkt_swapped, buf, buf_size);
659  buf = s->pkt_swapped;
660  init_get_bits(&s->gb, buf, buf_size * 8);
661  skip_bits(&s->gb, 22);
662 
663  src = (uint32_t *)(s->pkt_swapped + 4);
664 
665  for (i = 0; i < 4; i++)
666  src[i] = ((src[i] << 16) | (src[i] >> 16)) ^ src[7 - i];
667  }
668 
669  result = svq1_decode_frame_header(avctx, cur);
670  if (result != 0) {
671  ff_dlog(avctx, "Error in svq1_decode_frame_header %i\n", result);
672  return result;
673  }
674 
675  result = ff_set_dimensions(avctx, s->width, s->height);
676  if (result < 0)
677  return result;
678 
679  if ((avctx->skip_frame >= AVDISCARD_NONREF && s->nonref) ||
680  (avctx->skip_frame >= AVDISCARD_NONKEY &&
681  cur->pict_type != AV_PICTURE_TYPE_I) ||
682  avctx->skip_frame >= AVDISCARD_ALL)
683  return buf_size;
684 
685  // Reject obviously too-small packets early: require at least one remaining bit per aligned luma macroblock.
686  // FFALIGN(s->width, 16) * FFALIGN(s->height, 16) / 256 represent the number of Macroblocks
687  if (get_bits_left(&s->gb) < FFALIGN(s->width, 16) * FFALIGN(s->height, 16) / 256)
688  return AVERROR_INVALIDDATA;
689 
690  result = ff_get_buffer(avctx, cur, s->nonref ? 0 : AV_GET_BUFFER_FLAG_REF);
691  if (result < 0)
692  return result;
693 
694  av_fast_padded_malloc(&s->pmv, &s->pmv_allocated, (FFALIGN(s->width, 16) / 8 + 3) * sizeof(*s->pmv));
695  if (!s->pmv)
696  return AVERROR(ENOMEM);
697 
698  /* decode y, u and v components */
699  for (i = 0; i < 3; i++) {
700  int linesize = cur->linesize[i];
701  if (i == 0) {
702  width = FFALIGN(s->width, 16);
703  height = FFALIGN(s->height, 16);
704  } else {
705  if (avctx->flags & AV_CODEC_FLAG_GRAY)
706  break;
707  width = FFALIGN(s->width / 4, 16);
708  height = FFALIGN(s->height / 4, 16);
709  }
710 
711  current = cur->data[i];
712 
713  if (cur->pict_type == AV_PICTURE_TYPE_I) {
714  /* keyframe */
715  for (y = 0; y < height; y += 16) {
716  for (x = 0; x < width; x += 16) {
717  result = svq1_decode_block_intra(&s->gb, &current[x],
718  linesize);
719  if (result) {
720  av_log(avctx, AV_LOG_ERROR,
721  "Error in svq1_decode_block %i (keyframe)\n",
722  result);
723  return result;
724  }
725  }
726  current += 16 * linesize;
727  }
728  } else {
729  /* delta frame */
730  uint8_t *previous = s->prev->data[i];
731  if (!previous ||
732  s->prev->width != s->width || s->prev->height != s->height) {
733  av_log(avctx, AV_LOG_ERROR, "Missing reference frame.\n");
734  return AVERROR_INVALIDDATA;
735  }
736 
737  memset(s->pmv, 0, ((width / 8) + 3) * sizeof(svq1_pmv));
738 
739  for (y = 0; y < height; y += 16) {
740  for (x = 0; x < width; x += 16) {
741  result = svq1_decode_delta_block(avctx, &s->hdsp,
742  &s->gb, &current[x],
743  previous, linesize,
744  s->pmv, x, y, width, height);
745  if (result != 0) {
746  ff_dlog(avctx,
747  "Error in svq1_decode_delta_block %i\n",
748  result);
749  return result;
750  }
751  }
752 
753  s->pmv[0].x =
754  s->pmv[0].y = 0;
755 
756  current += 16 * linesize;
757  }
758  }
759  }
760 
761  if (!s->nonref) {
762  av_frame_unref(s->prev);
763  result = av_frame_ref(s->prev, cur);
764  if (result < 0)
765  return result;
766  }
767 
768  *got_frame = 1;
769  result = buf_size;
770 
771  return result;
772 }
773 
774 static av_cold void svq1_static_init(void)
775 {
777  &ff_svq1_block_type_vlc[0][1], 2, 1,
778  &ff_svq1_block_type_vlc[0][0], 2, 1, 8);
779 
781  &ff_mvtab[0][1], 2, 1,
782  &ff_mvtab[0][0], 2, 1, 176);
783 
784  for (int i = 0, offset = 0; i < 6; i++) {
785  static const uint8_t sizes[2][6] = { { 14, 10, 14, 18, 16, 18 },
786  { 10, 10, 14, 14, 14, 16 } };
787  static VLC_TYPE table[168][2];
790  offset += sizes[0][i];
792  &ff_svq1_intra_multistage_vlc[i][0][1], 2, 1,
793  &ff_svq1_intra_multistage_vlc[i][0][0], 2, 1,
797  offset += sizes[1][i];
799  &ff_svq1_inter_multistage_vlc[i][0][1], 2, 1,
800  &ff_svq1_inter_multistage_vlc[i][0][0], 2, 1,
802  }
803 
805  &ff_svq1_intra_mean_vlc[0][1], 4, 2,
806  &ff_svq1_intra_mean_vlc[0][0], 4, 2, 632);
807 
809  &ff_svq1_inter_mean_vlc[0][1], 4, 2,
810  &ff_svq1_inter_mean_vlc[0][0], 4, 2, 1434);
811 }
812 
814 {
815  static AVOnce init_static_once = AV_ONCE_INIT;
816  SVQ1Context *s = avctx->priv_data;
817 
818  s->prev = av_frame_alloc();
819  if (!s->prev)
820  return AVERROR(ENOMEM);
821 
822  s->width = avctx->width + 3 & ~3;
823  s->height = avctx->height + 3 & ~3;
824  avctx->pix_fmt = AV_PIX_FMT_YUV410P;
825 
826  ff_hpeldsp_init(&s->hdsp, avctx->flags);
827 
828  ff_thread_once(&init_static_once, svq1_static_init);
829 
830  return 0;
831 }
832 
834 {
835  SVQ1Context *s = avctx->priv_data;
836 
837  av_frame_free(&s->prev);
838  av_freep(&s->pkt_swapped);
839  s->pkt_swapped_allocated = 0;
840  av_freep(&s->pmv);
841  s->pmv_allocated = 0;
842 
843  return 0;
844 }
845 
846 static void svq1_flush(AVCodecContext *avctx)
847 {
848  SVQ1Context *s = avctx->priv_data;
849 
850  av_frame_unref(s->prev);
851 }
852 
854  .name = "svq1",
855  .long_name = NULL_IF_CONFIG_SMALL("Sorenson Vector Quantizer 1 / Sorenson Video 1 / SVQ1"),
856  .type = AVMEDIA_TYPE_VIDEO,
857  .id = AV_CODEC_ID_SVQ1,
858  .priv_data_size = sizeof(SVQ1Context),
860  .close = svq1_decode_end,
862  .capabilities = AV_CODEC_CAP_DR1,
863  .flush = svq1_flush,
864  .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV410P,
865  AV_PIX_FMT_NONE },
866  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
867 };
static void flush(AVCodecContext *avctx)
#define av_cold
Definition: attributes.h:88
uint8_t
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
Libavcodec external API header.
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:31
#define s(width, name)
Definition: cbs_vp9.c:257
static const unsigned codebook[256][2]
Definition: cfhdenc.c:42
#define av_clip
Definition: common.h:122
#define NULL
Definition: coverity.c:32
Public header for CRC hash function implementation.
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1911
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:71
static AVFrame * frame
bitstream reader API header.
static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE(*table)[2], int bits, int max_depth)
Parse a vlc code.
Definition: get_bits.h:797
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:849
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:498
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:467
static int skip_1stop_8data_bits(GetBitContext *gb)
Definition: get_bits.h:854
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:677
static void skip_bits1(GetBitContext *s)
Definition: get_bits.h:538
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:379
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:659
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
Definition: avcodec.h:514
#define AV_CODEC_FLAG_GRAY
Only decode/encode grayscale.
Definition: avcodec.h:308
@ AV_CODEC_ID_SVQ1
Definition: codec_id.h:71
@ AVDISCARD_ALL
discard all
Definition: avcodec.h:236
@ AVDISCARD_NONKEY
discard all frames except keyframes
Definition: avcodec.h:235
@ AVDISCARD_NONREF
discard all non reference
Definition: avcodec.h:232
void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
Same behaviour av_fast_malloc but the buffer has additional AV_INPUT_BUFFER_PADDING_SIZE at the end w...
Definition: utils.c:50
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition: crc.c:374
uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length)
Calculate the CRC of a block.
Definition: crc.c:394
@ AV_CRC_16_CCITT
Definition: crc.h:52
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define AVERROR(e)
Definition: error.h:43
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:553
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition: frame.c:443
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:203
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:190
#define AV_LOG_INFO
Standard information.
Definition: log.h:205
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
@ AV_PICTURE_TYPE_P
Predicted.
Definition: avutil.h:275
const uint8_t ff_mvtab[33][2]
Definition: h263data.c:88
av_cold void ff_hpeldsp_init(HpelDSPContext *c, int flags)
Definition: hpeldsp.c:338
static const int sizes[][2]
Definition: img2dec.c:54
int i
Definition: input.c:407
static const int8_t mv[256][2]
Definition: 4xm.c:78
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:41
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Check that the provided frame dimensions are valid and set them on the codec context.
Definition: utils.c:84
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
#define AVOnce
Definition: thread.h:172
static int ff_thread_once(char *control, void(*routine)(void))
Definition: thread.h:175
#define AV_ONCE_INIT
Definition: thread.h:173
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:309
#define FFALIGN(x, a)
Definition: macros.h:48
static av_const int sign_extend(int val, unsigned bits)
Definition: mathops.h:130
#define mid_pred
Definition: mathops.h:97
const char data[16]
Definition: mxf.c:142
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
@ AV_PIX_FMT_YUV410P
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:72
static const uint16_t table[]
Definition: prosumer.c:206
#define av_bswap16
Definition: bswap.h:31
main external API structure.
Definition: avcodec.h:536
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:746
int width
picture width / height.
Definition: avcodec.h:709
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:616
void * priv_data
Definition: avcodec.h:563
enum AVDiscard skip_frame
Skip decoding for selected frames.
Definition: avcodec.h:2010
AVCodec.
Definition: codec.h:197
const char * name
Name of the codec implementation.
Definition: codec.h:204
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:332
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:349
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:401
This structure stores compressed data.
Definition: packet.h:346
int size
Definition: packet.h:370
uint8_t * data
Definition: packet.h:369
const uint8_t * buffer
Definition: get_bits.h:62
int size_in_bits
Definition: get_bits.h:68
Half-pel DSP context.
Definition: hpeldsp.h:45
op_pixels_func put_pixels_tab[4][4]
Halfpel motion compensation with rounding (a+b+1)>>1.
Definition: hpeldsp.h:56
HpelDSPContext hdsp
Definition: svq1dec.c:61
int pmv_allocated
Definition: svq1dec.c:69
uint8_t * pkt_swapped
Definition: svq1dec.c:65
int width
Definition: svq1dec.c:71
int pkt_swapped_allocated
Definition: svq1dec.c:66
int height
Definition: svq1dec.c:72
GetBitContext gb
Definition: svq1dec.c:62
int frame_code
Definition: svq1dec.c:73
AVFrame * prev
Definition: svq1dec.c:63
svq1_pmv * pmv
Definition: svq1dec.c:68
int nonref
Definition: svq1dec.c:74
Definition: vlc.h:26
int table_allocated
Definition: vlc.h:29
VLC_TYPE(* table)[2]
code, bits
Definition: vlc.h:28
int y
Definition: svq1dec.c:57
int x
Definition: svq1dec.c:56
const uint16_t ff_svq1_frame_size_table[7][2]
Definition: svq1.c:40
Sorenson Vector Quantizer #1 (SVQ1) video codec.
#define SVQ1_BLOCK_INTER_4V
Definition: svq1.h:42
#define SVQ1_BLOCK_INTER
Definition: svq1.h:41
const uint8_t ff_svq1_intra_multistage_vlc[6][8][2]
Definition: svq1_vlc.h:33
const uint8_t ff_svq1_inter_multistage_vlc[6][8][2]
Definition: svq1_vlc.h:50
const int8_t *const ff_svq1_intra_codebooks[6]
Definition: svq1_cb.h:1519
const int8_t *const ff_svq1_inter_codebooks[6]
Definition: svq1_cb.h:776
#define SVQ1_BLOCK_INTRA
Definition: svq1.h:43
#define SVQ1_BLOCK_SKIP
Definition: svq1.h:40
const uint16_t ff_svq1_inter_mean_vlc[512][2]
Definition: svq1_vlc.h:136
const uint16_t ff_svq1_intra_mean_vlc[256][2]
Definition: svq1_vlc.h:67
const uint8_t ff_svq1_block_type_vlc[4][2]
Definition: svq1_vlc.h:27
static int svq1_motion_inter_block(HpelDSPContext *hdsp, GetBitContext *bitbuf, uint8_t *current, uint8_t *previous, ptrdiff_t pitch, svq1_pmv *motion, int x, int y, int width, int height)
Definition: svq1dec.c:335
static av_cold int svq1_decode_end(AVCodecContext *avctx)
Definition: svq1dec.c:833
static VLC svq1_inter_multistage[6]
Definition: svq1dec.c:50
static int svq1_decode_frame_header(AVCodecContext *avctx, AVFrame *frame)
Definition: svq1dec.c:528
static int svq1_decode_motion_vector(GetBitContext *bitbuf, svq1_pmv *mv, svq1_pmv **pmv)
Definition: svq1dec.c:292
static VLC svq1_inter_mean
Definition: svq1dec.c:52
static av_cold int svq1_decode_init(AVCodecContext *avctx)
Definition: svq1dec.c:813
static const uint8_t string_table[256]
Definition: svq1dec.c:77
AVCodec ff_svq1_decoder
Definition: svq1dec.c:853
static int svq1_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: svq1dec.c:621
#define SVQ1_CALC_CODEBOOK_ENTRIES(cbook)
Definition: svq1dec.c:151
#define SVQ1_BLOCK_TYPE_VLC_BITS
Definition: svq1dec.c:46
static void svq1_flush(AVCodecContext *avctx)
Definition: svq1dec.c:846
static VLC svq1_motion_component
Definition: svq1dec.c:48
static int svq1_decode_block_non_intra(GetBitContext *bitbuf, uint8_t *pixels, ptrdiff_t pitch)
Definition: svq1dec.c:230
static void svq1_parse_string(GetBitContext *bitbuf, uint8_t out[257])
Definition: svq1dec.c:513
#define SVQ1_ADD_CODEBOOK()
Definition: svq1dec.c:128
static int svq1_decode_block_intra(GetBitContext *bitbuf, uint8_t *pixels, ptrdiff_t pitch)
Definition: svq1dec.c:163
static void svq1_skip_block(uint8_t *current, uint8_t *previous, ptrdiff_t pitch, int x, int y)
Definition: svq1dec.c:318
static av_cold void svq1_static_init(void)
Definition: svq1dec.c:774
static VLC svq1_intra_multistage[6]
Definition: svq1dec.c:49
static int svq1_decode_delta_block(AVCodecContext *avctx, HpelDSPContext *hdsp, GetBitContext *bitbuf, uint8_t *current, uint8_t *previous, ptrdiff_t pitch, svq1_pmv *motion, int x, int y, int width, int height)
Definition: svq1dec.c:455
#define SVQ1_PROCESS_VECTOR()
Definition: svq1dec.c:112
static VLC svq1_intra_mean
Definition: svq1dec.c:51
static int svq1_motion_inter_4v_block(HpelDSPContext *hdsp, GetBitContext *bitbuf, uint8_t *current, uint8_t *previous, ptrdiff_t pitch, svq1_pmv *motion, int x, int y, int width, int height)
Definition: svq1dec.c:378
static VLC svq1_block_type
Definition: svq1dec.c:47
uint8_t level
Definition: svq3.c:206
#define ff_dlog(a,...)
#define av_freep(p)
#define av_log(a,...)
#define src
Definition: vp8dsp.c:255
FILE * out
Definition: movenc.c:54
#define height
#define width
static float mean(const float *input, int size)
Definition: vf_nnedi.c:867
static av_always_inline int diff(const uint32_t a, const uint32_t b)
static const uint8_t offset[127][2]
Definition: vf_spp.c:107
static unsigned int seed
Definition: videogen.c:78
#define init_vlc(vlc, nb_bits, nb_codes, bits, bits_wrap, bits_size, codes, codes_wrap, codes_size, flags)
Definition: vlc.h:38
#define INIT_VLC_STATIC(vlc, bits, a, b, c, d, e, f, g, static_size)
Definition: vlc.h:120
#define INIT_VLC_USE_NEW_STATIC
Definition: vlc.h:95
#define VLC_TYPE
Definition: vlc.h:24