es: replace i_original_channels with i_chan_mode
[vlc.git] / modules / packetizer / dts.c
blobd9eb147ce967746f9a50ab5baea8fc45c93c5596
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;
179 case STATE_SYNC:
180 /* New frame, set the Presentation Time Stamp */
181 p_sys->i_pts = p_sys->bytestream.p_block->i_pts;
182 if( p_sys->i_pts > VLC_TS_INVALID &&
183 p_sys->i_pts != date_Get( &p_sys->end_date ) )
185 date_Set( &p_sys->end_date, p_sys->i_pts );
187 p_sys->i_state = STATE_HEADER;
189 case STATE_HEADER:
190 /* Get DTS frame header (VLC_DTS_HEADER_SIZE bytes) */
191 if( block_PeekBytes( &p_sys->bytestream, p_header,
192 VLC_DTS_HEADER_SIZE ) != VLC_SUCCESS )
194 /* Need more data */
195 return NULL;
198 /* Check if frame is valid and get frame info */
199 if( vlc_dts_header_Parse( &p_sys->dts, p_header,
200 VLC_DTS_HEADER_SIZE ) != VLC_SUCCESS )
202 msg_Dbg( p_dec, "emulated sync word" );
203 block_SkipByte( &p_sys->bytestream );
204 p_sys->i_state = STATE_NOSYNC;
205 break;
208 if( p_sys->dts.b_substream )
210 msg_Warn( p_dec, "substream without the paired core stream, "
211 "skip it" );
212 p_sys->i_state = STATE_NOSYNC;
213 if( block_SkipBytes( &p_sys->bytestream,
214 p_sys->dts.i_frame_size ) != VLC_SUCCESS )
215 return NULL;
216 break;
219 p_sys->i_input_size = p_sys->i_next_offset = p_sys->dts.i_frame_size;
220 p_sys->i_state = STATE_NEXT_SYNC;
222 case STATE_NEXT_SYNC:
223 /* Check if next expected frame contains the sync word */
224 while( p_sys->i_state == STATE_NEXT_SYNC )
226 if( block_PeekOffsetBytes( &p_sys->bytestream,
227 p_sys->i_next_offset, p_header,
228 VLC_DTS_HEADER_SIZE )
229 != VLC_SUCCESS )
231 if( p_block == NULL ) /* drain */
233 p_sys->i_state = STATE_GET_DATA;
234 break;
236 /* Need more data */
237 return NULL;
240 if( p_header[0] == 0 )
242 /* DTS wav files, audio CD's and some mkvs use stuffing */
243 p_sys->i_next_offset++;
244 continue;
247 if( !vlc_dts_header_IsSync( p_header, VLC_DTS_HEADER_SIZE ) )
249 /* Even frame size is likely incorrect FSIZE #18166 */
250 if( (p_sys->dts.i_frame_size % 2) && p_sys->i_next_offset > 0 &&
251 block_PeekOffsetBytes( &p_sys->bytestream,
252 p_sys->i_next_offset - 1, p_header,
253 VLC_DTS_HEADER_SIZE ) == 0 &&
254 vlc_dts_header_IsSync( p_header, VLC_DTS_HEADER_SIZE ) )
256 p_sys->i_input_size = p_sys->i_next_offset = p_sys->dts.i_frame_size - 1;
257 /* reenter */
258 break;
260 msg_Dbg( p_dec, "emulated sync word "
261 "(no sync on following frame)" );
262 p_sys->i_state = STATE_NOSYNC;
263 block_SkipByte( &p_sys->bytestream );
264 break;
267 /* Check if a DTS substream packet is located just after
268 * the core packet */
269 if( p_sys->i_next_offset == p_sys->dts.i_frame_size )
271 vlc_dts_header_t next_header;
272 if( vlc_dts_header_Parse( &next_header, p_header,
273 VLC_DTS_HEADER_SIZE )
274 == VLC_SUCCESS && next_header.b_substream )
276 p_sys->i_input_size += next_header.i_frame_size;
279 p_sys->i_state = STATE_GET_DATA;
281 break;
283 case STATE_GET_DATA:
284 /* Make sure we have enough data. */
285 if( block_WaitBytes( &p_sys->bytestream,
286 p_sys->i_input_size ) != VLC_SUCCESS )
288 /* Need more data */
289 return NULL;
291 p_sys->i_state = STATE_SEND_DATA;
293 case STATE_SEND_DATA:
294 if( !(p_out_buffer = GetOutBuffer( p_dec )) )
296 return NULL;
299 /* Copy the whole frame into the buffer. When we reach this point
300 * we already know we have enough data available. */
301 block_GetBytes( &p_sys->bytestream, p_out_buffer->p_buffer,
302 p_out_buffer->i_buffer );
304 /* Make sure we don't reuse the same pts twice */
305 if( p_sys->i_pts == p_sys->bytestream.p_block->i_pts )
306 p_sys->i_pts = p_sys->bytestream.p_block->i_pts = VLC_TS_INVALID;
308 if( p_sys->b_discontinuity )
310 p_sys->b_discontinuity = false;
311 p_out_buffer->i_flags |= BLOCK_FLAG_DISCONTINUITY;
314 /* So p_block doesn't get re-added several times */
315 if( pp_block )
316 *pp_block = block_BytestreamPop( &p_sys->bytestream );
318 p_sys->i_state = STATE_NOSYNC;
320 return p_out_buffer;
325 static void Close( vlc_object_t *p_this )
327 decoder_t *p_dec = (decoder_t*)p_this;
328 decoder_sys_t *p_sys = p_dec->p_sys;
330 block_BytestreamRelease( &p_sys->bytestream );
332 free( p_sys );
335 static int Open( vlc_object_t *p_this )
337 decoder_t *p_dec = (decoder_t*)p_this;
338 decoder_sys_t *p_sys;
340 if( p_dec->fmt_in.i_codec != VLC_CODEC_DTS )
341 return VLC_EGENERIC;
343 /* Allocate the memory needed to store the decoder's structure */
344 if( ( p_dec->p_sys = p_sys = malloc(sizeof(decoder_sys_t)) ) == NULL )
345 return VLC_ENOMEM;
347 /* Misc init */
348 p_sys->i_state = STATE_NOSYNC;
349 date_Set( &p_sys->end_date, 0 );
350 p_sys->i_pts = VLC_TS_INVALID;
351 p_sys->b_date_set = false;
352 p_sys->b_discontinuity = false;
353 memset(&p_sys->dts, 0, sizeof(vlc_dts_header_t));
354 block_BytestreamInit( &p_sys->bytestream );
356 /* Set output properties (passthrough only) */
357 p_dec->fmt_out.i_codec = p_dec->fmt_in.i_codec;
358 p_dec->fmt_out.audio = p_dec->fmt_in.audio;
360 /* Set callback */
361 p_dec->pf_packetize = PacketizeBlock;
362 p_dec->pf_flush = PacketizeFlush;
363 return VLC_SUCCESS;