cinepakenc.c 47.1 KB
Newer Older
Tomas Härdin's avatar
Tomas Härdin committed
1
/*
2
 * Cinepak encoder (c) 2011 Tomas Härdin
Tomas Härdin's avatar
Tomas Härdin committed
3
 * http://titan.codemill.se/~tomhar/cinepakenc.patch
4 5 6
 *
 * Fixes and improvements, vintage decoders compatibility
 *  (c) 2013, 2014 Rl, Aetey Global Technologies AB
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
Tomas Härdin's avatar
Tomas Härdin committed
26

27
/*
28 29
 * TODO:
 * - optimize: color space conversion (move conversion to libswscale), ...
30 31 32 33 34
 * MAYBE:
 * - "optimally" split the frame into several non-regular areas
 *   using a separate codebook pair for each area and approximating
 *   the area by several rectangular strips (generally not full width ones)
 *   (use quadtree splitting? a simple fixed-granularity grid?)
Tomas Härdin's avatar
Tomas Härdin committed
35 36
 */

37 38
#include <string.h>

39
#include "libavutil/avassert.h"
Tomas Härdin's avatar
Tomas Härdin committed
40
#include "libavutil/common.h"
41
#include "libavutil/internal.h"
Tomas Härdin's avatar
Tomas Härdin committed
42 43
#include "libavutil/intreadwrite.h"
#include "libavutil/lfg.h"
44 45
#include "libavutil/opt.h"

Tomas Härdin's avatar
Tomas Härdin committed
46
#include "avcodec.h"
Tomas Härdin's avatar
Tomas Härdin committed
47
#include "elbg.h"
48 49
#include "internal.h"

Tomas Härdin's avatar
Tomas Härdin committed
50 51 52 53 54
#define CVID_HEADER_SIZE 10
#define STRIP_HEADER_SIZE 12
#define CHUNK_HEADER_SIZE 4

#define MB_SIZE 4           //4x4 MBs
55
#define MB_AREA (MB_SIZE * MB_SIZE)
Tomas Härdin's avatar
Tomas Härdin committed
56

57 58
#define VECTOR_MAX     6    // six or four entries per vector depending on format
#define CODEBOOK_MAX 256    // size of a codebook
Tomas Härdin's avatar
Tomas Härdin committed
59

60 61
#define MAX_STRIPS  32      // Note: having fewer choices regarding the number of strips speeds up encoding (obviously)
#define MIN_STRIPS   1      // Note: having more strips speeds up encoding the frame (this is less obvious)
62
// MAX_STRIPS limits the maximum quality you can reach
63
//            when you want high quality on high resolutions,
64 65 66 67 68 69
// MIN_STRIPS limits the minimum efficiently encodable bit rate
//            on low resolutions
// the numbers are only used for brute force optimization for the first frame,
// for the following frames they are adaptively readjusted
// NOTE the decoder in ffmpeg has its own arbitrary limitation on the number
// of strips, currently 32
Tomas Härdin's avatar
Tomas Härdin committed
70

71
typedef enum CinepakMode {
Tomas Härdin's avatar
Tomas Härdin committed
72 73 74 75 76 77 78
    MODE_V1_ONLY = 0,
    MODE_V1_V4,
    MODE_MC,

    MODE_COUNT,
} CinepakMode;

79
typedef enum mb_encoding {
Tomas Härdin's avatar
Tomas Härdin committed
80 81
    ENC_V1,
    ENC_V4,
82 83 84
    ENC_SKIP,

    ENC_UNCERTAIN
Tomas Härdin's avatar
Tomas Härdin committed
85 86
} mb_encoding;

87
typedef struct mb_info {
88 89 90 91 92 93
    int v1_vector;              // index into v1 codebook
    int v1_error;               // error when using V1 encoding
    int v4_vector[4];           // indices into v4 codebook
    int v4_error;               // error when using V4 encoding
    int skip_error;             // error when block is skipped (aka copied from last frame)
    mb_encoding best_encoding;  // last result from calculate_mode_score()
Tomas Härdin's avatar
Tomas Härdin committed
94 95
} mb_info;

96
typedef struct strip_info {
97 98
    int v1_codebook[CODEBOOK_MAX * VECTOR_MAX];
    int v4_codebook[CODEBOOK_MAX * VECTOR_MAX];
99 100 101
    int v1_size;
    int v4_size;
    CinepakMode mode;
Tomas Härdin's avatar
Tomas Härdin committed
102 103
} strip_info;

104
typedef struct CinepakEncContext {
105
    const AVClass *class;
Tomas Härdin's avatar
Tomas Härdin committed
106
    AVCodecContext *avctx;
107 108 109 110 111 112
    unsigned char *pict_bufs[4], *strip_buf, *frame_buf;
    AVFrame *last_frame;
    AVFrame *best_frame;
    AVFrame *scratch_frame;
    AVFrame *input_frame;
    enum AVPixelFormat pix_fmt;
Tomas Härdin's avatar
Tomas Härdin committed
113
    int w, h;
114
    int frame_buf_size;
Tomas Härdin's avatar
Tomas Härdin committed
115 116 117 118 119
    int curframe, keyint;
    AVLFG randctx;
    uint64_t lambda;
    int *codebook_input;
    int *codebook_closest;
120 121 122 123
    mb_info *mb;                // MB RD state
    int min_strips;             // the current limit
    int max_strips;             // the current limit
    // options
124 125 126 127 128
    int max_extra_cb_iterations;
    int skip_empty_cb;
    int min_min_strips;
    int max_max_strips;
    int strip_number_delta_range;
Tomas Härdin's avatar
Tomas Härdin committed
129 130
} CinepakEncContext;

