blob: 9291c52f8e23cb876574a2c895dce773f9e250ee [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
60 /**
61 * \return C2Buffer object represents this buffer.
62 */
63 virtual std::shared_ptr<C2Buffer> asC2Buffer() = 0;
64
65 /**
66 * Test if we can copy the content of |buffer| into this object.
67 *
68 * \param buffer C2Buffer object to copy.
69 * \return true if the content of buffer can be copied over to this buffer
70 * false otherwise.
71 */
72 virtual bool canCopy(const std::shared_ptr<C2Buffer> &buffer) const {
73 (void)buffer;
74 return false;
75 }
76
77 /**
78 * Copy the content of |buffer| into this object. This method assumes that
79 * canCopy() check already passed.
80 *
81 * \param buffer C2Buffer object to copy.
82 * \return true if successful
83 * false otherwise.
84 */
85 virtual bool copy(const std::shared_ptr<C2Buffer> &buffer) {
86 (void)buffer;
87 return false;
88 }
89
Wonsik Kimc48ddcf2019-02-11 16:16:57 -080090 sp<ABuffer> getImageData() const { return mImageData; }
91
Pawin Vongmasa36653902018-11-15 00:10:25 -080092protected:
93 /**
94 * canCopy() implementation for linear buffers.
95 */
96 bool canCopyLinear(const std::shared_ptr<C2Buffer> &buffer) const;
97
98 /**
99 * copy() implementation for linear buffers.
100 */
101 bool copyLinear(const std::shared_ptr<C2Buffer> &buffer);
102
103 /**
104 * sets MediaImage data for flexible graphic buffers
105 */
106 void setImageData(const sp<ABuffer> &imageData);
Wonsik Kimc48ddcf2019-02-11 16:16:57 -0800107
108 sp<ABuffer> mImageData;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800109};
110
111/**
112 * MediaCodecBuffer implementation on top of local linear buffer. This cannot
113 * cross process boundary so asC2Buffer() returns only nullptr.
114 */
115class LocalLinearBuffer : public Codec2Buffer {
116public:
117 using Codec2Buffer::Codec2Buffer;
118
119 std::shared_ptr<C2Buffer> asC2Buffer() override { return nullptr; }
120 bool canCopy(const std::shared_ptr<C2Buffer> &buffer) const override;
121 bool copy(const std::shared_ptr<C2Buffer> &buffer) override;
122};
123
124/**
125 * MediaCodecBuffer implementation to be used only as a dummy wrapper around a
126 * C2Buffer object.
127 */
128class DummyContainerBuffer : public Codec2Buffer {
129public:
130 DummyContainerBuffer(
131 const sp<AMessage> &format,
132 const std::shared_ptr<C2Buffer> &buffer = nullptr);
133
134 std::shared_ptr<C2Buffer> asC2Buffer() override;
135 bool canCopy(const std::shared_ptr<C2Buffer> &buffer) const override;
136 bool copy(const std::shared_ptr<C2Buffer> &buffer) override;
137
138private:
139 std::shared_ptr<C2Buffer> mBufferRef;
140};
141
142/**
143 * MediaCodecBuffer implementation wraps around C2LinearBlock.
144 */
145class LinearBlockBuffer : public Codec2Buffer {
146public:
147 /**
148 * Allocate a new LinearBufferBlock wrapping around C2LinearBlock object.
149 *
150 * \param format mandatory buffer format for MediaCodecBuffer
151 * \param block C2LinearBlock object to wrap around.
152 * \return LinearBlockBuffer object with writable mapping.
153 * nullptr if unsuccessful.
154 */
155 static sp<LinearBlockBuffer> Allocate(
156 const sp<AMessage> &format, const std::shared_ptr<C2LinearBlock> &block);
157
158 virtual ~LinearBlockBuffer() = default;
159
160 std::shared_ptr<C2Buffer> asC2Buffer() override;
161 bool canCopy(const std::shared_ptr<C2Buffer> &buffer) const override;
162 bool copy(const std::shared_ptr<C2Buffer> &buffer) override;
163
164private:
165 LinearBlockBuffer(
166 const sp<AMessage> &format,
167 C2WriteView &&writeView,
168 const std::shared_ptr<C2LinearBlock> &block);
169 LinearBlockBuffer() = delete;
170
171 C2WriteView mWriteView;
172 std::shared_ptr<C2LinearBlock> mBlock;
173};
174
175/**
176 * MediaCodecBuffer implementation wraps around C2ConstLinearBlock.
177 */
178class ConstLinearBlockBuffer : public Codec2Buffer {
179public:
180 /**
181 * Allocate a new ConstLinearBlockBuffer wrapping around C2Buffer object.
182 *
183 * \param format mandatory buffer format for MediaCodecBuffer
184 * \param buffer linear C2Buffer object to wrap around.
185 * \return ConstLinearBlockBuffer object with readable mapping.
186 * nullptr if unsuccessful.
187 */
188 static sp<ConstLinearBlockBuffer> Allocate(
189 const sp<AMessage> &format, const std::shared_ptr<C2Buffer> &buffer);
190
191 virtual ~ConstLinearBlockBuffer() = default;
192
193 std::shared_ptr<C2Buffer> asC2Buffer() override;
194
195private:
196 ConstLinearBlockBuffer(
197 const sp<AMessage> &format,
198 C2ReadView &&readView,
199 const std::shared_ptr<C2Buffer> &buffer);
200 ConstLinearBlockBuffer() = delete;
201
202 C2ReadView mReadView;
203 std::shared_ptr<C2Buffer> mBufferRef;
204};
205
206/**
207 * MediaCodecBuffer implementation wraps around C2GraphicBlock.
208 *
209 * This object exposes the underlying bits via accessor APIs and "image-data"
210 * metadata, created automatically at allocation time.
211 */
212class GraphicBlockBuffer : public Codec2Buffer {
213public:
214 /**
215 * Allocate a new GraphicBlockBuffer wrapping around C2GraphicBlock object.
216 * If |block| is not in good color formats, it allocates YV12 local buffer
217 * and copies the content over at asC2Buffer().
218 *
219 * \param format mandatory buffer format for MediaCodecBuffer
220 * \param block C2GraphicBlock object to wrap around.
221 * \param alloc a function to allocate backing ABuffer if needed.
222 * \return GraphicBlockBuffer object with writable mapping.
223 * nullptr if unsuccessful.
224 */
225 static sp<GraphicBlockBuffer> Allocate(
226 const sp<AMessage> &format,
227 const std::shared_ptr<C2GraphicBlock> &block,
228 std::function<sp<ABuffer>(size_t)> alloc);
229
Pawin Vongmasa36653902018-11-15 00:10:25 -0800230 virtual ~GraphicBlockBuffer() = default;
231
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700232 std::shared_ptr<C2Buffer> asC2Buffer() override;
233
Pawin Vongmasa36653902018-11-15 00:10:25 -0800234private:
235 GraphicBlockBuffer(
236 const sp<AMessage> &format,
237 const sp<ABuffer> &buffer,
238 C2GraphicView &&view,
239 const std::shared_ptr<C2GraphicBlock> &block,
240 const sp<ABuffer> &imageData,
241 bool wrapped);
242 GraphicBlockBuffer() = delete;
243
244 inline MediaImage2 *imageData() { return (MediaImage2 *)mImageData->data(); }
245
246 C2GraphicView mView;
247 std::shared_ptr<C2GraphicBlock> mBlock;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800248 const bool mWrapped;
249};
250
251/**
252 * MediaCodecBuffer implementation wraps around VideoNativeMetadata.
253 */
254class GraphicMetadataBuffer : public Codec2Buffer {
255public:
256 /**
257 * Construct a new GraphicMetadataBuffer with local linear buffer for
258 * VideoNativeMetadata.
259 *
260 * \param format mandatory buffer format for MediaCodecBuffer
261 */
262 GraphicMetadataBuffer(
263 const sp<AMessage> &format, const std::shared_ptr<C2Allocator> &alloc);
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700264 virtual ~GraphicMetadataBuffer() = default;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800265
266 std::shared_ptr<C2Buffer> asC2Buffer() override;
267
Pawin Vongmasa36653902018-11-15 00:10:25 -0800268private:
269 GraphicMetadataBuffer() = delete;
270
271 std::shared_ptr<C2Allocator> mAlloc;
272};
273
274/**
275 * MediaCodecBuffer implementation wraps around graphic C2Buffer object.
276 *
277 * This object exposes the underlying bits via accessor APIs and "image-data"
278 * metadata, created automatically at allocation time.
279 */
280class ConstGraphicBlockBuffer : public Codec2Buffer {
281public:
282 /**
283 * Allocate a new ConstGraphicBlockBuffer wrapping around C2Buffer object.
284 * If |buffer| is not in good color formats, it allocates YV12 local buffer
285 * and copies the content of |buffer| over to expose.
286 *
287 * \param format mandatory buffer format for MediaCodecBuffer
288 * \param buffer graphic C2Buffer object to wrap around.
289 * \param alloc a function to allocate backing ABuffer if needed.
290 * \return ConstGraphicBlockBuffer object with readable mapping.
291 * nullptr if unsuccessful.
292 */
293 static sp<ConstGraphicBlockBuffer> Allocate(
294 const sp<AMessage> &format,
295 const std::shared_ptr<C2Buffer> &buffer,
296 std::function<sp<ABuffer>(size_t)> alloc);
297
298 /**
299 * Allocate a new ConstGraphicBlockBuffer which allocates YV12 local buffer
300 * and copies the content of |buffer| over to expose.
301 *
302 * \param format mandatory buffer format for MediaCodecBuffer
303 * \param alloc a function to allocate backing ABuffer if needed.
304 * \return ConstGraphicBlockBuffer object with no wrapping buffer.
305 */
306 static sp<ConstGraphicBlockBuffer> AllocateEmpty(
307 const sp<AMessage> &format,
308 std::function<sp<ABuffer>(size_t)> alloc);
309
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700310 virtual ~ConstGraphicBlockBuffer() = default;
311
Pawin Vongmasa36653902018-11-15 00:10:25 -0800312 std::shared_ptr<C2Buffer> asC2Buffer() override;
313 bool canCopy(const std::shared_ptr<C2Buffer> &buffer) const override;
314 bool copy(const std::shared_ptr<C2Buffer> &buffer) override;
315
Pawin Vongmasa36653902018-11-15 00:10:25 -0800316private:
317 ConstGraphicBlockBuffer(
318 const sp<AMessage> &format,
319 const sp<ABuffer> &aBuffer,
320 std::unique_ptr<const C2GraphicView> &&view,
321 const std::shared_ptr<C2Buffer> &buffer,
322 const sp<ABuffer> &imageData,
323 bool wrapped);
324 ConstGraphicBlockBuffer() = delete;
325
326 sp<ABuffer> mImageData;
327 std::unique_ptr<const C2GraphicView> mView;
328 std::shared_ptr<C2Buffer> mBufferRef;
329 const bool mWrapped;
330};
331
332/**
333 * MediaCodecBuffer implementation wraps around C2LinearBlock for component
334 * and IMemory for client. Underlying C2LinearBlock won't be mapped for secure
335 * usecases..
336 */
337class EncryptedLinearBlockBuffer : public Codec2Buffer {
338public:
339 /**
340 * Construct a new EncryptedLinearBufferBlock wrapping around C2LinearBlock
341 * object and writable IMemory region.
342 *
343 * \param format mandatory buffer format for MediaCodecBuffer
344 * \param block C2LinearBlock object to wrap around.
345 * \param memory IMemory object to store encrypted content.
346 * \param heapSeqNum Heap sequence number from ICrypto; -1 if N/A
347 */
348 EncryptedLinearBlockBuffer(
349 const sp<AMessage> &format,
350 const std::shared_ptr<C2LinearBlock> &block,
351 const sp<IMemory> &memory,
352 int32_t heapSeqNum = -1);
353 EncryptedLinearBlockBuffer() = delete;
354
355 virtual ~EncryptedLinearBlockBuffer() = default;
356
357 std::shared_ptr<C2Buffer> asC2Buffer() override;
358
359 /**
360 * Fill the source buffer structure with appropriate value based on
361 * internal IMemory object.
362 *
363 * \param source source buffer structure to fill.
364 */
Robert Shih895fba92019-07-16 16:29:44 -0700365 void fillSourceBuffer(
366 hardware::drm::V1_0::SharedBuffer *source);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800367 void fillSourceBuffer(
368 hardware::cas::native::V1_0::SharedBuffer *source);
369
370 /**
371 * Copy the content of |decrypted| into C2LinearBlock inside. This shall
372 * only be called in non-secure usecases.
373 *
374 * \param decrypted decrypted content to copy from.
375 * \param length length of the content
376 * \return true if successful
377 * false otherwise.
378 */
379 bool copyDecryptedContent(const sp<IMemory> &decrypted, size_t length);
380
381 /**
382 * Copy the content of internal IMemory object into C2LinearBlock inside.
383 * This shall only be called in non-secure usecases.
384 *
385 * \param length length of the content
386 * \return true if successful
387 * false otherwise.
388 */
389 bool copyDecryptedContentFromMemory(size_t length);
390
391 /**
392 * Return native handle of secure buffer understood by ICrypto.
393 *
394 * \return secure buffer handle
395 */
396 native_handle_t *handle() const;
397
398private:
399
400 std::shared_ptr<C2LinearBlock> mBlock;
401 sp<IMemory> mMemory;
402 sp<hardware::HidlMemory> mHidlMemory;
403 int32_t mHeapSeqNum;
404};
405
406} // namespace android
407
408#endif // CODEC2_BUFFER_H_