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