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.
23 #include "gstjpegenc.h"
25 /* elementfactory information */
26 GstElementDetails gst_jpegenc_details = {
28 "Filter/Encoder/Image",
31 "Wim Taymans <wim.taymans@tvd.be>",
35 /* JpegEnc signals and args */
47 static void gst_jpegenc_class_init (GstJpegEnc *klass);
48 static void gst_jpegenc_init (GstJpegEnc *jpegenc);
50 static void gst_jpegenc_chain (GstPad *pad,GstBuffer *buf);
51 static GstBuffer *gst_jpegenc_get (GstPad *pad);
53 static void gst_jpegenc_resync (GstJpegEnc *jpegenc);
55 static GstElementClass *parent_class = NULL;
56 static guint gst_jpegenc_signals[LAST_SIGNAL] = { 0 };
59 gst_jpegenc_get_type (void)
61 static GType jpegenc_type = 0;
64 static const GTypeInfo jpegenc_info = {
65 sizeof(GstJpegEnc), NULL,
67 (GClassInitFunc)gst_jpegenc_class_init,
72 (GInstanceInitFunc)gst_jpegenc_init,
74 jpegenc_type = g_type_register_static(GST_TYPE_ELEMENT, "GstJpegEnc", &jpegenc_info, 0);
80 gst_jpegenc_class_init (GstJpegEnc *klass)
82 GObjectClass *gobject_class;
83 GstElementClass *gstelement_class;
85 gobject_class = (GObjectClass*)klass;
86 gstelement_class = (GstElementClass*)klass;
88 parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
90 gst_jpegenc_signals[FRAME_ENCODED] =
91 g_signal_new ("frame_encoded", G_TYPE_FROM_CLASS(klass), G_SIGNAL_RUN_LAST,
92 G_STRUCT_OFFSET (GstJpegEncClass, frame_encoded), NULL, NULL,
93 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
98 gst_jpegenc_init_destination (j_compress_ptr cinfo)
100 GST_DEBUG (0,"gst_jpegenc_chain: init_destination\n");
104 gst_jpegenc_flush_destination (j_compress_ptr cinfo)
106 GST_DEBUG (0,"gst_jpegenc_chain: flush_destination: buffer too small !!!\n");
110 static void gst_jpegenc_term_destination (j_compress_ptr cinfo)
112 GST_DEBUG (0,"gst_jpegenc_chain: term_source\n");
116 gst_jpegenc_init (GstJpegEnc *jpegenc)
118 /* create the sink and src pads */
119 jpegenc->sinkpad = gst_pad_new("sink",GST_PAD_SINK);
120 gst_element_add_pad(GST_ELEMENT(jpegenc),jpegenc->sinkpad);
121 gst_pad_set_chain_function(jpegenc->sinkpad,gst_jpegenc_chain);
122 gst_pad_set_get_function(jpegenc->sinkpad,gst_jpegenc_get);
123 jpegenc->srcpad = gst_pad_new("src",GST_PAD_SRC);
124 gst_element_add_pad(GST_ELEMENT(jpegenc),jpegenc->srcpad);
126 /* reset the initial video state */
128 jpegenc->height = -1;
131 memset(&jpegenc->cinfo, 0, sizeof(jpegenc->cinfo));
132 memset(&jpegenc->jerr, 0, sizeof(jpegenc->jerr));
133 jpegenc->cinfo.err = jpeg_std_error(&jpegenc->jerr);
134 jpeg_create_compress(&jpegenc->cinfo);
136 GST_DEBUG (0,"gst_jpegenc_init: setting line buffers\n");
137 jpegenc->line[0] = NULL;
138 jpegenc->line[1] = NULL;
139 jpegenc->line[2] = NULL;
141 gst_jpegenc_resync(jpegenc);
143 jpegenc->jdest.init_destination = gst_jpegenc_init_destination;
144 jpegenc->jdest.empty_output_buffer = gst_jpegenc_flush_destination;
145 jpegenc->jdest.term_destination = gst_jpegenc_term_destination;
146 jpegenc->cinfo.dest = &jpegenc->jdest;
151 gst_jpegenc_resync (GstJpegEnc *jpegenc)
156 GST_DEBUG (0,"gst_jpegenc_resync: resync\n");
158 jpegenc->cinfo.image_width = width = jpegenc->width;
159 jpegenc->cinfo.image_height = height = jpegenc->height;
160 jpegenc->cinfo.input_components = 3;
162 GST_DEBUG (0,"gst_jpegenc_resync: wdith %d, height %d\n", width, height);
164 jpeg_set_defaults(&jpegenc->cinfo);
165 jpegenc->cinfo.dct_method = JDCT_FASTEST;
166 /*jpegenc->cinfo.dct_method = JDCT_DEFAULT; */
167 /*jpegenc->cinfo.smoothing_factor = 10; */
168 jpeg_set_quality(&jpegenc->cinfo, 85, TRUE);
171 switch (jpegenc->format) {
172 case GST_COLORSPACE_RGB24:
174 GST_DEBUG (0,"gst_jpegenc_resync: setting format to RGB24\n");
175 jpegenc->cinfo.in_color_space = JCS_RGB;
176 jpegenc->cinfo.raw_data_in = FALSE;
178 case GST_COLORSPACE_YUV420P:
180 jpegenc->cinfo.raw_data_in = TRUE;
181 jpegenc->cinfo.in_color_space = JCS_YCbCr;
182 GST_DEBUG (0,"gst_jpegenc_resync: setting format to YUV420P\n");
183 jpegenc->cinfo.comp_info[0].h_samp_factor = 2;
184 jpegenc->cinfo.comp_info[0].v_samp_factor = 2;
185 jpegenc->cinfo.comp_info[1].h_samp_factor = 1;
186 jpegenc->cinfo.comp_info[1].v_samp_factor = 1;
187 jpegenc->cinfo.comp_info[2].h_samp_factor = 1;
188 jpegenc->cinfo.comp_info[2].v_samp_factor = 1;
191 jpegenc->line[0] = g_realloc(jpegenc->line[0], height*sizeof(char*));
192 jpegenc->line[1] = g_realloc(jpegenc->line[1], height*sizeof(char*)/2);
193 jpegenc->line[2] = g_realloc(jpegenc->line[2], height*sizeof(char*)/2);
196 GST_DEBUG (0,"gst_jpegenc_resync: setting format done\n");
199 printf("gst_jpegenc_resync: unsupported colorspace, using RGB\n");
201 jpegenc->cinfo.in_color_space = JCS_RGB;
205 jpegenc->bufsize = jpegenc->width*jpegenc->height*size;
206 jpegenc->row_stride = width * size;
208 jpeg_suppress_tables(&jpegenc->cinfo, TRUE);
210 jpegenc->buffer = NULL;
211 GST_DEBUG (0,"gst_jpegenc_resync: resync done\n");
215 gst_jpegenc_get (GstPad *pad)
220 GST_DEBUG (0,"gst_jpegenc_chain: pull buffer\n");
222 g_return_val_if_fail (pad != NULL, NULL);
223 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
225 jpegenc = GST_JPEGENC (GST_OBJECT_PARENT (pad));
227 if (jpegenc->buffer == NULL || GST_BUFFER_REFCOUNT(jpegenc->buffer) != 1) {
228 if (jpegenc->buffer) gst_buffer_unref(jpegenc->buffer);
229 GST_DEBUG (0,"gst_jpegenc_chain: new buffer\n");
230 newbuf = jpegenc->buffer = gst_buffer_new();
231 GST_BUFFER_DATA(newbuf) = g_malloc(jpegenc->bufsize);
232 GST_BUFFER_SIZE(newbuf) = jpegenc->bufsize;
234 gst_buffer_ref(jpegenc->buffer);
236 return jpegenc->buffer;
240 gst_jpegenc_chain (GstPad *pad, GstBuffer *buf)
243 guchar *data, *outdata;
244 gulong size, outsize;
247 guint height, width, width2;
251 g_return_if_fail (pad != NULL);
252 g_return_if_fail (GST_IS_PAD (pad));
253 g_return_if_fail (buf != NULL);
254 /*g_return_if_fail(GST_IS_BUFFER(buf)); */
257 jpegenc = GST_JPEGENC (GST_OBJECT_PARENT (pad));
259 data = GST_BUFFER_DATA(buf);
260 size = GST_BUFFER_SIZE(buf);
262 GST_DEBUG (0,"gst_jpegenc_chain: got buffer of %ld bytes in '%s'\n",size,
263 GST_OBJECT_NAME (jpegenc));
265 outbuf = gst_buffer_new();
266 outsize = GST_BUFFER_SIZE(outbuf) = jpegenc->bufsize;
267 outdata = GST_BUFFER_DATA(outbuf) = g_malloc(outsize);
268 GST_BUFFER_TIMESTAMP(outbuf) = GST_BUFFER_TIMESTAMP(buf);
270 width = jpegenc->width;
271 height = jpegenc->height;
274 base[1] = base[0]+width*height;
275 base[2] = base[1]+width*height/4;
277 jpegenc->jdest.next_output_byte = outdata;
278 jpegenc->jdest.free_in_buffer = outsize;
280 jpeg_start_compress(&jpegenc->cinfo, TRUE);
283 GST_DEBUG (0,"gst_jpegdec_chain: compressing\n");
285 for (i = 0; i < height; i += 2*DCTSIZE) {
286 for (j=0, k=0; j<2*DCTSIZE;j+=2, k++) {
287 jpegenc->line[0][j] = base[0]; base[0] += width;
288 jpegenc->line[0][j+1] = base[0]; base[0] += width;
289 jpegenc->line[1][k] = base[1]; base[1] += width2;
290 jpegenc->line[2][k] = base[2]; base[2] += width2;
292 jpeg_write_raw_data(&jpegenc->cinfo, jpegenc->line, 2*DCTSIZE);
294 jpeg_finish_compress(&jpegenc->cinfo);
295 GST_DEBUG (0,"gst_jpegdec_chain: compressing done\n");
297 GST_BUFFER_SIZE(outbuf) = (((outsize - jpegenc->jdest.free_in_buffer)+3)&~3);
299 gst_pad_push(jpegenc->srcpad, outbuf);
301 g_signal_emit(G_OBJECT(jpegenc),gst_jpegenc_signals[FRAME_ENCODED], 0);
303 gst_buffer_unref(buf);