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
21 #include "mpegvideo.h"
22 #include "simple_idct.h"
24 #define NTSC_FRAME_SIZE 120000
25 #define PAL_FRAME_SIZE 144000
27 #define TEX_VLC_BITS 9
29 typedef struct DVVideoDecodeContext {
30 AVCodecContext *avctx;
33 int sampling_411; /* 0 = 420, 1 = 411 */
35 uint8_t *current_picture[3]; /* picture structure */
38 DCTELEM block[5*6][64] __align8;
39 uint8_t dv_zigzag[2][64];
40 uint8_t idct_permutation[64];
41 /* XXX: move it to static storage ? */
42 uint8_t dv_shift[2][22][64];
43 void (*idct_put[2])(uint8_t *dest, int line_size, DCTELEM *block);
44 } DVVideoDecodeContext;
49 /* XXX: also include quantization */
50 static RL_VLC_ELEM *dv_rl_vlc[1];
52 static void dv_build_unquantize_tables(DVVideoDecodeContext *s)
56 /* NOTE: max left shift is 6 */
57 for(q = 0; q < 22; q++) {
59 for(i = 1; i < 64; i++) {
61 j = s->idct_permutation[i];
62 s->dv_shift[0][q][j] =
63 dv_quant_shifts[q][dv_88_areas[i]] + 1;
67 for(i = 1; i < 64; i++) {
69 s->dv_shift[1][q][i] =
70 dv_quant_shifts[q][dv_248_areas[i]] + 1;
75 static int dvvideo_decode_init(AVCodecContext *avctx)
77 DVVideoDecodeContext *s = avctx->priv_data;
86 /* NOTE: as a trick, we use the fact the no codes are unused
87 to accelerate the parsing of partial codes */
88 init_vlc(&dv_vlc, TEX_VLC_BITS, NB_DV_VLC,
89 dv_vlc_len, 1, 1, dv_vlc_bits, 2, 2);
91 dv_rl_vlc[0] = av_malloc(dv_vlc.table_size * sizeof(RL_VLC_ELEM));
92 for(i = 0; i < dv_vlc.table_size; i++){
93 int code= dv_vlc.table[i][0];
94 int len = dv_vlc.table[i][1];
97 if(len<0){ //more bits needed
100 } else if (code == (NB_DV_VLC - 1)) {
105 run= dv_vlc_run[code] + 1;
106 level= dv_vlc_level[code];
108 dv_rl_vlc[0][i].len = len;
109 dv_rl_vlc[0][i].level = level;
110 dv_rl_vlc[0][i].run = run;
114 /* ugly way to get the idct & scantable */
116 memset(&s2, 0, sizeof(MpegEncContext));
118 dsputil_init(&s2.dsp, avctx->dsp_mask);
119 if (DCT_common_init(&s2) < 0)
122 s->idct_put[0] = s2.idct_put;
123 memcpy(s->idct_permutation, s2.idct_permutation, 64);
124 memcpy(s->dv_zigzag[0], s2.intra_scantable.permutated, 64);
126 /* XXX: use MMX also for idct248 */
127 s->idct_put[1] = simple_idct248_put;
128 memcpy(s->dv_zigzag[1], dv_248_zigzag, 64);
130 /* XXX: do it only for constant case */
131 dv_build_unquantize_tables(s);
138 typedef struct BlockInfo {
139 const uint8_t *shift_table;
140 const uint8_t *scan_table;
141 uint8_t pos; /* position in block */
142 uint8_t eob_reached; /* true if EOB has been reached */
144 uint8_t partial_bit_count;
145 uint16_t partial_bit_buffer;
149 /* block size in bits */
150 static const uint16_t block_sizes[6] = {
151 112, 112, 112, 112, 80, 80
154 #ifndef ALT_BITSTREAM_READER
155 #error only works with ALT_BITSTREAM_READER
158 /* decode ac coefs */
159 static void dv_decode_ac(DVVideoDecodeContext *s,
160 BlockInfo *mb, DCTELEM *block, int last_index)
163 int shift_offset = mb->shift_offset;
164 const uint8_t *scan_table = mb->scan_table;
165 const uint8_t *shift_table = mb->shift_table;
167 int level, pos1, sign, run;
168 int partial_bit_count;
170 OPEN_READER(re, &s->gb);
176 /* if we must parse a partial vlc, we do it here */
177 partial_bit_count = mb->partial_bit_count;
178 if (partial_bit_count > 0) {
184 /* build the dummy bit buffer */
185 l = 16 - partial_bit_count;
186 UPDATE_CACHE(re, &s->gb);
188 printf("show=%04x\n", SHOW_UBITS(re, &s->gb, 16));
190 v = (mb->partial_bit_buffer << l) | SHOW_UBITS(re, &s->gb, l);
194 printf("v=%04x cnt=%d %04x\n",
195 v, partial_bit_count, (mb->partial_bit_buffer << l));
197 /* try to read the codeword */
198 init_get_bits(&gb1, buf, 4*8);
200 OPEN_READER(re1, &gb1);
201 UPDATE_CACHE(re1, &gb1);
202 GET_RL_VLC(level, run, re1, &gb1, dv_rl_vlc[0],
205 CLOSE_READER(re1, &gb1);
208 printf("****run=%d level=%d size=%d\n", run, level, l);
210 /* compute codeword length */
211 l1 = (level != 256 && level != 0);
212 /* if too long, we cannot parse */
213 l -= partial_bit_count;
214 if ((re_index + l + l1) > last_index)
217 last_re_index = 0; /* avoid warning */
219 /* by definition, if we can read the vlc, all partial bits
220 will be read (otherwise we could have read the vlc before) */
221 mb->partial_bit_count = 0;
222 UPDATE_CACHE(re, &s->gb);
226 /* get the AC coefficients until last_index is reached */
228 UPDATE_CACHE(re, &s->gb);
230 printf("%2d: bits=%04x index=%d\n",
231 pos, SHOW_UBITS(re, &s->gb, 16), re_index);
233 last_re_index = re_index;
234 GET_RL_VLC(level, run, re, &s->gb, dv_rl_vlc[0],
238 printf("run=%d level=%d\n", run, level);
241 if (re_index > last_index) {
243 /* put position before read code */
244 re_index = last_re_index;
251 } else if (level != 0) {
252 if ((re_index + 1) > last_index)
254 sign = SHOW_SBITS(re, &s->gb, 1);
255 level = (level ^ sign) - sign;
256 LAST_SKIP_BITS(re, &s->gb, 1);
262 pos1 = scan_table[pos];
263 level = level << (shift_table[pos1] + shift_offset);
265 // printf("run=%d level=%d shift=%d\n", run, level, shift_table[pos1]);
267 if (re_index > last_index)
269 /* level is zero: means run without coding. No
275 #if defined(VLC_DEBUG) || 1
276 printf("error pos=%d\n", pos);
278 /* for errors, we consider the eob is reached */
284 CLOSE_READER(re, &s->gb);
288 static inline void bit_copy(PutBitContext *pb, GetBitContext *gb, int bits_left)
290 while (bits_left >= 16) {
291 put_bits(pb, 16, get_bits(gb, 16));
295 put_bits(pb, bits_left, get_bits(gb, bits_left));
299 /* mb_x and mb_y are in units of 8 pixels */
300 static inline void dv_decode_video_segment(DVVideoDecodeContext *s,
302 const uint16_t *mb_pos_ptr)
304 int quant, dc, dct_mode, class1, j;
305 int mb_index, mb_x, mb_y, v, last_index;
306 DCTELEM *block, *block1;
307 int c_offset, bits_left;
309 BlockInfo mb_data[5 * 6], *mb, *mb1;
310 void (*idct_put)(uint8_t *dest, int line_size, DCTELEM *block);
312 PutBitContext pb, vs_pb;
313 uint8_t mb_bit_buffer[80 + 4]; /* allow some slack */
315 uint8_t vs_bit_buffer[5 * 80 + 4]; /* allow some slack */
318 memset(s->block, 0, sizeof(s->block));
320 /* pass 1 : read DC and AC coefficients in blocks */
322 block1 = &s->block[0][0];
324 init_put_bits(&vs_pb, vs_bit_buffer, 5 * 80, NULL, NULL);
326 for(mb_index = 0; mb_index < 5; mb_index++) {
328 quant = buf_ptr[3] & 0x0f;
330 init_put_bits(&pb, mb_bit_buffer, 80, NULL, NULL);
334 for(j = 0;j < 6; j++) {
335 /* NOTE: size is not important here */
336 init_get_bits(&s->gb, buf_ptr, 14*8);
339 dc = get_bits(&s->gb, 9);
340 dc = (dc << (32 - 9)) >> (32 - 9);
341 dct_mode = get_bits1(&s->gb);
342 mb->dct_mode = dct_mode;
343 mb->scan_table = s->dv_zigzag[dct_mode];
344 class1 = get_bits(&s->gb, 2);
345 mb->shift_offset = (class1 == 3);
346 mb->shift_table = s->dv_shift[dct_mode]
347 [quant + dv_quant_offset[class1]];
349 /* convert to unsigned because 128 is not added in the
353 last_index = block_sizes[j];
354 buf_ptr += last_index >> 3;
356 mb->partial_bit_count = 0;
358 dv_decode_ac(s, mb, block, last_index);
360 /* write the remaining bits in a new buffer only if the
362 bits_left = last_index - s->gb.index;
363 if (mb->eob_reached) {
364 mb->partial_bit_count = 0;
365 mb_bit_count += bits_left;
366 bit_copy(&pb, &s->gb, bits_left);
368 /* should be < 16 bits otherwise a codeword could have
370 mb->partial_bit_count = bits_left;
371 mb->partial_bit_buffer = get_bits(&s->gb, bits_left);
379 /* pass 2 : we can do it just after */
381 printf("***pass 2 size=%d\n", mb_bit_count);
385 init_get_bits(&s->gb, mb_bit_buffer, 80*8);
386 for(j = 0;j < 6; j++) {
387 if (!mb->eob_reached && s->gb.index < mb_bit_count) {
388 dv_decode_ac(s, mb, block, mb_bit_count);
389 /* if still not finished, no need to parse other blocks */
390 if (!mb->eob_reached) {
391 /* we could not parse the current AC coefficient,
392 so we add the remaining bytes */
393 bits_left = mb_bit_count - s->gb.index;
395 mb->partial_bit_count += bits_left;
396 mb->partial_bit_buffer =
397 (mb->partial_bit_buffer << bits_left) |
398 get_bits(&s->gb, bits_left);
406 /* all blocks are finished, so the extra bytes can be used at
407 the video segment level */
408 bits_left = mb_bit_count - s->gb.index;
409 vs_bit_count += bits_left;
410 bit_copy(&vs_pb, &s->gb, bits_left);
416 /* we need a pass other the whole video segment */
417 flush_put_bits(&vs_pb);
420 printf("***pass 3 size=%d\n", vs_bit_count);
422 block = &s->block[0][0];
424 init_get_bits(&s->gb, vs_bit_buffer, 5 * 80*8);
425 for(mb_index = 0; mb_index < 5; mb_index++) {
426 for(j = 0;j < 6; j++) {
427 if (!mb->eob_reached) {
429 printf("start %d:%d\n", mb_index, j);
431 dv_decode_ac(s, mb, block, vs_bit_count);
438 /* compute idct and place blocks */
439 block = &s->block[0][0];
441 for(mb_index = 0; mb_index < 5; mb_index++) {
445 y_ptr = s->current_picture[0] + (mb_y * s->linesize[0] * 8) + (mb_x * 8);
447 c_offset = (mb_y * s->linesize[1] * 8) + ((mb_x >> 2) * 8);
449 c_offset = ((mb_y >> 1) * s->linesize[1] * 8) + ((mb_x >> 1) * 8);
450 for(j = 0;j < 6; j++) {
451 idct_put = s->idct_put[mb->dct_mode];
453 if (s->sampling_411 && mb_x < (704 / 8)) {
454 /* NOTE: at end of line, the macroblock is handled as 420 */
455 idct_put(y_ptr + (j * 8), s->linesize[0], block);
457 idct_put(y_ptr + ((j & 1) * 8) + ((j >> 1) * 8 * s->linesize[0]),
458 s->linesize[0], block);
461 if (s->sampling_411 && mb_x >= (704 / 8)) {
462 uint8_t pixels[64], *c_ptr, *c_ptr1, *ptr;
464 /* NOTE: at end of line, the macroblock is handled as 420 */
465 idct_put(pixels, 8, block);
466 linesize = s->linesize[6 - j];
467 c_ptr = s->current_picture[6 - j] + c_offset;
469 for(y = 0;y < 8; y++) {
470 /* convert to 411P */
471 c_ptr1 = c_ptr + linesize;
472 c_ptr1[0] = c_ptr[0] = (ptr[0] + ptr[1]) >> 1;
473 c_ptr1[1] = c_ptr[1] = (ptr[2] + ptr[3]) >> 1;
474 c_ptr1[2] = c_ptr[2] = (ptr[4] + ptr[5]) >> 1;
475 c_ptr1[3] = c_ptr[3] = (ptr[6] + ptr[7]) >> 1;
476 c_ptr += linesize * 2;
480 /* don't ask me why they inverted Cb and Cr ! */
481 idct_put(s->current_picture[6 - j] + c_offset,
482 s->linesize[6 - j], block);
492 /* NOTE: exactly one frame must be given (120000 bytes for NTSC,
493 144000 bytes for PAL) */
494 static int dvvideo_decode_frame(AVCodecContext *avctx,
495 void *data, int *data_size,
496 uint8_t *buf, int buf_size)
498 DVVideoDecodeContext *s = avctx->priv_data;
499 int sct, dsf, apt, ds, nb_dif_segs, vs, width, height, i, packet_size;
501 const uint16_t *mb_pos_ptr;
504 init_get_bits(&s->gb, buf, buf_size*8);
505 sct = get_bits(&s->gb, 3);
508 skip_bits(&s->gb, 5);
509 get_bits(&s->gb, 4); /* dsn (sequence number */
510 get_bits(&s->gb, 1); /* fsc (channel number) */
511 skip_bits(&s->gb, 3);
512 get_bits(&s->gb, 8); /* dbn (diff block number 0-134) */
514 dsf = get_bits(&s->gb, 1); /* 0 = NTSC 1 = PAL */
515 if (get_bits(&s->gb, 1) != 0)
517 skip_bits(&s->gb, 11);
518 apt = get_bits(&s->gb, 3); /* apt */
520 get_bits(&s->gb, 1); /* tf1 */
521 skip_bits(&s->gb, 4);
522 get_bits(&s->gb, 3); /* ap1 */
524 get_bits(&s->gb, 1); /* tf2 */
525 skip_bits(&s->gb, 4);
526 get_bits(&s->gb, 3); /* ap2 */
528 get_bits(&s->gb, 1); /* tf3 */
529 skip_bits(&s->gb, 4);
530 get_bits(&s->gb, 3); /* ap3 */
535 avctx->frame_rate = 25 * FRAME_RATE_BASE;
536 packet_size = PAL_FRAME_SIZE;
540 avctx->frame_rate = 30 * FRAME_RATE_BASE;
541 packet_size = NTSC_FRAME_SIZE;
545 /* NOTE: we only accept several full frames */
546 if (buf_size < packet_size)
549 /* XXX: is it correct to assume that 420 is always used in PAL
551 s->sampling_411 = !dsf;
552 if (s->sampling_411) {
553 mb_pos_ptr = dv_place_411;
554 avctx->pix_fmt = PIX_FMT_YUV411P;
556 mb_pos_ptr = dv_place_420;
557 avctx->pix_fmt = PIX_FMT_YUV420P;
560 avctx->width = width;
561 avctx->height = height;
563 s->picture.reference= 0;
564 if(avctx->get_buffer(avctx, &s->picture) < 0) {
565 fprintf(stderr, "get_buffer() failed\n");
570 s->current_picture[i] = s->picture.data[i];
571 s->linesize[i] = s->picture.linesize[i];
572 if (!s->current_picture[i])
578 /* for each DIF segment */
580 for (ds = 0; ds < nb_dif_segs; ds++) {
581 buf_ptr += 6 * 80; /* skip DIF segment header */
583 for(vs = 0; vs < 27; vs++) {
585 /* skip audio block */
588 dv_decode_video_segment(s, buf_ptr, mb_pos_ptr);
597 *data_size = sizeof(AVFrame);
598 *(AVFrame*)data= s->picture;
600 avctx->release_buffer(avctx, &s->picture);
605 static int dvvideo_decode_end(AVCodecContext *avctx)
607 DVVideoDecodeContext *s = avctx->priv_data;
610 if(avctx->get_buffer == avcodec_default_get_buffer){
612 av_freep(&s->picture.base[i]);
613 s->picture.data[i]= NULL;
615 av_freep(&s->picture.opaque);
621 AVCodec dvvideo_decoder = {
625 sizeof(DVVideoDecodeContext),
629 dvvideo_decode_frame,
634 typedef struct DVAudioDecodeContext {
635 AVCodecContext *avctx;
637 } DVAudioDecodeContext;
639 static int dvaudio_decode_init(AVCodecContext *avctx)
641 // DVAudioDecodeContext *s = avctx->priv_data;
645 static uint16_t dv_audio_12to16(uint16_t sample)
647 uint16_t shift, result;
649 sample = (sample < 0x800) ? sample : sample | 0xf000;
650 shift = (sample & 0xf00) >> 8;
652 if (shift < 0x2 || shift > 0xd) {
654 } else if (shift < 0x8) {
656 result = (sample - (256 * shift)) << shift;
659 result = ((sample + ((256 * shift) + 1)) << shift) - 1;
665 /* NOTE: exactly one frame must be given (120000 bytes for NTSC,
666 144000 bytes for PAL)
668 There's a couple of assumptions being made here:
669 1. We don't do any kind of audio error correction. It means,
670 that erroneous samples 0x8000 are being passed upwards.
671 Do we need to silence erroneous samples ? Average them ?
672 2. We don't do software emphasis.
673 3. We are not checking for 'speed' argument being valid.
674 4. Audio is always returned as 16bit linear samples: 12bit
675 nonlinear samples are converted into 16bit linear ones.
677 static int dvaudio_decode_frame(AVCodecContext *avctx,
678 void *data, int *data_size,
679 uint8_t *buf, int buf_size)
681 DVVideoDecodeContext *s = avctx->priv_data;
682 const uint16_t (*unshuffle)[9];
683 int smpls, freq, quant, sys, stride, difseg, ad, dp, nb_dif_segs, i;
688 init_get_bits(&s->gb, &buf[AAUX_OFFSET], 5*8);
689 i = get_bits(&s->gb, 8);
690 if (i != 0x50) { /* No audio ? */
695 get_bits(&s->gb, 1); /* 0 - locked audio, 1 - unlocked audio */
696 skip_bits(&s->gb, 1);
697 smpls = get_bits(&s->gb, 6); /* samples in this frame - min. samples */
699 skip_bits(&s->gb, 8);
701 skip_bits(&s->gb, 2);
702 sys = get_bits(&s->gb, 1); /* 0 - 60 fields, 1 = 50 fields */
703 skip_bits(&s->gb, 5);
705 get_bits(&s->gb, 1); /* 0 - emphasis on, 1 - emphasis off */
706 get_bits(&s->gb, 1); /* 0 - reserved, 1 - emphasis time constant 50/15us */
707 freq = get_bits(&s->gb, 3); /* 0 - 48KHz, 1 - 44,1kHz, 2 - 32 kHz */
708 quant = get_bits(&s->gb, 3); /* 0 - 16bit linear, 1 - 12bit nonlinear */
711 return -1; /* Unsupported quantization */
713 avctx->sample_rate = dv_audio_frequency[freq];
716 // avctx->frame_size =
718 *data_size = (dv_audio_min_samples[sys][freq] + smpls) *
724 unshuffle = dv_place_audio50;
728 unshuffle = dv_place_audio60;
731 /* for each DIF segment */
733 for (difseg = 0; difseg < nb_dif_segs; difseg++) {
734 buf_ptr += 6 * 80; /* skip DIF segment header */
735 for (ad = 0; ad < 9; ad++) {
737 for (dp = 8; dp < 80; dp+=2) {
738 if (quant == 0) { /* 16bit quantization */
739 i = unshuffle[difseg][ad] + (dp - 8)/2 * stride;
740 ((short *)data)[i] = (buf_ptr[dp] << 8) | buf_ptr[dp+1];
741 } else { /* 12bit quantization */
742 if (difseg >= nb_dif_segs/2)
743 goto out; /* We're not doing 4ch at this time */
745 lc = ((uint16_t)buf_ptr[dp] << 4) |
746 ((uint16_t)buf_ptr[dp+2] >> 4);
747 rc = ((uint16_t)buf_ptr[dp+1] << 4) |
748 ((uint16_t)buf_ptr[dp+2] & 0x0f);
749 lc = dv_audio_12to16(lc);
750 rc = dv_audio_12to16(rc);
752 i = unshuffle[difseg][ad] + (dp - 8)/3 * stride;
753 ((short *)data)[i] = lc;
754 i = unshuffle[difseg+nb_dif_segs/2][ad] + (dp - 8)/3 * stride;
755 ((short *)data)[i] = rc;
760 buf_ptr += 16 * 80; /* 15 Video DIFs + 1 Audio DIF */
768 static int dvaudio_decode_end(AVCodecContext *avctx)
770 // DVAudioDecodeContext *s = avctx->priv_data;
774 AVCodec dvaudio_decoder = {
778 sizeof(DVAudioDecodeContext),
782 dvaudio_decode_frame,