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