bf30edaa6122f17005c081dbf68146638d949636
[vlc.git] / modules / packetizer / a52.c
1 /*****************************************************************************
2  * a52.c: parse A/52 audio sync info and packetize the stream
3  *****************************************************************************
4  * Copyright (C) 2001-2016 VLC authors and VideoLAN
5  *
6  * Authors: St├ęphane Borel <stef@via.ecp.fr>
7  *          Christophe Massiot <massiot@via.ecp.fr>
8  *          Gildas Bazin <gbazin@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <assert.h>
33
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_codec.h>
37 #include <vlc_block_helper.h>
38 #include <vlc_modules.h>
39
40 #include "a52.h"
41
42 #include "packetizer_helper.h"
43
44 static int  Open( vlc_object_t * );
45 static void Close( vlc_object_t * );
46
47 vlc_module_begin ()
48     set_category(CAT_SOUT)
49     set_subcategory(SUBCAT_SOUT_PACKETIZER)
50     set_description( N_("A/52 audio packetizer") )
51     set_capability( "packetizer", 10 )
52     set_callbacks( Open, Close )
53 vlc_module_end ()
54
55 typedef struct
56 {
57     /*
58      * Input properties
59      */
60     int i_state;
61
62     block_bytestream_t bytestream;
63
64     /*
65      * Common properties
66      */
67     date_t  end_date;
68     vlc_tick_t i_prev_bytestream_pts;
69     bool    b_discontuinity;
70
71     vlc_a52_header_t frame;
72     size_t  i_input_size;
73 } decoder_sys_t;
74
75 static void PacketizeFlush( decoder_t *p_dec )
76 {
77     decoder_sys_t *p_sys = p_dec->p_sys;
78
79     p_sys->b_discontuinity = true;
80     date_Set( &p_sys->end_date, VLC_TICK_INVALID );
81     p_sys->i_state = STATE_NOSYNC;
82     p_sys->i_prev_bytestream_pts = VLC_TICK_INVALID;
83     block_BytestreamEmpty( &p_sys->bytestream );
84 }
85
86 static block_t *GetOutBuffer( decoder_t *p_dec )
87 {
88     decoder_sys_t *p_sys = p_dec->p_sys;
89
90     assert( p_sys->frame.i_rate > 0 );
91
92     block_t *p_block = block_Alloc( p_sys->i_input_size );
93     if( p_block == NULL )
94         return NULL;
95
96     if( p_dec->fmt_out.audio.i_rate != p_sys->frame.i_rate )
97     {
98         msg_Dbg( p_dec, "A/52 channels:%d samplerate:%d bitrate:%d",
99                  p_sys->frame.i_channels, p_sys->frame.i_rate, p_sys->frame.i_bitrate );
100         if( p_sys->end_date.i_divider_num )
101             date_Change( &p_sys->end_date, p_sys->frame.i_rate, 1 );
102         else
103             date_Init( &p_sys->end_date, p_sys->frame.i_rate, 1 );
104     }
105
106     if( p_sys->bytestream.p_block->i_pts != date_Get( &p_sys->end_date ) &&
107         p_sys->bytestream.p_block->i_pts != VLC_TICK_INVALID )
108     {
109         /* Make sure we don't reuse the same pts twice
110          * as A/52 in PES sends multiple times the same pts */
111         if( p_sys->bytestream.p_block->i_pts != p_sys->i_prev_bytestream_pts )
112         {
113             date_t tempdate = p_sys->end_date;
114             date_Increment( &tempdate, p_sys->frame.i_samples );
115             vlc_tick_t samplesduration = date_Get( &tempdate ) - date_Get( &p_sys->end_date );
116             if( llabs(p_sys->bytestream.p_block->i_pts  - date_Get( &p_sys->end_date )) > samplesduration )
117                 date_Set( &p_sys->end_date, p_sys->bytestream.p_block->i_pts );
118             p_sys->i_prev_bytestream_pts = p_sys->bytestream.p_block->i_pts;
119             p_sys->bytestream.p_block->i_pts = VLC_TICK_INVALID;
120         }
121     }
122
123     p_dec->fmt_out.audio.i_rate     = p_sys->frame.i_rate;
124     p_dec->fmt_out.audio.i_channels = p_sys->frame.i_channels;
125     if( p_dec->fmt_out.audio.i_bytes_per_frame < p_sys->frame.i_size )
126         p_dec->fmt_out.audio.i_bytes_per_frame = p_sys->frame.i_size;
127     p_dec->fmt_out.audio.i_frame_length = p_sys->frame.i_samples;
128
129     p_dec->fmt_out.audio.i_chan_mode = p_sys->frame.i_chan_mode;
130     p_dec->fmt_out.audio.i_physical_channels = p_sys->frame.i_channels_conf;
131
132     p_dec->fmt_out.i_bitrate = p_sys->frame.i_bitrate;
133
134     p_block->i_nb_samples = p_sys->frame.i_samples;
135     p_block->i_pts = p_block->i_dts = date_Get( &p_sys->end_date );
136     if( p_block->i_pts != VLC_TICK_INVALID )
137         p_block->i_length = date_Increment( &p_sys->end_date,
138                                             p_block->i_nb_samples ) - p_block->i_pts;
139
140     return p_block;
141 }
142
143 static block_t *PacketizeBlock( decoder_t *p_dec, block_t **pp_block )
144 {
145     decoder_sys_t *p_sys = p_dec->p_sys;
146     uint8_t p_header[VLC_A52_MIN_HEADER_SIZE];
147     block_t *p_out_buffer;
148
149     block_t *p_block = pp_block ? *pp_block : NULL;
150
151     if( p_block )
152     {
153         if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY | BLOCK_FLAG_CORRUPTED) )
154         {
155             /* First always drain complete blocks before discontinuity */
156             block_t *p_drain = PacketizeBlock( p_dec, NULL );
157             if(p_drain)
158                 return p_drain;
159
160             PacketizeFlush( p_dec );
161
162             if( p_block->i_flags & BLOCK_FLAG_CORRUPTED )
163             {
164                 block_Release( p_block );
165                 return NULL;
166             }
167         }
168
169         block_BytestreamPush( &p_sys->bytestream, p_block );
170     }
171
172     while( 1 )
173     {
174         switch( p_sys->i_state )
175         {
176         case STATE_NOSYNC:
177             while( block_PeekBytes( &p_sys->bytestream, p_header, 2 )
178                    == VLC_SUCCESS )
179             {
180                 if( p_header[0] == 0x0b && p_header[1] == 0x77 )
181                 {
182                     p_sys->i_state = STATE_SYNC;
183                     break;
184                 }
185                 block_SkipByte( &p_sys->bytestream );
186             }
187             if( p_sys->i_state != STATE_SYNC )
188             {
189                 block_BytestreamFlush( &p_sys->bytestream );
190
191                 /* Need more data */
192                 return NULL;
193             }
194             /* fallthrough */
195
196         case STATE_SYNC:
197             p_sys->i_state = STATE_HEADER;
198             /* fallthrough */
199
200         case STATE_HEADER:
201             /* Get A/52 frame header (VLC_A52_HEADER_SIZE bytes) */
202             if( block_PeekBytes( &p_sys->bytestream, p_header,
203                                  VLC_A52_MIN_HEADER_SIZE ) != VLC_SUCCESS )
204             {
205                 /* Need more data */
206                 return NULL;
207             }
208
209             /* Check if frame is valid and get frame info */
210             if( vlc_a52_header_Parse( &p_sys->frame, p_header,
211                                       VLC_A52_MIN_HEADER_SIZE ) != VLC_SUCCESS )
212             {
213                 msg_Dbg( p_dec, "emulated sync word" );
214                 block_SkipByte( &p_sys->bytestream );
215                 p_sys->i_state = STATE_NOSYNC;
216                 break;
217             }
218
219             if( p_sys->frame.b_eac3 && p_sys->frame.bs.eac3.strmtyp == EAC3_STRMTYP_DEPENDENT )
220             {
221                 msg_Warn( p_dec, "starting with dependent stream, skip it" );
222                 p_sys->i_state = STATE_NOSYNC;
223                 if( block_SkipBytes( &p_sys->bytestream,
224                                      p_sys->frame.i_size ) != VLC_SUCCESS )
225                     return NULL;
226                 break;
227             }
228
229             p_sys->i_input_size = p_sys->frame.i_size;
230             p_sys->i_state = STATE_NEXT_SYNC;
231             /* fallthrough */
232
233         case STATE_NEXT_SYNC:
234             /* Check if next expected frame contains the sync word */
235             if( block_PeekOffsetBytes( &p_sys->bytestream, p_sys->i_input_size,
236                                        p_header, VLC_A52_MIN_HEADER_SIZE )
237                                        != VLC_SUCCESS )
238             {
239                 if( p_block == NULL ) /* drain */
240                 {
241                     p_sys->i_state = STATE_GET_DATA;
242                     break;
243                 }
244                 /* Need more data */
245                 return NULL;
246             }
247
248             if( p_header[0] == 0 || p_header[1] == 0 )
249             {
250                 /* A52 wav files and audio CD's use stuffing */
251                 p_sys->i_state = STATE_GET_DATA;
252                 break;
253             }
254
255             if( p_header[0] != 0x0b || p_header[1] != 0x77 )
256             {
257                 msg_Dbg( p_dec, "emulated sync word "
258                          "(no sync on following frame)" );
259                 p_sys->i_state = STATE_NOSYNC;
260                 block_SkipByte( &p_sys->bytestream );
261                 break;
262             }
263
264             vlc_a52_header_t a52;
265             if( !vlc_a52_header_Parse( &a52, p_header, VLC_A52_MIN_HEADER_SIZE )
266              && a52.b_eac3 && a52.bs.eac3.strmtyp == EAC3_STRMTYP_DEPENDENT )
267             {
268                 p_sys->i_input_size += a52.i_size;
269                 p_dec->fmt_out.i_codec = VLC_CODEC_A52;
270                 p_dec->fmt_out.i_profile = VLC_A52_PROFILE_EAC3_DEPENDENT;
271             }
272
273             p_sys->i_state = STATE_GET_DATA;
274             break;
275
276         case STATE_GET_DATA:
277             /* Make sure we have enough data. */
278             if( block_WaitBytes( &p_sys->bytestream,
279                                  p_sys->i_input_size ) != VLC_SUCCESS )
280             {
281                 /* Need more data */
282                 return NULL;
283             }
284             p_sys->i_state = STATE_SEND_DATA;
285             /* fallthrough */
286
287         case STATE_SEND_DATA:
288             if( !(p_out_buffer = GetOutBuffer( p_dec )) )
289             {
290                 return NULL;
291             }
292
293             /* Copy the whole frame into the buffer. When we reach this point
294              * we already know we have enough data available. */
295             block_GetBytes( &p_sys->bytestream, p_out_buffer->p_buffer,
296                             p_out_buffer->i_buffer );
297
298             p_sys->i_state = STATE_NOSYNC;
299
300             if( p_out_buffer->i_dts == VLC_TICK_INVALID )
301             {
302                 block_Release( p_out_buffer );
303                 return NULL;
304             }
305
306             if( p_sys->b_discontuinity )
307             {
308                 p_out_buffer->i_flags |= BLOCK_FLAG_DISCONTINUITY;
309                 p_sys->b_discontuinity = false;
310             }
311
312             /* So p_block doesn't get re-added several times */
313             if( pp_block )
314                 *pp_block = block_BytestreamPop( &p_sys->bytestream );
315
316             return p_out_buffer;
317         }
318     }
319 }
320
321 static void Close( vlc_object_t *p_this )
322 {
323     decoder_t *p_dec = (decoder_t*)p_this;
324     decoder_sys_t *p_sys = p_dec->p_sys;
325
326     block_BytestreamRelease( &p_sys->bytestream );
327
328     free( p_sys );
329 }
330
331 static int Open( vlc_object_t *p_this )
332 {
333     decoder_t *p_dec = (decoder_t*)p_this;
334     decoder_sys_t *p_sys;
335
336     switch( p_dec->fmt_in.i_codec )
337     {
338     case VLC_CODEC_EAC3:
339     case VLC_CODEC_A52:
340         break;
341     default:
342         return VLC_EGENERIC;
343     }
344
345     /* Allocate the memory needed to store the decoder's structure */
346     if( ( p_dec->p_sys = p_sys =
347           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
348         return VLC_ENOMEM;
349
350     /* Misc init */
351     p_sys->i_state = STATE_NOSYNC;
352     date_Set( &p_sys->end_date, VLC_TICK_INVALID );
353     p_sys->b_discontuinity = false;
354     p_sys->i_prev_bytestream_pts = VLC_TICK_INVALID;
355     memset(&p_sys->frame, 0, sizeof(vlc_a52_header_t));
356
357     block_BytestreamInit( &p_sys->bytestream );
358
359     /* Set output properties (Passthrough ONLY) */
360     p_dec->fmt_out.i_codec = p_dec->fmt_in.i_codec;
361     p_dec->fmt_out.audio = p_dec->fmt_in.audio;
362     p_dec->fmt_out.audio.i_rate = 0;
363
364     /* Set callback */
365     p_dec->pf_packetize = PacketizeBlock;
366     p_dec->pf_flush     = PacketizeFlush;
367     p_dec->pf_get_cc    = NULL;
368     return VLC_SUCCESS;
369 }