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
38
51
58
61
64
67
68
70 {
76
78
82 "in DV, input value is: %s\n",
name ?
name :
"unknown");
85 }
86
90 "Valid DV profiles are:\n",
94 }
95
100 }
101
102 memset(&fdsp,0, sizeof(fdsp));
103 memset(&mecc,0, sizeof(mecc));
104 memset(&pdsp,0, sizeof(pdsp));
111
114
115 s->fdct[0] = fdsp.
fdct;
117
118 #if !CONFIG_HARDCODED_TABLES
119 {
122 }
123 #endif
124
125 return 0;
126 }
127
128 /* bit budget for AC only in 5 MBs */
132
133 #if CONFIG_SMALL
134 /* Convert run and level (where level != 0) pair into VLC, returning bit size */
136 uint32_t *vlc)
137 {
142 } else {
146 } else {
147 *vlc = 0xfe00 | (
level << 1) | sign;
149 }
154 }
155 }
156
158 }
159
161 {
163
166 } else {
170 }
172 }
173 #else
175 {
178 }
179
181 {
183 }
184 #endif
185
198 /* used by DV100 only: a copy of the weighted and classified but
199 not-yet-quantized AC coefficients. This is necessary for
200 re-quantizing at different steps. */
202 int min_qlevel;
/* DV100 only: minimum qlevel (for AC coefficients >255) */
204
208 {
213
216 for (;;) {
217 /* Find suitable storage space */
223 }
224 if (pb + 1 >= pb_end) {
227 return pb;
228 }
229 }
230
231 /* Store VLC */
233
235 break;
236
237 /* Construct the next VLC */
243 } else {
245 vlc = 6; /* End Of Block stamp */
246 }
247 }
248 return pb;
249 }
250
252 ptrdiff_t linesize)
253 {
256 if (ps > 0) {
260 }
261 }
262
263 return 0;
264 }
265
268 131072, 257107, 257107, 242189, 252167, 242189, 235923, 237536,
269 237536, 235923, 229376, 231390, 223754, 231390, 229376, 222935,
270 224969, 217965, 217965, 224969, 222935, 200636, 218652, 211916,
271 212325, 211916, 218652, 200636, 188995, 196781, 205965, 206433,
272 206433, 205965, 196781, 188995, 185364, 185364, 200636, 200704,
273 200636, 185364, 185364, 174609, 180568, 195068, 195068, 180568,
274 174609, 170091, 175557, 189591, 175557, 170091, 165371, 170627,
275 170627, 165371, 160727, 153560, 160727, 144651, 144651, 136258,
276 };
278 131072, 262144, 257107, 257107, 242189, 242189, 242189, 242189,
279 237536, 237536, 229376, 229376, 200636, 200636, 224973, 224973,
280 223754, 223754, 235923, 235923, 229376, 229376, 217965, 217965,
281 211916, 211916, 196781, 196781, 185364, 185364, 206433, 206433,
282 211916, 211916, 222935, 222935, 200636, 200636, 205964, 205964,
283 200704, 200704, 180568, 180568, 175557, 175557, 195068, 195068,
284 185364, 185364, 188995, 188995, 174606, 174606, 175557, 175557,
285 170627, 170627, 153560, 153560, 165371, 165371, 144651, 144651,
286 };
287
288 /* setting this to 1 results in a faster codec but
289 * somewhat lower image quality */
290 #define DV100_SACRIFICE_QUALITY_FOR_SPEED 1
291 #define DV100_ENABLE_FINER 1
292
293 /* pack combination of QNO and CNO into a single 8-bit value */
294 #define DV100_MAKE_QLEVEL(qno,cno) ((qno<<2) | (cno))
295 #define DV100_QLEVEL_QNO(qlevel) (qlevel>>2)
296 #define DV100_QLEVEL_CNO(qlevel) (qlevel&0x3)
297
298 #define DV100_NUM_QLEVELS 31
299
300 /* The quantization step is determined by a combination of QNO and
301 CNO. We refer to these combinations as "qlevels" (this term is our
302 own, it's not mentioned in the spec). We use CNO, a multiplier on
303 the quantization step, to "fill in the gaps" between quantization
304 steps associated with successive values of QNO. e.g. there is no
305 QNO for a quantization step of 10, but we can use QNO=5 CNO=1 to
306 get the same result. The table below encodes combinations of QNO
307 and CNO in order of increasing quantization coarseness. */
339 // ...
341 };
342
346
347 #if DV100_SACRIFICE_QUALITY_FOR_SPEED
349 #else
351 #endif
352
353 // 1/qstep, shifted up by 16 bits
356 65536, 65536, 32768, 21845, 16384, 13107, 10923, 9362, 8192, 4096, 3641, 3277, 2979, 2731, 2341, 1260,
357 };
358
359 /* DV100 weights are pre-zigzagged, inverted and multiplied by 2^16
360 (in DV100 the AC components are divided by the spec weights) */
362 { 8192, 65536, 65536, 61681, 61681, 61681, 58254, 58254,
363 58254, 58254, 58254, 58254, 55188, 58254, 58254, 55188,
364 55188, 55188, 55188, 55188, 55188, 24966, 27594, 26214,
365 26214, 26214, 27594, 24966, 23831, 24385, 25575, 25575,
366 25575, 25575, 24385, 23831, 23302, 23302, 24966, 24966,
367 24966, 23302, 23302, 21845, 22795, 24385, 24385, 22795,
368 21845, 21400, 21845, 23831, 21845, 21400, 10382, 10700,
369 10700, 10382, 10082, 9620, 10082, 9039, 9039, 8525, },
370 { 8192, 65536, 65536, 61681, 61681, 61681, 41943, 41943,
371 41943, 41943, 40330, 41943, 40330, 41943, 40330, 40330,
372 40330, 38836, 38836, 40330, 40330, 24966, 27594, 26214,
373 26214, 26214, 27594, 24966, 23831, 24385, 25575, 25575,
374 25575, 25575, 24385, 23831, 11523, 11523, 12483, 12483,
375 12483, 11523, 11523, 10923, 11275, 12193, 12193, 11275,
376 10923, 5323, 5490, 5924, 5490, 5323, 5165, 5323,
377 5323, 5165, 5017, 4788, 5017, 4520, 4520, 4263, }
378 };
379
381 { 8192, 65536, 65536, 61681, 61681, 61681, 58254, 58254,
382 58254, 58254, 58254, 58254, 55188, 58254, 58254, 55188,
383 55188, 55188, 55188, 55188, 55188, 24966, 27594, 26214,
384 26214, 26214, 27594, 24966, 23831, 24385, 25575, 25575,
385 25575, 25575, 24385, 23831, 15420, 15420, 16644, 16644,
386 16644, 15420, 15420, 10923, 11398, 12193, 12193, 11398,
387 10923, 10700, 10923, 11916, 10923, 10700, 5191, 5350,
388 5350, 5191, 5041, 4810, 5041, 4520, 4520, 4263, },
389 { 8192, 43691, 43691, 40330, 40330, 40330, 29127, 29127,
390 29127, 29127, 29127, 29127, 27594, 29127, 29127, 27594,
391 27594, 27594, 27594, 27594, 27594, 12483, 13797, 13107,
392 13107, 13107, 13797, 12483, 11916, 12193, 12788, 12788,
393 12788, 12788, 12193, 11916, 5761, 5761, 6242, 6242,
394 6242, 5761, 5761, 5461, 5638, 5461, 6096, 5638,
395 5461, 2661, 2745, 2962, 2745, 2661, 2583, 2661,
396 2661, 2583, 2509, 2394, 2509, 2260, 2260, 2131, }
397 };
398
401 const uint8_t *zigzag_scan,
403 {
405 /* We offer two different methods for class number assignment: the
406 * method suggested in SMPTE 314M Table 22, and an improved
407 * method. The SMPTE method is very conservative; it assigns class
408 * 3 (i.e. severe quantization) to any block where the largest AC
409 * component is greater than 36. FFmpeg's DV encoder tracks AC bit
410 * consumption precisely, so there is no need to bias most blocks
411 * towards strongly lossy compression. Instead, we assign class 2
412 * to most blocks, and use class 3 only when strictly necessary
413 * (for blocks whose largest AC component exceeds 255). */
414
415 #if 0 /* SMPTE spec method */
416 static const int classes[] = { 12, 24, 36, 0xffff };
417 #else /* improved FFmpeg method */
418 static const int classes[] = { -1, -1, 255, 0xffff };
419 #endif
420 int max = classes[0];
421 int prev = 0;
422 const unsigned deadzone =
s->quant_deadzone;
423 const unsigned threshold = 2 * deadzone;
424
426
427 for (area = 0; area < 4; area++) {
428 bi->
prev[area] = prev;
429 bi->
bit_size[area] = 1;
// 4 areas 4 bits for EOB :)
432
433 if (
level + deadzone > threshold) {
435 /* Weight it and shift down into range, adding for rounding.
436 * The extra division by a factor of 2^4 reverses the 8x
437 * expansion of the DCT AND the 2x doubling of the weights. */
441 continue;
448 }
449 }
450 }
453 ;
454
456
459 prev = 0;
461 for (area = 0; area < 4; area++) {
462 bi->
prev[area] = prev;
463 bi->
bit_size[area] = 1;
// 4 areas 4 bits for EOB :)
466
471 }
472 }
473 }
475 }
476
479 }
480
481 /* this function just copies the DCT coefficients and performs
482 the initial (non-)quantization. */
485 const uint8_t *zigzag_scan,
487 {
489
490 /* the first quantization (none at all) */
492
493 /* weigh AC components and store to save[] */
494 /* (i=0 is the DC component; we only include it to make the
495 number of loop iterations even, for future possible SIMD optimization) */
496 for (
i = 0;
i < 64;
i += 2) {
497 int level0, level1;
498
499 /* get the AC component (in zig-zag order) */
500 level0 =
blk[zigzag_scan[
i+0]];
501 level1 =
blk[zigzag_scan[
i+1]];
502
503 /* extract sign and make it the lowest bit */
504 bi->
sign[
i+0] = (level0>>31)&1;
505 bi->
sign[
i+1] = (level1>>31)&1;
506
507 /* take absolute value of the level */
508 level0 =
FFABS(level0);
509 level1 =
FFABS(level1);
510
511 /* weigh it */
512 level0 = (level0*
weight[
i+0] + 4096 + (1<<17)) >> 18;
513 level1 = (level1*
weight[
i+1] + 4096 + (1<<17)) >> 18;
514
515 /* save unquantized value */
516 bi->
save[
i+0] = level0;
517 bi->
save[
i+1] = level1;
518
519 /* find max component */
524 }
525
526 /* copy DC component */
528
529 /* the EOB code is 4 bits */
532
533 /* ensure that no AC coefficients are cut off */
535
536 bi->
area_q[0] = 25;
/* set to an "impossible" value */
538 }
539
542 {
544
549
554 } else {
558 }
559 } else {
560 /* We rely on the fact that encoding all zeros leads to an immediate EOB,
561 which is precisely what the spec calls for in the "dummy" blocks. */
562 memset(
blk, 0, 64*
sizeof(*
blk));
564 }
565
568 if (
s->sys->height == 1080) {
570 } else { /* 720p */
572 }
577 } else {
582 }
583
585 }
586
587 /* DV100 quantize
588 Perform quantization by divinding the AC component by the qstep.
589 As an optimization we use a fixed-point integer multiply instead
590 of a divide. */
592 {
593 /* this code is equivalent to */
594 /* return (level + qs/2) / qs; */
595
597
598 /* the extra +1024 is needed to make the rounding come out right. */
599
600 /* I (DJM) have verified that the results are exactly the same as
601 division for level 0-2048 at all QNOs. */
602 }
603
605 {
606 int prev, k, qsinv;
607
610
611 if (
b->area_q[0] == qno &&
b->cno == cno)
612 return b->bit_size[0];
613
615
616 /* record the new qstep */
619
620 /* reset encoded size (EOB = 4 bits) */
622
623 /* visit nonzero components and quantize */
624 prev = 0;
625 for (k = 1; k < 64; k++) {
626 /* quantize */
628 if (ac) {
629 if (ac > 255)
630 ac = 255;
634 prev = k;
635 }
636 }
638
639 return b->bit_size[0];
640 }
641
643 {
645 int min_qlevel[5];
646 int qlevels[5];
649 /* cache block sizes at hypothetical qlevels */
651
652 /* get minimum qlevels */
653 for (
i = 0;
i < 5;
i++) {
655 for (j = 0; j < 8; j++) {
656 if (blks[8*
i+j].min_qlevel > min_qlevel[
i])
658 }
659 }
660
661 /* initialize sizes */
662 for (
i = 0;
i < 5;
i++) {
664 if (qlevels[
i] < min_qlevel[
i])
665 qlevels[
i] = min_qlevel[
i];
666
669 for (j = 0; j < 8; j++) {
671 size[
i] += size_cache[8*
i+j][qlevels[
i]];
672 }
673 }
674
675 /* must we go coarser? */
677 int largest =
size[0] % 5;
/* 'random' number */
678 int qlevels_done = 0;
679
680 do {
681 /* find the macroblock with the lowest qlevel */
682 for (
i = 0;
i < 5;
i++) {
683 if (qlevels[
i] < qlevels[largest])
685 }
686
688 /* ensure that we don't enter infinite loop */
689 largest = (largest+1) % 5;
690
691 /* quantize a little bit more */
695 qlevels_done++;
696 }
697
700
701 /* for each block */
703 for (j = 0; j < 8; j++,
b++) {
704 /* accumulate block size into macroblock */
705 if(size_cache[8*
i+j][qlevels[
i]] == 0) {
706 /* it is safe to use actual_quantize() here because we only go from finer to coarser,
707 and it saves the final actual_quantize() down below */
709 }
710 size[
i] += size_cache[8*
i+j][qlevels[
i]];
711 } /* for each block */
712
714
715 // can we go finer?
718 int save_qlevel;
719 int largest =
size[0] % 5;
/* 'random' number */
720
721 while (qlevels[0] > min_qlevel[0] ||
722 qlevels[1] > min_qlevel[1] ||
723 qlevels[2] > min_qlevel[2] ||
724 qlevels[3] > min_qlevel[3] ||
725 qlevels[4] > min_qlevel[4]) {
726
727 /* find the macroblock with the highest qlevel */
728 for (
i = 0;
i < 5;
i++) {
729 if (qlevels[
i] > min_qlevel[
i] && qlevels[
i] > qlevels[largest])
731 }
732
734
735 /* ensure that we don't enter infinite loop */
736 largest = (largest+1) % 5;
737
738 if (qlevels[
i] <= min_qlevel[
i]) {
739 /* can't unquantize any more */
740 continue;
741 }
742 /* quantize a little bit less */
743 save_qlevel = qlevels[
i];
745 if (qlevels[
i] < min_qlevel[
i])
746 qlevels[
i] = min_qlevel[
i];
747
749
751
752 /* for each block */
754 for (j = 0; j < 8; j++,
b++) {
755 /* accumulate block size into macroblock */
756 if(size_cache[8*
i+j][qlevels[
i]] == 0) {
758 }
759 size[
i] += size_cache[8*
i+j][qlevels[
i]];
760 } /* for each block */
761
762 /* did we bust the limit? */
764 /* go back down and exit */
765 qlevels[
i] = save_qlevel;
767 break;
768 }
769 }
770 }
771
772 /* now do the actual quantization */
773 for (
i = 0;
i < 5;
i++) {
774 /* for each block */
777 for (j = 0; j < 8; j++,
b++) {
778 /* accumulate block size into macroblock */
780 } /* for each block */
781 }
782 }
783
785 {
787 int i, j, k,
a, prev,
a2;
789
795 do {
797 for (
i = 0;
i < 5;
i++) {
799 continue;
800
803 for (j = 0; j < 6; j++,
b++) {
804 for (
a = 0;
a < 4;
a++) {
806 b->bit_size[
a] = 1;
// 4 areas 4 bits for EOB :)
814 prev = k;
815 } else {
825 }
826 b->next[prev] =
b->next[k];
827 }
828 }
829 b->prev[
a + 1] = prev;
830 }
832 }
833 }
835 return;
836 }
837 } while (qnos[0] | qnos[1] | qnos[2] | qnos[3] | qnos[4]);
838
841 size[0] = 5 * 6 * 4;
// EOB
842 for (j = 0; j < 6 * 5; j++,
b++) {
844 for (k =
b->next[prev]; k < 64; k = b->next[k]) {
845 if (
b->mb[k] <
a &&
b->mb[k] > -
a) {
846 b->next[prev] =
b->next[k];
847 } else {
849 prev = k;
850 }
851 }
852 }
853 }
854 }
855
856 /* update all cno values into the blocks, over-writing the old values without
857 touching anything else. (only used for DV100) */
859 {
862
863 for (mb_index = 0; mb_index < 5; mb_index++) {
864 data = dif + mb_index*80 + 4;
866 /* zero out the class number */
868 /* add the new one */
870
872 }
873 }
874 }
875
877 {
881 int mb_x, mb_y, c_offset;
882 ptrdiff_t linesize, y_stride;
883 const uint8_t *y_ptr;
884 uint8_t *dif, *p;
890 int vs_bit_size = 0;
891 int qnos[5];
892 int *qnosp = &qnos[0];
893
895 enc_blk = &enc_blks[0];
896 for (mb_index = 0; mb_index < 5; mb_index++) {
898
900
901 y_ptr =
s->frame->data[0] + (mb_y *
s->frame->linesize[0] + mb_x) * 8;
902 linesize =
s->frame->linesize[0];
903
904 if (
s->sys->height == 1080 && mb_y < 134)
906 else
908 for (
i = 1;
i < 8;
i++)
910
911 /* initializing luminance blocks */
914 (
s->sys->height >= 720 && mb_y != 134)) {
915 y_stride =
s->frame->linesize[0] * (1 << (3*!enc_blk->
dct_mode));
916 } else {
917 y_stride = 16;
918 }
919 y_ptr =
s->frame->data[0] +
920 (mb_y *
s->frame->linesize[0] + mb_x) * 8;
921 linesize =
s->frame->linesize[0];
922
923 if (
s->sys->video_stype == 4) {
/* SD 422 */
924 vs_bit_size +=
929 } else {
930 vs_bit_size +=
935 }
936 enc_blk += 4;
937
938 /* initializing chrominance blocks */
941 for (j = 2; j; j--) {
942 const uint8_t *c_ptr =
s->frame->data[j] + c_offset;
943 linesize =
s->frame->linesize[j];
944 y_stride = (mb_y == 134) ? 8 : (
s->frame->linesize[j] * (1 << (3*!enc_blk->
dct_mode)));
946 uint8_t *
b = scratch;
947 for (
i = 0;
i < 8;
i++) {
948 const uint8_t *
d = c_ptr + linesize * 8;
957 c_ptr += linesize;
959 }
960 c_ptr = scratch;
961 linesize = 16;
962 }
963
965 if (
s->sys->bpm == 8)
968 }
969 }
970
972 /* unconditional */
976 }
977
978 /* DIF encoding process */
979 for (j = 0; j < 5 *
s->sys->bpm;) {
980 int start_mb = j;
981
982 p[3] = *qnosp++;
983 p += 4;
984
985 /* First pass over individual cells only */
986 for (
i = 0;
i <
s->sys->bpm;
i++, j++) {
987 int sz =
s->sys->block_sizes[
i] >> 3;
988
990 put_sbits(&pbs[j], 9, ((enc_blks[j].
mb[0] >> 3) - 1024 + 2) >> 2);
992 put_bits(&pbs[j], 2, enc_blks[j].cno);
993
995 p += sz;
996 }
997
998 /* Second pass over each MB space */
999 pb = &pbs[start_mb];
1000 for (
i = 0;
i <
s->sys->bpm;
i++)
1001 if (enc_blks[start_mb +
i].partial_bit_count)
1003 &pbs[start_mb +
s->sys->bpm]);
1004 }
1005
1006 /* Third and final pass over the whole video segment space */
1007 pb = &pbs[0];
1008 for (j = 0; j < 5 *
s->sys->bpm; j++) {
1009 if (enc_blks[j].partial_bit_count)
1011 if (enc_blks[j].partial_bit_count)
1013 }
1014
1015 for (j = 0; j < 5 *
s->sys->bpm; j++) {
1018 }
1019
1022
1023 return 0;
1024 }
1025
1027 uint8_t *buf)
1028 {
1029 /*
1030 * Here's what SMPTE314M says about these two:
1031 * (page 6) APTn, AP1n, AP2n, AP3n: These data shall be identical
1032 * as track application IDs (APTn = 001, AP1n =
1033 * 001, AP2n = 001, AP3n = 001), if the source signal
1034 * comes from a digital VCR. If the signal source is
1035 * unknown, all bits for these data shall be set to 1.
1036 * (page 12) STYPE: STYPE defines a signal type of video signal
1037 * 00000b = 4:1:1 compression
1038 * 00100b = 4:2:2 compression
1039 * XXXXXX = Reserved
1040 * Now, I've got two problems with these statements:
1041 * 1. it looks like APT == 111b should be a safe bet, but it isn't.
1042 * It seems that for PAL as defined in IEC 61834 we have to set
1043 * APT to 000 and for SMPTE314M to 001.
1044 * 2. It is not at all clear what STYPE is used for 4:2:0 PAL
1045 * compression scheme (if any).
1046 */
1047 uint8_t aspect = 0;
1050
1051 if (
c->avctx->height >= 720)
1053 else
1055
1057 (
int)(
av_q2d(
c->avctx->sample_aspect_ratio) *
1058 c->avctx->width /
c->avctx->height * 10) >= 17)
1059 /* HD formats are always 16:9 */
1060 aspect = 0x02;
1061
1062 buf[0] = (uint8_t) pack_id;
1063 switch (pack_id) {
1064 case DV_HEADER525:
/* I can't imagine why these two weren't defined as real */
1065 case DV_HEADER625:
/* packs in SMPTE314M -- they definitely look like ones */
1066 buf[1] = 0xf8 | /* reserved -- always 1 */
1067 (apt & 0x07); /* APT: Track application ID */
1068 buf[2] = (0 << 7) | /* TF1: audio data is 0 - valid; 1 - invalid */
1069 (0x0f << 3) | /* reserved -- always 1 */
1070 (apt & 0x07); /* AP1: Audio application ID */
1071 buf[3] = (0 << 7) | /* TF2: video data is 0 - valid; 1 - invalid */
1072 (0x0f << 3) | /* reserved -- always 1 */
1073 (apt & 0x07); /* AP2: Video application ID */
1074 buf[4] = (0 << 7) | /* TF3: subcode(SSYB) is 0 - valid; 1 - invalid */
1075 (0x0f << 3) | /* reserved -- always 1 */
1076 (apt & 0x07); /* AP3: Subcode application ID */
1077 break;
1079 buf[1] = 0xff; /* reserved -- always 1 */
1080 buf[2] = (1 << 7) | /* B/W: 0 - b/w, 1 - color */
1081 (1 << 6) | /* following CLF is valid - 0, invalid - 1 */
1082 (3 << 4) | /* CLF: color frames ID (see ITU-R BT.470-4) */
1083 0
xf;
/* reserved -- always 1 */
1084 buf[3] = (3 << 6) | /* reserved -- always 1 */
1085 (
c->sys->dsf << 5) |
/* system: 60fields/50fields */
1086 c->sys->video_stype;
/* signal type video compression */
1087 buf[4] = 0xff; /* VISC: 0xff -- no information */
1088 break;
1090 buf[1] = (0 << 6) | /* Copy generation management (CGMS) 0 -- free */
1091 0x3f; /* reserved -- always 1 */
1092 buf[2] = 0xc8 | /* reserved -- always b11001xxx */
1093 aspect;
1094 buf[3] = (1 << 7) | /* frame/field flag 1 -- frame, 0 -- field */
1095 fs |
/* first/second field flag 0 -- field 2, 1 -- field 1 */
1096 (1 << 5) | /* frame change flag 0 -- same picture as before, 1 -- different */
1097 (1 << 4) | /* 1 - interlaced, 0 - noninterlaced */
1098 0xc; /* reserved -- always b1100 */
1099 buf[4] = 0xff; /* reserved -- always 1 */
1100 break;
1101 default:
1102 buf[1] =
1103 buf[2] =
1104 buf[3] =
1105 buf[4] = 0xff;
1106 }
1107 return 5;
1108 }
1109
1111 uint8_t seq_num, uint8_t dif_num,
1112 uint8_t *buf)
1113 {
1114 int fsc = chan_num & 1;
1115 int fsp = 1 - (chan_num >> 1);
1116
1117 buf[0] = (uint8_t) t; /* Section type */
1118 buf[1] = (seq_num << 4) | /* DIF seq number 0-9 for 525/60; 0-11 for 625/50 */
1119 (fsc << 3) | /* FSC: for 50 and 100Mb/s 0 - first channel; 1 - second */
1120 (fsp << 2) | /* FSP: for 100Mb/s 1 - channels 0-1; 0 - channels 2-3 */
1121 3; /* reserved -- always 1 */
1122 buf[2] = dif_num; /* DIF block number Video: 0-134, Audio: 0-8 */
1123 return 3;
1124 }
1125
1127 {
1128 if (syb_num == 0 || syb_num == 6) {
1129 buf[0] = (fr << 7) | /* FR ID 1 - first half of each channel; 0 - second */
1130 (0 << 4) | /* AP3 (Subcode application ID) */
1131 0x0f; /* reserved -- always 1 */
1132 } else if (syb_num == 11) {
1133 buf[0] = (fr << 7) | /* FR ID 1 - first half of each channel; 0 - second */
1134 0x7f; /* reserved -- always 1 */
1135 } else {
1136 buf[0] = (fr << 7) | /* FR ID 1 - first half of each channel; 0 - second */
1137 (0 << 4) | /* APT (Track application ID) */
1138 0x0f; /* reserved -- always 1 */
1139 }
1140 buf[1] = 0xf0 | /* reserved -- always 1 */
1141 (syb_num & 0x0f); /* SSYB number 0 - 11 */
1142 buf[2] = 0xff; /* reserved -- always 1 */
1143 return 3;
1144 }
1145
1147 {
1149 /* We work with 720p frames split in half. The odd half-frame is chan 2,3 */
1150 int chan_offset = 2*(
c->sys->height == 720 &&
c->avctx->frame_num & 1);
1151
1152 for (chan = 0; chan <
c->sys->n_difchan; chan++) {
1153 for (
i = 0;
i <
c->sys->difseg_size;
i++) {
1154 memset(buf, 0xff, 80 * 6); /* first 6 DIF blocks are for control data */
1155
1156 /* DV header: 1DIF */
1160 buf += 72; /* unused bytes */
1161
1162 /* DV subcode: 2DIFs */
1163 for (j = 0; j < 2; j++) {
1165 for (k = 0; k < 6; k++)
1167 buf += 29; /* unused bytes */
1168 }
1169
1170 /* DV VAUX: 3DIFS */
1171 for (j = 0; j < 3; j++) {
1175 buf += 7 * 5;
1178 buf += 4 * 5 + 2; /* unused bytes */
1179 }
1180
1181 /* DV Audio/Video: 135 Video DIFs + 9 Audio DIFs */
1182 for (j = 0; j < 135; j++) {
1183 if (j % 15 == 0) {
1184 memset(buf, 0xff, 80);
1186 buf += 77; /* audio control & shuffled PCM audio */
1187 }
1189 buf += 77; /* 1 video macroblock: 1 bytes control
1190 * 4 * 14 bytes Y 8x8 data
1191 * 10 bytes Cr 8x8 data
1192 * 10 bytes Cb 8x8 data */
1193 }
1194 }
1195 }
1196 }
1197
1200 {
1203
1206 /* Fixme: Only zero the part that is not overwritten later. */
1208
1209 c->pix_fmt =
s->sys->pix_fmt;
1212
1214
1217
1219
1220 *got_packet = 1;
1221
1222 return 0;
1223 }
1224
1225 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1226 #define OFFSET(x) offsetof(DVEncContext, x)
1228 {
"quant_deadzone",
"Quantizer dead zone",
OFFSET(quant_deadzone),
AV_OPT_TYPE_INT, { .i64 = 7 }, 0, 1024,
VE },
1230 };
1231
1237 };
1238
1240 .
p.
name =
"dvvideo",
1253 },
1255 };