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

Side by Side Diff: content/common/gpu/media/vaapi_jpeg_decoder.cc

Issue 825843002: Add JPEG decoder for VAAPI JPEG decode acceleration (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@mjpeg-vaapi-jpeg-parser
Patch Set: Created 5 years, 11 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
(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 <vector>
Owen Lin 2015/01/19 09:29:31 not used.
kcwu 2015/01/19 09:58:17 Done.
8
9 #include "base/logging.h"
10 #include "media/filters/jpeg_parser.h"
11
12 namespace media {
13
14 // K.3.3.1 "Specification of typical tables for DC difference coding"
15 static JpegHuffmanTable default_dc_table[kJpegMaxHuffmanTableNumBaseline] = {
16 // luminance DC coefficients
17 {
18 true,
19 {0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0},
20 {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b},
21 },
22 // chrominance DC coefficients
23 {
24 true,
25 {0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},
26 {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb},
27 },
28 };
29
30 // K.3.3.2 "Specification of typical tables for AC coefficient coding"
31 static JpegHuffmanTable default_ac_table[kJpegMaxHuffmanTableNumBaseline] = {
32 // luminance AC coefficients
33 {
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},
57 },
58 // chrominance AC coefficients
59 {
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},
83 },
84 };
85
86 } // namespace media
87
88 namespace content {
89
90 VaapiJpegDecoder::VaapiJpegDecoder(VaapiWrapper* vaapi_wrapper)
91 : vaapi_wrapper_(vaapi_wrapper) {
Owen Lin 2015/01/19 09:29:31 DCHECK on vaapi_wrapper ?
kcwu 2015/01/19 09:58:17 Done.
92 }
93
94 VaapiJpegDecoder::~VaapiJpegDecoder() {
95 }
96
97 static bool IsVaapiSupportedJpeg(const media::JpegParseResult& jpeg) {
98 // Size 64k*64k is the maximum in the JPEG standard. VAAPI doesn't support
99 // larger than 16k*16k.
100 const int kMaxDimension = 16384;
101 if (jpeg.frame_header.visible_height > kMaxDimension ||
102 jpeg.frame_header.visible_width > kMaxDimension) {
103 DLOG(ERROR) << "VAAPI don't support size("
104 << jpeg.frame_header.visible_width << "*"
105 << jpeg.frame_header.visible_height << ") larger than "
106 << kMaxDimension << "*" << kMaxDimension;
107 return false;
108 }
109
110 if (jpeg.frame_header.num_components != 3) {
111 DLOG(ERROR) << "VAAPI don't support num_components("
112 << static_cast<int>(jpeg.frame_header.num_components)
113 << ") != 3";
114 return false;
115 }
116
117 if (jpeg.frame_header.components[0].horizontal_sampling_factor <
118 jpeg.frame_header.components[1].horizontal_sampling_factor ||
119 jpeg.frame_header.components[0].horizontal_sampling_factor <
120 jpeg.frame_header.components[2].horizontal_sampling_factor) {
121 DLOG(ERROR) << "VAAPI don't supports horizontal sampling factor of Y"
122 << " smaller than Cb and Cr";
123 return false;
124 }
125
126 if (jpeg.frame_header.components[0].vertical_sampling_factor <
127 jpeg.frame_header.components[1].vertical_sampling_factor ||
128 jpeg.frame_header.components[0].vertical_sampling_factor <
129 jpeg.frame_header.components[2].vertical_sampling_factor) {
130 DLOG(ERROR) << "VAAPI don't supports vertical sampling factor of Y"
131 << " smaller than Cb and Cr";
132 return false;
133 }
134
135 return true;
136 }
137
138 static void FillPictureParameters(
139 const media::JpegFrameHeader& frame_header,
140 VAPictureParameterBufferJPEGBaseline* pic_param) {
141 memset(pic_param, 0, sizeof(*pic_param));
142 pic_param->picture_width = frame_header.visible_width;
143 pic_param->picture_height = frame_header.visible_height;
144 pic_param->num_components = frame_header.num_components;
145
146 for (int i = 0; i < pic_param->num_components; i++) {
147 pic_param->components[i].component_id = frame_header.components[i].id;
148 pic_param->components[i].h_sampling_factor =
149 frame_header.components[i].horizontal_sampling_factor;
150 pic_param->components[i].v_sampling_factor =
151 frame_header.components[i].vertical_sampling_factor;
152 pic_param->components[i].quantiser_table_selector =
153 frame_header.components[i].quantization_table_selector;
154 }
155 }
156
157 static void FillIQMatrix(const media::JpegQuantizationTable* q_table,
158 VAIQMatrixBufferJPEGBaseline* iq_matrix) {
159 memset(iq_matrix, 0, sizeof(*iq_matrix));
160 static_assert(media::kJpegMaxQuantizationTableNum ==
161 arraysize(iq_matrix->load_quantiser_table),
162 "max number of quantization table mismatched");
163 for (size_t i = 0; i < media::kJpegMaxQuantizationTableNum; i++) {
164 if (!q_table[i].valid)
165 continue;
166 iq_matrix->load_quantiser_table[i] = 1;
167 for (int j = 0; j < 64; j++)
168 iq_matrix->quantiser_table[i][j] = q_table[i].value[j];
169 }
170 }
171
172 static void FillHuffmanTable(const media::JpegHuffmanTable* dc_table,
173 const media::JpegHuffmanTable* ac_table,
174 VAHuffmanTableBufferJPEGBaseline* huffman_table) {
175 memset(huffman_table, 0, sizeof(*huffman_table));
176 // Use default huffman tables if not specified in header.
177 bool has_huffman_table = false;
178 for (size_t i = 0; i < media::kJpegMaxHuffmanTableNumBaseline; i++) {
179 if (dc_table[i].valid || ac_table[i].valid) {
180 has_huffman_table = true;
181 break;
182 }
183 }
184 if (!has_huffman_table) {
185 dc_table = media::default_dc_table;
186 ac_table = media::default_ac_table;
187 }
188
189 static_assert(media::kJpegMaxHuffmanTableNumBaseline ==
190 arraysize(huffman_table->load_huffman_table),
191 "max number of huffman table mismatched");
192 static_assert(sizeof(huffman_table->huffman_table[0].num_dc_codes) ==
193 sizeof(dc_table[0].code_length),
194 "size of huffman table code length mismatch");
195 static_assert(sizeof(huffman_table->huffman_table[0].dc_values[0]) ==
196 sizeof(dc_table[0].code_value[0]),
197 "size of huffman table code value mismatch");
198 for (size_t i = 0; i < media::kJpegMaxHuffmanTableNumBaseline; i++) {
199 if (!dc_table[i].valid || !ac_table[i].valid)
200 continue;
201 huffman_table->load_huffman_table[i] = 1;
202
203 memcpy(huffman_table->huffman_table[i].num_dc_codes,
204 dc_table[i].code_length,
205 sizeof(huffman_table->huffman_table[i].num_dc_codes));
206 memcpy(huffman_table->huffman_table[i].dc_values, dc_table[i].code_value,
207 sizeof(huffman_table->huffman_table[i].dc_values));
208 memcpy(huffman_table->huffman_table[i].num_ac_codes,
209 ac_table[i].code_length,
210 sizeof(huffman_table->huffman_table[i].num_ac_codes));
211 memcpy(huffman_table->huffman_table[i].ac_values, ac_table[i].code_value,
212 sizeof(huffman_table->huffman_table[i].ac_values));
213 }
214 }
215
216 static void FillSliceParameters(
217 const media::JpegParseResult& parse_result,
218 VASliceParameterBufferJPEGBaseline* slice_param) {
219 memset(slice_param, 0, sizeof(*slice_param));
220 int max_h_factor =
221 parse_result.frame_header.components[0].horizontal_sampling_factor;
222 int max_v_factor =
223 parse_result.frame_header.components[0].vertical_sampling_factor;
224 slice_param->slice_data_size = parse_result.data_size;
225 slice_param->slice_data_offset = 0;
226 slice_param->slice_data_flag = VA_SLICE_DATA_FLAG_ALL;
227 slice_param->slice_horizontal_position = 0;
228 slice_param->slice_vertical_position = 0;
229 slice_param->num_components = parse_result.scan.num_components;
230 for (int i = 0; i < slice_param->num_components; i++) {
231 slice_param->components[i].component_selector =
232 parse_result.scan.components[i].component_selector;
233 slice_param->components[i].dc_table_selector =
234 parse_result.scan.components[i].dc_selector;
235 slice_param->components[i].ac_table_selector =
236 parse_result.scan.components[i].ac_selector;
237 }
238 slice_param->restart_interval = parse_result.restart_interval;
239 int mcu_cols =
240 (parse_result.frame_header.visible_width + max_h_factor * 8 - 1) /
241 (max_h_factor * 8);
242 int mcu_rows =
243 (parse_result.frame_header.visible_height + max_v_factor * 8 - 1) /
244 (max_v_factor * 8);
245 slice_param->num_mcus = mcu_rows * mcu_cols;
246 }
247
248 bool VaapiJpegDecoder::Decode(const media::JpegParseResult& parse_result,
249 VASurfaceID va_surface) {
250 DCHECK_NE(va_surface, VA_INVALID_SURFACE);
251 if (!IsVaapiSupportedJpeg(parse_result))
252 return false;
253
254 // Set picture parameters.
255 VAPictureParameterBufferJPEGBaseline pic_param;
256 FillPictureParameters(parse_result.frame_header, &pic_param);
257 if (!vaapi_wrapper_->SubmitBuffer(VAPictureParameterBufferType,
258 sizeof(pic_param), &pic_param))
259 return false;
260
261 // Set quantization table.
262 VAIQMatrixBufferJPEGBaseline iq_matrix;
263 FillIQMatrix(parse_result.q_table, &iq_matrix);
264 if (!vaapi_wrapper_->SubmitBuffer(VAIQMatrixBufferType, sizeof(iq_matrix),
265 &iq_matrix))
266 return false;
267
268 // Set huffman table.
269 VAHuffmanTableBufferJPEGBaseline huffman_table;
270 FillHuffmanTable(parse_result.dc_table, parse_result.ac_table,
271 &huffman_table);
272 if (!vaapi_wrapper_->SubmitBuffer(VAHuffmanTableBufferType,
273 sizeof(huffman_table), &huffman_table))
274 return false;
275
276 // Set slice parameters.
277 VASliceParameterBufferJPEGBaseline slice_param;
278 FillSliceParameters(parse_result, &slice_param);
279 if (!vaapi_wrapper_->SubmitBuffer(VASliceParameterBufferType,
280 sizeof(slice_param), &slice_param))
281 return false;
282
283 // Set scan data.
284 if (!vaapi_wrapper_->SubmitBuffer(VASliceDataBufferType,
285 parse_result.data_size,
286 const_cast<char*>(parse_result.data)))
287 return false;
288
289 if (!vaapi_wrapper_->ExecuteAndDestroyPendingBuffers(va_surface))
290 return false;
291
292 return true;
293 }
294
295 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698