~ubuntu-branches/ubuntu/quantal/gst-plugins-bad0.10/quantal-proposed

« back to all changes in this revision

Viewing changes to gst/mpegtsdemux/gstmpegdesc.c

  • Committer: Bazaar Package Importer
  • Author(s): Ken VanDine
  • Date: 2011-07-19 14:32:43 UTC
  • mfrom: (18.4.21 sid)
  • Revision ID: james.westby@ubuntu.com-20110719143243-p7pnkh45akfp0ihk
Tags: 0.10.22-2ubuntu1
* Rebased on debian unstable, remaining changes:
  - debian/gstreamer-plugins-bad.install
    * don't include dtmf, liveadder, rtpmux, autoconvert and shm, we include 
      them in -good

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* 
 
2
 *
 
3
 * This library is free software; you can redistribute it and/or
 
4
 * modify it under the terms of the GNU Library General Public
 
5
 * License as published by the Free Software Foundation; either
 
6
 * version 2 of the License, or (at your option) any later version.
 
7
 *
 
8
 * This library is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
11
 * Library General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU Library General Public
 
14
 * License along with this library; if not, write to the
 
15
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
16
 * Boston, MA 02111-1307, USA.
 
17
 *
 
18
 * The Original Code is Fluendo MPEG Demuxer plugin.
 
19
 *
 
20
 * The Initial Developer of the Original Code is Fluendo, S.L.
 
21
 * Portions created by Fluendo, S.L. are Copyright (C) 2005
 
22
 * Fluendo, S.L. All Rights Reserved.
 
23
 *
 
24
 * Contributor(s): Wim Taymans <wim@fluendo.com>
 
25
 *
 
26
 */
 
27
 
 
28
#include <string.h>
 
29
 
 
30
#include <gst/gst.h>
 
31
 
 
32
#include "gstmpegdesc.h"
 
33
 
 
34
GST_DEBUG_CATEGORY (gstmpegtsdesc_debug);
 
35
#define GST_CAT_DEFAULT (gstmpegtsdesc_debug)
 
36
 
 
37
void
 
38
gst_mpeg_descriptor_free (GstMPEGDescriptor * desc)
 
39
{
 
40
  g_return_if_fail (desc != NULL);
 
41
 
 
42
  g_free (desc);
 
43
}
 
44
 
 
45
static guint
 
46
gst_mpeg_descriptor_parse_1 (guint8 * data, guint size)
 
47
{
 
48
  guint8 tag;
 
49
  guint8 length;
 
50
 
 
51
  /* need at least 2 bytes for tag and length */
 
52
  if (size < 2)
 
53
    return 0;
 
54
 
 
55
  tag = *data++;
 
56
  length = *data++;
 
57
  size -= 2;
 
58
 
 
59
  GST_DEBUG ("tag: 0x%02x, length: %d", tag, length);
 
60
 
 
61
  if (length > size)
 
62
    return 0;
 
63
 
 
64
  GST_MEMDUMP ("tag contents:", data, length);
 
65
 
 
66
  return length + 2;
 
67
}
 
68
 
 
69
GstMPEGDescriptor *
 
70
gst_mpeg_descriptor_parse (guint8 * data, guint size)
 
71
{
 
72
  guint8 *current;
 
73
  guint consumed, total, n_desc;
 
74
  GstMPEGDescriptor *result;
 
75
 
 
76
  g_return_val_if_fail (data != NULL, NULL);
 
77
 
 
78
  current = data;
 
79
  total = 0;
 
80
  n_desc = 0;
 
81
 
 
82
  do {
 
83
    consumed = gst_mpeg_descriptor_parse_1 (current, size);
 
84
 
 
85
    if (consumed > 0) {
 
86
      current += consumed;
 
87
      total += consumed;
 
88
      size -= consumed;
 
89
      n_desc++;
 
90
    }
 
91
  }
 
92
  while (consumed > 0);
 
93
 
 
94
  GST_DEBUG ("parsed %d descriptors", n_desc);
 
95
 
 
96
  if (total == 0)
 
97
    return NULL;
 
98
 
 
99
  result = g_malloc (sizeof (GstMPEGDescriptor) + total);
 
100
  result->n_desc = n_desc;
 
101
  result->data_length = total;
 
102
  result->data = ((guint8 *) result) + sizeof (GstMPEGDescriptor);
 
103
 
 
104
  memcpy (result->data, data, total);
 
105
 
 
106
  return result;
 
107
}
 
108
 
 
109
guint
 
110
gst_mpeg_descriptor_n_desc (GstMPEGDescriptor * desc)
 
111
{
 
112
  g_return_val_if_fail (desc != NULL, 0);
 
113
 
 
114
  return desc->n_desc;
 
115
}
 
116
 
 
117
guint8 *
 
118
gst_mpeg_descriptor_find (GstMPEGDescriptor * desc, gint tag)
 
119
{
 
120
  guint8 length;
 
121
  guint8 *current;
 
122
  guint size;
 
123
 
 
124
  g_return_val_if_fail (desc != NULL, NULL);
 
125
 
 
126
  current = desc->data;
 
127
  length = desc->data_length;
 
128
 
 
129
  while (length > 0) {
 
130
    if (DESC_TAG (current) == tag)
 
131
      return current;
 
132
 
 
133
    size = DESC_LENGTH (current) + 2;
 
134
 
 
135
    current += size;
 
136
    length -= size;
 
137
  }
 
138
  return NULL;
 
139
}
 
140
 
 
141
/* array needs freeing afterwards */
 
142
GArray *
 
143
gst_mpeg_descriptor_find_all (GstMPEGDescriptor * desc, gint tag)
 
144
{
 
145
  GArray *all;
 
146
 
 
147
  guint8 length;
 
148
  guint8 *current;
 
149
  guint size;
 
150
 
 
151
  g_return_val_if_fail (desc != NULL, NULL);
 
152
  all = g_array_new (TRUE, TRUE, sizeof (guint8 *));
 
153
 
 
154
  current = desc->data;
 
155
  length = desc->data_length;
 
156
 
 
157
  while (length > 0) {
 
158
    if (DESC_TAG (current) == tag)
 
159
      g_array_append_val (all, current);
 
160
    size = DESC_LENGTH (current) + 2;
 
161
 
 
162
    current += size;
 
163
    length -= size;
 
164
  }
 
165
 
 
166
  GST_DEBUG ("found tag 0x%02x %d times", tag, all->len);
 
167
 
 
168
  return all;
 
169
}
 
170
 
 
171
guint8 *
 
172
gst_mpeg_descriptor_nth (GstMPEGDescriptor * desc, guint i)
 
173
{
 
174
  guint8 length;
 
175
  guint8 *current;
 
176
  guint size;
 
177
 
 
178
  g_return_val_if_fail (desc != NULL, NULL);
 
179
 
 
180
  if (i > desc->n_desc)
 
181
    return NULL;
 
182
 
 
183
  current = desc->data;
 
184
  length = desc->data_length;
 
185
 
 
186
  while (length > 0) {
 
187
    if (i == 0)
 
188
      return current;
 
189
 
 
190
    size = DESC_LENGTH (current) + 2;
 
191
 
 
192
    current += size;
 
193
    length -= size;
 
194
    i--;
 
195
 
 
196
  }
 
197
  return NULL;
 
198
}
 
199
 
 
200
void
 
201
gst_mpegtsdesc_init_debug (void)
 
202
{
 
203
  GST_DEBUG_CATEGORY_INIT (gstmpegtsdesc_debug, "mpegtsdesc", 0,
 
204
      "MPEG transport stream parser (descriptor)");
 
205
}