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