summaryrefslogtreecommitdiff
path: root/ext/schroedinger/gstschroutils.c
blob: 9057f9843dca1d3ef32b6abef92f050fde27f4b9 (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
/* Schrodinger
 * Copyright (C) 2008 David Schleef <ds@schleef.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., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

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

#include "gstschroutils.h"

//#define SCHRO_ENABLE_UNSTABLE_API

#include <gst/gst.h>
#include <gst/video/video.h>
#include <schroedinger/schro.h>
#include <schroedinger/schrobitstream.h>
#include <schroedinger/schrovirtframe.h>
#include <math.h>
#include <string.h>

GST_DEBUG_CATEGORY_EXTERN (schro_debug);
#define GST_CAT_DEFAULT schro_debug

typedef struct
{
  GstVideoFrame frame;
} FrameData;


static void
gst_schro_frame_free (SchroFrame * frame, void *priv)
{
  FrameData *data = priv;

  gst_video_frame_unmap (&data->frame);

  g_slice_free (FrameData, data);
}

GstBuffer *
gst_schro_frame_get_buffer (SchroFrame * frame)
{
  if (frame->priv)
    return gst_buffer_ref (((FrameData *) frame->priv)->frame.buffer);

  return NULL;
}

SchroFrame *
gst_schro_buffer_wrap (GstBuffer * buf, gboolean write, GstVideoInfo * vinfo)
{
  SchroFrame *frame;
  GstVideoFrame vframe;
  FrameData *data;
  gint i;

  if (!gst_video_frame_map (&vframe, vinfo, buf,
          (write ? GST_MAP_READWRITE : GST_MAP_READ)))
    return NULL;

  frame = schro_frame_new ();

  frame->width = GST_VIDEO_FRAME_WIDTH (&vframe);
  frame->height = GST_VIDEO_FRAME_HEIGHT (&vframe);

  switch (GST_VIDEO_FRAME_FORMAT (&vframe)) {
    case GST_VIDEO_FORMAT_I420:
    case GST_VIDEO_FORMAT_YV12:
      frame->format = SCHRO_FRAME_FORMAT_U8_420;
      break;
    case GST_VIDEO_FORMAT_YUY2:
      frame->format = SCHRO_FRAME_FORMAT_YUYV;
      break;
    case GST_VIDEO_FORMAT_UYVY:
      frame->format = SCHRO_FRAME_FORMAT_UYVY;
      break;
    case GST_VIDEO_FORMAT_AYUV:
      frame->format = SCHRO_FRAME_FORMAT_AYUV;
      break;
#if SCHRO_CHECK_VERSION(1,0,12)
    case GST_VIDEO_FORMAT_ARGB:
      frame->format = SCHRO_FRAME_FORMAT_ARGB;
      break;
#endif
#if SCHRO_CHECK_VERSION(1,0,11)
    case GST_VIDEO_FORMAT_Y42B:
      frame->format = SCHRO_FRAME_FORMAT_U8_422;
      break;
    case GST_VIDEO_FORMAT_Y444:
      frame->format = SCHRO_FRAME_FORMAT_U8_444;
      break;
    case GST_VIDEO_FORMAT_v210:
      frame->format = SCHRO_FRAME_FORMAT_v210;
      break;
    case GST_VIDEO_FORMAT_v216:
      frame->format = SCHRO_FRAME_FORMAT_v216;
      break;
    case GST_VIDEO_FORMAT_AYUV64:
      frame->format = SCHRO_FRAME_FORMAT_AY64;
      break;
#endif
    default:
      g_assert_not_reached ();
      return NULL;
  }

  if (SCHRO_FRAME_IS_PACKED (frame->format)) {
    frame->components[0].format = frame->format;
    frame->components[0].width = frame->width;
    frame->components[0].height = frame->height;
    frame->components[0].stride = GST_VIDEO_FRAME_COMP_STRIDE (&vframe, 0);
    frame->components[0].length = frame->components[0].stride * frame->height;
    frame->components[0].data = vframe.data[0];
    frame->components[0].v_shift = 0;
    frame->components[0].h_shift = 0;
  } else {
    for (i = 0; i < GST_VIDEO_FRAME_N_COMPONENTS (&vframe); i++) {
      frame->components[i].format = frame->format;
      frame->components[i].width = GST_VIDEO_FRAME_COMP_WIDTH (&vframe, i);
      frame->components[i].height = GST_VIDEO_FRAME_COMP_HEIGHT (&vframe, i);
      frame->components[i].stride = GST_VIDEO_FRAME_COMP_STRIDE (&vframe, i);
      frame->components[i].length =
          frame->components[i].stride * frame->components[i].height;
      frame->components[i].data = GST_VIDEO_FRAME_COMP_DATA (&vframe, i);
      if (i == 0) {
        frame->components[i].v_shift = 0;
        frame->components[i].h_shift = 0;
      } else {
        frame->components[i].v_shift =
            SCHRO_FRAME_FORMAT_H_SHIFT (frame->format);
        frame->components[i].h_shift =
            SCHRO_FRAME_FORMAT_H_SHIFT (frame->format);
      }
    }
  }

  data = g_slice_new0 (FrameData);
  data->frame = vframe;
  schro_frame_set_free_callback (frame, gst_schro_frame_free, data);

  return frame;
}

static void
schro_buf_free_func (gpointer priv)
{
  SchroBuffer *buffer = (SchroBuffer *) priv;

  schro_buffer_unref (buffer);
}

/* takes the reference */
GstBuffer *
gst_schro_wrap_schro_buffer (SchroBuffer * buffer)
{
  GstMemory *mem;
  GstBuffer *buf;

  mem =
      gst_memory_new_wrapped (0, buffer->data, buffer->length, 0,
      buffer->length, buffer, schro_buf_free_func);
  buf = gst_buffer_new ();
  gst_buffer_append_memory (buf, mem);

  return buf;
}

typedef struct
{
  GstMemory *mem;
  GstMapInfo info;
} BufferData;

static void
gst_schro_buffer_free (SchroBuffer * buffer, void *priv)
{
  BufferData *data = priv;

  gst_memory_unmap (data->mem, &data->info);
  gst_memory_unref (data->mem);
  g_slice_free (BufferData, priv);
}

SchroBuffer *
gst_schro_wrap_gst_buffer (GstBuffer * buffer)
{
  SchroBuffer *schrobuf;
  GstMemory *mem;
  GstMapInfo info;
  BufferData *data;

  mem = gst_buffer_get_all_memory (buffer);
  if (!gst_memory_map (mem, &info, GST_MAP_READ)) {
    GST_ERROR ("Couldn't get readable memory from gstbuffer");
    return NULL;
  }

  /* FIXME : We can't control if data won't be read/write outside
   * of schro ... */
  data = g_slice_new0 (BufferData);
  data->info = info;
  data->mem = mem;

  schrobuf = schro_buffer_new_with_data (info.data, info.size);
  schrobuf->free = gst_schro_buffer_free;
  schrobuf->priv = data;

  return schrobuf;
}