[M120 Migration][MM] Framerate calculation
[platform/framework/web/chromium-efl.git] / media / formats / mp4 / hevc.h
1 // Copyright 2015 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef MEDIA_FORMATS_MP4_HEVC_H_
6 #define MEDIA_FORMATS_MP4_HEVC_H_
7
8 #include <stddef.h>
9 #include <stdint.h>
10
11 #include <memory>
12 #include <vector>
13
14 #include "media/base/media_export.h"
15 #include "media/base/video_codecs.h"
16 #include "media/base/video_decoder_config.h"
17 #include "media/formats/mp4/bitstream_converter.h"
18 #include "media/formats/mp4/box_definitions.h"
19
20 #if BUILDFLAG(IS_TIZEN_TV)
21 #include "third_party/abseil-cpp/absl/types/optional.h"
22 #endif
23
24 namespace media {
25
26 struct SubsampleEntry;
27
28 namespace mp4 {
29
30 struct MEDIA_EXPORT HEVCDecoderConfigurationRecord : Box {
31   DECLARE_BOX_METHODS(HEVCDecoderConfigurationRecord);
32
33   // Parallel processing tools used by decoder.
34   enum {
35     kMixedParallel = 0,  // mixed mode of slice-based/tile-based/wavefront
36     kSliceParallel,      // slices can be decoded independently
37     kTileParallel,       // tiles can be decoded independently
38     kWaveFrontParallel,  // first row of CTUs decoded normally and rest
39                          // parallelized
40   };
41   // Parses HEVCDecoderConfigurationRecord data encoded in |data|.
42   // Note: This method is intended to parse data outside the MP4StreamParser
43   //       context and therefore the box header is not expected to be present
44   //       in |data|.
45   // Returns true if |data| was successfully parsed.
46   bool Parse(const uint8_t* data, int data_size);
47   bool Serialize(std::vector<uint8_t>& output) const;
48
49   uint8_t configurationVersion;
50   uint8_t general_profile_space;
51   uint8_t general_tier_flag;
52   uint8_t general_profile_idc;
53   uint32_t general_profile_compatibility_flags;
54   uint64_t general_constraint_indicator_flags;
55   uint8_t general_level_idc;
56   uint16_t min_spatial_segmentation_idc;
57   uint8_t parallelismType;
58   uint8_t chromaFormat;
59   uint8_t bitDepthLumaMinus8;
60   uint8_t bitDepthChromaMinus8;
61   uint16_t avgFrameRate;
62   uint8_t constantFrameRate;
63   uint8_t numTemporalLayers;
64   uint8_t temporalIdNested;
65   uint8_t lengthSizeMinusOne;
66   uint8_t numOfArrays;
67
68   typedef std::vector<uint8_t> HVCCNALUnit;
69   struct HVCCNALArray {
70     HVCCNALArray();
71     HVCCNALArray(const HVCCNALArray& other);
72     ~HVCCNALArray();
73     uint8_t first_byte =
74         0;  // array_completeness(1)/reserved0(1)/NAL_unit_type(6)
75     std::vector<HVCCNALUnit> units;
76   };
77   std::vector<HVCCNALArray> arrays;
78
79   VideoCodecProfile GetVideoProfile() const;
80 #if BUILDFLAG(ENABLE_HEVC_PARSER_AND_HW_DECODER)
81   VideoColorSpace GetColorSpace();
82   gfx::HDRMetadata GetHDRMetadata();
83   VideoDecoderConfig::AlphaMode GetAlphaMode();
84 #endif  // BUILDFLAG(ENABLE_HEVC_PARSER_AND_HW_DECODER)
85
86  private:
87   bool ParseInternal(BufferReader* reader, MediaLog* media_log);
88   VideoColorSpace color_space;
89   gfx::HDRMetadata hdr_metadata;
90   VideoDecoderConfig::AlphaMode alpha_mode;
91 };
92
93 class MEDIA_EXPORT HEVC {
94  public:
95   static void ConvertConfigToAnnexB(
96       const HEVCDecoderConfigurationRecord& hevc_config,
97       std::vector<uint8_t>* buffer);
98
99   static bool InsertParamSetsAnnexB(
100       const HEVCDecoderConfigurationRecord& hevc_config,
101       std::vector<uint8_t>* buffer,
102       std::vector<SubsampleEntry>* subsamples);
103
104   // Analyzes the contents of |buffer| for conformance to
105   // Section 7.4.2.4.4 of ISO/IEC 23008-2, and if conformant, further inspects
106   // |buffer| to report whether or not it looks like a keyframe.
107   // |subsamples| contains the information about what parts of the buffer are
108   // encrypted and which parts are clear.
109   static BitstreamConverter::AnalysisResult AnalyzeAnnexB(
110       const uint8_t* buffer,
111       size_t size,
112       const std::vector<SubsampleEntry>& subsamples);
113 };
114
115 class HEVCBitstreamConverter : public BitstreamConverter {
116  public:
117   explicit HEVCBitstreamConverter(
118       std::unique_ptr<HEVCDecoderConfigurationRecord> hevc_config);
119
120   // BitstreamConverter interface
121   bool ConvertAndAnalyzeFrame(std::vector<uint8_t>* frame_buf,
122                               bool is_keyframe,
123                               std::vector<SubsampleEntry>* subsamples,
124                               AnalysisResult* analysis_result) const override;
125
126 #if BUILDFLAG(IS_TIZEN_TV)
127   void SetFramerate(const StreamFramerate::Framerate& framerate) override;
128 #endif
129
130  private:
131   ~HEVCBitstreamConverter() override;
132   AnalysisResult Analyze(
133       std::vector<uint8_t>* frame_buf,
134       std::vector<SubsampleEntry>* subsamples) const override;
135   std::unique_ptr<HEVCDecoderConfigurationRecord> hevc_config_;
136
137 #if BUILDFLAG(IS_TIZEN_TV)
138   absl::optional<StreamFramerate::Framerate> framerate_;
139 #endif
140 };
141
142 }  // namespace mp4
143 }  // namespace media
144
145 #endif  // MEDIA_FORMATS_MP4_HEVC_H_