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

libavcodec/mpeg12.c

Go to the documentation of this file.
00001 /*
00002  * MPEG-1/2 decoder
00003  * Copyright (c) 2000,2001 Fabrice Bellard
00004  * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
00005  *
00006  * This file is part of FFmpeg.
00007  *
00008  * FFmpeg is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2.1 of the License, or (at your option) any later version.
00012  *
00013  * FFmpeg is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with FFmpeg; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00021  */
00022 
00028 //#define DEBUG
00029 #include "internal.h"
00030 #include "avcodec.h"
00031 #include "dsputil.h"
00032 #include "mpegvideo.h"
00033 
00034 #include "mpeg12.h"
00035 #include "mpeg12data.h"
00036 #include "mpeg12decdata.h"
00037 #include "bytestream.h"
00038 #include "vdpau_internal.h"
00039 #include "xvmc_internal.h"
00040 
00041 //#undef NDEBUG
00042 //#include <assert.h>
00043 
00044 
00045 #define MV_VLC_BITS 9
00046 #define MBINCR_VLC_BITS 9
00047 #define MB_PAT_VLC_BITS 9
00048 #define MB_PTYPE_VLC_BITS 6
00049 #define MB_BTYPE_VLC_BITS 6
00050 
00051 static inline int mpeg1_decode_block_inter(MpegEncContext *s,
00052                               DCTELEM *block,
00053                               int n);
00054 static inline int mpeg1_fast_decode_block_inter(MpegEncContext *s, DCTELEM *block, int n);
00055 static inline int mpeg2_decode_block_non_intra(MpegEncContext *s,
00056                                         DCTELEM *block,
00057                                         int n);
00058 static inline int mpeg2_decode_block_intra(MpegEncContext *s,
00059                                     DCTELEM *block,
00060                                     int n);
00061 static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext *s, DCTELEM *block, int n);
00062 static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s, DCTELEM *block, int n);
00063 static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred);
00064 static void exchange_uv(MpegEncContext *s);
00065 
00066 static const enum PixelFormat pixfmt_xvmc_mpg2_420[] = {
00067                                            PIX_FMT_XVMC_MPEG2_IDCT,
00068                                            PIX_FMT_XVMC_MPEG2_MC,
00069                                            PIX_FMT_NONE};
00070 
00071 uint8_t ff_mpeg12_static_rl_table_store[2][2][2*MAX_RUN + MAX_LEVEL + 3];
00072 
00073 
00074 #define INIT_2D_VLC_RL(rl, static_size)\
00075 {\
00076     static RL_VLC_ELEM rl_vlc_table[static_size];\
00077     INIT_VLC_STATIC(&rl.vlc, TEX_VLC_BITS, rl.n + 2,\
00078              &rl.table_vlc[0][1], 4, 2,\
00079              &rl.table_vlc[0][0], 4, 2, static_size);\
00080 \
00081     rl.rl_vlc[0]= rl_vlc_table;\
00082     init_2d_vlc_rl(&rl);\
00083 }
00084 
00085 static void init_2d_vlc_rl(RLTable *rl)
00086 {
00087     int i;
00088 
00089     for(i=0; i<rl->vlc.table_size; i++){
00090         int code= rl->vlc.table[i][0];
00091         int len = rl->vlc.table[i][1];
00092         int level, run;
00093 
00094         if(len==0){ // illegal code
00095             run= 65;
00096             level= MAX_LEVEL;
00097         }else if(len<0){ //more bits needed
00098             run= 0;
00099             level= code;
00100         }else{
00101             if(code==rl->n){ //esc
00102                 run= 65;
00103                 level= 0;
00104             }else if(code==rl->n+1){ //eob
00105                 run= 0;
00106                 level= 127;
00107             }else{
00108                 run=   rl->table_run  [code] + 1;
00109                 level= rl->table_level[code];
00110             }
00111         }
00112         rl->rl_vlc[0][i].len= len;
00113         rl->rl_vlc[0][i].level= level;
00114         rl->rl_vlc[0][i].run= run;
00115     }
00116 }
00117 
00118 void ff_mpeg12_common_init(MpegEncContext *s)
00119 {
00120 
00121     s->y_dc_scale_table=
00122     s->c_dc_scale_table= mpeg2_dc_scale_table[s->intra_dc_precision];
00123 
00124 }
00125 
00126 void ff_mpeg1_clean_buffers(MpegEncContext *s){
00127     s->last_dc[0] = 1 << (7 + s->intra_dc_precision);
00128     s->last_dc[1] = s->last_dc[0];
00129     s->last_dc[2] = s->last_dc[0];
00130     memset(s->last_mv, 0, sizeof(s->last_mv));
00131 }
00132 
00133 
00134 /******************************************/
00135 /* decoding */
00136 
00137 static VLC mv_vlc;
00138 static VLC mbincr_vlc;
00139 static VLC mb_ptype_vlc;
00140 static VLC mb_btype_vlc;
00141 static VLC mb_pat_vlc;
00142 
00143 av_cold void ff_mpeg12_init_vlcs(void)
00144 {
00145     static int done = 0;
00146 
00147     if (!done) {
00148         done = 1;
00149 
00150         INIT_VLC_STATIC(&dc_lum_vlc, DC_VLC_BITS, 12,
00151                  ff_mpeg12_vlc_dc_lum_bits, 1, 1,
00152                  ff_mpeg12_vlc_dc_lum_code, 2, 2, 512);
00153         INIT_VLC_STATIC(&dc_chroma_vlc,  DC_VLC_BITS, 12,
00154                  ff_mpeg12_vlc_dc_chroma_bits, 1, 1,
00155                  ff_mpeg12_vlc_dc_chroma_code, 2, 2, 514);
00156         INIT_VLC_STATIC(&mv_vlc, MV_VLC_BITS, 17,
00157                  &ff_mpeg12_mbMotionVectorTable[0][1], 2, 1,
00158                  &ff_mpeg12_mbMotionVectorTable[0][0], 2, 1, 518);
00159         INIT_VLC_STATIC(&mbincr_vlc, MBINCR_VLC_BITS, 36,
00160                  &ff_mpeg12_mbAddrIncrTable[0][1], 2, 1,
00161                  &ff_mpeg12_mbAddrIncrTable[0][0], 2, 1, 538);
00162         INIT_VLC_STATIC(&mb_pat_vlc, MB_PAT_VLC_BITS, 64,
00163                  &ff_mpeg12_mbPatTable[0][1], 2, 1,
00164                  &ff_mpeg12_mbPatTable[0][0], 2, 1, 512);
00165 
00166         INIT_VLC_STATIC(&mb_ptype_vlc, MB_PTYPE_VLC_BITS, 7,
00167                  &table_mb_ptype[0][1], 2, 1,
00168                  &table_mb_ptype[0][0], 2, 1, 64);
00169         INIT_VLC_STATIC(&mb_btype_vlc, MB_BTYPE_VLC_BITS, 11,
00170                  &table_mb_btype[0][1], 2, 1,
00171                  &table_mb_btype[0][0], 2, 1, 64);
00172         init_rl(&ff_rl_mpeg1, ff_mpeg12_static_rl_table_store[0]);
00173         init_rl(&ff_rl_mpeg2, ff_mpeg12_static_rl_table_store[1]);
00174 
00175         INIT_2D_VLC_RL(ff_rl_mpeg1, 680);
00176         INIT_2D_VLC_RL(ff_rl_mpeg2, 674);
00177     }
00178 }
00179 
00180 static inline int get_dmv(MpegEncContext *s)
00181 {
00182     if(get_bits1(&s->gb))
00183         return 1 - (get_bits1(&s->gb) << 1);
00184     else
00185         return 0;
00186 }
00187 
00188 static inline int get_qscale(MpegEncContext *s)
00189 {
00190     int qscale = get_bits(&s->gb, 5);
00191     if (s->q_scale_type) {
00192         return non_linear_qscale[qscale];
00193     } else {
00194         return qscale << 1;
00195     }
00196 }
00197 
00198 /* motion type (for MPEG-2) */
00199 #define MT_FIELD 1
00200 #define MT_FRAME 2
00201 #define MT_16X8  2
00202 #define MT_DMV   3
00203 
00204 static int mpeg_decode_mb(MpegEncContext *s,
00205                           DCTELEM block[12][64])
00206 {
00207     int i, j, k, cbp, val, mb_type, motion_type;
00208     const int mb_block_count = 4 + (1<< s->chroma_format);
00209 
00210     dprintf(s->avctx, "decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y);
00211 
00212     assert(s->mb_skipped==0);
00213 
00214     if (s->mb_skip_run-- != 0) {
00215         if (s->pict_type == FF_P_TYPE) {
00216             s->mb_skipped = 1;
00217             s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]= MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
00218         } else {
00219             int mb_type;
00220 
00221             if(s->mb_x)
00222                 mb_type= s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride - 1];
00223             else
00224                 mb_type= s->current_picture.mb_type[ s->mb_width + (s->mb_y-1)*s->mb_stride - 1]; // FIXME not sure if this is allowed in MPEG at all
00225             if(IS_INTRA(mb_type))
00226                 return -1;
00227 
00228             s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]=
00229                 mb_type | MB_TYPE_SKIP;
00230 //            assert(s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride - 1]&(MB_TYPE_16x16|MB_TYPE_16x8));
00231 
00232             if((s->mv[0][0][0]|s->mv[0][0][1]|s->mv[1][0][0]|s->mv[1][0][1])==0)
00233                 s->mb_skipped = 1;
00234         }
00235 
00236         return 0;
00237     }
00238 
00239     switch(s->pict_type) {
00240     default:
00241     case FF_I_TYPE:
00242         if (get_bits1(&s->gb) == 0) {
00243             if (get_bits1(&s->gb) == 0){
00244                 av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in I Frame at %d %d\n", s->mb_x, s->mb_y);
00245                 return -1;
00246             }
00247             mb_type = MB_TYPE_QUANT | MB_TYPE_INTRA;
00248         } else {
00249             mb_type = MB_TYPE_INTRA;
00250         }
00251         break;
00252     case FF_P_TYPE:
00253         mb_type = get_vlc2(&s->gb, mb_ptype_vlc.table, MB_PTYPE_VLC_BITS, 1);
00254         if (mb_type < 0){
00255             av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in P Frame at %d %d\n", s->mb_x, s->mb_y);
00256             return -1;
00257         }
00258         mb_type = ptype2mb_type[ mb_type ];
00259         break;
00260     case FF_B_TYPE:
00261         mb_type = get_vlc2(&s->gb, mb_btype_vlc.table, MB_BTYPE_VLC_BITS, 1);
00262         if (mb_type < 0){
00263             av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in B Frame at %d %d\n", s->mb_x, s->mb_y);
00264             return -1;
00265         }
00266         mb_type = btype2mb_type[ mb_type ];
00267         break;
00268     }
00269     dprintf(s->avctx, "mb_type=%x\n", mb_type);
00270 //    motion_type = 0; /* avoid warning */
00271     if (IS_INTRA(mb_type)) {
00272         s->dsp.clear_blocks(s->block[0]);
00273 
00274         if(!s->chroma_y_shift){
00275             s->dsp.clear_blocks(s->block[6]);
00276         }
00277 
00278         /* compute DCT type */
00279         if (s->picture_structure == PICT_FRAME && //FIXME add an interlaced_dct coded var?
00280             !s->frame_pred_frame_dct) {
00281             s->interlaced_dct = get_bits1(&s->gb);
00282         }
00283 
00284         if (IS_QUANT(mb_type))
00285             s->qscale = get_qscale(s);
00286 
00287         if (s->concealment_motion_vectors) {
00288             /* just parse them */
00289             if (s->picture_structure != PICT_FRAME)
00290                 skip_bits1(&s->gb); /* field select */
00291 
00292             s->mv[0][0][0]= s->last_mv[0][0][0]= s->last_mv[0][1][0] =
00293                 mpeg_decode_motion(s, s->mpeg_f_code[0][0], s->last_mv[0][0][0]);
00294             s->mv[0][0][1]= s->last_mv[0][0][1]= s->last_mv[0][1][1] =
00295                 mpeg_decode_motion(s, s->mpeg_f_code[0][1], s->last_mv[0][0][1]);
00296 
00297             skip_bits1(&s->gb); /* marker */
00298         }else
00299             memset(s->last_mv, 0, sizeof(s->last_mv)); /* reset mv prediction */
00300         s->mb_intra = 1;
00301         //if 1, we memcpy blocks in xvmcvideo
00302         if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1){
00303             ff_xvmc_pack_pblocks(s,-1);//inter are always full blocks
00304             if(s->swap_uv){
00305                 exchange_uv(s);
00306             }
00307         }
00308 
00309         if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
00310             if(s->flags2 & CODEC_FLAG2_FAST){
00311                 for(i=0;i<6;i++) {
00312                     mpeg2_fast_decode_block_intra(s, *s->pblocks[i], i);
00313                 }
00314             }else{
00315                 for(i=0;i<mb_block_count;i++) {
00316                     if (mpeg2_decode_block_intra(s, *s->pblocks[i], i) < 0)
00317                         return -1;
00318                 }
00319             }
00320         } else {
00321             for(i=0;i<6;i++) {
00322                 if (ff_mpeg1_decode_block_intra(s, *s->pblocks[i], i) < 0)
00323                     return -1;
00324             }
00325         }
00326     } else {
00327         if (mb_type & MB_TYPE_ZERO_MV){
00328             assert(mb_type & MB_TYPE_CBP);
00329 
00330             s->mv_dir = MV_DIR_FORWARD;
00331             if(s->picture_structure == PICT_FRAME){
00332                 if(!s->frame_pred_frame_dct)
00333                     s->interlaced_dct = get_bits1(&s->gb);
00334                 s->mv_type = MV_TYPE_16X16;
00335             }else{
00336                 s->mv_type = MV_TYPE_FIELD;
00337                 mb_type |= MB_TYPE_INTERLACED;
00338                 s->field_select[0][0]= s->picture_structure - 1;
00339             }
00340 
00341             if (IS_QUANT(mb_type))
00342                 s->qscale = get_qscale(s);
00343 
00344             s->last_mv[0][0][0] = 0;
00345             s->last_mv[0][0][1] = 0;
00346             s->last_mv[0][1][0] = 0;
00347             s->last_mv[0][1][1] = 0;
00348             s->mv[0][0][0] = 0;
00349             s->mv[0][0][1] = 0;
00350         }else{
00351             assert(mb_type & MB_TYPE_L0L1);
00352 //FIXME decide if MBs in field pictures are MB_TYPE_INTERLACED
00353             /* get additional motion vector type */
00354             if (s->frame_pred_frame_dct)
00355                 motion_type = MT_FRAME;
00356             else{
00357                 motion_type = get_bits(&s->gb, 2);
00358                 if (s->picture_structure == PICT_FRAME && HAS_CBP(mb_type))
00359                     s->interlaced_dct = get_bits1(&s->gb);
00360             }
00361 
00362             if (IS_QUANT(mb_type))
00363                 s->qscale = get_qscale(s);
00364 
00365             /* motion vectors */
00366             s->mv_dir= (mb_type>>13)&3;
00367             dprintf(s->avctx, "motion_type=%d\n", motion_type);
00368             switch(motion_type) {
00369             case MT_FRAME: /* or MT_16X8 */
00370                 if (s->picture_structure == PICT_FRAME) {
00371                     mb_type |= MB_TYPE_16x16;
00372                     s->mv_type = MV_TYPE_16X16;
00373                     for(i=0;i<2;i++) {
00374                         if (USES_LIST(mb_type, i)) {
00375                             /* MT_FRAME */
00376                             s->mv[i][0][0]= s->last_mv[i][0][0]= s->last_mv[i][1][0] =
00377                                 mpeg_decode_motion(s, s->mpeg_f_code[i][0], s->last_mv[i][0][0]);
00378                             s->mv[i][0][1]= s->last_mv[i][0][1]= s->last_mv[i][1][1] =
00379                                 mpeg_decode_motion(s, s->mpeg_f_code[i][1], s->last_mv[i][0][1]);
00380                             /* full_pel: only for MPEG-1 */
00381                             if (s->full_pel[i]){
00382                                 s->mv[i][0][0] <<= 1;
00383                                 s->mv[i][0][1] <<= 1;
00384                             }
00385                         }
00386                     }
00387                 } else {
00388                     mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
00389                     s->mv_type = MV_TYPE_16X8;
00390                     for(i=0;i<2;i++) {
00391                         if (USES_LIST(mb_type, i)) {
00392                             /* MT_16X8 */
00393                             for(j=0;j<2;j++) {
00394                                 s->field_select[i][j] = get_bits1(&s->gb);
00395                                 for(k=0;k<2;k++) {
00396                                     val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
00397                                                              s->last_mv[i][j][k]);
00398                                     s->last_mv[i][j][k] = val;
00399                                     s->mv[i][j][k] = val;
00400                                 }
00401                             }
00402                         }
00403                     }
00404                 }
00405                 break;
00406             case MT_FIELD:
00407                 s->mv_type = MV_TYPE_FIELD;
00408                 if (s->picture_structure == PICT_FRAME) {
00409                     mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
00410                     for(i=0;i<2;i++) {
00411                         if (USES_LIST(mb_type, i)) {
00412                             for(j=0;j<2;j++) {
00413                                 s->field_select[i][j] = get_bits1(&s->gb);
00414                                 val = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
00415                                                          s->last_mv[i][j][0]);
00416                                 s->last_mv[i][j][0] = val;
00417                                 s->mv[i][j][0] = val;
00418                                 dprintf(s->avctx, "fmx=%d\n", val);
00419                                 val = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
00420                                                          s->last_mv[i][j][1] >> 1);
00421                                 s->last_mv[i][j][1] = val << 1;
00422                                 s->mv[i][j][1] = val;
00423                                 dprintf(s->avctx, "fmy=%d\n", val);
00424                             }
00425                         }
00426                     }
00427                 } else {
00428                     mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
00429                     for(i=0;i<2;i++) {
00430                         if (USES_LIST(mb_type, i)) {
00431                             s->field_select[i][0] = get_bits1(&s->gb);
00432                             for(k=0;k<2;k++) {
00433                                 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
00434                                                          s->last_mv[i][0][k]);
00435                                 s->last_mv[i][0][k] = val;
00436                                 s->last_mv[i][1][k] = val;
00437                                 s->mv[i][0][k] = val;
00438                             }
00439                         }
00440                     }
00441                 }
00442                 break;
00443             case MT_DMV:
00444                 s->mv_type = MV_TYPE_DMV;
00445                 for(i=0;i<2;i++) {
00446                     if (USES_LIST(mb_type, i)) {
00447                         int dmx, dmy, mx, my, m;
00448                         mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
00449                                                 s->last_mv[i][0][0]);
00450                         s->last_mv[i][0][0] = mx;
00451                         s->last_mv[i][1][0] = mx;
00452                         dmx = get_dmv(s);
00453                         my = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
00454                                                 s->last_mv[i][0][1] >> 1);
00455                         dmy = get_dmv(s);
00456 
00457 
00458                         s->last_mv[i][0][1] = my<<1;
00459                         s->last_mv[i][1][1] = my<<1;
00460 
00461                         s->mv[i][0][0] = mx;
00462                         s->mv[i][0][1] = my;
00463                         s->mv[i][1][0] = mx;//not used
00464                         s->mv[i][1][1] = my;//not used
00465 
00466                         if (s->picture_structure == PICT_FRAME) {
00467                             mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
00468 
00469                             //m = 1 + 2 * s->top_field_first;
00470                             m = s->top_field_first ? 1 : 3;
00471 
00472                             /* top -> top pred */
00473                             s->mv[i][2][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
00474                             s->mv[i][2][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
00475                             m = 4 - m;
00476                             s->mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
00477                             s->mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
00478                         } else {
00479                             mb_type |= MB_TYPE_16x16;
00480 
00481                             s->mv[i][2][0] = ((mx + (mx > 0)) >> 1) + dmx;
00482                             s->mv[i][2][1] = ((my + (my > 0)) >> 1) + dmy;
00483                             if(s->picture_structure == PICT_TOP_FIELD)
00484                                 s->mv[i][2][1]--;
00485                             else
00486                                 s->mv[i][2][1]++;
00487                         }
00488                     }
00489                 }
00490                 break;
00491             default:
00492                 av_log(s->avctx, AV_LOG_ERROR, "00 motion_type at %d %d\n", s->mb_x, s->mb_y);
00493                 return -1;
00494             }
00495         }
00496 
00497         s->mb_intra = 0;
00498         if (HAS_CBP(mb_type)) {
00499             s->dsp.clear_blocks(s->block[0]);
00500 
00501             cbp = get_vlc2(&s->gb, mb_pat_vlc.table, MB_PAT_VLC_BITS, 1);
00502             if(mb_block_count > 6){
00503                  cbp<<= mb_block_count-6;
00504                  cbp |= get_bits(&s->gb, mb_block_count-6);
00505                  s->dsp.clear_blocks(s->block[6]);
00506             }
00507             if (cbp <= 0){
00508                 av_log(s->avctx, AV_LOG_ERROR, "invalid cbp at %d %d\n", s->mb_x, s->mb_y);
00509                 return -1;
00510             }
00511 
00512             //if 1, we memcpy blocks in xvmcvideo
00513             if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1){
00514                 ff_xvmc_pack_pblocks(s,cbp);
00515                 if(s->swap_uv){
00516                     exchange_uv(s);
00517                 }
00518             }
00519 
00520             if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
00521                 if(s->flags2 & CODEC_FLAG2_FAST){
00522                     for(i=0;i<6;i++) {
00523                         if(cbp & 32) {
00524                             mpeg2_fast_decode_block_non_intra(s, *s->pblocks[i], i);
00525                         } else {
00526                             s->block_last_index[i] = -1;
00527                         }
00528                         cbp+=cbp;
00529                     }
00530                 }else{
00531                     cbp<<= 12-mb_block_count;
00532 
00533                     for(i=0;i<mb_block_count;i++) {
00534                         if ( cbp & (1<<11) ) {
00535                             if (mpeg2_decode_block_non_intra(s, *s->pblocks[i], i) < 0)
00536                                 return -1;
00537                         } else {
00538                             s->block_last_index[i] = -1;
00539                         }
00540                         cbp+=cbp;
00541                     }
00542                 }
00543             } else {
00544                 if(s->flags2 & CODEC_FLAG2_FAST){
00545                     for(i=0;i<6;i++) {
00546                         if (cbp & 32) {
00547                             mpeg1_fast_decode_block_inter(s, *s->pblocks[i], i);
00548                         } else {
00549                             s->block_last_index[i] = -1;
00550                         }
00551                         cbp+=cbp;
00552                     }
00553                 }else{
00554                     for(i=0;i<6;i++) {
00555                         if (cbp & 32) {
00556                             if (mpeg1_decode_block_inter(s, *s->pblocks[i], i) < 0)
00557                                 return -1;
00558                         } else {
00559                             s->block_last_index[i] = -1;
00560                         }
00561                         cbp+=cbp;
00562                     }
00563                 }
00564             }
00565         }else{
00566             for(i=0;i<12;i++)
00567                 s->block_last_index[i] = -1;
00568         }
00569     }
00570 
00571     s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]= mb_type;
00572 
00573     return 0;
00574 }
00575 
00576 /* as H.263, but only 17 codes */
00577 static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
00578 {
00579     int code, sign, val, l, shift;
00580 
00581     code = get_vlc2(&s->gb, mv_vlc.table, MV_VLC_BITS, 2);
00582     if (code == 0) {
00583         return pred;
00584     }
00585     if (code < 0) {
00586         return 0xffff;
00587     }
00588 
00589     sign = get_bits1(&s->gb);
00590     shift = fcode - 1;
00591     val = code;
00592     if (shift) {
00593         val = (val - 1) << shift;
00594         val |= get_bits(&s->gb, shift);
00595         val++;
00596     }
00597     if (sign)
00598         val = -val;
00599     val += pred;
00600 
00601     /* modulo decoding */
00602     l= INT_BIT - 5 - shift;
00603     val = (val<<l)>>l;
00604     return val;
00605 }
00606 
00607 inline int ff_mpeg1_decode_block_intra(MpegEncContext *s,
00608                                DCTELEM *block,
00609                                int n)
00610 {
00611     int level, dc, diff, i, j, run;
00612     int component;
00613     RLTable *rl = &ff_rl_mpeg1;
00614     uint8_t * const scantable= s->intra_scantable.permutated;
00615     const uint16_t *quant_matrix= s->intra_matrix;
00616     const int qscale= s->qscale;
00617 
00618     /* DC coefficient */
00619     component = (n <= 3 ? 0 : n - 4 + 1);
00620     diff = decode_dc(&s->gb, component);
00621     if (diff >= 0xffff)
00622         return -1;
00623     dc = s->last_dc[component];
00624     dc += diff;
00625     s->last_dc[component] = dc;
00626     block[0] = dc*quant_matrix[0];
00627     dprintf(s->avctx, "dc=%d diff=%d\n", dc, diff);
00628     i = 0;
00629     {
00630         OPEN_READER(re, &s->gb);
00631         /* now quantify & encode AC coefficients */
00632         for(;;) {
00633             UPDATE_CACHE(re, &s->gb);
00634             GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
00635 
00636             if(level == 127){
00637                 break;
00638             } else if(level != 0) {
00639                 i += run;
00640                 j = scantable[i];
00641                 level= (level*qscale*quant_matrix[j])>>4;
00642                 level= (level-1)|1;
00643                 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
00644                 LAST_SKIP_BITS(re, &s->gb, 1);
00645             } else {
00646                 /* escape */
00647                 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
00648                 UPDATE_CACHE(re, &s->gb);
00649                 level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
00650                 if (level == -128) {
00651                     level = SHOW_UBITS(re, &s->gb, 8) - 256; LAST_SKIP_BITS(re, &s->gb, 8);
00652                 } else if (level == 0) {
00653                     level = SHOW_UBITS(re, &s->gb, 8)      ; LAST_SKIP_BITS(re, &s->gb, 8);
00654                 }
00655                 i += run;
00656                 j = scantable[i];
00657                 if(level<0){
00658                     level= -level;
00659                     level= (level*qscale*quant_matrix[j])>>4;
00660                     level= (level-1)|1;
00661                     level= -level;
00662                 }else{
00663                     level= (level*qscale*quant_matrix[j])>>4;
00664                     level= (level-1)|1;
00665                 }
00666             }
00667             if (i > 63){
00668                 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
00669                 return -1;
00670             }
00671 
00672             block[j] = level;
00673         }
00674         CLOSE_READER(re, &s->gb);
00675     }
00676     s->block_last_index[n] = i;
00677    return 0;
00678 }
00679 
00680 static inline int mpeg1_decode_block_inter(MpegEncContext *s,
00681                                DCTELEM *block,
00682                                int n)
00683 {
00684     int level, i, j, run;
00685     RLTable *rl = &ff_rl_mpeg1;
00686     uint8_t * const scantable= s->intra_scantable.permutated;
00687     const uint16_t *quant_matrix= s->inter_matrix;
00688     const int qscale= s->qscale;
00689 
00690     {
00691         OPEN_READER(re, &s->gb);
00692         i = -1;
00693         // special case for first coefficient, no need to add second VLC table
00694         UPDATE_CACHE(re, &s->gb);
00695         if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
00696             level= (3*qscale*quant_matrix[0])>>5;
00697             level= (level-1)|1;
00698             if(GET_CACHE(re, &s->gb)&0x40000000)
00699                 level= -level;
00700             block[0] = level;
00701             i++;
00702             SKIP_BITS(re, &s->gb, 2);
00703             if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
00704                 goto end;
00705         }
00706 #if MIN_CACHE_BITS < 19
00707         UPDATE_CACHE(re, &s->gb);
00708 #endif
00709         /* now quantify & encode AC coefficients */
00710         for(;;) {
00711             GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
00712 
00713             if(level != 0) {
00714                 i += run;
00715                 j = scantable[i];
00716                 level= ((level*2+1)*qscale*quant_matrix[j])>>5;
00717                 level= (level-1)|1;
00718                 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
00719                 SKIP_BITS(re, &s->gb, 1);
00720             } else {
00721                 /* escape */
00722                 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
00723                 UPDATE_CACHE(re, &s->gb);
00724                 level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
00725                 if (level == -128) {
00726                     level = SHOW_UBITS(re, &s->gb, 8) - 256; SKIP_BITS(re, &s->gb, 8);
00727                 } else if (level == 0) {
00728                     level = SHOW_UBITS(re, &s->gb, 8)      ; SKIP_BITS(re, &s->gb, 8);
00729                 }
00730                 i += run;
00731                 j = scantable[i];
00732                 if(level<0){
00733                     level= -level;
00734                     level= ((level*2+1)*qscale*quant_matrix[j])>>5;
00735                     level= (level-1)|1;
00736                     level= -level;
00737                 }else{
00738                     level= ((level*2+1)*qscale*quant_matrix[j])>>5;
00739                     level= (level-1)|1;
00740                 }
00741             }
00742             if (i > 63){
00743                 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
00744                 return -1;
00745             }
00746 
00747             block[j] = level;
00748 #if MIN_CACHE_BITS < 19
00749             UPDATE_CACHE(re, &s->gb);
00750 #endif
00751             if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
00752                 break;
00753 #if MIN_CACHE_BITS >= 19
00754             UPDATE_CACHE(re, &s->gb);
00755 #endif
00756         }
00757 end:
00758         LAST_SKIP_BITS(re, &s->gb, 2);
00759         CLOSE_READER(re, &s->gb);
00760     }
00761     s->block_last_index[n] = i;
00762     return 0;
00763 }
00764 
00765 static inline int mpeg1_fast_decode_block_inter(MpegEncContext *s, DCTELEM *block, int n)
00766 {
00767     int level, i, j, run;
00768     RLTable *rl = &ff_rl_mpeg1;
00769     uint8_t * const scantable= s->intra_scantable.permutated;
00770     const int qscale= s->qscale;
00771 
00772     {
00773         OPEN_READER(re, &s->gb);
00774         i = -1;
00775         // special case for first coefficient, no need to add second VLC table
00776         UPDATE_CACHE(re, &s->gb);
00777         if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
00778             level= (3*qscale)>>1;
00779             level= (level-1)|1;
00780             if(GET_CACHE(re, &s->gb)&0x40000000)
00781                 level= -level;
00782             block[0] = level;
00783             i++;
00784             SKIP_BITS(re, &s->gb, 2);
00785             if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
00786                 goto end;
00787         }
00788 #if MIN_CACHE_BITS < 19
00789         UPDATE_CACHE(re, &s->gb);
00790 #endif
00791 
00792         /* now quantify & encode AC coefficients */
00793         for(;;) {
00794             GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
00795 
00796             if(level != 0) {
00797                 i += run;
00798                 j = scantable[i];
00799                 level= ((level*2+1)*qscale)>>1;
00800                 level= (level-1)|1;
00801                 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
00802                 SKIP_BITS(re, &s->gb, 1);
00803             } else {
00804                 /* escape */
00805                 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
00806                 UPDATE_CACHE(re, &s->gb);
00807                 level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
00808                 if (level == -128) {
00809                     level = SHOW_UBITS(re, &s->gb, 8) - 256; SKIP_BITS(re, &s->gb, 8);
00810                 } else if (level == 0) {
00811                     level = SHOW_UBITS(re, &s->gb, 8)      ; SKIP_BITS(re, &s->gb, 8);
00812                 }
00813                 i += run;
00814                 j = scantable[i];
00815                 if(level<0){
00816                     level= -level;
00817                     level= ((level*2+1)*qscale)>>1;
00818                     level= (level-1)|1;
00819                     level= -level;
00820                 }else{
00821                     level= ((level*2+1)*qscale)>>1;
00822                     level= (level-1)|1;
00823                 }
00824             }
00825 
00826             block[j] = level;
00827 #if MIN_CACHE_BITS < 19
00828             UPDATE_CACHE(re, &s->gb);
00829 #endif
00830             if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
00831                 break;
00832 #if MIN_CACHE_BITS >= 19
00833             UPDATE_CACHE(re, &s->gb);
00834 #endif
00835         }
00836 end:
00837         LAST_SKIP_BITS(re, &s->gb, 2);
00838         CLOSE_READER(re, &s->gb);
00839     }
00840     s->block_last_index[n] = i;
00841     return 0;
00842 }
00843 
00844 
00845 static inline int mpeg2_decode_block_non_intra(MpegEncContext *s,
00846                                DCTELEM *block,
00847                                int n)
00848 {
00849     int level, i, j, run;
00850     RLTable *rl = &ff_rl_mpeg1;
00851     uint8_t * const scantable= s->intra_scantable.permutated;
00852     const uint16_t *quant_matrix;
00853     const int qscale= s->qscale;
00854     int mismatch;
00855 
00856     mismatch = 1;
00857 
00858     {
00859         OPEN_READER(re, &s->gb);
00860         i = -1;
00861         if (n < 4)
00862             quant_matrix = s->inter_matrix;
00863         else
00864             quant_matrix = s->chroma_inter_matrix;
00865 
00866         // special case for first coefficient, no need to add second VLC table
00867         UPDATE_CACHE(re, &s->gb);
00868         if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
00869             level= (3*qscale*quant_matrix[0])>>5;
00870             if(GET_CACHE(re, &s->gb)&0x40000000)
00871                 level= -level;
00872             block[0] = level;
00873             mismatch ^= level;
00874             i++;
00875             SKIP_BITS(re, &s->gb, 2);
00876             if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
00877                 goto end;
00878         }
00879 #if MIN_CACHE_BITS < 19
00880         UPDATE_CACHE(re, &s->gb);
00881 #endif
00882 
00883         /* now quantify & encode AC coefficients */
00884         for(;;) {
00885             GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
00886 
00887             if(level != 0) {
00888                 i += run;
00889                 j = scantable[i];
00890                 level= ((level*2+1)*qscale*quant_matrix[j])>>5;
00891                 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
00892                 SKIP_BITS(re, &s->gb, 1);
00893             } else {
00894                 /* escape */
00895                 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
00896                 UPDATE_CACHE(re, &s->gb);
00897                 level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
00898 
00899                 i += run;
00900                 j = scantable[i];
00901                 if(level<0){
00902                     level= ((-level*2+1)*qscale*quant_matrix[j])>>5;
00903                     level= -level;
00904                 }else{
00905                     level= ((level*2+1)*qscale*quant_matrix[j])>>5;
00906                 }
00907             }
00908             if (i > 63){
00909                 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
00910                 return -1;
00911             }
00912 
00913             mismatch ^= level;
00914             block[j] = level;
00915 #if MIN_CACHE_BITS < 19
00916             UPDATE_CACHE(re, &s->gb);
00917 #endif
00918             if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
00919                 break;
00920 #if MIN_CACHE_BITS >= 19
00921             UPDATE_CACHE(re, &s->gb);
00922 #endif
00923         }
00924 end:
00925         LAST_SKIP_BITS(re, &s->gb, 2);
00926         CLOSE_READER(re, &s->gb);
00927     }
00928     block[63] ^= (mismatch & 1);
00929 
00930     s->block_last_index[n] = i;
00931     return 0;
00932 }
00933 
00934 static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext *s,
00935                                DCTELEM *block,
00936                                int n)
00937 {
00938     int level, i, j, run;
00939     RLTable *rl = &ff_rl_mpeg1;
00940     uint8_t * const scantable= s->intra_scantable.permutated;
00941     const int qscale= s->qscale;
00942     OPEN_READER(re, &s->gb);
00943     i = -1;
00944 
00945     // special case for first coefficient, no need to add second VLC table
00946     UPDATE_CACHE(re, &s->gb);
00947     if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
00948         level= (3*qscale)>>1;
00949         if(GET_CACHE(re, &s->gb)&0x40000000)
00950             level= -level;
00951         block[0] = level;
00952         i++;
00953         SKIP_BITS(re, &s->gb, 2);
00954         if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
00955             goto end;
00956     }
00957 #if MIN_CACHE_BITS < 19
00958     UPDATE_CACHE(re, &s->gb);
00959 #endif
00960 
00961     /* now quantify & encode AC coefficients */
00962     for(;;) {
00963         GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
00964 
00965         if(level != 0) {
00966             i += run;
00967             j = scantable[i];
00968             level= ((level*2+1)*qscale)>>1;
00969             level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
00970             SKIP_BITS(re, &s->gb, 1);
00971         } else {
00972             /* escape */
00973             run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
00974             UPDATE_CACHE(re, &s->gb);
00975             level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
00976 
00977             i += run;
00978             j = scantable[i];
00979             if(level<0){
00980                 level= ((-level*2+1)*qscale)>>1;
00981                 level= -level;
00982             }else{
00983                 level= ((level*2+1)*qscale)>>1;
00984             }
00985         }
00986 
00987         block[j] = level;
00988 #if MIN_CACHE_BITS < 19
00989         UPDATE_CACHE(re, &s->gb);
00990 #endif
00991         if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
00992             break;
00993 #if MIN_CACHE_BITS >=19
00994         UPDATE_CACHE(re, &s->gb);
00995 #endif
00996     }
00997 end:
00998     LAST_SKIP_BITS(re, &s->gb, 2);
00999     CLOSE_READER(re, &s->gb);
01000     s->block_last_index[n] = i;
01001     return 0;
01002 }
01003 
01004 
01005 static inline int mpeg2_decode_block_intra(MpegEncContext *s,
01006                                DCTELEM *block,
01007                                int n)
01008 {
01009     int level, dc, diff, i, j, run;
01010     int component;
01011     RLTable *rl;
01012     uint8_t * const scantable= s->intra_scantable.permutated;
01013     const uint16_t *quant_matrix;
01014     const int qscale= s->qscale;
01015     int mismatch;
01016 
01017     /* DC coefficient */
01018     if (n < 4){
01019         quant_matrix = s->intra_matrix;
01020         component = 0;
01021     }else{
01022         quant_matrix = s->chroma_intra_matrix;
01023         component = (n&1) + 1;
01024     }
01025     diff = decode_dc(&s->gb, component);
01026     if (diff >= 0xffff)
01027         return -1;
01028     dc = s->last_dc[component];
01029     dc += diff;
01030     s->last_dc[component] = dc;
01031     block[0] = dc << (3 - s->intra_dc_precision);
01032     dprintf(s->avctx, "dc=%d\n", block[0]);
01033     mismatch = block[0] ^ 1;
01034     i = 0;
01035     if (s->intra_vlc_format)
01036         rl = &ff_rl_mpeg2;
01037     else
01038         rl = &ff_rl_mpeg1;
01039 
01040     {
01041         OPEN_READER(re, &s->gb);
01042         /* now quantify & encode AC coefficients */
01043         for(;;) {
01044             UPDATE_CACHE(re, &s->gb);
01045             GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
01046 
01047             if(level == 127){
01048                 break;
01049             } else if(level != 0) {
01050                 i += run;
01051                 j = scantable[i];
01052                 level= (level*qscale*quant_matrix[j])>>4;
01053                 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
01054                 LAST_SKIP_BITS(re, &s->gb, 1);
01055             } else {
01056                 /* escape */
01057                 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
01058                 UPDATE_CACHE(re, &s->gb);
01059                 level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
01060                 i += run;
01061                 j = scantable[i];
01062                 if(level<0){
01063                     level= (-level*qscale*quant_matrix[j])>>4;
01064                     level= -level;
01065                 }else{
01066                     level= (level*qscale*quant_matrix[j])>>4;
01067                 }
01068             }
01069             if (i > 63){
01070                 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
01071                 return -1;
01072             }
01073 
01074             mismatch^= level;
01075             block[j] = level;
01076         }
01077         CLOSE_READER(re, &s->gb);
01078     }
01079     block[63]^= mismatch&1;
01080 
01081     s->block_last_index[n] = i;
01082     return 0;
01083 }
01084 
01085 static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s,
01086                                DCTELEM *block,
01087                                int n)
01088 {
01089     int level, dc, diff, j, run;
01090     int component;
01091     RLTable *rl;
01092     uint8_t * scantable= s->intra_scantable.permutated;
01093     const uint16_t *quant_matrix;
01094     const int qscale= s->qscale;
01095 
01096     /* DC coefficient */
01097     if (n < 4){
01098         quant_matrix = s->intra_matrix;
01099         component = 0;
01100     }else{
01101         quant_matrix = s->chroma_intra_matrix;
01102         component = (n&1) + 1;
01103     }
01104     diff = decode_dc(&s->gb, component);
01105     if (diff >= 0xffff)
01106         return -1;
01107     dc = s->last_dc[component];
01108     dc += diff;
01109     s->last_dc[component] = dc;
01110     block[0] = dc << (3 - s->intra_dc_precision);
01111     if (s->intra_vlc_format)
01112         rl = &ff_rl_mpeg2;
01113     else
01114         rl = &ff_rl_mpeg1;
01115 
01116     {
01117         OPEN_READER(re, &s->gb);
01118         /* now quantify & encode AC coefficients */
01119         for(;;) {
01120             UPDATE_CACHE(re, &s->gb);
01121             GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
01122 
01123             if(level == 127){
01124                 break;
01125             } else if(level != 0) {
01126                 scantable += run;
01127                 j = *scantable;
01128                 level= (level*qscale*quant_matrix[j])>>4;
01129                 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
01130                 LAST_SKIP_BITS(re, &s->gb, 1);
01131             } else {
01132                 /* escape */
01133                 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
01134                 UPDATE_CACHE(re, &s->gb);
01135                 level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
01136                 scantable += run;
01137                 j = *scantable;
01138                 if(level<0){
01139                     level= (-level*qscale*quant_matrix[j])>>4;
01140                     level= -level;
01141                 }else{
01142                     level= (level*qscale*quant_matrix[j])>>4;
01143                 }
01144             }
01145 
01146             block[j] = level;
01147         }
01148         CLOSE_READER(re, &s->gb);
01149     }
01150 
01151     s->block_last_index[n] = scantable - s->intra_scantable.permutated;
01152     return 0;
01153 }
01154 
01155 typedef struct Mpeg1Context {
01156     MpegEncContext mpeg_enc_ctx;
01157     int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
01158     int repeat_field; /* true if we must repeat the field */
01159     AVPanScan pan_scan; 
01160     int slice_count;
01161     int swap_uv;//indicate VCR2
01162     int save_aspect_info;
01163     int save_width, save_height;
01164     AVRational frame_rate_ext;       
01165 
01166     int extradata_decoded;
01167 } Mpeg1Context;
01168 
01169 static av_cold int mpeg_decode_init(AVCodecContext *avctx)
01170 {
01171     Mpeg1Context *s = avctx->priv_data;
01172     MpegEncContext *s2 = &s->mpeg_enc_ctx;
01173     int i;
01174 
01175     /* we need some permutation to store matrices,
01176      * until MPV_common_init() sets the real permutation. */
01177     for(i=0;i<64;i++)
01178        s2->dsp.idct_permutation[i]=i;
01179 
01180     MPV_decode_defaults(s2);
01181 
01182     s->mpeg_enc_ctx.avctx= avctx;
01183     s->mpeg_enc_ctx.flags= avctx->flags;
01184     s->mpeg_enc_ctx.flags2= avctx->flags2;
01185     ff_mpeg12_common_init(&s->mpeg_enc_ctx);
01186     ff_mpeg12_init_vlcs();
01187 
01188     s->mpeg_enc_ctx_allocated = 0;
01189     s->mpeg_enc_ctx.picture_number = 0;
01190     s->repeat_field = 0;
01191     s->mpeg_enc_ctx.codec_id= avctx->codec->id;
01192     return 0;
01193 }
01194 
01195 static void quant_matrix_rebuild(uint16_t *matrix, const uint8_t *old_perm,
01196                                      const uint8_t *new_perm){
01197     uint16_t temp_matrix[64];
01198     int i;
01199 
01200     memcpy(temp_matrix,matrix,64*sizeof(uint16_t));
01201 
01202     for(i=0;i<64;i++){
01203         matrix[new_perm[i]] = temp_matrix[old_perm[i]];
01204     }
01205 }
01206 
01207 static enum PixelFormat mpeg_get_pixelformat(AVCodecContext *avctx){
01208     Mpeg1Context *s1 = avctx->priv_data;
01209     MpegEncContext *s = &s1->mpeg_enc_ctx;
01210 
01211     if(avctx->xvmc_acceleration)
01212         return avctx->get_format(avctx,pixfmt_xvmc_mpg2_420);
01213     else if(avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU){
01214         if(avctx->codec_id == CODEC_ID_MPEG1VIDEO)
01215             return PIX_FMT_VDPAU_MPEG1;
01216         else
01217             return PIX_FMT_VDPAU_MPEG2;
01218     }else{
01219         if(s->chroma_format <  2)
01220             return avctx->get_format(avctx,ff_hwaccel_pixfmt_list_420);
01221         else if(s->chroma_format == 2)
01222             return PIX_FMT_YUV422P;
01223         else
01224             return PIX_FMT_YUV444P;
01225     }
01226 }
01227 
01228 /* Call this function when we know all parameters.
01229  * It may be called in different places for MPEG-1 and MPEG-2. */
01230 static int mpeg_decode_postinit(AVCodecContext *avctx){
01231     Mpeg1Context *s1 = avctx->priv_data;
01232     MpegEncContext *s = &s1->mpeg_enc_ctx;
01233     uint8_t old_permutation[64];
01234 
01235     if (
01236         (s1->mpeg_enc_ctx_allocated == 0)||
01237         avctx->coded_width  != s->width ||
01238         avctx->coded_height != s->height||
01239         s1->save_width != s->width ||
01240         s1->save_height != s->height ||
01241         s1->save_aspect_info != s->aspect_ratio_info||
01242         0)
01243     {
01244 
01245         if (s1->mpeg_enc_ctx_allocated) {
01246             ParseContext pc= s->parse_context;
01247             s->parse_context.buffer=0;
01248             MPV_common_end(s);
01249             s->parse_context= pc;
01250         }
01251 
01252         if( (s->width == 0 )||(s->height == 0))
01253             return -2;
01254 
01255         avcodec_set_dimensions(avctx, s->width, s->height);
01256         avctx->bit_rate = s->bit_rate;
01257         s1->save_aspect_info = s->aspect_ratio_info;
01258         s1->save_width = s->width;
01259         s1->save_height = s->height;
01260 
01261         /* low_delay may be forced, in this case we will have B-frames
01262          * that behave like P-frames. */
01263         avctx->has_b_frames = !(s->low_delay);
01264 
01265         assert((avctx->sub_id==1) == (avctx->codec_id==CODEC_ID_MPEG1VIDEO));
01266         if(avctx->codec_id==CODEC_ID_MPEG1VIDEO){
01267             //MPEG-1 fps
01268             avctx->time_base.den= ff_frame_rate_tab[s->frame_rate_index].num;
01269             avctx->time_base.num= ff_frame_rate_tab[s->frame_rate_index].den;
01270             //MPEG-1 aspect
01271             avctx->sample_aspect_ratio= av_d2q(
01272                     1.0/ff_mpeg1_aspect[s->aspect_ratio_info], 255);
01273             avctx->ticks_per_frame=1;
01274         }else{//MPEG-2
01275         //MPEG-2 fps
01276             av_reduce(
01277                 &s->avctx->time_base.den,
01278                 &s->avctx->time_base.num,
01279                 ff_frame_rate_tab[s->frame_rate_index].num * s1->frame_rate_ext.num*2,
01280                 ff_frame_rate_tab[s->frame_rate_index].den * s1->frame_rate_ext.den,
01281                 1<<30);
01282             avctx->ticks_per_frame=2;
01283         //MPEG-2 aspect
01284             if(s->aspect_ratio_info > 1){
01285                 //we ignore the spec here as reality does not match the spec, see for example
01286                 // res_change_ffmpeg_aspect.ts and sequence-display-aspect.mpg
01287                 if( (s1->pan_scan.width == 0 )||(s1->pan_scan.height == 0) || 1){
01288                     s->avctx->sample_aspect_ratio=
01289                         av_div_q(
01290                          ff_mpeg2_aspect[s->aspect_ratio_info],
01291                          (AVRational){s->width, s->height}
01292                          );
01293                 }else{
01294                     s->avctx->sample_aspect_ratio=
01295                         av_div_q(
01296                          ff_mpeg2_aspect[s->aspect_ratio_info],
01297                          (AVRational){s1->pan_scan.width, s1->pan_scan.height}
01298                         );
01299                 }
01300             }else{
01301                 s->avctx->sample_aspect_ratio=
01302                     ff_mpeg2_aspect[s->aspect_ratio_info];
01303             }
01304         }//MPEG-2
01305 
01306         avctx->pix_fmt = mpeg_get_pixelformat(avctx);
01307         avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
01308         //until then pix_fmt may be changed right after codec init
01309         if( avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT ||
01310             avctx->hwaccel ||
01311             s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU )
01312             if( avctx->idct_algo == FF_IDCT_AUTO )
01313                 avctx->idct_algo = FF_IDCT_SIMPLE;
01314 
01315         /* Quantization matrices may need reordering
01316          * if DCT permutation is changed. */
01317         memcpy(old_permutation,s->dsp.idct_permutation,64*sizeof(uint8_t));
01318 
01319         if (MPV_common_init(s) < 0)
01320             return -2;
01321 
01322         quant_matrix_rebuild(s->intra_matrix,       old_permutation,s->dsp.idct_permutation);
01323         quant_matrix_rebuild(s->inter_matrix,       old_permutation,s->dsp.idct_permutation);
01324         quant_matrix_rebuild(s->chroma_intra_matrix,old_permutation,s->dsp.idct_permutation);
01325         quant_matrix_rebuild(s->chroma_inter_matrix,old_permutation,s->dsp.idct_permutation);
01326 
01327         s1->mpeg_enc_ctx_allocated = 1;
01328     }
01329     return 0;
01330 }
01331 
01332 static int mpeg1_decode_picture(AVCodecContext *avctx,
01333                                 const uint8_t *buf, int buf_size)
01334 {
01335     Mpeg1Context *s1 = avctx->priv_data;
01336     MpegEncContext *s = &s1->mpeg_enc_ctx;
01337     int ref, f_code, vbv_delay;
01338 
01339     if(mpeg_decode_postinit(s->avctx) < 0)
01340        return -2;
01341 
01342     init_get_bits(&s->gb, buf, buf_size*8);
01343 
01344     ref = get_bits(&s->gb, 10); /* temporal ref */
01345     s->pict_type = get_bits(&s->gb, 3);
01346     if(s->pict_type == 0 || s->pict_type > 3)
01347         return -1;
01348 
01349     vbv_delay= get_bits(&s->gb, 16);
01350     if (s->pict_type == FF_P_TYPE || s->pict_type == FF_B_TYPE) {
01351         s->full_pel[0] = get_bits1(&s->gb);
01352         f_code = get_bits(&s->gb, 3);
01353         if (f_code == 0 && avctx->error_recognition >= FF_ER_COMPLIANT)
01354             return -1;
01355         s->mpeg_f_code[0][0] = f_code;
01356         s->mpeg_f_code[0][1] = f_code;
01357     }
01358     if (s->pict_type == FF_B_TYPE) {
01359         s->full_pel[1] = get_bits1(&s->gb);
01360         f_code = get_bits(&s->gb, 3);
01361         if (f_code == 0 && avctx->error_recognition >= FF_ER_COMPLIANT)
01362             return -1;
01363         s->mpeg_f_code[1][0] = f_code;
01364         s->mpeg_f_code[1][1] = f_code;
01365     }
01366     s->current_picture.pict_type= s->pict_type;
01367     s->current_picture.key_frame= s->pict_type == FF_I_TYPE;
01368 
01369     if(avctx->debug & FF_DEBUG_PICT_INFO)
01370         av_log(avctx, AV_LOG_DEBUG, "vbv_delay %d, ref %d type:%d\n", vbv_delay, ref, s->pict_type);
01371 
01372     s->y_dc_scale = 8;
01373     s->c_dc_scale = 8;
01374     s->first_slice = 1;
01375     return 0;
01376 }
01377 
01378 static void mpeg_decode_sequence_extension(Mpeg1Context *s1)
01379 {
01380     MpegEncContext *s= &s1->mpeg_enc_ctx;
01381     int horiz_size_ext, vert_size_ext;
01382     int bit_rate_ext;
01383 
01384     skip_bits(&s->gb, 1); /* profile and level esc*/
01385     s->avctx->profile= get_bits(&s->gb, 3);
01386     s->avctx->level= get_bits(&s->gb, 4);
01387     s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */
01388     s->chroma_format = get_bits(&s->gb, 2); /* chroma_format 1=420, 2=422, 3=444 */
01389     horiz_size_ext = get_bits(&s->gb, 2);
01390     vert_size_ext = get_bits(&s->gb, 2);
01391     s->width |= (horiz_size_ext << 12);
01392     s->height |= (vert_size_ext << 12);
01393     bit_rate_ext = get_bits(&s->gb, 12);  /* XXX: handle it */
01394     s->bit_rate += (bit_rate_ext << 18) * 400;
01395     skip_bits1(&s->gb); /* marker */
01396     s->avctx->rc_buffer_size += get_bits(&s->gb, 8)*1024*16<<10;
01397 
01398     s->low_delay = get_bits1(&s->gb);
01399     if(s->flags & CODEC_FLAG_LOW_DELAY) s->low_delay=1;
01400 
01401     s1->frame_rate_ext.num = get_bits(&s->gb, 2)+1;
01402     s1->frame_rate_ext.den = get_bits(&s->gb, 5)+1;
01403 
01404     dprintf(s->avctx, "sequence extension\n");
01405     s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG2VIDEO;
01406     s->avctx->sub_id = 2; /* indicates MPEG-2 found */
01407 
01408     if(s->avctx->debug & FF_DEBUG_PICT_INFO)
01409         av_log(s->avctx, AV_LOG_DEBUG, "profile: %d, level: %d vbv buffer: %d, bitrate:%d\n",
01410                s->avctx->profile, s->avctx->level, s->avctx->rc_buffer_size, s->bit_rate);
01411 
01412 }
01413 
01414 static void mpeg_decode_sequence_display_extension(Mpeg1Context *s1)
01415 {
01416     MpegEncContext *s= &s1->mpeg_enc_ctx;
01417     int color_description, w, h;
01418 
01419     skip_bits(&s->gb, 3); /* video format */
01420     color_description= get_bits1(&s->gb);
01421     if(color_description){
01422         skip_bits(&s->gb, 8); /* color primaries */
01423         skip_bits(&s->gb, 8); /* transfer_characteristics */
01424         skip_bits(&s->gb, 8); /* matrix_coefficients */
01425     }
01426     w= get_bits(&s->gb, 14);
01427     skip_bits(&s->gb, 1); //marker
01428     h= get_bits(&s->gb, 14);
01429     skip_bits(&s->gb, 1); //marker
01430 
01431     s1->pan_scan.width= 16*w;
01432     s1->pan_scan.height=16*h;
01433 
01434     if(s->avctx->debug & FF_DEBUG_PICT_INFO)
01435         av_log(s->avctx, AV_LOG_DEBUG, "sde w:%d, h:%d\n", w, h);
01436 }
01437 
01438 static void mpeg_decode_picture_display_extension(Mpeg1Context *s1)
01439 {
01440     MpegEncContext *s= &s1->mpeg_enc_ctx;
01441     int i,nofco;
01442 
01443     nofco = 1;
01444     if(s->progressive_sequence){
01445         if(s->repeat_first_field){
01446             nofco++;
01447             if(s->top_field_first)
01448                 nofco++;
01449         }
01450     }else{
01451         if(s->picture_structure == PICT_FRAME){
01452             nofco++;
01453             if(s->repeat_first_field)
01454                 nofco++;
01455         }
01456     }
01457     for(i=0; i<nofco; i++){
01458         s1->pan_scan.position[i][0]= get_sbits(&s->gb, 16);
01459         skip_bits(&s->gb, 1); //marker
01460         s1->pan_scan.position[i][1]= get_sbits(&s->gb, 16);
01461         skip_bits(&s->gb, 1); //marker
01462     }
01463 
01464     if(s->avctx->debug & FF_DEBUG_PICT_INFO)
01465         av_log(s->avctx, AV_LOG_DEBUG, "pde (%d,%d) (%d,%d) (%d,%d)\n",
01466             s1->pan_scan.position[0][0], s1->pan_scan.position[0][1],
01467             s1->pan_scan.position[1][0], s1->pan_scan.position[1][1],
01468             s1->pan_scan.position[2][0], s1->pan_scan.position[2][1]
01469         );
01470 }
01471 
01472 static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
01473 {
01474     int i, v, j;
01475 
01476     dprintf(s->avctx, "matrix extension\n");
01477 
01478     if (get_bits1(&s->gb)) {
01479         for(i=0;i<64;i++) {
01480             v = get_bits(&s->gb, 8);
01481             j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
01482             s->intra_matrix[j] = v;
01483             s->chroma_intra_matrix[j] = v;
01484         }
01485     }
01486     if (get_bits1(&s->gb)) {
01487         for(i=0;i<64;i++) {
01488             v = get_bits(&s->gb, 8);
01489             j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
01490             s->inter_matrix[j] = v;
01491             s->chroma_inter_matrix[j] = v;
01492         }
01493     }
01494     if (get_bits1(&s->gb)) {
01495         for(i=0;i<64;i++) {
01496             v = get_bits(&s->gb, 8);
01497             j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
01498             s->chroma_intra_matrix[j] = v;
01499         }
01500     }
01501     if (get_bits1(&s->gb)) {
01502         for(i=0;i<64;i++) {
01503             v = get_bits(&s->gb, 8);
01504             j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
01505             s->chroma_inter_matrix[j] = v;
01506         }
01507     }
01508 }
01509 
01510 static void mpeg_decode_picture_coding_extension(Mpeg1Context *s1)
01511 {
01512     MpegEncContext *s= &s1->mpeg_enc_ctx;
01513 
01514     s->full_pel[0] = s->full_pel[1] = 0;
01515     s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
01516     s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
01517     s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
01518     s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
01519     if(!s->pict_type && s1->mpeg_enc_ctx_allocated){
01520         av_log(s->avctx, AV_LOG_ERROR, "Missing picture start code, guessing missing values\n");
01521         if(s->mpeg_f_code[1][0] == 15 && s->mpeg_f_code[1][1]==15){
01522             if(s->mpeg_f_code[0][0] == 15 && s->mpeg_f_code[0][1] == 15)
01523                 s->pict_type= FF_I_TYPE;
01524             else
01525                 s->pict_type= FF_P_TYPE;
01526         }else
01527             s->pict_type= FF_B_TYPE;
01528         s->current_picture.pict_type= s->pict_type;
01529         s->current_picture.key_frame= s->pict_type == FF_I_TYPE;
01530         s->first_slice= 1;
01531     }
01532     s->intra_dc_precision = get_bits(&s->gb, 2);
01533     s->picture_structure = get_bits(&s->gb, 2);
01534     s->top_field_first = get_bits1(&s->gb);
01535     s->frame_pred_frame_dct = get_bits1(&s->gb);
01536     s->concealment_motion_vectors = get_bits1(&s->gb);
01537     s->q_scale_type = get_bits1(&s->gb);
01538     s->intra_vlc_format = get_bits1(&s->gb);
01539     s->alternate_scan = get_bits1(&s->gb);
01540     s->repeat_first_field = get_bits1(&s->gb);
01541     s->chroma_420_type = get_bits1(&s->gb);
01542     s->progressive_frame = get_bits1(&s->gb);
01543 
01544     if(s->picture_structure == PICT_FRAME){
01545         s->first_field=0;
01546         s->v_edge_pos= 16*s->mb_height;
01547     }else{
01548         s->first_field ^= 1;
01549         s->v_edge_pos=  8*s->mb_height;
01550         memset(s->mbskip_table, 0, s->mb_stride*s->mb_height);
01551     }
01552 
01553     if(s->alternate_scan){
01554         ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable  , ff_alternate_vertical_scan);
01555         ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable  , ff_alternate_vertical_scan);
01556     }else{
01557         ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable  , ff_zigzag_direct);
01558         ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable  , ff_zigzag_direct);
01559     }
01560 
01561     /* composite display not parsed */
01562     dprintf(s->avctx, "intra_dc_precision=%d\n", s->intra_dc_precision);
01563     dprintf(s->avctx, "picture_structure=%d\n", s->picture_structure);
01564     dprintf(s->avctx, "top field first=%d\n", s->top_field_first);
01565     dprintf(s->avctx, "repeat first field=%d\n", s->repeat_first_field);
01566     dprintf(s->avctx, "conceal=%d\n", s->concealment_motion_vectors);
01567     dprintf(s->avctx, "intra_vlc_format=%d\n", s->intra_vlc_format);
01568     dprintf(s->avctx, "alternate_scan=%d\n", s->alternate_scan);
01569     dprintf(s->avctx, "frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
01570     dprintf(s->avctx, "progressive_frame=%d\n", s->progressive_frame);
01571 }
01572 
01573 static void mpeg_decode_extension(AVCodecContext *avctx,
01574                                   const uint8_t *buf, int buf_size)
01575 {
01576     Mpeg1Context *s1 = avctx->priv_data;
01577     MpegEncContext *s = &s1->mpeg_enc_ctx;
01578     int ext_type;
01579 
01580     init_get_bits(&s->gb, buf, buf_size*8);
01581 
01582     ext_type = get_bits(&s->gb, 4);
01583     switch(ext_type) {
01584     case 0x1:
01585         mpeg_decode_sequence_extension(s1);
01586         break;
01587     case 0x2:
01588         mpeg_decode_sequence_display_extension(s1);
01589         break;
01590     case 0x3:
01591         mpeg_decode_quant_matrix_extension(s);
01592         break;
01593     case 0x7:
01594         mpeg_decode_picture_display_extension(s1);
01595         break;
01596     case 0x8:
01597         mpeg_decode_picture_coding_extension(s1);
01598         break;
01599     }
01600 }
01601 
01602 static void exchange_uv(MpegEncContext *s){
01603     DCTELEM (*tmp)[64];
01604 
01605     tmp           = s->pblocks[4];
01606     s->pblocks[4] = s->pblocks[5];
01607     s->pblocks[5] = tmp;
01608 }
01609 
01610 static int mpeg_field_start(MpegEncContext *s, const uint8_t *buf, int buf_size){
01611     AVCodecContext *avctx= s->avctx;
01612     Mpeg1Context *s1 = (Mpeg1Context*)s;
01613 
01614     /* start frame decoding */
01615     if(s->first_field || s->picture_structure==PICT_FRAME){
01616         if(MPV_frame_start(s, avctx) < 0)
01617             return -1;
01618 
01619         ff_er_frame_start(s);
01620 
01621         /* first check if we must repeat the frame */
01622         s->current_picture_ptr->repeat_pict = 0;
01623         if (s->repeat_first_field) {
01624             if (s->progressive_sequence) {
01625                 if (s->top_field_first)
01626                     s->current_picture_ptr->repeat_pict = 4;
01627                 else
01628                     s->current_picture_ptr->repeat_pict = 2;
01629             } else if (s->progressive_frame) {
01630                 s->current_picture_ptr->repeat_pict = 1;
01631             }
01632         }
01633 
01634         *s->current_picture_ptr->pan_scan= s1->pan_scan;
01635     }else{ //second field
01636             int i;
01637 
01638             if(!s->current_picture_ptr){
01639                 av_log(s->avctx, AV_LOG_ERROR, "first field missing\n");
01640                 return -1;
01641             }
01642 
01643             for(i=0; i<4; i++){
01644                 s->current_picture.data[i] = s->current_picture_ptr->data[i];
01645                 if(s->picture_structure == PICT_BOTTOM_FIELD){
01646                     s->current_picture.data[i] += s->current_picture_ptr->linesize[i];
01647                 }
01648             }
01649     }
01650 
01651     if (avctx->hwaccel) {
01652         if (avctx->hwaccel->start_frame(avctx, buf, buf_size) < 0)
01653             return -1;
01654     }
01655 
01656 // MPV_frame_start will call this function too,
01657 // but we need to call it on every field
01658     if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
01659         if(ff_xvmc_field_start(s,avctx) < 0)
01660             return -1;
01661 
01662     return 0;
01663 }
01664 
01665 #define DECODE_SLICE_ERROR -1
01666 #define DECODE_SLICE_OK 0
01667 
01673 static int mpeg_decode_slice(Mpeg1Context *s1, int mb_y,
01674                              const uint8_t **buf, int buf_size)
01675 {
01676     MpegEncContext *s = &s1->mpeg_enc_ctx;
01677     AVCodecContext *avctx= s->avctx;
01678     const int field_pic= s->picture_structure != PICT_FRAME;
01679     const int lowres= s->avctx->lowres;
01680 
01681     s->resync_mb_x=
01682     s->resync_mb_y= -1;
01683 
01684     if (mb_y<<field_pic >= s->mb_height){
01685         av_log(s->avctx, AV_LOG_ERROR, "slice below image (%d >= %d)\n", mb_y, s->mb_height);
01686         return -1;
01687     }
01688 
01689     init_get_bits(&s->gb, *buf, buf_size*8);
01690 
01691     ff_mpeg1_clean_buffers(s);
01692     s->interlaced_dct = 0;
01693 
01694     s->qscale = get_qscale(s);
01695 
01696     if(s->qscale == 0){
01697         av_log(s->avctx, AV_LOG_ERROR, "qscale == 0\n");
01698         return -1;
01699     }
01700 
01701     /* extra slice info */
01702     while (get_bits1(&s->gb) != 0) {
01703         skip_bits(&s->gb, 8);
01704     }
01705 
01706     s->mb_x=0;
01707 
01708     if (avctx->hwaccel) {
01709         const uint8_t *buf_end, *buf_start = *buf - 4; /* include start_code */
01710         int start_code = -1;
01711         buf_end = ff_find_start_code(buf_start + 2, *buf + buf_size, &start_code);
01712         if (buf_end < *buf + buf_size)
01713             buf_end -= 4;
01714         s->mb_y = mb_y;
01715         if (avctx->hwaccel->decode_slice(avctx, buf_start, buf_end - buf_start) < 0)
01716             return DECODE_SLICE_ERROR;
01717         *buf = buf_end;
01718         return DECODE_SLICE_OK;
01719     }
01720 
01721     for(;;) {
01722         int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
01723         if (code < 0){
01724             av_log(s->avctx, AV_LOG_ERROR, "first mb_incr damaged\n");
01725             return -1;
01726         }
01727         if (code >= 33) {
01728             if (code == 33) {
01729                 s->mb_x += 33;
01730             }
01731             /* otherwise, stuffing, nothing to do */
01732         } else {
01733             s->mb_x += code;
01734             break;
01735         }
01736     }
01737     if(s->mb_x >= (unsigned)s->mb_width){
01738         av_log(s->avctx, AV_LOG_ERROR, "initial skip overflow\n");
01739         return -1;
01740     }
01741 
01742     s->resync_mb_x= s->mb_x;
01743     s->resync_mb_y= s->mb_y= mb_y;
01744     s->mb_skip_run= 0;
01745     ff_init_block_index(s);
01746 
01747     if (s->mb_y==0 && s->mb_x==0 && (s->first_field || s->picture_structure==PICT_FRAME)) {
01748         if(s->avctx->debug&FF_DEBUG_PICT_INFO){
01749              av_log(s->avctx, AV_LOG_DEBUG, "qp:%d fc:%2d%2d%2d%2d %s %s %s %s %s dc:%d pstruct:%d fdct:%d cmv:%d qtype:%d ivlc:%d rff:%d %s\n",
01750                  s->qscale, s->mpeg_f_code[0][0],s->mpeg_f_code[0][1],s->mpeg_f_code[1][0],s->mpeg_f_code[1][1],
01751                  s->pict_type == FF_I_TYPE ? "I" : (s->pict_type == FF_P_TYPE ? "P" : (s->pict_type == FF_B_TYPE ? "B" : "S")),
01752                  s->progressive_sequence ? "ps" :"", s->progressive_frame ? "pf" : "", s->alternate_scan ? "alt" :"", s->top_field_first ? "top" :"",
01753                  s->intra_dc_precision, s->picture_structure, s->frame_pred_frame_dct, s->concealment_motion_vectors,
01754                  s->q_scale_type, s->intra_vlc_format, s->repeat_first_field, s->chroma_420_type ? "420" :"");
01755         }
01756     }
01757 
01758     for(;;) {
01759         //If 1, we memcpy blocks in xvmcvideo.
01760         if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1)
01761             ff_xvmc_init_block(s);//set s->block
01762 
01763         if(mpeg_decode_mb(s, s->block) < 0)
01764             return -1;
01765 
01766         if(s->current_picture.motion_val[0] && !s->encoding){ //note motion_val is normally NULL unless we want to extract the MVs
01767             const int wrap = field_pic ? 2*s->b8_stride : s->b8_stride;
01768             int xy = s->mb_x*2 + s->mb_y*2*wrap;
01769             int motion_x, motion_y, dir, i;
01770             if(field_pic && !s->first_field)
01771                 xy += wrap/2;
01772 
01773             for(i=0; i<2; i++){
01774                 for(dir=0; dir<2; dir++){
01775                     if (s->mb_intra || (dir==1 && s->pict_type != FF_B_TYPE)) {
01776                         motion_x = motion_y = 0;
01777                     }else if (s->mv_type == MV_TYPE_16X16 || (s->mv_type == MV_TYPE_FIELD && field_pic)){
01778                         motion_x = s->mv[dir][0][0];
01779                         motion_y = s->mv[dir][0][1];
01780                     } else /*if ((s->mv_type == MV_TYPE_FIELD) || (s->mv_type == MV_TYPE_16X8))*/ {
01781                         motion_x = s->mv[dir][i][0];
01782                         motion_y = s->mv[dir][i][1];
01783                     }
01784 
01785                     s->current_picture.motion_val[dir][xy    ][0] = motion_x;
01786                     s->current_picture.motion_val[dir][xy    ][1] = motion_y;
01787                     s->current_picture.motion_val[dir][xy + 1][0] = motion_x;
01788                     s->current_picture.motion_val[dir][xy + 1][1] = motion_y;
01789                     s->current_picture.ref_index [dir][xy    ]=
01790                     s->current_picture.ref_index [dir][xy + 1]= s->field_select[dir][i];
01791                     assert(s->field_select[dir][i]==0 || s->field_select[dir][i]==1);
01792                 }
01793                 xy += wrap;
01794             }
01795         }
01796 
01797         s->dest[0] += 16 >> lowres;
01798         s->dest[1] +=(16 >> lowres) >> s->chroma_x_shift;
01799         s->dest[2] +=(16 >> lowres) >> s->chroma_x_shift;
01800 
01801         MPV_decode_mb(s, s->block);
01802 
01803         if (++s->mb_x >= s->mb_width) {
01804             const int mb_size= 16>>s->avctx->lowres;
01805 
01806             ff_draw_horiz_band(s, mb_size*s->mb_y, mb_size);
01807 
01808             s->mb_x = 0;
01809             s->mb_y++;
01810 
01811             if(s->mb_y<<field_pic >= s->mb_height){
01812                 int left= s->gb.size_in_bits - get_bits_count(&s->gb);
01813                 int is_d10= s->chroma_format==2 && s->pict_type==FF_I_TYPE && avctx->profile==0 && avctx->level==5
01814                             && s->intra_dc_precision == 2 && s->q_scale_type == 1 && s->alternate_scan == 0
01815                             && s->progressive_frame == 0 /* vbv_delay == 0xBBB || 0xE10*/;
01816 
01817                 if(left < 0 || (left && show_bits(&s->gb, FFMIN(left, 23)) && !is_d10)
01818                    || (avctx->error_recognition >= FF_ER_AGGRESSIVE && left>8)){
01819                     av_log(avctx, AV_LOG_ERROR, "end mismatch left=%d %0X\n", left, show_bits(&s->gb, FFMIN(left, 23)));
01820                     return -1;
01821                 }else
01822                     goto eos;
01823             }
01824 
01825             ff_init_block_index(s);
01826         }
01827 
01828         /* skip mb handling */
01829         if (s->mb_skip_run == -1) {
01830             /* read increment again */
01831             s->mb_skip_run = 0;
01832             for(;;) {
01833                 int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
01834                 if (code < 0){
01835                     av_log(s->avctx, AV_LOG_ERROR, "mb incr damaged\n");
01836                     return -1;
01837                 }
01838                 if (code >= 33) {
01839                     if (code == 33) {
01840                         s->mb_skip_run += 33;
01841                     }else if(code == 35){
01842                         if(s->mb_skip_run != 0 || show_bits(&s->gb, 15) != 0){
01843                             av_log(s->avctx, AV_LOG_ERROR, "slice mismatch\n");
01844                             return -1;
01845                         }
01846                         goto eos; /* end of slice */
01847                     }
01848                     /* otherwise, stuffing, nothing to do */
01849                 } else {
01850                     s->mb_skip_run += code;
01851                     break;
01852                 }
01853             }
01854             if(s->mb_skip_run){
01855                 int i;
01856                 if(s->pict_type == FF_I_TYPE){
01857                     av_log(s->avctx, AV_LOG_ERROR, "skipped MB in I frame at %d %d\n", s->mb_x, s->mb_y);
01858                     return -1;
01859                 }
01860 
01861                 /* skip mb */
01862                 s->mb_intra = 0;
01863                 for(i=0;i<12;i++)
01864                     s->block_last_index[i] = -1;
01865                 if(s->picture_structure == PICT_FRAME)
01866                     s->mv_type = MV_TYPE_16X16;
01867                 else
01868                     s->mv_type = MV_TYPE_FIELD;
01869                 if (s->pict_type == FF_P_TYPE) {
01870                     /* if P type, zero motion vector is implied */
01871                     s->mv_dir = MV_DIR_FORWARD;
01872                     s->mv[0][0][0] = s->mv[0][0][1] = 0;
01873                     s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
01874                     s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
01875                     s->field_select[0][0]= s->picture_structure - 1;
01876                 } else {
01877                     /* if B type, reuse previous vectors and directions */
01878                     s->mv[0][0][0] = s->last_mv[0][0][0];
01879                     s->mv[0][0][1] = s->last_mv[0][0][1];
01880                     s->mv[1][0][0] = s->last_mv[1][0][0];
01881                     s->mv[1][0][1] = s->last_mv[1][0][1];
01882                 }
01883             }
01884         }
01885     }
01886 eos: // end of slice
01887     *buf += (get_bits_count(&s->gb)-1)/8;
01888 //printf("y %d %d %d %d\n", s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y);
01889     return 0;
01890 }
01891 
01892 static int slice_decode_thread(AVCodecContext *c, void *arg){
01893     MpegEncContext *s= *(void**)arg;
01894     const uint8_t *buf= s->gb.buffer;
01895     int mb_y= s->start_mb_y;
01896 
01897     s->error_count= 3*(s->end_mb_y - s->start_mb_y)*s->mb_width;
01898 
01899     for(;;){
01900         uint32_t start_code;
01901         int ret;
01902 
01903         ret= mpeg_decode_slice((Mpeg1Context*)s, mb_y, &buf, s->gb.buffer_end - buf);
01904         emms_c();
01905 //av_log(c, AV_LOG_DEBUG, "ret:%d resync:%d/%d mb:%d/%d ts:%d/%d ec:%d\n",
01906 //ret, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, s->start_mb_y, s->end_mb_y, s->error_count);
01907         if(ret < 0){
01908             if(s->resync_mb_x>=0 && s->resync_mb_y>=0)
01909                 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, AC_ERROR|DC_ERROR|MV_ERROR);
01910         }else{
01911             ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, AC_END|DC_END|MV_END);
01912         }
01913 
01914         if(s->mb_y == s->end_mb_y)
01915             return 0;
01916 
01917         start_code= -1;
01918         buf = ff_find_start_code(buf, s->gb.buffer_end, &start_code);
01919         mb_y= start_code - SLICE_MIN_START_CODE;
01920         if(mb_y < 0 || mb_y >= s->end_mb_y)
01921             return -1;
01922     }
01923 
01924     return 0; //not reached
01925 }
01926 
01931 static int slice_end(AVCodecContext *avctx, AVFrame *pict)
01932 {
01933     Mpeg1Context *s1 = avctx->priv_data;
01934     MpegEncContext *s = &s1->mpeg_enc_ctx;
01935 
01936     if (!s1->mpeg_enc_ctx_allocated || !s->current_picture_ptr)
01937         return 0;
01938 
01939     if (s->avctx->hwaccel) {
01940         if (s->avctx->hwaccel->end_frame(s->avctx) < 0)
01941             av_log(avctx, AV_LOG_ERROR, "hardware accelerator failed to decode picture\n");
01942     }
01943 
01944     if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
01945         ff_xvmc_field_end(s);
01946 
01947     /* end of slice reached */
01948     if (/*s->mb_y<<field_pic == s->mb_height &&*/ !s->first_field) {
01949         /* end of image */
01950 
01951         s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_MPEG2;
01952 
01953         ff_er_frame_end(s);
01954 
01955         MPV_frame_end(s);
01956 
01957         if (s->pict_type == FF_B_TYPE || s->low_delay) {
01958             *pict= *(AVFrame*)s->current_picture_ptr;
01959             ff_print_debug_info(s, pict);
01960         } else {
01961             s->picture_number++;
01962             /* latency of 1 frame for I- and P-frames */
01963             /* XXX: use another variable than picture_number */
01964             if (s->last_picture_ptr != NULL) {
01965                 *pict= *(AVFrame*)s->last_picture_ptr;
01966                  ff_print_debug_info(s, pict);
01967             }
01968         }
01969 
01970         return 1;
01971     } else {
01972         return 0;
01973     }
01974 }
01975 
01976 static int mpeg1_decode_sequence(AVCodecContext *avctx,
01977                                  const uint8_t *buf, int buf_size)
01978 {
01979     Mpeg1Context *s1 = avctx->priv_data;
01980     MpegEncContext *s = &s1->mpeg_enc_ctx;
01981     int width,height;
01982     int i, v, j;
01983 
01984     init_get_bits(&s->gb, buf, buf_size*8);
01985 
01986     width = get_bits(&s->gb, 12);
01987     height = get_bits(&s->gb, 12);
01988     if (width <= 0 || height <= 0)
01989         return -1;
01990     s->aspect_ratio_info= get_bits(&s->gb, 4);
01991     if (s->aspect_ratio_info == 0) {
01992         av_log(avctx, AV_LOG_ERROR, "aspect ratio has forbidden 0 value\n");
01993         if (avctx->error_recognition >= FF_ER_COMPLIANT)
01994             return -1;
01995     }
01996     s->frame_rate_index = get_bits(&s->gb, 4);
01997     if (s->frame_rate_index == 0 || s->frame_rate_index > 13)
01998         return -1;
01999     s->bit_rate = get_bits(&s->gb, 18) * 400;
02000     if (get_bits1(&s->gb) == 0) /* marker */
02001         return -1;
02002     s->width = width;
02003     s->height = height;
02004 
02005     s->avctx->rc_buffer_size= get_bits(&s->gb, 10) * 1024*16;
02006     skip_bits(&s->gb, 1);
02007 
02008     /* get matrix */
02009     if (get_bits1(&s->gb)) {
02010         for(i=0;i<64;i++) {
02011             v = get_bits(&s->gb, 8);
02012             if(v==0){
02013                 av_log(s->avctx, AV_LOG_ERROR, "intra matrix damaged\n");
02014                 return -1;
02015             }
02016             j = s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
02017             s->intra_matrix[j] = v;
02018             s->chroma_intra_matrix[j] = v;
02019         }
02020 #ifdef DEBUG
02021         dprintf(s->avctx, "intra matrix present\n");
02022         for(i=0;i<64;i++)
02023             dprintf(s->avctx, " %d", s->intra_matrix[s->dsp.idct_permutation[i]]);
02024         dprintf(s->avctx, "\n");
02025 #endif
02026     } else {
02027         for(i=0;i<64;i++) {
02028             j = s->dsp.idct_permutation[i];
02029             v = ff_mpeg1_default_intra_matrix[i];
02030             s->intra_matrix[j] = v;
02031             s->chroma_intra_matrix[j] = v;
02032         }
02033     }
02034     if (get_bits1(&s->gb)) {
02035         for(i=0;i<64;i++) {
02036             v = get_bits(&s->gb, 8);
02037             if(v==0){
02038                 av_log(s->avctx, AV_LOG_ERROR, "inter matrix damaged\n");
02039                 return -1;
02040             }
02041             j = s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
02042             s->inter_matrix[j] = v;
02043             s->chroma_inter_matrix[j] = v;
02044         }
02045 #ifdef DEBUG
02046         dprintf(s->avctx, "non-intra matrix present\n");
02047         for(i=0;i<64;i++)
02048             dprintf(s->avctx, " %d", s->inter_matrix[s->dsp.idct_permutation[i]]);
02049         dprintf(s->avctx, "\n");
02050 #endif
02051     } else {
02052         for(i=0;i<64;i++) {
02053             int j= s->dsp.idct_permutation[i];
02054             v = ff_mpeg1_default_non_intra_matrix[i];
02055             s->inter_matrix[j] = v;
02056             s->chroma_inter_matrix[j] = v;
02057         }
02058     }
02059 
02060     if(show_bits(&s->gb, 23) != 0){
02061         av_log(s->avctx, AV_LOG_ERROR, "sequence header damaged\n");
02062         return -1;
02063     }
02064 
02065     /* we set MPEG-2 parameters so that it emulates MPEG-1 */
02066     s->progressive_sequence = 1;
02067     s->progressive_frame = 1;
02068     s->picture_structure = PICT_FRAME;
02069     s->frame_pred_frame_dct = 1;
02070     s->chroma_format = 1;
02071     s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG1VIDEO;
02072     avctx->sub_id = 1; /* indicates MPEG-1 */
02073     s->out_format = FMT_MPEG1;
02074     s->swap_uv = 0;//AFAIK VCR2 does not have SEQ_HEADER
02075     if(s->flags & CODEC_FLAG_LOW_DELAY) s->low_delay=1;
02076 
02077     if(s->avctx->debug & FF_DEBUG_PICT_INFO)
02078         av_log(s->avctx, AV_LOG_DEBUG, "vbv buffer: %d, bitrate:%d\n",
02079                s->avctx->rc_buffer_size, s->bit_rate);
02080 
02081     return 0;
02082 }
02083 
02084 static int vcr2_init_sequence(AVCodecContext *avctx)
02085 {
02086     Mpeg1Context *s1 = avctx->priv_data;
02087     MpegEncContext *s = &s1->mpeg_enc_ctx;
02088     int i, v;
02089 
02090     /* start new MPEG-1 context decoding */
02091     s->out_format = FMT_MPEG1;
02092     if (s1->mpeg_enc_ctx_allocated) {
02093         MPV_common_end(s);
02094     }
02095     s->width  = avctx->coded_width;
02096     s->height = avctx->coded_height;
02097     avctx->has_b_frames= 0; //true?
02098     s->low_delay= 1;
02099 
02100     avctx->pix_fmt = mpeg_get_pixelformat(avctx);
02101     avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
02102 
02103     if( avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT || avctx->hwaccel ||
02104         s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU )
02105         if( avctx->idct_algo == FF_IDCT_AUTO )
02106             avctx->idct_algo = FF_IDCT_SIMPLE;
02107 
02108     if (MPV_common_init(s) < 0)
02109         return -1;
02110     exchange_uv(s);//common init reset pblocks, so we swap them here
02111     s->swap_uv = 1;// in case of xvmc we need to swap uv for each MB
02112     s1->mpeg_enc_ctx_allocated = 1;
02113 
02114     for(i=0;i<64;i++) {
02115         int j= s->dsp.idct_permutation[i];
02116         v = ff_mpeg1_default_intra_matrix[i];
02117         s->intra_matrix[j] = v;
02118         s->chroma_intra_matrix[j] = v;
02119 
02120         v = ff_mpeg1_default_non_intra_matrix[i];
02121         s->inter_matrix[j] = v;
02122         s->chroma_inter_matrix[j] = v;
02123     }
02124 
02125     s->progressive_sequence = 1;
02126     s->progressive_frame = 1;
02127     s->picture_structure = PICT_FRAME;
02128     s->frame_pred_frame_dct = 1;
02129     s->chroma_format = 1;
02130     s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG2VIDEO;
02131     avctx->sub_id = 2; /* indicates MPEG-2 */
02132     return 0;
02133 }
02134 
02135 
02136 static void mpeg_decode_user_data(AVCodecContext *avctx,
02137                                   const uint8_t *buf, int buf_size)
02138 {
02139     const uint8_t *p;
02140     int len, flags;
02141     p = buf;
02142     len = buf_size;
02143 
02144     /* we parse the DTG active format information */
02145     if (len >= 5 &&
02146         p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') {
02147         flags = p[4];
02148         p += 5;
02149         len -= 5;
02150         if (flags & 0x80) {
02151             /* skip event id */
02152             if (len < 2)
02153                 return;
02154             p += 2;
02155             len -= 2;
02156         }
02157         if (flags & 0x40) {
02158             if (len < 1)
02159                 return;
02160             avctx->dtg_active_format = p[0] & 0x0f;
02161         }
02162     }
02163 }
02164 
02165 static void mpeg_decode_gop(AVCodecContext *avctx,
02166                             const uint8_t *buf, int buf_size){
02167     Mpeg1Context *s1 = avctx->priv_data;
02168     MpegEncContext *s = &s1->mpeg_enc_ctx;
02169 
02170     int drop_frame_flag;
02171     int time_code_hours, time_code_minutes;
02172     int time_code_seconds, time_code_pictures;
02173     int closed_gop, broken_link;
02174 
02175     init_get_bits(&s->gb, buf, buf_size*8);
02176 
02177     drop_frame_flag = get_bits1(&s->gb);
02178 
02179     time_code_hours=get_bits(&s->gb,5);
02180     time_code_minutes = get_bits(&s->gb,6);
02181     skip_bits1(&s->gb);//marker bit
02182     time_code_seconds = get_bits(&s->gb,6);
02183     time_code_pictures = get_bits(&s->gb,6);
02184 
02185     closed_gop  = get_bits1(&s->gb);
02186     /*broken_link indicate that after editing the
02187       reference frames of the first B-Frames after GOP I-Frame
02188       are missing (open gop)*/
02189     broken_link = get_bits1(&s->gb);
02190 
02191     if(s->avctx->debug & FF_DEBUG_PICT_INFO)
02192         av_log(s->avctx, AV_LOG_DEBUG, "GOP (%2d:%02d:%02d.[%02d]) closed_gop=%d broken_link=%d\n",
02193             time_code_hours, time_code_minutes, time_code_seconds,
02194             time_code_pictures, closed_gop, broken_link);
02195 }
02200 int ff_mpeg1_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size)
02201 {
02202     int i;
02203     uint32_t state= pc->state;
02204 
02205     /* EOF considered as end of frame */
02206     if (buf_size == 0)
02207         return 0;
02208 
02209 /*
02210  0  frame start         -> 1/4
02211  1  first_SEQEXT        -> 0/2
02212  2  first field start   -> 3/0
02213  3  second_SEQEXT       -> 2/0
02214  4  searching end
02215 */
02216 
02217     for(i=0; i<buf_size; i++){
02218         assert(pc->frame_start_found>=0 && pc->frame_start_found<=4);
02219         if(pc->frame_start_found&1){
02220             if(state == EXT_START_CODE && (buf[i]&0xF0) != 0x80)
02221                 pc->frame_start_found--;
02222             else if(state == EXT_START_CODE+2){
02223                 if((buf[i]&3) == 3) pc->frame_start_found= 0;
02224                 else                pc->frame_start_found= (pc->frame_start_found+1)&3;
02225             }
02226             state++;
02227         }else{
02228             i= ff_find_start_code(buf+i, buf+buf_size, &state) - buf - 1;
02229             if(pc->frame_start_found==0 && state >= SLICE_MIN_START_CODE && state <= SLICE_MAX_START_CODE){
02230                 i++;
02231                 pc->frame_start_found=4;
02232             }
02233             if(state == SEQ_END_CODE){
02234                 pc->state=-1;
02235                 return i+1;
02236             }
02237             if(pc->frame_start_found==2 && state == SEQ_START_CODE)
02238                 pc->frame_start_found= 0;
02239             if(pc->frame_start_found<4 && state == EXT_START_CODE)
02240                 pc->frame_start_found++;
02241             if(pc->frame_start_found == 4 && (state&0xFFFFFF00) == 0x100){
02242                 if(state < SLICE_MIN_START_CODE || state > SLICE_MAX_START_CODE){
02243                     pc->frame_start_found=0;
02244                     pc->state=-1;
02245                     return i-3;
02246                 }
02247             }
02248         }
02249     }
02250     pc->state= state;
02251     return END_NOT_FOUND;
02252 }
02253 
02254 static int decode_chunks(AVCodecContext *avctx,
02255                              AVFrame *picture, int *data_size,
02256                              const uint8_t *buf, int buf_size);
02257 
02258 /* handle buffering and image synchronisation */
02259 static int mpeg_decode_frame(AVCodecContext *avctx,
02260                              void *data, int *data_size,
02261                              const uint8_t *buf, int buf_size)
02262 {
02263     Mpeg1Context *s = avctx->priv_data;
02264     AVFrame *picture = data;
02265     MpegEncContext *s2 = &s->mpeg_enc_ctx;
02266     dprintf(avctx, "fill_buffer\n");
02267 
02268     if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == SEQ_END_CODE)) {
02269         /* special case for last picture */
02270         if (s2->low_delay==0 && s2->next_picture_ptr) {
02271             *picture= *(AVFrame*)s2->next_picture_ptr;
02272             s2->next_picture_ptr= NULL;
02273 
02274             *data_size = sizeof(AVFrame);
02275         }
02276         return buf_size;
02277     }
02278 
02279     if(s2->flags&CODEC_FLAG_TRUNCATED){
02280         int next= ff_mpeg1_find_frame_end(&s2->parse_context, buf, buf_size);
02281 
02282         if( ff_combine_frame(&s2->parse_context, next, (const uint8_t **)&buf, &buf_size) < 0 )
02283             return buf_size;
02284     }
02285 
02286 #if 0
02287     if (s->repeat_field % 2 == 1) {
02288         s->repeat_field++;
02289         //fprintf(stderr,"\nRepeating last frame: %d -> %d! pict: %d %d", avctx->frame_number-1, avctx->frame_number,
02290         //        s2->picture_number, s->repeat_field);
02291         if (avctx->flags & CODEC_FLAG_REPEAT_FIELD) {
02292             *data_size = sizeof(AVPicture);
02293             goto the_end;
02294         }
02295     }
02296 #endif
02297 
02298     if(s->mpeg_enc_ctx_allocated==0 && avctx->codec_tag == AV_RL32("VCR2"))
02299         vcr2_init_sequence(avctx);
02300 
02301     s->slice_count= 0;
02302 
02303     if (avctx->extradata && !s->extradata_decoded) {
02304         decode_chunks(avctx, picture, data_size, avctx->extradata, avctx->extradata_size);
02305         s->extradata_decoded = 1;
02306     }
02307 
02308     return decode_chunks(avctx, picture, data_size, buf, buf_size);
02309 }
02310 
02311 static int decode_chunks(AVCodecContext *avctx,
02312                              AVFrame *picture, int *data_size,
02313                              const uint8_t *buf, int buf_size)
02314 {
02315     Mpeg1Context *s = avctx->priv_data;
02316     MpegEncContext *s2 = &s->mpeg_enc_ctx;
02317     const uint8_t *buf_ptr = buf;
02318     const uint8_t *buf_end = buf + buf_size;
02319     int ret, input_size;
02320 
02321     for(;;) {
02322         /* find next start code */
02323         uint32_t start_code = -1;
02324         buf_ptr = ff_find_start_code(buf_ptr,buf_end, &start_code);
02325         if (start_code > 0x1ff){
02326             if(s2->pict_type != FF_B_TYPE || avctx->skip_frame <= AVDISCARD_DEFAULT){
02327                 if(avctx->thread_count > 1){
02328                     int i;
02329 
02330                     avctx->execute(avctx, slice_decode_thread,  (void**)&(s2->thread_context[0]), NULL, s->slice_count, sizeof(void*));
02331                     for(i=0; i<s->slice_count; i++)
02332                         s2->error_count += s2->thread_context[i]->error_count;
02333                 }
02334 
02335                 if (CONFIG_MPEG_VDPAU_DECODER && avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
02336                     ff_vdpau_mpeg_picture_complete(s2, buf, buf_size, s->slice_count);
02337 
02338                 if (slice_end(avctx, picture)) {
02339                     if(s2->last_picture_ptr || s2->low_delay) //FIXME merge with the stuff in mpeg_decode_slice
02340                         *data_size = sizeof(AVPicture);
02341                 }
02342             }
02343             s2->pict_type= 0;
02344             return FFMAX(0, buf_ptr - buf - s2->parse_context.last_index);
02345         }
02346 
02347         input_size = buf_end - buf_ptr;
02348 
02349         if(avctx->debug & FF_DEBUG_STARTCODE){
02350             av_log(avctx, AV_LOG_DEBUG, "%3X at %td left %d\n", start_code, buf_ptr-buf, input_size);
02351         }
02352 
02353         /* prepare data for next start code */
02354         switch(start_code) {
02355         case SEQ_START_CODE:
02356             mpeg1_decode_sequence(avctx, buf_ptr,
02357                                     input_size);
02358             break;
02359 
02360         case PICTURE_START_CODE:
02361             /* we have a complete image: we try to decompress it */
02362             if(mpeg1_decode_picture(avctx,
02363                                     buf_ptr, input_size) < 0)
02364                 s2->pict_type=0;
02365             break;
02366         case EXT_START_CODE:
02367             mpeg_decode_extension(avctx,
02368                                     buf_ptr, input_size);
02369             break;
02370         case USER_START_CODE:
02371             mpeg_decode_user_data(avctx,
02372                                     buf_ptr, input_size);
02373             break;
02374         case GOP_START_CODE:
02375             s2->first_field=0;
02376             mpeg_decode_gop(avctx,
02377                                     buf_ptr, input_size);
02378             break;
02379         default:
02380             if (start_code >= SLICE_MIN_START_CODE &&
02381                 start_code <= SLICE_MAX_START_CODE) {
02382                 int mb_y= start_code - SLICE_MIN_START_CODE;
02383 
02384                 if(s2->last_picture_ptr==NULL){
02385                 /* Skip B-frames if we do not have reference frames. */
02386                     if(s2->pict_type==FF_B_TYPE) break;
02387                 }
02388                 if(s2->next_picture_ptr==NULL){
02389                 /* Skip P-frames if we do not have a reference frame or we have an invalid header. */
02390                     if(s2->pict_type==FF_P_TYPE && (s2->first_field || s2->picture_structure==PICT_FRAME)) break;
02391                 }
02392                 /* Skip B-frames if we are in a hurry. */
02393                 if(avctx->hurry_up && s2->pict_type==FF_B_TYPE) break;
02394                 if(  (avctx->skip_frame >= AVDISCARD_NONREF && s2->pict_type==FF_B_TYPE)
02395                     ||(avctx->skip_frame >= AVDISCARD_NONKEY && s2->pict_type!=FF_I_TYPE)
02396                     || avctx->skip_frame >= AVDISCARD_ALL)
02397                     break;
02398                 /* Skip everything if we are in a hurry>=5. */
02399                 if(avctx->hurry_up>=5) break;
02400 
02401                 if (!s->mpeg_enc_ctx_allocated) break;
02402 
02403                 if(s2->codec_id == CODEC_ID_MPEG2VIDEO){
02404                     if(mb_y < avctx->skip_top || mb_y >= s2->mb_height - avctx->skip_bottom)
02405                         break;
02406                 }
02407 
02408                 if(!s2->pict_type){
02409                     av_log(avctx, AV_LOG_ERROR, "Missing picture start code\n");
02410                     break;
02411                 }
02412 
02413                 if(s2->first_slice){
02414                     s2->first_slice=0;
02415                     if(mpeg_field_start(s2, buf, buf_size) < 0)
02416                         return -1;
02417                 }
02418                 if(!s2->current_picture_ptr){
02419                     av_log(avctx, AV_LOG_ERROR, "current_picture not initialized\n");
02420                     return -1;
02421                 }
02422 
02423                 if (avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU) {
02424                     s->slice_count++;
02425                     break;
02426                 }
02427 
02428                 if(avctx->thread_count > 1){
02429                     int threshold= (s2->mb_height*s->slice_count + avctx->thread_count/2) / avctx->thread_count;
02430                     if(threshold <= mb_y){
02431                         MpegEncContext *thread_context= s2->thread_context[s->slice_count];
02432 
02433                         thread_context->start_mb_y= mb_y;
02434                         thread_context->end_mb_y  = s2->mb_height;
02435                         if(s->slice_count){
02436                             s2->thread_context[s->slice_count-1]->end_mb_y= mb_y;
02437                             ff_update_duplicate_context(thread_context, s2);
02438                         }
02439                         init_get_bits(&thread_context->gb, buf_ptr, input_size*8);
02440                         s->slice_count++;
02441                     }
02442                     buf_ptr += 2; //FIXME add minimum number of bytes per slice
02443                 }else{
02444                     ret = mpeg_decode_slice(s, mb_y, &buf_ptr, input_size);
02445                     emms_c();
02446 
02447                     if(ret < 0){
02448                         if(s2->resync_mb_x>=0 && s2->resync_mb_y>=0)
02449                             ff_er_add_slice(s2, s2->resync_mb_x, s2->resync_mb_y, s2->mb_x, s2->mb_y, AC_ERROR|DC_ERROR|MV_ERROR);
02450                     }else{
02451                         ff_er_add_slice(s2, s2->resync_mb_x, s2->resync_mb_y, s2->mb_x-1, s2->mb_y, AC_END|DC_END|MV_END);
02452                     }
02453                 }
02454             }
02455             break;
02456         }
02457     }
02458 }
02459 
02460 static int mpeg_decode_end(AVCodecContext *avctx)
02461 {
02462     Mpeg1Context *s = avctx->priv_data;
02463 
02464     if (s->mpeg_enc_ctx_allocated)
02465         MPV_common_end(&s->mpeg_enc_ctx);
02466     return 0;
02467 }
02468 
02469 AVCodec mpeg1video_decoder = {
02470     "mpeg1video",
02471     CODEC_TYPE_VIDEO,
02472     CODEC_ID_MPEG1VIDEO,
02473     sizeof(Mpeg1Context),
02474     mpeg_decode_init,
02475     NULL,
02476     mpeg_decode_end,
02477     mpeg_decode_frame,
02478     CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
02479     .flush= ff_mpeg_flush,
02480     .long_name= NULL_IF_CONFIG_SMALL("MPEG-1 video"),
02481 };
02482 
02483 AVCodec mpeg2video_decoder = {
02484     "mpeg2video",
02485     CODEC_TYPE_VIDEO,
02486     CODEC_ID_MPEG2VIDEO,
02487     sizeof(Mpeg1Context),
02488     mpeg_decode_init,
02489     NULL,
02490     mpeg_decode_end,
02491     mpeg_decode_frame,
02492     CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
02493     .flush= ff_mpeg_flush,
02494     .long_name= NULL_IF_CONFIG_SMALL("MPEG-2 video"),
02495 };
02496 
02497 //legacy decoder
02498 AVCodec mpegvideo_decoder = {
02499     "mpegvideo",
02500     CODEC_TYPE_VIDEO,
02501     CODEC_ID_MPEG2VIDEO,
02502     sizeof(Mpeg1Context),
02503     mpeg_decode_init,
02504     NULL,
02505     mpeg_decode_end,
02506     mpeg_decode_frame,
02507     CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
02508     .flush= ff_mpeg_flush,
02509     .long_name= NULL_IF_CONFIG_SMALL("MPEG-1 video"),
02510 };
02511 
02512 #if CONFIG_MPEG_XVMC_DECODER
02513 static av_cold int mpeg_mc_decode_init(AVCodecContext *avctx){
02514     if( avctx->thread_count > 1)
02515         return -1;
02516     if( !(avctx->slice_flags & SLICE_FLAG_CODED_ORDER) )
02517         return -1;
02518     if( !(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD) ){
02519         dprintf(avctx, "mpeg12.c: XvMC decoder will work better if SLICE_FLAG_ALLOW_FIELD is set\n");
02520     }
02521     mpeg_decode_init(avctx);
02522 
02523     avctx->pix_fmt = PIX_FMT_XVMC_MPEG2_IDCT;
02524     avctx->xvmc_acceleration = 2;//2 - the blocks are packed!
02525 
02526     return 0;
02527 }
02528 
02529 AVCodec mpeg_xvmc_decoder = {
02530     "mpegvideo_xvmc",
02531     CODEC_TYPE_VIDEO,
02532     CODEC_ID_MPEG2VIDEO_XVMC,
02533     sizeof(Mpeg1Context),
02534     mpeg_mc_decode_init,
02535     NULL,
02536     mpeg_decode_end,
02537     mpeg_decode_frame,
02538     CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED| CODEC_CAP_HWACCEL | CODEC_CAP_DELAY,
02539     .flush= ff_mpeg_flush,
02540     .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video XvMC (X-Video Motion Compensation)"),
02541 };
02542 
02543 #endif
02544 
02545 #if CONFIG_MPEG_VDPAU_DECODER
02546 AVCodec mpeg_vdpau_decoder = {
02547     "mpegvideo_vdpau",
02548     CODEC_TYPE_VIDEO,
02549     CODEC_ID_MPEG2VIDEO,
02550     sizeof(Mpeg1Context),
02551     mpeg_decode_init,
02552     NULL,
02553     mpeg_decode_end,
02554     mpeg_decode_frame,
02555     CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_HWACCEL_VDPAU | CODEC_CAP_DELAY,
02556     .flush= ff_mpeg_flush,
02557     .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video (VDPAU acceleration)"),
02558 };
02559 #endif
02560 
02561 #if CONFIG_MPEG1_VDPAU_DECODER
02562 AVCodec mpeg1_vdpau_decoder = {
02563     "mpeg1video_vdpau",
02564     CODEC_TYPE_VIDEO,
02565     CODEC_ID_MPEG1VIDEO,
02566     sizeof(Mpeg1Context),
02567     mpeg_decode_init,
02568     NULL,
02569     mpeg_decode_end,
02570     mpeg_decode_frame,
02571     CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_HWACCEL_VDPAU | CODEC_CAP_DELAY,
02572     .flush= ff_mpeg_flush,
02573     .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video (VDPAU acceleration)"),
02574 };
02575 #endif
02576 

Generated on Tue Nov 4 2014 12:59:22 for ffmpeg by  doxygen 1.7.1