3 * Copyright (C) 2002 Ronald Bultje <rbultje@ronald.bitfreak.net>
4 * 2006 Edgard Lima <edgard.lima@indt.org.br>
5 * 2008-2010 Nokia Corporation <multimedia@maemo.org>
6 * 2010 Intel Corporation
8 * v4l2camsrc.c - system calls
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Library General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Library General Public License for more details.
20 * You should have received a copy of the GNU Library General Public
21 * License along with this library; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 02111-1307, USA.
30 #include <sys/types.h>
33 #include <sys/ioctl.h>
40 /* Needed on older Solaris Nevada builds (72 at least) */
42 #include <sys/ioccom.h>
45 #include <gst/camera/gstmfldcameracolorbalance.h>
46 #include <gst/controller/gstcontroller.h>
48 #include "v4l2camsrc_calls.h"
49 #include <atomisp_v4l2.h>
52 GST_DEBUG_CATEGORY_EXTERN (gst_v4l2camsrc_debug);
53 #define GST_CAT_DEFAULT gst_v4l2camsrc_debug
56 static const gint gst_v4l2camsrc_capture_map[] = {
57 CAM_VIEWFINDER_MODE_VIEWFINDER,
58 CAM_VIEWFINDER_MODE_STILL_CAPTURE,
59 CAM_VIEWFINDER_MODE_VIDEO_RECORD,
63 static const gint gst_v4l2camsrc_effect_map[] = {
64 CAM_GENERAL_EFFECT_TYPE_NORMAL,
65 CAM_GENERAL_EFFECT_TYPE_SEPIA,
66 CAM_GENERAL_EFFECT_TYPE_NEGATIVE,
67 CAM_GENERAL_EFFECT_TYPE_GRAYSCALE,
68 CAM_GENERAL_EFFECT_TYPE_NORMAL,
69 CAM_GENERAL_EFFECT_TYPE_VIVID,
70 CAM_GENERAL_EFFECT_TYPE_NORMAL,
71 CAM_GENERAL_EFFECT_TYPE_NORMAL,
72 CAM_GENERAL_EFFECT_TYPE_NORMAL,
73 CAM_GENERAL_EFFECT_TYPE_SKY_BLUE,
74 CAM_GENERAL_EFFECT_TYPE_GRASS_GREEN,
75 CAM_GENERAL_EFFECT_TYPE_SKIN_WHITEN,
79 static const gint gst_v4l2camsrc_wb_map[] = {
81 CAM_AWB_MODE_DAYLIGHT,
84 CAM_AWB_MODE_TUNGSTEN,
85 CAM_AWB_MODE_FLUORESCENT,
90 static gint find_item (const gint table[], const gint item);
91 /* Define this to use memory locking for video buffers */
92 /* #define USE_MLOCK */
95 #define GST_V4L2CAMSRC_SET_ACTIVE(element) (element)->buffer = GINT_TO_POINTER (-1)
96 #define GST_V4L2CAMSRC_SET_INACTIVE(element) (element)->buffer = NULL
98 /* On some systems MAP_FAILED seems to be missing */
100 #define MAP_FAILED ((caddr_t) -1)
103 #define RESIZER_MAX_DOWNSCALE_FACTOR 4
104 #define V4L2CAMSRC_POLL_TIMEOUT (20 * GST_SECOND)
106 #define GST_TYPE_V4L2CAMSRC_BUFFER (gst_v4l2camsrc_buffer_get_type())
107 #define GST_IS_V4L2CAMSRC_BUFFER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_V4L2CAMSRC_BUFFER))
108 #define GST_V4L2CAMSRC_BUFFER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_V4L2CAMSRC_BUFFER, GstV4l2Buffer))
110 /* Local functions */
112 gst_v4l2camsrc_get_nearest_size (GstMFLDV4l2CamSrc * v4l2camsrc,
113 guint32 pixelformat, gint * width, gint * height);
114 static void gst_v4l2camsrc_buffer_pool_destroy (GstMFLDV4l2CamSrcBufferPool *
115 pool, GstMFLDV4l2CamSrc * v4l2camsrc);
116 static gboolean gst_v4l2camsrc_update_cropping (GstMFLDV4l2CamSrc * v4l2camsrc,
117 gint width, gint height, gfloat zoom);
119 gst_v4l2camsrc_check_focus_status (GstMFLDV4l2CamSrc * v4l2camsrc,
120 GstCameraFocusStatus *fs, gboolean detailed);
122 static gboolean gst_v4l2camsrc_libmfldcam_init (GstMFLDV4l2CamSrc * v4l2camsrc);
123 static gboolean gst_v4l2camsrc_libmfldcam_deinit (GstMFLDV4l2CamSrc *
130 static GstBufferClass *v4l2buffer_parent_class = NULL;
133 gst_v4l2camsrc_buffer_finalize (GstV4l2Buffer * buffer)
135 GstMFLDV4l2CamSrcBufferPool *pool;
136 gboolean resuscitated = FALSE;
137 struct v4l2_buffer *vbuffer;
141 vbuffer = (struct v4l2_buffer *) buffer->vbuffer;
142 index = vbuffer->index;
144 GST_LOG ("finalizing buffer %p %d", buffer, index);
146 g_mutex_lock (pool->lock);
147 if (GST_BUFFER_SIZE (buffer) != 0)
148 /* BUFFER_SIZE is only set if the frame was dequeued */
149 pool->num_live_buffers--;
152 if (pool->is_vaapi_sharing && buffer->gbuffer)
153 GST_BUFFER_DATA(buffer) = GST_BUFFER_DATA(buffer->gbuffer);
154 if (ioctl (pool->video_fd, VIDIOC_QBUF, vbuffer) < 0) {
155 GST_WARNING ("could not requeue buffer %p %d", buffer, index);
157 /* FIXME: check that the caps didn't change */
158 GST_LOG ("reviving buffer %p, %d", buffer, index);
159 gst_buffer_ref (GST_BUFFER (buffer));
160 GST_BUFFER_SIZE (buffer) = 0;
161 pool->buffers[index] = buffer;
162 pool->queued[index] = 1;
163 g_cond_signal (pool->data_cond);
167 GST_LOG ("the pool is shutting down");
169 g_mutex_unlock (pool->lock);
172 GST_LOG ("buffer %p not recovered, unmapping", buffer);
173 if (buffer->use_mmap)
174 munmap ((void *) GST_BUFFER_DATA (buffer), vbuffer->length);
175 gst_mini_object_unref (GST_MINI_OBJECT (pool));
177 if (!buffer->use_mmap) {
178 if (buffer->gbuffer) {
179 /* It was allocated with gst_pad_alloc_buffer */
180 /* FIXME temporal fix for double free error */
181 if (pool->is_vaapi_sharing)
182 gst_buffer_unref (buffer->gbuffer);
183 buffer->gbuffer = NULL;
185 /* It was allocated with posix_memalign */
186 free (GST_BUFFER_DATA (buffer));
190 GST_BUFFER_DATA (buffer) = NULL;
192 g_free (buffer->vbuffer);
194 GST_LOG ("free v4l2buffer");
195 GST_MINI_OBJECT_CLASS (v4l2buffer_parent_class)->finalize (GST_MINI_OBJECT
201 gst_v4l2camsrc_buffer_class_init (gpointer g_class, gpointer class_data)
203 GstMiniObjectClass *mini_object_class = GST_MINI_OBJECT_CLASS (g_class);
205 v4l2buffer_parent_class = g_type_class_peek_parent (g_class);
207 mini_object_class->finalize =
208 (GstMiniObjectFinalizeFunction) gst_v4l2camsrc_buffer_finalize;
212 gst_v4l2camsrc_buffer_get_type (void)
214 static GType _gst_v4l2camsrc_buffer_type;
216 if (G_UNLIKELY (_gst_v4l2camsrc_buffer_type == 0)) {
217 static const GTypeInfo v4l2camsrc_buffer_info = {
218 sizeof (GstBufferClass),
221 gst_v4l2camsrc_buffer_class_init,
224 sizeof (GstV4l2Buffer),
229 _gst_v4l2camsrc_buffer_type = g_type_register_static (GST_TYPE_BUFFER,
230 "GstCameraBuffer", &v4l2camsrc_buffer_info, 0);
232 return _gst_v4l2camsrc_buffer_type;
235 static GstV4l2Buffer *
236 gst_v4l2camsrc_buffer_new (GstMFLDV4l2CamSrcBufferPool * pool,
237 GstMFLDV4l2CamSrc * v4l2camsrc, guint index, GstCaps * caps)
239 GstV4l2Buffer *ret = NULL;
240 GstFlowReturn flow_ret;
241 struct v4l2_buffer *vbuffer;
243 ret = (GstV4l2Buffer *) gst_mini_object_new (GST_TYPE_V4L2CAMSRC_BUFFER);
244 ret->use_mmap = v4l2camsrc->use_mmap;
245 vbuffer = ret->vbuffer = g_new0 (struct v4l2_buffer, 1);
246 GST_LOG ("creating buffer %u, %p in pool %p", index, ret, pool);
248 (GstMFLDV4l2CamSrcBufferPool *)
249 gst_mini_object_ref (GST_MINI_OBJECT (pool));
251 vbuffer->index = index;
252 vbuffer->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
254 if (v4l2camsrc->use_mmap)
255 vbuffer->memory = V4L2_MEMORY_MMAP;
257 vbuffer->memory = V4L2_MEMORY_USERPTR;
259 vbuffer->length = v4l2camsrc->frame_byte_size;
262 if (ioctl (pool->video_fd, VIDIOC_QUERYBUF, vbuffer) < 0)
263 goto querybuf_failed;
265 if (v4l2camsrc->use_mmap) {
267 data = (guint8 *) mmap (0, vbuffer->length,
268 PROT_READ | PROT_WRITE, MAP_SHARED, pool->video_fd, vbuffer->m.offset);
269 if (data == MAP_FAILED)
271 GST_BUFFER_DATA (ret) = (guint8 *) data;
273 if (gst_pad_is_linked (GST_BASE_SRC_PAD (v4l2camsrc))) {
274 GST_LOG ("using pad_alloc, size=%d", v4l2camsrc->frame_byte_size);
275 GST_LOG ("ALLOC CAPS: %" GST_PTR_FORMAT, caps);
278 gst_pad_alloc_buffer_and_set_caps (GST_BASE_SRC_PAD (v4l2camsrc), 0LL,
279 v4l2camsrc->frame_byte_size, caps, &ret->gbuffer);
280 if (flow_ret != GST_FLOW_OK)
281 goto pad_alloc_failed;
282 GST_BUFFER_DATA (ret) = ret->gbuffer->data;
286 GST_LOG ("using posix_memalign");
287 if (posix_memalign (&data, getpagesize (), vbuffer->length) != 0) {
288 goto memalign_failed;
290 GST_BUFFER_DATA (ret) = (guint8 *) data;
294 GST_BUFFER_SIZE (ret) = v4l2camsrc->frame_byte_size;
295 GST_BUFFER_FLAG_SET (ret, GST_BUFFER_FLAG_READONLY);
296 gst_buffer_set_caps (GST_BUFFER (ret), caps);
299 GST_DEBUG ("mlocking buffer data");
300 if (mlock ((void *) GST_BUFFER_DATA (ret), v4l2camsrc->frame_byte_size) == -1)
304 /* mlocking succeeded, now we can set the pointer to vbuffer. The existence
305 * of this pointer will be used later to determine if the munlock() is
307 if (!v4l2camsrc->use_mmap)
308 vbuffer->m.userptr = (unsigned int) GST_BUFFER_DATA (ret);
312 GST_LOG (" index: %u", vbuffer->index);
313 GST_LOG (" type: %d", vbuffer->type);
314 GST_LOG (" bytesused: %u", vbuffer->bytesused);
315 GST_LOG (" flags: %08x", vbuffer->flags);
316 GST_LOG (" field: %d", vbuffer->field);
317 GST_LOG (" memory: %d", vbuffer->memory);
318 if (vbuffer->memory == V4L2_MEMORY_MMAP)
319 GST_LOG (" MMAP offset: %p", vbuffer->m.offset);
320 else if (vbuffer->memory == V4L2_MEMORY_USERPTR)
321 GST_LOG (" user address: %p", vbuffer->m.userptr);
322 GST_LOG (" length: %u", vbuffer->length);
323 GST_LOG (" input: %u", vbuffer->input);
332 GST_WARNING ("Failed to mlock memory: %s", g_strerror (errno));
333 gst_buffer_unref (GST_BUFFER (ret));
340 gint errnosave = errno;
342 GST_WARNING ("Failed QUERYBUF: %s", g_strerror (errnosave));
343 gst_buffer_unref (GST_BUFFER (ret));
350 GST_WARNING ("Failed to posix_memalign a buffer");
356 GST_WARNING ("Failed to pad_alloc_buffer: %s",
357 gst_flow_get_name (flow_ret));
363 gint errnosave = errno;
364 GST_WARNING ("Failed to mmap: %s", g_strerror (errnosave));
365 gst_buffer_unref (GST_BUFFER (ret));
373 #define GST_TYPE_V4L2CAMSRC_BUFFER_POOL (gst_v4l2camsrc_buffer_pool_get_type())
374 #define GST_IS_V4L2CAMSRC_BUFFER_POOL(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_V4L2CAMSRC_BUFFER_POOL))
375 #define GST_V4L2CAMSRC_BUFFER_POOL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_V4L2CAMSRC_BUFFER_POOL, GstMFLDV4l2CamSrcBufferPool))
377 static GstMiniObjectClass *buffer_pool_parent_class = NULL;
380 gst_v4l2camsrc_buffer_pool_finalize (GstMFLDV4l2CamSrcBufferPool * pool)
382 g_mutex_free (pool->lock);
385 if (pool->video_fd >= 0)
386 close (pool->video_fd);
389 g_free (pool->buffers);
390 pool->buffers = NULL;
394 g_free (pool->queued);
398 g_cond_free (pool->data_cond);
399 pool->data_cond = NULL;
401 GST_MINI_OBJECT_CLASS (buffer_pool_parent_class)->finalize (GST_MINI_OBJECT
408 gst_v4l2camsrc_buffer_pool_init (GstMFLDV4l2CamSrcBufferPool * pool,
411 pool->lock = g_mutex_new ();
412 pool->running = FALSE;
413 pool->num_live_buffers = 0;
414 pool->data_cond = g_cond_new ();
415 pool->is_vaapi_sharing = FALSE;
421 gst_v4l2camsrc_buffer_pool_class_init (gpointer g_class, gpointer class_data)
423 GstMiniObjectClass *mini_object_class = GST_MINI_OBJECT_CLASS (g_class);
425 buffer_pool_parent_class = g_type_class_peek_parent (g_class);
427 mini_object_class->finalize = (GstMiniObjectFinalizeFunction)
428 gst_v4l2camsrc_buffer_pool_finalize;
434 gst_v4l2camsrc_buffer_pool_get_type (void)
436 static GType _gst_v4l2camsrc_buffer_pool_type;
438 if (G_UNLIKELY (_gst_v4l2camsrc_buffer_pool_type == 0)) {
439 static const GTypeInfo v4l2camsrc_buffer_pool_info = {
440 sizeof (GstBufferClass),
443 gst_v4l2camsrc_buffer_pool_class_init,
446 sizeof (GstMFLDV4l2CamSrcBufferPool),
448 (GInstanceInitFunc) gst_v4l2camsrc_buffer_pool_init,
451 _gst_v4l2camsrc_buffer_pool_type =
452 g_type_register_static (GST_TYPE_MINI_OBJECT,
453 "GstMFLDV4l2CamSrcBufferPool", &v4l2camsrc_buffer_pool_info, 0);
455 return _gst_v4l2camsrc_buffer_pool_type;
460 static GstMFLDV4l2CamSrcBufferPool *
461 gst_v4l2camsrc_buffer_pool_new (GstMFLDV4l2CamSrc * v4l2camsrc, gint fd,
464 GstMFLDV4l2CamSrcBufferPool *pool;
467 pool = (GstMFLDV4l2CamSrcBufferPool *)
468 gst_mini_object_new (GST_TYPE_V4L2CAMSRC_BUFFER_POOL);
470 pool->video_fd = dup (fd);
471 if (pool->video_fd < 0)
474 GstStructure *structure = gst_caps_get_structure (caps, 0);
475 if (structure && gst_structure_has_name(structure, "video/x-vaapi-sharing"))
476 pool->is_vaapi_sharing = TRUE;
478 pool->buffer_count = v4l2camsrc->num_buffers;
479 pool->buffers = g_new0 (GstV4l2Buffer *, pool->buffer_count);
480 pool->queued = g_new0 (guint, pool->buffer_count);
482 for (n = 0; n < pool->buffer_count; n++) {
483 pool->buffers[n] = gst_v4l2camsrc_buffer_new (pool, v4l2camsrc, n, caps);
484 GST_LOG ("buffer ref is %d", GST_MINI_OBJECT_REFCOUNT (pool->buffers[n]));
485 if (!pool->buffers[n])
486 goto buffer_new_failed;
494 gint errnosave = errno;
496 gst_v4l2camsrc_buffer_pool_destroy (pool, v4l2camsrc);
504 gint errnosave = errno;
506 GST_LOG ("creating a new buffer failed");
507 gst_mini_object_unref (GST_MINI_OBJECT (pool));
518 gst_v4l2camsrc_buffer_pool_activate (GstMFLDV4l2CamSrcBufferPool * pool,
519 GstMFLDV4l2CamSrc * v4l2camsrc)
523 g_mutex_lock (pool->lock);
525 for (n = 0; n < pool->buffer_count; n++) {
527 struct v4l2_buffer *buf;
529 buf = (struct v4l2_buffer *) pool->buffers[n]->vbuffer;
531 GST_LOG ("enqueue pool buffer %d", n);
533 if (ioctl (pool->video_fd, VIDIOC_QBUF, buf) < 0)
538 pool->running = TRUE;
540 g_mutex_unlock (pool->lock);
547 GST_ELEMENT_ERROR (v4l2camsrc, RESOURCE, READ,
548 ("Could not enqueue buffers in device '%s'.",
549 v4l2camsrc->videodev),
550 ("enqueing buffer %d/%d failed: %s",
551 n, v4l2camsrc->num_buffers, g_strerror (errno)));
552 g_mutex_unlock (pool->lock);
557 /* requeue buffers that are writable again */
558 /* FIXME: This isn't needed anymore. Buffers are re-queued automatically
559 * when they are finalized, so there is no need to wait for them separately */
561 gst_v4l2_buffer_pool_update (GstMFLDV4l2CamSrcBufferPool * pool,
562 GstMFLDV4l2CamSrc * v4l2camsrc)
567 g_mutex_lock (pool->lock);
569 for (n = 0; n < pool->buffer_count; n++) {
570 if (!pool->queued[n]) {
571 GST_LOG ("buffer %d is dequeued", n);
576 /* if all the buffers are dequeued, wait */
577 if (ref == v4l2camsrc->num_buffers) {
578 GST_LOG ("no free buffers available");
579 g_cond_wait (pool->data_cond, pool->lock);
582 g_mutex_unlock (pool->lock);
584 return (ref != v4l2camsrc->num_buffers) ? TRUE : FALSE;
590 gst_v4l2camsrc_buffer_pool_destroy (GstMFLDV4l2CamSrcBufferPool * pool,
591 GstMFLDV4l2CamSrc * v4l2camsrc)
595 g_mutex_lock (pool->lock);
596 pool->running = FALSE;
597 g_mutex_unlock (pool->lock);
599 GST_DEBUG ("destroy pool");
601 /* after this point, no more buffers will be queued or dequeued; no buffer
602 * from pool->buffers that is NULL will be set to a buffer, and no buffer that
603 * is not NULL will be pushed out. */
605 /* miniobjects have no dispose, so they can't break ref-cycles, as buffers ref
606 * the pool, we need to unref the buffer to properly finalize te pool */
607 for (n = 0; n < pool->buffer_count; n++) {
610 g_mutex_lock (pool->lock);
611 buf = GST_BUFFER (pool->buffers[n]);
612 g_mutex_unlock (pool->lock);
616 if (pool->buffers[n].m.userptr) {
617 GST_DEBUG ("munlocking buffer data");
618 munlock ((void *) pool->buffers[n].m.userptr,
619 v4l2camsrc->frame_byte_size);
622 /* we own the ref if the buffer is in pool->buffers; drop it. */
623 gst_buffer_unref (buf);
626 pool->is_vaapi_sharing = FALSE;
627 gst_mini_object_unref (GST_MINI_OBJECT (pool));
630 /******************************************************
631 * gst_v4l2camsrc_get_capture_capabilities():
632 * get the device's capturing capabilities
633 * return value: TRUE on success, FALSE on error
634 ******************************************************/
636 gst_v4l2camsrc_get_capture_capabilities (GstMFLDV4l2CamSrc * v4l2camsrc)
638 GST_DEBUG_OBJECT (v4l2camsrc, "getting capabilities");
640 if (!GST_V4L2CAMSRC_IS_OPEN (v4l2camsrc))
643 if (ioctl (v4l2camsrc->video_fd, VIDIOC_QUERYCAP, &v4l2camsrc->vcap) < 0)
646 GST_LOG_OBJECT (v4l2camsrc, "driver: '%s'", v4l2camsrc->vcap.driver);
647 GST_LOG_OBJECT (v4l2camsrc, "card: '%s'", v4l2camsrc->vcap.card);
648 GST_LOG_OBJECT (v4l2camsrc, "bus_info: '%s'", v4l2camsrc->vcap.bus_info);
649 GST_LOG_OBJECT (v4l2camsrc, "version: %08x", v4l2camsrc->vcap.version);
650 GST_LOG_OBJECT (v4l2camsrc, "capabilites: %08x",
651 v4l2camsrc->vcap.capabilities);
658 GST_ELEMENT_ERROR (v4l2camsrc, RESOURCE, SETTINGS,
659 ("Error getting capabilities for device '%s': "
660 "It isn't a v4l2 driver. Check if it is a v4l1 driver.",
661 v4l2camsrc->videodev), GST_ERROR_SYSTEM);
667 /******************************************************
668 * gst_v4l2camsrc_set_input_sensor():
669 * set which sensor is the input of ISP
670 * return value: TRUE on success, FALSE on error
671 ******************************************************/
673 gst_v4l2camsrc_set_input_sensor (GstMFLDV4l2CamSrc * v4l2camsrc)
675 V4L2CameraInputSensor select_sensor;
677 GST_DEBUG_OBJECT (v4l2camsrc, "setting input sensor");
679 if (!GST_V4L2CAMSRC_IS_OPEN (v4l2camsrc))
682 if (v4l2camsrc->input_sensor == GST_CAMERA_INPUT_SENSOR_PRIMARY)
683 select_sensor = V2L2_CAMERA_INPUT_SENSOR_PRIMARY;
685 select_sensor =V2L2_CAMERA_INPUT_SENSOR_SECONDARY;
687 if (ioctl (v4l2camsrc->video_fd, VIDIOC_S_INPUT,
691 GST_LOG_OBJECT (v4l2camsrc, "set input sensor to: %d",
699 GST_ELEMENT_ERROR (v4l2camsrc, RESOURCE, SETTINGS,
700 ("Error setting input for device '%s': ",
701 v4l2camsrc->videodev), GST_ERROR_SYSTEM);
707 get_supported_mmfw_control (GstMFLDV4l2CamSrc * v4l2camsrc, struct v4l2_queryctrl *control)
710 GST_DEBUG_OBJECT (v4l2camsrc, "set private control (%x)", control->id);
712 for (i = 0; i < N_MMFW_CONTROLS; i++) {
713 if (mmfw_wb_controls[i].id == control->id) {
714 *control = mmfw_wb_controls[i];
722 /******************************************************
723 * gst_v4l2camsrc_fill_lists():
724 * fill the lists of enumerations
725 * return value: TRUE on success, FALSE on error
726 ******************************************************/
728 gst_v4l2camsrc_fill_lists (GstMFLDV4l2CamSrc * v4l2camsrc)
732 GST_DEBUG_OBJECT (v4l2camsrc, "getting enumerations");
733 GST_V4L2CAMSRC_CHECK_OPEN (v4l2camsrc);
735 GST_DEBUG_OBJECT (v4l2camsrc, " controls+menus");
736 /* and lastly, controls+menus (if appropriate) */
737 for (n = V4L2_CID_BASE;; n++) {
738 struct v4l2_queryctrl control = { 0, };
739 GstCameraSrcColorBalanceChannel *v4l2channel;
741 GstColorBalanceChannel *channel;
743 /* when we reached the last official CID, continue with private CIDs */
744 if (n == V4L2_CID_LASTP1) {
745 GST_DEBUG_OBJECT (v4l2camsrc, "checking private CIDs");
746 n = V4L2_CID_PRIVATE_BASE;
747 /* FIXME: We are still not handling private controls. We need a
748 new GstInterface to export those controls */
750 GST_DEBUG_OBJECT (v4l2camsrc, "private ID");
755 if( n > SOURCE_PRIV_BASE) {
756 if( n > MM_CAM_SOURCE_PRIV_LAST)
759 get_supported_mmfw_control(v4l2camsrc, &control);
761 else if (ioctl (v4l2camsrc->video_fd, VIDIOC_QUERYCTRL, &control) < 0) {
762 if (errno == EINVAL) {
763 if (n < V4L2_CID_PRIVATE_BASE)
764 /* continue so that we also check private controls */
769 GST_ELEMENT_ERROR (v4l2camsrc, RESOURCE, SETTINGS,
770 ("Failed getting controls attributes on device '%s.'",
771 v4l2camsrc->videodev),
772 ("Failed querying control %d on device '%s'. (%d - %s)",
773 n, v4l2camsrc->videodev, errno, strerror (errno)));
777 if (control.flags & V4L2_CTRL_FLAG_DISABLED)
781 case V4L2_CID_BRIGHTNESS:
782 case V4L2_CID_CONTRAST:
783 case V4L2_CID_SATURATION:
785 case V4L2_CID_BLACK_LEVEL:
786 case V4L2_CID_AUTO_WHITE_BALANCE:
787 case V4L2_CID_DO_WHITE_BALANCE:
788 case V4L2_CID_RED_BALANCE:
789 case V4L2_CID_BLUE_BALANCE:
791 case V4L2_CID_EXPOSURE:
792 case V4L2_CID_AUTOGAIN:
794 case V4L2_CID_COLORFX:
795 case MM_CAM_FILTER_WB_SOURCE_PRIV:
796 case MM_CAM_FILTER_COLOR_TONE_SOURCE_PRIV:
797 /* we only handle these for now (why?) */
801 case V4L2_CID_HCENTER:
802 case V4L2_CID_VCENTER:
803 #ifdef V4L2_CID_PAN_RESET
804 case V4L2_CID_PAN_RESET:
806 #ifdef V4L2_CID_TILT_RESET
807 case V4L2_CID_TILT_RESET:
809 /* not handled here, handled by VideoOrientation interface */
812 case V4L2_CID_AUDIO_VOLUME:
813 case V4L2_CID_AUDIO_BALANCE:
814 case V4L2_CID_AUDIO_BASS:
815 case V4L2_CID_AUDIO_TREBLE:
816 case V4L2_CID_AUDIO_MUTE:
817 case V4L2_CID_AUDIO_LOUDNESS:
818 /* FIXME: We should implement GstMixer interface */
821 GST_DEBUG_OBJECT (v4l2camsrc,
822 "ControlID %s (%x) unhandled, FIXME", control.name, n);
829 GST_DEBUG_OBJECT (v4l2camsrc, "Adding ControlID %s (%x)", control.name, n);
831 g_object_new (GST_TYPE_CAMERA_SRC_COLOR_BALANCE_CHANNEL, NULL);
832 channel = GST_COLOR_BALANCE_CHANNEL (v4l2channel);
833 channel->label = g_strdup ((const gchar *) control.name);
837 switch (control.type) {
838 case V4L2_CTRL_TYPE_INTEGER:
839 channel->min_value = control.minimum;
840 channel->max_value = control.maximum;
842 case V4L2_CTRL_TYPE_BOOLEAN:
843 channel->min_value = FALSE;
844 channel->max_value = TRUE;
847 /* FIXME we should find out how to handle V4L2_CTRL_TYPE_BUTTON.
848 BUTTON controls like V4L2_CID_DO_WHITE_BALANCE can just be set (1) or
849 unset (0), but can't be queried */
850 GST_DEBUG_OBJECT (v4l2camsrc,
851 "Control with non supported type %s (%x), type=%d",
852 control.name, n, control.type);
853 channel->min_value = channel->max_value = 0;
857 gst_camerasrc_add_color_channel (GST_CAMERA_SRC (v4l2camsrc), channel);
860 GST_DEBUG_OBJECT (v4l2camsrc, "done");
864 /******************************************************
865 * gst_v4l2camsrc_open():
866 * open the video device (v4l2camsrc->videodev)
867 * return value: TRUE on success, FALSE on error
868 ******************************************************/
870 gst_v4l2camsrc_open (GstCameraSrc * camsrc)
873 GstPollFD pollfd = GST_POLL_FD_INIT;
875 GstMFLDV4l2CamSrc *v4l2camsrc = GST_V4L2CAMSRC (camsrc);
877 GST_DEBUG_OBJECT (v4l2camsrc, "Trying to open device %s",
878 v4l2camsrc->videodev);
880 GST_V4L2CAMSRC_CHECK_NOT_OPEN (v4l2camsrc);
881 GST_V4L2CAMSRC_CHECK_NOT_ACTIVE (v4l2camsrc);
883 /* be sure we have a device */
884 if (!v4l2camsrc->videodev)
885 v4l2camsrc->videodev = g_strdup ("/dev/video");
887 /* check if it is a device */
888 if (stat (v4l2camsrc->videodev, &st) == -1)
891 if (!S_ISCHR (st.st_mode))
894 /* open the device */
895 v4l2camsrc->video_fd =
896 open (v4l2camsrc->videodev, O_RDWR /* | O_NONBLOCK */ );
898 if (!GST_V4L2CAMSRC_IS_OPEN (v4l2camsrc))
901 /* get capabilities, error will be posted */
902 if (!gst_v4l2camsrc_get_capture_capabilities (v4l2camsrc))
905 /* get capabilities, error will be posted */
906 if (!gst_v4l2camsrc_set_input_sensor (v4l2camsrc))
909 /* do we need to be a capture device? */
910 if (GST_IS_V4L2CAMSRC (v4l2camsrc) &&
911 !(v4l2camsrc->vcap.capabilities & V4L2_CAP_VIDEO_CAPTURE))
914 /* Before iterating enumerations, clear the parent's color channel list */
915 gst_camerasrc_clear_color_channels (camsrc);
917 /* create enumerations, posts errors. */
918 if (!gst_v4l2camsrc_fill_lists (v4l2camsrc))
921 GST_INFO_OBJECT (v4l2camsrc,
922 "Opened device '%s' (%s) successfully",
923 v4l2camsrc->vcap.card, v4l2camsrc->videodev);
925 pollfd.fd = v4l2camsrc->video_fd;
926 gst_poll_add_fd (v4l2camsrc->poll, &pollfd);
927 gst_poll_fd_ctl_read (v4l2camsrc->poll, &pollfd, TRUE);
929 gst_v4l2camsrc_libmfldcam_init (v4l2camsrc);
936 GST_ELEMENT_ERROR (v4l2camsrc, RESOURCE, NOT_FOUND,
937 ("Cannot identify device '%s'.", v4l2camsrc->videodev),
943 GST_ELEMENT_ERROR (v4l2camsrc, RESOURCE, NOT_FOUND,
944 ("This isn't a device '%s'.", v4l2camsrc->videodev), GST_ERROR_SYSTEM);
949 GST_ELEMENT_ERROR (v4l2camsrc, RESOURCE, OPEN_READ_WRITE,
950 ("Could not open device '%s' for reading and writing.",
951 v4l2camsrc->videodev), GST_ERROR_SYSTEM);
956 GST_ELEMENT_ERROR (v4l2camsrc, RESOURCE, NOT_FOUND,
957 ("Device '%s' is not a capture device.",
958 v4l2camsrc->videodev),
959 ("Capabilities: 0x%x", v4l2camsrc->vcap.capabilities));
964 if (GST_V4L2CAMSRC_IS_OPEN (v4l2camsrc)) {
966 close (v4l2camsrc->video_fd);
967 v4l2camsrc->video_fd = -1;
974 /******************************************************
975 * gst_v4l2camsrc_close():
976 * close the video device (v4l2camsrc->video_fd)
977 * return value: TRUE on success, FALSE on error
978 ******************************************************/
980 gst_v4l2camsrc_close (GstCameraSrc * camsrc)
982 GstMFLDV4l2CamSrc *v4l2camsrc = GST_V4L2CAMSRC (camsrc);
984 GstPollFD pollfd = GST_POLL_FD_INIT;
986 GST_DEBUG_OBJECT (v4l2camsrc, "Trying to close %s", v4l2camsrc->videodev);
988 GST_V4L2CAMSRC_CHECK_OPEN (v4l2camsrc);
989 GST_V4L2CAMSRC_CHECK_NOT_ACTIVE (v4l2camsrc);
992 gst_v4l2camsrc_libmfldcam_deinit (v4l2camsrc);
994 close (v4l2camsrc->video_fd);
995 pollfd.fd = v4l2camsrc->video_fd;
996 gst_poll_remove_fd (v4l2camsrc->poll, &pollfd);
997 v4l2camsrc->video_fd = -1;
1002 /******************************************************
1003 * gst_v4l2camsrc_get_attribute():
1004 * try to get the value of one specific attribute
1005 * return value: TRUE on success, FALSE on error
1006 ******************************************************/
1008 gst_v4l2camsrc_get_attribute (GstCameraSrc * camsrc,
1009 int attribute_num, int *value)
1011 GstMFLDV4l2CamSrc *v4l2camsrc = GST_V4L2CAMSRC (camsrc);
1013 struct v4l2_control control;
1017 GST_DEBUG_OBJECT (v4l2camsrc, "getting value of attribute %d", attribute_num);
1019 if (!GST_V4L2CAMSRC_IS_OPEN (v4l2camsrc))
1022 control.id = attribute_num;
1024 if (control.id > SOURCE_PRIV_BASE) {
1025 switch (control.id) {
1026 case MM_CAM_FILTER_WB_SOURCE_PRIV:
1027 err = cam_feature_get (v4l2camsrc->video_fd, CAM_AWB_MODE, &tmp_value);
1028 *value = find_item (gst_v4l2camsrc_wb_map, tmp_value);
1030 case MM_CAM_FILTER_COLOR_TONE_SOURCE_PRIV:
1031 err = cam_feature_get (v4l2camsrc->video_fd, CAM_GENERAL_EFFECT_TYPE, &tmp_value);
1032 *value = find_item (gst_v4l2camsrc_effect_map, tmp_value);
1039 if (ioctl (v4l2camsrc->video_fd, VIDIOC_G_CTRL, &control) < 0)
1041 *value = control.value;
1048 struct v4l2_ext_controls controls;
1049 struct v4l2_ext_control control;
1051 controls.ctrl_class = V4L2_CTRL_CLASS_USER;
1053 controls.controls = &control;
1055 control.id = attribute_num;
1057 if (ioctl (v4l2camsrc->video_fd, VIDIOC_G_EXT_CTRLS, &controls) < 0)
1060 *value = control.value;
1069 GST_ELEMENT_WARNING (v4l2camsrc, RESOURCE, SETTINGS,
1070 ("Failed to get value for control %d on device '%s'.",
1071 attribute_num, v4l2camsrc->videodev), GST_ERROR_SYSTEM);
1076 /******************************************************
1077 * gst_v4l2camsrc_set_attribute():
1078 * try to set the value of one specific attribute
1079 * return value: TRUE on success, FALSE on error
1080 ******************************************************/
1082 gst_v4l2camsrc_set_attribute (GstCameraSrc * camsrc,
1083 int attribute_num, const int value)
1085 GstMFLDV4l2CamSrc *v4l2camsrc = GST_V4L2CAMSRC (camsrc);
1086 struct v4l2_control control;
1088 GST_DEBUG_OBJECT (v4l2camsrc, "setting value of attribute %d to %d",
1089 attribute_num, value);
1091 if (!GST_V4L2CAMSRC_IS_OPEN (v4l2camsrc))
1094 control.id = attribute_num;
1095 control.value = value;
1096 if (control.id > SOURCE_PRIV_BASE) {
1097 switch (control.id) {
1098 case MM_CAM_FILTER_WB_SOURCE_PRIV:
1099 cam_feature_set (v4l2camsrc->video_fd, CAM_AWB_MODE, gst_v4l2camsrc_wb_map[value]);
1101 case MM_CAM_FILTER_COLOR_TONE_SOURCE_PRIV:
1102 cam_feature_set (v4l2camsrc->video_fd, CAM_GENERAL_EFFECT_TYPE, gst_v4l2camsrc_effect_map[value]);
1110 if (ioctl (v4l2camsrc->video_fd, VIDIOC_S_CTRL, &control) < 0)
1117 struct v4l2_ext_controls controls;
1118 struct v4l2_ext_control control;
1120 controls.ctrl_class = V4L2_CTRL_CLASS_USER;
1122 controls.controls = &control;
1124 control.id = attribute_num;
1125 control.value = value;
1127 if (ioctl (v4l2camsrc->video_fd, VIDIOC_S_EXT_CTRLS, &controls) < 0)
1136 GST_ELEMENT_WARNING (v4l2camsrc, RESOURCE, SETTINGS,
1137 ("Failed to set value %d for control %d on device '%s'.",
1138 value, attribute_num, v4l2camsrc->videodev), GST_ERROR_SYSTEM);
1144 /* complete made up ranking, the values themselves are meaningless */
1145 #define YUV_BASE_RANK 1000
1146 #define JPEG_BASE_RANK 500
1147 #define DV_BASE_RANK 200
1148 #define RGB_BASE_RANK 100
1149 #define YUV_ODD_BASE_RANK 50
1150 #define RGB_ODD_BASE_RANK 25
1151 #define BAYER_BASE_RANK 15
1152 #define S910_BASE_RANK 10
1153 #define GREY_BASE_RANK 5
1154 #define PWC_BASE_RANK 1
1156 /* This flag is already used by libv4l2 although
1157 * it was added to the Linux kernel in 2.6.32
1159 #ifndef V4L2_FMT_FLAG_EMULATED
1160 #define V4L2_FMT_FLAG_EMULATED 0x0002
1164 gst_v4l2camsrc_format_get_rank (const struct v4l2_fmtdesc *fmt)
1166 guint32 fourcc = fmt->pixelformat;
1167 gboolean emulated = ((fmt->flags & V4L2_FMT_FLAG_EMULATED) != 0);
1171 case V4L2_PIX_FMT_MJPEG:
1172 rank = JPEG_BASE_RANK;
1174 case V4L2_PIX_FMT_JPEG:
1175 rank = JPEG_BASE_RANK + 1;
1177 case V4L2_PIX_FMT_MPEG: /* MPEG */
1178 rank = JPEG_BASE_RANK + 2;
1181 case V4L2_PIX_FMT_RGB332:
1182 case V4L2_PIX_FMT_RGB555:
1183 case V4L2_PIX_FMT_RGB555X:
1184 case V4L2_PIX_FMT_RGB565:
1185 case V4L2_PIX_FMT_RGB565X:
1186 rank = RGB_ODD_BASE_RANK;
1189 case V4L2_PIX_FMT_RGB24:
1190 case V4L2_PIX_FMT_BGR24:
1191 rank = RGB_BASE_RANK - 1;
1194 case V4L2_PIX_FMT_RGB32:
1195 case V4L2_PIX_FMT_BGR32:
1196 rank = RGB_BASE_RANK;
1199 case V4L2_PIX_FMT_GREY: /* 8 Greyscale */
1200 rank = GREY_BASE_RANK;
1203 case V4L2_PIX_FMT_NV12: /* 12 Y/CbCr 4:2:0 */
1204 case V4L2_PIX_FMT_NV21: /* 12 Y/CrCb 4:2:0 */
1205 case V4L2_PIX_FMT_YYUV: /* 16 YUV 4:2:2 */
1206 case V4L2_PIX_FMT_HI240: /* 8 8-bit color */
1207 case V4L2_PIX_FMT_NV16: /* 16 bit YUV 422, Y, UV plane */
1208 rank = YUV_ODD_BASE_RANK;
1210 case V4L2_PIX_FMT_YUV444: /* YUV 444, 24 bits per pixel */
1211 rank = YUV_BASE_RANK + 0;
1213 case V4L2_PIX_FMT_YVU410: /* YVU9, 9 bits per pixel */
1214 rank = YUV_BASE_RANK + 3;
1216 case V4L2_PIX_FMT_YUV410: /* YUV9, 9 bits per pixel */
1217 rank = YUV_BASE_RANK + 2;
1219 case V4L2_PIX_FMT_YUV420: /* I420, 12 bits per pixel */
1220 rank = YUV_BASE_RANK + 7;
1222 case V4L2_PIX_FMT_YUYV: /* YUY2, 16 bits per pixel */
1223 rank = YUV_BASE_RANK + 10;
1225 case V4L2_PIX_FMT_YVU420: /* YV12, 12 bits per pixel */
1226 rank = YUV_BASE_RANK + 6;
1228 case V4L2_PIX_FMT_UYVY: /* UYVY, 16 bits per pixel */
1229 rank = YUV_BASE_RANK + 9;
1231 case V4L2_PIX_FMT_Y41P: /* Y41P, 12 bits per pixel */
1232 rank = YUV_BASE_RANK + 5;
1234 case V4L2_PIX_FMT_YUV411P: /* Y41B, 12 bits per pixel */
1235 rank = YUV_BASE_RANK + 4;
1237 case V4L2_PIX_FMT_YUV422P: /* Y42B, 16 bits per pixel */
1238 rank = YUV_BASE_RANK + 8;
1241 case V4L2_PIX_FMT_DV:
1242 rank = DV_BASE_RANK;
1245 case V4L2_PIX_FMT_WNVA: /* Winnov hw compres */
1249 case V4L2_PIX_FMT_SBGGR8:
1250 case V4L2_PIX_FMT_SRGGB8:
1251 case V4L2_PIX_FMT_SGBRG8:
1252 case V4L2_PIX_FMT_SGRBG10:
1253 case V4L2_PIX_FMT_SRGGB10:
1254 case V4L2_PIX_FMT_SGBRG10:
1255 rank = BAYER_BASE_RANK;
1258 #ifdef V4L2_PIX_FMT_SN9C10X
1259 case V4L2_PIX_FMT_SN9C10X:
1260 rank = S910_BASE_RANK;
1264 #ifdef V4L2_PIX_FMT_PWC1
1265 case V4L2_PIX_FMT_PWC1:
1266 rank = PWC_BASE_RANK;
1269 #ifdef V4L2_PIX_FMT_PWC2
1270 case V4L2_PIX_FMT_PWC2:
1271 rank = PWC_BASE_RANK;
1276 GST_LOG("Don't know how to rank pixelformat %" GST_FOURCC_FORMAT,
1277 GST_FOURCC_ARGS(fourcc));
1282 /* All ranks are below 1<<15 so a shift by 15
1283 * will a) make all non-emulated formats larger
1284 * than emulated and b) will not overflow
1293 gst_v4l2camsrc_format_cmp_func (gconstpointer a, gconstpointer b)
1295 const struct v4l2_fmtdesc *fa = a;
1296 const struct v4l2_fmtdesc *fb = b;
1298 if (fa->pixelformat == fb->pixelformat)
1301 return gst_v4l2camsrc_format_get_rank (fb) -
1302 gst_v4l2camsrc_format_get_rank (fa);
1305 static gboolean is_supported_pixelformat(guint32 fourcc)
1307 if(fourcc == V4L2_PIX_FMT_NV12
1308 || fourcc == V4L2_PIX_FMT_YUV420
1309 || fourcc == V4L2_PIX_FMT_SGRBG10)
1314 /******************************************************
1315 * gst_v4l2camsrc_fill_format_list():
1316 * create list of supported capture formats
1317 * return value: TRUE on success, FALSE on error
1318 ******************************************************/
1320 gst_v4l2camsrc_fill_format_list (GstMFLDV4l2CamSrc * v4l2camsrc)
1323 struct v4l2_fmtdesc *format;
1325 GST_DEBUG_OBJECT (v4l2camsrc, "getting src format enumerations");
1327 /* format enumeration */
1329 format = g_new0 (struct v4l2_fmtdesc, 1);
1332 format->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1334 if (ioctl (v4l2camsrc->video_fd, VIDIOC_ENUM_FMT, format) < 0) {
1335 if (errno == EINVAL) {
1337 break; /* end of enumeration */
1342 if(!is_supported_pixelformat(format->pixelformat)) {
1343 GST_LOG_OBJECT(v4l2camsrc, " (skipping format %" GST_FOURCC_FORMAT ")",GST_FOURCC_ARGS (format->pixelformat));
1347 GST_LOG_OBJECT (v4l2camsrc, "index: %u", format->index);
1348 GST_LOG_OBJECT (v4l2camsrc, "type: %d", format->type);
1349 GST_LOG_OBJECT (v4l2camsrc, "flags: %08x", format->flags);
1350 GST_LOG_OBJECT (v4l2camsrc, "description: '%s'", format->description);
1351 GST_LOG_OBJECT (v4l2camsrc, "pixelformat: %" GST_FOURCC_FORMAT,
1352 GST_FOURCC_ARGS (format->pixelformat));
1354 /* sort formats according to our preference; we do this, because caps
1355 * are probed in the order the formats are in the list, and the order of
1356 * formats in the final probed caps matters for things like fixation */
1357 v4l2camsrc->formats = g_slist_insert_sorted (v4l2camsrc->formats, format,
1358 (GCompareFunc) gst_v4l2camsrc_format_cmp_func);
1361 GST_DEBUG_OBJECT (v4l2camsrc, "got %d format(s)", n);
1368 GST_ELEMENT_ERROR (v4l2camsrc, RESOURCE, SETTINGS,
1369 ("Failed to enumerate possible video formats device '%s' can work with",
1370 v4l2camsrc->videodev),
1371 ("Failed to get number %d in pixelformat enumeration for %s. (%d - %s)",
1372 n, v4l2camsrc->videodev, errno, g_strerror (errno)));
1378 /******************************************************
1379 * gst_v4l2camsrc_clear_format_list():
1380 * free list of supported capture formats
1381 * return value: TRUE on success, FALSE on error
1382 ******************************************************/
1384 gst_v4l2camsrc_clear_format_list (GstMFLDV4l2CamSrc * v4l2camsrc)
1386 g_slist_foreach (v4l2camsrc->formats, (GFunc) g_free, NULL);
1387 g_slist_free (v4l2camsrc->formats);
1388 v4l2camsrc->formats = NULL;
1393 /* The frame interval enumeration code first appeared in Linux 2.6.19. */
1394 #ifdef VIDIOC_ENUM_FRAMEINTERVALS
1395 static GstStructure *
1396 gst_v4l2camsrc_probe_caps_for_format_and_size (GstMFLDV4l2CamSrc * v4l2camsrc,
1397 guint32 pixelformat,
1398 guint32 width, guint32 height, const GstStructure * template)
1400 gint fd = v4l2camsrc->video_fd;
1401 struct v4l2_frmivalenum ival;
1404 GValue rates = { 0, };
1406 memset (&ival, 0, sizeof (struct v4l2_frmivalenum));
1408 ival.pixel_format = pixelformat;
1410 ival.height = height;
1412 GST_LOG_OBJECT (v4l2camsrc, "get frame interval for %ux%u, %"
1413 GST_FOURCC_FORMAT, width, height, GST_FOURCC_ARGS (pixelformat));
1415 /* keep in mind that v4l2 gives us frame intervals (durations); we invert the
1416 * fraction to get framerate */
1417 if (ioctl (fd, VIDIOC_ENUM_FRAMEINTERVALS, &ival) < 0)
1418 goto enum_frameintervals_failed;
1420 if (ival.type == V4L2_FRMIVAL_TYPE_DISCRETE) {
1421 GValue rate = { 0, };
1423 g_value_init (&rates, GST_TYPE_LIST);
1424 g_value_init (&rate, GST_TYPE_FRACTION);
1427 num = ival.discrete.numerator;
1428 denom = ival.discrete.denominator;
1430 if (num > G_MAXINT || denom > G_MAXINT) {
1431 /* let us hope we don't get here... */
1436 GST_LOG_OBJECT (v4l2camsrc, "adding discrete framerate: %d/%d",
1439 /* swap to get the framerate */
1440 gst_value_set_fraction (&rate, denom, num);
1441 gst_value_list_append_value (&rates, &rate);
1444 } while (ioctl (fd, VIDIOC_ENUM_FRAMEINTERVALS, &ival) >= 0);
1446 /* FIXME MASSIVE UGLY HACK: Pretend to support 30/1 fps always */
1447 gst_value_set_fraction(&rate, 30, 1);
1448 gst_value_list_append_value(&rates, &rate);
1450 } else if (ival.type == V4L2_FRMIVAL_TYPE_STEPWISE) {
1451 GValue min = { 0, };
1452 GValue step = { 0, };
1453 GValue max = { 0, };
1454 gboolean added = FALSE;
1455 guint32 minnum, mindenom;
1456 guint32 maxnum, maxdenom;
1458 g_value_init (&rates, GST_TYPE_LIST);
1460 g_value_init (&min, GST_TYPE_FRACTION);
1461 g_value_init (&step, GST_TYPE_FRACTION);
1462 g_value_init (&max, GST_TYPE_FRACTION);
1465 minnum = ival.stepwise.min.numerator;
1466 mindenom = ival.stepwise.min.denominator;
1467 if (minnum > G_MAXINT || mindenom > G_MAXINT) {
1471 GST_LOG_OBJECT (v4l2camsrc, "stepwise min frame interval: %d/%d", minnum,
1473 gst_value_set_fraction (&min, minnum, mindenom);
1476 maxnum = ival.stepwise.max.numerator;
1477 maxdenom = ival.stepwise.max.denominator;
1478 if (maxnum > G_MAXINT || maxdenom > G_MAXINT) {
1483 GST_LOG_OBJECT (v4l2camsrc, "stepwise max frame interval: %d/%d", maxnum,
1485 gst_value_set_fraction (&max, maxnum, maxdenom);
1488 num = ival.stepwise.step.numerator;
1489 denom = ival.stepwise.step.denominator;
1490 if (num > G_MAXINT || denom > G_MAXINT) {
1495 if (num == 0 || denom == 0) {
1496 /* in this case we have a wrong fraction or no step, set the step to max
1497 * so that we only add the min value in the loop below */
1502 /* since we only have gst_value_fraction_subtract and not add, negate the
1504 GST_LOG_OBJECT (v4l2camsrc, "stepwise step frame interval: %d/%d",
1507 gst_value_set_fraction (&step, -num, denom);
1509 while (gst_value_compare (&min, &max) <= 0) {
1510 GValue rate = { 0, };
1512 num = gst_value_get_fraction_numerator (&min);
1513 denom = gst_value_get_fraction_denominator (&min);
1514 GST_LOG_OBJECT (v4l2camsrc, "adding stepwise framerate: %d/%d",
1517 /* invert to get the framerate */
1518 g_value_init (&rate, GST_TYPE_FRACTION);
1519 gst_value_set_fraction (&rate, denom, num);
1520 gst_value_list_append_value (&rates, &rate);
1523 /* we're actually adding because step was negated above. This is because
1524 * there is no _add function... */
1525 if (!gst_value_fraction_subtract (&min, &min, &step)) {
1526 GST_WARNING_OBJECT (v4l2camsrc, "could not step fraction!");
1531 /* no range was added, leave the default range from the template */
1532 GST_WARNING_OBJECT (v4l2camsrc, "no range added, leaving default");
1533 g_value_unset (&rates);
1535 } else if (ival.type == V4L2_FRMIVAL_TYPE_CONTINUOUS) {
1536 guint32 maxnum, maxdenom;
1538 g_value_init (&rates, GST_TYPE_FRACTION_RANGE);
1540 num = ival.stepwise.min.numerator;
1541 denom = ival.stepwise.min.denominator;
1542 if (num > G_MAXINT || denom > G_MAXINT) {
1547 maxnum = ival.stepwise.max.numerator;
1548 maxdenom = ival.stepwise.max.denominator;
1549 if (maxnum > G_MAXINT || maxdenom > G_MAXINT) {
1554 GST_LOG_OBJECT (v4l2camsrc, "continuous frame interval %d/%d to %d/%d",
1555 maxdenom, maxnum, denom, num);
1557 gst_value_set_fraction_range_full (&rates, maxdenom, maxnum, denom, num);
1563 s = gst_structure_copy (template);
1564 /* https://projects.maemo.org/bugzilla/show_bug.cgi?id=105590 */
1565 gst_structure_set (s,
1566 "width", G_TYPE_INT, (gint) width,
1567 "height", G_TYPE_INT, (gint) height, NULL);
1569 if (G_IS_VALUE (&rates)) {
1570 /* only change the framerate on the template when we have a valid probed new
1572 gst_structure_set_value (s, "framerate", &rates);
1573 g_value_unset (&rates);
1578 enum_frameintervals_failed:
1580 GST_DEBUG_OBJECT (v4l2camsrc,
1581 "Unable to enumerate intervals for %" GST_FOURCC_FORMAT "@%ux%u",
1582 GST_FOURCC_ARGS (pixelformat), width, height);
1587 /* I don't see how this is actually an error, we ignore the format then */
1588 GST_WARNING_OBJECT (v4l2camsrc,
1589 "Unknown frame interval type at %" GST_FOURCC_FORMAT "@%ux%u: %u",
1590 GST_FOURCC_ARGS (pixelformat), width, height, ival.type);
1594 #endif /* defined VIDIOC_ENUM_FRAMEINTERVALS */
1600 compare_resolutions (gconstpointer a, gconstpointer b)
1602 GstStructure *as = (GstStructure *) a;
1603 GstStructure *bs = (GstStructure *) b;
1604 gint aw, bw, ah, bh;
1606 gst_structure_get_int (as, "width", &aw);
1607 gst_structure_get_int (bs, "width", &bw);
1608 gst_structure_get_int (as, "height", &ah);
1609 gst_structure_get_int (bs, "height", &bh);
1611 /* FIXME: What is the best way to compare resolutions if their aspect
1612 * ratio isn't the same? */
1613 return ((aw - bw) + (ah - bh));
1620 fractions_are_equal (gint num1, gint den1, gint num2, gint den2)
1622 GValue fraction1 = { 0, }, fraction2 = {
1625 g_value_init (&fraction1, GST_TYPE_FRACTION);
1626 g_value_init (&fraction2, GST_TYPE_FRACTION);
1627 gst_value_set_fraction (&fraction1, num1, den1);
1628 gst_value_set_fraction (&fraction2, num2, den2);
1629 /* we know we don't have to unset the values in this case */
1630 return (gst_value_compare (&fraction1, &fraction2) == GST_VALUE_EQUAL);
1634 gst_v4l2camsrc_get_frame_size (guint32 fourcc, guint * w, guint * h)
1639 case V4L2_PIX_FMT_YUV420:
1640 outsize = GST_ROUND_UP_4 (*w) * GST_ROUND_UP_2 (*h);
1641 outsize += 2 * ((GST_ROUND_UP_8 (*w) / 2) * (GST_ROUND_UP_2 (*h) / 2));
1643 case V4L2_PIX_FMT_YUYV:
1644 outsize = (GST_ROUND_UP_2 (*w) * 2) * *h;
1646 case V4L2_PIX_FMT_Y41P:
1647 outsize = (GST_ROUND_UP_2 (*w) * 2) * *h;
1649 case V4L2_PIX_FMT_UYVY:
1650 outsize = (GST_ROUND_UP_2 (*w) * 2) * *h;
1652 case V4L2_PIX_FMT_YVU420:
1653 outsize = GST_ROUND_UP_4 (*w) * GST_ROUND_UP_2 (*h);
1654 outsize += 2 * ((GST_ROUND_UP_8 (*w) / 2) * (GST_ROUND_UP_2 (*h) / 2));
1656 case V4L2_PIX_FMT_YUV411P:
1657 outsize = GST_ROUND_UP_4 (*w) * *h;
1658 outsize += 2 * ((GST_ROUND_UP_8 (*w) / 4) * *h);
1660 case V4L2_PIX_FMT_YUV422P:
1661 outsize = GST_ROUND_UP_4 (*w) * *h;
1662 outsize += 2 * ((GST_ROUND_UP_8 (*w) / 2) * *h);
1664 case V4L2_PIX_FMT_NV12:
1665 outsize = GST_ROUND_UP_4 (*w) * GST_ROUND_UP_2 (*h);
1666 outsize += (GST_ROUND_UP_4 (*w) * *h) / 2;
1668 case V4L2_PIX_FMT_NV21:
1669 outsize = GST_ROUND_UP_4 (*w) * GST_ROUND_UP_2 (*h);
1670 outsize += (GST_ROUND_UP_4 (*w) * *h) / 2;
1680 gst_v4l2camsrc_configure_device (GstMFLDV4l2CamSrc * v4l2camsrc,
1681 guint32 * pixelformat, guint * width, guint * height,
1682 guint * fps_n, guint * fps_d)
1684 gint fd = v4l2camsrc->video_fd;
1685 struct v4l2_format format;
1686 struct v4l2_streamparm stream;
1687 gboolean ret = TRUE;
1689 GST_DEBUG_OBJECT (v4l2camsrc, "Configuring device to %dx%d, format "
1690 "%" GST_FOURCC_FORMAT, *width, *height, GST_FOURCC_ARGS (*pixelformat));
1692 GST_V4L2CAMSRC_CHECK_OPEN (v4l2camsrc);
1694 memset (&format, 0x00, sizeof (struct v4l2_format));
1695 format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1697 if (ioctl (fd, VIDIOC_G_FMT, &format) < 0)
1698 goto get_fmt_failed;
1700 if (v4l2camsrc->dump_raw)
1701 format.type = V4L2_BUF_TYPE_PRIVATE;
1703 format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1704 format.fmt.pix.width = *width;
1705 format.fmt.pix.height = *height;
1706 format.fmt.pix.pixelformat = *pixelformat;
1707 /* request whole frames; change when gstreamer supports interlaced video
1708 * (INTERLACED mode returns frames where the fields have already been
1709 * combined, there are other modes for requesting fields individually) */
1710 format.fmt.pix.field = V4L2_FIELD_INTERLACED;
1712 if (ioctl (fd, VIDIOC_S_FMT, &format) < 0) {
1713 if (errno != EINVAL)
1714 goto set_fmt_failed;
1716 /* try again with progressive video */
1717 format.fmt.pix.width = *width;
1718 format.fmt.pix.height = *height;
1719 format.fmt.pix.pixelformat = *pixelformat;
1720 format.fmt.pix.field = V4L2_FIELD_NONE;
1721 if (ioctl (fd, VIDIOC_S_FMT, &format) < 0)
1722 goto set_fmt_failed;
1725 if (v4l2camsrc->dump_raw)
1726 v4l2camsrc->raw_output_size = format.fmt.pix.priv;
1728 if (format.fmt.pix.width != *width || format.fmt.pix.height != *height)
1729 goto invalid_dimensions;
1731 if (format.fmt.pix.pixelformat != *pixelformat)
1732 goto invalid_pixelformat;
1734 memset (&stream, 0x00, sizeof (struct v4l2_streamparm));
1735 stream.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1736 if (ioctl (fd, VIDIOC_G_PARM, &stream) < 0) {
1737 GST_ELEMENT_WARNING (v4l2camsrc, RESOURCE, SETTINGS,
1738 ("Could not get parameters on device '%s'",
1739 v4l2camsrc->videodev), GST_ERROR_SYSTEM);
1743 if (fps_n == NULL || fps_d == NULL) {
1744 GST_LOG_OBJECT (v4l2camsrc, "Framerate will not be set");
1748 GST_LOG_OBJECT (v4l2camsrc, "Desired framerate: %u/%u", *fps_n, *fps_d);
1750 /* Note: V4L2 provides the frame interval, we have the frame rate */
1751 if (stream.parm.capture.timeperframe.denominator &&
1752 fractions_are_equal (stream.parm.capture.timeperframe.numerator,
1753 stream.parm.capture.timeperframe.denominator, *fps_d, *fps_n)) {
1754 GST_LOG_OBJECT (v4l2camsrc, "Desired framerate already set, nothing to do");
1758 /* We want to change the frame rate, so check whether we can. Some cheap USB
1759 * cameras don't have the capability */
1760 if ((stream.parm.capture.capability & V4L2_CAP_TIMEPERFRAME) == 0) {
1761 GST_DEBUG_OBJECT (v4l2camsrc, "Not setting framerate (not supported)");
1765 GST_LOG_OBJECT (v4l2camsrc, "Setting framerate to %u/%u", *fps_n, *fps_d);
1767 /* Note: V4L2 wants the frame interval, we have the frame rate */
1768 stream.parm.capture.timeperframe.numerator = *fps_d;
1769 stream.parm.capture.timeperframe.denominator = *fps_n;
1771 /* some cheap USB cam's won't accept any change */
1772 if (ioctl (fd, VIDIOC_S_PARM, &stream) < 0) {
1773 GST_ELEMENT_WARNING (v4l2camsrc, RESOURCE, SETTINGS,
1774 ("Video input device did not accept new frame rate setting."),
1779 GST_INFO_OBJECT (v4l2camsrc, "Set frame interval to %u/%u",
1780 stream.parm.capture.timeperframe.numerator,
1781 stream.parm.capture.timeperframe.denominator);
1783 cam_set_frame_rate( ((gfloat)stream.parm.capture.timeperframe.denominator / stream.parm.capture.timeperframe.numerator));
1786 v4l2camsrc->max_zoom_factor = 10.0;
1787 v4l2camsrc->frame_byte_size = gst_v4l2camsrc_get_frame_size (*pixelformat,
1789 /* v4l2camsrc->frame_byte_size = GST_ROUND_UP_2 (*width) * 2 * (*height); */
1791 if (v4l2camsrc->bayer_downscaling) {
1792 struct v4l2_crop crop;
1793 memset (&v4l2camsrc->vcrop, 0, sizeof (struct v4l2_cropcap));
1794 v4l2camsrc->vcrop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1795 if (ioctl (v4l2camsrc->video_fd, VIDIOC_CROPCAP, &v4l2camsrc->vcrop) < 0) {
1796 GST_DEBUG_OBJECT (v4l2camsrc, "Failed to query crop cap");
1800 crop.c = v4l2camsrc->vcrop.defrect; /* reset to default */
1801 crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1802 if (-1 == ioctl (fd, VIDIOC_S_CROP, &crop)) {
1805 GST_DEBUG_OBJECT (v4l2camsrc, "Crop not support\n");
1808 GST_DEBUG_OBJECT (v4l2camsrc, "Crop failed\n");
1817 v4l2camsrc->crop_supported = TRUE;
1819 GST_DEBUG_OBJECT (v4l2camsrc, "got cropping bounds: x:%d, y:%d, w:%d, h:%d",
1820 v4l2camsrc->vcrop.bounds.left,
1821 v4l2camsrc->vcrop.bounds.top,
1822 v4l2camsrc->vcrop.bounds.width, v4l2camsrc->vcrop.bounds.height);
1824 GST_DEBUG_OBJECT (v4l2camsrc, "cropping defrect: x:%d, y:%d, w:%d, h:%d",
1825 v4l2camsrc->vcrop.defrect.left,
1826 v4l2camsrc->vcrop.defrect.top,
1827 v4l2camsrc->vcrop.defrect.width, v4l2camsrc->vcrop.defrect.height);
1829 gst_v4l2camsrc_update_cropping (v4l2camsrc, *width, *height, 1.0);
1833 cam_set_capture_fmt (v4l2camsrc->video_fd, *width, *height, *pixelformat);
1840 GST_ELEMENT_ERROR (v4l2camsrc, RESOURCE, SETTINGS,
1841 ("Device '%s' does not support video capture",
1842 v4l2camsrc->videodev),
1843 ("Call to G_FMT failed: (%s)", g_strerror (errno)));
1848 GST_ELEMENT_ERROR (v4l2camsrc, RESOURCE, SETTINGS,
1849 ("Device '%s' cannot capture at %dx%d",
1850 v4l2camsrc->videodev, *width, *height),
1851 ("Call to S_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
1852 GST_FOURCC_ARGS (*pixelformat), *width, *height,
1853 g_strerror (errno)));
1858 GST_ELEMENT_ERROR (v4l2camsrc, RESOURCE, SETTINGS,
1859 ("Device '%s' cannot capture at %dx%d",
1860 v4l2camsrc->videodev, *width, *height),
1861 ("Tried to capture at %dx%d, but device returned size %dx%d",
1862 *width, *height, format.fmt.pix.width, format.fmt.pix.height));
1865 invalid_pixelformat:
1867 GST_ELEMENT_ERROR (v4l2camsrc, RESOURCE, SETTINGS,
1868 ("Device '%s' cannot capture in the specified format",
1869 v4l2camsrc->videodev),
1870 ("Tried to capture in %" GST_FOURCC_FORMAT
1871 ", but device returned format" " %" GST_FOURCC_FORMAT,
1872 GST_FOURCC_ARGS (*pixelformat),
1873 GST_FOURCC_ARGS (format.fmt.pix.pixelformat)));
1878 static gboolean resolution_is_supported(int w, int h)
1880 return (w % 4) == 0 && (h % 4) == 0;
1883 #define probe_and_add_for_w_h(w, h) \
1885 GST_LOG(" w=%lu h=%lu",w,h); \
1886 tmp = gst_v4l2camsrc_probe_caps_for_format_and_size(v4l2camsrc, \
1887 pixelformat, w, h, template); \
1889 res = g_list_prepend(res, tmp); \
1893 static GList *build_caps_list(GstMFLDV4l2CamSrc * v4l2camsrc,
1894 guint32 pixelformat, const GstStructure * template,
1895 struct v4l2_frmsizeenum *size)
1897 GstStructure *tmp = NULL;
1899 long wl,wh,hl,hh,ws,hs;
1901 wl = size->stepwise.min_width; wh = size->stepwise.max_width; ws = size->stepwise.step_width;
1902 hl = size->stepwise.min_height; hh = size->stepwise.max_height; hs = size->stepwise.step_height;
1904 probe_and_add_for_w_h(wl, hl);
1905 probe_and_add_for_w_h(wh, hh);
1907 tmp = gst_structure_copy(template);
1908 gst_structure_set (tmp,
1909 "width", GST_TYPE_INT_RANGE, wl, wh,
1910 "height", GST_TYPE_INT_RANGE, hl, hh,
1912 res = g_list_prepend(res, tmp);
1919 * gst_v4l2camsrc_probe_caps_for_format:
1920 * @v4l2camsrc: #GstMFLDV4l2CamSrc object
1921 * @pixelformat: pixel format fourcc
1922 * @template: GstStructure template to be used for generating the caps
1924 * Creates GstCaps object which describes the supported caps of the device.
1926 * Returns: GStCaps containing supported video resolutions.
1929 gst_v4l2camsrc_probe_caps_for_format (GstMFLDV4l2CamSrc * v4l2camsrc,
1930 guint32 pixelformat, const GstStructure * template)
1932 GstCaps *ret = gst_caps_new_empty ();
1933 gint min_w, max_w, min_h, max_h;
1936 #ifdef VIDIOC_ENUM_FRAMESIZES
1938 gint fd = v4l2camsrc->video_fd;
1939 struct v4l2_frmsizeenum size;
1940 GList *results = NULL;
1943 memset (&size, 0, sizeof (struct v4l2_frmsizeenum));
1945 size.pixel_format = pixelformat;
1947 if (ioctl (fd, VIDIOC_ENUM_FRAMESIZES, &size) < 0)
1948 goto enum_framesizes_failed;
1950 if (size.type == V4L2_FRMSIZE_TYPE_DISCRETE) {
1952 w = MIN (size.discrete.width, G_MAXINT);
1953 h = MIN (size.discrete.height, G_MAXINT);
1955 if(resolution_is_supported(w, h)) {
1956 tmp = gst_v4l2camsrc_probe_caps_for_format_and_size (v4l2camsrc,
1957 pixelformat, w, h, template);
1960 results = g_list_prepend (results, tmp);
1964 } while (ioctl (fd, VIDIOC_ENUM_FRAMESIZES, &size) >= 0);
1965 } else if(size.type == V4L2_FRMSIZE_TYPE_STEPWISE
1966 || size.type == V4L2_FRMSIZE_TYPE_CONTINUOUS) {
1967 results = build_caps_list(v4l2camsrc, pixelformat, template, &size);
1972 /* Sort the resolutions from smallest to largest */
1973 results = g_list_sort (results, compare_resolutions);
1975 while (results != NULL) {
1976 tmp = GST_STRUCTURE (results->data);
1978 gst_caps_merge_structure (ret, tmp);
1980 results = g_list_delete_link (results, results);
1983 if (gst_caps_is_empty (ret))
1984 goto enum_framesizes_no_results;
1989 enum_framesizes_failed:
1991 /* I don't see how this is actually an error */
1992 GST_DEBUG_OBJECT (v4l2camsrc,
1993 "Failed to enumerate frame sizes for pixelformat %" GST_FOURCC_FORMAT
1994 " (%s)", GST_FOURCC_ARGS (pixelformat), g_strerror (errno));
1995 goto default_frame_sizes;
1997 enum_framesizes_no_results:
1999 /* it's possible that VIDIOC_ENUM_FRAMESIZES is defined but the driver in
2000 * question doesn't actually support it yet */
2001 GST_DEBUG_OBJECT (v4l2camsrc, "No results for pixelformat %"
2002 GST_FOURCC_FORMAT " enumerating frame sizes, trying fallback",
2003 GST_FOURCC_ARGS (pixelformat));
2004 goto default_frame_sizes;
2008 GST_WARNING_OBJECT (v4l2camsrc,
2009 "Unknown frame sizeenum type for pixelformat %" GST_FOURCC_FORMAT
2010 ": %u", GST_FOURCC_ARGS (pixelformat), size.type);
2011 goto default_frame_sizes;
2014 #endif /* defined VIDIOC_ENUM_FRAMESIZES */
2016 default_frame_sizes:
2019 max_w = max_h = GST_CAMERA_SRC_MAX_SIZE;
2020 if (!gst_v4l2camsrc_get_nearest_size (v4l2camsrc, pixelformat,
2022 GST_WARNING_OBJECT (v4l2camsrc,
2023 "Could not probe minimum capture size for pixelformat %"
2024 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (pixelformat));
2026 if (!gst_v4l2camsrc_get_nearest_size (v4l2camsrc, pixelformat,
2028 GST_WARNING_OBJECT (v4l2camsrc,
2029 "Could not probe maximum capture size for pixelformat %"
2030 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (pixelformat));
2033 tmp = gst_structure_copy (template);
2034 gst_structure_set (tmp,
2035 "width", GST_TYPE_INT_RANGE, min_w, max_w,
2036 "height", GST_TYPE_INT_RANGE, min_h, max_h, NULL);
2037 gst_caps_append_structure (ret, tmp);
2042 static void draw_corner_markers_nv12(unsigned char *buf, unsigned w, unsigned h)
2045 #define put_dot(x,y) buf[(x)+w*(y)] = (((x+y)&0x01)?0x00:0xff)
2047 for(j = 0; j < 4; ++j)
2048 for(i = 0; i < 8; ++i) {
2049 put_dot(i, j); put_dot(w-1-i, j);
2050 put_dot(j, i); put_dot(j, h-1-i);
2051 put_dot(i, h-1-j); put_dot(w-1-i, h-1-j);
2052 put_dot(w-1-j, i); put_dot(w-1-j, h-1-i);
2057 /* Crop to upper left corner of NV12 frame. */
2058 static gboolean crop_buffer_inplace_nv12(unsigned char *buf, unsigned in_w, unsigned in_h,
2059 unsigned out_w, unsigned out_h)
2062 unsigned char *in, *out;
2064 if(in_w < out_w || in_h < out_h
2065 || in_w % 4 || out_w % 4
2066 || in_h % 4 || out_h % 4)
2069 in = buf; out = buf;
2070 for(rows = 0; rows < out_h; ++rows) {
2071 memmove(out, in, out_w);
2075 in = buf + in_w * in_h;
2076 for(rows = 0; rows < out_h / 2; ++rows) {
2077 memmove(out, in, out_w);
2086 * gst_v4l2camsrc_libmfld_grab_hq_frame:
2087 * @v4l2camsrc: #GstMFLDV4l2CamSrc object
2088 * @buf: Pointer to v4l2_buffer where to place the image frame
2090 * Grab a High-Quality image by using libmfldcam.
2092 * Returns: TRUE if capture succeeds, FALSE otherwise
2095 gst_v4l2camsrc_libmfld_grab_hq_frame (GstMFLDV4l2CamSrc * v4l2camsrc,
2096 struct v4l2_buffer * buffer)
2100 struct cam_capture_settings st;
2102 st.mmap = v4l2camsrc->use_mmap;
2103 st.ae = v4l2camsrc->ae_enabled;
2104 st.af = v4l2camsrc->af_enabled;
2105 st.awb = v4l2camsrc->awb_enabled;
2106 st.dump_raw = v4l2camsrc->dump_raw;
2107 st.raw_output_size = v4l2camsrc->raw_output_size;
2108 st.dump_image = v4l2camsrc->dump_image;
2110 err = cam_capture_frames (v4l2camsrc->video_fd, buffer, &st);
2111 if (v4l2camsrc->ae_enabled || v4l2camsrc->af_enabled ||
2112 v4l2camsrc->awb_enabled)
2113 mfldcam_3a_start ();
2114 ret = (err == CAM_ERR_NONE);
2119 /******************************************************
2120 * gst_v4l2camsrc_grab_frame ():
2121 * grab a frame for capturing
2122 * return value: GST_FLOW_OK, GST_FLOW_WRONG_STATE or GST_FLOW_ERROR
2123 ******************************************************/
2125 gst_v4l2camsrc_grab_frame (GstCameraSrc * camsrc, GstBuffer ** buf,
2126 GstCameraCapturePhase phase)
2128 GstMFLDV4l2CamSrc *v4l2camsrc = GST_V4L2CAMSRC (camsrc);
2130 #define NUM_TRIALS 5000
2131 struct v4l2_buffer buffer;
2132 gint32 trials = NUM_TRIALS;
2133 GstBuffer *pool_buffer;
2134 gboolean need_copy = v4l2camsrc->use_copy;
2135 gboolean variable_frame_size = FALSE; /* FIXME if we ever support JPEG etc */
2140 /* wait if all buffers are DQBuf */
2141 g_mutex_lock (v4l2camsrc->device_mutex);
2142 gst_v4l2_buffer_pool_update (v4l2camsrc->pool, v4l2camsrc);
2143 g_mutex_unlock (v4l2camsrc->device_mutex);
2145 memset (&buffer, 0x00, sizeof (buffer));
2146 buffer.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2148 if (v4l2camsrc->use_mmap)
2149 buffer.memory = V4L2_MEMORY_MMAP;
2151 buffer.memory = V4L2_MEMORY_USERPTR;
2153 if (phase == GST_CAMERA_CAPTURE) {
2154 gst_camerasrc_send_capture_start_message (camsrc);
2158 ret = gst_poll_wait (v4l2camsrc->poll, V4L2CAMSRC_POLL_TIMEOUT);
2159 if (G_UNLIKELY (ret < 0)) {
2162 if (errno != EAGAIN && errno != EINTR)
2166 if (G_UNLIKELY (ret == 0)) {
2169 g_mutex_lock (v4l2camsrc->device_mutex);
2170 ret = gst_v4l2camsrc_libmfld_grab_hq_frame (v4l2camsrc, &buffer);
2171 g_mutex_unlock (v4l2camsrc->device_mutex);
2176 GST_WARNING_OBJECT (v4l2camsrc,
2177 "problem grabbing frame %d (ix=%d), trials=%d, pool-ct=%d, buf.flags=%d",
2178 buffer.sequence, buffer.index, trials,
2179 GST_MINI_OBJECT_REFCOUNT (v4l2camsrc->pool), buffer.flags);
2181 /* if the sync() got interrupted, we can retry */
2184 GST_WARNING_OBJECT (v4l2camsrc,
2185 "Non-blocking I/O has been selected using O_NONBLOCK and"
2186 " no buffer was in the outgoing queue. device %s",
2187 v4l2camsrc->videodev);
2194 GST_INFO_OBJECT (v4l2camsrc,
2195 "VIDIOC_DQBUF failed due to an internal error."
2196 " Can also indicate temporary problems like signal loss."
2197 " Note the driver might dequeue an (empty) buffer despite"
2198 " returning an error, or even stop capturing."
2199 " device %s", v4l2camsrc->videodev);
2200 /* have we de-queued a buffer ? */
2201 if (!(buffer.flags & (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE))) {
2203 if ((buffer.index >= 0) && (buffer.index < v4l2camsrc->breq.count)) {
2204 GST_DEBUG_OBJECT (v4l2camsrc, "reenqueing buffer (ix=%ld)", buffer.index);
2205 gst_v4l2camsrc_queue_frame (v4l2camsrc, buffer.index);
2209 GST_DEBUG_OBJECT (v4l2camsrc, "reenqueing buffer");
2210 /* FIXME: this is not a good idea, as drivers usualy return the buffer
2211 * with index-number set to 0, thus the re-enque will fail unless it
2212 * was incidentialy 0.
2213 * We could try to re-enque all buffers without handling the ioctl
2217 if (ioctl (v4l2camsrc->video_fd, VIDIOC_QBUF, &buffer) < 0) {
2225 GST_WARNING_OBJECT (v4l2camsrc,
2226 "could not sync on a buffer on device %s", v4l2camsrc->videodev);
2229 GST_WARNING_OBJECT (v4l2camsrc,
2230 "Grabbing frame got interrupted on %s. No expected reason.",
2231 v4l2camsrc->videodev);
2235 /* check nr. of attempts to capture */
2236 if (--trials == -1) {
2237 goto too_many_trials;
2239 memset (&buffer, 0x00, sizeof (buffer));
2240 buffer.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2241 if (v4l2camsrc->use_mmap)
2242 buffer.memory = V4L2_MEMORY_MMAP;
2244 buffer.memory = V4L2_MEMORY_USERPTR;
2248 if (phase == GST_CAMERA_CAPTURE) {
2249 gst_camerasrc_send_capture_stop_message (camsrc);
2251 /* Send the CAF status message */
2252 GstCameraFocusStatus focus_status;
2254 lret = gst_v4l2camsrc_check_focus_status(v4l2camsrc, &focus_status, FALSE);
2256 GST_LOG_OBJECT (v4l2camsrc, "CAF status: %d", focus_status.status);
2257 if (focus_status.status == GST_PHOTOGRAPHY_FOCUS_STATUS_SUCCESS )
2258 gst_camerasrc_send_af_status(GST_CAMERA_SRC (v4l2camsrc) ,CAMERASRC_AUTO_FOCUS_RESULT_FOCUSED);
2259 else if (focus_status.status == GST_PHOTOGRAPHY_FOCUS_STATUS_FAIL )
2260 gst_camerasrc_send_af_status(GST_CAMERA_SRC (v4l2camsrc) ,CAMERASRC_AUTO_FOCUS_RESULT_FAILED);
2262 GST_WARNING_OBJECT (v4l2camsrc, "CAF status erronous: %d send as failed", focus_status.status);
2263 gst_camerasrc_send_af_status(GST_CAMERA_SRC (v4l2camsrc) ,CAMERASRC_AUTO_FOCUS_RESULT_FAILED);
2269 g_mutex_lock (v4l2camsrc->pool->lock);
2271 index = buffer.index;
2273 /* get our GstBuffer with that index from the pool, if the buffer is
2274 * in use (NULL) we have a serious problem (should only be possible in mmap
2276 pool_buffer = GST_BUFFER (v4l2camsrc->pool->buffers[index]);
2278 if (G_UNLIKELY (pool_buffer == NULL))
2281 GST_LOG_OBJECT (v4l2camsrc, "grabbed buffer %p at index %d (refct = %d)",
2282 pool_buffer, index, GST_MINI_OBJECT_REFCOUNT (pool_buffer));
2284 /* ref the buffer and requeue, when if becomes writable again */
2285 v4l2camsrc->pool->buffers[index] = NULL;
2286 v4l2camsrc->pool->num_live_buffers++;
2287 v4l2camsrc->pool->queued[index] = 0;
2289 /* SW workaround for ISP padding. */
2291 if(v4l2camsrc->expected_capture_w < 320 && (v4l2camsrc->expected_capture_w % 32 != 0)
2292 && v4l2camsrc->expected_capture_fourcc == V4L2_PIX_FMT_NV12
2293 && !v4l2camsrc->disable_low_res_crop) {
2295 real_w = (v4l2camsrc->expected_capture_w & ~31) + 32;
2297 GST_LOG_OBJECT(v4l2camsrc, "Downscaling padded buffer with cropping (%u,%u) -> (%u,%u)",
2298 real_w, v4l2camsrc->expected_capture_h, v4l2camsrc->expected_capture_w, v4l2camsrc->expected_capture_h);
2300 if(!crop_buffer_inplace_nv12(GST_BUFFER_DATA(pool_buffer),
2301 real_w, v4l2camsrc->expected_capture_h,
2302 v4l2camsrc->expected_capture_w, v4l2camsrc->expected_capture_h)) {
2303 GST_ERROR_OBJECT(v4l2camsrc,"Software crop failed");
2307 if(v4l2camsrc->draw_corner_markers)
2308 draw_corner_markers_nv12(GST_BUFFER_DATA(pool_buffer),
2309 v4l2camsrc->expected_capture_w, v4l2camsrc->expected_capture_h);
2311 g_mutex_unlock (v4l2camsrc->pool->lock);
2313 /* this can change at every frame, esp. with jpeg */
2314 if(variable_frame_size)
2315 GST_BUFFER_SIZE (pool_buffer) = buffer.bytesused;
2317 GST_BUFFER_SIZE (pool_buffer) = v4l2camsrc->frame_byte_size;
2319 GST_BUFFER_OFFSET (pool_buffer) = v4l2camsrc->offset++;
2320 GST_BUFFER_OFFSET_END (pool_buffer) = v4l2camsrc->offset;
2322 /* timestamps, LOCK to get clock and base time. */
2325 GstClockTime timestamp;
2327 GST_OBJECT_LOCK (v4l2camsrc);
2328 if ((clock = GST_ELEMENT_CLOCK (v4l2camsrc))) {
2329 /* we have a clock, get base time and ref clock */
2330 timestamp = GST_ELEMENT (v4l2camsrc)->base_time;
2331 gst_object_ref (clock);
2333 /* no clock, can't set timestamps */
2334 timestamp = GST_CLOCK_TIME_NONE;
2336 GST_OBJECT_UNLOCK (v4l2camsrc);
2339 GstClockTime latency;
2341 /* the time now is the time of the clock minus the base time */
2342 timestamp = gst_clock_get_time (clock) - timestamp;
2343 gst_object_unref (clock);
2346 gst_util_uint64_scale_int (GST_SECOND, camsrc->fps_d, camsrc->fps_n);
2348 if (timestamp > latency)
2349 timestamp -= latency;
2352 GST_BUFFER_DURATION (pool_buffer) = latency;
2354 /* activate settings for next frame (Code from v4l2src) */
2355 if (GST_CLOCK_TIME_IS_VALID (camsrc->duration)) {
2356 v4l2camsrc->ctrl_time += camsrc->duration;
2358 v4l2camsrc->ctrl_time = timestamp;
2360 gst_object_sync_values (G_OBJECT (camsrc), v4l2camsrc->ctrl_time);
2362 /* FIXME: use the timestamp from the buffer itself! */
2363 GST_BUFFER_TIMESTAMP (pool_buffer) = timestamp;
2366 if (G_UNLIKELY (need_copy)) {
2367 GST_DEBUG_OBJECT (v4l2camsrc, "copying buffer");
2368 *buf = gst_buffer_copy (pool_buffer);
2369 GST_BUFFER_FLAG_UNSET (*buf, GST_BUFFER_FLAG_READONLY);
2370 /* this will requeue */
2371 gst_buffer_unref (pool_buffer);
2373 if (v4l2camsrc->pool->is_vaapi_sharing)
2374 GST_BUFFER_DATA(pool_buffer) = (GST_V4L2CAMSRC_BUFFER(pool_buffer))->gbuffer;
2378 GST_LOG_OBJECT (v4l2camsrc,
2379 "grabbed frame %d (ix=%d), flags %08x, pool-ct=%d", buffer.sequence,
2380 buffer.index, buffer.flags, v4l2camsrc->pool->num_live_buffers);
2387 GST_ELEMENT_ERROR (v4l2camsrc, RESOURCE, READ, (NULL),
2388 ("select error %d: %s (%d)", ret, g_strerror (errno), errno));
2389 return GST_FLOW_ERROR;
2393 GST_DEBUG ("stop called");
2394 return GST_FLOW_WRONG_STATE;
2398 GST_ELEMENT_ERROR (v4l2camsrc, RESOURCE, FAILED,
2399 ("Failed trying to get video frames from device '%s'.",
2400 v4l2camsrc->videodev),
2401 ("The buffer type is not supported, or the index is out of bounds,"
2402 " or no buffers have been allocated yet, or the userptr"
2403 " or length are invalid. device %s", v4l2camsrc->videodev));
2404 return GST_FLOW_ERROR;
2408 GST_ELEMENT_ERROR (v4l2camsrc, RESOURCE, FAILED,
2409 ("Failed trying to get video frames from device '%s'. Not enough memory.",
2410 v4l2camsrc->videodev), ("insufficient memory to enqueue a user "
2411 "pointer buffer. device %s.", v4l2camsrc->videodev));
2412 return GST_FLOW_ERROR;
2416 GST_ELEMENT_ERROR (v4l2camsrc, RESOURCE, FAILED,
2417 ("Failed trying to get video frames from device '%s'.",
2418 v4l2camsrc->videodev),
2419 ("Failed after %d tries. device %s. system error: %s",
2420 NUM_TRIALS, v4l2camsrc->videodev, g_strerror (errno)));
2421 return GST_FLOW_ERROR;
2425 GST_ELEMENT_ERROR (v4l2camsrc, RESOURCE, FAILED,
2426 ("Failed trying to get video frames from device '%s'.",
2427 v4l2camsrc->videodev),
2428 ("No free buffers found in the pool at index %d.", index));
2429 g_mutex_unlock (v4l2camsrc->pool->lock);
2430 return GST_FLOW_ERROR;
2435 GST_ELEMENT_ERROR (v4l2camsrc, RESOURCE, WRITE,
2436 ("Could not exchange data with device '%s'.",
2437 v4l2camsrc->videodev),
2438 ("Error queueing buffer on device %s. system error: %s",
2439 v4l2camsrc->videodev, g_strerror (errno)));
2440 return GST_FLOW_ERROR;
2445 GST_ELEMENT_ERROR (v4l2camsrc, RESOURCE, FAILED,
2446 ("Timeout when trying to get video frames from device '%s'.",
2447 v4l2camsrc->videodev), NULL);
2448 return GST_FLOW_ERROR;
2453 /******************************************************
2454 * gst_v4l2camsrc_set_capture():
2455 * set capture parameters for certain operation mode
2456 * return value: TRUE on success, FALSE on error
2457 ******************************************************/
2459 gst_v4l2camsrc_set_capture (GstCameraSrc * camsrc, GstOperationMode mode,
2460 gboolean try_only, guint32 * pixelformat, guint * width, guint * height,
2461 guint * fps_n, guint * fps_d)
2463 GstMFLDV4l2CamSrc *v4l2camsrc = GST_V4L2CAMSRC (camsrc);
2464 cam_err_t err = CAM_ERR_NONE;
2465 gboolean ret = TRUE;
2467 GST_DEBUG_OBJECT (v4l2camsrc, "mode = %d, try_only = %d", mode, try_only);
2469 v4l2camsrc->expected_capture_w = *width;
2470 v4l2camsrc->expected_capture_h = *height;
2471 v4l2camsrc->expected_capture_fourcc = *pixelformat;
2473 if (mode == GST_PHOTOGRAPHY_OPERATION_MODE_PREVIEW) {
2475 } else if (mode == GST_PHOTOGRAPHY_OPERATION_MODE_IMAGE_CAPTURE) {
2476 GST_DEBUG_OBJECT (v4l2camsrc, "Image capture: %dx%d, format "
2477 "%" GST_FOURCC_FORMAT, *width, *height, GST_FOURCC_ARGS (*pixelformat));
2480 /* Round width & height down to be dividable by 8 */
2481 *width = (*width) & ~7;
2482 *height = (*height) & ~7;
2485 ret = gst_v4l2camsrc_configure_device (v4l2camsrc, pixelformat,
2486 width, height, fps_n, fps_d);
2488 v4l2camsrc->tmp_num_buffers = v4l2camsrc->num_buffers;
2489 v4l2camsrc->num_buffers = 1;
2491 v4l2camsrc->capture_w = *width;
2492 v4l2camsrc->capture_h = *height;
2493 if (fps_n && fps_d) {
2494 v4l2camsrc->capture_fps_n = *fps_n;
2495 v4l2camsrc->capture_fps_d = *fps_d;
2497 v4l2camsrc->capture_fourcc = *pixelformat;
2500 /* VIEWFINDER mode */
2501 /* FIXME: handle try_only mode */
2503 v4l2camsrc->num_buffers = v4l2camsrc->tmp_num_buffers;
2505 ret = gst_v4l2camsrc_configure_device (v4l2camsrc, pixelformat,
2506 width, height, fps_n, fps_d);
2508 v4l2camsrc->vf_w = *width;
2509 v4l2camsrc->vf_h = *height;
2510 v4l2camsrc->vf_fourcc = *pixelformat;
2511 if (fps_n && fps_d) {
2512 v4l2camsrc->vf_fps_n = *fps_n;
2513 v4l2camsrc->vf_fps_d = *fps_d;
2516 if (err != CAM_ERR_NONE)
2522 /******************************************************
2523 * gst_v4l2camsrc_capture_start():
2524 * Start capturing frames from the device
2525 * return value: TRUE on success, FALSE on error
2526 ******************************************************/
2528 gst_v4l2camsrc_capture_start (GstCameraSrc * camsrc, GstCaps * caps)
2530 GstMFLDV4l2CamSrc *v4l2camsrc = GST_V4L2CAMSRC (camsrc);
2532 gint fd = v4l2camsrc->video_fd;
2533 struct v4l2_requestbuffers breq;
2534 gint type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2535 cam_capture_mode_t cam_mode;
2538 v4l2camsrc->offset = 0;
2540 /* activate settings for first frame */
2541 v4l2camsrc->ctrl_time = 0;
2542 gst_object_sync_values (G_OBJECT (camsrc), v4l2camsrc->ctrl_time);
2544 memset (&breq, 0, sizeof (struct v4l2_requestbuffers));
2546 GST_DEBUG_OBJECT (v4l2camsrc, "initializing the capture system");
2548 GST_V4L2CAMSRC_CHECK_OPEN (v4l2camsrc);
2549 GST_V4L2CAMSRC_CHECK_NOT_ACTIVE (v4l2camsrc);
2551 if (!(v4l2camsrc->vcap.capabilities & V4L2_CAP_STREAMING))
2552 goto no_capture_method;
2554 GST_DEBUG_OBJECT (v4l2camsrc, "STREAMING, requesting %d CAPTURE buffers",
2555 v4l2camsrc->num_buffers);
2557 err = cam_driver_set_mipi_interrupt(fd,1);
2558 if (err != CAM_ERR_NONE)
2559 GST_DEBUG_OBJECT (v4l2camsrc, "MIPI interrupt enable FAILED !");
2561 breq.count = v4l2camsrc->num_buffers;
2562 breq.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2563 if (v4l2camsrc->use_mmap)
2564 breq.memory = V4L2_MEMORY_MMAP;
2566 breq.memory = V4L2_MEMORY_USERPTR;
2568 if (ioctl (fd, VIDIOC_REQBUFS, &breq) < 0)
2569 goto reqbufs_failed;
2571 GST_LOG_OBJECT (v4l2camsrc, " count: %u", breq.count);
2572 GST_LOG_OBJECT (v4l2camsrc, " type: %d", breq.type);
2573 GST_LOG_OBJECT (v4l2camsrc, " memory: %d", breq.memory);
2575 if (breq.count < GST_V4L2CAMSRC_MIN_BUFFERS)
2578 if (v4l2camsrc->num_buffers != breq.count) {
2579 GST_WARNING_OBJECT (v4l2camsrc, "using %u buffers instead", breq.count);
2580 v4l2camsrc->num_buffers = breq.count;
2581 g_object_notify (G_OBJECT (v4l2camsrc), "queue-size");
2584 /* Map the buffers */
2585 GST_LOG_OBJECT (v4l2camsrc, "initiating buffer pool");
2587 if (!(v4l2camsrc->pool =
2588 gst_v4l2camsrc_buffer_pool_new (v4l2camsrc, fd, caps)))
2589 goto buffer_pool_new_failed;
2591 GST_INFO_OBJECT (v4l2camsrc, "capturing buffers");
2593 GST_V4L2CAMSRC_SET_ACTIVE (v4l2camsrc);
2595 GST_DEBUG_OBJECT (v4l2camsrc, "starting the capturing");
2596 GST_V4L2CAMSRC_CHECK_ACTIVE (v4l2camsrc);
2598 if (!gst_v4l2camsrc_buffer_pool_activate (v4l2camsrc->pool, v4l2camsrc))
2599 goto pool_activate_failed;
2601 if (ioctl (fd, VIDIOC_STREAMON, &type) < 0)
2602 goto streamon_failed;
2604 v4l2camsrc->is_active = TRUE;
2605 GST_DEBUG_OBJECT (v4l2camsrc, "STREAMON called");
2607 /* Update the focus only when the Sensor is stream on */
2608 if (v4l2camsrc->focus_updated) {
2609 cam_driver_set_focus_posi(v4l2camsrc->video_fd, v4l2camsrc->focus_posi);
2610 v4l2camsrc->focus_updated = FALSE;
2613 if (v4l2camsrc->zoom_updated) {
2614 gst_v4l2camsrc_libmfldcam_set_zoom(v4l2camsrc, v4l2camsrc->zoom_factor);
2615 v4l2camsrc->zoom_updated = FALSE;
2618 cam_mode = find_item (gst_v4l2camsrc_capture_map,v4l2camsrc->capture_mode);
2619 cam_set_capture_mode (v4l2camsrc->video_fd, cam_mode);
2626 GST_ELEMENT_ERROR (v4l2camsrc, RESOURCE, READ,
2627 ("Could not get buffers from device '%s'.",
2628 v4l2camsrc->videodev),
2629 ("error requesting %d buffers: %s",
2630 v4l2camsrc->num_buffers, g_strerror (errno)));
2631 cam_driver_set_mipi_interrupt(fd, 0);
2636 GST_ELEMENT_ERROR (v4l2camsrc, RESOURCE, READ,
2637 ("Could not get enough buffers from device '%s'.",
2638 v4l2camsrc->videodev),
2639 ("we received %d from device '%s', we want at least %d",
2640 breq.count, v4l2camsrc->videodev, GST_V4L2CAMSRC_MIN_BUFFERS));
2641 cam_driver_set_mipi_interrupt(fd, 0);
2644 buffer_pool_new_failed:
2646 GST_ELEMENT_ERROR (v4l2camsrc, RESOURCE, READ,
2647 ("Could not map buffers from device '%s'",
2648 v4l2camsrc->videodev),
2649 ("Failed to create buffer pool: %s", g_strerror (errno)));
2650 cam_driver_set_mipi_interrupt(fd, 0);
2655 GST_ELEMENT_ERROR (v4l2camsrc, RESOURCE, READ,
2656 ("The driver of device '%s' does not support streaming. ",
2657 v4l2camsrc->videodev), (NULL));
2658 cam_driver_set_mipi_interrupt(fd, 0);
2661 pool_activate_failed:
2663 /* already errored */
2664 cam_driver_set_mipi_interrupt(fd, 0);
2669 GST_ELEMENT_ERROR (v4l2camsrc, RESOURCE, OPEN_READ,
2670 ("Error starting streaming capture from device '%s'.",
2671 v4l2camsrc->videodev), GST_ERROR_SYSTEM);
2672 cam_driver_set_mipi_interrupt(fd, 0);
2677 /******************************************************
2678 * gst_v4l2camsrc_capture_stop():
2679 * stop streaming capture
2680 * return value: TRUE on success, FALSE on error
2681 ******************************************************/
2683 gst_v4l2camsrc_capture_stop (GstCameraSrc * camsrc)
2685 GstMFLDV4l2CamSrc *v4l2camsrc = GST_V4L2CAMSRC (camsrc);
2686 struct v4l2_requestbuffers breq;
2687 gint fd = v4l2camsrc->video_fd;
2690 gint type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2692 memset(&breq, 0, sizeof(struct v4l2_requestbuffers));
2694 GST_DEBUG_OBJECT (v4l2camsrc, "stopping capturing");
2696 if (!GST_V4L2CAMSRC_IS_OPEN (v4l2camsrc)) {
2699 if (!GST_V4L2CAMSRC_IS_ACTIVE (v4l2camsrc)) {
2703 err = cam_driver_set_mipi_interrupt(fd,0);
2704 if (err != CAM_ERR_NONE)
2705 GST_DEBUG_OBJECT (v4l2camsrc, "MIPI interrupt disable FAILED !");
2707 /* we actually need to sync on all queued buffers but not
2708 * on the non-queued ones */
2709 if (ioctl (v4l2camsrc->video_fd, VIDIOC_STREAMOFF, &type) < 0)
2710 goto streamoff_failed;
2712 GST_DEBUG_OBJECT (v4l2camsrc, "STREAMOFF called");
2715 breq.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2716 if(v4l2camsrc->use_mmap)
2717 breq.memory = V4L2_MEMORY_MMAP;
2719 breq.memory = V4L2_MEMORY_USERPTR;
2721 GST_DEBUG_OBJECT (v4l2camsrc, "REQUEST 0 to finalize buffer");
2723 if(ioctl(v4l2camsrc->video_fd, VIDIOC_REQBUFS, &breq) < 0)
2724 GST_DEBUG_OBJECT (v4l2camsrc, "Failed to REQUEST 0 to finalize buffer");
2727 if (v4l2camsrc->pool) {
2728 gst_v4l2camsrc_buffer_pool_destroy (v4l2camsrc->pool, v4l2camsrc);
2729 v4l2camsrc->pool = NULL;
2732 GST_V4L2CAMSRC_SET_INACTIVE (v4l2camsrc);
2733 v4l2camsrc->is_active = FALSE;
2741 GST_ELEMENT_ERROR (v4l2camsrc, RESOURCE, CLOSE,
2742 ("Error stopping streaming capture from device '%s'.",
2743 v4l2camsrc->videodev), GST_ERROR_SYSTEM);
2751 gst_v4l2camsrc_get_nearest_size (GstMFLDV4l2CamSrc * v4l2camsrc,
2752 guint32 pixelformat, gint * width, gint * height)
2754 struct v4l2_format fmt;
2758 g_return_val_if_fail (width != NULL, FALSE);
2759 g_return_val_if_fail (height != NULL, FALSE);
2761 GST_LOG_OBJECT (v4l2camsrc,
2762 "getting nearest size to %dx%d with format %" GST_FOURCC_FORMAT,
2763 *width, *height, GST_FOURCC_ARGS (pixelformat));
2765 fd = v4l2camsrc->video_fd;
2767 /* get size delimiters */
2768 memset (&fmt, 0, sizeof (fmt));
2769 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2770 fmt.fmt.pix.width = *width;
2771 fmt.fmt.pix.height = *height;
2772 fmt.fmt.pix.pixelformat = pixelformat;
2773 fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
2775 /* FIXME: Disable TRY_FMT check, driver is not returning anything useful yet */
2777 r = ioctl (fd, VIDIOC_TRY_FMT, &fmt);
2778 if (r < 0 && errno == EINVAL) {
2779 /* try again with progressive video */
2780 fmt.fmt.pix.width = *width;
2781 fmt.fmt.pix.height = *height;
2782 fmt.fmt.pix.pixelformat = pixelformat;
2783 fmt.fmt.pix.field = V4L2_FIELD_NONE;
2784 r = ioctl (fd, VIDIOC_TRY_FMT, &fmt);
2788 /* The driver might not implement TRY_FMT, in which case we will try
2790 if (errno != ENOTTY)
2794 /* Only try S_FMT if we're not actively capturing yet, which we shouldn't
2795 be, because we're still probing */
2796 if (GST_V4L2CAMSRC_IS_ACTIVE (v4l2camsrc))
2800 GST_LOG_OBJECT (v4l2camsrc,
2801 "Failed to probe size limit with VIDIOC_TRY_FMT, trying VIDIOC_S_FMT");
2804 fmt.fmt.pix.width = *width;
2805 fmt.fmt.pix.height = *height;
2807 r = ioctl (fd, VIDIOC_S_FMT, &fmt);
2808 if (r < 0 && errno == EINVAL) {
2809 /* try again with progressive video */
2810 fmt.fmt.pix.width = *width;
2811 fmt.fmt.pix.height = *height;
2812 fmt.fmt.pix.pixelformat = pixelformat;
2813 fmt.fmt.pix.field = V4L2_FIELD_NONE;
2814 r = ioctl (fd, VIDIOC_S_FMT, &fmt);
2823 GST_LOG_OBJECT (v4l2camsrc,
2824 "got nearest size %dx%d", fmt.fmt.pix.width, fmt.fmt.pix.height);
2826 *width = fmt.fmt.pix.width;
2827 *height = fmt.fmt.pix.height;
2833 /******************************************************
2834 * gst_v4l2camsrc_set_crop():
2835 * set cropping bounds
2836 * return value: TRUE on success, FALSE on error
2837 ******************************************************/
2839 gst_v4l2camsrc_set_crop (GstMFLDV4l2CamSrc * v4l2camsrc,
2840 gint x, gint y, gint w, gint h)
2842 gboolean ret = FALSE;
2843 struct v4l2_crop scrop;
2844 struct v4l2_crop gcrop;
2845 gint cx, cy, cw, ch;
2848 fd = v4l2camsrc->video_fd;
2849 cx = v4l2camsrc->vcrop.bounds.left;
2850 cy = v4l2camsrc->vcrop.bounds.top;
2851 cw = v4l2camsrc->vcrop.bounds.width;
2852 ch = v4l2camsrc->vcrop.bounds.height;
2854 memset (&scrop, 0, sizeof (scrop));
2855 scrop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2857 /* Make sure that cropping area stays inside the crop boundaries */
2858 scrop.c.left = x > cx ? x : cx;
2859 scrop.c.top = y > cy ? y : cy;
2860 scrop.c.width = (scrop.c.left + w) < (cx + cw) ? w : cx + cw - scrop.c.left;
2861 scrop.c.height = (scrop.c.top + h) < (cy + ch) ? h : cy + ch - scrop.c.top;
2863 GST_DEBUG_OBJECT (v4l2camsrc,
2864 "Crop request: left = %d, top = %d, w = %d, h = %d",
2865 scrop.c.left, scrop.c.top, scrop.c.width, scrop.c.height);
2867 g_mutex_lock (v4l2camsrc->device_mutex);
2869 if (-1 == ioctl (fd, VIDIOC_S_CROP, &scrop) && errno != EINVAL) {
2873 if (-1 == ioctl (fd, VIDIOC_G_CROP, &gcrop)) {
2877 g_mutex_unlock (v4l2camsrc->device_mutex);
2879 GST_DEBUG_OBJECT (v4l2camsrc,
2880 "Crop selected: left = %d, top = %d, w = %d, h = %d",
2881 gcrop.c.left, gcrop.c.top, gcrop.c.width, gcrop.c.height);
2884 if (gcrop.c.left != scrop.c.left || gcrop.c.top != scrop.c.top ||
2885 gcrop.c.width != scrop.c.width || gcrop.c.height != scrop.c.height) {
2886 goto crop_not_supported;
2898 g_mutex_unlock (v4l2camsrc->device_mutex);
2899 GST_DEBUG_OBJECT (v4l2camsrc, "VIDIOC_S_CROP not supported");
2903 g_mutex_unlock (v4l2camsrc->device_mutex);
2904 GST_DEBUG_OBJECT (v4l2camsrc, "VIDIOC_G_CROP not supported");
2909 g_mutex_unlock (v4l2camsrc->device_mutex);
2910 GST_DEBUG_OBJECT (v4l2camsrc, "Given crop value not accepted");
2916 /******************************************************
2917 * gst_v4l2camsrc_update_cropping():
2918 * update cropping area according to width, height and zoom factors.
2919 * return value: TRUE on success, FALSE on error
2920 ******************************************************/
2922 gst_v4l2camsrc_update_cropping (GstMFLDV4l2CamSrc * v4l2camsrc, gint width,
2923 gint height, gfloat zoom)
2925 gfloat sensor_ar, reso_ar;
2926 gint crop_x, crop_y, crop_w, crop_h;
2928 g_return_val_if_fail (width != 0, FALSE);
2929 g_return_val_if_fail (height != 0, FALSE);
2930 g_return_val_if_fail (zoom != 0, FALSE);
2932 if (zoom < 1.0 || zoom > v4l2camsrc->max_zoom_factor) {
2933 GST_DEBUG_OBJECT (v4l2camsrc, "invalid zoom = %.2f", zoom);
2936 if (!v4l2camsrc->crop_supported) {
2937 GST_DEBUG_OBJECT (v4l2camsrc, "crop not supported");
2941 sensor_ar = v4l2camsrc->vcrop.defrect.width /
2942 v4l2camsrc->vcrop.defrect.height;
2943 reso_ar = width / height;
2945 if (sensor_ar > reso_ar) {
2946 crop_w = (width * v4l2camsrc->vcrop.defrect.height / height) / zoom;
2947 crop_h = MAX (v4l2camsrc->vcrop.defrect.height, height) / zoom;
2949 crop_w = MAX (v4l2camsrc->vcrop.defrect.width, width) / zoom;
2950 crop_h = (height * v4l2camsrc->vcrop.defrect.width / width) / zoom;
2953 crop_x = ABS ((v4l2camsrc->vcrop.defrect.width - crop_w) / 2);
2954 crop_y = ABS ((v4l2camsrc->vcrop.defrect.height - crop_h) / 2);
2956 GST_LOG_OBJECT (v4l2camsrc, "set cropping: x: %d, y: %d, w: %d, h: %d",
2957 crop_x, crop_y, crop_w, crop_h);
2959 return gst_v4l2camsrc_set_crop (v4l2camsrc, crop_x, crop_y, crop_w, crop_h);
2962 /***************************************************************************
2963 * New MFLD Camera Code ****************************************************
2964 * *************************************************************************/
2966 /* These map tables help to adapt the general photography interface to
2967 * different device implementations.
2969 * Table index = Enumeration value from GstPhotography
2970 * Table value = Device/Implementation -specific setting value
2973 static const gint gst_v4l2camsrc_scene_map[] = {
2974 999, /* GST_PHOTOGRAPHY_SCENE_MODE_MANUAL */
2975 CAM_GENERAL_SCENE_MODE_CLOSEUP,
2976 CAM_GENERAL_SCENE_MODE_PORTRAIT,
2977 CAM_GENERAL_SCENE_MODE_LANDSCAPE,
2978 CAM_GENERAL_SCENE_MODE_SPORT,
2979 CAM_GENERAL_SCENE_MODE_NIGHT,
2980 CAM_GENERAL_SCENE_MODE_AUTO,
2984 static const gint gst_v4l2camsrc_flash_map[] = {
2985 CAM_LIGHT_FLASH_MODE_AUTO,
2986 CAM_LIGHT_FLASH_MODE_OFF,
2987 CAM_LIGHT_FLASH_MODE_ON,
2988 CAM_LIGHT_FLASH_MODE_FILL_IN,
2989 CAM_LIGHT_FLASH_MODE_RED_EYE,
2993 static const gint gst_v4l2camsrc_focus_map[] = {
2994 CAM_FOCUS_MODE_AUTO, /* GST_PHOTOGRAPHY_FOCUS_MODE_AUTO = 0 */
2995 CAM_FOCUS_MODE_MACRO, /* GST_PHOTOGRAPHY_FOCUS_MODE_MACRO */
2996 CAM_FOCUS_MODE_TOUCH_AUTO, /* GST_PHOTOGRAPHY_FOCUS_MODE_PORTRAIT */
2997 CAM_FOCUS_MODE_FULL, /* GST_PHOTOGRAPHY_FOCUS_MODE_INFINITY */
2998 CAM_FOCUS_MODE_AUTO, /* GST_PHOTOGRAPHY_FOCUS_MODE_HYPERFOCAL */
2999 CAM_FOCUS_MODE_FULL, /* GST_PHOTOGRAPHY_FOCUS_MODE_EXTENDED */
3000 CAM_FOCUS_MODE_CONTINUOUS, /* GST_PHOTOGRAPHY_FOCUS_MODE_CONTINUOUS_NORMAL */
3001 CAM_FOCUS_MODE_CONTINUOUS, /* GST_PHOTOGRAPHY_FOCUS_MODE_CONTINUOUS_EXTENDED */
3005 static const gint gst_v4l2camsrc_flicker_map[] = {
3006 CAM_GENERAL_FLICKER_REDUCTION_MODE_OFF, /* GST_PHOTOGRAPHY_FLICKER_REDUCTION_OFF = 0 */
3007 CAM_GENERAL_FLICKER_REDUCTION_MODE_50HZ, /* GST_PHOTOGRAPHY_FLICKER_REDUCTION_50HZ */
3008 CAM_GENERAL_FLICKER_REDUCTION_MODE_60HZ, /* GST_PHOTOGRAPHY_FLICKER_REDUCTION_60HZ */
3009 CAM_GENERAL_FLICKER_REDUCTION_MODE_AUTO, /* GST_PHOTOGRAPHY_FLICKER_REDUCTION_AUTO */
3013 static const gint gst_v4l2camsrc_iso_map[][2] = {
3014 {CAM_EXPOSURE_ISO_VALUE_1600, 1600},
3015 {CAM_EXPOSURE_ISO_VALUE_800, 800},
3016 {CAM_EXPOSURE_ISO_VALUE_400, 400},
3017 {CAM_EXPOSURE_ISO_VALUE_200, 200},
3018 {CAM_EXPOSURE_ISO_VALUE_100, 100},
3019 {CAM_EXPOSURE_ISO_VALUE_AUTO, 0}
3022 static const gint gst_v4l2camsrc_focus_status_map[][2] =
3024 { GST_PHOTOGRAPHY_FOCUS_STATUS_NONE, CAM_FOCUS_STATUS_IDLE },
3025 { GST_PHOTOGRAPHY_FOCUS_STATUS_RUNNING, CAM_FOCUS_STATUS_RUNNING },
3026 { GST_PHOTOGRAPHY_FOCUS_STATUS_SUCCESS, CAM_FOCUS_STATUS_SUCCESS },
3027 { GST_PHOTOGRAPHY_FOCUS_STATUS_FAIL, CAM_FOCUS_STATUS_FAIL },
3031 static const gchar *cameralib_error_map[] = {
3038 "CAM_ERR_DEPRECATED",
3039 "CAM_ERR_INVALID_STATE",
3048 static cam_exposure_iso_value_t
3049 gst_v4l2camsrc_libmfld_map_iso (guint iso)
3053 while (gst_v4l2camsrc_iso_map[i][1] != 0) {
3054 if (iso >= gst_v4l2camsrc_iso_map[i][1]) {
3060 return gst_v4l2camsrc_iso_map[i][1];
3068 gst_v4l2camsrc_libmfld_reverse_map_iso (cam_exposure_iso_value_t iso)
3072 while (gst_v4l2camsrc_iso_map[i][1] != 0) {
3073 if (iso == gst_v4l2camsrc_iso_map[i][0]) {
3079 return gst_v4l2camsrc_iso_map[i][1];
3082 static const gint gst_v4l2camsrc_shakerisk_map[] = {
3083 GST_PHOTOGRAPHY_SHAKE_RISK_LOW, /* CAM_EXPOSURE_SHAKE_RISK_LOW = 0 */
3084 GST_PHOTOGRAPHY_SHAKE_RISK_MEDIUM, /* CAM_EXPOSURE_SHAKE_RISK_MEDIUM */
3085 GST_PHOTOGRAPHY_SHAKE_RISK_HIGH, /* CAM_EXPOSURE_SHAKE_RISK_HIGH */
3089 * Return index of the item in the table.
3092 find_item (const gint table[], const gint item)
3096 while (table[i] != -1) {
3097 if (table[i] == item)
3105 gst_v4l2camsrc_read_settings (GstCameraSrc * camsrc,
3106 GstPhotoSettings * photoconf)
3108 GstMFLDV4l2CamSrc *v4l2camsrc;
3109 gint32 wbmode, effect, scene, flash, focus;
3111 gint32 ev, iso, aperture, exposure, noise_reduction;
3114 v4l2camsrc = GST_V4L2CAMSRC (camsrc);
3116 g_mutex_lock (v4l2camsrc->device_mutex);
3118 if (!v4l2camsrc->initialized) {
3119 GST_DEBUG ("Device not initialized");
3120 g_mutex_unlock (v4l2camsrc->device_mutex);
3124 GST_DEBUG ("Retrieving settings from camera");
3126 err = cam_feature_get (v4l2camsrc->video_fd, CAM_AWB_MODE, &wbmode);
3127 if (err != CAM_ERR_NONE) {
3128 GST_DEBUG ("CAM_AWB_MODE query failed: %s", cameralib_error_map[err]);
3133 cam_feature_get (v4l2camsrc->video_fd, CAM_GENERAL_EFFECT_TYPE, &effect);
3134 if (err != CAM_ERR_NONE) {
3135 GST_DEBUG ("CAM_GENERAL_EFFECT_TYPE query failed: %s",
3136 cameralib_error_map[err]);
3140 err = cam_feature_get (v4l2camsrc->video_fd, CAM_GENERAL_SCENE_MODE, &scene);
3141 if (err != CAM_ERR_NONE) {
3142 GST_DEBUG ("CAM_GENERAL_SCENE_MODE query failed: %s",
3143 cameralib_error_map[err]);
3147 err = cam_feature_get (v4l2camsrc->video_fd, CAM_LIGHT_FLASH_MODE, &flash);
3148 if (err != CAM_ERR_NONE) {
3149 GST_DEBUG ("CAM_LIGHT_FLASH_MODE query failed: %s",
3150 cameralib_error_map[err]);
3154 err = cam_feature_get (v4l2camsrc->video_fd, CAM_EXPOSURE_COMPENSATION, &ev);
3155 if (err != CAM_ERR_NONE) {
3156 GST_DEBUG ("CAM_EXPOSURE_COMPENSATION query failed: %s",
3157 cameralib_error_map[err]);
3161 err = cam_feature_get (v4l2camsrc->video_fd, CAM_EXPOSURE_ISO_VALUE, &iso);
3162 if (err != CAM_ERR_NONE) {
3163 GST_DEBUG ("CAM_EXPOSURE_ISO_VALUE query failed: %s",
3164 cameralib_error_map[err]);
3168 err = cam_feature_get (v4l2camsrc->video_fd, CAM_EXPOSURE_MANUAL_APERTURE,
3170 if (err != CAM_ERR_NONE) {
3171 GST_DEBUG ("CAM_EXPOSURE_MANUAL_APERTURE query failed: %s",
3172 cameralib_error_map[err]);
3177 cam_feature_get (v4l2camsrc->video_fd, CAM_EXPOSURE_MANUAL_TIME,
3179 if (err != CAM_ERR_NONE) {
3180 GST_DEBUG ("CAM_EXPOSURE_MANUAL_TIME query failed: %s",
3181 cameralib_error_map[err]);
3185 err = cam_feature_get (v4l2camsrc->video_fd, CAM_FOCUS_MODE, &focus);
3186 if (err != CAM_ERR_NONE) {
3187 GST_DEBUG ("CAM_FOCUS_MODE query failed: %s", cameralib_error_map[err]);
3192 cam_feature_get (v4l2camsrc->video_fd, CAM_GENERAL_FLICKER_REDUCTION_MODE,
3194 if (err != CAM_ERR_NONE) {
3195 GST_DEBUG ("CAM_GENERAL_FLICKER_REDUCTION_MODE query failed: %s",
3196 cameralib_error_map[err]);
3201 cam_feature_get (v4l2camsrc->video_fd, CAM_NOISE_REDUCTION_MODE,
3203 if (err != CAM_ERR_NONE) {
3204 GST_DEBUG ("CAM_NOISE_REDUCTION query failed: %s",
3205 cameralib_error_map[err]);
3209 photoconf->wb_mode = find_item (gst_v4l2camsrc_wb_map, wbmode);
3210 photoconf->tone_mode = find_item (gst_v4l2camsrc_effect_map, effect);
3211 photoconf->scene_mode = find_item (gst_v4l2camsrc_scene_map, scene);
3212 photoconf->flash_mode = find_item (gst_v4l2camsrc_flash_map, flash);
3213 photoconf->focus_mode = find_item (gst_v4l2camsrc_focus_map, focus);
3214 photoconf->flicker_mode = find_item (gst_v4l2camsrc_flicker_map, flicker);
3216 photoconf->ev_compensation = (gfloat) ev / 10;
3217 photoconf->iso_speed = gst_v4l2camsrc_libmfld_map_iso (iso);
3218 photoconf->aperture = aperture;
3219 photoconf->exposure = exposure;
3220 photoconf->noise_reduction = noise_reduction;
3222 GST_DEBUG ("Scene mode ext: %d, lib: %d", photoconf->scene_mode, scene);
3223 GST_DEBUG ("Focus mode ext: %d, lib: %d", photoconf->focus_mode, focus);
3224 GST_DEBUG ("Flash mode ext: %d, lib: %d", photoconf->flash_mode, flash);
3225 GST_DEBUG ("ISO: %d, EV_comp: %f (%d)", iso, photoconf->ev_compensation, ev);
3227 g_mutex_unlock (v4l2camsrc->device_mutex);
3234 g_mutex_unlock (v4l2camsrc->device_mutex);
3239 gst_v4l2camsrc_read_exif (GstCameraSrc * camsrc,
3240 GstCameraControlExifInfo *exif_info)
3242 GstMFLDV4l2CamSrc *v4l2camsrc;
3244 struct atomisp_makernote_info maker_note;
3247 v4l2camsrc = GST_V4L2CAMSRC (camsrc);
3250 if (!v4l2camsrc->initialized) {
3251 GST_DEBUG ("Device not initialized");
3254 buf = (unsigned char *)&maker_note;
3256 GST_DEBUG ("Retrieving makernote from atomisp");
3257 err = cam_get_makernote (v4l2camsrc->video_fd, buf, 0);
3263 exif_info->focal_len_numerator = maker_note.focal_length >> 16;
3264 exif_info->focal_len_denominator = maker_note.focal_length & 0xFFFF;
3265 /* Aperture value (f_num) */
3266 exif_info->aperture_f_num_numerator = maker_note.f_number_curr >> 16;
3267 exif_info->aperture_f_num_denominator = maker_note.f_number_curr & 0xFFFF;
3268 /* isp major, minor,patch level */
3269 exif_info->software_used = v4l2camsrc->vcap.version;
3270 exif_info->colorspace = 65535; /* Uncalibrated (=65535) */
3271 exif_info->metering_mode = 1; /* Average */
3272 cam_feature_get (v4l2camsrc->video_fd, CAM_EXPOSURE_ISO_VALUE, &exif_info->iso);
3274 //* TODO complete exif info */
3275 exif_info->exposure_time_denominator = 0;
3276 exif_info->exposure_time_numerator = 0;
3278 exif_info->shutter_speed_numerator = 0;
3279 exif_info->shutter_speed_denominator = 0;
3280 exif_info->brigtness_numerator = 0;
3281 exif_info->brightness_denominator = 0;
3282 exif_info->flash = cam_is_flash_used();
3283 GST_DEBUG ("Focal lenght numerator : %d", exif_info->focal_len_numerator);
3284 GST_DEBUG ("Focal lenght denominator : %d", exif_info->focal_len_denominator);
3285 GST_DEBUG ("F num numerator : %d", exif_info->aperture_f_num_numerator);
3286 GST_DEBUG ("F num denominator : %d", exif_info->aperture_f_num_denominator);
3287 GST_DEBUG ("flash used : %d", exif_info->flash);
3294 g_mutex_unlock (v4l2camsrc->device_mutex);
3300 gst_v4l2camsrc_set_flash_mode (GstCameraSrc * camsrc,
3303 GstMFLDV4l2CamSrc *v4l2camsrc;
3308 v4l2camsrc = GST_V4L2CAMSRC (camsrc);
3310 g_mutex_lock (v4l2camsrc->device_mutex);
3312 if (!v4l2camsrc->initialized) {
3313 GST_DEBUG ("Device not initialized");
3314 g_mutex_unlock (v4l2camsrc->device_mutex);
3319 case GST_CAMERA_STROBE_CAP_NONE:
3320 case GST_CAMERA_STROBE_CAP_OFF:
3321 flash = CAM_LIGHT_FLASH_MODE_OFF;
3323 case GST_CAMERA_STROBE_CAP_ON:
3324 flash = CAM_LIGHT_FLASH_MODE_ON;
3326 case GST_CAMERA_STROBE_CAP_AUTO:
3327 flash = CAM_LIGHT_FLASH_MODE_AUTO;
3329 case GST_CAMERA_STROBE_CAP_REDEYE:
3330 flash = CAM_LIGHT_FLASH_MODE_RED_EYE;
3333 flash = CAM_LIGHT_FLASH_MODE_OFF;
3337 GST_DEBUG_OBJECT (v4l2camsrc, "Set flash mode: %d",flash);
3338 err= cam_feature_set (v4l2camsrc->video_fd, CAM_LIGHT_FLASH_MODE,
3344 g_mutex_unlock (v4l2camsrc->device_mutex);
3351 g_mutex_unlock (v4l2camsrc->device_mutex);
3357 gst_v4l2camsrc_write_settings (GstCameraSrc * camsrc,
3358 GstPhotoSettings * photoconf, gboolean scene_override)
3360 GstMFLDV4l2CamSrc *v4l2camsrc;
3361 v4l2camsrc = GST_V4L2CAMSRC (camsrc);
3362 GST_DEBUG ("Write settings to libmfldcamd");
3364 g_mutex_lock (v4l2camsrc->device_mutex);
3366 if (!v4l2camsrc->initialized) {
3367 g_mutex_unlock (v4l2camsrc->device_mutex);
3368 GST_DEBUG ("Device not initialized");
3372 if (scene_override &&
3373 photoconf->scene_mode != GST_PHOTOGRAPHY_SCENE_MODE_MANUAL) {
3374 /* If scene override flag is set, we just leave the new settings in use */
3375 GST_DEBUG ("SCENE OVERRIDE, ext=%d", photoconf->scene_mode);
3376 cam_feature_set (v4l2camsrc->video_fd, CAM_GENERAL_SCENE_MODE,
3377 gst_v4l2camsrc_scene_map[photoconf->scene_mode]);
3379 GST_DEBUG ("Normal settings");
3380 cam_feature_set (v4l2camsrc->video_fd, CAM_AWB_MODE,
3381 gst_v4l2camsrc_wb_map[photoconf->wb_mode]);
3383 cam_feature_set (v4l2camsrc->video_fd, CAM_GENERAL_EFFECT_TYPE,
3384 gst_v4l2camsrc_effect_map[photoconf->tone_mode]);
3386 cam_feature_set (v4l2camsrc->video_fd, CAM_LIGHT_FLASH_MODE,
3387 gst_v4l2camsrc_flash_map[photoconf->flash_mode]);
3389 /* These will set exposure mode to MANUAL, is value is != 0 */
3390 cam_feature_set (v4l2camsrc->video_fd, CAM_EXPOSURE_MANUAL_TIME,
3391 photoconf->exposure);
3392 cam_feature_set (v4l2camsrc->video_fd, CAM_EXPOSURE_MANUAL_APERTURE,
3393 photoconf->aperture);
3395 cam_feature_set (v4l2camsrc->video_fd, CAM_EXPOSURE_COMPENSATION,
3396 (gint) (photoconf->ev_compensation * 10));
3398 cam_feature_set (v4l2camsrc->video_fd, CAM_EXPOSURE_ISO_VALUE,
3399 gst_v4l2camsrc_libmfld_reverse_map_iso (photoconf->iso_speed));
3401 cam_feature_set (v4l2camsrc->video_fd, CAM_FOCUS_MODE,
3402 gst_v4l2camsrc_focus_map[photoconf->focus_mode]);
3404 cam_feature_set (v4l2camsrc->video_fd, CAM_GENERAL_FLICKER_REDUCTION_MODE,
3405 gst_v4l2camsrc_flicker_map[photoconf->flicker_mode]);
3407 cam_feature_set (v4l2camsrc->video_fd, CAM_NOISE_REDUCTION_MODE,
3408 photoconf->noise_reduction);
3410 g_mutex_unlock (v4l2camsrc->device_mutex);
3416 gst_v4l2camsrc_set_AeAafwindow (GstCameraSrc * camsrc, GstCameraSrc3a_window window)
3418 GstMFLDV4l2CamSrc *v4l2camsrc;
3419 gboolean ret = TRUE;
3421 v4l2camsrc = GST_V4L2CAMSRC (camsrc);
3423 GST_DEBUG_OBJECT(v4l2camsrc,"ae-af-window-setting: x_left:%d, x_right:%d,"
3424 "y_bottom:%d, y_top:%d, weight:%d.\n",
3425 window.x_left,window.x_right,
3426 window.y_bottom, window.y_top,
3429 v4l2camsrc->af_window = v4l2camsrc->ae_window = *(GstCameraWindow*)&window;
3431 if (v4l2camsrc->is_active) {
3432 g_mutex_lock (v4l2camsrc->device_mutex);
3433 cam_set_af_ae_window((advci_window *) (&v4l2camsrc->af_window));
3434 g_mutex_unlock (v4l2camsrc->device_mutex);
3441 gst_v4l2camsrc_set_autofocus (GstCameraSrc * camsrc, gboolean on)
3443 GstMFLDV4l2CamSrc *v4l2camsrc;
3444 gboolean ret = FALSE;
3446 v4l2camsrc = GST_V4L2CAMSRC (camsrc);
3448 g_mutex_lock (v4l2camsrc->device_mutex);
3450 cam_err_t err = CAM_ERR_NONE;
3452 err= cam_set_autofocus(on);
3453 v4l2camsrc->af_enabled = on;
3455 ret = (err == CAM_ERR_NONE);
3457 g_mutex_unlock (v4l2camsrc->device_mutex);
3459 GST_DEBUG ("setting autofocus: %s", ret ? "ok" : "failed");
3468 gst_v4l2camsrc_set_autoexposure (GstCameraSrc * camsrc, gboolean on)
3470 GstMFLDV4l2CamSrc *v4l2camsrc;
3471 gboolean ret = FALSE;
3474 v4l2camsrc = GST_V4L2CAMSRC (camsrc);
3475 mode = on ? "ON" : "OFF";
3477 GST_DEBUG_OBJECT (v4l2camsrc, "setting autoexposure: %s", mode);
3480 g_mutex_lock (v4l2camsrc->device_mutex);
3482 cam_err_t err = CAM_ERR_NONE;
3484 err = cam_set_autoexposure(v4l2camsrc->video_fd, on);
3485 v4l2camsrc->ae_enabled = on;
3487 ret = (err == CAM_ERR_NONE);
3488 g_mutex_unlock (v4l2camsrc->device_mutex);
3490 GST_DEBUG_OBJECT (v4l2camsrc, "Setting autoexposure %s: %s", mode,
3491 ret ? "SUCCESS" : "FAILED");
3500 gst_v4l2camsrc_get_capabilities (GstCameraSrc * camsrc)
3504 pcaps = GST_PHOTOGRAPHY_CAPS_ZOOM | GST_PHOTOGRAPHY_CAPS_EV_COMP |
3505 GST_PHOTOGRAPHY_CAPS_WB_MODE | GST_PHOTOGRAPHY_CAPS_TONE |
3506 GST_PHOTOGRAPHY_CAPS_SCENE | GST_PHOTOGRAPHY_CAPS_FLASH |
3507 GST_PHOTOGRAPHY_CAPS_FOCUS | GST_PHOTOGRAPHY_CAPS_APERTURE |
3508 GST_PHOTOGRAPHY_CAPS_EXPOSURE | GST_PHOTOGRAPHY_CAPS_SHAKE |
3509 GST_PHOTOGRAPHY_CAPS_ISO_SPEED;
3518 gst_v4l2camsrc_check_focus_status (GstMFLDV4l2CamSrc * v4l2camsrc,
3519 GstCameraFocusStatus *fs, gboolean detailed)
3521 gboolean ret = FALSE;
3522 cam_focus_status_t status;
3525 GST_DEBUG_OBJECT (v4l2camsrc, "Retrieving focus status");
3527 update = cam_checkfocus_status (&status, (v4l2camsrc->debug_flags & GST_CAMERASRC_DEBUG_FLAGS_AUTO_FOCUS));
3529 if (update == TRUE) {
3530 fs->status = cam_find_item_new (gst_v4l2camsrc_focus_status_map, status, TRUE);
3533 GST_DEBUG_OBJECT (v4l2camsrc, "Focus status: %d", fs->status);
3539 gst_v4l2camsrc_set_capture_mode (GstCameraSrc * camsrc,
3540 GstCameraSrcCaptureMode mode)
3542 GstMFLDV4l2CamSrc *v4l2camsrc;
3543 gboolean ret = FALSE;
3544 int err; //cam_err_t err;
3545 cam_capture_mode_t cam_mode;
3546 static const gchar *cmodes[] = { "VIEWFINDER", "STILL", "VIDEO" };
3547 struct v4l2_streamparm parm;
3551 v4l2camsrc = GST_V4L2CAMSRC (camsrc);
3553 fd = v4l2camsrc->video_fd;
3555 cam_mode = find_item (gst_v4l2camsrc_capture_map, mode);
3557 g_mutex_lock (v4l2camsrc->device_mutex);
3559 if (v4l2camsrc->initialized) {
3560 v4l2camsrc->capture_mode = mode;
3561 GST_DEBUG ("Setting isp capture mode: %s", cmodes[mode]);
3562 memset (&parm, 0x00, sizeof (struct v4l2_streamparm));
3564 parm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
3565 err = ioctl(fd, VIDIOC_G_PARM, &parm);
3567 GST_DEBUG("Unable to get frame rate: %s (%d).\n",
3568 strerror(errno), errno);
3571 GST_DEBUG("Current frame rate: %u/%u\n",
3572 parm.parm.capture.timeperframe.numerator,
3573 parm.parm.capture.timeperframe.denominator);
3575 GST_DEBUG("Current run mode: %04x\n", parm.parm.capture.capturemode);
3578 case GST_CAMERA_SRC_CAPTURE_MODE_VIEWFINDER:
3579 binary = CI_MODE_PREVIEW;
3581 case GST_CAMERA_SRC_CAPTURE_MODE_STILL:
3582 binary = CI_MODE_STILL_CAPTURE;
3584 case GST_CAMERA_SRC_CAPTURE_MODE_VIDEO:
3585 binary = CI_MODE_VIDEO;
3588 binary = CI_MODE_PREVIEW;
3591 parm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
3592 parm.parm.capture.capturemode = binary;
3594 GST_DEBUG("New run mode: %04x\n", parm.parm.capture.capturemode);
3596 if (ioctl (fd, VIDIOC_S_PARM, &parm) < 0) {
3597 GST_DEBUG("Switch Mode failed !\n");
3601 GST_DEBUG_OBJECT (v4l2camsrc, "Setting capture mode done: %s",
3602 ret ? "OK" : "FAIL");
3605 GST_DEBUG_OBJECT (v4l2camsrc, "Device not initialized");
3609 g_mutex_unlock (v4l2camsrc->device_mutex);
3614 gboolean gst_v4l2camsrc_set_strobe_state (GstCameraSrc * camsrc,
3618 GstMFLDV4l2CamSrc *v4l2camsrc;
3620 cam_err_t err = CAM_ERR_NONE;
3622 v4l2camsrc = GST_V4L2CAMSRC (camsrc);
3623 fd = v4l2camsrc->video_fd;
3625 err = cam_set_flash (fd, state);
3626 return(err == CAM_ERR_NONE);
3629 gst_libmfldcam_capture_correction_update (GstMFLDV4l2CamSrc * v4l2camsrc)
3631 int fd = v4l2camsrc->video_fd;
3634 if (!v4l2camsrc->cc_updated)
3637 if (v4l2camsrc->gdc_enabled)
3638 ret = cam_set_capture_correction (fd, CAM_CAPTURE_CORRECTION_GDC, TRUE);
3640 ret = cam_set_capture_correction (fd, CAM_CAPTURE_CORRECTION_GDC, FALSE);
3645 if (v4l2camsrc->cac_enabled)
3646 ret = cam_set_capture_correction (fd, CAM_CAPTURE_CORRECTION_CAC, TRUE);
3648 ret = cam_set_capture_correction (fd, CAM_CAPTURE_CORRECTION_CAC, FALSE);
3653 if (v4l2camsrc->dvs_enabled)
3654 ret = cam_set_capture_correction (fd, CAM_CAPTURE_CORRECTION_DVS, TRUE);
3656 ret = cam_set_capture_correction (fd, CAM_CAPTURE_CORRECTION_DVS, FALSE);
3661 if (v4l2camsrc->ee_enabled)
3662 ret = cam_set_capture_correction (fd, CAM_CAPTURE_CORRECTION_EE, TRUE);
3664 ret = cam_set_capture_correction (fd, CAM_CAPTURE_CORRECTION_EE, FALSE);
3669 if (v4l2camsrc->sc_enabled)
3670 ret = cam_set_capture_correction (fd, CAM_CAPTURE_CORRECTION_SC, TRUE);
3672 ret = cam_set_capture_correction (fd, CAM_CAPTURE_CORRECTION_SC, FALSE);
3677 if (v4l2camsrc->blc_enabled)
3678 ret = cam_set_capture_correction (fd, CAM_CAPTURE_CORRECTION_BLC, TRUE);
3680 ret = cam_set_capture_correction (fd, CAM_CAPTURE_CORRECTION_BLC, FALSE);
3682 if (v4l2camsrc->bpd_enabled)
3683 ret = cam_set_capture_correction (fd, CAM_CAPTURE_CORRECTION_BPD, TRUE);
3685 ret = cam_set_capture_correction (fd, CAM_CAPTURE_CORRECTION_BPD, FALSE);
3690 v4l2camsrc->cc_updated = FALSE;
3699 GST_WARNING_OBJECT (v4l2camsrc, "Set capture correction failed \n");
3705 * gst_v4l2camsrc_libmfldcam_set_zoom:
3706 * @v4l2camsrc: #GstMFLDV4l2CamSrc object.
3707 * @zoom: Desired zoom factor.
3709 * Set the zoom factor for captured image.
3711 * Returns: TRUE on success.
3714 gst_v4l2camsrc_libmfldcam_set_zoom (GstMFLDV4l2CamSrc * v4l2camsrc, gfloat zoom)
3716 cam_err_t err = CAM_ERR_NONE;
3719 GST_DEBUG_OBJECT (v4l2camsrc, "ZOOM: %f", zoom);
3721 err = cam_set_zoom (v4l2camsrc->video_fd, zoom);
3722 /* ret = err == CAM_ERR_NONE; */
3725 GST_DEBUG_OBJECT (v4l2camsrc, "Setting zoom: %s", ret ? "SUCCESS" : "FAIL");
3731 gst_v4l2camsrc_libmfldcam_get_makernote (GstMFLDV4l2CamSrc * v4l2camsrc, unsigned char *buf, unsigned size)
3733 cam_err_t err = CAM_ERR_NONE;
3735 GST_DEBUG_OBJECT (v4l2camsrc, "%s, !!!!!!line:%d\n", __func__, __LINE__);
3737 err = cam_get_makernote (v4l2camsrc->video_fd, buf, size);
3742 gst_v4l2camsrc_libmfldcam_get_focus_posi(GstMFLDV4l2CamSrc * v4l2camsrc, unsigned *posi)
3744 cam_err_t err = CAM_ERR_NONE;
3745 err = cam_get_focus_posi(v4l2camsrc->video_fd, posi);
3746 if(err != CAM_ERR_NONE)
3753 * @v4l2camsrc: #GstMFLDV4l2CamSrc object
3755 * configure driver default settings and set the opened ISP fd to libmfldcam
3758 gst_v4l2camsrc_libmfldcam_init (GstMFLDV4l2CamSrc * v4l2camsrc)
3761 if (v4l2camsrc->input_sensor == GST_CAMERA_INPUT_SENSOR_PRIMARY)
3762 err = cam_driver_init (v4l2camsrc->video_fd, "mt9e013");
3764 err = cam_driver_init (v4l2camsrc->video_fd, "mt9m114");
3766 if (err != CAM_ERR_NONE) {
3767 GST_WARNING ("libmfldcam initialization failed");
3770 if (v4l2camsrc->cc_updated)
3771 gst_libmfldcam_capture_correction_update (v4l2camsrc);
3773 if (v4l2camsrc->gamma_updated) {
3774 cam_set_tone_control (v4l2camsrc->video_fd, CAM_GAMMA_VALUE,
3776 cam_set_tone_control (v4l2camsrc->video_fd, CAM_BRIGHTNESS_VALUE,
3778 cam_set_tone_control (v4l2camsrc->video_fd, CAM_CONTRAST_VALUE,
3780 v4l2camsrc->gamma_updated = FALSE;
3783 /* Set the default settings here */
3785 GST_DEBUG_OBJECT (v4l2camsrc, "Configure default settings %s",
3786 v4l2camsrc->videodev);
3788 gst_v4l2camsrc_set_attribute (GST_CAMERA_SRC (v4l2camsrc),V4L2_CID_VFLIP,v4l2camsrc->vflip);
3789 gst_v4l2camsrc_set_attribute (GST_CAMERA_SRC (v4l2camsrc),V4L2_CID_HFLIP,v4l2camsrc->hflip);
3791 v4l2camsrc->initialized = TRUE;
3792 v4l2camsrc->is_open = TRUE;
3797 gst_v4l2camsrc_libmfldcam_deinit (GstMFLDV4l2CamSrc * v4l2camsrc)
3799 //FIXME : determin whether is it initialized
3801 err = cam_driver_deinit (v4l2camsrc->video_fd);
3802 if (err != CAM_ERR_NONE) {
3803 GST_WARNING ("libmfldcam initialization failed");
3807 v4l2camsrc->is_open = FALSE;