demux: mp4: avoid audio cuts on seek
[vlc.git] / modules / codec / uleaddvaudio.c
blobb90f0125e4b48b3fa055a7a6b49a248479369cf1
1 /*****************************************************************************
2 * uleaddvaudio.c
3 *****************************************************************************
4 * Copyright (C) 2012 Laurent Aimar
5 * $Id$
7 * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
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 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
28 #include <vlc_common.h>
29 #include <vlc_plugin.h>
30 #include <vlc_codec.h>
32 #include "../demux/rawdv.h"
34 /*****************************************************************************
35 * Module descriptor
36 *****************************************************************************/
37 static int Open(vlc_object_t *);
38 static void Close(vlc_object_t *);
40 vlc_module_begin()
41 set_description(N_("Ulead DV audio decoder"))
42 set_capability("audio decoder", 50)
43 set_category(CAT_INPUT)
44 set_subcategory(SUBCAT_INPUT_ACODEC)
45 set_callbacks(Open, Close)
46 vlc_module_end()
48 struct decoder_sys_t
50 date_t end_date;
52 bool is_pal;
53 bool is_12bit;
54 uint16_t shuffle[2000];
57 static void Flush(decoder_t *dec)
59 decoder_sys_t *sys = dec->p_sys;
61 date_Set(&sys->end_date, 0);
64 static block_t *DecodeBlock(decoder_t *dec, block_t **block_ptr)
66 decoder_sys_t *sys = dec->p_sys;
68 if (!*block_ptr)
69 return NULL;
71 block_t *block = *block_ptr;
72 if (block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED)) {
73 Flush(dec);
74 if (block->i_flags & BLOCK_FLAG_CORRUPTED) {
75 block_Release(block);
76 *block_ptr = NULL;
77 return NULL;
81 if (block->i_pts > VLC_TS_INVALID &&
82 block->i_pts != date_Get(&sys->end_date))
83 date_Set(&sys->end_date, block->i_pts);
84 block->i_pts = VLC_TS_INVALID;
85 if (!date_Get(&sys->end_date)) {
86 /* We've just started the stream, wait for the first PTS. */
87 block_Release(block);
88 return NULL;
91 const unsigned int block_size = sys->is_pal ? 8640 : 7200;
92 if (block->i_buffer >= block_size) {
93 uint8_t *src = block->p_buffer;
95 block->i_buffer -= block_size;
96 block->p_buffer += block_size;
98 int sample_count = dv_get_audio_sample_count(&src[244], sys->is_pal);
100 if( decoder_UpdateAudioFormat(dec))
101 return NULL;
102 block_t *output = decoder_NewAudioBuffer(dec, sample_count);
103 if (!output)
104 return NULL;
105 output->i_pts = date_Get(&sys->end_date);
106 output->i_length = date_Increment(&sys->end_date, sample_count) - output->i_pts;
108 int16_t *dst = (int16_t*)output->p_buffer;
109 for (int i = 0; i < sample_count; i++) {
110 const uint8_t *v = &src[sys->shuffle[i]];
111 if (sys->is_12bit) {
112 *dst++ = dv_audio_12to16((v[0] << 4) | ((v[2] >> 4) & 0x0f));
113 *dst++ = dv_audio_12to16((v[1] << 4) | ((v[2] >> 0) & 0x0f));
114 } else {
115 *dst++ = GetWBE(&v[0]);
116 *dst++ = GetWBE(&v[sys->is_pal ? 4320 : 3600]);
119 return output;
121 block_Release(block);
122 return NULL;
125 static int DecodeAudio(decoder_t *dec, block_t *block)
127 if (block == NULL) /* No Drain */
128 return VLCDEC_SUCCESS;
130 block_t **block_ptr = &block, *out;
131 while ((out = DecodeBlock(dec, block_ptr)) != NULL)
132 decoder_QueueAudio(dec,out);
133 return VLCDEC_SUCCESS;
136 static int Open(vlc_object_t *object)
138 decoder_t *dec = (decoder_t*)object;
140 if (dec->fmt_in.i_codec != VLC_CODEC_ULEAD_DV_AUDIO_NTSC &&
141 dec->fmt_in.i_codec != VLC_CODEC_ULEAD_DV_AUDIO_PAL)
142 return VLC_EGENERIC;
143 if (dec->fmt_in.audio.i_bitspersample != 12 && dec->fmt_in.audio.i_bitspersample != 16)
144 return VLC_EGENERIC;
145 if (dec->fmt_in.audio.i_channels != 2)
146 return VLC_EGENERIC;
147 if (dec->fmt_in.audio.i_rate <= 0)
148 return VLC_EGENERIC;
150 decoder_sys_t *sys = dec->p_sys = malloc(sizeof(*sys));
151 if (!sys)
152 return VLC_ENOMEM;
154 sys->is_pal = dec->fmt_in.i_codec == VLC_CODEC_ULEAD_DV_AUDIO_PAL;
155 sys->is_12bit = dec->fmt_in.audio.i_bitspersample == 12;
157 date_Init(&sys->end_date, dec->fmt_in.audio.i_rate, 1);
158 date_Set(&sys->end_date, 0);
160 for (unsigned i = 0; i < sizeof(sys->shuffle) / sizeof(*sys->shuffle); i++) {
161 const unsigned a = sys->is_pal ? 18 : 15;
162 const unsigned b = 3 * a;
163 sys->shuffle[i] = 80 * ((21 * (i % 3) + 9 * (i / 3) + ((i / a) % 3)) % b) +
164 (2 + sys->is_12bit) * (i / b) + 8;
167 dec->fmt_out.i_codec = VLC_CODEC_S16N;
168 dec->fmt_out.audio.i_rate = dec->fmt_in.audio.i_rate;
169 dec->fmt_out.audio.i_channels = 2;
170 dec->fmt_out.audio.i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
172 dec->pf_decode = DecodeAudio;
173 dec->pf_flush = Flush;
175 return VLC_SUCCESS;
178 static void Close(vlc_object_t *object)
180 decoder_t *dec = (decoder_t *)object;
182 free(dec->p_sys);