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