Statistics
| Branch: | Revision:

ffmpeg / libavcodec / dnxhdenc.c @ 72415b2a

History | View | Annotate | Download (29.2 KB)

1
/*
2
 * VC3/DNxHD encoder
3
 * Copyright (c) 2007 Baptiste Coudurier <baptiste dot coudurier at smartjog dot com>
4
 *
5
 * VC-3 encoder funded by the British Broadcasting Corporation
6
 *
7
 * This file is part of FFmpeg.
8
 *
9
 * FFmpeg is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU Lesser General Public
11
 * License as published by the Free Software Foundation; either
12
 * version 2.1 of the License, or (at your option) any later version.
13
 *
14
 * FFmpeg is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
 * Lesser General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Lesser General Public
20
 * License along with FFmpeg; if not, write to the Free Software
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22
 */
23

    
24
//#define DEBUG
25
#define RC_VARIANCE 1 // use variance or ssd for fast rc
26

    
27
#include "avcodec.h"
28
#include "dsputil.h"
29
#include "mpegvideo.h"
30
#include "dnxhdenc.h"
31

    
32
int dct_quantize_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow);
33

    
34
#define LAMBDA_FRAC_BITS 10
35

    
36
static av_always_inline void dnxhd_get_pixels_8x4(DCTELEM *restrict block, const uint8_t *pixels, int line_size)
37
{
38
    int i;
39
    for (i = 0; i < 4; i++) {
40
        block[0] = pixels[0]; block[1] = pixels[1];
41
        block[2] = pixels[2]; block[3] = pixels[3];
42
        block[4] = pixels[4]; block[5] = pixels[5];
43
        block[6] = pixels[6]; block[7] = pixels[7];
44
        pixels += line_size;
45
        block += 8;
46
    }
47
    memcpy(block   , block- 8, sizeof(*block)*8);
48
    memcpy(block+ 8, block-16, sizeof(*block)*8);
49
    memcpy(block+16, block-24, sizeof(*block)*8);
50
    memcpy(block+24, block-32, sizeof(*block)*8);
51
}
52

    
53
static int dnxhd_init_vlc(DNXHDEncContext *ctx)
54
{
55
    int i, j, level, run;
56
    int max_level = 1<<(ctx->cid_table->bit_depth+2);
57

    
58
    FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->vlc_codes, max_level*4*sizeof(*ctx->vlc_codes), fail);
59
    FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->vlc_bits , max_level*4*sizeof(*ctx->vlc_bits ), fail);
60
    FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->run_codes, 63*2                               , fail);
61
    FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->run_bits , 63                                 , fail);
62

    
63
    ctx->vlc_codes += max_level*2;
64
    ctx->vlc_bits  += max_level*2;
65
    for (level = -max_level; level < max_level; level++) {
66
        for (run = 0; run < 2; run++) {
67
            int index = (level<<1)|run;
68
            int sign, offset = 0, alevel = level;
69

    
70
            MASK_ABS(sign, alevel);
71
            if (alevel > 64) {
72
                offset = (alevel-1)>>6;
73
                alevel -= offset<<6;
74
            }
75
            for (j = 0; j < 257; j++) {
76
                if (ctx->cid_table->ac_level[j] == alevel &&
77
                    (!offset || (ctx->cid_table->ac_index_flag[j] && offset)) &&
78
                    (!run    || (ctx->cid_table->ac_run_flag  [j] && run))) {
79
                    assert(!ctx->vlc_codes[index]);
80
                    if (alevel) {
81
                        ctx->vlc_codes[index] = (ctx->cid_table->ac_codes[j]<<1)|(sign&1);
82
                        ctx->vlc_bits [index] = ctx->cid_table->ac_bits[j]+1;
83
                    } else {
84
                        ctx->vlc_codes[index] = ctx->cid_table->ac_codes[j];
85
                        ctx->vlc_bits [index] = ctx->cid_table->ac_bits [j];
86
                    }
87
                    break;
88
                }
89
            }
90
            assert(!alevel || j < 257);
91
            if (offset) {
92
                ctx->vlc_codes[index] = (ctx->vlc_codes[index]<<ctx->cid_table->index_bits)|offset;
93
                ctx->vlc_bits [index]+= ctx->cid_table->index_bits;
94
            }
95
        }
96
    }
97
    for (i = 0; i < 62; i++) {
98
        int run = ctx->cid_table->run[i];
99
        assert(run < 63);
100
        ctx->run_codes[run] = ctx->cid_table->run_codes[i];
101
        ctx->run_bits [run] = ctx->cid_table->run_bits[i];
102
    }
103
    return 0;
104
 fail:
105
    return -1;
106
}
107

    
108
static int dnxhd_init_qmat(DNXHDEncContext *ctx, int lbias, int cbias)
109
{
110
    // init first elem to 1 to avoid div by 0 in convert_matrix
111
    uint16_t weight_matrix[64] = {1,}; // convert_matrix needs uint16_t*
112
    int qscale, i;
113

    
114
    FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->qmatrix_l,   (ctx->m.avctx->qmax+1) * 64 *     sizeof(int)     , fail);
115
    FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->qmatrix_c,   (ctx->m.avctx->qmax+1) * 64 *     sizeof(int)     , fail);
