2 * codec.c - Codec utilities for the tests
4 * Copyright (C) 2013 Intel Corporation
5 * Author: Gwenole Beauchesne <gwenole.beauchesne@intel.com>
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.
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.
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
23 #include "gst/vaapi/sysdeps.h"
24 #include <gst/vaapi/gstvaapidisplay.h>
29 const gchar *codec_str;
31 const gchar *caps_str;
34 static const CodecMap g_codec_map[] = {
35 {"h264", GST_VAAPI_CODEC_H264,
37 {"jpeg", GST_VAAPI_CODEC_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"},
50 static const CodecMap *
51 get_codec_map (GstVaapiCodec codec)
56 for (m = g_codec_map; m->codec_str != NULL; m++) {
57 if (m->codec == codec)
65 string_from_codec (GstVaapiCodec codec)
67 const CodecMap *const m = get_codec_map (codec);
69 return m ? m->codec_str : NULL;
73 caps_from_codec (GstVaapiCodec codec)
75 const CodecMap *const m = get_codec_map (codec);
77 return m ? gst_caps_from_string (m->caps_str) : NULL;
81 identify_codec_from_string (const gchar * codec_str)
86 for (m = g_codec_map; m->codec_str != NULL; m++) {
87 if (g_ascii_strcasecmp (m->codec_str, codec_str) == 0)
101 GstTypeFind type_find;
104 static const guint8 *
105 codec_identifier_peek (gpointer data, gint64 offset, guint size)
107 CodecIdentifier *const cip = data;
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];
117 codec_identifier_suggest (gpointer data, guint probability, GstCaps * caps)
119 CodecIdentifier *const cip = data;
121 if (cip->probability < probability) {
122 cip->probability = probability;
123 gst_caps_replace (&cip->caps, caps);
128 codec_identifier_free (CodecIdentifier * cip)
134 g_mapped_file_unref (cip->file);
137 gst_caps_replace (&cip->caps, NULL);
138 g_slice_free (CodecIdentifier, cip);
141 static CodecIdentifier *
142 codec_identifier_new (const gchar * filename)
144 CodecIdentifier *cip;
147 cip = g_slice_new0 (CodecIdentifier);
151 cip->file = g_mapped_file_new (filename, FALSE, NULL);
155 cip->size = g_mapped_file_get_length (cip->file);
156 cip->data = (guint8 *) g_mapped_file_get_contents (cip->file);
160 tfp = &cip->type_find;
161 tfp->peek = codec_identifier_peek;
162 tfp->suggest = codec_identifier_suggest;
167 codec_identifier_free (cip);
172 identify_codec (const gchar * filename)
174 CodecIdentifier *cip;
175 GList *type_list, *l;
178 cip = codec_identifier_new (filename);
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);
187 g_list_free (type_list);
189 if (cip->probability >= GST_TYPE_FIND_LIKELY)
191 gst_vaapi_profile_get_codec (gst_vaapi_profile_from_caps (cip->caps));
193 codec_identifier_free (cip);