Revision 7458ccbb

View differences:

libavcodec/allcodecs.c
103 103
    register_avcodec(&mpeg_xvmc_decoder);
104 104
#endif
105 105
    register_avcodec(&dvvideo_decoder);
106
    register_avcodec(&dvaudio_decoder);
107 106
    register_avcodec(&mjpeg_decoder);
108 107
    register_avcodec(&mjpegb_decoder);
109 108
    register_avcodec(&mp2_decoder);
libavcodec/avcodec.h
1388 1388
extern AVCodec svq1_decoder;
1389 1389
extern AVCodec svq3_decoder;
1390 1390
extern AVCodec dvvideo_decoder;
1391
extern AVCodec dvaudio_decoder;
1392 1391
extern AVCodec wmav1_decoder;
1393 1392
extern AVCodec wmav2_decoder;
1394 1393
extern AVCodec mjpeg_decoder;
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
};
libavcodec/dvdata.h
21 21
 * @file dvdata.h
22 22
 * Constants for DV codec.
23 23
 */
24
 
24

  
25
/* 
26
 * DVprofile is used to express the differences between various 
27
 * DV flavors. For now it's primarily used for differentiating
28
 * 525/60 and 625/50, but the plans are to use it for various
29
 * DV specs as well (e.g. SMPTE314M vs. IEC 61834).
30
 */
