Browse code

Adding Cinepak encoder

With permission of Tomas Härdin applied by Rl aetey.se

Signed-off-by: Michael Niedermayer <michaelni@gmx.at>

Tomas Härdin authored on 2014/01/22 19:12:11
Showing 4 changed files
... ...
@@ -155,6 +155,7 @@ OBJS-$(CONFIG_CAVS_DECODER)            += cavs.o cavsdec.o cavsdsp.o \
155 155
 OBJS-$(CONFIG_CDGRAPHICS_DECODER)      += cdgraphics.o
156 156
 OBJS-$(CONFIG_CDXL_DECODER)            += cdxl.o
157 157
 OBJS-$(CONFIG_CINEPAK_DECODER)         += cinepak.o
158
+OBJS-$(CONFIG_CINEPAK_ENCODER)         += cinepakenc.o
158 159
 OBJS-$(CONFIG_CLJR_DECODER)            += cljr.o
159 160
 OBJS-$(CONFIG_CLJR_ENCODER)            += cljr.o
160 161
 OBJS-$(CONFIG_CLLC_DECODER)            += cllc.o
... ...
@@ -122,7 +122,7 @@ void avcodec_register_all(void)
122 122
     REGISTER_DECODER(CAVS,              cavs);
123 123
     REGISTER_DECODER(CDGRAPHICS,        cdgraphics);
124 124
     REGISTER_DECODER(CDXL,              cdxl);
125
-    REGISTER_DECODER(CINEPAK,           cinepak);
125
+    REGISTER_ENCDEC (CINEPAK,           cinepak);
126 126
     REGISTER_ENCDEC (CLJR,              cljr);
127 127
     REGISTER_DECODER(CLLC,              cllc);
128 128
     REGISTER_ENCDEC (COMFORTNOISE,      comfortnoise);
