gstjpegenc.c 21.5 KB
Newer Older
Christian Schaller's avatar
Christian Schaller committed
1
/* GStreamer
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
19 20 21 22
/**
 * SECTION:element-jpegenc
 *
 * Encodes jpeg images.
23 24 25 26 27 28 29 30
 *
 * <refsect2>
 * <title>Example launch line</title>
 * |[
 * gst-launch videotestsrc num-buffers=50 ! video/x-raw-yuv, framerate='(fraction)'5/1 ! jpegenc ! avimux ! filesink location=mjpeg.avi
 * ]| a pipeline to mux 5 JPEG frames per second into a 10 sec. long motion jpeg
 * avi.
 * </refsect2>
31
 */
32

33 34 35
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
36 37 38
#include <string.h>

#include "gstjpegenc.h"
39
#include "gstjpeg.h"
Iain Holmes's avatar
Iain Holmes committed
40
#include <gst/video/video.h>
41

Stefan Kost's avatar
Stefan Kost committed
42 43 44 45
/* experimental */
/* setting smoothig seems to have no effect in libjepeg
#define ENABLE_SMOOTHING 1
*/
46

47
GST_DEBUG_CATEGORY_STATIC (jpegenc_debug);
48 49
#define GST_CAT_DEFAULT jpegenc_debug

50 51 52
#define JPEG_DEFAULT_QUALITY 85
#define JPEG_DEFAULT_SMOOTHING 0
#define JPEG_DEFAULT_IDCT_METHOD	JDCT_FASTEST
53

54
/* JpegEnc signals and args */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
55 56
enum
{
57 58 59 60
  /* FILL ME */
  LAST_SIGNAL
};

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
61 62
enum
{
Stefan Kost's avatar
Stefan Kost committed
63 64 65 66
  PROP_0,
  PROP_QUALITY,
  PROP_SMOOTHING,
  PROP_IDCT_METHOD
67 68
};

69
static void gst_jpegenc_reset (GstJpegEnc * enc);
70
static void gst_jpegenc_finalize (GObject * object);
71

Wim Taymans's avatar
Wim Taymans committed
72 73 74 75
static GstFlowReturn gst_jpegenc_chain (GstPad * pad, GstObject * parent,
    GstBuffer * buf);
static gboolean gst_jpegenc_sink_event (GstPad * pad, GstObject * parent,
    GstEvent * event);
Wim Taymans's avatar
Wim Taymans committed
76
static GstCaps *gst_jpegenc_getcaps (GstPad * pad, GstCaps * filter);
Wim Taymans's avatar
Wim Taymans committed
77 78
static gboolean gst_jpegenc_sink_query (GstPad * pad, GstObject * parent,
    GstQuery * query);
79

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
80 81 82 83 84
static void gst_jpegenc_resync (GstJpegEnc * jpegenc);
static void gst_jpegenc_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec);
static void gst_jpegenc_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec);
85 86
static GstStateChangeReturn gst_jpegenc_change_state (GstElement * element,
    GstStateChange transition);
87

88 89
#define gst_jpegenc_parent_class parent_class
G_DEFINE_TYPE (GstJpegEnc, gst_jpegenc, GST_TYPE_ELEMENT);
90

91
/* *INDENT-OFF* */
92
static GstStaticPadTemplate gst_jpegenc_sink_pad_template =
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
93
GST_STATIC_PAD_TEMPLATE ("sink",
94 95
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
96 97 98
    GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE
        ("{ I420, YV12, YUY2, UYVY, Y41B, Y42B, YVYU, Y444, "
         "RGB, BGR, RGBx, xRGB, BGRx, xBGR, GRAY8 }"))
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
99
    );
100
/* *INDENT-ON* */
101 102

static GstStaticPadTemplate gst_jpegenc_src_pad_template =
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
103
GST_STATIC_PAD_TEMPLATE ("src",
104 105 106
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("image/jpeg, "
107 108
        "width = (int) [ 16, 65535 ], "
        "height = (int) [ 16, 65535 ], " "framerate = (fraction) [ 0/1, MAX ]")
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
109
    );
Iain Holmes's avatar
Iain Holmes committed
110 111


