FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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/pixdesc.h"
23 #include "libavutil/opt.h"
24 #include "dirac.h"
25 #include "put_bits.h"
26 #include "internal.h"
27 #include "version.h"
28 
29 #include "vc2enc_dwt.h"
30 #include "diractab.h"
31 
32 /* 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
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 
42 typedef struct VC2BaseVideoFormat {
46  const char *name;
48 
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" },
57 
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" },
60 
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" },
67 
68  { AV_PIX_FMT_YUV444P12, { 1, 24 }, 2048, 1080, 0, 4, "DC2K" },
69  { AV_PIX_FMT_YUV444P12, { 1, 24 }, 4096, 2160, 0, 5, "DC4K" },
70 
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" },
73 
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" },
76 
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" },
79 };
80 static const int base_video_fmts_len = FF_ARRAY_ELEMS(base_video_fmts);
81 
82 enum VC2_QM {
86 
88 };
89 
90 typedef struct SubBand {
92  ptrdiff_t stride;
93  int width;
94  int height;
95 } SubBand;
96 
97 typedef struct Plane {
100  int width;
101  int height;
104  ptrdiff_t coef_stride;
105 } Plane;
106 
107 typedef struct SliceArgs {
110  void *ctx;
111  int x;
112  int y;
116  int bytes;
117 } SliceArgs;
118 
119 typedef struct TransformArgs {
120  void *ctx;
122  void *idata;
123  ptrdiff_t istride;
124  int field;
126 } TransformArgs;
127 
128 typedef struct VC2EncContext {
134 
137 
138  /* For conversion from unsigned pixel values to signed */
140  int bpp;
141  int bpp_idx;
142 
143  /* Picture number */
144  uint32_t picture_number;
145 
146  /* Base video format */
147  int base_vf;
148  int level;
149  int profile;
150 
151  /* Quantization matrix */
154 
155  /* Coefficient LUT */
156  uint32_t *coef_lut_val;
158 
159  int num_x; /* #slices horizontally */
160  int num_y; /* #slices vertically */
165 
166  /* Rate control stuff */
169  int q_ceil;
170  int q_avg;
171 
172  /* Options */
173  double tolerance;
181 
182  /* Parse code state */
185 } VC2EncContext;
186 
188 {
189  int i;
190  int pbits = 0, bits = 0, topbit = 1, maxval = 1;
191 
192  if (!val++) {
193  put_bits(pb, 1, 1);
194  return;
195  }
196 
197  while (val > maxval) {
198  topbit <<= 1;
199  maxval <<= 1;
200  maxval |= 1;
201  }
202 
203  bits = ff_log2(topbit);
204 
205  for (i = 0; i < bits; i++) {
206  topbit >>= 1;
207  pbits <<= 2;
208  if (val & topbit)
209  pbits |= 0x1;
210  }
211 
212  put_bits(pb, bits*2 + 1, (pbits << 1) | 1);
213 }
214 
216 {
217  int topbit = 1, maxval = 1;
218 
219  if (!val++)
220  return 1;
221 
222  while (val > maxval) {
223  topbit <<= 1;
224  maxval <<= 1;
225  maxval |= 1;
226  }
227 
228  return ff_log2(topbit)*2 + 1;
229 }
230 
231 static av_always_inline void get_vc2_ue_uint(int val, uint8_t *nbits,
232  uint32_t *eval)
233 {
234  int i;
235  int pbits = 0, bits = 0, topbit = 1, maxval = 1;
236 
237  if (!val++) {
238  *nbits = 1;
239  *eval = 1;
240  return;
241  }
242 
243  while (val > maxval) {
244  topbit <<= 1;
245  maxval <<= 1;
246  maxval |= 1;
247  }
248 
249  bits = ff_log2(topbit);
250 
251  for (i = 0; i < bits; i++) {
252  topbit >>= 1;
253  pbits <<= 2;
254  if (val & topbit)
255  pbits |= 0x1;
256  }
257 
258  *nbits = bits*2 + 1;
259  *eval = (pbits << 1) | 1;
260 }
261 
262 /* VC-2 10.4 - parse_info() */
264 {
265  uint32_t cur_pos, dist;
266 
268 
269  cur_pos = put_bits_count(&s->pb) >> 3;
270 
271  /* Magic string */
272  avpriv_put_string(&s->pb, "BBCD", 0);
273 
274  /* Parse code */
275  put_bits(&s->pb, 8, pcode);
276 
277  /* Next parse offset */
278  dist = cur_pos - s->next_parse_offset;
279  AV_WB32(s->pb.buf + s->next_parse_offset + 5, dist);
280  s->next_parse_offset = cur_pos;
281  put_bits32(&s->pb, pcode == DIRAC_PCODE_END_SEQ ? 13 : 0);
282 
283  /* Last parse offset */
284  put_bits32(&s->pb, s->last_parse_code == DIRAC_PCODE_END_SEQ ? 13 : dist);
285 
286  s->last_parse_code = pcode;
287 }
288 
289 /* VC-2 11.1 - parse_parameters()
290  * The level dictates what the decoder should expect in terms of resolution
291  * and allows it to quickly reject whatever it can't support. Remember,
292  * this codec kinda targets cheapo FPGAs without much memory. Unfortunately
293  * it also limits us greatly in our choice of formats, hence the flag to disable
294  * strict_compliance */
296 {
297  put_vc2_ue_uint(&s->pb, s->ver.major); /* VC-2 demands this to be 2 */
298  put_vc2_ue_uint(&s->pb, s->ver.minor); /* ^^ and this to be 0 */
299  put_vc2_ue_uint(&s->pb, s->profile); /* 3 to signal HQ profile */
300  put_vc2_ue_uint(&s->pb, s->level); /* 3 - 1080/720, 6 - 4K */
301 }
302 
303 /* VC-2 11.3 - frame_size() */
305 {
306  put_bits(&s->pb, 1, !s->strict_compliance);
307  if (!s->strict_compliance) {
308  AVCodecContext *avctx = s->avctx;
309  put_vc2_ue_uint(&s->pb, avctx->width);
310  put_vc2_ue_uint(&s->pb, avctx->height);
311  }
312 }
313 
314 /* VC-2 11.3.3 - color_diff_sampling_format() */
316 {
317  put_bits(&s->pb, 1, !s->strict_compliance);
318  if (!s->strict_compliance) {
319  int idx;
320  if (s->chroma_x_shift == 1 && s->chroma_y_shift == 0)
321  idx = 1; /* 422 */
322  else if (s->chroma_x_shift == 1 && s->chroma_y_shift == 1)
323  idx = 2; /* 420 */
324  else
325  idx = 0; /* 444 */
326  put_vc2_ue_uint(&s->pb, idx);
327  }
328 }
329 
330 /* VC-2 11.3.4 - scan_format() */
332 {
333  put_bits(&s->pb, 1, !s->strict_compliance);
334  if (!s->strict_compliance)
335  put_vc2_ue_uint(&s->pb, s->interlaced);
336 }
337 
338 /* VC-2 11.3.5 - frame_rate() */
340 {
341  put_bits(&s->pb, 1, !s->strict_compliance);
342  if (!s->strict_compliance) {
343  AVCodecContext *avctx = s->avctx;
344  put_vc2_ue_uint(&s->pb, 0);
345  put_vc2_ue_uint(&s->pb, avctx->time_base.den);
346  put_vc2_ue_uint(&s->pb, avctx->time_base.num);
347  }
348 }
349 
350 /* VC-2 11.3.6 - aspect_ratio() */
352 {
353  put_bits(&s->pb, 1, !s->strict_compliance);
354  if (!s->strict_compliance) {
355  AVCodecContext *avctx = s->avctx;
356  put_vc2_ue_uint(&s->pb, 0);
359  }
360 }
361 
362 /* VC-2 11.3.7 - clean_area() */
364 {
365  put_bits(&s->pb, 1, 0);
366 }
367 
368 /* VC-2 11.3.8 - signal_range() */
370 {
371  put_bits(&s->pb, 1, !s->strict_compliance);
372  if (!s->strict_compliance)
373  put_vc2_ue_uint(&s->pb, s->bpp_idx);
374 }
375 
376 /* VC-2 11.3.9 - color_spec() */
378 {
379  AVCodecContext *avctx = s->avctx;
380  put_bits(&s->pb, 1, !s->strict_compliance);
381  if (!s->strict_compliance) {
382  int val;
383  put_vc2_ue_uint(&s->pb, 0);
384 
385  /* primaries */
386  put_bits(&s->pb, 1, 1);
387  if (avctx->color_primaries == AVCOL_PRI_BT470BG)
388  val = 2;
389  else if (avctx->color_primaries == AVCOL_PRI_SMPTE170M)
390  val = 1;
391  else if (avctx->color_primaries == AVCOL_PRI_SMPTE240M)
392  val = 1;
393  else
394  val = 0;
395  put_vc2_ue_uint(&s->pb, val);
396 
397  /* color matrix */
398  put_bits(&s->pb, 1, 1);
399  if (avctx->colorspace == AVCOL_SPC_RGB)
400  val = 3;
401  else if (avctx->colorspace == AVCOL_SPC_YCOCG)
402  val = 2;
403  else if (avctx->colorspace == AVCOL_SPC_BT470BG)
404  val = 1;
405  else
406  val = 0;
407  put_vc2_ue_uint(&s->pb, val);
408 
409  /* transfer function */
410  put_bits(&s->pb, 1, 1);
411  if (avctx->color_trc == AVCOL_TRC_LINEAR)
412  val = 2;
413  else if (avctx->color_trc == AVCOL_TRC_BT1361_ECG)
414  val = 1;
415  else
416  val = 0;
417  put_vc2_ue_uint(&s->pb, val);
418  }
419 }
420 
421 /* VC-2 11.3 - source_parameters() */
423 {
432 }
433 
434 /* VC-2 11 - sequence_header() */
436 {
439  put_vc2_ue_uint(&s->pb, s->base_vf);
441  put_vc2_ue_uint(&s->pb, s->interlaced); /* Frames or fields coding */
442 }
443 
444 /* VC-2 12.1 - picture_header() */
446 {
448  put_bits32(&s->pb, s->picture_number++);
449 }
450 
451 /* VC-2 12.3.4.1 - slice_parameters() */
453 {
454  put_vc2_ue_uint(&s->pb, s->num_x);
455  put_vc2_ue_uint(&s->pb, s->num_y);
457  put_vc2_ue_uint(&s->pb, s->size_scaler);
458 }
459 
460 /* 1st idx = LL, second - vertical, third - horizontal, fourth - total */
461 const uint8_t vc2_qm_col_tab[][4] = {
462  {20, 9, 15, 4},
463  { 0, 6, 6, 4},
464  { 0, 3, 3, 5},
465  { 0, 3, 5, 1},
466  { 0, 11, 10, 11}
467 };
468 
469 const uint8_t vc2_qm_flat_tab[][4] = {
470  { 0, 0, 0, 0},
471  { 0, 0, 0, 0},
472  { 0, 0, 0, 0},
473  { 0, 0, 0, 0},
474  { 0, 0, 0, 0}
475 };
476 
478 {
479  int level, orientation;
480 
481  if (s->wavelet_depth <= 4 && s->quant_matrix == VC2_QM_DEF) {
482  s->custom_quant_matrix = 0;
483  for (level = 0; level < s->wavelet_depth; level++) {
488  }
489  return;
490  }
491 
492  s->custom_quant_matrix = 1;
493 
494  if (s->quant_matrix == VC2_QM_DEF) {
495  for (level = 0; level < s->wavelet_depth; level++) {
496  for (orientation = 0; orientation < 4; orientation++) {
497  if (level <= 3)
498  s->quant[level][orientation] = ff_dirac_default_qmat[s->wavelet_idx][level][orientation];
499  else
500  s->quant[level][orientation] = vc2_qm_col_tab[level][orientation];
501  }
502  }
503  } else if (s->quant_matrix == VC2_QM_COL) {
504  for (level = 0; level < s->wavelet_depth; level++) {
505  for (orientation = 0; orientation < 4; orientation++) {
506  s->quant[level][orientation] = vc2_qm_col_tab[level][orientation];
507  }
508  }
509  } else {
510  for (level = 0; level < s->wavelet_depth; level++) {
511  for (orientation = 0; orientation < 4; orientation++) {
512  s->quant[level][orientation] = vc2_qm_flat_tab[level][orientation];
513  }
514  }
515  }
516 }
517 
518 /* VC-2 12.3.4.2 - quant_matrix() */
520 {
521  int level;
522  put_bits(&s->pb, 1, s->custom_quant_matrix);
523  if (s->custom_quant_matrix) {
524  put_vc2_ue_uint(&s->pb, s->quant[0][0]);
525  for (level = 0; level < s->wavelet_depth; level++) {
526  put_vc2_ue_uint(&s->pb, s->quant[level][1]);
527  put_vc2_ue_uint(&s->pb, s->quant[level][2]);
528  put_vc2_ue_uint(&s->pb, s->quant[level][3]);
529  }
530  }
531 }
532 
533 /* VC-2 12.3 - transform_parameters() */
535 {
536  put_vc2_ue_uint(&s->pb, s->wavelet_idx);
538 
541 }
542 
543 /* VC-2 12.2 - wavelet_transform() */
545 {
548 }
549 
550 /* VC-2 12 - picture_parse() */
552 {
557 }
558 
559 #define QUANT(c, qf) (((c) << 2)/(qf))
560 
561 /* VC-2 13.5.5.2 - slice_band() */
562 static void encode_subband(VC2EncContext *s, PutBitContext *pb, int sx, int sy,
563  SubBand *b, int quant)
564 {
565  int x, y;
566 
567  const int left = b->width * (sx+0) / s->num_x;
568  const int right = b->width * (sx+1) / s->num_x;
569  const int top = b->height * (sy+0) / s->num_y;
570  const int bottom = b->height * (sy+1) / s->num_y;
571 
572  const int qfactor = ff_dirac_qscale_tab[quant];
573  const uint8_t *len_lut = &s->coef_lut_len[quant*COEF_LUT_TAB];
574  const uint32_t *val_lut = &s->coef_lut_val[quant*COEF_LUT_TAB];
575 
576  dwtcoef *coeff = b->buf + top * b->stride;
577 
578  for (y = top; y < bottom; y++) {
579  for (x = left; x < right; x++) {
580  const int neg = coeff[x] < 0;
581  uint32_t c_abs = FFABS(coeff[x]);
582  if (c_abs < COEF_LUT_TAB) {
583  put_bits(pb, len_lut[c_abs], val_lut[c_abs] | neg);
584  } else {
585  c_abs = QUANT(c_abs, qfactor);
586  put_vc2_ue_uint(pb, c_abs);
587  if (c_abs)
588  put_bits(pb, 1, neg);
589  }
590  }
591  coeff += b->stride;
592  }
593 }
594 
595 static int count_hq_slice(SliceArgs *slice, int quant_idx)
596 {
597  int x, y;
598  uint8_t quants[MAX_DWT_LEVELS][4];
599  int bits = 0, p, level, orientation;
600  VC2EncContext *s = slice->ctx;
601 
602  if (slice->cache[quant_idx])
603  return slice->cache[quant_idx];
604 
605  bits += 8*s->prefix_bytes;
606  bits += 8; /* quant_idx */
607 
608  for (level = 0; level < s->wavelet_depth; level++)
609  for (orientation = !!level; orientation < 4; orientation++)
610  quants[level][orientation] = FFMAX(quant_idx - s->quant[level][orientation], 0);
611 
612  for (p = 0; p < 3; p++) {
613  int bytes_start, bytes_len, pad_s, pad_c;
614  bytes_start = bits >> 3;
615  bits += 8;
616  for (level = 0; level < s->wavelet_depth; level++) {
617  for (orientation = !!level; orientation < 4; orientation++) {
618  SubBand *b = &s->plane[p].band[level][orientation];
619 
620  const int q_idx = quants[level][orientation];
621  const uint8_t *len_lut = &s->coef_lut_len[q_idx*COEF_LUT_TAB];
622  const int qfactor = ff_dirac_qscale_tab[q_idx];
623 
624  const int left = b->width * slice->x / s->num_x;
625  const int right = b->width *(slice->x+1) / s->num_x;
626  const int top = b->height * slice->y / s->num_y;
627  const int bottom = b->height *(slice->y+1) / s->num_y;
628 
629  dwtcoef *buf = b->buf + top * b->stride;
630 
631  for (y = top; y < bottom; y++) {
632  for (x = left; x < right; x++) {
633  uint32_t c_abs = FFABS(buf[x]);
634  if (c_abs < COEF_LUT_TAB) {
635  bits += len_lut[c_abs];
636  } else {
637  c_abs = QUANT(c_abs, qfactor);
638  bits += count_vc2_ue_uint(c_abs);
639  bits += !!c_abs;
640  }
641  }
642  buf += b->stride;
643  }
644  }
645  }
646  bits += FFALIGN(bits, 8) - bits;
647  bytes_len = (bits >> 3) - bytes_start - 1;
648  pad_s = FFALIGN(bytes_len, s->size_scaler)/s->size_scaler;
649  pad_c = (pad_s*s->size_scaler) - bytes_len;
650  bits += pad_c*8;
651  }
652 
653  slice->cache[quant_idx] = bits;
654 
655  return bits;
656 }
657 
658 /* Approaches the best possible quantizer asymptotically, its kinda exaustive
659  * but we have a LUT to get the coefficient size in bits. Guaranteed to never
660  * overshoot, which is apparently very important when streaming */
661 static int rate_control(AVCodecContext *avctx, void *arg)
662 {
663  SliceArgs *slice_dat = arg;
664  VC2EncContext *s = slice_dat->ctx;
665  const int top = slice_dat->bits_ceil;
666  const int bottom = slice_dat->bits_floor;
667  int quant_buf[2] = {-1, -1};
668  int quant = slice_dat->quant_idx, step = 1;
669  int bits_last, bits = count_hq_slice(slice_dat, quant);
670  while ((bits > top) || (bits < bottom)) {
671  const int signed_step = bits > top ? +step : -step;
672  quant = av_clip(quant + signed_step, 0, s->q_ceil-1);
673  bits = count_hq_slice(slice_dat, quant);
674  if (quant_buf[1] == quant) {
675  quant = FFMAX(quant_buf[0], quant);
676  bits = quant == quant_buf[0] ? bits_last : bits;
677  break;
678  }
679  step = av_clip(step/2, 1, (s->q_ceil-1)/2);
680  quant_buf[1] = quant_buf[0];
681  quant_buf[0] = quant;
682  bits_last = bits;
683  }
684  slice_dat->quant_idx = av_clip(quant, 0, s->q_ceil-1);
685  slice_dat->bytes = SSIZE_ROUND(bits >> 3);
686  return 0;
687 }
688 
690 {
691  int i, j, slice_x, slice_y, bytes_left = 0;
692  int bytes_top[SLICE_REDIST_TOTAL] = {0};
693  int64_t total_bytes_needed = 0;
694  int slice_redist_range = FFMIN(SLICE_REDIST_TOTAL, s->num_x*s->num_y);
695  SliceArgs *enc_args = s->slice_args;
696  SliceArgs *top_loc[SLICE_REDIST_TOTAL] = {NULL};
697 
699 
700  for (slice_y = 0; slice_y < s->num_y; slice_y++) {
701  for (slice_x = 0; slice_x < s->num_x; slice_x++) {
702  SliceArgs *args = &enc_args[s->num_x*slice_y + slice_x];
703  args->ctx = s;
704  args->x = slice_x;
705  args->y = slice_y;
706  args->bits_ceil = s->slice_max_bytes << 3;
707  args->bits_floor = s->slice_min_bytes << 3;
708  memset(args->cache, 0, s->q_ceil*sizeof(*args->cache));
709  }
710  }
711 
712  /* First pass - determine baseline slice sizes w.r.t. max_slice_size */
713  s->avctx->execute(s->avctx, rate_control, enc_args, NULL, s->num_x*s->num_y,
714  sizeof(SliceArgs));
715 
716  for (i = 0; i < s->num_x*s->num_y; i++) {
717  SliceArgs *args = &enc_args[i];
718  bytes_left += s->slice_max_bytes - args->bytes;
719  for (j = 0; j < slice_redist_range; j++) {
720  if (args->bytes > bytes_top[j]) {
721  bytes_top[j] = args->bytes;
722  top_loc[j] = args;
723  break;
724  }
725  }
726  }
727 
728  /* Second pass - distribute leftover bytes */
729  while (1) {
730  int distributed = 0;
731  for (i = 0; i < slice_redist_range; i++) {
732  SliceArgs *args;
733  int bits, bytes, diff, prev_bytes, new_idx;
734  if (bytes_left <= 0)
735  break;
736  if (!top_loc[i] || !top_loc[i]->quant_idx)
737  break;
738  args = top_loc[i];
739  prev_bytes = args->bytes;
740  new_idx = FFMAX(args->quant_idx - 1, 0);
741  bits = count_hq_slice(args, new_idx);
742  bytes = SSIZE_ROUND(bits >> 3);
743  diff = bytes - prev_bytes;
744  if ((bytes_left - diff) > 0) {
745  args->quant_idx = new_idx;
746  args->bytes = bytes;
747  bytes_left -= diff;
748  distributed++;
749  }
750  }
751  if (!distributed)
752  break;
753  }
754 
755  for (i = 0; i < s->num_x*s->num_y; i++) {
756  SliceArgs *args = &enc_args[i];
757  total_bytes_needed += args->bytes;
758  s->q_avg = (s->q_avg + args->quant_idx)/2;
759  }
760 
761  return total_bytes_needed;
762 }
763 
764 /* VC-2 13.5.3 - hq_slice */
765 static int encode_hq_slice(AVCodecContext *avctx, void *arg)
766 {
767  SliceArgs *slice_dat = arg;
768  VC2EncContext *s = slice_dat->ctx;
769  PutBitContext *pb = &slice_dat->pb;
770  const int slice_x = slice_dat->x;
771  const int slice_y = slice_dat->y;
772  const int quant_idx = slice_dat->quant_idx;
773  const int slice_bytes_max = slice_dat->bytes;
774  uint8_t quants[MAX_DWT_LEVELS][4];
775  int p, level, orientation;
776 
777  /* The reference decoder ignores it, and its typical length is 0 */
778  memset(put_bits_ptr(pb), 0, s->prefix_bytes);
780 
781  put_bits(pb, 8, quant_idx);
782 
783  /* Slice quantization (slice_quantizers() in the specs) */
784  for (level = 0; level < s->wavelet_depth; level++)
785  for (orientation = !!level; orientation < 4; orientation++)
786  quants[level][orientation] = FFMAX(quant_idx - s->quant[level][orientation], 0);
787 
788  /* Luma + 2 Chroma planes */
789  for (p = 0; p < 3; p++) {
790  int bytes_start, bytes_len, pad_s, pad_c;
791  bytes_start = put_bits_count(pb) >> 3;
792  put_bits(pb, 8, 0);
793  for (level = 0; level < s->wavelet_depth; level++) {
794  for (orientation = !!level; orientation < 4; orientation++) {
795  encode_subband(s, pb, slice_x, slice_y,
796  &s->plane[p].band[level][orientation],
797  quants[level][orientation]);
798  }
799  }
801  bytes_len = (put_bits_count(pb) >> 3) - bytes_start - 1;
802  if (p == 2) {
803  int len_diff = slice_bytes_max - (put_bits_count(pb) >> 3);
804  pad_s = FFALIGN((bytes_len + len_diff), s->size_scaler)/s->size_scaler;
805  pad_c = (pad_s*s->size_scaler) - bytes_len;
806  } else {
807  pad_s = FFALIGN(bytes_len, s->size_scaler)/s->size_scaler;
808  pad_c = (pad_s*s->size_scaler) - bytes_len;
809  }
810  pb->buf[bytes_start] = pad_s;
811  flush_put_bits(pb);
812  /* vc2-reference uses that padding that decodes to '0' coeffs */
813  memset(put_bits_ptr(pb), 0xFF, pad_c);
814  skip_put_bytes(pb, pad_c);
815  }
816 
817  return 0;
818 }
819 
820 /* VC-2 13.5.1 - low_delay_transform_data() */
822 {
823  uint8_t *buf;
824  int slice_x, slice_y, skip = 0;
825  SliceArgs *enc_args = s->slice_args;
826 
828  flush_put_bits(&s->pb);
829  buf = put_bits_ptr(&s->pb);
830 
831  for (slice_y = 0; slice_y < s->num_y; slice_y++) {
832  for (slice_x = 0; slice_x < s->num_x; slice_x++) {
833  SliceArgs *args = &enc_args[s->num_x*slice_y + slice_x];
834  init_put_bits(&args->pb, buf + skip, args->bytes+s->prefix_bytes);
835  skip += args->bytes;
836  }
837  }
838 
839  s->avctx->execute(s->avctx, encode_hq_slice, enc_args, NULL, s->num_x*s->num_y,
840  sizeof(SliceArgs));
841 
842  skip_put_bytes(&s->pb, skip);
843 
844  return 0;
845 }
846 
847 /*
848  * Transform basics for a 3 level transform
849  * |---------------------------------------------------------------------|
850  * | LL-0 | HL-0 | | |
851  * |--------|-------| HL-1 | |
852  * | LH-0 | HH-0 | | |
853  * |----------------|-----------------| HL-2 |
854  * | | | |
855  * | LH-1 | HH-1 | |
856  * | | | |
857  * |----------------------------------|----------------------------------|
858  * | | |
859  * | | |
860  * | | |
861  * | LH-2 | HH-2 |
862  * | | |
863  * | | |
864  * | | |
865  * |---------------------------------------------------------------------|
866  *
867  * DWT transforms are generally applied by splitting the image in two vertically
868  * and applying a low pass transform on the left part and a corresponding high
869  * pass transform on the right hand side. This is known as the horizontal filter
870  * stage.
871  * After that, the same operation is performed except the image is divided
872  * horizontally, with the high pass on the lower and the low pass on the higher
873  * side.
874  * Therefore, you're left with 4 subdivisions - known as low-low, low-high,
875  * high-low and high-high. They're referred to as orientations in the decoder
876  * and encoder.
877  *
878  * The LL (low-low) area contains the original image downsampled by the amount
879  * of levels. The rest of the areas can be thought as the details needed
880  * to restore the image perfectly to its original size.
881  */
882 static int dwt_plane(AVCodecContext *avctx, void *arg)
883 {
884  TransformArgs *transform_dat = arg;
885  VC2EncContext *s = transform_dat->ctx;
886  const void *frame_data = transform_dat->idata;
887  const ptrdiff_t linesize = transform_dat->istride;
888  const int field = transform_dat->field;
889  const Plane *p = transform_dat->plane;
890  VC2TransformContext *t = &transform_dat->t;
891  dwtcoef *buf = p->coef_buf;
892  const int idx = s->wavelet_idx;
893  const int skip = 1 + s->interlaced;
894 
895  int x, y, level, offset;
896  ptrdiff_t pix_stride = linesize >> (s->bpp - 1);
897 
898  if (field == 1) {
899  offset = 0;
900  pix_stride <<= 1;
901  } else if (field == 2) {
902  offset = pix_stride;
903  pix_stride <<= 1;
904  } else {
905  offset = 0;
906  }
907 
908  if (s->bpp == 1) {
909  const uint8_t *pix = (const uint8_t *)frame_data + offset;
910  for (y = 0; y < p->height*skip; y+=skip) {
911  for (x = 0; x < p->width; x++) {
912  buf[x] = pix[x] - s->diff_offset;
913  }
914  buf += p->coef_stride;
915  pix += pix_stride;
916  }
917  } else {
918  const uint16_t *pix = (const uint16_t *)frame_data + offset;
919  for (y = 0; y < p->height*skip; y+=skip) {
920  for (x = 0; x < p->width; x++) {
921  buf[x] = pix[x] - s->diff_offset;
922  }
923  buf += p->coef_stride;
924  pix += pix_stride;
925  }
926  }
927 
928  memset(buf, 0, p->coef_stride * (p->dwt_height - p->height) * sizeof(dwtcoef));
929 
930  for (level = s->wavelet_depth-1; level >= 0; level--) {
931  const SubBand *b = &p->band[level][0];
932  t->vc2_subband_dwt[idx](t, p->coef_buf, p->coef_stride,
933  b->width, b->height);
934  }
935 
936  return 0;
937 }
938 
939 static int encode_frame(VC2EncContext *s, AVPacket *avpkt, const AVFrame *frame,
940  const char *aux_data, const int header_size, int field)
941 {
942  int i, ret;
943  int64_t max_frame_bytes;
944 
945  /* Threaded DWT transform */
946  for (i = 0; i < 3; i++) {
947  s->transform_args[i].ctx = s;
948  s->transform_args[i].field = field;
949  s->transform_args[i].plane = &s->plane[i];
950  s->transform_args[i].idata = frame->data[i];
951  s->transform_args[i].istride = frame->linesize[i];
952  }
954  sizeof(TransformArgs));
955 
956  /* Calculate per-slice quantizers and sizes */
957  max_frame_bytes = header_size + calc_slice_sizes(s);
958 
959  if (field < 2) {
960  ret = ff_alloc_packet2(s->avctx, avpkt,
961  max_frame_bytes << s->interlaced,
962  max_frame_bytes << s->interlaced);
963  if (ret) {
964  av_log(s->avctx, AV_LOG_ERROR, "Error getting output packet.\n");
965  return ret;
966  }
967  init_put_bits(&s->pb, avpkt->data, avpkt->size);
968  }
969 
970  /* Sequence header */
973 
974  /* Encoder version */
975  if (aux_data) {
977  avpriv_put_string(&s->pb, aux_data, 1);
978  }
979 
980  /* Picture header */
983 
984  /* Encode slices */
985  encode_slices(s);
986 
987  /* End sequence */
989 
990  return 0;
991 }
992 
994  const AVFrame *frame, int *got_packet)
995 {
996  int ret = 0;
997  int sig_size = 256;
998  VC2EncContext *s = avctx->priv_data;
999  const int bitexact = avctx->flags & AV_CODEC_FLAG_BITEXACT;
1000  const char *aux_data = bitexact ? "Lavc" : LIBAVCODEC_IDENT;
1001  const int aux_data_size = bitexact ? sizeof("Lavc") : sizeof(LIBAVCODEC_IDENT);
1002  const int header_size = 100 + aux_data_size;
1003  int64_t max_frame_bytes, r_bitrate = avctx->bit_rate >> (s->interlaced);
1004 
1005  s->avctx = avctx;
1006  s->size_scaler = 2;
1007  s->prefix_bytes = 0;
1008  s->last_parse_code = 0;
1009  s->next_parse_offset = 0;
1010 
1011  /* Rate control */
1012  max_frame_bytes = (av_rescale(r_bitrate, s->avctx->time_base.num,
1013  s->avctx->time_base.den) >> 3) - header_size;
1014  s->slice_max_bytes = av_rescale(max_frame_bytes, 1, s->num_x*s->num_y);
1015 
1016  /* Find an appropriate size scaler */
1017  while (sig_size > 255) {
1018  int r_size = SSIZE_ROUND(s->slice_max_bytes);
1019  sig_size = r_size/s->size_scaler; /* Signalled slize size */
1020  s->size_scaler <<= 1;
1021  }
1022 
1024  s->slice_min_bytes = s->slice_max_bytes - s->slice_max_bytes*(s->tolerance/100.0f);
1025 
1026  ret = encode_frame(s, avpkt, frame, aux_data, header_size, s->interlaced);
1027  if (ret)
1028  return ret;
1029  if (s->interlaced) {
1030  ret = encode_frame(s, avpkt, frame, aux_data, header_size, 2);
1031  if (ret)
1032  return ret;
1033  }
1034 
1035  flush_put_bits(&s->pb);
1036  avpkt->size = put_bits_count(&s->pb) >> 3;
1037 
1038  *got_packet = 1;
1039 
1040  return 0;
1041 }
1042 
1044 {
1045  int i;
1046  VC2EncContext *s = avctx->priv_data;
1047 
1048  av_log(avctx, AV_LOG_INFO, "Qavg: %i\n", s->q_avg);
1049 
1050  for (i = 0; i < 3; i++) {
1052  av_freep(&s->plane[i].coef_buf);
1053  }
1054 
1055  av_freep(&s->slice_args);
1056  av_freep(&s->coef_lut_len);
1057  av_freep(&s->coef_lut_val);
1058 
1059  return 0;
1060 }
1061 
1063 {
1064  Plane *p;
1065  SubBand *b;
1066  int i, j, level, o, shift;
1068  const int depth = fmt->comp[0].depth;
1069  VC2EncContext *s = avctx->priv_data;
1070 
1071  s->picture_number = 0;
1072 
1073  /* Total allowed quantization range */
1075 
1076  s->ver.major = 2;
1077  s->ver.minor = 0;
1078  s->profile = 3;
1079  s->level = 3;
1080 
1081  s->base_vf = -1;
1082  s->strict_compliance = 1;
1083 
1084  s->q_avg = 0;
1085  s->slice_max_bytes = 0;
1086  s->slice_min_bytes = 0;
1087 
1088  /* Mark unknown as progressive */
1089  s->interlaced = !((avctx->field_order == AV_FIELD_UNKNOWN) ||
1090  (avctx->field_order == AV_FIELD_PROGRESSIVE));
1091 
1092  for (i = 0; i < base_video_fmts_len; i++) {
1093  const VC2BaseVideoFormat *fmt = &base_video_fmts[i];
1094  if (avctx->pix_fmt != fmt->pix_fmt)
1095  continue;
1096  if (avctx->time_base.num != fmt->time_base.num)
1097  continue;
1098  if (avctx->time_base.den != fmt->time_base.den)
1099  continue;
1100  if (avctx->width != fmt->width)
1101  continue;
1102  if (avctx->height != fmt->height)
1103  continue;
1104  if (s->interlaced != fmt->interlaced)
1105  continue;
1106  s->base_vf = i;
1107  s->level = base_video_fmts[i].level;
1108  break;
1109  }
1110 
1111  if (s->interlaced)
1112  av_log(avctx, AV_LOG_WARNING, "Interlacing enabled!\n");
1113 
1114  if ((s->slice_width & (s->slice_width - 1)) ||
1115  (s->slice_height & (s->slice_height - 1))) {
1116  av_log(avctx, AV_LOG_ERROR, "Slice size is not a power of two!\n");
1117  return AVERROR_UNKNOWN;
1118  }
1119 
1120  if ((s->slice_width > avctx->width) ||
1121  (s->slice_height > avctx->height)) {
1122  av_log(avctx, AV_LOG_ERROR, "Slice size is bigger than the image!\n");
1123  return AVERROR_UNKNOWN;
1124  }
1125 
1126  if (s->base_vf <= 0) {
1128  s->strict_compliance = s->base_vf = 0;
1129  av_log(avctx, AV_LOG_WARNING, "Disabling strict compliance\n");
1130  } else {
1131  av_log(avctx, AV_LOG_WARNING, "Given format does not strictly comply with "
1132  "the specifications, please add a -strict -1 flag to use it\n");
1133  return AVERROR_UNKNOWN;
1134  }
1135  } else {
1136  av_log(avctx, AV_LOG_INFO, "Selected base video format = %i (%s)\n",
1137  s->base_vf, base_video_fmts[s->base_vf].name);
1138  }
1139 
1140  /* Chroma subsampling */
1142 
1143  /* Bit depth and color range index */
1144  if (depth == 8 && avctx->color_range == AVCOL_RANGE_JPEG) {
1145  s->bpp = 1;
1146  s->bpp_idx = 1;
1147  s->diff_offset = 128;
1148  } else if (depth == 8 && (avctx->color_range == AVCOL_RANGE_MPEG ||
1149  avctx->color_range == AVCOL_RANGE_UNSPECIFIED)) {
1150  s->bpp = 1;
1151  s->bpp_idx = 2;
1152  s->diff_offset = 128;
1153  } else if (depth == 10) {
1154  s->bpp = 2;
1155  s->bpp_idx = 3;
1156  s->diff_offset = 512;
1157  } else {
1158  s->bpp = 2;
1159  s->bpp_idx = 4;
1160  s->diff_offset = 2048;
1161  }
1162 
1163  /* Planes initialization */
1164  for (i = 0; i < 3; i++) {
1165  int w, h;
1166  p = &s->plane[i];
1167  p->width = avctx->width >> (i ? s->chroma_x_shift : 0);
1168  p->height = avctx->height >> (i ? s->chroma_y_shift : 0);
1169  if (s->interlaced)
1170  p->height >>= 1;
1171  p->dwt_width = w = FFALIGN(p->width, (1 << s->wavelet_depth));
1172  p->dwt_height = h = FFALIGN(p->height, (1 << s->wavelet_depth));
1173  p->coef_stride = FFALIGN(p->dwt_width, 32);
1174  p->coef_buf = av_mallocz(p->coef_stride*p->dwt_height*sizeof(dwtcoef));
1175  if (!p->coef_buf)
1176  goto alloc_fail;
1177  for (level = s->wavelet_depth-1; level >= 0; level--) {
1178  w = w >> 1;
1179  h = h >> 1;
1180  for (o = 0; o < 4; o++) {
1181  b = &p->band[level][o];
1182  b->width = w;
1183  b->height = h;
1184  b->stride = p->coef_stride;
1185  shift = (o > 1)*b->height*b->stride + (o & 1)*b->width;
1186  b->buf = p->coef_buf + shift;
1187  }
1188  }
1189 
1190  /* DWT init */
1192  s->plane[i].coef_stride,
1193  s->plane[i].dwt_height,
1194  s->slice_width, s->slice_height))
1195  goto alloc_fail;
1196  }
1197 
1198  /* Slices */
1199  s->num_x = s->plane[0].dwt_width/s->slice_width;
1200  s->num_y = s->plane[0].dwt_height/s->slice_height;
1201 
1202  s->slice_args = av_calloc(s->num_x*s->num_y, sizeof(SliceArgs));
1203  if (!s->slice_args)
1204  goto alloc_fail;
1205 
1206  /* Lookup tables */
1207  s->coef_lut_len = av_malloc(COEF_LUT_TAB*(s->q_ceil+1)*sizeof(*s->coef_lut_len));
1208  if (!s->coef_lut_len)
1209  goto alloc_fail;
1210 
1211  s->coef_lut_val = av_malloc(COEF_LUT_TAB*(s->q_ceil+1)*sizeof(*s->coef_lut_val));
1212  if (!s->coef_lut_val)
1213  goto alloc_fail;
1214 
1215  for (i = 0; i < s->q_ceil; i++) {
1216  uint8_t *len_lut = &s->coef_lut_len[i*COEF_LUT_TAB];
1217  uint32_t *val_lut = &s->coef_lut_val[i*COEF_LUT_TAB];
1218  for (j = 0; j < COEF_LUT_TAB; j++) {
1220  &len_lut[j], &val_lut[j]);
1221  if (len_lut[j] != 1) {
1222  len_lut[j] += 1;
1223  val_lut[j] <<= 1;
1224  } else {
1225  val_lut[j] = 1;
1226  }
1227  }
1228  }
1229 
1230  return 0;
1231 
1232 alloc_fail:
1233  vc2_encode_end(avctx);
1234  av_log(avctx, AV_LOG_ERROR, "Unable to allocate memory!\n");
1235  return AVERROR(ENOMEM);
1236 }
1237 
1238 #define VC2ENC_FLAGS (AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
1239 static const AVOption vc2enc_options[] = {
1240  {"tolerance", "Max undershoot in percent", offsetof(VC2EncContext, tolerance), AV_OPT_TYPE_DOUBLE, {.dbl = 5.0f}, 0.0f, 45.0f, VC2ENC_FLAGS, "tolerance"},
1241  {"slice_width", "Slice width", offsetof(VC2EncContext, slice_width), AV_OPT_TYPE_INT, {.i64 = 32}, 32, 1024, VC2ENC_FLAGS, "slice_width"},
1242  {"slice_height", "Slice height", offsetof(VC2EncContext, slice_height), AV_OPT_TYPE_INT, {.i64 = 16}, 8, 1024, VC2ENC_FLAGS, "slice_height"},
1243  {"wavelet_depth", "Transform depth", offsetof(VC2EncContext, wavelet_depth), AV_OPT_TYPE_INT, {.i64 = 4}, 1, 5, VC2ENC_FLAGS, "wavelet_depth"},
1244  {"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"},
1245  {"9_7", "Deslauriers-Dubuc (9,7)", 0, AV_OPT_TYPE_CONST, {.i64 = VC2_TRANSFORM_9_7}, INT_MIN, INT_MAX, VC2ENC_FLAGS, "wavelet_idx"},
1246  {"5_3", "LeGall (5,3)", 0, AV_OPT_TYPE_CONST, {.i64 = VC2_TRANSFORM_5_3}, INT_MIN, INT_MAX, VC2ENC_FLAGS, "wavelet_idx"},
1247  {"haar", "Haar (with shift)", 0, AV_OPT_TYPE_CONST, {.i64 = VC2_TRANSFORM_HAAR_S}, INT_MIN, INT_MAX, VC2ENC_FLAGS, "wavelet_idx"},
1248  {"haar_noshift", "Haar (without shift)", 0, AV_OPT_TYPE_CONST, {.i64 = VC2_TRANSFORM_HAAR}, INT_MIN, INT_MAX, VC2ENC_FLAGS, "wavelet_idx"},
1249  {"qm", "Custom quantization matrix", offsetof(VC2EncContext, quant_matrix), AV_OPT_TYPE_INT, {.i64 = VC2_QM_DEF}, 0, VC2_QM_NB, VC2ENC_FLAGS, "quant_matrix"},
1250  {"default", "Default from the specifications", 0, AV_OPT_TYPE_CONST, {.i64 = VC2_QM_DEF}, INT_MIN, INT_MAX, VC2ENC_FLAGS, "quant_matrix"},
1251  {"color", "Prevents low bitrate discoloration", 0, AV_OPT_TYPE_CONST, {.i64 = VC2_QM_COL}, INT_MIN, INT_MAX, VC2ENC_FLAGS, "quant_matrix"},
1252  {"flat", "Optimize for PSNR", 0, AV_OPT_TYPE_CONST, {.i64 = VC2_QM_FLAT}, INT_MIN, INT_MAX, VC2ENC_FLAGS, "quant_matrix"},
1253  {NULL}
1254 };
1255 
1256 static const AVClass vc2enc_class = {
1257  .class_name = "SMPTE VC-2 encoder",
1258  .category = AV_CLASS_CATEGORY_ENCODER,
1259  .option = vc2enc_options,
1260  .item_name = av_default_item_name,
1261  .version = LIBAVUTIL_VERSION_INT
1262 };
1263 
1265  { "b", "600000000" },
1266  { NULL },
1267 };
1268 
1269 static const enum AVPixelFormat allowed_pix_fmts[] = {
1274 };
1275 
1277  .name = "vc2",
1278  .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-2"),
1279  .type = AVMEDIA_TYPE_VIDEO,
1280  .id = AV_CODEC_ID_DIRAC,
1281  .priv_data_size = sizeof(VC2EncContext),
1282  .init = vc2_encode_init,
1283  .close = vc2_encode_end,
1284  .capabilities = AV_CODEC_CAP_SLICE_THREADS,
1285  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
1286  .encode2 = vc2_encode_frame,
1287  .priv_class = &vc2enc_class,
1288  .defaults = vc2enc_defaults,
1290 };
int strict_compliance
Definition: vc2enc.c:176
static void encode_parse_info(VC2EncContext *s, enum DiracParseCodes pcode)
Definition: vc2enc.c:263
static void av_unused put_bits32(PutBitContext *s, uint32_t value)
Write exactly 32 bits into a bitstream.
Definition: put_bits.h:210
#define NULL
Definition: coverity.c:32
const char const char void * val
Definition: avisynth_c.h:771
const int32_t ff_dirac_qscale_tab[116]
Definition: diractab.c:34
const char * s
Definition: avisynth_c.h:768
static int encode_frame(VC2EncContext *s, AVPacket *avpkt, const AVFrame *frame, const char *aux_data, const int header_size, int field)
Definition: vc2enc.c:939
static void encode_wavelet_transform(VC2EncContext *s)
Definition: vc2enc.c:544
static void encode_aspect_ratio(VC2EncContext *s)
Definition: vc2enc.c:351
static int shift(int a, int b)
Definition: sonic.c:82
static av_cold int vc2_encode_init(AVCodecContext *avctx)
Definition: vc2enc.c:1062
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2333
This structure describes decoded (raw) audio or video data.
Definition: frame.h:187
av_cold void ff_vc2enc_free_transforms(VC2TransformContext *s)
Definition: vc2enc_dwt.c:277
int base_vf
Definition: vc2enc.c:147
AVOption.
Definition: opt.h:246
int slice_height
Definition: vc2enc.c:177
"Linear transfer characteristics"
Definition: pixfmt.h:432
TransformArgs transform_args[3]
Definition: vc2enc.c:136
uint32_t next_parse_offset
Definition: vc2enc.c:183
const char * fmt
Definition: avisynth_c.h:769
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:67
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:206
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
int64_t bit_rate
the average bitrate
Definition: avcodec.h:1797
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
static void encode_source_params(VC2EncContext *s)
Definition: vc2enc.c:422
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
void * ctx
Definition: vc2enc.c:110
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM / IEC 61966-2-4 xvYCC601 ...
Definition: pixfmt.h:457
int wavelet_depth
Definition: vc2enc.c:175
static av_always_inline void put_vc2_ue_uint(PutBitContext *pb, uint32_t val)
Definition: vc2enc.c:187
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:2469
int num
Numerator.
Definition: rational.h:59
int size
Definition: avcodec.h:1658
const char * b
Definition: vf_curves.c:113
void avpriv_align_put_bits(PutBitContext *s)
Pad the bitstream with zeros up to the next byte boundary.
Definition: bitstream.c:48
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:2143
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1960
#define AV_PIX_FMT_YUV420P12
Definition: pixfmt.h:355
uint32_t picture_number
Definition: vc2enc.c:144
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:222
ptrdiff_t stride
Definition: vc2enc.c:92
AVCodec.
Definition: avcodec.h:3681
order of coefficients is actually GBR, also IEC 61966-2-1 (sRGB)
Definition: pixfmt.h:452
AVCodec ff_vc2_encoder
Definition: vc2enc.c:1276
VC2_QM
Definition: vc2enc.c:82
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
Definition: mem.c:230
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1869
static av_cold int vc2_encode_end(AVCodecContext *avctx)
Definition: vc2enc.c:1043
int width
Definition: cfhd.h:48
#define VC2ENC_FLAGS
Definition: vc2enc.c:1238
const uint8_t vc2_qm_flat_tab[][4]
Definition: vc2enc.c:469
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
static void encode_transform_params(VC2EncContext *s)
Definition: vc2enc.c:534
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:40
uint8_t bits
Definition: crc.c:296
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition: pixdesc.h:117
uint8_t
#define av_cold
Definition: attributes.h:82
int interlaced
Definition: vc2enc.c:179
#define av_malloc(s)
enum DiracParseCodes last_parse_code
Definition: vc2enc.c:184
Interface to Dirac Decoder/Encoder.
AVOptions.
dwtcoef * coef_buf
Definition: vc2enc.c:99
VC2TransformContext t
Definition: vc2enc.c:125
static AVFrame * frame
uint8_t * data
Definition: avcodec.h:1657
int bytes
Definition: vc2enc.c:116
int size_scaler
Definition: vc2enc.c:162
static av_always_inline int count_vc2_ue_uint(uint32_t val)
Definition: vc2enc.c:215
static const int base_video_fmts_len
Definition: vc2enc.c:80
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:258
#define AV_PIX_FMT_YUV422P12
Definition: pixfmt.h:356
int x
Definition: vc2enc.c:111
static av_cold int vc2_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet)
Definition: vc2enc.c:993
#define FFALIGN(x, a)
Definition: macros.h:48
#define av_log(a,...)
ptrdiff_t coef_stride
Definition: vc2enc.c:104
Definition: cfhd.h:43
int slice_min_bytes
Definition: vc2enc.c:168
#define FF_COMPLIANCE_UNOFFICIAL
Allow unofficial extensions.
Definition: avcodec.h:2955
static void init_quant_matrix(VC2EncContext *s)
Definition: vc2enc.c:477
Libavcodec version macros.
static void encode_clean_area(VC2EncContext *s)
Definition: vc2enc.c:363
const uint8_t vc2_qm_col_tab[][4]
Definition: vc2enc.c:461
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
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:227
ITU-R BT1361 Extended Colour Gamut.
Definition: pixfmt.h:436
int bpp_idx
Definition: vc2enc.c:141
av_default_item_name
#define AVERROR(e)
Definition: error.h:43
static int count_hq_slice(SliceArgs *slice, int quant_idx)
Definition: vc2enc.c:595
PutBitContext pb
Definition: vc2enc.c:130
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:179
static void encode_parse_params(VC2EncContext *s)
Definition: vc2enc.c:295
const char * arg
Definition: jacosubdec.c:66
const uint8_t ff_dirac_default_qmat[7][4][4]
Definition: diractab.c:24
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:1827
uint8_t * buf
Definition: put_bits.h:38
int stride
Definition: cfhd.h:46
const char * name
Name of the codec implementation.
Definition: avcodec.h:3688
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:354
static const AVCodecDefault vc2enc_defaults[]
Definition: vc2enc.c:1264
#define DIRAC_MAX_QUANT_INDEX
Definition: diractab.h:41
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
#define FFMAX(a, b)
Definition: common.h:94
int depth
Definition: v4l.c:62
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:85
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:66
static av_always_inline void get_vc2_ue_uint(int val, uint8_t *nbits, uint32_t *eval)
Definition: vc2enc.c:231
#define SSIZE_ROUND(b)
Definition: vc2enc.c:37
static void encode_seq_header(VC2EncContext *s)
Definition: vc2enc.c:435
int chroma_x_shift
Definition: vc2enc.c:163
int profile
Definition: vc2enc.c:149
SubBand band[DWT_LEVELS][4]
Definition: cfhd.h:68
static void skip_put_bytes(PutBitContext *s, int n)
Skip the given number of bytes.
Definition: put_bits.h:236
int dwt_height
Definition: vc2enc.c:103
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition: avcodec.h:921
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
Definition: mathematics.c:129
static void encode_scan_format(VC2EncContext *s)
Definition: vc2enc.c:331
#define FFMIN(a, b)
Definition: common.h:96
int diff_offset
Definition: vc2enc.c:139
int custom_quant_matrix
Definition: vc2enc.c:153
int width
picture width / height.
Definition: avcodec.h:1919
int quant_idx
Definition: vc2enc.c:113
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM
Definition: pixfmt.h:407
AVS_Value args
Definition: avisynth_c.h:699
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition: avcodec.h:2448
int bits_ceil
Definition: vc2enc.c:114
static void encode_frame_size(VC2EncContext *s)
Definition: vc2enc.c:304
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
static void encode_quant_matrix(VC2EncContext *s)
Definition: vc2enc.c:519
void * ctx
Definition: vc2enc.c:120
void * idata
Definition: vc2enc.c:122
AVCodecContext * avctx
Definition: vc2enc.c:132
void avcodec_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
Utility function to access log2_chroma_w log2_chroma_h from the pixel format AVPixFmtDescriptor.
Definition: imgconvert.c:38
#define FF_ARRAY_ELEMS(a)
the normal 2^n-1 "JPEG" YUV ranges
Definition: pixfmt.h:476
#define SLICE_REDIST_TOTAL
Definition: vc2enc.c:40
static void encode_slice_params(VC2EncContext *s)
Definition: vc2enc.c:452
static void encode_sample_fmt(VC2EncContext *s)
Definition: vc2enc.c:315
#define AV_CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
Definition: avcodec.h:1061
uint8_t quant[MAX_DWT_LEVELS][4]
Definition: vc2enc.c:152
int q_ceil
Definition: vc2enc.c:169
uint32_t * coef_lut_val
Definition: vc2enc.c:156
static void encode_frame_rate(VC2EncContext *s)
Definition: vc2enc.c:339
int slice_width
Definition: vc2enc.c:178
static void encode_picture_header(VC2EncContext *s)
Definition: vc2enc.c:445
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
static void encode_signal_range(VC2EncContext *s)
Definition: vc2enc.c:369
static int calc_slice_sizes(VC2EncContext *s)
Definition: vc2enc.c:689
static int dwt_plane(AVCodecContext *avctx, void *arg)
Definition: vc2enc.c:882
functionally identical to above
Definition: pixfmt.h:409
uint8_t * coef_lut_len
Definition: vc2enc.c:157
#define ff_log2
Definition: intmath.h:50
#define QUANT(c, qf)
Definition: vc2enc.c:559
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:218
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
AVClass * av_class
Definition: vc2enc.c:129
main external API structure.
Definition: avcodec.h:1732
DiracParseCodes
Parse code values:
Definition: dirac.h:57
static void encode_color_spec(VC2EncContext *s)
Definition: vc2enc.c:377
void * buf
Definition: avisynth_c.h:690
static int encode_hq_slice(AVCodecContext *avctx, void *arg)
Definition: vc2enc.c:765
#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:45
static int encode_slices(VC2EncContext *s)
Definition: vc2enc.c:821
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:351
Describe the class of an AVClass context structure.
Definition: log.h:67
#define AV_WB32(p, v)
Definition: intreadwrite.h:424
void(* vc2_subband_dwt[VC2_TRANSFORMS_NB])(struct VC2TransformContext *t, dwtcoef *data, ptrdiff_t stride, int width, int height)
Definition: vc2enc_dwt.h:45
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:2462
Rational number (pair of numerator and denominator).
Definition: rational.h:58
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:2455
double tolerance
Definition: vc2enc.c:173
#define COEF_LUT_TAB
Definition: vc2enc.c:34
SliceArgs * slice_args
Definition: vc2enc.c:135
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
Definition: utils.c:1736
Plane * plane
Definition: vc2enc.c:121
static void encode_subband(VC2EncContext *s, PutBitContext *pb, int sx, int sy, SubBand *b, int quant)
Definition: vc2enc.c:562
int cache[DIRAC_MAX_QUANT_INDEX]
Definition: vc2enc.c:109
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:262
const uint8_t * quant
ptrdiff_t istride
Definition: vc2enc.c:123
Plane plane[3]
Definition: vc2enc.c:131
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:352
#define AV_PIX_FMT_YUV444P12
Definition: pixfmt.h:358
int chroma_y_shift
Definition: vc2enc.c:164
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:201
uint8_t level
Definition: svq3.c:207
static enum AVPixelFormat allowed_pix_fmts[]
Definition: vc2enc.c:1269
int dwt_width
Definition: vc2enc.c:102
the normal 219*2^(n-8) "MPEG" YUV ranges
Definition: pixfmt.h:475
DiracVersionInfo ver
Definition: vc2enc.c:133
int wavelet_idx
Definition: vc2enc.c:174
int slice_max_bytes
Definition: vc2enc.c:167
static const VC2BaseVideoFormat base_video_fmts[]
Definition: vc2enc.c:49
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:62
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:101
static const AVOption vc2enc_options[]
Definition: vc2enc.c:1239
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:48
int den
Denominator.
Definition: rational.h:60
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:71
dwtcoef * buf
Definition: vc2enc.c:91
int32_t dwtcoef
Definition: vc2enc_dwt.h:28
int y
Definition: vc2enc.c:112
void * priv_data
Definition: avcodec.h:1774
static av_always_inline int diff(const uint32_t a, const uint32_t b)
static int rate_control(AVCodecContext *avctx, void *arg)
Definition: vc2enc.c:661
int(* execute)(struct AVCodecContext *c, int(*func)(struct AVCodecContext *c2, void *arg), void *arg2, int *ret, int count, int size)
The codec may call this to execute several independent things.
Definition: avcodec.h:3201
int height
Definition: cfhd.h:50
static const double coeff[2][5]
Definition: vf_owdenoise.c:72
static void encode_picture_start(VC2EncContext *s)
Definition: vc2enc.c:551
int width
Definition: cfhd.h:57
#define LIBAVCODEC_IDENT
Definition: version.h:42
void avpriv_put_string(PutBitContext *pb, const char *string, int terminate_string)
Put the string string in the bitstream.
Definition: bitstream.c:53
enum AVPixelFormat pix_fmt
Definition: vc2enc.c:43
also ITU-R BT601-6 525 / ITU-R BT1358 525 / ITU-R BT1700 NTSC
Definition: pixfmt.h:408
DWTELEM * buf
Definition: snow.h:88
#define av_freep(p)
enum AVFieldOrder field_order
Field order.
Definition: avcodec.h:2491
int prefix_bytes
Definition: vc2enc.c:161
#define av_always_inline
Definition: attributes.h:39
static const AVCodecDefault defaults[]
Definition: dcaenc.c:1095
int height
Definition: cfhd.h:58
AVRational time_base
Definition: vc2enc.c:44
int depth
Number of bits in the component.
Definition: pixdesc.h:58
AVPixelFormat
Pixel format.
Definition: pixfmt.h:60
This structure stores compressed data.
Definition: avcodec.h:1634
enum VC2_QM quant_matrix
Definition: vc2enc.c:180
int strict_std_compliance
strictly follow the standard (MPEG-4, ...).
Definition: avcodec.h:2951
static const AVClass vc2enc_class
Definition: vc2enc.c:1256
Definition: cfhd.h:56
int bits_floor
Definition: vc2enc.c:115
const char * name
Definition: vc2enc.c:46
PutBitContext pb
Definition: vc2enc.c:108
bitstream writer API