42aec93c1ec52af2dbc01b437767edeb66ad282d
[vlc.git] / modules / demux / asf / asf.c
1 /*****************************************************************************
2  * asf.c : ASFv01 file input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2001 VideoLAN
5  * $Id: asf.c,v 1.1 2002/10/20 17:22:33 fenrir Exp $
6  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7  * 
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  * 
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
21  *****************************************************************************/
22
23 /*****************************************************************************
24  * Preamble
25  *****************************************************************************/
26 #include <stdlib.h>                                      /* malloc(), free() */
27 #include <string.h>                                              /* strdup() */
28 #include <errno.h>
29 #include <sys/types.h>
30
31 #include <vlc/vlc.h>
32 #include <vlc/input.h>
33
34 #include "libasf.h"
35 #include "asf.h"
36
37 /*****************************************************************************
38  * Local prototypes
39  *****************************************************************************/
40 static int    Activate   ( vlc_object_t * );
41 static void   Deactivate ( vlc_object_t * );
42 static int    Demux      ( input_thread_t * );
43
44 /*****************************************************************************
45  * Module descriptor
46  *****************************************************************************/
47 vlc_module_begin();
48     set_description( "ASF v1.0 demuxer (file only)" );
49     set_capability( "demux", 200 );
50     set_callbacks( Activate, Deactivate );
51     add_shortcut( "asf" );
52 vlc_module_end();
53
54 static u16 GetWLE( u8 *p_buff )
55 {
56     return( (p_buff[0]) + ( p_buff[1] <<8 ) );
57 }
58 static u32 GetDWLE( u8 *p_buff )
59 {   
60     return( p_buff[0] + ( p_buff[1] <<8 ) +
61             ( p_buff[2] <<16 ) + ( p_buff[3] <<24 ) );
62 }
63
64 /*****************************************************************************
65  * Activate: check file and initializes ASF structures
66  *****************************************************************************/
67 static int Activate( vlc_object_t * p_this )
68 {   
69     input_thread_t *p_input = (input_thread_t *)p_this;
70     u8  *p_peek;
71     guid_t guid;
72     
73     demux_sys_t *p_demux;
74     int i_stream;
75     
76     /* Initialize access plug-in structures. */
77     if( p_input->i_mtu == 0 )
78     {
79         /* Improve speed. */
80         p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE ;
81     }
82
83     p_input->pf_demux = Demux;
84
85     /* a little test to see if it could be a asf stream */
86     if( input_Peek( p_input, &p_peek, 16 ) < 8 )
87     {
88         msg_Warn( p_input, "ASF v1.0 plugin discarded (cannot peek)" );
89         return( -1 );
90     }
91     GetGUID( &guid, p_peek );
92     if( !CmpGUID( &guid, &asf_object_header_guid ) )
93     {
94         msg_Warn( p_input, "ASF v1.0 plugin discarded (not a valid file)" );
95         return( -1 );
96     }
97
98     /* create our structure that will contains all data */
99     if( !( p_input->p_demux_data = 
100                 p_demux = malloc( sizeof( demux_sys_t ) ) ) )
101     {
102         msg_Err( p_input, "out of memory" );
103         return( -1 );
104     }
105     memset( p_demux, 0, sizeof( demux_sys_t ) );
106     p_input->p_demux_data = p_demux;
107        
108
109     /* Now load all object ( except raw data ) */
110     if( !ASF_ReadObjectRoot( p_input, &p_demux->root, p_input->stream.b_seekable ) )
111     {
112         msg_Warn( p_input, "ASF v1.0 plugin discarded (not a valid file)" );
113         free( p_demux );
114         return( -1 );
115     }
116     /* Check if we have found all mandatory asf object */
117     if( !p_demux->root.p_hdr || !p_demux->root.p_data )
118     {
119         ASF_FreeObjectRoot( p_input, &p_demux->root );
120         free( p_demux );
121         msg_Warn( p_input, "ASF v1.0 plugin discarded (not a valid file)" );
122         return( -1 );
123     }
124    
125     if( !( p_demux->p_fp = ASF_FindObject( (asf_object_t*)p_demux->root.p_hdr,
126                                     &asf_object_file_properties_guid, 0 ) ) )
127     {
128         ASF_FreeObjectRoot( p_input, &p_demux->root );
129         free( p_demux );
130         msg_Warn( p_input, "ASF v1.0 plugin discarded (missing file_properties object)" );
131         return( -1 );
132     }
133     
134     if( p_demux->p_fp->i_min_data_packet_size != p_demux->p_fp->i_max_data_packet_size )
135     {
136         ASF_FreeObjectRoot( p_input, &p_demux->root );
137         free( p_demux );
138         msg_Warn( p_input, "ASF v1.0 plugin discarded (invalid file_properties object)" );
139         return( -1 );
140     }
141     
142     p_demux->i_streams = ASF_CountObject( (asf_object_t*)p_demux->root.p_hdr, 
143                                           &asf_object_stream_properties_guid );
144     if( !p_demux->i_streams )
145     {
146         ASF_FreeObjectRoot( p_input, &p_demux->root );
147         free( p_demux );
148         msg_Warn( p_input, "ASF plugin discarded (cannot find any stream!)" );
149         return( -1 );
150     }
151     else
152     {
153         msg_Dbg( p_input, "found %d streams", p_demux->i_streams );
154     }
155
156     /*  create one program */
157     vlc_mutex_lock( &p_input->stream.stream_lock );
158     if( input_InitStream( p_input, 0 ) == -1)
159     {
160         vlc_mutex_unlock( &p_input->stream.stream_lock );
161         msg_Err( p_input, "cannot init stream" );
162         return( -1 );
163     }
164     if( input_AddProgram( p_input, 0, 0) == NULL )
165     {
166         vlc_mutex_unlock( &p_input->stream.stream_lock );
167         msg_Err( p_input, "cannot add program" );
168         return( -1 );
169     }
170     p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
171     p_input->stream.i_mux_rate = 0 ; /* FIXME */
172     vlc_mutex_unlock( &p_input->stream.stream_lock );
173
174     for( i_stream = 0; i_stream < p_demux->i_streams; i_stream ++ )
175     {
176         asf_stream_t    *p_stream;
177         asf_object_stream_properties_t *p_sp;
178         
179         p_sp = (asf_object_stream_properties_t*)
180                     ASF_FindObject( (asf_object_t*)p_demux->root.p_hdr,
181                                     &asf_object_stream_properties_guid,
182                                     i_stream );
183
184         p_stream = p_demux->stream[p_sp->i_stream_number] = malloc( sizeof( asf_stream_t ) );
185         memset( p_stream, 0, sizeof( asf_stream_t ) );
186         p_stream->p_sp = p_sp;
187
188         vlc_mutex_lock( &p_input->stream.stream_lock );
189         p_stream->p_es = 
190             input_AddES( p_input,
191                          p_input->stream.p_selected_program,
192                          p_sp->i_stream_number,
193                          p_sp->i_type_specific_data_length );
194         if( p_sp->i_type_specific_data_length > 11 )
195         {
196             memcpy( p_stream->p_es->p_demux_data,
197                     p_sp->p_type_specific_data + 11,
198                     p_sp->i_type_specific_data_length - 11 );
199         }
200
201         vlc_mutex_unlock( &p_input->stream.stream_lock );
202         if( CmpGUID( &p_sp->i_stream_type, &asf_object_stream_type_audio ) )
203         {
204             int i_codec;
205             if( p_sp->p_type_specific_data )
206             {
207                 i_codec = GetWLE( p_sp->p_type_specific_data );
208             }
209             else
210             {
211                 i_codec = -1;
212             }
213
214             p_stream->i_cat = AUDIO_ES;
215             msg_Dbg( p_input,
216                     "adding new audio stream(codec:0x%x,ID:%d)", 
217                     i_codec,
218                     p_sp->i_stream_number );
219             switch( i_codec )
220             {
221                 case( 0x50 ):
222                 case( 0x55 ):
223                     p_stream->p_es->i_fourcc = 
224                         VLC_FOURCC( 'm','p','g','a' );
225                     break;
226                 case( 0x2000 ):
227                     p_stream->p_es->i_fourcc = 
228                         VLC_FOURCC( 'a','5','2',' ' );
229                     break;
230                 case( 0x160 ):
231                 case( 0x161 ):
232                     p_stream->p_es->i_fourcc = 
233                         VLC_FOURCC( 'w','m','a',' ' );
234                     break;
235                 default:
236                     p_stream->p_es->i_fourcc = 
237                         VLC_FOURCC( 'u','n','d','f' );
238             }
239             
240         }
241         else
242         if( CmpGUID( &p_sp->i_stream_type, &asf_object_stream_type_video ) )
243         {
244             p_stream->i_cat = VIDEO_ES;
245             msg_Dbg( p_input,
246                     "adding new video stream(ID:%d)", 
247                     p_sp->i_stream_number );
248             if( p_sp->p_type_specific_data )
249             {
250                 p_stream->p_es->i_fourcc =
251                     GetDWLE( p_sp->p_type_specific_data + 27 );
252             }
253             else
254             {
255                 p_stream->p_es->i_fourcc = 
256                     VLC_FOURCC( 'u','n','d','f' );
257             }
258
259         }
260         else
261         {
262             p_stream->i_cat = UNKNOWN_ES;
263             msg_Dbg( p_input, 
264                     "ignoring unknown stream(ID:%d)", 
265                     p_sp->i_stream_number );
266             p_stream->p_es->i_fourcc = VLC_FOURCC( 'u','n','d','f' );
267         }
268         p_stream->p_es->i_cat = p_stream->i_cat;
269
270         vlc_mutex_lock( &p_input->stream.stream_lock );
271         input_SelectES( p_input, p_stream->p_es );
272         vlc_mutex_unlock( &p_input->stream.stream_lock );
273     }
274     
275     vlc_mutex_lock( &p_input->stream.stream_lock );
276     p_input->stream.p_selected_program->b_is_ok = 1;
277     vlc_mutex_unlock( &p_input->stream.stream_lock );
278
279     // go to first packet
280     p_demux->i_data_begin = p_demux->root.p_data->i_object_pos + 50;
281     if( p_demux->root.p_data->i_object_size != 0 )
282     { // local file
283         p_demux->i_data_end = p_demux->root.p_data->i_object_pos +
284                                     p_demux->root.p_data->i_object_size;
285     }
286     else
287     { // live/broacast
288         p_demux->i_data_end = -1;
289     }
290     ASF_SeekAbsolute( p_input, p_demux->i_data_begin );
291     return( 0 );
292 }
293
294 /*****************************************************************************
295  * Demux: read packet and send them to decoders 
296  *****************************************************************************/
297 #define GETVALUE2b( bits, var, def ) \
298     switch( bits ) \
299     { \
300         case 1: var = p_peek[i_skip]; i_skip++; break; \
301         case 2: var = GetWLE( p_peek + i_skip );  i_skip+= 2; break; \
302         case 3: var = GetDWLE( p_peek + i_skip ); i_skip+= 4; break; \
303         case 0: \
304         default: var = def; break;\
305     }
306
307 static int Demux( input_thread_t *p_input )
308 {
309     demux_sys_t *p_demux = p_input->p_demux_data;
310     int i;
311
312     /* catch seek from user */
313     if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
314     {
315         off_t i_offset;
316         i_offset = ASF_TellAbsolute( p_input ) - p_demux->i_data_begin;
317
318         if( i_offset  < 0 )
319         {
320             i_offset = 0;
321         }
322         /* XXX work only when i_min_data_packet_size == i_max_data_packet_size */
323         i_offset -= i_offset % p_demux->p_fp->i_min_data_packet_size;
324         ASF_SeekAbsolute( p_input, p_demux->i_data_begin + i_offset );
325
326         p_demux->i_time = 0;
327         for( i = 0; i < 127 ; i++ )
328         {
329 #define p_stream p_demux->stream[i]
330             if( p_stream )
331             {
332                 p_stream->i_time = 0;
333             }
334 #undef p_stream
335         }
336
337     }
338     /* first wait for the good time to read a packet */
339
340     input_ClockManageRef( p_input,
341                           p_input->stream.p_selected_program,
342                           p_demux->i_pcr );
343
344     /* update pcr XXX in mpeg scale so in 90000 unit/s */
345     p_demux->i_pcr = p_demux->i_time * 9 / 100;
346     
347 //    /* we will read 100ms for each stream so */
348 //    p_demux->i_time += 100 * 1000;
349
350     for( i = 0; i < 10; i++ ) // parse 10 packets
351     {
352         int i_data_packet_min = p_demux->p_fp->i_min_data_packet_size;
353         u8  *p_peek;
354         int i_skip;
355         
356         int i_packet_size_left;
357         int i_packet_flags;
358         int i_packet_property;
359
360         int b_packet_multiple_payload;
361         int i_packet_length;
362         int i_packet_sequence;
363         int i_packet_padding_length;
364         
365         u32 i_packet_send_time;
366         u16 i_packet_duration;
367         int i_payload;
368         int i_payload_count;
369         int i_payload_length_type;
370
371         
372         if( input_Peek( p_input, &p_peek, i_data_packet_min ) < i_data_packet_min )
373         {
374             // EOF ?
375             msg_Err( p_input, "cannot peek while getting new packet, EOF ?" );
376             return( 0 );
377         }
378         i_skip = 0;
379         
380         /* *** parse error correction if present *** */
381         if( p_peek[0]&0x80 )
382         {
383             int i_error_correction_length_type;
384             int i_error_correction_data_length;
385             int i_opaque_data_present;
386
387             i_error_correction_data_length = p_peek[0] & 0x0f;  // 4bits
388             i_opaque_data_present = ( p_peek[0] >> 4 )& 0x01;    // 1bit
389             i_error_correction_length_type = ( p_peek[0] >> 5 ) & 0x03; // 2bits
390             i_skip += 1; // skip error correction flags
391         
392             if( i_error_correction_length_type != 0x00 ||
393                 i_opaque_data_present != 0 ||
394                 i_error_correction_data_length != 0x02 )
395             {
396                 goto loop_error_recovery;
397             }
398
399             i_skip += i_error_correction_data_length;
400         }
401         else
402         {
403             msg_Warn( p_input, "p_peek[0]&0x80 != 0x80" );
404         }
405
406         i_packet_flags = p_peek[i_skip]; i_skip++;
407         i_packet_property = p_peek[i_skip]; i_skip++;
408         
409         b_packet_multiple_payload = i_packet_flags&0x01;
410
411         /* read some value */
412         GETVALUE2b( ( i_packet_flags >> 5 )&0x3, i_packet_length, i_data_packet_min );
413         GETVALUE2b( ( i_packet_flags >> 1 )&0x3, i_packet_sequence, 0 );
414         GETVALUE2b( ( i_packet_flags >> 3 )&0x3, i_packet_padding_length, 0 );
415
416         i_packet_send_time = GetDWLE( p_peek + i_skip ); i_skip += 4;
417         i_packet_duration  = GetWLE( p_peek + i_skip ); i_skip += 2;
418     
419         i_packet_size_left = i_packet_length;   // XXX donnĂ©es reellement lu
420
421         if( b_packet_multiple_payload )
422         {
423             i_payload_count = p_peek[i_skip] & 0x3f;
424             i_payload_length_type = ( p_peek[i_skip] >> 6 )&0x03;
425             i_skip++;
426         }
427         else
428         {
429             i_payload_count = 1;
430             i_payload_length_type = 0x02; // unused
431         }
432
433         for( i_payload = 0; i_payload < i_payload_count ; i_payload++ )
434         {
435             asf_stream_t   *p_stream;
436
437             int i_stream_number;
438             int i_media_object_number;
439             int i_media_object_offset;
440             int i_replicated_data_length;
441             int i_payload_data_length;
442             int i_payload_data_pos;
443             int i_sub_payload_data_length;
444             int i_tmp;
445
446             mtime_t i_pts;
447             mtime_t i_pts_delta;
448
449             i_stream_number = p_peek[i_skip] & 0x7f; 
450             i_skip++;
451             
452             GETVALUE2b( ( i_packet_property >> 4 )&0x03, i_media_object_number, 0 );
453             GETVALUE2b( ( i_packet_property >> 2 )&0x03, i_tmp, 0 );
454             GETVALUE2b( i_packet_property&0x03, i_replicated_data_length, 0 );
455             
456             if( i_replicated_data_length > 1 ) // should be at least 8 bytes
457             {
458                 i_pts = (mtime_t)GetDWLE( p_peek + i_skip + 4 ) * 1000;
459                 i_skip += i_replicated_data_length;
460                 i_pts_delta = 0;
461
462                 i_media_object_offset = i_tmp;
463             }
464             else if( i_replicated_data_length == 1 )
465             {
466
467                 msg_Warn( p_input, "found compressed payload" );
468
469                 i_pts = (mtime_t)i_tmp * 1000;
470                 i_pts_delta = (mtime_t)p_peek[i_skip] * 1000; i_skip++;
471
472                 i_media_object_offset = 0;
473             }
474             else
475             {
476                 i_pts = (mtime_t)i_packet_send_time * 1000;
477                 i_pts_delta = 0;
478
479                 i_media_object_offset = i_tmp;
480             }
481
482
483             if( b_packet_multiple_payload )
484             {
485                 GETVALUE2b( i_payload_length_type, i_payload_data_length, 0 );
486             }
487             else
488             {
489                 msg_Warn( p_input, "single payload" );
490                 i_payload_data_length = i_packet_length - i_packet_padding_length - i_skip;
491             }
492             
493              msg_Dbg( p_input, 
494                       "payload(%d/%d) stream_number:%d media_object_number:%d media_object_offset:%d replicated_data_length:%d payload_data_length %d",
495                       i_payload + 1, 
496                       i_payload_count,
497                       i_stream_number,
498                       i_media_object_number, 
499                       i_media_object_offset, 
500                       i_replicated_data_length, 
501                       i_payload_data_length );
502            
503
504             if( !( p_stream = p_demux->stream[i_stream_number] ) )
505             {
506                 msg_Warn( p_input, "undeclared stream[Id 0x%x]", i_stream_number );
507                 i_skip += i_payload_data_length;
508                 continue;   // over payload
509             }
510
511             if( !p_stream->p_es || !p_stream->p_es->p_decoder_fifo )
512             {
513                 i_skip += i_payload_data_length;
514                 continue;
515             }
516
517
518             for( i_payload_data_pos = 0; i_payload_data_pos < i_payload_data_length; i_payload_data_pos += i_sub_payload_data_length )
519             {
520                 data_packet_t  *p_data;
521                 int i_read;
522                 // read sub payload length
523                 if( i_replicated_data_length == 1 )
524                 {
525                     i_sub_payload_data_length = p_peek[i_skip]; i_skip++;
526                     i_payload_data_pos++;
527                 }
528                 else
529                 {
530                     i_sub_payload_data_length = i_payload_data_length;
531                 }
532
533
534                 if( p_stream->p_pes && i_media_object_offset == 0 ) // I don't use i_media_object_number, sould I ?
535                 {
536                     // send complete packet to decoder
537                     if( p_stream->p_pes->i_pes_size > 0 )
538                     {
539                         input_DecodePES( p_stream->p_es->p_decoder_fifo, p_stream->p_pes );
540                         p_stream->p_pes = NULL;
541                     }
542                 }
543
544                 if( !p_stream->p_pes )  // add a new PES
545                 {
546                     mtime_t i_date;
547                     p_stream->i_time = ( (mtime_t)i_pts + i_payload * (mtime_t)i_pts_delta );
548                     
549                     p_stream->p_pes = input_NewPES( p_input->p_method_data );
550                     p_stream->p_pes->i_dts = 
551                         p_stream->p_pes->i_pts = 
552                             input_ClockGetTS( p_input, 
553                                               p_input->stream.p_selected_program,
554                                               p_stream->i_time * 9 / 100 );
555
556                     p_stream->p_pes->p_next = NULL;
557                     p_stream->p_pes->i_nb_data = 0;
558                     p_stream->p_pes->i_pes_size = 0;
559                 }
560
561                 i_read = i_sub_payload_data_length + i_skip;
562                 if( input_SplitBuffer( p_input, &p_data, i_read ) < i_read )
563                 {
564                     msg_Err( p_input, "cannot read data" );
565                     return( 0 );
566                 }
567                 p_data->p_payload_start += i_skip;
568                 i_packet_size_left -= i_read;
569
570                 
571                 if( !p_stream->p_pes->p_first )
572                 {
573                     p_stream->p_pes->p_first = p_stream->p_pes->p_last = p_data;
574                 }
575                 else
576                 {
577                     p_stream->p_pes->p_last->p_next = p_data;
578                     p_stream->p_pes->p_last = p_data;
579                 }
580                 p_stream->p_pes->i_pes_size += i_sub_payload_data_length;
581                 p_stream->p_pes->i_nb_data++;
582
583                 i_skip = 0;
584                 if( i_packet_size_left <= 0 )
585                 {
586                     break;
587                 }
588                 if( input_Peek( p_input, &p_peek, i_packet_size_left ) < i_packet_size_left )
589                 {
590                     // EOF ?
591                     msg_Err( p_input, "cannot peek, EOF ?" );
592                     return( 0 );
593                 }
594
595
596             }
597             if( i_packet_size_left <= 0 )
598             {
599                 break;
600             }
601         }
602         
603 loop_ok:
604         if( i_packet_size_left > 0 )
605         {
606             if( !ASF_SkipBytes( p_input, i_packet_size_left ) )
607             {
608                 msg_Err( p_input, "cannot skip data, EOF ?" );
609                 return( 0 );
610             }
611         }
612
613
614         continue;
615
616 loop_error_recovery:
617         msg_Warn( p_input, "unsupported packet header" );
618         if( p_demux->p_fp->i_min_data_packet_size != p_demux->p_fp->i_max_data_packet_size )
619         {
620             msg_Err( p_input, "unsupported packet header, fatal error" );
621             return( -1 );
622         }
623         ASF_SkipBytes( p_input, i_data_packet_min );
624
625         continue;
626     }   // loop over packet
627     
628     p_demux->i_time = 0;
629     for( i = 0; i < 127 ; i++ )
630     {
631 #define p_stream p_demux->stream[i]
632         if( p_stream && p_stream->p_es && p_stream->p_es->p_decoder_fifo )
633         {
634             p_demux->i_time = __MAX( p_demux->i_time, p_stream->i_time );
635         }
636 #undef p_stream
637     }
638
639     return( 1 );
640 }
641
642 /*****************************************************************************
643  * MP4End: frees unused data
644  *****************************************************************************/
645 static void Deactivate( vlc_object_t * p_this )
646 {   
647 #define FREE( p ) \
648     if( p ) { free( p ); } 
649    
650     input_thread_t *  p_input = (input_thread_t *)p_this;
651     demux_sys_t *p_demux = p_input->p_demux_data;
652     
653     msg_Dbg( p_input, "Freeing all memory" );
654     ASF_FreeObjectRoot( p_input, &p_demux->root );
655     
656 #undef FREE
657 }
658