summaryrefslogtreecommitdiff
path: root/gst-libs/gst/video/video-multiview.c
blob: 107728db02c5ac6e78a85f6743025e5f262fc696 (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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
/* GStreamer
 * Copyright (C) <2015> Jan Schmidt <jan@centricular.com>
 *
 * 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., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

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

#include <string.h>

#include "video.h"

GType
gst_video_multiview_flagset_get_type (void)
{
  static GType type = 0;

  if (g_once_init_enter (&type)) {
    GType _type = gst_flagset_register (GST_TYPE_VIDEO_MULTIVIEW_FLAGS);
    g_once_init_leave (&type, _type);
  }
  return type;
}


/* Caps mnemonics for the various multiview representations */

static const struct mview_map_t
{
  const gchar *caps_repr;
  GstVideoMultiviewMode mode;
} gst_multiview_modes[] = {
  {
  "mono", GST_VIDEO_MULTIVIEW_MODE_MONO}, {
  "left", GST_VIDEO_MULTIVIEW_MODE_LEFT}, {
  "right", GST_VIDEO_MULTIVIEW_MODE_RIGHT}, {
  "side-by-side", GST_VIDEO_MULTIVIEW_MODE_SIDE_BY_SIDE}, {
  "side-by-side-quincunx", GST_VIDEO_MULTIVIEW_MODE_SIDE_BY_SIDE_QUINCUNX}, {
  "column-interleaved", GST_VIDEO_MULTIVIEW_MODE_COLUMN_INTERLEAVED}, {
  "row-interleaved", GST_VIDEO_MULTIVIEW_MODE_ROW_INTERLEAVED}, {
  "top-bottom", GST_VIDEO_MULTIVIEW_MODE_TOP_BOTTOM}, {
  "checkerboard", GST_VIDEO_MULTIVIEW_MODE_CHECKERBOARD}, {
  "frame-by-frame", GST_VIDEO_MULTIVIEW_MODE_FRAME_BY_FRAME}, {
  "multiview-frame-by-frame",
        GST_VIDEO_MULTIVIEW_MODE_MULTIVIEW_FRAME_BY_FRAME}, {
  "separated", GST_VIDEO_MULTIVIEW_MODE_SEPARATED}
};

/**
 * gst_video_multiview_mode_to_caps_string:
 * @mview_mode: A #GstVideoMultiviewMode value
 *
 * Returns: The caps string representation of the mode, or NULL if invalid.
 *
 * Given a #GstVideoMultiviewMode returns the multiview-mode caps string
 * for insertion into a caps structure
 *
 * Since: 1.6
 */
const gchar *
gst_video_multiview_mode_to_caps_string (GstVideoMultiviewMode mview_mode)
{
  gint i;

  for (i = 0; i < G_N_ELEMENTS (gst_multiview_modes); i++) {
    if (gst_multiview_modes[i].mode == mview_mode) {
      return gst_multiview_modes[i].caps_repr;
    }
  }

  return NULL;
}

/**
 * gst_video_multiview_mode_from_caps_string:
 * @caps_mview_mode: multiview-mode field string from caps
 *
 * Returns: The #GstVideoMultiviewMode value
 *
 * Given a string from a caps multiview-mode field,
 * output the corresponding #GstVideoMultiviewMode
 * or #GST_VIDEO_MULTIVIEW_MODE_NONE
 *
 * Since: 1.6
 */
GstVideoMultiviewMode
gst_video_multiview_mode_from_caps_string (const gchar * caps_mview_mode)
{
  gint i;

  for (i = 0; i < G_N_ELEMENTS (gst_multiview_modes); i++) {
    if (g_str_equal (gst_multiview_modes[i].caps_repr, caps_mview_mode)) {
      return gst_multiview_modes[i].mode;
    }
  }

  GST_ERROR ("Invalid multiview info %s", caps_mview_mode);
  g_warning ("Invalid multiview info %s", caps_mview_mode);

  return GST_VIDEO_MULTIVIEW_MODE_NONE;
}

/* Array of mono, unpacked, double-height and double-width modes */
static GValue mode_values[5];

static void
init_mview_mode_vals (void)
{
  static gsize mview_mode_vals_init = 0;

  if (g_once_init_enter (&mview_mode_vals_init)) {
    GValue item = { 0, };
    GValue *list;

    g_value_init (&item, G_TYPE_STRING);

    /* Mono modes */
    list = mode_values;
    g_value_init (list, GST_TYPE_LIST);
    g_value_set_static_string (&item, "mono");
    gst_value_list_append_value (list, &item);
    g_value_set_static_string (&item, "left");
    gst_value_list_append_value (list, &item);
    g_value_set_static_string (&item, "right");
    gst_value_list_append_value (list, &item);

    /* Unpacked modes - ones split across buffers or memories */
    list = mode_values + 1;
    g_value_init (list, GST_TYPE_LIST);
    g_value_set_static_string (&item, "separated");
    gst_value_list_append_value (list, &item);
    g_value_set_static_string (&item, "frame-by-frame");
    gst_value_list_append_value (list, &item);
    g_value_set_static_string (&item, "multiview-frame-by-frame");
    gst_value_list_append_value (list, &item);

    /* Double height modes */
    list = mode_values + 2;
    g_value_init (list, GST_TYPE_LIST);
    g_value_set_static_string (&item, "top-bottom");
    gst_value_list_append_value (list, &item);
    g_value_set_static_string (&item, "row-interleaved");
    gst_value_list_append_value (list, &item);

    /* Double width modes */
    list = mode_values + 3;
    g_value_init (list, GST_TYPE_LIST);
    g_value_set_static_string (&item, "side-by-side");
    gst_value_list_append_value (list, &item);
    g_value_set_static_string (&item, "side-by-side-quincunx");
    gst_value_list_append_value (list, &item);
    g_value_set_static_string (&item, "column-interleaved");
    gst_value_list_append_value (list, &item);

    /* Double size (both width & height) modes */
    list = mode_values + 4;
    g_value_init (list, GST_TYPE_LIST);
    g_value_set_static_string (&item, "checkerboard");
    gst_value_list_append_value (list, &item);

    g_value_unset (&item);
    g_once_init_leave (&mview_mode_vals_init, 1);
  }
}

/**
 * gst_video_multiview_get_mono_modes:
 *
 * Returns: A const #GValue containing a list of mono video modes
 *
 * Utility function that returns a #GValue with a GstList of mono video
 * modes (mono/left/right) for use in caps negotiations.
 *
 * Since: 1.6
 */
const GValue *
gst_video_multiview_get_mono_modes (void)
{
  init_mview_mode_vals ();
  return mode_values;
}

/**
 * gst_video_multiview_get_unpacked_modes:
 *
 * Returns: A const #GValue containing a list of 'unpacked' stereo video modes
 *
 * Utility function that returns a #GValue with a GstList of unpacked
 * stereo video modes (separated/frame-by-frame/frame-by-frame-multiview)
 * for use in caps negotiations.
 *
 * Since: 1.6
 */
const GValue *
gst_video_multiview_get_unpacked_modes (void)
{
  init_mview_mode_vals ();
  return mode_values + 1;
}

/**
 * gst_video_multiview_get_doubled_height_modes:
 *
 * Returns: A const #GValue containing a list of stereo video modes
 *
 * Utility function that returns a #GValue with a GstList of packed stereo
 * video modes with double the height of a single view for use in
 * caps negotiations. Currently this is top-bottom and row-interleaved.
 *
 * Since: 1.6
 */
const GValue *
gst_video_multiview_get_doubled_height_modes (void)
{
  init_mview_mode_vals ();
  return mode_values + 2;
}

/**
 * gst_video_multiview_get_doubled_width_modes:
 *
 * Returns: A const #GValue containing a list of stereo video modes
 *
 * Utility function that returns a #GValue with a GstList of packed stereo
 * video modes with double the width of a single view for use in
 * caps negotiations. Currently this is side-by-side, side-by-side-quincunx
 * and column-interleaved.
 *
 * Since: 1.6
 */
const GValue *
gst_video_multiview_get_doubled_width_modes (void)
{
  init_mview_mode_vals ();
  return mode_values + 3;
}

/**
 * gst_video_multiview_get_doubled_size_modes:
 *
 * Returns: A const #GValue containing a list of stereo video modes
 *
 * Utility function that returns a #GValue with a GstList of packed
 * stereo video modes that have double the width/height of a single
 * view for use in caps negotiation. Currently this is just
 * 'checkerboard' layout.
 *
 * Since: 1.6
 */
const GValue *
gst_video_multiview_get_doubled_size_modes (void)
{
  init_mview_mode_vals ();
  return mode_values + 4;
}

static void
gst_video_multiview_separated_video_info_from_packed (GstVideoInfo * info)
{
  GstVideoMultiviewMode mview_mode;

  mview_mode = GST_VIDEO_INFO_MULTIVIEW_MODE (info);

  /* Normalise the half-aspect flag by adjusting PAR */
  switch (mview_mode) {
    case GST_VIDEO_MULTIVIEW_MODE_SIDE_BY_SIDE:
    case GST_VIDEO_MULTIVIEW_MODE_SIDE_BY_SIDE_QUINCUNX:
    case GST_VIDEO_MULTIVIEW_MODE_COLUMN_INTERLEAVED:
    case GST_VIDEO_MULTIVIEW_MODE_CHECKERBOARD:
      info->width /= 2;
      info->views *= 2;
      GST_VIDEO_INFO_MULTIVIEW_MODE (info) = GST_VIDEO_MULTIVIEW_MODE_SEPARATED;
      if (GST_VIDEO_INFO_MULTIVIEW_FLAGS (info) &
          GST_VIDEO_MULTIVIEW_FLAGS_HALF_ASPECT)
        info->par_n *= 2;
      break;
    case GST_VIDEO_MULTIVIEW_MODE_ROW_INTERLEAVED:
    case GST_VIDEO_MULTIVIEW_MODE_TOP_BOTTOM:
      info->height /= 2;
      info->views *= 2;
      GST_VIDEO_INFO_MULTIVIEW_MODE (info) = GST_VIDEO_MULTIVIEW_MODE_SEPARATED;
      if (GST_VIDEO_INFO_MULTIVIEW_FLAGS (info) &
          GST_VIDEO_MULTIVIEW_FLAGS_HALF_ASPECT)
        info->par_d *= 2;
      break;
    default:
      /* Mono/left/right/frame-by-frame/already separated */
      break;
  }
  GST_VIDEO_INFO_MULTIVIEW_FLAGS (info) &=
      ~GST_VIDEO_MULTIVIEW_FLAGS_HALF_ASPECT;
}

static void
gst_video_multiview_separated_video_info_to_packed (GstVideoInfo * info,
    GstVideoMultiviewMode packed_mview_mode,
    GstVideoMultiviewFlags packed_mview_flags)
{
  /* Convert single-frame info to a packed mode */
  GST_VIDEO_INFO_MULTIVIEW_MODE (info) = packed_mview_mode;
  GST_VIDEO_INFO_MULTIVIEW_FLAGS (info) = packed_mview_flags;

  switch (packed_mview_mode) {
    case GST_VIDEO_MULTIVIEW_MODE_SIDE_BY_SIDE:
    case GST_VIDEO_MULTIVIEW_MODE_SIDE_BY_SIDE_QUINCUNX:
    case GST_VIDEO_MULTIVIEW_MODE_COLUMN_INTERLEAVED:
    case GST_VIDEO_MULTIVIEW_MODE_CHECKERBOARD:
      info->width *= 2;
      info->views /= 2;
      if (packed_mview_flags & GST_VIDEO_MULTIVIEW_FLAGS_HALF_ASPECT)
        info->par_d *= 2;
      break;
    case GST_VIDEO_MULTIVIEW_MODE_ROW_INTERLEAVED:
    case GST_VIDEO_MULTIVIEW_MODE_TOP_BOTTOM:
      info->height *= 2;
      info->views /= 2;
      if (packed_mview_flags & GST_VIDEO_MULTIVIEW_FLAGS_HALF_ASPECT)
        info->par_n *= 2;
      break;
    default:
      break;
  }
}

/**
 * gst_video_multiview_video_info_change_mode:
 * @info: A #GstVideoInfo structure to operate on
 * @out_mview_mode: A #GstVideoMultiviewMode value
 * @out_mview_flags: A set of #GstVideoMultiviewFlags
 *
 * Utility function that transforms the width/height/PAR
 * and multiview mode and flags of a #GstVideoInfo into
 * the requested mode.
 *
 * Since: 1.6
 */
void
gst_video_multiview_video_info_change_mode (GstVideoInfo * info,
    GstVideoMultiviewMode out_mview_mode,
    GstVideoMultiviewFlags out_mview_flags)
{
  gst_video_multiview_separated_video_info_from_packed (info);
  gst_video_multiview_separated_video_info_to_packed (info, out_mview_mode,
      out_mview_flags);
}

/**
 * gst_video_multiview_guess_half_aspect:
 * @mv_mode: A #GstVideoMultiviewMode
 * @width: Video frame width in pixels
 * @height: Video frame height in pixels
 * @par_n: Numerator of the video pixel-aspect-ratio
 * @par_d: Denominator of the video pixel-aspect-ratio
 *
 * Returns: A boolean indicating whether the
 *   #GST_VIDEO_MULTIVIEW_FLAGS_HALF_ASPECT flag should be set.
 *
 * Utility function that heuristically guess whether a
 * frame-packed stereoscopic video contains half width/height
 * encoded views, or full-frame views by looking at the
 * overall display aspect ratio.
 *
 * Since: 1.6
 */
gboolean
gst_video_multiview_guess_half_aspect (GstVideoMultiviewMode mv_mode,
    guint width, guint height, guint par_n, guint par_d)
{
  switch (mv_mode) {
    case GST_VIDEO_MULTIVIEW_MODE_TOP_BOTTOM:
    case GST_VIDEO_MULTIVIEW_MODE_ROW_INTERLEAVED:
      /* If the video is wider than it is tall, assume half aspect */
      if (height * par_d <= width * par_n)
        return TRUE;
      break;
    case GST_VIDEO_MULTIVIEW_MODE_SIDE_BY_SIDE:
    case GST_VIDEO_MULTIVIEW_MODE_SIDE_BY_SIDE_QUINCUNX:
    case GST_VIDEO_MULTIVIEW_MODE_COLUMN_INTERLEAVED:
      /* If the video DAR is less than 2.39:1, assume half-aspect */
      if (width * par_n < 2.39 * height * par_d)
        return TRUE;
      break;
    default:
      break;
  }
  return FALSE;
}

#if 0                           /* Multiview meta disabled for now */
GType
gst_video_multiview_meta_api_get_type (void)
{
  static GType type = 0;
  static const gchar *tags[] =
      { GST_META_TAG_VIDEO_STR, GST_META_TAG_MEMORY_STR,
    NULL
  };

  if (g_once_init_enter (&type)) {
    GType _type = gst_meta_api_type_register ("GstVideoMultiviewMetaAPI", tags);
    g_once_init_leave (&type, _type);
  }
  return type;
}

static gboolean
gst_video_multiview_meta_init (GstVideoMultiviewMeta * mview_meta,
    gpointer params, GstBuffer * buffer)
{
  mview_meta->n_views = 0;
  mview_meta->view_info = NULL;

  return TRUE;
}

static void
gst_video_multiview_meta_free (GstVideoMultiviewMeta * mview_meta,
    GstBuffer * buffer)
{
  g_free (mview_meta->view_info);
}

/* video multiview metadata */
const GstMetaInfo *
gst_video_multiview_meta_get_info (void)
{
  static const GstMetaInfo *video_meta_info = NULL;

  if (g_once_init_enter (&video_meta_info)) {
    const GstMetaInfo *meta =
        gst_meta_register (GST_VIDEO_MULTIVIEW_META_API_TYPE,
        "GstVideoMultiviewMeta",
        sizeof (GstVideoMultiviewMeta),
        (GstMetaInitFunction) gst_video_multiview_meta_init,
        (GstMetaFreeFunction) gst_video_multiview_meta_free,
        NULL);
    g_once_init_leave (&video_meta_info, meta);
  }

  return video_meta_info;
}


GstVideoMultiviewMeta *
gst_buffer_add_video_multiview_meta (GstBuffer * buffer, guint n_views)
{
  GstVideoMultiviewMeta *meta;

  meta =
      (GstVideoMultiviewMeta *) gst_buffer_add_meta (buffer,
      GST_VIDEO_MULTIVIEW_META_INFO, NULL);

  if (!meta)
    return NULL;

  meta->view_info = g_new0 (GstVideoMultiviewViewInfo, n_views);
  meta->n_views = n_views;

  return meta;
}

void
gst_video_multiview_meta_set_n_views (GstVideoMultiviewMeta * mview_meta,
    guint n_views)
{
  guint i;

  mview_meta->view_info =
      g_renew (GstVideoMultiviewViewInfo, mview_meta->view_info, n_views);

  if (mview_meta->view_info == NULL) {
    if (n_views > 0)
      g_warning ("Failed to allocate GstVideoMultiview data");
    mview_meta->n_views = 0;
    return;
  }

  /* Make sure new entries are zero */
  for (i = mview_meta->n_views; i < n_views; i++) {
    GstVideoMultiviewViewInfo *info = mview_meta->view_info + i;

    info->meta_id = 0;
    info->view_label = GST_VIDEO_MULTIVIEW_VIEW_UNKNOWN;
  }
  mview_meta->n_views = n_views;
}

#endif