demux: ogg: only invalid pts on no granule interpolation
[vlc.git] / modules / demux / rawaud.c
blob87b21121020541a802ca0dccfd0ba056c64dc3ec
1 /*****************************************************************************
2 * rawaud.c : raw audio input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2009 VLC authors and VideoLAN
6 * Authors: Jarmo Torvinen <jarmo.torvinen@jutel.fi>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 /*****************************************************************************
24 * Preamble
25 *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_demux.h>
35 /*****************************************************************************
36 * Module descriptor
37 *****************************************************************************/
38 static int Open ( vlc_object_t * );
39 static void Close( vlc_object_t * );
42 #define SAMPLERATE_TEXT N_("Audio samplerate (Hz)")
43 #define SAMPLERATE_LONGTEXT N_("Audio sample rate in Hertz. Default is 48000 Hz.")
45 #define CHANNELS_TEXT N_("Audio channels")
46 #define CHANNELS_LONGTEXT N_("Audio channels in input stream. Numeric value >0. Default is 2.")
48 #define FOURCC_TEXT N_("FOURCC code of raw input format")
49 #define FOURCC_LONGTEXT N_( \
50 "FOURCC code of the raw input format. This is a four character string." )
52 #define LANG_TEXT N_("Forces the audio language")
53 #define LANG_LONGTEXT N_("Forces the audio language for the output mux. Three letter ISO639 code. Default is 'eng'.")
55 #ifdef WORDS_BIGENDIAN
56 # define FOURCC_DEFAULT "s16b"
57 #else
58 # define FOURCC_DEFAULT "s16l"
59 #endif
61 vlc_module_begin();
62 set_shortname( "Raw Audio" );
63 set_description( N_("Raw audio demuxer") );
64 set_capability( "demux", 0 );
65 set_category( CAT_INPUT );
66 set_subcategory( SUBCAT_INPUT_DEMUX );
67 set_callbacks( Open, Close );
68 add_shortcut( "rawaud" );
69 add_integer( "rawaud-channels", 2, CHANNELS_TEXT, CHANNELS_LONGTEXT, false );
70 change_safe()
71 add_integer( "rawaud-samplerate", 48000, SAMPLERATE_TEXT, SAMPLERATE_LONGTEXT, false );
72 change_safe()
73 add_string( "rawaud-fourcc", FOURCC_DEFAULT,
74 FOURCC_TEXT, FOURCC_LONGTEXT, false );
75 change_safe()
76 add_string( "rawaud-lang", "eng", LANG_TEXT, LANG_LONGTEXT, false);
77 vlc_module_end();
79 /*****************************************************************************
80 * Definitions of structures used by this plugin
81 *****************************************************************************/
82 typedef struct
84 es_out_id_t *p_es;
85 es_format_t fmt;
86 unsigned int i_frame_size;
87 unsigned int i_frame_samples;
88 unsigned int i_seek_step;
89 date_t pts;
90 } demux_sys_t;
93 /*****************************************************************************
94 * Local prototypes
95 *****************************************************************************/
96 static int Demux( demux_t * );
97 static int Control( demux_t *, int i_query, va_list args );
100 /*****************************************************************************
101 * Open: initializes raw audio demuxer
102 *****************************************************************************/
103 static int Open( vlc_object_t * p_this )
105 demux_t *p_demux = (demux_t*)p_this;
106 demux_sys_t *p_sys;
108 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
109 if( !p_sys )
110 return VLC_ENOMEM;
112 char *psz_fourcc = var_CreateGetString( p_demux, "rawaud-fourcc" );
113 es_format_Init( &p_sys->fmt, AUDIO_ES,
114 vlc_fourcc_GetCodecFromString( AUDIO_ES, psz_fourcc ) );
115 free( psz_fourcc );
117 if( !p_sys->fmt.i_codec )
119 msg_Err( p_demux, "rawaud-fourcc must be a 4 character string");
120 es_format_Clean( &p_sys->fmt );
121 free( p_sys );
122 return VLC_EGENERIC;
125 // get the bits per sample ratio based on codec
126 switch( p_sys->fmt.i_codec )
129 case VLC_CODEC_FL64:
130 p_sys->fmt.audio.i_bitspersample = 64;
131 break;
133 case VLC_CODEC_FL32:
134 case VLC_CODEC_S32L:
135 case VLC_CODEC_S32B:
136 p_sys->fmt.audio.i_bitspersample = 32;
137 break;
139 case VLC_CODEC_S24L:
140 case VLC_CODEC_S24B:
141 p_sys->fmt.audio.i_bitspersample = 24;
142 break;
144 case VLC_CODEC_S16L:
145 case VLC_CODEC_S16B:
146 p_sys->fmt.audio.i_bitspersample = 16;
147 break;
149 case VLC_CODEC_S8:
150 case VLC_CODEC_U8:
151 p_sys->fmt.audio.i_bitspersample = 8;
152 break;
154 default:
155 msg_Err( p_demux, "unknown fourcc format %4.4s",
156 (char *)&p_sys->fmt.i_codec);
157 es_format_Clean( &p_sys->fmt );
158 free( p_sys );
159 return VLC_EGENERIC;
164 p_sys->fmt.psz_language = var_CreateGetString( p_demux, "rawaud-lang" );
165 p_sys->fmt.audio.i_channels = var_CreateGetInteger( p_demux, "rawaud-channels" );
166 p_sys->fmt.audio.i_rate = var_CreateGetInteger( p_demux, "rawaud-samplerate" );
168 if( p_sys->fmt.audio.i_rate == 0 || p_sys->fmt.audio.i_rate > 384000 )
170 msg_Err( p_demux, "invalid sample rate");
171 es_format_Clean( &p_sys->fmt );
172 free( p_sys );
173 return VLC_EGENERIC;
176 if( p_sys->fmt.audio.i_channels == 0 || p_sys->fmt.audio.i_channels > 32 )
178 msg_Err( p_demux, "invalid number of channels");
179 es_format_Clean( &p_sys->fmt );
180 free( p_sys );
181 return VLC_EGENERIC;
184 p_sys->fmt.i_bitrate = p_sys->fmt.audio.i_rate *
185 p_sys->fmt.audio.i_channels *
186 p_sys->fmt.audio.i_bitspersample;
188 if( p_sys->fmt.i_bitrate > 50000000)
190 msg_Err( p_demux, "invalid bitrate");
191 es_format_Clean( &p_sys->fmt );
192 free( p_sys );
193 return VLC_EGENERIC;
196 msg_Dbg( p_demux,
197 "format initialized: channels=%d , samplerate=%d Hz, fourcc=%4.4s, bits per sample = %d, bitrate = %d bit/s",
198 p_sys->fmt.audio.i_channels,
199 p_sys->fmt.audio.i_rate,
200 (char*)&p_sys->fmt.i_codec,
201 p_sys->fmt.audio.i_bitspersample,
202 p_sys->fmt.i_bitrate);
204 /* add the es */
205 p_sys->p_es = es_out_Add( p_demux->out, &p_sys->fmt );
206 msg_Dbg( p_demux, "elementary stream added");
208 /* initialize timing */
209 date_Init( &p_sys->pts, p_sys->fmt.audio.i_rate, 1 );
210 date_Set( &p_sys->pts, VLC_TICK_0 );
212 /* calculate 50ms frame size/time */
213 p_sys->i_frame_samples = __MAX( p_sys->fmt.audio.i_rate / 20, 1 );
214 p_sys->i_seek_step = p_sys->fmt.audio.i_channels *
215 ( (p_sys->fmt.audio.i_bitspersample + 7) / 8 );
216 p_sys->i_frame_size = p_sys->i_frame_samples * p_sys->i_seek_step;
217 msg_Dbg( p_demux, "frame size is %d bytes ", p_sys->i_frame_size);
219 p_demux->pf_demux = Demux;
220 p_demux->pf_control = Control;
221 return VLC_SUCCESS;
224 /*****************************************************************************
225 * Close: frees unused data
226 *****************************************************************************/
227 static void Close( vlc_object_t *p_this )
229 demux_t *p_demux = (demux_t*)p_this;
230 demux_sys_t *p_sys = p_demux->p_sys;
232 es_format_Clean( &p_sys->fmt );
233 free( p_sys );
236 /*****************************************************************************
237 * Demux: reads and demuxes data packets
238 *****************************************************************************
239 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
240 *****************************************************************************/
241 static int Demux( demux_t *p_demux )
243 demux_sys_t *p_sys = p_demux->p_sys;
244 block_t *p_block;
246 p_block = vlc_stream_Block( p_demux->s, p_sys->i_frame_size );
247 if( p_block == NULL )
248 return VLC_DEMUXER_EOF;
250 p_block->i_dts = p_block->i_pts = date_Get( &p_sys->pts );
252 es_out_SetPCR( p_demux->out, p_block->i_pts );
253 es_out_Send( p_demux->out, p_sys->p_es, p_block );
255 date_Increment( &p_sys->pts, p_sys->i_frame_samples );
257 return VLC_DEMUXER_SUCCESS;
260 /*****************************************************************************
261 * Control:
262 *****************************************************************************/
263 static int Control( demux_t *p_demux, int i_query, va_list args )
265 demux_sys_t *p_sys = p_demux->p_sys;
267 return demux_vaControlHelper( p_demux->s, 0, -1,
268 p_sys->fmt.i_bitrate, p_sys->i_seek_step, i_query, args );