summaryrefslogtreecommitdiff
path: root/gst-libs/gst/cairo/gstcairoformat.c
blob: 17271436333460e838eaaccb69ccbf15c994549e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/* GStreamer
 * Copyright (C) 2009 Benjamin Otte <otte@gnome.org>
 *
 * 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.
 */

#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif

#include "gstcairoformat.h"
#include "gstcairoformat-private.h"

#include <string.h>
#include <pixman.h>

#include "gstcairocaps.h"
#include "gstcairocapsany.h"

/* huge map of pixman format => caps */
struct
{
  pixman_format_code_t format;
  GstVideoFormat video_format;
  GstStaticCaps caps;
} formatcaps[] = {
#define GST_CAIRO_CAPS_FORMAT(f, vf, c) { f, vf, GST_STATIC_CAPS (c) },
#include "gstcairocaps2format.h"
#undef GST_CAIRO_CAPS_FORMAT
  /* necessary because otherwise we'd get a comma at the end of an array */
  {
  0,}
};

/**
 * GstCairoFormat:
 *
 * Opaque structure representing a fixed #GstCaps that gst-plugins-cairo can
 * accept. It is use instead of caps because it is more convenient and 
 * provides useful utility functions.
 */

/**
 * GstCairoFormatOption:
 * @GST_CAIRO_FORMAT_FORMAT: operate on colorspaces
 * @GST_CAIRO_FORMAT_WIDTH: operate on width
 * @GST_CAIRO_FORMAT_HEIGHT: operate on height
 * @GST_CAIRO_FORMAT_SIZE: operate on width and height - this option is
 *                         equivalent to listing width and height separately
 * @GST_CAIRO_FORMAT_FRAMERATE: operate on framerate
 * @GST_CAIRO_FORMAT_PAR: operate on pixel aspect ratio
 *
 * Some functions operating on #GstCairoFormat or #GstCaps allow specifying
 * on which parts to operate. This enumeration is used to specify that.
 */

static void
get_format_for_caps (GstCairoFormat * format, GstCaps * caps)
{
  guint i;
  GstCaps *current;
  gboolean can_intersect;

  for (i = 0; i == 0 || formatcaps[i].format != 0; i++) {
    current = gst_static_caps_get (&formatcaps[i].caps);
    can_intersect = gst_caps_can_intersect (current, caps);
    gst_caps_unref (current);
    if (can_intersect) {
      format->format = formatcaps[i].format;
      format->video_format = formatcaps[i].video_format;
      return;
    }
  }
  g_assert_not_reached ();
}

/**
 * gst_cairo_format_new:
 * @caps: caps to parse. The caps must be fixed and valid cairo caps.
 *
 * Parses the fixed @caps into a #GstCairoFormat for easy use during 
 * operation like when using buffers in the given caps.
 *
 * Returns: a new #GstCairoFormat, free with gst_cairo_format_free()
 **/
GstCairoFormat *
gst_cairo_format_new (GstCaps * caps)
{
  GstStructure *structure;
  GstCairoFormat *format;

  g_return_val_if_fail (GST_IS_CAPS (caps), NULL);
  g_return_val_if_fail (gst_caps_is_fixed (caps), NULL);

  format = g_slice_new (GstCairoFormat);
  format->refcount = 1;

  structure = gst_caps_get_structure (caps, 0);

  get_format_for_caps (format, caps);

  if (!gst_structure_get_int (structure, "width", &format->width) ||
      !gst_structure_get_int (structure, "height", &format->height) ||
      !gst_structure_get_fraction (structure, "framerate",
          &format->framerate.num, &format->framerate.denom)) {
    /* It's an application error to pass unparsable caps */
    g_return_val_if_reached (NULL);
  }

  if (!gst_structure_get_fraction (structure, "pixel-aspect-ratio",
          &format->par.num, &format->par.denom)) {
    format->par.num = 0;
    format->par.denom = 0;
  }

  return format;
}

/**
 * gst_cairo_format_copy:
 * @format: the format to copy
 *
 * Creates a copy for the given @format.
 *
 * Returns: a copy of the given @format
 **/
GstCairoFormat *
gst_cairo_format_copy (const GstCairoFormat * format)
{
  GstCairoFormat *copy;

  g_return_val_if_fail (format != NULL, NULL);

  copy = (GstCairoFormat *) format;
  g_atomic_int_inc ((int *) &copy->refcount);

  return copy;
}

