Home | History | Annotate | Download | only in libs3cjpeg
      1 /*
      2  * Copyright Samsung Electronics Co.,LTD.
      3  * Copyright (C) 2010 The Android Open Source Project
      4  *
      5  * Licensed under the Apache License, Version 2.0 (the "License");
      6  * you may not use this file except in compliance with the License.
      7  * You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  *
     17  * JPEG DRIVER MODULE (JpegEncoder.h)
     18  * Author  : ge.lee       -- initial version
     19  * Date    : 03 June 2010
     20  * Purpose : This file implements the JPEG encoder APIs as needed by Camera HAL
     21  */
     22 #ifndef __JPG_API_H__
     23 #define __JPG_API_H__
     24 
     25 #include <stdint.h>
     26 #include <sys/ioctl.h>
     27 
     28 #include "Exif.h"
     29 
     30 namespace android {
     31 #define MAX_JPG_WIDTH                   800
     32 #define MAX_JPG_HEIGHT                  480
     33 #define MAX_JPG_RESOLUTION              (MAX_JPG_WIDTH * MAX_JPG_HEIGHT)
     34 
     35 #define MAX_JPG_THUMBNAIL_WIDTH         320
     36 #define MAX_JPG_THUMBNAIL_HEIGHT        240
     37 #define MAX_JPG_THUMBNAIL_RESOLUTION    (MAX_JPG_THUMBNAIL_WIDTH *  \
     38                                             MAX_JPG_THUMBNAIL_HEIGHT)
     39 
     40 #define MAX_RGB_WIDTH                   800
     41 #define MAX_RGB_HEIGHT                  480
     42 #define MAX_RGB_RESOLUTION              (MAX_RGB_WIDTH * MAX_RGB_HEIGHT)
     43 
     44 /*******************************************************************************/
     45 /* define JPG & image memory */
     46 /* memory area is 4k(PAGE_SIZE) aligned because of VirtualCopyEx() */
     47 #define JPG_STREAM_BUF_SIZE     \
     48         (MAX_JPG_RESOLUTION / PAGE_SIZE + 1) * PAGE_SIZE
     49 #define JPG_STREAM_THUMB_BUF_SIZE   \
     50         (MAX_JPG_THUMBNAIL_RESOLUTION / PAGE_SIZE + 1) * PAGE_SIZE
     51 #define JPG_FRAME_BUF_SIZE  \
     52         ((MAX_JPG_RESOLUTION * 3) / PAGE_SIZE + 1) * PAGE_SIZE
     53 #define JPG_FRAME_THUMB_BUF_SIZE    \
     54         ((MAX_JPG_THUMBNAIL_RESOLUTION * 3) / PAGE_SIZE + 1) * PAGE_SIZE
     55 #define JPG_RGB_BUF_SIZE    \
     56         ((MAX_RGB_RESOLUTION * 4) / PAGE_SIZE + 1) * PAGE_SIZE
     57 
     58 #define JPG_TOTAL_BUF_SIZE  (JPG_STREAM_BUF_SIZE + \
     59                              JPG_STREAM_THUMB_BUF_SIZE + \
     60                              JPG_FRAME_BUF_SIZE + \
     61                              JPG_FRAME_THUMB_BUF_SIZE + \
     62                              JPG_RGB_BUF_SIZE)
     63 
     64 #define JPG_MAIN_START      0x00
     65 #define JPG_THUMB_START     JPG_STREAM_BUF_SIZE
     66 #define IMG_MAIN_START      (JPG_STREAM_BUF_SIZE + JPG_STREAM_THUMB_BUF_SIZE)
     67 #define IMG_THUMB_START     (IMG_MAIN_START + JPG_FRAME_BUF_SIZE)
     68 /*******************************************************************************/
     69 
     70 #define JPG_DRIVER_NAME     "/dev/s3c-jpg"
     71 
     72 #define JPEG_IOCTL_MAGIC                'J'
     73 #define IOCTL_JPG_DECODE                _IO(JPEG_IOCTL_MAGIC, 1)
     74 #define IOCTL_JPG_ENCODE                _IO(JPEG_IOCTL_MAGIC, 2)
     75 #define IOCTL_JPG_GET_STRBUF            _IO(JPEG_IOCTL_MAGIC, 3)
     76 #define IOCTL_JPG_GET_FRMBUF            _IO(JPEG_IOCTL_MAGIC, 4)
     77 #define IOCTL_JPG_GET_THUMB_STRBUF      _IO(JPEG_IOCTL_MAGIC, 5)
     78 #define IOCTL_JPG_GET_THUMB_FRMBUF      _IO(JPEG_IOCTL_MAGIC, 6)
     79 #define IOCTL_JPG_GET_PHY_FRMBUF        _IO(JPEG_IOCTL_MAGIC, 7)
     80 #define IOCTL_JPG_GET_PHY_THUMB_FRMBUF  _IO(JPEG_IOCTL_MAGIC, 8)
     81 
     82 typedef enum {
     83     JPEG_SET_ENCODE_WIDTH,
     84     JPEG_SET_ENCODE_HEIGHT,
     85     JPEG_SET_ENCODE_QUALITY,
     86     JPEG_SET_ENCODE_IN_FORMAT,
     87     JPEG_SET_SAMPING_MODE,
     88     JPEG_SET_THUMBNAIL_WIDTH,
     89     JPEG_SET_THUMBNAIL_HEIGHT
     90 } jpeg_conf;
     91 
     92 typedef enum {
     93     JPG_FAIL,
     94     JPG_SUCCESS,
     95     OK_HD_PARSING,
     96     ERR_HD_PARSING,
     97     OK_ENC_OR_DEC,
     98     ERR_ENC_OR_DEC,
     99     ERR_UNKNOWN
    100 } jpg_return_status;
    101 
    102 typedef enum {
    103     JPG_RGB16,
    104     JPG_YCBYCR,
    105     JPG_TYPE_UNKNOWN
    106 } image_type_t;
    107 
    108 typedef enum {
    109     JPG_444,
    110     JPG_422,
    111     JPG_420,
    112     JPG_400,
    113     RESERVED1,
    114     RESERVED2,
    115     JPG_411,
    116     JPG_SAMPLE_UNKNOWN
    117 } sample_mode_t;
    118 
    119 typedef enum {
    120     YCBCR_422,
    121     YCBCR_420,
    122     YCBCR_SAMPLE_UNKNOWN
    123 } out_mode_t;
    124 
    125 typedef enum {
    126     JPG_MODESEL_YCBCR = 1,
    127     JPG_MODESEL_RGB,
    128     JPG_MODESEL_UNKNOWN
    129 } in_mode_t;
    130 
    131 typedef enum {
    132     JPG_MAIN,
    133     JPG_THUMBNAIL
    134 } encode_type_t;
    135 
    136 typedef enum {
    137     JPG_QUALITY_LEVEL_1,        /* high */
    138     JPG_QUALITY_LEVEL_2,
    139     JPG_QUALITY_LEVEL_3,
    140     JPG_QUALITY_LEVEL_4         /* low */
    141 } image_quality_type_t;
    142 
    143 typedef struct {
    144     sample_mode_t   sample_mode;
    145     encode_type_t   dec_type;
    146     out_mode_t      out_format;
    147     uint32_t        width;
    148     uint32_t        height;
    149     uint32_t        data_size;
    150     uint32_t        file_size;
    151 } jpg_dec_proc_param;
    152 
    153 typedef struct {
    154     sample_mode_t       sample_mode;
    155     encode_type_t       enc_type;
    156     in_mode_t           in_format;
    157     image_quality_type_t quality;
    158     uint32_t            width;
    159     uint32_t            height;
    160     uint32_t            data_size;
    161     uint32_t            file_size;
    162     uint32_t            set_framebuf;
    163 } jpg_enc_proc_param;
    164 
    165 typedef struct {
    166     char    *in_buf;
    167     char    *phy_in_buf;
    168     int     in_buf_size;
    169     char    *out_buf;
    170     char    *phy_out_buf;
    171     int     out_buf_size;
    172     char    *in_thumb_buf;
    173     char    *phy_in_thumb_buf;
    174     int     in_thumb_buf_size;
    175     char    *out_thumb_buf;
    176     char    *phy_out_thumb_buf;
    177     int     out_thumb_buf_size;
    178     char    *mmapped_addr;
    179     jpg_dec_proc_param  *dec_param;
    180     jpg_enc_proc_param  *enc_param;
    181     jpg_enc_proc_param  *thumb_enc_param;
    182 } jpg_args;
    183 
    184 class JpegEncoder {
    185 public:
    186     JpegEncoder();
    187     virtual ~JpegEncoder();
    188 
    189     int openHardware();
    190     jpg_return_status setConfig(jpeg_conf type, int32_t value);
    191     void *getInBuf(uint64_t size);
    192     void *getOutBuf(uint64_t *size);
    193     void *getThumbInBuf(uint64_t size);
    194     void *getThumbOutBuf(uint64_t *size);
    195     jpg_return_status encode(unsigned int *size, exif_attribute_t *exifInfo);
    196     jpg_return_status encodeThumbImg(unsigned int *size, bool useMain = true);
    197     jpg_return_status makeExif(unsigned char *exifOut,
    198                                exif_attribute_t *exifIn,
    199                                unsigned int *size,
    200                                bool useMainbufForThumb = false);
    201 
    202 private:
    203     jpg_return_status checkMcu(sample_mode_t sampleMode, uint32_t width, uint32_t height, bool isThumb);
    204     bool pad(char *srcBuf, uint32_t srcWidth, uint32_t srcHight,
    205              char *dstBuf, uint32_t dstWidth, uint32_t dstHight);
    206     bool scaleDownYuv422(char *srcBuf, uint32_t srcWidth, uint32_t srcHight,
    207                          char *dstBuf, uint32_t dstWidth, uint32_t dstHight);
    208 
    209     inline void writeExifIfd(unsigned char **pCur,
    210                                  unsigned short tag,
    211                                  unsigned short type,
    212                                  unsigned int count,
    213                                  uint32_t value);
    214     inline void writeExifIfd(unsigned char **pCur,
    215                                  unsigned short tag,
    216                                  unsigned short type,
    217                                  unsigned int count,
    218                                  unsigned char *pValue);
    219     inline void writeExifIfd(unsigned char **pCur,
    220                                  unsigned short tag,
    221                                  unsigned short type,
    222                                  unsigned int count,
    223                                  rational_t *pValue,
    224                                  unsigned int *offset,
    225                                  unsigned char *start);
    226     inline void writeExifIfd(unsigned char **pCur,
    227                                  unsigned short tag,
    228                                  unsigned short type,
    229                                  unsigned int count,
    230                                  unsigned char *pValue,
    231                                  unsigned int *offset,
    232                                  unsigned char *start);
    233     int mDevFd;
    234     jpg_args mArgs;
    235 
    236     bool available;
    237 
    238 };
    239 };
    240 #endif /* __JPG_API_H__ */
    241