1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef FFMPEG_FORMAT_HELPER_H
17 #define FFMPEG_FORMAT_HELPER_H
18 
19 #include <cstdint>
20 #include "meta/meta.h"
21 #include "ffmpeg_utils.h"
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 #include "libavformat/avformat.h"
27 #include "libavutil/channel_layout.h"
28 #include "libavcodec/av3a.h"
29 #include "libavutil/parseutils.h"
30 #ifdef __cplusplus
31 }
32 #endif
33 
34 namespace OHOS {
35 namespace Media {
36 namespace Plugins {
37 namespace Ffmpeg {
38 struct ParserSdtpInfo {
39     void *pb = nullptr;
40     int pbIsCopied = 0;
41     int ffindex = 0;
42     int nextChunk = 0;
43     unsigned int chunkCount = 0;
44     int64_t *chunkOffsets = nullptr;
45     unsigned int sttsCount = 0;
46     void *sttsData = nullptr;
47     unsigned int sdtpCount = 0;
48     uint8_t *sdtpData = nullptr;
49 };
50 
51 struct HevcParseFormat {
52     int32_t isHdrVivid = 0;
53     int32_t colorRange = 0;
54     uint8_t colorPrimaries = 0x02;
55     uint8_t colorTransfer = 0x02;
56     uint8_t colorMatrixCoeff = 0x02;
57     uint8_t profile = 0;
58     uint8_t level = 0;
59     uint32_t chromaLocation = 0;
60     uint32_t picWidInLumaSamples = 0;
61     uint32_t picHetInLumaSamples = 0;
62 };
63 
64 class FFmpegFormatHelper {
65 public:
66     static void ParseMediaInfo(const AVFormatContext& avFormatContext, Meta& format);
67     static void ParseTrackInfo(const AVStream& avStream, Meta& format, const AVFormatContext& avFormatContext);
68     static void ParseUserMeta(const AVFormatContext& avFormatContext, std::shared_ptr<Meta> format);
69     static void ParseHevcInfo(const AVFormatContext& avFormatContext, HevcParseFormat parse, Meta &format);
70     static FileType GetFileTypeByName(const AVFormatContext& avFormatContext);
71 private:
72     FFmpegFormatHelper() = delete;
73     ~FFmpegFormatHelper() = delete;
74 
75     static void ParseBaseTrackInfo(const AVStream& avStream, Meta &format, const AVFormatContext& avFormatContext);
76     static void ParseAVTrackInfo(const AVStream& avStream, Meta &format);
77     static void ParseVideoTrackInfo(const AVStream& avStream, Meta &format, const AVFormatContext& avFormatContext);
78     static void ParseAudioTrackInfo(const AVStream& avStream, Meta &format);
79     static void ParseImageTrackInfo(const AVStream& avStream, Meta &format);
80     static void ParseTimedMetaTrackInfo(const AVStream& avStream, Meta &format);
81     static void ParseHvccBoxInfo(const AVStream& avStream, Meta &format);
82     static void ParseColorBoxInfo(const AVStream& avStream, Meta &format);
83 
84     static void ParseLocationInfo(const AVFormatContext& avFormatContext, Meta &format);
85 
86     static void ParseInfoFromMetadata(const AVDictionary* metadata, const TagType key, Meta &format);
87     static void PutInfoToFormat(const Tag key, int32_t value, Meta &format);
88     static void PutInfoToFormat(const Tag key, int64_t value, Meta &format);
89     static void PutInfoToFormat(const Tag key, float value, Meta &format);
90     static void PutInfoToFormat(const Tag key, double value, Meta &format);
91     static void PutInfoToFormat(const Tag key, const std::string_view &value, Meta &format);
92     static void PutBufferToFormat(const Tag key, const uint8_t *addr, size_t size, Meta &format);
93     static void ParseRotationFromMatrix(const AVStream& avStream, Meta &format);
94     static void ParseOrientationFromMatrix(const AVStream& avStream, Meta &format);
95     static void ParseTrackType(const AVFormatContext& avFormatContext, Meta& format);
96 
97     static void ParseAv3aInfo(const AVStream& avStream, Meta &format);
98     static void ConvertAv3aSampleFormat(const AVStream& avStream, Meta &format);
99 };
100 extern std::vector<TagType> g_supportSourceFormat;
101 } // namespace Ffmpeg
102 } // namespace Plugins
103 } // namespace Media
104 } // namespace OHOS
105 #endif // FFMPEG_FORMAT_HELPER_H