Snap for 9550733 from 61e0951e671386282452309d31eadabcec003704 to mainline-tzdata5-release

Change-Id: I87a1550889799a92b837df6f08599e73252f198e
diff --git a/common/V4L2ComponentCommon.cpp b/common/V4L2ComponentCommon.cpp
index 518b489..f67a516 100644
--- a/common/V4L2ComponentCommon.cpp
+++ b/common/V4L2ComponentCommon.cpp
@@ -18,15 +18,18 @@
 const std::string V4L2ComponentName::kH264Decoder = "c2.v4l2.avc.decoder";
 const std::string V4L2ComponentName::kVP8Decoder = "c2.v4l2.vp8.decoder";
 const std::string V4L2ComponentName::kVP9Decoder = "c2.v4l2.vp9.decoder";
+const std::string V4L2ComponentName::kHEVCDecoder = "c2.v4l2.hevc.decoder";
 const std::string V4L2ComponentName::kH264SecureDecoder = "c2.v4l2.avc.decoder.secure";
 const std::string V4L2ComponentName::kVP8SecureDecoder = "c2.v4l2.vp8.decoder.secure";
 const std::string V4L2ComponentName::kVP9SecureDecoder = "c2.v4l2.vp9.decoder.secure";
+const std::string V4L2ComponentName::kHEVCSecureDecoder = "c2.v4l2.hevc.decoder.secure";
 
 // static
 bool V4L2ComponentName::isValid(const char* name) {
     return name == kH264Encoder || name == kVP8Encoder || name == kVP9Encoder ||
            name == kH264Decoder || name == kVP8Decoder || name == kVP9Decoder ||
-           name == kH264SecureDecoder || name == kVP8SecureDecoder || name == kVP9SecureDecoder;
+           name == kHEVCDecoder || name == kH264SecureDecoder || name == kVP8SecureDecoder ||
+           name == kVP9SecureDecoder || name == kHEVCSecureDecoder;
 }
 
 // static
diff --git a/common/V4L2Device.cpp b/common/V4L2Device.cpp
index 68a9dca..4e44fed 100644
--- a/common/V4L2Device.cpp
+++ b/common/V4L2Device.cpp
@@ -50,6 +50,11 @@
 #define V4L2_PIX_FMT_H264_SLICE v4l2_fourcc('S', '2', '6', '4')
 #endif
 
+// HEVC parsed slices
+#ifndef V4L2_PIX_FMT_HEVC_SLICE
+#define V4L2_PIX_FMT_HEVC_SLICE v4l2_fourcc('S', '2', '6', '5')
+#endif
+
 namespace android {
 
 struct v4l2_format buildV4L2Format(const enum v4l2_buf_type type, uint32_t fourcc,
@@ -1237,6 +1242,13 @@
         } else {
             return V4L2_PIX_FMT_VP9;
         }
+    } else if (profile >= C2Config::PROFILE_HEVC_MAIN &&
+               profile <= C2Config::PROFILE_HEVC_3D_MAIN) {
+        if (sliceBased) {
+            return V4L2_PIX_FMT_HEVC_SLICE;
+        } else {
+            return V4L2_PIX_FMT_HEVC;
+        }
     } else {
         ALOGE("Unknown profile: %s", profileToString(profile));
         return 0;
@@ -1283,6 +1295,16 @@
             return C2Config::PROFILE_VP9_3;
         }
         break;
+    case VideoCodec::HEVC:
+        switch (profile) {
+        case V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN:
+            return C2Config::PROFILE_HEVC_MAIN;
+        case V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_STILL_PICTURE:
+            return C2Config::PROFILE_HEVC_MAIN_STILL;
+        case V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_10:
+            return C2Config::PROFILE_HEVC_MAIN_10;
+        }
+        break;
     default:
         ALOGE("Unknown codec: %u", codec);
     }
@@ -1305,6 +1327,9 @@
         case VideoCodec::VP9:
             queryId = V4L2_CID_MPEG_VIDEO_VP9_PROFILE;
             break;
