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