gstffmpegcolorspace.c 14 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/* GStreamer
 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
 * This file:
 * Copyright (C) 2003 Ronald Bultje <rbultje@ronald.bitfreak.net>
 *
 * 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.
 */

22
23
24
25
26
27
28
29
30
31
32
33
34
/**
 * SECTION:element-ffmpegcolorspace
 *
 * <refsect2>
 * <title>Example launch line</title>
 * <para>
 * <programlisting>
 * gst-launch -v videotestsrc ! video/x-raw-yuv,format=\(fourcc\)YUY2 ! ffmpegcolorspace ! ximagesink
 * </programlisting>
 * </para>
 * </refsect2>
 */

35
#ifdef HAVE_CONFIG_H
36
#  include "config.h"
37
38
#endif

39
#include "gstffmpegcolorspace.h"
40
41
#include "gstffmpegcodecmap.h"

42
GST_DEBUG_CATEGORY_STATIC (ffmpegcolorspace_debug);
43
44
#define GST_CAT_DEFAULT ffmpegcolorspace_debug

45
/* elementfactory information */
Stefan Kost's avatar
Stefan Kost committed
46
static const GstElementDetails ffmpegcsp_details =
47
48
49
50
GST_ELEMENT_DETAILS ("FFMPEG Colorspace converter",
    "Filter/Converter/Video",
    "Converts video from one colorspace to another",
    "Ronald Bultje <rbultje@ronald.bitfreak.net>");
51
52
53
54
55
56
57
58
59
60
61


/* Stereo signals and args */
enum
{
  /* FILL ME */
  LAST_SIGNAL
};

enum
{
62
  ARG_0,
63
64
};

65
static GType gst_ffmpegcsp_get_type (void);
66

67
68
69
static void gst_ffmpegcsp_base_init (GstFFMpegCspClass * klass);
static void gst_ffmpegcsp_class_init (GstFFMpegCspClass * klass);
static void gst_ffmpegcsp_init (GstFFMpegCsp * space);
70

71
72
static gboolean gst_ffmpegcsp_set_caps (GstBaseTransform * btrans,
    GstCaps * incaps, GstCaps * outcaps);
73
74
75
76
static gboolean gst_ffmpegcsp_get_unit_size (GstBaseTransform * btrans,
    GstCaps * caps, guint * size);
static GstFlowReturn gst_ffmpegcsp_transform (GstBaseTransform * btrans,
    GstBuffer * inbuf, GstBuffer * outbuf);
77
#if 0
78
79
static GstFlowReturn gst_ffmpegcsp_transform_ip (GstBaseTransform * btrans,
    GstBuffer * inbuf);
80
#endif
81

82
static GstPadTemplate *sinktempl, *srctempl;
83
84
static GstElementClass *parent_class = NULL;

85
/*static guint gst_ffmpegcsp_signals[LAST_SIGNAL] = { 0 }; */
86

87
/* copies the given caps */
88
static GstCaps *
89
gst_ffmpegcsp_caps_remove_format_info (GstCaps * caps)
90
91
92
93
{
  int i;
  GstStructure *structure;
  GstCaps *rgbcaps;
94
  GstCaps *graycaps;
95

96
  caps = gst_caps_copy (caps);
97

98
99
100
101
102
103
104
105
106
107
108
  for (i = 0; i < gst_caps_get_size (caps); i++) {
    structure = gst_caps_get_structure (caps, i);

    gst_structure_set_name (structure, "video/x-raw-yuv");
    gst_structure_remove_field (structure, "format");
    gst_structure_remove_field (structure, "endianness");
    gst_structure_remove_field (structure, "depth");
    gst_structure_remove_field (structure, "bpp");
    gst_structure_remove_field (structure, "red_mask");
    gst_structure_remove_field (structure, "green_mask");
    gst_structure_remove_field (structure, "blue_mask");
109
    gst_structure_remove_field (structure, "alpha_mask");
110
    gst_structure_remove_field (structure, "palette_data");
111
112
  }

113
114
  gst_caps_do_simplify (caps);
  rgbcaps = gst_caps_copy (caps);
115
116
117
118
119
120

  for (i = 0; i < gst_caps_get_size (rgbcaps); i++) {
    structure = gst_caps_get_structure (rgbcaps, i);

    gst_structure_set_name (structure, "video/x-raw-rgb");
  }
121
  graycaps = gst_caps_copy (caps);
122

123
124
125
126
127
128
129
  for (i = 0; i < gst_caps_get_size (graycaps); i++) {
    structure = gst_caps_get_structure (graycaps, i);

    gst_structure_set_name (structure, "video/x-raw-gray");
  }

  gst_caps_append (caps, graycaps);
130
131
132
133
134
  gst_caps_append (caps, rgbcaps);

  return caps;
}