+        case VideoCodec::HEVC:
+            queryId = V4L2_CID_MPEG_VIDEO_HEVC_PROFILE;
+            break;
         default:
             return false;
         }
@@ -1355,6 +1380,16 @@
             profiles = {C2Config::PROFILE_VP9_0};
         }
         break;
+    case V4L2_PIX_FMT_HEVC:
+    case V4L2_PIX_FMT_HEVC_SLICE:
+        if (!getSupportedProfiles(VideoCodec::HEVC, &profiles)) {
+            ALOGW("Driver doesn't support QUERY HEVC profiles, "
+                  "use default values, Main");
+            profiles = {
+                    C2Config::PROFILE_HEVC_MAIN,
+            };
+        }
+        break;
     default:
         ALOGE("Unhandled pixelformat %s", fourccToString(pixFmt).c_str());
         return {};
diff --git a/common/VideoTypes.cpp b/common/VideoTypes.cpp
index c123ad1..18ebfc9 100644
--- a/common/VideoTypes.cpp
+++ b/common/VideoTypes.cpp
@@ -19,6 +19,8 @@
         return "VP8";
     case VideoCodec::VP9:
         return "VP9";
+    case VideoCodec::HEVC:
+        return "HEVC";
     }
 }
 
diff --git a/common/include/v4l2_codec2/common/V4L2ComponentCommon.h b/common/include/v4l2_codec2/common/V4L2ComponentCommon.h
index b8cf031..a5fbdaf 100644
--- a/common/include/v4l2_codec2/common/V4L2ComponentCommon.h
+++ b/common/include/v4l2_codec2/common/V4L2ComponentCommon.h
@@ -18,9 +18,11 @@
     static const std::string kH264Decoder;
     static const std::string kVP8Decoder;
     static const std::string kVP9Decoder;
+    static const std::string kHEVCDecoder;
     static const std::string kH264SecureDecoder;
     static const std::string kVP8SecureDecoder;
     static const std::string kVP9SecureDecoder;
+    static const std::string kHEVCSecureDecoder;
 
     // Return true if |name| is a valid component name.
     static bool isValid(const char* name);
diff --git a/common/include/v4l2_codec2/common/VideoTypes.h b/common/include/v4l2_codec2/common/VideoTypes.h
index 076f096..54786e8 100644
--- a/common/include/v4l2_codec2/common/VideoTypes.h
+++ b/common/include/v4l2_codec2/common/VideoTypes.h
@@ -18,10 +18,11 @@
     H264,
     VP8,
     VP9,
+    HEVC,
 };
 
 constexpr std::initializer_list<VideoCodec> kAllCodecs = {VideoCodec::H264, VideoCodec::VP8,
-                                                          VideoCodec::VP9};
+                                                          VideoCodec::VP9, VideoCodec::HEVC};
 
 const char* VideoCodecToString(VideoCodec codec);
 const char* profileToString(C2Config::profile_t profile);
diff --git a/components/V4L2ComponentStore.cpp b/components/V4L2ComponentStore.cpp
index 4004ce5..feb5799 100644
--- a/components/V4L2ComponentStore.cpp
+++ b/components/V4L2ComponentStore.cpp
@@ -37,6 +37,9 @@
         name == V4L2ComponentName::kVP9Encoder) {
         return MEDIA_MIMETYPE_VIDEO_VP9;
     }
+    if (name == V4L2ComponentName::kHEVCDecoder || name == V4L2ComponentName::kHEVCSecureDecoder) {
+        return MEDIA_MIMETYPE_VIDEO_HEVC;
+    }
     return "";
 }
 
@@ -118,6 +121,8 @@
     ret.push_back(GetTraits(V4L2ComponentName::kVP9Encoder));
     ret.push_back(GetTraits(V4L2ComponentName::kVP9Decoder));
     ret.push_back(GetTraits(V4L2ComponentName::kVP9SecureDecoder));
