2013-01-15 16:30:57 +00:00
|
|
|
/*
|
|
|
|
* codec.c - Codec utilities for the tests
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 Intel Corporation
|
2013-11-22 04:57:18 +00:00
|
|
|
* Author: Gwenole Beauchesne <gwenole.beauchesne@intel.com>
|
2013-01-15 16:30:57 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2.1
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free
|
|
|
|
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
2013-03-20 10:50:15 +00:00
|
|
|
#include "gst/vaapi/sysdeps.h"
|
2013-01-15 16:30:57 +00:00
|
|
|
#include <gst/vaapi/gstvaapidisplay.h>
|
|
|
|
#include "codec.h"
|
|
|
|
|
2016-04-29 10:48:44 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
const gchar *codec_str;
|
|
|
|
GstVaapiCodec codec;
|
|
|
|
const gchar *caps_str;
|
2013-01-15 16:30:57 +00:00
|
|
|
} CodecMap;
|
|
|
|
|
|
|
|
static const CodecMap g_codec_map[] = {
|
2016-04-29 10:48:44 +00:00
|
|
|
{"h264", GST_VAAPI_CODEC_H264,
|
|
|
|
"video/x-h264"},
|
|
|
|
{"jpeg", GST_VAAPI_CODEC_JPEG,
|
|
|
|
"image/jpeg"},
|
|
|
|
{"mpeg2", GST_VAAPI_CODEC_MPEG2,
|
|
|
|
"video/mpeg, mpegversion=2"},
|
|
|
|
{"mpeg4", GST_VAAPI_CODEC_MPEG4,
|
|
|
|
"video/mpeg, mpegversion=4"},
|
|
|
|
{"wmv3", GST_VAAPI_CODEC_VC1,
|
|
|
|
"video/x-wmv, wmvversion=3"},
|
|
|
|
{"vc1", GST_VAAPI_CODEC_VC1,
|
|
|
|
"video/x-wmv, wmvversion=3, format=(string)WVC1"},
|
|
|
|
{NULL,}
|
2013-01-15 16:30:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const CodecMap *
|
2016-04-29 10:48:44 +00:00
|
|
|
get_codec_map (GstVaapiCodec codec)
|
2013-01-15 16:30:57 +00:00
|
|
|
{
|
2016-04-29 10:48:44 +00:00
|
|
|
const CodecMap *m;
|
2013-01-15 16:30:57 +00:00
|
|
|
|
2016-04-29 10:48:44 +00:00
|
|
|
if (codec) {
|
|
|
|
for (m = g_codec_map; m->codec_str != NULL; m++) {
|
|
|
|
if (m->codec == codec)
|
|
|
|
return m;
|
2013-01-15 16:30:57 +00:00
|
|
|
}
|
2016-04-29 10:48:44 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
2013-01-15 16:30:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const gchar *
|
2016-04-29 10:48:44 +00:00
|
|
|
string_from_codec (GstVaapiCodec codec)
|
2013-01-15 16:30:57 +00:00
|
|
|
{
|
2016-04-29 10:48:44 +00:00
|
|
|
const CodecMap *const m = get_codec_map (codec);
|
2013-01-15 16:30:57 +00:00
|
|
|
|
2016-04-29 10:48:44 +00:00
|
|
|
return m ? m->codec_str : NULL;
|
2013-01-15 16:30:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GstCaps *
|
2016-04-29 10:48:44 +00:00
|
|
|
caps_from_codec (GstVaapiCodec codec)
|
2013-01-15 16:30:57 +00:00
|
|
|
{
|
2016-04-29 10:48:44 +00:00
|
|
|
const CodecMap *const m = get_codec_map (codec);
|
2013-01-15 16:30:57 +00:00
|
|
|
|
2016-04-29 10:48:44 +00:00
|
|
|
return m ? gst_caps_from_string (m->caps_str) : NULL;
|
2013-01-15 16:30:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GstVaapiCodec
|
2016-04-29 10:48:44 +00:00
|
|
|
identify_codec_from_string (const gchar * codec_str)
|
2013-01-15 16:30:57 +00:00
|
|
|
{
|
2016-04-29 10:48:44 +00:00
|
|
|
const CodecMap *m;
|
2013-01-15 16:30:57 +00:00
|
|
|
|
2016-04-29 10:48:44 +00:00
|
|
|
if (codec_str) {
|
|
|
|
for (m = g_codec_map; m->codec_str != NULL; m++) {
|
|
|
|
if (g_ascii_strcasecmp (m->codec_str, codec_str) == 0)
|
|
|
|
return m->codec;
|
2013-01-15 16:30:57 +00:00
|
|
|
}
|
2016-04-29 10:48:44 +00:00
|
|
|
}
|
|
|
|
return 0;
|
2013-01-15 16:30:57 +00:00
|
|
|
}
|
|
|
|
|
2016-04-29 10:48:44 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
GMappedFile *file;
|
|
|
|
guint8 *data;
|
|
|
|
guint size;
|
|
|
|
guint probability;
|
|
|
|
GstCaps *caps;
|
|
|
|
GstTypeFind type_find;
|
2013-01-15 16:30:57 +00:00
|
|
|
} CodecIdentifier;
|
|
|
|
|
2013-03-20 17:33:23 +00:00
|
|
|
static const guint8 *
|
2016-04-29 10:48:44 +00:00
|
|
|
codec_identifier_peek (gpointer data, gint64 offset, guint size)
|
2013-01-15 16:30:57 +00:00
|
|
|
{
|
2016-04-29 10:48:44 +00:00
|
|
|
CodecIdentifier *const cip = data;
|
2013-01-15 16:30:57 +00:00
|
|
|
|
2016-04-29 10:48:44 +00:00
|
|
|
if (offset >= 0 && offset + size <= cip->size)
|
|
|
|
return cip->data + offset;
|
|
|
|
if (offset < 0 && ((gint) cip->size + offset) >= 0)
|
|
|
|
return &cip->data[cip->size + offset];
|
|
|
|
return NULL;
|
2013-01-15 16:30:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-04-29 10:48:44 +00:00
|
|
|
codec_identifier_suggest (gpointer data, guint probability, GstCaps * caps)
|
2013-01-15 16:30:57 +00:00
|
|
|
{
|
2016-04-29 10:48:44 +00:00
|
|
|
CodecIdentifier *const cip = data;
|
2013-01-15 16:30:57 +00:00
|
|
|
|
2016-04-29 10:48:44 +00:00
|
|
|
if (cip->probability < probability) {
|
|
|
|
cip->probability = probability;
|
|
|
|
gst_caps_replace (&cip->caps, caps);
|
|
|
|
}
|
2013-01-15 16:30:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-04-29 10:48:44 +00:00
|
|
|
codec_identifier_free (CodecIdentifier * cip)
|
2013-01-15 16:30:57 +00:00
|
|
|
{
|
2016-04-29 10:48:44 +00:00
|
|
|
if (!cip)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (cip->file) {
|
|
|
|
g_mapped_file_unref (cip->file);
|
|
|
|
cip->file = NULL;
|
|
|
|
}
|
|
|
|
gst_caps_replace (&cip->caps, NULL);
|
|
|
|
g_slice_free (CodecIdentifier, cip);
|
2013-01-15 16:30:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static CodecIdentifier *
|
2016-04-29 10:48:44 +00:00
|
|
|
codec_identifier_new (const gchar * filename)
|
2013-01-15 16:30:57 +00:00
|
|
|
{
|
2016-04-29 10:48:44 +00:00
|
|
|
CodecIdentifier *cip;
|
|
|
|
GstTypeFind *tfp;
|
2013-01-15 16:30:57 +00:00
|
|
|
|
2016-04-29 10:48:44 +00:00
|
|
|
cip = g_slice_new0 (CodecIdentifier);
|
|
|
|
if (!cip)
|
|
|
|
return NULL;
|
2013-01-15 16:30:57 +00:00
|
|
|
|
2016-04-29 10:48:44 +00:00
|
|
|
cip->file = g_mapped_file_new (filename, FALSE, NULL);
|
|
|
|
if (!cip->file)
|
|
|
|
goto error;
|
2013-01-15 16:30:57 +00:00
|
|
|
|
2016-04-29 10:48:44 +00:00
|
|
|
cip->size = g_mapped_file_get_length (cip->file);
|
|
|
|
cip->data = (guint8 *) g_mapped_file_get_contents (cip->file);
|
|
|
|
if (!cip->data)
|
|
|
|
goto error;
|
2013-01-15 16:30:57 +00:00
|
|
|
|
2016-04-29 10:48:44 +00:00
|
|
|
tfp = &cip->type_find;
|
|
|
|
tfp->peek = codec_identifier_peek;
|
|
|
|
tfp->suggest = codec_identifier_suggest;
|
|
|
|
tfp->data = cip;
|
|
|
|
return cip;
|
2013-01-15 16:30:57 +00:00
|
|
|
|
|
|
|
error:
|
2016-04-29 10:48:44 +00:00
|
|
|
codec_identifier_free (cip);
|
|
|
|
return NULL;
|
2013-01-15 16:30:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GstVaapiCodec
|
2016-04-29 10:48:44 +00:00
|
|
|
identify_codec (const gchar * filename)
|
2013-01-15 16:30:57 +00:00
|
|
|
{
|
2016-04-29 10:48:44 +00:00
|
|
|
CodecIdentifier *cip;
|
|
|
|
GList *type_list, *l;
|
|
|
|
guint32 codec = 0;
|
|
|
|
|
|
|
|
cip = codec_identifier_new (filename);
|
|
|
|
if (!cip)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
type_list = gst_type_find_factory_get_list ();
|
|
|
|
for (l = type_list; l != NULL; l = l->next) {
|
|
|
|
GstTypeFindFactory *const factory = GST_TYPE_FIND_FACTORY (l->data);
|
|
|
|
gst_type_find_factory_call_function (factory, &cip->type_find);
|
|
|
|
}
|
|
|
|
g_list_free (type_list);
|
2013-01-15 16:30:57 +00:00
|
|
|
|
2016-04-29 10:48:44 +00:00
|
|
|
if (cip->probability >= GST_TYPE_FIND_LIKELY)
|
|
|
|
codec =
|
|
|
|
gst_vaapi_profile_get_codec (gst_vaapi_profile_from_caps (cip->caps));
|
2013-01-15 16:30:57 +00:00
|
|
|
|
2016-04-29 10:48:44 +00:00
|
|
|
codec_identifier_free (cip);
|
|
|
|
return codec;
|
2013-01-15 16:30:57 +00:00
|
|
|
}
|