2 * Copyright (C) 2016 Open Broadcast Systems Ltd.
3 * Author 2016 Rostislav Pehlivanov <atomnuker@gmail.com>
5 * This file is part of FFmpeg.
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.
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.
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
22 #include "libavutil/mem.h"
23 #include "libavutil/pixdesc.h"
24 #include "libavutil/opt.h"
25 #include "libavutil/version.h"
26 #include "codec_internal.h"
32 #include "vc2enc_dwt.h"
35 /* The limited size resolution of each slice forces us to do this */
36 #define SSIZE_ROUND(b) (FFALIGN((b), s->size_scaler) + 4 + s->prefix_bytes)
38 /* Decides the cutoff point in # of slices to distribute the leftover bytes */
39 #define SLICE_REDIST_TOTAL 150
41 typedef struct VC2BaseVideoFormat
{
42 enum AVPixelFormat pix_fmt
;
45 uint8_t interlaced
, level
;
49 static const VC2BaseVideoFormat base_video_fmts
[] = {
50 { 0 }, /* Custom format, here just to make indexing equal to base_vf */
51 { AV_PIX_FMT_YUV420P
, { 1001, 15000 }, 176, 120, 0, 1, "QSIF525" },
52 { AV_PIX_FMT_YUV420P
, { 2, 25 }, 176, 144, 0, 1, "QCIF" },
53 { AV_PIX_FMT_YUV420P
, { 1001, 15000 }, 352, 240, 0, 1, "SIF525" },
54 { AV_PIX_FMT_YUV420P
, { 2, 25 }, 352, 288, 0, 1, "CIF" },
55 { AV_PIX_FMT_YUV420P
, { 1001, 15000 }, 704, 480, 0, 1, "4SIF525" },
56 { AV_PIX_FMT_YUV420P
, { 2, 25 }, 704, 576, 0, 1, "4CIF" },
58 { AV_PIX_FMT_YUV422P10
, { 1001, 30000 }, 720, 480, 1, 2, "SD480I-60" },
59 { AV_PIX_FMT_YUV422P10
, { 1, 25 }, 720, 576, 1, 2, "SD576I-50" },
61 { AV_PIX_FMT_YUV422P10
, { 1001, 60000 }, 1280, 720, 0, 3, "HD720P-60" },
62 { AV_PIX_FMT_YUV422P10
, { 1, 50 }, 1280, 720, 0, 3, "HD720P-50" },
63 { AV_PIX_FMT_YUV422P10
, { 1001, 30000 }, 1920, 1080, 1, 3, "HD1080I-60" },
64 { AV_PIX_FMT_YUV422P10
, { 1, 25 }, 1920, 1080, 1, 3, "HD1080I-50" },
65 { AV_PIX_FMT_YUV422P10
, { 1001, 60000 }, 1920, 1080, 0, 3, "HD1080P-60" },
66 { AV_PIX_FMT_YUV422P10
, { 1, 50 }, 1920, 1080, 0, 3, "HD1080P-50" },
68 { AV_PIX_FMT_YUV444P12
, { 1, 24 }, 2048, 1080, 0, 4, "DC2K" },
69 { AV_PIX_FMT_YUV444P12
, { 1, 24 }, 4096, 2160, 0, 5, "DC4K" },
71 { AV_PIX_FMT_YUV422P10
, { 1001, 60000 }, 3840, 2160, 0, 6, "UHDTV 4K-60" },
72 { AV_PIX_FMT_YUV422P10
, { 1, 50 }, 3840, 2160, 0, 6, "UHDTV 4K-50" },
74 { AV_PIX_FMT_YUV422P10
, { 1001, 60000 }, 7680, 4320, 0, 7, "UHDTV 8K-60" },
75 { AV_PIX_FMT_YUV422P10
, { 1, 50 }, 7680, 4320, 0, 7, "UHDTV 8K-50" },
77 { AV_PIX_FMT_YUV422P10
, { 1001, 24000 }, 1920, 1080, 0, 3, "HD1080P-24" },
78 { AV_PIX_FMT_YUV422P10
, { 1001, 30000 }, 720, 486, 1, 2, "SD Pro486" },
80 static const int base_video_fmts_len
= FF_ARRAY_ELEMS(base_video_fmts
);
90 typedef struct SubBand
{
97 typedef struct Plane
{
98 SubBand band
[MAX_DWT_LEVELS
][4];
104 ptrdiff_t coef_stride
;
107 typedef struct SliceArgs
{
108 const struct VC2EncContext
*ctx
;
110 int cache
[DIRAC_MAX_QUANT_INDEX
];
121 typedef struct TransformArgs
{
122 const struct VC2EncContext
*ctx
;
127 VC2TransformContext t
;
130 typedef struct VC2EncContext
{
134 AVCodecContext
*avctx
;
135 DiracVersionInfo ver
;
137 SliceArgs
*slice_args
;
138 TransformArgs transform_args
[3];
140 /* For conversion from unsigned pixel values to signed */
146 uint32_t picture_number
;
148 /* Base video format */
153 /* Quantization matrix */
154 uint8_t quant
[MAX_DWT_LEVELS
][4];
155 int custom_quant_matrix
;
158 uint32_t qmagic_lut
[116][2];
160 int num_x
; /* #slices horizontally */
161 int num_y
; /* #slices vertically */
167 /* Rate control stuff */
178 int strict_compliance
;
182 enum VC2_QM quant_matrix
;
184 /* Parse code state */
185 uint32_t next_parse_offset
;
186 enum DiracParseCodes last_parse_code
;
189 static av_always_inline
void put_vc2_ue_uint(PutBitContext
*pb
, uint32_t val
)
193 unsigned topbit
= 1, maxval
= 1;
201 while (val
> maxval
) {
207 bits
= ff_log2(topbit
);
209 for (i
= 0; i
< bits
; i
++) {
211 av_assert2(pbits
<= UINT64_MAX
>>3);
217 put_bits64(pb
, bits
*2 + 1, (pbits
<< 1) | 1);
220 static av_always_inline
int count_vc2_ue_uint(uint32_t val
)
222 int topbit
= 1, maxval
= 1;
227 while (val
> maxval
) {
233 return ff_log2(topbit
)*2 + 1;
236 /* VC-2 10.4 - parse_info() */
237 static void encode_parse_info(VC2EncContext
*s
, enum DiracParseCodes pcode
)
239 uint32_t cur_pos
, dist
;
241 align_put_bits(&s
->pb
);
243 cur_pos
= put_bytes_count(&s
->pb
, 0);
246 ff_put_string(&s
->pb
, "BBCD", 0);
249 put_bits(&s
->pb
, 8, pcode
);
251 /* Next parse offset */
252 dist
= cur_pos
- s
->next_parse_offset
;
253 AV_WB32(s
->pb
.buf
+ s
->next_parse_offset
+ 5, dist
);
254 s
->next_parse_offset
= cur_pos
;
255 put_bits32(&s
->pb
, pcode
== DIRAC_PCODE_END_SEQ
? 13 : 0);
257 /* Last parse offset */
258 put_bits32(&s
->pb
, s
->last_parse_code
== DIRAC_PCODE_END_SEQ
? 13 : dist
);
260 s
->last_parse_code
= pcode
;
263 /* VC-2 11.1 - parse_parameters()
264 * The level dictates what the decoder should expect in terms of resolution
265 * and allows it to quickly reject whatever it can't support. Remember,
266 * this codec kinda targets cheapo FPGAs without much memory. Unfortunately
267 * it also limits us greatly in our choice of formats, hence the flag to disable
268 * strict_compliance */
269 static void encode_parse_params(VC2EncContext
*s
)
271 put_vc2_ue_uint(&s
->pb
, s
->ver
.major
); /* VC-2 demands this to be 2 */
272 put_vc2_ue_uint(&s
->pb
, s
->ver
.minor
); /* ^^ and this to be 0 */
273 put_vc2_ue_uint(&s
->pb
, s
->profile
); /* 3 to signal HQ profile */
274 put_vc2_ue_uint(&s
->pb
, s
->level
); /* 3 - 1080/720, 6 - 4K */
277 /* VC-2 11.3 - frame_size() */
278 static void encode_frame_size(VC2EncContext
*s
)
280 put_bits(&s
->pb
, 1, !s
->strict_compliance
);
281 if (!s
->strict_compliance
) {
282 AVCodecContext
*avctx
= s
->avctx
;
283 put_vc2_ue_uint(&s
->pb
, avctx
->width
);
284 put_vc2_ue_uint(&s
->pb
, avctx
->height
);
288 /* VC-2 11.3.3 - color_diff_sampling_format() */
289 static void encode_sample_fmt(VC2EncContext
*s
)
291 put_bits(&s
->pb
, 1, !s
->strict_compliance
);
292 if (!s
->strict_compliance
) {
294 if (s
->chroma_x_shift
== 1 && s
->chroma_y_shift
== 0)
296 else if (s
->chroma_x_shift
== 1 && s
->chroma_y_shift
== 1)
300 put_vc2_ue_uint(&s
->pb
, idx
);
304 /* VC-2 11.3.4 - scan_format() */
305 static void encode_scan_format(VC2EncContext
*s
)
307 put_bits(&s
->pb
, 1, !s
->strict_compliance
);
308 if (!s
->strict_compliance
)
309 put_vc2_ue_uint(&s
->pb
, s
->interlaced
);
312 /* VC-2 11.3.5 - frame_rate() */
313 static void encode_frame_rate(VC2EncContext
*s
)
315 put_bits(&s
->pb
, 1, !s
->strict_compliance
);
316 if (!s
->strict_compliance
) {
317 AVCodecContext
*avctx
= s
->avctx
;
318 put_vc2_ue_uint(&s
->pb
, 0);
319 put_vc2_ue_uint(&s
->pb
, avctx
->time_base
.den
);
320 put_vc2_ue_uint(&s
->pb
, avctx
->time_base
.num
);
324 /* VC-2 11.3.6 - aspect_ratio() */
325 static void encode_aspect_ratio(VC2EncContext
*s
)
327 put_bits(&s
->pb
, 1, !s
->strict_compliance
);
328 if (!s
->strict_compliance
) {
329 AVCodecContext
*avctx
= s
->avctx
;
330 put_vc2_ue_uint(&s
->pb
, 0);
331 put_vc2_ue_uint(&s
->pb
, avctx
->sample_aspect_ratio
.num
);
332 put_vc2_ue_uint(&s
->pb
, avctx
->sample_aspect_ratio
.den
);
336 /* VC-2 11.3.7 - clean_area() */
337 static void encode_clean_area(VC2EncContext
*s
)
339 put_bits(&s
->pb
, 1, 0);
342 /* VC-2 11.3.8 - signal_range() */
343 static void encode_signal_range(VC2EncContext
*s
)
345 put_bits(&s
->pb
, 1, !s
->strict_compliance
);
346 if (!s
->strict_compliance
)
347 put_vc2_ue_uint(&s
->pb
, s
->bpp_idx
);
350 /* VC-2 11.3.9 - color_spec() */
351 static void encode_color_spec(VC2EncContext
*s
)
353 AVCodecContext
*avctx
= s
->avctx
;
354 put_bits(&s
->pb
, 1, !s
->strict_compliance
);
355 if (!s
->strict_compliance
) {
357 put_vc2_ue_uint(&s
->pb
, 0);
360 put_bits(&s
->pb
, 1, 1);
361 if (avctx
->color_primaries
== AVCOL_PRI_BT470BG
)
363 else if (avctx
->color_primaries
== AVCOL_PRI_SMPTE170M
)
365 else if (avctx
->color_primaries
== AVCOL_PRI_SMPTE240M
)
369 put_vc2_ue_uint(&s
->pb
, val
);
372 put_bits(&s
->pb
, 1, 1);
373 if (avctx
->colorspace
== AVCOL_SPC_RGB
)
375 else if (avctx
->colorspace
== AVCOL_SPC_YCOCG
)
377 else if (avctx
->colorspace
== AVCOL_SPC_BT470BG
)
381 put_vc2_ue_uint(&s
->pb
, val
);
383 /* transfer function */
384 put_bits(&s
->pb
, 1, 1);
385 if (avctx
->color_trc
== AVCOL_TRC_LINEAR
)
387 else if (avctx
->color_trc
== AVCOL_TRC_BT1361_ECG
)
391 put_vc2_ue_uint(&s
->pb
, val
);
395 /* VC-2 11.3 - source_parameters() */
396 static void encode_source_params(VC2EncContext
*s
)
398 encode_frame_size(s
);
399 encode_sample_fmt(s
);
400 encode_scan_format(s
);
401 encode_frame_rate(s
);
402 encode_aspect_ratio(s
);
403 encode_clean_area(s
);
404 encode_signal_range(s
);
405 encode_color_spec(s
);
408 /* VC-2 11 - sequence_header() */
409 static void encode_seq_header(VC2EncContext
*s
)
411 align_put_bits(&s
->pb
);
412 encode_parse_params(s
);
413 put_vc2_ue_uint(&s
->pb
, s
->base_vf
);
414 encode_source_params(s
);
415 put_vc2_ue_uint(&s
->pb
, s
->interlaced
); /* Frames or fields coding */
418 /* VC-2 12.1 - picture_header() */
419 static void encode_picture_header(VC2EncContext
*s
)
421 align_put_bits(&s
->pb
);
422 put_bits32(&s
->pb
, s
->picture_number
++);
425 /* VC-2 12.3.4.1 - slice_parameters() */
426 static void encode_slice_params(VC2EncContext
*s
)
428 put_vc2_ue_uint(&s
->pb
, s
->num_x
);
429 put_vc2_ue_uint(&s
->pb
, s
->num_y
);
430 put_vc2_ue_uint(&s
->pb
, s
->prefix_bytes
);
431 put_vc2_ue_uint(&s
->pb
, s
->size_scaler
);
434 /* 1st idx = LL, second - vertical, third - horizontal, fourth - total */
435 static const uint8_t vc2_qm_col_tab
[][4] = {
443 static const uint8_t vc2_qm_flat_tab
[][4] = {
451 static void init_quant_matrix(VC2EncContext
*s
)
453 int level
, orientation
;
455 if (s
->wavelet_depth
<= 4 && s
->quant_matrix
== VC2_QM_DEF
) {
456 s
->custom_quant_matrix
= 0;
457 for (level
= 0; level
< s
->wavelet_depth
; level
++) {
458 s
->quant
[level
][0] = ff_dirac_default_qmat
[s
->wavelet_idx
][level
][0];
459 s
->quant
[level
][1] = ff_dirac_default_qmat
[s
->wavelet_idx
][level
][1];
460 s
->quant
[level
][2] = ff_dirac_default_qmat
[s
->wavelet_idx
][level
][2];
461 s
->quant
[level
][3] = ff_dirac_default_qmat
[s
->wavelet_idx
][level
][3];
466 s
->custom_quant_matrix
= 1;
468 if (s
->quant_matrix
== VC2_QM_DEF
) {
469 for (level
= 0; level
< s
->wavelet_depth
; level
++) {
470 for (orientation
= 0; orientation
< 4; orientation
++) {
472 s
->quant
[level
][orientation
] = ff_dirac_default_qmat
[s
->wavelet_idx
][level
][orientation
];
474 s
->quant
[level
][orientation
] = vc2_qm_col_tab
[level
][orientation
];
477 } else if (s
->quant_matrix
== VC2_QM_COL
) {
478 for (level
= 0; level
< s
->wavelet_depth
; level
++) {
479 for (orientation
= 0; orientation
< 4; orientation
++) {
480 s
->quant
[level
][orientation
] = vc2_qm_col_tab
[level
][orientation
];
484 for (level
= 0; level
< s
->wavelet_depth
; level
++) {
485 for (orientation
= 0; orientation
< 4; orientation
++) {
486 s
->quant
[level
][orientation
] = vc2_qm_flat_tab
[level
][orientation
];
492 /* VC-2 12.3.4.2 - quant_matrix() */
493 static void encode_quant_matrix(VC2EncContext
*s
)
496 put_bits(&s
->pb
, 1, s
->custom_quant_matrix
);
497 if (s
->custom_quant_matrix
) {
498 put_vc2_ue_uint(&s
->pb
, s
->quant
[0][0]);
499 for (level
= 0; level
< s
->wavelet_depth
; level
++) {
500 put_vc2_ue_uint(&s
->pb
, s
->quant
[level
][1]);
501 put_vc2_ue_uint(&s
->pb
, s
->quant
[level
][2]);
502 put_vc2_ue_uint(&s
->pb
, s
->quant
[level
][3]);
507 /* VC-2 12.3 - transform_parameters() */
508 static void encode_transform_params(VC2EncContext
*s
)
510 put_vc2_ue_uint(&s
->pb
, s
->wavelet_idx
);
511 put_vc2_ue_uint(&s
->pb
, s
->wavelet_depth
);
513 encode_slice_params(s
);
514 encode_quant_matrix(s
);
517 /* VC-2 12.2 - wavelet_transform() */
518 static void encode_wavelet_transform(VC2EncContext
*s
)
520 encode_transform_params(s
);
521 align_put_bits(&s
->pb
);
524 /* VC-2 12 - picture_parse() */
525 static void encode_picture_start(VC2EncContext
*s
)
527 align_put_bits(&s
->pb
);
528 encode_picture_header(s
);
529 align_put_bits(&s
->pb
);
530 encode_wavelet_transform(s
);
533 #define QUANT(c, mul, add, shift) (((mul) * (c) + (add)) >> (shift))
535 /* VC-2 13.5.5.2 - slice_band() */
536 static void encode_subband(const VC2EncContext
*s
, PutBitContext
*pb
,
537 int sx
, int sy
, const SubBand
*b
, int quant
)
541 const int left
= b
->width
* (sx
+0) / s
->num_x
;
542 const int right
= b
->width
* (sx
+1) / s
->num_x
;
543 const int top
= b
->height
* (sy
+0) / s
->num_y
;
544 const int bottom
= b
->height
* (sy
+1) / s
->num_y
;
546 dwtcoef
*coeff
= b
->buf
+ top
* b
->stride
;
547 const uint64_t q_m
= ((uint64_t)(s
->qmagic_lut
[quant
][0])) << 2;
548 const uint64_t q_a
= s
->qmagic_lut
[quant
][1];
549 const int q_s
= av_log2(ff_dirac_qscale_tab
[quant
]) + 32;
551 for (y
= top
; y
< bottom
; y
++) {
552 for (x
= left
; x
< right
; x
++) {
553 uint32_t c_abs
= QUANT(FFABS(coeff
[x
]), q_m
, q_a
, q_s
);
554 put_vc2_ue_uint(pb
, c_abs
);
556 put_bits(pb
, 1, coeff
[x
] < 0);
562 static int count_hq_slice(SliceArgs
*slice
, int quant_idx
)
565 uint8_t quants
[MAX_DWT_LEVELS
][4];
566 int bits
= 0, p
, level
, orientation
;
567 const VC2EncContext
*s
= slice
->ctx
;
569 if (slice
->cache
[quant_idx
])
570 return slice
->cache
[quant_idx
];
572 bits
+= 8*s
->prefix_bytes
;
573 bits
+= 8; /* quant_idx */
575 for (level
= 0; level
< s
->wavelet_depth
; level
++)
576 for (orientation
= !!level
; orientation
< 4; orientation
++)
577 quants
[level
][orientation
] = FFMAX(quant_idx
- s
->quant
[level
][orientation
], 0);
579 for (p
= 0; p
< 3; p
++) {
580 int bytes_start
, bytes_len
, pad_s
, pad_c
;
581 bytes_start
= bits
>> 3;
583 for (level
= 0; level
< s
->wavelet_depth
; level
++) {
584 for (orientation
= !!level
; orientation
< 4; orientation
++) {
585 const SubBand
*b
= &s
->plane
[p
].band
[level
][orientation
];
587 const int q_idx
= quants
[level
][orientation
];
588 const uint64_t q_m
= ((uint64_t)s
->qmagic_lut
[q_idx
][0]) << 2;
589 const uint64_t q_a
= s
->qmagic_lut
[q_idx
][1];
590 const int q_s
= av_log2(ff_dirac_qscale_tab
[q_idx
]) + 32;
592 const int left
= b
->width
* slice
->x
/ s
->num_x
;
593 const int right
= b
->width
*(slice
->x
+1) / s
->num_x
;
594 const int top
= b
->height
* slice
->y
/ s
->num_y
;
595 const int bottom
= b
->height
*(slice
->y
+1) / s
->num_y
;
597 dwtcoef
*buf
= b
->buf
+ top
* b
->stride
;
599 for (y
= top
; y
< bottom
; y
++) {
600 for (x
= left
; x
< right
; x
++) {
601 uint32_t c_abs
= QUANT(FFABS(buf
[x
]), q_m
, q_a
, q_s
);
602 bits
+= count_vc2_ue_uint(c_abs
);
609 bits
+= FFALIGN(bits
, 8) - bits
;
610 bytes_len
= (bits
>> 3) - bytes_start
- 1;
611 pad_s
= FFALIGN(bytes_len
, s
->size_scaler
)/s
->size_scaler
;
612 pad_c
= (pad_s
*s
->size_scaler
) - bytes_len
;
616 slice
->cache
[quant_idx
] = bits
;
621 /* Approaches the best possible quantizer asymptotically, its kinda exaustive
622 * but we have a LUT to get the coefficient size in bits. Guaranteed to never
623 * overshoot, which is apparently very important when streaming */
624 static int rate_control(AVCodecContext
*avctx
, void *arg
)
626 SliceArgs
*slice_dat
= arg
;
627 const VC2EncContext
*s
= slice_dat
->ctx
;
628 const int top
= slice_dat
->bits_ceil
;
629 const int bottom
= slice_dat
->bits_floor
;
630 int quant_buf
[2] = {-1, -1};
631 int quant
= slice_dat
->quant_idx
, step
= 1;
632 int bits_last
, bits
= count_hq_slice(slice_dat
, quant
);
633 while ((bits
> top
) || (bits
< bottom
)) {
634 const int signed_step
= bits
> top
? +step
: -step
;
635 quant
= av_clip(quant
+ signed_step
, 0, s
->q_ceil
-1);
636 bits
= count_hq_slice(slice_dat
, quant
);
637 if (quant_buf
[1] == quant
) {
638 quant
= FFMAX(quant_buf
[0], quant
);
639 bits
= quant
== quant_buf
[0] ? bits_last
: bits
;
642 step
= av_clip(step
/2, 1, (s
->q_ceil
-1)/2);
643 quant_buf
[1] = quant_buf
[0];
644 quant_buf
[0] = quant
;
647 slice_dat
->quant_idx
= av_clip(quant
, 0, s
->q_ceil
-1);
648 slice_dat
->bytes
= SSIZE_ROUND(bits
>> 3);
652 static int calc_slice_sizes(VC2EncContext
*s
)
654 int i
, j
, slice_x
, slice_y
, bytes_left
= 0;
655 int bytes_top
[SLICE_REDIST_TOTAL
] = {0};
656 int64_t total_bytes_needed
= 0;
657 int slice_redist_range
= FFMIN(SLICE_REDIST_TOTAL
, s
->num_x
*s
->num_y
);
658 SliceArgs
*enc_args
= s
->slice_args
;
659 SliceArgs
*top_loc
[SLICE_REDIST_TOTAL
] = {NULL
};
661 init_quant_matrix(s
);
663 for (slice_y
= 0; slice_y
< s
->num_y
; slice_y
++) {
664 for (slice_x
= 0; slice_x
< s
->num_x
; slice_x
++) {
665 SliceArgs
*args
= &enc_args
[s
->num_x
*slice_y
+ slice_x
];
669 args
->bits_ceil
= s
->slice_max_bytes
<< 3;
670 args
->bits_floor
= s
->slice_min_bytes
<< 3;
671 memset(args
->cache
, 0, s
->q_ceil
*sizeof(*args
->cache
));
675 /* First pass - determine baseline slice sizes w.r.t. max_slice_size */
676 s
->avctx
->execute(s
->avctx
, rate_control
, enc_args
, NULL
, s
->num_x
*s
->num_y
,
679 for (i
= 0; i
< s
->num_x
*s
->num_y
; i
++) {
680 SliceArgs
*args
= &enc_args
[i
];
681 bytes_left
+= args
->bytes
;
682 for (j
= 0; j
< slice_redist_range
; j
++) {
683 if (args
->bytes
> bytes_top
[j
]) {
684 bytes_top
[j
] = args
->bytes
;
691 bytes_left
= s
->frame_max_bytes
- bytes_left
;
693 /* Second pass - distribute leftover bytes */
694 while (bytes_left
> 0) {
696 for (i
= 0; i
< slice_redist_range
; i
++) {
698 int bits
, bytes
, diff
, prev_bytes
, new_idx
;
701 if (!top_loc
[i
] || !top_loc
[i
]->quant_idx
)
704 prev_bytes
= args
->bytes
;
705 new_idx
= FFMAX(args
->quant_idx
- 1, 0);
706 bits
= count_hq_slice(args
, new_idx
);
707 bytes
= SSIZE_ROUND(bits
>> 3);
708 diff
= bytes
- prev_bytes
;
709 if ((bytes_left
- diff
) > 0) {
710 args
->quant_idx
= new_idx
;
720 for (i
= 0; i
< s
->num_x
*s
->num_y
; i
++) {
721 SliceArgs
*args
= &enc_args
[i
];
722 total_bytes_needed
+= args
->bytes
;
723 s
->q_avg
= (s
->q_avg
+ args
->quant_idx
)/2;
726 return total_bytes_needed
;
729 /* VC-2 13.5.3 - hq_slice */
730 static int encode_hq_slice(AVCodecContext
*avctx
, void *arg
)
732 const SliceArgs
*slice_dat
= arg
;
733 const VC2EncContext
*s
= slice_dat
->ctx
;
734 PutBitContext pb0
, *const pb
= &pb0
;
735 const int slice_x
= slice_dat
->x
;
736 const int slice_y
= slice_dat
->y
;
737 const int quant_idx
= slice_dat
->quant_idx
;
738 const int slice_bytes_max
= slice_dat
->bytes
;
739 uint8_t quants
[MAX_DWT_LEVELS
][4];
740 int p
, level
, orientation
;
742 /* The reference decoder ignores it, and its typical length is 0 */
743 memset(slice_dat
->buf
, 0, s
->prefix_bytes
);
745 init_put_bits(pb
, slice_dat
->buf
+ s
->prefix_bytes
, slice_dat
->bytes
- s
->prefix_bytes
);
747 put_bits(pb
, 8, quant_idx
);
749 /* Slice quantization (slice_quantizers() in the specs) */
750 for (level
= 0; level
< s
->wavelet_depth
; level
++)
751 for (orientation
= !!level
; orientation
< 4; orientation
++)
752 quants
[level
][orientation
] = FFMAX(quant_idx
- s
->quant
[level
][orientation
], 0);
754 /* Luma + 2 Chroma planes */
755 for (p
= 0; p
< 3; p
++) {
756 int bytes_start
, bytes_len
, pad_s
, pad_c
;
757 bytes_start
= put_bytes_count(pb
, 0);
759 for (level
= 0; level
< s
->wavelet_depth
; level
++) {
760 for (orientation
= !!level
; orientation
< 4; orientation
++) {
761 encode_subband(s
, pb
, slice_x
, slice_y
,
762 &s
->plane
[p
].band
[level
][orientation
],
763 quants
[level
][orientation
]);
767 bytes_len
= put_bytes_output(pb
) - bytes_start
- 1;
769 int len_diff
= slice_bytes_max
- put_bytes_output(pb
);
770 pad_s
= FFALIGN((bytes_len
+ len_diff
), s
->size_scaler
)/s
->size_scaler
;
771 pad_c
= (pad_s
*s
->size_scaler
) - bytes_len
;
773 pad_s
= FFALIGN(bytes_len
, s
->size_scaler
)/s
->size_scaler
;
774 pad_c
= (pad_s
*s
->size_scaler
) - bytes_len
;
776 pb
->buf
[bytes_start
] = pad_s
;
777 /* vc2-reference uses that padding that decodes to '0' coeffs */
778 memset(put_bits_ptr(pb
), 0xFF, pad_c
);
779 skip_put_bytes(pb
, pad_c
);
785 /* VC-2 13.5.1 - low_delay_transform_data() */
786 static int encode_slices(VC2EncContext
*s
)
789 int slice_x
, slice_y
, skip
= 0;
790 SliceArgs
*enc_args
= s
->slice_args
;
792 flush_put_bits(&s
->pb
);
793 buf
= put_bits_ptr(&s
->pb
);
795 for (slice_y
= 0; slice_y
< s
->num_y
; slice_y
++) {
796 for (slice_x
= 0; slice_x
< s
->num_x
; slice_x
++) {
797 SliceArgs
*args
= &enc_args
[s
->num_x
*slice_y
+ slice_x
];
798 args
->buf
= buf
+ skip
;
803 s
->avctx
->execute(s
->avctx
, encode_hq_slice
, enc_args
, NULL
, s
->num_x
*s
->num_y
,
806 skip_put_bytes(&s
->pb
, skip
);
812 * Transform basics for a 3 level transform
813 * |---------------------------------------------------------------------|
814 * | LL-0 | HL-0 | | |
815 * |--------|-------| HL-1 | |
816 * | LH-0 | HH-0 | | |
817 * |----------------|-----------------| HL-2 |
821 * |----------------------------------|----------------------------------|
829 * |---------------------------------------------------------------------|
831 * DWT transforms are generally applied by splitting the image in two vertically
832 * and applying a low pass transform on the left part and a corresponding high
833 * pass transform on the right hand side. This is known as the horizontal filter
835 * After that, the same operation is performed except the image is divided
836 * horizontally, with the high pass on the lower and the low pass on the higher
838 * Therefore, you're left with 4 subdivisions - known as low-low, low-high,
839 * high-low and high-high. They're referred to as orientations in the decoder
842 * The LL (low-low) area contains the original image downsampled by the amount
843 * of levels. The rest of the areas can be thought as the details needed
844 * to restore the image perfectly to its original size.
846 static int dwt_plane(AVCodecContext
*avctx
, void *arg
)
848 TransformArgs
*transform_dat
= arg
;
849 const VC2EncContext
*s
= transform_dat
->ctx
;
850 const void *frame_data
= transform_dat
->idata
;
851 const ptrdiff_t linesize
= transform_dat
->istride
;
852 const int field
= transform_dat
->field
;
853 const Plane
*p
= transform_dat
->plane
;
854 VC2TransformContext
*t
= &transform_dat
->t
;
855 dwtcoef
*buf
= p
->coef_buf
;
856 const int idx
= s
->wavelet_idx
;
857 const int skip
= 1 + s
->interlaced
;
859 int x
, y
, level
, offset
;
860 ptrdiff_t pix_stride
= linesize
>> (s
->bpp
- 1);
865 } else if (field
== 2) {
873 const uint8_t *pix
= (const uint8_t *)frame_data
+ offset
;
874 for (y
= 0; y
< p
->height
*skip
; y
+=skip
) {
875 for (x
= 0; x
< p
->width
; x
++) {
876 buf
[x
] = pix
[x
] - s
->diff_offset
;
878 memset(&buf
[x
], 0, (p
->coef_stride
- p
->width
)*sizeof(dwtcoef
));
879 buf
+= p
->coef_stride
;
883 const uint16_t *pix
= (const uint16_t *)frame_data
+ offset
;
884 for (y
= 0; y
< p
->height
*skip
; y
+=skip
) {
885 for (x
= 0; x
< p
->width
; x
++) {
886 buf
[x
] = pix
[x
] - s
->diff_offset
;
888 memset(&buf
[x
], 0, (p
->coef_stride
- p
->width
)*sizeof(dwtcoef
));
889 buf
+= p
->coef_stride
;
894 memset(buf
, 0, p
->coef_stride
* (p
->dwt_height
- p
->height
) * sizeof(dwtcoef
));
896 for (level
= s
->wavelet_depth
-1; level
>= 0; level
--) {
897 const SubBand
*b
= &p
->band
[level
][0];
898 t
->vc2_subband_dwt
[idx
](t
, p
->coef_buf
, p
->coef_stride
,
899 b
->width
, b
->height
);
905 static int encode_frame(VC2EncContext
*s
, AVPacket
*avpkt
, const AVFrame
*frame
,
906 const char *aux_data
, const int header_size
, int field
)
909 int64_t max_frame_bytes
;
911 /* Threaded DWT transform */
912 for (i
= 0; i
< 3; i
++) {
913 s
->transform_args
[i
].ctx
= s
;
914 s
->transform_args
[i
].field
= field
;
915 s
->transform_args
[i
].plane
= &s
->plane
[i
];
916 s
->transform_args
[i
].idata
= frame
->data
[i
];
917 s
->transform_args
[i
].istride
= frame
->linesize
[i
];
919 s
->avctx
->execute(s
->avctx
, dwt_plane
, s
->transform_args
, NULL
, 3,
920 sizeof(TransformArgs
));
922 /* Calculate per-slice quantizers and sizes */
923 max_frame_bytes
= header_size
+ calc_slice_sizes(s
);
926 ret
= ff_get_encode_buffer(s
->avctx
, avpkt
,
927 max_frame_bytes
<< s
->interlaced
, 0);
930 init_put_bits(&s
->pb
, avpkt
->data
, avpkt
->size
);
933 /* Sequence header */
934 encode_parse_info(s
, DIRAC_PCODE_SEQ_HEADER
);
935 encode_seq_header(s
);
937 /* Encoder version */
939 encode_parse_info(s
, DIRAC_PCODE_AUX
);
940 ff_put_string(&s
->pb
, aux_data
, 1);
944 encode_parse_info(s
, DIRAC_PCODE_PICTURE_HQ
);
945 encode_picture_start(s
);
951 encode_parse_info(s
, DIRAC_PCODE_END_SEQ
);
956 static av_cold
int vc2_encode_frame(AVCodecContext
*avctx
, AVPacket
*avpkt
,
957 const AVFrame
*frame
, int *got_packet
)
960 int slice_ceil
, sig_size
= 256;
961 VC2EncContext
*s
= avctx
->priv_data
;
962 const int bitexact
= avctx
->flags
& AV_CODEC_FLAG_BITEXACT
;
963 const char *aux_data
= bitexact
? "Lavc" : LIBAVCODEC_IDENT
;
964 const int aux_data_size
= bitexact
? sizeof("Lavc") : sizeof(LIBAVCODEC_IDENT
);
965 const int header_size
= 100 + aux_data_size
;
966 int64_t r_bitrate
= avctx
->bit_rate
>> (s
->interlaced
);
971 s
->last_parse_code
= 0;
972 s
->next_parse_offset
= 0;
975 s
->frame_max_bytes
= (av_rescale(r_bitrate
, s
->avctx
->time_base
.num
,
976 s
->avctx
->time_base
.den
) >> 3) - header_size
;
977 s
->slice_max_bytes
= slice_ceil
= av_rescale(s
->frame_max_bytes
, 1, s
->num_x
*s
->num_y
);
979 /* Find an appropriate size scaler */
980 while (sig_size
> 255) {
981 int r_size
= SSIZE_ROUND(s
->slice_max_bytes
);
982 if (r_size
> slice_ceil
) {
983 s
->slice_max_bytes
-= r_size
- slice_ceil
;
984 r_size
= SSIZE_ROUND(s
->slice_max_bytes
);
986 sig_size
= r_size
/s
->size_scaler
; /* Signalled slize size */
987 s
->size_scaler
<<= 1;
990 s
->slice_min_bytes
= s
->slice_max_bytes
- s
->slice_max_bytes
*(s
->tolerance
/100.0f
);
991 if (s
->slice_min_bytes
< 0 || s
->slice_max_bytes
> INT_MAX
>> 3)
992 return AVERROR(EINVAL
);
994 ret
= encode_frame(s
, avpkt
, frame
, aux_data
, header_size
, s
->interlaced
);
998 ret
= encode_frame(s
, avpkt
, frame
, aux_data
, header_size
, 2);
1003 flush_put_bits(&s
->pb
);
1004 av_shrink_packet(avpkt
, put_bytes_output(&s
->pb
));
1011 static av_cold
int vc2_encode_end(AVCodecContext
*avctx
)
1014 VC2EncContext
*s
= avctx
->priv_data
;
1016 av_log(avctx
, AV_LOG_INFO
, "Qavg: %i\n", s
->q_avg
);
1018 for (i
= 0; i
< 3; i
++) {
1019 ff_vc2enc_free_transforms(&s
->transform_args
[i
].t
);
1020 av_freep(&s
->plane
[i
].coef_buf
);
1023 av_freep(&s
->slice_args
);
1028 static av_cold
int vc2_encode_init(AVCodecContext
*avctx
)
1032 int i
, level
, o
, shift
;
1033 const AVPixFmtDescriptor
*pixdesc
;
1035 VC2EncContext
*s
= avctx
->priv_data
;
1037 s
->picture_number
= 0;
1039 /* Total allowed quantization range */
1040 s
->q_ceil
= DIRAC_MAX_QUANT_INDEX
;
1048 s
->strict_compliance
= 1;
1051 s
->slice_max_bytes
= 0;
1052 s
->slice_min_bytes
= 0;
1054 /* Mark unknown as progressive */
1055 s
->interlaced
= !((avctx
->field_order
== AV_FIELD_UNKNOWN
) ||
1056 (avctx
->field_order
== AV_FIELD_PROGRESSIVE
));
1058 for (i
= 0; i
< base_video_fmts_len
; i
++) {
1059 const VC2BaseVideoFormat
*fmt
= &base_video_fmts
[i
];
1060 if (avctx
->pix_fmt
!= fmt
->pix_fmt
)
1062 if (avctx
->time_base
.num
!= fmt
->time_base
.num
)
1064 if (avctx
->time_base
.den
!= fmt
->time_base
.den
)
1066 if (avctx
->width
!= fmt
->width
)
1068 if (avctx
->height
!= fmt
->height
)
1070 if (s
->interlaced
!= fmt
->interlaced
)
1073 s
->level
= base_video_fmts
[i
].level
;
1078 av_log(avctx
, AV_LOG_WARNING
, "Interlacing enabled!\n");
1080 if ((s
->slice_width
& (s
->slice_width
- 1)) ||
1081 (s
->slice_height
& (s
->slice_height
- 1))) {
1082 av_log(avctx
, AV_LOG_ERROR
, "Slice size is not a power of two!\n");
1083 return AVERROR_UNKNOWN
;
1086 if ((s
->slice_width
> avctx
->width
) ||
1087 (s
->slice_height
> avctx
->height
)) {
1088 av_log(avctx
, AV_LOG_ERROR
, "Slice size is bigger than the image!\n");
1089 return AVERROR_UNKNOWN
;
1092 if (s
->base_vf
<= 0) {
1093 if (avctx
->strict_std_compliance
< FF_COMPLIANCE_STRICT
) {
1094 s
->strict_compliance
= s
->base_vf
= 0;
1095 av_log(avctx
, AV_LOG_WARNING
, "Format does not strictly comply with VC2 specs\n");
1097 av_log(avctx
, AV_LOG_WARNING
, "Given format does not strictly comply with "
1098 "the specifications, decrease strictness to use it.\n");
1099 return AVERROR_UNKNOWN
;
1102 av_log(avctx
, AV_LOG_INFO
, "Selected base video format = %i (%s)\n",
1103 s
->base_vf
, base_video_fmts
[s
->base_vf
].name
);
1106 pixdesc
= av_pix_fmt_desc_get(avctx
->pix_fmt
);
1107 /* Chroma subsampling */
1108 s
->chroma_x_shift
= pixdesc
->log2_chroma_w
;
1109 s
->chroma_y_shift
= pixdesc
->log2_chroma_h
;
1111 /* Bit depth and color range index */
1112 depth
= pixdesc
->comp
[0].depth
;
1113 if (depth
== 8 && avctx
->color_range
== AVCOL_RANGE_JPEG
) {
1116 s
->diff_offset
= 128;
1117 } else if (depth
== 8 && (avctx
->color_range
== AVCOL_RANGE_MPEG
||
1118 avctx
->color_range
== AVCOL_RANGE_UNSPECIFIED
)) {
1121 s
->diff_offset
= 128;
1122 } else if (depth
== 10) {
1125 s
->diff_offset
= 512;
1129 s
->diff_offset
= 2048;
1132 /* Planes initialization */
1133 for (i
= 0; i
< 3; i
++) {
1136 p
->width
= avctx
->width
>> (i
? s
->chroma_x_shift
: 0);
1137 p
->height
= avctx
->height
>> (i
? s
->chroma_y_shift
: 0);
1140 p
->dwt_width
= w
= FFALIGN(p
->width
, (1 << s
->wavelet_depth
));
1141 p
->dwt_height
= h
= FFALIGN(p
->height
, (1 << s
->wavelet_depth
));
1142 p
->coef_stride
= FFALIGN(p
->dwt_width
, 32);
1143 p
->coef_buf
= av_mallocz(p
->coef_stride
*p
->dwt_height
*sizeof(dwtcoef
));
1145 return AVERROR(ENOMEM
);
1146 for (level
= s
->wavelet_depth
-1; level
>= 0; level
--) {
1149 for (o
= 0; o
< 4; o
++) {
1150 b
= &p
->band
[level
][o
];
1153 b
->stride
= p
->coef_stride
;
1154 shift
= (o
> 1)*b
->height
*b
->stride
+ (o
& 1)*b
->width
;
1155 b
->buf
= p
->coef_buf
+ shift
;
1160 if (ff_vc2enc_init_transforms(&s
->transform_args
[i
].t
,
1161 s
->plane
[i
].coef_stride
,
1162 s
->plane
[i
].dwt_height
,
1163 s
->slice_width
, s
->slice_height
))
1164 return AVERROR(ENOMEM
);
1168 s
->num_x
= s
->plane
[0].dwt_width
/s
->slice_width
;
1169 s
->num_y
= s
->plane
[0].dwt_height
/s
->slice_height
;
1171 s
->slice_args
= av_calloc(s
->num_x
*s
->num_y
, sizeof(SliceArgs
));
1173 return AVERROR(ENOMEM
);
1175 for (i
= 0; i
< 116; i
++) {
1176 const uint64_t qf
= ff_dirac_qscale_tab
[i
];
1177 const uint32_t m
= av_log2(qf
);
1178 const uint32_t t
= (1ULL << (m
+ 32)) / qf
;
1179 const uint32_t r
= (t
*qf
+ qf
) & UINT32_MAX
;
1180 if (!(qf
& (qf
- 1))) {
1181 s
->qmagic_lut
[i
][0] = 0xFFFFFFFF;
1182 s
->qmagic_lut
[i
][1] = 0xFFFFFFFF;
1183 } else if (r
<= 1 << m
) {
1184 s
->qmagic_lut
[i
][0] = t
+ 1;
1185 s
->qmagic_lut
[i
][1] = 0;
1187 s
->qmagic_lut
[i
][0] = t
;
1188 s
->qmagic_lut
[i
][1] = t
;
1195 #define VC2ENC_FLAGS (AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
1196 static const AVOption vc2enc_options
[] = {
1197 {"tolerance", "Max undershoot in percent", offsetof(VC2EncContext
, tolerance
), AV_OPT_TYPE_DOUBLE
, {.dbl
= 5.0f
}, 0.0f
, 45.0f
, VC2ENC_FLAGS
, .unit
= "tolerance"},
1198 {"slice_width", "Slice width", offsetof(VC2EncContext
, slice_width
), AV_OPT_TYPE_INT
, {.i64
= 32}, 32, 1024, VC2ENC_FLAGS
, .unit
= "slice_width"},
1199 {"slice_height", "Slice height", offsetof(VC2EncContext
, slice_height
), AV_OPT_TYPE_INT
, {.i64
= 16}, 8, 1024, VC2ENC_FLAGS
, .unit
= "slice_height"},
1200 {"wavelet_depth", "Transform depth", offsetof(VC2EncContext
, wavelet_depth
), AV_OPT_TYPE_INT
, {.i64
= 4}, 1, 5, VC2ENC_FLAGS
, .unit
= "wavelet_depth"},
1201 {"wavelet_type", "Transform type", offsetof(VC2EncContext
, wavelet_idx
), AV_OPT_TYPE_INT
, {.i64
= VC2_TRANSFORM_9_7
}, 0, VC2_TRANSFORMS_NB
, VC2ENC_FLAGS
, .unit
= "wavelet_idx"},
1202 {"9_7", "Deslauriers-Dubuc (9,7)", 0, AV_OPT_TYPE_CONST
, {.i64
= VC2_TRANSFORM_9_7
}, INT_MIN
, INT_MAX
, VC2ENC_FLAGS
, .unit
= "wavelet_idx"},
1203 {"5_3", "LeGall (5,3)", 0, AV_OPT_TYPE_CONST
, {.i64
= VC2_TRANSFORM_5_3
}, INT_MIN
, INT_MAX
, VC2ENC_FLAGS
, .unit
= "wavelet_idx"},
1204 {"haar", "Haar (with shift)", 0, AV_OPT_TYPE_CONST
, {.i64
= VC2_TRANSFORM_HAAR_S
}, INT_MIN
, INT_MAX
, VC2ENC_FLAGS
, .unit
= "wavelet_idx"},
1205 {"haar_noshift", "Haar (without shift)", 0, AV_OPT_TYPE_CONST
, {.i64
= VC2_TRANSFORM_HAAR
}, INT_MIN
, INT_MAX
, VC2ENC_FLAGS
, .unit
= "wavelet_idx"},
1206 {"qm", "Custom quantization matrix", offsetof(VC2EncContext
, quant_matrix
), AV_OPT_TYPE_INT
, {.i64
= VC2_QM_DEF
}, 0, VC2_QM_NB
, VC2ENC_FLAGS
, .unit
= "quant_matrix"},
1207 {"default", "Default from the specifications", 0, AV_OPT_TYPE_CONST
, {.i64
= VC2_QM_DEF
}, INT_MIN
, INT_MAX
, VC2ENC_FLAGS
, .unit
= "quant_matrix"},
1208 {"color", "Prevents low bitrate discoloration", 0, AV_OPT_TYPE_CONST
, {.i64
= VC2_QM_COL
}, INT_MIN
, INT_MAX
, VC2ENC_FLAGS
, .unit
= "quant_matrix"},
1209 {"flat", "Optimize for PSNR", 0, AV_OPT_TYPE_CONST
, {.i64
= VC2_QM_FLAT
}, INT_MIN
, INT_MAX
, VC2ENC_FLAGS
, .unit
= "quant_matrix"},
1213 static const AVClass vc2enc_class
= {
1214 .class_name
= "SMPTE VC-2 encoder",
1215 .category
= AV_CLASS_CATEGORY_ENCODER
,
1216 .option
= vc2enc_options
,
1217 .item_name
= av_default_item_name
,
1218 .version
= LIBAVUTIL_VERSION_INT
1221 static const FFCodecDefault vc2enc_defaults
[] = {
1222 { "b", "600000000" },
1226 static const enum AVPixelFormat allowed_pix_fmts
[] = {
1227 AV_PIX_FMT_YUV420P
, AV_PIX_FMT_YUV422P
, AV_PIX_FMT_YUV444P
,
1228 AV_PIX_FMT_YUV420P10
, AV_PIX_FMT_YUV422P10
, AV_PIX_FMT_YUV444P10
,
1229 AV_PIX_FMT_YUV420P12
, AV_PIX_FMT_YUV422P12
, AV_PIX_FMT_YUV444P12
,
1233 const FFCodec ff_vc2_encoder
= {
1235 CODEC_LONG_NAME("SMPTE VC-2"),
1236 .p
.type
= AVMEDIA_TYPE_VIDEO
,
1237 .p
.id
= AV_CODEC_ID_DIRAC
,
1238 .p
.capabilities
= AV_CODEC_CAP_DR1
| AV_CODEC_CAP_SLICE_THREADS
|
1239 AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
,
1240 .caps_internal
= FF_CODEC_CAP_INIT_CLEANUP
,
1241 .priv_data_size
= sizeof(VC2EncContext
),
1242 .init
= vc2_encode_init
,
1243 .close
= vc2_encode_end
,
1244 FF_CODEC_ENCODE_CB(vc2_encode_frame
),
1245 .p
.priv_class
= &vc2enc_class
,
1246 .defaults
= vc2enc_defaults
,
1247 .p
.pix_fmts
= allowed_pix_fmts
,
1248 .color_ranges
= AVCOL_RANGE_MPEG
| AVCOL_RANGE_JPEG
,