FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/vc2enc.c
Date: 2021-09-22 05:58:22
Exec Total Coverage
Lines: 524 602 87.0%
Branches: 186 272 68.4%

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