Wei-Ta Chen | 4b6f494 | 2009-09-01 17:44:49 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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 | #define LOG_TAG "OmxJpegDecoder" |
| 18 | #include <sys/time.h> |
| 19 | #include <utils/Log.h> |
| 20 | |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | #include <unistd.h> |
| 24 | |
| 25 | #include <binder/IServiceManager.h> |
| 26 | #include <binder/ProcessState.h> |
| 27 | #include <media/IMediaPlayerService.h> |
James Dong | 8e9d67a | 2012-02-06 23:46:37 -0800 | [diff] [blame] | 28 | #include <media/stagefright/foundation/ADebug.h> |
Wei-Ta Chen | 4b6f494 | 2009-09-01 17:44:49 +0800 | [diff] [blame] | 29 | #include <media/stagefright/MediaSource.h> |
| 30 | #include <media/stagefright/MetaData.h> |
| 31 | #include <media/stagefright/OMXClient.h> |
| 32 | #include <media/stagefright/OMXCodec.h> |
Leon Scroggins III | 8790be6 | 2013-12-03 16:26:51 -0500 | [diff] [blame] | 33 | #include <SkImage.h> |
Wei-Ta Chen | 4b6f494 | 2009-09-01 17:44:49 +0800 | [diff] [blame] | 34 | #include <SkMallocPixelRef.h> |
| 35 | |
| 36 | #include "omx_jpeg_decoder.h" |
Wei-Ta Chen | 4b6f494 | 2009-09-01 17:44:49 +0800 | [diff] [blame] | 37 | #include "StreamSource.h" |
| 38 | |
| 39 | using namespace android; |
| 40 | |
Andreas Huber | 987dbde | 2009-12-16 13:13:27 -0800 | [diff] [blame] | 41 | static void getJpegOutput(MediaBuffer* buffer, const char* filename) { |
Wei-Ta Chen | 4b6f494 | 2009-09-01 17:44:49 +0800 | [diff] [blame] | 42 | int size = buffer->range_length(); |
| 43 | int offset = buffer->range_offset(); |
| 44 | FILE *pFile = fopen(filename, "w+"); |
| 45 | |
| 46 | if (pFile == NULL) { |
| 47 | printf("Error: cannot open %s.\n", filename); |
| 48 | } else { |
| 49 | char* data = (char*) buffer->data(); |
| 50 | data += offset; |
| 51 | while (size > 0) { |
| 52 | int numChars = fwrite(data, sizeof(char), 1024, pFile); |
| 53 | int numBytes = numChars * sizeof(char); |
| 54 | size -= numBytes; |
| 55 | data += numBytes; |
| 56 | } |
| 57 | fclose(pFile); |
| 58 | } |
| 59 | return; |
| 60 | } |
| 61 | |
Andreas Huber | 987dbde | 2009-12-16 13:13:27 -0800 | [diff] [blame] | 62 | extern int storeBitmapToFile(SkBitmap* bitmap, const char* filename) { |
Wei-Ta Chen | 4b6f494 | 2009-09-01 17:44:49 +0800 | [diff] [blame] | 63 | bitmap->lockPixels(); |
Andreas Huber | 987dbde | 2009-12-16 13:13:27 -0800 | [diff] [blame] | 64 | uint8_t* data = (uint8_t *)bitmap->getPixels(); |
Wei-Ta Chen | 4b6f494 | 2009-09-01 17:44:49 +0800 | [diff] [blame] | 65 | int size = bitmap->getSize(); |
| 66 | FILE* fp = fopen(filename, "w+"); |
| 67 | |
| 68 | if (NULL == fp) { |
| 69 | printf("Cannot open the output file! \n"); |
| 70 | return -1; |
| 71 | } else { |
| 72 | while (size > 0) { |
| 73 | int numChars = fwrite(data, sizeof(char), 1024, fp); |
| 74 | int numBytes = numChars * sizeof(char); |
| 75 | size -= numBytes; |
| 76 | data += numBytes; |
| 77 | } |
| 78 | fclose(fp); |
| 79 | } |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | static int64_t getNowUs() { |
| 84 | struct timeval tv; |
| 85 | gettimeofday(&tv, NULL); |
| 86 | |
| 87 | return (int64_t)tv.tv_usec + tv.tv_sec * 1000000; |
| 88 | } |
| 89 | |
| 90 | OmxJpegImageDecoder::OmxJpegImageDecoder() { |
| 91 | status_t err = mClient.connect(); |
James Dong | 8e9d67a | 2012-02-06 23:46:37 -0800 | [diff] [blame] | 92 | CHECK_EQ(err, (status_t)OK); |
Wei-Ta Chen | 4b6f494 | 2009-09-01 17:44:49 +0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | OmxJpegImageDecoder::~OmxJpegImageDecoder() { |
| 96 | mClient.disconnect(); |
| 97 | } |
| 98 | |
| 99 | bool OmxJpegImageDecoder::onDecode(SkStream* stream, |
Mike Reed | 945a9df | 2010-03-03 13:24:37 -0500 | [diff] [blame] | 100 | SkBitmap* bm, Mode mode) { |
Wei-Ta Chen | 4b6f494 | 2009-09-01 17:44:49 +0800 | [diff] [blame] | 101 | sp<MediaSource> source = prepareMediaSource(stream); |
| 102 | sp<MetaData> meta = source->getFormat(); |
| 103 | int width; |
| 104 | int height; |
| 105 | meta->findInt32(kKeyWidth, &width); |
| 106 | meta->findInt32(kKeyHeight, &height); |
Ji-Hwan Lee | 162d699 | 2014-06-13 20:10:33 +0900 | [diff] [blame] | 107 | configBitmapSize( |
Ying Wang | 7c0e621 | 2014-06-20 17:30:45 -0700 | [diff] [blame] | 108 | bm, getPrefColorType(k32Bit_SrcDepth, false), |
Ji-Hwan Lee | 162d699 | 2014-06-13 20:10:33 +0900 | [diff] [blame] | 109 | width, height); |
Wei-Ta Chen | 4b6f494 | 2009-09-01 17:44:49 +0800 | [diff] [blame] | 110 | |
| 111 | // mode == DecodeBounds |
| 112 | if (mode == SkImageDecoder::kDecodeBounds_Mode) { |
| 113 | return true; |
| 114 | } |
| 115 | |
| 116 | // mode == DecodePixels |
| 117 | if (!this->allocPixelRef(bm, NULL)) { |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 118 | ALOGI("Cannot allocPixelRef()!"); |
Wei-Ta Chen | 4b6f494 | 2009-09-01 17:44:49 +0800 | [diff] [blame] | 119 | return false; |
| 120 | } |
| 121 | |
Andreas Huber | 987dbde | 2009-12-16 13:13:27 -0800 | [diff] [blame] | 122 | sp<MediaSource> decoder = getDecoder(&mClient, source); |
Wei-Ta Chen | 4b6f494 | 2009-09-01 17:44:49 +0800 | [diff] [blame] | 123 | return decodeSource(decoder, source, bm); |
| 124 | } |
| 125 | |
| 126 | JPEGSource* OmxJpegImageDecoder::prepareMediaSource(SkStream* stream) { |
| 127 | DataSource::RegisterDefaultSniffers(); |
| 128 | sp<DataSource> dataSource = new StreamSource(stream); |
| 129 | return new JPEGSource(dataSource); |
| 130 | } |
| 131 | |
Andreas Huber | 987dbde | 2009-12-16 13:13:27 -0800 | [diff] [blame] | 132 | sp<MediaSource> OmxJpegImageDecoder::getDecoder( |
Wei-Ta Chen | 4b6f494 | 2009-09-01 17:44:49 +0800 | [diff] [blame] | 133 | OMXClient *client, const sp<MediaSource>& source) { |
| 134 | sp<MetaData> meta = source->getFormat(); |
Andreas Huber | 987dbde | 2009-12-16 13:13:27 -0800 | [diff] [blame] | 135 | sp<MediaSource> decoder = OMXCodec::Create( |
Wei-Ta Chen | 4b6f494 | 2009-09-01 17:44:49 +0800 | [diff] [blame] | 136 | client->interface(), meta, false /* createEncoder */, source); |
| 137 | |
| 138 | CHECK(decoder != NULL); |
| 139 | return decoder; |
| 140 | } |
| 141 | |
Andreas Huber | 987dbde | 2009-12-16 13:13:27 -0800 | [diff] [blame] | 142 | bool OmxJpegImageDecoder::decodeSource(sp<MediaSource> decoder, |
Wei-Ta Chen | 4b6f494 | 2009-09-01 17:44:49 +0800 | [diff] [blame] | 143 | const sp<MediaSource>& source, SkBitmap* bm) { |
| 144 | status_t rt = decoder->start(); |
| 145 | if (rt != OK) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 146 | ALOGE("Cannot start OMX Decoder!"); |
Wei-Ta Chen | 4b6f494 | 2009-09-01 17:44:49 +0800 | [diff] [blame] | 147 | return false; |
| 148 | } |
| 149 | int64_t startTime = getNowUs(); |
| 150 | MediaBuffer *buffer; |
| 151 | |
| 152 | // decode source |
| 153 | status_t err = decoder->read(&buffer, NULL); |
| 154 | int64_t duration = getNowUs() - startTime; |
| 155 | |
| 156 | if (err != OK) { |
James Dong | 8e9d67a | 2012-02-06 23:46:37 -0800 | [diff] [blame] | 157 | CHECK(buffer == NULL); |
Wei-Ta Chen | 4b6f494 | 2009-09-01 17:44:49 +0800 | [diff] [blame] | 158 | } |
| 159 | printf("Duration in decoder->read(): %.1f (msecs). \n", |
| 160 | duration / 1E3 ); |
| 161 | |
Wei-Ta Chen | 4b6f494 | 2009-09-01 17:44:49 +0800 | [diff] [blame] | 162 | // Copy pixels from buffer to bm. |
| 163 | // May need to check buffer->rawBytes() == bm->rawBytes(). |
| 164 | CHECK_EQ(buffer->size(), bm->getSize()); |
| 165 | memcpy(bm->getPixels(), buffer->data(), buffer->size()); |
| 166 | buffer->release(); |
| 167 | decoder->stop(); |
| 168 | |
| 169 | return true; |
| 170 | } |
| 171 | |
Mike Reed | b933055 | 2014-06-16 17:31:48 -0400 | [diff] [blame] | 172 | void OmxJpegImageDecoder::configBitmapSize(SkBitmap* bm, SkColorType pref, |
Wei-Ta Chen | 4b6f494 | 2009-09-01 17:44:49 +0800 | [diff] [blame] | 173 | int width, int height) { |
Mike Reed | b933055 | 2014-06-16 17:31:48 -0400 | [diff] [blame] | 174 | // Set the color space to ARGB_8888 for now (ignoring pref) |
Wei-Ta Chen | 4b6f494 | 2009-09-01 17:44:49 +0800 | [diff] [blame] | 175 | // because of limitation in hardware support. |
Ying Wang | 7c0e621 | 2014-06-20 17:30:45 -0700 | [diff] [blame] | 176 | bm->setInfo(SkImageInfo::MakeN32(width, height, kOpaque_SkAlphaType)); |
Wei-Ta Chen | 4b6f494 | 2009-09-01 17:44:49 +0800 | [diff] [blame] | 177 | } |