3 * Copyright (c) 2002 Fabrice Bellard.
4 * Copyright (c) 2004 Roman Shaposhnik.
7 * Copyright (c) 2003 Roman Shaposhnik.
9 * 50 Mbps (DVCPRO50) support
10 * Copyright (c) 2006 Daniel Maas <dmaas@maasdigital.com>
12 * Many thanks to Dan Dennedy <dan@dennedy.org> for providing wealth
13 * of DV technical info.
15 * This library is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU Lesser General Public
17 * License as published by the Free Software Foundation; either
18 * version 2 of the License, or (at your option) any later version.
20 * This library is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * Lesser General Public License for more details.
25 * You should have received a copy of the GNU Lesser General Public
26 * License along with this library; if not, write to the Free Software
27 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
34 #define ALT_BITSTREAM_READER
37 #include "mpegvideo.h"
38 #include "simple_idct.h"
44 typedef struct DVVideoContext {
47 AVCodecContext *avctx;
50 uint8_t dv_zigzag[2][64];
51 uint8_t dv_idct_shift[2][2][22][64];
53 void (*get_pixels)(DCTELEM *block, const uint8_t *pixels, int line_size);
54 void (*fdct[2])(DCTELEM *block);
55 void (*idct_put[2])(uint8_t *dest, int line_size, DCTELEM *block);
58 /* MultiThreading - dv_anchor applies to entire DV codec, not just the avcontext */
59 /* one element is needed for each video segment in a DV frame */
60 /* at most there are 2 DIF channels * 12 DIF sequences * 27 video segments (PAL 50Mbps) */
61 #define DV_ANCHOR_SIZE (2*12*27)
63 static void* dv_anchor[DV_ANCHOR_SIZE];
65 #define TEX_VLC_BITS 9
67 #ifdef DV_CODEC_TINY_TARGET
68 #define DV_VLC_MAP_RUN_SIZE 15
69 #define DV_VLC_MAP_LEV_SIZE 23
71 #define DV_VLC_MAP_RUN_SIZE 64
72 #define DV_VLC_MAP_LEV_SIZE 512 //FIXME sign was removed so this should be /2 but needs check
75 /* XXX: also include quantization */
76 static RL_VLC_ELEM *dv_rl_vlc;
77 /* VLC encoding lookup table */
78 static struct dv_vlc_pair {
81 } (*dv_vlc_map)[DV_VLC_MAP_LEV_SIZE] = NULL;
83 static void dv_build_unquantize_tables(DVVideoContext *s, uint8_t* perm)
87 /* NOTE: max left shift is 6 */
88 for(q = 0; q < 22; q++) {
90 for(i = 1; i < 64; i++) {
93 s->dv_idct_shift[0][0][q][j] =
94 dv_quant_shifts[q][dv_88_areas[i]] + 1;
95 s->dv_idct_shift[1][0][q][j] = s->dv_idct_shift[0][0][q][j] + 1;
99 for(i = 1; i < 64; i++) {
101 s->dv_idct_shift[0][1][q][i] =
102 dv_quant_shifts[q][dv_248_areas[i]] + 1;
103 s->dv_idct_shift[1][1][q][i] = s->dv_idct_shift[0][1][q][i] + 1;
108 static int dvvideo_init(AVCodecContext *avctx)
110 DVVideoContext *s = avctx->priv_data;
117 uint16_t new_dv_vlc_bits[NB_DV_VLC*2];
118 uint8_t new_dv_vlc_len[NB_DV_VLC*2];
119 uint8_t new_dv_vlc_run[NB_DV_VLC*2];
120 int16_t new_dv_vlc_level[NB_DV_VLC*2];
124 dv_vlc_map = av_mallocz_static(DV_VLC_MAP_LEV_SIZE*DV_VLC_MAP_RUN_SIZE*sizeof(struct dv_vlc_pair));
128 /* dv_anchor lets each thread know its Id */
129 for (i=0; i<DV_ANCHOR_SIZE; i++)
130 dv_anchor[i] = (void*)(size_t)i;
132 /* it's faster to include sign bit in a generic VLC parsing scheme */
133 for (i=0, j=0; i<NB_DV_VLC; i++, j++) {
134 new_dv_vlc_bits[j] = dv_vlc_bits[i];
135 new_dv_vlc_len[j] = dv_vlc_len[i];
136 new_dv_vlc_run[j] = dv_vlc_run[i];
137 new_dv_vlc_level[j] = dv_vlc_level[i];
139 if (dv_vlc_level[i]) {
140 new_dv_vlc_bits[j] <<= 1;
144 new_dv_vlc_bits[j] = (dv_vlc_bits[i] << 1) | 1;
145 new_dv_vlc_len[j] = dv_vlc_len[i] + 1;
146 new_dv_vlc_run[j] = dv_vlc_run[i];
147 new_dv_vlc_level[j] = -dv_vlc_level[i];
151 /* NOTE: as a trick, we use the fact the no codes are unused
152 to accelerate the parsing of partial codes */
153 init_vlc(&dv_vlc, TEX_VLC_BITS, j,
154 new_dv_vlc_len, 1, 1, new_dv_vlc_bits, 2, 2, 0);
156 dv_rl_vlc = av_mallocz_static(dv_vlc.table_size * sizeof(RL_VLC_ELEM));
160 for(i = 0; i < dv_vlc.table_size; i++){
161 int code= dv_vlc.table[i][0];
162 int len = dv_vlc.table[i][1];
165 if(len<0){ //more bits needed
169 run= new_dv_vlc_run[code] + 1;
170 level= new_dv_vlc_level[code];
172 dv_rl_vlc[i].len = len;
173 dv_rl_vlc[i].level = level;
174 dv_rl_vlc[i].run = run;
178 for (i = 0; i < NB_DV_VLC - 1; i++) {
179 if (dv_vlc_run[i] >= DV_VLC_MAP_RUN_SIZE)
181 #ifdef DV_CODEC_TINY_TARGET
182 if (dv_vlc_level[i] >= DV_VLC_MAP_LEV_SIZE)
186 if (dv_vlc_map[dv_vlc_run[i]][dv_vlc_level[i]].size != 0)
189 dv_vlc_map[dv_vlc_run[i]][dv_vlc_level[i]].vlc = dv_vlc_bits[i] <<
191 dv_vlc_map[dv_vlc_run[i]][dv_vlc_level[i]].size = dv_vlc_len[i] +
194 for (i = 0; i < DV_VLC_MAP_RUN_SIZE; i++) {
195 #ifdef DV_CODEC_TINY_TARGET
196 for (j = 1; j < DV_VLC_MAP_LEV_SIZE; j++) {
197 if (dv_vlc_map[i][j].size == 0) {
198 dv_vlc_map[i][j].vlc = dv_vlc_map[0][j].vlc |
199 (dv_vlc_map[i-1][0].vlc << (dv_vlc_map[0][j].size));
200 dv_vlc_map[i][j].size = dv_vlc_map[i-1][0].size +
201 dv_vlc_map[0][j].size;
205 for (j = 1; j < DV_VLC_MAP_LEV_SIZE/2; j++) {
206 if (dv_vlc_map[i][j].size == 0) {
207 dv_vlc_map[i][j].vlc = dv_vlc_map[0][j].vlc |
208 (dv_vlc_map[i-1][0].vlc << (dv_vlc_map[0][j].size));
209 dv_vlc_map[i][j].size = dv_vlc_map[i-1][0].size +
210 dv_vlc_map[0][j].size;
212 dv_vlc_map[i][((uint16_t)(-j))&0x1ff].vlc =
213 dv_vlc_map[i][j].vlc | 1;
214 dv_vlc_map[i][((uint16_t)(-j))&0x1ff].size =
215 dv_vlc_map[i][j].size;
221 /* Generic DSP setup */
222 dsputil_init(&dsp, avctx);
223 s->get_pixels = dsp.get_pixels;
226 s->fdct[0] = dsp.fdct;
227 s->idct_put[0] = dsp.idct_put;
229 s->dv_zigzag[0][i] = dsp.idct_permutation[ff_zigzag_direct[i]];
232 s->fdct[1] = dsp.fdct248;
233 s->idct_put[1] = simple_idct248_put; // FIXME: need to add it to DSP
235 for (i=0; i<64; i++){
236 int j= ff_zigzag248_direct[i];
237 s->dv_zigzag[1][i] = dsp.idct_permutation[(j&7) + (j&8)*4 + (j&48)/2];
240 memcpy(s->dv_zigzag[1], ff_zigzag248_direct, 64);
242 /* XXX: do it only for constant case */
243 dv_build_unquantize_tables(s, dsp.idct_permutation);
245 avctx->coded_frame = &s->picture;
252 // #define printf(...) av_log(NULL, AV_LOG_ERROR, __VA_ARGS__)
254 typedef struct BlockInfo {
255 const uint8_t *shift_table;
256 const uint8_t *scan_table;
257 const int *iweight_table;
258 uint8_t pos; /* position in block */
260 uint8_t partial_bit_count;
261 uint16_t partial_bit_buffer;
265 /* block size in bits */
266 static const uint16_t block_sizes[6] = {
267 112, 112, 112, 112, 80, 80
269 /* bit budget for AC only in 5 MBs */
270 static const int vs_total_ac_bits = (100 * 4 + 68*2) * 5;
271 /* see dv_88_areas and dv_248_areas for details */
272 static const int mb_area_start[5] = { 1, 6, 21, 43, 64 };
274 static inline int get_bits_left(GetBitContext *s)
276 return s->size_in_bits - get_bits_count(s);
279 static inline int get_bits_size(GetBitContext *s)
281 return s->size_in_bits;
284 static inline int put_bits_left(PutBitContext* s)
286 return (s->buf_end - s->buf) * 8 - put_bits_count(s);
289 /* decode ac coefs */
290 static void dv_decode_ac(GetBitContext *gb, BlockInfo *mb, DCTELEM *block)
292 int last_index = get_bits_size(gb);
293 const uint8_t *scan_table = mb->scan_table;
294 const uint8_t *shift_table = mb->shift_table;
295 const int *iweight_table = mb->iweight_table;
297 int partial_bit_count = mb->partial_bit_count;
298 int level, pos1, run, vlc_len, index;
301 UPDATE_CACHE(re, gb);
303 /* if we must parse a partial vlc, we do it here */
304 if (partial_bit_count > 0) {
305 re_cache = ((unsigned)re_cache >> partial_bit_count) |
306 (mb->partial_bit_buffer << (sizeof(re_cache)*8 - partial_bit_count));
307 re_index -= partial_bit_count;
308 mb->partial_bit_count = 0;
311 /* get the AC coefficients until last_index is reached */
314 printf("%2d: bits=%04x index=%d\n", pos, SHOW_UBITS(re, gb, 16), re_index);
316 /* our own optimized GET_RL_VLC */
317 index = NEG_USR32(re_cache, TEX_VLC_BITS);
318 vlc_len = dv_rl_vlc[index].len;
320 index = NEG_USR32((unsigned)re_cache << TEX_VLC_BITS, -vlc_len) + dv_rl_vlc[index].level;
321 vlc_len = TEX_VLC_BITS - vlc_len;
323 level = dv_rl_vlc[index].level;
324 run = dv_rl_vlc[index].run;
326 /* gotta check if we're still within gb boundaries */
327 if (re_index + vlc_len > last_index) {
328 /* should be < 16 bits otherwise a codeword could have been parsed */
329 mb->partial_bit_count = last_index - re_index;
330 mb->partial_bit_buffer = NEG_USR32(re_cache, mb->partial_bit_count);
331 re_index = last_index;
337 printf("run=%d level=%d\n", run, level);
343 pos1 = scan_table[pos];
344 level <<= shift_table[pos1];
346 /* unweigh, round, and shift down */
347 level = (level*iweight_table[pos] + (1 << (dv_iweight_bits-1))) >> dv_iweight_bits;
351 UPDATE_CACHE(re, gb);
353 CLOSE_READER(re, gb);
357 static inline void bit_copy(PutBitContext *pb, GetBitContext *gb)
359 int bits_left = get_bits_left(gb);
360 while (bits_left >= MIN_CACHE_BITS) {
361 put_bits(pb, MIN_CACHE_BITS, get_bits(gb, MIN_CACHE_BITS));
362 bits_left -= MIN_CACHE_BITS;
365 put_bits(pb, bits_left, get_bits(gb, bits_left));
369 /* mb_x and mb_y are in units of 8 pixels */
370 static inline void dv_decode_video_segment(DVVideoContext *s,
372 const uint16_t *mb_pos_ptr)
374 int quant, dc, dct_mode, class1, j;
375 int mb_index, mb_x, mb_y, v, last_index;
376 DCTELEM *block, *block1;
379 void (*idct_put)(uint8_t *dest, int line_size, DCTELEM *block);
381 PutBitContext pb, vs_pb;
383 BlockInfo mb_data[5 * 6], *mb, *mb1;
384 DECLARE_ALIGNED_8(DCTELEM, sblock[5*6][64]);
385 DECLARE_ALIGNED_8(uint8_t, mb_bit_buffer[80 + 4]); /* allow some slack */
386 DECLARE_ALIGNED_8(uint8_t, vs_bit_buffer[5 * 80 + 4]); /* allow some slack */
387 const int log2_blocksize= 3-s->avctx->lowres;
389 assert((((int)mb_bit_buffer)&7)==0);
390 assert((((int)vs_bit_buffer)&7)==0);
392 memset(sblock, 0, sizeof(sblock));
394 /* pass 1 : read DC and AC coefficients in blocks */
396 block1 = &sblock[0][0];
398 init_put_bits(&vs_pb, vs_bit_buffer, 5 * 80);
399 for(mb_index = 0; mb_index < 5; mb_index++, mb1 += 6, block1 += 6 * 64) {
401 quant = buf_ptr[3] & 0x0f;
403 init_put_bits(&pb, mb_bit_buffer, 80);
406 for(j = 0;j < 6; j++) {
407 last_index = block_sizes[j];
408 init_get_bits(&gb, buf_ptr, last_index);
411 dc = get_sbits(&gb, 9);
412 dct_mode = get_bits1(&gb);
413 mb->dct_mode = dct_mode;
414 mb->scan_table = s->dv_zigzag[dct_mode];
415 mb->iweight_table = dct_mode ? dv_iweight_248 : dv_iweight_88;
416 class1 = get_bits(&gb, 2);
417 mb->shift_table = s->dv_idct_shift[class1 == 3][dct_mode]
418 [quant + dv_quant_offset[class1]];
420 /* convert to unsigned because 128 is not added in the
424 buf_ptr += last_index >> 3;
426 mb->partial_bit_count = 0;
429 printf("MB block: %d, %d ", mb_index, j);
431 dv_decode_ac(&gb, mb, block);
433 /* write the remaining bits in a new buffer only if the
442 /* pass 2 : we can do it just after */
444 printf("***pass 2 size=%d MB#=%d\n", put_bits_count(&pb), mb_index);
448 init_get_bits(&gb, mb_bit_buffer, put_bits_count(&pb));
450 for(j = 0;j < 6; j++, block += 64, mb++) {
451 if (mb->pos < 64 && get_bits_left(&gb) > 0) {
452 dv_decode_ac(&gb, mb, block);
453 /* if still not finished, no need to parse other blocks */
458 /* all blocks are finished, so the extra bytes can be used at
459 the video segment level */
461 bit_copy(&vs_pb, &gb);
464 /* we need a pass other the whole video segment */
466 printf("***pass 3 size=%d\n", put_bits_count(&vs_pb));
468 block = &sblock[0][0];
470 init_get_bits(&gb, vs_bit_buffer, put_bits_count(&vs_pb));
471 flush_put_bits(&vs_pb);
472 for(mb_index = 0; mb_index < 5; mb_index++) {
473 for(j = 0;j < 6; j++) {
476 printf("start %d:%d\n", mb_index, j);
478 dv_decode_ac(&gb, mb, block);
480 if (mb->pos >= 64 && mb->pos < 127)
481 av_log(NULL, AV_LOG_ERROR, "AC EOB marker is absent pos=%d\n", mb->pos);
487 /* compute idct and place blocks */
488 block = &sblock[0][0];
490 for(mb_index = 0; mb_index < 5; mb_index++) {
494 if (s->sys->pix_fmt == PIX_FMT_YUV422P) {
495 y_ptr = s->picture.data[0] + ((mb_y * s->picture.linesize[0] + (mb_x>>1))<<log2_blocksize);
496 c_offset = ((mb_y * s->picture.linesize[1] + (mb_x >> 2))<<log2_blocksize);
497 } else { /* 4:1:1 or 4:2:0 */
498 y_ptr = s->picture.data[0] + ((mb_y * s->picture.linesize[0] + mb_x)<<log2_blocksize);
499 if (s->sys->pix_fmt == PIX_FMT_YUV411P)
500 c_offset = ((mb_y * s->picture.linesize[1] + (mb_x >> 2))<<log2_blocksize);
502 c_offset = (((mb_y >> 1) * s->picture.linesize[1] + (mb_x >> 1))<<log2_blocksize);
504 for(j = 0;j < 6; j++) {
505 idct_put = s->idct_put[mb->dct_mode && log2_blocksize==3];
506 if (s->sys->pix_fmt == PIX_FMT_YUV422P) { /* 4:2:2 */
507 if (j == 0 || j == 2) {
509 idct_put(y_ptr + ((j >> 1)<<log2_blocksize),
510 s->picture.linesize[0], block);
513 idct_put(s->picture.data[6 - j] + c_offset,
514 s->picture.linesize[6 - j], block);
516 /* note: j=1 and j=3 are "dummy" blocks in 4:2:2 */
517 } else { /* 4:1:1 or 4:2:0 */
519 if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x < (704 / 8)) {
520 /* NOTE: at end of line, the macroblock is handled as 420 */
521 idct_put(y_ptr + (j<<log2_blocksize), s->picture.linesize[0], block);
523 idct_put(y_ptr + (((j & 1) + (j >> 1) * s->picture.linesize[0])<<log2_blocksize),
524 s->picture.linesize[0], block);
527 if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8)) {
528 uint64_t aligned_pixels[64/8];
529 uint8_t *pixels= (uint8_t*)aligned_pixels;
530 uint8_t *c_ptr, *c_ptr1, *ptr, *ptr1;
532 /* NOTE: at end of line, the macroblock is handled as 420 */
533 idct_put(pixels, 8, block);
534 linesize = s->picture.linesize[6 - j];
535 c_ptr = s->picture.data[6 - j] + c_offset;
537 for(y = 0;y < (1<<log2_blocksize); y++) {
538 ptr1= ptr + (1<<(log2_blocksize-1));
539 c_ptr1 = c_ptr + (linesize<<log2_blocksize);
540 for(x=0; x < (1<<(log2_blocksize-1)); x++){
541 c_ptr[x]= ptr[x]; c_ptr1[x]= ptr1[x];
547 /* don't ask me why they inverted Cb and Cr ! */
548 idct_put(s->picture.data[6 - j] + c_offset,
549 s->picture.linesize[6 - j], block);
559 #ifdef DV_CODEC_TINY_TARGET
560 /* Converts run and level (where level != 0) pair into vlc, returning bit size */
561 static always_inline int dv_rl2vlc(int run, int level, int sign, uint32_t* vlc)
564 if (run < DV_VLC_MAP_RUN_SIZE && level < DV_VLC_MAP_LEV_SIZE) {
565 *vlc = dv_vlc_map[run][level].vlc | sign;
566 size = dv_vlc_map[run][level].size;
569 if (level < DV_VLC_MAP_LEV_SIZE) {
570 *vlc = dv_vlc_map[0][level].vlc | sign;
571 size = dv_vlc_map[0][level].size;
573 *vlc = 0xfe00 | (level << 1) | sign;
577 *vlc |= ((run < 16) ? dv_vlc_map[run-1][0].vlc :
578 (0x1f80 | (run - 1))) << size;
579 size += (run < 16) ? dv_vlc_map[run-1][0].size : 13;
586 static always_inline int dv_rl2vlc_size(int run, int level)
590 if (run < DV_VLC_MAP_RUN_SIZE && level < DV_VLC_MAP_LEV_SIZE) {
591 size = dv_vlc_map[run][level].size;
594 size = (level < DV_VLC_MAP_LEV_SIZE) ? dv_vlc_map[0][level].size : 16;
596 size += (run < 16) ? dv_vlc_map[run-1][0].size : 13;
602 static always_inline int dv_rl2vlc(int run, int l, int sign, uint32_t* vlc)
604 *vlc = dv_vlc_map[run][l].vlc | sign;
605 return dv_vlc_map[run][l].size;
608 static always_inline int dv_rl2vlc_size(int run, int l)
610 return dv_vlc_map[run][l].size;
614 typedef struct EncBlockInfo {
624 uint8_t partial_bit_count;
625 uint32_t partial_bit_buffer; /* we can't use uint16_t here */
628 static always_inline PutBitContext* dv_encode_ac(EncBlockInfo* bi, PutBitContext* pb_pool,
629 PutBitContext* pb_end)
633 PutBitContext* pb = pb_pool;
634 int size = bi->partial_bit_count;
635 uint32_t vlc = bi->partial_bit_buffer;
637 bi->partial_bit_count = bi->partial_bit_buffer = 0;
639 /* Find suitable storage space */
640 for (; size > (bits_left = put_bits_left(pb)); pb++) {
643 put_bits(pb, bits_left, vlc >> size);
644 vlc = vlc & ((1<<size)-1);
646 if (pb + 1 >= pb_end) {
647 bi->partial_bit_count = size;
648 bi->partial_bit_buffer = vlc;
654 put_bits(pb, size, vlc);
659 /* Construct the next VLC */
661 bi->cur_ac = bi->next[prev];
663 size = dv_rl2vlc(bi->cur_ac - prev - 1, bi->mb[bi->cur_ac], bi->sign[bi->cur_ac], &vlc);
665 size = 4; vlc = 6; /* End Of Block stamp */
671 static always_inline void dv_set_class_number(DCTELEM* blk, EncBlockInfo* bi,
672 const uint8_t* zigzag_scan, const int *weight, int bias)
675 /* We offer two different methods for class number assignment: the
676 method suggested in SMPTE 314M Table 22, and an improved
677 method. The SMPTE method is very conservative; it assigns class
678 3 (i.e. severe quantization) to any block where the largest AC
679 component is greater than 36. ffmpeg's DV encoder tracks AC bit
680 consumption precisely, so there is no need to bias most blocks
681 towards strongly lossy compression. Instead, we assign class 2
682 to most blocks, and use class 3 only when strictly necessary
683 (for blocks whose largest AC component exceeds 255). */
685 #if 0 /* SMPTE spec method */
686 static const int classes[] = {12, 24, 36, 0xffff};
687 #else /* improved ffmpeg method */
688 static const int classes[] = {-1, -1, 255, 0xffff};
695 for (area = 0; area < 4; area++) {
696 bi->prev[area] = prev;
697 bi->bit_size[area] = 1; // 4 areas 4 bits for EOB :)
698 for (i=mb_area_start[area]; i<mb_area_start[area+1]; i++) {
699 int level = blk[zigzag_scan[i]];
701 if (level+15 > 30U) {
702 bi->sign[i] = (level>>31)&1;
703 /* weigh it and and shift down into range, adding for rounding */
704 /* the extra division by a factor of 2^4 reverses the 8x expansion of the DCT
705 AND the 2x doubling of the weights */
706 level = (ABS(level) * weight[i] + (1<<(dv_weight_bits+3))) >> (dv_weight_bits+4);
708 if(level>max) max= level;
709 bi->bit_size[area] += dv_rl2vlc_size(i - prev - 1, level);
716 for(bi->cno = 0; max > classes[bi->cno]; bi->cno++);
724 for (area = 0; area < 4; area++) {
725 bi->prev[area] = prev;
726 bi->bit_size[area] = 1; // 4 areas 4 bits for EOB :)
727 for (; i<mb_area_start[area+1]; i= bi->next[i]) {
731 bi->bit_size[area] += dv_rl2vlc_size(i - prev - 1, bi->mb[i]);
741 //FIXME replace this by dsputil
742 #define SC(x, y) ((s[x] - s[y]) ^ ((s[x] - s[y]) >> 7))
743 static always_inline int dv_guess_dct_mode(DCTELEM *blk) {
749 /* Compute 8-8 score (small values give a better chance for 8-8 DCT) */
752 score88 += SC(0, 8) + SC(1, 9) + SC(2, 10) + SC(3, 11) +
753 SC(4, 12) + SC(5,13) + SC(6, 14) + SC(7, 15);
756 /* Compute 2-4-8 score (small values give a better chance for 2-4-8 DCT) */
759 score248 += SC(0, 16) + SC(1,17) + SC(2, 18) + SC(3, 19) +
760 SC(4, 20) + SC(5,21) + SC(6, 22) + SC(7, 23);
764 return (score88 - score248 > -10);
767 static inline void dv_guess_qnos(EncBlockInfo* blks, int* qnos)
770 int i, j, k, a, prev, a2;
773 size[0] = size[1] = size[2] = size[3] = size[4] = 1<<24;
776 for (i=0; i<5; i++) {
782 for (j=0; j<6; j++, b++) {
783 for (a=0; a<4; a++) {
784 if (b->area_q[a] != dv_quant_shifts[qnos[i] + dv_quant_offset[b->cno]][a]) {
785 b->bit_size[a] = 1; // 4 areas 4 bits for EOB :)
788 assert(b->next[prev] >= mb_area_start[a+1] || b->mb[prev]);
789 for (k= b->next[prev] ; k<mb_area_start[a+1]; k= b->next[k]) {
792 b->bit_size[a] += dv_rl2vlc_size(k - prev - 1, b->mb[k]);
795 if(b->next[k] >= mb_area_start[a+1] && b->next[k]<64){
796 for(a2=a+1; b->next[k] >= mb_area_start[a2+1]; a2++)
799 assert(b->mb[b->next[k]]);
800 b->bit_size[a2] += dv_rl2vlc_size(b->next[k] - prev - 1, b->mb[b->next[k]])
801 -dv_rl2vlc_size(b->next[k] - k - 1, b->mb[b->next[k]]);
802 assert(b->prev[a2]==k && (a2+1 >= 4 || b->prev[a2+1]!=k));
805 b->next[prev] = b->next[k];
810 size[i] += b->bit_size[a];
813 if(vs_total_ac_bits >= size[0] + size[1] + size[2] + size[3] + size[4])
816 } while (qnos[0]|qnos[1]|qnos[2]|qnos[3]|qnos[4]);
819 for(a=2; a==2 || vs_total_ac_bits < size[0]; a+=a){
821 size[0] = 5*6*4; //EOB
822 for (j=0; j<6*5; j++, b++) {
824 for (k= b->next[prev]; k<64; k= b->next[k]) {
825 if(b->mb[k] < a && b->mb[k] > -a){
826 b->next[prev] = b->next[k];
828 size[0] += dv_rl2vlc_size(k - prev - 1, b->mb[k]);
836 static inline void dv_encode_video_segment(DVVideoContext *s,
838 const uint16_t *mb_pos_ptr)
840 int mb_index, i, j, v;
841 int mb_x, mb_y, c_offset, linesize;
846 DECLARE_ALIGNED_8(DCTELEM, block[64]);
847 EncBlockInfo enc_blks[5*6];
848 PutBitContext pbs[5*6];
850 EncBlockInfo* enc_blk;
854 assert((((int)block) & 7) == 0);
856 enc_blk = &enc_blks[0];
858 for(mb_index = 0; mb_index < 5; mb_index++) {
862 if (s->sys->pix_fmt == PIX_FMT_YUV422P) {
863 y_ptr = s->picture.data[0] + (mb_y * s->picture.linesize[0] * 8) + (mb_x * 4);
865 y_ptr = s->picture.data[0] + (mb_y * s->picture.linesize[0] * 8) + (mb_x * 8);
867 if (s->sys->pix_fmt == PIX_FMT_YUV420P) {
868 c_offset = (((mb_y >> 1) * s->picture.linesize[1] * 8) + ((mb_x >> 1) * 8));
869 } else { /* 4:2:2 or 4:1:1 */
870 c_offset = ((mb_y * s->picture.linesize[1] * 8) + ((mb_x >> 2) * 8));
873 qnos[mb_index] = 15; /* No quantization */
874 ptr = dif + mb_index*80 + 4;
875 for(j = 0;j < 6; j++) {
877 if (s->sys->pix_fmt == PIX_FMT_YUV422P) { /* 4:2:2 */
878 if (j == 0 || j == 2) {
880 data = y_ptr + ((j>>1) * 8);
881 linesize = s->picture.linesize[0];
884 data = s->picture.data[6 - j] + c_offset;
885 linesize = s->picture.linesize[6 - j];
887 /* j=1 and j=3 are "dummy" blocks, used for AC data only */
892 } else { /* 4:1:1 or 4:2:0 */
893 if (j < 4) { /* Four Y blocks */
894 /* NOTE: at end of line, the macroblock is handled as 420 */
895 if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x < (704 / 8)) {
896 data = y_ptr + (j * 8);
898 data = y_ptr + ((j & 1) * 8) + ((j >> 1) * 8 * s->picture.linesize[0]);
900 linesize = s->picture.linesize[0];
901 } else { /* Cr and Cb blocks */
902 /* don't ask Fabrice why they inverted Cb and Cr ! */
903 data = s->picture.data[6 - j] + c_offset;
904 linesize = s->picture.linesize[6 - j];
905 if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8))
910 /* Everything is set up -- now just copy data -> DCT block */
911 if (do_edge_wrap) { /* Edge wrap copy: 4x16 -> 8x8 */
915 d = data + 8 * linesize;
916 b[0] = data[0]; b[1] = data[1]; b[2] = data[2]; b[3] = data[3];
917 b[4] = d[0]; b[5] = d[1]; b[6] = d[2]; b[7] = d[3];
921 } else { /* Simple copy: 8x8 -> 8x8 */
923 s->get_pixels(block, data, linesize);
926 if(s->avctx->flags & CODEC_FLAG_INTERLACED_DCT)
927 enc_blk->dct_mode = dv_guess_dct_mode(block);
929 enc_blk->dct_mode = 0;
930 enc_blk->area_q[0] = enc_blk->area_q[1] = enc_blk->area_q[2] = enc_blk->area_q[3] = 0;
931 enc_blk->partial_bit_count = 0;
932 enc_blk->partial_bit_buffer = 0;
936 /* We rely on the fact that encoding all zeros leads to an immediate EOB,
937 which is precisely what the spec calls for in the "dummy" blocks. */
938 memset(block, 0, sizeof(block));
940 s->fdct[enc_blk->dct_mode](block);
943 dv_set_class_number(block, enc_blk,
944 enc_blk->dct_mode ? ff_zigzag248_direct : ff_zigzag_direct,
945 enc_blk->dct_mode ? dv_weight_248 : dv_weight_88,
948 init_put_bits(pb, ptr, block_sizes[j]/8);
949 put_bits(pb, 9, (uint16_t)(((enc_blk->mb[0] >> 3) - 1024 + 2) >> 2));
950 put_bits(pb, 1, enc_blk->dct_mode);
951 put_bits(pb, 2, enc_blk->cno);
953 vs_bit_size += enc_blk->bit_size[0] + enc_blk->bit_size[1] +
954 enc_blk->bit_size[2] + enc_blk->bit_size[3];
957 ptr += block_sizes[j]/8;
961 if (vs_total_ac_bits < vs_bit_size)
962 dv_guess_qnos(&enc_blks[0], &qnos[0]);
964 for (i=0; i<5; i++) {
965 dif[i*80 + 3] = qnos[i];
968 /* First pass over individual cells only */
969 for (j=0; j<5*6; j++)
970 dv_encode_ac(&enc_blks[j], &pbs[j], &pbs[j+1]);
972 /* Second pass over each MB space */
973 for (j=0; j<5*6; j+=6) {
975 for (i=0; i<6; i++) {
976 if (enc_blks[i+j].partial_bit_count)
977 pb=dv_encode_ac(&enc_blks[i+j], pb, &pbs[j+6]);
981 /* Third and final pass over the whole vides segment space */
983 for (j=0; j<5*6; j++) {
984 if (enc_blks[j].partial_bit_count)
985 pb=dv_encode_ac(&enc_blks[j], pb, &pbs[6*5]);
986 if (enc_blks[j].partial_bit_count)
987 av_log(NULL, AV_LOG_ERROR, "ac bitstream overflow\n");
990 for (j=0; j<5*6; j++)
991 flush_put_bits(&pbs[j]);
994 static int dv_decode_mt(AVCodecContext *avctx, void* sl)
996 DVVideoContext *s = avctx->priv_data;
997 int slice = (size_t)sl;
999 /* which DIF channel is this? */
1000 int chan = slice / (s->sys->difseg_size * 27);
1002 /* slice within the DIF channel */
1003 int chan_slice = slice % (s->sys->difseg_size * 27);
1005 /* byte offset of this channel's data */
1006 int chan_offset = chan * s->sys->difseg_size * 150 * 80;
1008 dv_decode_video_segment(s, &s->buf[((chan_slice/27)*6+(chan_slice/3)+chan_slice*5+7)*80 + chan_offset],
1009 &s->sys->video_place[slice*5]);
1013 static int dv_encode_mt(AVCodecContext *avctx, void* sl)
1015 DVVideoContext *s = avctx->priv_data;
1016 int slice = (size_t)sl;
1018 /* which DIF channel is this? */
1019 int chan = slice / (s->sys->difseg_size * 27);
1021 /* slice within the DIF channel */
1022 int chan_slice = slice % (s->sys->difseg_size * 27);
1024 /* byte offset of this channel's data */
1025 int chan_offset = chan * s->sys->difseg_size * 150 * 80;
1027 dv_encode_video_segment(s, &s->buf[((chan_slice/27)*6+(chan_slice/3)+chan_slice*5+7)*80 + chan_offset],
1028 &s->sys->video_place[slice*5]);
1032 /* NOTE: exactly one frame must be given (120000 bytes for NTSC,
1033 144000 bytes for PAL - or twice those for 50Mbps) */
1034 static int dvvideo_decode_frame(AVCodecContext *avctx,
1035 void *data, int *data_size,
1036 uint8_t *buf, int buf_size)
1038 DVVideoContext *s = avctx->priv_data;
1040 s->sys = dv_frame_profile(buf);
1041 if (!s->sys || buf_size < s->sys->frame_size)
1042 return -1; /* NOTE: we only accept several full frames */
1044 if(s->picture.data[0])
1045 avctx->release_buffer(avctx, &s->picture);
1047 s->picture.reference = 0;
1048 s->picture.key_frame = 1;
1049 s->picture.pict_type = FF_I_TYPE;
1050 avctx->pix_fmt = s->sys->pix_fmt;
1051 avcodec_set_dimensions(avctx, s->sys->width, s->sys->height);
1052 if(avctx->get_buffer(avctx, &s->picture) < 0) {
1053 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1056 s->picture.interlaced_frame = 1;
1057 s->picture.top_field_first = 0;
1060 avctx->execute(avctx, dv_decode_mt, (void**)&dv_anchor[0], NULL,
1061 s->sys->n_difchan * s->sys->difseg_size * 27);
1066 *data_size = sizeof(AVFrame);
1067 *(AVFrame*)data= s->picture;
1069 return s->sys->frame_size;
1073 static inline int dv_write_pack(enum dv_pack_type pack_id, DVVideoContext *c, uint8_t* buf)
1076 * Here's what SMPTE314M says about these two:
1077 * (page 6) APTn, AP1n, AP2n, AP3n: These data shall be identical
1078 * as track application IDs (APTn = 001, AP1n =
1079 * 001, AP2n = 001, AP3n = 001), if the source signal
1080 * comes from a digital VCR. If the signal source is
1081 * unknown, all bits for these data shall be set to 1.
1082 * (page 12) STYPE: STYPE defines a signal type of video signal
1083 * 00000b = 4:1:1 compression
1084 * 00100b = 4:2:2 compression
1086 * Now, I've got two problems with these statements:
1087 * 1. it looks like APT == 111b should be a safe bet, but it isn't.
1088 * It seems that for PAL as defined in IEC 61834 we have to set
1089 * APT to 000 and for SMPTE314M to 001.
1090 * 2. It is not at all clear what STYPE is used for 4:2:0 PAL
1091 * compression scheme (if any).
1093 int apt = (c->sys->pix_fmt == PIX_FMT_YUV420P ? 0 : 1);
1094 int stype = (c->sys->pix_fmt == PIX_FMT_YUV422P ? 4 : 0);
1097 if((int)(av_q2d(c->avctx->sample_aspect_ratio) * c->avctx->width / c->avctx->height * 10) == 17) /* 16:9 */
1100 buf[0] = (uint8_t)pack_id;
1102 case dv_header525: /* I can't imagine why these two weren't defined as real */
1103 case dv_header625: /* packs in SMPTE314M -- they definitely look like ones */
1104 buf[1] = 0xf8 | /* reserved -- always 1 */
1105 (apt & 0x07); /* APT: Track application ID */
1106 buf[2] = (0 << 7) | /* TF1: audio data is 0 - valid; 1 - invalid */
1107 (0x0f << 3) | /* reserved -- always 1 */
1108 (apt & 0x07); /* AP1: Audio application ID */
1109 buf[3] = (0 << 7) | /* TF2: video data is 0 - valid; 1 - invalid */
1110 (0x0f << 3) | /* reserved -- always 1 */
1111 (apt & 0x07); /* AP2: Video application ID */
1112 buf[4] = (0 << 7) | /* TF3: subcode(SSYB) is 0 - valid; 1 - invalid */
1113 (0x0f << 3) | /* reserved -- always 1 */
1114 (apt & 0x07); /* AP3: Subcode application ID */
1116 case dv_video_source:
1117 buf[1] = 0xff; /* reserved -- always 1 */
1118 buf[2] = (1 << 7) | /* B/W: 0 - b/w, 1 - color */
1119 (1 << 6) | /* following CLF is valid - 0, invalid - 1 */
1120 (3 << 4) | /* CLF: color frames id (see ITU-R BT.470-4) */
1121 0xf; /* reserved -- always 1 */
1122 buf[3] = (3 << 6) | /* reserved -- always 1 */
1123 (c->sys->dsf << 5) | /* system: 60fields/50fields */
1124 stype; /* signal type video compression */
1125 buf[4] = 0xff; /* VISC: 0xff -- no information */
1127 case dv_video_control:
1128 buf[1] = (0 << 6) | /* Copy generation management (CGMS) 0 -- free */
1129 0x3f; /* reserved -- always 1 */
1130 buf[2] = 0xc8 | /* reserved -- always b11001xxx */
1132 buf[3] = (1 << 7) | /* Frame/field flag 1 -- frame, 0 -- field */
1133 (1 << 6) | /* First/second field flag 0 -- field 2, 1 -- field 1 */
1134 (1 << 5) | /* Frame change flag 0 -- same picture as before, 1 -- different */
1135 (1 << 4) | /* 1 - interlaced, 0 - noninterlaced */
1136 0xc; /* reserved -- always b1100 */
1137 buf[4] = 0xff; /* reserved -- always 1 */
1140 buf[1] = buf[2] = buf[3] = buf[4] = 0xff;
1145 static void dv_format_frame(DVVideoContext* c, uint8_t* buf)
1149 for (chan = 0; chan < c->sys->n_difchan; chan++) {
1150 for (i = 0; i < c->sys->difseg_size; i++) {
1151 memset(buf, 0xff, 80 * 6); /* First 6 DIF blocks are for control data */
1153 /* DV header: 1DIF */
1154 buf += dv_write_dif_id(dv_sect_header, chan, i, 0, buf);
1155 buf += dv_write_pack((c->sys->dsf ? dv_header625 : dv_header525), c, buf);
1156 buf += 72; /* unused bytes */
1158 /* DV subcode: 2DIFs */
1159 for (j = 0; j < 2; j++) {
1160 buf += dv_write_dif_id(dv_sect_subcode, chan, i, j, buf);
1161 for (k = 0; k < 6; k++)
1162 buf += dv_write_ssyb_id(k, (i < c->sys->difseg_size/2), buf) + 5;
1163 buf += 29; /* unused bytes */
1166 /* DV VAUX: 3DIFS */
1167 for (j = 0; j < 3; j++) {
1168 buf += dv_write_dif_id(dv_sect_vaux, chan, i, j, buf);
1169 buf += dv_write_pack(dv_video_source, c, buf);
1170 buf += dv_write_pack(dv_video_control, c, buf);
1172 buf += dv_write_pack(dv_video_source, c, buf);
1173 buf += dv_write_pack(dv_video_control, c, buf);
1174 buf += 4*5 + 2; /* unused bytes */
1177 /* DV Audio/Video: 135 Video DIFs + 9 Audio DIFs */
1178 for (j = 0; j < 135; j++) {
1180 memset(buf, 0xff, 80);
1181 buf += dv_write_dif_id(dv_sect_audio, chan, i, j/15, buf);
1182 buf += 77; /* audio control & shuffled PCM audio */
1184 buf += dv_write_dif_id(dv_sect_video, chan, i, j, buf);
1185 buf += 77; /* 1 video macro block: 1 bytes control
1186 4 * 14 bytes Y 8x8 data
1187 10 bytes Cr 8x8 data
1188 10 bytes Cb 8x8 data */
1196 static int dvvideo_encode_frame(AVCodecContext *c, uint8_t *buf, int buf_size,
1199 DVVideoContext *s = c->priv_data;
1201 s->sys = dv_codec_profile(c);
1204 if(buf_size < s->sys->frame_size)
1207 c->pix_fmt = s->sys->pix_fmt;
1208 s->picture = *((AVFrame *)data);
1209 s->picture.key_frame = 1;
1210 s->picture.pict_type = FF_I_TYPE;
1213 c->execute(c, dv_encode_mt, (void**)&dv_anchor[0], NULL,
1214 s->sys->n_difchan * s->sys->difseg_size * 27);
1218 dv_format_frame(s, buf);
1220 return s->sys->frame_size;
1223 static int dvvideo_close(AVCodecContext *c)
1230 #ifdef CONFIG_DVVIDEO_ENCODER
1231 AVCodec dvvideo_encoder = {
1235 sizeof(DVVideoContext),
1237 dvvideo_encode_frame,
1243 #endif // CONFIG_DVVIDEO_ENCODER
1245 AVCodec dvvideo_decoder = {
1249 sizeof(DVVideoContext),
1253 dvvideo_decode_frame,