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

libavcodec/mpeg12.c

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

Generated on Fri Feb 22 2013 07:24:27 for FFmpeg by  doxygen 1.7.1