+    ret.push_back(GetTraits(V4L2ComponentName::kHEVCDecoder));
+    ret.push_back(GetTraits(V4L2ComponentName::kHEVCSecureDecoder));
     return ret;
 }
 
diff --git a/components/V4L2DecodeInterface.cpp b/components/V4L2DecodeInterface.cpp
index 4bc4121..d22f77b 100644
--- a/components/V4L2DecodeInterface.cpp
+++ b/components/V4L2DecodeInterface.cpp
@@ -34,6 +34,8 @@
         return VideoCodec::VP8;
     if (name == V4L2ComponentName::kVP9Decoder || name == V4L2ComponentName::kVP9SecureDecoder)
         return VideoCodec::VP9;
+    if (name == V4L2ComponentName::kHEVCDecoder || name == V4L2ComponentName::kHEVCSecureDecoder)
+        return VideoCodec::HEVC;
 
     ALOGE("Unknown name: %s", name.c_str());
     return std::nullopt;
@@ -179,6 +181,35 @@
                         .withSetter(ProfileLevelSetter)
                         .build());
         break;
+
+    case VideoCodec::HEVC:
+        inputMime = MEDIA_MIMETYPE_VIDEO_HEVC;
+        addParameter(
+                DefineParam(mProfileLevel, C2_PARAMKEY_PROFILE_LEVEL)
+                        .withDefault(new C2StreamProfileLevelInfo::input(
+                                0u, C2Config::PROFILE_HEVC_MAIN, C2Config::LEVEL_HEVC_MAIN_5_1))
+                        .withFields({C2F(mProfileLevel, profile)
+                                             .oneOf({C2Config::PROFILE_HEVC_MAIN,
+                                                     C2Config::PROFILE_HEVC_MAIN_STILL}),
+                                     C2F(mProfileLevel, level)
+                                             .oneOf({C2Config::LEVEL_HEVC_MAIN_1,
+                                                     C2Config::LEVEL_HEVC_MAIN_2,
+                                                     C2Config::LEVEL_HEVC_MAIN_2_1,
+                                                     C2Config::LEVEL_HEVC_MAIN_3,
+                                                     C2Config::LEVEL_HEVC_MAIN_3_1,
+                                                     C2Config::LEVEL_HEVC_MAIN_4,
+                                                     C2Config::LEVEL_HEVC_MAIN_4_1,
+                                                     C2Config::LEVEL_HEVC_MAIN_5,
+                                                     C2Config::LEVEL_HEVC_MAIN_5_1,
+                                                     C2Config::LEVEL_HEVC_MAIN_5_2,
+                                                     C2Config::LEVEL_HEVC_HIGH_4,
+                                                     C2Config::LEVEL_HEVC_HIGH_4_1,
+                                                     C2Config::LEVEL_HEVC_HIGH_5,
+                                                     C2Config::LEVEL_HEVC_HIGH_5_1,
+                                                     C2Config::LEVEL_HEVC_HIGH_5_2})})
+                        .withSetter(ProfileLevelSetter)
+                        .build());
+        break;
     }
 
     addParameter(
@@ -346,6 +377,8 @@
         // codec2 framework that it should not stop queuing new work items until the maximum number
         // of frame reordering is reached, to avoid stalling the decoder.
         return 16;
+    case VideoCodec::HEVC:
+        return 16;
     case VideoCodec::VP8:
         return 0;
     case VideoCodec::VP9:
diff --git a/components/V4L2Decoder.cpp b/components/V4L2Decoder.cpp
index aa59e91..cc2c1d1 100644
--- a/components/V4L2Decoder.cpp
+++ b/components/V4L2Decoder.cpp
@@ -42,6 +42,8 @@
         return V4L2_PIX_FMT_VP8;
     case VideoCodec::VP9:
         return V4L2_PIX_FMT_VP9;
+    case VideoCodec::HEVC:
+        return V4L2_PIX_FMT_HEVC;
     }
 }
 
