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

Side by Side Diff: media/filters/jpeg_parser.cc

Issue 1291933002: File video capture device supports MJPEG format (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address wucheng@ 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 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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/filters/jpeg_parser.h" 5 #include "media/filters/jpeg_parser.h"
6 6
7 #include "base/big_endian.h" 7 #include "base/big_endian.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 9
10 using base::BigEndianReader; 10 using base::BigEndianReader;
(...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after
262 return false; 262 return false;
263 } 263 }
264 if (point_transform != 0) { 264 if (point_transform != 0) {
265 DLOG(ERROR) << "Point transform should be 0 for baseline mode"; 265 DLOG(ERROR) << "Point transform should be 0 for baseline mode";
266 return false; 266 return false;
267 } 267 }
268 268
269 return true; 269 return true;
270 } 270 }
271 271
272 // |eoi_ptr| is pointing to the end of image (after EOI marker).
xhwang 2015/08/18 17:17:55 This comment is ambiguous. I suppose you mean that
henryhsu 2015/08/19 01:57:24 yes. thanks for correction
273 static bool SearchEOI(const char* buffer,
xhwang 2015/08/18 17:17:55 What does the return value mean?
henryhsu 2015/08/19 01:57:24 The return value means search EOI succeed or not.
274 size_t length,
xhwang 2015/08/18 17:17:55 Is the indent off by 1 char? Try run "git cl forma
henryhsu 2015/08/19 10:05:11 Done.
275 const char** eoi_ptr) {
276 DCHECK(buffer);
277 DCHECK(eoi_ptr);
278 BigEndianReader reader(buffer, length);
279 uint8_t marker1;
280 uint8_t marker2;
281
282 while (reader.remaining() > 0) {
283 READ_U8_OR_RETURN_FALSE(&marker1);
284 if (marker1 != JPEG_MARKER_PREFIX)
285 continue;
286
287 do {
288 READ_U8_OR_RETURN_FALSE(&marker2);
289 } while (marker2 == JPEG_MARKER_PREFIX); // skip fill bytes
290
291 switch (marker2) {
292 // Compressed data escape.
293 case 0x00:
294 break;
295 // Restart
296 case JPEG_RST0:
297 case JPEG_RST1:
298 case JPEG_RST2:
299 case JPEG_RST3:
300 case JPEG_RST4:
301 case JPEG_RST5:
302 case JPEG_RST6:
303 case JPEG_RST7:
304 break;
305 case JPEG_EOI:
306 *eoi_ptr = reader.ptr();
307 return true;
308 default:
309 // Skip for other markers.
310 uint16_t size;
311 READ_U16_OR_RETURN_FALSE(&size);
312 if (size < sizeof(size)) {
313 DLOG(ERROR) << "Ill-formed JPEG. Segment size (" << size
314 << ") is smaller than size field (" << sizeof(size)
315 << ")";
316 return false;
317 }
318 size -= sizeof(size);
319
320 if (!reader.Skip(size)) {
321 DLOG(ERROR) << "Ill-formed JPEG. Remaining size ("
322 << reader.remaining()
323 << ") is smaller than header specified (" << size << ")";
324 return false;
325 }
326 break;
327 }
328 }
329 return false;
330 }
331
272 // |result| is already initialized to 0 in ParseJpegPicture. 332 // |result| is already initialized to 0 in ParseJpegPicture.
273 static bool ParseSOI(const char* buffer, 333 static bool ParseSOI(const char* buffer,
274 size_t length, 334 size_t length,
275 JpegParseResult* result) { 335 JpegParseResult* result) {
276 // Spec B.2.1 High-level syntax 336 // Spec B.2.1 High-level syntax
277 DCHECK(buffer); 337 DCHECK(buffer);
278 DCHECK(result); 338 DCHECK(result);
279 BigEndianReader reader(buffer, length); 339 BigEndianReader reader(buffer, length);
280 uint8_t marker1; 340 uint8_t marker1;
281 uint8_t marker2; 341 uint8_t marker2;
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
364 reader.Skip(size); 424 reader.Skip(size);
365 } 425 }
366 426
367 if (!has_marker_dqt) { 427 if (!has_marker_dqt) {
368 DLOG(ERROR) << "No DQT marker found"; 428 DLOG(ERROR) << "No DQT marker found";
369 return false; 429 return false;
370 } 430 }
371 431
372 // Scan data follows scan header immediately. 432 // Scan data follows scan header immediately.
373 result->data = reader.ptr(); 433 result->data = reader.ptr();
374 result->data_size = reader.remaining(); 434 const char* eoi_ptr = nullptr;
435 if (!SearchEOI(reader.ptr(), reader.remaining(), &eoi_ptr)) {
436 DLOG(ERROR) << "SearchEOI failed";
437 return false;
438 }
439 result->data_size = eoi_ptr - result->data;
440 const int kSoiSize = 2;
441 result->image_size = eoi_ptr - buffer + kSoiSize;
375 442
376 return true; 443 return true;
377 } 444 }
378 445
379 bool ParseJpegPicture(const uint8_t* buffer, 446 bool ParseJpegPicture(const uint8_t* buffer,
380 size_t length, 447 size_t length,
381 JpegParseResult* result) { 448 JpegParseResult* result) {
382 DCHECK(buffer); 449 DCHECK(buffer);
383 DCHECK(result); 450 DCHECK(result);
384 BigEndianReader reader(reinterpret_cast<const char*>(buffer), length); 451 BigEndianReader reader(reinterpret_cast<const char*>(buffer), length);
385 memset(result, 0, sizeof(JpegParseResult)); 452 memset(result, 0, sizeof(JpegParseResult));
386 453
387 uint8_t marker1, marker2; 454 uint8_t marker1, marker2;
388 READ_U8_OR_RETURN_FALSE(&marker1); 455 READ_U8_OR_RETURN_FALSE(&marker1);
389 READ_U8_OR_RETURN_FALSE(&marker2); 456 READ_U8_OR_RETURN_FALSE(&marker2);
390 if (marker1 != JPEG_MARKER_PREFIX || marker2 != JPEG_SOI) { 457 if (marker1 != JPEG_MARKER_PREFIX || marker2 != JPEG_SOI) {
391 DLOG(ERROR) << "Not a JPEG"; 458 DLOG(ERROR) << "Not a JPEG";
392 return false; 459 return false;
393 } 460 }
394 461
395 return ParseSOI(reader.ptr(), reader.remaining(), result); 462 return ParseSOI(reader.ptr(), reader.remaining(), result);
396 } 463 }
397 464
398 } // namespace media 465 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698