OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "media/capture/video/file_video_capture_device.h" | 5 #include "media/capture/video/file_video_capture_device.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
9 #include "base/strings/string_piece.h" | 9 #include "base/strings/string_piece.h" |
10 #include "media/base/video_capture_types.h" | 10 #include "media/base/video_capture_types.h" |
11 #include "media/filters/jpeg_parser.h" | |
11 | 12 |
12 namespace media { | 13 namespace media { |
13 static const int kY4MHeaderMaxSize = 200; | 14 static const int kY4MHeaderMaxSize = 200; |
14 static const char kY4MSimpleFrameDelimiter[] = "FRAME"; | 15 static const char kY4MSimpleFrameDelimiter[] = "FRAME"; |
15 static const int kY4MSimpleFrameDelimiterSize = 6; | 16 static const int kY4MSimpleFrameDelimiterSize = 6; |
17 static const int kMJpegFrameRate = 30; | |
16 | 18 |
17 int ParseY4MInt(const base::StringPiece& token) { | 19 static int ParseY4MInt(const base::StringPiece& token) { |
mcasas
2015/08/18 18:28:42
IIRC, media/ folder has this strange thing
of _not
henryhsu
2015/08/19 01:57:24
Hi xhwang, do you know this?
| |
18 int temp_int; | 20 int temp_int; |
19 CHECK(base::StringToInt(token, &temp_int)) << token; | 21 CHECK(base::StringToInt(token, &temp_int)) << token; |
20 return temp_int; | 22 return temp_int; |
21 } | 23 } |
22 | 24 |
23 // Extract numerator and denominator out of a token that must have the aspect | 25 // Extract numerator and denominator out of a token that must have the aspect |
24 // numerator:denominator, both integer numbers. | 26 // numerator:denominator, both integer numbers. |
25 void ParseY4MRational(const base::StringPiece& token, | 27 static void ParseY4MRational(const base::StringPiece& token, |
26 int* numerator, | 28 int* numerator, |
27 int* denominator) { | 29 int* denominator) { |
28 size_t index_divider = token.find(':'); | 30 size_t index_divider = token.find(':'); |
29 CHECK_NE(index_divider, token.npos); | 31 CHECK_NE(index_divider, token.npos); |
30 *numerator = ParseY4MInt(token.substr(0, index_divider)); | 32 *numerator = ParseY4MInt(token.substr(0, index_divider)); |
31 *denominator = ParseY4MInt(token.substr(index_divider + 1, token.length())); | 33 *denominator = ParseY4MInt(token.substr(index_divider + 1, token.length())); |
32 CHECK(*denominator); | 34 CHECK(*denominator); |
33 } | 35 } |
34 | 36 |
35 // This function parses the ASCII string in |header| as belonging to a Y4M file, | 37 // This function parses the ASCII string in |header| as belonging to a Y4M file, |
36 // returning the collected format in |video_format|. For a non authoritative | 38 // returning the collected format in |video_format|. For a non authoritative |
37 // explanation of the header format, check | 39 // explanation of the header format, check |
38 // http://wiki.multimedia.cx/index.php?title=YUV4MPEG2 | 40 // http://wiki.multimedia.cx/index.php?title=YUV4MPEG2 |
39 // Restrictions: Only interlaced I420 pixel format is supported, and pixel | 41 // Restrictions: Only interlaced I420 pixel format is supported, and pixel |
40 // aspect ratio is ignored. | 42 // aspect ratio is ignored. |
41 // Implementation notes: Y4M header should end with an ASCII 0x20 (whitespace) | 43 // Implementation notes: Y4M header should end with an ASCII 0x20 (whitespace) |
42 // character, however all examples mentioned in the Y4M header description end | 44 // character, however all examples mentioned in the Y4M header description end |
43 // with a newline character instead. Also, some headers do _not_ specify pixel | 45 // with a newline character instead. Also, some headers do _not_ specify pixel |
44 // format, in this case it means I420. | 46 // format, in this case it means I420. |
45 // This code was inspired by third_party/libvpx/.../y4minput.* . | 47 // This code was inspired by third_party/libvpx/.../y4minput.* . |
46 void ParseY4MTags(const std::string& file_header, | 48 static bool ParseY4MTags(const std::string& file_header, |
47 media::VideoCaptureFormat* video_format) { | 49 media::VideoCaptureFormat* video_format) { |
48 video_format->pixel_format = media::VIDEO_CAPTURE_PIXEL_FORMAT_I420; | 50 media::VideoCaptureFormat format; |
49 video_format->frame_size.set_width(0); | 51 format.pixel_format = media::VIDEO_CAPTURE_PIXEL_FORMAT_I420; |
50 video_format->frame_size.set_height(0); | |
51 size_t index = 0; | 52 size_t index = 0; |
52 size_t blank_position = 0; | 53 size_t blank_position = 0; |
53 base::StringPiece token; | 54 base::StringPiece token; |
54 while ((blank_position = file_header.find_first_of("\n ", index)) != | 55 while ((blank_position = file_header.find_first_of("\n ", index)) != |
55 std::string::npos) { | 56 std::string::npos) { |
56 // Every token is supposed to have an identifier letter and a bunch of | 57 // Every token is supposed to have an identifier letter and a bunch of |
57 // information immediately after, which we extract into a |token| here. | 58 // information immediately after, which we extract into a |token| here. |
58 token = | 59 token = |
59 base::StringPiece(&file_header[index + 1], blank_position - index - 1); | 60 base::StringPiece(&file_header[index + 1], blank_position - index - 1); |
60 CHECK(!token.empty()); | 61 if (token.empty()) |
62 return false; | |
61 switch (file_header[index]) { | 63 switch (file_header[index]) { |
62 case 'W': | 64 case 'W': |
63 video_format->frame_size.set_width(ParseY4MInt(token)); | 65 format.frame_size.set_width(ParseY4MInt(token)); |
64 break; | 66 break; |
65 case 'H': | 67 case 'H': |
66 video_format->frame_size.set_height(ParseY4MInt(token)); | 68 format.frame_size.set_height(ParseY4MInt(token)); |
67 break; | 69 break; |
68 case 'F': { | 70 case 'F': { |
69 // If the token is "FRAME", it means we have finished with the header. | 71 // If the token is "FRAME", it means we have finished with the header. |
70 if (token[0] == 'R') | 72 if (token[0] == 'R') |
71 break; | 73 break; |
72 int fps_numerator, fps_denominator; | 74 int fps_numerator, fps_denominator; |
73 ParseY4MRational(token, &fps_numerator, &fps_denominator); | 75 ParseY4MRational(token, &fps_numerator, &fps_denominator); |
74 video_format->frame_rate = fps_numerator / fps_denominator; | 76 format.frame_rate = fps_numerator / fps_denominator; |
75 break; | 77 break; |
76 } | 78 } |
77 case 'I': | 79 case 'I': |
78 // Interlacing is ignored, but we don't like mixed modes. | 80 // Interlacing is ignored, but we don't like mixed modes. |
79 CHECK_NE(token[0], 'm'); | 81 if (token[0] == 'm') |
80 break; | 82 return false; |
81 case 'A': | 83 case 'A': |
82 // Pixel aspect ratio ignored. | 84 // Pixel aspect ratio ignored. |
83 break; | 85 break; |
84 case 'C': | 86 case 'C': |
85 CHECK(token == "420" || token == "420jpeg" || token == "420paldv") | 87 // Only I420 is supported, and we fudge the variants. |
86 << token; // Only I420 is supported, and we fudge the variants. | 88 if (token != "420" && token != "420jpeg" && token != "420paldv") |
87 break; | 89 return false; |
88 default: | 90 default: |
89 break; | 91 break; |
90 } | 92 } |
91 // We're done if we have found a newline character right after the token. | 93 // We're done if we have found a newline character right after the token. |
92 if (file_header[blank_position] == '\n') | 94 if (file_header[blank_position] == '\n') |
93 break; | 95 break; |
94 index = blank_position + 1; | 96 index = blank_position + 1; |
95 } | 97 } |
96 // Last video format semantic correctness check before sending it back. | 98 // Last video format semantic correctness check before sending it back. |
97 CHECK(video_format->IsValid()); | 99 if (!format.IsValid()) |
mcasas
2015/08/18 18:28:42
The original CHECK() was correct (and hammered by
henryhsu
2015/08/19 01:57:24
Current implementation is to parse Y4M and MJPEG.
| |
100 return false; | |
101 *video_format = format; | |
102 return true; | |
98 } | 103 } |
99 | 104 |
100 // Reads and parses the header of a Y4M |file|, returning the collected pixel | |
101 // format in |video_format|. Returns the index of the first byte of the first | |
102 // video frame. | |
103 // Restrictions: Only trivial per-frame headers are supported. | |
104 // static | 105 // static |
105 int64 FileVideoCaptureDevice::ParseFileAndExtractVideoFormat( | 106 size_t FileVideoCaptureDevice::ParseFileAndExtractVideoFormat( |
106 base::File* file, | 107 const base::MemoryMappedFile* mapped_file, |
107 media::VideoCaptureFormat* video_format) { | 108 media::VideoCaptureFormat* video_format, |
108 std::string header(kY4MHeaderMaxSize, 0); | 109 int* frame_size) { |
109 file->Read(0, &header[0], kY4MHeaderMaxSize - 1); | 110 const char* buf = reinterpret_cast<const char*>(mapped_file->data()); |
110 | 111 |
112 // Try Y4M format first. | |
113 std::string header(buf, kY4MHeaderMaxSize - 1); | |
mcasas
2015/08/18 18:28:42
const (maybe)
| |
111 size_t header_end = header.find(kY4MSimpleFrameDelimiter); | 114 size_t header_end = header.find(kY4MSimpleFrameDelimiter); |
mcasas
2015/08/18 18:28:41
const
henryhsu
2015/08/19 10:05:11
Done.
| |
112 CHECK_NE(header_end, header.npos); | 115 if (header_end != header.npos && ParseY4MTags(header, video_format)) { |
116 *frame_size = video_format->ImageAllocationSize(); | |
117 return header_end + kY4MSimpleFrameDelimiterSize; | |
118 } | |
113 | 119 |
114 ParseY4MTags(header, video_format); | 120 // Try MJPEG format. |
115 return header_end + kY4MSimpleFrameDelimiterSize; | 121 JpegParseResult result; |
122 if (!ParseJpegPicture(mapped_file->data(), mapped_file->length(), &result)) | |
123 return -1; | |
124 video_format->pixel_format = media::VIDEO_CAPTURE_PIXEL_FORMAT_MJPEG; | |
125 video_format->frame_size.set_width(result.frame_header.visible_width); | |
126 video_format->frame_size.set_height(result.frame_header.visible_height); | |
127 video_format->frame_rate = kMJpegFrameRate; | |
128 if (!video_format->IsValid()) | |
129 return -1; | |
130 *frame_size = result.image_size; | |
131 return 0; | |
116 } | 132 } |
117 | 133 |
118 // Opens a given file for reading, and returns the file to the caller, who is | |
119 // responsible for closing it. | |
120 // static | 134 // static |
121 base::File FileVideoCaptureDevice::OpenFileForRead( | 135 scoped_ptr<base::MemoryMappedFile> FileVideoCaptureDevice::OpenFileForRead( |
122 const base::FilePath& file_path) { | 136 const base::FilePath& file_path) { |
123 base::File file(file_path, base::File::FLAG_OPEN | base::File::FLAG_READ); | 137 scoped_ptr<base::MemoryMappedFile> mapped_file(new base::MemoryMappedFile()); |
124 DLOG_IF(ERROR, file.IsValid()) | 138 |
125 << file_path.value() | 139 if (!mapped_file->Initialize(file_path) || !mapped_file->IsValid()) { |
126 << ", error: " << base::File::ErrorToString(file.error_details()); | 140 LOG(ERROR) << "File memory map error: " << file_path.value(); |
127 return file.Pass(); | 141 return nullptr; |
142 } | |
143 return mapped_file.Pass(); | |
128 } | 144 } |
129 | 145 |
130 FileVideoCaptureDevice::FileVideoCaptureDevice(const base::FilePath& file_path) | 146 FileVideoCaptureDevice::FileVideoCaptureDevice(const base::FilePath& file_path) |
131 : capture_thread_("CaptureThread"), | 147 : capture_thread_("CaptureThread"), |
132 file_path_(file_path), | 148 file_path_(file_path), |
133 frame_size_(0), | 149 frame_size_(0), |
134 current_byte_index_(0), | 150 current_byte_index_(0), |
135 first_frame_byte_index_(0) { | 151 first_frame_byte_index_(0) { |
136 } | 152 } |
137 | 153 |
(...skipping 20 matching lines...) Expand all Loading... | |
158 void FileVideoCaptureDevice::StopAndDeAllocate() { | 174 void FileVideoCaptureDevice::StopAndDeAllocate() { |
159 DCHECK(thread_checker_.CalledOnValidThread()); | 175 DCHECK(thread_checker_.CalledOnValidThread()); |
160 CHECK(capture_thread_.IsRunning()); | 176 CHECK(capture_thread_.IsRunning()); |
161 | 177 |
162 capture_thread_.message_loop()->PostTask( | 178 capture_thread_.message_loop()->PostTask( |
163 FROM_HERE, base::Bind(&FileVideoCaptureDevice::OnStopAndDeAllocate, | 179 FROM_HERE, base::Bind(&FileVideoCaptureDevice::OnStopAndDeAllocate, |
164 base::Unretained(this))); | 180 base::Unretained(this))); |
165 capture_thread_.Stop(); | 181 capture_thread_.Stop(); |
166 } | 182 } |
167 | 183 |
168 int FileVideoCaptureDevice::CalculateFrameSize() const { | |
169 DCHECK_EQ(capture_format_.pixel_format, VIDEO_CAPTURE_PIXEL_FORMAT_I420); | |
170 DCHECK_EQ(capture_thread_.message_loop(), base::MessageLoop::current()); | |
171 return capture_format_.ImageAllocationSize(); | |
172 } | |
173 | |
174 void FileVideoCaptureDevice::OnAllocateAndStart( | 184 void FileVideoCaptureDevice::OnAllocateAndStart( |
175 const VideoCaptureParams& params, | 185 const VideoCaptureParams& params, |
176 scoped_ptr<VideoCaptureDevice::Client> client) { | 186 scoped_ptr<VideoCaptureDevice::Client> client) { |
177 DCHECK_EQ(capture_thread_.message_loop(), base::MessageLoop::current()); | 187 DCHECK_EQ(capture_thread_.message_loop(), base::MessageLoop::current()); |
178 | 188 |
179 client_ = client.Pass(); | 189 client_ = client.Pass(); |
180 | 190 |
181 // Open the file and parse the header. Get frame size and format. | 191 // Open the file and parse the header. Get frame size and format. |
182 DCHECK(!file_.IsValid()); | 192 DCHECK(!mapped_file_); |
183 file_ = OpenFileForRead(file_path_); | 193 mapped_file_ = OpenFileForRead(file_path_); |
184 if (!file_.IsValid()) { | 194 if (!mapped_file_) { |
185 client_->OnError("Could not open Video file"); | 195 client_->OnError("Could not open Video file"); |
186 return; | 196 return; |
187 } | 197 } |
188 first_frame_byte_index_ = | 198 first_frame_byte_index_ = ParseFileAndExtractVideoFormat( |
189 ParseFileAndExtractVideoFormat(&file_, &capture_format_); | 199 mapped_file_.get(), &capture_format_, &frame_size_); |
200 | |
201 if (first_frame_byte_index_ < 0 || | |
202 first_frame_byte_index_ + frame_size_ > mapped_file_->length()) { | |
203 client_->OnError("File is incomplete"); | |
204 return; | |
205 } | |
190 current_byte_index_ = first_frame_byte_index_; | 206 current_byte_index_ = first_frame_byte_index_; |
191 DVLOG(1) << "Opened video file " << capture_format_.frame_size.ToString() | 207 DVLOG(1) << "Opened video file " << capture_format_.frame_size.ToString() |
192 << ", fps: " << capture_format_.frame_rate; | 208 << ", fps: " << capture_format_.frame_rate; |
193 | 209 |
194 frame_size_ = CalculateFrameSize(); | |
195 video_frame_.reset(new uint8[frame_size_]); | |
196 | |
197 capture_thread_.message_loop()->PostTask( | 210 capture_thread_.message_loop()->PostTask( |
198 FROM_HERE, base::Bind(&FileVideoCaptureDevice::OnCaptureTask, | 211 FROM_HERE, base::Bind(&FileVideoCaptureDevice::OnCaptureTask, |
199 base::Unretained(this))); | 212 base::Unretained(this))); |
200 } | 213 } |
201 | 214 |
202 void FileVideoCaptureDevice::OnStopAndDeAllocate() { | 215 void FileVideoCaptureDevice::OnStopAndDeAllocate() { |
203 DCHECK_EQ(capture_thread_.message_loop(), base::MessageLoop::current()); | 216 DCHECK_EQ(capture_thread_.message_loop(), base::MessageLoop::current()); |
204 file_.Close(); | 217 mapped_file_.reset(); |
205 client_.reset(); | 218 client_.reset(); |
206 current_byte_index_ = 0; | 219 current_byte_index_ = 0; |
207 first_frame_byte_index_ = 0; | 220 first_frame_byte_index_ = 0; |
208 frame_size_ = 0; | 221 frame_size_ = 0; |
209 next_frame_time_ = base::TimeTicks(); | 222 next_frame_time_ = base::TimeTicks(); |
210 video_frame_.reset(); | 223 } |
224 | |
225 const uint8_t* FileVideoCaptureDevice::GetNextFrame() { | |
226 DCHECK_EQ(capture_thread_.message_loop(), base::MessageLoop::current()); | |
227 const uint8_t* buf_ptr = mapped_file_->data() + current_byte_index_; | |
228 | |
229 if (capture_format_.pixel_format == media::VIDEO_CAPTURE_PIXEL_FORMAT_MJPEG) { | |
230 JpegParseResult result; | |
231 if (!ParseJpegPicture(buf_ptr, mapped_file_->length() - current_byte_index_, | |
232 &result)) { | |
233 return nullptr; | |
234 } | |
235 // TODO(henryhsu): Update |capture_format_| if video has resolution change. | |
236 frame_size_ = result.image_size; | |
237 current_byte_index_ += frame_size_; | |
238 } else if (capture_format_.pixel_format == | |
239 media::VIDEO_CAPTURE_PIXEL_FORMAT_I420) { | |
240 current_byte_index_ += frame_size_ + kY4MSimpleFrameDelimiterSize; | |
241 } else { | |
242 NOTREACHED() << "Not supported format: " << capture_format_.pixel_format; | |
243 } | |
244 // Play the video repeatly. | |
mcasas
2015/08/18 18:28:42
s/repeatly/repeatedly/
but I'd just remove this co
henryhsu
2015/08/19 10:05:11
Done.
| |
245 if (current_byte_index_ >= mapped_file_->length()) | |
246 current_byte_index_ = first_frame_byte_index_; | |
247 return buf_ptr; | |
211 } | 248 } |
212 | 249 |
213 void FileVideoCaptureDevice::OnCaptureTask() { | 250 void FileVideoCaptureDevice::OnCaptureTask() { |
214 DCHECK_EQ(capture_thread_.message_loop(), base::MessageLoop::current()); | 251 DCHECK_EQ(capture_thread_.message_loop(), base::MessageLoop::current()); |
215 if (!client_) | 252 if (!client_) |
216 return; | 253 return; |
217 int result = | |
218 file_.Read(current_byte_index_, | |
219 reinterpret_cast<char*>(video_frame_.get()), frame_size_); | |
220 | |
221 // If we passed EOF to base::File, it will return 0 read characters. In that | |
222 // case, reset the pointer and read again. | |
223 if (result != frame_size_) { | |
224 CHECK_EQ(result, 0); | |
225 current_byte_index_ = first_frame_byte_index_; | |
226 CHECK_EQ( | |
227 file_.Read(current_byte_index_, | |
228 reinterpret_cast<char*>(video_frame_.get()), frame_size_), | |
229 frame_size_); | |
230 } else { | |
231 current_byte_index_ += frame_size_ + kY4MSimpleFrameDelimiterSize; | |
232 } | |
233 | 254 |
234 // Give the captured frame to the client. | 255 // Give the captured frame to the client. |
256 const uint8_t* frame_ptr = GetNextFrame(); | |
257 CHECK(frame_ptr); | |
235 const base::TimeTicks current_time = base::TimeTicks::Now(); | 258 const base::TimeTicks current_time = base::TimeTicks::Now(); |
236 client_->OnIncomingCapturedData(video_frame_.get(), frame_size_, | 259 client_->OnIncomingCapturedData(frame_ptr, frame_size_, capture_format_, 0, |
237 capture_format_, 0, current_time); | 260 current_time); |
238 // Reschedule next CaptureTask. | 261 // Reschedule next CaptureTask. |
239 const base::TimeDelta frame_interval = | 262 const base::TimeDelta frame_interval = |
240 base::TimeDelta::FromMicroseconds(1E6 / capture_format_.frame_rate); | 263 base::TimeDelta::FromMicroseconds(1E6 / capture_format_.frame_rate); |
241 if (next_frame_time_.is_null()) { | 264 if (next_frame_time_.is_null()) { |
242 next_frame_time_ = current_time + frame_interval; | 265 next_frame_time_ = current_time + frame_interval; |
243 } else { | 266 } else { |
244 next_frame_time_ += frame_interval; | 267 next_frame_time_ += frame_interval; |
245 // Don't accumulate any debt if we are lagging behind - just post next frame | 268 // Don't accumulate any debt if we are lagging behind - just post next frame |
246 // immediately and continue as normal. | 269 // immediately and continue as normal. |
247 if (next_frame_time_ < current_time) | 270 if (next_frame_time_ < current_time) |
248 next_frame_time_ = current_time; | 271 next_frame_time_ = current_time; |
249 } | 272 } |
250 base::MessageLoop::current()->PostDelayedTask( | 273 base::MessageLoop::current()->PostDelayedTask( |
251 FROM_HERE, base::Bind(&FileVideoCaptureDevice::OnCaptureTask, | 274 FROM_HERE, base::Bind(&FileVideoCaptureDevice::OnCaptureTask, |
252 base::Unretained(this)), | 275 base::Unretained(this)), |
253 next_frame_time_ - current_time); | 276 next_frame_time_ - current_time); |
254 } | 277 } |
255 | 278 |
256 } // namespace media | 279 } // namespace media |
OLD | NEW |