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