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 #define VLC_MODULE_LICENSE VLC_LICENSE_GPL_2_PLUS
37 #include <vlc_common.h>
38 #include <vlc_plugin.h>
41 #include <vlc_filter.h>
43 /*****************************************************************************
45 *****************************************************************************/
50 /* 3 DTS frames (max 2048) have to be packed into an S/PDIF frame (6144).
51 * We accumulate DTS frames from the decoder until we have enough to
58 /*****************************************************************************
60 *****************************************************************************/
61 static int Create ( vlc_object_t * );
62 static void Close ( vlc_object_t * );
63 static block_t *DoWork( filter_t *, block_t * );
65 /*****************************************************************************
67 *****************************************************************************/
69 set_category( CAT_AUDIO )
70 set_subcategory( SUBCAT_AUDIO_MISC )
71 set_description( N_("Audio filter for DTS->S/PDIF encapsulation") )
72 set_capability( "audio converter", 10 )
73 set_callbacks( Create, Close )
76 /*****************************************************************************
78 *****************************************************************************/
79 static int Create( vlc_object_t *p_this )
81 filter_t * p_filter = (filter_t *)p_this;
84 if( p_filter->fmt_in.audio.i_format != VLC_CODEC_DTS ||
85 ( p_filter->fmt_out.audio.i_format != VLC_CODEC_SPDIFL &&
86 p_filter->fmt_out.audio.i_format != VLC_CODEC_SPDIFB ) )
91 /* Allocate the memory needed to store the module's structure */
92 p_sys = p_filter->p_sys = malloc( sizeof(*p_sys) );
97 p_sys->i_frame_size = 0;
100 p_filter->pf_audio_filter = DoWork;
105 /*****************************************************************************
106 * Close: free our resources
107 *****************************************************************************/
108 static void Close( vlc_object_t * p_this )
110 filter_t * p_filter = (filter_t *)p_this;
112 free( p_filter->p_sys->p_buf );
113 free( p_filter->p_sys );
116 /*****************************************************************************
117 * DoWork: convert a buffer
118 *****************************************************************************/
119 static block_t *DoWork( filter_t * p_filter, block_t * p_in_buf )
121 uint32_t i_ac5_spdif_type = 0;
122 uint16_t i_fz = p_in_buf->i_nb_samples * 4;
123 uint16_t i_frame, i_length = p_in_buf->i_buffer;
124 static const uint8_t p_sync_le[6] = { 0x72, 0xF8, 0x1F, 0x4E, 0x00, 0x00 };
125 static const uint8_t p_sync_be[6] = { 0xF8, 0x72, 0x4E, 0x1F, 0x00, 0x00 };
127 if( p_in_buf->i_buffer != p_filter->p_sys->i_frame_size )
129 /* Frame size changed, reset everything */
130 msg_Warn( p_filter, "Frame size changed from %zu to %zu, "
131 "resetting everything.",
132 p_filter->p_sys->i_frame_size, p_in_buf->i_buffer );
134 p_filter->p_sys->i_frame_size = p_in_buf->i_buffer;
135 p_filter->p_sys->p_buf = xrealloc( p_filter->p_sys->p_buf,
136 p_in_buf->i_buffer * 3 );
137 p_filter->p_sys->i_frames = 0;
141 /* TODO: keeping the blocks in a list would save one memcpy */
142 memcpy( p_filter->p_sys->p_buf + p_in_buf->i_buffer *
143 p_filter->p_sys->i_frames,
144 p_in_buf->p_buffer, p_in_buf->i_buffer );
146 p_filter->p_sys->i_frames++;
148 if( p_filter->p_sys->i_frames < 3 )
150 if( p_filter->p_sys->i_frames == 1 )
151 /* We'll need the starting date */
152 p_filter->p_sys->start_date = p_in_buf->i_pts;
154 /* Not enough data */
155 block_Release( p_in_buf );
159 p_filter->p_sys->i_frames = 0;
160 block_t *p_out_buf = block_Alloc( 12 * p_in_buf->i_nb_samples );
164 for( i_frame = 0; i_frame < 3; i_frame++ )
166 uint16_t i_length_padded = i_length;
167 uint8_t * p_out = p_out_buf->p_buffer + (i_frame * i_fz);
168 uint8_t * p_in = p_filter->p_sys->p_buf + (i_frame * i_length);
170 switch( p_in_buf->i_nb_samples )
172 case 512: i_ac5_spdif_type = 0x0B; break;
173 case 1024: i_ac5_spdif_type = 0x0C; break;
174 case 2048: i_ac5_spdif_type = 0x0D; break;
177 /* Copy the S/PDIF headers. */
178 if( p_filter->fmt_out.audio.i_format == VLC_CODEC_SPDIFB )
180 memcpy( p_out, p_sync_be, 6 );
181 p_out[5] = i_ac5_spdif_type;
182 SetWBE( p_out + 6, i_length << 3 );
186 memcpy( p_out, p_sync_le, 6 );
187 p_out[4] = i_ac5_spdif_type;
188 SetWLE( p_out + 6, i_length << 3 );
191 if( ( (p_in[0] == 0x1F || p_in[0] == 0x7F) && p_filter->fmt_out.audio.i_format == VLC_CODEC_SPDIFL ) ||
192 ( (p_in[0] == 0xFF || p_in[0] == 0xFE) && p_filter->fmt_out.audio.i_format == VLC_CODEC_SPDIFB ) )
194 /* We are dealing with a big endian bitstream and a little endian output
195 * or a little endian bitstream and a big endian output.
196 * Byteswap the stream */
197 swab( p_in, p_out + 8, i_length );
199 /* If i_length is odd, we have to adjust swapping a bit.. */
202 p_out[8+i_length-1] = 0;
203 p_out[8+i_length] = p_in[i_length-1];
209 memcpy( p_out + 8, p_in, i_length );
212 if( i_fz > i_length + 8 )
214 memset( p_out + 8 + i_length_padded, 0,
215 i_fz - i_length_padded - 8 );
219 p_out_buf->i_pts = p_filter->p_sys->start_date;
220 p_out_buf->i_nb_samples = p_in_buf->i_nb_samples * 3;
221 p_out_buf->i_buffer = p_out_buf->i_nb_samples * 4;
223 block_Release( p_in_buf );