Chromium Code Reviews| Index: webrtc/modules/video_coding/utility/vp9_uncomp_header_parser.cc |
| diff --git a/webrtc/modules/video_coding/utility/vp9_uncomp_header_parser.cc b/webrtc/modules/video_coding/utility/vp9_uncomp_header_parser.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..0621a9228d0ae26e67fc416af427d5ada0b67a1f |
| --- /dev/null |
| +++ b/webrtc/modules/video_coding/utility/vp9_uncomp_header_parser.cc |
| @@ -0,0 +1,283 @@ |
| +/* |
| + * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. |
| + * |
| + * Use of this source code is governed by a BSD-style license |
| + * that can be found in the LICENSE file in the root of the source |
| + * tree. An additional intellectual property rights grant can be found |
| + * in the file PATENTS. All contributing project authors may |
| + * be found in the AUTHORS file in the root of the source tree. |
| + */ |
| +#include "webrtc/modules/video_coding/utility/vp9_uncomp_header_parser.h" |
| + |
|
brandtr
2017/05/19 14:10:27
General comment: have you looked into using the rt
jianj1
2017/05/19 21:20:56
Done.
|
| +#include "webrtc/base/logging.h" |
| + |
| +namespace webrtc { |
| + |
| +namespace vp9 { |
| +namespace { |
| +const size_t kVp9MaxProfile = 4; |
| +const size_t kVp9NumRefsPerFrame = 3; |
| +const size_t kVp9MaxRefLFDeltas = 4; |
| +const size_t kVp9MaxModeLFDeltas = 2; |
| +} // namespace |
| + |
| +static void VP9InitBitReader(VP9BitReader *const br, const uint8_t *const start, |
|
brandtr
2017/05/19 14:10:27
To me, it seems that VP9BitReader could be a full-
jianj1
2017/05/19 21:20:56
Done.
BTW, the same thing could be done to vp8_he
|
| + const uint8_t *const end) { |
|
brandtr
2017/05/19 14:10:27
const uint8_t*, here and in other signatures.
jianj1
2017/05/19 21:20:56
Done.
|
| + br->offset_ = 0; |
| + br->buf_ = start; |
| + br->buf_end_ = end; |
| +} |
| + |
| +static int32_t VP9GetBit(VP9BitReader *const br) { |
| + const size_t off = br->offset_; |
| + const size_t p = off >> 3; |
| + const int q = 7 - static_cast<int>(off & 0x7); |
| + if (br->buf_ + p < br->buf_end_) { |
| + const int bit = (br->buf_[p] >> q) & 1; |
| + br->offset_ = off + 1; |
| + return bit; |
| + } else { |
| + LOG(LS_WARNING) << "Failed to get QP. Reached EOF."; |
| + return 0; |
| + } |
| +} |
| + |
| +static int32_t VP9GetValue(VP9BitReader *const br, int bits) { |
| + int value = 0, bit; |
| + for (bit = bits - 1; bit >= 0; bit--) |
| + value |= VP9GetBit(br) << bit; |
| + return value; |
| +} |
| + |
| +static int32_t VP9GetSignedValue(VP9BitReader *const br, int bits) { |
| + const int value = VP9GetValue(br, bits); |
| + return VP9GetBit(br) ? -value : value; |
| +} |
| + |
| +static uint8_t VP9ReadProfile(VP9BitReader *const br) { |
| + uint8_t profile = 0; |
| + if (VP9GetBit(br)) |
| + profile |= 1; |
| + if (VP9GetBit(br)) |
| + profile |= 2; |
| + if (profile > 2 && VP9GetBit(br)) |
| + profile += 1; |
| + return profile; |
| +} |
| + |
| +static bool VP9ReadColorConfig(VP9BitReader *const br, const uint8_t profile) { |
| + if (profile == 2 || profile == 3) { |
| + // bitdepth |
| + VP9GetBit(br); |
| + } |
| + |
| + uint8_t color_space = VP9GetValue(br, 3); |
| + // SRGB is 7 |
| + if (color_space != 7) { |
| + // yuv range flag |
| + VP9GetBit(br); |
| + if (profile == 1 || profile == 3) { |
| + // subsampling x |
|
brandtr
2017/05/19 14:10:27
Comments: start with capital letter and end with p
jianj1
2017/05/19 21:20:56
Done.
|
| + VP9GetBit(br); |
| + // subsampling y |
| + VP9GetBit(br); |
| + // reserved |
| + if (VP9GetBit(br)) { |
| + LOG(LS_WARNING) << "Failed to get QP. Reserved bit set."; |
| + return false; |
| + } |
| + } |
| + } else { |
| + if (profile == 1 || profile == 3) { |
| + // reserved |
| + if (VP9GetBit(br)) { |
| + LOG(LS_WARNING) << "Failed to get QP. Reserved bit set."; |
| + return false; |
| + } |
| + } else { |
| + LOG(LS_WARNING) << "Failed to get QP. 4:4:4 color not supported in " |
| + "profile 0 or 2."; |
| + return false; |
| + } |
| + } |
| + |
| + return true; |
| +} |
| + |
| +static void VP9ReadFrameSize(VP9BitReader *const br) { |
| + // frame width |
| + uint32_t width = VP9GetValue(br, 16); |
| + (void)width; |
| + // frame height |
| + uint32_t height = VP9GetValue(br, 16); |
| + (void)height; |
| +} |
| + |
| +static void VP9ReadRenderSize(VP9BitReader *const br) { |
| + // scaling |
| + if (VP9GetBit(br)) { |
| + // render width |
| + uint32_t width = VP9GetValue(br, 16); |
| + // render height |
| + uint32_t height = VP9GetValue(br, 16); |
| + (void)width; |
| + (void)height; |
| + } |
| +} |
| + |
| +static void VP9ReadFrameSizeFromRefs(VP9BitReader *const br) { |
| + int found_ref = 0; |
| + for (size_t i = 0; i < kVp9NumRefsPerFrame; i++) { |
| + // size in refs |
| + found_ref = VP9GetBit(br); |
| + if (found_ref) |
| + break; |
| + } |
| + |
| + if (!found_ref) |
| + VP9ReadFrameSize(br); |
| + |
| + VP9ReadRenderSize(br); |
| +} |
| + |
| +static void VP9ReadInterpolationFilter(VP9BitReader *const br) { |
| + if (VP9GetBit(br)) |
| + return; |
| + |
| + VP9GetValue(br, 2); |
| +} |
| + |
| +static void VP9ReadLoopfilter(VP9BitReader *const br) { |
| + // filter level |
| + VP9GetValue(br, 6); |
| + // sharpness level |
| + VP9GetValue(br, 3); |
| + uint32_t mode_ref_delta_enabled = VP9GetBit(br); |
| + if (mode_ref_delta_enabled) { |
| + uint32_t mode_ref_delta_update = VP9GetBit(br); |
| + if (mode_ref_delta_update) { |
| + for (size_t i = 0; i < kVp9MaxRefLFDeltas; i++) { |
| + if (VP9GetBit(br)) |
| + VP9GetSignedValue(br, 6); |
| + } |
| + for (size_t i = 0; i < kVp9MaxModeLFDeltas; i++) { |
| + if (VP9GetBit(br)) |
| + VP9GetSignedValue(br, 6); |
| + } |
| + } |
| + } |
| +} |
| + |
| +bool GetQp(const uint8_t *buf, size_t length, int *qp) { |
| + VP9BitReader br; |
| + VP9InitBitReader(&br, buf, buf + length); |
| + |
| + // frame marker |
| + if (VP9GetValue(&br, 2) != 0x2) { |
| + LOG(LS_WARNING) << "Failed to get QP. Frame marker should be 2."; |
| + return false; |
| + } |
| + |
| + // profile |
| + uint8_t profile = VP9ReadProfile(&br); |
| + if (profile > kVp9MaxProfile) { |
| + LOG(LS_WARNING) << "Failed to get QP. Unsupported bitstream profile: " |
| + << profile; |
| + return false; |
| + } |
| + |
| + // show existing frame |
| + if (VP9GetBit(&br)) { |
| + VP9GetValue(&br, 3); |
| + return false; |
| + } |
| + |
| + // frame type: KEY_FRAME(0), INTER_FRAME(1) |
| + uint8_t frame_type = VP9GetBit(&br); |
| + // show frame |
| + uint8_t show_frame = VP9GetBit(&br); |
| + // error resilient |
| + uint8_t error_resilient = VP9GetBit(&br); |
| + |
| + if (!frame_type) { |
| + // sync code |
| + uint32_t sync_code = VP9GetValue(&br, 24); |
| + if (sync_code != 0x498342) { |
| + LOG(LS_WARNING) << "Failed to get QP. Invalid sync code."; |
| + return false; |
| + } |
| + |
| + if (!VP9ReadColorConfig(&br, profile)) |
| + return false; |
| + |
| + VP9ReadFrameSize(&br); |
| + VP9ReadRenderSize(&br); |
| + } else { |
| + uint8_t intra_only = 0; |
| + if (!show_frame) |
| + intra_only = VP9GetBit(&br); |
| + |
| + if (!error_resilient) |
| + // reset frame context |
| + VP9GetValue(&br, 2); |
| + |
| + if (intra_only) { |
| + // sync code |
| + if (VP9GetValue(&br, 24) != 0x498342) { |
| + LOG(LS_WARNING) << "Failed to get QP. Invalid sync code."; |
| + return false; |
| + } |
| + if (profile > 0) { |
| + if (!VP9ReadColorConfig(&br, profile)) |
| + return false; |
| + } |
| + // refresh frame flags |
| + VP9GetValue(&br, 8); |
| + |
| + VP9ReadFrameSize(&br); |
| + VP9ReadRenderSize(&br); |
| + } else { |
| + // refresh frame flags |
| + int32_t test = VP9GetValue(&br, 8); |
| + (void)test; |
| + |
| + for (size_t i = 0; i < kVp9NumRefsPerFrame; i++) { |
| + // ref frame index |
| + int32_t idx = VP9GetValue(&br, 3); |
| + // ref frame sign biases |
| + int32_t bias = VP9GetBit(&br); |
| + (void)idx; |
| + (void)bias; |
| + } |
| + |
| + VP9ReadFrameSizeFromRefs(&br); |
| + |
| + // allow high precision mv |
| + VP9GetBit(&br); |
| + |
| + // interpolation filter |
| + VP9ReadInterpolationFilter(&br); |
| + } |
| + } |
| + |
| + if (!error_resilient) { |
| + // refresh frame context |
| + VP9GetBit(&br); |
| + // frame parallel decoding mode |
| + VP9GetBit(&br); |
| + } |
| + |
| + // frame context index |
| + VP9GetValue(&br, 2); |
| + |
| + VP9ReadLoopfilter(&br); |
| + |
| + // Base QP. |
| + const int base_q0 = VP9GetValue(&br, 8); |
| + *qp = base_q0; |
| + return true; |
| +} |
| + |
| +} // namespace vp9 |
| + |
| +} // namespace webrtc |