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