comparison dnxhdenc.c @ 5790:c3f2a6425d2d libavcodec

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