]> git.sesse.net Git - vlc/blob - modules/demux/mkv/stream_io_callback.cpp
demux: mkv: enforce valid frame rate
[vlc] / modules / demux / mkv / stream_io_callback.cpp
1 /*****************************************************************************
2  * stream_io_callback.cpp : matroska demuxer
3  *****************************************************************************
4  * Copyright (C) 2003-2004, 2010 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Steve Lhomme <steve.lhomme@free.fr>
9  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #include "stream_io_callback.hpp"
26
27 #include "matroska_segment.hpp"
28 #include "demux.hpp"
29
30 /*****************************************************************************
31  * Stream managment
32  *****************************************************************************/
33 vlc_stream_io_callback::vlc_stream_io_callback( stream_t *s_, bool b_owner_ )
34                        : s( s_), b_owner( b_owner_ )
35 {
36     mb_eof = false;
37 }
38
39 uint32 vlc_stream_io_callback::read( void *p_buffer, size_t i_size )
40 {
41     if( i_size <= 0 || mb_eof )
42         return 0;
43
44     return stream_Read( s, p_buffer, i_size );
45 }
46
47 void vlc_stream_io_callback::setFilePointer(int64_t i_offset, seek_mode mode )
48 {
49     int64_t i_pos, i_size;
50
51     switch( mode )
52     {
53         case seek_beginning:
54             i_pos = i_offset;
55             break;
56         case seek_end:
57             i_pos = stream_Size( s ) - i_offset;
58             break;
59         default:
60             i_pos= stream_Tell( s ) + i_offset;
61             break;
62     }
63
64     if( i_pos < 0 || ( ( i_size = stream_Size( s ) ) != 0 && i_pos >= i_size ) )
65     {
66         mb_eof = true;
67         return;
68     }
69
70     mb_eof = false;
71     if( stream_Seek( s, i_pos ) )
72     {
73         mb_eof = true;
74     }
75     return;
76 }
77
78 uint64 vlc_stream_io_callback::getFilePointer( void )
79 {
80     if ( s == NULL )
81         return 0;
82     return stream_Tell( s );
83 }
84
85 size_t vlc_stream_io_callback::write(const void *, size_t )
86 {
87     return 0;
88 }
89
90 uint64 vlc_stream_io_callback::toRead( void )
91 {
92     uint64_t i_size;
93
94     if( s == NULL)
95         return 0;
96
97     stream_Control( s, STREAM_GET_SIZE, &i_size );
98
99     if( i_size == 0 )
100         return UINT64_MAX;
101
102     return (uint64) i_size - stream_Tell( s );
103 }
104