summaryrefslogtreecommitdiff
path: root/tests/codec.c
blob: d14981ac8e43997480c24629bd864a226aa174c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
 *  codec.c - Codec utilities for the tests
 *
 *  Copyright (C) 2013 Intel Corporation
 *    Author: Gwenole Beauchesne <gwenole.beauchesne@intel.com>
 *
 *  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
 */

#include "gst/vaapi/sysdeps.h"
#include <gst/vaapi/gstvaapidisplay.h>
#include "codec.h"

typedef struct
{
  const gchar *codec_str;
  GstVaapiCodec codec;
  const gchar *caps_str;
} CodecMap;

static const CodecMap g_codec_map[] = {
  {"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,}
};

static const CodecMap *
get_codec_map (GstVaapiCodec codec)
{
  const CodecMap *m;

  if (codec) {
    for (m = g_codec_map; m->codec_str != NULL; m++) {
      if (m->codec == codec)
        return m;
    }
  }
  return NULL;
}

const gchar *
string_from_codec (GstVaapiCodec codec)
{
  const CodecMap *const m = get_codec_map (codec);

  return m ? m->codec_str : NULL;
}

GstCaps *
caps_from_codec (GstVaapiCodec codec)
{
  const CodecMap *const m = get_codec_map (codec);

  return m ? gst_caps_from_string (m->caps_str) : NULL;
}

GstVaapiCodec
identify_codec_from_string (const gchar * codec_str)
{
  const CodecMap *m;

  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;
    }
  }
  return 0;
}

typedef struct
{
  GMappedFile *file;
  guint8 *data;
  guint size;
  guint probability;
  GstCaps *caps;
  GstTypeFind type_find;
} CodecIdentifier;

static const guint8 *
codec_identifier_peek (gpointer data, gint64 offset, guint size)
{
  CodecIdentifier *const cip = data;

  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;
}

static void
codec_identifier_suggest (gpointer data, guint probability, GstCaps * caps)
{
  CodecIdentifier *const cip = data;

  if (cip->probability < probability) {
    cip->probability = probability;
    gst_caps_replace (&cip->caps, caps);
  }
}

static void
codec_identifier_free (CodecIdentifier * cip)
{
  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);
}

static CodecIdentifier *
codec_identifier_new (const gchar * filename)
{
  CodecIdentifier *cip;
  GstTypeFind *tfp;

  cip = g_slice_new0 (CodecIdentifier);
  if (!cip)
    return NULL;

  cip->file = g_mapped_file_new (filename, FALSE, NULL);
  if (!cip->file)
    goto error;

  cip->size = g_mapped_file_get_length (cip->file);
  cip->data = (guint8 *) g_mapped_file_get_contents (cip->file);
  if (!cip->data)
    goto error;

  tfp = &cip->type_find;
  tfp->peek = codec_identifier_peek;
  tfp->suggest = codec_identifier_suggest;
  tfp->data = cip;
  return cip;

error:
  codec_identifier_free (cip);
  return NULL;
}

GstVaapiCodec
identify_codec (const gchar * filename)
{
  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);

  if (cip->probability >= GST_TYPE_FIND_LIKELY)
    codec =
        gst_vaapi_profile_get_codec (gst_vaapi_profile_from_caps (cip->caps));

  codec_identifier_free (cip);
  return codec;
}