116
    FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->qmatrix_l16, (ctx->m.avctx->qmax+1) * 64 * 2 * sizeof(uint16_t), fail);
117
    FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->qmatrix_c16, (ctx->m.avctx->qmax+1) * 64 * 2 * sizeof(uint16_t), fail);
118

    
119
    for (i = 1; i < 64; i++) {
120
        int j = ctx->m.dsp.idct_permutation[ff_zigzag_direct[i]];
121
        weight_matrix[j] = ctx->cid_table->luma_weight[i];
122
    }
123
    ff_convert_matrix(&ctx->m.dsp, ctx->qmatrix_l, ctx->qmatrix_l16, weight_matrix,
124
                      ctx->m.intra_quant_bias, 1, ctx->m.avctx->qmax, 1);
125
    for (i = 1; i < 64; i++) {
126
        int j = ctx->m.dsp.idct_permutation[ff_zigzag_direct[i]];
127
        weight_matrix[j] = ctx->cid_table->chroma_weight[i];
128
    }
129
    ff_convert_matrix(&ctx->m.dsp, ctx->qmatrix_c, ctx->qmatrix_c16, weight_matrix,
130
                      ctx->m.intra_quant_bias, 1, ctx->m.avctx->qmax, 1);
131
    for (qscale = 1; qscale <= ctx->m.avctx->qmax; qscale++) {
132
        for (i = 0; i < 64; i++) {
133
            ctx->qmatrix_l  [qscale]   [i] <<= 2; ctx->qmatrix_c  [qscale]   [i] <<= 2;
134
            ctx->qmatrix_l16[qscale][0][i] <<= 2; ctx->qmatrix_l16[qscale][1][i] <<= 2;
135
            ctx->qmatrix_c16[qscale][0][i] <<= 2; ctx->qmatrix_c16[qscale][1][i] <<= 2;
136
        }
137
    }
138
    return 0;
139
 fail:
140
    return -1;
141
}
142

    
143
static int dnxhd_init_rc(DNXHDEncContext *ctx)
144
{
145
    FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->mb_rc, 8160*ctx->m.avctx->qmax*sizeof(RCEntry), fail);
146
    if (ctx->m.avctx->mb_decision != FF_MB_DECISION_RD)
147
        FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->mb_cmp, ctx->m.mb_num*sizeof(RCCMPEntry), fail);
148

    
149
    ctx->frame_bits = (ctx->cid_table->coding_unit_size - 640 - 4) * 8;
150
    ctx->qscale = 1;
151
    ctx->lambda = 2<<LAMBDA_FRAC_BITS; // qscale 2
152
    return 0;
153
 fail:
154
    return -1;
155
}
156

    
157
static int dnxhd_encode_init(AVCodecContext *avctx)
158
{
159
    DNXHDEncContext *ctx = avctx->priv_data;
160
    int i, index;
161

    
162
    ctx->cid = ff_dnxhd_find_cid(avctx);
163
    if (!ctx->cid || avctx->pix_fmt != PIX_FMT_YUV422P) {
164
        av_log(avctx, AV_LOG_ERROR, "video parameters incompatible with DNxHD\n");
165
        return -1;
166
    }
167
    av_log(avctx, AV_LOG_DEBUG, "cid %d\n", ctx->cid);
168

    
169
    index = ff_dnxhd_get_cid_table(ctx->cid);
170
    ctx->cid_table = &ff_dnxhd_cid_table[index];
171

    
172
    ctx->m.avctx = avctx;
173
    ctx->m.mb_intra = 1;
174
    ctx->m.h263_aic = 1;
175

    
176
    ctx->get_pixels_8x4_sym = dnxhd_get_pixels_8x4;
177

    
178
    dsputil_init(&ctx->m.dsp, avctx);
179
    ff_dct_common_init(&ctx->m);
180
#if HAVE_MMX
181
    ff_dnxhd_init_mmx(ctx);
182
#endif
183
    if (!ctx->m.dct_quantize)
184
        ctx->m.dct_quantize = dct_quantize_c;
185

    
186
    ctx->m.mb_height = (avctx->height + 15) / 16;
187
    ctx->m.mb_width  = (avctx->width  + 15) / 16;
188

    
189
    if (avctx->flags & CODEC_FLAG_INTERLACED_DCT) {
190
        ctx->interlaced = 1;
191
        ctx->m.mb_height /= 2;
192
    }
193

    
194
    ctx->m.mb_num = ctx->m.mb_height * ctx->m.mb_width;
195

    
196
    if (avctx->intra_quant_bias != FF_DEFAULT_QUANT_BIAS)
197
        ctx->m.intra_quant_bias = avctx->intra_quant_bias;
198
    if (dnxhd_init_qmat(ctx, ctx->m.intra_quant_bias, 0) < 0) // XXX tune lbias/cbias
199
        return -1;
200

    
201
    if (dnxhd_init_vlc(ctx) < 0)
202
        return -1;
203
    if (dnxhd_init_rc(ctx) < 0)
204
        return -1;
205

    
206
    FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->slice_size, ctx->m.mb_height*sizeof(uint32_t), fail);
207
    FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->slice_offs, ctx->m.mb_height*sizeof(uint32_t), fail);
208
    FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->mb_bits,    ctx->m.mb_num   *sizeof(uint16_t), fail);
