]> git.sesse.net Git - vlc/blob - modules/codec/a52.c
mediacodec: fix warning
[vlc] / modules / codec / a52.c
1 /*****************************************************************************
2  * a52.c: parse A/52 audio sync info and packetize the stream
3  *****************************************************************************
4  * Copyright (C) 2001-2002 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Stéphane Borel <stef@via.ecp.fr>
8  *          Christophe Massiot <massiot@via.ecp.fr>
9  *          Gildas Bazin <gbazin@videolan.org>
10  *
11  * This program is free software; you can redistribute it and/or modify it
12  * under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation; either version 2.1 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_codec.h>
36 #include <vlc_block_helper.h>
37 #include <vlc_modules.h>
38
39 #include "a52.h"
40
41 #include "../packetizer/packetizer_helper.h"
42
43 /*****************************************************************************
44  * Module descriptor
45  *****************************************************************************/
46 static int  OpenDecoder   ( vlc_object_t * );
47 static int  OpenPacketizer( vlc_object_t * );
48 static void CloseCommon   ( vlc_object_t * );
49
50 vlc_module_begin ()
51     set_description( N_("A/52 parser") )
52     set_capability( "decoder", 100 )
53     set_callbacks( OpenDecoder, CloseCommon )
54     set_category( CAT_INPUT )
55     set_subcategory( SUBCAT_INPUT_ACODEC )
56
57     add_submodule ()
58     set_description( N_("A/52 audio packetizer") )
59     set_capability( "packetizer", 10 )
60     set_callbacks( OpenPacketizer, CloseCommon )
61 vlc_module_end ()
62
63 /*****************************************************************************
64  * decoder_sys_t : decoder descriptor
65  *****************************************************************************/
66
67 struct decoder_sys_t
68 {
69     /* Module mode */
70     bool b_packetizer;
71
72     /*
73      * Input properties
74      */
75     int i_state;
76
77     block_bytestream_t bytestream;
78
79     /*
80      * Common properties
81      */
82     date_t  end_date;
83
84     mtime_t i_pts;
85
86     vlc_a52_header_t frame;
87 };
88
89 /****************************************************************************
90  * Local prototypes
91  ****************************************************************************/
92 static block_t *DecodeBlock  ( decoder_t *, block_t ** );
93
94 static uint8_t *GetOutBuffer ( decoder_t *, block_t ** );
95 static block_t *GetAoutBuffer( decoder_t * );
96 static block_t *GetSoutBuffer( decoder_t * );
97
98 /*****************************************************************************
99  * OpenCommon: probe the decoder/packetizer and return score
100  *****************************************************************************/
101 static int OpenCommon( vlc_object_t *p_this, bool b_packetizer )
102 {
103     decoder_t *p_dec = (decoder_t*)p_this;
104     decoder_sys_t *p_sys;
105     vlc_fourcc_t i_codec;
106
107     switch( p_dec->fmt_in.i_codec )
108     {
109     case VLC_CODEC_A52:
110         i_codec = VLC_CODEC_A52;
111         break;
112     case VLC_CODEC_EAC3:
113         /* XXX ugly hack, a52 does not support eac3 so no eac3 pass-through
114          * support */
115         if( !b_packetizer )
116             return VLC_EGENERIC;
117         i_codec = VLC_CODEC_EAC3;
118         break;
119     default:
120         return VLC_EGENERIC;
121     }
122
123     /* Allocate the memory needed to store the decoder's structure */
124     if( ( p_dec->p_sys = p_sys =
125           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
126         return VLC_ENOMEM;
127
128     /* Misc init */
129     p_sys->b_packetizer = b_packetizer;
130     p_sys->i_state = STATE_NOSYNC;
131     date_Set( &p_sys->end_date, 0 );
132     p_sys->i_pts = VLC_TS_INVALID;
133
134     block_BytestreamInit( &p_sys->bytestream );
135
136     /* Set output properties */
137     p_dec->fmt_out.i_cat = AUDIO_ES;
138     p_dec->fmt_out.i_codec = i_codec;
139     p_dec->fmt_out.audio.i_rate = 0; /* So end_date gets initialized */
140     p_dec->fmt_out.audio.i_bytes_per_frame = 0;
141
142     /* Set callback */
143     if( b_packetizer )
144         p_dec->pf_packetize    = DecodeBlock;
145     else
146         p_dec->pf_decode_audio = DecodeBlock;
147     return VLC_SUCCESS;
148 }
149
150 static int OpenDecoder( vlc_object_t *p_this )
151 {
152     /* HACK: Don't use this codec if we don't have an a52 audio filter */
153     if( !module_exists( "a52tofloat32" ) )
154         return VLC_EGENERIC;
155     return OpenCommon( p_this, false );
156 }
157
158 static int OpenPacketizer( vlc_object_t *p_this )
159 {
160     return OpenCommon( p_this, true );
161 }
162
163 /****************************************************************************
164  * DecodeBlock: the whole thing
165  ****************************************************************************
166  * This function is called just after the thread is launched.
167  ****************************************************************************/
168 static block_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
169 {
170     decoder_sys_t *p_sys = p_dec->p_sys;
171     uint8_t p_header[VLC_A52_HEADER_SIZE];
172     uint8_t *p_buf;
173     block_t *p_out_buffer;
174
175     if( !pp_block || !*pp_block ) return NULL;
176
177     if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
178     {
179         if( (*pp_block)->i_flags&BLOCK_FLAG_CORRUPTED )
180         {
181             p_sys->i_state = STATE_NOSYNC;
182             block_BytestreamEmpty( &p_sys->bytestream );
183         }
184         date_Set( &p_sys->end_date, 0 );
185         block_Release( *pp_block );
186         return NULL;
187     }
188
189     if( !date_Get( &p_sys->end_date ) && (*pp_block)->i_pts <= VLC_TS_INVALID)
190     {
191         /* We've just started the stream, wait for the first PTS. */
192         block_Release( *pp_block );
193         return NULL;
194     }
195
196     block_BytestreamPush( &p_sys->bytestream, *pp_block );
197
198     while( 1 )
199     {
200         switch( p_sys->i_state )
201         {
202         case STATE_NOSYNC:
203             while( block_PeekBytes( &p_sys->bytestream, p_header, 2 )
204                    == VLC_SUCCESS )
205             {
206                 if( p_header[0] == 0x0b && p_header[1] == 0x77 )
207                 {
208                     p_sys->i_state = STATE_SYNC;
209                     break;
210                 }
211                 block_SkipByte( &p_sys->bytestream );
212             }
213             if( p_sys->i_state != STATE_SYNC )
214             {
215                 block_BytestreamFlush( &p_sys->bytestream );
216
217                 /* Need more data */
218                 return NULL;
219             }
220
221         case STATE_SYNC:
222             /* New frame, set the Presentation Time Stamp */
223             p_sys->i_pts = p_sys->bytestream.p_block->i_pts;
224             if( p_sys->i_pts > VLC_TS_INVALID &&
225                 p_sys->i_pts != date_Get( &p_sys->end_date ) )
226             {
227                 date_Set( &p_sys->end_date, p_sys->i_pts );
228             }
229             p_sys->i_state = STATE_HEADER;
230
231         case STATE_HEADER:
232             /* Get A/52 frame header (VLC_A52_HEADER_SIZE bytes) */
233             if( block_PeekBytes( &p_sys->bytestream, p_header,
234                                  VLC_A52_HEADER_SIZE ) != VLC_SUCCESS )
235             {
236                 /* Need more data */
237                 return NULL;
238             }
239
240             /* Check if frame is valid and get frame info */
241             if( vlc_a52_header_Parse( &p_sys->frame, p_header, VLC_A52_HEADER_SIZE ) )
242             {
243                 msg_Dbg( p_dec, "emulated sync word" );
244                 block_SkipByte( &p_sys->bytestream );
245                 p_sys->i_state = STATE_NOSYNC;
246                 break;
247             }
248
249             p_sys->i_state = STATE_NEXT_SYNC;
250
251         case STATE_NEXT_SYNC:
252             /* TODO: If pp_block == NULL, flush the buffer without checking the
253              * next sync word */
254
255             /* Check if next expected frame contains the sync word */
256             if( block_PeekOffsetBytes( &p_sys->bytestream,
257                                        p_sys->frame.i_size, p_header, 2 )
258                 != VLC_SUCCESS )
259             {
260                 /* Need more data */
261                 return NULL;
262             }
263
264             if( p_sys->b_packetizer &&
265                 p_header[0] == 0 && p_header[1] == 0 )
266             {
267                 /* A52 wav files and audio CD's use stuffing */
268                 p_sys->i_state = STATE_GET_DATA;
269                 break;
270             }
271
272             if( p_header[0] != 0x0b || p_header[1] != 0x77 )
273             {
274                 msg_Dbg( p_dec, "emulated sync word "
275                          "(no sync on following frame)" );
276                 p_sys->i_state = STATE_NOSYNC;
277                 block_SkipByte( &p_sys->bytestream );
278                 break;
279             }
280             p_sys->i_state = STATE_SEND_DATA;
281             break;
282
283         case STATE_GET_DATA:
284             /* Make sure we have enough data.
285              * (Not useful if we went through NEXT_SYNC) */
286             if( block_WaitBytes( &p_sys->bytestream,
287                                  p_sys->frame.i_size ) != VLC_SUCCESS )
288             {
289                 /* Need more data */
290                 return NULL;
291             }
292             p_sys->i_state = STATE_SEND_DATA;
293
294         case STATE_SEND_DATA:
295             if( !(p_buf = GetOutBuffer( p_dec, &p_out_buffer )) )
296             {
297                 //p_dec->b_error = true;
298                 return NULL;
299             }
300
301             /* Copy the whole frame into the buffer. When we reach this point
302              * we already know we have enough data available. */
303             block_GetBytes( &p_sys->bytestream,
304                             p_buf, __MIN( p_sys->frame.i_size, p_out_buffer->i_buffer ) );
305
306             /* Make sure we don't reuse the same pts twice */
307             if( p_sys->i_pts == p_sys->bytestream.p_block->i_pts )
308                 p_sys->i_pts = p_sys->bytestream.p_block->i_pts = VLC_TS_INVALID;
309
310             /* So p_block doesn't get re-added several times */
311             *pp_block = block_BytestreamPop( &p_sys->bytestream );
312
313             p_sys->i_state = STATE_NOSYNC;
314
315             return p_out_buffer;
316         }
317     }
318 }
319
320 /*****************************************************************************
321  * CloseCommon: clean up the decoder
322  *****************************************************************************/
323 static void CloseCommon( vlc_object_t *p_this )
324 {
325     decoder_t *p_dec = (decoder_t*)p_this;
326     decoder_sys_t *p_sys = p_dec->p_sys;
327
328     block_BytestreamRelease( &p_sys->bytestream );
329
330     free( p_sys );
331 }
332
333 /*****************************************************************************
334  * GetOutBuffer:
335  *****************************************************************************/
336 static uint8_t *GetOutBuffer( decoder_t *p_dec, block_t **pp_out_buffer )
337 {
338     decoder_sys_t *p_sys = p_dec->p_sys;
339     uint8_t *p_buf;
340
341     if( p_dec->fmt_out.audio.i_rate != p_sys->frame.i_rate )
342     {
343         msg_Dbg( p_dec, "A/52 channels:%d samplerate:%d bitrate:%d",
344                  p_sys->frame.i_channels, p_sys->frame.i_rate, p_sys->frame.i_bitrate );
345
346         date_Init( &p_sys->end_date, p_sys->frame.i_rate, 1 );
347         date_Set( &p_sys->end_date, p_sys->i_pts );
348     }
349
350     p_dec->fmt_out.audio.i_rate     = p_sys->frame.i_rate;
351     p_dec->fmt_out.audio.i_channels = p_sys->frame.i_channels;
352     if( p_dec->fmt_out.audio.i_bytes_per_frame < p_sys->frame.i_size )
353         p_dec->fmt_out.audio.i_bytes_per_frame = p_sys->frame.i_size;
354     p_dec->fmt_out.audio.i_frame_length = p_sys->frame.i_samples;
355
356     p_dec->fmt_out.audio.i_original_channels = p_sys->frame.i_channels_conf;
357     p_dec->fmt_out.audio.i_physical_channels =
358         p_sys->frame.i_channels_conf & AOUT_CHAN_PHYSMASK;
359
360     p_dec->fmt_out.i_bitrate = p_sys->frame.i_bitrate;
361
362     if( p_sys->b_packetizer )
363     {
364         block_t *p_sout_buffer = GetSoutBuffer( p_dec );
365         p_buf = p_sout_buffer ? p_sout_buffer->p_buffer : NULL;
366         *pp_out_buffer = p_sout_buffer;
367     }
368     else
369     {
370         block_t *p_aout_buffer = GetAoutBuffer( p_dec );
371         p_buf = p_aout_buffer ? p_aout_buffer->p_buffer : NULL;
372         *pp_out_buffer = p_aout_buffer;
373     }
374
375     return p_buf;
376 }
377
378 /*****************************************************************************
379  * GetAoutBuffer:
380  *****************************************************************************/
381 static block_t *GetAoutBuffer( decoder_t *p_dec )
382 {
383     decoder_sys_t *p_sys = p_dec->p_sys;
384
385     block_t *p_buf = decoder_NewAudioBuffer( p_dec, p_sys->frame.i_samples );
386     if( p_buf )
387     {
388         p_buf->i_pts = date_Get( &p_sys->end_date );
389         p_buf->i_length = date_Increment( &p_sys->end_date,
390                                           p_sys->frame.i_samples ) - p_buf->i_pts;
391     }
392
393     return p_buf;
394 }
395
396 /*****************************************************************************
397  * GetSoutBuffer:
398  *****************************************************************************/
399 static block_t *GetSoutBuffer( decoder_t *p_dec )
400 {
401     decoder_sys_t *p_sys = p_dec->p_sys;
402
403     block_t *p_block = block_Alloc( p_sys->frame.i_size );
404     if( p_block )
405     {
406         p_block->i_pts = p_block->i_dts = date_Get( &p_sys->end_date );
407         p_block->i_length =
408             date_Increment( &p_sys->end_date, p_sys->frame.i_samples ) - p_block->i_pts;
409     }
410
411     return p_block;
412 }
413