add Intel Copyright
[profile/ivi/gstreamer-vaapi.git] / gst / vaapiencode / 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 #include "gst/vaapi/gstvaapidisplay.h"
31 #include "gst/vaapi/gstvaapicontext.h"
32
33 G_BEGIN_DECLS
34
35 #define ENCODER_NO_ERROR       0
36 #define ENCODER_BUFFER_WAITING 1
37 #define ENCODER_BUFFER_EMPTY   2
38
39
40 #define ENCODER_MEM_ERR       -1
41 #define ENCODER_DISPLAY_ERR   -2
42 #define ENCODER_CONFIG_ERR    -3
43 #define ENCODER_CONTEXT_ERR    -3
44 #define ENCODER_STATE_ERR     -4
45 #define ENCODER_ENC_RES_ERR   -5
46 #define ENCODER_PICTURE_ERR   -6
47 #define ENCODER_SURFACE_ERR   -7
48 #define ENCODER_QUERY_STATUS_ERR -8
49 #define ENCODER_DATA_NOT_READY   -9
50 #define ENCODER_DATA_ERR      -10
51 #define ENCODER_PROFILE_ERR   -11
52 #define ENCODER_PARAMETER_ERR -12
53 #define ENCODER_FUNC_PTR_ERR  -13
54
55 #ifdef DEBUG
56   #define ENCODER_LOG_ERROR(str_fmt,...)    fprintf(stdout, str_fmt "\n", ## __VA_ARGS__)
57   #define ENCODER_LOG_WARNING(str_fmt,...)  fprintf(stdout, str_fmt "\n", ## __VA_ARGS__)
58   #define ENCODER_LOG_DEBUG(str_fmt,...)    fprintf(stdout, str_fmt "\n", ## __VA_ARGS__)
59   #define ENCODER_LOG_INFO(str_fmt,...)     fprintf(stdout, str_fmt "\n", ## __VA_ARGS__)
60 #else
61   #define ENCODER_LOG_ERROR(...)   GST_ERROR( __VA_ARGS__)
62   #define ENCODER_LOG_WARNING(...) GST_WARNING( __VA_ARGS__)
63   #define ENCODER_LOG_DEBUG(...)   GST_DEBUG( __VA_ARGS__)
64   #define ENCODER_LOG_INFO(...)    GST_INFO( __VA_ARGS__)
65 #endif
66
67 #define VAAPI_UNUSED_ARG(arg) (void)(arg)
68
69 #ifdef DEBUG
70 #include <assert.h>
71 #define ENCODER_ASSERT(exp) assert(exp)
72 #else
73 #define ENCODER_ASSERT(exp) g_assert(exp)
74 #endif
75
76 #define ENCODER_CHECK_STATUS(exp, err_num, err_reason, ...)  \
77   if (!(exp)) {                                   \
78     ENCODER_ASSERT(FALSE);                         \
79     ret = err_num;                                 \
80     ENCODER_LOG_ERROR(err_reason, ## __VA_ARGS__); \
81     goto end;                                      \
82   }
83
84 /* must have <gboolean is_locked = FALSE;> declared first*/
85 #define ENCODER_ACQUIRE_DISPLAY_LOCK(display)    \
86      if (!is_locked) {                     \
87       GST_VAAPI_DISPLAY_LOCK(display);     \
88       is_locked = TRUE;                    \
89      }
90
91 #define ENCODER_RELEASE_DISPLAY_LOCK(display)    \
92     if (is_locked) {                       \
93      GST_VAAPI_DISPLAY_UNLOCK(display);    \
94      is_locked = FALSE;                    \
95     }
96
97
98 typedef enum {
99   VAAPI_ENC_NULL,
100   VAAPI_ENC_INIT,
101   VAAPI_ENC_OPENED,
102   VAAPI_ENC_ENCODING,
103 } VAAPI_Encode_State;
104
105 typedef int                                  EncoderStatus;
106 typedef struct _GstVaapiEncoder              GstVaapiEncoder;
107 typedef struct _GstVaapiEncoderPrivate       GstVaapiEncoderPrivate;
108 typedef struct _GstVaapiEncoderClass         GstVaapiEncoderClass;
109
110 #define GST_TYPE_VAAPI_ENCODER             (gst_vaapi_encoder_get_type())
111 #define GST_IS_VAAPI_ENCODER(obj)          (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_VAAPI_ENCODER))
112 #define GST_IS_VAAPI_ENCODER_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_VAAPI_ENCODER))
113 #define GST_VAAPI_ENCODER_GET_CLASS(obj)   (G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_VAAPI_ENCODER,  GstVaapiEncoderClass))
114 #define GST_VAAPI_ENCODER(obj)             (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_VAAPI_ENCODER, GstVaapiEncoder))
115 #define GST_VAAPI_ENCODER_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_VAAPI_ENCODER,  GstVaapiEncoderClass))
116 #define GST_VAAPI_ENCODER_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj),GST_TYPE_VAAPI_ENCODER,  GstVaapiEncoderPrivate))
117
118 #define ENCODER_DISPLAY(encoder) (GST_VAAPI_ENCODER_GET_PRIVATE(encoder)->display)
119 #define ENCODER_CONTEXT(encoder) (GST_VAAPI_ENCODER_GET_PRIVATE(encoder)->context)
120 #define ENCODER_WIDTH(encoder)   (((GstVaapiEncoder*)(encoder))->width)
121 #define ENCODER_HEIGHT(encoder)  (((GstVaapiEncoder*)(encoder))->height)
122 #define ENCODER_FPS(encoder)     (((GstVaapiEncoder*)(encoder))->frame_rate)
123
124 struct _GstVaapiEncoder {
125   GObject parent;
126
127   guint32 width;
128   guint32 height;
129   guint32 frame_rate;
130 };
131
132 struct _GstVaapiEncoderClass {
133   GObjectClass parent_class;
134
135   EncoderStatus (*initialize)    (GstVaapiEncoder* encoder, GstVaapiDisplay *display);  /* can be NULL */
136   EncoderStatus (*uninitialize)  (GstVaapiEncoder* encoder, GstVaapiDisplay *display);  /* can be NULL */
137
138   /* context [out] */
139   EncoderStatus (*open)          (GstVaapiEncoder* encoder, GstVaapiDisplay *display,
140                                   void* private_data, GstVaapiContext **context);
141
142   EncoderStatus (*close)         (GstVaapiEncoder* encoder, GstVaapiDisplay *display, GstVaapiContext *context);
143   /* coded_pics [out] */
144   EncoderStatus (*encode)        (GstVaapiEncoder* encoder, GstVaapiDisplay *display,
145                                   GstVaapiContext *context, GstBuffer *raw_pic, GList **coded_pics);
146   /* coded_pics [out] */
147   EncoderStatus (*flush)         (GstVaapiEncoder* encoder, GstVaapiDisplay *display,
148                                   GstVaapiContext *context, GList **coded_pics);
149
150   EncoderStatus (*get_codec_data)(GstVaapiEncoder* encoder, GstBuffer **codec_data); /* can be NULL */
151 };
152
153 struct _GstVaapiEncoderPrivate {
154   GstVaapiDisplay     *display;
155   GstVaapiContext     *context;
156   VAAPI_Encode_State   state;
157 };
158
159 GType         gst_vaapi_encoder_get_type(void);
160
161 /* set/get display */
162 gboolean      gst_vaapi_encoder_set_display(GstVaapiEncoder* encoder, GstVaapiDisplay *display);
163 GstVaapiDisplay   *gst_vaapi_encoder_get_display(GstVaapiEncoder* encoder);
164
165 /* get context */
166 GstVaapiContext   *gst_vaapi_encoder_get_context(GstVaapiEncoder* encoder);
167
168 /* get encoding state */
169 VAAPI_Encode_State gst_vaapi_encoder_get_state(GstVaapiEncoder* encoder);
170
171 /* check/open display */
172 EncoderStatus gst_vaapi_encoder_initialize(GstVaapiEncoder* encoder);
173
174 /* check/open context */
175 EncoderStatus gst_vaapi_encoder_open(GstVaapiEncoder* encoder, void* private_data);
176
177 /* encode one frame */
178 EncoderStatus gst_vaapi_encoder_encode(GstVaapiEncoder* encoder, GstBuffer *raw_pic, GList **coded_pics);
179
180 EncoderStatus gst_vaapi_encoder_get_codec_data(GstVaapiEncoder* encoder, GstBuffer **codec_data);
181
182 /* flush all frames */
183 EncoderStatus gst_vaapi_encoder_flush(GstVaapiEncoder* encoder, GList **coded_pics);
184
185 /* close context */
186 EncoderStatus gst_vaapi_encoder_close(GstVaapiEncoder* encoder);
187
188 /* close display */
189 EncoderStatus gst_vaapi_encoder_uninitialize(GstVaapiEncoder* encoder);
190
191 static inline void gst_vaapi_encoder_unref (GstVaapiEncoder *encoder)
192 {
193   g_object_unref (encoder);
194 }
195
196 /* other functions */
197 char *vaapi_encoder_dump_bytes(const guint8 *buf, guint32 num);
198
199
200 G_END_DECLS
201
202 #endif
203