summaryrefslogtreecommitdiffstats
path: root/ext/annodex/gstannodex.c
blob: 22fe798d974dcac6734c8b33b9f813d9859012de (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
/*
 * gstannodex.c - GStreamer annodex plugin
 * Copyright (C) 2005 Alessandro Decina
 * 
 * Authors:
 *   Alessandro Decina <alessandro@nnva.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <math.h>
#include <gst/tag/tag.h>
#include "gstcmmlparser.h"
#include "gstcmmlenc.h"
#include "gstcmmldec.h"

GstClockTime
gst_annodex_granule_to_time (gint64 granulepos, gint64 granulerate_n,
    gint64 granulerate_d, guint8 granuleshift)
{
  gint64 keyindex, keyoffset;
  gint64 granulerate;
  GstClockTime res;

  g_return_val_if_fail (granuleshift <= 64, GST_CLOCK_TIME_NONE);

  if (granulepos == -1)
    return GST_CLOCK_TIME_NONE;

  if (granulepos == 0 || granulerate_n == 0 || granulerate_d == 0)
    return 0;

  if (granuleshift != 0 && granuleshift != 64) {
    keyindex = granulepos >> granuleshift;
    keyoffset = granulepos - (keyindex << granuleshift);
    granulepos = keyindex + keyoffset;
  }

  /* GST_SECOND / (granulerate_n / granulerate_d) */
  granulerate = gst_util_uint64_scale (GST_SECOND,
      granulerate_d, granulerate_n);

  /* granulepos * granulerate */
  res = gst_util_uint64_scale (granulepos, granulerate, 1);

  return res;
}

GValueArray *
gst_annodex_parse_headers (const gchar * headers)
{
  GValueArray *array;
  GValue val = { 0 };
  gchar *header_name = NULL;
  gchar *header_value = NULL;
  gchar *line, *column, *space, *tmp;
  gchar **lines;
  gint i = 0;

  array = g_value_array_new (0);
  g_value_init (&val, G_TYPE_STRING);

  lines = g_strsplit (headers, "\r\n", 0);
  line = lines[i];
  while (line != NULL && *line != '\0') {
    if (line[0] == '\t' || line[0] == ' ') {
      /* WSP: continuation line */
      if (header_value == NULL)
        /* continuation line without a previous value */
        goto fail;

      tmp = g_strjoin (" ", header_value, g_strstrip (line), NULL);
      g_free (header_value);
      header_value = tmp;
    } else {
      if (header_name) {
        g_value_take_string (&val, header_name);
        g_value_array_append (array, &val);
        g_value_take_string (&val, header_value);
        g_value_array_append (array, &val);
      }
      /* search the column starting from line[1] as an header name can't be
       * empty */
      column = g_strstr_len (line + 1, strlen (line) - 1, ":");
      if (column == NULL)
        /* bad syntax */
        goto fail;

      if (*(space = column + 1) != ' ')
        /* bad syntax */
        goto fail;

      header_name = g_strndup (line, column - line);
      header_value = g_strdup (space + 1);
    }

    line = lines[++i];
  }

  if (header_name) {
    g_value_take_string (&val, header_name);
    g_value_array_append (array, &val);
    g_value_take_string (&val, header_value);
    g_value_array_append (array, &val);
  }

  g_value_unset (&val);
  g_strfreev (lines);

  return array;

fail:
  GST_WARNING ("could not parse annodex headers");
  g_free (header_name);
  g_free (header_value);
  g_strfreev (lines);
  g_value_array_free (array);
  g_value_unset (&val);
  return NULL;
}

static gboolean
plugin_init (GstPlugin * plugin)
{
  gst_tag_register (GST_TAG_CMML_STREAM, GST_TAG_FLAG_META,
      GST_TYPE_CMML_TAG_STREAM, "cmml-stream", "annodex CMML stream tag", NULL);

  gst_tag_register (GST_TAG_CMML_HEAD, GST_TAG_FLAG_META,
      GST_TYPE_CMML_TAG_HEAD, "cmml-head", "annodex CMML head tag", NULL);

  gst_tag_register (GST_TAG_CMML_CLIP, GST_TAG_FLAG_META,
      GST_TYPE_CMML_TAG_CLIP, "cmml-clip", "annodex CMML clip tag", NULL);

  gst_cmml_parser_init ();

  if (!gst_cmml_enc_plugin_init (plugin))
    return FALSE;

  if (!gst_cmml_dec_plugin_init (plugin))
    return FALSE;

  return TRUE;
}

GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
    GST_VERSION_MINOR,
    "annodex",
    "annodex stream manipulation (info about annodex: http://www.annodex.net)",
    plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)