131 132 133
#define OFFSET(x) offsetof(CinepakEncContext, x)
#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
static const AVOption options[] = {
134 135 136
    { "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",
James Almer's avatar
James Almer committed
137
      OFFSET(skip_empty_cb),            AV_OPT_TYPE_BOOL, { .i64 =         0 },          0, 1,                       VE },
138 139 140 141 142 143
    { "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 },
144 145 146 147 148 149 150 151 152 153
    { NULL },
};

static const AVClass cinepak_class = {
    .class_name = "cinepak",
    .item_name  = av_default_item_name,
    .option     = options,
    .version    = LIBAVUTIL_VERSION_INT,
};

Tomas Härdin's avatar
Tomas Härdin committed
154 155 156 157 158 159 160
static av_cold int cinepak_encode_init(AVCodecContext *avctx)
{
    CinepakEncContext *s = avctx->priv_data;
    int x, mb_count, strip_buf_size, frame_buf_size;

    if (avctx->width & 3 || avctx->height & 3) {
        av_log(avctx, AV_LOG_ERROR, "width and height must be multiples of four (got %ix%i)\n",
161
               avctx->width, avctx->height);
Tomas Härdin's avatar
Tomas Härdin committed
162 163 164
        return AVERROR(EINVAL);
    }

165
    if (s->min_min_strips > s->max_max_strips) {
166
        av_log(avctx, AV_LOG_ERROR, "minimum number of strips must not exceed maximum (got %i and %i)\n",
167
               s->min_min_strips, s->max_max_strips);
168 169 170
        return AVERROR(EINVAL);
    }

171
    if (!(s->last_frame = av_frame_alloc()))
Tomas Härdin's avatar
Tomas Härdin committed
172
        return AVERROR(ENOMEM);
173 174 175 176 177 178 179 180
    if (!(s->best_frame = av_frame_alloc()))
        goto enomem;
    if (!(s->scratch_frame = av_frame_alloc()))
        goto enomem;
    if (avctx->pix_fmt == AV_PIX_FMT_RGB24)
        if (!(s->input_frame = av_frame_alloc()))
            goto enomem;

181
    if (!(s->codebook_input = av_malloc_array((avctx->pix_fmt == AV_PIX_FMT_RGB24 ? 6 : 4) * (avctx->width * avctx->height) >> 2, sizeof(*s->codebook_input))))
182
        goto enomem;
Tomas Härdin's avatar
Tomas Härdin committed
183

184
    if (!(s->codebook_closest = av_malloc_array((avctx->width * avctx->height) >> 2, sizeof(*s->codebook_closest))))
Tomas Härdin's avatar
Tomas Härdin committed
185 186
        goto enomem;

187 188
    for (x = 0; x < (avctx->pix_fmt == AV_PIX_FMT_RGB24 ? 4 : 3); x++)
        if (!(s->pict_bufs[x] = av_malloc((avctx->pix_fmt == AV_PIX_FMT_RGB24 ? 6 : 4) * (avctx->width * avctx->height) >> 2)))
Tomas Härdin's avatar
Tomas Härdin committed
189 190 191 192
            goto enomem;

    mb_count = avctx->width * avctx->height / MB_AREA;

193 194
    // the largest possible chunk is 0x31 with all MBs encoded in V4 mode
    // and full codebooks being replaced in INTER mode,
195
    // which is 34 bits per MB
196 197
    // and 2*256 extra flag bits per strip
    strip_buf_size = STRIP_HEADER_SIZE + 3 * CHUNK_HEADER_SIZE + 2 * VECTOR_MAX * CODEBOOK_MAX + 4 * (mb_count + (mb_count + 15) / 16) + (2 * CODEBOOK_MAX) / 8;
Tomas Härdin's avatar
Tomas Härdin committed
198

199
    frame_buf_size = CVID_HEADER_SIZE + s->max_max_strips * strip_buf_size;
Tomas Härdin's avatar
Tomas Härdin committed
200 201 202 203 204 205 206

    if (!(s->strip_buf = av_malloc(strip_buf_size)))
        goto enomem;

    if (!(s->frame_buf = av_malloc(frame_buf_size)))
        goto enomem;

207
    if (!(s->mb = av_malloc_array(mb_count, sizeof(mb_info))))
Tomas Härdin's avatar
Tomas Härdin committed
208 209 210
        goto enomem;

    av_lfg_init(&s->randctx, 1);
211 212 213
    s->avctx          = avctx;
    s->w              = avctx->width;
    s->h              = avctx->height;
214
    s->frame_buf_size = frame_buf_size;
215 216 217
    s->curframe       = 0;
    s->keyint         = avctx->keyint_min;
    s->pix_fmt        = avctx->pix_fmt;
Tomas Härdin's avatar
Tomas Härdin committed
218

219
    // set up AVFrames
220 221 222 223 224 225 226 227
    s->last_frame->data[0]        = s->pict_bufs[0];
    s->last_frame->linesize[0]    = s->w;
    s->best_frame->data[0]        = s->pict_bufs[1];
    s->best_frame->linesize[0]    = s->w;
    s->scratch_frame->data[0]     = s->pict_bufs[2];
    s->scratch_frame->linesize[0] = s->w;

    if (s->pix_fmt == AV_PIX_FMT_RGB24) {
228 229 230 231
        s->last_frame->data[1]     = s->last_frame->data[0] +   s->w * s->h;
        s->last_frame->data[2]     = s->last_frame->data[1] + ((s->w * s->h) >> 2);
        s->last_frame->linesize[1] =
        s->last_frame->linesize[2] = s->w >> 1;
232

233 234 235 236
        s->best_frame->data[1]     = s->best_frame->data[0] +   s->w * s->h;
        s->best_frame->data[2]     = s->best_frame->data[1] + ((s->w * s->h) >> 2);
        s->best_frame->linesize[1] =
        s->best_frame->linesize[2] = s->w >> 1;
237

238
        s->scratch_frame->data[1]     = s->scratch_frame->data[0] +   s->w * s->h;
239
        s->scratch_frame->data[2]     = s->scratch_frame->data[1] + ((s->w * s->h) >> 2);
240 241 242 243 244 245 246 247 248
        s->scratch_frame->linesize[1] =
        s->scratch_frame->linesize[2] = s->w >> 1;

        s->input_frame->data[0]     = s->pict_bufs[3];
        s->input_frame->linesize[0] = s->w;
        s->input_frame->data[1]     = s->input_frame->data[0] +   s->w * s->h;
        s->input_frame->data[2]     = s->input_frame->data[1] + ((s->w * s->h) >> 2);
        s->input_frame->linesize[1] =
        s->input_frame->linesize[2] = s->w >> 1;
Tomas Härdin's avatar
Tomas Härdin committed
249 250
    }

251 252
    s->min_strips = s->min_min_strips;
    s->max_strips = s->max_max_strips;
253

Tomas Härdin's avatar
Tomas Härdin committed
254 255 256
    return 0;

enomem:
257 258 259 260 261 262 263 264 265 266
    av_frame_free(&s->last_frame);
    av_frame_free(&s->best_frame);
    av_frame_free(&s->scratch_frame);
    if (avctx->pix_fmt == AV_PIX_FMT_RGB24)
        av_frame_free(&s->input_frame);
    av_freep(&s->codebook_input);
    av_freep(&s->codebook_closest);
    av_freep(&s->strip_buf);
    av_freep(&s->frame_buf);
    av_freep(&s->mb);
Tomas Härdin's avatar
Tomas Härdin committed
267

268
    for (x = 0; x < (avctx->pix_fmt == AV_PIX_FMT_RGB24 ? 4 : 3); x++)
269
        av_freep(&s->pict_bufs[x]);
Tomas Härdin's avatar
Tomas Härdin committed
270 271 272 273

    return AVERROR(ENOMEM);
}

274 275 276 277
static int64_t calculate_mode_score(CinepakEncContext *s, int h,
                                    strip_info *info, int report,
                                    int *training_set_v1_shrunk,
                                    int *training_set_v4_shrunk)
Tomas Härdin's avatar
Tomas Härdin committed
278
{
279
    // score = FF_LAMBDA_SCALE * error + lambda * bits
Tomas Härdin's avatar
Tomas Härdin committed
280
    int x;
281
    int entry_size = s->pix_fmt == AV_PIX_FMT_RGB24 ? 6 : 4;
282
    int mb_count   = s->w * h / MB_AREA;
Tomas Härdin's avatar
Tomas Härdin committed
283 284
    mb_info *mb;
    int64_t score1, score2, score3;
285
    int64_t ret = s->lambda * ((info->v1_size ? CHUNK_HEADER_SIZE + info->v1_size * entry_size : 0) +
286 287
                               (info->v4_size ? CHUNK_HEADER_SIZE + info->v4_size * entry_size : 0) +
                               CHUNK_HEADER_SIZE) << 3;
288

289
    switch (info->mode) {
Tomas Härdin's avatar
Tomas Härdin committed
290
    case MODE_V1_ONLY:
291
        // one byte per MB
Tomas Härdin's avatar
Tomas Härdin committed
292 293
        ret += s->lambda * 8 * mb_count;

294 295 296
        // while calculating we assume all blocks are ENC_V1
        for (x = 0; x < mb_count; x++) {
            mb   = &s->mb[x];
Tomas Härdin's avatar
Tomas Härdin committed
297
            ret += FF_LAMBDA_SCALE * mb->v1_error;
298 299
            // this function is never called for report in MODE_V1_ONLY
            // if (!report)
Tomas Härdin's avatar
Tomas Härdin committed
300 301 302 303 304
            mb->best_encoding = ENC_V1;
        }

        break;
    case MODE_V1_V4:
305 306 307
        // 9 or 33 bits per MB
        if (report) {
            // no moves between the corresponding training sets are allowed
308
            *training_set_v1_shrunk = *training_set_v4_shrunk = 0;
309
            for (x = 0; x < mb_count; x++) {
310 311
                int mberr;
                mb = &s->mb[x];
312 313
                if (mb->best_encoding == ENC_V1)
                    score1 = s->lambda * 9 + FF_LAMBDA_SCALE * (mberr = mb->v1_error);
314
                else
315
                    score1 = s->lambda * 33 + FF_LAMBDA_SCALE * (mberr = mb->v4_error);
Tomas Härdin's avatar
Tomas Härdin committed
316
                ret += score1;
317 318
            }
        } else { // find best mode per block
319 320 321
            for (x = 0; x < mb_count; x++) {
                mb     = &s->mb[x];
                score1 = s->lambda * 9 + FF_LAMBDA_SCALE * mb->v1_error;
322 323
                score2 = s->lambda * 33 + FF_LAMBDA_SCALE * mb->v4_error;

324
                if (score1 <= score2) {
325 326 327 328 329 330
                    ret += score1;
                    mb->best_encoding = ENC_V1;
                } else {
                    ret += score2;
                    mb->best_encoding = ENC_V4;
                }
Tomas Härdin's avatar
Tomas Härdin committed
331 332 333 334 335
            }
        }

        break;
    case MODE_MC:
336 337
        // 1, 10 or 34 bits per MB
        if (report) {
338
            int v1_shrunk = 0, v4_shrunk = 0;
339
            for (x = 0; x < mb_count; x++) {
340
                mb = &s->mb[x];
341 342 343 344
                // it is OK to move blocks to ENC_SKIP here
                // but not to any codebook encoding!
                score1 = s->lambda * 1 + FF_LAMBDA_SCALE * mb->skip_error;
                if (mb->best_encoding == ENC_SKIP) {
345
                    ret += score1;
346 347
                } else if (mb->best_encoding == ENC_V1) {
                    if ((score2 = s->lambda * 10 + FF_LAMBDA_SCALE * mb->v1_error) >= score1) {
348 349 350 351 352 353 354
                        mb->best_encoding = ENC_SKIP;
                        ++v1_shrunk;
                        ret += score1;
                    } else {
                        ret += score2;
                    }
                } else {
355
                    if ((score3 = s->lambda * 34 + FF_LAMBDA_SCALE * mb->v4_error) >= score1) {
356 357 358 359 360 361 362 363 364 365 366
                        mb->best_encoding = ENC_SKIP;
                        ++v4_shrunk;
                        ret += score1;
                    } else {
                        ret += score3;
                    }
                }
            }
            *training_set_v1_shrunk = v1_shrunk;
            *training_set_v4_shrunk = v4_shrunk;
        } else { // find best mode per block
367 368 369
            for (x = 0; x < mb_count; x++) {
                mb     = &s->mb[x];
                score1 = s->lambda * 1 + FF_LAMBDA_SCALE * mb->skip_error;
370 371 372
                score2 = s->lambda * 10 + FF_LAMBDA_SCALE * mb->v1_error;
                score3 = s->lambda * 34 + FF_LAMBDA_SCALE * mb->v4_error;

373
                if (score1 <= score2 && score1 <= score3) {
374 375
                    ret += score1;
                    mb->best_encoding = ENC_SKIP;
376
                } else if (score2 <= score3) {
377 378 379 380 381 382
                    ret += score2;
                    mb->best_encoding = ENC_V1;
                } else {
                    ret += score3;
                    mb->best_encoding = ENC_V4;
                }
Tomas Härdin's avatar
Tomas Härdin committed
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
            }
        }

        break;
    }

    return ret;
}

static int write_chunk_header(unsigned char *buf, int chunk_type, int chunk_size)
{
    buf[0] = chunk_type;
    AV_WB24(&buf[1], chunk_size + CHUNK_HEADER_SIZE);
    return CHUNK_HEADER_SIZE;
}

399 400 401
static int encode_codebook(CinepakEncContext *s, int *codebook, int size,
                           int chunk_type_yuv, int chunk_type_gray,
                           unsigned char *buf)
Tomas Härdin's avatar
Tomas Härdin committed
402
{
403 404
    int x, y, ret, entry_size = s->pix_fmt == AV_PIX_FMT_RGB24 ? 6 : 4;
    int incremental_codebook_replacement_mode = 0; // hardcoded here,
405
    // the compiler should notice that this is a constant -- rl
406 407

    ret = write_chunk_header(buf,
408 409 410 411 412 413 414 415 416 417 418 419
                             s->pix_fmt == AV_PIX_FMT_RGB24 ?
                             chunk_type_yuv  + (incremental_codebook_replacement_mode ? 1 : 0) :
                             chunk_type_gray + (incremental_codebook_replacement_mode ? 1 : 0),
                             entry_size * size +
                             (incremental_codebook_replacement_mode ? (size + 31) / 32 * 4 : 0));

    // we do codebook encoding according to the "intra" mode
    // but we keep the "dead" code for reference in case we will want
    // to use incremental codebook updates (which actually would give us
    // "kind of" motion compensation, especially in 1 strip/frame case) -- rl
    // (of course, the code will be not useful as-is)
    if (incremental_codebook_replacement_mode) {
420 421
        int flags = 0;
        int flagsind;
422 423
        for (x = 0; x < size; x++) {
            if (flags == 0) {
424
                flagsind = ret;
425 426
                ret     += 4;
                flags    = 0x80000000;
427
            } else
428 429 430 431
                flags = ((flags >> 1) | 0x80000000);
            for (y = 0; y < entry_size; y++)
                buf[ret++] = codebook[y + x * entry_size] ^ (y >= 4 ? 0x80 : 0);
            if ((flags & 0xffffffff) == 0xffffffff) {
432 433 434 435
                AV_WB32(&buf[flagsind], flags);
                flags = 0;
            }
        }
436
        if (flags)
437 438
            AV_WB32(&buf[flagsind], flags);
    } else
439 440 441
        for (x = 0; x < size; x++)
            for (y = 0; y < entry_size; y++)
                buf[ret++] = codebook[y + x * entry_size] ^ (y >= 4 ? 0x80 : 0);
Tomas Härdin's avatar
Tomas Härdin committed
442 443 444 445

    return ret;
}

446
// sets out to the sub picture starting at (x,y) in in
447 448 449
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])
Tomas Härdin's avatar
Tomas Härdin committed
450
{
451
    out_data[0]     = in_data[0] + x + y * in_linesize[0];
452
    out_linesize[0] = in_linesize[0];
Tomas Härdin's avatar
Tomas Härdin committed
453

454 455
    if (s->pix_fmt == AV_PIX_FMT_RGB24) {
        out_data[1]     = in_data[1] + (x >> 1) + (y >> 1) * in_linesize[1];
456
        out_linesize[1] = in_linesize[1];
Tomas Härdin's avatar
Tomas Härdin committed
457

458
        out_data[2]     = in_data[2] + (x >> 1) + (y >> 1) * in_linesize[2];
459
        out_linesize[2] = in_linesize[2];
Tomas Härdin's avatar
Tomas Härdin committed
460 461 462
    }
}

