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;
107 /* Have a peep at the show. */
108 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 ) return VLC_EGENERIC;
110 if( p_peek[0]!='f' || p_peek[1]!='L' || p_peek[2]!='a' || p_peek[3]!='C' )
112 if( !p_demux->b_force ) return VLC_EGENERIC;
115 msg_Err( p_demux, "this doesn't look like a flac stream, "
116 "continuing anyway" );
119 p_demux->pf_demux = Demux;
120 p_demux->pf_control = Control;
121 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
122 p_sys->b_start = true;
123 p_sys->p_meta = NULL;
124 memset( &p_sys->replay_gain, 0, sizeof(p_sys->replay_gain) );
126 p_sys->i_time_offset = 0;
128 p_sys->i_pts_start = 0;
130 TAB_INIT( p_sys->i_seekpoint, p_sys->seekpoint );
131 TAB_INIT( p_sys->i_attachments, p_sys->attachments);
132 p_sys->i_cover_idx = 0;
133 p_sys->i_cover_score = 0;
135 /* We need to read and store the STREAMINFO metadata */
136 if( ReadMeta( p_demux, &p_streaminfo, &i_streaminfo ) )
142 /* Load the FLAC packetizer */
143 INIT_APACKETIZER( p_sys->p_packetizer, 'f', 'l', 'a', 'c' );
145 /* Store STREAMINFO for the decoder and packetizer */
146 p_streaminfo[4] |= 0x80; /* Fake this as the last metadata block */
147 p_sys->p_packetizer->fmt_in.i_extra = i_streaminfo;
148 p_sys->p_packetizer->fmt_in.p_extra = p_streaminfo;
150 p_sys->p_packetizer->p_module =
151 module_Need( p_sys->p_packetizer, "packetizer", NULL, 0 );
152 if( !p_sys->p_packetizer->p_module )
154 free( p_sys->p_packetizer->fmt_in.p_extra );
155 vlc_object_release( 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 free( p_sys->p_packetizer->fmt_in.p_extra );
194 /* Delete the decoder */
195 vlc_object_release( p_sys->p_packetizer );
197 vlc_meta_Delete( p_sys->p_meta );
201 /*****************************************************************************
202 * Demux: reads and demuxes data packets
203 *****************************************************************************
204 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
205 *****************************************************************************/
206 static int Demux( demux_t *p_demux )
208 demux_sys_t *p_sys = p_demux->p_sys;
209 block_t *p_block_in, *p_block_out;
211 if( !( p_block_in = stream_Block( p_demux->s, FLAC_PACKET_SIZE ) ) )
214 p_block_in->i_pts = p_block_in->i_dts = p_sys->b_start ? 1 : 0;
215 p_sys->b_start = false;
217 while( (p_block_out = p_sys->p_packetizer->pf_packetize(
218 p_sys->p_packetizer, &p_block_in )) )
222 block_t *p_next = p_block_out->p_next;
224 p_block_out->p_next = NULL;
226 if( p_sys->p_es == NULL )
228 p_sys->p_packetizer->fmt_out.b_packetized = true;
229 p_sys->p_packetizer->fmt_out.audio_replay_gain = p_sys->replay_gain;
230 p_sys->p_es = es_out_Add( p_demux->out, &p_sys->p_packetizer->fmt_out);
234 if( p_block_out->i_dts >= p_sys->i_pts_start )
235 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block_out->i_dts );
237 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
239 p_sys->i_pts = p_block_out->i_dts;
240 es_out_Send( p_demux->out, p_sys->p_es, p_block_out );
242 p_block_out = p_next;
248 /*****************************************************************************
250 *****************************************************************************/
251 static int64_t ControlGetLength( demux_t *p_demux )
253 demux_sys_t *p_sys = p_demux->p_sys;
254 const int64_t i_size = stream_Size(p_demux->s) - p_sys->i_data_pos;
255 int64_t i_length = p_sys->i_length;
258 /* Try to fix length using seekpoint and current size for truncated file */
259 for( i = p_sys->i_seekpoint-1; i >= 0; i-- )
261 seekpoint_t *s = p_sys->seekpoint[i];
262 if( s->i_byte_offset <= i_size )
264 if( i+1 < p_sys->i_seekpoint )
267 seekpoint_t *n = p_sys->seekpoint[i+1];
268 assert( n->i_byte_offset != s->i_byte_offset); /* Should be ensured by ParseSeekTable */
269 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);
277 static int64_t ControlGetTime( demux_t *p_demux )
279 demux_sys_t *p_sys = p_demux->p_sys;
280 return __MAX(p_sys->i_pts, p_sys->i_pts_start) + p_sys->i_time_offset;
283 static int ControlSetTime( demux_t *p_demux, int64_t i_time )
285 demux_sys_t *p_sys = p_demux->p_sys;
286 int64_t i_delta_time;
291 stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_seekable );
296 assert( p_sys->i_seekpoint > 0 ); /* ReadMeta ensure at least (0,0) */
297 for( i = p_sys->i_seekpoint-1; i >= 0; i-- )
299 if( p_sys->seekpoint[i]->i_time_offset <= i_time )
302 i_delta_time = i_time - p_sys->seekpoint[i]->i_time_offset;
304 /* XXX We do exact seek if it's not too far away(45s) */
305 if( i_delta_time < 45*INT64_C(1000000) )
307 if( stream_Seek( p_demux->s, p_sys->seekpoint[i]->i_byte_offset+p_sys->i_data_pos ) )
310 p_sys->i_time_offset = p_sys->seekpoint[i]->i_time_offset - p_sys->i_pts;
311 p_sys->i_pts_start = p_sys->i_pts+i_delta_time;
312 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, p_sys->p_es, p_sys->i_pts_start );
316 int64_t i_delta_offset;
318 int64_t i_next_offset;
320 if( i+1 < p_sys->i_seekpoint )
322 i_next_time = p_sys->seekpoint[i+1]->i_time_offset;
323 i_next_offset = p_sys->seekpoint[i+1]->i_byte_offset;
327 i_next_time = p_sys->i_length;
328 i_next_offset = stream_Size(p_demux->s)-p_sys->i_data_pos;
332 if( i_next_time-p_sys->seekpoint[i]->i_time_offset > 0 )
333 i_delta_offset = (i_next_offset - p_sys->seekpoint[i]->i_byte_offset) * i_delta_time /
334 (i_next_time-p_sys->seekpoint[i]->i_time_offset);
336 if( stream_Seek( p_demux->s, p_sys->seekpoint[i]->i_byte_offset+p_sys->i_data_pos + i_delta_offset ) )
339 p_sys->i_pts_start = p_sys->i_pts;
340 p_sys->i_time_offset = (p_sys->seekpoint[i]->i_time_offset+i_delta_time) - p_sys->i_pts;
345 static int Control( demux_t *p_demux, int i_query, va_list args )
347 demux_sys_t *p_sys = p_demux->p_sys;
349 if( i_query == DEMUX_GET_META )
351 vlc_meta_t *p_meta = (vlc_meta_t *)va_arg( args, vlc_meta_t* );
352 if( p_demux->p_sys->p_meta )
353 vlc_meta_Merge( p_meta, p_demux->p_sys->p_meta );
356 else if( i_query == DEMUX_HAS_UNSUPPORTED_META )
358 bool *pb_bool = (bool*)va_arg( args, bool* );
362 else if( i_query == DEMUX_GET_LENGTH )
364 int64_t *pi64 = (int64_t*)va_arg( args, int64_t * );
365 *pi64 = ControlGetLength( p_demux );
368 else if( i_query == DEMUX_SET_TIME )
370 int64_t i_time = (int64_t)va_arg( args, int64_t );
371 return ControlSetTime( p_demux, i_time );
373 else if( i_query == DEMUX_SET_POSITION )
375 const double f = (double)va_arg( args, double );
376 int64_t i_time = f * ControlGetLength( p_demux );
377 return ControlSetTime( p_demux, i_time );
379 else if( i_query == DEMUX_GET_TIME )
381 int64_t *pi64 = (int64_t*)va_arg( args, int64_t * );
382 *pi64 = ControlGetTime( p_demux );
385 else if( i_query == DEMUX_GET_POSITION )
387 double *pf = (double*)va_arg( args, double * );
388 const int64_t i_length = ControlGetLength(p_demux);
390 *pf = (double)ControlGetTime(p_demux) / (double)i_length;
395 else if( i_query == DEMUX_GET_ATTACHMENTS )
397 input_attachment_t ***ppp_attach =
398 (input_attachment_t***)va_arg( args, input_attachment_t*** );
399 int *pi_int = (int*)va_arg( args, int * );
402 if( p_sys->i_attachments <= 0 )
405 *pi_int = p_sys->i_attachments;;
406 *ppp_attach = malloc( sizeof(input_attachment_t**) * p_sys->i_attachments );
407 for( i = 0; i < p_sys->i_attachments; i++ )
408 (*ppp_attach)[i] = vlc_input_attachment_Duplicate( p_sys->attachments[i] );
412 return demux_vaControlHelper( p_demux->s, p_sys->i_data_pos, -1,
413 8*0, 1, i_query, args );
424 static inline int Get24bBE( const uint8_t *p )
426 return (p[0] << 16)|(p[1] << 8)|(p[2]);
429 static void ParseStreamInfo( demux_t *p_demux, int *pi_rate, int64_t *pi_count, uint8_t *p_data, int i_data );
430 static void ParseSeekTable( demux_t *p_demux, const uint8_t *p_data, int i_data,
432 static void ParseComment( demux_t *, const uint8_t *p_data, int i_data );
433 static void ParsePicture( demux_t *, const uint8_t *p_data, int i_data );
435 static int ReadMeta( demux_t *p_demux, uint8_t **pp_streaminfo, int *pi_streaminfo )
437 demux_sys_t *p_sys = p_demux->p_sys;
439 const uint8_t *p_peek;
442 int64_t i_sample_count;
445 /* Read STREAMINFO */
446 i_peek = stream_Peek( p_demux->s, &p_peek, 8 );
447 if( (p_peek[4] & 0x7F) != META_STREAMINFO )
449 msg_Err( p_demux, "this isn't a STREAMINFO metadata block" );
452 if( Get24bBE(&p_peek[5]) != (STREAMINFO_SIZE - 4) )
454 msg_Err( p_demux, "invalid size for a STREAMINFO metadata block" );
458 *pi_streaminfo = 4 + STREAMINFO_SIZE;
459 *pp_streaminfo = malloc( 4 + STREAMINFO_SIZE );
460 if( *pp_streaminfo == NULL )
463 if( stream_Read( p_demux->s, *pp_streaminfo, 4+STREAMINFO_SIZE ) != 4+STREAMINFO_SIZE )
465 msg_Err( p_demux, "failed to read STREAMINFO metadata block" );
466 free( *pp_streaminfo );
471 ParseStreamInfo( p_demux, &i_sample_rate, &i_sample_count, *pp_streaminfo, *pi_streaminfo );
472 if( i_sample_rate > 0 )
473 p_sys->i_length = i_sample_count * INT64_C(1000000)/i_sample_rate;
475 /* Be sure we have seekpoint 0 */
476 s = vlc_seekpoint_New();
477 s->i_time_offset = 0;
478 s->i_byte_offset = 0;
479 TAB_APPEND( p_sys->i_seekpoint, p_sys->seekpoint, s );
481 b_last = (*pp_streaminfo)[4]&0x80;
487 i_peek = stream_Peek( p_demux->s, &p_peek, 4 );
490 b_last = p_peek[0]&0x80;
491 i_type = p_peek[0]&0x7f;
492 i_len = Get24bBE( &p_peek[1] );
494 if( i_type == META_SEEKTABLE )
496 i_peek = stream_Peek( p_demux->s, &p_peek, 4+i_len );
497 if( i_peek == 4+i_len )
498 ParseSeekTable( p_demux, p_peek, i_peek, i_sample_rate );
500 else if( i_type == META_COMMENT )
502 i_peek = stream_Peek( p_demux->s, &p_peek, 4+i_len );
503 if( i_peek == 4+i_len )
504 ParseComment( p_demux, p_peek, i_peek );
506 else if( i_type == META_PICTURE )
508 i_peek = stream_Peek( p_demux->s, &p_peek, 4+i_len );
509 if( i_peek == 4+i_len )
510 ParsePicture( p_demux, p_peek, i_peek );
513 if( stream_Read( p_demux->s, NULL, 4+i_len ) < 4+i_len )
518 p_sys->i_data_pos = stream_Tell( p_demux->s );
522 static void ParseStreamInfo( demux_t *p_demux, int *pi_rate, int64_t *pi_count, uint8_t *p_data, int i_data )
524 const int i_skip = 4+4;
526 *pi_rate = GetDWBE(&p_data[i_skip+4+6]) >> 12;
527 *pi_count = GetQWBE(&p_data[i_skip+4+6]) & ((INT64_C(1)<<36)-1);
530 static void ParseSeekTable( demux_t *p_demux, const uint8_t *p_data, int i_data,
533 demux_sys_t *p_sys = p_demux->p_sys;
537 if( i_sample_rate <= 0 )
541 for( i = 0; i < (i_data-4)/18; i++ )
543 const int64_t i_sample = GetQWBE( &p_data[4+18*i+0] );
546 if( i_sample < 0 || i_sample >= INT64_MAX )
549 s = vlc_seekpoint_New();
550 s->i_time_offset = i_sample * INT64_C(1000000)/i_sample_rate;
551 s->i_byte_offset = GetQWBE( &p_data[4+18*i+8] );
553 /* Check for duplicate entry */
554 for( j = 0; j < p_sys->i_seekpoint; j++ )
556 if( p_sys->seekpoint[j]->i_time_offset == s->i_time_offset ||
557 p_sys->seekpoint[j]->i_byte_offset == s->i_byte_offset )
559 vlc_seekpoint_Delete( s );
566 TAB_APPEND( p_sys->i_seekpoint, p_sys->seekpoint, s );
569 /* TODO sort it by size and remove wrong seek entry (time not increasing) */
572 #define RM(x) do { i_data -= (x); p_data += (x); } while(0)
573 static void ParseComment( demux_t *p_demux, const uint8_t *p_data, int i_data )
575 demux_sys_t *p_sys = p_demux->p_sys;
584 n = GetDWLE(p_data); RM(4);
585 if( n < 0 || n > i_data )
590 /* TODO report vendor string ? */
591 char *psz_vendor = psz_vendor = strndup( p_data, n );
592 msg_Dbg( p_demux, "FLAC: COMMENT vendor length=%d vendor=%s\n", n, psz_vendor );
601 i_comment = GetDWLE(p_data); RM(4);
605 p_sys->p_meta = vlc_meta_New();
607 for( ; i_comment > 0; i_comment-- )
612 n = GetDWLE(p_data); RM(4);
618 psz = strndup( p_data, n );
623 #define IF_EXTRACT(txt,var) \
624 if( !strncasecmp(psz, txt, strlen(txt)) ) \
626 const char *oldval = vlc_meta_Get( p_sys->p_meta, vlc_meta_ ## var ); \
630 if( asprintf( &newval, "%s,%s", oldval, &psz[strlen(txt)] ) == -1 ) \
632 vlc_meta_Set( p_sys->p_meta, vlc_meta_ ## var, newval ); \
636 vlc_meta_Set( p_sys->p_meta, vlc_meta_ ## var, &psz[strlen(txt)] ); \
638 IF_EXTRACT("TITLE=", Title )
639 else IF_EXTRACT("ALBUM=", Album )
640 else IF_EXTRACT("TRACKNUMBER=", TrackNumber )
641 else IF_EXTRACT("ARTIST=", Artist )
642 else IF_EXTRACT("COPYRIGHT=", Copyright )
643 else IF_EXTRACT("DESCRIPTION=", Description )
644 else IF_EXTRACT("GENRE=", Genre )
645 else IF_EXTRACT("DATE=", Date )
646 else if( strchr( psz, '=' ) )
648 /* generic (PERFORMER/LICENSE/ORGANIZATION/LOCATION/CONTACT/ISRC,
649 * undocumented tags and replay gain ) */
650 char *p = strchr( psz, '=' );
652 vlc_meta_AddExtra( p_sys->p_meta, psz, p );
660 static void ParsePicture( demux_t *p_demux, const uint8_t *p_data, int i_data )
662 static const int pi_cover_score[] = {
665 10, /* front cover */
667 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
668 6, /* movie/video screen capture */
670 7, /* Illustration */
671 8, /* Band/Artist logotype */
672 0, /* Publisher/Studio */
674 demux_sys_t *p_sys = p_demux->p_sys;
677 char *psz_mime = NULL;
678 char *psz_description = NULL;
679 input_attachment_t *p_attachment;
682 if( i_data < 4 + 3*4 )
684 #define RM(x) do { i_data -= (x); p_data += (x); } while(0)
687 i_type = GetDWBE( p_data ); RM(4);
688 i_len = GetDWBE( p_data ); RM(4);
689 if( i_len < 0 || i_data < i_len + 4 )
691 psz_mime = strndup( p_data, i_len ); RM(i_len);
692 i_len = GetDWBE( p_data ); RM(4);
693 if( i_len < 0 || i_data < i_len + 4*4 + 4)
695 psz_description = strndup( p_data, i_len ); RM(i_len);
696 EnsureUTF8( psz_description );
698 i_len = GetDWBE( p_data ); RM(4);
699 if( i_len < 0 || i_len > i_data )
702 msg_Dbg( p_demux, "FLAC: Picture type=%d mime=%s description='%s' file length=%d",
703 i_type, psz_mime, psz_description, i_len );
705 snprintf( psz_name, sizeof(psz_name), "picture%d", p_sys->i_attachments );
706 if( !strcasecmp( psz_mime, "image/jpeg" ) )
707 strcat( psz_name, ".jpg" );
708 else if( !strcasecmp( psz_mime, "image/png" ) )
709 strcat( psz_name, ".png" );
711 p_attachment = vlc_input_attachment_New( psz_name, psz_mime, psz_description,
713 TAB_APPEND( p_sys->i_attachments, p_sys->attachments, p_attachment );
715 if( i_type >= 0 && i_type < sizeof(pi_cover_score)/sizeof(pi_cover_score[0]) &&
716 p_sys->i_cover_score < pi_cover_score[i_type] )
718 p_sys->i_cover_idx = p_sys->i_attachments-1;
719 p_sys->i_cover_score = pi_cover_score[i_type];
723 free( psz_description );