/**
 * gst_cairo_format_free:
 * @format: the format to free or %NULL
 *
 * Frees the given @format structure.
 **/
void
gst_cairo_format_free (GstCairoFormat * format)
{
  if (format == NULL)
    return;

  if (!g_atomic_int_dec_and_test ((int *) &format->refcount))
    return;

  g_slice_free (GstCairoFormat, format);
}

/**
 * gst_cairo_format_is_native:
 * @format: a format
 *
 * Checks if the given @format is native. Native formats accept 
 * buffers created using gst_cairo_buffer_new() that allow carrying
 * any #cairo_surface_t.
 *
 * Returns: %TRUE if the format is native
 **/
gboolean
gst_cairo_format_is_native (const GstCairoFormat * format)
{
  return format->format == 0;
}

/**
 * gst_cairo_format_to_caps:
 * @format: the format
 * @fixed: parts of the format to respect
 *
 * Creates a #GstCaps that is as permissible as possible while 
 * having fixed the parts of @format that are in @fixed.
 *
 * Returns: a new #GstCaps conforming to the given @format
 **/
GstCaps *
gst_cairo_format_to_caps (const GstCairoFormat * format,
    GstCairoFormatOption fixed)
{
  GstCaps *caps;

  if (fixed & GST_CAIRO_FORMAT_FORMAT) {
    caps = gst_cairo_caps_any ();
  } else {
    guint i;

    caps = NULL;
    for (i = 0; i == 0 && formatcaps[i].format != 0; i++) {
      if (format->format == formatcaps[i].format) {
        caps = gst_static_caps_get (&formatcaps[i].caps);
        break;
      }
    }
    g_assert (caps != NULL);
  }

  caps = gst_caps_make_writable (caps);

  if (fixed & GST_CAIRO_FORMAT_WIDTH)
    gst_caps_set_simple (caps, "width", G_TYPE_INT, format->width, NULL);
  if (fixed & GST_CAIRO_FORMAT_HEIGHT)
    gst_caps_set_simple (caps, "height", G_TYPE_INT, format->height, NULL);
  if (fixed & GST_CAIRO_FORMAT_FRAMERATE)
    gst_caps_set_simple (caps, "framerate", GST_TYPE_FRACTION,
        format->framerate.num, format->framerate.denom, NULL);
  if (fixed & GST_CAIRO_FORMAT_PAR && format->par.denom != 0)
    gst_caps_set_simple (caps, "pixel-aspect-ratio", GST_TYPE_FRACTION,
        format->par.num, format->par.denom, NULL);

  return caps;
}

/**
 * gst_cairo_format_get_width:
 * @format: a format
 *
 * Queries the width of a given format
 *
 * Returns: the width in pixels
 **/
guint
gst_cairo_format_get_width (const GstCairoFormat * format)
{
  g_return_val_if_fail (format != NULL, 0);

  return format->width;
}

/**
 * gst_cairo_format_get_height:
 * @format: a format
 *
 * Queries the height of a given format.
 *
 * Returns: the height in pixels
 **/
guint
gst_cairo_format_get_height (const GstCairoFormat * format)
{
  g_return_val_if_fail (format != NULL, 0);

  return format->height;
}

/**
 * gst_cairo_format_get_framerate:
 * @format: a format
 * @numerator: value to store the framerate's numerator
 * @denominator: value to store the framerate's denominator
 *
 * Queries the framerate of the given @format and stores it in the given 
 * values.
 **/
void
gst_cairo_format_get_framerate (const GstCairoFormat * format,
    guint * numerator, guint * denominator)
{
  g_return_if_fail (format != NULL);

  if (numerator)
    *numerator = format->framerate.num;
  if (denominator)
    *denominator = format->framerate.denom;
}

/**
 * gst_cairo_format_get_pixel_aspect_ratio:
 * @format: format to query
 * @numerator: location to store numerator of aspect ratio or %NULL
 * @denominator: location to store denominator of aspect ratio %NULL
 *
 * Queries the pixel aspect ratio of @format. If no format was set, %FALSE 
 * is returned and the aspect ratio is set to 1/1.
 * 
 * Returns: %TRUE if a pixel aspect ratio was set.
 **/
