FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/dnxhdenc.c
Date: 2022-01-21 12:56:39
Exec Total Coverage
Lines: 675 793 85.1%
Branches: 305 415 73.5%

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