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 /* Quantizations above this usually zero coefficients and lower the quality */
33 #define MAX_QUANT_INDEX 50
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
39 /* Per slice quantization bit cost cache */
40 #define SLICE_CACHED_QUANTIZERS 30
42 /* Decides the cutoff point in # of slices to distribute the leftover bytes */
43 #define SLICE_REDIST_TOTAL 150
53 typedef struct SubBand {
60 typedef struct Plane {
61 SubBand band[MAX_DWT_LEVELS][4];
67 ptrdiff_t coef_stride;
70 typedef struct BitCostCache {
75 typedef struct SliceArgs {
77 BitCostCache cache[SLICE_CACHED_QUANTIZERS];
89 typedef struct TransformArgs {
95 VC2TransformContext t;
98 typedef struct VC2EncContext {
102 AVCodecContext *avctx;
103 DiracVersionInfo ver;
105 SliceArgs *slice_args;
106 TransformArgs transform_args[3];
108 /* For conversion from unsigned pixel values to signed */
113 uint32_t picture_number;
115 /* Base video format */
120 /* Quantization matrix */
121 uint8_t quant[MAX_DWT_LEVELS][4];
123 /* Coefficient LUT */
124 uint32_t *coef_lut_val;
125 uint8_t *coef_lut_len;
127 int num_x; /* #slices horizontally */
128 int num_y; /* #slices vertically */
134 /* Rate control stuff */
144 int strict_compliance;
148 enum VC2_QM quant_matrix;
150 /* Parse code state */
151 uint32_t next_parse_offset;
152 enum DiracParseCodes last_parse_code;
155 static av_always_inline void put_padding(PutBitContext *pb, int bytes)
165 put_bits(pb, bits, 0);
168 static av_always_inline void put_vc2_ue_uint(PutBitContext *pb, uint32_t val)
171 int pbits = 0, bits = 0, topbit = 1, maxval = 1;
178 while (val > maxval) {
184 bits = ff_log2(topbit);
186 for (i = 0; i < bits; i++) {
193 put_bits(pb, bits*2 + 1, (pbits << 1) | 1);
196 static av_always_inline int count_vc2_ue_uint(uint32_t val)
198 int topbit = 1, maxval = 1;
203 while (val > maxval) {
209 return ff_log2(topbit)*2 + 1;
212 static av_always_inline void get_vc2_ue_uint(int val, uint8_t *nbits,
216 int pbits = 0, bits = 0, topbit = 1, maxval = 1;
224 while (val > maxval) {
230 bits = ff_log2(topbit);
232 for (i = 0; i < bits; i++) {
240 *eval = (pbits << 1) | 1;
243 /* VC-2 10.4 - parse_info() */
244 static void encode_parse_info(VC2EncContext *s, enum DiracParseCodes pcode)
246 uint32_t cur_pos, dist;
248 avpriv_align_put_bits(&s->pb);
250 cur_pos = put_bits_count(&s->pb) >> 3;
253 avpriv_put_string(&s->pb, "BBCD", 0);
256 put_bits(&s->pb, 8, pcode);
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);
264 /* Last parse offset */
265 put_bits32(&s->pb, s->last_parse_code == DIRAC_PCODE_END_SEQ ? 13 : dist);
267 s->last_parse_code = pcode;
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)
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 */
284 /* VC-2 11.3 - frame_size() */
285 static void encode_frame_size(VC2EncContext *s)
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);
295 /* VC-2 11.3.3 - color_diff_sampling_format() */
296 static void encode_sample_fmt(VC2EncContext *s)
298 put_bits(&s->pb, 1, !s->strict_compliance);
299 if (!s->strict_compliance) {
301 if (s->chroma_x_shift == 1 && s->chroma_y_shift == 0)
303 else if (s->chroma_x_shift == 1 && s->chroma_y_shift == 1)
307 put_vc2_ue_uint(&s->pb, idx);
311 /* VC-2 11.3.4 - scan_format() */
312 static void encode_scan_format(VC2EncContext *s)
314 put_bits(&s->pb, 1, !s->strict_compliance);
315 if (!s->strict_compliance)
316 put_vc2_ue_uint(&s->pb, s->interlaced);
319 /* VC-2 11.3.5 - frame_rate() */
320 static void encode_frame_rate(VC2EncContext *s)
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);
331 /* VC-2 11.3.6 - aspect_ratio() */
332 static void encode_aspect_ratio(VC2EncContext *s)
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);
343 /* VC-2 11.3.7 - clean_area() */
344 static void encode_clean_area(VC2EncContext *s)
346 put_bits(&s->pb, 1, 0);
349 /* VC-2 11.3.8 - signal_range() */
350 static void encode_signal_range(VC2EncContext *s)
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) {
359 s->diff_offset = 128;
360 } else if (depth == 8 && (avctx->color_range == AVCOL_RANGE_MPEG ||
361 avctx->color_range == AVCOL_RANGE_UNSPECIFIED)) {
364 s->diff_offset = 128;
365 } else if (depth == 10) {
368 s->diff_offset = 512;
372 s->diff_offset = 2048;
374 put_bits(&s->pb, 1, !s->strict_compliance);
375 if (!s->strict_compliance)
376 put_vc2_ue_uint(&s->pb, idx);
379 /* VC-2 11.3.9 - color_spec() */
380 static void encode_color_spec(VC2EncContext *s)
382 AVCodecContext *avctx = s->avctx;
383 put_bits(&s->pb, 1, !s->strict_compliance);
384 if (!s->strict_compliance) {
386 put_vc2_ue_uint(&s->pb, 0);
389 put_bits(&s->pb, 1, 1);
390 if (avctx->color_primaries == AVCOL_PRI_BT470BG)
392 else if (avctx->color_primaries == AVCOL_PRI_SMPTE170M)
394 else if (avctx->color_primaries == AVCOL_PRI_SMPTE240M)
398 put_vc2_ue_uint(&s->pb, val);
401 put_bits(&s->pb, 1, 1);
402 if (avctx->colorspace == AVCOL_SPC_RGB)
404 else if (avctx->colorspace == AVCOL_SPC_YCOCG)
406 else if (avctx->colorspace == AVCOL_SPC_BT470BG)
410 put_vc2_ue_uint(&s->pb, val);
412 /* transfer function */
413 put_bits(&s->pb, 1, 1);
414 if (avctx->color_trc == AVCOL_TRC_LINEAR)
416 else if (avctx->color_trc == AVCOL_TRC_BT1361_ECG)
420 put_vc2_ue_uint(&s->pb, val);
424 /* VC-2 11.3 - source_parameters() */
425 static void encode_source_params(VC2EncContext *s)
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);
437 /* VC-2 11 - sequence_header() */
438 static void encode_seq_header(VC2EncContext *s)
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 */
447 /* VC-2 12.1 - picture_header() */
448 static void encode_picture_header(VC2EncContext *s)
450 avpriv_align_put_bits(&s->pb);
451 put_bits32(&s->pb, s->picture_number++);
454 /* VC-2 12.3.4.1 - slice_parameters() */
455 static void encode_slice_params(VC2EncContext *s)
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);
463 /* 1st idx = LL, second - vertical, third - horizontal, fourth - total */
464 const uint8_t vc2_qm_col_tab[][4] = {
472 const uint8_t vc2_qm_flat_tab[][4] = {
480 static void init_custom_qm(VC2EncContext *s)
482 int level, orientation;
484 if (s->quant_matrix == VC2_QM_DEF) {
485 for (level = 0; level < s->wavelet_depth; level++) {
486 for (orientation = 0; orientation < 4; orientation++) {
488 s->quant[level][orientation] = ff_dirac_default_qmat[s->wavelet_idx][level][orientation];
490 s->quant[level][orientation] = vc2_qm_col_tab[level][orientation];
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];
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];
508 /* VC-2 12.3.4.2 - quant_matrix() */
509 static void encode_quant_matrix(VC2EncContext *s)
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) {
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]);
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];
533 /* VC-2 12.3 - transform_parameters() */
534 static void encode_transform_params(VC2EncContext *s)
536 put_vc2_ue_uint(&s->pb, s->wavelet_idx);
537 put_vc2_ue_uint(&s->pb, s->wavelet_depth);
539 encode_slice_params(s);
540 encode_quant_matrix(s);
543 /* VC-2 12.2 - wavelet_transform() */
544 static void encode_wavelet_transform(VC2EncContext *s)
546 encode_transform_params(s);
547 avpriv_align_put_bits(&s->pb);
548 /* Continued after DWT in encode_transform_data() */
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 const uint8_t len = len_lut[c_abs];
588 put_bits(pb, len + 1, (val_lut[c_abs] << 1) | neg);
590 c_abs = QUANT(c_abs, qfactor);
591 put_vc2_ue_uint(pb, c_abs);
593 put_bits(pb, 1, neg);
600 static int count_hq_slice(VC2EncContext *s, BitCostCache *cache,
601 int *cached_results, int slice_x, int slice_y,
605 uint8_t quants[MAX_DWT_LEVELS][4];
606 int bits = 0, p, level, orientation;
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;
613 bits += 8*s->prefix_bytes;
614 bits += 8; /* quant_idx */
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);
620 for (p = 0; p < 3; p++) {
621 int bytes_start, bytes_len, pad_s, pad_c;
622 bytes_start = bits >> 3;
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];
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];
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;
637 dwtcoef *buf = b->buf + top * b->stride;
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);
646 c_abs = QUANT(c_abs, qfactor);
647 bits += count_vc2_ue_uint(c_abs);
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;
663 cache[*cached_results].quantizer = quant_idx;
664 cache[*cached_results].bits = bits;
665 *cached_results = FFMIN(*cached_results + 1, SLICE_CACHED_QUANTIZERS);
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)
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,
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,
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;
698 quant_buf[1] = quant_buf[0];
699 quant_buf[0] = quant;
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;
709 static void calc_slice_sizes(VC2EncContext *s)
711 int slice_x, slice_y;
712 SliceArgs *enc_args = s->slice_args;
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];
720 args->cached_results = 0;
721 args->bits_ceil = s->slice_max_bytes << 3;
722 args->bits_floor = s->slice_min_bytes << 3;
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,
731 /* VC-2 13.5.3 - hq_slice */
732 static int encode_hq_slice(AVCodecContext *avctx, void *arg)
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;
744 avpriv_align_put_bits(pb);
745 put_padding(pb, s->prefix_bytes);
746 put_bits(pb, 8, quant_idx);
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);
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;
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]);
765 avpriv_align_put_bits(pb);
766 bytes_len = (put_bits_count(pb) >> 3) - bytes_start - 1;
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;
772 pad_s = FFALIGN(bytes_len, s->size_scaler)/s->size_scaler;
773 pad_c = (pad_s*s->size_scaler) - bytes_len;
775 pb->buf[bytes_start] = pad_s;
776 put_padding(pb, pad_c);
782 /* VC-2 13.5.1 - low_delay_transform_data() */
783 static int encode_slices(VC2EncContext *s)
786 int i, slice_x, slice_y, skip = 0;
788 SliceArgs *enc_args = s->slice_args;
790 int bytes_top[SLICE_REDIST_TOTAL] = {0};
791 SliceArgs *top_loc[SLICE_REDIST_TOTAL] = {NULL};
793 avpriv_align_put_bits(&s->pb);
794 flush_put_bits(&s->pb);
795 buf = put_bits_ptr(&s->pb);
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;
813 for (i = 0; i < FFMIN(SLICE_REDIST_TOTAL, s->num_x*s->num_y); i++) {
815 int bits, bytes, diff, prev_bytes, new_idx;
818 if (!top_loc[i] || !top_loc[i]->quant_idx)
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;
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;
847 s->avctx->execute(s->avctx, encode_hq_slice, enc_args, NULL, s->num_x*s->num_y,
850 skip_put_bytes(&s->pb, skip);
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 |
865 * |----------------------------------|----------------------------------|
873 * |---------------------------------------------------------------------|
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
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
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
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.
890 static int dwt_plane(AVCodecContext *avctx, void *arg)
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;
903 int x, y, level, offset;
904 ptrdiff_t pix_stride = linesize >> (s->bpp - 1);
909 } else if (field == 2) {
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;
922 buf += p->coef_stride;
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;
931 buf += p->coef_stride;
936 memset(buf, 0, p->coef_stride * (p->dwt_height - p->height) * sizeof(dwtcoef));
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);
947 static void encode_frame(VC2EncContext *s, const AVFrame *frame,
948 const char *aux_data, int field)
952 /* Sequence header */
953 encode_parse_info(s, DIRAC_PCODE_SEQ_HEADER);
954 encode_seq_header(s);
956 /* Encoder version */
958 encode_parse_info(s, DIRAC_PCODE_AUX);
959 avpriv_put_string(&s->pb, aux_data, 1);
963 encode_parse_info(s, DIRAC_PCODE_PICTURE_HQ);
964 encode_picture_start(s);
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];
974 /* Do a DWT transform */
975 s->avctx->execute(s->avctx, dwt_plane, s->transform_args, NULL, 3,
976 sizeof(TransformArgs));
978 /* Calculate per-slice quantizers and sizes */
981 /* Init planes and encode slices */
985 encode_parse_info(s, DIRAC_PCODE_END_SEQ);
988 static av_cold int vc2_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
989 const AVFrame *frame, int *got_packet_ptr)
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);
1001 s->prefix_bytes = 0;
1002 s->last_parse_code = 0;
1003 s->next_parse_offset = 0;
1006 max_frame_bytes = (av_rescale(r_bitrate, s->avctx->time_base.num,
1007 s->avctx->time_base.den) >> 3) - header_size;
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;
1018 s->slice_min_bytes = s->slice_max_bytes - s->slice_max_bytes*(s->tolerance/100.0f);
1020 ret = ff_alloc_packet2(avctx, avpkt, max_frame_bytes*2, 0);
1022 av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
1025 init_put_bits(&s->pb, avpkt->data, avpkt->size);
1028 encode_frame(s, frame, aux_data, s->interlaced);
1030 encode_frame(s, frame, NULL, 2);
1032 flush_put_bits(&s->pb);
1033 avpkt->size = put_bits_count(&s->pb) >> 3;
1035 *got_packet_ptr = 1;
1040 static av_cold int vc2_encode_end(AVCodecContext *avctx)
1043 VC2EncContext *s = avctx->priv_data;
1045 av_log(avctx, AV_LOG_INFO, "Qavg: %i\n", s->q_avg);
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);
1052 av_freep(&s->slice_args);
1053 av_freep(&s->coef_lut_len);
1054 av_freep(&s->coef_lut_val);
1059 static int minimum_frame_bits(VC2EncContext *s)
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);
1071 static av_cold int vc2_encode_init(AVCodecContext *avctx)
1075 int i, j, level, o, shift;
1076 int64_t bits_per_frame, min_bits_per_frame;
1077 VC2EncContext *s = avctx->priv_data;
1079 s->picture_number = 0;
1081 /* Total allowed quantization range */
1082 s->q_ceil = MAX_QUANT_INDEX;
1090 s->strict_compliance = 1;
1093 s->slice_max_bytes = 0;
1094 s->slice_min_bytes = 0;
1096 /* Mark unknown as progressive */
1097 s->interlaced = !((avctx->field_order == AV_FIELD_UNKNOWN) ||
1098 (avctx->field_order == AV_FIELD_PROGRESSIVE));
1100 if (avctx->pix_fmt == AV_PIX_FMT_YUV422P10) {
1101 if (avctx->width == 1280 && avctx->height == 720) {
1103 if (avctx->time_base.num == 1001 && avctx->time_base.den == 60000)
1105 if (avctx->time_base.num == 1 && avctx->time_base.den == 50)
1107 } else if (avctx->width == 1920 && avctx->height == 1080) {
1109 if (s->interlaced) {
1110 if (avctx->time_base.num == 1001 && avctx->time_base.den == 30000)
1112 if (avctx->time_base.num == 1 && avctx->time_base.den == 50)
1115 if (avctx->time_base.num == 1001 && avctx->time_base.den == 60000)
1117 if (avctx->time_base.num == 1 && avctx->time_base.den == 50)
1119 if (avctx->time_base.num == 1001 && avctx->time_base.den == 24000)
1122 } else if (avctx->width == 3840 && avctx->height == 2160) {
1124 if (avctx->time_base.num == 1001 && avctx->time_base.den == 60000)
1126 if (avctx->time_base.num == 1 && avctx->time_base.den == 50)
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;
1137 av_log(avctx, AV_LOG_WARNING, "Interlacing enabled!\n");
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;
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;
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");
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;
1161 av_log(avctx, AV_LOG_INFO, "Selected base video format = %i\n", s->base_vf);
1164 avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_x_shift, &s->chroma_y_shift);
1166 /* Planes initialization */
1167 for (i = 0; i < 3; i++) {
1170 p->width = avctx->width >> (i ? s->chroma_x_shift : 0);
1171 p->height = avctx->height >> (i ? s->chroma_y_shift : 0);
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));
1180 for (level = s->wavelet_depth-1; level >= 0; level--) {
1183 for (o = 0; o < 4; o++) {
1184 b = &p->band[level][o];
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;
1194 if (ff_vc2enc_init_transforms(&s->transform_args[i].t,
1195 s->plane[i].coef_stride,
1196 s->plane[i].dwt_height))
1201 s->num_x = s->plane[0].dwt_width/s->slice_width;
1202 s->num_y = s->plane[0].dwt_height/s->slice_height;
1204 s->slice_args = av_malloc(s->num_x*s->num_y*sizeof(SliceArgs));
1209 s->coef_lut_len = av_malloc(COEF_LUT_TAB*s->q_ceil*sizeof(*s->coef_lut_len));
1210 if (!s->coef_lut_len)
1213 s->coef_lut_val = av_malloc(COEF_LUT_TAB*s->q_ceil*sizeof(*s->coef_lut_val));
1214 if (!s->coef_lut_val)
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]);
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);
1240 vc2_encode_end(avctx);
1241 av_log(avctx, AV_LOG_ERROR, "Unable to allocate memory!\n");
1242 return AVERROR(ENOMEM);
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"},
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
1271 static const AVCodecDefault vc2enc_defaults[] = {
1272 { "b", "600000000" },
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,
1283 AVCodec ff_vc2_encoder = {
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