1 /*****************************************************************************
2 * libioRIFF.c : AVI file Stream input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: libioRIFF.c,v 1.6 2002/05/25 16:23:07 fenrir Exp $
6 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
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.
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.
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 *****************************************************************************/
23 typedef struct riffchunk_s
29 data_packet_t *p_data;
30 struct riffchunk_s *p_next;
31 struct riffchunk_s *p_subchunk;
34 static riffchunk_t * RIFF_ReadChunk(input_thread_t * p_input);
35 static int RIFF_NextChunk(input_thread_t * p_input,riffchunk_t *p_rifffather);
36 static int RIFF_DescendChunk(input_thread_t * p_input);
37 static int RIFF_AscendChunk(input_thread_t * p_input,riffchunk_t *p_rifffather);
38 static int RIFF_FindChunk(input_thread_t * p_input,u32 i_id,riffchunk_t *p_rifffather);
39 static int RIFF_GoToChunkData(input_thread_t * p_input);
40 static int RIFF_LoadChunkData(input_thread_t * p_input,riffchunk_t *p_riff);
41 static int RIFF_TestFileHeader(input_thread_t * p_input, riffchunk_t **pp_riff, u32 i_type);
42 static int RIFF_FindAndLoadChunk( input_thread_t * p_input, riffchunk_t *p_riff, riffchunk_t **pp_fmt, u32 i_type );
43 static int RIFF_FindAndGotoDataChunk( input_thread_t * p_input, riffchunk_t *p_riff, riffchunk_t **pp_data, u32 i_type );
44 static int RIFF_FindListChunk( input_thread_t *p_input, riffchunk_t **pp_riff, riffchunk_t *p_rifffather, u32 i_type );
46 static void RIFF_DeleteChunk( input_thread_t * p_input, riffchunk_t *p_chunk );
49 static int RIFF_GoToChunk(input_thread_t * p_input,riffchunk_t *p_riff);
51 static char * RIFF_IToStr(u32 i);
53 /*************************************************************************/
55 /********************************************
56 * Fonction locale maintenant *
57 ********************************************/
59 static int __RIFF_TellPos( input_thread_t *p_input, u32 *pos )
63 vlc_mutex_lock( &p_input->stream.stream_lock );
64 i = p_input->stream.p_selected_area->i_tell - ( p_input->p_last_data - p_input->p_current_data );
65 vlc_mutex_unlock( &p_input->stream.stream_lock );
70 static int __RIFF_SkipBytes(input_thread_t * p_input,int nb)
72 data_packet_t *p_pack;
75 if( p_input->stream.b_seekable )
78 __RIFF_TellPos( p_input, &i_pos);
79 p_input->pf_seek( p_input, (off_t)(i_pos + nb) );
80 input_AccessReinit( p_input );
84 intf_WarnMsg( 1, "input demux: cannot seek, it will take times" );
85 if( nb < 0 ) { return( -1 ); }
91 i = input_SplitBuffer( p_input, &p_pack, 4096);
95 i = input_SplitBuffer( p_input, &p_pack, i_rest);
98 if ( i < 0 ) { return ( -1 ); }
100 input_DeletePacket( p_input->p_method_data, p_pack);
101 if( ( i == 0 )&&( i_rest != 0 )) { return( -1 ); }
108 static void RIFF_DeleteChunk( input_thread_t *p_input, riffchunk_t *p_chunk )
112 if( p_chunk->p_data != NULL )
114 input_DeletePacket( p_input->p_method_data, p_chunk->p_data );
120 /* ******************************************
121 * lit une structure riffchunk sans avancer *
122 ********************************************/
123 static riffchunk_t * RIFF_ReadChunk(input_thread_t * p_input)
125 riffchunk_t * p_riff;
129 if((p_riff = malloc( sizeof(riffchunk_t))) == NULL)
134 p_riff->p_data = NULL; /* Par defaut */
135 p_riff->p_next = NULL;
136 p_riff->p_subchunk = NULL;
137 /* peek to have the begining, 8+4 where 4 are to get type */
138 count=input_Peek( p_input, &p_peek, 12 );
145 p_riff->i_id = __GetDoubleWordLittleEndianFromBuff( p_peek );
146 p_riff->i_size =__GetDoubleWordLittleEndianFromBuff( p_peek + 4 );
149 p_riff->i_type = __GetDoubleWordLittleEndianFromBuff( p_peek + 8 );
155 __RIFF_TellPos(p_input, &(p_riff->i_pos) );
160 /**************************************************
161 * Va au chunk juste d'apres si il en a encore *
162 * -1 si erreur , 1 si y'en a plus *
163 **************************************************/
164 static int RIFF_NextChunk( input_thread_t * p_input,riffchunk_t *p_rifffather)
170 if( ( p_riff = RIFF_ReadChunk( p_input ) ) == NULL )
174 i_len = p_riff->i_size;
175 if( i_len%2 != 0 ) {i_len++;} /* aligné sur un mot */
177 if ( p_rifffather != NULL )
179 i_lenfather=p_rifffather->i_size;
180 if ( i_lenfather%2 !=0 ) {i_lenfather++;}
181 if ( p_rifffather->i_pos + i_lenfather <= p_riff->i_pos + i_len + 8 )
184 return( 1 ); /* pas dans nos frontiere */
187 if ( __RIFF_SkipBytes( p_input,i_len + 8 ) != 0 )
196 /****************************************************************
197 * Permet de rentrer dans un ck RIFF ou LIST *
198 ****************************************************************/
199 static int RIFF_DescendChunk(input_thread_t * p_input)
201 if ( __RIFF_SkipBytes(p_input,12) != 0)
208 /***************************************************************
209 * Permet de sortir d'un sous chunk et d'aller sur le suivant *
211 ***************************************************************/
213 static int RIFF_AscendChunk(input_thread_t * p_input ,riffchunk_t *p_rifffather)
218 i_skip = p_rifffather->i_pos + p_rifffather->i_size + 8;
219 if ( i_skip%2 != 0) {i_skip++;}
221 __RIFF_TellPos(p_input, &i_posactu);
224 if (( __RIFF_SkipBytes(p_input,i_skip)) != 0)
231 /***************************************************************
232 * Permet de se deplacer jusqu'au premier chunk avec le bon id *
233 * *************************************************************/
234 static int RIFF_FindChunk(input_thread_t * p_input ,u32 i_id,riffchunk_t *p_rifffather)
236 riffchunk_t *p_riff=NULL;
242 if ( RIFF_NextChunk(p_input ,p_rifffather) != 0 )
247 p_riff=RIFF_ReadChunk(p_input);
248 } while ( ( p_riff != NULL )&&( p_riff->i_id != i_id ) );
250 if ( ( p_riff == NULL )||( p_riff->i_id != i_id ) )
258 /*****************************************************************
259 * Permet de pointer sur la zone de donné du chunk courant *
260 *****************************************************************/
261 static int RIFF_GoToChunkData(input_thread_t * p_input)
263 if ( __RIFF_SkipBytes(p_input,8) != 0 )
270 static int RIFF_LoadChunkData(input_thread_t * p_input,riffchunk_t *p_riff )
273 RIFF_GoToChunkData(p_input);
274 if ( input_SplitBuffer( p_input, &p_riff->p_data, p_riff->i_size ) != p_riff->i_size )
278 if ( p_riff->i_size%2 != 0)
280 __RIFF_SkipBytes(p_input,1);
281 } /* aligne sur un mot */
285 static int RIFF_LoadChunkDataInPES(input_thread_t * p_input,
286 pes_packet_t **pp_pes,
290 data_packet_t *p_data;
294 if( (p_riff = RIFF_ReadChunk( p_input )) == NULL )
299 RIFF_GoToChunkData(p_input);
300 *pp_pes = input_NewPES( p_input->p_method_data );
302 if( *pp_pes == NULL )
307 if( (!p_riff->i_size) || (!i_size_index ) )
309 i_size = __MAX( i_size_index, p_riff->i_size );
313 i_size = __MIN( p_riff->i_size, i_size_index );
316 if( p_riff->i_size == 0 )
318 p_data = input_NewPacket( p_input->p_method_data, 0 );
319 (*pp_pes)->p_first = p_data;
320 (*pp_pes)->p_last = p_data;
321 (*pp_pes)->i_nb_data = 1;
322 (*pp_pes)->i_pes_size = 0;
328 i_read = input_SplitBuffer(p_input, &p_data, i_size -
329 (*pp_pes)->i_pes_size );
332 /* FIXME free sur tout les packets */
335 if( (*pp_pes)->p_first == NULL )
337 (*pp_pes)->p_first = p_data;
338 (*pp_pes)->p_last = p_data;
339 (*pp_pes)->i_nb_data = 1;
340 (*pp_pes)->i_pes_size = ( p_data->p_payload_end -
341 p_data->p_payload_start );
345 (*pp_pes)->p_last->p_next = p_data;
346 (*pp_pes)->p_last = p_data;
347 (*pp_pes)->i_nb_data++;
348 (*pp_pes)->i_pes_size += ( p_data->p_payload_end -
349 p_data->p_payload_start );
351 } while( ((*pp_pes)->i_pes_size < i_size)&&(i_read != 0) );
352 /* i_read = 0 si fin du stream sinon block */
355 __RIFF_SkipBytes(p_input,1);
356 } /* aligne sur un mot */
363 static int RIFF_GoToChunk(input_thread_t * p_input, riffchunk_t *p_riff)
365 /* TODO rajouter les test */
366 if( p_input->stream.b_seekable )
368 p_input->pf_seek( p_input, (off_t)p_riff->i_pos );
369 input_AccessReinit( p_input );
376 static char * RIFF_IToStr(u32 l)
380 str=calloc(5,sizeof(char));
381 for( i = 0; i < 4; i++)
383 str[i] = ( l >> ( i * 8) )&0xFF;
389 static int RIFF_TestFileHeader( input_thread_t * p_input, riffchunk_t ** pp_riff, u32 i_type )
391 *pp_riff = RIFF_ReadChunk( p_input );
393 if( *pp_riff == NULL )
397 if( (*pp_riff)->i_id != FOURCC_RIFF )
402 if( (*pp_riff)->i_type != i_type )
411 static int RIFF_FindAndLoadChunk( input_thread_t * p_input, riffchunk_t *p_riff, riffchunk_t **pp_fmt, u32 i_type )
414 if ( RIFF_FindChunk( p_input, i_type, p_riff ) != 0)
418 if ( ( (*pp_fmt = RIFF_ReadChunk( p_input )) == NULL) || ( RIFF_LoadChunkData( p_input, *pp_fmt ) != 0 ) )
420 if( *pp_fmt != NULL ) { RIFF_DeleteChunk( p_input, *pp_fmt ); }
426 static int RIFF_FindAndGotoDataChunk( input_thread_t * p_input, riffchunk_t *p_riff, riffchunk_t **pp_data, u32 i_type )
429 if ( RIFF_FindChunk( p_input, i_type, p_riff ) != 0)
433 if ( ( *pp_data = RIFF_ReadChunk( p_input ) ) == NULL )
437 if ( RIFF_GoToChunkData( p_input ) != 0 )
439 RIFF_DeleteChunk( p_input, *pp_data );
445 static int RIFF_FindListChunk( input_thread_t *p_input, riffchunk_t **pp_riff, riffchunk_t *p_rifffather, u32 i_type )
453 if( *pp_riff != NULL )
457 if( RIFF_FindChunk( p_input, FOURCC_LIST, p_rifffather) != 0 )
461 *pp_riff = RIFF_ReadChunk( p_input );
463 if( *pp_riff == NULL )
467 if( (*pp_riff)->i_type != i_type )
469 if( RIFF_NextChunk( p_input, p_rifffather ) != 0 )