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