diff --git a/components/V4L2EncodeComponent.cpp b/components/V4L2EncodeComponent.cpp
index c2a2679..f9d735d 100644
--- a/components/V4L2EncodeComponent.cpp
+++ b/components/V4L2EncodeComponent.cpp
@@ -652,6 +652,12 @@
     // Get the requested bitrate mode and bitrate. The C2 framework doesn't offer a parameter to
     // configure the peak bitrate, so we use a multiple of the target bitrate.
     mBitrateMode = mInterface->getBitrateMode();
+    if (property_get_bool("persist.vendor.v4l2_codec2.disable_vbr", false)) {
+        // NOTE: This is a workaround for b/235771157.
+        ALOGW("VBR is disabled on this device");
+        mBitrateMode = C2Config::BITRATE_CONST;
+    }
+
     mBitrate = mInterface->getBitrate();
 
     mEncoder = V4L2Encoder::create(
diff --git a/tests/c2_comp_intf/Android.bp b/tests/c2_comp_intf/Android.bp
index 0201ad0..5c91ef4 100644
--- a/tests/c2_comp_intf/Android.bp
+++ b/tests/c2_comp_intf/Android.bp
@@ -41,5 +41,5 @@
         "-Wall",
     ],
     ldflags: ["-Wl,-Bsymbolic"],
-    clang: true,
+
 }
diff --git a/tests/c2_e2e_test/jni/common.cpp b/tests/c2_e2e_test/jni/common.cpp
index 50f4cdf..673e36c 100644
--- a/tests/c2_e2e_test/jni/common.cpp
+++ b/tests/c2_e2e_test/jni/common.cpp
@@ -257,6 +257,7 @@
     if (profile >= H264PROFILE_MIN && profile <= H264PROFILE_MAX) return VideoCodecType::H264;
     if (profile >= VP8PROFILE_MIN && profile <= VP8PROFILE_MAX) return VideoCodecType::VP8;
     if (profile >= VP9PROFILE_MIN && profile <= VP9PROFILE_MAX) return VideoCodecType::VP9;
+    if (profile >= HEVCPROFILE_MIN && profile <= HEVCPROFILE_MAX) return VideoCodecType::HEVC;
     return VideoCodecType::UNKNOWN;
 }
 
@@ -286,6 +287,8 @@
         return "video/x-vnd.on2.vp8";
     case VideoCodecType::VP9:
         return "video/x-vnd.on2.vp9";
+    case VideoCodecType::HEVC:
+        return "video/hevc";
     default:  // unknown type
         return nullptr;
     }
diff --git a/tests/c2_e2e_test/jni/common.h b/tests/c2_e2e_test/jni/common.h
index ea8d212..b28fd3a 100644
--- a/tests/c2_e2e_test/jni/common.h
+++ b/tests/c2_e2e_test/jni/common.h
@@ -41,6 +41,11 @@
     VP9PROFILE_PROFILE2 = 14,
     VP9PROFILE_PROFILE3 = 15,
     VP9PROFILE_MAX = VP9PROFILE_PROFILE3,
+    HEVCPROFILE_MIN = 16,
+    HEVCPROFILE_MAIN = HEVCPROFILE_MIN,
+    HEVCPROFILE_MAIN10 = 17,
+    HEVCPROFILE_MAIN_STILL_PICTURE = 18,
+    HEVCPROFILE_MAX = HEVCPROFILE_MAIN_STILL_PICTURE,
 };
 
 // The enum class of video codec type.
@@ -49,6 +54,7 @@
     H264,
     VP8,
     VP9,
+    HEVC,
 };
 
 // Structure to store resolution.
diff --git a/tests/c2_e2e_test/jni/encoded_data_helper.cpp b/tests/c2_e2e_test/jni/encoded_data_helper.cpp
index 0ced022..54e4382 100644
--- a/tests/c2_e2e_test/jni/encoded_data_helper.cpp
+++ b/tests/c2_e2e_test/jni/encoded_data_helper.cpp
@@ -43,12 +43,12 @@
     }
     if (pos + 3 >= data.size()) return false;  // No more NALUs
 
