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