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