FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/vc2enc.c
Date: 2024-07-26 21:54:09
Exec Total Coverage
Lines: 525 597 87.9%
Functions: 32 32 100.0%
Branches: 184 270 68.1%

Line Branch Exec Source
1 /*
2 * Copyright (C) 2016 Open Broadcast Systems Ltd.
3 * Author 2016 Rostislav Pehlivanov <atomnuker@gmail.com>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include "libavutil/mem.h"
23 #include "libavutil/pixdesc.h"
24 #include "libavutil/opt.h"
25 #include "libavutil/version.h"
26 #include "codec_internal.h"
27 #include "dirac.h"
28 #include "encode.h"
29 #include "put_bits.h"
30 #include "version.h"
31
32 #include "vc2enc_dwt.h"
33 #include "diractab.h"
34
35 /* The limited size resolution of each slice forces us to do this */
36 #define SSIZE_ROUND(b) (FFALIGN((b), s->size_scaler) + 4 + s->prefix_bytes)
37
38 /* Decides the cutoff point in # of slices to distribute the leftover bytes */
39 #define SLICE_REDIST_TOTAL 150
40
41 typedef struct VC2BaseVideoFormat {
42 enum AVPixelFormat pix_fmt;
43 AVRational time_base;
44 int width, height;
45 uint8_t interlaced, level;
46 char name[13];
47 } VC2BaseVideoFormat;
48
49 static const VC2BaseVideoFormat base_video_fmts[] = {
50 { 0 }, /* Custom format, here just to make indexing equal to base_vf */
51 { AV_PIX_FMT_YUV420P, { 1001, 15000 }, 176, 120, 0, 1, "QSIF525" },
52 { AV_PIX_FMT_YUV420P, { 2, 25 }, 176, 144, 0, 1, "QCIF" },
53 { AV_PIX_FMT_YUV420P, { 1001, 15000 }, 352, 240, 0, 1, "SIF525" },
54 { AV_PIX_FMT_YUV420P, { 2, 25 }, 352, 288, 0, 1, "CIF" },
55 { AV_PIX_FMT_YUV420P, { 1001, 15000 }, 704, 480, 0, 1, "4SIF525" },
56 { AV_PIX_FMT_YUV420P, { 2, 25 }, 704, 576, 0, 1, "4CIF" },
57
58 { AV_PIX_FMT_YUV422P10, { 1001, 30000 }, 720, 480, 1, 2, "SD480I-60" },
59 { AV_PIX_FMT_YUV422P10, { 1, 25 }, 720, 576, 1, 2, "SD576I-50" },
60
61 { AV_PIX_FMT_YUV422P10, { 1001, 60000 }, 1280, 720, 0, 3, "HD720P-60" },
62 { AV_PIX_FMT_YUV422P10, { 1, 50 }, 1280, 720, 0, 3, "HD720P-50" },
63 { AV_PIX_FMT_YUV422P10, { 1001, 30000 }, 1920, 1080, 1, 3, "HD1080I-60" },
64 { AV_PIX_FMT_YUV422P10, { 1, 25 }, 1920, 1080, 1, 3, "HD1080I-50" },
65 { AV_PIX_FMT_YUV422P10, { 1001, 60000 }, 1920, 1080, 0, 3, "HD1080P-60" },
66 { AV_PIX_FMT_YUV422P10, { 1, 50 }, 1920, 1080, 0, 3, "HD1080P-50" },
67
68 { AV_PIX_FMT_YUV444P12, { 1, 24 }, 2048, 1080, 0, 4, "DC2K" },
69 { AV_PIX_FMT_YUV444P12, { 1, 24 }, 4096, 2160, 0, 5, "DC4K" },
70
71 { AV_PIX_FMT_YUV422P10, { 1001, 60000 }, 3840, 2160, 0, 6, "UHDTV 4K-60" },
72 { AV_PIX_FMT_YUV422P10, { 1, 50 }, 3840, 2160, 0, 6, "UHDTV 4K-50" },
73
74 { AV_PIX_FMT_YUV422P10, { 1001, 60000 }, 7680, 4320, 0, 7, "UHDTV 8K-60" },
75 { AV_PIX_FMT_YUV422P10, { 1, 50 }, 7680, 4320, 0, 7, "UHDTV 8K-50" },
76
77 { AV_PIX_FMT_YUV422P10, { 1001, 24000 }, 1920, 1080, 0, 3, "HD1080P-24" },
78 { AV_PIX_FMT_YUV422P10, { 1001, 30000 }, 720, 486, 1, 2, "SD Pro486" },
79 };
80 static const int base_video_fmts_len = FF_ARRAY_ELEMS(base_video_fmts);
81
82 enum VC2_QM {
83 VC2_QM_DEF = 0,
84 VC2_QM_COL,
85 VC2_QM_FLAT,
86
87 VC2_QM_NB
88 };
89
90 typedef struct SubBand {
91 dwtcoef *buf;
92 ptrdiff_t stride;
93 int width;
94 int height;
95 } SubBand;
96
97 typedef struct Plane {
98 SubBand band[MAX_DWT_LEVELS][4];
99 dwtcoef *coef_buf;
100 int width;
101 int height;
102 int dwt_width;
103 int dwt_height;
104 ptrdiff_t coef_stride;
105 } Plane;
106
107 typedef struct SliceArgs {
108 const struct VC2EncContext *ctx;
109 union {
110 int cache[DIRAC_MAX_QUANT_INDEX];
111 uint8_t *buf;
112 };
113 int x;
114 int y;
115 int quant_idx;
116 int bits_ceil;
117 int bits_floor;
118 int bytes;
119 } SliceArgs;
120
121 typedef struct TransformArgs {
122 const struct VC2EncContext *ctx;
123 Plane *plane;
124 const void *idata;
125 ptrdiff_t istride;
126 int field;
127 VC2TransformContext t;
128 } TransformArgs;
129
130 typedef struct VC2EncContext {
131 AVClass *av_class;
132 PutBitContext pb;
133 Plane plane[3];
134 AVCodecContext *avctx;
135 DiracVersionInfo ver;
136
137 SliceArgs *slice_args;
138 TransformArgs transform_args[3];
139
140 /* For conversion from unsigned pixel values to signed */
141 int diff_offset;
142 int bpp;
143 int bpp_idx;
144
145 /* Picture number */
146 uint32_t picture_number;
147
148 /* Base video format */
149 int base_vf;
150 int level;
151 int profile;
152
153 /* Quantization matrix */
154 uint8_t quant[MAX_DWT_LEVELS][4];
155 int custom_quant_matrix;
156
157 /* Division LUT */
158 uint32_t qmagic_lut[116][2];
159
160 int num_x; /* #slices horizontally */
161 int num_y; /* #slices vertically */
162 int prefix_bytes;
163 int size_scaler;
164 int chroma_x_shift;
165 int chroma_y_shift;
166
167 /* Rate control stuff */
168 int frame_max_bytes;
169 int slice_max_bytes;
170 int slice_min_bytes;
171 int q_ceil;
172 int q_avg;
173
174 /* Options */
175 double tolerance;
176 int wavelet_idx;
177 int wavelet_depth;
178 int strict_compliance;
179 int slice_height;
180 int slice_width;
181 int interlaced;
182 enum VC2_QM quant_matrix;
183
184 /* Parse code state */
185 uint32_t next_parse_offset;
186 enum DiracParseCodes last_parse_code;
187 } VC2EncContext;
188
189 35739495 static av_always_inline void put_vc2_ue_uint(PutBitContext *pb, uint32_t val)
190 {
191 int i;
192 35739495 int bits = 0;
193 35739495 unsigned topbit = 1, maxval = 1;
194 35739495 uint64_t pbits = 0;
195
196
2/2
✓ Branch 0 taken 3422589 times.
✓ Branch 1 taken 32316906 times.
35739495 if (!val++) {
197 3422589 put_bits(pb, 1, 1);
198 3422589 return;
199 }
200
201
2/2
✓ Branch 0 taken 123368486 times.
✓ Branch 1 taken 32316906 times.
155685392 while (val > maxval) {
202 123368486 topbit <<= 1;
203 123368486 maxval <<= 1;
204 123368486 maxval |= 1;
205 }
206
207 32316906 bits = ff_log2(topbit);
208
209
2/2
✓ Branch 0 taken 123368486 times.
✓ Branch 1 taken 32316906 times.
155685392 for (i = 0; i < bits; i++) {
210 123368486 topbit >>= 1;
211 av_assert2(pbits <= UINT64_MAX>>3);
212 123368486 pbits <<= 2;
213
2/2
✓ Branch 0 taken 55341066 times.
✓ Branch 1 taken 68027420 times.
123368486 if (val & topbit)
214 55341066 pbits |= 0x1;
215 }
216
217 32316906 put_bits64(pb, bits*2 + 1, (pbits << 1) | 1);
218 }
219
220 35735040 static av_always_inline int count_vc2_ue_uint(uint32_t val)
221 {
222 35735040 int topbit = 1, maxval = 1;
223
224
2/2
✓ Branch 0 taken 3420429 times.
✓ Branch 1 taken 32314611 times.
35735040 if (!val++)
225 3420429 return 1;
226
227
2/2
✓ Branch 0 taken 123360941 times.
✓ Branch 1 taken 32314611 times.
155675552 while (val > maxval) {
228 123360941 topbit <<= 1;
229 123360941 maxval <<= 1;
230 123360941 maxval |= 1;
231 }
232
233 32314611 return ff_log2(topbit)*2 + 1;
234 }
235
236 /* VC-2 10.4 - parse_info() */
237 660 static void encode_parse_info(VC2EncContext *s, enum DiracParseCodes pcode)
238 {
239 uint32_t cur_pos, dist;
240
241 660 align_put_bits(&s->pb);
242
243 660 cur_pos = put_bytes_count(&s->pb, 0);
244
245 /* Magic string */
246 660 ff_put_string(&s->pb, "BBCD", 0);
247
248 /* Parse code */
249 660 put_bits(&s->pb, 8, pcode);
250
251 /* Next parse offset */
252 660 dist = cur_pos - s->next_parse_offset;
253 660 AV_WB32(s->pb.buf + s->next_parse_offset + 5, dist);
254 660 s->next_parse_offset = cur_pos;
255
2/2
✓ Branch 0 taken 165 times.
✓ Branch 1 taken 495 times.
660 put_bits32(&s->pb, pcode == DIRAC_PCODE_END_SEQ ? 13 : 0);
256
257 /* Last parse offset */
258
1/2
✓ Branch 0 taken 660 times.
✗ Branch 1 not taken.
660 put_bits32(&s->pb, s->last_parse_code == DIRAC_PCODE_END_SEQ ? 13 : dist);
259
260 660 s->last_parse_code = pcode;
261 660 }
262
263 /* VC-2 11.1 - parse_parameters()
264 * The level dictates what the decoder should expect in terms of resolution
265 * and allows it to quickly reject whatever it can't support. Remember,
266 * this codec kinda targets cheapo FPGAs without much memory. Unfortunately
267 * it also limits us greatly in our choice of formats, hence the flag to disable
268 * strict_compliance */
269 165 static void encode_parse_params(VC2EncContext *s)
270 {
271 165 put_vc2_ue_uint(&s->pb, s->ver.major); /* VC-2 demands this to be 2 */
272 165 put_vc2_ue_uint(&s->pb, s->ver.minor); /* ^^ and this to be 0 */
273 165 put_vc2_ue_uint(&s->pb, s->profile); /* 3 to signal HQ profile */
274 165 put_vc2_ue_uint(&s->pb, s->level); /* 3 - 1080/720, 6 - 4K */
275 165 }
276
277 /* VC-2 11.3 - frame_size() */
278 165 static void encode_frame_size(VC2EncContext *s)
279 {
280 165 put_bits(&s->pb, 1, !s->strict_compliance);
281
1/2
✓ Branch 0 taken 165 times.
✗ Branch 1 not taken.
165 if (!s->strict_compliance) {
282 165 AVCodecContext *avctx = s->avctx;
283 165 put_vc2_ue_uint(&s->pb, avctx->width);
284 165 put_vc2_ue_uint(&s->pb, avctx->height);
285 }
286 165 }
287
288 /* VC-2 11.3.3 - color_diff_sampling_format() */
289 165 static void encode_sample_fmt(VC2EncContext *s)
290 {
291 165 put_bits(&s->pb, 1, !s->strict_compliance);
292
1/2
✓ Branch 0 taken 165 times.
✗ Branch 1 not taken.
165 if (!s->strict_compliance) {
293 int idx;
294
4/4
✓ Branch 0 taken 120 times.
✓ Branch 1 taken 45 times.
✓ Branch 2 taken 75 times.
✓ Branch 3 taken 45 times.
165 if (s->chroma_x_shift == 1 && s->chroma_y_shift == 0)
295 75 idx = 1; /* 422 */
296
3/4
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 45 times.
✓ Branch 2 taken 45 times.
✗ Branch 3 not taken.
90 else if (s->chroma_x_shift == 1 && s->chroma_y_shift == 1)
297 45 idx = 2; /* 420 */
298 else
299 45 idx = 0; /* 444 */
300 165 put_vc2_ue_uint(&s->pb, idx);
301 }
302 165 }
303
304 /* VC-2 11.3.4 - scan_format() */
305 165 static void encode_scan_format(VC2EncContext *s)
306 {
307 165 put_bits(&s->pb, 1, !s->strict_compliance);
308
1/2
✓ Branch 0 taken 165 times.
✗ Branch 1 not taken.
165 if (!s->strict_compliance)
309 165 put_vc2_ue_uint(&s->pb, s->interlaced);
310 165 }
311
312 /* VC-2 11.3.5 - frame_rate() */
313 165 static void encode_frame_rate(VC2EncContext *s)
314 {
315 165 put_bits(&s->pb, 1, !s->strict_compliance);
316
1/2
✓ Branch 0 taken 165 times.
✗ Branch 1 not taken.
165 if (!s->strict_compliance) {
317 165 AVCodecContext *avctx = s->avctx;
318 165 put_vc2_ue_uint(&s->pb, 0);
319 165 put_vc2_ue_uint(&s->pb, avctx->time_base.den);
320 165 put_vc2_ue_uint(&s->pb, avctx->time_base.num);
321 }
322 165 }
323
324 /* VC-2 11.3.6 - aspect_ratio() */
325 165 static void encode_aspect_ratio(VC2EncContext *s)
326 {
327 165 put_bits(&s->pb, 1, !s->strict_compliance);
328
1/2
✓ Branch 0 taken 165 times.
✗ Branch 1 not taken.
165 if (!s->strict_compliance) {
329 165 AVCodecContext *avctx = s->avctx;
330 165 put_vc2_ue_uint(&s->pb, 0);
331 165 put_vc2_ue_uint(&s->pb, avctx->sample_aspect_ratio.num);
332 165 put_vc2_ue_uint(&s->pb, avctx->sample_aspect_ratio.den);
333 }
334 165 }
335
336 /* VC-2 11.3.7 - clean_area() */
337 165 static void encode_clean_area(VC2EncContext *s)
338 {
339 165 put_bits(&s->pb, 1, 0);
340 165 }
341
342 /* VC-2 11.3.8 - signal_range() */
343 165 static void encode_signal_range(VC2EncContext *s)
344 {
345 165 put_bits(&s->pb, 1, !s->strict_compliance);
346
1/2
✓ Branch 0 taken 165 times.
✗ Branch 1 not taken.
165 if (!s->strict_compliance)
347 165 put_vc2_ue_uint(&s->pb, s->bpp_idx);
348 165 }
349
350 /* VC-2 11.3.9 - color_spec() */
351 165 static void encode_color_spec(VC2EncContext *s)
352 {
353 165 AVCodecContext *avctx = s->avctx;
354 165 put_bits(&s->pb, 1, !s->strict_compliance);
355
1/2
✓ Branch 0 taken 165 times.
✗ Branch 1 not taken.
165 if (!s->strict_compliance) {
356 int val;
357 165 put_vc2_ue_uint(&s->pb, 0);
358
359 /* primaries */
360 165 put_bits(&s->pb, 1, 1);
361
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 165 times.
165 if (avctx->color_primaries == AVCOL_PRI_BT470BG)
362 val = 2;
363
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 165 times.
165 else if (avctx->color_primaries == AVCOL_PRI_SMPTE170M)
364 val = 1;
365
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 165 times.
165 else if (avctx->color_primaries == AVCOL_PRI_SMPTE240M)
366 val = 1;
367 else
368 165 val = 0;
369 165 put_vc2_ue_uint(&s->pb, val);
370
371 /* color matrix */
372 165 put_bits(&s->pb, 1, 1);
373
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 165 times.
165 if (avctx->colorspace == AVCOL_SPC_RGB)
374 val = 3;
375
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 165 times.
165 else if (avctx->colorspace == AVCOL_SPC_YCOCG)
376 val = 2;
377
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 165 times.
165 else if (avctx->colorspace == AVCOL_SPC_BT470BG)
378 val = 1;
379 else
380 165 val = 0;
381 165 put_vc2_ue_uint(&s->pb, val);
382
383 /* transfer function */
384 165 put_bits(&s->pb, 1, 1);
385
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 165 times.
165 if (avctx->color_trc == AVCOL_TRC_LINEAR)
386 val = 2;
387
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 165 times.
165 else if (avctx->color_trc == AVCOL_TRC_BT1361_ECG)
388 val = 1;
389 else
390 165 val = 0;
391 165 put_vc2_ue_uint(&s->pb, val);
392 }
393 165 }
394
395 /* VC-2 11.3 - source_parameters() */
396 165 static void encode_source_params(VC2EncContext *s)
397 {
398 165 encode_frame_size(s);
399 165 encode_sample_fmt(s);
400 165 encode_scan_format(s);
401 165 encode_frame_rate(s);
402 165 encode_aspect_ratio(s);
403 165 encode_clean_area(s);
404 165 encode_signal_range(s);
405 165 encode_color_spec(s);
406 165 }
407
408 /* VC-2 11 - sequence_header() */
409 165 static void encode_seq_header(VC2EncContext *s)
410 {
411 165 align_put_bits(&s->pb);
412 165 encode_parse_params(s);
413 165 put_vc2_ue_uint(&s->pb, s->base_vf);
414 165 encode_source_params(s);
415 165 put_vc2_ue_uint(&s->pb, s->interlaced); /* Frames or fields coding */
416 165 }
417
418 /* VC-2 12.1 - picture_header() */
419 165 static void encode_picture_header(VC2EncContext *s)
420 {
421 165 align_put_bits(&s->pb);
422 165 put_bits32(&s->pb, s->picture_number++);
423 165 }
424
425 /* VC-2 12.3.4.1 - slice_parameters() */
426 165 static void encode_slice_params(VC2EncContext *s)
427 {
428 165 put_vc2_ue_uint(&s->pb, s->num_x);
429 165 put_vc2_ue_uint(&s->pb, s->num_y);
430 165 put_vc2_ue_uint(&s->pb, s->prefix_bytes);
431 165 put_vc2_ue_uint(&s->pb, s->size_scaler);
432 165 }
433
434 /* 1st idx = LL, second - vertical, third - horizontal, fourth - total */
435 static const uint8_t vc2_qm_col_tab[][4] = {
436 {20, 9, 15, 4},
437 { 0, 6, 6, 4},
438 { 0, 3, 3, 5},
439 { 0, 3, 5, 1},
440 { 0, 11, 10, 11}
441 };
442
443 static const uint8_t vc2_qm_flat_tab[][4] = {
444 { 0, 0, 0, 0},
445 { 0, 0, 0, 0},
446 { 0, 0, 0, 0},
447 { 0, 0, 0, 0},
448 { 0, 0, 0, 0}
449 };
450
451 165 static void init_quant_matrix(VC2EncContext *s)
452 {
453 int level, orientation;
454
455
2/4
✓ Branch 0 taken 165 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 165 times.
✗ Branch 3 not taken.
165 if (s->wavelet_depth <= 4 && s->quant_matrix == VC2_QM_DEF) {
456 165 s->custom_quant_matrix = 0;
457
2/2
✓ Branch 0 taken 660 times.
✓ Branch 1 taken 165 times.
825 for (level = 0; level < s->wavelet_depth; level++) {
458 660 s->quant[level][0] = ff_dirac_default_qmat[s->wavelet_idx][level][0];
459 660 s->quant[level][1] = ff_dirac_default_qmat[s->wavelet_idx][level][1];
460 660 s->quant[level][2] = ff_dirac_default_qmat[s->wavelet_idx][level][2];
461 660 s->quant[level][3] = ff_dirac_default_qmat[s->wavelet_idx][level][3];
462 }
463 165 return;
464 }
465
466 s->custom_quant_matrix = 1;
467
468 if (s->quant_matrix == VC2_QM_DEF) {
469 for (level = 0; level < s->wavelet_depth; level++) {
470 for (orientation = 0; orientation < 4; orientation++) {
471 if (level <= 3)
472 s->quant[level][orientation] = ff_dirac_default_qmat[s->wavelet_idx][level][orientation];
473 else
474 s->quant[level][orientation] = vc2_qm_col_tab[level][orientation];
475 }
476 }
477 } else if (s->quant_matrix == VC2_QM_COL) {
478 for (level = 0; level < s->wavelet_depth; level++) {
479 for (orientation = 0; orientation < 4; orientation++) {
480 s->quant[level][orientation] = vc2_qm_col_tab[level][orientation];
481 }
482 }
483 } else {
484 for (level = 0; level < s->wavelet_depth; level++) {
485 for (orientation = 0; orientation < 4; orientation++) {
486 s->quant[level][orientation] = vc2_qm_flat_tab[level][orientation];
487 }
488 }
489 }
490 }
491
492 /* VC-2 12.3.4.2 - quant_matrix() */
493 165 static void encode_quant_matrix(VC2EncContext *s)
494 {
495 int level;
496 165 put_bits(&s->pb, 1, s->custom_quant_matrix);
497
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 165 times.
165 if (s->custom_quant_matrix) {
498 put_vc2_ue_uint(&s->pb, s->quant[0][0]);
499 for (level = 0; level < s->wavelet_depth; level++) {
500 put_vc2_ue_uint(&s->pb, s->quant[level][1]);
501 put_vc2_ue_uint(&s->pb, s->quant[level][2]);
502 put_vc2_ue_uint(&s->pb, s->quant[level][3]);
503 }
504 }
505 165 }
506
507 /* VC-2 12.3 - transform_parameters() */
508 165 static void encode_transform_params(VC2EncContext *s)
509 {
510 165 put_vc2_ue_uint(&s->pb, s->wavelet_idx);
511 165 put_vc2_ue_uint(&s->pb, s->wavelet_depth);
512
513 165 encode_slice_params(s);
514 165 encode_quant_matrix(s);
515 165 }
516
517 /* VC-2 12.2 - wavelet_transform() */
518 165 static void encode_wavelet_transform(VC2EncContext *s)
519 {
520 165 encode_transform_params(s);
521 165 align_put_bits(&s->pb);
522 165 }
523
524 /* VC-2 12 - picture_parse() */
525 165 static void encode_picture_start(VC2EncContext *s)
526 {
527 165 align_put_bits(&s->pb);
528 165 encode_picture_header(s);
529 165 align_put_bits(&s->pb);
530 165 encode_wavelet_transform(s);
531 165 }
532
533 #define QUANT(c, mul, add, shift) (((mul) * (c) + (add)) >> (shift))
534
535 /* VC-2 13.5.5.2 - slice_band() */
536 1274130 static void encode_subband(const VC2EncContext *s, PutBitContext *pb,
537 int sx, int sy, const SubBand *b, int quant)
538 {
539 int x, y;
540
541 1274130 const int left = b->width * (sx+0) / s->num_x;
542 1274130 const int right = b->width * (sx+1) / s->num_x;
543 1274130 const int top = b->height * (sy+0) / s->num_y;
544 1274130 const int bottom = b->height * (sy+1) / s->num_y;
545
546 1274130 dwtcoef *coeff = b->buf + top * b->stride;
547 1274130 const uint64_t q_m = ((uint64_t)(s->qmagic_lut[quant][0])) << 2;
548 1274130 const uint64_t q_a = s->qmagic_lut[quant][1];
549 1274130 const int q_s = av_log2(ff_dirac_qscale_tab[quant]) + 32;
550
551
2/2
✓ Branch 0 taken 4098600 times.
✓ Branch 1 taken 1274130 times.
5372730 for (y = top; y < bottom; y++) {
552
2/2
✓ Branch 0 taken 35735040 times.
✓ Branch 1 taken 4098600 times.
39833640 for (x = left; x < right; x++) {
553
2/2
✓ Branch 0 taken 19457858 times.
✓ Branch 1 taken 16277182 times.
35735040 uint32_t c_abs = QUANT(FFABS(coeff[x]), q_m, q_a, q_s);
554 35735040 put_vc2_ue_uint(pb, c_abs);
555
2/2
✓ Branch 0 taken 32314611 times.
✓ Branch 1 taken 3420429 times.
35735040 if (c_abs)
556 32314611 put_bits(pb, 1, coeff[x] < 0);
557 }
558 4098600 coeff += b->stride;
559 }
560 1274130 }
561
562 130680 static int count_hq_slice(SliceArgs *slice, int quant_idx)
563 {
564 int x, y;
565 uint8_t quants[MAX_DWT_LEVELS][4];
566 130680 int bits = 0, p, level, orientation;
567 130680 const VC2EncContext *s = slice->ctx;
568
569
2/2
✓ Branch 0 taken 98010 times.
✓ Branch 1 taken 32670 times.
130680 if (slice->cache[quant_idx])
570 98010 return slice->cache[quant_idx];
571
572 32670 bits += 8*s->prefix_bytes;
573 32670 bits += 8; /* quant_idx */
574
575
2/2
✓ Branch 0 taken 130680 times.
✓ Branch 1 taken 32670 times.
163350 for (level = 0; level < s->wavelet_depth; level++)
576
2/2
✓ Branch 0 taken 424710 times.
✓ Branch 1 taken 130680 times.
555390 for (orientation = !!level; orientation < 4; orientation++)
577 424710 quants[level][orientation] = FFMAX(quant_idx - s->quant[level][orientation], 0);
578
579
2/2
✓ Branch 0 taken 98010 times.
✓ Branch 1 taken 32670 times.
130680 for (p = 0; p < 3; p++) {
580 int bytes_start, bytes_len, pad_s, pad_c;
581 98010 bytes_start = bits >> 3;
582 98010 bits += 8;
583
2/2
✓ Branch 0 taken 392040 times.
✓ Branch 1 taken 98010 times.
490050 for (level = 0; level < s->wavelet_depth; level++) {
584
2/2
✓ Branch 0 taken 1274130 times.
✓ Branch 1 taken 392040 times.
1666170 for (orientation = !!level; orientation < 4; orientation++) {
585 1274130 const SubBand *b = &s->plane[p].band[level][orientation];
586
587 1274130 const int q_idx = quants[level][orientation];
588 1274130 const uint64_t q_m = ((uint64_t)s->qmagic_lut[q_idx][0]) << 2;
589 1274130 const uint64_t q_a = s->qmagic_lut[q_idx][1];
590 1274130 const int q_s = av_log2(ff_dirac_qscale_tab[q_idx]) + 32;
591
592 1274130 const int left = b->width * slice->x / s->num_x;
593 1274130 const int right = b->width *(slice->x+1) / s->num_x;
594 1274130 const int top = b->height * slice->y / s->num_y;
595 1274130 const int bottom = b->height *(slice->y+1) / s->num_y;
596
597 1274130 dwtcoef *buf = b->buf + top * b->stride;
598
599
2/2
✓ Branch 0 taken 4098600 times.
✓ Branch 1 taken 1274130 times.
5372730 for (y = top; y < bottom; y++) {
600
2/2
✓ Branch 0 taken 35735040 times.
✓ Branch 1 taken 4098600 times.
39833640 for (x = left; x < right; x++) {
601
2/2
✓ Branch 0 taken 19457858 times.
✓ Branch 1 taken 16277182 times.
35735040 uint32_t c_abs = QUANT(FFABS(buf[x]), q_m, q_a, q_s);
602 35735040 bits += count_vc2_ue_uint(c_abs);
603 35735040 bits += !!c_abs;
604 }
605 4098600 buf += b->stride;
606 }
607 }
608 }
609 98010 bits += FFALIGN(bits, 8) - bits;
610 98010 bytes_len = (bits >> 3) - bytes_start - 1;
611 98010 pad_s = FFALIGN(bytes_len, s->size_scaler)/s->size_scaler;
612 98010 pad_c = (pad_s*s->size_scaler) - bytes_len;
613 98010 bits += pad_c*8;
614 }
615
616 32670 slice->cache[quant_idx] = bits;
617
618 32670 return bits;
619 }
620
621 /* Approaches the best possible quantizer asymptotically, its kinda exaustive
622 * but we have a LUT to get the coefficient size in bits. Guaranteed to never
623 * overshoot, which is apparently very important when streaming */
624 32670 static int rate_control(AVCodecContext *avctx, void *arg)
625 {
626 32670 SliceArgs *slice_dat = arg;
627 32670 const VC2EncContext *s = slice_dat->ctx;
628 32670 const int top = slice_dat->bits_ceil;
629 32670 const int bottom = slice_dat->bits_floor;
630 32670 int quant_buf[2] = {-1, -1};
631 32670 int quant = slice_dat->quant_idx, step = 1;
632 32670 int bits_last, bits = count_hq_slice(slice_dat, quant);
633
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 98010 times.
✓ Branch 2 taken 98010 times.
✗ Branch 3 not taken.
98010 while ((bits > top) || (bits < bottom)) {
634
1/2
✓ Branch 0 taken 98010 times.
✗ Branch 1 not taken.
98010 const int signed_step = bits > top ? +step : -step;
635 98010 quant = av_clip(quant + signed_step, 0, s->q_ceil-1);
636 98010 bits = count_hq_slice(slice_dat, quant);
637
2/2
✓ Branch 0 taken 32670 times.
✓ Branch 1 taken 65340 times.
98010 if (quant_buf[1] == quant) {
638 32670 quant = FFMAX(quant_buf[0], quant);
639
1/2
✓ Branch 0 taken 32670 times.
✗ Branch 1 not taken.
32670 bits = quant == quant_buf[0] ? bits_last : bits;
640 32670 break;
641 }
642 65340 step = av_clip(step/2, 1, (s->q_ceil-1)/2);
643 65340 quant_buf[1] = quant_buf[0];
644 65340 quant_buf[0] = quant;
645 65340 bits_last = bits;
646 }
647 32670 slice_dat->quant_idx = av_clip(quant, 0, s->q_ceil-1);
648 32670 slice_dat->bytes = SSIZE_ROUND(bits >> 3);
649 32670 return 0;
650 }
651
652 165 static int calc_slice_sizes(VC2EncContext *s)
653 {
654 165 int i, j, slice_x, slice_y, bytes_left = 0;
655 165 int bytes_top[SLICE_REDIST_TOTAL] = {0};
656 165 int64_t total_bytes_needed = 0;
657 165 int slice_redist_range = FFMIN(SLICE_REDIST_TOTAL, s->num_x*s->num_y);
658 165 SliceArgs *enc_args = s->slice_args;
659 165 SliceArgs *top_loc[SLICE_REDIST_TOTAL] = {NULL};
660
661 165 init_quant_matrix(s);
662
663
2/2
✓ Branch 0 taken 2970 times.
✓ Branch 1 taken 165 times.
3135 for (slice_y = 0; slice_y < s->num_y; slice_y++) {
664
2/2
✓ Branch 0 taken 32670 times.
✓ Branch 1 taken 2970 times.
35640 for (slice_x = 0; slice_x < s->num_x; slice_x++) {
665 32670 SliceArgs *args = &enc_args[s->num_x*slice_y + slice_x];
666 32670 args->ctx = s;
667 32670 args->x = slice_x;
668 32670 args->y = slice_y;
669 32670 args->bits_ceil = s->slice_max_bytes << 3;
670 32670 args->bits_floor = s->slice_min_bytes << 3;
671 32670 memset(args->cache, 0, s->q_ceil*sizeof(*args->cache));
672 }
673 }
674
675 /* First pass - determine baseline slice sizes w.r.t. max_slice_size */
676 165 s->avctx->execute(s->avctx, rate_control, enc_args, NULL, s->num_x*s->num_y,
677 sizeof(SliceArgs));
678
679
2/2
✓ Branch 0 taken 32670 times.
✓ Branch 1 taken 165 times.
32835 for (i = 0; i < s->num_x*s->num_y; i++) {
680 32670 SliceArgs *args = &enc_args[i];
681 32670 bytes_left += args->bytes;
682
1/2
✓ Branch 0 taken 1155403 times.
✗ Branch 1 not taken.
1155403 for (j = 0; j < slice_redist_range; j++) {
683
2/2
✓ Branch 0 taken 32670 times.
✓ Branch 1 taken 1122733 times.
1155403 if (args->bytes > bytes_top[j]) {
684 32670 bytes_top[j] = args->bytes;
685 32670 top_loc[j] = args;
686 32670 break;
687 }
688 }
689 }
690
691 165 bytes_left = s->frame_max_bytes - bytes_left;
692
693 /* Second pass - distribute leftover bytes */
694
1/2
✓ Branch 0 taken 165 times.
✗ Branch 1 not taken.
165 while (bytes_left > 0) {
695 165 int distributed = 0;
696
1/2
✓ Branch 0 taken 165 times.
✗ Branch 1 not taken.
165 for (i = 0; i < slice_redist_range; i++) {
697 SliceArgs *args;
698 int bits, bytes, diff, prev_bytes, new_idx;
699
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 165 times.
165 if (bytes_left <= 0)
700 break;
701
2/4
✓ Branch 0 taken 165 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 165 times.
165 if (!top_loc[i] || !top_loc[i]->quant_idx)
702 break;
703 args = top_loc[i];
704 prev_bytes = args->bytes;
705 new_idx = FFMAX(args->quant_idx - 1, 0);
706 bits = count_hq_slice(args, new_idx);
707 bytes = SSIZE_ROUND(bits >> 3);
708 diff = bytes - prev_bytes;
709 if ((bytes_left - diff) > 0) {
710 args->quant_idx = new_idx;
711 args->bytes = bytes;
712 bytes_left -= diff;
713 distributed++;
714 }
715 }
716
1/2
✓ Branch 0 taken 165 times.
✗ Branch 1 not taken.
165 if (!distributed)
717 165 break;
718 }
719
720
2/2
✓ Branch 0 taken 32670 times.
✓ Branch 1 taken 165 times.
32835 for (i = 0; i < s->num_x*s->num_y; i++) {
721 32670 SliceArgs *args = &enc_args[i];
722 32670 total_bytes_needed += args->bytes;
723 32670 s->q_avg = (s->q_avg + args->quant_idx)/2;
724 }
725
726 165 return total_bytes_needed;
727 }
728
729 /* VC-2 13.5.3 - hq_slice */
730 32670 static int encode_hq_slice(AVCodecContext *avctx, void *arg)
731 {
732 32670 const SliceArgs *slice_dat = arg;
733 32670 const VC2EncContext *s = slice_dat->ctx;
734 32670 PutBitContext pb0, *const pb = &pb0;
735 32670 const int slice_x = slice_dat->x;
736 32670 const int slice_y = slice_dat->y;
737 32670 const int quant_idx = slice_dat->quant_idx;
738 32670 const int slice_bytes_max = slice_dat->bytes;
739 uint8_t quants[MAX_DWT_LEVELS][4];
740 int p, level, orientation;
741
742 /* The reference decoder ignores it, and its typical length is 0 */
743 32670 memset(slice_dat->buf, 0, s->prefix_bytes);
744
745 32670 init_put_bits(pb, slice_dat->buf + s->prefix_bytes, slice_dat->bytes - s->prefix_bytes);
746
747 32670 put_bits(pb, 8, quant_idx);
748
749 /* Slice quantization (slice_quantizers() in the specs) */
750
2/2
✓ Branch 0 taken 130680 times.
✓ Branch 1 taken 32670 times.
163350 for (level = 0; level < s->wavelet_depth; level++)
751
2/2
✓ Branch 0 taken 424710 times.
✓ Branch 1 taken 130680 times.
555390 for (orientation = !!level; orientation < 4; orientation++)
752 424710 quants[level][orientation] = FFMAX(quant_idx - s->quant[level][orientation], 0);
753
754 /* Luma + 2 Chroma planes */
755
2/2
✓ Branch 0 taken 98010 times.
✓ Branch 1 taken 32670 times.
130680 for (p = 0; p < 3; p++) {
756 int bytes_start, bytes_len, pad_s, pad_c;
757 98010 bytes_start = put_bytes_count(pb, 0);
758 98010 put_bits(pb, 8, 0);
759
2/2
✓ Branch 0 taken 392040 times.
✓ Branch 1 taken 98010 times.
490050 for (level = 0; level < s->wavelet_depth; level++) {
760
2/2
✓ Branch 0 taken 1274130 times.
✓ Branch 1 taken 392040 times.
1666170 for (orientation = !!level; orientation < 4; orientation++) {
761 1274130 encode_subband(s, pb, slice_x, slice_y,
762 &s->plane[p].band[level][orientation],
763 1274130 quants[level][orientation]);
764 }
765 }
766 98010 flush_put_bits(pb);
767 98010 bytes_len = put_bytes_output(pb) - bytes_start - 1;
768
2/2
✓ Branch 0 taken 32670 times.
✓ Branch 1 taken 65340 times.
98010 if (p == 2) {
769 32670 int len_diff = slice_bytes_max - put_bytes_output(pb);
770 32670 pad_s = FFALIGN((bytes_len + len_diff), s->size_scaler)/s->size_scaler;
771 32670 pad_c = (pad_s*s->size_scaler) - bytes_len;
772 } else {
773 65340 pad_s = FFALIGN(bytes_len, s->size_scaler)/s->size_scaler;
774 65340 pad_c = (pad_s*s->size_scaler) - bytes_len;
775 }
776 98010 pb->buf[bytes_start] = pad_s;
777 /* vc2-reference uses that padding that decodes to '0' coeffs */
778 98010 memset(put_bits_ptr(pb), 0xFF, pad_c);
779 98010 skip_put_bytes(pb, pad_c);
780 }
781
782 32670 return 0;
783 }
784
785 /* VC-2 13.5.1 - low_delay_transform_data() */
786 165 static int encode_slices(VC2EncContext *s)
787 {
788 uint8_t *buf;
789 165 int slice_x, slice_y, skip = 0;
790 165 SliceArgs *enc_args = s->slice_args;
791
792 165 flush_put_bits(&s->pb);
793 165 buf = put_bits_ptr(&s->pb);
794
795
2/2
✓ Branch 0 taken 2970 times.
✓ Branch 1 taken 165 times.
3135 for (slice_y = 0; slice_y < s->num_y; slice_y++) {
796
2/2
✓ Branch 0 taken 32670 times.
✓ Branch 1 taken 2970 times.
35640 for (slice_x = 0; slice_x < s->num_x; slice_x++) {
797 32670 SliceArgs *args = &enc_args[s->num_x*slice_y + slice_x];
798 32670 args->buf = buf + skip;
799 32670 skip += args->bytes;
800 }
801 }
802
803 165 s->avctx->execute(s->avctx, encode_hq_slice, enc_args, NULL, s->num_x*s->num_y,
804 sizeof(SliceArgs));
805
806 165 skip_put_bytes(&s->pb, skip);
807
808 165 return 0;
809 }
810
811 /*
812 * Transform basics for a 3 level transform
813 * |---------------------------------------------------------------------|
814 * | LL-0 | HL-0 | | |
815 * |--------|-------| HL-1 | |
816 * | LH-0 | HH-0 | | |
817 * |----------------|-----------------| HL-2 |
818 * | | | |
819 * | LH-1 | HH-1 | |
820 * | | | |
821 * |----------------------------------|----------------------------------|
822 * | | |
823 * | | |
824 * | | |
825 * | LH-2 | HH-2 |
826 * | | |
827 * | | |
828 * | | |
829 * |---------------------------------------------------------------------|
830 *
831 * DWT transforms are generally applied by splitting the image in two vertically
832 * and applying a low pass transform on the left part and a corresponding high
833 * pass transform on the right hand side. This is known as the horizontal filter
834 * stage.
835 * After that, the same operation is performed except the image is divided
836 * horizontally, with the high pass on the lower and the low pass on the higher
837 * side.
838 * Therefore, you're left with 4 subdivisions - known as low-low, low-high,
839 * high-low and high-high. They're referred to as orientations in the decoder
840 * and encoder.
841 *
842 * The LL (low-low) area contains the original image downsampled by the amount
843 * of levels. The rest of the areas can be thought as the details needed
844 * to restore the image perfectly to its original size.
845 */
846 495 static int dwt_plane(AVCodecContext *avctx, void *arg)
847 {
848 495 TransformArgs *transform_dat = arg;
849 495 const VC2EncContext *s = transform_dat->ctx;
850 495 const void *frame_data = transform_dat->idata;
851 495 const ptrdiff_t linesize = transform_dat->istride;
852 495 const int field = transform_dat->field;
853 495 const Plane *p = transform_dat->plane;
854 495 VC2TransformContext *t = &transform_dat->t;
855 495 dwtcoef *buf = p->coef_buf;
856 495 const int idx = s->wavelet_idx;
857 495 const int skip = 1 + s->interlaced;
858
859 int x, y, level, offset;
860 495 ptrdiff_t pix_stride = linesize >> (s->bpp - 1);
861
862
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 495 times.
495 if (field == 1) {
863 offset = 0;
864 pix_stride <<= 1;
865
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 495 times.
495 } else if (field == 2) {
866 offset = pix_stride;
867 pix_stride <<= 1;
868 } else {
869 495 offset = 0;
870 }
871
872
2/2
✓ Branch 0 taken 135 times.
✓ Branch 1 taken 360 times.
495 if (s->bpp == 1) {
873 135 const uint8_t *pix = (const uint8_t *)frame_data + offset;
874
2/2
✓ Branch 0 taken 34560 times.
✓ Branch 1 taken 135 times.
34695 for (y = 0; y < p->height*skip; y+=skip) {
875
2/2
✓ Branch 0 taken 9884160 times.
✓ Branch 1 taken 34560 times.
9918720 for (x = 0; x < p->width; x++) {
876 9884160 buf[x] = pix[x] - s->diff_offset;
877 }
878 34560 memset(&buf[x], 0, (p->coef_stride - p->width)*sizeof(dwtcoef));
879 34560 buf += p->coef_stride;
880 34560 pix += pix_stride;
881 }
882 } else {
883 360 const uint16_t *pix = (const uint16_t *)frame_data + offset;
884
2/2
✓ Branch 0 taken 95040 times.
✓ Branch 1 taken 360 times.
95400 for (y = 0; y < p->height*skip; y+=skip) {
885
2/2
✓ Branch 0 taken 25850880 times.
✓ Branch 1 taken 95040 times.
25945920 for (x = 0; x < p->width; x++) {
886 25850880 buf[x] = pix[x] - s->diff_offset;
887 }
888 95040 memset(&buf[x], 0, (p->coef_stride - p->width)*sizeof(dwtcoef));
889 95040 buf += p->coef_stride;
890 95040 pix += pix_stride;
891 }
892 }
893
894 495 memset(buf, 0, p->coef_stride * (p->dwt_height - p->height) * sizeof(dwtcoef));
895
896
2/2
✓ Branch 0 taken 1980 times.
✓ Branch 1 taken 495 times.
2475 for (level = s->wavelet_depth-1; level >= 0; level--) {
897 1980 const SubBand *b = &p->band[level][0];
898 1980 t->vc2_subband_dwt[idx](t, p->coef_buf, p->coef_stride,
899 1980 b->width, b->height);
900 }
901
902 495 return 0;
903 }
904
905 165 static int encode_frame(VC2EncContext *s, AVPacket *avpkt, const AVFrame *frame,
906 const char *aux_data, const int header_size, int field)
907 {
908 int i, ret;
909 int64_t max_frame_bytes;
910
911 /* Threaded DWT transform */
912
2/2
✓ Branch 0 taken 495 times.
✓ Branch 1 taken 165 times.
660 for (i = 0; i < 3; i++) {
913 495 s->transform_args[i].ctx = s;
914 495 s->transform_args[i].field = field;
915 495 s->transform_args[i].plane = &s->plane[i];
916 495 s->transform_args[i].idata = frame->data[i];
917 495 s->transform_args[i].istride = frame->linesize[i];
918 }
919 165 s->avctx->execute(s->avctx, dwt_plane, s->transform_args, NULL, 3,
920 sizeof(TransformArgs));
921
922 /* Calculate per-slice quantizers and sizes */
923 165 max_frame_bytes = header_size + calc_slice_sizes(s);
924
925
1/2
✓ Branch 0 taken 165 times.
✗ Branch 1 not taken.
165 if (field < 2) {
926 165 ret = ff_get_encode_buffer(s->avctx, avpkt,
927 165 max_frame_bytes << s->interlaced, 0);
928
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 165 times.
165 if (ret < 0)
929 return ret;
930 165 init_put_bits(&s->pb, avpkt->data, avpkt->size);
931 }
932
933 /* Sequence header */
934 165 encode_parse_info(s, DIRAC_PCODE_SEQ_HEADER);
935 165 encode_seq_header(s);
936
937 /* Encoder version */
938
1/2
✓ Branch 0 taken 165 times.
✗ Branch 1 not taken.
165 if (aux_data) {
939 165 encode_parse_info(s, DIRAC_PCODE_AUX);
940 165 ff_put_string(&s->pb, aux_data, 1);
941 }
942
943 /* Picture header */
944 165 encode_parse_info(s, DIRAC_PCODE_PICTURE_HQ);
945 165 encode_picture_start(s);
946
947 /* Encode slices */
948 165 encode_slices(s);
949
950 /* End sequence */
951 165 encode_parse_info(s, DIRAC_PCODE_END_SEQ);
952
953 165 return 0;
954 }
955
956 165 static av_cold int vc2_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
957 const AVFrame *frame, int *got_packet)
958 {
959 165 int ret = 0;
960 165 int slice_ceil, sig_size = 256;
961 165 VC2EncContext *s = avctx->priv_data;
962 165 const int bitexact = avctx->flags & AV_CODEC_FLAG_BITEXACT;
963
1/2
✓ Branch 0 taken 165 times.
✗ Branch 1 not taken.
165 const char *aux_data = bitexact ? "Lavc" : LIBAVCODEC_IDENT;
964
1/2
✓ Branch 0 taken 165 times.
✗ Branch 1 not taken.
165 const int aux_data_size = bitexact ? sizeof("Lavc") : sizeof(LIBAVCODEC_IDENT);
965 165 const int header_size = 100 + aux_data_size;
966 165 int64_t r_bitrate = avctx->bit_rate >> (s->interlaced);
967
968 165 s->avctx = avctx;
969 165 s->size_scaler = 2;
970 165 s->prefix_bytes = 0;
971 165 s->last_parse_code = 0;
972 165 s->next_parse_offset = 0;
973
974 /* Rate control */
975 165 s->frame_max_bytes = (av_rescale(r_bitrate, s->avctx->time_base.num,
976 165 s->avctx->time_base.den) >> 3) - header_size;
977 165 s->slice_max_bytes = slice_ceil = av_rescale(s->frame_max_bytes, 1, s->num_x*s->num_y);
978
979 /* Find an appropriate size scaler */
980
2/2
✓ Branch 0 taken 990 times.
✓ Branch 1 taken 165 times.
1155 while (sig_size > 255) {
981 990 int r_size = SSIZE_ROUND(s->slice_max_bytes);
982
2/2
✓ Branch 0 taken 825 times.
✓ Branch 1 taken 165 times.
990 if (r_size > slice_ceil) {
983 825 s->slice_max_bytes -= r_size - slice_ceil;
984 825 r_size = SSIZE_ROUND(s->slice_max_bytes);
985 }
986 990 sig_size = r_size/s->size_scaler; /* Signalled slize size */
987 990 s->size_scaler <<= 1;
988 }
989
990 165 s->slice_min_bytes = s->slice_max_bytes - s->slice_max_bytes*(s->tolerance/100.0f);
991
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 165 times.
165 if (s->slice_min_bytes < 0)
992 return AVERROR(EINVAL);
993
994 165 ret = encode_frame(s, avpkt, frame, aux_data, header_size, s->interlaced);
995
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 165 times.
165 if (ret)
996 return ret;
997
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 165 times.
165 if (s->interlaced) {
998 ret = encode_frame(s, avpkt, frame, aux_data, header_size, 2);
999 if (ret)
1000 return ret;
1001 }
1002
1003 165 flush_put_bits(&s->pb);
1004 165 av_shrink_packet(avpkt, put_bytes_output(&s->pb));
1005
1006 165 *got_packet = 1;
1007
1008 165 return 0;
1009 }
1010
1011 33 static av_cold int vc2_encode_end(AVCodecContext *avctx)
1012 {
1013 int i;
1014 33 VC2EncContext *s = avctx->priv_data;
1015
1016 33 av_log(avctx, AV_LOG_INFO, "Qavg: %i\n", s->q_avg);
1017
1018
2/2
✓ Branch 0 taken 99 times.
✓ Branch 1 taken 33 times.
132 for (i = 0; i < 3; i++) {
1019 99 ff_vc2enc_free_transforms(&s->transform_args[i].t);
1020 99 av_freep(&s->plane[i].coef_buf);
1021 }
1022
1023 33 av_freep(&s->slice_args);
1024
1025 33 return 0;
1026 }
1027
1028 33 static av_cold int vc2_encode_init(AVCodecContext *avctx)
1029 {
1030 Plane *p;
1031 SubBand *b;
1032 int i, level, o, shift;
1033 const AVPixFmtDescriptor *pixdesc;
1034 int depth;
1035 33 VC2EncContext *s = avctx->priv_data;
1036
1037 33 s->picture_number = 0;
1038
1039 /* Total allowed quantization range */
1040 33 s->q_ceil = DIRAC_MAX_QUANT_INDEX;
1041
1042 33 s->ver.major = 2;
1043 33 s->ver.minor = 0;
1044 33 s->profile = 3;
1045 33 s->level = 3;
1046
1047 33 s->base_vf = -1;
1048 33 s->strict_compliance = 1;
1049
1050 33 s->q_avg = 0;
1051 33 s->slice_max_bytes = 0;
1052 33 s->slice_min_bytes = 0;
1053
1054 /* Mark unknown as progressive */
1055
1/2
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
66 s->interlaced = !((avctx->field_order == AV_FIELD_UNKNOWN) ||
1056
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 (avctx->field_order == AV_FIELD_PROGRESSIVE));
1057
1058
2/2
✓ Branch 0 taken 759 times.
✓ Branch 1 taken 33 times.
792 for (i = 0; i < base_video_fmts_len; i++) {
1059 759 const VC2BaseVideoFormat *fmt = &base_video_fmts[i];
1060
2/2
✓ Branch 0 taken 606 times.
✓ Branch 1 taken 153 times.
759 if (avctx->pix_fmt != fmt->pix_fmt)
1061 606 continue;
1062
2/2
✓ Branch 0 taken 93 times.
✓ Branch 1 taken 60 times.
153 if (avctx->time_base.num != fmt->time_base.num)
1063 93 continue;
1064
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 18 times.
60 if (avctx->time_base.den != fmt->time_base.den)
1065 42 continue;
1066
1/2
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
18 if (avctx->width != fmt->width)
1067 18 continue;
1068 if (avctx->height != fmt->height)
1069 continue;
1070 if (s->interlaced != fmt->interlaced)
1071 continue;
1072 s->base_vf = i;
1073 s->level = base_video_fmts[i].level;
1074 break;
1075 }
1076
1077
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 if (s->interlaced)
1078 av_log(avctx, AV_LOG_WARNING, "Interlacing enabled!\n");
1079
1080
1/2
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
33 if ((s->slice_width & (s->slice_width - 1)) ||
1081
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 (s->slice_height & (s->slice_height - 1))) {
1082 av_log(avctx, AV_LOG_ERROR, "Slice size is not a power of two!\n");
1083 return AVERROR_UNKNOWN;
1084 }
1085
1086
1/2
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
33 if ((s->slice_width > avctx->width) ||
1087
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 (s->slice_height > avctx->height)) {
1088 av_log(avctx, AV_LOG_ERROR, "Slice size is bigger than the image!\n");
1089 return AVERROR_UNKNOWN;
1090 }
1091
1092
1/2
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
33 if (s->base_vf <= 0) {
1093
1/2
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
33 if (avctx->strict_std_compliance < FF_COMPLIANCE_STRICT) {
1094 33 s->strict_compliance = s->base_vf = 0;
1095 33 av_log(avctx, AV_LOG_WARNING, "Format does not strictly comply with VC2 specs\n");
1096 } else {
1097 av_log(avctx, AV_LOG_WARNING, "Given format does not strictly comply with "
1098 "the specifications, decrease strictness to use it.\n");
1099 return AVERROR_UNKNOWN;
1100 }
1101 } else {
1102 av_log(avctx, AV_LOG_INFO, "Selected base video format = %i (%s)\n",
1103 s->base_vf, base_video_fmts[s->base_vf].name);
1104 }
1105
1106 33 pixdesc = av_pix_fmt_desc_get(avctx->pix_fmt);
1107 /* Chroma subsampling */
1108 33 s->chroma_x_shift = pixdesc->log2_chroma_w;
1109 33 s->chroma_y_shift = pixdesc->log2_chroma_h;
1110
1111 /* Bit depth and color range index */
1112 33 depth = pixdesc->comp[0].depth;
1113
3/4
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 9 times.
33 if (depth == 8 && avctx->color_range == AVCOL_RANGE_JPEG) {
1114 s->bpp = 1;
1115 s->bpp_idx = 1;
1116 s->diff_offset = 128;
1117
3/4
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 24 times.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
33 } else if (depth == 8 && (avctx->color_range == AVCOL_RANGE_MPEG ||
1118
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 avctx->color_range == AVCOL_RANGE_UNSPECIFIED)) {
1119 9 s->bpp = 1;
1120 9 s->bpp_idx = 2;
1121 9 s->diff_offset = 128;
1122
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 9 times.
24 } else if (depth == 10) {
1123 15 s->bpp = 2;
1124 15 s->bpp_idx = 3;
1125 15 s->diff_offset = 512;
1126 } else {
1127 9 s->bpp = 2;
1128 9 s->bpp_idx = 4;
1129 9 s->diff_offset = 2048;
1130 }
1131
1132 /* Planes initialization */
1133
2/2
✓ Branch 0 taken 99 times.
✓ Branch 1 taken 33 times.
132 for (i = 0; i < 3; i++) {
1134 int w, h;
1135 99 p = &s->plane[i];
1136
2/2
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 33 times.
99 p->width = avctx->width >> (i ? s->chroma_x_shift : 0);
1137
2/2
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 33 times.
99 p->height = avctx->height >> (i ? s->chroma_y_shift : 0);
1138
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 99 times.
99 if (s->interlaced)
1139 p->height >>= 1;
1140 99 p->dwt_width = w = FFALIGN(p->width, (1 << s->wavelet_depth));
1141 99 p->dwt_height = h = FFALIGN(p->height, (1 << s->wavelet_depth));
1142 99 p->coef_stride = FFALIGN(p->dwt_width, 32);
1143 99 p->coef_buf = av_mallocz(p->coef_stride*p->dwt_height*sizeof(dwtcoef));
1144
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 99 times.
99 if (!p->coef_buf)
1145 return AVERROR(ENOMEM);
1146
2/2
✓ Branch 0 taken 396 times.
✓ Branch 1 taken 99 times.
495 for (level = s->wavelet_depth-1; level >= 0; level--) {
1147 396 w = w >> 1;
1148 396 h = h >> 1;
1149
2/2
✓ Branch 0 taken 1584 times.
✓ Branch 1 taken 396 times.
1980 for (o = 0; o < 4; o++) {
1150 1584 b = &p->band[level][o];
1151 1584 b->width = w;
1152 1584 b->height = h;
1153 1584 b->stride = p->coef_stride;
1154 1584 shift = (o > 1)*b->height*b->stride + (o & 1)*b->width;
1155 1584 b->buf = p->coef_buf + shift;
1156 }
1157 }
1158
1159 /* DWT init */
1160
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 99 times.
99 if (ff_vc2enc_init_transforms(&s->transform_args[i].t,
1161 99 s->plane[i].coef_stride,
1162 s->plane[i].dwt_height,
1163 s->slice_width, s->slice_height))
1164 return AVERROR(ENOMEM);
1165 }
1166
1167 /* Slices */
1168 33 s->num_x = s->plane[0].dwt_width/s->slice_width;
1169 33 s->num_y = s->plane[0].dwt_height/s->slice_height;
1170
1171 33 s->slice_args = av_calloc(s->num_x*s->num_y, sizeof(SliceArgs));
1172
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 if (!s->slice_args)
1173 return AVERROR(ENOMEM);
1174
1175
2/2
✓ Branch 0 taken 3828 times.
✓ Branch 1 taken 33 times.
3861 for (i = 0; i < 116; i++) {
1176 3828 const uint64_t qf = ff_dirac_qscale_tab[i];
1177 3828 const uint32_t m = av_log2(qf);
1178 3828 const uint32_t t = (1ULL << (m + 32)) / qf;
1179 3828 const uint32_t r = (t*qf + qf) & UINT32_MAX;
1180
2/2
✓ Branch 0 taken 957 times.
✓ Branch 1 taken 2871 times.
3828 if (!(qf & (qf - 1))) {
1181 957 s->qmagic_lut[i][0] = 0xFFFFFFFF;
1182 957 s->qmagic_lut[i][1] = 0xFFFFFFFF;
1183
2/2
✓ Branch 0 taken 2145 times.
✓ Branch 1 taken 726 times.
2871 } else if (r <= 1 << m) {
1184 2145 s->qmagic_lut[i][0] = t + 1;
1185 2145 s->qmagic_lut[i][1] = 0;
1186 } else {
1187 726 s->qmagic_lut[i][0] = t;
1188 726 s->qmagic_lut[i][1] = t;
1189 }
1190 }
1191
1192 33 return 0;
1193 }
1194
1195 #define VC2ENC_FLAGS (AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
1196 static const AVOption vc2enc_options[] = {
1197 {"tolerance", "Max undershoot in percent", offsetof(VC2EncContext, tolerance), AV_OPT_TYPE_DOUBLE, {.dbl = 5.0f}, 0.0f, 45.0f, VC2ENC_FLAGS, .unit = "tolerance"},
1198 {"slice_width", "Slice width", offsetof(VC2EncContext, slice_width), AV_OPT_TYPE_INT, {.i64 = 32}, 32, 1024, VC2ENC_FLAGS, .unit = "slice_width"},
1199 {"slice_height", "Slice height", offsetof(VC2EncContext, slice_height), AV_OPT_TYPE_INT, {.i64 = 16}, 8, 1024, VC2ENC_FLAGS, .unit = "slice_height"},
1200 {"wavelet_depth", "Transform depth", offsetof(VC2EncContext, wavelet_depth), AV_OPT_TYPE_INT, {.i64 = 4}, 1, 5, VC2ENC_FLAGS, .unit = "wavelet_depth"},
1201 {"wavelet_type", "Transform type", offsetof(VC2EncContext, wavelet_idx), AV_OPT_TYPE_INT, {.i64 = VC2_TRANSFORM_9_7}, 0, VC2_TRANSFORMS_NB, VC2ENC_FLAGS, .unit = "wavelet_idx"},
1202 {"9_7", "Deslauriers-Dubuc (9,7)", 0, AV_OPT_TYPE_CONST, {.i64 = VC2_TRANSFORM_9_7}, INT_MIN, INT_MAX, VC2ENC_FLAGS, .unit = "wavelet_idx"},
1203 {"5_3", "LeGall (5,3)", 0, AV_OPT_TYPE_CONST, {.i64 = VC2_TRANSFORM_5_3}, INT_MIN, INT_MAX, VC2ENC_FLAGS, .unit = "wavelet_idx"},
1204 {"haar", "Haar (with shift)", 0, AV_OPT_TYPE_CONST, {.i64 = VC2_TRANSFORM_HAAR_S}, INT_MIN, INT_MAX, VC2ENC_FLAGS, .unit = "wavelet_idx"},
1205 {"haar_noshift", "Haar (without shift)", 0, AV_OPT_TYPE_CONST, {.i64 = VC2_TRANSFORM_HAAR}, INT_MIN, INT_MAX, VC2ENC_FLAGS, .unit = "wavelet_idx"},
1206 {"qm", "Custom quantization matrix", offsetof(VC2EncContext, quant_matrix), AV_OPT_TYPE_INT, {.i64 = VC2_QM_DEF}, 0, VC2_QM_NB, VC2ENC_FLAGS, .unit = "quant_matrix"},
1207 {"default", "Default from the specifications", 0, AV_OPT_TYPE_CONST, {.i64 = VC2_QM_DEF}, INT_MIN, INT_MAX, VC2ENC_FLAGS, .unit = "quant_matrix"},
1208 {"color", "Prevents low bitrate discoloration", 0, AV_OPT_TYPE_CONST, {.i64 = VC2_QM_COL}, INT_MIN, INT_MAX, VC2ENC_FLAGS, .unit = "quant_matrix"},
1209 {"flat", "Optimize for PSNR", 0, AV_OPT_TYPE_CONST, {.i64 = VC2_QM_FLAT}, INT_MIN, INT_MAX, VC2ENC_FLAGS, .unit = "quant_matrix"},
1210 {NULL}
1211 };
1212
1213 static const AVClass vc2enc_class = {
1214 .class_name = "SMPTE VC-2 encoder",
1215 .category = AV_CLASS_CATEGORY_ENCODER,
1216 .option = vc2enc_options,
1217 .item_name = av_default_item_name,
1218 .version = LIBAVUTIL_VERSION_INT
1219 };
1220
1221 static const FFCodecDefault vc2enc_defaults[] = {
1222 { "b", "600000000" },
1223 { NULL },
1224 };
1225
1226 static const enum AVPixelFormat allowed_pix_fmts[] = {
1227 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P,
1228 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
1229 AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12,
1230 AV_PIX_FMT_NONE
1231 };
1232
1233 const FFCodec ff_vc2_encoder = {
1234 .p.name = "vc2",
1235 CODEC_LONG_NAME("SMPTE VC-2"),
1236 .p.type = AVMEDIA_TYPE_VIDEO,
1237 .p.id = AV_CODEC_ID_DIRAC,
1238 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_SLICE_THREADS |
1239 AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
1240 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
1241 .priv_data_size = sizeof(VC2EncContext),
1242 .init = vc2_encode_init,
1243 .close = vc2_encode_end,
1244 FF_CODEC_ENCODE_CB(vc2_encode_frame),
1245 .p.priv_class = &vc2enc_class,
1246 .defaults = vc2enc_defaults,
1247 .p.pix_fmts = allowed_pix_fmts
1248 };
1249