2 * Copyright 2011, The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 // #define LOG_NDEBUG 0
18 #define LOG_TAG "AndroidMediaUtils"
20 #include <utils/Log.h>
21 #include "android_media_Utils.h"
23 #include <media/stagefright/foundation/ADebug.h>
24 #include <media/stagefright/foundation/ABuffer.h>
25 #include <media/stagefright/foundation/AMessage.h>
27 #include <nativehelper/ScopedLocalRef.h>
29 #define ALIGN(x, mask) ( ((x) + (mask) - 1) & ~((mask) - 1) )
33 AssetStream::AssetStream(SkStream* stream)
34 : mStream(stream), mPosition(0) {
37 AssetStream::~AssetStream() {
40 piex::Error AssetStream::GetData(
41 const size_t offset, const size_t length, std::uint8_t* data) {
43 if (mPosition != offset) {
44 if (!mStream->seek(offset)) {
45 return piex::Error::kFail;
50 size_t size = mStream->read((void*)data, length);
51 mPosition = offset + size;
53 return size == length ? piex::Error::kOk : piex::Error::kFail;
56 BufferedStream::BufferedStream(SkStream* stream)
60 BufferedStream::~BufferedStream() {
63 piex::Error BufferedStream::GetData(
64 const size_t offset, const size_t length, std::uint8_t* data) {
66 if (offset + length > mStreamBuffer.bytesWritten()) {
67 size_t sizeToRead = offset + length - mStreamBuffer.bytesWritten();
68 if (sizeToRead <= kMinSizeToRead) {
69 sizeToRead = kMinSizeToRead;
71 void* tempBuffer = malloc(sizeToRead);
72 if (tempBuffer != NULL) {
73 size_t bytesRead = mStream->read(tempBuffer, sizeToRead);
74 if (bytesRead != sizeToRead) {
76 return piex::Error::kFail;
78 mStreamBuffer.write(tempBuffer, bytesRead);
84 if (mStreamBuffer.read((void*)data, offset, length)) {
85 return piex::Error::kOk;
87 return piex::Error::kFail;
91 FileStream::FileStream(const int fd)
93 mFile = fdopen(fd, "r");
99 FileStream::FileStream(const String8 filename)
101 mFile = fopen(filename.string(), "r");
107 FileStream::~FileStream() {
114 piex::Error FileStream::GetData(
115 const size_t offset, const size_t length, std::uint8_t* data) {
117 return piex::Error::kFail;
121 if (mPosition != offset) {
122 fseek(mFile, offset, SEEK_SET);
126 size_t size = fread((void*)data, sizeof(std::uint8_t), length, mFile);
131 ALOGV("GetData read failed: (offset: %zu, length: %zu)", offset, length);
132 return piex::Error::kFail;
134 return piex::Error::kOk;
137 bool FileStream::exists() const {
138 return mFile != NULL;
141 bool GetExifFromRawImage(
142 piex::StreamInterface* stream, const String8& filename,
143 piex::PreviewImageData& image_data) {
144 // Reset the PreviewImageData to its default.
145 image_data = piex::PreviewImageData();
147 if (!piex::IsRaw(stream)) {
148 // Format not supported.
149 ALOGV("Format not supported: %s", filename.string());
153 piex::Error err = piex::GetPreviewImageData(stream, &image_data);
155 if (err != piex::Error::kOk) {
156 // The input data seems to be broken.
157 ALOGV("Raw image not detected: %s (piex error code: %d)", filename.string(), (int32_t)err);
164 bool ConvertKeyValueArraysToKeyedVector(
165 JNIEnv *env, jobjectArray keys, jobjectArray values,
166 KeyedVector<String8, String8>* keyedVector) {
168 int nKeyValuePairs = 0;
170 if (keys != NULL && values != NULL) {
171 nKeyValuePairs = env->GetArrayLength(keys);
172 failed = (nKeyValuePairs != env->GetArrayLength(values));
176 failed = ((keys != NULL && values == NULL) ||
177 (keys == NULL && values != NULL));
181 ALOGE("keys and values arrays have different length");
182 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
186 for (int i = 0; i < nKeyValuePairs; ++i) {
187 // No need to check on the ArrayIndexOutOfBoundsException, since
188 // it won't happen here.
189 jstring key = (jstring) env->GetObjectArrayElement(keys, i);
190 jstring value = (jstring) env->GetObjectArrayElement(values, i);
192 const char* keyStr = env->GetStringUTFChars(key, NULL);
193 if (!keyStr) { // OutOfMemoryError
197 const char* valueStr = env->GetStringUTFChars(value, NULL);
198 if (!valueStr) { // OutOfMemoryError
199 env->ReleaseStringUTFChars(key, keyStr);
203 keyedVector->add(String8(keyStr), String8(valueStr));
205 env->ReleaseStringUTFChars(key, keyStr);
206 env->ReleaseStringUTFChars(value, valueStr);
207 env->DeleteLocalRef(key);
208 env->DeleteLocalRef(value);
213 static jobject makeIntegerObject(JNIEnv *env, int32_t value) {
214 ScopedLocalRef<jclass> clazz(env, env->FindClass("java/lang/Integer"));
215 CHECK(clazz.get() != NULL);
217 jmethodID integerConstructID =
218 env->GetMethodID(clazz.get(), "<init>", "(I)V");
219 CHECK(integerConstructID != NULL);
221 return env->NewObject(clazz.get(), integerConstructID, value);
224 static jobject makeLongObject(JNIEnv *env, int64_t value) {
225 ScopedLocalRef<jclass> clazz(env, env->FindClass("java/lang/Long"));
226 CHECK(clazz.get() != NULL);
228 jmethodID longConstructID = env->GetMethodID(clazz.get(), "<init>", "(J)V");
229 CHECK(longConstructID != NULL);
231 return env->NewObject(clazz.get(), longConstructID, value);
234 static jobject makeFloatObject(JNIEnv *env, float value) {
235 ScopedLocalRef<jclass> clazz(env, env->FindClass("java/lang/Float"));
236 CHECK(clazz.get() != NULL);
238 jmethodID floatConstructID =
239 env->GetMethodID(clazz.get(), "<init>", "(F)V");
240 CHECK(floatConstructID != NULL);
242 return env->NewObject(clazz.get(), floatConstructID, value);
245 static jobject makeByteBufferObject(
246 JNIEnv *env, const void *data, size_t size) {
247 jbyteArray byteArrayObj = env->NewByteArray(size);
248 env->SetByteArrayRegion(byteArrayObj, 0, size, (const jbyte *)data);
250 ScopedLocalRef<jclass> clazz(env, env->FindClass("java/nio/ByteBuffer"));
251 CHECK(clazz.get() != NULL);
253 jmethodID byteBufWrapID =
254 env->GetStaticMethodID(
255 clazz.get(), "wrap", "([B)Ljava/nio/ByteBuffer;");
256 CHECK(byteBufWrapID != NULL);
258 jobject byteBufObj = env->CallStaticObjectMethod(
259 clazz.get(), byteBufWrapID, byteArrayObj);
261 env->DeleteLocalRef(byteArrayObj); byteArrayObj = NULL;
266 static void SetMapInt32(
267 JNIEnv *env, jobject hashMapObj, jmethodID hashMapPutID,
268 const char *key, int32_t value) {
269 jstring keyObj = env->NewStringUTF(key);
270 jobject valueObj = makeIntegerObject(env, value);
272 env->CallObjectMethod(hashMapObj, hashMapPutID, keyObj, valueObj);
274 env->DeleteLocalRef(valueObj); valueObj = NULL;
275 env->DeleteLocalRef(keyObj); keyObj = NULL;
278 status_t ConvertMessageToMap(
279 JNIEnv *env, const sp<AMessage> &msg, jobject *map) {
280 ScopedLocalRef<jclass> hashMapClazz(
281 env, env->FindClass("java/util/HashMap"));
283 if (hashMapClazz.get() == NULL) {
287 jmethodID hashMapConstructID =
288 env->GetMethodID(hashMapClazz.get(), "<init>", "()V");
290 if (hashMapConstructID == NULL) {
294 jmethodID hashMapPutID =
298 "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
300 if (hashMapPutID == NULL) {
304 jobject hashMap = env->NewObject(hashMapClazz.get(), hashMapConstructID);
306 for (size_t i = 0; i < msg->countEntries(); ++i) {
307 AMessage::Type valueType;
308 const char *key = msg->getEntryNameAt(i, &valueType);
310 if (!strncmp(key, "android._", 9)) {
311 // don't expose private keys (starting with android._)
315 jobject valueObj = NULL;
318 case AMessage::kTypeInt32:
321 CHECK(msg->findInt32(key, &val));
323 valueObj = makeIntegerObject(env, val);
327 case AMessage::kTypeInt64:
330 CHECK(msg->findInt64(key, &val));
332 valueObj = makeLongObject(env, val);
336 case AMessage::kTypeFloat:
339 CHECK(msg->findFloat(key, &val));
341 valueObj = makeFloatObject(env, val);
345 case AMessage::kTypeString:
348 CHECK(msg->findString(key, &val));
350 valueObj = env->NewStringUTF(val.c_str());
354 case AMessage::kTypeBuffer:
357 CHECK(msg->findBuffer(key, &buffer));
359 valueObj = makeByteBufferObject(
360 env, buffer->data(), buffer->size());
364 case AMessage::kTypeRect:
366 int32_t left, top, right, bottom;
367 CHECK(msg->findRect(key, &left, &top, &right, &bottom));
373 AStringPrintf("%s-left", key).c_str(),
380 AStringPrintf("%s-top", key).c_str(),
387 AStringPrintf("%s-right", key).c_str(),
394 AStringPrintf("%s-bottom", key).c_str(),
403 if (valueObj != NULL) {
404 jstring keyObj = env->NewStringUTF(key);
406 env->CallObjectMethod(hashMap, hashMapPutID, keyObj, valueObj);
408 env->DeleteLocalRef(keyObj); keyObj = NULL;
409 env->DeleteLocalRef(valueObj); valueObj = NULL;
418 status_t ConvertKeyValueArraysToMessage(
419 JNIEnv *env, jobjectArray keys, jobjectArray values,
421 ScopedLocalRef<jclass> stringClass(env, env->FindClass("java/lang/String"));
422 CHECK(stringClass.get() != NULL);
423 ScopedLocalRef<jclass> integerClass(env, env->FindClass("java/lang/Integer"));
424 CHECK(integerClass.get() != NULL);
425 ScopedLocalRef<jclass> longClass(env, env->FindClass("java/lang/Long"));
426 CHECK(longClass.get() != NULL);
427 ScopedLocalRef<jclass> floatClass(env, env->FindClass("java/lang/Float"));
428 CHECK(floatClass.get() != NULL);
429 ScopedLocalRef<jclass> byteBufClass(env, env->FindClass("java/nio/ByteBuffer"));
430 CHECK(byteBufClass.get() != NULL);
432 sp<AMessage> msg = new AMessage;
434 jsize numEntries = 0;
437 if (values == NULL) {
441 numEntries = env->GetArrayLength(keys);
443 if (numEntries != env->GetArrayLength(values)) {
446 } else if (values != NULL) {
450 for (jsize i = 0; i < numEntries; ++i) {
451 jobject keyObj = env->GetObjectArrayElement(keys, i);
453 if (!env->IsInstanceOf(keyObj, stringClass.get())) {
457 const char *tmp = env->GetStringUTFChars((jstring)keyObj, NULL);
465 env->ReleaseStringUTFChars((jstring)keyObj, tmp);
468 if (key.startsWith("android._")) {
469 // don't propagate private keys (starting with android._)
473 jobject valueObj = env->GetObjectArrayElement(values, i);
475 if (env->IsInstanceOf(valueObj, stringClass.get())) {
476 const char *value = env->GetStringUTFChars((jstring)valueObj, NULL);
482 msg->setString(key.c_str(), value);
484 env->ReleaseStringUTFChars((jstring)valueObj, value);
486 } else if (env->IsInstanceOf(valueObj, integerClass.get())) {
487 jmethodID intValueID =
488 env->GetMethodID(integerClass.get(), "intValue", "()I");
489 CHECK(intValueID != NULL);
491 jint value = env->CallIntMethod(valueObj, intValueID);
493 msg->setInt32(key.c_str(), value);
494 } else if (env->IsInstanceOf(valueObj, longClass.get())) {
495 jmethodID longValueID =
496 env->GetMethodID(longClass.get(), "longValue", "()J");
497 CHECK(longValueID != NULL);
499 jlong value = env->CallLongMethod(valueObj, longValueID);
501 msg->setInt64(key.c_str(), value);
502 } else if (env->IsInstanceOf(valueObj, floatClass.get())) {
503 jmethodID floatValueID =
504 env->GetMethodID(floatClass.get(), "floatValue", "()F");
505 CHECK(floatValueID != NULL);
507 jfloat value = env->CallFloatMethod(valueObj, floatValueID);
509 msg->setFloat(key.c_str(), value);
510 } else if (env->IsInstanceOf(valueObj, byteBufClass.get())) {
511 jmethodID positionID =
512 env->GetMethodID(byteBufClass.get(), "position", "()I");
513 CHECK(positionID != NULL);
516 env->GetMethodID(byteBufClass.get(), "limit", "()I");
517 CHECK(limitID != NULL);
519 jint position = env->CallIntMethod(valueObj, positionID);
520 jint limit = env->CallIntMethod(valueObj, limitID);
522 sp<ABuffer> buffer = new ABuffer(limit - position);
524 void *data = env->GetDirectBufferAddress(valueObj);
527 memcpy(buffer->data(),
528 (const uint8_t *)data + position,
532 env->GetMethodID(byteBufClass.get(), "array", "()[B");
533 CHECK(arrayID != NULL);
535 jbyteArray byteArray =
536 (jbyteArray)env->CallObjectMethod(valueObj, arrayID);
537 CHECK(byteArray != NULL);
539 env->GetByteArrayRegion(
543 (jbyte *)buffer->data());
545 env->DeleteLocalRef(byteArray); byteArray = NULL;
548 msg->setBuffer(key.c_str(), buffer);
557 // -----------Utility functions used by ImageReader/Writer JNI-----------------
560 IMAGE_MAX_NUM_PLANES = 3,
563 bool usingRGBAToJpegOverride(int32_t imageFormat,
564 int32_t containerFormat) {
565 return containerFormat == HAL_PIXEL_FORMAT_BLOB && imageFormat == HAL_PIXEL_FORMAT_RGBA_8888;
568 int32_t applyFormatOverrides(int32_t imageFormat, int32_t containerFormat) {
569 // Using HAL_PIXEL_FORMAT_RGBA_8888 gralloc buffers containing JPEGs to get around SW
570 // write limitations for some platforms (b/17379185).
571 if (usingRGBAToJpegOverride(imageFormat, containerFormat)) {
572 return HAL_PIXEL_FORMAT_BLOB;
574 return containerFormat;
577 bool isFormatOpaque(int format) {
578 // This is the only opaque format exposed in the ImageFormat public API.
579 // Note that we do support CPU access for HAL_PIXEL_FORMAT_RAW_OPAQUE
580 // (ImageFormat#RAW_PRIVATE) so it doesn't count as opaque here.
581 return format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
584 bool isPossiblyYUV(PixelFormat format) {
585 switch (static_cast<int>(format)) {
586 case HAL_PIXEL_FORMAT_RGBA_8888:
587 case HAL_PIXEL_FORMAT_RGBX_8888:
588 case HAL_PIXEL_FORMAT_RGB_888:
589 case HAL_PIXEL_FORMAT_RGB_565:
590 case HAL_PIXEL_FORMAT_BGRA_8888:
591 case HAL_PIXEL_FORMAT_Y8:
592 case HAL_PIXEL_FORMAT_Y16:
593 case HAL_PIXEL_FORMAT_RAW16:
594 case HAL_PIXEL_FORMAT_RAW10:
595 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
596 case HAL_PIXEL_FORMAT_BLOB:
597 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
600 case HAL_PIXEL_FORMAT_YV12:
601 case HAL_PIXEL_FORMAT_YCbCr_420_888:
602 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
608 uint32_t Image_getJpegSize(LockedImage* buffer, bool usingRGBAOverride) {
609 ALOGV("%s", __FUNCTION__);
610 LOG_ALWAYS_FATAL_IF(buffer == NULL, "Input buffer is NULL!!!");
612 uint32_t width = buffer->width;
613 uint8_t* jpegBuffer = buffer->data;
615 if (usingRGBAOverride) {
616 width = (buffer->width + buffer->stride * (buffer->height - 1)) * 4;
619 // First check for JPEG transport header at the end of the buffer
620 uint8_t* header = jpegBuffer + (width - sizeof(struct camera3_jpeg_blob));
621 struct camera3_jpeg_blob *blob = (struct camera3_jpeg_blob*)(header);
622 if (blob->jpeg_blob_id == CAMERA3_JPEG_BLOB_ID) {
623 size = blob->jpeg_size;
624 ALOGV("%s: Jpeg size = %d", __FUNCTION__, size);
627 // failed to find size, default to whole buffer
630 * This is a problem because not including the JPEG header
631 * means that in certain rare situations a regular JPEG blob
632 * will be mis-identified as having a header, in which case
633 * we will get a garbage size value.
635 ALOGW("%s: No JPEG header detected, defaulting to size=width=%d",
636 __FUNCTION__, width);
643 status_t getLockedImageInfo(LockedImage* buffer, int idx,
644 int32_t containerFormat, uint8_t **base, uint32_t *size, int *pixelStride, int *rowStride) {
645 ALOGV("%s", __FUNCTION__);
646 LOG_ALWAYS_FATAL_IF(buffer == NULL, "Input buffer is NULL!!!");
647 LOG_ALWAYS_FATAL_IF(base == NULL, "base is NULL!!!");
648 LOG_ALWAYS_FATAL_IF(size == NULL, "size is NULL!!!");
649 LOG_ALWAYS_FATAL_IF(pixelStride == NULL, "pixelStride is NULL!!!");
650 LOG_ALWAYS_FATAL_IF(rowStride == NULL, "rowStride is NULL!!!");
651 LOG_ALWAYS_FATAL_IF((idx >= IMAGE_MAX_NUM_PLANES) || (idx < 0), "idx (%d) is illegal", idx);
653 ALOGV("%s: buffer: %p", __FUNCTION__, buffer);
655 uint32_t dataSize, ySize, cSize, cStride;
656 uint32_t pStride = 0, rStride = 0;
658 uint8_t *pData = NULL;
659 int bytesPerPixel = 0;
661 dataSize = ySize = cSize = cStride = 0;
662 int32_t fmt = buffer->flexFormat;
664 bool usingRGBAOverride = usingRGBAToJpegOverride(fmt, containerFormat);
665 fmt = applyFormatOverrides(fmt, containerFormat);
667 case HAL_PIXEL_FORMAT_YCbCr_420_888:
674 // only map until last pixel
677 rStride = buffer->stride;
678 dataSize = buffer->stride * (buffer->height - 1) + buffer->width;
680 pStride = buffer->chromaStep;
681 rStride = buffer->chromaStride;
682 dataSize = buffer->chromaStride * (buffer->height / 2 - 1) +
683 buffer->chromaStep * (buffer->width / 2 - 1) + 1;
687 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
688 cr = buffer->data + (buffer->stride * buffer->height);
690 // only map until last pixel
691 ySize = buffer->width * (buffer->height - 1) + buffer->width;
692 cSize = buffer->width * (buffer->height / 2 - 1) + buffer->width - 1;
701 dataSize = (idx == 0) ? ySize : cSize;
702 pStride = (idx == 0) ? 1 : 2;
703 rStride = buffer->width;
705 case HAL_PIXEL_FORMAT_YV12:
706 // Y and C stride need to be 16 pixel aligned.
707 LOG_ALWAYS_FATAL_IF(buffer->stride % 16,
708 "Stride is not 16 pixel aligned %d", buffer->stride);
710 ySize = buffer->stride * buffer->height;
711 cStride = ALIGN(buffer->stride / 2, 16);
712 cr = buffer->data + ySize;
713 cSize = cStride * buffer->height / 2;
722 dataSize = (idx == 0) ? ySize : cSize;
724 rStride = (idx == 0) ? buffer->stride : ALIGN(buffer->stride / 2, 16);
726 case HAL_PIXEL_FORMAT_Y8:
727 // Single plane, 8bpp.
728 LOG_ALWAYS_FATAL_IF(idx != 0, "Wrong index: %d", idx);
730 pData = buffer->data;
731 dataSize = buffer->stride * buffer->height;
733 rStride = buffer->stride;
735 case HAL_PIXEL_FORMAT_Y16:
737 // Single plane, 16bpp, strides are specified in pixels, not in bytes
738 LOG_ALWAYS_FATAL_IF(idx != 0, "Wrong index: %d", idx);
740 pData = buffer->data;
741 dataSize = buffer->stride * buffer->height * bytesPerPixel;
742 pStride = bytesPerPixel;
743 rStride = buffer->stride * 2;
745 case HAL_PIXEL_FORMAT_BLOB:
746 // Used for JPEG data, height must be 1, width == size, single plane.
747 LOG_ALWAYS_FATAL_IF(idx != 0, "Wrong index: %d", idx);
748 // When RGBA override is being used, buffer height will be equal to width
749 if (usingRGBAOverride) {
750 LOG_ALWAYS_FATAL_IF(buffer->height != buffer->width,
751 "RGBA override BLOB format buffer should have height == width");
753 LOG_ALWAYS_FATAL_IF(buffer->height != 1,
754 "BLOB format buffer should have height value 1");
758 pData = buffer->data;
759 dataSize = Image_getJpegSize(buffer, usingRGBAOverride);
763 case HAL_PIXEL_FORMAT_RAW16:
764 // Single plane 16bpp bayer data.
766 LOG_ALWAYS_FATAL_IF(idx != 0, "Wrong index: %d", idx);
767 pData = buffer->data;
768 dataSize = buffer->stride * buffer->height * bytesPerPixel;
769 pStride = bytesPerPixel;
770 rStride = buffer->stride * 2;
772 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
773 // Used for RAW_OPAQUE data, height must be 1, width == size, single plane.
774 LOG_ALWAYS_FATAL_IF(idx != 0, "Wrong index: %d", idx);
775 LOG_ALWAYS_FATAL_IF(buffer->height != 1,
776 "RAW_PRIVATE should has height value one but got %d", buffer->height);
777 pData = buffer->data;
778 dataSize = buffer->width;
779 pStride = 0; // RAW OPAQUE doesn't have pixel stride
780 rStride = 0; // RAW OPAQUE doesn't have row stride
782 case HAL_PIXEL_FORMAT_RAW10:
783 // Single plane 10bpp bayer data.
784 LOG_ALWAYS_FATAL_IF(idx != 0, "Wrong index: %d", idx);
785 LOG_ALWAYS_FATAL_IF(buffer->width % 4,
786 "Width is not multiple of 4 %d", buffer->width);
787 LOG_ALWAYS_FATAL_IF(buffer->height % 2,
788 "Height is not even %d", buffer->height);
789 LOG_ALWAYS_FATAL_IF(buffer->stride < (buffer->width * 10 / 8),
790 "stride (%d) should be at least %d",
791 buffer->stride, buffer->width * 10 / 8);
792 pData = buffer->data;
793 dataSize = buffer->stride * buffer->height;
795 rStride = buffer->stride;
797 case HAL_PIXEL_FORMAT_RAW12:
798 // Single plane 10bpp bayer data.
799 LOG_ALWAYS_FATAL_IF(idx != 0, "Wrong index: %d", idx);
800 LOG_ALWAYS_FATAL_IF(buffer->width % 4,
801 "Width is not multiple of 4 %d", buffer->width);
802 LOG_ALWAYS_FATAL_IF(buffer->height % 2,
803 "Height is not even %d", buffer->height);
804 LOG_ALWAYS_FATAL_IF(buffer->stride < (buffer->width * 12 / 8),
805 "stride (%d) should be at least %d",
806 buffer->stride, buffer->width * 12 / 8);
807 pData = buffer->data;
808 dataSize = buffer->stride * buffer->height;
810 rStride = buffer->stride;
812 case HAL_PIXEL_FORMAT_RGBA_8888:
813 case HAL_PIXEL_FORMAT_RGBX_8888:
814 // Single plane, 32bpp.
816 LOG_ALWAYS_FATAL_IF(idx != 0, "Wrong index: %d", idx);
817 pData = buffer->data;
818 dataSize = buffer->stride * buffer->height * bytesPerPixel;
819 pStride = bytesPerPixel;
820 rStride = buffer->stride * 4;
822 case HAL_PIXEL_FORMAT_RGB_565:
823 // Single plane, 16bpp.
825 LOG_ALWAYS_FATAL_IF(idx != 0, "Wrong index: %d", idx);
826 pData = buffer->data;
827 dataSize = buffer->stride * buffer->height * bytesPerPixel;
828 pStride = bytesPerPixel;
829 rStride = buffer->stride * 2;
831 case HAL_PIXEL_FORMAT_RGB_888:
832 // Single plane, 24bpp.
834 LOG_ALWAYS_FATAL_IF(idx != 0, "Wrong index: %d", idx);
835 pData = buffer->data;
836 dataSize = buffer->stride * buffer->height * bytesPerPixel;
837 pStride = bytesPerPixel;
838 rStride = buffer->stride * 3;
846 *pixelStride = pStride;
847 *rowStride = rStride;
852 status_t lockImageFromBuffer(sp<GraphicBuffer> buffer, uint32_t inUsage,
853 const Rect& rect, int fenceFd, LockedImage* outputImage) {
854 ALOGV("%s: Try to lock the GraphicBuffer", __FUNCTION__);
856 if (buffer == nullptr || outputImage == nullptr) {
857 ALOGE("Input BufferItem or output LockedImage is NULL!");
860 if (isFormatOpaque(buffer->getPixelFormat())) {
861 ALOGE("Opaque format buffer is not lockable!");
866 android_ycbcr ycbcr = android_ycbcr();
868 int format = buffer->getPixelFormat();
869 int flexFormat = format;
870 if (isPossiblyYUV(format)) {
871 res = buffer->lockAsyncYCbCr(inUsage, rect, &ycbcr, fenceFd);
873 flexFormat = HAL_PIXEL_FORMAT_YCbCr_420_888;
876 // lockAsyncYCbCr for YUV is unsuccessful.
878 res = buffer->lockAsync(inUsage, rect, &pData, fenceFd);
880 ALOGE("Lock buffer failed!");
885 outputImage->data = reinterpret_cast<uint8_t*>(pData);
886 outputImage->width = buffer->getWidth();
887 outputImage->height = buffer->getHeight();
888 outputImage->format = format;
889 outputImage->flexFormat = flexFormat;
890 outputImage->stride =
891 (ycbcr.y != NULL) ? static_cast<uint32_t>(ycbcr.ystride) : buffer->getStride();
893 outputImage->dataCb = reinterpret_cast<uint8_t*>(ycbcr.cb);
894 outputImage->dataCr = reinterpret_cast<uint8_t*>(ycbcr.cr);
895 outputImage->chromaStride = static_cast<uint32_t>(ycbcr.cstride);
896 outputImage->chromaStep = static_cast<uint32_t>(ycbcr.chroma_step);
897 ALOGV("%s: Successfully locked the image from the GraphicBuffer", __FUNCTION__);
898 // Crop, transform, scalingMode, timestamp, and frameNumber should be set by caller,
899 // and cann't be set them here.
903 status_t lockImageFromBuffer(BufferItem* bufferItem, uint32_t inUsage,
904 int fenceFd, LockedImage* outputImage) {
905 ALOGV("%s: Try to lock the BufferItem", __FUNCTION__);
906 if (bufferItem == nullptr || outputImage == nullptr) {
907 ALOGE("Input BufferItem or output LockedImage is NULL!");
911 status_t res = lockImageFromBuffer(bufferItem->mGraphicBuffer, inUsage, bufferItem->mCrop,
912 fenceFd, outputImage);
914 ALOGE("%s: lock graphic buffer failed", __FUNCTION__);
918 outputImage->crop = bufferItem->mCrop;
919 outputImage->transform = bufferItem->mTransform;
920 outputImage->scalingMode = bufferItem->mScalingMode;
921 outputImage->timestamp = bufferItem->mTimestamp;
922 outputImage->dataSpace = bufferItem->mDataSpace;
923 outputImage->frameNumber = bufferItem->mFrameNumber;
924 ALOGV("%s: Successfully locked the image from the BufferItem", __FUNCTION__);
928 int getBufferWidth(BufferItem* buffer) {
929 if (buffer == NULL) return -1;
931 if (!buffer->mCrop.isEmpty()) {
932 return buffer->mCrop.getWidth();
935 ALOGV("%s: buffer->mGraphicBuffer: %p", __FUNCTION__, buffer->mGraphicBuffer.get());
936 return buffer->mGraphicBuffer->getWidth();
939 int getBufferHeight(BufferItem* buffer) {
940 if (buffer == NULL) return -1;
942 if (!buffer->mCrop.isEmpty()) {
943 return buffer->mCrop.getHeight();
946 ALOGV("%s: buffer->mGraphicBuffer: %p", __FUNCTION__, buffer->mGraphicBuffer.get());
947 return buffer->mGraphicBuffer->getHeight();
950 } // namespace android