135
136
137
/* The caps can be transformed into any other caps with format info removed.
 * However, we should prefer passthrough, so if passthrough is possible,
 * put it first in the list. */
138
static GstCaps *
139
140
gst_ffmpegcsp_transform_caps (GstBaseTransform * btrans,
    GstPadDirection direction, GstCaps * caps)
141
{
142
  GstFFMpegCsp *space;
143
  GstCaps *template;
144
  GstCaps *result;
145

146
147
  space = GST_FFMPEGCSP (btrans);

148
149
  template = gst_ffmpegcsp_codectype_to_caps (CODEC_TYPE_VIDEO, NULL);
  result = gst_caps_intersect (caps, template);
150
  gst_caps_unref (template);
151

152
153
154
155
  gst_caps_append (result, gst_ffmpegcsp_caps_remove_format_info (caps));

  GST_DEBUG_OBJECT (btrans, "transformed %" GST_PTR_FORMAT " into %"
      GST_PTR_FORMAT, caps, result);
156

157
  return result;
158
159
}

160
static gboolean
161
162
gst_ffmpegcsp_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
    GstCaps * outcaps)
163
{
164
  GstFFMpegCsp *space;
165
166
167
  GstStructure *structure;
  gint in_height, in_width;
  gint out_height, out_width;
168
169
  const GValue *in_framerate = NULL;
  const GValue *out_framerate = NULL;
170
171
172
  const GValue *in_par = NULL;
  const GValue *out_par = NULL;
  AVCodecContext *ctx;
173
  gboolean res;
174
175
176
177
178

  space = GST_FFMPEGCSP (btrans);

  /* parse in and output values */
  structure = gst_caps_get_structure (incaps, 0);
179
180
181
182
183
184
185

  /* we have to have width and height */
  res = gst_structure_get_int (structure, "width", &in_width);
  res &= gst_structure_get_int (structure, "height", &in_height);
  if (!res)
    goto no_width_height;

186
187
188
189
190
  /* and framerate */
  in_framerate = gst_structure_get_value (structure, "framerate");
  if (in_framerate == NULL || !GST_VALUE_HOLDS_FRACTION (in_framerate))
    goto no_framerate;

191
  /* this is optional */
192
193
194
  in_par = gst_structure_get_value (structure, "pixel-aspect-ratio");

  structure = gst_caps_get_structure (outcaps, 0);
195
196
197
198
199
200
201

  /* we have to have width and height */
  res = gst_structure_get_int (structure, "width", &out_width);
  res &= gst_structure_get_int (structure, "height", &out_height);
  if (!res)
    goto no_width_height;

202
203
204
205
206
  /* and framerate */
  out_framerate = gst_structure_get_value (structure, "framerate");
  if (out_framerate == NULL || !GST_VALUE_HOLDS_FRACTION (out_framerate))
    goto no_framerate;

207
  /* this is optional */
208
  out_par = gst_structure_get_value (structure, "pixel-aspect-ratio");
209

210
  /* these must match */
211
  if (in_width != out_width || in_height != out_height ||
212
      gst_value_compare (in_framerate, out_framerate) != GST_VALUE_EQUAL)
213
214
    goto format_mismatch;

215
  /* if present, these must match too */
216
217
218
  if (in_par && out_par
      && gst_value_compare (in_par, out_par) != GST_VALUE_EQUAL)
    goto format_mismatch;
219

220
221
  ctx = avcodec_alloc_context ();

222
223
224
225
  space->width = ctx->width = in_width;
  space->height = ctx->height = in_height;

  /* get from format */
226
  ctx->pix_fmt = PIX_FMT_NB;
227
228
229
230
  gst_ffmpegcsp_caps_with_codectype (CODEC_TYPE_VIDEO, incaps, ctx);
  if (ctx->pix_fmt == PIX_FMT_NB)
    goto invalid_in_caps;
  space->from_pixfmt = ctx->pix_fmt;
231

232
233
234
235
  /* palette, only for from data */
  if (space->palette)
    av_free (space->palette);
  space->palette = ctx->palctrl;
236
  ctx->palctrl = NULL;
237

238
239
240
241
242
243
  /* get to format */
  ctx->pix_fmt = PIX_FMT_NB;
  gst_ffmpegcsp_caps_with_codectype (CODEC_TYPE_VIDEO, outcaps, ctx);
  if (ctx->pix_fmt == PIX_FMT_NB)
    goto invalid_out_caps;
  space->to_pixfmt = ctx->pix_fmt;
244

245
  GST_DEBUG ("reconfigured %d %d", space->from_pixfmt, space->to_pixfmt);
246

247
  av_free (ctx);
248

249
  return TRUE;
250

251
  /* ERRORS */
252
253
no_width_height:
  {
254
    GST_DEBUG_OBJECT (space, "did not specify width or height");
255
256
257
258
    space->from_pixfmt = PIX_FMT_NB;
    space->to_pixfmt = PIX_FMT_NB;
    return FALSE;
  }
259
260
no_framerate:
  {
261
    GST_DEBUG_OBJECT (space, "did not specify framerate");
262
263
264
265
    space->from_pixfmt = PIX_FMT_NB;
    space->to_pixfmt = PIX_FMT_NB;
    return FALSE;
  }
266
format_mismatch:
Wim Taymans's avatar
Wim Taymans committed
267
  {
268
    GST_DEBUG_OBJECT (space, "input and output formats do not match");
269
270
    space->from_pixfmt = PIX_FMT_NB;
    space->to_pixfmt = PIX_FMT_NB;
Wim Taymans's avatar
Wim Taymans committed
271
272
    return FALSE;
  }
273
invalid_in_caps:
274
  {
275
    GST_DEBUG_OBJECT (space, "could not configure context for input format");
276
277
278
279
280
281
282
    av_free (ctx);
    space->from_pixfmt = PIX_FMT_NB;
    space->to_pixfmt = PIX_FMT_NB;
    return FALSE;
  }
invalid_out_caps:
  {
283
    GST_DEBUG_OBJECT (space, "could not configure context for output format");
284
285
286
    av_free (ctx);
    space->from_pixfmt = PIX_FMT_NB;
    space->to_pixfmt = PIX_FMT_NB;
287
288
    return FALSE;
  }
289
290
291
}

