camera-common.h revision c68dbbef0118eab4256acfc0d9430f0e557a82a1
1/*
2 * Copyright (C) 2011 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#ifndef ANDROID_CAMERA_CAMERA_COMMON_H_
18#define ANDROID_CAMERA_CAMERA_COMMON_H_
19
20/*
21 * Contains declarations of platform-independent the stuff that is used in
22 * camera emulation.
23 */
24
25#include "qemu-common.h"
26#include "android/utils/debug.h"
27#include "android/utils/misc.h"
28#include "android/utils/system.h"
29#ifdef _WIN32
30/* Include declarations that are missing in non-Linux headers. */
31#include "android/camera/camera-win.h"
32#elif _DARWIN_C_SOURCE
33/* Include declarations that are missing in non-Linux headers. */
34#include "android/camera/camera-win.h"
35#else
36#include <linux/videodev2.h>
37#endif  /* _WIN32 */
38
39/*
40 * These are missing in the current linux/videodev2.h
41 */
42
43#ifndef V4L2_PIX_FMT_YVYU
44#define V4L2_PIX_FMT_YVYU    v4l2_fourcc('Y', 'V', 'Y', 'U')
45#endif /* V4L2_PIX_FMT_YVYU */
46#ifndef V4L2_PIX_FMT_VYUY
47#define V4L2_PIX_FMT_VYUY    v4l2_fourcc('V', 'Y', 'U', 'Y')
48#endif /* V4L2_PIX_FMT_VYUY */
49#ifndef V4L2_PIX_FMT_YUY2
50#define V4L2_PIX_FMT_YUY2    v4l2_fourcc('Y', 'U', 'Y', '2')
51#endif /* V4L2_PIX_FMT_YUY2 */
52#ifndef V4L2_PIX_FMT_YUNV
53#define V4L2_PIX_FMT_YUNV    v4l2_fourcc('Y', 'U', 'N', 'V')
54#endif /* V4L2_PIX_FMT_YUNV */
55#ifndef V4L2_PIX_FMT_V422
56#define V4L2_PIX_FMT_V422    v4l2_fourcc('V', '4', '2', '2')
57#endif /* V4L2_PIX_FMT_V422 */
58#ifndef V4L2_PIX_FMT_YYVU
59#define V4L2_PIX_FMT_YYVU    v4l2_fourcc('Y', 'Y', 'V', 'U')
60#endif /* V4L2_PIX_FMT_YYVU */
61#ifndef V4L2_PIX_FMT_SGBRG8
62#define V4L2_PIX_FMT_SGBRG8  v4l2_fourcc('G', 'B', 'R', 'G')
63#endif  /* V4L2_PIX_FMT_SGBRG8 */
64#ifndef V4L2_PIX_FMT_SGRBG8
65#define V4L2_PIX_FMT_SGRBG8  v4l2_fourcc('G', 'R', 'B', 'G')
66#endif  /* V4L2_PIX_FMT_SGRBG8 */
67#ifndef V4L2_PIX_FMT_SRGGB8
68#define V4L2_PIX_FMT_SRGGB8  v4l2_fourcc('R', 'G', 'G', 'B')
69#endif  /* V4L2_PIX_FMT_SRGGB8 */
70#ifndef V4L2_PIX_FMT_SBGGR10
71#define V4L2_PIX_FMT_SBGGR10 v4l2_fourcc('B', 'G', '1', '\0')
72#endif  /* V4L2_PIX_FMT_SBGGR10 */
73#ifndef V4L2_PIX_FMT_SGBRG10
74#define V4L2_PIX_FMT_SGBRG10 v4l2_fourcc('G', 'B', '1', '\0')
75#endif  /* V4L2_PIX_FMT_SGBRG10 */
76#ifndef V4L2_PIX_FMT_SGRBG10
77#define V4L2_PIX_FMT_SGRBG10 v4l2_fourcc('B', 'A', '1', '\0')
78#endif  /* V4L2_PIX_FMT_SGRBG10 */
79#ifndef V4L2_PIX_FMT_SRGGB10
80#define V4L2_PIX_FMT_SRGGB10 v4l2_fourcc('R', 'G', '1', '\0')
81#endif  /* V4L2_PIX_FMT_SRGGB10 */
82#ifndef V4L2_PIX_FMT_SBGGR12
83#define V4L2_PIX_FMT_SBGGR12 v4l2_fourcc('B', 'G', '1', '2')
84#endif  /* V4L2_PIX_FMT_SBGGR12 */
85#ifndef V4L2_PIX_FMT_SGBRG12
86#define V4L2_PIX_FMT_SGBRG12 v4l2_fourcc('G', 'B', '1', '2')
87#endif  /* V4L2_PIX_FMT_SGBRG12 */
88#ifndef V4L2_PIX_FMT_SGRBG12
89#define V4L2_PIX_FMT_SGRBG12 v4l2_fourcc('B', 'A', '1', '2')
90#endif  /* V4L2_PIX_FMT_SGRBG12 */
91#ifndef V4L2_PIX_FMT_SRGGB12
92#define V4L2_PIX_FMT_SRGGB12 v4l2_fourcc('R', 'G', '1', '2')
93#endif  /* V4L2_PIX_FMT_SRGGB12 */
94
95/* Describes framebuffer, used by the client of camera capturing API.
96 * This descritptor is used in camera_device_read_frame call.
97 */
98typedef struct ClientFrameBuffer {
99    /* Pixel format used in the client framebuffer. */
100    uint32_t    pixel_format;
101    /* Address of the client framebuffer. */
102    void*       framebuffer;
103} ClientFrameBuffer;
104
105/* Describes frame dimensions.
106 */
107typedef struct CameraFrameDim {
108    /* Frame width. */
109    int     width;
110    /* Frame height. */
111    int     height;
112} CameraFrameDim;
113
114/* Camera information descriptor, containing properties of a camera connected
115 * to the host.
116 *
117 * Instances of this structure are created during camera device enumerations,
118 * and are considered to be constant everywhere else. The only exception to this
119 * rule is changing the 'in_use' flag during creation / destruction of a service
120 * representing that camera.
121 */
122typedef struct CameraInfo {
123    /* User-friendly camera display name. */
124    char*               display_name;
125    /* Device name for the camera. */
126    char*               device_name;
127    /* Input channel for the camera. */
128    int                 inp_channel;
129    /* Pixel format chosen for the camera. */
130    uint32_t            pixel_format;
131    /* Direction the camera is facing: 'front', or 'back' */
132    char*               direction;
133    /* Array of frame sizes supported for the pixel format chosen for the camera.
134     * The size of the array is defined by the frame_sizes_num field of this
135     * structure. */
136    CameraFrameDim*     frame_sizes;
137    /* Number of frame sizes supported for the pixel format chosen
138     * for the camera. */
139    int                 frame_sizes_num;
140    /* In use status. When there is a camera service created for this camera,
141     * "in use" is set to one. Otherwise this flag is zet to 0. */
142    int                 in_use;
143} CameraInfo;
144
145/* Allocates CameraInfo instance. */
146static __inline__ CameraInfo* _camera_info_alloc(void)
147{
148    CameraInfo* ci;
149    ANEW0(ci);
150    return ci;
151}
152
153/* Frees all resources allocated for CameraInfo instance (including the
154 * instance itself).
155 */
156static __inline__ void _camera_info_free(CameraInfo* ci)
157{
158    if (ci != NULL) {
159        if (ci->display_name != NULL)
160            free(ci->display_name);
161        if (ci->device_name != NULL)
162            free(ci->device_name);
163        if (ci->direction != NULL)
164            free(ci->direction);
165        if (ci->frame_sizes != NULL)
166            free(ci->frame_sizes);
167        AFREE(ci);
168    }
169}
170
171/* Describes a connected camera device.
172 * This is a pratform-independent camera device descriptor that is used in
173 * the camera API.
174 */
175typedef struct CameraDevice {
176    /* Opaque pointer used by the camera capturing API. */
177    void*       opaque;
178} CameraDevice;
179
180/* Returns current time in microseconds. */
181static __inline__ uint64_t
182_get_timestamp(void)
183{
184    struct timeval t;
185    t.tv_sec = t.tv_usec = 0;
186    gettimeofday(&t, NULL);
187    return (uint64_t)t.tv_sec * 1000000LL + t.tv_usec;
188}
189
190/* Sleeps for the given amount of milliseconds */
191static __inline__ void
192_sleep(int millisec)
193{
194    struct timeval t;
195    const uint64_t wake_at = _get_timestamp() + (uint64_t)millisec * 1000;
196    do {
197        const uint64_t stamp = _get_timestamp();
198        if ((stamp / 1000) >= (wake_at / 1000)) {
199            break;
200        }
201        t.tv_sec = (wake_at - stamp) / 1000000;
202        t.tv_usec = (wake_at - stamp) - (uint64_t)t.tv_sec * 1000000;
203    } while (select(0, NULL, NULL, NULL, &t) < 0 && errno == EINTR);
204}
205
206#endif  /* ANDROID_CAMERA_CAMERA_COMMON_H_ */
207