Store temporal_id and spatial_id of decoded frame

BUG=aomedia:2993

Change-Id: Iee4ad2d35f221b8a02377f8c09d7d19255c65c52
diff --git a/av1/av1_dx_iface.c b/av1/av1_dx_iface.c
index 02968ab..43374d0 100644
--- a/av1/av1_dx_iface.c
+++ b/av1/av1_dx_iface.c
@@ -821,8 +821,8 @@
 
         ctx->img.fb_priv = output_frame_buf->raw_frame_buffer.priv;
         img = &ctx->img;
-        img->temporal_id = cm->temporal_layer_id;
-        img->spatial_id = cm->spatial_layer_id;
+        img->temporal_id = output_frame_buf->temporal_id;
+        img->spatial_id = output_frame_buf->spatial_id;
         if (pbi->skip_film_grain) grain_params->apply_grain = 0;
         aom_image_t *res =
             add_grain_if_needed(ctx, img, &ctx->image_with_grain, grain_params);
diff --git a/av1/common/av1_common_int.h b/av1/common/av1_common_int.h
index 981a186..0552324 100644
--- a/av1/common/av1_common_int.h
+++ b/av1/common/av1_common_int.h
@@ -154,6 +154,8 @@
   aom_film_grain_t film_grain_params;
   aom_codec_frame_buffer_t raw_frame_buffer;
   YV12_BUFFER_CONFIG buf;
+  int temporal_id;  // Temporal layer ID of the frame
+  int spatial_id;   // Spatial layer ID of the frame
   FRAME_TYPE frame_type;
 
   // This is only used in the encoder but needs to be indexed per ref frame
diff --git a/av1/decoder/obu.c b/av1/decoder/obu.c
index 6c80148..6011daf 100644
--- a/av1/decoder/obu.c
+++ b/av1/decoder/obu.c
@@ -983,6 +983,8 @@
 
         decoded_payload_size = frame_header_size;
         pbi->frame_header_size = frame_header_size;
