summaryrefslogtreecommitdiff
path: root/src/gstducatih264enc.c
blob: 91407a2543bfb84f92695a62d789a4d7e251599e (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
/* GStreamer
 * Copyright (c) 2011, Texas Instruments Incorporated
 * Copyright (c) 2011, Collabora Ltd.
 *
 * 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.
 *
 * Author: Alessandro Decina <alessandro.decina@collabora.com>
 */

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

#include "gstducati.h"
#include "gstducatih264enc.h"

#include <string.h>

#include <math.h>

#define GST_CAT_DEFAULT gst_ducati_debug

#define DEFAULT_PROFILE GST_DUCATI_H264ENC_PROFILE_HIGH
#define DEFAULT_LEVEL GST_DUCATI_H264ENC_LEVEL_40

#define GST_TYPE_DUCATI_H264ENC_PROFILE (gst_ducati_h264enc_profile_get_type ())
#define GST_TYPE_DUCATI_H264ENC_LEVEL (gst_ducati_h264enc_level_get_type ())


enum
{
  LAST_SIGNAL
};

enum
{
  PROP_0,
  PROP_PROFILE,
  PROP_LEVEL,
};

static void gst_ducati_h264enc_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec);
static void gst_ducati_h264enc_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec);

static gboolean gst_ducati_h264enc_allocate_params (GstDucatiVidEnc *
    self, gint params_sz, gint dynparams_sz, gint status_sz, gint inargs_sz,
    gint outargs_sz);
static gboolean gst_ducati_h264enc_configure (GstDucatiVidEnc * self);


static GstStaticPadTemplate gst_ducati_h264enc_sink_template =
GST_STATIC_PAD_TEMPLATE ("sink",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("NV12"))
    );

static GstStaticPadTemplate gst_ducati_h264enc_src_template =
GST_STATIC_PAD_TEMPLATE ("src",
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("video/x-h264")
    );


#define gst_ducati_h264enc_parent_class parent_class
G_DEFINE_TYPE (GstDucatiH264Enc, gst_ducati_h264enc, GST_TYPE_DUCATIVIDENC);


/* the values for the following enums are taken from the codec */

enum
{
  GST_DUCATI_H264ENC_PROFILE_BASELINE = 66,            /**< BaseLine Profile   */
  GST_DUCATI_H264ENC_PROFILE_MAIN = 77,                /**< Main Profile       */
  GST_DUCATI_H264ENC_PROFILE_EXTENDED = 88,            /**< Extended Profile   */
  GST_DUCATI_H264ENC_PROFILE_HIGH = 100,               /**< High Profile       */
  GST_DUCATI_H264ENC_PROFILE_HIGH_10 = 110,             /**< High 10 Profile    */
  GST_DUCATI_H264ENC_PROFILE_HIGH_422 = 122             /**< High 4:2:2 Profile */
};

enum
{
  GST_DUCATI_H264ENC_LEVEL_10 = 10, /**<  Level 1.0  */
  GST_DUCATI_H264ENC_LEVEL_1b = 9,  /**<  Level 1.b  */
  GST_DUCATI_H264ENC_LEVEL_11 = 11, /**<  Level 1.1  */
  GST_DUCATI_H264ENC_LEVEL_12 = 12, /**<  Level 1.2  */
  GST_DUCATI_H264ENC_LEVEL_13 = 13, /**<  Level 1.3  */
  GST_DUCATI_H264ENC_LEVEL_20 = 20, /**<  Level 2.0  */
  GST_DUCATI_H264ENC_LEVEL_21 = 21, /**<  Level 2.1  */
  GST_DUCATI_H264ENC_LEVEL_22 = 22, /**<  Level 2.2  */
  GST_DUCATI_H264ENC_LEVEL_30 = 30, /**<  Level 3.0  */
  GST_DUCATI_H264ENC_LEVEL_31 = 31, /**<  Level 3.1  */
  GST_DUCATI_H264ENC_LEVEL_32 = 32, /**<  Level 3.2  */
  GST_DUCATI_H264ENC_LEVEL_40 = 40, /**<  Level 4.0  */
  GST_DUCATI_H264ENC_LEVEL_41 = 41, /**<  Level 4.1  */
  GST_DUCATI_H264ENC_LEVEL_42 = 42, /**<  Level 4.2  */
  GST_DUCATI_H264ENC_LEVEL_50 = 50, /**<  Level 5.0  */
  GST_DUCATI_H264ENC_LEVEL_51 = 51 /**<  Level 5.1  */
};

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

  if (!type) {
    static const GEnumValue vals[] = {
      {GST_DUCATI_H264ENC_PROFILE_BASELINE, "Base Profile", "baseline"},
      {GST_DUCATI_H264ENC_PROFILE_MAIN, "Main Profile", "main"},
      {GST_DUCATI_H264ENC_PROFILE_EXTENDED, "Extended Profile", "extended"},
      {GST_DUCATI_H264ENC_PROFILE_HIGH, "High Profile", "high"},
      {GST_DUCATI_H264ENC_PROFILE_HIGH_10, "High 10 Profile", "high-10"},
      {GST_DUCATI_H264ENC_PROFILE_HIGH_422, "High 4:2:2 Profile", "high-422"},
      {0, NULL, NULL},
    };

    type = g_enum_register_static ("GstDucatiH264EncProfile", vals);
  }

  return type;
}

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

  if (!type) {
    static const GEnumValue vals[] = {
      {GST_DUCATI_H264ENC_LEVEL_10, "Level 1", "level-1"},
      {GST_DUCATI_H264ENC_LEVEL_1b, "Level 1b", "level-1b"},
      {GST_DUCATI_H264ENC_LEVEL_11, "Level 11", "level-11"},
      {GST_DUCATI_H264ENC_LEVEL_12, "Level 12", "level-12"},
      {GST_DUCATI_H264ENC_LEVEL_13, "Level 13", "level-13"},
      {GST_DUCATI_H264ENC_LEVEL_20, "Level 2", "level-2"},
      {GST_DUCATI_H264ENC_LEVEL_21, "Level 21", "level-21"},
      {GST_DUCATI_H264ENC_LEVEL_22, "Level 22", "level-22"},
      {GST_DUCATI_H264ENC_LEVEL_30, "Level 3", "level-3"},
      {GST_DUCATI_H264ENC_LEVEL_31, "Level 31", "level-31"},
      {GST_DUCATI_H264ENC_LEVEL_32, "Level 32", "level-32"},
      {GST_DUCATI_H264ENC_LEVEL_40, "Level 4", "level-4"},
      {GST_DUCATI_H264ENC_LEVEL_41, "Level 41", "level-41"},
      {GST_DUCATI_H264ENC_LEVEL_42, "Level 42", "level-42"},
      {GST_DUCATI_H264ENC_LEVEL_50, "Level 5", "level-5"},
      {GST_DUCATI_H264ENC_LEVEL_51, "Level 51", "level-51"},
      {0, NULL, NULL},
    };

    type = g_enum_register_static ("GstDucatiH264EncLevel", vals);
  }

  return type;
}

