Revision 7458ccbb libavcodec/dv.c

View differences:

libavcodec/dv.c
25 25
#include "dsputil.h"
26 26
#include "mpegvideo.h"
27 27
#include "simple_idct.h"
28

  
29
#define NTSC_FRAME_SIZE 120000
30
#define PAL_FRAME_SIZE  144000
31

  
32
#define TEX_VLC_BITS 9
28
#include "dvdata.h"
33 29

  
34 30
typedef struct DVVideoDecodeContext {
35
    AVCodecContext *avctx;
31
    const DVprofile* sys;
36 32
    GetBitContext gb;
37
    VLC *vlc;
38
    int sampling_411; /* 0 = 420, 1 = 411 */
39
    int width, height;
40
    uint8_t *current_picture[3]; /* picture structure */
41 33
    AVFrame picture;
42
    int linesize[3];
43 34
    DCTELEM block[5*6][64] __align8;
35
    
36
    /* FIXME: the following is extracted from DSP */
44 37
    uint8_t dv_zigzag[2][64];
45 38
    uint8_t idct_permutation[64];
39
    void (*get_pixels)(DCTELEM *block, const uint8_t *pixels, int line_size);
40
    void (*fdct)(DCTELEM *block);
41
    
46 42
    /* XXX: move it to static storage ? */
47 43
    uint8_t dv_shift[2][22][64];
48 44
    void (*idct_put[2])(uint8_t *dest, int line_size, DCTELEM *block);
49 45
} DVVideoDecodeContext;
50 46

  
51
#include "dvdata.h"
52

  
53
static VLC dv_vlc;
47
#define TEX_VLC_BITS 9
54 48
/* XXX: also include quantization */
55 49
static RL_VLC_ELEM *dv_rl_vlc[1];
50
static VLC_TYPE dv_vlc_codes[15][23];
56 51

  
57 52
static void dv_build_unquantize_tables(DVVideoDecodeContext *s)
58 53
{
......
85 80

  
86 81
    if (!done) {
87 82
        int i;
83
        VLC dv_vlc;
88 84

  
89 85
        done = 1;
90 86

  
......
114 110
            dv_rl_vlc[0][i].level = level;
115 111
            dv_rl_vlc[0][i].run = run;
116 112
        }
113

  
114
	memset(dv_vlc_codes, 0xff, sizeof(dv_vlc_codes));
115
	for (i = 0; i < NB_DV_VLC - 1; i++) {
116
	   if (dv_vlc_run[i] < 15 && dv_vlc_level[i] < 23 && dv_vlc_len[i] < 15)
117
	       dv_vlc_codes[dv_vlc_run[i]][dv_vlc_level[i]] = i;
118
	}
117 119
    }
118 120

  
119 121
    /* ugly way to get the idct & scantable */
......
124 126
    if (DCT_common_init(&s2) < 0)
125 127
       return -1;
126 128

  
129
    s->get_pixels = s2.dsp.get_pixels;
130
    s->fdct = s2.dsp.fdct;
131
    
127 132
    s->idct_put[0] = s2.dsp.idct_put;
128 133
    memcpy(s->idct_permutation, s2.dsp.idct_permutation, 64);
129 134
    memcpy(s->dv_zigzag[0], s2.intra_scantable.permutated, 64);
......
134 139

  
135 140
    /* XXX: do it only for constant case */
136 141
    dv_build_unquantize_tables(s);
137
    
142

  
138 143
    return 0;
