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 /*#define DEBUG_ENABLED*/
24 #include "gstjpegdec.h"
26 extern GstPadTemplate *jpegdec_src_template, *jpegdec_sink_template;
28 /* elementfactory information */
29 GstElementDetails gst_jpegdec_details = {
31 "Filter/Decoder/Image",
34 "Wim Taymans <wim.taymans@tvd.be>",
38 /* JpegDec signals and args */
49 static void gst_jpegdec_class_init (GstJpegDec *klass);
50 static void gst_jpegdec_init (GstJpegDec *jpegdec);
52 static void gst_jpegdec_chain (GstPad *pad, GstBuffer *buf);
54 static GstElementClass *parent_class = NULL;
55 /*static guint gst_jpegdec_signals[LAST_SIGNAL] = { 0 }; */
58 gst_jpegdec_get_type(void) {
59 static GType jpegdec_type = 0;
62 static const GTypeInfo jpegdec_info = {
63 sizeof(GstJpegDec), NULL,
65 (GClassInitFunc)gst_jpegdec_class_init,
70 (GInstanceInitFunc)gst_jpegdec_init,
72 jpegdec_type = g_type_register_static(GST_TYPE_ELEMENT, "GstJpegDec", &jpegdec_info, 0);
78 gst_jpegdec_class_init (GstJpegDec *klass)
80 GstElementClass *gstelement_class;
82 gstelement_class = (GstElementClass*)klass;
84 parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
88 gst_jpegdec_init_source (j_decompress_ptr cinfo)
90 GST_DEBUG (0,"gst_jpegdec_chain: init_source");
93 gst_jpegdec_fill_input_buffer (j_decompress_ptr cinfo)
95 GST_DEBUG (0,"gst_jpegdec_chain: fill_input_buffer");
100 gst_jpegdec_skip_input_data (j_decompress_ptr cinfo, glong num_bytes)
102 GST_DEBUG (0,"gst_jpegdec_chain: skip_input_data");
106 gst_jpegdec_resync_to_restart (j_decompress_ptr cinfo, gint desired)
108 GST_DEBUG (0,"gst_jpegdec_chain: resync_to_start");
113 gst_jpegdec_term_source (j_decompress_ptr cinfo)
115 GST_DEBUG (0,"gst_jpegdec_chain: term_source");
119 gst_jpegdec_init (GstJpegDec *jpegdec)
121 GST_DEBUG (0,"gst_jpegdec_init: initializing");
122 /* create the sink and src pads */
123 jpegdec->sinkpad = gst_pad_new_from_template (jpegdec_sink_template, "sink");
124 gst_element_add_pad(GST_ELEMENT(jpegdec),jpegdec->sinkpad);
125 gst_pad_set_chain_function(jpegdec->sinkpad,gst_jpegdec_chain);
126 jpegdec->srcpad = gst_pad_new_from_template (jpegdec_src_template, "src");
127 gst_element_add_pad(GST_ELEMENT(jpegdec),jpegdec->srcpad);
129 /* initialize the jpegdec decoder state */
130 jpegdec->next_time = 0;
132 /* reset the initial video state */
133 jpegdec->format = -1;
135 jpegdec->height = -1;
137 jpegdec->line[0] = NULL;
138 jpegdec->line[1] = NULL;
139 jpegdec->line[2] = NULL;
142 memset(&jpegdec->cinfo, 0, sizeof(jpegdec->cinfo));
143 memset(&jpegdec->jerr, 0, sizeof(jpegdec->jerr));
144 jpegdec->cinfo.err = jpeg_std_error(&jpegdec->jerr);
145 jpeg_create_decompress(&jpegdec->cinfo);
147 jpegdec->jsrc.init_source = gst_jpegdec_init_source;
148 jpegdec->jsrc.fill_input_buffer = gst_jpegdec_fill_input_buffer;
149 jpegdec->jsrc.skip_input_data = gst_jpegdec_skip_input_data;
150 jpegdec->jsrc.resync_to_restart = gst_jpegdec_resync_to_restart;
151 jpegdec->jsrc.term_source = gst_jpegdec_term_source;
152 jpegdec->cinfo.src = &jpegdec->jsrc;
157 gst_jpegdec_chain (GstPad *pad, GstBuffer *buf)
160 guchar *data, *outdata;
161 gulong size, outsize;
164 gint width, height, width2;
169 g_return_if_fail(pad != NULL);
170 g_return_if_fail(GST_IS_PAD(pad));
171 g_return_if_fail(buf != NULL);
172 /*g_return_if_fail(GST_IS_BUFFER(buf));*/
174 jpegdec = GST_JPEGDEC (GST_OBJECT_PARENT (pad));
176 if (!GST_PAD_IS_CONNECTED (jpegdec->srcpad)) {
177 gst_buffer_unref (buf);
181 data = (guchar *)GST_BUFFER_DATA(buf);
182 size = GST_BUFFER_SIZE(buf);
183 GST_DEBUG (0,"gst_jpegdec_chain: got buffer of %ld bytes in '%s'",size,
184 GST_OBJECT_NAME (jpegdec));
186 jpegdec->jsrc.next_input_byte = data;
187 jpegdec->jsrc.bytes_in_buffer = size;
190 GST_DEBUG (0,"gst_jpegdec_chain: reading header %08lx", *(gulong *)data);
191 jpeg_read_header(&jpegdec->cinfo, TRUE);
193 r_h = jpegdec->cinfo.cur_comp_info[0]->h_samp_factor;
194 r_v = jpegdec->cinfo.cur_comp_info[0]->v_samp_factor;
196 /*g_print ("%d %d\n", r_h, r_v);*/
197 /*g_print ("%d %d\n", jpegdec->cinfo.cur_comp_info[1]->h_samp_factor, jpegdec->cinfo.cur_comp_info[1]->v_samp_factor);*/
198 /*g_print ("%d %d\n", jpegdec->cinfo.cur_comp_info[2]->h_samp_factor, jpegdec->cinfo.cur_comp_info[2]->v_samp_factor);*/
200 jpegdec->cinfo.do_fancy_upsampling = FALSE;
201 jpegdec->cinfo.do_block_smoothing = FALSE;
202 jpegdec->cinfo.out_color_space = JCS_YCbCr;
203 jpegdec->cinfo.dct_method = JDCT_IFAST;
204 jpegdec->cinfo.raw_data_out = TRUE;
205 GST_DEBUG (0,"gst_jpegdec_chain: starting decompress");
206 jpeg_start_decompress(&jpegdec->cinfo);
207 width = jpegdec->cinfo.output_width;
208 height = jpegdec->cinfo.output_height;
209 GST_DEBUG (0,"gst_jpegdec_chain: width %d, height %d", width, height);
211 outbuf = gst_buffer_new();
212 outsize = GST_BUFFER_SIZE(outbuf) = width*height +
214 outdata = GST_BUFFER_DATA(outbuf) = g_malloc(outsize);
215 GST_BUFFER_TIMESTAMP(outbuf) = GST_BUFFER_TIMESTAMP(buf);
217 if (jpegdec->height != height) {
218 jpegdec->line[0] = g_realloc(jpegdec->line[0], height*sizeof(char*));
219 jpegdec->line[1] = g_realloc(jpegdec->line[1], height*sizeof(char*));
220 jpegdec->line[2] = g_realloc(jpegdec->line[2], height*sizeof(char*));
221 jpegdec->height = height;
223 gst_pad_try_set_caps (jpegdec->srcpad,
227 "format", GST_PROPS_FOURCC (GST_MAKE_FOURCC ('I','4','2','0')),
228 "width", GST_PROPS_INT (width),
229 "height", GST_PROPS_INT (height)
233 /* mind the swap, jpeglib outputs blue chroma first */
235 base[1] = base[0]+width*height;
236 base[2] = base[1]+width*height/4;
240 GST_DEBUG (0,"gst_jpegdec_chain: decompressing %u", jpegdec->cinfo.rec_outbuf_height);
241 for (i = 0; i < height; i += r_v*DCTSIZE) {
242 for (j=0, k=0; j< (r_v*DCTSIZE); j += r_v, k++) {
243 jpegdec->line[0][j] = base[0]; base[0] += width;
245 jpegdec->line[0][j+1] = base[0]; base[0] += width;
247 jpegdec->line[1][k] = base[1];
248 jpegdec->line[2][k] = base[2];
249 if (r_v == 2 || k&1) {
250 base[1] += width2; base[2] += width2;
253 /*g_print ("%d\n", jpegdec->cinfo.output_scanline);*/
254 jpeg_read_raw_data(&jpegdec->cinfo, jpegdec->line, r_v*DCTSIZE);
257 GST_DEBUG (0,"gst_jpegdec_chain: decompressing finished");
258 jpeg_finish_decompress(&jpegdec->cinfo);
260 GST_DEBUG (0,"gst_jpegdec_chain: sending buffer");
261 gst_pad_push(jpegdec->srcpad, outbuf);
263 gst_buffer_unref(buf);