1 /*****************************************************************************
2 * dts.c: parse DTS audio sync info and packetize the stream
3 *****************************************************************************
4 * Copyright (C) 2001-2016 VLC authors and VideoLAN
6 * Authors: Gildas Bazin <gbazin@videolan.org>
7 * Thomas Guillem <thomas@gllm.fr>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_codec.h>
34 #include <vlc_block_helper.h>
35 #include <vlc_modules.h>
37 #include "dts_header.h"
39 #include "packetizer_helper.h"
41 static int Open( vlc_object_t
* );
42 static void Close( vlc_object_t
* );
45 set_category( CAT_SOUT
)
46 set_subcategory( SUBCAT_SOUT_PACKETIZER
)
47 set_description( N_("DTS audio packetizer") )
48 set_capability( "packetizer", 10 )
49 set_callbacks( Open
, Close
)
59 block_bytestream_t bytestream
;
75 static void PacketizeFlush( decoder_t
*p_dec
)
77 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
79 p_sys
->b_discontinuity
= 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
->dts
.i_rate
)
92 msg_Dbg( p_dec
, "DTS samplerate:%d bitrate:%d",
93 p_sys
->dts
.i_rate
, p_sys
->dts
.i_bitrate
);
95 date_Init( &p_sys
->end_date
, p_sys
->dts
.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
->dts
.i_rate
;
101 if( p_dec
->fmt_out
.audio
.i_bytes_per_frame
< p_sys
->dts
.i_frame_size
)
102 p_dec
->fmt_out
.audio
.i_bytes_per_frame
= p_sys
->dts
.i_frame_size
;
103 p_dec
->fmt_out
.audio
.i_frame_length
= p_sys
->dts
.i_frame_length
;
105 p_dec
->fmt_out
.audio
.i_original_channels
= p_sys
->dts
.i_original_channels
;
106 p_dec
->fmt_out
.audio
.i_physical_channels
=
107 p_sys
->dts
.i_original_channels
& AOUT_CHAN_PHYSMASK
;
108 p_dec
->fmt_out
.audio
.i_channels
=
109 popcount( p_dec
->fmt_out
.audio
.i_physical_channels
);
111 p_dec
->fmt_out
.i_bitrate
= p_sys
->dts
.i_bitrate
;
113 block_t
*p_block
= block_Alloc( p_sys
->i_input_size
);
114 if( p_block
== NULL
)
117 p_block
->i_nb_samples
= p_sys
->dts
.i_frame_length
;
118 p_block
->i_pts
= p_block
->i_dts
= date_Get( &p_sys
->end_date
);
120 date_Increment( &p_sys
->end_date
, p_block
->i_nb_samples
) - p_block
->i_pts
;
124 static block_t
*PacketizeBlock( decoder_t
*p_dec
, block_t
**pp_block
)
126 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
127 uint8_t p_header
[VLC_DTS_HEADER_SIZE
];
128 block_t
*p_out_buffer
;
130 block_t
*p_block
= pp_block
? *pp_block
: NULL
;
134 if ( p_block
->i_flags
& (BLOCK_FLAG_DISCONTINUITY
|BLOCK_FLAG_CORRUPTED
) ) {
135 /* First always drain complete blocks before discontinuity */
136 block_t
*p_drain
= PacketizeBlock( p_dec
, NULL
);
140 PacketizeFlush( p_dec
);
142 if ( p_block
->i_flags
& BLOCK_FLAG_CORRUPTED
) {
143 block_Release( p_block
);
148 if ( !date_Get( &p_sys
->end_date
) && p_block
->i_pts
<= VLC_TS_INVALID
) {
149 /* We've just started the stream, wait for the first PTS. */
150 block_Release( p_block
);
154 block_BytestreamPush( &p_sys
->bytestream
, p_block
);
159 switch( p_sys
->i_state
)
162 while( block_PeekBytes( &p_sys
->bytestream
, p_header
, 6 )
165 if( vlc_dts_header_IsSync( p_header
, 6 ) )
167 p_sys
->i_state
= STATE_SYNC
;
170 block_SkipByte( &p_sys
->bytestream
);
172 if( p_sys
->i_state
!= STATE_SYNC
)
174 block_BytestreamFlush( &p_sys
->bytestream
);
181 /* New frame, set the Presentation Time Stamp */
182 p_sys
->i_pts
= p_sys
->bytestream
.p_block
->i_pts
;
183 if( p_sys
->i_pts
> VLC_TS_INVALID
&&
184 p_sys
->i_pts
!= date_Get( &p_sys
->end_date
) )
186 date_Set( &p_sys
->end_date
, p_sys
->i_pts
);
188 p_sys
->i_state
= STATE_HEADER
;
191 /* Get DTS frame header (VLC_DTS_HEADER_SIZE bytes) */
192 if( block_PeekBytes( &p_sys
->bytestream
, p_header
,
193 VLC_DTS_HEADER_SIZE
) != VLC_SUCCESS
)
199 /* Check if frame is valid and get frame info */
200 if( vlc_dts_header_Parse( &p_sys
->dts
, p_header
,
201 VLC_DTS_HEADER_SIZE
) != VLC_SUCCESS
)
203 msg_Dbg( p_dec
, "emulated sync word" );
204 block_SkipByte( &p_sys
->bytestream
);
205 p_sys
->i_state
= STATE_NOSYNC
;
209 if( p_sys
->dts
.b_substream
)
211 msg_Warn( p_dec
, "substream without the paired core stream, "
213 p_sys
->i_state
= STATE_NOSYNC
;
214 if( block_SkipBytes( &p_sys
->bytestream
,
215 p_sys
->dts
.i_frame_size
) != VLC_SUCCESS
)
220 p_sys
->i_input_size
= p_sys
->i_next_offset
= p_sys
->dts
.i_frame_size
;
221 p_sys
->i_state
= STATE_NEXT_SYNC
;
223 case STATE_NEXT_SYNC
:
224 /* Check if next expected frame contains the sync word */
225 while( p_sys
->i_state
== STATE_NEXT_SYNC
)
227 if( block_PeekOffsetBytes( &p_sys
->bytestream
,
228 p_sys
->i_next_offset
, p_header
,
229 VLC_DTS_HEADER_SIZE
)
232 if( p_block
== NULL
) /* drain */
234 p_sys
->i_state
= STATE_GET_DATA
;
241 if( p_header
[0] == 0 )
243 /* DTS wav files, audio CD's and some mkvs use stuffing */
244 p_sys
->i_next_offset
++;
248 if( !vlc_dts_header_IsSync( p_header
, VLC_DTS_HEADER_SIZE
) )
250 /* Even frame size is likely incorrect FSIZE #18166 */
251 if( (p_sys
->dts
.i_frame_size
% 2) && p_sys
->i_next_offset
> 0 &&
252 block_PeekOffsetBytes( &p_sys
->bytestream
,
253 p_sys
->i_next_offset
- 1, p_header
,
254 VLC_DTS_HEADER_SIZE
) == 0 &&
255 vlc_dts_header_IsSync( p_header
, VLC_DTS_HEADER_SIZE
) )
257 p_sys
->i_input_size
= p_sys
->i_next_offset
= p_sys
->dts
.i_frame_size
- 1;
261 msg_Dbg( p_dec
, "emulated sync word "
262 "(no sync on following frame)" );
263 p_sys
->i_state
= STATE_NOSYNC
;
264 block_SkipByte( &p_sys
->bytestream
);
268 /* Check if a DTS substream packet is located just after
270 if( p_sys
->i_next_offset
== p_sys
->dts
.i_frame_size
)
272 vlc_dts_header_t next_header
;
273 if( vlc_dts_header_Parse( &next_header
, p_header
,
274 VLC_DTS_HEADER_SIZE
)
275 == VLC_SUCCESS
&& next_header
.b_substream
)
277 p_sys
->i_input_size
+= next_header
.i_frame_size
;
280 p_sys
->i_state
= STATE_GET_DATA
;
285 /* Make sure we have enough data. */
286 if( block_WaitBytes( &p_sys
->bytestream
,
287 p_sys
->i_input_size
) != VLC_SUCCESS
)
292 p_sys
->i_state
= STATE_SEND_DATA
;
294 case STATE_SEND_DATA
:
295 if( !(p_out_buffer
= GetOutBuffer( p_dec
)) )
300 /* Copy the whole frame into the buffer. When we reach this point
301 * we already know we have enough data available. */
302 block_GetBytes( &p_sys
->bytestream
, p_out_buffer
->p_buffer
,
303 p_out_buffer
->i_buffer
);
305 /* Make sure we don't reuse the same pts twice */
306 if( p_sys
->i_pts
== p_sys
->bytestream
.p_block
->i_pts
)
307 p_sys
->i_pts
= p_sys
->bytestream
.p_block
->i_pts
= VLC_TS_INVALID
;
309 if( p_sys
->b_discontinuity
)
311 p_sys
->b_discontinuity
= false;
312 p_out_buffer
->i_flags
|= BLOCK_FLAG_DISCONTINUITY
;
315 /* So p_block doesn't get re-added several times */
317 *pp_block
= block_BytestreamPop( &p_sys
->bytestream
);
319 p_sys
->i_state
= STATE_NOSYNC
;
326 static void Close( vlc_object_t
*p_this
)
328 decoder_t
*p_dec
= (decoder_t
*)p_this
;
329 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
331 block_BytestreamRelease( &p_sys
->bytestream
);
336 static int Open( vlc_object_t
*p_this
)
338 decoder_t
*p_dec
= (decoder_t
*)p_this
;
339 decoder_sys_t
*p_sys
;
341 switch( p_dec
->fmt_in
.i_codec
)
349 /* Allocate the memory needed to store the decoder's structure */
350 if( ( p_dec
->p_sys
= p_sys
= malloc(sizeof(decoder_sys_t
)) ) == NULL
)
354 p_sys
->i_state
= STATE_NOSYNC
;
355 date_Set( &p_sys
->end_date
, 0 );
356 p_sys
->i_pts
= VLC_TS_INVALID
;
357 p_sys
->b_date_set
= false;
358 p_sys
->b_discontinuity
= false;
359 memset(&p_sys
->dts
, 0, sizeof(vlc_dts_header_t
));
360 block_BytestreamInit( &p_sys
->bytestream
);
362 /* Set output properties (passthrough only) */
363 p_dec
->fmt_out
.i_cat
= AUDIO_ES
;
364 p_dec
->fmt_out
.i_codec
= p_dec
->fmt_in
.i_codec
;
365 p_dec
->fmt_out
.audio
= p_dec
->fmt_in
.audio
;
368 p_dec
->pf_packetize
= PacketizeBlock
;
369 p_dec
->pf_flush
= PacketizeFlush
;