FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/vc2enc.c
Date: 2025-04-25 22:50:00
Exec Total Coverage
Lines: 522 595 87.7%
Functions: 34 34 100.0%
Branches: 176 264 66.7%

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