1 /*****************************************************************************
2 * flac.c : FLAC demux module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2007 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>
34 #include <vlc_charset.h>
36 /*****************************************************************************
38 *****************************************************************************/
39 static int Open ( vlc_object_t * );
40 static void Close ( vlc_object_t * );
43 set_description( _("FLAC demuxer") );
44 set_capability( "demux2", 155 );
45 set_category( CAT_INPUT );
46 set_subcategory( SUBCAT_INPUT_DEMUX );
47 set_callbacks( Open, Close );
48 add_shortcut( "flac" );
51 /*****************************************************************************
53 *****************************************************************************/
54 static int Demux ( demux_t * );
55 static int Control( demux_t *, int, va_list );
57 static int ReadMeta( demux_t *, uint8_t **pp_streaminfo, int *pi_streaminfo );
65 decoder_t *p_packetizer;
67 audio_replay_gain_t replay_gain;
69 int64_t i_time_offset;
73 int64_t i_length; /* Length from stream info */
78 seekpoint_t **seekpoint;
82 input_attachment_t **attachment;
87 #define STREAMINFO_SIZE 38
88 #define FLAC_PACKET_SIZE 16384
90 /*****************************************************************************
91 * Open: initializes ES structures
92 *****************************************************************************/
93 static int Open( vlc_object_t * p_this )
95 demux_t *p_demux = (demux_t*)p_this;
99 uint8_t *p_streaminfo;
102 /* Have a peep at the show. */
103 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 ) return VLC_EGENERIC;
105 if( p_peek[0]!='f' || p_peek[1]!='L' || p_peek[2]!='a' || p_peek[3]!='C' )
107 if( !p_demux->b_force ) return VLC_EGENERIC;
110 msg_Err( p_demux, "this doesn't look like a flac stream, "
111 "continuing anyway" );
114 p_demux->pf_demux = Demux;
115 p_demux->pf_control = Control;
116 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
117 p_sys->b_start = VLC_TRUE;
118 p_sys->p_meta = NULL;
119 memset( &p_sys->replay_gain, 0, sizeof(p_sys->replay_gain) );
121 p_sys->i_time_offset = 0;
123 p_sys->i_pts_start = 0;
125 TAB_INIT( p_sys->i_seekpoint, p_sys->seekpoint );
126 TAB_INIT( p_sys->i_attachment, p_sys->attachment );
127 p_sys->i_cover_idx = 0;
128 p_sys->i_cover_score = 0;
130 /* We need to read and store the STREAMINFO metadata */
131 if( ReadMeta( p_demux, &p_streaminfo, &i_streaminfo ) )
137 /* Load the FLAC packetizer */
138 INIT_APACKETIZER( p_sys->p_packetizer, 'f', 'l', 'a', 'c' );
140 /* Store STREAMINFO for the decoder and packetizer */
141 p_streaminfo[4] |= 0x80; /* Fake this as the last metadata block */
142 p_sys->p_packetizer->fmt_in.i_extra = i_streaminfo;
143 p_sys->p_packetizer->fmt_in.p_extra = p_streaminfo;
145 p_sys->p_packetizer->p_module =
146 module_Need( p_sys->p_packetizer, "packetizer", NULL, 0 );
147 if( !p_sys->p_packetizer->p_module )
149 if( p_sys->p_packetizer->fmt_in.p_extra )
150 free( p_sys->p_packetizer->fmt_in.p_extra );
151 vlc_object_destroy( p_sys->p_packetizer );
153 msg_Err( p_demux, "cannot find flac packetizer" );
157 /* Parse possible id3 header */
158 if( ( p_id3 = module_Need( p_demux, "meta reader", NULL, 0 ) ) )
160 vlc_meta_t *p_meta = (vlc_meta_t *)p_demux->p_private;
164 p_sys->p_meta = p_meta;
168 vlc_meta_Merge( p_sys->p_meta, p_meta );
169 vlc_meta_Delete( p_meta );
171 p_demux->p_private = NULL;
172 module_Unneed( p_demux, p_id3 );
175 if( p_sys->i_cover_idx < p_sys->i_attachment )
179 p_sys->p_meta = vlc_meta_New();
180 snprintf( psz_url, sizeof(psz_url), "attachment://%s",
181 p_sys->attachment[p_sys->i_cover_idx]->psz_name );
182 vlc_meta_Set( p_sys->p_meta, vlc_meta_ArtworkURL, psz_url );
184 vlc_audio_replay_gain_MergeFromMeta( &p_sys->replay_gain, p_sys->p_meta );
188 /*****************************************************************************
189 * Close: frees unused data
190 *****************************************************************************/
191 static void Close( vlc_object_t * p_this )
193 demux_t *p_demux = (demux_t*)p_this;
194 demux_sys_t *p_sys = p_demux->p_sys;
197 module_Unneed( p_sys->p_packetizer, p_sys->p_packetizer->p_module );
199 if( p_sys->p_packetizer->fmt_in.p_extra )
200 free( p_sys->p_packetizer->fmt_in.p_extra );
202 /* Delete the decoder */
203 vlc_object_destroy( p_sys->p_packetizer );
205 vlc_meta_Delete( p_sys->p_meta );
209 /*****************************************************************************
210 * Demux: reads and demuxes data packets
211 *****************************************************************************
212 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
213 *****************************************************************************/
214 static int Demux( demux_t *p_demux )
216 demux_sys_t *p_sys = p_demux->p_sys;
217 block_t *p_block_in, *p_block_out;
219 if( !( p_block_in = stream_Block( p_demux->s, FLAC_PACKET_SIZE ) ) )
222 p_block_in->i_pts = p_block_in->i_dts = p_sys->b_start ? 1 : 0;
223 p_sys->b_start = VLC_FALSE;
225 while( (p_block_out = p_sys->p_packetizer->pf_packetize(
226 p_sys->p_packetizer, &p_block_in )) )
230 block_t *p_next = p_block_out->p_next;
232 p_block_out->p_next = NULL;
234 if( p_sys->p_es == NULL )
236 p_sys->p_packetizer->fmt_out.b_packetized = VLC_TRUE;
237 p_sys->p_packetizer->fmt_out.audio_replay_gain = p_sys->replay_gain;
238 p_sys->p_es = es_out_Add( p_demux->out, &p_sys->p_packetizer->fmt_out);
242 if( p_block_out->i_dts >= p_sys->i_pts_start )
243 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block_out->i_dts );
245 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
247 p_sys->i_pts = p_block_out->i_dts;
248 es_out_Send( p_demux->out, p_sys->p_es, p_block_out );
250 p_block_out = p_next;
256 /*****************************************************************************
258 *****************************************************************************/
259 static int64_t ControlGetLength( demux_t *p_demux )
261 demux_sys_t *p_sys = p_demux->p_sys;
262 const int64_t i_size = stream_Size(p_demux->s) - p_sys->i_data_pos;
263 int64_t i_length = p_sys->i_length;
266 /* Try to fix length using seekpoint and current size for truncated file */
267 for( i = p_sys->i_seekpoint-1; i >= 0; i-- )
269 seekpoint_t *s = p_sys->seekpoint[i];
270 if( s->i_byte_offset <= i_size )
272 if( i+1 < p_sys->i_seekpoint )
275 seekpoint_t *n = p_sys->seekpoint[i+1];
276 assert( n->i_byte_offset != s->i_byte_offset); /* Should be ensured by ParseSeekTable */
277 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);
285 static int64_t ControlGetTime( demux_t *p_demux )
287 demux_sys_t *p_sys = p_demux->p_sys;
288 return __MAX(p_sys->i_pts, p_sys->i_pts_start) + p_sys->i_time_offset;
291 static int ControlSetTime( demux_t *p_demux, int64_t i_time )
293 demux_sys_t *p_sys = p_demux->p_sys;
295 int64_t i_next_offset;
296 int64_t i_delta_time;
297 int64_t i_delta_offset;
298 vlc_bool_t b_seekable;
302 stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_seekable );
307 assert( p_sys->i_seekpoint > 0 ); /* ReadMeta ensure at least (0,0) */
308 for( i = p_sys->i_seekpoint-1; i >= 0; i-- )
310 if( p_sys->seekpoint[i]->i_time_offset <= i_time )
313 if( i+1 < p_sys->i_seekpoint )
315 i_next_time = p_sys->seekpoint[i+1]->i_time_offset;
316 i_next_offset = p_sys->seekpoint[i+1]->i_byte_offset;
320 i_next_time = p_sys->i_length;
321 i_next_offset = stream_Size(p_demux->s)-p_sys->i_data_pos;
323 i_delta_time = i_time - p_sys->seekpoint[i]->i_time_offset;
324 i_delta_offset = (i_next_offset - p_sys->seekpoint[i]->i_byte_offset) * i_delta_time /
325 (p_sys->seekpoint[i+1]->i_time_offset-p_sys->seekpoint[i]->i_time_offset);
327 /* XXX We do exact seek if it's not too far away(45s) */
328 if( i_delta_time < 45*I64C(1000000) )
330 if( stream_Seek( p_demux->s, p_sys->seekpoint[i]->i_byte_offset+p_sys->i_data_pos ) )
332 p_sys->i_time_offset = p_sys->seekpoint[i]->i_time_offset - p_sys->i_pts;
333 p_sys->i_pts_start = p_sys->i_pts+i_delta_time;
334 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, p_sys->p_es, p_sys->i_pts_start );
338 if( stream_Seek( p_demux->s, p_sys->seekpoint[i]->i_byte_offset+p_sys->i_data_pos + i_delta_offset ) )
340 p_sys->i_pts_start = p_sys->i_pts;
341 p_sys->i_time_offset = (p_sys->seekpoint[i]->i_time_offset+i_delta_time) - p_sys->i_pts;
346 static int Control( demux_t *p_demux, int i_query, va_list args )
348 demux_sys_t *p_sys = p_demux->p_sys;
350 if( i_query == DEMUX_GET_META )
352 vlc_meta_t *p_meta = (vlc_meta_t *)va_arg( args, vlc_meta_t* );
353 if( p_demux->p_sys->p_meta )
354 vlc_meta_Merge( p_meta, p_demux->p_sys->p_meta );
357 else if( i_query == DEMUX_GET_LENGTH )
359 int64_t *pi64 = (int64_t*)va_arg( args, int64_t * );
360 *pi64 = ControlGetLength( p_demux );
363 else if( i_query == DEMUX_SET_TIME )
365 int64_t i_time = (int64_t)va_arg( args, int64_t );
366 return ControlSetTime( p_demux, i_time );
368 else if( i_query == DEMUX_SET_POSITION )
370 const double f = (double)va_arg( args, double );
371 int64_t i_time = f * ControlGetLength( p_demux );
372 return ControlSetTime( p_demux, i_time );
374 else if( i_query == DEMUX_GET_TIME )
376 int64_t *pi64 = (int64_t*)va_arg( args, int64_t * );
377 *pi64 = ControlGetTime( p_demux );
380 else if( i_query == DEMUX_GET_POSITION )
382 double *pf = (double*)va_arg( args, double * );
383 const int64_t i_length = ControlGetLength(p_demux);
385 *pf = (double)ControlGetTime(p_demux) / (double)i_length;
390 else if( i_query == DEMUX_GET_ATTACHMENTS )
392 input_attachment_t ***ppp_attach =
393 (input_attachment_t***)va_arg( args, input_attachment_t*** );
394 int *pi_int = (int*)va_arg( args, int * );
397 if( p_sys->i_attachment <= 0 )
400 *pi_int = p_sys->i_attachment;;
401 *ppp_attach = malloc( sizeof(input_attachment_t**) * p_sys->i_attachment );
402 for( i = 0; i < p_sys->i_attachment; i++ )
403 *(ppp_attach)[i] = vlc_input_attachment_Duplicate( p_sys->attachment[i] );
407 return demux2_vaControlHelper( p_demux->s, p_sys->i_data_pos, -1,
408 8*0, 1, i_query, args );
419 static inline int Get24bBE( const uint8_t *p )
421 return (p[0] << 16)|(p[1] << 8)|(p[2]);
424 static void ParseStreamInfo( demux_t *p_demux, int *pi_rate, int64_t *pi_count, uint8_t *p_data, int i_data );
425 static void ParseSeekTable( demux_t *p_demux, const uint8_t *p_data, int i_data,
427 static void ParseComment( demux_t *, const uint8_t *p_data, int i_data );
428 static void ParsePicture( demux_t *, const uint8_t *p_data, int i_data );
430 static int ReadMeta( demux_t *p_demux, uint8_t **pp_streaminfo, int *pi_streaminfo )
432 demux_sys_t *p_sys = p_demux->p_sys;
434 const uint8_t *p_peek;
437 int64_t i_sample_count;
440 /* Read STREAMINFO */
441 i_peek = stream_Peek( p_demux->s, &p_peek, 8 );
442 if( (p_peek[4] & 0x7F) != META_STREAMINFO )
444 msg_Err( p_demux, "this isn't a STREAMINFO metadata block" );
447 if( Get24bBE(&p_peek[5]) != (STREAMINFO_SIZE - 4) )
449 msg_Err( p_demux, "invalid size for a STREAMINFO metadata block" );
453 *pi_streaminfo = 4 + STREAMINFO_SIZE;
454 *pp_streaminfo = malloc( 4 + STREAMINFO_SIZE );
455 if( *pp_streaminfo == NULL )
458 if( stream_Read( p_demux->s, *pp_streaminfo, 4+STREAMINFO_SIZE ) != 4+STREAMINFO_SIZE )
460 msg_Err( p_demux, "failed to read STREAMINFO metadata block" );
461 free( *pp_streaminfo );
466 ParseStreamInfo( p_demux, &i_sample_rate, &i_sample_count, *pp_streaminfo, *pi_streaminfo );
467 if( i_sample_rate > 0 )
468 p_sys->i_length = i_sample_count * I64C(1000000)/i_sample_rate;
470 /* Be sure we have seekpoint 0 */
471 s = vlc_seekpoint_New();
472 s->i_time_offset = 0;
473 s->i_byte_offset = 0;
474 TAB_APPEND( p_sys->i_seekpoint, p_sys->seekpoint, s );
478 b_last = (*pp_streaminfo)[4]&0x80;
484 i_peek = stream_Peek( p_demux->s, &p_peek, 4 );
487 b_last = p_peek[0]&0x80;
488 i_type = p_peek[0]&0x7f;
489 i_len = Get24bBE( &p_peek[1] );
491 if( i_type == META_SEEKTABLE )
493 i_peek = stream_Peek( p_demux->s, &p_peek, 4+i_len );
494 if( i_peek == 4+i_len )
495 ParseSeekTable( p_demux, p_peek, i_peek, i_sample_rate );
497 else if( i_type == META_COMMENT )
499 i_peek = stream_Peek( p_demux->s, &p_peek, 4+i_len );
500 if( i_peek == 4+i_len )
501 ParseComment( p_demux, p_peek, i_peek );
503 else if( i_type == META_PICTURE )
505 i_peek = stream_Peek( p_demux->s, &p_peek, 4+i_len );
506 if( i_peek == 4+i_len )
507 ParsePicture( p_demux, p_peek, i_peek );
510 if( stream_Read( p_demux->s, NULL, 4+i_len ) < 4+i_len )
515 p_sys->i_data_pos = stream_Tell( p_demux->s );
519 static void ParseStreamInfo( demux_t *p_demux, int *pi_rate, int64_t *pi_count, uint8_t *p_data, int i_data )
521 const int i_skip = 4+4;
523 *pi_rate = GetDWBE(&p_data[i_skip+4+6]) >> 12;
524 *pi_count = GetQWBE(&p_data[i_skip+4+6]) & ((I64C(1)<<36)-1);
527 static void ParseSeekTable( demux_t *p_demux, const uint8_t *p_data, int i_data,
530 demux_sys_t *p_sys = p_demux->p_sys;
534 if( i_sample_rate <= 0 )
538 for( i = 0; i < (i_data-4)/18; i++ )
540 const int64_t i_sample = GetQWBE( &p_data[4+18*i+0] );
543 if( i_sample < 0 || i_sample >= INT64_MAX )
546 s = vlc_seekpoint_New();
547 s->i_time_offset = i_sample * I64C(1000000)/i_sample_rate;
548 s->i_byte_offset = GetQWBE( &p_data[4+18*i+8] );
550 /* Check for duplicate entry */
551 for( j = 0; j < p_sys->i_seekpoint; j++ )
553 if( p_sys->seekpoint[j]->i_time_offset == s->i_time_offset ||
554 p_sys->seekpoint[j]->i_byte_offset == s->i_byte_offset )
556 vlc_seekpoint_Delete( s );
563 TAB_APPEND( p_sys->i_seekpoint, p_sys->seekpoint, s );
566 /* TODO sort it by size and remove wrong seek entry (time not increasing) */
569 #define RM(x) do { i_data -= (x); p_data += (x); } while(0)
570 static void ParseComment( demux_t *p_demux, const uint8_t *p_data, int i_data )
572 demux_sys_t *p_sys = p_demux->p_sys;
581 n = GetDWLE(p_data); RM(4);
582 if( n < 0 || n > i_data )
587 /* TODO report vendor string ? */
588 char *psz_vendor = psz_vendor = strndup( p_data, n );
589 msg_Dbg( p_demux, "FLAC: COMMENT vendor length=%d vendor=%s\n", n, psz_vendor );
598 i_comment = GetDWLE(p_data); RM(4);
602 p_sys->p_meta = vlc_meta_New();
604 for( ; i_comment > 0; i_comment-- )
609 n = GetDWLE(p_data); RM(4);
615 psz = strndup( p_data, n );
620 #define IF_EXTRACT(txt,var) \
621 if( !strncasecmp(psz, txt, strlen(txt)) ) \
623 char * oldval = vlc_meta_Get( p_sys->p_meta, vlc_meta_ ## var ); \
627 asprintf( &newval, "%s,%s", oldval, &psz[strlen(txt)] ); \
628 vlc_meta_Set( p_sys->p_meta, vlc_meta_ ## var, newval ); \
632 vlc_meta_Set( p_sys->p_meta, vlc_meta_ ## var, &psz[strlen(txt)] ); \
635 IF_EXTRACT("TITLE=", Title )
636 else IF_EXTRACT("ALBUM=", Album )
637 else IF_EXTRACT("TRACKNUMBER=", TrackNumber )
638 else IF_EXTRACT("ARTIST=", Artist )
639 else IF_EXTRACT("COPYRIGHT=", Copyright )
640 else IF_EXTRACT("DESCRIPTION=", Description )
641 else IF_EXTRACT("GENRE=", Genre )
642 else IF_EXTRACT("DATE=", Date )
643 else if( strchr( psz, '=' ) )
645 /* generic (PERFORMER/LICENSE/ORGANIZATION/LOCATION/CONTACT/ISRC,
646 * undocumented tags and replay gain ) */
647 audio_replay_gain_t *r = &p_sys->replay_gain;
648 char *p = strchr( psz, '=' );
650 vlc_meta_AddExtra( p_sys->p_meta, psz, p );
658 static void ParsePicture( demux_t *p_demux, const uint8_t *p_data, int i_data )
660 static const int pi_cover_score[] = {
663 10, /* front cover */
665 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
666 6, /* movie/video screen capture */
668 7, /* Illustration */
669 8, /* Band/Artist logotype */
670 0, /* Publisher/Studio */
672 demux_sys_t *p_sys = p_demux->p_sys;
675 char *psz_mime = NULL;
676 char *psz_description = NULL;
677 input_attachment_t *p_attachment;
680 if( i_data < 4 + 3*4 )
682 #define RM(x) do { i_data -= (x); p_data += (x); } while(0)
685 i_type = GetDWBE( p_data ); RM(4);
686 i_len = GetDWBE( p_data ); RM(4);
687 if( i_data < i_len + 4 )
689 psz_mime = strndup( p_data, i_len ); RM(i_len);
690 i_len = GetDWBE( p_data ); RM(4);
691 if( i_data < i_len + 4*4 + 4)
693 psz_description = strndup( p_data, i_len ); RM(i_len);
694 EnsureUTF8( psz_description );
696 i_len = GetDWBE( p_data ); RM(4);
700 msg_Dbg( p_demux, "FLAC: Picture type=%d mime=%s description='%s' file length=%d",
701 i_type, psz_mime, psz_description, i_len );
703 snprintf( psz_name, sizeof(psz_name), "picture%d", p_sys->i_attachment );
704 if( !strcasecmp( psz_mime, "image/jpeg" ) )
705 strcat( psz_name, ".jpg" );
706 else if( !strcasecmp( psz_mime, "image/png" ) )
707 strcat( psz_name, ".png" );
709 p_attachment = vlc_input_attachment_New( psz_name, psz_mime, psz_description,
711 TAB_APPEND( p_sys->i_attachment, p_sys->attachment, p_attachment );
713 if( i_type >= 0 && i_type < sizeof(pi_cover_score)/sizeof(pi_cover_score[0]) &&
714 p_sys->i_cover_score < pi_cover_score[i_type] )
716 p_sys->i_cover_idx = p_sys->i_attachment-1;
717 p_sys->i_cover_score = pi_cover_score[i_type];
722 if( psz_description )
723 free( psz_description );