1 /*****************************************************************************
2 * flac.c : FLAC demux module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2003 the VideoLAN team
7 * Authors: Gildas Bazin <gbazin@netcourrier.com>
8 * Laurent Aimar <fenrir@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
29 #include <vlc_demux.h>
31 #include <vlc_input.h>
32 #include <vlc_codec.h>
35 /*****************************************************************************
37 *****************************************************************************/
38 static int Open ( vlc_object_t * );
39 static void Close ( vlc_object_t * );
42 set_description( _("FLAC demuxer") );
43 set_capability( "demux2", 155 );
44 set_category( CAT_INPUT );
45 set_subcategory( SUBCAT_INPUT_DEMUX );
46 set_callbacks( Open, Close );
47 add_shortcut( "flac" );
50 /*****************************************************************************
52 *****************************************************************************/
53 static int Demux ( demux_t * );
54 static int Control( demux_t *, int, va_list );
56 static int ReadMeta( demux_t *, uint8_t **pp_streaminfo, int *pi_streaminfo );
64 decoder_t *p_packetizer;
67 int64_t i_time_offset;
71 int64_t i_length; /* Length from stream info */
76 seekpoint_t **seekpoint;
79 #define STREAMINFO_SIZE 38
80 #define FLAC_PACKET_SIZE 16384
82 /*****************************************************************************
83 * Open: initializes ES structures
84 *****************************************************************************/
85 static int Open( vlc_object_t * p_this )
87 demux_t *p_demux = (demux_t*)p_this;
91 uint8_t *p_streaminfo;
94 /* Have a peep at the show. */
95 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 ) return VLC_EGENERIC;
97 if( p_peek[0]!='f' || p_peek[1]!='L' || p_peek[2]!='a' || p_peek[3]!='C' )
99 if( !p_demux->b_force ) return VLC_EGENERIC;
102 msg_Err( p_demux, "this doesn't look like a flac stream, "
103 "continuing anyway" );
106 p_demux->pf_demux = Demux;
107 p_demux->pf_control = Control;
108 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
109 p_sys->b_start = VLC_TRUE;
110 p_sys->p_meta = NULL;
112 p_sys->i_time_offset = 0;
114 p_sys->i_pts_start = 0;
116 TAB_INIT( p_sys->i_seekpoint, p_sys->seekpoint );
118 /* We need to read and store the STREAMINFO metadata */
119 if( ReadMeta( p_demux, &p_streaminfo, &i_streaminfo ) )
125 /* Load the FLAC packetizer */
126 INIT_APACKETIZER( p_sys->p_packetizer, 'f', 'l', 'a', 'c' );
128 /* Store STREAMINFO for the decoder and packetizer */
129 p_streaminfo[4] |= 0x80; /* Fake this as the last metadata block */
130 p_sys->p_packetizer->fmt_in.i_extra = i_streaminfo;
131 p_sys->p_packetizer->fmt_in.p_extra = p_streaminfo;
133 p_sys->p_packetizer->p_module =
134 module_Need( p_sys->p_packetizer, "packetizer", NULL, 0 );
135 if( !p_sys->p_packetizer->p_module )
137 if( p_sys->p_packetizer->fmt_in.p_extra )
138 free( p_sys->p_packetizer->fmt_in.p_extra );
139 vlc_object_destroy( p_sys->p_packetizer );
141 msg_Err( p_demux, "cannot find flac packetizer" );
145 /* Parse possible id3 header */
146 if( ( p_id3 = module_Need( p_demux, "meta reader", NULL, 0 ) ) )
148 vlc_meta_t *p_meta = (vlc_meta_t *)p_demux->p_private;
152 p_sys->p_meta = p_meta;
156 vlc_meta_Merge( p_sys->p_meta, p_meta );
157 vlc_meta_Delete( p_meta );
159 p_demux->p_private = NULL;
160 module_Unneed( p_demux, p_id3 );
166 /*****************************************************************************
167 * Close: frees unused data
168 *****************************************************************************/
169 static void Close( vlc_object_t * p_this )
171 demux_t *p_demux = (demux_t*)p_this;
172 demux_sys_t *p_sys = p_demux->p_sys;
175 module_Unneed( p_sys->p_packetizer, p_sys->p_packetizer->p_module );
177 if( p_sys->p_packetizer->fmt_in.p_extra )
178 free( p_sys->p_packetizer->fmt_in.p_extra );
180 /* Delete the decoder */
181 vlc_object_destroy( p_sys->p_packetizer );
183 vlc_meta_Delete( p_sys->p_meta );
187 /*****************************************************************************
188 * Demux: reads and demuxes data packets
189 *****************************************************************************
190 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
191 *****************************************************************************/
192 static int Demux( demux_t *p_demux )
194 demux_sys_t *p_sys = p_demux->p_sys;
195 block_t *p_block_in, *p_block_out;
197 if( !( p_block_in = stream_Block( p_demux->s, FLAC_PACKET_SIZE ) ) )
200 p_block_in->i_pts = p_block_in->i_dts = p_sys->b_start ? 1 : 0;
201 p_sys->b_start = VLC_FALSE;
203 while( (p_block_out = p_sys->p_packetizer->pf_packetize(
204 p_sys->p_packetizer, &p_block_in )) )
208 block_t *p_next = p_block_out->p_next;
210 p_block_out->p_next = NULL;
212 if( p_sys->p_es == NULL )
214 p_sys->p_packetizer->fmt_out.b_packetized = VLC_TRUE;
215 p_sys->p_es = es_out_Add( p_demux->out, &p_sys->p_packetizer->fmt_out);
219 if( p_block_out->i_dts >= p_sys->i_pts_start )
220 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block_out->i_dts );
222 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
224 p_sys->i_pts = p_block_out->i_dts;
225 es_out_Send( p_demux->out, p_sys->p_es, p_block_out );
227 p_block_out = p_next;
234 /*****************************************************************************
236 *****************************************************************************/
237 static int64_t ControlGetLength( demux_t *p_demux )
239 demux_sys_t *p_sys = p_demux->p_sys;
240 const int64_t i_size = stream_Size(p_demux->s) - p_sys->i_data_pos;
241 int64_t i_length = p_sys->i_length;
244 /* Try to fix length using seekpoint and current size for truncated file */
245 for( i = p_sys->i_seekpoint-1; i >= 0; i-- )
247 seekpoint_t *s = p_sys->seekpoint[i];
248 if( s->i_byte_offset <= i_size )
250 if( i+1 < p_sys->i_seekpoint )
253 seekpoint_t *n = p_sys->seekpoint[i+1];
254 assert( n->i_byte_offset != s->i_byte_offset); /* Should be ensured by ParseSeekTable */
255 i_length = s->i_time_offset + (n->i_time_offset-s->i_time_offset) * (i_size-s->i_byte_offset) / (n->i_byte_offset-s->i_byte_offset);
262 static int64_t ControlGetTime( demux_t *p_demux )
264 demux_sys_t *p_sys = p_demux->p_sys;
265 return __MAX(p_sys->i_pts, p_sys->i_pts_start) + p_sys->i_time_offset;
267 static int ControlSetTime( demux_t *p_demux, int64_t i_time )
269 demux_sys_t *p_sys = p_demux->p_sys;
271 int64_t i_next_offset;
272 int64_t i_delta_time;
273 int64_t i_delta_offset;
274 vlc_bool_t b_seekable;
278 stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_seekable );
283 assert( p_sys->i_seekpoint > 0 ); /* ReadMeta ensure at least (0,0) */
284 for( i = p_sys->i_seekpoint-1; i >= 0; i-- )
286 if( p_sys->seekpoint[i]->i_time_offset <= i_time )
289 if( i+1 < p_sys->i_seekpoint )
291 i_next_time = p_sys->seekpoint[i+1]->i_time_offset;
292 i_next_offset = p_sys->seekpoint[i+1]->i_byte_offset;
296 i_next_time = p_sys->i_length;
297 i_next_offset = stream_Size(p_demux->s)-p_sys->i_data_pos;
299 i_delta_time = i_time - p_sys->seekpoint[i]->i_time_offset;
300 i_delta_offset = (i_next_offset - p_sys->seekpoint[i]->i_byte_offset) * i_delta_time /
301 (p_sys->seekpoint[i+1]->i_time_offset-p_sys->seekpoint[i]->i_time_offset);
303 /* XXX We do exact seek if it's not too far away(45s) */
304 if( i_delta_time < 45*I64C(1000000) )
306 if( stream_Seek( p_demux->s, p_sys->seekpoint[i]->i_byte_offset+p_sys->i_data_pos ) )
308 p_sys->i_time_offset = p_sys->seekpoint[i]->i_time_offset - p_sys->i_pts;
309 p_sys->i_pts_start = p_sys->i_pts+i_delta_time;
310 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, p_sys->p_es, p_sys->i_pts_start );
314 if( stream_Seek( p_demux->s, p_sys->seekpoint[i]->i_byte_offset+p_sys->i_data_pos + i_delta_offset ) )
316 p_sys->i_pts_start = p_sys->i_pts;
317 p_sys->i_time_offset = (p_sys->seekpoint[i]->i_time_offset+i_delta_time) - p_sys->i_pts;
322 static int Control( demux_t *p_demux, int i_query, va_list args )
324 demux_sys_t *p_sys = p_demux->p_sys;
326 if( i_query == DEMUX_GET_META )
328 vlc_meta_t *p_meta = (vlc_meta_t *)va_arg( args, vlc_meta_t* );
329 if( p_demux->p_sys->p_meta )
330 vlc_meta_Merge( p_meta, p_demux->p_sys->p_meta );
333 else if( i_query == DEMUX_GET_LENGTH )
335 int64_t *pi64 = (int64_t*)va_arg( args, int64_t * );
336 *pi64 = ControlGetLength( p_demux );
339 else if( i_query == DEMUX_SET_TIME )
341 int64_t i_time = (int64_t)va_arg( args, int64_t );
342 return ControlSetTime( p_demux, i_time );
344 else if( i_query == DEMUX_SET_POSITION )
346 const double f = (double)va_arg( args, double );
347 int64_t i_time = f * ControlGetLength( p_demux );
348 return ControlSetTime( p_demux, i_time );
350 else if( i_query == DEMUX_GET_TIME )
352 int64_t *pi64 = (int64_t*)va_arg( args, int64_t * );
353 *pi64 = ControlGetTime( p_demux );
356 else if( i_query == DEMUX_GET_POSITION )
358 double *pf = (double*)va_arg( args, double * );
359 const int64_t i_length = ControlGetLength(p_demux);
361 *pf = (double)ControlGetTime(p_demux) / (double)i_length;
367 return demux2_vaControlHelper( p_demux->s, p_sys->i_data_pos, -1,
368 8*0, 1, i_query, args );
378 static inline int Get24bBE( uint8_t *p )
380 return (p[0] << 16)|(p[1] << 8)|(p[2]);
383 static void ParseStreamInfo( demux_t *p_demux, int *pi_rate, int64_t *pi_count, uint8_t *p_data, int i_data );
384 static void ParseSeekTable( demux_t *p_demux, uint8_t *p_data, int i_data, int i_sample_rate );
385 static void ParseComment( demux_t *, uint8_t *p_data, int i_data );
387 static int ReadMeta( demux_t *p_demux, uint8_t **pp_streaminfo, int *pi_streaminfo )
389 demux_sys_t *p_sys = p_demux->p_sys;
394 int64_t i_sample_count;
397 /* Read STREAMINFO */
398 i_peek = stream_Peek( p_demux->s, &p_peek, 8 );
399 if( (p_peek[4] & 0x7F) != META_STREAMINFO )
401 msg_Err( p_demux, "this isn't a STREAMINFO metadata block" );
404 if( Get24bBE(&p_peek[5]) != (STREAMINFO_SIZE - 4) )
406 msg_Err( p_demux, "invalid size for a STREAMINFO metadata block" );
410 *pi_streaminfo = 4 + STREAMINFO_SIZE;
411 *pp_streaminfo = malloc( 4 + STREAMINFO_SIZE );
412 if( *pp_streaminfo == NULL )
415 if( stream_Read( p_demux->s, *pp_streaminfo, 4+STREAMINFO_SIZE ) != 4+STREAMINFO_SIZE )
417 msg_Err( p_demux, "failed to read STREAMINFO metadata block" );
418 free( *pp_streaminfo );
423 ParseStreamInfo( p_demux, &i_sample_rate, &i_sample_count, *pp_streaminfo, *pi_streaminfo );
424 if( i_sample_rate > 0 )
425 p_sys->i_length = i_sample_count * I64C(1000000)/i_sample_rate;
427 /* Be sure we have seekpoint 0 */
428 s = vlc_seekpoint_New();
429 s->i_time_offset = 0;
430 s->i_byte_offset = 0;
431 TAB_APPEND( p_sys->i_seekpoint, p_sys->seekpoint, s );
435 b_last = (*pp_streaminfo)[4]&0x80;
441 i_peek = stream_Peek( p_demux->s, &p_peek, 4 );
444 b_last = p_peek[0]&0x80;
445 i_type = p_peek[0]&0x7f;
446 i_len = Get24bBE( &p_peek[1] );
448 if( i_type == META_SEEKTABLE )
450 i_peek = stream_Peek( p_demux->s, &p_peek, 4+i_len );
451 if( i_peek == 4+i_len )
452 ParseSeekTable( p_demux, p_peek, i_peek, i_sample_rate );
454 else if( i_type == META_COMMENT )
456 i_peek = stream_Peek( p_demux->s, &p_peek, 4+i_len );
457 if( i_peek == 4+i_len )
458 ParseComment( p_demux, p_peek, i_peek );
461 if( stream_Read( p_demux->s, NULL, 4+i_len ) < 4+i_len )
466 p_sys->i_data_pos = stream_Tell( p_demux->s );
470 static void ParseStreamInfo( demux_t *p_demux, int *pi_rate, int64_t *pi_count, uint8_t *p_data, int i_data )
472 const int i_skip = 4+4;
474 *pi_rate = GetDWBE(&p_data[i_skip+4+6]) >> 12;
475 *pi_count = GetQWBE(&p_data[i_skip+4+6]) & ((I64C(1)<<36)-1);
477 static void ParseSeekTable( demux_t *p_demux, uint8_t *p_data, int i_data, int i_sample_rate )
479 demux_sys_t *p_sys = p_demux->p_sys;
483 if( i_sample_rate <= 0 )
487 for( i = 0; i < (i_data-4)/18; i++ )
489 const int64_t i_sample = GetQWBE( &p_data[4+18*i+0] );
492 if( i_sample < 0 || i_sample >= INT64_MAX )
495 s = vlc_seekpoint_New();
496 s->i_time_offset = i_sample * I64C(1000000)/i_sample_rate;
497 s->i_byte_offset = GetQWBE( &p_data[4+18*i+8] );
499 /* Check for duplicate entry */
500 for( j = 0; j < p_sys->i_seekpoint; j++ )
502 if( p_sys->seekpoint[j]->i_time_offset == s->i_time_offset ||
503 p_sys->seekpoint[j]->i_byte_offset == s->i_byte_offset )
505 vlc_seekpoint_Delete( s );
512 TAB_APPEND( p_sys->i_seekpoint, p_sys->seekpoint, s );
515 /* TODO sort it by size and remove wrong seek entry (time not increasing) */
517 static inline void astrcat( char **ppsz_dst, const char *psz_src )
519 char *psz_old = *ppsz_dst;
521 if( !psz_src || !psz_src[0] )
526 asprintf( ppsz_dst, "%s,%s", psz_old, psz_src );
531 *ppsz_dst = strdup( psz_src );
535 static void ParseComment( demux_t *p_demux, uint8_t *p_data, int i_data )
537 demux_sys_t *p_sys = p_demux->p_sys;
544 #define RM(x) do { i_data -= (x); p_data += (x); } while(0)
547 n = GetDWLE(p_data); RM(4);
548 if( n < 0 || n > i_data )
553 /* TODO report vendor string ? */
554 char *psz_vendor = psz_vendor = strndup( p_data, n );
555 msg_Dbg( p_demux, "FLAC: COMMENT vendor length=%d vendor=%s\n", n, psz_vendor );
564 i_comment = GetDWLE(p_data); RM(4);
568 p_sys->p_meta = vlc_meta_New();
570 for( ; i_comment > 0; i_comment-- )
575 n = GetDWLE(p_data); RM(4);
581 psz = strndup( p_data, n );
586 #define IF_EXTRACT(txt,var) if( !strncasecmp(psz, txt, strlen(txt)) ) { astrcat( &p_sys->p_meta->var, &psz[strlen(txt)] ); }
587 IF_EXTRACT("TITLE=", psz_title )
588 else IF_EXTRACT("ALBUM=", psz_album )
589 else IF_EXTRACT("TRACKNUMBER=", psz_tracknum )
590 else IF_EXTRACT("ARTIST=", psz_artist )
591 else IF_EXTRACT("COPYRIGHT=", psz_copyright )
592 else IF_EXTRACT("DESCRIPTION=", psz_description )
593 else IF_EXTRACT("GENRE=", psz_genre )
594 else IF_EXTRACT("DATE=", psz_date )
595 else if( strchr( psz, '=' ) )
597 /* generic (PERFORMER/LICENSE/ORGANIZATION/LOCATION/CONTACT/ISRC and undocumented tags) */
598 char *p = strchr( psz, '=' );
600 vlc_meta_AddExtra( p_sys->p_meta, psz, p );