1 /*---------------------------------------------------------------------------*
2 
3   Copyright (C) 2010-2011 Nintendo.  All rights reserved.
4 
5   These coded instructions, statements, and computer programs contain
6   proprietary information of Nintendo of America Inc. and/or Nintendo
7   Company Ltd., and are protected by Federal copyright law.  They may
8   not be disclosed to third parties or copied or duplicated in any form,
9   in whole or in part, without the prior written consent of Nintendo.
10 
11  *---------------------------------------------------------------------------*/
12 
13 // camera.h
14 //
15 // Interface for Camera Library
16 
17 #ifndef _CAMERA_LIB_H
18 #define _CAMERA_LIB_H
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
24 
25 /*
26  * Camera Spec
27  */
28 
29 #define CAMERA_STREAM_WIDTH  (640)
30 #define CAMERA_STREAM_PITCH  (768)
31 #define CAMERA_STREAM_HEIGHT (480)
32 #define NUM_STREAM_BUFFER    (20)
33 
34 #define CAMERA_Y_BUFFER_SIZE  ( CAMERA_STREAM_PITCH * CAMERA_STREAM_HEIGHT )
35 #define CAMERA_UV_BUFFER_SIZE ( CAMERA_STREAM_PITCH * CAMERA_STREAM_HEIGHT / 2 )
36 #define CAMERA_YUV_BUFFER_SIZE ( CAMERA_Y_BUFFER_SIZE + CAMERA_UV_BUFFER_SIZE )
37 
38 #define CAMERA_YUV_BUFFER_ALIGNMENT (256)
39 
40 /*
41  * error codes
42  */
43 #define CAMERA_ERROR_NONE                (0)
44 #define CAMERA_ERROR_INVALID_ARG         (-1)
45 #define CAMERA_ERROR_INVALID_HANDLE      (-2)
46 #define CAMERA_ERROR_INVALID_STATE       (-3)
47 #define CAMERA_ERROR_QUEUE_FULL          (-4)
48 #define CAMERA_ERROR_INSUFFICIENT_MEMORY (-5)
49 #define CAMERA_ERROR_NOT_READY           (-6)
50 #define CAMERA_ERROR_FSA_FAILURE         (-7)
51 #define CAMERA_ERROR_UNIINITIALIZED      (-8)
52 #define CAMERA_ERROR_DEVICE_NOT_READY    (-9)
53 #define CAMERA_ERROR_UNKNOWN             (-10)
54 #define CAMERA_ERROR_NOT_IMPLEMENTED     (-11)
55 #define CAMERA_ERROR_DEVICE_IN_USE       (-12)
56 #define CAMERA_ERROR_UVD_FAILURE         (-13)
57 #define CAMERA_ERROR_NOT_SUPPORTED       (-14)
58 #define CAMERA_ERROR_SEGMENT_VIOLATION   (-15)
59 
60 typedef enum _CAM_STATE
61 {
62     CAMERA_DEVICE_POWER_MODE = 0,        ///< Not Implemented
63     CAMERA_AUTO_EXPOSURE_MODE,
64     CAMERA_AUTO_EXPOSURE_TIME_ABSOLUTE,
65     CAMERA_BRIGHTNESS,
66     CAMERA_CONTRAST,
67     CAMERA_GAIN,
68     CAMERA_POWER_LINE_FREQUENCY,
69     CAMERA_HUE,
70     CAMERA_SATURATION,
71     CAMERA_SHARPNESS,
72     CAMERA_GAMMA,
73     CAMERA_DIGITAL_MULTIPLIER_STEP,      ///< Not Implemented
74     CAMERA_DIGITAL_MULTIPLIER_LIMIT,     ///< Not Implemented
75     CAMERA_WHITE_BALANCE_COMPONENT,
76     CAMERA_WHITE_BALANCE_COMPONENT_AUTO,
77     CAMERA_DRC_CONNECTION_STATE,          ///< 1: DRC attached, 0: DRC detached; Only implemented in CAMGetState
78     CAMERA_GENERATE_KEY_FRAME,
79     CAMERA_STATE_NUM                     ///< Number of camera states
80 } CAM_STATE;
81 
82 
83 /// \brief define a type for camera handle
84 typedef int    CAMHandle;
85 
86 /// \brief define all supported Camera IDs here
87 typedef enum _CAMERA_INSTANCE
88 {
89     CAMERA_INSTANCE_0 = 0,
90     CAMERA_INSTANCE_NUM
91 } CAMERA_INSTANCE;
92 
93 
94 /// \brief enumerates pixel formats for target decode surface
95 typedef enum _CAMERA_PIXEL_FORMAT
96 {
97     CAMERA_NV12 = 0
98 
99 } CAMERA_PIXEL_FORMAT;
100 
101 /// \brief enumerates tiling modes for target decode surface
102 typedef enum _CAMERA_TILE_MODE
103 {
104     CAMERA_LINEAR = 0
105 
106 } CAMERA_TILE_MODE;
107 
108 /// \brief enumerates event types
109 typedef enum _CAMERA_EVENT_TYPE
110 {
111     CAMERA_DECODE_DONE = 0,
112     CAMERA_DRC_DETACH
113 
114 } CAMERA_EVENT_TYPE;
115 
116 /// \brief enumerates DRC camera's display modes
117 typedef enum _DRC_CAMERA_DISPLAY_MODE
118 {
119     DRC_CAMERA_DOWNSTREAM,
120     DRC_CAMERA_LOCAL_LOOPBACK
121 }DRC_CAMERA_DISPLAY_MODE;
122 
123 /// \brief enumerates DRC camera's FPS
124 typedef enum _DRC_CAMERA_FPS
125 {
126     DRC_CAMERA_FPS_15,
127     DRC_CAMERA_FPS_30
128 }DRC_CAMERA_FPS;
129 
130 /// \brief camera request to prevent flickering
131 typedef enum _DRC_POWER_LINE_FREQUENCY
132 {
133     DRC_POWER_LINE_FREQUENCY_DISABLED = 0,
134     DRC_POWER_LINE_FREQUENCY_50Hz     = 1,
135     DRC_POWER_LINE_FREQUENCY_60Hz     = 2
136 }DRC_POWER_LINE_FREQUENCY;
137 
138 
139 /// \brief holds different mode settings for camera
140 typedef struct _CAMMode {
141     DRC_CAMERA_DISPLAY_MODE display;
142     DRC_CAMERA_FPS          fps;
143 }CAMMode;
144 
145 
146 /// \brief holds different state information
147 typedef struct _CAMStateInfo {
148     int max;
149     int min;
150     int res;
151 }CAMStateInfo;
152 
153 
154 /// \brief Input parameters for UVDEventHandler method
155 typedef struct _Camera_Event_Handler_Input
156 {
157     CAMERA_EVENT_TYPE   type;    ///< event type
158     u32                 data0;   ///< event data
159     u32                 data1;   ///< event data
160     u32                 data2;   ///< event data
161 } Camera_Event_Handler_Input;
162 
163 /**
164  * \brief Event handler method
165  * \param event_handler_input input structure
166  * \return Whether this call is successfull or not
167  *
168  * This method is called from an ISR. So, any processing
169  * in this function should be kept to the bare minimum
170  */
171 typedef void
172 (*CAMEventHandler)(
173     Camera_Event_Handler_Input    *event_handler_input
174 );
175 
176 /// \brief enumerates camera stream types
177 typedef enum _CAMERA_STREAM_TYPE
178 {
179     CAMERA_STREAM_VIDEO
180 } CAMERA_STREAM_TYPE;
181 
182 /// \brief holds information about work memory
183 typedef struct _CAMWorkMem
184 {
185     int  size;    ///< size of work memory
186     void *pMem;   ///< pointer to the work memory
187 } CAMWorkMem;
188 
189 /// \brief holds information about the compressed data stream
190 typedef struct _CamearaStreamInfo
191 {
192     CAMERA_STREAM_TYPE type;    ///< stream type
193     int height;                 ///< height of the encoded picture
194     int width;                  ///< width of the encoded picture
195 } CAMStreamInfo;
196 
197 
198 #define CAM_THREAD_ATTR_AFFINITY_NONE  0x00000007u        // affinity to run on every core
199 #define CAM_THREAD_ATTR_AFFINITY_CORE0 0x00000001u        // run only on core0
200 #define CAM_THREAD_ATTR_AFFINITY_CORE1 0x00000002u        // run only on core1
201 #define CAM_THREAD_ATTR_AFFINITY_CORE2 0x00000004u        // run only on core2
202 
203 /// \brief holds information to setup camera
204 typedef struct _CAMSetupInfo
205 {
206     CAMStreamInfo    StreamInfo;       ///< info about stream
207     CAMWorkMem       WorkMem;          ///< info about the work memory
208     CAMEventHandler  eventHandler;     ///< event handler function
209     CAMMode          mode;             ///< camera mode settings
210     int              ThreadAffinity;   ///< core affinity attribute for camera threads;
211                                        ///< This is OR'd bit value of CAM_THREAD_ATTR_AFFINITY_* attributes
212     int              reserve[4];       ///< Reserved for future use
213 } CAMSetupInfo;
214 
215 /// \brief camear surface structure
216 typedef struct _CAMSurface
217 {
218     int  imageSize;    ///< size of image in bytes
219     void *imagePtr;    ///< pointer to the image
220     int  height;       ///< height of the image in pixels
221     int  width;        ///< width of the image in pixels
222     int  pitch;        ///< pitch of the image in bytes
223     int  alignment;    ///< alignment to use when allocating memory for the image
224     CAMERA_TILE_MODE     tileMode;   ///< tiling mode of the image
225     CAMERA_PIXEL_FORMAT  pixelFormat;  ///< pixel format of the image
226 } CAMSurface;
227 
228 /**
229  *  \brief Initializes camera library
230  *
231  *  \param instance camera instance number
232  *  \param pSetup   camera setup info
233  *  \return whether the call is successfull or not
234  */
235 CAMHandle CAMInit(CAMERA_INSTANCE instance, CAMSetupInfo *pSetup, int *err);
236 
237 /**
238  *  \brief Exit camera library
239  *
240  *  \param hCam camera handle
241  */
242 void CAMExit(CAMHandle hCam);
243 
244 /**
245  *  \brief Opens camera library for use
246  *
247  *  \param hCam camera handle
248  *  \return whether the call is successfull or not
249  */
250 int CAMOpen(CAMHandle hCam);
251 
252 /**
253  *  \brief Closes camera library
254  *
255  *  \param hCam camera handle
256  *  \return whether the call is successfull or not
257  */
258 int CAMClose(CAMHandle hCam);
259 
260 /**
261  *  \brief Get total memory required for camera
262  *
263  *  \param StreamInfo camera stream information like desired resolution
264  *  \return total memory required for operating camera
265  */
266 int CAMGetMemReq(CAMStreamInfo *StreamInfo);
267 
268 /**
269  *  \brief submits a target surface for camera library to write out the picture
270  *
271  *  \param hCam camera handle
272  *  \param surface camera surface where a picture is desired
273  *  \return whether the call is successfull or not
274  */
275 int CAMSubmitTargetSurface(CAMHandle hCam, CAMSurface *surface);
276 
277 /**
278  *  \brief gets the current value of a camera state (CAM_STATE) variable
279  *
280  *  \param hCam camera handle
281  *  \param state one of camera state variables
282  *  \return whether the call is successfull or not
283  */
284 int CAMGetState(CAMHandle hCam, CAM_STATE state, int *pVal);
285 
286 /**
287  *  \brief gets information about a camera state (CAM_STATE) variable
288  *
289  *  \param hCam camera handle
290  *  \param state one of camera state variables
291  *  \param pInfo holds min, max and resolution (res) of a camera state
292  *  \return whether the call is successfull or not
293  */
294 int CAMGetStateInfo(CAMHandle hCam, CAM_STATE state, CAMStateInfo *pInfo);
295 
296 /**
297  *  \brief sets the value of a camera state (CAM_STATE) variable
298  *
299  *  \param hCam camera handle
300  *  \param state one of camera state variables
301  *  \return whether the call is successfull or not
302  */
303 int CAMSetState(CAMHandle hCam, CAM_STATE state, int *pVal);
304 /**
305  *  \brief This method checks whether the memory allocated by the
306  *  caller meets hardware limitation of not crossing a 256 MB segment.
307  *  This limitation applies only to the work memory and target decode
308  *  surface memory.
309  *  If this API returns FALSE, caller should re-allocate the
310  *  memory, free the memory allocated earlier, and call this API
311  *  again to validate the new memory.
312  *
313  *  \param pMem start address of the allocated memory region
314  *  \param size size of the allocated memory region
315  *  \returns CAMERA_ERROR_NONE if the allocated memory does not straddle
316  *  256 MB boundary; otherwise returns CAMERA_ERROR_SEGMENT_VIOLATION
317  */
318 int CAMCheckMemSegmentation(void *pMem, unsigned int size);
319 
320 #ifdef __cplusplus
321 }
322 #endif
323 
324 #endif //_CAMERA_LIB_H
325