Tizen 2.0 Release
[framework/multimedia/gst-plugins-good0.10.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  * SECTION:element-jpegenc
21  *
22  * Encodes jpeg images.
23  *
24  * <refsect2>
25  * <title>Example launch line</title>
26  * |[
27  * gst-launch videotestsrc num-buffers=50 ! video/x-raw-yuv, framerate='(fraction)'5/1 ! jpegenc ! avimux ! filesink location=mjpeg.avi
28  * ]| a pipeline to mux 5 JPEG frames per second into a 10 sec. long motion jpeg
29  * avi.
30  * </refsect2>
31  */
32
33 #ifdef HAVE_CONFIG_H
34 #include "config.h"
35 #endif
36 #include <string.h>
37
38 #include "gstjpegenc.h"
39 #include "gstjpeg.h"
40 #include <gst/video/video.h>
41
42 /* experimental */
43 /* setting smoothig seems to have no effect in libjepeg
44 #define ENABLE_SMOOTHING 1
45 */
46
47 GST_DEBUG_CATEGORY_STATIC (jpegenc_debug);
48 #define GST_CAT_DEFAULT jpegenc_debug
49
50 #define JPEG_DEFAULT_QUALITY 85
51 #define JPEG_DEFAULT_SMOOTHING 0
52 #define JPEG_DEFAULT_IDCT_METHOD        JDCT_FASTEST
53
54 /* JpegEnc signals and args */
55 enum
56 {
57   FRAME_ENCODED,
58   /* FILL ME */
59   LAST_SIGNAL
60 };
61
62 enum
63 {
64   PROP_0,
65   PROP_QUALITY,
66   PROP_SMOOTHING,
67   PROP_IDCT_METHOD
68 };
69
70 static void gst_jpegenc_reset (GstJpegEnc * enc);
71 static void gst_jpegenc_base_init (gpointer g_class);
72 static void gst_jpegenc_class_init (GstJpegEnc * klass);
73 static void gst_jpegenc_init (GstJpegEnc * jpegenc);
74 static void gst_jpegenc_finalize (GObject * object);
75
76 static GstFlowReturn gst_jpegenc_chain (GstPad * pad, GstBuffer * buf);
77 static gboolean gst_jpegenc_setcaps (GstPad * pad, GstCaps * caps);
78 static GstCaps *gst_jpegenc_getcaps (GstPad * pad);
79
80 static void gst_jpegenc_resync (GstJpegEnc * jpegenc);
81 static void gst_jpegenc_set_property (GObject * object, guint prop_id,
82     const GValue * value, GParamSpec * pspec);
83 static void gst_jpegenc_get_property (GObject * object, guint prop_id,
84     GValue * value, GParamSpec * pspec);
85 static GstStateChangeReturn gst_jpegenc_change_state (GstElement * element,
86     GstStateChange transition);
87
88
89 static GstElementClass *parent_class = NULL;
90 static guint gst_jpegenc_signals[LAST_SIGNAL] = { 0 };
91
92 GType
93 gst_jpegenc_get_type (void)
94 {
95   static GType jpegenc_type = 0;
96
97   if (!jpegenc_type) {
98     static const GTypeInfo jpegenc_info = {
99       sizeof (GstJpegEnc),
100       (GBaseInitFunc) gst_jpegenc_base_init,
101       NULL,
102       (GClassInitFunc) gst_jpegenc_class_init,
103       NULL,
104       NULL,
105       sizeof (GstJpegEnc),
106       0,
107       (GInstanceInitFunc) gst_jpegenc_init,
108     };
109
110     jpegenc_type =
111         g_type_register_static (GST_TYPE_ELEMENT, "GstJpegEnc", &jpegenc_info,
112         0);
113   }
114   return jpegenc_type;
115 }
116
117 /* *INDENT-OFF* */
118 static GstStaticPadTemplate gst_jpegenc_sink_pad_template =
119 GST_STATIC_PAD_TEMPLATE ("sink",
120     GST_PAD_SINK,
121     GST_PAD_ALWAYS,
122     GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV
123         ("{ I420, YV12, YUY2, UYVY, Y41B, Y42B, YVYU, Y444 }") "; "
124         GST_VIDEO_CAPS_RGB "; " GST_VIDEO_CAPS_BGR "; "
125         GST_VIDEO_CAPS_RGBx "; " GST_VIDEO_CAPS_xRGB "; "
126         GST_VIDEO_CAPS_BGRx "; " GST_VIDEO_CAPS_xBGR "; "
127         GST_VIDEO_CAPS_GRAY8)
128     );
129 /* *INDENT-ON* */
130
131 static GstStaticPadTemplate gst_jpegenc_src_pad_template =
132 GST_STATIC_PAD_TEMPLATE ("src",
133     GST_PAD_SRC,
134     GST_PAD_ALWAYS,
135     GST_STATIC_CAPS ("image/jpeg, "
136         "width = (int) [ 16, 65535 ], "
137         "height = (int) [ 16, 65535 ], " "framerate = (fraction) [ 0/1, MAX ]")
138     );
139
140 static void
141 gst_jpegenc_base_init (gpointer g_class)
142 {
143   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
144
145   gst_element_class_add_static_pad_template (element_class,
146       &gst_jpegenc_sink_pad_template);
147   gst_element_class_add_static_pad_template (element_class,
148       &gst_jpegenc_src_pad_template);
149   gst_element_class_set_details_simple (element_class, "JPEG image encoder",
150       "Codec/Encoder/Image",
151       "Encode images in JPEG format", "Wim Taymans <wim.taymans@tvd.be>");
152 }
153
154 static void
155 gst_jpegenc_class_init (GstJpegEnc * klass)
156 {
157   GObjectClass *gobject_class;
158   GstElementClass *gstelement_class;
159
160   gobject_class = (GObjectClass *) klass;
161   gstelement_class = (GstElementClass *) klass;
162
163   parent_class = g_type_class_peek_parent (klass);
164
165   gst_jpegenc_signals[FRAME_ENCODED] =
166       g_signal_new ("frame-encoded", G_TYPE_FROM_CLASS (klass),
167       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstJpegEncClass, frame_encoded), NULL,
168       NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
169
170   gobject_class->set_property = gst_jpegenc_set_property;
171   gobject_class->get_property = gst_jpegenc_get_property;
172
173
174   g_object_class_install_property (gobject_class, PROP_QUALITY,
175       g_param_spec_int ("quality", "Quality", "Quality of encoding",
176           0, 100, JPEG_DEFAULT_QUALITY,
177           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
178
179 #ifdef ENABLE_SMOOTHING
180   /* disabled, since it doesn't seem to work */
181   g_object_class_install_property (gobject_class, PROP_SMOOTHING,
182       g_param_spec_int ("smoothing", "Smoothing", "Smoothing factor",
183           0, 100, JPEG_DEFAULT_SMOOTHING,
184           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
185 #endif
186
187   g_object_class_install_property (gobject_class, PROP_IDCT_METHOD,
188       g_param_spec_enum ("idct-method", "IDCT Method",
189           "The IDCT algorithm to use", GST_TYPE_IDCT_METHOD,
190           JPEG_DEFAULT_IDCT_METHOD,
191           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
192
193   gstelement_class->change_state = gst_jpegenc_change_state;
194
195   gobject_class->finalize = gst_jpegenc_finalize;
196
197   GST_DEBUG_CATEGORY_INIT (jpegenc_debug, "jpegenc", 0,
198       "JPEG encoding element");
199 }
200
201 static void
202 gst_jpegenc_init_destination (j_compress_ptr cinfo)
203 {
204   GST_DEBUG ("gst_jpegenc_chain: init_destination");
205 }
206
207 static boolean
208 gst_jpegenc_flush_destination (j_compress_ptr cinfo)
209 {
210   GstBuffer *overflow_buffer;
211   guint32 old_buffer_size;
212   GstJpegEnc *jpegenc = (GstJpegEnc *) (cinfo->client_data);
213   GstFlowReturn ret;
214
215   GST_DEBUG_OBJECT (jpegenc,
216       "gst_jpegenc_chain: flush_destination: buffer too small");
217
218   /* Our output buffer wasn't big enough.
219    * Make a new buffer that's twice the size, */
220   old_buffer_size = GST_BUFFER_SIZE (jpegenc->output_buffer);
221   ret = gst_pad_alloc_buffer_and_set_caps (jpegenc->srcpad,
222       GST_BUFFER_OFFSET_NONE, old_buffer_size * 2,
223       GST_PAD_CAPS (jpegenc->srcpad), &overflow_buffer);
224   /* handle here if needed */
225   if (ret != GST_FLOW_OK) {
226     overflow_buffer = gst_buffer_new_and_alloc (old_buffer_size * 2);
227     gst_buffer_set_caps (overflow_buffer, GST_PAD_CAPS (jpegenc->srcpad));
228   }
229
230   memcpy (GST_BUFFER_DATA (overflow_buffer),
231       GST_BUFFER_DATA (jpegenc->output_buffer), old_buffer_size);
232
233   gst_buffer_copy_metadata (overflow_buffer, jpegenc->output_buffer,
234       GST_BUFFER_COPY_TIMESTAMPS);
235
236   /* drop it into place, */
237   gst_buffer_unref (jpegenc->output_buffer);
238   jpegenc->output_buffer = overflow_buffer;
239
240   /* and last, update libjpeg on where to work. */
241   jpegenc->jdest.next_output_byte =
242       GST_BUFFER_DATA (jpegenc->output_buffer) + old_buffer_size;
243   jpegenc->jdest.free_in_buffer =
244       GST_BUFFER_SIZE (jpegenc->output_buffer) - old_buffer_size;
245
246   return TRUE;
247 }
248
249 static void
250 gst_jpegenc_term_destination (j_compress_ptr cinfo)
251 {
252   GstJpegEnc *jpegenc = (GstJpegEnc *) (cinfo->client_data);
253   GST_DEBUG_OBJECT (jpegenc, "gst_jpegenc_chain: term_source");
254
255   /* Trim the buffer size and push it. */
256   GST_BUFFER_SIZE (jpegenc->output_buffer) =
257       GST_BUFFER_SIZE (jpegenc->output_buffer) - jpegenc->jdest.free_in_buffer;
258
259   g_signal_emit (G_OBJECT (jpegenc), gst_jpegenc_signals[FRAME_ENCODED], 0);
260
261   jpegenc->last_ret = gst_pad_push (jpegenc->srcpad, jpegenc->output_buffer);
262   jpegenc->output_buffer = NULL;
263 }
264
265 static void
266 gst_jpegenc_init (GstJpegEnc * jpegenc)
267 {
268   /* create the sink and src pads */
269   jpegenc->sinkpad =
270       gst_pad_new_from_static_template (&gst_jpegenc_sink_pad_template, "sink");
271   gst_pad_set_chain_function (jpegenc->sinkpad,
272       GST_DEBUG_FUNCPTR (gst_jpegenc_chain));
273   gst_pad_set_getcaps_function (jpegenc->sinkpad,
274       GST_DEBUG_FUNCPTR (gst_jpegenc_getcaps));
275   gst_pad_set_setcaps_function (jpegenc->sinkpad,
276       GST_DEBUG_FUNCPTR (gst_jpegenc_setcaps));
277   gst_element_add_pad (GST_ELEMENT (jpegenc), jpegenc->sinkpad);
278
279   jpegenc->srcpad =
280       gst_pad_new_from_static_template (&gst_jpegenc_src_pad_template, "src");
281   gst_pad_use_fixed_caps (jpegenc->srcpad);
282   gst_element_add_pad (GST_ELEMENT (jpegenc), jpegenc->srcpad);
283
284   /* reset the initial video state */
285   jpegenc->width = -1;
286   jpegenc->height = -1;
287
288   /* setup jpeglib */
289   memset (&jpegenc->cinfo, 0, sizeof (jpegenc->cinfo));
290   memset (&jpegenc->jerr, 0, sizeof (jpegenc->jerr));
291   jpegenc->cinfo.err = jpeg_std_error (&jpegenc->jerr);
292   jpeg_create_compress (&jpegenc->cinfo);
293
294   jpegenc->jdest.init_destination = gst_jpegenc_init_destination;
295   jpegenc->jdest.empty_output_buffer = gst_jpegenc_flush_destination;
296   jpegenc->jdest.term_destination = gst_jpegenc_term_destination;
297   jpegenc->cinfo.dest = &jpegenc->jdest;
298   jpegenc->cinfo.client_data = jpegenc;
299
300   /* init properties */
301   jpegenc->quality = JPEG_DEFAULT_QUALITY;
302   jpegenc->smoothing = JPEG_DEFAULT_SMOOTHING;
303   jpegenc->idct_method = JPEG_DEFAULT_IDCT_METHOD;
304
305   gst_jpegenc_reset (jpegenc);
306 }
307
308 static void
309 gst_jpegenc_reset (GstJpegEnc * enc)
310 {
311   gint i, j;
312
313   g_free (enc->line[0]);
314   g_free (enc->line[1]);
315   g_free (enc->line[2]);
316   enc->line[0] = NULL;
317   enc->line[1] = NULL;
318   enc->line[2] = NULL;
319   for (i = 0; i < 3; i++) {
320     for (j = 0; j < 4 * DCTSIZE; j++) {
321       g_free (enc->row[i][j]);
322       enc->row[i][j] = NULL;
323     }
324   }
325
326   enc->width = -1;
327   enc->height = -1;
328   enc->format = GST_VIDEO_FORMAT_UNKNOWN;
329   enc->fps_den = enc->par_den = 0;
330   enc->height = enc->width = 0;
331 }
332
333 static void
334 gst_jpegenc_finalize (GObject * object)
335 {
336   GstJpegEnc *filter = GST_JPEGENC (object);
337
338   jpeg_destroy_compress (&filter->cinfo);
339
340   G_OBJECT_CLASS (parent_class)->finalize (object);
341 }
342
343 static GstCaps *
344 gst_jpegenc_getcaps (GstPad * pad)
345 {
346   GstJpegEnc *jpegenc = GST_JPEGENC (gst_pad_get_parent (pad));
347   GstCaps *caps, *othercaps;
348   const GstCaps *templ;
349   gint i, j;
350   GstStructure *structure = NULL;
351
352   /* we want to proxy properties like width, height and framerate from the
353      other end of the element */
354
355   othercaps = gst_pad_peer_get_caps_reffed (jpegenc->srcpad);
356   if (othercaps == NULL ||
357       gst_caps_is_empty (othercaps) || gst_caps_is_any (othercaps)) {
358     caps = gst_caps_copy (gst_pad_get_pad_template_caps (pad));
359     goto done;
360   }
361
362   caps = gst_caps_new_empty ();
363   templ = gst_pad_get_pad_template_caps (pad);
364
365   for (i = 0; i < gst_caps_get_size (templ); i++) {
366     /* pick fields from peer caps */
367     for (j = 0; j < gst_caps_get_size (othercaps); j++) {
368       GstStructure *s = gst_caps_get_structure (othercaps, j);
369       const GValue *val;
370
371       structure = gst_structure_copy (gst_caps_get_structure (templ, i));
372       if ((val = gst_structure_get_value (s, "width")))
373         gst_structure_set_value (structure, "width", val);
374       if ((val = gst_structure_get_value (s, "height")))
375         gst_structure_set_value (structure, "height", val);
376       if ((val = gst_structure_get_value (s, "framerate")))
377         gst_structure_set_value (structure, "framerate", val);
378
379       gst_caps_merge_structure (caps, structure);
380     }
381   }
382
383 done:
384
385   gst_caps_replace (&othercaps, NULL);
386   gst_object_unref (jpegenc);
387
388   return caps;
389 }
390
391 static gboolean
392 gst_jpegenc_setcaps (GstPad * pad, GstCaps * caps)
393 {
394   GstJpegEnc *enc = GST_JPEGENC (gst_pad_get_parent (pad));
395   GstVideoFormat format;
396   gint width, height;
397   gint fps_num, fps_den;
398   gint par_num, par_den;
399   gint i;
400   GstCaps *othercaps;
401   gboolean ret;
402
403   /* get info from caps */
404   if (!gst_video_format_parse_caps (caps, &format, &width, &height))
405     goto refuse_caps;
406   /* optional; pass along if present */
407   fps_num = fps_den = -1;
408   par_num = par_den = -1;
409   gst_video_parse_caps_framerate (caps, &fps_num, &fps_den);
410   gst_video_parse_caps_pixel_aspect_ratio (caps, &par_num, &par_den);
411
412   if (width == enc->width && height == enc->height && enc->format == format
413       && fps_num == enc->fps_num && fps_den == enc->fps_den
414       && par_num == enc->par_num && par_den == enc->par_den)
415     return TRUE;
416
417   /* store input description */
418   enc->format = format;
419   enc->width = width;
420   enc->height = height;
421   enc->fps_num = fps_num;
422   enc->fps_den = fps_den;
423   enc->par_num = par_num;
424   enc->par_den = par_den;
425
426   /* prepare a cached image description  */
427   enc->channels = 3 + (gst_video_format_has_alpha (format) ? 1 : 0);
428   /* ... but any alpha is disregarded in encoding */
429   if (gst_video_format_is_gray (format))
430     enc->channels = 1;
431   else
432     enc->channels = 3;
433   enc->h_max_samp = 0;
434   enc->v_max_samp = 0;
435   for (i = 0; i < enc->channels; ++i) {
436     enc->cwidth[i] = gst_video_format_get_component_width (format, i, width);
437     enc->cheight[i] = gst_video_format_get_component_height (format, i, height);
438     enc->offset[i] = gst_video_format_get_component_offset (format, i, width,
439         height);
440     enc->stride[i] = gst_video_format_get_row_stride (format, i, width);
441     enc->inc[i] = gst_video_format_get_pixel_stride (format, i);
442     enc->h_samp[i] = GST_ROUND_UP_4 (width) / enc->cwidth[i];
443     enc->h_max_samp = MAX (enc->h_max_samp, enc->h_samp[i]);
444     enc->v_samp[i] = GST_ROUND_UP_4 (height) / enc->cheight[i];
445     enc->v_max_samp = MAX (enc->v_max_samp, enc->v_samp[i]);
446   }
447   /* samp should only be 1, 2 or 4 */
448   g_assert (enc->h_max_samp <= 4);
449   g_assert (enc->v_max_samp <= 4);
450   /* now invert */
451   /* maximum is invariant, as one of the components should have samp 1 */
452   for (i = 0; i < enc->channels; ++i) {
453     enc->h_samp[i] = enc->h_max_samp / enc->h_samp[i];
454     enc->v_samp[i] = enc->v_max_samp / enc->v_samp[i];
455   }
456   enc->planar = (enc->inc[0] == 1 && enc->inc[1] == 1 && enc->inc[2] == 1);
457
458   othercaps = gst_caps_copy (gst_pad_get_pad_template_caps (enc->srcpad));
459   gst_caps_set_simple (othercaps,
460       "width", G_TYPE_INT, enc->width, "height", G_TYPE_INT, enc->height, NULL);
461   if (enc->fps_den > 0)
462     gst_caps_set_simple (othercaps,
463         "framerate", GST_TYPE_FRACTION, enc->fps_num, enc->fps_den, NULL);
464   if (enc->par_den > 0)
465     gst_caps_set_simple (othercaps,
466         "pixel-aspect-ratio", GST_TYPE_FRACTION, enc->par_num, enc->par_den,
467         NULL);
468
469   ret = gst_pad_set_caps (enc->srcpad, othercaps);
470   gst_caps_unref (othercaps);
471
472   if (ret)
473     gst_jpegenc_resync (enc);
474
475   gst_object_unref (enc);
476
477   return ret;
478
479   /* ERRORS */
480 refuse_caps:
481   {
482     GST_WARNING_OBJECT (enc, "refused caps %" GST_PTR_FORMAT, caps);
483     gst_object_unref (enc);
484     return FALSE;
485   }
486 }
487
488 static void
489 gst_jpegenc_resync (GstJpegEnc * jpegenc)
490 {
491   gint width, height;
492   gint i, j;
493
494   GST_DEBUG_OBJECT (jpegenc, "resync");
495
496   jpegenc->cinfo.image_width = width = jpegenc->width;
497   jpegenc->cinfo.image_height = height = jpegenc->height;
498   jpegenc->cinfo.input_components = jpegenc->channels;
499
500   GST_DEBUG_OBJECT (jpegenc, "width %d, height %d", width, height);
501   GST_DEBUG_OBJECT (jpegenc, "format %d", jpegenc->format);
502
503   if (gst_video_format_is_rgb (jpegenc->format)) {
504     GST_DEBUG_OBJECT (jpegenc, "RGB");
505     jpegenc->cinfo.in_color_space = JCS_RGB;
506   } else if (gst_video_format_is_gray (jpegenc->format)) {
507     GST_DEBUG_OBJECT (jpegenc, "gray");
508     jpegenc->cinfo.in_color_space = JCS_GRAYSCALE;
509   } else {
510     GST_DEBUG_OBJECT (jpegenc, "YUV");
511     jpegenc->cinfo.in_color_space = JCS_YCbCr;
512   }
513
514   /* input buffer size as max output */
515   jpegenc->bufsize = gst_video_format_get_size (jpegenc->format, width, height);
516   jpeg_set_defaults (&jpegenc->cinfo);
517   jpegenc->cinfo.raw_data_in = TRUE;
518   /* duh, libjpeg maps RGB to YUV ... and don't expect some conversion */
519   if (jpegenc->cinfo.in_color_space == JCS_RGB)
520     jpeg_set_colorspace (&jpegenc->cinfo, JCS_RGB);
521
522   GST_DEBUG_OBJECT (jpegenc, "h_max_samp=%d, v_max_samp=%d",
523       jpegenc->h_max_samp, jpegenc->v_max_samp);
524   /* image dimension info */
525   for (i = 0; i < jpegenc->channels; i++) {
526     GST_DEBUG_OBJECT (jpegenc, "comp %i: h_samp=%d, v_samp=%d", i,
527         jpegenc->h_samp[i], jpegenc->v_samp[i]);
528     jpegenc->cinfo.comp_info[i].h_samp_factor = jpegenc->h_samp[i];
529     jpegenc->cinfo.comp_info[i].v_samp_factor = jpegenc->v_samp[i];
530     g_free (jpegenc->line[i]);
531     jpegenc->line[i] = g_new (guchar *, jpegenc->v_max_samp * DCTSIZE);
532     if (!jpegenc->planar) {
533       for (j = 0; j < jpegenc->v_max_samp * DCTSIZE; j++) {
534         g_free (jpegenc->row[i][j]);
535         jpegenc->row[i][j] = g_malloc (width);
536         jpegenc->line[i][j] = jpegenc->row[i][j];
537       }
538     }
539   }
540
541   /* guard against a potential error in gst_jpegenc_term_destination
542      which occurs iff bufsize % 4 < free_space_remaining */
543   jpegenc->bufsize = GST_ROUND_UP_4 (jpegenc->bufsize);
544
545   jpeg_suppress_tables (&jpegenc->cinfo, TRUE);
546
547   GST_DEBUG_OBJECT (jpegenc, "resync done");
548 }
549
550 static GstFlowReturn
551 gst_jpegenc_chain (GstPad * pad, GstBuffer * buf)
552 {
553   GstFlowReturn ret;
554   GstJpegEnc *jpegenc;
555   guchar *data;
556   gulong size;
557   guint height;
558   guchar *base[3], *end[3];
559   gint i, j, k;
560
561   jpegenc = GST_JPEGENC (GST_OBJECT_PARENT (pad));
562
563   if (G_UNLIKELY (jpegenc->width <= 0 || jpegenc->height <= 0))
564     goto not_negotiated;
565
566   data = GST_BUFFER_DATA (buf);
567   size = GST_BUFFER_SIZE (buf);
568
569   GST_LOG_OBJECT (jpegenc, "got buffer of %lu bytes", size);
570
571   ret =
572       gst_pad_alloc_buffer_and_set_caps (jpegenc->srcpad,
573       GST_BUFFER_OFFSET_NONE, jpegenc->bufsize, GST_PAD_CAPS (jpegenc->srcpad),
574       &jpegenc->output_buffer);
575
576   if (ret != GST_FLOW_OK)
577     goto done;
578
579   gst_buffer_copy_metadata (jpegenc->output_buffer, buf,
580       GST_BUFFER_COPY_TIMESTAMPS);
581
582   height = jpegenc->height;
583
584   for (i = 0; i < jpegenc->channels; i++) {
585     base[i] = data + jpegenc->offset[i];
586     end[i] = base[i] + jpegenc->cheight[i] * jpegenc->stride[i];
587   }
588
589   jpegenc->jdest.next_output_byte = GST_BUFFER_DATA (jpegenc->output_buffer);
590   jpegenc->jdest.free_in_buffer = GST_BUFFER_SIZE (jpegenc->output_buffer);
591
592   /* prepare for raw input */
593 #if JPEG_LIB_VERSION >= 70
594   jpegenc->cinfo.do_fancy_downsampling = FALSE;
595 #endif
596   jpegenc->cinfo.smoothing_factor = jpegenc->smoothing;
597   jpegenc->cinfo.dct_method = jpegenc->idct_method;
598   jpeg_set_quality (&jpegenc->cinfo, jpegenc->quality, TRUE);
599   jpeg_start_compress (&jpegenc->cinfo, TRUE);
600
601   GST_LOG_OBJECT (jpegenc, "compressing");
602
603   if (jpegenc->planar) {
604     for (i = 0; i < height; i += jpegenc->v_max_samp * DCTSIZE) {
605       for (k = 0; k < jpegenc->channels; k++) {
606         for (j = 0; j < jpegenc->v_samp[k] * DCTSIZE; j++) {
607           jpegenc->line[k][j] = base[k];
608           if (base[k] + jpegenc->stride[k] < end[k])
609             base[k] += jpegenc->stride[k];
610         }
611       }
612       jpeg_write_raw_data (&jpegenc->cinfo, jpegenc->line,
613           jpegenc->v_max_samp * DCTSIZE);
614     }
615   } else {
616     for (i = 0; i < height; i += jpegenc->v_max_samp * DCTSIZE) {
617       for (k = 0; k < jpegenc->channels; k++) {
618         for (j = 0; j < jpegenc->v_samp[k] * DCTSIZE; j++) {
619           guchar *src, *dst;
620           gint l;
621
622           /* ouch, copy line */
623           src = base[k];
624           dst = jpegenc->line[k][j];
625           for (l = jpegenc->cwidth[k]; l > 0; l--) {
626             *dst = *src;
627             src += jpegenc->inc[k];
628             dst++;
629           }
630           if (base[k] + jpegenc->stride[k] < end[k])
631             base[k] += jpegenc->stride[k];
632         }
633       }
634       jpeg_write_raw_data (&jpegenc->cinfo, jpegenc->line,
635           jpegenc->v_max_samp * DCTSIZE);
636     }
637   }
638
639   /* This will ensure that gst_jpegenc_term_destination is called; we push
640      the final output buffer from there */
641   jpeg_finish_compress (&jpegenc->cinfo);
642   GST_LOG_OBJECT (jpegenc, "compressing done");
643
644 done:
645   gst_buffer_unref (buf);
646
647   return ret;
648
649 /* ERRORS */
650 not_negotiated:
651   {
652     GST_WARNING_OBJECT (jpegenc, "no input format set (no caps on buffer)");
653     ret = GST_FLOW_NOT_NEGOTIATED;
654     goto done;
655   }
656 }
657
658 static void
659 gst_jpegenc_set_property (GObject * object, guint prop_id,
660     const GValue * value, GParamSpec * pspec)
661 {
662   GstJpegEnc *jpegenc = GST_JPEGENC (object);
663
664   GST_OBJECT_LOCK (jpegenc);
665
666   switch (prop_id) {
667     case PROP_QUALITY:
668       jpegenc->quality = g_value_get_int (value);
669       break;
670 #ifdef ENABLE_SMOOTHING
671     case PROP_SMOOTHING:
672       jpegenc->smoothing = g_value_get_int (value);
673       break;
674 #endif
675     case PROP_IDCT_METHOD:
676       jpegenc->idct_method = g_value_get_enum (value);
677       break;
678     default:
679       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
680       break;
681   }
682
683   GST_OBJECT_UNLOCK (jpegenc);
684 }
685
686 static void
687 gst_jpegenc_get_property (GObject * object, guint prop_id, GValue * value,
688     GParamSpec * pspec)
689 {
690   GstJpegEnc *jpegenc = GST_JPEGENC (object);
691
692   GST_OBJECT_LOCK (jpegenc);
693
694   switch (prop_id) {
695     case PROP_QUALITY:
696       g_value_set_int (value, jpegenc->quality);
697       break;
698 #ifdef ENABLE_SMOOTHING
699     case PROP_SMOOTHING:
700       g_value_set_int (value, jpegenc->smoothing);
701       break;
702 #endif
703     case PROP_IDCT_METHOD:
704       g_value_set_enum (value, jpegenc->idct_method);
705       break;
706     default:
707       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
708       break;
709   }
710
711   GST_OBJECT_UNLOCK (jpegenc);
712 }
713
714 static GstStateChangeReturn
715 gst_jpegenc_change_state (GstElement * element, GstStateChange transition)
716 {
717   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
718   GstJpegEnc *filter = GST_JPEGENC (element);
719
720   switch (transition) {
721     case GST_STATE_CHANGE_NULL_TO_READY:
722       GST_DEBUG_OBJECT (element, "setting line buffers");
723       filter->line[0] = NULL;
724       filter->line[1] = NULL;
725       filter->line[2] = NULL;
726       break;
727     default:
728       break;
729   }
730
731   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
732   if (ret == GST_STATE_CHANGE_FAILURE)
733     return ret;
734
735   switch (transition) {
736     case GST_STATE_CHANGE_PAUSED_TO_READY:
737       gst_jpegenc_reset (filter);
738       break;
739     default:
740       break;
741   }
742
743   return ret;
744 }