3 * gstv4ljpegsrc.c: V4L source element for JPEG cameras
5 * Copyright (C) 2004-2005 Jan Schmidt <thaytan@mad.scientist.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 e Boston, MA 02111-1307, USA.
29 #include "gstv4ljpegsrc.h"
30 #include "v4lsrc_calls.h"
32 /* elementfactory information */
33 static const GstElementDetails gst_v4ljpegsrc_details =
34 GST_ELEMENT_DETAILS ("Video (video4linux/raw) Jpeg Source",
36 "Reads jpeg frames from a video4linux (eg ov519) device",
37 "Jan Schmidt <thaytan@mad.scientist.com>");
39 GST_DEBUG_CATEGORY_STATIC (v4ljpegsrc_debug);
40 #define GST_CAT_DEFAULT v4ljpegsrc_debug
43 static void gst_v4ljpegsrc_base_init (gpointer g_class);
44 static void gst_v4ljpegsrc_class_init (GstV4lJpegSrcClass * klass);
45 static void gst_v4ljpegsrc_init (GstV4lJpegSrc * v4ljpegsrc);
47 /* buffer functions */
48 static GstPadLinkReturn gst_v4ljpegsrc_src_link (GstPad * pad,
49 const GstCaps * caps);
50 static GstCaps *gst_v4ljpegsrc_getcaps (GstPad * pad);
51 static GstData *gst_v4ljpegsrc_get (GstPad * pad);
53 static GstElementClass *parent_class = NULL;
56 gst_v4ljpegsrc_get_type (void)
58 static GType v4ljpegsrc_type = 0;
60 if (!v4ljpegsrc_type) {
61 static const GTypeInfo v4ljpegsrc_info = {
62 sizeof (GstV4lJpegSrcClass),
63 gst_v4ljpegsrc_base_init,
65 (GClassInitFunc) gst_v4ljpegsrc_class_init,
68 sizeof (GstV4lJpegSrc),
70 (GInstanceInitFunc) gst_v4ljpegsrc_init,
75 g_type_register_static (GST_TYPE_V4LSRC, "GstV4lJpegSrc",
77 GST_DEBUG_CATEGORY_INIT (v4ljpegsrc_debug, "v4ljpegsrc", 0,
78 "V4L JPEG source element");
80 return v4ljpegsrc_type;
84 gst_v4ljpegsrc_base_init (gpointer g_class)
86 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
88 gst_element_class_set_details (gstelement_class, &gst_v4ljpegsrc_details);
92 gst_v4ljpegsrc_class_init (GstV4lJpegSrcClass * klass)
94 parent_class = g_type_class_peek_parent (klass);
98 gst_v4ljpegsrc_init (GstV4lJpegSrc * v4ljpegsrc)
100 GstV4lSrc *v4lsrc = GST_V4LSRC (v4ljpegsrc);
101 GstPad *pad = v4lsrc->srcpad;
104 * Stash away and then replace the getcaps and get functions on the src pad
106 v4ljpegsrc->getfn = GST_RPAD_GETFUNC (pad);
107 v4ljpegsrc->getcapsfn = GST_RPAD_GETCAPSFUNC (pad);
109 gst_pad_set_get_function (v4lsrc->srcpad, gst_v4ljpegsrc_get);
110 gst_pad_set_getcaps_function (v4lsrc->srcpad, gst_v4ljpegsrc_getcaps);
111 gst_pad_set_link_function (v4lsrc->srcpad, gst_v4ljpegsrc_src_link);
114 static GstPadLinkReturn
115 gst_v4ljpegsrc_src_link (GstPad * pad, const GstCaps * vscapslist)
117 GstV4lJpegSrc *v4ljpegsrc;
119 gint w, h, palette = -1;
121 GstStructure *structure;
122 gboolean was_capturing;
123 struct video_window *vwin;
125 v4ljpegsrc = GST_V4LJPEGSRC (gst_pad_get_parent (pad));
126 v4lsrc = GST_V4LSRC (v4ljpegsrc);
127 vwin = &GST_V4LELEMENT (v4lsrc)->vwin;
128 was_capturing = v4lsrc->is_capturing;
130 /* in case the buffers are active (which means that we already
131 * did capsnego before and didn't clean up), clean up anyways */
132 if (GST_V4L_IS_ACTIVE (GST_V4LELEMENT (v4lsrc))) {
134 if (!gst_v4lsrc_capture_stop (v4lsrc))
135 return GST_PAD_LINK_REFUSED;
137 if (!gst_v4lsrc_capture_deinit (v4lsrc))
138 return GST_PAD_LINK_REFUSED;
139 } else if (!GST_V4L_IS_OPEN (GST_V4LELEMENT (v4lsrc))) {
140 return GST_PAD_LINK_DELAYED;
143 structure = gst_caps_get_structure (vscapslist, 0);
145 gst_structure_get_int (structure, "width", &w);
146 gst_structure_get_int (structure, "height", &h);
147 fps = gst_structure_get_value (structure, "framerate");
149 GST_DEBUG_OBJECT (v4ljpegsrc, "linking with %dx%d at %d/%d fps", w, h,
150 gst_value_get_fraction_numerator (fps),
151 gst_value_get_fraction_denominator (fps));
153 /* set framerate if it's not already correct */
154 if (fps != gst_v4lsrc_get_fps (v4lsrc)) {
155 int fps_index = fps / 15.0 * 16;
157 GST_DEBUG_OBJECT (v4ljpegsrc, "Trying to set fps index %d", fps_index);
158 /* set bits 16 to 21 to 0 */
159 vwin->flags &= (0x3F00 - 1);
160 /* set bits 16 to 21 to the index */
161 vwin->flags |= fps_index << 16;
162 if (!gst_v4l_set_window_properties (GST_V4LELEMENT (v4lsrc))) {
163 return GST_PAD_LINK_DELAYED;
168 * Try to set the camera to capture RGB24
170 palette = VIDEO_PALETTE_RGB24;
171 v4lsrc->buffer_size = w * h * 3;
173 GST_DEBUG_OBJECT (v4ljpegsrc, "trying to set_capture %dx%d, palette %d",
175 /* this only fills in v4lsrc->mmap values */
176 if (!gst_v4lsrc_set_capture (v4lsrc, w, h, palette)) {
177 GST_WARNING_OBJECT (v4ljpegsrc, "could not set_capture %dx%d, palette %d",
179 return GST_PAD_LINK_REFUSED;
182 /* first try the negotiated settings using try_capture */
183 if (!gst_v4lsrc_try_capture (v4lsrc, w, h, palette)) {
184 GST_DEBUG_OBJECT (v4ljpegsrc, "failed trying palette %d for %dx%d", palette,
186 return GST_PAD_LINK_REFUSED;
189 if (!gst_v4lsrc_capture_init (v4lsrc))
190 return GST_PAD_LINK_REFUSED;
192 if (was_capturing || GST_STATE (v4lsrc) == GST_STATE_PLAYING) {
193 if (!gst_v4lsrc_capture_start (v4lsrc))
194 return GST_PAD_LINK_REFUSED;
197 return GST_PAD_LINK_OK;
201 gst_v4ljpegsrc_getcaps (GstPad * pad)
204 GstV4lJpegSrc *v4ljpegsrc = GST_V4LJPEGSRC (gst_pad_get_parent (pad));
205 GstV4lSrc *v4lsrc = GST_V4LSRC (v4ljpegsrc);
206 struct video_capability *vcap = &GST_V4LELEMENT (v4lsrc)->vcap;
209 if (!GST_V4L_IS_OPEN (GST_V4LELEMENT (v4lsrc))) {
210 return gst_caps_new_any ();
212 if (!v4lsrc->autoprobe) {
213 /* FIXME: query current caps and return those, with _any appended */
214 return gst_caps_new_any ();
217 list = gst_caps_new_simple ("image/jpeg", NULL);
218 GST_DEBUG_OBJECT (v4ljpegsrc,
219 "Device reports w: %d-%d, h: %d-%d, fps: %f",
220 vcap->minwidth, vcap->maxwidth, vcap->minheight, vcap->maxheight, fps);
222 if (vcap->minwidth < vcap->maxwidth) {
223 gst_caps_set_simple (list, "width", GST_TYPE_INT_RANGE, vcap->minwidth,
224 vcap->maxwidth, NULL);
226 gst_caps_set_simple (list, "width", G_TYPE_INT, vcap->minwidth, NULL);
228 if (vcap->minheight < vcap->maxheight) {
229 gst_caps_set_simple (list, "height", GST_TYPE_INT_RANGE, vcap->minheight,
230 vcap->maxheight, NULL);
232 gst_caps_set_simple (list, "height", G_TYPE_INT, vcap->minheight, NULL);
235 if (v4lsrc->fps_list) {
236 GstStructure *structure = gst_caps_get_structure (list, 0);
238 gst_structure_set_value (structure, "framerate", v4lsrc->fps_list);
240 GST_DEBUG_OBJECT (v4ljpegsrc, "caps: %" GST_PTR_FORMAT, list);
246 gst_v4ljpegsrc_get (GstPad * pad)
248 GstV4lJpegSrc *v4ljpegsrc;
255 g_return_val_if_fail (pad != NULL, NULL);
256 v4ljpegsrc = GST_V4LJPEGSRC (gst_pad_get_parent (pad));
257 v4lsrc = GST_V4LSRC (v4ljpegsrc);
259 /* Fetch from the v4lsrc class get fn. */
260 data = v4ljpegsrc->getfn (pad);
262 /* If not a buffer, return it unchanged */
263 if (!data || (!GST_IS_BUFFER (data)))
266 buf = GST_BUFFER (data);
268 /* Confirm that the buffer contains jpeg data */
271 * Create a new subbuffer from the jpeg data
272 * The first 2 bytes in the buffer are the size of the jpeg data
274 if (GST_BUFFER_SIZE (buf) > 2) {
275 jpeg_size = (int) (GST_READ_UINT16_LE (GST_BUFFER_DATA (buf))) * 8;
279 /* Check that the size is sensible */
280 if ((jpeg_size <= 0) || (jpeg_size > GST_BUFFER_SIZE (buf) - 2)) {
281 GST_ELEMENT_ERROR (v4ljpegsrc, STREAM, FORMAT, (NULL),
282 ("Invalid non-jpeg frame from camera"));
286 GST_DEBUG_OBJECT (v4ljpegsrc, "Creating JPEG subbuffer of size %d",
288 outbuf = gst_buffer_create_sub (buf, 2, jpeg_size);
290 /* Copy timestamps onto the subbuffer */
291 gst_buffer_stamp (outbuf, buf);
293 /* Release the main buffer */
294 gst_buffer_unref (buf);
296 return GST_DATA (outbuf);