@@ -25,29 +25,11 @@ extern "C" {
25
25
namespace avtranscoder
26
26
{
27
27
28
- InputFile::InputFile ()
28
+ InputFile::InputFile ( const std::string& filename )
29
29
: m_formatContext ( NULL )
30
- , m_stream ( NULL )
31
- , m_filename ( " " )
32
- , m_packetCount ( 0 )
30
+ , m_filename ( filename )
33
31
{
34
32
av_register_all (); // Warning: should be called only once
35
- }
36
-
37
- InputFile::~InputFile ()
38
- {
39
- if ( m_formatContext != NULL )
40
- {
41
- avformat_close_input ( &m_formatContext );
42
- m_formatContext = NULL ;
43
- }
44
- }
45
-
46
- InputFile& InputFile::setup ( const std::string& filename )
47
- {
48
- m_filename = filename;
49
- assert ( m_formatContext == NULL );
50
-
51
33
if ( avformat_open_input ( &m_formatContext, m_filename.c_str (), NULL , NULL ) < 0 )
52
34
{
53
35
throw std::runtime_error ( " unable to open file" );
@@ -60,26 +42,35 @@ InputFile& InputFile::setup( const std::string& filename )
60
42
m_formatContext = NULL ;
61
43
throw std::runtime_error ( " unable to find stream informations" );
62
44
}
45
+ }
63
46
64
- return *this ;
47
+ InputFile::~InputFile ()
48
+ {
49
+ if ( m_formatContext != NULL )
50
+ {
51
+ avformat_close_input ( &m_formatContext );
52
+ m_formatContext = NULL ;
53
+ }
65
54
}
66
55
67
56
InputFile& InputFile::analyse ()
68
57
{
69
- properties.filename = m_formatContext->filename ;
70
- properties.formatName = m_formatContext->iformat ->name ;
71
- properties.formatLongName = m_formatContext->iformat ->long_name ;
72
- properties.streamsCount = m_formatContext->nb_streams ;
73
- properties.programsCount = m_formatContext->nb_programs ;
74
- properties.startTime = 1.0 * (uint)m_formatContext->start_time / AV_TIME_BASE;
75
- properties.duration = 1.0 * m_formatContext->duration / AV_TIME_BASE;
76
- properties.bitRate = m_formatContext->bit_rate ;
77
- properties.packetSize = m_formatContext->packet_size ;
58
+ assert ( m_formatContext != NULL );
59
+
60
+ m_properties.filename = m_formatContext->filename ;
61
+ m_properties.formatName = m_formatContext->iformat ->name ;
62
+ m_properties.formatLongName = m_formatContext->iformat ->long_name ;
63
+ m_properties.streamsCount = m_formatContext->nb_streams ;
64
+ m_properties.programsCount = m_formatContext->nb_programs ;
65
+ m_properties.startTime = 1.0 * (uint)m_formatContext->start_time / AV_TIME_BASE;
66
+ m_properties.duration = 1.0 * m_formatContext->duration / AV_TIME_BASE;
67
+ m_properties.bitRate = m_formatContext->bit_rate ;
68
+ m_properties.packetSize = m_formatContext->packet_size ;
78
69
79
70
AVDictionaryEntry *tag = NULL ;
80
71
while ( ( tag = av_dict_get ( m_formatContext->metadata , " " , tag, AV_DICT_IGNORE_SUFFIX ) ) )
81
72
{
82
- properties .metadatas .push_back ( std::pair<std::string, std::string>( tag->key , tag->value ) );
73
+ m_properties .metadatas .push_back ( std::pair<std::string, std::string>( tag->key , tag->value ) );
83
74
}
84
75
85
76
for ( size_t streamId = 0 ; streamId < m_formatContext->nb_streams ; streamId++ )
@@ -88,32 +79,32 @@ InputFile& InputFile::analyse()
88
79
{
89
80
case AVMEDIA_TYPE_VIDEO:
90
81
{
91
- properties .videoStreams .push_back ( videoStreamInfo ( m_formatContext, streamId ) );
82
+ m_properties .videoStreams .push_back ( videoStreamInfo ( m_formatContext, streamId ) );
92
83
break ;
93
84
}
94
85
case AVMEDIA_TYPE_AUDIO:
95
86
{
96
- properties .audioStreams .push_back ( audioStreamInfo ( m_formatContext, streamId ) );
87
+ m_properties .audioStreams .push_back ( audioStreamInfo ( m_formatContext, streamId ) );
97
88
break ;
98
89
}
99
90
case AVMEDIA_TYPE_DATA:
100
91
{
101
- properties .dataStreams .push_back ( dataStreamInfo ( m_formatContext, streamId ) );
92
+ m_properties .dataStreams .push_back ( dataStreamInfo ( m_formatContext, streamId ) );
102
93
break ;
103
94
}
104
95
case AVMEDIA_TYPE_SUBTITLE:
105
96
{
106
- properties .subtitleStreams .push_back ( subtitleStreamInfo ( m_formatContext, streamId ) );
97
+ m_properties .subtitleStreams .push_back ( subtitleStreamInfo ( m_formatContext, streamId ) );
107
98
break ;
108
99
}
109
100
case AVMEDIA_TYPE_ATTACHMENT:
110
101
{
111
- properties .attachementStreams .push_back ( attachementStreamInfo ( m_formatContext, streamId ) );
102
+ m_properties .attachementStreams .push_back ( attachementStreamInfo ( m_formatContext, streamId ) );
112
103
break ;
113
104
}
114
105
case AVMEDIA_TYPE_UNKNOWN:
115
106
{
116
- properties .unknownStreams .push_back ( unknownStreamInfo ( m_formatContext, streamId ) );
107
+ m_properties .unknownStreams .push_back ( unknownStreamInfo ( m_formatContext, streamId ) );
117
108
break ;
118
109
}
119
110
case AVMEDIA_TYPE_NB:
@@ -127,98 +118,9 @@ InputFile& InputFile::analyse()
127
118
return *this ;
128
119
}
129
120
130
- VideoDesc InputFile::getVideoDesc ( size_t videoStreamId )
121
+ InputStream InputFile::getStream ( size_t index )
131
122
{
132
- int selectedStream = -1 ;
133
- size_t videoStreamCount = 0 ;
134
-
135
- for ( size_t streamId = 0 ; streamId < m_formatContext->nb_streams ; streamId++ )
136
- {
137
- if ( m_formatContext->streams [streamId]->codec ->codec_type == AVMEDIA_TYPE_VIDEO )
138
- {
139
- if ( videoStreamCount == videoStreamId )
140
- {
141
- selectedStream = streamId;
142
- }
143
- videoStreamCount++;
144
- }
145
- }
146
-
147
- if ( selectedStream == -1 )
148
- {
149
- throw std::runtime_error ( " unable to find video stream " );
150
- }
151
-
152
- AVCodecContext* codecContext = m_formatContext->streams [selectedStream]->codec ;
153
-
154
- VideoDesc desc ( codecContext->codec_id );
155
-
156
- desc.setImageParameters ( codecContext->width , codecContext->height , codecContext->pix_fmt );
157
- desc.setTimeBase ( codecContext->time_base .num , codecContext->time_base .den );
158
-
159
- return desc;
160
- }
161
-
162
- AudioDesc InputFile::getAudioDesc ( size_t audioStreamId )
163
- {
164
- int selectedStream = -1 ;
165
- size_t audioStreamCount = 0 ;
166
-
167
- for ( size_t streamId = 0 ; streamId < m_formatContext->nb_streams ; streamId++ )
168
- {
169
- if ( m_formatContext->streams [streamId]->codec ->codec_type == AVMEDIA_TYPE_AUDIO )
170
- {
171
- if ( audioStreamCount == audioStreamId )
172
- {
173
- selectedStream = streamId;
174
- }
175
- audioStreamCount++;
176
- }
177
- }
178
-
179
- if ( selectedStream == -1 )
180
- {
181
- throw std::runtime_error ( " unable to find audio stream " );
182
- }
183
-
184
- AVCodecContext* codecContext = m_formatContext->streams [selectedStream]->codec ;
185
-
186
- AudioDesc desc ( codecContext->codec_id );
187
-
188
- return desc;
189
- }
190
-
191
- bool InputFile::unwrap ( DataStream& data, const size_t streamIndex )
192
- {
193
- AVPacket packet;
194
- av_init_packet ( &packet );
195
-
196
- readNextPacket ( packet, streamIndex );
197
-
198
- data.getBuffer ().resize ( packet.size );
199
- memcpy ( data.getPtr (), packet.data , packet.size );
200
-
201
- av_free_packet ( &packet );
202
-
203
- return true ;
204
- }
205
-
206
- bool InputFile::readNextPacket ( AVPacket& packet, const size_t streamIndex )
207
- {
208
- while ( 1 )
209
- {
210
- int ret = av_read_frame ( m_formatContext, &packet );
211
- if ( ret < 0 ) // error or end of file
212
- {
213
- return false ;
214
- }
215
-
216
- // We only read one stream and skip others
217
- if ( packet.stream_index == (int )streamIndex )
218
- {
219
- return true ;
220
- }
221
- }
123
+ InputStream inputStream ( m_filename, index );
222
124
}
223
125
224
126
}
0 commit comments