]> git.sesse.net Git - vlc/blob - modules/demux/flac.c
Removes trailing spaces. Removes tabs.
[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_attachment;
82     input_attachment_t **attachment;
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_attachment, p_sys->attachment );
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     if( ( p_id3 = module_Need( p_demux, "meta reader", NULL, 0 ) ) )
159     {
160         vlc_meta_t *p_meta = (vlc_meta_t *)p_demux->p_private;
161
162         if( !p_sys->p_meta )
163         {
164             p_sys->p_meta = p_meta;
165         }
166         else if( p_meta )
167         {
168             vlc_meta_Merge( p_sys->p_meta, p_meta );
169             vlc_meta_Delete( p_meta );
170         }
171         p_demux->p_private = NULL;
172         module_Unneed( p_demux, p_id3 );
173     }
174
175     if( p_sys->i_cover_idx < p_sys->i_attachment )
176     {
177         char psz_url[128];
178         if( !p_sys->p_meta )
179             p_sys->p_meta = vlc_meta_New();
180         snprintf( psz_url, sizeof(psz_url), "attachment://%s",
181                   p_sys->attachment[p_sys->i_cover_idx]->psz_name );
182         vlc_meta_Set( p_sys->p_meta, vlc_meta_ArtworkURL, psz_url );
183     }
184     vlc_audio_replay_gain_MergeFromMeta( &p_sys->replay_gain, p_sys->p_meta );
185     return VLC_SUCCESS;
186 }
187
188 /*****************************************************************************
189  * Close: frees unused data
190  *****************************************************************************/
191 static void Close( vlc_object_t * p_this )
192 {
193     demux_t     *p_demux = (demux_t*)p_this;
194     demux_sys_t *p_sys = p_demux->p_sys;
195
196     /* Unneed module */
197     module_Unneed( p_sys->p_packetizer, p_sys->p_packetizer->p_module );
198
199     if( p_sys->p_packetizer->fmt_in.p_extra )
200         free( p_sys->p_packetizer->fmt_in.p_extra );
201
202     /* Delete the decoder */
203     vlc_object_destroy( p_sys->p_packetizer );
204     if( p_sys->p_meta )
205         vlc_meta_Delete( p_sys->p_meta );
206     free( p_sys );
207 }
208
209 /*****************************************************************************
210  * Demux: reads and demuxes data packets
211  *****************************************************************************
212  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
213  *****************************************************************************/
214 static int Demux( demux_t *p_demux )
215 {
216     demux_sys_t *p_sys = p_demux->p_sys;
217     block_t     *p_block_in, *p_block_out;
218
219     if( !( p_block_in = stream_Block( p_demux->s, FLAC_PACKET_SIZE ) ) )
220         return 0;
221
222     p_block_in->i_pts = p_block_in->i_dts = p_sys->b_start ? 1 : 0;
223     p_sys->b_start = VLC_FALSE;
224
225     while( (p_block_out = p_sys->p_packetizer->pf_packetize(
226                 p_sys->p_packetizer, &p_block_in )) )
227     {
228         while( p_block_out )
229         {
230             block_t *p_next = p_block_out->p_next;
231
232             p_block_out->p_next = NULL;
233
234             if( p_sys->p_es == NULL )
235             {
236                 p_sys->p_packetizer->fmt_out.b_packetized = VLC_TRUE;
237                 p_sys->p_packetizer->fmt_out.audio_replay_gain = p_sys->replay_gain;
238                 p_sys->p_es = es_out_Add( p_demux->out, &p_sys->p_packetizer->fmt_out);
239             }
240
241             /* set PCR */
242             if( p_block_out->i_dts >= p_sys->i_pts_start )
243                 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block_out->i_dts );
244             else
245                 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
246
247             p_sys->i_pts = p_block_out->i_dts;
248             es_out_Send( p_demux->out, p_sys->p_es, p_block_out );
249
250             p_block_out = p_next;
251         }
252     }
253     return 1;
254 }
255
256 /*****************************************************************************
257  * Control:
258  *****************************************************************************/
259 static int64_t ControlGetLength( demux_t *p_demux )
260 {
261     demux_sys_t *p_sys = p_demux->p_sys;
262     const int64_t i_size = stream_Size(p_demux->s) - p_sys->i_data_pos;
263     int64_t i_length = p_sys->i_length;
264     int i;
265
266     /* Try to fix length using seekpoint and current size for truncated file */
267     for( i = p_sys->i_seekpoint-1; i >= 0; i-- )
268     {
269         seekpoint_t *s = p_sys->seekpoint[i];
270         if( s->i_byte_offset <= i_size )
271         {
272             if( i+1 < p_sys->i_seekpoint )
273             {
274                 /* Broken file */
275                 seekpoint_t *n = p_sys->seekpoint[i+1];
276                 assert( n->i_byte_offset != s->i_byte_offset); /* Should be ensured by ParseSeekTable */
277                 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);
278             }
279             break;
280         }
281     }
282     return i_length;
283 }
284
285 static int64_t ControlGetTime( demux_t *p_demux )
286 {
287     demux_sys_t *p_sys = p_demux->p_sys;
288     return __MAX(p_sys->i_pts, p_sys->i_pts_start) + p_sys->i_time_offset;
289 }
290
291 static int ControlSetTime( demux_t *p_demux, int64_t i_time )
292 {
293     demux_sys_t *p_sys = p_demux->p_sys;
294     int64_t i_next_time;
295     int64_t i_next_offset;
296     int64_t i_delta_time;
297     int64_t i_delta_offset;
298     vlc_bool_t b_seekable;
299     int i;
300
301     /* */
302     stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_seekable );
303     if( !b_seekable )
304         return VLC_EGENERIC;
305
306     /* */
307     assert( p_sys->i_seekpoint > 0 );   /* ReadMeta ensure at least (0,0) */
308     for( i = p_sys->i_seekpoint-1; i >= 0; i-- )
309     {
310         if( p_sys->seekpoint[i]->i_time_offset <= i_time )
311             break;
312     }
313     if( i+1 < p_sys->i_seekpoint )
314     {
315         i_next_time   = p_sys->seekpoint[i+1]->i_time_offset;
316         i_next_offset = p_sys->seekpoint[i+1]->i_byte_offset;
317     }
318     else
319     {
320         i_next_time   = p_sys->i_length;
321         i_next_offset = stream_Size(p_demux->s)-p_sys->i_data_pos;
322     }
323     i_delta_time = i_time - p_sys->seekpoint[i]->i_time_offset;
324     i_delta_offset = (i_next_offset - p_sys->seekpoint[i]->i_byte_offset) * i_delta_time /
325                             (p_sys->seekpoint[i+1]->i_time_offset-p_sys->seekpoint[i]->i_time_offset);
326
327     /* XXX We do exact seek if it's not too far away(45s) */
328     if( i_delta_time < 45*I64C(1000000) )
329     {
330         if( stream_Seek( p_demux->s, p_sys->seekpoint[i]->i_byte_offset+p_sys->i_data_pos ) )
331             return VLC_EGENERIC;
332         p_sys->i_time_offset = p_sys->seekpoint[i]->i_time_offset - p_sys->i_pts;
333         p_sys->i_pts_start = p_sys->i_pts+i_delta_time;
334         es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, p_sys->p_es, p_sys->i_pts_start );
335     }
336     else
337     {
338         if( stream_Seek( p_demux->s, p_sys->seekpoint[i]->i_byte_offset+p_sys->i_data_pos + i_delta_offset ) )
339             return VLC_EGENERIC;
340         p_sys->i_pts_start = p_sys->i_pts;
341         p_sys->i_time_offset = (p_sys->seekpoint[i]->i_time_offset+i_delta_time) - p_sys->i_pts;
342     }
343     return VLC_SUCCESS;
344 }
345
346 static int Control( demux_t *p_demux, int i_query, va_list args )
347 {
348     demux_sys_t *p_sys = p_demux->p_sys;
349
350     if( i_query == DEMUX_GET_META )
351     {
352         vlc_meta_t *p_meta = (vlc_meta_t *)va_arg( args, vlc_meta_t* );
353         if( p_demux->p_sys->p_meta )
354             vlc_meta_Merge( p_meta, p_demux->p_sys->p_meta );
355         return VLC_SUCCESS;
356     }
357     else if( i_query == DEMUX_GET_LENGTH )
358     {
359         int64_t *pi64 = (int64_t*)va_arg( args, int64_t * );
360         *pi64 = ControlGetLength( p_demux );
361         return VLC_SUCCESS;
362     }
363     else if( i_query == DEMUX_SET_TIME )
364     {
365         int64_t i_time = (int64_t)va_arg( args, int64_t );
366         return ControlSetTime( p_demux, i_time );
367     }
368     else if( i_query == DEMUX_SET_POSITION )
369     {
370         const double f = (double)va_arg( args, double );
371         int64_t i_time = f * ControlGetLength( p_demux );
372         return ControlSetTime( p_demux, i_time );
373     }
374     else if( i_query == DEMUX_GET_TIME )
375     {
376         int64_t *pi64 = (int64_t*)va_arg( args, int64_t * );
377         *pi64 = ControlGetTime( p_demux );
378         return VLC_SUCCESS;
379     }
380     else if( i_query == DEMUX_GET_POSITION )
381     {
382         double *pf = (double*)va_arg( args, double * );
383         const int64_t i_length = ControlGetLength(p_demux);
384         if( i_length > 0 )
385             *pf = (double)ControlGetTime(p_demux) / (double)i_length;
386         else
387             *pf= 0.0;
388         return VLC_SUCCESS;
389     }
390     else if( i_query == DEMUX_GET_ATTACHMENTS )
391     {
392         input_attachment_t ***ppp_attach =
393             (input_attachment_t***)va_arg( args, input_attachment_t*** );
394         int *pi_int = (int*)va_arg( args, int * );
395         int i;
396
397         if( p_sys->i_attachment <= 0 )
398             return VLC_EGENERIC;
399
400         *pi_int = p_sys->i_attachment;;
401         *ppp_attach = malloc( sizeof(input_attachment_t**) * p_sys->i_attachment );
402         for( i = 0; i < p_sys->i_attachment; i++ )
403             *(ppp_attach)[i] = vlc_input_attachment_Duplicate( p_sys->attachment[i] );
404         return VLC_SUCCESS;
405     }
406
407     return demux2_vaControlHelper( p_demux->s, p_sys->i_data_pos, -1,
408                                    8*0, 1, i_query, args );
409 }
410
411 enum
412 {
413     META_STREAMINFO = 0,
414     META_SEEKTABLE = 3,
415     META_COMMENT = 4,
416     META_PICTURE = 6,
417 };
418
419 static inline int Get24bBE( const uint8_t *p )
420 {
421     return (p[0] << 16)|(p[1] << 8)|(p[2]);
422 }
423
424 static void ParseStreamInfo( demux_t *p_demux, int *pi_rate, int64_t *pi_count, uint8_t *p_data, int i_data );
425 static void ParseSeekTable( demux_t *p_demux, const uint8_t *p_data, int i_data,
426                             int i_sample_rate );
427 static void ParseComment( demux_t *, const uint8_t *p_data, int i_data );
428 static void ParsePicture( demux_t *, const uint8_t *p_data, int i_data );
429
430 static int  ReadMeta( demux_t *p_demux, uint8_t **pp_streaminfo, int *pi_streaminfo )
431 {
432     demux_sys_t *p_sys = p_demux->p_sys;
433     int     i_peek;
434     const uint8_t *p_peek;
435     vlc_bool_t b_last;
436     int i_sample_rate;
437     int64_t i_sample_count;
438     seekpoint_t *s;
439
440     /* Read STREAMINFO */
441     i_peek = stream_Peek( p_demux->s, &p_peek, 8 );
442     if( (p_peek[4] & 0x7F) != META_STREAMINFO )
443     {
444         msg_Err( p_demux, "this isn't a STREAMINFO metadata block" );
445         return VLC_EGENERIC;
446     }
447     if( Get24bBE(&p_peek[5]) != (STREAMINFO_SIZE - 4) )
448     {
449         msg_Err( p_demux, "invalid size for a STREAMINFO metadata block" );
450         return VLC_EGENERIC;
451     }
452
453     *pi_streaminfo = 4 + STREAMINFO_SIZE;
454     *pp_streaminfo = malloc( 4 + STREAMINFO_SIZE );
455     if( *pp_streaminfo == NULL )
456         return VLC_EGENERIC;
457
458     if( stream_Read( p_demux->s, *pp_streaminfo, 4+STREAMINFO_SIZE ) != 4+STREAMINFO_SIZE )
459     {
460         msg_Err( p_demux, "failed to read STREAMINFO metadata block" );
461         free( *pp_streaminfo );
462         return VLC_EGENERIC;
463     }
464
465     /* */
466     ParseStreamInfo( p_demux, &i_sample_rate, &i_sample_count,  *pp_streaminfo, *pi_streaminfo );
467     if( i_sample_rate > 0 )
468         p_sys->i_length = i_sample_count * I64C(1000000)/i_sample_rate;
469
470     /* Be sure we have seekpoint 0 */
471     s = vlc_seekpoint_New();
472     s->i_time_offset = 0;
473     s->i_byte_offset = 0;
474     TAB_APPEND( p_sys->i_seekpoint, p_sys->seekpoint, s );
475
476
477
478     b_last = (*pp_streaminfo)[4]&0x80;
479     while( !b_last )
480     {
481         int i_len;
482         int i_type;
483
484         i_peek = stream_Peek( p_demux->s, &p_peek, 4 );
485         if( i_peek < 4 )
486             break;
487         b_last = p_peek[0]&0x80;
488         i_type = p_peek[0]&0x7f;
489         i_len  = Get24bBE( &p_peek[1] );
490
491         if( i_type == META_SEEKTABLE )
492         {
493             i_peek = stream_Peek( p_demux->s, &p_peek, 4+i_len );
494             if( i_peek == 4+i_len )
495                 ParseSeekTable( p_demux, p_peek, i_peek, i_sample_rate );
496         }
497         else if( i_type == META_COMMENT )
498         {
499             i_peek = stream_Peek( p_demux->s, &p_peek, 4+i_len );
500             if( i_peek == 4+i_len )
501                 ParseComment( p_demux, p_peek, i_peek );
502         }
503         else if( i_type == META_PICTURE )
504         {
505             i_peek = stream_Peek( p_demux->s, &p_peek, 4+i_len );
506             if( i_peek == 4+i_len )
507                 ParsePicture( p_demux, p_peek, i_peek );
508         }
509
510         if( stream_Read( p_demux->s, NULL, 4+i_len ) < 4+i_len )
511             break;
512     }
513
514     /* */
515     p_sys->i_data_pos = stream_Tell( p_demux->s );
516
517     return VLC_SUCCESS;
518 }
519 static void ParseStreamInfo( demux_t *p_demux, int *pi_rate, int64_t *pi_count, uint8_t *p_data, int i_data )
520 {
521     const int i_skip = 4+4;
522
523     *pi_rate = GetDWBE(&p_data[i_skip+4+6]) >> 12;
524     *pi_count = GetQWBE(&p_data[i_skip+4+6]) &  ((I64C(1)<<36)-1);
525 }
526
527 static void ParseSeekTable( demux_t *p_demux, const uint8_t *p_data, int i_data,
528                             int i_sample_rate )
529 {
530     demux_sys_t *p_sys = p_demux->p_sys;
531     seekpoint_t *s;
532     int i;
533
534     if( i_sample_rate <= 0 )
535         return;
536
537     /* */
538     for( i = 0; i < (i_data-4)/18; i++ )
539     {
540         const int64_t i_sample = GetQWBE( &p_data[4+18*i+0] );
541         int j;
542
543         if( i_sample < 0 || i_sample >= INT64_MAX )
544             continue;
545
546         s = vlc_seekpoint_New();
547         s->i_time_offset = i_sample * I64C(1000000)/i_sample_rate;
548         s->i_byte_offset = GetQWBE( &p_data[4+18*i+8] );
549
550         /* Check for duplicate entry */
551         for( j = 0; j < p_sys->i_seekpoint; j++ )
552         {
553             if( p_sys->seekpoint[j]->i_time_offset == s->i_time_offset ||
554                 p_sys->seekpoint[j]->i_byte_offset == s->i_byte_offset )
555             {
556                 vlc_seekpoint_Delete( s );
557                 s = NULL;
558                 break;
559             }
560         }
561         if( s )
562         {
563             TAB_APPEND( p_sys->i_seekpoint, p_sys->seekpoint, s );
564         }
565     }
566     /* TODO sort it by size and remove wrong seek entry (time not increasing) */
567 }
568
569 #define RM(x) do { i_data -= (x); p_data += (x); } while(0)
570 static void ParseComment( demux_t *p_demux, const uint8_t *p_data, int i_data )
571 {
572     demux_sys_t *p_sys = p_demux->p_sys;
573     int n;
574     int i_comment;
575
576     if( i_data < 8 )
577         return;
578
579     RM(4);
580
581     n = GetDWLE(p_data); RM(4);
582     if( n < 0 || n > i_data )
583         return;
584 #if 0
585     if( n > 0 )
586     {
587         /* TODO report vendor string ? */
588         char *psz_vendor = psz_vendor = strndup( p_data, n );
589         msg_Dbg( p_demux, "FLAC: COMMENT vendor length=%d vendor=%s\n", n, psz_vendor );
590         free( psz_vendor );
591     }
592 #endif
593     RM(n);
594
595     if( i_data < 4 )
596         return;
597
598     i_comment = GetDWLE(p_data); RM(4);
599     if( i_comment <= 0 )
600         return;
601
602     p_sys->p_meta = vlc_meta_New();
603
604     for( ; i_comment > 0; i_comment-- )
605     {
606         char *psz;
607         if( i_data < 4 )
608             break;
609         n = GetDWLE(p_data); RM(4);
610         if( n > i_data )
611             break;
612         if( n <= 0 )
613             continue;
614
615         psz = strndup( p_data, n );
616         RM(n);
617
618         EnsureUTF8( psz );
619
620 #define IF_EXTRACT(txt,var) \
621     if( !strncasecmp(psz, txt, strlen(txt)) ) \
622     { \
623         char * oldval = vlc_meta_Get( p_sys->p_meta, vlc_meta_ ## var ); \
624         if( oldval ) \
625         { \
626             char * newval; \
627             asprintf( &newval, "%s,%s", oldval, &psz[strlen(txt)] ); \
628             vlc_meta_Set( p_sys->p_meta, vlc_meta_ ## var, newval ); \
629             free( newval ); \
630         } \
631         else \
632             vlc_meta_Set( p_sys->p_meta, vlc_meta_ ## var, &psz[strlen(txt)] ); \
633         free( oldval ); \
634     }
635         IF_EXTRACT("TITLE=", Title )
636         else IF_EXTRACT("ALBUM=", Album )
637         else IF_EXTRACT("TRACKNUMBER=", TrackNumber )
638         else IF_EXTRACT("ARTIST=", Artist )
639         else IF_EXTRACT("COPYRIGHT=", Copyright )
640         else IF_EXTRACT("DESCRIPTION=", Description )
641         else IF_EXTRACT("GENRE=", Genre )
642         else IF_EXTRACT("DATE=", Date )
643         else if( strchr( psz, '=' ) )
644         {
645             /* generic (PERFORMER/LICENSE/ORGANIZATION/LOCATION/CONTACT/ISRC,
646              * undocumented tags and replay gain ) */
647             audio_replay_gain_t *r = &p_sys->replay_gain;
648             char *p = strchr( psz, '=' );
649             *p++ = '\0';
650             vlc_meta_AddExtra( p_sys->p_meta, psz, p );
651         }
652 #undef IF_EXTRACT
653         free( psz );
654     }
655 #undef RM
656 }
657
658 static void ParsePicture( demux_t *p_demux, const uint8_t *p_data, int i_data )
659 {
660     static const int pi_cover_score[] = {
661         0,      /* other */
662         2, 1,   /* icons */
663         10,     /* front cover */
664         9,      /* back cover */
665         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
666         6,      /* movie/video screen capture */
667         0,
668         7,      /* Illustration */
669         8,      /* Band/Artist logotype */
670         0,      /* Publisher/Studio */
671     };
672     demux_sys_t *p_sys = p_demux->p_sys;
673     int i_type;
674     int i_len;
675     char *psz_mime = NULL;
676     char *psz_description = NULL;
677     input_attachment_t *p_attachment;
678     char psz_name[128];
679
680     if( i_data < 4 + 3*4 )
681         return;
682 #define RM(x) do { i_data -= (x); p_data += (x); } while(0)
683     RM(4);
684
685     i_type = GetDWBE( p_data ); RM(4);
686     i_len = GetDWBE( p_data ); RM(4);
687     if( i_data < i_len + 4 )
688         goto error;
689     psz_mime = strndup( p_data, i_len ); RM(i_len);
690     i_len = GetDWBE( p_data ); RM(4);
691     if( i_data < i_len + 4*4 + 4)
692         goto error;
693     psz_description = strndup( p_data, i_len ); RM(i_len);
694     EnsureUTF8( psz_description );
695     RM(4*4);
696     i_len = GetDWBE( p_data ); RM(4);
697     if( i_len > i_data )
698         goto error;
699
700     msg_Dbg( p_demux, "FLAC: Picture type=%d mime=%s description='%s' file length=%d",
701              i_type, psz_mime, psz_description, i_len );
702
703     snprintf( psz_name, sizeof(psz_name), "picture%d", p_sys->i_attachment );
704     if( !strcasecmp( psz_mime, "image/jpeg" ) )
705         strcat( psz_name, ".jpg" );
706     else if( !strcasecmp( psz_mime, "image/png" ) )
707         strcat( psz_name, ".png" );
708
709     p_attachment = vlc_input_attachment_New( psz_name, psz_mime, psz_description,
710                                              p_data, i_data );
711     TAB_APPEND( p_sys->i_attachment, p_sys->attachment, p_attachment );
712
713     if( i_type >= 0 && i_type < sizeof(pi_cover_score)/sizeof(pi_cover_score[0]) &&
714         p_sys->i_cover_score < pi_cover_score[i_type] )
715     {
716         p_sys->i_cover_idx = p_sys->i_attachment-1;
717         p_sys->i_cover_score = pi_cover_score[i_type];
718     }
719 error:
720     if( psz_mime )
721         free( psz_mime );
722     if( psz_description )
723         free( psz_description );
724 }
725 #undef RM
726