139 144
}
140 145

  
141
//#define VLC_DEBUG
146
// #define VLC_DEBUG
142 147

  
143 148
typedef struct BlockInfo {
144 149
    const uint8_t *shift_table;
......
450 455
        v = *mb_pos_ptr++;
451 456
        mb_x = v & 0xff;
452 457
        mb_y = v >> 8;
453
        y_ptr = s->current_picture[0] + (mb_y * s->linesize[0] * 8) + (mb_x * 8);
454
        if (s->sampling_411)
455
            c_offset = (mb_y * s->linesize[1] * 8) + ((mb_x >> 2) * 8);
458
        y_ptr = s->picture.data[0] + (mb_y * s->picture.linesize[0] * 8) + (mb_x * 8);
459
        if (s->sys->pix_fmt == PIX_FMT_YUV411P)
460
            c_offset = (mb_y * s->picture.linesize[1] * 8) + ((mb_x >> 2) * 8);
456 461
        else
457
            c_offset = ((mb_y >> 1) * s->linesize[1] * 8) + ((mb_x >> 1) * 8);
462
            c_offset = ((mb_y >> 1) * s->picture.linesize[1] * 8) + ((mb_x >> 1) * 8);
458 463
        for(j = 0;j < 6; j++) {
459 464
            idct_put = s->idct_put[mb->dct_mode];
460 465
            if (j < 4) {
461
                if (s->sampling_411 && mb_x < (704 / 8)) {
466
                if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x < (704 / 8)) {
462 467
                    /* NOTE: at end of line, the macroblock is handled as 420 */
463
                    idct_put(y_ptr + (j * 8), s->linesize[0], block);
468
                    idct_put(y_ptr + (j * 8), s->picture.linesize[0], block);
464 469
                } else {
465
                    idct_put(y_ptr + ((j & 1) * 8) + ((j >> 1) * 8 * s->linesize[0]),
466
                             s->linesize[0], block);
470
                    idct_put(y_ptr + ((j & 1) * 8) + ((j >> 1) * 8 * s->picture.linesize[0]),
471
                             s->picture.linesize[0], block);
467 472
                }
468 473
            } else {
469
                if (s->sampling_411 && mb_x >= (704 / 8)) {
474
                if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8)) {
470 475
                    uint8_t pixels[64], *c_ptr, *c_ptr1, *ptr;
471 476
                    int y, linesize;
472 477
                    /* NOTE: at end of line, the macroblock is handled as 420 */
473 478
                    idct_put(pixels, 8, block);
474
                    linesize = s->linesize[6 - j];
475
                    c_ptr = s->current_picture[6 - j] + c_offset;
479
                    linesize = s->picture.linesize[6 - j];
480
                    c_ptr = s->picture.data[6 - j] + c_offset;
476 481
                    ptr = pixels;
477 482
                    for(y = 0;y < 8; y++) {
478 483
                        /* convert to 411P */
......
486 491
                    }
487 492
                } else {
488 493
                    /* 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);
494
                    idct_put(s->picture.data[6 - j] + c_offset, 
495
                             s->picture.linesize[6 - j], block);
491 496
                }
492 497
            }
493 498
            block += 64;
......
496 501
    }
497 502
}
498 503

  
499

  
500 504
/* NOTE: exactly one frame must be given (120000 bytes for NTSC,
501 505
   144000 bytes for PAL) */
502 506
static int dvvideo_decode_frame(AVCodecContext *avctx, 
......
504 508
                                 uint8_t *buf, int buf_size)
505 509
{
506 510
    DVVideoDecodeContext *s = avctx->priv_data;
507
    int sct, dsf, apt, ds, nb_dif_segs, vs, width, height, i, packet_size;
508
    uint8_t *buf_ptr;
511
    int ds, vs;
509 512
    const uint16_t *mb_pos_ptr;
510 513
    
511
    /* parse id */
512
    init_get_bits(&s->gb, buf, buf_size*8);
513
    sct = get_bits(&s->gb, 3);
514
    if (sct != 0)
515
        return -1;
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) */
521

  
522
    dsf = get_bits(&s->gb, 1); /* 0 = NTSC 1 = PAL */
523
    if (get_bits(&s->gb, 1) != 0)
524
        return -1;
525
    skip_bits(&s->gb, 11);
526
    apt = get_bits(&s->gb, 3); /* apt */
527

  
528
    get_bits(&s->gb, 1); /* tf1 */
529
    skip_bits(&s->gb, 4);
530
    get_bits(&s->gb, 3); /* ap1 */
531

  
532
    get_bits(&s->gb, 1); /* tf2 */
533
    skip_bits(&s->gb, 4);
534
    get_bits(&s->gb, 3); /* ap2 */
535

  
536
    get_bits(&s->gb, 1); /* tf3 */
537
    skip_bits(&s->gb, 4);
538
    get_bits(&s->gb, 3); /* ap3 */
539
    
540
    /* init size */
541
    width = 720;
542
    if (dsf) {
543
        avctx->frame_rate = 25;
544
	avctx->frame_rate_base = 1;
545
        packet_size = PAL_FRAME_SIZE;
546
        height = 576;
547
        nb_dif_segs = 12;
548
    } else {
549
        avctx->frame_rate = 30000;
550
	avctx->frame_rate_base = 1001;
551
        packet_size = NTSC_FRAME_SIZE;
552
        height = 480;
553
        nb_dif_segs = 10;
554
    }
555
    /* NOTE: we only accept several full frames */
556
    if (buf_size < packet_size)
557
        return -1;
558
    
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
562
     */
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;
567
    } else {
568
        mb_pos_ptr = dv_place_420;
569
        avctx->pix_fmt = PIX_FMT_YUV420P;
570
    }
571

  
572
    avctx->width = width;
573
    avctx->height = height;
574
    
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].
578
     */
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;
583
    else
584
        avctx->aspect_ratio = 4.0 / 3.0;
514
    s->sys = dv_frame_profile(buf);
515
    if (!s->sys || buf_size < s->sys->frame_size)
516
        return -1; /* NOTE: we only accept several full frames */
585 517

  
518
	
586 519
    if(s->picture.data[0])
587 520
        avctx->release_buffer(avctx, &s->picture);
588 521
    
589
    s->picture.reference= 0;
522
    s->picture.reference = 0;
523
    avctx->pix_fmt = s->sys->pix_fmt;
590 524
    if(avctx->get_buffer(avctx, &s->picture) < 0) {
591 525
        fprintf(stderr, "get_buffer() failed\n");
592 526
        return -1;
593 527
    }
594 528

  
595
    for(i=0;i<3;i++) {
596
        s->current_picture[i] = s->picture.data[i];
597
        s->linesize[i] = s->picture.linesize[i];
598
        if (!s->current_picture[i])
599
            return -1;
600
    }
601
    s->width = width;
602
    s->height = height;
603

  
604 529
    /* for each DIF segment */
605
    buf_ptr = buf;
606
    for (ds = 0; ds < nb_dif_segs; ds++) {
607
        buf_ptr += 6 * 80; /* skip DIF segment header */
530
    mb_pos_ptr = s->sys->video_place;
531
    for (ds = 0; ds < s->sys->difseg_size; ds++) {
532
        buf += 6 * 80; /* skip DIF segment header */
608 533
        
609 534
        for(vs = 0; vs < 27; vs++) {
610
            if ((vs % 3) == 0) {
611
                /* skip audio block */
612
                buf_ptr += 80;
613
            }
614
            dv_decode_video_segment(s, buf_ptr, mb_pos_ptr);
615
            buf_ptr += 5 * 80;
535
            if ((vs % 3) == 0)
536
	        buf += 80; /* skip audio block */
537
            
538
	    dv_decode_video_segment(s, buf, mb_pos_ptr);
539
            buf += 5 * 80;
616 540
            mb_pos_ptr += 5;
617 541
        }
618 542
    }
......
623 547
    *data_size = sizeof(AVFrame);
624 548
    *(AVFrame*)data= s->picture;
625 549
    
626
    return packet_size;
550
    return s->sys->frame_size;
627 551
}
628 552

  
629 553
static int dvvideo_decode_end(AVCodecContext *avctx)
......
645 569
    CODEC_CAP_DR1,
646 570
    NULL
647 571
};
648

  
649
typedef struct DVAudioDecodeContext {
650
    AVCodecContext *avctx;
651
    GetBitContext gb;
652
} DVAudioDecodeContext;
653

  
654
static int dvaudio_decode_init(AVCodecContext *avctx)
655
{
656
    //    DVAudioDecodeContext *s = avctx->priv_data;
657
    return 0;
658
}
659

  
660
static uint16_t dv_audio_12to16(uint16_t sample)
661
{
662
    uint16_t shift, result;
663
    
664
    sample = (sample < 0x800) ? sample : sample | 0xf000;
665
    shift = (sample & 0xf00) >> 8;
666

  
667
    if (shift < 0x2 || shift > 0xd) {
668
	result = sample;
669
    } else if (shift < 0x8) {
670
        shift--;
671
	result = (sample - (256 * shift)) << shift;
672
    } else {
673
	shift = 0xe - shift;
674
	result = ((sample + ((256 * shift) + 1)) << shift) - 1;
675
    }
676

  
677
    return result;
678
}
679

  
680
/* NOTE: exactly one frame must be given (120000 bytes for NTSC,
681
   144000 bytes for PAL) 
682

  
683
   There's a couple of assumptions being made here:
684
         1. By default we silence erroneous (0x8000/16bit 0x800/12bit) 
685
	    audio samples. We can pass them upwards when ffmpeg will be ready
686
	    to deal with them.
687
	 2. We don't do software emphasis.
688
	 3. Audio is always returned as 16bit linear samples: 12bit
689
	    nonlinear samples are converted into 16bit linear ones.
690
*/
691
static int dvaudio_decode_frame(AVCodecContext *avctx, 
692
                                 void *data, int *data_size,
693
                                 uint8_t *buf, int buf_size)
