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