FFmpeg  4.4.9
dvenc.c
Go to the documentation of this file.
1 /*
2  * DV encoder
3  * Copyright (c) 2003 Roman Shaposhnik
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  * quant_deadzone code and fixes sponsored by NOA GmbH
22  */
23 
24 /**
25  * @file
26  * DV encoder
27  */
28 
29 #include "config.h"
30 
31 #include "libavutil/attributes.h"
32 #include "libavutil/internal.h"
33 #include "libavutil/mem_internal.h"
34 #include "libavutil/opt.h"
35 #include "libavutil/pixdesc.h"
36 
37 #include "avcodec.h"
38 #include "dv.h"
39 #include "dv_profile_internal.h"
40 #include "dv_tablegen.h"
41 #include "fdctdsp.h"
42 #include "internal.h"
43 #include "mathops.h"
44 #include "me_cmp.h"
45 #include "pixblockdsp.h"
46 #include "put_bits.h"
47 
49 {
50  DVVideoContext *s = avctx->priv_data;
51  FDCTDSPContext fdsp;
52  int ret;
53 
54  s->sys = av_dv_codec_profile2(avctx->width, avctx->height, avctx->pix_fmt, avctx->time_base);
55  if (!s->sys) {
56  av_log(avctx, AV_LOG_ERROR, "Found no DV profile for %ix%i %s video. "
57  "Valid DV profiles are:\n",
58  avctx->width, avctx->height, av_get_pix_fmt_name(avctx->pix_fmt));
60  return AVERROR(EINVAL);
61  }
62 
63  ret = ff_dv_init_dynamic_tables(s, s->sys);
64  if (ret < 0) {
65  av_log(avctx, AV_LOG_ERROR, "Error initializing work tables.\n");
66  return ret;
67  }
68 
70 
71  if (avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) {
72  MECmpContext mecc;
73 
74  memset(&mecc,0, sizeof(mecc));
75  ff_me_cmp_init(&mecc, avctx);
76  ret = ff_set_cmp(&mecc, mecc.ildct_cmp, avctx->ildct_cmp);
77  if (ret < 0)
78  return ret;
79  if (!mecc.ildct_cmp[5])
80  return AVERROR(EINVAL);
81  s->ildct_cmp = mecc.ildct_cmp[5];
82  }
83 
84  memset(&fdsp,0, sizeof(fdsp));
85  ff_fdctdsp_init(&fdsp, avctx);
86  s->fdct[0] = fdsp.fdct;
87  s->fdct[1] = fdsp.fdct248;
88  ff_pixblockdsp_init(&s->pdsp, avctx);
89 
90  return ff_dvvideo_init(avctx);
91 }
92 
93 /* bit budget for AC only in 5 MBs */
94 static const int vs_total_ac_bits_hd = (68 * 6 + 52*2) * 5;
95 static const int vs_total_ac_bits = (100 * 4 + 68 * 2) * 5;
96 static const int mb_area_start[5] = { 1, 6, 21, 43, 64 };
97 
98 #if CONFIG_SMALL
99 /* Convert run and level (where level != 0) pair into VLC, returning bit size */
100 static av_always_inline int dv_rl2vlc(int run, int level, int sign,
101  uint32_t *vlc)
102 {
103  int size;
105  *vlc = dv_vlc_map[run][level].vlc | sign;
107  } else {
108  if (level < DV_VLC_MAP_LEV_SIZE) {
109  *vlc = dv_vlc_map[0][level].vlc | sign;
110  size = dv_vlc_map[0][level].size;
111  } else {
112  *vlc = 0xfe00 | (level << 1) | sign;
113  size = 16;
114  }
115  if (run) {
116  *vlc |= ((run < 16) ? dv_vlc_map[run - 1][0].vlc :
117  (0x1f80 | (run - 1))) << size;
118  size += (run < 16) ? dv_vlc_map[run - 1][0].size : 13;
119  }
120  }
121 
122  return size;
123 }
124 
125 static av_always_inline int dv_rl2vlc_size(int run, int level)
126 {
127  int size;
128 
131  } else {
133  if (run)
134  size += (run < 16) ? dv_vlc_map[run - 1][0].size : 13;
135  }
136  return size;
137 }
138 #else
139 static av_always_inline int dv_rl2vlc(int run, int l, int sign, uint32_t *vlc)
140 {
141  *vlc = dv_vlc_map[run][l].vlc | sign;
142  return dv_vlc_map[run][l].size;
143 }
144 
145 static av_always_inline int dv_rl2vlc_size(int run, int l)
146 {
147  return dv_vlc_map[run][l].size;
148 }
149 #endif
150 
151 typedef struct EncBlockInfo {
152  int area_q[4];
153  int bit_size[4];
154  int prev[5];
155  int cur_ac;
156  int cno;
157  int dct_mode;
158  int16_t mb[64];
162  uint32_t partial_bit_buffer; /* we can't use uint16_t here */
163  /* used by DV100 only: a copy of the weighted and classified but
164  not-yet-quantized AC coefficients. This is necessary for
165  re-quantizing at different steps. */
166  int16_t save[64];
167  int min_qlevel; /* DV100 only: minimum qlevel (for AC coefficients >255) */
168 } EncBlockInfo;
169 
171  PutBitContext *pb_pool,
172  PutBitContext *pb_end)
173 {
174  int prev, bits_left;
175  PutBitContext *pb = pb_pool;
176  int size = bi->partial_bit_count;
177  uint32_t vlc = bi->partial_bit_buffer;
178 
179  bi->partial_bit_count =
180  bi->partial_bit_buffer = 0;
181  for (;;) {
182  /* Find suitable storage space */
183  for (; size > (bits_left = put_bits_left(pb)); pb++) {
184  if (bits_left) {
185  size -= bits_left;
186  put_bits(pb, bits_left, vlc >> size);
187  vlc = av_mod_uintp2(vlc, size);
188  }
189  if (pb + 1 >= pb_end) {
190  bi->partial_bit_count = size;
191  bi->partial_bit_buffer = vlc;
192  return pb;
193  }
194  }
195 
196  /* Store VLC */
197  put_bits(pb, size, vlc);
198 
199  if (bi->cur_ac >= 64)
200  break;
201 
202  /* Construct the next VLC */
203  prev = bi->cur_ac;
204  bi->cur_ac = bi->next[prev];
205  if (bi->cur_ac < 64) {
206  size = dv_rl2vlc(bi->cur_ac - prev - 1, bi->mb[bi->cur_ac],
207  bi->sign[bi->cur_ac], &vlc);
208  } else {
209  size = 4;
210  vlc = 6; /* End Of Block stamp */
211  }
212  }
213  return pb;
214 }
215 
217  ptrdiff_t linesize)
218 {
219  if (s->avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) {
220  int ps = s->ildct_cmp(NULL, data, NULL, linesize, 8) - 400;
221  if (ps > 0) {
222  int is = s->ildct_cmp(NULL, data, NULL, linesize * 2, 4) +
223  s->ildct_cmp(NULL, data + linesize, NULL, linesize * 2, 4);
224  return ps > is;
225  }
226  }
227 
228  return 0;
229 }
230 
231 static const int dv_weight_bits = 18;
232 static const int dv_weight_88[64] = {
233  131072, 257107, 257107, 242189, 252167, 242189, 235923, 237536,
234  237536, 235923, 229376, 231390, 223754, 231390, 229376, 222935,
235  224969, 217965, 217965, 224969, 222935, 200636, 218652, 211916,
236  212325, 211916, 218652, 200636, 188995, 196781, 205965, 206433,
237  206433, 205965, 196781, 188995, 185364, 185364, 200636, 200704,
238  200636, 185364, 185364, 174609, 180568, 195068, 195068, 180568,
239  174609, 170091, 175557, 189591, 175557, 170091, 165371, 170627,
240  170627, 165371, 160727, 153560, 160727, 144651, 144651, 136258,
241 };
242 static const int dv_weight_248[64] = {
243  131072, 262144, 257107, 257107, 242189, 242189, 242189, 242189,
244  237536, 237536, 229376, 229376, 200636, 200636, 224973, 224973,
245  223754, 223754, 235923, 235923, 229376, 229376, 217965, 217965,
246  211916, 211916, 196781, 196781, 185364, 185364, 206433, 206433,
247  211916, 211916, 222935, 222935, 200636, 200636, 205964, 205964,
248  200704, 200704, 180568, 180568, 175557, 175557, 195068, 195068,
249  185364, 185364, 188995, 188995, 174606, 174606, 175557, 175557,
250  170627, 170627, 153560, 153560, 165371, 165371, 144651, 144651,
251 };
252 
253 /* setting this to 1 results in a faster codec but
254  * somewhat lower image quality */
255 #define DV100_SACRIFICE_QUALITY_FOR_SPEED 1
256 #define DV100_ENABLE_FINER 1
257 
258 /* pack combination of QNO and CNO into a single 8-bit value */
259 #define DV100_MAKE_QLEVEL(qno,cno) ((qno<<2) | (cno))
260 #define DV100_QLEVEL_QNO(qlevel) (qlevel>>2)
261 #define DV100_QLEVEL_CNO(qlevel) (qlevel&0x3)
262 
263 #define DV100_NUM_QLEVELS 31
264 
265 /* The quantization step is determined by a combination of QNO and
266  CNO. We refer to these combinations as "qlevels" (this term is our
267  own, it's not mentioned in the spec). We use CNO, a multiplier on
268  the quantization step, to "fill in the gaps" between quantization
269  steps associated with successive values of QNO. e.g. there is no
270  QNO for a quantization step of 10, but we can use QNO=5 CNO=1 to
271  get the same result. The table below encodes combinations of QNO
272  and CNO in order of increasing quantization coarseness. */
274  DV100_MAKE_QLEVEL( 1,0), // 1*1= 1
275  DV100_MAKE_QLEVEL( 1,0), // 1*1= 1
276  DV100_MAKE_QLEVEL( 2,0), // 2*1= 2
277  DV100_MAKE_QLEVEL( 3,0), // 3*1= 3
278  DV100_MAKE_QLEVEL( 4,0), // 4*1= 4
279  DV100_MAKE_QLEVEL( 5,0), // 5*1= 5
280  DV100_MAKE_QLEVEL( 6,0), // 6*1= 6
281  DV100_MAKE_QLEVEL( 7,0), // 7*1= 7
282  DV100_MAKE_QLEVEL( 8,0), // 8*1= 8
283  DV100_MAKE_QLEVEL( 5,1), // 5*2=10
284  DV100_MAKE_QLEVEL( 6,1), // 6*2=12
285  DV100_MAKE_QLEVEL( 7,1), // 7*2=14
286  DV100_MAKE_QLEVEL( 9,0), // 16*1=16
287  DV100_MAKE_QLEVEL(10,0), // 18*1=18
288  DV100_MAKE_QLEVEL(11,0), // 20*1=20
289  DV100_MAKE_QLEVEL(12,0), // 22*1=22
290  DV100_MAKE_QLEVEL(13,0), // 24*1=24
291  DV100_MAKE_QLEVEL(14,0), // 28*1=28
292  DV100_MAKE_QLEVEL( 9,1), // 16*2=32
293  DV100_MAKE_QLEVEL(10,1), // 18*2=36
294  DV100_MAKE_QLEVEL(11,1), // 20*2=40
295  DV100_MAKE_QLEVEL(12,1), // 22*2=44
296  DV100_MAKE_QLEVEL(13,1), // 24*2=48
297  DV100_MAKE_QLEVEL(15,0), // 52*1=52
298  DV100_MAKE_QLEVEL(14,1), // 28*2=56
299  DV100_MAKE_QLEVEL( 9,2), // 16*4=64
300  DV100_MAKE_QLEVEL(10,2), // 18*4=72
301  DV100_MAKE_QLEVEL(11,2), // 20*4=80
302  DV100_MAKE_QLEVEL(12,2), // 22*4=88
303  DV100_MAKE_QLEVEL(13,2), // 24*4=96
304  // ...
305  DV100_MAKE_QLEVEL(15,3), // 52*8=416
306 };
307 
308 static const int dv100_min_bias = 0;
309 static const int dv100_chroma_bias = 0;
310 static const int dv100_starting_qno = 1;
311 
312 #if DV100_SACRIFICE_QUALITY_FOR_SPEED
313 static const int dv100_qlevel_inc = 4;
314 #else
315 static const int dv100_qlevel_inc = 1;
316 #endif
317 
318 // 1/qstep, shifted up by 16 bits
319 static const int dv100_qstep_bits = 16;
320 static const int dv100_qstep_inv[16] = {
321  65536, 65536, 32768, 21845, 16384, 13107, 10923, 9362, 8192, 4096, 3641, 3277, 2979, 2731, 2341, 1260,
322 };
323 
324 /* DV100 weights are pre-zigzagged, inverted and multiplied by 2^16
325  (in DV100 the AC components are divided by the spec weights) */
326 static const int dv_weight_1080[2][64] = {
327  { 8192, 65536, 65536, 61681, 61681, 61681, 58254, 58254,
328  58254, 58254, 58254, 58254, 55188, 58254, 58254, 55188,
329  55188, 55188, 55188, 55188, 55188, 24966, 27594, 26214,
330  26214, 26214, 27594, 24966, 23831, 24385, 25575, 25575,
331  25575, 25575, 24385, 23831, 23302, 23302, 24966, 24966,
332  24966, 23302, 23302, 21845, 22795, 24385, 24385, 22795,
333  21845, 21400, 21845, 23831, 21845, 21400, 10382, 10700,
334  10700, 10382, 10082, 9620, 10082, 9039, 9039, 8525, },
335  { 8192, 65536, 65536, 61681, 61681, 61681, 41943, 41943,
336  41943, 41943, 40330, 41943, 40330, 41943, 40330, 40330,
337  40330, 38836, 38836, 40330, 40330, 24966, 27594, 26214,
338  26214, 26214, 27594, 24966, 23831, 24385, 25575, 25575,
339  25575, 25575, 24385, 23831, 11523, 11523, 12483, 12483,
340  12483, 11523, 11523, 10923, 11275, 12193, 12193, 11275,
341  10923, 5323, 5490, 5924, 5490, 5323, 5165, 5323,
342  5323, 5165, 5017, 4788, 5017, 4520, 4520, 4263, }
343 };
344 
345 static const int dv_weight_720[2][64] = {
346  { 8192, 65536, 65536, 61681, 61681, 61681, 58254, 58254,
347  58254, 58254, 58254, 58254, 55188, 58254, 58254, 55188,
348  55188, 55188, 55188, 55188, 55188, 24966, 27594, 26214,
349  26214, 26214, 27594, 24966, 23831, 24385, 25575, 25575,
350  25575, 25575, 24385, 23831, 15420, 15420, 16644, 16644,
351  16644, 15420, 15420, 10923, 11398, 12193, 12193, 11398,
352  10923, 10700, 10923, 11916, 10923, 10700, 5191, 5350,
353  5350, 5191, 5041, 4810, 5041, 4520, 4520, 4263, },
354  { 8192, 43691, 43691, 40330, 40330, 40330, 29127, 29127,
355  29127, 29127, 29127, 29127, 27594, 29127, 29127, 27594,
356  27594, 27594, 27594, 27594, 27594, 12483, 13797, 13107,
357  13107, 13107, 13797, 12483, 11916, 12193, 12788, 12788,
358  12788, 12788, 12193, 11916, 5761, 5761, 6242, 6242,
359  6242, 5761, 5761, 5461, 5638, 5461, 6096, 5638,
360  5461, 2661, 2745, 2962, 2745, 2661, 2583, 2661,
361  2661, 2583, 2509, 2394, 2509, 2260, 2260, 2131, }
362 };
363 
365  int16_t *blk, EncBlockInfo *bi,
366  const uint8_t *zigzag_scan,
367  const int *weight, int bias)
368 {
369  int i, area;
370  /* We offer two different methods for class number assignment: the
371  * method suggested in SMPTE 314M Table 22, and an improved
372  * method. The SMPTE method is very conservative; it assigns class
373  * 3 (i.e. severe quantization) to any block where the largest AC
374  * component is greater than 36. FFmpeg's DV encoder tracks AC bit
375  * consumption precisely, so there is no need to bias most blocks
376  * towards strongly lossy compression. Instead, we assign class 2
377  * to most blocks, and use class 3 only when strictly necessary
378  * (for blocks whose largest AC component exceeds 255). */
379 
380 #if 0 /* SMPTE spec method */
381  static const int classes[] = { 12, 24, 36, 0xffff };
382 #else /* improved FFmpeg method */
383  static const int classes[] = { -1, -1, 255, 0xffff };
384 #endif
385  int max = classes[0];
386  int prev = 0;
387  const unsigned deadzone = s->quant_deadzone;
388  const unsigned threshold = 2 * deadzone;
389 
390  bi->mb[0] = blk[0];
391 
392  for (area = 0; area < 4; area++) {
393  bi->prev[area] = prev;
394  bi->bit_size[area] = 1; // 4 areas 4 bits for EOB :)
395  for (i = mb_area_start[area]; i < mb_area_start[area + 1]; i++) {
396  int level = blk[zigzag_scan[i]];
397 
398  if (level + deadzone > threshold) {
399  bi->sign[i] = (level >> 31) & 1;
400  /* Weight it and shift down into range, adding for rounding.
401  * The extra division by a factor of 2^4 reverses the 8x
402  * expansion of the DCT AND the 2x doubling of the weights. */
403  level = (FFABS(level) * weight[i] + (1 << (dv_weight_bits + 3))) >>
404  (dv_weight_bits + 4);
405  if (!level)
406  continue;
407  bi->mb[i] = level;
408  if (level > max)
409  max = level;
410  bi->bit_size[area] += dv_rl2vlc_size(i - prev - 1, level);
411  bi->next[prev] = i;
412  prev = i;
413  }
414  }
415  }
416  bi->next[prev] = i;
417  for (bi->cno = 0; max > classes[bi->cno]; bi->cno++)
418  ;
419 
420  bi->cno += bias;
421 
422  if (bi->cno >= 3) {
423  bi->cno = 3;
424  prev = 0;
425  i = bi->next[prev];
426  for (area = 0; area < 4; area++) {
427  bi->prev[area] = prev;
428  bi->bit_size[area] = 1; // 4 areas 4 bits for EOB :)
429  for (; i < mb_area_start[area + 1]; i = bi->next[i]) {
430  bi->mb[i] >>= 1;
431 
432  if (bi->mb[i]) {
433  bi->bit_size[area] += dv_rl2vlc_size(i - prev - 1, bi->mb[i]);
434  bi->next[prev] = i;
435  prev = i;
436  }
437  }
438  }
439  bi->next[prev] = i;
440  }
441 
442  return bi->bit_size[0] + bi->bit_size[1] +
443  bi->bit_size[2] + bi->bit_size[3];
444 }
445 
446 /* this function just copies the DCT coefficients and performs
447  the initial (non-)quantization. */
449  int16_t *blk, EncBlockInfo *bi,
450  const uint8_t *zigzag_scan,
451  const int *weight, int bias)
452 {
453  int i, max = 0;
454 
455  /* the first quantization (none at all) */
456  bi->area_q[0] = 1;
457 
458  /* weigh AC components and store to save[] */
459  /* (i=0 is the DC component; we only include it to make the
460  number of loop iterations even, for future possible SIMD optimization) */
461  for (i = 0; i < 64; i += 2) {
462  int level0, level1;
463 
464  /* get the AC component (in zig-zag order) */
465  level0 = blk[zigzag_scan[i+0]];
466  level1 = blk[zigzag_scan[i+1]];
467 
468  /* extract sign and make it the lowest bit */
469  bi->sign[i+0] = (level0>>31)&1;
470  bi->sign[i+1] = (level1>>31)&1;
471 
472  /* take absolute value of the level */
473  level0 = FFABS(level0);
474  level1 = FFABS(level1);
475 
476  /* weigh it */
477  level0 = (level0*weight[i+0] + 4096 + (1<<17)) >> 18;
478  level1 = (level1*weight[i+1] + 4096 + (1<<17)) >> 18;
479 
480  /* save unquantized value */
481  bi->save[i+0] = level0;
482  bi->save[i+1] = level1;
483 
484  /* find max component */
485  if (bi->save[i+0] > max)
486  max = bi->save[i+0];
487  if (bi->save[i+1] > max)
488  max = bi->save[i+1];
489  }
490 
491  /* copy DC component */
492  bi->mb[0] = blk[0];
493 
494  /* the EOB code is 4 bits */
495  bi->bit_size[0] = 4;
496  bi->bit_size[1] = bi->bit_size[2] = bi->bit_size[3] = 0;
497 
498  /* ensure that no AC coefficients are cut off */
499  bi->min_qlevel = ((max+256) >> 8);
500 
501  bi->area_q[0] = 25; /* set to an "impossible" value */
502  bi->cno = 0;
503 }
504 
506  DVVideoContext *s, int chroma)
507 {
508  LOCAL_ALIGNED_16(int16_t, blk, [64]);
509 
510  bi->area_q[0] = bi->area_q[1] = bi->area_q[2] = bi->area_q[3] = 0;
511  bi->partial_bit_count = 0;
512  bi->partial_bit_buffer = 0;
513  bi->cur_ac = 0;
514 
515  if (data) {
516  if (DV_PROFILE_IS_HD(s->sys)) {
517  s->get_pixels(blk, data, linesize * (1 << bi->dct_mode));
518  s->fdct[0](blk);
519  } else {
520  bi->dct_mode = dv_guess_dct_mode(s, data, linesize);
521  s->get_pixels(blk, data, linesize);
522  s->fdct[bi->dct_mode](blk);
523  }
524  } else {
525  /* We rely on the fact that encoding all zeros leads to an immediate EOB,
526  which is precisely what the spec calls for in the "dummy" blocks. */
527  memset(blk, 0, 64*sizeof(*blk));
528  bi->dct_mode = 0;
529  }
530 
531  if (DV_PROFILE_IS_HD(s->sys)) {
532  const int *weights;
533  if (s->sys->height == 1080) {
535  } else { /* 720p */
537  }
540  weights,
542  } else {
546  chroma);
547  }
548 
549  return bi->bit_size[0] + bi->bit_size[1] + bi->bit_size[2] + bi->bit_size[3];
550 }
551 
552 /* DV100 quantize
553  Perform quantization by divinding the AC component by the qstep.
554  As an optimization we use a fixed-point integer multiply instead
555  of a divide. */
556 static av_always_inline int dv100_quantize(int level, int qsinv)
557 {
558  /* this code is equivalent to */
559  /* return (level + qs/2) / qs; */
560 
561  return (level * qsinv + 1024 + (1<<(dv100_qstep_bits-1))) >> dv100_qstep_bits;
562 
563  /* the extra +1024 is needed to make the rounding come out right. */
564 
565  /* I (DJM) have verified that the results are exactly the same as
566  division for level 0-2048 at all QNOs. */
567 }
568 
569 static int dv100_actual_quantize(EncBlockInfo *b, int qlevel)
570 {
571  int prev, k, qsinv;
572 
573  int qno = DV100_QLEVEL_QNO(dv100_qlevels[qlevel]);
574  int cno = DV100_QLEVEL_CNO(dv100_qlevels[qlevel]);
575 
576  if (b->area_q[0] == qno && b->cno == cno)
577  return b->bit_size[0];
578 
579  qsinv = dv100_qstep_inv[qno];
580 
581  /* record the new qstep */
582  b->area_q[0] = qno;
583  b->cno = cno;
584 
585  /* reset encoded size (EOB = 4 bits) */
586  b->bit_size[0] = 4;
587 
588  /* visit nonzero components and quantize */
589  prev = 0;
590  for (k = 1; k < 64; k++) {
591  /* quantize */
592  int ac = dv100_quantize(b->save[k], qsinv) >> cno;
593  if (ac) {
594  if (ac > 255)
595  ac = 255;
596  b->mb[k] = ac;
597  b->bit_size[0] += dv_rl2vlc_size(k - prev - 1, ac);
598  b->next[prev] = k;
599  prev = k;
600  }
601  }
602  b->next[prev] = k;
603 
604  return b->bit_size[0];
605 }
606 
607 static inline void dv_guess_qnos_hd(EncBlockInfo *blks, int *qnos)
608 {
609  EncBlockInfo *b;
610  int min_qlevel[5];
611  int qlevels[5];
612  int size[5];
613  int i, j;
614  /* cache block sizes at hypothetical qlevels */
615  uint16_t size_cache[5*8][DV100_NUM_QLEVELS] = {{0}};
616 
617  /* get minimum qlevels */
618  for (i = 0; i < 5; i++) {
619  min_qlevel[i] = 1;
620  for (j = 0; j < 8; j++) {
621  if (blks[8*i+j].min_qlevel > min_qlevel[i])
622  min_qlevel[i] = blks[8*i+j].min_qlevel;
623  }
624  }
625 
626  /* initialize sizes */
627  for (i = 0; i < 5; i++) {
628  qlevels[i] = dv100_starting_qno;
629  if (qlevels[i] < min_qlevel[i])
630  qlevels[i] = min_qlevel[i];
631 
632  qnos[i] = DV100_QLEVEL_QNO(dv100_qlevels[qlevels[i]]);
633  size[i] = 0;
634  for (j = 0; j < 8; j++) {
635  size_cache[8*i+j][qlevels[i]] = dv100_actual_quantize(&blks[8*i+j], qlevels[i]);
636  size[i] += size_cache[8*i+j][qlevels[i]];
637  }
638  }
639 
640  /* must we go coarser? */
641  if (size[0]+size[1]+size[2]+size[3]+size[4] > vs_total_ac_bits_hd) {
642  int largest = size[0] % 5; /* 'random' number */
643  int qlevels_done = 0;
644 
645  do {
646  /* find the macroblock with the lowest qlevel */
647  for (i = 0; i < 5; i++) {
648  if (qlevels[i] < qlevels[largest])
649  largest = i;
650  }
651 
652  i = largest;
653  /* ensure that we don't enter infinite loop */
654  largest = (largest+1) % 5;
655 
656  /* quantize a little bit more */
657  qlevels[i] += dv100_qlevel_inc;
658  if (qlevels[i] > DV100_NUM_QLEVELS-1) {
659  qlevels[i] = DV100_NUM_QLEVELS-1;
660  qlevels_done++;
661  }
662 
663  qnos[i] = DV100_QLEVEL_QNO(dv100_qlevels[qlevels[i]]);
664  size[i] = 0;
665 
666  /* for each block */
667  b = &blks[8*i];
668  for (j = 0; j < 8; j++, b++) {
669  /* accumulate block size into macroblock */
670  if(size_cache[8*i+j][qlevels[i]] == 0) {
671  /* it is safe to use actual_quantize() here because we only go from finer to coarser,
672  and it saves the final actual_quantize() down below */
673  size_cache[8*i+j][qlevels[i]] = dv100_actual_quantize(b, qlevels[i]);
674  }
675  size[i] += size_cache[8*i+j][qlevels[i]];
676  } /* for each block */
677 
678  } while (vs_total_ac_bits_hd < size[0] + size[1] + size[2] + size[3] + size[4] && qlevels_done < 5);
679 
680  // can we go finer?
681  } else if (DV100_ENABLE_FINER &&
682  size[0]+size[1]+size[2]+size[3]+size[4] < vs_total_ac_bits_hd) {
683  int save_qlevel;
684  int largest = size[0] % 5; /* 'random' number */
685 
686  while (qlevels[0] > min_qlevel[0] ||
687  qlevels[1] > min_qlevel[1] ||
688  qlevels[2] > min_qlevel[2] ||
689  qlevels[3] > min_qlevel[3] ||
690  qlevels[4] > min_qlevel[4]) {
691 
692  /* find the macroblock with the highest qlevel */
693  for (i = 0; i < 5; i++) {
694  if (qlevels[i] > min_qlevel[i] && qlevels[i] > qlevels[largest])
695  largest = i;
696  }
697 
698  i = largest;
699 
700  /* ensure that we don't enter infinite loop */
701  largest = (largest+1) % 5;
702 
703  if (qlevels[i] <= min_qlevel[i]) {
704  /* can't unquantize any more */
705  continue;
706  }
707  /* quantize a little bit less */
708  save_qlevel = qlevels[i];
709  qlevels[i] -= dv100_qlevel_inc;
710  if (qlevels[i] < min_qlevel[i])
711  qlevels[i] = min_qlevel[i];
712 
713  qnos[i] = DV100_QLEVEL_QNO(dv100_qlevels[qlevels[i]]);
714 
715  size[i] = 0;
716 
717  /* for each block */
718  b = &blks[8*i];
719  for (j = 0; j < 8; j++, b++) {
720  /* accumulate block size into macroblock */
721  if(size_cache[8*i+j][qlevels[i]] == 0) {
722  size_cache[8*i+j][qlevels[i]] = dv100_actual_quantize(b, qlevels[i]);
723  }
724  size[i] += size_cache[8*i+j][qlevels[i]];
725  } /* for each block */
726 
727  /* did we bust the limit? */
728  if (vs_total_ac_bits_hd < size[0] + size[1] + size[2] + size[3] + size[4]) {
729  /* go back down and exit */
730  qlevels[i] = save_qlevel;
731  qnos[i] = DV100_QLEVEL_QNO(dv100_qlevels[qlevels[i]]);
732  break;
733  }
734  }
735  }
736 
737  /* now do the actual quantization */
738  for (i = 0; i < 5; i++) {
739  /* for each block */
740  b = &blks[8*i];
741  size[i] = 0;
742  for (j = 0; j < 8; j++, b++) {
743  /* accumulate block size into macroblock */
744  size[i] += dv100_actual_quantize(b, qlevels[i]);
745  } /* for each block */
746  }
747 }
748 
749 static inline void dv_guess_qnos(EncBlockInfo *blks, int *qnos)
750 {
751  int size[5];
752  int i, j, k, a, prev, a2;
753  EncBlockInfo *b;
754 
755  size[0] =
756  size[1] =
757  size[2] =
758  size[3] =
759  size[4] = 1 << 24;
760  do {
761  b = blks;
762  for (i = 0; i < 5; i++) {
763  if (!qnos[i])
764  continue;
765 
766  qnos[i]--;
767  size[i] = 0;
768  for (j = 0; j < 6; j++, b++) {
769  for (a = 0; a < 4; a++) {
770  if (b->area_q[a] != ff_dv_quant_shifts[qnos[i] + ff_dv_quant_offset[b->cno]][a]) {
771  b->bit_size[a] = 1; // 4 areas 4 bits for EOB :)
772  b->area_q[a]++;
773  prev = b->prev[a];
774  av_assert2(b->next[prev] >= mb_area_start[a + 1] || b->mb[prev]);
775  for (k = b->next[prev]; k < mb_area_start[a + 1]; k = b->next[k]) {
776  b->mb[k] >>= 1;
777  if (b->mb[k]) {
778  b->bit_size[a] += dv_rl2vlc_size(k - prev - 1, b->mb[k]);
779  prev = k;
780  } else {
781  if (b->next[k] >= mb_area_start[a + 1] && b->next[k] < 64) {
782  for (a2 = a + 1; b->next[k] >= mb_area_start[a2 + 1]; a2++)
783  b->prev[a2] = prev;
784  av_assert2(a2 < 4);
785  av_assert2(b->mb[b->next[k]]);
786  b->bit_size[a2] += dv_rl2vlc_size(b->next[k] - prev - 1, b->mb[b->next[k]]) -
787  dv_rl2vlc_size(b->next[k] - k - 1, b->mb[b->next[k]]);
788  av_assert2(b->prev[a2] == k && (a2 + 1 >= 4 || b->prev[a2 + 1] != k));
789  b->prev[a2] = prev;
790  }
791  b->next[prev] = b->next[k];
792  }
793  }
794  b->prev[a + 1] = prev;
795  }
796  size[i] += b->bit_size[a];
797  }
798  }
799  if (vs_total_ac_bits >= size[0] + size[1] + size[2] + size[3] + size[4])
800  return;
801  }
802  } while (qnos[0] | qnos[1] | qnos[2] | qnos[3] | qnos[4]);
803 
804  for (a = 2; a == 2 || vs_total_ac_bits < size[0]; a += a) {
805  b = blks;
806  size[0] = 5 * 6 * 4; // EOB
807  for (j = 0; j < 6 * 5; j++, b++) {
808  prev = b->prev[0];
809  for (k = b->next[prev]; k < 64; k = b->next[k]) {
810  if (b->mb[k] < a && b->mb[k] > -a) {
811  b->next[prev] = b->next[k];
812  } else {
813  size[0] += dv_rl2vlc_size(k - prev - 1, b->mb[k]);
814  prev = k;
815  }
816  }
817  }
818  }
819 }
820 
821 /* update all cno values into the blocks, over-writing the old values without
822  touching anything else. (only used for DV100) */
823 static inline void dv_revise_cnos(uint8_t *dif, EncBlockInfo *blk, const AVDVProfile *profile)
824 {
825  uint8_t *data;
826  int mb_index, i;
827 
828  for (mb_index = 0; mb_index < 5; mb_index++) {
829  data = dif + mb_index*80 + 4;
830  for (i = 0; i < profile->bpm; i++) {
831  /* zero out the class number */
832  data[1] &= 0xCF;
833  /* add the new one */
834  data[1] |= blk[profile->bpm*mb_index+i].cno << 4;
835 
836  data += profile->block_sizes[i] >> 3;
837  }
838  }
839 }
840 
841 static int dv_encode_video_segment(AVCodecContext *avctx, void *arg)
842 {
843  DVVideoContext *s = avctx->priv_data;
844  DVwork_chunk *work_chunk = arg;
845  int mb_index, i, j;
846  int mb_x, mb_y, c_offset;
847  ptrdiff_t linesize, y_stride;
848  uint8_t *y_ptr;
849  uint8_t *dif, *p;
850  LOCAL_ALIGNED_8(uint8_t, scratch, [128]);
851  EncBlockInfo enc_blks[5 * DV_MAX_BPM];
852  PutBitContext pbs[5 * DV_MAX_BPM];
853  PutBitContext *pb;
854  EncBlockInfo *enc_blk;
855  int vs_bit_size = 0;
856  int qnos[5];
857  int *qnosp = &qnos[0];
858 
859  p = dif = &s->buf[work_chunk->buf_offset * 80];
860  enc_blk = &enc_blks[0];
861  for (mb_index = 0; mb_index < 5; mb_index++) {
862  dv_calculate_mb_xy(s, work_chunk, mb_index, &mb_x, &mb_y);
863 
864  qnos[mb_index] = DV_PROFILE_IS_HD(s->sys) ? 1 : 15;
865 
866  y_ptr = s->frame->data[0] + (mb_y * s->frame->linesize[0] + mb_x) * 8;
867  linesize = s->frame->linesize[0];
868 
869  if (s->sys->height == 1080 && mb_y < 134)
870  enc_blk->dct_mode = dv_guess_dct_mode(s, y_ptr, linesize);
871  else
872  enc_blk->dct_mode = 0;
873  for (i = 1; i < 8; i++)
874  enc_blk[i].dct_mode = enc_blk->dct_mode;
875 
876  /* initializing luminance blocks */
877  if ((s->sys->pix_fmt == AV_PIX_FMT_YUV420P) ||
878  (s->sys->pix_fmt == AV_PIX_FMT_YUV411P && mb_x >= (704 / 8)) ||
879  (s->sys->height >= 720 && mb_y != 134)) {
880  y_stride = s->frame->linesize[0] * (1 << (3*!enc_blk->dct_mode));
881  } else {
882  y_stride = 16;
883  }
884  y_ptr = s->frame->data[0] +
885  (mb_y * s->frame->linesize[0] + mb_x) * 8;
886  linesize = s->frame->linesize[0];
887 
888  if (s->sys->video_stype == 4) { /* SD 422 */
889  vs_bit_size +=
890  dv_init_enc_block(enc_blk + 0, y_ptr, linesize, s, 0) +
891  dv_init_enc_block(enc_blk + 1, NULL, linesize, s, 0) +
892  dv_init_enc_block(enc_blk + 2, y_ptr + 8, linesize, s, 0) +
893  dv_init_enc_block(enc_blk + 3, NULL, linesize, s, 0);
894  } else {
895  vs_bit_size +=
896  dv_init_enc_block(enc_blk + 0, y_ptr, linesize, s, 0) +
897  dv_init_enc_block(enc_blk + 1, y_ptr + 8, linesize, s, 0) +
898  dv_init_enc_block(enc_blk + 2, y_ptr + y_stride, linesize, s, 0) +
899  dv_init_enc_block(enc_blk + 3, y_ptr + 8 + y_stride, linesize, s, 0);
900  }
901  enc_blk += 4;
902 
903  /* initializing chrominance blocks */
904  c_offset = ((mb_y >> (s->sys->pix_fmt == AV_PIX_FMT_YUV420P)) * s->frame->linesize[1] +
905  (mb_x >> ((s->sys->pix_fmt == AV_PIX_FMT_YUV411P) ? 2 : 1))) * 8;
906  for (j = 2; j; j--) {
907  uint8_t *c_ptr = s->frame->data[j] + c_offset;
908  linesize = s->frame->linesize[j];
909  y_stride = (mb_y == 134) ? 8 : (s->frame->linesize[j] * (1 << (3*!enc_blk->dct_mode)));
910  if (s->sys->pix_fmt == AV_PIX_FMT_YUV411P && mb_x >= (704 / 8)) {
911  uint8_t *d;
912  uint8_t *b = scratch;
913  for (i = 0; i < 8; i++) {
914  d = c_ptr + linesize * 8;
915  b[0] = c_ptr[0];
916  b[1] = c_ptr[1];
917  b[2] = c_ptr[2];
918  b[3] = c_ptr[3];
919  b[4] = d[0];
920  b[5] = d[1];
921  b[6] = d[2];
922  b[7] = d[3];
923  c_ptr += linesize;
924  b += 16;
925  }
926  c_ptr = scratch;
927  linesize = 16;
928  }
929 
930  vs_bit_size += dv_init_enc_block(enc_blk++, c_ptr, linesize, s, 1);
931  if (s->sys->bpm == 8)
932  vs_bit_size += dv_init_enc_block(enc_blk++, c_ptr + y_stride,
933  linesize, s, 1);
934  }
935  }
936 
937  if (DV_PROFILE_IS_HD(s->sys)) {
938  /* unconditional */
939  dv_guess_qnos_hd(&enc_blks[0], qnosp);
940  } else if (vs_total_ac_bits < vs_bit_size) {
941  dv_guess_qnos(&enc_blks[0], qnosp);
942  }
943 
944  /* DIF encoding process */
945  for (j = 0; j < 5 * s->sys->bpm;) {
946  int start_mb = j;
947 
948  p[3] = *qnosp++;
949  p += 4;
950 
951  /* First pass over individual cells only */
952  for (i = 0; i < s->sys->bpm; i++, j++) {
953  int sz = s->sys->block_sizes[i] >> 3;
954 
955  init_put_bits(&pbs[j], p, sz);
956  put_sbits(&pbs[j], 9, ((enc_blks[j].mb[0] >> 3) - 1024 + 2) >> 2);
957  put_bits(&pbs[j], 1, DV_PROFILE_IS_HD(s->sys) && i ? 1 : enc_blks[j].dct_mode);
958  put_bits(&pbs[j], 2, enc_blks[j].cno);
959 
960  dv_encode_ac(&enc_blks[j], &pbs[j], &pbs[j + 1]);
961  p += sz;
962  }
963 
964  /* Second pass over each MB space */
965  pb = &pbs[start_mb];
966  for (i = 0; i < s->sys->bpm; i++)
967  if (enc_blks[start_mb + i].partial_bit_count)
968  pb = dv_encode_ac(&enc_blks[start_mb + i], pb,
969  &pbs[start_mb + s->sys->bpm]);
970  }
971 
972  /* Third and final pass over the whole video segment space */
973  pb = &pbs[0];
974  for (j = 0; j < 5 * s->sys->bpm; j++) {
975  if (enc_blks[j].partial_bit_count)
976  pb = dv_encode_ac(&enc_blks[j], pb, &pbs[s->sys->bpm * 5]);
977  if (enc_blks[j].partial_bit_count)
978  av_log(avctx, AV_LOG_ERROR, "ac bitstream overflow\n");
979  }
980 
981  for (j = 0; j < 5 * s->sys->bpm; j++) {
982  int pos;
983  int size = pbs[j].size_in_bits >> 3;
984  flush_put_bits(&pbs[j]);
985  pos = put_bits_count(&pbs[j]) >> 3;
986  if (pos > size) {
987  av_log(avctx, AV_LOG_ERROR,
988  "bitstream written beyond buffer size\n");
989  return -1;
990  }
991  memset(pbs[j].buf + pos, 0xff, size - pos);
992  }
993 
994  if (DV_PROFILE_IS_HD(s->sys))
995  dv_revise_cnos(dif, enc_blks, s->sys);
996 
997  return 0;
998 }
999 
1000 static inline int dv_write_pack(enum dv_pack_type pack_id, DVVideoContext *c,
1001  uint8_t *buf)
1002 {
1003  /*
1004  * Here's what SMPTE314M says about these two:
1005  * (page 6) APTn, AP1n, AP2n, AP3n: These data shall be identical
1006  * as track application IDs (APTn = 001, AP1n =
1007  * 001, AP2n = 001, AP3n = 001), if the source signal
1008  * comes from a digital VCR. If the signal source is
1009  * unknown, all bits for these data shall be set to 1.
1010  * (page 12) STYPE: STYPE defines a signal type of video signal
1011  * 00000b = 4:1:1 compression
1012  * 00100b = 4:2:2 compression
1013  * XXXXXX = Reserved
1014  * Now, I've got two problems with these statements:
1015  * 1. it looks like APT == 111b should be a safe bet, but it isn't.
1016  * It seems that for PAL as defined in IEC 61834 we have to set
1017  * APT to 000 and for SMPTE314M to 001.
1018  * 2. It is not at all clear what STYPE is used for 4:2:0 PAL
1019  * compression scheme (if any).
1020  */
1021  uint8_t aspect = 0;
1022  int apt = (c->sys->pix_fmt == AV_PIX_FMT_YUV420P ? 0 : 1);
1023  int fs;
1024 
1025  if (c->avctx->height >= 720)
1026  fs = c->avctx->height == 720 || c->frame->top_field_first ? 0x40 : 0x00;
1027  else
1028  fs = c->frame->top_field_first ? 0x00 : 0x40;
1029 
1030  if (DV_PROFILE_IS_HD(c->sys) ||
1031  (int)(av_q2d(c->avctx->sample_aspect_ratio) *
1032  c->avctx->width / c->avctx->height * 10) >= 17)
1033  /* HD formats are always 16:9 */
1034  aspect = 0x02;
1035 
1036  buf[0] = (uint8_t) pack_id;
1037  switch (pack_id) {
1038  case dv_header525: /* I can't imagine why these two weren't defined as real */
1039  case dv_header625: /* packs in SMPTE314M -- they definitely look like ones */
1040  buf[1] = 0xf8 | /* reserved -- always 1 */
1041  (apt & 0x07); /* APT: Track application ID */
1042  buf[2] = (0 << 7) | /* TF1: audio data is 0 - valid; 1 - invalid */
1043  (0x0f << 3) | /* reserved -- always 1 */
1044  (apt & 0x07); /* AP1: Audio application ID */
1045  buf[3] = (0 << 7) | /* TF2: video data is 0 - valid; 1 - invalid */
1046  (0x0f << 3) | /* reserved -- always 1 */
1047  (apt & 0x07); /* AP2: Video application ID */
1048  buf[4] = (0 << 7) | /* TF3: subcode(SSYB) is 0 - valid; 1 - invalid */
1049  (0x0f << 3) | /* reserved -- always 1 */
1050  (apt & 0x07); /* AP3: Subcode application ID */
1051  break;
1052  case dv_video_source:
1053  buf[1] = 0xff; /* reserved -- always 1 */
1054  buf[2] = (1 << 7) | /* B/W: 0 - b/w, 1 - color */
1055  (1 << 6) | /* following CLF is valid - 0, invalid - 1 */
1056  (3 << 4) | /* CLF: color frames ID (see ITU-R BT.470-4) */
1057  0xf; /* reserved -- always 1 */
1058  buf[3] = (3 << 6) | /* reserved -- always 1 */
1059  (c->sys->dsf << 5) | /* system: 60fields/50fields */
1060  c->sys->video_stype; /* signal type video compression */
1061  buf[4] = 0xff; /* VISC: 0xff -- no information */
1062  break;
1063  case dv_video_control:
1064  buf[1] = (0 << 6) | /* Copy generation management (CGMS) 0 -- free */
1065  0x3f; /* reserved -- always 1 */
1066  buf[2] = 0xc8 | /* reserved -- always b11001xxx */
1067  aspect;
1068  buf[3] = (1 << 7) | /* frame/field flag 1 -- frame, 0 -- field */
1069  fs | /* first/second field flag 0 -- field 2, 1 -- field 1 */
1070  (1 << 5) | /* frame change flag 0 -- same picture as before, 1 -- different */
1071  (1 << 4) | /* 1 - interlaced, 0 - noninterlaced */
1072  0xc; /* reserved -- always b1100 */
1073  buf[4] = 0xff; /* reserved -- always 1 */
1074  break;
1075  default:
1076  buf[1] =
1077  buf[2] =
1078  buf[3] =
1079  buf[4] = 0xff;
1080  }
1081  return 5;
1082 }
1083 
1084 static inline int dv_write_dif_id(enum dv_section_type t, uint8_t chan_num,
1085  uint8_t seq_num, uint8_t dif_num,
1086  uint8_t *buf)
1087 {
1088  int fsc = chan_num & 1;
1089  int fsp = 1 - (chan_num >> 1);
1090 
1091  buf[0] = (uint8_t) t; /* Section type */
1092  buf[1] = (seq_num << 4) | /* DIF seq number 0-9 for 525/60; 0-11 for 625/50 */
1093  (fsc << 3) | /* FSC: for 50 and 100Mb/s 0 - first channel; 1 - second */
1094  (fsp << 2) | /* FSP: for 100Mb/s 1 - channels 0-1; 0 - channels 2-3 */
1095  3; /* reserved -- always 1 */
1096  buf[2] = dif_num; /* DIF block number Video: 0-134, Audio: 0-8 */
1097  return 3;
1098 }
1099 
1100 static inline int dv_write_ssyb_id(uint8_t syb_num, uint8_t fr, uint8_t *buf)
1101 {
1102  if (syb_num == 0 || syb_num == 6) {
1103  buf[0] = (fr << 7) | /* FR ID 1 - first half of each channel; 0 - second */
1104  (0 << 4) | /* AP3 (Subcode application ID) */
1105  0x0f; /* reserved -- always 1 */
1106  } else if (syb_num == 11) {
1107  buf[0] = (fr << 7) | /* FR ID 1 - first half of each channel; 0 - second */
1108  0x7f; /* reserved -- always 1 */
1109  } else {
1110  buf[0] = (fr << 7) | /* FR ID 1 - first half of each channel; 0 - second */
1111  (0 << 4) | /* APT (Track application ID) */
1112  0x0f; /* reserved -- always 1 */
1113  }
1114  buf[1] = 0xf0 | /* reserved -- always 1 */
1115  (syb_num & 0x0f); /* SSYB number 0 - 11 */
1116  buf[2] = 0xff; /* reserved -- always 1 */
1117  return 3;
1118 }
1119 
1121 {
1122  int chan, i, j, k;
1123  /* We work with 720p frames split in half. The odd half-frame is chan 2,3 */
1124  int chan_offset = 2*(c->sys->height == 720 && c->avctx->frame_number & 1);
1125 
1126  for (chan = 0; chan < c->sys->n_difchan; chan++) {
1127  for (i = 0; i < c->sys->difseg_size; i++) {
1128  memset(buf, 0xff, 80 * 6); /* first 6 DIF blocks are for control data */
1129 
1130  /* DV header: 1DIF */
1131  buf += dv_write_dif_id(dv_sect_header, chan+chan_offset, i, 0, buf);
1132  buf += dv_write_pack((c->sys->dsf ? dv_header625 : dv_header525),
1133  c, buf);
1134  buf += 72; /* unused bytes */
1135 
1136  /* DV subcode: 2DIFs */
1137  for (j = 0; j < 2; j++) {
1138  buf += dv_write_dif_id(dv_sect_subcode, chan+chan_offset, i, j, buf);
1139  for (k = 0; k < 6; k++)
1140  buf += dv_write_ssyb_id(k, (i < c->sys->difseg_size / 2), buf) + 5;
1141  buf += 29; /* unused bytes */
1142  }
1143 
1144  /* DV VAUX: 3DIFS */
1145  for (j = 0; j < 3; j++) {
1146  buf += dv_write_dif_id(dv_sect_vaux, chan+chan_offset, i, j, buf);
1147  buf += dv_write_pack(dv_video_source, c, buf);
1148  buf += dv_write_pack(dv_video_control, c, buf);
1149  buf += 7 * 5;
1150  buf += dv_write_pack(dv_video_source, c, buf);
1151  buf += dv_write_pack(dv_video_control, c, buf);
1152  buf += 4 * 5 + 2; /* unused bytes */
1153  }
1154 
1155  /* DV Audio/Video: 135 Video DIFs + 9 Audio DIFs */
1156  for (j = 0; j < 135; j++) {
1157  if (j % 15 == 0) {
1158  memset(buf, 0xff, 80);
1159  buf += dv_write_dif_id(dv_sect_audio, chan+chan_offset, i, j/15, buf);
1160  buf += 77; /* audio control & shuffled PCM audio */
1161  }
1162  buf += dv_write_dif_id(dv_sect_video, chan+chan_offset, i, j, buf);
1163  buf += 77; /* 1 video macroblock: 1 bytes control
1164  * 4 * 14 bytes Y 8x8 data
1165  * 10 bytes Cr 8x8 data
1166  * 10 bytes Cb 8x8 data */
1167  }
1168  }
1169  }
1170 }
1171 
1173  const AVFrame *frame, int *got_packet)
1174 {
1175  DVVideoContext *s = c->priv_data;
1176  int ret;
1177 
1178  if ((uintptr_t)frame->data[0] & 7 || frame->linesize[0] & 7 ||
1179  (uintptr_t)frame->data[1] & 7 || frame->linesize[1] & 7 ||
1180  (uintptr_t)frame->data[2] & 7 || frame->linesize[2] & 7)
1181  s->get_pixels = s->pdsp.get_pixels_unaligned;
1182  else
1183  s->get_pixels = s->pdsp.get_pixels;
1184 
1185  if ((ret = ff_alloc_packet2(c, pkt, s->sys->frame_size, 0)) < 0)
1186  return ret;
1187 
1188  c->pix_fmt = s->sys->pix_fmt;
1189  s->frame = frame;
1190 #if FF_API_CODED_FRAME
1192  c->coded_frame->key_frame = 1;
1193  c->coded_frame->pict_type = AV_PICTURE_TYPE_I;
1195 #endif
1196  s->buf = pkt->data;
1197 
1199 
1200  c->execute(c, dv_encode_video_segment, s->work_chunks, NULL,
1201  dv_work_pool_size(s->sys), sizeof(DVwork_chunk));
1202 
1203  emms_c();
1204 
1206  *got_packet = 1;
1207 
1208  return 0;
1209 }
1210 
1211 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1212 #define OFFSET(x) offsetof(DVVideoContext, x)
1213 static const AVOption dv_options[] = {
1214  { "quant_deadzone", "Quantizer dead zone", OFFSET(quant_deadzone), AV_OPT_TYPE_INT, { .i64 = 7 }, 0, 1024, VE },
1215  { NULL },
1216 };
1217 
1219  .class_name = "dvvideo encoder",
1220  .item_name = av_default_item_name,
1221  .option = dv_options,
1222  .version = LIBAVUTIL_VERSION_INT,
1223 };
1224 
1226  .name = "dvvideo",
1227  .long_name = NULL_IF_CONFIG_SMALL("DV (Digital Video)"),
1228  .type = AVMEDIA_TYPE_VIDEO,
1229  .id = AV_CODEC_ID_DVVIDEO,
1230  .priv_data_size = sizeof(DVVideoContext),
1232  .encode2 = dvvideo_encode_frame,
1234  .pix_fmts = (const enum AVPixelFormat[]) {
1237  },
1238  .priv_class = &dvvideo_encode_class,
1239 };
Macro definitions for various function/variable attributes.
#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
Libavcodec external API header.
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:31
#define is(width, name, range_min, range_max, subs,...)
Definition: cbs_h2645.c:296
#define s(width, name)
Definition: cbs_vp9.c:257
#define fs(width, name, subs,...)
Definition: cbs_vp9.c:259
#define av_mod_uintp2
Definition: common.h:149
#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
#define max(a, b)
Definition: cuda_runtime.h:33
static AVFrame * frame
const AVDVProfile * av_dv_codec_profile2(int width, int height, enum AVPixelFormat pix_fmt, AVRational frame_rate)
Get a DV profile for the provided stream parameters.
Definition: dv_profile.c:314
void ff_dv_print_profiles(void *logctx, int loglevel)
Print all allowed DV profiles into logctx at specified logging level.
static av_cold void dv_vlc_map_tableinit(void)
Definition: dv_tablegen.h:51
#define DV_VLC_MAP_RUN_SIZE
Definition: dv_tablegen.h:35
#define DV_VLC_MAP_LEV_SIZE
Definition: dv_tablegen.h:36
static struct dv_vlc_pair dv_vlc_map[DV_VLC_MAP_RUN_SIZE][DV_VLC_MAP_LEV_SIZE]
Definition: dv_tablegen.h:49
const uint8_t ff_dv_quant_offset[4]
Definition: dvdata.c:70
const uint8_t ff_dv_zigzag248_direct[64]
Definition: dvdata.c:33
const uint8_t ff_dv_quant_shifts[22][4]
Definition: dvdata.c:45
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:34
av_cold void ff_fdctdsp_init(FDCTDSPContext *c, AVCodecContext *avctx)
Definition: fdctdsp.c:26
int
@ AV_OPT_TYPE_INT
Definition: opt.h:225
#define AV_CODEC_FLAG_INTERLACED_DCT
Use interlaced DCT.
Definition: avcodec.h:321
#define AV_CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
Definition: codec.h:112
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: codec.h:108
@ AV_CODEC_ID_DVVIDEO
Definition: codec_id.h:73
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:410
#define AVERROR(e)
Definition: error.h:43
#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
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition: rational.h:104
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
static const int weights[]
Definition: hevc_pel.c:32
int i
Definition: input.c:407
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:218
static int weight(int i, int blen, int offset)
Definition: diracdec.c:1563
av_cold int ff_dvvideo_init(AVCodecContext *avctx)
Definition: dv.c:198
int ff_dv_init_dynamic_tables(DVVideoContext *ctx, const AVDVProfile *d)
Definition: dv.c:175
static void dv_calculate_mb_xy(DVVideoContext *s, DVwork_chunk *work_chunk, int m, int *mb_x, int *mb_y)
Definition: dv.h:120
dv_pack_type
Definition: dv.h:72
@ dv_header525
Definition: dv.h:73
@ dv_video_source
Definition: dv.h:80
@ dv_video_control
Definition: dv.h:81
@ dv_header625
Definition: dv.h:74
#define DV_MAX_BPM
maximum number of blocks per macroblock in any DV format
Definition: dv.h:100
static int dv_work_pool_size(const AVDVProfile *d)
Definition: dv.h:110
#define DV_PROFILE_IS_HD(p)
Definition: dv.h:87
dv_section_type
Definition: dv.h:64
@ dv_sect_header
Definition: dv.h:65
@ dv_sect_audio
Definition: dv.h:68
@ dv_sect_video
Definition: dv.h:69
@ dv_sect_vaux
Definition: dv.h:67
@ dv_sect_subcode
Definition: dv.h:66
#define DV100_ENABLE_FINER
Definition: dvenc.c:256
static av_cold int dvvideo_encode_init(AVCodecContext *avctx)
Definition: dvenc.c:48
static av_always_inline int dv100_quantize(int level, int qsinv)
Definition: dvenc.c:556
static int dv100_actual_quantize(EncBlockInfo *b, int qlevel)
Definition: dvenc.c:569
static int dv_write_ssyb_id(uint8_t syb_num, uint8_t fr, uint8_t *buf)
Definition: dvenc.c:1100
static av_always_inline int dv_set_class_number_sd(DVVideoContext *s, int16_t *blk, EncBlockInfo *bi, const uint8_t *zigzag_scan, const int *weight, int bias)
Definition: dvenc.c:364
static int dv_write_dif_id(enum dv_section_type t, uint8_t chan_num, uint8_t seq_num, uint8_t dif_num, uint8_t *buf)
Definition: dvenc.c:1084
static av_always_inline PutBitContext * dv_encode_ac(EncBlockInfo *bi, PutBitContext *pb_pool, PutBitContext *pb_end)
Definition: dvenc.c:170
#define VE
Definition: dvenc.c:1211
static const int dv100_qstep_inv[16]
Definition: dvenc.c:320
static const int dv100_starting_qno
Definition: dvenc.c:310
static av_always_inline int dv_init_enc_block(EncBlockInfo *bi, uint8_t *data, int linesize, DVVideoContext *s, int chroma)
Definition: dvenc.c:505
static void dv_set_class_number_hd(DVVideoContext *s, int16_t *blk, EncBlockInfo *bi, const uint8_t *zigzag_scan, const int *weight, int bias)
Definition: dvenc.c:448
static const AVClass dvvideo_encode_class
Definition: dvenc.c:1218
static av_always_inline int dv_rl2vlc_size(int run, int l)
Definition: dvenc.c:145
static const int vs_total_ac_bits
Definition: dvenc.c:95
static const int vs_total_ac_bits_hd
Definition: dvenc.c:94
static const int dv100_chroma_bias
Definition: dvenc.c:309
static const uint8_t dv100_qlevels[DV100_NUM_QLEVELS]
Definition: dvenc.c:273
static void dv_revise_cnos(uint8_t *dif, EncBlockInfo *blk, const AVDVProfile *profile)
Definition: dvenc.c:823
#define DV100_MAKE_QLEVEL(qno, cno)
Definition: dvenc.c:259
#define DV100_QLEVEL_CNO(qlevel)
Definition: dvenc.c:261
#define DV100_NUM_QLEVELS
Definition: dvenc.c:263
static const int dv100_min_bias
Definition: dvenc.c:308
static const int dv_weight_248[64]
Definition: dvenc.c:242
AVCodec ff_dvvideo_encoder
Definition: dvenc.c:1225
static int dv_encode_video_segment(AVCodecContext *avctx, void *arg)
Definition: dvenc.c:841
static const int dv_weight_88[64]
Definition: dvenc.c:232
static const AVOption dv_options[]
Definition: dvenc.c:1213
static const int dv_weight_1080[2][64]
Definition: dvenc.c:326
static int dvvideo_encode_frame(AVCodecContext *c, AVPacket *pkt, const AVFrame *frame, int *got_packet)
Definition: dvenc.c:1172
static av_always_inline int dv_rl2vlc(int run, int l, int sign, uint32_t *vlc)
Definition: dvenc.c:139
static const int dv100_qstep_bits
Definition: dvenc.c:319
static void dv_guess_qnos(EncBlockInfo *blks, int *qnos)
Definition: dvenc.c:749
static const int dv100_qlevel_inc
Definition: dvenc.c:313
static av_always_inline int dv_guess_dct_mode(DVVideoContext *s, uint8_t *data, ptrdiff_t linesize)
Definition: dvenc.c:216
#define OFFSET(x)
Definition: dvenc.c:1212
static void dv_format_frame(DVVideoContext *c, uint8_t *buf)
Definition: dvenc.c:1120
static int dv_write_pack(enum dv_pack_type pack_id, DVVideoContext *c, uint8_t *buf)
Definition: dvenc.c:1000
static const int dv_weight_720[2][64]
Definition: dvenc.c:345
static void dv_guess_qnos_hd(EncBlockInfo *blks, int *qnos)
Definition: dvenc.c:607
#define DV100_QLEVEL_QNO(qlevel)
Definition: dvenc.c:260
static const int mb_area_start[5]
Definition: dvenc.c:96
static const int dv_weight_bits
Definition: dvenc.c:231
const char * arg
Definition: jacosubdec.c:67
av_cold void ff_pixblockdsp_init(PixblockDSPContext *c, AVCodecContext *avctx)
Definition: pixblockdsp.c:81
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
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:83
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:84
#define emms_c()
Definition: internal.h:54
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:309
const uint8_t ff_zigzag_direct[64]
Definition: mathtables.c:98
av_cold void ff_me_cmp_init(MECmpContext *c, AVCodecContext *avctx)
Definition: me_cmp.c:1020
int ff_set_cmp(MECmpContext *c, me_cmp_func *cmp, int type)
Definition: me_cmp.c:475
#define LOCAL_ALIGNED_16(t, v,...)
Definition: mem_internal.h:115
#define LOCAL_ALIGNED_8(t, v,...)
Definition: mem_internal.h:113
const char data[16]
Definition: mxf.c:142
AVOptions.
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
Definition: pixdesc.c:2489
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_YUV411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:73
bitstream writer API
static void put_sbits(PutBitContext *pb, int n, int32_t value)
Definition: put_bits.h:253
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:57
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:76
static int put_bits_left(PutBitContext *s)
Definition: put_bits.h:102
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:110
mfxU16 profile
Definition: qsvenc.c:45
#define a2
Definition: regdef.h:48
#define blk(i)
Definition: sha.c:185
unsigned int pos
Definition: spdifenc.c:412
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
int ildct_cmp
interlaced DCT comparison function
Definition: avcodec.h:940
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
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 flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:375
uint8_t * data
Definition: packet.h:369
uint16_t buf_offset
Definition: dv.h:38
uint8_t next[64]
Definition: dvenc.c:159
int prev[5]
Definition: dvenc.c:154
int bit_size[4]
Definition: dvenc.c:153
int16_t save[64]
Definition: dvenc.c:166
int16_t mb[64]
Definition: dvenc.c:158
int dct_mode
Definition: dvenc.c:157
int area_q[4]
Definition: dvenc.c:152
uint32_t partial_bit_buffer
Definition: dvenc.c:162
uint8_t partial_bit_count
Definition: dvenc.c:161
int min_qlevel
Definition: dvenc.c:167
int cur_ac
Definition: dvenc.c:155
int cno
Definition: dvenc.c:156
uint8_t sign[64]
Definition: dvenc.c:160
void(* fdct248)(int16_t *block)
Definition: fdctdsp.h:28
void(* fdct)(int16_t *block)
Definition: fdctdsp.h:27
me_cmp_func ildct_cmp[6]
Definition: me_cmp.h:75
int size_in_bits
Definition: put_bits.h:48
uint32_t size
Definition: dv_tablegen.h:42
uint32_t vlc
Definition: dv_tablegen.h:41
uint8_t run
Definition: svq3.c:205
uint8_t level
Definition: svq3.c:206
#define av_log(a,...)
AVPacket * pkt
Definition: movenc.c:59
int size
#define mb
const char * b
Definition: vf_curves.c:119
if(ret< 0)
Definition: vf_mcdeint.c:282
static av_always_inline void chroma(WaveformContext *s, AVFrame *in, AVFrame *out, int component, int intensity, int offset_y, int offset_x, int column, int mirror, int jobnr, int nb_jobs)
Definition: vf_waveform.c:1625
static double c[64]