FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/vc2enc.c
Date: 2024-04-19 17:50:32
Exec Total Coverage
Lines: 523 597 87.6%
Functions: 32 32 100.0%
Branches: 185 272 68.0%

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