configure: drop check for --enable-vaapi-glx.
[profile/ivi/gstreamer-vaapi.git] / tests / test-decode.c
1 /*
2  *  test-decode.c - Test GstVaapiDecoder
3  *
4  *  Copyright (C) 2010-2011 Splitted-Desktop Systems
5  *  Copyright (C) 2011-2012 Intel Corporation
6  *
7  *  This library is free software; you can redistribute it and/or
8  *  modify it under the terms of the GNU Lesser General Public License
9  *  as published by the Free Software Foundation; either version 2.1
10  *  of the License, or (at your option) any later version.
11  *
12  *  This library is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  *  Lesser General Public License for more details.
16  *
17  *  You should have received a copy of the GNU Lesser General Public
18  *  License along with this library; if not, write to the Free
19  *  Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  *  Boston, MA 02110-1301 USA
21 */
22
23 #include "config.h"
24 #include <string.h>
25 #include <gst/vaapi/gstvaapidecoder.h>
26 #include <gst/vaapi/gstvaapisurface.h>
27 #include <gst/vaapi/gstvaapidecoder_h264.h>
28 #include <gst/vaapi/gstvaapidecoder_jpeg.h>
29 #include <gst/vaapi/gstvaapidecoder_mpeg2.h>
30 #include <gst/vaapi/gstvaapidecoder_vc1.h>
31 #include "test-jpeg.h"
32 #include "test-mpeg2.h"
33 #include "test-h264.h"
34 #include "test-vc1.h"
35 #include "output.h"
36
37 /* Set to 1 to check display cache works (shared VA display) */
38 #define CHECK_DISPLAY_CACHE 1
39
40 typedef void (*GetVideoInfoFunc)(VideoDecodeInfo *info);
41
42 typedef struct _CodecDefs CodecDefs;
43 struct _CodecDefs {
44     const gchar        *codec_str;
45     GetVideoInfoFunc    get_video_info;
46 };
47
48 static const CodecDefs g_codec_defs[] = {
49 #define INIT_FUNCS(CODEC) { #CODEC, CODEC##_get_video_info }
50     INIT_FUNCS(jpeg),
51     INIT_FUNCS(mpeg2),
52     INIT_FUNCS(h264),
53     INIT_FUNCS(vc1),
54 #undef INIT_FUNCS
55     { NULL, }
56 };
57
58 static const CodecDefs *
59 get_codec_defs(const gchar *codec_str)
60 {
61     const CodecDefs *c;
62     for (c = g_codec_defs; c->codec_str; c++)
63         if (strcmp(codec_str, c->codec_str) == 0)
64             return c;
65     return NULL;
66 }
67
68 static inline void pause(void)
69 {
70     g_print("Press any key to continue...\n");
71     getchar();
72 }
73
74 static gchar *g_codec_str;
75
76 static GOptionEntry g_options[] = {
77     { "codec", 'c',
78       0,
79       G_OPTION_ARG_STRING, &g_codec_str,
80       "codec to test", NULL },
81     { NULL, }
82 };
83
84 int
85 main(int argc, char *argv[])
86 {
87     GstVaapiDisplay      *display, *display2;
88     GstVaapiWindow       *window;
89     GstVaapiDecoder      *decoder;
90     GstCaps              *decoder_caps;
91     GstStructure         *structure;
92     GstVaapiDecoderStatus status;
93     const CodecDefs      *codec;
94     GstBuffer            *buffer;
95     GstVaapiSurfaceProxy *proxy;
96     VideoDecodeInfo       info;
97
98     static const guint win_width  = 640;
99     static const guint win_height = 480;
100
101     if (!video_output_init(&argc, argv, g_options))
102         g_error("failed to initialize video output subsystem");
103
104     if (!g_codec_str)
105         g_codec_str = g_strdup("h264");
106
107     g_print("Test %s decode\n", g_codec_str);
108     codec = get_codec_defs(g_codec_str);
109     if (!codec)
110         g_error("no %s codec data found", g_codec_str);
111
112     display = video_output_create_display(NULL);
113     if (!display)
114         g_error("could not create VA display");
115
116     if (CHECK_DISPLAY_CACHE)
117         display2 = video_output_create_display(NULL);
118     else
119         display2 = g_object_ref(display);
120     if (!display2)
121         g_error("could not create second VA display");
122
123     window = video_output_create_window(display, win_width, win_height);
124     if (!window)
125         g_error("could not create window");
126
127     codec->get_video_info(&info);
128     decoder_caps = gst_vaapi_profile_get_caps(info.profile);
129     if (!decoder_caps)
130         g_error("could not create decoder caps");
131
132     structure = gst_caps_get_structure(decoder_caps, 0);
133     if (info.width > 0 && info.height > 0)
134         gst_structure_set(
135             structure,
136             "width",  G_TYPE_INT, info.width,
137             "height", G_TYPE_INT, info.height,
138             NULL
139         );
140
141     switch (gst_vaapi_profile_get_codec(info.profile)) {
142     case GST_VAAPI_CODEC_H264:
143         decoder = gst_vaapi_decoder_h264_new(display, decoder_caps);
144         break;
145 #if USE_JPEG_DECODER
146     case GST_VAAPI_CODEC_JPEG:
147         decoder = gst_vaapi_decoder_jpeg_new(display, decoder_caps);
148         break;
149 #endif
150     case GST_VAAPI_CODEC_MPEG2:
151         decoder = gst_vaapi_decoder_mpeg2_new(display, decoder_caps);
152         break;
153     case GST_VAAPI_CODEC_VC1:
154         decoder = gst_vaapi_decoder_vc1_new(display, decoder_caps);
155         break;
156     default:
157         decoder = NULL;
158         break;
159     }
160     if (!decoder)
161         g_error("could not create decoder");
162     gst_caps_unref(decoder_caps);
163
164     buffer = gst_buffer_new();
165     if (!buffer)
166         g_error("could not create encoded data buffer");
167     gst_buffer_set_data(buffer, (guchar *)info.data, info.data_size);
168
169     if (!gst_vaapi_decoder_put_buffer(decoder, buffer))
170         g_error("could not send video data to the decoder");
171     gst_buffer_unref(buffer);
172
173     if (!gst_vaapi_decoder_put_buffer(decoder, NULL))
174         g_error("could not send EOS to the decoder");
175
176     proxy = gst_vaapi_decoder_get_surface(decoder, &status);
177     if (!proxy)
178         g_error("could not get decoded surface (decoder status %d)", status);
179
180     gst_vaapi_window_show(window);
181
182     if (!gst_vaapi_window_put_surface(window,
183                                       GST_VAAPI_SURFACE_PROXY_SURFACE(proxy),
184                                       NULL,
185                                       NULL,
186                                       GST_VAAPI_PICTURE_STRUCTURE_FRAME))
187         g_error("could not render surface");
188
189     pause();
190
191     g_object_unref(proxy);
192     g_object_unref(decoder);
193     g_object_unref(window);
194     g_object_unref(display);
195     g_object_unref(display2);
196     g_free(g_codec_str);
197     video_output_exit();
198     return 0;
199 }