Tizen 2.0 Release
[framework/multimedia/gstreamer-vaapi.git] / gst-libs / gst / vaapi / gstvaapiencoder.h
1 /*
2  *  gstvaapiencoder.h - VA-API encoder interface
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 #ifndef GST_VAAPI_ENCODER_H
23 #define GST_VAAPI_ENCODER_H
24
25 #include <stdio.h>
26 #include <stdint.h>
27
28 #include "gst/gstinfo.h"
29 #include "gst/gstbuffer.h"
30
31 #include "gst/vaapi/gstvaapidisplay.h"
32 #include "gst/vaapi/gstvaapicontext.h"
33
34 G_BEGIN_DECLS
35
36 #define ENCODER_NO_ERROR       0
37 #define ENCODER_NO_DATA        1
38 #define ENCODER_NO_BUSY_BUF    2
39 #define ENCODER_NO_IDLE_BUF    3
40 #define ENCODER_FRAME_IN_ORDER 4
41
42 #define ENCODER_MEM_ERR       -1
43 #define ENCODER_DISPLAY_ERR   -2
44 #define ENCODER_CONFIG_ERR    -3
45 #define ENCODER_CONTEXT_ERR    -3
46 #define ENCODER_STATE_ERR     -4
47 #define ENCODER_ENC_RES_ERR   -5
48 #define ENCODER_PICTURE_ERR   -6
49 #define ENCODER_SURFACE_ERR   -7
50 #define ENCODER_QUERY_STATUS_ERR -8
51 #define ENCODER_DATA_NOT_READY   -9
52 #define ENCODER_DATA_ERR      -10
53 #define ENCODER_PROFILE_ERR   -11
54 #define ENCODER_PARAMETER_ERR -12
55 #define ENCODER_FUNC_PTR_ERR  -13
56
57 #ifdef DEBUG
58   #define ENCODER_LOG_ERROR(str_fmt,...)   \
59       fprintf(stdout, str_fmt "\n", ## __VA_ARGS__)
60   #define ENCODER_LOG_WARNING(str_fmt,...) \
61       fprintf(stdout, str_fmt "\n", ## __VA_ARGS__)
62   #define ENCODER_LOG_DEBUG(str_fmt,...)   \
63       fprintf(stdout, str_fmt "\n", ## __VA_ARGS__)
64   #define ENCODER_LOG_INFO(str_fmt,...)    \
65       fprintf(stdout, str_fmt "\n", ## __VA_ARGS__)
66 #else
67   #define ENCODER_LOG_ERROR(...)   GST_ERROR( __VA_ARGS__)
68   #define ENCODER_LOG_WARNING(...) GST_WARNING( __VA_ARGS__)
69   #define ENCODER_LOG_DEBUG(...)   GST_DEBUG( __VA_ARGS__)
70   #define ENCODER_LOG_INFO(...)    GST_INFO( __VA_ARGS__)
71 #endif
72
73 #define VAAPI_UNUSED_ARG(arg) (void)(arg)
74
75 #ifdef DEBUG
76 #include <assert.h>
77 #define ENCODER_ASSERT(exp) assert(exp)
78 #else
79 #define ENCODER_ASSERT(exp) g_assert(exp)
80 #endif
81
82 #define ENCODER_CHECK_STATUS(exp, err_num, err_reason, ...)  \
83   if (!(exp)) {                                   \
84     ENCODER_ASSERT(FALSE);                         \
85     ret = err_num;                                 \
86     ENCODER_LOG_ERROR(err_reason, ## __VA_ARGS__); \
87     goto end;                                      \
88   }
89
90 /* must have <gboolean is_locked = FALSE;> declared first*/
91 #define ENCODER_ACQUIRE_DISPLAY_LOCK(display)    \
92      if (!is_locked) {                     \
93       GST_VAAPI_DISPLAY_LOCK(display);     \
94       is_locked = TRUE;                    \
95      }
96
97 #define ENCODER_RELEASE_DISPLAY_LOCK(display)    \
98     if (is_locked) {                       \
99      GST_VAAPI_DISPLAY_UNLOCK(display);    \
100      is_locked = FALSE;                    \
101     }
102
103
104 typedef enum {
105   VAAPI_ENC_NULL,
106   VAAPI_ENC_INIT,
107   VAAPI_ENC_OPENED,
108   VAAPI_ENC_ENCODING,
109 } VAAPI_Encode_State;
110
111 typedef int                                  EncoderStatus;
112 typedef struct _GstVaapiEncoder              GstVaapiEncoder;
113 typedef struct _GstVaapiEncoderPrivate       GstVaapiEncoderPrivate;
114 typedef struct _GstVaapiEncoderClass         GstVaapiEncoderClass;
115
116 #define GST_TYPE_VAAPI_ENCODER \
117     (gst_vaapi_encoder_get_type())
118
119 #define GST_IS_VAAPI_ENCODER(obj) \
120     (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_VAAPI_ENCODER))
121
122 #define GST_IS_VAAPI_ENCODER_CLASS(klass) \
123     (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_VAAPI_ENCODER))
124
125 #define GST_VAAPI_ENCODER_GET_CLASS(obj)                 \
126     (G_TYPE_INSTANCE_GET_CLASS ((obj),                   \
127                                 GST_TYPE_VAAPI_ENCODER,  \
128                                 GstVaapiEncoderClass))
129
130 #define GST_VAAPI_ENCODER(obj)                           \
131     (G_TYPE_CHECK_INSTANCE_CAST ((obj),                  \
132                                  GST_TYPE_VAAPI_ENCODER, \
133                                  GstVaapiEncoder))
134
135 #define GST_VAAPI_ENCODER_CLASS(klass)                   \
136     (G_TYPE_CHECK_CLASS_CAST ((klass),                   \
137                               GST_TYPE_VAAPI_ENCODER,    \
138                               GstVaapiEncoderClass))
139
140 #define GST_VAAPI_ENCODER_GET_PRIVATE(obj)               \
141     (G_TYPE_INSTANCE_GET_PRIVATE((obj),                  \
142                                  GST_TYPE_VAAPI_ENCODER, \
143                                  GstVaapiEncoderPrivate))
144
145 #define GST_VAAPI_ENCODER_CAST(encoder) \
146     ((GstVaapiEncoder *)(encoder))
147
148 /* Get GstVaapiDisplay* */
149 #define ENCODER_DISPLAY(encoder) \
150     (((GstVaapiEncoder*)(encoder))->priv->display)
151
152 /* Get VADisplay */
153 #define ENCODER_VA_DISPLAY(encoder) \
154     (((GstVaapiEncoder*)(encoder))->priv->va_display)
155
156 /* Get GstVaapiContext* */
157 #define ENCODER_CONTEXT(encoder) \
158     (((GstVaapiEncoder*)(encoder))->priv->context)
159
160 /* Get VAContext */
161 #define ENCODER_VA_CONTEXT(encoder) \
162     (((GstVaapiEncoder*)(encoder))->priv->va_context)
163
164 #define ENCODER_WIDTH(encoder)   (((GstVaapiEncoder*)(encoder))->width)
165 #define ENCODER_HEIGHT(encoder)  (((GstVaapiEncoder*)(encoder))->height)
166 #define ENCODER_FPS(encoder)     (((GstVaapiEncoder*)(encoder))->frame_rate)
167 #define ENCODER_RATE_CONTROL(encoder)   \
168     (((GstVaapiEncoder*)(encoder))->rate_control)
169
170 struct _GstVaapiEncoder {
171   GObject parent;
172   GstVaapiEncoderPrivate *priv;
173
174   guint32 width;
175   guint32 height;
176   guint32 frame_rate;
177   GstVaapiRateControl rate_control;
178 };
179
180 struct _GstVaapiEncoderClass {
181   GObjectClass parent_class;
182
183   EncoderStatus (*initialize)    (GstVaapiEncoder* encoder);  /* can be NULL */
184   EncoderStatus (*uninitialize)  (GstVaapiEncoder* encoder);  /* can be NULL */
185
186   /* context [out] */
187   EncoderStatus (*open)          (GstVaapiEncoder* encoder,
188                                   GstVaapiContext **context);
189
190   EncoderStatus (*close)         (GstVaapiEncoder* encoder);
191   EncoderStatus (*encode)        (GstVaapiEncoder* encoder,
192                                   GstBuffer *pic);
193   /* buf [out] */
194   EncoderStatus (*get_buf)       (GstVaapiEncoder* encoder,
195                                   GstBuffer **buf);
196   EncoderStatus (*flush)         (GstVaapiEncoder* encoder);
197
198   /* get_codec_data can be NULL */
199   EncoderStatus (*get_codec_data)(GstVaapiEncoder* encoder, GstBuffer **codec_data);
200 };
201
202 struct _GstVaapiEncoderPrivate {
203   GstVaapiDisplay     *display;
204   VADisplay            va_display;
205   GstVaapiContext     *context;
206   VAContextID          va_context;
207   VAAPI_Encode_State   state;
208 };
209
210 GType
211 gst_vaapi_encoder_get_type(void);
212
213 /* set/get display */
214 gboolean
215 gst_vaapi_encoder_set_display(
216     GstVaapiEncoder* encoder,
217     GstVaapiDisplay *display
218 );
219 GstVaapiDisplay *
220 gst_vaapi_encoder_get_display(GstVaapiEncoder* encoder);
221
222 /* get context */
223 GstVaapiContext *
224 gst_vaapi_encoder_get_context(GstVaapiEncoder* encoder);
225
226 /* get encoding state */
227 VAAPI_Encode_State
228 gst_vaapi_encoder_get_state(GstVaapiEncoder* encoder);
229
230 /* check/open display */
231 EncoderStatus
232 gst_vaapi_encoder_initialize(GstVaapiEncoder* encoder);
233
234 /* check/open context */
235 EncoderStatus
236 gst_vaapi_encoder_open(GstVaapiEncoder* encoder);
237
238 /* encode one frame */
239 EncoderStatus
240 gst_vaapi_encoder_encode(
241     GstVaapiEncoder* encoder,
242     GstBuffer *pic
243 );
244
245 EncoderStatus
246 gst_vaapi_encoder_get_encoded_buffer(
247     GstVaapiEncoder* encoder,
248     GstBuffer **buf
249 );
250
251 EncoderStatus
252 gst_vaapi_encoder_get_codec_data(
253     GstVaapiEncoder* encoder,
254     GstBuffer **codec_data
255 );
256
257 /* flush all frames */
258 EncoderStatus
259 gst_vaapi_encoder_flush(GstVaapiEncoder* encoder);
260
261 /* close context */
262 EncoderStatus
263 gst_vaapi_encoder_close(GstVaapiEncoder* encoder);
264
265 EncoderStatus
266 gst_vaapi_encoder_render_picture(
267     GstVaapiEncoder *encoder,
268     GstVaapiSurface *buffer_surface,
269     VABufferID *bufs,
270     guint num
271 );
272
273 /* close display */
274 EncoderStatus
275 gst_vaapi_encoder_uninitialize(GstVaapiEncoder* encoder);
276
277 static inline void
278 gst_vaapi_encoder_unref (GstVaapiEncoder *encoder)
279 {
280   g_object_unref (encoder);
281 }
282
283 /* other functions */
284 char *vaapi_encoder_dump_bytes(const guint8 *buf, guint32 num);
285
286 G_END_DECLS
287
288 #endif /* GST_VAAPI_ENCODER_H */