~ubuntu-branches/ubuntu/quantal/gstreamer-vaapi/quantal

« back to all changes in this revision

Viewing changes to tests/test-decode.c

  • Committer: Package Import Robot
  • Author(s): Timo Aaltonen
  • Date: 2012-02-10 14:35:09 UTC
  • Revision ID: package-import@ubuntu.com-20120210143509-wq9j8uqb5leu1iik
Tags: upstream-0.3.4
ImportĀ upstreamĀ versionĀ 0.3.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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/gstvaapidisplay_x11.h>
 
26
#include <gst/vaapi/gstvaapiwindow_x11.h>
 
27
#include <gst/vaapi/gstvaapidecoder.h>
 
28
#include <gst/vaapi/gstvaapisurface.h>
 
29
#include "test-mpeg2.h"
 
30
#include "test-h264.h"
 
31
#include "test-vc1.h"
 
32
 
 
33
#if USE_FFMPEG
 
34
# include <gst/vaapi/gstvaapidecoder_ffmpeg.h>
 
35
#endif
 
36
#if USE_CODEC_PARSERS
 
37
# include <gst/vaapi/gstvaapidecoder_h264.h>
 
38
# include <gst/vaapi/gstvaapidecoder_mpeg2.h>
 
39
# include <gst/vaapi/gstvaapidecoder_vc1.h>
 
40
#endif
 
41
 
 
42
/* Set to 1 to check display cache works (shared VA display) */
 
43
#define CHECK_DISPLAY_CACHE 1
 
44
 
 
45
typedef void (*GetVideoInfoFunc)(VideoDecodeInfo *info);
 
46
 
 
47
typedef struct _CodecDefs CodecDefs;
 
48
struct _CodecDefs {
 
49
    const gchar        *codec_str;
 
50
    GetVideoInfoFunc    get_video_info;
 
51
};
 
52
 
 
53
static const CodecDefs g_codec_defs[] = {
 
54
#define INIT_FUNCS(CODEC) { #CODEC, CODEC##_get_video_info }
 
55
    INIT_FUNCS(mpeg2),
 
56
    INIT_FUNCS(h264),
 
57
    INIT_FUNCS(vc1),
 
58
#undef INIT_FUNCS
 
59
    { NULL, }
 
60
};
 
61
 
 
62
static const CodecDefs *
 
63
get_codec_defs(const gchar *codec_str)
 
64
{
 
65
    const CodecDefs *c;
 
66
    for (c = g_codec_defs; c->codec_str; c++)
 
67
        if (strcmp(codec_str, c->codec_str) == 0)
 
68
            return c;
 
69
    return NULL;
 
70
}
 
71
 
 
72
static inline void pause(void)
 
73
{
 
74
    g_print("Press any key to continue...\n");
 
75
    getchar();
 
76
}
 
77
 
 
78
static gchar *g_codec_str;
 
79
static gboolean g_use_ffmpeg = FALSE;
 
80
 
 
81
static GOptionEntry g_options[] = {
 
82
    { "codec", 'c',
 
83
      0,
 
84
      G_OPTION_ARG_STRING, &g_codec_str,
 
85
      "codec to test", NULL },
 
86
    { "ffmpeg", 0,
 
87
      0,
 
88
      G_OPTION_ARG_NONE, &g_use_ffmpeg,
 
89
      "use ffmpeg", NULL },
 
90
    { "codecparsers", 0,
 
91
      G_OPTION_FLAG_REVERSE,
 
92
      G_OPTION_ARG_NONE, &g_use_ffmpeg,
 
93
      "use codec parsers", NULL },
 
94
    { NULL, }
 
95
};
 
96
 
 
97
int
 
98
main(int argc, char *argv[])
 
