lpcm.c
上传用户:kjfoods
上传日期:2020-07-06
资源大小:29949k
文件大小:17k
源码类别:

midi

开发平台:

Unix_Linux

  1. /*****************************************************************************
  2.  * lpcm.c: lpcm decoder/packetizer module
  3.  *****************************************************************************
  4.  * Copyright (C) 1999-2008 the VideoLAN team
  5.  * $Id: 52adfc9a923ece7ba17e6d8ebba24e04265ba748 $
  6.  *
  7.  * Authors: Samuel Hocevar <sam@zoy.org>
  8.  *          Henri Fallon <henri@videolan.org>
  9.  *          Christophe Massiot <massiot@via.ecp.fr>
  10.  *          Gildas Bazin <gbazin@videolan.org>
  11.  *          Lauren Aimar <fenrir _AT_ videolan _DOT_ org >
  12.  *
  13.  * This program is free software; you can redistribute it and/or modify
  14.  * it under the terms of the GNU General Public License as published by
  15.  * the Free Software Foundation; either version 2 of the License, or
  16.  * (at your option) any later version.
  17.  *
  18.  * This program is distributed in the hope that it will be useful,
  19.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21.  * GNU General Public License for more details.
  22.  *
  23.  * You should have received a copy of the GNU General Public License
  24.  * along with this program; if not, write to the Free Software
  25.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  26.  *****************************************************************************/
  27. /*****************************************************************************
  28.  * Preamble
  29.  *****************************************************************************/
  30. #ifdef HAVE_CONFIG_H
  31. # include "config.h"
  32. #endif
  33. #include <vlc_common.h>
  34. #include <vlc_plugin.h>
  35. #include <vlc_codec.h>
  36. #include <vlc_aout.h>
  37. #include <assert.h>
  38. /*****************************************************************************
  39.  * Module descriptor
  40.  *****************************************************************************/
  41. static int  OpenDecoder   ( vlc_object_t * );
  42. static int  OpenPacketizer( vlc_object_t * );
  43. static void CloseCommon   ( vlc_object_t * );
  44. vlc_module_begin ()
  45.     set_category( CAT_INPUT )
  46.     set_subcategory( SUBCAT_INPUT_ACODEC )
  47.     set_description( N_("Linear PCM audio decoder") )
  48.     set_capability( "decoder", 100 )
  49.     set_callbacks( OpenDecoder, CloseCommon )
  50.     add_submodule ()
  51.     set_description( N_("Linear PCM audio packetizer") )
  52.     set_capability( "packetizer", 100 )
  53.     set_callbacks( OpenPacketizer, CloseCommon )
  54. vlc_module_end ()
  55. /*****************************************************************************
  56.  * decoder_sys_t : lpcm decoder descriptor
  57.  *****************************************************************************/
  58. struct decoder_sys_t
  59. {
  60.     /* Module mode */
  61.     bool b_packetizer;
  62.     /*
  63.      * Output properties
  64.      */
  65.     audio_date_t end_date;
  66.     /* */
  67.     unsigned i_header_size;
  68.     bool b_dvd;
  69. };
  70. /*
  71.  * LPCM DVD header :
  72.  * - frame number (8 bits)
  73.  * - unknown (16 bits) == 0x0003 ?
  74.  * - unknown (4 bits)
  75.  * - current frame (4 bits)
  76.  * - unknown (2 bits)
  77.  * - frequency (2 bits) 0 == 48 kHz, 1 == 32 kHz, 2 == ?, 3 == ?
  78.  * - unknown (1 bit)
  79.  * - number of channels - 1 (3 bits) 1 == 2 channels
  80.  * - start code (8 bits) == 0x80
  81.  *
  82.  * LPCM BD header :
  83.  * - unkown (16 bits)
  84.  * - number of channels (4 bits)
  85.  * - frequency (4 bits)
  86.  * - bits per sample (2 bits)
  87.  * - unknown (6 bits)
  88.  */
  89. #define LPCM_DVD_HEADER_LEN (6)
  90. #define LPCM_BD_HEADER_LEN (4)
  91. /*****************************************************************************
  92.  * Local prototypes
  93.  *****************************************************************************/
  94. static void *DecodeFrame  ( decoder_t *, block_t ** );
  95. static int DvdHeader( unsigned *pi_rate,
  96.                       unsigned *pi_channels, unsigned *pi_original_channels,
  97.                       unsigned *pi_bits,
  98.                       const uint8_t *p_header );
  99. static void DvdExtract( aout_buffer_t *, block_t *, unsigned i_bits );
  100. static int BdHeader( unsigned *pi_rate,
  101.                      unsigned *pi_channels, unsigned *pi_original_channels,
  102.                      unsigned *pi_bits,
  103.                      const uint8_t *p_header );
  104. static void BdExtract( aout_buffer_t *, block_t * );
  105. /*****************************************************************************
  106.  * OpenCommon:
  107.  *****************************************************************************/
  108. static int OpenCommon( vlc_object_t *p_this, bool b_packetizer )
  109. {
  110.     decoder_t *p_dec = (decoder_t*)p_this;
  111.     decoder_sys_t *p_sys;
  112.     bool b_dvd;
  113.     switch( p_dec->fmt_in.i_codec )
  114.     {
  115.     /* DVD LPCM */
  116.     case VLC_FOURCC('l','p','c','m'):
  117.     case VLC_FOURCC('l','p','c','b'):
  118.         b_dvd = true;
  119.         break;
  120.     /* BD LPCM */
  121.     case VLC_FOURCC('b','p','c','m'):
  122.         b_dvd = false;
  123.         break;
  124.     default:
  125.         return VLC_EGENERIC;
  126.     }
  127.     /* Allocate the memory needed to store the decoder's structure */
  128.     if( ( p_dec->p_sys = p_sys = malloc(sizeof(decoder_sys_t)) ) == NULL )
  129.         return VLC_ENOMEM;
  130.     /* Misc init */
  131.     p_sys->b_packetizer = b_packetizer;
  132.     aout_DateSet( &p_sys->end_date, 0 );
  133.     p_sys->i_header_size = b_dvd ? LPCM_DVD_HEADER_LEN : LPCM_BD_HEADER_LEN;
  134.     p_sys->b_dvd = b_dvd;
  135.     /* Set output properties */
  136.     p_dec->fmt_out.i_cat = AUDIO_ES;
  137.     if( b_packetizer )
  138.     {
  139.         p_dec->fmt_out.i_codec = b_dvd ? VLC_FOURCC('l','p','c','m') : VLC_FOURCC('b','p','c','m');
  140.     }
  141.     else
  142.     {
  143.         switch( p_dec->fmt_out.audio.i_bitspersample )
  144.         {
  145.         case 24:
  146.         case 20:
  147.             p_dec->fmt_out.i_codec = VLC_FOURCC('s','2','4','b');
  148.             p_dec->fmt_out.audio.i_bitspersample = 24;
  149.             break;
  150.         default:
  151.             p_dec->fmt_out.i_codec = VLC_FOURCC('s','1','6','b');
  152.             p_dec->fmt_out.audio.i_bitspersample = 16;
  153.             break;
  154.         }
  155.     }
  156.     /* Set callback */
  157.     p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
  158.         DecodeFrame;
  159.     p_dec->pf_packetize    = (block_t *(*)(decoder_t *, block_t **))
  160.         DecodeFrame;
  161.     return VLC_SUCCESS;
  162. }
  163. static int OpenDecoder( vlc_object_t *p_this )
  164. {
  165.     return OpenCommon( p_this, false );
  166. }
  167. static int OpenPacketizer( vlc_object_t *p_this )
  168. {
  169.     return OpenCommon( p_this, true );
  170. }
  171. /*****************************************************************************
  172.  * DecodeFrame: decodes an lpcm frame.
  173.  ****************************************************************************
  174.  * Beware, this function must be fed with complete frames (PES packet).
  175.  *****************************************************************************/
  176. static void *DecodeFrame( decoder_t *p_dec, block_t **pp_block )
  177. {
  178.     decoder_sys_t *p_sys = p_dec->p_sys;
  179.     block_t       *p_block;
  180.     unsigned int  i_rate = 0, i_original_channels = 0, i_channels = 0, i_bits = 0;
  181.     int           i_frame_length;
  182.     if( !pp_block || !*pp_block ) return NULL;
  183.     p_block = *pp_block;
  184.     *pp_block = NULL; /* So the packet doesn't get re-sent */
  185.     /* Date management */
  186.     if( p_block->i_pts > 0 &&
  187.         p_block->i_pts != aout_DateGet( &p_sys->end_date ) )
  188.     {
  189.         aout_DateSet( &p_sys->end_date, p_block->i_pts );
  190.     }
  191.     if( !aout_DateGet( &p_sys->end_date ) )
  192.     {
  193.         /* We've just started the stream, wait for the first PTS. */
  194.         block_Release( p_block );
  195.         return NULL;
  196.     }
  197.     if( p_block->i_buffer <= p_sys->i_header_size )
  198.     {
  199.         msg_Err(p_dec, "frame is too short");
  200.         block_Release( p_block );
  201.         return NULL;
  202.     }
  203.     int i_ret;
  204.     if( p_sys->b_dvd )
  205.         i_ret = DvdHeader( &i_rate, &i_channels, &i_original_channels, &i_bits,
  206.                            p_block->p_buffer );
  207.     else
  208.         i_ret = BdHeader( &i_rate, &i_channels, &i_original_channels, &i_bits,
  209.                           p_block->p_buffer );
  210.     if( i_ret )
  211.     {
  212.         msg_Warn( p_dec, "no frame sync" );
  213.         block_Release( p_block );
  214.         return NULL;
  215.     }
  216.     /* Set output properties */
  217.     if( p_dec->fmt_out.audio.i_rate != i_rate )
  218.     {
  219.         aout_DateInit( &p_sys->end_date, i_rate );
  220.         aout_DateSet( &p_sys->end_date, p_block->i_pts );
  221.     }
  222.     p_dec->fmt_out.audio.i_rate = i_rate;
  223.     p_dec->fmt_out.audio.i_channels = i_channels;
  224.     p_dec->fmt_out.audio.i_original_channels = i_original_channels;
  225.     p_dec->fmt_out.audio.i_physical_channels = i_original_channels & AOUT_CHAN_PHYSMASK;
  226.     i_frame_length = (p_block->i_buffer - p_sys->i_header_size) / i_channels * 8 / i_bits;
  227.     if( p_sys->b_packetizer )
  228.     {
  229.         p_block->i_pts = p_block->i_dts = aout_DateGet( &p_sys->end_date );
  230.         p_block->i_length =
  231.             aout_DateIncrement( &p_sys->end_date, i_frame_length ) -
  232.             p_block->i_pts;
  233.         /* Just pass on the incoming frame */
  234.         return p_block;
  235.     }
  236.     else
  237.     {
  238.         /* */
  239.         if( i_bits == 16 )
  240.         {
  241.             p_dec->fmt_out.i_codec = VLC_FOURCC('s','1','6','b');
  242.             p_dec->fmt_out.audio.i_bitspersample = 16;
  243.         }
  244.         else
  245.         {
  246.             p_dec->fmt_out.i_codec = VLC_FOURCC('s','2','4','b');
  247.             p_dec->fmt_out.audio.i_bitspersample = 24;
  248.         }
  249.         /* */
  250.         aout_buffer_t *p_aout_buffer;
  251.         p_aout_buffer = decoder_NewAudioBuffer( p_dec, i_frame_length );
  252.         if( !p_aout_buffer )
  253.             return NULL;
  254.         p_aout_buffer->start_date = aout_DateGet( &p_sys->end_date );
  255.         p_aout_buffer->end_date =
  256.             aout_DateIncrement( &p_sys->end_date, i_frame_length );
  257.         p_block->p_buffer += p_sys->i_header_size;
  258.         p_block->i_buffer -= p_sys->i_header_size;
  259.         if( p_sys->b_dvd )
  260.             DvdExtract( p_aout_buffer, p_block, i_bits );
  261.         else
  262.             BdExtract( p_aout_buffer, p_block );
  263.         block_Release( p_block );
  264.         return p_aout_buffer;
  265.     }
  266. }
  267. /*****************************************************************************
  268.  * CloseCommon : lpcm decoder destruction
  269.  *****************************************************************************/
  270. static void CloseCommon( vlc_object_t *p_this )
  271. {
  272.     decoder_t *p_dec = (decoder_t*)p_this;
  273.     free( p_dec->p_sys );
  274. }
  275. /*****************************************************************************
  276.  *
  277.  *****************************************************************************/
  278. static int DvdHeader( unsigned *pi_rate,
  279.                       unsigned *pi_channels, unsigned *pi_original_channels,
  280.                       unsigned *pi_bits,
  281.                       const uint8_t *p_header )
  282. {
  283.     const uint8_t i_header = p_header[4];
  284.     switch( (i_header >> 4) & 0x3 )
  285.     {
  286.     case 0:
  287.         *pi_rate = 48000;
  288.         break;
  289.     case 1:
  290.         *pi_rate = 96000;
  291.         break;
  292.     case 2:
  293.         *pi_rate = 44100;
  294.         break;
  295.     case 3:
  296.         *pi_rate = 32000;
  297.         break;
  298.     }
  299.     *pi_channels = (i_header & 0x7) + 1;
  300.     switch( *pi_channels - 1 )
  301.     {
  302.     case 0:
  303.         *pi_original_channels = AOUT_CHAN_CENTER;
  304.         break;
  305.     case 1:
  306.         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
  307.         break;
  308.     case 2:
  309.         /* This is unsure. */
  310.         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_LFE;
  311.         break;
  312.     case 3:
  313.         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
  314.                                | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
  315.         break;
  316.     case 4:
  317.         /* This is unsure. */
  318.         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
  319.                                | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
  320.                                | AOUT_CHAN_LFE;
  321.         break;
  322.     case 5:
  323.         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
  324.                                | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
  325.                                | AOUT_CHAN_CENTER | AOUT_CHAN_LFE;
  326.         break;
  327.     case 6:
  328.         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
  329.                                | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
  330.                                | AOUT_CHAN_CENTER | AOUT_CHAN_MIDDLELEFT
  331.                                | AOUT_CHAN_MIDDLERIGHT;
  332.         break;
  333.     case 7:
  334.         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
  335.                                | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
  336.                                | AOUT_CHAN_CENTER | AOUT_CHAN_MIDDLELEFT
  337.                                | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE;
  338.         break;
  339.     }
  340.     switch( (i_header >> 6) & 0x3 )
  341.     {
  342.     case 2:
  343.         *pi_bits = 24;
  344.         break;
  345.     case 1:
  346.         *pi_bits = 20;
  347.         break;
  348.     case 0:
  349.     default:
  350.         *pi_bits = 16;
  351.         break;
  352.     }
  353.     /* Check frame sync and drop it. */
  354.     if( p_header[5] != 0x80 )
  355.         return -1;
  356.     return 0;
  357. }
  358. static int BdHeader( unsigned *pi_rate,
  359.                      unsigned *pi_channels, unsigned *pi_original_channels,
  360.                      unsigned *pi_bits,
  361.                      const uint8_t *p_header )
  362. {
  363.     const uint32_t h = GetDWBE( p_header );
  364.     switch( ( h & 0xf000) >> 12 )
  365.     {
  366.     case 1:
  367.         *pi_channels = 1;
  368.         *pi_original_channels = AOUT_CHAN_CENTER;
  369.         break;
  370.     case 3:
  371.         *pi_channels = 2;
  372.         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
  373.         break;
  374.     case 4:
  375.         *pi_channels = 3;
  376.         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER;
  377.         break;
  378.     case 5:
  379.         *pi_channels = 3;
  380.         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER;
  381.         break;
  382.     case 6:
  383.         *pi_channels = 4;
  384.         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
  385.                                 AOUT_CHAN_REARCENTER;
  386.         break;
  387.     case 7:
  388.         *pi_channels = 4;
  389.         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
  390.                                 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
  391.         break;
  392.     case 8:
  393.         *pi_channels = 5;
  394.         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
  395.                                 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
  396.         break;
  397.     case 9:
  398.         *pi_channels = 6;
  399.         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
  400.                                 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT |
  401.                                 AOUT_CHAN_LFE;
  402.         break;
  403.     case 10:
  404.         *pi_channels = 7;
  405.         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
  406.                                 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT |
  407.                                 AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT;
  408.         break;
  409.     case 11:
  410.         *pi_channels = 8;
  411.         *pi_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
  412.                                 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT |
  413.                                 AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT |
  414.                                 AOUT_CHAN_LFE;
  415.         break;
  416.     default:
  417.         return -1;
  418.     }
  419.     switch( (h >> 6) & 0x03 )
  420.     {
  421.     case 1:
  422.         *pi_bits = 16;
  423.         break;
  424.     case 2: /* 20 bits but samples are stored on 24 bits */
  425.     case 3: /* 24 bits */
  426.         *pi_bits = 24;
  427.         break;
  428.     default:
  429.         return -1;
  430.     }
  431.     switch( (h >> 8) & 0x0f ) 
  432.     {
  433.     case 1:
  434.         *pi_rate = 48000;
  435.         break;
  436.     case 4:
  437.         *pi_rate = 96000;
  438.         break;
  439.     case 5:
  440.         *pi_rate = 192000;
  441.         break;
  442.     default:
  443.         return -1;
  444.     }
  445.     return 0;
  446. }
  447. static void DvdExtract( aout_buffer_t *p_aout_buffer, block_t *p_block,
  448.                         unsigned i_bits )
  449. {
  450.     uint8_t *p_out = p_aout_buffer->p_buffer;
  451.     /* 20/24 bits LPCM use special packing */
  452.     if( i_bits == 24 )
  453.     {
  454.         while( p_block->i_buffer / 12 )
  455.         {
  456.             /* Sample 1 */
  457.             p_out[0] = p_block->p_buffer[0];
  458.             p_out[1] = p_block->p_buffer[1];
  459.             p_out[2] = p_block->p_buffer[8];
  460.             /* Sample 2 */
  461.             p_out[3] = p_block->p_buffer[2];
  462.             p_out[4] = p_block->p_buffer[3];
  463.             p_out[5] = p_block->p_buffer[9];
  464.             /* Sample 3 */
  465.             p_out[6] = p_block->p_buffer[4];
  466.             p_out[7] = p_block->p_buffer[5];
  467.             p_out[8] = p_block->p_buffer[10];
  468.             /* Sample 4 */
  469.             p_out[9] = p_block->p_buffer[6];
  470.             p_out[10] = p_block->p_buffer[7];
  471.             p_out[11] = p_block->p_buffer[11];
  472.             p_block->i_buffer -= 12;
  473.             p_block->p_buffer += 12;
  474.             p_out += 12;
  475.         }
  476.     }
  477.     else if( i_bits == 20 )
  478.     {
  479.         while( p_block->i_buffer / 10 )
  480.         {
  481.             /* Sample 1 */
  482.             p_out[0] = p_block->p_buffer[0];
  483.             p_out[1] = p_block->p_buffer[1];
  484.             p_out[2] = p_block->p_buffer[8] & 0xF0;
  485.             /* Sample 2 */
  486.             p_out[3] = p_block->p_buffer[2];
  487.             p_out[4] = p_block->p_buffer[3];
  488.             p_out[5] = p_block->p_buffer[8] << 4;
  489.             /* Sample 3 */
  490.             p_out[6] = p_block->p_buffer[4];
  491.             p_out[7] = p_block->p_buffer[5];
  492.             p_out[8] = p_block->p_buffer[9] & 0xF0;
  493.             /* Sample 4 */
  494.             p_out[9] = p_block->p_buffer[6];
  495.             p_out[10] = p_block->p_buffer[7];
  496.             p_out[11] = p_block->p_buffer[9] << 4;
  497.             p_block->i_buffer -= 10;
  498.             p_block->p_buffer += 10;
  499.             p_out += 12;
  500.         }
  501.     }
  502.     else
  503.     {
  504.         assert( i_bits == 16 );
  505.         memcpy( p_out, p_block->p_buffer, p_block->i_buffer );
  506.     }
  507. }
  508. static void BdExtract( aout_buffer_t *p_aout_buffer, block_t *p_block )
  509. {
  510.     memcpy( p_aout_buffer->p_buffer, p_block->p_buffer, p_block->i_buffer );
  511. }