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