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