1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Borrowed from media/tools/omx_test/file_reader_util.cc.
// Added some functionalities related to timestamps on packets.
#include "media/mf/file_reader_util.h"
#include <cstring>
#include <algorithm>
#include "base/scoped_comptr_win.h"
#include "base/logging.h"
#include "media/ffmpeg/ffmpeg_common.h"
#include "media/filters/bitstream_converter.h"
namespace media {
//////////////////////////////////////////////////////////////////////////////
// FFmpegFileReader
FFmpegFileReader::FFmpegFileReader(const std::string& filename)
: filename_(filename),
format_context_(NULL),
codec_context_(NULL),
target_stream_(-1),
converter_(NULL),
end_of_stream_(false) {
}
FFmpegFileReader::~FFmpegFileReader() {
if (format_context_)
av_close_input_file(format_context_);
}
bool FFmpegFileReader::Initialize() {
int result = av_open_input_file(&format_context_, filename_.c_str(),
NULL, 0, NULL);
if (result < 0) {
switch (result) {
case AVERROR_NOFMT:
LOG(ERROR) << "Error: File format not supported "
<< filename_;
break;
default:
LOG(ERROR) << "Error: Could not open input for "
<< filename_ << ": " << result;
break;
}
return false;
}
if (av_find_stream_info(format_context_) < 0) {
LOG(ERROR) << "can't use FFmpeg to parse stream info";
return false;
}
for (size_t i = 0; i < format_context_->nb_streams; ++i) {
codec_context_ = format_context_->streams[i]->codec;
// Find the video stream.
if (codec_context_->codec_type == CODEC_TYPE_VIDEO) {
target_stream_ = i;
break;
}
}
if (target_stream_ == -1) {
LOG(ERROR) << "no video in the stream";
return false;
}
// Initialize the bitstream filter if needed.
// TODO(hclam): find a better way to identify mp4 container.
if (codec_context_->codec_id == CODEC_ID_H264) {
converter_.reset(new media::FFmpegBitstreamConverter(
"h264_mp4toannexb", codec_context_));
} else if (codec_context_->codec_id == CODEC_ID_MPEG4) {
converter_.reset(new media::FFmpegBitstreamConverter(
"mpeg4video_es", codec_context_));
} else if (codec_context_->codec_id == CODEC_ID_WMV3) {
converter_.reset(new media::FFmpegBitstreamConverter(
"vc1_asftorcv", codec_context_));
} else if (codec_context_->codec_id == CODEC_ID_VC1) {
converter_.reset(new media::FFmpegBitstreamConverter(
"vc1_asftoannexg", codec_context_));
}
if (converter_.get() && !converter_->Initialize()) {
converter_.reset();
LOG(ERROR) << "failed to initialize h264_mp4toannexb filter";
return false;
}
return true;
}
void FFmpegFileReader::Read(uint8** output, int* size) {
Read2(output, size, NULL, NULL);
}
void FFmpegFileReader::Read2(uint8** output, int* size, int64* timestamp,
int64* duration) {
if (!format_context_ || !codec_context_ || target_stream_ == -1) {
*size = 0;
*output = NULL;
return;
}
AVPacket packet;
bool found = false;
while (!found) {
int result = av_read_frame(format_context_, &packet);
if (result < 0) {
*output = NULL;
*size = 0;
end_of_stream_ = true;
return;
}
if (packet.stream_index == target_stream_) {
if (converter_.get() && !converter_->ConvertPacket(&packet)) {
LOG(ERROR) << "failed to convert AVPacket";
}
*output = new uint8[packet.size];
if (*output == NULL) {
LOG(ERROR) << "Failed to allocate buffer for annex b stream";
*size = 0;
return;
}
*size = packet.size;
memcpy(*output, packet.data, packet.size);
if (duration) {
if (packet.duration == 0) {
LOG(WARNING) << "Packet duration not known";
}
// This is in AVCodecContext::time_base units
*duration = ConvertFFmpegTimeBaseTo100Ns(packet.duration);
}
if (timestamp) {
if (packet.pts == AV_NOPTS_VALUE) {
LOG(ERROR) << "Packet presentation time not known";
*timestamp = 0L;
} else {
// This is in AVCodecContext::time_base units
*timestamp = ConvertFFmpegTimeBaseTo100Ns(packet.pts);
}
}
found = true;
}
av_free_packet(&packet);
}
}
bool FFmpegFileReader::GetFrameRate(int* num, int* denom) const {
if (!codec_context_)
return false;
*denom = codec_context_->time_base.num;
*num = codec_context_->time_base.den;
if (*denom == 0) {
*num = 0;
return false;
}
return true;
}
bool FFmpegFileReader::GetWidth(int* width) const {
if (!codec_context_)
return false;
*width = codec_context_->width;
return true;
}
bool FFmpegFileReader::GetHeight(int* height) const {
if (!codec_context_)
return false;
*height = codec_context_->height;
return true;
}
bool FFmpegFileReader::GetAspectRatio(int* num, int* denom) const {
if (!codec_context_)
return false;
AVRational aspect_ratio = codec_context_->sample_aspect_ratio;
if (aspect_ratio.num == 0 || aspect_ratio.den == 0)
return false;
*num = aspect_ratio.num;
*denom = aspect_ratio.den;
return true;
}
int64 FFmpegFileReader::ConvertFFmpegTimeBaseTo100Ns(
int64 time_base_unit) const {
// FFmpeg units after time base conversion seems to be actually given in
// milliseconds (instead of seconds...) so we need to multiply it by a factor
// of 10,000 to convert it into units compatible with MF.
// Note we need to double this because the frame rate is doubled in
// ffmpeg.
CHECK(codec_context_) << "Codec context needs to be initialized";
return time_base_unit * 20000 * codec_context_->time_base.num /
codec_context_->time_base.den;
}
} // namespace media
|