encodeh264: set profile to enum values
[vaapi:windyuan-gstreamer-vaapi.git] / gst / vaapi / gstvaapiencode_h264.c
1 /*
2  *  gstvaapiencode_h264.c - VA-API H.264 encoder
3  *
4  *  Copyright (C) 2012-2013 Intel Corporation
5  *
6  *  This library is free software; you can redistribute it and/or
7  *  modify it under the terms of the GNU Lesser General Public License
8  *  as published by the Free Software Foundation; either version 2.1
9  *  of the License, or (at your option) any later version.
10  *
11  *  This library is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  *  Lesser General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Lesser General Public
17  *  License along with this library; if not, write to the Free
18  *  Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  *  Boston, MA 02110-1301 USA
20  */
21
22 #include "gst/vaapi/sysdeps.h"
23 #include "gst/vaapi/gstvaapicompat.h"
24
25 #include "gstvaapiencode_h264.h"
26 #include "gstvaapivideomemory.h"
27 #include "gst/vaapi/gstvaapiencoder_h264.h"
28 #include "gst/vaapi/gstvaapiencoder_h264_priv.h"
29 #include "gst/vaapi/gstvaapidisplay.h"
30 #include "gst/vaapi/gstvaapivalue.h"
31 #include "gst/vaapi/gstvaapisurface.h"
32
33 #include <string.h>
34
35 GST_DEBUG_CATEGORY_STATIC (gst_vaapi_h264_encode_debug);
36 #define GST_CAT_DEFAULT gst_vaapi_h264_encode_debug
37
38 #define GST_CAPS_CODEC(CODEC) CODEC "; "
39
40 static const char gst_vaapiencode_h264_sink_caps_str[] =
41 #if GST_CHECK_VERSION(1,1,0)
42         GST_VIDEO_CAPS_MAKE_WITH_FEATURES(
43             GST_CAPS_FEATURE_MEMORY_VAAPI_SURFACE, GST_VIDEO_FORMATS_ALL)
44         ", interlace-mode=progressive";
45 #else
46     GST_VIDEO_CAPS_MAKE(GST_VIDEO_FORMATS_ALL) ",interlace-mode=progressive; "
47     GST_VAAPI_SURFACE_CAPS ",interlace-mode=progressive";
48 #endif
49
50 static const char gst_vaapiencode_h264_src_caps_str[] =
51     GST_CAPS_CODEC("video/x-h264");
52
53 static GstStaticPadTemplate gst_vaapiencode_h264_sink_factory =
54     GST_STATIC_PAD_TEMPLATE(
55         "sink",
56         GST_PAD_SINK,
57         GST_PAD_ALWAYS,
58         GST_STATIC_CAPS(gst_vaapiencode_h264_sink_caps_str));
59
60 static GstStaticPadTemplate gst_vaapiencode_h264_src_factory =
61     GST_STATIC_PAD_TEMPLATE(
62         "src",
63         GST_PAD_SRC,
64         GST_PAD_ALWAYS,
65         GST_STATIC_CAPS(gst_vaapiencode_h264_src_caps_str));
66
67 /* h264 encode */
68 G_DEFINE_TYPE(
69     GstVaapiEncodeH264,
70     gst_vaapiencode_h264,
71     GST_TYPE_VAAPIENCODE)
72
73 enum {
74     H264_PROP_0,
75     H264_PROP_PROFILE,
76     H264_PROP_LEVEL,
77     H264_PROP_RATE_CONTROL,
78     H264_PROP_BITRATE,
79     H264_PROP_INTRA_PERIOD,
80     H264_PROP_INIT_QP,
81     H264_PROP_MIN_QP,
82     H264_PROP_SLICE_NUM,
83     H264_PROP_B_FRAME_NUM,
84 };
85
86 #define GST_VAAPI_TYPE_ENCODER_H264_PROFILE gst_vaapiencoder_h264_profile_get_type()
87
88 static GType
89 gst_vaapiencoder_h264_profile_get_type(void)
90 {
91     static GType g_type = 0;
92
93     static const GEnumValue h264_profile_values[] = {
94         { GST_VAAPI_PROFILE_H264_BASELINE,
95           "Baseline profile", "baseline" },
96         { GST_VAAPI_PROFILE_H264_MAIN,
97           "Main profile", "main" },
98         { GST_VAAPI_PROFILE_H264_HIGH,
99           "High profile", "high" },
100         { GST_VAAPI_PROFILE_UNKNOWN,
101           "auto", "auto" },
102         { 0, NULL, NULL },
103     };
104
105     if (!g_type)
106         g_type = g_enum_register_static("GstVaapiEncoderH264Profile", h264_profile_values);
107     return g_type;
108 }
109
110 static void
111 gst_vaapiencode_h264_init(
112     GstVaapiEncodeH264 *h264_encode
113 )
114 {
115     GstVaapiEncode *encode = GST_VAAPIENCODE(h264_encode);
116     encode->encoder = gst_vaapi_encoder_h264_new(NULL);
117     g_assert(encode->encoder);
118 }
119
120 static void
121 gst_vaapiencode_h264_finalize(GObject *object)
122 {
123     //GstVaapiEncodeH264 * const h264_encode = GST_VAAPIENCODE_H264(object);
124     G_OBJECT_CLASS(gst_vaapiencode_h264_parent_class)->finalize(object);
125 }
126
127 static void
128 gst_vaapiencode_h264_set_property(
129     GObject *object,
130     guint prop_id,
131     const GValue *value,
132     GParamSpec *pspec
133 )
134 {
135     GstVaapiEncode *encode = GST_VAAPIENCODE(object);
136     GstVaapiEncoderH264 *h264encoder = GST_VAAPI_ENCODER_H264(encode->encoder);
137
138     g_assert(h264encoder);
139
140     switch (prop_id) {
141     case H264_PROP_PROFILE: {
142         h264encoder->profile = g_value_get_enum(value);
143         break;
144     }
145
146     case H264_PROP_LEVEL: {
147         guint level = g_value_get_uint(value);
148         h264encoder->level = level;
149         break;
150     }
151
152     case H264_PROP_RATE_CONTROL: {
153       GST_VAAPI_ENCODER_RATE_CONTROL(h264encoder) = g_value_get_enum(value);
154       break;
155     }
156
157     case H264_PROP_BITRATE: {
158         h264encoder->bitrate = g_value_get_uint(value);
159         break;
160     }
161
162     case H264_PROP_INTRA_PERIOD:
163         h264encoder->intra_period = g_value_get_uint(value);
164         break;
165
166     case H264_PROP_INIT_QP:
167         h264encoder->init_qp = g_value_get_uint(value);
168         break;
169
170     case H264_PROP_MIN_QP:
171         h264encoder->min_qp = g_value_get_uint(value);
172         break;
173
174     case H264_PROP_SLICE_NUM:
175         h264encoder->slice_num= g_value_get_uint(value);
176         break;
177
178     case H264_PROP_B_FRAME_NUM:
179         h264encoder->b_frame_num= g_value_get_uint(value);
180         break;
181
182     default:
183         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
184         break;
185     }
186 }
187
188 static void
189 gst_vaapiencode_h264_get_property (
190     GObject * object,
191     guint prop_id,
192     GValue * value,
193     GParamSpec * pspec
194 )
195 {
196     GstVaapiEncode *encode = GST_VAAPIENCODE(object);
197     GstVaapiEncoderH264 *h264encoder = GST_VAAPI_ENCODER_H264(encode->encoder);
198     g_assert(h264encoder);
199
200     switch (prop_id) {
201     case H264_PROP_PROFILE:
202         g_value_set_enum (value, h264encoder->profile);
203         break;
204     case H264_PROP_LEVEL:
205         g_value_set_uint (value, h264encoder->level);
206         break;
207     case H264_PROP_RATE_CONTROL:
208         g_value_set_enum(value, GST_VAAPI_ENCODER_RATE_CONTROL(h264encoder));
209         break;
210     case H264_PROP_BITRATE:
211         g_value_set_uint (value, h264encoder->bitrate);
212         break;
213     case H264_PROP_INTRA_PERIOD:
214         g_value_set_uint (value, h264encoder->intra_period);
215         break;
216     case H264_PROP_INIT_QP:
217         g_value_set_uint (value, h264encoder->init_qp);
218         break;
219     case H264_PROP_MIN_QP:
220         g_value_set_uint (value, h264encoder->min_qp);
221         break;
222     case H264_PROP_SLICE_NUM:
223         g_value_set_uint (value, h264encoder->slice_num);
224         break;
225     case H264_PROP_B_FRAME_NUM:
226         g_value_set_uint (value, h264encoder->b_frame_num);
227         break;
228     default:
229         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
230         break;
231     }
232 }
233
234 static GstVaapiEncoder *
235 gst_vaapiencode_h264_create_encoder(GstVaapiDisplay *display)
236 {
237     return gst_vaapi_encoder_h264_new(display);
238 }
239
240 static void
241 gst_vaapiencode_h264_class_init(GstVaapiEncodeH264Class *klass)
242 {
243     GObjectClass * const object_class = G_OBJECT_CLASS(klass);
244     GstElementClass *const element_class = GST_ELEMENT_CLASS(klass);
245     GstVaapiEncodeClass *const encode_class = GST_VAAPIENCODE_CLASS(klass);
246     GST_DEBUG_CATEGORY_INIT (gst_vaapi_h264_encode_debug,
247                            "vaapih264encode",
248                            0,
249                            "vaapih264encode element");
250
251     object_class->finalize      = gst_vaapiencode_h264_finalize;
252     object_class->set_property  = gst_vaapiencode_h264_set_property;
253     object_class->get_property  = gst_vaapiencode_h264_get_property;
254
255     encode_class->create_encoder = gst_vaapiencode_h264_create_encoder;
256
257     gst_element_class_set_static_metadata(element_class,
258         "VA-API h264 encoder",
259         "Codec/Encoder/Video",
260         "A VA-API based video encoder",
261         "Wind Yuan <feng.yuan@intel.com>");
262
263     /* sink pad */
264     gst_element_class_add_pad_template(
265         element_class,
266         gst_static_pad_template_get(&gst_vaapiencode_h264_sink_factory)
267     );
268
269     /* src pad */
270     gst_element_class_add_pad_template(
271         element_class,
272         gst_static_pad_template_get(&gst_vaapiencode_h264_src_factory)
273     );
274
275     g_object_class_install_property (
276         object_class,
277         H264_PROP_PROFILE,
278         g_param_spec_enum (
279             "profile",
280             "H264 Profile",
281             "Profile supports: Baseline, Main, High",
282             GST_VAAPI_TYPE_ENCODER_H264_PROFILE,
283             GST_VAAPI_PROFILE_UNKNOWN,
284             G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
285
286     g_object_class_install_property (
287       object_class,
288       H264_PROP_LEVEL,
289       g_param_spec_uint (
290           "level",
291           "H264 level idc",
292           "Level idc supports: 10, 11, 12, 13, 20, 21, 22, 30, 31, 32, 40, 41",
293           GST_VAAPI_ENCODER_H264_LEVEL_10,
294           GST_VAAPI_ENCODER_H264_LEVEL_51,
295           GST_VAAPI_ENCODER_H264_DEFAULT_LEVEL,
296           G_PARAM_READWRITE));
297
298     g_object_class_install_property (
299       object_class,
300       H264_PROP_RATE_CONTROL,
301       g_param_spec_enum ("rate-control",
302           "rate-control",
303           "Rate control mode",
304           GST_VAAPI_TYPE_RATE_CONTROL,
305           GST_VAAPI_RATECONTROL_NONE,
306           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
307
308     g_object_class_install_property (
309       object_class,
310       H264_PROP_BITRATE,
311       g_param_spec_uint (
312           "bitrate",
313           "H264 encoding bitrate(kbps)",
314           "H264 encoding bitrate(kbps), (0, auto-calculate)",
315           0,
316           100*1024,
317           0,
318           G_PARAM_READWRITE));
319
320     g_object_class_install_property (
321       object_class,
322       H264_PROP_INTRA_PERIOD,
323       g_param_spec_uint (
324           "intra-period",
325           "H264 encoding intra-period",
326           "H264 encoding intra-period",
327           1,
328           300,
329           GST_VAAPI_ENCODER_H264_DEFAULT_INTRA_PERIOD,
330           G_PARAM_READWRITE));
331
332     g_object_class_install_property (
333       object_class,
334       H264_PROP_INIT_QP,
335       g_param_spec_uint (
336           "init-qp",
337           "H264 init-qp",
338           "H264 init-qp",
339           1,
340           51,
341           GST_VAAPI_ENCODER_H264_DEFAULT_INIT_QP,
342           G_PARAM_READWRITE));
343
344     g_object_class_install_property (
345       object_class,
346       H264_PROP_MIN_QP,
347       g_param_spec_uint (
348           "min-qp",
349           "H264 min-qp",
350           "H264 min-qp",
351           1,
352           51,
353           GST_VAAPI_ENCODER_H264_DEFAULT_MIN_QP,
354           G_PARAM_READWRITE));
355
356     g_object_class_install_property (
357       object_class,
358       H264_PROP_SLICE_NUM,
359       g_param_spec_uint (
360           "slice-num",
361           "H264 slice num",
362           "H264 slice num",
363           1,
364           200,
365           1,
366           G_PARAM_READWRITE));
367
368     g_object_class_install_property (
369       object_class,
370       H264_PROP_B_FRAME_NUM,
371       g_param_spec_uint (
372           "b-frame-num",
373           "B frams num",
374           "B frams num",
375           0,
376           10,
377           0,
378           G_PARAM_READWRITE));
379 }