Home | History | Annotate | Download | only in omxjpegdecoder
      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 <binder/ProcessState.h>
     22 
     23 #include "SkBitmap.h"
     24 #include "SkImageDecoder.h"
     25 #include "SkStream.h"
     26 #include "omx_jpeg_decoder.h"
     27 
     28 class SkJPEGImageDecoder : public SkImageDecoder {
     29 public:
     30     virtual Format getFormat() const {
     31         return kJPEG_Format;
     32     }
     33 
     34 protected:
     35     virtual bool onDecode(SkStream* stream, SkBitmap* bm, Mode);
     36 };
     37 
     38 int nullObjectReturn(const char msg[]) {
     39     if (msg) {
     40         SkDebugf("--- %s\n", msg);
     41     }
     42     return -1;
     43 }
     44 
     45 static int64_t getNowUs() {
     46     struct timeval tv;
     47     gettimeofday(&tv, NULL);
     48 
     49     return tv.tv_usec + (int64_t) tv.tv_sec * 1000000;
     50 }
     51 
     52 int testDecodeBounds(SkImageDecoder* decoder, SkStream* stream,
     53         SkBitmap* bitmap) {
     54     int64_t startTime = getNowUs();
     55     SkBitmap::Config prefConfig = SkBitmap::kARGB_8888_Config;
     56     SkImageDecoder::Mode decodeMode = SkImageDecoder::kDecodeBounds_Mode;
     57 
     58     // Decode the input stream and then use the bitmap.
     59     if (!decoder->decode(stream, bitmap, prefConfig, decodeMode)) {
     60         return nullObjectReturn("decoder->decode returned false");
     61     } else {
     62         int64_t delay = getNowUs() - startTime;
     63         printf("WidthxHeight: %dx%d\n", bitmap->width(), bitmap->height());
     64         printf("Decoding Time in BoundsMode %.1f msec.\n", delay / 1000.0f);
     65         return 0;
     66     }
     67 }
     68 
     69 int testDecodePixels(SkImageDecoder* decoder, SkStream* stream,
     70         SkBitmap* bitmap) {
     71     int64_t startTime = getNowUs();
     72     SkBitmap::Config prefConfig = SkBitmap::kARGB_8888_Config;
     73     SkImageDecoder::Mode decodeMode = SkImageDecoder::kDecodePixels_Mode;
     74 
     75     // Decode the input stream and then use the bitmap.
     76     if (!decoder->decode(stream, bitmap, prefConfig, decodeMode)) {
     77         return nullObjectReturn("decoder->decode returned false");
     78     } else {
     79         int64_t delay = getNowUs() - startTime;
     80         printf("Decoding Time in PixelsMode %.1f msec.\n", delay / 1000.0f);
     81         const char* filename = "/sdcard/omxJpegDecodedBitmap.rgba";
     82         return storeBitmapToFile(bitmap, filename);
     83     }
     84 }
     85 
     86 int testDecoder(SkImageDecoder* decoder, char* filename) {
     87     // test DecodeMode == Pixels
     88     SkStream* stream = new SkFILEStream(filename);
     89     SkBitmap* bitmap = new SkBitmap;
     90     testDecodePixels(decoder, stream, bitmap);
     91     delete bitmap;
     92 
     93     // test DecodeMode == Bounds
     94     stream = new SkFILEStream(filename);
     95     bitmap = new SkBitmap;
     96     testDecodeBounds(decoder, stream, bitmap);
     97     delete bitmap;
     98 
     99     delete decoder;
    100     return 0;
    101 }
    102 
    103 int main(int argc, char** argv) {
    104     android::ProcessState::self()->startThreadPool();
    105 
    106     printf("Decoding jpeg with libjpeg...\n");
    107     SkJPEGImageDecoder* libjpeg = new SkJPEGImageDecoder;
    108     testDecoder(libjpeg, argv[1]);
    109 
    110     printf("\nDecoding jpeg with OMX...\n");
    111     OmxJpegImageDecoder* omx = new OmxJpegImageDecoder;
    112     testDecoder(omx, argv[1]);
    113     return 0;
    114 }
    115