Tizen 2.0 Release
[framework/multimedia/gst-plugins-good0.10.git] / ext / annodex / gstannodex.c
1 /*
2  * gstannodex.c - GStreamer annodex plugin
3  * Copyright (C) 2005 Alessandro Decina
4  * 
5  * Authors:
6  *   Alessandro Decina <alessandro@nnva.org>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #include <math.h>
29 #include <gst/tag/tag.h>
30 #include "gstannodex.h"
31 #include "gstcmmlparser.h"
32 #include "gstcmmlenc.h"
33 #include "gstcmmldec.h"
34
35 GstClockTime
36 gst_annodex_granule_to_time (gint64 granulepos, gint64 granulerate_n,
37     gint64 granulerate_d, guint8 granuleshift)
38 {
39   gint64 keyindex, keyoffset;
40   gint64 granulerate;
41   GstClockTime res;
42
43   g_return_val_if_fail (granuleshift <= 64, GST_CLOCK_TIME_NONE);
44
45   if (granulepos == -1)
46     return GST_CLOCK_TIME_NONE;
47
48   if (granulepos == 0 || granulerate_n == 0 || granulerate_d == 0)
49     return 0;
50
51   if (granuleshift != 0 && granuleshift != 64) {
52     keyindex = granulepos >> granuleshift;
53     keyoffset = granulepos - (keyindex << granuleshift);
54     granulepos = keyindex + keyoffset;
55   }
56
57   /* GST_SECOND / (granulerate_n / granulerate_d) */
58   granulerate = gst_util_uint64_scale (GST_SECOND,
59       granulerate_d, granulerate_n);
60
61   /* granulepos * granulerate */
62   res = gst_util_uint64_scale (granulepos, granulerate, 1);
63
64   return res;
65 }
66
67 GValueArray *
68 gst_annodex_parse_headers (const gchar * headers)
69 {
70   GValueArray *array;
71   GValue val = { 0 };
72   gchar *header_name = NULL;
73   gchar *header_value = NULL;
74   gchar *line, *column, *space, *tmp;
75   gchar **lines;
76   gint i = 0;
77
78   array = g_value_array_new (0);
79   g_value_init (&val, G_TYPE_STRING);
80
81   lines = g_strsplit (headers, "\r\n", 0);
82   line = lines[i];
83   while (line != NULL && *line != '\0') {
84     if (line[0] == '\t' || line[0] == ' ') {
85       /* WSP: continuation line */
86       if (header_value == NULL)
87         /* continuation line without a previous value */
88         goto fail;
89
90       tmp = g_strjoin (" ", header_value, g_strstrip (line), NULL);
91       g_free (header_value);
92       header_value = tmp;
93     } else {
94       if (header_name) {
95         g_value_take_string (&val, header_name);
96         g_value_array_append (array, &val);
97         g_value_take_string (&val, header_value);
98         g_value_array_append (array, &val);
99       }
100       /* search the column starting from line[1] as an header name can't be
101        * empty */
102       column = g_strstr_len (line + 1, strlen (line) - 1, ":");
103       if (column == NULL)
104         /* bad syntax */
105         goto fail;
106
107       if (*(space = column + 1) != ' ')
108         /* bad syntax */
109         goto fail;
110
111       header_name = g_strndup (line, column - line);
112       header_value = g_strdup (space + 1);
113     }
114
115     line = lines[++i];
116   }
117
118   if (header_name) {
119     g_value_take_string (&val, header_name);
120     g_value_array_append (array, &val);
121     g_value_take_string (&val, header_value);
122     g_value_array_append (array, &val);
123   }
124
125   g_value_unset (&val);
126   g_strfreev (lines);
127
128   return array;
129
130 fail:
131   GST_WARNING ("could not parse annodex headers");
132   g_free (header_name);
133   g_free (header_value);
134   g_strfreev (lines);
135   g_value_array_free (array);
136   g_value_unset (&val);
137   return NULL;
138 }
139
140 static gboolean
141 plugin_init (GstPlugin * plugin)
142 {
143   gst_tag_register (GST_TAG_CMML_STREAM, GST_TAG_FLAG_META,
144       GST_TYPE_CMML_TAG_STREAM, "cmml-stream", "annodex CMML stream tag", NULL);
145
146   gst_tag_register (GST_TAG_CMML_HEAD, GST_TAG_FLAG_META,
147       GST_TYPE_CMML_TAG_HEAD, "cmml-head", "annodex CMML head tag", NULL);
148
149   gst_tag_register (GST_TAG_CMML_CLIP, GST_TAG_FLAG_META,
150       GST_TYPE_CMML_TAG_CLIP, "cmml-clip", "annodex CMML clip tag", NULL);
151
152   gst_cmml_parser_init ();
153
154   if (!gst_cmml_enc_plugin_init (plugin))
155     return FALSE;
156
157   if (!gst_cmml_dec_plugin_init (plugin))
158     return FALSE;
159
160   return TRUE;
161 }
162
163 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
164     GST_VERSION_MINOR,
165     "annodex",
166     "annodex stream manipulation (info about annodex: http://www.annodex.net)",
167     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)