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