1 /*****************************************************************************
2 * dtstospdif.c : encapsulates DTS frames into S/PDIF packets
3 *****************************************************************************
4 * Copyright (C) 2003, 2006 the VideoLAN team
7 * Authors: Jon Lech Johansen <jon-vl@nanocrew.net>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
35 #include <vlc_filter.h>
37 /*****************************************************************************
39 *****************************************************************************/
44 /* 3 DTS frames have to be packed into an S/PDIF frame.
45 * We accumulate DTS frames from the decoder until we have enough to
52 /*****************************************************************************
54 *****************************************************************************/
55 static int Create ( vlc_object_t * );
56 static void Close ( vlc_object_t * );
57 static block_t *DoWork( filter_t *, block_t * );
59 /*****************************************************************************
61 *****************************************************************************/
63 set_category( CAT_AUDIO )
64 set_subcategory( SUBCAT_AUDIO_MISC )
65 set_description( N_("Audio filter for DTS->S/PDIF encapsulation") )
66 set_capability( "audio filter", 10 )
67 set_callbacks( Create, Close )
70 /*****************************************************************************
72 *****************************************************************************/
73 static int Create( vlc_object_t *p_this )
75 filter_t * p_filter = (filter_t *)p_this;
78 if( p_filter->fmt_in.audio.i_format != VLC_CODEC_DTS ||
79 ( p_filter->fmt_out.audio.i_format != VLC_CODEC_SPDIFL &&
80 p_filter->fmt_out.audio.i_format != VLC_CODEC_SPDIFB ) )
85 /* Allocate the memory needed to store the module's structure */
86 p_sys = p_filter->p_sys = malloc( sizeof(*p_sys) );
90 p_sys->i_frame_size = 0;
93 p_filter->pf_audio_filter = DoWork;
98 /*****************************************************************************
99 * Close: free our resources
100 *****************************************************************************/
101 static void Close( vlc_object_t * p_this )
103 filter_t * p_filter = (filter_t *)p_this;
105 free( p_filter->p_sys->p_buf );
106 free( p_filter->p_sys );
109 /*****************************************************************************
110 * DoWork: convert a buffer
111 *****************************************************************************/
112 static block_t *DoWork( filter_t * p_filter, block_t * p_in_buf )
114 uint32_t i_ac5_spdif_type = 0;
115 uint16_t i_fz = p_in_buf->i_nb_samples * 4;
116 uint16_t i_frame, i_length = p_in_buf->i_buffer;
117 static const uint8_t p_sync_le[6] = { 0x72, 0xF8, 0x1F, 0x4E, 0x00, 0x00 };
118 static const uint8_t p_sync_be[6] = { 0xF8, 0x72, 0x4E, 0x1F, 0x00, 0x00 };
120 if( p_in_buf->i_buffer != p_filter->p_sys->i_frame_size )
122 /* Frame size changed, reset everything */
123 msg_Warn( p_filter, "Frame size changed from %zu to %zu, "
124 "resetting everything.",
125 p_filter->p_sys->i_frame_size, p_in_buf->i_buffer );
127 p_filter->p_sys->i_frame_size = p_in_buf->i_buffer;
128 p_filter->p_sys->p_buf = xrealloc( p_filter->p_sys->p_buf,
129 p_in_buf->i_buffer * 3 );
130 p_filter->p_sys->i_frames = 0;
134 /* TODO: keeping the blocks in a list would save one memcpy */
135 vlc_memcpy( p_filter->p_sys->p_buf + p_in_buf->i_buffer *
136 p_filter->p_sys->i_frames,
137 p_in_buf->p_buffer, p_in_buf->i_buffer );
139 p_filter->p_sys->i_frames++;
141 if( p_filter->p_sys->i_frames < 3 )
143 if( p_filter->p_sys->i_frames == 1 )
144 /* We'll need the starting date */
145 p_filter->p_sys->start_date = p_in_buf->i_pts;
147 /* Not enough data */
148 block_Release( p_in_buf );
152 p_filter->p_sys->i_frames = 0;
153 block_t *p_out_buf = filter_NewAudioBuffer( p_filter,
154 12 * p_in_buf->i_nb_samples );
158 for( i_frame = 0; i_frame < 3; i_frame++ )
160 uint16_t i_length_padded = i_length;
161 uint8_t * p_out = p_out_buf->p_buffer + (i_frame * i_fz);
162 uint8_t * p_in = p_filter->p_sys->p_buf + (i_frame * i_length);
164 switch( p_in_buf->i_nb_samples )
166 case 512: i_ac5_spdif_type = 0x0B; break;
167 case 1024: i_ac5_spdif_type = 0x0C; break;
168 case 2048: i_ac5_spdif_type = 0x0D; break;
171 /* Copy the S/PDIF headers. */
172 if( p_filter->fmt_out.audio.i_format == VLC_CODEC_SPDIFB )
174 vlc_memcpy( p_out, p_sync_be, 6 );
175 p_out[5] = i_ac5_spdif_type;
176 p_out[6] = (( i_length ) >> 5 ) & 0xFF;
177 p_out[7] = ( i_length << 3 ) & 0xFF;
181 vlc_memcpy( p_out, p_sync_le, 6 );
182 p_out[4] = i_ac5_spdif_type;
183 p_out[6] = ( i_length << 3 ) & 0xFF;
184 p_out[7] = (( i_length ) >> 5 ) & 0xFF;
187 if( ( (p_in[0] == 0x1F || p_in[0] == 0x7F) && p_filter->fmt_out.audio.i_format == VLC_CODEC_SPDIFL ) ||
188 ( (p_in[0] == 0xFF || p_in[0] == 0xFE) && p_filter->fmt_out.audio.i_format == VLC_CODEC_SPDIFB ) )
190 /* We are dealing with a big endian bitstream and a little endian output
191 * or a little endian bitstream and a big endian output.
192 * Byteswap the stream */
193 swab( p_in, p_out + 8, i_length );
195 /* If i_length is odd, we have to adjust swapping a bit.. */
198 p_out[8+i_length-1] = 0;
199 p_out[8+i_length] = p_in[i_length-1];
205 vlc_memcpy( p_out + 8, p_in, i_length );
208 if( i_fz > i_length + 8 )
210 vlc_memset( p_out + 8 + i_length_padded, 0,
211 i_fz - i_length_padded - 8 );
215 p_out_buf->i_pts = p_filter->p_sys->start_date;
216 p_out_buf->i_nb_samples = p_in_buf->i_nb_samples * 3;
217 p_out_buf->i_buffer = p_out_buf->i_nb_samples * 4;
219 block_Release( p_in_buf );