Merge remote-tracking branch 'qatar/master'
authorMichael Niedermayer <michaelni@gmx.at>
Wed, 5 Feb 2014 02:59:37 +0000 (03:59 +0100)
committerMichael Niedermayer <michaelni@gmx.at>
Wed, 5 Feb 2014 02:59:37 +0000 (03:59 +0100)
* qatar/master:
  mpeg: K&R formatting cosmetics

Conflicts:
libavcodec/mpeg12dec.c

Merged-by: Michael Niedermayer <michaelni@gmx.at>
1  2 
libavcodec/mpeg12dec.c

  #include "libavutil/attributes.h"
  #include "libavutil/internal.h"
  #include "libavutil/stereo3d.h"
- #include "internal.h"
  #include "avcodec.h"
+ #include "bytestream.h"
  #include "dsputil.h"
- #include "mpegvideo.h"
  #include "error_resilience.h"
+ #include "internal.h"
  #include "mpeg12.h"
  #include "mpeg12data.h"
- #include "bytestream.h"
- #include "vdpau_internal.h"
- #include "xvmc_internal.h"
+ #include "mpegvideo.h"
  #include "thread.h"
  #include "version.h"
++#include "vdpau_internal.h"
+ #include "xvmc_internal.h"
  
  typedef struct Mpeg1Context {
      MpegEncContext mpeg_enc_ctx;
@@@ -52,9 -51,9 +53,9 @@@
      int slice_count;
      int save_aspect_info;
      int save_width, save_height, save_progressive_seq;
-     AVRational frame_rate_ext;       ///< MPEG-2 specific framerate modificator
-     int sync;                        ///< Did we reach a sync point like a GOP/SEQ/KEYFrame?
+     AVRational frame_rate_ext;  /* MPEG-2 specific framerate modificator */
+     int sync;                   /* Did we reach a sync point like a GOP/SEQ/KEYFrame? */
 -    int closed_gop;             /* GOP is closed */
 +    int tmpgexs;
      int first_slice;
      int extradata_decoded;
  } Mpeg1Context;
@@@ -152,31 -150,36 +152,37 @@@ static inline int mpeg1_decode_block_in
      i = 0;
      {
          OPEN_READER(re, &s->gb);
 +        UPDATE_CACHE(re, &s->gb);
 +        if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
 +            goto end;
 +
          /* now quantify & encode AC coefficients */
          for (;;) {
-             GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
 -            UPDATE_CACHE(re, &s->gb);
+             GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
+                        TEX_VLC_BITS, 2, 0);
  
 -            if (level == 127) {
 -                break;
 -            } else if (level != 0) {
 +            if (level != 0) {
                  i += run;
                  check_scantable_index(s, i);
                  j = scantable[i];
                  level = (level * qscale * quant_matrix[j]) >> 4;
                  level = (level - 1) | 1;
-                 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
+                 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
+                         SHOW_SBITS(re, &s->gb, 1);
 -                LAST_SKIP_BITS(re, &s->gb, 1);
 +                SKIP_BITS(re, &s->gb, 1);
              } else {
                  /* escape */
-                 run = SHOW_UBITS(re, &s->gb, 6) + 1; LAST_SKIP_BITS(re, &s->gb, 6);
+                 run = SHOW_UBITS(re, &s->gb, 6) + 1;
+                 LAST_SKIP_BITS(re, &s->gb, 6);
                  UPDATE_CACHE(re, &s->gb);
-                 level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
+                 level = SHOW_SBITS(re, &s->gb, 8);
+                 SKIP_BITS(re, &s->gb, 8);
                  if (level == -128) {
-                     level = SHOW_UBITS(re, &s->gb, 8) - 256; SKIP_BITS(re, &s->gb, 8);
+                     level = SHOW_UBITS(re, &s->gb, 8) - 256;
 -                    LAST_SKIP_BITS(re, &s->gb, 8);
++                    SKIP_BITS(re, &s->gb, 8);
                  } else if (level == 0) {
-                     level = SHOW_UBITS(re, &s->gb, 8)      ; SKIP_BITS(re, &s->gb, 8);
+                     level = SHOW_UBITS(re, &s->gb, 8);
 -                    LAST_SKIP_BITS(re, &s->gb, 8);
++                    SKIP_BITS(re, &s->gb, 8);
                  }
                  i += run;
                  check_scantable_index(s, i);
@@@ -284,17 -288,13 +297,18 @@@ end
      return 0;
  }
  
- static inline int mpeg1_fast_decode_block_inter(MpegEncContext *s, int16_t *block, int n)
 +/**
 + * Note: this function can read out of range and crash for corrupt streams.
 + * Changing this would eat up any speed benefits it has.
 + * Do not use "fast" flag if you need the code to be robust.
 + */
+ static inline int mpeg1_fast_decode_block_inter(MpegEncContext *s,
+                                                 int16_t *block, int n)
  {
      int level, i, j, run;
-     RLTable *rl = &ff_rl_mpeg1;
-     uint8_t * const scantable = s->intra_scantable.permutated;
-     const int qscale          = s->qscale;
+     RLTable *rl              = &ff_rl_mpeg1;
+     uint8_t *const scantable = s->intra_scantable.permutated;
+     const int qscale         = s->qscale;
  
      {
          OPEN_READER(re, &s->gb);
@@@ -474,18 -479,23 +498,21 @@@ static inline int mpeg2_fast_decode_blo
  
          if (level != 0) {
              i += run;
-             j  = scantable[i];
 -            check_scantable_index(s, i);
+             j = scantable[i];
              level = ((level * 2 + 1) * qscale) >> 1;
-             level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
+             level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
+                     SHOW_SBITS(re, &s->gb, 1);
              SKIP_BITS(re, &s->gb, 1);
          } else {
              /* escape */
-             run = SHOW_UBITS(re, &s->gb, 6) + 1; LAST_SKIP_BITS(re, &s->gb, 6);
+             run = SHOW_UBITS(re, &s->gb, 6) + 1;
+             LAST_SKIP_BITS(re, &s->gb, 6);
              UPDATE_CACHE(re, &s->gb);
-             level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
+             level = SHOW_SBITS(re, &s->gb, 12);
+             SKIP_BITS(re, &s->gb, 12);
  
              i += run;
-             j  = scantable[i];
 -            check_scantable_index(s, i);
+             j = scantable[i];
              if (level < 0) {
                  level = ((-level * 2 + 1) * qscale) >> 1;
                  level = -level;
          }
  
          block[j] = level;
-         if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF || i > 63)
 -        if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
++        if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF || i > 63)
              break;
 +
          UPDATE_CACHE(re, &s->gb);
      }
  end:
@@@ -584,12 -597,8 +615,13 @@@ static inline int mpeg2_decode_block_in
      return 0;
  }
  
- static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s, int16_t *block, int n)
 +/**
 + * Note: this function can read out of range and crash for corrupt streams.
 + * Changing this would eat up any speed benefits it has.
 + * Do not use "fast" flag if you need the code to be robust.
 + */