static void
gst_ducati_h264enc_class_init (GstDucatiH264EncClass * klass)
{
  GObjectClass *gobject_class;
  GstElementClass *element_class;
  GstDucatiVidEncClass *videnc_class;

  gobject_class = G_OBJECT_CLASS (klass);
  element_class = GST_ELEMENT_CLASS (klass);
  videnc_class = GST_DUCATIVIDENC_CLASS (klass);

  gobject_class->set_property = gst_ducati_h264enc_set_property;
  gobject_class->get_property = gst_ducati_h264enc_get_property;

  gst_element_class_add_pad_template (element_class,
      gst_static_pad_template_get (&gst_ducati_h264enc_src_template));
  gst_element_class_add_pad_template (element_class,
      gst_static_pad_template_get (&gst_ducati_h264enc_sink_template));

  gst_element_class_set_details_simple (element_class, "H264 Encoder",
      "Codec/Encoder/Video",
      "Encode raw video into H264 stream",
      "Alessandro Decina <alessandro.decina@collabora.com>");

  videnc_class->codec_name = "ivahd_h264enc";

  videnc_class->allocate_params = gst_ducati_h264enc_allocate_params;
  videnc_class->configure = gst_ducati_h264enc_configure;

  g_object_class_install_property (gobject_class, PROP_PROFILE,
      g_param_spec_enum ("profile", "H.264 Profile", "H.264 Profile",
          GST_TYPE_DUCATI_H264ENC_PROFILE, DEFAULT_PROFILE, G_PARAM_READWRITE));

  g_object_class_install_property (gobject_class, PROP_LEVEL,
      g_param_spec_enum ("level", "H.264 Level", "H.264 Level",
          GST_TYPE_DUCATI_H264ENC_LEVEL, DEFAULT_LEVEL, G_PARAM_READWRITE));
}

static void
gst_ducati_h264enc_init (GstDucatiH264Enc * self)
{
  GST_DEBUG ("gst_ducati_h264enc_init");

  self->profile = DEFAULT_PROFILE;
  self->level = DEFAULT_LEVEL;
}

static void
gst_ducati_h264enc_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec)
{
  GstDucatiH264Enc *self = GST_DUCATIH264ENC (object);

  g_return_if_fail (GST_IS_DUCATIH264ENC (object));
  self = GST_DUCATIH264ENC (object);

  switch (prop_id) {
    case PROP_PROFILE:
      self->profile = g_value_get_enum (value);
      break;
    case PROP_LEVEL:
      self->level = g_value_get_enum (value);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
  }
}

static void
gst_ducati_h264enc_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec)
{
  GstDucatiH264Enc *self = GST_DUCATIH264ENC (object);

  g_return_if_fail (GST_IS_DUCATIH264ENC (object));
  self = GST_DUCATIH264ENC (object);

  switch (prop_id) {
    case PROP_PROFILE:
      g_value_set_enum (value, self->profile);
      break;
    case PROP_LEVEL:
      g_value_set_enum (value, self->level);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
  }
}

static gboolean
gst_ducati_h264enc_configure (GstDucatiVidEnc * videnc)
{
  GstDucatiH264Enc *self = GST_DUCATIH264ENC (videnc);
  IH264ENC_Params *params;

  videnc->params->profile = self->profile;
  videnc->params->level = self->level;

  params = (IH264ENC_Params *) videnc->params;
  /* this is the only non-base field strictly required */
  params->maxIntraFrameInterval = 0x7fffffff;

  return GST_DUCATIVIDENC_CLASS (parent_class)->configure (videnc);
}

static gboolean
gst_ducati_h264enc_allocate_params (GstDucatiVidEnc *
    videnc, gint params_sz, gint dynparams_sz, gint status_sz, gint inargs_sz,
    gint outargs_sz)
{
  return GST_DUCATIVIDENC_CLASS (parent_class)->allocate_params (videnc,
      sizeof (IH264ENC_Params), sizeof (IVIDENC2_DynamicParams),
      sizeof (IVIDENC2_Status), sizeof (IVIDENC2_InArgs),
      sizeof (IVIDENC2_OutArgs));
}