avcodec/h264_slice: assert relation between current_slice ans slice_ctx
[ffmpeg.git] / libavcodec / cinepakenc.c
index 9c69ec5..06b06da 100644 (file)
@@ -169,7 +169,7 @@ typedef struct {
 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
 static const AVOption options[] = {
     { "max_extra_cb_iterations", "Max extra codebook recalculation passes, more is better and slower", OFFSET(max_extra_cb_iterations), AV_OPT_TYPE_INT, { .i64 = 2 }, 0, INT_MAX, VE },
-    { "skip_empty_cb", "Avoid wasting bytes, ignore vintage MacOS decoder", OFFSET(skip_empty_cb), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
+    { "skip_empty_cb", "Avoid wasting bytes, ignore vintage MacOS decoder", OFFSET(skip_empty_cb), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
     { "max_strips", "Limit strips/frame, vintage compatible is 1..3, otherwise the more the better", OFFSET(max_max_strips), AV_OPT_TYPE_INT, { .i64 = 3 }, MIN_STRIPS, MAX_STRIPS, VE },
     { "min_strips", "Enforce min strips/frame, more is worse and faster, must be <= max_strips", OFFSET(min_min_strips), AV_OPT_TYPE_INT, { .i64 = MIN_STRIPS }, MIN_STRIPS, MAX_STRIPS, VE },
     { "strip_number_adaptivity", "How fast the strip number adapts, more is slightly better, much slower", OFFSET(strip_number_delta_range), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, MAX_STRIPS-MIN_STRIPS, VE },
@@ -236,11 +236,11 @@ static av_cold int cinepak_encode_init(AVCodecContext *avctx)
     if (!(s->frame_buf = av_malloc(frame_buf_size)))
         goto enomem;
 
-    if (!(s->mb = av_malloc(mb_count*sizeof(mb_info))))
+    if (!(s->mb = av_malloc_array(mb_count, sizeof(mb_info))))
         goto enomem;
 
 #ifdef CINEPAKENC_DEBUG
-    if (!(s->best_mb = av_malloc(mb_count*sizeof(mb_info))))
+    if (!(s->best_mb = av_malloc_array(mb_count, sizeof(mb_info))))
         goto enomem;
 #endif
 
@@ -327,7 +327,7 @@ static int64_t calculate_mode_score(CinepakEncContext *s, int h, strip_info *inf
                    (info->v4_size ? CHUNK_HEADER_SIZE + info->v4_size * entry_size : 0) +
                    CHUNK_HEADER_SIZE) << 3;
 
-    //av_log(s->avctx, AV_LOG_INFO, "sizes %3i %3i -> %9lli score mb_count %i", info->v1_size, info->v4_size, (long long int)ret, mb_count);
+    //av_log(s->avctx, AV_LOG_INFO, "sizes %3i %3i -> %9"PRId64" score mb_count %i", info->v1_size, info->v4_size, ret, mb_count);
 
 #ifdef CINEPAK_REPORT_SERR
     *serr = 0;
@@ -525,105 +525,115 @@ static int encode_codebook(CinepakEncContext *s, int *codebook, int size, int ch
 }
 
 //sets out to the sub picture starting at (x,y) in in
-static void get_sub_picture(CinepakEncContext *s, int x, int y, AVPicture *in, AVPicture *out)
+static void get_sub_picture(CinepakEncContext *s, int x, int y,
+                            uint8_t * in_data[4], int  in_linesize[4],
+                            uint8_t *out_data[4], int out_linesize[4])
 {
-    out->data[0] = in->data[0] + x + y * in->linesize[0];
-    out->linesize[0] = in->linesize[0];
+    out_data[0] = in_data[0] + x + y * in_linesize[0];
+    out_linesize[0] = in_linesize[0];
 
     if(s->pix_fmt == AV_PIX_FMT_RGB24) {
-        out->data[1] = in->data[1] + (x >> 1) + (y >> 1) * in->linesize[1];
-        out->linesize[1] = in->linesize[1];
+        out_data[1] = in_data[1] + (x >> 1) + (y >> 1) * in_linesize[1];
+        out_linesize[1] = in_linesize[1];
 
-        out->data[2] = in->data[2] + (x >> 1) + (y >> 1) * in->linesize[2];
-        out->linesize[2] = in->linesize[2];
+        out_data[2] = in_data[2] + (x >> 1) + (y >> 1) * in_linesize[2];
+        out_linesize[2] = in_linesize[2];
     }
 }
 
-//decodes the V1 vector in mb into the 4x4 MB pointed to by sub_pict
-static void decode_v1_vector(CinepakEncContext *s, AVPicture *sub_pict, int v1_vector, strip_info *info)
+//decodes the V1 vector in mb into the 4x4 MB pointed to by data
+static void decode_v1_vector(CinepakEncContext *s, uint8_t *data[4],
+                             int linesize[4], int v1_vector, strip_info *info)
 {
     int entry_size = s->pix_fmt == AV_PIX_FMT_RGB24 ? 6 : 4;
 
-    sub_pict->data[0][0] =
-            sub_pict->data[0][1] =
-            sub_pict->data[0][    sub_pict->linesize[0]] =
-            sub_pict->data[0][1+  sub_pict->linesize[0]] = info->v1_codebook[v1_vector*entry_size];
+    data[0][0] =
+            data[0][1] =
+            data[0][    linesize[0]] =
+            data[0][1+  linesize[0]] = info->v1_codebook[v1_vector*entry_size];
 
-    sub_pict->data[0][2] =
-            sub_pict->data[0][3] =
-            sub_pict->data[0][2+  sub_pict->linesize[0]] =
-            sub_pict->data[0][3+  sub_pict->linesize[0]] = info->v1_codebook[v1_vector*entry_size+1];
+    data[0][2] =
+            data[0][3] =
+            data[0][2+  linesize[0]] =
+            data[0][3+  linesize[0]] = info->v1_codebook[v1_vector*entry_size+1];
 
-    sub_pict->data[0][2*sub_pict->linesize[0]] =
-            sub_pict->data[0][1+2*sub_pict->linesize[0]] =
-            sub_pict->data[0][  3*sub_pict->linesize[0]] =
-            sub_pict->data[0][1+3*sub_pict->linesize[0]] = info->v1_codebook[v1_vector*entry_size+2];
+    data[0][2*linesize[0]] =
+            data[0][1+2*linesize[0]] =
+            data[0][  3*linesize[0]] =
+            data[0][1+3*linesize[0]] = info->v1_codebook[v1_vector*entry_size+2];
 
-    sub_pict->data[0][2+2*sub_pict->linesize[0]] =
-            sub_pict->data[0][3+2*sub_pict->linesize[0]] =
-            sub_pict->data[0][2+3*sub_pict->linesize[0]] =
-            sub_pict->data[0][3+3*sub_pict->linesize[0]] = info->v1_codebook[v1_vector*entry_size+3];
+    data[0][2+2*linesize[0]] =
+            data[0][3+2*linesize[0]] =
+            data[0][2+3*linesize[0]] =
+            data[0][3+3*linesize[0]] = info->v1_codebook[v1_vector*entry_size+3];
 
     if(s->pix_fmt == AV_PIX_FMT_RGB24) {
-        sub_pict->data[1][0] =
-            sub_pict->data[1][1] =
-            sub_pict->data[1][    sub_pict->linesize[1]] =
-            sub_pict->data[1][1+  sub_pict->linesize[1]] = info->v1_codebook[v1_vector*entry_size+4];
-
-        sub_pict->data[2][0] =
-            sub_pict->data[2][1] =
-            sub_pict->data[2][    sub_pict->linesize[2]] =
-            sub_pict->data[2][1+  sub_pict->linesize[2]] = info->v1_codebook[v1_vector*entry_size+5];
+        data[1][0] =
+            data[1][1] =
+            data[1][    linesize[1]] =
+            data[1][1+  linesize[1]] = info->v1_codebook[v1_vector*entry_size+4];
+
+        data[2][0] =
+            data[2][1] =
+            data[2][    linesize[2]] =
+            data[2][1+  linesize[2]] = info->v1_codebook[v1_vector*entry_size+5];
     }
 }
 
-//decodes the V4 vectors in mb into the 4x4 MB pointed to by sub_pict
-static void decode_v4_vector(CinepakEncContext *s, AVPicture *sub_pict, int *v4_vector, strip_info *info)
+//decodes the V4 vectors in mb into the 4x4 MB pointed to by data
+static void decode_v4_vector(CinepakEncContext *s, uint8_t *data[4],
+                             int linesize[4], int *v4_vector, strip_info *info)
 {
     int i, x, y, entry_size = s->pix_fmt == AV_PIX_FMT_RGB24 ? 6 : 4;
 
     for(i = y = 0; y < 4; y += 2) {
         for(x = 0; x < 4; x += 2, i++) {
-            sub_pict->data[0][x   +     y*sub_pict->linesize[0]] = info->v4_codebook[v4_vector[i]*entry_size];
-            sub_pict->data[0][x+1 +     y*sub_pict->linesize[0]] = info->v4_codebook[v4_vector[i]*entry_size+1];
-            sub_pict->data[0][x   + (y+1)*sub_pict->linesize[0]] = info->v4_codebook[v4_vector[i]*entry_size+2];
-            sub_pict->data[0][x+1 + (y+1)*sub_pict->linesize[0]] = info->v4_codebook[v4_vector[i]*entry_size+3];
+            data[0][x   +     y*linesize[0]] = info->v4_codebook[v4_vector[i]*entry_size];
+            data[0][x+1 +     y*linesize[0]] = info->v4_codebook[v4_vector[i]*entry_size+1];
+            data[0][x   + (y+1)*linesize[0]] = info->v4_codebook[v4_vector[i]*entry_size+2];
+            data[0][x+1 + (y+1)*linesize[0]] = info->v4_codebook[v4_vector[i]*entry_size+3];
 
             if(s->pix_fmt == AV_PIX_FMT_RGB24) {
-                sub_pict->data[1][(x>>1) + (y>>1)*sub_pict->linesize[1]] = info->v4_codebook[v4_vector[i]*entry_size+4];
-                sub_pict->data[2][(x>>1) + (y>>1)*sub_pict->linesize[2]] = info->v4_codebook[v4_vector[i]*entry_size+5];
+                data[1][(x>>1) + (y>>1)*linesize[1]] = info->v4_codebook[v4_vector[i]*entry_size+4];
+                data[2][(x>>1) + (y>>1)*linesize[2]] = info->v4_codebook[v4_vector[i]*entry_size+5];
             }
         }
     }
 }
 
-static void copy_mb(CinepakEncContext *s, AVPicture *a, AVPicture *b)
+static void copy_mb(CinepakEncContext *s,
+                    uint8_t *a_data[4], int a_linesize[4],
+                    uint8_t *b_data[4], int b_linesize[4])
 {
     int y, p;
 
     for(y = 0; y < MB_SIZE; y++) {
-        memcpy(a->data[0]+y*a->linesize[0], b->data[0]+y*b->linesize[0],
+        memcpy(a_data[0]+y*a_linesize[0], b_data[0]+y*b_linesize[0],
                MB_SIZE);
     }
 
     if(s->pix_fmt == AV_PIX_FMT_RGB24) {
         for(p = 1; p <= 2; p++) {
             for(y = 0; y < MB_SIZE/2; y++) {
-                memcpy(a->data[p] + y*a->linesize[p],
-                       b->data[p] + y*b->linesize[p],
+                memcpy(a_data[p] + y*a_linesize[p],
+                       b_data[p] + y*b_linesize[p],
                        MB_SIZE/2);
             }
         }
     }
 }
 
-static int encode_mode(CinepakEncContext *s, int h, AVPicture *scratch_pict, AVPicture *last_pict, strip_info *info, unsigned char *buf)
+static int encode_mode(CinepakEncContext *s, int h,
+                       uint8_t *scratch_data[4], int scratch_linesize[4],
+                       uint8_t *last_data[4], int last_linesize[4],
+                       strip_info *info, unsigned char *buf)
 {
     int x, y, z, flags, bits, temp_size, header_ofs, ret = 0, mb_count = s->w * h / MB_AREA;
     int needs_extra_bit, should_write_temp;
     unsigned char temp[64]; //32/2 = 16 V4 blocks at 4 B each -> 64 B
     mb_info *mb;
-    AVPicture sub_scratch = {{0}}, sub_last = {{0}};
+    uint8_t *sub_scratch_data[4] = {0}, *sub_last_data[4] = {0};
+    int sub_scratch_linesize[4] = {0}, sub_last_linesize[4] = {0};
 
     //encode codebooks
 ////// MacOS vintage decoder compatibility dictates the presence of
@@ -640,15 +650,21 @@ static int encode_mode(CinepakEncContext *s, int h, AVPicture *scratch_pict, AVP
         for(x = 0; x < s->w; x += MB_SIZE, z++) {
             mb = &s->mb[z];
 
-            get_sub_picture(s, x, y, scratch_pict, &sub_scratch);
+            get_sub_picture(s, x, y, scratch_data, scratch_linesize,
+                            sub_scratch_data, sub_scratch_linesize);
 
             if(info->mode == MODE_MC && mb->best_encoding == ENC_SKIP) {
-                get_sub_picture(s, x, y, last_pict, &sub_last);
-                copy_mb(s, &sub_scratch, &sub_last);
+                get_sub_picture(s, x, y,
+                                last_data, last_linesize,
+                                sub_last_data, sub_last_linesize);
+                copy_mb(s, sub_scratch_data, sub_scratch_linesize,
+                        sub_last_data, sub_last_linesize);
             } else if(info->mode == MODE_V1_ONLY || mb->best_encoding == ENC_V1)
-                decode_v1_vector(s, &sub_scratch, mb->v1_vector, info);
+                decode_v1_vector(s, sub_scratch_data, sub_scratch_linesize,
+                                 mb->v1_vector, info);
             else
-                decode_v4_vector(s, &sub_scratch, mb->v4_vector, info);
+                decode_v4_vector(s, sub_scratch_data, sub_scratch_linesize,
+                                 mb->v4_vector, info);
         }
     }
 
@@ -755,13 +771,15 @@ static int encode_mode(CinepakEncContext *s, int h, AVPicture *scratch_pict, AVP
 }
 
 //computes distortion of 4x4 MB in b compared to a
-static int compute_mb_distortion(CinepakEncContext *s, AVPicture *a, AVPicture *b)
+static int compute_mb_distortion(CinepakEncContext *s,
+                                 uint8_t *a_data[4], int a_linesize[4],
+                                 uint8_t *b_data[4], int b_linesize[4])
 {
     int x, y, p, d, ret = 0;
 
     for(y = 0; y < MB_SIZE; y++) {
         for(x = 0; x < MB_SIZE; x++) {
-            d = a->data[0][x + y*a->linesize[0]] - b->data[0][x + y*b->linesize[0]];
+            d = a_data[0][x + y*a_linesize[0]] - b_data[0][x + y*b_linesize[0]];
             ret += d*d;
         }
     }
@@ -770,7 +788,7 @@ static int compute_mb_distortion(CinepakEncContext *s, AVPicture *a, AVPicture *
         for(p = 1; p <= 2; p++) {
             for(y = 0; y < MB_SIZE/2; y++) {
                 for(x = 0; x < MB_SIZE/2; x++) {
-                    d = a->data[p][x + y*a->linesize[p]] - b->data[p][x + y*b->linesize[p]];
+                    d = a_data[p][x + y*a_linesize[p]] - b_data[p][x + y*b_linesize[p]];
                     ret += d*d;
                 }
             }
@@ -782,7 +800,8 @@ static int compute_mb_distortion(CinepakEncContext *s, AVPicture *a, AVPicture *
 
 // return the possibly adjusted size of the codebook
 #define CERTAIN(x) ((x)!=ENC_UNCERTAIN)
-static int quantize(CinepakEncContext *s, int h, AVPicture *pict,
+static int quantize(CinepakEncContext *s, int h,
+                    uint8_t *data[4], int linesize[4],
                     int v1mode, strip_info *info,
                     mb_encoding encoding)
 {
@@ -792,7 +811,8 @@ static int quantize(CinepakEncContext *s, int h, AVPicture *pict,
     int size = v1mode ? info->v1_size : info->v4_size;
     int64_t total_error = 0;
     uint8_t vq_pict_buf[(MB_AREA*3)/2];
-    AVPicture sub_pict, vq_pict;
+    uint8_t *sub_data    [4], *vq_data    [4];
+    int      sub_linesize[4],  vq_linesize[4];
 
     for(mbn = i = y = 0; y < h; y += MB_SIZE) {
         for(x = 0; x < s->w; x += MB_SIZE, ++mbn) {
@@ -812,10 +832,10 @@ static int quantize(CinepakEncContext *s, int h, AVPicture *pict,
                         shift = y2 < 4 ? 0 : 1;
                         x3 = shift ? 0 : x2;
                         y3 = shift ? 0 : y2;
-                        base[j] = (pict->data[plane][((x+x3) >> shift) +      ((y+y3) >> shift)      * pict->linesize[plane]] +
-                                   pict->data[plane][((x+x3) >> shift) + 1 +  ((y+y3) >> shift)      * pict->linesize[plane]] +
-                                   pict->data[plane][((x+x3) >> shift) +     (((y+y3) >> shift) + 1) * pict->linesize[plane]] +
-                                   pict->data[plane][((x+x3) >> shift) + 1 + (((y+y3) >> shift) + 1) * pict->linesize[plane]]) >> 2;
+                        base[j] = (data[plane][((x+x3) >> shift) +      ((y+y3) >> shift)      * linesize[plane]] +
+                                   data[plane][((x+x3) >> shift) + 1 +  ((y+y3) >> shift)      * linesize[plane]] +
+                                   data[plane][((x+x3) >> shift) +     (((y+y3) >> shift) + 1) * linesize[plane]] +
+                                   data[plane][((x+x3) >> shift) + 1 + (((y+y3) >> shift) + 1) * linesize[plane]]) >> 2;
                     }
                 }
             } else {
@@ -833,7 +853,7 @@ static int quantize(CinepakEncContext *s, int h, AVPicture *pict,
                                 y3 = y + y2 + (k >> 1);
                             }
 
-                            base[j] = pict->data[plane][x3 + y3*pict->linesize[plane]];
+                            base[j] = data[plane][x3 + y3*linesize[plane]];
                         }
                     }
                 }
@@ -855,12 +875,12 @@ static int quantize(CinepakEncContext *s, int h, AVPicture *pict,
     avpriv_init_elbg(s->codebook_input, entry_size, i, codebook, size, 1, s->codebook_closest, &s->randctx);
     avpriv_do_elbg(s->codebook_input, entry_size, i, codebook, size, 1, s->codebook_closest, &s->randctx);
 
-    //setup vq_pict, which contains a single MB
-    vq_pict.data[0] = vq_pict_buf;
-    vq_pict.linesize[0] = MB_SIZE;
-    vq_pict.data[1] = &vq_pict_buf[MB_AREA];
-    vq_pict.data[2] = vq_pict.data[1] + (MB_AREA >> 2);
-    vq_pict.linesize[1] = vq_pict.linesize[2] = MB_SIZE >> 1;
+    //setup vq_data, which contains a single MB
+    vq_data[0] = vq_pict_buf;
+    vq_linesize[0] = MB_SIZE;
+    vq_data[1] = &vq_pict_buf[MB_AREA];
+    vq_data[2] = vq_data[1] + (MB_AREA >> 2);
+    vq_linesize[1] = vq_linesize[2] = MB_SIZE >> 1;
 
     //copy indices
     for(i = j = y = 0; y < h; y += MB_SIZE) {
@@ -870,25 +890,27 @@ static int quantize(CinepakEncContext *s, int h, AVPicture *pict,
             if(CERTAIN(encoding) && mb->best_encoding != encoding)
                 continue;
 
-            //point sub_pict to current MB
-            get_sub_picture(s, x, y, pict, &sub_pict);
+            //point sub_data to current MB
+            get_sub_picture(s, x, y, data, linesize, sub_data, sub_linesize);
 
             if(v1mode) {
                 mb->v1_vector = s->codebook_closest[i];
 
-                //fill in vq_pict with V1 data
-                decode_v1_vector(s, &vq_pict, mb->v1_vector, info);
+                //fill in vq_data with V1 data
+                decode_v1_vector(s, vq_data, vq_linesize, mb->v1_vector, info);
 
-                mb->v1_error = compute_mb_distortion(s, &sub_pict, &vq_pict);
+                mb->v1_error = compute_mb_distortion(s, sub_data, sub_linesize,
+                                                     vq_data, vq_linesize);
                 total_error += mb->v1_error;
             } else {
                 for(k = 0; k < 4; k++)
                     mb->v4_vector[k] = s->codebook_closest[i+k];
 
-                //fill in vq_pict with V4 data
-                decode_v4_vector(s, &vq_pict, mb->v4_vector, info);
+                //fill in vq_data with V4 data
+                decode_v4_vector(s, vq_data, vq_linesize, mb->v4_vector, info);
 
-                mb->v4_error = compute_mb_distortion(s, &sub_pict, &vq_pict);
+                mb->v4_error = compute_mb_distortion(s, sub_data, sub_linesize,
+                                                     vq_data, vq_linesize);
                 total_error += mb->v4_error;
             }
             i += v1mode ? 1 : 4;
@@ -897,22 +919,30 @@ static int quantize(CinepakEncContext *s, int h, AVPicture *pict,
 // check that we did it right in the beginning of the function
     av_assert0(i >= size); // training set is no smaller than the codebook
 
-    //av_log(s->avctx, AV_LOG_INFO, "isv1 %i size= %i i= %i error %lli\n", v1mode, size, i, (long long int)total_error);
+    //av_log(s->avctx, AV_LOG_INFO, "isv1 %i size= %i i= %i error %"PRId64"\n", v1mode, size, i, total_error);
 
     return size;
 }
 
-static void calculate_skip_errors(CinepakEncContext *s, int h, AVPicture *last_pict, AVPicture *pict, strip_info *info)
+static void calculate_skip_errors(CinepakEncContext *s, int h,
+                                  uint8_t *last_data[4], int last_linesize[4],
+                                  uint8_t *data[4], int linesize[4],
+                                  strip_info *info)
 {
     int x, y, i;
-    AVPicture sub_last, sub_pict;
+    uint8_t *sub_last_data    [4], *sub_pict_data    [4];
+    int      sub_last_linesize[4],  sub_pict_linesize[4];
 
     for(i = y = 0; y < h; y += MB_SIZE) {
         for(x = 0; x < s->w; x += MB_SIZE, i++) {
-            get_sub_picture(s, x, y, last_pict, &sub_last);
-            get_sub_picture(s, x, y, pict,      &sub_pict);
-
-            s->mb[i].skip_error = compute_mb_distortion(s, &sub_last, &sub_pict);
+            get_sub_picture(s, x, y, last_data,     last_linesize,
+                                 sub_last_data, sub_last_linesize);
+            get_sub_picture(s, x, y,      data,          linesize,
+                                 sub_pict_data, sub_pict_linesize);
+
+            s->mb[i].skip_error = compute_mb_distortion(s,
+                                            sub_last_data, sub_last_linesize,
+                                            sub_pict_data, sub_pict_linesize);
         }
     }
 }
@@ -935,7 +965,11 @@ static void write_strip_header(CinepakEncContext *s, int y, int h, int keyframe,
     //av_log(s->avctx, AV_LOG_INFO, "write_strip_header() %x keyframe=%d\n", buf[0], keyframe);
 }
 
-static int rd_strip(CinepakEncContext *s, int y, int h, int keyframe, AVPicture *last_pict, AVPicture *pict, AVPicture *scratch_pict, unsigned char *buf, int64_t *best_score
+static int rd_strip(CinepakEncContext *s, int y, int h, int keyframe,
+                    uint8_t *last_data[4], int last_linesize[4],
+                    uint8_t *data[4], int linesize[4],
+                    uint8_t *scratch_data[4], int scratch_linesize[4],
+                    unsigned char *buf, int64_t *best_score
 #ifdef CINEPAK_REPORT_SERR
 , int64_t *best_serr
 #endif
@@ -953,7 +987,8 @@ static int rd_strip(CinepakEncContext *s, int y, int h, int keyframe, AVPicture
     int v1shrunk, v4shrunk;
 
     if(!keyframe)
-        calculate_skip_errors(s, h, last_pict, pict, &info);
+        calculate_skip_errors(s, h, last_data, last_linesize, data, linesize,
+                              &info);
 
     //try some powers of 4 for the size of the codebooks
     //constraint the v4 codebook to be no bigger than v1 one,
@@ -971,7 +1006,8 @@ static int rd_strip(CinepakEncContext *s, int y, int h, int keyframe, AVPicture
                 if(mode == MODE_V1_ONLY) {
                     info.v1_size = v1_size;
 // the size may shrink even before optimizations if the input is short:
-                    info.v1_size = quantize(s, h, pict, 1, &info, ENC_UNCERTAIN);
+                    info.v1_size = quantize(s, h, data, linesize, 1,
+                                            &info, ENC_UNCERTAIN);
                     if(info.v1_size < v1_size)
 // too few eligible blocks, no sense in trying bigger sizes
                         v1enough = 1;
@@ -984,7 +1020,8 @@ static int rd_strip(CinepakEncContext *s, int y, int h, int keyframe, AVPicture
 
                     if(mode == MODE_V1_V4) {
                         info.v4_size = v4_size;
-                        info.v4_size = quantize(s, h, pict, 0, &info, ENC_UNCERTAIN);
+                        info.v4_size = quantize(s, h, data, linesize, 0,
+                                                &info, ENC_UNCERTAIN);
                         if(info.v4_size < v4_size)
 // too few eligible blocks, no sense in trying bigger sizes
                             v4enough = 1;
@@ -1005,14 +1042,14 @@ static int rd_strip(CinepakEncContext *s, int y, int h, int keyframe, AVPicture
 // recompute the codebooks, omitting the extra blocks
 // we assume we _may_ come here with more blocks to encode than before
                     info.v1_size = v1_size;
-                    new_v1_size = quantize(s, h, pict, 1, &info, ENC_V1);
+                    new_v1_size = quantize(s, h, data, linesize, 1, &info, ENC_V1);
                     if(new_v1_size < info.v1_size){
                         //av_log(s->avctx, AV_LOG_INFO, "mode %i, %3i, %3i: cut v1 codebook to %i entries\n", mode, v1_size, v4_size, new_v1_size);
                         info.v1_size = new_v1_size;
                     }
 // we assume we _may_ come here with more blocks to encode than before
                     info.v4_size = v4_size;
-                    new_v4_size = quantize(s, h, pict, 0, &info, ENC_V4);
+                    new_v4_size = quantize(s, h, data, linesize, 0, &info, ENC_V4);
                     if(new_v4_size < info.v4_size) {
                         //av_log(s->avctx, AV_LOG_INFO, "mode %i, %3i, %3i: cut v4 codebook to %i entries at first iteration\n", mode, v1_size, v4_size, new_v4_size);
                         info.v4_size = new_v4_size;
@@ -1033,7 +1070,7 @@ static int rd_strip(CinepakEncContext *s, int y, int h, int keyframe, AVPicture
 // recompute the codebooks, omitting the extra blocks
                         if(v1shrunk) {
                             info.v1_size = v1_size;
-                            new_v1_size = quantize(s, h, pict, 1, &info, ENC_V1);
+                            new_v1_size = quantize(s, h, data, linesize, 1, &info, ENC_V1);
                             if(new_v1_size < info.v1_size){
                                 //av_log(s->avctx, AV_LOG_INFO, "mode %i, %3i, %3i: cut v1 codebook to %i entries\n", mode, v1_size, v4_size, new_v1_size);
                                 info.v1_size = new_v1_size;
@@ -1041,7 +1078,7 @@ static int rd_strip(CinepakEncContext *s, int y, int h, int keyframe, AVPicture
                         }
                         if(v4shrunk) {
                             info.v4_size = v4_size;
-                            new_v4_size = quantize(s, h, pict, 0, &info, ENC_V4);
+                            new_v4_size = quantize(s, h, data, linesize, 0, &info, ENC_V4);
                             if(new_v4_size < info.v4_size) {
                                 //av_log(s->avctx, AV_LOG_INFO, "mode %i, %3i, %3i: cut v4 codebook to %i entries\n", mode, v1_size, v4_size, new_v4_size);
                                 info.v4_size = new_v4_size;
@@ -1050,7 +1087,7 @@ static int rd_strip(CinepakEncContext *s, int y, int h, int keyframe, AVPicture
                     }
                 }
 
-                //av_log(s->avctx, AV_LOG_INFO, "%3i %3i score = %lli\n", v1_size, v4_size, (long long int)score);
+                //av_log(s->avctx, AV_LOG_INFO, "%3i %3i score = %"PRId64"\n", v1_size, v4_size, score);
 
                 if(best_size == 0 || score < *best_score) {
 
@@ -1058,12 +1095,15 @@ static int rd_strip(CinepakEncContext *s, int y, int h, int keyframe, AVPicture
 #ifdef CINEPAK_REPORT_SERR
                     *best_serr = serr;
 #endif
-                    best_size = encode_mode(s, h, scratch_pict, last_pict, &info, s->strip_buf + STRIP_HEADER_SIZE);
+                    best_size = encode_mode(s, h,
+                                            scratch_data, scratch_linesize,
+                                            last_data, last_linesize, &info,
+                                            s->strip_buf + STRIP_HEADER_SIZE);
 
-                    //av_log(s->avctx, AV_LOG_INFO, "mode %i, %3i, %3i: %18lli %i B", mode, info.v1_size, info.v4_size, (long long int)score, best_size);
+                    //av_log(s->avctx, AV_LOG_INFO, "mode %i, %3i, %3i: %18"PRId64" %i B", mode, info.v1_size, info.v4_size, score, best_size);
                     //av_log(s->avctx, AV_LOG_INFO, "\n");
 #ifdef CINEPAK_REPORT_SERR
-                    av_log(s->avctx, AV_LOG_INFO, "mode %i, %3i, %3i: %18lli %i B\n", mode, v1_size, v4_size, (long long int)serr, best_size);
+                    av_log(s->avctx, AV_LOG_INFO, "mode %i, %3i, %3i: %18"PRId64" %i B\n", mode, v1_size, v4_size, serr, best_size);
 #endif
 
 #ifdef CINEPAKENC_DEBUG
@@ -1118,10 +1158,12 @@ static int write_cvid_header(CinepakEncContext *s, unsigned char *buf, int num_s
     return CVID_HEADER_SIZE;
 }
 
-static int rd_frame(CinepakEncContext *s, const AVFrame *frame, int isakeyframe, unsigned char *buf, int buf_size)
+static int rd_frame(CinepakEncContext *s, const AVFrame *frame,
+                    int isakeyframe, unsigned char *buf, int buf_size)
 {
     int num_strips, strip, i, y, nexty, size, temp_size;
-    AVPicture last_pict, pict, scratch_pict;
+    uint8_t *last_data    [4], *data    [4], *scratch_data    [4];
+    int      last_linesize[4],  linesize[4],  scratch_linesize[4];
     int64_t best_score = 0, score, score_temp;
 #ifdef CINEPAK_REPORT_SERR
     int64_t best_serr = 0, serr, serr_temp;
@@ -1135,9 +1177,11 @@ static int rd_frame(CinepakEncContext *s, const AVFrame *frame, int isakeyframe,
         for(y = 0; y < s->h; y += 2) {
             for(x = 0; x < s->w; x += 2) {
                 uint8_t *ir[2]; int32_t r, g, b, rr, gg, bb;
-                ir[0] = ((AVPicture*)frame)->data[0] + x*3 + y*((AVPicture*)frame)->linesize[0];
-                ir[1] = ir[0] + ((AVPicture*)frame)->linesize[0];
-                get_sub_picture(s, x, y, (AVPicture*)s->input_frame, &scratch_pict);
+                ir[0] = frame->data[0] + x*3 + y*frame->linesize[0];
+                ir[1] = ir[0] + frame->linesize[0];
+                get_sub_picture(s, x, y,
+                                s->input_frame->data, s->input_frame->linesize,
+                                scratch_data, scratch_linesize);
                 r = g = b = 0;
                 for(i=0; i<4; ++i) {
                     int i1, i2;
@@ -1152,7 +1196,7 @@ static int rd_frame(CinepakEncContext *s, const AVFrame *frame, int isakeyframe,
                     rr = (2396625*rr + 4793251*gg + 1198732*bb) >> 23;
                     if(      rr <   0) rr =   0;
                     else if (rr > 255) rr = 255;
-                    scratch_pict.data[0][i1 + i2*scratch_pict.linesize[0]] = rr;
+                    scratch_data[0][i1 + i2*scratch_linesize[0]] = rr;
                 }
 // let us scale down as late as possible
 //                r /= 4; g /= 4; b /= 4;
@@ -1161,13 +1205,13 @@ static int rd_frame(CinepakEncContext *s, const AVFrame *frame, int isakeyframe,
                 rr = (-299683*r - 599156*g + 898839*b) >> 23;
                 if(      rr < -128) rr = -128;
                 else if (rr >  127) rr =  127;
-                scratch_pict.data[1][0] = rr + 128; // quantize needs unsigned
+                scratch_data[1][0] = rr + 128; // quantize needs unsigned
 // "V"
 //                rr = 0.3571*r - 0.2857*g - 0.0714*b;
                 rr = (748893*r - 599156*g - 149737*b) >> 23;
                 if(      rr < -128) rr = -128;
                 else if (rr >  127) rr =  127;
-                scratch_pict.data[2][0] = rr + 128; // quantize needs unsigned
+                scratch_data[2][0] = rr + 128; // quantize needs unsigned
             }
         }
     }
@@ -1196,13 +1240,24 @@ static int rd_frame(CinepakEncContext *s, const AVFrame *frame, int isakeyframe,
             }
 
             if(s->pix_fmt == AV_PIX_FMT_RGB24)
-                get_sub_picture(s, 0, y, (AVPicture*)s->input_frame,    &pict);
+                get_sub_picture(s, 0, y,
+                                s->input_frame->data, s->input_frame->linesize,
+                                data, linesize);
             else
-                get_sub_picture(s, 0, y, (AVPicture*)frame,              &pict);
-            get_sub_picture(s, 0, y, (AVPicture*)s->last_frame,    &last_pict);
-            get_sub_picture(s, 0, y, (AVPicture*)s->scratch_frame, &scratch_pict);
-
-            if((temp_size = rd_strip(s, y, strip_height, isakeyframe, &last_pict, &pict, &scratch_pict, s->frame_buf + size + CVID_HEADER_SIZE, &score_temp
+                get_sub_picture(s, 0, y,
+                                (uint8_t **)frame->data, (int*)frame->linesize,
+                                data, linesize);
+            get_sub_picture(s, 0, y,
+                            s->last_frame->data, s->last_frame->linesize,
+                            last_data, last_linesize);
+            get_sub_picture(s, 0, y,
+                            s->scratch_frame->data, s->scratch_frame->linesize,
+                            scratch_data, scratch_linesize);
+
+            if((temp_size = rd_strip(s, y, strip_height, isakeyframe,
+                                     last_data, last_linesize, data, linesize,
+                                     scratch_data, scratch_linesize,
+                                     s->frame_buf + size + CVID_HEADER_SIZE, &score_temp
 #ifdef CINEPAK_REPORT_SERR
 , &serr_temp
 #endif
@@ -1224,9 +1279,9 @@ static int rd_frame(CinepakEncContext *s, const AVFrame *frame, int isakeyframe,
             best_serr = serr;
 #endif
             best_size = size + write_cvid_header(s, s->frame_buf, num_strips, size, isakeyframe);
-            //av_log(s->avctx, AV_LOG_INFO, "best number of strips so far: %2i, %12lli, %i B\n", num_strips, (long long int)score, best_size);
+            //av_log(s->avctx, AV_LOG_INFO, "best number of strips so far: %2i, %12"PRId64", %i B\n", num_strips, score, best_size);
 #ifdef CINEPAK_REPORT_SERR
-            av_log(s->avctx, AV_LOG_INFO, "best number of strips so far: %2i, %12lli, %i B\n", num_strips, (long long int)serr, best_size);
+            av_log(s->avctx, AV_LOG_INFO, "best number of strips so far: %2i, %12"PRId64", %i B\n", num_strips, serr, best_size);
 #endif
 
             FFSWAP(AVFrame *, s->best_frame, s->scratch_frame);
@@ -1275,7 +1330,7 @@ static int cinepak_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
 
     s->lambda = frame->quality ? frame->quality - 1 : 2 * FF_LAMBDA_SCALE;
 
-    if ((ret = ff_alloc_packet2(avctx, pkt, s->frame_buf_size)) < 0)
+    if ((ret = ff_alloc_packet2(avctx, pkt, s->frame_buf_size, 0)) < 0)
         return ret;
     ret = rd_frame(s, frame, (s->curframe == 0), pkt->data, s->frame_buf_size);
     pkt->size = ret;