• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  * Copyright (C) 2012 Collabora Ltd.
4  *	Author : Edward Hervey <edward@collabora.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21 /**
22  * SECTION:element-jpegenc
23  * @title: jpegenc
24  *
25  * Encodes jpeg images.
26  *
27  * ## Example launch line
28  * |[
29  * gst-launch-1.0 videotestsrc num-buffers=50 ! video/x-raw, framerate='(fraction)'5/1 ! jpegenc ! avimux ! filesink location=mjpeg.avi
30  * ]| a pipeline to mux 5 JPEG frames per second into a 10 sec. long motion jpeg
31  * avi.
32  *
33  */
34 
35 #ifdef HAVE_CONFIG_H
36 #include "config.h"
37 #endif
38 #include <string.h>
39 
40 #include "gstjpeg.h"
41 #include "gstjpegenc.h"
42 #include "gstjpegelements.h"
43 #include <gst/video/video.h>
44 #include <gst/video/gstvideometa.h>
45 #include <gst/base/base.h>
46 
47 /* experimental */
48 /* setting smoothig seems to have no effect in libjepeg
49 #define ENABLE_SMOOTHING 1
50 */
51 
52 GST_DEBUG_CATEGORY_STATIC (jpegenc_debug);
53 #define GST_CAT_DEFAULT jpegenc_debug
54 
55 #define JPEG_DEFAULT_QUALITY 85
56 #define JPEG_DEFAULT_SMOOTHING 0
57 #define JPEG_DEFAULT_IDCT_METHOD	JDCT_FASTEST
58 #define JPEG_DEFAULT_SNAPSHOT		FALSE
59 
60 /* JpegEnc signals and args */
61 enum
62 {
63   /* FILL ME */
64   LAST_SIGNAL
65 };
66 
67 enum
68 {
69   PROP_0,
70   PROP_QUALITY,
71   PROP_SMOOTHING,
72   PROP_IDCT_METHOD,
73   PROP_SNAPSHOT
74 };
75 
76 static void gst_jpegenc_finalize (GObject * object);
77 
78 static void gst_jpegenc_resync (GstJpegEnc * jpegenc);
79 static void gst_jpegenc_set_property (GObject * object, guint prop_id,
80     const GValue * value, GParamSpec * pspec);
81 static void gst_jpegenc_get_property (GObject * object, guint prop_id,
82     GValue * value, GParamSpec * pspec);
83 
84 static gboolean gst_jpegenc_start (GstVideoEncoder * benc);
85 static gboolean gst_jpegenc_stop (GstVideoEncoder * benc);
86 static gboolean gst_jpegenc_set_format (GstVideoEncoder * encoder,
87     GstVideoCodecState * state);
88 static GstFlowReturn gst_jpegenc_handle_frame (GstVideoEncoder * encoder,
89     GstVideoCodecFrame * frame);
90 static gboolean gst_jpegenc_propose_allocation (GstVideoEncoder * encoder,
91     GstQuery * query);
92 
93 /* static guint gst_jpegenc_signals[LAST_SIGNAL] = { 0 }; */
94 
95 #define gst_jpegenc_parent_class parent_class
96 G_DEFINE_TYPE (GstJpegEnc, gst_jpegenc, GST_TYPE_VIDEO_ENCODER);
97 GST_ELEMENT_REGISTER_DEFINE (jpegenc, "jpegenc", GST_RANK_PRIMARY,
98     GST_TYPE_JPEGENC);
99 
100 /* *INDENT-OFF* */
101 static GstStaticPadTemplate gst_jpegenc_sink_pad_template =
102 GST_STATIC_PAD_TEMPLATE ("sink",
103     GST_PAD_SINK,
104     GST_PAD_ALWAYS,
105     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE
106         ("{ I420, YV12, YUY2, UYVY, Y41B, Y42B, YVYU, Y444, NV21, "
107          "NV12, RGB, BGR, RGBx, xRGB, BGRx, xBGR, GRAY8 }"))
108     );
109 /* *INDENT-ON* */
110 
111 static GstStaticPadTemplate gst_jpegenc_src_pad_template =
112 GST_STATIC_PAD_TEMPLATE ("src",
113     GST_PAD_SRC,
114     GST_PAD_ALWAYS,
115     GST_STATIC_CAPS ("image/jpeg, "
116         "width = (int) [ 1, 65535 ], "
117         "height = (int) [ 1, 65535 ], "
118         "framerate = (fraction) [ 0/1, MAX ], "
119         "sof-marker = (int) { 0, 1, 2, 4, 9 }")
120     );
121 
122 static void
gst_jpegenc_class_init(GstJpegEncClass * klass)123 gst_jpegenc_class_init (GstJpegEncClass * klass)
124 {
125   GObjectClass *gobject_class;
126   GstElementClass *element_class;
127   GstVideoEncoderClass *venc_class;
128 
129   gobject_class = (GObjectClass *) klass;
130   element_class = (GstElementClass *) klass;
131   venc_class = (GstVideoEncoderClass *) klass;
132 
133   parent_class = g_type_class_peek_parent (klass);
134 
135   gobject_class->finalize = gst_jpegenc_finalize;
136   gobject_class->set_property = gst_jpegenc_set_property;
137   gobject_class->get_property = gst_jpegenc_get_property;
138 
139   g_object_class_install_property (gobject_class, PROP_QUALITY,
140       g_param_spec_int ("quality", "Quality", "Quality of encoding",
141           0, 100, JPEG_DEFAULT_QUALITY,
142           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
143           GST_PARAM_MUTABLE_PLAYING));
144 
145 #ifdef ENABLE_SMOOTHING
146   /* disabled, since it doesn't seem to work */
147   g_object_class_install_property (gobject_class, PROP_SMOOTHING,
148       g_param_spec_int ("smoothing", "Smoothing", "Smoothing factor",
149           0, 100, JPEG_DEFAULT_SMOOTHING,
150           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
151 #endif
152 
153   g_object_class_install_property (gobject_class, PROP_IDCT_METHOD,
154       g_param_spec_enum ("idct-method", "IDCT Method",
155           "The IDCT algorithm to use", GST_TYPE_IDCT_METHOD,
156           JPEG_DEFAULT_IDCT_METHOD,
157           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
158   /**
159    * GstJpegEnc:snapshot:
160    *
161    * Send EOS after encoding a frame, useful for snapshots.
162    *
163    * Since: 1.14
164    */
165   g_object_class_install_property (gobject_class, PROP_SNAPSHOT,
166       g_param_spec_boolean ("snapshot", "Snapshot",
167           "Send EOS after encoding a frame, useful for snapshots",
168           JPEG_DEFAULT_SNAPSHOT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
169 
170   gst_element_class_add_static_pad_template (element_class,
171       &gst_jpegenc_sink_pad_template);
172   gst_element_class_add_static_pad_template (element_class,
173       &gst_jpegenc_src_pad_template);
174 
175   gst_element_class_set_static_metadata (element_class, "JPEG image encoder",
176       "Codec/Encoder/Image", "Encode images in JPEG format",
177       "Wim Taymans <wim.taymans@tvd.be>");
178 
179   venc_class->start = gst_jpegenc_start;
180   venc_class->stop = gst_jpegenc_stop;
181   venc_class->set_format = gst_jpegenc_set_format;
182   venc_class->handle_frame = gst_jpegenc_handle_frame;
183   venc_class->propose_allocation = gst_jpegenc_propose_allocation;
184 
185   GST_DEBUG_CATEGORY_INIT (jpegenc_debug, "jpegenc", 0,
186       "JPEG encoding element");
187 }
188 
189 static void
gst_jpegenc_init_destination(j_compress_ptr cinfo)190 gst_jpegenc_init_destination (j_compress_ptr cinfo)
191 {
192   GST_DEBUG ("gst_jpegenc_chain: init_destination");
193 }
194 
195 static void
ensure_memory(GstJpegEnc * jpegenc)196 ensure_memory (GstJpegEnc * jpegenc)
197 {
198   GstMemory *new_memory;
199   GstMapInfo map;
200   gsize old_size, desired_size, new_size;
201   guint8 *new_data;
202   static GstAllocationParams params = { 0, 3, 0, 0, };
203 
204   old_size = jpegenc->output_map.size;
205   if (old_size == 0)
206     desired_size = jpegenc->bufsize;
207   else
208     desired_size = old_size * 2;
209 
210   /* Our output memory wasn't big enough.
211    * Make a new memory that's twice the size, */
212   new_memory = gst_allocator_alloc (NULL, desired_size, &params);
213   gst_memory_map (new_memory, &map, GST_MAP_READWRITE);
214   new_data = map.data;
215   new_size = map.size;
216 
217   /* copy previous data if any */
218   if (jpegenc->output_mem) {
219     memcpy (new_data, jpegenc->output_map.data, old_size);
220     gst_memory_unmap (jpegenc->output_mem, &jpegenc->output_map);
221     gst_memory_unref (jpegenc->output_mem);
222   }
223 
224   /* drop it into place, */
225   jpegenc->output_mem = new_memory;
226   jpegenc->output_map = map;
227 
228   /* and last, update libjpeg on where to work. */
229   jpegenc->jdest.next_output_byte = new_data + old_size;
230   jpegenc->jdest.free_in_buffer = new_size - old_size;
231 }
232 
233 static boolean
gst_jpegenc_flush_destination(j_compress_ptr cinfo)234 gst_jpegenc_flush_destination (j_compress_ptr cinfo)
235 {
236   GstJpegEnc *jpegenc = (GstJpegEnc *) (cinfo->client_data);
237 
238   GST_DEBUG_OBJECT (jpegenc,
239       "gst_jpegenc_chain: flush_destination: buffer too small");
240 
241   ensure_memory (jpegenc);
242 
243   return TRUE;
244 }
245 
246 static void
gst_jpegenc_term_destination(j_compress_ptr cinfo)247 gst_jpegenc_term_destination (j_compress_ptr cinfo)
248 {
249   GstBuffer *outbuf;
250   GstJpegEnc *jpegenc = (GstJpegEnc *) (cinfo->client_data);
251   gsize memory_size = jpegenc->output_map.size - jpegenc->jdest.free_in_buffer;
252   GstByteReader reader =
253       GST_BYTE_READER_INIT (jpegenc->output_map.data, memory_size);
254   guint16 marker;
255   gint sof_marker = -1;
256 
257   GST_DEBUG_OBJECT (jpegenc, "gst_jpegenc_chain: term_source");
258 
259   /* Find the SOF marker */
260   while (gst_byte_reader_get_uint16_be (&reader, &marker)) {
261     /* SOF marker */
262     if (marker >> 4 == 0x0ffc) {
263       sof_marker = marker & 0x4;
264       break;
265     }
266   }
267 
268   gst_memory_unmap (jpegenc->output_mem, &jpegenc->output_map);
269   /* Trim the buffer size. we will push it in the chain function */
270   gst_memory_resize (jpegenc->output_mem, 0, memory_size);
271   jpegenc->output_map.data = NULL;
272   jpegenc->output_map.size = 0;
273 
274   if (jpegenc->sof_marker != sof_marker || jpegenc->input_caps_changed) {
275     GstVideoCodecState *output;
276     output =
277         gst_video_encoder_set_output_state (GST_VIDEO_ENCODER (jpegenc),
278         gst_caps_new_simple ("image/jpeg", "sof-marker", G_TYPE_INT, sof_marker,
279             NULL), jpegenc->input_state);
280     gst_video_codec_state_unref (output);
281     jpegenc->sof_marker = sof_marker;
282     jpegenc->input_caps_changed = FALSE;
283   }
284 
285   outbuf = gst_buffer_new ();
286   gst_buffer_append_memory (outbuf, jpegenc->output_mem);
287   jpegenc->output_mem = NULL;
288 
289   jpegenc->current_frame->output_buffer = outbuf;
290 
291   gst_video_frame_unmap (&jpegenc->current_vframe);
292 
293   GST_VIDEO_CODEC_FRAME_SET_SYNC_POINT (jpegenc->current_frame);
294 
295   jpegenc->res = gst_video_encoder_finish_frame (GST_VIDEO_ENCODER (jpegenc),
296       jpegenc->current_frame);
297   jpegenc->current_frame = NULL;
298 }
299 
300 static void
gst_jpegenc_init(GstJpegEnc * jpegenc)301 gst_jpegenc_init (GstJpegEnc * jpegenc)
302 {
303   GST_PAD_SET_ACCEPT_TEMPLATE (GST_VIDEO_ENCODER_SINK_PAD (jpegenc));
304 
305   /* setup jpeglib */
306   memset (&jpegenc->cinfo, 0, sizeof (jpegenc->cinfo));
307   memset (&jpegenc->jerr, 0, sizeof (jpegenc->jerr));
308   jpegenc->cinfo.err = jpeg_std_error (&jpegenc->jerr);
309   jpeg_create_compress (&jpegenc->cinfo);
310 
311   jpegenc->jdest.init_destination = gst_jpegenc_init_destination;
312   jpegenc->jdest.empty_output_buffer = gst_jpegenc_flush_destination;
313   jpegenc->jdest.term_destination = gst_jpegenc_term_destination;
314   jpegenc->cinfo.dest = &jpegenc->jdest;
315   jpegenc->cinfo.client_data = jpegenc;
316 
317   /* init properties */
318   jpegenc->quality = JPEG_DEFAULT_QUALITY;
319   jpegenc->smoothing = JPEG_DEFAULT_SMOOTHING;
320   jpegenc->idct_method = JPEG_DEFAULT_IDCT_METHOD;
321   jpegenc->snapshot = JPEG_DEFAULT_SNAPSHOT;
322 }
323 
324 static void
gst_jpegenc_finalize(GObject * object)325 gst_jpegenc_finalize (GObject * object)
326 {
327   GstJpegEnc *filter = GST_JPEGENC (object);
328 
329   jpeg_destroy_compress (&filter->cinfo);
330 
331   if (filter->input_state)
332     gst_video_codec_state_unref (filter->input_state);
333 
334   G_OBJECT_CLASS (parent_class)->finalize (object);
335 }
336 
337 static gboolean
gst_jpegenc_set_format(GstVideoEncoder * encoder,GstVideoCodecState * state)338 gst_jpegenc_set_format (GstVideoEncoder * encoder, GstVideoCodecState * state)
339 {
340   GstJpegEnc *enc = GST_JPEGENC (encoder);
341   gint i;
342   GstVideoInfo *info = &state->info;
343 
344   if (enc->input_state)
345     gst_video_codec_state_unref (enc->input_state);
346   enc->input_state = gst_video_codec_state_ref (state);
347 
348   /* prepare a cached image description  */
349   enc->channels = GST_VIDEO_INFO_N_COMPONENTS (info);
350 
351   /* ... but any alpha is disregarded in encoding */
352   if (GST_VIDEO_INFO_IS_GRAY (info))
353     enc->channels = 1;
354 
355   enc->h_max_samp = 0;
356   enc->v_max_samp = 0;
357   for (i = 0; i < enc->channels; ++i) {
358     enc->cwidth[i] = GST_VIDEO_INFO_COMP_WIDTH (info, i);
359     enc->cheight[i] = GST_VIDEO_INFO_COMP_HEIGHT (info, i);
360     enc->inc[i] = GST_VIDEO_INFO_COMP_PSTRIDE (info, i);
361     enc->h_samp[i] =
362         GST_ROUND_UP_4 (GST_VIDEO_INFO_WIDTH (info)) / enc->cwidth[i];
363     enc->h_max_samp = MAX (enc->h_max_samp, enc->h_samp[i]);
364     enc->v_samp[i] =
365         GST_ROUND_UP_4 (GST_VIDEO_INFO_HEIGHT (info)) / enc->cheight[i];
366     enc->v_max_samp = MAX (enc->v_max_samp, enc->v_samp[i]);
367   }
368   /* samp should only be 1, 2 or 4 */
369   g_assert (enc->h_max_samp <= 4);
370   g_assert (enc->v_max_samp <= 4);
371 
372   /* now invert */
373   /* maximum is invariant, as one of the components should have samp 1 */
374   for (i = 0; i < enc->channels; ++i) {
375     GST_DEBUG ("%d %d", enc->h_samp[i], enc->h_max_samp);
376     enc->h_samp[i] = enc->h_max_samp / enc->h_samp[i];
377     enc->v_samp[i] = enc->v_max_samp / enc->v_samp[i];
378   }
379   enc->planar = (enc->inc[0] == 1 && enc->inc[1] == 1 && enc->inc[2] == 1);
380 
381   enc->input_caps_changed = TRUE;
382   gst_jpegenc_resync (enc);
383 
384   return TRUE;
385 }
386 
387 static void
gst_jpegenc_resync(GstJpegEnc * jpegenc)388 gst_jpegenc_resync (GstJpegEnc * jpegenc)
389 {
390   GstVideoInfo *info;
391   gint width, height;
392   gint i, j;
393 
394   GST_DEBUG_OBJECT (jpegenc, "resync");
395 
396   if (!jpegenc->input_state)
397     return;
398 
399   info = &jpegenc->input_state->info;
400 
401   jpegenc->cinfo.image_width = width = GST_VIDEO_INFO_WIDTH (info);
402   jpegenc->cinfo.image_height = height = GST_VIDEO_INFO_HEIGHT (info);
403   jpegenc->cinfo.input_components = jpegenc->channels;
404 
405   GST_DEBUG_OBJECT (jpegenc, "width %d, height %d", width, height);
406   GST_DEBUG_OBJECT (jpegenc, "format %d", GST_VIDEO_INFO_FORMAT (info));
407 
408   if (GST_VIDEO_INFO_IS_RGB (info)) {
409     GST_DEBUG_OBJECT (jpegenc, "RGB");
410     jpegenc->cinfo.in_color_space = JCS_RGB;
411   } else if (GST_VIDEO_INFO_IS_GRAY (info)) {
412     GST_DEBUG_OBJECT (jpegenc, "gray");
413     jpegenc->cinfo.in_color_space = JCS_GRAYSCALE;
414   } else {
415     GST_DEBUG_OBJECT (jpegenc, "YUV");
416     jpegenc->cinfo.in_color_space = JCS_YCbCr;
417   }
418 
419   /* input buffer size as max output */
420   jpegenc->bufsize = GST_VIDEO_INFO_SIZE (info);
421   jpeg_set_defaults (&jpegenc->cinfo);
422   jpegenc->cinfo.raw_data_in = TRUE;
423   /* duh, libjpeg maps RGB to YUV ... and don't expect some conversion */
424   if (jpegenc->cinfo.in_color_space == JCS_RGB)
425     jpeg_set_colorspace (&jpegenc->cinfo, JCS_RGB);
426 
427   GST_DEBUG_OBJECT (jpegenc, "h_max_samp=%d, v_max_samp=%d",
428       jpegenc->h_max_samp, jpegenc->v_max_samp);
429   /* image dimension info */
430   for (i = 0; i < jpegenc->channels; i++) {
431     GST_DEBUG_OBJECT (jpegenc, "comp %i: h_samp=%d, v_samp=%d", i,
432         jpegenc->h_samp[i], jpegenc->v_samp[i]);
433     jpegenc->cinfo.comp_info[i].h_samp_factor = jpegenc->h_samp[i];
434     jpegenc->cinfo.comp_info[i].v_samp_factor = jpegenc->v_samp[i];
435     g_free (jpegenc->line[i]);
436     jpegenc->line[i] = g_new (guchar *, jpegenc->v_max_samp * DCTSIZE);
437     if (!jpegenc->planar) {
438       for (j = 0; j < jpegenc->v_max_samp * DCTSIZE; j++) {
439         g_free (jpegenc->row[i][j]);
440         jpegenc->row[i][j] = g_malloc (width);
441         jpegenc->line[i][j] = jpegenc->row[i][j];
442       }
443     }
444   }
445 
446   /* guard against a potential error in gst_jpegenc_term_destination
447      which occurs iff bufsize % 4 < free_space_remaining */
448   jpegenc->bufsize = GST_ROUND_UP_4 (jpegenc->bufsize);
449 
450   jpeg_suppress_tables (&jpegenc->cinfo, TRUE);
451 
452   GST_DEBUG_OBJECT (jpegenc, "resync done");
453 }
454 
455 static GstFlowReturn
gst_jpegenc_handle_frame(GstVideoEncoder * encoder,GstVideoCodecFrame * frame)456 gst_jpegenc_handle_frame (GstVideoEncoder * encoder, GstVideoCodecFrame * frame)
457 {
458   GstJpegEnc *jpegenc;
459   guint height;
460   guchar *base[3], *end[3];
461   guint stride[3];
462   gint i, j, k;
463   static GstAllocationParams params = { 0, 0, 0, 3, };
464 
465   jpegenc = GST_JPEGENC (encoder);
466 
467   GST_LOG_OBJECT (jpegenc, "got new frame");
468 
469   if (!gst_video_frame_map (&jpegenc->current_vframe,
470           &jpegenc->input_state->info, frame->input_buffer, GST_MAP_READ))
471     goto invalid_frame;
472 
473   jpegenc->current_frame = frame;
474 
475   height = GST_VIDEO_INFO_HEIGHT (&jpegenc->input_state->info);
476 
477   for (i = 0; i < jpegenc->channels; i++) {
478     base[i] = GST_VIDEO_FRAME_COMP_DATA (&jpegenc->current_vframe, i);
479     stride[i] = GST_VIDEO_FRAME_COMP_STRIDE (&jpegenc->current_vframe, i);
480     end[i] =
481         base[i] + GST_VIDEO_FRAME_COMP_HEIGHT (&jpegenc->current_vframe,
482         i) * stride[i];
483   }
484 
485   jpegenc->res = GST_FLOW_OK;
486   jpegenc->output_mem = gst_allocator_alloc (NULL, jpegenc->bufsize, &params);
487   gst_memory_map (jpegenc->output_mem, &jpegenc->output_map, GST_MAP_READWRITE);
488 
489   jpegenc->jdest.next_output_byte = jpegenc->output_map.data;
490   jpegenc->jdest.free_in_buffer = jpegenc->output_map.size;
491 
492   /* prepare for raw input */
493 #if JPEG_LIB_VERSION >= 70
494   jpegenc->cinfo.do_fancy_downsampling = FALSE;
495 #endif
496 
497   GST_OBJECT_LOCK (jpegenc);
498   jpegenc->cinfo.smoothing_factor = jpegenc->smoothing;
499   jpegenc->cinfo.dct_method = jpegenc->idct_method;
500   jpeg_set_quality (&jpegenc->cinfo, jpegenc->quality, TRUE);
501   GST_OBJECT_UNLOCK (jpegenc);
502 
503   jpeg_start_compress (&jpegenc->cinfo, TRUE);
504 
505   GST_LOG_OBJECT (jpegenc, "compressing");
506 
507   if (jpegenc->planar) {
508     for (i = 0; i < height; i += jpegenc->v_max_samp * DCTSIZE) {
509       for (k = 0; k < jpegenc->channels; k++) {
510         for (j = 0; j < jpegenc->v_samp[k] * DCTSIZE; j++) {
511           jpegenc->line[k][j] = base[k];
512           if (base[k] + stride[k] < end[k])
513             base[k] += stride[k];
514         }
515       }
516       jpeg_write_raw_data (&jpegenc->cinfo, jpegenc->line,
517           jpegenc->v_max_samp * DCTSIZE);
518     }
519   } else {
520     for (i = 0; i < height; i += jpegenc->v_max_samp * DCTSIZE) {
521       for (k = 0; k < jpegenc->channels; k++) {
522         for (j = 0; j < jpegenc->v_samp[k] * DCTSIZE; j++) {
523           guchar *src, *dst;
524           gint l;
525 
526           /* ouch, copy line */
527           src = base[k];
528           dst = jpegenc->line[k][j];
529           for (l = jpegenc->cwidth[k]; l > 0; l--) {
530             *dst = *src;
531             src += jpegenc->inc[k];
532             dst++;
533           }
534           if (base[k] + stride[k] < end[k])
535             base[k] += stride[k];
536         }
537       }
538       jpeg_write_raw_data (&jpegenc->cinfo, jpegenc->line,
539           jpegenc->v_max_samp * DCTSIZE);
540     }
541   }
542 
543   /* This will ensure that gst_jpegenc_term_destination is called */
544   jpeg_finish_compress (&jpegenc->cinfo);
545   GST_LOG_OBJECT (jpegenc, "compressing done");
546 
547   return (jpegenc->snapshot) ? GST_FLOW_EOS : jpegenc->res;
548 
549 invalid_frame:
550   {
551     GST_WARNING_OBJECT (jpegenc, "invalid frame received");
552     return gst_video_encoder_finish_frame (encoder, frame);
553   }
554 }
555 
556 static gboolean
gst_jpegenc_propose_allocation(GstVideoEncoder * encoder,GstQuery * query)557 gst_jpegenc_propose_allocation (GstVideoEncoder * encoder, GstQuery * query)
558 {
559   gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
560 
561   return GST_VIDEO_ENCODER_CLASS (parent_class)->propose_allocation (encoder,
562       query);
563 }
564 
565 static void
gst_jpegenc_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)566 gst_jpegenc_set_property (GObject * object, guint prop_id,
567     const GValue * value, GParamSpec * pspec)
568 {
569   GstJpegEnc *jpegenc = GST_JPEGENC (object);
570 
571   GST_OBJECT_LOCK (jpegenc);
572 
573   switch (prop_id) {
574     case PROP_QUALITY:
575       jpegenc->quality = g_value_get_int (value);
576       break;
577 #ifdef ENABLE_SMOOTHING
578     case PROP_SMOOTHING:
579       jpegenc->smoothing = g_value_get_int (value);
580       break;
581 #endif
582     case PROP_IDCT_METHOD:
583       jpegenc->idct_method = g_value_get_enum (value);
584       break;
585     case PROP_SNAPSHOT:
586       jpegenc->snapshot = g_value_get_boolean (value);
587       break;
588     default:
589       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
590       break;
591   }
592 
593   GST_OBJECT_UNLOCK (jpegenc);
594 }
595 
596 static void
gst_jpegenc_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)597 gst_jpegenc_get_property (GObject * object, guint prop_id, GValue * value,
598     GParamSpec * pspec)
599 {
600   GstJpegEnc *jpegenc = GST_JPEGENC (object);
601 
602   GST_OBJECT_LOCK (jpegenc);
603 
604   switch (prop_id) {
605     case PROP_QUALITY:
606       g_value_set_int (value, jpegenc->quality);
607       break;
608 #ifdef ENABLE_SMOOTHING
609     case PROP_SMOOTHING:
610       g_value_set_int (value, jpegenc->smoothing);
611       break;
612 #endif
613     case PROP_IDCT_METHOD:
614       g_value_set_enum (value, jpegenc->idct_method);
615       break;
616     case PROP_SNAPSHOT:
617       g_value_set_boolean (value, jpegenc->snapshot);
618       break;
619     default:
620       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
621       break;
622   }
623 
624   GST_OBJECT_UNLOCK (jpegenc);
625 }
626 
627 static gboolean
gst_jpegenc_start(GstVideoEncoder * benc)628 gst_jpegenc_start (GstVideoEncoder * benc)
629 {
630   GstJpegEnc *enc = (GstJpegEnc *) benc;
631 
632   enc->line[0] = NULL;
633   enc->line[1] = NULL;
634   enc->line[2] = NULL;
635   enc->sof_marker = -1;
636 
637   return TRUE;
638 }
639 
640 static gboolean
gst_jpegenc_stop(GstVideoEncoder * benc)641 gst_jpegenc_stop (GstVideoEncoder * benc)
642 {
643   GstJpegEnc *enc = (GstJpegEnc *) benc;
644   gint i, j;
645 
646   g_free (enc->line[0]);
647   g_free (enc->line[1]);
648   g_free (enc->line[2]);
649   enc->line[0] = NULL;
650   enc->line[1] = NULL;
651   enc->line[2] = NULL;
652   for (i = 0; i < 3; i++) {
653     for (j = 0; j < 4 * DCTSIZE; j++) {
654       g_free (enc->row[i][j]);
655       enc->row[i][j] = NULL;
656     }
657   }
658 
659   return TRUE;
660 }
661