00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include <stdlib.h>
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
00041
00042 struct aout_filter_sys_t
00043 {
00044
00045
00046
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
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
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
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
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
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
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
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
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
00147 p_filter->p_sys->start_date = p_in_buf->start_date;
00148
00149
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
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
00189
00190
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];
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 }