es: replace i_original_channels with i_chan_mode
[vlc.git] / modules / packetizer / a52.c
blob197ee6c698f753bc17f2fd205d8a6b34da7dc218
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 * $Id$
7 * Authors: Stéphane Borel <stef@via.ecp.fr>
8 * Christophe Massiot <massiot@via.ecp.fr>
9 * Gildas Bazin <gbazin@videolan.org>
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.
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.
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 *****************************************************************************/
26 /*****************************************************************************
27 * Preamble
28 *****************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
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>
39 #include "a52.h"
41 #include "packetizer_helper.h"
43 static int Open( vlc_object_t * );
44 static void Close( vlc_object_t * );
46 vlc_module_begin ()
47 set_category(CAT_SOUT)
48 set_subcategory(SUBCAT_SOUT_PACKETIZER)
49 set_description( N_("A/52 audio packetizer") )
50 set_capability( "packetizer", 10 )
51 set_callbacks( Open, Close )
52 vlc_module_end ()
54 struct decoder_sys_t
57 * Input properties
59 int i_state;
61 block_bytestream_t bytestream;
64 * Common properties
66 date_t end_date;
67 bool b_date_set;
69 mtime_t i_pts;
70 bool b_discontuinity;
72 vlc_a52_header_t frame;
75 static void PacketizeFlush( decoder_t *p_dec )
77 decoder_sys_t *p_sys = p_dec->p_sys;
79 p_sys->b_discontuinity = true;
80 date_Set( &p_sys->end_date, 0 );
81 p_sys->i_state = STATE_NOSYNC;
82 block_BytestreamEmpty( &p_sys->bytestream );
85 static block_t *GetOutBuffer( decoder_t *p_dec )
87 decoder_sys_t *p_sys = p_dec->p_sys;
89 if( !p_sys->b_date_set
90 || p_dec->fmt_out.audio.i_rate != p_sys->frame.i_rate )
92 msg_Dbg( p_dec, "A/52 channels:%d samplerate:%d bitrate:%d",
93 p_sys->frame.i_channels, p_sys->frame.i_rate, p_sys->frame.i_bitrate );
95 date_Init( &p_sys->end_date, p_sys->frame.i_rate, 1 );
96 date_Set( &p_sys->end_date, p_sys->i_pts );
97 p_sys->b_date_set = true;
100 p_dec->fmt_out.audio.i_rate = p_sys->frame.i_rate;
101 p_dec->fmt_out.audio.i_channels = p_sys->frame.i_channels;
102 if( p_dec->fmt_out.audio.i_bytes_per_frame < p_sys->frame.i_size )
103 p_dec->fmt_out.audio.i_bytes_per_frame = p_sys->frame.i_size;
104 p_dec->fmt_out.audio.i_frame_length = p_sys->frame.i_samples;
106 p_dec->fmt_out.audio.i_chan_mode = p_sys->frame.i_chan_mode;
107 p_dec->fmt_out.audio.i_physical_channels = p_sys->frame.i_channels_conf;
109 p_dec->fmt_out.i_bitrate = p_sys->frame.i_bitrate;
111 block_t *p_block = block_Alloc( p_sys->frame.i_size );
112 if( p_block == NULL )
113 return NULL;
115 p_block->i_nb_samples = p_sys->frame.i_samples;
116 p_block->i_pts = p_block->i_dts = date_Get( &p_sys->end_date );
117 p_block->i_length =
118 date_Increment( &p_sys->end_date, p_block->i_nb_samples ) - p_block->i_pts;
119 return p_block;
122 static block_t *PacketizeBlock( decoder_t *p_dec, block_t **pp_block )
124 decoder_sys_t *p_sys = p_dec->p_sys;
125 uint8_t p_header[VLC_A52_HEADER_SIZE];
126 block_t *p_out_buffer;
128 block_t *p_block = pp_block ? *pp_block : NULL;
130 if( p_block )
132 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY | BLOCK_FLAG_CORRUPTED) )
134 /* First always drain complete blocks before discontinuity */
135 block_t *p_drain = PacketizeBlock( p_dec, NULL );
136 if(p_drain)
137 return p_drain;
139 PacketizeFlush( p_dec );
141 if( p_block->i_flags & BLOCK_FLAG_CORRUPTED )
143 block_Release( p_block );
144 return NULL;
148 if( !date_Get( &p_sys->end_date ) && p_block->i_pts <= VLC_TS_INVALID)
150 /* We've just started the stream, wait for the first PTS. */
151 block_Release( p_block );
152 return NULL;
155 block_BytestreamPush( &p_sys->bytestream, p_block );
158 while( 1 )
160 switch( p_sys->i_state )
162 case STATE_NOSYNC:
163 while( block_PeekBytes( &p_sys->bytestream, p_header, 2 )
164 == VLC_SUCCESS )
166 if( p_header[0] == 0x0b && p_header[1] == 0x77 )
168 p_sys->i_state = STATE_SYNC;
169 break;
171 block_SkipByte( &p_sys->bytestream );
173 if( p_sys->i_state != STATE_SYNC )
175 block_BytestreamFlush( &p_sys->bytestream );
177 /* Need more data */
178 return NULL;
181 case STATE_SYNC:
182 /* New frame, set the Presentation Time Stamp */
183 p_sys->i_pts = p_sys->bytestream.p_block->i_pts;
184 if( p_sys->i_pts > VLC_TS_INVALID &&
185 p_sys->i_pts != date_Get( &p_sys->end_date ) )
187 date_Set( &p_sys->end_date, p_sys->i_pts );
189 p_sys->i_state = STATE_HEADER;
191 case STATE_HEADER:
192 /* Get A/52 frame header (VLC_A52_HEADER_SIZE bytes) */
193 if( block_PeekBytes( &p_sys->bytestream, p_header,
194 VLC_A52_HEADER_SIZE ) != VLC_SUCCESS )
196 /* Need more data */
197 return NULL;
200 /* Check if frame is valid and get frame info */
201 vlc_a52_header_t a52;
202 if( vlc_a52_header_Parse( &a52, p_header, VLC_A52_HEADER_SIZE ) )
204 msg_Dbg( p_dec, "emulated sync word" );
205 block_SkipByte( &p_sys->bytestream );
206 p_sys->i_state = STATE_NOSYNC;
207 break;
210 if( a52.b_eac3 && a52.eac3.strmtyp != EAC3_STRMTYP_INDEPENDENT )
212 /* Use the channel configuration of the independent stream */
213 p_sys->frame.i_samples = a52.i_samples;
214 p_sys->frame.i_size = a52.i_size;
216 else
217 p_sys->frame = a52;
219 p_sys->i_state = STATE_NEXT_SYNC;
221 case STATE_NEXT_SYNC:
222 /* Check if next expected frame contains the sync word */
223 if( block_PeekOffsetBytes( &p_sys->bytestream,
224 p_sys->frame.i_size, p_header, 2 )
225 != VLC_SUCCESS )
227 if( p_block == NULL ) /* drain */
229 p_sys->i_state = STATE_GET_DATA;
230 break;
232 /* Need more data */
233 return NULL;
236 if( p_header[0] == 0 && p_header[1] == 0 )
238 /* A52 wav files and audio CD's use stuffing */
239 p_sys->i_state = STATE_GET_DATA;
240 break;
243 if( p_header[0] != 0x0b || p_header[1] != 0x77 )
245 msg_Dbg( p_dec, "emulated sync word "
246 "(no sync on following frame)" );
247 p_sys->i_state = STATE_NOSYNC;
248 block_SkipByte( &p_sys->bytestream );
249 break;
251 p_sys->i_state = STATE_GET_DATA;
252 break;
254 case STATE_GET_DATA:
255 /* Make sure we have enough data. */
256 if( block_WaitBytes( &p_sys->bytestream,
257 p_sys->frame.i_size ) != VLC_SUCCESS )
259 /* Need more data */
260 return NULL;
262 p_sys->i_state = STATE_SEND_DATA;
264 case STATE_SEND_DATA:
265 if( !(p_out_buffer = GetOutBuffer( p_dec )) )
267 return NULL;
270 /* Copy the whole frame into the buffer. When we reach this point
271 * we already know we have enough data available. */
272 block_GetBytes( &p_sys->bytestream, p_out_buffer->p_buffer,
273 __MIN( p_sys->frame.i_size, p_out_buffer->i_buffer ) );
275 /* Make sure we don't reuse the same pts twice */
276 if( p_sys->i_pts == p_sys->bytestream.p_block->i_pts )
277 p_sys->i_pts = p_sys->bytestream.p_block->i_pts = VLC_TS_INVALID;
279 if( p_sys->b_discontuinity )
281 p_out_buffer->i_flags |= BLOCK_FLAG_DISCONTINUITY;
282 p_sys->b_discontuinity = false;
285 /* So p_block doesn't get re-added several times */
286 if( pp_block )
287 *pp_block = block_BytestreamPop( &p_sys->bytestream );
289 p_sys->i_state = STATE_NOSYNC;
291 return p_out_buffer;
296 static void Close( vlc_object_t *p_this )
298 decoder_t *p_dec = (decoder_t*)p_this;
299 decoder_sys_t *p_sys = p_dec->p_sys;
301 block_BytestreamRelease( &p_sys->bytestream );
303 free( p_sys );
306 static int Open( vlc_object_t *p_this )
308 decoder_t *p_dec = (decoder_t*)p_this;
309 decoder_sys_t *p_sys;
311 switch( p_dec->fmt_in.i_codec )
313 case VLC_CODEC_EAC3:
314 case VLC_CODEC_A52:
315 break;
316 default:
317 return VLC_EGENERIC;
320 /* Allocate the memory needed to store the decoder's structure */
321 if( ( p_dec->p_sys = p_sys =
322 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
323 return VLC_ENOMEM;
325 /* Misc init */
326 p_sys->i_state = STATE_NOSYNC;
327 date_Set( &p_sys->end_date, 0 );
328 p_sys->i_pts = VLC_TS_INVALID;
329 p_sys->b_date_set = false;
330 p_sys->b_discontuinity = false;
331 memset(&p_sys->frame, 0, sizeof(vlc_a52_header_t));
333 block_BytestreamInit( &p_sys->bytestream );
335 /* Set output properties (Passthrough ONLY) */
336 p_dec->fmt_out.i_codec = p_dec->fmt_in.i_codec;
337 p_dec->fmt_out.audio = p_dec->fmt_in.audio;
339 /* Set callback */
340 p_dec->pf_packetize = PacketizeBlock;
341 p_dec->pf_flush = PacketizeFlush;
342 return VLC_SUCCESS;