1 // Copyright 2015 The Chromium Authors. All rights reserved.
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/formats/mp4/bitstream_converter.h"
17 #include "media/formats/mp4/box_definitions.h"
18 
19 namespace media {
20 
21 struct SubsampleEntry;
22 
23 namespace mp4 {
24 
25 struct MEDIA_EXPORT HEVCDecoderConfigurationRecord : Box {
26   DECLARE_BOX_METHODS(HEVCDecoderConfigurationRecord);
27 
28   // Parses HEVCDecoderConfigurationRecord data encoded in |data|.
29   // Note: This method is intended to parse data outside the MP4StreamParser
30   //       context and therefore the box header is not expected to be present
31   //       in |data|.
32   // Returns true if |data| was successfully parsed.
33   bool Parse(const uint8_t* data, int data_size);
34 
35   uint8_t configurationVersion;
36   uint8_t general_profile_space;
37   uint8_t general_tier_flag;
38   uint8_t general_profile_idc;
39   uint32_t general_profile_compatibility_flags;
40   uint64_t general_constraint_indicator_flags;
41   uint8_t general_level_idc;
42   uint16_t min_spatial_segmentation_idc;
43   uint8_t parallelismType;
44   uint8_t chromaFormat;
45   uint8_t bitDepthLumaMinus8;
46   uint8_t bitDepthChromaMinus8;
47   uint16_t avgFrameRate;
48   uint8_t constantFrameRate;
49   uint8_t numTemporalLayers;
50   uint8_t temporalIdNested;
51   uint8_t lengthSizeMinusOne;
52   uint8_t numOfArrays;
53 
54   typedef std::vector<uint8_t> HVCCNALUnit;
55   struct HVCCNALArray {
56     HVCCNALArray();
57     HVCCNALArray(const HVCCNALArray& other);
58     ~HVCCNALArray();
59     uint8_t first_byte;
60     std::vector<HVCCNALUnit> units;
61   };
62   std::vector<HVCCNALArray> arrays;
63 
64   VideoCodecProfile GetVideoProfile() const;
65 
66  private:
67   bool ParseInternal(BufferReader* reader, MediaLog* media_log);
68 };
69 
70 class MEDIA_EXPORT HEVC {
71  public:
72   static bool ConvertConfigToAnnexB(
73       const HEVCDecoderConfigurationRecord& hevc_config,
74       std::vector<uint8_t>* buffer);
75 
76   static bool InsertParamSetsAnnexB(
77       const HEVCDecoderConfigurationRecord& hevc_config,
78       std::vector<uint8_t>* buffer,
79       std::vector<SubsampleEntry>* subsamples);
80 
81   // Analyzes the contents of |buffer| for conformance to
82   // Section 7.4.2.4.4 of ISO/IEC 23008-2, and if conformant, further inspects
83   // |buffer| to report whether or not it looks like a keyframe.
84   // |subsamples| contains the information about what parts of the buffer are
85   // encrypted and which parts are clear.
86   static BitstreamConverter::AnalysisResult AnalyzeAnnexB(
87       const uint8_t* buffer,
88       size_t size,
89       const std::vector<SubsampleEntry>& subsamples);
90 };
91 
92 class HEVCBitstreamConverter : public BitstreamConverter {
93  public:
94   explicit HEVCBitstreamConverter(
95       std::unique_ptr<HEVCDecoderConfigurationRecord> hevc_config);
96 
97   // BitstreamConverter interface
98   bool ConvertAndAnalyzeFrame(std::vector<uint8_t>* frame_buf,
99                               bool is_keyframe,
100                               std::vector<SubsampleEntry>* subsamples,
101                               AnalysisResult* analysis_result) const override;
102 
103  private:
104   ~HEVCBitstreamConverter() override;
105   AnalysisResult Analyze(
106       std::vector<uint8_t>* frame_buf,
107       std::vector<SubsampleEntry>* subsamples) const override;
108   std::unique_ptr<HEVCDecoderConfigurationRecord> hevc_config_;
109 };
110 
111 }  // namespace mp4
112 }  // namespace media
113 
114 #endif  // MEDIA_FORMATS_MP4_HEVC_H_
115