static GType
292
gst_ffmpegcsp_get_type (void)
293
{
294
  static GType ffmpegcsp_type = 0;
295

296
297
298
299
  if (!ffmpegcsp_type) {
    static const GTypeInfo ffmpegcsp_info = {
      sizeof (GstFFMpegCspClass),
      (GBaseInitFunc) gst_ffmpegcsp_base_init,
300
      NULL,
301
      (GClassInitFunc) gst_ffmpegcsp_class_init,
302
303
      NULL,
      NULL,
304
      sizeof (GstFFMpegCsp),
305
      0,
306
      (GInstanceInitFunc) gst_ffmpegcsp_init,
307
308
    };

309
    ffmpegcsp_type = g_type_register_static (GST_TYPE_BASE_TRANSFORM,
310
        "GstFFMpegCsp", &ffmpegcsp_info, 0);
311
312
  }

313
  return ffmpegcsp_type;
314
315
316
}

static void
317
gst_ffmpegcsp_base_init (GstFFMpegCspClass * klass)
318
319
320
321
322
{
  GstElementClass *element_class = GST_ELEMENT_CLASS (klass);

  gst_element_class_add_pad_template (element_class, srctempl);
  gst_element_class_add_pad_template (element_class, sinktempl);
323
  gst_element_class_set_details (element_class, &ffmpegcsp_details);
324
325
}

