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 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_demux.h>
36 #include <vlc_input.h>
37 #include <vlc_codec.h>
39 #include <vlc_charset.h>
41 /*****************************************************************************
43 *****************************************************************************/
44 static int Open ( vlc_object_t * );
45 static void Close ( vlc_object_t * );
48 set_description( N_("FLAC demuxer") );
49 set_capability( "demux", 155 );
50 set_category( CAT_INPUT );
51 set_subcategory( SUBCAT_INPUT_DEMUX );
52 set_callbacks( Open, Close );
53 add_shortcut( "flac" );
56 /*****************************************************************************
58 *****************************************************************************/
59 static int Demux ( demux_t * );
60 static int Control( demux_t *, int, va_list );
62 static int ReadMeta( demux_t *, uint8_t **pp_streaminfo, int *pi_streaminfo );
70 decoder_t *p_packetizer;
73 audio_replay_gain_t replay_gain;
75 int64_t i_time_offset;
79 int64_t i_length; /* Length from stream info */
84 seekpoint_t **seekpoint;
88 input_attachment_t **attachments;
93 #define STREAMINFO_SIZE 38
94 #define FLAC_PACKET_SIZE 16384
96 /*****************************************************************************
97 * Open: initializes ES structures
98 *****************************************************************************/
99 static int Open( vlc_object_t * p_this )
101 demux_t *p_demux = (demux_t*)p_this;
103 const uint8_t *p_peek;
104 uint8_t *p_streaminfo;
108 /* Have a peep at the show. */
109 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 ) return VLC_EGENERIC;
111 if( p_peek[0]!='f' || p_peek[1]!='L' || p_peek[2]!='a' || p_peek[3]!='C' )
113 if( !p_demux->b_force ) return VLC_EGENERIC;
116 msg_Err( p_demux, "this doesn't look like a flac stream, "
117 "continuing anyway" );
120 p_demux->pf_demux = Demux;
121 p_demux->pf_control = Control;
122 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
123 p_sys->b_start = true;
124 p_sys->p_meta = NULL;
125 memset( &p_sys->replay_gain, 0, sizeof(p_sys->replay_gain) );
127 p_sys->i_time_offset = 0;
129 p_sys->i_pts_start = 0;
131 TAB_INIT( p_sys->i_seekpoint, p_sys->seekpoint );
132 TAB_INIT( p_sys->i_attachments, p_sys->attachments);
133 p_sys->i_cover_idx = 0;
134 p_sys->i_cover_score = 0;
136 /* We need to read and store the STREAMINFO metadata */
137 if( ReadMeta( p_demux, &p_streaminfo, &i_streaminfo ) )
143 /* Load the FLAC packetizer */
144 /* Store STREAMINFO for the decoder and packetizer */
145 p_streaminfo[4] |= 0x80; /* Fake this as the last metadata block */
146 es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 'f', 'l', 'a', 'c' ) );
147 fmt.i_extra = i_streaminfo;
148 fmt.p_extra = p_streaminfo;
150 p_sys->p_packetizer = demux_PacketizerNew( p_demux, &fmt, "flac" );
151 if( !p_sys->p_packetizer )
157 if( p_sys->i_cover_idx < p_sys->i_attachments )
161 p_sys->p_meta = vlc_meta_New();
162 snprintf( psz_url, sizeof(psz_url), "attachment://%s",
163 p_sys->attachments[p_sys->i_cover_idx]->psz_name );
164 vlc_meta_Set( p_sys->p_meta, vlc_meta_ArtworkURL, psz_url );
166 vlc_audio_replay_gain_MergeFromMeta( &p_sys->replay_gain, p_sys->p_meta );
170 /*****************************************************************************
171 * Close: frees unused data
172 *****************************************************************************/
173 static void Close( vlc_object_t * p_this )
175 demux_t *p_demux = (demux_t*)p_this;
176 demux_sys_t *p_sys = p_demux->p_sys;
178 TAB_CLEAN( p_sys->i_seekpoint, p_sys->seekpoint );
181 for( i = 0; i < p_sys->i_attachments; i++ )
182 free( p_sys->attachments[i] );
183 TAB_CLEAN( p_sys->i_attachments, p_sys->attachments);
185 /* Delete the decoder */
186 demux_PacketizerDestroy( p_sys->p_packetizer );
189 vlc_meta_Delete( p_sys->p_meta );
193 /*****************************************************************************
194 * Demux: reads and demuxes data packets
195 *****************************************************************************
196 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
197 *****************************************************************************/
198 static int Demux( demux_t *p_demux )
200 demux_sys_t *p_sys = p_demux->p_sys;
201 block_t *p_block_in, *p_block_out;
203 if( !( p_block_in = stream_Block( p_demux->s, FLAC_PACKET_SIZE ) ) )
206 p_block_in->i_pts = p_block_in->i_dts = p_sys->b_start ? 1 : 0;
207 p_sys->b_start = false;
209 while( (p_block_out = p_sys->p_packetizer->pf_packetize(
210 p_sys->p_packetizer, &p_block_in )) )
214 block_t *p_next = p_block_out->p_next;
216 p_block_out->p_next = NULL;
218 if( p_sys->p_es == NULL )
220 p_sys->p_packetizer->fmt_out.b_packetized = true;
221 p_sys->p_packetizer->fmt_out.audio_replay_gain = p_sys->replay_gain;
222 p_sys->p_es = es_out_Add( p_demux->out, &p_sys->p_packetizer->fmt_out);
225 p_sys->i_pts = p_block_out->i_dts;
227 /* Correct timestamp */
228 p_block_out->i_pts += p_sys->i_time_offset;
229 p_block_out->i_dts += p_sys->i_time_offset;
232 if( p_block_out->i_dts >= p_sys->i_pts_start + p_sys->i_time_offset )
233 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block_out->i_dts );
235 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
237 es_out_Send( p_demux->out, p_sys->p_es, p_block_out );
239 p_block_out = p_next;
245 /*****************************************************************************
247 *****************************************************************************/
248 static int64_t ControlGetLength( demux_t *p_demux )
250 demux_sys_t *p_sys = p_demux->p_sys;
251 const int64_t i_size = stream_Size(p_demux->s) - p_sys->i_data_pos;
252 int64_t i_length = p_sys->i_length;
255 /* Try to fix length using seekpoint and current size for truncated file */
256 for( i = p_sys->i_seekpoint-1; i >= 0; i-- )
258 seekpoint_t *s = p_sys->seekpoint[i];
259 if( s->i_byte_offset <= i_size )
261 if( i+1 < p_sys->i_seekpoint )
264 seekpoint_t *n = p_sys->seekpoint[i+1];
265 assert( n->i_byte_offset != s->i_byte_offset); /* Should be ensured by ParseSeekTable */
266 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);
274 static int64_t ControlGetTime( demux_t *p_demux )
276 demux_sys_t *p_sys = p_demux->p_sys;
277 return __MAX(p_sys->i_pts, p_sys->i_pts_start) + p_sys->i_time_offset;
280 static int ControlSetTime( demux_t *p_demux, int64_t i_time )
282 demux_sys_t *p_sys = p_demux->p_sys;
283 int64_t i_delta_time;
288 stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_seekable );
293 assert( p_sys->i_seekpoint > 0 ); /* ReadMeta ensure at least (0,0) */
294 for( i = p_sys->i_seekpoint-1; i >= 0; i-- )
296 if( p_sys->seekpoint[i]->i_time_offset <= i_time )
299 i_delta_time = i_time - p_sys->seekpoint[i]->i_time_offset;
301 /* XXX We do exact seek if it's not too far away(45s) */
302 if( i_delta_time < 45*INT64_C(1000000) )
304 if( stream_Seek( p_demux->s, p_sys->seekpoint[i]->i_byte_offset+p_sys->i_data_pos ) )
307 p_sys->i_time_offset = p_sys->seekpoint[i]->i_time_offset - p_sys->i_pts;
308 p_sys->i_pts_start = p_sys->i_pts+i_delta_time;
309 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, p_sys->p_es, p_sys->i_pts_start + p_sys->i_time_offset );
313 int64_t i_delta_offset;
315 int64_t i_next_offset;
317 if( i+1 < p_sys->i_seekpoint )
319 i_next_time = p_sys->seekpoint[i+1]->i_time_offset;
320 i_next_offset = p_sys->seekpoint[i+1]->i_byte_offset;
324 i_next_time = p_sys->i_length;
325 i_next_offset = stream_Size(p_demux->s)-p_sys->i_data_pos;
329 if( i_next_time-p_sys->seekpoint[i]->i_time_offset > 0 )
330 i_delta_offset = (i_next_offset - p_sys->seekpoint[i]->i_byte_offset) * i_delta_time /
331 (i_next_time-p_sys->seekpoint[i]->i_time_offset);
333 if( stream_Seek( p_demux->s, p_sys->seekpoint[i]->i_byte_offset+p_sys->i_data_pos + i_delta_offset ) )
336 p_sys->i_pts_start = p_sys->i_pts;
337 p_sys->i_time_offset = (p_sys->seekpoint[i]->i_time_offset+i_delta_time) - p_sys->i_pts;
342 static int Control( demux_t *p_demux, int i_query, va_list args )
344 demux_sys_t *p_sys = p_demux->p_sys;
346 if( i_query == DEMUX_GET_META )
348 vlc_meta_t *p_meta = (vlc_meta_t *)va_arg( args, vlc_meta_t* );
349 if( p_demux->p_sys->p_meta )
350 vlc_meta_Merge( p_meta, p_demux->p_sys->p_meta );
353 else if( i_query == DEMUX_HAS_UNSUPPORTED_META )
355 bool *pb_bool = (bool*)va_arg( args, bool* );
359 else if( i_query == DEMUX_GET_LENGTH )
361 int64_t *pi64 = (int64_t*)va_arg( args, int64_t * );
362 *pi64 = ControlGetLength( p_demux );
365 else if( i_query == DEMUX_SET_TIME )
367 int64_t i_time = (int64_t)va_arg( args, int64_t );
368 return ControlSetTime( p_demux, i_time );
370 else if( i_query == DEMUX_SET_POSITION )
372 const double f = (double)va_arg( args, double );
373 int64_t i_time = f * ControlGetLength( p_demux );
374 return ControlSetTime( p_demux, i_time );
376 else if( i_query == DEMUX_GET_TIME )
378 int64_t *pi64 = (int64_t*)va_arg( args, int64_t * );
379 *pi64 = ControlGetTime( p_demux );
382 else if( i_query == DEMUX_GET_POSITION )
384 double *pf = (double*)va_arg( args, double * );
385 const int64_t i_length = ControlGetLength(p_demux);
387 *pf = (double)ControlGetTime(p_demux) / (double)i_length;
392 else if( i_query == DEMUX_GET_ATTACHMENTS )
394 input_attachment_t ***ppp_attach =
395 (input_attachment_t***)va_arg( args, input_attachment_t*** );
396 int *pi_int = (int*)va_arg( args, int * );
399 if( p_sys->i_attachments <= 0 )
402 *pi_int = p_sys->i_attachments;;
403 *ppp_attach = malloc( sizeof(input_attachment_t**) * p_sys->i_attachments );
404 for( i = 0; i < p_sys->i_attachments; i++ )
405 (*ppp_attach)[i] = vlc_input_attachment_Duplicate( p_sys->attachments[i] );
409 return demux_vaControlHelper( p_demux->s, p_sys->i_data_pos, -1,
410 8*0, 1, i_query, args );
421 static inline int Get24bBE( const uint8_t *p )
423 return (p[0] << 16)|(p[1] << 8)|(p[2]);
426 static void ParseStreamInfo( int *pi_rate, int64_t *pi_count, uint8_t *p_data );
427 static void ParseSeekTable( demux_t *p_demux, const uint8_t *p_data, int i_data,
429 static void ParseComment( demux_t *, const uint8_t *p_data, int i_data );
430 static void ParsePicture( demux_t *, const uint8_t *p_data, int i_data );
432 static int ReadMeta( demux_t *p_demux, uint8_t **pp_streaminfo, int *pi_streaminfo )
434 demux_sys_t *p_sys = p_demux->p_sys;
436 const uint8_t *p_peek;
439 int64_t i_sample_count;
442 /* Read STREAMINFO */
443 i_peek = stream_Peek( p_demux->s, &p_peek, 8 );
444 if( (p_peek[4] & 0x7F) != META_STREAMINFO )
446 msg_Err( p_demux, "this isn't a STREAMINFO metadata block" );
449 if( Get24bBE(&p_peek[5]) != (STREAMINFO_SIZE - 4) )
451 msg_Err( p_demux, "invalid size for a STREAMINFO metadata block" );
455 *pi_streaminfo = 4 + STREAMINFO_SIZE;
456 *pp_streaminfo = malloc( 4 + STREAMINFO_SIZE );
457 if( *pp_streaminfo == NULL )
460 if( stream_Read( p_demux->s, *pp_streaminfo, 4+STREAMINFO_SIZE ) != 4+STREAMINFO_SIZE )
462 msg_Err( p_demux, "failed to read STREAMINFO metadata block" );
463 free( *pp_streaminfo );
468 ParseStreamInfo( &i_sample_rate, &i_sample_count, *pp_streaminfo );
469 if( i_sample_rate > 0 )
470 p_sys->i_length = i_sample_count * INT64_C(1000000)/i_sample_rate;
472 /* Be sure we have seekpoint 0 */
473 s = vlc_seekpoint_New();
474 s->i_time_offset = 0;
475 s->i_byte_offset = 0;
476 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( int *pi_rate, int64_t *pi_count, uint8_t *p_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]) & ((INT64_C(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 * INT64_C(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( (const char*)p_data, n );
620 #define IF_EXTRACT(txt,var) \
621 if( !strncasecmp(psz, txt, strlen(txt)) ) \
623 const char *oldval = vlc_meta_Get( p_sys->p_meta, vlc_meta_ ## var ); \
627 if( asprintf( &newval, "%s,%s", oldval, &psz[strlen(txt)] ) == -1 ) \
629 vlc_meta_Set( p_sys->p_meta, vlc_meta_ ## var, newval ); \
633 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 char *p = strchr( psz, '=' );
649 vlc_meta_AddExtra( p_sys->p_meta, psz, p );
657 static void ParsePicture( demux_t *p_demux, const uint8_t *p_data, int i_data )
659 static const int pi_cover_score[] = {
662 10, /* front cover */
664 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
665 6, /* movie/video screen capture */
667 7, /* Illustration */
668 8, /* Band/Artist logotype */
669 0, /* Publisher/Studio */
671 demux_sys_t *p_sys = p_demux->p_sys;
674 char *psz_mime = NULL;
675 char *psz_description = NULL;
676 input_attachment_t *p_attachment;
679 if( i_data < 4 + 3*4 )
681 #define RM(x) do { i_data -= (x); p_data += (x); } while(0)
684 i_type = GetDWBE( p_data ); RM(4);
685 i_len = GetDWBE( p_data ); RM(4);
686 if( i_len < 0 || i_data < i_len + 4 )
688 psz_mime = strndup( (const char*)p_data, i_len ); RM(i_len);
689 i_len = GetDWBE( p_data ); RM(4);
690 if( i_len < 0 || i_data < i_len + 4*4 + 4)
692 psz_description = strndup( (const char*)p_data, i_len ); RM(i_len);
693 EnsureUTF8( psz_description );
695 i_len = GetDWBE( p_data ); RM(4);
696 if( i_len < 0 || i_len > i_data )
699 msg_Dbg( p_demux, "FLAC: Picture type=%d mime=%s description='%s' file length=%d",
700 i_type, psz_mime, psz_description, i_len );
702 snprintf( psz_name, sizeof(psz_name), "picture%d", p_sys->i_attachments );
703 if( !strcasecmp( psz_mime, "image/jpeg" ) )
704 strcat( psz_name, ".jpg" );
705 else if( !strcasecmp( psz_mime, "image/png" ) )
706 strcat( psz_name, ".png" );
708 p_attachment = vlc_input_attachment_New( psz_name, psz_mime, psz_description,
710 TAB_APPEND( p_sys->i_attachments, p_sys->attachments, p_attachment );
712 if( i_type >= 0 && (unsigned int)i_type < sizeof(pi_cover_score)/sizeof(pi_cover_score[0]) &&
713 p_sys->i_cover_score < pi_cover_score[i_type] )
715 p_sys->i_cover_idx = p_sys->i_attachments-1;
716 p_sys->i_cover_score = pi_cover_score[i_type];
720 free( psz_description );