1 /*****************************************************************************
2 * libioRIFF.c : AVI file Stream input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: libioRIFF.c,v 1.8 2002/06/27 18:10:16 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 *****************************************************************************/
25 #include <vlc/input.h>
28 #include "libioRIFF.h"
30 inline u16 __GetWLE( byte_t *p_buff )
32 return( (*p_buff) + ( *(p_buff+1) <<8 ) );
35 inline u32 __GetDWLE( byte_t *p_buff )
37 return( *(p_buff) + ( *(p_buff+1) <<8 ) +
38 ( *(p_buff+2) <<16 ) + ( *(p_buff+3) <<24 ) );
41 inline u32 __EVEN( u32 i )
43 return( (i & 1) ? ++i : i );
46 int __RIFF_TellPos( input_thread_t *p_input, u32 *pos )
48 vlc_mutex_lock( &p_input->stream.stream_lock );
49 *pos= p_input->stream.p_selected_area->i_tell -
50 ( p_input->p_last_data - p_input->p_current_data );
51 vlc_mutex_unlock( &p_input->stream.stream_lock );
55 int __RIFF_SkipBytes(input_thread_t * p_input,int nb)
57 data_packet_t *p_pack;
60 if( p_input->stream.b_seekable )
63 __RIFF_TellPos( p_input, &i_pos);
64 p_input->pf_seek( p_input, (off_t)(i_pos + nb) );
65 input_AccessReinit( p_input );
69 msg_Warn( p_input, "cannot seek, it will take times" );
70 if( nb < 0 ) { return( -1 ); }
76 i = input_SplitBuffer( p_input, &p_pack, 4096);
80 i = input_SplitBuffer( p_input, &p_pack, i_rest);
83 if ( i < 0 ) { return ( -1 ); }
85 input_DeletePacket( p_input->p_method_data, p_pack);
86 if( ( i == 0 )&&( i_rest != 0 )) { return( -1 ); }
93 void RIFF_DeleteChunk( input_thread_t *p_input, riffchunk_t *p_chunk )
97 if( p_chunk->p_data != NULL )
99 input_DeletePacket( p_input->p_method_data, p_chunk->p_data );
105 riffchunk_t * RIFF_ReadChunk(input_thread_t * p_input)
107 riffchunk_t * p_riff;
111 if( !(p_riff = malloc( sizeof(riffchunk_t))) )
116 p_riff->p_data = NULL;
117 /* peek to have the begining, 8+4 where 4 are to get type */
118 if( ( count = input_Peek( p_input, &p_peek, 12 ) ) < 8 )
120 msg_Err( p_input, "cannot peek()" );
125 p_riff->i_id = __GetDWLE( p_peek );
126 p_riff->i_size =__GetDWLE( p_peek + 4 );
127 p_riff->i_type = ( count == 12 ) ? __GetDWLE( p_peek + 8 ) : 0 ;
129 __RIFF_TellPos(p_input, &(p_riff->i_pos) );
134 /**************************************************
135 * Va au chunk juste d'apres si il en a encore *
136 * -1 si erreur , 1 si y'en a plus *
137 **************************************************/
138 int RIFF_NextChunk( input_thread_t * p_input,riffchunk_t *p_rifffather)
144 if( ( p_riff = RIFF_ReadChunk( p_input ) ) == NULL )
146 msg_Err( p_input, "cannot read chunk" );
149 i_len = __EVEN( p_riff->i_size );
151 if ( p_rifffather != NULL )
153 i_lenfather = __EVEN( p_rifffather->i_size );
154 if ( p_rifffather->i_pos + i_lenfather <= p_riff->i_pos + i_len + 8 )
156 msg_Err( p_input, "next chunk out of bounds" );
158 return( 1 ); /* pas dans nos frontiere */
161 if ( __RIFF_SkipBytes( p_input,i_len + 8 ) != 0 )
164 msg_Err( p_input, "cannot go to the next chunk" );
171 /****************************************************************
172 * Permet de rentrer dans un ck RIFF ou LIST *
173 ****************************************************************/
174 int RIFF_DescendChunk(input_thread_t * p_input)
176 return( __RIFF_SkipBytes(p_input,12) != 0 ? -1 : 0 );
179 /***************************************************************
180 * Permet de sortir d'un sous chunk et d'aller sur le suivant *
182 ***************************************************************/
184 int RIFF_AscendChunk(input_thread_t * p_input ,riffchunk_t *p_riff)
189 __RIFF_TellPos(p_input, &i_posactu);
190 i_skip = __EVEN( p_riff->i_pos + p_riff->i_size + 8 ) - i_posactu;
191 return( (( __RIFF_SkipBytes(p_input,i_skip)) != 0) ? -1 : 0 );
194 /***************************************************************
195 * Permet de se deplacer jusqu'au premier chunk avec le bon id *
196 * *************************************************************/
197 int RIFF_FindChunk(input_thread_t * p_input ,u32 i_id,riffchunk_t *p_rifffather)
199 riffchunk_t *p_riff = NULL;
205 if ( RIFF_NextChunk(p_input ,p_rifffather) != 0 )
210 p_riff=RIFF_ReadChunk(p_input);
211 } while ( ( p_riff )&&( p_riff->i_id != i_id ) );
213 if ( ( !p_riff )||( p_riff->i_id != i_id ) )
221 /*****************************************************************
222 * Permet de pointer sur la zone de donné du chunk courant *
223 *****************************************************************/
224 int RIFF_GoToChunkData(input_thread_t * p_input)
226 return( ( __RIFF_SkipBytes(p_input,8) != 0 ) ? -1 : 0 );
229 int RIFF_LoadChunkData(input_thread_t * p_input,riffchunk_t *p_riff )
231 off_t i_read = __EVEN( p_riff->i_size );
233 RIFF_GoToChunkData(p_input);
234 if ( input_SplitBuffer( p_input,
238 msg_Err( p_input, "cannot read enough data " );
242 if( p_riff->i_size&1 )
244 p_riff->p_data->p_payload_end--;
249 int RIFF_LoadChunkDataInPES(input_thread_t * p_input,
250 pes_packet_t **pp_pes,
254 data_packet_t *p_data;
259 if( (p_riff = RIFF_ReadChunk( p_input )) == NULL )
264 RIFF_GoToChunkData(p_input);
265 *pp_pes = input_NewPES( p_input->p_method_data );
267 if( *pp_pes == NULL )
272 if( (!p_riff->i_size) || (!i_size_index ) )
274 i_size = __MAX( i_size_index, p_riff->i_size );
278 i_size = __MIN( p_riff->i_size, i_size_index );
281 if( !p_riff->i_size )
283 p_data = input_NewPacket( p_input->p_method_data, 0 );
284 (*pp_pes)->p_first = p_data;
285 (*pp_pes)->p_last = p_data;
286 (*pp_pes)->i_nb_data = 1;
287 (*pp_pes)->i_pes_size = 0;
298 i_read = input_SplitBuffer(p_input, &p_data, i_size -
299 (*pp_pes)->i_pes_size );
302 /* FIXME free on all packets */
305 if( (*pp_pes)->p_first == NULL )
307 (*pp_pes)->p_first = p_data;
308 (*pp_pes)->p_last = p_data;
309 (*pp_pes)->i_nb_data = 1;
310 (*pp_pes)->i_pes_size = ( p_data->p_payload_end -
311 p_data->p_payload_start );
315 (*pp_pes)->p_last->p_next = p_data;
316 (*pp_pes)->p_last = p_data;
317 (*pp_pes)->i_nb_data++;
318 (*pp_pes)->i_pes_size += ( p_data->p_payload_end -
319 p_data->p_payload_start );
321 } while( ((*pp_pes)->i_pes_size < i_size)&&(i_read != 0) );
325 (*pp_pes)->i_pes_size--;
326 (*pp_pes)->p_last->p_payload_end--;
331 int RIFF_GoToChunk(input_thread_t * p_input, riffchunk_t *p_riff)
333 if( p_input->stream.b_seekable )
335 p_input->pf_seek( p_input, (off_t)p_riff->i_pos );
336 input_AccessReinit( p_input );
342 int RIFF_TestFileHeader( input_thread_t * p_input, riffchunk_t ** pp_riff, u32 i_type )
344 if( !( *pp_riff = RIFF_ReadChunk( p_input ) ) )
348 if( ( (*pp_riff)->i_id != FOURCC_RIFF )||( (*pp_riff)->i_type != i_type ) )
357 int RIFF_FindAndLoadChunk( input_thread_t * p_input, riffchunk_t *p_riff, riffchunk_t **pp_fmt, u32 i_type )
360 if ( RIFF_FindChunk( p_input, i_type, p_riff ) != 0)
364 if ( ( (*pp_fmt = RIFF_ReadChunk( p_input )) == NULL)
365 || ( RIFF_LoadChunkData( p_input, *pp_fmt ) != 0 ) )
367 if( *pp_fmt != NULL )
369 RIFF_DeleteChunk( p_input, *pp_fmt );
376 int RIFF_FindAndGotoDataChunk( input_thread_t * p_input, riffchunk_t *p_riff, riffchunk_t **pp_data, u32 i_type )
379 if ( RIFF_FindChunk( p_input, i_type, p_riff ) != 0)
383 if ( ( *pp_data = RIFF_ReadChunk( p_input ) ) == NULL )
387 if ( RIFF_GoToChunkData( p_input ) != 0 )
389 RIFF_DeleteChunk( p_input, *pp_data );
395 int RIFF_FindListChunk( input_thread_t *p_input, riffchunk_t **pp_riff, riffchunk_t *p_rifffather, u32 i_type )
403 if( *pp_riff != NULL )
407 if( RIFF_FindChunk( p_input, FOURCC_LIST, p_rifffather) != 0 )
411 *pp_riff = RIFF_ReadChunk( p_input );
413 if( *pp_riff == NULL )
417 if( (*pp_riff)->i_type != i_type )
419 if( RIFF_NextChunk( p_input, p_rifffather ) != 0 )