Revert^2 "VP9: Synthesize CSD from frame header"
44ad2bf9b0596fd392a03e3b868c03cbf1cb08ca
Relands commits in /q/submissionid:2953128-vp9_csd_synthesis.
Reason for revert: The encoder tests have been modified to now
accept the synthesized VP9 CSD in aosp/3019090.
Test: atest android.mediav2.cts.DecoderColorAspectsTest#testColorAspects
Bug: 295804596
Change-Id: Iad5c9c698615424b2ebe854d82e94b07f3646e64
diff --git a/media/module/extractors/mkv/MatroskaExtractor.cpp b/media/module/extractors/mkv/MatroskaExtractor.cpp
index 6900341..a328ab9 100644
--- a/media/module/extractors/mkv/MatroskaExtractor.cpp
+++ b/media/module/extractors/mkv/MatroskaExtractor.cpp
@@ -1769,6 +1769,30 @@
}
+status_t MatroskaExtractor::synthesizeVP9(TrackInfo* trackInfo, size_t index) {
+ BlockIterator iter(this, trackInfo->mTrackNum, index);
+ if (iter.eos()) {
+ return ERROR_MALFORMED;
+ }
+
+ const mkvparser::Block* block = iter.block();
+ if (block->GetFrameCount() <= 0) {
+ return ERROR_MALFORMED;
+ }
+
+ const mkvparser::Block::Frame& frame = block->GetFrame(0);
+ auto tmpData = heapbuffer<unsigned char>(frame.len);
+ long n = frame.Read(mReader, tmpData.get());
+ if (n != 0) {
+ return ERROR_MALFORMED;
+ }
+
+ if (!MakeVP9CodecSpecificData(trackInfo->mMeta, tmpData.get(), frame.len)) {
+ return ERROR_MALFORMED;
+ }
+
+ return OK;
+}
static inline bool isValidInt32ColourValue(long long value) {
return value != mkvparser::Colour::kValueNotPresent
@@ -2002,6 +2026,8 @@
// specified in http://www.webmproject.org/vp9/profiles/.
AMediaFormat_setBuffer(meta,
AMEDIAFORMAT_KEY_CSD_0, codecPrivate, codecPrivateSize);
+ } else {
+ isSetCsdFrom1stFrame = true;
}
} else if (!strcmp("V_AV1", codecID)) {
AMediaFormat_setString(meta, AMEDIAFORMAT_KEY_MIME, MEDIA_MIMETYPE_VIDEO_AV1);
@@ -2254,6 +2280,14 @@
mTracks.pop();
continue;
}
+ } else if ((!strcmp("V_VP9", codecID) && codecPrivateSize == 0) ||
+ (!strcmp(mimetype, MEDIA_MIMETYPE_VIDEO_VP9) && isSetCsdFrom1stFrame)) {
+ // Attempt to recover from VP9 track without codec private data
+ err = synthesizeVP9(trackInfo, n);
+ if (err != OK) {
+ mTracks.pop();
+ continue;
+ }
}
// the TrackInfo owns the metadata now
meta = nullptr;
diff --git a/media/module/extractors/mkv/include/MatroskaExtractor.h b/media/module/extractors/mkv/include/MatroskaExtractor.h
index 99fad17..2e4d955 100644
--- a/media/module/extractors/mkv/include/MatroskaExtractor.h
+++ b/media/module/extractors/mkv/include/MatroskaExtractor.h
@@ -95,6 +95,7 @@
status_t synthesizeAVCC(TrackInfo *trackInfo, size_t index);
status_t synthesizeMPEG2(TrackInfo *trackInfo, size_t index);
status_t synthesizeMPEG4(TrackInfo *trackInfo, size_t index);
+ status_t synthesizeVP9(TrackInfo* trackInfo, size_t index);
status_t initTrackInfo(
const mkvparser::Track *track,
AMediaFormat *meta,
diff --git a/media/module/metadatautils/MetaDataUtils.cpp b/media/module/metadatautils/MetaDataUtils.cpp
index db60f04..e5854b6 100644
--- a/media/module/metadatautils/MetaDataUtils.cpp
+++ b/media/module/metadatautils/MetaDataUtils.cpp
@@ -81,6 +81,124 @@
return true;
}
+// The param data contains the first frame data, starting with the uncompressed frame
+// header. This uncompressed header (refer section 6.2 of the VP9 bitstream spec) is
+// used to parse profile, bitdepth and subsampling.
+bool MakeVP9CodecSpecificData(AMediaFormat* meta, const uint8_t* data, size_t size) {
+ if (meta == nullptr || data == nullptr || size == 0) {
+ return false;
+ }
+
+ ABitReader bits(data, size);
+
+ // First 2 bits of the uncompressed header should be the frame_marker.
+ if (bits.getBits(2) != 0b10) {
+ return false;
+ }
+
+ int32_t profileLowBit = bits.getBits(1);
+ int32_t profileHighBit = bits.getBits(1);
+ int32_t profile = profileHighBit * 2 + profileLowBit;
+
+ // One reserved '0' bit if profile is 3.
+ if (profile == 3 && bits.getBits(1) != 0) {
+ return false;
+ }
+
+ // If show_existing_frame is set, we get no more data. Since this is
+ // expected to be the first frame, we can return false which will cascade
+ // into ERROR_MALFORMED.
+ if (bits.getBits(1)) {
+ return false;
+ }
+
+ // This should be the first frame, so expect a KEY_FRAME.
+ // if (frame_type != KEY_FRAME)
+ if (bits.getBits(1) != 0) {
+ return false;
+ }
+
+ // Upto 7 bits could be read till now, which were guaranteed to be available
+ // since size > 0. Check for bits available before reading them from now on.
+ if (bits.numBitsLeft() < 26) {
+ return false;
+ }
+ // Discard show_frame and error_resilient_mode.
+ bits.skipBits(2);
+
+ // Check for sync code.
+ if (bits.getBits(24) != 0x498342) {
+ return false;
+ }
+
+ int32_t bitDepth;
+ if (profile >= 2) {
+ if (bits.numBitsLeft() < 1) {
+ return false;
+ }
+ bitDepth = bits.getBits(1) ? 12 : 10;
+ } else {
+ bitDepth = 8;
+ }
+
+ uint32_t colorspace;
+ if (!bits.getBitsGraceful(3, &colorspace)) {
+ return false;
+ }
+
+ int32_t chromaSubsampling = -1;
+ if (colorspace != 7 /*SRGB*/) {
+ // Skip yuv_range_flag
+ if (!bits.skipBits(1)) {
+ return false;
+ }
+ // Check for subsampling only for profiles 1 and 3.
+ if (profile == 1 || profile == 3) {
+ uint32_t ss_x;
+ uint32_t ss_y;
+ if (bits.getBitsGraceful(1, &ss_x) && bits.getBitsGraceful(1, &ss_y)) {
+ chromaSubsampling = ss_x << 1 & ss_y;
+ } else {
+ return false;
+ }
+ } else {
+ chromaSubsampling = 3;
+ }
+ } else {
+ if (profile == 1 || profile == 3) {
+ chromaSubsampling = 0;
+ }
+ }
+
+ int32_t csdSize = 6;
+ if (chromaSubsampling != -1) {
+ csdSize += 3;
+ }
+
+ // Create VP9 Codec Feature Metadata (CodecPrivate) that can be parsed
+ // https://www.webmproject.org/docs/container/#vp9-codec-feature-metadata-codecprivate
+ sp<ABuffer> csd = sp<ABuffer>::make(csdSize);
+ uint8_t* csdData = csd->data();
+
+ *csdData++ = 0x01 /* FEATURE PROFILE */;
+ *csdData++ = 0x01 /* length */;
+ *csdData++ = profile;
+
+ *csdData++ = 0x03 /* FEATURE BITDEPTH */;
+ *csdData++ = 0x01 /* length */;
+ *csdData++ = bitDepth;
+
+ // csdSize more than 6 means chroma subsampling data was found.
+ if (csdSize > 6) {
+ *csdData++ = 0x04 /* FEATURE SUBSAMPLING */;
+ *csdData++ = 0x01 /* length */;
+ *csdData++ = chromaSubsampling;
+ }
+
+ AMediaFormat_setBuffer(meta, AMEDIAFORMAT_KEY_CSD_0, csd->data(), csd->size());
+ return true;
+}
+
bool MakeAACCodecSpecificData(MetaDataBase &meta, const uint8_t *data, size_t size) {
if (data == nullptr || size < 7) {
return false;
diff --git a/media/module/metadatautils/include/media/stagefright/MetaDataUtils.h b/media/module/metadatautils/include/media/stagefright/MetaDataUtils.h
index dcaf27f..69cf21a 100644
--- a/media/module/metadatautils/include/media/stagefright/MetaDataUtils.h
+++ b/media/module/metadatautils/include/media/stagefright/MetaDataUtils.h
@@ -38,6 +38,8 @@
void parseVorbisComment(
AMediaFormat *fileMeta, const char *comment, size_t commentLength);
+bool MakeVP9CodecSpecificData(AMediaFormat* meta, const uint8_t* data, size_t size);
+
} // namespace android
#endif // META_DATA_UTILS_H_