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