]> git.sesse.net Git - vlc/blob - modules/demux/asf/asf.c
all: fix a bug in packet parsing and can now play unseekable stream.
[vlc] / 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.4 2002/11/10 16:31:20 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 ) < 16 )
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
107     /* Now load all object ( except raw data ) */
108     if( !ASF_ReadObjectRoot( p_input, &p_demux->root, p_input->stream.b_seekable ) )
109     {
110         msg_Warn( p_input, "ASF v1.0 plugin discarded (not a valid file)" );
111         free( p_demux );
112         return( -1 );
113     }
114     /* Check if we have found all mandatory asf object */
115     if( !p_demux->root.p_hdr || !p_demux->root.p_data )
116     {
117         ASF_FreeObjectRoot( p_input, &p_demux->root );
118         free( p_demux );
119         msg_Warn( p_input, "ASF v1.0 plugin discarded (not a valid file)" );
120         return( -1 );
121     }
122    
123     if( !( p_demux->p_fp = ASF_FindObject( p_demux->root.p_hdr,
124                                     &asf_object_file_properties_guid, 0 ) ) )
125     {
126         ASF_FreeObjectRoot( p_input, &p_demux->root );
127         free( p_demux );
128         msg_Warn( p_input, "ASF v1.0 plugin discarded (missing file_properties object)" );
129         return( -1 );
130     }
131     
132     if( p_demux->p_fp->i_min_data_packet_size != p_demux->p_fp->i_max_data_packet_size )
133     {
134         ASF_FreeObjectRoot( p_input, &p_demux->root );
135         free( p_demux );
136         msg_Warn( p_input, "ASF v1.0 plugin discarded (invalid file_properties object)" );
137         return( -1 );
138     }
139     
140     p_demux->i_streams = ASF_CountObject( p_demux->root.p_hdr, 
141                                           &asf_object_stream_properties_guid );
142     if( !p_demux->i_streams )
143     {
144         ASF_FreeObjectRoot( p_input, &p_demux->root );
145         free( p_demux );
146         msg_Warn( p_input, "ASF plugin discarded (cannot find any stream!)" );
147         return( -1 );
148     }
149     else
150     {
151         msg_Dbg( p_input, "found %d streams", p_demux->i_streams );
152     }
153
154     /*  create one program */
155     vlc_mutex_lock( &p_input->stream.stream_lock );
156     if( input_InitStream( p_input, 0 ) == -1)
157     {
158         vlc_mutex_unlock( &p_input->stream.stream_lock );
159         msg_Err( p_input, "cannot init stream" );
160         return( -1 );
161     }
162     if( input_AddProgram( p_input, 0, 0) == NULL )
163     {
164         vlc_mutex_unlock( &p_input->stream.stream_lock );
165         msg_Err( p_input, "cannot add program" );
166         return( -1 );
167     }
168     p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
169     p_input->stream.i_mux_rate = 0 ; /* FIXME */
170     vlc_mutex_unlock( &p_input->stream.stream_lock );
171
172     for( i_stream = 0; i_stream < p_demux->i_streams; i_stream ++ )
173     {
174         asf_stream_t    *p_stream;
175         asf_object_stream_properties_t *p_sp;
176         
177         p_sp = ASF_FindObject( p_demux->root.p_hdr,
178                                &asf_object_stream_properties_guid,
179                                i_stream );
180
181         p_stream = 
182             p_demux->stream[p_sp->i_stream_number] = 
183                 malloc( sizeof( asf_stream_t ) );
184         memset( p_stream, 0, sizeof( asf_stream_t ) );
185         p_stream->p_sp = p_sp;
186
187         vlc_mutex_lock( &p_input->stream.stream_lock );
188         p_stream->p_es = 
189             input_AddES( p_input,
190                          p_input->stream.p_selected_program,
191                          p_sp->i_stream_number,
192                          0 );
193
194         vlc_mutex_unlock( &p_input->stream.stream_lock );
195         if( CmpGUID( &p_sp->i_stream_type, &asf_object_stream_type_audio ) )
196         {
197             int i_codec;
198             if( p_sp->p_type_specific_data )
199             {
200                 i_codec = GetWLE( p_sp->p_type_specific_data );
201             }
202             else
203             {
204                 i_codec = -1;
205             }
206
207             p_stream->i_cat = AUDIO_ES;
208             msg_Dbg( p_input,
209                     "adding new audio stream(codec:0x%x,ID:%d)", 
210                     i_codec,
211                     p_sp->i_stream_number );
212             switch( i_codec )
213             {
214                 case( 0x01 ):
215                     p_stream->p_es->i_fourcc = 
216                         VLC_FOURCC( 'a', 'r', 'a', 'w' );
217                     break;
218                 case( 0x50 ):
219                 case( 0x55 ):
220                     p_stream->p_es->i_fourcc = 
221                         VLC_FOURCC( 'm','p','g','a' );
222                     break;
223                 case( 0x2000 ):
224                     p_stream->p_es->i_fourcc = 
225                         VLC_FOURCC( 'a','5','2',' ' );
226                     break;
227                 case( 0x160 ):
228                     p_stream->p_es->i_fourcc = 
229                         VLC_FOURCC( 'w','m','a','1' );
230                     break;
231                 case( 0x161 ):
232                     p_stream->p_es->i_fourcc = 
233                         VLC_FOURCC( 'w','m','a','2' );
234                     break;
235                 default:
236                     p_stream->p_es->i_fourcc = 
237                         VLC_FOURCC( 'u','n','d','f' );
238             }
239             if( p_sp->i_type_specific_data_length > 0 )
240             {
241                 p_stream->p_es->p_demux_data = 
242                     malloc( p_sp->i_type_specific_data_length );
243                 memcpy( p_stream->p_es->p_demux_data,
244                         p_sp->p_type_specific_data,
245                         p_sp->i_type_specific_data_length );
246             }
247             
248         }
249         else
250         if( CmpGUID( &p_sp->i_stream_type, &asf_object_stream_type_video ) )
251         {
252             p_stream->i_cat = VIDEO_ES;
253             msg_Dbg( p_input,
254                     "adding new video stream(ID:%d)", 
255                     p_sp->i_stream_number );
256             if( p_sp->p_type_specific_data )
257             {
258                 p_stream->p_es->i_fourcc =
259                     GetDWLE( p_sp->p_type_specific_data + 27 );
260             }
261             else
262             {
263                 p_stream->p_es->i_fourcc = 
264                     VLC_FOURCC( 'u','n','d','f' );
265             }
266             if( p_sp->i_type_specific_data_length > 11 )
267             {
268                 p_stream->p_es->p_demux_data =
269                     malloc( p_sp->i_type_specific_data_length - 11);
270                 memcpy( p_stream->p_es->p_demux_data,
271                         p_sp->p_type_specific_data + 11,
272                         p_sp->i_type_specific_data_length - 11 );
273             }
274
275         }
276         else
277         {
278             p_stream->i_cat = UNKNOWN_ES;
279             msg_Dbg( p_input, 
280                     "ignoring unknown stream(ID:%d)", 
281                     p_sp->i_stream_number );
282             p_stream->p_es->i_fourcc = VLC_FOURCC( 'u','n','d','f' );
283         }
284         p_stream->p_es->i_cat = p_stream->i_cat;
285
286         vlc_mutex_lock( &p_input->stream.stream_lock );
287         input_SelectES( p_input, p_stream->p_es );
288         vlc_mutex_unlock( &p_input->stream.stream_lock );
289     }
290     
291     vlc_mutex_lock( &p_input->stream.stream_lock );
292     p_input->stream.p_selected_program->b_is_ok = 1;
293     vlc_mutex_unlock( &p_input->stream.stream_lock );
294
295     p_demux->i_data_begin = p_demux->root.p_data->i_object_pos + 50;
296     if( p_demux->root.p_data->i_object_size != 0 )
297     { // local file
298         p_demux->i_data_end = p_demux->root.p_data->i_object_pos +
299                                     p_demux->root.p_data->i_object_size;
300     }
301     else
302     { // live/broacast
303         p_demux->i_data_end = -1;
304     }
305
306
307     // go to first packet
308     ASF_SeekAbsolute( p_input, p_demux->i_data_begin );
309     return( 0 );
310 }
311
312 /*****************************************************************************
313  * Demux: read packet and send them to decoders 
314  *****************************************************************************/
315 #define GETVALUE2b( bits, var, def ) \
316     switch( (bits)&0x03 ) \
317     { \
318         case 1: var = p_peek[i_skip]; i_skip++; break; \
319         case 2: var = GetWLE( p_peek + i_skip );  i_skip+= 2; break; \
320         case 3: var = GetDWLE( p_peek + i_skip ); i_skip+= 4; break; \
321         case 0: \
322         default: var = def; break;\
323     }
324
325 static int Demux( input_thread_t *p_input )
326 {
327     demux_sys_t *p_demux = p_input->p_demux_data;
328     int i;
329
330     /* catch seek from user */
331     if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
332     {
333         off_t i_offset;
334         
335         i_offset = ASF_TellAbsolute( p_input ) - p_demux->i_data_begin;
336         
337         if( i_offset  < 0 )
338         {
339             i_offset = 0;
340         }
341         /* XXX work only when i_min_data_packet_size == i_max_data_packet_size */
342         i_offset -= i_offset % p_demux->p_fp->i_min_data_packet_size;
343         ASF_SeekAbsolute( p_input, p_demux->i_data_begin + i_offset );
344
345         p_demux->i_time = 0;
346         for( i = 0; i < 128 ; i++ )
347         {
348 #define p_stream p_demux->stream[i]
349             if( p_stream )
350             {
351                 p_stream->i_time = 0;
352             }
353 #undef p_stream
354         }
355
356     }
357     /* first wait for the good time to read a packet */
358
359     input_ClockManageRef( p_input,
360                           p_input->stream.p_selected_program,
361                           p_demux->i_pcr );
362
363     /* update pcr XXX in mpeg scale so in 90000 unit/s */
364     p_demux->i_pcr = p_demux->i_time * 9 / 100;
365
366     for( i = 0; i < 10; i++ ) // parse 10 packets
367     {
368         int i_data_packet_min = p_demux->p_fp->i_min_data_packet_size;
369         u8  *p_peek;
370         int i_skip;
371         
372         int i_packet_size_left;
373         int i_packet_flags;
374         int i_packet_property;
375
376         int b_packet_multiple_payload;
377         int i_packet_length;
378         int i_packet_sequence;
379         int i_packet_padding_length;
380         
381         u32 i_packet_send_time;
382         u16 i_packet_duration;
383         int i_payload;
384         int i_payload_count;
385         int i_payload_length_type;
386
387         
388         if( input_Peek( p_input, &p_peek, i_data_packet_min ) < i_data_packet_min )
389         {
390             // EOF ?
391             msg_Err( p_input, "cannot peek while getting new packet, EOF ?" );
392             return( 0 );
393         }
394         i_skip = 0;
395         
396         /* *** parse error correction if present *** */
397         if( p_peek[0]&0x80 )
398         {
399             int i_error_correction_length_type;
400             int i_error_correction_data_length;
401             int i_opaque_data_present;
402
403             i_error_correction_data_length = p_peek[0] & 0x0f;  // 4bits
404             i_opaque_data_present = ( p_peek[0] >> 4 )& 0x01;    // 1bit
405             i_error_correction_length_type = ( p_peek[0] >> 5 ) & 0x03; // 2bits
406             i_skip += 1; // skip error correction flags
407         
408             if( i_error_correction_length_type != 0x00 ||
409                 i_opaque_data_present != 0 ||
410                 i_error_correction_data_length != 0x02 )
411             {
412                 goto loop_error_recovery;
413             }
414
415             i_skip += i_error_correction_data_length;
416         }
417         else
418         {
419             msg_Warn( p_input, "p_peek[0]&0x80 != 0x80" );
420         }
421
422         i_packet_flags = p_peek[i_skip]; i_skip++;
423         i_packet_property = p_peek[i_skip]; i_skip++;
424         
425         b_packet_multiple_payload = i_packet_flags&0x01;
426
427         /* read some value */
428         GETVALUE2b( i_packet_flags >> 5, i_packet_length, i_data_packet_min );
429         GETVALUE2b( i_packet_flags >> 1, i_packet_sequence, 0 );
430         GETVALUE2b( i_packet_flags >> 3, i_packet_padding_length, 0 );
431
432         i_packet_send_time = GetDWLE( p_peek + i_skip ); i_skip += 4;
433         i_packet_duration  = GetWLE( p_peek + i_skip ); i_skip += 2;
434     
435 //        i_packet_size_left = i_packet_length;   // XXX données reellement lu
436         /* FIXME I have to do that for some file, I don't known why */
437         i_packet_size_left = i_data_packet_min;
438         
439         if( b_packet_multiple_payload )
440         {
441             i_payload_count = p_peek[i_skip] & 0x3f;
442             i_payload_length_type = ( p_peek[i_skip] >> 6 )&0x03;
443             i_skip++;
444         }
445         else
446         {
447             i_payload_count = 1;
448             i_payload_length_type = 0x02; // unused
449         }
450
451         for( i_payload = 0; i_payload < i_payload_count ; i_payload++ )
452         {
453             asf_stream_t   *p_stream;
454
455             int i_stream_number;
456             int i_media_object_number;
457             int i_media_object_offset;
458             int i_replicated_data_length;
459             int i_payload_data_length;
460             int i_payload_data_pos;
461             int i_sub_payload_data_length;
462             int i_tmp;
463
464             mtime_t i_pts;
465             mtime_t i_pts_delta;
466
467             i_stream_number = p_peek[i_skip] & 0x7f; 
468             i_skip++;
469             
470             GETVALUE2b( i_packet_property >> 4, i_media_object_number, 0 );
471             GETVALUE2b( i_packet_property >> 2, i_tmp, 0 );
472             GETVALUE2b( i_packet_property, i_replicated_data_length, 0 );
473             
474             if( i_replicated_data_length > 1 ) // should be at least 8 bytes
475             {
476                 i_pts = (mtime_t)GetDWLE( p_peek + i_skip + 4 ) * 1000;
477                 i_skip += i_replicated_data_length;
478                 i_pts_delta = 0;
479
480                 i_media_object_offset = i_tmp;
481             }
482             else if( i_replicated_data_length == 1 )
483             {
484
485                 msg_Warn( p_input, "found compressed payload" );
486
487                 i_pts = (mtime_t)i_tmp * 1000;
488                 i_pts_delta = (mtime_t)p_peek[i_skip] * 1000; i_skip++;
489
490                 i_media_object_offset = 0;
491             }
492             else
493             {
494                 i_pts = (mtime_t)i_packet_send_time * 1000;
495                 i_pts_delta = 0;
496
497                 i_media_object_offset = i_tmp;
498             }
499
500             i_pts = __MAX( i_pts - p_demux->p_fp->i_preroll * 1000, 0 );
501
502             if( b_packet_multiple_payload )
503             {
504                 GETVALUE2b( i_payload_length_type, i_payload_data_length, 0 );
505             }
506             else
507             {
508                 msg_Warn( p_input, "single payload" );
509                 i_payload_data_length = i_packet_length - i_packet_padding_length - i_skip;
510             }
511             
512 #if 0
513              msg_Dbg( p_input, 
514                       "payload(%d/%d) stream_number:%d media_object_number:%d media_object_offset:%d replicated_data_length:%d payload_data_length %d",
515                       i_payload + 1, 
516                       i_payload_count,
517                       i_stream_number,
518                       i_media_object_number, 
519                       i_media_object_offset, 
520                       i_replicated_data_length, 
521                       i_payload_data_length );
522 #endif
523
524             if( !( p_stream = p_demux->stream[i_stream_number] ) )
525             {
526                 msg_Warn( p_input, "undeclared stream[Id 0x%x]", i_stream_number );
527                 i_skip += i_payload_data_length;
528                 continue;   // over payload
529             }
530
531             if( !p_stream->p_es || !p_stream->p_es->p_decoder_fifo )
532             {
533                 i_skip += i_payload_data_length;
534                 continue;
535             }
536
537
538             for( i_payload_data_pos = 0; 
539                  i_payload_data_pos < i_payload_data_length && 
540                         i_packet_size_left > 0; 
541                  i_payload_data_pos += i_sub_payload_data_length )
542             {
543                 data_packet_t  *p_data;
544                 int i_read;
545                 // read sub payload length
546                 if( i_replicated_data_length == 1 )
547                 {
548                     i_sub_payload_data_length = p_peek[i_skip]; i_skip++;
549                     i_payload_data_pos++;
550                 }
551                 else
552                 {
553                     i_sub_payload_data_length = i_payload_data_length;
554                 }
555
556                 /* FIXME I don't use i_media_object_number, sould I ? */
557                 if( p_stream->p_pes && i_media_object_offset == 0 )                 {
558                     /* send complete packet to decoder */
559                     if( p_stream->p_pes->i_pes_size > 0 )
560                     {
561                         input_DecodePES( p_stream->p_es->p_decoder_fifo, p_stream->p_pes );
562                         p_stream->p_pes = NULL;
563                     }
564                 }
565
566                 if( !p_stream->p_pes )  // add a new PES
567                 {
568                     p_stream->i_time = ( (mtime_t)i_pts + i_payload * (mtime_t)i_pts_delta );
569                     
570                     p_stream->p_pes = input_NewPES( p_input->p_method_data );
571                     p_stream->p_pes->i_dts = 
572                         p_stream->p_pes->i_pts = 
573                             input_ClockGetTS( p_input, 
574                                               p_input->stream.p_selected_program,
575                                               p_stream->i_time * 9 / 100 );
576
577                     p_stream->p_pes->p_next = NULL;
578                     p_stream->p_pes->i_nb_data = 0;
579                     p_stream->p_pes->i_pes_size = 0;
580                 }
581
582                 i_read = i_sub_payload_data_length + i_skip;
583                 if( input_SplitBuffer( p_input, &p_data, i_read ) < i_read )
584                 {
585                     msg_Err( p_input, "cannot read data" );
586                     return( 0 );
587                 }
588                 p_data->p_payload_start += i_skip;
589                 i_packet_size_left -= i_read;
590
591                 
592                 if( !p_stream->p_pes->p_first )
593                 {
594                     p_stream->p_pes->p_first = p_stream->p_pes->p_last = p_data;
595                 }
596                 else
597                 {
598                     p_stream->p_pes->p_last->p_next = p_data;
599                     p_stream->p_pes->p_last = p_data;
600                 }
601                 p_stream->p_pes->i_pes_size += i_sub_payload_data_length;
602                 p_stream->p_pes->i_nb_data++;
603
604                 i_skip = 0;
605                 if( i_packet_size_left > 0 )
606                 {
607                     if( input_Peek( p_input, &p_peek, i_packet_size_left ) < i_packet_size_left )
608                     {
609                         // EOF ?
610                         msg_Warn( p_input, "cannot peek, EOF ?" );
611                         return( 0 );
612                     }
613                 }
614             }
615         }
616         
617         if( i_packet_size_left > 0 )
618         {
619             if( !ASF_SkipBytes( p_input, i_packet_size_left ) )
620             {
621                 msg_Warn( p_input, "cannot skip data, EOF ?" );
622                 return( 0 );
623             }
624         }
625
626         continue;
627
628 loop_error_recovery:
629         msg_Warn( p_input, "unsupported packet header" );
630         if( p_demux->p_fp->i_min_data_packet_size != p_demux->p_fp->i_max_data_packet_size )
631         {
632             msg_Err( p_input, "unsupported packet header, fatal error" );
633             return( -1 );
634         }
635         ASF_SkipBytes( p_input, i_data_packet_min );
636
637     }   // loop over packet
638     p_demux->i_time = 0;
639     for( i = 0; i < 128 ; i++ )
640     {
641 #define p_stream p_demux->stream[i]
642         if( p_stream && p_stream->p_es && p_stream->p_es->p_decoder_fifo )
643         {
644             p_demux->i_time = __MAX( p_demux->i_time, p_stream->i_time );
645         }
646 #undef p_stream
647     }
648     
649     return( 1 );
650 }
651
652 /*****************************************************************************
653  * MP4End: frees unused data
654  *****************************************************************************/
655 static void Deactivate( vlc_object_t * p_this )
656 {   
657 #define FREE( p ) \
658     if( p ) { free( p ); } 
659    
660     input_thread_t *  p_input = (input_thread_t *)p_this;
661     demux_sys_t *p_demux = p_input->p_demux_data;
662     int i_stream;
663     
664     msg_Dbg( p_input, "Freeing all memory" );
665     ASF_FreeObjectRoot( p_input, &p_demux->root );
666     for( i_stream = 0; i_stream < 128; i_stream++ )
667     {
668 #define p_stream p_demux->stream[i_stream]
669         if( p_stream )
670         {
671             if( p_stream->p_pes )
672             {
673                 input_DeletePES( p_input->p_method_data, p_stream->p_pes );
674             }
675             free( p_stream );
676         }
677 #undef p_stream
678     }
679     
680 #undef FREE
681 }
682