Add FFmpeg/VAAPI decoder for the new `vaapidecode' element.
[platform/upstream/gstreamer-vaapi.git] / gst / vaapidecode / gstvaapidecode.c
1 /*
2  *  gstvaapidecode.c - VA-API video decoder
3  *
4  *  gstreamer-vaapi (C) 2010 Splitted-Desktop Systems
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program 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
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
19  */
20
21 /**
22  * SECTION:gstvaapidecode
23  * @short_description: A VA-API based video decoder
24  *
25  * vaapidecode decodes from raw bitstreams to surfaces suitable for
26  * the vaapisink element.
27  */
28
29 #include "config.h"
30 #include "gstvaapidecode.h"
31 #include <gst/vaapi/gstvaapivideosink.h>
32 #include <gst/vaapi/gstvaapivideobuffer.h>
33 #include <gst/vaapi/gstvaapidecoder_ffmpeg.h>
34
35 #define GST_PLUGIN_NAME "vaapidecode"
36 #define GST_PLUGIN_DESC "A VA-API based video decoder"
37
38 GST_DEBUG_CATEGORY_STATIC(gst_debug_vaapidecode);
39 #define GST_CAT_DEFAULT gst_debug_vaapidecode
40
41 /* ElementFactory information */
42 static const GstElementDetails gst_vaapidecode_details =
43     GST_ELEMENT_DETAILS(
44         "Video decode",
45         "Codec/Decoder/Video",
46         GST_PLUGIN_DESC,
47         "Gwenole Beauchesne <gbeauchesne@splitted-desktop.com>");
48
49 /* Default templates */
50 #define GST_CAPS_CODEC(CODEC)                   \
51     CODEC ", "                                  \
52     "width = (int) [ 1, MAX ], "                \
53     "height = (int) [ 1, MAX ]; "
54
55 static const char gst_vaapidecode_sink_caps_str[] =
56     GST_CAPS_CODEC("video/mpeg, mpegversion=2")
57     GST_CAPS_CODEC("video/mpeg, mpegversion=4")
58     GST_CAPS_CODEC("video/x-h263")
59     GST_CAPS_CODEC("video/x-h264")
60     GST_CAPS_CODEC("video/x-vc1")
61     ;
62
63 static const char gst_vaapidecode_src_caps_str[] =
64     "video/x-vaapi-surface, "
65     "width = (int) [ 1, MAX ], "
66     "height = (int) [ 1, MAX ]; ";
67
68 static GstStaticPadTemplate gst_vaapidecode_sink_factory =
69     GST_STATIC_PAD_TEMPLATE(
70         "sink",
71         GST_PAD_SINK,
72         GST_PAD_ALWAYS,
73         GST_STATIC_CAPS(gst_vaapidecode_sink_caps_str));
74
75 static GstStaticPadTemplate gst_vaapidecode_src_factory =
76     GST_STATIC_PAD_TEMPLATE(
77         "src",
78         GST_PAD_SRC,
79         GST_PAD_ALWAYS,
80         GST_STATIC_CAPS(gst_vaapidecode_src_caps_str));
81
82 GST_BOILERPLATE(
83     GstVaapiDecode,
84     gst_vaapidecode,
85     GstElement,
86     GST_TYPE_ELEMENT);
87
88 enum {
89     PROP_0,
90
91     PROP_USE_FFMPEG,
92 };
93
94 static void
95 gst_vaapidecode_task_cb(gpointer data)
96 {
97     GstVaapiDecode * const decode = GST_VAAPIDECODE(data);
98     GstVaapiDecoderStatus status;
99     GstVaapiSurfaceProxy *proxy;
100     GstBuffer *buffer;
101     GstFlowReturn ret;
102
103     proxy = gst_vaapi_decoder_timed_get_surface(decode->decoder, 10000, &status);
104     if (!proxy || status != GST_VAAPI_DECODER_STATUS_SUCCESS)
105         return;
106
107     buffer = NULL;
108     ret = gst_pad_alloc_buffer(decode->srcpad, 0, 0, GST_PAD_CAPS(decode->srcpad), &buffer);
109     if (ret != GST_FLOW_OK || !buffer)
110         goto error_create_buffer;
111
112     GST_BUFFER_TIMESTAMP(buffer) = GST_VAAPI_SURFACE_PROXY_TIMESTAMP(proxy);
113
114     ret = gst_pad_push(decode->srcpad, buffer);
115     if (ret != GST_FLOW_OK)
116         goto error_commit_buffer;
117
118     g_object_unref(proxy);
119     return;
120
121     /* ERRORS */
122 error_create_buffer:
123     {
124         const GstVaapiID surface_id =
125             gst_vaapi_surface_get_id(GST_VAAPI_SURFACE_PROXY_SURFACE(proxy));
126
127         GST_DEBUG("video sink failed to create video buffer for proxy'ed "
128                   "surface %" GST_VAAPI_ID_FORMAT " (error %d)",
129                   GST_VAAPI_ID_ARGS(surface_id), ret);
130         g_object_unref(proxy);
131         return;
132     }
133 error_commit_buffer:
134     {
135         GST_DEBUG("video sink rejected the video buffer (error %d)", ret);
136         g_object_unref(proxy);
137         return;
138     }
139 }
140
141 static gboolean
142 gst_vaapidecode_start(GstVaapiDecode *decode)
143 {
144     if (gst_task_get_state(decode->decoder_task) == GST_TASK_STARTED)
145         return TRUE;
146
147     GST_DEBUG("start decoding threads");
148     if (!gst_vaapi_decoder_start(decode->decoder))
149         return FALSE;
150     return gst_task_start(decode->decoder_task);
151 }
152
153 static gboolean
154 gst_vaapidecode_pause(GstVaapiDecode *decode)
155 {
156     if (gst_task_get_state(decode->decoder_task) == GST_TASK_PAUSED)
157         return TRUE;
158
159     GST_DEBUG("pause decoding threads");
160     if (!gst_vaapi_decoder_pause(decode->decoder))
161         return FALSE;
162     return gst_task_pause(decode->decoder_task);
163 }
164
165 static gboolean
166 gst_vaapidecode_stop(GstVaapiDecode *decode)
167 {
168     if (gst_task_get_state(decode->decoder_task) == GST_TASK_STOPPED)
169         return TRUE;
170
171     GST_DEBUG("stop decoding threads");
172     if (!gst_vaapi_decoder_stop(decode->decoder))
173         return FALSE;
174     return gst_task_stop(decode->decoder_task);
175 }
176
177 static gboolean
178 gst_vaapidecode_create(GstVaapiDecode *decode)
179 {
180     GstVaapiVideoSink *sink;
181     GstVaapiDisplay *display;
182     GstVaapiCodec codec;
183
184     /* Look for a downstream vaapisink */
185     sink = gst_vaapi_video_sink_lookup(GST_ELEMENT(decode));
186     if (!sink)
187         return FALSE;
188
189     display = gst_vaapi_video_sink_get_display(sink);
190     if (!display)
191         return FALSE;
192
193     decode->display = g_object_ref(display);
194
195     codec = gst_vaapi_profile_get_codec(decode->profile);
196     if (!codec)
197         return FALSE;
198
199     if (decode->use_ffmpeg)
200         decode->decoder =
201             gst_vaapi_decoder_ffmpeg_new(display, codec, decode->codec_data);
202     if (!decode->decoder)
203         return FALSE;
204
205     decode->decoder_task = gst_task_create(gst_vaapidecode_task_cb, decode);
206     if (!decode->decoder_task)
207         return FALSE;
208
209     gst_task_set_lock(decode->decoder_task, &decode->decoder_task_lock);
210     return TRUE;
211 }
212
213 static void
214 gst_vaapidecode_destroy(GstVaapiDecode *decode)
215 {
216     if (decode->decoder_task) {
217         gst_task_join(decode->decoder_task);
218         g_object_unref(decode->decoder_task);
219         decode->decoder_task = NULL;
220     }
221
222     if (decode->decoder) {
223         gst_vaapi_decoder_put_buffer(decode->decoder, NULL);
224         g_object_unref(decode->decoder);
225         decode->decoder = NULL;
226     }
227
228     if (decode->codec_data) {
229         gst_buffer_unref(decode->codec_data);
230         decode->codec_data = NULL;
231     }
232
233     if (decode->display) {
234         g_object_unref(decode->display);
235         decode->display = NULL;
236     }
237 }
238
239 static void gst_vaapidecode_base_init(gpointer klass)
240 {
241     GstElementClass * const element_class = GST_ELEMENT_CLASS(klass);
242
243     gst_element_class_set_details(element_class, &gst_vaapidecode_details);
244
245     /* sink pad */
246     gst_element_class_add_pad_template(
247         element_class,
248         gst_static_pad_template_get(&gst_vaapidecode_sink_factory)
249     );
250
251     /* src pad */
252     gst_element_class_add_pad_template(
253         element_class,
254         gst_static_pad_template_get(&gst_vaapidecode_src_factory)
255     );
256 }
257
258 static void
259 gst_vaapidecode_finalize(GObject *object)
260 {
261     gst_vaapidecode_destroy(GST_VAAPIDECODE(object));
262
263     G_OBJECT_CLASS(parent_class)->finalize(object);
264 }
265
266 static void
267 gst_vaapidecode_set_property(
268     GObject      *object,
269     guint         prop_id,
270     const GValue *value,
271     GParamSpec   *pspec
272 )
273 {
274     GstVaapiDecode * const decode = GST_VAAPIDECODE(object);
275
276     switch (prop_id) {
277     case PROP_USE_FFMPEG:
278         decode->use_ffmpeg = g_value_get_boolean(value);
279         break;
280     default:
281         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
282         break;
283     }
284 }
285
286 static void
287 gst_vaapidecode_get_property(
288     GObject    *object,
289     guint       prop_id,
290     GValue     *value,
291     GParamSpec *pspec
292 )
293 {
294     GstVaapiDecode * const decode = GST_VAAPIDECODE(object);
295
296     switch (prop_id) {
297     case PROP_USE_FFMPEG:
298         g_value_set_boolean(value, decode->use_ffmpeg);
299         break;
300     default:
301         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
302         break;
303     }
304 }
305
306 static GstStateChangeReturn
307 gst_vaapidecode_change_state(GstElement *element, GstStateChange transition)
308 {
309     GstVaapiDecode * const decode = GST_VAAPIDECODE(element);
310     GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
311
312     switch (transition) {
313     case GST_STATE_CHANGE_READY_TO_PAUSED:
314         break;
315     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
316         if (!gst_vaapidecode_start(decode))
317             return GST_STATE_CHANGE_FAILURE;
318         break;
319     default:
320         break;
321     }
322
323     ret = GST_ELEMENT_CLASS(parent_class)->change_state(element, transition);
324     if (ret != GST_STATE_CHANGE_SUCCESS)
325         return ret;
326
327     switch (transition) {
328     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
329         if (!gst_vaapidecode_pause(decode))
330             return GST_STATE_CHANGE_FAILURE;
331         break;
332     case GST_STATE_CHANGE_PAUSED_TO_READY:
333         if (!gst_vaapidecode_stop(decode))
334             return GST_STATE_CHANGE_FAILURE;
335         break;
336     default:
337         break;
338     }
339     return GST_STATE_CHANGE_SUCCESS;
340 }
341
342 static void
343 gst_vaapidecode_class_init(GstVaapiDecodeClass *klass)
344 {
345     GObjectClass * const object_class = G_OBJECT_CLASS(klass);
346     GstElementClass * const element_class = GST_ELEMENT_CLASS(klass);
347
348     object_class->finalize      = gst_vaapidecode_finalize;
349     object_class->set_property  = gst_vaapidecode_set_property;
350     object_class->get_property  = gst_vaapidecode_get_property;
351
352     element_class->change_state = gst_vaapidecode_change_state;
353
354     g_object_class_install_property
355         (object_class,
356          PROP_USE_FFMPEG,
357          g_param_spec_boolean("use-ffmpeg",
358                               "Use FFmpeg/VAAPI for decoding",
359                               "Uses FFmpeg/VAAPI for decoding",
360                               TRUE,
361                               G_PARAM_READWRITE));
362 }
363
364 static gboolean
365 gst_vaapidecode_set_caps(GstPad *pad, GstCaps *caps)
366 {
367     GstVaapiDecode * const decode = GST_VAAPIDECODE(GST_OBJECT_PARENT(pad));
368     GstPad *other_pad;
369     GstCaps *other_caps = NULL;
370     GstStructure *structure;
371     const GValue *v_width, *v_height, *v_framerate, *v_par, *v_codec_data;
372
373     if (pad == decode->sinkpad) {
374         other_pad  = decode->srcpad;
375         other_caps = gst_caps_from_string(gst_vaapidecode_src_caps_str);
376     }
377     else {
378         other_pad  = decode->sinkpad;
379         other_caps = gst_caps_from_string(gst_vaapidecode_sink_caps_str);
380     }
381
382     /* Negotiation succeeded, so now configure ourselves */
383     structure    = gst_caps_get_structure(caps, 0);
384     v_width      = gst_structure_get_value(structure, "width");
385     v_height     = gst_structure_get_value(structure, "height");
386     v_framerate  = gst_structure_get_value(structure, "framerate");
387     v_par        = gst_structure_get_value(structure, "pixel-aspect-ratio");
388     v_codec_data = gst_structure_get_value(structure, "codec_data");
389
390     if (pad == decode->sinkpad) {
391         decode->profile = gst_vaapi_profile_from_caps(caps);
392         if (v_codec_data)
393             decode->codec_data =
394                 gst_buffer_ref(gst_value_get_buffer(v_codec_data));
395     }
396
397     structure = gst_caps_get_structure(other_caps, 0);
398     gst_structure_set_value(structure, "width", v_width);
399     gst_structure_set_value(structure, "height", v_height);
400     if (v_framerate)
401         gst_structure_set_value(structure, "framerate", v_framerate);
402     if (v_par)
403         gst_structure_set_value(structure, "pixel-aspect-ratio", v_par);
404
405     return gst_pad_set_caps(other_pad, other_caps);
406 }
407
408 static GstFlowReturn
409 gst_vaapidecode_chain(GstPad *pad, GstBuffer *buf)
410 {
411     GstVaapiDecode * const decode = GST_VAAPIDECODE(GST_OBJECT_PARENT(pad));
412
413     if (!decode->decoder) {
414         if (!gst_vaapidecode_create(decode))
415             goto error_create_decoder;
416     }
417
418     if (!gst_vaapidecode_start(decode))
419         goto error_start_decoder;
420
421     if (!gst_vaapi_decoder_put_buffer(decode->decoder, buf))
422         goto error_push_buffer;
423
424     gst_buffer_unref(buf);
425     return GST_FLOW_OK;
426
427     /* ERRORS */
428 error_create_decoder:
429     {
430         GST_DEBUG("failed to create decoder");
431         gst_buffer_unref(buf);
432         return GST_FLOW_UNEXPECTED;
433     }
434 error_start_decoder:
435     {
436         GST_DEBUG("failed to start decoder");
437         gst_buffer_unref(buf);
438         return GST_FLOW_UNEXPECTED;
439     }
440 error_push_buffer:
441     {
442         GST_DEBUG("failed to push input buffer to decoder");
443         gst_buffer_unref(buf);
444         return GST_FLOW_UNEXPECTED;
445     }
446 }
447
448 static gboolean
449 gst_vaapidecode_sink_event(GstPad *pad, GstEvent *event)
450 {
451     GstVaapiDecode * const decode = GST_VAAPIDECODE(GST_OBJECT_PARENT(pad));
452
453     GST_DEBUG("handle sink event '%s'", GST_EVENT_TYPE_NAME(event));
454
455     /* Propagate event downstream */
456     return gst_pad_push_event(decode->srcpad, event);
457 }
458
459 static gboolean
460 gst_vaapidecode_src_event(GstPad *pad, GstEvent *event)
461 {
462     GstVaapiDecode * const decode = GST_VAAPIDECODE(GST_OBJECT_PARENT(pad));
463
464     GST_DEBUG("handle src event '%s'", GST_EVENT_TYPE_NAME(event));
465
466     /* Propagate event upstream */
467     return gst_pad_push_event(decode->sinkpad, event);
468 }
469
470 static void
471 gst_vaapidecode_init(GstVaapiDecode *decode, GstVaapiDecodeClass *klass)
472 {
473     GstElementClass * const element_class = GST_ELEMENT_CLASS(klass);
474
475     decode->display             = NULL;
476     decode->profile             = 0;
477     decode->codec_data          = NULL;
478     decode->decoder             = NULL;
479     decode->decoder_task        = NULL;
480     decode->use_ffmpeg          = TRUE;
481
482     g_static_rec_mutex_init(&decode->decoder_task_lock);
483
484     /* Pad through which data comes in to the element */
485     decode->sinkpad = gst_pad_new_from_template(
486         gst_element_class_get_pad_template(element_class, "sink"),
487         "sink"
488     );
489
490     gst_pad_set_setcaps_function(decode->sinkpad, gst_vaapidecode_set_caps);
491     gst_pad_set_chain_function(decode->sinkpad, gst_vaapidecode_chain);
492     gst_pad_set_event_function(decode->sinkpad, gst_vaapidecode_sink_event);
493     gst_element_add_pad(GST_ELEMENT(decode), decode->sinkpad);
494
495     /* Pad through which data goes out of the element */
496     decode->srcpad = gst_pad_new_from_template(
497         gst_element_class_get_pad_template(element_class, "src"),
498         "src"
499     );
500
501     gst_pad_set_event_function(decode->srcpad, gst_vaapidecode_src_event);
502     gst_element_add_pad(GST_ELEMENT(decode), decode->srcpad);
503 }
504
505 static gboolean plugin_init(GstPlugin *plugin)
506 {
507     GST_DEBUG_CATEGORY_INIT(gst_debug_vaapidecode,
508                             GST_PLUGIN_NAME, 0, GST_PLUGIN_DESC);
509
510     return gst_element_register(plugin,
511                                 GST_PLUGIN_NAME,
512                                 GST_RANK_PRIMARY,
513                                 GST_TYPE_VAAPIDECODE);
514 }
515
516 GST_PLUGIN_DEFINE(
517     GST_VERSION_MAJOR, GST_VERSION_MINOR,
518     GST_PLUGIN_NAME,
519     GST_PLUGIN_DESC,
520     plugin_init,
521     PACKAGE_VERSION,
522     "GPL",
523     PACKAGE,
524     PACKAGE_BUGREPORT);