463
// decodes the V1 vector in mb into the 4x4 MB pointed to by data
464 465
static void decode_v1_vector(CinepakEncContext *s, uint8_t *data[4],
                             int linesize[4], int v1_vector, strip_info *info)
Tomas Härdin's avatar
Tomas Härdin committed
466
{
467
    int entry_size = s->pix_fmt == AV_PIX_FMT_RGB24 ? 6 : 4;
Tomas Härdin's avatar
Tomas Härdin committed
468

469
    data[0][0] =
470 471 472
    data[0][1] =
    data[0][    linesize[0]] =
    data[0][1 + linesize[0]] = info->v1_codebook[v1_vector * entry_size];
Tomas Härdin's avatar
Tomas Härdin committed
473

474
    data[0][2] =
475 476 477
    data[0][3] =
    data[0][2 + linesize[0]] =
    data[0][3 + linesize[0]] = info->v1_codebook[v1_vector * entry_size + 1];
Tomas Härdin's avatar
Tomas Härdin committed
478

479 480 481 482
    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];
Tomas Härdin's avatar
Tomas Härdin committed
483

484 485 486 487
    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];
Tomas Härdin's avatar
Tomas Härdin committed
488

489
    if (s->pix_fmt == AV_PIX_FMT_RGB24) {
490
        data[1][0] =
491 492 493
        data[1][1] =
        data[1][    linesize[1]] =
        data[1][1 + linesize[1]] = info->v1_codebook[v1_vector * entry_size + 4];
494 495

        data[2][0] =
496 497 498
        data[2][1] =
        data[2][    linesize[2]] =
        data[2][1 + linesize[2]] = info->v1_codebook[v1_vector * entry_size + 5];
Tomas Härdin's avatar
Tomas Härdin committed
499 500 501
    }
}

