dlna: add PrepareForConnection action
[vlc.git] / modules / packetizer / dts.c
blob89cb202523479160d258fabdddb88203f8bb93d6
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 /*****************************************************************************
25 * Preamble
26 *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
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 * );
44 vlc_module_begin ()
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 )
50 vlc_module_end ()
52 typedef struct
55 * Input properties
57 int i_state;
59 block_bytestream_t bytestream;
60 size_t i_next_offset;
63 * Common properties
65 date_t end_date;
66 bool b_date_set;
68 vlc_tick_t i_pts;
69 bool b_discontinuity;
71 vlc_dts_header_t dts;
72 size_t i_input_size;
73 } decoder_sys_t;
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, VLC_TICK_INVALID );
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_chan_mode = p_sys->dts.i_chan_mode;
106 p_dec->fmt_out.audio.i_physical_channels = p_sys->dts.i_physical_channels;
107 p_dec->fmt_out.audio.i_channels =
108 vlc_popcount( p_dec->fmt_out.audio.i_physical_channels );
110 p_dec->fmt_out.i_bitrate = p_sys->dts.i_bitrate;
112 block_t *p_block = block_Alloc( p_sys->i_input_size );
113 if( p_block == NULL )
114 return NULL;
116 p_block->i_nb_samples = p_sys->dts.i_frame_length;
117 p_block->i_pts = p_block->i_dts = date_Get( &p_sys->end_date );
118 p_block->i_length =
119 date_Increment( &p_sys->end_date, p_block->i_nb_samples ) - p_block->i_pts;
120 return p_block;
123 static block_t *PacketizeBlock( decoder_t *p_dec, block_t **pp_block )
125 decoder_sys_t *p_sys = p_dec->p_sys;
126 uint8_t p_header[VLC_DTS_HEADER_SIZE];
127 block_t *p_out_buffer;
129 block_t *p_block = pp_block ? *pp_block : NULL;
131 if( p_block )
133 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 ) {
142 block_Release( p_block );
143 return NULL;
147 if ( p_block->i_pts == VLC_TICK_INVALID &&
148 date_Get( &p_sys->end_date ) == VLC_TICK_INVALID ) {
149 /* We've just started the stream, wait for the first PTS. */
150 block_Release( p_block );
151 return NULL;
154 block_BytestreamPush( &p_sys->bytestream, p_block );
157 while( 1 )
159 switch( p_sys->i_state )
161 case STATE_NOSYNC:
162 while( block_PeekBytes( &p_sys->bytestream, p_header, 6 )
163 == VLC_SUCCESS )
165 if( vlc_dts_header_IsSync( p_header, 6 ) )
167 p_sys->i_state = STATE_SYNC;
168 break;
170 block_SkipByte( &p_sys->bytestream );
172 if( p_sys->i_state != STATE_SYNC )
174 block_BytestreamFlush( &p_sys->bytestream );
176 /* Need more data */
177 return NULL;
179 /* fallthrough */
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_TICK_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;
190 /* fallthrough */
192 case STATE_HEADER:
193 /* Get DTS frame header (VLC_DTS_HEADER_SIZE bytes) */
194 if( block_PeekBytes( &p_sys->bytestream, p_header,
195 VLC_DTS_HEADER_SIZE ) != VLC_SUCCESS )
197 /* Need more data */
198 return NULL;
201 /* Check if frame is valid and get frame info */
202 if( vlc_dts_header_Parse( &p_sys->dts, p_header,
203 VLC_DTS_HEADER_SIZE ) != VLC_SUCCESS )
205 msg_Dbg( p_dec, "emulated sync word" );
206 block_SkipByte( &p_sys->bytestream );
207 p_sys->i_state = STATE_NOSYNC;
208 break;
211 if( p_sys->dts.b_substream )
213 msg_Warn( p_dec, "substream without the paired core stream, "
214 "skip it" );
215 p_sys->i_state = STATE_NOSYNC;
216 if( block_SkipBytes( &p_sys->bytestream,
217 p_sys->dts.i_frame_size ) != VLC_SUCCESS )
218 return NULL;
219 break;
222 p_sys->i_input_size = p_sys->i_next_offset = p_sys->dts.i_frame_size;
223 p_sys->i_state = STATE_NEXT_SYNC;
224 /* fallthrough */
226 case STATE_NEXT_SYNC:
227 /* Check if next expected frame contains the sync word */
228 while( p_sys->i_state == STATE_NEXT_SYNC )
230 if( block_PeekOffsetBytes( &p_sys->bytestream,
231 p_sys->i_next_offset, p_header,
232 VLC_DTS_HEADER_SIZE )
233 != VLC_SUCCESS )
235 if( p_block == NULL ) /* drain */
237 p_sys->i_state = STATE_GET_DATA;
238 break;
240 /* Need more data */
241 return NULL;
244 if( p_header[0] == 0 )
246 /* DTS wav files, audio CD's and some mkvs use stuffing */
247 p_sys->i_next_offset++;
248 continue;
251 if( !vlc_dts_header_IsSync( p_header, VLC_DTS_HEADER_SIZE ) )
253 /* Even frame size is likely incorrect FSIZE #18166 */
254 if( (p_sys->dts.i_frame_size % 2) && p_sys->i_next_offset > 0 &&
255 block_PeekOffsetBytes( &p_sys->bytestream,
256 p_sys->i_next_offset - 1, p_header,
257 VLC_DTS_HEADER_SIZE ) == 0 &&
258 vlc_dts_header_IsSync( p_header, VLC_DTS_HEADER_SIZE ) )
260 p_sys->i_input_size = p_sys->i_next_offset = p_sys->dts.i_frame_size - 1;
261 /* reenter */
262 break;
264 msg_Dbg( p_dec, "emulated sync word "
265 "(no sync on following frame)" );
266 p_sys->i_state = STATE_NOSYNC;
267 block_SkipByte( &p_sys->bytestream );
268 break;
271 /* Check if a DTS substream packet is located just after
272 * the core packet */
273 if( p_sys->i_next_offset == p_sys->dts.i_frame_size )
275 vlc_dts_header_t next_header;
276 if( vlc_dts_header_Parse( &next_header, p_header,
277 VLC_DTS_HEADER_SIZE )
278 == VLC_SUCCESS && next_header.b_substream )
280 p_dec->fmt_out.i_profile = PROFILE_DTS_HD;
281 p_sys->i_input_size += next_header.i_frame_size;
284 p_sys->i_state = STATE_GET_DATA;
286 break;
288 case STATE_GET_DATA:
289 /* Make sure we have enough data. */
290 if( block_WaitBytes( &p_sys->bytestream,
291 p_sys->i_input_size ) != VLC_SUCCESS )
293 /* Need more data */
294 return NULL;
296 p_sys->i_state = STATE_SEND_DATA;
297 /* fallthrough */
299 case STATE_SEND_DATA:
300 if( !(p_out_buffer = GetOutBuffer( p_dec )) )
302 return NULL;
305 /* Copy the whole frame into the buffer. When we reach this point
306 * we already know we have enough data available. */
307 block_GetBytes( &p_sys->bytestream, p_out_buffer->p_buffer,
308 p_out_buffer->i_buffer );
310 /* Make sure we don't reuse the same pts twice */
311 if( p_sys->i_pts == p_sys->bytestream.p_block->i_pts )
312 p_sys->i_pts = p_sys->bytestream.p_block->i_pts = VLC_TICK_INVALID;
314 if( p_sys->b_discontinuity )
316 p_sys->b_discontinuity = false;
317 p_out_buffer->i_flags |= BLOCK_FLAG_DISCONTINUITY;
320 /* So p_block doesn't get re-added several times */
321 if( pp_block )
322 *pp_block = block_BytestreamPop( &p_sys->bytestream );
324 p_sys->i_state = STATE_NOSYNC;
326 return p_out_buffer;
331 static void Close( vlc_object_t *p_this )
333 decoder_t *p_dec = (decoder_t*)p_this;
334 decoder_sys_t *p_sys = p_dec->p_sys;
336 block_BytestreamRelease( &p_sys->bytestream );
338 free( p_sys );
341 static int Open( vlc_object_t *p_this )
343 decoder_t *p_dec = (decoder_t*)p_this;
344 decoder_sys_t *p_sys;
346 if( p_dec->fmt_in.i_codec != VLC_CODEC_DTS )
347 return VLC_EGENERIC;
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 )
351 return VLC_ENOMEM;
353 /* Misc init */
354 p_sys->i_state = STATE_NOSYNC;
355 date_Set( &p_sys->end_date, VLC_TICK_INVALID );
356 p_sys->i_pts = VLC_TICK_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_codec = p_dec->fmt_in.i_codec;
364 p_dec->fmt_out.audio = p_dec->fmt_in.audio;
366 /* Set callback */
367 p_dec->pf_packetize = PacketizeBlock;
368 p_dec->pf_flush = PacketizeFlush;
369 p_dec->pf_get_cc = NULL;
370 return VLC_SUCCESS;