+        cm->cur_frame->temporal_id = obu_header.temporal_layer_id;
+        cm->cur_frame->spatial_id = obu_header.spatial_layer_id;
 
         if (cm->show_existing_frame) {
           if (obu_header.type == OBU_FRAME) {
diff --git a/test/decode_scalability_test.cc b/test/decode_scalability_test.cc
new file mode 100644
index 0000000..30ff01a
--- /dev/null
+++ b/test/decode_scalability_test.cc
@@ -0,0 +1,117 @@
+/*
+ * Copyright (c) 2021, Alliance for Open Media. All rights reserved
+ *
+ * This source code is subject to the terms of the BSD 2 Clause License and
+ * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
+ * was not distributed with this source code in the LICENSE file, you can
+ * obtain it at www.aomedia.org/license/software. If the Alliance for Open
+ * Media Patent License 1.0 was not distributed with this source code in the
+ * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
+ */
+
+#include <ostream>
+#include <string>
+
+#include "test/codec_factory.h"
+#include "test/decode_test_driver.h"
+#include "test/ivf_video_source.h"
+#include "test/util.h"
+#include "test/video_source.h"
+#include "third_party/googletest/src/googletest/include/gtest/gtest.h"
+
+namespace {
+
+struct ObuExtensionHeader {
+  int temporal_id;
+  int spatial_id;
+};
+
+struct DecodeParam {
+  const char *filename;
+  const ObuExtensionHeader *headers;
+  size_t num_headers;
+};
+
+std::ostream &operator<<(std::ostream &os, const DecodeParam &dp) {
+  return os << "file: " << dp.filename;
+}
+
+class DecodeScalabilityTest
+    : public ::libaom_test::DecoderTest,
+      public ::libaom_test::CodecTestWithParam<DecodeParam> {
+ protected:
+  DecodeScalabilityTest()
+      : DecoderTest(GET_PARAM(0)), headers_(GET_PARAM(1).headers),
+        num_headers_(GET_PARAM(1).num_headers) {}
+
+  ~DecodeScalabilityTest() override {}
+
+  void PreDecodeFrameHook(const libaom_test::CompressedVideoSource &video,
+                          libaom_test::Decoder *decoder) override {
+    if (video.frame_number() == 0)
+      decoder->Control(AV1D_SET_OUTPUT_ALL_LAYERS, 1);
+  }
+
+  void DecompressedFrameHook(const aom_image_t &img,
+                             const unsigned int /*frame_number*/) override {
+    const ObuExtensionHeader &header = headers_[header_index_];
+    EXPECT_EQ(img.temporal_id, header.temporal_id);
+    EXPECT_EQ(img.spatial_id, header.spatial_id);
+    header_index_ = (header_index_ + 1) % num_headers_;
+  }
+
+  void RunTest() {
+    const DecodeParam input = GET_PARAM(1);
+    aom_codec_dec_cfg_t cfg = { 1, 0, 0, !FORCE_HIGHBITDEPTH_DECODING };
+    const std::string filename = input.filename;
+    libaom_test::IVFVideoSource decode_video(filename);
+    decode_video.Init();
+
+    ASSERT_NO_FATAL_FAILURE(RunLoop(&decode_video, cfg));
+  }
+
+ private:
+  const ObuExtensionHeader *const headers_;
+  const size_t num_headers_;
+  size_t header_index_ = 0;
+};
+
+TEST_P(DecodeScalabilityTest, ObuExtensionHeader) { RunTest(); }
+
+// For all test files, we have:
+//   operatingPoint = 0
+//   OperatingPointIdc = operating_point_idc[ 0 ]
+
+// av1-1-b8-22-svc-L1T2.ivf:
+//   operating_points_cnt_minus_1 = 1
+//   operating_point_idc[ 0 ] = 0x103
+//   operating_point_idc[ 1 ] = 0x101
+const ObuExtensionHeader kL1T2Headers[2] = { { 0, 0 }, { 1, 0 } };
+
+// av1-1-b8-22-svc-L2T1.ivf:
+//   operating_points_cnt_minus_1 = 1
+//   operating_point_idc[ 0 ] = 0x301
+//   operating_point_idc[ 1 ] = 0x101
+const ObuExtensionHeader kL2T1Headers[2] = { { 0, 0 }, { 0, 1 } };
+
+// av1-1-b8-22-svc-L2T2.ivf:
+//   operating_points_cnt_minus_1 = 3
+//   operating_point_idc[ 0 ] = 0x303
+//   operating_point_idc[ 1 ] = 0x301
+//   operating_point_idc[ 2 ] = 0x103
+//   operating_point_idc[ 3 ] = 0x101
+const ObuExtensionHeader kL2T2Headers[4] = {
+  { 0, 0 }, { 0, 1 }, { 1, 0 }, { 1, 1 }
+};
+
+const DecodeParam kAV1DecodeScalabilityTests[] = {
+  // { filename, headers, num_headers }
+  { "av1-1-b8-22-svc-L1T2.ivf", kL1T2Headers, 2 },
+  { "av1-1-b8-22-svc-L2T1.ivf", kL2T1Headers, 2 },
+  { "av1-1-b8-22-svc-L2T2.ivf", kL2T2Headers, 4 },
+};
+
+AV1_INSTANTIATE_TEST_SUITE(DecodeScalabilityTest,
+                           ::testing::ValuesIn(kAV1DecodeScalabilityTests));
+
+}  // namespace
diff --git a/test/test.cmake b/test/test.cmake
index 47785d6..35c0d28 100644
--- a/test/test.cmake
+++ b/test/test.cmake
@@ -55,6 +55,7 @@
 endif()
 
 list(APPEND AOM_UNIT_TEST_DECODER_SOURCES "${AOM_ROOT}/test/decode_api_test.cc"
+            "${AOM_ROOT}/test/decode_scalability_test.cc"
             "${AOM_ROOT}/test/external_frame_buffer_test.cc"
             "${AOM_ROOT}/test/invalid_file_test.cc"
             "${AOM_ROOT}/test/test_vector_test.cc"