]> git.sesse.net Git - vlc/blob - modules/audio_filter/converter/dtstospdif.c
vlc_plugin: fix non-LGPL plugins meta infos
[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 #define VLC_MODULE_LICENSE VLC_LICENSE_GPL_2_PLUS
37 #include <vlc_common.h>
38 #include <vlc_plugin.h>
39
40 #include <vlc_aout.h>
41 #include <vlc_filter.h>
42
43 /*****************************************************************************
44  * Local structures
45  *****************************************************************************/
46 struct filter_sys_t
47 {
48     mtime_t start_date;
49
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
52      * send. */
53     size_t i_frame_size;
54     uint8_t *p_buf;
55     unsigned i_frames;
56 };
57
58 /*****************************************************************************
59  * Local prototypes
60  *****************************************************************************/
61 static int  Create    ( vlc_object_t * );
62 static void Close     ( vlc_object_t * );
63 static block_t *DoWork( filter_t *, block_t * );
64
65 /*****************************************************************************
66  * Module descriptor
67  *****************************************************************************/
68 vlc_module_begin ()
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 )
74 vlc_module_end ()
75
76 /*****************************************************************************
77  * Create:
78  *****************************************************************************/
79 static int Create( vlc_object_t *p_this )
80 {
81     filter_t * p_filter = (filter_t *)p_this;
82     filter_sys_t *p_sys;
83
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 ) )
87     {
88         return VLC_EGENERIC;
89     }
90
91     /* Allocate the memory needed to store the module's structure */
92     p_sys = p_filter->p_sys = malloc( sizeof(*p_sys) );
93     if( !p_sys )
94         return VLC_ENOMEM;
95
96     p_sys->p_buf = NULL;
97     p_sys->i_frame_size = 0;
98     p_sys->i_frames = 0;
99
100     p_filter->pf_audio_filter = DoWork;
101
102     return VLC_SUCCESS;
103 }
104
105 /*****************************************************************************
106  * Close: free our resources
107  *****************************************************************************/
108 static void Close( vlc_object_t * p_this )
109 {
110     filter_t * p_filter = (filter_t *)p_this;
111
112     free( p_filter->p_sys->p_buf );
113     free( p_filter->p_sys );
114 }
115
116 /*****************************************************************************
117  * DoWork: convert a buffer
118  *****************************************************************************/
119 static block_t *DoWork( filter_t * p_filter, block_t * p_in_buf )
120 {
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 };
126
127     if( p_in_buf->i_buffer != p_filter->p_sys->i_frame_size )
128     {
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 );
133
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;
138     }
139
140     /* Backup frame */
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 );
145
146     p_filter->p_sys->i_frames++;
147
148     if( p_filter->p_sys->i_frames < 3 )
149     {
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;
153
154         /* Not enough data */
155         block_Release( p_in_buf );
156         return NULL;
157     }
158
159     p_filter->p_sys->i_frames = 0;
160     block_t *p_out_buf = block_Alloc( 12 * p_in_buf->i_nb_samples );
161     if( !p_out_buf )
162         goto out;
163
164     for( i_frame = 0; i_frame < 3; i_frame++ )
165     {
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);
169
170         switch( p_in_buf->i_nb_samples )
171         {
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;
175         }
176
177         /* Copy the S/PDIF headers. */
178         if( p_filter->fmt_out.audio.i_format == VLC_CODEC_SPDIFB )
179         {
180             memcpy( p_out, p_sync_be, 6 );
181             p_out[5] = i_ac5_spdif_type;
182             SetWBE( p_out + 6, i_length << 3 );
183         }
184         else
185         {
186             memcpy( p_out, p_sync_le, 6 );
187             p_out[4] = i_ac5_spdif_type;
188             SetWLE( p_out + 6, i_length << 3 );
189         }
190
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 ) )
193         {
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 );
198
199             /* If i_length is odd, we have to adjust swapping a bit.. */
200             if( i_length & 1 )
201             {
202                 p_out[8+i_length-1] = 0;
203                 p_out[8+i_length] = p_in[i_length-1];
204                 i_length_padded++;
205             }
206         }
207         else
208         {
209             memcpy( p_out + 8, p_in, i_length );
210         }
211
212         if( i_fz > i_length + 8 )
213         {
214             memset( p_out + 8 + i_length_padded, 0,
215                         i_fz - i_length_padded - 8 );
216         }
217     }
218
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;
222 out:
223     block_Release( p_in_buf );
224     return p_out_buf;
225 }