]> git.sesse.net Git - vlc/blob - modules/audio_filter/converter/dtstospdif.c
DTS to SPDIF: audio filter2
[vlc] / modules / audio_filter / converter / dtstospdif.c
1 /*****************************************************************************
2  * dtstospdif.c : encapsulates DTS frames into S/PDIF packets
3  *****************************************************************************
4  * Copyright (C) 2003, 2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Jon Lech Johansen <jon-vl@nanocrew.net>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33
34 #include <vlc_aout.h>
35 #include <vlc_filter.h>
36
37 /*****************************************************************************
38  * Local structures
39  *****************************************************************************/
40 struct filter_sys_t
41 {
42     mtime_t start_date;
43
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
46      * send. */
47     size_t i_frame_size;
48     uint8_t *p_buf;
49     unsigned i_frames;
50 };
51
52 /*****************************************************************************
53  * Local prototypes
54  *****************************************************************************/
55 static int  Create    ( vlc_object_t * );
56 static void Close     ( vlc_object_t * );
57 static block_t *DoWork( filter_t *, block_t * );
58
59 /*****************************************************************************
60  * Module descriptor
61  *****************************************************************************/
62 vlc_module_begin ()
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 filter2", 10 )
67     set_callbacks( Create, Close )
68 vlc_module_end ()
69
70 /*****************************************************************************
71  * Create:
72  *****************************************************************************/
73 static int Create( vlc_object_t *p_this )
74 {
75     filter_t * p_filter = (filter_t *)p_this;
76     filter_sys_t *p_sys;
77
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 ) )
81     {
82         return VLC_EGENERIC;
83     }
84
85     /* Allocate the memory needed to store the module's structure */
86     p_sys = p_filter->p_sys = malloc( sizeof(*p_sys) );
87     if( !p_sys )
88         return VLC_ENOMEM;
89     p_sys->p_buf = NULL;
90     p_sys->i_frame_size = 0;
91     p_sys->i_frames = 0;
92
93     p_filter->pf_audio_filter = DoWork;
94
95     return VLC_SUCCESS;
96 }
97
98 /*****************************************************************************
99  * Close: free our resources
100  *****************************************************************************/
101 static void Close( vlc_object_t * p_this )
102 {
103     filter_t * p_filter = (filter_t *)p_this;
104
105     free( p_filter->p_sys->p_buf );
106     free( p_filter->p_sys );
107 }
108
109 /*****************************************************************************
110  * DoWork: convert a buffer
111  *****************************************************************************/
112 static block_t *DoWork( filter_t * p_filter, block_t * p_in_buf )
113 {
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 };
119
120     if( p_in_buf->i_buffer != p_filter->p_sys->i_frame_size )
121     {
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 );
126
127         p_filter->p_sys->i_frame_size = p_in_buf->i_buffer;
128         p_filter->p_sys->p_buf = realloc( p_filter->p_sys->p_buf,
129                                           p_in_buf->i_buffer * 3 );
130         p_filter->p_sys->i_frames = 0;
131     }
132
133     /* Backup frame */
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 );
138
139     p_filter->p_sys->i_frames++;
140
141     if( p_filter->p_sys->i_frames < 3 )
142     {
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;
146
147         /* Not enough data */
148         block_Release( p_in_buf );
149         return NULL;
150     }
151
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 );
155     if( !p_out_buf )
156         goto out;
157
158     for( i_frame = 0; i_frame < 3; i_frame++ )
159     {
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);
163
164         switch( p_in_buf->i_nb_samples )
165         {
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;
169         }
170
171         /* Copy the S/PDIF headers. */
172         if( p_filter->fmt_out.audio.i_format == VLC_CODEC_SPDIFB )
173         {
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;
178         }
179         else
180         {
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;
185         }
186
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 ) )
189         {
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 );
194
195             /* If i_length is odd, we have to adjust swapping a bit.. */
196             if( i_length & 1 )
197             {
198                 p_out[8+i_length-1] = 0;
199                 p_out[8+i_length] = p_in[i_length-1];
200                 i_length_padded++;
201             }
202         }
203         else
204         {
205             vlc_memcpy( p_out + 8, p_in, i_length );
206         }
207
208         if( i_fz > i_length + 8 )
209         {
210             vlc_memset( p_out + 8 + i_length_padded, 0,
211                         i_fz - i_length_padded - 8 );
212         }
213     }
214
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;
218 out:
219     block_Release( p_in_buf );
220     return p_out_buf;
221 }