Merge commit '2d09b36c0379fcda8f984bc8ad8816c8326fd7bd'
authorMichael Niedermayer <michaelni@gmx.at>
Sun, 21 Oct 2012 14:03:31 +0000 (16:03 +0200)
committerMichael Niedermayer <michaelni@gmx.at>
Sun, 21 Oct 2012 14:13:55 +0000 (16:13 +0200)
* commit '2d09b36c0379fcda8f984bc8ad8816c8326fd7bd':
  doc/platform: Add info on shared builds with MSVC
  doc/platform: Move a caveat down to the notes section
  ARM: reinstate optimised intmath.h
  ffv1: update to ffv1 version 3

Conflicts:
doc/platform.texi
libavcodec/ffv1.c
libavcodec/ffv1.h
libavcodec/ffv1dec.c
libavcodec/ffv1enc.c

Merged-by: Michael Niedermayer <michaelni@gmx.at>
1  2 
libavcodec/ffv1.c
libavcodec/ffv1.h
libavcodec/ffv1dec.c
libavcodec/ffv1enc.c
libavutil/intmath.h

@@@ -67,46 -159,36 +67,47 @@@ int ffv1_init_slice_state(FFV1Context *
  {
      int j;
  
-         fs->plane_count  = f->plane_count;
-         fs->transparency = f->transparency;
-         for (j = 0; j < f->plane_count; j++) {
-             PlaneContext *const p = &fs->plane[j];
-             if (fs->ac) {
-                 if (!p->state)
-                     p->state = av_malloc(CONTEXT_SIZE * p->context_count *
-                                          sizeof(uint8_t));
-                 if (!p->state)
-                     return AVERROR(ENOMEM);
-             } else {
-                 if (!p->vlc_state)
-                     p->vlc_state = av_malloc(p->context_count * sizeof(VlcState));
-                 if (!p->vlc_state)
-                     return AVERROR(ENOMEM);
-             }
+     fs->plane_count  = f->plane_count;
+     fs->transparency = f->transparency;
+     for (j = 0; j < f->plane_count; j++) {
+         PlaneContext *const p = &fs->plane[j];
+         if (fs->ac) {
+             if (!p->state)
+                 p->state = av_malloc(CONTEXT_SIZE * p->context_count *
+                                      sizeof(uint8_t));
+             if (!p->state)
+                 return AVERROR(ENOMEM);
+         } else {
+             if (!p->vlc_state)
+                 p->vlc_state = av_malloc(p->context_count * sizeof(VlcState));
+             if (!p->vlc_state)
+                 return AVERROR(ENOMEM);
          }
+     }
  
-         if (fs->ac > 1) {
-             // FIXME: only redo if state_transition changed
-             for (j = 1; j < 256; j++) {
-                 fs->c. one_state[      j] = f->state_transition[j];
-                 fs->c.zero_state[256 - j] = 256 - fs->c.one_state[j];
-             }
+     if (fs->ac > 1) {
+         //FIXME only redo if state_transition changed
+         for (j = 1; j < 256; j++) {
 -            fs->c.one_state[j]        = f->state_transition[j];
++            fs->c. one_state[      j] = f->state_transition[j];
+             fs->c.zero_state[256 - j] = 256 - fs->c.one_state[j];
          }
+     }
  
      return 0;
  }
  
- int ffv1_init_slices_state(FFV1Context *f) {
-     int i;
++int ffv1_init_slices_state(FFV1Context *f)
++{
++    int i, ret;
 +    for (i = 0; i < f->slice_count; i++) {
 +        FFV1Context *fs = f->slice_context[i];
-         if (ffv1_init_slice_state(f, fs) < 0)
-             return -1;
++        if ((ret = ffv1_init_slice_state(f, fs)) < 0)
++            return AVERROR(ENOMEM);
 +    }
 +    return 0;
 +}
 +
  av_cold int ffv1_init_slice_contexts(FFV1Context *f)
  {
      int i;
@@@ -156,30 -239,29 +158,30 @@@ void ffv1_clear_slice_state(FFV1Contex
  {
      int i, j;
  
-         for (i = 0; i < f->plane_count; i++) {
-             PlaneContext *p = &fs->plane[i];
-             p->interlace_bit_state[0] = 128;
-             p->interlace_bit_state[1] = 128;
-             if (fs->ac) {
-                 if (f->initial_states[p->quant_table_index]) {
-                     memcpy(p->state, f->initial_states[p->quant_table_index],
-                            CONTEXT_SIZE * p->context_count);
-                 } else
-                     memset(p->state, 128, CONTEXT_SIZE * p->context_count);
-             } else {
-                 for (j = 0; j < p->context_count; j++) {
-                     p->vlc_state[j].drift     = 0;
-                     p->vlc_state[j].error_sum = 4; // FFMAX((RANGE + 32)/64, 2);
-                     p->vlc_state[j].bias      = 0;
-                     p->vlc_state[j].count     = 1;
-                 }
+     for (i = 0; i < f->plane_count; i++) {
+         PlaneContext *p = &fs->plane[i];
+         p->interlace_bit_state[0] = 128;
+         p->interlace_bit_state[1] = 128;
+         if (fs->ac) {
+             if (f->initial_states[p->quant_table_index]) {
+                 memcpy(p->state, f->initial_states[p->quant_table_index],
+                        CONTEXT_SIZE * p->context_count);
+             } else
+                 memset(p->state, 128, CONTEXT_SIZE * p->context_count);
+         } else {
+             for (j = 0; j < p->context_count; j++) {
+                 p->vlc_state[j].drift     = 0;
+                 p->vlc_state[j].error_sum = 4;    //FFMAX((RANGE + 32)/64, 2);
+                 p->vlc_state[j].bias      = 0;
+                 p->vlc_state[j].count     = 1;
              }
          }
+     }
  }
  
 +
  av_cold int ffv1_close(AVCodecContext *avctx)
  {
      FFV1Context *s = avctx->priv_data;
@@@ -120,17 -106,8 +123,16 @@@ typedef struct FFV1Context 
      int slice_height;
      int slice_x;
      int slice_y;
-     int bits_per_raw_sample;
  } FFV1Context;
  
 +int ffv1_common_init(AVCodecContext *avctx);
 +int ffv1_init_slice_state(FFV1Context *f, FFV1Context *fs);
 +int ffv1_init_slices_state(FFV1Context *f);
 +int ffv1_init_slice_contexts(FFV1Context *f);
 +int ffv1_allocate_initial_states(FFV1Context *f);
 +void ffv1_clear_slice_state(FFV1Context *f, FFV1Context *fs);
 +int ffv1_close(AVCodecContext *avctx);
 +
  static av_always_inline int fold(int diff, int bits)
  {
      if (bits == 8)
@@@ -215,9 -213,12 +215,10 @@@ static void decode_rgb_frame(FFV1Contex
  {
      int x, y, p;
      int16_t *sample[4][2];
--    int lbd  = s->avctx->bits_per_raw_sample <= 8;
-     int bits = s->avctx->bits_per_raw_sample > 0 ? s->avctx->bits_per_raw_sample : 8;
 -    int bits = s->avctx->bits_per_raw_sample > 0
 -               ? s->avctx->bits_per_raw_sample
 -               : 8;
++    int lbd    = s->avctx->bits_per_raw_sample <= 8;
++    int bits   = s->avctx->bits_per_raw_sample > 0 ? s->avctx->bits_per_raw_sample : 8;
      int offset = 1 << bits;
      for (x = 0; x < 4; x++) {
          sample[x][0] = s->sample_buffer +  x * 2      * (w + 6) + 3;
          sample[x][1] = s->sample_buffer + (x * 2 + 1) * (w + 6) + 3;
              sample[p][0] = sample[p][1];
              sample[p][1] = temp;
  
 -            sample[p][1][-1] = sample[p][0][0];
 -            sample[p][0][w]  = sample[p][0][w - 1];
 +            sample[p][1][-1]= sample[p][0][0  ];
 +            sample[p][0][ w]= sample[p][0][w-1];
              if (lbd)
-                 decode_line(s, w, sample[p], (p+1)/2, 9);
 -                decode_line(s, w, sample[p], (p + 1) / 2, 9);
++                decode_line(s, w, sample[p], (p + 1)/2, 9);
              else
-                 decode_line(s, w, sample[p], (p+1)/2, bits+1);
 -                decode_line(s, w, sample[p], (p + 1) / 2, bits + 1);
++                decode_line(s, w, sample[p], (p + 1)/2, bits + 1);
          }
          for (x = 0; x < w; x++) {
              int g = sample[0][1][x];
@@@ -324,36 -327,36 +325,37 @@@ static int decode_slice_header(FFV1Cont
  
  static int decode_slice(AVCodecContext *c, void *arg)
  {
-     FFV1Context *fs  = *(void **)arg;
-     FFV1Context *f   = fs->avctx->priv_data;
-     int width, height, x, y;
 -    FFV1Context *fs = *(void **)arg;
 -    FFV1Context *f  = fs->avctx->priv_data;
++    FFV1Context *fs   = *(void **)arg;
++    FFV1Context *f    = fs->avctx->priv_data;
+     int width, height, x, y, ret;
 -    const int ps = (av_pix_fmt_desc_get(c->pix_fmt)->flags & PIX_FMT_PLANAR)
 -                   ? (c->bits_per_raw_sample > 8) + 1
 -                   : 4;
 -    AVFrame *const p = &f->picture;
 +    const int ps      = av_pix_fmt_desc_get(c->pix_fmt)->comp[0].step_minus1 + 1;
 +    AVFrame * const p = &f->picture;
  
      if (f->version > 2) {
 +        if (ffv1_init_slice_state(f, fs) < 0)
 +            return AVERROR(ENOMEM);
          if (decode_slice_header(f, fs) < 0) {
              fs->slice_damaged = 1;
              return AVERROR_INVALIDDATA;
          }
      }
-     if (ffv1_init_slice_state(f, fs) < 0)
-         return AVERROR(ENOMEM);
+     if ((ret = ffv1_init_slice_state(f, fs)) < 0)
+         return ret;
      if (f->picture.key_frame)
          ffv1_clear_slice_state(f, fs);
-     width = fs->slice_width;
-     height= fs->slice_height;
-     x= fs->slice_x;
-     y= fs->slice_y;
++
+     width  = fs->slice_width;
+     height = fs->slice_height;
+     x      = fs->slice_x;
+     y      = fs->slice_y;
  
      if (!fs->ac) {
          if (f->version == 3 && f->minor_version > 1 || f->version > 3)
-             get_rac(&fs->c, (uint8_t[]) {129});
-         fs->ac_byte_count = f->version > 2 || (!x&&!y) ? fs->c.bytestream - fs->c.bytestream_start - 1 : 0;
+             get_rac(&fs->c, (uint8_t[]) { 129 });
+         fs->ac_byte_count = f->version > 2 || (!x && !y) ? fs->c.bytestream - fs->c.bytestream_start - 1 : 0;
 -        init_get_bits(&fs->gb, fs->c.bytestream_start + fs->ac_byte_count,
 -                      (fs->c.bytestream_end - fs->c.bytestream_start -
 -                       fs->ac_byte_count) * 8);
 +        init_get_bits(&fs->gb,
 +                      fs->c.bytestream_start + fs->ac_byte_count,
 +                      (fs->c.bytestream_end - fs->c.bytestream_start - fs->ac_byte_count) * 8);
      }
  
      av_assert1(width && height);
          const int chroma_height = -((-height) >> f->chroma_v_shift);
          const int cx            = x >> f->chroma_h_shift;
          const int cy            = y >> f->chroma_v_shift;
 -        decode_plane(fs, p->data[0] + ps * x + y * p->linesize[0], width,
 -                     height, p->linesize[0],
 -                     0);
 +        decode_plane(fs, p->data[0] + ps*x + y*p->linesize[0], width, height, p->linesize[0], 0);
  
          if (f->chroma_planes) {
 -            decode_plane(fs, p->data[1] + ps * cx + cy * p->linesize[1],
 -                         chroma_width, chroma_height, p->linesize[1],
 -                         1);
 -            decode_plane(fs, p->data[2] + ps * cx + cy * p->linesize[2],
 -                         chroma_width, chroma_height, p->linesize[2],
 -                         1);
 +            decode_plane(fs, p->data[1] + ps*cx+cy*p->linesize[1], chroma_width, chroma_height, p->linesize[1], 1);
 +            decode_plane(fs, p->data[2] + ps*cx+cy*p->linesize[2], chroma_width, chroma_height, p->linesize[2], 1);
          }
          if (fs->transparency)
 -            decode_plane(fs, p->data[3] + ps * x + y * p->linesize[3], width,
 -                         height, p->linesize[3],
 -                         2);
 +            decode_plane(fs, p->data[3] + ps*x + y*p->linesize[3], width, height, p->linesize[3], 2);
      } else {
-         uint8_t *planes[3] = {p->data[0] + ps*x + y*p->linesize[0],
-                               p->data[1] + ps*x + y*p->linesize[1],
-                               p->data[2] + ps*x + y*p->linesize[2]};
+         uint8_t *planes[3] = { p->data[0] + ps * x + y * p->linesize[0],
+                                p->data[1] + ps * x + y * p->linesize[1],
+                                p->data[2] + ps * x + y * p->linesize[2] };
          decode_rgb_frame(fs, planes, width, height, p->linesize);
      }
      if (fs->ac && f->version > 2) {
          int v;
-         get_rac(&fs->c, (uint8_t[]) {129});
+         get_rac(&fs->c, (uint8_t[]) { 129 });
 -        v = fs->c.bytestream_end - fs->c.bytestream - 2 - 5 * f->ec;
 +        v = fs->c.bytestream_end - fs->c.bytestream - 2 - 5*f->ec;
          if (v) {
 -            av_log(f->avctx, AV_LOG_ERROR, "bytestream end mismatching by %d\n",
 -                   v);
 +            av_log(f->avctx, AV_LOG_ERROR, "bytestream end mismatching by %d\n", v);
              fs->slice_damaged = 1;
          }
      }
@@@ -449,22 -461,24 +451,23 @@@ static int read_extra_header(FFV1Contex
      f->version = get_symbol(c, state, 0);
      if (f->version > 2) {
          c->bytestream_end -= 4;
 -        f->minor_version   = get_symbol(c, state, 0);
 +        f->minor_version = get_symbol(c, state, 0);
      }
      f->ac = f->avctx->coder_type = get_symbol(c, state, 0);
-     if (f->ac > 1)
 -
+     if (f->ac > 1) {
          for (i = 1; i < 256; i++)
              f->state_transition[i] = get_symbol(c, state, 1) + c->one_state[i];
+     }
  
-     f->colorspace                 = get_symbol(c, state, 0); // YUV cs type
+     f->colorspace                 = get_symbol(c, state, 0); //YUV cs type
      f->avctx->bits_per_raw_sample = get_symbol(c, state, 0);
-     f->chroma_planes  = get_rac(c, state);
-     f->chroma_h_shift = get_symbol(c, state, 0);
-     f->chroma_v_shift = get_symbol(c, state, 0);
-     f->transparency = get_rac(c, state);
-     f->plane_count  = 2 + f->transparency;
-     f->num_h_slices = 1 + get_symbol(c, state, 0);
-     f->num_v_slices = 1 + get_symbol(c, state, 0);
+     f->chroma_planes              = get_rac(c, state);
+     f->chroma_h_shift             = get_symbol(c, state, 0);
+     f->chroma_v_shift             = get_symbol(c, state, 0);
+     f->transparency               = get_rac(c, state);
+     f->plane_count                = 2 + f->transparency;
+     f->num_h_slices               = 1 + get_symbol(c, state, 0);
+     f->num_v_slices               = 1 + get_symbol(c, state, 0);
  
      if (f->num_h_slices > (unsigned)f->width ||
          f->num_v_slices > (unsigned)f->height) {
      }
  
      f->quant_table_count = get_symbol(c, state, 0);
      if (f->quant_table_count > (unsigned)MAX_QUANT_TABLES)
          return AVERROR_INVALIDDATA;
 +
      for (i = 0; i < f->quant_table_count; i++) {
          f->context_count[i] = read_quant_tables(c, f->quant_tables[i]);
          if (f->context_count[i] < 0) {
@@@ -528,13 -543,20 +531,17 @@@ static int read_header(FFV1Context *f
              return AVERROR_INVALIDDATA;
          }
          f->version = v;
 -
 -        f->ac = f->avctx->coder_type = get_symbol(c, state, 0);
 -
 +        f->ac      = f->avctx->coder_type = get_symbol(c, state, 0);
-         if (f->ac > 1)
+         if (f->ac > 1) {
              for (i = 1; i < 256; i++)
 -                f->state_transition[i] =
 -                    get_symbol(c, state, 1) + c->one_state[i];
 +                f->state_transition[i] = get_symbol(c, state, 1) + c->one_state[i];
-         f->colorspace = get_symbol(c, state, 0); // YUV cs type
+         }
+         f->colorspace = get_symbol(c, state, 0); //YUV cs type
          if (f->version > 0)
              f->avctx->bits_per_raw_sample = get_symbol(c, state, 0);
          f->chroma_planes  = get_rac(c, state);
          f->chroma_h_shift = get_symbol(c, state, 0);
          f->chroma_v_shift = get_symbol(c, state, 0);
                  f->avctx->pix_fmt = AV_PIX_FMT_GRAY8;
              else
                  f->avctx->pix_fmt = AV_PIX_FMT_GRAY16;
 -        } else if (f->avctx->bits_per_raw_sample <= 8 && !f->transparency) {
 -            switch (16 * f->chroma_h_shift + f->chroma_v_shift) {
 -            case 0x00:
 -                f->avctx->pix_fmt = AV_PIX_FMT_YUV444P;
 -                break;
 -            case 0x01:
 -                f->avctx->pix_fmt = AV_PIX_FMT_YUV440P;
 -                break;
 -            case 0x10:
 -                f->avctx->pix_fmt = AV_PIX_FMT_YUV422P;
 -                break;
 -            case 0x11:
 -                f->avctx->pix_fmt = AV_PIX_FMT_YUV420P;
 -                break;
 -            case 0x20:
 -                f->avctx->pix_fmt = AV_PIX_FMT_YUV411P;
 -                break;
 -            case 0x22:
 -                f->avctx->pix_fmt = AV_PIX_FMT_YUV410P;
 -                break;
 +        } else if (f->avctx->bits_per_raw_sample<=8 && !f->transparency) {
 +            switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
 +            case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P; break;
 +            case 0x01: f->avctx->pix_fmt = AV_PIX_FMT_YUV440P; break;
 +            case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P; break;
 +            case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P; break;
 +            case 0x20: f->avctx->pix_fmt = AV_PIX_FMT_YUV411P; break;
 +            case 0x22: f->avctx->pix_fmt = AV_PIX_FMT_YUV410P; break;
              default:
                  av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
-                 return -1;
+                 return AVERROR(ENOSYS);
              }
          } else if (f->avctx->bits_per_raw_sample <= 8 && f->transparency) {
 -            switch (16 * f->chroma_h_shift + f->chroma_v_shift) {
 -            case 0x00:
 -                f->avctx->pix_fmt = AV_PIX_FMT_YUVA444P;
 -                break;
 -            case 0x10:
 -                f->avctx->pix_fmt = AV_PIX_FMT_YUVA422P;
 -                break;
 -            case 0x11:
 -                f->avctx->pix_fmt = AV_PIX_FMT_YUVA420P;
 -                break;
 +            switch(16*f->chroma_h_shift + f->chroma_v_shift) {
 +            case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUVA444P; break;
 +            case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUVA422P; break;
 +            case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUVA420P; break;
              default:
                  av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
-                 return -1;
+                 return AVERROR(ENOSYS);
              }
          } else if (f->avctx->bits_per_raw_sample == 9) {
              f->packed_at_lsb = 1;
 -            switch (16 * f->chroma_h_shift + f->chroma_v_shift) {
 -            case 0x00:
 -                f->avctx->pix_fmt = AV_PIX_FMT_YUV444P9;
 -                break;
 -            case 0x10:
 -                f->avctx->pix_fmt = AV_PIX_FMT_YUV422P9;
 -                break;
 -            case 0x11:
 -                f->avctx->pix_fmt = AV_PIX_FMT_YUV420P9;
 -                break;
 +            switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
 +            case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P9; break;
 +            case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P9; break;
 +            case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P9; break;
              default:
                  av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
-                 return -1;
+                 return AVERROR(ENOSYS);
              }
          } else if (f->avctx->bits_per_raw_sample == 10) {
              f->packed_at_lsb = 1;
          f->slice_count = get_symbol(c, state, 0);
      } else {
          const uint8_t *p = c->bytestream_end;
-         for (f->slice_count = 0; f->slice_count < MAX_SLICES && 3 < p - c->bytestream_start; f->slice_count++) {
+         for (f->slice_count = 0;
+              f->slice_count < MAX_SLICES && 3 < p - c->bytestream_start;
+              f->slice_count++) {
 -            int trailer = 3 + 5 * !!f->ec;
 -            int size    = AV_RB24(p - trailer);
 +            int trailer = 3 + 5*!!f->ec;
 +            int size = AV_RB24(p-trailer);
              if (size + trailer > p - c->bytestream_start)
                  break;
              p -= size + trailer;
          fs->slice_damaged = 0;
  
          if (f->version == 2) {
 -            fs->slice_x     = get_symbol(c, state, 0) * f->width;
 -            fs->slice_y     = get_symbol(c, state, 0) * f->height;
 -            fs->slice_width =
 -                (get_symbol(c, state, 0) + 1) * f->width + fs->slice_x;
 -            fs->slice_height =
 -                (get_symbol(c, state, 0) + 1) * f->height + fs->slice_y;
 -
 -            fs->slice_x      /= f->num_h_slices;
 -            fs->slice_y      /= f->num_v_slices;
 -            fs->slice_width  /= f->num_h_slices - fs->slice_x;
 -            fs->slice_height /= f->num_v_slices - fs->slice_y;
 -            if ((unsigned)fs->slice_width > f->width ||
 +            fs->slice_x      =  get_symbol(c, state, 0)      * f->width ;
 +            fs->slice_y      =  get_symbol(c, state, 0)      * f->height;
 +            fs->slice_width  = (get_symbol(c, state, 0) + 1) * f->width  + fs->slice_x;
 +            fs->slice_height = (get_symbol(c, state, 0) + 1) * f->height + fs->slice_y;
 +
 +            fs->slice_x     /= f->num_h_slices;
 +            fs->slice_y     /= f->num_v_slices;
 +            fs->slice_width  = fs->slice_width  / f->num_h_slices - fs->slice_x;
 +            fs->slice_height = fs->slice_height / f->num_v_slices - fs->slice_y;
 +            if ((unsigned)fs->slice_width  > f->width ||
                  (unsigned)fs->slice_height > f->height)
                  return AVERROR_INVALIDDATA;
-             if ((unsigned)fs->slice_x + (uint64_t)fs->slice_width  > f->width ||
-                 (unsigned)fs->slice_y + (uint64_t)fs->slice_height > f->height)
 -            if ((unsigned)fs->slice_x + (uint64_t)fs->slice_width > f->width
 -                || (unsigned)fs->slice_y + (uint64_t)fs->slice_height >
 -                f->height)
++            if (   (unsigned)fs->slice_x + (uint64_t)fs->slice_width  > f->width
++                || (unsigned)fs->slice_y + (uint64_t)fs->slice_height > f->height)
                  return AVERROR_INVALIDDATA;
          }
  
@@@ -760,18 -830,22 +769,18 @@@ static int decode_frame(AVCodecContext 
          return ret;
      }
  
-     if (avctx->debug&FF_DEBUG_PICT_INFO)
+     if (avctx->debug & FF_DEBUG_PICT_INFO)
 -        av_log(avctx, AV_LOG_DEBUG,
 -               "ver:%d keyframe:%d coder:%d ec:%d slices:%d bps:%d\n",
 -               f->version, p->key_frame, f->ac, f->ec, f->slice_count,
 -               f->avctx->bits_per_raw_sample);
 +        av_log(avctx, AV_LOG_DEBUG, "ver:%d keyframe:%d coder:%d ec:%d slices:%d bps:%d\n",
 +               f->version, p->key_frame, f->ac, f->ec, f->slice_count, f->avctx->bits_per_raw_sample);
  
      buf_p = buf + buf_size;
      for (i = f->slice_count - 1; i >= 0; i--) {
          FFV1Context *fs = f->slice_context[i];
 -        int trailer     = 3 + 5 * !!f->ec;
 +        int trailer = 3 + 5*!!f->ec;
          int v;
  
-         if (i || f->version>2) v = AV_RB24(buf_p-trailer)+trailer;
-         else                   v = buf_p - c->bytestream_start;
 -        if (i || f->version > 2)
 -            v = AV_RB24(buf_p - trailer) + trailer;
 -        else
 -            v = buf_p - c->bytestream_start;
++        if (i || f->version > 2) v = AV_RB24(buf_p-trailer) + trailer;
++        else                     v = buf_p - c->bytestream_start;
          if (buf_p - c->bytestream_start < v) {
              av_log(avctx, AV_LOG_ERROR, "Slice pointer chain broken\n");
              return AVERROR_INVALIDDATA;
              fs->c.bytestream_end = (uint8_t *)(buf_p + v);
      }
  
-     avctx->execute(avctx, decode_slice, &f->slice_context[0], NULL, f->slice_count, sizeof(void*));
 -    avctx->execute(avctx, decode_slice, &f->slice_context[0], NULL,
++    avctx->execute(avctx,
++                   decode_slice,
++                   &f->slice_context[0],
++                   NULL,
+                    f->slice_count,
 -                   sizeof(void *));
++                   sizeof(void*));
  
-     for (i=f->slice_count-1; i>=0; i--) {
-         FFV1Context *fs= f->slice_context[i];
+     for (i = f->slice_count - 1; i >= 0; i--) {
+         FFV1Context *fs = f->slice_context[i];
          int j;
          if (fs->slice_damaged && f->last_picture.data[0]) {
-             uint8_t *dst[4], *src[4];
-             for (j=0; j<4; j++) {
+             const uint8_t *src[4];
+             uint8_t *dst[4];
+             for (j = 0; j < 4; j++) {
 -                int sh = (j == 1 || j == 2) ? f->chroma_h_shift : 0;
 -                int sv = (j == 1 || j == 2) ? f->chroma_v_shift : 0;
 -                dst[j] = f->picture.data[j] + f->picture.linesize[j] *
 -                         (fs->slice_y >> sv) + (fs->slice_x >> sh);
 -                src[j] = f->last_picture.data[j] +
 -                         f->last_picture.linesize[j] *
 -                         (fs->slice_y >> sv) + (fs->slice_x >> sh);
 +                int sh = (j==1 || j==2) ? f->chroma_h_shift : 0;
 +                int sv = (j==1 || j==2) ? f->chroma_v_shift : 0;
 +                dst[j] = f->picture     .data[j] + f->picture     .linesize[j]*
 +                         (fs->slice_y>>sv) + (fs->slice_x>>sh);
 +                src[j] = f->last_picture.data[j] + f->last_picture.linesize[j]*
 +                         (fs->slice_y>>sv) + (fs->slice_x>>sh);
              }
-             av_image_copy(dst, f->picture.linesize, (const uint8_t **)src, f->last_picture.linesize,
-                           avctx->pix_fmt, fs->slice_width, fs->slice_height);
 -            av_image_copy(dst, f->picture.linesize, (const uint8_t **)src,
++            av_image_copy(dst,
++                          f->picture.linesize,
++                          (const uint8_t **)src,
+                           f->last_picture.linesize,
 -                          avctx->pix_fmt, fs->slice_width,
++                          avctx->pix_fmt,
++                          fs->slice_width,
+                           fs->slice_height);
          }
      }
  
@@@ -245,7 -149,7 +245,7 @@@ static inline void put_vlc_symbol(PutBi
          i += i;
      }
  
-     av_assert2(k<=13);
 -    assert(k <= 13);
++    av_assert2(k <= 13);
  
  #if 0 // JPEG LS
      if (k == 0 && 2 * state->drift <= -state->count)
@@@ -394,18 -298,24 +394,20 @@@ static void encode_rgb_frame(FFV1Contex
  {
      int x, y, p, i;
      const int ring_size = s->avctx->context_model ? 3 : 2;
 -    int16_t *sample[MAX_PLANES][3];
 -    int lbd  = s->avctx->bits_per_raw_sample <= 8;
 -    int bits = s->avctx->bits_per_raw_sample > 0
 -               ? s->avctx->bits_per_raw_sample
 -               : 8;
 +    int16_t *sample[4][3];
 +    int lbd    = s->avctx->bits_per_raw_sample <= 8;
 +    int bits   = s->avctx->bits_per_raw_sample > 0 ? s->avctx->bits_per_raw_sample : 8;
      int offset = 1 << bits;
      s->run_index = 0;
  
-     memset(s->sample_buffer, 0, ring_size * 4 * (w + 6) * sizeof(*s->sample_buffer));
+     memset(s->sample_buffer, 0, ring_size * MAX_PLANES *
+                                 (w + 6) * sizeof(*s->sample_buffer));
  
      for (y = 0; y < h; y++) {
          for (i = 0; i < ring_size; i++)
-             for (p = 0; p < 4; p++)
+             for (p = 0; p < MAX_PLANES; p++)
 -                sample[p][i] = s->sample_buffer + p * ring_size *
 -                               (w + 6) +
 -                               ((h + i - y) % ring_size) * (w + 6) + 3;
 +                sample[p][i]= s->sample_buffer + p*ring_size*(w+6) + ((h+i-y)%ring_size)*(w+6) + 3;
  
          for (x = 0; x < w; x++) {
              int b, g, r, av_uninit(a);
              sample[3][0][x] = a;
          }
          for (p = 0; p < 3 + s->transparency; p++) {
 -            sample[p][0][-1] = sample[p][1][0];
 -            sample[p][1][w]  = sample[p][1][w - 1];
 +            sample[p][0][-1] = sample[p][1][0  ];
 +            sample[p][1][ w] = sample[p][1][w-1];
              if (lbd)
-                 encode_line(s, w, sample[p], (p+1)/2, 9);
+                 encode_line(s, w, sample[p], (p + 1) / 2, 9);
              else
-                 encode_line(s, w, sample[p], (p+1)/2, bits+1);
+                 encode_line(s, w, sample[p], (p + 1) / 2, bits + 1);
          }
      }
  }
@@@ -629,10 -545,21 +634,10 @@@ static int sort_stt(FFV1Context *s, uin
      return print;
  }
  
 -static int init_slices_state(FFV1Context *f)
 -{
 -    int i, ret;
 -    for (i = 0; i < f->slice_count; i++) {
 -        FFV1Context *fs = f->slice_context[i];
 -        if ((ret = ffv1_init_slice_state(f, fs)) < 0)
 -            return AVERROR(ENOMEM);
 -    }
 -    return 0;
 -}
 -
 -static av_cold int ffv1_encode_init(AVCodecContext *avctx)
 +static av_cold int encode_init(AVCodecContext *avctx)
  {
-     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
      FFV1Context *s = avctx->priv_data;
+     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
      int i, j, k, m, ret;
  
      ffv1_common_init(avctx);
      }
  
      if (s->version > 1) {
 -        for (s->num_v_slices = 2; s->num_v_slices < 9; s->num_v_slices++)
 -            for (s->num_h_slices = s->num_v_slices;
 -                 s->num_h_slices < 2 * s->num_v_slices; s->num_h_slices++)
 -                if (avctx->slices == s->num_h_slices * s->num_v_slices &&
 -                    avctx->slices <= 64 || !avctx->slices)
 +        for (s->num_v_slices = 2; s->num_v_slices < 9; s->num_v_slices++) {
 +            for (s->num_h_slices = s->num_v_slices; s->num_h_slices < 2*s->num_v_slices; s->num_h_slices++) {
 +                if (avctx->slices == s->num_h_slices * s->num_v_slices && avctx->slices <= 64 || !avctx->slices)
                      goto slices_ok;
-         av_log(avctx, AV_LOG_ERROR, "Unsupported number %d of slices requested, please specify a supported number with -slices (ex:4,6,9,12,16, ...)\n", avctx->slices);
-         return -1;
-         slices_ok:
-         write_extra_header(s);
 +            }
 +        }
+         av_log(avctx, AV_LOG_ERROR,
+                "Unsupported number %d of slices requested, please specify a "
+                "supported number with -slices (ex:4,6,9,12,16, ...)\n",
+                avctx->slices);
+         return AVERROR(ENOSYS);
+ slices_ok:
+         write_extradata(s);
      }
  
      if ((ret = ffv1_init_slice_contexts(s)) < 0)
@@@ -940,9 -886,10 +950,11 @@@ static int encode_slice(AVCodecContext 
      }
      if (!fs->ac) {
          if (f->version > 2)
-             put_rac(&fs->c, (uint8_t[]) {129}, 0);
-         fs->ac_byte_count = f->version > 2 || (!x&&!y) ? ff_rac_terminate(&fs->c) : 0;
-         init_put_bits(&fs->pb, fs->c.bytestream_start + fs->ac_byte_count, fs->c.bytestream_end - fs->c.bytestream_start - fs->ac_byte_count);
+             put_rac(&fs->c, (uint8_t[]) { 129 }, 0);
 -        fs->ac_byte_count = f->version > 2 || (!x && !y) ? ff_rac_terminate( &fs->c) : 0;
 -        init_put_bits(&fs->pb, fs->c.bytestream_start + fs->ac_byte_count,
++        fs->ac_byte_count = f->version > 2 || (!x && !y) ? ff_rac_terminate(&fs->c) : 0;
++        init_put_bits(&fs->pb,
++                      fs->c.bytestream_start + fs->ac_byte_count,
+                       fs->c.bytestream_end - fs->c.bytestream_start - fs->ac_byte_count);
      }
  
      if (f->colorspace == 0) {
@@@ -1101,8 -1057,9 +1114,8 @@@ static int encode_frame(AVCodecContext 
  #define OFFSET(x) offsetof(FFV1Context, x)
  #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  static const AVOption options[] = {
-     { "slicecrc",        "Protect slices with CRCs",               OFFSET(ec),              AV_OPT_TYPE_INT, {.i64 = -1}, -1, 1, VE},
- {NULL}
 -    { "slicecrc", "Protect slices with CRCs", OFFSET(ec), AV_OPT_TYPE_INT,
 -             { .i64 = -1 }, -1, 1, VE },
++    { "slicecrc", "Protect slices with CRCs", OFFSET(ec), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, VE },
+     { NULL }
  };
  
  static const AVClass class = {
@@@ -1122,21 -1079,24 +1135,22 @@@ AVCodec ff_ffv1_encoder = 
      .type           = AVMEDIA_TYPE_VIDEO,
      .id             = AV_CODEC_ID_FFV1,
      .priv_data_size = sizeof(FFV1Context),
 -    .init           = ffv1_encode_init,
 -    .encode2        = ffv1_encode_frame,
 +    .init           = encode_init,
 +    .encode2        = encode_frame,
      .close          = ffv1_close,
      .capabilities   = CODEC_CAP_SLICE_THREADS,
-     .defaults       = ffv1_defaults,
      .pix_fmts       = (const enum AVPixelFormat[]) {
-         AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV444P,
-         AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV411P,
-         AV_PIX_FMT_YUV410P, AV_PIX_FMT_0RGB32, AV_PIX_FMT_RGB32, AV_PIX_FMT_YUV420P16,
-         AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16, AV_PIX_FMT_YUV444P9, AV_PIX_FMT_YUV422P9,
-         AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
-         AV_PIX_FMT_GRAY16, AV_PIX_FMT_GRAY8, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
-         AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14,
 -        AV_PIX_FMT_YUV420P,   AV_PIX_FMT_YUV422P,   AV_PIX_FMT_YUV444P,
 -        AV_PIX_FMT_YUV411P,   AV_PIX_FMT_YUV410P,
 -        AV_PIX_FMT_YUV444P9,  AV_PIX_FMT_YUV422P9,  AV_PIX_FMT_YUV420P9,
 -        AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
 -        AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
 -        AV_PIX_FMT_RGB32,
 -        AV_PIX_FMT_GBRP9,     AV_PIX_FMT_GBRP10,
 -        AV_PIX_FMT_YUVA420P,  AV_PIX_FMT_YUVA422P,  AV_PIX_FMT_YUVA444P,
 -        AV_PIX_FMT_GRAY16,    AV_PIX_FMT_GRAY8,
++        AV_PIX_FMT_YUV420P,   AV_PIX_FMT_YUVA420P,  AV_PIX_FMT_YUVA422P,  AV_PIX_FMT_YUV444P,
++        AV_PIX_FMT_YUVA444P,  AV_PIX_FMT_YUV440P,   AV_PIX_FMT_YUV422P,   AV_PIX_FMT_YUV411P,
++        AV_PIX_FMT_YUV410P,   AV_PIX_FMT_0RGB32,    AV_PIX_FMT_RGB32,     AV_PIX_FMT_YUV420P16,
++        AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16, AV_PIX_FMT_YUV444P9,  AV_PIX_FMT_YUV422P9,
++        AV_PIX_FMT_YUV420P9,  AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
++        AV_PIX_FMT_GRAY16,    AV_PIX_FMT_GRAY8,     AV_PIX_FMT_GBRP9,     AV_PIX_FMT_GBRP10,
++        AV_PIX_FMT_GBRP12,    AV_PIX_FMT_GBRP14,
          AV_PIX_FMT_NONE
      },
      .long_name      = NULL_IF_CONFIG_SMALL("FFmpeg video codec #1"),
+     .defaults       = ffv1_defaults,
      .priv_class     = &class,
  };
Simple merge