7 #include "packager/media/codecs/vp9_parser.h"
9 #include "packager/base/logging.h"
10 #include "packager/media/base/bit_reader.h"
11 #include "packager/media/base/rcheck.h"
17 const uint32_t VP9_FRAME_MARKER = 2;
18 const uint32_t VP9_SYNC_CODE = 0x498342;
19 const uint32_t REFS_PER_FRAME = 3;
20 const uint32_t REF_FRAMES_LOG2 = 3;
21 const uint32_t REF_FRAMES = (1 << REF_FRAMES_LOG2);
22 const uint32_t FRAME_CONTEXTS_LOG2 = 2;
23 const uint32_t MAX_REF_LF_DELTAS = 4;
24 const uint32_t MAX_MODE_LF_DELTAS = 2;
25 const uint32_t QINDEX_BITS = 8;
26 const uint32_t MAX_SEGMENTS = 8;
27 const uint32_t SEG_TREE_PROBS = (MAX_SEGMENTS - 1);
28 const uint32_t PREDICTION_PROBS = 3;
29 const uint32_t SEG_LVL_MAX = 4;
30 const uint32_t MI_SIZE_LOG2 = 3;
31 const uint32_t MI_BLOCK_SIZE_LOG2 = (6 - MI_SIZE_LOG2);
32 const uint32_t MIN_TILE_WIDTH_B64 = 4;
33 const uint32_t MAX_TILE_WIDTH_B64 = 64;
35 const bool SEG_FEATURE_DATA_SIGNED[SEG_LVL_MAX] = {
true,
true,
false,
false};
36 const uint32_t SEG_FEATURE_DATA_MAX_BITS[SEG_LVL_MAX] = {8, 6, 2, 0};
39 VPX_COLOR_SPACE_UNKNOWN = 0,
40 VPX_COLOR_SPACE_BT_601 = 1,
41 VPX_COLOR_SPACE_BT_709 = 2,
42 VPX_COLOR_SPACE_SMPTE_170 = 3,
43 VPX_COLOR_SPACE_SMPTE_240 = 4,
44 VPX_COLOR_SPACE_BT_2020 = 5,
45 VPX_COLOR_SPACE_RESERVED = 6,
46 VPX_COLOR_SPACE_SRGB = 7,
49 uint32_t RoundupShift(uint32_t value, uint32_t n) {
50 return (value + (1 << n) - 1) >> n;
54 uint32_t GetNumMiUnits(uint32_t pixels) {
55 return RoundupShift(pixels, MI_SIZE_LOG2);
59 uint32_t GetNumBlocks(uint32_t mi_units) {
60 return RoundupShift(mi_units, MI_BLOCK_SIZE_LOG2);
63 uint32_t GetMinLog2TileCols(uint32_t sb64_cols) {
64 uint32_t min_log2 = 0;
65 while ((MAX_TILE_WIDTH_B64 << min_log2) < sb64_cols)
70 uint32_t GetMaxLog2TileCols(uint32_t sb64_cols) {
71 uint32_t max_log2 = 1;
72 while ((sb64_cols >> max_log2) >= MIN_TILE_WIDTH_B64)
77 void GetTileNBits(uint32_t mi_cols,
78 uint32_t* min_log2_tile_cols,
79 uint32_t* max_log2_tile_cols) {
80 const uint32_t sb64_cols = GetNumBlocks(mi_cols);
81 *min_log2_tile_cols = GetMinLog2TileCols(sb64_cols);
82 *max_log2_tile_cols = GetMaxLog2TileCols(sb64_cols);
83 CHECK_LE(*min_log2_tile_cols, *max_log2_tile_cols);
90 bool ParseIfSuperframeIndex(
const uint8_t* data,
92 std::vector<VPxFrameInfo>* vpx_frames) {
94 uint8_t superframe_marker = data[data_size - 1];
95 VPxFrameInfo vpx_frame;
96 if ((superframe_marker & 0xe0) != 0xc0) {
98 vpx_frame.frame_size = data_size;
99 vpx_frames->push_back(vpx_frame);
103 const size_t num_frames = (superframe_marker & 0x07) + 1;
104 const size_t frame_size_length = ((superframe_marker >> 3) & 0x03) + 1;
106 const size_t index_size = 2 + num_frames * frame_size_length;
108 if (data_size < index_size) {
109 LOG(ERROR) <<
"This chunk is marked as having a superframe index but "
110 "doesn't have enough data for it.";
113 const uint8_t superframe_marker2 = data[data_size - index_size];
114 if (superframe_marker2 != superframe_marker) {
115 LOG(ERROR) <<
"This chunk is marked as having a superframe index but "
116 "doesn't have the matching marker byte at the front of the "
120 VLOG(3) <<
"Superframe num_frames=" << num_frames
121 <<
" frame_size_length=" << frame_size_length;
123 data += data_size - index_size + 1;
124 size_t total_frame_sizes = 0;
125 for (
size_t i = 0; i < num_frames; ++i) {
126 vpx_frame.frame_size = 0;
127 for (
size_t i = 0; i < frame_size_length; ++i) {
128 vpx_frame.frame_size |= *data << (i * 8);
131 total_frame_sizes += vpx_frame.frame_size;
132 vpx_frames->push_back(vpx_frame);
134 if (total_frame_sizes + index_size != data_size) {
135 LOG(ERROR) <<
"Data size (" << data_size
136 <<
") does not match with sum of frame sizes ("
137 << total_frame_sizes <<
") + index_size (" << index_size <<
")";
143 bool ReadProfile(BitReader* reader, VPCodecConfigurationRecord* codec_config) {
145 RCHECK(reader->ReadBits(1, &bit[0]));
146 RCHECK(reader->ReadBits(1, &bit[1]));
147 uint8_t profile = bit[0] | (bit[1] << 1);
150 RCHECK(reader->ReadBits(1, &reserved));
153 codec_config->set_profile(profile);
157 bool ReadSyncCode(BitReader* reader) {
159 RCHECK(reader->ReadBits(24, &sync_code));
160 return sync_code == VP9_SYNC_CODE;
163 VPCodecConfigurationRecord::ColorSpace GetColorSpace(uint8_t color_space) {
164 switch (color_space) {
165 case VPX_COLOR_SPACE_UNKNOWN:
166 return VPCodecConfigurationRecord::COLOR_SPACE_UNSPECIFIED;
167 case VPX_COLOR_SPACE_BT_601:
168 return VPCodecConfigurationRecord::COLOR_SPACE_BT_601;
169 case VPX_COLOR_SPACE_BT_709:
170 return VPCodecConfigurationRecord::COLOR_SPACE_BT_709;
171 case VPX_COLOR_SPACE_SMPTE_170:
172 return VPCodecConfigurationRecord::COLOR_SPACE_SMPTE_170;
173 case VPX_COLOR_SPACE_SMPTE_240:
174 return VPCodecConfigurationRecord::COLOR_SPACE_SMPTE_240;
175 case VPX_COLOR_SPACE_BT_2020:
180 return VPCodecConfigurationRecord::
181 COLOR_SPACE_BT_2020_NON_CONSTANT_LUMINANCE;
182 case VPX_COLOR_SPACE_SRGB:
183 return VPCodecConfigurationRecord::COLOR_SPACE_SRGB;
185 LOG(WARNING) <<
"Unknown color space: " <<
static_cast<int>(color_space);
186 return VPCodecConfigurationRecord::COLOR_SPACE_UNSPECIFIED;
190 VPCodecConfigurationRecord::ChromaSubsampling GetChromaSubsampling(
191 uint8_t subsampling) {
192 switch (subsampling) {
194 return VPCodecConfigurationRecord::CHROMA_444;
196 return VPCodecConfigurationRecord::CHROMA_440;
198 return VPCodecConfigurationRecord::CHROMA_422;
202 return VPCodecConfigurationRecord::CHROMA_420_COLLOCATED_WITH_LUMA;
204 LOG(WARNING) <<
"Unexpected chroma subsampling value: "
205 <<
static_cast<int>(subsampling);
206 return VPCodecConfigurationRecord::CHROMA_420_COLLOCATED_WITH_LUMA;
210 bool ReadBitDepthAndColorSpace(BitReader* reader,
211 VPCodecConfigurationRecord* codec_config) {
212 uint8_t bit_depth = 8;
213 if (codec_config->profile() >= 2) {
214 bool use_vpx_bits_12;
215 RCHECK(reader->ReadBits(1, &use_vpx_bits_12));
216 bit_depth = use_vpx_bits_12 ? 12 : 10;
218 codec_config->set_bit_depth(bit_depth);
221 RCHECK(reader->ReadBits(3, &color_space));
222 codec_config->set_color_space(GetColorSpace(color_space));
224 bool yuv_full_range =
false;
225 auto chroma_subsampling = VPCodecConfigurationRecord::CHROMA_444;
226 if (color_space != VPX_COLOR_SPACE_SRGB) {
227 RCHECK(reader->ReadBits(1, &yuv_full_range));
229 if (codec_config->profile() & 1) {
231 RCHECK(reader->ReadBits(2, &subsampling));
232 chroma_subsampling = GetChromaSubsampling(subsampling);
233 if (chroma_subsampling ==
234 VPCodecConfigurationRecord::CHROMA_420_COLLOCATED_WITH_LUMA) {
235 LOG(ERROR) <<
"4:2:0 color not supported in profile "
236 << codec_config->profile();
241 RCHECK(reader->ReadBits(1, &reserved));
245 VPCodecConfigurationRecord::CHROMA_420_COLLOCATED_WITH_LUMA;
249 chroma_subsampling = VPCodecConfigurationRecord::CHROMA_444;
250 if (codec_config->profile() & 1) {
252 RCHECK(reader->ReadBits(1, &reserved));
255 LOG(ERROR) <<
"4:4:4 color not supported in profile 0 or 2.";
259 codec_config->set_video_full_range_flag(yuv_full_range);
260 codec_config->set_chroma_subsampling(chroma_subsampling);
262 VLOG(3) <<
"\n profile " <<
static_cast<int>(codec_config->profile())
263 <<
"\n bit depth " << static_cast<int>(codec_config->bit_depth())
264 <<
"\n color space " << static_cast<int>(codec_config->color_space())
266 << static_cast<int>(codec_config->video_full_range_flag())
267 <<
"\n chroma subsampling "
268 << static_cast<int>(codec_config->chroma_subsampling());
272 bool ReadFrameSize(BitReader* reader, uint32_t* width, uint32_t* height) {
273 RCHECK(reader->ReadBits(16, width));
275 RCHECK(reader->ReadBits(16, height));
280 bool ReadDisplayFrameSize(BitReader* reader,
281 uint32_t* display_width,
282 uint32_t* display_height) {
283 bool has_display_size;
284 RCHECK(reader->ReadBits(1, &has_display_size));
285 if (has_display_size)
286 RCHECK(ReadFrameSize(reader, display_width, display_height));
290 bool ReadFrameSizes(BitReader* reader, uint32_t* width, uint32_t* height) {
293 RCHECK(ReadFrameSize(reader, &new_width, &new_height));
294 if (new_width != *width) {
295 VLOG(1) <<
"Width updates from " << *width <<
" to " << new_width;
298 if (new_height != *height) {
299 VLOG(1) <<
"Height updates from " << *height <<
" to " << new_height;
300 *height = new_height;
303 uint32_t display_width = *width;
304 uint32_t display_height = *height;
305 RCHECK(ReadDisplayFrameSize(reader, &display_width, &display_height));
309 bool ReadFrameSizesWithRefs(BitReader* reader,
313 for (uint32_t i = 0; i < REFS_PER_FRAME; ++i) {
314 RCHECK(reader->ReadBits(1, &found));
319 RCHECK(ReadFrameSizes(reader, width, height));
321 uint32_t display_width;
322 uint32_t display_height;
323 RCHECK(ReadDisplayFrameSize(reader, &display_width, &display_height));
328 bool ReadLoopFilter(BitReader* reader) {
329 RCHECK(reader->SkipBits(9));
330 bool mode_ref_delta_enabled;
331 RCHECK(reader->ReadBits(1, &mode_ref_delta_enabled));
332 if (!mode_ref_delta_enabled)
334 bool mode_ref_delta_update;
335 RCHECK(reader->ReadBits(1, &mode_ref_delta_update));
336 if (!mode_ref_delta_update)
339 for (uint32_t i = 0; i < MAX_REF_LF_DELTAS + MAX_MODE_LF_DELTAS; ++i)
340 RCHECK(reader->SkipBitsConditional(
true, 6 + 1));
344 bool ReadQuantization(BitReader* reader) {
345 RCHECK(reader->SkipBits(QINDEX_BITS));
347 for (uint32_t i = 0; i < 3; ++i)
348 RCHECK(reader->SkipBitsConditional(
true, 4 + 1));
352 bool ReadSegmentation(BitReader* reader) {
354 RCHECK(reader->ReadBits(1, &enabled));
359 RCHECK(reader->ReadBits(1, &update_map));
361 for (uint32_t i = 0; i < SEG_TREE_PROBS; ++i)
362 RCHECK(reader->SkipBitsConditional(
true, 8));
364 bool temporal_update;
365 RCHECK(reader->ReadBits(1, &temporal_update));
366 if (temporal_update) {
367 for (uint32_t j = 0; j < PREDICTION_PROBS; ++j)
368 RCHECK(reader->SkipBitsConditional(
true, 8));
373 RCHECK(reader->ReadBits(1, &update_data));
375 RCHECK(reader->SkipBits(1));
376 for (uint32_t i = 0; i < MAX_SEGMENTS; ++i) {
377 for (uint32_t j = 0; j < SEG_LVL_MAX; ++j) {
378 bool feature_enabled;
379 RCHECK(reader->ReadBits(1, &feature_enabled));
380 if (feature_enabled) {
381 RCHECK(reader->SkipBits(SEG_FEATURE_DATA_MAX_BITS[j]));
382 if (SEG_FEATURE_DATA_SIGNED[j])
383 RCHECK(reader->SkipBits(1));
391 bool ReadTileInfo(uint32_t width, BitReader* reader) {
392 uint32_t mi_cols = GetNumMiUnits(width);
394 uint32_t min_log2_tile_cols;
395 uint32_t max_log2_tile_cols;
396 GetTileNBits(mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
397 uint32_t max_ones = max_log2_tile_cols - min_log2_tile_cols;
399 uint32_t log2_tile_cols = min_log2_tile_cols;
402 RCHECK(reader->ReadBits(1, &has_more));
407 RCHECK(log2_tile_cols <= 6);
409 RCHECK(reader->SkipBitsConditional(
true, 1));
415 VP9Parser::VP9Parser() : width_(0), height_(0) {}
416 VP9Parser::~VP9Parser() {}
420 std::vector<VPxFrameInfo>* vpx_frames) {
423 RCHECK(ParseIfSuperframeIndex(data, data_size, vpx_frames));
425 for (
auto& vpx_frame : *vpx_frames) {
426 VLOG(4) <<
"process frame with size " << vpx_frame.frame_size;
427 BitReader reader(data, vpx_frame.frame_size);
428 uint8_t frame_marker;
429 RCHECK(reader.
ReadBits(2, &frame_marker));
430 RCHECK(frame_marker == VP9_FRAME_MARKER);
432 RCHECK(ReadProfile(&reader, writable_codec_config()));
434 bool show_existing_frame;
435 RCHECK(reader.
ReadBits(1, &show_existing_frame));
436 if (show_existing_frame) {
441 vpx_frame.is_keyframe =
false;
442 vpx_frame.uncompressed_header_size = vpx_frame.frame_size;
443 vpx_frame.width = width_;
444 vpx_frame.height = height_;
449 RCHECK(reader.
ReadBits(1, &is_interframe));
450 vpx_frame.is_keyframe = !is_interframe;
453 RCHECK(reader.
ReadBits(1, &show_frame));
454 bool error_resilient_mode;
455 RCHECK(reader.
ReadBits(1, &error_resilient_mode));
457 if (vpx_frame.is_keyframe) {
458 RCHECK(ReadSyncCode(&reader));
459 RCHECK(ReadBitDepthAndColorSpace(&reader, writable_codec_config()));
460 RCHECK(ReadFrameSizes(&reader, &width_, &height_));
462 bool intra_only =
false;
464 RCHECK(reader.
ReadBits(1, &intra_only));
465 if (!error_resilient_mode)
469 RCHECK(ReadSyncCode(&reader));
471 RCHECK(ReadBitDepthAndColorSpace(&reader, writable_codec_config()));
477 writable_codec_config()->set_chroma_subsampling(
478 VPCodecConfigurationRecord::CHROMA_420_COLLOCATED_WITH_LUMA);
479 writable_codec_config()->set_bit_depth(8);
482 RCHECK(reader.
SkipBits(REF_FRAMES));
483 RCHECK(ReadFrameSizes(&reader, &width_, &height_));
485 RCHECK(reader.
SkipBits(REF_FRAMES));
486 RCHECK(reader.
SkipBits(REFS_PER_FRAME * (REF_FRAMES_LOG2 + 1)));
491 RCHECK(ReadFrameSizesWithRefs(&reader, &width_, &height_));
496 RCHECK(reader.
ReadBits(1, &interp_filter));
502 if (!error_resilient_mode) {
506 RCHECK(reader.
SkipBits(FRAME_CONTEXTS_LOG2));
508 VLOG(4) <<
"bits read before ReadLoopFilter: " << reader.
bit_position();
509 RCHECK(ReadLoopFilter(&reader));
510 RCHECK(ReadQuantization(&reader));
511 RCHECK(ReadSegmentation(&reader));
512 RCHECK(ReadTileInfo(width_, &reader));
514 uint16_t header_size;
515 RCHECK(reader.
ReadBits(16, &header_size));
516 vpx_frame.uncompressed_header_size =
518 vpx_frame.width = width_;
519 vpx_frame.height = height_;
521 VLOG(3) <<
"\n frame_size: " << vpx_frame.frame_size
522 <<
"\n uncompressed_header_size: "
523 << vpx_frame.uncompressed_header_size
525 <<
"\n header_size: " << header_size;
527 RCHECK(header_size > 0);
530 data += vpx_frame.frame_size;
537 uint8_t frame_marker;
538 RCHECK(reader.
ReadBits(2, &frame_marker));
539 RCHECK(frame_marker == VP9_FRAME_MARKER);
542 RCHECK(ReadProfile(&reader, &codec_config));
544 bool show_existing_frame;
545 RCHECK(reader.
ReadBits(1, &show_existing_frame));
546 if (show_existing_frame)
550 RCHECK(reader.
ReadBits(1, &is_interframe));
556 RCHECK(ReadSyncCode(&reader));