Changeset 070b6a3abf9a0e47d8510fee0ab2b76e2e22476b

Show
Ignore:
Timestamp:
29/02/04 20:01:22 (5 years ago)
Author:
Gildas Bazin <gbazin@videolan.org>
git-committer:
Gildas Bazin <gbazin@videolan.org> 1078081282 +0000
git-parent:

[23c72b3d07e97587413e3d1b34bda1835f7f0938]

git-author:
Gildas Bazin <gbazin@videolan.org> 1078081282 +0000
Message:

* modules/demux/rawdv.c: DV audio support was removed from libavcodec so reimplemented it here.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • modules/demux/rawdv.c

    rffdca9a r070b6a3  
    33 ***************************************************************************** 
    44 * Copyright (C) 2001-2003 VideoLAN 
    5  * $Id: rawdv.c,v 1.14 2004/01/25 20:05:28 hartman Exp $ 
     5 * $Id: rawdv.c,v 1.15 2004/02/29 19:01:22 gbazin Exp $ 
    66 * 
    77 * Authors: Gildas Bazin <gbazin@netcourrier.com> 
     
    3636static void Close( vlc_object_t * ); 
    3737 
     38static block_t *dv_extract_audio( input_thread_t * p_input, 
     39                                  block_t* p_frame_block ); 
     40 
    3841vlc_module_begin(); 
    3942    set_description( _("raw dv demuxer") ); 
     
    9699    es_format_t  fmt_audio; 
    97100 
     101    int    i_dsf; 
    98102    double f_rate; 
    99103    int    i_bitrate; 
     
    136140    } 
    137141 
    138     if( stream_Peek( p_input->s, &p_peek, DV_PAL_FRAME_SIZE ) < DV_NTSC_FRAME_SIZE ) 
     142    if( stream_Peek( p_input->s, &p_peek, DV_PAL_FRAME_SIZE ) < 
     143        DV_NTSC_FRAME_SIZE ) 
    139144    { 
    140145        /* Stream too short ... */ 
     
    194199    p_input->p_demux_data = p_sys = malloc( sizeof( demux_sys_t ) ); 
    195200 
     201    p_sys->i_dsf = dv_header.dsf; 
    196202    p_sys->frame_size = dv_header.dsf ? 12 * 150 * 80 : 10 * 150 * 80; 
    197203    p_sys->f_rate = dv_header.dsf ? 25 : 29.97; 
     
    203209    p_sys->i_bitrate = 0; 
    204210 
    205     es_format_Init( &p_sys->fmt_video, VIDEO_ES, VLC_FOURCC( 'd','v','s','d' ) ); 
     211    es_format_Init( &p_sys->fmt_video, VIDEO_ES, VLC_FOURCC('d','v','s','d') ); 
    206212    p_sys->fmt_video.video.i_width = 720; 
    207213    p_sys->fmt_video.video.i_height= dv_header.dsf ? 576 : 480;; 
    208  
    209     /* Audio stuff */ 
    210 #if 0 
    211     p_peek = p_peek_backup + 80*6+80*16*3 + 3; /* beginning of AAUX pack */ 
    212  
    213     if( *p_peek != 0x50 || *p_peek != 0x51 ) 
    214     { 
    215         msg_Err( p_input, "AAUX should begin with 0x50" ); 
    216     } 
    217 #endif 
    218  
    219     es_format_Init( &p_sys->fmt_audio, AUDIO_ES, VLC_FOURCC( 'd','v','a','u' ) ); 
    220     p_sys->fmt_audio.audio.i_channels = 2; 
    221     p_sys->fmt_audio.audio.i_rate = 44100;  /* FIXME */ 
    222     p_sys->fmt_audio.audio.i_bitspersample = 16; 
    223     p_sys->fmt_audio.audio.i_blockalign = p_sys->frame_size;    /* ??? */ 
    224     p_sys->fmt_audio.i_bitrate = p_sys->f_rate * p_sys->frame_size; /* ??? */ 
    225214 
    226215    /* necessary because input_SplitBuffer() will only get 
     
    240229 
    241230    p_sys->p_es_video = es_out_Add( p_input->p_es_out, &p_sys->fmt_video ); 
    242     p_sys->p_es_audio = es_out_Add( p_input->p_es_out, &p_sys->fmt_audio ); 
     231 
     232    /* Audio stuff */ 
     233    p_peek = p_peek_backup + 80*6+80*16*3 + 3; /* beginning of AAUX pack */ 
     234    if( *p_peek == 0x50 ) 
     235    { 
     236        es_format_Init( &p_sys->fmt_audio, AUDIO_ES, 
     237                        VLC_FOURCC('a','r','a','w') ); 
     238 
     239        p_sys->fmt_audio.audio.i_channels = 2; 
     240        switch( (p_peek[4] >> 3) & 0x07 ) 
     241        { 
     242        case 0: 
     243            p_sys->fmt_audio.audio.i_rate = 48000; 
     244            break; 
     245        case 1: 
     246            p_sys->fmt_audio.audio.i_rate = 44100; 
     247            break; 
     248        case 2: 
     249        default: 
     250            p_sys->fmt_audio.audio.i_rate = 32000; 
     251            break; 
     252        } 
     253 
     254        /* 12 bits non-linear will be converted to 16 bits linear */ 
     255        p_sys->fmt_audio.audio.i_bitspersample = 16; 
     256 
     257        p_sys->p_es_audio = es_out_Add( p_input->p_es_out, &p_sys->fmt_audio ); 
     258    } 
    243259 
    244260    return VLC_SUCCESS; 
     
    265281    demux_sys_t    *p_sys = p_input->p_demux_data; 
    266282    block_t        *p_block; 
    267     vlc_bool_t     b_audio, b_video; 
    268  
     283    vlc_bool_t     b_audio = VLC_FALSE, b_video = VLC_FALSE; 
    269284 
    270285    if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT ) 
     
    299314 
    300315    es_out_Control( p_input->p_es_out, ES_OUT_GET_ES_STATE, 
    301                     p_sys->p_es_audio, &b_audio ); 
    302     es_out_Control( p_input->p_es_out, ES_OUT_GET_ES_STATE, 
    303316                    p_sys->p_es_video, &b_video ); 
     317 
     318    if( p_sys->p_es_audio ) 
     319    { 
     320        es_out_Control( p_input->p_es_out, ES_OUT_GET_ES_STATE, 
     321                        p_sys->p_es_audio, &b_audio ); 
     322    } 
    304323 
    305324    p_block->i_dts = 
     
    307326                                       p_input->stream.p_selected_program, 
    308327                                       p_sys->i_pcr ); 
    309     if( b_audio && b_video ) 
    310     { 
    311         block_t *p_dup = block_Duplicate( p_block ); 
    312  
     328    if( b_audio ) 
     329    { 
     330        block_t *p_audio_block = dv_extract_audio( p_input, p_block ); 
     331        if( p_audio_block ) 
     332        { 
     333            p_audio_block->i_pts = p_audio_block->i_dts = p_block->i_dts; 
     334            es_out_Send( p_input->p_es_out, p_sys->p_es_audio, p_audio_block ); 
     335        } 
     336    } 
     337 
     338    if( b_video ) 
    313339        es_out_Send( p_input->p_es_out, p_sys->p_es_video, p_block ); 
    314         if( p_dup ) 
    315         { 
    316             es_out_Send( p_input->p_es_out, p_sys->p_es_video, p_dup ); 
    317         } 
    318     } 
    319     else if( b_audio ) 
    320     { 
    321         es_out_Send( p_input->p_es_out, p_sys->p_es_audio, p_block ); 
    322     } 
    323     else if( b_video ) 
    324     { 
    325         es_out_Send( p_input->p_es_out, p_sys->p_es_video, p_block ); 
    326     } 
    327340    else 
    328     { 
    329341        block_Release( p_block ); 
    330     } 
    331342 
    332343    p_sys->i_pcr += ( 90000 / p_sys->f_rate ); 
     
    335346} 
    336347 
     348static const uint16_t dv_audio_shuffle525[10][9] = { 
     349  {  0, 30, 60, 20, 50, 80, 10, 40, 70 }, /* 1st channel */ 
     350  {  6, 36, 66, 26, 56, 86, 16, 46, 76 }, 
     351  { 12, 42, 72,  2, 32, 62, 22, 52, 82 }, 
     352  { 18, 48, 78,  8, 38, 68, 28, 58, 88 }, 
     353  { 24, 54, 84, 14, 44, 74,  4, 34, 64 }, 
     354   
     355  {  1, 31, 61, 21, 51, 81, 11, 41, 71 }, /* 2nd channel */ 
     356  {  7, 37, 67, 27, 57, 87, 17, 47, 77 }, 
     357  { 13, 43, 73,  3, 33, 63, 23, 53, 83 }, 
     358  { 19, 49, 79,  9, 39, 69, 29, 59, 89 }, 
     359  { 25, 55, 85, 15, 45, 75,  5, 35, 65 }, 
     360}; 
     361 
     362static const uint16_t dv_audio_shuffle625[12][9] = { 
     363  {   0,  36,  72,  26,  62,  98,  16,  52,  88}, /* 1st channel */ 
     364  {   6,  42,  78,  32,  68, 104,  22,  58,  94}, 
     365  {  12,  48,  84,   2,  38,  74,  28,  64, 100}, 
     366  {  18,  54,  90,   8,  44,  80,  34,  70, 106}, 
     367  {  24,  60,  96,  14,  50,  86,   4,  40,  76},   
     368  {  30,  66, 102,  20,  56,  92,  10,  46,  82}, 
     369     
     370  {   1,  37,  73,  27,  63,  99,  17,  53,  89}, /* 2nd channel */ 
     371  {   7,  43,  79,  33,  69, 105,  23,  59,  95}, 
     372  {  13,  49,  85,   3,  39,  75,  29,  65, 101}, 
     373  {  19,  55,  91,   9,  45,  81,  35,  71, 107}, 
     374  {  25,  61,  97,  15,  51,  87,   5,  41,  77},   
     375  {  31,  67, 103,  21,  57,  93,  11,  47,  83}, 
     376}; 
     377 
     378static inline uint16_t dv_audio_12to16( uint16_t sample ) 
     379{ 
     380    uint16_t shift, result; 
     381     
     382    sample = (sample < 0x800) ? sample : sample | 0xf000; 
     383    shift = (sample & 0xf00) >> 8; 
     384 
     385    if (shift < 0x2 || shift > 0xd) { 
     386        result = sample; 
     387    } else if (shift < 0x8) { 
     388        shift--; 
     389        result = (sample - (256 * shift)) << shift; 
     390    } else { 
     391        shift = 0xe - shift; 
     392        result = ((sample + ((256 * shift) + 1)) << shift) - 1; 
     393    } 
     394 
     395    return result; 
     396} 
     397 
     398static block_t *dv_extract_audio( input_thread_t * p_input, 
     399                                  block_t* p_frame_block ) 
     400{ 
     401    demux_sys_t *p_sys = p_input->p_demux_data; 
     402    block_t *p_block; 
     403    uint8_t *p_frame, *p_buf; 
     404    int i_audio_quant, i_samples, i_size, i_half_ch; 
     405    const uint16_t (*audio_shuffle)[9]; 
     406    int i, j, d, of; 
     407    uint16_t lc; 
     408 
     409    /* Beginning of AAUX pack */ 
     410    p_buf = p_frame_block->p_buffer + 80*6+80*16*3 + 3; 
     411    if( *p_buf != 0x50 ) return NULL; 
     412 
     413    i_audio_quant = p_buf[4] & 0x07; /* 0 - 16bit, 1 - 12bit */ 
     414    if( i_audio_quant > 1 ) 
     415    { 
     416        msg_Dbg( p_input, "Unsupported quantization for DV audio"); 
     417        return NULL; 
     418    } 
     419 
     420    i_samples = p_buf[1] & 0x3f; /* samples in this frame - min samples */ 
     421    switch( (p_buf[4] >> 3) & 0x07 ) 
     422    { 
     423    case 0: 
     424        i_size = p_sys->i_dsf ? 1896 : 1580; 
     425        break; 
     426    case 1: 
     427        i_size = p_sys->i_dsf ? 1742 : 1452; 
     428        break; 
     429    case 2: 
     430    default: 
     431        i_size = p_sys->i_dsf ? 1264 : 1053; 
     432        break; 
     433    } 
     434    i_size = (i_size + i_samples) * 4; /* 2ch, 2bytes */ 
     435 
     436    p_block = block_New( p_input, i_size ); 
     437 
     438    /* for each DIF segment */ 
     439    p_frame = p_frame_block->p_buffer; 
     440    audio_shuffle = p_sys->i_dsf ? dv_audio_shuffle625 : dv_audio_shuffle525; 
     441    i_half_ch = (p_sys->i_dsf ? 12 : 10)/2; 
     442    for( i = 0; i < (p_sys->i_dsf ? 12 : 10); i++ ) 
     443    { 
     444        p_frame += 6 * 80; /* skip DIF segment header */ 
     445 
     446        if( i_audio_quant == 1 && i == i_half_ch ) break; 
     447 
     448        for( j = 0; j < 9; j++ ) 
     449        { 
     450            for( d = 8; d < 80; d += 2 ) 
     451            { 
     452                if( i_audio_quant == 0 ) 
     453                { 
     454                    /* 16bit quantization */ 
     455                    of = audio_shuffle[i][j] + (d - 8) / 2 * 
     456                           (p_sys->i_dsf ? 108 : 90); 
     457 
     458                    if( of * 2 >= i_size ) continue; 
     459 
     460                    /* big endian */ 
     461                    p_block->p_buffer[of*2] = p_frame[d+1]; 
     462                    p_block->p_buffer[of*2+1] = p_frame[d]; 
     463 
     464                    if( p_block->p_buffer[of*2+1] == 0x80 && 
     465                        p_block->p_buffer[of*2] == 0x00 ) 
     466                        p_block->p_buffer[of*2+1] = 0; 
     467                } 
     468                else 
     469                { 
     470                    /* 12bit quantization */ 
     471                    lc = ((uint16_t)p_frame[d] << 4) |  
     472                         ((uint16_t)p_frame[d+2] >> 4); 
     473                    lc = (lc == 0x800 ? 0 : dv_audio_12to16(lc)); 
     474 
     475                    of = audio_shuffle[i][j] + (d - 8) / 3 * 
     476                         (p_sys->i_dsf ? 108 : 90); 
     477                    if( of*2 >= i_size ) continue; 
     478 
     479                    /* big endian */ 
     480                    p_block->p_buffer[of*2] = lc & 0xff; 
     481                    p_block->p_buffer[of*2+1] = lc >> 8; 
     482                    ++d; 
     483                } 
     484            } 
     485 
     486            p_frame += 16 * 80; /* 15 Video DIFs + 1 Audio DIF */ 
     487        } 
     488    } 
     489 
     490    return p_block; 
     491}