1 /*****************************************************************************
2 * a52.c: parse A/52 audio sync info and packetize the stream
3 *****************************************************************************
4 * Copyright (C) 2001-2016 VLC authors and VideoLAN
6 * Authors: Stéphane Borel <stef@via.ecp.fr>
7 * Christophe Massiot <massiot@via.ecp.fr>
8 * Gildas Bazin <gbazin@videolan.org>
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
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>
42 #include "packetizer_helper.h"
44 static int Open( vlc_object_t
* );
45 static void Close( vlc_object_t
* );
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
)
62 block_bytestream_t bytestream
;
68 vlc_tick_t i_prev_bytestream_pts
;
71 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
, 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
);
86 static block_t
*GetOutBuffer( decoder_t
*p_dec
)
88 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
90 assert( p_sys
->frame
.i_rate
> 0 );
92 block_t
*p_block
= block_Alloc( p_sys
->i_input_size
);
96 if( p_dec
->fmt_out
.audio
.i_rate
!= p_sys
->frame
.i_rate
)
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 );
103 date_Init( &p_sys
->end_date
, p_sys
->frame
.i_rate
, 1 );
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
)
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 date_Set( &p_sys
->end_date
, p_sys
->bytestream
.p_block
->i_pts
);
113 p_sys
->i_prev_bytestream_pts
= p_sys
->bytestream
.p_block
->i_pts
;
114 p_sys
->bytestream
.p_block
->i_pts
= VLC_TICK_INVALID
;
117 p_dec
->fmt_out
.audio
.i_rate
= p_sys
->frame
.i_rate
;
118 p_dec
->fmt_out
.audio
.i_channels
= p_sys
->frame
.i_channels
;
119 if( p_dec
->fmt_out
.audio
.i_bytes_per_frame
< p_sys
->frame
.i_size
)
120 p_dec
->fmt_out
.audio
.i_bytes_per_frame
= p_sys
->frame
.i_size
;
121 p_dec
->fmt_out
.audio
.i_frame_length
= p_sys
->frame
.i_samples
;
123 p_dec
->fmt_out
.audio
.i_chan_mode
= p_sys
->frame
.i_chan_mode
;
124 p_dec
->fmt_out
.audio
.i_physical_channels
= p_sys
->frame
.i_channels_conf
;
126 p_dec
->fmt_out
.i_bitrate
= p_sys
->frame
.i_bitrate
;
128 p_block
->i_nb_samples
= p_sys
->frame
.i_samples
;
129 p_block
->i_pts
= p_block
->i_dts
= date_Get( &p_sys
->end_date
);
130 if( p_block
->i_pts
!= VLC_TICK_INVALID
)
131 p_block
->i_length
= date_Increment( &p_sys
->end_date
,
132 p_block
->i_nb_samples
) - p_block
->i_pts
;
137 static block_t
*PacketizeBlock( decoder_t
*p_dec
, block_t
**pp_block
)
139 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
140 uint8_t p_header
[VLC_A52_MIN_HEADER_SIZE
];
141 block_t
*p_out_buffer
;
143 block_t
*p_block
= pp_block
? *pp_block
: NULL
;
147 if( p_block
->i_flags
& (BLOCK_FLAG_DISCONTINUITY
| BLOCK_FLAG_CORRUPTED
) )
149 /* First always drain complete blocks before discontinuity */
150 block_t
*p_drain
= PacketizeBlock( p_dec
, NULL
);
154 PacketizeFlush( p_dec
);
156 if( p_block
->i_flags
& BLOCK_FLAG_CORRUPTED
)
158 block_Release( p_block
);
163 block_BytestreamPush( &p_sys
->bytestream
, p_block
);
168 switch( p_sys
->i_state
)
171 while( block_PeekBytes( &p_sys
->bytestream
, p_header
, 2 )
174 if( p_header
[0] == 0x0b && p_header
[1] == 0x77 )
176 p_sys
->i_state
= STATE_SYNC
;
179 block_SkipByte( &p_sys
->bytestream
);
181 if( p_sys
->i_state
!= STATE_SYNC
)
183 block_BytestreamFlush( &p_sys
->bytestream
);
191 p_sys
->i_state
= STATE_HEADER
;
195 /* Get A/52 frame header (VLC_A52_HEADER_SIZE bytes) */
196 if( block_PeekBytes( &p_sys
->bytestream
, p_header
,
197 VLC_A52_MIN_HEADER_SIZE
) != VLC_SUCCESS
)
203 /* Check if frame is valid and get frame info */
204 if( vlc_a52_header_Parse( &p_sys
->frame
, p_header
,
205 VLC_A52_MIN_HEADER_SIZE
) != VLC_SUCCESS
)
207 msg_Dbg( p_dec
, "emulated sync word" );
208 block_SkipByte( &p_sys
->bytestream
);
209 p_sys
->i_state
= STATE_NOSYNC
;
213 if( p_sys
->frame
.b_eac3
&& p_sys
->frame
.bs
.eac3
.strmtyp
== EAC3_STRMTYP_DEPENDENT
)
215 msg_Warn( p_dec
, "starting with dependent stream, skip it" );
216 p_sys
->i_state
= STATE_NOSYNC
;
217 if( block_SkipBytes( &p_sys
->bytestream
,
218 p_sys
->frame
.i_size
) != VLC_SUCCESS
)
223 p_sys
->i_input_size
= p_sys
->frame
.i_size
;
224 p_sys
->i_state
= STATE_NEXT_SYNC
;
227 case STATE_NEXT_SYNC
:
228 /* Check if next expected frame contains the sync word */
229 if( block_PeekOffsetBytes( &p_sys
->bytestream
, p_sys
->i_input_size
,
230 p_header
, VLC_A52_MIN_HEADER_SIZE
)
233 if( p_block
== NULL
) /* drain */
235 p_sys
->i_state
= STATE_GET_DATA
;
242 if( p_header
[0] == 0 || p_header
[1] == 0 )
244 /* A52 wav files and audio CD's use stuffing */
245 p_sys
->i_state
= STATE_GET_DATA
;
249 if( p_header
[0] != 0x0b || p_header
[1] != 0x77 )
251 msg_Dbg( p_dec
, "emulated sync word "
252 "(no sync on following frame)" );
253 p_sys
->i_state
= STATE_NOSYNC
;
254 block_SkipByte( &p_sys
->bytestream
);
258 vlc_a52_header_t a52
;
259 if( !vlc_a52_header_Parse( &a52
, p_header
, VLC_A52_MIN_HEADER_SIZE
)
260 && a52
.b_eac3
&& a52
.bs
.eac3
.strmtyp
== EAC3_STRMTYP_DEPENDENT
)
262 p_sys
->i_input_size
+= a52
.i_size
;
263 p_dec
->fmt_out
.i_codec
= VLC_CODEC_A52
;
264 p_dec
->fmt_out
.i_profile
= VLC_A52_PROFILE_EAC3_DEPENDENT
;
267 p_sys
->i_state
= STATE_GET_DATA
;
271 /* Make sure we have enough data. */
272 if( block_WaitBytes( &p_sys
->bytestream
,
273 p_sys
->i_input_size
) != VLC_SUCCESS
)
278 p_sys
->i_state
= STATE_SEND_DATA
;
281 case STATE_SEND_DATA
:
282 if( !(p_out_buffer
= GetOutBuffer( p_dec
)) )
287 /* Copy the whole frame into the buffer. When we reach this point
288 * we already know we have enough data available. */
289 block_GetBytes( &p_sys
->bytestream
, p_out_buffer
->p_buffer
,
290 p_out_buffer
->i_buffer
);
292 p_sys
->i_state
= STATE_NOSYNC
;
294 if( p_out_buffer
->i_dts
== VLC_TICK_INVALID
)
296 block_Release( p_out_buffer
);
300 if( p_sys
->b_discontuinity
)
302 p_out_buffer
->i_flags
|= BLOCK_FLAG_DISCONTINUITY
;
303 p_sys
->b_discontuinity
= false;
306 /* So p_block doesn't get re-added several times */
308 *pp_block
= block_BytestreamPop( &p_sys
->bytestream
);
315 static void Close( vlc_object_t
*p_this
)
317 decoder_t
*p_dec
= (decoder_t
*)p_this
;
318 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
320 block_BytestreamRelease( &p_sys
->bytestream
);
325 static int Open( vlc_object_t
*p_this
)
327 decoder_t
*p_dec
= (decoder_t
*)p_this
;
328 decoder_sys_t
*p_sys
;
330 switch( p_dec
->fmt_in
.i_codec
)
339 /* Allocate the memory needed to store the decoder's structure */
340 if( ( p_dec
->p_sys
= p_sys
=
341 (decoder_sys_t
*)malloc(sizeof(decoder_sys_t
)) ) == NULL
)
345 p_sys
->i_state
= STATE_NOSYNC
;
346 date_Set( &p_sys
->end_date
, VLC_TICK_INVALID
);
347 p_sys
->b_discontuinity
= false;
348 p_sys
->i_prev_bytestream_pts
= VLC_TICK_INVALID
;
349 memset(&p_sys
->frame
, 0, sizeof(vlc_a52_header_t
));
351 block_BytestreamInit( &p_sys
->bytestream
);
353 /* Set output properties (Passthrough ONLY) */
354 p_dec
->fmt_out
.i_codec
= p_dec
->fmt_in
.i_codec
;
355 p_dec
->fmt_out
.audio
= p_dec
->fmt_in
.audio
;
356 p_dec
->fmt_out
.audio
.i_rate
= 0;
359 p_dec
->pf_packetize
= PacketizeBlock
;
360 p_dec
->pf_flush
= PacketizeFlush
;
361 p_dec
->pf_get_cc
= NULL
;