502
// decodes the V4 vectors in mb into the 4x4 MB pointed to by data
503 504
static void decode_v4_vector(CinepakEncContext *s, uint8_t *data[4],
                             int linesize[4], int *v4_vector, strip_info *info)
Tomas Härdin's avatar
Tomas Härdin committed
505
{
506
    int i, x, y, entry_size = s->pix_fmt == AV_PIX_FMT_RGB24 ? 6 : 4;
Tomas Härdin's avatar
Tomas Härdin committed
507

508 509 510 511 512 513
    for (i = y = 0; y < 4; y += 2) {
        for (x = 0; x < 4; x += 2, i++) {
            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];
Tomas Härdin's avatar
Tomas Härdin committed
514

515 516 517
            if (s->pix_fmt == AV_PIX_FMT_RGB24) {
                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];
Tomas Härdin's avatar
Tomas Härdin committed
518 519 520 521 522
            }
        }
    }
}

523 524 525
static void copy_mb(CinepakEncContext *s,
                    uint8_t *a_data[4], int a_linesize[4],
                    uint8_t *b_data[4], int b_linesize[4])
526 527 528
{
    int y, p;

529 530
    for (y = 0; y < MB_SIZE; y++)
        memcpy(a_data[0] + y * a_linesize[0], b_data[0] + y * b_linesize[0],
531 532
               MB_SIZE);

533 534 535 536 537 538
    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],
                       MB_SIZE / 2);
539 540 541
    }
}

