Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2406)

Side by Side Diff: media/capture/video/file_video_capture_device.cc

Issue 1291933002: File video capture device supports MJPEG format (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 = 25;
16 18
17 int ParseY4MInt(const base::StringPiece& token) { 19 int ParseY4MInt(const base::StringPiece& token) {
kcwu 2015/08/17 06:24:13 Enclose this and other functions in this file into
henryhsu 2015/08/17 06:52:11 Done.
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 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 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 video_format->pixel_format = media::VIDEO_CAPTURE_PIXEL_FORMAT_I420;
49 video_format->frame_size.set_width(0); 51 video_format->frame_size.set_width(0);
50 video_format->frame_size.set_height(0); 52 video_format->frame_size.set_height(0);
51 size_t index = 0; 53 size_t index = 0;
52 size_t blank_position = 0; 54 size_t blank_position = 0;
53 base::StringPiece token; 55 base::StringPiece token;
54 while ((blank_position = file_header.find_first_of("\n ", index)) != 56 while ((blank_position = file_header.find_first_of("\n ", index)) !=
55 std::string::npos) { 57 std::string::npos) {
56 // Every token is supposed to have an identifier letter and a bunch of 58 // Every token is supposed to have an identifier letter and a bunch of
57 // information immediately after, which we extract into a |token| here. 59 // information immediately after, which we extract into a |token| here.
58 token = 60 token =
59 base::StringPiece(&file_header[index + 1], blank_position - index - 1); 61 base::StringPiece(&file_header[index + 1], blank_position - index - 1);
60 CHECK(!token.empty()); 62 if (token.empty())
63 return false;
61 switch (file_header[index]) { 64 switch (file_header[index]) {
62 case 'W': 65 case 'W':
63 video_format->frame_size.set_width(ParseY4MInt(token)); 66 video_format->frame_size.set_width(ParseY4MInt(token));
64 break; 67 break;
65 case 'H': 68 case 'H':
66 video_format->frame_size.set_height(ParseY4MInt(token)); 69 video_format->frame_size.set_height(ParseY4MInt(token));
67 break; 70 break;
68 case 'F': { 71 case 'F': {
69 // If the token is "FRAME", it means we have finished with the header. 72 // If the token is "FRAME", it means we have finished with the header.
70 if (token[0] == 'R') 73 if (token[0] == 'R')
71 break; 74 break;
72 int fps_numerator, fps_denominator; 75 int fps_numerator, fps_denominator;
73 ParseY4MRational(token, &fps_numerator, &fps_denominator); 76 ParseY4MRational(token, &fps_numerator, &fps_denominator);
74 video_format->frame_rate = fps_numerator / fps_denominator; 77 video_format->frame_rate = fps_numerator / fps_denominator;
75 break; 78 break;
76 } 79 }
77 case 'I': 80 case 'I':
78 // Interlacing is ignored, but we don't like mixed modes. 81 // Interlacing is ignored, but we don't like mixed modes.
79 CHECK_NE(token[0], 'm'); 82 if (token[0] == 'm')
80 break; 83 return false;
81 case 'A': 84 case 'A':
82 // Pixel aspect ratio ignored. 85 // Pixel aspect ratio ignored.
83 break; 86 break;
84 case 'C': 87 case 'C':
85 CHECK(token == "420" || token == "420jpeg" || token == "420paldv") 88 // Only I420 is supported, and we fudge the variants.
86 << token; // Only I420 is supported, and we fudge the variants. 89 if (token != "420" && token != "420jpeg" && token != "420paldv")
87 break; 90 return false;
88 default: 91 default:
89 break; 92 break;
90 } 93 }
91 // We're done if we have found a newline character right after the token. 94 // We're done if we have found a newline character right after the token.
92 if (file_header[blank_position] == '\n') 95 if (file_header[blank_position] == '\n')
93 break; 96 break;
94 index = blank_position + 1; 97 index = blank_position + 1;
95 } 98 }
96 // Last video format semantic correctness check before sending it back. 99 // Last video format semantic correctness check before sending it back.
97 CHECK(video_format->IsValid()); 100 return video_format->IsValid();
98 } 101 }
99 102
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 103 // static
105 int64 FileVideoCaptureDevice::ParseFileAndExtractVideoFormat( 104 size_t FileVideoCaptureDevice::ParseFileAndExtractVideoFormat(
106 base::File* file, 105 const base::MemoryMappedFile* mapped_file,
107 media::VideoCaptureFormat* video_format) { 106 media::VideoCaptureFormat* video_format,
108 std::string header(kY4MHeaderMaxSize, 0); 107 int* frame_size) {
109 file->Read(0, &header[0], kY4MHeaderMaxSize - 1); 108 const char* buf = reinterpret_cast<const char*>(mapped_file->data());
110 109
110 // Try Y4M format first.
111 std::string header(buf, kY4MHeaderMaxSize - 1);
111 size_t header_end = header.find(kY4MSimpleFrameDelimiter); 112 size_t header_end = header.find(kY4MSimpleFrameDelimiter);
112 CHECK_NE(header_end, header.npos); 113 if (header_end != header.npos && ParseY4MTags(header, video_format)) {
114 *frame_size = video_format->ImageAllocationSize();
115 return header_end + kY4MSimpleFrameDelimiterSize;
116 }
113 117
114 ParseY4MTags(header, video_format); 118 // Try MJPEG format.
115 return header_end + kY4MSimpleFrameDelimiterSize; 119 JpegParseResult result;
120 if (!ParseJpegPicture(mapped_file->data(), mapped_file->length(), &result))
121 return -1;
122 video_format->pixel_format = media::VIDEO_CAPTURE_PIXEL_FORMAT_MJPEG;
123 video_format->frame_size.set_width(result.frame_header.visible_width);
124 video_format->frame_size.set_height(result.frame_header.visible_height);
125 video_format->frame_rate = kMJpegFrameRate;
126 if (!video_format->IsValid())
127 return -1;
128 *frame_size = result.image_size;
129 return 0;
116 } 130 }
117 131
118 // Opens a given file for reading, and returns the file to the caller, who is
119 // responsible for closing it.
120 // static 132 // static
121 base::File FileVideoCaptureDevice::OpenFileForRead( 133 scoped_ptr<base::MemoryMappedFile> FileVideoCaptureDevice::OpenFileForRead(
122 const base::FilePath& file_path) { 134 const base::FilePath& file_path) {
123 base::File file(file_path, base::File::FLAG_OPEN | base::File::FLAG_READ); 135 scoped_ptr<base::MemoryMappedFile> mapped_file;
124 DLOG_IF(ERROR, file.IsValid()) 136 mapped_file.reset(new base::MemoryMappedFile());
125 << file_path.value() 137 if (!mapped_file)
126 << ", error: " << base::File::ErrorToString(file.error_details()); 138 return nullptr;
127 return file.Pass(); 139
140 if (!mapped_file->Initialize(file_path)) {
141 LOG(ERROR) << "File memory map error: " << file_path.value();
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
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_ || !mapped_file_->IsValid());
183 file_ = OpenFileForRead(file_path_); 193 mapped_file_ = OpenFileForRead(file_path_);
184 if (!file_.IsValid()) { 194 if (!mapped_file_ || !mapped_file_->IsValid()) {
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 int image_size = 0;
229
230 if (capture_format_.pixel_format == media::VIDEO_CAPTURE_PIXEL_FORMAT_MJPEG) {
231 JpegParseResult result;
232 if (!ParseJpegPicture(mapped_file_->data() + current_byte_index_,
233 mapped_file_->length() - current_byte_index_,
234 &result)) {
235 return nullptr;
236 }
237 // TODO(henryhsu): Update |capture_format_| if video has resolution change.
238 frame_size_ = result.image_size;
239 image_size = frame_size_;
240 } else if (capture_format_.pixel_format ==
241 media::VIDEO_CAPTURE_PIXEL_FORMAT_I420) {
242 image_size = frame_size_ + kY4MSimpleFrameDelimiterSize;
243 } else {
244 NOTREACHED() << "Not supported format: " << capture_format_.pixel_format;
245 }
246 current_byte_index_ += image_size;
247 // Play the video repeatly.
248 if (current_byte_index_ >= mapped_file_->length())
249 current_byte_index_ = first_frame_byte_index_;
250 return buf_ptr;
211 } 251 }
212 252
213 void FileVideoCaptureDevice::OnCaptureTask() { 253 void FileVideoCaptureDevice::OnCaptureTask() {
214 DCHECK_EQ(capture_thread_.message_loop(), base::MessageLoop::current()); 254 DCHECK_EQ(capture_thread_.message_loop(), base::MessageLoop::current());
215 if (!client_) 255 if (!client_)
216 return; 256 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 257
234 // Give the captured frame to the client. 258 // Give the captured frame to the client.
259 const uint8_t* frame_ptr = GetNextFrame();
260 CHECK(frame_ptr);
235 const base::TimeTicks current_time = base::TimeTicks::Now(); 261 const base::TimeTicks current_time = base::TimeTicks::Now();
236 client_->OnIncomingCapturedData(video_frame_.get(), frame_size_, 262 client_->OnIncomingCapturedData(frame_ptr, frame_size_, capture_format_, 0,
237 capture_format_, 0, current_time); 263 current_time);
238 // Reschedule next CaptureTask. 264 // Reschedule next CaptureTask.
239 const base::TimeDelta frame_interval = 265 const base::TimeDelta frame_interval =
240 base::TimeDelta::FromMicroseconds(1E6 / capture_format_.frame_rate); 266 base::TimeDelta::FromMicroseconds(1E6 / capture_format_.frame_rate);
241 if (next_frame_time_.is_null()) { 267 if (next_frame_time_.is_null()) {
242 next_frame_time_ = current_time + frame_interval; 268 next_frame_time_ = current_time + frame_interval;
243 } else { 269 } else {
244 next_frame_time_ += frame_interval; 270 next_frame_time_ += frame_interval;
245 // Don't accumulate any debt if we are lagging behind - just post next frame 271 // Don't accumulate any debt if we are lagging behind - just post next frame
246 // immediately and continue as normal. 272 // immediately and continue as normal.
247 if (next_frame_time_ < current_time) 273 if (next_frame_time_ < current_time)
248 next_frame_time_ = current_time; 274 next_frame_time_ = current_time;
249 } 275 }
250 base::MessageLoop::current()->PostDelayedTask( 276 base::MessageLoop::current()->PostDelayedTask(
251 FROM_HERE, base::Bind(&FileVideoCaptureDevice::OnCaptureTask, 277 FROM_HERE, base::Bind(&FileVideoCaptureDevice::OnCaptureTask,
252 base::Unretained(this)), 278 base::Unretained(this)),
253 next_frame_time_ - current_time); 279 next_frame_time_ - current_time);
254 } 280 }
255 281
256 } // namespace media 282 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698