libavcodec/svq3.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2003 The Libav Project
00003  *
00004  * This file is part of Libav.
00005  *
00006  * Libav 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  * Libav 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 Libav; 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  *  http://samples.libav.org/V-codecs/SVQ3/Vertical400kbit.sorenson3.mov
00041  */
00042 #include "internal.h"
00043 #include "dsputil.h"
00044 #include "avcodec.h"
00045 #include "mpegvideo.h"
00046 #include "h264.h"
00047 
00048 #include "h264data.h" //FIXME FIXME FIXME
00049 
00050 #include "h264_mvpred.h"
00051 #include "golomb.h"
00052 #include "rectangle.h"
00053 #include "vdpau_internal.h"
00054 
00055 #if CONFIG_ZLIB
00056 #include <zlib.h>
00057 #endif
00058 
00059 #include "svq1.h"
00060 
00066 typedef struct {
00067     H264Context h;
00068     int halfpel_flag;
00069     int thirdpel_flag;
00070     int unknown_flag;
00071     int next_slice_index;
00072     uint32_t watermark_key;
00073 } SVQ3Context;
00074 
00075 #define FULLPEL_MODE  1
00076 #define HALFPEL_MODE  2
00077 #define THIRDPEL_MODE 3
00078 #define PREDICT_MODE  4
00079 
00080 /* dual scan (from some older h264 draft)
00081  o-->o-->o   o
00082          |  /|
00083  o   o   o / o
00084  | / |   |/  |
00085  o   o   o   o
00086    /
00087  o-->o-->o-->o
00088 */
00089 static const uint8_t svq3_scan[16] = {
00090     0+0*4, 1+0*4, 2+0*4, 2+1*4,
00091     2+2*4, 3+0*4, 3+1*4, 3+2*4,
00092     0+1*4, 0+2*4, 1+1*4, 1+2*4,
00093     0+3*4, 1+3*4, 2+3*4, 3+3*4,
00094 };
00095 
00096 static const uint8_t svq3_pred_0[25][2] = {
00097     { 0, 0 },
00098     { 1, 0 }, { 0, 1 },
00099     { 0, 2 }, { 1, 1 }, { 2, 0 },
00100     { 3, 0 }, { 2, 1 }, { 1, 2 }, { 0, 3 },
00101     { 0, 4 }, { 1, 3 }, { 2, 2 }, { 3, 1 }, { 4, 0 },
00102     { 4, 1 }, { 3, 2 }, { 2, 3 }, { 1, 4 },
00103     { 2, 4 }, { 3, 3 }, { 4, 2 },
00104     { 4, 3 }, { 3, 4 },
00105     { 4, 4 }
00106 };
00107 
00108 static const int8_t svq3_pred_1[6][6][5] = {
00109     { { 2,-1,-1,-1,-1 }, { 2, 1,-1,-1,-1 }, { 1, 2,-1,-1,-1 },
00110       { 2, 1,-1,-1,-1 }, { 1, 2,-1,-1,-1 }, { 1, 2,-1,-1,-1 } },
00111     { { 0, 2,-1,-1,-1 }, { 0, 2, 1, 4, 3 }, { 0, 1, 2, 4, 3 },
00112       { 0, 2, 1, 4, 3 }, { 2, 0, 1, 3, 4 }, { 0, 4, 2, 1, 3 } },
00113     { { 2, 0,-1,-1,-1 }, { 2, 1, 0, 4, 3 }, { 1, 2, 4, 0, 3 },
00114       { 2, 1, 0, 4, 3 }, { 2, 1, 4, 3, 0 }, { 1, 2, 4, 0, 3 } },
00115     { { 2, 0,-1,-1,-1 }, { 2, 0, 1, 4, 3 }, { 1, 2, 0, 4, 3 },
00116       { 2, 1, 0, 4, 3 }, { 2, 1, 3, 4, 0 }, { 2, 4, 1, 0, 3 } },
00117     { { 0, 2,-1,-1,-1 }, { 0, 2, 1, 3, 4 }, { 1, 2, 3, 0, 4 },
00118       { 2, 0, 1, 3, 4 }, { 2, 1, 3, 0, 4 }, { 2, 0, 4, 3, 1 } },
00119     { { 0, 2,-1,-1,-1 }, { 0, 2, 4, 1, 3 }, { 1, 4, 2, 0, 3 },
00120       { 4, 2, 0, 1, 3 }, { 2, 0, 1, 4, 3 }, { 4, 2, 1, 0, 3 } },
00121 };
00122 
00123 static const struct { uint8_t run; uint8_t level; } svq3_dct_tables[2][16] = {
00124     { { 0, 0 }, { 0, 1 }, { 1, 1 }, { 2, 1 }, { 0, 2 }, { 3, 1 }, { 4, 1 }, { 5, 1 },
00125       { 0, 3 }, { 1, 2 }, { 2, 2 }, { 6, 1 }, { 7, 1 }, { 8, 1 }, { 9, 1 }, { 0, 4 } },
00126     { { 0, 0 }, { 0, 1 }, { 1, 1 }, { 0, 2 }, { 2, 1 }, { 0, 3 }, { 0, 4 }, { 0, 5 },
00127       { 3, 1 }, { 4, 1 }, { 1, 2 }, { 1, 3 }, { 0, 6 }, { 0, 7 }, { 0, 8 }, { 0, 9 } }
00128 };
00129 
00130 static const uint32_t svq3_dequant_coeff[32] = {
00131      3881,  4351,  4890,  5481,  6154,  6914,  7761,  8718,
00132      9781, 10987, 12339, 13828, 15523, 17435, 19561, 21873,
00133     24552, 27656, 30847, 34870, 38807, 43747, 49103, 54683,
00134     61694, 68745, 77615, 89113,100253,109366,126635,141533
00135 };
00136 
00137 void ff_svq3_luma_dc_dequant_idct_c(DCTELEM *output, DCTELEM *input, int qp){
00138     const int qmul = svq3_dequant_coeff[qp];
00139 #define stride 16
00140     int i;
00141     int temp[16];
00142     static const uint8_t x_offset[4]={0, 1*stride, 4*stride, 5*stride};
00143 
00144     for(i=0; i<4; i++){
00145         const int z0 = 13*(input[4*i+0] +    input[4*i+2]);
00146         const int z1 = 13*(input[4*i+0] -    input[4*i+2]);
00147         const int z2 =  7* input[4*i+1] - 17*input[4*i+3];
00148         const int z3 = 17* input[4*i+1] +  7*input[4*i+3];
00149 
00150         temp[4*i+0] = z0+z3;
00151         temp[4*i+1] = z1+z2;
00152         temp[4*i+2] = z1-z2;
00153         temp[4*i+3] = z0-z3;
00154     }
00155 
00156     for(i=0; i<4; i++){
00157         const int offset= x_offset[i];
00158         const int z0= 13*(temp[4*0+i] +    temp[4*2+i]);
00159         const int z1= 13*(temp[4*0+i] -    temp[4*2+i]);
00160         const int z2=  7* temp[4*1+i] - 17*temp[4*3+i];
00161         const int z3= 17* temp[4*1+i] +  7*temp[4*3+i];
00162 
00163         output[stride* 0+offset] = ((z0 + z3)*qmul + 0x80000) >> 20;
00164         output[stride* 2+offset] = ((z1 + z2)*qmul + 0x80000) >> 20;
00165         output[stride* 8+offset] = ((z1 - z2)*qmul + 0x80000) >> 20;
00166         output[stride*10+offset] = ((z0 - z3)*qmul + 0x80000) >> 20;
00167     }
00168 }
00169 #undef stride
00170 
00171 void ff_svq3_add_idct_c(uint8_t *dst, DCTELEM *block, int stride, int qp,
00172                             int dc)
00173 {
00174     const int qmul = svq3_dequant_coeff[qp];
00175     int i;
00176 
00177     if (dc) {
00178         dc = 13*13*((dc == 1) ? 1538*block[0] : ((qmul*(block[0] >> 3)) / 2));
00179         block[0] = 0;
00180     }
00181 
00182     for (i = 0; i < 4; i++) {
00183         const int z0 = 13*(block[0 + 4*i] +    block[2 + 4*i]);
00184         const int z1 = 13*(block[0 + 4*i] -    block[2 + 4*i]);
00185         const int z2 =  7* block[1 + 4*i] - 17*block[3 + 4*i];
00186         const int z3 = 17* block[1 + 4*i] +  7*block[3 + 4*i];
00187 
00188         block[0 + 4*i] = z0 + z3;
00189         block[1 + 4*i] = z1 + z2;
00190         block[2 + 4*i] = z1 - z2;
00191         block[3 + 4*i] = z0 - z3;
00192     }
00193 
00194     for (i = 0; i < 4; i++) {
00195         const int z0 = 13*(block[i + 4*0] +    block[i + 4*2]);
00196         const int z1 = 13*(block[i + 4*0] -    block[i + 4*2]);
00197         const int z2 =  7* block[i + 4*1] - 17*block[i + 4*3];
00198         const int z3 = 17* block[i + 4*1] +  7*block[i + 4*3];
00199         const int rr = (dc + 0x80000);
00200 
00201         dst[i + stride*0] = av_clip_uint8( dst[i + stride*0] + (((z0 + z3)*qmul + rr) >> 20) );
00202         dst[i + stride*1] = av_clip_uint8( dst[i + stride*1] + (((z1 + z2)*qmul + rr) >> 20) );
00203         dst[i + stride*2] = av_clip_uint8( dst[i + stride*2] + (((z1 - z2)*qmul + rr) >> 20) );
00204         dst[i + stride*3] = av_clip_uint8( dst[i + stride*3] + (((z0 - z3)*qmul + rr) >> 20) );
00205     }
00206 }
00207 
00208 static inline int svq3_decode_block(GetBitContext *gb, DCTELEM *block,
00209                                     int index, const int type)
00210 {
00211     static const uint8_t *const scan_patterns[4] =
00212     { luma_dc_zigzag_scan, zigzag_scan, svq3_scan, chroma_dc_scan };
00213 
00214     int run, level, sign, vlc, limit;
00215     const int intra = (3 * type) >> 2;
00216     const uint8_t *const scan = scan_patterns[type];
00217 
00218     for (limit = (16 >> intra); index < 16; index = limit, limit += 8) {
00219         for (; (vlc = svq3_get_ue_golomb(gb)) != 0; index++) {
00220 
00221           if (vlc == INVALID_VLC)
00222               return -1;
00223 
00224           sign = (vlc & 0x1) - 1;
00225           vlc  = (vlc + 1) >> 1;
00226 
00227           if (type == 3) {
00228               if (vlc < 3) {
00229                   run   = 0;
00230                   level = vlc;
00231               } else if (vlc < 4) {
00232                   run   = 1;
00233                   level = 1;
00234               } else {
00235                   run   = (vlc & 0x3);
00236                   level = ((vlc + 9) >> 2) - run;
00237               }
00238           } else {
00239               if (vlc < 16) {
00240                   run   = svq3_dct_tables[intra][vlc].run;
00241                   level = svq3_dct_tables[intra][vlc].level;
00242               } else if (intra) {
00243                   run   = (vlc & 0x7);
00244                   level = (vlc >> 3) + ((run == 0) ? 8 : ((run < 2) ? 2 : ((run < 5) ? 0 : -1)));
00245               } else {
00246                   run   = (vlc & 0xF);
00247                   level = (vlc >> 4) + ((run == 0) ? 4 : ((run < 3) ? 2 : ((run < 10) ? 1 : 0)));
00248               }
00249           }
00250 
00251           if ((index += run) >= limit)
00252               return -1;
00253 
00254           block[scan[index]] = (level ^ sign) - sign;
00255         }
00256 
00257         if (type != 2) {
00258             break;
00259         }
00260     }
00261 
00262     return 0;
00263 }
00264 
00265 static inline void svq3_mc_dir_part(MpegEncContext *s,
00266                                     int x, int y, int width, int height,
00267                                     int mx, int my, int dxy,
00268                                     int thirdpel, int dir, int avg)
00269 {
00270     const Picture *pic = (dir == 0) ? &s->last_picture : &s->next_picture;
00271     uint8_t *src, *dest;
00272     int i, emu = 0;
00273     int blocksize = 2 - (width>>3); //16->0, 8->1, 4->2
00274 
00275     mx += x;
00276     my += y;
00277 
00278     if (mx < 0 || mx >= (s->h_edge_pos - width  - 1) ||
00279         my < 0 || my >= (s->v_edge_pos - height - 1)) {
00280 
00281         if ((s->flags & CODEC_FLAG_EMU_EDGE)) {
00282             emu = 1;
00283         }
00284 
00285         mx = av_clip (mx, -16, (s->h_edge_pos - width  + 15));
00286         my = av_clip (my, -16, (s->v_edge_pos - height + 15));
00287     }
00288 
00289     /* form component predictions */
00290     dest = s->current_picture.f.data[0] + x + y*s->linesize;
00291     src  = pic->f.data[0] + mx + my*s->linesize;
00292 
00293     if (emu) {
00294         s->dsp.emulated_edge_mc(s->edge_emu_buffer, src, s->linesize, (width + 1), (height + 1),
00295                             mx, my, s->h_edge_pos, s->v_edge_pos);
00296         src = s->edge_emu_buffer;
00297     }
00298     if (thirdpel)
00299         (avg ? s->dsp.avg_tpel_pixels_tab : s->dsp.put_tpel_pixels_tab)[dxy](dest, src, s->linesize, width, height);
00300     else
00301         (avg ? s->dsp.avg_pixels_tab : s->dsp.put_pixels_tab)[blocksize][dxy](dest, src, s->linesize, height);
00302 
00303     if (!(s->flags & CODEC_FLAG_GRAY)) {
00304         mx     = (mx + (mx < (int) x)) >> 1;
00305         my     = (my + (my < (int) y)) >> 1;
00306         width  = (width  >> 1);
00307         height = (height >> 1);
00308         blocksize++;
00309 
00310         for (i = 1; i < 3; i++) {
00311             dest = s->current_picture.f.data[i] + (x >> 1) + (y >> 1) * s->uvlinesize;
00312             src  = pic->f.data[i] + mx + my * s->uvlinesize;
00313 
00314             if (emu) {
00315                 s->dsp.emulated_edge_mc(s->edge_emu_buffer, src, s->uvlinesize, (width + 1), (height + 1),
00316                                     mx, my, (s->h_edge_pos >> 1), (s->v_edge_pos >> 1));
00317                 src = s->edge_emu_buffer;
00318             }
00319             if (thirdpel)
00320                 (avg ? s->dsp.avg_tpel_pixels_tab : s->dsp.put_tpel_pixels_tab)[dxy](dest, src, s->uvlinesize, width, height);
00321             else
00322                 (avg ? s->dsp.avg_pixels_tab : s->dsp.put_pixels_tab)[blocksize][dxy](dest, src, s->uvlinesize, height);
00323         }
00324     }
00325 }
00326 
00327 static inline int svq3_mc_dir(H264Context *h, int size, int mode, int dir,
00328                               int avg)
00329 {
00330     int i, j, k, mx, my, dx, dy, x, y;
00331     MpegEncContext *const s = (MpegEncContext *) h;
00332     const int part_width  = ((size & 5) == 4) ? 4 : 16 >> (size & 1);
00333     const int part_height = 16 >> ((unsigned) (size + 1) / 3);
00334     const int extra_width = (mode == PREDICT_MODE) ? -16*6 : 0;
00335     const int h_edge_pos  = 6*(s->h_edge_pos - part_width ) - extra_width;
00336     const int v_edge_pos  = 6*(s->v_edge_pos - part_height) - extra_width;
00337 
00338     for (i = 0; i < 16; i += part_height) {
00339         for (j = 0; j < 16; j += part_width) {
00340             const int b_xy = (4*s->mb_x + (j >> 2)) + (4*s->mb_y + (i >> 2))*h->b_stride;
00341             int dxy;
00342             x = 16*s->mb_x + j;
00343             y = 16*s->mb_y + i;
00344             k = ((j >> 2) & 1) + ((i >> 1) & 2) + ((j >> 1) & 4) + (i & 8);
00345 
00346             if (mode != PREDICT_MODE) {
00347                 pred_motion(h, k, (part_width >> 2), dir, 1, &mx, &my);
00348             } else {
00349                 mx = s->next_picture.f.motion_val[0][b_xy][0] << 1;
00350                 my = s->next_picture.f.motion_val[0][b_xy][1] << 1;
00351 
00352                 if (dir == 0) {
00353                     mx = ((mx * h->frame_num_offset) / h->prev_frame_num_offset + 1) >> 1;
00354                     my = ((my * h->frame_num_offset) / h->prev_frame_num_offset + 1) >> 1;
00355                 } else {
00356                     mx = ((mx * (h->frame_num_offset - h->prev_frame_num_offset)) / h->prev_frame_num_offset + 1) >> 1;
00357                     my = ((my * (h->frame_num_offset - h->prev_frame_num_offset)) / h->prev_frame_num_offset + 1) >> 1;
00358                 }
00359             }
00360 
00361             /* clip motion vector prediction to frame border */
00362             mx = av_clip(mx, extra_width - 6*x, h_edge_pos - 6*x);
00363             my = av_clip(my, extra_width - 6*y, v_edge_pos - 6*y);
00364 
00365             /* get (optional) motion vector differential */
00366             if (mode == PREDICT_MODE) {
00367                 dx = dy = 0;
00368             } else {
00369                 dy = svq3_get_se_golomb(&s->gb);
00370                 dx = svq3_get_se_golomb(&s->gb);
00371 
00372                 if (dx == INVALID_VLC || dy == INVALID_VLC) {
00373                     av_log(h->s.avctx, AV_LOG_ERROR, "invalid MV vlc\n");
00374                     return -1;
00375                 }
00376             }
00377 
00378             /* compute motion vector */
00379             if (mode == THIRDPEL_MODE) {
00380                 int fx, fy;
00381                 mx  = ((mx + 1)>>1) + dx;
00382                 my  = ((my + 1)>>1) + dy;
00383                 fx  = ((unsigned)(mx + 0x3000))/3 - 0x1000;
00384                 fy  = ((unsigned)(my + 0x3000))/3 - 0x1000;
00385                 dxy = (mx - 3*fx) + 4*(my - 3*fy);
00386 
00387                 svq3_mc_dir_part(s, x, y, part_width, part_height, fx, fy, dxy, 1, dir, avg);
00388                 mx += mx;
00389                 my += my;
00390             } else if (mode == HALFPEL_MODE || mode == PREDICT_MODE) {
00391                 mx  = ((unsigned)(mx + 1 + 0x3000))/3 + dx - 0x1000;
00392                 my  = ((unsigned)(my + 1 + 0x3000))/3 + dy - 0x1000;
00393                 dxy = (mx&1) + 2*(my&1);
00394 
00395                 svq3_mc_dir_part(s, x, y, part_width, part_height, mx>>1, my>>1, dxy, 0, dir, avg);
00396                 mx *= 3;
00397                 my *= 3;
00398             } else {
00399                 mx = ((unsigned)(mx + 3 + 0x6000))/6 + dx - 0x1000;
00400                 my = ((unsigned)(my + 3 + 0x6000))/6 + dy - 0x1000;
00401 
00402                 svq3_mc_dir_part(s, x, y, part_width, part_height, mx, my, 0, 0, dir, avg);
00403                 mx *= 6;
00404                 my *= 6;
00405             }
00406 
00407             /* update mv_cache */
00408             if (mode != PREDICT_MODE) {
00409                 int32_t mv = pack16to32(mx,my);
00410 
00411                 if (part_height == 8 && i < 8) {
00412                     AV_WN32A(h->mv_cache[dir][scan8[k] + 1*8], mv);
00413 
00414                     if (part_width == 8 && j < 8) {
00415                         AV_WN32A(h->mv_cache[dir][scan8[k] + 1 + 1*8], mv);
00416                     }
00417                 }
00418                 if (part_width == 8 && j < 8) {
00419                     AV_WN32A(h->mv_cache[dir][scan8[k] + 1], mv);
00420                 }
00421                 if (part_width == 4 || part_height == 4) {
00422                     AV_WN32A(h->mv_cache[dir][scan8[k]], mv);
00423                 }
00424             }
00425 
00426             /* write back motion vectors */
00427             fill_rectangle(s->current_picture.f.motion_val[dir][b_xy],
00428                            part_width >> 2, part_height >> 2, h->b_stride,
00429                            pack16to32(mx, my), 4);
00430         }
00431     }
00432 
00433     return 0;
00434 }
00435 
00436 static int svq3_decode_mb(SVQ3Context *svq3, unsigned int mb_type)
00437 {
00438     H264Context *h = &svq3->h;
00439     int i, j, k, m, dir, mode;
00440     int cbp = 0;
00441     uint32_t vlc;
00442     int8_t *top, *left;
00443     MpegEncContext *const s = (MpegEncContext *) h;
00444     const int mb_xy = h->mb_xy;
00445     const int b_xy  = 4*s->mb_x + 4*s->mb_y*h->b_stride;
00446 
00447     h->top_samples_available      = (s->mb_y == 0) ? 0x33FF : 0xFFFF;
00448     h->left_samples_available     = (s->mb_x == 0) ? 0x5F5F : 0xFFFF;
00449     h->topright_samples_available = 0xFFFF;
00450 
00451     if (mb_type == 0) {           /* SKIP */
00452         if (s->pict_type == AV_PICTURE_TYPE_P || s->next_picture.f.mb_type[mb_xy] == -1) {
00453             svq3_mc_dir_part(s, 16*s->mb_x, 16*s->mb_y, 16, 16, 0, 0, 0, 0, 0, 0);
00454 
00455             if (s->pict_type == AV_PICTURE_TYPE_B) {
00456                 svq3_mc_dir_part(s, 16*s->mb_x, 16*s->mb_y, 16, 16, 0, 0, 0, 0, 1, 1);
00457             }
00458 
00459             mb_type = MB_TYPE_SKIP;
00460         } else {
00461             mb_type = FFMIN(s->next_picture.f.mb_type[mb_xy], 6);
00462             if (svq3_mc_dir(h, mb_type, PREDICT_MODE, 0, 0) < 0)
00463                 return -1;
00464             if (svq3_mc_dir(h, mb_type, PREDICT_MODE, 1, 1) < 0)
00465                 return -1;
00466 
00467             mb_type = MB_TYPE_16x16;
00468         }
00469     } else if (mb_type < 8) {     /* INTER */
00470         if (svq3->thirdpel_flag && svq3->halfpel_flag == !get_bits1 (&s->gb)) {
00471             mode = THIRDPEL_MODE;
00472         } else if (svq3->halfpel_flag && svq3->thirdpel_flag == !get_bits1 (&s->gb)) {
00473             mode = HALFPEL_MODE;
00474         } else {
00475             mode = FULLPEL_MODE;
00476         }
00477 
00478         /* fill caches */
00479         /* note ref_cache should contain here:
00480             ????????
00481             ???11111
00482             N??11111
00483             N??11111
00484             N??11111
00485         */
00486 
00487         for (m = 0; m < 2; m++) {
00488             if (s->mb_x > 0 && h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - 1]+6] != -1) {
00489                 for (i = 0; i < 4; i++) {
00490                     AV_COPY32(h->mv_cache[m][scan8[0] - 1 + i*8], s->current_picture.f.motion_val[m][b_xy - 1 + i*h->b_stride]);
00491                 }
00492             } else {
00493                 for (i = 0; i < 4; i++) {
00494                     AV_ZERO32(h->mv_cache[m][scan8[0] - 1 + i*8]);
00495                 }
00496             }
00497             if (s->mb_y > 0) {
00498                 memcpy(h->mv_cache[m][scan8[0] - 1*8], s->current_picture.f.motion_val[m][b_xy - h->b_stride], 4*2*sizeof(int16_t));
00499                 memset(&h->ref_cache[m][scan8[0] - 1*8], (h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - s->mb_stride]] == -1) ? PART_NOT_AVAILABLE : 1, 4);
00500 
00501                 if (s->mb_x < (s->mb_width - 1)) {
00502                     AV_COPY32(h->mv_cache[m][scan8[0] + 4 - 1*8], s->current_picture.f.motion_val[m][b_xy - h->b_stride + 4]);
00503                     h->ref_cache[m][scan8[0] + 4 - 1*8] =
00504                         (h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - s->mb_stride + 1]+6] == -1 ||
00505                          h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - s->mb_stride    ]  ] == -1) ? PART_NOT_AVAILABLE : 1;
00506                 }else
00507                     h->ref_cache[m][scan8[0] + 4 - 1*8] = PART_NOT_AVAILABLE;
00508                 if (s->mb_x > 0) {
00509                     AV_COPY32(h->mv_cache[m][scan8[0] - 1 - 1*8], s->current_picture.f.motion_val[m][b_xy - h->b_stride - 1]);
00510                     h->ref_cache[m][scan8[0] - 1 - 1*8] = (h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - s->mb_stride - 1]+3] == -1) ? PART_NOT_AVAILABLE : 1;
00511                 }else
00512                     h->ref_cache[m][scan8[0] - 1 - 1*8] = PART_NOT_AVAILABLE;
00513             }else
00514                 memset(&h->ref_cache[m][scan8[0] - 1*8 - 1], PART_NOT_AVAILABLE, 8);
00515 
00516             if (s->pict_type != AV_PICTURE_TYPE_B)
00517                 break;
00518         }
00519 
00520         /* decode motion vector(s) and form prediction(s) */
00521         if (s->pict_type == AV_PICTURE_TYPE_P) {
00522             if (svq3_mc_dir(h, (mb_type - 1), mode, 0, 0) < 0)
00523                 return -1;
00524         } else {        /* AV_PICTURE_TYPE_B */
00525             if (mb_type != 2) {
00526                 if (svq3_mc_dir(h, 0, mode, 0, 0) < 0)
00527                     return -1;
00528             } else {
00529                 for (i = 0; i < 4; i++) {
00530                     memset(s->current_picture.f.motion_val[0][b_xy + i*h->b_stride], 0, 4*2*sizeof(int16_t));
00531                 }
00532             }
00533             if (mb_type != 1) {
00534                 if (svq3_mc_dir(h, 0, mode, 1, (mb_type == 3)) < 0)
00535                     return -1;
00536             } else {
00537                 for (i = 0; i < 4; i++) {
00538                     memset(s->current_picture.f.motion_val[1][b_xy + i*h->b_stride], 0, 4*2*sizeof(int16_t));
00539                 }
00540             }
00541         }
00542 
00543         mb_type = MB_TYPE_16x16;
00544     } else if (mb_type == 8 || mb_type == 33) {   /* INTRA4x4 */
00545         memset(h->intra4x4_pred_mode_cache, -1, 8*5*sizeof(int8_t));
00546 
00547         if (mb_type == 8) {
00548             if (s->mb_x > 0) {
00549                 for (i = 0; i < 4; i++) {
00550                     h->intra4x4_pred_mode_cache[scan8[0] - 1 + i*8] = h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - 1]+6-i];
00551                 }
00552                 if (h->intra4x4_pred_mode_cache[scan8[0] - 1] == -1) {
00553                     h->left_samples_available = 0x5F5F;
00554                 }
00555             }
00556             if (s->mb_y > 0) {
00557                 h->intra4x4_pred_mode_cache[4+8*0] = h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - s->mb_stride]+0];
00558                 h->intra4x4_pred_mode_cache[5+8*0] = h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - s->mb_stride]+1];
00559                 h->intra4x4_pred_mode_cache[6+8*0] = h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - s->mb_stride]+2];
00560                 h->intra4x4_pred_mode_cache[7+8*0] = h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - s->mb_stride]+3];
00561 
00562                 if (h->intra4x4_pred_mode_cache[4+8*0] == -1) {
00563                     h->top_samples_available = 0x33FF;
00564                 }
00565             }
00566 
00567             /* decode prediction codes for luma blocks */
00568             for (i = 0; i < 16; i+=2) {
00569                 vlc = svq3_get_ue_golomb(&s->gb);
00570 
00571                 if (vlc >= 25){
00572                     av_log(h->s.avctx, AV_LOG_ERROR, "luma prediction:%d\n", vlc);
00573                     return -1;
00574                 }
00575 
00576                 left    = &h->intra4x4_pred_mode_cache[scan8[i] - 1];
00577                 top     = &h->intra4x4_pred_mode_cache[scan8[i] - 8];
00578 
00579                 left[1] = svq3_pred_1[top[0] + 1][left[0] + 1][svq3_pred_0[vlc][0]];
00580                 left[2] = svq3_pred_1[top[1] + 1][left[1] + 1][svq3_pred_0[vlc][1]];
00581 
00582                 if (left[1] == -1 || left[2] == -1){
00583                     av_log(h->s.avctx, AV_LOG_ERROR, "weird prediction\n");
00584                     return -1;
00585                 }
00586             }
00587         } else {    /* mb_type == 33, DC_128_PRED block type */
00588             for (i = 0; i < 4; i++) {
00589                 memset(&h->intra4x4_pred_mode_cache[scan8[0] + 8*i], DC_PRED, 4);
00590             }
00591         }
00592 
00593         write_back_intra_pred_mode(h);
00594 
00595         if (mb_type == 8) {
00596             ff_h264_check_intra4x4_pred_mode(h);
00597 
00598             h->top_samples_available  = (s->mb_y == 0) ? 0x33FF : 0xFFFF;
00599             h->left_samples_available = (s->mb_x == 0) ? 0x5F5F : 0xFFFF;
00600         } else {
00601             for (i = 0; i < 4; i++) {
00602                 memset(&h->intra4x4_pred_mode_cache[scan8[0] + 8*i], DC_128_PRED, 4);
00603             }
00604 
00605             h->top_samples_available  = 0x33FF;
00606             h->left_samples_available = 0x5F5F;
00607         }
00608 
00609         mb_type = MB_TYPE_INTRA4x4;
00610     } else {                      /* INTRA16x16 */
00611         dir = i_mb_type_info[mb_type - 8].pred_mode;
00612         dir = (dir >> 1) ^ 3*(dir & 1) ^ 1;
00613 
00614         if ((h->intra16x16_pred_mode = ff_h264_check_intra_pred_mode(h, dir, 0)) < 0) {
00615             av_log(h->s.avctx, AV_LOG_ERROR, "ff_h264_check_intra_pred_mode < 0\n");
00616             return h->intra16x16_pred_mode;
00617         }
00618 
00619         cbp = i_mb_type_info[mb_type - 8].cbp;
00620         mb_type = MB_TYPE_INTRA16x16;
00621     }
00622 
00623     if (!IS_INTER(mb_type) && s->pict_type != AV_PICTURE_TYPE_I) {
00624         for (i = 0; i < 4; i++) {
00625             memset(s->current_picture.f.motion_val[0][b_xy + i*h->b_stride], 0, 4*2*sizeof(int16_t));
00626         }
00627         if (s->pict_type == AV_PICTURE_TYPE_B) {
00628             for (i = 0; i < 4; i++) {
00629                 memset(s->current_picture.f.motion_val[1][b_xy + i*h->b_stride], 0, 4*2*sizeof(int16_t));
00630             }
00631         }
00632     }
00633     if (!IS_INTRA4x4(mb_type)) {
00634         memset(h->intra4x4_pred_mode+h->mb2br_xy[mb_xy], DC_PRED, 8);
00635     }
00636     if (!IS_SKIP(mb_type) || s->pict_type == AV_PICTURE_TYPE_B) {
00637         memset(h->non_zero_count_cache + 8, 0, 14*8*sizeof(uint8_t));
00638         s->dsp.clear_blocks(h->mb+  0);
00639         s->dsp.clear_blocks(h->mb+384);
00640     }
00641 
00642     if (!IS_INTRA16x16(mb_type) && (!IS_SKIP(mb_type) || s->pict_type == AV_PICTURE_TYPE_B)) {
00643         if ((vlc = svq3_get_ue_golomb(&s->gb)) >= 48){
00644             av_log(h->s.avctx, AV_LOG_ERROR, "cbp_vlc=%d\n", vlc);
00645             return -1;
00646         }
00647 
00648         cbp = IS_INTRA(mb_type) ? golomb_to_intra4x4_cbp[vlc] : golomb_to_inter_cbp[vlc];
00649     }
00650     if (IS_INTRA16x16(mb_type) || (s->pict_type != AV_PICTURE_TYPE_I && s->adaptive_quant && cbp)) {
00651         s->qscale += svq3_get_se_golomb(&s->gb);
00652 
00653         if (s->qscale > 31u){
00654             av_log(h->s.avctx, AV_LOG_ERROR, "qscale:%d\n", s->qscale);
00655             return -1;
00656         }
00657     }
00658     if (IS_INTRA16x16(mb_type)) {
00659         AV_ZERO128(h->mb_luma_dc[0]+0);
00660         AV_ZERO128(h->mb_luma_dc[0]+8);
00661         if (svq3_decode_block(&s->gb, h->mb_luma_dc, 0, 1)){
00662             av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding intra luma dc\n");
00663             return -1;
00664         }
00665     }
00666 
00667     if (cbp) {
00668         const int index = IS_INTRA16x16(mb_type) ? 1 : 0;
00669         const int type = ((s->qscale < 24 && IS_INTRA4x4(mb_type)) ? 2 : 1);
00670 
00671         for (i = 0; i < 4; i++) {
00672             if ((cbp & (1 << i))) {
00673                 for (j = 0; j < 4; j++) {
00674                     k = index ? ((j&1) + 2*(i&1) + 2*(j&2) + 4*(i&2)) : (4*i + j);
00675                     h->non_zero_count_cache[ scan8[k] ] = 1;
00676 
00677                     if (svq3_decode_block(&s->gb, &h->mb[16*k], index, type)){
00678                         av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding block\n");
00679                         return -1;
00680                     }
00681                 }
00682             }
00683         }
00684 
00685         if ((cbp & 0x30)) {
00686             for (i = 1; i < 3; ++i) {
00687               if (svq3_decode_block(&s->gb, &h->mb[16*16*i], 0, 3)){
00688                 av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding chroma dc block\n");
00689                 return -1;
00690               }
00691             }
00692 
00693             if ((cbp & 0x20)) {
00694                 for (i = 1; i < 3; i++) {
00695                     for (j = 0; j < 4; j++) {
00696                         k = 16*i + j;
00697                         h->non_zero_count_cache[ scan8[k] ] = 1;
00698 
00699                         if (svq3_decode_block(&s->gb, &h->mb[16*k], 1, 1)){
00700                             av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding chroma ac block\n");
00701                             return -1;
00702                         }
00703                     }
00704                 }
00705             }
00706         }
00707     }
00708 
00709     h->cbp= cbp;
00710     s->current_picture.f.mb_type[mb_xy] = mb_type;
00711 
00712     if (IS_INTRA(mb_type)) {
00713         h->chroma_pred_mode = ff_h264_check_intra_pred_mode(h, DC_PRED8x8, 1);
00714     }
00715 
00716     return 0;
00717 }
00718 
00719 static int svq3_decode_slice_header(AVCodecContext *avctx)
00720 {
00721     SVQ3Context *svq3 = avctx->priv_data;
00722     H264Context *h = &svq3->h;
00723     MpegEncContext *s = &h->s;
00724     const int mb_xy = h->mb_xy;
00725     int i, header;
00726 
00727     header = get_bits(&s->gb, 8);
00728 
00729     if (((header & 0x9F) != 1 && (header & 0x9F) != 2) || (header & 0x60) == 0) {
00730         /* TODO: what? */
00731         av_log(avctx, AV_LOG_ERROR, "unsupported slice header (%02X)\n", header);
00732         return -1;
00733     } else {
00734         int length = (header >> 5) & 3;
00735 
00736         svq3->next_slice_index = get_bits_count(&s->gb) + 8*show_bits(&s->gb, 8*length) + 8*length;
00737 
00738         if (svq3->next_slice_index > s->gb.size_in_bits) {
00739             av_log(avctx, AV_LOG_ERROR, "slice after bitstream end\n");
00740             return -1;
00741     }
00742 
00743         s->gb.size_in_bits = svq3->next_slice_index - 8*(length - 1);
00744         skip_bits(&s->gb, 8);
00745 
00746         if (svq3->watermark_key) {
00747             uint32_t header = AV_RL32(&s->gb.buffer[(get_bits_count(&s->gb)>>3)+1]);
00748             AV_WL32(&s->gb.buffer[(get_bits_count(&s->gb)>>3)+1], header ^ svq3->watermark_key);
00749         }
00750         if (length > 0) {
00751             memcpy((uint8_t *) &s->gb.buffer[get_bits_count(&s->gb) >> 3],
00752                    &s->gb.buffer[s->gb.size_in_bits >> 3], (length - 1));
00753         }
00754         skip_bits_long(&s->gb, 0);
00755     }
00756 
00757     if ((i = svq3_get_ue_golomb(&s->gb)) == INVALID_VLC || i >= 3){
00758         av_log(h->s.avctx, AV_LOG_ERROR, "illegal slice type %d \n", i);
00759         return -1;
00760     }
00761 
00762     h->slice_type = golomb_to_pict_type[i];
00763 
00764     if ((header & 0x9F) == 2) {
00765         i = (s->mb_num < 64) ? 6 : (1 + av_log2 (s->mb_num - 1));
00766         s->mb_skip_run = get_bits(&s->gb, i) - (s->mb_x + (s->mb_y * s->mb_width));
00767     } else {
00768         skip_bits1(&s->gb);
00769         s->mb_skip_run = 0;
00770     }
00771 
00772     h->slice_num = get_bits(&s->gb, 8);
00773     s->qscale = get_bits(&s->gb, 5);
00774     s->adaptive_quant = get_bits1(&s->gb);
00775 
00776     /* unknown fields */
00777     skip_bits1(&s->gb);
00778 
00779     if (svq3->unknown_flag) {
00780         skip_bits1(&s->gb);
00781     }
00782 
00783     skip_bits1(&s->gb);
00784     skip_bits(&s->gb, 2);
00785 
00786     while (get_bits1(&s->gb)) {
00787         skip_bits(&s->gb, 8);
00788     }
00789 
00790     /* reset intra predictors and invalidate motion vector references */
00791     if (s->mb_x > 0) {
00792         memset(h->intra4x4_pred_mode+h->mb2br_xy[mb_xy - 1      ]+3, -1, 4*sizeof(int8_t));
00793         memset(h->intra4x4_pred_mode+h->mb2br_xy[mb_xy - s->mb_x]  , -1, 8*sizeof(int8_t)*s->mb_x);
00794     }
00795     if (s->mb_y > 0) {
00796         memset(h->intra4x4_pred_mode+h->mb2br_xy[mb_xy - s->mb_stride], -1, 8*sizeof(int8_t)*(s->mb_width - s->mb_x));
00797 
00798         if (s->mb_x > 0) {
00799             h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - s->mb_stride - 1]+3] = -1;
00800         }
00801     }
00802 
00803     return 0;
00804 }
00805 
00806 static av_cold int svq3_decode_init(AVCodecContext *avctx)
00807 {
00808     SVQ3Context *svq3 = avctx->priv_data;
00809     H264Context *h = &svq3->h;
00810     MpegEncContext *s = &h->s;
00811     int m;
00812     unsigned char *extradata;
00813     unsigned char *extradata_end;
00814     unsigned int size;
00815     int marker_found = 0;
00816 
00817     if (ff_h264_decode_init(avctx) < 0)
00818         return -1;
00819 
00820     s->flags  = avctx->flags;
00821     s->flags2 = avctx->flags2;
00822     s->unrestricted_mv = 1;
00823     h->is_complex=1;
00824     avctx->pix_fmt = avctx->codec->pix_fmts[0];
00825 
00826     if (!s->context_initialized) {
00827         h->chroma_qp[0] = h->chroma_qp[1] = 4;
00828 
00829         svq3->halfpel_flag  = 1;
00830         svq3->thirdpel_flag = 1;
00831         svq3->unknown_flag  = 0;
00832 
00833         /* prowl for the "SEQH" marker in the extradata */
00834         extradata = (unsigned char *)avctx->extradata;
00835         extradata_end = avctx->extradata + avctx->extradata_size;
00836         if (extradata) {
00837             for (m = 0; m + 8 < avctx->extradata_size; m++) {
00838                 if (!memcmp(extradata, "SEQH", 4)) {
00839                     marker_found = 1;
00840                     break;
00841                 }
00842                 extradata++;
00843             }
00844         }
00845 
00846         /* if a match was found, parse the extra data */
00847         if (marker_found) {
00848 
00849             GetBitContext gb;
00850             int frame_size_code;
00851 
00852             size = AV_RB32(&extradata[4]);
00853             if (size > extradata_end - extradata - 8)
00854                 return AVERROR_INVALIDDATA;
00855             init_get_bits(&gb, extradata + 8, size*8);
00856 
00857             /* 'frame size code' and optional 'width, height' */
00858             frame_size_code = get_bits(&gb, 3);
00859             switch (frame_size_code) {
00860                 case 0: avctx->width = 160; avctx->height = 120; break;
00861                 case 1: avctx->width = 128; avctx->height =  96; break;
00862                 case 2: avctx->width = 176; avctx->height = 144; break;
00863                 case 3: avctx->width = 352; avctx->height = 288; break;
00864                 case 4: avctx->width = 704; avctx->height = 576; break;
00865                 case 5: avctx->width = 240; avctx->height = 180; break;
00866                 case 6: avctx->width = 320; avctx->height = 240; break;
00867                 case 7:
00868                     avctx->width  = get_bits(&gb, 12);
00869                     avctx->height = get_bits(&gb, 12);
00870                     break;
00871             }
00872 
00873             svq3->halfpel_flag  = get_bits1(&gb);
00874             svq3->thirdpel_flag = get_bits1(&gb);
00875 
00876             /* unknown fields */
00877             skip_bits1(&gb);
00878             skip_bits1(&gb);
00879             skip_bits1(&gb);
00880             skip_bits1(&gb);
00881 
00882             s->low_delay = get_bits1(&gb);
00883 
00884             /* unknown field */
00885             skip_bits1(&gb);
00886 
00887             while (get_bits1(&gb)) {
00888                 skip_bits(&gb, 8);
00889             }
00890 
00891             svq3->unknown_flag = get_bits1(&gb);
00892             avctx->has_b_frames = !s->low_delay;
00893             if (svq3->unknown_flag) {
00894 #if CONFIG_ZLIB
00895                 unsigned watermark_width  = svq3_get_ue_golomb(&gb);
00896                 unsigned watermark_height = svq3_get_ue_golomb(&gb);
00897                 int u1 = svq3_get_ue_golomb(&gb);
00898                 int u2 = get_bits(&gb, 8);
00899                 int u3 = get_bits(&gb, 2);
00900                 int u4 = svq3_get_ue_golomb(&gb);
00901                 unsigned long buf_len = watermark_width*watermark_height*4;
00902                 int offset = (get_bits_count(&gb)+7)>>3;
00903                 uint8_t *buf;
00904 
00905                 if (watermark_height > 0 &&
00906                     (uint64_t)watermark_width * 4 > UINT_MAX / watermark_height)
00907                     return -1;
00908 
00909                 buf = av_malloc(buf_len);
00910                 av_log(avctx, AV_LOG_DEBUG, "watermark size: %dx%d\n", watermark_width, watermark_height);
00911                 av_log(avctx, AV_LOG_DEBUG, "u1: %x u2: %x u3: %x compressed data size: %d offset: %d\n", u1, u2, u3, u4, offset);
00912                 if (uncompress(buf, &buf_len, extradata + 8 + offset, size - offset) != Z_OK) {
00913                     av_log(avctx, AV_LOG_ERROR, "could not uncompress watermark logo\n");
00914                     av_free(buf);
00915                     return -1;
00916                 }
00917                 svq3->watermark_key = ff_svq1_packet_checksum(buf, buf_len, 0);
00918                 svq3->watermark_key = svq3->watermark_key << 16 | svq3->watermark_key;
00919                 av_log(avctx, AV_LOG_DEBUG, "watermark key %#x\n", svq3->watermark_key);
00920                 av_free(buf);
00921 #else
00922                 av_log(avctx, AV_LOG_ERROR, "this svq3 file contains watermark which need zlib support compiled in\n");
00923                 return -1;
00924 #endif
00925             }
00926         }
00927 
00928         s->width  = avctx->width;
00929         s->height = avctx->height;
00930 
00931         if (MPV_common_init(s) < 0)
00932             return -1;
00933 
00934         h->b_stride = 4*s->mb_width;
00935 
00936         if (ff_h264_alloc_tables(h) < 0) {
00937             av_log(avctx, AV_LOG_ERROR, "svq3 memory allocation failed\n");
00938             return AVERROR(ENOMEM);
00939         }
00940     }
00941 
00942     return 0;
00943 }
00944 
00945 static int svq3_decode_frame(AVCodecContext *avctx,
00946                              void *data, int *data_size,
00947                              AVPacket *avpkt)
00948 {
00949     const uint8_t *buf = avpkt->data;
00950     SVQ3Context *svq3 = avctx->priv_data;
00951     H264Context *h = &svq3->h;
00952     MpegEncContext *s = &h->s;
00953     int buf_size = avpkt->size;
00954     int m, mb_type;
00955 
00956     /* special case for last picture */
00957     if (buf_size == 0) {
00958         if (s->next_picture_ptr && !s->low_delay) {
00959             *(AVFrame *) data = *(AVFrame *) &s->next_picture;
00960             s->next_picture_ptr = NULL;
00961             *data_size = sizeof(AVFrame);
00962         }
00963         return 0;
00964     }
00965 
00966     init_get_bits (&s->gb, buf, 8*buf_size);
00967 
00968     s->mb_x = s->mb_y = h->mb_xy = 0;
00969 
00970     if (svq3_decode_slice_header(avctx))
00971         return -1;
00972 
00973     s->pict_type = h->slice_type;
00974     s->picture_number = h->slice_num;
00975 
00976     if (avctx->debug&FF_DEBUG_PICT_INFO){
00977         av_log(h->s.avctx, AV_LOG_DEBUG, "%c hpel:%d, tpel:%d aqp:%d qp:%d, slice_num:%02X\n",
00978                av_get_picture_type_char(s->pict_type), svq3->halfpel_flag, svq3->thirdpel_flag,
00979                s->adaptive_quant, s->qscale, h->slice_num);
00980     }
00981 
00982     /* for skipping the frame */
00983     s->current_picture.f.pict_type = s->pict_type;
00984     s->current_picture.f.key_frame = (s->pict_type == AV_PICTURE_TYPE_I);
00985 
00986     /* Skip B-frames if we do not have reference frames. */
00987     if (s->last_picture_ptr == NULL && s->pict_type == AV_PICTURE_TYPE_B)
00988         return 0;
00989     if (  (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B)
00990         ||(avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I)
00991         || avctx->skip_frame >= AVDISCARD_ALL)
00992         return 0;
00993 
00994     if (s->next_p_frame_damaged) {
00995         if (s->pict_type == AV_PICTURE_TYPE_B)
00996             return 0;
00997         else
00998             s->next_p_frame_damaged = 0;
00999     }
01000 
01001     if (ff_h264_frame_start(h) < 0)
01002         return -1;
01003 
01004     if (s->pict_type == AV_PICTURE_TYPE_B) {
01005         h->frame_num_offset = (h->slice_num - h->prev_frame_num);
01006 
01007         if (h->frame_num_offset < 0) {
01008             h->frame_num_offset += 256;
01009         }
01010         if (h->frame_num_offset == 0 || h->frame_num_offset >= h->prev_frame_num_offset) {
01011             av_log(h->s.avctx, AV_LOG_ERROR, "error in B-frame picture id\n");
01012             return -1;
01013         }
01014     } else {
01015         h->prev_frame_num = h->frame_num;
01016         h->frame_num = h->slice_num;
01017         h->prev_frame_num_offset = (h->frame_num - h->prev_frame_num);
01018 
01019         if (h->prev_frame_num_offset < 0) {
01020             h->prev_frame_num_offset += 256;
01021         }
01022     }
01023 
01024     for (m = 0; m < 2; m++){
01025         int i;
01026         for (i = 0; i < 4; i++){
01027             int j;
01028             for (j = -1; j < 4; j++)
01029                 h->ref_cache[m][scan8[0] + 8*i + j]= 1;
01030             if (i < 3)
01031                 h->ref_cache[m][scan8[0] + 8*i + j]= PART_NOT_AVAILABLE;
01032         }
01033     }
01034 
01035     for (s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
01036         for (s->mb_x = 0; s->mb_x < s->mb_width; s->mb_x++) {
01037             h->mb_xy = s->mb_x + s->mb_y*s->mb_stride;
01038 
01039             if ( (get_bits_count(&s->gb) + 7) >= s->gb.size_in_bits &&
01040                 ((get_bits_count(&s->gb) & 7) == 0 || show_bits(&s->gb, (-get_bits_count(&s->gb) & 7)) == 0)) {
01041 
01042                 skip_bits(&s->gb, svq3->next_slice_index - get_bits_count(&s->gb));
01043                 s->gb.size_in_bits = 8*buf_size;
01044 
01045                 if (svq3_decode_slice_header(avctx))
01046                     return -1;
01047 
01048                 /* TODO: support s->mb_skip_run */
01049             }
01050 
01051             mb_type = svq3_get_ue_golomb(&s->gb);
01052 
01053             if (s->pict_type == AV_PICTURE_TYPE_I) {
01054                 mb_type += 8;
01055             } else if (s->pict_type == AV_PICTURE_TYPE_B && mb_type >= 4) {
01056                 mb_type += 4;
01057             }
01058             if ((unsigned)mb_type > 33 || svq3_decode_mb(svq3, mb_type)) {
01059                 av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
01060                 return -1;
01061             }
01062 
01063             if (mb_type != 0) {
01064                 ff_h264_hl_decode_mb (h);
01065             }
01066 
01067             if (s->pict_type != AV_PICTURE_TYPE_B && !s->low_delay) {
01068                 s->current_picture.f.mb_type[s->mb_x + s->mb_y * s->mb_stride] =
01069                     (s->pict_type == AV_PICTURE_TYPE_P && mb_type < 8) ? (mb_type - 1) : -1;
01070             }
01071         }
01072 
01073         ff_draw_horiz_band(s, 16*s->mb_y, 16);
01074     }
01075 
01076     MPV_frame_end(s);
01077 
01078     if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
01079         *(AVFrame *) data = *(AVFrame *) &s->current_picture;
01080     } else {
01081         *(AVFrame *) data = *(AVFrame *) &s->last_picture;
01082     }
01083 
01084     /* Do not output the last pic after seeking. */
01085     if (s->last_picture_ptr || s->low_delay) {
01086         *data_size = sizeof(AVFrame);
01087     }
01088 
01089     return buf_size;
01090 }
01091 
01092 static int svq3_decode_end(AVCodecContext *avctx)
01093 {
01094     SVQ3Context *svq3 = avctx->priv_data;
01095     H264Context *h = &svq3->h;
01096     MpegEncContext *s = &h->s;
01097 
01098     ff_h264_free_context(h);
01099 
01100     MPV_common_end(s);
01101 
01102     return 0;
01103 }
01104 
01105 AVCodec ff_svq3_decoder = {
01106     .name           = "svq3",
01107     .type           = AVMEDIA_TYPE_VIDEO,
01108     .id             = CODEC_ID_SVQ3,
01109     .priv_data_size = sizeof(SVQ3Context),
01110     .init           = svq3_decode_init,
01111     .close          = svq3_decode_end,
01112     .decode         = svq3_decode_frame,
01113     .capabilities   = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_DELAY,
01114     .long_name = NULL_IF_CONFIG_SMALL("Sorenson Vector Quantizer 3 / Sorenson Video 3 / SVQ3"),
01115     .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUVJ420P, PIX_FMT_NONE},
01116 };