+ static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s,
+                                                 int16_t *block, int n)
  {
      int level, dc, diff, i, j, run;
      int component;
          /* now quantify & encode AC coefficients */
          for (;;) {
              UPDATE_CACHE(re, &s->gb);
-             GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
+             GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
+                        TEX_VLC_BITS, 2, 0);
  
 -            if (level == 127) {
 +            if (level >= 64 || i > 63) {
                  break;
              } else if (level != 0) {
                  i += run;
-                 j  = scantable[i];
 -                check_scantable_index(s, i);
+                 j = scantable[i];
                  level = (level * qscale * quant_matrix[j]) >> 4;
-                 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
+                 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
+                         SHOW_SBITS(re, &s->gb, 1);
                  LAST_SKIP_BITS(re, &s->gb, 1);
              } else {
                  /* escape */
-                 run = SHOW_UBITS(re, &s->gb, 6) + 1; LAST_SKIP_BITS(re, &s->gb, 6);
+                 run = SHOW_UBITS(re, &s->gb, 6) + 1;
+                 LAST_SKIP_BITS(re, &s->gb, 6);
                  UPDATE_CACHE(re, &s->gb);
-                 level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
+                 level = SHOW_SBITS(re, &s->gb, 12);
+                 SKIP_BITS(re, &s->gb, 12);
                  i += run;
-                 j  = scantable[i];
 -                check_scantable_index(s, i);
+                 j = scantable[i];
                  if (level < 0) {
                      level = (-level * qscale * quant_matrix[j]) >> 4;
                      level = -level;
@@@ -672,14 -687,12 +708,13 @@@ static inline int get_dmv(MpegEncContex
  static inline int get_qscale(MpegEncContext *s)
  {
      int qscale = get_bits(&s->gb, 5);
-     if (s->q_scale_type) {
+     if (s->q_scale_type)
          return non_linear_qscale[qscale];
-     } else {
+     else
          return qscale << 1;
-     }
  }
  
 +
  /* motion type (for MPEG-2) */
  #define MT_FIELD 1
  #define MT_FRAME 2
@@@ -705,13 -719,13 +741,14 @@@ static int mpeg_decode_mb(MpegEncContex
              if (s->mb_x)
                  mb_type = s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride - 1];
              else
-                 mb_type = s->current_picture.mb_type[s->mb_width + (s->mb_y - 1) * s->mb_stride - 1]; // FIXME not sure if this is allowed in MPEG at all
+                 // FIXME not sure if this is allowed in MPEG at all
+                 mb_type = s->current_picture.mb_type[s->mb_width + (s->mb_y - 1) * s->mb_stride - 1];
 -            if (IS_INTRA(mb_type))
 +            if (IS_INTRA(mb_type)) {
 +                av_log(s->avctx, AV_LOG_ERROR, "skip with previntra\n");
                  return -1;
-             s->current_picture.mb_type[s->mb_x + s->mb_y*s->mb_stride] =
 +            }
+             s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride] =
                  mb_type | MB_TYPE_SKIP;
 -//            assert(s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride - 1] & (MB_TYPE_16x16 | MB_TYPE_16x8));
  
              if ((s->mv[0][0][0] | s->mv[0][0][1] | s->mv[1][0][0] | s->mv[1][0][1]) == 0)
                  s->mb_skipped = 1;
          if (s->concealment_motion_vectors) {
              /* just parse them */
              if (s->picture_structure != PICT_FRAME)
-                 skip_bits1(&s->gb); /* field select */
+                 skip_bits1(&s->gb);  /* field select */
  
-             s->mv[0][0][0]= s->last_mv[0][0][0]= s->last_mv[0][1][0] =
-                 mpeg_decode_motion(s, s->mpeg_f_code[0][0], s->last_mv[0][0][0]);
-             s->mv[0][0][1]= s->last_mv[0][0][1]= s->last_mv[0][1][1] =
-                 mpeg_decode_motion(s, s->mpeg_f_code[0][1], s->last_mv[0][0][1]);
+             s->mv[0][0][0]      =
+             s->last_mv[0][0][0] =
+             s->last_mv[0][1][0] = mpeg_decode_motion(s, s->mpeg_f_code[0][0],
+                                                      s->last_mv[0][0][0]);
+             s->mv[0][0][1]      =
+             s->last_mv[0][0][1] =
+             s->last_mv[0][1][1] = mpeg_decode_motion(s, s->mpeg_f_code[0][1],
+                                                      s->last_mv[0][0][1]);
  
              skip_bits1(&s->gb); /* marker */
-         } else
-             memset(s->last_mv, 0, sizeof(s->last_mv)); /* reset mv prediction */
+         } else {
+             /* reset mv prediction */
+             memset(s->last_mv, 0, sizeof(s->last_mv));
+         }
          s->mb_intra = 1;
 -#if FF_API_XVMC
 -FF_DISABLE_DEPRECATION_WARNINGS
          // if 1, we memcpy blocks in xvmcvideo
-         if ((CONFIG_MPEG1_XVMC_HWACCEL || CONFIG_MPEG2_XVMC_HWACCEL) && s->pack_pblocks) {
 -        if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1)
++        if ((CONFIG_MPEG1_XVMC_HWACCEL || CONFIG_MPEG2_XVMC_HWACCEL) && s->pack_pblocks)
              ff_xvmc_pack_pblocks(s, -1); // inter are always full blocks
-         }
 -FF_ENABLE_DEPRECATION_WARNINGS
 -#endif /* FF_API_XVMC */
  
          if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
              if (s->flags2 & CODEC_FLAG2_FAST) {
              s->last_mv[0][0][1] = 0;
              s->last_mv[0][1][0] = 0;
              s->last_mv[0][1][1] = 0;
-             s->mv[0][0][0] = 0;
-             s->mv[0][0][1] = 0;
+             s->mv[0][0][0]      = 0;
+             s->mv[0][0][1]      = 0;
          } else {
 -            assert(mb_type & MB_TYPE_L0L1);
 +            av_assert2(mb_type & MB_TYPE_L0L1);
              // FIXME decide if MBs in field pictures are MB_TYPE_INTERLACED
              /* get additional motion vector type */
-             if (s->picture_structure == PICT_FRAME && s->frame_pred_frame_dct)
 -            if (s->frame_pred_frame_dct) {
++            if (s->picture_structure == PICT_FRAME && s->frame_pred_frame_dct) {
                  motion_type = MT_FRAME;
-             else {
+             else {
                  motion_type = get_bits(&s->gb, 2);
                  if (s->picture_structure == PICT_FRAME && HAS_CBP(mb_type))
                      s->interlaced_dct = get_bits1(&s->gb);
                  }
                  break;
              case MT_DMV:
-                 if(s->progressive_sequence){
++                if (s->progressive_sequence){
 +                    av_log(s->avctx, AV_LOG_ERROR, "MT_DMV in progressive_sequence\n");
 +                    return -1;
 +                }
                  s->mv_type = MV_TYPE_DMV;
                  for (i = 0; i < 2; i++) {
                      if (USES_LIST(mb_type, i)) {
  
              cbp = get_vlc2(&s->gb, ff_mb_pat_vlc.table, MB_PAT_VLC_BITS, 1);
              if (mb_block_count > 6) {
-                  cbp <<= mb_block_count - 6;
-                  cbp  |= get_bits(&s->gb, mb_block_count - 6);
-                  s->dsp.clear_blocks(s->block[6]);
+                 cbp <<= mb_block_count - 6;
+                 cbp  |= get_bits(&s->gb, mb_block_count - 6);
+                 s->dsp.clear_blocks(s->block[6]);
              }
              if (cbp <= 0) {
-                 av_log(s->avctx, AV_LOG_ERROR, "invalid cbp %d at %d %d\n", cbp, s->mb_x, s->mb_y);
+                 av_log(s->avctx, AV_LOG_ERROR,
 -                       "invalid cbp at %d %d\n", s->mb_x, s->mb_y);
++                       "invalid cbp %d at %d %d\n", cbp, s->mb_x, s->mb_y);
                  return -1;
              }
  
-             //if 1, we memcpy blocks in xvmcvideo
-             if ((CONFIG_MPEG1_XVMC_HWACCEL || CONFIG_MPEG2_XVMC_HWACCEL) && s->pack_pblocks) {
 -#if FF_API_XVMC
 -FF_DISABLE_DEPRECATION_WARNINGS
+             // if 1, we memcpy blocks in xvmcvideo
 -            if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1)
++            if ((CONFIG_MPEG1_XVMC_HWACCEL || CONFIG_MPEG2_XVMC_HWACCEL) && s->pack_pblocks)
                  ff_xvmc_pack_pblocks(s, cbp);
-             }
 -FF_ENABLE_DEPRECATION_WARNINGS
 -#endif /* FF_API_XVMC */
  
              if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
                  if (s->flags2 & CODEC_FLAG2_FAST) {
@@@ -1119,31 -1149,19 +1170,30 @@@ static void quant_matrix_rebuild(uint16
  
      memcpy(temp_matrix, matrix, 64 * sizeof(uint16_t));
  
-     for (i = 0; i < 64; i++) {
+     for (i = 0; i < 64; i++)
          matrix[new_perm[i]] = temp_matrix[old_perm[i]];
-     }
  }
  
 -#if FF_API_XVMC
 -static const enum AVPixelFormat pixfmt_xvmc_mpg2_420[] = {
 -    AV_PIX_FMT_XVMC_MPEG2_IDCT,
 -    AV_PIX_FMT_XVMC_MPEG2_MC,
 +static const enum AVPixelFormat mpeg1_hwaccel_pixfmt_list_420[] = {
 +#if CONFIG_MPEG1_XVMC_HWACCEL
 +    AV_PIX_FMT_XVMC,
 +#endif
 +#if CONFIG_MPEG1_VDPAU_HWACCEL
 +    AV_PIX_FMT_VDPAU_MPEG1,
 +    AV_PIX_FMT_VDPAU,
 +#endif
 +    AV_PIX_FMT_YUV420P,
      AV_PIX_FMT_NONE
  };
 -#endif /* FF_API_XVMC */
  
 -static const enum AVPixelFormat mpeg12_hwaccel_pixfmt_list_420[] = {
 +static const enum AVPixelFormat mpeg2_hwaccel_pixfmt_list_420[] = {
 +#if CONFIG_MPEG2_XVMC_HWACCEL
 +    AV_PIX_FMT_XVMC,
 +#endif
 +#if CONFIG_MPEG2_VDPAU_HWACCEL
 +    AV_PIX_FMT_VDPAU_MPEG2,
 +    AV_PIX_FMT_VDPAU,
 +#endif
  #if CONFIG_MPEG2_DXVA2_HWACCEL
      AV_PIX_FMT_DXVA2_VLD,
  #endif
      AV_PIX_FMT_NONE
  };
  
 +static inline int uses_vdpau(AVCodecContext *avctx) {
 +    return avctx->pix_fmt == AV_PIX_FMT_VDPAU_MPEG1 || avctx->pix_fmt == AV_PIX_FMT_VDPAU_MPEG2;
 +}
 +
  static enum AVPixelFormat mpeg_get_pixelformat(AVCodecContext *avctx)
  {
-     Mpeg1Context *s1 = avctx->priv_data;
+     Mpeg1Context *s1  = avctx->priv_data;
      MpegEncContext *s = &s1->mpeg_enc_ctx;
  
-     if(s->chroma_format < 2) {
 -#if FF_API_XVMC
 -FF_DISABLE_DEPRECATION_WARNINGS
 -    if (avctx->xvmc_acceleration)
 -        return avctx->get_format(avctx, pixfmt_xvmc_mpg2_420);
 -FF_ENABLE_DEPRECATION_WARNINGS
 -#endif /* FF_API_XVMC */
 -
+     if (s->chroma_format < 2)
 -        return avctx->get_format(avctx, mpeg12_hwaccel_pixfmt_list_420);
 +        return ff_thread_get_format(avctx,
 +                                avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO ?
 +                                mpeg1_hwaccel_pixfmt_list_420 :
 +                                mpeg2_hwaccel_pixfmt_list_420);
-     } else if(s->chroma_format == 2)
+     else if (s->chroma_format == 2)
          return AV_PIX_FMT_YUV422P;
      else
          return AV_PIX_FMT_YUV444P;
@@@ -1208,10 -1210,8 +1258,8 @@@ static int mpeg_decode_postinit(AVCodec
          s1->save_width           != s->width                ||
          s1->save_height          != s->height               ||
          s1->save_aspect_info     != s->aspect_ratio_info    ||
 -        s1->save_progressive_seq != s->progressive_sequence ||
 +        (s1->save_progressive_seq != s->progressive_sequence && (s->height&31)) ||
-         0)
-     {
+         0) {
          if (s1->mpeg_enc_ctx_allocated) {
              ParseContext pc = s->parse_context;
              s->parse_context.buffer = 0;
@@@ -1326,13 -1332,12 +1378,14 @@@ static int mpeg1_decode_picture(AVCodec
          return -1;
  
      vbv_delay = get_bits(&s->gb, 16);
-     if (s->pict_type == AV_PICTURE_TYPE_P || s->pict_type == AV_PICTURE_TYPE_B) {
 +    s->vbv_delay = vbv_delay;
+     if (s->pict_type == AV_PICTURE_TYPE_P ||
+         s->pict_type == AV_PICTURE_TYPE_B) {
          s->full_pel[0] = get_bits1(&s->gb);
          f_code = get_bits(&s->gb, 3);
 -        if (f_code == 0 && (avctx->err_recognition & AV_EF_BITSTREAM))
 +        if (f_code == 0 && (avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT)))
              return -1;
 +        f_code += !f_code;
          s->mpeg_f_code[0][0] = f_code;
          s->mpeg_f_code[0][1] = f_code;
      }
@@@ -1384,12 -1389,13 +1438,13 @@@ static void mpeg_decode_sequence_extens
      s1->frame_rate_ext.den = get_bits(&s->gb, 5) + 1;
  
      av_dlog(s->avctx, "sequence extension\n");
-     s->codec_id      = s->avctx->codec_id = AV_CODEC_ID_MPEG2VIDEO;
+     s->codec_id = s->avctx->codec_id = AV_CODEC_ID_MPEG2VIDEO;
  
      if (s->avctx->debug & FF_DEBUG_PICT_INFO)
-         av_log(s->avctx, AV_LOG_DEBUG, "profile: %d, level: %d ps: %d cf:%d vbv buffer: %d, bitrate:%d\n",
-                s->avctx->profile, s->avctx->level, s->progressive_sequence, s->chroma_format, s->avctx->rc_buffer_size, s->bit_rate);
+         av_log(s->avctx, AV_LOG_DEBUG,
 -               "profile: %d, level: %d vbv buffer: %d, bitrate:%d\n",
 -               s->avctx->profile, s->avctx->level,
++               "profile: %d, level: %d ps: %d cf:%d vbv buffer: %d, bitrate:%d\n",
++               s->avctx->profile, s->avctx->level, s->progressive_sequence, s->chroma_format,
+                s->avctx->rc_buffer_size, s->bit_rate);
  }
  
  static void mpeg_decode_sequence_display_extension(Mpeg1Context *s1)
@@@ -1519,7 -1526,32 +1580,6 @@@ static void mpeg_decode_picture_coding_
      s->chroma_420_type            = get_bits1(&s->gb);
      s->progressive_frame          = get_bits1(&s->gb);
  
 -    if (s->progressive_sequence && !s->progressive_frame) {
 -        s->progressive_frame = 1;
 -        av_log(s->avctx, AV_LOG_ERROR,
 -               "interlaced frame in progressive sequence, ignoring\n");
 -    }
 -
 -    if (s->picture_structure == 0 ||
 -        (s->progressive_frame && s->picture_structure != PICT_FRAME)) {
 -        av_log(s->avctx, AV_LOG_ERROR,
 -               "picture_structure %d invalid, ignoring\n",
 -               s->picture_structure);
 -        s->picture_structure = PICT_FRAME;
 -    }
 -
 -    if (s->progressive_sequence && !s->frame_pred_frame_dct)
 -        av_log(s->avctx, AV_LOG_WARNING, "invalid frame_pred_frame_dct\n");
 -
 -    if (s->picture_structure == PICT_FRAME) {
 -        s->first_field = 0;
 -        s->v_edge_pos  = 16 * s->mb_height;
 -    } else {
 -        s->first_field ^= 1;
 -        s->v_edge_pos   = 8 * s->mb_height;
 -        memset(s->mbskip_table, 0, s->mb_stride * s->mb_height);
 -    }
--
      if (s->alternate_scan) {
          ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable, ff_alternate_vertical_scan);
          ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable, ff_alternate_vertical_scan);
@@@ -1633,11 -1675,9 +1694,11 @@@ static int mpeg_decode_slice(MpegEncCon
      s->resync_mb_x =
      s->resync_mb_y = -1;
  
 -    assert(mb_y < s->mb_height);
 +    av_assert0(mb_y < s->mb_height);
  
      init_get_bits(&s->gb, *buf, buf_size * 8);
-     if(s->codec_id != AV_CODEC_ID_MPEG1VIDEO && s->mb_height > 2800/16)
++    if (s->codec_id != AV_CODEC_ID_MPEG1VIDEO && s->mb_height > 2800/16)
 +        skip_bits(&s->gb, 3);
  
      ff_mpeg1_clean_buffers(s);
      s->interlaced_dct = 0;
                          motion_y = s->mv[dir][i][1];
                      }
  
-                     s->current_picture.motion_val[dir][xy    ][0] = motion_x;
-                     s->current_picture.motion_val[dir][xy    ][1] = motion_y;
+                     s->current_picture.motion_val[dir][xy][0]     = motion_x;
+                     s->current_picture.motion_val[dir][xy][1]     = motion_y;
                      s->current_picture.motion_val[dir][xy + 1][0] = motion_x;
                      s->current_picture.motion_val[dir][xy + 1][1] = motion_y;
-                     s->current_picture.ref_index [dir][b8_xy    ] =
+                     s->current_picture.ref_index [dir][b8_xy]     =
                      s->current_picture.ref_index [dir][b8_xy + 1] = s->field_select[dir][i];
-                     av_assert2(s->field_select[dir][i] == 0 || s->field_select[dir][i] == 1);
 -                    assert(s->field_select[dir][i] == 0 ||
 -                           s->field_select[dir][i] == 1);
++                    av_assert2(s->field_select[dir][i] == 0 ||
++                               s->field_select[dir][i] == 1);
                  }
-                 xy += wrap;
-                 b8_xy +=2;
+                 xy    += wrap;
+                 b8_xy += 2;
              }
          }
  
          ff_MPV_decode_mb(s, s->block);
  
          if (++s->mb_x >= s->mb_width) {
 -            const int mb_size = 16;
 +            const int mb_size = 16 >> s->avctx->lowres;
  
-             ff_mpeg_draw_horiz_band(s, mb_size*(s->mb_y >> field_pic), mb_size);
+             ff_mpeg_draw_horiz_band(s, mb_size * (s->mb_y >> field_pic), mb_size);
              ff_MPV_report_decode_progress(s);
  
-             s->mb_x = 0;
+             s->mb_x  = 0;
              s->mb_y += 1 << field_pic;
  
              if (s->mb_y >= s->mb_height) {
                  int left   = get_bits_left(&s->gb);
-                 int is_d10 = s->chroma_format == 2 && s->pict_type == AV_PICTURE_TYPE_I && avctx->profile == 0 && avctx->level == 5
-                              && s->intra_dc_precision == 2 && s->q_scale_type == 1 && s->alternate_scan == 0
-                              && s->progressive_frame == 0 /* vbv_delay == 0xBBB || 0xE10*/;
+                 int is_d10 = s->chroma_format == 2 &&
+                              s->pict_type == AV_PICTURE_TYPE_I &&
+                              avctx->profile == 0 && avctx->level == 5 &&
+                              s->intra_dc_precision == 2 &&
+                              s->q_scale_type == 1 && s->alternate_scan == 0 &&
+                              s->progressive_frame == 0
+                              /* vbv_delay == 0xBBB || 0xE10 */;
  
-                 if (left < 0 || (left && show_bits(&s->gb, FFMIN(left, 23)) && !is_d10)
-                     || ((avctx->err_recognition & (AV_EF_BITSTREAM | AV_EF_AGGRESSIVE)) && left > 8)) {
-                     av_log(avctx, AV_LOG_ERROR, "end mismatch left=%d %0X\n", left, show_bits(&s->gb, FFMIN(left, 23)));
 +                if (left >= 32 && !is_d10) {
 +                    GetBitContext gb = s->gb;
 +                    align_get_bits(&gb);
 +                    if (show_bits(&gb, 24) == 0x060E2B) {
 +                        av_log(avctx, AV_LOG_DEBUG, "Invalid MXF data found in video stream\n");
 +                        is_d10 = 1;
 +                    }
 +                }
 +
 -                    ((avctx->err_recognition & AV_EF_BUFFER) && left > 8)) {
+                 if (left < 0 ||
+                     (left && show_bits(&s->gb, FFMIN(left, 23)) && !is_d10) ||
++                    ((avctx->err_recognition & (AV_EF_BITSTREAM | AV_EF_AGGRESSIVE)) && left > 8)) {
+                     av_log(avctx, AV_LOG_ERROR, "end mismatch left=%d %0X\n",
+                            left, show_bits(&s->gb, FFMIN(left, 23)));
                      return -1;
                  } else
                      goto eos;
          }
      }
  eos: // end of slice
-     *buf += (get_bits_count(&s->gb)-1)/8;
 +    if (get_bits_left(&s->gb) < 0)
 +        return AVERROR_INVALIDDATA;
+     *buf += (get_bits_count(&s->gb) - 1) / 8;
      av_dlog(s, "y %d %d %d %d\n", s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y);
      return 0;
  }
@@@ -1889,11 -1946,8 +1974,11 @@@ static int slice_decode_thread(AVCodecC
              return 0;
  
          start_code = -1;
-         buf = avpriv_find_start_code(buf, s->gb.buffer_end, &start_code);
-         mb_y= start_code - SLICE_MIN_START_CODE;
-         if(s->codec_id != AV_CODEC_ID_MPEG1VIDEO && s->mb_height > 2800/16)
+         buf        = avpriv_find_start_code(buf, s->gb.buffer_end, &start_code);
 -        mb_y       = (start_code - SLICE_MIN_START_CODE) << field_pic;
++        mb_y       = start_code - SLICE_MIN_START_CODE;
++        if (s->codec_id != AV_CODEC_ID_MPEG1VIDEO && s->mb_height > 2800/16)
 +            mb_y += (*buf&0xE0)<<2;
 +        mb_y <<= field_pic;
          if (s->picture_structure == PICT_BOTTOM_FIELD)
              mb_y++;
          if (mb_y < 0 || mb_y >= s->end_mb_y)
@@@ -1915,11 -1969,19 +2000,12 @@@ static int slice_end(AVCodecContext *av
  
      if (s->avctx->hwaccel) {
          if (s->avctx->hwaccel->end_frame(s->avctx) < 0)
-             av_log(avctx, AV_LOG_ERROR, "hardware accelerator failed to decode picture\n");
+             av_log(avctx, AV_LOG_ERROR,
+                    "hardware accelerator failed to decode picture\n");
      }
  
 -#if FF_API_XVMC
 -FF_DISABLE_DEPRECATION_WARNINGS
 -    if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
 -        ff_xvmc_field_end(s);
 -FF_ENABLE_DEPRECATION_WARNINGS
 -#endif /* FF_API_XVMC */
 -
      /* end of slice reached */
-     if (/*s->mb_y << field_pic == s->mb_height &&*/ !s->first_field && !s1->first_slice) {
 -    if (/* s->mb_y << field_pic == s->mb_height && */ !s->first_field) {
++    if (/* s->mb_y << field_pic == s->mb_height && */ !s->first_field && !s1->first_slice) {
          /* end of image */
  
          ff_er_frame_end(&s->er);
@@@ -1965,9 -2025,9 +2051,9 @@@ static int mpeg1_decode_sequence(AVCode
      width  = get_bits(&s->gb, 12);
      height = get_bits(&s->gb, 12);
      if (width == 0 || height == 0) {
-         av_log(avctx, AV_LOG_WARNING, "Invalid horizontal or vertical size "
-                "value.\n");
+         av_log(avctx, AV_LOG_WARNING,
+                "Invalid horizontal or vertical size value.\n");
 -        if (avctx->err_recognition & AV_EF_BITSTREAM)
 +        if (avctx->err_recognition & (AV_EF_BITSTREAM | AV_EF_COMPLIANT))
              return AVERROR_INVALIDDATA;
      }
      s->aspect_ratio_info = get_bits(&s->gb, 4);
      s->progressive_sequence = 1;
      s->progressive_frame    = 1;
      s->picture_structure    = PICT_FRAME;
 +    s->first_field          = 0;
      s->frame_pred_frame_dct = 1;
      s->chroma_format        = 1;
-     s->codec_id             = s->avctx->codec_id = AV_CODEC_ID_MPEG1VIDEO;
+     s->codec_id             =
+     s->avctx->codec_id      = AV_CODEC_ID_MPEG1VIDEO;
      s->out_format           = FMT_MPEG1;
 +    s->swap_uv              = 0; // AFAIK VCR2 does not have SEQ_HEADER
      if (s->flags & CODEC_FLAG_LOW_DELAY)
          s->low_delay = 1;
  
@@@ -2045,15 -2104,22 +2132,15 @@@ static int vcr2_init_sequence(AVCodecCo
      s->out_format = FMT_MPEG1;
      if (s1->mpeg_enc_ctx_allocated) {
          ff_MPV_common_end(s);
 +        s1->mpeg_enc_ctx_allocated = 0;
      }
-     s->width  = avctx->coded_width;
-     s->height = avctx->coded_height;
+     s->width            = avctx->coded_width;
+     s->height           = avctx->coded_height;
      avctx->has_b_frames = 0; // true?
-     s->low_delay = 1;
+     s->low_delay        = 1;
  
      avctx->pix_fmt = mpeg_get_pixelformat(avctx);
 -    avctx->hwaccel = ff_find_hwaccel(avctx);
 -
 -#if FF_API_XVMC
 -    if ((avctx->pix_fmt == AV_PIX_FMT_XVMC_MPEG2_IDCT || avctx->hwaccel) &&
 -        avctx->idct_algo == FF_IDCT_AUTO)
 -#else
 -    if (avctx->hwaccel && avctx->idct_algo == FF_IDCT_AUTO)
 -#endif /* FF_API_XVMC */
 -        avctx->idct_algo = FF_IDCT_SIMPLE;
 +    setup_hwaccel_for_pixfmt(avctx);
  
      if (ff_MPV_common_init(s) < 0)
          return -1;
@@@ -2146,22 -2204,8 +2231,22 @@@ static int mpeg_decode_a53_cc(AVCodecCo
  static void mpeg_decode_user_data(AVCodecContext *avctx,
                                    const uint8_t *p, int buf_size)
  {
 +    Mpeg1Context *s = avctx->priv_data;
      const uint8_t *buf_end = p + buf_size;
  
-     if(buf_size > 29){
++    if (buf_size > 29){
 +        int i;
 +        for(i=0; i<20; i++)
-             if(!memcmp(p+i, "\0TMPGEXS\0", 9)){
++            if (!memcmp(p+i, "\0TMPGEXS\0", 9)){
 +                s->tmpgexs= 1;
 +            }
 +
 +/*        for(i=0; !(!p[i-2] && !p[i-1] && p[i]==1) && i<buf_size; i++){
 +            av_log(avctx, AV_LOG_ERROR, "%c", p[i]);
 +        }
 +            av_log(avctx, AV_LOG_ERROR, "\n");*/
 +    }
 +
      /* we parse the DTG active format information */
      if (buf_end - p >= 5 &&
          p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') {
              S3D_video_format_type == 0x23) {
              Mpeg1Context *s1   = avctx->priv_data;
              MpegEncContext *s  = &s1->mpeg_enc_ctx;
 -            AVStereo3D *stereo =
 +            AVStereo3D *stereo;
 +            if (!s->current_picture_ptr)
 +                return;
 +
-             stereo = av_stereo3d_create_side_data(&s->current_picture_ptr->f);
++            stereo =
+                 av_stereo3d_create_side_data(&s->current_picture_ptr->f);
              if (!stereo)
                  return;
  
@@@ -2221,31 -2262,36 +2307,30 @@@ static void mpeg_decode_gop(AVCodecCont
  {
      Mpeg1Context *s1  = avctx->priv_data;
      MpegEncContext *s = &s1->mpeg_enc_ctx;
 -
 -    int time_code_hours, time_code_minutes;
 -    int time_code_seconds, time_code_pictures;
      int broken_link;
 +    int64_t tc;
  
-     init_get_bits(&s->gb, buf, buf_size*8);
+     init_get_bits(&s->gb, buf, buf_size * 8);
  
 -    skip_bits1(&s->gb); /* drop_frame_flag */
 -
 -    time_code_hours   = get_bits(&s->gb, 5);
 -    time_code_minutes = get_bits(&s->gb, 6);
 -    skip_bits1(&s->gb); // marker bit
 -    time_code_seconds  = get_bits(&s->gb, 6);
 -    time_code_pictures = get_bits(&s->gb, 6);
 +    tc = avctx->timecode_frame_start = get_bits(&s->gb, 25);
  
 -    s1->closed_gop = get_bits1(&s->gb);
 +    s->closed_gop = get_bits1(&s->gb);
-     /*broken_link indicate that after editing the
-       reference frames of the first B-Frames after GOP I-Frame
-       are missing (open gop)*/
+     /* broken_link indicate that after editing the
+      * reference frames of the first B-Frames after GOP I-Frame
+      * are missing (open gop) */
      broken_link = get_bits1(&s->gb);
  
 -    if (s->avctx->debug & FF_DEBUG_PICT_INFO)
 +    if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
 +        char tcbuf[AV_TIMECODE_STR_SIZE];
 +        av_timecode_make_mpeg_tc_string(tcbuf, tc);
          av_log(s->avctx, AV_LOG_DEBUG,
 -               "GOP (%2d:%02d:%02d.[%02d]) closed_gop=%d broken_link=%d\n",
 -               time_code_hours, time_code_minutes, time_code_seconds,
 -               time_code_pictures, s1->closed_gop, broken_link);
 +               "GOP (%s) closed_gop=%d broken_link=%d\n",
 +               tcbuf, s->closed_gop, broken_link);
 +    }
  }
  
- static int decode_chunks(AVCodecContext *avctx,
-                          AVFrame *picture, int *got_output,
-                          const uint8_t *buf, int buf_size)
+ static int decode_chunks(AVCodecContext *avctx, AVFrame *picture,
+                          int *got_output, const uint8_t *buf, int buf_size)
  {
      Mpeg1Context *s = avctx->priv_data;
      MpegEncContext *s2 = &s->mpeg_enc_ctx;
          buf_ptr = avpriv_find_start_code(buf_ptr, buf_end, &start_code);
          if (start_code > 0x1ff) {
              if (!skip_frame) {
-                 if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE) &&
+                 if (HAVE_THREADS &&
+                     (avctx->active_thread_type & FF_THREAD_SLICE) &&
                      !avctx->hwaccel) {
                      int i;
 +                    av_assert0(avctx->thread_count > 1);
  
-                     avctx->execute(avctx, slice_decode_thread,  &s2->thread_context[0], NULL, s->slice_count, sizeof(void*));
+                     avctx->execute(avctx, slice_decode_thread,
+                                    &s2->thread_context[0], NULL,
+                                    s->slice_count, sizeof(void *));
                      for (i = 0; i < s->slice_count; i++)
                          s2->er.error_count += s2->thread_context[i]->er.error_count;
                  }
          case SEQ_START_CODE:
              if (last_code == 0) {
                  mpeg1_decode_sequence(avctx, buf_ptr, input_size);
-                 if(buf != avctx->extradata)
-                     s->sync=1;
 -                s->sync = 1;
++                if (buf != avctx->extradata)
++                    s->sync = 1;
              } else {
-                 av_log(avctx, AV_LOG_ERROR, "ignoring SEQ_START_CODE after %X\n", last_code);
+                 av_log(avctx, AV_LOG_ERROR,
+                        "ignoring SEQ_START_CODE after %X\n", last_code);
                  if (avctx->err_recognition & AV_EF_EXPLODE)
                      return AVERROR_INVALIDDATA;
              }
                  return AVERROR_INVALIDDATA;
              }
  
-             if(s->tmpgexs){
++            if (s->tmpgexs){
 +                s2->intra_dc_precision= 3;
 +                s2->intra_matrix[0]= 1;
 +            }
              if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE) &&
                  !avctx->hwaccel && s->slice_count) {
                  int i;
              break;
          default:
              if (start_code >= SLICE_MIN_START_CODE &&
 +                start_code <= SLICE_MAX_START_CODE && last_code == PICTURE_START_CODE) {
-                     av_log(s2->avctx, AV_LOG_ERROR, "interlaced frame in progressive sequence, ignoring\n");
 +                if (s2->progressive_sequence && !s2->progressive_frame) {
 +                    s2->progressive_frame = 1;
-                 if (s2->picture_structure == 0 || (s2->progressive_frame && s2->picture_structure != PICT_FRAME)) {
-                     av_log(s2->avctx, AV_LOG_ERROR, "picture_structure %d invalid, ignoring\n", s2->picture_structure);
++                    av_log(s2->avctx, AV_LOG_ERROR,
++                           "interlaced frame in progressive sequence, ignoring\n");
 +                }
 +
-                 if (s2->progressive_sequence && !s2->frame_pred_frame_dct) {
++                if (s2->picture_structure == 0 ||
++                    (s2->progressive_frame && s2->picture_structure != PICT_FRAME)) {
++                    av_log(s2->avctx, AV_LOG_ERROR,
++                           "picture_structure %d invalid, ignoring\n",
++                           s2->picture_structure);
 +                    s2->picture_structure = PICT_FRAME;
 +                }
 +
-                 }
++                if (s2->progressive_sequence && !s2->frame_pred_frame_dct)
 +                    av_log(s2->avctx, AV_LOG_WARNING, "invalid frame_pred_frame_dct\n");
 +
 +                if (s2->picture_structure == PICT_FRAME) {
 +                    s2->first_field = 0;
 +                    s2->v_edge_pos  = 16 * s2->mb_height;
 +                } else {
 +                    s2->first_field ^= 1;
 +                    s2->v_edge_pos   = 8 * s2->mb_height;
 +                    memset(s2->mbskip_table, 0, s2->mb_stride * s2->mb_height);
 +                }
 +            }
 +            if (start_code >= SLICE_MIN_START_CODE &&
                  start_code <= SLICE_MAX_START_CODE && last_code != 0) {
                  const int field_pic = s2->picture_structure != PICT_FRAME;
 -                int mb_y = (start_code - SLICE_MIN_START_CODE) << field_pic;
 +                int mb_y = start_code - SLICE_MIN_START_CODE;
                  last_code = SLICE_MIN_START_CODE;
-                 if(s2->codec_id != AV_CODEC_ID_MPEG1VIDEO && s2->mb_height > 2800/16)
++                if (s2->codec_id != AV_CODEC_ID_MPEG1VIDEO && s2->mb_height > 2800/16)
 +                    mb_y += (*buf_ptr&0xE0)<<2;
  
 +                mb_y <<= field_pic;
                  if (s2->picture_structure == PICT_BOTTOM_FIELD)
                      mb_y++;
  
 +                if (buf_end - buf_ptr < 2) {
 +                    av_log(s2->avctx, AV_LOG_ERROR, "slice too small\n");
 +                    return AVERROR_INVALIDDATA;
 +                }
 +
                  if (mb_y >= s2->mb_height) {
-                     av_log(s2->avctx, AV_LOG_ERROR, "slice below image (%d >= %d)\n", mb_y, s2->mb_height);
+                     av_log(s2->avctx, AV_LOG_ERROR,
+                            "slice below image (%d >= %d)\n", mb_y, s2->mb_height);
                      return -1;
                  }
  
                  if (s2->last_picture_ptr == NULL) {
-                 /* Skip B-frames if we do not have reference frames and gop is not closed */
+                     /* Skip B-frames if we do not have reference frames and
+                      * GOP is not closed. */
                      if (s2->pict_type == AV_PICTURE_TYPE_B) {
 -                        if (!s->closed_gop) {
 +                        if (!s2->closed_gop) {
                              skip_frame = 1;
                              break;
                          }
                      }
                  }
 -                if (s2->pict_type == AV_PICTURE_TYPE_I)
 +                if (s2->pict_type == AV_PICTURE_TYPE_I || (s2->flags2 & CODEC_FLAG2_SHOW_ALL))
-                     s->sync=1;
+                     s->sync = 1;
                  if (s2->next_picture_ptr == NULL) {
-                 /* Skip P-frames if we do not have a reference frame or we have an invalid header. */
+                     /* Skip P-frames if we do not have a reference frame or
+                      * we have an invalid header. */
                      if (s2->pict_type == AV_PICTURE_TYPE_P && !s->sync) {
                          skip_frame = 1;
                          break;
                      return AVERROR_INVALIDDATA;
                  }
  
-                 if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE) &&
 +                if (uses_vdpau(avctx)) {
 +                    s->slice_count++;
 +                    break;
 +                }
 +
+                 if (HAVE_THREADS &&
+                     (avctx->active_thread_type & FF_THREAD_SLICE) &&
                      !avctx->hwaccel) {
                      int threshold = (s2->mb_height * s->slice_count +
                                       s2->slice_context_count / 2) /
      }
  }
  
- static int mpeg_decode_frame(AVCodecContext *avctx,
-                              void *data, int *got_output,
-                              AVPacket *avpkt)
+ static int mpeg_decode_frame(AVCodecContext *avctx, void *data,
+                              int *got_output, AVPacket *avpkt)
  {
      const uint8_t *buf = avpkt->data;
 +    int ret;
      int buf_size = avpkt->size;
      Mpeg1Context *s = avctx->priv_data;
      AVFrame *picture = data;
      s->slice_count = 0;
  
      if (avctx->extradata && !s->extradata_decoded) {
-         ret = decode_chunks(avctx, picture, got_output, avctx->extradata, avctx->extradata_size);
-         if(*got_output) {
 -        int ret = decode_chunks(avctx, picture, got_output,
 -                                avctx->extradata, avctx->extradata_size);
++        ret = decode_chunks(avctx, picture, got_output,
++                            avctx->extradata, avctx->extradata_size);
++        if (*got_output) {
 +            av_log(avctx, AV_LOG_ERROR, "picture in extradata\n");
 +            *got_output = 0;
 +        }
          s->extradata_decoded = 1;
 -        if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
 +        if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE)) {
 +            s2->current_picture_ptr = NULL;
              return ret;
 +        }
      }
  
 -    return decode_chunks(avctx, picture, got_output, buf, buf_size);
 +    ret = decode_chunks(avctx, picture, got_output, buf, buf_size);
 +    if (ret<0 || *got_output)
 +        s2->current_picture_ptr = NULL;
 +
 +    return ret;
  }
  
  static void flush(AVCodecContext *avctx)
  {
      Mpeg1Context *s = avctx->priv_data;
  
-     s->sync=0;
+     s->sync       = 0;
 -    s->closed_gop = 0;
  
      ff_mpeg_flush(avctx);
  }