99
{
 
100
    GOptionContext       *options;
 
101
    GstVaapiDisplay      *display, *display2;
 
102
    GstVaapiWindow       *window;
 
103
    GstVaapiDecoder      *decoder;
 
104
    GstCaps              *decoder_caps;
 
105
    GstStructure         *structure;
 
106
    GstVaapiDecoderStatus status;
 
107
    const CodecDefs      *codec;
 
108
    GstBuffer            *buffer;
 
109
    GstVaapiSurfaceProxy *proxy;
 
110
    VideoDecodeInfo       info;
 
111
 
 
112
    static const guint win_width  = 640;
 
113
    static const guint win_height = 480;
 
114
 
 
115
    gst_init(&argc, &argv);
 
116
 
 
117
    options = g_option_context_new(" - test-decode options");
 
118
    g_option_context_add_main_entries(options, g_options, NULL);
 
119
    g_option_context_parse(options, &argc, &argv, NULL);
 
120
    g_option_context_free(options);
 
121
 
 
122
    if (!g_codec_str)
 
123
        g_codec_str = g_strdup("h264");
 
124
 
 
125
    g_print("Test %s decode\n", g_codec_str);
 
126
    codec = get_codec_defs(g_codec_str);
 
127
    if (!codec)
 
128
        g_error("no %s codec data found", g_codec_str);
 
129
 
 
130
    display = gst_vaapi_display_x11_new(NULL);
 
131
    if (!display)
 
132
        g_error("could not create VA display");
 
133
 
 
134
    if (CHECK_DISPLAY_CACHE)
 
135
        display2 = gst_vaapi_display_x11_new(NULL);
 
136
    else
 
137
        display2 = g_object_ref(display);
 
138
    if (!display2)
 
139
        g_error("could not create second VA display");
 
140
 
 
141
    window = gst_vaapi_window_x11_new(display, win_width, win_height);
 
142
    if (!window)
 
143
        g_error("could not create window");
 
144
 
 
145
    codec->get_video_info(&info);
 
146
    decoder_caps = gst_vaapi_profile_get_caps(info.profile);
 
147
    if (!decoder_caps)
 
148
        g_error("could not create decoder caps");
 
149
 
 
150
    structure = gst_caps_get_structure(decoder_caps, 0);
 
151
    if (info.width > 0 && info.height > 0)
 
152
        gst_structure_set(
 
153
            structure,
 
154
            "width",  G_TYPE_INT, info.width,
 
155
            "height", G_TYPE_INT, info.height,
 
156
            NULL
 
157
        );
 
158
 
 
159
    if (g_use_ffmpeg) {
 
160
#if USE_FFMPEG
 
161
        decoder = gst_vaapi_decoder_ffmpeg_new(display, decoder_caps);
 
162
#else
 
163
        g_error("FFmpeg-based decoders are not supported");
 
164
#endif
 
165
    }
 
166
    else {
 
167
#if USE_CODEC_PARSERS
 
168
        switch (gst_vaapi_profile_get_codec(info.profile)) {
 
169
        case GST_VAAPI_CODEC_H264:
 
170
            decoder = gst_vaapi_decoder_h264_new(display, decoder_caps);
 
171
            break;
 
172
        case GST_VAAPI_CODEC_MPEG2:
 
173
            decoder = gst_vaapi_decoder_mpeg2_new(display, decoder_caps);
 
174
            break;
 
175
        case GST_VAAPI_CODEC_VC1:
 
176
            decoder = gst_vaapi_decoder_vc1_new(display, decoder_caps);
 
177
            break;
 
178
        default:
 
179
            decoder = NULL;
 
180
            break;
 
181
        }
 
182
#else
 
183
        g_error("codecparsers-based decoders are not supported");
 
184
#endif
 
185
    }
 
186
    if (!decoder)
 
187
        g_error("could not create decoder");
 
188
    gst_caps_unref(decoder_caps);
 
189
 
 
190
    buffer = gst_buffer_new();
 
191
    if (!buffer)
 
192
        g_error("could not create encoded data buffer");
 
193
    gst_buffer_set_data(buffer, (guchar *)info.data, info.data_size);
 
194
 
 
195
    if (!gst_vaapi_decoder_put_buffer(decoder, buffer))
 
196
        g_error("could not send video data to the decoder");
 
197
    gst_buffer_unref(buffer);
 
198
 
 
199
    if (!gst_vaapi_decoder_put_buffer(decoder, NULL))
 
200
        g_error("could not send EOS to the decoder");
 
201
 
 
202
    proxy = gst_vaapi_decoder_get_surface(decoder, &status);
 
203
    if (!proxy)
 
204
        g_error("could not get decoded surface (decoder status %d)", status);
 
205
 
 
206
    gst_vaapi_window_show(window);
 
207
 
 
208
    if (!gst_vaapi_window_put_surface(window,
 
209
                                      GST_VAAPI_SURFACE_PROXY_SURFACE(proxy),
 
210
                                      NULL,
 
211
                                      NULL,
 
212
                                      GST_VAAPI_PICTURE_STRUCTURE_FRAME))
 
213
        g_error("could not render surface");
 
214
 
 
215
    pause();
 
216
 
 
217
    g_object_unref(proxy);
 
218
    g_object_unref(decoder);
 
219
    g_object_unref(window);
 
220
    g_object_unref(display);
 
221
    g_object_unref(display2);
 
222
    g_free(g_codec_str);
 
223
    gst_deinit();
 
224
    return 0;
 
225
}