1 /* G-Streamer hardware MJPEG video source plugin
2 * Copyright (C) 2001-2002 Ronald Bultje <rbultje@ronald.bitfreak.net>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
24 #include <sys/types.h>
27 #include <sys/ioctl.h>
31 #include "v4lmjpegsrc_calls.h"
33 /* On some systems MAP_FAILED seems to be missing */
35 #define MAP_FAILED ( (caddr_t) -1 )
38 #define MIN_BUFFERS_QUEUED 2
40 #define DEBUG(format, args...) \
42 GST_ELEMENT(v4lmjpegsrc), \
43 "V4LMJPEGSRC: " format, ##args)
46 QUEUE_STATE_ERROR = -1,
47 QUEUE_STATE_READY_FOR_QUEUE,
52 /******************************************************
53 * gst_v4lmjpegsrc_queue_frame():
54 * queue a frame for capturing
55 * return value: TRUE on success, FALSE on error
56 ******************************************************/
59 gst_v4lmjpegsrc_queue_frame (GstV4lMjpegSrc *v4lmjpegsrc,
62 DEBUG("queueing frame %d", num);
64 if (v4lmjpegsrc->frame_queue_state[num] != QUEUE_STATE_READY_FOR_QUEUE) {
68 if (ioctl(GST_V4LELEMENT(v4lmjpegsrc)->video_fd, MJPIOC_QBUF_CAPT, &num) < 0)
70 GST_ELEMENT_ERROR (v4lmjpegsrc, RESOURCE, READ, (NULL),
71 ("Error queueing a buffer (%d): %s",
72 num, g_strerror(errno)));
76 v4lmjpegsrc->frame_queue_state[num] = QUEUE_STATE_QUEUED;
77 v4lmjpegsrc->num_queued++;
83 /******************************************************
84 * gst_v4lmjpegsrc_sync_next_frame():
85 * sync on the next frame for capturing
86 * return value: TRUE on success, FALSE on error
87 ******************************************************/
90 gst_v4lmjpegsrc_sync_next_frame (GstV4lMjpegSrc *v4lmjpegsrc,
93 DEBUG("syncing on next frame");
95 if (v4lmjpegsrc->num_queued <= 0) {
99 while (ioctl(GST_V4LELEMENT(v4lmjpegsrc)->video_fd,
100 MJPIOC_SYNC, &(v4lmjpegsrc->bsync)) < 0) {
101 if (errno != EINTR) {
102 GST_ELEMENT_ERROR (v4lmjpegsrc, RESOURCE, SYNC, NULL, GST_ERROR_SYSTEM);
105 DEBUG("Sync got interrupted");
108 *num = v4lmjpegsrc->bsync.frame;
110 v4lmjpegsrc->frame_queue_state[*num] = QUEUE_STATE_SYNCED;
111 v4lmjpegsrc->num_queued--;
117 /******************************************************
118 * gst_v4lmjpegsrc_set_buffer():
119 * set buffer parameters (size/count)
120 * return value: TRUE on success, FALSE on error
121 ******************************************************/
124 gst_v4lmjpegsrc_set_buffer (GstV4lMjpegSrc *v4lmjpegsrc,
128 DEBUG("setting buffer info to numbufs = %d, bufsize = %d KB",
130 GST_V4L_CHECK_OPEN(GST_V4LELEMENT(v4lmjpegsrc));
131 GST_V4L_CHECK_NOT_ACTIVE(GST_V4LELEMENT(v4lmjpegsrc));
133 v4lmjpegsrc->breq.size = bufsize * 1024;
134 v4lmjpegsrc->breq.count = numbufs;
140 /******************************************************
141 * gst_v4lmjpegsrc_set_capture():
142 * set capture parameters (simple)
143 * return value: TRUE on success, FALSE on error
144 ******************************************************/
147 gst_v4lmjpegsrc_set_capture (GstV4lMjpegSrc *v4lmjpegsrc,
152 struct mjpeg_params bparm;
154 DEBUG("setting decimation = %d, quality = %d",
155 decimation, quality);
156 GST_V4L_CHECK_OPEN(GST_V4LELEMENT(v4lmjpegsrc));
157 GST_V4L_CHECK_NOT_ACTIVE(GST_V4LELEMENT(v4lmjpegsrc));
159 gst_v4l_get_chan_norm(GST_V4LELEMENT(v4lmjpegsrc), &input, &norm);
161 /* Query params for capture */
162 if (ioctl(GST_V4LELEMENT(v4lmjpegsrc)->video_fd, MJPIOC_G_PARAMS, &bparm) < 0)
164 GST_ELEMENT_ERROR (v4lmjpegsrc, RESOURCE, SETTINGS, NULL, GST_ERROR_SYSTEM);
168 bparm.decimation = decimation;
169 bparm.quality = quality;
172 bparm.APP_len = 0; /* no JPEG markers - TODO: this is definately not right for decimation==1 */
174 mw = GST_V4LELEMENT(v4lmjpegsrc)->vcap.maxwidth;
175 if (mw != 768 && mw != 640)
182 v4lmjpegsrc->end_width = mw / decimation;
183 v4lmjpegsrc->end_height = (norm==VIDEO_MODE_NTSC?480:576) / decimation;
185 /* TODO: interlacing */
187 /* Set params for capture */
188 if (ioctl(GST_V4LELEMENT(v4lmjpegsrc)->video_fd, MJPIOC_S_PARAMS, &bparm) < 0)
190 GST_ELEMENT_ERROR (v4lmjpegsrc, RESOURCE, SETTINGS, NULL, GST_ERROR_SYSTEM);
198 /******************************************************
199 * gst_v4lmjpegsrc_set_capture_m():
200 * set capture parameters (advanced)
201 * return value: TRUE on success, FALSE on error
202 ******************************************************/
204 gboolean gst_v4lmjpegsrc_set_capture_m (GstV4lMjpegSrc *v4lmjpegsrc,
215 struct mjpeg_params bparm;
217 DEBUG("setting x_offset = %d, y_offset = %d, "
218 "width = %d, height = %d, h_decimation = %d, v_decimation = %d, quality = %d\n",
219 x_offset, y_offset, width, height, h_decimation, v_decimation, quality);
220 GST_V4L_CHECK_OPEN(GST_V4LELEMENT(v4lmjpegsrc));
221 GST_V4L_CHECK_NOT_ACTIVE(GST_V4LELEMENT(v4lmjpegsrc));
223 gst_v4l_get_chan_norm(GST_V4LELEMENT(v4lmjpegsrc), &input, &norm);
225 if (GST_V4LELEMENT(v4lmjpegsrc)->vcap.maxwidth != 768 &&
226 GST_V4LELEMENT(v4lmjpegsrc)->vcap.maxwidth != 640)
229 maxwidth = GST_V4LELEMENT(v4lmjpegsrc)->vcap.maxwidth;
231 /* Query params for capture */
232 if (ioctl(GST_V4LELEMENT(v4lmjpegsrc)->video_fd, MJPIOC_G_PARAMS, &bparm) < 0)
234 GST_ELEMENT_ERROR (v4lmjpegsrc, RESOURCE, SETTINGS, NULL, GST_ERROR_SYSTEM);
238 bparm.decimation = 0;
239 bparm.quality = quality;
242 bparm.APP_len = 0; /* no JPEG markers - TODO: this is definately
243 * not right for decimation==1 */
247 if (x_offset < 0) x_offset = 0;
248 width = (maxwidth==720&&h_decimation!=1)?704:maxwidth - 2*x_offset;
253 x_offset = (maxwidth - width)/2;
258 if (y_offset < 0) y_offset = 0;
259 height = (norm==VIDEO_MODE_NTSC)?480:576 - 2*y_offset;
264 y_offset = ((norm==VIDEO_MODE_NTSC)?480:576 - height)/2;
267 if (width + x_offset > maxwidth)
269 GST_ELEMENT_ERROR (v4lmjpegsrc, RESOURCE, TOO_LAZY, NULL,
270 ("Image width+offset (%d) bigger than maximum (%d)",
271 width + x_offset, maxwidth));
274 if ((width%(bparm.HorDcm*16))!=0)
276 GST_ELEMENT_ERROR (v4lmjpegsrc, STREAM, FORMAT, NULL,
277 ("Image width (%d) not multiple of %d (required for JPEG)",
278 width, bparm.HorDcm*16));
281 if (height + y_offset > (norm==VIDEO_MODE_NTSC ? 480 : 576))
283 GST_ELEMENT_ERROR (v4lmjpegsrc, RESOURCE, TOO_LAZY, NULL,
284 ("Image height+offset (%d) bigger than maximum (%d)",
285 height + y_offset, (norm==VIDEO_MODE_NTSC ? 480 : 576)));
288 /* RJ: Image height must only be a multiple of 8, but geom_height
289 * is double the field height
291 if ((height%(bparm.VerDcm*16))!=0)
293 GST_ELEMENT_ERROR (v4lmjpegsrc, STREAM, FORMAT, NULL,
294 ("Image height (%d) not multiple of %d (required for JPEG)",
295 height, bparm.VerDcm*16));
299 bparm.img_x = x_offset;
300 bparm.img_width = width;
301 bparm.img_y = y_offset;
302 bparm.img_height = height;
303 bparm.HorDcm = h_decimation;
304 bparm.VerDcm = (v_decimation==4) ? 2 : 1;
305 bparm.TmpDcm = (v_decimation==1) ? 1 : 2;
306 bparm.field_per_buff = (v_decimation==1) ? 2 : 1;
308 v4lmjpegsrc->end_width = width / h_decimation;
309 v4lmjpegsrc->end_width = height / v_decimation;
311 /* TODO: interlacing */
313 /* Set params for capture */
314 if (ioctl(GST_V4LELEMENT(v4lmjpegsrc)->video_fd, MJPIOC_S_PARAMS, &bparm) < 0)
316 GST_ELEMENT_ERROR (v4lmjpegsrc, RESOURCE, SETTINGS, NULL, GST_ERROR_SYSTEM);
324 /******************************************************
325 * gst_v4lmjpegsrc_capture_init():
326 * initialize the capture system
327 * return value: TRUE on success, FALSE on error
328 ******************************************************/
331 gst_v4lmjpegsrc_capture_init (GstV4lMjpegSrc *v4lmjpegsrc)
333 DEBUG("initting capture subsystem");
334 GST_V4L_CHECK_OPEN(GST_V4LELEMENT(v4lmjpegsrc));
335 GST_V4L_CHECK_NOT_ACTIVE(GST_V4LELEMENT(v4lmjpegsrc));
337 /* Request buffers */
338 if (ioctl(GST_V4LELEMENT(v4lmjpegsrc)->video_fd,
339 MJPIOC_REQBUFS, &(v4lmjpegsrc->breq)) < 0)
341 GST_ELEMENT_ERROR (v4lmjpegsrc, RESOURCE, READ, NULL, GST_ERROR_SYSTEM);
345 if (v4lmjpegsrc->breq.count < MIN_BUFFERS_QUEUED)
347 GST_ELEMENT_ERROR (v4lmjpegsrc, RESOURCE, READ, NULL,
348 ("Too little buffers. We got %ld, we want at least %d",
349 v4lmjpegsrc->breq.count, MIN_BUFFERS_QUEUED));
353 gst_info("Got %ld buffers of size %ld KB\n",
354 v4lmjpegsrc->breq.count, v4lmjpegsrc->breq.size/1024);
356 /* keep track of queued buffers */
357 v4lmjpegsrc->frame_queue_state = (gint8 *)
358 g_malloc(sizeof(gint8) * v4lmjpegsrc->breq.count);
360 /* track how often to use each frame */
361 v4lmjpegsrc->use_num_times = (gint *)
362 g_malloc(sizeof(gint) * v4lmjpegsrc->breq.count);
364 /* lock for the frame_state */
365 v4lmjpegsrc->mutex_queue_state = g_mutex_new();
366 v4lmjpegsrc->cond_queue_state = g_cond_new();
368 /* Map the buffers */
369 GST_V4LELEMENT(v4lmjpegsrc)->buffer = mmap(0,
370 v4lmjpegsrc->breq.count * v4lmjpegsrc->breq.size,
371 PROT_READ|PROT_WRITE, MAP_SHARED, GST_V4LELEMENT(v4lmjpegsrc)->video_fd, 0);
372 if (GST_V4LELEMENT(v4lmjpegsrc)->buffer == MAP_FAILED)
374 GST_ELEMENT_ERROR (v4lmjpegsrc, RESOURCE, TOO_LAZY, NULL,
375 ("Error mapping video buffers: %s", g_strerror (errno)));
376 GST_V4LELEMENT(v4lmjpegsrc)->buffer = NULL;
384 /******************************************************
385 * gst_v4lmjpegsrc_capture_start():
386 * start streaming capture
387 * return value: TRUE on success, FALSE on error
388 ******************************************************/
391 gst_v4lmjpegsrc_capture_start (GstV4lMjpegSrc *v4lmjpegsrc)
395 DEBUG("starting capture");
396 GST_V4L_CHECK_OPEN(GST_V4LELEMENT(v4lmjpegsrc));
397 GST_V4L_CHECK_ACTIVE(GST_V4LELEMENT(v4lmjpegsrc));
399 g_mutex_lock(v4lmjpegsrc->mutex_queue_state);
401 v4lmjpegsrc->quit = FALSE;
402 v4lmjpegsrc->num_queued = 0;
403 v4lmjpegsrc->queue_frame = 0;
405 /* set all buffers ready to queue , this starts streaming capture */
406 for (n=0;n<v4lmjpegsrc->breq.count;n++) {
407 v4lmjpegsrc->frame_queue_state[n] = QUEUE_STATE_READY_FOR_QUEUE;
408 if (!gst_v4lmjpegsrc_queue_frame(v4lmjpegsrc, n)) {
409 g_mutex_unlock(v4lmjpegsrc->mutex_queue_state);
410 gst_v4lmjpegsrc_capture_stop(v4lmjpegsrc);
415 g_mutex_unlock(v4lmjpegsrc->mutex_queue_state);
421 /******************************************************
422 * gst_v4lmjpegsrc_grab_frame():
423 * grab one frame during streaming capture
424 * return value: TRUE on success, FALSE on error
425 ******************************************************/
428 gst_v4lmjpegsrc_grab_frame (GstV4lMjpegSrc *v4lmjpegsrc,
432 DEBUG("grabbing frame");
433 GST_V4L_CHECK_OPEN(GST_V4LELEMENT(v4lmjpegsrc));
434 GST_V4L_CHECK_ACTIVE(GST_V4LELEMENT(v4lmjpegsrc));
436 g_mutex_lock(v4lmjpegsrc->mutex_queue_state);
438 /* do we have enough frames? */
439 while (v4lmjpegsrc->num_queued < MIN_BUFFERS_QUEUED ||
440 v4lmjpegsrc->frame_queue_state[v4lmjpegsrc->queue_frame] ==
441 QUEUE_STATE_READY_FOR_QUEUE) {
442 while (v4lmjpegsrc->frame_queue_state[v4lmjpegsrc->queue_frame] !=
443 QUEUE_STATE_READY_FOR_QUEUE &&
444 !v4lmjpegsrc->quit) {
446 "Waiting for frames to become available (%d < %d)",
447 v4lmjpegsrc->num_queued, MIN_BUFFERS_QUEUED);
448 g_cond_wait(v4lmjpegsrc->cond_queue_state,
449 v4lmjpegsrc->mutex_queue_state);
451 if (v4lmjpegsrc->quit) {
452 g_mutex_unlock(v4lmjpegsrc->mutex_queue_state);
453 return TRUE; /* it won't get through anyway */
455 if (!gst_v4lmjpegsrc_queue_frame(v4lmjpegsrc, v4lmjpegsrc->queue_frame)) {
456 g_mutex_unlock(v4lmjpegsrc->mutex_queue_state);
459 v4lmjpegsrc->queue_frame = (v4lmjpegsrc->queue_frame + 1) % v4lmjpegsrc->breq.count;
462 /* syncing on the buffer grabs it */
463 if (!gst_v4lmjpegsrc_sync_next_frame(v4lmjpegsrc, num)) {
467 *size = v4lmjpegsrc->bsync.length;
469 g_mutex_unlock(v4lmjpegsrc->mutex_queue_state);
475 /******************************************************
476 * gst_v4lmjpegsrc_get_buffer():
477 * get the memory address of a single buffer
478 * return value: TRUE on success, FALSE on error
479 ******************************************************/
482 gst_v4lmjpegsrc_get_buffer (GstV4lMjpegSrc *v4lmjpegsrc,
485 /*DEBUG("gst_v4lmjpegsrc_get_buffer(), num = %d", num);*/
487 if (!GST_V4L_IS_ACTIVE(GST_V4LELEMENT(v4lmjpegsrc)) ||
488 !GST_V4L_IS_OPEN(GST_V4LELEMENT(v4lmjpegsrc)))
491 if (num < 0 || num >= v4lmjpegsrc->breq.count)
494 return GST_V4LELEMENT(v4lmjpegsrc)->buffer+(v4lmjpegsrc->breq.size*num);
498 /******************************************************
499 * gst_v4lmjpegsrc_requeue_frame():
500 * requeue a frame for capturing
501 * return value: TRUE on success, FALSE on error
502 ******************************************************/
505 gst_v4lmjpegsrc_requeue_frame (GstV4lMjpegSrc *v4lmjpegsrc,
508 DEBUG("requeueing frame %d", num);
509 GST_V4L_CHECK_OPEN(GST_V4LELEMENT(v4lmjpegsrc));
510 GST_V4L_CHECK_ACTIVE(GST_V4LELEMENT(v4lmjpegsrc));
512 /* mark frame as 'ready to requeue' */
513 g_mutex_lock(v4lmjpegsrc->mutex_queue_state);
515 if (v4lmjpegsrc->frame_queue_state[num] != QUEUE_STATE_SYNCED) {
516 GST_ELEMENT_ERROR (v4lmjpegsrc, RESOURCE, TOO_LAZY, NULL,
517 ("Invalid state %d (expected %d), can't requeue",
518 v4lmjpegsrc->frame_queue_state[num],
519 QUEUE_STATE_SYNCED));
523 v4lmjpegsrc->frame_queue_state[num] = QUEUE_STATE_READY_FOR_QUEUE;
525 /* let an optional wait know */
526 g_cond_broadcast(v4lmjpegsrc->cond_queue_state);
528 g_mutex_unlock(v4lmjpegsrc->mutex_queue_state);
534 /******************************************************
535 * gst_v4lmjpegsrc_capture_stop():
536 * stop streaming capture
537 * return value: TRUE on success, FALSE on error
538 ******************************************************/
541 gst_v4lmjpegsrc_capture_stop (GstV4lMjpegSrc *v4lmjpegsrc)
545 DEBUG("stopping capture");
546 GST_V4L_CHECK_OPEN(GST_V4LELEMENT(v4lmjpegsrc));
547 GST_V4L_CHECK_ACTIVE(GST_V4LELEMENT(v4lmjpegsrc));
549 g_mutex_lock(v4lmjpegsrc->mutex_queue_state);
551 /* make an optional pending wait stop */
552 v4lmjpegsrc->quit = TRUE;
553 g_cond_broadcast(v4lmjpegsrc->cond_queue_state);
555 /* sync on remaining frames */
556 while (v4lmjpegsrc->num_queued > 0) {
557 gst_v4lmjpegsrc_sync_next_frame(v4lmjpegsrc, &n);
560 g_mutex_unlock(v4lmjpegsrc->mutex_queue_state);
566 /******************************************************
567 * gst_v4lmjpegsrc_capture_deinit():
568 * deinitialize the capture system
569 * return value: TRUE on success, FALSE on error
570 ******************************************************/
573 gst_v4lmjpegsrc_capture_deinit (GstV4lMjpegSrc *v4lmjpegsrc)
575 DEBUG("quitting capture subsystem");
576 GST_V4L_CHECK_OPEN(GST_V4LELEMENT(v4lmjpegsrc));
577 GST_V4L_CHECK_ACTIVE(GST_V4LELEMENT(v4lmjpegsrc));
579 /* unmap the buffer */
580 munmap(GST_V4LELEMENT(v4lmjpegsrc)->buffer, v4lmjpegsrc->breq.size * v4lmjpegsrc->breq.count);
581 GST_V4LELEMENT(v4lmjpegsrc)->buffer = NULL;
583 /* free buffer tracker */
584 g_mutex_free(v4lmjpegsrc->mutex_queue_state);
585 g_cond_free(v4lmjpegsrc->cond_queue_state);
586 g_free(v4lmjpegsrc->frame_queue_state);
587 g_free(v4lmjpegsrc->use_num_times);