FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/proresenc_kostya.c
Date: 2024-02-16 17:37:06
Exec Total Coverage
Lines: 455 641 71.0%
Functions: 16 22 72.7%
Branches: 174 296 58.8%

Line Branch Exec Source
1 /*
2 * Apple ProRes encoder
3 *
4 * Copyright (c) 2011 Anatoliy Wasserman
5 * Copyright (c) 2012 Konstantin Shishkov
6 *
7 * This encoder appears to be based on Anatoliy Wassermans considering
8 * similarities in the bugs.
9 *
10 * This file is part of FFmpeg.
11 *
12 * FFmpeg is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
16 *
17 * FFmpeg is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with FFmpeg; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 */
26
27 #include "libavutil/mem_internal.h"
28 #include "libavutil/opt.h"
29 #include "libavutil/pixdesc.h"
30 #include "avcodec.h"
31 #include "codec_internal.h"
32 #include "encode.h"
33 #include "fdctdsp.h"
34 #include "put_bits.h"
35 #include "profiles.h"
36 #include "bytestream.h"
37 #include "proresdata.h"
38
39 #define CFACTOR_Y422 2
40 #define CFACTOR_Y444 3
41
42 #define MAX_MBS_PER_SLICE 8
43
44 #define MAX_PLANES 4
45
46 enum {
47 PRORES_PROFILE_AUTO = -1,
48 PRORES_PROFILE_PROXY = 0,
49 PRORES_PROFILE_LT,
50 PRORES_PROFILE_STANDARD,
51 PRORES_PROFILE_HQ,
52 PRORES_PROFILE_4444,
53 PRORES_PROFILE_4444XQ,
54 };
55
56 enum {
57 QUANT_MAT_PROXY = 0,
58 QUANT_MAT_PROXY_CHROMA,
59 QUANT_MAT_LT,
60 QUANT_MAT_STANDARD,
61 QUANT_MAT_HQ,
62 QUANT_MAT_XQ_LUMA,
63 QUANT_MAT_DEFAULT,
64 };
65
66 static const uint8_t prores_quant_matrices[][64] = {
67 { // proxy
68 4, 7, 9, 11, 13, 14, 15, 63,
69 7, 7, 11, 12, 14, 15, 63, 63,
70 9, 11, 13, 14, 15, 63, 63, 63,
71 11, 11, 13, 14, 63, 63, 63, 63,
72 11, 13, 14, 63, 63, 63, 63, 63,
73 13, 14, 63, 63, 63, 63, 63, 63,
74 13, 63, 63, 63, 63, 63, 63, 63,
75 63, 63, 63, 63, 63, 63, 63, 63,
76 },
77 { // proxy chromas
78 4, 7, 9, 11, 13, 14, 63, 63,
79 7, 7, 11, 12, 14, 63, 63, 63,
80 9, 11, 13, 14, 63, 63, 63, 63,
81 11, 11, 13, 14, 63, 63, 63, 63,
82 11, 13, 14, 63, 63, 63, 63, 63,
83 13, 14, 63, 63, 63, 63, 63, 63,
84 13, 63, 63, 63, 63, 63, 63, 63,
85 63, 63, 63, 63, 63, 63, 63, 63
86 },
87 { // LT
88 4, 5, 6, 7, 9, 11, 13, 15,
89 5, 5, 7, 8, 11, 13, 15, 17,
90 6, 7, 9, 11, 13, 15, 15, 17,
91 7, 7, 9, 11, 13, 15, 17, 19,
92 7, 9, 11, 13, 14, 16, 19, 23,
93 9, 11, 13, 14, 16, 19, 23, 29,
94 9, 11, 13, 15, 17, 21, 28, 35,
95 11, 13, 16, 17, 21, 28, 35, 41,
96 },
97 { // standard
98 4, 4, 5, 5, 6, 7, 7, 9,
99 4, 4, 5, 6, 7, 7, 9, 9,
100 5, 5, 6, 7, 7, 9, 9, 10,
101 5, 5, 6, 7, 7, 9, 9, 10,
102 5, 6, 7, 7, 8, 9, 10, 12,
103 6, 7, 7, 8, 9, 10, 12, 15,
104 6, 7, 7, 9, 10, 11, 14, 17,
105 7, 7, 9, 10, 11, 14, 17, 21,
106 },
107 { // high quality
108 4, 4, 4, 4, 4, 4, 4, 4,
109 4, 4, 4, 4, 4, 4, 4, 4,
110 4, 4, 4, 4, 4, 4, 4, 4,
111 4, 4, 4, 4, 4, 4, 4, 5,
112 4, 4, 4, 4, 4, 4, 5, 5,
113 4, 4, 4, 4, 4, 5, 5, 6,
114 4, 4, 4, 4, 5, 5, 6, 7,
115 4, 4, 4, 4, 5, 6, 7, 7,
116 },
117 { // XQ luma
118 2, 2, 2, 2, 2, 2, 2, 2,
119 2, 2, 2, 2, 2, 2, 2, 2,
120 2, 2, 2, 2, 2, 2, 2, 2,
121 2, 2, 2, 2, 2, 2, 2, 3,
122 2, 2, 2, 2, 2, 2, 3, 3,
123 2, 2, 2, 2, 2, 3, 3, 3,
124 2, 2, 2, 2, 3, 3, 3, 4,
125 2, 2, 2, 2, 3, 3, 4, 4,
126 },
127 { // codec default
128 4, 4, 4, 4, 4, 4, 4, 4,
129 4, 4, 4, 4, 4, 4, 4, 4,
130 4, 4, 4, 4, 4, 4, 4, 4,
131 4, 4, 4, 4, 4, 4, 4, 4,
132 4, 4, 4, 4, 4, 4, 4, 4,
133 4, 4, 4, 4, 4, 4, 4, 4,
134 4, 4, 4, 4, 4, 4, 4, 4,
135 4, 4, 4, 4, 4, 4, 4, 4,
136 },
137 };
138
139 #define NUM_MB_LIMITS 4
140 static const int prores_mb_limits[NUM_MB_LIMITS] = {
141 1620, // up to 720x576
142 2700, // up to 960x720
143 6075, // up to 1440x1080
144 9216, // up to 2048x1152
145 };
146
147 static const struct prores_profile {
148 const char *full_name;
149 uint32_t tag;
150 int min_quant;
151 int max_quant;
152 int br_tab[NUM_MB_LIMITS];
153 int quant;
154 int quant_chroma;
155 } prores_profile_info[6] = {
156 {
157 .full_name = "proxy",
158 .tag = MKTAG('a', 'p', 'c', 'o'),
159 .min_quant = 4,
160 .max_quant = 8,
161 .br_tab = { 300, 242, 220, 194 },
162 .quant = QUANT_MAT_PROXY,
163 .quant_chroma = QUANT_MAT_PROXY_CHROMA,
164 },
165 {
166 .full_name = "LT",
167 .tag = MKTAG('a', 'p', 'c', 's'),
168 .min_quant = 1,
169 .max_quant = 9,
170 .br_tab = { 720, 560, 490, 440 },
171 .quant = QUANT_MAT_LT,
172 .quant_chroma = QUANT_MAT_LT,
173 },
174 {
175 .full_name = "standard",
176 .tag = MKTAG('a', 'p', 'c', 'n'),
177 .min_quant = 1,
178 .max_quant = 6,
179 .br_tab = { 1050, 808, 710, 632 },
180 .quant = QUANT_MAT_STANDARD,
181 .quant_chroma = QUANT_MAT_STANDARD,
182 },
183 {
184 .full_name = "high quality",
185 .tag = MKTAG('a', 'p', 'c', 'h'),
186 .min_quant = 1,
187 .max_quant = 6,
188 .br_tab = { 1566, 1216, 1070, 950 },
189 .quant = QUANT_MAT_HQ,
190 .quant_chroma = QUANT_MAT_HQ,
191 },
192 {
193 .full_name = "4444",
194 .tag = MKTAG('a', 'p', '4', 'h'),
195 .min_quant = 1,
196 .max_quant = 6,
197 .br_tab = { 2350, 1828, 1600, 1425 },
198 .quant = QUANT_MAT_HQ,
199 .quant_chroma = QUANT_MAT_HQ,
200 },
201 {
202 .full_name = "4444XQ",
203 .tag = MKTAG('a', 'p', '4', 'x'),
204 .min_quant = 1,
205 .max_quant = 6,
206 .br_tab = { 3525, 2742, 2400, 2137 },
207 .quant = QUANT_MAT_HQ, /* Fix me : use QUANT_MAT_XQ_LUMA */
208 .quant_chroma = QUANT_MAT_HQ,
209 }
210 };
211
212 #define TRELLIS_WIDTH 16
213 #define SCORE_LIMIT INT_MAX / 2
214
215 struct TrellisNode {
216 int prev_node;
217 int quant;
218 int bits;
219 int score;
220 };
221
222 #define MAX_STORED_Q 16
223
224 typedef struct ProresThreadData {
225 DECLARE_ALIGNED(16, int16_t, blocks)[MAX_PLANES][64 * 4 * MAX_MBS_PER_SLICE];
226 DECLARE_ALIGNED(16, uint16_t, emu_buf)[16 * 16];
227 int16_t custom_q[64];
228 int16_t custom_chroma_q[64];
229 struct TrellisNode *nodes;
230 } ProresThreadData;
231
232 typedef struct ProresContext {
233 AVClass *class;
234 DECLARE_ALIGNED(16, int16_t, blocks)[MAX_PLANES][64 * 4 * MAX_MBS_PER_SLICE];
235 DECLARE_ALIGNED(16, uint16_t, emu_buf)[16*16];
236 int16_t quants[MAX_STORED_Q][64];
237 int16_t quants_chroma[MAX_STORED_Q][64];
238 int16_t custom_q[64];
239 int16_t custom_chroma_q[64];
240 const uint8_t *quant_mat;
241 const uint8_t *quant_chroma_mat;
242 const uint8_t *scantable;
243
244 void (*fdct)(FDCTDSPContext *fdsp, const uint16_t *src,
245 ptrdiff_t linesize, int16_t *block);
246 FDCTDSPContext fdsp;
247
248 const AVFrame *pic;
249 int mb_width, mb_height;
250 int mbs_per_slice;
251 int num_chroma_blocks, chroma_factor;
252 int slices_width;
253 int slices_per_picture;
254 int pictures_per_frame; // 1 for progressive, 2 for interlaced
255 int cur_picture_idx;
256 int num_planes;
257 int bits_per_mb;
258 int force_quant;
259 int alpha_bits;
260 int warn;
261
262 char *vendor;
263 int quant_sel;
264
265 int frame_size_upper_bound;
266
267 int profile;
268 const struct prores_profile *profile_info;
269
270 int *slice_q;
271
272 ProresThreadData *tdata;
273 } ProresContext;
274
275 66600 static void get_slice_data(ProresContext *ctx, const uint16_t *src,
276 ptrdiff_t linesize, int x, int y, int w, int h,
277 int16_t *blocks, uint16_t *emu_buf,
278 int mbs_per_slice, int blocks_per_mb, int is_chroma)
279 {
280 const uint16_t *esrc;
281 66600 const int mb_width = 4 * blocks_per_mb;
282 ptrdiff_t elinesize;
283 int i, j, k;
284
285
2/2
✓ Branch 0 taken 359100 times.
✓ Branch 1 taken 66600 times.
425700 for (i = 0; i < mbs_per_slice; i++, src += mb_width) {
286
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 359100 times.
359100 if (x >= w) {
287 memset(blocks, 0, 64 * (mbs_per_slice - i) * blocks_per_mb
288 * sizeof(*blocks));
289 return;
290 }
291
4/4
✓ Branch 0 taken 358200 times.
✓ Branch 1 taken 900 times.
✓ Branch 2 taken 357600 times.
✓ Branch 3 taken 600 times.
359100 if (x + mb_width <= w && y + 16 <= h) {
292 357600 esrc = src;
293 357600 elinesize = linesize;
294 } else {
295 int bw, bh, pix;
296
297 1500 esrc = emu_buf;
298 1500 elinesize = 16 * sizeof(*emu_buf);
299
300 1500 bw = FFMIN(w - x, mb_width);
301 1500 bh = FFMIN(h - y, 16);
302
303
2/2
✓ Branch 0 taken 11400 times.
✓ Branch 1 taken 1500 times.
12900 for (j = 0; j < bh; j++) {
304 11400 memcpy(emu_buf + j * 16,
305 11400 (const uint8_t*)src + j * linesize,
306 bw * sizeof(*src));
307 11400 pix = emu_buf[j * 16 + bw - 1];
308
2/2
✓ Branch 0 taken 95200 times.
✓ Branch 1 taken 11400 times.
106600 for (k = bw; k < mb_width; k++)
309 95200 emu_buf[j * 16 + k] = pix;
310 }
311
2/2
✓ Branch 0 taken 12600 times.
✓ Branch 1 taken 1500 times.
14100 for (; j < 16; j++)
312 12600 memcpy(emu_buf + j * 16,
313 12600 emu_buf + (bh - 1) * 16,
314 mb_width * sizeof(*emu_buf));
315 }
316
2/2
✓ Branch 0 taken 119700 times.
✓ Branch 1 taken 239400 times.
359100 if (!is_chroma) {
317 119700 ctx->fdct(&ctx->fdsp, esrc, elinesize, blocks);
318 119700 blocks += 64;
319
1/2
✓ Branch 0 taken 119700 times.
✗ Branch 1 not taken.
119700 if (blocks_per_mb > 2) {
320 119700 ctx->fdct(&ctx->fdsp, esrc + 8, elinesize, blocks);
321 119700 blocks += 64;
322 }
323 119700 ctx->fdct(&ctx->fdsp, esrc + elinesize * 4, elinesize, blocks);
324 119700 blocks += 64;
325
1/2
✓ Branch 0 taken 119700 times.
✗ Branch 1 not taken.
119700 if (blocks_per_mb > 2) {
326 119700 ctx->fdct(&ctx->fdsp, esrc + elinesize * 4 + 8, elinesize, blocks);
327 119700 blocks += 64;
328 }
329 } else {
330 239400 ctx->fdct(&ctx->fdsp, esrc, elinesize, blocks);
331 239400 blocks += 64;
332 239400 ctx->fdct(&ctx->fdsp, esrc + elinesize * 4, elinesize, blocks);
333 239400 blocks += 64;
334
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 239400 times.
239400 if (blocks_per_mb > 2) {
335 ctx->fdct(&ctx->fdsp, esrc + 8, elinesize, blocks);
336 blocks += 64;
337 ctx->fdct(&ctx->fdsp, esrc + elinesize * 4 + 8, elinesize, blocks);
338 blocks += 64;
339 }
340 }
341
342 359100 x += mb_width;
343 }
344 }
345
346 static void get_alpha_data(ProresContext *ctx, const uint16_t *src,
347 ptrdiff_t linesize, int x, int y, int w, int h,
348 int16_t *blocks, int mbs_per_slice, int abits)
349 {
350 const int slice_width = 16 * mbs_per_slice;
351 int i, j, copy_w, copy_h;
352
353 copy_w = FFMIN(w - x, slice_width);
354 copy_h = FFMIN(h - y, 16);
355 for (i = 0; i < copy_h; i++) {
356 memcpy(blocks, src, copy_w * sizeof(*src));
357 if (abits == 8)
358 for (j = 0; j < copy_w; j++)
359 blocks[j] >>= 2;
360 else
361 for (j = 0; j < copy_w; j++)
362 blocks[j] = (blocks[j] << 6) | (blocks[j] >> 4);
363 for (j = copy_w; j < slice_width; j++)
364 blocks[j] = blocks[copy_w - 1];
365 blocks += slice_width;
366 src += linesize >> 1;
367 }
368 for (; i < 16; i++) {
369 memcpy(blocks, blocks - slice_width, slice_width * sizeof(*blocks));
370 blocks += slice_width;
371 }
372 }
373
374 /**
375 * Write an unsigned rice/exp golomb codeword.
376 */
377 26885174 static inline void encode_vlc_codeword(PutBitContext *pb, unsigned codebook, int val)
378 {
379 unsigned int rice_order, exp_order, switch_bits, switch_val;
380 int exponent;
381
382 /* number of prefix bits to switch between Rice and expGolomb */
383 26885174 switch_bits = (codebook & 3) + 1;
384 26885174 rice_order = codebook >> 5; /* rice code order */
385 26885174 exp_order = (codebook >> 2) & 7; /* exp golomb code order */
386
387 26885174 switch_val = switch_bits << rice_order;
388
389
2/2
✓ Branch 0 taken 8074309 times.
✓ Branch 1 taken 18810865 times.
26885174 if (val >= switch_val) {
390 8074309 val -= switch_val - (1 << exp_order);
391 8074309 exponent = av_log2(val);
392
393 8074309 put_bits(pb, exponent - exp_order + switch_bits, 0);
394 8074309 put_bits(pb, exponent + 1, val);
395 } else {
396 18810865 exponent = val >> rice_order;
397
398
2/2
✓ Branch 0 taken 4838901 times.
✓ Branch 1 taken 13971964 times.
18810865 if (exponent)
399 4838901 put_bits(pb, exponent, 0);
400 18810865 put_bits(pb, 1, 1);
401
2/2
✓ Branch 0 taken 1904515 times.
✓ Branch 1 taken 16906350 times.
18810865 if (rice_order)
402 1904515 put_sbits(pb, rice_order, val);
403 }
404 26885174 }
405
406 #define GET_SIGN(x) ((x) >> 31)
407 #define MAKE_CODE(x) (((x) * 2) ^ GET_SIGN(x))
408
409 33300 static void encode_dcs(PutBitContext *pb, int16_t *blocks,
410 int blocks_per_slice, int scale)
411 {
412 int i;
413 33300 int codebook = 5, code, dc, prev_dc, delta, sign, new_sign;
414
415 33300 prev_dc = (blocks[0] - 0x4000) / scale;
416 33300 encode_vlc_codeword(pb, FIRST_DC_CB, MAKE_CODE(prev_dc));
417 33300 sign = 0;
418 33300 blocks += 64;
419
420
2/2
✓ Branch 0 taken 445500 times.
✓ Branch 1 taken 33300 times.
478800 for (i = 1; i < blocks_per_slice; i++, blocks += 64) {
421 445500 dc = (blocks[0] - 0x4000) / scale;
422 445500 delta = dc - prev_dc;
423 445500 new_sign = GET_SIGN(delta);
424 445500 delta = (delta ^ sign) - sign;
425 445500 code = MAKE_CODE(delta);
426 445500 encode_vlc_codeword(pb, ff_prores_dc_codebook[codebook], code);
427 445500 codebook = FFMIN(code, 6);
428 445500 sign = new_sign;
429 445500 prev_dc = dc;
430 }
431 33300 }
432
433 33300 static void encode_acs(PutBitContext *pb, int16_t *blocks,
434 int blocks_per_slice,
435 const uint8_t *scan, const int16_t *qmat)
436 {
437 int idx, i;
438 33300 int prev_run = 4;
439 33300 int prev_level = 2;
440 33300 int run = 0, level;
441 int max_coeffs, abs_level;
442
443 33300 max_coeffs = blocks_per_slice << 6;
444
445
2/2
✓ Branch 0 taken 2097900 times.
✓ Branch 1 taken 33300 times.
2131200 for (i = 1; i < 64; i++) {
446
2/2
✓ Branch 0 taken 30164400 times.
✓ Branch 1 taken 2097900 times.
32262300 for (idx = scan[i]; idx < max_coeffs; idx += 64) {
447 30164400 level = blocks[idx] / qmat[scan[i]];
448
2/2
✓ Branch 0 taken 13203187 times.
✓ Branch 1 taken 16961213 times.
30164400 if (level) {
449 13203187 abs_level = FFABS(level);
450 13203187 encode_vlc_codeword(pb, ff_prores_run_to_cb[prev_run], run);
451 13203187 encode_vlc_codeword(pb, ff_prores_level_to_cb[prev_level], abs_level - 1);
452 13203187 put_sbits(pb, 1, GET_SIGN(level));
453
454 13203187 prev_run = FFMIN(run, 15);
455 13203187 prev_level = FFMIN(abs_level, 9);
456 13203187 run = 0;
457 } else {
458 16961213 run++;
459 }
460 }
461 }
462 33300 }
463
464 33300 static void encode_slice_plane(ProresContext *ctx, PutBitContext *pb,
465 const uint16_t *src, ptrdiff_t linesize,
466 int mbs_per_slice, int16_t *blocks,
467 int blocks_per_mb,
468 const int16_t *qmat)
469 {
470 33300 int blocks_per_slice = mbs_per_slice * blocks_per_mb;
471
472 33300 encode_dcs(pb, blocks, blocks_per_slice, qmat[0]);
473 33300 encode_acs(pb, blocks, blocks_per_slice, ctx->scantable, qmat);
474 33300 }
475
476 static void put_alpha_diff(PutBitContext *pb, int cur, int prev, int abits)
477 {
478 const int dbits = (abits == 8) ? 4 : 7;
479 const int dsize = 1 << dbits - 1;
480 int diff = cur - prev;
481
482 diff = av_mod_uintp2(diff, abits);
483 if (diff >= (1 << abits) - dsize)
484 diff -= 1 << abits;
485 if (diff < -dsize || diff > dsize || !diff) {
486 put_bits(pb, 1, 1);
487 put_bits(pb, abits, diff);
488 } else {
489 put_bits(pb, 1, 0);
490 put_bits(pb, dbits - 1, FFABS(diff) - 1);
491 put_bits(pb, 1, diff < 0);
492 }
493 }
494
495 static void put_alpha_run(PutBitContext *pb, int run)
496 {
497 if (run) {
498 put_bits(pb, 1, 0);
499 if (run < 0x10)
500 put_bits(pb, 4, run);
501 else
502 put_bits(pb, 15, run);
503 } else {
504 put_bits(pb, 1, 1);
505 }
506 }
507
508 // todo alpha quantisation for high quants
509 static void encode_alpha_plane(ProresContext *ctx, PutBitContext *pb,
510 int mbs_per_slice, uint16_t *blocks,
511 int quant)
512 {
513 const int abits = ctx->alpha_bits;
514 const int mask = (1 << abits) - 1;
515 const int num_coeffs = mbs_per_slice * 256;
516 int prev = mask, cur;
517 int idx = 0;
518 int run = 0;
519
520 cur = blocks[idx++];
521 put_alpha_diff(pb, cur, prev, abits);
522 prev = cur;
523 do {
524 cur = blocks[idx++];
525 if (cur != prev) {
526 put_alpha_run (pb, run);
527 put_alpha_diff(pb, cur, prev, abits);
528 prev = cur;
529 run = 0;
530 } else {
531 run++;
532 }
533 } while (idx < num_coeffs);
534 put_alpha_run(pb, run);
535 }
536
537 11100 static int encode_slice(AVCodecContext *avctx, const AVFrame *pic,
538 PutBitContext *pb,
539 int sizes[4], int x, int y, int quant,
540 int mbs_per_slice)
541 {
542 11100 ProresContext *ctx = avctx->priv_data;
543 int i, xp, yp;
544 11100 int total_size = 0;
545 const uint16_t *src;
546 int num_cblocks, pwidth, line_add;
547 ptrdiff_t linesize;
548 int is_chroma;
549 uint16_t *qmat;
550 uint16_t *qmat_chroma;
551
552
1/2
✓ Branch 0 taken 11100 times.
✗ Branch 1 not taken.
11100 if (ctx->pictures_per_frame == 1)
553 11100 line_add = 0;
554 else
555 line_add = ctx->cur_picture_idx ^ !(pic->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST);
556
557
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11100 times.
11100 if (ctx->force_quant) {
558 qmat = ctx->quants[0];
559 qmat_chroma = ctx->quants_chroma[0];
560
2/2
✓ Branch 0 taken 8353 times.
✓ Branch 1 taken 2747 times.
11100 } else if (quant < MAX_STORED_Q) {
561 8353 qmat = ctx->quants[quant];
562 8353 qmat_chroma = ctx->quants_chroma[quant];
563 } else {
564 2747 qmat = ctx->custom_q;
565 2747 qmat_chroma = ctx->custom_chroma_q;
566
2/2
✓ Branch 0 taken 175808 times.
✓ Branch 1 taken 2747 times.
178555 for (i = 0; i < 64; i++) {
567 175808 qmat[i] = ctx->quant_mat[i] * quant;
568 175808 qmat_chroma[i] = ctx->quant_chroma_mat[i] * quant;
569 }
570 }
571
572
2/2
✓ Branch 0 taken 33300 times.
✓ Branch 1 taken 11100 times.
44400 for (i = 0; i < ctx->num_planes; i++) {
573
4/4
✓ Branch 0 taken 22200 times.
✓ Branch 1 taken 11100 times.
✓ Branch 2 taken 11100 times.
✓ Branch 3 taken 11100 times.
33300 is_chroma = (i == 1 || i == 2);
574
3/4
✓ Branch 0 taken 22200 times.
✓ Branch 1 taken 11100 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 22200 times.
33300 if (!is_chroma || ctx->chroma_factor == CFACTOR_Y444) {
575 11100 xp = x << 4;
576 11100 yp = y << 4;
577 11100 num_cblocks = 4;
578 11100 pwidth = avctx->width;
579 } else {
580 22200 xp = x << 3;
581 22200 yp = y << 4;
582 22200 num_cblocks = 2;
583 22200 pwidth = avctx->width >> 1;
584 }
585
586 33300 linesize = pic->linesize[i] * ctx->pictures_per_frame;
587 33300 src = (const uint16_t*)(pic->data[i] + yp * linesize +
588 33300 line_add * pic->linesize[i]) + xp;
589
590
1/2
✓ Branch 0 taken 33300 times.
✗ Branch 1 not taken.
33300 if (i < 3) {
591 33300 get_slice_data(ctx, src, linesize, xp, yp,
592 33300 pwidth, avctx->height / ctx->pictures_per_frame,
593 33300 ctx->blocks[0], ctx->emu_buf,
594 mbs_per_slice, num_cblocks, is_chroma);
595
2/2
✓ Branch 0 taken 11100 times.
✓ Branch 1 taken 22200 times.
33300 if (!is_chroma) {/* luma quant */
596 11100 encode_slice_plane(ctx, pb, src, linesize,
597 11100 mbs_per_slice, ctx->blocks[0],
598 num_cblocks, qmat);
599 } else { /* chroma plane */
600 22200 encode_slice_plane(ctx, pb, src, linesize,
601 22200 mbs_per_slice, ctx->blocks[0],
602 num_cblocks, qmat_chroma);
603 }
604 } else {
605 get_alpha_data(ctx, src, linesize, xp, yp,
606 pwidth, avctx->height / ctx->pictures_per_frame,
607 ctx->blocks[0], mbs_per_slice, ctx->alpha_bits);
608 encode_alpha_plane(ctx, pb, mbs_per_slice, ctx->blocks[0], quant);
609 }
610 33300 flush_put_bits(pb);
611 33300 sizes[i] = put_bytes_output(pb) - total_size;
612 33300 total_size = put_bytes_output(pb);
613 }
614 11100 return total_size;
615 }
616
617 390320122 static inline int estimate_vlc(unsigned codebook, int val)
618 {
619 unsigned int rice_order, exp_order, switch_bits, switch_val;
620 int exponent;
621
622 /* number of prefix bits to switch between Rice and expGolomb */
623 390320122 switch_bits = (codebook & 3) + 1;
624 390320122 rice_order = codebook >> 5; /* rice code order */
625 390320122 exp_order = (codebook >> 2) & 7; /* exp golomb code order */
626
627 390320122 switch_val = switch_bits << rice_order;
628
629
2/2
✓ Branch 0 taken 135366530 times.
✓ Branch 1 taken 254953592 times.
390320122 if (val >= switch_val) {
630 135366530 val -= switch_val - (1 << exp_order);
631 135366530 exponent = av_log2(val);
632
633 135366530 return exponent * 2 - exp_order + switch_bits + 1;
634 } else {
635 254953592 return (val >> rice_order) + rice_order + 1;
636 }
637 }
638
639 379686 static int estimate_dcs(int *error, int16_t *blocks, int blocks_per_slice,
640 int scale)
641 {
642 int i;
643 379686 int codebook = 5, code, dc, prev_dc, delta, sign, new_sign;
644 int bits;
645
646 379686 prev_dc = (blocks[0] - 0x4000) / scale;
647 379686 bits = estimate_vlc(FIRST_DC_CB, MAKE_CODE(prev_dc));
648 379686 sign = 0;
649 379686 blocks += 64;
650
2/2
✓ Branch 0 taken 199846 times.
✓ Branch 1 taken 179840 times.
379686 *error += FFABS(blocks[0] - 0x4000) % scale;
651
652
2/2
✓ Branch 0 taken 5069986 times.
✓ Branch 1 taken 379686 times.
5449672 for (i = 1; i < blocks_per_slice; i++, blocks += 64) {
653 5069986 dc = (blocks[0] - 0x4000) / scale;
654
2/2
✓ Branch 0 taken 2618934 times.
✓ Branch 1 taken 2451052 times.
5069986 *error += FFABS(blocks[0] - 0x4000) % scale;
655 5069986 delta = dc - prev_dc;
656 5069986 new_sign = GET_SIGN(delta);
657 5069986 delta = (delta ^ sign) - sign;
658 5069986 code = MAKE_CODE(delta);
659 5069986 bits += estimate_vlc(ff_prores_dc_codebook[codebook], code);
660 5069986 codebook = FFMIN(code, 6);
661 5069986 sign = new_sign;
662 5069986 prev_dc = dc;
663 }
664
665 379686 return bits;
666 }
667
668 379686 static int estimate_acs(int *error, int16_t *blocks, int blocks_per_slice,
669 const uint8_t *scan, const int16_t *qmat)
670 {
671 int idx, i;
672 379686 int prev_run = 4;
673 379686 int prev_level = 2;
674 int run, level;
675 int max_coeffs, abs_level;
676 379686 int bits = 0;
677
678 379686 max_coeffs = blocks_per_slice << 6;
679 379686 run = 0;
680
681
2/2
✓ Branch 0 taken 23920218 times.
✓ Branch 1 taken 379686 times.
24299904 for (i = 1; i < 64; i++) {
682
2/2
✓ Branch 0 taken 343329336 times.
✓ Branch 1 taken 23920218 times.
367249554 for (idx = scan[i]; idx < max_coeffs; idx += 64) {
683 343329336 level = blocks[idx] / qmat[scan[i]];
684 343329336 *error += FFABS(blocks[idx]) % qmat[scan[i]];
685
2/2
✓ Branch 0 taken 192435225 times.
✓ Branch 1 taken 150894111 times.
343329336 if (level) {
686 192435225 abs_level = FFABS(level);
687 192435225 bits += estimate_vlc(ff_prores_run_to_cb[prev_run], run);
688 192435225 bits += estimate_vlc(ff_prores_level_to_cb[prev_level],
689 192435225 abs_level - 1) + 1;
690
691 192435225 prev_run = FFMIN(run, 15);
692 192435225 prev_level = FFMIN(abs_level, 9);
693 192435225 run = 0;
694 } else {
695 150894111 run++;
696 }
697 }
698 }
699
700 379686 return bits;
701 }
702
703 379686 static int estimate_slice_plane(ProresContext *ctx, int *error, int plane,
704 const uint16_t *src, ptrdiff_t linesize,
705 int mbs_per_slice,
706 int blocks_per_mb,
707 const int16_t *qmat, ProresThreadData *td)
708 {
709 int blocks_per_slice;
710 int bits;
711
712 379686 blocks_per_slice = mbs_per_slice * blocks_per_mb;
713
714 379686 bits = estimate_dcs(error, td->blocks[plane], blocks_per_slice, qmat[0]);
715 379686 bits += estimate_acs(error, td->blocks[plane], blocks_per_slice, ctx->scantable, qmat);
716
717 379686 return FFALIGN(bits, 8);
718 }
719
720 static int est_alpha_diff(int cur, int prev, int abits)
721 {
722 const int dbits = (abits == 8) ? 4 : 7;
723 const int dsize = 1 << dbits - 1;
724 int diff = cur - prev;
725
726 diff = av_mod_uintp2(diff, abits);
727 if (diff >= (1 << abits) - dsize)
728 diff -= 1 << abits;
729 if (diff < -dsize || diff > dsize || !diff)
730 return abits + 1;
731 else
732 return dbits + 1;
733 }
734
735 static int estimate_alpha_plane(ProresContext *ctx,
736 const uint16_t *src, ptrdiff_t linesize,
737 int mbs_per_slice, int16_t *blocks)
738 {
739 const int abits = ctx->alpha_bits;
740 const int mask = (1 << abits) - 1;
741 const int num_coeffs = mbs_per_slice * 256;
742 int prev = mask, cur;
743 int idx = 0;
744 int run = 0;
745 int bits;
746
747 cur = blocks[idx++];
748 bits = est_alpha_diff(cur, prev, abits);
749 prev = cur;
750 do {
751 cur = blocks[idx++];
752 if (cur != prev) {
753 if (!run)
754 bits++;
755 else if (run < 0x10)
756 bits += 4;
757 else
758 bits += 15;
759 bits += est_alpha_diff(cur, prev, abits);
760 prev = cur;
761 run = 0;
762 } else {
763 run++;
764 }
765 } while (idx < num_coeffs);
766
767 if (run) {
768 if (run < 0x10)
769 bits += 4;
770 else
771 bits += 15;
772 }
773
774 return bits;
775 }
776
777 11100 static int find_slice_quant(AVCodecContext *avctx,
778 int trellis_node, int x, int y, int mbs_per_slice,
779 ProresThreadData *td)
780 {
781 11100 ProresContext *ctx = avctx->priv_data;
782 int i, q, pq, xp, yp;
783 const uint16_t *src;
784 int num_cblocks[MAX_PLANES], pwidth;
785 int is_chroma[MAX_PLANES];
786 11100 const int min_quant = ctx->profile_info->min_quant;
787 11100 const int max_quant = ctx->profile_info->max_quant;
788 int error, bits, bits_limit;
789 int mbs, prev, cur, new_score;
790 int slice_bits[TRELLIS_WIDTH], slice_score[TRELLIS_WIDTH];
791 int overquant;
792 uint16_t *qmat;
793 uint16_t *qmat_chroma;
794 int linesize[4], line_add;
795 11100 int alpha_bits = 0;
796
797
1/2
✓ Branch 0 taken 11100 times.
✗ Branch 1 not taken.
11100 if (ctx->pictures_per_frame == 1)
798 11100 line_add = 0;
799 else
800 line_add = ctx->cur_picture_idx ^ !(ctx->pic->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST);
801 11100 mbs = x + mbs_per_slice;
802
803
2/2
✓ Branch 0 taken 33300 times.
✓ Branch 1 taken 11100 times.
44400 for (i = 0; i < ctx->num_planes; i++) {
804
4/4
✓ Branch 0 taken 22200 times.
✓ Branch 1 taken 11100 times.
✓ Branch 2 taken 11100 times.
✓ Branch 3 taken 11100 times.
33300 is_chroma[i] = (i == 1 || i == 2);
805
3/4
✓ Branch 0 taken 22200 times.
✓ Branch 1 taken 11100 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 22200 times.
33300 if (!is_chroma[i] || ctx->chroma_factor == CFACTOR_Y444) {
806 11100 xp = x << 4;
807 11100 yp = y << 4;
808 11100 num_cblocks[i] = 4;
809 11100 pwidth = avctx->width;
810 } else {
811 22200 xp = x << 3;
812 22200 yp = y << 4;
813 22200 num_cblocks[i] = 2;
814 22200 pwidth = avctx->width >> 1;
815 }
816
817 33300 linesize[i] = ctx->pic->linesize[i] * ctx->pictures_per_frame;
818 33300 src = (const uint16_t *)(ctx->pic->data[i] + yp * linesize[i] +
819 33300 line_add * ctx->pic->linesize[i]) + xp;
820
821
1/2
✓ Branch 0 taken 33300 times.
✗ Branch 1 not taken.
33300 if (i < 3) {
822 33300 get_slice_data(ctx, src, linesize[i], xp, yp,
823 33300 pwidth, avctx->height / ctx->pictures_per_frame,
824 33300 td->blocks[i], td->emu_buf,
825 mbs_per_slice, num_cblocks[i], is_chroma[i]);
826 } else {
827 get_alpha_data(ctx, src, linesize[i], xp, yp,
828 pwidth, avctx->height / ctx->pictures_per_frame,
829 td->blocks[i], mbs_per_slice, ctx->alpha_bits);
830 }
831 }
832
833
2/2
✓ Branch 0 taken 77700 times.
✓ Branch 1 taken 11100 times.
88800 for (q = min_quant; q < max_quant + 2; q++) {
834 77700 td->nodes[trellis_node + q].prev_node = -1;
835 77700 td->nodes[trellis_node + q].quant = q;
836 }
837
838
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11100 times.
11100 if (ctx->alpha_bits)
839 alpha_bits = estimate_alpha_plane(ctx, src, linesize[3],
840 mbs_per_slice, td->blocks[3]);
841 // todo: maybe perform coarser quantising to fit into frame size when needed
842
2/2
✓ Branch 0 taken 66600 times.
✓ Branch 1 taken 11100 times.
77700 for (q = min_quant; q <= max_quant; q++) {
843 66600 bits = alpha_bits;
844 66600 error = 0;
845 133200 bits += estimate_slice_plane(ctx, &error, 0,
846 66600 src, linesize[0],
847 mbs_per_slice,
848 num_cblocks[0],
849 66600 ctx->quants[q], td); /* estimate luma plane */
850
2/2
✓ Branch 0 taken 133200 times.
✓ Branch 1 taken 66600 times.
199800 for (i = 1; i < ctx->num_planes - !!ctx->alpha_bits; i++) { /* estimate chroma plane */
851 133200 bits += estimate_slice_plane(ctx, &error, i,
852 133200 src, linesize[i],
853 mbs_per_slice,
854 num_cblocks[i],
855 133200 ctx->quants_chroma[q], td);
856 }
857
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 66600 times.
66600 if (bits > 65000 * 8)
858 error = SCORE_LIMIT;
859
860 66600 slice_bits[q] = bits;
861 66600 slice_score[q] = error;
862 }
863
2/2
✓ Branch 0 taken 3643 times.
✓ Branch 1 taken 7457 times.
11100 if (slice_bits[max_quant] <= ctx->bits_per_mb * mbs_per_slice) {
864 3643 slice_bits[max_quant + 1] = slice_bits[max_quant];
865 3643 slice_score[max_quant + 1] = slice_score[max_quant] + 1;
866 3643 overquant = max_quant;
867 } else {
868
1/2
✓ Branch 0 taken 59962 times.
✗ Branch 1 not taken.
59962 for (q = max_quant + 1; q < 128; q++) {
869 59962 bits = alpha_bits;
870 59962 error = 0;
871
2/2
✓ Branch 0 taken 44939 times.
✓ Branch 1 taken 15023 times.
59962 if (q < MAX_STORED_Q) {
872 44939 qmat = ctx->quants[q];
873 44939 qmat_chroma = ctx->quants_chroma[q];
874 } else {
875 15023 qmat = td->custom_q;
876 15023 qmat_chroma = td->custom_chroma_q;
877
2/2
✓ Branch 0 taken 961472 times.
✓ Branch 1 taken 15023 times.
976495 for (i = 0; i < 64; i++) {
878 961472 qmat[i] = ctx->quant_mat[i] * q;
879 961472 qmat_chroma[i] = ctx->quant_chroma_mat[i] * q;
880 }
881 }
882 119924 bits += estimate_slice_plane(ctx, &error, 0,
883 59962 src, linesize[0],
884 mbs_per_slice,
885 num_cblocks[0],
886 qmat, td);/* estimate luma plane */
887
2/2
✓ Branch 0 taken 119924 times.
✓ Branch 1 taken 59962 times.
179886 for (i = 1; i < ctx->num_planes - !!ctx->alpha_bits; i++) { /* estimate chroma plane */
888 119924 bits += estimate_slice_plane(ctx, &error, i,
889 119924 src, linesize[i],
890 mbs_per_slice,
891 num_cblocks[i],
892 qmat_chroma, td);
893 }
894
2/2
✓ Branch 0 taken 7457 times.
✓ Branch 1 taken 52505 times.
59962 if (bits <= ctx->bits_per_mb * mbs_per_slice)
895 7457 break;
896 }
897
898 7457 slice_bits[max_quant + 1] = bits;
899 7457 slice_score[max_quant + 1] = error;
900 7457 overquant = q;
901 }
902 11100 td->nodes[trellis_node + max_quant + 1].quant = overquant;
903
904 11100 bits_limit = mbs * ctx->bits_per_mb;
905
2/2
✓ Branch 0 taken 77700 times.
✓ Branch 1 taken 11100 times.
88800 for (pq = min_quant; pq < max_quant + 2; pq++) {
906 77700 prev = trellis_node - TRELLIS_WIDTH + pq;
907
908
2/2
✓ Branch 0 taken 543900 times.
✓ Branch 1 taken 77700 times.
621600 for (q = min_quant; q < max_quant + 2; q++) {
909 543900 cur = trellis_node + q;
910
911 543900 bits = td->nodes[prev].bits + slice_bits[q];
912 543900 error = slice_score[q];
913
2/2
✓ Branch 0 taken 438632 times.
✓ Branch 1 taken 105268 times.
543900 if (bits > bits_limit)
914 438632 error = SCORE_LIMIT;
915
916
4/4
✓ Branch 0 taken 268352 times.
✓ Branch 1 taken 275548 times.
✓ Branch 2 taken 98480 times.
✓ Branch 3 taken 169872 times.
543900 if (td->nodes[prev].score < SCORE_LIMIT && error < SCORE_LIMIT)
917 98480 new_score = td->nodes[prev].score + error;
918 else
919 445420 new_score = SCORE_LIMIT;
920
2/2
✓ Branch 0 taken 466200 times.
✓ Branch 1 taken 77700 times.
543900 if (td->nodes[cur].prev_node == -1 ||
921
2/2
✓ Branch 0 taken 427864 times.
✓ Branch 1 taken 38336 times.
466200 td->nodes[cur].score >= new_score) {
922
923 505564 td->nodes[cur].bits = bits;
924 505564 td->nodes[cur].score = new_score;
925 505564 td->nodes[cur].prev_node = prev;
926 }
927 }
928 }
929
930 11100 error = td->nodes[trellis_node + min_quant].score;
931 11100 pq = trellis_node + min_quant;
932
2/2
✓ Branch 0 taken 66600 times.
✓ Branch 1 taken 11100 times.
77700 for (q = min_quant + 1; q < max_quant + 2; q++) {
933
2/2
✓ Branch 0 taken 52033 times.
✓ Branch 1 taken 14567 times.
66600 if (td->nodes[trellis_node + q].score <= error) {
934 52033 error = td->nodes[trellis_node + q].score;
935 52033 pq = trellis_node + q;
936 }
937 }
938
939 11100 return pq;
940 }
941
942 2850 static int find_quant_thread(AVCodecContext *avctx, void *arg,
943 int jobnr, int threadnr)
944 {
945 2850 ProresContext *ctx = avctx->priv_data;
946 2850 ProresThreadData *td = ctx->tdata + threadnr;
947 2850 int mbs_per_slice = ctx->mbs_per_slice;
948 2850 int x, y = jobnr, mb, q = 0;
949
950
2/2
✓ Branch 0 taken 11100 times.
✓ Branch 1 taken 2850 times.
13950 for (x = mb = 0; x < ctx->mb_width; x += mbs_per_slice, mb++) {
951
2/2
✓ Branch 0 taken 5850 times.
✓ Branch 1 taken 11100 times.
16950 while (ctx->mb_width - x < mbs_per_slice)
952 5850 mbs_per_slice >>= 1;
953 11100 q = find_slice_quant(avctx,
954 11100 (mb + 1) * TRELLIS_WIDTH, x, y,
955 mbs_per_slice, td);
956 }
957
958
2/2
✓ Branch 0 taken 11100 times.
✓ Branch 1 taken 2850 times.
13950 for (x = ctx->slices_width - 1; x >= 0; x--) {
959 11100 ctx->slice_q[x + y * ctx->slices_width] = td->nodes[q].quant;
960 11100 q = td->nodes[q].prev_node;
961 }
962
963 2850 return 0;
964 }
965
966 200 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
967 const AVFrame *pic, int *got_packet)
968 {
969 200 ProresContext *ctx = avctx->priv_data;
970 uint8_t *orig_buf, *buf, *slice_hdr, *slice_sizes, *tmp;
971 uint8_t *picture_size_pos;
972 PutBitContext pb;
973 200 int x, y, i, mb, q = 0;
974 200 int sizes[4] = { 0 };
975 200 int slice_hdr_size = 2 + 2 * (ctx->num_planes - 1);
976 int frame_size, picture_size, slice_size;
977 int pkt_size, ret;
978 200 int max_slice_size = (ctx->frame_size_upper_bound - 200) / (ctx->pictures_per_frame * ctx->slices_per_picture + 1);
979 uint8_t frame_flags;
980
981 200 ctx->pic = pic;
982 200 pkt_size = ctx->frame_size_upper_bound;
983
984
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 200 times.
200 if ((ret = ff_alloc_packet(avctx, pkt, pkt_size + AV_INPUT_BUFFER_MIN_SIZE)) < 0)
985 return ret;
986
987 200 orig_buf = pkt->data;
988
989 // frame atom
990 200 orig_buf += 4; // frame size
991 200 bytestream_put_be32 (&orig_buf, FRAME_ID); // frame container ID
992 200 buf = orig_buf;
993
994 // frame header
995 200 tmp = buf;
996 200 buf += 2; // frame header size will be stored here
997
2/4
✓ Branch 0 taken 200 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 200 times.
200 bytestream_put_be16 (&buf, ctx->chroma_factor != CFACTOR_Y422 || ctx->alpha_bits ? 1 : 0);
998 200 bytestream_put_buffer(&buf, ctx->vendor, 4);
999 200 bytestream_put_be16 (&buf, avctx->width);
1000 200 bytestream_put_be16 (&buf, avctx->height);
1001
1002 200 frame_flags = ctx->chroma_factor << 6;
1003
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 200 times.
200 if (avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT)
1004 frame_flags |= (pic->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST) ? 0x04 : 0x08;
1005 200 bytestream_put_byte (&buf, frame_flags);
1006
1007 200 bytestream_put_byte (&buf, 0); // reserved
1008 200 bytestream_put_byte (&buf, pic->color_primaries);
1009 200 bytestream_put_byte (&buf, pic->color_trc);
1010 200 bytestream_put_byte (&buf, pic->colorspace);
1011 200 bytestream_put_byte (&buf, ctx->alpha_bits >> 3);
1012 200 bytestream_put_byte (&buf, 0); // reserved
1013
1/2
✓ Branch 0 taken 200 times.
✗ Branch 1 not taken.
200 if (ctx->quant_sel != QUANT_MAT_DEFAULT) {
1014 200 bytestream_put_byte (&buf, 0x03); // matrix flags - both matrices are present
1015 200 bytestream_put_buffer(&buf, ctx->quant_mat, 64); // luma quantisation matrix
1016 200 bytestream_put_buffer(&buf, ctx->quant_chroma_mat, 64); // chroma quantisation matrix
1017 } else {
1018 bytestream_put_byte (&buf, 0x00); // matrix flags - default matrices are used
1019 }
1020 200 bytestream_put_be16 (&tmp, buf - orig_buf); // write back frame header size
1021
1022 200 for (ctx->cur_picture_idx = 0;
1023
2/2
✓ Branch 0 taken 200 times.
✓ Branch 1 taken 200 times.
400 ctx->cur_picture_idx < ctx->pictures_per_frame;
1024 200 ctx->cur_picture_idx++) {
1025 // picture header
1026 200 picture_size_pos = buf + 1;
1027 200 bytestream_put_byte (&buf, 0x40); // picture header size (in bits)
1028 200 buf += 4; // picture data size will be stored here
1029 200 bytestream_put_be16 (&buf, ctx->slices_per_picture);
1030 200 bytestream_put_byte (&buf, av_log2(ctx->mbs_per_slice) << 4); // slice width and height in MBs
1031
1032 // seek table - will be filled during slice encoding
1033 200 slice_sizes = buf;
1034 200 buf += ctx->slices_per_picture * 2;
1035
1036 // slices
1037
1/2
✓ Branch 0 taken 200 times.
✗ Branch 1 not taken.
200 if (!ctx->force_quant) {
1038 200 ret = avctx->execute2(avctx, find_quant_thread, (void*)pic, NULL,
1039 ctx->mb_height);
1040
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 200 times.
200 if (ret)
1041 return ret;
1042 }
1043
1044
2/2
✓ Branch 0 taken 2850 times.
✓ Branch 1 taken 200 times.
3050 for (y = 0; y < ctx->mb_height; y++) {
1045 2850 int mbs_per_slice = ctx->mbs_per_slice;
1046
2/2
✓ Branch 0 taken 11100 times.
✓ Branch 1 taken 2850 times.
13950 for (x = mb = 0; x < ctx->mb_width; x += mbs_per_slice, mb++) {
1047 22200 q = ctx->force_quant ? ctx->force_quant
1048
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11100 times.
11100 : ctx->slice_q[mb + y * ctx->slices_width];
1049
1050
2/2
✓ Branch 0 taken 5850 times.
✓ Branch 1 taken 11100 times.
16950 while (ctx->mb_width - x < mbs_per_slice)
1051 5850 mbs_per_slice >>= 1;
1052
1053 11100 bytestream_put_byte(&buf, slice_hdr_size << 3);
1054 11100 slice_hdr = buf;
1055 11100 buf += slice_hdr_size - 1;
1056
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11100 times.
11100 if (pkt_size <= buf - orig_buf + 2 * max_slice_size) {
1057 uint8_t *start = pkt->data;
1058 // Recompute new size according to max_slice_size
1059 // and deduce delta
1060 int delta = 200 + (ctx->pictures_per_frame *
1061 ctx->slices_per_picture + 1) *
1062 max_slice_size - pkt_size;
1063
1064 delta = FFMAX(delta, 2 * max_slice_size);
1065 ctx->frame_size_upper_bound += delta;
1066
1067 if (!ctx->warn) {
1068 avpriv_request_sample(avctx,
1069 "Packet too small: is %i,"
1070 " needs %i (slice: %i). "
1071 "Correct allocation",
1072 pkt_size, delta, max_slice_size);
1073 ctx->warn = 1;
1074 }
1075
1076 ret = av_grow_packet(pkt, delta);
1077 if (ret < 0)
1078 return ret;
1079
1080 pkt_size += delta;
1081 // restore pointers
1082 orig_buf = pkt->data + (orig_buf - start);
1083 buf = pkt->data + (buf - start);
1084 picture_size_pos = pkt->data + (picture_size_pos - start);
1085 slice_sizes = pkt->data + (slice_sizes - start);
1086 slice_hdr = pkt->data + (slice_hdr - start);
1087 tmp = pkt->data + (tmp - start);
1088 }
1089 11100 init_put_bits(&pb, buf, (pkt_size - (buf - orig_buf)));
1090 11100 ret = encode_slice(avctx, pic, &pb, sizes, x, y, q,
1091 mbs_per_slice);
1092
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11100 times.
11100 if (ret < 0)
1093 return ret;
1094
1095 11100 bytestream_put_byte(&slice_hdr, q);
1096 11100 slice_size = slice_hdr_size + sizes[ctx->num_planes - 1];
1097
2/2
✓ Branch 0 taken 22200 times.
✓ Branch 1 taken 11100 times.
33300 for (i = 0; i < ctx->num_planes - 1; i++) {
1098 22200 bytestream_put_be16(&slice_hdr, sizes[i]);
1099 22200 slice_size += sizes[i];
1100 }
1101 11100 bytestream_put_be16(&slice_sizes, slice_size);
1102 11100 buf += slice_size - slice_hdr_size;
1103
2/2
✓ Branch 0 taken 216 times.
✓ Branch 1 taken 10884 times.
11100 if (max_slice_size < slice_size)
1104 216 max_slice_size = slice_size;
1105 }
1106 }
1107
1108 200 picture_size = buf - (picture_size_pos - 1);
1109 200 bytestream_put_be32(&picture_size_pos, picture_size);
1110 }
1111
1112 200 orig_buf -= 8;
1113 200 frame_size = buf - orig_buf;
1114 200 bytestream_put_be32(&orig_buf, frame_size);
1115
1116 200 pkt->size = frame_size;
1117 200 *got_packet = 1;
1118
1119 200 return 0;
1120 }
1121
1122 4 static av_cold int encode_close(AVCodecContext *avctx)
1123 {
1124 4 ProresContext *ctx = avctx->priv_data;
1125 int i;
1126
1127
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (ctx->tdata) {
1128
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
8 for (i = 0; i < avctx->thread_count; i++)
1129 4 av_freep(&ctx->tdata[i].nodes);
1130 }
1131 4 av_freep(&ctx->tdata);
1132 4 av_freep(&ctx->slice_q);
1133
1134 4 return 0;
1135 }
1136
1137 957600 static void prores_fdct(FDCTDSPContext *fdsp, const uint16_t *src,
1138 ptrdiff_t linesize, int16_t *block)
1139 {
1140 int x, y;
1141 957600 const uint16_t *tsrc = src;
1142
1143
2/2
✓ Branch 0 taken 7660800 times.
✓ Branch 1 taken 957600 times.
8618400 for (y = 0; y < 8; y++) {
1144
2/2
✓ Branch 0 taken 61286400 times.
✓ Branch 1 taken 7660800 times.
68947200 for (x = 0; x < 8; x++)
1145 61286400 block[y * 8 + x] = tsrc[x];
1146 7660800 tsrc += linesize >> 1;
1147 }
1148 957600 fdsp->fdct(block);
1149 957600 }
1150
1151 4 static av_cold int encode_init(AVCodecContext *avctx)
1152 {
1153 4 ProresContext *ctx = avctx->priv_data;
1154 int mps;
1155 int i, j;
1156 int min_quant, max_quant;
1157 4 int interlaced = !!(avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT);
1158
1159 4 avctx->bits_per_raw_sample = 10;
1160
1161 4 ctx->fdct = prores_fdct;
1162 4 ctx->scantable = interlaced ? ff_prores_interlaced_scan
1163
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 : ff_prores_progressive_scan;
1164 4 ff_fdctdsp_init(&ctx->fdsp, avctx);
1165
1166 4 mps = ctx->mbs_per_slice;
1167
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (mps & (mps - 1)) {
1168 av_log(avctx, AV_LOG_ERROR,
1169 "there should be an integer power of two MBs per slice\n");
1170 return AVERROR(EINVAL);
1171 }
1172
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ctx->profile == PRORES_PROFILE_AUTO) {
1173 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
1174 ctx->profile = (desc->flags & AV_PIX_FMT_FLAG_ALPHA ||
1175 !(desc->log2_chroma_w + desc->log2_chroma_h))
1176 ? PRORES_PROFILE_4444 : PRORES_PROFILE_HQ;
1177 av_log(avctx, AV_LOG_INFO, "Autoselected %s. It can be overridden "
1178 "through -profile option.\n", ctx->profile == PRORES_PROFILE_4444
1179 ? "4:4:4:4 profile because of the used input colorspace"
1180 : "HQ profile to keep best quality");
1181 }
1182
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 if (av_pix_fmt_desc_get(avctx->pix_fmt)->flags & AV_PIX_FMT_FLAG_ALPHA) {
1183 if (ctx->profile != PRORES_PROFILE_4444 &&
1184 ctx->profile != PRORES_PROFILE_4444XQ) {
1185 // force alpha and warn
1186 av_log(avctx, AV_LOG_WARNING, "Profile selected will not "
1187 "encode alpha. Override with -profile if needed.\n");
1188 ctx->alpha_bits = 0;
1189 }
1190 if (ctx->alpha_bits & 7) {
1191 av_log(avctx, AV_LOG_ERROR, "alpha bits should be 0, 8 or 16\n");
1192 return AVERROR(EINVAL);
1193 }
1194 avctx->bits_per_coded_sample = 32;
1195 } else {
1196 4 ctx->alpha_bits = 0;
1197 }
1198
1199 8 ctx->chroma_factor = avctx->pix_fmt == AV_PIX_FMT_YUV422P10
1200 ? CFACTOR_Y422
1201
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 : CFACTOR_Y444;
1202 4 ctx->profile_info = prores_profile_info + ctx->profile;
1203
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 ctx->num_planes = 3 + !!ctx->alpha_bits;
1204
1205 4 ctx->mb_width = FFALIGN(avctx->width, 16) >> 4;
1206
1207
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (interlaced)
1208 ctx->mb_height = FFALIGN(avctx->height, 32) >> 5;
1209 else
1210 4 ctx->mb_height = FFALIGN(avctx->height, 16) >> 4;
1211
1212 4 ctx->slices_width = ctx->mb_width / mps;
1213 4 ctx->slices_width += av_popcount(ctx->mb_width - ctx->slices_width * mps);
1214 4 ctx->slices_per_picture = ctx->mb_height * ctx->slices_width;
1215 4 ctx->pictures_per_frame = 1 + interlaced;
1216
1217
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (ctx->quant_sel == -1) {
1218 4 ctx->quant_mat = prores_quant_matrices[ctx->profile_info->quant];
1219 4 ctx->quant_chroma_mat = prores_quant_matrices[ctx->profile_info->quant_chroma];
1220 } else {
1221 ctx->quant_mat = prores_quant_matrices[ctx->quant_sel];
1222 ctx->quant_chroma_mat = prores_quant_matrices[ctx->quant_sel];
1223 }
1224
1225
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (strlen(ctx->vendor) != 4) {
1226 av_log(avctx, AV_LOG_ERROR, "vendor ID should be 4 bytes\n");
1227 return AVERROR_INVALIDDATA;
1228 }
1229
1230 4 ctx->force_quant = avctx->global_quality / FF_QP2LAMBDA;
1231
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (!ctx->force_quant) {
1232
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (!ctx->bits_per_mb) {
1233
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 for (i = 0; i < NUM_MB_LIMITS - 1; i++)
1234 4 if (prores_mb_limits[i] >= ctx->mb_width * ctx->mb_height *
1235
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 ctx->pictures_per_frame)
1236 4 break;
1237 4 ctx->bits_per_mb = ctx->profile_info->br_tab[i];
1238
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ctx->alpha_bits)
1239 ctx->bits_per_mb *= 20;
1240 } else if (ctx->bits_per_mb < 128) {
1241 av_log(avctx, AV_LOG_ERROR, "too few bits per MB, please set at least 128\n");
1242 return AVERROR_INVALIDDATA;
1243 }
1244
1245 4 min_quant = ctx->profile_info->min_quant;
1246 4 max_quant = ctx->profile_info->max_quant;
1247
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 4 times.
64 for (i = min_quant; i < MAX_STORED_Q; i++) {
1248
2/2
✓ Branch 0 taken 3840 times.
✓ Branch 1 taken 60 times.
3900 for (j = 0; j < 64; j++) {
1249 3840 ctx->quants[i][j] = ctx->quant_mat[j] * i;
1250 3840 ctx->quants_chroma[i][j] = ctx->quant_chroma_mat[j] * i;
1251 }
1252 }
1253
1254 4 ctx->slice_q = av_malloc_array(ctx->slices_per_picture, sizeof(*ctx->slice_q));
1255
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!ctx->slice_q)
1256 return AVERROR(ENOMEM);
1257
1258 4 ctx->tdata = av_calloc(avctx->thread_count, sizeof(*ctx->tdata));
1259
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!ctx->tdata)
1260 return AVERROR(ENOMEM);
1261
1262
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
8 for (j = 0; j < avctx->thread_count; j++) {
1263 4 ctx->tdata[j].nodes = av_malloc_array(ctx->slices_width + 1,
1264 TRELLIS_WIDTH
1265 * sizeof(*ctx->tdata->nodes));
1266
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!ctx->tdata[j].nodes)
1267 return AVERROR(ENOMEM);
1268
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 4 times.
32 for (i = min_quant; i < max_quant + 2; i++) {
1269 28 ctx->tdata[j].nodes[i].prev_node = -1;
1270 28 ctx->tdata[j].nodes[i].bits = 0;
1271 28 ctx->tdata[j].nodes[i].score = 0;
1272 }
1273 }
1274 } else {
1275 int ls = 0;
1276 int ls_chroma = 0;
1277
1278 if (ctx->force_quant > 64) {
1279 av_log(avctx, AV_LOG_ERROR, "too large quantiser, maximum is 64\n");
1280 return AVERROR_INVALIDDATA;
1281 }
1282
1283 for (j = 0; j < 64; j++) {
1284 ctx->quants[0][j] = ctx->quant_mat[j] * ctx->force_quant;
1285 ctx->quants_chroma[0][j] = ctx->quant_chroma_mat[j] * ctx->force_quant;
1286 ls += av_log2((1 << 11) / ctx->quants[0][j]) * 2 + 1;
1287 ls_chroma += av_log2((1 << 11) / ctx->quants_chroma[0][j]) * 2 + 1;
1288 }
1289
1290 ctx->bits_per_mb = ls * 4 + ls_chroma * 4;
1291 if (ctx->chroma_factor == CFACTOR_Y444)
1292 ctx->bits_per_mb += ls_chroma * 4;
1293 }
1294
1295 4 ctx->frame_size_upper_bound = (ctx->pictures_per_frame *
1296 4 ctx->slices_per_picture + 1) *
1297 4 (2 + 2 * ctx->num_planes +
1298 4 (mps * ctx->bits_per_mb) / 8)
1299 4 + 200;
1300
1301
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ctx->alpha_bits) {
1302 // The alpha plane is run-coded and might exceed the bit budget.
1303 ctx->frame_size_upper_bound += (ctx->pictures_per_frame *
1304 ctx->slices_per_picture + 1) *
1305 /* num pixels per slice */ (ctx->mbs_per_slice * 256 *
1306 /* bits per pixel */ (1 + ctx->alpha_bits + 1) + 7 >> 3);
1307 }
1308
1309 4 avctx->codec_tag = ctx->profile_info->tag;
1310 4 avctx->profile = ctx->profile;
1311
1312
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 av_log(avctx, AV_LOG_DEBUG,
1313 "profile %d, %d slices, interlacing: %s, %d bits per MB\n",
1314 4 ctx->profile, ctx->slices_per_picture * ctx->pictures_per_frame,
1315 interlaced ? "yes" : "no", ctx->bits_per_mb);
1316 4 av_log(avctx, AV_LOG_DEBUG, "frame size upper bound: %d\n",
1317 ctx->frame_size_upper_bound);
1318
1319 4 return 0;
1320 }
1321
1322 #define OFFSET(x) offsetof(ProresContext, x)
1323 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1324
1325 static const AVOption options[] = {
1326 { "mbs_per_slice", "macroblocks per slice", OFFSET(mbs_per_slice),
1327 AV_OPT_TYPE_INT, { .i64 = 8 }, 1, MAX_MBS_PER_SLICE, VE },
1328 { "profile", NULL, OFFSET(profile), AV_OPT_TYPE_INT,
1329 { .i64 = PRORES_PROFILE_AUTO },
1330 PRORES_PROFILE_AUTO, PRORES_PROFILE_4444XQ, VE, .unit = "profile" },
1331 { "auto", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PRORES_PROFILE_AUTO },
1332 0, 0, VE, .unit = "profile" },
1333 { "proxy", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PRORES_PROFILE_PROXY },
1334 0, 0, VE, .unit = "profile" },
1335 { "lt", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PRORES_PROFILE_LT },
1336 0, 0, VE, .unit = "profile" },
1337 { "standard", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PRORES_PROFILE_STANDARD },
1338 0, 0, VE, .unit = "profile" },
1339 { "hq", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PRORES_PROFILE_HQ },
1340 0, 0, VE, .unit = "profile" },
1341 { "4444", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PRORES_PROFILE_4444 },
1342 0, 0, VE, .unit = "profile" },
1343 { "4444xq", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PRORES_PROFILE_4444XQ },
1344 0, 0, VE, .unit = "profile" },
1345 { "vendor", "vendor ID", OFFSET(vendor),
1346 AV_OPT_TYPE_STRING, { .str = "Lavc" }, 0, 0, VE },
1347 { "bits_per_mb", "desired bits per macroblock", OFFSET(bits_per_mb),
1348 AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 8192, VE },
1349 { "quant_mat", "quantiser matrix", OFFSET(quant_sel), AV_OPT_TYPE_INT,
1350 { .i64 = -1 }, -1, QUANT_MAT_DEFAULT, VE, .unit = "quant_mat" },
1351 { "auto", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = -1 },
1352 0, 0, VE, .unit = "quant_mat" },
1353 { "proxy", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = QUANT_MAT_PROXY },
1354 0, 0, VE, .unit = "quant_mat" },
1355 { "lt", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = QUANT_MAT_LT },
1356 0, 0, VE, .unit = "quant_mat" },
1357 { "standard", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = QUANT_MAT_STANDARD },
1358 0, 0, VE, .unit = "quant_mat" },
1359 { "hq", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = QUANT_MAT_HQ },
1360 0, 0, VE, .unit = "quant_mat" },
1361 { "default", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = QUANT_MAT_DEFAULT },
1362 0, 0, VE, .unit = "quant_mat" },
1363 { "alpha_bits", "bits for alpha plane", OFFSET(alpha_bits), AV_OPT_TYPE_INT,
1364 { .i64 = 16 }, 0, 16, VE },
1365 { NULL }
1366 };
1367
1368 static const AVClass proresenc_class = {
1369 .class_name = "ProRes encoder",
1370 .item_name = av_default_item_name,
1371 .option = options,
1372 .version = LIBAVUTIL_VERSION_INT,
1373 };
1374
1375 const FFCodec ff_prores_ks_encoder = {
1376 .p.name = "prores_ks",
1377 CODEC_LONG_NAME("Apple ProRes (iCodec Pro)"),
1378 .p.type = AVMEDIA_TYPE_VIDEO,
1379 .p.id = AV_CODEC_ID_PRORES,
1380 .priv_data_size = sizeof(ProresContext),
1381 .init = encode_init,
1382 .close = encode_close,
1383 FF_CODEC_ENCODE_CB(encode_frame),
1384 .p.capabilities = AV_CODEC_CAP_SLICE_THREADS | AV_CODEC_CAP_FRAME_THREADS |
1385 AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
1386 .p.pix_fmts = (const enum AVPixelFormat[]) {
1387 AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
1388 AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_NONE
1389 },
1390 .p.priv_class = &proresenc_class,
1391 .p.profiles = NULL_IF_CONFIG_SMALL(ff_prores_profiles),
1392 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
1393 };
1394