31
typedef struct DVprofile {
32
    int              dsf;                 /* value of the dsf in the DV header */
33
    int              frame_size;          /* total size of one frame in bytes */
34
    int              difseg_size;         /* number of DIF segments */
35
    int              frame_rate;      
36
    int              frame_rate_base;
37
    int              ltc_divisor;         /* FPS from the LTS standpoint */
38
    int              height;              /* picture height in pixels */
39
    int              width;               /* picture width in pixels */
40
    const uint16_t  *video_place;         /* positions of all DV macro blocks */
41
    enum PixelFormat pix_fmt;             /* picture pixel format */
42
    
43
    int              audio_stride;        /* size of audio_shuffle table */
44
    int              audio_min_samples[3];/* min ammount of audio samples */
45
                                          /* for 48Khz, 44.1Khz and 32Khz */
46
    int              audio_samples_dist[5];/* how many samples are supposed to be */
47
                                         /* in each frame in a 5 frames window */
48
    const uint16_t (*audio_shuffle)[9];  /* PCM shuffling table */
49
} DVprofile;
50

  
25 51
#define NB_DV_VLC 409
26
#define AAUX_AS_OFFSET  (80*6 + 80*16*3 + 3)
27
#define AAUX_ASC_OFFSET (80*6 + 80*16*4 + 3)
28
#define VAUX_TC61_OFFSET (80*5 + 48 + 5)
29 52

  
30 53
static const uint16_t dv_vlc_bits[409] = {
31 54
 0x0000, 0x0002, 0x0007, 0x0008, 0x0009, 0x0014, 0x0015, 0x0016,
......
283 306
    1,2,2,3,3,3,3,3,
284 307
};
285 308

  
286
static uint8_t dv_quant_shifts[22][4] = {
309
static const uint8_t dv_quant_shifts[22][4] = {
287 310
  { 3,3,4,4 }, 
288 311
  { 3,3,4,4 }, 
289 312
  { 2,3,3,4 }, 
......
1240 1263
 0x0834, 0x2320, 0x2f44, 0x3810, 0x1658,
1241 1264
};
1242 1265

  
1243
static const uint16_t dv_place_audio60[10][9] = {
1266
static const uint16_t dv_audio_shuffle525[10][9] = {
1244 1267
  {  0, 30, 60, 20, 50, 80, 10, 40, 70 }, /* 1st channel */
1245 1268
  {  6, 36, 66, 26, 56, 86, 16, 46, 76 },
1246 1269
  { 12, 42, 72,  2, 32, 62, 22, 52, 82 },
......
1254 1277
  { 25, 55, 85, 15, 45, 75,  5, 35, 65 },
1255 1278
};
1256 1279

  
1257
static const uint16_t dv_place_audio50[12][9] = {
1280
static const uint16_t dv_audio_shuffle625[12][9] = {
1258 1281
  {   0,  36,  72,  26,  62,  98,  16,  52,  88}, /* 1st channel */
1259 1282
  {   6,  42,  78,  32,  68, 104,  22,  58,  94},
1260 1283
  {  12,  48,  84,   2,  38,  74,  28,  64, 100},
......
1271 1294
};
1272 1295

  
1273 1296
static const int dv_audio_frequency[3] = {
1274
    48000, 44100, 32000, 
1297
    48000, 44100, 32000,
1275 1298
};
1276

  
1277
static const int dv_audio_min_samples[2][3] = {
1278
    { 1580, 1452, 1053 }, /* 60 fields */
1279
    { 1896, 1742, 1264 }, /* 50 fileds */
1299
    
1300
static const DVprofile dv_profiles[] = {
1301
    { .dsf = 0,
1302
      .frame_size = 120000,        /* IEC 61834, SMPTE-314M - 525/60 (NTSC) */
1303
      .difseg_size = 10,
1304
      .frame_rate = 30000,
1305
      .ltc_divisor = 30,
1306
      .frame_rate_base = 1001,
1307
      .height = 480,
1308
      .width = 720,
1309
      .video_place = dv_place_411,
1310
      .pix_fmt = PIX_FMT_YUV411P,
1311
      .audio_stride = 90,
1312
      .audio_min_samples = { 1580, 1452, 1053 }, /* for 48, 44.1 and 32Khz */
1313
      .audio_samples_dist = { 1602, 1601, 1602, 1601, 1602 },
1314
      .audio_shuffle = dv_audio_shuffle525,
1315
    }, 
1316
    { .dsf = 1,
1317
      .frame_size = 144000,        /* IEC 61834 - 625/50 (PAL) */
1318
      .difseg_size = 12,
1319
      .frame_rate = 25,
1320
      .frame_rate_base = 1,
1321
      .ltc_divisor = 25,
1322
      .height = 576,
1323
      .width = 720,
1324
      .video_place = dv_place_420,
1325
      .pix_fmt = PIX_FMT_YUV420P,
1326
      .audio_stride = 108,
1327
      .audio_min_samples = { 1896, 1742, 1264 }, /* for 48, 44.1 and 32Khz */
1328
      .audio_samples_dist = { 1920, 1920, 1920, 1920, 1920 },
1329
      .audio_shuffle = dv_audio_shuffle625,
1330
    },
1331
    { .dsf = 1,
1332
      .frame_size = 144000,        /* SMPTE-314M - 625/50 (PAL) */
1333
      .difseg_size = 12,
1334
      .frame_rate = 25,
1335
      .frame_rate_base = 1,
1336
      .ltc_divisor = 25,
1337
      .height = 576,
1338
      .width = 720,
1339
      .video_place = dv_place_411P,
1340
      .pix_fmt = PIX_FMT_YUV411P,
1341
      .audio_stride = 108,
1342
      .audio_min_samples = { 1896, 1742, 1264 }, /* for 48, 44.1 and 32Khz */
1343
      .audio_samples_dist = { 1920, 1920, 1920, 1920, 1920 },
1344
      .audio_shuffle = dv_audio_shuffle625,
1345
     }
1280 1346
};
1347

  
1348
static inline const DVprofile* dv_frame_profile(uint8_t* frame)
1349
{
1350
    if ((frame[3] & 0x80) == 0) {      /* DSF flag */
1351
        return &dv_profiles[0];
1352
    }
1353
    else if ((frame[5] & 0x07) == 0) { /* APT flag */
1354
        return &dv_profiles[1];
1355
    }
1356
    else 
1357
        return &dv_profiles[2];
1358
}
1359

  
1360
static inline const DVprofile* dv_codec_profile(AVCodecContext* codec)
1361
{
1362
    if (codec->width != 720) {
1363
        return NULL;
1364
    } 
1365
    else if (codec->height == 480) {
1366
        return &dv_profiles[0];
1367
    } 
1368
    else 
1369
        return &dv_profiles[1];
1370
}
libavformat/Makefile
13 13

  
14 14
# mux and demuxes
15 15
OBJS+=mpeg.o mpegts.o mpegtsenc.o ffm.o crc.o img.o raw.o rm.o \
16
      avienc.o avidec.o wav.o swf.o au.o gif.o mov.o mpjpeg.o dvcore.o dv.o \
16
      avienc.o avidec.o wav.o swf.o au.o gif.o mov.o mpjpeg.o dv.o \
17 17
      yuv4mpeg.o 4xm.o flvenc.o flvdec.o movenc.o psxstr.o idroq.o ipmovie.o \
18 18
      nut.o wc3movie.o mp3.o
19 19

  
libavformat/avidec.c
18 18
 */
19 19
#include "avformat.h"
20 20
#include "avi.h"
21
#include "dv.h"
21 22

  
22 23
//#define DEBUG
23 24

  
24
static const struct AVI1Handler {
25
   enum CodecID vcid;
26
   enum CodecID acid;
27
   uint32_t tag;
28
} AVI1Handlers[] = {
29
  { CODEC_ID_DVVIDEO, CODEC_ID_DVAUDIO, MKTAG('d', 'v', 's', 'd') },
30
  { CODEC_ID_DVVIDEO, CODEC_ID_DVAUDIO, MKTAG('d', 'v', 'h', 'd') },
31
  { CODEC_ID_DVVIDEO, CODEC_ID_DVAUDIO, MKTAG('d', 'v', 's', 'l') },
32
  /* This is supposed to be the last one */
33
  { CODEC_ID_NONE, CODEC_ID_NONE, 0 },
34
};
35

  
36 25
typedef struct AVIIndex {
37 26
    unsigned char tag[4];
38 27
    unsigned int flags, pos, len;
......
40 29
} AVIIndex;
41 30

  
42 31
typedef struct {
43
    int64_t riff_end;
44
    int64_t movi_end;
45
    int     type;
46
    uint8_t *buf;
47
    int      buf_size;
48
    int      stream_index;
32
    int64_t  riff_end;
33
    int64_t  movi_end;
49 34
    offset_t movi_list;
50 35
    AVIIndex *first, *last;
36
    void* dv_demux;
51 37
} AVIContext;
52 38

  
53 39
#ifdef DEBUG
......
97 83
    stream_index = -1;
98 84
    codec_type = -1;
99 85
    frame_period = 0;
100
    avi->type = 2;
101
    avi->buf = av_malloc(1);
102
    if (!avi->buf)
103
        return -1;
104
    avi->buf_size = 1;
105 86
    for(;;) {
106 87
        if (url_feof(pb))
107 88
            goto fail;
......
134 115
	    url_fskip(pb, 4 * 4);
135 116
            n = get_le32(pb);
136 117
            for(i=0;i<n;i++) {
137
                st = av_new_stream(s, 0);
118
                st = av_new_stream(s, i);
138 119
                if (!st)
139 120
                    goto fail;
140 121
	    }
......
144 125
            /* stream header */
145 126
            stream_index++;
146 127
            tag1 = get_le32(pb);
128
            handler = get_le32(pb); /* codec tag */
147 129
            switch(tag1) {
148 130
            case MKTAG('i', 'a', 'v', 's'):
149 131
	    case MKTAG('i', 'v', 'a', 's'):
132
                /* 
133
	         * After some consideration -- I don't think we 
134
	         * have to support anything but DV in a type1 AVIs.
135
	         */
150 136
	        if (s->nb_streams != 1)
151 137
		    goto fail;
152
		avi->type = 1;
153
		avi->stream_index = 0;
138
	        
139
		if (handler != MKTAG('d', 'v', 's', 'd') &&
140
	            handler != MKTAG('d', 'v', 'h', 'd') &&
141
		    handler != MKTAG('d', 'v', 's', 'l'))
142
	           goto fail;
143

  
144
	        avi->dv_demux = dv_init_demux(s, stream_index, stream_index + 1);
145
		if (!avi->dv_demux)
146
		    goto fail;
147
	        stream_index++;
154 148
	    case MKTAG('v', 'i', 'd', 's'):
155 149
                codec_type = CODEC_TYPE_VIDEO;
156 150

  
157 151
                if (stream_index >= s->nb_streams) {
158
                    url_fskip(pb, size - 4);
152
                    url_fskip(pb, size - 8);
159 153
                    break;
160 154
                } 
161 155

  
162 156
                st = s->streams[stream_index];
163 157

  
164
                handler = get_le32(pb); /* codec tag */
165 158
                get_le32(pb); /* flags */
166 159
                get_le16(pb); /* priority */
167 160
                get_le16(pb); /* language */
......
186 179
                    st->codec.frame_rate_base * AV_TIME_BASE / 
187 180
                    st->codec.frame_rate;
188 181
                
189
                if (avi->type == 1) {
190
                    AVStream *st;
191

  
192
                    st = av_new_stream(s, 0);
193
                    if (!st)
194
		        goto fail;
195
                    
196
		    stream_index++;
197
		    
198
		    for (i=0; AVI1Handlers[i].tag != 0; ++i)
199
		       if (AVI1Handlers[i].tag == handler)
200
		           break;
201

  
202
		    if (AVI1Handlers[i].tag != 0) {
203
		        s->streams[0]->codec.codec_type = CODEC_TYPE_VIDEO;
204
                        s->streams[0]->codec.codec_id   = AVI1Handlers[i].vcid;
205
		        s->streams[1]->codec.codec_type = CODEC_TYPE_AUDIO;
206
                        s->streams[1]->codec.codec_id   = AVI1Handlers[i].acid;
207
		    } else {
208
		        goto fail;
209
                    }
210
		}
211
		
212 182
		url_fskip(pb, size - 9 * 4);
213 183
                break;
214 184
            case MKTAG('a', 'u', 'd', 's'):
......
218 188
                    codec_type = CODEC_TYPE_AUDIO;
219 189

  
220 190
                    if (stream_index >= s->nb_streams) {
221
                        url_fskip(pb, size - 4);
191
                        url_fskip(pb, size - 8);
222 192
                        break;
223 193
                    } 
224 194
                    st = s->streams[stream_index];
225 195

  
226
                    get_le32(pb); /* tag */
227 196
                    get_le32(pb); /* flags */
228 197
                    get_le16(pb); /* priority */
229 198
                    get_le16(pb); /* language */
......
244 213
            break;
245 214
        case MKTAG('s', 't', 'r', 'f'):
246 215
            /* stream header */
247
            if (stream_index >= s->nb_streams || avi->type == 1) {
216
            if (stream_index >= s->nb_streams || avi->dv_demux) {
248 217
                url_fskip(pb, size);
249 218
            } else {
250 219
                st = s->streams[stream_index];
......
305 274
    /* check stream number */
306 275
    if (stream_index != s->nb_streams - 1) {
307 276
    fail:
308
        av_free(avi->buf);
309 277
        for(i=0;i<s->nb_streams;i++) {
310 278
            av_freep(&s->streams[i]->codec.extradata);
311 279
            av_freep(&s->streams[i]);
......
316 284
    return 0;
317 285
}
318 286

  
319
static void __destruct_pkt(struct AVPacket *pkt)
320
{
321
    pkt->data = NULL; pkt->size = 0;
322
    return;
323
}
324

  
325 287
static int avi_read_packet(AVFormatContext *s, AVPacket *pkt)
326 288
{
327 289
    AVIContext *avi = s->priv_data;
328 290
    ByteIOContext *pb = &s->pb;
329 291
    int n, d[8], size, i;
292
    void* dstr;
330 293

  
331 294
    memset(d, -1, sizeof(int)*8);
332
    
333
    if (avi->type == 1 && avi->stream_index) {
334
        /* duplicate DV packet */
335
        av_init_packet(pkt);
336
        pkt->data = avi->buf;
337
        pkt->size = avi->buf_size;
338
        pkt->destruct = __destruct_pkt;
339
        pkt->stream_index = avi->stream_index;
340
        avi->stream_index = !avi->stream_index;
341
        return 0;
295
   
296
    if (avi->dv_demux) {
297
        size = dv_get_packet(avi->dv_demux, pkt);
298
	if (size >= 0)
299
	    return size;
342 300
    }
343

  
301
        
344 302
    for(i=url_ftell(pb); !url_feof(pb); i++) {
345 303
        int j;
346 304

  
......
387 345
            && n < s->nb_streams
388 346
            && i + size <= avi->movi_end) {
389 347
        
390
            if (avi->type == 1) {
391
                uint8_t *tbuf = av_realloc(avi->buf, size + FF_INPUT_BUFFER_PADDING_SIZE);
392
                if (!tbuf)
393
                    return -1;
394
                avi->buf = tbuf;
395
                avi->buf_size = size;
396
                av_init_packet(pkt);
397
                pkt->data = avi->buf;
398
                pkt->size = avi->buf_size;
399
                pkt->destruct = __destruct_pkt;
400
                avi->stream_index = n;
401
            } else {
402
                av_new_packet(pkt, size);
403
            }
348
            av_new_packet(pkt, size);
404 349
            get_buffer(pb, pkt->data, size);
405
            if (size & 1)
350
            if (size & 1) {
406 351
                get_byte(pb);
407
            pkt->stream_index = n;
408
            pkt->flags |= PKT_FLAG_KEY; // FIXME: We really should read index for that
409
            return 0;
352
		size++;
353
	    }
354
	
355
	    if (avi->dv_demux) {
356
	        dstr = pkt->destruct;
357
	        size = dv_produce_packet(avi->dv_demux, pkt,
358
		                         pkt->data, pkt->size);
359
		pkt->destruct = dstr;
360
	    } else {
361
                pkt->stream_index = n;
362
                pkt->flags |= PKT_FLAG_KEY; // FIXME: We really should read 
363
		                            //        index for that
364
	    }
365
            return size;
410 366
        }
411 367
    }
412 368
    return -1;
......
416 372
{
417 373
    int i;
418 374
    AVIContext *avi = s->priv_data;
419
    av_free(avi->buf);
420 375

  
421 376
    for(i=0;i<s->nb_streams;i++) {
422 377
        AVStream *st = s->streams[i];
......
424 379
        av_free(st->codec.extradata);
425 380
    }
426 381

  
382
    if (avi->dv_demux)
383
        av_free(avi->dv_demux);
384

  
427 385
    return 0;
428 386
}
429 387

  
libavformat/dv.c
1 1
/* 
2
 * General DV muxer/demuxer 
3
 * Copyright (c) 2003 Roman Shaposhnick
4
 *
5
 * Many thanks to Dan Dennedy <dan@dennedy.org> for providing wealth
6
 * of DV technical info.
7
 *
2 8
 * Raw DV format
3 9
 * Copyright (c) 2002 Fabrice Bellard.
4 10
 *
......
16 22
 * License along with this library; if not, write to the Free Software
17 23
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 24
 */
25
#include <time.h>
19 26
#include "avformat.h"
20
#include "dvcore.h"
27
#include "dvdata.h"
21 28

  
22 29
typedef struct DVDemuxContext {
23
    int       is_audio;
24
    uint8_t   buf[144000];    
25
    int       size;
30
    AVPacket  audio_pkt;
31
    AVStream  *vst;
32
    AVStream  *ast;
26 33
} DVDemuxContext;
27 34

  
28
/* raw input */
29
static int dv_read_header(AVFormatContext *s,
30
                          AVFormatParameters *ap)
35
typedef struct DVMuxContext {
36
    const DVprofile*  sys;    /* Current DV profile. E.g.: 525/60, 625/50 */
37
    uint8_t     frame_buf[144000]; /* frame under contruction */
38
    FifoBuffer  audio_data;   /* Fifo for storing excessive amounts of PCM */
39
    int         frames;       /* Number of a current frame */
40
    time_t      start_time;   /* Start time of recording */
41
    uint8_t     aspect;       /* Aspect ID 0 - 4:3, 7 - 16:9 */
42
    int         has_audio;    /* frame under contruction has audio */
43
    int         has_video;    /* frame under contruction has video */
44
} DVMuxContext;
45

  
46
enum dv_section_type {
47
     dv_sect_header  = 0x1f,
48
     dv_sect_subcode = 0x3f,
49
     dv_sect_vaux    = 0x56,
50
     dv_sect_audio   = 0x76,
51
     dv_sect_video   = 0x96,
52
};
53

  
54
enum dv_pack_type {
55
     dv_header525     = 0x3f, /* see dv_write_pack for important details on */ 
56
     dv_header625     = 0xbf, /* these two packs */
57
     dv_timecode      = 0x13,
58
     dv_audio_source  = 0x50,
59
     dv_audio_control = 0x51,
60
     dv_audio_recdate = 0x52,
61
     dv_audio_rectime = 0x53,
62
     dv_video_source  = 0x60,
63
     dv_video_control = 0x61,
64
     dv_viedo_recdate = 0x62,
65
     dv_video_rectime = 0x63,
66
     dv_unknown_pack  = 0xff,
67
};
68

  
69

  
70

  
71
/*
72
 * The reason why the following three big ugly looking tables are
73
 * here is my lack of DV spec IEC 61834. The tables were basically 
74
 * constructed to make code that places packs in SSYB, VAUX and 
75
 * AAUX blocks very simple and table-driven. They conform to the
76
 * SMPTE 314M and the output of my personal DV camcorder, neither
77
 * of which is sufficient for a reliable DV stream producing. Thus
78
 * while code is still in development I'll be gathering input from
79
 * people with different DV equipment and modifying the tables to
80
 * accommodate all the quirks. Later on, if possible, some of them
81
 * will be folded into smaller tables and/or switch-if logic. For 
82
 * now, my only excuse is -- they don't eat up that much of a space.
83
 */
84

  
85
static const int dv_ssyb_packs_dist[12][6] = {
86
    { 0x13, 0x13, 0x13, 0x13, 0x13, 0x13 },
87
    { 0x13, 0x13, 0x13, 0x13, 0x13, 0x13 },
88
    { 0x13, 0x13, 0x13, 0x13, 0x13, 0x13 },
89
    { 0x13, 0x13, 0x13, 0x13, 0x13, 0x13 },
90
    { 0x13, 0x13, 0x13, 0x13, 0x13, 0x13 },
91
    { 0x13, 0x13, 0x13, 0x13, 0x13, 0x13 },
92
    { 0x13, 0x62, 0x63, 0x13, 0x62, 0x63 },
93
    { 0x13, 0x62, 0x63, 0x13, 0x62, 0x63 },
94
    { 0x13, 0x62, 0x63, 0x13, 0x62, 0x63 },
95
    { 0x13, 0x62, 0x63, 0x13, 0x62, 0x63 },
96
    { 0x13, 0x62, 0x63, 0x13, 0x62, 0x63 },
97
    { 0x13, 0x62, 0x63, 0x13, 0x62, 0x63 },
98
};
99

  
100
static const int dv_vaux_packs_dist[12][15] = {
101
    { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff, 
102
      0x60, 0x61, 0x62, 0x63, 0xff, 0xff },
103
    { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff, 
104
      0x60, 0x61, 0x62, 0x63, 0xff, 0xff },
105
    { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff, 
106
      0x60, 0x61, 0x62, 0x63, 0xff, 0xff },
107
    { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff, 
108
      0x60, 0x61, 0x62, 0x63, 0xff, 0xff },
109
    { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff, 
110
      0x60, 0x61, 0x62, 0x63, 0xff, 0xff },
111
    { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff, 
112
      0x60, 0x61, 0x62, 0x63, 0xff, 0xff },
113
    { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff, 
114
      0x60, 0x61, 0x62, 0x63, 0xff, 0xff },
115
    { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff, 
116
      0x60, 0x61, 0x62, 0x63, 0xff, 0xff },
117
    { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff, 
118
      0x60, 0x61, 0x62, 0x63, 0xff, 0xff },
119
    { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff, 
120
      0x60, 0x61, 0x62, 0x63, 0xff, 0xff },
121
    { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff, 
122
      0x60, 0x61, 0x62, 0x63, 0xff, 0xff },
123
    { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff, 
124
      0x60, 0x61, 0x62, 0x63, 0xff, 0xff },
125
};
126

  
127
static const int dv_aaux_packs_dist[12][9] = {
128
    { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff },
129
    { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff },
130
    { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff },
131
    { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff },
132
    { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff },
133
    { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff },
134
    { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff },
135
    { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff },
136
    { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff },
137
    { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff },
138
    { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff },
139
    { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff },
140
};
141

  
142
static inline uint16_t dv_audio_12to16(uint16_t sample)
143
{
144
    uint16_t shift, result;
145
    
146
    sample = (sample < 0x800) ? sample : sample | 0xf000;
147
    shift = (sample & 0xf00) >> 8;
148

  
149
    if (shift < 0x2 || shift > 0xd) {
150
	result = sample;
151
    } else if (shift < 0x8) {
152
        shift--;
153
	result = (sample - (256 * shift)) << shift;
154
    } else {
155
	shift = 0xe - shift;
156
	result = ((sample + ((256 * shift) + 1)) << shift) - 1;
157
    }
158

  
159
    return result;
160
}
161

  
162
static int dv_audio_frame_size(const DVprofile* sys, int frame)
31 163
{
32
    AVStream *vst, *ast;
33
    DVDemuxContext *c = s->priv_data;
164
    return sys->audio_samples_dist[frame % (sizeof(sys->audio_samples_dist)/
165
		                            sizeof(sys->audio_samples_dist[0]))];
166
}
34 167

  
35
    vst = av_new_stream(s, 0);
36
    if (!vst)
37
        return AVERROR_NOMEM;
38
    vst->codec.codec_type = CODEC_TYPE_VIDEO;
39
    vst->codec.codec_id = CODEC_ID_DVVIDEO;
40
    vst->codec.bit_rate = 25000000;
168
static int dv_write_pack(enum dv_pack_type pack_id, DVMuxContext *c, uint8_t* buf)
169
{
170
    struct tm tc;
171
    time_t ct;
172
    int ltc_frame;
41 173

  
42
    ast = av_new_stream(s, 1);
43
    if (!ast)
44
        return AVERROR_NOMEM;
174
    buf[0] = (uint8_t)pack_id;
175
    switch (pack_id) {
176
    case dv_header525: /* I can't imagine why these two weren't defined as real */
177
    case dv_header625: /* packs in SMPTE314M -- they definitely look like ones */
178
          buf[1] = 0xf8 |               /* reserved -- always 1 */
179
	           (0 & 0x07);          /* APT: Track application ID */
180
          buf[2] = (0 << 7)    | /* TF1: audio data is 0 - valid; 1 - invalid */
181
	           (0x0f << 3) | /* reserved -- always 1 */
182
		   (0 & 0x07);   /* AP1: Audio application ID */
183
          buf[3] = (0 << 7)    | /* TF2: video data is 0 - valid; 1 - invalid */  
184
	           (0x0f << 3) | /* reserved -- always 1 */
185
		   (0 & 0x07);   /* AP2: Video application ID */
186
          buf[4] = (0 << 7)    | /* TF3: subcode(SSYB) is 0 - valid; 1 - invalid */ 
187
	           (0x0f << 3) | /* reserved -- always 1 */
188
		   (0 & 0x07);   /* AP3: Subcode application ID */
189
	  break;
190
    case dv_timecode:
191
          ct = (time_t)(c->frames / ((float)c->sys->frame_rate / 
192
                                     (float)c->sys->frame_rate_base));
193
          localtime_r(&ct, &tc);
194
          /* 
195
           * LTC drop-frame frame counter drops two frames (0 and 1) every 
196
           * minute, unless it is exactly divisible by 10
197
           */
198
          ltc_frame = (c->frames + 2*ct/60 - 2*ct/600) % c->sys->ltc_divisor;
199
	  buf[1] = (0 << 7) | /* Color fame: 0 - unsync; 1 - sync mode */
200
		   (1 << 6) | /* Drop frame timecode: 0 - nondrop; 1 - drop */
201
		   ((ltc_frame / 10) << 4) | /* Tens of frames */
202
		   (ltc_frame % 10);         /* Units of frames */
203
	  buf[2] = (1 << 7) | /* Biphase mark polarity correction: 0 - even; 1 - odd */
204
		   ((tc.tm_sec / 10) << 4) | /* Tens of seconds */
205
		   (tc.tm_sec % 10);         /* Units of seconds */
206
	  buf[3] = (1 << 7) | /* Binary group flag BGF0 */
207
		   ((tc.tm_min / 10) << 4) | /* Tens of minutes */
208
		   (tc.tm_min % 10);         /* Units of minutes */
209
	  buf[4] = (1 << 7) | /* Binary group flag BGF2 */
210
		   (1 << 6) | /* Binary group flag BGF1 */
211
	           ((tc.tm_hour / 10) << 4) | /* Tens of hours */
212
		   (tc.tm_hour % 10);         /* Units of hours */
213
          break;
214
    case dv_audio_source:  /* AAUX source pack */
215
          buf[1] = (0 << 7) | /* locked mode       */
216
                   (1 << 6) | /* reserved -- always 1 */
217
	           (dv_audio_frame_size(c->sys, c->frames) -
218
		    c->sys->audio_min_samples[0]);
219
	                      /* # of samples      */
220
          buf[2] = (0 << 7) | /* multi-stereo      */
221
                   (0 << 5) | /* #of audio channels per block: 0 -- 1 channel */
222
                   (0 << 4) | /* pair bit: 0 -- one pair of channels */
223
	            0;        /* audio mode        */
224
          buf[3] = (1 << 7) | /* res               */
225
                   (1 << 6) | /* multi-language flag */
226
	           (c->sys->dsf << 5) | /*  system: 60fields/50fields */
227
	            0;        /* definition: 0 -- SD (525/625) */
228
          buf[4] = (1 << 7) | /* emphasis: 1 -- off */
229
                   (0 << 6) | /* emphasis time constant: 0 -- reserved */
230
	           (0 << 3) | /* frequency: 0 -- 48Khz, 1 -- 44,1Khz, 2 -- 32Khz */
231
                    0;        /* quantization: 0 -- 16bit linear, 1 -- 12bit nonlinear */			    
232
          break;
233
    case dv_audio_control:
234
          buf[1] = (0 << 6) | /* copy protection: 0 -- unrestricted */
235
                   (1 << 4) | /* input source: 1 -- digital input */
236
	           (3 << 2) | /* compression: 3 -- no information */
237
	            0;        /* misc. info/SMPTE emphasis off */
238
          buf[2] = (1 << 7) | /* recording start point: 1 -- no */
239
                   (1 << 6) | /* recording end point: 1 -- no */
240
	           (1 << 3) | /* recording mode: 1 -- original */
241
	            7;         
242
          buf[3] = (1 << 7) | /* direction: 1 -- forward */
243
                    0x20;     /* speed */
244
          buf[4] = (1 << 7) | /* reserved -- always 1 */
245
                    0x7f;     /* genre category */
246
	  break;
247
    case dv_audio_recdate:
248
    case dv_viedo_recdate:  /* VAUX recording date */
249
          ct = c->start_time + (time_t)(c->frames / 
250
	       ((float)c->sys->frame_rate / (float)c->sys->frame_rate_base));
251
          localtime_r(&ct, &tc);
252
	  buf[1] = 0xff; /* ds, tm, tens of time zone, units of time zone */
253
	                 /* 0xff is very likely to be "unknown" */
254
	  buf[2] = (3 << 6) | /* reserved -- always 1 */
255
		   ((tc.tm_mday / 10) << 4) | /* Tens of day */
256
		   (tc.tm_mday % 10);         /* Units of day */
257
	  buf[3] = /* we set high 4 bits to 0, shouldn't we set them to week? */
258
		   (tc.tm_mon % 10);         /* Units of month */
259
	  buf[4] = (((tc.tm_year % 100) / 10) << 4) | /* Tens of year */
260
		   (tc.tm_year % 10);                 /* Units of year */
261
          break;
262
    case dv_audio_rectime:  /* AAUX recording time */
263
    case dv_video_rectime:  /* VAUX recording time */
264
          ct = c->start_time + (time_t)(c->frames / 
265
	       ((float)c->sys->frame_rate / (float)c->sys->frame_rate_base));
266
          localtime_r(&ct, &tc);
267
	  buf[1] = (3 << 6) | /* reserved -- always 1 */
268
		   0x3f; /* tens of frame, units of frame: 0x3f - "unknown" ? */
269
	  buf[2] = (1 << 7) | /* reserved -- always 1 */ 
270
		   ((tc.tm_sec / 10) << 4) | /* Tens of seconds */
271
		   (tc.tm_sec % 10);         /* Units of seconds */
272
	  buf[3] = (1 << 7) | /* reserved -- always 1 */
273
		   ((tc.tm_min / 10) << 4) | /* Tens of minutes */
274
		   (tc.tm_min % 10);         /* Units of minutes */
275
	  buf[4] = (3 << 6) | /* reserved -- always 1 */ 
276
	           ((tc.tm_hour / 10) << 4) | /* Tens of hours */
277
		   (tc.tm_hour % 10);         /* Units of hours */
278
	  break;
279
    case dv_video_source:
280
	  buf[1] = 0xff; /* reserved -- always 1 */
281
	  buf[2] = (1 << 7) | /* B/W: 0 - b/w, 1 - color */
282
		   (1 << 6) | /* following CLF is valid - 0, invalid - 1 */
283
		   (3 << 4) | /* CLF: color frames id (see ITU-R BT.470-4) */
284
		   0xf; /* reserved -- always 1 */
285
	  buf[3] = (3 << 6) | /* reserved -- always 1 */
286
		   (c->sys->dsf << 5) | /*  system: 60fields/50fields */
287
		   0; /* signal type video compression */
288
	  buf[4] = 0xff; /* VISC: 0xff -- no information */
289
          break;
290
    case dv_video_control:
291
	  buf[1] = (0 << 6) | /* Copy generation management (CGMS) 0 -- free */
292
		   0x3f; /* reserved -- always 1 */
293
	  buf[2] = 0xc8 | /* reserved -- always b11001xxx */
294
		   c->aspect;
295
	  buf[3] = (1 << 7) | /* Frame/field flag 1 -- frame, 0 -- field */
296
		   (1 << 6) | /* First/second field flag 0 -- field 2, 1 -- field 1 */
297
		   (1 << 5) | /* Frame change flag 0 -- same picture as before, 1 -- different */
298
		   (1 << 4) | /* 1 - interlaced, 0 - noninterlaced */
299
		   0xc; /* reserved -- always b1100 */
300
	  buf[4] = 0xff; /* reserved -- always 1 */
301
          break;
302
    default:
303
          buf[1] = buf[2] = buf[3] = buf[4] = 0xff;
304
    }
305
    return 5;
306
}
45 307

  
46
    ast->codec.codec_type = CODEC_TYPE_AUDIO;
47
    ast->codec.codec_id = CODEC_ID_DVAUDIO;
48
    c->is_audio = 0;
308
static inline int dv_write_dif_id(enum dv_section_type t, uint8_t seq_num, 
309
                                  uint8_t dif_num, uint8_t* buf)
310
{
311
    buf[0] = (uint8_t)t;    /* Section type */
312
    buf[1] = (seq_num<<4) | /* DIF seq number 0-9 for 525/60; 0-11 for 625/50 */
313
	     (0 << 3) |     /* FSC: for 50Mb/s 0 - first channel; 1 - second */
314
	     7;             /* reserved -- always 1 */
315
    buf[2] = dif_num;       /* DIF block number Video: 0-134, Audio: 0-8 */
316
    return 3;
317
}
49 318

  
50
    return 0;
319
static inline int dv_write_ssyb_id(uint8_t syb_num, uint8_t fr, uint8_t* buf)
320
{
321
    if (syb_num == 0 || syb_num == 6) {
322
	buf[0] = (fr<<7) | /* FR ID 1 - first half of each channel; 0 - second */
323
		 (0<<4)  | /* AP3 (Subcode application ID) */
324
		 0x0f;     /* reserved -- always 1 */
325
    } 
326
    else if (syb_num == 11) {
327
	buf[0] = (fr<<7) | /* FR ID 1 - first half of each channel; 0 - second */
328
                 0x7f;     /* reserved -- always 1 */
329
    }
330
    else {
331
	buf[0] = (fr<<7) | /* FR ID 1 - first half of each channel; 0 - second */
332
                 (0<<4)  | /* APT (Track application ID) */
333
		 0x0f;     /* reserved -- always 1 */
334
    }
335
    buf[1] = 0xf0 |            /* reserved -- always 1 */
336
             (syb_num & 0x0f); /* SSYB number 0 - 11   */
337
    buf[2] = 0xff;             /* reserved -- always 1 */
338
    return 3;
339
}
340

  
341
static void dv_format_frame(DVMuxContext *c, uint8_t* buf)
342
{
343
    int i, j, k;
344
    
345
    for (i = 0; i < c->sys->difseg_size; i++) {
346
       memset(buf, 0xff, 80 * 6); /* First 6 DIF blocks are for control data */
347
       
348
       /* DV header: 1DIF */
349
       buf += dv_write_dif_id(dv_sect_header, i, 0, buf);
350
       buf += dv_write_pack((c->sys->dsf ? dv_header625 : dv_header525), c, buf);
351
       buf += 72; /* unused bytes */
352
				   
353
       /* DV subcode: 2DIFs */
354
       for (j = 0; j < 2; j++) {
355
          buf += dv_write_dif_id( dv_sect_subcode, i, j, buf);
356
	  for (k = 0; k < 6; k++) {
357
	     buf += dv_write_ssyb_id(k, (i < c->sys->difseg_size/2), buf);
358
	     buf += dv_write_pack(dv_ssyb_packs_dist[i][k], c, buf);
359
	  }
360
	  buf += 29; /* unused bytes */
361
       }
362
       
363
       /* DV VAUX: 3DIFs */
364
       for (j = 0; j < 3; j++) {
365
	  buf += dv_write_dif_id(dv_sect_vaux, i, j, buf);
366
	  for (k = 0; k < 15 ; k++)
367
	     buf += dv_write_pack(dv_vaux_packs_dist[i][k], c, buf);
368
	  buf += 2; /* unused bytes */
369
       } 
370
       
371
       /* DV Audio/Video: 135 Video DIFs + 9 Audio DIFs */
372
       for (j = 0; j < 135; j++) {
373
            if (j%15 == 0) {
374
	        buf += dv_write_dif_id(dv_sect_audio, i, j/15, buf);
375
	        buf += dv_write_pack(dv_aaux_packs_dist[i][j/15], c, buf);
376
		buf += 72; /* shuffled PCM audio */
377
	    }
378
	    buf += dv_write_dif_id(dv_sect_video, i, j, buf);
379
	    buf += 77; /* 1 video macro block: 1 bytes control
380
			                       4 * 14 bytes Y 8x8 data
381
			                           10 bytes Cr 8x8 data
382
						   10 bytes Cb 8x8 data */
383
       }
384
    }
385
}
386

  
387
static void dv_inject_audio(DVMuxContext *c, const uint8_t* pcm, uint8_t* frame_ptr)
388
{
389
    int i, j, d, of;
390
    for (i = 0; i < c->sys->difseg_size; i++) {
391
       frame_ptr += 6 * 80; /* skip DIF segment header */
392
       for (j = 0; j < 9; j++) {
393
          for (d = 8; d < 80; d+=2) {
394
	     of = c->sys->audio_shuffle[i][j] + (d - 8)/2 * c->sys->audio_stride;
395
	     frame_ptr[d] = pcm[of*2+1]; // FIXME: may be we have to admit
396
	     frame_ptr[d+1] = pcm[of*2]; //        that DV is a big endian PCM       
397
          }
398
          frame_ptr += 16 * 80; /* 15 Video DIFs + 1 Audio DIF */
399
       }
400
    }
401
}
402

  
403
static void dv_inject_video(DVMuxContext *c, const uint8_t* video_data, uint8_t* frame_ptr)
404
{
405
    int i, j;
406
    int ptr = 0;
407

  
408
    for (i = 0; i < c->sys->difseg_size; i++) {
409
       ptr += 6 * 80; /* skip DIF segment header */
410
       for (j = 0; j < 135; j++) {
411
            if (j%15 == 0)
412
	        ptr += 80; /* skip Audio DIF */
413
	    ptr += 3;
414
	    memcpy(frame_ptr + ptr, video_data + ptr, 77);
415
	    ptr += 77;
416
       }
417
    }
418
}
419

  
420
/* 
421
 * This is the dumbest implementation of all -- it simply looks at
422
 * a fixed offset and if pack isn't there -- fails. We might want
423
 * to have a fallback mechanism for complete search of missing packs.
424
 */
425
static const uint8_t* dv_extract_pack(uint8_t* frame, enum dv_pack_type t)
426
{
427
    int offs;
428
    
429
    switch (t) {
430
    case dv_audio_source:
431
          offs = (80*6 + 80*16*3 + 3);
432
	  break;
433
    case dv_audio_control:
434
          offs = (80*6 + 80*16*4 + 3);
435
	  break;
436
    case dv_video_control:
437
          offs = (80*5 + 48 + 5);
438
          break;
439
    default:
440
          return NULL;
441
    }   
442

  
443
    return (frame[offs] == t ? &frame[offs] : NULL);
444
}
445

  
446
/* 
447
 * There's a couple of assumptions being made here:
448
 * 1. By default we silence erroneous (0x8000/16bit 0x800/12bit) audio samples.
449
 *    We can pass them upwards when ffmpeg will be ready to deal with them.
450
 * 2. We don't do software emphasis.
451
 * 3. Audio is always returned as 16bit linear samples: 12bit nonlinear samples
452
 *    are converted into 16bit linear ones.
453
 */
454
static int dv_extract_audio(uint8_t* frame, uint8_t* pcm)
455
{
456
    int size, i, j, d, of, smpls, freq, quant;
457
    uint16_t lc, rc;
458
    const DVprofile* sys;
459
    const uint8_t* as_pack;
460
    
461
    as_pack = dv_extract_pack(frame, dv_audio_source);
462
    if (!as_pack)    /* No audio ? */
463
	return 0;
464
   
465
    sys = dv_frame_profile(frame);
466
    smpls = as_pack[1] & 0x3f; /* samples in this frame - min. samples */
467
    freq = (as_pack[4] >> 3) & 0x07; /* 0 - 48KHz, 1 - 44,1kHz, 2 - 32 kHz */
468
    quant = as_pack[4] & 0x07; /* 0 - 16bit linear, 1 - 12bit nonlinear */
469
    
470
    if (quant > 1)
471
	return -1; /* Unsupported quantization */
472

  
473
    size = (sys->audio_min_samples[freq] + smpls) * 4; /* 2ch, 2bytes */
474

  
475
    /* for each DIF segment */
476
    for (i = 0; i < sys->difseg_size; i++) {
477
       frame += 6 * 80; /* skip DIF segment header */
478
       for (j = 0; j < 9; j++) {
479
          for (d = 8; d < 80; d += 2) {
480
	     if (quant == 0) {  /* 16bit quantization */
481
		 of = sys->audio_shuffle[i][j] + (d - 8)/2 * sys->audio_stride;
482
                 if (of*2 >= size)
483
		     continue;
484
		 
485
		 pcm[of*2] = frame[d+1]; // FIXME: may be we have to admit
486
		 pcm[of*2+1] = frame[d]; //        that DV is a big endian PCM
487
		 if (pcm[of*2+1] == 0x80 && pcm[of*2] == 0x00)
488
		     pcm[of*2+1] = 0;
489
	      } else {           /* 12bit quantization */
490
		 if (i >= sys->difseg_size/2)
491
	             goto out;  /* We're not doing 4ch at this time */
492
		       
493
		 lc = ((uint16_t)frame[d] << 4) | 
494
		      ((uint16_t)frame[d+2] >> 4);
495
		 rc = ((uint16_t)frame[d+1] << 4) |
496
	              ((uint16_t)frame[d+2] & 0x0f);
497
		 lc = (lc == 0x800 ? 0 : dv_audio_12to16(lc));
498
		 rc = (rc == 0x800 ? 0 : dv_audio_12to16(rc));
499

  
500
		 of = sys->audio_shuffle[i][j] + (d - 8)/3 * sys->audio_stride;
501
                 if (of*2 >= size)
502
		     continue;
503

  
504
		 pcm[of*2] = lc & 0xff; // FIXME: may be we have to admit
505
		 pcm[of*2+1] = lc >> 8; //        that DV is a big endian PCM
506
		 of = sys->audio_shuffle[i+sys->difseg_size/2][j] + 
507
		      (d - 8)/3 * sys->audio_stride;
508
		 pcm[of*2] = rc & 0xff; // FIXME: may be we have to admit
509
		 pcm[of*2+1] = rc >> 8; //        that DV is a big endian PCM
510
		 ++d;
511
	      }
512
	  }
513
		
514
	  frame += 16 * 80; /* 15 Video DIFs + 1 Audio DIF */
515
        }
516
    }
517

  
518
out:
519
    return size;
520
}
521

  
522
static int dv_extract_audio_info(uint8_t* frame, AVCodecContext* avctx)
523
{
524
    const uint8_t* as_pack;
525
    const DVprofile* sys;
526
    int freq, smpls;
527

  
528
    sys = dv_frame_profile(frame);
529
    as_pack = dv_extract_pack(frame, dv_audio_source);
530
    if (!as_pack || !sys)    /* No audio ? */
531
	return -1;
532
    
533
    smpls = as_pack[1] & 0x3f; /* samples in this frame - min. samples */
534
    freq = (as_pack[4] >> 3) & 0x07; /* 0 - 48KHz, 1 - 44,1kHz, 2 - 32 kHz */
535
   
536
    avctx->sample_rate = dv_audio_frequency[freq];
537
    avctx->channels = 2;
538
    avctx->bit_rate = avctx->channels * avctx->sample_rate * 16;
539

  
540
    return (sys->audio_min_samples[freq] + smpls) * 4; /* 2ch, 2bytes */;
541
}
542

  
543
static int dv_extract_video_info(uint8_t* frame, AVCodecContext* avctx)
544
{
545
    const DVprofile* sys; 
546
    const uint8_t* vsc_pack;
547
    int apt;
548
    int size = 0;
549
    
550
    sys = dv_frame_profile(frame);
551
    if (sys) {
552
        apt = frame[4] & 0x07;
553
        avctx->frame_rate = sys->frame_rate;
554
        avctx->frame_rate_base = sys->frame_rate_base;
555
        avctx->width = sys->width;
556
        avctx->height = sys->height;
557
        avctx->pix_fmt = sys->pix_fmt;
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff