OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "content/common/gpu/media/vaapi_jpeg_decoder.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "media/filters/jpeg_parser.h" | |
9 | |
10 namespace media { | |
11 | |
12 // K.3.3.1 "Specification of typical tables for DC difference coding" | |
13 static JpegHuffmanTable default_dc_table[kJpegMaxHuffmanTableNumBaseline] = { | |
wuchengli
2015/01/20 07:55:53
Why this is exposed to media namespace? If no spec
kcwu
2015/01/21 12:23:47
Done.
Actually they are not exposed to media due t
| |
14 // luminance DC coefficients | |
15 { | |
16 true, | |
17 {0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0}, | |
18 {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b}, | |
19 }, | |
20 // chrominance DC coefficients | |
21 { | |
22 true, | |
23 {0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0}, | |
24 {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb}, | |
25 }, | |
26 }; | |
27 | |
28 // K.3.3.2 "Specification of typical tables for AC coefficient coding" | |
29 static JpegHuffmanTable default_ac_table[kJpegMaxHuffmanTableNumBaseline] = { | |
wuchengli
2015/01/20 07:55:53
s/default_ac_table/kDefaultAcTable/
kcwu
2015/01/21 12:23:47
Done.
| |
30 // luminance AC coefficients | |
31 { | |
32 true, | |
33 {0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d}, | |
34 {0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, | |
35 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07, | |
36 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08, | |
37 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0, | |
38 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16, | |
39 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28, | |
40 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, | |
41 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, | |
42 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, | |
43 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, | |
44 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, | |
45 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, | |
46 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, | |
47 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, | |
48 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, | |
49 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5, | |
50 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4, | |
51 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2, | |
52 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, | |
53 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, | |
54 0xf9, 0xfa}, | |
55 }, | |
56 // chrominance AC coefficients | |
57 { | |
58 true, | |
59 {0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77}, | |
60 {0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21, | |
61 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71, | |
62 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91, | |
63 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0, | |
64 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34, | |
65 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26, | |
66 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38, | |
67 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, | |
68 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, | |
69 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, | |
70 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, | |
71 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, | |
72 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, | |
73 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, | |
74 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, | |
75 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, | |
76 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, | |
77 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, | |
78 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, | |
79 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, | |
80 0xf9, 0xfa}, | |
81 }, | |
82 }; | |
83 | |
84 } // namespace media | |
85 | |
86 namespace content { | |
87 | |
88 VaapiJpegDecoder::VaapiJpegDecoder(VaapiWrapper* vaapi_wrapper) | |
89 : vaapi_wrapper_(vaapi_wrapper) { | |
90 DCHECK(vaapi_wrapper); | |
91 } | |
92 | |
93 VaapiJpegDecoder::~VaapiJpegDecoder() { | |
94 } | |
95 | |
96 static bool IsVaapiSupportedJpeg(const media::JpegParseResult& jpeg) { | |
97 // Size 64k*64k is the maximum in the JPEG standard. VAAPI doesn't support | |
98 // larger than 16k*16k. | |
99 const int kMaxDimension = 16384; | |
100 if (jpeg.frame_header.visible_height > kMaxDimension || | |
wuchengli
2015/01/20 07:55:53
nit: check visible_width first then visible_height
kcwu
2015/01/21 12:23:47
Done.
| |
101 jpeg.frame_header.visible_width > kMaxDimension) { | |
102 DLOG(ERROR) << "VAAPI don't support size(" | |
wuchengli
2015/01/20 07:55:53
s/don't/doesn't/. Same below.
kcwu
2015/01/21 12:23:47
Done.
| |
103 << jpeg.frame_header.visible_width << "*" | |
104 << jpeg.frame_header.visible_height << ") larger than " | |
105 << kMaxDimension << "*" << kMaxDimension; | |
106 return false; | |
107 } | |
108 | |
109 if (jpeg.frame_header.num_components != 3) { | |
110 DLOG(ERROR) << "VAAPI don't support num_components(" | |
111 << static_cast<int>(jpeg.frame_header.num_components) | |
112 << ") != 3"; | |
113 return false; | |
114 } | |
115 | |
116 if (jpeg.frame_header.components[0].horizontal_sampling_factor < | |
117 jpeg.frame_header.components[1].horizontal_sampling_factor || | |
118 jpeg.frame_header.components[0].horizontal_sampling_factor < | |
119 jpeg.frame_header.components[2].horizontal_sampling_factor) { | |
120 DLOG(ERROR) << "VAAPI don't supports horizontal sampling factor of Y" | |
121 << " smaller than Cb and Cr"; | |
122 return false; | |
123 } | |
124 | |
125 if (jpeg.frame_header.components[0].vertical_sampling_factor < | |
126 jpeg.frame_header.components[1].vertical_sampling_factor || | |
127 jpeg.frame_header.components[0].vertical_sampling_factor < | |
128 jpeg.frame_header.components[2].vertical_sampling_factor) { | |
129 DLOG(ERROR) << "VAAPI don't supports vertical sampling factor of Y" | |
130 << " smaller than Cb and Cr"; | |
131 return false; | |
132 } | |
133 | |
134 return true; | |
135 } | |
136 | |
137 static void FillPictureParameters( | |
138 const media::JpegFrameHeader& frame_header, | |
139 VAPictureParameterBufferJPEGBaseline* pic_param) { | |
140 memset(pic_param, 0, sizeof(*pic_param)); | |
141 pic_param->picture_width = frame_header.visible_width; | |
142 pic_param->picture_height = frame_header.visible_height; | |
143 pic_param->num_components = frame_header.num_components; | |
144 | |
145 for (int i = 0; i < pic_param->num_components; i++) { | |
146 pic_param->components[i].component_id = frame_header.components[i].id; | |
147 pic_param->components[i].h_sampling_factor = | |
148 frame_header.components[i].horizontal_sampling_factor; | |
149 pic_param->components[i].v_sampling_factor = | |
150 frame_header.components[i].vertical_sampling_factor; | |
151 pic_param->components[i].quantiser_table_selector = | |
152 frame_header.components[i].quantization_table_selector; | |
153 } | |
154 } | |
155 | |
156 static void FillIQMatrix(const media::JpegQuantizationTable* q_table, | |
157 VAIQMatrixBufferJPEGBaseline* iq_matrix) { | |
158 memset(iq_matrix, 0, sizeof(*iq_matrix)); | |
159 static_assert(media::kJpegMaxQuantizationTableNum == | |
160 arraysize(iq_matrix->load_quantiser_table), | |
161 "max number of quantization table mismatched"); | |
162 for (size_t i = 0; i < media::kJpegMaxQuantizationTableNum; i++) { | |
163 if (!q_table[i].valid) | |
164 continue; | |
165 iq_matrix->load_quantiser_table[i] = 1; | |
166 for (int j = 0; j < 64; j++) | |
167 iq_matrix->quantiser_table[i][j] = q_table[i].value[j]; | |
168 } | |
169 } | |
170 | |
171 static void FillHuffmanTable(const media::JpegHuffmanTable* dc_table, | |
172 const media::JpegHuffmanTable* ac_table, | |
173 VAHuffmanTableBufferJPEGBaseline* huffman_table) { | |
174 memset(huffman_table, 0, sizeof(*huffman_table)); | |
175 // Use default huffman tables if not specified in header. | |
176 bool has_huffman_table = false; | |
177 for (size_t i = 0; i < media::kJpegMaxHuffmanTableNumBaseline; i++) { | |
178 if (dc_table[i].valid || ac_table[i].valid) { | |
179 has_huffman_table = true; | |
180 break; | |
181 } | |
182 } | |
183 if (!has_huffman_table) { | |
184 dc_table = media::default_dc_table; | |
185 ac_table = media::default_ac_table; | |
186 } | |
187 | |
188 static_assert(media::kJpegMaxHuffmanTableNumBaseline == | |
189 arraysize(huffman_table->load_huffman_table), | |
190 "max number of huffman table mismatched"); | |
191 static_assert(sizeof(huffman_table->huffman_table[0].num_dc_codes) == | |
192 sizeof(dc_table[0].code_length), | |
193 "size of huffman table code length mismatch"); | |
194 static_assert(sizeof(huffman_table->huffman_table[0].dc_values[0]) == | |
195 sizeof(dc_table[0].code_value[0]), | |
196 "size of huffman table code value mismatch"); | |
197 for (size_t i = 0; i < media::kJpegMaxHuffmanTableNumBaseline; i++) { | |
198 if (!dc_table[i].valid || !ac_table[i].valid) | |
199 continue; | |
200 huffman_table->load_huffman_table[i] = 1; | |
201 | |
202 memcpy(huffman_table->huffman_table[i].num_dc_codes, | |
203 dc_table[i].code_length, | |
204 sizeof(huffman_table->huffman_table[i].num_dc_codes)); | |
205 memcpy(huffman_table->huffman_table[i].dc_values, dc_table[i].code_value, | |
206 sizeof(huffman_table->huffman_table[i].dc_values)); | |
207 memcpy(huffman_table->huffman_table[i].num_ac_codes, | |
208 ac_table[i].code_length, | |
209 sizeof(huffman_table->huffman_table[i].num_ac_codes)); | |
210 memcpy(huffman_table->huffman_table[i].ac_values, ac_table[i].code_value, | |
211 sizeof(huffman_table->huffman_table[i].ac_values)); | |
212 } | |
213 } | |
214 | |
215 static void FillSliceParameters( | |
216 const media::JpegParseResult& parse_result, | |
217 VASliceParameterBufferJPEGBaseline* slice_param) { | |
218 memset(slice_param, 0, sizeof(*slice_param)); | |
219 int max_h_factor = | |
220 parse_result.frame_header.components[0].horizontal_sampling_factor; | |
221 int max_v_factor = | |
222 parse_result.frame_header.components[0].vertical_sampling_factor; | |
223 slice_param->slice_data_size = parse_result.data_size; | |
224 slice_param->slice_data_offset = 0; | |
225 slice_param->slice_data_flag = VA_SLICE_DATA_FLAG_ALL; | |
226 slice_param->slice_horizontal_position = 0; | |
227 slice_param->slice_vertical_position = 0; | |
228 slice_param->num_components = parse_result.scan.num_components; | |
229 for (int i = 0; i < slice_param->num_components; i++) { | |
230 slice_param->components[i].component_selector = | |
231 parse_result.scan.components[i].component_selector; | |
232 slice_param->components[i].dc_table_selector = | |
233 parse_result.scan.components[i].dc_selector; | |
234 slice_param->components[i].ac_table_selector = | |
235 parse_result.scan.components[i].ac_selector; | |
236 } | |
237 slice_param->restart_interval = parse_result.restart_interval; | |
238 int mcu_cols = | |
239 (parse_result.frame_header.visible_width + max_h_factor * 8 - 1) / | |
240 (max_h_factor * 8); | |
241 int mcu_rows = | |
242 (parse_result.frame_header.visible_height + max_v_factor * 8 - 1) / | |
243 (max_v_factor * 8); | |
244 slice_param->num_mcus = mcu_rows * mcu_cols; | |
245 } | |
246 | |
247 bool VaapiJpegDecoder::Decode(const media::JpegParseResult& parse_result, | |
248 VASurfaceID va_surface) { | |
249 DCHECK_NE(va_surface, VA_INVALID_SURFACE); | |
250 if (!IsVaapiSupportedJpeg(parse_result)) | |
251 return false; | |
252 | |
253 // Set picture parameters. | |
254 VAPictureParameterBufferJPEGBaseline pic_param; | |
255 FillPictureParameters(parse_result.frame_header, &pic_param); | |
256 if (!vaapi_wrapper_->SubmitBuffer(VAPictureParameterBufferType, | |
257 sizeof(pic_param), &pic_param)) | |
258 return false; | |
259 | |
260 // Set quantization table. | |
261 VAIQMatrixBufferJPEGBaseline iq_matrix; | |
262 FillIQMatrix(parse_result.q_table, &iq_matrix); | |
263 if (!vaapi_wrapper_->SubmitBuffer(VAIQMatrixBufferType, sizeof(iq_matrix), | |
264 &iq_matrix)) | |
265 return false; | |
266 | |
267 // Set huffman table. | |
268 VAHuffmanTableBufferJPEGBaseline huffman_table; | |
269 FillHuffmanTable(parse_result.dc_table, parse_result.ac_table, | |
270 &huffman_table); | |
271 if (!vaapi_wrapper_->SubmitBuffer(VAHuffmanTableBufferType, | |
272 sizeof(huffman_table), &huffman_table)) | |
273 return false; | |
274 | |
275 // Set slice parameters. | |
276 VASliceParameterBufferJPEGBaseline slice_param; | |
277 FillSliceParameters(parse_result, &slice_param); | |
278 if (!vaapi_wrapper_->SubmitBuffer(VASliceParameterBufferType, | |
279 sizeof(slice_param), &slice_param)) | |
280 return false; | |
281 | |
282 // Set scan data. | |
283 if (!vaapi_wrapper_->SubmitBuffer(VASliceDataBufferType, | |
284 parse_result.data_size, | |
285 const_cast<char*>(parse_result.data))) | |
286 return false; | |
287 | |
288 if (!vaapi_wrapper_->ExecuteAndDestroyPendingBuffers(va_surface)) | |
289 return false; | |
290 | |
291 return true; | |
292 } | |
293 | |
294 } // namespace content | |
OLD | NEW |