3 * Copyright (c) 2007 Baptiste Coudurier <baptiste dot coudurier at smartjog dot com>
4 * Copyright (c) 2011 MirriAd Ltd
6 * VC-3 encoder funded by the British Broadcasting Corporation
7 * 10 bit support added by MirriAd Ltd, Joseph Artsimovich <joseph@mirriad.com>
9 * This file is part of Libav.
11 * Libav is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * Libav is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with Libav; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #define RC_VARIANCE 1 // use variance or ssd for fast rc
29 #include "libavutil/internal.h"
30 #include "libavutil/opt.h"
34 #include "mpegvideo.h"
37 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
38 #define DNX10BIT_QMAT_SHIFT 18 // The largest value that will not lead to overflow for 10bit samples.
40 static const AVOption options
[]={
41 {"nitris_compat", "encode with Avid Nitris compatibility", offsetof(DNXHDEncContext
, nitris_compat
), AV_OPT_TYPE_INT
, {.i64
= 0}, 0, 1, VE
},
44 static const AVClass
class = { "dnxhd", av_default_item_name
, options
, LIBAVUTIL_VERSION_INT
};
46 #define LAMBDA_FRAC_BITS 10
48 static void dnxhd_8bit_get_pixels_8x4_sym(int16_t *restrict block
, const uint8_t *pixels
, int line_size
)
51 for (i
= 0; i
< 4; i
++) {
52 block
[0] = pixels
[0]; block
[1] = pixels
[1];
53 block
[2] = pixels
[2]; block
[3] = pixels
[3];
54 block
[4] = pixels
[4]; block
[5] = pixels
[5];
55 block
[6] = pixels
[6]; block
[7] = pixels
[7];
59 memcpy(block
, block
- 8, sizeof(*block
) * 8);
60 memcpy(block
+ 8, block
- 16, sizeof(*block
) * 8);
61 memcpy(block
+ 16, block
- 24, sizeof(*block
) * 8);
62 memcpy(block
+ 24, block
- 32, sizeof(*block
) * 8);
65 static av_always_inline
void dnxhd_10bit_get_pixels_8x4_sym(int16_t *restrict block
, const uint8_t *pixels
, int line_size
)
71 for (i
= 0; i
< 4; i
++) {
72 memcpy(block
+ i
* 8, pixels
+ i
* line_size
, 8 * sizeof(*block
));
73 memcpy(block
- (i
+1) * 8, pixels
+ i
* line_size
, 8 * sizeof(*block
));
77 static int dnxhd_10bit_dct_quantize(MpegEncContext
*ctx
, int16_t *block
,
78 int n
, int qscale
, int *overflow
)
80 const uint8_t *scantable
= ctx
->intra_scantable
.scantable
;
81 const int *qmat
= ctx
->q_intra_matrix
[qscale
];
82 int last_non_zero
= 0;
87 // Divide by 4 with rounding, to compensate scaling of DCT coefficients
88 block
[0] = (block
[0] + 2) >> 2;
90 for (i
= 1; i
< 64; ++i
) {
92 int sign
= block
[j
] >> 31;
93 int level
= (block
[j
] ^ sign
) - sign
;
94 level
= level
* qmat
[j
] >> DNX10BIT_QMAT_SHIFT
;
95 block
[j
] = (level
^ sign
) - sign
;
100 return last_non_zero
;
103 static int dnxhd_init_vlc(DNXHDEncContext
*ctx
)
105 int i
, j
, level
, run
;
106 int max_level
= 1<<(ctx
->cid_table
->bit_depth
+2);
108 FF_ALLOCZ_OR_GOTO(ctx
->m
.avctx
, ctx
->vlc_codes
, max_level
*4*sizeof(*ctx
->vlc_codes
), fail
);
109 FF_ALLOCZ_OR_GOTO(ctx
->m
.avctx
, ctx
->vlc_bits
, max_level
*4*sizeof(*ctx
->vlc_bits
) , fail
);
110 FF_ALLOCZ_OR_GOTO(ctx
->m
.avctx
, ctx
->run_codes
, 63*2, fail
);
111 FF_ALLOCZ_OR_GOTO(ctx
->m
.avctx
, ctx
->run_bits
, 63, fail
);
113 ctx
->vlc_codes
+= max_level
*2;
114 ctx
->vlc_bits
+= max_level
*2;
115 for (level
= -max_level
; level
< max_level
; level
++) {
116 for (run
= 0; run
< 2; run
++) {
117 int index
= (level
<<1)|run
;
118 int sign
, offset
= 0, alevel
= level
;
120 MASK_ABS(sign
, alevel
);
122 offset
= (alevel
-1)>>6;
125 for (j
= 0; j
< 257; j
++) {
126 if (ctx
->cid_table
->ac_level
[j
] == alevel
&&
127 (!offset
|| (ctx
->cid_table
->ac_index_flag
[j
] && offset
)) &&
128 (!run
|| (ctx
->cid_table
->ac_run_flag
[j
] && run
))) {
129 assert(!ctx
->vlc_codes
[index
]);
131 ctx
->vlc_codes
[index
] = (ctx
->cid_table
->ac_codes
[j
]<<1)|(sign
&1);
132 ctx
->vlc_bits
[index
] = ctx
->cid_table
->ac_bits
[j
]+1;
134 ctx
->vlc_codes
[index
] = ctx
->cid_table
->ac_codes
[j
];
135 ctx
->vlc_bits
[index
] = ctx
->cid_table
->ac_bits
[j
];
140 assert(!alevel
|| j
< 257);
142 ctx
->vlc_codes
[index
] = (ctx
->vlc_codes
[index
]<<ctx
->cid_table
->index_bits
)|offset
;
143 ctx
->vlc_bits
[index
]+= ctx
->cid_table
->index_bits
;
147 for (i
= 0; i
< 62; i
++) {
148 int run
= ctx
->cid_table
->run
[i
];
150 ctx
->run_codes
[run
] = ctx
->cid_table
->run_codes
[i
];
151 ctx
->run_bits
[run
] = ctx
->cid_table
->run_bits
[i
];
158 static int dnxhd_init_qmat(DNXHDEncContext
*ctx
, int lbias
, int cbias
)
160 // init first elem to 1 to avoid div by 0 in convert_matrix
161 uint16_t weight_matrix
[64] = {1,}; // convert_matrix needs uint16_t*
163 const uint8_t *luma_weight_table
= ctx
->cid_table
->luma_weight
;
164 const uint8_t *chroma_weight_table
= ctx
->cid_table
->chroma_weight
;
166 FF_ALLOCZ_OR_GOTO(ctx
->m
.avctx
, ctx
->qmatrix_l
, (ctx
->m
.avctx
->qmax
+1) * 64 * sizeof(int), fail
);
167 FF_ALLOCZ_OR_GOTO(ctx
->m
.avctx
, ctx
->qmatrix_c
, (ctx
->m
.avctx
->qmax
+1) * 64 * sizeof(int), fail
);
168 FF_ALLOCZ_OR_GOTO(ctx
->m
.avctx
, ctx
->qmatrix_l16
, (ctx
->m
.avctx
->qmax
+1) * 64 * 2 * sizeof(uint16_t), fail
);
169 FF_ALLOCZ_OR_GOTO(ctx
->m
.avctx
, ctx
->qmatrix_c16
, (ctx
->m
.avctx
->qmax
+1) * 64 * 2 * sizeof(uint16_t), fail
);
171 if (ctx
->cid_table
->bit_depth
== 8) {
172 for (i
= 1; i
< 64; i
++) {
173 int j
= ctx
->m
.dsp
.idct_permutation
[ff_zigzag_direct
[i
]];
174 weight_matrix
[j
] = ctx
->cid_table
->luma_weight
[i
];
176 ff_convert_matrix(&ctx
->m
.dsp
, ctx
->qmatrix_l
, ctx
->qmatrix_l16
, weight_matrix
,
177 ctx
->m
.intra_quant_bias
, 1, ctx
->m
.avctx
->qmax
, 1);
178 for (i
= 1; i
< 64; i
++) {
179 int j
= ctx
->m
.dsp
.idct_permutation
[ff_zigzag_direct
[i
]];
180 weight_matrix
[j
] = ctx
->cid_table
->chroma_weight
[i
];
182 ff_convert_matrix(&ctx
->m
.dsp
, ctx
->qmatrix_c
, ctx
->qmatrix_c16
, weight_matrix
,
183 ctx
->m
.intra_quant_bias
, 1, ctx
->m
.avctx
->qmax
, 1);
185 for (qscale
= 1; qscale
<= ctx
->m
.avctx
->qmax
; qscale
++) {
186 for (i
= 0; i
< 64; i
++) {
187 ctx
->qmatrix_l
[qscale
] [i
] <<= 2; ctx
->qmatrix_c
[qscale
] [i
] <<= 2;
188 ctx
->qmatrix_l16
[qscale
][0][i
] <<= 2; ctx
->qmatrix_l16
[qscale
][1][i
] <<= 2;
189 ctx
->qmatrix_c16
[qscale
][0][i
] <<= 2; ctx
->qmatrix_c16
[qscale
][1][i
] <<= 2;
194 for (qscale
= 1; qscale
<= ctx
->m
.avctx
->qmax
; qscale
++) {
195 for (i
= 1; i
< 64; i
++) {
196 int j
= ctx
->m
.dsp
.idct_permutation
[ff_zigzag_direct
[i
]];
198 // The quantization formula from the VC-3 standard is:
199 // quantized = sign(block[i]) * floor(abs(block[i]/s) * p / (qscale * weight_table[i]))
200 // Where p is 32 for 8-bit samples and 8 for 10-bit ones.
201 // The s factor compensates scaling of DCT coefficients done by the DCT routines,
202 // and therefore is not present in standard. It's 8 for 8-bit samples and 4 for 10-bit ones.
203 // We want values of ctx->qtmatrix_l and ctx->qtmatrix_r to be:
204 // ((1 << DNX10BIT_QMAT_SHIFT) * (p / s)) / (qscale * weight_table[i])
205 // For 10-bit samples, p / s == 2
206 ctx
->qmatrix_l
[qscale
][j
] = (1 << (DNX10BIT_QMAT_SHIFT
+ 1)) / (qscale
* luma_weight_table
[i
]);
207 ctx
->qmatrix_c
[qscale
][j
] = (1 << (DNX10BIT_QMAT_SHIFT
+ 1)) / (qscale
* chroma_weight_table
[i
]);
217 static int dnxhd_init_rc(DNXHDEncContext
*ctx
)
219 FF_ALLOCZ_OR_GOTO(ctx
->m
.avctx
, ctx
->mb_rc
, 8160*ctx
->m
.avctx
->qmax
*sizeof(RCEntry
), fail
);
220 if (ctx
->m
.avctx
->mb_decision
!= FF_MB_DECISION_RD
)
221 FF_ALLOCZ_OR_GOTO(ctx
->m
.avctx
, ctx
->mb_cmp
, ctx
->m
.mb_num
*sizeof(RCCMPEntry
), fail
);
223 ctx
->frame_bits
= (ctx
->cid_table
->coding_unit_size
- 640 - 4 - ctx
->min_padding
) * 8;
225 ctx
->lambda
= 2<<LAMBDA_FRAC_BITS
; // qscale 2
231 static int dnxhd_encode_init(AVCodecContext
*avctx
)
233 DNXHDEncContext
*ctx
= avctx
->priv_data
;
234 int i
, index
, bit_depth
;
236 switch (avctx
->pix_fmt
) {
237 case AV_PIX_FMT_YUV422P
:
240 case AV_PIX_FMT_YUV422P10
:
244 av_log(avctx
, AV_LOG_ERROR
, "pixel format is incompatible with DNxHD\n");
248 ctx
->cid
= ff_dnxhd_find_cid(avctx
, bit_depth
);
250 av_log(avctx
, AV_LOG_ERROR
, "video parameters incompatible with DNxHD\n");
253 av_log(avctx
, AV_LOG_DEBUG
, "cid %d\n", ctx
->cid
);
255 index
= ff_dnxhd_get_cid_table(ctx
->cid
);
256 ctx
->cid_table
= &ff_dnxhd_cid_table
[index
];
258 ctx
->m
.avctx
= avctx
;
262 avctx
->bits_per_raw_sample
= ctx
->cid_table
->bit_depth
;
264 ff_dsputil_init(&ctx
->m
.dsp
, avctx
);
265 ff_dct_common_init(&ctx
->m
);
266 if (!ctx
->m
.dct_quantize
)
267 ctx
->m
.dct_quantize
= ff_dct_quantize_c
;
269 if (ctx
->cid_table
->bit_depth
== 10) {
270 ctx
->m
.dct_quantize
= dnxhd_10bit_dct_quantize
;
271 ctx
->get_pixels_8x4_sym
= dnxhd_10bit_get_pixels_8x4_sym
;
272 ctx
->block_width_l2
= 4;
274 ctx
->get_pixels_8x4_sym
= dnxhd_8bit_get_pixels_8x4_sym
;
275 ctx
->block_width_l2
= 3;
279 ff_dnxhdenc_init_x86(ctx
);
281 ctx
->m
.mb_height
= (avctx
->height
+ 15) / 16;
282 ctx
->m
.mb_width
= (avctx
->width
+ 15) / 16;
284 if (avctx
->flags
& CODEC_FLAG_INTERLACED_DCT
) {
286 ctx
->m
.mb_height
/= 2;
289 ctx
->m
.mb_num
= ctx
->m
.mb_height
* ctx
->m
.mb_width
;
291 if (avctx
->intra_quant_bias
!= FF_DEFAULT_QUANT_BIAS
)
292 ctx
->m
.intra_quant_bias
= avctx
->intra_quant_bias
;
293 if (dnxhd_init_qmat(ctx
, ctx
->m
.intra_quant_bias
, 0) < 0) // XXX tune lbias/cbias
296 // Avid Nitris hardware decoder requires a minimum amount of padding in the coding unit payload
297 if (ctx
->nitris_compat
)
298 ctx
->min_padding
= 1600;
300 if (dnxhd_init_vlc(ctx
) < 0)
302 if (dnxhd_init_rc(ctx
) < 0)
305 FF_ALLOCZ_OR_GOTO(ctx
->m
.avctx
, ctx
->slice_size
, ctx
->m
.mb_height
*sizeof(uint32_t), fail
);
306 FF_ALLOCZ_OR_GOTO(ctx
->m
.avctx
, ctx
->slice_offs
, ctx
->m
.mb_height
*sizeof(uint32_t), fail
);
307 FF_ALLOCZ_OR_GOTO(ctx
->m
.avctx
, ctx
->mb_bits
, ctx
->m
.mb_num
*sizeof(uint16_t), fail
);
308 FF_ALLOCZ_OR_GOTO(ctx
->m
.avctx
, ctx
->mb_qscale
, ctx
->m
.mb_num
*sizeof(uint8_t), fail
);
310 ctx
->frame
.key_frame
= 1;
311 ctx
->frame
.pict_type
= AV_PICTURE_TYPE_I
;
312 ctx
->m
.avctx
->coded_frame
= &ctx
->frame
;
314 if (avctx
->thread_count
> MAX_THREADS
) {
315 av_log(avctx
, AV_LOG_ERROR
, "too many threads\n");
319 ctx
->thread
[0] = ctx
;
320 for (i
= 1; i
< avctx
->thread_count
; i
++) {
321 ctx
->thread
[i
] = av_malloc(sizeof(DNXHDEncContext
));
322 memcpy(ctx
->thread
[i
], ctx
, sizeof(DNXHDEncContext
));
326 fail
: //for FF_ALLOCZ_OR_GOTO
330 static int dnxhd_write_header(AVCodecContext
*avctx
, uint8_t *buf
)
332 DNXHDEncContext
*ctx
= avctx
->priv_data
;
333 const uint8_t header_prefix
[5] = { 0x00,0x00,0x02,0x80,0x01 };
337 memcpy(buf
, header_prefix
, 5);
338 buf
[5] = ctx
->interlaced
? ctx
->cur_field
+2 : 0x01;
339 buf
[6] = 0x80; // crc flag off
340 buf
[7] = 0xa0; // reserved
341 AV_WB16(buf
+ 0x18, avctx
->height
>>ctx
->interlaced
); // ALPF
342 AV_WB16(buf
+ 0x1a, avctx
->width
); // SPL
343 AV_WB16(buf
+ 0x1d, avctx
->height
>>ctx
->interlaced
); // NAL
345 buf
[0x21] = ctx
->cid_table
->bit_depth
== 10 ? 0x58 : 0x38;
346 buf
[0x22] = 0x88 + (ctx
->interlaced
<<2);
347 AV_WB32(buf
+ 0x28, ctx
->cid
); // CID
348 buf
[0x2c] = ctx
->interlaced
? 0 : 0x80;
350 buf
[0x5f] = 0x01; // UDL
352 buf
[0x167] = 0x02; // reserved
353 AV_WB16(buf
+ 0x16a, ctx
->m
.mb_height
* 4 + 4); // MSIPS
354 buf
[0x16d] = ctx
->m
.mb_height
; // Ns
355 buf
[0x16f] = 0x10; // reserved
357 ctx
->msip
= buf
+ 0x170;
361 static av_always_inline
void dnxhd_encode_dc(DNXHDEncContext
*ctx
, int diff
)
365 nbits
= av_log2_16bit(-2*diff
);
368 nbits
= av_log2_16bit(2*diff
);
370 put_bits(&ctx
->m
.pb
, ctx
->cid_table
->dc_bits
[nbits
] + nbits
,
371 (ctx
->cid_table
->dc_codes
[nbits
]<<nbits
) + (diff
& ((1 << nbits
) - 1)));
374 static av_always_inline
void dnxhd_encode_block(DNXHDEncContext
*ctx
, int16_t *block
, int last_index
, int n
)
376 int last_non_zero
= 0;
379 dnxhd_encode_dc(ctx
, block
[0] - ctx
->m
.last_dc
[n
]);
380 ctx
->m
.last_dc
[n
] = block
[0];
382 for (i
= 1; i
<= last_index
; i
++) {
383 j
= ctx
->m
.intra_scantable
.permutated
[i
];
386 int run_level
= i
- last_non_zero
- 1;
387 int rlevel
= (slevel
<<1)|!!run_level
;
388 put_bits(&ctx
->m
.pb
, ctx
->vlc_bits
[rlevel
], ctx
->vlc_codes
[rlevel
]);
390 put_bits(&ctx
->m
.pb
, ctx
->run_bits
[run_level
], ctx
->run_codes
[run_level
]);
394 put_bits(&ctx
->m
.pb
, ctx
->vlc_bits
[0], ctx
->vlc_codes
[0]); // EOB
397 static av_always_inline
void dnxhd_unquantize_c(DNXHDEncContext
*ctx
, int16_t *block
, int n
, int qscale
, int last_index
)
399 const uint8_t *weight_matrix
;
403 weight_matrix
= (n
&2) ? ctx
->cid_table
->chroma_weight
: ctx
->cid_table
->luma_weight
;
405 for (i
= 1; i
<= last_index
; i
++) {
406 int j
= ctx
->m
.intra_scantable
.permutated
[i
];
410 level
= (1-2*level
) * qscale
* weight_matrix
[i
];
411 if (ctx
->cid_table
->bit_depth
== 10) {
412 if (weight_matrix
[i
] != 8)
416 if (weight_matrix
[i
] != 32)
422 level
= (2*level
+1) * qscale
* weight_matrix
[i
];
423 if (ctx
->cid_table
->bit_depth
== 10) {
424 if (weight_matrix
[i
] != 8)
428 if (weight_matrix
[i
] != 32)
438 static av_always_inline
int dnxhd_ssd_block(int16_t *qblock
, int16_t *block
)
442 for (i
= 0; i
< 64; i
++)
443 score
+= (block
[i
] - qblock
[i
]) * (block
[i
] - qblock
[i
]);
447 static av_always_inline
int dnxhd_calc_ac_bits(DNXHDEncContext
*ctx
, int16_t *block
, int last_index
)
449 int last_non_zero
= 0;
452 for (i
= 1; i
<= last_index
; i
++) {
453 j
= ctx
->m
.intra_scantable
.permutated
[i
];
456 int run_level
= i
- last_non_zero
- 1;
457 bits
+= ctx
->vlc_bits
[(level
<<1)|!!run_level
]+ctx
->run_bits
[run_level
];
464 static av_always_inline
void dnxhd_get_blocks(DNXHDEncContext
*ctx
, int mb_x
, int mb_y
)
466 const int bs
= ctx
->block_width_l2
;
467 const int bw
= 1 << bs
;
468 const uint8_t *ptr_y
= ctx
->thread
[0]->src
[0] + ((mb_y
<< 4) * ctx
->m
.linesize
) + (mb_x
<< bs
+1);
469 const uint8_t *ptr_u
= ctx
->thread
[0]->src
[1] + ((mb_y
<< 4) * ctx
->m
.uvlinesize
) + (mb_x
<< bs
);
470 const uint8_t *ptr_v
= ctx
->thread
[0]->src
[2] + ((mb_y
<< 4) * ctx
->m
.uvlinesize
) + (mb_x
<< bs
);
471 DSPContext
*dsp
= &ctx
->m
.dsp
;
473 dsp
->get_pixels(ctx
->blocks
[0], ptr_y
, ctx
->m
.linesize
);
474 dsp
->get_pixels(ctx
->blocks
[1], ptr_y
+ bw
, ctx
->m
.linesize
);
475 dsp
->get_pixels(ctx
->blocks
[2], ptr_u
, ctx
->m
.uvlinesize
);
476 dsp
->get_pixels(ctx
->blocks
[3], ptr_v
, ctx
->m
.uvlinesize
);
478 if (mb_y
+1 == ctx
->m
.mb_height
&& ctx
->m
.avctx
->height
== 1080) {
479 if (ctx
->interlaced
) {
480 ctx
->get_pixels_8x4_sym(ctx
->blocks
[4], ptr_y
+ ctx
->dct_y_offset
, ctx
->m
.linesize
);
481 ctx
->get_pixels_8x4_sym(ctx
->blocks
[5], ptr_y
+ ctx
->dct_y_offset
+ bw
, ctx
->m
.linesize
);
482 ctx
->get_pixels_8x4_sym(ctx
->blocks
[6], ptr_u
+ ctx
->dct_uv_offset
, ctx
->m
.uvlinesize
);
483 ctx
->get_pixels_8x4_sym(ctx
->blocks
[7], ptr_v
+ ctx
->dct_uv_offset
, ctx
->m
.uvlinesize
);
485 dsp
->clear_block(ctx
->blocks
[4]);
486 dsp
->clear_block(ctx
->blocks
[5]);
487 dsp
->clear_block(ctx
->blocks
[6]);
488 dsp
->clear_block(ctx
->blocks
[7]);
491 dsp
->get_pixels(ctx
->blocks
[4], ptr_y
+ ctx
->dct_y_offset
, ctx
->m
.linesize
);
492 dsp
->get_pixels(ctx
->blocks
[5], ptr_y
+ ctx
->dct_y_offset
+ bw
, ctx
->m
.linesize
);
493 dsp
->get_pixels(ctx
->blocks
[6], ptr_u
+ ctx
->dct_uv_offset
, ctx
->m
.uvlinesize
);
494 dsp
->get_pixels(ctx
->blocks
[7], ptr_v
+ ctx
->dct_uv_offset
, ctx
->m
.uvlinesize
);
498 static av_always_inline
int dnxhd_switch_matrix(DNXHDEncContext
*ctx
, int i
)
501 ctx
->m
.q_intra_matrix16
= ctx
->qmatrix_c16
;
502 ctx
->m
.q_intra_matrix
= ctx
->qmatrix_c
;
505 ctx
->m
.q_intra_matrix16
= ctx
->qmatrix_l16
;
506 ctx
->m
.q_intra_matrix
= ctx
->qmatrix_l
;
511 static int dnxhd_calc_bits_thread(AVCodecContext
*avctx
, void *arg
, int jobnr
, int threadnr
)
513 DNXHDEncContext
*ctx
= avctx
->priv_data
;
514 int mb_y
= jobnr
, mb_x
;
515 int qscale
= ctx
->qscale
;
516 LOCAL_ALIGNED_16(int16_t, block
, [64]);
517 ctx
= ctx
->thread
[threadnr
];
521 ctx
->m
.last_dc
[2] = 1 << (ctx
->cid_table
->bit_depth
+ 2);
523 for (mb_x
= 0; mb_x
< ctx
->m
.mb_width
; mb_x
++) {
524 unsigned mb
= mb_y
* ctx
->m
.mb_width
+ mb_x
;
530 dnxhd_get_blocks(ctx
, mb_x
, mb_y
);
532 for (i
= 0; i
< 8; i
++) {
533 int16_t *src_block
= ctx
->blocks
[i
];
534 int overflow
, nbits
, diff
, last_index
;
535 int n
= dnxhd_switch_matrix(ctx
, i
);
537 memcpy(block
, src_block
, 64*sizeof(*block
));
538 last_index
= ctx
->m
.dct_quantize(&ctx
->m
, block
, i
, qscale
, &overflow
);
539 ac_bits
+= dnxhd_calc_ac_bits(ctx
, block
, last_index
);
541 diff
= block
[0] - ctx
->m
.last_dc
[n
];
542 if (diff
< 0) nbits
= av_log2_16bit(-2*diff
);
543 else nbits
= av_log2_16bit( 2*diff
);
545 assert(nbits
< ctx
->cid_table
->bit_depth
+ 4);
546 dc_bits
+= ctx
->cid_table
->dc_bits
[nbits
] + nbits
;
548 ctx
->m
.last_dc
[n
] = block
[0];
550 if (avctx
->mb_decision
== FF_MB_DECISION_RD
|| !RC_VARIANCE
) {
551 dnxhd_unquantize_c(ctx
, block
, i
, qscale
, last_index
);
552 ctx
->m
.dsp
.idct(block
);
553 ssd
+= dnxhd_ssd_block(block
, src_block
);
556 ctx
->mb_rc
[qscale
][mb
].ssd
= ssd
;
557 ctx
->mb_rc
[qscale
][mb
].bits
= ac_bits
+dc_bits
+12+8*ctx
->vlc_bits
[0];
562 static int dnxhd_encode_thread(AVCodecContext
*avctx
, void *arg
, int jobnr
, int threadnr
)
564 DNXHDEncContext
*ctx
= avctx
->priv_data
;
565 int mb_y
= jobnr
, mb_x
;
566 ctx
= ctx
->thread
[threadnr
];
567 init_put_bits(&ctx
->m
.pb
, (uint8_t *)arg
+ 640 + ctx
->slice_offs
[jobnr
], ctx
->slice_size
[jobnr
]);
571 ctx
->m
.last_dc
[2] = 1 << (ctx
->cid_table
->bit_depth
+ 2);
572 for (mb_x
= 0; mb_x
< ctx
->m
.mb_width
; mb_x
++) {
573 unsigned mb
= mb_y
* ctx
->m
.mb_width
+ mb_x
;
574 int qscale
= ctx
->mb_qscale
[mb
];
577 put_bits(&ctx
->m
.pb
, 12, qscale
<<1);
579 dnxhd_get_blocks(ctx
, mb_x
, mb_y
);
581 for (i
= 0; i
< 8; i
++) {
582 int16_t *block
= ctx
->blocks
[i
];
583 int overflow
, n
= dnxhd_switch_matrix(ctx
, i
);
584 int last_index
= ctx
->m
.dct_quantize(&ctx
->m
, block
, i
,
587 dnxhd_encode_block(ctx
, block
, last_index
, n
);
588 //STOP_TIMER("encode_block");
591 if (put_bits_count(&ctx
->m
.pb
)&31)
592 put_bits(&ctx
->m
.pb
, 32-(put_bits_count(&ctx
->m
.pb
)&31), 0);
593 flush_put_bits(&ctx
->m
.pb
);
597 static void dnxhd_setup_threads_slices(DNXHDEncContext
*ctx
)
601 for (mb_y
= 0; mb_y
< ctx
->m
.mb_height
; mb_y
++) {
603 ctx
->slice_offs
[mb_y
] = offset
;
604 ctx
->slice_size
[mb_y
] = 0;
605 for (mb_x
= 0; mb_x
< ctx
->m
.mb_width
; mb_x
++) {
606 unsigned mb
= mb_y
* ctx
->m
.mb_width
+ mb_x
;
607 ctx
->slice_size
[mb_y
] += ctx
->mb_bits
[mb
];
609 ctx
->slice_size
[mb_y
] = (ctx
->slice_size
[mb_y
]+31)&~31;
610 ctx
->slice_size
[mb_y
] >>= 3;
611 thread_size
= ctx
->slice_size
[mb_y
];
612 offset
+= thread_size
;
616 static int dnxhd_mb_var_thread(AVCodecContext
*avctx
, void *arg
, int jobnr
, int threadnr
)
618 DNXHDEncContext
*ctx
= avctx
->priv_data
;
619 int mb_y
= jobnr
, mb_x
, x
, y
;
620 int partial_last_row
= (mb_y
== ctx
->m
.mb_height
- 1) &&
621 ((avctx
->height
>> ctx
->interlaced
) & 0xF);
623 ctx
= ctx
->thread
[threadnr
];
624 if (ctx
->cid_table
->bit_depth
== 8) {
625 uint8_t *pix
= ctx
->thread
[0]->src
[0] + ((mb_y
<<4) * ctx
->m
.linesize
);
626 for (mb_x
= 0; mb_x
< ctx
->m
.mb_width
; ++mb_x
, pix
+= 16) {
627 unsigned mb
= mb_y
* ctx
->m
.mb_width
+ mb_x
;
631 if (!partial_last_row
&& mb_x
* 16 <= avctx
->width
- 16) {
632 sum
= ctx
->m
.dsp
.pix_sum(pix
, ctx
->m
.linesize
);
633 varc
= ctx
->m
.dsp
.pix_norm1(pix
, ctx
->m
.linesize
);
635 int bw
= FFMIN(avctx
->width
- 16 * mb_x
, 16);
636 int bh
= FFMIN((avctx
->height
>> ctx
->interlaced
) - 16 * mb_y
, 16);
638 for (y
= 0; y
< bh
; y
++) {
639 for (x
= 0; x
< bw
; x
++) {
640 uint8_t val
= pix
[x
+ y
* ctx
->m
.linesize
];
646 varc
= (varc
- (((unsigned)sum
* sum
) >> 8) + 128) >> 8;
648 ctx
->mb_cmp
[mb
].value
= varc
;
649 ctx
->mb_cmp
[mb
].mb
= mb
;
652 int const linesize
= ctx
->m
.linesize
>> 1;
653 for (mb_x
= 0; mb_x
< ctx
->m
.mb_width
; ++mb_x
) {
654 uint16_t *pix
= (uint16_t*)ctx
->thread
[0]->src
[0] + ((mb_y
<< 4) * linesize
) + (mb_x
<< 4);
655 unsigned mb
= mb_y
* ctx
->m
.mb_width
+ mb_x
;
660 // Macroblocks are 16x16 pixels, unlike DCT blocks which are 8x8.
661 for (i
= 0; i
< 16; ++i
) {
662 for (j
= 0; j
< 16; ++j
) {
663 // Turn 16-bit pixels into 10-bit ones.
664 int const sample
= (unsigned)pix
[j
] >> 6;
666 sqsum
+= sample
* sample
;
667 // 2^10 * 2^10 * 16 * 16 = 2^28, which is less than INT_MAX
671 mean
= sum
>> 8; // 16*16 == 2^8
673 ctx
->mb_cmp
[mb
].value
= sqmean
- mean
* mean
;
674 ctx
->mb_cmp
[mb
].mb
= mb
;
680 static int dnxhd_encode_rdo(AVCodecContext
*avctx
, DNXHDEncContext
*ctx
)
682 int lambda
, up_step
, down_step
;
683 int last_lower
= INT_MAX
, last_higher
= 0;
686 for (q
= 1; q
< avctx
->qmax
; q
++) {
688 avctx
->execute2(avctx
, dnxhd_calc_bits_thread
, NULL
, NULL
, ctx
->m
.mb_height
);
690 up_step
= down_step
= 2<<LAMBDA_FRAC_BITS
;
691 lambda
= ctx
->lambda
;
696 if (lambda
== last_higher
) {
698 end
= 1; // need to set final qscales/bits
700 for (y
= 0; y
< ctx
->m
.mb_height
; y
++) {
701 for (x
= 0; x
< ctx
->m
.mb_width
; x
++) {
702 unsigned min
= UINT_MAX
;
704 int mb
= y
*ctx
->m
.mb_width
+x
;
705 for (q
= 1; q
< avctx
->qmax
; q
++) {
706 unsigned score
= ctx
->mb_rc
[q
][mb
].bits
*lambda
+
707 ((unsigned)ctx
->mb_rc
[q
][mb
].ssd
<<LAMBDA_FRAC_BITS
);
713 bits
+= ctx
->mb_rc
[qscale
][mb
].bits
;
714 ctx
->mb_qscale
[mb
] = qscale
;
715 ctx
->mb_bits
[mb
] = ctx
->mb_rc
[qscale
][mb
].bits
;
717 bits
= (bits
+31)&~31; // padding
718 if (bits
> ctx
->frame_bits
)
721 //av_dlog(ctx->m.avctx, "lambda %d, up %u, down %u, bits %d, frame %d\n",
722 // lambda, last_higher, last_lower, bits, ctx->frame_bits);
724 if (bits
> ctx
->frame_bits
)
728 if (bits
< ctx
->frame_bits
) {
729 last_lower
= FFMIN(lambda
, last_lower
);
730 if (last_higher
!= 0)
731 lambda
= (lambda
+last_higher
)>>1;
734 down_step
= FFMIN((int64_t)down_step
*5, INT_MAX
);
735 up_step
= 1<<LAMBDA_FRAC_BITS
;
736 lambda
= FFMAX(1, lambda
);
737 if (lambda
== last_lower
)
740 last_higher
= FFMAX(lambda
, last_higher
);
741 if (last_lower
!= INT_MAX
)
742 lambda
= (lambda
+last_lower
)>>1;
743 else if ((int64_t)lambda
+ up_step
> INT_MAX
)
747 up_step
= FFMIN((int64_t)up_step
*5, INT_MAX
);
748 down_step
= 1<<LAMBDA_FRAC_BITS
;
751 //av_dlog(ctx->m.avctx, "out lambda %d\n", lambda);
752 ctx
->lambda
= lambda
;
756 static int dnxhd_find_qscale(DNXHDEncContext
*ctx
)
762 int last_lower
= INT_MAX
;
766 qscale
= ctx
->qscale
;
769 ctx
->qscale
= qscale
;
770 // XXX avoid recalculating bits
771 ctx
->m
.avctx
->execute2(ctx
->m
.avctx
, dnxhd_calc_bits_thread
, NULL
, NULL
, ctx
->m
.mb_height
);
772 for (y
= 0; y
< ctx
->m
.mb_height
; y
++) {
773 for (x
= 0; x
< ctx
->m
.mb_width
; x
++)
774 bits
+= ctx
->mb_rc
[qscale
][y
*ctx
->m
.mb_width
+x
].bits
;
775 bits
= (bits
+31)&~31; // padding
776 if (bits
> ctx
->frame_bits
)
779 //av_dlog(ctx->m.avctx, "%d, qscale %d, bits %d, frame %d, higher %d, lower %d\n",
780 // ctx->m.avctx->frame_number, qscale, bits, ctx->frame_bits, last_higher, last_lower);
781 if (bits
< ctx
->frame_bits
) {
784 if (last_higher
== qscale
- 1) {
785 qscale
= last_higher
;
788 last_lower
= FFMIN(qscale
, last_lower
);
789 if (last_higher
!= 0)
790 qscale
= (qscale
+last_higher
)>>1;
792 qscale
-= down_step
++;
797 if (last_lower
== qscale
+ 1)
799 last_higher
= FFMAX(qscale
, last_higher
);
800 if (last_lower
!= INT_MAX
)
801 qscale
= (qscale
+last_lower
)>>1;
805 if (qscale
>= ctx
->m
.avctx
->qmax
)
809 //av_dlog(ctx->m.avctx, "out qscale %d\n", qscale);
810 ctx
->qscale
= qscale
;
814 #define BUCKET_BITS 8
815 #define RADIX_PASSES 4
816 #define NBUCKETS (1 << BUCKET_BITS)
818 static inline int get_bucket(int value
, int shift
)
821 value
&= NBUCKETS
- 1;
822 return NBUCKETS
- 1 - value
;
825 static void radix_count(const RCCMPEntry
*data
, int size
, int buckets
[RADIX_PASSES
][NBUCKETS
])
828 memset(buckets
, 0, sizeof(buckets
[0][0]) * RADIX_PASSES
* NBUCKETS
);
829 for (i
= 0; i
< size
; i
++) {
830 int v
= data
[i
].value
;
831 for (j
= 0; j
< RADIX_PASSES
; j
++) {
832 buckets
[j
][get_bucket(v
, 0)]++;
837 for (j
= 0; j
< RADIX_PASSES
; j
++) {
839 for (i
= NBUCKETS
- 1; i
>= 0; i
--)
840 buckets
[j
][i
] = offset
-= buckets
[j
][i
];
841 assert(!buckets
[j
][0]);
845 static void radix_sort_pass(RCCMPEntry
*dst
, const RCCMPEntry
*data
, int size
, int buckets
[NBUCKETS
], int pass
)
847 int shift
= pass
* BUCKET_BITS
;
849 for (i
= 0; i
< size
; i
++) {
850 int v
= get_bucket(data
[i
].value
, shift
);
851 int pos
= buckets
[v
]++;
856 static void radix_sort(RCCMPEntry
*data
, int size
)
858 int buckets
[RADIX_PASSES
][NBUCKETS
];
859 RCCMPEntry
*tmp
= av_malloc(sizeof(*tmp
) * size
);
860 radix_count(data
, size
, buckets
);
861 radix_sort_pass(tmp
, data
, size
, buckets
[0], 0);
862 radix_sort_pass(data
, tmp
, size
, buckets
[1], 1);
863 if (buckets
[2][NBUCKETS
- 1] || buckets
[3][NBUCKETS
- 1]) {
864 radix_sort_pass(tmp
, data
, size
, buckets
[2], 2);
865 radix_sort_pass(data
, tmp
, size
, buckets
[3], 3);
870 static int dnxhd_encode_fast(AVCodecContext
*avctx
, DNXHDEncContext
*ctx
)
874 if ((ret
= dnxhd_find_qscale(ctx
)) < 0)
876 for (y
= 0; y
< ctx
->m
.mb_height
; y
++) {
877 for (x
= 0; x
< ctx
->m
.mb_width
; x
++) {
878 int mb
= y
*ctx
->m
.mb_width
+x
;
880 ctx
->mb_qscale
[mb
] = ctx
->qscale
;
881 ctx
->mb_bits
[mb
] = ctx
->mb_rc
[ctx
->qscale
][mb
].bits
;
882 max_bits
+= ctx
->mb_rc
[ctx
->qscale
][mb
].bits
;
884 delta_bits
= ctx
->mb_rc
[ctx
->qscale
][mb
].bits
-ctx
->mb_rc
[ctx
->qscale
+1][mb
].bits
;
885 ctx
->mb_cmp
[mb
].mb
= mb
;
886 ctx
->mb_cmp
[mb
].value
= delta_bits
?
887 ((ctx
->mb_rc
[ctx
->qscale
][mb
].ssd
-ctx
->mb_rc
[ctx
->qscale
+1][mb
].ssd
)*100)/delta_bits
888 : INT_MIN
; //avoid increasing qscale
891 max_bits
+= 31; //worst padding
895 avctx
->execute2(avctx
, dnxhd_mb_var_thread
, NULL
, NULL
, ctx
->m
.mb_height
);
896 radix_sort(ctx
->mb_cmp
, ctx
->m
.mb_num
);
897 for (x
= 0; x
< ctx
->m
.mb_num
&& max_bits
> ctx
->frame_bits
; x
++) {
898 int mb
= ctx
->mb_cmp
[x
].mb
;
899 max_bits
-= ctx
->mb_rc
[ctx
->qscale
][mb
].bits
- ctx
->mb_rc
[ctx
->qscale
+1][mb
].bits
;
900 ctx
->mb_qscale
[mb
] = ctx
->qscale
+1;
901 ctx
->mb_bits
[mb
] = ctx
->mb_rc
[ctx
->qscale
+1][mb
].bits
;
907 static void dnxhd_load_picture(DNXHDEncContext
*ctx
, const AVFrame
*frame
)
911 for (i
= 0; i
< 3; i
++) {
912 ctx
->frame
.data
[i
] = frame
->data
[i
];
913 ctx
->frame
.linesize
[i
] = frame
->linesize
[i
];
916 for (i
= 0; i
< ctx
->m
.avctx
->thread_count
; i
++) {
917 ctx
->thread
[i
]->m
.linesize
= ctx
->frame
.linesize
[0]<<ctx
->interlaced
;
918 ctx
->thread
[i
]->m
.uvlinesize
= ctx
->frame
.linesize
[1]<<ctx
->interlaced
;
919 ctx
->thread
[i
]->dct_y_offset
= ctx
->m
.linesize
*8;
920 ctx
->thread
[i
]->dct_uv_offset
= ctx
->m
.uvlinesize
*8;
923 ctx
->frame
.interlaced_frame
= frame
->interlaced_frame
;
924 ctx
->cur_field
= frame
->interlaced_frame
&& !frame
->top_field_first
;
927 static int dnxhd_encode_picture(AVCodecContext
*avctx
, AVPacket
*pkt
,
928 const AVFrame
*frame
, int *got_packet
)
930 DNXHDEncContext
*ctx
= avctx
->priv_data
;
935 if ((ret
= ff_alloc_packet(pkt
, ctx
->cid_table
->frame_size
)) < 0) {
936 av_log(avctx
, AV_LOG_ERROR
, "output buffer is too small to compress picture\n");
941 dnxhd_load_picture(ctx
, frame
);
944 for (i
= 0; i
< 3; i
++) {
945 ctx
->src
[i
] = ctx
->frame
.data
[i
];
946 if (ctx
->interlaced
&& ctx
->cur_field
)
947 ctx
->src
[i
] += ctx
->frame
.linesize
[i
];
950 dnxhd_write_header(avctx
, buf
);
952 if (avctx
->mb_decision
== FF_MB_DECISION_RD
)
953 ret
= dnxhd_encode_rdo(avctx
, ctx
);
955 ret
= dnxhd_encode_fast(avctx
, ctx
);
957 av_log(avctx
, AV_LOG_ERROR
,
958 "picture could not fit ratecontrol constraints, increase qmax\n");
962 dnxhd_setup_threads_slices(ctx
);
965 for (i
= 0; i
< ctx
->m
.mb_height
; i
++) {
966 AV_WB32(ctx
->msip
+ i
* 4, offset
);
967 offset
+= ctx
->slice_size
[i
];
968 assert(!(ctx
->slice_size
[i
] & 3));
971 avctx
->execute2(avctx
, dnxhd_encode_thread
, buf
, NULL
, ctx
->m
.mb_height
);
973 assert(640 + offset
+ 4 <= ctx
->cid_table
->coding_unit_size
);
974 memset(buf
+ 640 + offset
, 0, ctx
->cid_table
->coding_unit_size
- 4 - offset
- 640);
976 AV_WB32(buf
+ ctx
->cid_table
->coding_unit_size
- 4, 0x600DC0DE); // EOF
978 if (ctx
->interlaced
&& first_field
) {
981 buf
+= ctx
->cid_table
->coding_unit_size
;
982 goto encode_coding_unit
;
985 ctx
->frame
.quality
= ctx
->qscale
*FF_QP2LAMBDA
;
987 pkt
->flags
|= AV_PKT_FLAG_KEY
;
992 static int dnxhd_encode_end(AVCodecContext
*avctx
)
994 DNXHDEncContext
*ctx
= avctx
->priv_data
;
995 int max_level
= 1<<(ctx
->cid_table
->bit_depth
+2);
998 av_free(ctx
->vlc_codes
-max_level
*2);
999 av_free(ctx
->vlc_bits
-max_level
*2);
1000 av_freep(&ctx
->run_codes
);
1001 av_freep(&ctx
->run_bits
);
1003 av_freep(&ctx
->mb_bits
);
1004 av_freep(&ctx
->mb_qscale
);
1005 av_freep(&ctx
->mb_rc
);
1006 av_freep(&ctx
->mb_cmp
);
1007 av_freep(&ctx
->slice_size
);
1008 av_freep(&ctx
->slice_offs
);
1010 av_freep(&ctx
->qmatrix_c
);
1011 av_freep(&ctx
->qmatrix_l
);
1012 av_freep(&ctx
->qmatrix_c16
);
1013 av_freep(&ctx
->qmatrix_l16
);
1015 for (i
= 1; i
< avctx
->thread_count
; i
++)
1016 av_freep(&ctx
->thread
[i
]);
1021 AVCodec ff_dnxhd_encoder
= {
1023 .type
= AVMEDIA_TYPE_VIDEO
,
1024 .id
= AV_CODEC_ID_DNXHD
,
1025 .priv_data_size
= sizeof(DNXHDEncContext
),
1026 .init
= dnxhd_encode_init
,
1027 .encode2
= dnxhd_encode_picture
,
1028 .close
= dnxhd_encode_end
,
1029 .capabilities
= CODEC_CAP_SLICE_THREADS
,
1030 .pix_fmts
= (const enum AVPixelFormat
[]){ AV_PIX_FMT_YUV422P
,
1031 AV_PIX_FMT_YUV422P10
,
1033 .long_name
= NULL_IF_CONFIG_SMALL("VC3/DNxHD"),
1034 .priv_class
= &class,