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>
28 const gchar *codec_str;
30 const gchar *caps_str;
33 static const CodecMap g_codec_map[] = {
34 { "h264", GST_VAAPI_CODEC_H264,
36 { "jpeg", GST_VAAPI_CODEC_JPEG,
38 { "mpeg2", GST_VAAPI_CODEC_MPEG2,
39 "video/mpeg, mpegversion=2" },
40 { "mpeg4", GST_VAAPI_CODEC_MPEG4,
41 "video/mpeg, mpegversion=4" },
42 { "wmv3", GST_VAAPI_CODEC_VC1,
43 "video/x-wmv, wmvversion=3" },
44 { "vc1", GST_VAAPI_CODEC_VC1,
45 "video/x-wmv, wmvversion=3, " GST_MAKE_FORMAT_STRING(WVC1) },
49 static const CodecMap *
50 get_codec_map(GstVaapiCodec codec)
55 for (m = g_codec_map; m->codec_str != NULL; m++) {
56 if (m->codec == codec)
64 string_from_codec(GstVaapiCodec codec)
66 const CodecMap * const m = get_codec_map(codec);
68 return m ? m->codec_str : NULL;
72 caps_from_codec(GstVaapiCodec codec)
74 const CodecMap * const m = get_codec_map(codec);
76 return m ? gst_caps_from_string(m->caps_str) : NULL;
80 identify_codec_from_string(const gchar *codec_str)
85 for (m = g_codec_map; m->codec_str != NULL; m++) {
86 if (g_ascii_strcasecmp(m->codec_str, codec_str) == 0)
99 GstTypeFind type_find;
102 static const guint8 *
103 codec_identifier_peek(gpointer data, gint64 offset, guint size)
105 CodecIdentifier * const cip = data;
107 if (offset >= 0 && offset + size <= cip->size)
108 return cip->data + offset;
109 if (offset < 0 && ((gint)cip->size + offset) >= 0)
110 return &cip->data[cip->size + offset];
115 codec_identifier_suggest(gpointer data, guint probability, GstCaps *caps)
117 CodecIdentifier * const cip = data;
119 if (cip->probability < probability) {
120 cip->probability = probability;
121 gst_caps_replace(&cip->caps, caps);
126 codec_identifier_free(CodecIdentifier *cip)
132 g_mapped_file_unref(cip->file);
135 gst_caps_replace(&cip->caps, NULL);
136 g_slice_free(CodecIdentifier, cip);
139 static CodecIdentifier *
140 codec_identifier_new(const gchar *filename)
142 CodecIdentifier *cip;
145 cip = g_slice_new0(CodecIdentifier);
149 cip->file = g_mapped_file_new(filename, FALSE, NULL);
153 cip->size = g_mapped_file_get_length(cip->file);
154 cip->data = (guint8 *)g_mapped_file_get_contents(cip->file);
158 tfp = &cip->type_find;
159 tfp->peek = (GstTypeFindPeekFunction)codec_identifier_peek;
160 tfp->suggest = (GstTypeFindSuggestFunction)codec_identifier_suggest;
165 codec_identifier_free(cip);
170 identify_codec(const gchar *filename)
172 CodecIdentifier *cip;
173 GList *type_list, *l;
176 cip = codec_identifier_new(filename);
180 type_list = gst_type_find_factory_get_list();
181 for (l = type_list; l != NULL; l = l->next) {
182 GstTypeFindFactory * const factory = GST_TYPE_FIND_FACTORY(l->data);
183 gst_type_find_factory_call_function(factory, &cip->type_find);
185 g_list_free(type_list);
187 if (cip->probability >= GST_TYPE_FIND_LIKELY)
188 codec = gst_vaapi_profile_get_codec(
189 gst_vaapi_profile_from_caps(cip->caps));
191 codec_identifier_free(cip);