Fix make dist.
[profile/ivi/gstreamer-vaapi.git] / tests / test-decode.c
1 /*
2  *  test-decode.c - Test GstVaapiDecoder
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 #include <string.h>
22 #include <gst/vaapi/gstvaapidisplay_x11.h>
23 #include <gst/vaapi/gstvaapiwindow_x11.h>
24 #include <gst/vaapi/gstvaapidecoder.h>
25 #include <gst/vaapi/gstvaapidecoder_ffmpeg.h>
26 #include <gst/vaapi/gstvaapisurface.h>
27 #include "test-mpeg2.h"
28 #include "test-h264.h"
29 #include "test-vc1.h"
30
31 typedef void (*GetVideoInfoFunc)(VideoDecodeInfo *info);
32
33 typedef struct _CodecDefs CodecDefs;
34 struct _CodecDefs {
35     const gchar        *codec_str;
36     GetVideoInfoFunc    get_video_info;
37 };
38
39 static const CodecDefs g_codec_defs[] = {
40 #define INIT_FUNCS(CODEC) { #CODEC, CODEC##_get_video_info }
41     INIT_FUNCS(mpeg2),
42     INIT_FUNCS(h264),
43     INIT_FUNCS(vc1),
44 #undef INIT_FUNCS
45     { NULL, }
46 };
47
48 static const CodecDefs *
49 get_codec_defs(const gchar *codec_str)
50 {
51     const CodecDefs *c;
52     for (c = g_codec_defs; c->codec_str; c++)
53         if (strcmp(codec_str, c->codec_str) == 0)
54             return c;
55     return NULL;
56 }
57
58 static inline void pause(void)
59 {
60     g_print("Press any key to continue...\n");
61     getchar();
62 }
63
64 static gchar *g_codec_str;
65
66 static GOptionEntry g_options[] = {
67     { "codec", 'c',
68       0,
69       G_OPTION_ARG_STRING, &g_codec_str,
70       "codec to test", NULL },
71     { NULL, }
72 };
73
74 int
75 main(int argc, char *argv[])
76 {
77     GOptionContext       *options;
78     GstVaapiDisplay      *display;
79     GstVaapiWindow       *window;
80     GstVaapiDecoder      *decoder;
81     GstCaps              *decoder_caps;
82     GstStructure         *structure;
83     GstVaapiDecoderStatus status;
84     const CodecDefs      *codec;
85     GstBuffer            *buffer;
86     GstVaapiSurfaceProxy *proxy;
87     VideoDecodeInfo       info;
88
89     static const guint win_width  = 640;
90     static const guint win_height = 480;
91
92     gst_init(&argc, &argv);
93
94     options = g_option_context_new(" - test-decode options");
95     g_option_context_add_main_entries(options, g_options, NULL);
96     g_option_context_parse(options, &argc, &argv, NULL);
97     g_option_context_free(options);
98
99     if (!g_codec_str)
100         g_codec_str = g_strdup("h264");
101
102     g_print("Test %s decode\n", g_codec_str);
103     codec = get_codec_defs(g_codec_str);
104     if (!codec)
105         g_error("no %s codec data found", g_codec_str);
106
107     display = gst_vaapi_display_x11_new(NULL);
108     if (!display)
109         g_error("could not create VA display");
110
111     window = gst_vaapi_window_x11_new(display, win_width, win_height);
112     if (!window)
113         g_error("could not create window");
114
115     codec->get_video_info(&info);
116     decoder_caps = gst_vaapi_profile_get_caps(info.profile);
117     if (!decoder_caps)
118         g_error("could not create decoder caps");
119
120     structure = gst_caps_get_structure(decoder_caps, 0);
121     if (info.width > 0 && info.height > 0)
122         gst_structure_set(
123             structure,
124             "width",  G_TYPE_INT, info.width,
125             "height", G_TYPE_INT, info.height,
126             NULL
127         );
128
129     decoder = gst_vaapi_decoder_ffmpeg_new(display, decoder_caps);
130     if (!decoder)
131         g_error("could not create FFmpeg decoder");
132     gst_caps_unref(decoder_caps);
133
134     buffer = gst_buffer_new();
135     if (!buffer)
136         g_error("could not create encoded data buffer");
137     gst_buffer_set_data(buffer, (guchar *)info.data, info.data_size);
138
139     if (!gst_vaapi_decoder_put_buffer(decoder, buffer))
140         g_error("could not send video data to the decoder");
141     gst_buffer_unref(buffer);
142
143     if (!gst_vaapi_decoder_put_buffer(decoder, NULL))
144         g_error("could not send EOS to the decoder");
145
146     proxy = gst_vaapi_decoder_get_surface(decoder, &status);
147     if (!proxy)
148         g_error("could not get decoded surface (decoder status %d)", status);
149
150     gst_vaapi_window_show(window);
151
152     if (!gst_vaapi_window_put_surface(window,
153                                       GST_VAAPI_SURFACE_PROXY_SURFACE(proxy),
154                                       NULL,
155                                       NULL,
156                                       GST_VAAPI_PICTURE_STRUCTURE_FRAME))
157         g_error("could not render surface");
158
159     pause();
160
161     g_object_unref(proxy);
162     g_object_unref(decoder);
163     g_object_unref(window);
164     g_object_unref(display);
165     g_free(g_codec_str);
166     gst_deinit();
167     return 0;
168 }