mp4: fix PADB parsing
[vlc.git] / modules / packetizer / dts.c
blobed8acd1d41a29918fbea7392dfde43b2fea7ef26
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 struct decoder_sys_t
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 mtime_t i_pts;
69 bool b_discontinuity;
71 vlc_dts_header_t dts;
72 size_t i_input_size;
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_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 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 ( !date_Get( &p_sys->end_date ) && p_block->i_pts <= VLC_TS_INVALID ) {
148 /* We've just started the stream, wait for the first PTS. */
149 block_Release( p_block );
150 return NULL;
153 block_BytestreamPush( &p_sys->bytestream, p_block );
156 while( 1 )
158 switch( p_sys->i_state )
160 case STATE_NOSYNC:
161 while( block_PeekBytes( &p_sys->bytestream, p_header, 6 )
162 == VLC_SUCCESS )
164 if( vlc_dts_header_IsSync( p_header, 6 ) )
166 p_sys->i_state = STATE_SYNC;
167 break;
169 block_SkipByte( &p_sys->bytestream );
171 if( p_sys->i_state != STATE_SYNC )
173 block_BytestreamFlush( &p_sys->bytestream );
175 /* Need more data */
176 return NULL;
178 /* fallthrough */
180 case STATE_SYNC:
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;
189 /* fallthrough */
191 case STATE_HEADER:
192 /* Get DTS frame header (VLC_DTS_HEADER_SIZE bytes) */
193 if( block_PeekBytes( &p_sys->bytestream, p_header,
194 VLC_DTS_HEADER_SIZE ) != VLC_SUCCESS )
196 /* Need more data */
197 return NULL;
200 /* Check if frame is valid and get frame info */
201 if( vlc_dts_header_Parse( &p_sys->dts, p_header,
202 VLC_DTS_HEADER_SIZE ) != VLC_SUCCESS )
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( p_sys->dts.b_substream )
212 msg_Warn( p_dec, "substream without the paired core stream, "
213 "skip it" );
214 p_sys->i_state = STATE_NOSYNC;
215 if( block_SkipBytes( &p_sys->bytestream,
216 p_sys->dts.i_frame_size ) != VLC_SUCCESS )
217 return NULL;
218 break;
221 p_sys->i_input_size = p_sys->i_next_offset = p_sys->dts.i_frame_size;
222 p_sys->i_state = STATE_NEXT_SYNC;
223 /* fallthrough */
225 case STATE_NEXT_SYNC:
226 /* Check if next expected frame contains the sync word */
227 while( p_sys->i_state == STATE_NEXT_SYNC )
229 if( block_PeekOffsetBytes( &p_sys->bytestream,
230 p_sys->i_next_offset, p_header,
231 VLC_DTS_HEADER_SIZE )
232 != VLC_SUCCESS )
234 if( p_block == NULL ) /* drain */
236 p_sys->i_state = STATE_GET_DATA;
237 break;
239 /* Need more data */
240 return NULL;
243 if( p_header[0] == 0 )
245 /* DTS wav files, audio CD's and some mkvs use stuffing */
246 p_sys->i_next_offset++;
247 continue;
250 if( !vlc_dts_header_IsSync( p_header, VLC_DTS_HEADER_SIZE ) )
252 /* Even frame size is likely incorrect FSIZE #18166 */
253 if( (p_sys->dts.i_frame_size % 2) && p_sys->i_next_offset > 0 &&
254 block_PeekOffsetBytes( &p_sys->bytestream,
255 p_sys->i_next_offset - 1, p_header,
256 VLC_DTS_HEADER_SIZE ) == 0 &&
257 vlc_dts_header_IsSync( p_header, VLC_DTS_HEADER_SIZE ) )
259 p_sys->i_input_size = p_sys->i_next_offset = p_sys->dts.i_frame_size - 1;
260 /* reenter */
261 break;
263 msg_Dbg( p_dec, "emulated sync word "
264 "(no sync on following frame)" );
265 p_sys->i_state = STATE_NOSYNC;
266 block_SkipByte( &p_sys->bytestream );
267 break;
270 /* Check if a DTS substream packet is located just after
271 * the core packet */
272 if( p_sys->i_next_offset == p_sys->dts.i_frame_size )
274 vlc_dts_header_t next_header;
275 if( vlc_dts_header_Parse( &next_header, p_header,
276 VLC_DTS_HEADER_SIZE )
277 == VLC_SUCCESS && next_header.b_substream )
279 p_sys->i_input_size += next_header.i_frame_size;
282 p_sys->i_state = STATE_GET_DATA;
284 break;
286 case STATE_GET_DATA:
287 /* Make sure we have enough data. */
288 if( block_WaitBytes( &p_sys->bytestream,
289 p_sys->i_input_size ) != VLC_SUCCESS )
291 /* Need more data */
292 return NULL;
294 p_sys->i_state = STATE_SEND_DATA;
295 /* fallthrough */
297 case STATE_SEND_DATA:
298 if( !(p_out_buffer = GetOutBuffer( p_dec )) )
300 return NULL;
303 /* Copy the whole frame into the buffer. When we reach this point
304 * we already know we have enough data available. */
305 block_GetBytes( &p_sys->bytestream, p_out_buffer->p_buffer,
306 p_out_buffer->i_buffer );
308 /* Make sure we don't reuse the same pts twice */
309 if( p_sys->i_pts == p_sys->bytestream.p_block->i_pts )
310 p_sys->i_pts = p_sys->bytestream.p_block->i_pts = VLC_TS_INVALID;
312 if( p_sys->b_discontinuity )
314 p_sys->b_discontinuity = false;
315 p_out_buffer->i_flags |= BLOCK_FLAG_DISCONTINUITY;
318 /* So p_block doesn't get re-added several times */
319 if( pp_block )
320 *pp_block = block_BytestreamPop( &p_sys->bytestream );
322 p_sys->i_state = STATE_NOSYNC;
324 return p_out_buffer;
329 static void Close( vlc_object_t *p_this )
331 decoder_t *p_dec = (decoder_t*)p_this;
332 decoder_sys_t *p_sys = p_dec->p_sys;
334 block_BytestreamRelease( &p_sys->bytestream );
336 free( p_sys );
339 static int Open( vlc_object_t *p_this )
341 decoder_t *p_dec = (decoder_t*)p_this;
342 decoder_sys_t *p_sys;
344 if( p_dec->fmt_in.i_codec != VLC_CODEC_DTS )
345 return VLC_EGENERIC;
347 /* Allocate the memory needed to store the decoder's structure */
348 if( ( p_dec->p_sys = p_sys = malloc(sizeof(decoder_sys_t)) ) == NULL )
349 return VLC_ENOMEM;
351 /* Misc init */
352 p_sys->i_state = STATE_NOSYNC;
353 date_Set( &p_sys->end_date, 0 );
354 p_sys->i_pts = VLC_TS_INVALID;
355 p_sys->b_date_set = false;
356 p_sys->b_discontinuity = false;
357 memset(&p_sys->dts, 0, sizeof(vlc_dts_header_t));
358 block_BytestreamInit( &p_sys->bytestream );
360 /* Set output properties (passthrough only) */
361 p_dec->fmt_out.i_codec = p_dec->fmt_in.i_codec;
362 p_dec->fmt_out.audio = p_dec->fmt_in.audio;
364 /* Set callback */
365 p_dec->pf_packetize = PacketizeBlock;
366 p_dec->pf_flush = PacketizeFlush;
367 return VLC_SUCCESS;