demux: mkv: set original fourcc for LATM
[vlc.git] / modules / packetizer / dts.c
blob2d4c9dbb7fc18d6071507c005bbf80585aa5941f
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_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 )
115 return 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 );
119 p_block->i_length =
120 date_Increment( &p_sys->end_date, p_block->i_nb_samples ) - p_block->i_pts;
121 return p_block;
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;
132 if( p_block )
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 );
137 if(p_drain)
138 return p_drain;
140 PacketizeFlush( p_dec );
142 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 ) {
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;
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;
190 case 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 )
195 /* Need more data */
196 return NULL;
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;
206 break;
209 if( p_sys->dts.b_substream )
211 msg_Warn( p_dec, "substream without the paired core stream, "
212 "skip it" );
213 p_sys->i_state = STATE_NOSYNC;
214 if( block_SkipBytes( &p_sys->bytestream,
215 p_sys->dts.i_frame_size ) != VLC_SUCCESS )
216 return NULL;
217 break;
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 )
230 != VLC_SUCCESS )
232 if( p_block == NULL ) /* drain */
234 p_sys->i_state = STATE_GET_DATA;
235 break;
237 /* Need more data */
238 return NULL;
241 if( p_header[0] == 0 )
243 /* DTS wav files, audio CD's and some mkvs use stuffing */
244 p_sys->i_next_offset++;
245 continue;
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;
258 /* reenter */
259 break;
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 );
265 break;
268 /* Check if a DTS substream packet is located just after
269 * the core packet */
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;
282 break;
284 case 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 )
289 /* Need more data */
290 return NULL;
292 p_sys->i_state = STATE_SEND_DATA;
294 case STATE_SEND_DATA:
295 if( !(p_out_buffer = GetOutBuffer( p_dec )) )
297 return NULL;
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 */
316 if( pp_block )
317 *pp_block = block_BytestreamPop( &p_sys->bytestream );
319 p_sys->i_state = STATE_NOSYNC;
321 return p_out_buffer;
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 );
333 free( p_sys );
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 )
343 case VLC_CODEC_DTS:
344 break;
345 default:
346 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, 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;
367 /* Set callback */
368 p_dec->pf_packetize = PacketizeBlock;
369 p_dec->pf_flush = PacketizeFlush;
370 return VLC_SUCCESS;