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/pixdesc.h"
23 #include "libavutil/opt.h"
29 #include "vc2enc_dwt.h"
32 /* Total range is -COEF_LUT_TAB to +COEFF_LUT_TAB, but total tab size is half
33 * (COEF_LUT_TAB*DIRAC_MAX_QUANT_INDEX), as the sign is appended during encoding */
34 #define COEF_LUT_TAB 2048
36 /* The limited size resolution of each slice forces us to do this */
37 #define SSIZE_ROUND(b) (FFALIGN((b), s->size_scaler) + 4 + s->prefix_bytes)
39 /* Decides the cutoff point in # of slices to distribute the leftover bytes */
40 #define SLICE_REDIST_TOTAL 150
42 typedef struct VC2BaseVideoFormat {
43 enum AVPixelFormat pix_fmt;
45 int width, height, 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 {
109 int cache[DIRAC_MAX_QUANT_INDEX];
119 typedef struct TransformArgs {
125 VC2TransformContext t;
128 typedef struct VC2EncContext {
132 AVCodecContext *avctx;
133 DiracVersionInfo ver;
135 SliceArgs *slice_args;
136 TransformArgs transform_args[3];
138 /* For conversion from unsigned pixel values to signed */
144 uint32_t picture_number;
146 /* Base video format */
151 /* Quantization matrix */
152 uint8_t quant[MAX_DWT_LEVELS][4];
153 int custom_quant_matrix;
155 /* Coefficient LUT */
156 uint32_t *coef_lut_val;
157 uint8_t *coef_lut_len;
159 int num_x; /* #slices horizontally */
160 int num_y; /* #slices vertically */
166 /* Rate control stuff */
177 int strict_compliance;
181 enum VC2_QM quant_matrix;
183 /* Parse code state */
184 uint32_t next_parse_offset;
185 enum DiracParseCodes last_parse_code;
188 static av_always_inline void put_vc2_ue_uint(PutBitContext *pb, uint32_t val)
191 int pbits = 0, bits = 0, topbit = 1, maxval = 1;
198 while (val > maxval) {
204 bits = ff_log2(topbit);
206 for (i = 0; i < bits; i++) {
213 put_bits(pb, bits*2 + 1, (pbits << 1) | 1);
216 static av_always_inline int count_vc2_ue_uint(uint32_t val)
218 int topbit = 1, maxval = 1;
223 while (val > maxval) {
229 return ff_log2(topbit)*2 + 1;
232 static av_always_inline void get_vc2_ue_uint(int val, uint8_t *nbits,
236 int pbits = 0, bits = 0, topbit = 1, maxval = 1;
244 while (val > maxval) {
250 bits = ff_log2(topbit);
252 for (i = 0; i < bits; i++) {
260 *eval = (pbits << 1) | 1;
263 /* VC-2 10.4 - parse_info() */
264 static void encode_parse_info(VC2EncContext *s, enum DiracParseCodes pcode)
266 uint32_t cur_pos, dist;
268 avpriv_align_put_bits(&s->pb);
270 cur_pos = put_bits_count(&s->pb) >> 3;
273 avpriv_put_string(&s->pb, "BBCD", 0);
276 put_bits(&s->pb, 8, pcode);
278 /* Next parse offset */
279 dist = cur_pos - s->next_parse_offset;
280 AV_WB32(s->pb.buf + s->next_parse_offset + 5, dist);
281 s->next_parse_offset = cur_pos;
282 put_bits32(&s->pb, pcode == DIRAC_PCODE_END_SEQ ? 13 : 0);
284 /* Last parse offset */
285 put_bits32(&s->pb, s->last_parse_code == DIRAC_PCODE_END_SEQ ? 13 : dist);
287 s->last_parse_code = pcode;
290 /* VC-2 11.1 - parse_parameters()
291 * The level dictates what the decoder should expect in terms of resolution
292 * and allows it to quickly reject whatever it can't support. Remember,
293 * this codec kinda targets cheapo FPGAs without much memory. Unfortunately
294 * it also limits us greatly in our choice of formats, hence the flag to disable
295 * strict_compliance */
296 static void encode_parse_params(VC2EncContext *s)
298 put_vc2_ue_uint(&s->pb, s->ver.major); /* VC-2 demands this to be 2 */
299 put_vc2_ue_uint(&s->pb, s->ver.minor); /* ^^ and this to be 0 */
300 put_vc2_ue_uint(&s->pb, s->profile); /* 3 to signal HQ profile */
301 put_vc2_ue_uint(&s->pb, s->level); /* 3 - 1080/720, 6 - 4K */
304 /* VC-2 11.3 - frame_size() */
305 static void encode_frame_size(VC2EncContext *s)
307 put_bits(&s->pb, 1, !s->strict_compliance);
308 if (!s->strict_compliance) {
309 AVCodecContext *avctx = s->avctx;
310 put_vc2_ue_uint(&s->pb, avctx->width);
311 put_vc2_ue_uint(&s->pb, avctx->height);
315 /* VC-2 11.3.3 - color_diff_sampling_format() */
316 static void encode_sample_fmt(VC2EncContext *s)
318 put_bits(&s->pb, 1, !s->strict_compliance);
319 if (!s->strict_compliance) {
321 if (s->chroma_x_shift == 1 && s->chroma_y_shift == 0)
323 else if (s->chroma_x_shift == 1 && s->chroma_y_shift == 1)
327 put_vc2_ue_uint(&s->pb, idx);
331 /* VC-2 11.3.4 - scan_format() */
332 static void encode_scan_format(VC2EncContext *s)
334 put_bits(&s->pb, 1, !s->strict_compliance);
335 if (!s->strict_compliance)
336 put_vc2_ue_uint(&s->pb, s->interlaced);
339 /* VC-2 11.3.5 - frame_rate() */
340 static void encode_frame_rate(VC2EncContext *s)
342 put_bits(&s->pb, 1, !s->strict_compliance);
343 if (!s->strict_compliance) {
344 AVCodecContext *avctx = s->avctx;
345 put_vc2_ue_uint(&s->pb, 0);
346 put_vc2_ue_uint(&s->pb, avctx->time_base.den);
347 put_vc2_ue_uint(&s->pb, avctx->time_base.num);
351 /* VC-2 11.3.6 - aspect_ratio() */
352 static void encode_aspect_ratio(VC2EncContext *s)
354 put_bits(&s->pb, 1, !s->strict_compliance);
355 if (!s->strict_compliance) {
356 AVCodecContext *avctx = s->avctx;
357 put_vc2_ue_uint(&s->pb, 0);
358 put_vc2_ue_uint(&s->pb, avctx->sample_aspect_ratio.num);
359 put_vc2_ue_uint(&s->pb, avctx->sample_aspect_ratio.den);
363 /* VC-2 11.3.7 - clean_area() */
364 static void encode_clean_area(VC2EncContext *s)
366 put_bits(&s->pb, 1, 0);
369 /* VC-2 11.3.8 - signal_range() */
370 static void encode_signal_range(VC2EncContext *s)
372 put_bits(&s->pb, 1, !s->strict_compliance);
373 if (!s->strict_compliance)
374 put_vc2_ue_uint(&s->pb, s->bpp_idx);
377 /* VC-2 11.3.9 - color_spec() */
378 static void encode_color_spec(VC2EncContext *s)
380 AVCodecContext *avctx = s->avctx;
381 put_bits(&s->pb, 1, !s->strict_compliance);
382 if (!s->strict_compliance) {
384 put_vc2_ue_uint(&s->pb, 0);
387 put_bits(&s->pb, 1, 1);
388 if (avctx->color_primaries == AVCOL_PRI_BT470BG)
390 else if (avctx->color_primaries == AVCOL_PRI_SMPTE170M)
392 else if (avctx->color_primaries == AVCOL_PRI_SMPTE240M)
396 put_vc2_ue_uint(&s->pb, val);
399 put_bits(&s->pb, 1, 1);
400 if (avctx->colorspace == AVCOL_SPC_RGB)
402 else if (avctx->colorspace == AVCOL_SPC_YCOCG)
404 else if (avctx->colorspace == AVCOL_SPC_BT470BG)
408 put_vc2_ue_uint(&s->pb, val);
410 /* transfer function */
411 put_bits(&s->pb, 1, 1);
412 if (avctx->color_trc == AVCOL_TRC_LINEAR)
414 else if (avctx->color_trc == AVCOL_TRC_BT1361_ECG)
418 put_vc2_ue_uint(&s->pb, val);
422 /* VC-2 11.3 - source_parameters() */
423 static void encode_source_params(VC2EncContext *s)
425 encode_frame_size(s);
426 encode_sample_fmt(s);
427 encode_scan_format(s);
428 encode_frame_rate(s);
429 encode_aspect_ratio(s);
430 encode_clean_area(s);
431 encode_signal_range(s);
432 encode_color_spec(s);
435 /* VC-2 11 - sequence_header() */
436 static void encode_seq_header(VC2EncContext *s)
438 avpriv_align_put_bits(&s->pb);
439 encode_parse_params(s);
440 put_vc2_ue_uint(&s->pb, s->base_vf);
441 encode_source_params(s);
442 put_vc2_ue_uint(&s->pb, s->interlaced); /* Frames or fields coding */
445 /* VC-2 12.1 - picture_header() */
446 static void encode_picture_header(VC2EncContext *s)
448 avpriv_align_put_bits(&s->pb);
449 put_bits32(&s->pb, s->picture_number++);
452 /* VC-2 12.3.4.1 - slice_parameters() */
453 static void encode_slice_params(VC2EncContext *s)
455 put_vc2_ue_uint(&s->pb, s->num_x);
456 put_vc2_ue_uint(&s->pb, s->num_y);
457 put_vc2_ue_uint(&s->pb, s->prefix_bytes);
458 put_vc2_ue_uint(&s->pb, s->size_scaler);
461 /* 1st idx = LL, second - vertical, third - horizontal, fourth - total */
462 const uint8_t vc2_qm_col_tab[][4] = {
470 const uint8_t vc2_qm_flat_tab[][4] = {
478 static void init_quant_matrix(VC2EncContext *s)
480 int level, orientation;
482 if (s->wavelet_depth <= 4 && s->quant_matrix == VC2_QM_DEF) {
483 s->custom_quant_matrix = 0;
484 for (level = 0; level < s->wavelet_depth; level++) {
485 s->quant[level][0] = ff_dirac_default_qmat[s->wavelet_idx][level][0];
486 s->quant[level][1] = ff_dirac_default_qmat[s->wavelet_idx][level][1];
487 s->quant[level][2] = ff_dirac_default_qmat[s->wavelet_idx][level][2];
488 s->quant[level][3] = ff_dirac_default_qmat[s->wavelet_idx][level][3];
493 s->custom_quant_matrix = 1;
495 if (s->quant_matrix == VC2_QM_DEF) {
496 for (level = 0; level < s->wavelet_depth; level++) {
497 for (orientation = 0; orientation < 4; orientation++) {
499 s->quant[level][orientation] = ff_dirac_default_qmat[s->wavelet_idx][level][orientation];
501 s->quant[level][orientation] = vc2_qm_col_tab[level][orientation];
504 } else if (s->quant_matrix == VC2_QM_COL) {
505 for (level = 0; level < s->wavelet_depth; level++) {
506 for (orientation = 0; orientation < 4; orientation++) {
507 s->quant[level][orientation] = vc2_qm_col_tab[level][orientation];
511 for (level = 0; level < s->wavelet_depth; level++) {
512 for (orientation = 0; orientation < 4; orientation++) {
513 s->quant[level][orientation] = vc2_qm_flat_tab[level][orientation];
519 /* VC-2 12.3.4.2 - quant_matrix() */
520 static void encode_quant_matrix(VC2EncContext *s)
523 put_bits(&s->pb, 1, s->custom_quant_matrix);
524 if (s->custom_quant_matrix) {
525 put_vc2_ue_uint(&s->pb, s->quant[0][0]);
526 for (level = 0; level < s->wavelet_depth; level++) {
527 put_vc2_ue_uint(&s->pb, s->quant[level][1]);
528 put_vc2_ue_uint(&s->pb, s->quant[level][2]);
529 put_vc2_ue_uint(&s->pb, s->quant[level][3]);
534 /* VC-2 12.3 - transform_parameters() */
535 static void encode_transform_params(VC2EncContext *s)
537 put_vc2_ue_uint(&s->pb, s->wavelet_idx);
538 put_vc2_ue_uint(&s->pb, s->wavelet_depth);
540 encode_slice_params(s);
541 encode_quant_matrix(s);
544 /* VC-2 12.2 - wavelet_transform() */
545 static void encode_wavelet_transform(VC2EncContext *s)
547 encode_transform_params(s);
548 avpriv_align_put_bits(&s->pb);
551 /* VC-2 12 - picture_parse() */
552 static void encode_picture_start(VC2EncContext *s)
554 avpriv_align_put_bits(&s->pb);
555 encode_picture_header(s);
556 avpriv_align_put_bits(&s->pb);
557 encode_wavelet_transform(s);
560 #define QUANT(c, qf) (((c) << 2)/(qf))
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)
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;
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];
577 dwtcoef *coeff = b->buf + top * b->stride;
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 put_bits(pb, len_lut[c_abs], val_lut[c_abs] | neg);
586 c_abs = QUANT(c_abs, qfactor);
587 put_vc2_ue_uint(pb, c_abs);
589 put_bits(pb, 1, neg);
596 static int count_hq_slice(SliceArgs *slice, int quant_idx)
599 uint8_t quants[MAX_DWT_LEVELS][4];
600 int bits = 0, p, level, orientation;
601 VC2EncContext *s = slice->ctx;
603 if (slice->cache[quant_idx])
604 return slice->cache[quant_idx];
606 bits += 8*s->prefix_bytes;
607 bits += 8; /* quant_idx */
609 for (level = 0; level < s->wavelet_depth; level++)
610 for (orientation = !!level; orientation < 4; orientation++)
611 quants[level][orientation] = FFMAX(quant_idx - s->quant[level][orientation], 0);
613 for (p = 0; p < 3; p++) {
614 int bytes_start, bytes_len, pad_s, pad_c;
615 bytes_start = bits >> 3;
617 for (level = 0; level < s->wavelet_depth; level++) {
618 for (orientation = !!level; orientation < 4; orientation++) {
619 SubBand *b = &s->plane[p].band[level][orientation];
621 const int q_idx = quants[level][orientation];
622 const uint8_t *len_lut = &s->coef_lut_len[q_idx*COEF_LUT_TAB];
623 const int qfactor = ff_dirac_qscale_tab[q_idx];
625 const int left = b->width * slice->x / s->num_x;
626 const int right = b->width *(slice->x+1) / s->num_x;
627 const int top = b->height * slice->y / s->num_y;
628 const int bottom = b->height *(slice->y+1) / s->num_y;
630 dwtcoef *buf = b->buf + top * b->stride;
632 for (y = top; y < bottom; y++) {
633 for (x = left; x < right; x++) {
634 uint32_t c_abs = FFABS(buf[x]);
635 if (c_abs < COEF_LUT_TAB) {
636 bits += len_lut[c_abs];
638 c_abs = QUANT(c_abs, qfactor);
639 bits += count_vc2_ue_uint(c_abs);
647 bits += FFALIGN(bits, 8) - bits;
648 bytes_len = (bits >> 3) - bytes_start - 1;
649 pad_s = FFALIGN(bytes_len, s->size_scaler)/s->size_scaler;
650 pad_c = (pad_s*s->size_scaler) - bytes_len;
654 slice->cache[quant_idx] = bits;
659 /* Approaches the best possible quantizer asymptotically, its kinda exaustive
660 * but we have a LUT to get the coefficient size in bits. Guaranteed to never
661 * overshoot, which is apparently very important when streaming */
662 static int rate_control(AVCodecContext *avctx, void *arg)
664 SliceArgs *slice_dat = arg;
665 VC2EncContext *s = slice_dat->ctx;
666 const int top = slice_dat->bits_ceil;
667 const int bottom = slice_dat->bits_floor;
668 int quant_buf[2] = {-1, -1};
669 int quant = slice_dat->quant_idx, step = 1;
670 int bits_last, bits = count_hq_slice(slice_dat, quant);
671 while ((bits > top) || (bits < bottom)) {
672 const int signed_step = bits > top ? +step : -step;
673 quant = av_clip(quant + signed_step, 0, s->q_ceil-1);
674 bits = count_hq_slice(slice_dat, quant);
675 if (quant_buf[1] == quant) {
676 quant = FFMAX(quant_buf[0], quant);
677 bits = quant == quant_buf[0] ? bits_last : bits;
680 step = av_clip(step/2, 1, (s->q_ceil-1)/2);
681 quant_buf[1] = quant_buf[0];
682 quant_buf[0] = quant;
685 slice_dat->quant_idx = av_clip(quant, 0, s->q_ceil-1);
686 slice_dat->bytes = SSIZE_ROUND(bits >> 3);
690 static int calc_slice_sizes(VC2EncContext *s)
692 int i, j, slice_x, slice_y, bytes_left = 0;
693 int bytes_top[SLICE_REDIST_TOTAL] = {0};
694 int64_t total_bytes_needed = 0;
695 int slice_redist_range = FFMIN(SLICE_REDIST_TOTAL, s->num_x*s->num_y);
696 SliceArgs *enc_args = s->slice_args;
697 SliceArgs *top_loc[SLICE_REDIST_TOTAL] = {NULL};
699 init_quant_matrix(s);
701 for (slice_y = 0; slice_y < s->num_y; slice_y++) {
702 for (slice_x = 0; slice_x < s->num_x; slice_x++) {
703 SliceArgs *args = &enc_args[s->num_x*slice_y + slice_x];
707 args->bits_ceil = s->slice_max_bytes << 3;
708 args->bits_floor = s->slice_min_bytes << 3;
709 memset(args->cache, 0, s->q_ceil*sizeof(*args->cache));
713 /* First pass - determine baseline slice sizes w.r.t. max_slice_size */
714 s->avctx->execute(s->avctx, rate_control, enc_args, NULL, s->num_x*s->num_y,
717 for (i = 0; i < s->num_x*s->num_y; i++) {
718 SliceArgs *args = &enc_args[i];
719 bytes_left += args->bytes;
720 for (j = 0; j < slice_redist_range; j++) {
721 if (args->bytes > bytes_top[j]) {
722 bytes_top[j] = args->bytes;
729 bytes_left = s->frame_max_bytes - bytes_left;
731 /* Second pass - distribute leftover bytes */
732 while (bytes_left > 0) {
734 for (i = 0; i < slice_redist_range; i++) {
736 int bits, bytes, diff, prev_bytes, new_idx;
739 if (!top_loc[i] || !top_loc[i]->quant_idx)
742 prev_bytes = args->bytes;
743 new_idx = FFMAX(args->quant_idx - 1, 0);
744 bits = count_hq_slice(args, new_idx);
745 bytes = SSIZE_ROUND(bits >> 3);
746 diff = bytes - prev_bytes;
747 if ((bytes_left - diff) > 0) {
748 args->quant_idx = new_idx;
758 for (i = 0; i < s->num_x*s->num_y; i++) {
759 SliceArgs *args = &enc_args[i];
760 total_bytes_needed += args->bytes;
761 s->q_avg = (s->q_avg + args->quant_idx)/2;
764 return total_bytes_needed;
767 /* VC-2 13.5.3 - hq_slice */
768 static int encode_hq_slice(AVCodecContext *avctx, void *arg)
770 SliceArgs *slice_dat = arg;
771 VC2EncContext *s = slice_dat->ctx;
772 PutBitContext *pb = &slice_dat->pb;
773 const int slice_x = slice_dat->x;
774 const int slice_y = slice_dat->y;
775 const int quant_idx = slice_dat->quant_idx;
776 const int slice_bytes_max = slice_dat->bytes;
777 uint8_t quants[MAX_DWT_LEVELS][4];
778 int p, level, orientation;
780 /* The reference decoder ignores it, and its typical length is 0 */
781 memset(put_bits_ptr(pb), 0, s->prefix_bytes);
782 skip_put_bytes(pb, s->prefix_bytes);
784 put_bits(pb, 8, quant_idx);
786 /* Slice quantization (slice_quantizers() in the specs) */
787 for (level = 0; level < s->wavelet_depth; level++)
788 for (orientation = !!level; orientation < 4; orientation++)
789 quants[level][orientation] = FFMAX(quant_idx - s->quant[level][orientation], 0);
791 /* Luma + 2 Chroma planes */
792 for (p = 0; p < 3; p++) {
793 int bytes_start, bytes_len, pad_s, pad_c;
794 bytes_start = put_bits_count(pb) >> 3;
796 for (level = 0; level < s->wavelet_depth; level++) {
797 for (orientation = !!level; orientation < 4; orientation++) {
798 encode_subband(s, pb, slice_x, slice_y,
799 &s->plane[p].band[level][orientation],
800 quants[level][orientation]);
803 avpriv_align_put_bits(pb);
804 bytes_len = (put_bits_count(pb) >> 3) - bytes_start - 1;
806 int len_diff = slice_bytes_max - (put_bits_count(pb) >> 3);
807 pad_s = FFALIGN((bytes_len + len_diff), s->size_scaler)/s->size_scaler;
808 pad_c = (pad_s*s->size_scaler) - bytes_len;
810 pad_s = FFALIGN(bytes_len, s->size_scaler)/s->size_scaler;
811 pad_c = (pad_s*s->size_scaler) - bytes_len;
813 pb->buf[bytes_start] = pad_s;
815 /* vc2-reference uses that padding that decodes to '0' coeffs */
816 memset(put_bits_ptr(pb), 0xFF, pad_c);
817 skip_put_bytes(pb, pad_c);
823 /* VC-2 13.5.1 - low_delay_transform_data() */
824 static int encode_slices(VC2EncContext *s)
827 int slice_x, slice_y, skip = 0;
828 SliceArgs *enc_args = s->slice_args;
830 avpriv_align_put_bits(&s->pb);
831 flush_put_bits(&s->pb);
832 buf = put_bits_ptr(&s->pb);
834 for (slice_y = 0; slice_y < s->num_y; slice_y++) {
835 for (slice_x = 0; slice_x < s->num_x; slice_x++) {
836 SliceArgs *args = &enc_args[s->num_x*slice_y + slice_x];
837 init_put_bits(&args->pb, buf + skip, args->bytes+s->prefix_bytes);
842 s->avctx->execute(s->avctx, encode_hq_slice, enc_args, NULL, s->num_x*s->num_y,
845 skip_put_bytes(&s->pb, skip);
851 * Transform basics for a 3 level transform
852 * |---------------------------------------------------------------------|
853 * | LL-0 | HL-0 | | |
854 * |--------|-------| HL-1 | |
855 * | LH-0 | HH-0 | | |
856 * |----------------|-----------------| HL-2 |
860 * |----------------------------------|----------------------------------|
868 * |---------------------------------------------------------------------|
870 * DWT transforms are generally applied by splitting the image in two vertically
871 * and applying a low pass transform on the left part and a corresponding high
872 * pass transform on the right hand side. This is known as the horizontal filter
874 * After that, the same operation is performed except the image is divided
875 * horizontally, with the high pass on the lower and the low pass on the higher
877 * Therefore, you're left with 4 subdivisions - known as low-low, low-high,
878 * high-low and high-high. They're referred to as orientations in the decoder
881 * The LL (low-low) area contains the original image downsampled by the amount
882 * of levels. The rest of the areas can be thought as the details needed
883 * to restore the image perfectly to its original size.
885 static int dwt_plane(AVCodecContext *avctx, void *arg)
887 TransformArgs *transform_dat = arg;
888 VC2EncContext *s = transform_dat->ctx;
889 const void *frame_data = transform_dat->idata;
890 const ptrdiff_t linesize = transform_dat->istride;
891 const int field = transform_dat->field;
892 const Plane *p = transform_dat->plane;
893 VC2TransformContext *t = &transform_dat->t;
894 dwtcoef *buf = p->coef_buf;
895 const int idx = s->wavelet_idx;
896 const int skip = 1 + s->interlaced;
898 int x, y, level, offset;
899 ptrdiff_t pix_stride = linesize >> (s->bpp - 1);
904 } else if (field == 2) {
912 const uint8_t *pix = (const uint8_t *)frame_data + offset;
913 for (y = 0; y < p->height*skip; y+=skip) {
914 for (x = 0; x < p->width; x++) {
915 buf[x] = pix[x] - s->diff_offset;
917 buf += p->coef_stride;
921 const uint16_t *pix = (const uint16_t *)frame_data + offset;
922 for (y = 0; y < p->height*skip; y+=skip) {
923 for (x = 0; x < p->width; x++) {
924 buf[x] = pix[x] - s->diff_offset;
926 buf += p->coef_stride;
931 memset(buf, 0, p->coef_stride * (p->dwt_height - p->height) * sizeof(dwtcoef));
933 for (level = s->wavelet_depth-1; level >= 0; level--) {
934 const SubBand *b = &p->band[level][0];
935 t->vc2_subband_dwt[idx](t, p->coef_buf, p->coef_stride,
936 b->width, b->height);
942 static int encode_frame(VC2EncContext *s, AVPacket *avpkt, const AVFrame *frame,
943 const char *aux_data, const int header_size, int field)
946 int64_t max_frame_bytes;
948 /* Threaded DWT transform */
949 for (i = 0; i < 3; i++) {
950 s->transform_args[i].ctx = s;
951 s->transform_args[i].field = field;
952 s->transform_args[i].plane = &s->plane[i];
953 s->transform_args[i].idata = frame->data[i];
954 s->transform_args[i].istride = frame->linesize[i];
956 s->avctx->execute(s->avctx, dwt_plane, s->transform_args, NULL, 3,
957 sizeof(TransformArgs));
959 /* Calculate per-slice quantizers and sizes */
960 max_frame_bytes = header_size + calc_slice_sizes(s);
963 ret = ff_alloc_packet2(s->avctx, avpkt,
964 max_frame_bytes << s->interlaced,
965 max_frame_bytes << s->interlaced);
967 av_log(s->avctx, AV_LOG_ERROR, "Error getting output packet.\n");
970 init_put_bits(&s->pb, avpkt->data, avpkt->size);
973 /* Sequence header */
974 encode_parse_info(s, DIRAC_PCODE_SEQ_HEADER);
975 encode_seq_header(s);
977 /* Encoder version */
979 encode_parse_info(s, DIRAC_PCODE_AUX);
980 avpriv_put_string(&s->pb, aux_data, 1);
984 encode_parse_info(s, DIRAC_PCODE_PICTURE_HQ);
985 encode_picture_start(s);
991 encode_parse_info(s, DIRAC_PCODE_END_SEQ);
996 static av_cold int vc2_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
997 const AVFrame *frame, int *got_packet)
1000 int slice_ceil, sig_size = 256;
1001 VC2EncContext *s = avctx->priv_data;
1002 const int bitexact = avctx->flags & AV_CODEC_FLAG_BITEXACT;
1003 const char *aux_data = bitexact ? "Lavc" : LIBAVCODEC_IDENT;
1004 const int aux_data_size = bitexact ? sizeof("Lavc") : sizeof(LIBAVCODEC_IDENT);
1005 const int header_size = 100 + aux_data_size;
1006 int64_t r_bitrate = avctx->bit_rate >> (s->interlaced);
1010 s->prefix_bytes = 0;
1011 s->last_parse_code = 0;
1012 s->next_parse_offset = 0;
1015 s->frame_max_bytes = (av_rescale(r_bitrate, s->avctx->time_base.num,
1016 s->avctx->time_base.den) >> 3) - header_size;
1017 s->slice_max_bytes = slice_ceil = av_rescale(s->frame_max_bytes, 1, s->num_x*s->num_y);
1019 /* Find an appropriate size scaler */
1020 while (sig_size > 255) {
1021 int r_size = SSIZE_ROUND(s->slice_max_bytes);
1022 if (r_size > slice_ceil) {
1023 s->slice_max_bytes -= r_size - slice_ceil;
1024 r_size = SSIZE_ROUND(s->slice_max_bytes);
1026 sig_size = r_size/s->size_scaler; /* Signalled slize size */
1027 s->size_scaler <<= 1;
1030 s->slice_min_bytes = s->slice_max_bytes - s->slice_max_bytes*(s->tolerance/100.0f);
1032 ret = encode_frame(s, avpkt, frame, aux_data, header_size, s->interlaced);
1035 if (s->interlaced) {
1036 ret = encode_frame(s, avpkt, frame, aux_data, header_size, 2);
1041 flush_put_bits(&s->pb);
1042 avpkt->size = put_bits_count(&s->pb) >> 3;
1049 static av_cold int vc2_encode_end(AVCodecContext *avctx)
1052 VC2EncContext *s = avctx->priv_data;
1054 av_log(avctx, AV_LOG_INFO, "Qavg: %i\n", s->q_avg);
1056 for (i = 0; i < 3; i++) {
1057 ff_vc2enc_free_transforms(&s->transform_args[i].t);
1058 av_freep(&s->plane[i].coef_buf);
1061 av_freep(&s->slice_args);
1062 av_freep(&s->coef_lut_len);
1063 av_freep(&s->coef_lut_val);
1068 static av_cold int vc2_encode_init(AVCodecContext *avctx)
1072 int i, j, level, o, shift, ret;
1073 const AVPixFmtDescriptor *fmt = av_pix_fmt_desc_get(avctx->pix_fmt);
1074 const int depth = fmt->comp[0].depth;
1075 VC2EncContext *s = avctx->priv_data;
1077 s->picture_number = 0;
1079 /* Total allowed quantization range */
1080 s->q_ceil = DIRAC_MAX_QUANT_INDEX;
1088 s->strict_compliance = 1;
1091 s->slice_max_bytes = 0;
1092 s->slice_min_bytes = 0;
1094 /* Mark unknown as progressive */
1095 s->interlaced = !((avctx->field_order == AV_FIELD_UNKNOWN) ||
1096 (avctx->field_order == AV_FIELD_PROGRESSIVE));
1098 for (i = 0; i < base_video_fmts_len; i++) {
1099 const VC2BaseVideoFormat *fmt = &base_video_fmts[i];
1100 if (avctx->pix_fmt != fmt->pix_fmt)
1102 if (avctx->time_base.num != fmt->time_base.num)
1104 if (avctx->time_base.den != fmt->time_base.den)
1106 if (avctx->width != fmt->width)
1108 if (avctx->height != fmt->height)
1110 if (s->interlaced != fmt->interlaced)
1113 s->level = base_video_fmts[i].level;
1118 av_log(avctx, AV_LOG_WARNING, "Interlacing enabled!\n");
1120 if ((s->slice_width & (s->slice_width - 1)) ||
1121 (s->slice_height & (s->slice_height - 1))) {
1122 av_log(avctx, AV_LOG_ERROR, "Slice size is not a power of two!\n");
1123 return AVERROR_UNKNOWN;
1126 if ((s->slice_width > avctx->width) ||
1127 (s->slice_height > avctx->height)) {
1128 av_log(avctx, AV_LOG_ERROR, "Slice size is bigger than the image!\n");
1129 return AVERROR_UNKNOWN;
1132 if (s->base_vf <= 0) {
1133 if (avctx->strict_std_compliance < FF_COMPLIANCE_STRICT) {
1134 s->strict_compliance = s->base_vf = 0;
1135 av_log(avctx, AV_LOG_WARNING, "Format does not strictly comply with VC2 specs\n");
1137 av_log(avctx, AV_LOG_WARNING, "Given format does not strictly comply with "
1138 "the specifications, decrease strictness to use it.\n");
1139 return AVERROR_UNKNOWN;
1142 av_log(avctx, AV_LOG_INFO, "Selected base video format = %i (%s)\n",
1143 s->base_vf, base_video_fmts[s->base_vf].name);
1146 /* Chroma subsampling */
1147 ret = av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_x_shift, &s->chroma_y_shift);
1151 /* Bit depth and color range index */
1152 if (depth == 8 && avctx->color_range == AVCOL_RANGE_JPEG) {
1155 s->diff_offset = 128;
1156 } else if (depth == 8 && (avctx->color_range == AVCOL_RANGE_MPEG ||
1157 avctx->color_range == AVCOL_RANGE_UNSPECIFIED)) {
1160 s->diff_offset = 128;
1161 } else if (depth == 10) {
1164 s->diff_offset = 512;
1168 s->diff_offset = 2048;
1171 /* Planes initialization */
1172 for (i = 0; i < 3; i++) {
1175 p->width = avctx->width >> (i ? s->chroma_x_shift : 0);
1176 p->height = avctx->height >> (i ? s->chroma_y_shift : 0);
1179 p->dwt_width = w = FFALIGN(p->width, (1 << s->wavelet_depth));
1180 p->dwt_height = h = FFALIGN(p->height, (1 << s->wavelet_depth));
1181 p->coef_stride = FFALIGN(p->dwt_width, 32);
1182 p->coef_buf = av_mallocz(p->coef_stride*p->dwt_height*sizeof(dwtcoef));
1185 for (level = s->wavelet_depth-1; level >= 0; level--) {
1188 for (o = 0; o < 4; o++) {
1189 b = &p->band[level][o];
1192 b->stride = p->coef_stride;
1193 shift = (o > 1)*b->height*b->stride + (o & 1)*b->width;
1194 b->buf = p->coef_buf + shift;
1199 if (ff_vc2enc_init_transforms(&s->transform_args[i].t,
1200 s->plane[i].coef_stride,
1201 s->plane[i].dwt_height,
1202 s->slice_width, s->slice_height))
1207 s->num_x = s->plane[0].dwt_width/s->slice_width;
1208 s->num_y = s->plane[0].dwt_height/s->slice_height;
1210 s->slice_args = av_calloc(s->num_x*s->num_y, sizeof(SliceArgs));
1215 s->coef_lut_len = av_malloc(COEF_LUT_TAB*(s->q_ceil+1)*sizeof(*s->coef_lut_len));
1216 if (!s->coef_lut_len)
1219 s->coef_lut_val = av_malloc(COEF_LUT_TAB*(s->q_ceil+1)*sizeof(*s->coef_lut_val));
1220 if (!s->coef_lut_val)
1223 for (i = 0; i < s->q_ceil; i++) {
1224 uint8_t *len_lut = &s->coef_lut_len[i*COEF_LUT_TAB];
1225 uint32_t *val_lut = &s->coef_lut_val[i*COEF_LUT_TAB];
1226 for (j = 0; j < COEF_LUT_TAB; j++) {
1227 get_vc2_ue_uint(QUANT(j, ff_dirac_qscale_tab[i]),
1228 &len_lut[j], &val_lut[j]);
1229 if (len_lut[j] != 1) {
1241 vc2_encode_end(avctx);
1242 av_log(avctx, AV_LOG_ERROR, "Unable to allocate memory!\n");
1243 return AVERROR(ENOMEM);
1246 #define VC2ENC_FLAGS (AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
1247 static const AVOption vc2enc_options[] = {
1248 {"tolerance", "Max undershoot in percent", offsetof(VC2EncContext, tolerance), AV_OPT_TYPE_DOUBLE, {.dbl = 5.0f}, 0.0f, 45.0f, VC2ENC_FLAGS, "tolerance"},
1249 {"slice_width", "Slice width", offsetof(VC2EncContext, slice_width), AV_OPT_TYPE_INT, {.i64 = 32}, 32, 1024, VC2ENC_FLAGS, "slice_width"},
1250 {"slice_height", "Slice height", offsetof(VC2EncContext, slice_height), AV_OPT_TYPE_INT, {.i64 = 16}, 8, 1024, VC2ENC_FLAGS, "slice_height"},
1251 {"wavelet_depth", "Transform depth", offsetof(VC2EncContext, wavelet_depth), AV_OPT_TYPE_INT, {.i64 = 4}, 1, 5, VC2ENC_FLAGS, "wavelet_depth"},
1252 {"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"},
1253 {"9_7", "Deslauriers-Dubuc (9,7)", 0, AV_OPT_TYPE_CONST, {.i64 = VC2_TRANSFORM_9_7}, INT_MIN, INT_MAX, VC2ENC_FLAGS, "wavelet_idx"},
1254 {"5_3", "LeGall (5,3)", 0, AV_OPT_TYPE_CONST, {.i64 = VC2_TRANSFORM_5_3}, INT_MIN, INT_MAX, VC2ENC_FLAGS, "wavelet_idx"},
1255 {"haar", "Haar (with shift)", 0, AV_OPT_TYPE_CONST, {.i64 = VC2_TRANSFORM_HAAR_S}, INT_MIN, INT_MAX, VC2ENC_FLAGS, "wavelet_idx"},
1256 {"haar_noshift", "Haar (without shift)", 0, AV_OPT_TYPE_CONST, {.i64 = VC2_TRANSFORM_HAAR}, INT_MIN, INT_MAX, VC2ENC_FLAGS, "wavelet_idx"},
1257 {"qm", "Custom quantization matrix", offsetof(VC2EncContext, quant_matrix), AV_OPT_TYPE_INT, {.i64 = VC2_QM_DEF}, 0, VC2_QM_NB, VC2ENC_FLAGS, "quant_matrix"},
1258 {"default", "Default from the specifications", 0, AV_OPT_TYPE_CONST, {.i64 = VC2_QM_DEF}, INT_MIN, INT_MAX, VC2ENC_FLAGS, "quant_matrix"},
1259 {"color", "Prevents low bitrate discoloration", 0, AV_OPT_TYPE_CONST, {.i64 = VC2_QM_COL}, INT_MIN, INT_MAX, VC2ENC_FLAGS, "quant_matrix"},
1260 {"flat", "Optimize for PSNR", 0, AV_OPT_TYPE_CONST, {.i64 = VC2_QM_FLAT}, INT_MIN, INT_MAX, VC2ENC_FLAGS, "quant_matrix"},
1264 static const AVClass vc2enc_class = {
1265 .class_name = "SMPTE VC-2 encoder",
1266 .category = AV_CLASS_CATEGORY_ENCODER,
1267 .option = vc2enc_options,
1268 .item_name = av_default_item_name,
1269 .version = LIBAVUTIL_VERSION_INT
1272 static const AVCodecDefault vc2enc_defaults[] = {
1273 { "b", "600000000" },
1277 static const enum AVPixelFormat allowed_pix_fmts[] = {
1278 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P,
1279 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
1280 AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12,
1284 AVCodec ff_vc2_encoder = {
1286 .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-2"),
1287 .type = AVMEDIA_TYPE_VIDEO,
1288 .id = AV_CODEC_ID_DIRAC,
1289 .priv_data_size = sizeof(VC2EncContext),
1290 .init = vc2_encode_init,
1291 .close = vc2_encode_end,
1292 .capabilities = AV_CODEC_CAP_SLICE_THREADS,
1293 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
1294 .encode2 = vc2_encode_frame,
1295 .priv_class = &vc2enc_class,
1296 .defaults = vc2enc_defaults,
1297 .pix_fmts = allowed_pix_fmts