542 543 544 545
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)
Tomas Härdin's avatar
Tomas Härdin committed
546 547 548
{
    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;
549
    unsigned char temp[64]; // 32/2 = 16 V4 blocks at 4 B each -> 64 B
Tomas Härdin's avatar
Tomas Härdin committed
550
    mb_info *mb;
551 552 553 554 555 556 557 558
    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
    ////// the codebook chunk even when the codebook is empty - pretty dumb...
    ////// and also the certain order of the codebook chunks -- rl
    if (info->v4_size || !s->skip_empty_cb)
559
        ret += encode_codebook(s, info->v4_codebook, info->v4_size, 0x20, 0x24, buf + ret);
Tomas Härdin's avatar
Tomas Härdin committed
560

561
    if (info->v1_size || !s->skip_empty_cb)
562
        ret += encode_codebook(s, info->v1_codebook, info->v1_size, 0x22, 0x26, buf + ret);
Tomas Härdin's avatar
Tomas Härdin committed
563

564 565 566
    // update scratch picture
    for (z = y = 0; y < h; y += MB_SIZE)
        for (x = 0; x < s->w; x += MB_SIZE, z++) {
Tomas Härdin's avatar
Tomas Härdin committed
567 568
            mb = &s->mb[z];

569 570
            get_sub_picture(s, x, y, scratch_data, scratch_linesize,
                            sub_scratch_data, sub_scratch_linesize);
Tomas Härdin's avatar
Tomas Härdin committed
571

572 573
            if (info->mode == MODE_MC && mb->best_encoding == ENC_SKIP) {
                get_sub_picture(s, x, y, last_data, last_linesize,
574 575 576
                                sub_last_data, sub_last_linesize);
                copy_mb(s, sub_scratch_data, sub_scratch_linesize,
                        sub_last_data, sub_last_linesize);
577
            } else if (info->mode == MODE_V1_ONLY || mb->best_encoding == ENC_V1)
578 579
                decode_v1_vector(s, sub_scratch_data, sub_scratch_linesize,
                                 mb->v1_vector, info);
580
            else
581 582
                decode_v4_vector(s, sub_scratch_data, sub_scratch_linesize,
                                 mb->v4_vector, info);
Tomas Härdin's avatar
Tomas Härdin committed
583 584
        }

585
    switch (info->mode) {
Tomas Härdin's avatar
Tomas Härdin committed
586 587 588
    case MODE_V1_ONLY:
        ret += write_chunk_header(buf + ret, 0x32, mb_count);

589
        for (x = 0; x < mb_count; x++)
Tomas Härdin's avatar
Tomas Härdin committed
590 591 592 593
            buf[ret++] = s->mb[x].v1_vector;

        break;
    case MODE_V1_V4:
594
        // remember header position
Tomas Härdin's avatar
Tomas Härdin committed
595
        header_ofs = ret;
596
        ret       += CHUNK_HEADER_SIZE;
Tomas Härdin's avatar
Tomas Härdin committed
597

598
        for (x = 0; x < mb_count; x += 32) {
Tomas Härdin's avatar
Tomas Härdin committed
599
            flags = 0;
600 601
            for (y = x; y < FFMIN(x + 32, mb_count); y++)
                if (s->mb[y].best_encoding == ENC_V4)
Tomas Härdin's avatar
Tomas Härdin committed
602 603 604 605 606
                    flags |= 1 << (31 - y + x);

            AV_WB32(&buf[ret], flags);
            ret += 4;

607
            for (y = x; y < FFMIN(x + 32, mb_count); y++) {
Tomas Härdin's avatar
Tomas Härdin committed
608 609
                mb = &s->mb[y];

610
                if (mb->best_encoding == ENC_V1)
Tomas Härdin's avatar
Tomas Härdin committed
611 612
                    buf[ret++] = mb->v1_vector;
                else
613
                    for (z = 0; z < 4; z++)
614
                        buf[ret++] = mb->v4_vector[z];
Tomas Härdin's avatar
Tomas Härdin committed
615 616 617 618 619 620 621
            }
        }

        write_chunk_header(buf + header_ofs, 0x30, ret - header_ofs - CHUNK_HEADER_SIZE);

        break;
    case MODE_MC:
622
        // remember header position
Tomas Härdin's avatar
Tomas Härdin committed
623
        header_ofs = ret;
624 625
        ret       += CHUNK_HEADER_SIZE;
        flags      = bits = temp_size = 0;
Tomas Härdin's avatar
Tomas Härdin committed
626

627 628 629 630
        for (x = 0; x < mb_count; x++) {
            mb                = &s->mb[x];
            flags            |= (mb->best_encoding != ENC_SKIP) << (31 - bits++);
            needs_extra_bit   = 0;
Tomas Härdin's avatar
Tomas Härdin committed
631 632
            should_write_temp = 0;

633 634
            if (mb->best_encoding != ENC_SKIP) {
                if (bits < 32)
Tomas Härdin's avatar
Tomas Härdin committed
635 636 637 638 639
                    flags |= (mb->best_encoding == ENC_V4) << (31 - bits++);
                else
                    needs_extra_bit = 1;
            }

640
            if (bits == 32) {
Tomas Härdin's avatar
Tomas Härdin committed
641
                AV_WB32(&buf[ret], flags);
642
                ret  += 4;
Tomas Härdin's avatar
Tomas Härdin committed
643 644
                flags = bits = 0;

645
                if (mb->best_encoding == ENC_SKIP || needs_extra_bit) {
Tomas Härdin's avatar
Tomas Härdin committed
646
                    memcpy(&buf[ret], temp, temp_size);
647
                    ret      += temp_size;
Tomas Härdin's avatar
Tomas Härdin committed
648 649 650 651 652
                    temp_size = 0;
                } else
                    should_write_temp = 1;
            }

653
            if (needs_extra_bit) {
Tomas Härdin's avatar
Tomas Härdin committed
654
                flags = (mb->best_encoding == ENC_V4) << 31;
655
                bits  = 1;
Tomas Härdin's avatar
Tomas Härdin committed
656 657
            }

658
            if (mb->best_encoding == ENC_V1)
Tomas Härdin's avatar
Tomas Härdin committed
659
                temp[temp_size++] = mb->v1_vector;
660 661
            else if (mb->best_encoding == ENC_V4)
                for (z = 0; z < 4; z++)
662
                    temp[temp_size++] = mb->v4_vector[z];
Tomas Härdin's avatar
Tomas Härdin committed
663

664
            if (should_write_temp) {
Tomas Härdin's avatar
Tomas Härdin committed
665
                memcpy(&buf[ret], temp, temp_size);
666
                ret      += temp_size;
Tomas Härdin's avatar
Tomas Härdin committed
667 668 669 670
                temp_size = 0;
            }
        }

671
        if (bits > 0) {
Tomas Härdin's avatar
Tomas Härdin committed
672 673 674 675 676 677 678 679 680 681 682 683 684 685
            AV_WB32(&buf[ret], flags);
            ret += 4;
            memcpy(&buf[ret], temp, temp_size);
            ret += temp_size;
        }

        write_chunk_header(buf + header_ofs, 0x31, ret - header_ofs - CHUNK_HEADER_SIZE);

        break;
    }

    return ret;
}

686
// computes distortion of 4x4 MB in b compared to a
687 688 689
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])
Tomas Härdin's avatar
Tomas Härdin committed
690 691 692
{
    int x, y, p, d, ret = 0;

693 694 695 696
    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]];
            ret += d * d;
Tomas Härdin's avatar
Tomas Härdin committed
697 698
        }

699 700 701 702 703 704
    if (s->pix_fmt == AV_PIX_FMT_RGB24) {
        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]];
                    ret += d * d;
Tomas Härdin's avatar
Tomas Härdin committed
705 706 707 708 709 710 711
                }
        }
    }

    return ret;
}

712
// return the possibly adjusted size of the codebook
713 714 715
#define CERTAIN(x) ((x) != ENC_UNCERTAIN)
static int quantize(CinepakEncContext *s, int h, uint8_t *data[4],
                    int linesize[4], int v1mode, strip_info *info,
716
                    mb_encoding encoding)
Tomas Härdin's avatar
Tomas Härdin committed
717
{
718
    int x, y, i, j, k, x2, y2, x3, y3, plane, shift, mbn;
719 720 721
    int entry_size      = s->pix_fmt == AV_PIX_FMT_RGB24 ? 6 : 4;
    int *codebook       = v1mode ? info->v1_codebook : info->v4_codebook;
    int size            = v1mode ? info->v1_size : info->v4_size;
Tomas Härdin's avatar
Tomas Härdin committed
722
    int64_t total_error = 0;
723 724
    uint8_t vq_pict_buf[(MB_AREA * 3) / 2];
    uint8_t     *sub_data[4],     *vq_data[4];
725
    int      sub_linesize[4],  vq_linesize[4];
Tomas Härdin's avatar
Tomas Härdin committed
726

727 728
    for (mbn = i = y = 0; y < h; y += MB_SIZE) {
        for (x = 0; x < s->w; x += MB_SIZE, ++mbn) {
729
            int *base;
Tomas Härdin's avatar
Tomas Härdin committed
730

731 732 733 734
            if (CERTAIN(encoding)) {
                // use for the training only the blocks known to be to be encoded [sic:-]
                if (s->mb[mbn].best_encoding != encoding)
                    continue;
735 736
            }

737 738 739 740 741 742 743 744 745 746 747 748 749
            base = s->codebook_input + i * entry_size;
            if (v1mode) {
                // subsample
                for (j = y2 = 0; y2 < entry_size; y2 += 2)
                    for (x2 = 0; x2 < 4; x2 += 2, j++) {
                        plane   = y2 < 4 ? 0 : 1 + (x2 >> 1);
                        shift   = y2 < 4 ? 0 : 1;
                        x3      = shift ? 0 : x2;
                        y3      = shift ? 0 : y2;
                        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;
Tomas Härdin's avatar
Tomas Härdin committed
750 751
                    }
            } else {
752 753 754 755
                // copy
                for (j = y2 = 0; y2 < MB_SIZE; y2 += 2) {
                    for (x2 = 0; x2 < MB_SIZE; x2 += 2)
                        for (k = 0; k < entry_size; k++, j++) {
Tomas Härdin's avatar
Tomas Härdin committed
756 757
                            plane = k >= 4 ? k - 3 : 0;

758 759 760
                            if (k >= 4) {
                                x3 = (x + x2) >> 1;
                                y3 = (y + y2) >> 1;
Tomas Härdin's avatar
Tomas Härdin committed
761 762 763 764 765
                            } else {
                                x3 = x + x2 + (k & 1);
                                y3 = y + y2 + (k >> 1);
                            }

766
                            base[j] = data[plane][x3 + y3 * linesize[plane]];
Tomas Härdin's avatar
Tomas Härdin committed
767 768 769
                        }
                }
            }
770
            i += v1mode ? 1 : 4;
Tomas Härdin's avatar
Tomas Härdin committed
771 772
        }
    }
773

774
    if (i == 0) // empty training set, nothing to do
775
        return 0;
776
    if (i < size)
777
        size = i;
Tomas Härdin's avatar
Tomas Härdin committed
778

779 780
    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);
