tests: image: fix string representation for GstVideoFormat.
[platform/upstream/gstreamer-vaapi.git] / tests / decoder.c
1 /*
2  *  decoder.h - Decoder utilities for the tests
3  *
4  *  Copyright (C) 2013 Intel Corporation
5  *
6  *  This library is free software; you can redistribute it and/or
7  *  modify it under the terms of the GNU Lesser General Public License
8  *  as published by the Free Software Foundation; either version 2.1
9  *  of the License, or (at your option) any later version.
10  *
11  *  This library 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 GNU
14  *  Lesser General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Lesser General Public
17  *  License along with this library; if not, write to the Free
18  *  Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  *  Boston, MA 02110-1301 USA
20  */
21
22 #include "gst/vaapi/sysdeps.h"
23 #include <string.h>
24 #include <gst/vaapi/gstvaapidecoder_h264.h>
25 #include <gst/vaapi/gstvaapidecoder_jpeg.h>
26 #include <gst/vaapi/gstvaapidecoder_mpeg2.h>
27 #include <gst/vaapi/gstvaapidecoder_mpeg4.h>
28 #include <gst/vaapi/gstvaapidecoder_vc1.h>
29 #include "decoder.h"
30 #include "test-jpeg.h"
31 #include "test-mpeg2.h"
32 #include "test-mpeg4.h"
33 #include "test-h264.h"
34 #include "test-vc1.h"
35
36 typedef void (*GetVideoInfoFunc)(VideoDecodeInfo *info);
37
38 typedef struct _CodecDefs CodecDefs;
39 struct _CodecDefs {
40     const gchar        *codec_str;
41     GetVideoInfoFunc    get_video_info;
42 };
43
44 static const CodecDefs g_codec_defs[] = {
45 #define INIT_FUNCS(CODEC) { #CODEC, CODEC##_get_video_info }
46     INIT_FUNCS(jpeg),
47     INIT_FUNCS(mpeg2),
48     INIT_FUNCS(mpeg4),
49     INIT_FUNCS(h264),
50     INIT_FUNCS(vc1),
51 #undef INIT_FUNCS
52     { NULL, }
53 };
54
55 static const CodecDefs *
56 find_codec_defs(const gchar *codec_str)
57 {
58     const CodecDefs *c;
59     for (c = g_codec_defs; c->codec_str; c++)
60         if (strcmp(codec_str, c->codec_str) == 0)
61             return c;
62     return NULL;
63 }
64
65 static inline const CodecDefs *
66 get_codec_defs(GstVaapiDecoder *decoder)
67 {
68     return gst_vaapi_decoder_get_user_data(decoder);
69 }
70
71 static inline void
72 set_codec_defs(GstVaapiDecoder *decoder, const CodecDefs *c)
73 {
74     gst_vaapi_decoder_set_user_data(decoder, (gpointer)c);
75 }
76
77 GstVaapiDecoder *
78 decoder_new(GstVaapiDisplay *display, const gchar *codec_name)
79 {
80     GstVaapiDecoder *decoder;
81     const CodecDefs *codec;
82     GstCaps *caps;
83     VideoDecodeInfo info;
84
85     if (!codec_name)
86         codec_name = "h264";
87
88     codec = find_codec_defs(codec_name);
89     if (!codec) {
90         GST_ERROR("failed to find %s codec data", codec_name);
91         return NULL;
92     }
93
94     codec->get_video_info(&info);
95     caps = gst_vaapi_profile_get_caps(info.profile);
96     if (!caps) {
97         GST_ERROR("failed to create decoder caps");
98         return NULL;
99     }
100
101     if (info.width > 0 && info.height > 0)
102         gst_caps_set_simple(caps,
103             "width",  G_TYPE_INT, info.width,
104             "height", G_TYPE_INT, info.height,
105             NULL);
106
107     switch (gst_vaapi_profile_get_codec(info.profile)) {
108     case GST_VAAPI_CODEC_H264:
109         decoder = gst_vaapi_decoder_h264_new(display, caps);
110         break;
111 #if USE_JPEG_DECODER
112     case GST_VAAPI_CODEC_JPEG:
113         decoder = gst_vaapi_decoder_jpeg_new(display, caps);
114         break;
115 #endif
116     case GST_VAAPI_CODEC_MPEG2:
117         decoder = gst_vaapi_decoder_mpeg2_new(display, caps);
118         break;
119     case GST_VAAPI_CODEC_MPEG4:
120         decoder = gst_vaapi_decoder_mpeg4_new(display, caps);
121         break;
122     case GST_VAAPI_CODEC_VC1:
123         decoder = gst_vaapi_decoder_vc1_new(display, caps);
124         break;
125     default:
126         decoder = NULL;
127         break;
128     }
129     gst_caps_unref(caps);
130     if (!decoder) {
131         GST_ERROR("failed to create %s decoder", codec->codec_str);
132         return NULL;
133     }
134
135     set_codec_defs(decoder, codec);
136     return decoder;
137 }
138
139 gboolean
140 decoder_put_buffers(GstVaapiDecoder *decoder)
141 {
142     const CodecDefs *codec;
143     VideoDecodeInfo info;
144     GstBuffer *buffer;
145     gboolean success;
146
147     g_return_val_if_fail(decoder != NULL, FALSE);
148
149     codec = get_codec_defs(decoder);
150     g_return_val_if_fail(codec != NULL, FALSE);
151
152     codec->get_video_info(&info);
153     buffer = gst_buffer_new_wrapped_full(GST_MEMORY_FLAG_READONLY,
154         (guchar *)info.data, info.data_size, 0, info.data_size, NULL, NULL);
155     if (!buffer) {
156         GST_ERROR("failed to create encoded data buffer");
157         return FALSE;
158     }
159
160     success = gst_vaapi_decoder_put_buffer(decoder, buffer);
161     gst_buffer_unref(buffer);
162     if (!success) {
163         GST_ERROR("failed to send video data to the decoder");
164         return FALSE;
165     }
166
167     if (!gst_vaapi_decoder_put_buffer(decoder, NULL)) {
168         GST_ERROR("failed to submit <end-of-stream> to the decoder");
169         return FALSE;
170     }
171     return TRUE;
172 }
173
174 GstVaapiSurfaceProxy *
175 decoder_get_surface(GstVaapiDecoder *decoder)
176 {
177     GstVaapiSurfaceProxy *proxy;
178     GstVaapiDecoderStatus status;
179
180     g_return_val_if_fail(decoder != NULL, NULL);
181
182     status = gst_vaapi_decoder_get_surface(decoder, &proxy);
183     if (status != GST_VAAPI_DECODER_STATUS_SUCCESS) {
184         GST_ERROR("failed to get decoded surface (decoder status %d)", status);
185         return NULL;
186     }
187     return proxy;
188 }
189
190 const gchar *
191 decoder_get_codec_name(GstVaapiDecoder *decoder)
192 {
193     const CodecDefs *codec;
194
195     g_return_val_if_fail(decoder != NULL, NULL);
196
197     codec = get_codec_defs(decoder);
198     g_return_val_if_fail(codec != NULL, FALSE);
199
200     return codec->codec_str;
201 }