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 } Mpeg1Context;
01151 
01152 static av_cold int mpeg_decode_init(AVCodecContext *avctx)
01153 {
01154     Mpeg1Context *s = avctx->priv_data;
01155     MpegEncContext *s2 = &s->mpeg_enc_ctx;
01156     int i;
01157 
01158     /* we need some permutation to store matrices,
01159      * until MPV_common_init() sets the real permutation. */
01160     for(i=0;i<64;i++)
01161        s2->dsp.idct_permutation[i]=i;
01162 
01163     MPV_decode_defaults(s2);
01164 
01165     s->mpeg_enc_ctx.avctx= avctx;
01166     s->mpeg_enc_ctx.flags= avctx->flags;
01167     s->mpeg_enc_ctx.flags2= avctx->flags2;
01168     ff_mpeg12_common_init(&s->mpeg_enc_ctx);
01169     ff_mpeg12_init_vlcs();
01170 
01171     s->mpeg_enc_ctx_allocated = 0;
01172     s->mpeg_enc_ctx.picture_number = 0;
01173     s->repeat_field = 0;
01174     s->mpeg_enc_ctx.codec_id= avctx->codec->id;
01175     avctx->color_range= AVCOL_RANGE_MPEG;
01176     if (avctx->codec->id == CODEC_ID_MPEG1VIDEO)
01177         avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
01178     else
01179         avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
01180     return 0;
01181 }
01182 
01183 static int mpeg_decode_update_thread_context(AVCodecContext *avctx, const AVCodecContext *avctx_from)
01184 {
01185     Mpeg1Context *ctx = avctx->priv_data, *ctx_from = avctx_from->priv_data;
01186     MpegEncContext *s = &ctx->mpeg_enc_ctx, *s1 = &ctx_from->mpeg_enc_ctx;
01187     int err;
01188 
01189     if(avctx == avctx_from || !ctx_from->mpeg_enc_ctx_allocated || !s1->context_initialized)
01190         return 0;
01191 
01192     err = ff_mpeg_update_thread_context(avctx, avctx_from);
01193     if(err) return err;
01194 
01195     if(!ctx->mpeg_enc_ctx_allocated)
01196         memcpy(s + 1, s1 + 1, sizeof(Mpeg1Context) - sizeof(MpegEncContext));
01197 
01198     if(!(s->pict_type == FF_B_TYPE || s->low_delay))
01199         s->picture_number++;
01200 
01201     return 0;
01202 }
01203 
01204 static void quant_matrix_rebuild(uint16_t *matrix, const uint8_t *old_perm,
01205                                      const uint8_t *new_perm){
01206     uint16_t temp_matrix[64];
01207     int i;
01208 
01209     memcpy(temp_matrix,matrix,64*sizeof(uint16_t));
01210 
01211     for(i=0;i<64;i++){
01212         matrix[new_perm[i]] = temp_matrix[old_perm[i]];
01213     }
01214 }
01215 
01216 static enum PixelFormat mpeg_get_pixelformat(AVCodecContext *avctx){
01217     Mpeg1Context *s1 = avctx->priv_data;
01218     MpegEncContext *s = &s1->mpeg_enc_ctx;
01219 
01220     if(avctx->xvmc_acceleration)
01221         return avctx->get_format(avctx,pixfmt_xvmc_mpg2_420);
01222     else if(avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU){
01223         if(avctx->codec_id == CODEC_ID_MPEG1VIDEO)
01224             return PIX_FMT_VDPAU_MPEG1;
01225         else
01226             return PIX_FMT_VDPAU_MPEG2;
01227     }else{
01228         if(s->chroma_format <  2)
01229             return avctx->get_format(avctx,ff_hwaccel_pixfmt_list_420);
01230         else if(s->chroma_format == 2)
01231             return PIX_FMT_YUV422P;
01232         else
01233             return PIX_FMT_YUV444P;
01234     }
01235 }
01236 
01237 /* Call this function when we know all parameters.
01238  * It may be called in different places for MPEG-1 and MPEG-2. */
01239 static int mpeg_decode_postinit(AVCodecContext *avctx){
01240     Mpeg1Context *s1 = avctx->priv_data;
01241     MpegEncContext *s = &s1->mpeg_enc_ctx;
01242     uint8_t old_permutation[64];
01243 
01244     if (
01245         (s1->mpeg_enc_ctx_allocated == 0)||
01246         avctx->coded_width  != s->width ||
01247         avctx->coded_height != s->height||
01248         s1->save_width != s->width ||
01249         s1->save_height != s->height ||
01250         s1->save_aspect_info != s->aspect_ratio_info||
01251         s1->save_progressive_seq != s->progressive_sequence ||
01252         0)
01253     {
01254 
01255         if (s1->mpeg_enc_ctx_allocated) {
01256             ParseContext pc= s->parse_context;
01257             s->parse_context.buffer=0;
01258             MPV_common_end(s);
01259             s->parse_context= pc;
01260         }
01261 
01262         if( (s->width == 0 )||(s->height == 0))
01263             return -2;
01264 
01265         avcodec_set_dimensions(avctx, s->width, s->height);
01266         avctx->bit_rate = s->bit_rate;
01267         s1->save_aspect_info = s->aspect_ratio_info;
01268         s1->save_width = s->width;
01269         s1->save_height = s->height;
01270         s1->save_progressive_seq = s->progressive_sequence;
01271 
01272         /* low_delay may be forced, in this case we will have B-frames
01273          * that behave like P-frames. */
01274         avctx->has_b_frames = !(s->low_delay);
01275 
01276         assert((avctx->sub_id==1) == (avctx->codec_id==CODEC_ID_MPEG1VIDEO));
01277         if(avctx->codec_id==CODEC_ID_MPEG1VIDEO){
01278             //MPEG-1 fps
01279             avctx->time_base.den= ff_frame_rate_tab[s->frame_rate_index].num;
01280             avctx->time_base.num= ff_frame_rate_tab[s->frame_rate_index].den;
01281             //MPEG-1 aspect
01282             avctx->sample_aspect_ratio= av_d2q(
01283                     1.0/ff_mpeg1_aspect[s->aspect_ratio_info], 255);
01284             avctx->ticks_per_frame=1;
01285         }else{//MPEG-2
01286         //MPEG-2 fps
01287             av_reduce(
01288                 &s->avctx->time_base.den,
01289                 &s->avctx->time_base.num,
01290                 ff_frame_rate_tab[s->frame_rate_index].num * s1->frame_rate_ext.num*2,
01291                 ff_frame_rate_tab[s->frame_rate_index].den * s1->frame_rate_ext.den,
01292                 1<<30);
01293             avctx->ticks_per_frame=2;
01294         //MPEG-2 aspect
01295             if(s->aspect_ratio_info > 1){
01296                 AVRational dar =
01297                     av_mul_q(
01298                         av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
01299                                  (AVRational){s1->pan_scan.width, s1->pan_scan.height}),
01300                         (AVRational){s->width, s->height});
01301 
01302                 // we ignore the spec here and guess a bit as reality does not match the spec, see for example
01303                 // res_change_ffmpeg_aspect.ts and sequence-display-aspect.mpg
01304                 // issue1613, 621, 562
01305                 if((s1->pan_scan.width == 0 ) || (s1->pan_scan.height == 0) ||
01306                    (av_cmp_q(dar,(AVRational){4,3}) && av_cmp_q(dar,(AVRational){16,9}))) {
01307                     s->avctx->sample_aspect_ratio=
01308                         av_div_q(
01309                          ff_mpeg2_aspect[s->aspect_ratio_info],
01310                          (AVRational){s->width, s->height}
01311                          );
01312                 }else{
01313                     s->avctx->sample_aspect_ratio=
01314                         av_div_q(
01315                          ff_mpeg2_aspect[s->aspect_ratio_info],
01316                          (AVRational){s1->pan_scan.width, s1->pan_scan.height}
01317                         );
01318 //issue1613 4/3 16/9 -> 16/9
01319 //res_change_ffmpeg_aspect.ts 4/3 225/44 ->4/3
01320 //widescreen-issue562.mpg 4/3 16/9 -> 16/9
01321 //                    s->avctx->sample_aspect_ratio= av_mul_q(s->avctx->sample_aspect_ratio, (AVRational){s->width, s->height});
01322 //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);
01323 //av_log(NULL, AV_LOG_ERROR, "B %d/%d\n",s->avctx->sample_aspect_ratio.num, s->avctx->sample_aspect_ratio.den);
01324                 }
01325             }else{
01326                 s->avctx->sample_aspect_ratio=
01327                     ff_mpeg2_aspect[s->aspect_ratio_info];
01328             }
01329         }//MPEG-2
01330 
01331         avctx->pix_fmt = mpeg_get_pixelformat(avctx);
01332         avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
01333         //until then pix_fmt may be changed right after codec init
01334         if( avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT ||
01335             avctx->hwaccel ||
01336             s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU )
01337             if( avctx->idct_algo == FF_IDCT_AUTO )
01338                 avctx->idct_algo = FF_IDCT_SIMPLE;
01339 
01340         /* Quantization matrices may need reordering
01341          * if DCT permutation is changed. */
01342         memcpy(old_permutation,s->dsp.idct_permutation,64*sizeof(uint8_t));
01343 
01344         if (MPV_common_init(s) < 0)
01345             return -2;
01346 
01347         quant_matrix_rebuild(s->intra_matrix,       old_permutation,s->dsp.idct_permutation);
01348         quant_matrix_rebuild(s->inter_matrix,       old_permutation,s->dsp.idct_permutation);
01349         quant_matrix_rebuild(s->chroma_intra_matrix,old_permutation,s->dsp.idct_permutation);
01350         quant_matrix_rebuild(s->chroma_inter_matrix,old_permutation,s->dsp.idct_permutation);
01351 
01352         s1->mpeg_enc_ctx_allocated = 1;
01353     }
01354     return 0;
01355 }
01356 
01357 static int mpeg1_decode_picture(AVCodecContext *avctx,
01358                                 const uint8_t *buf, int buf_size)
01359 {
01360     Mpeg1Context *s1 = avctx->priv_data;
01361     MpegEncContext *s = &s1->mpeg_enc_ctx;
01362     int ref, f_code, vbv_delay;
01363 
01364     init_get_bits(&s->gb, buf, buf_size*8);
01365 
01366     ref = get_bits(&s->gb, 10); /* temporal ref */
01367     s->pict_type = get_bits(&s->gb, 3);
01368     if(s->pict_type == 0 || s->pict_type > 3)
01369         return -1;
01370 
01371     vbv_delay= get_bits(&s->gb, 16);
01372     if (s->pict_type == AV_PICTURE_TYPE_P || s->pict_type == AV_PICTURE_TYPE_B) {
01373         s->full_pel[0] = get_bits1(&s->gb);
01374         f_code = get_bits(&s->gb, 3);
01375         if (f_code == 0 && avctx->error_recognition >= FF_ER_COMPLIANT)
01376             return -1;
01377         s->mpeg_f_code[0][0] = f_code;
01378         s->mpeg_f_code[0][1] = f_code;
01379     }
01380     if (s->pict_type == AV_PICTURE_TYPE_B) {
01381         s->full_pel[1] = get_bits1(&s->gb);
01382         f_code = get_bits(&s->gb, 3);
01383         if (f_code == 0 && avctx->error_recognition >= FF_ER_COMPLIANT)
01384             return -1;
01385         s->mpeg_f_code[1][0] = f_code;
01386         s->mpeg_f_code[1][1] = f_code;
01387     }
01388     s->current_picture.pict_type= s->pict_type;
01389     s->current_picture.key_frame= s->pict_type == AV_PICTURE_TYPE_I;
01390 
01391     if(avctx->debug & FF_DEBUG_PICT_INFO)
01392         av_log(avctx, AV_LOG_DEBUG, "vbv_delay %d, ref %d type:%d\n", vbv_delay, ref, s->pict_type);
01393 
01394     s->y_dc_scale = 8;
01395     s->c_dc_scale = 8;
01396     return 0;
01397 }
01398 
01399 static void mpeg_decode_sequence_extension(Mpeg1Context *s1)
01400 {
01401     MpegEncContext *s= &s1->mpeg_enc_ctx;
01402     int horiz_size_ext, vert_size_ext;
01403     int bit_rate_ext;
01404 
01405     skip_bits(&s->gb, 1); /* profile and level esc*/
01406     s->avctx->profile= get_bits(&s->gb, 3);
01407     s->avctx->level= get_bits(&s->gb, 4);
01408     s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */
01409     s->chroma_format = get_bits(&s->gb, 2); /* chroma_format 1=420, 2=422, 3=444 */
01410     horiz_size_ext = get_bits(&s->gb, 2);
01411     vert_size_ext = get_bits(&s->gb, 2);
01412     s->width |= (horiz_size_ext << 12);
01413     s->height |= (vert_size_ext << 12);
01414     bit_rate_ext = get_bits(&s->gb, 12);  /* XXX: handle it */
01415     s->bit_rate += (bit_rate_ext << 18) * 400;
01416     skip_bits1(&s->gb); /* marker */
01417     s->avctx->rc_buffer_size += get_bits(&s->gb, 8)*1024*16<<10;
01418 
01419     s->low_delay = get_bits1(&s->gb);
01420     if(s->flags & CODEC_FLAG_LOW_DELAY) s->low_delay=1;
01421 
01422     s1->frame_rate_ext.num = get_bits(&s->gb, 2)+1;
01423     s1->frame_rate_ext.den = get_bits(&s->gb, 5)+1;
01424 
01425     av_dlog(s->avctx, "sequence extension\n");
01426     s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG2VIDEO;
01427     s->avctx->sub_id = 2; /* indicates MPEG-2 found */
01428 
01429     if(s->avctx->debug & FF_DEBUG_PICT_INFO)
01430         av_log(s->avctx, AV_LOG_DEBUG, "profile: %d, level: %d vbv buffer: %d, bitrate:%d\n",
01431                s->avctx->profile, s->avctx->level, s->avctx->rc_buffer_size, s->bit_rate);
01432 
01433 }
01434 
01435 static void mpeg_decode_sequence_display_extension(Mpeg1Context *s1)
01436 {
01437     MpegEncContext *s= &s1->mpeg_enc_ctx;
01438     int color_description, w, h;
01439 
01440     skip_bits(&s->gb, 3); /* video format */
01441     color_description= get_bits1(&s->gb);
01442     if(color_description){
01443         s->avctx->color_primaries= get_bits(&s->gb, 8);
01444         s->avctx->color_trc      = get_bits(&s->gb, 8);
01445         s->avctx->colorspace     = get_bits(&s->gb, 8);
01446     }
01447     w= get_bits(&s->gb, 14);
01448     skip_bits(&s->gb, 1); //marker
01449     h= get_bits(&s->gb, 14);
01450     // remaining 3 bits are zero padding
01451 
01452     s1->pan_scan.width= 16*w;
01453     s1->pan_scan.height=16*h;
01454 
01455     if(s->avctx->debug & FF_DEBUG_PICT_INFO)
01456         av_log(s->avctx, AV_LOG_DEBUG, "sde w:%d, h:%d\n", w, h);
01457 }
01458 
01459 static void mpeg_decode_picture_display_extension(Mpeg1Context *s1)
01460 {
01461     MpegEncContext *s= &s1->mpeg_enc_ctx;
01462     int i,nofco;
01463 
01464     nofco = 1;
01465     if(s->progressive_sequence){
01466         if(s->repeat_first_field){
01467             nofco++;
01468             if(s->top_field_first)
01469                 nofco++;
01470         }
01471     }else{
01472         if(s->picture_structure == PICT_FRAME){
01473             nofco++;
01474             if(s->repeat_first_field)
01475                 nofco++;
01476         }
01477     }
01478     for(i=0; i<nofco; i++){
01479         s1->pan_scan.position[i][0]= get_sbits(&s->gb, 16);
01480         skip_bits(&s->gb, 1); //marker
01481         s1->pan_scan.position[i][1]= get_sbits(&s->gb, 16);
01482         skip_bits(&s->gb, 1); //marker
01483     }
01484 
01485     if(s->avctx->debug & FF_DEBUG_PICT_INFO)
01486         av_log(s->avctx, AV_LOG_DEBUG, "pde (%d,%d) (%d,%d) (%d,%d)\n",
01487             s1->pan_scan.position[0][0], s1->pan_scan.position[0][1],
01488             s1->pan_scan.position[1][0], s1->pan_scan.position[1][1],
01489             s1->pan_scan.position[2][0], s1->pan_scan.position[2][1]
01490         );
01491 }
01492 
01493 static int load_matrix(MpegEncContext *s, uint16_t matrix0[64], uint16_t matrix1[64], int intra){
01494     int i;
01495 
01496     for(i=0; i<64; i++) {
01497         int j = s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
01498         int v = get_bits(&s->gb, 8);
01499         if(v==0){
01500             av_log(s->avctx, AV_LOG_ERROR, "matrix damaged\n");
01501             return -1;
01502         }
01503         if(intra && i==0 && v!=8){
01504             av_log(s->avctx, AV_LOG_ERROR, "intra matrix invalid, ignoring\n");
01505             v= 8; // needed by pink.mpg / issue1046
01506         }
01507         matrix0[j] = v;
01508         if(matrix1)
01509             matrix1[j] = v;
01510     }
01511     return 0;
01512 }
01513 
01514 static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
01515 {
01516     av_dlog(s->avctx, "matrix extension\n");
01517 
01518     if(get_bits1(&s->gb)) load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
01519     if(get_bits1(&s->gb)) load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
01520     if(get_bits1(&s->gb)) load_matrix(s, s->chroma_intra_matrix, NULL           , 1);
01521     if(get_bits1(&s->gb)) load_matrix(s, s->chroma_inter_matrix, NULL           , 0);
01522 }
01523 
01524 static void mpeg_decode_picture_coding_extension(Mpeg1Context *s1)
01525 {
01526     MpegEncContext *s= &s1->mpeg_enc_ctx;
01527 
01528     s->full_pel[0] = s->full_pel[1] = 0;
01529     s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
01530     s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
01531     s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
01532     s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
01533     if(!s->pict_type && s1->mpeg_enc_ctx_allocated){
01534         av_log(s->avctx, AV_LOG_ERROR, "Missing picture start code, guessing missing values\n");
01535         if(s->mpeg_f_code[1][0] == 15 && s->mpeg_f_code[1][1]==15){
01536             if(s->mpeg_f_code[0][0] == 15 && s->mpeg_f_code[0][1] == 15)
01537                 s->pict_type= AV_PICTURE_TYPE_I;
01538             else
01539                 s->pict_type= AV_PICTURE_TYPE_P;
01540         }else
01541             s->pict_type= AV_PICTURE_TYPE_B;
01542         s->current_picture.pict_type= s->pict_type;
01543         s->current_picture.key_frame= s->pict_type == AV_PICTURE_TYPE_I;
01544     }
01545     s->intra_dc_precision = get_bits(&s->gb, 2);
01546     s->picture_structure = get_bits(&s->gb, 2);
01547     s->top_field_first = get_bits1(&s->gb);
01548     s->frame_pred_frame_dct = get_bits1(&s->gb);
01549     s->concealment_motion_vectors = get_bits1(&s->gb);
01550     s->q_scale_type = get_bits1(&s->gb);
01551     s->intra_vlc_format = get_bits1(&s->gb);
01552     s->alternate_scan = get_bits1(&s->gb);
01553     s->repeat_first_field = get_bits1(&s->gb);
01554     s->chroma_420_type = get_bits1(&s->gb);
01555     s->progressive_frame = get_bits1(&s->gb);
01556 
01557     if(s->progressive_sequence && !s->progressive_frame){
01558         s->progressive_frame= 1;
01559         av_log(s->avctx, AV_LOG_ERROR, "interlaced frame in progressive sequence, ignoring\n");
01560     }
01561 
01562     if(s->picture_structure==0 || (s->progressive_frame && s->picture_structure!=PICT_FRAME)){
01563         av_log(s->avctx, AV_LOG_ERROR, "picture_structure %d invalid, ignoring\n", s->picture_structure);
01564         s->picture_structure= PICT_FRAME;
01565     }
01566 
01567     if(s->progressive_sequence && !s->frame_pred_frame_dct){
01568         av_log(s->avctx, AV_LOG_ERROR, "invalid frame_pred_frame_dct\n");
01569         s->frame_pred_frame_dct= 1;
01570     }
01571 
01572     if(s->picture_structure == PICT_FRAME){
01573         s->first_field=0;
01574         s->v_edge_pos= 16*s->mb_height;
01575     }else{
01576         s->first_field ^= 1;
01577         s->v_edge_pos=  8*s->mb_height;
01578         memset(s->mbskip_table, 0, s->mb_stride*s->mb_height);
01579     }
01580 
01581     if(s->alternate_scan){
01582         ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable  , ff_alternate_vertical_scan);
01583         ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable  , ff_alternate_vertical_scan);
01584     }else{
01585         ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable  , ff_zigzag_direct);
01586         ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable  , ff_zigzag_direct);
01587     }
01588 
01589     /* composite display not parsed */
01590     av_dlog(s->avctx, "intra_dc_precision=%d\n", s->intra_dc_precision);
01591     av_dlog(s->avctx, "picture_structure=%d\n", s->picture_structure);
01592     av_dlog(s->avctx, "top field first=%d\n", s->top_field_first);
01593     av_dlog(s->avctx, "repeat first field=%d\n", s->repeat_first_field);
01594     av_dlog(s->avctx, "conceal=%d\n", s->concealment_motion_vectors);
01595     av_dlog(s->avctx, "intra_vlc_format=%d\n", s->intra_vlc_format);
01596     av_dlog(s->avctx, "alternate_scan=%d\n", s->alternate_scan);
01597     av_dlog(s->avctx, "frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
01598     av_dlog(s->avctx, "progressive_frame=%d\n", s->progressive_frame);
01599 }
01600 
01601 static void exchange_uv(MpegEncContext *s){
01602     DCTELEM (*tmp)[64];
01603 
01604     tmp           = s->pblocks[4];
01605     s->pblocks[4] = s->pblocks[5];
01606     s->pblocks[5] = tmp;
01607 }
01608 
01609 static int mpeg_field_start(MpegEncContext *s, const uint8_t *buf, int buf_size){
01610     AVCodecContext *avctx= s->avctx;
01611     Mpeg1Context *s1 = (Mpeg1Context*)s;
01612 
01613     /* start frame decoding */
01614     if(s->first_field || s->picture_structure==PICT_FRAME){
01615         if(MPV_frame_start(s, avctx) < 0)
01616             return -1;
01617 
01618         ff_er_frame_start(s);
01619 
01620         /* first check if we must repeat the frame */
01621         s->current_picture_ptr->repeat_pict = 0;
01622         if (s->repeat_first_field) {
01623             if (s->progressive_sequence) {
01624                 if (s->top_field_first)
01625                     s->current_picture_ptr->repeat_pict = 4;
01626                 else
01627                     s->current_picture_ptr->repeat_pict = 2;
01628             } else if (s->progressive_frame) {
01629                 s->current_picture_ptr->repeat_pict = 1;
01630             }
01631         }
01632 
01633         *s->current_picture_ptr->pan_scan= s1->pan_scan;
01634 
01635         if (HAVE_PTHREADS && (avctx->active_thread_type & FF_THREAD_FRAME))
01636             ff_thread_finish_setup(avctx);
01637     }else{ //second field
01638             int i;
01639 
01640             if(!s->current_picture_ptr){
01641                 av_log(s->avctx, AV_LOG_ERROR, "first field missing\n");
01642                 return -1;
01643             }
01644 
01645             for(i=0; i<4; i++){
01646                 s->current_picture.data[i] = s->current_picture_ptr->data[i];
01647                 if(s->picture_structure == PICT_BOTTOM_FIELD){
01648                     s->current_picture.data[i] += s->current_picture_ptr->linesize[i];
01649                 }
01650             }
01651     }
01652 
01653     if (avctx->hwaccel) {
01654         if (avctx->hwaccel->start_frame(avctx, buf, buf_size) < 0)
01655             return -1;
01656     }
01657 
01658 // MPV_frame_start will call this function too,
01659 // but we need to call it on every field
01660     if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
01661         if(ff_xvmc_field_start(s,avctx) < 0)
01662             return -1;
01663 
01664     return 0;
01665 }
01666 
01667 #define DECODE_SLICE_ERROR -1
01668 #define DECODE_SLICE_OK 0
01669 
01675 static int mpeg_decode_slice(Mpeg1Context *s1, int mb_y,
01676                              const uint8_t **buf, int buf_size)
01677 {
01678     MpegEncContext *s = &s1->mpeg_enc_ctx;
01679     AVCodecContext *avctx= s->avctx;
01680     const int field_pic= s->picture_structure != PICT_FRAME;
01681     const int lowres= s->avctx->lowres;
01682 
01683     s->resync_mb_x=
01684     s->resync_mb_y= -1;
01685 
01686     assert(mb_y < s->mb_height);
01687 
01688     init_get_bits(&s->gb, *buf, buf_size*8);
01689 
01690     ff_mpeg1_clean_buffers(s);
01691     s->interlaced_dct = 0;
01692 
01693     s->qscale = get_qscale(s);
01694 
01695     if(s->qscale == 0){
01696         av_log(s->avctx, AV_LOG_ERROR, "qscale == 0\n");
01697         return -1;
01698     }
01699 
01700     /* extra slice info */
01701     while (get_bits1(&s->gb) != 0) {
01702         skip_bits(&s->gb, 8);
01703     }
01704 
01705     s->mb_x=0;
01706 
01707     if(mb_y==0 && s->codec_tag == AV_RL32("SLIF")){
01708         skip_bits1(&s->gb);
01709     }else{
01710         for(;;) {
01711             int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
01712             if (code < 0){
01713                 av_log(s->avctx, AV_LOG_ERROR, "first mb_incr damaged\n");
01714                 return -1;
01715             }
01716             if (code >= 33) {
01717                 if (code == 33) {
01718                     s->mb_x += 33;
01719                 }
01720                 /* otherwise, stuffing, nothing to do */
01721             } else {
01722                 s->mb_x += code;
01723                 break;
01724             }
01725         }
01726     }
01727 
01728     if(s->mb_x >= (unsigned)s->mb_width){
01729         av_log(s->avctx, AV_LOG_ERROR, "initial skip overflow\n");
01730         return -1;
01731     }
01732 
01733     if (avctx->hwaccel) {
01734         const uint8_t *buf_end, *buf_start = *buf - 4; /* include start_code */
01735         int start_code = -1;
01736         buf_end = ff_find_start_code(buf_start + 2, *buf + buf_size, &start_code);
01737         if (buf_end < *buf + buf_size)
01738             buf_end -= 4;
01739         s->mb_y = mb_y;
01740         if (avctx->hwaccel->decode_slice(avctx, buf_start, buf_end - buf_start) < 0)
01741             return DECODE_SLICE_ERROR;
01742         *buf = buf_end;
01743         return DECODE_SLICE_OK;
01744     }
01745 
01746     s->resync_mb_x= s->mb_x;
01747     s->resync_mb_y= s->mb_y= mb_y;
01748     s->mb_skip_run= 0;
01749     ff_init_block_index(s);
01750 
01751     if (s->mb_y==0 && s->mb_x==0 && (s->first_field || s->picture_structure==PICT_FRAME)) {
01752         if(s->avctx->debug&FF_DEBUG_PICT_INFO){
01753              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",
01754                  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],
01755                  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")),
01756                  s->progressive_sequence ? "ps" :"", s->progressive_frame ? "pf" : "", s->alternate_scan ? "alt" :"", s->top_field_first ? "top" :"",
01757                  s->intra_dc_precision, s->picture_structure, s->frame_pred_frame_dct, s->concealment_motion_vectors,
01758                  s->q_scale_type, s->intra_vlc_format, s->repeat_first_field, s->chroma_420_type ? "420" :"");
01759         }
01760     }
01761 
01762     for(;;) {
01763         //If 1, we memcpy blocks in xvmcvideo.
01764         if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1)
01765             ff_xvmc_init_block(s);//set s->block
01766 
01767         if(mpeg_decode_mb(s, s->block) < 0)
01768             return -1;
01769 
01770         if(s->current_picture.motion_val[0] && !s->encoding){ //note motion_val is normally NULL unless we want to extract the MVs
01771             const int wrap = s->b8_stride;
01772             int xy = s->mb_x*2 + s->mb_y*2*wrap;
01773             int b8_xy= 4*(s->mb_x + s->mb_y*s->mb_stride);
01774             int motion_x, motion_y, dir, i;
01775 
01776             for(i=0; i<2; i++){
01777                 for(dir=0; dir<2; dir++){
01778                     if (s->mb_intra || (dir==1 && s->pict_type != AV_PICTURE_TYPE_B)) {
01779                         motion_x = motion_y = 0;
01780                     }else if (s->mv_type == MV_TYPE_16X16 || (s->mv_type == MV_TYPE_FIELD && field_pic)){
01781                         motion_x = s->mv[dir][0][0];
01782                         motion_y = s->mv[dir][0][1];
01783                     } else /*if ((s->mv_type == MV_TYPE_FIELD) || (s->mv_type == MV_TYPE_16X8))*/ {
01784                         motion_x = s->mv[dir][i][0];
01785                         motion_y = s->mv[dir][i][1];
01786                     }
01787 
01788                     s->current_picture.motion_val[dir][xy    ][0] = motion_x;
01789                     s->current_picture.motion_val[dir][xy    ][1] = motion_y;
01790                     s->current_picture.motion_val[dir][xy + 1][0] = motion_x;
01791                     s->current_picture.motion_val[dir][xy + 1][1] = motion_y;
01792                     s->current_picture.ref_index [dir][b8_xy    ]=
01793                     s->current_picture.ref_index [dir][b8_xy + 1]= s->field_select[dir][i];
01794                     assert(s->field_select[dir][i]==0 || s->field_select[dir][i]==1);
01795                 }
01796                 xy += wrap;
01797                 b8_xy +=2;
01798             }
01799         }
01800 
01801         s->dest[0] += 16 >> lowres;
01802         s->dest[1] +=(16 >> lowres) >> s->chroma_x_shift;
01803         s->dest[2] +=(16 >> lowres) >> s->chroma_x_shift;
01804 
01805         MPV_decode_mb(s, s->block);
01806 
01807         if (++s->mb_x >= s->mb_width) {
01808             const int mb_size= 16>>s->avctx->lowres;
01809 
01810             ff_draw_horiz_band(s, mb_size*(s->mb_y>>field_pic), mb_size);
01811             MPV_report_decode_progress(s);
01812 
01813             s->mb_x = 0;
01814             s->mb_y += 1<<field_pic;
01815 
01816             if(s->mb_y >= s->mb_height){
01817                 int left= get_bits_left(&s->gb);
01818                 int is_d10= s->chroma_format==2 && s->pict_type==AV_PICTURE_TYPE_I && avctx->profile==0 && avctx->level==5
01819                             && s->intra_dc_precision == 2 && s->q_scale_type == 1 && s->alternate_scan == 0
01820                             && s->progressive_frame == 0 /* vbv_delay == 0xBBB || 0xE10*/;
01821 
01822                 if(left < 0 || (left && show_bits(&s->gb, FFMIN(left, 23)) && !is_d10)
01823                    || (avctx->error_recognition >= FF_ER_AGGRESSIVE && left>8)){
01824                     av_log(avctx, AV_LOG_ERROR, "end mismatch left=%d %0X\n", left, show_bits(&s->gb, FFMIN(left, 23)));
01825                     return -1;
01826                 }else
01827                     goto eos;
01828             }
01829 
01830             ff_init_block_index(s);
01831         }
01832 
01833         /* skip mb handling */
01834         if (s->mb_skip_run == -1) {
01835             /* read increment again */
01836             s->mb_skip_run = 0;
01837             for(;;) {
01838                 int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
01839                 if (code < 0){
01840                     av_log(s->avctx, AV_LOG_ERROR, "mb incr damaged\n");
01841                     return -1;
01842                 }
01843                 if (code >= 33) {
01844                     if (code == 33) {
01845                         s->mb_skip_run += 33;
01846                     }else if(code == 35){
01847                         if(s->mb_skip_run != 0 || show_bits(&s->gb, 15) != 0){
01848                             av_log(s->avctx, AV_LOG_ERROR, "slice mismatch\n");
01849                             return -1;
01850                         }
01851                         goto eos; /* end of slice */
01852                     }
01853                     /* otherwise, stuffing, nothing to do */
01854                 } else {
01855                     s->mb_skip_run += code;
01856                     break;
01857                 }
01858             }
01859             if(s->mb_skip_run){
01860                 int i;
01861                 if(s->pict_type == AV_PICTURE_TYPE_I){
01862                     av_log(s->avctx, AV_LOG_ERROR, "skipped MB in I frame at %d %d\n", s->mb_x, s->mb_y);
01863                     return -1;
01864                 }
01865 
01866                 /* skip mb */
01867                 s->mb_intra = 0;
01868                 for(i=0;i<12;i++)
01869                     s->block_last_index[i] = -1;
01870                 if(s->picture_structure == PICT_FRAME)
01871                     s->mv_type = MV_TYPE_16X16;
01872                 else
01873                     s->mv_type = MV_TYPE_FIELD;
01874                 if (s->pict_type == AV_PICTURE_TYPE_P) {
01875                     /* if P type, zero motion vector is implied */
01876                     s->mv_dir = MV_DIR_FORWARD;
01877                     s->mv[0][0][0] = s->mv[0][0][1] = 0;
01878                     s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
01879                     s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
01880                     s->field_select[0][0]= (s->picture_structure - 1) & 1;
01881                 } else {
01882                     /* if B type, reuse previous vectors and directions */
01883                     s->mv[0][0][0] = s->last_mv[0][0][0];
01884                     s->mv[0][0][1] = s->last_mv[0][0][1];
01885                     s->mv[1][0][0] = s->last_mv[1][0][0];
01886                     s->mv[1][0][1] = s->last_mv[1][0][1];
01887                 }
01888             }
01889         }
01890     }
01891 eos: // end of slice
01892     *buf += (get_bits_count(&s->gb)-1)/8;
01893 //printf("y %d %d %d %d\n", s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y);
01894     return 0;
01895 }
01896 
01897 static int slice_decode_thread(AVCodecContext *c, void *arg){
01898     MpegEncContext *s= *(void**)arg;
01899     const uint8_t *buf= s->gb.buffer;
01900     int mb_y= s->start_mb_y;
01901     const int field_pic= s->picture_structure != PICT_FRAME;
01902 
01903     s->error_count= (3*(s->end_mb_y - s->start_mb_y)*s->mb_width) >> field_pic;
01904 
01905     for(;;){
01906         uint32_t start_code;
01907         int ret;
01908 
01909         ret= mpeg_decode_slice((Mpeg1Context*)s, mb_y, &buf, s->gb.buffer_end - buf);
01910         emms_c();
01911 //av_log(c, AV_LOG_DEBUG, "ret:%d resync:%d/%d mb:%d/%d ts:%d/%d ec:%d\n",
01912 //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);
01913         if(ret < 0){
01914             if(s->resync_mb_x>=0 && s->resync_mb_y>=0)
01915                 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);
01916         }else{
01917             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);
01918         }
01919 
01920         if(s->mb_y == s->end_mb_y)
01921             return 0;
01922 
01923         start_code= -1;
01924         buf = ff_find_start_code(buf, s->gb.buffer_end, &start_code);
01925         mb_y= (start_code - SLICE_MIN_START_CODE) << field_pic;
01926         if (s->picture_structure == PICT_BOTTOM_FIELD)
01927             mb_y++;
01928         if(mb_y < 0 || mb_y >= s->end_mb_y)
01929             return -1;
01930     }
01931 
01932     return 0; //not reached
01933 }
01934 
01939 static int slice_end(AVCodecContext *avctx, AVFrame *pict)
01940 {
01941     Mpeg1Context *s1 = avctx->priv_data;
01942     MpegEncContext *s = &s1->mpeg_enc_ctx;
01943 
01944     if (!s1->mpeg_enc_ctx_allocated || !s->current_picture_ptr)
01945         return 0;
01946 
01947     if (s->avctx->hwaccel) {
01948         if (s->avctx->hwaccel->end_frame(s->avctx) < 0)
01949             av_log(avctx, AV_LOG_ERROR, "hardware accelerator failed to decode picture\n");
01950     }
01951 
01952     if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
01953         ff_xvmc_field_end(s);
01954 
01955     /* end of slice reached */
01956     if (/*s->mb_y<<field_pic == s->mb_height &&*/ !s->first_field) {
01957         /* end of image */
01958 
01959         s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_MPEG2;
01960 
01961         ff_er_frame_end(s);
01962 
01963         MPV_frame_end(s);
01964 
01965         if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
01966             *pict= *(AVFrame*)s->current_picture_ptr;
01967             ff_print_debug_info(s, pict);
01968         } else {
01969             if (avctx->active_thread_type & FF_THREAD_FRAME)
01970                 s->picture_number++;
01971             /* latency of 1 frame for I- and P-frames */
01972             /* XXX: use another variable than picture_number */
01973             if (s->last_picture_ptr != NULL) {
01974                 *pict= *(AVFrame*)s->last_picture_ptr;
01975                  ff_print_debug_info(s, pict);
01976             }
01977         }
01978 
01979         return 1;
01980     } else {
01981         return 0;
01982     }
01983 }
01984 
01985 static int mpeg1_decode_sequence(AVCodecContext *avctx,
01986                                  const uint8_t *buf, int buf_size)
01987 {
01988     Mpeg1Context *s1 = avctx->priv_data;
01989     MpegEncContext *s = &s1->mpeg_enc_ctx;
01990     int width,height;
01991     int i, v, j;
01992 
01993     init_get_bits(&s->gb, buf, buf_size*8);
01994 
01995     width = get_bits(&s->gb, 12);
01996     height = get_bits(&s->gb, 12);
01997     if (width <= 0 || height <= 0)
01998         return -1;
01999     s->aspect_ratio_info= get_bits(&s->gb, 4);
02000     if (s->aspect_ratio_info == 0) {
02001         av_log(avctx, AV_LOG_ERROR, "aspect ratio has forbidden 0 value\n");
02002         if (avctx->error_recognition >= FF_ER_COMPLIANT)
02003             return -1;
02004     }
02005     s->frame_rate_index = get_bits(&s->gb, 4);
02006     if (s->frame_rate_index == 0 || s->frame_rate_index > 13)
02007         return -1;
02008     s->bit_rate = get_bits(&s->gb, 18) * 400;
02009     if (get_bits1(&s->gb) == 0) /* marker */
02010         return -1;
02011     s->width = width;
02012     s->height = height;
02013 
02014     s->avctx->rc_buffer_size= get_bits(&s->gb, 10) * 1024*16;
02015     skip_bits(&s->gb, 1);
02016 
02017     /* get matrix */
02018     if (get_bits1(&s->gb)) {
02019         load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
02020     } else {
02021         for(i=0;i<64;i++) {
02022             j = s->dsp.idct_permutation[i];
02023             v = ff_mpeg1_default_intra_matrix[i];
02024             s->intra_matrix[j] = v;
02025             s->chroma_intra_matrix[j] = v;
02026         }
02027     }
02028     if (get_bits1(&s->gb)) {
02029         load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
02030     } else {
02031         for(i=0;i<64;i++) {
02032             int j= s->dsp.idct_permutation[i];
02033             v = ff_mpeg1_default_non_intra_matrix[i];
02034             s->inter_matrix[j] = v;
02035             s->chroma_inter_matrix[j] = v;
02036         }
02037     }
02038 
02039     if(show_bits(&s->gb, 23) != 0){
02040         av_log(s->avctx, AV_LOG_ERROR, "sequence header damaged\n");
02041         return -1;
02042     }
02043 
02044     /* we set MPEG-2 parameters so that it emulates MPEG-1 */
02045     s->progressive_sequence = 1;
02046     s->progressive_frame = 1;
02047     s->picture_structure = PICT_FRAME;
02048     s->frame_pred_frame_dct = 1;
02049     s->chroma_format = 1;
02050     s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG1VIDEO;
02051     avctx->sub_id = 1; /* indicates MPEG-1 */
02052     s->out_format = FMT_MPEG1;
02053     s->swap_uv = 0;//AFAIK VCR2 does not have SEQ_HEADER
02054     if(s->flags & CODEC_FLAG_LOW_DELAY) s->low_delay=1;
02055 
02056     if(s->avctx->debug & FF_DEBUG_PICT_INFO)
02057         av_log(s->avctx, AV_LOG_DEBUG, "vbv buffer: %d, bitrate:%d\n",
02058                s->avctx->rc_buffer_size, s->bit_rate);
02059 
02060     return 0;
02061 }
02062 
02063 static int vcr2_init_sequence(AVCodecContext *avctx)
02064 {
02065     Mpeg1Context *s1 = avctx->priv_data;
02066     MpegEncContext *s = &s1->mpeg_enc_ctx;
02067     int i, v;
02068 
02069     /* start new MPEG-1 context decoding */
02070     s->out_format = FMT_MPEG1;
02071     if (s1->mpeg_enc_ctx_allocated) {
02072         MPV_common_end(s);
02073     }
02074     s->width  = avctx->coded_width;
02075     s->height = avctx->coded_height;
02076     avctx->has_b_frames= 0; //true?
02077     s->low_delay= 1;
02078 
02079     avctx->pix_fmt = mpeg_get_pixelformat(avctx);
02080     avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
02081 
02082     if( avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT || avctx->hwaccel ||
02083         s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU )
02084         if( avctx->idct_algo == FF_IDCT_AUTO )
02085             avctx->idct_algo = FF_IDCT_SIMPLE;
02086 
02087     if (MPV_common_init(s) < 0)
02088         return -1;
02089     exchange_uv(s);//common init reset pblocks, so we swap them here
02090     s->swap_uv = 1;// in case of xvmc we need to swap uv for each MB
02091     s1->mpeg_enc_ctx_allocated = 1;
02092 
02093     for(i=0;i<64;i++) {
02094         int j= s->dsp.idct_permutation[i];
02095         v = ff_mpeg1_default_intra_matrix[i];
02096         s->intra_matrix[j] = v;
02097         s->chroma_intra_matrix[j] = v;
02098 
02099         v = ff_mpeg1_default_non_intra_matrix[i];
02100         s->inter_matrix[j] = v;
02101         s->chroma_inter_matrix[j] = v;
02102     }
02103 
02104     s->progressive_sequence = 1;
02105     s->progressive_frame = 1;
02106     s->picture_structure = PICT_FRAME;
02107     s->frame_pred_frame_dct = 1;
02108     s->chroma_format = 1;
02109     s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG2VIDEO;
02110     avctx->sub_id = 2; /* indicates MPEG-2 */
02111     s1->save_width           = s->width;
02112     s1->save_height          = s->height;
02113     s1->save_progressive_seq = s->progressive_sequence;
02114     return 0;
02115 }
02116 
02117 
02118 static void mpeg_decode_user_data(AVCodecContext *avctx,
02119                                   const uint8_t *p, int buf_size)
02120 {
02121     const uint8_t *buf_end = p+buf_size;
02122 
02123     /* we parse the DTG active format information */
02124     if (buf_end - p >= 5 &&
02125         p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') {
02126         int flags = p[4];
02127         p += 5;
02128         if (flags & 0x80) {
02129             /* skip event id */
02130             p += 2;
02131         }
02132         if (flags & 0x40) {
02133             if (buf_end - p < 1)
02134                 return;
02135             avctx->dtg_active_format = p[0] & 0x0f;
02136         }
02137     }
02138 }
02139 
02140 static void mpeg_decode_gop(AVCodecContext *avctx,
02141                             const uint8_t *buf, int buf_size){
02142     Mpeg1Context *s1 = avctx->priv_data;
02143     MpegEncContext *s = &s1->mpeg_enc_ctx;
02144 
02145     int time_code_hours, time_code_minutes;
02146     int time_code_seconds, time_code_pictures;
02147     int broken_link;
02148 
02149     init_get_bits(&s->gb, buf, buf_size*8);
02150 
02151     skip_bits1(&s->gb); /* drop_frame_flag */
02152 
02153     time_code_hours=get_bits(&s->gb,5);
02154     time_code_minutes = get_bits(&s->gb,6);
02155     skip_bits1(&s->gb);//marker bit
02156     time_code_seconds = get_bits(&s->gb,6);
02157     time_code_pictures = get_bits(&s->gb,6);
02158 
02159     s->closed_gop = get_bits1(&s->gb);
02160     /*broken_link indicate that after editing the
02161       reference frames of the first B-Frames after GOP I-Frame
02162       are missing (open gop)*/
02163     broken_link = get_bits1(&s->gb);
02164 
02165     if(s->avctx->debug & FF_DEBUG_PICT_INFO)
02166         av_log(s->avctx, AV_LOG_DEBUG, "GOP (%2d:%02d:%02d.[%02d]) closed_gop=%d broken_link=%d\n",
02167             time_code_hours, time_code_minutes, time_code_seconds,
02168             time_code_pictures, s->closed_gop, broken_link);
02169 }
02174 int ff_mpeg1_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size, AVCodecParserContext *s)
02175 {
02176     int i;
02177     uint32_t state= pc->state;
02178 
02179     /* EOF considered as end of frame */
02180     if (buf_size == 0)
02181         return 0;
02182 
02183 /*
02184  0  frame start         -> 1/4
02185  1  first_SEQEXT        -> 0/2
02186  2  first field start   -> 3/0
02187  3  second_SEQEXT       -> 2/0
02188  4  searching end
02189 */
02190 
02191     for(i=0; i<buf_size; i++){
02192         assert(pc->frame_start_found>=0 && pc->frame_start_found<=4);
02193         if(pc->frame_start_found&1){
02194             if(state == EXT_START_CODE && (buf[i]&0xF0) != 0x80)
02195                 pc->frame_start_found--;
02196             else if(state == EXT_START_CODE+2){
02197                 if((buf[i]&3) == 3) pc->frame_start_found= 0;
02198                 else                pc->frame_start_found= (pc->frame_start_found+1)&3;
02199             }
02200             state++;
02201         }else{
02202             i= ff_find_start_code(buf+i, buf+buf_size, &state) - buf - 1;
02203             if(pc->frame_start_found==0 && state >= SLICE_MIN_START_CODE && state <= SLICE_MAX_START_CODE){
02204                 i++;
02205                 pc->frame_start_found=4;
02206             }
02207             if(state == SEQ_END_CODE){
02208                 pc->state=-1;
02209                 return i+1;
02210             }
02211             if(pc->frame_start_found==2 && state == SEQ_START_CODE)
02212                 pc->frame_start_found= 0;
02213             if(pc->frame_start_found<4 && state == EXT_START_CODE)
02214                 pc->frame_start_found++;
02215             if(pc->frame_start_found == 4 && (state&0xFFFFFF00) == 0x100){
02216                 if(state < SLICE_MIN_START_CODE || state > SLICE_MAX_START_CODE){
02217                     pc->frame_start_found=0;
02218                     pc->state=-1;
02219                     return i-3;
02220                 }
02221             }
02222             if(pc->frame_start_found == 0 && s && state == PICTURE_START_CODE){
02223                 ff_fetch_timestamp(s, i-3, 1);
02224             }
02225         }
02226     }
02227     pc->state= state;
02228     return END_NOT_FOUND;
02229 }
02230 
02231 static int decode_chunks(AVCodecContext *avctx,
02232                              AVFrame *picture, int *data_size,
02233                              const uint8_t *buf, int buf_size);
02234 
02235 /* handle buffering and image synchronisation */
02236 static int mpeg_decode_frame(AVCodecContext *avctx,
02237                              void *data, int *data_size,
02238                              AVPacket *avpkt)
02239 {
02240     const uint8_t *buf = avpkt->data;
02241     int buf_size = avpkt->size;
02242     Mpeg1Context *s = avctx->priv_data;
02243     AVFrame *picture = data;
02244     MpegEncContext *s2 = &s->mpeg_enc_ctx;
02245     av_dlog(avctx, "fill_buffer\n");
02246 
02247     if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == SEQ_END_CODE)) {
02248         /* special case for last picture */
02249         if (s2->low_delay==0 && s2->next_picture_ptr) {
02250             *picture= *(AVFrame*)s2->next_picture_ptr;
02251             s2->next_picture_ptr= NULL;
02252 
02253             *data_size = sizeof(AVFrame);
02254         }
02255         return buf_size;
02256     }
02257 
02258     if(s2->flags&CODEC_FLAG_TRUNCATED){
02259         int next= ff_mpeg1_find_frame_end(&s2->parse_context, buf, buf_size, NULL);
02260 
02261         if( ff_combine_frame(&s2->parse_context, next, (const uint8_t **)&buf, &buf_size) < 0 )
02262             return buf_size;
02263     }
02264 
02265 #if 0
02266     if (s->repeat_field % 2 == 1) {
02267         s->repeat_field++;
02268         //fprintf(stderr,"\nRepeating last frame: %d -> %d! pict: %d %d", avctx->frame_number-1, avctx->frame_number,
02269         //        s2->picture_number, s->repeat_field);
02270         if (avctx->flags & CODEC_FLAG_REPEAT_FIELD) {
02271             *data_size = sizeof(AVPicture);
02272             goto the_end;
02273         }
02274     }
02275 #endif
02276 
02277     if(s->mpeg_enc_ctx_allocated==0 && avctx->codec_tag == AV_RL32("VCR2"))
02278         vcr2_init_sequence(avctx);
02279 
02280     s->slice_count= 0;
02281 
02282     if(avctx->extradata && !avctx->frame_number)
02283         decode_chunks(avctx, picture, data_size, avctx->extradata, avctx->extradata_size);
02284 
02285     return decode_chunks(avctx, picture, data_size, buf, buf_size);
02286 }
02287 
02288 static int decode_chunks(AVCodecContext *avctx,
02289                              AVFrame *picture, int *data_size,
02290                              const uint8_t *buf, int buf_size)
02291 {
02292     Mpeg1Context *s = avctx->priv_data;
02293     MpegEncContext *s2 = &s->mpeg_enc_ctx;
02294     const uint8_t *buf_ptr = buf;
02295     const uint8_t *buf_end = buf + buf_size;
02296     int ret, input_size;
02297     int last_code= 0;
02298 
02299     for(;;) {
02300         /* find next start code */
02301         uint32_t start_code = -1;
02302         buf_ptr = ff_find_start_code(buf_ptr,buf_end, &start_code);
02303         if (start_code > 0x1ff){
02304             if(s2->pict_type != AV_PICTURE_TYPE_B || avctx->skip_frame <= AVDISCARD_DEFAULT){
02305                 if(HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE)){
02306                     int i;
02307 
02308                     avctx->execute(avctx, slice_decode_thread,  &s2->thread_context[0], NULL, s->slice_count, sizeof(void*));
02309                     for(i=0; i<s->slice_count; i++)
02310                         s2->error_count += s2->thread_context[i]->error_count;
02311                 }
02312 
02313                 if (CONFIG_MPEG_VDPAU_DECODER && avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
02314                     ff_vdpau_mpeg_picture_complete(s2, buf, buf_size, s->slice_count);
02315 
02316                 if (slice_end(avctx, picture)) {
02317                     if(s2->last_picture_ptr || s2->low_delay) //FIXME merge with the stuff in mpeg_decode_slice
02318                         *data_size = sizeof(AVPicture);
02319                 }
02320             }
02321             s2->pict_type= 0;
02322             return FFMAX(0, buf_ptr - buf - s2->parse_context.last_index);
02323         }
02324 
02325         input_size = buf_end - buf_ptr;
02326 
02327         if(avctx->debug & FF_DEBUG_STARTCODE){
02328             av_log(avctx, AV_LOG_DEBUG, "%3X at %td left %d\n", start_code, buf_ptr-buf, input_size);
02329         }
02330 
02331         /* prepare data for next start code */
02332         switch(start_code) {
02333         case SEQ_START_CODE:
02334             if(last_code == 0){
02335             mpeg1_decode_sequence(avctx, buf_ptr,
02336                                     input_size);
02337                 s->sync=1;
02338             }else{
02339                 av_log(avctx, AV_LOG_ERROR, "ignoring SEQ_START_CODE after %X\n", last_code);
02340             }
02341             break;
02342 
02343         case PICTURE_START_CODE:
02344             if (HAVE_THREADS && (avctx->active_thread_type&FF_THREAD_SLICE) && s->slice_count) {
02345                 int i;
02346 
02347                 avctx->execute(avctx, slice_decode_thread,
02348                                s2->thread_context, NULL,
02349                                s->slice_count, sizeof(void*));
02350                 for (i = 0; i < s->slice_count; i++)
02351                     s2->error_count += s2->thread_context[i]->error_count;
02352                 s->slice_count = 0;
02353             }
02354             if(last_code == 0 || last_code == SLICE_MIN_START_CODE){
02355             if(mpeg_decode_postinit(avctx) < 0){
02356                 av_log(avctx, AV_LOG_ERROR, "mpeg_decode_postinit() failure\n");
02357                 return -1;
02358             }
02359 
02360             /* we have a complete image: we try to decompress it */
02361             if(mpeg1_decode_picture(avctx,
02362                                     buf_ptr, input_size) < 0)
02363                 s2->pict_type=0;
02364                 s2->first_slice = 1;
02365             last_code= PICTURE_START_CODE;
02366             }else{
02367                 av_log(avctx, AV_LOG_ERROR, "ignoring pic after %X\n", last_code);
02368             }
02369             break;
02370         case EXT_START_CODE:
02371             init_get_bits(&s2->gb, buf_ptr, input_size*8);
02372 
02373             switch(get_bits(&s2->gb, 4)) {
02374             case 0x1:
02375                 if(last_code == 0){
02376                 mpeg_decode_sequence_extension(s);
02377                 }else{
02378                     av_log(avctx, AV_LOG_ERROR, "ignoring seq ext after %X\n", last_code);
02379                 }
02380                 break;
02381             case 0x2:
02382                 mpeg_decode_sequence_display_extension(s);
02383                 break;
02384             case 0x3:
02385                 mpeg_decode_quant_matrix_extension(s2);
02386                 break;
02387             case 0x7:
02388                 mpeg_decode_picture_display_extension(s);
02389                 break;
02390             case 0x8:
02391                 if(last_code == PICTURE_START_CODE){
02392                 mpeg_decode_picture_coding_extension(s);
02393                 }else{
02394                     av_log(avctx, AV_LOG_ERROR, "ignoring pic cod ext after %X\n", last_code);
02395                 }
02396                 break;
02397             }
02398             break;
02399         case USER_START_CODE:
02400             mpeg_decode_user_data(avctx,
02401                                     buf_ptr, input_size);
02402             break;
02403         case GOP_START_CODE:
02404             if(last_code == 0){
02405             s2->first_field=0;
02406             mpeg_decode_gop(avctx,
02407                                     buf_ptr, input_size);
02408                 s->sync=1;
02409             }else{
02410                 av_log(avctx, AV_LOG_ERROR, "ignoring GOP_START_CODE after %X\n", last_code);
02411             }
02412             break;
02413         default:
02414             if (start_code >= SLICE_MIN_START_CODE &&
02415                 start_code <= SLICE_MAX_START_CODE && last_code!=0) {
02416                 const int field_pic= s2->picture_structure != PICT_FRAME;
02417                 int mb_y= (start_code - SLICE_MIN_START_CODE) << field_pic;
02418                 last_code= SLICE_MIN_START_CODE;
02419 
02420                 if(s2->picture_structure == PICT_BOTTOM_FIELD)
02421                     mb_y++;
02422 
02423                 if (mb_y >= s2->mb_height){
02424                     av_log(s2->avctx, AV_LOG_ERROR, "slice below image (%d >= %d)\n", mb_y, s2->mb_height);
02425                     return -1;
02426                 }
02427 
02428                 if(s2->last_picture_ptr==NULL){
02429                 /* Skip B-frames if we do not have reference frames and gop is not closed */
02430                     if(s2->pict_type==AV_PICTURE_TYPE_B){
02431                         if(!s2->closed_gop)
02432                             break;
02433                     }
02434                 }
02435                 if(s2->pict_type==AV_PICTURE_TYPE_I)
02436                     s->sync=1;
02437                 if(s2->next_picture_ptr==NULL){
02438                 /* Skip P-frames if we do not have a reference frame or we have an invalid header. */
02439                     if(s2->pict_type==AV_PICTURE_TYPE_P && !s->sync) break;
02440                 }
02441                 if(  (avctx->skip_frame >= AVDISCARD_NONREF && s2->pict_type==AV_PICTURE_TYPE_B)
02442                     ||(avctx->skip_frame >= AVDISCARD_NONKEY && s2->pict_type!=AV_PICTURE_TYPE_I)
02443                     || avctx->skip_frame >= AVDISCARD_ALL)
02444                     break;
02445 
02446                 if (!s->mpeg_enc_ctx_allocated) break;
02447 
02448                 if(s2->codec_id == CODEC_ID_MPEG2VIDEO){
02449                     if(mb_y < avctx->skip_top || mb_y >= s2->mb_height - avctx->skip_bottom)
02450                         break;
02451                 }
02452 
02453                 if(!s2->pict_type){
02454                     av_log(avctx, AV_LOG_ERROR, "Missing picture start code\n");
02455                     break;
02456                 }
02457 
02458                 if(s2->first_slice){
02459                     s2->first_slice=0;
02460                     if(mpeg_field_start(s2, buf, buf_size) < 0)
02461                         return -1;
02462                 }
02463                 if(!s2->current_picture_ptr){
02464                     av_log(avctx, AV_LOG_ERROR, "current_picture not initialized\n");
02465                     return -1;
02466                 }
02467 
02468                 if (avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU) {
02469                     s->slice_count++;
02470                     break;
02471                 }
02472 
02473                 if(HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE)){
02474                     int threshold= (s2->mb_height*s->slice_count + avctx->thread_count/2) / avctx->thread_count;
02475                     if(threshold <= mb_y){
02476                         MpegEncContext *thread_context= s2->thread_context[s->slice_count];
02477 
02478                         thread_context->start_mb_y= mb_y;
02479                         thread_context->end_mb_y  = s2->mb_height;
02480                         if(s->slice_count){
02481                             s2->thread_context[s->slice_count-1]->end_mb_y= mb_y;
02482                             ff_update_duplicate_context(thread_context, s2);
02483                         }
02484                         init_get_bits(&thread_context->gb, buf_ptr, input_size*8);
02485                         s->slice_count++;
02486                     }
02487                     buf_ptr += 2; //FIXME add minimum number of bytes per slice
02488                 }else{
02489                     ret = mpeg_decode_slice(s, mb_y, &buf_ptr, input_size);
02490                     emms_c();
02491 
02492                     if(ret < 0){
02493                         if(s2->resync_mb_x>=0 && s2->resync_mb_y>=0)
02494                             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);
02495                     }else{
02496                         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);
02497                     }
02498                 }
02499             }
02500             break;
02501         }
02502     }
02503 }
02504 
02505 static void flush(AVCodecContext *avctx){
02506     Mpeg1Context *s = avctx->priv_data;
02507 
02508     s->sync=0;
02509 
02510     ff_mpeg_flush(avctx);
02511 }
02512 
02513 static int mpeg_decode_end(AVCodecContext *avctx)
02514 {
02515     Mpeg1Context *s = avctx->priv_data;
02516 
02517     if (s->mpeg_enc_ctx_allocated)
02518         MPV_common_end(&s->mpeg_enc_ctx);
02519     return 0;
02520 }
02521 
02522 static const AVProfile mpeg2_video_profiles[] = {
02523     { FF_PROFILE_MPEG2_422,          "4:2:2"              },
02524     { FF_PROFILE_MPEG2_HIGH,         "High"               },
02525     { FF_PROFILE_MPEG2_SS,           "Spatially Scalable" },
02526     { FF_PROFILE_MPEG2_SNR_SCALABLE, "SNR Scalable"       },
02527     { FF_PROFILE_MPEG2_MAIN,         "Main"               },
02528     { FF_PROFILE_MPEG2_SIMPLE,       "Simple"             },
02529     { FF_PROFILE_RESERVED,           "Reserved"           },
02530     { FF_PROFILE_RESERVED,           "Reserved"           },
02531     { FF_PROFILE_UNKNOWN },
02532 };
02533 
02534 
02535 AVCodec ff_mpeg1video_decoder = {
02536     "mpeg1video",
02537     AVMEDIA_TYPE_VIDEO,
02538     CODEC_ID_MPEG1VIDEO,
02539     sizeof(Mpeg1Context),
02540     mpeg_decode_init,
02541     NULL,
02542     mpeg_decode_end,
02543     mpeg_decode_frame,
02544     CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
02545     .flush= flush,
02546     .max_lowres= 3,
02547     .long_name= NULL_IF_CONFIG_SMALL("MPEG-1 video"),
02548     .update_thread_context= ONLY_IF_THREADS_ENABLED(mpeg_decode_update_thread_context)
02549 };
02550 
02551 AVCodec ff_mpeg2video_decoder = {
02552     "mpeg2video",
02553     AVMEDIA_TYPE_VIDEO,
02554     CODEC_ID_MPEG2VIDEO,
02555     sizeof(Mpeg1Context),
02556     mpeg_decode_init,
02557     NULL,
02558     mpeg_decode_end,
02559     mpeg_decode_frame,
02560     CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
02561     .flush= flush,
02562     .max_lowres= 3,
02563     .long_name= NULL_IF_CONFIG_SMALL("MPEG-2 video"),
02564     .profiles = NULL_IF_CONFIG_SMALL(mpeg2_video_profiles),
02565 };
02566 
02567 //legacy decoder
02568 AVCodec ff_mpegvideo_decoder = {
02569     "mpegvideo",
02570     AVMEDIA_TYPE_VIDEO,
02571     CODEC_ID_MPEG2VIDEO,
02572     sizeof(Mpeg1Context),
02573     mpeg_decode_init,
02574     NULL,
02575     mpeg_decode_end,
02576     mpeg_decode_frame,
02577     CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
02578     .flush= flush,
02579     .max_lowres= 3,
02580     .long_name= NULL_IF_CONFIG_SMALL("MPEG-1 video"),
02581 };
02582 
02583 #if CONFIG_MPEG_XVMC_DECODER
02584 static av_cold int mpeg_mc_decode_init(AVCodecContext *avctx){
02585     if( avctx->active_thread_type & FF_THREAD_SLICE )
02586         return -1;
02587     if( !(avctx->slice_flags & SLICE_FLAG_CODED_ORDER) )
02588         return -1;
02589     if( !(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD) ){
02590         av_dlog(avctx, "mpeg12.c: XvMC decoder will work better if SLICE_FLAG_ALLOW_FIELD is set\n");
02591     }
02592     mpeg_decode_init(avctx);
02593 
02594     avctx->pix_fmt = PIX_FMT_XVMC_MPEG2_IDCT;
02595     avctx->xvmc_acceleration = 2;//2 - the blocks are packed!
02596 
02597     return 0;
02598 }
02599 
02600 AVCodec ff_mpeg_xvmc_decoder = {
02601     "mpegvideo_xvmc",
02602     AVMEDIA_TYPE_VIDEO,
02603     CODEC_ID_MPEG2VIDEO_XVMC,
02604     sizeof(Mpeg1Context),
02605     mpeg_mc_decode_init,
02606     NULL,
02607     mpeg_decode_end,
02608     mpeg_decode_frame,
02609     CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED| CODEC_CAP_HWACCEL | CODEC_CAP_DELAY,
02610     .flush= flush,
02611     .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video XvMC (X-Video Motion Compensation)"),
02612 };
02613 
02614 #endif
02615 
02616 #if CONFIG_MPEG_VDPAU_DECODER
02617 AVCodec ff_mpeg_vdpau_decoder = {
02618     "mpegvideo_vdpau",
02619     AVMEDIA_TYPE_VIDEO,
02620     CODEC_ID_MPEG2VIDEO,
02621     sizeof(Mpeg1Context),
02622     mpeg_decode_init,
02623     NULL,
02624     mpeg_decode_end,
02625     mpeg_decode_frame,
02626     CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_HWACCEL_VDPAU | CODEC_CAP_DELAY,
02627     .flush= flush,
02628     .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video (VDPAU acceleration)"),
02629 };
02630 #endif
02631 
02632 #if CONFIG_MPEG1_VDPAU_DECODER
02633 AVCodec ff_mpeg1_vdpau_decoder = {
02634     "mpeg1video_vdpau",
02635     AVMEDIA_TYPE_VIDEO,
02636     CODEC_ID_MPEG1VIDEO,
02637     sizeof(Mpeg1Context),
02638     mpeg_decode_init,
02639     NULL,
02640     mpeg_decode_end,
02641     mpeg_decode_frame,
02642     CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_HWACCEL_VDPAU | CODEC_CAP_DELAY,
02643     .flush= flush,
02644     .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video (VDPAU acceleration)"),
02645 };
02646 #endif
02647