Tomas Härdin's avatar
Tomas Härdin committed
781

782 783
    // set up vq_data, which contains a single MB
    vq_data[0]     = vq_pict_buf;
784
    vq_linesize[0] = MB_SIZE;
785 786 787 788 789 790 791 792
    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)
        for (x = 0; x < s->w; x += MB_SIZE, j++) {
Tomas Härdin's avatar
Tomas Härdin committed
793
            mb_info *mb = &s->mb[j];
794 795
            // skip uninteresting blocks if we know their preferred encoding
            if (CERTAIN(encoding) && mb->best_encoding != encoding)
796
                continue;
Tomas Härdin's avatar
Tomas Härdin committed
797

798
            // point sub_data to current MB
799
            get_sub_picture(s, x, y, data, linesize, sub_data, sub_linesize);
Tomas Härdin's avatar
Tomas Härdin committed
800

801
            if (v1mode) {
Tomas Härdin's avatar
Tomas Härdin committed
802 803
                mb->v1_vector = s->codebook_closest[i];

804
                // fill in vq_data with V1 data
805
                decode_v1_vector(s, vq_data, vq_linesize, mb->v1_vector, info);
Tomas Härdin's avatar
Tomas Härdin committed
806

807 808
                mb->v1_error = compute_mb_distortion(s, sub_data, sub_linesize,
                                                     vq_data, vq_linesize);
Tomas Härdin's avatar
Tomas Härdin committed
809 810
                total_error += mb->v1_error;
            } else {
811 812
                for (k = 0; k < 4; k++)
                    mb->v4_vector[k] = s->codebook_closest[i + k];
Tomas Härdin's avatar
Tomas Härdin committed
813

814
                // fill in vq_data with V4 data
815
                decode_v4_vector(s, vq_data, vq_linesize, mb->v4_vector, info);
Tomas Härdin's avatar
Tomas Härdin committed
816

817 818
                mb->v4_error = compute_mb_distortion(s, sub_data, sub_linesize,
                                                     vq_data, vq_linesize);
819
                total_error += mb->v4_error;
Tomas Härdin's avatar
Tomas Härdin committed
820
            }
821
            i += v1mode ? 1 : 4;
Tomas Härdin's avatar
Tomas Härdin committed
822
        }
823
    // check that we did it right in the beginning of the function
824
    av_assert0(i >= size); // training set is no smaller than the codebook
Tomas Härdin's avatar
Tomas Härdin committed
825

826
    return size;
Tomas Härdin's avatar
Tomas Härdin committed
827 828
}

829 830 831 832
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)
Tomas Härdin's avatar
Tomas Härdin committed
833 834
{
    int x, y, i;
835 836
    uint8_t *sub_last_data    [4], *sub_pict_data    [4];
    int      sub_last_linesize[4],  sub_pict_linesize[4];
Tomas Härdin's avatar
Tomas Härdin committed
837

838 839 840 841 842 843 844 845 846 847 848
    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_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);
Tomas Härdin's avatar
Tomas Härdin committed
849 850 851
        }
}

852 853
static void write_strip_header(CinepakEncContext *s, int y, int h, int keyframe,
                               unsigned char *buf, int strip_size)
Tomas Härdin's avatar
Tomas Härdin committed
854
{
855 856 857 858 859 860
    // actually we are exclusively using intra strip coding (how much can we win
    // otherwise? how to choose which part of a codebook to update?),
    // keyframes are different only because we disallow ENC_SKIP on them -- rl
    // (besides, the logic here used to be inverted: )
    //    buf[0] = keyframe ? 0x11: 0x10;
    buf[0] = keyframe ? 0x10 : 0x11;
Tomas Härdin's avatar
Tomas Härdin committed
861
    AV_WB24(&buf[1], strip_size + STRIP_HEADER_SIZE);
862
    // AV_WB16(&buf[4], y); /* using absolute y values works -- rl */
863
    AV_WB16(&buf[4], 0); /* using relative values works as well -- rl */
Tomas Härdin's avatar
Tomas Härdin committed
864
    AV_WB16(&buf[6], 0);
865
    // AV_WB16(&buf[8], y + h); /* using absolute y values works -- rl */
866
    AV_WB16(&buf[8], h); /* using relative values works as well -- rl */
Tomas Härdin's avatar
Tomas Härdin committed
867 868 869
    AV_WB16(&buf[10], s->w);
}

870 871 872 873
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],
874
                    unsigned char *buf, int64_t *best_score)
