]> git.sesse.net Git - vlc/blob - modules/demux/flac.c
Demuxers: do not load "meta reader" module if the input item has already been preparsed.
[vlc] / modules / demux / flac.c
1 /*****************************************************************************
2  * flac.c : FLAC demux module for vlc
3  *****************************************************************************
4  * Copyright (C) 2001-2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@netcourrier.com>
8  *          Laurent Aimar <fenrir@via.ecp.fr>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <vlc/vlc.h>
29 #include <vlc_demux.h>
30 #include <vlc_meta.h>
31 #include <vlc_input.h>
32 #include <vlc_codec.h>
33 #include <assert.h>
34 #include <vlc_charset.h>
35
36 /*****************************************************************************
37  * Module descriptor
38  *****************************************************************************/
39 static int  Open  ( vlc_object_t * );
40 static void Close ( vlc_object_t * );
41
42 vlc_module_begin();
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" );
49 vlc_module_end();
50
51 /*****************************************************************************
52  * Local prototypes
53  *****************************************************************************/
54 static int Demux  ( demux_t * );
55 static int Control( demux_t *, int, va_list );
56
57 static int  ReadMeta( demux_t *, uint8_t **pp_streaminfo, int *pi_streaminfo );
58
59 struct demux_sys_t
60 {
61     vlc_bool_t  b_start;
62     es_out_id_t *p_es;
63
64     /* Packetizer */
65     decoder_t *p_packetizer;
66     vlc_meta_t *p_meta;
67     audio_replay_gain_t replay_gain;
68
69     int64_t i_time_offset;
70     int64_t i_pts;
71     int64_t i_pts_start;
72
73     int64_t i_length; /* Length from stream info */
74     int64_t i_data_pos;
75
76     /* */
77     int         i_seekpoint;
78     seekpoint_t **seekpoint;
79
80     /* */
81     int                i_attachments;
82     input_attachment_t **attachments;
83     int                i_cover_idx;
84     int                i_cover_score;
85 };
86
87 #define STREAMINFO_SIZE 38
88 #define FLAC_PACKET_SIZE 16384
89
90 /*****************************************************************************
91  * Open: initializes ES structures
92  *****************************************************************************/
93 static int Open( vlc_object_t * p_this )
94 {
95     demux_t     *p_demux = (demux_t*)p_this;
96     module_t    *p_id3;
97     demux_sys_t *p_sys;
98     const byte_t *p_peek;
99     uint8_t     *p_streaminfo;
100     int         i_streaminfo;
101
102     /* Have a peep at the show. */
103     if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 ) return VLC_EGENERIC;
104
105     if( p_peek[0]!='f' || p_peek[1]!='L' || p_peek[2]!='a' || p_peek[3]!='C' )
106     {
107         if( !p_demux->b_force ) return VLC_EGENERIC;
108
109         /* User forced */
110         msg_Err( p_demux, "this doesn't look like a flac stream, "
111                  "continuing anyway" );
112     }
113
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) );
120     p_sys->i_length = 0;
121     p_sys->i_time_offset = 0;
122     p_sys->i_pts = 0;
123     p_sys->i_pts_start = 0;
124     p_sys->p_es = NULL;
125     TAB_INIT( p_sys->i_seekpoint, p_sys->seekpoint );
126     TAB_INIT( p_sys->i_attachments, p_sys->attachments);
127     p_sys->i_cover_idx = 0;
128     p_sys->i_cover_score = 0;
129
130     /* We need to read and store the STREAMINFO metadata */
131     if( ReadMeta( p_demux, &p_streaminfo, &i_streaminfo ) )
132     {
133         free( p_sys );
134         return VLC_EGENERIC;
135     }
136
137     /* Load the FLAC packetizer */
138     INIT_APACKETIZER( p_sys->p_packetizer, 'f', 'l', 'a', 'c' );
139
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;
144
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 )
148     {
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 );
152
153         msg_Err( p_demux, "cannot find flac packetizer" );
154         return VLC_EGENERIC;
155     }
156
157     /* Parse possible id3 header */
158     input_thread_t *p_input = (input_thread_t *)vlc_object_find( p_demux, VLC_OBJECT_INPUT, FIND_PARENT );
159     if( p_input )
160     {
161         if( !( input_GetItem( p_input )->p_meta->i_status & ITEM_PREPARSED ) )
162         {
163             p_demux->p_private = malloc( sizeof( demux_meta_t ) );
164             if( !p_demux->p_private )
165             {
166                 vlc_object_release( p_input );
167                 return VLC_ENOMEM;
168             }
169             if( ( p_id3 = module_Need( p_demux, "meta reader", NULL, 0 ) ) )
170             {
171                 demux_meta_t *p_demux_meta = (demux_meta_t *)p_demux->p_private;
172                 vlc_meta_t *p_meta = p_demux_meta->p_meta;
173
174                 if( !p_sys->p_meta )
175                 {
176                     p_sys->p_meta = p_meta;
177                 }
178                 else if( p_meta )
179                 {
180                     vlc_meta_Merge( p_sys->p_meta, p_meta );
181                     vlc_meta_Delete( p_meta );
182                 }
183                 p_demux->p_private = NULL;
184                 module_Unneed( p_demux, p_id3 );
185                 int i;
186                 for( i = 0; i < p_demux_meta->i_attachments; i++ )
187                     TAB_APPEND_CAST( (input_attachment_t**),
188                             p_sys->i_attachments, p_sys->attachments,
189                             p_demux_meta->attachments[p_demux_meta->i_attachments] );
190                 TAB_CLEAN( p_demux_meta->i_attachments,
191                             p_demux_meta->attachments );
192             }
193             free( p_demux->p_private );
194         }
195         vlc_object_release( p_input );
196     }
197
198     if( p_sys->i_cover_idx < p_sys->i_attachments )
199     {
200         char psz_url[128];
201         if( !p_sys->p_meta )
202             p_sys->p_meta = vlc_meta_New();
203         snprintf( psz_url, sizeof(psz_url), "attachment://%s",
204                   p_sys->attachments[p_sys->i_cover_idx]->psz_name );
205         vlc_meta_Set( p_sys->p_meta, vlc_meta_ArtworkURL, psz_url );
206     }
207     vlc_audio_replay_gain_MergeFromMeta( &p_sys->replay_gain, p_sys->p_meta );
208     return VLC_SUCCESS;
209 }
210
211 /*****************************************************************************
212  * Close: frees unused data
213  *****************************************************************************/
214 static void Close( vlc_object_t * p_this )
215 {
216     demux_t     *p_demux = (demux_t*)p_this;
217     demux_sys_t *p_sys = p_demux->p_sys;
218
219     TAB_CLEAN( p_sys->i_seekpoint, p_sys->seekpoint );
220
221     int i;
222     for( i = 0; i < p_sys->i_attachments; i++ )
223         free( p_sys->attachments[i] );
224     TAB_CLEAN( p_sys->i_attachments, p_sys->attachments);
225
226     /* Unneed module */
227     module_Unneed( p_sys->p_packetizer, p_sys->p_packetizer->p_module );
228
229     if( p_sys->p_packetizer->fmt_in.p_extra )
230         free( p_sys->p_packetizer->fmt_in.p_extra );
231
232     /* Delete the decoder */
233     vlc_object_destroy( p_sys->p_packetizer );
234     if( p_sys->p_meta )
235         vlc_meta_Delete( p_sys->p_meta );
236     free( p_sys );
237 }
238
239 /*****************************************************************************
240  * Demux: reads and demuxes data packets
241  *****************************************************************************
242  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
243  *****************************************************************************/
244 static int Demux( demux_t *p_demux )
245 {
246     demux_sys_t *p_sys = p_demux->p_sys;
247     block_t     *p_block_in, *p_block_out;
248
249     if( !( p_block_in = stream_Block( p_demux->s, FLAC_PACKET_SIZE ) ) )
250         return 0;
251
252     p_block_in->i_pts = p_block_in->i_dts = p_sys->b_start ? 1 : 0;
253     p_sys->b_start = VLC_FALSE;
254
255     while( (p_block_out = p_sys->p_packetizer->pf_packetize(
256                 p_sys->p_packetizer, &p_block_in )) )
257     {
258         while( p_block_out )
259         {
260             block_t *p_next = p_block_out->p_next;
261
262             p_block_out->p_next = NULL;
263
264             if( p_sys->p_es == NULL )
265             {
266                 p_sys->p_packetizer->fmt_out.b_packetized = VLC_TRUE;
267                 p_sys->p_packetizer->fmt_out.audio_replay_gain = p_sys->replay_gain;
268                 p_sys->p_es = es_out_Add( p_demux->out, &p_sys->p_packetizer->fmt_out);
269             }
270
271             /* set PCR */
272             if( p_block_out->i_dts >= p_sys->i_pts_start )
273                 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block_out->i_dts );
274             else
275                 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
276
277             p_sys->i_pts = p_block_out->i_dts;
278             es_out_Send( p_demux->out, p_sys->p_es, p_block_out );
279
280             p_block_out = p_next;
281         }
282     }
283     return 1;
284 }
285
286 /*****************************************************************************
287  * Control:
288  *****************************************************************************/
289 static int64_t ControlGetLength( demux_t *p_demux )
290 {
291     demux_sys_t *p_sys = p_demux->p_sys;
292     const int64_t i_size = stream_Size(p_demux->s) - p_sys->i_data_pos;
293     int64_t i_length = p_sys->i_length;
294     int i;
295
296     /* Try to fix length using seekpoint and current size for truncated file */
297     for( i = p_sys->i_seekpoint-1; i >= 0; i-- )
298     {
299         seekpoint_t *s = p_sys->seekpoint[i];
300         if( s->i_byte_offset <= i_size )
301         {
302             if( i+1 < p_sys->i_seekpoint )
303             {
304                 /* Broken file */
305                 seekpoint_t *n = p_sys->seekpoint[i+1];
306                 assert( n->i_byte_offset != s->i_byte_offset); /* Should be ensured by ParseSeekTable */
307                 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);
308             }
309             break;
310         }
311     }
312     return i_length;
313 }
314
315 static int64_t ControlGetTime( demux_t *p_demux )
316 {
317     demux_sys_t *p_sys = p_demux->p_sys;
318     return __MAX(p_sys->i_pts, p_sys->i_pts_start) + p_sys->i_time_offset;
319 }
320
321 static int ControlSetTime( demux_t *p_demux, int64_t i_time )
322 {
323     demux_sys_t *p_sys = p_demux->p_sys;
324     int64_t i_next_time;
325     int64_t i_next_offset;
326     int64_t i_delta_time;
327     int64_t i_delta_offset;
328     vlc_bool_t b_seekable;
329     int i;
330
331     /* */
332     stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_seekable );
333     if( !b_seekable )
334         return VLC_EGENERIC;
335
336     /* */
337     assert( p_sys->i_seekpoint > 0 );   /* ReadMeta ensure at least (0,0) */
338     for( i = p_sys->i_seekpoint-1; i >= 0; i-- )
339     {
340         if( p_sys->seekpoint[i]->i_time_offset <= i_time )
341             break;
342     }
343     if( i+1 < p_sys->i_seekpoint )
344     {
345         i_next_time   = p_sys->seekpoint[i+1]->i_time_offset;
346         i_next_offset = p_sys->seekpoint[i+1]->i_byte_offset;
347     }
348     else
349     {
350         i_next_time   = p_sys->i_length;
351         i_next_offset = stream_Size(p_demux->s)-p_sys->i_data_pos;
352     }
353     i_delta_time = i_time - p_sys->seekpoint[i]->i_time_offset;
354     i_delta_offset = (i_next_offset - p_sys->seekpoint[i]->i_byte_offset) * i_delta_time /
355                             (p_sys->seekpoint[i+1]->i_time_offset-p_sys->seekpoint[i]->i_time_offset);
356
357     /* XXX We do exact seek if it's not too far away(45s) */
358     if( i_delta_time < 45*I64C(1000000) )
359     {
360         if( stream_Seek( p_demux->s, p_sys->seekpoint[i]->i_byte_offset+p_sys->i_data_pos ) )
361             return VLC_EGENERIC;
362         p_sys->i_time_offset = p_sys->seekpoint[i]->i_time_offset - p_sys->i_pts;
363         p_sys->i_pts_start = p_sys->i_pts+i_delta_time;
364         es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, p_sys->p_es, p_sys->i_pts_start );
365     }
366     else
367     {
368         if( stream_Seek( p_demux->s, p_sys->seekpoint[i]->i_byte_offset+p_sys->i_data_pos + i_delta_offset ) )
369             return VLC_EGENERIC;
370         p_sys->i_pts_start = p_sys->i_pts;
371         p_sys->i_time_offset = (p_sys->seekpoint[i]->i_time_offset+i_delta_time) - p_sys->i_pts;
372     }
373     return VLC_SUCCESS;
374 }
375
376 static int Control( demux_t *p_demux, int i_query, va_list args )
377 {
378     demux_sys_t *p_sys = p_demux->p_sys;
379
380     if( i_query == DEMUX_GET_META )
381     {
382         vlc_meta_t *p_meta = (vlc_meta_t *)va_arg( args, vlc_meta_t* );
383         if( p_demux->p_sys->p_meta )
384             vlc_meta_Merge( p_meta, p_demux->p_sys->p_meta );
385         return VLC_SUCCESS;
386     }
387     else if( i_query == DEMUX_GET_LENGTH )
388     {
389         int64_t *pi64 = (int64_t*)va_arg( args, int64_t * );
390         *pi64 = ControlGetLength( p_demux );
391         return VLC_SUCCESS;
392     }
393     else if( i_query == DEMUX_SET_TIME )
394     {
395         int64_t i_time = (int64_t)va_arg( args, int64_t );
396         return ControlSetTime( p_demux, i_time );
397     }
398     else if( i_query == DEMUX_SET_POSITION )
399     {
400         const double f = (double)va_arg( args, double );
401         int64_t i_time = f * ControlGetLength( p_demux );
402         return ControlSetTime( p_demux, i_time );
403     }
404     else if( i_query == DEMUX_GET_TIME )
405     {
406         int64_t *pi64 = (int64_t*)va_arg( args, int64_t * );
407         *pi64 = ControlGetTime( p_demux );
408         return VLC_SUCCESS;
409     }
410     else if( i_query == DEMUX_GET_POSITION )
411     {
412         double *pf = (double*)va_arg( args, double * );
413         const int64_t i_length = ControlGetLength(p_demux);
414         if( i_length > 0 )
415             *pf = (double)ControlGetTime(p_demux) / (double)i_length;
416         else
417             *pf= 0.0;
418         return VLC_SUCCESS;
419     }
420     else if( i_query == DEMUX_GET_ATTACHMENTS )
421     {
422         input_attachment_t ***ppp_attach =
423             (input_attachment_t***)va_arg( args, input_attachment_t*** );
424         int *pi_int = (int*)va_arg( args, int * );
425         int i;
426
427         if( p_sys->i_attachments <= 0 )
428             return VLC_EGENERIC;
429
430         *pi_int = p_sys->i_attachments;;
431         *ppp_attach = malloc( sizeof(input_attachment_t**) * p_sys->i_attachments );
432         for( i = 0; i < p_sys->i_attachments; i++ )
433             (*ppp_attach)[i] = vlc_input_attachment_Duplicate( p_sys->attachments[i] );
434         return VLC_SUCCESS;
435     }
436
437     return demux2_vaControlHelper( p_demux->s, p_sys->i_data_pos, -1,
438                                    8*0, 1, i_query, args );
439 }
440
441 enum
442 {
443     META_STREAMINFO = 0,
444     META_SEEKTABLE = 3,
445     META_COMMENT = 4,
446     META_PICTURE = 6,
447 };
448
449 static inline int Get24bBE( const uint8_t *p )
450 {
451     return (p[0] << 16)|(p[1] << 8)|(p[2]);
452 }
453
454 static void ParseStreamInfo( demux_t *p_demux, int *pi_rate, int64_t *pi_count, uint8_t *p_data, int i_data );
455 static void ParseSeekTable( demux_t *p_demux, const uint8_t *p_data, int i_data,
456                             int i_sample_rate );
457 static void ParseComment( demux_t *, const uint8_t *p_data, int i_data );
458 static void ParsePicture( demux_t *, const uint8_t *p_data, int i_data );
459
460 static int  ReadMeta( demux_t *p_demux, uint8_t **pp_streaminfo, int *pi_streaminfo )
461 {
462     demux_sys_t *p_sys = p_demux->p_sys;
463     int     i_peek;
464     const uint8_t *p_peek;
465     vlc_bool_t b_last;
466     int i_sample_rate;
467     int64_t i_sample_count;
468     seekpoint_t *s;
469
470     /* Read STREAMINFO */
471     i_peek = stream_Peek( p_demux->s, &p_peek, 8 );
472     if( (p_peek[4] & 0x7F) != META_STREAMINFO )
473     {
474         msg_Err( p_demux, "this isn't a STREAMINFO metadata block" );
475         return VLC_EGENERIC;
476     }
477     if( Get24bBE(&p_peek[5]) != (STREAMINFO_SIZE - 4) )
478     {
479         msg_Err( p_demux, "invalid size for a STREAMINFO metadata block" );
480         return VLC_EGENERIC;
481     }
482
483     *pi_streaminfo = 4 + STREAMINFO_SIZE;
484     *pp_streaminfo = malloc( 4 + STREAMINFO_SIZE );
485     if( *pp_streaminfo == NULL )
486         return VLC_EGENERIC;
487
488     if( stream_Read( p_demux->s, *pp_streaminfo, 4+STREAMINFO_SIZE ) != 4+STREAMINFO_SIZE )
489     {
490         msg_Err( p_demux, "failed to read STREAMINFO metadata block" );
491         free( *pp_streaminfo );
492         return VLC_EGENERIC;
493     }
494
495     /* */
496     ParseStreamInfo( p_demux, &i_sample_rate, &i_sample_count,  *pp_streaminfo, *pi_streaminfo );
497     if( i_sample_rate > 0 )
498         p_sys->i_length = i_sample_count * I64C(1000000)/i_sample_rate;
499
500     /* Be sure we have seekpoint 0 */
501     s = vlc_seekpoint_New();
502     s->i_time_offset = 0;
503     s->i_byte_offset = 0;
504     TAB_APPEND( p_sys->i_seekpoint, p_sys->seekpoint, s );
505
506     b_last = (*pp_streaminfo)[4]&0x80;
507     while( !b_last )
508     {
509         int i_len;
510         int i_type;
511
512         i_peek = stream_Peek( p_demux->s, &p_peek, 4 );
513         if( i_peek < 4 )
514             break;
515         b_last = p_peek[0]&0x80;
516         i_type = p_peek[0]&0x7f;
517         i_len  = Get24bBE( &p_peek[1] );
518
519         if( i_type == META_SEEKTABLE )
520         {
521             i_peek = stream_Peek( p_demux->s, &p_peek, 4+i_len );
522             if( i_peek == 4+i_len )
523                 ParseSeekTable( p_demux, p_peek, i_peek, i_sample_rate );
524         }
525         else if( i_type == META_COMMENT )
526         {
527             i_peek = stream_Peek( p_demux->s, &p_peek, 4+i_len );
528             if( i_peek == 4+i_len )
529                 ParseComment( p_demux, p_peek, i_peek );
530         }
531         else if( i_type == META_PICTURE )
532         {
533             i_peek = stream_Peek( p_demux->s, &p_peek, 4+i_len );
534             if( i_peek == 4+i_len )
535                 ParsePicture( p_demux, p_peek, i_peek );
536         }
537
538         if( stream_Read( p_demux->s, NULL, 4+i_len ) < 4+i_len )
539             break;
540     }
541
542     /* */
543     p_sys->i_data_pos = stream_Tell( p_demux->s );
544
545     return VLC_SUCCESS;
546 }
547 static void ParseStreamInfo( demux_t *p_demux, int *pi_rate, int64_t *pi_count, uint8_t *p_data, int i_data )
548 {
549     const int i_skip = 4+4;
550
551     *pi_rate = GetDWBE(&p_data[i_skip+4+6]) >> 12;
552     *pi_count = GetQWBE(&p_data[i_skip+4+6]) &  ((I64C(1)<<36)-1);
553 }
554
555 static void ParseSeekTable( demux_t *p_demux, const uint8_t *p_data, int i_data,
556                             int i_sample_rate )
557 {
558     demux_sys_t *p_sys = p_demux->p_sys;
559     seekpoint_t *s;
560     int i;
561
562     if( i_sample_rate <= 0 )
563         return;
564
565     /* */
566     for( i = 0; i < (i_data-4)/18; i++ )
567     {
568         const int64_t i_sample = GetQWBE( &p_data[4+18*i+0] );
569         int j;
570
571         if( i_sample < 0 || i_sample >= INT64_MAX )
572             continue;
573
574         s = vlc_seekpoint_New();
575         s->i_time_offset = i_sample * I64C(1000000)/i_sample_rate;
576         s->i_byte_offset = GetQWBE( &p_data[4+18*i+8] );
577
578         /* Check for duplicate entry */
579         for( j = 0; j < p_sys->i_seekpoint; j++ )
580         {
581             if( p_sys->seekpoint[j]->i_time_offset == s->i_time_offset ||
582                 p_sys->seekpoint[j]->i_byte_offset == s->i_byte_offset )
583             {
584                 vlc_seekpoint_Delete( s );
585                 s = NULL;
586                 break;
587             }
588         }
589         if( s )
590         {
591             TAB_APPEND( p_sys->i_seekpoint, p_sys->seekpoint, s );
592         }
593     }
594     /* TODO sort it by size and remove wrong seek entry (time not increasing) */
595 }
596
597 #define RM(x) do { i_data -= (x); p_data += (x); } while(0)
598 static void ParseComment( demux_t *p_demux, const uint8_t *p_data, int i_data )
599 {
600     demux_sys_t *p_sys = p_demux->p_sys;
601     int n;
602     int i_comment;
603
604     if( i_data < 8 )
605         return;
606
607     RM(4);
608
609     n = GetDWLE(p_data); RM(4);
610     if( n < 0 || n > i_data )
611         return;
612 #if 0
613     if( n > 0 )
614     {
615         /* TODO report vendor string ? */
616         char *psz_vendor = psz_vendor = strndup( p_data, n );
617         msg_Dbg( p_demux, "FLAC: COMMENT vendor length=%d vendor=%s\n", n, psz_vendor );
618         free( psz_vendor );
619     }
620 #endif
621     RM(n);
622
623     if( i_data < 4 )
624         return;
625
626     i_comment = GetDWLE(p_data); RM(4);
627     if( i_comment <= 0 )
628         return;
629
630     p_sys->p_meta = vlc_meta_New();
631
632     for( ; i_comment > 0; i_comment-- )
633     {
634         char *psz;
635         if( i_data < 4 )
636             break;
637         n = GetDWLE(p_data); RM(4);
638         if( n > i_data )
639             break;
640         if( n <= 0 )
641             continue;
642
643         psz = strndup( p_data, n );
644         RM(n);
645
646         EnsureUTF8( psz );
647
648 #define IF_EXTRACT(txt,var) \
649     if( !strncasecmp(psz, txt, strlen(txt)) ) \
650     { \
651         const char *oldval = vlc_meta_Get( p_sys->p_meta, vlc_meta_ ## var ); \
652         if( oldval ) \
653         { \
654             char * newval; \
655             asprintf( &newval, "%s,%s", oldval, &psz[strlen(txt)] ); \
656             vlc_meta_Set( p_sys->p_meta, vlc_meta_ ## var, newval ); \
657             free( newval ); \
658         } \
659         else \
660             vlc_meta_Set( p_sys->p_meta, vlc_meta_ ## var, &psz[strlen(txt)] ); \
661     }
662         IF_EXTRACT("TITLE=", Title )
663         else IF_EXTRACT("ALBUM=", Album )
664         else IF_EXTRACT("TRACKNUMBER=", TrackNumber )
665         else IF_EXTRACT("ARTIST=", Artist )
666         else IF_EXTRACT("COPYRIGHT=", Copyright )
667         else IF_EXTRACT("DESCRIPTION=", Description )
668         else IF_EXTRACT("GENRE=", Genre )
669         else IF_EXTRACT("DATE=", Date )
670         else if( strchr( psz, '=' ) )
671         {
672             /* generic (PERFORMER/LICENSE/ORGANIZATION/LOCATION/CONTACT/ISRC,
673              * undocumented tags and replay gain ) */
674             audio_replay_gain_t *r = &p_sys->replay_gain;
675             char *p = strchr( psz, '=' );
676             *p++ = '\0';
677             vlc_meta_AddExtra( p_sys->p_meta, psz, p );
678         }
679 #undef IF_EXTRACT
680         free( psz );
681     }
682 #undef RM
683 }
684
685 static void ParsePicture( demux_t *p_demux, const uint8_t *p_data, int i_data )
686 {
687     static const int pi_cover_score[] = {
688         0,      /* other */
689         2, 1,   /* icons */
690         10,     /* front cover */
691         9,      /* back cover */
692         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
693         6,      /* movie/video screen capture */
694         0,
695         7,      /* Illustration */
696         8,      /* Band/Artist logotype */
697         0,      /* Publisher/Studio */
698     };
699     demux_sys_t *p_sys = p_demux->p_sys;
700     int i_type;
701     int i_len;
702     char *psz_mime = NULL;
703     char *psz_description = NULL;
704     input_attachment_t *p_attachment;
705     char psz_name[128];
706
707     if( i_data < 4 + 3*4 )
708         return;
709 #define RM(x) do { i_data -= (x); p_data += (x); } while(0)
710     RM(4);
711
712     i_type = GetDWBE( p_data ); RM(4);
713     i_len = GetDWBE( p_data ); RM(4);
714     if( i_data < i_len + 4 )
715         goto error;
716     psz_mime = strndup( p_data, i_len ); RM(i_len);
717     i_len = GetDWBE( p_data ); RM(4);
718     if( i_data < i_len + 4*4 + 4)
719         goto error;
720     psz_description = strndup( p_data, i_len ); RM(i_len);
721     EnsureUTF8( psz_description );
722     RM(4*4);
723     i_len = GetDWBE( p_data ); RM(4);
724     if( i_len > i_data )
725         goto error;
726
727     msg_Dbg( p_demux, "FLAC: Picture type=%d mime=%s description='%s' file length=%d",
728              i_type, psz_mime, psz_description, i_len );
729
730     snprintf( psz_name, sizeof(psz_name), "picture%d", p_sys->i_attachments );
731     if( !strcasecmp( psz_mime, "image/jpeg" ) )
732         strcat( psz_name, ".jpg" );
733     else if( !strcasecmp( psz_mime, "image/png" ) )
734         strcat( psz_name, ".png" );
735
736     p_attachment = vlc_input_attachment_New( psz_name, psz_mime, psz_description,
737                                              p_data, i_data );
738     TAB_APPEND( p_sys->i_attachments, p_sys->attachments, p_attachment );
739
740     if( i_type >= 0 && i_type < sizeof(pi_cover_score)/sizeof(pi_cover_score[0]) &&
741         p_sys->i_cover_score < pi_cover_score[i_type] )
742     {
743         p_sys->i_cover_idx = p_sys->i_attachments-1;
744         p_sys->i_cover_score = pi_cover_score[i_type];
745     }
746 error:
747     if( psz_mime )
748         free( psz_mime );
749     if( psz_description )
750         free( psz_description );
751 }
752 #undef RM
753