OLD | NEW |
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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 DQT = 0xDB, // define quantization table | 42 DQT = 0xDB, // define quantization table |
43 DRI = 0xDD, // define restart internal | 43 DRI = 0xDD, // define restart internal |
44 MARKER1 = 0xFF, // jpeg marker prefix | 44 MARKER1 = 0xFF, // jpeg marker prefix |
45 }; | 45 }; |
46 } | 46 } |
47 | 47 |
48 static bool InRange(int value, int a, int b) { | 48 static bool InRange(int value, int a, int b) { |
49 return a <= value && value <= b; | 49 return a <= value && value <= b; |
50 } | 50 } |
51 | 51 |
| 52 static int RoundUp(int value, int mul) { |
| 53 return (value + mul - 1) / mul * mul; |
| 54 } |
| 55 |
52 // |frame_header| is already initialized to 0 in ParseJpegPicture. | 56 // |frame_header| is already initialized to 0 in ParseJpegPicture. |
53 static bool ParseSOF(const char* buffer, | 57 static bool ParseSOF(const char* buffer, |
54 size_t length, | 58 size_t length, |
55 JpegFrameHeader* frame_header) { | 59 JpegFrameHeader* frame_header) { |
56 // Spec B.2.2 Frame header syntax | 60 // Spec B.2.2 Frame header syntax |
57 DCHECK(buffer); | 61 DCHECK(buffer); |
58 DCHECK(frame_header); | 62 DCHECK(frame_header); |
59 BigEndianReader reader(buffer, length); | 63 BigEndianReader reader(buffer, length); |
60 | 64 |
61 uint8_t precision; | 65 uint8_t precision; |
62 READ_U8_OR_RETURN_FALSE(&precision); | 66 READ_U8_OR_RETURN_FALSE(&precision); |
63 READ_U16_OR_RETURN_FALSE(&frame_header->visible_height); | 67 READ_U16_OR_RETURN_FALSE(&frame_header->visible_height); |
64 READ_U16_OR_RETURN_FALSE(&frame_header->visible_width); | 68 READ_U16_OR_RETURN_FALSE(&frame_header->visible_width); |
65 READ_U8_OR_RETURN_FALSE(&frame_header->num_components); | 69 READ_U8_OR_RETURN_FALSE(&frame_header->num_components); |
66 | 70 |
67 if (precision != 8) { | 71 if (precision != 8) { |
68 DLOG(ERROR) << "Only support 8-bit precision, not " | 72 DLOG(ERROR) << "Only support 8-bit precision, not " |
69 << static_cast<int>(precision) << " bit for baseline"; | 73 << static_cast<int>(precision) << " bit for baseline"; |
70 return false; | 74 return false; |
71 } | 75 } |
72 if (frame_header->num_components >= arraysize(frame_header->components)) { | 76 if (frame_header->num_components >= arraysize(frame_header->components)) { |
73 DLOG(ERROR) << "num_components=" | 77 DLOG(ERROR) << "num_components=" |
74 << static_cast<int>(frame_header->num_components) | 78 << static_cast<int>(frame_header->num_components) |
75 << " is not supported"; | 79 << " is not supported"; |
76 return false; | 80 return false; |
77 } | 81 } |
78 | 82 |
| 83 int max_h_factor = 0; |
| 84 int max_v_factor = 0; |
79 for (size_t i = 0; i < frame_header->num_components; i++) { | 85 for (size_t i = 0; i < frame_header->num_components; i++) { |
80 JpegComponent& component = frame_header->components[i]; | 86 JpegComponent& component = frame_header->components[i]; |
81 READ_U8_OR_RETURN_FALSE(&component.id); | 87 READ_U8_OR_RETURN_FALSE(&component.id); |
82 if (component.id > frame_header->num_components) { | 88 if (component.id > frame_header->num_components) { |
83 DLOG(ERROR) << "component id (" << static_cast<int>(component.id) | 89 DLOG(ERROR) << "component id (" << static_cast<int>(component.id) |
84 << ") should be <= num_components (" | 90 << ") should be <= num_components (" |
85 << static_cast<int>(frame_header->num_components) << ")"; | 91 << static_cast<int>(frame_header->num_components) << ")"; |
86 return false; | 92 return false; |
87 } | 93 } |
88 uint8_t hv; | 94 uint8_t hv; |
89 READ_U8_OR_RETURN_FALSE(&hv); | 95 READ_U8_OR_RETURN_FALSE(&hv); |
90 component.horizontal_sampling_factor = hv / 16; | 96 component.horizontal_sampling_factor = hv / 16; |
91 component.vertical_sampling_factor = hv % 16; | 97 component.vertical_sampling_factor = hv % 16; |
| 98 if (component.horizontal_sampling_factor > max_h_factor) |
| 99 max_h_factor = component.horizontal_sampling_factor; |
| 100 if (component.vertical_sampling_factor > max_v_factor) |
| 101 max_v_factor = component.vertical_sampling_factor; |
92 if (!InRange(component.horizontal_sampling_factor, 1, 4)) { | 102 if (!InRange(component.horizontal_sampling_factor, 1, 4)) { |
93 DVLOG(1) << "Invalid horizontal sampling factor " | 103 DVLOG(1) << "Invalid horizontal sampling factor " |
94 << static_cast<int>(component.horizontal_sampling_factor); | 104 << static_cast<int>(component.horizontal_sampling_factor); |
95 return false; | 105 return false; |
96 } | 106 } |
97 if (!InRange(component.vertical_sampling_factor, 1, 4)) { | 107 if (!InRange(component.vertical_sampling_factor, 1, 4)) { |
98 DVLOG(1) << "Invalid vertical sampling factor " | 108 DVLOG(1) << "Invalid vertical sampling factor " |
99 << static_cast<int>(component.horizontal_sampling_factor); | 109 << static_cast<int>(component.horizontal_sampling_factor); |
100 return false; | 110 return false; |
101 } | 111 } |
102 READ_U8_OR_RETURN_FALSE(&component.quantization_table_selector); | 112 READ_U8_OR_RETURN_FALSE(&component.quantization_table_selector); |
103 } | 113 } |
104 | 114 |
| 115 // The size of data unit is 8*8 and the coded size should be extended |
| 116 // to complete minimum coded unit, MCU. See Spec A.2. |
| 117 frame_header->coded_width = RoundUp(frame_header->visible_width, |
| 118 max_h_factor * 8); |
| 119 frame_header->coded_height = RoundUp(frame_header->visible_height, |
| 120 max_v_factor * 8); |
| 121 |
105 return true; | 122 return true; |
106 } | 123 } |
107 | 124 |
108 // |q_table| is already initialized to 0 in ParseJpegPicture. | 125 // |q_table| is already initialized to 0 in ParseJpegPicture. |
109 static bool ParseDQT(const char* buffer, | 126 static bool ParseDQT(const char* buffer, |
110 size_t length, | 127 size_t length, |
111 JpegQuantizationTable* q_table) { | 128 JpegQuantizationTable* q_table) { |
112 // Spec B.2.4.1 Quantization table-specification syntax | 129 // Spec B.2.4.1 Quantization table-specification syntax |
113 DCHECK(buffer); | 130 DCHECK(buffer); |
114 DCHECK(q_table); | 131 DCHECK(q_table); |
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
364 READ_U8_OR_RETURN_FALSE(&marker2); | 381 READ_U8_OR_RETURN_FALSE(&marker2); |
365 if (marker1 != MARKER1 || marker2 != SOI) { | 382 if (marker1 != MARKER1 || marker2 != SOI) { |
366 DLOG(ERROR) << "Not a JPEG"; | 383 DLOG(ERROR) << "Not a JPEG"; |
367 return false; | 384 return false; |
368 } | 385 } |
369 | 386 |
370 return ParseSOI(reader.ptr(), reader.remaining(), result); | 387 return ParseSOI(reader.ptr(), reader.remaining(), result); |
371 } | 388 } |
372 | 389 |
373 } // namespace media | 390 } // namespace media |
OLD | NEW |