7 #include "packager/media/filters/vp9_parser.h"
9 #include "packager/base/logging.h"
10 #include "packager/media/base/bit_reader.h"
11 #include "packager/media/formats/mp4/rcheck.h"
13 namespace edash_packager {
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, VPCodecConfiguration* 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 VPCodecConfiguration::ColorSpace GetColorSpace(uint8_t color_space) {
164 switch (color_space) {
165 case VPX_COLOR_SPACE_UNKNOWN:
166 return VPCodecConfiguration::COLOR_SPACE_UNSPECIFIED;
167 case VPX_COLOR_SPACE_BT_601:
168 return VPCodecConfiguration::COLOR_SPACE_BT_601;
169 case VPX_COLOR_SPACE_BT_709:
170 return VPCodecConfiguration::COLOR_SPACE_BT_709;
171 case VPX_COLOR_SPACE_SMPTE_170:
172 return VPCodecConfiguration::COLOR_SPACE_SMPTE_170;
173 case VPX_COLOR_SPACE_SMPTE_240:
174 return VPCodecConfiguration::COLOR_SPACE_SMPTE_240;
175 case VPX_COLOR_SPACE_BT_2020:
180 return VPCodecConfiguration::COLOR_SPACE_BT_2020_NON_CONSTANT_LUMINANCE;
181 case VPX_COLOR_SPACE_SRGB:
182 return VPCodecConfiguration::COLOR_SPACE_SRGB;
184 LOG(WARNING) <<
"Unknown color space: " <<
static_cast<int>(color_space);
185 return VPCodecConfiguration::COLOR_SPACE_UNSPECIFIED;
189 VPCodecConfiguration::ChromaSubsampling GetChromaSubsampling(
190 uint8_t subsampling) {
191 switch (subsampling) {
193 return VPCodecConfiguration::CHROMA_444;
195 return VPCodecConfiguration::CHROMA_440;
197 return VPCodecConfiguration::CHROMA_422;
201 return VPCodecConfiguration::CHROMA_420_COLLOCATED_WITH_LUMA;
203 LOG(WARNING) <<
"Unexpected chroma subsampling value: "
204 <<
static_cast<int>(subsampling);
205 return VPCodecConfiguration::CHROMA_420_COLLOCATED_WITH_LUMA;
209 bool ReadBitDepthAndColorSpace(BitReader* reader,
210 VPCodecConfiguration* codec_config) {
211 uint8_t bit_depth = 8;
212 if (codec_config->profile() >= 2) {
213 bool use_vpx_bits_12;
214 RCHECK(reader->ReadBits(1, &use_vpx_bits_12));
215 bit_depth = use_vpx_bits_12 ? 12 : 10;
217 codec_config->set_bit_depth(bit_depth);
220 RCHECK(reader->ReadBits(3, &color_space));
221 codec_config->set_color_space(GetColorSpace(color_space));
223 bool yuv_full_range =
false;
224 auto chroma_subsampling = VPCodecConfiguration::CHROMA_444;
225 if (color_space != VPX_COLOR_SPACE_SRGB) {
226 RCHECK(reader->ReadBits(1, &yuv_full_range));
228 if (codec_config->profile() & 1) {
230 RCHECK(reader->ReadBits(2, &subsampling));
231 chroma_subsampling = GetChromaSubsampling(subsampling);
232 if (chroma_subsampling ==
233 VPCodecConfiguration::CHROMA_420_COLLOCATED_WITH_LUMA) {
234 LOG(ERROR) <<
"4:2:0 color not supported in profile "
235 << codec_config->profile();
240 RCHECK(reader->ReadBits(1, &reserved));
244 VPCodecConfiguration::CHROMA_420_COLLOCATED_WITH_LUMA;
248 chroma_subsampling = VPCodecConfiguration::CHROMA_444;
249 if (codec_config->profile() & 1) {
251 RCHECK(reader->ReadBits(1, &reserved));
254 LOG(ERROR) <<
"4:4:4 color not supported in profile 0 or 2.";
258 codec_config->set_video_full_range_flag(yuv_full_range);
259 codec_config->set_chroma_subsampling(chroma_subsampling);
261 VLOG(3) <<
"\n profile " <<
static_cast<int>(codec_config->profile())
262 <<
"\n bit depth " << static_cast<int>(codec_config->bit_depth())
263 <<
"\n color space " << static_cast<int>(codec_config->color_space())
265 << static_cast<int>(codec_config->video_full_range_flag())
266 <<
"\n chroma subsampling "
267 << static_cast<int>(codec_config->chroma_subsampling());
271 bool ReadFrameSize(BitReader* reader, uint32_t* width, uint32_t* height) {
272 RCHECK(reader->ReadBits(16, width));
274 RCHECK(reader->ReadBits(16, height));
279 bool ReadDisplayFrameSize(BitReader* reader,
280 uint32_t* display_width,
281 uint32_t* display_height) {
282 bool has_display_size;
283 RCHECK(reader->ReadBits(1, &has_display_size));
284 if (has_display_size)
285 RCHECK(ReadFrameSize(reader, display_width, display_height));
289 bool ReadFrameSizes(BitReader* reader, uint32_t* width, uint32_t* height) {
292 RCHECK(ReadFrameSize(reader, &new_width, &new_height));
293 if (new_width != *width) {
294 VLOG(1) <<
"Width updates from " << *width <<
" to " << new_width;
297 if (new_height != *height) {
298 VLOG(1) <<
"Height updates from " << *height <<
" to " << new_height;
299 *height = new_height;
302 uint32_t display_width = *width;
303 uint32_t display_height = *height;
304 RCHECK(ReadDisplayFrameSize(reader, &display_width, &display_height));
308 bool ReadFrameSizesWithRefs(BitReader* reader,
312 for (uint32_t i = 0; i < REFS_PER_FRAME; ++i) {
313 RCHECK(reader->ReadBits(1, &found));
318 RCHECK(ReadFrameSizes(reader, width, height));
320 uint32_t display_width;
321 uint32_t display_height;
322 RCHECK(ReadDisplayFrameSize(reader, &display_width, &display_height));
327 bool ReadLoopFilter(BitReader* reader) {
328 RCHECK(reader->SkipBits(9));
329 bool mode_ref_delta_enabled;
330 RCHECK(reader->ReadBits(1, &mode_ref_delta_enabled));
331 if (!mode_ref_delta_enabled)
333 bool mode_ref_delta_update;
334 RCHECK(reader->ReadBits(1, &mode_ref_delta_update));
335 if (!mode_ref_delta_update)
338 for (uint32_t i = 0; i < MAX_REF_LF_DELTAS + MAX_MODE_LF_DELTAS; ++i)
339 RCHECK(reader->SkipBitsConditional(
true, 6 + 1));
343 bool ReadQuantization(BitReader* reader) {
344 RCHECK(reader->SkipBits(QINDEX_BITS));
346 for (uint32_t i = 0; i < 3; ++i)
347 RCHECK(reader->SkipBitsConditional(
true, 4 + 1));
351 bool ReadSegmentation(BitReader* reader) {
353 RCHECK(reader->ReadBits(1, &enabled));
358 RCHECK(reader->ReadBits(1, &update_map));
360 for (uint32_t i = 0; i < SEG_TREE_PROBS; ++i)
361 RCHECK(reader->SkipBitsConditional(
true, 8));
363 bool temporal_update;
364 RCHECK(reader->ReadBits(1, &temporal_update));
365 if (temporal_update) {
366 for (uint32_t j = 0; j < PREDICTION_PROBS; ++j)
367 RCHECK(reader->SkipBitsConditional(
true, 8));
372 RCHECK(reader->ReadBits(1, &update_data));
374 RCHECK(reader->SkipBits(1));
375 for (uint32_t i = 0; i < MAX_SEGMENTS; ++i) {
376 for (uint32_t j = 0; j < SEG_LVL_MAX; ++j) {
377 bool feature_enabled;
378 RCHECK(reader->ReadBits(1, &feature_enabled));
379 if (feature_enabled) {
380 RCHECK(reader->SkipBits(SEG_FEATURE_DATA_MAX_BITS[j]));
381 if (SEG_FEATURE_DATA_SIGNED[j])
382 RCHECK(reader->SkipBits(1));
390 bool ReadTileInfo(uint32_t width, BitReader* reader) {
391 uint32_t mi_cols = GetNumMiUnits(width);
393 uint32_t min_log2_tile_cols;
394 uint32_t max_log2_tile_cols;
395 GetTileNBits(mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
396 uint32_t max_ones = max_log2_tile_cols - min_log2_tile_cols;
398 uint32_t log2_tile_cols = min_log2_tile_cols;
401 RCHECK(reader->ReadBits(1, &has_more));
406 RCHECK(log2_tile_cols <= 6);
408 RCHECK(reader->SkipBitsConditional(
true, 1));
414 VP9Parser::VP9Parser() : width_(0), height_(0) {}
415 VP9Parser::~VP9Parser() {}
419 std::vector<VPxFrameInfo>* vpx_frames) {
422 RCHECK(ParseIfSuperframeIndex(data, data_size, vpx_frames));
424 for (
auto& vpx_frame : *vpx_frames) {
425 VLOG(4) <<
"process frame with size " << vpx_frame.frame_size;
426 BitReader reader(data, vpx_frame.frame_size);
427 uint8_t frame_marker;
428 RCHECK(reader.
ReadBits(2, &frame_marker));
429 RCHECK(frame_marker == VP9_FRAME_MARKER);
431 RCHECK(ReadProfile(&reader, writable_codec_config()));
433 bool show_existing_frame;
434 RCHECK(reader.
ReadBits(1, &show_existing_frame));
435 if (show_existing_frame) {
440 vpx_frame.is_keyframe =
false;
441 vpx_frame.uncompressed_header_size = vpx_frame.frame_size;
442 vpx_frame.width = width_;
443 vpx_frame.height = height_;
448 RCHECK(reader.
ReadBits(1, &is_interframe));
449 vpx_frame.is_keyframe = !is_interframe;
452 RCHECK(reader.
ReadBits(1, &show_frame));
453 bool error_resilient_mode;
454 RCHECK(reader.
ReadBits(1, &error_resilient_mode));
456 if (vpx_frame.is_keyframe) {
457 RCHECK(ReadSyncCode(&reader));
458 RCHECK(ReadBitDepthAndColorSpace(&reader, writable_codec_config()));
459 RCHECK(ReadFrameSizes(&reader, &width_, &height_));
461 bool intra_only =
false;
463 RCHECK(reader.
ReadBits(1, &intra_only));
464 if (!error_resilient_mode)
468 RCHECK(ReadSyncCode(&reader));
470 RCHECK(ReadBitDepthAndColorSpace(&reader, writable_codec_config()));
476 writable_codec_config()->set_chroma_subsampling(
477 VPCodecConfiguration::CHROMA_420_COLLOCATED_WITH_LUMA);
478 writable_codec_config()->set_bit_depth(8);
481 RCHECK(reader.
SkipBits(REF_FRAMES));
482 RCHECK(ReadFrameSizes(&reader, &width_, &height_));
484 RCHECK(reader.
SkipBits(REF_FRAMES));
485 RCHECK(reader.
SkipBits(REFS_PER_FRAME * (REF_FRAMES_LOG2 + 1)));
490 RCHECK(ReadFrameSizesWithRefs(&reader, &width_, &height_));
495 RCHECK(reader.
ReadBits(1, &interp_filter));
501 if (!error_resilient_mode) {
505 RCHECK(reader.
SkipBits(FRAME_CONTEXTS_LOG2));
507 VLOG(4) <<
"bits read before ReadLoopFilter: " << reader.
bit_position();
508 RCHECK(ReadLoopFilter(&reader));
509 RCHECK(ReadQuantization(&reader));
510 RCHECK(ReadSegmentation(&reader));
511 RCHECK(ReadTileInfo(width_, &reader));
513 uint16_t header_size;
514 RCHECK(reader.
ReadBits(16, &header_size));
515 vpx_frame.uncompressed_header_size =
517 vpx_frame.width = width_;
518 vpx_frame.height = height_;
520 VLOG(3) <<
"\n frame_size: " << vpx_frame.frame_size
521 <<
"\n uncompressed_header_size: "
522 << vpx_frame.uncompressed_header_size
524 <<
"\n header_size: " << header_size;
526 RCHECK(header_size > 0);
529 data += vpx_frame.frame_size;
536 uint8_t frame_marker;
537 RCHECK(reader.
ReadBits(2, &frame_marker));
538 RCHECK(frame_marker == VP9_FRAME_MARKER);
541 RCHECK(ReadProfile(&reader, &codec_config));
543 bool show_existing_frame;
544 RCHECK(reader.
ReadBits(1, &show_existing_frame));
545 if (show_existing_frame)
549 RCHECK(reader.
ReadBits(1, &is_interframe));
555 RCHECK(ReadSyncCode(&reader));