3 * Copyright (c) 2002 Fabrice Bellard.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include "mpegvideo.h"
27 #include "simple_idct.h"
29 #define NTSC_FRAME_SIZE 120000
30 #define PAL_FRAME_SIZE 144000
32 #define TEX_VLC_BITS 9
34 typedef struct DVVideoDecodeContext {
35 AVCodecContext *avctx;
38 int sampling_411; /* 0 = 420, 1 = 411 */
40 uint8_t *current_picture[3]; /* picture structure */
43 DCTELEM block[5*6][64] __align8;
44 uint8_t dv_zigzag[2][64];
45 uint8_t idct_permutation[64];
46 /* XXX: move it to static storage ? */
47 uint8_t dv_shift[2][22][64];
48 void (*idct_put[2])(uint8_t *dest, int line_size, DCTELEM *block);
49 } DVVideoDecodeContext;
54 /* XXX: also include quantization */
55 static RL_VLC_ELEM *dv_rl_vlc[1];
57 static void dv_build_unquantize_tables(DVVideoDecodeContext *s)
61 /* NOTE: max left shift is 6 */
62 for(q = 0; q < 22; q++) {
64 for(i = 1; i < 64; i++) {
66 j = s->idct_permutation[i];
67 s->dv_shift[0][q][j] =
68 dv_quant_shifts[q][dv_88_areas[i]] + 1;
72 for(i = 1; i < 64; i++) {
74 s->dv_shift[1][q][i] =
75 dv_quant_shifts[q][dv_248_areas[i]] + 1;
80 static int dvvideo_decode_init(AVCodecContext *avctx)
82 DVVideoDecodeContext *s = avctx->priv_data;
91 /* NOTE: as a trick, we use the fact the no codes are unused
92 to accelerate the parsing of partial codes */
93 init_vlc(&dv_vlc, TEX_VLC_BITS, NB_DV_VLC,
94 dv_vlc_len, 1, 1, dv_vlc_bits, 2, 2);
96 dv_rl_vlc[0] = av_malloc(dv_vlc.table_size * sizeof(RL_VLC_ELEM));
97 for(i = 0; i < dv_vlc.table_size; i++){
98 int code= dv_vlc.table[i][0];
99 int len = dv_vlc.table[i][1];
102 if(len<0){ //more bits needed
105 } else if (code == (NB_DV_VLC - 1)) {
110 run= dv_vlc_run[code] + 1;
111 level= dv_vlc_level[code];
113 dv_rl_vlc[0][i].len = len;
114 dv_rl_vlc[0][i].level = level;
115 dv_rl_vlc[0][i].run = run;
119 /* ugly way to get the idct & scantable */
121 memset(&s2, 0, sizeof(MpegEncContext));
123 dsputil_init(&s2.dsp, avctx);
124 if (DCT_common_init(&s2) < 0)
127 s->idct_put[0] = s2.dsp.idct_put;
128 memcpy(s->idct_permutation, s2.dsp.idct_permutation, 64);
129 memcpy(s->dv_zigzag[0], s2.intra_scantable.permutated, 64);
131 /* XXX: use MMX also for idct248 */
132 s->idct_put[1] = simple_idct248_put;
133 memcpy(s->dv_zigzag[1], dv_248_zigzag, 64);
135 /* XXX: do it only for constant case */
136 dv_build_unquantize_tables(s);
143 typedef struct BlockInfo {
144 const uint8_t *shift_table;
145 const uint8_t *scan_table;
146 uint8_t pos; /* position in block */
147 uint8_t eob_reached; /* true if EOB has been reached */
149 uint8_t partial_bit_count;
150 uint16_t partial_bit_buffer;
154 /* block size in bits */
155 static const uint16_t block_sizes[6] = {
156 112, 112, 112, 112, 80, 80
159 #ifndef ALT_BITSTREAM_READER
160 #warning only works with ALT_BITSTREAM_READER
163 /* decode ac coefs */
164 static void dv_decode_ac(DVVideoDecodeContext *s,
165 BlockInfo *mb, DCTELEM *block, int last_index)
168 int shift_offset = mb->shift_offset;
169 const uint8_t *scan_table = mb->scan_table;
170 const uint8_t *shift_table = mb->shift_table;
172 int level, pos1, sign, run;
173 int partial_bit_count;
174 #ifndef ALT_BITSTREAM_READER //FIXME
178 OPEN_READER(re, &s->gb);
184 /* if we must parse a partial vlc, we do it here */
185 partial_bit_count = mb->partial_bit_count;
186 if (partial_bit_count > 0) {
192 /* build the dummy bit buffer */
193 l = 16 - partial_bit_count;
194 UPDATE_CACHE(re, &s->gb);
196 printf("show=%04x\n", SHOW_UBITS(re, &s->gb, 16));
198 v = (mb->partial_bit_buffer << l) | SHOW_UBITS(re, &s->gb, l);
202 printf("v=%04x cnt=%d %04x\n",
203 v, partial_bit_count, (mb->partial_bit_buffer << l));
205 /* try to read the codeword */
206 init_get_bits(&gb1, buf, 4*8);
208 OPEN_READER(re1, &gb1);
209 UPDATE_CACHE(re1, &gb1);
210 GET_RL_VLC(level, run, re1, &gb1, dv_rl_vlc[0],
213 CLOSE_READER(re1, &gb1);
216 printf("****run=%d level=%d size=%d\n", run, level, l);
218 /* compute codeword length */
219 l1 = (level != 256 && level != 0);
220 /* if too long, we cannot parse */
221 l -= partial_bit_count;
222 if ((re_index + l + l1) > last_index)
225 last_re_index = 0; /* avoid warning */
227 /* by definition, if we can read the vlc, all partial bits
228 will be read (otherwise we could have read the vlc before) */
229 mb->partial_bit_count = 0;
230 UPDATE_CACHE(re, &s->gb);
234 /* get the AC coefficients until last_index is reached */
236 UPDATE_CACHE(re, &s->gb);
238 printf("%2d: bits=%04x index=%d\n",
239 pos, SHOW_UBITS(re, &s->gb, 16), re_index);
241 last_re_index = re_index;
242 GET_RL_VLC(level, run, re, &s->gb, dv_rl_vlc[0],
246 printf("run=%d level=%d\n", run, level);
249 if (re_index > last_index) {
251 /* put position before read code */
252 re_index = last_re_index;
259 } else if (level != 0) {
260 if ((re_index + 1) > last_index)
262 sign = SHOW_SBITS(re, &s->gb, 1);
263 level = (level ^ sign) - sign;
264 LAST_SKIP_BITS(re, &s->gb, 1);
270 pos1 = scan_table[pos];
271 level = level << (shift_table[pos1] + shift_offset);
273 // printf("run=%d level=%d shift=%d\n", run, level, shift_table[pos1]);
275 if (re_index > last_index)
277 /* level is zero: means run without coding. No
283 #if defined(VLC_DEBUG) || 1
284 fprintf(stderr, "error pos=%d\n", pos);
286 /* for errors, we consider the eob is reached */
292 CLOSE_READER(re, &s->gb);
296 static inline void bit_copy(PutBitContext *pb, GetBitContext *gb, int bits_left)
298 while (bits_left >= 16) {
299 put_bits(pb, 16, get_bits(gb, 16));
303 put_bits(pb, bits_left, get_bits(gb, bits_left));
307 /* mb_x and mb_y are in units of 8 pixels */
308 static inline void dv_decode_video_segment(DVVideoDecodeContext *s,
310 const uint16_t *mb_pos_ptr)
312 int quant, dc, dct_mode, class1, j;
313 int mb_index, mb_x, mb_y, v, last_index;
314 DCTELEM *block, *block1;
315 int c_offset, bits_left;
317 BlockInfo mb_data[5 * 6], *mb, *mb1;
318 void (*idct_put)(uint8_t *dest, int line_size, DCTELEM *block);
320 PutBitContext pb, vs_pb;
321 uint8_t mb_bit_buffer[80 + 4]; /* allow some slack */
323 uint8_t vs_bit_buffer[5 * 80 + 4]; /* allow some slack */
326 memset(s->block, 0, sizeof(s->block));
328 /* pass 1 : read DC and AC coefficients in blocks */
330 block1 = &s->block[0][0];
332 init_put_bits(&vs_pb, vs_bit_buffer, 5 * 80, NULL, NULL);
334 for(mb_index = 0; mb_index < 5; mb_index++) {
336 quant = buf_ptr[3] & 0x0f;
338 init_put_bits(&pb, mb_bit_buffer, 80, NULL, NULL);
342 for(j = 0;j < 6; j++) {
343 /* NOTE: size is not important here */
344 init_get_bits(&s->gb, buf_ptr, 14*8);
347 dc = get_bits(&s->gb, 9);
348 dc = (dc << (32 - 9)) >> (32 - 9);
349 dct_mode = get_bits1(&s->gb);
350 mb->dct_mode = dct_mode;
351 mb->scan_table = s->dv_zigzag[dct_mode];
352 class1 = get_bits(&s->gb, 2);
353 mb->shift_offset = (class1 == 3);
354 mb->shift_table = s->dv_shift[dct_mode]
355 [quant + dv_quant_offset[class1]];
357 /* convert to unsigned because 128 is not added in the
361 last_index = block_sizes[j];
362 buf_ptr += last_index >> 3;
364 mb->partial_bit_count = 0;
366 dv_decode_ac(s, mb, block, last_index);
368 /* write the remaining bits in a new buffer only if the
370 bits_left = last_index - get_bits_count(&s->gb);
371 if (mb->eob_reached) {
372 mb->partial_bit_count = 0;
373 mb_bit_count += bits_left;
374 bit_copy(&pb, &s->gb, bits_left);
376 /* should be < 16 bits otherwise a codeword could have
378 mb->partial_bit_count = bits_left;
379 mb->partial_bit_buffer = get_bits(&s->gb, bits_left);
387 /* pass 2 : we can do it just after */
389 printf("***pass 2 size=%d\n", mb_bit_count);
393 init_get_bits(&s->gb, mb_bit_buffer, 80*8);
394 for(j = 0;j < 6; j++) {
395 if (!mb->eob_reached && get_bits_count(&s->gb) < mb_bit_count) {
396 dv_decode_ac(s, mb, block, mb_bit_count);
397 /* if still not finished, no need to parse other blocks */
398 if (!mb->eob_reached) {
399 /* we could not parse the current AC coefficient,
400 so we add the remaining bytes */
401 bits_left = mb_bit_count - get_bits_count(&s->gb);
403 mb->partial_bit_count += bits_left;
404 mb->partial_bit_buffer =
405 (mb->partial_bit_buffer << bits_left) |
406 get_bits(&s->gb, bits_left);
414 /* all blocks are finished, so the extra bytes can be used at
415 the video segment level */
416 bits_left = mb_bit_count - get_bits_count(&s->gb);
417 vs_bit_count += bits_left;
418 bit_copy(&vs_pb, &s->gb, bits_left);
424 /* we need a pass other the whole video segment */
425 flush_put_bits(&vs_pb);
428 printf("***pass 3 size=%d\n", vs_bit_count);
430 block = &s->block[0][0];
432 init_get_bits(&s->gb, vs_bit_buffer, 5 * 80*8);
433 for(mb_index = 0; mb_index < 5; mb_index++) {
434 for(j = 0;j < 6; j++) {
435 if (!mb->eob_reached) {
437 printf("start %d:%d\n", mb_index, j);
439 dv_decode_ac(s, mb, block, vs_bit_count);
446 /* compute idct and place blocks */
447 block = &s->block[0][0];
449 for(mb_index = 0; mb_index < 5; mb_index++) {
453 y_ptr = s->current_picture[0] + (mb_y * s->linesize[0] * 8) + (mb_x * 8);
455 c_offset = (mb_y * s->linesize[1] * 8) + ((mb_x >> 2) * 8);
457 c_offset = ((mb_y >> 1) * s->linesize[1] * 8) + ((mb_x >> 1) * 8);
458 for(j = 0;j < 6; j++) {
459 idct_put = s->idct_put[mb->dct_mode];
461 if (s->sampling_411 && mb_x < (704 / 8)) {
462 /* NOTE: at end of line, the macroblock is handled as 420 */
463 idct_put(y_ptr + (j * 8), s->linesize[0], block);
465 idct_put(y_ptr + ((j & 1) * 8) + ((j >> 1) * 8 * s->linesize[0]),
466 s->linesize[0], block);
469 if (s->sampling_411 && mb_x >= (704 / 8)) {
470 uint8_t pixels[64], *c_ptr, *c_ptr1, *ptr;
472 /* NOTE: at end of line, the macroblock is handled as 420 */
473 idct_put(pixels, 8, block);
474 linesize = s->linesize[6 - j];
475 c_ptr = s->current_picture[6 - j] + c_offset;
477 for(y = 0;y < 8; y++) {
478 /* convert to 411P */
479 c_ptr1 = c_ptr + 8*linesize;
480 c_ptr[0]= ptr[0]; c_ptr1[0]= ptr[4];
481 c_ptr[1]= ptr[1]; c_ptr1[1]= ptr[5];
482 c_ptr[2]= ptr[2]; c_ptr1[2]= ptr[6];
483 c_ptr[3]= ptr[3]; c_ptr1[3]= ptr[7];
488 /* don't ask me why they inverted Cb and Cr ! */
489 idct_put(s->current_picture[6 - j] + c_offset,
490 s->linesize[6 - j], block);
500 /* NOTE: exactly one frame must be given (120000 bytes for NTSC,
501 144000 bytes for PAL) */
502 static int dvvideo_decode_frame(AVCodecContext *avctx,
503 void *data, int *data_size,
504 uint8_t *buf, int buf_size)
506 DVVideoDecodeContext *s = avctx->priv_data;
507 int sct, dsf, apt, ds, nb_dif_segs, vs, width, height, i, packet_size;
509 const uint16_t *mb_pos_ptr;
512 init_get_bits(&s->gb, buf, buf_size*8);
513 sct = get_bits(&s->gb, 3);
516 skip_bits(&s->gb, 5);
517 get_bits(&s->gb, 4); /* dsn (sequence number */
518 get_bits(&s->gb, 1); /* fsc (channel number) */
519 skip_bits(&s->gb, 3);
520 get_bits(&s->gb, 8); /* dbn (diff block number 0-134) */
522 dsf = get_bits(&s->gb, 1); /* 0 = NTSC 1 = PAL */
523 if (get_bits(&s->gb, 1) != 0)
525 skip_bits(&s->gb, 11);
526 apt = get_bits(&s->gb, 3); /* apt */
528 get_bits(&s->gb, 1); /* tf1 */
529 skip_bits(&s->gb, 4);
530 get_bits(&s->gb, 3); /* ap1 */
532 get_bits(&s->gb, 1); /* tf2 */
533 skip_bits(&s->gb, 4);
534 get_bits(&s->gb, 3); /* ap2 */
536 get_bits(&s->gb, 1); /* tf3 */
537 skip_bits(&s->gb, 4);
538 get_bits(&s->gb, 3); /* ap3 */
543 avctx->frame_rate = 25;
544 avctx->frame_rate_base = 1;
545 packet_size = PAL_FRAME_SIZE;
549 avctx->frame_rate = 30000;
550 avctx->frame_rate_base = 1001;
551 packet_size = NTSC_FRAME_SIZE;
555 /* NOTE: we only accept several full frames */
556 if (buf_size < packet_size)
559 /* NTSC[dsf == 0] is always 720x480, 4:1:1
560 * PAL[dsf == 1] is always 720x576, 4:2:0 for IEC 68134[apt == 0]
561 * but for the SMPTE 314M[apt == 1] it is 720x576, 4:1:1
563 s->sampling_411 = !dsf || apt;
564 if (s->sampling_411) {
565 mb_pos_ptr = dsf ? dv_place_411P : dv_place_411;
566 avctx->pix_fmt = PIX_FMT_YUV411P;
568 mb_pos_ptr = dv_place_420;
569 avctx->pix_fmt = PIX_FMT_YUV420P;
572 avctx->width = width;
573 avctx->height = height;
575 /* Once again, this is pretty complicated by the fact that the same
576 * field is used differently by IEC 68134[apt == 0] and
577 * SMPTE 314M[apt == 1].
579 if (buf[VAUX_TC61_OFFSET] == 0x61 &&
580 ((apt == 0 && (buf[VAUX_TC61_OFFSET + 2] & 0x07) == 0x07) ||
581 (apt == 1 && (buf[VAUX_TC61_OFFSET + 2] & 0x07) == 0x02)))
582 avctx->aspect_ratio = 16.0 / 9.0;
584 avctx->aspect_ratio = 4.0 / 3.0;
586 if(s->picture.data[0])
587 avctx->release_buffer(avctx, &s->picture);
589 s->picture.reference= 0;
590 if(avctx->get_buffer(avctx, &s->picture) < 0) {
591 fprintf(stderr, "get_buffer() failed\n");
596 s->current_picture[i] = s->picture.data[i];
597 s->linesize[i] = s->picture.linesize[i];
598 if (!s->current_picture[i])
604 /* for each DIF segment */
606 for (ds = 0; ds < nb_dif_segs; ds++) {
607 buf_ptr += 6 * 80; /* skip DIF segment header */
609 for(vs = 0; vs < 27; vs++) {
611 /* skip audio block */
614 dv_decode_video_segment(s, buf_ptr, mb_pos_ptr);
623 *data_size = sizeof(AVFrame);
624 *(AVFrame*)data= s->picture;
629 static int dvvideo_decode_end(AVCodecContext *avctx)
631 DVVideoDecodeContext *s = avctx->priv_data;
633 avcodec_default_free_buffers(avctx);
638 AVCodec dvvideo_decoder = {
642 sizeof(DVVideoDecodeContext),
646 dvvideo_decode_frame,
651 typedef struct DVAudioDecodeContext {
652 AVCodecContext *avctx;
654 } DVAudioDecodeContext;
656 static int dvaudio_decode_init(AVCodecContext *avctx)
658 // DVAudioDecodeContext *s = avctx->priv_data;
662 static uint16_t dv_audio_12to16(uint16_t sample)
664 uint16_t shift, result;
666 sample = (sample < 0x800) ? sample : sample | 0xf000;
667 shift = (sample & 0xf00) >> 8;
669 if (shift < 0x2 || shift > 0xd) {
671 } else if (shift < 0x8) {
673 result = (sample - (256 * shift)) << shift;
676 result = ((sample + ((256 * shift) + 1)) << shift) - 1;
682 /* NOTE: exactly one frame must be given (120000 bytes for NTSC,
683 144000 bytes for PAL)
685 There's a couple of assumptions being made here:
686 1. By default we silence erroneous (0x8000/16bit 0x800/12bit)
687 audio samples. We can pass them upwards when ffmpeg will be ready
689 2. We don't do software emphasis.
690 3. Audio is always returned as 16bit linear samples: 12bit
691 nonlinear samples are converted into 16bit linear ones.
693 static int dvaudio_decode_frame(AVCodecContext *avctx,
694 void *data, int *data_size,
695 uint8_t *buf, int buf_size)
697 DVVideoDecodeContext *s = avctx->priv_data;
698 const uint16_t (*unshuffle)[9];
699 int smpls, freq, quant, sys, stride, difseg, ad, dp, nb_dif_segs, i;
704 init_get_bits(&s->gb, &buf[AAUX_AS_OFFSET], 5*8);
705 i = get_bits(&s->gb, 8);
706 if (i != 0x50) { /* No audio ? */
711 get_bits(&s->gb, 1); /* 0 - locked audio, 1 - unlocked audio */
712 skip_bits(&s->gb, 1);
713 smpls = get_bits(&s->gb, 6); /* samples in this frame - min. samples */
715 skip_bits(&s->gb, 8);
717 skip_bits(&s->gb, 2);
718 sys = get_bits(&s->gb, 1); /* 0 - 60 fields, 1 = 50 fields */
719 skip_bits(&s->gb, 5);
721 get_bits(&s->gb, 1); /* 0 - emphasis on, 1 - emphasis off */
722 get_bits(&s->gb, 1); /* 0 - reserved, 1 - emphasis time constant 50/15us */
723 freq = get_bits(&s->gb, 3); /* 0 - 48KHz, 1 - 44,1kHz, 2 - 32 kHz */
724 quant = get_bits(&s->gb, 3); /* 0 - 16bit linear, 1 - 12bit nonlinear */
727 return -1; /* Unsupported quantization */
729 avctx->sample_rate = dv_audio_frequency[freq];
731 avctx->bit_rate = avctx->channels * avctx->sample_rate * 16;
733 // avctx->frame_size =
735 *data_size = (dv_audio_min_samples[sys][freq] + smpls) *
741 unshuffle = dv_place_audio50;
745 unshuffle = dv_place_audio60;
748 /* for each DIF segment */
750 for (difseg = 0; difseg < nb_dif_segs; difseg++) {
751 buf_ptr += 6 * 80; /* skip DIF segment header */
752 for (ad = 0; ad < 9; ad++) {
754 for (dp = 8; dp < 80; dp+=2) {
755 if (quant == 0) { /* 16bit quantization */
756 i = unshuffle[difseg][ad] + (dp - 8)/2 * stride;
757 ((short *)data)[i] = (buf_ptr[dp] << 8) | buf_ptr[dp+1];
758 if (((unsigned short *)data)[i] == 0x8000)
759 ((short *)data)[i] = 0;
760 } else { /* 12bit quantization */
761 if (difseg >= nb_dif_segs/2)
762 goto out; /* We're not doing 4ch at this time */
764 lc = ((uint16_t)buf_ptr[dp] << 4) |
765 ((uint16_t)buf_ptr[dp+2] >> 4);
766 rc = ((uint16_t)buf_ptr[dp+1] << 4) |
767 ((uint16_t)buf_ptr[dp+2] & 0x0f);
768 lc = (lc == 0x800 ? 0 : dv_audio_12to16(lc));
769 rc = (rc == 0x800 ? 0 : dv_audio_12to16(rc));
771 i = unshuffle[difseg][ad] + (dp - 8)/3 * stride;
772 ((short *)data)[i] = lc;
773 i = unshuffle[difseg+nb_dif_segs/2][ad] + (dp - 8)/3 * stride;
774 ((short *)data)[i] = rc;
779 buf_ptr += 16 * 80; /* 15 Video DIFs + 1 Audio DIF */
787 static int dvaudio_decode_end(AVCodecContext *avctx)
789 // DVAudioDecodeContext *s = avctx->priv_data;
793 AVCodec dvaudio_decoder = {
797 sizeof(DVAudioDecodeContext),
801 dvaudio_decode_frame,