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 { |
| 11 |
| 12 // K.3.3.1 "Specification of typical tables for DC difference coding" |
| 13 media::JpegHuffmanTable |
| 14 kDefaultDcTable[media::kJpegMaxHuffmanTableNumBaseline] = { |
| 15 // luminance DC coefficients |
| 16 { |
| 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}, |
| 20 }, |
| 21 // chrominance DC coefficients |
| 22 { |
| 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}, |
| 26 }, |
| 27 }; |
| 28 |
| 29 // K.3.3.2 "Specification of typical tables for AC coefficient coding" |
| 30 media::JpegHuffmanTable |
| 31 kDefaultAcTable[media::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 |
| 87 |
| 88 namespace content { |
| 89 |
| 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; |
| 99 } |
| 100 |
| 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.visible_width > kMaxDimension || |
| 105 jpeg.frame_header.visible_height > kMaxDimension) { |
| 106 DLOG(ERROR) << "VAAPI doesn't support size(" |
| 107 << jpeg.frame_header.visible_width << "*" |
| 108 << jpeg.frame_header.visible_height << ") larger than " |
| 109 << kMaxDimension << "*" << kMaxDimension; |
| 110 return false; |
| 111 } |
| 112 |
| 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; |
| 118 } |
| 119 |
| 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; |
| 127 } |
| 128 |
| 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; |
| 136 } |
| 137 |
| 138 return true; |
| 139 } |
| 140 |
| 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.visible_width; |
| 146 pic_param->picture_height = frame_header.visible_height; |
| 147 pic_param->num_components = frame_header.num_components; |
| 148 |
| 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; |
| 157 } |
| 158 } |
| 159 |
| 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]; |
| 175 } |
| 176 } |
| 177 |
| 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; |
| 188 } |
| 189 } |
| 190 if (!has_huffman_table) { |
| 191 dc_table = kDefaultDcTable; |
| 192 ac_table = kDefaultAcTable; |
| 193 } |
| 194 |
| 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; |
| 208 |
| 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)); |
| 219 } |
| 220 } |
| 221 |
| 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; |
| 239 } |
| 240 slice_param->restart_interval = parse_result.restart_interval; |
| 241 |
| 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 visible_width = parse_result.frame_header.visible_width; |
| 248 int visible_height = parse_result.frame_header.visible_height; |
| 249 int mcu_cols = (visible_width + max_h_factor * 8 - 1) / (max_h_factor * 8); |
| 250 DCHECK_GT(mcu_cols, 0); |
| 251 int mcu_rows = (visible_height + max_v_factor * 8 - 1) / (max_v_factor * 8); |
| 252 DCHECK_GT(mcu_rows, 0); |
| 253 slice_param->num_mcus = mcu_rows * mcu_cols; |
| 254 } |
| 255 |
| 256 // static |
| 257 bool VaapiJpegDecoder::Decode(VaapiWrapper* vaapi_wrapper, |
| 258 const media::JpegParseResult& parse_result, |
| 259 VASurfaceID va_surface) { |
| 260 DCHECK_NE(va_surface, VA_INVALID_SURFACE); |
| 261 if (!IsVaapiSupportedJpeg(parse_result)) |
| 262 return false; |
| 263 |
| 264 // Set picture parameters. |
| 265 VAPictureParameterBufferJPEGBaseline pic_param; |
| 266 FillPictureParameters(parse_result.frame_header, &pic_param); |
| 267 if (!vaapi_wrapper->SubmitBuffer(VAPictureParameterBufferType, |
| 268 sizeof(pic_param), &pic_param)) |
| 269 return false; |
| 270 |
| 271 // Set quantization table. |
| 272 VAIQMatrixBufferJPEGBaseline iq_matrix; |
| 273 FillIQMatrix(parse_result.q_table, &iq_matrix); |
| 274 if (!vaapi_wrapper->SubmitBuffer(VAIQMatrixBufferType, sizeof(iq_matrix), |
| 275 &iq_matrix)) |
| 276 return false; |
| 277 |
| 278 // Set huffman table. |
| 279 VAHuffmanTableBufferJPEGBaseline huffman_table; |
| 280 FillHuffmanTable(parse_result.dc_table, parse_result.ac_table, |
| 281 &huffman_table); |
| 282 if (!vaapi_wrapper->SubmitBuffer(VAHuffmanTableBufferType, |
| 283 sizeof(huffman_table), &huffman_table)) |
| 284 return false; |
| 285 |
| 286 // Set slice parameters. |
| 287 VASliceParameterBufferJPEGBaseline slice_param; |
| 288 FillSliceParameters(parse_result, &slice_param); |
| 289 if (!vaapi_wrapper->SubmitBuffer(VASliceParameterBufferType, |
| 290 sizeof(slice_param), &slice_param)) |
| 291 return false; |
| 292 |
| 293 // Set scan data. |
| 294 if (!vaapi_wrapper->SubmitBuffer(VASliceDataBufferType, |
| 295 parse_result.data_size, |
| 296 const_cast<char*>(parse_result.data))) |
| 297 return false; |
| 298 |
| 299 if (!vaapi_wrapper->ExecuteAndDestroyPendingBuffers(va_surface)) |
| 300 return false; |
| 301 |
| 302 return true; |
| 303 } |
| 304 |
| 305 } // namespace content |
OLD | NEW |