1 /*
2 * VC3/DNxHD encoder
3 * Copyright (c) 2007 Baptiste Coudurier <baptiste dot coudurier at smartjog dot com>
4 * Copyright (c) 2011 MirriAd Ltd
5 *
6 * VC-3 encoder funded by the British Broadcasting Corporation
7 * 10 bit support added by MirriAd Ltd, Joseph Artsimovich <joseph@mirriad.com>
8 *
9 * This file is part of FFmpeg.
10 *
11 * FFmpeg 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.
15 *
16 * FFmpeg 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.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with FFmpeg; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
26 #include "libavutil/attributes.h"
27 #include "libavutil/internal.h"
28 #include "libavutil/mem_internal.h"
29 #include "libavutil/opt.h"
30
31 #include "avcodec.h"
32 #include "blockdsp.h"
33 #include "fdctdsp.h"
34 #include "internal.h"
35 #include "mpegvideo.h"
36 #include "pixblockdsp.h"
37 #include "packet_internal.h"
38 #include "profiles.h"
39 #include "dnxhdenc.h"
40
41 // The largest value that will not lead to overflow for 10-bit samples.
42 #define DNX10BIT_QMAT_SHIFT 18
43 #define RC_VARIANCE 1 // use variance or ssd for fast rc
44 #define LAMBDA_FRAC_BITS 10
45
46 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
47 static const AVOption options[] = {
48 { "nitris_compat", "encode with Avid Nitris compatibility",
49 offsetof(DNXHDEncContext, nitris_compat), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
50 { "ibias", "intra quant bias",
51 offsetof(DNXHDEncContext, intra_quant_bias), AV_OPT_TYPE_INT,
52 { .i64 = 0 }, INT_MIN, INT_MAX, VE },
53 { "profile", NULL, offsetof(DNXHDEncContext, profile), AV_OPT_TYPE_INT,
54 { .i64 = FF_PROFILE_DNXHD },
55 FF_PROFILE_DNXHD, FF_PROFILE_DNXHR_444, VE, "profile" },
56 { "dnxhd", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FF_PROFILE_DNXHD },
57 0, 0, VE, "profile" },
58 { "dnxhr_444", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FF_PROFILE_DNXHR_444 },
59 0, 0, VE, "profile" },
60 { "dnxhr_hqx", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FF_PROFILE_DNXHR_HQX },
61 0, 0, VE, "profile" },
62 { "dnxhr_hq", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FF_PROFILE_DNXHR_HQ },
63 0, 0, VE, "profile" },
64 { "dnxhr_sq", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FF_PROFILE_DNXHR_SQ },
65 0, 0, VE, "profile" },
66 { "dnxhr_lb", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FF_PROFILE_DNXHR_LB },
67 0, 0, VE, "profile" },
68 { NULL }
69 };
70
71 static const AVClass dnxhd_class = {
72 .class_name = "dnxhd",
73 .item_name = av_default_item_name,
74 .option = options,
75 .version = LIBAVUTIL_VERSION_INT,
76 };
77
dnxhd_8bit_get_pixels_8x4_sym(int16_t * av_restrict block,const uint8_t * pixels,ptrdiff_t line_size)78 static void dnxhd_8bit_get_pixels_8x4_sym(int16_t *av_restrict block,
79 const uint8_t *pixels,
80 ptrdiff_t line_size)
81 {
82 int i;
83 for (i = 0; i < 4; i++) {
84 block[0] = pixels[0];
85 block[1] = pixels[1];
86 block[2] = pixels[2];
87 block[3] = pixels[3];
88 block[4] = pixels[4];
89 block[5] = pixels[5];
90 block[6] = pixels[6];
91 block[7] = pixels[7];
92 pixels += line_size;
93 block += 8;
94 }
95 memcpy(block, block - 8, sizeof(*block) * 8);
96 memcpy(block + 8, block - 16, sizeof(*block) * 8);
97 memcpy(block + 16, block - 24, sizeof(*block) * 8);
98 memcpy(block + 24, block - 32, sizeof(*block) * 8);
99 }
100
101 static av_always_inline
dnxhd_10bit_get_pixels_8x4_sym(int16_t * av_restrict block,const uint8_t * pixels,ptrdiff_t line_size)102 void dnxhd_10bit_get_pixels_8x4_sym(int16_t *av_restrict block,
103 const uint8_t *pixels,
104 ptrdiff_t line_size)
105 {
106 memcpy(block + 0 * 8, pixels + 0 * line_size, 8 * sizeof(*block));
107 memcpy(block + 7 * 8, pixels + 0 * line_size, 8 * sizeof(*block));
108 memcpy(block + 1 * 8, pixels + 1 * line_size, 8 * sizeof(*block));
109 memcpy(block + 6 * 8, pixels + 1 * line_size, 8 * sizeof(*block));
110 memcpy(block + 2 * 8, pixels + 2 * line_size, 8 * sizeof(*block));
111 memcpy(block + 5 * 8, pixels + 2 * line_size, 8 * sizeof(*block));
112 memcpy(block + 3 * 8, pixels + 3 * line_size, 8 * sizeof(*block));
113 memcpy(block + 4 * 8, pixels + 3 * line_size, 8 * sizeof(*block));
114 }
115
dnxhd_10bit_dct_quantize_444(MpegEncContext * ctx,int16_t * block,int n,int qscale,int * overflow)116 static int dnxhd_10bit_dct_quantize_444(MpegEncContext *ctx, int16_t *block,
117 int n, int qscale, int *overflow)
118 {
119 int i, j, level, last_non_zero, start_i;
120 const int *qmat;
121 const uint8_t *scantable= ctx->intra_scantable.scantable;
122 int bias;
123 int max = 0;
124 unsigned int threshold1, threshold2;
125
126 ctx->fdsp.fdct(block);
127
128 block[0] = (block[0] + 2) >> 2;
129 start_i = 1;
130 last_non_zero = 0;
131 qmat = n < 4 ? ctx->q_intra_matrix[qscale] : ctx->q_chroma_intra_matrix[qscale];
132 bias= ctx->intra_quant_bias * (1 << (16 - 8));
133 threshold1 = (1 << 16) - bias - 1;
134 threshold2 = (threshold1 << 1);
135
136 for (i = 63; i >= start_i; i--) {
137 j = scantable[i];
138 level = block[j] * qmat[j];
139
140 if (((unsigned)(level + threshold1)) > threshold2) {
141 last_non_zero = i;
142 break;
143 } else{
144 block[j]=0;
145 }
146 }
147
148 for (i = start_i; i <= last_non_zero; i++) {
149 j = scantable[i];
150 level = block[j] * qmat[j];
151
152 if (((unsigned)(level + threshold1)) > threshold2) {
153 if (level > 0) {
154 level = (bias + level) >> 16;
155 block[j] = level;
156 } else{
157 level = (bias - level) >> 16;
158 block[j] = -level;
159 }
160 max |= level;
161 } else {
162 block[j] = 0;
163 }
164 }
165 *overflow = ctx->max_qcoeff < max; //overflow might have happened
166
167 /* we need this permutation so that we correct the IDCT, we only permute the !=0 elements */
168 if (ctx->idsp.perm_type != FF_IDCT_PERM_NONE)
169 ff_block_permute(block, ctx->idsp.idct_permutation,
170 scantable, last_non_zero);
171
172 return last_non_zero;
173 }
174
dnxhd_10bit_dct_quantize(MpegEncContext * ctx,int16_t * block,int n,int qscale,int * overflow)175 static int dnxhd_10bit_dct_quantize(MpegEncContext *ctx, int16_t *block,
176 int n, int qscale, int *overflow)
177 {
178 const uint8_t *scantable= ctx->intra_scantable.scantable;
179 const int *qmat = n<4 ? ctx->q_intra_matrix[qscale] : ctx->q_chroma_intra_matrix[qscale];
180 int last_non_zero = 0;
181 int i;
182
183 ctx->fdsp.fdct(block);
184
185 // Divide by 4 with rounding, to compensate scaling of DCT coefficients
186 block[0] = (block[0] + 2) >> 2;
187
188 for (i = 1; i < 64; ++i) {
189 int j = scantable[i];
190 int sign = FF_SIGNBIT(block[j]);
191 int level = (block[j] ^ sign) - sign;
192 level = level * qmat[j] >> DNX10BIT_QMAT_SHIFT;
193 block[j] = (level ^ sign) - sign;
194 if (level)
195 last_non_zero = i;
196 }
197
198 /* we need this permutation so that we correct the IDCT, we only permute the !=0 elements */
199 if (ctx->idsp.perm_type != FF_IDCT_PERM_NONE)
200 ff_block_permute(block, ctx->idsp.idct_permutation,
201 scantable, last_non_zero);
202
203 return last_non_zero;
204 }
205
dnxhd_init_vlc(DNXHDEncContext * ctx)206 static av_cold int dnxhd_init_vlc(DNXHDEncContext *ctx)
207 {
208 int i, j, level, run;
209 int max_level = 1 << (ctx->bit_depth + 2);
210
211 if (!FF_ALLOCZ_TYPED_ARRAY(ctx->orig_vlc_codes, max_level * 4) ||
212 !FF_ALLOCZ_TYPED_ARRAY(ctx->orig_vlc_bits, max_level * 4) ||
213 !(ctx->run_codes = av_mallocz(63 * 2)) ||
214 !(ctx->run_bits = av_mallocz(63)))
215 return AVERROR(ENOMEM);
216 ctx->vlc_codes = ctx->orig_vlc_codes + max_level * 2;
217 ctx->vlc_bits = ctx->orig_vlc_bits + max_level * 2;
218 for (level = -max_level; level < max_level; level++) {
219 for (run = 0; run < 2; run++) {
220 int index = level * (1 << 1) | run;
221 int sign, offset = 0, alevel = level;
222
223 MASK_ABS(sign, alevel);
224 if (alevel > 64) {
225 offset = (alevel - 1) >> 6;
226 alevel -= offset << 6;
227 }
228 for (j = 0; j < 257; j++) {
229 if (ctx->cid_table->ac_info[2*j+0] >> 1 == alevel &&
230 (!offset || (ctx->cid_table->ac_info[2*j+1] & 1) && offset) &&
231 (!run || (ctx->cid_table->ac_info[2*j+1] & 2) && run)) {
232 av_assert1(!ctx->vlc_codes[index]);
233 if (alevel) {
234 ctx->vlc_codes[index] =
235 (ctx->cid_table->ac_codes[j] << 1) | (sign & 1);
236 ctx->vlc_bits[index] = ctx->cid_table->ac_bits[j] + 1;
237 } else {
238 ctx->vlc_codes[index] = ctx->cid_table->ac_codes[j];
239 ctx->vlc_bits[index] = ctx->cid_table->ac_bits[j];
240 }
241 break;
242 }
243 }
244 av_assert0(!alevel || j < 257);
245 if (offset) {
246 ctx->vlc_codes[index] =
247 (ctx->vlc_codes[index] << ctx->cid_table->index_bits) | offset;
248 ctx->vlc_bits[index] += ctx->cid_table->index_bits;
249 }
250 }
251 }
252 for (i = 0; i < 62; i++) {
253 int run = ctx->cid_table->run[i];
254 av_assert0(run < 63);
255 ctx->run_codes[run] = ctx->cid_table->run_codes[i];
256 ctx->run_bits[run] = ctx->cid_table->run_bits[i];
257 }
258 return 0;
259 }
260
dnxhd_init_qmat(DNXHDEncContext * ctx,int lbias,int cbias)261 static av_cold int dnxhd_init_qmat(DNXHDEncContext *ctx, int lbias, int cbias)
262 {
263 // init first elem to 1 to avoid div by 0 in convert_matrix
264 uint16_t weight_matrix[64] = { 1, }; // convert_matrix needs uint16_t*
265 int qscale, i;
266 const uint8_t *luma_weight_table = ctx->cid_table->luma_weight;
267 const uint8_t *chroma_weight_table = ctx->cid_table->chroma_weight;
268
269 if (!FF_ALLOCZ_TYPED_ARRAY(ctx->qmatrix_l, ctx->m.avctx->qmax + 1) ||
270 !FF_ALLOCZ_TYPED_ARRAY(ctx->qmatrix_c, ctx->m.avctx->qmax + 1) ||
271 !FF_ALLOCZ_TYPED_ARRAY(ctx->qmatrix_l16, ctx->m.avctx->qmax + 1) ||
272 !FF_ALLOCZ_TYPED_ARRAY(ctx->qmatrix_c16, ctx->m.avctx->qmax + 1))
273 return AVERROR(ENOMEM);
274
275 if (ctx->bit_depth == 8) {
276 for (i = 1; i < 64; i++) {
277 int j = ctx->m.idsp.idct_permutation[ff_zigzag_direct[i]];
278 weight_matrix[j] = ctx->cid_table->luma_weight[i];
279 }
280 ff_convert_matrix(&ctx->m, ctx->qmatrix_l, ctx->qmatrix_l16,
281 weight_matrix, ctx->intra_quant_bias, 1,
282 ctx->m.avctx->qmax, 1);
283 for (i = 1; i < 64; i++) {
284 int j = ctx->m.idsp.idct_permutation[ff_zigzag_direct[i]];
285 weight_matrix[j] = ctx->cid_table->chroma_weight[i];
286 }
287 ff_convert_matrix(&ctx->m, ctx->qmatrix_c, ctx->qmatrix_c16,
288 weight_matrix, ctx->intra_quant_bias, 1,
289 ctx->m.avctx->qmax, 1);
290
291 for (qscale = 1; qscale <= ctx->m.avctx->qmax; qscale++) {
292 for (i = 0; i < 64; i++) {
293 ctx->qmatrix_l[qscale][i] <<= 2;
294 ctx->qmatrix_c[qscale][i] <<= 2;
295 ctx->qmatrix_l16[qscale][0][i] <<= 2;
296 ctx->qmatrix_l16[qscale][1][i] <<= 2;
297 ctx->qmatrix_c16[qscale][0][i] <<= 2;
298 ctx->qmatrix_c16[qscale][1][i] <<= 2;
299 }
300 }
301 } else {
302 // 10-bit
303 for (qscale = 1; qscale <= ctx->m.avctx->qmax; qscale++) {
304 for (i = 1; i < 64; i++) {
305 int j = ff_zigzag_direct[i];
306
307 /* The quantization formula from the VC-3 standard is:
308 * quantized = sign(block[i]) * floor(abs(block[i]/s) * p /
309 * (qscale * weight_table[i]))
310 * Where p is 32 for 8-bit samples and 8 for 10-bit ones.
311 * The s factor compensates scaling of DCT coefficients done by
312 * the DCT routines, and therefore is not present in standard.
313 * It's 8 for 8-bit samples and 4 for 10-bit ones.
314 * We want values of ctx->qtmatrix_l and ctx->qtmatrix_r to be:
315 * ((1 << DNX10BIT_QMAT_SHIFT) * (p / s)) /
316 * (qscale * weight_table[i])
317 * For 10-bit samples, p / s == 2 */
318 ctx->qmatrix_l[qscale][j] = (1 << (DNX10BIT_QMAT_SHIFT + 1)) /
319 (qscale * luma_weight_table[i]);
320 ctx->qmatrix_c[qscale][j] = (1 << (DNX10BIT_QMAT_SHIFT + 1)) /
321 (qscale * chroma_weight_table[i]);
322 }
323 }
324 }
325
326 ctx->m.q_chroma_intra_matrix16 = ctx->qmatrix_c16;
327 ctx->m.q_chroma_intra_matrix = ctx->qmatrix_c;
328 ctx->m.q_intra_matrix16 = ctx->qmatrix_l16;
329 ctx->m.q_intra_matrix = ctx->qmatrix_l;
330
331 return 0;
332 }
333
dnxhd_init_rc(DNXHDEncContext * ctx)334 static av_cold int dnxhd_init_rc(DNXHDEncContext *ctx)
335 {
336 if (!FF_ALLOCZ_TYPED_ARRAY(ctx->mb_rc, (ctx->m.avctx->qmax + 1) * ctx->m.mb_num))
337 return AVERROR(ENOMEM);
338
339 if (ctx->m.avctx->mb_decision != FF_MB_DECISION_RD) {
340 if (!FF_ALLOCZ_TYPED_ARRAY(ctx->mb_cmp, ctx->m.mb_num) ||
341 !FF_ALLOCZ_TYPED_ARRAY(ctx->mb_cmp_tmp, ctx->m.mb_num))
342 return AVERROR(ENOMEM);
343 }
344 ctx->frame_bits = (ctx->coding_unit_size -
345 ctx->data_offset - 4 - ctx->min_padding) * 8;
346 ctx->qscale = 1;
347 ctx->lambda = 2 << LAMBDA_FRAC_BITS; // qscale 2
348 return 0;
349 }
350
dnxhd_encode_init(AVCodecContext * avctx)351 static av_cold int dnxhd_encode_init(AVCodecContext *avctx)
352 {
353 DNXHDEncContext *ctx = avctx->priv_data;
354 int i, ret;
355
356 switch (avctx->pix_fmt) {
357 case AV_PIX_FMT_YUV422P:
358 ctx->bit_depth = 8;
359 break;
360 case AV_PIX_FMT_YUV422P10:
361 case AV_PIX_FMT_YUV444P10:
362 case AV_PIX_FMT_GBRP10:
363 ctx->bit_depth = 10;
364 break;
365 default:
366 av_log(avctx, AV_LOG_ERROR,
367 "pixel format is incompatible with DNxHD\n");
368 return AVERROR(EINVAL);
369 }
370
371 if ((ctx->profile == FF_PROFILE_DNXHR_444 && (avctx->pix_fmt != AV_PIX_FMT_YUV444P10 &&
372 avctx->pix_fmt != AV_PIX_FMT_GBRP10)) ||
373 (ctx->profile != FF_PROFILE_DNXHR_444 && (avctx->pix_fmt == AV_PIX_FMT_YUV444P10 ||
374 avctx->pix_fmt == AV_PIX_FMT_GBRP10))) {
375 av_log(avctx, AV_LOG_ERROR,
376 "pixel format is incompatible with DNxHD profile\n");
377 return AVERROR(EINVAL);
378 }
379
380 if (ctx->profile == FF_PROFILE_DNXHR_HQX && avctx->pix_fmt != AV_PIX_FMT_YUV422P10) {
381 av_log(avctx, AV_LOG_ERROR,
382 "pixel format is incompatible with DNxHR HQX profile\n");
383 return AVERROR(EINVAL);
384 }
385
386 if ((ctx->profile == FF_PROFILE_DNXHR_LB ||
387 ctx->profile == FF_PROFILE_DNXHR_SQ ||
388 ctx->profile == FF_PROFILE_DNXHR_HQ) && avctx->pix_fmt != AV_PIX_FMT_YUV422P) {
389 av_log(avctx, AV_LOG_ERROR,
390 "pixel format is incompatible with DNxHR LB/SQ/HQ profile\n");
391 return AVERROR(EINVAL);
392 }
393
394 ctx->is_444 = ctx->profile == FF_PROFILE_DNXHR_444;
395 avctx->profile = ctx->profile;
396 ctx->cid = ff_dnxhd_find_cid(avctx, ctx->bit_depth);
397 if (!ctx->cid) {
398 av_log(avctx, AV_LOG_ERROR,
399 "video parameters incompatible with DNxHD. Valid DNxHD profiles:\n");
400 ff_dnxhd_print_profiles(avctx, AV_LOG_ERROR);
401 return AVERROR(EINVAL);
402 }
403 av_log(avctx, AV_LOG_DEBUG, "cid %d\n", ctx->cid);
404
405 if (ctx->cid >= 1270 && ctx->cid <= 1274)
406 avctx->codec_tag = MKTAG('A','V','d','h');
407
408 if (avctx->width < 256 || avctx->height < 120) {
409 av_log(avctx, AV_LOG_ERROR,
410 "Input dimensions too small, input must be at least 256x120\n");
411 return AVERROR(EINVAL);
412 }
413
414 ctx->cid_table = ff_dnxhd_get_cid_table(ctx->cid);
415 av_assert0(ctx->cid_table);
416
417 ctx->m.avctx = avctx;
418 ctx->m.mb_intra = 1;
419 ctx->m.h263_aic = 1;
420
421 avctx->bits_per_raw_sample = ctx->bit_depth;
422
423 ff_blockdsp_init(&ctx->bdsp, avctx);
424 ff_fdctdsp_init(&ctx->m.fdsp, avctx);
425 ff_mpv_idct_init(&ctx->m);
426 ff_mpegvideoencdsp_init(&ctx->m.mpvencdsp, avctx);
427 ff_pixblockdsp_init(&ctx->m.pdsp, avctx);
428 ff_dct_encode_init(&ctx->m);
429
430 if (ctx->profile != FF_PROFILE_DNXHD)
431 ff_videodsp_init(&ctx->m.vdsp, ctx->bit_depth);
432
433 if (!ctx->m.dct_quantize)
434 ctx->m.dct_quantize = ff_dct_quantize_c;
435
436 if (ctx->is_444 || ctx->profile == FF_PROFILE_DNXHR_HQX) {
437 ctx->m.dct_quantize = dnxhd_10bit_dct_quantize_444;
438 ctx->get_pixels_8x4_sym = dnxhd_10bit_get_pixels_8x4_sym;
439 ctx->block_width_l2 = 4;
440 } else if (ctx->bit_depth == 10) {
441 ctx->m.dct_quantize = dnxhd_10bit_dct_quantize;
442 ctx->get_pixels_8x4_sym = dnxhd_10bit_get_pixels_8x4_sym;
443 ctx->block_width_l2 = 4;
444 } else {
445 ctx->get_pixels_8x4_sym = dnxhd_8bit_get_pixels_8x4_sym;
446 ctx->block_width_l2 = 3;
447 }
448
449 if (ARCH_X86)
450 ff_dnxhdenc_init_x86(ctx);
451
452 ctx->m.mb_height = (avctx->height + 15) / 16;
453 ctx->m.mb_width = (avctx->width + 15) / 16;
454
455 if (avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) {
456 ctx->interlaced = 1;
457 ctx->m.mb_height /= 2;
458 }
459
460 if (ctx->interlaced && ctx->profile != FF_PROFILE_DNXHD) {
461 av_log(avctx, AV_LOG_ERROR,
462 "Interlaced encoding is not supported for DNxHR profiles.\n");
463 return AVERROR(EINVAL);
464 }
465
466 ctx->m.mb_num = ctx->m.mb_height * ctx->m.mb_width;
467
468 if (ctx->cid_table->frame_size == DNXHD_VARIABLE) {
469 ctx->frame_size = avpriv_dnxhd_get_hr_frame_size(ctx->cid,
470 avctx->width, avctx->height);
471 av_assert0(ctx->frame_size >= 0);
472 ctx->coding_unit_size = ctx->frame_size;
473 } else {
474 ctx->frame_size = ctx->cid_table->frame_size;
475 ctx->coding_unit_size = ctx->cid_table->coding_unit_size;
476 }
477
478 if (ctx->m.mb_height > 68)
479 ctx->data_offset = 0x170 + (ctx->m.mb_height << 2);
480 else
481 ctx->data_offset = 0x280;
482
483 // XXX tune lbias/cbias
484 if ((ret = dnxhd_init_qmat(ctx, ctx->intra_quant_bias, 0)) < 0)
485 return ret;
486
487 /* Avid Nitris hardware decoder requires a minimum amount of padding
488 * in the coding unit payload */
489 if (ctx->nitris_compat)
490 ctx->min_padding = 1600;
491
492 if ((ret = dnxhd_init_vlc(ctx)) < 0)
493 return ret;
494 if ((ret = dnxhd_init_rc(ctx)) < 0)
495 return ret;
496
497 if (!FF_ALLOCZ_TYPED_ARRAY(ctx->slice_size, ctx->m.mb_height) ||
498 !FF_ALLOCZ_TYPED_ARRAY(ctx->slice_offs, ctx->m.mb_height) ||
499 !FF_ALLOCZ_TYPED_ARRAY(ctx->mb_bits, ctx->m.mb_num) ||
500 !FF_ALLOCZ_TYPED_ARRAY(ctx->mb_qscale, ctx->m.mb_num))
501 return AVERROR(ENOMEM);
502 #if FF_API_CODED_FRAME
503 FF_DISABLE_DEPRECATION_WARNINGS
504 avctx->coded_frame->key_frame = 1;
505 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
506 FF_ENABLE_DEPRECATION_WARNINGS
507 #endif
508
509 if (avctx->active_thread_type == FF_THREAD_SLICE) {
510 if (avctx->thread_count > MAX_THREADS) {
511 av_log(avctx, AV_LOG_ERROR, "too many threads\n");
512 return AVERROR(EINVAL);
513 }
514 }
515
516 if (avctx->qmax <= 1) {
517 av_log(avctx, AV_LOG_ERROR, "qmax must be at least 2\n");
518 return AVERROR(EINVAL);
519 }
520
521 ctx->thread[0] = ctx;
522 if (avctx->active_thread_type == FF_THREAD_SLICE) {
523 for (i = 1; i < avctx->thread_count; i++) {
524 ctx->thread[i] = av_malloc(sizeof(DNXHDEncContext));
525 if (!ctx->thread[i])
526 return AVERROR(ENOMEM);
527 memcpy(ctx->thread[i], ctx, sizeof(DNXHDEncContext));
528 }
529 }
530
531 return 0;
532 }
533
dnxhd_write_header(AVCodecContext * avctx,uint8_t * buf)534 static int dnxhd_write_header(AVCodecContext *avctx, uint8_t *buf)
535 {
536 DNXHDEncContext *ctx = avctx->priv_data;
537
538 memset(buf, 0, ctx->data_offset);
539
540 // * write prefix */
541 AV_WB16(buf + 0x02, ctx->data_offset);
542 if (ctx->cid >= 1270 && ctx->cid <= 1274)
543 buf[4] = 0x03;
544 else
545 buf[4] = 0x01;
546
547 buf[5] = ctx->interlaced ? ctx->cur_field + 2 : 0x01;
548 buf[6] = 0x80; // crc flag off
549 buf[7] = 0xa0; // reserved
550 AV_WB16(buf + 0x18, avctx->height >> ctx->interlaced); // ALPF
551 AV_WB16(buf + 0x1a, avctx->width); // SPL
552 AV_WB16(buf + 0x1d, avctx->height >> ctx->interlaced); // NAL
553
554 buf[0x21] = ctx->bit_depth == 10 ? 0x58 : 0x38;
555 buf[0x22] = 0x88 + (ctx->interlaced << 2);
556 AV_WB32(buf + 0x28, ctx->cid); // CID
557 buf[0x2c] = (!ctx->interlaced << 7) | (ctx->is_444 << 6) | (avctx->pix_fmt == AV_PIX_FMT_YUV444P10);
558
559 buf[0x5f] = 0x01; // UDL
560
561 buf[0x167] = 0x02; // reserved
562 AV_WB16(buf + 0x16a, ctx->m.mb_height * 4 + 4); // MSIPS
563 AV_WB16(buf + 0x16c, ctx->m.mb_height); // Ns
564 buf[0x16f] = 0x10; // reserved
565
566 ctx->msip = buf + 0x170;
567 return 0;
568 }
569
dnxhd_encode_dc(DNXHDEncContext * ctx,int diff)570 static av_always_inline void dnxhd_encode_dc(DNXHDEncContext *ctx, int diff)
571 {
572 int nbits;
573 if (diff < 0) {
574 nbits = av_log2_16bit(-2 * diff);
575 diff--;
576 } else {
577 nbits = av_log2_16bit(2 * diff);
578 }
579 put_bits(&ctx->m.pb, ctx->cid_table->dc_bits[nbits] + nbits,
580 (ctx->cid_table->dc_codes[nbits] << nbits) +
581 av_mod_uintp2(diff, nbits));
582 }
583
584 static av_always_inline
dnxhd_encode_block(DNXHDEncContext * ctx,int16_t * block,int last_index,int n)585 void dnxhd_encode_block(DNXHDEncContext *ctx, int16_t *block,
586 int last_index, int n)
587 {
588 int last_non_zero = 0;
589 int slevel, i, j;
590
591 dnxhd_encode_dc(ctx, block[0] - ctx->m.last_dc[n]);
592 ctx->m.last_dc[n] = block[0];
593
594 for (i = 1; i <= last_index; i++) {
595 j = ctx->m.intra_scantable.permutated[i];
596 slevel = block[j];
597 if (slevel) {
598 int run_level = i - last_non_zero - 1;
599 int rlevel = slevel * (1 << 1) | !!run_level;
600 put_bits(&ctx->m.pb, ctx->vlc_bits[rlevel], ctx->vlc_codes[rlevel]);
601 if (run_level)
602 put_bits(&ctx->m.pb, ctx->run_bits[run_level],
603 ctx->run_codes[run_level]);
604 last_non_zero = i;
605 }
606 }
607 put_bits(&ctx->m.pb, ctx->vlc_bits[0], ctx->vlc_codes[0]); // EOB
608 }
609
610 static av_always_inline
dnxhd_unquantize_c(DNXHDEncContext * ctx,int16_t * block,int n,int qscale,int last_index)611 void dnxhd_unquantize_c(DNXHDEncContext *ctx, int16_t *block, int n,
612 int qscale, int last_index)
613 {
614 const uint8_t *weight_matrix;
615 int level;
616 int i;
617
618 if (ctx->is_444) {
619 weight_matrix = ((n % 6) < 2) ? ctx->cid_table->luma_weight
620 : ctx->cid_table->chroma_weight;
621 } else {
622 weight_matrix = (n & 2) ? ctx->cid_table->chroma_weight
623 : ctx->cid_table->luma_weight;
624 }
625
626 for (i = 1; i <= last_index; i++) {
627 int j = ctx->m.intra_scantable.permutated[i];
628 level = block[j];
629 if (level) {
630 if (level < 0) {
631 level = (1 - 2 * level) * qscale * weight_matrix[i];
632 if (ctx->bit_depth == 10) {
633 if (weight_matrix[i] != 8)
634 level += 8;
635 level >>= 4;
636 } else {
637 if (weight_matrix[i] != 32)
638 level += 32;
639 level >>= 6;
640 }
641 level = -level;
642 } else {
643 level = (2 * level + 1) * qscale * weight_matrix[i];
644 if (ctx->bit_depth == 10) {
645 if (weight_matrix[i] != 8)
646 level += 8;
647 level >>= 4;
648 } else {
649 if (weight_matrix[i] != 32)
650 level += 32;
651 level >>= 6;
652 }
653 }
654 block[j] = level;
655 }
656 }
657 }
658
dnxhd_ssd_block(int16_t * qblock,int16_t * block)659 static av_always_inline int dnxhd_ssd_block(int16_t *qblock, int16_t *block)
660 {
661 int score = 0;
662 int i;
663 for (i = 0; i < 64; i++)
664 score += (block[i] - qblock[i]) * (block[i] - qblock[i]);
665 return score;
666 }
667
668 static av_always_inline
dnxhd_calc_ac_bits(DNXHDEncContext * ctx,int16_t * block,int last_index)669 int dnxhd_calc_ac_bits(DNXHDEncContext *ctx, int16_t *block, int last_index)
670 {
671 int last_non_zero = 0;
672 int bits = 0;
673 int i, j, level;
674 for (i = 1; i <= last_index; i++) {
675 j = ctx->m.intra_scantable.permutated[i];
676 level = block[j];
677 if (level) {
678 int run_level = i - last_non_zero - 1;
679 bits += ctx->vlc_bits[level * (1 << 1) |
680 !!run_level] + ctx->run_bits[run_level];
681 last_non_zero = i;
682 }
683 }
684 return bits;
685 }
686
687 static av_always_inline
dnxhd_get_blocks(DNXHDEncContext * ctx,int mb_x,int mb_y)688 void dnxhd_get_blocks(DNXHDEncContext *ctx, int mb_x, int mb_y)
689 {
690 const int bs = ctx->block_width_l2;
691 const int bw = 1 << bs;
692 int dct_y_offset = ctx->dct_y_offset;
693 int dct_uv_offset = ctx->dct_uv_offset;
694 int linesize = ctx->m.linesize;
695 int uvlinesize = ctx->m.uvlinesize;
696 const uint8_t *ptr_y = ctx->thread[0]->src[0] +
697 ((mb_y << 4) * ctx->m.linesize) + (mb_x << bs + 1);
698 const uint8_t *ptr_u = ctx->thread[0]->src[1] +
699 ((mb_y << 4) * ctx->m.uvlinesize) + (mb_x << bs + ctx->is_444);
700 const uint8_t *ptr_v = ctx->thread[0]->src[2] +
701 ((mb_y << 4) * ctx->m.uvlinesize) + (mb_x << bs + ctx->is_444);
702 PixblockDSPContext *pdsp = &ctx->m.pdsp;
703 VideoDSPContext *vdsp = &ctx->m.vdsp;
704
705 if (ctx->bit_depth != 10 && vdsp->emulated_edge_mc && ((mb_x << 4) + 16 > ctx->m.avctx->width ||
706 (mb_y << 4) + 16 > ctx->m.avctx->height)) {
707 int y_w = ctx->m.avctx->width - (mb_x << 4);
708 int y_h = ctx->m.avctx->height - (mb_y << 4);
709 int uv_w = (y_w + 1) / 2;
710 int uv_h = y_h;
711 linesize = 16;
712 uvlinesize = 8;
713
714 vdsp->emulated_edge_mc(&ctx->edge_buf_y[0], ptr_y,
715 linesize, ctx->m.linesize,
716 linesize, 16,
717 0, 0, y_w, y_h);
718 vdsp->emulated_edge_mc(&ctx->edge_buf_uv[0][0], ptr_u,
719 uvlinesize, ctx->m.uvlinesize,
720 uvlinesize, 16,
721 0, 0, uv_w, uv_h);
722 vdsp->emulated_edge_mc(&ctx->edge_buf_uv[1][0], ptr_v,
723 uvlinesize, ctx->m.uvlinesize,
724 uvlinesize, 16,
725 0, 0, uv_w, uv_h);
726
727 dct_y_offset = bw * linesize;
728 dct_uv_offset = bw * uvlinesize;
729 ptr_y = &ctx->edge_buf_y[0];
730 ptr_u = &ctx->edge_buf_uv[0][0];
731 ptr_v = &ctx->edge_buf_uv[1][0];
732 } else if (ctx->bit_depth == 10 && vdsp->emulated_edge_mc && ((mb_x << 4) + 16 > ctx->m.avctx->width ||
733 (mb_y << 4) + 16 > ctx->m.avctx->height)) {
734 int y_w = ctx->m.avctx->width - (mb_x << 4);
735 int y_h = ctx->m.avctx->height - (mb_y << 4);
736 int uv_w = ctx->is_444 ? y_w : (y_w + 1) / 2;
737 int uv_h = y_h;
738 linesize = 32;
739 uvlinesize = 16 + 16 * ctx->is_444;
740
741 vdsp->emulated_edge_mc(&ctx->edge_buf_y[0], ptr_y,
742 linesize, ctx->m.linesize,
743 linesize / 2, 16,
744 0, 0, y_w, y_h);
745 vdsp->emulated_edge_mc(&ctx->edge_buf_uv[0][0], ptr_u,
746 uvlinesize, ctx->m.uvlinesize,
747 uvlinesize / 2, 16,
748 0, 0, uv_w, uv_h);
749 vdsp->emulated_edge_mc(&ctx->edge_buf_uv[1][0], ptr_v,
750 uvlinesize, ctx->m.uvlinesize,
751 uvlinesize / 2, 16,
752 0, 0, uv_w, uv_h);
753
754 dct_y_offset = bw * linesize / 2;
755 dct_uv_offset = bw * uvlinesize / 2;
756 ptr_y = &ctx->edge_buf_y[0];
757 ptr_u = &ctx->edge_buf_uv[0][0];
758 ptr_v = &ctx->edge_buf_uv[1][0];
759 }
760
761 if (!ctx->is_444) {
762 pdsp->get_pixels(ctx->blocks[0], ptr_y, linesize);
763 pdsp->get_pixels(ctx->blocks[1], ptr_y + bw, linesize);
764 pdsp->get_pixels(ctx->blocks[2], ptr_u, uvlinesize);
765 pdsp->get_pixels(ctx->blocks[3], ptr_v, uvlinesize);
766
767 if (mb_y + 1 == ctx->m.mb_height && ctx->m.avctx->height == 1080) {
768 if (ctx->interlaced) {
769 ctx->get_pixels_8x4_sym(ctx->blocks[4],
770 ptr_y + dct_y_offset,
771 linesize);
772 ctx->get_pixels_8x4_sym(ctx->blocks[5],
773 ptr_y + dct_y_offset + bw,
774 linesize);
775 ctx->get_pixels_8x4_sym(ctx->blocks[6],
776 ptr_u + dct_uv_offset,
777 uvlinesize);
778 ctx->get_pixels_8x4_sym(ctx->blocks[7],
779 ptr_v + dct_uv_offset,
780 uvlinesize);
781 } else {
782 ctx->bdsp.clear_block(ctx->blocks[4]);
783 ctx->bdsp.clear_block(ctx->blocks[5]);
784 ctx->bdsp.clear_block(ctx->blocks[6]);
785 ctx->bdsp.clear_block(ctx->blocks[7]);
786 }
787 } else {
788 pdsp->get_pixels(ctx->blocks[4],
789 ptr_y + dct_y_offset, linesize);
790 pdsp->get_pixels(ctx->blocks[5],
791 ptr_y + dct_y_offset + bw, linesize);
792 pdsp->get_pixels(ctx->blocks[6],
793 ptr_u + dct_uv_offset, uvlinesize);
794 pdsp->get_pixels(ctx->blocks[7],
795 ptr_v + dct_uv_offset, uvlinesize);
796 }
797 } else {
798 pdsp->get_pixels(ctx->blocks[0], ptr_y, linesize);
799 pdsp->get_pixels(ctx->blocks[1], ptr_y + bw, linesize);
800 pdsp->get_pixels(ctx->blocks[6], ptr_y + dct_y_offset, linesize);
801 pdsp->get_pixels(ctx->blocks[7], ptr_y + dct_y_offset + bw, linesize);
802
803 pdsp->get_pixels(ctx->blocks[2], ptr_u, uvlinesize);
804 pdsp->get_pixels(ctx->blocks[3], ptr_u + bw, uvlinesize);
805 pdsp->get_pixels(ctx->blocks[8], ptr_u + dct_uv_offset, uvlinesize);
806 pdsp->get_pixels(ctx->blocks[9], ptr_u + dct_uv_offset + bw, uvlinesize);
807
808 pdsp->get_pixels(ctx->blocks[4], ptr_v, uvlinesize);
809 pdsp->get_pixels(ctx->blocks[5], ptr_v + bw, uvlinesize);
810 pdsp->get_pixels(ctx->blocks[10], ptr_v + dct_uv_offset, uvlinesize);
811 pdsp->get_pixels(ctx->blocks[11], ptr_v + dct_uv_offset + bw, uvlinesize);
812 }
813 }
814
815 static av_always_inline
dnxhd_switch_matrix(DNXHDEncContext * ctx,int i)816 int dnxhd_switch_matrix(DNXHDEncContext *ctx, int i)
817 {
818 int x;
819
820 if (ctx->is_444) {
821 x = (i >> 1) % 3;
822 } else {
823 const static uint8_t component[8]={0,0,1,2,0,0,1,2};
824 x = component[i];
825 }
826 return x;
827 }
828
dnxhd_calc_bits_thread(AVCodecContext * avctx,void * arg,int jobnr,int threadnr)829 static int dnxhd_calc_bits_thread(AVCodecContext *avctx, void *arg,
830 int jobnr, int threadnr)
831 {
832 DNXHDEncContext *ctx = avctx->priv_data;
833 int mb_y = jobnr, mb_x;
834 int qscale = ctx->qscale;
835 LOCAL_ALIGNED_16(int16_t, block, [64]);
836 ctx = ctx->thread[threadnr];
837
838 ctx->m.last_dc[0] =
839 ctx->m.last_dc[1] =
840 ctx->m.last_dc[2] = 1 << (ctx->bit_depth + 2);
841
842 for (mb_x = 0; mb_x < ctx->m.mb_width; mb_x++) {
843 unsigned mb = mb_y * ctx->m.mb_width + mb_x;
844 int ssd = 0;
845 int ac_bits = 0;
846 int dc_bits = 0;
847 int i;
848
849 dnxhd_get_blocks(ctx, mb_x, mb_y);
850
851 for (i = 0; i < 8 + 4 * ctx->is_444; i++) {
852 int16_t *src_block = ctx->blocks[i];
853 int overflow, nbits, diff, last_index;
854 int n = dnxhd_switch_matrix(ctx, i);
855
856 memcpy(block, src_block, 64 * sizeof(*block));
857 last_index = ctx->m.dct_quantize(&ctx->m, block,
858 ctx->is_444 ? 4 * (n > 0): 4 & (2*i),
859 qscale, &overflow);
860 ac_bits += dnxhd_calc_ac_bits(ctx, block, last_index);
861
862 diff = block[0] - ctx->m.last_dc[n];
863 if (diff < 0)
864 nbits = av_log2_16bit(-2 * diff);
865 else
866 nbits = av_log2_16bit(2 * diff);
867
868 av_assert1(nbits < ctx->bit_depth + 4);
869 dc_bits += ctx->cid_table->dc_bits[nbits] + nbits;
870
871 ctx->m.last_dc[n] = block[0];
872
873 if (avctx->mb_decision == FF_MB_DECISION_RD || !RC_VARIANCE) {
874 dnxhd_unquantize_c(ctx, block, i, qscale, last_index);
875 ctx->m.idsp.idct(block);
876 ssd += dnxhd_ssd_block(block, src_block);
877 }
878 }
879 ctx->mb_rc[(qscale * ctx->m.mb_num) + mb].ssd = ssd;
880 ctx->mb_rc[(qscale * ctx->m.mb_num) + mb].bits = ac_bits + dc_bits + 12 +
881 (1 + ctx->is_444) * 8 * ctx->vlc_bits[0];
882 }
883 return 0;
884 }
885
dnxhd_encode_thread(AVCodecContext * avctx,void * arg,int jobnr,int threadnr)886 static int dnxhd_encode_thread(AVCodecContext *avctx, void *arg,
887 int jobnr, int threadnr)
888 {
889 DNXHDEncContext *ctx = avctx->priv_data;
890 int mb_y = jobnr, mb_x;
891 ctx = ctx->thread[threadnr];
892 init_put_bits(&ctx->m.pb, (uint8_t *)arg + ctx->data_offset + ctx->slice_offs[jobnr],
893 ctx->slice_size[jobnr]);
894
895 ctx->m.last_dc[0] =
896 ctx->m.last_dc[1] =
897 ctx->m.last_dc[2] = 1 << (ctx->bit_depth + 2);
898 for (mb_x = 0; mb_x < ctx->m.mb_width; mb_x++) {
899 unsigned mb = mb_y * ctx->m.mb_width + mb_x;
900 int qscale = ctx->mb_qscale[mb];
901 int i;
902
903 put_bits(&ctx->m.pb, 11, qscale);
904 put_bits(&ctx->m.pb, 1, avctx->pix_fmt == AV_PIX_FMT_YUV444P10);
905
906 dnxhd_get_blocks(ctx, mb_x, mb_y);
907
908 for (i = 0; i < 8 + 4 * ctx->is_444; i++) {
909 int16_t *block = ctx->blocks[i];
910 int overflow, n = dnxhd_switch_matrix(ctx, i);
911 int last_index = ctx->m.dct_quantize(&ctx->m, block,
912 ctx->is_444 ? (((i >> 1) % 3) < 1 ? 0 : 4): 4 & (2*i),
913 qscale, &overflow);
914
915 dnxhd_encode_block(ctx, block, last_index, n);
916 }
917 }
918 if (put_bits_count(&ctx->m.pb) & 31)
919 put_bits(&ctx->m.pb, 32 - (put_bits_count(&ctx->m.pb) & 31), 0);
920 flush_put_bits(&ctx->m.pb);
921 return 0;
922 }
923
dnxhd_setup_threads_slices(DNXHDEncContext * ctx)924 static void dnxhd_setup_threads_slices(DNXHDEncContext *ctx)
925 {
926 int mb_y, mb_x;
927 int offset = 0;
928 for (mb_y = 0; mb_y < ctx->m.mb_height; mb_y++) {
929 int thread_size;
930 ctx->slice_offs[mb_y] = offset;
931 ctx->slice_size[mb_y] = 0;
932 for (mb_x = 0; mb_x < ctx->m.mb_width; mb_x++) {
933 unsigned mb = mb_y * ctx->m.mb_width + mb_x;
934 ctx->slice_size[mb_y] += ctx->mb_bits[mb];
935 }
936 ctx->slice_size[mb_y] = (ctx->slice_size[mb_y] + 31) & ~31;
937 ctx->slice_size[mb_y] >>= 3;
938 thread_size = ctx->slice_size[mb_y];
939 offset += thread_size;
940 }
941 }
942
dnxhd_mb_var_thread(AVCodecContext * avctx,void * arg,int jobnr,int threadnr)943 static int dnxhd_mb_var_thread(AVCodecContext *avctx, void *arg,
944 int jobnr, int threadnr)
945 {
946 DNXHDEncContext *ctx = avctx->priv_data;
947 int mb_y = jobnr, mb_x, x, y;
948 int partial_last_row = (mb_y == ctx->m.mb_height - 1) &&
949 ((avctx->height >> ctx->interlaced) & 0xF);
950
951 ctx = ctx->thread[threadnr];
952 if (ctx->bit_depth == 8) {
953 uint8_t *pix = ctx->thread[0]->src[0] + ((mb_y << 4) * ctx->m.linesize);
954 for (mb_x = 0; mb_x < ctx->m.mb_width; ++mb_x, pix += 16) {
955 unsigned mb = mb_y * ctx->m.mb_width + mb_x;
956 int sum;
957 int varc;
958
959 if (!partial_last_row && mb_x * 16 <= avctx->width - 16 && (avctx->width % 16) == 0) {
960 sum = ctx->m.mpvencdsp.pix_sum(pix, ctx->m.linesize);
961 varc = ctx->m.mpvencdsp.pix_norm1(pix, ctx->m.linesize);
962 } else {
963 int bw = FFMIN(avctx->width - 16 * mb_x, 16);
964 int bh = FFMIN((avctx->height >> ctx->interlaced) - 16 * mb_y, 16);
965 sum = varc = 0;
966 for (y = 0; y < bh; y++) {
967 for (x = 0; x < bw; x++) {
968 uint8_t val = pix[x + y * ctx->m.linesize];
969 sum += val;
970 varc += val * val;
971 }
972 }
973 }
974 varc = (varc - (((unsigned) sum * sum) >> 8) + 128) >> 8;
975
976 ctx->mb_cmp[mb].value = varc;
977 ctx->mb_cmp[mb].mb = mb;
978 }
979 } else { // 10-bit
980 const int linesize = ctx->m.linesize >> 1;
981 for (mb_x = 0; mb_x < ctx->m.mb_width; ++mb_x) {
982 uint16_t *pix = (uint16_t *)ctx->thread[0]->src[0] +
983 ((mb_y << 4) * linesize) + (mb_x << 4);
984 unsigned mb = mb_y * ctx->m.mb_width + mb_x;
985 int sum = 0;
986 int sqsum = 0;
987 int bw = FFMIN(avctx->width - 16 * mb_x, 16);
988 int bh = FFMIN((avctx->height >> ctx->interlaced) - 16 * mb_y, 16);
989 int mean, sqmean;
990 int i, j;
991 // Macroblocks are 16x16 pixels, unlike DCT blocks which are 8x8.
992 for (i = 0; i < bh; ++i) {
993 for (j = 0; j < bw; ++j) {
994 // Turn 16-bit pixels into 10-bit ones.
995 const int sample = (unsigned) pix[j] >> 6;
996 sum += sample;
997 sqsum += sample * sample;
998 // 2^10 * 2^10 * 16 * 16 = 2^28, which is less than INT_MAX
999 }
1000 pix += linesize;
1001 }
1002 mean = sum >> 8; // 16*16 == 2^8
1003 sqmean = sqsum >> 8;
1004 ctx->mb_cmp[mb].value = sqmean - mean * mean;
1005 ctx->mb_cmp[mb].mb = mb;
1006 }
1007 }
1008 return 0;
1009 }
1010
dnxhd_encode_rdo(AVCodecContext * avctx,DNXHDEncContext * ctx)1011 static int dnxhd_encode_rdo(AVCodecContext *avctx, DNXHDEncContext *ctx)
1012 {
1013 int lambda, up_step, down_step;
1014 int last_lower = INT_MAX, last_higher = 0;
1015 int x, y, q;
1016
1017 for (q = 1; q < avctx->qmax; q++) {
1018 ctx->qscale = q;
1019 avctx->execute2(avctx, dnxhd_calc_bits_thread,
1020 NULL, NULL, ctx->m.mb_height);
1021 }
1022 up_step = down_step = 2 << LAMBDA_FRAC_BITS;
1023 lambda = ctx->lambda;
1024
1025 for (;;) {
1026 int bits = 0;
1027 int end = 0;
1028 if (lambda == last_higher) {
1029 lambda++;
1030 end = 1; // need to set final qscales/bits
1031 }
1032 for (y = 0; y < ctx->m.mb_height; y++) {
1033 for (x = 0; x < ctx->m.mb_width; x++) {
1034 unsigned min = UINT_MAX;
1035 int qscale = 1;
1036 int mb = y * ctx->m.mb_width + x;
1037 int rc = 0;
1038 for (q = 1; q < avctx->qmax; q++) {
1039 int i = (q*ctx->m.mb_num) + mb;
1040 unsigned score = ctx->mb_rc[i].bits * lambda +
1041 ((unsigned) ctx->mb_rc[i].ssd << LAMBDA_FRAC_BITS);
1042 if (score < min) {
1043 min = score;
1044 qscale = q;
1045 rc = i;
1046 }
1047 }
1048 bits += ctx->mb_rc[rc].bits;
1049 ctx->mb_qscale[mb] = qscale;
1050 ctx->mb_bits[mb] = ctx->mb_rc[rc].bits;
1051 }
1052 bits = (bits + 31) & ~31; // padding
1053 if (bits > ctx->frame_bits)
1054 break;
1055 }
1056 if (end) {
1057 if (bits > ctx->frame_bits)
1058 return AVERROR(EINVAL);
1059 break;
1060 }
1061 if (bits < ctx->frame_bits) {
1062 last_lower = FFMIN(lambda, last_lower);
1063 if (last_higher != 0)
1064 lambda = (lambda+last_higher)>>1;
1065 else
1066 lambda -= down_step;
1067 down_step = FFMIN((int64_t)down_step*5, INT_MAX);
1068 up_step = 1<<LAMBDA_FRAC_BITS;
1069 lambda = FFMAX(1, lambda);
1070 if (lambda == last_lower)
1071 break;
1072 } else {
1073 last_higher = FFMAX(lambda, last_higher);
1074 if (last_lower != INT_MAX)
1075 lambda = (lambda+last_lower)>>1;
1076 else if ((int64_t)lambda + up_step > INT_MAX)
1077 return AVERROR(EINVAL);
1078 else
1079 lambda += up_step;
1080 up_step = FFMIN((int64_t)up_step*5, INT_MAX);
1081 down_step = 1<<LAMBDA_FRAC_BITS;
1082 }
1083 }
1084 ctx->lambda = lambda;
1085 return 0;
1086 }
1087
dnxhd_find_qscale(DNXHDEncContext * ctx)1088 static int dnxhd_find_qscale(DNXHDEncContext *ctx)
1089 {
1090 int bits = 0;
1091 int up_step = 1;
1092 int down_step = 1;
1093 int last_higher = 0;
1094 int last_lower = INT_MAX;
1095 int qscale;
1096 int x, y;
1097
1098 qscale = ctx->qscale;
1099 for (;;) {
1100 bits = 0;
1101 ctx->qscale = qscale;
1102 // XXX avoid recalculating bits
1103 ctx->m.avctx->execute2(ctx->m.avctx, dnxhd_calc_bits_thread,
1104 NULL, NULL, ctx->m.mb_height);
1105 for (y = 0; y < ctx->m.mb_height; y++) {
1106 for (x = 0; x < ctx->m.mb_width; x++)
1107 bits += ctx->mb_rc[(qscale*ctx->m.mb_num) + (y*ctx->m.mb_width+x)].bits;
1108 bits = (bits+31)&~31; // padding
1109 if (bits > ctx->frame_bits)
1110 break;
1111 }
1112 if (bits < ctx->frame_bits) {
1113 if (qscale == 1)
1114 return 1;
1115 if (last_higher == qscale - 1) {
1116 qscale = last_higher;
1117 break;
1118 }
1119 last_lower = FFMIN(qscale, last_lower);
1120 if (last_higher != 0)
1121 qscale = (qscale + last_higher) >> 1;
1122 else
1123 qscale -= down_step++;
1124 if (qscale < 1)
1125 qscale = 1;
1126 up_step = 1;
1127 } else {
1128 if (last_lower == qscale + 1)
1129 break;
1130 last_higher = FFMAX(qscale, last_higher);
1131 if (last_lower != INT_MAX)
1132 qscale = (qscale + last_lower) >> 1;
1133 else
1134 qscale += up_step++;
1135 down_step = 1;
1136 if (qscale >= ctx->m.avctx->qmax)
1137 return AVERROR(EINVAL);
1138 }
1139 }
1140 ctx->qscale = qscale;
1141 return 0;
1142 }
1143
1144 #define BUCKET_BITS 8
1145 #define RADIX_PASSES 4
1146 #define NBUCKETS (1 << BUCKET_BITS)
1147
get_bucket(int value,int shift)1148 static inline int get_bucket(int value, int shift)
1149 {
1150 value >>= shift;
1151 value &= NBUCKETS - 1;
1152 return NBUCKETS - 1 - value;
1153 }
1154
radix_count(const RCCMPEntry * data,int size,int buckets[RADIX_PASSES][NBUCKETS])1155 static void radix_count(const RCCMPEntry *data, int size,
1156 int buckets[RADIX_PASSES][NBUCKETS])
1157 {
1158 int i, j;
1159 memset(buckets, 0, sizeof(buckets[0][0]) * RADIX_PASSES * NBUCKETS);
1160 for (i = 0; i < size; i++) {
1161 int v = data[i].value;
1162 for (j = 0; j < RADIX_PASSES; j++) {
1163 buckets[j][get_bucket(v, 0)]++;
1164 v >>= BUCKET_BITS;
1165 }
1166 av_assert1(!v);
1167 }
1168 for (j = 0; j < RADIX_PASSES; j++) {
1169 int offset = size;
1170 for (i = NBUCKETS - 1; i >= 0; i--)
1171 buckets[j][i] = offset -= buckets[j][i];
1172 av_assert1(!buckets[j][0]);
1173 }
1174 }
1175
radix_sort_pass(RCCMPEntry * dst,const RCCMPEntry * data,int size,int buckets[NBUCKETS],int pass)1176 static void radix_sort_pass(RCCMPEntry *dst, const RCCMPEntry *data,
1177 int size, int buckets[NBUCKETS], int pass)
1178 {
1179 int shift = pass * BUCKET_BITS;
1180 int i;
1181 for (i = 0; i < size; i++) {
1182 int v = get_bucket(data[i].value, shift);
1183 int pos = buckets[v]++;
1184 dst[pos] = data[i];
1185 }
1186 }
1187
radix_sort(RCCMPEntry * data,RCCMPEntry * tmp,int size)1188 static void radix_sort(RCCMPEntry *data, RCCMPEntry *tmp, int size)
1189 {
1190 int buckets[RADIX_PASSES][NBUCKETS];
1191 radix_count(data, size, buckets);
1192 radix_sort_pass(tmp, data, size, buckets[0], 0);
1193 radix_sort_pass(data, tmp, size, buckets[1], 1);
1194 if (buckets[2][NBUCKETS - 1] || buckets[3][NBUCKETS - 1]) {
1195 radix_sort_pass(tmp, data, size, buckets[2], 2);
1196 radix_sort_pass(data, tmp, size, buckets[3], 3);
1197 }
1198 }
1199
dnxhd_encode_fast(AVCodecContext * avctx,DNXHDEncContext * ctx)1200 static int dnxhd_encode_fast(AVCodecContext *avctx, DNXHDEncContext *ctx)
1201 {
1202 int max_bits = 0;
1203 int ret, x, y;
1204 if ((ret = dnxhd_find_qscale(ctx)) < 0)
1205 return ret;
1206 for (y = 0; y < ctx->m.mb_height; y++) {
1207 for (x = 0; x < ctx->m.mb_width; x++) {
1208 int mb = y * ctx->m.mb_width + x;
1209 int rc = (ctx->qscale * ctx->m.mb_num ) + mb;
1210 int delta_bits;
1211 ctx->mb_qscale[mb] = ctx->qscale;
1212 ctx->mb_bits[mb] = ctx->mb_rc[rc].bits;
1213 max_bits += ctx->mb_rc[rc].bits;
1214 if (!RC_VARIANCE) {
1215 delta_bits = ctx->mb_rc[rc].bits -
1216 ctx->mb_rc[rc + ctx->m.mb_num].bits;
1217 ctx->mb_cmp[mb].mb = mb;
1218 ctx->mb_cmp[mb].value =
1219 delta_bits ? ((ctx->mb_rc[rc].ssd -
1220 ctx->mb_rc[rc + ctx->m.mb_num].ssd) * 100) /
1221 delta_bits
1222 : INT_MIN; // avoid increasing qscale
1223 }
1224 }
1225 max_bits += 31; // worst padding
1226 }
1227 if (!ret) {
1228 if (RC_VARIANCE)
1229 avctx->execute2(avctx, dnxhd_mb_var_thread,
1230 NULL, NULL, ctx->m.mb_height);
1231 radix_sort(ctx->mb_cmp, ctx->mb_cmp_tmp, ctx->m.mb_num);
1232 for (x = 0; x < ctx->m.mb_num && max_bits > ctx->frame_bits; x++) {
1233 int mb = ctx->mb_cmp[x].mb;
1234 int rc = (ctx->qscale * ctx->m.mb_num ) + mb;
1235 max_bits -= ctx->mb_rc[rc].bits -
1236 ctx->mb_rc[rc + ctx->m.mb_num].bits;
1237 ctx->mb_qscale[mb] = ctx->qscale + 1;
1238 ctx->mb_bits[mb] = ctx->mb_rc[rc + ctx->m.mb_num].bits;
1239 }
1240 }
1241 return 0;
1242 }
1243
dnxhd_load_picture(DNXHDEncContext * ctx,const AVFrame * frame)1244 static void dnxhd_load_picture(DNXHDEncContext *ctx, const AVFrame *frame)
1245 {
1246 int i;
1247
1248 for (i = 0; i < ctx->m.avctx->thread_count; i++) {
1249 ctx->thread[i]->m.linesize = frame->linesize[0] << ctx->interlaced;
1250 ctx->thread[i]->m.uvlinesize = frame->linesize[1] << ctx->interlaced;
1251 ctx->thread[i]->dct_y_offset = ctx->m.linesize *8;
1252 ctx->thread[i]->dct_uv_offset = ctx->m.uvlinesize*8;
1253 }
1254
1255 #if FF_API_CODED_FRAME
1256 FF_DISABLE_DEPRECATION_WARNINGS
1257 ctx->m.avctx->coded_frame->interlaced_frame = frame->interlaced_frame;
1258 FF_ENABLE_DEPRECATION_WARNINGS
1259 #endif
1260 ctx->cur_field = frame->interlaced_frame && !frame->top_field_first;
1261 }
1262
dnxhd_encode_picture(AVCodecContext * avctx,AVPacket * pkt,const AVFrame * frame,int * got_packet)1263 static int dnxhd_encode_picture(AVCodecContext *avctx, AVPacket *pkt,
1264 const AVFrame *frame, int *got_packet)
1265 {
1266 DNXHDEncContext *ctx = avctx->priv_data;
1267 int first_field = 1;
1268 int offset, i, ret;
1269 uint8_t *buf;
1270
1271 if ((ret = ff_alloc_packet2(avctx, pkt, ctx->frame_size, 0)) < 0)
1272 return ret;
1273 buf = pkt->data;
1274
1275 dnxhd_load_picture(ctx, frame);
1276
1277 encode_coding_unit:
1278 for (i = 0; i < 3; i++) {
1279 ctx->src[i] = frame->data[i];
1280 if (ctx->interlaced && ctx->cur_field)
1281 ctx->src[i] += frame->linesize[i];
1282 }
1283
1284 dnxhd_write_header(avctx, buf);
1285
1286 if (avctx->mb_decision == FF_MB_DECISION_RD)
1287 ret = dnxhd_encode_rdo(avctx, ctx);
1288 else
1289 ret = dnxhd_encode_fast(avctx, ctx);
1290 if (ret < 0) {
1291 av_log(avctx, AV_LOG_ERROR,
1292 "picture could not fit ratecontrol constraints, increase qmax\n");
1293 return ret;
1294 }
1295
1296 dnxhd_setup_threads_slices(ctx);
1297
1298 offset = 0;
1299 for (i = 0; i < ctx->m.mb_height; i++) {
1300 AV_WB32(ctx->msip + i * 4, offset);
1301 offset += ctx->slice_size[i];
1302 av_assert1(!(ctx->slice_size[i] & 3));
1303 }
1304
1305 avctx->execute2(avctx, dnxhd_encode_thread, buf, NULL, ctx->m.mb_height);
1306
1307 av_assert1(ctx->data_offset + offset + 4 <= ctx->coding_unit_size);
1308 memset(buf + ctx->data_offset + offset, 0,
1309 ctx->coding_unit_size - 4 - offset - ctx->data_offset);
1310
1311 AV_WB32(buf + ctx->coding_unit_size - 4, 0x600DC0DE); // EOF
1312
1313 if (ctx->interlaced && first_field) {
1314 first_field = 0;
1315 ctx->cur_field ^= 1;
1316 buf += ctx->coding_unit_size;
1317 goto encode_coding_unit;
1318 }
1319
1320 #if FF_API_CODED_FRAME
1321 FF_DISABLE_DEPRECATION_WARNINGS
1322 avctx->coded_frame->quality = ctx->qscale * FF_QP2LAMBDA;
1323 FF_ENABLE_DEPRECATION_WARNINGS
1324 #endif
1325
1326 ff_side_data_set_encoder_stats(pkt, ctx->qscale * FF_QP2LAMBDA, NULL, 0, AV_PICTURE_TYPE_I);
1327
1328 pkt->flags |= AV_PKT_FLAG_KEY;
1329 *got_packet = 1;
1330 return 0;
1331 }
1332
dnxhd_encode_end(AVCodecContext * avctx)1333 static av_cold int dnxhd_encode_end(AVCodecContext *avctx)
1334 {
1335 DNXHDEncContext *ctx = avctx->priv_data;
1336 int i;
1337
1338 av_freep(&ctx->orig_vlc_codes);
1339 av_freep(&ctx->orig_vlc_bits);
1340 av_freep(&ctx->run_codes);
1341 av_freep(&ctx->run_bits);
1342
1343 av_freep(&ctx->mb_bits);
1344 av_freep(&ctx->mb_qscale);
1345 av_freep(&ctx->mb_rc);
1346 av_freep(&ctx->mb_cmp);
1347 av_freep(&ctx->mb_cmp_tmp);
1348 av_freep(&ctx->slice_size);
1349 av_freep(&ctx->slice_offs);
1350
1351 av_freep(&ctx->qmatrix_c);
1352 av_freep(&ctx->qmatrix_l);
1353 av_freep(&ctx->qmatrix_c16);
1354 av_freep(&ctx->qmatrix_l16);
1355
1356 if (avctx->active_thread_type == FF_THREAD_SLICE) {
1357 for (i = 1; i < avctx->thread_count; i++)
1358 av_freep(&ctx->thread[i]);
1359 }
1360
1361 return 0;
1362 }
1363
1364 static const AVCodecDefault dnxhd_defaults[] = {
1365 { "qmax", "1024" }, /* Maximum quantization scale factor allowed for VC-3 */
1366 { NULL },
1367 };
1368
1369 AVCodec ff_dnxhd_encoder = {
1370 .name = "dnxhd",
1371 .long_name = NULL_IF_CONFIG_SMALL("VC3/DNxHD"),
1372 .type = AVMEDIA_TYPE_VIDEO,
1373 .id = AV_CODEC_ID_DNXHD,
1374 .priv_data_size = sizeof(DNXHDEncContext),
1375 .init = dnxhd_encode_init,
1376 .encode2 = dnxhd_encode_picture,
1377 .close = dnxhd_encode_end,
1378 .capabilities = AV_CODEC_CAP_SLICE_THREADS | AV_CODEC_CAP_FRAME_THREADS,
1379 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
1380 .pix_fmts = (const enum AVPixelFormat[]) {
1381 AV_PIX_FMT_YUV422P,
1382 AV_PIX_FMT_YUV422P10,
1383 AV_PIX_FMT_YUV444P10,
1384 AV_PIX_FMT_GBRP10,
1385 AV_PIX_FMT_NONE
1386 },
1387 .priv_class = &dnxhd_class,
1388 .defaults = dnxhd_defaults,
1389 .profiles = NULL_IF_CONFIG_SMALL(ff_dnxhd_profiles),
1390 };
1391