Add -Wold-style-definition
[platform/upstream/gstreamer.git] / gst / mpegdemux / gstmpegdesc.c
1 /* 
2  * The contents of this file are subject to the Mozilla Public License
3  * Version 1.1 (the "License"); you may not use this file except in
4  * compliance with the License. You may obtain a copy of the License at
5  * http://www.mozilla.org/MPL/.
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
9  * License for the specific language governing rights and limitations
10  * under the License.
11  *
12  * The Original Code is Fluendo MPEG Demuxer plugin.
13  *
14  * The Initial Developer of the Original Code is Fluendo, S.L.
15  * Portions created by Fluendo, S.L. are Copyright (C) 2005
16  * Fluendo, S.L. All Rights Reserved.
17  *
18  * Contributor(s): Wim Taymans <wim@fluendo.com>
19  *
20  * Alternatively, the contents of this file may be used under the terms of
21  * the GNU Lesser General Public License Version 2 or later (the "LGPL"),
22  * in which case the provisions of the LGPL are applicable instead
23  * of those above. If you wish to allow use of your version of this file only
24  * under the terms of the LGPL, and not to allow others to
25  * use your version of this file under the terms of the MPL, indicate your
26  * decision by deleting the provisions above and replace them with the notice
27  * and other provisions required by the LGPL. If you do not delete
28  * the provisions above, a recipient may use your version of this file under
29  * the terms of the MPL or the LGPL.
30  */
31
32 #include <string.h>
33
34 #include <gst/gst.h>
35
36 #include "gstmpegdesc.h"
37
38 GST_DEBUG_CATEGORY (gstmpegtsdesc_debug);
39 #define GST_CAT_DEFAULT (gstmpegtsdesc_debug)
40
41 void
42 gst_mpeg_descriptor_free (GstMPEGDescriptor * desc)
43 {
44   g_return_if_fail (desc != NULL);
45
46   g_free (desc);
47 }
48
49 static guint
50 gst_mpeg_descriptor_parse_1 (guint8 * data, guint size)
51 {
52   guint8 tag;
53   guint8 length;
54
55   /* need at least 2 bytes for tag and length */
56   if (size < 2)
57     return 0;
58
59   tag = *data++;
60   length = *data++;
61   size -= 2;
62
63   GST_DEBUG ("tag: 0x%02x, length: %d", tag, length);
64
65   if (length > size)
66     return 0;
67
68   GST_MEMDUMP ("tag contents:", data, length);
69
70   return length + 2;
71 }
72
73 GstMPEGDescriptor *
74 gst_mpeg_descriptor_parse (guint8 * data, guint size)
75 {
76   guint8 *current;
77   guint consumed, total, n_desc;
78   GstMPEGDescriptor *result;
79
80   g_return_val_if_fail (data != NULL, NULL);
81
82   current = data;
83   total = 0;
84   n_desc = 0;
85
86   do {
87     consumed = gst_mpeg_descriptor_parse_1 (current, size);
88
89     if (consumed > 0) {
90       current += consumed;
91       total += consumed;
92       size -= consumed;
93       n_desc++;
94     }
95   }
96   while (consumed > 0);
97
98   GST_DEBUG ("parsed %d descriptors", n_desc);
99
100   if (total == 0)
101     return NULL;
102
103   result = g_malloc (sizeof (GstMPEGDescriptor) + total);
104   result->n_desc = n_desc;
105   result->data_length = total;
106   result->data = ((guint8 *) result) + sizeof (GstMPEGDescriptor);
107
108   memcpy (result->data, data, total);
109
110   return result;
111 }
112
113 guint
114 gst_mpeg_descriptor_n_desc (GstMPEGDescriptor * desc)
115 {
116   g_return_val_if_fail (desc != NULL, 0);
117
118   return desc->n_desc;
119 }
120
121 guint8 *
122 gst_mpeg_descriptor_find (GstMPEGDescriptor * desc, gint tag)
123 {
124   guint8 length;
125   guint8 *current;
126   guint size;
127
128   g_return_val_if_fail (desc != NULL, NULL);
129
130   current = desc->data;
131   length = desc->data_length;
132
133   while (length > 0) {
134     if (DESC_TAG (current) == tag)
135       return current;
136
137     size = DESC_LENGTH (current) + 2;
138
139     current += size;
140     length -= size;
141   }
142   return NULL;
143 }
144
145 /* array needs freeing afterwards */
146 GArray *
147 gst_mpeg_descriptor_find_all (GstMPEGDescriptor * desc, gint tag)
148 {
149   GArray *all;
150
151   guint8 length;
152   guint8 *current;
153   guint size;
154
155   g_return_val_if_fail (desc != NULL, NULL);
156   all = g_array_new (TRUE, TRUE, sizeof (guint8 *));
157
158   current = desc->data;
159   length = desc->data_length;
160
161   while (length > 0) {
162     if (DESC_TAG (current) == tag)
163       g_array_append_val (all, current);
164     size = DESC_LENGTH (current) + 2;
165
166     current += size;
167     length -= size;
168   }
169
170   GST_DEBUG ("found tag 0x%02x %d times", tag, all->len);
171
172   return all;
173 }
174
175 guint8 *
176 gst_mpeg_descriptor_nth (GstMPEGDescriptor * desc, guint i)
177 {
178   guint8 length;
179   guint8 *current;
180   guint size;
181
182   g_return_val_if_fail (desc != NULL, NULL);
183
184   if (i > desc->n_desc)
185     return NULL;
186
187   current = desc->data;
188   length = desc->data_length;
189
190   while (length > 0) {
191     if (i == 0)
192       return current;
193
194     size = DESC_LENGTH (current) + 2;
195
196     current += size;
197     length -= size;
198     i--;
199
200   }
201   return NULL;
202 }
203
204 void
205 gst_mpegtsdesc_init_debug (void)
206 {
207   GST_DEBUG_CATEGORY_INIT (gstmpegtsdesc_debug, "mpegtsdesc", 0,
208       "MPEG transport stream parser (descriptor)");
209 }