Branch data Line data Source code
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 : 0 : gst_mpeg_descriptor_free (GstMPEGDescriptor * desc)
43 : : {
44 [ # # ]: 0 : g_return_if_fail (desc != NULL);
45 : :
46 : 0 : g_free (desc);
47 : : }
48 : :
49 : : static guint
50 : 0 : 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 [ # # ]: 0 : if (size < 2)
57 : 0 : return 0;
58 : :
59 : 0 : tag = *data++;
60 : 0 : length = *data++;
61 : 0 : size -= 2;
62 : :
63 [ # # ]: 0 : GST_DEBUG ("tag: 0x%02x, length: %d", tag, length);
64 : :
65 [ # # ]: 0 : if (length > size)
66 : 0 : return 0;
67 : :
68 [ # # ]: 0 : GST_MEMDUMP ("tag contents:", data, length);
69 : :
70 : 0 : return length + 2;
71 : : }
72 : :
73 : : GstMPEGDescriptor *
74 : 0 : gst_mpeg_descriptor_parse (guint8 * data, guint size)
75 : : {
76 : : guint8 *current;
77 : : guint consumed, total, n_desc;
78 : : GstMPEGDescriptor *result;
79 : :
80 [ # # ]: 0 : g_return_val_if_fail (data != NULL, NULL);
81 : :
82 : 0 : current = data;
83 : 0 : total = 0;
84 : 0 : n_desc = 0;
85 : :
86 : : do {
87 : 0 : consumed = gst_mpeg_descriptor_parse_1 (current, size);
88 : :
89 [ # # ]: 0 : if (consumed > 0) {
90 : 0 : current += consumed;
91 : 0 : total += consumed;
92 : 0 : size -= consumed;
93 : 0 : n_desc++;
94 : : }
95 : : }
96 [ # # ]: 0 : while (consumed > 0);
97 : :
98 [ # # ]: 0 : GST_DEBUG ("parsed %d descriptors", n_desc);
99 : :
100 [ # # ]: 0 : if (total == 0)
101 : 0 : return NULL;
102 : :
103 : 0 : result = g_malloc (sizeof (GstMPEGDescriptor) + total);
104 : 0 : result->n_desc = n_desc;
105 : 0 : result->data_length = total;
106 : 0 : result->data = ((guint8 *) result) + sizeof (GstMPEGDescriptor);
107 : :
108 : 0 : memcpy (result->data, data, total);
109 : :
110 : 0 : return result;
111 : : }
112 : :
113 : : guint
114 : 0 : gst_mpeg_descriptor_n_desc (GstMPEGDescriptor * desc)
115 : : {
116 [ # # ]: 0 : g_return_val_if_fail (desc != NULL, 0);
117 : :
118 : 0 : return desc->n_desc;
119 : : }
120 : :
121 : : guint8 *
122 : 0 : gst_mpeg_descriptor_find (GstMPEGDescriptor * desc, gint tag)
123 : : {
124 : : guint8 length;
125 : : guint8 *current;
126 : : guint size;
127 : :
128 [ # # ]: 0 : g_return_val_if_fail (desc != NULL, NULL);
129 : :
130 : 0 : current = desc->data;
131 : 0 : length = desc->data_length;
132 : :
133 [ # # ]: 0 : while (length > 0) {
134 [ # # ]: 0 : if (DESC_TAG (current) == tag)
135 : 0 : return current;
136 : :
137 : 0 : size = DESC_LENGTH (current) + 2;
138 : :
139 : 0 : current += size;
140 : 0 : length -= size;
141 : : }
142 : 0 : return NULL;
143 : : }
144 : :
145 : : /* array needs freeing afterwards */
146 : : GArray *
147 : 0 : 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 [ # # ]: 0 : g_return_val_if_fail (desc != NULL, NULL);
156 : 0 : all = g_array_new (TRUE, TRUE, sizeof (guint8 *));
157 : :
158 : 0 : current = desc->data;
159 : 0 : length = desc->data_length;
160 : :
161 [ # # ]: 0 : while (length > 0) {
162 [ # # ]: 0 : if (DESC_TAG (current) == tag)
163 : 0 : g_array_append_val (all, current);
164 : 0 : size = DESC_LENGTH (current) + 2;
165 : :
166 : 0 : current += size;
167 : 0 : length -= size;
168 : : }
169 : :
170 [ # # ]: 0 : GST_DEBUG ("found tag 0x%02x %d times", tag, all->len);
171 : :
172 : 0 : return all;
173 : : }
174 : :
175 : : guint8 *
176 : 0 : gst_mpeg_descriptor_nth (GstMPEGDescriptor * desc, guint i)
177 : : {
178 : : guint8 length;
179 : : guint8 *current;
180 : : guint size;
181 : :
182 [ # # ]: 0 : g_return_val_if_fail (desc != NULL, NULL);
183 : :
184 [ # # ]: 0 : if (i > desc->n_desc)
185 : 0 : return NULL;
186 : :
187 : 0 : current = desc->data;
188 : 0 : length = desc->data_length;
189 : :
190 [ # # ]: 0 : while (length > 0) {
191 [ # # ]: 0 : if (i == 0)
192 : 0 : return current;
193 : :
194 : 0 : size = DESC_LENGTH (current) + 2;
195 : :
196 : 0 : current += size;
197 : 0 : length -= size;
198 : 0 : i--;
199 : :
200 : : }
201 : 0 : return NULL;
202 : : }
203 : :
204 : : void
205 : 6 : gst_mpegtsdesc_init_debug (void)
206 : : {
207 [ + - ]: 6 : GST_DEBUG_CATEGORY_INIT (gstmpegtsdesc_debug, "mpegtsdesc", 0,
208 : : "MPEG transport stream parser (descriptor)");
209 : 6 : }
|