Tomas Härdin's avatar
Tomas Härdin committed
875 876
{
    int64_t score = 0;
877
    int best_size = 0;
Tomas Härdin's avatar
Tomas Härdin committed
878
    strip_info info;
879
    // for codebook optimization:
880 881 882
    int v1enough, v1_size, v4enough, v4_size;
    int new_v1_size, new_v4_size;
    int v1shrunk, v4shrunk;
Tomas Härdin's avatar
Tomas Härdin committed
883

884
    if (!keyframe)
885 886
        calculate_skip_errors(s, h, last_data, last_linesize, data, linesize,
                              &info);
Tomas Härdin's avatar
Tomas Härdin committed
887

888 889 890 891
    // try some powers of 4 for the size of the codebooks
    // constraint the v4 codebook to be no bigger than v1 one,
    // (and no less than v1_size/4)
    // thus making v1 preferable and possibly losing small details? should be ok
892
#define SMALLEST_CODEBOOK 1
893 894
    for (v1enough = 0, v1_size = SMALLEST_CODEBOOK; v1_size <= CODEBOOK_MAX && !v1enough; v1_size <<= 2) {
        for (v4enough = 0, v4_size = 0; v4_size <= v1_size && !v4enough; v4_size = v4_size ? v4_size << 2 : v1_size >= SMALLEST_CODEBOOK << 2 ? v1_size >> 2 : SMALLEST_CODEBOOK) {
895
            CinepakMode mode;
896
            // try all modes
James Almer's avatar
James Almer committed
897
            for (mode = 0; mode < MODE_COUNT; mode++) {
898 899
                // don't allow MODE_MC in intra frames
                if (keyframe && mode == MODE_MC)
Tomas Härdin's avatar
Tomas Härdin committed
900 901
                    continue;

902
                if (mode == MODE_V1_ONLY) {
903
                    info.v1_size = v1_size;
904
                    // the size may shrink even before optimizations if the input is short:
905 906
                    info.v1_size = quantize(s, h, data, linesize, 1,
                                            &info, ENC_UNCERTAIN);
907 908
                    if (info.v1_size < v1_size)
                        // too few eligible blocks, no sense in trying bigger sizes
909 910 911 912 913
                        v1enough = 1;

                    info.v4_size = 0;
                } else { // mode != MODE_V1_ONLY
                    // if v4 codebook is empty then only allow V1-only mode
914
                    if (!v4_size)
915 916
                        continue;

917
                    if (mode == MODE_V1_V4) {
918
                        info.v4_size = v4_size;
919 920
                        info.v4_size = quantize(s, h, data, linesize, 0,
                                                &info, ENC_UNCERTAIN);
921 922
                        if (info.v4_size < v4_size)
                            // too few eligible blocks, no sense in trying bigger sizes
923 924 925
                            v4enough = 1;
                    }
                }
Tomas Härdin's avatar
Tomas Härdin committed
926

927
                info.mode = mode;
928
                // choose the best encoding per block, based on current experience
929
                score = calculate_mode_score(s, h, &info, 0,
930
                                             &v1shrunk, &v4shrunk);
931

932
                if (mode != MODE_V1_ONLY) {
933
                    int extra_iterations_limit = s->max_extra_cb_iterations;
934 935
                    // recompute the codebooks, omitting the extra blocks
                    // we assume we _may_ come here with more blocks to encode than before
936
                    info.v1_size = v1_size;
937
                    new_v1_size = quantize(s, h, data, linesize, 1, &info, ENC_V1);
938
                    if (new_v1_size < info.v1_size)
939
                        info.v1_size = new_v1_size;
940
                    // we assume we _may_ come here with more blocks to encode than before
941
                    info.v4_size = v4_size;
942
                    new_v4_size = quantize(s, h, data, linesize, 0, &info, ENC_V4);
943
                    if (new_v4_size < info.v4_size)
944
                        info.v4_size = new_v4_size;
945 946 947 948 949
                    // calculate the resulting score
                    // (do not move blocks to codebook encodings now, as some blocks may have
                    // got bigger errors despite a smaller training set - but we do not
                    // ever grow the training sets back)
                    for (;;) {
950
                        score = calculate_mode_score(s, h, &info, 1,
951
                                                     &v1shrunk, &v4shrunk);
952 953 954 955 956
                        // do we have a reason to reiterate? if so, have we reached the limit?
                        if ((!v1shrunk && !v4shrunk) || !extra_iterations_limit--)
                            break;
                        // recompute the codebooks, omitting the extra blocks
                        if (v1shrunk) {
957
                            info.v1_size = v1_size;
958
                            new_v1_size = quantize(s, h, data, linesize, 1, &info, ENC_V1);
959
                            if (new_v1_size < info.v1_size)
960 961
                                info.v1_size = new_v1_size;
                        }
962
                        if (v4shrunk) {
963
                            info.v4_size = v4_size;
964
                            new_v4_size = quantize(s, h, data, linesize, 0, &info, ENC_V4);
965
                            if (new_v4_size < info.v4_size)
966 967 968 969
                                info.v4_size = new_v4_size;
                        }
                    }
                }
Tomas Härdin's avatar
Tomas Härdin committed
970

971
                if (best_size == 0 || score < *best_score) {
Tomas Härdin's avatar
Tomas Härdin committed
972
                    *best_score = score;
973 974 975 976
                    best_size = encode_mode(s, h,
                                            scratch_data, scratch_linesize,
                                            last_data, last_linesize, &info,
                                            s->strip_buf + STRIP_HEADER_SIZE);
Tomas Härdin's avatar
Tomas Härdin committed
977 978 979 980 981 982 983 984 985 986 987 988 989

                    write_strip_header(s, y, h, keyframe, s->strip_buf, best_size);
                }
            }
        }
    }

    best_size += STRIP_HEADER_SIZE;
    memcpy(buf, s->strip_buf, best_size);

    return best_size;
}

990 991
static int write_cvid_header(CinepakEncContext *s, unsigned char *buf,
                             int num_strips, int data_size, int isakeyframe)
Tomas Härdin's avatar
Tomas Härdin committed
992
{
993
    buf[0] = isakeyframe ? 0 : 1;
Tomas Härdin's avatar
Tomas Härdin committed
994 995 996 997 998 999 1000 1001
    AV_WB24(&buf[1], data_size + CVID_HEADER_SIZE);
    AV_WB16(&buf[4], s->w);
    AV_WB16(&buf[6], s->h);
    AV_WB16(&buf[8], num_strips);

    return CVID_HEADER_SIZE;
}

1002 1003
static int rd_frame(CinepakEncContext *s, const AVFrame *frame,
                    int isakeyframe, unsigned char *buf, int buf_size)
Tomas Härdin's avatar
Tomas Härdin committed
1004
{
1005
    int num_strips, strip, i, y, nexty, size, temp_size, best_size;
1006 1007
    uint8_t *last_data    [4], *data    [4], *scratch_data    [4];
    int      last_linesize[4],  linesize[4],  scratch_linesize[4];
Tomas Härdin's avatar
Tomas Härdin committed
1008
    int64_t best_score = 0, score, score_temp;
1009
    int best_nstrips;
1010

1011
    if (s->pix_fmt == AV_PIX_FMT_RGB24) {
1012
        int x;
1013 1014 1015 1016 1017 1018
        // build a copy of the given frame in the correct colorspace
        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] = frame->data[0] + x * 3 + y * frame->linesize[0];
1019 1020 1021 1022
                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);
1023
                r = g = b = 0;
1024
                for (i = 0; i < 4; ++i) {
1025
                    int i1, i2;
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
                    i1 = (i & 1);
                    i2 = (i >= 2);
                    rr = ir[i2][i1 * 3 + 0];
                    gg = ir[i2][i1 * 3 + 1];
                    bb = ir[i2][i1 * 3 + 2];
                    r += rr;
                    g += gg;
                    b += bb;
                    // using fixed point arithmetic for portable repeatability, scaling by 2^23
                    // "Y"
                    // rr = 0.2857 * rr + 0.5714 * gg + 0.1429 * bb;
                    rr = (2396625 * rr + 4793251 * gg + 1198732 * bb) >> 23;
                    if (rr < 0)
                        rr = 0;
                    else if (rr > 255)
                        rr = 255;
                    scratch_data[0][i1 + i2 * scratch_linesize[0]] = rr;
1043
                }
1044 1045 1046 1047 1048 1049 1050 1051 1052
                // let us scale down as late as possible
                //                r /= 4; g /= 4; b /= 4;
                // "U"
                // rr = -0.1429 * r - 0.2857 * g + 0.4286 * b;
                rr = (-299683 * r - 599156 * g + 898839 * b) >> 23;
                if (rr < -128)
                    rr = -128;
                else if (rr > 127)
                    rr = 127;
1053
                scratch_data[1][0] = rr + 128; // quantize needs unsigned