129 129
new file mode 100644
... ...
@@ -0,0 +1,823 @@
0
+/*
1

                
2
+ * http://titan.codemill.se/~tomhar/cinepakenc.patch
3
+
4
+Permission is hereby granted, free of charge, to any person obtaining a
5
+copy of this software and associated documentation files (the "Software"),
6
+to deal in the Software without restriction, including without limitation
7
+the rights to use, copy, modify, merge, publish, distribute, sublicense,
8
+and/or sell copies of the Software, and to permit persons to whom the
9
+Software is furnished to do so, subject to the following conditions:
10
+
11
+The above copyright notice and this permission notice shall be included
12
+in all copies or substantial portions of the Software.
13
+
14
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18
+OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19
+ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20
+OTHER DEALINGS IN THE SOFTWARE.
21
+
22
+ */
23
+
24
+#include "libavutil/intreadwrite.h"
25
+#include "avcodec.h"
26
+#include "libavutil/lfg.h"
27
+#include "elbg.h"
28
+
29
+#define CVID_HEADER_SIZE 10
30
+#define STRIP_HEADER_SIZE 12
31
+#define CHUNK_HEADER_SIZE 4
32
+
33
+#define MB_SIZE 4           //4x4 MBs
34
+#define MB_AREA (MB_SIZE*MB_SIZE)
35
+
36
+#define VECTOR_MAX 6        //six or four entries per vector depending on format
37
+#define CODEBOOK_MAX 256
38
+#define CODEBOOK_NUM 5      //five potential codebooks (1, 4, 16, 64, 256) for V1 and V4
39
+
40
+#define MAX_STRIPS  1       //Note: having fewer choices regarding the number of strip speeds up encoding (obviously)
41
+#define MIN_STRIPS  1       //Note: having more strips speeds up encoding the frame (this is less obvious)
42
+
43
+typedef enum {
44
+    MODE_V1_ONLY = 0,
45
+    MODE_V1_V4,
46
+    MODE_MC,
47
+
48
+    MODE_COUNT,
49
+} CinepakMode;
50
+
51
+typedef enum {
52
+    ENC_V1,
53
+    ENC_V4,
54
+    ENC_SKIP
55
+} mb_encoding;
56
+
57
+typedef struct {
58
+    int v1_vector;                  //index into v1 codebook
59
+    int v1_error;                   //error when using V1 encoding
60
+    int v4_vector[CODEBOOK_NUM][4]; //indices into v4 codebooks
61
+    int v4_error[CODEBOOK_NUM];     //error when using V4 encodings
62
+    int skip_error;                 //error when block is skipped (aka copied from last frame)
63
+    mb_encoding best_encoding;      //last result from calculate_mode_score()
64
+} mb_info;
65
+
66
+typedef struct {
67
+    int v1_codebook[CODEBOOK_MAX*VECTOR_MAX];
68
+    int *v4_codebook;
69
+} strip_info;
70
+
71
+typedef struct {
72
+    AVCodecContext *avctx;
73
+    unsigned char *pict_bufs[3], *strip_buf, *frame_buf;
74
+    AVFrame last_frame;
75
+    AVFrame best_frame;
76
+    AVFrame scratch_frame;
77
+    enum PixelFormat pix_fmt;
78
+    int w, h;
79
+    int curframe, keyint;
80
+    AVLFG randctx;
81
+    uint64_t lambda;
82
+    int *codebook_input;
83
+    int *codebook_closest;
84
+    mb_info *mb;                                //MB RD state
85
+#ifdef CINEPAKENC_DEBUG
86
+    mb_info *best_mb;                           //TODO: remove. only used for printing stats
87
+#endif
88
+    int num_v1_mode, num_v4_mode, num_mc_mode;
89
+    int num_v1_encs, num_v4_encs, num_skips;
90
+} CinepakEncContext;
91
+
92
+static av_cold int cinepak_encode_init(AVCodecContext *avctx)
93
+{
94
+    CinepakEncContext *s = avctx->priv_data;
95
+    int x, mb_count, strip_buf_size, frame_buf_size;
96
+
97
+    if (avctx->width & 3 || avctx->height & 3) {
98
+        av_log(avctx, AV_LOG_ERROR, "width and height must be multiples of four (got %ix%i)\n",
99
+                avctx->width, avctx->height);
100
+        return AVERROR(EINVAL);
101
+    }
102
+
103
+    if (!(s->codebook_input = av_malloc(sizeof(int) * (avctx->pix_fmt == PIX_FMT_YUV420P ? 6 : 4) * (avctx->width * avctx->height) >> 2)))
104
+        return AVERROR(ENOMEM);
105
+
106
+    if (!(s->codebook_closest = av_malloc(sizeof(int) * (avctx->width * avctx->height) >> 2)))
107
+        goto enomem;
108
+
109
+    for(x = 0; x < 3; x++)
110
+        if(!(s->pict_bufs[x] = av_malloc((avctx->pix_fmt == PIX_FMT_YUV420P ? 6 : 4) * (avctx->width * avctx->height) >> 2)))
111
+            goto enomem;
112
+
113
+    mb_count = avctx->width * avctx->height / MB_AREA;
114
+
115
+    //the largest possible chunk is 0x31 with all MBs encoded in V4 mode, which is 34 bits per MB
116
+    strip_buf_size = STRIP_HEADER_SIZE + 3 * CHUNK_HEADER_SIZE + 2 * VECTOR_MAX * CODEBOOK_MAX + 4 * (mb_count + (mb_count + 15) / 16);
117
+
118
+    frame_buf_size = CVID_HEADER_SIZE + MAX_STRIPS * strip_buf_size;
119
+
120
+    if (!(s->strip_buf = av_malloc(strip_buf_size)))
121
+        goto enomem;
122
+
123
+    if (!(s->frame_buf = av_malloc(frame_buf_size)))
124
+        goto enomem;
125
+
126
+    if (!(s->mb = av_malloc(mb_count*sizeof(mb_info))))
127
+        goto enomem;
128
+
129
+#ifdef CINEPAKENC_DEBUG
130
+    if (!(s->best_mb = av_malloc(mb_count*sizeof(mb_info))))
131
+        goto enomem;
132
+#endif
133
+
134
+    av_lfg_init(&s->randctx, 1);
135
+    s->avctx = avctx;
136
+    s->w = avctx->width;
137
+    s->h = avctx->height;
138
+    s->curframe = 0;
139
+    s->keyint = avctx->keyint_min;
140
+    s->pix_fmt = avctx->pix_fmt;
141
+
142
+    //set up AVFrames
143
+    s->last_frame.data[0]        = s->pict_bufs[0];
144
+    s->last_frame.linesize[0]    = s->w;
145
+    s->best_frame.data[0]        = s->pict_bufs[1];
146
+    s->best_frame.linesize[0]    = s->w;
147
+    s->scratch_frame.data[0]     = s->pict_bufs[2];
148
+    s->scratch_frame.linesize[0] = s->w;
149
+
150
+    if(s->pix_fmt == PIX_FMT_YUV420P) {
151
+        s->last_frame.data[1]        = s->last_frame.data[0] + s->w * s->h;
152
+        s->last_frame.data[2]        = s->last_frame.data[1] + ((s->w * s->h) >> 2);
153
+        s->last_frame.linesize[1]    = s->last_frame.linesize[2] = s->w >> 1;
154
+
155
+        s->best_frame.data[1]        = s->best_frame.data[0] + s->w * s->h;
156
+        s->best_frame.data[2]        = s->best_frame.data[1] + ((s->w * s->h) >> 2);
157
+        s->best_frame.linesize[1]    = s->best_frame.linesize[2] = s->w >> 1;
158
+
159
+        s->scratch_frame.data[1]     = s->scratch_frame.data[0] + s->w * s->h;
160
+        s->scratch_frame.data[2]     = s->scratch_frame.data[1] + ((s->w * s->h) >> 2);
161
+        s->scratch_frame.linesize[1] = s->scratch_frame.linesize[2] = s->w >> 1;
162
+    }
163
+
164
+    s->num_v1_mode = s->num_v4_mode = s->num_mc_mode = s->num_v1_encs = s->num_v4_encs = s->num_skips = 0;
165
+
166
+    return 0;
167
+
168
+enomem:
169
+    av_free(s->codebook_input);
170
+    av_free(s->codebook_closest);
171
+    av_free(s->strip_buf);
172
+    av_free(s->frame_buf);
173
+    av_free(s->mb);
174
+#ifdef CINEPAKENC_DEBUG
175
+    av_free(s->best_mb);
176
+#endif
177
+
178
+    for(x = 0; x < 3; x++)
179
+        av_free(s->pict_bufs[x]);
180
+
181
+    return AVERROR(ENOMEM);
182
+}
183
+
184
+static int64_t calculate_mode_score(CinepakEncContext *s, CinepakMode mode, int h, int v1_size, int v4_size, int v4, strip_info *info)
185
+{
186
+    //score = FF_LAMBDA_SCALE * error + lambda * bits
187
+    int x;
188
+    int entry_size = s->pix_fmt == PIX_FMT_YUV420P ? 6 : 4;
189
+    int mb_count = s->w * h / MB_AREA;
190
+    mb_info *mb;
191
+    int64_t score1, score2, score3;
192
+    int64_t ret = s->lambda * ((v1_size ? CHUNK_HEADER_SIZE + v1_size * entry_size : 0) +
193
+                   (v4_size ? CHUNK_HEADER_SIZE + v4_size * entry_size : 0) +
194
+                   CHUNK_HEADER_SIZE) << 3;
195
+
196
+    //av_log(s->avctx, AV_LOG_INFO, "sizes %3i %3i -> %9li score mb_count %i", v1_size, v4_size, ret, mb_count);
197
+
198
+    switch(mode) {
199
+    case MODE_V1_ONLY:
200
+        //one byte per MB
201
+        ret += s->lambda * 8 * mb_count;
202
+
203
+        for(x = 0; x < mb_count; x++) {
204
+            mb = &s->mb[x];
205
+            ret += FF_LAMBDA_SCALE * mb->v1_error;
206
+            mb->best_encoding = ENC_V1;
207
+        }
208
+
209
+        break;
210
+    case MODE_V1_V4:
211
+        //9 or 33 bits per MB
212
+        for(x = 0; x < mb_count; x++) {
213
+            mb = &s->mb[x];
214
+            score1 = s->lambda * 9  + FF_LAMBDA_SCALE * mb->v1_error;
215
+            score2 = s->lambda * 33 + FF_LAMBDA_SCALE * mb->v4_error[v4];
216
+
217
+            if(score1 <= score2) {
218
+                ret += score1;
219
+                mb->best_encoding = ENC_V1;
220
+            } else {
221
+                ret += score2;
222
+                mb->best_encoding = ENC_V4;
223
+            }
224
+        }
225
+
226
+        break;
227
+    case MODE_MC:
228
+        //1, 10 or 34 bits per MB
229
+        for(x = 0; x < mb_count; x++) {
230
+            mb = &s->mb[x];
231
+            score1 = s->lambda * 1  + FF_LAMBDA_SCALE * mb->skip_error;
232
+            score2 = s->lambda * 10 + FF_LAMBDA_SCALE * mb->v1_error;
233
+            score3 = s->lambda * 34 + FF_LAMBDA_SCALE * mb->v4_error[v4];
234
+
235
+
236
+            if(score1 <= score2 && score1 <= score3) {
237
+                ret += score1;
238
+                mb->best_encoding = ENC_SKIP;
239
+            } else if(score2 <= score1 && score2 <= score3) {
240
+                ret += score2;
241
+                mb->best_encoding = ENC_V1;
242
+            } else {
243
+                ret += score3;
244
+                mb->best_encoding = ENC_V4;
245
+            }
246
+        }
247
+
248
+        break;
249
+    }
250
+
251
+    return ret;
252
+}
253
+
254
+static int write_chunk_header(unsigned char *buf, int chunk_type, int chunk_size)
255
+{
256
+    buf[0] = chunk_type;
257
+    AV_WB24(&buf[1], chunk_size + CHUNK_HEADER_SIZE);
258
+    return CHUNK_HEADER_SIZE;
259
+}
260
+
261
+static int encode_codebook(CinepakEncContext *s, int *codebook, int size, int chunk_type_yuv, int chunk_type_gray, unsigned char *buf)
262
+{
263
+    int x, y, ret, entry_size = s->pix_fmt == PIX_FMT_YUV420P ? 6 : 4;
264
+
265
+    ret = write_chunk_header(buf, s->pix_fmt == PIX_FMT_YUV420P ? chunk_type_yuv : chunk_type_gray, entry_size * size);
266
+
267
+    for(x = 0; x < size; x++)
268
+        for(y = 0; y < entry_size; y++)
269
+            buf[ret++] = codebook[y + x*entry_size] ^ (y >= 4 ? 0x80 : 0);
270
+
271
+    return ret;
272
+}
273
+
274
+//sets out to the sub picture starting at (x,y) in in
275
+static void get_sub_picture(CinepakEncContext *s, int x, int y, AVPicture *in, AVPicture *out)
276
+{
277
+    out->data[0] = in->data[0] + x + y * in->linesize[0];
278
+    out->linesize[0] = in->linesize[0];
279
+
280
+    if(s->pix_fmt == PIX_FMT_YUV420P) {
281
+        out->data[1] = in->data[1] + (x >> 1) + (y >> 1) * in->linesize[1];
282
+        out->linesize[1] = in->linesize[1];
283
+
284
+        out->data[2] = in->data[2] + (x >> 1) + (y >> 1) * in->linesize[2];
285
+        out->linesize[2] = in->linesize[2];
286
+    }
287
+}
288
+
289
+//decodes the V1 vector in mb into the 4x4 MB pointed to by sub_pict
290
+static void decode_v1_vector(CinepakEncContext *s, AVPicture *sub_pict, mb_info *mb, strip_info *info)
291
+{
292
+    int entry_size = s->pix_fmt == PIX_FMT_YUV420P ? 6 : 4;
293
+
294
+    sub_pict->data[0][0] =
295
+            sub_pict->data[0][1] =
296
+            sub_pict->data[0][    sub_pict->linesize[0]] =
297
+            sub_pict->data[0][1+  sub_pict->linesize[0]] = info->v1_codebook[mb->v1_vector*entry_size];
298
+
299
+    sub_pict->data[0][2] =
300
+            sub_pict->data[0][3] =
301
+            sub_pict->data[0][2+  sub_pict->linesize[0]] =
302
+            sub_pict->data[0][3+  sub_pict->linesize[0]] = info->v1_codebook[mb->v1_vector*entry_size+1];
303
+
304
+    sub_pict->data[0][2*sub_pict->linesize[0]] =
305
+            sub_pict->data[0][1+2*sub_pict->linesize[0]] =
306
+            sub_pict->data[0][  3*sub_pict->linesize[0]] =
307
+            sub_pict->data[0][1+3*sub_pict->linesize[0]] = info->v1_codebook[mb->v1_vector*entry_size+2];
308
+
309
+    sub_pict->data[0][2+2*sub_pict->linesize[0]] =
310
+            sub_pict->data[0][3+2*sub_pict->linesize[0]] =
311
+            sub_pict->data[0][2+3*sub_pict->linesize[0]] =
312
+            sub_pict->data[0][3+3*sub_pict->linesize[0]] = info->v1_codebook[mb->v1_vector*entry_size+3];
313
+
314
+    if(s->pix_fmt == PIX_FMT_YUV420P) {
315
+        sub_pict->data[1][0] =
316
+            sub_pict->data[1][1] =
317
+            sub_pict->data[1][    sub_pict->linesize[1]] =
318
+            sub_pict->data[1][1+  sub_pict->linesize[1]] = info->v1_codebook[mb->v1_vector*entry_size+4];
319
+
320
+        sub_pict->data[2][0] =
321
+            sub_pict->data[2][1] =
322
+            sub_pict->data[2][    sub_pict->linesize[2]] =
323
+            sub_pict->data[2][1+  sub_pict->linesize[2]] = info->v1_codebook[mb->v1_vector*entry_size+5];
324
+    }
325
+}
326
+
327
+//decodes the V4 vectors in mb into the 4x4 MB pointed to by sub_pict
328
+static void decode_v4_vector(CinepakEncContext *s, AVPicture *sub_pict, int *v4_vector, strip_info *info)
329
+{
330
+    int i, x, y, entry_size = s->pix_fmt == PIX_FMT_YUV420P ? 6 : 4;
331
+
332
+    for(i = y = 0; y < 4; y += 2) {
333
+        for(x = 0; x < 4; x += 2, i++) {
334
+            sub_pict->data[0][x   +     y*sub_pict->linesize[0]] = info->v4_codebook[v4_vector[i]*entry_size];
335
+            sub_pict->data[0][x+1 +     y*sub_pict->linesize[0]] = info->v4_codebook[v4_vector[i]*entry_size+1];
336
+            sub_pict->data[0][x   + (y+1)*sub_pict->linesize[0]] = info->v4_codebook[v4_vector[i]*entry_size+2];
337
+            sub_pict->data[0][x+1 + (y+1)*sub_pict->linesize[0]] = info->v4_codebook[v4_vector[i]*entry_size+3];
338
+
339
+            if(s->pix_fmt == PIX_FMT_YUV420P) {
340
+                sub_pict->data[1][(x>>1) + (y>>1)*sub_pict->linesize[1]] = info->v4_codebook[v4_vector[i]*entry_size+4];
341
+                sub_pict->data[2][(x>>1) + (y>>1)*sub_pict->linesize[2]] = info->v4_codebook[v4_vector[i]*entry_size+5];
342
+            }
343
+        }
344
+    }
345
+}
346
+
347
+static int encode_mode(CinepakEncContext *s, CinepakMode mode, int h, int v1_size, int v4_size, int v4, AVPicture *scratch_pict, strip_info *info, unsigned char *buf)
348
+{
349
+    int x, y, z, flags, bits, temp_size, header_ofs, ret = 0, mb_count = s->w * h / MB_AREA;
350
+    int needs_extra_bit, should_write_temp;
351
+    unsigned char temp[64]; //32/2 = 16 V4 blocks at 4 B each -> 64 B
352
+    mb_info *mb;
353
+    AVPicture sub_scratch;
354
+
355
+    //encode codebooks
356
+    if(v1_size)
357
+        ret += encode_codebook(s, info->v1_codebook, v1_size, 0x22, 0x26, buf + ret);
358
+
359
+    if(v4_size)
360
+        ret += encode_codebook(s, info->v4_codebook, v4_size, 0x20, 0x24, buf + ret);
361
+
362
+    //update scratch picture
363
+    for(z = y = 0; y < h; y += MB_SIZE) {
364
+        for(x = 0; x < s->w; x += MB_SIZE, z++) {
365
+            mb = &s->mb[z];
366
+
367
+            if(mode == MODE_MC && mb->best_encoding == ENC_SKIP)
368
+                continue;
369
+
370
+            get_sub_picture(s, x, y, scratch_pict, &sub_scratch);
371
+
372
+            if(mode == MODE_V1_ONLY || mb->best_encoding == ENC_V1)
373
+                decode_v1_vector(s, &sub_scratch, mb, info);
374
+            else if(mode != MODE_V1_ONLY && mb->best_encoding == ENC_V4)
375
+                decode_v4_vector(s, &sub_scratch, mb->v4_vector[v4], info);
376
+        }
377
+    }
378
+
379
+    switch(mode) {
380
+    case MODE_V1_ONLY:
381
+        //av_log(s->avctx, AV_LOG_INFO, "mb_count = %i\n", mb_count);
382
+        ret += write_chunk_header(buf + ret, 0x32, mb_count);
383
+
384
+        for(x = 0; x < mb_count; x++)
385
+            buf[ret++] = s->mb[x].v1_vector;
386
+
387
+        break;
388
+    case MODE_V1_V4:
389
+        //remember header position
390
+        header_ofs = ret;
391
+        ret += CHUNK_HEADER_SIZE;
392
+
393
+        for(x = 0; x < mb_count; x += 32) {
394
+            flags = 0;
395
+            for(y = x; y < FFMIN(x+32, mb_count); y++)
396
+                if(s->mb[y].best_encoding == ENC_V4)
397
+                    flags |= 1 << (31 - y + x);
398
+
399
+            AV_WB32(&buf[ret], flags);
400
+            ret += 4;
401
+
402
+            for(y = x; y < FFMIN(x+32, mb_count); y++) {
403
+                mb = &s->mb[y];
404
+
405
+                if(mb->best_encoding == ENC_V1)
406
+                    buf[ret++] = mb->v1_vector;
407
+                else
408
+                    for(z = 0; z < 4; z++)
409
+                        buf[ret++] = mb->v4_vector[v4][z];
410
+            }
411
+        }
412
+
413
+        write_chunk_header(buf + header_ofs, 0x30, ret - header_ofs - CHUNK_HEADER_SIZE);
414
+
415
+        break;
416
+    case MODE_MC:
417
+        //remember header position
418
+        header_ofs = ret;
419
+        ret += CHUNK_HEADER_SIZE;
420
+        flags = bits = temp_size = 0;
421
+
422
+        for(x = 0; x < mb_count; x++) {
423
+            mb = &s->mb[x];
424
+            flags |= (mb->best_encoding != ENC_SKIP) << (31 - bits++);
425
+            needs_extra_bit = 0;
426
+            should_write_temp = 0;
427
+
428
+            if(mb->best_encoding != ENC_SKIP) {
429
+                if(bits < 32)
430
+                    flags |= (mb->best_encoding == ENC_V4) << (31 - bits++);
431
+                else
432
+                    needs_extra_bit = 1;
433
+            }
434
+
435
+            if(bits == 32) {
436
+                AV_WB32(&buf[ret], flags);
437
+                ret += 4;
438
+                flags = bits = 0;
439
+
440
+                if(mb->best_encoding == ENC_SKIP || needs_extra_bit) {
441
+                    memcpy(&buf[ret], temp, temp_size);
442
+                    ret += temp_size;
443
+                    temp_size = 0;
444
+                } else
445
+                    should_write_temp = 1;
446
+            }
447
+
448
+            if(needs_extra_bit) {
449
+                flags = (mb->best_encoding == ENC_V4) << 31;
450
+                bits = 1;
451
+            }
452
+
453
+            if(mb->best_encoding == ENC_V1)
454
+                temp[temp_size++] = mb->v1_vector;
455
+            else if(mb->best_encoding == ENC_V4)
456
+                for(z = 0; z < 4; z++)
457
+                    temp[temp_size++] = mb->v4_vector[v4][z];
458
+
459
+            if(should_write_temp) {
460
+                memcpy(&buf[ret], temp, temp_size);
461
+                ret += temp_size;
462
+                temp_size = 0;
463
+            }
464
+        }
465
+
466
+        if(bits > 0) {
467
+            AV_WB32(&buf[ret], flags);
468
+            ret += 4;
469
+            memcpy(&buf[ret], temp, temp_size);
470
+            ret += temp_size;
471
+        }
472
+
473
+        write_chunk_header(buf + header_ofs, 0x31, ret - header_ofs - CHUNK_HEADER_SIZE);
474
+
475
+        break;
476
+    }
477
+
478
+    return ret;
479
+}
480
+
481
+//computes distortion of 4x4 MB in b compared to a
482
+static int compute_mb_distortion(CinepakEncContext *s, AVPicture *a, AVPicture *b)
483
+{
484
+    int x, y, p, d, ret = 0;
485
+
486
+    for(y = 0; y < MB_SIZE; y++) {
487
+        for(x = 0; x < MB_SIZE; x++) {
488
+            d = a->data[0][x + y*a->linesize[0]] - b->data[0][x + y*b->linesize[0]];
489
+            ret += d*d;
490
+        }
491
+    }
492
+
493
+    if(s->pix_fmt == PIX_FMT_YUV420P) {
494
+        for(p = 1; p <= 2; p++) {
495
+            for(y = 0; y < MB_SIZE/2; y++) {
496
+                for(x = 0; x < MB_SIZE/2; x++) {
497
+                    d = a->data[p][x + y*a->linesize[p]] - b->data[p][x + y*b->linesize[p]];
498
+                    ret += d*d;
499
+                }
500
+            }
501
+        }
502
+    }
503
+
504
+    return ret;
505
+}
506
+
507
+static int quantize(CinepakEncContext *s, int h, AVPicture *pict, int v1mode, int size, int v4, strip_info *info)
508
+{
509
+    int x, y, i, j, k, x2, y2, x3, y3, plane, shift;
510
+    int entry_size = s->pix_fmt == PIX_FMT_YUV420P ? 6 : 4;
511
+    int *codebook = v1mode ? info->v1_codebook : info->v4_codebook;
512
+    int64_t total_error = 0;
513
+    uint8_t vq_pict_buf[(MB_AREA*3)/2];
514
+    AVPicture sub_pict, vq_pict;
515
+
516
+    for(i = y = 0; y < h; y += MB_SIZE) {
517
+        for(x = 0; x < s->w; x += MB_SIZE, i += v1mode ? 1 : 4) {
518
+            int *base = s->codebook_input + i*entry_size;
519
+
520
+            if(v1mode) {
521
+                //subsample
522
+                for(j = y2 = 0; y2 < entry_size; y2 += 2) {
523
+                    for(x2 = 0; x2 < 4; x2 += 2, j++) {
524
+                        plane = y2 < 4 ? 0 : 1 + (x2 >> 1);
525
+                        shift = y2 < 4 ? 0 : 1;
526
+                        x3 = shift ? 0 : x2;
527
+                        y3 = shift ? 0 : y2;
528
+                        base[j] = (pict->data[plane][((x+x3) >> shift) +      ((y+y3) >> shift)      * pict->linesize[plane]] +
529
+                                   pict->data[plane][((x+x3) >> shift) + 1 +  ((y+y3) >> shift)      * pict->linesize[plane]] +
530
+                                   pict->data[plane][((x+x3) >> shift) +     (((y+y3) >> shift) + 1) * pict->linesize[plane]] +
531
+                                   pict->data[plane][((x+x3) >> shift) + 1 + (((y+y3) >> shift) + 1) * pict->linesize[plane]]) >> 2;
532
+                    }
533
+                }
534
+            } else {
535
+                //copy
536
+                for(j = y2 = 0; y2 < MB_SIZE; y2 += 2) {
537
+                    for(x2 = 0; x2 < MB_SIZE; x2 += 2) {
538
+                        for(k = 0; k < entry_size; k++, j++) {
539
+                            plane = k >= 4 ? k - 3 : 0;
540
+
541
+                            if(k >= 4) {
542
+                                x3 = (x+x2) >> 1;
543
+                                y3 = (y+y2) >> 1;
544
+                            } else {
545
+                                x3 = x + x2 + (k & 1);
546
+                                y3 = y + y2 + (k >> 1);
547
+                            }
548
+
549
+                            base[j] = pict->data[plane][x3 + y3*pict->linesize[plane]];
550
+                        }
551
+                    }
552
+                }
553
+            }
554
+        }
555
+    }
556
+
557
+    ff_init_elbg(s->codebook_input, entry_size, i, codebook, size, 1, s->codebook_closest, &s->randctx);
558
+    ff_do_elbg(s->codebook_input, entry_size, i, codebook, size, 1, s->codebook_closest, &s->randctx);
559
+
560
+    //setup vq_pict, which contains a single MB
561
+    vq_pict.data[0] = vq_pict_buf;
562
+    vq_pict.linesize[0] = MB_SIZE;
563
+    vq_pict.data[1] = &vq_pict_buf[MB_AREA];
564
+    vq_pict.data[2] = vq_pict.data[1] + (MB_AREA >> 2);
565
+    vq_pict.linesize[1] = vq_pict.linesize[2] = MB_SIZE >> 1;
566
+
567
+    //copy indices
568
+    for(i = j = y = 0; y < h; y += MB_SIZE) {
569
+        for(x = 0; x < s->w; x += MB_SIZE, j++, i += v1mode ? 1 : 4) {
570
+            mb_info *mb = &s->mb[j];
571
+
572
+            //point sub_pict to current MB
573
+            get_sub_picture(s, x, y, pict, &sub_pict);
574
+
575
+            if(v1mode) {
576
+                mb->v1_vector = s->codebook_closest[i];
577
+
578
+                //fill in vq_pict with V1 data
579
+                decode_v1_vector(s, &vq_pict, mb, info);
580
+
581
+                mb->v1_error = compute_mb_distortion(s, &sub_pict, &vq_pict);
582
+                total_error += mb->v1_error;
583
+            } else {
584
+                for(k = 0; k < 4; k++)
585
+                    mb->v4_vector[v4][k] = s->codebook_closest[i+k];
586
+
587
+                //fill in vq_pict with V4 data
588
+                decode_v4_vector(s, &vq_pict, mb->v4_vector[v4], info);
589
+
590
+                mb->v4_error[v4] = compute_mb_distortion(s, &sub_pict, &vq_pict);
591
+                total_error += mb->v4_error[v4];
592
+            }
593
+        }
594
+    }
595
+
596
+    //av_log(s->avctx, AV_LOG_INFO, "mode %i size %i i %i error %li\n", v1mode, size, i, total_error);
597
+
598
+    return 0;
599
+}
600
+
601
+static void calculate_skip_errors(CinepakEncContext *s, int h, AVPicture *last_pict, AVPicture *pict, strip_info *info)
602
+{
603
+    int x, y, i;
604
+    AVPicture sub_last, sub_pict;
605
+
606
+    for(i = y = 0; y < h; y += MB_SIZE) {
607
+        for(x = 0; x < s->w; x += MB_SIZE, i++) {
608
+            get_sub_picture(s, x, y, last_pict, &sub_last);
609
+            get_sub_picture(s, x, y, pict,      &sub_pict);
610
+
611
+            s->mb[i].skip_error = compute_mb_distortion(s, &sub_last, &sub_pict);
612
+        }
613
+    }
614
+}
615
+
616
+static void write_strip_header(CinepakEncContext *s, int y, int h, int keyframe, unsigned char *buf, int strip_size)
617
+{
618
+    buf[0] = keyframe ? 0x11: 0x10;
619
+    AV_WB24(&buf[1], strip_size + STRIP_HEADER_SIZE);
620
+    AV_WB16(&buf[4], y);
621
+    AV_WB16(&buf[6], 0);
622
+    AV_WB16(&buf[8], h);
623
+    AV_WB16(&buf[10], s->w);
624
+}
625
+
626
+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)
627
+{
628
+    int64_t score = 0;
629
+    int best_size = 0, v1_size, v4_size, v4, mb_count = s->w * h / MB_AREA;
630
+    strip_info info;
631
+    CinepakMode best_mode;
632
+    int v4_codebooks[CODEBOOK_NUM][CODEBOOK_MAX*VECTOR_MAX];
633
+
634
+    if(!keyframe)
635
+        calculate_skip_errors(s, h, last_pict, pict, &info);
636
+
637
+    //precompute V4 codebooks
638
+    for(v4_size = 1, v4 = 0; v4_size <= 256; v4_size <<= 2, v4++) {
639
+        info.v4_codebook = v4_codebooks[v4];
640
+        quantize(s, h, pict, 0, v4_size, v4, &info);
641
+    }
642
+
643
+    //try all powers of 4 for the size of the codebooks
644
+    //constraint the v4 codebook to be no bigger than the v1 codebook
645
+    for(v1_size = 1; v1_size <= 256; v1_size <<= 2) {
646
+        //compute V1 codebook
647
+        quantize(s, h, pict, 1, v1_size, -1, &info);
648
+
649
+        for(v4_size = 0, v4 = -1; v4_size <= v1_size; v4_size = v4_size ? v4_size << 2 : v1_size >= 4 ? v1_size >> 2 : 1, v4++) {
650
+            //try all modes
651
+            for(CinepakMode mode = 0; mode < MODE_COUNT; mode++) {
652
+                //don't allow MODE_MC in inter frames
653
+                if(keyframe && mode == MODE_MC)
654
+                    continue;
655
+
656
+                //only allow V1-only mode if v4 codebook is empty
657
+                if(!v4_size && mode != MODE_V1_ONLY)
658
+                    continue;
659
+
660
+                info.v4_codebook = v4 >= 0 ? v4_codebooks[v4] : NULL;
661
+                score = calculate_mode_score(s, mode, h, v1_size, v4_size, v4, &info);
662
+
663
+                //av_log(s->avctx, AV_LOG_INFO, "%3i %3i score = %li\n", v1_size, v4_size, score);
664
+
665
+                if(best_size == 0 || score < *best_score) {
666
+                    *best_score = score;
667
+                    best_size = encode_mode(s, mode, h, v1_size, v4_size, v4, scratch_pict, &info, s->strip_buf + STRIP_HEADER_SIZE);
668
+                    best_mode = mode;
669
+
670
+                    av_log(s->avctx, AV_LOG_INFO, "mode %i, %3i, %3i: %18li %i B\n", mode, v1_size, v4_size, score, best_size);
671
+
672
+#ifdef CINEPAKENC_DEBUG
673
+                    //save MB encoding choices
674
+                    memcpy(s->best_mb, s->mb, mb_count*sizeof(mb_info));
675
+#endif
676
+
677
+                    //memcpy(strip_temp + STRIP_HEADER_SIZE, strip_temp, best_size);
678
+                    write_strip_header(s, y, h, keyframe, s->strip_buf, best_size);
679
+                }
680
+            }
681
+        }
682
+    }
683
+
684
+#ifdef CINEPAKENC_DEBUG
685
+    //gather stats. this will only work properly of MAX_STRIPS == 1
686
+    if(best_mode == MODE_V1_ONLY) {
687
+        s->num_v1_mode++;
688
+        s->num_v1_encs += s->w*h/MB_AREA;
689
+    } else {
690
+        if(best_mode == MODE_V1_V4)
691
+            s->num_v4_mode++;
692
+        else
693
+            s->num_mc_mode++;
694
+
695
+        int x;
696
+        for(x = 0; x < s->w*h/MB_AREA; x++)
697
+            if(s->best_mb[x].best_encoding == ENC_V1)
698
+                s->num_v1_encs++;
699
+            else if(s->best_mb[x].best_encoding == ENC_V4)
700
+                s->num_v4_encs++;
701
+            else
702
+                s->num_skips++;
703
+    }
704
+#endif
705
+
706
+    best_size += STRIP_HEADER_SIZE;
707
+    memcpy(buf, s->strip_buf, best_size);
708
+
709
+
710
+    return best_size;
711
+}
712
+
713
+static int write_cvid_header(CinepakEncContext *s, unsigned char *buf, int num_strips, int data_size)
714
+{
715
+    buf[0] = 0;
716
+    AV_WB24(&buf[1], data_size + CVID_HEADER_SIZE);
717
+    AV_WB16(&buf[4], s->w);
718
+    AV_WB16(&buf[6], s->h);
719
+    AV_WB16(&buf[8], num_strips);
720
+
721
+    return CVID_HEADER_SIZE;
722
+}
723
+
724
+static int rd_frame(CinepakEncContext *s, AVFrame *frame, unsigned char *buf, int buf_size)
725
+{
726
+    int num_strips, strip, h, i, y, size, temp_size, best_size;
727
+    AVPicture last_pict, pict, scratch_pict;
728
+    int64_t best_score = 0, score, score_temp;
729
+
730
+    //TODO: support encoding zero strips (meaning skip the whole frame)
731
+    for(num_strips = MIN_STRIPS; num_strips <= MAX_STRIPS && num_strips <= s->h / MB_SIZE; num_strips++) {
732
+        score = 0;
733
+        size = 0;
734
+        h = s->h / num_strips;
735
+        //make h into next multiple of 4
736
+        h += 4 - (h & 3);
737
+
738
+        for(strip = 0; strip < num_strips; strip++) {
739
+            y = strip*h;
740
+
741
+            get_sub_picture(s, 0, y, (AVPicture*)frame,            &pict);
742
+            get_sub_picture(s, 0, y, (AVPicture*)&s->last_frame,    &last_pict);
743
+            get_sub_picture(s, 0, y, (AVPicture*)&s->scratch_frame, &scratch_pict);
744
+
745
+            if((temp_size = rd_strip(s, y, FFMIN(h, s->h - y), frame->key_frame, &last_pict, &pict, &scratch_pict, s->frame_buf + CVID_HEADER_SIZE, &score_temp)) < 0)
746
+                return temp_size;
747
+
748
+            score += score_temp;
749
+            size += temp_size;
750
+        }
751
+
752
+        if(best_score == 0 || score < best_score) {
753
+            best_score = score;
754
+            best_size = size + write_cvid_header(s, s->frame_buf, num_strips, size);
755
+            av_log(s->avctx, AV_LOG_INFO, "best number of strips so far: %2i, %12li, %i B\n", num_strips, score, best_size);
756
+
757
+            FFSWAP(AVFrame, s->best_frame, s->scratch_frame);
758
+        }
759
+    }
760
+
761
+    memcpy(buf, s->frame_buf, best_size);
762
+
763
+    return best_size;
764
+}
765
+
766
+static int cinepak_encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data)
767
+{
768
+    CinepakEncContext *s = avctx->priv_data;
769
+    AVFrame *frame = data;
770
+    int ret;
771
+
772
+    s->lambda = frame->quality ? frame->quality - 1 : 2 * FF_LAMBDA_SCALE;
773
+
774
+    frame->key_frame = s->curframe == 0;
775
+    frame->pict_type = frame->key_frame ? FF_I_TYPE : FF_P_TYPE;
776
+
777
+    ret = rd_frame(s, frame, buf, buf_size);
778
+
779
+    avctx->coded_frame = frame;
780
+
781
+    FFSWAP(AVFrame, s->last_frame, s->best_frame);
782
+
783
+    if (++s->curframe >= s->keyint)
784
+        s->curframe = 0;
785
+
786
+    return ret;
787
+}
788
+
789
+static av_cold int cinepak_encode_end(AVCodecContext *avctx)
790
+{
791
+    CinepakEncContext *s = avctx->priv_data;
792
+    int x;
793
+
794
+    av_free(s->codebook_input);
795
+    av_free(s->codebook_closest);
796
+    av_free(s->strip_buf);
797
+    av_free(s->frame_buf);
798
+    av_free(s->mb);
799
+#ifdef CINEPAKENC_DEBUG
800
+    av_free(s->best_mb);
801
+#endif
802
+
803
+    for(x = 0; x < 3; x++)
804
+        av_free(s->pict_bufs[x]);
805
+
806
+    av_log(avctx, AV_LOG_INFO, "strip coding stats: %i V1 mode, %i V4 mode, %i MC mode (%i V1 encs, %i V4 encs, %i skips)\n",
807
+        s->num_v1_mode, s->num_v4_mode, s->num_mc_mode, s->num_v1_encs, s->num_v4_encs, s->num_skips);
808
+
809
+    return 0;
810
+}
811
+
812
+AVCodec ff_cinepak_encoder = {
813
+    "cinepak",
814
+    AVMEDIA_TYPE_VIDEO,
815
+    CODEC_ID_CINEPAK,
816
+    sizeof(CinepakEncContext),
817
+    cinepak_encode_init,
818
+    cinepak_encode_frame,
819
+    cinepak_encode_end,
820
+    .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_GRAY8, PIX_FMT_NONE},
821
+    .long_name= NULL_IF_CONFIG_SMALL("Cinepak / CVID"),
822
+};
... ...
@@ -29,8 +29,8 @@
29 29
 #include "libavutil/version.h"
30 30
 
31 31
 #define LIBAVCODEC_VERSION_MAJOR 55
32
-#define LIBAVCODEC_VERSION_MINOR  48
33
-#define LIBAVCODEC_VERSION_MICRO 102
32
+#define LIBAVCODEC_VERSION_MINOR  49
33
+#define LIBAVCODEC_VERSION_MICRO 100
34 34
 
35 35
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
36 36
                                                LIBAVCODEC_VERSION_MINOR, \