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

Unified Diff: media/filters/vp9_parser.cc

Issue 1258083004: Add a Vp9Parser implementation (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: remove dependent files from this CL Created 5 years, 5 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 side-by-side diff with in-line comments
Download patch
Index: media/filters/vp9_parser.cc
diff --git a/media/filters/vp9_parser.cc b/media/filters/vp9_parser.cc
new file mode 100644
index 0000000000000000000000000000000000000000..278772126394f3c02dd077744f6d0aabe8fae9ff
--- /dev/null
+++ b/media/filters/vp9_parser.cc
@@ -0,0 +1,409 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+//
+// This file contains an implementation of a VP9 bitstream parser.
+
+#include "media/filters/vp9_parser.h"
+
+#include "base/logging.h"
+
+namespace {
+
+int GetMinLog2TileCols(int sb64_cols) {
Pawel Osciak 2015/07/30 08:27:37 static? Also please document what this function is
kcwu 2015/07/31 08:51:51 IIUC, no need to add static qualifier if it is alr
+ const int kMaxTileWidthB64 = 64;
+ int min_log2 = 0;
+ while ((kMaxTileWidthB64 << min_log2) < sb64_cols)
+ min_log2++;
+ return min_log2;
+}
+
+int GetMaxLog2TileCols(int sb64_cols) {
Pawel Osciak 2015/07/30 08:27:37 static?
kcwu 2015/07/31 08:51:51 Acknowledged.
+ const int kMinTileWidthB64 = 4;
+ int max_log2 = 1;
+ while ((sb64_cols >> max_log2) >= kMinTileWidthB64)
+ max_log2++;
+ return max_log2 - 1;
+}
+
+} // namespace
+
+namespace media {
+
+Vp9Parser::Vp9Parser() : stream_(nullptr), size_(0) {
+ memset(&ref_slots_, 0, sizeof(ref_slots_));
+}
+
+uint8_t Vp9Parser::ReadProfile() {
+ uint8_t profile = 0;
+
+ // LSB first.
+ profile |= reader_.ReadBit();
+ profile |= reader_.ReadBit() << 1;
+ if (profile > 2)
+ profile |= reader_.ReadBit() << 2;
+ return profile;
+}
+
+bool Vp9Parser::VerifySyncCode() {
+ const int kSyncCode = 0x498342;
+ if (reader_.ReadLiteral(8 * 3) != kSyncCode) {
Pawel Osciak 2015/07/30 08:27:37 General comment: ReadLiteral/ReadBit should have a
kcwu 2015/07/31 08:51:51 Per our chat. We agreed checking reader failure at
+ DLOG(ERROR) << "Invalid frame sync code";
+ return false;
Pawel Osciak 2015/07/30 08:27:38 DVLOG please
kcwu1 2015/07/30 08:55:49 Only this one or all in this class?
Pawel Osciak 2015/07/30 08:59:24 All please.
kcwu1 2015/07/31 04:36:01 Done.
+ }
+ return true;
+}
+
+bool Vp9Parser::ReadBitDepthColorSpaceSampling(Vp9FrameHeader* fhdr) {
+ if (fhdr->profile >= 2) {
+ if (reader_.ReadBit())
+ fhdr->bit_depth = 12;
+ else
+ fhdr->bit_depth = 10;
+ } else {
+ fhdr->bit_depth = 8;
+ }
+
+ fhdr->color_space = static_cast<Vp9ColorSpace>(reader_.ReadLiteral(3));
+ if (fhdr->color_space != Vp9ColorSpace::SRGB) {
+ fhdr->yuv_range = reader_.ReadBit();
+ if (fhdr->profile == 1 || fhdr->profile == 3) {
+ fhdr->subsampling_x = reader_.ReadBit();
+ fhdr->subsampling_y = reader_.ReadBit();
+ if (fhdr->subsampling_x == 1 && fhdr->subsampling_y == 1) {
+ DLOG(ERROR) << "4:2:0 color not supported in profile 1 or 3";
+ return false;
+ }
+ bool reserved = reader_.ReadBit();
+ if (reserved) {
+ DLOG(ERROR) << "reserved bit set";
+ return false;
+ }
+ } else {
+ fhdr->subsampling_x = fhdr->subsampling_y = 1;
+ }
+ } else {
+ if (fhdr->profile == 1 || fhdr->profile == 3) {
+ fhdr->subsampling_x = fhdr->subsampling_y = 0;
+
+ // this bit is not specified in spec??
Pawel Osciak 2015/07/30 08:27:37 s/this/This/ s/??/?/
kcwu1 2015/07/30 08:55:49 just keep note. I expect this will be removed befo
+ bool reserved = reader_.ReadBit();
+ if (reserved) {
+ DLOG(ERROR) << "reserved bit set";
+ return false;
+ }
+ } else {
+ DLOG(ERROR) << "4:4:4 color not supported in profile 0 or 2";
+ return false;
+ }
+ }
+
+ return true;
+}
+
+void Vp9Parser::ReadFrameSize(Vp9FrameHeader* fhdr) {
+ fhdr->width = reader_.ReadLiteral(16) + 1;
+ fhdr->height = reader_.ReadLiteral(16) + 1;
+}
+
+void Vp9Parser::ReadFrameSizeFromRefs(Vp9FrameHeader* fhdr) {
+ for (int i = 0; i < kVp9RefsPerFrame; i++) {
Pawel Osciak 2015/07/30 08:27:37 s/int/size_t/ Please use size_t in general for it
kcwu1 2015/07/31 04:36:02 Done.
+ if (reader_.ReadBit()) {
+ fhdr->width = ref_slots_[i].width;
+ fhdr->height = ref_slots_[i].height;
+ return;
+ }
+ }
+
+ fhdr->width = reader_.ReadLiteral(16) + 1;
+ fhdr->height = reader_.ReadLiteral(16) + 1;
+}
+
+void Vp9Parser::ReadDisplayFrameSize(Vp9FrameHeader* fhdr) {
+ if (reader_.ReadBit()) {
+ fhdr->display_width = reader_.ReadLiteral(16) + 1;
+ fhdr->display_height = reader_.ReadLiteral(16) + 1;
+ } else {
+ fhdr->display_width = fhdr->width;
+ fhdr->display_height = fhdr->height;
+ }
+}
+
+Vp9InterpFilter Vp9Parser::ReadInterpFilter() {
+ if (reader_.ReadBit())
+ return Vp9InterpFilter::INTERP_FILTER_SELECT;
+
+ // The mapping table for next two bits.
+ Vp9InterpFilter table[] = {
Pawel Osciak 2015/07/30 08:27:37 const?
kcwu1 2015/07/31 04:36:01 Done.
+ Vp9InterpFilter::EIGHTTAP_SMOOTH, Vp9InterpFilter::EIGHTTAP,
+ Vp9InterpFilter::EIGHTTAP_SHARP, Vp9InterpFilter::BILINEAR,
+ };
+ return table[reader_.ReadLiteral(2)];
Pawel Osciak 2015/07/30 08:27:38 Is this different from return ReadLiteral(2) + 1 ?
kcwu1 2015/07/30 08:55:49 No difference. I just followed libvpx and feel it
Pawel Osciak 2015/07/30 08:59:24 Acknowledged.
+}
+
+void Vp9Parser::ReadLoopFilter(Vp9LoopFilter* loop_filter) {
+ loop_filter->filter_level = reader_.ReadLiteral(6);
+ loop_filter->sharpness_level = reader_.ReadLiteral(3);
+
+ loop_filter->mode_ref_delta_enabled = reader_.ReadBit();
+ if (loop_filter->mode_ref_delta_enabled) {
+ loop_filter->mode_ref_delta_update = reader_.ReadBit();
+ if (loop_filter->mode_ref_delta_update) {
+ for (int i = 0; i < Vp9LoopFilter::kNumRefDeltas; i++) {
+ loop_filter->update_ref_deltas[i] = reader_.ReadBit();
+ if (loop_filter->update_ref_deltas[i])
+ loop_filter->ref_deltas[i] = reader_.ReadSignedLiteral(6);
+ }
+
+ for (int i = 0; i < Vp9LoopFilter::kNumModeDeltas; i++) {
+ loop_filter->update_mode_deltas[i] = reader_.ReadBit();
+ if (loop_filter->update_mode_deltas[i])
+ loop_filter->mode_deltas[i] = reader_.ReadLiteral(6);
+ }
+ }
+ } else {
+ loop_filter->mode_ref_delta_update = false;
+ }
+}
+
+void Vp9Parser::ReadQuantization(Vp9QuantizationParams* quants) {
+ quants->base_qindex = reader_.ReadLiteral(8);
+
+ if (reader_.ReadBit())
+ quants->y_dc_delta = reader_.ReadSignedLiteral(4);
+ else
+ quants->y_dc_delta = 0;
+
+ if (reader_.ReadBit())
+ quants->uv_ac_delta = reader_.ReadSignedLiteral(4);
+ else
+ quants->uv_ac_delta = 0;
+
+ if (reader_.ReadBit())
+ quants->uv_dc_delta = reader_.ReadSignedLiteral(4);
+ else
+ quants->uv_dc_delta = 0;
+}
+
+void Vp9Parser::ReadSegmentationMap(Vp9Segmentation* segment) {
+ for (int i = 0; i < Vp9Segmentation::kTreeProbs; i++) {
+ if (reader_.ReadBit())
+ segment->tree_probs[i] = reader_.ReadLiteral(8);
+ else
+ segment->tree_probs[i] = kVp9MaxProb;
+ }
+
+ for (int i = 0; i < Vp9Segmentation::kPredictionProbs; i++)
+ segment->pred_probs[i] = kVp9MaxProb;
+ if (reader_.ReadBit()) {
Pawel Osciak 2015/07/30 11:52:31 Please store this in frame header (as temporal_upd
kcwu1 2015/07/31 04:36:01 Done.
+ for (int i = 0; i < Vp9Segmentation::kPredictionProbs; i++) {
+ if (reader_.ReadBit())
+ segment->pred_probs[i] = reader_.ReadLiteral(8);
+ }
+ }
+}
+
+void Vp9Parser::ReadSegmentationData(Vp9Segmentation* segment) {
+ segment->abs_delta = reader_.ReadBit();
+
+ const int kFeatureDataBits[] = {7, 6, 2, 0};
+ const bool kFeatureDataSigned[] = {true, true, false, false};
+
+ for (int i = 0; i < Vp9Segmentation::kNumSegment; i++) {
+ for (int j = 0; j < Vp9Segmentation::kNumFeature; j++) {
+ int8_t data = 0;
+ segment->feature_enabled[i][j] = reader_.ReadBit();
+ if (segment->feature_enabled[i][j]) {
+ data = reader_.ReadLiteral(kFeatureDataBits[j]);
+ if (kFeatureDataSigned[j])
+ if (reader_.ReadBit())
+ data = -data;
+ }
+ segment->feature_data[i][j] = data;
+ }
+ }
+}
+
+void Vp9Parser::ReadSegmentation(Vp9Segmentation* segment) {
+ segment->enabled = reader_.ReadBit();
+
+ if (!segment->enabled) {
+ segment->update_map = false;
+ segment->update_data = false;
+ return;
+ }
+
+ segment->update_map = reader_.ReadBit();
+ if (segment->update_map)
+ ReadSegmentationMap(segment);
+
+ segment->update_data = reader_.ReadBit();
+ if (segment->update_data)
+ ReadSegmentationData(segment);
+}
+
+void Vp9Parser::ReadTiles(Vp9FrameHeader* fhdr) {
+ int sb64_cols = (fhdr->width + 63) / 64;
+
+ int min_log2_tile_cols = GetMinLog2TileCols(sb64_cols);
+ int max_log2_tile_cols = GetMaxLog2TileCols(sb64_cols);
+
+ int max_ones = max_log2_tile_cols - min_log2_tile_cols;
+ fhdr->log2_tile_cols = min_log2_tile_cols;
+ while (max_ones-- && reader_.ReadBit())
+ fhdr->log2_tile_cols++;
+
+ if (reader_.ReadBit())
+ fhdr->log2_tile_rows = reader_.ReadLiteral(2) - 1;
+ else
+ fhdr->log2_tile_rows = 0;
+}
+
+bool Vp9Parser::ParseUncompressedHeader(Vp9FrameHeader* fhdr) {
+ reader_.Initialize(stream_, size_);
+
+ // frame marker
+ if (reader_.ReadLiteral(2) != 0x2)
+ return false;
+
+ fhdr->profile = ReadProfile();
+ if (fhdr->profile >= kVp9MaxProfile) {
+ DLOG(ERROR) << "Unsupported bitstream profile";
+ return false;
+ }
+
+ fhdr->show_existing_frame = reader_.ReadBit();
+ if (fhdr->show_existing_frame) {
+ fhdr->frame_to_show = reader_.ReadLiteral(3);
+ fhdr->loop_filter.filter_level = 0;
Pawel Osciak 2015/07/30 08:27:37 It seems that sometimes we initialize members to 0
kcwu1 2015/07/31 04:36:01 Done.
+ fhdr->show_frame = true;
+
+ fhdr->first_partition_size = 0;
+ fhdr->compressed_header = nullptr;
+
+ if (reader_.IsOutOfBuffer()) {
Pawel Osciak 2015/07/30 08:27:37 This shouldn't be needed once we make ReadBit/Lite
kcwu1 2015/07/30 08:55:49 Since out of data is rare, I'd prefer checked only
Pawel Osciak 2015/07/30 08:59:24 We can't read beyond memory that we have available
kcwu1 2015/07/30 09:04:15 The reader returns 0 if out of buffer. The return
+ DLOG(ERROR) << "parser reads beyond the end of buffer";
+ return false;
+ }
+ return true;
+ }
+
+ fhdr->frame_type = static_cast<Vp9FrameHeader::FrameType>(reader_.ReadBit());
+ fhdr->show_frame = reader_.ReadBit();
+ fhdr->error_resilient_mode = reader_.ReadBit();
+
+ if (fhdr->IsKeyframe()) {
+ if (!VerifySyncCode())
+ return false;
+
+ if (!ReadBitDepthColorSpaceSampling(fhdr))
+ return false;
+
+ memset(&ref_slots_, 0, sizeof(ref_slots_));
+ for (int i = 0; i < kVp9RefFrames; i++)
+ fhdr->refresh_flag[i] = true;
+
+ ReadFrameSize(fhdr);
+ ReadDisplayFrameSize(fhdr);
+ } else {
+ if (fhdr->show_frame)
+ fhdr->intra_only = false;
+ else
+ fhdr->intra_only = reader_.ReadBit();
+ if (fhdr->error_resilient_mode)
Pawel Osciak 2015/07/30 08:27:37 Please add empty line above.
kcwu1 2015/07/31 04:36:01 Done.
+ fhdr->reset_context = false;
+ else
+ fhdr->reset_context = reader_.ReadLiteral(2);
+
+ if (fhdr->intra_only) {
+ if (!VerifySyncCode())
+ return false;
+
+ if (fhdr->profile > 0) {
+ if (!ReadBitDepthColorSpaceSampling(fhdr))
+ return false;
+ } else {
+ fhdr->bit_depth = 8;
+ fhdr->color_space = Vp9ColorSpace::BT_601;
+ fhdr->subsampling_x = fhdr->subsampling_y = 1;
+ }
+
+ for (int i = 0; i < kVp9RefFrames; i++)
+ fhdr->refresh_flag[i] = reader_.ReadBit();
+ ReadFrameSize(fhdr);
+ ReadDisplayFrameSize(fhdr);
+ } else {
+ for (int i = 0; i < kVp9RefFrames; i++)
+ fhdr->refresh_flag[i] = reader_.ReadBit();
+
+ for (int i = 0; i < kVp9RefsPerFrame; i++) {
+ fhdr->frame_refs[i] = reader_.ReadLiteral(kVp9RefFramesLog2);
+ fhdr->ref_sign_biases[i] = reader_.ReadBit();
+ }
+
+ ReadFrameSizeFromRefs(fhdr);
+ ReadDisplayFrameSize(fhdr);
+
+ fhdr->allow_high_precision_mv = reader_.ReadBit();
+ fhdr->interp_filter = ReadInterpFilter();
+ }
+ }
+
+ if (fhdr->error_resilient_mode) {
+ fhdr->refresh_frame_context = false;
+ fhdr->frame_parallel_decoding_mode = true;
+ } else {
+ fhdr->refresh_frame_context = reader_.ReadBit();
+ fhdr->frame_parallel_decoding_mode = reader_.ReadBit();
+ }
+
+ const int kFrameContextLog2 = 2;
Pawel Osciak 2015/07/30 08:27:37 Any reason to make this specifically into a consta
kcwu1 2015/07/30 08:55:49 Do you mean why not use literal 2 directly?
Pawel Osciak 2015/07/30 08:59:24 Yes.
kcwu1 2015/07/31 04:36:01 Done.
+ fhdr->frame_context_idx = reader_.ReadLiteral(kFrameContextLog2);
+
+ ReadLoopFilter(&fhdr->loop_filter);
+ ReadQuantization(&fhdr->quant_params);
+ ReadSegmentation(&fhdr->segment);
+
+ ReadTiles(fhdr);
+
+ fhdr->first_partition_size = reader_.ReadLiteral(16);
+ if (fhdr->first_partition_size == 0) {
+ DLOG(ERROR) << "invalid header size";
+ return false;
+ }
+ fhdr->compressed_header = stream_ + reader_.GetBytesRead();
Pawel Osciak 2015/07/30 11:52:31 Could we also store the size of uncompressed heade
kcwu1 2015/07/31 04:36:01 Done. And remove |compressed_header| since it is r
+
+ if (reader_.IsOutOfBuffer()) {
+ DLOG(ERROR) << "parser reads beyond the end of buffer";
+ return false;
+ }
+
+ return true;
+}
+
+void Vp9Parser::UpdateSlots(Vp9FrameHeader* fhdr) {
+ for (int i = 0; i < kVp9RefFrames; i++) {
+ if (fhdr->refresh_flag[i]) {
+ ref_slots_[i].used = true;
+ ref_slots_[i].width = fhdr->width;
+ ref_slots_[i].height = fhdr->height;
+ }
+ }
+}
+
+bool Vp9Parser::ParseFrame(const uint8_t* ptr,
+ size_t frame_size,
+ Vp9FrameHeader* fhdr) {
+ stream_ = ptr;
+ size_ = frame_size;
+ memset(fhdr, 0, sizeof(*fhdr));
+
+ if (!ParseUncompressedHeader(fhdr))
+ return false;
+
+ UpdateSlots(fhdr);
+
+ return true;
+}
+
+} // namespace media

Powered by Google App Engine
This is Rietveld 408576698