b84821c2c738dec263f5c664bb7c399fa5718b2e
[platform/upstream/gstreamer-vaapi.git] / gst / vaapi / gstvaapiencode_mpeg2.c
1 /*
2  *  gstvaapiencode_mpeg2.c - VA-API MPEG2 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/gstvaapidisplay.h>
24 #include <gst/vaapi/gstvaapiencoder_mpeg2.h>
25 #include "gstvaapiencode_mpeg2.h"
26 #include "gstvaapipluginutil.h"
27 #if GST_CHECK_VERSION(1,0,0)
28 #include "gstvaapivideomemory.h"
29 #endif
30
31 #define GST_PLUGIN_NAME "vaapiencode_mpeg2"
32 #define GST_PLUGIN_DESC "A VA-API based MPEG-2 video encoder"
33
34 GST_DEBUG_CATEGORY_STATIC (gst_vaapi_mpeg2_encode_debug);
35 #define GST_CAT_DEFAULT gst_vaapi_mpeg2_encode_debug
36
37 #define GST_CODEC_CAPS                          \
38   "video/mpeg, mpegversion = (int) 2, "         \
39   "systemstream = (boolean) false"
40
41 /* *INDENT-OFF* */
42 static const char gst_vaapiencode_mpeg2_sink_caps_str[] =
43 #if GST_CHECK_VERSION(1,1,0)
44   GST_VIDEO_CAPS_MAKE_WITH_FEATURES (GST_CAPS_FEATURE_MEMORY_VAAPI_SURFACE,
45       "{ ENCODED, NV12, I420, YV12 }") ", "
46 #else
47   GST_VAAPI_SURFACE_CAPS ", "
48 #endif
49   GST_CAPS_INTERLACED_FALSE "; "
50 #if GST_CHECK_VERSION(1,0,0)
51   GST_VIDEO_CAPS_MAKE (GST_VIDEO_FORMATS_ALL) ", "
52 #else
53   "video/x-raw-yuv, "
54   "width  = (int) [ 1, MAX ], "
55   "height = (int) [ 1, MAX ], "
56 #endif
57   GST_CAPS_INTERLACED_FALSE;
58 /* *INDENT-ON* */
59
60 /* *INDENT-OFF* */
61 static const char gst_vaapiencode_mpeg2_src_caps_str[] =
62   GST_CODEC_CAPS;
63 /* *INDENT-ON* */
64
65 /* *INDENT-OFF* */
66 static GstStaticPadTemplate gst_vaapiencode_mpeg2_sink_factory =
67   GST_STATIC_PAD_TEMPLATE ("sink",
68       GST_PAD_SINK,
69       GST_PAD_ALWAYS,
70       GST_STATIC_CAPS (gst_vaapiencode_mpeg2_sink_caps_str));
71 /* *INDENT-ON* */
72
73 /* *INDENT-OFF* */
74 static GstStaticPadTemplate gst_vaapiencode_mpeg2_src_factory =
75   GST_STATIC_PAD_TEMPLATE ("src",
76       GST_PAD_SRC,
77       GST_PAD_ALWAYS,
78       GST_STATIC_CAPS (gst_vaapiencode_mpeg2_src_caps_str));
79 /* *INDENT-ON* */
80
81 /* mpeg2 encode */
82 G_DEFINE_TYPE (GstVaapiEncodeMpeg2, gst_vaapiencode_mpeg2,
83     GST_TYPE_VAAPIENCODE);
84
85 static void
86 gst_vaapiencode_mpeg2_init (GstVaapiEncodeMpeg2 * encode)
87 {
88   gst_vaapiencode_init_properties (GST_VAAPIENCODE_CAST (encode));
89 }
90
91 static void
92 gst_vaapiencode_mpeg2_finalize (GObject * object)
93 {
94   G_OBJECT_CLASS (gst_vaapiencode_mpeg2_parent_class)->finalize (object);
95 }
96
97 static void
98 gst_vaapiencode_mpeg2_set_property (GObject * object,
99     guint prop_id, const GValue * value, GParamSpec * pspec)
100 {
101   GstVaapiEncodeClass *const encode_class = GST_VAAPIENCODE_GET_CLASS (object);
102   GstVaapiEncode *const base_encode = GST_VAAPIENCODE_CAST (object);
103
104   switch (prop_id) {
105     default:
106       if (!encode_class->set_property (base_encode, prop_id, value))
107         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
108       break;
109   }
110 }
111
112 static void
113 gst_vaapiencode_mpeg2_get_property (GObject * object,
114     guint prop_id, GValue * value, GParamSpec * pspec)
115 {
116   GstVaapiEncodeClass *const encode_class = GST_VAAPIENCODE_GET_CLASS (object);
117   GstVaapiEncode *const base_encode = GST_VAAPIENCODE_CAST (object);
118
119   switch (prop_id) {
120     default:
121       if (!encode_class->get_property (base_encode, prop_id, value))
122         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
123       break;
124   }
125 }
126
127 static GstCaps *
128 gst_vaapiencode_mpeg2_get_caps (GstVaapiEncode * base_encode)
129 {
130   GstCaps *caps;
131
132   caps = gst_caps_from_string (GST_CODEC_CAPS);
133
134   /* XXX: update profile and level information */
135   return caps;
136 }
137
138 static GstVaapiEncoder *
139 gst_vaapiencode_mpeg2_alloc_encoder (GstVaapiEncode * base,
140     GstVaapiDisplay * display)
141 {
142   return gst_vaapi_encoder_mpeg2_new (display);
143 }
144
145 static void
146 gst_vaapiencode_mpeg2_class_init (GstVaapiEncodeMpeg2Class * klass)
147 {
148   GObjectClass *const object_class = G_OBJECT_CLASS (klass);
149   GstElementClass *const element_class = GST_ELEMENT_CLASS (klass);
150   GstVaapiEncodeClass *const encode_class = GST_VAAPIENCODE_CLASS (klass);
151
152   GST_DEBUG_CATEGORY_INIT (gst_vaapi_mpeg2_encode_debug,
153       GST_PLUGIN_NAME, 0, GST_PLUGIN_DESC);
154
155   object_class->finalize = gst_vaapiencode_mpeg2_finalize;
156   object_class->set_property = gst_vaapiencode_mpeg2_set_property;
157   object_class->get_property = gst_vaapiencode_mpeg2_get_property;
158
159   encode_class->get_properties = gst_vaapi_encoder_mpeg2_get_default_properties;
160   encode_class->get_caps = gst_vaapiencode_mpeg2_get_caps;
161   encode_class->alloc_encoder = gst_vaapiencode_mpeg2_alloc_encoder;
162
163   gst_element_class_set_static_metadata (element_class,
164       "VA-API MPEG-2 encoder",
165       "Codec/Encoder/Video",
166       GST_PLUGIN_DESC, "Guangxin Xu <guangxin.xu@intel.com>");
167
168   /* sink pad */
169   gst_element_class_add_pad_template (element_class,
170       gst_static_pad_template_get (&gst_vaapiencode_mpeg2_sink_factory));
171
172   /* src pad */
173   gst_element_class_add_pad_template (element_class,
174       gst_static_pad_template_get (&gst_vaapiencode_mpeg2_src_factory));
175
176   gst_vaapiencode_class_init_properties (encode_class);
177 }