]> git.sesse.net Git - ffmpeg/blob - libavcodec/svq1dec.c
4af8b42624a6d145ae4dd6bf92e845f6387012a5
[ffmpeg] / libavcodec / svq1dec.c
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
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
36 #include "avcodec.h"
37 #include "dsputil.h"
38 #include "mpegvideo.h"
39 #include "mathops.h"
40
41 #include "svq1.h"
42
43 #undef NDEBUG
44 #include <assert.h>
45
46 extern const uint8_t ff_mvtab[33][2];
47
48 static VLC svq1_block_type;
49 static VLC svq1_motion_component;
50 static VLC svq1_intra_multistage[6];
51 static VLC svq1_inter_multistage[6];
52 static VLC svq1_intra_mean;
53 static VLC svq1_inter_mean;
54
55 /* motion vector (prediction) */
56 typedef struct svq1_pmv_s {
57   int           x;
58   int           y;
59 } svq1_pmv;
60
61 static const uint8_t string_table[256] = {
62   0x00, 0xD5, 0x7F, 0xAA, 0xFE, 0x2B, 0x81, 0x54,
63   0x29, 0xFC, 0x56, 0x83, 0xD7, 0x02, 0xA8, 0x7D,
64   0x52, 0x87, 0x2D, 0xF8, 0xAC, 0x79, 0xD3, 0x06,
65   0x7B, 0xAE, 0x04, 0xD1, 0x85, 0x50, 0xFA, 0x2F,
66   0xA4, 0x71, 0xDB, 0x0E, 0x5A, 0x8F, 0x25, 0xF0,
67   0x8D, 0x58, 0xF2, 0x27, 0x73, 0xA6, 0x0C, 0xD9,
68   0xF6, 0x23, 0x89, 0x5C, 0x08, 0xDD, 0x77, 0xA2,
69   0xDF, 0x0A, 0xA0, 0x75, 0x21, 0xF4, 0x5E, 0x8B,
70   0x9D, 0x48, 0xE2, 0x37, 0x63, 0xB6, 0x1C, 0xC9,
71   0xB4, 0x61, 0xCB, 0x1E, 0x4A, 0x9F, 0x35, 0xE0,
72   0xCF, 0x1A, 0xB0, 0x65, 0x31, 0xE4, 0x4E, 0x9B,
73   0xE6, 0x33, 0x99, 0x4C, 0x18, 0xCD, 0x67, 0xB2,
74   0x39, 0xEC, 0x46, 0x93, 0xC7, 0x12, 0xB8, 0x6D,
75   0x10, 0xC5, 0x6F, 0xBA, 0xEE, 0x3B, 0x91, 0x44,
76   0x6B, 0xBE, 0x14, 0xC1, 0x95, 0x40, 0xEA, 0x3F,
77   0x42, 0x97, 0x3D, 0xE8, 0xBC, 0x69, 0xC3, 0x16,
78   0xEF, 0x3A, 0x90, 0x45, 0x11, 0xC4, 0x6E, 0xBB,
79   0xC6, 0x13, 0xB9, 0x6C, 0x38, 0xED, 0x47, 0x92,
80   0xBD, 0x68, 0xC2, 0x17, 0x43, 0x96, 0x3C, 0xE9,
81   0x94, 0x41, 0xEB, 0x3E, 0x6A, 0xBF, 0x15, 0xC0,
82   0x4B, 0x9E, 0x34, 0xE1, 0xB5, 0x60, 0xCA, 0x1F,
83   0x62, 0xB7, 0x1D, 0xC8, 0x9C, 0x49, 0xE3, 0x36,
84   0x19, 0xCC, 0x66, 0xB3, 0xE7, 0x32, 0x98, 0x4D,
85   0x30, 0xE5, 0x4F, 0x9A, 0xCE, 0x1B, 0xB1, 0x64,
86   0x72, 0xA7, 0x0D, 0xD8, 0x8C, 0x59, 0xF3, 0x26,
87   0x5B, 0x8E, 0x24, 0xF1, 0xA5, 0x70, 0xDA, 0x0F,
88   0x20, 0xF5, 0x5F, 0x8A, 0xDE, 0x0B, 0xA1, 0x74,
89   0x09, 0xDC, 0x76, 0xA3, 0xF7, 0x22, 0x88, 0x5D,
90   0xD6, 0x03, 0xA9, 0x7C, 0x28, 0xFD, 0x57, 0x82,
91   0xFF, 0x2A, 0x80, 0x55, 0x01, 0xD4, 0x7E, 0xAB,
92   0x84, 0x51, 0xFB, 0x2E, 0x7A, 0xAF, 0x05, 0xD0,
93   0xAD, 0x78, 0xD2, 0x07, 0x53, 0x86, 0x2C, 0xF9
94 };
95
96 #define SVQ1_PROCESS_VECTOR()\
97     for (; level > 0; i++) {\
98       /* process next depth */\
99       if (i == m) {\
100         m = n;\
101         if (--level == 0)\
102           break;\
103       }\
104       /* divide block if next bit set */\
105       if (get_bits1 (bitbuf) == 0)\
106         break;\
107       /* add child nodes */\
108       list[n++] = list[i];\
109       list[n++] = list[i] + (((level & 1) ? pitch : 1) << ((level / 2) + 1));\
110     }
111
112 #define SVQ1_ADD_CODEBOOK()\
113           /* add codebook entries to vector */\
114           for (j=0; j < stages; j++) {\
115             n3  = codebook[entries[j]] ^ 0x80808080;\
116             n1 += ((n3 & 0xFF00FF00) >> 8);\
117             n2 +=  (n3 & 0x00FF00FF);\
118           }\
119 \
120           /* clip to [0..255] */\
121           if (n1 & 0xFF00FF00) {\
122             n3  = ((( n1 >> 15) & 0x00010001) | 0x01000100) - 0x00010001;\
123             n1 += 0x7F007F00;\
124             n1 |= (((~n1 >> 15) & 0x00010001) | 0x01000100) - 0x00010001;\
125             n1 &= (n3 & 0x00FF00FF);\
126           }\
127 \
128           if (n2 & 0xFF00FF00) {\
129             n3  = ((( n2 >> 15) & 0x00010001) | 0x01000100) - 0x00010001;\
130             n2 += 0x7F007F00;\
131             n2 |= (((~n2 >> 15) & 0x00010001) | 0x01000100) - 0x00010001;\
132             n2 &= (n3 & 0x00FF00FF);\
133           }
134
135 #define SVQ1_DO_CODEBOOK_INTRA()\
136       for (y=0; y < height; y++) {\
137         for (x=0; x < (width / 4); x++, codebook++) {\
138         n1 = n4;\
139         n2 = n4;\
140         SVQ1_ADD_CODEBOOK()\
141         /* store result */\
142         dst[x] = (n1 << 8) | n2;\
143         }\
144         dst += (pitch / 4);\
145       }
146
147 #define SVQ1_DO_CODEBOOK_NONINTRA()\
148       for (y=0; y < height; y++) {\
149         for (x=0; x < (width / 4); x++, codebook++) {\
150         n3 = dst[x];\
151         /* add mean value to vector */\
152         n1 = ((n3 & 0xFF00FF00) >> 8) + n4;\
153         n2 =  (n3 & 0x00FF00FF)          + n4;\
154         SVQ1_ADD_CODEBOOK()\
155         /* store result */\
156         dst[x] = (n1 << 8) | n2;\
157         }\
158         dst += (pitch / 4);\
159       }
160
161 #define SVQ1_CALC_CODEBOOK_ENTRIES(cbook)\
162       codebook = (const uint32_t *) cbook[level];\
163       if (stages > 0)\
164         bit_cache = get_bits (bitbuf, 4*stages);\
165       /* calculate codebook entries for this vector */\
166       for (j=0; j < stages; j++) {\
167         entries[j] = (((bit_cache >> (4*(stages - j - 1))) & 0xF) + 16*j) << (level + 1);\
168       }\
169       mean -= (stages * 128);\
170       n4    = ((mean + (mean >> 31)) << 16) | (mean & 0xFFFF);
171
172 static int svq1_decode_block_intra (GetBitContext *bitbuf, uint8_t *pixels, int pitch ) {
173   uint32_t    bit_cache;
174   uint8_t    *list[63];
175   uint32_t   *dst;
176   const uint32_t *codebook;
177   int         entries[6];
178   int         i, j, m, n;
179   int         mean, stages;
180   unsigned    x, y, width, height, level;
181   uint32_t    n1, n2, n3, n4;
182
183   /* initialize list for breadth first processing of vectors */
184   list[0] = pixels;
185
186   /* recursively process vector */
187   for (i=0, m=1, n=1, level=5; i < n; i++) {
188     SVQ1_PROCESS_VECTOR();
189
190     /* destination address and vector size */
191     dst = (uint32_t *) list[i];
192     width = 1 << ((4 + level) /2);
193     height = 1 << ((3 + level) /2);
194
195     /* get number of stages (-1 skips vector, 0 for mean only) */
196     stages = get_vlc2(bitbuf, svq1_intra_multistage[level].table, 3, 3) - 1;
197
198     if (stages == -1) {
199         for (y=0; y < height; y++) {
200           memset (&dst[y*(pitch / 4)], 0, width);
201         }
202       continue;                 /* skip vector */
203     }
204
205     if ((stages > 0) && (level >= 4)) {
206       av_dlog(NULL,
207               "Error (svq1_decode_block_intra): invalid vector: stages=%i level=%i\n",
208               stages, level);
209       return -1;        /* invalid vector */
210     }
211
212     mean = get_vlc2(bitbuf, svq1_intra_mean.table, 8, 3);
213
214     if (stages == 0) {
215       for (y=0; y < height; y++) {
216         memset (&dst[y*(pitch / 4)], mean, width);
217       }
218     } else {
219       SVQ1_CALC_CODEBOOK_ENTRIES(ff_svq1_intra_codebooks);
220       SVQ1_DO_CODEBOOK_INTRA()
221     }
222   }
223
224   return 0;
225 }
226
227 static int svq1_decode_block_non_intra (GetBitContext *bitbuf, uint8_t *pixels, int pitch ) {
228   uint32_t    bit_cache;
229   uint8_t    *list[63];
230   uint32_t   *dst;
231   const uint32_t *codebook;
232   int         entries[6];
233   int         i, j, m, n;
234   int         mean, stages;
235   int         x, y, width, height, level;
236   uint32_t    n1, n2, n3, n4;
237
238   /* initialize list for breadth first processing of vectors */
239   list[0] = pixels;
240
241   /* recursively process vector */
242   for (i=0, m=1, n=1, level=5; i < n; i++) {
243     SVQ1_PROCESS_VECTOR();
244
245     /* destination address and vector size */
246     dst = (uint32_t *) list[i];
247     width = 1 << ((4 + level) /2);
248     height = 1 << ((3 + level) /2);
249
250     /* get number of stages (-1 skips vector, 0 for mean only) */
251     stages = get_vlc2(bitbuf, svq1_inter_multistage[level].table, 3, 2) - 1;
252
253     if (stages == -1) continue; /* skip vector */
254
255     if ((stages > 0) && (level >= 4)) {
256       av_dlog(NULL,
257               "Error (svq1_decode_block_non_intra): invalid vector: stages=%i level=%i\n",
258               stages, level);
259       return -1;        /* invalid vector */
260     }
261
262     mean = get_vlc2(bitbuf, svq1_inter_mean.table, 9, 3) - 256;
263
264     SVQ1_CALC_CODEBOOK_ENTRIES(ff_svq1_inter_codebooks);
265     SVQ1_DO_CODEBOOK_NONINTRA()
266   }
267   return 0;
268 }
269
270 static int svq1_decode_motion_vector (GetBitContext *bitbuf, svq1_pmv *mv, svq1_pmv **pmv) {
271   int        diff;
272   int        i;
273
274   for (i=0; i < 2; i++) {
275
276     /* get motion code */
277     diff = get_vlc2(bitbuf, svq1_motion_component.table, 7, 2);
278     if(diff<0)
279         return -1;
280     else if(diff){
281         if(get_bits1(bitbuf)) diff= -diff;
282     }
283
284     /* add median of motion vector predictors and clip result */
285     if (i == 1)
286       mv->y = sign_extend(diff + mid_pred(pmv[0]->y, pmv[1]->y, pmv[2]->y), 6);
287     else
288       mv->x = sign_extend(diff + mid_pred(pmv[0]->x, pmv[1]->x, pmv[2]->x), 6);
289   }
290
291   return 0;
292 }
293
294 static void svq1_skip_block (uint8_t *current, uint8_t *previous, int pitch, int x, int y) {
295   uint8_t *src;
296   uint8_t *dst;
297   int      i;
298
299   src = &previous[x + y*pitch];
300   dst = current;
301
302   for (i=0; i < 16; i++) {
303     memcpy (dst, src, 16);
304     src += pitch;
305     dst += pitch;
306   }
307 }
308
309 static int svq1_motion_inter_block (MpegEncContext *s, GetBitContext *bitbuf,
310                                uint8_t *current, uint8_t *previous, int pitch,
311                                svq1_pmv *motion, int x, int y) {
312   uint8_t    *src;
313   uint8_t    *dst;
314   svq1_pmv    mv;
315   svq1_pmv   *pmv[3];
316   int         result;
317
318   /* predict and decode motion vector */
319   pmv[0] = &motion[0];
320   if (y == 0) {
321     pmv[1] =
322     pmv[2] = pmv[0];
323   }
324   else {
325     pmv[1] = &motion[(x / 8) + 2];
326     pmv[2] = &motion[(x / 8) + 4];
327   }
328
329   result = svq1_decode_motion_vector (bitbuf, &mv, pmv);
330
331   if (result != 0)
332     return result;
333
334   motion[0].x                =
335   motion[(x / 8) + 2].x      =
336   motion[(x / 8) + 3].x      = mv.x;
337   motion[0].y                =
338   motion[(x / 8) + 2].y      =
339   motion[(x / 8) + 3].y      = mv.y;
340
341   if(y + (mv.y >> 1)<0)
342      mv.y= 0;
343   if(x + (mv.x >> 1)<0)
344      mv.x= 0;
345
346   src = &previous[(x + (mv.x >> 1)) + (y + (mv.y >> 1))*pitch];
347   dst = current;
348
349   s->dsp.put_pixels_tab[0][((mv.y & 1) << 1) | (mv.x & 1)](dst,src,pitch,16);
350
351   return 0;
352 }
353
354 static int svq1_motion_inter_4v_block (MpegEncContext *s, GetBitContext *bitbuf,
355                                   uint8_t *current, uint8_t *previous, int pitch,
356                                   svq1_pmv *motion,int x, int y) {
357   uint8_t    *src;
358   uint8_t    *dst;
359   svq1_pmv    mv;
360   svq1_pmv   *pmv[4];
361   int         i, result;
362
363   /* predict and decode motion vector (0) */
364   pmv[0] = &motion[0];
365   if (y == 0) {
366     pmv[1] =
367     pmv[2] = pmv[0];
368   }
369   else {
370     pmv[1] = &motion[(x / 8) + 2];
371     pmv[2] = &motion[(x / 8) + 4];
372   }
373
374   result = svq1_decode_motion_vector (bitbuf, &mv, pmv);
375
376   if (result != 0)
377     return result;
378
379   /* predict and decode motion vector (1) */
380   pmv[0] = &mv;
381   if (y == 0) {
382     pmv[1] =
383     pmv[2] = pmv[0];
384   }
385   else {
386     pmv[1] = &motion[(x / 8) + 3];
387   }
388   result = svq1_decode_motion_vector (bitbuf, &motion[0], pmv);
389
390   if (result != 0)
391     return result;
392
393   /* predict and decode motion vector (2) */
394   pmv[1] = &motion[0];
395   pmv[2] = &motion[(x / 8) + 1];
396
397   result = svq1_decode_motion_vector (bitbuf, &motion[(x / 8) + 2], pmv);
398
399   if (result != 0)
400     return result;
401
402   /* predict and decode motion vector (3) */
403   pmv[2] = &motion[(x / 8) + 2];
404   pmv[3] = &motion[(x / 8) + 3];
405
406   result = svq1_decode_motion_vector (bitbuf, pmv[3], pmv);
407
408   if (result != 0)
409     return result;
410
411   /* form predictions */
412   for (i=0; i < 4; i++) {
413     int mvx= pmv[i]->x + (i&1)*16;
414     int mvy= pmv[i]->y + (i>>1)*16;
415
416     ///XXX /FIXME clipping or padding?
417     if(y + (mvy >> 1)<0)
418        mvy= 0;
419     if(x + (mvx >> 1)<0)
420        mvx= 0;
421
422     src = &previous[(x + (mvx >> 1)) + (y + (mvy >> 1))*pitch];
423     dst = current;
424
425     s->dsp.put_pixels_tab[1][((mvy & 1) << 1) | (mvx & 1)](dst,src,pitch,8);
426
427     /* select next block */
428     if (i & 1) {
429       current  += 8*(pitch - 1);
430     } else {
431       current  += 8;
432     }
433   }
434
435   return 0;
436 }
437
438 static int svq1_decode_delta_block (MpegEncContext *s, GetBitContext *bitbuf,
439                         uint8_t *current, uint8_t *previous, int pitch,
440                         svq1_pmv *motion, int x, int y) {
441   uint32_t block_type;
442   int      result = 0;
443
444   /* get block type */
445   block_type = get_vlc2(bitbuf, svq1_block_type.table, 2, 2);
446
447   /* reset motion vectors */
448   if (block_type == SVQ1_BLOCK_SKIP || block_type == SVQ1_BLOCK_INTRA) {
449     motion[0].x                 =
450     motion[0].y                 =
451     motion[(x / 8) + 2].x =
452     motion[(x / 8) + 2].y =
453     motion[(x / 8) + 3].x =
454     motion[(x / 8) + 3].y = 0;
455   }
456
457   switch (block_type) {
458   case SVQ1_BLOCK_SKIP:
459     svq1_skip_block (current, previous, pitch, x, y);
460     break;
461
462   case SVQ1_BLOCK_INTER:
463     result = svq1_motion_inter_block (s, bitbuf, current, previous, pitch, motion, x, y);
464
465     if (result != 0)
466     {
467       av_dlog(s->avctx, "Error in svq1_motion_inter_block %i\n", result);
468       break;
469     }
470     result = svq1_decode_block_non_intra (bitbuf, current, pitch);
471     break;
472
473   case SVQ1_BLOCK_INTER_4V:
474     result = svq1_motion_inter_4v_block (s, bitbuf, current, previous, pitch, motion, x, y);
475
476     if (result != 0)
477     {
478       av_dlog(s->avctx, "Error in svq1_motion_inter_4v_block %i\n", result);
479       break;
480     }
481     result = svq1_decode_block_non_intra (bitbuf, current, pitch);
482     break;
483
484   case SVQ1_BLOCK_INTRA:
485     result = svq1_decode_block_intra (bitbuf, current, pitch);
486     break;
487   }
488
489   return result;
490 }
491
492 static void svq1_parse_string (GetBitContext *bitbuf, uint8_t *out) {
493   uint8_t seed;
494   int     i;
495
496   out[0] = get_bits (bitbuf, 8);
497
498   seed = string_table[out[0]];
499
500   for (i=1; i <= out[0]; i++) {
501     out[i] = get_bits (bitbuf, 8) ^ seed;
502     seed   = string_table[out[i] ^ seed];
503   }
504 }
505
506 static int svq1_decode_frame_header (GetBitContext *bitbuf,MpegEncContext *s) {
507   int frame_size_code;
508
509   skip_bits(bitbuf, 8); /* temporal_reference */
510
511   /* frame type */
512   s->pict_type= get_bits (bitbuf, 2)+1;
513   if(s->pict_type==4)
514       return -1;
515
516   if (s->pict_type == AV_PICTURE_TYPE_I) {
517
518     /* unknown fields */
519     if (s->f_code == 0x50 || s->f_code == 0x60) {
520       int csum = get_bits (bitbuf, 16);
521
522       csum = ff_svq1_packet_checksum (bitbuf->buffer, bitbuf->size_in_bits>>3, csum);
523
524       av_dlog(s->avctx, "%s checksum (%02x) for packet data\n",
525               (csum == 0) ? "correct" : "incorrect", csum);
526     }
527
528     if ((s->f_code ^ 0x10) >= 0x50) {
529       uint8_t msg[256];
530
531       svq1_parse_string (bitbuf, msg);
532
533       av_log(s->avctx, AV_LOG_INFO, "embedded message: \"%s\"\n", (char *) msg);
534     }
535
536     skip_bits (bitbuf, 2);
537     skip_bits (bitbuf, 2);
538     skip_bits1 (bitbuf);
539
540     /* load frame size */
541     frame_size_code = get_bits (bitbuf, 3);
542
543     if (frame_size_code == 7) {
544       /* load width, height (12 bits each) */
545       s->width = get_bits (bitbuf, 12);
546       s->height = get_bits (bitbuf, 12);
547
548       if (!s->width || !s->height)
549         return -1;
550     } else {
551       /* get width, height from table */
552       s->width = ff_svq1_frame_size_table[frame_size_code].width;
553       s->height = ff_svq1_frame_size_table[frame_size_code].height;
554     }
555   }
556
557   /* unknown fields */
558   if (get_bits1 (bitbuf) == 1) {
559     skip_bits1 (bitbuf);       /* use packet checksum if (1) */
560     skip_bits1 (bitbuf);       /* component checksums after image data if (1) */
561
562     if (get_bits (bitbuf, 2) != 0)
563       return -1;
564   }
565
566   if (get_bits1 (bitbuf) == 1) {
567     skip_bits1 (bitbuf);
568     skip_bits (bitbuf, 4);
569     skip_bits1 (bitbuf);
570     skip_bits (bitbuf, 2);
571
572     while (get_bits1 (bitbuf) == 1) {
573       skip_bits (bitbuf, 8);
574     }
575   }
576
577   return 0;
578 }
579
580 static int svq1_decode_frame(AVCodecContext *avctx,
581                              void *data, int *data_size,
582                              AVPacket *avpkt)
583 {
584   const uint8_t *buf = avpkt->data;
585   int buf_size = avpkt->size;
586   MpegEncContext *s=avctx->priv_data;
587   uint8_t        *current, *previous;
588   int             result, i, x, y, width, height;
589   AVFrame *pict = data;
590   svq1_pmv *pmv;
591
592   /* initialize bit buffer */
593   init_get_bits(&s->gb,buf,buf_size*8);
594
595   /* decode frame header */
596   s->f_code = get_bits (&s->gb, 22);
597
598   if ((s->f_code & ~0x70) || !(s->f_code & 0x60))
599     return -1;
600
601   /* swap some header bytes (why?) */
602   if (s->f_code != 0x20) {
603     uint32_t *src = (uint32_t *) (buf + 4);
604
605     for (i=0; i < 4; i++) {
606       src[i] = ((src[i] << 16) | (src[i] >> 16)) ^ src[7 - i];
607     }
608   }
609
610   result = svq1_decode_frame_header (&s->gb, s);
611
612   if (result != 0)
613   {
614     av_dlog(s->avctx, "Error in svq1_decode_frame_header %i\n",result);
615     return result;
616   }
617   avcodec_set_dimensions(avctx, s->width, s->height);
618
619   //FIXME this avoids some confusion for "B frames" without 2 references
620   //this should be removed after libavcodec can handle more flexible picture types & ordering
621   if(s->pict_type==AV_PICTURE_TYPE_B && s->last_picture_ptr==NULL) return buf_size;
622
623   if(  (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type==AV_PICTURE_TYPE_B)
624      ||(avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type!=AV_PICTURE_TYPE_I)
625      || avctx->skip_frame >= AVDISCARD_ALL)
626       return buf_size;
627
628   if(ff_MPV_frame_start(s, avctx) < 0)
629       return -1;
630
631   pmv = av_malloc((FFALIGN(s->width, 16)/8 + 3) * sizeof(*pmv));
632   if (!pmv)
633       return -1;
634
635   /* decode y, u and v components */
636   for (i=0; i < 3; i++) {
637     int linesize;
638     if (i == 0) {
639       width  = FFALIGN(s->width, 16);
640       height = FFALIGN(s->height, 16);
641       linesize= s->linesize;
642     } else {
643       if(s->flags&CODEC_FLAG_GRAY) break;
644       width  = FFALIGN(s->width/4, 16);
645       height = FFALIGN(s->height/4, 16);
646       linesize= s->uvlinesize;
647     }
648
649     current = s->current_picture.f.data[i];
650
651     if(s->pict_type==AV_PICTURE_TYPE_B){
652         previous = s->next_picture.f.data[i];
653     }else{
654         previous = s->last_picture.f.data[i];
655     }
656
657     if (s->pict_type == AV_PICTURE_TYPE_I) {
658       /* keyframe */
659       for (y=0; y < height; y+=16) {
660         for (x=0; x < width; x+=16) {
661           result = svq1_decode_block_intra (&s->gb, &current[x], linesize);
662           if (result != 0)
663           {
664             av_log(s->avctx, AV_LOG_INFO, "Error in svq1_decode_block %i (keyframe)\n",result);
665             goto err;
666           }
667         }
668         current += 16*linesize;
669       }
670     } else {
671       /* delta frame */
672       memset (pmv, 0, ((width / 8) + 3) * sizeof(svq1_pmv));
673
674       for (y=0; y < height; y+=16) {
675         for (x=0; x < width; x+=16) {
676           result = svq1_decode_delta_block (s, &s->gb, &current[x], previous,
677                                             linesize, pmv, x, y);
678           if (result != 0)
679           {
680             av_dlog(s->avctx, "Error in svq1_decode_delta_block %i\n",result);
681             goto err;
682           }
683         }
684
685         pmv[0].x =
686         pmv[0].y = 0;
687
688         current += 16*linesize;
689       }
690     }
691   }
692
693   *pict = s->current_picture.f;
694
695
696   ff_MPV_frame_end(s);
697
698   *data_size=sizeof(AVFrame);
699   result = buf_size;
700 err:
701   av_free(pmv);
702   return result;
703 }
704
705 static av_cold int svq1_decode_init(AVCodecContext *avctx)
706 {
707     MpegEncContext *s = avctx->priv_data;
708     int i;
709     int offset = 0;
710
711     ff_MPV_decode_defaults(s);
712
713     s->avctx = avctx;
714     s->width = (avctx->width+3)&~3;
715     s->height = (avctx->height+3)&~3;
716     s->codec_id= avctx->codec->id;
717     avctx->pix_fmt = PIX_FMT_YUV410P;
718     avctx->has_b_frames= 1; // not true, but DP frames and these behave like unidirectional b frames
719     s->flags= avctx->flags;
720     if (ff_MPV_common_init(s) < 0) return -1;
721
722     INIT_VLC_STATIC(&svq1_block_type, 2, 4,
723         &ff_svq1_block_type_vlc[0][1], 2, 1,
724         &ff_svq1_block_type_vlc[0][0], 2, 1, 6);
725
726     INIT_VLC_STATIC(&svq1_motion_component, 7, 33,
727         &ff_mvtab[0][1], 2, 1,
728         &ff_mvtab[0][0], 2, 1, 176);
729
730     for (i = 0; i < 6; i++) {
731         static const uint8_t sizes[2][6] = {{14, 10, 14, 18, 16, 18}, {10, 10, 14, 14, 14, 16}};
732         static VLC_TYPE table[168][2];
733         svq1_intra_multistage[i].table = &table[offset];
734         svq1_intra_multistage[i].table_allocated = sizes[0][i];
735         offset += sizes[0][i];
736         init_vlc(&svq1_intra_multistage[i], 3, 8,
737             &ff_svq1_intra_multistage_vlc[i][0][1], 2, 1,
738             &ff_svq1_intra_multistage_vlc[i][0][0], 2, 1, INIT_VLC_USE_NEW_STATIC);
739         svq1_inter_multistage[i].table = &table[offset];
740         svq1_inter_multistage[i].table_allocated = sizes[1][i];
741         offset += sizes[1][i];
742         init_vlc(&svq1_inter_multistage[i], 3, 8,
743             &ff_svq1_inter_multistage_vlc[i][0][1], 2, 1,
744             &ff_svq1_inter_multistage_vlc[i][0][0], 2, 1, INIT_VLC_USE_NEW_STATIC);
745     }
746
747     INIT_VLC_STATIC(&svq1_intra_mean, 8, 256,
748         &ff_svq1_intra_mean_vlc[0][1], 4, 2,
749         &ff_svq1_intra_mean_vlc[0][0], 4, 2, 632);
750
751     INIT_VLC_STATIC(&svq1_inter_mean, 9, 512,
752         &ff_svq1_inter_mean_vlc[0][1], 4, 2,
753         &ff_svq1_inter_mean_vlc[0][0], 4, 2, 1434);
754
755     return 0;
756 }
757
758 static av_cold int svq1_decode_end(AVCodecContext *avctx)
759 {
760     MpegEncContext *s = avctx->priv_data;
761
762     ff_MPV_common_end(s);
763     return 0;
764 }
765
766
767 AVCodec ff_svq1_decoder = {
768     .name           = "svq1",
769     .type           = AVMEDIA_TYPE_VIDEO,
770     .id             = AV_CODEC_ID_SVQ1,
771     .priv_data_size = sizeof(MpegEncContext),
772     .init           = svq1_decode_init,
773     .close          = svq1_decode_end,
774     .decode         = svq1_decode_frame,
775     .capabilities   = CODEC_CAP_DR1,
776     .flush          = ff_mpeg_flush,
777     .pix_fmts       = (const enum PixelFormat[]){ PIX_FMT_YUV410P, PIX_FMT_NONE },
778     .long_name      = NULL_IF_CONFIG_SMALL("Sorenson Vector Quantizer 1 / Sorenson Video 1 / SVQ1"),
779 };