1 /*
2 * DV decoder
3 * Copyright (c) 2002 Fabrice Bellard
4 * Copyright (c) 2004 Roman Shaposhnik
5 *
6 * DV encoder
7 * Copyright (c) 2003 Roman Shaposhnik
8 *
9 * 50 Mbps (DVCPRO50) support
10 * Copyright (c) 2006 Daniel Maas <dmaas@maasdigital.com>
11 *
12 * 100 Mbps (DVCPRO HD) support
13 * Initial code by Daniel Maas <dmaas@maasdigital.com> (funded by BBC R&D)
14 * Final code by Roman Shaposhnik
15 *
16 * Many thanks to Dan Dennedy <dan@dennedy.org> for providing wealth
17 * of DV technical info.
18 *
19 * This file is part of FFmpeg.
20 *
21 * FFmpeg is free software; you can redistribute it and/or
22 * modify it under the terms of the GNU Lesser General Public
23 * License as published by the Free Software Foundation; either
24 * version 2.1 of the License, or (at your option) any later version.
25 *
26 * FFmpeg is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
29 * Lesser General Public License for more details.
30 *
31 * You should have received a copy of the GNU Lesser General Public
32 * License along with FFmpeg; if not, write to the Free Software
33 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
34 */
35
36 /**
37 * @file
38 * DV codec.
39 */
40
50
51 /* XXX: also include quantization */
53
55 uint16_t *tbl)
56 {
57 static const uint8_t off[] = { 2, 6, 8, 0, 4 };
58 static const uint8_t shuf1[] = { 36, 18, 54, 0, 72 };
59 static const uint8_t shuf2[] = { 24, 12, 36, 0, 48 };
60 static const uint8_t shuf3[] = { 18, 9, 27, 0, 36 };
61
62 static const uint8_t l_start[] = {0, 4, 9, 13, 18, 22, 27, 31, 36, 40};
63 static const uint8_t l_start_shuffled[] = { 9, 4, 13, 0, 18 };
64
65 static const uint8_t serpent1[] = {0, 1, 2, 2, 1, 0,
66 0, 1, 2, 2, 1, 0,
67 0, 1, 2, 2, 1, 0,
68 0, 1, 2, 2, 1, 0,
69 0, 1, 2};
70 static const uint8_t serpent2[] = {0, 1, 2, 3, 4, 5, 5, 4, 3, 2, 1, 0,
71 0, 1, 2, 3, 4, 5, 5, 4, 3, 2, 1, 0,
72 0, 1, 2, 3, 4, 5};
73
74 static const uint8_t remap[][2] = {{ 0, 0}, { 0, 0}, { 0, 0}, { 0, 0},
/* dummy */
75 { 0, 0}, { 0, 1}, { 0, 2}, { 0, 3}, {10, 0},
76 {10, 1}, {10, 2}, {10, 3}, {20, 0}, {20, 1},
77 {20, 2}, {20, 3}, {30, 0}, {30, 1}, {30, 2},
78 {30, 3}, {40, 0}, {40, 1}, {40, 2}, {40, 3},
79 {50, 0}, {50, 1}, {50, 2}, {50, 3}, {60, 0},
80 {60, 1}, {60, 2}, {60, 3}, {70, 0}, {70, 1},
81 {70, 2}, {70, 3}, { 0,64}, { 0,65}, { 0,66},
82 {10,64}, {10,65}, {10,66}, {20,64}, {20,65},
83 {20,66}, {30,64}, {30,65}, {30,66}, {40,64},
84 {40,65}, {40,66}, {50,64}, {50,65}, {50,66},
85 {60,64}, {60,65}, {60,66}, {70,64}, {70,65},
86 {70,66}, { 0,67}, {20,67}, {40,67}, {60,67}};
87
90
91 for (m=0; m<5; m++) {
93 case 1440:
94 blk = (chan*11+seq)*27+slot;
95
96 if (chan == 0 && seq == 11) {
97 x = m*27+slot;
98 if (x<90) {
99 y = 0;
100 } else {
101 x = (x - 90)*2;
102 y = 67;
103 }
104 } else {
105 i = (4*chan + blk + off[
m])%11;
106 k = (blk/11)%27;
107
108 x = shuf1[
m] + (chan&1)*9 + k%9;
109 y = (i*3+k/9)*2 + (chan>>1) + 1;
110 }
111 tbl[
m] = (x<<1)|(y<<9);
112 break;
113 case 1280:
114 blk = (chan*10+seq)*27+slot;
115
116 i = (4*chan + (seq/5) + 2*blk + off[m])%10;
117 k = (blk/5)%27;
118
119 x = shuf1[
m]+(chan&1)*9 + k%9;
120 y = (i*3+k/9)*2 + (chan>>1) + 4;
121
122 if (x >= 80) {
123 x = remap[
y][0]+((x-80)<<(y>59));
125 }
126 tbl[
m] = (x<<1)|(y<<9);
127 break;
128 case 960:
129 blk = (chan*10+seq)*27+slot;
130
131 i = (4*chan + (seq/5) + 2*blk + off[m])%10;
132 k = (blk/5)%27 + (i&1)*3;
133
134 x = shuf2[
m] + k%6 + 6*(chan&1);
135 y = l_start[i] + k/6 + 45*(chan>>1);
136 tbl[
m] = (x<<1)|(y<<9);
137 break;
138 case 720:
141 x = shuf3[
m] + slot/3;
142 y = serpent1[slot] +
144 tbl[
m] = (x<<1)|(y<<8);
145 break;
147 x = shuf3[
m] + slot/3;
148 y = serpent1[slot] +
150 tbl[
m] = (x<<1)|(y<<9);
151 break;
154 k = slot + ((m==1||m==2)?3:0);
155
156 x = l_start_shuffled[
m] + k/6;
157 y = serpent2[k] + i*6;
158 if (x>21)
159 y = y*2 - i*6;
160 tbl[
m] = (x<<2)|(y<<8);
161 break;
162 }
163 default:
164 break;
165 }
166 }
167 }
168
169 /* quantization quanta by QNO for DV100 */
171 1, /* QNO = 0 and 1 both have no quantization */
172 1,
173 2, 3, 4, 5, 6, 7, 8, 16, 18, 20, 22, 24, 28, 52
174 };
175
177
179 {
181 uint32_t *factor1, *factor2;
182 const int *iweight1, *iweight2;
183
185 p = i = 0;
188 p += 6;
189 for (j=0; j<27; j++) {
190 p += !(j%3);
195 }
196 p += 5;
197 }
198 }
199 }
200 }
201
208 } else {
211 }
213 for (c = 0; c < 4; c++) {
214 for (s = 0; s < 16; s++) {
215 for (i = 0; i < 64; i++) {
218 }
219 }
220 }
221 } else {
224 for (s = 0; s < 22; s++) {
225 for (i = c = 0; c < 4; c++) {
228 *factor2++ = (*factor1++) << 1;
229 }
230 }
231 }
232 }
233 }
234 }
235
236 return 0;
237 }
238
240 {
243 static int done = 0;
244 int i, j;
245
246 if (!done) {
252
253 done = 1;
254
255 /* it's faster to include sign bit in a generic VLC parsing scheme */
256 for (i = 0, j = 0; i <
NB_DV_VLC; i++, j++) {
261
263 new_dv_vlc_bits[j] <<= 1;
264 new_dv_vlc_len[j]++;
265
266 j++;
271 }
272 }
273
274 /* NOTE: as a trick, we use the fact the no codes are unused
275 to accelerate the parsing of partial codes */
277 new_dv_vlc_len, 1, 1, new_dv_vlc_bits, 2, 2, 0);
279
281 int code = dv_vlc.
table[i][0];
284
285 if (len < 0){ //more bits needed
286 run = 0;
287 level = code;
288 } else {
289 run = new_dv_vlc_run [code] + 1;
290 level = new_dv_vlc_level[code];
291 }
292 ff_dv_rl_vlc[i].
len =
len;
294 ff_dv_rl_vlc[i].
run =
run;
295 }
297 }
298
299 /* Generic DSP setup */
300 memset(&dsp,0, sizeof(dsp));
305
306 /* 88DCT setup */
309 for (i = 0; i < 64; i++)
311
312 /* 248DCT setup */
316 for (i = 0; i < 64; i++){
319 }
320 }else
322
327
328 return 0;
329 }
330
332 {
335 "Valid DV profiles are:\n",
339 }
340 if (avctx->
height > 576) {
343 }
344
346
348 }
349
350 /* bit budget for AC only in 5 MBs */
353
354 #if CONFIG_SMALL
355 /* Converts run and level (where level != 0) pair into VLC, returning bit size */
357 {
362 }
363 else {
367 } else {
368 *vlc = 0xfe00 | (level << 1) | sign;
369 size = 16;
370 }
371 if (run) {
372 *vlc |= ((run < 16) ?
dv_vlc_map[run-1][0].vlc :
373 (0x1f80 | (run - 1))) <<
size;
374 size += (run < 16) ?
dv_vlc_map[run-1][0].size : 13;
375 }
376 }
377
379 }
380
382 {
384
387 }
388 else {
390 if (run) {
391 size += (run < 16) ?
dv_vlc_map[run-1][0].size : 13;
392 }
393 }
395 }
396 #else
398 {
401 }
402
404 {
406 }
407 #endif
408
422
426 {
427 int prev, bits_left;
431
433 for (;;){
434 /* Find suitable storage space */
436 if (bits_left) {
437 size -= bits_left;
438 put_bits(pb, bits_left, vlc >> size);
439 vlc = vlc & ((1 <<
size) - 1);
440 }
441 if (pb + 1 >= pb_end) {
444 return pb;
445 }
446 }
447
448 /* Store VLC */
450
452 break;
453
454 /* Construct the next VLC */
459 } else {
460 size = 4; vlc = 6; /* End Of Block stamp */
461 }
462 }
463 return pb;
464 }
465
468 int ps = s->
ildct_cmp(NULL, data, NULL, linesize, 8) - 400;
469 if (ps > 0) {
470 int is = s->
ildct_cmp(NULL, data , NULL, linesize<<1, 4) +
471 s->
ildct_cmp(NULL, data + linesize, NULL, linesize<<1, 4);
472 return ps > is;
473 }
474 }
475
476 return 0;
477 }
478
481 131072, 257107, 257107, 242189, 252167, 242189, 235923, 237536,
482 237536, 235923, 229376, 231390, 223754, 231390, 229376, 222935,
483 224969, 217965, 217965, 224969, 222935, 200636, 218652, 211916,
484 212325, 211916, 218652, 200636, 188995, 196781, 205965, 206433,
485 206433, 205965, 196781, 188995, 185364, 185364, 200636, 200704,
486 200636, 185364, 185364, 174609, 180568, 195068, 195068, 180568,
487 174609, 170091, 175557, 189591, 175557, 170091, 165371, 170627,
488 170627, 165371, 160727, 153560, 160727, 144651, 144651, 136258,
489 };
491 131072, 242189, 257107, 237536, 229376, 200636, 242189, 223754,
492 224969, 196781, 262144, 242189, 229376, 200636, 257107, 237536,
493 211916, 185364, 235923, 217965, 229376, 211916, 206433, 180568,
494 242189, 223754, 224969, 196781, 211916, 185364, 235923, 217965,
495 200704, 175557, 222935, 205965, 200636, 185364, 195068, 170627,
496 229376, 211916, 206433, 180568, 200704, 175557, 222935, 205965,
497 175557, 153560, 188995, 174609, 165371, 144651, 200636, 185364,
498 195068, 170627, 175557, 153560, 188995, 174609, 165371, 144651,
499 };
500
502 {
506 int i, area;
507 /* We offer two different methods for class number assignment: the
508 method suggested in SMPTE 314M Table 22, and an improved
509 method. The SMPTE method is very conservative; it assigns class
510 3 (i.e. severe quantization) to any block where the largest AC
511 component is greater than 36. FFmpeg's DV encoder tracks AC bit
512 consumption precisely, so there is no need to bias most blocks
513 towards strongly lossy compression. Instead, we assign class 2
514 to most blocks, and use class 3 only when strictly necessary
515 (for blocks whose largest AC component exceeds 255). */
516
517 #if 0 /* SMPTE spec method */
518 static const int classes[] = {12, 24, 36, 0xffff};
519 #else /* improved FFmpeg method */
520 static const int classes[] = {-1, -1, 255, 0xffff};
521 #endif
522 int max = classes[0];
523 int prev = 0;
524
526
531 if (data) {
535 } else {
536 /* We rely on the fact that encoding all zeros leads to an immediate EOB,
537 which is precisely what the spec calls for in the "dummy" blocks. */
538 memset(blk, 0, 64*sizeof(*blk));
540 }
542
545
546 for (area = 0; area < 4; area++) {
547 bi->
prev[area] = prev;
548 bi->
bit_size[area] = 1;
// 4 areas 4 bits for EOB :)
550 int level = blk[zigzag_scan[i]];
551
552 if (level + 15 > 30
U) {
553 bi->
sign[i] = (level >> 31) & 1;
554 /* weight it and and shift down into range, adding for rounding */
555 /* the extra division by a factor of 2^4 reverses the 8x expansion of the DCT
556 AND the 2x doubling of the weights */
557 level = (
FFABS(level) * weight[i] + (1 << (dv_weight_bits+3))) >> (dv_weight_bits+4);
559 if (level > max)
563 prev = i;
564 }
565 }
566 }
568 for (bi->
cno = 0; max > classes[bi->
cno]; bi->
cno++);
569
571
574 prev = 0;
576 for (area = 0; area < 4; area++) {
577 bi->
prev[area] = prev;
578 bi->
bit_size[area] = 1;
// 4 areas 4 bits for EOB :)
581
585 prev = i;
586 }
587 }
588 }
590 }
591
593 }
594
596 {
597 int size[5];
598 int i, j, k,
a, prev,
a2;
600
601 size[0] = size[1] = size[2] = size[3] = size[4] = 1 << 24;
602 do {
603 b = blks;
604 for (i = 0; i < 5; i++) {
605 if (!qnos[i])
606 continue;
607
608 qnos[i]--;
609 size[i] = 0;
610 for (j = 0; j < 6; j++, b++) {
611 for (a = 0; a < 4; a++) {
613 b->
bit_size[
a] = 1;
// 4 areas 4 bits for EOB :)
621 prev = k;
622 } else {
632 }
634 }
635 }
637 }
639 }
640 }
642 return;
643 }
644 } while (qnos[0]|qnos[1]|qnos[2]|qnos[3]|qnos[4]);
645
646
648 b = blks;
649 size[0] = 5 * 6 * 4; //EOB
650 for (j = 0; j < 6 *5; j++, b++) {
652 for (k = b->
next[prev]; k < 64; k = b->next[k]) {
653 if (b->
mb[k] < a && b->
mb[k] > -a){
655 }else{
657 prev = k;
658 }
659 }
660 }
661 }
662 }
663
665 {
668 int mb_index, i, j;
669 int mb_x, mb_y, c_offset, linesize, y_stride;
677 int vs_bit_size = 0;
678 int qnos[5] = {15, 15, 15, 15, 15}; /* No quantization */
679 int* qnosp = &qnos[0];
680
682 enc_blk = &enc_blks[0];
683 for (mb_index = 0; mb_index < 5; mb_index++) {
685
686 /* initializing luminance blocks */
689 (s->
sys->
height >= 720 && mb_y != 134)) {
691 } else {
692 y_stride = 16;
693 }
696
698 vs_bit_size +=
703 } else {
704 vs_bit_size +=
709 }
710 enc_blk += 4;
711
712 /* initializing chrominance blocks */
715 for (j = 2; j; j--) {
722 for (i = 0; i < 8; i++) {
723 d = c_ptr + (linesize << 3);
724 b[0] = c_ptr[0]; b[1] = c_ptr[1]; b[2] = c_ptr[2]; b[3] = c_ptr[3];
725 b[4] = d[0]; b[5] = d[1]; b[6] = d[2]; b[7] = d[3];
726 c_ptr += linesize;
727 b += 16;
728 }
729 c_ptr = scratch;
730 linesize = 16;
731 }
732
736 }
737 }
738 }
739
742
743 /* DIF encoding process */
744 for (j=0; j<5*s->
sys->
bpm;) {
745 int start_mb = j;
746
747 dif[3] = *qnosp++;
748 dif += 4;
749
750 /* First pass over individual cells only */
751 for (i=0; i<s->
sys->
bpm; i++, j++) {
753
755 put_sbits(&pbs[j], 9, ((enc_blks[j].
mb[0] >> 3) - 1024 + 2) >> 2);
756 put_bits(&pbs[j], 1, enc_blks[j].dct_mode);
757 put_bits(&pbs[j], 2, enc_blks[j].cno);
758
760 dif += sz;
761 }
762
763 /* Second pass over each MB space */
764 pb = &pbs[start_mb];
765 for (i=0; i<s->
sys->
bpm; i++) {
766 if (enc_blks[start_mb+i].partial_bit_count)
768 }
769 }
770
771 /* Third and final pass over the whole video segment space */
772 pb = &pbs[0];
773 for (j=0; j<5*s->
sys->
bpm; j++) {
774 if (enc_blks[j].partial_bit_count)
776 if (enc_blks[j].partial_bit_count)
778 }
779
780 for (j=0; j<5*s->
sys->
bpm; j++) {
781 int pos;
785 if (pos > size) {
787 return -1;
788 }
789 memset(pbs[j].
buf + pos, 0xff, size - pos);
790 }
791
792 return 0;
793 }
794
797 {
798 /*
799 * Here's what SMPTE314M says about these two:
800 * (page 6) APTn, AP1n, AP2n, AP3n: These data shall be identical
801 * as track application IDs (APTn = 001, AP1n =
802 * 001, AP2n = 001, AP3n = 001), if the source signal
803 * comes from a digital VCR. If the signal source is
804 * unknown, all bits for these data shall be set to 1.
805 * (page 12) STYPE: STYPE defines a signal type of video signal
806 * 00000b = 4:1:1 compression
807 * 00100b = 4:2:2 compression
808 * XXXXXX = Reserved
809 * Now, I've got two problems with these statements:
810 * 1. it looks like APT == 111b should be a safe bet, but it isn't.
811 * It seems that for PAL as defined in IEC 61834 we have to set
812 * APT to 000 and for SMPTE314M to 001.
813 * 2. It is not at all clear what STYPE is used for 4:2:0 PAL
814 * compression scheme (if any).
815 */
818
821 aspect = 0x02;
822
824 switch (pack_id) {
825 case dv_header525:
/* I can't imagine why these two weren't defined as real */
826 case dv_header625:
/* packs in SMPTE314M -- they definitely look like ones */
827 buf[1] = 0xf8 | /* reserved -- always 1 */
828 (apt & 0x07); /* APT: Track application ID */
829 buf[2] = (0 << 7) | /* TF1: audio data is 0 - valid; 1 - invalid */
830 (0x0f << 3) | /* reserved -- always 1 */
831 (apt & 0x07); /* AP1: Audio application ID */
832 buf[3] = (0 << 7) | /* TF2: video data is 0 - valid; 1 - invalid */
833 (0x0f << 3) | /* reserved -- always 1 */
834 (apt & 0x07); /* AP2: Video application ID */
835 buf[4] = (0 << 7) | /* TF3: subcode(SSYB) is 0 - valid; 1 - invalid */
836 (0x0f << 3) | /* reserved -- always 1 */
837 (apt & 0x07); /* AP3: Subcode application ID */
838 break;
840 buf[1] = 0xff; /* reserved -- always 1 */
841 buf[2] = (1 << 7) | /* B/W: 0 - b/w, 1 - color */
842 (1 << 6) | /* following CLF is valid - 0, invalid - 1 */
843 (3 << 4) | /* CLF: color frames ID (see ITU-R BT.470-4) */
844 0xf; /* reserved -- always 1 */
845 buf[3] = (3 << 6) | /* reserved -- always 1 */
846 (c->
sys->
dsf << 5) |
/* system: 60fields/50fields */
848 buf[4] = 0xff; /* VISC: 0xff -- no information */
849 break;
851 buf[1] = (0 << 6) | /* Copy generation management (CGMS) 0 -- free */
852 0x3f; /* reserved -- always 1 */
853 buf[2] = 0xc8 | /* reserved -- always b11001xxx */
854 aspect;
855 buf[3] = (1 << 7) | /* frame/field flag 1 -- frame, 0 -- field */
856 fs | /* first/second field flag 0 -- field 2, 1 -- field 1 */
857 (1 << 5) | /* frame change flag 0 -- same picture as before, 1 -- different */
858 (1 << 4) | /* 1 - interlaced, 0 - noninterlaced */
859 0xc; /* reserved -- always b1100 */
860 buf[4] = 0xff; /* reserved -- always 1 */
861 break;
862 default:
863 buf[1] = buf[2] = buf[3] = buf[4] = 0xff;
864 }
865 return 5;
866 }
867
868 #if CONFIG_DVVIDEO_ENCODER
872 {
873 buf[0] = (
uint8_t)t;
/* Section type */
874 buf[1] = (seq_num << 4) | /* DIF seq number 0-9 for 525/60; 0-11 for 625/50 */
875 (chan_num << 3) | /* FSC: for 50Mb/s 0 - first channel; 1 - second */
876 7; /* reserved -- always 1 */
877 buf[2] = dif_num; /* DIF block number Video: 0-134, Audio: 0-8 */
878 return 3;
879 }
880
881
883 {
884 if (syb_num == 0 || syb_num == 6) {
885 buf[0] = (fr << 7) | /* FR ID 1 - first half of each channel; 0 - second */
886 (0 << 4) | /* AP3 (Subcode application ID) */
887 0x0f; /* reserved -- always 1 */
888 }
889 else if (syb_num == 11) {
890 buf[0] = (fr << 7) | /* FR ID 1 - first half of each channel; 0 - second */
891 0x7f; /* reserved -- always 1 */
892 }
893 else {
894 buf[0] = (fr << 7) | /* FR ID 1 - first half of each channel; 0 - second */
895 (0 << 4) | /* APT (Track application ID) */
896 0x0f; /* reserved -- always 1 */
897 }
898 buf[1] = 0xf0 | /* reserved -- always 1 */
899 (syb_num & 0x0f); /* SSYB number 0 - 11 */
900 buf[2] = 0xff; /* reserved -- always 1 */
901 return 3;
902 }
903
905 {
906 int chan, i, j, k;
907
910 memset(buf, 0xff, 80 * 6); /* first 6 DIF blocks are for control data */
911
912 /* DV header: 1DIF */
915 buf += 72; /* unused bytes */
916
917 /* DV subcode: 2DIFs */
918 for (j = 0; j < 2; j++) {
920 for (k = 0; k < 6; k++)
921 buf += dv_write_ssyb_id(k, (i < c->sys->difseg_size/2), buf) + 5;
922 buf += 29; /* unused bytes */
923 }
924
925 /* DV VAUX: 3DIFS */
926 for (j = 0; j < 3; j++) {
930 buf += 7*5;
933 buf += 4*5 + 2; /* unused bytes */
934 }
935
936 /* DV Audio/Video: 135 Video DIFs + 9 Audio DIFs */
937 for (j = 0; j < 135; j++) {
938 if (j%15 == 0) {
939 memset(buf, 0xff, 80);
941 buf += 77; /* audio control & shuffled PCM audio */
942 }
944 buf += 77; /* 1 video macroblock: 1 bytes control
945 4 * 14 bytes Y 8x8 data
946 10 bytes Cr 8x8 data
947 10 bytes Cb 8x8 data */
948 }
949 }
950 }
951 }
952
953
956 {
959
962 return -1;
965
970
974
976
977 dv_format_frame(s, pkt->
data);
978
980 *got_packet = 1;
981
982 return 0;
983 }
984
991 .encode2 = dvvideo_encode_frame,
995 },
997 };
998 #endif // CONFIG_DVVIDEO_ENCODER