tests: move examples and tests to subfolders
[platform/upstream/gstreamer.git] / tests / internal / codec.c
1 /*
2  *  codec.c - Codec 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 <gst/vaapi/gstvaapidisplay.h>
25 #include "codec.h"
26
27 typedef struct
28 {
29   const gchar *codec_str;
30   GstVaapiCodec codec;
31   const gchar *caps_str;
32 } CodecMap;
33
34 static const CodecMap g_codec_map[] = {
35   {"h264", GST_VAAPI_CODEC_H264,
36       "video/x-h264"},
37   {"jpeg", GST_VAAPI_CODEC_JPEG,
38       "image/jpeg"},
39   {"mpeg2", GST_VAAPI_CODEC_MPEG2,
40       "video/mpeg, mpegversion=2"},
41   {"mpeg4", GST_VAAPI_CODEC_MPEG4,
42       "video/mpeg, mpegversion=4"},
43   {"wmv3", GST_VAAPI_CODEC_VC1,
44       "video/x-wmv, wmvversion=3"},
45   {"vc1", GST_VAAPI_CODEC_VC1,
46       "video/x-wmv, wmvversion=3, format=(string)WVC1"},
47   {NULL,}
48 };
49
50 static const CodecMap *
51 get_codec_map (GstVaapiCodec codec)
52 {
53   const CodecMap *m;
54
55   if (codec) {
56     for (m = g_codec_map; m->codec_str != NULL; m++) {
57       if (m->codec == codec)
58         return m;
59     }
60   }
61   return NULL;
62 }
63
64 const gchar *
65 string_from_codec (GstVaapiCodec codec)
66 {
67   const CodecMap *const m = get_codec_map (codec);
68
69   return m ? m->codec_str : NULL;
70 }
71
72 GstCaps *
73 caps_from_codec (GstVaapiCodec codec)
74 {
75   const CodecMap *const m = get_codec_map (codec);
76
77   return m ? gst_caps_from_string (m->caps_str) : NULL;
78 }
79
80 GstVaapiCodec
81 identify_codec_from_string (const gchar * codec_str)
82 {
83   const CodecMap *m;
84
85   if (codec_str) {
86     for (m = g_codec_map; m->codec_str != NULL; m++) {
87       if (g_ascii_strcasecmp (m->codec_str, codec_str) == 0)
88         return m->codec;
89     }
90   }
91   return 0;
92 }
93
94 typedef struct
95 {
96   GMappedFile *file;
97   guint8 *data;
98   guint size;
99   guint probability;
100   GstCaps *caps;
101   GstTypeFind type_find;
102 } CodecIdentifier;
103
104 static const guint8 *
105 codec_identifier_peek (gpointer data, gint64 offset, guint size)
106 {
107   CodecIdentifier *const cip = data;
108
109   if (offset >= 0 && offset + size <= cip->size)
110     return cip->data + offset;
111   if (offset < 0 && ((gint) cip->size + offset) >= 0)
112     return &cip->data[cip->size + offset];
113   return NULL;
114 }
115
116 static void
117 codec_identifier_suggest (gpointer data, guint probability, GstCaps * caps)
118 {
119   CodecIdentifier *const cip = data;
120
121   if (cip->probability < probability) {
122     cip->probability = probability;
123     gst_caps_replace (&cip->caps, caps);
124   }
125 }
126
127 static void
128 codec_identifier_free (CodecIdentifier * cip)
129 {
130   if (!cip)
131     return;
132
133   if (cip->file) {
134     g_mapped_file_unref (cip->file);
135     cip->file = NULL;
136   }
137   gst_caps_replace (&cip->caps, NULL);
138   g_slice_free (CodecIdentifier, cip);
139 }
140
141 static CodecIdentifier *
142 codec_identifier_new (const gchar * filename)
143 {
144   CodecIdentifier *cip;
145   GstTypeFind *tfp;
146
147   cip = g_slice_new0 (CodecIdentifier);
148   if (!cip)
149     return NULL;
150
151   cip->file = g_mapped_file_new (filename, FALSE, NULL);
152   if (!cip->file)
153     goto error;
154
155   cip->size = g_mapped_file_get_length (cip->file);
156   cip->data = (guint8 *) g_mapped_file_get_contents (cip->file);
157   if (!cip->data)
158     goto error;
159
160   tfp = &cip->type_find;
161   tfp->peek = codec_identifier_peek;
162   tfp->suggest = codec_identifier_suggest;
163   tfp->data = cip;
164   return cip;
165
166 error:
167   codec_identifier_free (cip);
168   return NULL;
169 }
170
171 GstVaapiCodec
172 identify_codec (const gchar * filename)
173 {
174   CodecIdentifier *cip;
175   GList *type_list, *l;
176   guint32 codec = 0;
177
178   cip = codec_identifier_new (filename);
179   if (!cip)
180     return 0;
181
182   type_list = gst_type_find_factory_get_list ();
183   for (l = type_list; l != NULL; l = l->next) {
184     GstTypeFindFactory *const factory = GST_TYPE_FIND_FACTORY (l->data);
185     gst_type_find_factory_call_function (factory, &cip->type_find);
186   }
187   g_list_free (type_list);
188
189   if (cip->probability >= GST_TYPE_FIND_LIKELY)
190     codec =
191         gst_vaapi_profile_get_codec (gst_vaapi_profile_from_caps (cip->caps));
192
193   codec_identifier_free (cip);
194   return codec;
195 }