326
327
328
329
330
331
332
333
334
335
336
static void
gst_ffmpegcsp_finalize (GObject * obj)
{
  GstFFMpegCsp *space = GST_FFMPEGCSP (obj);

  if (space->palette)
    av_free (space->palette);

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

337
static void
338
gst_ffmpegcsp_class_init (GstFFMpegCspClass * klass)
339
340
341
{
  GObjectClass *gobject_class;
  GstElementClass *gstelement_class;
342
  GstBaseTransformClass *gstbasetransform_class;
343
344
345

  gobject_class = (GObjectClass *) klass;
  gstelement_class = (GstElementClass *) klass;
346
  gstbasetransform_class = (GstBaseTransformClass *) klass;
347

348
349
350
  parent_class = g_type_class_peek_parent (klass);

  gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_ffmpegcsp_finalize);
351

352
353
354
355
356
357
358
  gstbasetransform_class->transform_caps =
      GST_DEBUG_FUNCPTR (gst_ffmpegcsp_transform_caps);
  gstbasetransform_class->set_caps = GST_DEBUG_FUNCPTR (gst_ffmpegcsp_set_caps);
  gstbasetransform_class->get_unit_size =
      GST_DEBUG_FUNCPTR (gst_ffmpegcsp_get_unit_size);
  gstbasetransform_class->transform =
      GST_DEBUG_FUNCPTR (gst_ffmpegcsp_transform);
359
#if 0
360
361
  gstbasetransform_class->transform_ip =
      GST_DEBUG_FUNCPTR (gst_ffmpegcsp_transform_ip);
362
363
364
#endif

  gstbasetransform_class->passthrough_on_same_caps = TRUE;
365
366
367

  GST_DEBUG_CATEGORY_INIT (ffmpegcolorspace_debug, "ffmpegcolorspace", 0,
      "FFMPEG-based colorspace converter");
368
369
370
}

static void
371
gst_ffmpegcsp_init (GstFFMpegCsp * space)
372
{
373
  gst_base_transform_set_qos_enabled (GST_BASE_TRANSFORM (space), TRUE);
374
  space->from_pixfmt = space->to_pixfmt = PIX_FMT_NB;
375
  space->palette = NULL;
376
377
}

378
379
380
static gboolean
gst_ffmpegcsp_get_unit_size (GstBaseTransform * btrans, GstCaps * caps,
    guint * size)
Wim Taymans's avatar
Wim Taymans committed
381
{
382
383
384
385
  GstFFMpegCsp *space = NULL;
  GstStructure *structure = NULL;
  AVCodecContext *ctx = NULL;
  gint width, height;
386

387
  g_assert (size);
Wim Taymans's avatar
Wim Taymans committed
388

389
  space = GST_FFMPEGCSP (btrans);
390
391
392
393
394
395
396
397
398
399
400
401
402

  structure = gst_caps_get_structure (caps, 0);
  gst_structure_get_int (structure, "width", &width);
  gst_structure_get_int (structure, "height", &height);

  ctx = avcodec_alloc_context ();

  g_assert (ctx != NULL);

  gst_ffmpegcsp_caps_with_codectype (CODEC_TYPE_VIDEO, caps, ctx);

  *size = avpicture_get_size (ctx->pix_fmt, width, height);

403
404
405
406
407
408
409
  /* ffmpeg frames have the palette after the frame data, whereas
   * GStreamer currently puts it into the caps as 'palette_data' field,
   * so for paletted data the frame size avpicture_get_size() returns is
   * 1024 bytes larger than what GStreamer expects. */
  if (gst_structure_has_field (structure, "palette_data")) {
    *size -= 4 * 256;           /* = AVPALETTE_SIZE */
  }
410

411
412
  if (ctx->palctrl)
    av_free (ctx->palctrl);
413
  av_free (ctx);
Wim Taymans's avatar
Wim Taymans committed
414

415
  return TRUE;
416
417
}

