]> git.sesse.net Git - vlc/blob - modules/demux/flac.c
Let the input handle *extra* meta data and attachments for flac audio.
[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
67     vlc_meta_t *p_meta;
68     audio_replay_gain_t replay_gain;
69
70     int64_t i_time_offset;
71     int64_t i_pts;
72     int64_t i_pts_start;
73
74     int64_t i_length; /* Length from stream info */
75     int64_t i_data_pos;
76
77     /* */
78     int         i_seekpoint;
79     seekpoint_t **seekpoint;
80
81     /* */
82     int                i_attachments;
83     input_attachment_t **attachments;
84     int                i_cover_idx;
85     int                i_cover_score;
86 };
87
88 #define STREAMINFO_SIZE 38
89 #define FLAC_PACKET_SIZE 16384
90
91 /*****************************************************************************
92  * Open: initializes ES structures
93  *****************************************************************************/
94 static int Open( vlc_object_t * p_this )
95 {
96     demux_t     *p_demux = (demux_t*)p_this;
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     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     /* Unneed module */
186     module_Unneed( p_sys->p_packetizer, p_sys->p_packetizer->p_module );
187
188     if( p_sys->p_packetizer->fmt_in.p_extra )
189         free( p_sys->p_packetizer->fmt_in.p_extra );
190
191     /* Delete the decoder */
192     vlc_object_destroy( p_sys->p_packetizer );
193     if( p_sys->p_meta )
194         vlc_meta_Delete( p_sys->p_meta );
195     free( p_sys );
196 }
197
198 /*****************************************************************************
199  * Demux: reads and demuxes data packets
200  *****************************************************************************
201  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
202  *****************************************************************************/
203 static int Demux( demux_t *p_demux )
204 {
205     demux_sys_t *p_sys = p_demux->p_sys;
206     block_t     *p_block_in, *p_block_out;
207
208     if( !( p_block_in = stream_Block( p_demux->s, FLAC_PACKET_SIZE ) ) )
209         return 0;
210
211     p_block_in->i_pts = p_block_in->i_dts = p_sys->b_start ? 1 : 0;
212     p_sys->b_start = VLC_FALSE;
213
214     while( (p_block_out = p_sys->p_packetizer->pf_packetize(
215                 p_sys->p_packetizer, &p_block_in )) )
216     {
217         while( p_block_out )
218         {
219             block_t *p_next = p_block_out->p_next;
220
221             p_block_out->p_next = NULL;
222
223             if( p_sys->p_es == NULL )
224             {
225                 p_sys->p_packetizer->fmt_out.b_packetized = VLC_TRUE;
226                 p_sys->p_packetizer->fmt_out.audio_replay_gain = p_sys->replay_gain;
227                 p_sys->p_es = es_out_Add( p_demux->out, &p_sys->p_packetizer->fmt_out);
228             }
229
230             /* set PCR */
231             if( p_block_out->i_dts >= p_sys->i_pts_start )
232                 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block_out->i_dts );
233             else
234                 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
235
236             p_sys->i_pts = p_block_out->i_dts;
237             es_out_Send( p_demux->out, p_sys->p_es, p_block_out );
238
239             p_block_out = p_next;
240         }
241     }
242     return 1;
243 }
244
245 /*****************************************************************************
246  * Control:
247  *****************************************************************************/
248 static int64_t ControlGetLength( demux_t *p_demux )
249 {
250     demux_sys_t *p_sys = p_demux->p_sys;
251     const int64_t i_size = stream_Size(p_demux->s) - p_sys->i_data_pos;
252     int64_t i_length = p_sys->i_length;
253     int i;
254
255     /* Try to fix length using seekpoint and current size for truncated file */
256     for( i = p_sys->i_seekpoint-1; i >= 0; i-- )
257     {
258         seekpoint_t *s = p_sys->seekpoint[i];
259         if( s->i_byte_offset <= i_size )
260         {
261             if( i+1 < p_sys->i_seekpoint )
262             {
263                 /* Broken file */
264                 seekpoint_t *n = p_sys->seekpoint[i+1];
265                 assert( n->i_byte_offset != s->i_byte_offset); /* Should be ensured by ParseSeekTable */
266                 i_length = s->i_time_offset + (n->i_time_offset-s->i_time_offset) * (i_size-s->i_byte_offset) / (n->i_byte_offset-s->i_byte_offset);
267             }
268             break;
269         }
270     }
271     return i_length;
272 }
273
274 static int64_t ControlGetTime( demux_t *p_demux )
275 {
276     demux_sys_t *p_sys = p_demux->p_sys;
277     return __MAX(p_sys->i_pts, p_sys->i_pts_start) + p_sys->i_time_offset;
278 }
279
280 static int ControlSetTime( demux_t *p_demux, int64_t i_time )
281 {
282     demux_sys_t *p_sys = p_demux->p_sys;
283     int64_t i_next_time;
284     int64_t i_next_offset;
285     int64_t i_delta_time;
286     int64_t i_delta_offset;
287     vlc_bool_t b_seekable;
288     int i;
289
290     /* */
291     stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_seekable );
292     if( !b_seekable )
293         return VLC_EGENERIC;
294
295     /* */
296     assert( p_sys->i_seekpoint > 0 );   /* ReadMeta ensure at least (0,0) */
297     for( i = p_sys->i_seekpoint-1; i >= 0; i-- )
298     {
299         if( p_sys->seekpoint[i]->i_time_offset <= i_time )
300             break;
301     }
302     if( i+1 < p_sys->i_seekpoint )
303     {
304         i_next_time   = p_sys->seekpoint[i+1]->i_time_offset;
305         i_next_offset = p_sys->seekpoint[i+1]->i_byte_offset;
306     }
307     else
308     {
309         i_next_time   = p_sys->i_length;
310         i_next_offset = stream_Size(p_demux->s)-p_sys->i_data_pos;
311     }
312     i_delta_time = i_time - p_sys->seekpoint[i]->i_time_offset;
313     i_delta_offset = (i_next_offset - p_sys->seekpoint[i]->i_byte_offset) * i_delta_time /
314                             (p_sys->seekpoint[i+1]->i_time_offset-p_sys->seekpoint[i]->i_time_offset);
315
316     /* XXX We do exact seek if it's not too far away(45s) */
317     if( i_delta_time < 45*I64C(1000000) )
318     {
319         if( stream_Seek( p_demux->s, p_sys->seekpoint[i]->i_byte_offset+p_sys->i_data_pos ) )
320             return VLC_EGENERIC;
321         p_sys->i_time_offset = p_sys->seekpoint[i]->i_time_offset - p_sys->i_pts;
322         p_sys->i_pts_start = p_sys->i_pts+i_delta_time;
323         es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, p_sys->p_es, p_sys->i_pts_start );
324     }
325     else
326     {
327         if( stream_Seek( p_demux->s, p_sys->seekpoint[i]->i_byte_offset+p_sys->i_data_pos + i_delta_offset ) )
328             return VLC_EGENERIC;
329         p_sys->i_pts_start = p_sys->i_pts;
330         p_sys->i_time_offset = (p_sys->seekpoint[i]->i_time_offset+i_delta_time) - p_sys->i_pts;
331     }
332     return VLC_SUCCESS;
333 }
334
335 static int Control( demux_t *p_demux, int i_query, va_list args )
336 {
337     demux_sys_t *p_sys = p_demux->p_sys;
338
339     if( i_query == DEMUX_GET_META )
340     {
341         vlc_meta_t *p_meta = (vlc_meta_t *)va_arg( args, vlc_meta_t* );
342         if( p_demux->p_sys->p_meta )
343             vlc_meta_Merge( p_meta, p_demux->p_sys->p_meta );
344         return VLC_SUCCESS;
345     }
346     else if( i_query == DEMUX_HAS_UNSUPPORTED_META )
347     {
348         vlc_bool_t *pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
349         *pb_bool = VLC_TRUE;
350         return VLC_SUCCESS;
351     }
352     else if( i_query == DEMUX_GET_LENGTH )
353     {
354         int64_t *pi64 = (int64_t*)va_arg( args, int64_t * );
355         *pi64 = ControlGetLength( p_demux );
356         return VLC_SUCCESS;
357     }
358     else if( i_query == DEMUX_SET_TIME )
359     {
360         int64_t i_time = (int64_t)va_arg( args, int64_t );
361         return ControlSetTime( p_demux, i_time );
362     }
363     else if( i_query == DEMUX_SET_POSITION )
364     {
365         const double f = (double)va_arg( args, double );
366         int64_t i_time = f * ControlGetLength( p_demux );
367         return ControlSetTime( p_demux, i_time );
368     }
369     else if( i_query == DEMUX_GET_TIME )
370     {
371         int64_t *pi64 = (int64_t*)va_arg( args, int64_t * );
372         *pi64 = ControlGetTime( p_demux );
373         return VLC_SUCCESS;
374     }
375     else if( i_query == DEMUX_GET_POSITION )
376     {
377         double *pf = (double*)va_arg( args, double * );
378         const int64_t i_length = ControlGetLength(p_demux);
379         if( i_length > 0 )
380             *pf = (double)ControlGetTime(p_demux) / (double)i_length;
381         else
382             *pf= 0.0;
383         return VLC_SUCCESS;
384     }
385     else if( i_query == DEMUX_GET_ATTACHMENTS )
386     {
387         input_attachment_t ***ppp_attach =
388             (input_attachment_t***)va_arg( args, input_attachment_t*** );
389         int *pi_int = (int*)va_arg( args, int * );
390         int i;
391
392         if( p_sys->i_attachments <= 0 )
393             return VLC_EGENERIC;
394
395         *pi_int = p_sys->i_attachments;;
396         *ppp_attach = malloc( sizeof(input_attachment_t**) * p_sys->i_attachments );
397         for( i = 0; i < p_sys->i_attachments; i++ )
398             (*ppp_attach)[i] = vlc_input_attachment_Duplicate( p_sys->attachments[i] );
399         return VLC_SUCCESS;
400     }
401
402     return demux2_vaControlHelper( p_demux->s, p_sys->i_data_pos, -1,
403                                    8*0, 1, i_query, args );
404 }
405
406 enum
407 {
408     META_STREAMINFO = 0,
409     META_SEEKTABLE = 3,
410     META_COMMENT = 4,
411     META_PICTURE = 6,
412 };
413
414 static inline int Get24bBE( const uint8_t *p )
415 {
416     return (p[0] << 16)|(p[1] << 8)|(p[2]);
417 }
418
419 static void ParseStreamInfo( demux_t *p_demux, int *pi_rate, int64_t *pi_count, uint8_t *p_data, int i_data );
420 static void ParseSeekTable( demux_t *p_demux, const uint8_t *p_data, int i_data,
421                             int i_sample_rate );
422 static void ParseComment( demux_t *, const uint8_t *p_data, int i_data );
423 static void ParsePicture( demux_t *, const uint8_t *p_data, int i_data );
424
425 static int  ReadMeta( demux_t *p_demux, uint8_t **pp_streaminfo, int *pi_streaminfo )
426 {
427     demux_sys_t *p_sys = p_demux->p_sys;
428     int     i_peek;
429     const uint8_t *p_peek;
430     vlc_bool_t b_last;
431     int i_sample_rate;
432     int64_t i_sample_count;
433     seekpoint_t *s;
434
435     /* Read STREAMINFO */
436     i_peek = stream_Peek( p_demux->s, &p_peek, 8 );
437     if( (p_peek[4] & 0x7F) != META_STREAMINFO )
438     {
439         msg_Err( p_demux, "this isn't a STREAMINFO metadata block" );
440         return VLC_EGENERIC;
441     }
442     if( Get24bBE(&p_peek[5]) != (STREAMINFO_SIZE - 4) )
443     {
444         msg_Err( p_demux, "invalid size for a STREAMINFO metadata block" );
445         return VLC_EGENERIC;
446     }
447
448     *pi_streaminfo = 4 + STREAMINFO_SIZE;
449     *pp_streaminfo = malloc( 4 + STREAMINFO_SIZE );
450     if( *pp_streaminfo == NULL )
451         return VLC_EGENERIC;
452
453     if( stream_Read( p_demux->s, *pp_streaminfo, 4+STREAMINFO_SIZE ) != 4+STREAMINFO_SIZE )
454     {
455         msg_Err( p_demux, "failed to read STREAMINFO metadata block" );
456         free( *pp_streaminfo );
457         return VLC_EGENERIC;
458     }
459
460     /* */
461     ParseStreamInfo( p_demux, &i_sample_rate, &i_sample_count,  *pp_streaminfo, *pi_streaminfo );
462     if( i_sample_rate > 0 )
463         p_sys->i_length = i_sample_count * I64C(1000000)/i_sample_rate;
464
465     /* Be sure we have seekpoint 0 */
466     s = vlc_seekpoint_New();
467     s->i_time_offset = 0;
468     s->i_byte_offset = 0;
469     TAB_APPEND( p_sys->i_seekpoint, p_sys->seekpoint, s );
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
520 static void ParseSeekTable( demux_t *p_demux, const uint8_t *p_data, int i_data,
521                             int i_sample_rate )
522 {
523     demux_sys_t *p_sys = p_demux->p_sys;
524     seekpoint_t *s;
525     int i;
526
527     if( i_sample_rate <= 0 )
528         return;
529
530     /* */
531     for( i = 0; i < (i_data-4)/18; i++ )
532     {
533         const int64_t i_sample = GetQWBE( &p_data[4+18*i+0] );
534         int j;
535
536         if( i_sample < 0 || i_sample >= INT64_MAX )
537             continue;
538
539         s = vlc_seekpoint_New();
540         s->i_time_offset = i_sample * I64C(1000000)/i_sample_rate;
541         s->i_byte_offset = GetQWBE( &p_data[4+18*i+8] );
542
543         /* Check for duplicate entry */
544         for( j = 0; j < p_sys->i_seekpoint; j++ )
545         {
546             if( p_sys->seekpoint[j]->i_time_offset == s->i_time_offset ||
547                 p_sys->seekpoint[j]->i_byte_offset == s->i_byte_offset )
548             {
549                 vlc_seekpoint_Delete( s );
550                 s = NULL;
551                 break;
552             }
553         }
554         if( s )
555         {
556             TAB_APPEND( p_sys->i_seekpoint, p_sys->seekpoint, s );
557         }
558     }
559     /* TODO sort it by size and remove wrong seek entry (time not increasing) */
560 }
561
562 #define RM(x) do { i_data -= (x); p_data += (x); } while(0)
563 static void ParseComment( demux_t *p_demux, const uint8_t *p_data, int i_data )
564 {
565     demux_sys_t *p_sys = p_demux->p_sys;
566     int n;
567     int i_comment;
568
569     if( i_data < 8 )
570         return;
571
572     RM(4);
573
574     n = GetDWLE(p_data); RM(4);
575     if( n < 0 || n > i_data )
576         return;
577 #if 0
578     if( n > 0 )
579     {
580         /* TODO report vendor string ? */
581         char *psz_vendor = psz_vendor = strndup( p_data, n );
582         msg_Dbg( p_demux, "FLAC: COMMENT vendor length=%d vendor=%s\n", n, psz_vendor );
583         free( psz_vendor );
584     }
585 #endif
586     RM(n);
587
588     if( i_data < 4 )
589         return;
590
591     i_comment = GetDWLE(p_data); RM(4);
592     if( i_comment <= 0 )
593         return;
594
595     p_sys->p_meta = vlc_meta_New();
596
597     for( ; i_comment > 0; i_comment-- )
598     {
599         char *psz;
600         if( i_data < 4 )
601             break;
602         n = GetDWLE(p_data); RM(4);
603         if( n > i_data )
604             break;
605         if( n <= 0 )
606             continue;
607
608         psz = strndup( p_data, n );
609         RM(n);
610
611         EnsureUTF8( psz );
612
613 #define IF_EXTRACT(txt,var) \
614     if( !strncasecmp(psz, txt, strlen(txt)) ) \
615     { \
616         const char *oldval = vlc_meta_Get( p_sys->p_meta, vlc_meta_ ## var ); \
617         if( oldval ) \
618         { \
619             char * newval; \
620             asprintf( &newval, "%s,%s", oldval, &psz[strlen(txt)] ); \
621             vlc_meta_Set( p_sys->p_meta, vlc_meta_ ## var, newval ); \
622             free( newval ); \
623         } \
624         else \
625             vlc_meta_Set( p_sys->p_meta, vlc_meta_ ## var, &psz[strlen(txt)] ); \
626     }
627         IF_EXTRACT("TITLE=", Title )
628         else IF_EXTRACT("ALBUM=", Album )
629         else IF_EXTRACT("TRACKNUMBER=", TrackNumber )
630         else IF_EXTRACT("ARTIST=", Artist )
631         else IF_EXTRACT("COPYRIGHT=", Copyright )
632         else IF_EXTRACT("DESCRIPTION=", Description )
633         else IF_EXTRACT("GENRE=", Genre )
634         else IF_EXTRACT("DATE=", Date )
635         else if( strchr( psz, '=' ) )
636         {
637             /* generic (PERFORMER/LICENSE/ORGANIZATION/LOCATION/CONTACT/ISRC,
638              * undocumented tags and replay gain ) */
639             audio_replay_gain_t *r = &p_sys->replay_gain;
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, const 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_attachments );
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_attachments, p_sys->attachments, 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_attachments-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