]> git.sesse.net Git - vlc/blob - modules/codec/aes3.c
Use var_InheritString for --decklink-video-connection.
[vlc] / modules / codec / aes3.c
1 /*****************************************************************************
2  * aes3.c: aes3 decoder/packetizer module
3  *****************************************************************************
4  * Copyright (C) 2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@videolan.org>
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 #include <vlc_codec.h>
34 #include <vlc_aout.h>
35 #include <assert.h>
36
37 /*****************************************************************************
38  * Module descriptor
39  *****************************************************************************/
40 static int  OpenDecoder   ( vlc_object_t * );
41 static int  OpenPacketizer( vlc_object_t * );
42 static void Close         ( vlc_object_t * );
43
44 vlc_module_begin ()
45
46     set_category( CAT_INPUT )
47     set_subcategory( SUBCAT_INPUT_ACODEC )
48     set_description( N_("AES3/SMPTE 302M audio decoder") )
49     set_capability( "decoder", 100 )
50     set_callbacks( OpenDecoder, Close )
51
52     add_submodule ()
53     set_description( N_("AES3/SMPTE 302M audio packetizer") )
54     set_capability( "packetizer", 100 )
55     set_callbacks( OpenPacketizer, Close )
56
57 vlc_module_end ()
58
59 /*****************************************************************************
60  * decoder_sys_t : aes3 decoder descriptor
61  *****************************************************************************/
62 struct decoder_sys_t
63 {
64     /*
65      * Output properties
66      */
67     date_t end_date;
68 };
69
70 #define AES3_HEADER_LEN 4
71
72 /*****************************************************************************
73  * Local prototypes
74  *****************************************************************************/
75 static int Open( decoder_t *p_dec, bool b_packetizer );
76
77 static block_t *Parse( decoder_t *p_dec, int *pi_frame_length, int *pi_bits,
78                        block_t **pp_block, bool b_packetizer );
79
80 static inline uint8_t Reverse8( int n )
81 {
82     n = ((n >> 1) & 0x55) | ((n << 1) & 0xaa);
83     n = ((n >> 2) & 0x33) | ((n << 2) & 0xcc);
84     n = ((n >> 4) & 0x0f) | ((n << 4) & 0xf0);
85     return n;
86 }
87
88 /*****************************************************************************
89  * OpenDecoder:
90  *****************************************************************************/
91 static int OpenDecoder( vlc_object_t *p_this )
92 {
93     decoder_t *p_dec = (decoder_t*)p_this;
94
95     return Open( p_dec, false );
96 }
97
98 /*****************************************************************************
99  * OpenPacketizer:
100  *****************************************************************************/
101 static int OpenPacketizer( vlc_object_t *p_this )
102 {
103     decoder_t *p_dec = (decoder_t*)p_this;
104
105     return Open( p_dec, true );
106 }
107
108 /*****************************************************************************
109  * Close : aes3 decoder destruction
110  *****************************************************************************/
111 static void Close( vlc_object_t *p_this )
112 {
113     decoder_t *p_dec = (decoder_t*)p_this;
114     free( p_dec->p_sys );
115 }
116
117 /*****************************************************************************
118  * Decode: decodes an aes3 frame.
119  ****************************************************************************
120  * Beware, this function must be fed with complete frames (PES packet).
121  *****************************************************************************/
122 static aout_buffer_t *Decode( decoder_t *p_dec, block_t **pp_block )
123 {
124     decoder_sys_t *p_sys = p_dec->p_sys;
125     block_t       *p_block;
126     aout_buffer_t *p_aout_buffer;
127     int            i_frame_length, i_bits;
128
129     p_block = Parse( p_dec, &i_frame_length, &i_bits, pp_block, false );
130     if( !p_block )
131         return NULL;
132
133     p_aout_buffer = decoder_NewAudioBuffer( p_dec, i_frame_length );
134     if( p_aout_buffer == NULL )
135         goto exit;
136
137     p_aout_buffer->i_pts = date_Get( &p_sys->end_date );
138     p_aout_buffer->i_length = date_Increment( &p_sys->end_date,
139                                       i_frame_length ) - p_aout_buffer->i_pts;
140
141     p_block->i_buffer -= AES3_HEADER_LEN;
142     p_block->p_buffer += AES3_HEADER_LEN;
143
144     if( i_bits == 24 )
145     {
146         uint8_t *p_out = p_aout_buffer->p_buffer;
147
148         while( p_block->i_buffer / 7 )
149         {
150             p_out[0] = Reverse8( p_block->p_buffer[0] );
151             p_out[1] = Reverse8( p_block->p_buffer[1] );
152             p_out[2] = Reverse8( p_block->p_buffer[2] );
153
154             p_out[3] = (Reverse8( p_block->p_buffer[3] ) >> 4) | ( (Reverse8( p_block->p_buffer[4] ) << 4) & 0xf0 );
155             p_out[4] = (Reverse8( p_block->p_buffer[4] ) >> 4) | ( (Reverse8( p_block->p_buffer[5] ) << 4) & 0xf0 );
156             p_out[5] = (Reverse8( p_block->p_buffer[5] ) >> 4) | ( (Reverse8( p_block->p_buffer[6] ) << 4) & 0xf0 );
157
158             p_block->i_buffer -= 7;
159             p_block->p_buffer += 7;
160             p_out += 6;
161         }
162
163     }
164     else if( i_bits == 20 )
165     {
166         uint8_t *p_out = p_aout_buffer->p_buffer;
167
168         while( p_block->i_buffer / 6 )
169         {
170             p_out[0] =                                            ( (Reverse8( p_block->p_buffer[0] ) << 4) & 0xf0 );
171             p_out[1] = ( Reverse8( p_block->p_buffer[0]) >> 4 ) | ( (Reverse8( p_block->p_buffer[1]) << 4 ) & 0xf0 );
172             p_out[2] = ( Reverse8( p_block->p_buffer[1]) >> 4 ) | ( (Reverse8( p_block->p_buffer[2]) << 4 ) & 0xf0 );
173
174             p_out[3] =                                            ( (Reverse8( p_block->p_buffer[3] ) << 4) & 0xf0 );
175             p_out[4] = ( Reverse8( p_block->p_buffer[3]) >> 4 ) | ( (Reverse8( p_block->p_buffer[4]) << 4 ) & 0xf0 );
176             p_out[5] = ( Reverse8( p_block->p_buffer[4]) >> 4 ) | ( (Reverse8( p_block->p_buffer[5]) << 4 ) & 0xf0 );
177
178             p_block->i_buffer -= 6;
179             p_block->p_buffer += 6;
180             p_out += 6;
181         }
182     }
183     else
184     {
185         uint8_t *p_out = p_aout_buffer->p_buffer;
186
187         assert( i_bits == 16 );
188
189         while( p_block->i_buffer / 5 )
190         {
191             p_out[0] = Reverse8( p_block->p_buffer[0] );
192             p_out[1] = Reverse8( p_block->p_buffer[1] );
193
194             p_out[2] = (Reverse8( p_block->p_buffer[2] ) >> 4) | ( (Reverse8( p_block->p_buffer[3] ) << 4) & 0xf0 );
195             p_out[3] = (Reverse8( p_block->p_buffer[3] ) >> 4) | ( (Reverse8( p_block->p_buffer[4] ) << 4) & 0xf0 );
196
197             p_block->i_buffer -= 5;
198             p_block->p_buffer += 5;
199             p_out += 4;
200         }
201     }
202
203 exit:
204     block_Release( p_block );
205     return p_aout_buffer;
206 }
207
208 /*****************************************************************************
209  * Packetize: packetizes an aes3 frame.
210  ****************************************************************************
211  * Beware, this function must be fed with complete frames (PES packet).
212  *****************************************************************************/
213 static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
214 {
215     decoder_sys_t *p_sys = p_dec->p_sys;
216     block_t       *p_block;
217     int           i_frame_length, i_bits;
218
219     p_block = Parse( p_dec, &i_frame_length, &i_bits, pp_block, true );
220     if( !p_block )
221         return NULL;
222
223     p_block->i_pts = p_block->i_dts = date_Get( &p_sys->end_date );
224     p_block->i_length = date_Increment( &p_sys->end_date, i_frame_length ) - p_block->i_pts;
225
226     /* Just pass on the incoming frame */
227     return p_block;
228 }
229
230 /*****************************************************************************
231  *
232  ****************************************************************************/
233 static int Open( decoder_t *p_dec, bool b_packetizer )
234 {
235     decoder_sys_t *p_sys;
236
237     if( p_dec->fmt_in.i_codec != VLC_CODEC_302M )
238         return VLC_EGENERIC;
239
240     /* Allocate the memory needed to store the decoder's structure */
241     p_dec->p_sys = p_sys = malloc( sizeof(decoder_sys_t) );
242
243     if( !p_sys )
244         return VLC_EGENERIC;
245
246     /* Misc init */
247     date_Init( &p_sys->end_date, 48000, 1 );
248     date_Set( &p_sys->end_date, 0 );
249
250     /* Set output properties */
251     p_dec->fmt_out.i_cat = AUDIO_ES;
252     p_dec->fmt_out.audio.i_rate = 48000;
253
254     /* Set callback */
255     if( b_packetizer )
256     {
257         p_dec->fmt_out.i_codec = VLC_CODEC_302M;
258
259         p_dec->pf_decode_audio = NULL;
260         p_dec->pf_packetize    = Packetize;
261     }
262     else
263     {
264         p_dec->fmt_out.i_codec = VLC_CODEC_S16N;
265         p_dec->fmt_out.audio.i_bitspersample = 16;
266
267         p_dec->pf_decode_audio = Decode;
268         p_dec->pf_packetize    = NULL;
269     }
270     return VLC_SUCCESS;
271 }
272
273 static const unsigned int pi_original_channels[4] = {
274     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
275     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
276         AOUT_CHAN_CENTER | AOUT_CHAN_LFE,
277     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
278         AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT |
279         AOUT_CHAN_CENTER | AOUT_CHAN_LFE,
280     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
281         AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT |
282         AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT |
283         AOUT_CHAN_CENTER | AOUT_CHAN_LFE,
284 };
285
286 static block_t *Parse( decoder_t *p_dec, int *pi_frame_length, int *pi_bits,
287                        block_t **pp_block, bool b_packetizer )
288 {
289     decoder_sys_t *p_sys = p_dec->p_sys;
290     block_t       *p_block;
291     uint32_t h;
292     unsigned int i_size;
293     int i_channels;
294     int i_id;
295     int i_bits;
296
297     if( !pp_block || !*pp_block ) return NULL;
298
299     p_block = *pp_block;
300     *pp_block = NULL; /* So the packet doesn't get re-sent */
301
302     /* Date management */
303     if( p_block->i_pts > VLC_TS_INVALID &&
304         p_block->i_pts != date_Get( &p_sys->end_date ) )
305     {
306         date_Set( &p_sys->end_date, p_block->i_pts );
307     }
308
309     if( !date_Get( &p_sys->end_date ) )
310     {
311         /* We've just started the stream, wait for the first PTS. */
312         block_Release( p_block );
313         return NULL;
314     }
315
316     if( p_block->i_buffer <= AES3_HEADER_LEN )
317     {
318         msg_Err(p_dec, "frame is too short");
319         block_Release( p_block );
320         return NULL;
321     }
322
323     /*
324      * AES3 header :
325      * size:            16
326      * number channels   2
327      * channel_id        8
328      * bits per samples  2
329      * alignments        4
330      */
331
332     h = GetDWBE( p_block->p_buffer );
333     i_size = (h >> 16) & 0xffff;
334     i_channels = 2 + 2*( (h >> 14) & 0x03 );
335     i_id = (h >> 6) & 0xff;
336     i_bits = 16 + 4*( (h >> 4)&0x03 );
337
338     if( AES3_HEADER_LEN + i_size != p_block->i_buffer || i_bits > 24 )
339     {
340         msg_Err(p_dec, "frame has invalid header");
341         block_Release( p_block );
342         return NULL;
343     }
344
345     /* Set output properties */
346     if( b_packetizer )
347     {
348         p_dec->fmt_out.audio.i_bitspersample = i_bits;
349     }
350     else
351     {
352         p_dec->fmt_out.i_codec = i_bits == 16 ? VLC_CODEC_S16L : VLC_CODEC_S24L;
353         p_dec->fmt_out.audio.i_bitspersample = i_bits == 16 ? 16 : 24;
354     }
355
356     p_dec->fmt_out.audio.i_channels = i_channels;
357     p_dec->fmt_out.audio.i_original_channels = pi_original_channels[i_channels/2-1];
358     p_dec->fmt_out.audio.i_physical_channels = pi_original_channels[i_channels/2-1] & AOUT_CHAN_PHYSMASK;
359
360     *pi_frame_length = (p_block->i_buffer - AES3_HEADER_LEN) / ( (4+i_bits) * i_channels / 8 );
361     *pi_bits = i_bits;
362     return p_block;
363 }
364