Drop excessive threading that over-complicates synchronisation.
[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 (*GetVideoDataFunc)(const guchar **data, guint *size);
32
33 typedef struct _CodecDefs CodecDefs;
34 struct _CodecDefs {
35     const gchar        *codec_str;
36     GstVaapiCodec       codec;
37     GetVideoDataFunc    get_video_data;
38 };
39
40 static const CodecDefs g_codec_defs[] = {
41     { "mpeg2",  GST_VAAPI_CODEC_MPEG2,  mpeg2_get_video_data    },
42     { "h264",   GST_VAAPI_CODEC_H264,   h264_get_video_data     },
43     { "vc1",    GST_VAAPI_CODEC_VC1,    vc1_get_video_data      },
44     { NULL, }
45 };
46
47 static const CodecDefs *
48 get_codec_defs(const gchar *codec_str)
49 {
50     const CodecDefs *c;
51     for (c = g_codec_defs; c->codec_str; c++)
52         if (strcmp(codec_str, c->codec_str) == 0)
53             return c;
54     return NULL;
55 }
56
57 static inline void pause(void)
58 {
59     g_print("Press any key to continue...\n");
60     getchar();
61 }
62
63 static gchar *g_codec_str;
64
65 static GOptionEntry g_options[] = {
66     { "codec", 'c',
67       0,
68       G_OPTION_ARG_STRING, &g_codec_str,
69       "codec to test", NULL },
70     { NULL, }
71 };
72
73 int
74 main(int argc, char *argv[])
75 {
76     GOptionContext       *options;
77     GstVaapiDisplay      *display;
78     GstVaapiWindow       *window;
79     GstVaapiDecoder      *decoder;
80     GstVaapiDecoderStatus status;
81     const CodecDefs      *codec;
82     GstVaapiSurfaceProxy *proxy;
83     const guchar         *vdata;
84     guint                 vdata_size;
85
86     static const guint win_width  = 640;
87     static const guint win_height = 480;
88
89     gst_init(&argc, &argv);
90
91     options = g_option_context_new(" - test-decode options");
92     g_option_context_add_main_entries(options, g_options, NULL);
93     g_option_context_parse(options, &argc, &argv, NULL);
94     g_option_context_free(options);
95
96     if (!g_codec_str)
97         g_codec_str = g_strdup("h264");
98
99     g_print("Test %s decode\n", g_codec_str);
100     codec = get_codec_defs(g_codec_str);
101     if (!codec)
102         g_error("no %s codec data found", g_codec_str);
103
104     display = gst_vaapi_display_x11_new(NULL);
105     if (!display)
106         g_error("could not create VA display");
107
108     window = gst_vaapi_window_x11_new(display, win_width, win_height);
109     if (!window)
110         g_error("could not create window");
111
112     codec->get_video_data(&vdata, &vdata_size);
113     decoder = gst_vaapi_decoder_ffmpeg_new(display, codec->codec, NULL);
114     if (!decoder)
115         g_error("could not create FFmpeg decoder");
116
117     if (!gst_vaapi_decoder_put_buffer_data(decoder, vdata, vdata_size))
118         g_error("could not send video data to the decoder");
119     if (!gst_vaapi_decoder_put_buffer(decoder, NULL))
120         g_error("could not send EOS to the decoder");
121
122     proxy = gst_vaapi_decoder_get_surface(decoder, &status);
123     if (!proxy)
124         g_error("could not get decoded surface (decoder status %d)", status);
125
126     gst_vaapi_window_show(window);
127
128     if (!gst_vaapi_window_put_surface(window,
129                                       GST_VAAPI_SURFACE_PROXY_SURFACE(proxy),
130                                       NULL,
131                                       NULL,
132                                       GST_VAAPI_PICTURE_STRUCTURE_FRAME))
133         g_error("could not render surface");
134
135     pause();
136
137     g_object_unref(proxy);
138     g_object_unref(decoder);
139     g_object_unref(window);
140     g_object_unref(display);
141     g_free(g_codec_str);
142     gst_deinit();
143     return 0;
144 }