gboolean
gst_cairo_format_get_pixel_aspect_ratio (const GstCairoFormat * format,
    int *numerator, int *denominator)
{
  g_return_val_if_fail (format != NULL, FALSE);

  if (format->par.denom == 0) {
    if (numerator)
      *numerator = 1;
    if (denominator)
      *denominator = 1;
    return FALSE;
  } else {
    if (numerator)
      *numerator = format->par.num;
    if (denominator)
      *denominator = format->par.denom;
    return TRUE;
  }
}

/**
 * gst_cairo_format_get_buffer_size:
 * @format: the format
 *
 * Computes the size buffers passed in the given @format must have.
 * This function must not be called if @format is native 
 *
 * Returns: Size of buffers for this format
 **/
gsize
gst_cairo_format_get_buffer_size (const GstCairoFormat * format)
{
  g_return_val_if_fail (format != NULL, 0);

  if (format->video_format != GST_VIDEO_FORMAT_UNKNOWN)
    return gst_video_format_get_size (format->video_format,
        format->width, format->height);

  switch (format->format) {
    case PIXMAN_g8:
      return GST_ROUND_UP_4 (format->width) * format->height;
    case PIXMAN_nv12:
    case PIXMAN_nv21:
      return GST_ROUND_UP_4 (format->width) * GST_ROUND_UP_2 (format->height) *
          3 / 2;
    default:
      g_assert_not_reached ();
      return 0;
  }
}

gsize
gst_cairo_format_get_offset (const GstCairoFormat * format, guint plane)
{
  g_return_val_if_fail (format != NULL, 0);

  if (format->video_format != GST_VIDEO_FORMAT_UNKNOWN)
    return gst_video_format_get_component_offset (format->video_format, plane,
        format->width, format->height);

  switch (format->format) {
    case PIXMAN_g8:
      return 0;
    case PIXMAN_nv12:
    case PIXMAN_nv21:
      if (plane == 1)
        return GST_ROUND_UP_4 (format->width) * GST_ROUND_UP_2 (format->height);
      else
        return 0;
    default:
      g_assert_not_reached ();
      return 0;
  }
}

gsize
gst_cairo_format_get_stride (const GstCairoFormat * format, guint plane)
{
  g_return_val_if_fail (format != NULL, 0);

  if (format->video_format != GST_VIDEO_FORMAT_UNKNOWN)
    return gst_video_format_get_row_stride (format->video_format, plane,
        format->width);

  switch (format->format) {
    case PIXMAN_g8:
      return GST_ROUND_UP_4 (format->width);
    case PIXMAN_nv12:
    case PIXMAN_nv21:
      return GST_ROUND_UP_4 (format->width);
    default:
      g_assert_not_reached ();
      return 0;
  }
}

pixman_format_code_t
gst_cairo_format_get_pixman_format (const GstCairoFormat * format)
{
  return format->format ? format->format : PIXMAN_a8r8g8b8;
}

/**
 * gst_cairo_format_equal:
 * @a: first format to compare
 * @b: second format to compare
 * @ignore: the settings of the formats that are to be ignored
 *
 * Compares the two formats @a and @b for equality in the all settings but 
 * those in @ignore.
 *
 * Returns: %TRUE if the relevant parts of the formats where equal
 **/
gboolean
gst_cairo_format_equal (const GstCairoFormat * a, const GstCairoFormat * b,
    GstCairoFormatOption ignore)
{
  g_return_val_if_fail (a != NULL, FALSE);
  g_return_val_if_fail (b != NULL, FALSE);

  if (!(ignore & GST_CAIRO_FORMAT_FORMAT) && a->format != b->format)
    return FALSE;

  if (!(ignore & GST_CAIRO_FORMAT_WIDTH) && a->width != b->width)
    return FALSE;

  if (!(ignore & GST_CAIRO_FORMAT_HEIGHT) && a->height != b->height)
    return FALSE;

  if (!(ignore & GST_CAIRO_FORMAT_FRAMERATE) &&
      (a->framerate.num != b->framerate.num ||
          a->framerate.denom != b->framerate.denom))
    return FALSE;

  if (!(ignore & GST_CAIRO_FORMAT_PAR) &&
      (a->par.num != b->par.num || a->par.denom != b->par.denom))
    return FALSE;

  return TRUE;
}