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