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