209
    FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->mb_qscale,  ctx->m.mb_num   *sizeof(uint8_t) , fail);
210

    
211
    ctx->frame.key_frame = 1;
212
    ctx->frame.pict_type = FF_I_TYPE;
213
    ctx->m.avctx->coded_frame = &ctx->frame;
214

    
215
    if (avctx->thread_count > MAX_THREADS) {
216
        av_log(avctx, AV_LOG_ERROR, "too many threads\n");
217
        return -1;
218
    }
219

    
220
    ctx->thread[0] = ctx;
221
    for (i = 1; i < avctx->thread_count; i++) {
222
        ctx->thread[i] =  av_malloc(sizeof(DNXHDEncContext));
223
        memcpy(ctx->thread[i], ctx, sizeof(DNXHDEncContext));
224
    }
225

    
226
    return 0;
227
 fail: //for FF_ALLOCZ_OR_GOTO
228
    return -1;
229
}
230

    
231
static int dnxhd_write_header(AVCodecContext *avctx, uint8_t *buf)
232
{
233
    DNXHDEncContext *ctx = avctx->priv_data;
234
    const uint8_t header_prefix[5] = { 0x00,0x00,0x02,0x80,0x01 };
235

    
236
    memset(buf, 0, 640);
237

    
238
    memcpy(buf, header_prefix, 5);
239
    buf[5] = ctx->interlaced ? ctx->cur_field+2 : 0x01;
240
    buf[6] = 0x80; // crc flag off
241
    buf[7] = 0xa0; // reserved
242
    AV_WB16(buf + 0x18, avctx->height); // ALPF
243
    AV_WB16(buf + 0x1a, avctx->width);  // SPL
244
    AV_WB16(buf + 0x1d, avctx->height); // NAL
245

    
246
    buf[0x21] = 0x38; // FIXME 8 bit per comp
247
    buf[0x22] = 0x88 + (ctx->frame.interlaced_frame<<2);
248
    AV_WB32(buf + 0x28, ctx->cid); // CID
249
    buf[0x2c] = ctx->interlaced ? 0 : 0x80;
250

    
251
    buf[0x5f] = 0x01; // UDL
252

    
253
    buf[0x167] = 0x02; // reserved
254
    AV_WB16(buf + 0x16a, ctx->m.mb_height * 4 + 4); // MSIPS
255
    buf[0x16d] = ctx->m.mb_height; // Ns
256
    buf[0x16f] = 0x10; // reserved
257

    
258
    ctx->msip = buf + 0x170;
259
    return 0;
260
}
261

    
262
static av_always_inline void dnxhd_encode_dc(DNXHDEncContext *ctx, int diff)
263
{
264
    int nbits;
265
    if (diff < 0) {
266
        nbits = av_log2_16bit(-2*diff);
267
        diff--;
268
    } else {
269
        nbits = av_log2_16bit(2*diff);
270
    }
271
    put_bits(&ctx->m.pb, ctx->cid_table->dc_bits[nbits] + nbits,
272
             (ctx->cid_table->dc_codes[nbits]<<nbits) + (diff & ((1 << nbits) - 1)));
273
}
274

    
275
static av_always_inline void dnxhd_encode_block(DNXHDEncContext *ctx, DCTELEM *block, int last_index, int n)
276
{
277
    int last_non_zero = 0;
278
    int slevel, i, j;
279

    
280
    dnxhd_encode_dc(ctx, block[0] - ctx->m.last_dc[n]);
281
    ctx->m.last_dc[n] = block[0];
282

    
283
    for (i = 1; i <= last_index; i++) {
284
        j = ctx->m.intra_scantable.permutated[i];
285
        slevel = block[j];
286
        if (slevel) {
287
            int run_level = i - last_non_zero - 1;
288
            int rlevel = (slevel<<1)|!!run_level;
289
            put_bits(&ctx->m.pb, ctx->vlc_bits[rlevel], ctx->vlc_codes[rlevel]);
290
            if (run_level)
291
                put_bits(&ctx->m.pb, ctx->run_bits[run_level], ctx->run_codes[run_level]);
292
            last_non_zero = i;
293
        }
294
    }
295
    put_bits(&ctx->m.pb, ctx->vlc_bits[0], ctx->vlc_codes[0]); // EOB
296
}
297

    
298
static av_always_inline void dnxhd_unquantize_c(DNXHDEncContext *ctx, DCTELEM *block, int n, int qscale, int last_index)
299
{
300
    const uint8_t *weight_matrix;
301
    int level;
302
    int i;
303

    
304
    weight_matrix = (n&2) ? ctx->cid_table->chroma_weight : ctx->cid_table->luma_weight;
305

    
306
    for (i = 1; i <= last_index; i++) {
307
        int j = ctx->m.intra_scantable.permutated[i];
308
        level = block[j];
309
        if (level) {
310
            if (level < 0) {
311
                level = (1-2*level) * qscale * weight_matrix[i];
312
                if (weight_matrix[i] != 32)
313
                    level += 32;
314
                level >>= 6;
315
                level = -level;
316
            } else {
317
                level = (2*level+1) * qscale * weight_matrix[i];
318
                if (weight_matrix[i] != 32)
319
                    level += 32;
320
                level >>= 6;
321
            }
322
            block[j] = level;
323
        }
324
    }
325
}
326

    
327
static av_always_inline int dnxhd_ssd_block(DCTELEM *qblock, DCTELEM *block)
328
{
329
    int score = 0;
330
    int i;
331
    for (i = 0; i < 64; i++)
332
        score += (block[i]-qblock[i])*(block[i]-qblock[i]);
333
    return score;
334
}
335

    
336
static av_always_inline int dnxhd_calc_ac_bits(DNXHDEncContext *ctx, DCTELEM *block, int last_index)
337
{
338
    int last_non_zero = 0;
339
    int bits = 0;
340
    int i, j, level;
341
    for (i = 1; i <= last_index; i++) {
342
        j = ctx->m.intra_scantable.permutated[i];
343
        level = block[j];
344
        if (level) {
345
            int run_level = i - last_non_zero - 1;
346
            bits += ctx->vlc_bits[(level<<1)|!!run_level]+ctx->run_bits[run_level];
347
            last_non_zero = i;
348
        }
349
    }
350
    return bits;
351
}
352

    
353
static av_always_inline void dnxhd_get_blocks(DNXHDEncContext *ctx, int mb_x, int mb_y)
354
{
355
    const uint8_t *ptr_y = ctx->thread[0]->src[0] + ((mb_y << 4) * ctx->m.linesize)   + (mb_x << 4);
356
    const uint8_t *ptr_u = ctx->thread[0]->src[1] + ((mb_y << 4) * ctx->m.uvlinesize) + (mb_x << 3);
357
    const uint8_t *ptr_v = ctx->thread[0]->src[2] + ((mb_y << 4) * ctx->m.uvlinesize) + (mb_x << 3);
358
    DSPContext *dsp = &ctx->m.dsp;
359

    
360
    dsp->get_pixels(ctx->blocks[0], ptr_y    , ctx->m.linesize);
361
    dsp->get_pixels(ctx->blocks[1], ptr_y + 8, ctx->m.linesize);
362
    dsp->get_pixels(ctx->blocks[2], ptr_u    , ctx->m.uvlinesize);
363
    dsp->get_pixels(ctx->blocks[3], ptr_v    , ctx->m.uvlinesize);
364

    
365
    if (mb_y+1 == ctx->m.mb_height && ctx->m.avctx->height == 1080) {
366
        if (ctx->interlaced) {
367
            ctx->get_pixels_8x4_sym(ctx->blocks[4], ptr_y + ctx->dct_y_offset    , ctx->m.linesize);
368
            ctx->get_pixels_8x4_sym(ctx->blocks[5], ptr_y + ctx->dct_y_offset + 8, ctx->m.linesize);
369
            ctx->get_pixels_8x4_sym(ctx->blocks[6], ptr_u + ctx->dct_uv_offset   , ctx->m.uvlinesize);
370
            ctx->get_pixels_8x4_sym(ctx->blocks[7], ptr_v + ctx->dct_uv_offset   , ctx->m.uvlinesize);
371
        } else {
372
            dsp->clear_block(ctx->blocks[4]); dsp->clear_block(ctx->blocks[5]);
373
            dsp->clear_block(ctx->blocks[6]); dsp->clear_block(ctx->blocks[7]);
374
        }
375
    } else {
376
        dsp->get_pixels(ctx->blocks[4], ptr_y + ctx->dct_y_offset    , ctx->m.linesize);
377
        dsp->get_pixels(ctx->blocks[5], ptr_y + ctx->dct_y_offset + 8, ctx->m.linesize);
378
        dsp->get_pixels(ctx->blocks[6], ptr_u + ctx->dct_uv_offset   , ctx->m.uvlinesize);
379
        dsp->get_pixels(ctx->blocks[7], ptr_v + ctx->dct_uv_offset   , ctx->m.uvlinesize);
380
    }
381
}
382

    
383
static av_always_inline int dnxhd_switch_matrix(DNXHDEncContext *ctx, int i)
384
{
385
    if (i&2) {
386
        ctx->m.q_intra_matrix16 = ctx->qmatrix_c16;
387
        ctx->m.q_intra_matrix   = ctx->qmatrix_c;
388
        return 1 + (i&1);
389
    } else {
390
        ctx->m.q_intra_matrix16 = ctx->qmatrix_l16;
391
        ctx->m.q_intra_matrix   = ctx->qmatrix_l;
392
        return 0;
393
    }
394
}
395

    
396
static int dnxhd_calc_bits_thread(AVCodecContext *avctx, void *arg, int jobnr, int threadnr)
397
{
398
    DNXHDEncContext *ctx = avctx->priv_data;
399
    int mb_y = jobnr, mb_x;
400
    int qscale = ctx->qscale;
401
    LOCAL_ALIGNED_16(DCTELEM, block, [64]);
402
    ctx = ctx->thread[threadnr];
403

    
404
    ctx->m.last_dc[0] =
405
    ctx->m.last_dc[1] =
406
    ctx->m.last_dc[2] = 1024;
407

    
408
    for (mb_x = 0; mb_x < ctx->m.mb_width; mb_x++) {
409
        unsigned mb = mb_y * ctx->m.mb_width + mb_x;
410
        int ssd     = 0;
411
        int ac_bits = 0;
412
        int dc_bits = 0;
413
        int i;
414

    
415
        dnxhd_get_blocks(ctx, mb_x, mb_y);
416

    
417
        for (i = 0; i < 8; i++) {
418
            DCTELEM *src_block = ctx->blocks[i];
419
            int overflow, nbits, diff, last_index;
420
            int n = dnxhd_switch_matrix(ctx, i);
421

    
422
            memcpy(block, src_block, 64*sizeof(*block));
423
            last_index = ctx->m.dct_quantize((MpegEncContext*)ctx, block, i, qscale, &overflow);
424
            ac_bits += dnxhd_calc_ac_bits(ctx, block, last_index);
425

    
426
            diff = block[0] - ctx->m.last_dc[n];
427
            if (diff < 0) nbits = av_log2_16bit(-2*diff);
428
            else          nbits = av_log2_16bit( 2*diff);
429
            dc_bits += ctx->cid_table->dc_bits[nbits] + nbits;
430

    
431
            ctx->m.last_dc[n] = block[0];
432

    
433
            if (avctx->mb_decision == FF_MB_DECISION_RD || !RC_VARIANCE) {
434
                dnxhd_unquantize_c(ctx, block, i, qscale, last_index);
435
                ctx->m.dsp.idct(block);
436
                ssd += dnxhd_ssd_block(block, src_block);
437
            }
438
        }
439
        ctx->mb_rc[qscale][mb].ssd = ssd;
440
        ctx->mb_rc[qscale][mb].bits = ac_bits+dc_bits+12+8*ctx->vlc_bits[0];
441
    }
442
    return 0;
443
}
444

    
445
static int dnxhd_encode_thread(AVCodecContext *avctx, void *arg, int jobnr, int threadnr)
446
{
447
    DNXHDEncContext *ctx = avctx->priv_data;
448
    int mb_y = jobnr, mb_x;
449
    ctx = ctx->thread[threadnr];
450
    init_put_bits(&ctx->m.pb, (uint8_t *)arg + 640 + ctx->slice_offs[jobnr], ctx->slice_size[jobnr]);
451

    
452
    ctx->m.last_dc[0] =
453
    ctx->m.last_dc[1] =
454
    ctx->m.last_dc[2] = 1024;
455
    for (mb_x = 0; mb_x < ctx->m.mb_width; mb_x++) {
456
        unsigned mb = mb_y * ctx->m.mb_width + mb_x;
457
        int qscale = ctx->mb_qscale[mb];
458
        int i;
459

    
460
        put_bits(&ctx->m.pb, 12, qscale<<1);
461

    
462
        dnxhd_get_blocks(ctx, mb_x, mb_y);
463

    
464
        for (i = 0; i < 8; i++) {
465
            DCTELEM *block = ctx->blocks[i];
466
            int last_index, overflow;
467
            int n = dnxhd_switch_matrix(ctx, i);
468
            last_index = ctx->m.dct_quantize((MpegEncContext*)ctx, block, i, qscale, &overflow);
469
            //START_TIMER;
470
            dnxhd_encode_block(ctx, block, last_index, n);
471
            //STOP_TIMER("encode_block");
472
        }
473
    }
474
    if (put_bits_count(&ctx->m.pb)&31)
475
        put_bits(&ctx->m.pb, 32-(put_bits_count(&ctx->m.pb)&31), 0);
476
    flush_put_bits(&ctx->m.pb);
477
    return 0;
478
}
479

    
480
static void dnxhd_setup_threads_slices(DNXHDEncContext *ctx)
481
{
482
    int mb_y, mb_x;
483
    int offset = 0;
484
    for (mb_y = 0; mb_y < ctx->m.mb_height; mb_y++) {
485
        int thread_size;
486
        ctx->slice_offs[mb_y] = offset;
487
            ctx->slice_size[mb_y] = 0;
488
            for (mb_x = 0; mb_x < ctx->m.mb_width; mb_x++) {
489
                unsigned mb = mb_y * ctx->m.mb_width + mb_x;
490
                ctx->slice_size[mb_y] += ctx->mb_bits[mb];
491
            }
492
            ctx->slice_size[mb_y] = (ctx->slice_size[mb_y]+31)&~31;
493
            ctx->slice_size[mb_y] >>= 3;
494
            thread_size = ctx->slice_size[mb_y];
495
        offset += thread_size;
496
    }
497
}
498

    
499
static int dnxhd_mb_var_thread(AVCodecContext *avctx, void *arg, int jobnr, int threadnr)
500
{
501
    DNXHDEncContext *ctx = avctx->priv_data;
502
    int mb_y = jobnr, mb_x;
503
    ctx = ctx->thread[threadnr];
504
    for (mb_x = 0; mb_x < ctx->m.mb_width; mb_x++) {
505
        unsigned mb  = mb_y * ctx->m.mb_width + mb_x;
506
        uint8_t *pix = ctx->thread[0]->src[0] + ((mb_y<<4) * ctx->m.linesize) + (mb_x<<4);
507
        int sum      = ctx->m.dsp.pix_sum(pix, ctx->m.linesize);
508
        int varc     = (ctx->m.dsp.pix_norm1(pix, ctx->m.linesize) - (((unsigned)(sum*sum))>>8)+128)>>8;
509
        ctx->mb_cmp[mb].value = varc;
510
        ctx->mb_cmp[mb].mb = mb;
511
    }
512
    return 0;
513
}
514

    
515
static int dnxhd_encode_rdo(AVCodecContext *avctx, DNXHDEncContext *ctx)
516
{
517
    int lambda, up_step, down_step;
518
    int last_lower = INT_MAX, last_higher = 0;
519
    int x, y, q;
520

    
521
    for (q = 1; q < avctx->qmax; q++) {
522
        ctx->qscale = q;
523
        avctx->execute2(avctx, dnxhd_calc_bits_thread, NULL, NULL, ctx->m.mb_height);
524
    }
525
    up_step = down_step = 2<<LAMBDA_FRAC_BITS;
526
    lambda = ctx->lambda;
527

    
528
    for (;;) {
529
        int bits = 0;
530
        int end = 0;
531
        if (lambda == last_higher) {
532
            lambda++;
533
            end = 1; // need to set final qscales/bits
534
        }
535
        for (y = 0; y < ctx->m.mb_height; y++) {
536
            for (x = 0; x < ctx->m.mb_width; x++) {
537
                unsigned min = UINT_MAX;
538
                int qscale = 1;
539
                int mb = y*ctx->m.mb_width+x;
540
                for (q = 1; q < avctx->qmax; q++) {
541
                    unsigned score = ctx->mb_rc[q][mb].bits*lambda+(ctx->mb_rc[q][mb].ssd<<LAMBDA_FRAC_BITS);
542
                    if (score < min) {
543
                        min = score;
544
                        qscale = q;
545
                    }
546
                }
547
                bits += ctx->mb_rc[qscale][mb].bits;
548
                ctx->mb_qscale[mb] = qscale;
549
                ctx->mb_bits[mb] = ctx->mb_rc[qscale][mb].bits;
550
            }
551
            bits = (bits+31)&~31; // padding
552
            if (bits > ctx->frame_bits)
553
                break;
554
        }
555
        //dprintf(ctx->m.avctx, "lambda %d, up %u, down %u, bits %d, frame %d\n",
556
        //        lambda, last_higher, last_lower, bits, ctx->frame_bits);
557
        if (end) {
558
            if (bits > ctx->frame_bits)
559
                return -1;
560
            break;
561
        }
562
        if (bits < ctx->frame_bits) {
563
            last_lower = FFMIN(lambda, last_lower);
564
            if (last_higher != 0)
565
                lambda = (lambda+last_higher)>>1;
566
            else
567
                lambda -= down_step;
568
            down_step *= 5; // XXX tune ?
569
            up_step = 1<<LAMBDA_FRAC_BITS;
570
            lambda = FFMAX(1, lambda);
571
            if (lambda == last_lower)
572
                break;
573
        } else {
574
            last_higher = FFMAX(lambda, last_higher);
575
            if (last_lower != INT_MAX)
576
                lambda = (lambda+last_lower)>>1;
577
            else if ((int64_t)lambda + up_step > INT_MAX)
578
                return -1;
579
            else
580
                lambda += up_step;
581
            up_step = FFMIN((int64_t)up_step*5, INT_MAX);
582
            down_step = 1<<LAMBDA_FRAC_BITS;
583
        }
584
    }
585
    //dprintf(ctx->m.avctx, "out lambda %d\n", lambda);
586
    ctx->lambda = lambda;
587
    return 0;
588
}
589

    
590
static int dnxhd_find_qscale(DNXHDEncContext *ctx)
591
{
592
    int bits = 0;
593
    int up_step = 1;
594
    int down_step = 1;
595
    int last_higher = 0;
596
    int last_lower = INT_MAX;
597
    int qscale;
598
    int x, y;
599

    
600
    qscale = ctx->qscale;
601
    for (;;) {
602
        bits = 0;
603
        ctx->qscale = qscale;
604
        // XXX avoid recalculating bits
605
        ctx->m.avctx->execute2(ctx->m.avctx, dnxhd_calc_bits_thread, NULL, NULL, ctx->m.mb_height);
606
        for (y = 0; y < ctx->m.mb_height; y++) {
607
            for (x = 0; x < ctx->m.mb_width; x++)
608
                bits += ctx->mb_rc[qscale][y*ctx->m.mb_width+x].bits;
609
            bits = (bits+31)&~31; // padding
610
            if (bits > ctx->frame_bits)
611
                break;
612
        }
613
        //dprintf(ctx->m.avctx, "%d, qscale %d, bits %d, frame %d, higher %d, lower %d\n",
614
        //        ctx->m.avctx->frame_number, qscale, bits, ctx->frame_bits, last_higher, last_lower);
615
        if (bits < ctx->frame_bits) {
616
            if (qscale == 1)
617
                return 1;
618
            if (last_higher == qscale - 1) {
619
                qscale = last_higher;
620
                break;
621
            }
622
            last_lower = FFMIN(qscale, last_lower);
623
            if (last_higher != 0)
624
                qscale = (qscale+last_higher)>>1;
625
            else
626
                qscale -= down_step++;
627
            if (qscale < 1)
628
                qscale = 1;
629
            up_step = 1;
630
        } else {
631
            if (last_lower == qscale + 1)
632
                break;
633
            last_higher = FFMAX(qscale, last_higher);
634
            if (last_lower != INT_MAX)
635
                qscale = (qscale+last_lower)>>1;
636
            else
637
                qscale += up_step++;
638
            down_step = 1;
639
            if (qscale >= ctx->m.avctx->qmax)
640
                return -1;
641
        }
642
    }
643
    //dprintf(ctx->m.avctx, "out qscale %d\n", qscale);
644
    ctx->qscale = qscale;
645
    return 0;
646
}
647

    
648
#define BUCKET_BITS 8
649
#define RADIX_PASSES 4
650
#define NBUCKETS (1 << BUCKET_BITS)
651

    
652
static inline int get_bucket(int value, int shift)
653
{
654
    value >>= shift;
655
    value &= NBUCKETS - 1;
656
    return NBUCKETS - 1 - value;
657
}
658

    
659
static void radix_count(const RCCMPEntry *data, int size, int buckets[RADIX_PASSES][NBUCKETS])
660
{
661
    int i, j;
662
    memset(buckets, 0, sizeof(buckets[0][0]) * RADIX_PASSES * NBUCKETS);
663
    for (i = 0; i < size; i++) {
664
        int v = data[i].value;
665
        for (j = 0; j < RADIX_PASSES; j++) {
666
            buckets[j][get_bucket(v, 0)]++;
667
            v >>= BUCKET_BITS;
668
        }
669
        assert(!v);
670
    }
671
    for (j = 0; j < RADIX_PASSES; j++) {
672
        int offset = size;
673
        for (i = NBUCKETS - 1; i >= 0; i--)
674
            buckets[j][i] = offset -= buckets[j][i];
675
        assert(!buckets[j][0]);
676
    }
677
}
678

    
679
static void radix_sort_pass(RCCMPEntry *dst, const RCCMPEntry *data, int size, int buckets[NBUCKETS], int pass)
680
{
681
    int shift = pass * BUCKET_BITS;
682
    int i;
683
    for (i = 0; i < size; i++) {
684
        int v = get_bucket(data[i].value, shift);
685
        int pos = buckets[v]++;
686
        dst[pos] = data[i];
687
    }
688
}
689

    
690
static void radix_sort(RCCMPEntry *data, int size)
691
{
692
    int buckets[RADIX_PASSES][NBUCKETS];
693
    RCCMPEntry *tmp = av_malloc(sizeof(*tmp) * size);
694
    radix_count(data, size, buckets);
695
    radix_sort_pass(tmp, data, size, buckets[0], 0);
696
    radix_sort_pass(data, tmp, size, buckets[1], 1);
697
    if (buckets[2][NBUCKETS - 1] || buckets[3][NBUCKETS - 1]) {
698
        radix_sort_pass(tmp, data, size, buckets[2], 2);
699
        radix_sort_pass(data, tmp, size, buckets[3], 3);
700
    }
701
    av_free(tmp);
702
}
703

    
704
static int dnxhd_encode_fast(AVCodecContext *avctx, DNXHDEncContext *ctx)
705
{
706
    int max_bits = 0;
707
    int ret, x, y;
708
    if ((ret = dnxhd_find_qscale(ctx)) < 0)
709
        return -1;
710
    for (y = 0; y < ctx->m.mb_height; y++) {
711
        for (x = 0; x < ctx->m.mb_width; x++) {
712
            int mb = y*ctx->m.mb_width+x;
713
            int delta_bits;
714
            ctx->mb_qscale[mb] = ctx->qscale;
715
            ctx->mb_bits[mb] = ctx->mb_rc[ctx->qscale][mb].bits;
716
            max_bits += ctx->mb_rc[ctx->qscale][mb].bits;
717
            if (!RC_VARIANCE) {
718
                delta_bits = ctx->mb_rc[ctx->qscale][mb].bits-ctx->mb_rc[ctx->qscale+1][mb].bits;
719
                ctx->mb_cmp[mb].mb = mb;
720
                ctx->mb_cmp[mb].value = delta_bits ?
721
                    ((ctx->mb_rc[ctx->qscale][mb].ssd-ctx->mb_rc[ctx->qscale+1][mb].ssd)*100)/delta_bits
722
                    : INT_MIN; //avoid increasing qscale
723
            }
724
        }
725
        max_bits += 31; //worst padding
726
    }
727
    if (!ret) {
728
        if (RC_VARIANCE)
729
            avctx->execute2(avctx, dnxhd_mb_var_thread, NULL, NULL, ctx->m.mb_height);
730
        radix_sort(ctx->mb_cmp, ctx->m.mb_num);
731
        for (x = 0; x < ctx->m.mb_num && max_bits > ctx->frame_bits; x++) {
732
            int mb = ctx->mb_cmp[x].mb;
733
            max_bits -= ctx->mb_rc[ctx->qscale][mb].bits - ctx->mb_rc[ctx->qscale+1][mb].bits;
734
            ctx->mb_qscale[mb] = ctx->qscale+1;
735
            ctx->mb_bits[mb] = ctx->mb_rc[ctx->qscale+1][mb].bits;
736
        }
737
    }
738
    return 0;
739
}
740

    
741
static void dnxhd_load_picture(DNXHDEncContext *ctx, const AVFrame *frame)
742
{
743
    int i;
744

    
745
    for (i = 0; i < 3; i++) {
746
        ctx->frame.data[i]     = frame->data[i];
747
        ctx->frame.linesize[i] = frame->linesize[i];
748
    }
749

    
750
    for (i = 0; i < ctx->m.avctx->thread_count; i++) {
751
        ctx->thread[i]->m.linesize    = ctx->frame.linesize[0]<<ctx->interlaced;
752
        ctx->thread[i]->m.uvlinesize  = ctx->frame.linesize[1]<<ctx->interlaced;
753
        ctx->thread[i]->dct_y_offset  = ctx->m.linesize  *8;
754
        ctx->thread[i]->dct_uv_offset = ctx->m.uvlinesize*8;
755
    }
756

    
757
    ctx->frame.interlaced_frame = frame->interlaced_frame;
758
    ctx->cur_field = frame->interlaced_frame && !frame->top_field_first;
759
}
760

    
761
static int dnxhd_encode_picture(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data)
762
{
763
    DNXHDEncContext *ctx = avctx->priv_data;
764
    int first_field = 1;
765
    int offset, i, ret;
766

    
767
    if (buf_size < ctx->cid_table->frame_size) {
768
        av_log(avctx, AV_LOG_ERROR, "output buffer is too small to compress picture\n");
769
        return -1;
770
    }
771

    
772
    dnxhd_load_picture(ctx, data);
773

    
774
 encode_coding_unit:
775
    for (i = 0; i < 3; i++) {
776
        ctx->src[i] = ctx->frame.data[i];
777
        if (ctx->interlaced && ctx->cur_field)
778
            ctx->src[i] += ctx->frame.linesize[i];
779
    }
780

    
781
    dnxhd_write_header(avctx, buf);
782

    
783
    if (avctx->mb_decision == FF_MB_DECISION_RD)
784
        ret = dnxhd_encode_rdo(avctx, ctx);
785
    else
786
        ret = dnxhd_encode_fast(avctx, ctx);
787
    if (ret < 0) {
788
        av_log(avctx, AV_LOG_ERROR,
789
               "picture could not fit ratecontrol constraints, increase qmax\n");
790
        return -1;
791
    }
792

    
793
    dnxhd_setup_threads_slices(ctx);
794

    
795
    offset = 0;
796
    for (i = 0; i < ctx->m.mb_height; i++) {
797
        AV_WB32(ctx->msip + i * 4, offset);
798
        offset += ctx->slice_size[i];
799
        assert(!(ctx->slice_size[i] & 3));
800
    }
801

    
802
    avctx->execute2(avctx, dnxhd_encode_thread, buf, NULL, ctx->m.mb_height);
803

    
804
    assert(640 + offset + 4 <= ctx->cid_table->coding_unit_size);
805
    memset(buf + 640 + offset, 0, ctx->cid_table->coding_unit_size - 4 - offset - 640);
806

    
807
    AV_WB32(buf + ctx->cid_table->coding_unit_size - 4, 0x600DC0DE); // EOF
808

    
809
    if (ctx->interlaced && first_field) {
810
        first_field     = 0;
811
        ctx->cur_field ^= 1;
812
        buf      += ctx->cid_table->coding_unit_size;
813
        buf_size -= ctx->cid_table->coding_unit_size;
814
        goto encode_coding_unit;
815
    }
816

    
817
    ctx->frame.quality = ctx->qscale*FF_QP2LAMBDA;
818

    
819
    return ctx->cid_table->frame_size;
820
}
821

    
822
static int dnxhd_encode_end(AVCodecContext *avctx)
823
{
824
    DNXHDEncContext *ctx = avctx->priv_data;
825
    int max_level = 1<<(ctx->cid_table->bit_depth+2);
826
    int i;
827

    
828
    av_free(ctx->vlc_codes-max_level*2);
829
    av_free(ctx->vlc_bits -max_level*2);
830
    av_freep(&ctx->run_codes);
831
    av_freep(&ctx->run_bits);
832

    
833
    av_freep(&ctx->mb_bits);
834
    av_freep(&ctx->mb_qscale);
835
    av_freep(&ctx->mb_rc);
836
    av_freep(&ctx->mb_cmp);
837
    av_freep(&ctx->slice_size);
838
    av_freep(&ctx->slice_offs);
839

    
840
    av_freep(&ctx->qmatrix_c);
841
    av_freep(&ctx->qmatrix_l);
842
    av_freep(&ctx->qmatrix_c16);
843
    av_freep(&ctx->qmatrix_l16);
844

    
845
    for (i = 1; i < avctx->thread_count; i++)
846
        av_freep(&ctx->thread[i]);
847

    
848
    return 0;
849
}
850

    
851
AVCodec dnxhd_encoder = {
852
    "dnxhd",
853
    AVMEDIA_TYPE_VIDEO,
854
    CODEC_ID_DNXHD,
855
    sizeof(DNXHDEncContext),
856
    dnxhd_encode_init,
857
    dnxhd_encode_picture,
858
    dnxhd_encode_end,
859
    .pix_fmts = (const enum PixelFormat[]){PIX_FMT_YUV422P, PIX_FMT_NONE},
860
    .long_name = NULL_IF_CONFIG_SMALL("VC3/DNxHD"),
861
};