-    // NALU header is the first byte after Annex-B start code.
+    // NALU header is the first byte after Annex-B start code for H264/HEVC.
     *next_header_pos = pos + 3;
     return true;
 }
 
-// For H264, return data bytes of next AU fragment in |data| from |next_pos|,
+// For H264/HEVC, return data bytes of next AU fragment in |data| from |next_pos|,
 // and update the position to |next_pos|.
 std::string GetBytesForNextAU(const std::string& data, size_t* next_pos) {
     // Helpful description:
@@ -144,6 +144,7 @@
         std::unique_ptr<Fragment> fragment(new Fragment());
         switch (type_) {
         case VideoCodecType::H264:
+        case VideoCodecType::HEVC:
             fragment->data = GetBytesForNextAU(data, &next_pos);
             if (!ParseAUFragmentType(fragment.get())) continue;
             if (!seen_csd && !fragment->csd_flag)
@@ -178,23 +179,37 @@
             return false;
         }
 
-        // Check NALU type ([3:7], 5-bit).
-        uint8_t nalu_type = nalu_header & 0x1f;
-        switch (nalu_type) {
-        case NON_IDR_SLICE:
-        case IDR_SLICE:
-            // If AU contains both CSD and VCL NALUs (e.g. PPS + IDR_SLICE), don't
-            // raise csd_flag, treat this fragment as VCL one.
-            fragment->csd_flag = false;
-            return true;  // fragment in interest as VCL.
-        case SPS:
-        case PPS:
-            fragment->csd_flag = true;
-            // Continue on finding the subsequent NALUs, it may have VCL data.
-            break;
-        default:
-            // Skip uninterested NALU type.
-            break;
+        if (type_ == VideoCodecType::H264) {
+            // Check NALU type ([3:7], 5-bit).
+            uint8_t nalu_type = nalu_header & 0x1f;
+            switch (nalu_type) {
+            case NON_IDR_SLICE:
+            case IDR_SLICE:
+                // If AU contains both CSD and VCL NALUs (e.g. PPS + IDR_SLICE), don't
+                // raise csd_flag, treat this fragment as VCL one.
+                fragment->csd_flag = false;
+                return true;  // fragment in interest as VCL.
+            case SPS:
+            case PPS:
+                fragment->csd_flag = true;
+                // Continue on finding the subsequent NALUs, it may have VCL data.
+                break;
+            default:
+                // Skip uninterested NALU type.
+                break;
+            }
+        } else if (type_ == VideoCodecType::HEVC) {
+            // Check NALU type ([1:7], 6-bit).
+            uint8_t nalu_type = (nalu_header & 0x7e) >> 1;
+            if (nalu_type >= VCL_NALU_MIN && nalu_type <= VCL_NALU_MAX) {
+                // If AU contains both CSD and VCL NALUs (e.g. PPS + IDR_SLICE), don't
+                // raise csd_flag, treat this fragment as VCL one.
+                fragment->csd_flag = false;
+                return true;  // fragment in interest as VCL.
+            } else if (nalu_type >= CSD_NALU_MIN && nalu_type <= CSD_NALU_MAX) {
+                fragment->csd_flag = true;
+                // Continue on finding the subsequent NALUs, it may have VCL data.
+            }
         }
     }
     return fragment->csd_flag;  // fragment in interest as CSD.
diff --git a/tests/c2_e2e_test/jni/encoded_data_helper.h b/tests/c2_e2e_test/jni/encoded_data_helper.h
index cf107c2..9ec5086 100644
--- a/tests/c2_e2e_test/jni/encoded_data_helper.h
+++ b/tests/c2_e2e_test/jni/encoded_data_helper.h
@@ -21,7 +21,7 @@
     EncodedDataHelper(const std::string& file_path, VideoCodecType type);
     ~EncodedDataHelper();
 
-    // A fragment will contain the bytes of one AU (H264) or frame (VP8/9) in
+    // A fragment will contain the bytes of one AU (H264/HEVC) or frame (VP8/9) in
     // |data|, and |csd_flag| indicator for input buffer flag CODEC_CONFIG.
     struct Fragment {
         std::string data;
@@ -41,17 +41,25 @@
 private:
     // NALU type enumeration as defined in H264 Annex-B. Only interested ones are
     // listed here.
-    enum NALUType : uint8_t {
+    enum H264NALUType : uint8_t {
         NON_IDR_SLICE = 0x1,
         IDR_SLICE = 0x5,
         SPS = 0x7,
         PPS = 0x8,
     };
 
+    // NALU type enumeration for ranges for VCL and CSD NALUs for HEVC.
+    enum HEVCNALUType : uint8_t {
+        VCL_NALU_MIN = 0,
+        VCL_NALU_MAX = 31,
+        CSD_NALU_MIN = 32,  // VPS, SPS, PPS
+        CSD_NALU_MAX = 34,
+    };
+
     // Slice input stream into fragments. This should be done in constructor.
     void SliceToFragments(const std::string& data);
 
-    // For H264, parse csd_flag from |fragment| data and store inside. Return true
+    // For H264/HEVC, parse csd_flag from |fragment| data and store inside. Return true
     // if this fragment is in interest; false otherwise (fragment will be
     // discarded.)
     bool ParseAUFragmentType(Fragment* fragment);
diff --git a/tests/c2_e2e_test/jni/mediacodec_decoder.cpp b/tests/c2_e2e_test/jni/mediacodec_decoder.cpp
index b14a841..cbebfdd 100644
--- a/tests/c2_e2e_test/jni/mediacodec_decoder.cpp
+++ b/tests/c2_e2e_test/jni/mediacodec_decoder.cpp
@@ -32,7 +32,7 @@
 
 // Helper function to get possible C2 hardware decoder names from |type|.
 // Note: A single test APK is built for both ARC++ and ARCVM, so both the VDA decoder and the new
-// V4L2 decoder names need to be specified here.
+// V4L2 decoder names need to be specified here (except for HEVC, which is only on ARCVM).
 std::vector<const char*> GetC2VideoDecoderNames(VideoCodecType type) {
     switch (type) {
     case VideoCodecType::H264:
@@ -41,6 +41,8 @@
         return {"c2.v4l2.vp8.decoder", "c2.vda.vp8.decoder"};
     case VideoCodecType::VP9:
         return {"c2.v4l2.vp9.decoder", "c2.vda.vp9.decoder"};
+    case VideoCodecType::HEVC:
+        return {"c2.v4l2.hevc.decoder"};
     default:  // unknown type
         return {};
     }
diff --git a/tests/c2_e2e_test/jni/video_decoder_e2e_test.cpp b/tests/c2_e2e_test/jni/video_decoder_e2e_test.cpp
index 46b497f..62589c2 100644
--- a/tests/c2_e2e_test/jni/video_decoder_e2e_test.cpp
+++ b/tests/c2_e2e_test/jni/video_decoder_e2e_test.cpp
@@ -50,10 +50,10 @@
     // "input_file_path:width:height:num_frames:num_fragments:min_fps_render:
     //  min_fps_no_render:video_codec_profile[:output_file_path]"
     // - |input_file_path| is compressed video stream in H264 Annex B (NAL) format
-    //   (H264) or IVF (VP8/9).
+    //   (H264/HEVC) or IVF (VP8/9).
     // - |width| and |height| are visible frame size in pixels.
     // - |num_frames| is the number of picture frames for the input stream.
-    // - |num_fragments| is the number of AU (H264) or frame (VP8/9) in the input
+    // - |num_fragments| is the number of AU (H264/HEVC) or frame (VP8/9) in the input
     //   stream. (Unused. Test will automatically parse the number.)
     // - |min_fps_render| and |min_fps_no_render| are minimum frames/second speeds
     //   expected to be achieved with and without rendering respective.