112
static void
113
gst_jpegenc_class_init (GstJpegEncClass * klass)
114 115 116 117
{
  GObjectClass *gobject_class;
  GstElementClass *gstelement_class;

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
118 119
  gobject_class = (GObjectClass *) klass;
  gstelement_class = (GstElementClass *) klass;
120

121 122 123
  gobject_class->finalize = gst_jpegenc_finalize;
  gobject_class->set_property = gst_jpegenc_set_property;
  gobject_class->get_property = gst_jpegenc_get_property;
124

Stefan Kost's avatar
Stefan Kost committed
125
  g_object_class_install_property (gobject_class, PROP_QUALITY,
126
      g_param_spec_int ("quality", "Quality", "Quality of encoding",
127 128
          0, 100, JPEG_DEFAULT_QUALITY,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
Stefan Kost's avatar
Stefan Kost committed
129

Benjamin Otte's avatar
Benjamin Otte committed
130
#ifdef ENABLE_SMOOTHING
131
  /* disabled, since it doesn't seem to work */
Stefan Kost's avatar
Stefan Kost committed
132
  g_object_class_install_property (gobject_class, PROP_SMOOTHING,
133
      g_param_spec_int ("smoothing", "Smoothing", "Smoothing factor",
134 135
          0, 100, JPEG_DEFAULT_SMOOTHING,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
136 137
#endif

Stefan Kost's avatar
Stefan Kost committed
138 139 140
  g_object_class_install_property (gobject_class, PROP_IDCT_METHOD,
      g_param_spec_enum ("idct-method", "IDCT Method",
          "The IDCT algorithm to use", GST_TYPE_IDCT_METHOD,
141 142
          JPEG_DEFAULT_IDCT_METHOD,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
Stefan Kost's avatar
Stefan Kost committed
143

144 145
  gstelement_class->change_state = gst_jpegenc_change_state;

146 147 148 149 150 151 152
  gst_element_class_add_pad_template (gstelement_class,
      gst_static_pad_template_get (&gst_jpegenc_sink_pad_template));
  gst_element_class_add_pad_template (gstelement_class,
      gst_static_pad_template_get (&gst_jpegenc_src_pad_template));
  gst_element_class_set_details_simple (gstelement_class, "JPEG image encoder",
      "Codec/Encoder/Image",
      "Encode images in JPEG format", "Wim Taymans <wim.taymans@tvd.be>");
153 154 155

  GST_DEBUG_CATEGORY_INIT (jpegenc_debug, "jpegenc", 0,
      "JPEG encoding element");
156 157
}

Wim Taymans's avatar
Wim Taymans committed
158 159 160 161
static void
ensure_memory (GstJpegEnc * jpegenc)
{
  GstMemory *new_memory;
Wim Taymans's avatar
Wim Taymans committed
162
  GstMapInfo map;
Wim Taymans's avatar
Wim Taymans committed
163 164
  gsize old_size, desired_size, new_size;
  guint8 *new_data;
Wim Taymans's avatar
Wim Taymans committed
165
  static GstAllocationParams params = { 0, 0, 0, 3, };
Wim Taymans's avatar
Wim Taymans committed
166

Wim Taymans's avatar
Wim Taymans committed
167
  old_size = jpegenc->output_map.size;
Wim Taymans's avatar
Wim Taymans committed
168 169 170 171 172 173 174
  if (old_size == 0)
    desired_size = jpegenc->bufsize;
  else
    desired_size = old_size * 2;

  /* Our output memory wasn't big enough.
   * Make a new memory that's twice the size, */
Wim Taymans's avatar
Wim Taymans committed
175
  new_memory = gst_allocator_alloc (NULL, desired_size, &params);
Wim Taymans's avatar
Wim Taymans committed
176 177 178
  gst_memory_map (new_memory, &map, GST_MAP_READWRITE);
  new_data = map.data;
  new_size = map.size;
Wim Taymans's avatar
Wim Taymans committed
179 180 181

  /* copy previous data if any */
  if (jpegenc->output_mem) {
Wim Taymans's avatar
Wim Taymans committed
182 183
    memcpy (new_data, jpegenc->output_map.data, old_size);
    gst_memory_unmap (jpegenc->output_mem, &jpegenc->output_map);
Wim Taymans's avatar
Wim Taymans committed
184 185 186 187 188
    gst_memory_unref (jpegenc->output_mem);
  }

  /* drop it into place, */
  jpegenc->output_mem = new_memory;
Wim Taymans's avatar
Wim Taymans committed
189
  jpegenc->output_map = map;
Wim Taymans's avatar
Wim Taymans committed
190 191 192 193 194 195

  /* and last, update libjpeg on where to work. */
  jpegenc->jdest.next_output_byte = new_data + old_size;
  jpegenc->jdest.free_in_buffer = new_size - old_size;
}

196 197 198
static void
gst_jpegenc_init_destination (j_compress_ptr cinfo)
{
199
  GST_DEBUG ("gst_jpegenc_chain: init_destination");
200 201
}

202
static boolean
203 204
gst_jpegenc_flush_destination (j_compress_ptr cinfo)
{
205
  GstJpegEnc *jpegenc = (GstJpegEnc *) (cinfo->client_data);
Wim Taymans's avatar
Wim Taymans committed
206

207 208 209
  GST_DEBUG_OBJECT (jpegenc,
      "gst_jpegenc_chain: flush_destination: buffer too small");

Wim Taymans's avatar
Wim Taymans committed
210
  ensure_memory (jpegenc);
211

212 213 214
  return TRUE;
}

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
215 216
static void
gst_jpegenc_term_destination (j_compress_ptr cinfo)
217
{
218 219 220
  GstJpegEnc *jpegenc = (GstJpegEnc *) (cinfo->client_data);
  GST_DEBUG_OBJECT (jpegenc, "gst_jpegenc_chain: term_source");

Wim Taymans's avatar
Wim Taymans committed
221
  gst_memory_unmap (jpegenc->output_mem, &jpegenc->output_map);
Wim Taymans's avatar
Wim Taymans committed
222
  /* Trim the buffer size. we will push it in the chain function */
Wim Taymans's avatar
Wim Taymans committed
223
  gst_memory_resize (jpegenc->output_mem, 0,
Wim Taymans's avatar
Wim Taymans committed
224 225 226
      jpegenc->output_map.size - jpegenc->jdest.free_in_buffer);
  jpegenc->output_map.data = NULL;
  jpegenc->output_map.size = 0;
227 228 229
}

static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
230
gst_jpegenc_init (GstJpegEnc * jpegenc)
231 232
{
  /* create the sink and src pads */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
233
  jpegenc->sinkpad =
234
      gst_pad_new_from_static_template (&gst_jpegenc_sink_pad_template, "sink");
235 236
  gst_pad_set_chain_function (jpegenc->sinkpad,
      GST_DEBUG_FUNCPTR (gst_jpegenc_chain));
Wim Taymans's avatar
Wim Taymans committed
237 238
  gst_pad_set_query_function (jpegenc->sinkpad,
      GST_DEBUG_FUNCPTR (gst_jpegenc_sink_query));
Wim Taymans's avatar
Wim Taymans committed
239 240
  gst_pad_set_event_function (jpegenc->sinkpad,
      GST_DEBUG_FUNCPTR (gst_jpegenc_sink_event));
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
241 242 243
  gst_element_add_pad (GST_ELEMENT (jpegenc), jpegenc->sinkpad);

  jpegenc->srcpad =
244
      gst_pad_new_from_static_template (&gst_jpegenc_src_pad_template, "src");
245
  gst_pad_use_fixed_caps (jpegenc->srcpad);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
246
  gst_element_add_pad (GST_ELEMENT (jpegenc), jpegenc->srcpad);
247

Christian Schaller's avatar
Christian Schaller committed
248
  /* reset the initial video state */
Wim Taymans's avatar
Wim Taymans committed
249
  gst_video_info_init (&jpegenc->info);
250 251

  /* setup jpeglib */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
252 253 254 255
  memset (&jpegenc->cinfo, 0, sizeof (jpegenc->cinfo));
  memset (&jpegenc->jerr, 0, sizeof (jpegenc->jerr));
  jpegenc->cinfo.err = jpeg_std_error (&jpegenc->jerr);
  jpeg_create_compress (&jpegenc->cinfo);
256 257 258 259 260

  jpegenc->jdest.init_destination = gst_jpegenc_init_destination;
  jpegenc->jdest.empty_output_buffer = gst_jpegenc_flush_destination;
  jpegenc->jdest.term_destination = gst_jpegenc_term_destination;
  jpegenc->cinfo.dest = &jpegenc->jdest;
261 262
  jpegenc->cinfo.client_data = jpegenc;

Stefan Kost's avatar
Stefan Kost committed
263
  /* init properties */
264 265 266
  jpegenc->quality = JPEG_DEFAULT_QUALITY;
  jpegenc->smoothing = JPEG_DEFAULT_SMOOTHING;
  jpegenc->idct_method = JPEG_DEFAULT_IDCT_METHOD;
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284

  gst_jpegenc_reset (jpegenc);
}

static void
gst_jpegenc_reset (GstJpegEnc * enc)
{
  gint i, j;

  g_free (enc->line[0]);
  g_free (enc->line[1]);
  g_free (enc->line[2]);
  enc->line[0] = NULL;
  enc->line[1] = NULL;
  enc->line[2] = NULL;
  for (i = 0; i < 3; i++) {
    for (j = 0; j < 4 * DCTSIZE; j++) {
      g_free (enc->row[i][j]);
285
      enc->row[i][j] = NULL;
286 287 288
    }
  }

Wim Taymans's avatar
Wim Taymans committed
289
  gst_video_info_init (&enc->info);
290 291
}

292 293 294 295 296 297 298 299 300 301
static void
gst_jpegenc_finalize (GObject * object)
{
  GstJpegEnc *filter = GST_JPEGENC (object);

  jpeg_destroy_compress (&filter->cinfo);

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

302
static GstCaps *
Wim Taymans's avatar
Wim Taymans committed
303
gst_jpegenc_getcaps (GstPad * pad, GstCaps * filter)
304 305
{
  GstJpegEnc *jpegenc = GST_JPEGENC (gst_pad_get_parent (pad));
306 307 308
  GstCaps *caps, *othercaps;
  const GstCaps *templ;
  gint i, j;
309
  GstStructure *structure = NULL;
310

311 312
  /* we want to proxy properties like width, height and framerate from the
     other end of the element */
313

314
  othercaps = gst_pad_peer_query_caps (jpegenc->srcpad, filter);
315
  if (othercaps == NULL ||
Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
316 317 318 319
      gst_caps_is_empty (othercaps) || gst_caps_is_any (othercaps)) {
    caps = gst_caps_copy (gst_pad_get_pad_template_caps (pad));
    goto done;
  }
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337

  caps = gst_caps_new_empty ();
  templ = gst_pad_get_pad_template_caps (pad);

  for (i = 0; i < gst_caps_get_size (templ); i++) {
    /* pick fields from peer caps */
    for (j = 0; j < gst_caps_get_size (othercaps); j++) {
      GstStructure *s = gst_caps_get_structure (othercaps, j);
      const GValue *val;

      structure = gst_structure_copy (gst_caps_get_structure (templ, i));
      if ((val = gst_structure_get_value (s, "width")))
        gst_structure_set_value (structure, "width", val);
      if ((val = gst_structure_get_value (s, "height")))
        gst_structure_set_value (structure, "height", val);
      if ((val = gst_structure_get_value (s, "framerate")))
        gst_structure_set_value (structure, "framerate", val);

Wim Taymans's avatar
Wim Taymans committed
338
      caps = gst_caps_merge_structure (caps, structure);
339
    }
340
  }
Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
341 342 343 344

done:

  gst_caps_replace (&othercaps, NULL);
345
  gst_object_unref (jpegenc);
346 347

  return caps;
348 349
}

Wim Taymans's avatar
Wim Taymans committed
350
static gboolean
Wim Taymans's avatar
Wim Taymans committed
351
gst_jpegenc_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
Wim Taymans's avatar
Wim Taymans committed
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
{
  gboolean res;

  switch (GST_QUERY_TYPE (query)) {
    case GST_QUERY_CAPS:
    {
      GstCaps *filter, *caps;

      gst_query_parse_caps (query, &filter);
      caps = gst_jpegenc_getcaps (pad, filter);
      gst_query_set_caps_result (query, caps);
      gst_caps_unref (caps);
      res = TRUE;
      break;
    }
    default:
Wim Taymans's avatar
Wim Taymans committed
368
      res = gst_pad_query_default (pad, parent, query);
Wim Taymans's avatar
Wim Taymans committed
369 370 371 372 373
      break;
  }
  return res;
}

374
static gboolean
Wim Taymans's avatar
Wim Taymans committed
375
gst_jpegenc_setcaps (GstJpegEnc * enc, GstCaps * caps)
376
{
Wim Taymans's avatar
Wim Taymans committed
377
  GstVideoInfo info;
378 379
  gint i;
  GstCaps *othercaps;
380
  gboolean ret;
Wim Taymans's avatar
Wim Taymans committed
381
  const GstVideoFormatInfo *vinfo;
382

383
  /* get info from caps */
Wim Taymans's avatar
Wim Taymans committed
384
  if (!gst_video_info_from_caps (&info, caps))
385 386 387
    goto refuse_caps;

  /* store input description */
Wim Taymans's avatar
Wim Taymans committed
388 389 390
  enc->info = info;

  vinfo = info.finfo;
391 392

  /* prepare a cached image description  */
Wim Taymans's avatar
Wim Taymans committed
393
  enc->channels = 3 + (GST_VIDEO_FORMAT_INFO_HAS_ALPHA (vinfo) ? 1 : 0);
394
  /* ... but any alpha is disregarded in encoding */
Wim Taymans's avatar
Wim Taymans committed
395
  if (GST_VIDEO_FORMAT_INFO_IS_GRAY (vinfo))
396 397 398
    enc->channels = 1;
  else
    enc->channels = 3;
Wim Taymans's avatar
Wim Taymans committed
399

400 401 402
  enc->h_max_samp = 0;
  enc->v_max_samp = 0;
  for (i = 0; i < enc->channels; ++i) {
Wim Taymans's avatar
Wim Taymans committed
403 404 405 406 407
    enc->cwidth[i] = GST_VIDEO_INFO_COMP_WIDTH (&info, i);
    enc->cheight[i] = GST_VIDEO_INFO_COMP_HEIGHT (&info, i);
    enc->inc[i] = GST_VIDEO_INFO_COMP_PSTRIDE (&info, i);

    enc->h_samp[i] = GST_ROUND_UP_4 (info.width) / enc->cwidth[i];
408
    enc->h_max_samp = MAX (enc->h_max_samp, enc->h_samp[i]);
Wim Taymans's avatar
Wim Taymans committed
409
    enc->v_samp[i] = GST_ROUND_UP_4 (info.height) / enc->cheight[i];
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
    enc->v_max_samp = MAX (enc->v_max_samp, enc->v_samp[i]);
  }
  /* samp should only be 1, 2 or 4 */
  g_assert (enc->h_max_samp <= 4);
  g_assert (enc->v_max_samp <= 4);
  /* now invert */
  /* maximum is invariant, as one of the components should have samp 1 */
  for (i = 0; i < enc->channels; ++i) {
    enc->h_samp[i] = enc->h_max_samp / enc->h_samp[i];
    enc->v_samp[i] = enc->v_max_samp / enc->v_samp[i];
  }
  enc->planar = (enc->inc[0] == 1 && enc->inc[1] == 1 && enc->inc[2] == 1);

  othercaps = gst_caps_copy (gst_pad_get_pad_template_caps (enc->srcpad));
  gst_caps_set_simple (othercaps,
Wim Taymans's avatar
Wim Taymans committed
425 426
      "width", G_TYPE_INT, info.width, "height", G_TYPE_INT, info.height, NULL);
  if (info.fps_d > 0)
427
    gst_caps_set_simple (othercaps,
Wim Taymans's avatar
Wim Taymans committed
428 429
        "framerate", GST_TYPE_FRACTION, info.fps_n, info.fps_d, NULL);
  if (info.par_d > 0)
430
    gst_caps_set_simple (othercaps,
Wim Taymans's avatar
Wim Taymans committed
431
        "pixel-aspect-ratio", GST_TYPE_FRACTION, info.par_n, info.par_d, NULL);
432 433 434

  ret = gst_pad_set_caps (enc->srcpad, othercaps);
  gst_caps_unref (othercaps);
435

Wim Taymans's avatar
Wim Taymans committed
436
  if (ret)
437
    gst_jpegenc_resync (enc);
438 439

  return ret;
440 441 442 443 444 445 446

  /* ERRORS */
refuse_caps:
  {
    GST_WARNING_OBJECT (enc, "refused caps %" GST_PTR_FORMAT, caps);
    return FALSE;
  }
447 448
}

Wim Taymans's avatar
Wim Taymans committed
449
static gboolean
Wim Taymans's avatar
Wim Taymans committed
450
gst_jpegenc_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
Wim Taymans's avatar
Wim Taymans committed
451 452
{
  gboolean res;
Wim Taymans's avatar
Wim Taymans committed
453
  GstJpegEnc *enc = GST_JPEGENC (parent);
Wim Taymans's avatar
Wim Taymans committed
454 455 456 457 458 459 460 461

  switch (GST_EVENT_TYPE (event)) {
    case GST_EVENT_CAPS:
    {
      GstCaps *caps;

      gst_event_parse_caps (event, &caps);
      res = gst_jpegenc_setcaps (enc, caps);
Wim Taymans's avatar
Wim Taymans committed
462
      gst_event_unref (event);
Wim Taymans's avatar
Wim Taymans committed
463 464 465
      break;
    }
    default:
Wim Taymans's avatar
Wim Taymans committed
466
      res = gst_pad_event_default (pad, parent, event);
Wim Taymans's avatar
Wim Taymans committed
467 468 469 470 471 472
      break;
  }

  return res;
}

473
static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
474
gst_jpegenc_resync (GstJpegEnc * jpegenc)
475 476
{
  gint width, height;
477
  gint i, j;
Wim Taymans's avatar
Wim Taymans committed
478
  const GstVideoFormatInfo *finfo;
479

480
  GST_DEBUG_OBJECT (jpegenc, "resync");
481

Wim Taymans's avatar
Wim Taymans committed
482 483 484 485
  finfo = jpegenc->info.finfo;

  jpegenc->cinfo.image_width = width = GST_VIDEO_INFO_WIDTH (&jpegenc->info);
  jpegenc->cinfo.image_height = height = GST_VIDEO_INFO_HEIGHT (&jpegenc->info);
486
  jpegenc->cinfo.input_components = jpegenc->channels;
487

488
  GST_DEBUG_OBJECT (jpegenc, "width %d, height %d", width, height);
Wim Taymans's avatar
Wim Taymans committed
489 490
  GST_DEBUG_OBJECT (jpegenc, "format %d",
      GST_VIDEO_INFO_FORMAT (&jpegenc->info));
491

Wim Taymans's avatar
Wim Taymans committed
492
  if (GST_VIDEO_FORMAT_INFO_IS_RGB (finfo)) {
493 494
    GST_DEBUG_OBJECT (jpegenc, "RGB");
    jpegenc->cinfo.in_color_space = JCS_RGB;
Wim Taymans's avatar
Wim Taymans committed
495
  } else if (GST_VIDEO_FORMAT_INFO_IS_GRAY (finfo)) {
496 497
    GST_DEBUG_OBJECT (jpegenc, "gray");
    jpegenc->cinfo.in_color_space = JCS_GRAYSCALE;
498 499 500 501
  } else {
    GST_DEBUG_OBJECT (jpegenc, "YUV");
    jpegenc->cinfo.in_color_space = JCS_YCbCr;
  }
502

503
  /* input buffer size as max output */
Wim Taymans's avatar
Wim Taymans committed
504
  jpegenc->bufsize = GST_VIDEO_INFO_SIZE (&jpegenc->info);
505 506 507 508 509 510 511 512 513
  jpeg_set_defaults (&jpegenc->cinfo);
  jpegenc->cinfo.raw_data_in = TRUE;
  /* duh, libjpeg maps RGB to YUV ... and don't expect some conversion */
  if (jpegenc->cinfo.in_color_space == JCS_RGB)
    jpeg_set_colorspace (&jpegenc->cinfo, JCS_RGB);

  GST_DEBUG_OBJECT (jpegenc, "h_max_samp=%d, v_max_samp=%d",
      jpegenc->h_max_samp, jpegenc->v_max_samp);
  /* image dimension info */
514
  for (i = 0; i < jpegenc->channels; i++) {
515 516 517 518
    GST_DEBUG_OBJECT (jpegenc, "comp %i: h_samp=%d, v_samp=%d", i,
        jpegenc->h_samp[i], jpegenc->v_samp[i]);
    jpegenc->cinfo.comp_info[i].h_samp_factor = jpegenc->h_samp[i];
    jpegenc->cinfo.comp_info[i].v_samp_factor = jpegenc->v_samp[i];
519 520
    g_free (jpegenc->line[i]);
    jpegenc->line[i] = g_new (guchar *, jpegenc->v_max_samp * DCTSIZE);
521 522
    if (!jpegenc->planar) {
      for (j = 0; j < jpegenc->v_max_samp * DCTSIZE; j++) {
523 524
        g_free (jpegenc->row[i][j]);
        jpegenc->row[i][j] = g_malloc (width);
525
        jpegenc->line[i][j] = jpegenc->row[i][j];
526
      }
527
    }
528 529
  }

530 531 532 533
  /* guard against a potential error in gst_jpegenc_term_destination
     which occurs iff bufsize % 4 < free_space_remaining */
  jpegenc->bufsize = GST_ROUND_UP_4 (jpegenc->bufsize);

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
534
  jpeg_suppress_tables (&jpegenc->cinfo, TRUE);
535

536
  GST_DEBUG_OBJECT (jpegenc, "resync done");
537 538
}

539
static GstFlowReturn
Wim Taymans's avatar
Wim Taymans committed
540
gst_jpegenc_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
541
{
542
  GstFlowReturn ret;
543
  GstJpegEnc *jpegenc;
544
  guint height;
545
  guchar *base[3], *end[3];
Wim Taymans's avatar
Wim Taymans committed
546
  guint stride[3];
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
547
  gint i, j, k;
Wim Taymans's avatar
Wim Taymans committed
548 549
  GstBuffer *outbuf;
  GstVideoFrame frame;
Wim Taymans's avatar
Wim Taymans committed
550
  static GstAllocationParams params = { 0, 0, 0, 3, };
551

Wim Taymans's avatar
Wim Taymans committed
552
  jpegenc = GST_JPEGENC (parent);
553

Wim Taymans's avatar
Wim Taymans committed
554 555
  if (G_UNLIKELY (GST_VIDEO_INFO_FORMAT (&jpegenc->info) ==
          GST_VIDEO_FORMAT_UNKNOWN))
556 557
    goto not_negotiated;

Wim Taymans's avatar
Wim Taymans committed
558 559
  if (!gst_video_frame_map (&frame, &jpegenc->info, buf, GST_MAP_READ))
    goto invalid_frame;
560

Wim Taymans's avatar
Wim Taymans committed
561
  height = GST_VIDEO_FRAME_HEIGHT (&frame);
562

Wim Taymans's avatar
Wim Taymans committed
563 564
  GST_LOG_OBJECT (jpegenc, "got buffer of %lu bytes",
      gst_buffer_get_size (buf));
565

566
  for (i = 0; i < jpegenc->channels; i++) {
Wim Taymans's avatar
Wim Taymans committed
567 568 569
    base[i] = GST_VIDEO_FRAME_COMP_DATA (&frame, i);
    stride[i] = GST_VIDEO_FRAME_COMP_STRIDE (&frame, i);
    end[i] = base[i] + GST_VIDEO_FRAME_COMP_HEIGHT (&frame, i) * stride[i];
570
  }
571

Wim Taymans's avatar
Wim Taymans committed
572
  jpegenc->output_mem = gst_allocator_alloc (NULL, jpegenc->bufsize, &params);
Wim Taymans's avatar
Wim Taymans committed
573
  gst_memory_map (jpegenc->output_mem, &jpegenc->output_map, GST_MAP_READWRITE);
Wim Taymans's avatar
Wim Taymans committed
574

Wim Taymans's avatar
Wim Taymans committed
575 576
  jpegenc->jdest.next_output_byte = jpegenc->output_map.data;
  jpegenc->jdest.free_in_buffer = jpegenc->output_map.size;
577

578 579 580 581
  /* prepare for raw input */
#if JPEG_LIB_VERSION >= 70
  jpegenc->cinfo.do_fancy_downsampling = FALSE;
#endif
582
  jpegenc->cinfo.smoothing_factor = jpegenc->smoothing;
Stefan Kost's avatar
Stefan Kost committed
583
  jpegenc->cinfo.dct_method = jpegenc->idct_method;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
584 585
  jpeg_set_quality (&jpegenc->cinfo, jpegenc->quality, TRUE);
  jpeg_start_compress (&jpegenc->cinfo, TRUE);
586

Stefan Kost's avatar
Stefan Kost committed
587
  GST_LOG_OBJECT (jpegenc, "compressing");
588

589 590 591 592 593
  if (jpegenc->planar) {
    for (i = 0; i < height; i += jpegenc->v_max_samp * DCTSIZE) {
      for (k = 0; k < jpegenc->channels; k++) {
        for (j = 0; j < jpegenc->v_samp[k] * DCTSIZE; j++) {
          jpegenc->line[k][j] = base[k];
Wim Taymans's avatar
Wim Taymans committed
594 595
          if (base[k] + stride[k] < end[k])
            base[k] += stride[k];
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
        }
      }
      jpeg_write_raw_data (&jpegenc->cinfo, jpegenc->line,
          jpegenc->v_max_samp * DCTSIZE);
    }
  } else {
    for (i = 0; i < height; i += jpegenc->v_max_samp * DCTSIZE) {
      for (k = 0; k < jpegenc->channels; k++) {
        for (j = 0; j < jpegenc->v_samp[k] * DCTSIZE; j++) {
          guchar *src, *dst;
          gint l;

          /* ouch, copy line */
          src = base[k];
          dst = jpegenc->line[k][j];
          for (l = jpegenc->cwidth[k]; l > 0; l--) {
            *dst = *src;
            src += jpegenc->inc[k];
            dst++;
          }
Wim Taymans's avatar
Wim Taymans committed
616 617
          if (base[k] + stride[k] < end[k])
            base[k] += stride[k];
618 619 620 621
        }
      }
      jpeg_write_raw_data (&jpegenc->cinfo, jpegenc->line,
          jpegenc->v_max_samp * DCTSIZE);
622 623
    }
  }
Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
624

Wim Taymans's avatar
Wim Taymans committed
625
  /* This will ensure that gst_jpegenc_term_destination is called */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
626
  jpeg_finish_compress (&jpegenc->cinfo);
Stefan Kost's avatar
Stefan Kost committed
627
  GST_LOG_OBJECT (jpegenc, "compressing done");
628

Wim Taymans's avatar
Wim Taymans committed
629 630
  outbuf = gst_buffer_new ();
  gst_buffer_copy_into (outbuf, buf, GST_BUFFER_COPY_METADATA, 0, -1);
Wim Taymans's avatar
Wim Taymans committed
631
  gst_buffer_append_memory (outbuf, jpegenc->output_mem);
Wim Taymans's avatar
Wim Taymans committed
632 633 634 635 636
  jpegenc->output_mem = NULL;

  ret = gst_pad_push (jpegenc->srcpad, outbuf);

  gst_video_frame_unmap (&frame);
637
  gst_buffer_unref (buf);
638

639
  return ret;
640

Wim Taymans's avatar
Wim Taymans committed
641
  /* ERRORS */
642 643 644
not_negotiated:
  {
    GST_WARNING_OBJECT (jpegenc, "no input format set (no caps on buffer)");
Wim Taymans's avatar
Wim Taymans committed
645 646 647 648 649 650 651 652
    gst_buffer_unref (buf);
    return GST_FLOW_NOT_NEGOTIATED;
  }
invalid_frame:
  {
    GST_WARNING_OBJECT (jpegenc, "invalid frame received");
    gst_buffer_unref (buf);
    return GST_FLOW_OK;
653
  }
654
}
655 656 657 658 659

static void
gst_jpegenc_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec)
{
660
  GstJpegEnc *jpegenc = GST_JPEGENC (object);
661

662
  GST_OBJECT_LOCK (jpegenc);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
663

664
  switch (prop_id) {
Stefan Kost's avatar
Stefan Kost committed
665
    case PROP_QUALITY:
666 667
      jpegenc->quality = g_value_get_int (value);
      break;
Benjamin Otte's avatar
Benjamin Otte committed
668
#ifdef ENABLE_SMOOTHING
Stefan Kost's avatar
Stefan Kost committed
669
    case PROP_SMOOTHING:
670 671
      jpegenc->smoothing = g_value_get_int (value);
      break;
Stefan Kost's avatar
Stefan Kost committed
672 673 674 675
#endif
    case PROP_IDCT_METHOD:
      jpegenc->idct_method = g_value_get_enum (value);
      break;
676
    default:
677
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
678 679
      break;
  }
680 681

  GST_OBJECT_UNLOCK (jpegenc);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
682
}
683 684 685 686 687

static void
gst_jpegenc_get_property (GObject * object, guint prop_id, GValue * value,
    GParamSpec * pspec)
{
688
  GstJpegEnc *jpegenc = GST_JPEGENC (object);
689

690
  GST_OBJECT_LOCK (jpegenc);
691 692

  switch (prop_id) {
Stefan Kost's avatar
Stefan Kost committed
693
    case PROP_QUALITY:
694 695
      g_value_set_int (value, jpegenc->quality);
      break;
Benjamin Otte's avatar
Benjamin Otte committed
696
#ifdef ENABLE_SMOOTHING
Stefan Kost's avatar
Stefan Kost committed
697
    case PROP_SMOOTHING:
698 699
      g_value_set_int (value, jpegenc->smoothing);
      break;
Stefan Kost's avatar
Stefan Kost committed
700 701 702 703
#endif
    case PROP_IDCT_METHOD:
      g_value_set_enum (value, jpegenc->idct_method);
      break;
704 705 706 707
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
708 709

  GST_OBJECT_UNLOCK (jpegenc);
710
}
711

712 713
static GstStateChangeReturn
gst_jpegenc_change_state (GstElement * element, GstStateChange transition)
714
{
715
  GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
716 717
  GstJpegEnc *filter = GST_JPEGENC (element);

718 719
  switch (transition) {
    case GST_STATE_CHANGE_NULL_TO_READY:
720
      GST_DEBUG_OBJECT (element, "setting line buffers");
721 722 723 724
      filter->line[0] = NULL;
      filter->line[1] = NULL;
      filter->line[2] = NULL;
      break;
725 726 727 728 729 730 731 732 733
    default:
      break;
  }

  ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
  if (ret == GST_STATE_CHANGE_FAILURE)
    return ret;

  switch (transition) {
734 735
    case GST_STATE_CHANGE_PAUSED_TO_READY:
      gst_jpegenc_reset (filter);
736 737 738 739 740
      break;
    default:
      break;
  }

741
  return ret;
742
}