1 /*****************************************************************************
2 * dtstospdif.c : encapsulates DTS frames into S/PDIF packets
3 *****************************************************************************
4 * Copyright (C) 2003-2009 the VideoLAN team
7 * Authors: Jon Lech Johansen <jon-vl@nanocrew.net>
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
27 *****************************************************************************/
29 /*****************************************************************************
31 *****************************************************************************/
36 #include <vlc_common.h>
37 #include <vlc_plugin.h>
40 #include <vlc_filter.h>
42 /*****************************************************************************
44 *****************************************************************************/
49 /* 3 DTS frames (max 2048) have to be packed into an S/PDIF frame (6144).
50 * We accumulate DTS frames from the decoder until we have enough to
57 /*****************************************************************************
59 *****************************************************************************/
60 static int Create ( vlc_object_t * );
61 static void Close ( vlc_object_t * );
62 static block_t *DoWork( filter_t *, block_t * );
64 /*****************************************************************************
66 *****************************************************************************/
68 set_category( CAT_AUDIO )
69 set_subcategory( SUBCAT_AUDIO_MISC )
70 set_description( N_("Audio filter for DTS->S/PDIF encapsulation") )
71 set_capability( "audio converter", 10 )
72 set_callbacks( Create, Close )
75 /*****************************************************************************
77 *****************************************************************************/
78 static int Create( vlc_object_t *p_this )
80 filter_t * p_filter = (filter_t *)p_this;
83 if( p_filter->fmt_in.audio.i_format != VLC_CODEC_DTS ||
84 ( p_filter->fmt_out.audio.i_format != VLC_CODEC_SPDIFL &&
85 p_filter->fmt_out.audio.i_format != VLC_CODEC_SPDIFB ) )
90 /* Allocate the memory needed to store the module's structure */
91 p_sys = p_filter->p_sys = malloc( sizeof(*p_sys) );
96 p_sys->i_frame_size = 0;
99 p_filter->pf_audio_filter = DoWork;
104 /*****************************************************************************
105 * Close: free our resources
106 *****************************************************************************/
107 static void Close( vlc_object_t * p_this )
109 filter_t * p_filter = (filter_t *)p_this;
111 free( p_filter->p_sys->p_buf );
112 free( p_filter->p_sys );
115 /*****************************************************************************
116 * DoWork: convert a buffer
117 *****************************************************************************/
118 static block_t *DoWork( filter_t * p_filter, block_t * p_in_buf )
120 uint32_t i_ac5_spdif_type = 0;
121 uint16_t i_fz = p_in_buf->i_nb_samples * 4;
122 uint16_t i_frame, i_length = p_in_buf->i_buffer;
123 static const uint8_t p_sync_le[6] = { 0x72, 0xF8, 0x1F, 0x4E, 0x00, 0x00 };
124 static const uint8_t p_sync_be[6] = { 0xF8, 0x72, 0x4E, 0x1F, 0x00, 0x00 };
126 if( p_in_buf->i_buffer != p_filter->p_sys->i_frame_size )
128 /* Frame size changed, reset everything */
129 msg_Warn( p_filter, "Frame size changed from %zu to %zu, "
130 "resetting everything.",
131 p_filter->p_sys->i_frame_size, p_in_buf->i_buffer );
133 p_filter->p_sys->i_frame_size = p_in_buf->i_buffer;
134 p_filter->p_sys->p_buf = xrealloc( p_filter->p_sys->p_buf,
135 p_in_buf->i_buffer * 3 );
136 p_filter->p_sys->i_frames = 0;
140 /* TODO: keeping the blocks in a list would save one memcpy */
141 memcpy( p_filter->p_sys->p_buf + p_in_buf->i_buffer *
142 p_filter->p_sys->i_frames,
143 p_in_buf->p_buffer, p_in_buf->i_buffer );
145 p_filter->p_sys->i_frames++;
147 if( p_filter->p_sys->i_frames < 3 )
149 if( p_filter->p_sys->i_frames == 1 )
150 /* We'll need the starting date */
151 p_filter->p_sys->start_date = p_in_buf->i_pts;
153 /* Not enough data */
154 block_Release( p_in_buf );
158 p_filter->p_sys->i_frames = 0;
159 block_t *p_out_buf = block_Alloc( 12 * p_in_buf->i_nb_samples );
163 for( i_frame = 0; i_frame < 3; i_frame++ )
165 uint16_t i_length_padded = i_length;
166 uint8_t * p_out = p_out_buf->p_buffer + (i_frame * i_fz);
167 uint8_t * p_in = p_filter->p_sys->p_buf + (i_frame * i_length);
169 switch( p_in_buf->i_nb_samples )
171 case 512: i_ac5_spdif_type = 0x0B; break;
172 case 1024: i_ac5_spdif_type = 0x0C; break;
173 case 2048: i_ac5_spdif_type = 0x0D; break;
176 /* Copy the S/PDIF headers. */
177 if( p_filter->fmt_out.audio.i_format == VLC_CODEC_SPDIFB )
179 memcpy( p_out, p_sync_be, 6 );
180 p_out[5] = i_ac5_spdif_type;
181 SetWBE( p_out + 6, i_length << 3 );
185 memcpy( p_out, p_sync_le, 6 );
186 p_out[4] = i_ac5_spdif_type;
187 SetWLE( p_out + 6, i_length << 3 );
190 if( ( (p_in[0] == 0x1F || p_in[0] == 0x7F) && p_filter->fmt_out.audio.i_format == VLC_CODEC_SPDIFL ) ||
191 ( (p_in[0] == 0xFF || p_in[0] == 0xFE) && p_filter->fmt_out.audio.i_format == VLC_CODEC_SPDIFB ) )
193 /* We are dealing with a big endian bitstream and a little endian output
194 * or a little endian bitstream and a big endian output.
195 * Byteswap the stream */
196 swab( p_in, p_out + 8, i_length );
198 /* If i_length is odd, we have to adjust swapping a bit.. */
201 p_out[8+i_length-1] = 0;
202 p_out[8+i_length] = p_in[i_length-1];
208 memcpy( p_out + 8, p_in, i_length );
211 if( i_fz > i_length + 8 )
213 memset( p_out + 8 + i_length_padded, 0,
214 i_fz - i_length_padded - 8 );
218 p_out_buf->i_pts = p_filter->p_sys->start_date;
219 p_out_buf->i_nb_samples = p_in_buf->i_nb_samples * 3;
220 p_out_buf->i_buffer = p_out_buf->i_nb_samples * 4;
222 block_Release( p_in_buf );