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" |
| 37 | #include "SkOmxPixelRef.h" |
| 38 | #include "StreamSource.h" |
| 39 | |
| 40 | using namespace android; |
| 41 | |
Andreas Huber | 987dbde | 2009-12-16 13:13:27 -0800 | [diff] [blame] | 42 | static void getJpegOutput(MediaBuffer* buffer, const char* filename) { |
Wei-Ta Chen | 4b6f494 | 2009-09-01 17:44:49 +0800 | [diff] [blame] | 43 | int size = buffer->range_length(); |
| 44 | int offset = buffer->range_offset(); |
| 45 | FILE *pFile = fopen(filename, "w+"); |
| 46 | |
| 47 | if (pFile == NULL) { |
| 48 | printf("Error: cannot open %s.\n", filename); |
| 49 | } else { |
| 50 | char* data = (char*) buffer->data(); |
| 51 | data += offset; |
| 52 | while (size > 0) { |
| 53 | int numChars = fwrite(data, sizeof(char), 1024, pFile); |
| 54 | int numBytes = numChars * sizeof(char); |
| 55 | size -= numBytes; |
| 56 | data += numBytes; |
| 57 | } |
| 58 | fclose(pFile); |
| 59 | } |
| 60 | return; |
| 61 | } |
| 62 | |
Andreas Huber | 987dbde | 2009-12-16 13:13:27 -0800 | [diff] [blame] | 63 | extern int storeBitmapToFile(SkBitmap* bitmap, const char* filename) { |
Wei-Ta Chen | 4b6f494 | 2009-09-01 17:44:49 +0800 | [diff] [blame] | 64 | bitmap->lockPixels(); |
Andreas Huber | 987dbde | 2009-12-16 13:13:27 -0800 | [diff] [blame] | 65 | uint8_t* data = (uint8_t *)bitmap->getPixels(); |
Wei-Ta Chen | 4b6f494 | 2009-09-01 17:44:49 +0800 | [diff] [blame] | 66 | int size = bitmap->getSize(); |
| 67 | FILE* fp = fopen(filename, "w+"); |
| 68 | |
| 69 | if (NULL == fp) { |
| 70 | printf("Cannot open the output file! \n"); |
| 71 | return -1; |
| 72 | } else { |
| 73 | while (size > 0) { |
| 74 | int numChars = fwrite(data, sizeof(char), 1024, fp); |
| 75 | int numBytes = numChars * sizeof(char); |
| 76 | size -= numBytes; |
| 77 | data += numBytes; |
| 78 | } |
| 79 | fclose(fp); |
| 80 | } |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | static int64_t getNowUs() { |
| 85 | struct timeval tv; |
| 86 | gettimeofday(&tv, NULL); |
| 87 | |
| 88 | return (int64_t)tv.tv_usec + tv.tv_sec * 1000000; |
| 89 | } |
| 90 | |
| 91 | OmxJpegImageDecoder::OmxJpegImageDecoder() { |
| 92 | status_t err = mClient.connect(); |
James Dong | 8e9d67a | 2012-02-06 23:46:37 -0800 | [diff] [blame] | 93 | CHECK_EQ(err, (status_t)OK); |
Wei-Ta Chen | 4b6f494 | 2009-09-01 17:44:49 +0800 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | OmxJpegImageDecoder::~OmxJpegImageDecoder() { |
| 97 | mClient.disconnect(); |
| 98 | } |
| 99 | |
| 100 | bool OmxJpegImageDecoder::onDecode(SkStream* stream, |
Mike Reed | 945a9df | 2010-03-03 13:24:37 -0500 | [diff] [blame] | 101 | SkBitmap* bm, Mode mode) { |
Wei-Ta Chen | 4b6f494 | 2009-09-01 17:44:49 +0800 | [diff] [blame] | 102 | sp<MediaSource> source = prepareMediaSource(stream); |
| 103 | sp<MetaData> meta = source->getFormat(); |
| 104 | int width; |
| 105 | int height; |
| 106 | meta->findInt32(kKeyWidth, &width); |
| 107 | meta->findInt32(kKeyHeight, &height); |
Mike Reed | 945a9df | 2010-03-03 13:24:37 -0500 | [diff] [blame] | 108 | configBitmapSize(bm, getPrefConfig(k32Bit_SrcDepth, false), width, height); |
Wei-Ta Chen | 4b6f494 | 2009-09-01 17:44:49 +0800 | [diff] [blame] | 109 | |
| 110 | // mode == DecodeBounds |
| 111 | if (mode == SkImageDecoder::kDecodeBounds_Mode) { |
| 112 | return true; |
| 113 | } |
| 114 | |
| 115 | // mode == DecodePixels |
| 116 | if (!this->allocPixelRef(bm, NULL)) { |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 117 | ALOGI("Cannot allocPixelRef()!"); |
Wei-Ta Chen | 4b6f494 | 2009-09-01 17:44:49 +0800 | [diff] [blame] | 118 | return false; |
| 119 | } |
| 120 | |
Andreas Huber | 987dbde | 2009-12-16 13:13:27 -0800 | [diff] [blame] | 121 | sp<MediaSource> decoder = getDecoder(&mClient, source); |
Wei-Ta Chen | 4b6f494 | 2009-09-01 17:44:49 +0800 | [diff] [blame] | 122 | return decodeSource(decoder, source, bm); |
| 123 | } |
| 124 | |
| 125 | JPEGSource* OmxJpegImageDecoder::prepareMediaSource(SkStream* stream) { |
| 126 | DataSource::RegisterDefaultSniffers(); |
| 127 | sp<DataSource> dataSource = new StreamSource(stream); |
| 128 | return new JPEGSource(dataSource); |
| 129 | } |
| 130 | |
Andreas Huber | 987dbde | 2009-12-16 13:13:27 -0800 | [diff] [blame] | 131 | sp<MediaSource> OmxJpegImageDecoder::getDecoder( |
Wei-Ta Chen | 4b6f494 | 2009-09-01 17:44:49 +0800 | [diff] [blame] | 132 | OMXClient *client, const sp<MediaSource>& source) { |
| 133 | sp<MetaData> meta = source->getFormat(); |
Andreas Huber | 987dbde | 2009-12-16 13:13:27 -0800 | [diff] [blame] | 134 | sp<MediaSource> decoder = OMXCodec::Create( |
Wei-Ta Chen | 4b6f494 | 2009-09-01 17:44:49 +0800 | [diff] [blame] | 135 | client->interface(), meta, false /* createEncoder */, source); |
| 136 | |
| 137 | CHECK(decoder != NULL); |
| 138 | return decoder; |
| 139 | } |
| 140 | |
Andreas Huber | 987dbde | 2009-12-16 13:13:27 -0800 | [diff] [blame] | 141 | bool OmxJpegImageDecoder::decodeSource(sp<MediaSource> decoder, |
Wei-Ta Chen | 4b6f494 | 2009-09-01 17:44:49 +0800 | [diff] [blame] | 142 | const sp<MediaSource>& source, SkBitmap* bm) { |
| 143 | status_t rt = decoder->start(); |
| 144 | if (rt != OK) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 145 | ALOGE("Cannot start OMX Decoder!"); |
Wei-Ta Chen | 4b6f494 | 2009-09-01 17:44:49 +0800 | [diff] [blame] | 146 | return false; |
| 147 | } |
| 148 | int64_t startTime = getNowUs(); |
| 149 | MediaBuffer *buffer; |
| 150 | |
| 151 | // decode source |
| 152 | status_t err = decoder->read(&buffer, NULL); |
| 153 | int64_t duration = getNowUs() - startTime; |
| 154 | |
| 155 | if (err != OK) { |
James Dong | 8e9d67a | 2012-02-06 23:46:37 -0800 | [diff] [blame] | 156 | CHECK(buffer == NULL); |
Wei-Ta Chen | 4b6f494 | 2009-09-01 17:44:49 +0800 | [diff] [blame] | 157 | } |
| 158 | printf("Duration in decoder->read(): %.1f (msecs). \n", |
| 159 | duration / 1E3 ); |
| 160 | |
| 161 | /* Mark the code for now, since we attend to copy buffer to SkBitmap. |
| 162 | // Install pixelRef to Bitmap. |
| 163 | installPixelRef(buffer, decoder, bm);*/ |
| 164 | |
| 165 | // Copy pixels from buffer to bm. |
| 166 | // May need to check buffer->rawBytes() == bm->rawBytes(). |
| 167 | CHECK_EQ(buffer->size(), bm->getSize()); |
| 168 | memcpy(bm->getPixels(), buffer->data(), buffer->size()); |
| 169 | buffer->release(); |
| 170 | decoder->stop(); |
| 171 | |
| 172 | return true; |
| 173 | } |
| 174 | |
Andreas Huber | 987dbde | 2009-12-16 13:13:27 -0800 | [diff] [blame] | 175 | void OmxJpegImageDecoder::installPixelRef(MediaBuffer *buffer, sp<MediaSource> decoder, |
Wei-Ta Chen | 4b6f494 | 2009-09-01 17:44:49 +0800 | [diff] [blame] | 176 | SkBitmap* bm) { |
| 177 | |
| 178 | // set bm's pixelref based on the data in buffer. |
| 179 | SkAutoLockPixels alp(*bm); |
| 180 | SkPixelRef* pr = new SkOmxPixelRef(NULL, buffer, decoder); |
| 181 | bm->setPixelRef(pr)->unref(); |
| 182 | bm->lockPixels(); |
| 183 | return; |
| 184 | } |
| 185 | |
| 186 | void OmxJpegImageDecoder::configBitmapSize(SkBitmap* bm, SkBitmap::Config pref, |
| 187 | int width, int height) { |
Leon Scroggins III | 8790be6 | 2013-12-03 16:26:51 -0500 | [diff] [blame] | 188 | bm->setConfig(getColorSpaceConfig(pref), width, height, 0, kOpaque_SkAlphaType); |
Wei-Ta Chen | 4b6f494 | 2009-09-01 17:44:49 +0800 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | SkBitmap::Config OmxJpegImageDecoder::getColorSpaceConfig( |
| 192 | SkBitmap::Config pref) { |
| 193 | |
| 194 | // Set the color space to ARGB_8888 for now |
| 195 | // because of limitation in hardware support. |
| 196 | return SkBitmap::kARGB_8888_Config; |
| 197 | } |