179f407cc6c5ae34fc6f04d52fd034b49d1f002c4James Dong/*
279f407cc6c5ae34fc6f04d52fd034b49d1f002c4James Dong * Copyright 2011, The Android Open Source Project
379f407cc6c5ae34fc6f04d52fd034b49d1f002c4James Dong *
479f407cc6c5ae34fc6f04d52fd034b49d1f002c4James Dong * Licensed under the Apache License, Version 2.0 (the "License");
579f407cc6c5ae34fc6f04d52fd034b49d1f002c4James Dong * you may not use this file except in compliance with the License.
679f407cc6c5ae34fc6f04d52fd034b49d1f002c4James Dong * You may obtain a copy of the License at
779f407cc6c5ae34fc6f04d52fd034b49d1f002c4James Dong *
879f407cc6c5ae34fc6f04d52fd034b49d1f002c4James Dong *     http://www.apache.org/licenses/LICENSE-2.0
979f407cc6c5ae34fc6f04d52fd034b49d1f002c4James Dong *
1079f407cc6c5ae34fc6f04d52fd034b49d1f002c4James Dong * Unless required by applicable law or agreed to in writing, software
1179f407cc6c5ae34fc6f04d52fd034b49d1f002c4James Dong * distributed under the License is distributed on an "AS IS" BASIS,
1279f407cc6c5ae34fc6f04d52fd034b49d1f002c4James Dong * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1379f407cc6c5ae34fc6f04d52fd034b49d1f002c4James Dong * See the License for the specific language governing permissions and
1479f407cc6c5ae34fc6f04d52fd034b49d1f002c4James Dong * limitations under the License.
1579f407cc6c5ae34fc6f04d52fd034b49d1f002c4James Dong */
1679f407cc6c5ae34fc6f04d52fd034b49d1f002c4James Dong
1779f407cc6c5ae34fc6f04d52fd034b49d1f002c4James Dong#ifndef _ANDROID_MEDIA_UTILS_H_
1879f407cc6c5ae34fc6f04d52fd034b49d1f002c4James Dong#define _ANDROID_MEDIA_UTILS_H_
1979f407cc6c5ae34fc6f04d52fd034b49d1f002c4James Dong
208409c0691fb128fef6244305977342776bddf919Jaesung Chung#include "src/piex_types.h"
218409c0691fb128fef6244305977342776bddf919Jaesung Chung#include "src/piex.h"
2279f407cc6c5ae34fc6f04d52fd034b49d1f002c4James Dong
238409c0691fb128fef6244305977342776bddf919Jaesung Chung#include <android_runtime/AndroidRuntime.h>
246e08d2b082acfc773907733f2ddab4e9b60616e1Jaesung Chung#include <camera3.h>
256e08d2b082acfc773907733f2ddab4e9b60616e1Jaesung Chung#include <gui/CpuConsumer.h>
268409c0691fb128fef6244305977342776bddf919Jaesung Chung#include <jni.h>
278409c0691fb128fef6244305977342776bddf919Jaesung Chung#include <JNIHelp.h>
2879f407cc6c5ae34fc6f04d52fd034b49d1f002c4James Dong#include <utils/KeyedVector.h>
2979f407cc6c5ae34fc6f04d52fd034b49d1f002c4James Dong#include <utils/String8.h>
306e08d2b082acfc773907733f2ddab4e9b60616e1Jaesung Chung#include <SkStream.h>
3179f407cc6c5ae34fc6f04d52fd034b49d1f002c4James Dong
3279f407cc6c5ae34fc6f04d52fd034b49d1f002c4James Dongnamespace android {
3379f407cc6c5ae34fc6f04d52fd034b49d1f002c4James Dong
346e08d2b082acfc773907733f2ddab4e9b60616e1Jaesung Chungclass AssetStream : public piex::StreamInterface {
356e08d2b082acfc773907733f2ddab4e9b60616e1Jaesung Chungprivate:
366e08d2b082acfc773907733f2ddab4e9b60616e1Jaesung Chung    SkStream *mStream;
376e08d2b082acfc773907733f2ddab4e9b60616e1Jaesung Chung    size_t mPosition;
386e08d2b082acfc773907733f2ddab4e9b60616e1Jaesung Chung
396e08d2b082acfc773907733f2ddab4e9b60616e1Jaesung Chungpublic:
40bd42349f7e97f172d5496ecaf2273994ced43b0dChih-Hung Hsieh    explicit AssetStream(SkStream* stream);
416e08d2b082acfc773907733f2ddab4e9b60616e1Jaesung Chung    ~AssetStream();
426e08d2b082acfc773907733f2ddab4e9b60616e1Jaesung Chung
436e08d2b082acfc773907733f2ddab4e9b60616e1Jaesung Chung    // Reads 'length' amount of bytes from 'offset' to 'data'. The 'data' buffer
446e08d2b082acfc773907733f2ddab4e9b60616e1Jaesung Chung    // provided by the caller, guaranteed to be at least "length" bytes long.
456e08d2b082acfc773907733f2ddab4e9b60616e1Jaesung Chung    // On 'kOk' the 'data' pointer contains 'length' valid bytes beginning at
466e08d2b082acfc773907733f2ddab4e9b60616e1Jaesung Chung    // 'offset' bytes from the start of the stream.
476e08d2b082acfc773907733f2ddab4e9b60616e1Jaesung Chung    // Returns 'kFail' if 'offset' + 'length' exceeds the stream and does not
486e08d2b082acfc773907733f2ddab4e9b60616e1Jaesung Chung    // change the contents of 'data'.
496e08d2b082acfc773907733f2ddab4e9b60616e1Jaesung Chung    piex::Error GetData(
506e08d2b082acfc773907733f2ddab4e9b60616e1Jaesung Chung            const size_t offset, const size_t length, std::uint8_t* data) override;
516e08d2b082acfc773907733f2ddab4e9b60616e1Jaesung Chung};
526e08d2b082acfc773907733f2ddab4e9b60616e1Jaesung Chung
536e08d2b082acfc773907733f2ddab4e9b60616e1Jaesung Chungclass BufferedStream : public piex::StreamInterface {
546e08d2b082acfc773907733f2ddab4e9b60616e1Jaesung Chungprivate:
556e08d2b082acfc773907733f2ddab4e9b60616e1Jaesung Chung    SkStream *mStream;
566e08d2b082acfc773907733f2ddab4e9b60616e1Jaesung Chung    // Growable memory stream
576e08d2b082acfc773907733f2ddab4e9b60616e1Jaesung Chung    SkDynamicMemoryWStream mStreamBuffer;
586e08d2b082acfc773907733f2ddab4e9b60616e1Jaesung Chung
596e08d2b082acfc773907733f2ddab4e9b60616e1Jaesung Chung    // Minimum size to read on filling the buffer.
606e08d2b082acfc773907733f2ddab4e9b60616e1Jaesung Chung    const size_t kMinSizeToRead = 8192;
616e08d2b082acfc773907733f2ddab4e9b60616e1Jaesung Chung
626e08d2b082acfc773907733f2ddab4e9b60616e1Jaesung Chungpublic:
63bd42349f7e97f172d5496ecaf2273994ced43b0dChih-Hung Hsieh    explicit BufferedStream(SkStream* stream);
646e08d2b082acfc773907733f2ddab4e9b60616e1Jaesung Chung    ~BufferedStream();
656e08d2b082acfc773907733f2ddab4e9b60616e1Jaesung Chung
666e08d2b082acfc773907733f2ddab4e9b60616e1Jaesung Chung    // Reads 'length' amount of bytes from 'offset' to 'data'. The 'data' buffer
676e08d2b082acfc773907733f2ddab4e9b60616e1Jaesung Chung    // provided by the caller, guaranteed to be at least "length" bytes long.
686e08d2b082acfc773907733f2ddab4e9b60616e1Jaesung Chung    // On 'kOk' the 'data' pointer contains 'length' valid bytes beginning at
696e08d2b082acfc773907733f2ddab4e9b60616e1Jaesung Chung    // 'offset' bytes from the start of the stream.
706e08d2b082acfc773907733f2ddab4e9b60616e1Jaesung Chung    // Returns 'kFail' if 'offset' + 'length' exceeds the stream and does not
716e08d2b082acfc773907733f2ddab4e9b60616e1Jaesung Chung    // change the contents of 'data'.
726e08d2b082acfc773907733f2ddab4e9b60616e1Jaesung Chung    piex::Error GetData(
736e08d2b082acfc773907733f2ddab4e9b60616e1Jaesung Chung            const size_t offset, const size_t length, std::uint8_t* data) override;
746e08d2b082acfc773907733f2ddab4e9b60616e1Jaesung Chung};
756e08d2b082acfc773907733f2ddab4e9b60616e1Jaesung Chung
768409c0691fb128fef6244305977342776bddf919Jaesung Chungclass FileStream : public piex::StreamInterface {
778409c0691fb128fef6244305977342776bddf919Jaesung Chungprivate:
788409c0691fb128fef6244305977342776bddf919Jaesung Chung    FILE *mFile;
798409c0691fb128fef6244305977342776bddf919Jaesung Chung    size_t mPosition;
808409c0691fb128fef6244305977342776bddf919Jaesung Chung
818409c0691fb128fef6244305977342776bddf919Jaesung Chungpublic:
82bd42349f7e97f172d5496ecaf2273994ced43b0dChih-Hung Hsieh    explicit FileStream(const int fd);
83bd42349f7e97f172d5496ecaf2273994ced43b0dChih-Hung Hsieh    explicit FileStream(const String8 filename);
848409c0691fb128fef6244305977342776bddf919Jaesung Chung    ~FileStream();
858409c0691fb128fef6244305977342776bddf919Jaesung Chung
868409c0691fb128fef6244305977342776bddf919Jaesung Chung    // Reads 'length' amount of bytes from 'offset' to 'data'. The 'data' buffer
878409c0691fb128fef6244305977342776bddf919Jaesung Chung    // provided by the caller, guaranteed to be at least "length" bytes long.
888409c0691fb128fef6244305977342776bddf919Jaesung Chung    // On 'kOk' the 'data' pointer contains 'length' valid bytes beginning at
898409c0691fb128fef6244305977342776bddf919Jaesung Chung    // 'offset' bytes from the start of the stream.
908409c0691fb128fef6244305977342776bddf919Jaesung Chung    // Returns 'kFail' if 'offset' + 'length' exceeds the stream and does not
918409c0691fb128fef6244305977342776bddf919Jaesung Chung    // change the contents of 'data'.
928409c0691fb128fef6244305977342776bddf919Jaesung Chung    piex::Error GetData(
938409c0691fb128fef6244305977342776bddf919Jaesung Chung            const size_t offset, const size_t length, std::uint8_t* data) override;
948409c0691fb128fef6244305977342776bddf919Jaesung Chung    bool exists() const;
958409c0691fb128fef6244305977342776bddf919Jaesung Chung};
968409c0691fb128fef6244305977342776bddf919Jaesung Chung
978409c0691fb128fef6244305977342776bddf919Jaesung Chung// Reads EXIF metadata from a given raw image via piex.
988409c0691fb128fef6244305977342776bddf919Jaesung Chung// And returns true if the operation is successful; otherwise, false.
998409c0691fb128fef6244305977342776bddf919Jaesung Chungbool GetExifFromRawImage(
1006e08d2b082acfc773907733f2ddab4e9b60616e1Jaesung Chung        piex::StreamInterface* stream, const String8& filename, piex::PreviewImageData& image_data);
1018409c0691fb128fef6244305977342776bddf919Jaesung Chung
1028409c0691fb128fef6244305977342776bddf919Jaesung Chung// Returns true if the conversion is successful; otherwise, false.
10379f407cc6c5ae34fc6f04d52fd034b49d1f002c4James Dongbool ConvertKeyValueArraysToKeyedVector(
1048409c0691fb128fef6244305977342776bddf919Jaesung Chung        JNIEnv *env, jobjectArray keys, jobjectArray values,
1058409c0691fb128fef6244305977342776bddf919Jaesung Chung        KeyedVector<String8, String8>* vector);
10679f407cc6c5ae34fc6f04d52fd034b49d1f002c4James Dong
10788572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huberstruct AMessage;
10888572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huberstatus_t ConvertMessageToMap(
10988572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber        JNIEnv *env, const sp<AMessage> &msg, jobject *map);
11088572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber
11188572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huberstatus_t ConvertKeyValueArraysToMessage(
11288572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber        JNIEnv *env, jobjectArray keys, jobjectArray values,
11388572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber        sp<AMessage> *msg);
11488572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber
1150ab416269a866c8afa8f65d9351afa2407abee4cZhijun He// -----------Utility functions used by ImageReader/Writer JNI-----------------
1160ab416269a866c8afa8f65d9351afa2407abee4cZhijun He
1170ab416269a866c8afa8f65d9351afa2407abee4cZhijun Hetypedef CpuConsumer::LockedBuffer LockedImage;
1180ab416269a866c8afa8f65d9351afa2407abee4cZhijun He
1190ab416269a866c8afa8f65d9351afa2407abee4cZhijun Hebool usingRGBAToJpegOverride(int32_t imageFormat, int32_t containerFormat);
1200ab416269a866c8afa8f65d9351afa2407abee4cZhijun He
1210ab416269a866c8afa8f65d9351afa2407abee4cZhijun Heint32_t applyFormatOverrides(int32_t imageFormat, int32_t containerFormat);
1220ab416269a866c8afa8f65d9351afa2407abee4cZhijun He
1230ab416269a866c8afa8f65d9351afa2407abee4cZhijun Heuint32_t Image_getJpegSize(LockedImage* buffer, bool usingRGBAOverride);
1240ab416269a866c8afa8f65d9351afa2407abee4cZhijun He
1250ab416269a866c8afa8f65d9351afa2407abee4cZhijun Hebool isFormatOpaque(int format);
1260ab416269a866c8afa8f65d9351afa2407abee4cZhijun He
1270ab416269a866c8afa8f65d9351afa2407abee4cZhijun Hebool isPossiblyYUV(PixelFormat format);
1280ab416269a866c8afa8f65d9351afa2407abee4cZhijun He
1290ab416269a866c8afa8f65d9351afa2407abee4cZhijun Hestatus_t getLockedImageInfo(LockedImage* buffer, int idx, int32_t containerFormat,
1300ab416269a866c8afa8f65d9351afa2407abee4cZhijun He        uint8_t **base, uint32_t *size, int *pixelStride, int *rowStride);
1310ab416269a866c8afa8f65d9351afa2407abee4cZhijun He
1320ab416269a866c8afa8f65d9351afa2407abee4cZhijun Hestatus_t lockImageFromBuffer(sp<GraphicBuffer> buffer, uint32_t inUsage,
1330ab416269a866c8afa8f65d9351afa2407abee4cZhijun He        const Rect& rect, int fenceFd, LockedImage* outputImage);
1340ab416269a866c8afa8f65d9351afa2407abee4cZhijun He
1350ab416269a866c8afa8f65d9351afa2407abee4cZhijun Hestatus_t lockImageFromBuffer(BufferItem* bufferItem, uint32_t inUsage,
1360ab416269a866c8afa8f65d9351afa2407abee4cZhijun He        int fenceFd, LockedImage* outputImage);
1370ab416269a866c8afa8f65d9351afa2407abee4cZhijun He
1380ab416269a866c8afa8f65d9351afa2407abee4cZhijun Heint getBufferWidth(BufferItem *buffer);
1390ab416269a866c8afa8f65d9351afa2407abee4cZhijun He
1400ab416269a866c8afa8f65d9351afa2407abee4cZhijun Heint getBufferHeight(BufferItem *buffer);
1410ab416269a866c8afa8f65d9351afa2407abee4cZhijun He
14279f407cc6c5ae34fc6f04d52fd034b49d1f002c4James Dong};  // namespace android
14379f407cc6c5ae34fc6f04d52fd034b49d1f002c4James Dong
14479f407cc6c5ae34fc6f04d52fd034b49d1f002c4James Dong#endif //  _ANDROID_MEDIA_UTILS_H_
145