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> 28 #include <media/stagefright/foundation/ADebug.h> 29 #include <media/stagefright/MediaSource.h> 30 #include <media/stagefright/MetaData.h> 31 #include <media/stagefright/OMXClient.h> 32 #include <media/stagefright/OMXCodec.h> 33 #include <SkMallocPixelRef.h> 34 35 #include "omx_jpeg_decoder.h" 36 #include "SkOmxPixelRef.h" 37 #include "StreamSource.h" 38 39 using namespace android; 40 41 static void getJpegOutput(MediaBuffer* buffer, const char* filename) { 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 62 extern int storeBitmapToFile(SkBitmap* bitmap, const char* filename) { 63 bitmap->lockPixels(); 64 uint8_t* data = (uint8_t *)bitmap->getPixels(); 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(); 92 CHECK_EQ(err, (status_t)OK); 93 } 94 95 OmxJpegImageDecoder::~OmxJpegImageDecoder() { 96 mClient.disconnect(); 97 } 98 99 bool OmxJpegImageDecoder::onDecode(SkStream* stream, 100 SkBitmap* bm, Mode mode) { 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); 107 configBitmapSize(bm, getPrefConfig(k32Bit_SrcDepth, false), width, height); 108 109 // mode == DecodeBounds 110 if (mode == SkImageDecoder::kDecodeBounds_Mode) { 111 return true; 112 } 113 114 // mode == DecodePixels 115 if (!this->allocPixelRef(bm, NULL)) { 116 ALOGI("Cannot allocPixelRef()!"); 117 return false; 118 } 119 120 sp<MediaSource> decoder = getDecoder(&mClient, source); 121 return decodeSource(decoder, source, bm); 122 } 123 124 JPEGSource* OmxJpegImageDecoder::prepareMediaSource(SkStream* stream) { 125 DataSource::RegisterDefaultSniffers(); 126 sp<DataSource> dataSource = new StreamSource(stream); 127 return new JPEGSource(dataSource); 128 } 129 130 sp<MediaSource> OmxJpegImageDecoder::getDecoder( 131 OMXClient *client, const sp<MediaSource>& source) { 132 sp<MetaData> meta = source->getFormat(); 133 sp<MediaSource> decoder = OMXCodec::Create( 134 client->interface(), meta, false /* createEncoder */, source); 135 136 CHECK(decoder != NULL); 137 return decoder; 138 } 139 140 bool OmxJpegImageDecoder::decodeSource(sp<MediaSource> decoder, 141 const sp<MediaSource>& source, SkBitmap* bm) { 142 status_t rt = decoder->start(); 143 if (rt != OK) { 144 ALOGE("Cannot start OMX Decoder!"); 145 return false; 146 } 147 int64_t startTime = getNowUs(); 148 MediaBuffer *buffer; 149 150 // decode source 151 status_t err = decoder->read(&buffer, NULL); 152 int64_t duration = getNowUs() - startTime; 153 154 if (err != OK) { 155 CHECK(buffer == NULL); 156 } 157 printf("Duration in decoder->read(): %.1f (msecs). \n", 158 duration / 1E3 ); 159 160 /* Mark the code for now, since we attend to copy buffer to SkBitmap. 161 // Install pixelRef to Bitmap. 162 installPixelRef(buffer, decoder, bm);*/ 163 164 // Copy pixels from buffer to bm. 165 // May need to check buffer->rawBytes() == bm->rawBytes(). 166 CHECK_EQ(buffer->size(), bm->getSize()); 167 memcpy(bm->getPixels(), buffer->data(), buffer->size()); 168 buffer->release(); 169 decoder->stop(); 170 171 return true; 172 } 173 174 void OmxJpegImageDecoder::installPixelRef(MediaBuffer *buffer, sp<MediaSource> decoder, 175 SkBitmap* bm) { 176 177 // set bm's pixelref based on the data in buffer. 178 SkAutoLockPixels alp(*bm); 179 SkPixelRef* pr = new SkOmxPixelRef(NULL, buffer, decoder); 180 bm->setPixelRef(pr)->unref(); 181 bm->lockPixels(); 182 return; 183 } 184 185 void OmxJpegImageDecoder::configBitmapSize(SkBitmap* bm, SkBitmap::Config pref, 186 int width, int height) { 187 bm->setConfig(getColorSpaceConfig(pref), width, height); 188 bm->setIsOpaque(true); 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 } 198