James Dong | 79f407c | 2011-05-05 12:50:04 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011, 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 _ANDROID_MEDIA_UTILS_H_ |
| 18 | #define _ANDROID_MEDIA_UTILS_H_ |
| 19 | |
Jaesung Chung | 8409c06 | 2016-01-19 10:48:30 +0900 | [diff] [blame] | 20 | #include "src/piex_types.h" |
| 21 | #include "src/piex.h" |
James Dong | 79f407c | 2011-05-05 12:50:04 -0700 | [diff] [blame] | 22 | |
Jaesung Chung | 8409c06 | 2016-01-19 10:48:30 +0900 | [diff] [blame] | 23 | #include <android_runtime/AndroidRuntime.h> |
Jaesung Chung | 6e08d2b | 2016-02-25 15:04:56 +0900 | [diff] [blame] | 24 | #include <camera3.h> |
| 25 | #include <gui/CpuConsumer.h> |
Jaesung Chung | 8409c06 | 2016-01-19 10:48:30 +0900 | [diff] [blame] | 26 | #include <jni.h> |
| 27 | #include <JNIHelp.h> |
James Dong | 79f407c | 2011-05-05 12:50:04 -0700 | [diff] [blame] | 28 | #include <utils/KeyedVector.h> |
| 29 | #include <utils/String8.h> |
Jaesung Chung | 6e08d2b | 2016-02-25 15:04:56 +0900 | [diff] [blame] | 30 | #include <SkStream.h> |
James Dong | 79f407c | 2011-05-05 12:50:04 -0700 | [diff] [blame] | 31 | |
| 32 | namespace android { |
| 33 | |
Jaesung Chung | 6e08d2b | 2016-02-25 15:04:56 +0900 | [diff] [blame] | 34 | class AssetStream : public piex::StreamInterface { |
| 35 | private: |
| 36 | SkStream *mStream; |
| 37 | size_t mPosition; |
| 38 | |
| 39 | public: |
Chih-Hung Hsieh | bd42349 | 2016-08-29 14:49:57 -0700 | [diff] [blame] | 40 | explicit AssetStream(SkStream* stream); |
Jaesung Chung | 6e08d2b | 2016-02-25 15:04:56 +0900 | [diff] [blame] | 41 | ~AssetStream(); |
| 42 | |
| 43 | // Reads 'length' amount of bytes from 'offset' to 'data'. The 'data' buffer |
| 44 | // provided by the caller, guaranteed to be at least "length" bytes long. |
| 45 | // On 'kOk' the 'data' pointer contains 'length' valid bytes beginning at |
| 46 | // 'offset' bytes from the start of the stream. |
| 47 | // Returns 'kFail' if 'offset' + 'length' exceeds the stream and does not |
| 48 | // change the contents of 'data'. |
| 49 | piex::Error GetData( |
| 50 | const size_t offset, const size_t length, std::uint8_t* data) override; |
| 51 | }; |
| 52 | |
| 53 | class BufferedStream : public piex::StreamInterface { |
| 54 | private: |
| 55 | SkStream *mStream; |
| 56 | // Growable memory stream |
| 57 | SkDynamicMemoryWStream mStreamBuffer; |
| 58 | |
| 59 | // Minimum size to read on filling the buffer. |
| 60 | const size_t kMinSizeToRead = 8192; |
| 61 | |
| 62 | public: |
Chih-Hung Hsieh | bd42349 | 2016-08-29 14:49:57 -0700 | [diff] [blame] | 63 | explicit BufferedStream(SkStream* stream); |
Jaesung Chung | 6e08d2b | 2016-02-25 15:04:56 +0900 | [diff] [blame] | 64 | ~BufferedStream(); |
| 65 | |
| 66 | // Reads 'length' amount of bytes from 'offset' to 'data'. The 'data' buffer |
| 67 | // provided by the caller, guaranteed to be at least "length" bytes long. |
| 68 | // On 'kOk' the 'data' pointer contains 'length' valid bytes beginning at |
| 69 | // 'offset' bytes from the start of the stream. |
| 70 | // Returns 'kFail' if 'offset' + 'length' exceeds the stream and does not |
| 71 | // change the contents of 'data'. |
| 72 | piex::Error GetData( |
| 73 | const size_t offset, const size_t length, std::uint8_t* data) override; |
| 74 | }; |
| 75 | |
Jaesung Chung | 8409c06 | 2016-01-19 10:48:30 +0900 | [diff] [blame] | 76 | class FileStream : public piex::StreamInterface { |
| 77 | private: |
| 78 | FILE *mFile; |
| 79 | size_t mPosition; |
Jaesung Chung | 8409c06 | 2016-01-19 10:48:30 +0900 | [diff] [blame] | 80 | |
| 81 | public: |
Chih-Hung Hsieh | bd42349 | 2016-08-29 14:49:57 -0700 | [diff] [blame] | 82 | explicit FileStream(const int fd); |
| 83 | explicit FileStream(const String8 filename); |
Jaesung Chung | 8409c06 | 2016-01-19 10:48:30 +0900 | [diff] [blame] | 84 | ~FileStream(); |
| 85 | |
| 86 | // Reads 'length' amount of bytes from 'offset' to 'data'. The 'data' buffer |
| 87 | // provided by the caller, guaranteed to be at least "length" bytes long. |
| 88 | // On 'kOk' the 'data' pointer contains 'length' valid bytes beginning at |
| 89 | // 'offset' bytes from the start of the stream. |
| 90 | // Returns 'kFail' if 'offset' + 'length' exceeds the stream and does not |
| 91 | // change the contents of 'data'. |
| 92 | piex::Error GetData( |
| 93 | const size_t offset, const size_t length, std::uint8_t* data) override; |
| 94 | bool exists() const; |
Jaesung Chung | 8409c06 | 2016-01-19 10:48:30 +0900 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | // Reads EXIF metadata from a given raw image via piex. |
| 98 | // And returns true if the operation is successful; otherwise, false. |
| 99 | bool GetExifFromRawImage( |
Jaesung Chung | 6e08d2b | 2016-02-25 15:04:56 +0900 | [diff] [blame] | 100 | piex::StreamInterface* stream, const String8& filename, piex::PreviewImageData& image_data); |
Jaesung Chung | 8409c06 | 2016-01-19 10:48:30 +0900 | [diff] [blame] | 101 | |
| 102 | // Returns true if the conversion is successful; otherwise, false. |
James Dong | 79f407c | 2011-05-05 12:50:04 -0700 | [diff] [blame] | 103 | bool ConvertKeyValueArraysToKeyedVector( |
Jaesung Chung | 8409c06 | 2016-01-19 10:48:30 +0900 | [diff] [blame] | 104 | JNIEnv *env, jobjectArray keys, jobjectArray values, |
| 105 | KeyedVector<String8, String8>* vector); |
James Dong | 79f407c | 2011-05-05 12:50:04 -0700 | [diff] [blame] | 106 | |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 107 | struct AMessage; |
| 108 | status_t ConvertMessageToMap( |
| 109 | JNIEnv *env, const sp<AMessage> &msg, jobject *map); |
| 110 | |
| 111 | status_t ConvertKeyValueArraysToMessage( |
| 112 | JNIEnv *env, jobjectArray keys, jobjectArray values, |
| 113 | sp<AMessage> *msg); |
| 114 | |
Zhijun He | 0ab4162 | 2016-02-25 16:00:38 -0800 | [diff] [blame] | 115 | // -----------Utility functions used by ImageReader/Writer JNI----------------- |
| 116 | |
| 117 | typedef CpuConsumer::LockedBuffer LockedImage; |
| 118 | |
| 119 | bool usingRGBAToJpegOverride(int32_t imageFormat, int32_t containerFormat); |
| 120 | |
| 121 | int32_t applyFormatOverrides(int32_t imageFormat, int32_t containerFormat); |
| 122 | |
| 123 | uint32_t Image_getJpegSize(LockedImage* buffer, bool usingRGBAOverride); |
| 124 | |
| 125 | bool isFormatOpaque(int format); |
| 126 | |
| 127 | bool isPossiblyYUV(PixelFormat format); |
| 128 | |
| 129 | status_t getLockedImageInfo(LockedImage* buffer, int idx, int32_t containerFormat, |
| 130 | uint8_t **base, uint32_t *size, int *pixelStride, int *rowStride); |
| 131 | |
| 132 | status_t lockImageFromBuffer(sp<GraphicBuffer> buffer, uint32_t inUsage, |
| 133 | const Rect& rect, int fenceFd, LockedImage* outputImage); |
| 134 | |
| 135 | status_t lockImageFromBuffer(BufferItem* bufferItem, uint32_t inUsage, |
| 136 | int fenceFd, LockedImage* outputImage); |
| 137 | |
| 138 | int getBufferWidth(BufferItem *buffer); |
| 139 | |
| 140 | int getBufferHeight(BufferItem *buffer); |
| 141 | |
James Dong | 79f407c | 2011-05-05 12:50:04 -0700 | [diff] [blame] | 142 | }; // namespace android |
| 143 | |
| 144 | #endif // _ANDROID_MEDIA_UTILS_H_ |