Merge commit '16c22122c788b5e54a2f2e224bd0106429f0714c'
authorMichael Niedermayer <michaelni@gmx.at>
Sun, 28 Jul 2013 09:40:49 +0000 (11:40 +0200)
committerMichael Niedermayer <michaelni@gmx.at>
Sun, 28 Jul 2013 09:40:49 +0000 (11:40 +0200)
* commit '16c22122c788b5e54a2f2e224bd0106429f0714c':
  h264: K&R formatting cosmetics

Conflicts:
libavcodec/h264.c

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

   * @author Michael Niedermayer <michaelni@gmx.at>
   */
  
 +#define UNCHECKED_BITSTREAM_READER 1
 +
+ #include "libavutil/avassert.h"
  #include "libavutil/imgutils.h"
 +#include "libavutil/opt.h"
  #include "internal.h"
  #include "cabac.h"
  #include "cabac_functions.h"
@@@ -265,19 -242,15 +264,19 @@@ static int ref_picture(H264Context *h, 
      memcpy(dst->ref_poc,   src->ref_poc,   sizeof(src->ref_poc));
      memcpy(dst->ref_count, src->ref_count, sizeof(src->ref_count));
  
-     dst->poc                     = src->poc;
-     dst->frame_num               = src->frame_num;
-     dst->mmco_reset              = src->mmco_reset;
-     dst->pic_id                  = src->pic_id;
-     dst->long_ref                = src->long_ref;
-     dst->mbaff                   = src->mbaff;
-     dst->field_picture           = src->field_picture;
-     dst->needs_realloc           = src->needs_realloc;
-     dst->reference               = src->reference;
-     dst->sync                    = src->sync;
-     dst->crop                    = src->crop;
-     dst->crop_left               = src->crop_left;
-     dst->crop_top                = src->crop_top;
+     dst->poc           = src->poc;
+     dst->frame_num     = src->frame_num;
+     dst->mmco_reset    = src->mmco_reset;
+     dst->pic_id        = src->pic_id;
+     dst->long_ref      = src->long_ref;
+     dst->mbaff         = src->mbaff;
+     dst->field_picture = src->field_picture;
+     dst->needs_realloc = src->needs_realloc;
+     dst->reference     = src->reference;
++    dst->sync          = src->sync;
++    dst->crop          = src->crop;
++    dst->crop_left     = src->crop_left;
++    dst->crop_top      = src->crop_top;
  
      return 0;
  fail:
@@@ -651,14 -616,14 +651,14 @@@ static int decode_rbsp_trailing(H264Con
  static inline int get_lowest_part_list_y(H264Context *h, Picture *pic, int n,
                                           int height, int y_offset, int list)
  {
-     int raw_my        = h->mv_cache[list][scan8[n]][1];
+     int raw_my             = h->mv_cache[list][scan8[n]][1];
 -    int filter_height_up   = (raw_my & 3) ? 2 : 0;
      int filter_height_down = (raw_my & 3) ? 3 : 0;
-     int full_my       = (raw_my >> 2) + y_offset;
-     int bottom        = full_my + filter_height_down + height;
+     int full_my            = (raw_my >> 2) + y_offset;
 -    int top                = full_my - filter_height_up;
+     int bottom             = full_my + filter_height_down + height;
  
 -    return FFMAX(abs(top), bottom);
 +    av_assert2(height >= 0);
 +
 +    return FFMAX(0, bottom);
  }
  
  static inline void get_lowest_part_y(H264Context *h, int refs[2][48], int n,
@@@ -1240,8 -1204,8 +1240,8 @@@ static void init_dequant_tables(H264Con
  
  int ff_h264_alloc_tables(H264Context *h)
  {
-     const int big_mb_num    = h->mb_stride * (h->mb_height + 1);
-     const int row_mb_num    = 2*h->mb_stride*FFMAX(h->avctx->thread_count, 1);
+     const int big_mb_num = h->mb_stride * (h->mb_height + 1);
 -    const int row_mb_num = h->mb_stride * 2 * h->avctx->thread_count;
++    const int row_mb_num = 2*h->mb_stride*FFMAX(h->avctx->thread_count, 1);
      int x, y, i;
  
      FF_ALLOCZ_OR_GOTO(h->avctx, h->intra4x4_pred_mode,
@@@ -1683,21 -1613,19 +1683,21 @@@ static int decode_update_thread_context
          memset(&h->mb, 0, sizeof(h->mb));
          memset(&h->mb_luma_dc, 0, sizeof(h->mb_luma_dc));
          memset(&h->mb_padding, 0, sizeof(h->mb_padding));
 -        h->context_initialized = 0;
 -
 -        memset(&h->cur_pic, 0, sizeof(h->cur_pic));
 -        avcodec_get_frame_defaults(&h->cur_pic.f);
 -        h->cur_pic.tf.f = &h->cur_pic.f;
  
-         h->avctx = dst;
-         h->DPB   = NULL;
+         h->avctx             = dst;
+         h->DPB               = NULL;
          h->qscale_table_pool = NULL;
-         h->mb_type_pool = NULL;
-         h->ref_index_pool = NULL;
-         h->motion_val_pool = NULL;
+         h->mb_type_pool      = NULL;
+         h->ref_index_pool    = NULL;
+         h->motion_val_pool   = NULL;
  
 +        if (h1->context_initialized) {
 +        h->context_initialized = 0;
 +
 +        memset(&h->cur_pic, 0, sizeof(h->cur_pic));
 +        avcodec_get_frame_defaults(&h->cur_pic.f);
 +        h->cur_pic.tf.f = &h->cur_pic.f;
 +
          if (ff_h264_alloc_tables(h) < 0) {
              av_log(dst, AV_LOG_ERROR, "Could not allocate memory for h264\n");
              return AVERROR(ENOMEM);
              return ret;
      }
  
-     h->cur_pic_ptr     = REBASE_PICTURE(h1->cur_pic_ptr, h, h1);
+     h->cur_pic_ptr = REBASE_PICTURE(h1->cur_pic_ptr, h, h1);
      unref_picture(h, &h->cur_pic);
 -    if ((ret = ref_picture(h, &h->cur_pic, &h1->cur_pic)) < 0)
 +    if (h1->cur_pic.f.buf[0] && (ret = ref_picture(h, &h->cur_pic, &h1->cur_pic)) < 0)
          return ret;
  
      h->workaround_bugs = h1->workaround_bugs;
@@@ -1827,10 -1750,9 +1827,10 @@@ static int h264_frame_start(H264Contex
      }
      pic = &h->DPB[i];
  
-     pic->reference            = h->droppable ? 0 : h->picture_structure;
+     pic->reference              = h->droppable ? 0 : h->picture_structure;
      pic->f.coded_picture_number = h->coded_picture_number++;
      pic->field_picture          = h->picture_structure != PICT_FRAME;
 +
      /*
       * Zero key_frame here; IDR markings per slice in frame or fields are ORed
       * in later.
@@@ -1901,8 -1830,9 +1901,8 @@@ static void decode_postinit(H264Contex
      Picture *out = h->cur_pic_ptr;
      Picture *cur = h->cur_pic_ptr;
      int i, pics, out_of_order, out_idx;
 -    int invalid = 0, cnt = 0;
  
-     h->cur_pic_ptr->f.pict_type   = h->pict_type;
+     h->cur_pic_ptr->f.pict_type = h->pict_type;
  
      if (h->next_output_pic)
          return;
@@@ -2637,20 -2598,14 +2639,20 @@@ static void idr(H264Context *h
  /* forget old pics after a seek */
  static void flush_change(H264Context *h)
  {
 -    int i;
 -    for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++)
 -        h->last_pocs[i] = INT_MIN;
 +    int i, j;
 +
-     h->outputed_poc = h->next_outputed_poc = INT_MIN;
+     h->outputed_poc          = h->next_outputed_poc = INT_MIN;
      h->prev_interlaced_frame = 1;
      idr(h);
 -    if (h->cur_pic_ptr)
 +
 +    h->prev_frame_num = -1;
 +    if (h->cur_pic_ptr) {
          h->cur_pic_ptr->reference = 0;
 +        for (j=i=0; h->delayed_pic[i]; i++)
 +            if (h->delayed_pic[i] != h->cur_pic_ptr)
 +                h->delayed_pic[j++] = h->delayed_pic[i];
 +        h->delayed_pic[j] = NULL;
 +    }
      h->first_field = 0;
      memset(h->ref_list[0], 0, sizeof(h->ref_list[0]));
      memset(h->ref_list[1], 0, sizeof(h->ref_list[1]));
@@@ -3148,36 -3068,33 +3152,36 @@@ static int h264_slice_header_init(H264C
      } else {
          for (i = 1; i < h->slice_context_count; i++) {
              H264Context *c;
-             c = h->thread_context[i] = av_mallocz(sizeof(H264Context));
-             c->avctx       = h->avctx;
+             c                    = h->thread_context[i] = av_mallocz(sizeof(H264Context));
+             c->avctx             = h->avctx;
 -            c->dsp               = h->dsp;
 +            if (CONFIG_ERROR_RESILIENCE) {
-                 c->dsp         = h->dsp;
++                c->dsp               = h->dsp;
 +            }
-             c->vdsp        = h->vdsp;
-             c->h264dsp     = h->h264dsp;
-             c->h264qpel    = h->h264qpel;
-             c->h264chroma  = h->h264chroma;
-             c->sps         = h->sps;
-             c->pps         = h->pps;
-             c->pixel_shift = h->pixel_shift;
+             c->vdsp              = h->vdsp;
+             c->h264dsp           = h->h264dsp;
+             c->h264qpel          = h->h264qpel;
+             c->h264chroma        = h->h264chroma;
+             c->sps               = h->sps;
+             c->pps               = h->pps;
+             c->pixel_shift       = h->pixel_shift;
 +            c->cur_chroma_format_idc = h->cur_chroma_format_idc;
-             c->width       = h->width;
-             c->height      = h->height;
-             c->linesize    = h->linesize;
-             c->uvlinesize  = h->uvlinesize;
+             c->width             = h->width;
+             c->height            = h->height;
+             c->linesize          = h->linesize;
+             c->uvlinesize        = h->uvlinesize;
 -            c->chroma_x_shift    = h->chroma_x_shift;
 -            c->chroma_y_shift    = h->chroma_y_shift;
 +            c->chroma_x_shift = h->chroma_x_shift;
 +            c->chroma_y_shift = h->chroma_y_shift;
-             c->qscale      = h->qscale;
-             c->droppable   = h->droppable;
+             c->qscale            = h->qscale;
+             c->droppable         = h->droppable;
              c->data_partitioning = h->data_partitioning;
-             c->low_delay   = h->low_delay;
-             c->mb_width    = h->mb_width;
-             c->mb_height   = h->mb_height;
-             c->mb_stride   = h->mb_stride;
-             c->mb_num      = h->mb_num;
-             c->flags       = h->flags;
-             c->workaround_bugs = h->workaround_bugs;
-             c->pict_type   = h->pict_type;
+             c->low_delay         = h->low_delay;
+             c->mb_width          = h->mb_width;
+             c->mb_height         = h->mb_height;
+             c->mb_stride         = h->mb_stride;
+             c->mb_num            = h->mb_num;
+             c->flags             = h->flags;
+             c->workaround_bugs   = h->workaround_bugs;
+             c->pict_type         = h->pict_type;
  
              init_scan_tables(c);
              clone_tables(c, h, i);
@@@ -3347,9 -3253,7 +3351,8 @@@ static int decode_slice_header(H264Cont
      if (h->context_initialized &&
          (h->width  != h->avctx->coded_width   ||
           h->height != h->avctx->coded_height  ||
 +         must_reinit ||
           needs_reinit)) {
          if (h != h0) {
              av_log(h->avctx, AV_LOG_ERROR, "changing width/height on "
                     "slice %d\n", h0->current_slice + 1);
          return -1;
      }
      h->resync_mb_x = h->mb_x =  first_mb_in_slice % h->mb_width;
-     h->resync_mb_y = h->mb_y = (first_mb_in_slice / h->mb_width) << FIELD_OR_MBAFF_PICTURE(h);
+     h->resync_mb_y = h->mb_y = (first_mb_in_slice / h->mb_width) <<
+                                FIELD_OR_MBAFF_PICTURE(h);
      if (h->picture_structure == PICT_BOTTOM_FIELD)
          h->resync_mb_y = h->mb_y = h->mb_y + 1;
 -    assert(h->mb_y < h->mb_height);
 +    av_assert1(h->mb_y < h->mb_height);
  
      if (h->picture_structure == PICT_FRAME) {
          h->curr_pic_num = h->frame_num;
          else
              h->list_count = 1;
      } else {
-         h->list_count = 0;
+         h->list_count   = 0;
          h->ref_count[0] = h->ref_count[1] = 0;
      }
 -
 -    max_refs = h->picture_structure == PICT_FRAME ? 16 : 32;
 -
 -    if (h->ref_count[0] > max_refs || h->ref_count[1] > max_refs) {
 -        av_log(h->avctx, AV_LOG_ERROR, "reference overflow\n");
 -        h->ref_count[0] = h->ref_count[1] = 0;
 -        return AVERROR_INVALIDDATA;
 -    }
 -
 -    if (!default_ref_list_done)
 +    if (slice_type != AV_PICTURE_TYPE_I &&
 +        (h0->current_slice == 0 ||
 +         slice_type != h0->last_slice_type ||
 +         memcmp(h0->last_ref_count, h0->ref_count, sizeof(h0->ref_count)))) {
          ff_h264_fill_default_ref_list(h);
 +    }
  
      if (h->slice_type_nos != AV_PICTURE_TYPE_I &&
          ff_h264_decode_ref_pic_list_reordering(h) < 0) {
                     6 * (h->sps.bit_depth_luma - 8);
  
      h0->last_slice_type = slice_type;
-     h->slice_num = ++h0->current_slice;
 +    memcpy(h0->last_ref_count, h0->ref_count, sizeof(h0->last_ref_count));
 -    if (h->slice_num >= MAX_SLICES) {
 -        av_log(h->avctx, AV_LOG_ERROR,
 -               "Too many slices, increase MAX_SLICES and recompile\n");
+     h->slice_num        = ++h0->current_slice;
 +
 +    if (h->slice_num)
 +        h0->slice_row[(h->slice_num-1)&(MAX_SLICES-1)]= h->resync_mb_y;
 +    if (   h0->slice_row[h->slice_num&(MAX_SLICES-1)] + 3 >= h->resync_mb_y
 +        && h0->slice_row[h->slice_num&(MAX_SLICES-1)] <= h->resync_mb_y
 +        && h->slice_num >= MAX_SLICES) {
 +        //in case of ASO this check needs to be updated depending on how we decide to assign slice numbers in this case
 +        av_log(h->avctx, AV_LOG_WARNING, "Possibly too many slices (%d >= %d), increase MAX_SLICES and recompile if there are artifacts\n", h->slice_num, MAX_SLICES);
      }
  
      for (j = 0; j < 2; j++) {
@@@ -4409,17 -4261,16 +4418,17 @@@ static int decode_slice(struct AVCodecC
                      tprintf(h->avctx, "slice end %d %d\n",
                              get_bits_count(&h->gb), h->gb.size_in_bits);
  
 -                    if (get_bits_left(&h->gb) == 0) {
 +                    if (   get_bits_left(&h->gb) == 0
 +                        || get_bits_left(&h->gb) > 0 && !(h->avctx->err_recognition & AV_EF_AGGRESSIVE)) {
                          er_add_slice(h, h->resync_mb_x, h->resync_mb_y,
-                                         h->mb_x - 1, h->mb_y,
-                                         ER_MB_END);
+                                      h->mb_x - 1, h->mb_y,
+                                      ER_MB_END);
  
                          return 0;
                      } else {
                          er_add_slice(h, h->resync_mb_x, h->resync_mb_y,
-                                         h->mb_x, h->mb_y,
-                                         ER_MB_END);
 -                                     h->mb_x - 1, h->mb_y,
++                                     h->mb_x, h->mb_y,
+                                      ER_MB_END);
  
                          return -1;
                      }
@@@ -4466,13 -4317,9 +4475,13 @@@ static int execute_decode_slices(H264Co
      if (context_count == 1) {
          return decode_slice(avctx, &h);
      } else {
 +        av_assert0(context_count > 0);
          for (i = 1; i < context_count; i++) {
-             hx                    = h->thread_context[i];
+             hx                 = h->thread_context[i];
 -            hx->er.error_count = 0;
 +            if (CONFIG_ERROR_RESILIENCE) {
 +                hx->er.error_count = 0;
 +            }
-             hx->x264_build        = h->x264_build;
++            hx->x264_build     = h->x264_build;
          }
  
          avctx->execute(avctx, decode_slice, h->thread_context,
@@@ -4663,13 -4483,11 +4674,13 @@@ again
                      buf_index = -1;
                      goto end;
                  }
 -                idr(h); // FIXME ensure we don't lose some frames if there is reordering
 +                if(!idr_cleared)
 +                    idr(h); // FIXME ensure we don't lose some frames if there is reordering
 +                idr_cleared = 1;
              case NAL_SLICE:
                  init_get_bits(&hx->gb, ptr, bit_length);
-                 hx->intra_gb_ptr        =
-                     hx->inter_gb_ptr    = &hx->gb;
+                 hx->intra_gb_ptr      =
+                 hx->inter_gb_ptr      = &hx->gb;
                  hx->data_partitioning = 0;
  
                  if ((err = decode_slice_header(hx, h)))
@@@ -4868,8 -4660,8 +4879,8 @@@ static int output_frame(H264Context *h
      for (i = 0; i < 3; i++) {
          int hshift = (i > 0) ? h->chroma_x_shift : 0;
          int vshift = (i > 0) ? h->chroma_y_shift : 0;
 -        int off    = ((h->sps.crop_left >> hshift) << h->pixel_shift) +
 -                     (h->sps.crop_top >> vshift) * dst->linesize[i];
 +        int off    = ((srcp->crop_left >> hshift) << h->pixel_shift) +
-             (srcp->crop_top  >> vshift) * dst->linesize[i];
++                      (srcp->crop_top  >> vshift) * dst->linesize[i];
          dst->data[i] += off;
      }
      return 0;
@@@ -4883,18 -4675,17 +4894,18 @@@ static int decode_frame(AVCodecContext 
      H264Context *h     = avctx->priv_data;
      AVFrame *pict      = data;
      int buf_index      = 0;
 +    Picture *out;
 +    int i, out_idx;
      int ret;
  
-     h->flags  = avctx->flags;
+     h->flags = avctx->flags;
  
      /* end of stream, output what is still in the buffers */
 -out:
      if (buf_size == 0) {
 -        Picture *out;
 -        int i, out_idx;
 + out:
  
          h->cur_pic_ptr = NULL;
 +        h->first_field = 0;
  
          // FIXME factorize this with the output code below
          out     = h->delayed_pic[0];
@@@ -5004,9 -4767,8 +5015,9 @@@ av_cold void ff_h264_free_context(H264C
  
  static av_cold int h264_decode_end(AVCodecContext *avctx)
  {
-     H264Context *h    = avctx->priv_data;
+     H264Context *h = avctx->priv_data;
  
 +    ff_h264_remove_all_refs(h);
      ff_h264_free_context(h);
  
      unref_picture(h, &h->cur_pic);
@@@ -5083,8 -4824,7 +5094,8 @@@ AVCodec ff_h264_vdpau_decoder = 
      .flush          = flush_dpb,
      .long_name      = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (VDPAU acceleration)"),
      .pix_fmts       = (const enum AVPixelFormat[]) { AV_PIX_FMT_VDPAU_H264,
-                                                    AV_PIX_FMT_NONE},
+                                                      AV_PIX_FMT_NONE},
      .profiles       = NULL_IF_CONFIG_SMALL(profiles),
 +    .priv_class     = &h264_vdpau_class,
  };
  #endif