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 NATIVE_AVCENCINFO_H
17#define NATIVE_AVCENCINFO_H
18
19#include <stdint.h>
20#include "native_averrors.h"
21
22#ifdef __cplusplus
23extern "C" {
24#endif
25
26/**
27 * @brief AVBuffer Structure.
28 * @since 12
29 * @version 1.0
30 */
31typedef struct OH_AVBuffer OH_AVBuffer;
32/**
33 * @brief AVCencInfo Structure.
34 * @since 12
35 * @version 1.0
36 */
37typedef struct OH_AVCencInfo OH_AVCencInfo;
38/**
39 * @brief Key id size.
40 * @since 12
41 * @version 1.0
42 */
43#define DRM_KEY_ID_SIZE 16
44/**
45 * @brief Iv size.
46 * @since 12
47 * @version 1.0
48 */
49#define DRM_KEY_IV_SIZE 16
50/**
51 * @brief Max subsample num.
52 * @since 12
53 * @version 1.0
54 */
55#define DRM_KEY_MAX_SUB_SAMPLE_NUM 64
56
57/**
58 * @brief Drm cenc algorithm type.
59 * @since 12
60 * @version 1.0
61 */
62typedef enum DrmCencAlgorithm {
63    /**
64     * Unencrypted.
65     */
66    DRM_ALG_CENC_UNENCRYPTED = 0x0,
67    /**
68     * Aes ctr.
69     */
70    DRM_ALG_CENC_AES_CTR = 0x1,
71    /**
72     * Aes wv.
73     */
74    DRM_ALG_CENC_AES_WV = 0x2,
75    /**
76     * Aes cbc.
77     */
78    DRM_ALG_CENC_AES_CBC = 0x3,
79    /**
80     * Sm4 cbc.
81     */
82    DRM_ALG_CENC_SM4_CBC = 0x4,
83    /**
84     * Sm4 ctr.
85     */
86    DRM_ALG_CENC_SM4_CTR = 0x5
87} DrmCencAlgorithm;
88
89/**
90 * @brief Mode of cend info like set or not.
91 * @since 12
92 * @version 1.0
93 */
94typedef enum DrmCencInfoMode {
95    /* key/iv/subsample set. */
96    DRM_CENC_INFO_KEY_IV_SUBSAMPLES_SET = 0x0,
97    /* key/iv/subsample not set. */
98    DRM_CENC_INFO_KEY_IV_SUBSAMPLES_NOT_SET = 0x1
99} DrmCencInfoMode;
100
101/**
102 * @brief Subsample info of media.
103 * @since 12
104 * @version 1.0
105 */
106typedef struct DrmSubsample {
107    /* Clear header len. */
108    uint32_t clearHeaderLen;
109    /* Payload Len. */
110    uint32_t payLoadLen;
111} DrmSubsample;
112
113/**
114 * @brief Creates an OH_AVCencInfo instance for setting cencinfo.
115 * Free the resources of the instance by calling OH_AVCencInfo_Destory.
116 * @syscap SystemCapability.Multimedia.Media.Spliter
117 * @return Returns the newly created OH_AVCencInfo object. If nullptr is returned, the object failed to be created.
118 *         The possible failure is due to the application address space being full,
119 *         or the data in the initialization object has failed.
120 * @since 12
121 * @version 1.0
122 */
123OH_AVCencInfo *OH_AVCencInfo_Create();
124
125/**
126 * @brief Destroy the OH_AVCencInfo instance and free the internal resources.
127 * The same instance can only be destroyed once. The destroyed instance
128 * should not be used before it is created again. It is recommended setting
129 * the instance pointer to NULL right after the instance is destroyed successfully.
130 * @syscap SystemCapability.Multimedia.Media.Spliter
131 * @param cencInfo Pointer to an OH_AVCencInfo instance.
132 * @return {@link AV_ERR_OK} 0 - Success
133 *         {@link AV_ERR_INVALID_VAL} 3 - cencInfo is nullptr.
134 * @since 12
135 * @version 1.0
136*/
137OH_AVErrCode OH_AVCencInfo_Destroy(OH_AVCencInfo *cencInfo);
138
139/**
140 * @brief Method to set algo of cencinfo.
141 * @syscap SystemCapability.Multimedia.Media.Spliter
142 * @param cencInfo Pointer to an OH_AVCencInfo instance.
143 * @param algo Cenc algo.
144 * @return {@link AV_ERR_OK} 0 - Success
145 *         {@link AV_ERR_INVALID_VAL} 3 - cencInfo is nullptr.
146 * @since 12
147 * @version 1.0
148 */
149OH_AVErrCode OH_AVCencInfo_SetAlgorithm(OH_AVCencInfo *cencInfo, enum DrmCencAlgorithm algo);
150
151/**
152 * @brief Method to set key id and iv of cencinfo.
153 * @syscap SystemCapability.Multimedia.Media.Spliter
154 * @param cencInfo Pointer to an OH_AVCencInfo instance.
155 * @param keyId Key id.
156 * @param keyIdLen Key id len.
157 * @param iv Iv.
158 * @param ivLen Iv len.
159 * @return {@link AV_ERR_OK} 0 - Success
160 *         {@link AV_ERR_INVALID_VAL} 3 - If cencInfo is nullptr, or keyId is nullptr, or keyIdLen != DRM_KEY_ID_SIZE,
161 *         or iv is nullptr, or ivLen != DRM_KEY_IV_SIZE, or keyId copy fails, or iv copy fails.
162 * @since 12
163 * @version 1.0
164 */
165OH_AVErrCode OH_AVCencInfo_SetKeyIdAndIv(OH_AVCencInfo *cencInfo, uint8_t *keyId,
166    uint32_t keyIdLen, uint8_t *iv, uint32_t ivLen);
167
168/**
169 * @brief Method to set subsample info of cencinfo.
170 * @syscap SystemCapability.Multimedia.Media.Spliter
171 * @param cencInfo Pointer to an OH_AVCencInfo instance.
172 * @param encryptedBlockCount Number of encrypted blocks.
173 * @param skippedBlockCount Number of skip(clear) blocks.
174 * @param firstEncryptedOffset Offset of first encrypted payload.
175 * @param subsampleCount Subsample num.
176 * @param subsamples Subsample info
177 * @return {@link AV_ERR_OK} 0 - Success
178 *         {@link AV_ERR_INVALID_VAL} 3 - If cencInfo is nullptr, or subsampleCount > DRM_KEY_MAX_SUB_SAMPLE_NUM,
179 *         or subsamples is nullptr.
180 * @since 12
181 * @version 1.0
182 */
183OH_AVErrCode OH_AVCencInfo_SetSubsampleInfo(OH_AVCencInfo *cencInfo, uint32_t encryptedBlockCount,
184    uint32_t skippedBlockCount, uint32_t firstEncryptedOffset, uint32_t subsampleCount, DrmSubsample *subsamples);
185
186/**
187 * @brief Method to set mode of cencinfo.
188 * @syscap SystemCapability.Multimedia.Media.Spliter
189 * @param cencInfo Pointer to an OH_AVCencInfo instance.
190 * @param mode Cenc mode, indicate whether key/iv/subsample set or not.
191 * @return {@link AV_ERR_OK} 0 - Success
192 *         {@link AV_ERR_INVALID_VAL} 3 - cencInfo is nullptr.
193 * @since 12
194 * @version 1.0
195 */
196OH_AVErrCode OH_AVCencInfo_SetMode(OH_AVCencInfo *cencInfo, enum DrmCencInfoMode mode);
197
198/**
199 * @brief Method to attach cencinfo to AVBuffer.
200 * @syscap SystemCapability.Multimedia.Media.Spliter
201 * @param cencInfo Pointer to an OH_AVCencInfo instance.
202 * @param buffer AVBuffer to attach cencinfo.
203 * @return {@link AV_ERR_OK} 0 - Success
204 *         {@link AV_ERR_INVALID_VAL} 3 - If cencInfo is nullptr, or buffer is nullptr, or buffer->buffer_ is nullptr,
205 *         or buffer->buffer_->meta_ is nullptr.
206 * @since 12
207 * @version 1.0
208 */
209OH_AVErrCode OH_AVCencInfo_SetAVBuffer(OH_AVCencInfo *cencInfo, OH_AVBuffer *buffer);
210
211#ifdef __cplusplus
212}
213#endif
214
215#endif // NATIVE_AVCENCINFO_H