418
419
#if 0
/* FIXME: Could use transform_ip to implement endianness swap type operations */
420
421
422
423
424
static GstFlowReturn
gst_ffmpegcsp_transform_ip (GstBaseTransform * btrans, GstBuffer * inbuf)
{
  /* do nothing */
  return GST_FLOW_OK;
Wim Taymans's avatar
Wim Taymans committed
425
}
426
#endif
Wim Taymans's avatar
Wim Taymans committed
427

428
static GstFlowReturn
429
430
gst_ffmpegcsp_transform (GstBaseTransform * btrans, GstBuffer * inbuf,
    GstBuffer * outbuf)
431
{
432
  GstFFMpegCsp *space;
433
  gint result;
434

435
  space = GST_FFMPEGCSP (btrans);
436

437
  GST_DEBUG ("from %d -> to %d", space->from_pixfmt, space->to_pixfmt);
Wim Taymans's avatar
Wim Taymans committed
438
  if (space->from_pixfmt == PIX_FMT_NB || space->to_pixfmt == PIX_FMT_NB)
439
    goto unknown_format;
440

441
442
443
444
445
446
  /* fill from with source data */
  gst_ffmpegcsp_avpicture_fill (&space->from_frame,
      GST_BUFFER_DATA (inbuf), space->from_pixfmt, space->width, space->height);

  /* fill optional palette */
  if (space->palette)
447
    space->from_frame.data[1] = (uint8_t *) space->palette->palette;
448
449
450
451

  /* fill target frame */
  gst_ffmpegcsp_avpicture_fill (&space->to_frame,
      GST_BUFFER_DATA (outbuf), space->to_pixfmt, space->width, space->height);
452

453
  /* and convert */
454
  result = img_convert (&space->to_frame, space->to_pixfmt,
455
      &space->from_frame, space->from_pixfmt, space->width, space->height);
456
457
  if (result == -1)
    goto not_supported;
Wim Taymans's avatar
Wim Taymans committed
458

459
460
461
462
463
  /* copy timestamps */
  gst_buffer_stamp (outbuf, inbuf);
  GST_DEBUG ("from %d -> to %d done", space->from_pixfmt, space->to_pixfmt);

  return GST_FLOW_OK;
Wim Taymans's avatar
Wim Taymans committed
464
465

  /* ERRORS */
466
unknown_format:
Wim Taymans's avatar
Wim Taymans committed
467
468
469
470
471
  {
    GST_ELEMENT_ERROR (space, CORE, NOT_IMPLEMENTED, (NULL),
        ("attempting to convert colorspaces between unknown formats"));
    return GST_FLOW_NOT_NEGOTIATED;
  }
472
473
474
475
476
477
not_supported:
  {
    GST_ELEMENT_ERROR (space, CORE, NOT_IMPLEMENTED, (NULL),
        ("cannot convert between formats"));
    return GST_FLOW_NOT_SUPPORTED;
  }
478
479
480
481
482
483
484
485
}

gboolean
gst_ffmpegcolorspace_register (GstPlugin * plugin)
{
  GstCaps *caps;

  /* template caps */
486
  caps = gst_ffmpegcsp_codectype_to_caps (CODEC_TYPE_VIDEO, NULL);
487
488
489
490

  /* build templates */
  srctempl = gst_pad_template_new ("src",
      GST_PAD_SRC, GST_PAD_ALWAYS, gst_caps_copy (caps));
491
492

  /* the sink template will do palette handling as well... */
493
494
495
  sinktempl = gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, caps);

  return gst_element_register (plugin, "ffmpegcolorspace",
496
      GST_RANK_NONE, GST_TYPE_FFMPEGCSP);
497
}