gst-indent
[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 {
40   FRAME_ENCODED,
41   /* FILL ME */
42   LAST_SIGNAL
43 };
44
45 enum
46 {
47   ARG_0,
48   ARG_QUALITY,
49   ARG_SMOOTHING,
50   /* FILL ME */
51 };
52
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);
56
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);
60
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);
66
67 static GstElementClass *parent_class = NULL;
68 static guint gst_jpegenc_signals[LAST_SIGNAL] = { 0 };
69
70 GType
71 gst_jpegenc_get_type (void)
72 {
73   static GType jpegenc_type = 0;
74
75   if (!jpegenc_type) {
76     static const GTypeInfo jpegenc_info = {
77       sizeof (GstJpegEnc),
78       gst_jpegenc_base_init,
79       NULL,
80       (GClassInitFunc) gst_jpegenc_class_init,
81       NULL,
82       NULL,
83       sizeof (GstJpegEnc),
84       0,
85       (GInstanceInitFunc) gst_jpegenc_init,
86     };
87     jpegenc_type =
88         g_type_register_static (GST_TYPE_ELEMENT, "GstJpegEnc", &jpegenc_info,
89         0);
90   }
91   return jpegenc_type;
92 }
93
94 static GstStaticPadTemplate gst_jpegenc_sink_pad_template =
95 GST_STATIC_PAD_TEMPLATE ("sink",
96     GST_PAD_SINK,
97     GST_PAD_ALWAYS,
98     GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("I420"))
99     );
100
101 static GstStaticPadTemplate gst_jpegenc_src_pad_template =
102 GST_STATIC_PAD_TEMPLATE ("src",
103     GST_PAD_SRC,
104     GST_PAD_ALWAYS,
105     GST_STATIC_CAPS ("image/jpeg, "
106         "width = (int) [ 16, 4096 ], "
107         "height = (int) [ 16, 4096 ], " "framerate = (double) [ 1, MAX ]")
108     );
109
110 static void
111 gst_jpegenc_base_init (gpointer g_class)
112 {
113   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
114
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);
120 }
121
122 static void
123 gst_jpegenc_class_init (GstJpegEnc * klass)
124 {
125   GObjectClass *gobject_class;
126   GstElementClass *gstelement_class;
127
128   gobject_class = (GObjectClass *) klass;
129   gstelement_class = (GstElementClass *) klass;
130
131   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
132
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);
137
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));
141 #if 0
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));
146 #endif
147
148   gobject_class->set_property = gst_jpegenc_set_property;
149   gobject_class->get_property = gst_jpegenc_get_property;
150 }
151
152 static void
153 gst_jpegenc_init_destination (j_compress_ptr cinfo)
154 {
155   GST_DEBUG ("gst_jpegenc_chain: init_destination");
156 }
157
158 static gboolean
159 gst_jpegenc_flush_destination (j_compress_ptr cinfo)
160 {
161   GST_DEBUG ("gst_jpegenc_chain: flush_destination: buffer too small !!!");
162   return TRUE;
163 }
164
165 static void
166 gst_jpegenc_term_destination (j_compress_ptr cinfo)
167 {
168   GST_DEBUG ("gst_jpegenc_chain: term_source");
169 }
170
171 static void
172 gst_jpegenc_init (GstJpegEnc * jpegenc)
173 {
174   /* create the sink and src pads */
175   jpegenc->sinkpad =
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);
182
183   jpegenc->srcpad =
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);
189
190   /* reset the initial video state */
191   jpegenc->width = -1;
192   jpegenc->height = -1;
193
194   /* setup jpeglib */
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);
199
200   GST_DEBUG ("gst_jpegenc_init: setting line buffers");
201   jpegenc->line[0] = NULL;
202   jpegenc->line[1] = NULL;
203   jpegenc->line[2] = NULL;
204
205   gst_jpegenc_resync (jpegenc);
206
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;
211
212   jpegenc->quality = 85;
213   jpegenc->smoothing = 0;
214 }
215
216 static GstCaps *
217 gst_jpegenc_getcaps (GstPad * pad)
218 {
219   GstJpegEnc *jpegenc = GST_JPEGENC (gst_pad_get_parent (pad));
220   GstPad *otherpad;
221   GstCaps *caps;
222   const char *name;
223   int i;
224   GstStructure *structure;
225
226   otherpad = (pad == jpegenc->srcpad) ? jpegenc->sinkpad : jpegenc->srcpad;
227   caps = gst_pad_get_allowed_caps (otherpad);
228   if (pad == jpegenc->srcpad) {
229     name = "image/jpeg";
230   } else {
231     name = "video/x-raw-yuv";
232   }
233   for (i = 0; i < gst_caps_get_size (caps); i++) {
234     structure = gst_caps_get_structure (caps, i);
235
236     gst_structure_set_name (structure, name);
237     gst_structure_remove_field (structure, "format");
238   }
239
240   return caps;
241 }
242
243 static GstPadLinkReturn
244 gst_jpegenc_link (GstPad * pad, const GstCaps * caps)
245 {
246   GstJpegEnc *jpegenc = GST_JPEGENC (gst_pad_get_parent (pad));
247   GstStructure *structure;
248   GstPadLinkReturn ret;
249   GstCaps *othercaps;
250   GstPad *otherpad;
251
252   otherpad = (pad == jpegenc->srcpad) ? jpegenc->sinkpad : jpegenc->srcpad;
253
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);
258
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);
264
265   ret = gst_pad_try_set_caps (jpegenc->srcpad, othercaps);
266   gst_caps_free (othercaps);
267
268   if (GST_PAD_LINK_SUCCESSFUL (ret)) {
269     gst_jpegenc_resync (jpegenc);
270   }
271
272   return ret;
273 }
274
275 static void
276 gst_jpegenc_resync (GstJpegEnc * jpegenc)
277 {
278   guint size = 0;
279   gint width, height;
280
281   GST_DEBUG ("gst_jpegenc_resync: resync");
282
283   jpegenc->cinfo.image_width = width = jpegenc->width;
284   jpegenc->cinfo.image_height = height = jpegenc->height;
285   jpegenc->cinfo.input_components = 3;
286
287   GST_DEBUG ("gst_jpegenc_resync: wdith %d, height %d", width, height);
288
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);
294
295 #if 0
296   switch (jpegenc->format) {
297     case GST_COLORSPACE_RGB24:
298       size = 3;
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;
302       break;
303     case GST_COLORSPACE_YUV420P:
304 #endif
305       size = 2;
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;
315
316       if (height != -1) {
317         jpegenc->line[0] =
318             g_realloc (jpegenc->line[0], height * sizeof (char *));
319         jpegenc->line[1] =
320             g_realloc (jpegenc->line[1], height * sizeof (char *) / 2);
321         jpegenc->line[2] =
322             g_realloc (jpegenc->line[2], height * sizeof (char *) / 2);
323       }
324
325       GST_DEBUG ("gst_jpegenc_resync: setting format done");
326 #if 0
327       break;
328     default:
329       printf ("gst_jpegenc_resync: unsupported colorspace, using RGB\n");
330       size = 3;
331       jpegenc->cinfo.in_color_space = JCS_RGB;
332       break;
333   }
334 #endif
335   jpegenc->bufsize = jpegenc->width * jpegenc->height * size;
336
337   jpeg_suppress_tables (&jpegenc->cinfo, TRUE);
338   //jpeg_suppress_tables(&jpegenc->cinfo, FALSE);
339
340   jpegenc->buffer = NULL;
341   GST_DEBUG ("gst_jpegenc_resync: resync done");
342 }
343
344 static void
345 gst_jpegenc_chain (GstPad * pad, GstData * _data)
346 {
347   GstBuffer *buf = GST_BUFFER (_data);
348   GstJpegEnc *jpegenc;
349   guchar *data, *outdata;
350   gulong size, outsize;
351   GstBuffer *outbuf;
352
353 /*  GstMeta *meta; */
354   guint height, width, width2;
355   guchar *base[3];
356   gint i, j, k;
357
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)); */
362
363   /*usleep(10000); */
364   jpegenc = GST_JPEGENC (GST_OBJECT_PARENT (pad));
365
366   data = GST_BUFFER_DATA (buf);
367   size = GST_BUFFER_SIZE (buf);
368
369   GST_DEBUG ("gst_jpegenc_chain: got buffer of %ld bytes in '%s'", size,
370       GST_OBJECT_NAME (jpegenc));
371
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);
376
377   width = jpegenc->width;
378   height = jpegenc->height;
379
380   base[0] = data;
381   base[1] = base[0] + width * height;
382   base[2] = base[1] + width * height / 4;
383
384   jpegenc->jdest.next_output_byte = outdata;
385   jpegenc->jdest.free_in_buffer = outsize;
386
387   jpegenc->cinfo.smoothing_factor = jpegenc->smoothing;
388   jpeg_set_quality (&jpegenc->cinfo, jpegenc->quality, TRUE);
389   jpeg_start_compress (&jpegenc->cinfo, TRUE);
390
391   width2 = width >> 1;
392   GST_DEBUG ("gst_jpegdec_chain: compressing");
393
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];
397       base[0] += width;
398       jpegenc->line[0][j + 1] = base[0];
399       base[0] += width;
400       jpegenc->line[1][k] = base[1];
401       base[1] += width2;
402       jpegenc->line[2][k] = base[2];
403       base[2] += width2;
404     }
405     jpeg_write_raw_data (&jpegenc->cinfo, jpegenc->line, 2 * DCTSIZE);
406   }
407   jpeg_finish_compress (&jpegenc->cinfo);
408   GST_DEBUG ("gst_jpegdec_chain: compressing done");
409
410   GST_BUFFER_SIZE (outbuf) =
411       (((outsize - jpegenc->jdest.free_in_buffer) + 3) & ~3);
412
413   gst_pad_push (jpegenc->srcpad, GST_DATA (outbuf));
414
415   g_signal_emit (G_OBJECT (jpegenc), gst_jpegenc_signals[FRAME_ENCODED], 0);
416
417   gst_buffer_unref (buf);
418 }
419
420 static void
421 gst_jpegenc_set_property (GObject * object, guint prop_id,
422     const GValue * value, GParamSpec * pspec)
423 {
424   GstJpegEnc *jpegenc;
425
426   g_return_if_fail (GST_IS_JPEGENC (object));
427   jpegenc = GST_JPEGENC (object);
428
429   switch (prop_id) {
430     case ARG_QUALITY:
431       jpegenc->quality = g_value_get_int (value);
432       break;
433     case ARG_SMOOTHING:
434       jpegenc->smoothing = g_value_get_int (value);
435       break;
436     default:
437       break;
438   }
439 }
440
441 static void
442 gst_jpegenc_get_property (GObject * object, guint prop_id, GValue * value,
443     GParamSpec * pspec)
444 {
445   GstJpegEnc *jpegenc;
446
447   g_return_if_fail (GST_IS_JPEGENC (object));
448   jpegenc = GST_JPEGENC (object);
449
450   switch (prop_id) {
451     case ARG_QUALITY:
452       g_value_set_int (value, jpegenc->quality);
453       break;
454     case ARG_SMOOTHING:
455       g_value_set_int (value, jpegenc->smoothing);
456       break;
457     default:
458       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
459       break;
460   }
461 }