dnxhdenc: whitespace cosmetics
[libav.git] / libavcodec / dnxhdenc.c
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 Libav.
8 *
9 * Libav 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 * Libav 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 Libav; 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 "libavutil/opt.h"
28 #include "avcodec.h"
29 #include "dsputil.h"
30 #include "mpegvideo.h"
31 #include "mpegvideo_common.h"
32 #include "dnxhdenc.h"
33
34 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
35
36 static const AVOption options[]={
37 {"nitris_compat", "encode with Avid Nitris compatibility", offsetof(DNXHDEncContext, nitris_compat), FF_OPT_TYPE_INT, {.dbl = 0}, 0, 1, VE},
38 {NULL}
39 };
40 static const AVClass class = { "dnxhd", av_default_item_name, options, LIBAVUTIL_VERSION_INT };
41
42 #define LAMBDA_FRAC_BITS 10
43
44 static av_always_inline void dnxhd_get_pixels_8x4(DCTELEM *restrict block, const uint8_t *pixels, int line_size)
45 {
46 int i;
47 for (i = 0; i < 4; i++) {
48 block[0] = pixels[0]; block[1] = pixels[1];
49 block[2] = pixels[2]; block[3] = pixels[3];
50 block[4] = pixels[4]; block[5] = pixels[5];
51 block[6] = pixels[6]; block[7] = pixels[7];
52 pixels += line_size;
53 block += 8;
54 }
55 memcpy(block, block - 8, sizeof(*block) * 8);
56 memcpy(block + 8, block - 16, sizeof(*block) * 8);
57 memcpy(block + 16, block - 24, sizeof(*block) * 8);
58 memcpy(block + 24, block - 32, sizeof(*block) * 8);
59 }
60
61 static int dnxhd_init_vlc(DNXHDEncContext *ctx)
62 {
63 int i, j, level, run;
64 int max_level = 1<<(ctx->cid_table->bit_depth+2);
65
66 FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->vlc_codes, max_level*4*sizeof(*ctx->vlc_codes), fail);
67 FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->vlc_bits, max_level*4*sizeof(*ctx->vlc_bits) , fail);
68 FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->run_codes, 63*2, fail);
69 FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->run_bits, 63, fail);
70
71 ctx->vlc_codes += max_level*2;
72 ctx->vlc_bits += max_level*2;
73 for (level = -max_level; level < max_level; level++) {
74 for (run = 0; run < 2; run++) {
75 int index = (level<<1)|run;
76 int sign, offset = 0, alevel = level;
77
78 MASK_ABS(sign, alevel);
79 if (alevel > 64) {
80 offset = (alevel-1)>>6;
81 alevel -= offset<<6;
82 }
83 for (j = 0; j < 257; j++) {
84 if (ctx->cid_table->ac_level[j] == alevel &&
85 (!offset || (ctx->cid_table->ac_index_flag[j] && offset)) &&
86 (!run || (ctx->cid_table->ac_run_flag [j] && run))) {
87 assert(!ctx->vlc_codes[index]);
88 if (alevel) {
89 ctx->vlc_codes[index] = (ctx->cid_table->ac_codes[j]<<1)|(sign&1);
90 ctx->vlc_bits [index] = ctx->cid_table->ac_bits[j]+1;
91 } else {
92 ctx->vlc_codes[index] = ctx->cid_table->ac_codes[j];
93 ctx->vlc_bits [index] = ctx->cid_table->ac_bits [j];
94 }
95 break;
96 }
97 }
98 assert(!alevel || j < 257);
99 if (offset) {
100 ctx->vlc_codes[index] = (ctx->vlc_codes[index]<<ctx->cid_table->index_bits)|offset;
101 ctx->vlc_bits [index]+= ctx->cid_table->index_bits;
102 }
103 }
104 }
105 for (i = 0; i < 62; i++) {
106 int run = ctx->cid_table->run[i];
107 assert(run < 63);
108 ctx->run_codes[run] = ctx->cid_table->run_codes[i];
109 ctx->run_bits [run] = ctx->cid_table->run_bits[i];
110 }
111 return 0;
112 fail:
113 return -1;
114 }
115
116 static int dnxhd_init_qmat(DNXHDEncContext *ctx, int lbias, int cbias)
117 {
118 // init first elem to 1 to avoid div by 0 in convert_matrix
119 uint16_t weight_matrix[64] = {1,}; // convert_matrix needs uint16_t*
120 int qscale, i;
121
122 FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->qmatrix_l, (ctx->m.avctx->qmax+1) * 64 * sizeof(int), fail);
123 FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->qmatrix_c, (ctx->m.avctx->qmax+1) * 64 * sizeof(int), fail);
124 FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->qmatrix_l16, (ctx->m.avctx->qmax+1) * 64 * 2 * sizeof(uint16_t), fail);
125 FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->qmatrix_c16, (ctx->m.avctx->qmax+1) * 64 * 2 * sizeof(uint16_t), fail);
126
127 for (i = 1; i < 64; i++) {
128 int j = ctx->m.dsp.idct_permutation[ff_zigzag_direct[i]];
129 weight_matrix[j] = ctx->cid_table->luma_weight[i];
130 }
131 ff_convert_matrix(&ctx->m.dsp, ctx->qmatrix_l, ctx->qmatrix_l16, weight_matrix,
132 ctx->m.intra_quant_bias, 1, ctx->m.avctx->qmax, 1);
133 for (i = 1; i < 64; i++) {
134 int j = ctx->m.dsp.idct_permutation[ff_zigzag_direct[i]];
135 weight_matrix[j] = ctx->cid_table->chroma_weight[i];
136 }
137 ff_convert_matrix(&ctx->m.dsp, ctx->qmatrix_c, ctx->qmatrix_c16, weight_matrix,
138 ctx->m.intra_quant_bias, 1, ctx->m.avctx->qmax, 1);
139 for (qscale = 1; qscale <= ctx->m.avctx->qmax; qscale++) {
140 for (i = 0; i < 64; i++) {
141 ctx->qmatrix_l [qscale] [i] <<= 2; ctx->qmatrix_c [qscale] [i] <<= 2;
142 ctx->qmatrix_l16[qscale][0][i] <<= 2; ctx->qmatrix_l16[qscale][1][i] <<= 2;
143 ctx->qmatrix_c16[qscale][0][i] <<= 2; ctx->qmatrix_c16[qscale][1][i] <<= 2;
144 }
145 }
146 return 0;
147 fail:
148 return -1;
149 }
150
151 static int dnxhd_init_rc(DNXHDEncContext *ctx)
152 {
153 FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->mb_rc, 8160*ctx->m.avctx->qmax*sizeof(RCEntry), fail);
154 if (ctx->m.avctx->mb_decision != FF_MB_DECISION_RD)
155 FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->mb_cmp, ctx->m.mb_num*sizeof(RCCMPEntry), fail);
156
157 ctx->frame_bits = (ctx->cid_table->coding_unit_size - 640 - 4 - ctx->min_padding) * 8;
158 ctx->qscale = 1;
159 ctx->lambda = 2<<LAMBDA_FRAC_BITS; // qscale 2
160 return 0;
161 fail:
162 return -1;
163 }
164
165 static int dnxhd_encode_init(AVCodecContext *avctx)
166 {
167 DNXHDEncContext *ctx = avctx->priv_data;
168 int i, index;
169
170 ctx->cid = ff_dnxhd_find_cid(avctx);
171 if (!ctx->cid || avctx->pix_fmt != PIX_FMT_YUV422P) {
172 av_log(avctx, AV_LOG_ERROR, "video parameters incompatible with DNxHD\n");
173 return -1;
174 }
175 av_log(avctx, AV_LOG_DEBUG, "cid %d\n", ctx->cid);
176
177 index = ff_dnxhd_get_cid_table(ctx->cid);
178 ctx->cid_table = &ff_dnxhd_cid_table[index];
179
180 ctx->m.avctx = avctx;
181 ctx->m.mb_intra = 1;
182 ctx->m.h263_aic = 1;
183
184 ctx->get_pixels_8x4_sym = dnxhd_get_pixels_8x4;
185
186 dsputil_init(&ctx->m.dsp, avctx);
187 ff_dct_common_init(&ctx->m);
188 #if HAVE_MMX
189 ff_dnxhd_init_mmx(ctx);
190 #endif
191 if (!ctx->m.dct_quantize)
192 ctx->m.dct_quantize = dct_quantize_c;
193
194 ctx->m.mb_height = (avctx->height + 15) / 16;
195 ctx->m.mb_width = (avctx->width + 15) / 16;
196
197 if (avctx->flags & CODEC_FLAG_INTERLACED_DCT) {
198 ctx->interlaced = 1;
199 ctx->m.mb_height /= 2;
200 }
201
202 ctx->m.mb_num = ctx->m.mb_height * ctx->m.mb_width;
203
204 if (avctx->intra_quant_bias != FF_DEFAULT_QUANT_BIAS)
205 ctx->m.intra_quant_bias = avctx->intra_quant_bias;
206 if (dnxhd_init_qmat(ctx, ctx->m.intra_quant_bias, 0) < 0) // XXX tune lbias/cbias
207 return -1;
208
209 // Avid Nitris hardware decoder requires a minimum amount of padding in the coding unit payload
210 if (ctx->nitris_compat)
211 ctx->min_padding = 1600;
212
213 if (dnxhd_init_vlc(ctx) < 0)
214 return -1;
215 if (dnxhd_init_rc(ctx) < 0)
216 return -1;
217
218 FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->slice_size, ctx->m.mb_height*sizeof(uint32_t), fail);
219 FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->slice_offs, ctx->m.mb_height*sizeof(uint32_t), fail);
220 FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->mb_bits, ctx->m.mb_num *sizeof(uint16_t), fail);
221 FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->mb_qscale, ctx->m.mb_num *sizeof(uint8_t), fail);
222
223 ctx->frame.key_frame = 1;
224 ctx->frame.pict_type = AV_PICTURE_TYPE_I;
225 ctx->m.avctx->coded_frame = &ctx->frame;
226
227 if (avctx->thread_count > MAX_THREADS) {
228 av_log(avctx, AV_LOG_ERROR, "too many threads\n");
229 return -1;
230 }
231
232 ctx->thread[0] = ctx;
233 for (i = 1; i < avctx->thread_count; i++) {
234 ctx->thread[i] = av_malloc(sizeof(DNXHDEncContext));
235 memcpy(ctx->thread[i], ctx, sizeof(DNXHDEncContext));
236 }
237
238 return 0;
239 fail: //for FF_ALLOCZ_OR_GOTO
240 return -1;
241 }
242
243 static int dnxhd_write_header(AVCodecContext *avctx, uint8_t *buf)
244 {
245 DNXHDEncContext *ctx = avctx->priv_data;
246 const uint8_t header_prefix[5] = { 0x00,0x00,0x02,0x80,0x01 };
247
248 memset(buf, 0, 640);
249
250 memcpy(buf, header_prefix, 5);
251 buf[5] = ctx->interlaced ? ctx->cur_field+2 : 0x01;
252 buf[6] = 0x80; // crc flag off
253 buf[7] = 0xa0; // reserved
254 AV_WB16(buf + 0x18, avctx->height>>ctx->interlaced); // ALPF
255 AV_WB16(buf + 0x1a, avctx->width); // SPL
256 AV_WB16(buf + 0x1d, avctx->height>>ctx->interlaced); // NAL
257
258 buf[0x21] = 0x38; // FIXME 8 bit per comp
259 buf[0x22] = 0x88 + (ctx->interlaced<<2);
260 AV_WB32(buf + 0x28, ctx->cid); // CID
261 buf[0x2c] = ctx->interlaced ? 0 : 0x80;
262
263 buf[0x5f] = 0x01; // UDL
264
265 buf[0x167] = 0x02; // reserved
266 AV_WB16(buf + 0x16a, ctx->m.mb_height * 4 + 4); // MSIPS
267 buf[0x16d] = ctx->m.mb_height; // Ns
268 buf[0x16f] = 0x10; // reserved
269
270 ctx->msip = buf + 0x170;
271 return 0;
272 }
273
274 static av_always_inline void dnxhd_encode_dc(DNXHDEncContext *ctx, int diff)
275 {
276 int nbits;
277 if (diff < 0) {
278 nbits = av_log2_16bit(-2*diff);
279 diff--;
280 } else {
281 nbits = av_log2_16bit(2*diff);
282 }
283 put_bits(&ctx->m.pb, ctx->cid_table->dc_bits[nbits] + nbits,
284 (ctx->cid_table->dc_codes[nbits]<<nbits) + (diff & ((1 << nbits) - 1)));
285 }
286
287 static av_always_inline void dnxhd_encode_block(DNXHDEncContext *ctx, DCTELEM *block, int last_index, int n)
288 {
289 int last_non_zero = 0;
290 int slevel, i, j;
291
292 dnxhd_encode_dc(ctx, block[0] - ctx->m.last_dc[n]);
293 ctx->m.last_dc[n] = block[0];
294
295 for (i = 1; i <= last_index; i++) {
296 j = ctx->m.intra_scantable.permutated[i];
297 slevel = block[j];
298 if (slevel) {
299 int run_level = i - last_non_zero - 1;
300 int rlevel = (slevel<<1)|!!run_level;
301 put_bits(&ctx->m.pb, ctx->vlc_bits[rlevel], ctx->vlc_codes[rlevel]);
302 if (run_level)
303 put_bits(&ctx->m.pb, ctx->run_bits[run_level], ctx->run_codes[run_level]);
304 last_non_zero = i;
305 }
306 }
307 put_bits(&ctx->m.pb, ctx->vlc_bits[0], ctx->vlc_codes[0]); // EOB
308 }
309
310 static av_always_inline void dnxhd_unquantize_c(DNXHDEncContext *ctx, DCTELEM *block, int n, int qscale, int last_index)
311 {
312 const uint8_t *weight_matrix;
313 int level;
314 int i;
315
316 weight_matrix = (n&2) ? ctx->cid_table->chroma_weight : ctx->cid_table->luma_weight;
317
318 for (i = 1; i <= last_index; i++) {
319 int j = ctx->m.intra_scantable.permutated[i];
320 level = block[j];
321 if (level) {
322 if (level < 0) {
323 level = (1-2*level) * qscale * weight_matrix[i];
324 if (weight_matrix[i] != 32)
325 level += 32;
326 level >>= 6;
327 level = -level;
328 } else {
329 level = (2*level+1) * qscale * weight_matrix[i];
330 if (weight_matrix[i] != 32)
331 level += 32;
332 level >>= 6;
333 }
334 block[j] = level;
335 }
336 }
337 }
338
339 static av_always_inline int dnxhd_ssd_block(DCTELEM *qblock, DCTELEM *block)
340 {
341 int score = 0;
342 int i;
343 for (i = 0; i < 64; i++)
344 score += (block[i] - qblock[i]) * (block[i] - qblock[i]);
345 return score;
346 }
347
348 static av_always_inline int dnxhd_calc_ac_bits(DNXHDEncContext *ctx, DCTELEM *block, int last_index)
349 {
350 int last_non_zero = 0;
351 int bits = 0;
352 int i, j, level;
353 for (i = 1; i <= last_index; i++) {
354 j = ctx->m.intra_scantable.permutated[i];
355 level = block[j];
356 if (level) {
357 int run_level = i - last_non_zero - 1;
358 bits += ctx->vlc_bits[(level<<1)|!!run_level]+ctx->run_bits[run_level];
359 last_non_zero = i;
360 }
361 }
362 return bits;
363 }
364
365 static av_always_inline void dnxhd_get_blocks(DNXHDEncContext *ctx, int mb_x, int mb_y)
366 {
367 const uint8_t *ptr_y = ctx->thread[0]->src[0] + ((mb_y << 4) * ctx->m.linesize) + (mb_x << 4);
368 const uint8_t *ptr_u = ctx->thread[0]->src[1] + ((mb_y << 4) * ctx->m.uvlinesize) + (mb_x << 3);
369 const uint8_t *ptr_v = ctx->thread[0]->src[2] + ((mb_y << 4) * ctx->m.uvlinesize) + (mb_x << 3);
370 DSPContext *dsp = &ctx->m.dsp;
371
372 dsp->get_pixels(ctx->blocks[0], ptr_y, ctx->m.linesize);
373 dsp->get_pixels(ctx->blocks[1], ptr_y + 8, ctx->m.linesize);
374 dsp->get_pixels(ctx->blocks[2], ptr_u, ctx->m.uvlinesize);
375 dsp->get_pixels(ctx->blocks[3], ptr_v, ctx->m.uvlinesize);
376
377 if (mb_y+1 == ctx->m.mb_height && ctx->m.avctx->height == 1080) {
378 if (ctx->interlaced) {
379 ctx->get_pixels_8x4_sym(ctx->blocks[4], ptr_y + ctx->dct_y_offset, ctx->m.linesize);
380 ctx->get_pixels_8x4_sym(ctx->blocks[5], ptr_y + ctx->dct_y_offset + 8, ctx->m.linesize);
381 ctx->get_pixels_8x4_sym(ctx->blocks[6], ptr_u + ctx->dct_uv_offset, ctx->m.uvlinesize);
382 ctx->get_pixels_8x4_sym(ctx->blocks[7], ptr_v + ctx->dct_uv_offset, ctx->m.uvlinesize);
383 } else {
384 dsp->clear_block(ctx->blocks[4]);
385 dsp->clear_block(ctx->blocks[5]);
386 dsp->clear_block(ctx->blocks[6]);
387 dsp->clear_block(ctx->blocks[7]);
388 }
389 } else {
390 dsp->get_pixels(ctx->blocks[4], ptr_y + ctx->dct_y_offset, ctx->m.linesize);
391 dsp->get_pixels(ctx->blocks[5], ptr_y + ctx->dct_y_offset + 8, ctx->m.linesize);
392 dsp->get_pixels(ctx->blocks[6], ptr_u + ctx->dct_uv_offset, ctx->m.uvlinesize);
393 dsp->get_pixels(ctx->blocks[7], ptr_v + ctx->dct_uv_offset, ctx->m.uvlinesize);
394 }
395 }
396
397 static av_always_inline int dnxhd_switch_matrix(DNXHDEncContext *ctx, int i)
398 {
399 if (i&2) {
400 ctx->m.q_intra_matrix16 = ctx->qmatrix_c16;
401 ctx->m.q_intra_matrix = ctx->qmatrix_c;
402 return 1 + (i&1);
403 } else {
404 ctx->m.q_intra_matrix16 = ctx->qmatrix_l16;
405 ctx->m.q_intra_matrix = ctx->qmatrix_l;
406 return 0;
407 }
408 }
409
410 static int dnxhd_calc_bits_thread(AVCodecContext *avctx, void *arg, int jobnr, int threadnr)
411 {
412 DNXHDEncContext *ctx = avctx->priv_data;
413 int mb_y = jobnr, mb_x;
414 int qscale = ctx->qscale;
415 LOCAL_ALIGNED_16(DCTELEM, block, [64]);
416 ctx = ctx->thread[threadnr];
417
418 ctx->m.last_dc[0] =
419 ctx->m.last_dc[1] =
420 ctx->m.last_dc[2] = 1024;
421
422 for (mb_x = 0; mb_x < ctx->m.mb_width; mb_x++) {
423 unsigned mb = mb_y * ctx->m.mb_width + mb_x;
424 int ssd = 0;
425 int ac_bits = 0;
426 int dc_bits = 0;
427 int i;
428
429 dnxhd_get_blocks(ctx, mb_x, mb_y);
430
431 for (i = 0; i < 8; i++) {
432 DCTELEM *src_block = ctx->blocks[i];
433 int overflow, nbits, diff, last_index;
434 int n = dnxhd_switch_matrix(ctx, i);
435
436 memcpy(block, src_block, 64*sizeof(*block));
437 last_index = ctx->m.dct_quantize(&ctx->m, block, i, qscale, &overflow);
438 ac_bits += dnxhd_calc_ac_bits(ctx, block, last_index);
439
440 diff = block[0] - ctx->m.last_dc[n];
441 if (diff < 0) nbits = av_log2_16bit(-2*diff);
442 else nbits = av_log2_16bit( 2*diff);
443 dc_bits += ctx->cid_table->dc_bits[nbits] + nbits;
444
445 ctx->m.last_dc[n] = block[0];
446
447 if (avctx->mb_decision == FF_MB_DECISION_RD || !RC_VARIANCE) {
448 dnxhd_unquantize_c(ctx, block, i, qscale, last_index);
449 ctx->m.dsp.idct(block);
450 ssd += dnxhd_ssd_block(block, src_block);
451 }
452 }
453 ctx->mb_rc[qscale][mb].ssd = ssd;
454 ctx->mb_rc[qscale][mb].bits = ac_bits+dc_bits+12+8*ctx->vlc_bits[0];
455 }
456 return 0;
457 }
458
459 static int dnxhd_encode_thread(AVCodecContext *avctx, void *arg, int jobnr, int threadnr)
460 {
461 DNXHDEncContext *ctx = avctx->priv_data;
462 int mb_y = jobnr, mb_x;
463 ctx = ctx->thread[threadnr];
464 init_put_bits(&ctx->m.pb, (uint8_t *)arg + 640 + ctx->slice_offs[jobnr], ctx->slice_size[jobnr]);
465
466 ctx->m.last_dc[0] =
467 ctx->m.last_dc[1] =
468 ctx->m.last_dc[2] = 1024;
469 for (mb_x = 0; mb_x < ctx->m.mb_width; mb_x++) {
470 unsigned mb = mb_y * ctx->m.mb_width + mb_x;
471 int qscale = ctx->mb_qscale[mb];
472 int i;
473
474 put_bits(&ctx->m.pb, 12, qscale<<1);
475
476 dnxhd_get_blocks(ctx, mb_x, mb_y);
477
478 for (i = 0; i < 8; i++) {
479 DCTELEM *block = ctx->blocks[i];
480 int last_index, overflow;
481 int n = dnxhd_switch_matrix(ctx, i);
482 last_index = ctx->m.dct_quantize(&ctx->m, block, i, qscale, &overflow);
483 //START_TIMER;
484 dnxhd_encode_block(ctx, block, last_index, n);
485 //STOP_TIMER("encode_block");
486 }
487 }
488 if (put_bits_count(&ctx->m.pb)&31)
489 put_bits(&ctx->m.pb, 32-(put_bits_count(&ctx->m.pb)&31), 0);
490 flush_put_bits(&ctx->m.pb);
491 return 0;
492 }
493
494 static void dnxhd_setup_threads_slices(DNXHDEncContext *ctx)
495 {
496 int mb_y, mb_x;
497 int offset = 0;
498 for (mb_y = 0; mb_y < ctx->m.mb_height; mb_y++) {
499 int thread_size;
500 ctx->slice_offs[mb_y] = offset;
501 ctx->slice_size[mb_y] = 0;
502 for (mb_x = 0; mb_x < ctx->m.mb_width; mb_x++) {
503 unsigned mb = mb_y * ctx->m.mb_width + mb_x;
504 ctx->slice_size[mb_y] += ctx->mb_bits[mb];
505 }
506 ctx->slice_size[mb_y] = (ctx->slice_size[mb_y]+31)&~31;
507 ctx->slice_size[mb_y] >>= 3;
508 thread_size = ctx->slice_size[mb_y];
509 offset += thread_size;
510 }
511 }
512
513 static int dnxhd_mb_var_thread(AVCodecContext *avctx, void *arg, int jobnr, int threadnr)
514 {
515 DNXHDEncContext *ctx = avctx->priv_data;
516 int mb_y = jobnr, mb_x;
517 ctx = ctx->thread[threadnr];
518 for (mb_x = 0; mb_x < ctx->m.mb_width; mb_x++) {
519 unsigned mb = mb_y * ctx->m.mb_width + mb_x;
520 uint8_t *pix = ctx->thread[0]->src[0] + ((mb_y<<4) * ctx->m.linesize) + (mb_x<<4);
521 int sum = ctx->m.dsp.pix_sum(pix, ctx->m.linesize);
522 int varc = (ctx->m.dsp.pix_norm1(pix, ctx->m.linesize) - (((unsigned)(sum*sum))>>8)+128)>>8;
523 ctx->mb_cmp[mb].value = varc;
524 ctx->mb_cmp[mb].mb = mb;
525 }
526 return 0;
527 }
528
529 static int dnxhd_encode_rdo(AVCodecContext *avctx, DNXHDEncContext *ctx)
530 {
531 int lambda, up_step, down_step;
532 int last_lower = INT_MAX, last_higher = 0;
533 int x, y, q;
534
535 for (q = 1; q < avctx->qmax; q++) {
536 ctx->qscale = q;
537 avctx->execute2(avctx, dnxhd_calc_bits_thread, NULL, NULL, ctx->m.mb_height);
538 }
539 up_step = down_step = 2<<LAMBDA_FRAC_BITS;
540 lambda = ctx->lambda;
541
542 for (;;) {
543 int bits = 0;
544 int end = 0;
545 if (lambda == last_higher) {
546 lambda++;
547 end = 1; // need to set final qscales/bits
548 }
549 for (y = 0; y < ctx->m.mb_height; y++) {
550 for (x = 0; x < ctx->m.mb_width; x++) {
551 unsigned min = UINT_MAX;
552 int qscale = 1;
553 int mb = y*ctx->m.mb_width+x;
554 for (q = 1; q < avctx->qmax; q++) {
555 unsigned score = ctx->mb_rc[q][mb].bits*lambda+(ctx->mb_rc[q][mb].ssd<<LAMBDA_FRAC_BITS);
556 if (score < min) {
557 min = score;
558 qscale = q;
559 }
560 }
561 bits += ctx->mb_rc[qscale][mb].bits;
562 ctx->mb_qscale[mb] = qscale;
563 ctx->mb_bits[mb] = ctx->mb_rc[qscale][mb].bits;
564 }
565 bits = (bits+31)&~31; // padding
566 if (bits > ctx->frame_bits)
567 break;
568 }
569 //av_dlog(ctx->m.avctx, "lambda %d, up %u, down %u, bits %d, frame %d\n",
570 // lambda, last_higher, last_lower, bits, ctx->frame_bits);
571 if (end) {
572 if (bits > ctx->frame_bits)
573 return -1;
574 break;
575 }
576 if (bits < ctx->frame_bits) {
577 last_lower = FFMIN(lambda, last_lower);
578 if (last_higher != 0)
579 lambda = (lambda+last_higher)>>1;
580 else
581 lambda -= down_step;
582 down_step *= 5; // XXX tune ?
583 up_step = 1<<LAMBDA_FRAC_BITS;
584 lambda = FFMAX(1, lambda);
585 if (lambda == last_lower)
586 break;
587 } else {
588 last_higher = FFMAX(lambda, last_higher);
589 if (last_lower != INT_MAX)
590 lambda = (lambda+last_lower)>>1;
591 else if ((int64_t)lambda + up_step > INT_MAX)
592 return -1;
593 else
594 lambda += up_step;
595 up_step = FFMIN((int64_t)up_step*5, INT_MAX);
596 down_step = 1<<LAMBDA_FRAC_BITS;
597 }
598 }
599 //av_dlog(ctx->m.avctx, "out lambda %d\n", lambda);
600 ctx->lambda = lambda;
601 return 0;
602 }
603
604 static int dnxhd_find_qscale(DNXHDEncContext *ctx)
605 {
606 int bits = 0;
607 int up_step = 1;
608 int down_step = 1;
609 int last_higher = 0;
610 int last_lower = INT_MAX;
611 int qscale;
612 int x, y;
613
614 qscale = ctx->qscale;
615 for (;;) {
616 bits = 0;
617 ctx->qscale = qscale;
618 // XXX avoid recalculating bits
619 ctx->m.avctx->execute2(ctx->m.avctx, dnxhd_calc_bits_thread, NULL, NULL, ctx->m.mb_height);
620 for (y = 0; y < ctx->m.mb_height; y++) {
621 for (x = 0; x < ctx->m.mb_width; x++)
622 bits += ctx->mb_rc[qscale][y*ctx->m.mb_width+x].bits;
623 bits = (bits+31)&~31; // padding
624 if (bits > ctx->frame_bits)
625 break;
626 }
627 //av_dlog(ctx->m.avctx, "%d, qscale %d, bits %d, frame %d, higher %d, lower %d\n",
628 // ctx->m.avctx->frame_number, qscale, bits, ctx->frame_bits, last_higher, last_lower);
629 if (bits < ctx->frame_bits) {
630 if (qscale == 1)
631 return 1;
632 if (last_higher == qscale - 1) {
633 qscale = last_higher;
634 break;
635 }
636 last_lower = FFMIN(qscale, last_lower);
637 if (last_higher != 0)
638 qscale = (qscale+last_higher)>>1;
639 else
640 qscale -= down_step++;
641 if (qscale < 1)
642 qscale = 1;
643 up_step = 1;
644 } else {
645 if (last_lower == qscale + 1)
646 break;
647 last_higher = FFMAX(qscale, last_higher);
648 if (last_lower != INT_MAX)
649 qscale = (qscale+last_lower)>>1;
650 else
651 qscale += up_step++;
652 down_step = 1;
653 if (qscale >= ctx->m.avctx->qmax)
654 return -1;
655 }
656 }
657 //av_dlog(ctx->m.avctx, "out qscale %d\n", qscale);
658 ctx->qscale = qscale;
659 return 0;
660 }
661
662 #define BUCKET_BITS 8
663 #define RADIX_PASSES 4
664 #define NBUCKETS (1 << BUCKET_BITS)
665
666 static inline int get_bucket(int value, int shift)
667 {
668 value >>= shift;
669 value &= NBUCKETS - 1;
670 return NBUCKETS - 1 - value;
671 }
672
673 static void radix_count(const RCCMPEntry *data, int size, int buckets[RADIX_PASSES][NBUCKETS])
674 {
675 int i, j;
676 memset(buckets, 0, sizeof(buckets[0][0]) * RADIX_PASSES * NBUCKETS);
677 for (i = 0; i < size; i++) {
678 int v = data[i].value;
679 for (j = 0; j < RADIX_PASSES; j++) {
680 buckets[j][get_bucket(v, 0)]++;
681 v >>= BUCKET_BITS;
682 }
683 assert(!v);
684 }
685 for (j = 0; j < RADIX_PASSES; j++) {
686 int offset = size;
687 for (i = NBUCKETS - 1; i >= 0; i--)
688 buckets[j][i] = offset -= buckets[j][i];
689 assert(!buckets[j][0]);
690 }
691 }
692
693 static void radix_sort_pass(RCCMPEntry *dst, const RCCMPEntry *data, int size, int buckets[NBUCKETS], int pass)
694 {
695 int shift = pass * BUCKET_BITS;
696 int i;
697 for (i = 0; i < size; i++) {
698 int v = get_bucket(data[i].value, shift);
699 int pos = buckets[v]++;
700 dst[pos] = data[i];
701 }
702 }
703
704 static void radix_sort(RCCMPEntry *data, int size)
705 {
706 int buckets[RADIX_PASSES][NBUCKETS];
707 RCCMPEntry *tmp = av_malloc(sizeof(*tmp) * size);
708 radix_count(data, size, buckets);
709 radix_sort_pass(tmp, data, size, buckets[0], 0);
710 radix_sort_pass(data, tmp, size, buckets[1], 1);
711 if (buckets[2][NBUCKETS - 1] || buckets[3][NBUCKETS - 1]) {
712 radix_sort_pass(tmp, data, size, buckets[2], 2);
713 radix_sort_pass(data, tmp, size, buckets[3], 3);
714 }
715 av_free(tmp);
716 }
717
718 static int dnxhd_encode_fast(AVCodecContext *avctx, DNXHDEncContext *ctx)
719 {
720 int max_bits = 0;
721 int ret, x, y;
722 if ((ret = dnxhd_find_qscale(ctx)) < 0)
723 return -1;
724 for (y = 0; y < ctx->m.mb_height; y++) {
725 for (x = 0; x < ctx->m.mb_width; x++) {
726 int mb = y*ctx->m.mb_width+x;
727 int delta_bits;
728 ctx->mb_qscale[mb] = ctx->qscale;
729 ctx->mb_bits[mb] = ctx->mb_rc[ctx->qscale][mb].bits;
730 max_bits += ctx->mb_rc[ctx->qscale][mb].bits;
731 if (!RC_VARIANCE) {
732 delta_bits = ctx->mb_rc[ctx->qscale][mb].bits-ctx->mb_rc[ctx->qscale+1][mb].bits;
733 ctx->mb_cmp[mb].mb = mb;
734 ctx->mb_cmp[mb].value = delta_bits ?
735 ((ctx->mb_rc[ctx->qscale][mb].ssd-ctx->mb_rc[ctx->qscale+1][mb].ssd)*100)/delta_bits
736 : INT_MIN; //avoid increasing qscale
737 }
738 }
739 max_bits += 31; //worst padding
740 }
741 if (!ret) {
742 if (RC_VARIANCE)
743 avctx->execute2(avctx, dnxhd_mb_var_thread, NULL, NULL, ctx->m.mb_height);
744 radix_sort(ctx->mb_cmp, ctx->m.mb_num);
745 for (x = 0; x < ctx->m.mb_num && max_bits > ctx->frame_bits; x++) {
746 int mb = ctx->mb_cmp[x].mb;
747 max_bits -= ctx->mb_rc[ctx->qscale][mb].bits - ctx->mb_rc[ctx->qscale+1][mb].bits;
748 ctx->mb_qscale[mb] = ctx->qscale+1;
749 ctx->mb_bits[mb] = ctx->mb_rc[ctx->qscale+1][mb].bits;
750 }
751 }
752 return 0;
753 }
754
755 static void dnxhd_load_picture(DNXHDEncContext *ctx, const AVFrame *frame)
756 {
757 int i;
758
759 for (i = 0; i < 3; i++) {
760 ctx->frame.data[i] = frame->data[i];
761 ctx->frame.linesize[i] = frame->linesize[i];
762 }
763
764 for (i = 0; i < ctx->m.avctx->thread_count; i++) {
765 ctx->thread[i]->m.linesize = ctx->frame.linesize[0]<<ctx->interlaced;
766 ctx->thread[i]->m.uvlinesize = ctx->frame.linesize[1]<<ctx->interlaced;
767 ctx->thread[i]->dct_y_offset = ctx->m.linesize *8;
768 ctx->thread[i]->dct_uv_offset = ctx->m.uvlinesize*8;
769 }
770
771 ctx->frame.interlaced_frame = frame->interlaced_frame;
772 ctx->cur_field = frame->interlaced_frame && !frame->top_field_first;
773 }
774
775 static int dnxhd_encode_picture(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data)
776 {
777 DNXHDEncContext *ctx = avctx->priv_data;
778 int first_field = 1;
779 int offset, i, ret;
780
781 if (buf_size < ctx->cid_table->frame_size) {
782 av_log(avctx, AV_LOG_ERROR, "output buffer is too small to compress picture\n");
783 return -1;
784 }
785
786 dnxhd_load_picture(ctx, data);
787
788 encode_coding_unit:
789 for (i = 0; i < 3; i++) {
790 ctx->src[i] = ctx->frame.data[i];
791 if (ctx->interlaced && ctx->cur_field)
792 ctx->src[i] += ctx->frame.linesize[i];
793 }
794
795 dnxhd_write_header(avctx, buf);
796
797 if (avctx->mb_decision == FF_MB_DECISION_RD)
798 ret = dnxhd_encode_rdo(avctx, ctx);
799 else
800 ret = dnxhd_encode_fast(avctx, ctx);
801 if (ret < 0) {
802 av_log(avctx, AV_LOG_ERROR,
803 "picture could not fit ratecontrol constraints, increase qmax\n");
804 return -1;
805 }
806
807 dnxhd_setup_threads_slices(ctx);
808
809 offset = 0;
810 for (i = 0; i < ctx->m.mb_height; i++) {
811 AV_WB32(ctx->msip + i * 4, offset);
812 offset += ctx->slice_size[i];
813 assert(!(ctx->slice_size[i] & 3));
814 }
815
816 avctx->execute2(avctx, dnxhd_encode_thread, buf, NULL, ctx->m.mb_height);
817
818 assert(640 + offset + 4 <= ctx->cid_table->coding_unit_size);
819 memset(buf + 640 + offset, 0, ctx->cid_table->coding_unit_size - 4 - offset - 640);
820
821 AV_WB32(buf + ctx->cid_table->coding_unit_size - 4, 0x600DC0DE); // EOF
822
823 if (ctx->interlaced && first_field) {
824 first_field = 0;
825 ctx->cur_field ^= 1;
826 buf += ctx->cid_table->coding_unit_size;
827 buf_size -= ctx->cid_table->coding_unit_size;
828 goto encode_coding_unit;
829 }
830
831 ctx->frame.quality = ctx->qscale*FF_QP2LAMBDA;
832
833 return ctx->cid_table->frame_size;
834 }
835
836 static int dnxhd_encode_end(AVCodecContext *avctx)
837 {
838 DNXHDEncContext *ctx = avctx->priv_data;
839 int max_level = 1<<(ctx->cid_table->bit_depth+2);
840 int i;
841
842 av_free(ctx->vlc_codes-max_level*2);
843 av_free(ctx->vlc_bits -max_level*2);
844 av_freep(&ctx->run_codes);
845 av_freep(&ctx->run_bits);
846
847 av_freep(&ctx->mb_bits);
848 av_freep(&ctx->mb_qscale);
849 av_freep(&ctx->mb_rc);
850 av_freep(&ctx->mb_cmp);
851 av_freep(&ctx->slice_size);
852 av_freep(&ctx->slice_offs);
853
854 av_freep(&ctx->qmatrix_c);
855 av_freep(&ctx->qmatrix_l);
856 av_freep(&ctx->qmatrix_c16);
857 av_freep(&ctx->qmatrix_l16);
858
859 for (i = 1; i < avctx->thread_count; i++)
860 av_freep(&ctx->thread[i]);
861
862 return 0;
863 }
864
865 AVCodec ff_dnxhd_encoder = {
866 "dnxhd",
867 AVMEDIA_TYPE_VIDEO,
868 CODEC_ID_DNXHD,
869 sizeof(DNXHDEncContext),
870 dnxhd_encode_init,
871 dnxhd_encode_picture,
872 dnxhd_encode_end,
873 .capabilities = CODEC_CAP_SLICE_THREADS,
874 .pix_fmts = (const enum PixelFormat[]){PIX_FMT_YUV422P, PIX_FMT_NONE},
875 .long_name = NULL_IF_CONFIG_SMALL("VC3/DNxHD"),
876 .priv_class = &class,
877 };