]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavcodec/svq3.c
optimizations
[frescor/ffmpeg.git] / libavcodec / svq3.c
1 /*
2  * Copyright (c) 2003 The FFmpeg Project.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  *
18  *
19  * How to use this decoder:
20  * SVQ3 data is transported within Apple Quicktime files. Quicktime files
21  * have stsd atoms to describe media trak properties. A stsd atom for a
22  * video trak contains 1 or more ImageDescription atoms. These atoms begin
23  * with the 4-byte length of the atom followed by the codec fourcc. Some
24  * decoders need information in this atom to operate correctly. Such
25  * is the case with SVQ3. In order to get the best use out of this decoder,
26  * the calling app must make the SVQ3 ImageDescription atom available
27  * via the AVCodecContext's extradata[_size] field:
28  *
29  * AVCodecContext.extradata = pointer to ImageDescription, first characters 
30  * are expected to be 'S', 'V', 'Q', and '3', NOT the 4-byte atom length
31  * AVCodecContext.extradata_size = size of ImageDescription atom memory 
32  * buffer (which will be the same as the ImageDescription atom size field 
33  * from the QT file, minus 4 bytes since the length is missing)
34  *
35  * You will know you have these parameters passed correctly when the decoder
36  * correctly decodes this file:
37  *  ftp://ftp.mplayerhq.hu/MPlayer/samples/V-codecs/SVQ3/Vertical400kbit.sorenson3.mov
38  *
39  */
40  
41 /**
42  * @file svq3.c
43  * svq3 decoder.
44  */
45
46 /* dual scan (from some older h264 draft)
47  o-->o-->o   o
48          |  /|
49  o   o   o / o
50  | / |   |/  |
51  o   o   o   o
52    / 
53  o-->o-->o-->o
54 */
55 static const uint8_t svq3_scan[16]={
56  0+0*4, 1+0*4, 2+0*4, 2+1*4,
57  2+2*4, 3+0*4, 3+1*4, 3+2*4,
58  0+1*4, 0+2*4, 1+1*4, 1+2*4,
59  0+3*4, 1+3*4, 2+3*4, 3+3*4,
60 };
61
62 static const uint8_t svq3_pred_0[25][2] = {
63   { 0, 0 },
64   { 1, 0 }, { 0, 1 },
65   { 0, 2 }, { 1, 1 }, { 2, 0 },
66   { 3, 0 }, { 2, 1 }, { 1, 2 }, { 0, 3 },
67   { 0, 4 }, { 1, 3 }, { 2, 2 }, { 3, 1 }, { 4, 0 },
68   { 4, 1 }, { 3, 2 }, { 2, 3 }, { 1, 4 },
69   { 2, 4 }, { 3, 3 }, { 4, 2 },
70   { 4, 3 }, { 3, 4 },
71   { 4, 4 }
72 };
73
74 static const int8_t svq3_pred_1[6][6][5] = {
75   { { 2,-1,-1,-1,-1 }, { 2, 1,-1,-1,-1 }, { 1, 2,-1,-1,-1 },
76     { 2, 1,-1,-1,-1 }, { 1, 2,-1,-1,-1 }, { 1, 2,-1,-1,-1 } },
77   { { 0, 2,-1,-1,-1 }, { 0, 2, 1, 4, 3 }, { 0, 1, 2, 4, 3 },
78     { 0, 2, 1, 4, 3 }, { 2, 0, 1, 3, 4 }, { 0, 4, 2, 1, 3 } },
79   { { 2, 0,-1,-1,-1 }, { 2, 1, 0, 4, 3 }, { 1, 2, 4, 0, 3 },
80     { 2, 1, 0, 4, 3 }, { 2, 1, 4, 3, 0 }, { 1, 2, 4, 0, 3 } },
81   { { 2, 0,-1,-1,-1 }, { 2, 0, 1, 4, 3 }, { 1, 2, 0, 4, 3 },
82     { 2, 1, 0, 4, 3 }, { 2, 1, 3, 4, 0 }, { 2, 4, 1, 0, 3 } },
83   { { 0, 2,-1,-1,-1 }, { 0, 2, 1, 3, 4 }, { 1, 2, 3, 0, 4 },
84     { 2, 0, 1, 3, 4 }, { 2, 1, 3, 0, 4 }, { 2, 0, 4, 3, 1 } },
85   { { 0, 2,-1,-1,-1 }, { 0, 2, 4, 1, 3 }, { 1, 4, 2, 0, 3 },
86     { 4, 2, 0, 1, 3 }, { 2, 0, 1, 4, 3 }, { 4, 2, 1, 0, 3 } },
87 };
88
89 static const struct { uint8_t run; uint8_t level; } svq3_dct_tables[2][16] = {
90   { { 0, 0 }, { 0, 1 }, { 1, 1 }, { 2, 1 }, { 0, 2 }, { 3, 1 }, { 4, 1 }, { 5, 1 },
91     { 0, 3 }, { 1, 2 }, { 2, 2 }, { 6, 1 }, { 7, 1 }, { 8, 1 }, { 9, 1 }, { 0, 4 } },
92   { { 0, 0 }, { 0, 1 }, { 1, 1 }, { 0, 2 }, { 2, 1 }, { 0, 3 }, { 0, 4 }, { 0, 5 },
93     { 3, 1 }, { 4, 1 }, { 1, 2 }, { 1, 3 }, { 0, 6 }, { 0, 7 }, { 0, 8 }, { 0, 9 } }
94 };
95
96 static const uint32_t svq3_dequant_coeff[32] = {
97    3881,  4351,  4890,  5481,  6154,  6914,  7761,  8718,
98    9781, 10987, 12339, 13828, 15523, 17435, 19561, 21873,
99   24552, 27656, 30847, 34870, 38807, 43747, 49103, 54683,
100   61694, 68745, 77615, 89113,100253,109366,126635,141533
101 };
102
103
104 static void svq3_luma_dc_dequant_idct_c(DCTELEM *block, int qp){
105     const int qmul= svq3_dequant_coeff[qp];
106 #define stride 16
107     int i;
108     int temp[16];
109     static const int x_offset[4]={0, 1*stride, 4* stride,  5*stride};
110     static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
111
112     for(i=0; i<4; i++){
113         const int offset= y_offset[i];
114         const int z0= 13*(block[offset+stride*0] +    block[offset+stride*4]);
115         const int z1= 13*(block[offset+stride*0] -    block[offset+stride*4]);
116         const int z2=  7* block[offset+stride*1] - 17*block[offset+stride*5];
117         const int z3= 17* block[offset+stride*1] +  7*block[offset+stride*5];
118
119         temp[4*i+0]= z0+z3;
120         temp[4*i+1]= z1+z2;
121         temp[4*i+2]= z1-z2;
122         temp[4*i+3]= z0-z3;
123     }
124
125     for(i=0; i<4; i++){
126         const int offset= x_offset[i];
127         const int z0= 13*(temp[4*0+i] +    temp[4*2+i]);
128         const int z1= 13*(temp[4*0+i] -    temp[4*2+i]);
129         const int z2=  7* temp[4*1+i] - 17*temp[4*3+i];
130         const int z3= 17* temp[4*1+i] +  7*temp[4*3+i];
131
132         block[stride*0 +offset]= ((z0 + z3)*qmul + 0x80000)>>20;
133         block[stride*2 +offset]= ((z1 + z2)*qmul + 0x80000)>>20;
134         block[stride*8 +offset]= ((z1 - z2)*qmul + 0x80000)>>20;
135         block[stride*10+offset]= ((z0 - z3)*qmul + 0x80000)>>20;
136     }
137 }
138 #undef stride
139
140 static void svq3_add_idct_c (uint8_t *dst, DCTELEM *block, int stride, int qp, int dc){
141     const int qmul= svq3_dequant_coeff[qp];
142     int i;
143     uint8_t *cm = cropTbl + MAX_NEG_CROP;
144
145     if (dc) {
146         dc = 13*13*((dc == 1) ? 1538*block[0] : ((qmul*(block[0] >> 3)) / 2));
147         block[0] = 0;
148     }
149
150     for (i=0; i < 4; i++) {
151         const int z0= 13*(block[0 + 4*i] +    block[2 + 4*i]);
152         const int z1= 13*(block[0 + 4*i] -    block[2 + 4*i]);
153         const int z2=  7* block[1 + 4*i] - 17*block[3 + 4*i];
154         const int z3= 17* block[1 + 4*i] +  7*block[3 + 4*i];
155
156         block[0 + 4*i]= z0 + z3;
157         block[1 + 4*i]= z1 + z2;
158         block[2 + 4*i]= z1 - z2;
159         block[3 + 4*i]= z0 - z3;
160     }
161
162     for (i=0; i < 4; i++) {
163         const int z0= 13*(block[i + 4*0] +    block[i + 4*2]);
164         const int z1= 13*(block[i + 4*0] -    block[i + 4*2]);
165         const int z2=  7* block[i + 4*1] - 17*block[i + 4*3];
166         const int z3= 17* block[i + 4*1] +  7*block[i + 4*3];
167         const int rr= (dc + 0x80000);
168
169         dst[i + stride*0]= cm[ dst[i + stride*0] + (((z0 + z3)*qmul + rr) >> 20) ];
170         dst[i + stride*1]= cm[ dst[i + stride*1] + (((z1 + z2)*qmul + rr) >> 20) ];
171         dst[i + stride*2]= cm[ dst[i + stride*2] + (((z1 - z2)*qmul + rr) >> 20) ];
172         dst[i + stride*3]= cm[ dst[i + stride*3] + (((z0 - z3)*qmul + rr) >> 20) ];
173     }
174 }
175
176 static void pred4x4_down_left_svq3_c(uint8_t *src, uint8_t *topright, int stride){
177     LOAD_TOP_EDGE    
178     LOAD_LEFT_EDGE    
179     const __attribute__((unused)) int unu0= t0;
180     const __attribute__((unused)) int unu1= l0;
181
182     src[0+0*stride]=(l1 + t1)>>1;
183     src[1+0*stride]=
184     src[0+1*stride]=(l2 + t2)>>1;
185     src[2+0*stride]=
186     src[1+1*stride]=
187     src[0+2*stride]=
188     src[3+0*stride]=
189     src[2+1*stride]=
190     src[1+2*stride]=
191     src[0+3*stride]=
192     src[3+1*stride]=
193     src[2+2*stride]=
194     src[1+3*stride]=
195     src[3+2*stride]=
196     src[2+3*stride]=
197     src[3+3*stride]=(l3 + t3)>>1;
198 };
199
200 static void pred16x16_plane_svq3_c(uint8_t *src, int stride){
201     pred16x16_plane_compat_c(src, stride, 1);
202 }
203
204 static inline int svq3_decode_block (GetBitContext *gb, DCTELEM *block,
205                                      int index, const int type) {
206
207   static const uint8_t *const scan_patterns[4] =
208   { luma_dc_zigzag_scan, zigzag_scan, svq3_scan, chroma_dc_scan };
209
210   int run, level, sign, vlc, limit;
211   const int intra = (3 * type) >> 2;
212   const uint8_t *const scan = scan_patterns[type];
213
214   for (limit=(16 >> intra); index < 16; index=limit, limit+=8) {
215     for (; (vlc = svq3_get_ue_golomb (gb)) != 0; index++) {
216
217       if (vlc == INVALID_VLC)
218         return -1;
219
220       sign = (vlc & 0x1) - 1;
221       vlc  = (vlc + 1) >> 1;
222
223       if (type == 3) {
224         if (vlc < 3) {
225           run   = 0;
226           level = vlc;
227         } else if (vlc < 4) {
228           run   = 1;
229           level = 1;
230         } else {
231           run   = (vlc & 0x3);
232           level = ((vlc + 9) >> 2) - run;
233         }
234       } else {
235         if (vlc < 16) {
236           run   = svq3_dct_tables[intra][vlc].run;
237           level = svq3_dct_tables[intra][vlc].level;
238         } else if (intra) {
239           run   = (vlc & 0x7);
240           level = (vlc >> 3) + ((run == 0) ? 8 : ((run < 2) ? 2 : ((run < 5) ? 0 : -1)));
241         } else {
242           run   = (vlc & 0xF);
243           level = (vlc >> 4) + ((run == 0) ? 4 : ((run < 3) ? 2 : ((run < 10) ? 1 : 0)));
244         }
245       }
246
247       if ((index += run) >= limit)
248         return -1;
249
250       block[scan[index]] = (level ^ sign) - sign;
251     }
252
253     if (type != 2) {
254       break;
255     }
256   }
257
258   return 0;
259 }
260
261 static void sixpel_mc_put (MpegEncContext *s,
262                            uint8_t *src, uint8_t *dst, int stride,
263                            int dxy, int width, int height) {
264   int i, j;
265
266   switch (dxy) {
267   case 6*0+0:
268     for (i=0; i < height; i++) {
269       memcpy (dst, src, width);
270       src += stride;
271       dst += stride;
272     }
273     break;
274   case 6*0+2:
275     for (i=0; i < height; i++) {
276       for (j=0; j < width; j++) {
277         dst[j] = (683*(2*src[j] + src[j+1] + 1)) >> 11;
278       }
279       src += stride;
280       dst += stride;
281     }
282     break;
283   case 6*0+3:
284     for (i=0; i < height; i++) {
285       for (j=0; j < width; j++) {
286         dst[j] = (src[j] + src[j+1] + 1) >> 1;
287       }
288       src += stride;
289       dst += stride;
290     }
291     break;
292   case 6*0+4:
293     for (i=0; i < height; i++) {
294       for (j=0; j < width; j++) {
295         dst[j] = (683*(src[j] + 2*src[j+1] + 1)) >> 11;
296       }
297       src += stride;
298       dst += stride;
299     }
300     break;
301   case 6*2+0:
302     for (i=0; i < height; i++) {
303       for (j=0; j < width; j++) {
304         dst[j] = (683*(2*src[j] + src[j+stride] + 1)) >> 11;
305       }
306       src += stride;
307       dst += stride;
308     }
309     break;
310   case 6*2+2:
311     for (i=0; i < height; i++) {
312       for (j=0; j < width; j++) {
313         dst[j] = (2731*(4*src[j] + 3*src[j+1] + 3*src[j+stride] + 2*src[j+stride+1] + 6)) >> 15;
314       }
315       src += stride;
316       dst += stride;
317     }
318     break;
319   case 6*2+4:
320     for (i=0; i < height; i++) {
321       for (j=0; j < width; j++) {
322         dst[j] = (2731*(3*src[j] + 4*src[j+1] + 2*src[j+stride] + 3*src[j+stride+1] + 6)) >> 15;
323       }
324       src += stride;
325       dst += stride;
326     }
327     break;
328   case 6*3+0:
329     for (i=0; i < height; i++) {
330       for (j=0; j < width; j++) {
331         dst[j] = (src[j] + src[j+stride]+1) >> 1;
332       }
333       src += stride;
334       dst += stride;
335     }
336     break;
337   case 6*3+3:
338     for (i=0; i < height; i++) {
339       for (j=0; j < width; j++) {
340         dst[j] = (src[j] + src[j+1] + src[j+stride] + src[j+stride+1] + 2) >> 2;
341       }
342       src += stride;
343       dst += stride;
344     }
345     break;
346   case 6*4+0:
347     for (i=0; i < height; i++) {
348       for (j=0; j < width; j++) {
349         dst[j] = (683*(src[j] + 2*src[j+stride] + 1)) >> 11;
350       }
351       src += stride;
352       dst += stride;
353     }
354     break;
355   case 6*4+2:
356     for (i=0; i < height; i++) {
357       for (j=0; j < width; j++) {
358         dst[j] = (2731*(3*src[j] + 2*src[j+1] + 4*src[j+stride] + 3*src[j+stride+1] + 6)) >> 15;
359       }
360       src += stride;
361       dst += stride;
362     }
363     break;
364   case 6*4+4:
365     for (i=0; i < height; i++) {
366       for (j=0; j < width; j++) {
367         dst[j] = (2731*(2*src[j] + 3*src[j+1] + 3*src[j+stride] + 4*src[j+stride+1] + 6)) >> 15;
368       }
369       src += stride;
370       dst += stride;
371     }
372     break;
373   }
374 }
375
376 static inline void svq3_mc_dir_part (MpegEncContext *s, int x, int y,
377                                      int width, int height, int mx, int my) {
378   uint8_t *src, *dest;
379   int i, emu = 0;
380   const int sx = ((unsigned) (mx + 0x7FFFFFFE)) % 6;
381   const int sy = ((unsigned) (my + 0x7FFFFFFE)) % 6;
382   const int dxy= 6*sy + sx;
383
384   /* decode and clip motion vector to frame border (+16) */
385   mx = x + (mx - sx) / 6;
386   my = y + (my - sy) / 6;
387
388   if (mx < 0 || mx >= (s->width  - width  - 1) ||
389       my < 0 || my >= (s->height - height - 1)) {
390
391     if ((s->flags & CODEC_FLAG_EMU_EDGE)) {
392       emu = 1;
393     }
394
395     mx = clip (mx, -16, (s->width  - width  + 15));
396     my = clip (my, -16, (s->height - height + 15));
397   }
398
399   /* form component predictions */
400   dest = s->current_picture.data[0] + x + y*s->linesize;
401   src  = s->last_picture.data[0] + mx + my*s->linesize;
402
403   if (emu) {
404     ff_emulated_edge_mc (s, src, s->linesize, (width + 1), (height + 1),
405                          mx, my, s->width, s->height);
406     src = s->edge_emu_buffer;
407   }
408   sixpel_mc_put (s, src, dest, s->linesize, dxy, width, height);
409
410   if (!(s->flags & CODEC_FLAG_GRAY)) {
411     mx     = (mx + (mx < (int) x)) >> 1;
412     my     = (my + (my < (int) y)) >> 1;
413     width  = (width  >> 1);
414     height = (height >> 1);
415
416     for (i=1; i < 3; i++) {
417       dest = s->current_picture.data[i] + (x >> 1) + (y >> 1)*s->uvlinesize;
418       src  = s->last_picture.data[i] + mx + my*s->uvlinesize;
419
420       if (emu) {
421         ff_emulated_edge_mc (s, src, s->uvlinesize, (width + 1), (height + 1),
422                              mx, my, (s->width >> 1), (s->height >> 1));
423         src = s->edge_emu_buffer;
424       }
425       sixpel_mc_put (s, src, dest, s->uvlinesize, dxy, width, height);
426     }
427   }
428 }
429
430 static int svq3_decode_mb (H264Context *h, unsigned int mb_type) {
431   int cbp, dir, mode, mx, my, dx, dy, x, y, part_width, part_height;
432   int i, j, k, l, m;
433   uint32_t vlc;
434   int8_t *top, *left;
435   MpegEncContext *const s = (MpegEncContext *) h;
436   const int mb_xy = s->mb_x + s->mb_y*s->mb_stride;
437   const int b_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride;
438
439   h->top_samples_available      = (s->mb_y == 0) ? 0x33FF : 0xFFFF;
440   h->left_samples_available     = (s->mb_x == 0) ? 0x5F5F : 0xFFFF;
441   h->topright_samples_available = 0xFFFF;
442
443   if (mb_type == 0) {           /* SKIP */
444     svq3_mc_dir_part (s, 16*s->mb_x, 16*s->mb_y, 16, 16, 0, 0);
445
446     cbp = 0;
447     mb_type = MB_TYPE_SKIP;
448   } else if (mb_type < 8) {     /* INTER */
449     if (h->thirdpel_flag && h->halfpel_flag == !get_bits (&s->gb, 1)) {
450       mode = 3; /* thirdpel */
451     } else if (h->halfpel_flag && h->thirdpel_flag == !get_bits (&s->gb, 1)) {
452       mode = 2; /* halfpel */
453     } else {
454       mode = 1; /* fullpel */
455     }
456
457     /* fill caches */
458     /* note ref_cache[0] should contain here:
459         ????????
460         ???11111
461         N??11111
462         N??11111
463         N??11111
464         N
465     */
466     
467     if (s->mb_x > 0) {
468       for (i=0; i < 4; i++) {
469         *(uint32_t *) h->mv_cache[0][scan8[0] - 1 + i*8] = *(uint32_t *) s->current_picture.motion_val[0][b_xy - 1 + i*h->b_stride];
470       }
471     } else {
472       for (i=0; i < 4; i++) {
473         *(uint32_t *) h->mv_cache[0][scan8[0] - 1 + i*8] = 0;
474       }
475     }
476     if (s->mb_y > 0) {
477       memcpy (h->mv_cache[0][scan8[0] - 1*8], s->current_picture.motion_val[0][b_xy - h->b_stride], 4*2*sizeof(int16_t));
478       memset (&h->ref_cache[0][scan8[0] - 1*8], 1, 4);
479
480       if (s->mb_x < (s->mb_width - 1)) {
481         *(uint32_t *) h->mv_cache[0][scan8[0] + 4 - 1*8] = *(uint32_t *) s->current_picture.motion_val[0][b_xy - h->b_stride + 4];
482         h->ref_cache[0][scan8[0] + 4 - 1*8] = 1;
483       }else
484         h->ref_cache[0][scan8[0] + 4 - 1*8] = PART_NOT_AVAILABLE;
485       if (s->mb_x > 0) {
486         *(uint32_t *) h->mv_cache[0][scan8[0] - 1 - 1*8] = *(uint32_t *) s->current_picture.motion_val[0][b_xy - h->b_stride - 1];
487         h->ref_cache[0][scan8[0] - 1 - 1*8] = 1;
488       }else
489         h->ref_cache[0][scan8[0] - 1 - 1*8] = PART_NOT_AVAILABLE;
490     }else
491       memset (&h->ref_cache[0][scan8[0] - 1*8 - 1], PART_NOT_AVAILABLE, 8);
492
493     /* decode motion vector(s) and form prediction(s) */
494     part_width  = ((mb_type & 5) == 5) ? 4 : 8 << (mb_type & 1);
495     part_height = 16 >> ((unsigned) mb_type / 3);
496
497     for (i=0; i < 16; i+=part_height) {
498       for (j=0; j < 16; j+=part_width) {
499         x = 16*s->mb_x + j;
500         y = 16*s->mb_y + i;
501         k = ((j>>2)&1) + ((i>>1)&2) + ((j>>1)&4) + (i&8);
502
503         pred_motion (h, k, (part_width >> 2), 0, 1, &mx, &my);
504
505         /* clip motion vector prediction to frame border */
506         mx = clip (mx, -6*x, 6*(s->width  - part_width  - x));
507         my = clip (my, -6*y, 6*(s->height - part_height - y));
508
509         /* get motion vector differential */
510         dy = svq3_get_se_golomb (&s->gb);
511         dx = svq3_get_se_golomb (&s->gb);
512
513         if (dx == INVALID_VLC || dy == INVALID_VLC) {
514           return -1;
515         }
516
517         /* compute motion vector */
518         if (mode == 3) {
519           mx = ((mx + 1) & ~0x1) + 2*dx;
520           my = ((my + 1) & ~0x1) + 2*dy;
521         } else if (mode == 2) {
522           mx = (mx + 1) - ((unsigned) (0x7FFFFFFF + mx) % 3) + 3*dx;
523           my = (my + 1) - ((unsigned) (0x7FFFFFFF + my) % 3) + 3*dy;
524         } else if (mode == 1) {
525           mx = (mx + 3) - ((unsigned) (0x7FFFFFFB + mx) % 6) + 6*dx;
526           my = (my + 3) - ((unsigned) (0x7FFFFFFB + my) % 6) + 6*dy;
527         }
528
529         /* update mv_cache */
530         fill_rectangle(h->mv_cache[0][scan8[k]], part_width>>2, part_height>>2, 8, (mx&0xFFFF)+(my<<16), 4);
531
532         svq3_mc_dir_part (s, x, y, part_width, part_height, mx, my);
533       }
534     }
535
536     for (i=0; i < 4; i++) {
537       memcpy (s->current_picture.motion_val[0][b_xy + i*h->b_stride], h->mv_cache[0][scan8[0] + 8*i], 4*2*sizeof(int16_t));
538     }
539
540     if ((vlc = svq3_get_ue_golomb (&s->gb)) >= 48)
541       return -1;
542
543     cbp = golomb_to_inter_cbp[vlc];
544     mb_type = MB_TYPE_16x16;
545   } else if (mb_type == 8) {    /* INTRA4x4 */
546     memset (h->intra4x4_pred_mode_cache, -1, 8*5*sizeof(int8_t));
547
548     if (s->mb_x > 0) {
549       for (i=0; i < 4; i++) {
550         h->intra4x4_pred_mode_cache[scan8[0] - 1 + i*8] = h->intra4x4_pred_mode[mb_xy - 1][i];
551       }
552     }
553     if (s->mb_y > 0) {
554       h->intra4x4_pred_mode_cache[4+8*0] = h->intra4x4_pred_mode[mb_xy - s->mb_stride][4];
555       h->intra4x4_pred_mode_cache[5+8*0] = h->intra4x4_pred_mode[mb_xy - s->mb_stride][5];
556       h->intra4x4_pred_mode_cache[6+8*0] = h->intra4x4_pred_mode[mb_xy - s->mb_stride][6];
557       h->intra4x4_pred_mode_cache[7+8*0] = h->intra4x4_pred_mode[mb_xy - s->mb_stride][3];
558     }
559
560     /* decode prediction codes for luma blocks */
561     for (i=0; i < 16; i+=2) {
562       vlc = svq3_get_ue_golomb (&s->gb);
563
564       if (vlc >= 25)
565         return -1;
566
567       left    = &h->intra4x4_pred_mode_cache[scan8[i] - 1];
568       top     = &h->intra4x4_pred_mode_cache[scan8[i] - 8];
569
570       left[1] = svq3_pred_1[top[0] + 1][left[0] + 1][svq3_pred_0[vlc][0]];
571       left[2] = svq3_pred_1[top[1] + 1][left[1] + 1][svq3_pred_0[vlc][1]];
572
573       if (left[1] == -1 || left[2] == -1)
574         return -1;
575     }
576
577     write_back_intra_pred_mode (h);
578     check_intra4x4_pred_mode (h);
579
580     if ((vlc = svq3_get_ue_golomb (&s->gb)) >= 48)
581       return -1;
582
583     cbp = golomb_to_intra4x4_cbp[vlc];
584     mb_type = MB_TYPE_INTRA4x4;
585   } else {                      /* INTRA16x16 */
586     dir = i_mb_type_info[mb_type - 8].pred_mode;
587     dir = (dir >> 1) ^ 3*(dir & 1) ^ 1;
588
589     if ((h->intra16x16_pred_mode = check_intra_pred_mode (h, dir)) == -1)
590       return -1;
591
592     cbp = i_mb_type_info[mb_type - 8].cbp;
593     mb_type = MB_TYPE_INTRA16x16;
594   }
595
596   if (!IS_INTER(mb_type) && s->pict_type != I_TYPE) {
597     for (i=0; i < 4; i++) {
598       memset (s->current_picture.motion_val[0][b_xy + i*h->b_stride], 0, 4*2*sizeof(int16_t));
599     }
600   }
601   if (!IS_INTRA4x4(mb_type)) {
602     memset (h->intra4x4_pred_mode[mb_xy], DC_PRED, 8);
603   }
604   if (!IS_SKIP(mb_type)) {
605     memset (h->non_zero_count_cache + 8, 0, 4*9*sizeof(uint8_t));
606     s->dsp.clear_blocks(h->mb);
607   }
608
609   if (IS_INTRA16x16(mb_type) || (s->pict_type != I_TYPE && s->adaptive_quant && cbp)) {
610     s->qscale += svq3_get_se_golomb (&s->gb);
611
612     if (s->qscale > 31)
613       return -1;
614   }
615   if (IS_INTRA16x16(mb_type)) {
616     if (svq3_decode_block (&s->gb, h->mb, 0, 0))
617       return -1;
618   }
619
620   if (!IS_SKIP(mb_type) && cbp) {
621     l = IS_INTRA16x16(mb_type) ? 1 : 0;
622     m = ((s->qscale < 24 && IS_INTRA4x4(mb_type)) ? 2 : 1);
623
624     for (i=0; i < 4; i++) {
625       if ((cbp & (1 << i))) {
626         for (j=0; j < 4; j++) {
627           k = l ? ((j&1) + 2*(i&1) + 2*(j&2) + 4*(i&2)) : (4*i + j);
628           h->non_zero_count_cache[ scan8[k] ] = 1;
629
630           if (svq3_decode_block (&s->gb, &h->mb[16*k], l, m))
631             return -1;
632         }
633       }
634     }
635
636     if ((cbp & 0x30)) {
637       for (i=0; i < 2; ++i) {
638         if (svq3_decode_block (&s->gb, &h->mb[16*(16 + 4*i)], 0, 3))
639           return -1;
640       }
641
642       if ((cbp & 0x20)) {
643         for (i=0; i < 8; i++) {
644           h->non_zero_count_cache[ scan8[16+i] ] = 1;
645
646           if (svq3_decode_block (&s->gb, &h->mb[16*(16 + i)], 1, 1))
647             return -1;
648         }
649       }
650     }
651   }
652
653   s->current_picture.mb_type[mb_xy] = mb_type;
654
655   if (IS_INTRA(mb_type)) {
656     h->chroma_pred_mode = check_intra_pred_mode (h, DC_PRED8x8);
657   }
658
659   return 0;
660 }
661
662 static int svq3_decode_frame (AVCodecContext *avctx,
663                               void *data, int *data_size,
664                               uint8_t *buf, int buf_size) {
665   MpegEncContext *const s = avctx->priv_data;
666   H264Context *const h = avctx->priv_data;
667   int i;
668
669   s->flags = avctx->flags;
670
671   if (!s->context_initialized) {
672     s->width = (avctx->width + 15) & ~15;
673     s->height = (avctx->height + 15) & ~15;
674     h->b_stride = (s->width >> 2);
675     h->pred4x4[DIAG_DOWN_LEFT_PRED] = pred4x4_down_left_svq3_c;
676     h->pred16x16[PLANE_PRED8x8] = pred16x16_plane_svq3_c;
677     h->halfpel_flag = 1;
678     h->thirdpel_flag = 1;
679     h->chroma_qp = 4;
680
681     if (MPV_common_init (s) < 0)
682       return -1;
683
684     alloc_tables (h);
685   }
686   if (avctx->extradata && avctx->extradata_size >= 0x63
687       && !memcmp (avctx->extradata, "SVQ3", 4)) {
688
689     uint8_t *stsd = (uint8_t *) avctx->extradata + 0x62;
690
691     if ((*stsd >> 5) != 7 || avctx->extradata_size >= 0x66) {
692
693       if ((*stsd >> 5) == 7) {
694         stsd += 3;      /* skip width, height (12 bits each) */
695       }
696
697       h->halfpel_flag = (*stsd >> 4) & 1;
698       h->thirdpel_flag = (*stsd >> 3) & 1;
699     }
700   }
701
702   if ((buf[0] & 0x9F) != 1) {
703     /* TODO: what? */
704     fprintf (stderr, "unsupported header (%02X)\n", buf[0]);
705     return -1;
706   } else {
707     int length = (buf[0] >> 5) & 3;
708     int offset = 0;
709
710     for (i=0; i < length; i++) {
711       offset = (offset << 8) | buf[i + 1];
712     }
713
714     if (buf_size < (offset + length + 1) || length == 0)
715       return -1;
716
717     memcpy (&buf[2], &buf[offset + 2], (length - 1));
718   }
719
720   init_get_bits (&s->gb, &buf[2], 8*(buf_size - 2));
721
722   if ((i = svq3_get_ue_golomb (&s->gb)) == INVALID_VLC || i >= 3)
723     return -1;
724
725   s->pict_type = golomb_to_pict_type[i];
726
727   /* unknown fields */
728   get_bits (&s->gb, 1);
729   get_bits (&s->gb, 8);
730
731   s->qscale = get_bits (&s->gb, 5);
732   s->adaptive_quant = get_bits (&s->gb, 1);
733
734   /* unknown fields */
735   get_bits (&s->gb, 1);
736   get_bits (&s->gb, 1);
737   get_bits (&s->gb, 2);
738
739   while (get_bits (&s->gb, 1)) {
740     get_bits (&s->gb, 8);
741   }
742   
743   if(avctx->debug&FF_DEBUG_PICT_INFO){
744       printf("%c hpel:%d, tpel:%d aqp:%d qp:%d\n", 
745       ff_get_pict_type_char(s->pict_type), h->halfpel_flag, h->thirdpel_flag,
746       s->adaptive_quant, s->qscale
747       );
748   }
749
750   /* B-frames are not supported */
751   if (s->pict_type == B_TYPE/* && avctx->hurry_up*/)
752     return buf_size;
753
754   frame_start (h);
755
756   for(i=0; i<4; i++){
757     int j;
758     for(j=-1; j<4; j++)
759       h->ref_cache[0][scan8[0] + 8*i + j]= 1;
760     h->ref_cache[0][scan8[0] + 8*i + j]= PART_NOT_AVAILABLE;
761   }
762   
763   for (s->mb_y=0; s->mb_y < s->mb_height; s->mb_y++) {
764     for (s->mb_x=0; s->mb_x < s->mb_width; s->mb_x++) {
765       int mb_type = svq3_get_ue_golomb (&s->gb);
766
767       if (s->pict_type == I_TYPE) {
768         mb_type += 8;
769       }
770       if (mb_type > 32 || svq3_decode_mb (h, mb_type)) {
771         fprintf (stderr, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
772         return -1;
773       }
774
775       if (mb_type != 0) {
776         hl_decode_mb (h);
777       }
778     }
779   }
780
781   *(AVFrame *) data = *(AVFrame *) &s->current_picture;
782   *data_size = sizeof(AVFrame);
783
784   MPV_frame_end(s);
785   
786   return buf_size;
787 }
788
789
790 AVCodec svq3_decoder = {
791     "svq3",
792     CODEC_TYPE_VIDEO,
793     CODEC_ID_SVQ3,
794     sizeof(H264Context),
795     decode_init,
796     NULL,
797     decode_end,
798     svq3_decode_frame,
799     CODEC_CAP_DR1,
800 };