]> git.sesse.net Git - vlc/blob - modules/demux/asf/asf.c
* it's information, not informations (grep -r)
[vlc] / modules / demux / asf / asf.c
1 /*****************************************************************************
2  * asf.c : ASF demux module
3  *****************************************************************************
4  * Copyright (C) 2002-2003 VideoLAN
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28
29 #include <vlc/vlc.h>
30 #include <vlc/input.h>
31
32 #include "vlc_meta.h"
33
34 #include "codecs.h"                        /* BITMAPINFOHEADER, WAVEFORMATEX */
35 #include "libasf.h"
36
37 /*****************************************************************************
38  * Module descriptor
39  *****************************************************************************/
40 static int  Open  ( vlc_object_t * );
41 static void Close ( vlc_object_t * );
42
43 vlc_module_begin();
44     set_description( _("ASF v1.0 demuxer") );
45     set_capability( "demux2", 200 );
46     set_callbacks( Open, Close );
47     add_shortcut( "asf" );
48 vlc_module_end();
49
50
51 /*****************************************************************************
52  * Local prototypes
53  *****************************************************************************/
54 static int Demux  ( demux_t * );
55 static int Control( demux_t *, int i_query, va_list args );
56
57 typedef struct
58 {
59     int i_cat;
60
61     es_out_id_t     *p_es;
62
63     asf_object_stream_properties_t *p_sp;
64
65     mtime_t i_time;
66
67     block_t         *p_frame; /* use to gather complete frame */
68
69 } asf_track_t;
70
71 struct demux_sys_t
72 {
73     mtime_t             i_time;     /* µs */
74     mtime_t             i_length;   /* length of file file */
75     int64_t             i_bitrate;  /* global file bitrate */
76
77     asf_object_root_t            *p_root;
78     asf_object_file_properties_t *p_fp;
79
80     unsigned int        i_track;
81     asf_track_t         *track[128];
82
83     int64_t             i_data_begin;
84     int64_t             i_data_end;
85
86     vlc_meta_t          *meta;
87 };
88
89 static mtime_t  GetMoviePTS( demux_sys_t * );
90 static int      DemuxInit( demux_t * );
91 static void     DemuxEnd( demux_t * );
92 static int      DemuxPacket( demux_t * );
93
94 /*****************************************************************************
95  * Open: check file and initializes ASF structures
96  *****************************************************************************/
97 static int Open( vlc_object_t * p_this )
98 {
99     demux_t     *p_demux = (demux_t *)p_this;
100     demux_sys_t *p_sys;
101     guid_t      guid;
102     uint8_t     *p_peek;
103
104     /* a little test to see if it could be a asf stream */
105     if( stream_Peek( p_demux->s, &p_peek, 16 ) < 16 )
106     {
107         msg_Warn( p_demux, "ASF plugin discarded (cannot peek)" );
108         return VLC_EGENERIC;
109     }
110     ASF_GetGUID( &guid, p_peek );
111     if( !ASF_CmpGUID( &guid, &asf_object_header_guid ) )
112     {
113         msg_Warn( p_demux, "ASF plugin discarded (not a valid file)" );
114         return VLC_EGENERIC;
115     }
116
117     /* Set p_demux fields */
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     memset( p_sys, 0, sizeof( demux_sys_t ) );
122
123     /* Load the headers */
124     if( DemuxInit( p_demux ) )
125     {
126         return VLC_EGENERIC;
127     }
128     return VLC_SUCCESS;
129 }
130
131
132 /*****************************************************************************
133  * Demux: read packet and send them to decoders
134  *****************************************************************************/
135 static int Demux( demux_t *p_demux )
136 {
137     demux_sys_t *p_sys = p_demux->p_sys;
138
139     for( ;; )
140     {
141         uint8_t *p_peek;
142         mtime_t i_length;
143         mtime_t i_time_begin = GetMoviePTS( p_sys );
144         int i_result;
145
146         if( p_demux->b_die )
147         {
148             break;
149         }
150
151         /* Check if we have concatenated files */
152         if( stream_Peek( p_demux->s, &p_peek, 16 ) == 16 )
153         {
154             guid_t guid;
155
156             ASF_GetGUID( &guid, p_peek );
157             if( ASF_CmpGUID( &guid, &asf_object_header_guid ) )
158             {
159                 msg_Warn( p_demux, "Found a new ASF header" );
160                 /* We end this stream */
161                 DemuxEnd( p_demux );
162
163                 /* And we prepare to read the next one */
164                 if( DemuxInit( p_demux ) )
165                 {
166                     msg_Err( p_demux, "failed to load the new header" );
167                     return 0;
168                 }
169                 continue;
170             }
171         }
172
173         /* Read and demux a packet */
174         if( ( i_result = DemuxPacket( p_demux ) ) <= 0 )
175         {
176             return i_result;
177         }
178         if( i_time_begin == -1 )
179         {
180             i_time_begin = GetMoviePTS( p_sys );
181         }
182         else
183         {
184             i_length = GetMoviePTS( p_sys ) - i_time_begin;
185             if( i_length < 0 || i_length >= 40 * 1000 )
186             {
187                 break;
188             }
189         }
190     }
191
192     /* Set the PCR */
193     p_sys->i_time = GetMoviePTS( p_sys );
194     if( p_sys->i_time >= 0 )
195     {
196         es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time );
197     }
198
199     return 1;
200 }
201
202 /*****************************************************************************
203  * Close: frees unused data
204  *****************************************************************************/
205 static void Close( vlc_object_t * p_this )
206 {
207     demux_t     *p_demux = (demux_t *)p_this;
208
209     DemuxEnd( p_demux );
210
211     free( p_demux->p_sys );
212 }
213
214 /*****************************************************************************
215  * Control:
216  *****************************************************************************/
217 static int Control( demux_t *p_demux, int i_query, va_list args )
218 {
219     demux_sys_t *p_sys = p_demux->p_sys;
220     int64_t     *pi64;
221     int         i;
222     vlc_meta_t **pp_meta;
223
224     switch( i_query )
225     {
226         case DEMUX_SET_TIME:
227             return VLC_EGENERIC;
228
229         case DEMUX_GET_LENGTH:
230             pi64 = (int64_t*)va_arg( args, int64_t * );
231             *pi64 = p_sys->i_length;
232             return VLC_SUCCESS;
233
234         case DEMUX_GET_META:
235             pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
236             *pp_meta = vlc_meta_Duplicate( p_sys->meta );
237             return VLC_SUCCESS;
238
239         case DEMUX_SET_POSITION:
240             p_sys->i_time = -1;
241             for( i = 0; i < 128 ; i++ )
242             {
243                 asf_track_t *tk = p_sys->track[i];
244                 if( tk ) tk->i_time = -1;
245             }
246
247         default:
248             return demux2_vaControlHelper( p_demux->s,
249                                            p_sys->i_data_begin, p_sys->i_data_end,
250                                            p_sys->i_bitrate, p_sys->p_fp->i_min_data_packet_size,
251                                            i_query, args );
252     }
253 }
254
255 /*****************************************************************************
256  *
257  *****************************************************************************/
258 static mtime_t GetMoviePTS( demux_sys_t *p_sys )
259 {
260     mtime_t i_time;
261     int     i;
262
263     i_time = -1;
264     for( i = 0; i < 128 ; i++ )
265     {
266         asf_track_t *tk = p_sys->track[i];
267
268         if( tk && tk->p_es && tk->i_time > 0)
269         {
270             if( i_time < 0 )
271             {
272                 i_time = tk->i_time;
273             }
274             else
275             {
276                 i_time = __MIN( i_time, tk->i_time );
277             }
278         }
279     }
280
281     return i_time;
282 }
283
284 #define GETVALUE2b( bits, var, def ) \
285     switch( (bits)&0x03 ) \
286     { \
287         case 1: var = p_peek[i_skip]; i_skip++; break; \
288         case 2: var = GetWLE( p_peek + i_skip );  i_skip+= 2; break; \
289         case 3: var = GetDWLE( p_peek + i_skip ); i_skip+= 4; break; \
290         case 0: \
291         default: var = def; break;\
292     }
293
294 static int DemuxPacket( demux_t *p_demux )
295 {
296     demux_sys_t *p_sys = p_demux->p_sys;
297     int         i_data_packet_min = p_sys->p_fp->i_min_data_packet_size;
298     uint8_t     *p_peek;
299     int         i_skip;
300
301     int         i_packet_size_left;
302     int         i_packet_flags;
303     int         i_packet_property;
304
305     int         b_packet_multiple_payload;
306     int         i_packet_length;
307     int         i_packet_sequence;
308     int         i_packet_padding_length;
309
310     uint32_t    i_packet_send_time;
311     uint16_t    i_packet_duration;
312     int         i_payload;
313     int         i_payload_count;
314     int         i_payload_length_type;
315
316
317     if( stream_Peek( p_demux->s, &p_peek,i_data_packet_min)<i_data_packet_min )
318     {
319         msg_Warn( p_demux, "cannot peek while getting new packet, EOF ?" );
320         return 0;
321     }
322     i_skip = 0;
323
324     /* *** parse error correction if present *** */
325     if( p_peek[0]&0x80 )
326     {
327         unsigned int i_error_correction_length_type;
328         unsigned int i_error_correction_data_length;
329         unsigned int i_opaque_data_present;
330
331         i_error_correction_data_length = p_peek[0] & 0x0f;  // 4bits
332         i_opaque_data_present = ( p_peek[0] >> 4 )& 0x01;    // 1bit
333         i_error_correction_length_type = ( p_peek[0] >> 5 ) & 0x03; // 2bits
334         i_skip += 1; // skip error correction flags
335
336         if( i_error_correction_length_type != 0x00 ||
337             i_opaque_data_present != 0 ||
338             i_error_correction_data_length != 0x02 )
339         {
340             goto loop_error_recovery;
341         }
342
343         i_skip += i_error_correction_data_length;
344     }
345     else
346     {
347         msg_Warn( p_demux, "p_peek[0]&0x80 != 0x80" );
348     }
349
350     /* sanity check */
351     if( i_skip + 2 >= i_data_packet_min )
352     {
353         goto loop_error_recovery;
354     }
355
356     i_packet_flags = p_peek[i_skip]; i_skip++;
357     i_packet_property = p_peek[i_skip]; i_skip++;
358
359     b_packet_multiple_payload = i_packet_flags&0x01;
360
361     /* read some value */
362     GETVALUE2b( i_packet_flags >> 5, i_packet_length, i_data_packet_min );
363     GETVALUE2b( i_packet_flags >> 1, i_packet_sequence, 0 );
364     GETVALUE2b( i_packet_flags >> 3, i_packet_padding_length, 0 );
365
366     i_packet_send_time = GetDWLE( p_peek + i_skip ); i_skip += 4;
367     i_packet_duration  = GetWLE( p_peek + i_skip ); i_skip += 2;
368
369 //        i_packet_size_left = i_packet_length;   // XXX données reellement lu
370     /* FIXME I have to do that for some file, I don't known why */
371     i_packet_size_left = i_data_packet_min;
372
373     if( b_packet_multiple_payload )
374     {
375         i_payload_count = p_peek[i_skip] & 0x3f;
376         i_payload_length_type = ( p_peek[i_skip] >> 6 )&0x03;
377         i_skip++;
378     }
379     else
380     {
381         i_payload_count = 1;
382         i_payload_length_type = 0x02; // unused
383     }
384
385     for( i_payload = 0; i_payload < i_payload_count ; i_payload++ )
386     {
387         asf_track_t   *tk;
388
389         int i_stream_number;
390         int i_media_object_number;
391         int i_media_object_offset;
392         int i_replicated_data_length;
393         int i_payload_data_length;
394         int i_payload_data_pos;
395         int i_sub_payload_data_length;
396         int i_tmp;
397
398         mtime_t i_pts;
399         mtime_t i_pts_delta;
400
401         if( i_skip >= i_packet_size_left )
402         {
403             /* prevent some segfault with invalid file */
404             break;
405         }
406
407         i_stream_number = p_peek[i_skip] & 0x7f;
408         i_skip++;
409
410         GETVALUE2b( i_packet_property >> 4, i_media_object_number, 0 );
411         GETVALUE2b( i_packet_property >> 2, i_tmp, 0 );
412         GETVALUE2b( i_packet_property, i_replicated_data_length, 0 );
413
414         if( i_replicated_data_length > 1 ) // should be at least 8 bytes
415         {
416             i_pts = (mtime_t)GetDWLE( p_peek + i_skip + 4 ) * 1000;
417             i_skip += i_replicated_data_length;
418             i_pts_delta = 0;
419
420             i_media_object_offset = i_tmp;
421
422             if( i_skip >= i_packet_size_left )
423             {
424                 break;
425             }
426         }
427         else if( i_replicated_data_length == 1 )
428         {
429
430             msg_Dbg( p_demux, "found compressed payload" );
431
432             i_pts = (mtime_t)i_tmp * 1000;
433             i_pts_delta = (mtime_t)p_peek[i_skip] * 1000; i_skip++;
434
435             i_media_object_offset = 0;
436         }
437         else
438         {
439             i_pts = (mtime_t)i_packet_send_time * 1000;
440             i_pts_delta = 0;
441
442             i_media_object_offset = i_tmp;
443         }
444
445         i_pts = __MAX( i_pts - p_sys->p_fp->i_preroll * 1000, 0 );
446         if( b_packet_multiple_payload )
447         {
448             GETVALUE2b( i_payload_length_type, i_payload_data_length, 0 );
449         }
450         else
451         {
452             i_payload_data_length = i_packet_length -
453                                         i_packet_padding_length - i_skip;
454         }
455
456         if( i_payload_data_length < 0 || i_skip + i_payload_data_length > i_packet_size_left )
457         {
458             break;
459         }
460 #if 0
461          msg_Dbg( p_demux,
462                   "payload(%d/%d) stream_number:%d media_object_number:%d media_object_offset:%d replicated_data_length:%d payload_data_length %d",
463                   i_payload + 1, i_payload_count, i_stream_number, i_media_object_number,
464                   i_media_object_offset, i_replicated_data_length, i_payload_data_length );
465 #endif
466
467         if( ( tk = p_sys->track[i_stream_number] ) == NULL )
468         {
469             msg_Warn( p_demux,
470                       "undeclared stream[Id 0x%x]", i_stream_number );
471             i_skip += i_payload_data_length;
472             continue;   // over payload
473         }
474
475         if( !tk->p_es )
476         {
477             i_skip += i_payload_data_length;
478             continue;
479         }
480
481
482         for( i_payload_data_pos = 0;
483              i_payload_data_pos < i_payload_data_length &&
484                     i_packet_size_left > 0;
485              i_payload_data_pos += i_sub_payload_data_length )
486         {
487             block_t *p_frag;
488             int i_read;
489
490             // read sub payload length
491             if( i_replicated_data_length == 1 )
492             {
493                 i_sub_payload_data_length = p_peek[i_skip]; i_skip++;
494                 i_payload_data_pos++;
495             }
496             else
497             {
498                 i_sub_payload_data_length = i_payload_data_length;
499             }
500
501             /* FIXME I don't use i_media_object_number, sould I ? */
502             if( tk->p_frame && i_media_object_offset == 0 )
503             {
504                 /* send complete packet to decoder */
505                 block_t *p_gather = block_ChainGather( tk->p_frame );
506
507                 es_out_Send( p_demux->out, tk->p_es, p_gather );
508
509                 tk->p_frame = NULL;
510             }
511
512             i_read = i_sub_payload_data_length + i_skip;
513             if( ( p_frag = stream_Block( p_demux->s, i_read ) ) == NULL )
514             {
515                 msg_Warn( p_demux, "cannot read data" );
516                 return 0;
517             }
518             i_packet_size_left -= i_read;
519
520             p_frag->p_buffer += i_skip;
521             p_frag->i_buffer -= i_skip;
522
523             if( tk->p_frame == NULL )
524             {
525                 tk->i_time =
526                     ( (mtime_t)i_pts + i_payload * (mtime_t)i_pts_delta );
527
528                 p_frag->i_pts = tk->i_time;
529
530                 if( tk->i_cat != VIDEO_ES )
531                     p_frag->i_dts = p_frag->i_pts;
532                 else
533                 {
534                     p_frag->i_dts = p_frag->i_pts;
535                     p_frag->i_pts = 0;
536                 }
537             }
538
539             block_ChainAppend( &tk->p_frame, p_frag );
540
541             i_skip = 0;
542             if( i_packet_size_left > 0 )
543             {
544                 if( stream_Peek( p_demux->s, &p_peek, i_packet_size_left )
545                                                          < i_packet_size_left )
546                 {
547                     msg_Warn( p_demux, "cannot peek, EOF ?" );
548                     return 0;
549                 }
550             }
551         }
552     }
553
554     if( i_packet_size_left > 0 )
555     {
556         if( stream_Read( p_demux->s, NULL, i_packet_size_left )
557                                                          < i_packet_size_left )
558         {
559             msg_Warn( p_demux, "cannot skip data, EOF ?" );
560             return 0;
561         }
562     }
563
564     return 1;
565
566 loop_error_recovery:
567     msg_Warn( p_demux, "unsupported packet header" );
568     if( p_sys->p_fp->i_min_data_packet_size != p_sys->p_fp->i_max_data_packet_size )
569     {
570         msg_Err( p_demux, "unsupported packet header, fatal error" );
571         return -1;
572     }
573     stream_Read( p_demux->s, NULL, i_data_packet_min );
574
575     return 1;
576 }
577
578 /*****************************************************************************
579  *
580  *****************************************************************************/
581 static int DemuxInit( demux_t *p_demux )
582 {
583     demux_sys_t *p_sys = p_demux->p_sys;
584     vlc_bool_t  b_seekable;
585     int         i;
586
587     unsigned int    i_stream;
588     asf_object_content_description_t *p_cd;
589
590     /* init context */
591     p_sys->i_time   = -1;
592     p_sys->i_length = 0;
593     p_sys->i_bitrate = 0;
594     p_sys->p_root   = NULL;
595     p_sys->p_fp     = NULL;
596     p_sys->i_track  = 0;
597     for( i = 0; i < 128; i++ )
598     {
599         p_sys->track[i] = NULL;
600     }
601     p_sys->i_data_begin = -1;
602     p_sys->i_data_end   = -1;
603     p_sys->meta         = NULL;
604
605     /* Now load all object ( except raw data ) */
606     stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &b_seekable );
607     if( (p_sys->p_root = ASF_ReadObjectRoot( p_demux->s, b_seekable )) == NULL )
608     {
609         msg_Warn( p_demux, "ASF plugin discarded (not a valid file)" );
610         return VLC_EGENERIC;
611     }
612     p_sys->p_fp = p_sys->p_root->p_fp;
613
614     if( p_sys->p_fp->i_min_data_packet_size != p_sys->p_fp->i_max_data_packet_size )
615     {
616         msg_Warn( p_demux, "ASF plugin discarded (invalid file_properties object)" );
617         goto error;
618     }
619
620     p_sys->i_track = ASF_CountObject( p_sys->p_root->p_hdr,
621                                       &asf_object_stream_properties_guid );
622     if( p_sys->i_track <= 0 )
623     {
624         msg_Warn( p_demux, "ASF plugin discarded (cannot find any stream!)" );
625         goto error;
626     }
627
628     msg_Dbg( p_demux, "found %d streams", p_sys->i_track );
629
630     for( i_stream = 0; i_stream < p_sys->i_track; i_stream ++ )
631     {
632         asf_track_t    *tk;
633         asf_object_stream_properties_t *p_sp;
634
635         p_sp = ASF_FindObject( p_sys->p_root->p_hdr,
636                                &asf_object_stream_properties_guid,
637                                i_stream );
638
639         tk = p_sys->track[p_sp->i_stream_number] = malloc( sizeof( asf_track_t ) );
640         memset( tk, 0, sizeof( asf_track_t ) );
641
642         tk->i_time = -1;
643         tk->p_sp = p_sp;
644         tk->p_es = NULL;
645         tk->p_frame = NULL;
646
647         if( ASF_CmpGUID( &p_sp->i_stream_type, &asf_object_stream_type_audio ) &&
648             p_sp->i_type_specific_data_length >= sizeof( WAVEFORMATEX ) - 2 )
649         {
650             es_format_t  fmt;
651             uint8_t      *p_data = p_sp->p_type_specific_data;
652
653             es_format_Init( &fmt, AUDIO_ES, 0 );
654             wf_tag_to_fourcc( GetWLE( &p_data[0] ), &fmt.i_codec, NULL );
655             fmt.audio.i_channels        = GetWLE(  &p_data[2] );
656             fmt.audio.i_rate      = GetDWLE( &p_data[4] );
657             fmt.i_bitrate         = GetDWLE( &p_data[8] ) * 8;
658             fmt.audio.i_blockalign      = GetWLE(  &p_data[12] );
659             fmt.audio.i_bitspersample   = GetWLE(  &p_data[14] );
660
661             if( p_sp->i_type_specific_data_length > sizeof( WAVEFORMATEX ) )
662             {
663                 fmt.i_extra = __MIN( GetWLE( &p_data[16] ),
664                                      p_sp->i_type_specific_data_length - sizeof( WAVEFORMATEX ) );
665                 fmt.p_extra = malloc( fmt.i_extra );
666                 memcpy( fmt.p_extra, &p_data[sizeof( WAVEFORMATEX )], fmt.i_extra );
667             }
668
669             tk->i_cat = AUDIO_ES;
670             tk->p_es = es_out_Add( p_demux->out, &fmt );
671             es_format_Clean( &fmt );
672
673             msg_Dbg( p_demux, "added new audio stream(codec:0x%x,ID:%d)",
674                     GetWLE( p_data ), p_sp->i_stream_number );
675         }
676         else if( ASF_CmpGUID( &p_sp->i_stream_type, &asf_object_stream_type_video ) &&
677                  p_sp->i_type_specific_data_length >= 11 + sizeof( BITMAPINFOHEADER ) )
678         {
679             es_format_t  fmt;
680             uint8_t      *p_data = &p_sp->p_type_specific_data[11];
681
682             es_format_Init( &fmt, VIDEO_ES,
683                             VLC_FOURCC( p_data[16], p_data[17], p_data[18], p_data[19] ) );
684             fmt.video.i_width = GetDWLE( p_data + 4 );
685             fmt.video.i_height= GetDWLE( p_data + 8 );
686
687             if( p_sp->i_type_specific_data_length > 11 + sizeof( BITMAPINFOHEADER ) )
688             {
689                 fmt.i_extra = __MIN( GetDWLE( p_data ),
690                                      p_sp->i_type_specific_data_length - 11 - sizeof( BITMAPINFOHEADER ) );
691                 fmt.p_extra = malloc( fmt.i_extra );
692                 memcpy( fmt.p_extra, &p_data[sizeof( BITMAPINFOHEADER )], fmt.i_extra );
693             }
694
695             tk->i_cat = VIDEO_ES;
696             tk->p_es = es_out_Add( p_demux->out, &fmt );
697             es_format_Clean( &fmt );
698
699             msg_Dbg( p_demux, "added new video stream(ID:%d)",
700                      p_sp->i_stream_number );
701         }
702         else
703         {
704             tk->i_cat = UNKNOWN_ES;
705             msg_Dbg( p_demux, "ignoring unknown stream(ID:%d)",
706                      p_sp->i_stream_number );
707         }
708     }
709
710     p_sys->i_data_begin = p_sys->p_root->p_data->i_object_pos + 50;
711     if( p_sys->p_root->p_data->i_object_size != 0 )
712     { /* local file */
713         p_sys->i_data_end = p_sys->p_root->p_data->i_object_pos +
714                                     p_sys->p_root->p_data->i_object_size;
715     }
716     else
717     { /* live/broacast */
718         p_sys->i_data_end = -1;
719     }
720
721
722     /* go to first packet */
723     stream_Seek( p_demux->s, p_sys->i_data_begin );
724
725     /* try to calculate movie time */
726     if( p_sys->p_fp->i_data_packets_count > 0 )
727     {
728         int64_t i_count;
729         int64_t i_size = stream_Size( p_demux->s );
730
731         if( p_sys->i_data_end > 0 && i_size > p_sys->i_data_end )
732         {
733             i_size = p_sys->i_data_end;
734         }
735
736         /* real number of packets */
737         i_count = ( i_size - p_sys->i_data_begin ) /
738                   p_sys->p_fp->i_min_data_packet_size;
739
740         /* calculate the time duration in micro-s */
741         p_sys->i_length = (mtime_t)p_sys->p_fp->i_play_duration / 10 *
742                    (mtime_t)i_count /
743                    (mtime_t)p_sys->p_fp->i_data_packets_count;
744
745         if( p_sys->i_length > 0 )
746         {
747             p_sys->i_bitrate = 8 * i_size * (int64_t)1000000 / p_sys->i_length;
748         }
749     }
750
751     /* Create meta information */
752     p_sys->meta = vlc_meta_New();
753
754     if( ( p_cd = ASF_FindObject( p_sys->p_root->p_hdr,
755                                  &asf_object_content_description_guid, 0 ) ) )
756     {
757         if( p_cd->psz_title && *p_cd->psz_title )
758         {
759             vlc_meta_Add( p_sys->meta, VLC_META_TITLE, p_cd->psz_title );
760         }
761         if( p_cd->psz_author && *p_cd->psz_author )
762         {
763              vlc_meta_Add( p_sys->meta, VLC_META_AUTHOR, p_cd->psz_author );
764         }
765         if( p_cd->psz_copyright && *p_cd->psz_copyright )
766         {
767             vlc_meta_Add( p_sys->meta, VLC_META_COPYRIGHT, p_cd->psz_copyright );
768         }
769         if( p_cd->psz_description && *p_cd->psz_description )
770         {
771             vlc_meta_Add( p_sys->meta, VLC_META_DESCRIPTION, p_cd->psz_description );
772         }
773         if( p_cd->psz_rating && *p_cd->psz_rating )
774         {
775             vlc_meta_Add( p_sys->meta, VLC_META_RATING, p_cd->psz_rating );
776         }
777     }
778     for( i_stream = 0, i = 0; i < 128; i++ )
779     {
780         asf_object_codec_list_t *p_cl = ASF_FindObject( p_sys->p_root->p_hdr,
781                                                         &asf_object_codec_list_guid, 0 );
782
783         if( p_sys->track[i] )
784         {
785             vlc_meta_t *tk = vlc_meta_New();
786             TAB_APPEND( p_sys->meta->i_track, p_sys->meta->track, tk );
787
788             if( p_cl && i_stream < p_cl->i_codec_entries_count )
789             {
790                 if( p_cl->codec[i_stream].psz_name &&
791                     *p_cl->codec[i_stream].psz_name )
792                 {
793                     vlc_meta_Add( tk, VLC_META_CODEC_NAME,
794                                   p_cl->codec[i_stream].psz_name );
795                 }
796                 if( p_cl->codec[i_stream].psz_description &&
797                     *p_cl->codec[i_stream].psz_description )
798                 {
799                     vlc_meta_Add( tk, VLC_META_CODEC_DESCRIPTION,
800                                   p_cl->codec[i_stream].psz_description );
801                 }
802             }
803             i_stream++;
804         }
805     }
806
807     es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
808     return VLC_SUCCESS;
809
810 error:
811     ASF_FreeObjectRoot( p_demux->s, p_sys->p_root );
812     return VLC_EGENERIC;
813 }
814 /*****************************************************************************
815  *
816  *****************************************************************************/
817 static void DemuxEnd( demux_t *p_demux )
818 {
819     demux_sys_t *p_sys = p_demux->p_sys;
820     int         i;
821
822     if( p_sys->p_root )
823     {
824         ASF_FreeObjectRoot( p_demux->s, p_sys->p_root );
825         p_sys->p_root = NULL;
826     }
827     if( p_sys->meta )
828     {
829         vlc_meta_Delete( p_sys->meta );
830         p_sys->meta = NULL;
831     }
832
833     for( i = 0; i < 128; i++ )
834     {
835         asf_track_t *tk = p_sys->track[i];
836
837         if( tk )
838         {
839             if( tk->p_frame )
840             {
841                 block_ChainRelease( tk->p_frame );
842             }
843             if( tk->p_es )
844             {
845                 es_out_Del( p_demux->out, tk->p_es );
846             }
847             free( tk );
848         }
849         p_sys->track[i] = 0;
850     }
851 }
852