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 */
51 static void gst_jpegenc_base_init (gpointer g_class);
52 static void gst_jpegenc_class_init (GstJpegEnc *klass);
53 static void gst_jpegenc_init (GstJpegEnc *jpegenc);
55 static void gst_jpegenc_chain (GstPad *pad, GstData *_data);
56 static GstPadLinkReturn gst_jpegenc_link (GstPad *pad, const GstCaps *caps);
57 static GstCaps * gst_jpegenc_getcaps (GstPad *pad);
59 static void gst_jpegenc_resync (GstJpegEnc *jpegenc);
60 static void gst_jpegenc_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec);
61 static void gst_jpegenc_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec);
63 static GstElementClass *parent_class = NULL;
64 static guint gst_jpegenc_signals[LAST_SIGNAL] = { 0 };
67 gst_jpegenc_get_type (void)
69 static GType jpegenc_type = 0;
72 static const GTypeInfo jpegenc_info = {
74 gst_jpegenc_base_init,
76 (GClassInitFunc)gst_jpegenc_class_init,
81 (GInstanceInitFunc)gst_jpegenc_init,
83 jpegenc_type = g_type_register_static(GST_TYPE_ELEMENT, "GstJpegEnc", &jpegenc_info, 0);
88 static GstStaticPadTemplate gst_jpegenc_sink_pad_template =
89 GST_STATIC_PAD_TEMPLATE (
93 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV("I420"))
96 static GstStaticPadTemplate gst_jpegenc_src_pad_template =
97 GST_STATIC_PAD_TEMPLATE (
101 GST_STATIC_CAPS ("image/jpeg, "
102 "width = (int) [ 16, 4096 ], "
103 "height = (int) [ 16, 4096 ], "
104 "framerate = (double) [ 1, MAX ]"
109 gst_jpegenc_base_init (gpointer g_class)
111 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
113 gst_element_class_add_pad_template (element_class,
114 gst_static_pad_template_get (&gst_jpegenc_sink_pad_template));
115 gst_element_class_add_pad_template (element_class,
116 gst_static_pad_template_get (&gst_jpegenc_src_pad_template));
117 gst_element_class_set_details (element_class, &gst_jpegenc_details);
121 gst_jpegenc_class_init (GstJpegEnc *klass)
123 GObjectClass *gobject_class;
124 GstElementClass *gstelement_class;
126 gobject_class = (GObjectClass*)klass;
127 gstelement_class = (GstElementClass*)klass;
129 parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
131 gst_jpegenc_signals[FRAME_ENCODED] =
132 g_signal_new ("frame-encoded", G_TYPE_FROM_CLASS(klass), G_SIGNAL_RUN_LAST,
133 G_STRUCT_OFFSET (GstJpegEncClass, frame_encoded), NULL, NULL,
134 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
136 g_object_class_install_property (gobject_class, ARG_QUALITY,
137 g_param_spec_int ("quality", "Quality", "Quality of encoding",
138 0, 100, 85, G_PARAM_READWRITE));
140 /* disabled, since it doesn't seem to work */
141 g_object_class_install_property (gobject_class, ARG_SMOOTHING,
142 g_param_spec_int ("smoothing", "Smoothing", "Smoothing factor",
143 0, 100, 0, G_PARAM_READWRITE));
146 gobject_class->set_property = gst_jpegenc_set_property;
147 gobject_class->get_property = gst_jpegenc_get_property;
151 gst_jpegenc_init_destination (j_compress_ptr cinfo)
153 GST_DEBUG ("gst_jpegenc_chain: init_destination");
157 gst_jpegenc_flush_destination (j_compress_ptr cinfo)
159 GST_DEBUG ("gst_jpegenc_chain: flush_destination: buffer too small !!!");
163 static void gst_jpegenc_term_destination (j_compress_ptr cinfo)
165 GST_DEBUG ("gst_jpegenc_chain: term_source");
169 gst_jpegenc_init (GstJpegEnc *jpegenc)
171 /* create the sink and src pads */
172 jpegenc->sinkpad = gst_pad_new_from_template (
173 gst_static_pad_template_get (&gst_jpegenc_sink_pad_template), "sink");
174 gst_pad_set_chain_function(jpegenc->sinkpad,gst_jpegenc_chain);
175 gst_pad_set_getcaps_function(jpegenc->sinkpad, gst_jpegenc_getcaps);
176 gst_pad_set_link_function(jpegenc->sinkpad, gst_jpegenc_link);
177 gst_element_add_pad(GST_ELEMENT(jpegenc),jpegenc->sinkpad);
179 jpegenc->srcpad = gst_pad_new_from_template (
180 gst_static_pad_template_get (&gst_jpegenc_src_pad_template), "src");
181 gst_pad_set_getcaps_function(jpegenc->sinkpad, gst_jpegenc_getcaps);
182 gst_pad_set_link_function(jpegenc->sinkpad, gst_jpegenc_link);
183 gst_element_add_pad(GST_ELEMENT(jpegenc),jpegenc->srcpad);
185 /* reset the initial video state */
187 jpegenc->height = -1;
190 memset(&jpegenc->cinfo, 0, sizeof(jpegenc->cinfo));
191 memset(&jpegenc->jerr, 0, sizeof(jpegenc->jerr));
192 jpegenc->cinfo.err = jpeg_std_error(&jpegenc->jerr);
193 jpeg_create_compress(&jpegenc->cinfo);
195 GST_DEBUG ("gst_jpegenc_init: setting line buffers");
196 jpegenc->line[0] = NULL;
197 jpegenc->line[1] = NULL;
198 jpegenc->line[2] = NULL;
200 gst_jpegenc_resync(jpegenc);
202 jpegenc->jdest.init_destination = gst_jpegenc_init_destination;
203 jpegenc->jdest.empty_output_buffer = gst_jpegenc_flush_destination;
204 jpegenc->jdest.term_destination = gst_jpegenc_term_destination;
205 jpegenc->cinfo.dest = &jpegenc->jdest;
207 jpegenc->quality = 85;
208 jpegenc->smoothing = 0;
212 gst_jpegenc_getcaps (GstPad *pad)
214 GstJpegEnc *jpegenc = GST_JPEGENC (gst_pad_get_parent (pad));
219 GstStructure *structure;
221 otherpad = (pad == jpegenc->srcpad) ? jpegenc->sinkpad : jpegenc->srcpad;
222 caps = gst_pad_get_allowed_caps (otherpad);
223 if (pad == jpegenc->srcpad) {
226 name = "video/x-raw-yuv";
228 for (i=0;i<gst_caps_get_size (caps); i++){
229 structure = gst_caps_get_structure (caps, i);
231 gst_structure_set_name (structure, name);
232 gst_structure_remove_field (structure, "format");
238 static GstPadLinkReturn
239 gst_jpegenc_link (GstPad *pad, const GstCaps *caps)
241 GstJpegEnc *jpegenc = GST_JPEGENC (gst_pad_get_parent (pad));
242 GstStructure *structure;
243 GstPadLinkReturn ret;
247 otherpad = (pad == jpegenc->srcpad) ? jpegenc->sinkpad : jpegenc->srcpad;
249 structure = gst_caps_get_structure (caps, 0);
250 gst_structure_get_double (structure, "framerate", &jpegenc->fps);
251 gst_structure_get_int (structure, "width", &jpegenc->width);
252 gst_structure_get_int (structure, "height", &jpegenc->height);
254 othercaps = gst_caps_copy (gst_pad_get_pad_template_caps (otherpad));
255 gst_caps_set_simple (othercaps,
256 "width", G_TYPE_INT, jpegenc->width,
257 "height", G_TYPE_INT, jpegenc->height,
258 "framerate", G_TYPE_DOUBLE, jpegenc->fps,
261 ret = gst_pad_try_set_caps (jpegenc->srcpad, othercaps);
262 gst_caps_free(othercaps);
264 if (GST_PAD_LINK_SUCCESSFUL (ret)) {
265 gst_jpegenc_resync (jpegenc);
272 gst_jpegenc_resync (GstJpegEnc *jpegenc)
277 GST_DEBUG ("gst_jpegenc_resync: resync");
279 jpegenc->cinfo.image_width = width = jpegenc->width;
280 jpegenc->cinfo.image_height = height = jpegenc->height;
281 jpegenc->cinfo.input_components = 3;
283 GST_DEBUG ("gst_jpegenc_resync: wdith %d, height %d", width, height);
285 jpeg_set_defaults(&jpegenc->cinfo);
286 jpegenc->cinfo.dct_method = JDCT_FASTEST;
287 /*jpegenc->cinfo.dct_method = JDCT_DEFAULT;*/
288 /*jpegenc->cinfo.smoothing_factor = jpegenc->smoothing; */
289 jpeg_set_quality(&jpegenc->cinfo, jpegenc->quality, TRUE);
292 switch (jpegenc->format) {
293 case GST_COLORSPACE_RGB24:
295 GST_DEBUG ("gst_jpegenc_resync: setting format to RGB24");
296 jpegenc->cinfo.in_color_space = JCS_RGB;
297 jpegenc->cinfo.raw_data_in = FALSE;
299 case GST_COLORSPACE_YUV420P:
302 jpegenc->cinfo.raw_data_in = TRUE;
303 jpegenc->cinfo.in_color_space = JCS_YCbCr;
304 GST_DEBUG ("gst_jpegenc_resync: setting format to YUV420P");
305 jpegenc->cinfo.comp_info[0].h_samp_factor = 2;
306 jpegenc->cinfo.comp_info[0].v_samp_factor = 2;
307 jpegenc->cinfo.comp_info[1].h_samp_factor = 1;
308 jpegenc->cinfo.comp_info[1].v_samp_factor = 1;
309 jpegenc->cinfo.comp_info[2].h_samp_factor = 1;
310 jpegenc->cinfo.comp_info[2].v_samp_factor = 1;
313 jpegenc->line[0] = g_realloc(jpegenc->line[0], height*sizeof(char*));
314 jpegenc->line[1] = g_realloc(jpegenc->line[1], height*sizeof(char*)/2);
315 jpegenc->line[2] = g_realloc(jpegenc->line[2], height*sizeof(char*)/2);
318 GST_DEBUG ("gst_jpegenc_resync: setting format done");
322 printf("gst_jpegenc_resync: unsupported colorspace, using RGB\n");
324 jpegenc->cinfo.in_color_space = JCS_RGB;
328 jpegenc->bufsize = jpegenc->width*jpegenc->height*size;
330 jpeg_suppress_tables(&jpegenc->cinfo, TRUE);
331 //jpeg_suppress_tables(&jpegenc->cinfo, FALSE);
333 jpegenc->buffer = NULL;
334 GST_DEBUG ("gst_jpegenc_resync: resync done");
338 gst_jpegenc_chain (GstPad *pad, GstData *_data)
340 GstBuffer *buf = GST_BUFFER (_data);
342 guchar *data, *outdata;
343 gulong size, outsize;
346 guint height, width, width2;
350 g_return_if_fail (pad != NULL);
351 g_return_if_fail (GST_IS_PAD (pad));
352 g_return_if_fail (buf != NULL);
353 /*g_return_if_fail(GST_IS_BUFFER(buf)); */
356 jpegenc = GST_JPEGENC (GST_OBJECT_PARENT (pad));
358 data = GST_BUFFER_DATA(buf);
359 size = GST_BUFFER_SIZE(buf);
361 GST_DEBUG ("gst_jpegenc_chain: got buffer of %ld bytes in '%s'",size,
362 GST_OBJECT_NAME (jpegenc));
364 outbuf = gst_buffer_new();
365 outsize = GST_BUFFER_SIZE(outbuf) = jpegenc->bufsize;
366 outdata = GST_BUFFER_DATA(outbuf) = g_malloc(outsize);
367 GST_BUFFER_TIMESTAMP(outbuf) = GST_BUFFER_TIMESTAMP(buf);
369 width = jpegenc->width;
370 height = jpegenc->height;
373 base[1] = base[0]+width*height;
374 base[2] = base[1]+width*height/4;
376 jpegenc->jdest.next_output_byte = outdata;
377 jpegenc->jdest.free_in_buffer = outsize;
379 jpegenc->cinfo.smoothing_factor = jpegenc->smoothing;
380 jpeg_set_quality(&jpegenc->cinfo, jpegenc->quality, TRUE);
381 jpeg_start_compress(&jpegenc->cinfo, TRUE);
384 GST_DEBUG ("gst_jpegdec_chain: compressing");
386 for (i = 0; i < height; i += 2*DCTSIZE) {
387 for (j=0, k=0; j<2*DCTSIZE;j+=2, k++) {
388 jpegenc->line[0][j] = base[0]; base[0] += width;
389 jpegenc->line[0][j+1] = base[0]; base[0] += width;
390 jpegenc->line[1][k] = base[1]; base[1] += width2;
391 jpegenc->line[2][k] = base[2]; base[2] += width2;
393 jpeg_write_raw_data(&jpegenc->cinfo, jpegenc->line, 2*DCTSIZE);
395 jpeg_finish_compress(&jpegenc->cinfo);
396 GST_DEBUG ("gst_jpegdec_chain: compressing done");
398 GST_BUFFER_SIZE(outbuf) = (((outsize - jpegenc->jdest.free_in_buffer)+3)&~3);
400 gst_pad_push(jpegenc->srcpad, GST_DATA (outbuf));
402 g_signal_emit(G_OBJECT(jpegenc),gst_jpegenc_signals[FRAME_ENCODED], 0);
404 gst_buffer_unref(buf);
408 gst_jpegenc_set_property (GObject * object, guint prop_id,
409 const GValue * value, GParamSpec * pspec)
413 g_return_if_fail (GST_IS_JPEGENC (object));
414 jpegenc = GST_JPEGENC (object);
418 jpegenc->quality = g_value_get_int (value);
421 jpegenc->smoothing = g_value_get_int (value);
429 gst_jpegenc_get_property (GObject * object, guint prop_id, GValue * value,
434 g_return_if_fail (GST_IS_JPEGENC (object));
435 jpegenc = GST_JPEGENC (object);
439 g_value_set_int (value, jpegenc->quality);
442 g_value_set_int (value, jpegenc->smoothing);
445 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);