7f43ea97b103ac080eb5932133c482e0d0214830
[platform/upstream/gst-plugins-good.git] / ext / jpeg / gstjpegenc.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  *
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.
8  *
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.
13  *
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.
18  */
19
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24 #include <string.h>
25
26 #include "gstjpegenc.h"
27 #include <gst/video/video.h>
28
29 /* elementfactory information */
30 GstElementDetails gst_jpegenc_details = {
31   "JPEG image encoder",
32   "Codec/Encoder/Image",
33   "Encode images in JPEG format",
34   "Wim Taymans <wim.taymans@tvd.be>",
35 };
36
37 /* JpegEnc signals and args */
38 enum {
39   FRAME_ENCODED,
40   /* FILL ME */
41   LAST_SIGNAL
42 };
43
44 enum {
45   ARG_0,
46   ARG_QUALITY,
47   ARG_SMOOTHING,
48   /* FILL ME */
49 };
50
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);
54
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);
58
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);
62
63 static GstElementClass *parent_class = NULL;
64 static guint gst_jpegenc_signals[LAST_SIGNAL] = { 0 };
65
66 GType
67 gst_jpegenc_get_type (void)
68 {
69   static GType jpegenc_type = 0;
70
71   if (!jpegenc_type) {
72     static const GTypeInfo jpegenc_info = {
73       sizeof(GstJpegEnc),
74       gst_jpegenc_base_init,
75       NULL,
76       (GClassInitFunc)gst_jpegenc_class_init,
77       NULL,
78       NULL,
79       sizeof(GstJpegEnc),
80       0,
81       (GInstanceInitFunc)gst_jpegenc_init,
82     };
83     jpegenc_type = g_type_register_static(GST_TYPE_ELEMENT, "GstJpegEnc", &jpegenc_info, 0);
84   }
85   return jpegenc_type;
86 }
87
88 static GstStaticPadTemplate gst_jpegenc_sink_pad_template =
89 GST_STATIC_PAD_TEMPLATE (
90     "sink",
91     GST_PAD_SINK,
92     GST_PAD_ALWAYS,
93     GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV("I420"))
94 );
95
96 static GstStaticPadTemplate gst_jpegenc_src_pad_template =
97 GST_STATIC_PAD_TEMPLATE (
98     "src",
99     GST_PAD_SRC,
100     GST_PAD_ALWAYS,
101     GST_STATIC_CAPS ("image/jpeg, "
102       "width = (int) [ 16, 4096 ], "
103       "height = (int) [ 16, 4096 ], "
104       "framerate = (double) [ 1, MAX ]"
105     )
106 );
107
108 static void
109 gst_jpegenc_base_init (gpointer g_class)
110 {
111   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
112   
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);
118 }
119
120 static void
121 gst_jpegenc_class_init (GstJpegEnc *klass)
122 {
123   GObjectClass *gobject_class;
124   GstElementClass *gstelement_class;
125
126   gobject_class = (GObjectClass*)klass;
127   gstelement_class = (GstElementClass*)klass;
128
129   parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
130
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);
135
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));
139 #if 0
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));
144 #endif
145
146   gobject_class->set_property = gst_jpegenc_set_property;
147   gobject_class->get_property = gst_jpegenc_get_property;
148 }
149
150 static void
151 gst_jpegenc_init_destination (j_compress_ptr cinfo)
152 {
153   GST_DEBUG ("gst_jpegenc_chain: init_destination");
154 }
155
156 static gboolean
157 gst_jpegenc_flush_destination (j_compress_ptr cinfo)
158 {
159   GST_DEBUG ("gst_jpegenc_chain: flush_destination: buffer too small !!!");
160   return TRUE;
161 }
162
163 static void gst_jpegenc_term_destination (j_compress_ptr cinfo)
164 {
165   GST_DEBUG ("gst_jpegenc_chain: term_source");
166 }
167
168 static void
169 gst_jpegenc_init (GstJpegEnc *jpegenc)
170 {
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);
178
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);
184
185   /* reset the initial video state */
186   jpegenc->width = -1;
187   jpegenc->height = -1;
188
189   /* setup jpeglib */
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);
194
195   GST_DEBUG ("gst_jpegenc_init: setting line buffers");
196   jpegenc->line[0] = NULL;
197   jpegenc->line[1] = NULL;
198   jpegenc->line[2] = NULL;
199
200   gst_jpegenc_resync(jpegenc);
201
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;
206
207   jpegenc->quality = 85;
208   jpegenc->smoothing = 0;
209 }
210
211 static GstCaps *
212 gst_jpegenc_getcaps (GstPad *pad)
213 {
214   GstJpegEnc *jpegenc = GST_JPEGENC (gst_pad_get_parent (pad));
215   GstPad *otherpad;
216   GstCaps *caps;
217   const char *name;
218   int i;
219   GstStructure *structure;
220
221   otherpad = (pad == jpegenc->srcpad) ? jpegenc->sinkpad : jpegenc->srcpad;
222   caps = gst_pad_get_allowed_caps (otherpad);
223   if (pad == jpegenc->srcpad) {
224     name = "image/jpeg";
225   } else {
226     name = "video/x-raw-yuv";
227   }
228   for (i=0;i<gst_caps_get_size (caps); i++){
229     structure = gst_caps_get_structure (caps, i);
230
231     gst_structure_set_name (structure, name);
232     gst_structure_remove_field (structure, "format");
233   }
234
235   return caps;
236 }
237
238 static GstPadLinkReturn
239 gst_jpegenc_link (GstPad *pad, const GstCaps *caps)
240 {
241   GstJpegEnc *jpegenc = GST_JPEGENC (gst_pad_get_parent (pad));
242   GstStructure *structure;
243   GstPadLinkReturn ret;
244   GstCaps *othercaps;
245   GstPad *otherpad;
246
247   otherpad = (pad == jpegenc->srcpad) ? jpegenc->sinkpad : jpegenc->srcpad;
248
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);
253   
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,
259       NULL);
260
261   ret = gst_pad_try_set_caps (jpegenc->srcpad, othercaps);
262   gst_caps_free(othercaps);
263
264   if (GST_PAD_LINK_SUCCESSFUL (ret)) {
265     gst_jpegenc_resync (jpegenc);
266   }
267
268   return ret;
269 }
270
271 static void
272 gst_jpegenc_resync (GstJpegEnc *jpegenc)
273 {
274   guint size = 0;
275   gint width, height;
276
277   GST_DEBUG ("gst_jpegenc_resync: resync");
278
279   jpegenc->cinfo.image_width = width = jpegenc->width;
280   jpegenc->cinfo.image_height = height = jpegenc->height;
281   jpegenc->cinfo.input_components = 3;
282
283   GST_DEBUG ("gst_jpegenc_resync: wdith %d, height %d", width, height);
284
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);
290
291 #if 0
292   switch (jpegenc->format) {
293     case GST_COLORSPACE_RGB24:
294       size = 3;
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;
298       break;
299     case GST_COLORSPACE_YUV420P:
300 #endif
301       size = 2;
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;
311
312       if (height != -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);
316       }
317
318       GST_DEBUG ("gst_jpegenc_resync: setting format done");
319 #if 0
320       break;
321     default:
322       printf("gst_jpegenc_resync: unsupported colorspace, using RGB\n");
323       size = 3;
324       jpegenc->cinfo.in_color_space = JCS_RGB;
325       break;
326   }
327 #endif
328   jpegenc->bufsize = jpegenc->width*jpegenc->height*size;
329
330   jpeg_suppress_tables(&jpegenc->cinfo, TRUE);
331   //jpeg_suppress_tables(&jpegenc->cinfo, FALSE);
332
333   jpegenc->buffer = NULL;
334   GST_DEBUG ("gst_jpegenc_resync: resync done");
335 }
336
337 static void
338 gst_jpegenc_chain (GstPad *pad, GstData *_data)
339 {
340   GstBuffer *buf = GST_BUFFER (_data);
341   GstJpegEnc *jpegenc;
342   guchar *data, *outdata;
343   gulong size, outsize;
344   GstBuffer *outbuf;
345 /*  GstMeta *meta; */
346   guint height, width, width2;
347   guchar *base[3];
348   gint i,j, k;
349
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)); */
354
355   /*usleep(10000); */
356   jpegenc = GST_JPEGENC (GST_OBJECT_PARENT (pad));
357
358   data = GST_BUFFER_DATA(buf);
359   size = GST_BUFFER_SIZE(buf);
360
361   GST_DEBUG ("gst_jpegenc_chain: got buffer of %ld bytes in '%s'",size,
362           GST_OBJECT_NAME (jpegenc));
363
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);
368
369   width = jpegenc->width;
370   height = jpegenc->height;
371
372   base[0] = data;
373   base[1] = base[0]+width*height;
374   base[2] = base[1]+width*height/4;
375
376   jpegenc->jdest.next_output_byte = outdata;
377   jpegenc->jdest.free_in_buffer = outsize;
378
379   jpegenc->cinfo.smoothing_factor = jpegenc->smoothing;
380   jpeg_set_quality(&jpegenc->cinfo, jpegenc->quality, TRUE);
381   jpeg_start_compress(&jpegenc->cinfo, TRUE);
382
383   width2 = width>>1;
384   GST_DEBUG ("gst_jpegdec_chain: compressing");
385
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;
392     }
393     jpeg_write_raw_data(&jpegenc->cinfo, jpegenc->line, 2*DCTSIZE);
394   }
395   jpeg_finish_compress(&jpegenc->cinfo);
396   GST_DEBUG ("gst_jpegdec_chain: compressing done");
397
398   GST_BUFFER_SIZE(outbuf) = (((outsize - jpegenc->jdest.free_in_buffer)+3)&~3);
399
400   gst_pad_push(jpegenc->srcpad, GST_DATA (outbuf));
401
402   g_signal_emit(G_OBJECT(jpegenc),gst_jpegenc_signals[FRAME_ENCODED], 0);
403
404   gst_buffer_unref(buf);
405 }
406
407 static void
408 gst_jpegenc_set_property (GObject * object, guint prop_id,
409     const GValue * value, GParamSpec * pspec)
410 {
411   GstJpegEnc *jpegenc;
412
413   g_return_if_fail (GST_IS_JPEGENC (object));
414   jpegenc = GST_JPEGENC (object);
415   
416   switch (prop_id) {
417     case ARG_QUALITY:
418       jpegenc->quality = g_value_get_int (value);
419       break;
420     case ARG_SMOOTHING:
421       jpegenc->smoothing = g_value_get_int (value);
422       break;
423     default:
424       break;
425   }
426
427
428 static void
429 gst_jpegenc_get_property (GObject * object, guint prop_id, GValue * value,
430     GParamSpec * pspec)
431 {
432   GstJpegEnc *jpegenc;
433
434   g_return_if_fail (GST_IS_JPEGENC (object));
435   jpegenc = GST_JPEGENC (object);
436
437   switch (prop_id) {
438     case ARG_QUALITY:
439       g_value_set_int (value, jpegenc->quality);
440       break;
441     case ARG_SMOOTHING:
442       g_value_set_int (value, jpegenc->smoothing);
443       break;
444     default:
445       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
446       break;
447   }
448 }
449