• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

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