1 /*
2  * Copyright Samsung Electronics Co.,LTD.
3  * Copyright (C) 2015 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 #ifndef __HARDWARE_SAMSUNG_SLSI_EXYNOS_APPMARKER_WRITER_H__
18 #define __HARDWARE_SAMSUNG_SLSI_EXYNOS_APPMARKER_WRITER_H__
19 
20 #include <ExynosExif.h>
21 #include "include/hardware/exynos/ExynosExif.h"
22 
23 #define JPEG_MAX_SEGMENT_SIZE ((1 << 16) - 1)
24 #define JPEG_MARKER_SIZE 2
25 #define JPEG_SEGMENT_LENFIELD_SIZE 2
26 #define JPEG_APP1_OEM_RESERVED 200
27 
28 #define IFD_FIELDCOUNT_SIZE 2
29 #define IFD_NEXTIFDOFFSET_SIZE 4
30 
31 #define IFD_TAG_SIZE 2
32 #define IFD_TYPE_SIZE 2
33 #define IFD_COUNT_SIZE 4
34 #define IFD_VALOFF_SIZE 4
35 
36 #define IFD_FIELD_SIZE \
37             (IFD_TAG_SIZE + IFD_TYPE_SIZE + IFD_COUNT_SIZE + IFD_VALOFF_SIZE)
38 
39 #define EXTRA_APPMARKER_MIN 4
40 #define EXTRA_APPMARKER_LIMIT 10
41 
42 #define MAX_GPS_PROCESSINGMETHOD_SIZE 108
43 
44 #define EXIF_SUBSECTIME_LENGTH 5
45 #define EXIF_DATETIME_LENGTH 20
46 #define EXIF_GPSDATESTAMP_LENGTH 11
47 
48 class CAppMarkerWriter {
49     char *m_pAppBase;
50     char *m_pApp1End;
51     size_t m_szMaxThumbSize; // Maximum available thumbnail stream size minus JPEG_MARKER_SIZE
52     uint16_t m_szApp1; // The size of APP1 segment without marker
53     uint16_t m_szApp11; // The size of APP11 segment without marker
54     uint16_t m_n0thIFDFields;
55     uint16_t m_n1stIFDFields;
56     uint16_t m_nExifIFDFields;
57     uint16_t m_nGPSIFDFields;
58     exif_attribute_t *m_pExif;
59     extra_appinfo_t *m_pExtra;
60 
61     uint32_t m_szMake;
62     uint32_t m_szSoftware;
63     uint32_t m_szModel;
64     uint32_t m_szUniqueID;
65 
66     char *m_pMainBase;
67     // The address to write compressed stream of the thumbnail image
68     char *m_pThumbBase;
69     // points to the "ValueOffset" field of JPEGInterchangeFormatLen tag of 1st IFD
70     // This should be updated after compression of thumbnail image.
71     // Note that the address may not be aligned by 32-bit.
72     char *m_pThumbSizePlaceholder;
73 
74     void Init();
75 
76     char *WriteAPP1(char *base, bool reserve_thumbnail_space, bool updating = false);
77     char *WriteAPPX(char *base, bool just_reserve);
78     char *WriteAPP11(char *current, size_t dummy, size_t align);
79 public:
80     // dummy: number of dummy bytes written by the compressor of the main image
81     // this dummy size should be added to the APP1 length. Howerver, this dummy area
82     // for the main image stream should not be written by neither of appwriter nor
83     // thumbnail compressor.
84     CAppMarkerWriter();
85     CAppMarkerWriter(char *base, exif_attribute_t *exif, debug_attribute_t *debug);
86 
~CAppMarkerWriter()87     ~CAppMarkerWriter() { }
88 
89     void PrepareAppWriter(char *base, exif_attribute_t *exif, extra_appinfo_t *info);
90 
GetMainStreamBase()91     char *GetMainStreamBase() { return m_pMainBase; }
GetThumbStreamBase()92     char *GetThumbStreamBase() { return m_pThumbBase; }
GetThumbStreamSizeAddr()93     char *GetThumbStreamSizeAddr() {
94         char *p = m_pThumbSizePlaceholder;
95         m_pThumbSizePlaceholder = NULL;
96         return p;
97     }
GetMaxThumbnailSize()98     size_t GetMaxThumbnailSize() { return m_szMaxThumbSize; }
GetAPP1ResrevedSize()99     size_t GetAPP1ResrevedSize() { return JPEG_APP1_OEM_RESERVED; }
100     // CalculateAPPSize() is valid after Write() is successful.
101     size_t CalculateAPPSize(size_t thumblen = JPEG_MAX_SEGMENT_SIZE) {
102         size_t appsize = 0;
103         if (m_szApp1 > 0)
104             appsize += m_szApp1 + JPEG_MARKER_SIZE;
105         if (m_pExtra) {
106             for (int idx = 0; idx < m_pExtra->num_of_appmarker; idx++)
107                 appsize += m_pExtra->appInfo[idx].dataSize +
108                            + JPEG_MARKER_SIZE + JPEG_SEGMENT_LENFIELD_SIZE;
109         }
110         if (IsThumbSpaceReserved())
111             appsize += m_szMaxThumbSize + JPEG_APP1_OEM_RESERVED ;
112         else
113             appsize += min(m_szMaxThumbSize, thumblen);
114 
115         return appsize + m_szApp11;
116     }
117 
GetApp1End()118     char *GetApp1End() { return m_pApp1End; }
119 
120     void Write(bool reserve_thumbnail_space, size_t dummy, size_t align, bool reserve_debug = false) {
121         m_pApp1End = WriteAPP1(m_pAppBase, reserve_thumbnail_space);
122         char *appXend = WriteAPPX(m_pApp1End, reserve_debug);
123         char *app11end = WriteAPP11(appXend, dummy, align);
124         m_szApp11 = PTR_DIFF(appXend, app11end);
125         m_pMainBase = app11end - dummy;
126     }
127 
Update()128     void Update() { WriteAPP1(m_pAppBase, false, true); }
129 
IsThumbSpaceReserved()130     bool IsThumbSpaceReserved() {
131         return PTR_DIFF(m_pAppBase, m_pApp1End) ==
132 			(m_szApp1 + m_szMaxThumbSize + JPEG_APP1_OEM_RESERVED + JPEG_MARKER_SIZE);
133     }
134 
135     void Finalize(size_t thumbsize);
136 
137     void UpdateApp1Size(size_t amount);
138 };
139 #endif //__HARDWARE_SAMSUNG_SLSI_EXYNOS_APPMARKER_WRITER_H__
140