blob: 09475efec2cd59970f35a6d0a7eb0dc3ce19e021 [file] [log] [blame]
Pawin Vongmasa36653902018-11-15 00:10:25 -08001/*
2 * Copyright 2017, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef CODEC2_BUFFER_H_
18
19#define CODEC2_BUFFER_H_
20
21#include <C2Buffer.h>
22
23#include <android/hardware/cas/native/1.0/types.h>
Robert Shih895fba92019-07-16 16:29:44 -070024#include <android/hardware/drm/1.0/types.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080025#include <binder/IMemory.h>
26#include <media/hardware/VideoAPI.h>
Wonsik Kimc48ddcf2019-02-11 16:16:57 -080027#include <media/stagefright/foundation/ABuffer.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080028#include <media/MediaCodecBuffer.h>
Marco Nelissen13aa1a42019-09-27 10:21:55 -070029#include <mediadrm/ICrypto.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080030
31namespace android {
32
33/**
34 * Copies a graphic view into a media image.
35 *
36 * \param imgBase base of MediaImage
37 * \param img MediaImage data
38 * \param view graphic view
39 *
40 * \return OK on success
41 */
42status_t ImageCopy(uint8_t *imgBase, const MediaImage2 *img, const C2GraphicView &view);
43
44/**
45 * Copies a media image into a graphic view.
46 *
47 * \param view graphic view
48 * \param imgBase base of MediaImage
49 * \param img MediaImage data
50 *
51 * \return OK on success
52 */
53status_t ImageCopy(C2GraphicView &view, const uint8_t *imgBase, const MediaImage2 *img);
54
55class Codec2Buffer : public MediaCodecBuffer {
56public:
57 using MediaCodecBuffer::MediaCodecBuffer;
58 ~Codec2Buffer() override = default;
59
Wonsik Kimc48ddcf2019-02-11 16:16:57 -080060 sp<ABuffer> getImageData() const { return mImageData; }
61
Pawin Vongmasa36653902018-11-15 00:10:25 -080062protected:
63 /**
64 * canCopy() implementation for linear buffers.
65 */
66 bool canCopyLinear(const std::shared_ptr<C2Buffer> &buffer) const;
67
68 /**
69 * copy() implementation for linear buffers.
70 */
71 bool copyLinear(const std::shared_ptr<C2Buffer> &buffer);
72
73 /**
74 * sets MediaImage data for flexible graphic buffers
75 */
76 void setImageData(const sp<ABuffer> &imageData);
Wonsik Kimc48ddcf2019-02-11 16:16:57 -080077
78 sp<ABuffer> mImageData;
Pawin Vongmasa36653902018-11-15 00:10:25 -080079};
80
81/**
82 * MediaCodecBuffer implementation on top of local linear buffer. This cannot
83 * cross process boundary so asC2Buffer() returns only nullptr.
84 */
85class LocalLinearBuffer : public Codec2Buffer {
86public:
87 using Codec2Buffer::Codec2Buffer;
88
89 std::shared_ptr<C2Buffer> asC2Buffer() override { return nullptr; }
90 bool canCopy(const std::shared_ptr<C2Buffer> &buffer) const override;
91 bool copy(const std::shared_ptr<C2Buffer> &buffer) override;
92};
93
94/**
95 * MediaCodecBuffer implementation to be used only as a dummy wrapper around a
96 * C2Buffer object.
97 */
98class DummyContainerBuffer : public Codec2Buffer {
99public:
100 DummyContainerBuffer(
101 const sp<AMessage> &format,
102 const std::shared_ptr<C2Buffer> &buffer = nullptr);
103
104 std::shared_ptr<C2Buffer> asC2Buffer() override;
105 bool canCopy(const std::shared_ptr<C2Buffer> &buffer) const override;
106 bool copy(const std::shared_ptr<C2Buffer> &buffer) override;
107
108private:
109 std::shared_ptr<C2Buffer> mBufferRef;
110};
111
112/**
113 * MediaCodecBuffer implementation wraps around C2LinearBlock.
114 */
115class LinearBlockBuffer : public Codec2Buffer {
116public:
117 /**
118 * Allocate a new LinearBufferBlock wrapping around C2LinearBlock object.
119 *
120 * \param format mandatory buffer format for MediaCodecBuffer
121 * \param block C2LinearBlock object to wrap around.
122 * \return LinearBlockBuffer object with writable mapping.
123 * nullptr if unsuccessful.
124 */
125 static sp<LinearBlockBuffer> Allocate(
126 const sp<AMessage> &format, const std::shared_ptr<C2LinearBlock> &block);
127
128 virtual ~LinearBlockBuffer() = default;
129
130 std::shared_ptr<C2Buffer> asC2Buffer() override;
131 bool canCopy(const std::shared_ptr<C2Buffer> &buffer) const override;
132 bool copy(const std::shared_ptr<C2Buffer> &buffer) override;
133
134private:
135 LinearBlockBuffer(
136 const sp<AMessage> &format,
137 C2WriteView &&writeView,
138 const std::shared_ptr<C2LinearBlock> &block);
139 LinearBlockBuffer() = delete;
140
141 C2WriteView mWriteView;
142 std::shared_ptr<C2LinearBlock> mBlock;
143};
144
145/**
146 * MediaCodecBuffer implementation wraps around C2ConstLinearBlock.
147 */
148class ConstLinearBlockBuffer : public Codec2Buffer {
149public:
150 /**
151 * Allocate a new ConstLinearBlockBuffer wrapping around C2Buffer object.
152 *
153 * \param format mandatory buffer format for MediaCodecBuffer
154 * \param buffer linear C2Buffer object to wrap around.
155 * \return ConstLinearBlockBuffer object with readable mapping.
156 * nullptr if unsuccessful.
157 */
158 static sp<ConstLinearBlockBuffer> Allocate(
159 const sp<AMessage> &format, const std::shared_ptr<C2Buffer> &buffer);
160
161 virtual ~ConstLinearBlockBuffer() = default;
162
163 std::shared_ptr<C2Buffer> asC2Buffer() override;
164
165private:
166 ConstLinearBlockBuffer(
167 const sp<AMessage> &format,
168 C2ReadView &&readView,
169 const std::shared_ptr<C2Buffer> &buffer);
170 ConstLinearBlockBuffer() = delete;
171
172 C2ReadView mReadView;
173 std::shared_ptr<C2Buffer> mBufferRef;
174};
175
176/**
177 * MediaCodecBuffer implementation wraps around C2GraphicBlock.
178 *
179 * This object exposes the underlying bits via accessor APIs and "image-data"
180 * metadata, created automatically at allocation time.
181 */
182class GraphicBlockBuffer : public Codec2Buffer {
183public:
184 /**
185 * Allocate a new GraphicBlockBuffer wrapping around C2GraphicBlock object.
186 * If |block| is not in good color formats, it allocates YV12 local buffer
187 * and copies the content over at asC2Buffer().
188 *
189 * \param format mandatory buffer format for MediaCodecBuffer
190 * \param block C2GraphicBlock object to wrap around.
191 * \param alloc a function to allocate backing ABuffer if needed.
192 * \return GraphicBlockBuffer object with writable mapping.
193 * nullptr if unsuccessful.
194 */
195 static sp<GraphicBlockBuffer> Allocate(
196 const sp<AMessage> &format,
197 const std::shared_ptr<C2GraphicBlock> &block,
198 std::function<sp<ABuffer>(size_t)> alloc);
199
Pawin Vongmasa36653902018-11-15 00:10:25 -0800200 virtual ~GraphicBlockBuffer() = default;
201
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700202 std::shared_ptr<C2Buffer> asC2Buffer() override;
203
Pawin Vongmasa36653902018-11-15 00:10:25 -0800204private:
205 GraphicBlockBuffer(
206 const sp<AMessage> &format,
207 const sp<ABuffer> &buffer,
208 C2GraphicView &&view,
209 const std::shared_ptr<C2GraphicBlock> &block,
210 const sp<ABuffer> &imageData,
211 bool wrapped);
212 GraphicBlockBuffer() = delete;
213
214 inline MediaImage2 *imageData() { return (MediaImage2 *)mImageData->data(); }
215
216 C2GraphicView mView;
217 std::shared_ptr<C2GraphicBlock> mBlock;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800218 const bool mWrapped;
219};
220
221/**
222 * MediaCodecBuffer implementation wraps around VideoNativeMetadata.
223 */
224class GraphicMetadataBuffer : public Codec2Buffer {
225public:
226 /**
227 * Construct a new GraphicMetadataBuffer with local linear buffer for
228 * VideoNativeMetadata.
229 *
230 * \param format mandatory buffer format for MediaCodecBuffer
231 */
232 GraphicMetadataBuffer(
233 const sp<AMessage> &format, const std::shared_ptr<C2Allocator> &alloc);
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700234 virtual ~GraphicMetadataBuffer() = default;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800235
236 std::shared_ptr<C2Buffer> asC2Buffer() override;
237
Pawin Vongmasa36653902018-11-15 00:10:25 -0800238private:
239 GraphicMetadataBuffer() = delete;
240
241 std::shared_ptr<C2Allocator> mAlloc;
242};
243
244/**
245 * MediaCodecBuffer implementation wraps around graphic C2Buffer object.
246 *
247 * This object exposes the underlying bits via accessor APIs and "image-data"
248 * metadata, created automatically at allocation time.
249 */
250class ConstGraphicBlockBuffer : public Codec2Buffer {
251public:
252 /**
253 * Allocate a new ConstGraphicBlockBuffer wrapping around C2Buffer object.
254 * If |buffer| is not in good color formats, it allocates YV12 local buffer
255 * and copies the content of |buffer| over to expose.
256 *
257 * \param format mandatory buffer format for MediaCodecBuffer
258 * \param buffer graphic C2Buffer object to wrap around.
259 * \param alloc a function to allocate backing ABuffer if needed.
260 * \return ConstGraphicBlockBuffer object with readable mapping.
261 * nullptr if unsuccessful.
262 */
263 static sp<ConstGraphicBlockBuffer> Allocate(
264 const sp<AMessage> &format,
265 const std::shared_ptr<C2Buffer> &buffer,
266 std::function<sp<ABuffer>(size_t)> alloc);
267
268 /**
269 * Allocate a new ConstGraphicBlockBuffer which allocates YV12 local buffer
270 * and copies the content of |buffer| over to expose.
271 *
272 * \param format mandatory buffer format for MediaCodecBuffer
273 * \param alloc a function to allocate backing ABuffer if needed.
274 * \return ConstGraphicBlockBuffer object with no wrapping buffer.
275 */
276 static sp<ConstGraphicBlockBuffer> AllocateEmpty(
277 const sp<AMessage> &format,
278 std::function<sp<ABuffer>(size_t)> alloc);
279
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700280 virtual ~ConstGraphicBlockBuffer() = default;
281
Pawin Vongmasa36653902018-11-15 00:10:25 -0800282 std::shared_ptr<C2Buffer> asC2Buffer() override;
283 bool canCopy(const std::shared_ptr<C2Buffer> &buffer) const override;
284 bool copy(const std::shared_ptr<C2Buffer> &buffer) override;
285
Pawin Vongmasa36653902018-11-15 00:10:25 -0800286private:
287 ConstGraphicBlockBuffer(
288 const sp<AMessage> &format,
289 const sp<ABuffer> &aBuffer,
290 std::unique_ptr<const C2GraphicView> &&view,
291 const std::shared_ptr<C2Buffer> &buffer,
292 const sp<ABuffer> &imageData,
293 bool wrapped);
294 ConstGraphicBlockBuffer() = delete;
295
296 sp<ABuffer> mImageData;
297 std::unique_ptr<const C2GraphicView> mView;
298 std::shared_ptr<C2Buffer> mBufferRef;
299 const bool mWrapped;
300};
301
302/**
303 * MediaCodecBuffer implementation wraps around C2LinearBlock for component
304 * and IMemory for client. Underlying C2LinearBlock won't be mapped for secure
305 * usecases..
306 */
307class EncryptedLinearBlockBuffer : public Codec2Buffer {
308public:
309 /**
310 * Construct a new EncryptedLinearBufferBlock wrapping around C2LinearBlock
311 * object and writable IMemory region.
312 *
313 * \param format mandatory buffer format for MediaCodecBuffer
314 * \param block C2LinearBlock object to wrap around.
315 * \param memory IMemory object to store encrypted content.
316 * \param heapSeqNum Heap sequence number from ICrypto; -1 if N/A
317 */
318 EncryptedLinearBlockBuffer(
319 const sp<AMessage> &format,
320 const std::shared_ptr<C2LinearBlock> &block,
321 const sp<IMemory> &memory,
322 int32_t heapSeqNum = -1);
323 EncryptedLinearBlockBuffer() = delete;
324
325 virtual ~EncryptedLinearBlockBuffer() = default;
326
327 std::shared_ptr<C2Buffer> asC2Buffer() override;
328
329 /**
330 * Fill the source buffer structure with appropriate value based on
331 * internal IMemory object.
332 *
333 * \param source source buffer structure to fill.
334 */
Robert Shih895fba92019-07-16 16:29:44 -0700335 void fillSourceBuffer(
336 hardware::drm::V1_0::SharedBuffer *source);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800337 void fillSourceBuffer(
338 hardware::cas::native::V1_0::SharedBuffer *source);
339
340 /**
341 * Copy the content of |decrypted| into C2LinearBlock inside. This shall
342 * only be called in non-secure usecases.
343 *
344 * \param decrypted decrypted content to copy from.
345 * \param length length of the content
346 * \return true if successful
347 * false otherwise.
348 */
349 bool copyDecryptedContent(const sp<IMemory> &decrypted, size_t length);
350
351 /**
352 * Copy the content of internal IMemory object into C2LinearBlock inside.
353 * This shall only be called in non-secure usecases.
354 *
355 * \param length length of the content
356 * \return true if successful
357 * false otherwise.
358 */
359 bool copyDecryptedContentFromMemory(size_t length);
360
361 /**
362 * Return native handle of secure buffer understood by ICrypto.
363 *
364 * \return secure buffer handle
365 */
366 native_handle_t *handle() const;
367
368private:
369
370 std::shared_ptr<C2LinearBlock> mBlock;
371 sp<IMemory> mMemory;
372 sp<hardware::HidlMemory> mHidlMemory;
373 int32_t mHeapSeqNum;
374};
375
376} // namespace android
377
378#endif // CODEC2_BUFFER_H_