FFmpeg
Loading...
Searching...
No Matches
vc2enc.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2016 Open Broadcast Systems Ltd.
3 * Author 2016 Rostislav Pehlivanov <atomnuker@gmail.com>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#include "libavutil/mem.h"
23#include "libavutil/pixdesc.h"
24#include "libavutil/opt.h"
25#include "libavutil/thread.h"
26#include "libavutil/version.h"
27#include "codec_internal.h"
28#include "dirac.h"
29#include "encode.h"
30#include "put_bits.h"
31#include "version.h"
32
33#include "vc2enc_dwt.h"
34#include "diractab.h"
35
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)
38
39/* Decides the cutoff point in # of slices to distribute the leftover bytes */
40#define SLICE_REDIST_TOTAL 150
41
49
51 { 0 }, /* Custom format, here just to make indexing equal to base_vf */
52 { AV_PIX_FMT_YUV420P, { 1001, 15000 }, 176, 120, 0, 1, "QSIF525" },
53 { AV_PIX_FMT_YUV420P, { 2, 25 }, 176, 144, 0, 1, "QCIF" },
54 { AV_PIX_FMT_YUV420P, { 1001, 15000 }, 352, 240, 0, 1, "SIF525" },
55 { AV_PIX_FMT_YUV420P, { 2, 25 }, 352, 288, 0, 1, "CIF" },
56 { AV_PIX_FMT_YUV420P, { 1001, 15000 }, 704, 480, 0, 1, "4SIF525" },
57 { AV_PIX_FMT_YUV420P, { 2, 25 }, 704, 576, 0, 1, "4CIF" },
58
59 { AV_PIX_FMT_YUV422P10, { 1001, 30000 }, 720, 480, 1, 2, "SD480I-60" },
60 { AV_PIX_FMT_YUV422P10, { 1, 25 }, 720, 576, 1, 2, "SD576I-50" },
61
62 { AV_PIX_FMT_YUV422P10, { 1001, 60000 }, 1280, 720, 0, 3, "HD720P-60" },
63 { AV_PIX_FMT_YUV422P10, { 1, 50 }, 1280, 720, 0, 3, "HD720P-50" },
64 { AV_PIX_FMT_YUV422P10, { 1001, 30000 }, 1920, 1080, 1, 3, "HD1080I-60" },
65 { AV_PIX_FMT_YUV422P10, { 1, 25 }, 1920, 1080, 1, 3, "HD1080I-50" },
66 { AV_PIX_FMT_YUV422P10, { 1001, 60000 }, 1920, 1080, 0, 3, "HD1080P-60" },
67 { AV_PIX_FMT_YUV422P10, { 1, 50 }, 1920, 1080, 0, 3, "HD1080P-50" },
68
69 { AV_PIX_FMT_YUV444P12, { 1, 24 }, 2048, 1080, 0, 4, "DC2K" },
70 { AV_PIX_FMT_YUV444P12, { 1, 24 }, 4096, 2160, 0, 5, "DC4K" },
71
72 { AV_PIX_FMT_YUV422P10, { 1001, 60000 }, 3840, 2160, 0, 6, "UHDTV 4K-60" },
73 { AV_PIX_FMT_YUV422P10, { 1, 50 }, 3840, 2160, 0, 6, "UHDTV 4K-50" },
74
75 { AV_PIX_FMT_YUV422P10, { 1001, 60000 }, 7680, 4320, 0, 7, "UHDTV 8K-60" },
76 { AV_PIX_FMT_YUV422P10, { 1, 50 }, 7680, 4320, 0, 7, "UHDTV 8K-50" },
77
78 { AV_PIX_FMT_YUV422P10, { 1001, 24000 }, 1920, 1080, 0, 3, "HD1080P-24" },
79 { AV_PIX_FMT_YUV422P10, { 1001, 30000 }, 720, 486, 1, 2, "SD Pro486" },
80};
82
90
91typedef struct SubBand {
93 ptrdiff_t stride;
94 int width;
95 int height;
96} SubBand;
97
98typedef struct Plane {
101 int width;
102 int height;
105 ptrdiff_t coef_stride;
106} Plane;
107
108typedef struct SliceArgs {
109 const struct VC2EncContext *ctx;
110 union {
112 uint8_t *buf;
113 };
114 int x;
115 int y;
119 int bytes;
120} SliceArgs;
121
122typedef struct TransformArgs {
123 const struct VC2EncContext *ctx;
125 const void *idata;
126 ptrdiff_t istride;
127 int field;
130
131typedef struct VC2EncContext {
137
140
141 /* For conversion from unsigned pixel values to signed */
143 int bpp;
145
146 /* Picture number */
148
149 /* Base video format */
151 int level;
153
154 /* Quantization matrix */
157
158 /* Division LUT */
159 uint32_t qmagic_lut[116][2];
160
161 int num_x; /* #slices horizontally */
162 int num_y; /* #slices vertically */
167
168 /* Rate control stuff */
173 int q_avg;
174
175 /* Options */
176 double tolerance;
184
185 /* Parse code state */
189
190/// x_k x_{k-1} ... x_0 -> 0 x_k 0 x_{k - 1} ... 0 x_0
191static uint16_t interleaved_ue_golomb_tab[256];
192/// 1 x_{k-1} ... x_0 -> 0 0 0 x_{k - 1} ... 0 x_0
193static uint16_t top_interleaved_ue_golomb_tab[256];
194/// 1 x_{k-1} ... x_0 -> 2 * k
195static uint8_t golomb_len_tab[256];
196/// quant -> av_log2(ff_dirac_qscale_tab[quant]) + 32
198
200{
202 for (unsigned i = 2; i < 256; ++i) {
203 golomb_len_tab[i] = golomb_len_tab[i >> 1] + 2;
206 }
207 for (size_t i = 0; i < FF_ARRAY_ELEMS(qscale_len_tab); ++i)
209}
210
212{
213 uint64_t pbits = 1;
214 int bits = 1;
215
216 ++val;
217
218 while (val >> 8) {
219 pbits |= (uint64_t)interleaved_ue_golomb_tab[val & 0xff] << bits;
220 val >>= 8;
221 bits += 16;
222 }
223 pbits |= (uint64_t)top_interleaved_ue_golomb_tab[val] << bits;
225
226 put_bits63(pb, bits, pbits);
227}
228
230{
232}
233
235{
236 return 2 * av_log2(val + 1) + 1;
237}
238
239/* VC-2 10.4 - parse_info() */
241{
242 uint32_t cur_pos, dist;
243
244 align_put_bits(&s->pb);
245
246 cur_pos = put_bytes_count(&s->pb, 0);
247
248 /* Magic string */
249 ff_put_string(&s->pb, "BBCD", 0);
250
251 /* Parse code */
252 put_bits(&s->pb, 8, pcode);
253
254 /* Next parse offset */
255 dist = cur_pos - s->next_parse_offset;
256 AV_WB32(s->pb.buf + s->next_parse_offset + 5, dist);
257 s->next_parse_offset = cur_pos;
258 put_bits32(&s->pb, pcode == DIRAC_PCODE_END_SEQ ? 13 : 0);
259
260 /* Last parse offset */
261 put_bits32(&s->pb, s->last_parse_code == DIRAC_PCODE_END_SEQ ? 13 : dist);
262
263 s->last_parse_code = pcode;
264}
265
266/* VC-2 11.1 - parse_parameters()
267 * The level dictates what the decoder should expect in terms of resolution
268 * and allows it to quickly reject whatever it can't support. Remember,
269 * this codec kinda targets cheapo FPGAs without much memory. Unfortunately
270 * it also limits us greatly in our choice of formats, hence the flag to disable
271 * strict_compliance */
273{
274 put_vc2_ue_uint(&s->pb, s->ver.major); /* VC-2 demands this to be 2 */
275 put_vc2_ue_uint(&s->pb, s->ver.minor); /* ^^ and this to be 0 */
276 put_vc2_ue_uint(&s->pb, s->profile); /* 3 to signal HQ profile */
277 put_vc2_ue_uint(&s->pb, s->level); /* 3 - 1080/720, 6 - 4K */
278}
279
280/* VC-2 11.3 - frame_size() */
282{
283 put_bits(&s->pb, 1, !s->strict_compliance);
284 if (!s->strict_compliance) {
285 AVCodecContext *avctx = s->avctx;
286 put_vc2_ue_uint(&s->pb, avctx->width);
287 put_vc2_ue_uint(&s->pb, avctx->height);
288 }
289}
290
291/* VC-2 11.3.3 - color_diff_sampling_format() */
293{
294 put_bits(&s->pb, 1, !s->strict_compliance);
295 if (!s->strict_compliance) {
296 int idx;
297 if (s->chroma_x_shift == 1 && s->chroma_y_shift == 0)
298 idx = 1; /* 422 */
299 else if (s->chroma_x_shift == 1 && s->chroma_y_shift == 1)
300 idx = 2; /* 420 */
301 else
302 idx = 0; /* 444 */
303 put_vc2_ue_uint(&s->pb, idx);
304 }
305}
306
307/* VC-2 11.3.4 - scan_format() */
309{
310 put_bits(&s->pb, 1, !s->strict_compliance);
311 if (!s->strict_compliance)
312 put_vc2_ue_uint(&s->pb, s->interlaced);
313}
314
315/* VC-2 11.3.5 - frame_rate() */
317{
318 put_bits(&s->pb, 1, !s->strict_compliance);
319 if (!s->strict_compliance) {
320 AVCodecContext *avctx = s->avctx;
321 put_vc2_ue_uint(&s->pb, 0);
322 put_vc2_ue_uint(&s->pb, avctx->time_base.den);
323 put_vc2_ue_uint(&s->pb, avctx->time_base.num);
324 }
325}
326
327/* VC-2 11.3.6 - aspect_ratio() */
329{
330 put_bits(&s->pb, 1, !s->strict_compliance);
331 if (!s->strict_compliance) {
332 AVCodecContext *avctx = s->avctx;
333 put_vc2_ue_uint(&s->pb, 0);
336 }
337}
338
339/* VC-2 11.3.7 - clean_area() */
341{
342 put_bits(&s->pb, 1, 0);
343}
344
345/* VC-2 11.3.8 - signal_range() */
347{
348 put_bits(&s->pb, 1, !s->strict_compliance);
349 if (!s->strict_compliance)
350 put_vc2_ue_uint(&s->pb, s->bpp_idx);
351}
352
353/* VC-2 11.3.9 - color_spec() */
355{
356 AVCodecContext *avctx = s->avctx;
357 put_bits(&s->pb, 1, !s->strict_compliance);
358 if (!s->strict_compliance) {
359 int val;
360 put_vc2_ue_uint(&s->pb, 0);
361
362 /* primaries */
363 put_bits(&s->pb, 1, 1);
365 val = 2;
366 else if (avctx->color_primaries == AVCOL_PRI_SMPTE170M)
367 val = 1;
368 else if (avctx->color_primaries == AVCOL_PRI_SMPTE240M)
369 val = 1;
370 else
371 val = 0;
372 put_vc2_ue_uint(&s->pb, val);
373
374 /* color matrix */
375 put_bits(&s->pb, 1, 1);
376 if (avctx->colorspace == AVCOL_SPC_RGB)
377 val = 3;
378 else if (avctx->colorspace == AVCOL_SPC_YCOCG)
379 val = 2;
380 else if (avctx->colorspace == AVCOL_SPC_BT470BG)
381 val = 1;
382 else
383 val = 0;
384 put_vc2_ue_uint(&s->pb, val);
385
386 /* transfer function */
387 put_bits(&s->pb, 1, 1);
388 if (avctx->color_trc == AVCOL_TRC_LINEAR)
389 val = 2;
390 else if (avctx->color_trc == AVCOL_TRC_BT1361_ECG)
391 val = 1;
392 else
393 val = 0;
394 put_vc2_ue_uint(&s->pb, val);
395 }
396}
397
398/* VC-2 11.3 - source_parameters() */
410
411/* VC-2 11 - sequence_header() */
413{
414 align_put_bits(&s->pb);
416 put_vc2_ue_uint(&s->pb, s->base_vf);
418 put_vc2_ue_uint(&s->pb, s->interlaced); /* Frames or fields coding */
419}
420
421/* VC-2 12.1 - picture_header() */
423{
424 align_put_bits(&s->pb);
425 put_bits32(&s->pb, s->picture_number++);
426}
427
428/* VC-2 12.3.4.1 - slice_parameters() */
430{
431 put_vc2_ue_uint(&s->pb, s->num_x);
432 put_vc2_ue_uint(&s->pb, s->num_y);
433 put_vc2_ue_uint(&s->pb, s->prefix_bytes);
434 put_vc2_ue_uint(&s->pb, s->size_scaler);
435}
436
437/* 1st idx = LL, second - vertical, third - horizontal, fourth - total */
438static const uint8_t vc2_qm_col_tab[][4] = {
439 {20, 9, 15, 4},
440 { 0, 6, 6, 4},
441 { 0, 3, 3, 5},
442 { 0, 3, 5, 1},
443 { 0, 11, 10, 11}
444};
445
446static const uint8_t vc2_qm_flat_tab[][4] = {
447 { 0, 0, 0, 0},
448 { 0, 0, 0, 0},
449 { 0, 0, 0, 0},
450 { 0, 0, 0, 0},
451 { 0, 0, 0, 0}
452};
453
455{
456 int level, orientation;
457
458 if (s->wavelet_depth <= 4 && s->quant_matrix == VC2_QM_DEF) {
459 s->custom_quant_matrix = 0;
460 for (level = 0; level < s->wavelet_depth; level++) {
461 s->quant[level][0] = ff_dirac_default_qmat[s->wavelet_idx][level][0];
462 s->quant[level][1] = ff_dirac_default_qmat[s->wavelet_idx][level][1];
463 s->quant[level][2] = ff_dirac_default_qmat[s->wavelet_idx][level][2];
464 s->quant[level][3] = ff_dirac_default_qmat[s->wavelet_idx][level][3];
465 }
466 return;
467 }
468
469 s->custom_quant_matrix = 1;
470
471 if (s->quant_matrix == VC2_QM_DEF) {
472 for (level = 0; level < s->wavelet_depth; level++) {
473 for (orientation = 0; orientation < 4; orientation++) {
474 if (level <= 3)
475 s->quant[level][orientation] = ff_dirac_default_qmat[s->wavelet_idx][level][orientation];
476 else
477 s->quant[level][orientation] = vc2_qm_col_tab[level][orientation];
478 }
479 }
480 } else if (s->quant_matrix == VC2_QM_COL) {
481 for (level = 0; level < s->wavelet_depth; level++) {
482 for (orientation = 0; orientation < 4; orientation++) {
483 s->quant[level][orientation] = vc2_qm_col_tab[level][orientation];
484 }
485 }
486 } else {
487 for (level = 0; level < s->wavelet_depth; level++) {
488 for (orientation = 0; orientation < 4; orientation++) {
489 s->quant[level][orientation] = vc2_qm_flat_tab[level][orientation];
490 }
491 }
492 }
493}
494
495/* VC-2 12.3.4.2 - quant_matrix() */
497{
498 int level;
499 put_bits(&s->pb, 1, s->custom_quant_matrix);
500 if (s->custom_quant_matrix) {
501 put_vc2_ue_uint(&s->pb, s->quant[0][0]);
502 for (level = 0; level < s->wavelet_depth; level++) {
503 put_vc2_ue_uint(&s->pb, s->quant[level][1]);
504 put_vc2_ue_uint(&s->pb, s->quant[level][2]);
505 put_vc2_ue_uint(&s->pb, s->quant[level][3]);
506 }
507 }
508}
509
510/* VC-2 12.3 - transform_parameters() */
512{
513 put_vc2_ue_uint(&s->pb, s->wavelet_idx);
514 put_vc2_ue_uint(&s->pb, s->wavelet_depth);
515
518}
519
520/* VC-2 12.2 - wavelet_transform() */
526
527/* VC-2 12 - picture_parse() */
535
536#define QUANT(c, mul, add, shift) (((mul) * (c) + (add)) >> (shift))
537
538/* VC-2 13.5.5.2 - slice_band() */
540 int sx, int sy, const SubBand *b, int quant)
541{
542 int x, y;
543
544 const int left = b->width * (sx+0) / s->num_x;
545 const int right = b->width * (sx+1) / s->num_x;
546 const int top = b->height * (sy+0) / s->num_y;
547 const int bottom = b->height * (sy+1) / s->num_y;
548
549 dwtcoef *coeff = b->buf + top * b->stride;
550 const uint64_t q_m = ((uint64_t)(s->qmagic_lut[quant][0])) << 2;
551 const uint64_t q_a = s->qmagic_lut[quant][1];
552 const int q_s = qscale_len_tab[quant];
553
554 for (y = top; y < bottom; y++) {
555 for (x = left; x < right; x++) {
556 uint32_t c_abs = QUANT(FFABS(coeff[x]), q_m, q_a, q_s);
557 put_vc2_ue_uint_inline(pb, c_abs);
558 if (c_abs)
559 put_bits(pb, 1, coeff[x] < 0);
560 }
561 coeff += b->stride;
562 }
563}
564
565static int count_hq_slice(SliceArgs *slice, int quant_idx)
566{
567 int x, y;
568 uint8_t quants[MAX_DWT_LEVELS][4];
569 int bits = 0, p, level, orientation;
570 const VC2EncContext *s = slice->ctx;
571
572 if (slice->cache[quant_idx])
573 return slice->cache[quant_idx];
574
575 bits += 8*s->prefix_bytes;
576 bits += 8; /* quant_idx */
577
578 for (level = 0; level < s->wavelet_depth; level++)
579 for (orientation = !!level; orientation < 4; orientation++)
580 quants[level][orientation] = FFMAX(quant_idx - s->quant[level][orientation], 0);
581
582 for (p = 0; p < 3; p++) {
583 int bytes_start, bytes_len, pad_s, pad_c;
584 bytes_start = bits >> 3;
585 bits += 8;
586 for (level = 0; level < s->wavelet_depth; level++) {
587 for (orientation = !!level; orientation < 4; orientation++) {
588 const SubBand *b = &s->plane[p].band[level][orientation];
589
590 const int q_idx = quants[level][orientation];
591 const uint64_t q_m = ((uint64_t)s->qmagic_lut[q_idx][0]) << 2;
592 const uint64_t q_a = s->qmagic_lut[q_idx][1];
593 const int q_s = qscale_len_tab[q_idx];
594
595 const int left = b->width * slice->x / s->num_x;
596 const int right = b->width *(slice->x+1) / s->num_x;
597 const int top = b->height * slice->y / s->num_y;
598 const int bottom = b->height *(slice->y+1) / s->num_y;
599
600 dwtcoef *buf = b->buf + top * b->stride;
601
602 for (y = top; y < bottom; y++) {
603 for (x = left; x < right; x++) {
604 uint32_t c_abs = QUANT(FFABS(buf[x]), q_m, q_a, q_s);
605 bits += count_vc2_ue_uint(c_abs);
606 bits += !!c_abs;
607 }
608 buf += b->stride;
609 }
610 }
611 }
612 bits += FFALIGN(bits, 8) - bits;
613 bytes_len = (bits >> 3) - bytes_start - 1;
614 pad_s = FFALIGN(bytes_len, s->size_scaler)/s->size_scaler;
615 pad_c = (pad_s*s->size_scaler) - bytes_len;
616 bits += pad_c*8;
617 }
618
619 slice->cache[quant_idx] = bits;
620
621 return bits;
622}
623
624/* Approaches the best possible quantizer asymptotically, its kinda exhaustive
625 * but we have a LUT to get the coefficient size in bits. Guaranteed to never
626 * overshoot, which is apparently very important when streaming */
627static int rate_control(AVCodecContext *avctx, void *arg)
628{
629 SliceArgs *slice_dat = arg;
630 const VC2EncContext *s = slice_dat->ctx;
631 const int top = slice_dat->bits_ceil;
632 const int bottom = slice_dat->bits_floor;
633 int quant_buf[2] = {-1, -1};
634 int quant = slice_dat->quant_idx, step = 1;
635 int bits_last, bits = count_hq_slice(slice_dat, quant);
636 while ((bits > top) || (bits < bottom)) {
637 const int signed_step = bits > top ? +step : -step;
638 quant = av_clip(quant + signed_step, 0, s->q_ceil-1);
639 bits = count_hq_slice(slice_dat, quant);
640 if (quant_buf[1] == quant) {
641 quant = FFMAX(quant_buf[0], quant);
642 bits = quant == quant_buf[0] ? bits_last : bits;
643 break;
644 }
645 step = av_clip(step/2, 1, (s->q_ceil-1)/2);
646 quant_buf[1] = quant_buf[0];
647 quant_buf[0] = quant;
648 bits_last = bits;
649 }
650 slice_dat->quant_idx = av_clip(quant, 0, s->q_ceil-1);
651 slice_dat->bytes = SSIZE_ROUND(bits >> 3);
652 return 0;
653}
654
656{
657 int i, j, slice_x, slice_y, bytes_left = 0;
658 int bytes_top[SLICE_REDIST_TOTAL] = {0};
659 int64_t total_bytes_needed = 0;
660 int slice_redist_range = FFMIN(SLICE_REDIST_TOTAL, s->num_x*s->num_y);
661 SliceArgs *enc_args = s->slice_args;
662 SliceArgs *top_loc[SLICE_REDIST_TOTAL] = {NULL};
663
665
666 for (slice_y = 0; slice_y < s->num_y; slice_y++) {
667 for (slice_x = 0; slice_x < s->num_x; slice_x++) {
668 SliceArgs *args = &enc_args[s->num_x*slice_y + slice_x];
669 args->ctx = s;
670 args->x = slice_x;
671 args->y = slice_y;
672 args->bits_ceil = s->slice_max_bytes << 3;
673 args->bits_floor = s->slice_min_bytes << 3;
674 memset(args->cache, 0, s->q_ceil*sizeof(*args->cache));
675 }
676 }
677
678 /* First pass - determine baseline slice sizes w.r.t. max_slice_size */
679 s->avctx->execute(s->avctx, rate_control, enc_args, NULL, s->num_x*s->num_y,
680 sizeof(SliceArgs));
681
682 for (i = 0; i < s->num_x*s->num_y; i++) {
683 SliceArgs *args = &enc_args[i];
684 bytes_left += args->bytes;
685 for (j = 0; j < slice_redist_range; j++) {
686 if (args->bytes > bytes_top[j]) {
687 bytes_top[j] = args->bytes;
688 top_loc[j] = args;
689 break;
690 }
691 }
692 }
693
694 bytes_left = s->frame_max_bytes - bytes_left;
695
696 /* Second pass - distribute leftover bytes */
697 while (bytes_left > 0) {
698 int distributed = 0;
699 for (i = 0; i < slice_redist_range; i++) {
700 SliceArgs *args;
701 int bits, bytes, diff, prev_bytes, new_idx;
702 if (bytes_left <= 0)
703 break;
704 if (!top_loc[i] || !top_loc[i]->quant_idx)
705 break;
706 args = top_loc[i];
707 prev_bytes = args->bytes;
708 new_idx = FFMAX(args->quant_idx - 1, 0);
709 bits = count_hq_slice(args, new_idx);
710 bytes = SSIZE_ROUND(bits >> 3);
711 diff = bytes - prev_bytes;
712 if ((bytes_left - diff) > 0) {
713 args->quant_idx = new_idx;
714 args->bytes = bytes;
715 bytes_left -= diff;
716 distributed++;
717 }
718 }
719 if (!distributed)
720 break;
721 }
722
723 for (i = 0; i < s->num_x*s->num_y; i++) {
724 SliceArgs *args = &enc_args[i];
725 total_bytes_needed += args->bytes;
726 s->q_avg = (s->q_avg + args->quant_idx)/2;
727 }
728
729 return total_bytes_needed;
730}
731
732/* VC-2 13.5.3 - hq_slice */
733static int encode_hq_slice(AVCodecContext *avctx, void *arg)
734{
735 const SliceArgs *slice_dat = arg;
736 const VC2EncContext *s = slice_dat->ctx;
737 PutBitContext pb0, *const pb = &pb0;
738 const int slice_x = slice_dat->x;
739 const int slice_y = slice_dat->y;
740 const int quant_idx = slice_dat->quant_idx;
741 const int slice_bytes_max = slice_dat->bytes;
742 uint8_t quants[MAX_DWT_LEVELS][4];
743 int p, level, orientation;
744
745 /* The reference decoder ignores it, and its typical length is 0 */
746 memset(slice_dat->buf, 0, s->prefix_bytes);
747
748 init_put_bits(pb, slice_dat->buf + s->prefix_bytes, slice_dat->bytes - s->prefix_bytes);
749
750 put_bits(pb, 8, quant_idx);
751
752 /* Slice quantization (slice_quantizers() in the specs) */
753 for (level = 0; level < s->wavelet_depth; level++)
754 for (orientation = !!level; orientation < 4; orientation++)
755 quants[level][orientation] = FFMAX(quant_idx - s->quant[level][orientation], 0);
756
757 /* Luma + 2 Chroma planes */
758 for (p = 0; p < 3; p++) {
759 int bytes_start, bytes_len, pad_s, pad_c;
760 bytes_start = put_bytes_count(pb, 0);
761 put_bits(pb, 8, 0);
762 for (level = 0; level < s->wavelet_depth; level++) {
763 for (orientation = !!level; orientation < 4; orientation++) {
764 encode_subband(s, pb, slice_x, slice_y,
765 &s->plane[p].band[level][orientation],
766 quants[level][orientation]);
767 }
768 }
769 flush_put_bits(pb);
770 bytes_len = put_bytes_output(pb) - bytes_start - 1;
771 if (p == 2) {
772 int len_diff = slice_bytes_max - put_bytes_output(pb);
773 pad_s = FFALIGN((bytes_len + len_diff), s->size_scaler)/s->size_scaler;
774 pad_c = (pad_s*s->size_scaler) - bytes_len;
775 } else {
776 pad_s = FFALIGN(bytes_len, s->size_scaler)/s->size_scaler;
777 pad_c = (pad_s*s->size_scaler) - bytes_len;
778 }
779 pb->buf[bytes_start] = pad_s;
780 /* vc2-reference uses that padding that decodes to '0' coeffs */
781 memset(put_bits_ptr(pb), 0xFF, pad_c);
782 skip_put_bytes(pb, pad_c);
783 }
784
785 return 0;
786}
787
788/* VC-2 13.5.1 - low_delay_transform_data() */
790{
791 uint8_t *buf;
792 int slice_x, slice_y, skip = 0;
793 SliceArgs *enc_args = s->slice_args;
794
795 flush_put_bits(&s->pb);
796 buf = put_bits_ptr(&s->pb);
797
798 for (slice_y = 0; slice_y < s->num_y; slice_y++) {
799 for (slice_x = 0; slice_x < s->num_x; slice_x++) {
800 SliceArgs *args = &enc_args[s->num_x*slice_y + slice_x];
801 args->buf = buf + skip;
802 skip += args->bytes;
803 }
804 }
805
806 s->avctx->execute(s->avctx, encode_hq_slice, enc_args, NULL, s->num_x*s->num_y,
807 sizeof(SliceArgs));
808
809 skip_put_bytes(&s->pb, skip);
810
811 return 0;
812}
813
814/*
815 * Transform basics for a 3 level transform
816 * |---------------------------------------------------------------------|
817 * | LL-0 | HL-0 | | |
818 * |--------|-------| HL-1 | |
819 * | LH-0 | HH-0 | | |
820 * |----------------|-----------------| HL-2 |
821 * | | | |
822 * | LH-1 | HH-1 | |
823 * | | | |
824 * |----------------------------------|----------------------------------|
825 * | | |
826 * | | |
827 * | | |
828 * | LH-2 | HH-2 |
829 * | | |
830 * | | |
831 * | | |
832 * |---------------------------------------------------------------------|
833 *
834 * DWT transforms are generally applied by splitting the image in two vertically
835 * and applying a low pass transform on the left part and a corresponding high
836 * pass transform on the right hand side. This is known as the horizontal filter
837 * stage.
838 * After that, the same operation is performed except the image is divided
839 * horizontally, with the high pass on the lower and the low pass on the higher
840 * side.
841 * Therefore, you're left with 4 subdivisions - known as low-low, low-high,
842 * high-low and high-high. They're referred to as orientations in the decoder
843 * and encoder.
844 *
845 * The LL (low-low) area contains the original image downsampled by the amount
846 * of levels. The rest of the areas can be thought as the details needed
847 * to restore the image perfectly to its original size.
848 */
849static int dwt_plane(AVCodecContext *avctx, void *arg)
850{
851 TransformArgs *transform_dat = arg;
852 const VC2EncContext *s = transform_dat->ctx;
853 const void *frame_data = transform_dat->idata;
854 const ptrdiff_t linesize = transform_dat->istride;
855 const int field = transform_dat->field;
856 const Plane *p = transform_dat->plane;
857 VC2TransformContext *t = &transform_dat->t;
858 dwtcoef *buf = p->coef_buf;
859 const int idx = s->wavelet_idx;
860 const int skip = 1 + s->interlaced;
861
862 int x, y, level, offset;
863 ptrdiff_t pix_stride = linesize >> (s->bpp - 1);
864
865 if (field == 1) {
866 offset = 0;
867 pix_stride <<= 1;
868 } else if (field == 2) {
869 offset = pix_stride;
870 pix_stride <<= 1;
871 } else {
872 offset = 0;
873 }
874
875 if (s->bpp == 1) {
876 const uint8_t *pix = (const uint8_t *)frame_data + offset;
877 for (y = 0; y < p->height*skip; y+=skip) {
878 for (x = 0; x < p->width; x++) {
879 buf[x] = pix[x] - s->diff_offset;
880 }
881 memset(&buf[x], 0, (p->coef_stride - p->width)*sizeof(dwtcoef));
882 buf += p->coef_stride;
883 pix += pix_stride;
884 }
885 } else {
886 const uint16_t *pix = (const uint16_t *)frame_data + offset;
887 for (y = 0; y < p->height*skip; y+=skip) {
888 for (x = 0; x < p->width; x++) {
889 buf[x] = pix[x] - s->diff_offset;
890 }
891 memset(&buf[x], 0, (p->coef_stride - p->width)*sizeof(dwtcoef));
892 buf += p->coef_stride;
893 pix += pix_stride;
894 }
895 }
896
897 memset(buf, 0, p->coef_stride * (p->dwt_height - p->height) * sizeof(dwtcoef));
898
899 for (level = s->wavelet_depth-1; level >= 0; level--) {
900 const SubBand *b = &p->band[level][0];
901 t->vc2_subband_dwt[idx](t, p->coef_buf, p->coef_stride,
902 b->width, b->height);
903 }
904
905 return 0;
906}
907
908static int encode_frame(VC2EncContext *s, AVPacket *avpkt, const AVFrame *frame,
909 const char *aux_data, const int header_size, int field)
910{
911 int i, ret;
912 int64_t max_frame_bytes;
913
914 /* Threaded DWT transform */
915 for (i = 0; i < 3; i++) {
916 s->transform_args[i].ctx = s;
917 s->transform_args[i].field = field;
918 s->transform_args[i].plane = &s->plane[i];
919 s->transform_args[i].idata = frame->data[i];
920 s->transform_args[i].istride = frame->linesize[i];
921 }
922 s->avctx->execute(s->avctx, dwt_plane, s->transform_args, NULL, 3,
923 sizeof(TransformArgs));
924
925 /* Calculate per-slice quantizers and sizes */
926 max_frame_bytes = header_size + calc_slice_sizes(s);
927
928 if (field < 2) {
929 ret = ff_get_encode_buffer(s->avctx, avpkt,
930 max_frame_bytes << s->interlaced, 0);
931 if (ret < 0)
932 return ret;
933 init_put_bits(&s->pb, avpkt->data, avpkt->size);
934 }
935
936 /* Sequence header */
939
940 /* Encoder version */
941 if (aux_data) {
943 ff_put_string(&s->pb, aux_data, 1);
944 }
945
946 /* Picture header */
949
950 /* Encode slices */
952
953 /* End sequence */
955
956 return 0;
957}
958
960 const AVFrame *frame, int *got_packet)
961{
962 int ret = 0;
963 int slice_ceil, sig_size = 256;
964 VC2EncContext *s = avctx->priv_data;
965 const int bitexact = avctx->flags & AV_CODEC_FLAG_BITEXACT;
966 const char *aux_data = bitexact ? "Lavc" : LIBAVCODEC_IDENT;
967 const int aux_data_size = bitexact ? sizeof("Lavc") : sizeof(LIBAVCODEC_IDENT);
968 const int header_size = 100 + aux_data_size;
969 int64_t r_bitrate = avctx->bit_rate >> (s->interlaced);
970
971 s->avctx = avctx;
972 s->size_scaler = 2;
973 s->prefix_bytes = 0;
974 s->last_parse_code = 0;
975 s->next_parse_offset = 0;
976
977 /* Rate control */
978 s->frame_max_bytes = (av_rescale(r_bitrate, s->avctx->time_base.num,
979 s->avctx->time_base.den) >> 3) - header_size;
980 s->slice_max_bytes = slice_ceil = av_rescale(s->frame_max_bytes, 1, s->num_x*s->num_y);
981
982 /* Find an appropriate size scaler */
983 while (sig_size > 255) {
984 int r_size = SSIZE_ROUND(s->slice_max_bytes);
985 if (r_size > slice_ceil) {
986 s->slice_max_bytes -= r_size - slice_ceil;
987 r_size = SSIZE_ROUND(s->slice_max_bytes);
988 }
989 sig_size = r_size/s->size_scaler; /* Signalled slize size */
990 s->size_scaler <<= 1;
991 }
992
993 s->slice_min_bytes = s->slice_max_bytes - s->slice_max_bytes*(s->tolerance/100.0f);
994 if (s->slice_min_bytes < 0 || s->slice_max_bytes > INT_MAX >> 3)
995 return AVERROR(EINVAL);
996
997 ret = encode_frame(s, avpkt, frame, aux_data, header_size, s->interlaced);
998 if (ret)
999 return ret;
1000 if (s->interlaced) {
1001 ret = encode_frame(s, avpkt, frame, aux_data, header_size, 2);
1002 if (ret)
1003 return ret;
1004 }
1005
1006 flush_put_bits(&s->pb);
1007 av_shrink_packet(avpkt, put_bytes_output(&s->pb));
1008
1009 *got_packet = 1;
1010
1011 return 0;
1012}
1013
1015{
1016 int i;
1017 VC2EncContext *s = avctx->priv_data;
1018
1019 av_log(avctx, AV_LOG_INFO, "Qavg: %i\n", s->q_avg);
1020
1021 for (i = 0; i < 3; i++) {
1022 ff_vc2enc_free_transforms(&s->transform_args[i].t);
1023 av_freep(&s->plane[i].coef_buf);
1024 }
1025
1026 av_freep(&s->slice_args);
1027
1028 return 0;
1029}
1030
1032{
1033 static AVOnce init_static_once = AV_ONCE_INIT;
1034 Plane *p;
1035 SubBand *b;
1036 int i, level, o, shift;
1037 const AVPixFmtDescriptor *pixdesc;
1038 int depth;
1039 VC2EncContext *s = avctx->priv_data;
1040
1041 s->picture_number = 0;
1042
1043 /* Total allowed quantization range */
1044 s->q_ceil = DIRAC_MAX_QUANT_INDEX;
1045
1046 s->ver.major = 2;
1047 s->ver.minor = 0;
1048 s->profile = 3;
1049 s->level = 3;
1050
1051 s->base_vf = -1;
1052 s->strict_compliance = 1;
1053
1054 s->q_avg = 0;
1055 s->slice_max_bytes = 0;
1056 s->slice_min_bytes = 0;
1057
1058 /* Mark unknown as progressive */
1059 s->interlaced = !((avctx->field_order == AV_FIELD_UNKNOWN) ||
1060 (avctx->field_order == AV_FIELD_PROGRESSIVE));
1061
1062 for (i = 0; i < base_video_fmts_len; i++) {
1063 const VC2BaseVideoFormat *fmt = &base_video_fmts[i];
1064 if (avctx->pix_fmt != fmt->pix_fmt)
1065 continue;
1066 if (avctx->time_base.num != fmt->time_base.num)
1067 continue;
1068 if (avctx->time_base.den != fmt->time_base.den)
1069 continue;
1070 if (avctx->width != fmt->width)
1071 continue;
1072 if (avctx->height != fmt->height)
1073 continue;
1074 if (s->interlaced != fmt->interlaced)
1075 continue;
1076 s->base_vf = i;
1077 s->level = base_video_fmts[i].level;
1078 break;
1079 }
1080
1081 if (s->interlaced)
1082 av_log(avctx, AV_LOG_WARNING, "Interlacing enabled!\n");
1083
1084 if ((s->slice_width & (s->slice_width - 1)) ||
1085 (s->slice_height & (s->slice_height - 1))) {
1086 av_log(avctx, AV_LOG_ERROR, "Slice size is not a power of two!\n");
1087 return AVERROR(EINVAL);
1088 }
1089
1090 if ((s->slice_width > avctx->width) ||
1091 (s->slice_height > avctx->height)) {
1092 av_log(avctx, AV_LOG_ERROR, "Slice size is bigger than the image!\n");
1093 return AVERROR(EINVAL);
1094 }
1095
1096 if (s->base_vf <= 0) {
1098 s->strict_compliance = s->base_vf = 0;
1099 av_log(avctx, AV_LOG_WARNING, "Format does not strictly comply with VC2 specs\n");
1100 } else {
1101 av_log(avctx, AV_LOG_WARNING, "Given format does not strictly comply with "
1102 "the specifications, decrease strictness to use it.\n");
1103 return AVERROR(EINVAL);
1104 }
1105 } else {
1106 av_log(avctx, AV_LOG_INFO, "Selected base video format = %i (%s)\n",
1107 s->base_vf, base_video_fmts[s->base_vf].name);
1108 }
1109
1110 pixdesc = av_pix_fmt_desc_get(avctx->pix_fmt);
1111 /* Chroma subsampling */
1112 s->chroma_x_shift = pixdesc->log2_chroma_w;
1113 s->chroma_y_shift = pixdesc->log2_chroma_h;
1114
1115 /* Bit depth and color range index */
1116 depth = pixdesc->comp[0].depth;
1117 if (depth == 8 && avctx->color_range == AVCOL_RANGE_JPEG) {
1118 s->bpp = 1;
1119 s->bpp_idx = 1;
1120 s->diff_offset = 128;
1121 } else if (depth == 8 && (avctx->color_range == AVCOL_RANGE_MPEG ||
1123 s->bpp = 1;
1124 s->bpp_idx = 2;
1125 s->diff_offset = 128;
1126 } else if (depth == 10) {
1127 s->bpp = 2;
1128 s->bpp_idx = 3;
1129 s->diff_offset = 512;
1130 } else {
1131 s->bpp = 2;
1132 s->bpp_idx = 4;
1133 s->diff_offset = 2048;
1134 }
1135
1136 /* Planes initialization */
1137 for (i = 0; i < 3; i++) {
1138 int w, h;
1139 p = &s->plane[i];
1140 p->width = avctx->width >> (i ? s->chroma_x_shift : 0);
1141 p->height = avctx->height >> (i ? s->chroma_y_shift : 0);
1142 if (s->interlaced)
1143 p->height >>= 1;
1144 p->dwt_width = w = FFALIGN(p->width, (1 << s->wavelet_depth));
1145 p->dwt_height = h = FFALIGN(p->height, (1 << s->wavelet_depth));
1146 p->coef_stride = FFALIGN(p->dwt_width, 32);
1147 p->coef_buf = av_mallocz(p->coef_stride*p->dwt_height*sizeof(dwtcoef));
1148 if (!p->coef_buf)
1149 return AVERROR(ENOMEM);
1150 for (level = s->wavelet_depth-1; level >= 0; level--) {
1151 w = w >> 1;
1152 h = h >> 1;
1153 for (o = 0; o < 4; o++) {
1154 b = &p->band[level][o];
1155 b->width = w;
1156 b->height = h;
1157 b->stride = p->coef_stride;
1158 shift = (o > 1)*b->height*b->stride + (o & 1)*b->width;
1159 b->buf = p->coef_buf + shift;
1160 }
1161 }
1162
1163 /* DWT init */
1164 if (ff_vc2enc_init_transforms(&s->transform_args[i].t,
1165 s->plane[i].coef_stride,
1166 s->plane[i].dwt_height,
1167 s->slice_width, s->slice_height))
1168 return AVERROR(ENOMEM);
1169 }
1170
1171 /* Slices */
1172 s->num_x = s->plane[0].dwt_width/s->slice_width;
1173 s->num_y = s->plane[0].dwt_height/s->slice_height;
1174
1175 s->slice_args = av_calloc(s->num_x*s->num_y, sizeof(SliceArgs));
1176 if (!s->slice_args)
1177 return AVERROR(ENOMEM);
1178
1179 for (i = 0; i < 116; i++) {
1180 const uint64_t qf = ff_dirac_qscale_tab[i];
1181 const uint32_t m = av_log2(qf);
1182 const uint32_t t = (1ULL << (m + 32)) / qf;
1183 const uint32_t r = (t*qf + qf) & UINT32_MAX;
1184 if (!(qf & (qf - 1))) {
1185 s->qmagic_lut[i][0] = 0xFFFFFFFF;
1186 s->qmagic_lut[i][1] = 0xFFFFFFFF;
1187 } else if (r <= 1 << m) {
1188 s->qmagic_lut[i][0] = t + 1;
1189 s->qmagic_lut[i][1] = 0;
1190 } else {
1191 s->qmagic_lut[i][0] = t;
1192 s->qmagic_lut[i][1] = t;
1193 }
1194 }
1195
1196 ff_thread_once(&init_static_once, vc2_init_static_data);
1197
1198 return 0;
1199}
1200
1201#define VC2ENC_FLAGS (AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
1202static const AVOption vc2enc_options[] = {
1203 {"tolerance", "Max undershoot in percent", offsetof(VC2EncContext, tolerance), AV_OPT_TYPE_DOUBLE, {.dbl = 5.0f}, 0.0f, 45.0f, VC2ENC_FLAGS, .unit = "tolerance"},
1204 {"slice_width", "Slice width", offsetof(VC2EncContext, slice_width), AV_OPT_TYPE_INT, {.i64 = 32}, 32, 1024, VC2ENC_FLAGS, .unit = "slice_width"},
1205 {"slice_height", "Slice height", offsetof(VC2EncContext, slice_height), AV_OPT_TYPE_INT, {.i64 = 16}, 8, 1024, VC2ENC_FLAGS, .unit = "slice_height"},
1206 {"wavelet_depth", "Transform depth", offsetof(VC2EncContext, wavelet_depth), AV_OPT_TYPE_INT, {.i64 = 4}, 1, 5, VC2ENC_FLAGS, .unit = "wavelet_depth"},
1207 {"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"},
1208 {"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"},
1209 {"5_3", "LeGall (5,3)", 0, AV_OPT_TYPE_CONST, {.i64 = VC2_TRANSFORM_5_3}, INT_MIN, INT_MAX, VC2ENC_FLAGS, .unit = "wavelet_idx"},
1210 {"haar", "Haar (with shift)", 0, AV_OPT_TYPE_CONST, {.i64 = VC2_TRANSFORM_HAAR_S}, INT_MIN, INT_MAX, VC2ENC_FLAGS, .unit = "wavelet_idx"},
1211 {"haar_noshift", "Haar (without shift)", 0, AV_OPT_TYPE_CONST, {.i64 = VC2_TRANSFORM_HAAR}, INT_MIN, INT_MAX, VC2ENC_FLAGS, .unit = "wavelet_idx"},
1212 {"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"},
1213 {"default", "Default from the specifications", 0, AV_OPT_TYPE_CONST, {.i64 = VC2_QM_DEF}, INT_MIN, INT_MAX, VC2ENC_FLAGS, .unit = "quant_matrix"},
1214 {"color", "Prevents low bitrate discoloration", 0, AV_OPT_TYPE_CONST, {.i64 = VC2_QM_COL}, INT_MIN, INT_MAX, VC2ENC_FLAGS, .unit = "quant_matrix"},
1215 {"flat", "Optimize for PSNR", 0, AV_OPT_TYPE_CONST, {.i64 = VC2_QM_FLAT}, INT_MIN, INT_MAX, VC2ENC_FLAGS, .unit = "quant_matrix"},
1216 {NULL}
1217};
1218
1219static const AVClass vc2enc_class = {
1220 .class_name = "SMPTE VC-2 encoder",
1221 .category = AV_CLASS_CATEGORY_ENCODER,
1222 .option = vc2enc_options,
1223 .item_name = av_default_item_name,
1224 .version = LIBAVUTIL_VERSION_INT
1225};
1226
1228 { "b", "600000000" },
1229 { NULL },
1230};
1231
1238
1240 .p.name = "vc2",
1241 CODEC_LONG_NAME("SMPTE VC-2"),
1242 .p.type = AVMEDIA_TYPE_VIDEO,
1243 .p.id = AV_CODEC_ID_DIRAC,
1244 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_SLICE_THREADS |
1246 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
1247 .priv_data_size = sizeof(VC2EncContext),
1251 .p.priv_class = &vc2enc_class,
1252 .defaults = vc2enc_defaults,
1254 .color_ranges = AVCOL_RANGE_MPEG | AVCOL_RANGE_JPEG,
1255};
static double val(void *priv, double ch)
Definition aeval.c:77
const FFCodec ff_vc2_encoder
Definition vc2enc.c:1239
static av_cold void close(AVCodecParserContext *s)
Definition apv_parser.c:197
void ff_put_string(PutBitContext *pb, const char *string, int terminate_string)
Put the string string in the bitstream.
Definition bitstream.c:39
static void BS_FUNC skip(BSCTX *bc, unsigned int n)
Skip n bits in the buffer.
static int BS_FUNC left(const BSCTX *bc)
Return the number of the bits left in a buffer.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define CODEC_PIXFMTS_ARRAY(array)
#define FF_CODEC_ENCODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
#define av_clip
Definition common.h:100
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition common.h:74
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
#define FF_COMPLIANCE_STRICT
Strictly conform to all the things in the spec no matter what consequences.
Definition defs.h:59
@ AV_FIELD_UNKNOWN
Definition defs.h:212
@ AV_FIELD_PROGRESSIVE
Definition defs.h:213
static AVFrame * frame
Interface to Dirac Decoder/Encoder.
DiracParseCodes
Parse code values:
Definition dirac.h:61
@ DIRAC_PCODE_AUX
Definition dirac.h:64
@ DIRAC_PCODE_END_SEQ
Definition dirac.h:63
@ DIRAC_PCODE_SEQ_HEADER
Definition dirac.h:62
@ DIRAC_PCODE_PICTURE_HQ
Definition dirac.h:69
#define MAX_DWT_LEVELS
The spec limits the number of wavelet decompositions to 4 for both level 1 (VC-2) and 128 (long-gop d...
Definition dirac.h:49
const int32_t ff_dirac_qscale_tab[116]
Definition diractab.c:34
const uint8_t ff_dirac_default_qmat[7][4][4]
Definition diractab.c:24
#define DIRAC_MAX_QUANT_INDEX
Definition diractab.h:41
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
Get a buffer for a packet.
Definition encode.c:106
static const uint8_t bits[8]
Definition fastaudio.c:100
FrameData * frame_data(AVFrame *frame)
Get our axiliary frame data attached to the frame, allocating it if needed.
Definition ffmpeg.c:491
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_DOUBLE
Underlying C type is double.
Definition opt.h:266
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
Definition codec.h:147
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition avcodec.h:322
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
#define AV_CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
Definition codec.h:102
@ AV_CODEC_ID_DIRAC
Definition codec_id.h:166
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
Definition packet.c:113
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_INFO
Standard information.
Definition log.h:221
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
#define r
Definition input.c:42
#define b
Definition input.c:43
#define av_log2
Definition intmath.h:84
#define AV_WB32(p, v)
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition j2kenc.c:154
unsigned offset
Definition libaomenc.c:763
static int shift(int a, int b)
Definition bonk.c:261
const char * arg
Definition jacosubdec.c:65
Libavcodec version macros.
#define LIBAVCODEC_IDENT
Definition version.h:43
#define av_always_inline
Definition attributes.h:72
#define av_noinline
Definition attributes.h:97
#define av_cold
Definition attributes.h:117
#define AVOnce
Definition thread.h:202
static int ff_thread_once(char *control, void(*routine)(void))
Definition thread.h:205
#define AV_ONCE_INIT
Definition thread.h:203
Libavutil version macros.
uint8_t w
Definition llvidencdsp.c:39
@ AV_CLASS_CATEGORY_ENCODER
Definition log.h:34
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
#define FFALIGN(x, a)
Definition macros.h:78
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:264
Memory handling functions.
enum AVPixelFormat pix
Definition ohcodec.c:55
AVOptions.
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3460
#define AV_PIX_FMT_YUV444P12
Definition pixfmt.h:552
#define AV_PIX_FMT_YUV420P10
Definition pixfmt.h:545
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition pixfmt.h:766
@ AVCOL_RANGE_UNSPECIFIED
Definition pixfmt.h:749
@ AVCOL_RANGE_JPEG
Full range content.
Definition pixfmt.h:783
#define AV_PIX_FMT_YUV420P12
Definition pixfmt.h:549
#define AV_PIX_FMT_YUV422P12
Definition pixfmt.h:550
#define AV_PIX_FMT_YUV422P10
Definition pixfmt.h:546
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition pixfmt.h:73
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition pixfmt.h:77
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition pixfmt.h:78
@ AVCOL_PRI_BT470BG
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM
Definition pixfmt.h:649
@ AVCOL_PRI_SMPTE240M
identical to above, also called "SMPTE C" even though it uses D65
Definition pixfmt.h:651
@ AVCOL_PRI_SMPTE170M
also ITU-R BT601-6 525 / ITU-R BT1358 525 / ITU-R BT1700 NTSC
Definition pixfmt.h:650
@ AVCOL_TRC_BT1361_ECG
ITU-R BT1361 Extended Colour Gamut.
Definition pixfmt.h:685
@ AVCOL_TRC_LINEAR
"Linear transfer characteristics"
Definition pixfmt.h:681
#define AV_PIX_FMT_YUV444P10
Definition pixfmt.h:548
@ AVCOL_SPC_BT470BG
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM / IEC 61966-2-4 xvYCC601
Definition pixfmt.h:712
@ AVCOL_SPC_YCOCG
Definition pixfmt.h:716
@ AVCOL_SPC_RGB
order of coefficients is actually GBR, also IEC 61966-2-1 (sRGB), YZX and ST 428-1
Definition pixfmt.h:707
bitstream writer API
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition put_bits.h:62
static uint8_t * put_bits_ptr(PutBitContext *s)
Return the pointer to the byte where the bitstream writer will put the next bit.
Definition put_bits.h:402
static int put_bytes_count(const PutBitContext *s, int round_up)
Definition put_bits.h:110
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition put_bits.h:153
static void put_bits63(PutBitContext *s, int n, uint64_t value)
Write up to 63 bits into a bitstream.
Definition put_bits.h:344
static int put_bytes_output(const PutBitContext *s)
Definition put_bits.h:99
static void skip_put_bytes(PutBitContext *s, int n)
Skip the given number of bytes.
Definition put_bits.h:411
static av_unused void put_bits32(PutBitContext *s, uint32_t value)
Write exactly 32 bits into a bitstream.
Definition put_bits.h:301
static void align_put_bits(PutBitContext *s)
Pad the bitstream with zeros up to the next byte boundary.
Definition put_bits.h:445
#define FF_ARRAY_ELEMS(a)
Describe the class of an AVClass context structure.
Definition log.h:76
main external API structure.
Definition avcodec.h:443
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition avcodec.h:643
int width
picture width / height.
Definition avcodec.h:604
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition avcodec.h:681
int strict_std_compliance
strictly follow the standard (MPEG-4, ...).
Definition avcodec.h:1375
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition avcodec.h:657
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel.
Definition avcodec.h:628
enum AVFieldOrder field_order
Field order.
Definition avcodec.h:694
int64_t bit_rate
the average bitrate
Definition avcodec.h:493
enum AVColorSpace colorspace
YUV colorspace type.
Definition avcodec.h:671
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition avcodec.h:664
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition avcodec.h:547
int flags
AV_CODEC_FLAG_*.
Definition avcodec.h:500
void * priv_data
Definition avcodec.h:470
int depth
Number of bits in the component.
Definition pixdesc.h:57
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
AVOption.
Definition opt.h:428
This structure stores compressed data.
Definition packet.h:580
int size
Definition packet.h:604
uint8_t * data
Definition packet.h:603
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition pixdesc.h:69
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition pixdesc.h:105
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition pixdesc.h:80
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition pixdesc.h:89
Rational number (pair of numerator and denominator).
Definition rational.h:58
int num
Numerator.
Definition rational.h:59
int den
Denominator.
Definition rational.h:60
Definition cfhd.h:125
SubBand band[DWT_LEVELS_3D][4]
Definition cfhd.h:138
ptrdiff_t coef_stride
Definition vc2enc.c:105
int width
Definition cfhd.h:126
int height
Definition cfhd.h:127
dwtcoef * coef_buf
Definition vc2enc.c:100
int dwt_height
Definition vc2enc.c:104
int dwt_width
Definition vc2enc.c:103
uint8_t * buf
Definition put_bits.h:53
uint8_t * buf
Definition vc2enc.c:112
int x
Definition vc2enc.c:114
int quant_idx
Definition vc2enc.c:116
int bits_ceil
Definition vc2enc.c:117
int bits_floor
Definition vc2enc.c:118
int cache[DIRAC_MAX_QUANT_INDEX]
Definition vc2enc.c:111
int y
Definition vc2enc.c:115
const struct VC2EncContext * ctx
Definition vc2enc.c:109
int bytes
Definition vc2enc.c:119
DWTELEM * buf
Definition snow.h:88
ptrdiff_t stride
Definition cfhd.h:117
int width
Definition cfhd.h:119
int height
Definition cfhd.h:121
ptrdiff_t istride
Definition vc2enc.c:126
const struct VC2EncContext * ctx
Definition vc2enc.c:123
const void * idata
Definition vc2enc.c:125
VC2TransformContext t
Definition vc2enc.c:128
Plane * plane
Definition vc2enc.c:124
enum AVPixelFormat pix_fmt
Definition vc2enc.c:43
uint8_t interlaced
Definition vc2enc.c:46
char name[13]
Definition vc2enc.c:47
uint8_t level
Definition vc2enc.c:46
AVRational time_base
Definition vc2enc.c:44
int frame_max_bytes
Definition vc2enc.c:169
int slice_max_bytes
Definition vc2enc.c:170
int wavelet_idx
Definition vc2enc.c:177
TransformArgs transform_args[3]
Definition vc2enc.c:139
int prefix_bytes
Definition vc2enc.c:163
Plane plane[3]
Definition vc2enc.c:134
int chroma_y_shift
Definition vc2enc.c:166
double tolerance
Definition vc2enc.c:176
uint8_t quant[MAX_DWT_LEVELS][4]
Definition vc2enc.c:155
enum VC2_QM quant_matrix
Definition vc2enc.c:183
AVCodecContext * avctx
Definition vc2enc.c:135
int size_scaler
Definition vc2enc.c:164
SliceArgs * slice_args
Definition vc2enc.c:138
int custom_quant_matrix
Definition vc2enc.c:156
uint32_t next_parse_offset
Definition vc2enc.c:186
uint32_t picture_number
Definition vc2enc.c:147
int wavelet_depth
Definition vc2enc.c:178
DiracVersionInfo ver
Definition vc2enc.c:136
int diff_offset
Definition vc2enc.c:142
AVClass * av_class
Definition vc2enc.c:132
int strict_compliance
Definition vc2enc.c:179
int interlaced
Definition vc2enc.c:182
int chroma_x_shift
Definition vc2enc.c:165
int slice_height
Definition vc2enc.c:180
uint32_t qmagic_lut[116][2]
Definition vc2enc.c:159
int slice_min_bytes
Definition vc2enc.c:171
int slice_width
Definition vc2enc.c:181
enum DiracParseCodes last_parse_code
Definition vc2enc.c:187
PutBitContext pb
Definition vc2enc.c:133
void(* vc2_subband_dwt[VC2_TRANSFORMS_NB])(struct VC2TransformContext *t, dwtcoef *data, ptrdiff_t stride, int width, int height)
Definition vc2enc_dwt.h:45
uint8_t level
Definition svq3.c:208
#define av_mallocz(s)
#define av_freep(p)
#define av_log(a,...)
static void encode_parse_info(VC2EncContext *s, enum DiracParseCodes pcode)
Definition vc2enc.c:240
static int rate_control(AVCodecContext *avctx, void *arg)
Definition vc2enc.c:627
static void encode_frame_rate(VC2EncContext *s)
Definition vc2enc.c:316
static int calc_slice_sizes(VC2EncContext *s)
Definition vc2enc.c:655
static void encode_source_params(VC2EncContext *s)
Definition vc2enc.c:399
static void encode_frame_size(VC2EncContext *s)
Definition vc2enc.c:281
static void encode_transform_params(VC2EncContext *s)
Definition vc2enc.c:511
static const AVOption vc2enc_options[]
Definition vc2enc.c:1202
static void encode_signal_range(VC2EncContext *s)
Definition vc2enc.c:346
static av_cold int vc2_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet)
Definition vc2enc.c:959
static void encode_seq_header(VC2EncContext *s)
Definition vc2enc.c:412
static void encode_quant_matrix(VC2EncContext *s)
Definition vc2enc.c:496
static const FFCodecDefault vc2enc_defaults[]
Definition vc2enc.c:1227
static av_cold int vc2_encode_init(AVCodecContext *avctx)
Definition vc2enc.c:1031
static av_always_inline void put_vc2_ue_uint_inline(PutBitContext *pb, uint32_t val)
Definition vc2enc.c:211
VC2_QM
Definition vc2enc.c:83
@ VC2_QM_COL
Definition vc2enc.c:85
@ VC2_QM_FLAT
Definition vc2enc.c:86
@ VC2_QM_NB
Definition vc2enc.c:88
@ VC2_QM_DEF
Definition vc2enc.c:84
static void encode_color_spec(VC2EncContext *s)
Definition vc2enc.c:354
static const int base_video_fmts_len
Definition vc2enc.c:81
static av_always_inline int count_vc2_ue_uint(uint32_t val)
Definition vc2enc.c:234
static void encode_picture_header(VC2EncContext *s)
Definition vc2enc.c:422
static int encode_slices(VC2EncContext *s)
Definition vc2enc.c:789
static void encode_slice_params(VC2EncContext *s)
Definition vc2enc.c:429
static uint16_t top_interleaved_ue_golomb_tab[256]
1 x_{k-1} ... x_0 -> 0 0 0 x_{k - 1} ... 0 x_0
Definition vc2enc.c:193
static av_noinline void put_vc2_ue_uint(PutBitContext *pb, uint32_t val)
Definition vc2enc.c:229
#define VC2ENC_FLAGS
Definition vc2enc.c:1201
static void encode_picture_start(VC2EncContext *s)
Definition vc2enc.c:528
static av_cold int vc2_encode_end(AVCodecContext *avctx)
Definition vc2enc.c:1014
static void encode_wavelet_transform(VC2EncContext *s)
Definition vc2enc.c:521
static void encode_parse_params(VC2EncContext *s)
Definition vc2enc.c:272
#define SLICE_REDIST_TOTAL
Definition vc2enc.c:40
#define QUANT(c, mul, add, shift)
Definition vc2enc.c:536
static uint8_t golomb_len_tab[256]
1 x_{k-1} ... x_0 -> 2 * k
Definition vc2enc.c:195
static uint8_t qscale_len_tab[FF_ARRAY_ELEMS(ff_dirac_qscale_tab)]
quant -> av_log2(ff_dirac_qscale_tab[quant]) + 32
Definition vc2enc.c:197
#define SSIZE_ROUND(b)
Definition vc2enc.c:37
static int encode_hq_slice(AVCodecContext *avctx, void *arg)
Definition vc2enc.c:733
static void init_quant_matrix(VC2EncContext *s)
Definition vc2enc.c:454
static const VC2BaseVideoFormat base_video_fmts[]
Definition vc2enc.c:50
static int encode_frame(VC2EncContext *s, AVPacket *avpkt, const AVFrame *frame, const char *aux_data, const int header_size, int field)
Definition vc2enc.c:908
static void encode_subband(const VC2EncContext *s, PutBitContext *pb, int sx, int sy, const SubBand *b, int quant)
Definition vc2enc.c:539
static void encode_aspect_ratio(VC2EncContext *s)
Definition vc2enc.c:328
static const uint8_t vc2_qm_col_tab[][4]
Definition vc2enc.c:438
static void encode_scan_format(VC2EncContext *s)
Definition vc2enc.c:308
static const AVClass vc2enc_class
Definition vc2enc.c:1219
static uint16_t interleaved_ue_golomb_tab[256]
x_k x_{k-1} ... x_0 -> 0 x_k 0 x_{k - 1} ... 0 x_0
Definition vc2enc.c:191
static enum AVPixelFormat allowed_pix_fmts[]
Definition vc2enc.c:1232
static void encode_clean_area(VC2EncContext *s)
Definition vc2enc.c:340
static int count_hq_slice(SliceArgs *slice, int quant_idx)
Definition vc2enc.c:565
static av_cold void vc2_init_static_data(void)
Definition vc2enc.c:199
static const uint8_t vc2_qm_flat_tab[][4]
Definition vc2enc.c:446
static void encode_sample_fmt(VC2EncContext *s)
Definition vc2enc.c:292
static int dwt_plane(AVCodecContext *avctx, void *arg)
Definition vc2enc.c:849
av_cold int ff_vc2enc_init_transforms(VC2TransformContext *s, int p_stride, int p_height, int slice_w, int slice_h)
Definition vc2enc_dwt.c:257
av_cold void ff_vc2enc_free_transforms(VC2TransformContext *s)
Definition vc2enc_dwt.c:276
@ VC2_TRANSFORM_HAAR
Definition vc2enc_dwt.h:34
@ VC2_TRANSFORM_5_3
Definition vc2enc_dwt.h:32
@ VC2_TRANSFORMS_NB
Definition vc2enc_dwt.h:39
@ VC2_TRANSFORM_9_7
Definition vc2enc_dwt.h:31
@ VC2_TRANSFORM_HAAR_S
Definition vc2enc_dwt.h:35
int32_t dwtcoef
Definition vc2enc_dwt.h:28
static const double coeff[2][5]
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
static const uint8_t quant[64]
Definition vmixdec.c:71