/src/vlc/modules/packetizer/dts.c
Line | Count | Source (jump to first uncovered line) |
1 | | /***************************************************************************** |
2 | | * dts.c: parse DTS audio sync info and packetize the stream |
3 | | ***************************************************************************** |
4 | | * Copyright (C) 2001-2016 VLC authors and VideoLAN |
5 | | * |
6 | | * Authors: Gildas Bazin <gbazin@videolan.org> |
7 | | * Thomas Guillem <thomas@gllm.fr> |
8 | | * |
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. |
13 | | * |
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. |
18 | | * |
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 | | *****************************************************************************/ |
23 | | |
24 | | /***************************************************************************** |
25 | | * Preamble |
26 | | *****************************************************************************/ |
27 | | #ifdef HAVE_CONFIG_H |
28 | | # include "config.h" |
29 | | #endif |
30 | | |
31 | | #include <stdbit.h> |
32 | | |
33 | | #include <vlc_common.h> |
34 | | #include <vlc_plugin.h> |
35 | | #include <vlc_codec.h> |
36 | | #include <vlc_block_helper.h> |
37 | | #include <vlc_modules.h> |
38 | | |
39 | | #include "dts_header.h" |
40 | | |
41 | | #include "packetizer_helper.h" |
42 | | |
43 | | static int Open( vlc_object_t * ); |
44 | | static void Close( vlc_object_t * ); |
45 | | |
46 | 104 | vlc_module_begin () |
47 | 52 | set_subcategory( SUBCAT_SOUT_PACKETIZER ) |
48 | 52 | set_description( N_("DTS audio packetizer") ) |
49 | 52 | set_capability( "audio packetizer", 10 ) |
50 | 104 | set_callbacks( Open, Close ) |
51 | 52 | vlc_module_end () |
52 | | |
53 | | typedef struct |
54 | | { |
55 | | /* |
56 | | * Input properties |
57 | | */ |
58 | | int i_state; |
59 | | |
60 | | block_bytestream_t bytestream; |
61 | | size_t i_next_offset; |
62 | | |
63 | | /* |
64 | | * Common properties |
65 | | */ |
66 | | date_t end_date; |
67 | | bool b_date_set; |
68 | | |
69 | | vlc_tick_t i_pts; |
70 | | bool b_discontinuity; |
71 | | |
72 | | vlc_dts_header_t first, second; |
73 | | size_t i_input_size; |
74 | | } decoder_sys_t; |
75 | | |
76 | | enum |
77 | | { |
78 | | STATE_SYNC_SUBSTREAM_EXTENSIONS = STATE_CUSTOM_FIRST, |
79 | | STATE_NEXT_SYNC_SUBSTREAM_EXTENSIONS, |
80 | | }; |
81 | | |
82 | | static void PacketizeFlush( decoder_t *p_dec ) |
83 | 0 | { |
84 | 0 | decoder_sys_t *p_sys = p_dec->p_sys; |
85 | |
|
86 | 0 | p_sys->b_discontinuity = true; |
87 | 0 | date_Set( &p_sys->end_date, VLC_TICK_INVALID ); |
88 | 0 | p_sys->i_state = STATE_NOSYNC; |
89 | 0 | block_BytestreamEmpty( &p_sys->bytestream ); |
90 | 0 | } |
91 | | |
92 | | static block_t *GetOutBuffer( decoder_t *p_dec ) |
93 | 113k | { |
94 | 113k | decoder_sys_t *p_sys = p_dec->p_sys; |
95 | | |
96 | 113k | if( !p_sys->b_date_set |
97 | 113k | || p_dec->fmt_out.audio.i_rate != p_sys->first.i_rate ) |
98 | 2.30k | { |
99 | 2.30k | msg_Dbg( p_dec, "DTS samplerate:%d bitrate:%d", |
100 | 2.30k | p_sys->first.i_rate, p_sys->first.i_bitrate ); |
101 | | |
102 | 2.30k | date_Init( &p_sys->end_date, p_sys->first.i_rate, 1 ); |
103 | 2.30k | date_Set( &p_sys->end_date, p_sys->i_pts ); |
104 | 2.30k | p_sys->b_date_set = true; |
105 | 2.30k | } |
106 | | |
107 | 113k | p_dec->fmt_out.audio.i_rate = p_sys->first.i_rate; |
108 | 113k | if( p_dec->fmt_out.audio.i_bytes_per_frame < p_sys->first.i_frame_size ) |
109 | 1.42k | p_dec->fmt_out.audio.i_bytes_per_frame = p_sys->first.i_frame_size; |
110 | 113k | p_dec->fmt_out.audio.i_frame_length = p_sys->first.i_frame_length; |
111 | | |
112 | 113k | p_dec->fmt_out.audio.i_chan_mode = p_sys->first.i_chan_mode; |
113 | 113k | p_dec->fmt_out.audio.i_physical_channels = p_sys->first.i_physical_channels; |
114 | 113k | p_dec->fmt_out.audio.i_channels = |
115 | 113k | stdc_count_ones( p_dec->fmt_out.audio.i_physical_channels ); |
116 | | |
117 | 113k | p_dec->fmt_out.i_bitrate = p_sys->first.i_bitrate; |
118 | | |
119 | 113k | block_t *p_block = block_Alloc( p_sys->i_input_size ); |
120 | 113k | if( p_block == NULL ) |
121 | 0 | return NULL; |
122 | | |
123 | 113k | p_block->i_nb_samples = p_sys->first.i_frame_length; |
124 | 113k | p_block->i_pts = p_block->i_dts = date_Get( &p_sys->end_date ); |
125 | 113k | p_block->i_length = |
126 | 113k | date_Increment( &p_sys->end_date, p_block->i_nb_samples ) - p_block->i_pts; |
127 | 113k | return p_block; |
128 | 113k | } |
129 | | |
130 | | static block_t *PacketizeBlock( decoder_t *p_dec, block_t **pp_block ) |
131 | 128k | { |
132 | 128k | decoder_sys_t *p_sys = p_dec->p_sys; |
133 | 128k | uint8_t p_header[VLC_DTS_HEADER_SIZE]; |
134 | 128k | block_t *p_out_buffer; |
135 | | |
136 | 128k | block_t *p_block = pp_block ? *pp_block : NULL; |
137 | | |
138 | 128k | if( p_block ) |
139 | 120k | { |
140 | 120k | if ( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) ) { |
141 | | /* First always drain complete blocks before discontinuity */ |
142 | 0 | block_t *p_drain = PacketizeBlock( p_dec, NULL ); |
143 | 0 | if(p_drain) |
144 | 0 | return p_drain; |
145 | | |
146 | 0 | PacketizeFlush( p_dec ); |
147 | |
|
148 | 0 | if ( p_block->i_flags & BLOCK_FLAG_CORRUPTED ) { |
149 | 0 | block_Release( p_block ); |
150 | 0 | return NULL; |
151 | 0 | } |
152 | 0 | } |
153 | | |
154 | 120k | if ( p_block->i_pts == VLC_TICK_INVALID && |
155 | 120k | date_Get( &p_sys->end_date ) == VLC_TICK_INVALID ) { |
156 | | /* We've just started the stream, wait for the first PTS. */ |
157 | 817 | block_Release( p_block ); |
158 | 817 | return NULL; |
159 | 817 | } |
160 | | |
161 | 119k | block_BytestreamPush( &p_sys->bytestream, p_block ); |
162 | 119k | } |
163 | | |
164 | 5.41M | while( 1 ) |
165 | 5.41M | { |
166 | 5.41M | switch( p_sys->i_state ) |
167 | 5.41M | { |
168 | 3.45M | case STATE_NOSYNC: |
169 | 72.2M | while( block_PeekBytes( &p_sys->bytestream, p_header, 6 ) |
170 | 72.2M | == VLC_SUCCESS ) |
171 | 72.2M | { |
172 | 72.2M | if( vlc_dts_header_IsSync( p_header, 6 ) ) |
173 | 3.45M | { |
174 | 3.45M | p_sys->i_state = STATE_SYNC; |
175 | 3.45M | break; |
176 | 3.45M | } |
177 | 68.8M | block_SkipByte( &p_sys->bytestream ); |
178 | 68.8M | } |
179 | 3.45M | if( p_sys->i_state != STATE_SYNC ) |
180 | 4.75k | { |
181 | 4.75k | block_BytestreamFlush( &p_sys->bytestream ); |
182 | | |
183 | | /* Need more data */ |
184 | 4.75k | return NULL; |
185 | 4.75k | } |
186 | | /* fallthrough */ |
187 | | |
188 | 3.45M | case STATE_SYNC: |
189 | | /* New frame, set the Presentation Time Stamp */ |
190 | 3.45M | p_sys->i_pts = p_sys->bytestream.p_block->i_pts; |
191 | 3.45M | if( p_sys->i_pts != VLC_TICK_INVALID && |
192 | 3.45M | p_sys->i_pts != date_Get( &p_sys->end_date ) ) |
193 | 2.15k | { |
194 | 2.15k | date_Set( &p_sys->end_date, p_sys->i_pts ); |
195 | 2.15k | } |
196 | 3.45M | p_sys->i_state = STATE_HEADER; |
197 | | /* fallthrough */ |
198 | | |
199 | 3.45M | case STATE_HEADER: |
200 | | /* Get DTS frame header (VLC_DTS_HEADER_SIZE bytes) */ |
201 | 3.45M | if( block_PeekBytes( &p_sys->bytestream, p_header, |
202 | 3.45M | VLC_DTS_HEADER_SIZE ) != VLC_SUCCESS ) |
203 | 1.48k | { |
204 | | /* Need more data */ |
205 | 1.48k | return NULL; |
206 | 1.48k | } |
207 | | |
208 | | /* Check if frame is valid and get frame info */ |
209 | 3.45M | if( vlc_dts_header_Parse( &p_sys->first, p_header, |
210 | 3.45M | VLC_DTS_HEADER_SIZE ) != VLC_SUCCESS |
211 | 3.45M | || p_sys->first.i_frame_size == 0 ) |
212 | 1.80M | { |
213 | 1.80M | msg_Dbg( p_dec, "emulated sync word" ); |
214 | 1.80M | block_SkipByte( &p_sys->bytestream ); |
215 | 1.80M | p_sys->i_state = STATE_NOSYNC; |
216 | 1.80M | break; |
217 | 1.80M | } |
218 | | |
219 | 1.64M | if( p_sys->first.syncword == DTS_SYNC_SUBSTREAM ) |
220 | 1.39M | p_sys->i_state = STATE_SYNC_SUBSTREAM_EXTENSIONS; |
221 | 252k | else |
222 | 252k | p_sys->i_state = STATE_NEXT_SYNC; |
223 | 1.64M | p_sys->i_input_size = p_sys->i_next_offset = p_sys->first.i_frame_size; |
224 | 1.64M | break; |
225 | | |
226 | 1.39M | case STATE_SYNC_SUBSTREAM_EXTENSIONS: |
227 | | /* Peek into the substream extension (sync + header size < frame_size) */ |
228 | 1.39M | if( block_PeekOffsetBytes( &p_sys->bytestream, |
229 | 1.39M | p_sys->first.i_substream_header_size, |
230 | 1.39M | p_header, |
231 | 1.39M | VLC_DTS_HEADER_SIZE ) != VLC_SUCCESS ) |
232 | 1.27k | { |
233 | | /* Need more data */ |
234 | 1.27k | return NULL; |
235 | 1.27k | } |
236 | | |
237 | 1.39M | vlc_dts_header_t xssheader; |
238 | 1.39M | if( vlc_dts_header_Parse( &xssheader, p_header, |
239 | 1.39M | VLC_DTS_HEADER_SIZE ) != VLC_SUCCESS ) |
240 | 1.26M | { |
241 | 1.26M | msg_Dbg( p_dec, "emulated substream sync word, can't find extension" ); |
242 | 1.26M | block_SkipByte( &p_sys->bytestream ); |
243 | 1.26M | p_sys->i_state = STATE_NOSYNC; |
244 | 1.26M | break; |
245 | 1.26M | } |
246 | | |
247 | 124k | if( xssheader.syncword == DTS_SYNC_SUBSTREAM_LBR ) |
248 | 107k | { |
249 | | /* |
250 | | * LBR exists as independent SUBSTREAM. It is seen valid |
251 | | * only when SUBSTREAM[LBR]..SUBTREAM. |
252 | | * CORE...SUBSTREAM is regular extension. |
253 | | * SUBSTREAM...CORE is sync issue. |
254 | | */ |
255 | 107k | p_dec->fmt_out.i_profile = PROFILE_DTS_EXPRESS; |
256 | 107k | p_sys->first.i_rate = xssheader.i_rate; |
257 | 107k | p_sys->first.i_frame_length = xssheader.i_frame_length; |
258 | 107k | p_sys->i_state = STATE_NEXT_SYNC; |
259 | 107k | break; |
260 | 107k | } |
261 | | |
262 | 17.0k | msg_Warn( p_dec, "substream without the paired core stream, skip it" ); |
263 | 17.0k | p_sys->i_state = STATE_NOSYNC; |
264 | 17.0k | p_dec->fmt_out.i_profile = PROFILE_DTS; |
265 | 17.0k | if( block_SkipBytes( &p_sys->bytestream, |
266 | 17.0k | p_sys->first.i_frame_size ) != VLC_SUCCESS ) |
267 | 1.46k | return NULL; |
268 | 15.6k | break; |
269 | | |
270 | 395k | case STATE_NEXT_SYNC: |
271 | | /* Check if next expected frame contains the sync word */ |
272 | 47.5M | while( p_sys->i_state == STATE_NEXT_SYNC ) |
273 | 47.3M | { |
274 | 47.3M | if( block_PeekOffsetBytes( &p_sys->bytestream, |
275 | 47.3M | p_sys->i_next_offset, p_header, |
276 | 47.3M | VLC_DTS_HEADER_SIZE ) |
277 | 47.3M | != VLC_SUCCESS ) |
278 | 4.65k | { |
279 | 4.65k | if( p_block == NULL ) /* drain */ |
280 | 746 | { |
281 | 746 | p_sys->i_state = STATE_GET_DATA; |
282 | 746 | break; |
283 | 746 | } |
284 | | /* Need more data */ |
285 | 3.91k | return NULL; |
286 | 4.65k | } |
287 | | |
288 | 47.3M | if( p_header[0] == 0 ) |
289 | 46.9M | { |
290 | | /* DTS wav files, audio CD's and some mkvs use stuffing */ |
291 | 46.9M | p_sys->i_next_offset++; |
292 | 46.9M | continue; |
293 | 46.9M | } |
294 | | |
295 | 391k | if( !vlc_dts_header_IsSync( p_header, VLC_DTS_HEADER_SIZE ) ) |
296 | 277k | { |
297 | | /* Even frame size is likely incorrect FSIZE #18166 */ |
298 | 277k | if( (p_sys->first.i_frame_size % 2) && p_sys->i_next_offset > 0 && |
299 | 277k | block_PeekOffsetBytes( &p_sys->bytestream, |
300 | 118k | p_sys->i_next_offset - 1, p_header, |
301 | 118k | VLC_DTS_HEADER_SIZE ) == 0 && |
302 | 277k | vlc_dts_header_IsSync( p_header, VLC_DTS_HEADER_SIZE ) ) |
303 | 31.9k | { |
304 | 31.9k | p_sys->i_input_size = p_sys->i_next_offset = p_sys->first.i_frame_size - 1; |
305 | | /* reenter */ |
306 | 31.9k | break; |
307 | 31.9k | } |
308 | 245k | msg_Dbg( p_dec, "emulated sync word " |
309 | 245k | "(no sync on following frame)" ); |
310 | 245k | p_sys->i_state = STATE_NOSYNC; |
311 | 245k | block_SkipByte( &p_sys->bytestream ); |
312 | 245k | break; |
313 | 277k | } |
314 | | |
315 | | /* Check if a DTS substream packet is located just after |
316 | | * the core packet */ |
317 | 113k | if( p_sys->i_next_offset == p_sys->first.i_frame_size && |
318 | 113k | vlc_dts_header_Parse( &p_sys->second, |
319 | 72.6k | p_header, VLC_DTS_HEADER_SIZE ) == VLC_SUCCESS && |
320 | 113k | p_sys->second.syncword == DTS_SYNC_SUBSTREAM ) |
321 | 54.3k | { |
322 | 54.3k | p_sys->i_state = STATE_NEXT_SYNC_SUBSTREAM_EXTENSIONS; |
323 | 54.3k | } |
324 | 59.3k | else |
325 | 59.3k | { |
326 | 59.3k | p_dec->fmt_out.i_profile = PROFILE_DTS; |
327 | 59.3k | p_sys->i_state = STATE_GET_DATA; |
328 | 59.3k | } |
329 | 113k | } |
330 | 391k | break; |
331 | | |
332 | 391k | case STATE_NEXT_SYNC_SUBSTREAM_EXTENSIONS: |
333 | 54.3k | assert(p_sys->second.syncword == DTS_SYNC_SUBSTREAM); |
334 | 54.3k | if( p_sys->first.syncword == DTS_SYNC_SUBSTREAM ) |
335 | 52.5k | { |
336 | | /* First substream must have been LBR */ |
337 | 52.5k | p_dec->fmt_out.i_profile = PROFILE_DTS_EXPRESS; |
338 | 52.5k | } |
339 | 1.81k | else /* Otherwise that's core + extensions, we need to output both */ |
340 | 1.81k | { |
341 | 1.81k | p_dec->fmt_out.i_profile = PROFILE_DTS_HD; |
342 | 1.81k | p_sys->i_input_size += p_sys->second.i_frame_size; |
343 | 1.81k | } |
344 | 54.3k | p_sys->i_state = STATE_GET_DATA; |
345 | 54.3k | break; |
346 | | |
347 | 115k | case STATE_GET_DATA: |
348 | | /* Make sure we have enough data. */ |
349 | 115k | if( block_WaitBytes( &p_sys->bytestream, |
350 | 115k | p_sys->i_input_size ) != VLC_SUCCESS ) |
351 | 1.24k | { |
352 | | /* Need more data */ |
353 | 1.24k | return NULL; |
354 | 1.24k | } |
355 | 113k | p_sys->i_state = STATE_SEND_DATA; |
356 | | /* fallthrough */ |
357 | | |
358 | 113k | case STATE_SEND_DATA: |
359 | 113k | if( !(p_out_buffer = GetOutBuffer( p_dec )) ) |
360 | 0 | { |
361 | 0 | return NULL; |
362 | 0 | } |
363 | | |
364 | | /* Copy the whole frame into the buffer. When we reach this point |
365 | | * we already know we have enough data available. */ |
366 | 113k | block_GetBytes( &p_sys->bytestream, p_out_buffer->p_buffer, |
367 | 113k | p_out_buffer->i_buffer ); |
368 | | |
369 | | /* Make sure we don't reuse the same pts twice */ |
370 | 113k | if( p_sys->i_pts == p_sys->bytestream.p_block->i_pts ) |
371 | 113k | p_sys->i_pts = p_sys->bytestream.p_block->i_pts = VLC_TICK_INVALID; |
372 | | |
373 | 113k | if( p_sys->b_discontinuity ) |
374 | 0 | { |
375 | 0 | p_sys->b_discontinuity = false; |
376 | 0 | p_out_buffer->i_flags |= BLOCK_FLAG_DISCONTINUITY; |
377 | 0 | } |
378 | | |
379 | | /* So p_block doesn't get re-added several times */ |
380 | 113k | if( pp_block ) |
381 | 109k | *pp_block = block_BytestreamPop( &p_sys->bytestream ); |
382 | | |
383 | 113k | p_sys->i_state = STATE_NOSYNC; |
384 | | |
385 | 113k | return p_out_buffer; |
386 | 5.41M | } |
387 | 5.41M | } |
388 | 128k | } |
389 | | |
390 | | static void Close( vlc_object_t *p_this ) |
391 | 3.97k | { |
392 | 3.97k | decoder_t *p_dec = (decoder_t*)p_this; |
393 | 3.97k | decoder_sys_t *p_sys = p_dec->p_sys; |
394 | | |
395 | 3.97k | block_BytestreamRelease( &p_sys->bytestream ); |
396 | | |
397 | 3.97k | free( p_sys ); |
398 | 3.97k | } |
399 | | |
400 | | static int Open( vlc_object_t *p_this ) |
401 | 3.45M | { |
402 | 3.45M | decoder_t *p_dec = (decoder_t*)p_this; |
403 | 3.45M | decoder_sys_t *p_sys; |
404 | | |
405 | 3.45M | if( p_dec->fmt_in->i_codec != VLC_CODEC_DTS ) |
406 | 3.45M | return VLC_EGENERIC; |
407 | | |
408 | | /* Allocate the memory needed to store the decoder's structure */ |
409 | 3.97k | if( ( p_dec->p_sys = p_sys = malloc(sizeof(decoder_sys_t)) ) == NULL ) |
410 | 0 | return VLC_ENOMEM; |
411 | | |
412 | | /* Misc init */ |
413 | 3.97k | p_sys->i_state = STATE_NOSYNC; |
414 | 3.97k | date_Set( &p_sys->end_date, VLC_TICK_INVALID ); |
415 | 3.97k | p_sys->i_pts = VLC_TICK_INVALID; |
416 | 3.97k | p_sys->b_date_set = false; |
417 | 3.97k | p_sys->b_discontinuity = false; |
418 | 3.97k | memset(&p_sys->first, 0, sizeof(vlc_dts_header_t)); |
419 | 3.97k | memset(&p_sys->second, 0, sizeof(vlc_dts_header_t)); |
420 | 3.97k | block_BytestreamInit( &p_sys->bytestream ); |
421 | | |
422 | | /* Set output properties (passthrough only) */ |
423 | 3.97k | p_dec->fmt_out.i_codec = p_dec->fmt_in->i_codec; |
424 | 3.97k | p_dec->fmt_out.audio = p_dec->fmt_in->audio; |
425 | | |
426 | | /* Set callback */ |
427 | 3.97k | p_dec->pf_packetize = PacketizeBlock; |
428 | 3.97k | p_dec->pf_flush = PacketizeFlush; |
429 | 3.97k | p_dec->pf_get_cc = NULL; |
430 | 3.97k | return VLC_SUCCESS; |
431 | 3.97k | } |