h264encoder: auto-detect stream-format: avc/byte-stream by next linked-pad's caps
[vaapi:windyuan-gstreamer-vaapi.git] / gst / vaapiencode / gstvaapimpeg4encode.c
1 /*
2  *  gstvaapimpeg4encode.c - VA-API MPEG-4 encoder
3  *
4  *  Copyright (C) 2011 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 "gstvaapimpeg4encode.h"
23 #include "gstvaapimpeg4encoder.h"
24
25 GST_DEBUG_CATEGORY_STATIC (gst_vaapi_mpeg4_encode_debug);
26 #define GST_CAT_DEFAULT gst_vaapi_mpeg4_encode_debug
27
28
29 static const char gst_mpeg4encode_sink_caps_str[] =
30     GST_VAAPI_SURFACE_CAPS;
31
32 static const GstElementDetails gst_mpeg4encode_details =
33     GST_ELEMENT_DETAILS(
34         "VA-API mpeg4 encoder",
35         "Codec/Encoder/Video",
36         "A VA-API based mpeg4 encoder",
37         "Feng Yuan<feng.yuan@intel.com>");
38
39
40 static const char gst_mpeg4encode_src_caps_str[] =
41     GST_CAPS_CODEC("video/mpeg, mpegversion=4");
42
43 static GstStaticPadTemplate gst_mpeg4encode_sink_factory =
44     GST_STATIC_PAD_TEMPLATE(
45         "sink",
46         GST_PAD_SINK,
47         GST_PAD_ALWAYS,
48         GST_STATIC_CAPS(gst_mpeg4encode_sink_caps_str));
49
50 static GstStaticPadTemplate gst_mpeg4encode_src_factory =
51     GST_STATIC_PAD_TEMPLATE(
52         "src",
53         GST_PAD_SRC,
54         GST_PAD_ALWAYS,
55         GST_STATIC_CAPS(gst_mpeg4encode_src_caps_str));
56
57 static void gst_mpeg4encode_finalize(GObject *object);
58 static void gst_mpeg4encode_set_property(GObject *object, guint prop_id,
59     const GValue *value, GParamSpec *pspec);
60 static void gst_mpeg4encode_get_property (GObject * object, guint prop_id,
61     GValue * value, GParamSpec * pspec);
62
63
64 /* mpeg4 encode */
65 GST_BOILERPLATE(
66     GstMPEG4Encode,
67     gst_mpeg4encode,
68     GstVaapiEncode,
69     GST_TYPE_VAAPI_ENCODE);
70
71 enum {
72     MPEG4_PROP_0,
73     MPEG4_PROP_PROFILE,
74     MPEG4_PROP_BITRATE,
75     MPEG4_PROP_INTRA_PERIOD,
76     MPEG4_PROP_INIT_QP,
77     MPEG4_PROP_MIN_QP,
78 };
79
80
81 static void
82 gst_mpeg4encode_base_init(gpointer klass)
83 {
84   GstElementClass * const element_class = GST_ELEMENT_CLASS(klass);
85
86   gst_element_class_set_details(element_class, &gst_mpeg4encode_details);
87
88   /* sink pad */
89   gst_element_class_add_pad_template(
90       element_class,
91       gst_static_pad_template_get(&gst_mpeg4encode_sink_factory)
92   );
93
94   /* src pad */
95   gst_element_class_add_pad_template(
96       element_class,
97       gst_static_pad_template_get(&gst_mpeg4encode_src_factory)
98   );
99 }
100
101 static void
102 gst_mpeg4encode_class_init(GstMPEG4EncodeClass *klass)
103 {
104   GObjectClass * const object_class = G_OBJECT_CLASS(klass);
105
106   GST_DEBUG_CATEGORY_INIT (gst_vaapi_mpeg4_encode_debug, "vaapimpeg4encode", 0,
107       "vaapimpeg4encode element");
108
109   object_class->finalize      = gst_mpeg4encode_finalize;
110   object_class->set_property  = gst_mpeg4encode_set_property;
111   object_class->get_property  = gst_mpeg4encode_get_property;
112
113
114   g_object_class_install_property (object_class, MPEG4_PROP_PROFILE,
115           g_param_spec_uint ("profile",
116               "MPEG4 Profile",
117               "Profile supports: 2(Baseline), 3(ASP)",
118               2,
119               3,
120               2,
121               G_PARAM_READWRITE));
122     g_object_class_install_property (object_class, MPEG4_PROP_BITRATE,
123           g_param_spec_uint ("bitrate",
124               "MPEG4 encoding bitrate",
125               "MPEG4 encoding bitrate, 10k~100M, (0, auto-calculate)",
126               0,
127               100*1000*1000,
128               0,
129               G_PARAM_READWRITE));
130     g_object_class_install_property (object_class, MPEG4_PROP_INTRA_PERIOD,
131           g_param_spec_uint ("intra-period",
132               "MPEG4 encoding intra-period",
133               "MPEG4 encoding intra-period",
134               1,
135               300,
136               MPEG4_DEFAULT_INTRA_PERIOD,
137               G_PARAM_READWRITE));
138     g_object_class_install_property (object_class, MPEG4_PROP_INIT_QP,
139           g_param_spec_uint ("init-qp",
140               "MPEG4 init-qp",
141               "MPEG4 init-qp",
142               1,
143               51,
144               MPEG4_DEFAULT_INIT_QP,
145               G_PARAM_READWRITE));
146     g_object_class_install_property (object_class, MPEG4_PROP_MIN_QP,
147           g_param_spec_uint ("min-qp",
148               "MPEG4 min-qp",
149               "MPEG4 min-qp",
150               1,
151               51,
152               MPEG4_DEFAULT_MIN_QP,
153               G_PARAM_READWRITE));
154
155 }
156
157 static void
158 gst_mpeg4encode_init(GstMPEG4Encode *mpeg4_encode, GstMPEG4EncodeClass *klass)
159 {
160   GstVaapiEncode *encode = GST_VAAPI_ENCODE(mpeg4_encode);
161   encode->encoder = GST_VAAPI_ENCODER(gst_mpeg4_encoder_new());
162   ENCODER_ASSERT(encode->encoder);
163 }
164
165 static void
166 gst_mpeg4encode_finalize(GObject *object)
167 {
168   //GstMPEG4Encode * const mpeg4_encode = GST_MPEG4ENCODE(object);
169   G_OBJECT_CLASS(parent_class)->finalize(object);
170 }
171
172 static void
173 gst_mpeg4encode_set_property(GObject *object, guint prop_id,
174     const GValue *value, GParamSpec *pspec)
175 {
176   GstVaapiEncode *encode = GST_VAAPI_ENCODE(object);
177   GstMPEG4Encoder *mpeg4encoder = GST_MPEG4_ENCODER(encode->encoder);
178
179   ENCODER_ASSERT(mpeg4encoder);
180
181   switch (prop_id) {
182     case MPEG4_PROP_PROFILE: {
183       mpeg4encoder->profile = g_value_get_uint(value);
184     }
185       break;
186
187     case MPEG4_PROP_BITRATE: {
188       mpeg4encoder->bitrate = g_value_get_uint(value);
189     }
190       break;
191
192     case MPEG4_PROP_INTRA_PERIOD: {
193       mpeg4encoder->intra_period = g_value_get_uint(value);
194     }
195       break;
196
197     case MPEG4_PROP_INIT_QP: {
198       mpeg4encoder->init_qp = g_value_get_uint(value);
199     }
200       break;
201
202     case MPEG4_PROP_MIN_QP: {
203       mpeg4encoder->min_qp = g_value_get_uint(value);
204     }
205       break;
206
207     default:
208       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
209       break;
210   }
211 }
212
213 static void
214 gst_mpeg4encode_get_property (GObject * object, guint prop_id,
215     GValue * value, GParamSpec * pspec)
216 {
217   GstVaapiEncode *encode = GST_VAAPI_ENCODE(object);
218   GstMPEG4Encoder *mpeg4encoder = GST_MPEG4_ENCODER(encode->encoder);
219   ENCODER_ASSERT(mpeg4encoder);
220
221   switch (prop_id) {
222     case MPEG4_PROP_PROFILE:
223       g_value_set_uint (value, mpeg4encoder->profile);
224       break;
225
226     case MPEG4_PROP_BITRATE:
227       g_value_set_uint (value, mpeg4encoder->bitrate);
228       break;
229
230     case MPEG4_PROP_INTRA_PERIOD:
231       g_value_set_uint (value, mpeg4encoder->intra_period);
232       break;
233
234     case MPEG4_PROP_INIT_QP:
235       g_value_set_uint (value, mpeg4encoder->init_qp);
236       break;
237
238     case MPEG4_PROP_MIN_QP:
239       g_value_set_uint (value, mpeg4encoder->min_qp);
240       break;
241
242     default:
243       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
244       break;
245   }
246 }
247