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