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 *****************************************************************************/
33 #include <vlc_demux.h>
35 #include <vlc_input.h>
36 #include <vlc_codec.h>
38 #include <vlc_charset.h>
40 /*****************************************************************************
42 *****************************************************************************/
43 static int Open ( vlc_object_t * );
44 static void Close ( vlc_object_t * );
47 set_description( _("FLAC demuxer") );
48 set_capability( "demux2", 155 );
49 set_category( CAT_INPUT );
50 set_subcategory( SUBCAT_INPUT_DEMUX );
51 set_callbacks( Open, Close );
52 add_shortcut( "flac" );
55 /*****************************************************************************
57 *****************************************************************************/
58 static int Demux ( demux_t * );
59 static int Control( demux_t *, int, va_list );
61 static int ReadMeta( demux_t *, uint8_t **pp_streaminfo, int *pi_streaminfo );
69 decoder_t *p_packetizer;
72 audio_replay_gain_t replay_gain;
74 int64_t i_time_offset;
78 int64_t i_length; /* Length from stream info */
83 seekpoint_t **seekpoint;
87 input_attachment_t **attachments;
92 #define STREAMINFO_SIZE 38
93 #define FLAC_PACKET_SIZE 16384
95 /*****************************************************************************
96 * Open: initializes ES structures
97 *****************************************************************************/
98 static int Open( vlc_object_t * p_this )
100 demux_t *p_demux = (demux_t*)p_this;
102 const byte_t *p_peek;
103 uint8_t *p_streaminfo;
106 /* Have a peep at the show. */
107 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 ) return VLC_EGENERIC;
109 if( p_peek[0]!='f' || p_peek[1]!='L' || p_peek[2]!='a' || p_peek[3]!='C' )
111 if( !p_demux->b_force ) return VLC_EGENERIC;
114 msg_Err( p_demux, "this doesn't look like a flac stream, "
115 "continuing anyway" );
118 p_demux->pf_demux = Demux;
119 p_demux->pf_control = Control;
120 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
121 p_sys->b_start = VLC_TRUE;
122 p_sys->p_meta = NULL;
123 memset( &p_sys->replay_gain, 0, sizeof(p_sys->replay_gain) );
125 p_sys->i_time_offset = 0;
127 p_sys->i_pts_start = 0;
129 TAB_INIT( p_sys->i_seekpoint, p_sys->seekpoint );
130 TAB_INIT( p_sys->i_attachments, p_sys->attachments);
131 p_sys->i_cover_idx = 0;
132 p_sys->i_cover_score = 0;
134 /* We need to read and store the STREAMINFO metadata */
135 if( ReadMeta( p_demux, &p_streaminfo, &i_streaminfo ) )
141 /* Load the FLAC packetizer */
142 INIT_APACKETIZER( p_sys->p_packetizer, 'f', 'l', 'a', 'c' );
144 /* Store STREAMINFO for the decoder and packetizer */
145 p_streaminfo[4] |= 0x80; /* Fake this as the last metadata block */
146 p_sys->p_packetizer->fmt_in.i_extra = i_streaminfo;
147 p_sys->p_packetizer->fmt_in.p_extra = p_streaminfo;
149 p_sys->p_packetizer->p_module =
150 module_Need( p_sys->p_packetizer, "packetizer", NULL, 0 );
151 if( !p_sys->p_packetizer->p_module )
153 if( p_sys->p_packetizer->fmt_in.p_extra )
154 free( p_sys->p_packetizer->fmt_in.p_extra );
155 vlc_object_destroy( p_sys->p_packetizer );
157 msg_Err( p_demux, "cannot find flac packetizer" );
161 if( p_sys->i_cover_idx < p_sys->i_attachments )
165 p_sys->p_meta = vlc_meta_New();
166 snprintf( psz_url, sizeof(psz_url), "attachment://%s",
167 p_sys->attachments[p_sys->i_cover_idx]->psz_name );
168 vlc_meta_Set( p_sys->p_meta, vlc_meta_ArtworkURL, psz_url );
170 vlc_audio_replay_gain_MergeFromMeta( &p_sys->replay_gain, p_sys->p_meta );
174 /*****************************************************************************
175 * Close: frees unused data
176 *****************************************************************************/
177 static void Close( vlc_object_t * p_this )
179 demux_t *p_demux = (demux_t*)p_this;
180 demux_sys_t *p_sys = p_demux->p_sys;
182 TAB_CLEAN( p_sys->i_seekpoint, p_sys->seekpoint );
185 for( i = 0; i < p_sys->i_attachments; i++ )
186 free( p_sys->attachments[i] );
187 TAB_CLEAN( p_sys->i_attachments, p_sys->attachments);
190 module_Unneed( p_sys->p_packetizer, p_sys->p_packetizer->p_module );
192 if( p_sys->p_packetizer->fmt_in.p_extra )
193 free( p_sys->p_packetizer->fmt_in.p_extra );
195 /* Delete the decoder */
196 vlc_object_destroy( p_sys->p_packetizer );
198 vlc_meta_Delete( p_sys->p_meta );
202 /*****************************************************************************
203 * Demux: reads and demuxes data packets
204 *****************************************************************************
205 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
206 *****************************************************************************/
207 static int Demux( demux_t *p_demux )
209 demux_sys_t *p_sys = p_demux->p_sys;
210 block_t *p_block_in, *p_block_out;
212 if( !( p_block_in = stream_Block( p_demux->s, FLAC_PACKET_SIZE ) ) )
215 p_block_in->i_pts = p_block_in->i_dts = p_sys->b_start ? 1 : 0;
216 p_sys->b_start = VLC_FALSE;
218 while( (p_block_out = p_sys->p_packetizer->pf_packetize(
219 p_sys->p_packetizer, &p_block_in )) )
223 block_t *p_next = p_block_out->p_next;
225 p_block_out->p_next = NULL;
227 if( p_sys->p_es == NULL )
229 p_sys->p_packetizer->fmt_out.b_packetized = VLC_TRUE;
230 p_sys->p_packetizer->fmt_out.audio_replay_gain = p_sys->replay_gain;
231 p_sys->p_es = es_out_Add( p_demux->out, &p_sys->p_packetizer->fmt_out);
235 if( p_block_out->i_dts >= p_sys->i_pts_start )
236 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block_out->i_dts );
238 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
240 p_sys->i_pts = p_block_out->i_dts;
241 es_out_Send( p_demux->out, p_sys->p_es, p_block_out );
243 p_block_out = p_next;
249 /*****************************************************************************
251 *****************************************************************************/
252 static int64_t ControlGetLength( demux_t *p_demux )
254 demux_sys_t *p_sys = p_demux->p_sys;
255 const int64_t i_size = stream_Size(p_demux->s) - p_sys->i_data_pos;
256 int64_t i_length = p_sys->i_length;
259 /* Try to fix length using seekpoint and current size for truncated file */
260 for( i = p_sys->i_seekpoint-1; i >= 0; i-- )
262 seekpoint_t *s = p_sys->seekpoint[i];
263 if( s->i_byte_offset <= i_size )
265 if( i+1 < p_sys->i_seekpoint )
268 seekpoint_t *n = p_sys->seekpoint[i+1];
269 assert( n->i_byte_offset != s->i_byte_offset); /* Should be ensured by ParseSeekTable */
270 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);
278 static int64_t ControlGetTime( demux_t *p_demux )
280 demux_sys_t *p_sys = p_demux->p_sys;
281 return __MAX(p_sys->i_pts, p_sys->i_pts_start) + p_sys->i_time_offset;
284 static int ControlSetTime( demux_t *p_demux, int64_t i_time )
286 demux_sys_t *p_sys = p_demux->p_sys;
288 int64_t i_next_offset;
289 int64_t i_delta_time;
290 int64_t i_delta_offset;
291 vlc_bool_t b_seekable;
295 stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_seekable );
300 assert( p_sys->i_seekpoint > 0 ); /* ReadMeta ensure at least (0,0) */
301 for( i = p_sys->i_seekpoint-1; i >= 0; i-- )
303 if( p_sys->seekpoint[i]->i_time_offset <= i_time )
306 if( i+1 < p_sys->i_seekpoint )
308 i_next_time = p_sys->seekpoint[i+1]->i_time_offset;
309 i_next_offset = p_sys->seekpoint[i+1]->i_byte_offset;
313 i_next_time = p_sys->i_length;
314 i_next_offset = stream_Size(p_demux->s)-p_sys->i_data_pos;
316 i_delta_time = i_time - p_sys->seekpoint[i]->i_time_offset;
317 i_delta_offset = (i_next_offset - p_sys->seekpoint[i]->i_byte_offset) * i_delta_time /
318 (p_sys->seekpoint[i+1]->i_time_offset-p_sys->seekpoint[i]->i_time_offset);
320 /* XXX We do exact seek if it's not too far away(45s) */
321 if( i_delta_time < 45*I64C(1000000) )
323 if( stream_Seek( p_demux->s, p_sys->seekpoint[i]->i_byte_offset+p_sys->i_data_pos ) )
325 p_sys->i_time_offset = p_sys->seekpoint[i]->i_time_offset - p_sys->i_pts;
326 p_sys->i_pts_start = p_sys->i_pts+i_delta_time;
327 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, p_sys->p_es, p_sys->i_pts_start );
331 if( stream_Seek( p_demux->s, p_sys->seekpoint[i]->i_byte_offset+p_sys->i_data_pos + i_delta_offset ) )
333 p_sys->i_pts_start = p_sys->i_pts;
334 p_sys->i_time_offset = (p_sys->seekpoint[i]->i_time_offset+i_delta_time) - p_sys->i_pts;
339 static int Control( demux_t *p_demux, int i_query, va_list args )
341 demux_sys_t *p_sys = p_demux->p_sys;
343 if( i_query == DEMUX_GET_META )
345 vlc_meta_t *p_meta = (vlc_meta_t *)va_arg( args, vlc_meta_t* );
346 if( p_demux->p_sys->p_meta )
347 vlc_meta_Merge( p_meta, p_demux->p_sys->p_meta );
350 else if( i_query == DEMUX_HAS_UNSUPPORTED_META )
352 vlc_bool_t *pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
356 else if( i_query == DEMUX_GET_LENGTH )
358 int64_t *pi64 = (int64_t*)va_arg( args, int64_t * );
359 *pi64 = ControlGetLength( p_demux );
362 else if( i_query == DEMUX_SET_TIME )
364 int64_t i_time = (int64_t)va_arg( args, int64_t );
365 return ControlSetTime( p_demux, i_time );
367 else if( i_query == DEMUX_SET_POSITION )
369 const double f = (double)va_arg( args, double );
370 int64_t i_time = f * ControlGetLength( p_demux );
371 return ControlSetTime( p_demux, i_time );
373 else if( i_query == DEMUX_GET_TIME )
375 int64_t *pi64 = (int64_t*)va_arg( args, int64_t * );
376 *pi64 = ControlGetTime( p_demux );
379 else if( i_query == DEMUX_GET_POSITION )
381 double *pf = (double*)va_arg( args, double * );
382 const int64_t i_length = ControlGetLength(p_demux);
384 *pf = (double)ControlGetTime(p_demux) / (double)i_length;
389 else if( i_query == DEMUX_GET_ATTACHMENTS )
391 input_attachment_t ***ppp_attach =
392 (input_attachment_t***)va_arg( args, input_attachment_t*** );
393 int *pi_int = (int*)va_arg( args, int * );
396 if( p_sys->i_attachments <= 0 )
399 *pi_int = p_sys->i_attachments;;
400 *ppp_attach = malloc( sizeof(input_attachment_t**) * p_sys->i_attachments );
401 for( i = 0; i < p_sys->i_attachments; i++ )
402 (*ppp_attach)[i] = vlc_input_attachment_Duplicate( p_sys->attachments[i] );
406 return demux2_vaControlHelper( p_demux->s, p_sys->i_data_pos, -1,
407 8*0, 1, i_query, args );
418 static inline int Get24bBE( const uint8_t *p )
420 return (p[0] << 16)|(p[1] << 8)|(p[2]);
423 static void ParseStreamInfo( demux_t *p_demux, int *pi_rate, int64_t *pi_count, uint8_t *p_data, int i_data );
424 static void ParseSeekTable( demux_t *p_demux, const uint8_t *p_data, int i_data,
426 static void ParseComment( demux_t *, const uint8_t *p_data, int i_data );
427 static void ParsePicture( demux_t *, const uint8_t *p_data, int i_data );
429 static int ReadMeta( demux_t *p_demux, uint8_t **pp_streaminfo, int *pi_streaminfo )
431 demux_sys_t *p_sys = p_demux->p_sys;
433 const uint8_t *p_peek;
436 int64_t i_sample_count;
439 /* Read STREAMINFO */
440 i_peek = stream_Peek( p_demux->s, &p_peek, 8 );
441 if( (p_peek[4] & 0x7F) != META_STREAMINFO )
443 msg_Err( p_demux, "this isn't a STREAMINFO metadata block" );
446 if( Get24bBE(&p_peek[5]) != (STREAMINFO_SIZE - 4) )
448 msg_Err( p_demux, "invalid size for a STREAMINFO metadata block" );
452 *pi_streaminfo = 4 + STREAMINFO_SIZE;
453 *pp_streaminfo = malloc( 4 + STREAMINFO_SIZE );
454 if( *pp_streaminfo == NULL )
457 if( stream_Read( p_demux->s, *pp_streaminfo, 4+STREAMINFO_SIZE ) != 4+STREAMINFO_SIZE )
459 msg_Err( p_demux, "failed to read STREAMINFO metadata block" );
460 free( *pp_streaminfo );
465 ParseStreamInfo( p_demux, &i_sample_rate, &i_sample_count, *pp_streaminfo, *pi_streaminfo );
466 if( i_sample_rate > 0 )
467 p_sys->i_length = i_sample_count * I64C(1000000)/i_sample_rate;
469 /* Be sure we have seekpoint 0 */
470 s = vlc_seekpoint_New();
471 s->i_time_offset = 0;
472 s->i_byte_offset = 0;
473 TAB_APPEND( p_sys->i_seekpoint, p_sys->seekpoint, s );
475 b_last = (*pp_streaminfo)[4]&0x80;
481 i_peek = stream_Peek( p_demux->s, &p_peek, 4 );
484 b_last = p_peek[0]&0x80;
485 i_type = p_peek[0]&0x7f;
486 i_len = Get24bBE( &p_peek[1] );
488 if( i_type == META_SEEKTABLE )
490 i_peek = stream_Peek( p_demux->s, &p_peek, 4+i_len );
491 if( i_peek == 4+i_len )
492 ParseSeekTable( p_demux, p_peek, i_peek, i_sample_rate );
494 else if( i_type == META_COMMENT )
496 i_peek = stream_Peek( p_demux->s, &p_peek, 4+i_len );
497 if( i_peek == 4+i_len )
498 ParseComment( p_demux, p_peek, i_peek );
500 else if( i_type == META_PICTURE )
502 i_peek = stream_Peek( p_demux->s, &p_peek, 4+i_len );
503 if( i_peek == 4+i_len )
504 ParsePicture( p_demux, p_peek, i_peek );
507 if( stream_Read( p_demux->s, NULL, 4+i_len ) < 4+i_len )
512 p_sys->i_data_pos = stream_Tell( p_demux->s );
516 static void ParseStreamInfo( demux_t *p_demux, int *pi_rate, int64_t *pi_count, uint8_t *p_data, int i_data )
518 const int i_skip = 4+4;
520 *pi_rate = GetDWBE(&p_data[i_skip+4+6]) >> 12;
521 *pi_count = GetQWBE(&p_data[i_skip+4+6]) & ((I64C(1)<<36)-1);
524 static void ParseSeekTable( demux_t *p_demux, const uint8_t *p_data, int i_data,
527 demux_sys_t *p_sys = p_demux->p_sys;
531 if( i_sample_rate <= 0 )
535 for( i = 0; i < (i_data-4)/18; i++ )
537 const int64_t i_sample = GetQWBE( &p_data[4+18*i+0] );
540 if( i_sample < 0 || i_sample >= INT64_MAX )
543 s = vlc_seekpoint_New();
544 s->i_time_offset = i_sample * I64C(1000000)/i_sample_rate;
545 s->i_byte_offset = GetQWBE( &p_data[4+18*i+8] );
547 /* Check for duplicate entry */
548 for( j = 0; j < p_sys->i_seekpoint; j++ )
550 if( p_sys->seekpoint[j]->i_time_offset == s->i_time_offset ||
551 p_sys->seekpoint[j]->i_byte_offset == s->i_byte_offset )
553 vlc_seekpoint_Delete( s );
560 TAB_APPEND( p_sys->i_seekpoint, p_sys->seekpoint, s );
563 /* TODO sort it by size and remove wrong seek entry (time not increasing) */
566 #define RM(x) do { i_data -= (x); p_data += (x); } while(0)
567 static void ParseComment( demux_t *p_demux, const uint8_t *p_data, int i_data )
569 demux_sys_t *p_sys = p_demux->p_sys;
578 n = GetDWLE(p_data); RM(4);
579 if( n < 0 || n > i_data )
584 /* TODO report vendor string ? */
585 char *psz_vendor = psz_vendor = strndup( p_data, n );
586 msg_Dbg( p_demux, "FLAC: COMMENT vendor length=%d vendor=%s\n", n, psz_vendor );
595 i_comment = GetDWLE(p_data); RM(4);
599 p_sys->p_meta = vlc_meta_New();
601 for( ; i_comment > 0; i_comment-- )
606 n = GetDWLE(p_data); RM(4);
612 psz = strndup( p_data, n );
617 #define IF_EXTRACT(txt,var) \
618 if( !strncasecmp(psz, txt, strlen(txt)) ) \
620 const char *oldval = vlc_meta_Get( p_sys->p_meta, vlc_meta_ ## var ); \
624 asprintf( &newval, "%s,%s", oldval, &psz[strlen(txt)] ); \
625 vlc_meta_Set( p_sys->p_meta, vlc_meta_ ## var, newval ); \
629 vlc_meta_Set( p_sys->p_meta, vlc_meta_ ## var, &psz[strlen(txt)] ); \
631 IF_EXTRACT("TITLE=", Title )
632 else IF_EXTRACT("ALBUM=", Album )
633 else IF_EXTRACT("TRACKNUMBER=", TrackNumber )
634 else IF_EXTRACT("ARTIST=", Artist )
635 else IF_EXTRACT("COPYRIGHT=", Copyright )
636 else IF_EXTRACT("DESCRIPTION=", Description )
637 else IF_EXTRACT("GENRE=", Genre )
638 else IF_EXTRACT("DATE=", Date )
639 else if( strchr( psz, '=' ) )
641 /* generic (PERFORMER/LICENSE/ORGANIZATION/LOCATION/CONTACT/ISRC,
642 * undocumented tags and replay gain ) */
643 char *p = strchr( psz, '=' );
645 vlc_meta_AddExtra( p_sys->p_meta, psz, p );
653 static void ParsePicture( demux_t *p_demux, const uint8_t *p_data, int i_data )
655 static const int pi_cover_score[] = {
658 10, /* front cover */
660 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
661 6, /* movie/video screen capture */
663 7, /* Illustration */
664 8, /* Band/Artist logotype */
665 0, /* Publisher/Studio */
667 demux_sys_t *p_sys = p_demux->p_sys;
670 char *psz_mime = NULL;
671 char *psz_description = NULL;
672 input_attachment_t *p_attachment;
675 if( i_data < 4 + 3*4 )
677 #define RM(x) do { i_data -= (x); p_data += (x); } while(0)
680 i_type = GetDWBE( p_data ); RM(4);
681 i_len = GetDWBE( p_data ); RM(4);
682 if( i_data < i_len + 4 )
684 psz_mime = strndup( p_data, i_len ); RM(i_len);
685 i_len = GetDWBE( p_data ); RM(4);
686 if( i_data < i_len + 4*4 + 4)
688 psz_description = strndup( p_data, i_len ); RM(i_len);
689 EnsureUTF8( psz_description );
691 i_len = GetDWBE( p_data ); RM(4);
695 msg_Dbg( p_demux, "FLAC: Picture type=%d mime=%s description='%s' file length=%d",
696 i_type, psz_mime, psz_description, i_len );
698 snprintf( psz_name, sizeof(psz_name), "picture%d", p_sys->i_attachments );
699 if( !strcasecmp( psz_mime, "image/jpeg" ) )
700 strcat( psz_name, ".jpg" );
701 else if( !strcasecmp( psz_mime, "image/png" ) )
702 strcat( psz_name, ".png" );
704 p_attachment = vlc_input_attachment_New( psz_name, psz_mime, psz_description,
706 TAB_APPEND( p_sys->i_attachments, p_sys->attachments, p_attachment );
708 if( i_type >= 0 && i_type < sizeof(pi_cover_score)/sizeof(pi_cover_score[0]) &&
709 p_sys->i_cover_score < pi_cover_score[i_type] )
711 p_sys->i_cover_idx = p_sys->i_attachments-1;
712 p_sys->i_cover_score = pi_cover_score[i_type];
717 if( psz_description )
718 free( psz_description );