encoder: h264: fix NAL unit types in packed headers.
[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  *    Author: Gwenole Beauchesne <gwenole.beauchesne@intel.com>
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 "gst/vaapi/sysdeps.h"
24 #include <string.h>
25 #include <gst/vaapi/gstvaapidecoder_h264.h>
26 #include <gst/vaapi/gstvaapidecoder_jpeg.h>
27 #include <gst/vaapi/gstvaapidecoder_mpeg2.h>
28 #include <gst/vaapi/gstvaapidecoder_mpeg4.h>
29 #include <gst/vaapi/gstvaapidecoder_vc1.h>
30 #include "decoder.h"
31 #include "test-jpeg.h"
32 #include "test-mpeg2.h"
33 #include "test-mpeg4.h"
34 #include "test-h264.h"
35 #include "test-vc1.h"
36
37 typedef void (*GetVideoInfoFunc)(VideoDecodeInfo *info);
38
39 typedef struct _CodecDefs CodecDefs;
40 struct _CodecDefs {
41     const gchar        *codec_str;
42     GetVideoInfoFunc    get_video_info;
43 };
44
45 static const CodecDefs g_codec_defs[] = {
46 #define INIT_FUNCS(CODEC) { #CODEC, CODEC##_get_video_info }
47     INIT_FUNCS(jpeg),
48     INIT_FUNCS(mpeg2),
49     INIT_FUNCS(mpeg4),
50     INIT_FUNCS(h264),
51     INIT_FUNCS(vc1),
52 #undef INIT_FUNCS
53     { NULL, }
54 };
55
56 static const CodecDefs *
57 find_codec_defs(const gchar *codec_str)
58 {
59     const CodecDefs *c;
60     for (c = g_codec_defs; c->codec_str; c++)
61         if (strcmp(codec_str, c->codec_str) == 0)
62             return c;
63     return NULL;
64 }
65
66 static inline const CodecDefs *
67 get_codec_defs(GstVaapiDecoder *decoder)
68 {
69     return gst_vaapi_decoder_get_user_data(decoder);
70 }
71
72 static inline void
73 set_codec_defs(GstVaapiDecoder *decoder, const CodecDefs *c)
74 {
75     gst_vaapi_decoder_set_user_data(decoder, (gpointer)c);
76 }
77
78 GstVaapiDecoder *
79 decoder_new(GstVaapiDisplay *display, const gchar *codec_name)
80 {
81     GstVaapiDecoder *decoder;
82     const CodecDefs *codec;
83     GstCaps *caps;
84     VideoDecodeInfo info;
85
86     if (!codec_name)
87         codec_name = "h264";
88
89     codec = find_codec_defs(codec_name);
90     if (!codec) {
91         GST_ERROR("failed to find %s codec data", codec_name);
92         return NULL;
93     }
94
95     codec->get_video_info(&info);
96     caps = gst_vaapi_profile_get_caps(info.profile);
97     if (!caps) {
98         GST_ERROR("failed to create decoder caps");
99         return NULL;
100     }
101
102     if (info.width > 0 && info.height > 0)
103         gst_caps_set_simple(caps,
104             "width",  G_TYPE_INT, info.width,
105             "height", G_TYPE_INT, info.height,
106             NULL);
107
108     switch (gst_vaapi_profile_get_codec(info.profile)) {
109     case GST_VAAPI_CODEC_H264:
110         decoder = gst_vaapi_decoder_h264_new(display, caps);
111         break;
112 #if USE_JPEG_DECODER
113     case GST_VAAPI_CODEC_JPEG:
114         decoder = gst_vaapi_decoder_jpeg_new(display, caps);
115         break;
116 #endif
117     case GST_VAAPI_CODEC_MPEG2:
118         decoder = gst_vaapi_decoder_mpeg2_new(display, caps);
119         break;
120     case GST_VAAPI_CODEC_MPEG4:
121         decoder = gst_vaapi_decoder_mpeg4_new(display, caps);
122         break;
123     case GST_VAAPI_CODEC_VC1:
124         decoder = gst_vaapi_decoder_vc1_new(display, caps);
125         break;
126     default:
127         decoder = NULL;
128         break;
129     }
130     gst_caps_unref(caps);
131     if (!decoder) {
132         GST_ERROR("failed to create %s decoder", codec->codec_str);
133         return NULL;
134     }
135
136     set_codec_defs(decoder, codec);
137     return decoder;
138 }
139
140 gboolean
141 decoder_put_buffers(GstVaapiDecoder *decoder)
142 {
143     const CodecDefs *codec;
144     VideoDecodeInfo info;
145     GstBuffer *buffer;
146     gboolean success;
147
148     g_return_val_if_fail(decoder != NULL, FALSE);
149
150     codec = get_codec_defs(decoder);
151     g_return_val_if_fail(codec != NULL, FALSE);
152
153     codec->get_video_info(&info);
154     buffer = gst_buffer_new_wrapped_full(GST_MEMORY_FLAG_READONLY,
155         (guchar *)info.data, info.data_size, 0, info.data_size, NULL, NULL);
156     if (!buffer) {
157         GST_ERROR("failed to create encoded data buffer");
158         return FALSE;
159     }
160
161     success = gst_vaapi_decoder_put_buffer(decoder, buffer);
162     gst_buffer_unref(buffer);
163     if (!success) {
164         GST_ERROR("failed to send video data to the decoder");
165         return FALSE;
166     }
167
168     if (!gst_vaapi_decoder_put_buffer(decoder, NULL)) {
169         GST_ERROR("failed to submit <end-of-stream> to the decoder");
170         return FALSE;
171     }
172     return TRUE;
173 }
174
175 GstVaapiSurfaceProxy *
176 decoder_get_surface(GstVaapiDecoder *decoder)
177 {
178     GstVaapiSurfaceProxy *proxy;
179     GstVaapiDecoderStatus status;
180
181     g_return_val_if_fail(decoder != NULL, NULL);
182
183     status = gst_vaapi_decoder_get_surface(decoder, &proxy);
184     if (status != GST_VAAPI_DECODER_STATUS_SUCCESS) {
185         GST_ERROR("failed to get decoded surface (decoder status %d)", status);
186         return NULL;
187     }
188     return proxy;
189 }
190
191 const gchar *
192 decoder_get_codec_name(GstVaapiDecoder *decoder)
193 {
194     const CodecDefs *codec;
195
196     g_return_val_if_fail(decoder != NULL, NULL);
197
198     codec = get_codec_defs(decoder);
199     g_return_val_if_fail(codec != NULL, FALSE);
200
201     return codec->codec_str;
202 }