• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files

libavcodec/svq3.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2003 The FFmpeg Project.
00003  *
00004  * This file is part of FFmpeg.
00005  *
00006  * FFmpeg is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  *
00011  * FFmpeg is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with FFmpeg; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00019  */
00020 
00021 /*
00022  * How to use this decoder:
00023  * SVQ3 data is transported within Apple Quicktime files. Quicktime files
00024  * have stsd atoms to describe media trak properties. A stsd atom for a
00025  * video trak contains 1 or more ImageDescription atoms. These atoms begin
00026  * with the 4-byte length of the atom followed by the codec fourcc. Some
00027  * decoders need information in this atom to operate correctly. Such
00028  * is the case with SVQ3. In order to get the best use out of this decoder,
00029  * the calling app must make the SVQ3 ImageDescription atom available
00030  * via the AVCodecContext's extradata[_size] field:
00031  *
00032  * AVCodecContext.extradata = pointer to ImageDescription, first characters
00033  * are expected to be 'S', 'V', 'Q', and '3', NOT the 4-byte atom length
00034  * AVCodecContext.extradata_size = size of ImageDescription atom memory
00035  * buffer (which will be the same as the ImageDescription atom size field
00036  * from the QT file, minus 4 bytes since the length is missing)
00037  *
00038  * You will know you have these parameters passed correctly when the decoder
00039  * correctly decodes this file:
00040  *  ftp://ftp.mplayerhq.hu/MPlayer/samples/V-codecs/SVQ3/Vertical400kbit.sorenson3.mov
00041  */
00042 
00048 #define FULLPEL_MODE  1
00049 #define HALFPEL_MODE  2
00050 #define THIRDPEL_MODE 3
00051 #define PREDICT_MODE  4
00052 
00053 /* dual scan (from some older h264 draft)
00054  o-->o-->o   o
00055          |  /|
00056  o   o   o / o
00057  | / |   |/  |
00058  o   o   o   o
00059    /
00060  o-->o-->o-->o
00061 */
00062 static const uint8_t svq3_scan[16]={
00063  0+0*4, 1+0*4, 2+0*4, 2+1*4,
00064  2+2*4, 3+0*4, 3+1*4, 3+2*4,
00065  0+1*4, 0+2*4, 1+1*4, 1+2*4,
00066  0+3*4, 1+3*4, 2+3*4, 3+3*4,
00067 };
00068 
00069 static const uint8_t svq3_pred_0[25][2] = {
00070   { 0, 0 },
00071   { 1, 0 }, { 0, 1 },
00072   { 0, 2 }, { 1, 1 }, { 2, 0 },
00073   { 3, 0 }, { 2, 1 }, { 1, 2 }, { 0, 3 },
00074   { 0, 4 }, { 1, 3 }, { 2, 2 }, { 3, 1 }, { 4, 0 },
00075   { 4, 1 }, { 3, 2 }, { 2, 3 }, { 1, 4 },
00076   { 2, 4 }, { 3, 3 }, { 4, 2 },
00077   { 4, 3 }, { 3, 4 },
00078   { 4, 4 }
00079 };
00080 
00081 static const int8_t svq3_pred_1[6][6][5] = {
00082   { { 2,-1,-1,-1,-1 }, { 2, 1,-1,-1,-1 }, { 1, 2,-1,-1,-1 },
00083     { 2, 1,-1,-1,-1 }, { 1, 2,-1,-1,-1 }, { 1, 2,-1,-1,-1 } },
00084   { { 0, 2,-1,-1,-1 }, { 0, 2, 1, 4, 3 }, { 0, 1, 2, 4, 3 },
00085     { 0, 2, 1, 4, 3 }, { 2, 0, 1, 3, 4 }, { 0, 4, 2, 1, 3 } },
00086   { { 2, 0,-1,-1,-1 }, { 2, 1, 0, 4, 3 }, { 1, 2, 4, 0, 3 },
00087     { 2, 1, 0, 4, 3 }, { 2, 1, 4, 3, 0 }, { 1, 2, 4, 0, 3 } },
00088   { { 2, 0,-1,-1,-1 }, { 2, 0, 1, 4, 3 }, { 1, 2, 0, 4, 3 },
00089     { 2, 1, 0, 4, 3 }, { 2, 1, 3, 4, 0 }, { 2, 4, 1, 0, 3 } },
00090   { { 0, 2,-1,-1,-1 }, { 0, 2, 1, 3, 4 }, { 1, 2, 3, 0, 4 },
00091     { 2, 0, 1, 3, 4 }, { 2, 1, 3, 0, 4 }, { 2, 0, 4, 3, 1 } },
00092   { { 0, 2,-1,-1,-1 }, { 0, 2, 4, 1, 3 }, { 1, 4, 2, 0, 3 },
00093     { 4, 2, 0, 1, 3 }, { 2, 0, 1, 4, 3 }, { 4, 2, 1, 0, 3 } },
00094 };
00095 
00096 static const struct { uint8_t run; uint8_t level; } svq3_dct_tables[2][16] = {
00097   { { 0, 0 }, { 0, 1 }, { 1, 1 }, { 2, 1 }, { 0, 2 }, { 3, 1 }, { 4, 1 }, { 5, 1 },
00098     { 0, 3 }, { 1, 2 }, { 2, 2 }, { 6, 1 }, { 7, 1 }, { 8, 1 }, { 9, 1 }, { 0, 4 } },
00099   { { 0, 0 }, { 0, 1 }, { 1, 1 }, { 0, 2 }, { 2, 1 }, { 0, 3 }, { 0, 4 }, { 0, 5 },
00100     { 3, 1 }, { 4, 1 }, { 1, 2 }, { 1, 3 }, { 0, 6 }, { 0, 7 }, { 0, 8 }, { 0, 9 } }
00101 };
00102 
00103 static const uint32_t svq3_dequant_coeff[32] = {
00104    3881,  4351,  4890,  5481,  6154,  6914,  7761,  8718,
00105    9781, 10987, 12339, 13828, 15523, 17435, 19561, 21873,
00106   24552, 27656, 30847, 34870, 38807, 43747, 49103, 54683,
00107   61694, 68745, 77615, 89113,100253,109366,126635,141533
00108 };
00109 
00110 
00111 static void svq3_luma_dc_dequant_idct_c(DCTELEM *block, int qp){
00112     const int qmul= svq3_dequant_coeff[qp];
00113 #define stride 16
00114     int i;
00115     int temp[16];
00116     static const int x_offset[4]={0, 1*stride, 4* stride,  5*stride};
00117     static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
00118 
00119     for(i=0; i<4; i++){
00120         const int offset= y_offset[i];
00121         const int z0= 13*(block[offset+stride*0] +    block[offset+stride*4]);
00122         const int z1= 13*(block[offset+stride*0] -    block[offset+stride*4]);
00123         const int z2=  7* block[offset+stride*1] - 17*block[offset+stride*5];
00124         const int z3= 17* block[offset+stride*1] +  7*block[offset+stride*5];
00125 
00126         temp[4*i+0]= z0+z3;
00127         temp[4*i+1]= z1+z2;
00128         temp[4*i+2]= z1-z2;
00129         temp[4*i+3]= z0-z3;
00130     }
00131 
00132     for(i=0; i<4; i++){
00133         const int offset= x_offset[i];
00134         const int z0= 13*(temp[4*0+i] +    temp[4*2+i]);
00135         const int z1= 13*(temp[4*0+i] -    temp[4*2+i]);
00136         const int z2=  7* temp[4*1+i] - 17*temp[4*3+i];
00137         const int z3= 17* temp[4*1+i] +  7*temp[4*3+i];
00138 
00139         block[stride*0 +offset]= ((z0 + z3)*qmul + 0x80000)>>20;
00140         block[stride*2 +offset]= ((z1 + z2)*qmul + 0x80000)>>20;
00141         block[stride*8 +offset]= ((z1 - z2)*qmul + 0x80000)>>20;
00142         block[stride*10+offset]= ((z0 - z3)*qmul + 0x80000)>>20;
00143     }
00144 }
00145 #undef stride
00146 
00147 static void svq3_add_idct_c (uint8_t *dst, DCTELEM *block, int stride, int qp, int dc){
00148     const int qmul= svq3_dequant_coeff[qp];
00149     int i;
00150     uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
00151 
00152     if (dc) {
00153         dc = 13*13*((dc == 1) ? 1538*block[0] : ((qmul*(block[0] >> 3)) / 2));
00154         block[0] = 0;
00155     }
00156 
00157     for (i=0; i < 4; i++) {
00158         const int z0= 13*(block[0 + 4*i] +    block[2 + 4*i]);
00159         const int z1= 13*(block[0 + 4*i] -    block[2 + 4*i]);
00160         const int z2=  7* block[1 + 4*i] - 17*block[3 + 4*i];
00161         const int z3= 17* block[1 + 4*i] +  7*block[3 + 4*i];
00162 
00163         block[0 + 4*i]= z0 + z3;
00164         block[1 + 4*i]= z1 + z2;
00165         block[2 + 4*i]= z1 - z2;
00166         block[3 + 4*i]= z0 - z3;
00167     }
00168 
00169     for (i=0; i < 4; i++) {
00170         const int z0= 13*(block[i + 4*0] +    block[i + 4*2]);
00171         const int z1= 13*(block[i + 4*0] -    block[i + 4*2]);
00172         const int z2=  7* block[i + 4*1] - 17*block[i + 4*3];
00173         const int z3= 17* block[i + 4*1] +  7*block[i + 4*3];
00174         const int rr= (dc + 0x80000);
00175 
00176         dst[i + stride*0]= cm[ dst[i + stride*0] + (((z0 + z3)*qmul + rr) >> 20) ];
00177         dst[i + stride*1]= cm[ dst[i + stride*1] + (((z1 + z2)*qmul + rr) >> 20) ];
00178         dst[i + stride*2]= cm[ dst[i + stride*2] + (((z1 - z2)*qmul + rr) >> 20) ];
00179         dst[i + stride*3]= cm[ dst[i + stride*3] + (((z0 - z3)*qmul + rr) >> 20) ];
00180     }
00181 }
00182 
00183 static inline int svq3_decode_block (GetBitContext *gb, DCTELEM *block,
00184                                      int index, const int type) {
00185 
00186   static const uint8_t *const scan_patterns[4] =
00187   { luma_dc_zigzag_scan, zigzag_scan, svq3_scan, chroma_dc_scan };
00188 
00189   int run, level, sign, vlc, limit;
00190   const int intra = (3 * type) >> 2;
00191   const uint8_t *const scan = scan_patterns[type];
00192 
00193   for (limit=(16 >> intra); index < 16; index=limit, limit+=8) {
00194     for (; (vlc = svq3_get_ue_golomb (gb)) != 0; index++) {
00195 
00196       if (vlc == INVALID_VLC)
00197         return -1;
00198 
00199       sign = (vlc & 0x1) - 1;
00200       vlc  = (vlc + 1) >> 1;
00201 
00202       if (type == 3) {
00203         if (vlc < 3) {
00204           run   = 0;
00205           level = vlc;
00206         } else if (vlc < 4) {
00207           run   = 1;
00208           level = 1;
00209         } else {
00210           run   = (vlc & 0x3);
00211           level = ((vlc + 9) >> 2) - run;
00212         }
00213       } else {
00214         if (vlc < 16) {
00215           run   = svq3_dct_tables[intra][vlc].run;
00216           level = svq3_dct_tables[intra][vlc].level;
00217         } else if (intra) {
00218           run   = (vlc & 0x7);
00219           level = (vlc >> 3) + ((run == 0) ? 8 : ((run < 2) ? 2 : ((run < 5) ? 0 : -1)));
00220         } else {
00221           run   = (vlc & 0xF);
00222           level = (vlc >> 4) + ((run == 0) ? 4 : ((run < 3) ? 2 : ((run < 10) ? 1 : 0)));
00223         }
00224       }
00225 
00226       if ((index += run) >= limit)
00227         return -1;
00228 
00229       block[scan[index]] = (level ^ sign) - sign;
00230     }
00231 
00232     if (type != 2) {
00233       break;
00234     }
00235   }
00236 
00237   return 0;
00238 }
00239 
00240 static inline void svq3_mc_dir_part (MpegEncContext *s,
00241                                      int x, int y, int width, int height,
00242                                      int mx, int my, int dxy,
00243                                      int thirdpel, int dir, int avg) {
00244 
00245   const Picture *pic = (dir == 0) ? &s->last_picture : &s->next_picture;
00246   uint8_t *src, *dest;
00247   int i, emu = 0;
00248   int blocksize= 2 - (width>>3); //16->0, 8->1, 4->2
00249 
00250   mx += x;
00251   my += y;
00252 
00253   if (mx < 0 || mx >= (s->h_edge_pos - width  - 1) ||
00254       my < 0 || my >= (s->v_edge_pos - height - 1)) {
00255 
00256     if ((s->flags & CODEC_FLAG_EMU_EDGE)) {
00257       emu = 1;
00258     }
00259 
00260     mx = av_clip (mx, -16, (s->h_edge_pos - width  + 15));
00261     my = av_clip (my, -16, (s->v_edge_pos - height + 15));
00262   }
00263 
00264   /* form component predictions */
00265   dest = s->current_picture.data[0] + x + y*s->linesize;
00266   src  = pic->data[0] + mx + my*s->linesize;
00267 
00268   if (emu) {
00269     ff_emulated_edge_mc (s->edge_emu_buffer, src, s->linesize, (width + 1), (height + 1),
00270                          mx, my, s->h_edge_pos, s->v_edge_pos);
00271     src = s->edge_emu_buffer;
00272   }
00273   if(thirdpel)
00274     (avg ? s->dsp.avg_tpel_pixels_tab : s->dsp.put_tpel_pixels_tab)[dxy](dest, src, s->linesize, width, height);
00275   else
00276     (avg ? s->dsp.avg_pixels_tab : s->dsp.put_pixels_tab)[blocksize][dxy](dest, src, s->linesize, height);
00277 
00278   if (!(s->flags & CODEC_FLAG_GRAY)) {
00279     mx     = (mx + (mx < (int) x)) >> 1;
00280     my     = (my + (my < (int) y)) >> 1;
00281     width  = (width  >> 1);
00282     height = (height >> 1);
00283     blocksize++;
00284 
00285     for (i=1; i < 3; i++) {
00286       dest = s->current_picture.data[i] + (x >> 1) + (y >> 1)*s->uvlinesize;
00287       src  = pic->data[i] + mx + my*s->uvlinesize;
00288 
00289       if (emu) {
00290         ff_emulated_edge_mc (s->edge_emu_buffer, src, s->uvlinesize, (width + 1), (height + 1),
00291                              mx, my, (s->h_edge_pos >> 1), (s->v_edge_pos >> 1));
00292         src = s->edge_emu_buffer;
00293       }
00294       if(thirdpel)
00295         (avg ? s->dsp.avg_tpel_pixels_tab : s->dsp.put_tpel_pixels_tab)[dxy](dest, src, s->uvlinesize, width, height);
00296       else
00297         (avg ? s->dsp.avg_pixels_tab : s->dsp.put_pixels_tab)[blocksize][dxy](dest, src, s->uvlinesize, height);
00298     }
00299   }
00300 }
00301 
00302 static inline int svq3_mc_dir (H264Context *h, int size, int mode, int dir, int avg) {
00303 
00304   int i, j, k, mx, my, dx, dy, x, y;
00305   MpegEncContext *const s = (MpegEncContext *) h;
00306   const int part_width  = ((size & 5) == 4) ? 4 : 16 >> (size & 1);
00307   const int part_height = 16 >> ((unsigned) (size + 1) / 3);
00308   const int extra_width = (mode == PREDICT_MODE) ? -16*6 : 0;
00309   const int h_edge_pos  = 6*(s->h_edge_pos - part_width ) - extra_width;
00310   const int v_edge_pos  = 6*(s->v_edge_pos - part_height) - extra_width;
00311 
00312   for (i=0; i < 16; i+=part_height) {
00313     for (j=0; j < 16; j+=part_width) {
00314       const int b_xy = (4*s->mb_x+(j>>2)) + (4*s->mb_y+(i>>2))*h->b_stride;
00315       int dxy;
00316       x = 16*s->mb_x + j;
00317       y = 16*s->mb_y + i;
00318       k = ((j>>2)&1) + ((i>>1)&2) + ((j>>1)&4) + (i&8);
00319 
00320       if (mode != PREDICT_MODE) {
00321         pred_motion (h, k, (part_width >> 2), dir, 1, &mx, &my);
00322       } else {
00323         mx = s->next_picture.motion_val[0][b_xy][0]<<1;
00324         my = s->next_picture.motion_val[0][b_xy][1]<<1;
00325 
00326         if (dir == 0) {
00327           mx = ((mx * h->frame_num_offset) / h->prev_frame_num_offset + 1)>>1;
00328           my = ((my * h->frame_num_offset) / h->prev_frame_num_offset + 1)>>1;
00329         } else {
00330           mx = ((mx * (h->frame_num_offset - h->prev_frame_num_offset)) / h->prev_frame_num_offset + 1)>>1;
00331           my = ((my * (h->frame_num_offset - h->prev_frame_num_offset)) / h->prev_frame_num_offset + 1)>>1;
00332         }
00333       }
00334 
00335       /* clip motion vector prediction to frame border */
00336       mx = av_clip (mx, extra_width - 6*x, h_edge_pos - 6*x);
00337       my = av_clip (my, extra_width - 6*y, v_edge_pos - 6*y);
00338 
00339       /* get (optional) motion vector differential */
00340       if (mode == PREDICT_MODE) {
00341         dx = dy = 0;
00342       } else {
00343         dy = svq3_get_se_golomb (&s->gb);
00344         dx = svq3_get_se_golomb (&s->gb);
00345 
00346         if (dx == INVALID_VLC || dy == INVALID_VLC) {
00347           av_log(h->s.avctx, AV_LOG_ERROR, "invalid MV vlc\n");
00348           return -1;
00349         }
00350       }
00351 
00352       /* compute motion vector */
00353       if (mode == THIRDPEL_MODE) {
00354         int fx, fy;
00355         mx = ((mx + 1)>>1) + dx;
00356         my = ((my + 1)>>1) + dy;
00357         fx= ((unsigned)(mx + 0x3000))/3 - 0x1000;
00358         fy= ((unsigned)(my + 0x3000))/3 - 0x1000;
00359         dxy= (mx - 3*fx) + 4*(my - 3*fy);
00360 
00361         svq3_mc_dir_part (s, x, y, part_width, part_height, fx, fy, dxy, 1, dir, avg);
00362         mx += mx;
00363         my += my;
00364       } else if (mode == HALFPEL_MODE || mode == PREDICT_MODE) {
00365         mx = ((unsigned)(mx + 1 + 0x3000))/3 + dx - 0x1000;
00366         my = ((unsigned)(my + 1 + 0x3000))/3 + dy - 0x1000;
00367         dxy= (mx&1) + 2*(my&1);
00368 
00369         svq3_mc_dir_part (s, x, y, part_width, part_height, mx>>1, my>>1, dxy, 0, dir, avg);
00370         mx *= 3;
00371         my *= 3;
00372       } else {
00373         mx = ((unsigned)(mx + 3 + 0x6000))/6 + dx - 0x1000;
00374         my = ((unsigned)(my + 3 + 0x6000))/6 + dy - 0x1000;
00375 
00376         svq3_mc_dir_part (s, x, y, part_width, part_height, mx, my, 0, 0, dir, avg);
00377         mx *= 6;
00378         my *= 6;
00379       }
00380 
00381       /* update mv_cache */
00382       if (mode != PREDICT_MODE) {
00383         int32_t mv = pack16to32(mx,my);
00384 
00385         if (part_height == 8 && i < 8) {
00386           *(int32_t *) h->mv_cache[dir][scan8[k] + 1*8] = mv;
00387 
00388           if (part_width == 8 && j < 8) {
00389             *(int32_t *) h->mv_cache[dir][scan8[k] + 1 + 1*8] = mv;
00390           }
00391         }
00392         if (part_width == 8 && j < 8) {
00393           *(int32_t *) h->mv_cache[dir][scan8[k] + 1] = mv;
00394         }
00395         if (part_width == 4 || part_height == 4) {
00396           *(int32_t *) h->mv_cache[dir][scan8[k]] = mv;
00397         }
00398       }
00399 
00400       /* write back motion vectors */
00401       fill_rectangle(s->current_picture.motion_val[dir][b_xy], part_width>>2, part_height>>2, h->b_stride, pack16to32(mx,my), 4);
00402     }
00403   }
00404 
00405   return 0;
00406 }
00407 
00408 static int svq3_decode_mb (H264Context *h, unsigned int mb_type) {
00409   int i, j, k, m, dir, mode;
00410   int cbp = 0;
00411   uint32_t vlc;
00412   int8_t *top, *left;
00413   MpegEncContext *const s = (MpegEncContext *) h;
00414   const int mb_xy = s->mb_x + s->mb_y*s->mb_stride;
00415   const int b_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride;
00416 
00417   h->top_samples_available        = (s->mb_y == 0) ? 0x33FF : 0xFFFF;
00418   h->left_samples_available        = (s->mb_x == 0) ? 0x5F5F : 0xFFFF;
00419   h->topright_samples_available        = 0xFFFF;
00420 
00421   if (mb_type == 0) {           /* SKIP */
00422     if (s->pict_type == P_TYPE || s->next_picture.mb_type[mb_xy] == -1) {
00423       svq3_mc_dir_part (s, 16*s->mb_x, 16*s->mb_y, 16, 16, 0, 0, 0, 0, 0, 0);
00424 
00425       if (s->pict_type == B_TYPE) {
00426         svq3_mc_dir_part (s, 16*s->mb_x, 16*s->mb_y, 16, 16, 0, 0, 0, 0, 1, 1);
00427       }
00428 
00429       mb_type = MB_TYPE_SKIP;
00430     } else {
00431       mb_type= FFMIN(s->next_picture.mb_type[mb_xy], 6);
00432       if(svq3_mc_dir (h, mb_type, PREDICT_MODE, 0, 0) < 0)
00433         return -1;
00434       if(svq3_mc_dir (h, mb_type, PREDICT_MODE, 1, 1) < 0)
00435         return -1;
00436 
00437       mb_type = MB_TYPE_16x16;
00438     }
00439   } else if (mb_type < 8) {     /* INTER */
00440     if (h->thirdpel_flag && h->halfpel_flag == !get_bits1 (&s->gb)) {
00441       mode = THIRDPEL_MODE;
00442     } else if (h->halfpel_flag && h->thirdpel_flag == !get_bits1 (&s->gb)) {
00443       mode = HALFPEL_MODE;
00444     } else {
00445       mode = FULLPEL_MODE;
00446     }
00447 
00448     /* fill caches */
00449     /* note ref_cache should contain here:
00450         ????????
00451         ???11111
00452         N??11111
00453         N??11111
00454         N??11111
00455     */
00456 
00457     for (m=0; m < 2; m++) {
00458       if (s->mb_x > 0 && h->intra4x4_pred_mode[mb_xy - 1][0] != -1) {
00459         for (i=0; i < 4; i++) {
00460           *(uint32_t *) h->mv_cache[m][scan8[0] - 1 + i*8] = *(uint32_t *) s->current_picture.motion_val[m][b_xy - 1 + i*h->b_stride];
00461         }
00462       } else {
00463         for (i=0; i < 4; i++) {
00464           *(uint32_t *) h->mv_cache[m][scan8[0] - 1 + i*8] = 0;
00465         }
00466       }
00467       if (s->mb_y > 0) {
00468         memcpy (h->mv_cache[m][scan8[0] - 1*8], s->current_picture.motion_val[m][b_xy - h->b_stride], 4*2*sizeof(int16_t));
00469         memset (&h->ref_cache[m][scan8[0] - 1*8], (h->intra4x4_pred_mode[mb_xy - s->mb_stride][4] == -1) ? PART_NOT_AVAILABLE : 1, 4);
00470 
00471         if (s->mb_x < (s->mb_width - 1)) {
00472           *(uint32_t *) h->mv_cache[m][scan8[0] + 4 - 1*8] = *(uint32_t *) s->current_picture.motion_val[m][b_xy - h->b_stride + 4];
00473           h->ref_cache[m][scan8[0] + 4 - 1*8] =
00474                   (h->intra4x4_pred_mode[mb_xy - s->mb_stride + 1][0] == -1 ||
00475                    h->intra4x4_pred_mode[mb_xy - s->mb_stride][4] == -1) ? PART_NOT_AVAILABLE : 1;
00476         }else
00477           h->ref_cache[m][scan8[0] + 4 - 1*8] = PART_NOT_AVAILABLE;
00478         if (s->mb_x > 0) {
00479           *(uint32_t *) h->mv_cache[m][scan8[0] - 1 - 1*8] = *(uint32_t *) s->current_picture.motion_val[m][b_xy - h->b_stride - 1];
00480           h->ref_cache[m][scan8[0] - 1 - 1*8] = (h->intra4x4_pred_mode[mb_xy - s->mb_stride - 1][3] == -1) ? PART_NOT_AVAILABLE : 1;
00481         }else
00482           h->ref_cache[m][scan8[0] - 1 - 1*8] = PART_NOT_AVAILABLE;
00483       }else
00484         memset (&h->ref_cache[m][scan8[0] - 1*8 - 1], PART_NOT_AVAILABLE, 8);
00485 
00486       if (s->pict_type != B_TYPE)
00487         break;
00488     }
00489 
00490     /* decode motion vector(s) and form prediction(s) */
00491     if (s->pict_type == P_TYPE) {
00492       if(svq3_mc_dir (h, (mb_type - 1), mode, 0, 0) < 0)
00493         return -1;
00494     } else {        /* B_TYPE */
00495       if (mb_type != 2) {
00496         if(svq3_mc_dir (h, 0, mode, 0, 0) < 0)
00497           return -1;
00498       } else {
00499         for (i=0; i < 4; i++) {
00500           memset (s->current_picture.motion_val[0][b_xy + i*h->b_stride], 0, 4*2*sizeof(int16_t));
00501         }
00502       }
00503       if (mb_type != 1) {
00504         if(svq3_mc_dir (h, 0, mode, 1, (mb_type == 3)) < 0)
00505           return -1;
00506       } else {
00507         for (i=0; i < 4; i++) {
00508           memset (s->current_picture.motion_val[1][b_xy + i*h->b_stride], 0, 4*2*sizeof(int16_t));
00509         }
00510       }
00511     }
00512 
00513     mb_type = MB_TYPE_16x16;
00514   } else if (mb_type == 8 || mb_type == 33) {   /* INTRA4x4 */
00515     memset (h->intra4x4_pred_mode_cache, -1, 8*5*sizeof(int8_t));
00516 
00517     if (mb_type == 8) {
00518       if (s->mb_x > 0) {
00519         for (i=0; i < 4; i++) {
00520           h->intra4x4_pred_mode_cache[scan8[0] - 1 + i*8] = h->intra4x4_pred_mode[mb_xy - 1][i];
00521         }
00522         if (h->intra4x4_pred_mode_cache[scan8[0] - 1] == -1) {
00523           h->left_samples_available = 0x5F5F;
00524         }
00525       }
00526       if (s->mb_y > 0) {
00527         h->intra4x4_pred_mode_cache[4+8*0] = h->intra4x4_pred_mode[mb_xy - s->mb_stride][4];
00528         h->intra4x4_pred_mode_cache[5+8*0] = h->intra4x4_pred_mode[mb_xy - s->mb_stride][5];
00529         h->intra4x4_pred_mode_cache[6+8*0] = h->intra4x4_pred_mode[mb_xy - s->mb_stride][6];
00530         h->intra4x4_pred_mode_cache[7+8*0] = h->intra4x4_pred_mode[mb_xy - s->mb_stride][3];
00531 
00532         if (h->intra4x4_pred_mode_cache[4+8*0] == -1) {
00533           h->top_samples_available = 0x33FF;
00534         }
00535       }
00536 
00537       /* decode prediction codes for luma blocks */
00538       for (i=0; i < 16; i+=2) {
00539         vlc = svq3_get_ue_golomb (&s->gb);
00540 
00541         if (vlc >= 25){
00542           av_log(h->s.avctx, AV_LOG_ERROR, "luma prediction:%d\n", vlc);
00543           return -1;
00544         }
00545 
00546         left    = &h->intra4x4_pred_mode_cache[scan8[i] - 1];
00547         top     = &h->intra4x4_pred_mode_cache[scan8[i] - 8];
00548 
00549         left[1] = svq3_pred_1[top[0] + 1][left[0] + 1][svq3_pred_0[vlc][0]];
00550         left[2] = svq3_pred_1[top[1] + 1][left[1] + 1][svq3_pred_0[vlc][1]];
00551 
00552         if (left[1] == -1 || left[2] == -1){
00553           av_log(h->s.avctx, AV_LOG_ERROR, "weird prediction\n");
00554           return -1;
00555         }
00556       }
00557     } else {    /* mb_type == 33, DC_128_PRED block type */
00558       for (i=0; i < 4; i++) {
00559         memset (&h->intra4x4_pred_mode_cache[scan8[0] + 8*i], DC_PRED, 4);
00560       }
00561     }
00562 
00563     write_back_intra_pred_mode (h);
00564 
00565     if (mb_type == 8) {
00566       check_intra4x4_pred_mode (h);
00567 
00568       h->top_samples_available  = (s->mb_y == 0) ? 0x33FF : 0xFFFF;
00569       h->left_samples_available = (s->mb_x == 0) ? 0x5F5F : 0xFFFF;
00570     } else {
00571       for (i=0; i < 4; i++) {
00572         memset (&h->intra4x4_pred_mode_cache[scan8[0] + 8*i], DC_128_PRED, 4);
00573       }
00574 
00575       h->top_samples_available  = 0x33FF;
00576       h->left_samples_available = 0x5F5F;
00577     }
00578 
00579     mb_type = MB_TYPE_INTRA4x4;
00580   } else {                      /* INTRA16x16 */
00581     dir = i_mb_type_info[mb_type - 8].pred_mode;
00582     dir = (dir >> 1) ^ 3*(dir & 1) ^ 1;
00583 
00584     if ((h->intra16x16_pred_mode = check_intra_pred_mode (h, dir)) == -1){
00585       av_log(h->s.avctx, AV_LOG_ERROR, "check_intra_pred_mode = -1\n");
00586       return -1;
00587     }
00588 
00589     cbp = i_mb_type_info[mb_type - 8].cbp;
00590     mb_type = MB_TYPE_INTRA16x16;
00591   }
00592 
00593   if (!IS_INTER(mb_type) && s->pict_type != I_TYPE) {
00594     for (i=0; i < 4; i++) {
00595       memset (s->current_picture.motion_val[0][b_xy + i*h->b_stride], 0, 4*2*sizeof(int16_t));
00596     }
00597     if (s->pict_type == B_TYPE) {
00598       for (i=0; i < 4; i++) {
00599         memset (s->current_picture.motion_val[1][b_xy + i*h->b_stride], 0, 4*2*sizeof(int16_t));
00600       }
00601     }
00602   }
00603   if (!IS_INTRA4x4(mb_type)) {
00604     memset (h->intra4x4_pred_mode[mb_xy], DC_PRED, 8);
00605   }
00606   if (!IS_SKIP(mb_type) || s->pict_type == B_TYPE) {
00607     memset (h->non_zero_count_cache + 8, 0, 4*9*sizeof(uint8_t));
00608     s->dsp.clear_blocks(h->mb);
00609   }
00610 
00611   if (!IS_INTRA16x16(mb_type) && (!IS_SKIP(mb_type) || s->pict_type == B_TYPE)) {
00612     if ((vlc = svq3_get_ue_golomb (&s->gb)) >= 48){
00613       av_log(h->s.avctx, AV_LOG_ERROR, "cbp_vlc=%d\n", vlc);
00614       return -1;
00615     }
00616 
00617     cbp = IS_INTRA(mb_type) ? golomb_to_intra4x4_cbp[vlc] : golomb_to_inter_cbp[vlc];
00618   }
00619   if (IS_INTRA16x16(mb_type) || (s->pict_type != I_TYPE && s->adaptive_quant && cbp)) {
00620     s->qscale += svq3_get_se_golomb (&s->gb);
00621 
00622     if (s->qscale > 31){
00623       av_log(h->s.avctx, AV_LOG_ERROR, "qscale:%d\n", s->qscale);
00624       return -1;
00625     }
00626   }
00627   if (IS_INTRA16x16(mb_type)) {
00628     if (svq3_decode_block (&s->gb, h->mb, 0, 0)){
00629       av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding intra luma dc\n");
00630       return -1;
00631     }
00632   }
00633 
00634   if (cbp) {
00635     const int index = IS_INTRA16x16(mb_type) ? 1 : 0;
00636     const int type = ((s->qscale < 24 && IS_INTRA4x4(mb_type)) ? 2 : 1);
00637 
00638     for (i=0; i < 4; i++) {
00639       if ((cbp & (1 << i))) {
00640         for (j=0; j < 4; j++) {
00641           k = index ? ((j&1) + 2*(i&1) + 2*(j&2) + 4*(i&2)) : (4*i + j);
00642           h->non_zero_count_cache[ scan8[k] ] = 1;
00643 
00644           if (svq3_decode_block (&s->gb, &h->mb[16*k], index, type)){
00645             av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding block\n");
00646             return -1;
00647           }
00648         }
00649       }
00650     }
00651 
00652     if ((cbp & 0x30)) {
00653       for (i=0; i < 2; ++i) {
00654         if (svq3_decode_block (&s->gb, &h->mb[16*(16 + 4*i)], 0, 3)){
00655           av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding chroma dc block\n");
00656           return -1;
00657         }
00658       }
00659 
00660       if ((cbp & 0x20)) {
00661         for (i=0; i < 8; i++) {
00662           h->non_zero_count_cache[ scan8[16+i] ] = 1;
00663 
00664           if (svq3_decode_block (&s->gb, &h->mb[16*(16 + i)], 1, 1)){
00665             av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding chroma ac block\n");
00666             return -1;
00667           }
00668         }
00669       }
00670     }
00671   }
00672 
00673   s->current_picture.mb_type[mb_xy] = mb_type;
00674 
00675   if (IS_INTRA(mb_type)) {
00676     h->chroma_pred_mode = check_intra_pred_mode (h, DC_PRED8x8);
00677   }
00678 
00679   return 0;
00680 }
00681 
00682 static46             retu;
00605   }
00606   if (!IS_SKIP(mb_type) || s->pict_type == B_TYPE) {
00607     memset (h->non_zero_count_cache + 8, 0, 4*9*sizeof(uint8_t));
00608     s->dsp.clear_blocks(h->mb);
00609   }
00610 
00611   if (!IS_INTRA16x16(mb_type) && (!IS_SKIP(mb_type) || s->pict_type == B_TYPE)) {
00612     if ((vlc = svq3_get_ue_golomb (&s->gb)) >= 48){
00613       av_log(h->s.avctx, AV_LOG_ERROR, "cbp_vlc=%d\n", vlc);
00614       return -1;
00615     }
00616 
00617     cbp = IS_INTRA(mb_type) ? golomb_to_intra4x4_cbp[vlc] : golomb_to_inter_cbp[vlc];
00618   }
00619   if (IS_INTRA16x16(mb_type) || (s->pict_type != I_TYPE && s->adaptive_quant && cbp)) {
00620     s->qscale += svq3_get_se_golomb (&s->gb);
00621 
00622     if (s->qscale > 31){
00623       av_log(h->s.avctx, AV_LOG_ERROR, "qscale:%d\n", s->qscale);
00624       return -1;
00625     }
00626   }
00627   if (IS_INTRA16x16(mb_type)) {
00628     if (svq3_decode_block (&s->gb, h->mb, 0, 0)){
00629       av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding intra luma dc\n");
00630       return -1;
00631     }
00632   }
00633 
00634   if (cbp) {
00635     const int index = IS_INTRA16x16(mb_type) ? 1 : 0;
00636     const int type = ((s->qscale < 24 && IS_INTRA4x4(mb_type)) ? 2 : 1);
00637 
00638     for (i=0; i < 4; i++) {
00639       if ((cbp & (1 << i))) {
00640         for (j=0; j < 4; j++) {
00641           k = index ? ((j&1) + 2*(i&1) + 2*(j&2) + 4*(i&2)) : (4*i + j);
00642           h->non_zero_count_cache[ scan8[k] ] = 1;
00643 
00644           if (svq3_decode_block (&s->gb, &h->mb[16*k], index, type)){
00645             av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding block\n");
00646             return -1;
00647           }
00648         }
00649       }
00650     }
00651 
00652     if ((cbp & 0x30)) {
00653       for (i=0; i < 2; ++i) {
00654         if (svq3_decode_block (&s->gb, &h->mb[16*(16 + 4*i)], 0, 3)){
00655           av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding chroma dc block\n");
00656           return -1;
00657         }
00658       }
00659 
00660       if ((cbp & 0x20)) {
00661         for (i=0; i < 8; i++) {
00662           h->non_zero_count_cache[ scan8[16+i] ] = 1;
00663 
00664           if (svq3_decode_block (&s->gb, &h->mb[16*(16 + i)], 1, 1)){
00665             av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding chroma ac block\n");
00666             return -1;
00667           }
00668         }
00669       }
00670     }
00671   }
00672 
00673   s->current_picture.mb_type[mb_xy] = mb_type;
00674 
00675   if (IS_INTRA(mb_type)) {
00676     h->chroma_pred_mode = check_intra_pred_mode (h, DC_PRED8x8);
00677   }
00678 
00679   return 0;
00680 }
00681 
00682 static46             retu;
00605   }
00606   if (!IS_SKIP(mb_type) || s->pict_type == B_TYPE) {
00607     memset (h->non_zero_count_cache + 8, 0, 4*9*sizeof(uint8_t));
00608     s->dsp.clear_blocks(h->mb);
00609   }
00610 
00611   if (!IS_INTRA16x16(mb_type) && (!IS_SKIP(mb_type) || s->pict_type == B_TYPE)) {
00612     if ((vlc = svq3_get_ue_golomb (&s->gb)) >= 48){
00613       av_log(h->s.avctx, AV_LOG_ERROR, "cbp_vlc=%d\n", vlc);
00614       return -1;
00615     }
00616 
00617     cbp = IS_INTRA(mb_type) ? golomb_to_intra4x4_cbp[vlc] : golomb_to_inter_cbp[vlc];
00618   }
00619   if (IS_INTRA16x16(mb_type) || (s->pict_type != I_TYPE && s->adaptive_quant && cbp)) {
00620     s->qscale += svq3_get_se_golomb (&s->gb);
00621 
00622     if (s->qscale > 31){
00623       av_log(h->s.avctx, AV_LOG_ERROR, "qscale:%d\n", s->qscale);
00624       return -1;
00625     }
00626   }
00627   if (IS_INTRA16x16(mb_type)) {
00628     if (svq3_decode_block (&s->gb, h->mb, 0, 0)){
00629       av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding intra luma dc\n");
00630       return -1;
00631     }
00632   }
00633 
00634   if (cbp) {
00635     const int index = IS_INTRA16x16(mb_type) ? 1 : 0;
00636     const int type = ((s->qscale < 24 && IS_INTRA4x4(mb_type)) ? 2 : 1);
00637 
00638     for (i=0; i < 4; i++) {
00639       if ((cbp & (1 << i))) {
00640         for (j=0; j < 4; j++) {
00641           k = index ? ((j&1) + 2*(i&1) + 2*(j&2) + 4*(i&2)) : (4*i + j);
00642           h->non_zero_count_cache[ scan8[k] ] = 1;
00643 
00644           if (svq3_decode_block (&s->gb, &h->mb[16*k], index, type)){
00645             av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding block\n");
00646             return -1;
00647           }
00648         }
00649       }
00650     }
00651 
00652     if ((cbp & 0x30)) {
00653       for (i=0; i < 2; ++i) {
00654         if (svq3_decode_block (&s->gb, &h->mb[16*(16 + 4*i)], 0, 3)){
00655           av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding chroma dc block\n");
00656           return -1;
00657         }
00658       }
00659 
00660       if ((cbp & 0x20)) {
00661         for (i=0; i < 8; i++) {
00662           h->non_zero_count_cache[ scan8[16+i] ] = 1;
00663 
00664           if (svq3_decode_block (&s->gb, &h->mb[16*(16 + i)], 1, 1)){
00665             av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding chroma ac block\n");
00666             return -1;
00667           }
00668         }
00669       }
00670     }
00671   }
00672 
00673   s->current_picture.mb_type[mb_xy] = mb_type;
00674 
00675   if (IS_INTRA(mb_type)) {
00676     h->chroma_pred_mode = check_intra_pred_mode (h, DC_PRED8x8);
00677   }
00678 
00679   return 0;
00680 }
00681 
00682 static46             retu;
00605   }
00606   if (!IS_SKIP(mb_type) || s->pict_type == B_TYPE) {
00607     memset (h->non_zero_count_cache + 8, 0, 4*9*sizeof(uint8_t));
00608     s->dsp.clear_blocks(h->mb);
00609   }
00610 
00611   if (!IS_INTRA16x16(mb_type) && (!IS_SKIP(mb_type) || s->pict_type == B_TYPE)) {
00612     if ((vlc = svq3_get_ue_golomb (&s->gb)) >= 48){
00613       av_log(h->s.avctx, AV_LOG_ERROR, "cbp_vlc=%d\n", vlc);
00614       return -1;
00615     }
00616 
00617     cbp = IS_INTRA(mb_type) ? golomb_to_intra4x4_cbp[vlc] : golomb_to_inter_cbp[vlc];
00618   }
00619   if (IS_INTRA16x16(mb_type) || (s->pict_type != I_TYPE && s->adaptive_quant && cbp)) {
00620     s->qscale += svq3_get_se_golomb (&s->gb);
00621 
00622     if (s->qscale > 31){
00623       av_log(h->s.avctx, AV_LOG_ERROR, "qscale:%d\n", s->qscale);
00624       return -1;
00625     }
00626   }
00627   if (IS_INTRA16x16(mb_type)) {
00628     if (svq3_decode_block (&s->gb, h->mb, 0, 0)){
00629       av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding intra luma dc\n");
00630       return -1;
00631     }
00632   }
00633 
00634   if (cbp) {
00635     const int index = IS_INTRA16x16(mb_type) ? 1 : 0;
00636     const int type = ((s->qscale < 24 && IS_INTRA4x4(mb_type)) ? 2 : 1);
00637 
00638     for (i=0; i < 4; i++) {
00639       if ((cbp & (1 << i))) {
00640         for (j=0; j < 4; j++) {
00641           k = index ? ((j&1) + 2*(i&1) + 2*(j&2) + 4*(i&2)) : (4*i + j);
00642           h->non_zero_count_cache[ scan8[k] ] = 1;
00643 
00644           if (svq3_decode_block (&s->gb, &h->mb[16*k], index, type)){
00645             av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding block\n");
00646             return -1;
00647           }
00648         }
00649       }
00650     }
00651 
00652     if ((cbp & 0x30)) {
00653       for (i=0; i < 2; ++i) {
00654         if (svq3_decode_block (&s->gb, &h->mb[16*(16 + 4*i)], 0, 3)){
00655           av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding chroma dc block\n");
00656           return -1;
00657         }
00658       }
00659 
00660       if ((cbp & 0x20)) {
00661         for (i=0; i < 8; i++) {
00662           h->non_zero_count_cache[ scan8[16+i] ] = 1;
00663 
00664           if (svq3_decode_block (&s->gb, &h->mb[16*(16 + i)], 1, 1)){
00665             av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding chroma ac block\n");
00666             return -1;
00667           }
00668         }
00669       }
00670     }
00671   }
00672 
00673   s->current_picture.mb_type[mb_xy] = mb_type;
00674 
00675   if (IS_INTRA(mb_type)) {
00676     h->chroma_pred_mode = check_intra_pred_mode (h, DC_PRED8x8);
00677   }
00678 
00679   return 0;
00680 }
00681 
00682 static46             retu;
00605   }
00606   if (!IS_SKIP(mb_type) || s->pict_type == B_TYPE) {
00607     memset (h->non_zero_count_cache + 8, 0, 4*9*sizeof(uint8_t));
00608     s->dsp.clear_blocks(h->mb);
00609   }
00610 
00611   if (!IS_INTRA16x16(mb_type) && (!IS_SKIP(mb_type) || s->pict_type == B_TYPE)) {
00612     if ((vlc = svq3_get_ue_golomb (&s->gb)) >= 48){
00613       av_log(h->s.avctx, AV_LOG_ERROR, "cbp_vlc=%d\n", vlc);
00614       return -1;
00615     }
00616 
00617     cbp = IS_INTRA(mb_type) ? golomb_to_intra4x4_cbp[vlc] : golomb_to_inter_cbp[vlc];
00618   }
00619   if (IS_INTRA16x16(mb_type) || (s->pict_type != I_TYPE && s->adaptive_quant && cbp)) {
00620     s->qscale += svq3_get_se_golomb (&s->gb);
00621 
00622     if (s->qscale > 31){
00623       av_log(h->s.avctx, AV_LOG_ERROR, "qscale:%d\n", s->qscale);
00624       return -1;
00625     }
00626   }
00627   if (IS_INTRA16x16(mb_type)) {
00628     if (svq3_decode_block (&s->gb, h->mb, 0, 0)){
00629       av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding intra luma dc\n");
00630       return -1;
00631     }
00632   }
00633 
00634   if (cbp) {
00635     const int index = IS_INTRA16x16(mb_type) ? 1 : 0;
00636     const int type = ((s->qscale < 24 && IS_INTRA4x4(mb_type)) ? 2 : 1);
00637 
00638     for (i=0; i < 4; i++) {
00639       if ((cbp & (1 << i))) {
00640         for (j=0; j < 4; j++) {
00641           k = index ? ((j&1) + 2*(i&1) + 2*(j&2) + 4*(i&2)) : (4*i + j);
00642           h->non_zero_count_cache[ scan8[k] ] = 1;
00643 
00644           if (svq3_decode_block (&s->gb, &h->mb[16*k], index, type)){
00645             av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding block\n");
00646             return -1;
00647           }
00648         }
00649       }
00650     }
00651 
00652     if ((cbp & 0x30)) {
00653       for (i=0; i < 2; ++i) {
00654         if (svq3_decode_block (&s->gb, &h->mb[16*(16 + 4*i)], 0, 3)){
00655           av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding chroma dc block\n");
00656           return -1;
00657         }
00658       }
00659 
00660       if ((cbp & 0x20)) {
00661         for (i=0; i < 8; i++) {
00662           h->non_zero_count_cache[ scan8[16+i] ] = 1;
00663 
00664           if (svq3_decode_block (&s->gb, &h->mb[16*(16 + i)], 1, 1)){
00665             av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding chroma ac block\n");
00666             return -1;
00667           }
00668         }
00669       }
00670     }
00671   }
00672 
00673   s->current_picture.mb_type[mb_xy] = mb_type;
00674 
<