1054 1055 1056 1057 1058 1059 1060
                // "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;
1061
                scratch_data[2][0] = rr + 128; // quantize needs unsigned
1062 1063 1064
            }
    }

1065
    // would be nice but quite certainly incompatible with vintage players:
1066
    // support encoding zero strips (meaning skip the whole frame)
1067
    for (num_strips = s->min_strips; num_strips <= s->max_strips && num_strips <= s->h / MB_SIZE; num_strips++) {
Tomas Härdin's avatar
Tomas Härdin committed
1068
        score = 0;
1069
        size  = 0;
1070

1071
        for (y = 0, strip = 1; y < s->h; strip++, y = nexty) {
1072
            int strip_height;
Tomas Härdin's avatar
Tomas Härdin committed
1073

1074
            nexty = strip * s->h / num_strips; // <= s->h
1075 1076
            // make nexty the next multiple of 4 if not already there
            if (nexty & 3)
1077
                nexty += 4 - (nexty & 3);
Tomas Härdin's avatar
Tomas Härdin committed
1078

1079
            strip_height = nexty - y;
1080
            if (strip_height <= 0) { // can this ever happen?
1081 1082 1083 1084
                av_log(s->avctx, AV_LOG_INFO, "skipping zero height strip %i of %i\n", strip, num_strips);
                continue;
            }

1085
            if (s->pix_fmt == AV_PIX_FMT_RGB24)
1086 1087 1088
                get_sub_picture(s, 0, y,
                                s->input_frame->data, s->input_frame->linesize,
                                data, linesize);
1089
            else
1090
                get_sub_picture(s, 0, y,
1091
                                (uint8_t **)frame->data, (int *)frame->linesize,
1092 1093 1094 1095 1096 1097 1098 1099
                                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);

1100 1101 1102 1103 1104
            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)) < 0)
Tomas Härdin's avatar
Tomas Härdin committed
1105 1106 1107 1108 1109 1110
                return temp_size;

            score += score_temp;
            size += temp_size;
        }

1111
        if (best_score == 0 || score < best_score) {
Tomas Härdin's avatar
Tomas Härdin committed
1112
            best_score = score;
1113
            best_size = size + write_cvid_header(s, s->frame_buf, num_strips, size, isakeyframe);
Tomas Härdin's avatar
Tomas Härdin committed
1114

1115 1116 1117
            FFSWAP(AVFrame *, s->best_frame, s->scratch_frame);
            memcpy(buf, s->frame_buf, best_size);
            best_nstrips = num_strips;
Tomas Härdin's avatar
Tomas Härdin committed
1118
        }
1119 1120 1121
        // avoid trying too many strip numbers without a real reason
        // (this makes the processing of the very first frame faster)
        if (num_strips - best_nstrips > 4)
1122
            break;
Tomas Härdin's avatar
Tomas Härdin committed
1123 1124
    }

1125 1126 1127 1128 1129
    // let the number of strips slowly adapt to the changes in the contents,
    // compared to full bruteforcing every time this will occasionally lead
    // to some r/d performance loss but makes encoding up to several times faster
    if (!s->strip_number_delta_range) {
        if (best_nstrips == s->max_strips) { // let us try to step up
1130
            s->max_strips = best_nstrips + 1;
1131
            if (s->max_strips >= s->max_max_strips)
1132 1133 1134 1135 1136
                s->max_strips = s->max_max_strips;
        } else { // try to step down
            s->max_strips = best_nstrips;
        }
        s->min_strips = s->max_strips - 1;
1137
        if (s->min_strips < s->min_min_strips)
1138 1139 1140
            s->min_strips = s->min_min_strips;
    } else {
        s->max_strips = best_nstrips + s->strip_number_delta_range;
1141
        if (s->max_strips >= s->max_max_strips)
1142 1143
            s->max_strips = s->max_max_strips;
        s->min_strips = best_nstrips - s->strip_number_delta_range;
1144
        if (s->min_strips < s->min_min_strips)
1145
            s->min_strips = s->min_min_strips;
1146
    }
Tomas Härdin's avatar
Tomas Härdin committed
1147 1148 1149 1150

    return best_size;
}

1151 1152
static int cinepak_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
                                const AVFrame *frame, int *got_packet)
Tomas Härdin's avatar
Tomas Härdin committed
1153 1154 1155 1156 1157 1158
{
    CinepakEncContext *s = avctx->priv_data;
    int ret;

    s->lambda = frame->quality ? frame->quality - 1 : 2 * FF_LAMBDA_SCALE;

1159
    if ((ret = ff_alloc_packet2(avctx, pkt, s->frame_buf_size, 0)) < 0)
1160
        return ret;
1161
    ret       = rd_frame(s, frame, (s->curframe == 0), pkt->data, s->frame_buf_size);
1162 1163 1164 1165
    pkt->size = ret;
    if (s->curframe == 0)
        pkt->flags |= AV_PKT_FLAG_KEY;
    *got_packet = 1;
Tomas Härdin's avatar
Tomas Härdin committed
1166

1167
    FFSWAP(AVFrame *, s->last_frame, s->best_frame);
Tomas Härdin's avatar
Tomas Härdin committed
1168 1169 1170 1171

    if (++s->curframe >= s->keyint)
        s->curframe = 0;

1172
    return 0;
Tomas Härdin's avatar
Tomas Härdin committed
1173 1174 1175 1176 1177 1178 1179
}

static av_cold int cinepak_encode_end(AVCodecContext *avctx)
{
    CinepakEncContext *s = avctx->priv_data;
    int x;

1180 1181 1182 1183 1184 1185 1186 1187 1188 1189
    av_frame_free(&s->last_frame);
    av_frame_free(&s->best_frame);
    av_frame_free(&s->scratch_frame);
    if (avctx->pix_fmt == AV_PIX_FMT_RGB24)
        av_frame_free(&s->input_frame);
    av_freep(&s->codebook_input);
    av_freep(&s->codebook_closest);
    av_freep(&s->strip_buf);
    av_freep(&s->frame_buf);
    av_freep(&s->mb);
Tomas Härdin's avatar
Tomas Härdin committed
1190

1191
    for (x = 0; x < (avctx->pix_fmt == AV_PIX_FMT_RGB24 ? 4 : 3); x++)
1192
        av_freep(&s->pict_bufs[x]);
Tomas Härdin's avatar
Tomas Härdin committed
1193 1194 1195 1196 1197

    return 0;
}

AVCodec ff_cinepak_encoder = {
1198
    .name           = "cinepak",
1199
    .long_name      = NULL_IF_CONFIG_SMALL("Cinepak"),
1200 1201 1202 1203 1204 1205
    .type           = AVMEDIA_TYPE_VIDEO,
    .id             = AV_CODEC_ID_CINEPAK,
    .priv_data_size = sizeof(CinepakEncContext),
    .init           = cinepak_encode_init,
    .encode2        = cinepak_encode_frame,
    .close          = cinepak_encode_end,
1206
    .pix_fmts       = (const enum AVPixelFormat[]) { AV_PIX_FMT_RGB24, AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE },
1207
    .priv_class     = &cinepak_class,
Tomas Härdin's avatar
Tomas Härdin committed
1208
};