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