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