Main Page | Modules | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

dtstospdif.c

00001 /*****************************************************************************
00002  * dtstospdif.c : encapsulates DTS frames into S/PDIF packets
00003  *****************************************************************************
00004  * Copyright (C) 2003 the VideoLAN team
00005  * $Id: dtstospdif.c 13671 2005-12-10 19:26:03Z hartman $
00006  *
00007  * Authors: Jon Lech Johansen <[email protected]>
00008  *
00009  * This program is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  * 
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
00022  *****************************************************************************/
00023 
00024 /*****************************************************************************
00025  * Preamble
00026  *****************************************************************************/
00027 #include <stdlib.h>                                      /* malloc(), free() */
00028 #include <string.h>
00029 
00030 #include <vlc/vlc.h>
00031 
00032 #ifdef HAVE_UNISTD_H
00033 #   include <unistd.h>
00034 #endif
00035 
00036 #include "audio_output.h"
00037 #include "aout_internal.h"
00038 
00039 /*****************************************************************************
00040  * Local structures
00041  *****************************************************************************/
00042 struct aout_filter_sys_t
00043 {
00044     /* 3 DTS frames have to be packed into an S/PDIF frame.
00045      * We accumulate DTS frames from the decoder until we have enough to
00046      * send. */
00047 
00048     uint8_t *p_buf;
00049 
00050     mtime_t start_date;
00051 
00052     int i_frames;
00053     unsigned int i_frame_size;
00054 };
00055 
00056 /*****************************************************************************
00057  * Local prototypes
00058  *****************************************************************************/
00059 static int  Create    ( vlc_object_t * );
00060 static void Close     ( vlc_object_t * );
00061 static void DoWork    ( aout_instance_t *, aout_filter_t *, aout_buffer_t *,
00062                         aout_buffer_t * );
00063 
00064 /*****************************************************************************
00065  * Module descriptor
00066  *****************************************************************************/
00067 vlc_module_begin();
00068     set_category( CAT_AUDIO );
00069     set_subcategory( SUBCAT_AUDIO_MISC );
00070     set_description( _("audio filter for DTS->S/PDIF encapsulation") );
00071     set_capability( "audio filter", 10 );
00072     set_callbacks( Create, Close );
00073 vlc_module_end();
00074 
00075 /*****************************************************************************
00076  * Create:
00077  *****************************************************************************/
00078 static int Create( vlc_object_t *p_this )
00079 {
00080     aout_filter_t * p_filter = (aout_filter_t *)p_this;
00081 
00082     if( p_filter->input.i_format != VLC_FOURCC('d','t','s',' ') ||
00083         ( p_filter->output.i_format != VLC_FOURCC('s','p','d','i') &&
00084           p_filter->output.i_format != VLC_FOURCC('s','p','d','b') ) )
00085     {
00086         return -1;
00087     }
00088 
00089     /* Allocate the memory needed to store the module's structure */
00090     p_filter->p_sys = malloc( sizeof(struct aout_filter_sys_t) );
00091     if( p_filter->p_sys == NULL )
00092     {
00093         msg_Err( p_filter, "out of memory" );
00094         return VLC_ENOMEM;
00095     }
00096     memset( p_filter->p_sys, 0, sizeof(struct aout_filter_sys_t) );
00097     p_filter->p_sys->p_buf = 0;
00098 
00099     p_filter->pf_do_work = DoWork;
00100     p_filter->b_in_place = 1;
00101 
00102     return 0;
00103 }
00104 
00105 /*****************************************************************************
00106  * Close: free our resources
00107  *****************************************************************************/
00108 static void Close( vlc_object_t * p_this )
00109 {
00110     aout_filter_t * p_filter = (aout_filter_t *)p_this;
00111     if( p_filter->p_sys->i_frame_size ) free( p_filter->p_sys->p_buf );
00112     free( p_filter->p_sys );
00113 }
00114 
00115 /*****************************************************************************
00116  * DoWork: convert a buffer
00117  *****************************************************************************/
00118 static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
00119                     aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
00120 {
00121     uint32_t i_ac5_spdif_type = 0;
00122     uint16_t i_fz = p_in_buf->i_nb_samples * 4;
00123     uint16_t i_frame, i_length = p_in_buf->i_nb_bytes;
00124     static const uint8_t p_sync_le[6] = { 0x72, 0xF8, 0x1F, 0x4E, 0x00, 0x00 };
00125     static const uint8_t p_sync_be[6] = { 0xF8, 0x72, 0x4E, 0x1F, 0x00, 0x00 };
00126 
00127     if( p_in_buf->i_nb_bytes != p_filter->p_sys->i_frame_size )
00128     {
00129         /* Frame size changed, reset everything */
00130         p_filter->p_sys->i_frame_size = p_in_buf->i_nb_bytes;
00131         p_filter->p_sys->p_buf = realloc( p_filter->p_sys->p_buf,
00132                                           p_in_buf->i_nb_bytes * 3 );
00133         p_filter->p_sys->i_frames = 0;
00134     }
00135 
00136     /* Backup frame */
00137     p_filter->p_vlc->pf_memcpy( p_filter->p_sys->p_buf + p_in_buf->i_nb_bytes *
00138                                 p_filter->p_sys->i_frames, p_in_buf->p_buffer,
00139                                 p_in_buf->i_nb_bytes );
00140 
00141     p_filter->p_sys->i_frames++;
00142 
00143     if( p_filter->p_sys->i_frames < 3 )
00144     {
00145         if( !p_filter->p_sys->i_frames )
00146             /* We'll need the starting date */
00147             p_filter->p_sys->start_date = p_in_buf->start_date;
00148 
00149         /* Not enough data */
00150         p_out_buf->i_nb_samples = 0;
00151         p_out_buf->i_nb_bytes = 0;
00152         return;
00153     }
00154 
00155     p_filter->p_sys->i_frames = 0;
00156 
00157     for( i_frame = 0; i_frame < 3; i_frame++ )
00158     {
00159         byte_t * p_out = p_out_buf->p_buffer + (i_frame * i_fz);
00160         byte_t * p_in = p_filter->p_sys->p_buf + (i_frame * i_length);
00161 
00162         switch( p_in_buf->i_nb_samples )
00163         {
00164             case  512: i_ac5_spdif_type = 0x0B; break;
00165             case 1024: i_ac5_spdif_type = 0x0C; break;
00166             case 2048: i_ac5_spdif_type = 0x0D; break;
00167         }
00168 
00169         /* Copy the S/PDIF headers. */
00170         if( p_filter->output.i_format == VLC_FOURCC('s','p','d','b') )
00171         {
00172             p_filter->p_vlc->pf_memcpy( p_out, p_sync_be, 6 );
00173             p_out[5] = i_ac5_spdif_type;
00174             p_out[6] = (( i_length ) >> 5 ) & 0xFF;
00175             p_out[7] = ( i_length << 3 ) & 0xFF;
00176         }
00177         else
00178         {
00179             p_filter->p_vlc->pf_memcpy( p_out, p_sync_le, 6 );
00180             p_out[4] = i_ac5_spdif_type;
00181             p_out[6] = ( i_length << 3 ) & 0xFF;
00182             p_out[7] = (( i_length ) >> 5 ) & 0xFF;
00183         }
00184 
00185         if( ( (p_in[0] == 0x1F || p_in[0] == 0x7F) && p_filter->output.i_format == VLC_FOURCC('s','p','d','i') ) ||
00186             ( (p_in[0] == 0xFF || p_in[0] == 0xFE) && p_filter->output.i_format == VLC_FOURCC('s','p','d','b') ) )
00187         {
00188             /* We are dealing with a big endian bitstream and a little endian output
00189              * or a little endian bitstream and a big endian output.
00190              * Byteswap the stream */
00191 #ifdef HAVE_SWAB
00192             swab( p_in, p_out + 8, i_length );
00193 #else
00194             uint16_t i;
00195             byte_t * p_tmp, tmp;
00196             p_tmp = p_out + 8;
00197             for( i = i_length / 2 ; i-- ; )
00198             {
00199                 tmp = p_in[0]; /* in-place filter */
00200                 p_tmp[0] = p_in[1];
00201                 p_tmp[1] = tmp;
00202                 p_tmp += 2; p_in += 2;
00203             }
00204 #endif
00205         }
00206         else
00207         {
00208             p_filter->p_vlc->pf_memcpy( p_out + 8, p_in, i_length );
00209         }
00210 
00211         if( i_fz > i_length + 8 )
00212         {
00213             p_filter->p_vlc->pf_memset( p_out + 8 + i_length, 0,
00214                                         i_fz - i_length - 8 );
00215         }
00216     }
00217 
00218     p_out_buf->start_date = p_filter->p_sys->start_date;
00219     p_out_buf->i_nb_samples = p_in_buf->i_nb_samples * 3;
00220     p_out_buf->i_nb_bytes = p_out_buf->i_nb_samples * 4;
00221 }

Generated on Tue Dec 20 10:14:27 2005 for vlc-0.8.4a by  doxygen 1.4.2