]> git.sesse.net Git - vlc/blob - modules/audio_filter/converter/dtstospdif.c
88384c8e2329f96f50d9e69d75917b3d553963a3
[vlc] / modules / audio_filter / converter / dtstospdif.c
1 /*****************************************************************************
2  * dtstospdif.c : encapsulates DTS frames into S/PDIF packets
3  *****************************************************************************
4  * Copyright (C) 2003-2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Jon Lech Johansen <jon-vl@nanocrew.net>
8  *          Gildas Bazin
9  *          Derk-Jan Hartman
10  *          Pierre d'Herbemont
11  *          Rémi Denis-Courmont
12  *          Rafaël Carré
13  *
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.
18  *
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.
23  *
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  *****************************************************************************/
28
29 /*****************************************************************************
30  * Preamble
31  *****************************************************************************/
32 #ifdef HAVE_CONFIG_H
33 # include "config.h"
34 #endif
35
36 #include <vlc_common.h>
37 #include <vlc_plugin.h>
38
39 #include <vlc_aout.h>
40 #include <vlc_filter.h>
41
42 /*****************************************************************************
43  * Local structures
44  *****************************************************************************/
45 struct filter_sys_t
46 {
47     mtime_t start_date;
48
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
51      * send. */
52     size_t i_frame_size;
53     uint8_t *p_buf;
54     unsigned i_frames;
55 };
56
57 /*****************************************************************************
58  * Local prototypes
59  *****************************************************************************/
60 static int  Create    ( vlc_object_t * );
61 static void Close     ( vlc_object_t * );
62 static block_t *DoWork( filter_t *, block_t * );
63
64 /*****************************************************************************
65  * Module descriptor
66  *****************************************************************************/
67 vlc_module_begin ()
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 )
73 vlc_module_end ()
74
75 /*****************************************************************************
76  * Create:
77  *****************************************************************************/
78 static int Create( vlc_object_t *p_this )
79 {
80     filter_t * p_filter = (filter_t *)p_this;
81     filter_sys_t *p_sys;
82
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 ) )
86     {
87         return VLC_EGENERIC;
88     }
89
90     /* Allocate the memory needed to store the module's structure */
91     p_sys = p_filter->p_sys = malloc( sizeof(*p_sys) );
92     if( !p_sys )
93         return VLC_ENOMEM;
94
95     p_sys->p_buf = NULL;
96     p_sys->i_frame_size = 0;
97     p_sys->i_frames = 0;
98
99     p_filter->pf_audio_filter = DoWork;
100
101     return VLC_SUCCESS;
102 }
103
104 /*****************************************************************************
105  * Close: free our resources
106  *****************************************************************************/
107 static void Close( vlc_object_t * p_this )
108 {
109     filter_t * p_filter = (filter_t *)p_this;
110
111     free( p_filter->p_sys->p_buf );
112     free( p_filter->p_sys );
113 }
114
115 /*****************************************************************************
116  * DoWork: convert a buffer
117  *****************************************************************************/
118 static block_t *DoWork( filter_t * p_filter, block_t * p_in_buf )
119 {
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 };
125
126     if( p_in_buf->i_buffer != p_filter->p_sys->i_frame_size )
127     {
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 );
132
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;
137     }
138
139     /* Backup frame */
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 );
144
145     p_filter->p_sys->i_frames++;
146
147     if( p_filter->p_sys->i_frames < 3 )
148     {
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;
152
153         /* Not enough data */
154         block_Release( p_in_buf );
155         return NULL;
156     }
157
158     p_filter->p_sys->i_frames = 0;
159     block_t *p_out_buf = block_Alloc( 12 * p_in_buf->i_nb_samples );
160     if( !p_out_buf )
161         goto out;
162
163     for( i_frame = 0; i_frame < 3; i_frame++ )
164     {
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);
168
169         switch( p_in_buf->i_nb_samples )
170         {
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;
174         }
175
176         /* Copy the S/PDIF headers. */
177         if( p_filter->fmt_out.audio.i_format == VLC_CODEC_SPDIFB )
178         {
179             memcpy( p_out, p_sync_be, 6 );
180             p_out[5] = i_ac5_spdif_type;
181             SetWBE( p_out + 6, i_length << 3 );
182         }
183         else
184         {
185             memcpy( p_out, p_sync_le, 6 );
186             p_out[4] = i_ac5_spdif_type;
187             SetWLE( p_out + 6, i_length << 3 );
188         }
189
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 ) )
192         {
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 );
197
198             /* If i_length is odd, we have to adjust swapping a bit.. */
199             if( i_length & 1 )
200             {
201                 p_out[8+i_length-1] = 0;
202                 p_out[8+i_length] = p_in[i_length-1];
203                 i_length_padded++;
204             }
205         }
206         else
207         {
208             memcpy( p_out + 8, p_in, i_length );
209         }
210
211         if( i_fz > i_length + 8 )
212         {
213             memset( p_out + 8 + i_length_padded, 0,
214                         i_fz - i_length_padded - 8 );
215         }
216     }
217
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;
221 out:
222     block_Release( p_in_buf );
223     return p_out_buf;
224 }