1 /* GStreamer RealMedia utility functions
2 * Copyright (C) 2006 Tim-Philipp Müller <tim centricular net>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
28 gst_rm_utils_read_string8 (const guint8 * data, guint datalen,
39 length = GST_READ_UINT8 (data);
40 if (datalen < (1 + length))
44 *p_total_len = 1 + length;
46 return g_strndup ((gchar *) data + 1, length);
50 gst_rm_utils_read_string16 (const guint8 * data, guint datalen,
61 length = GST_READ_UINT16_BE (data);
62 if (datalen < (2 + length))
66 *p_total_len = 2 + length;
68 return g_strndup ((gchar *) data + 2, length);
72 gst_rm_utils_read_tags (const guint8 * data, guint datalen,
73 GstRmUtilsStringReadFunc read_string_func)
75 const gchar *gst_tags[] = { GST_TAG_TITLE, GST_TAG_ARTIST,
76 GST_TAG_COPYRIGHT, GST_TAG_COMMENT
81 g_assert (read_string_func != NULL);
83 GST_DEBUG ("File Content : (CONT) len = %d", datalen);
85 tags = gst_tag_list_new_empty ();
87 for (i = 0; i < G_N_ELEMENTS (gst_tags); ++i) {
89 guint total_length = 0;
91 str = read_string_func (data, datalen, &total_length);
93 datalen -= total_length;
95 if (str != NULL && !g_utf8_validate (str, -1, NULL)) {
96 const gchar *encoding;
99 encoding = g_getenv ("GST_TAG_ENCODING");
100 if (encoding == NULL || *encoding == '\0') {
101 if (g_get_charset (&encoding))
102 encoding = "ISO-8859-15";
104 GST_DEBUG ("converting tag from %s to UTF-8", encoding);
105 tmp = g_convert_with_fallback (str, -1, "UTF-8", encoding, (gchar *) "*",
111 GST_DEBUG ("%s = %s", gst_tags[i], GST_STR_NULL (str));
112 if (str != NULL && *str != '\0') {
113 gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, gst_tags[i], str, NULL);
118 if (gst_tag_list_n_tags (tags) > 0)
121 gst_tag_list_unref (tags);
126 gst_rm_utils_descramble_dnet_buffer (GstBuffer * buf)
129 guint8 *data, *end, tmp;
131 buf = gst_buffer_make_writable (buf);
133 /* dnet = byte-order swapped AC3 */
134 gst_buffer_map (buf, &map, GST_MAP_READWRITE);
136 end = data + map.size;
137 while ((data + 1) < end) {
142 data += sizeof (guint16);
144 gst_buffer_unmap (buf, &map);
149 gst_rm_utils_swap_nibbles (guint8 * data, gint idx1, gint idx2, gint len)
151 guint8 *d1, *d2, tmp1 = 0, tmp2, tmp1n, tmp2n;
153 if ((idx2 & 1) && !(idx1 & 1)) {
154 /* align destination to a byte by swapping the indexes */
159 d1 = data + (idx1 >> 1);
160 d2 = data + (idx2 >> 1);
162 /* check if we have aligned offsets and we can copy bytes */
163 if ((idx1 & 1) == (idx2 & 1)) {
165 /* swap first nibble */
168 *d1++ = (tmp2 & 0xf0) | (tmp1 & 0x0f);
169 *d2++ = (tmp1 & 0xf0) | (tmp2 & 0x0f);
172 for (; len > 1; len -= 2) {
179 /* swap leftover nibble */
182 *d1 = (tmp2 & 0x0f) | (tmp1 & 0xf0);
183 *d2 = (tmp1 & 0x0f) | (tmp2 & 0xf0);
186 /* preload nibbles from source */
190 for (; len > 1; len -= 2) {
191 /* assemble nibbles */
192 *d1++ = (tmp2n & 0x0f) | (tmp2 << 4);
194 *d2++ = (tmp1n << 4) | (tmp1 >> 4);
202 *d1 = (tmp2 << 4) | (tmp2n & 0x0f);
203 *d2 = (tmp1 >> 4) | (tmp2 & 0xf0);
205 *d1 = (tmp1 & 0xf0) | (tmp2n);
210 static const gint sipr_swap_index[38][2] = {
211 {0, 63}, {1, 22}, {2, 44}, {3, 90},
212 {5, 81}, {7, 31}, {8, 86}, {9, 58},
213 {10, 36}, {12, 68}, {13, 39}, {14, 73},
214 {15, 53}, {16, 69}, {17, 57}, {19, 88},
215 {20, 34}, {21, 71}, {24, 46}, {25, 94},
216 {26, 54}, {28, 75}, {29, 50}, {32, 70},
217 {33, 92}, {35, 74}, {38, 85}, {40, 56},
218 {42, 87}, {43, 65}, {45, 59}, {48, 79},
219 {49, 93}, {51, 89}, {55, 95}, {61, 76},
224 gst_rm_utils_descramble_sipr_buffer (GstBuffer * buf)
230 size = gst_buffer_get_size (buf);
232 /* split the packet in 96 blocks of nibbles */
237 buf = gst_buffer_make_writable (buf);
239 gst_buffer_map (buf, &map, GST_MAP_WRITE);
241 /* we need to perform 38 swaps on the blocks */
242 for (n = 0; n < 38; n++) {
245 /* get the indexes of the blocks of nibbles that need swapping */
246 idx1 = bs * sipr_swap_index[n][0];
247 idx2 = bs * sipr_swap_index[n][1];
249 /* swap the blocks */
250 gst_rm_utils_swap_nibbles (map.data, idx1, idx2, bs);
252 gst_buffer_unmap (buf, &map);
258 gst_rm_utils_run_tests (void)
261 guint8 tab1[] = { 0x10, 0x32, 0x54, 0x76, 0x98, 0xba, 0xdc, 0xfe };
264 memcpy (tab2, tab1, 8);
265 gst_util_dump_mem (tab2, 8);
267 gst_rm_utils_swap_nibbles (tab2, 0, 8, 4);
268 gst_util_dump_mem (tab2, 8);
269 memcpy (tab2, tab1, 8);
270 gst_rm_utils_swap_nibbles (tab2, 0, 8, 5);
271 gst_util_dump_mem (tab2, 8);
273 memcpy (tab2, tab1, 8);
274 gst_rm_utils_swap_nibbles (tab2, 1, 8, 4);
275 gst_util_dump_mem (tab2, 8);
276 memcpy (tab2, tab1, 8);
277 gst_rm_utils_swap_nibbles (tab2, 1, 8, 5);
278 gst_util_dump_mem (tab2, 8);
280 memcpy (tab2, tab1, 8);
281 gst_rm_utils_swap_nibbles (tab2, 0, 9, 4);
282 gst_util_dump_mem (tab2, 8);
283 memcpy (tab2, tab1, 8);
284 gst_rm_utils_swap_nibbles (tab2, 0, 9, 5);
285 gst_util_dump_mem (tab2, 8);
287 memcpy (tab2, tab1, 8);
288 gst_rm_utils_swap_nibbles (tab2, 1, 9, 4);
289 gst_util_dump_mem (tab2, 8);
290 memcpy (tab2, tab1, 8);
291 gst_rm_utils_swap_nibbles (tab2, 1, 9, 5);
292 gst_util_dump_mem (tab2, 8);