2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
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.
26 #include "gstjpegenc.h"
27 #include <gst/video/video.h>
29 /* elementfactory information */
30 GstElementDetails gst_jpegenc_details = {
32 "Codec/Encoder/Image",
33 "Encode images in JPEG format",
34 "Wim Taymans <wim.taymans@tvd.be>",
37 /* JpegEnc signals and args */
53 static void gst_jpegenc_base_init (gpointer g_class);
54 static void gst_jpegenc_class_init (GstJpegEnc * klass);
55 static void gst_jpegenc_init (GstJpegEnc * jpegenc);
57 static void gst_jpegenc_chain (GstPad * pad, GstData * _data);
58 static GstPadLinkReturn gst_jpegenc_link (GstPad * pad, const GstCaps * caps);
59 static GstCaps *gst_jpegenc_getcaps (GstPad * pad);
61 static void gst_jpegenc_resync (GstJpegEnc * jpegenc);
62 static void gst_jpegenc_set_property (GObject * object, guint prop_id,
63 const GValue * value, GParamSpec * pspec);
64 static void gst_jpegenc_get_property (GObject * object, guint prop_id,
65 GValue * value, GParamSpec * pspec);
67 static GstElementClass *parent_class = NULL;
68 static guint gst_jpegenc_signals[LAST_SIGNAL] = { 0 };
71 gst_jpegenc_get_type (void)
73 static GType jpegenc_type = 0;
76 static const GTypeInfo jpegenc_info = {
78 gst_jpegenc_base_init,
80 (GClassInitFunc) gst_jpegenc_class_init,
85 (GInstanceInitFunc) gst_jpegenc_init,
88 g_type_register_static (GST_TYPE_ELEMENT, "GstJpegEnc", &jpegenc_info,
94 static GstStaticPadTemplate gst_jpegenc_sink_pad_template =
95 GST_STATIC_PAD_TEMPLATE ("sink",
98 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("I420"))
101 static GstStaticPadTemplate gst_jpegenc_src_pad_template =
102 GST_STATIC_PAD_TEMPLATE ("src",
105 GST_STATIC_CAPS ("image/jpeg, "
106 "width = (int) [ 16, 4096 ], "
107 "height = (int) [ 16, 4096 ], " "framerate = (double) [ 1, MAX ]")
111 gst_jpegenc_base_init (gpointer g_class)
113 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
115 gst_element_class_add_pad_template (element_class,
116 gst_static_pad_template_get (&gst_jpegenc_sink_pad_template));
117 gst_element_class_add_pad_template (element_class,
118 gst_static_pad_template_get (&gst_jpegenc_src_pad_template));
119 gst_element_class_set_details (element_class, &gst_jpegenc_details);
123 gst_jpegenc_class_init (GstJpegEnc * klass)
125 GObjectClass *gobject_class;
126 GstElementClass *gstelement_class;
128 gobject_class = (GObjectClass *) klass;
129 gstelement_class = (GstElementClass *) klass;
131 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
133 gst_jpegenc_signals[FRAME_ENCODED] =
134 g_signal_new ("frame-encoded", G_TYPE_FROM_CLASS (klass),
135 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstJpegEncClass, frame_encoded), NULL,
136 NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
138 g_object_class_install_property (gobject_class, ARG_QUALITY,
139 g_param_spec_int ("quality", "Quality", "Quality of encoding",
140 0, 100, 85, G_PARAM_READWRITE));
142 /* disabled, since it doesn't seem to work */
143 g_object_class_install_property (gobject_class, ARG_SMOOTHING,
144 g_param_spec_int ("smoothing", "Smoothing", "Smoothing factor",
145 0, 100, 0, G_PARAM_READWRITE));
148 gobject_class->set_property = gst_jpegenc_set_property;
149 gobject_class->get_property = gst_jpegenc_get_property;
153 gst_jpegenc_init_destination (j_compress_ptr cinfo)
155 GST_DEBUG ("gst_jpegenc_chain: init_destination");
159 gst_jpegenc_flush_destination (j_compress_ptr cinfo)
161 GST_DEBUG ("gst_jpegenc_chain: flush_destination: buffer too small !!!");
166 gst_jpegenc_term_destination (j_compress_ptr cinfo)
168 GST_DEBUG ("gst_jpegenc_chain: term_source");
172 gst_jpegenc_init (GstJpegEnc * jpegenc)
174 /* create the sink and src pads */
176 gst_pad_new_from_template (gst_static_pad_template_get
177 (&gst_jpegenc_sink_pad_template), "sink");
178 gst_pad_set_chain_function (jpegenc->sinkpad, gst_jpegenc_chain);
179 gst_pad_set_getcaps_function (jpegenc->sinkpad, gst_jpegenc_getcaps);
180 gst_pad_set_link_function (jpegenc->sinkpad, gst_jpegenc_link);
181 gst_element_add_pad (GST_ELEMENT (jpegenc), jpegenc->sinkpad);
184 gst_pad_new_from_template (gst_static_pad_template_get
185 (&gst_jpegenc_src_pad_template), "src");
186 gst_pad_set_getcaps_function (jpegenc->sinkpad, gst_jpegenc_getcaps);
187 gst_pad_set_link_function (jpegenc->sinkpad, gst_jpegenc_link);
188 gst_element_add_pad (GST_ELEMENT (jpegenc), jpegenc->srcpad);
190 /* reset the initial video state */
192 jpegenc->height = -1;
195 memset (&jpegenc->cinfo, 0, sizeof (jpegenc->cinfo));
196 memset (&jpegenc->jerr, 0, sizeof (jpegenc->jerr));
197 jpegenc->cinfo.err = jpeg_std_error (&jpegenc->jerr);
198 jpeg_create_compress (&jpegenc->cinfo);
200 GST_DEBUG ("gst_jpegenc_init: setting line buffers");
201 jpegenc->line[0] = NULL;
202 jpegenc->line[1] = NULL;
203 jpegenc->line[2] = NULL;
205 gst_jpegenc_resync (jpegenc);
207 jpegenc->jdest.init_destination = gst_jpegenc_init_destination;
208 jpegenc->jdest.empty_output_buffer = gst_jpegenc_flush_destination;
209 jpegenc->jdest.term_destination = gst_jpegenc_term_destination;
210 jpegenc->cinfo.dest = &jpegenc->jdest;
212 jpegenc->quality = 85;
213 jpegenc->smoothing = 0;
217 gst_jpegenc_getcaps (GstPad * pad)
219 GstJpegEnc *jpegenc = GST_JPEGENC (gst_pad_get_parent (pad));
224 GstStructure *structure;
226 otherpad = (pad == jpegenc->srcpad) ? jpegenc->sinkpad : jpegenc->srcpad;
227 caps = gst_pad_get_allowed_caps (otherpad);
228 if (pad == jpegenc->srcpad) {
231 name = "video/x-raw-yuv";
233 for (i = 0; i < gst_caps_get_size (caps); i++) {
234 structure = gst_caps_get_structure (caps, i);
236 gst_structure_set_name (structure, name);
237 gst_structure_remove_field (structure, "format");
243 static GstPadLinkReturn
244 gst_jpegenc_link (GstPad * pad, const GstCaps * caps)
246 GstJpegEnc *jpegenc = GST_JPEGENC (gst_pad_get_parent (pad));
247 GstStructure *structure;
248 GstPadLinkReturn ret;
252 otherpad = (pad == jpegenc->srcpad) ? jpegenc->sinkpad : jpegenc->srcpad;
254 structure = gst_caps_get_structure (caps, 0);
255 gst_structure_get_double (structure, "framerate", &jpegenc->fps);
256 gst_structure_get_int (structure, "width", &jpegenc->width);
257 gst_structure_get_int (structure, "height", &jpegenc->height);
259 othercaps = gst_caps_copy (gst_pad_get_pad_template_caps (otherpad));
260 gst_caps_set_simple (othercaps,
261 "width", G_TYPE_INT, jpegenc->width,
262 "height", G_TYPE_INT, jpegenc->height,
263 "framerate", G_TYPE_DOUBLE, jpegenc->fps, NULL);
265 ret = gst_pad_try_set_caps (jpegenc->srcpad, othercaps);
266 gst_caps_free (othercaps);
268 if (GST_PAD_LINK_SUCCESSFUL (ret)) {
269 gst_jpegenc_resync (jpegenc);
276 gst_jpegenc_resync (GstJpegEnc * jpegenc)
281 GST_DEBUG ("gst_jpegenc_resync: resync");
283 jpegenc->cinfo.image_width = width = jpegenc->width;
284 jpegenc->cinfo.image_height = height = jpegenc->height;
285 jpegenc->cinfo.input_components = 3;
287 GST_DEBUG ("gst_jpegenc_resync: wdith %d, height %d", width, height);
289 jpeg_set_defaults (&jpegenc->cinfo);
290 jpegenc->cinfo.dct_method = JDCT_FASTEST;
291 /*jpegenc->cinfo.dct_method = JDCT_DEFAULT; */
292 /*jpegenc->cinfo.smoothing_factor = jpegenc->smoothing; */
293 jpeg_set_quality (&jpegenc->cinfo, jpegenc->quality, TRUE);
296 switch (jpegenc->format) {
297 case GST_COLORSPACE_RGB24:
299 GST_DEBUG ("gst_jpegenc_resync: setting format to RGB24");
300 jpegenc->cinfo.in_color_space = JCS_RGB;
301 jpegenc->cinfo.raw_data_in = FALSE;
303 case GST_COLORSPACE_YUV420P:
306 jpegenc->cinfo.raw_data_in = TRUE;
307 jpegenc->cinfo.in_color_space = JCS_YCbCr;
308 GST_DEBUG ("gst_jpegenc_resync: setting format to YUV420P");
309 jpegenc->cinfo.comp_info[0].h_samp_factor = 2;
310 jpegenc->cinfo.comp_info[0].v_samp_factor = 2;
311 jpegenc->cinfo.comp_info[1].h_samp_factor = 1;
312 jpegenc->cinfo.comp_info[1].v_samp_factor = 1;
313 jpegenc->cinfo.comp_info[2].h_samp_factor = 1;
314 jpegenc->cinfo.comp_info[2].v_samp_factor = 1;
318 g_realloc (jpegenc->line[0], height * sizeof (char *));
320 g_realloc (jpegenc->line[1], height * sizeof (char *) / 2);
322 g_realloc (jpegenc->line[2], height * sizeof (char *) / 2);
325 GST_DEBUG ("gst_jpegenc_resync: setting format done");
329 printf ("gst_jpegenc_resync: unsupported colorspace, using RGB\n");
331 jpegenc->cinfo.in_color_space = JCS_RGB;
335 jpegenc->bufsize = jpegenc->width * jpegenc->height * size;
337 jpeg_suppress_tables (&jpegenc->cinfo, TRUE);
338 //jpeg_suppress_tables(&jpegenc->cinfo, FALSE);
340 jpegenc->buffer = NULL;
341 GST_DEBUG ("gst_jpegenc_resync: resync done");
345 gst_jpegenc_chain (GstPad * pad, GstData * _data)
347 GstBuffer *buf = GST_BUFFER (_data);
349 guchar *data, *outdata;
350 gulong size, outsize;
354 guint height, width, width2;
358 g_return_if_fail (pad != NULL);
359 g_return_if_fail (GST_IS_PAD (pad));
360 g_return_if_fail (buf != NULL);
361 /*g_return_if_fail(GST_IS_BUFFER(buf)); */
364 jpegenc = GST_JPEGENC (GST_OBJECT_PARENT (pad));
366 data = GST_BUFFER_DATA (buf);
367 size = GST_BUFFER_SIZE (buf);
369 GST_DEBUG ("gst_jpegenc_chain: got buffer of %ld bytes in '%s'", size,
370 GST_OBJECT_NAME (jpegenc));
372 outbuf = gst_buffer_new ();
373 outsize = GST_BUFFER_SIZE (outbuf) = jpegenc->bufsize;
374 outdata = GST_BUFFER_DATA (outbuf) = g_malloc (outsize);
375 GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buf);
377 width = jpegenc->width;
378 height = jpegenc->height;
381 base[1] = base[0] + width * height;
382 base[2] = base[1] + width * height / 4;
384 jpegenc->jdest.next_output_byte = outdata;
385 jpegenc->jdest.free_in_buffer = outsize;
387 jpegenc->cinfo.smoothing_factor = jpegenc->smoothing;
388 jpeg_set_quality (&jpegenc->cinfo, jpegenc->quality, TRUE);
389 jpeg_start_compress (&jpegenc->cinfo, TRUE);
392 GST_DEBUG ("gst_jpegdec_chain: compressing");
394 for (i = 0; i < height; i += 2 * DCTSIZE) {
395 for (j = 0, k = 0; j < 2 * DCTSIZE; j += 2, k++) {
396 jpegenc->line[0][j] = base[0];
398 jpegenc->line[0][j + 1] = base[0];
400 jpegenc->line[1][k] = base[1];
402 jpegenc->line[2][k] = base[2];
405 jpeg_write_raw_data (&jpegenc->cinfo, jpegenc->line, 2 * DCTSIZE);
407 jpeg_finish_compress (&jpegenc->cinfo);
408 GST_DEBUG ("gst_jpegdec_chain: compressing done");
410 GST_BUFFER_SIZE (outbuf) =
411 (((outsize - jpegenc->jdest.free_in_buffer) + 3) & ~3);
413 gst_pad_push (jpegenc->srcpad, GST_DATA (outbuf));
415 g_signal_emit (G_OBJECT (jpegenc), gst_jpegenc_signals[FRAME_ENCODED], 0);
417 gst_buffer_unref (buf);
421 gst_jpegenc_set_property (GObject * object, guint prop_id,
422 const GValue * value, GParamSpec * pspec)
426 g_return_if_fail (GST_IS_JPEGENC (object));
427 jpegenc = GST_JPEGENC (object);
431 jpegenc->quality = g_value_get_int (value);
434 jpegenc->smoothing = g_value_get_int (value);
442 gst_jpegenc_get_property (GObject * object, guint prop_id, GValue * value,
447 g_return_if_fail (GST_IS_JPEGENC (object));
448 jpegenc = GST_JPEGENC (object);
452 g_value_set_int (value, jpegenc->quality);
455 g_value_set_int (value, jpegenc->smoothing);
458 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);