694
{
695
    DVVideoDecodeContext *s = avctx->priv_data;
696
    const uint16_t (*unshuffle)[9];
697
    int smpls, freq, quant, sys, stride, difseg, ad, dp, nb_dif_segs, i;
698
    uint16_t lc, rc;
699
    uint8_t *buf_ptr;
700
    
701
    /* parse id */
702
    init_get_bits(&s->gb, &buf[AAUX_AS_OFFSET], 5*8);
703
    i = get_bits(&s->gb, 8);
704
    if (i != 0x50) { /* No audio ? */
705
	*data_size = 0;
706
	return buf_size;
707
    }
708
    
709
    get_bits(&s->gb, 1); /* 0 - locked audio, 1 - unlocked audio */
710
    skip_bits(&s->gb, 1);
711
    smpls = get_bits(&s->gb, 6); /* samples in this frame - min. samples */
712

  
713
    skip_bits(&s->gb, 8);
714

  
715
    skip_bits(&s->gb, 2);
716
    sys = get_bits(&s->gb, 1); /* 0 - 60 fields, 1 = 50 fields */
717
    skip_bits(&s->gb, 5);
718

  
719
    get_bits(&s->gb, 1); /* 0 - emphasis on, 1 - emphasis off */
720
    get_bits(&s->gb, 1); /* 0 - reserved, 1 - emphasis time constant 50/15us */
721
    freq = get_bits(&s->gb, 3); /* 0 - 48KHz, 1 - 44,1kHz, 2 - 32 kHz */
722
    quant = get_bits(&s->gb, 3); /* 0 - 16bit linear, 1 - 12bit nonlinear */
723

  
724
    if (quant > 1)
725
	return -1; /* Unsupported quantization */
726

  
727
    avctx->sample_rate = dv_audio_frequency[freq];
728
    avctx->channels = 2;
729
    avctx->bit_rate = avctx->channels * avctx->sample_rate * 16;
730
    // What about:
731
    // avctx->frame_size =
732
   
733
    *data_size = (dv_audio_min_samples[sys][freq] + smpls) * 
734
	         avctx->channels * 2;
735

  
736
    if (sys) {
737
	nb_dif_segs = 12;
738
	stride = 108;
739
	unshuffle = dv_place_audio50;
740
    } else {
741
	nb_dif_segs = 10;
742
	stride = 90;
743
	unshuffle = dv_place_audio60;
744
    }
745
    
746
    /* for each DIF segment */
747
    buf_ptr = buf;
748
    for (difseg = 0; difseg < nb_dif_segs; difseg++) {
749
         buf_ptr += 6 * 80; /* skip DIF segment header */
750
         for (ad = 0; ad < 9; ad++) {
751
              
752
              for (dp = 8; dp < 80; dp+=2) {
753
		   if (quant == 0) {  /* 16bit quantization */
754
		       i = unshuffle[difseg][ad] + (dp - 8)/2 * stride;
755
		       ((short *)data)[i] = (buf_ptr[dp] << 8) | buf_ptr[dp+1]; 
756
		       if (((unsigned short *)data)[i] == 0x8000)
757
		           ((short *)data)[i] = 0;
758
		   } else {           /* 12bit quantization */
759
		       if (difseg >= nb_dif_segs/2)
760
			   goto out;  /* We're not doing 4ch at this time */
761
		       
762
		       lc = ((uint16_t)buf_ptr[dp] << 4) | 
763
			    ((uint16_t)buf_ptr[dp+2] >> 4);
764
		       rc = ((uint16_t)buf_ptr[dp+1] << 4) |
765
			    ((uint16_t)buf_ptr[dp+2] & 0x0f);
766
		       lc = (lc == 0x800 ? 0 : dv_audio_12to16(lc));
767
		       rc = (rc == 0x800 ? 0 : dv_audio_12to16(rc));
768

  
769
		       i = unshuffle[difseg][ad] + (dp - 8)/3 * stride;
770
		       ((short *)data)[i] = lc;
771
		       i = unshuffle[difseg+nb_dif_segs/2][ad] + (dp - 8)/3 * stride;
772
		       ((short *)data)[i] = rc;
773
		       ++dp;
774
		   }
775
	      }
776
		
777
	    buf_ptr += 16 * 80; /* 15 Video DIFs + 1 Audio DIF */
778
        }
779
    }
780

  
781
out:
782
    return buf_size;
783
}
784

  
785
static int dvaudio_decode_end(AVCodecContext *avctx)
786
{
787
    //    DVAudioDecodeContext *s = avctx->priv_data;
788
    return 0;
789
}
790

  
791
AVCodec dvaudio_decoder = {
792
    "dvaudio",
793
    CODEC_TYPE_AUDIO,
794
    CODEC_ID_DVAUDIO,
795
    sizeof(DVAudioDecodeContext),
796
    dvaudio_decode_init,
797
    NULL,
798
    dvaudio_decode_end,
799
    dvaudio_decode_frame,
800
    0,
801
    NULL
802
};

Also available in: Unified diff