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