1 /* GStreamer
2  * Copyright (C) 2008-2009 Sebastian Dröge <sebastian.droege@collabora.co.uk>
3  *
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.
8  *
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.
13  *
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.
18  */
19 
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23 
24 #include <gst/gst.h>
25 #include <gst/base/gstbytewriter.h>
26 #include <string.h>
27 
28 #include "mxftypes.h"
29 
30 GST_DEBUG_CATEGORY_EXTERN (mxf_debug);
31 #define GST_CAT_DEFAULT mxf_debug
32 
33 gboolean
mxf_is_mxf_packet(const MXFUL * ul)34 mxf_is_mxf_packet (const MXFUL * ul)
35 {
36   return mxf_ul_is_subclass (MXF_UL (SMPTE), ul);
37 }
38 
39 /* SMPTE 377M 6.1: Check if this is a valid partition pack */
40 gboolean
mxf_is_partition_pack(const MXFUL * ul)41 mxf_is_partition_pack (const MXFUL * ul)
42 {
43   if (mxf_ul_is_subclass (MXF_UL (PARTITION_PACK), ul) &&
44       ul->u[13] >= 0x02 && ul->u[13] <= 0x04 &&
45       ul->u[14] < 0x05 && ul->u[15] == 0x00)
46     return TRUE;
47 
48   return FALSE;
49 }
50 
51 /* SMPTE 377M 6.2: header partition pack has byte 14 == 0x02 */
52 gboolean
mxf_is_header_partition_pack(const MXFUL * ul)53 mxf_is_header_partition_pack (const MXFUL * ul)
54 {
55   if (mxf_is_partition_pack (ul) && ul->u[13] == 0x02)
56     return TRUE;
57 
58   return FALSE;
59 }
60 
61 /* SMPTE 377M 6.3: body partition pack has byte 14 == 0x03 */
62 gboolean
mxf_is_body_partition_pack(const MXFUL * ul)63 mxf_is_body_partition_pack (const MXFUL * ul)
64 {
65   if (mxf_is_partition_pack (ul) && ul->u[13] == 0x03)
66     return TRUE;
67 
68   return FALSE;
69 }
70 
71 /* SMPTE 377M 6.4: footer partition pack has byte 14 == 0x04 */
72 gboolean
mxf_is_footer_partition_pack(const MXFUL * ul)73 mxf_is_footer_partition_pack (const MXFUL * ul)
74 {
75   if (mxf_is_partition_pack (ul) && ul->u[13] == 0x04)
76     return TRUE;
77 
78   return FALSE;
79 }
80 
81 gboolean
mxf_is_fill(const MXFUL * ul)82 mxf_is_fill (const MXFUL * ul)
83 {
84   return (mxf_ul_is_subclass (MXF_UL (FILL), ul));
85 }
86 
87 gboolean
mxf_is_primer_pack(const MXFUL * ul)88 mxf_is_primer_pack (const MXFUL * ul)
89 {
90   return (mxf_ul_is_subclass (MXF_UL (PRIMER_PACK), ul));
91 }
92 
93 gboolean
mxf_is_metadata(const MXFUL * ul)94 mxf_is_metadata (const MXFUL * ul)
95 {
96   return (mxf_ul_is_subclass (MXF_UL (METADATA), ul));
97 }
98 
99 /* SMPTE 377M 8.7.3 */
100 gboolean
mxf_is_descriptive_metadata(const MXFUL * ul)101 mxf_is_descriptive_metadata (const MXFUL * ul)
102 {
103   return (mxf_ul_is_subclass (MXF_UL (DESCRIPTIVE_METADATA), ul));
104 }
105 
106 gboolean
mxf_is_random_index_pack(const MXFUL * ul)107 mxf_is_random_index_pack (const MXFUL * ul)
108 {
109   return (mxf_ul_is_subclass (MXF_UL (RANDOM_INDEX_PACK), ul));
110 }
111 
112 gboolean
mxf_is_index_table_segment(const MXFUL * ul)113 mxf_is_index_table_segment (const MXFUL * ul)
114 {
115   return (mxf_ul_is_subclass (MXF_UL (INDEX_TABLE_SEGMENT), ul));
116 }
117 
118 /* SMPTE 379M 6.2.1 */
119 gboolean
mxf_is_generic_container_system_item(const MXFUL * ul)120 mxf_is_generic_container_system_item (const MXFUL * ul)
121 {
122   return (mxf_ul_is_subclass (MXF_UL (GENERIC_CONTAINER_SYSTEM_ITEM), ul) &&
123       (ul->u[12] == 0x04 || ul->u[12] == 0x14));
124 }
125 
126 /* SMPTE 379M 7.1 */
127 gboolean
mxf_is_generic_container_essence_element(const MXFUL * ul)128 mxf_is_generic_container_essence_element (const MXFUL * ul)
129 {
130   return (mxf_ul_is_subclass (MXF_UL (GENERIC_CONTAINER_ESSENCE_ELEMENT), ul)
131       && (ul->u[12] == 0x05 || ul->u[12] == 0x06
132           || ul->u[12] == 0x07 || ul->u[12] == 0x15
133           || ul->u[12] == 0x16 || ul->u[12] == 0x17 || ul->u[12] == 0x18));
134 }
135 
136 /* SMPTE 379M 8 */
137 gboolean
mxf_is_generic_container_essence_container_label(const MXFUL * ul)138 mxf_is_generic_container_essence_container_label (const MXFUL * ul)
139 {
140   return (mxf_ul_is_subclass (MXF_UL
141           (GENERIC_CONTAINER_ESSENCE_CONTAINER_LABEL), ul) && (ul->u[12] == 0x01
142           || ul->u[12] == 0x02));
143 }
144 
145 /* Essence container label found in files generated by Avid */
146 gboolean
mxf_is_avid_essence_container_label(const MXFUL * ul)147 mxf_is_avid_essence_container_label (const MXFUL * ul)
148 {
149   return (mxf_ul_is_subclass (MXF_UL (AVID_ESSENCE_CONTAINER_ESSENCE_LABEL),
150           ul));
151 }
152 
153 /* Essence element key found in files generated by Avid */
154 gboolean
mxf_is_avid_essence_container_essence_element(const MXFUL * ul)155 mxf_is_avid_essence_container_essence_element (const MXFUL * ul)
156 {
157   return (mxf_ul_is_subclass (MXF_UL (AVID_ESSENCE_CONTAINER_ESSENCE_ELEMENT),
158           ul));
159 }
160 
161 guint
mxf_ber_encode_size(guint size,guint8 ber[9])162 mxf_ber_encode_size (guint size, guint8 ber[9])
163 {
164   guint8 slen, i;
165   guint8 tmp[8];
166 
167   memset (ber, 0, 9);
168 
169   if (size <= 127) {
170     ber[0] = size;
171     return 1;
172   }
173 
174   slen = 0;
175   while (size > 0) {
176     tmp[slen] = size & 0xff;
177     size >>= 8;
178     slen++;
179   }
180 
181   ber[0] = 0x80 | slen;
182   for (i = 0; i < slen; i++) {
183     ber[i + 1] = tmp[slen - i - 1];
184   }
185 
186   return slen + 1;
187 }
188 
189 GstBuffer *
mxf_fill_to_buffer(guint size)190 mxf_fill_to_buffer (guint size)
191 {
192   GstBuffer *ret;
193   GstMapInfo map;
194   guint slen;
195   guint8 ber[9];
196 
197   slen = mxf_ber_encode_size (size, ber);
198 
199   ret = gst_buffer_new_and_alloc (16 + slen + size);
200   gst_buffer_map (ret, &map, GST_MAP_WRITE);
201 
202   memcpy (map.data, MXF_UL (FILL), 16);
203   memcpy (map.data + 16, &ber, slen);
204   memset (map.data + slen, 0, size);
205 
206   gst_buffer_unmap (ret, &map);
207 
208   return ret;
209 }
210 
211 void
mxf_uuid_init(MXFUUID * uuid,GHashTable * hashtable)212 mxf_uuid_init (MXFUUID * uuid, GHashTable * hashtable)
213 {
214   guint i;
215 
216   do {
217     for (i = 0; i < 4; i++)
218       GST_WRITE_UINT32_BE (&uuid->u[i * 4], g_random_int ());
219     uuid->u[6] = 0x40 | (uuid->u[6] & 0x0f);
220     uuid->u[8] = (uuid->u[8] & 0xbf) | 0x80;
221   } while (hashtable && (mxf_uuid_is_zero (uuid) ||
222           g_hash_table_lookup_extended (hashtable, uuid, NULL, NULL)));
223 }
224 
225 gboolean
mxf_uuid_is_equal(const MXFUUID * a,const MXFUUID * b)226 mxf_uuid_is_equal (const MXFUUID * a, const MXFUUID * b)
227 {
228   g_return_val_if_fail (a != NULL, FALSE);
229   g_return_val_if_fail (b != NULL, FALSE);
230 
231   return (memcmp (a, b, 16) == 0);
232 }
233 
234 gboolean
mxf_uuid_is_zero(const MXFUUID * a)235 mxf_uuid_is_zero (const MXFUUID * a)
236 {
237   static const guint8 zero[16] = { 0x00, };
238 
239   g_return_val_if_fail (a != NULL, FALSE);
240 
241   return (memcmp (a, zero, 16) == 0);
242 }
243 
244 guint
mxf_uuid_hash(const MXFUUID * uuid)245 mxf_uuid_hash (const MXFUUID * uuid)
246 {
247   guint32 ret = 0;
248   guint i;
249 
250   g_return_val_if_fail (uuid != NULL, 0);
251 
252   for (i = 0; i < 4; i++)
253     ret ^= GST_READ_UINT32_BE (uuid->u + i * 4);
254 
255   return ret;
256 }
257 
258 gchar *
mxf_uuid_to_string(const MXFUUID * uuid,gchar str[48])259 mxf_uuid_to_string (const MXFUUID * uuid, gchar str[48])
260 {
261   gchar *ret = str;
262 
263   g_return_val_if_fail (uuid != NULL, NULL);
264 
265   if (ret == NULL)
266     ret = g_malloc (48);
267 
268   g_snprintf (ret, 48,
269       "%02x.%02x.%02x.%02x."
270       "%02x.%02x.%02x.%02x."
271       "%02x.%02x.%02x.%02x."
272       "%02x.%02x.%02x.%02x",
273       uuid->u[0], uuid->u[1], uuid->u[2], uuid->u[3],
274       uuid->u[4], uuid->u[5], uuid->u[6], uuid->u[7],
275       uuid->u[8], uuid->u[9], uuid->u[10], uuid->u[11],
276       uuid->u[12], uuid->u[13], uuid->u[14], uuid->u[15]);
277 
278   return ret;
279 }
280 
281 MXFUUID *
mxf_uuid_from_string(const gchar * str,MXFUUID * uuid)282 mxf_uuid_from_string (const gchar * str, MXFUUID * uuid)
283 {
284   MXFUUID *ret = uuid;
285   gint len;
286   guint i, j;
287 
288   g_return_val_if_fail (str != NULL, NULL);
289 
290   len = strlen (str);
291   if (len != 47) {
292     GST_ERROR ("Invalid UUID string length %d, should be 47", len);
293     return NULL;
294   }
295 
296   if (ret == NULL)
297     ret = g_new0 (MXFUUID, 1);
298 
299   memset (ret, 0, 16);
300 
301   for (i = 0, j = 0; i < 16; i++) {
302     if (!g_ascii_isxdigit (str[j]) ||
303         !g_ascii_isxdigit (str[j + 1]) ||
304         (str[j + 2] != '.' && str[j + 2] != '\0')) {
305       GST_ERROR ("Invalid UL string '%s'", str);
306       if (uuid == NULL)
307         g_free (ret);
308       return NULL;
309     }
310 
311     ret->u[i] = (g_ascii_xdigit_value (str[j]) << 4) |
312         (g_ascii_xdigit_value (str[j + 1]));
313     j += 3;
314   }
315   return ret;
316 }
317 
318 gboolean
mxf_uuid_array_parse(MXFUUID ** array,guint32 * count,const guint8 * data,guint size)319 mxf_uuid_array_parse (MXFUUID ** array, guint32 * count, const guint8 * data,
320     guint size)
321 {
322   guint32 element_count, element_size;
323   guint i;
324 
325   g_return_val_if_fail (array != NULL, FALSE);
326   g_return_val_if_fail (count != NULL, FALSE);
327 
328   if (size < 8)
329     return FALSE;
330 
331   g_return_val_if_fail (data != NULL, FALSE);
332 
333   element_count = GST_READ_UINT32_BE (data);
334   data += 4;
335   size -= 4;
336 
337   if (element_count == 0) {
338     *array = NULL;
339     *count = 0;
340     return TRUE;
341   }
342 
343   element_size = GST_READ_UINT32_BE (data);
344   data += 4;
345   size -= 4;
346 
347   if (element_size != 16) {
348     *array = NULL;
349     *count = 0;
350     return FALSE;
351   }
352 
353   if (element_count > size / 16) {
354     *array = NULL;
355     *count = 0;
356     return FALSE;
357   }
358 
359   *array = g_new (MXFUUID, element_count);
360   *count = element_count;
361 
362   for (i = 0; i < element_count; i++) {
363     memcpy (&((*array)[i]), data, 16);
364     data += 16;
365   }
366 
367   return TRUE;
368 }
369 
370 gboolean
mxf_umid_is_equal(const MXFUMID * a,const MXFUMID * b)371 mxf_umid_is_equal (const MXFUMID * a, const MXFUMID * b)
372 {
373   return (memcmp (a, b, 32) == 0);
374 }
375 
376 gboolean
mxf_umid_is_zero(const MXFUMID * umid)377 mxf_umid_is_zero (const MXFUMID * umid)
378 {
379   static const MXFUMID zero = { {0,} };
380 
381   return (memcmp (umid, &zero, 32) == 0);
382 }
383 
384 gchar *
mxf_umid_to_string(const MXFUMID * umid,gchar str[96])385 mxf_umid_to_string (const MXFUMID * umid, gchar str[96])
386 {
387   g_return_val_if_fail (umid != NULL, NULL);
388   g_return_val_if_fail (str != NULL, NULL);
389 
390   g_snprintf (str, 96,
391       "%02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x."
392       "%02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x",
393       umid->u[0], umid->u[1], umid->u[2], umid->u[3], umid->u[4], umid->u[5],
394       umid->u[6], umid->u[7], umid->u[8], umid->u[9], umid->u[10], umid->u[11],
395       umid->u[12], umid->u[13], umid->u[14], umid->u[15],
396       umid->u[16],
397       umid->u[17],
398       umid->u[18],
399       umid->u[19],
400       umid->u[20],
401       umid->u[21],
402       umid->u[22],
403       umid->u[23],
404       umid->u[24],
405       umid->u[25],
406       umid->u[26], umid->u[27], umid->u[28], umid->u[29], umid->u[30],
407       umid->u[31]
408       );
409 
410   return str;
411 }
412 
413 MXFUMID *
mxf_umid_from_string(const gchar * str,MXFUMID * umid)414 mxf_umid_from_string (const gchar * str, MXFUMID * umid)
415 {
416   gint len;
417   guint i, j;
418 
419   g_return_val_if_fail (str != NULL, NULL);
420   len = strlen (str);
421 
422   memset (umid, 0, 32);
423 
424   if (len != 95) {
425     GST_ERROR ("Invalid UMID string length %d", len);
426     return NULL;
427   }
428 
429   for (i = 0, j = 0; i < 32; i++) {
430     if (!g_ascii_isxdigit (str[j]) ||
431         !g_ascii_isxdigit (str[j + 1]) ||
432         (str[j + 2] != '.' && str[j + 2] != '\0')) {
433       GST_ERROR ("Invalid UMID string '%s'", str);
434       return NULL;
435     }
436 
437     umid->u[i] =
438         (g_ascii_xdigit_value (str[j]) << 4) | (g_ascii_xdigit_value (str[j +
439                 1]));
440     j += 3;
441   }
442   return umid;
443 }
444 
445 void
mxf_umid_init(MXFUMID * umid)446 mxf_umid_init (MXFUMID * umid)
447 {
448   guint i;
449   guint32 tmp;
450 
451   /* SMPTE S330M 5.1.1:
452    *    UMID Identifier
453    */
454   umid->u[0] = 0x06;
455   umid->u[1] = 0x0a;
456   umid->u[2] = 0x2b;
457   umid->u[3] = 0x34;
458   umid->u[4] = 0x01;
459   umid->u[5] = 0x01;
460   umid->u[6] = 0x01;
461   umid->u[7] = 0x05;            /* version, see RP210 */
462   umid->u[8] = 0x01;
463   umid->u[9] = 0x01;
464   umid->u[10] = 0x0d;           /* mixed group of components in a single container */
465 
466   /* - UUID/UL method for material number
467    * - 24 bit PRG for instance number
468    */
469   umid->u[11] = 0x20 | 0x02;
470 
471   /* Length of remaining data */
472   umid->u[12] = 0x13;
473 
474   /* Instance number */
475   tmp = g_random_int ();
476   umid->u[13] = (tmp >> 24) & 0xff;
477   umid->u[14] = (tmp >> 16) & 0xff;
478   umid->u[15] = (tmp >> 8) & 0xff;
479 
480   /* Material number: ISO UUID Version 4 */
481   for (i = 16; i < 32; i += 4)
482     GST_WRITE_UINT32_BE (&umid->u[i], g_random_int ());
483 
484   umid->u[16 + 6] &= 0x0f;
485   umid->u[16 + 6] |= 0x40;
486 
487   umid->u[16 + 8] &= 0x3f;
488   umid->u[16 + 8] |= 0x80;
489 }
490 
491 gboolean
mxf_timestamp_parse(MXFTimestamp * timestamp,const guint8 * data,guint size)492 mxf_timestamp_parse (MXFTimestamp * timestamp, const guint8 * data, guint size)
493 {
494   g_return_val_if_fail (timestamp != NULL, FALSE);
495 
496   memset (timestamp, 0, sizeof (MXFTimestamp));
497 
498   if (size < 8)
499     return FALSE;
500 
501   g_return_val_if_fail (data != NULL, FALSE);
502 
503   timestamp->year = GST_READ_UINT16_BE (data);
504   timestamp->month = GST_READ_UINT8 (data + 2);
505   timestamp->day = GST_READ_UINT8 (data + 3);
506   timestamp->hour = GST_READ_UINT8 (data + 4);
507   timestamp->minute = GST_READ_UINT8 (data + 5);
508   timestamp->second = GST_READ_UINT8 (data + 6);
509   timestamp->msecond = (GST_READ_UINT8 (data + 7) * 1000) / 256;
510 
511   return TRUE;
512 }
513 
514 /* SMPTE 377M 3.3: A value of 0 for every field means unknown timestamp */
515 gboolean
mxf_timestamp_is_unknown(const MXFTimestamp * a)516 mxf_timestamp_is_unknown (const MXFTimestamp * a)
517 {
518   static const MXFTimestamp unknown = { 0, 0, 0, 0, 0, 0, 0 };
519 
520   return (memcmp (a, &unknown, sizeof (MXFTimestamp)) == 0);
521 }
522 
523 gint
mxf_timestamp_compare(const MXFTimestamp * a,const MXFTimestamp * b)524 mxf_timestamp_compare (const MXFTimestamp * a, const MXFTimestamp * b)
525 {
526   gint diff;
527 
528   if ((diff = a->year - b->year) != 0)
529     return diff;
530   else if ((diff = a->month - b->month) != 0)
531     return diff;
532   else if ((diff = a->day - b->day) != 0)
533     return diff;
534   else if ((diff = a->hour - b->hour) != 0)
535     return diff;
536   else if ((diff = a->minute - b->minute) != 0)
537     return diff;
538   else if ((diff = a->second - b->second) != 0)
539     return diff;
540   else if ((diff = a->msecond - b->msecond) != 0)
541     return diff;
542   else
543     return 0;
544 }
545 
546 gchar *
mxf_timestamp_to_string(const MXFTimestamp * t,gchar str[32])547 mxf_timestamp_to_string (const MXFTimestamp * t, gchar str[32])
548 {
549   g_snprintf (str, 32,
550       "%04d-%02u-%02u %02u:%02u:%02u.%03u", t->year, t->month,
551       t->day, t->hour, t->minute, t->second, t->msecond);
552   return str;
553 }
554 
555 void
mxf_timestamp_set_now(MXFTimestamp * timestamp)556 mxf_timestamp_set_now (MXFTimestamp * timestamp)
557 {
558   GTimeVal tv;
559   time_t t;
560   struct tm *tm;
561 
562 #ifdef HAVE_GMTIME_R
563   struct tm tm_;
564 #endif
565 
566   g_get_current_time (&tv);
567   t = (time_t) tv.tv_sec;
568 
569 #ifdef HAVE_GMTIME_R
570   tm = gmtime_r (&t, &tm_);
571 #else
572   tm = gmtime (&t);
573 #endif
574 
575   timestamp->year = tm->tm_year + 1900;
576   timestamp->month = tm->tm_mon;
577   timestamp->day = tm->tm_mday;
578   timestamp->hour = tm->tm_hour;
579   timestamp->minute = tm->tm_min;
580   timestamp->second = tm->tm_sec;
581   timestamp->msecond = tv.tv_usec / 1000;
582 }
583 
584 void
mxf_timestamp_write(const MXFTimestamp * timestamp,guint8 * data)585 mxf_timestamp_write (const MXFTimestamp * timestamp, guint8 * data)
586 {
587   GST_WRITE_UINT16_BE (data, timestamp->year);
588   GST_WRITE_UINT8 (data + 2, timestamp->month);
589   GST_WRITE_UINT8 (data + 3, timestamp->day);
590   GST_WRITE_UINT8 (data + 4, timestamp->hour);
591   GST_WRITE_UINT8 (data + 5, timestamp->minute);
592   GST_WRITE_UINT8 (data + 6, timestamp->second);
593   GST_WRITE_UINT8 (data + 7, (timestamp->msecond * 256) / 1000);
594 }
595 
596 gboolean
mxf_fraction_parse(MXFFraction * fraction,const guint8 * data,guint size)597 mxf_fraction_parse (MXFFraction * fraction, const guint8 * data, guint size)
598 {
599   g_return_val_if_fail (fraction != NULL, FALSE);
600 
601   memset (fraction, 0, sizeof (MXFFraction));
602 
603   if (size < 8)
604     return FALSE;
605 
606   g_return_val_if_fail (data != NULL, FALSE);
607 
608   fraction->n = GST_READ_UINT32_BE (data);
609   fraction->d = GST_READ_UINT32_BE (data + 4);
610 
611   return TRUE;
612 }
613 
614 gdouble
mxf_fraction_to_double(const MXFFraction * fraction)615 mxf_fraction_to_double (const MXFFraction * fraction)
616 {
617   return ((gdouble) fraction->n) / ((gdouble) fraction->d);
618 }
619 
620 gchar *
mxf_utf16_to_utf8(const guint8 * data,guint size)621 mxf_utf16_to_utf8 (const guint8 * data, guint size)
622 {
623   gchar *ret;
624   GError *error = NULL;
625 
626   ret =
627       g_convert ((const gchar *) data, size, "UTF-8", "UTF-16BE", NULL, NULL,
628       &error);
629 
630   if (ret == NULL) {
631     GST_WARNING ("UTF-16-BE to UTF-8 conversion failed: %s", error->message);
632     g_error_free (error);
633     return NULL;
634   }
635 
636   return ret;
637 }
638 
639 guint8 *
mxf_utf8_to_utf16(const gchar * str,guint16 * size)640 mxf_utf8_to_utf16 (const gchar * str, guint16 * size)
641 {
642   guint8 *ret;
643   GError *error = NULL;
644   gsize s;
645 
646   g_return_val_if_fail (size != NULL, NULL);
647 
648   if (str == NULL) {
649     *size = 0;
650     return NULL;
651   }
652 
653   ret = (guint8 *)
654       g_convert_with_fallback (str, -1, "UTF-16BE", "UTF-8", (char *) "*", NULL,
655       &s, &error);
656 
657   if (ret == NULL) {
658     GST_WARNING ("UTF-16-BE to UTF-8 conversion failed: %s", error->message);
659     g_error_free (error);
660     *size = 0;
661     return NULL;
662   }
663 
664   *size = s;
665   return (guint8 *) ret;
666 }
667 
668 gboolean
mxf_product_version_parse(MXFProductVersion * product_version,const guint8 * data,guint size)669 mxf_product_version_parse (MXFProductVersion * product_version,
670     const guint8 * data, guint size)
671 {
672   g_return_val_if_fail (product_version != NULL, FALSE);
673 
674   memset (product_version, 0, sizeof (MXFProductVersion));
675 
676   if (size < 9)
677     return FALSE;
678 
679   g_return_val_if_fail (data != NULL, FALSE);
680 
681   product_version->major = GST_READ_UINT16_BE (data);
682   product_version->minor = GST_READ_UINT16_BE (data + 2);
683   product_version->patch = GST_READ_UINT16_BE (data + 4);
684   product_version->build = GST_READ_UINT16_BE (data + 6);
685 
686   /* Avid writes a 9 byte product version */
687   if (size == 9)
688     product_version->release = GST_READ_UINT8 (data + 8);
689   else
690     product_version->release = GST_READ_UINT16_BE (data + 8);
691 
692   return TRUE;
693 }
694 
695 gboolean
mxf_product_version_is_valid(const MXFProductVersion * version)696 mxf_product_version_is_valid (const MXFProductVersion * version)
697 {
698   static const guint8 null[sizeof (MXFProductVersion)] = { 0, };
699 
700   return (memcmp (version, &null, sizeof (MXFProductVersion)) == 0);
701 }
702 
703 void
mxf_product_version_write(const MXFProductVersion * version,guint8 * data)704 mxf_product_version_write (const MXFProductVersion * version, guint8 * data)
705 {
706   GST_WRITE_UINT16_BE (data, version->major);
707   GST_WRITE_UINT16_BE (data + 2, version->minor);
708   GST_WRITE_UINT16_BE (data + 4, version->patch);
709   GST_WRITE_UINT16_BE (data + 6, version->build);
710   GST_WRITE_UINT16_BE (data + 8, version->release);
711 }
712 
713 void
mxf_op_set_atom(MXFUL * ul,gboolean single_sourceclip,gboolean single_essence_track)714 mxf_op_set_atom (MXFUL * ul, gboolean single_sourceclip,
715     gboolean single_essence_track)
716 {
717   memcpy (&ul->u, MXF_UL (OPERATIONAL_PATTERN_IDENTIFICATION), 12);
718   ul->u[12] = 0x10;
719   ul->u[13] = 0;
720 
721   if (!single_sourceclip)
722     ul->u[13] |= 0x80;
723 
724   if (!single_essence_track)
725     ul->u[13] |= 0x40;
726 
727   ul->u[14] = 0;
728   ul->u[15] = 0;
729 }
730 
731 void
mxf_op_set_generalized(MXFUL * ul,MXFOperationalPattern pattern,gboolean internal_essence,gboolean streamable,gboolean single_track)732 mxf_op_set_generalized (MXFUL * ul, MXFOperationalPattern pattern,
733     gboolean internal_essence, gboolean streamable, gboolean single_track)
734 {
735   g_return_if_fail (pattern >= MXF_OP_1a);
736 
737   memcpy (&ul->u, MXF_UL (OPERATIONAL_PATTERN_IDENTIFICATION), 12);
738 
739   if (pattern == MXF_OP_1a || pattern == MXF_OP_1b || pattern == MXF_OP_1c)
740     ul->u[12] = 0x01;
741   else if (pattern == MXF_OP_2a || pattern == MXF_OP_2b || pattern == MXF_OP_2c)
742     ul->u[12] = 0x02;
743   else if (pattern == MXF_OP_3a || pattern == MXF_OP_3b || pattern == MXF_OP_3c)
744     ul->u[12] = 0x03;
745 
746   if (pattern == MXF_OP_1a || pattern == MXF_OP_2a || pattern == MXF_OP_3a)
747     ul->u[13] = 0x01;
748   else if (pattern == MXF_OP_1b || pattern == MXF_OP_2b || pattern == MXF_OP_3b)
749     ul->u[13] = 0x02;
750   else if (pattern == MXF_OP_1c || pattern == MXF_OP_2c || pattern == MXF_OP_3c)
751     ul->u[13] = 0x02;
752 
753   ul->u[14] = 0x08;
754   if (!internal_essence)
755     ul->u[14] |= 0x04;
756   if (!streamable)
757     ul->u[14] |= 0x02;
758   if (!single_track)
759     ul->u[14] |= 0x01;
760 
761   ul->u[15] = 0;
762 }
763 
764 /* SMPTE 377M 6.1, Table 2 */
765 gboolean
mxf_partition_pack_parse(const MXFUL * ul,MXFPartitionPack * pack,const guint8 * data,guint size)766 mxf_partition_pack_parse (const MXFUL * ul, MXFPartitionPack * pack,
767     const guint8 * data, guint size)
768 {
769 #ifndef GST_DISABLE_GST_DEBUG
770   guint i;
771   gchar str[48];
772 #endif
773 
774   if (size < 84)
775     return FALSE;
776 
777   g_return_val_if_fail (data != NULL, FALSE);
778 
779   memset (pack, 0, sizeof (MXFPartitionPack));
780 
781   GST_DEBUG ("Parsing partition pack:");
782 
783   if (ul->u[13] == 0x02)
784     pack->type = MXF_PARTITION_PACK_HEADER;
785   else if (ul->u[13] == 0x03)
786     pack->type = MXF_PARTITION_PACK_BODY;
787   else if (ul->u[13] == 0x04)
788     pack->type = MXF_PARTITION_PACK_FOOTER;
789 
790   GST_DEBUG ("  type = %s",
791       (pack->type == MXF_PARTITION_PACK_HEADER) ? "header" : (pack->type ==
792           MXF_PARTITION_PACK_BODY) ? "body" : "footer");
793 
794   pack->closed = (ul->u[14] == 0x02 || ul->u[14] == 0x04);
795   pack->complete = (ul->u[14] == 0x03 || ul->u[14] == 0x04);
796 
797   GST_DEBUG ("  closed = %s, complete = %s", (pack->closed) ? "yes" : "no",
798       (pack->complete) ? "yes" : "no");
799 
800   pack->major_version = GST_READ_UINT16_BE (data);
801   if (pack->major_version != 1)
802     goto error;
803   data += 2;
804   size -= 2;
805 
806   pack->minor_version = GST_READ_UINT16_BE (data);
807   data += 2;
808   size -= 2;
809 
810   GST_DEBUG ("  MXF version = %u.%u", pack->major_version, pack->minor_version);
811 
812   pack->kag_size = GST_READ_UINT32_BE (data);
813   data += 4;
814   size -= 4;
815 
816   GST_DEBUG ("  KAG size = %u", pack->kag_size);
817 
818   pack->this_partition = GST_READ_UINT64_BE (data);
819   data += 8;
820   size -= 8;
821 
822   GST_DEBUG ("  this partition offset = %" G_GUINT64_FORMAT,
823       pack->this_partition);
824 
825   pack->prev_partition = GST_READ_UINT64_BE (data);
826   data += 8;
827   size -= 8;
828 
829   GST_DEBUG ("  previous partition offset = %" G_GUINT64_FORMAT,
830       pack->prev_partition);
831 
832   pack->footer_partition = GST_READ_UINT64_BE (data);
833   data += 8;
834   size -= 8;
835 
836   GST_DEBUG ("  footer partition offset = %" G_GUINT64_FORMAT,
837       pack->footer_partition);
838 
839   pack->header_byte_count = GST_READ_UINT64_BE (data);
840   data += 8;
841   size -= 8;
842 
843   GST_DEBUG ("  header byte count = %" G_GUINT64_FORMAT,
844       pack->header_byte_count);
845 
846   pack->index_byte_count = GST_READ_UINT64_BE (data);
847   data += 8;
848   size -= 8;
849 
850   pack->index_sid = GST_READ_UINT32_BE (data);
851   data += 4;
852   size -= 4;
853 
854   GST_DEBUG ("  index sid = %u, size = %" G_GUINT64_FORMAT, pack->index_sid,
855       pack->index_byte_count);
856 
857   pack->body_offset = GST_READ_UINT64_BE (data);
858   data += 8;
859   size -= 8;
860 
861   pack->body_sid = GST_READ_UINT32_BE (data);
862   data += 4;
863   size -= 4;
864 
865   GST_DEBUG ("  body sid = %u, offset = %" G_GUINT64_FORMAT, pack->body_sid,
866       pack->body_offset);
867 
868   memcpy (&pack->operational_pattern, data, 16);
869   data += 16;
870   size -= 16;
871 
872   GST_DEBUG ("  operational pattern = %s",
873       mxf_ul_to_string (&pack->operational_pattern, str));
874 
875   if (!mxf_ul_array_parse (&pack->essence_containers,
876           &pack->n_essence_containers, data, size))
877     goto error;
878 
879 #ifndef GST_DISABLE_GST_DEBUG
880   GST_DEBUG ("  number of essence containers = %u", pack->n_essence_containers);
881   if (pack->n_essence_containers) {
882     for (i = 0; i < pack->n_essence_containers; i++) {
883       GST_DEBUG ("  essence container %u = %s", i,
884           mxf_ul_to_string (&pack->essence_containers[i], str));
885     }
886   }
887 #endif
888 
889   return TRUE;
890 
891 error:
892   GST_ERROR ("Invalid partition pack");
893 
894   mxf_partition_pack_reset (pack);
895   return FALSE;
896 }
897 
898 void
mxf_partition_pack_reset(MXFPartitionPack * pack)899 mxf_partition_pack_reset (MXFPartitionPack * pack)
900 {
901   g_return_if_fail (pack != NULL);
902 
903   g_free (pack->essence_containers);
904 
905   memset (pack, 0, sizeof (MXFPartitionPack));
906 }
907 
908 GstBuffer *
mxf_partition_pack_to_buffer(const MXFPartitionPack * pack)909 mxf_partition_pack_to_buffer (const MXFPartitionPack * pack)
910 {
911   guint slen;
912   guint8 ber[9];
913   GstBuffer *ret;
914   GstMapInfo map;
915   guint8 *data;
916   guint i;
917   guint size =
918       8 + 16 * pack->n_essence_containers + 16 + 4 + 8 + 4 + 8 + 8 + 8 + 8 + 8 +
919       4 + 2 + 2;
920 
921   slen = mxf_ber_encode_size (size, ber);
922 
923   ret = gst_buffer_new_and_alloc (16 + slen + size);
924   gst_buffer_map (ret, &map, GST_MAP_WRITE);
925 
926   memcpy (map.data, MXF_UL (PARTITION_PACK), 13);
927   if (pack->type == MXF_PARTITION_PACK_HEADER)
928     map.data[13] = 0x02;
929   else if (pack->type == MXF_PARTITION_PACK_BODY)
930     map.data[13] = 0x03;
931   else if (pack->type == MXF_PARTITION_PACK_FOOTER)
932     map.data[13] = 0x04;
933   map.data[14] = 0;
934   if (pack->complete)
935     map.data[14] |= 0x02;
936   if (pack->closed)
937     map.data[14] |= 0x01;
938   map.data[14] += 1;
939   map.data[15] = 0;
940   memcpy (map.data + 16, &ber, slen);
941 
942   data = map.data + 16 + slen;
943 
944   GST_WRITE_UINT16_BE (data, pack->major_version);
945   GST_WRITE_UINT16_BE (data + 2, pack->minor_version);
946   data += 4;
947 
948   GST_WRITE_UINT32_BE (data, pack->kag_size);
949   data += 4;
950 
951   GST_WRITE_UINT64_BE (data, pack->this_partition);
952   data += 8;
953 
954   GST_WRITE_UINT64_BE (data, pack->prev_partition);
955   data += 8;
956 
957   GST_WRITE_UINT64_BE (data, pack->footer_partition);
958   data += 8;
959 
960   GST_WRITE_UINT64_BE (data, pack->header_byte_count);
961   data += 8;
962 
963   GST_WRITE_UINT64_BE (data, pack->index_byte_count);
964   data += 8;
965 
966   GST_WRITE_UINT32_BE (data, pack->index_sid);
967   data += 4;
968 
969   GST_WRITE_UINT64_BE (data, pack->body_offset);
970   data += 8;
971 
972   GST_WRITE_UINT32_BE (data, pack->body_sid);
973   data += 4;
974 
975   memcpy (data, &pack->operational_pattern, 16);
976   data += 16;
977 
978   GST_WRITE_UINT32_BE (data, pack->n_essence_containers);
979   GST_WRITE_UINT32_BE (data + 4, 16);
980   data += 8;
981 
982   for (i = 0; i < pack->n_essence_containers; i++)
983     memcpy (data + 16 * i, &pack->essence_containers[i], 16);
984 
985   gst_buffer_unmap (ret, &map);
986 
987   return ret;
988 }
989 
990 /* SMPTE 377M 11.1 */
991 gboolean
mxf_random_index_pack_parse(const MXFUL * ul,const guint8 * data,guint size,GArray ** array)992 mxf_random_index_pack_parse (const MXFUL * ul, const guint8 * data, guint size,
993     GArray ** array)
994 {
995   guint len, i;
996   MXFRandomIndexPackEntry entry;
997 
998   g_return_val_if_fail (array != NULL, FALSE);
999 
1000   if (size < 4)
1001     return FALSE;
1002 
1003   g_return_val_if_fail (data != NULL, FALSE);
1004 
1005   if ((size - 4) % 12 != 0)
1006     return FALSE;
1007 
1008   GST_DEBUG ("Parsing random index pack:");
1009 
1010   len = (size - 4) / 12;
1011 
1012   GST_DEBUG ("  number of entries = %u", len);
1013 
1014   *array =
1015       g_array_sized_new (FALSE, FALSE, sizeof (MXFRandomIndexPackEntry), len);
1016 
1017   for (i = 0; i < len; i++) {
1018     entry.body_sid = GST_READ_UINT32_BE (data);
1019     entry.offset = GST_READ_UINT64_BE (data + 4);
1020     data += 12;
1021 
1022     GST_DEBUG ("  entry %u = body sid %u at offset %" G_GUINT64_FORMAT, i,
1023         entry.body_sid, entry.offset);
1024 
1025     g_array_append_val (*array, entry);
1026   }
1027 
1028   return TRUE;
1029 }
1030 
1031 GstBuffer *
mxf_random_index_pack_to_buffer(const GArray * array)1032 mxf_random_index_pack_to_buffer (const GArray * array)
1033 {
1034   MXFRandomIndexPackEntry *entry;
1035   guint i;
1036   GstBuffer *ret;
1037   GstMapInfo map;
1038   guint8 slen, ber[9];
1039   guint size;
1040   guint8 *data;
1041 
1042   if (array->len == 0)
1043     return NULL;
1044 
1045   size = array->len * 12 + 4;
1046   slen = mxf_ber_encode_size (size, ber);
1047   ret = gst_buffer_new_and_alloc (16 + slen + size);
1048   gst_buffer_map (ret, &map, GST_MAP_WRITE);
1049 
1050   memcpy (map.data, MXF_UL (RANDOM_INDEX_PACK), 16);
1051   memcpy (map.data + 16, ber, slen);
1052 
1053   data = map.data + 16 + slen;
1054 
1055   for (i = 0; i < array->len; i++) {
1056     entry = &g_array_index (array, MXFRandomIndexPackEntry, i);
1057     GST_WRITE_UINT32_BE (data, entry->body_sid);
1058     GST_WRITE_UINT64_BE (data + 4, entry->offset);
1059     data += 12;
1060   }
1061   GST_WRITE_UINT32_BE (data, gst_buffer_get_size (ret));
1062 
1063   gst_buffer_unmap (ret, &map);
1064 
1065   return ret;
1066 }
1067 
1068 /* SMPTE 377M 10.2.3 */
1069 gboolean
mxf_index_table_segment_parse(const MXFUL * ul,MXFIndexTableSegment * segment,const guint8 * data,guint size)1070 mxf_index_table_segment_parse (const MXFUL * ul,
1071     MXFIndexTableSegment * segment, const guint8 * data, guint size)
1072 {
1073 #ifndef GST_DISABLE_GST_DEBUG
1074   gchar str[48];
1075 #endif
1076   guint16 tag, tag_size;
1077   const guint8 *tag_data;
1078 
1079   g_return_val_if_fail (ul != NULL, FALSE);
1080 
1081   memset (segment, 0, sizeof (MXFIndexTableSegment));
1082 
1083   if (size < 70)
1084     return FALSE;
1085 
1086   g_return_val_if_fail (data != NULL, FALSE);
1087 
1088   GST_DEBUG ("Parsing index table segment:");
1089 
1090   while (mxf_local_tag_parse (data, size, &tag, &tag_size, &tag_data)) {
1091     data += 4 + tag_size;
1092     size -= 4 + tag_size;
1093 
1094     if (tag_size == 0 || tag == 0x0000)
1095       continue;
1096 
1097     switch (tag) {
1098       case 0x3c0a:
1099         if (tag_size != 16)
1100           goto error;
1101         memcpy (&segment->instance_id, tag_data, 16);
1102         GST_DEBUG ("  instance id = %s",
1103             mxf_uuid_to_string (&segment->instance_id, str));
1104         break;
1105       case 0x3f0b:
1106         if (!mxf_fraction_parse (&segment->index_edit_rate, tag_data, tag_size))
1107           goto error;
1108         GST_DEBUG ("  index edit rate = %d/%d", segment->index_edit_rate.n,
1109             segment->index_edit_rate.d);
1110         break;
1111       case 0x3f0c:
1112         if (tag_size != 8)
1113           goto error;
1114         segment->index_start_position = GST_READ_UINT64_BE (tag_data);
1115         GST_DEBUG ("  index start position = %" G_GINT64_FORMAT,
1116             segment->index_start_position);
1117         break;
1118       case 0x3f0d:
1119         if (tag_size != 8)
1120           goto error;
1121         segment->index_duration = GST_READ_UINT64_BE (tag_data);
1122         GST_DEBUG ("  index duration = %" G_GINT64_FORMAT,
1123             segment->index_duration);
1124         break;
1125       case 0x3f05:
1126         if (tag_size != 4)
1127           goto error;
1128         segment->edit_unit_byte_count = GST_READ_UINT32_BE (tag_data);
1129         GST_DEBUG ("  edit unit byte count = %u",
1130             segment->edit_unit_byte_count);
1131         break;
1132       case 0x3f06:
1133         if (tag_size != 4)
1134           goto error;
1135         segment->index_sid = GST_READ_UINT32_BE (tag_data);
1136         GST_DEBUG ("  index sid = %u", segment->index_sid);
1137         break;
1138       case 0x3f07:
1139         if (tag_size != 4)
1140           goto error;
1141         segment->body_sid = GST_READ_UINT32_BE (tag_data);
1142         GST_DEBUG ("  body sid = %u", segment->body_sid);
1143         break;
1144       case 0x3f08:
1145         if (tag_size != 1)
1146           goto error;
1147         segment->slice_count = GST_READ_UINT8 (tag_data);
1148         GST_DEBUG ("  slice count = %u", segment->slice_count);
1149         break;
1150       case 0x3f0e:
1151         if (tag_size != 1)
1152           goto error;
1153         segment->pos_table_count = GST_READ_UINT8 (tag_data);
1154         GST_DEBUG ("  pos table count = %u", segment->pos_table_count);
1155         break;
1156       case 0x3f09:{
1157         guint len, i;
1158 
1159         if (tag_size < 8)
1160           goto error;
1161 
1162         len = GST_READ_UINT32_BE (tag_data);
1163         segment->n_delta_entries = len;
1164         GST_DEBUG ("  number of delta entries = %u", segment->n_delta_entries);
1165         if (len == 0)
1166           continue;
1167         tag_data += 4;
1168         tag_size -= 4;
1169 
1170         if (GST_READ_UINT32_BE (tag_data) != 6)
1171           goto error;
1172 
1173         tag_data += 4;
1174         tag_size -= 4;
1175 
1176         if (tag_size / 6 < len)
1177           goto error;
1178 
1179         segment->delta_entries = g_new (MXFDeltaEntry, len);
1180 
1181         for (i = 0; i < len; i++) {
1182           GST_DEBUG ("    delta entry %u:", i);
1183 
1184           segment->delta_entries[i].pos_table_index = GST_READ_UINT8 (tag_data);
1185           tag_data += 1;
1186           tag_size -= 1;
1187           GST_DEBUG ("    pos table index = %d",
1188               segment->delta_entries[i].pos_table_index);
1189 
1190           segment->delta_entries[i].slice = GST_READ_UINT8 (tag_data);
1191           tag_data += 1;
1192           tag_size -= 1;
1193           GST_DEBUG ("    slice = %u", segment->delta_entries[i].slice);
1194 
1195           segment->delta_entries[i].element_delta =
1196               GST_READ_UINT32_BE (tag_data);
1197           tag_data += 4;
1198           tag_size -= 4;
1199           GST_DEBUG ("    element delta = %u",
1200               segment->delta_entries[i].element_delta);
1201         }
1202         break;
1203       }
1204       case 0x3f0a:{
1205         guint len, i, j;
1206 
1207         if (tag_size < 8)
1208           goto error;
1209 
1210         len = GST_READ_UINT32_BE (tag_data);
1211         segment->n_index_entries = len;
1212         GST_DEBUG ("  number of index entries = %u", segment->n_index_entries);
1213         if (len == 0)
1214           continue;
1215         tag_data += 4;
1216         tag_size -= 4;
1217 
1218         if (GST_READ_UINT32_BE (tag_data) !=
1219             (11 + 4 * segment->slice_count + 8 * segment->pos_table_count))
1220           goto error;
1221 
1222         tag_data += 4;
1223         tag_size -= 4;
1224 
1225         if (tag_size / (11 + 4 * segment->slice_count +
1226                 8 * segment->pos_table_count) < len)
1227           goto error;
1228 
1229         segment->index_entries = g_new0 (MXFIndexEntry, len);
1230 
1231         for (i = 0; i < len; i++) {
1232           MXFIndexEntry *entry = &segment->index_entries[i];
1233 
1234           GST_DEBUG ("    index entry %u:", i);
1235 
1236           entry->temporal_offset = GST_READ_UINT8 (tag_data);
1237           tag_data += 1;
1238           tag_size -= 1;
1239           GST_DEBUG ("    temporal offset = %d", entry->temporal_offset);
1240 
1241           entry->key_frame_offset = GST_READ_UINT8 (tag_data);
1242           tag_data += 1;
1243           tag_size -= 1;
1244           GST_DEBUG ("    keyframe offset = %d", entry->key_frame_offset);
1245 
1246           entry->flags = GST_READ_UINT8 (tag_data);
1247           tag_data += 1;
1248           tag_size -= 1;
1249           GST_DEBUG ("    flags = 0x%02x", entry->flags);
1250 
1251           entry->stream_offset = GST_READ_UINT64_BE (tag_data);
1252           tag_data += 8;
1253           tag_size -= 8;
1254           GST_DEBUG ("    stream offset = %" G_GUINT64_FORMAT,
1255               entry->stream_offset);
1256 
1257           entry->slice_offset = g_new0 (guint32, segment->slice_count);
1258           for (j = 0; j < segment->slice_count; j++) {
1259             entry->slice_offset[j] = GST_READ_UINT32_BE (tag_data);
1260             tag_data += 4;
1261             tag_size -= 4;
1262             GST_DEBUG ("    slice %u offset = %u", j, entry->slice_offset[j]);
1263           }
1264 
1265           entry->pos_table = g_new0 (MXFFraction, segment->pos_table_count);
1266           for (j = 0; j < segment->pos_table_count; j++) {
1267             if (!mxf_fraction_parse (&entry->pos_table[j], tag_data, tag_size))
1268               goto error;
1269             tag_data += 8;
1270             tag_size -= 8;
1271             GST_DEBUG ("    pos table %u = %d/%d", j, entry->pos_table[j].n,
1272                 entry->pos_table[j].d);
1273           }
1274         }
1275         break;
1276       }
1277       default:
1278         GST_WARNING
1279             ("Unknown local tag 0x%04x of size %d in index table segment", tag,
1280             tag_size);
1281         break;
1282     }
1283   }
1284   return TRUE;
1285 
1286 error:
1287   GST_ERROR ("Invalid index table segment");
1288   mxf_index_table_segment_reset (segment);
1289   return FALSE;
1290 }
1291 
1292 void
mxf_index_table_segment_reset(MXFIndexTableSegment * segment)1293 mxf_index_table_segment_reset (MXFIndexTableSegment * segment)
1294 {
1295   guint i;
1296 
1297   g_return_if_fail (segment != NULL);
1298 
1299   if (segment->index_entries) {
1300     for (i = 0; i < segment->n_index_entries; i++) {
1301       g_free (segment->index_entries[i].slice_offset);
1302       g_free (segment->index_entries[i].pos_table);
1303     }
1304   }
1305 
1306   g_free (segment->index_entries);
1307   g_free (segment->delta_entries);
1308 
1309   memset (segment, 0, sizeof (MXFIndexTableSegment));
1310 }
1311 
1312 GstBuffer *
mxf_index_table_segment_to_buffer(const MXFIndexTableSegment * segment)1313 mxf_index_table_segment_to_buffer (const MXFIndexTableSegment * segment)
1314 {
1315   guint len, slen, i;
1316   guint8 ber[9];
1317   GstBuffer *ret;
1318   GstMapInfo map;
1319   GstByteWriter bw;
1320 
1321   g_return_val_if_fail (segment != NULL, NULL);
1322   g_return_val_if_fail (segment->n_delta_entries * 6 < G_MAXUINT16, NULL);
1323   g_return_val_if_fail (segment->n_index_entries * (11 +
1324           4 * segment->slice_count + 8 * segment->pos_table_count) <
1325       G_MAXUINT16, NULL);
1326 
1327   len =
1328       16 + 4 + 8 + 4 + 8 + 4 + 8 + 4 + 4 + 4 + 4 + 4 + 4 + 4 + 1 + 4 + 1 + 4 +
1329       4 + 8 + segment->n_delta_entries * 6 + 4 + 8 +
1330       segment->n_index_entries * (11 + 4 * segment->slice_count +
1331       8 * segment->pos_table_count);
1332   slen = mxf_ber_encode_size (len, ber);
1333 
1334   ret = gst_buffer_new_and_alloc (16 + slen + len);
1335   gst_buffer_map (ret, &map, GST_MAP_WRITE);
1336 
1337   gst_byte_writer_init_with_data (&bw, map.data, map.size, FALSE);
1338 
1339   gst_byte_writer_put_data_unchecked (&bw,
1340       (const guint8 *) MXF_UL (INDEX_TABLE_SEGMENT), 16);
1341   gst_byte_writer_put_data_unchecked (&bw, ber, slen);
1342 
1343   gst_byte_writer_put_uint16_be_unchecked (&bw, 0x3c0a);
1344   gst_byte_writer_put_uint16_be_unchecked (&bw, 16);
1345   gst_byte_writer_put_data_unchecked (&bw,
1346       (const guint8 *) &segment->instance_id, 16);
1347 
1348   gst_byte_writer_put_uint16_be_unchecked (&bw, 0x3f0b);
1349   gst_byte_writer_put_uint16_be_unchecked (&bw, 8);
1350   gst_byte_writer_put_uint32_be_unchecked (&bw, segment->index_edit_rate.n);
1351   gst_byte_writer_put_uint32_be_unchecked (&bw, segment->index_edit_rate.d);
1352 
1353   gst_byte_writer_put_uint16_be_unchecked (&bw, 0x3f0c);
1354   gst_byte_writer_put_uint16_be_unchecked (&bw, 8);
1355   gst_byte_writer_put_uint64_be_unchecked (&bw, segment->index_start_position);
1356 
1357   gst_byte_writer_put_uint16_be_unchecked (&bw, 0x3f0d);
1358   gst_byte_writer_put_uint16_be_unchecked (&bw, 8);
1359   gst_byte_writer_put_uint64_be_unchecked (&bw, segment->index_duration);
1360 
1361   gst_byte_writer_put_uint16_be_unchecked (&bw, 0x3f05);
1362   gst_byte_writer_put_uint16_be_unchecked (&bw, 4);
1363   gst_byte_writer_put_uint32_be_unchecked (&bw, segment->edit_unit_byte_count);
1364 
1365   gst_byte_writer_put_uint16_be_unchecked (&bw, 0x3f06);
1366   gst_byte_writer_put_uint16_be_unchecked (&bw, 4);
1367   gst_byte_writer_put_uint32_be_unchecked (&bw, segment->index_sid);
1368 
1369   gst_byte_writer_put_uint16_be_unchecked (&bw, 0x3f07);
1370   gst_byte_writer_put_uint16_be_unchecked (&bw, 4);
1371   gst_byte_writer_put_uint32_be_unchecked (&bw, segment->body_sid);
1372 
1373   gst_byte_writer_put_uint16_be_unchecked (&bw, 0x3f08);
1374   gst_byte_writer_put_uint16_be_unchecked (&bw, 1);
1375   gst_byte_writer_put_uint8 (&bw, segment->slice_count);
1376 
1377   gst_byte_writer_put_uint16_be_unchecked (&bw, 0x3f0e);
1378   gst_byte_writer_put_uint16_be_unchecked (&bw, 1);
1379   gst_byte_writer_put_uint8 (&bw, segment->pos_table_count);
1380 
1381   gst_byte_writer_put_uint16_be_unchecked (&bw, 0x3f09);
1382   gst_byte_writer_put_uint16_be_unchecked (&bw,
1383       8 + segment->n_delta_entries * 6);
1384   gst_byte_writer_put_uint32_be_unchecked (&bw, segment->n_delta_entries);
1385   gst_byte_writer_put_uint32_be_unchecked (&bw, 6);
1386   for (i = 0; i < segment->n_delta_entries; i++) {
1387     gst_byte_writer_put_uint8_unchecked (&bw,
1388         segment->delta_entries[i].pos_table_index);
1389     gst_byte_writer_put_uint8_unchecked (&bw, segment->delta_entries[i].slice);
1390     gst_byte_writer_put_uint32_be_unchecked (&bw,
1391         segment->delta_entries[i].element_delta);
1392   }
1393 
1394   gst_byte_writer_put_uint16_be_unchecked (&bw, 0x3f0a);
1395   gst_byte_writer_put_uint16_be_unchecked (&bw,
1396       8 + segment->n_index_entries * (11 + 4 * segment->slice_count +
1397           8 * segment->pos_table_count));
1398   gst_byte_writer_put_uint32_be_unchecked (&bw, segment->n_index_entries);
1399   gst_byte_writer_put_uint32_be_unchecked (&bw,
1400       (11 + 4 * segment->slice_count + 8 * segment->pos_table_count));
1401   for (i = 0; i < segment->n_index_entries; i++) {
1402     guint j;
1403 
1404     gst_byte_writer_put_uint8_unchecked (&bw,
1405         segment->index_entries[i].temporal_offset);
1406     gst_byte_writer_put_uint8_unchecked (&bw,
1407         segment->index_entries[i].key_frame_offset);
1408     gst_byte_writer_put_uint8_unchecked (&bw, segment->index_entries[i].flags);
1409     gst_byte_writer_put_uint64_be_unchecked (&bw,
1410         segment->index_entries[i].stream_offset);
1411 
1412     for (j = 0; j < segment->slice_count; j++)
1413       gst_byte_writer_put_uint32_be_unchecked (&bw,
1414           segment->index_entries[i].slice_offset[j]);
1415 
1416     for (j = 0; j < segment->pos_table_count; j++) {
1417       gst_byte_writer_put_uint32_be_unchecked (&bw,
1418           segment->index_entries[i].pos_table[j].n);
1419       gst_byte_writer_put_uint32_be_unchecked (&bw,
1420           segment->index_entries[i].pos_table[j].d);
1421     }
1422   }
1423 
1424   g_assert (gst_byte_writer_get_pos (&bw) == map.size);
1425 
1426   gst_buffer_unmap (ret, &map);
1427 
1428   return ret;
1429 }
1430 
1431 /* SMPTE 377M 8.2 Table 1 and 2 */
1432 
1433 static void
_mxf_mapping_ul_free(MXFUL * ul)1434 _mxf_mapping_ul_free (MXFUL * ul)
1435 {
1436   g_slice_free (MXFUL, ul);
1437 }
1438 
1439 gboolean
mxf_primer_pack_parse(const MXFUL * ul,MXFPrimerPack * pack,const guint8 * data,guint size)1440 mxf_primer_pack_parse (const MXFUL * ul, MXFPrimerPack * pack,
1441     const guint8 * data, guint size)
1442 {
1443   guint i;
1444   guint32 n;
1445 
1446   if (size < 8)
1447     return FALSE;
1448 
1449   g_return_val_if_fail (data != NULL, FALSE);
1450 
1451   memset (pack, 0, sizeof (MXFPrimerPack));
1452 
1453   GST_DEBUG ("Parsing primer pack:");
1454 
1455   pack->mappings =
1456       g_hash_table_new_full (g_direct_hash, g_direct_equal,
1457       (GDestroyNotify) NULL, (GDestroyNotify) _mxf_mapping_ul_free);
1458 
1459   n = GST_READ_UINT32_BE (data);
1460   data += 4;
1461   size -= 4;
1462 
1463   GST_DEBUG ("  number of mappings = %u", n);
1464 
1465   if (GST_READ_UINT32_BE (data) != 18)
1466     goto error;
1467   data += 4;
1468   size -= 4;
1469 
1470   if (size / 18 < n)
1471     goto error;
1472 
1473   for (i = 0; i < n; i++) {
1474     guint local_tag;
1475 #ifndef GST_DISABLE_GST_DEBUG
1476     gchar str[48];
1477 #endif
1478     MXFUL *uid;
1479 
1480     local_tag = GST_READ_UINT16_BE (data);
1481     data += 2;
1482 
1483     if (g_hash_table_lookup (pack->mappings, GUINT_TO_POINTER (local_tag)))
1484       continue;
1485 
1486     uid = g_slice_new (MXFUL);
1487     memcpy (uid, data, 16);
1488     data += 16;
1489 
1490     g_hash_table_insert (pack->mappings, GUINT_TO_POINTER (local_tag), uid);
1491     GST_DEBUG ("  Adding mapping = 0x%04x -> %s", local_tag,
1492         mxf_ul_to_string (uid, str));
1493   }
1494 
1495   return TRUE;
1496 
1497 error:
1498   GST_DEBUG ("Invalid primer pack");
1499   mxf_primer_pack_reset (pack);
1500   return FALSE;
1501 }
1502 
1503 void
mxf_primer_pack_reset(MXFPrimerPack * pack)1504 mxf_primer_pack_reset (MXFPrimerPack * pack)
1505 {
1506   g_return_if_fail (pack != NULL);
1507 
1508   if (pack->mappings)
1509     g_hash_table_destroy (pack->mappings);
1510   if (pack->reverse_mappings)
1511     g_hash_table_destroy (pack->reverse_mappings);
1512 
1513   memset (pack, 0, sizeof (MXFPrimerPack));
1514 
1515   pack->next_free_tag = 0x8000;
1516 }
1517 
1518 guint16
mxf_primer_pack_add_mapping(MXFPrimerPack * primer,guint16 local_tag,const MXFUL * ul)1519 mxf_primer_pack_add_mapping (MXFPrimerPack * primer, guint16 local_tag,
1520     const MXFUL * ul)
1521 {
1522   MXFUL *uid;
1523 #ifndef GST_DISABLE_GST_DEBUG
1524   gchar str[48];
1525 #endif
1526   guint ltag_tmp = local_tag;
1527 
1528   if (primer->mappings == NULL) {
1529     primer->mappings = g_hash_table_new_full (g_direct_hash, g_direct_equal,
1530         (GDestroyNotify) NULL, (GDestroyNotify) _mxf_mapping_ul_free);
1531   }
1532 
1533   if (primer->reverse_mappings == NULL) {
1534     primer->reverse_mappings = g_hash_table_new_full ((GHashFunc) mxf_ul_hash,
1535         (GEqualFunc) mxf_ul_is_equal, (GDestroyNotify) _mxf_mapping_ul_free,
1536         (GDestroyNotify) NULL);
1537   }
1538 
1539   if (primer->next_free_tag == 0xffff && ltag_tmp == 0) {
1540     GST_ERROR ("Used too many dynamic tags");
1541     return 0;
1542   }
1543 
1544   if (ltag_tmp == 0) {
1545     guint tmp;
1546 
1547     tmp = GPOINTER_TO_UINT (g_hash_table_lookup (primer->reverse_mappings, ul));
1548     if (tmp == 0) {
1549       ltag_tmp = primer->next_free_tag;
1550       primer->next_free_tag++;
1551     }
1552   } else {
1553     if (g_hash_table_lookup (primer->mappings, GUINT_TO_POINTER (ltag_tmp)))
1554       return ltag_tmp;
1555   }
1556 
1557   g_assert (ltag_tmp != 0);
1558 
1559   uid = g_slice_new (MXFUL);
1560   memcpy (uid, ul, 16);
1561 
1562   GST_DEBUG ("Adding mapping = 0x%04x -> %s", ltag_tmp,
1563       mxf_ul_to_string (uid, str));
1564   g_hash_table_insert (primer->mappings, GUINT_TO_POINTER (ltag_tmp), uid);
1565   uid = g_slice_dup (MXFUL, uid);
1566   g_hash_table_insert (primer->reverse_mappings, uid,
1567       GUINT_TO_POINTER (ltag_tmp));
1568 
1569   return ltag_tmp;
1570 }
1571 
1572 GstBuffer *
mxf_primer_pack_to_buffer(const MXFPrimerPack * pack)1573 mxf_primer_pack_to_buffer (const MXFPrimerPack * pack)
1574 {
1575   guint slen;
1576   guint8 ber[9];
1577   GstBuffer *ret;
1578   GstMapInfo map;
1579   guint n;
1580   guint8 *data;
1581 
1582   if (pack->mappings)
1583     n = g_hash_table_size (pack->mappings);
1584   else
1585     n = 0;
1586 
1587   slen = mxf_ber_encode_size (8 + 18 * n, ber);
1588 
1589   ret = gst_buffer_new_and_alloc (16 + slen + 8 + 18 * n);
1590   gst_buffer_map (ret, &map, GST_MAP_WRITE);
1591 
1592   memcpy (map.data, MXF_UL (PRIMER_PACK), 16);
1593   memcpy (map.data + 16, &ber, slen);
1594 
1595   data = map.data + 16 + slen;
1596 
1597   GST_WRITE_UINT32_BE (data, n);
1598   GST_WRITE_UINT32_BE (data + 4, 18);
1599   data += 8;
1600 
1601   if (pack->mappings) {
1602     gpointer local_tag;
1603     MXFUL *ul;
1604     GHashTableIter iter;
1605 
1606     g_hash_table_iter_init (&iter, pack->mappings);
1607 
1608     while (g_hash_table_iter_next (&iter, &local_tag, (gpointer) & ul)) {
1609       GST_WRITE_UINT16_BE (data, GPOINTER_TO_UINT (local_tag));
1610       memcpy (data + 2, ul, 16);
1611       data += 18;
1612     }
1613   }
1614 
1615   gst_buffer_unmap (ret, &map);
1616 
1617   return ret;
1618 }
1619 
1620 /* structural metadata parsing */
1621 
1622 gboolean
mxf_local_tag_parse(const guint8 * data,guint size,guint16 * tag,guint16 * tag_size,const guint8 ** tag_data)1623 mxf_local_tag_parse (const guint8 * data, guint size, guint16 * tag,
1624     guint16 * tag_size, const guint8 ** tag_data)
1625 {
1626   if (size < 4)
1627     return FALSE;
1628 
1629   g_return_val_if_fail (data != NULL, FALSE);
1630 
1631   *tag = GST_READ_UINT16_BE (data);
1632   *tag_size = GST_READ_UINT16_BE (data + 2);
1633 
1634   data += 4;
1635   size -= 4;
1636 
1637   if (size < *tag_size)
1638     return FALSE;
1639 
1640   *tag_data = data;
1641 
1642   return TRUE;
1643 }
1644 
1645 void
mxf_local_tag_free(MXFLocalTag * tag)1646 mxf_local_tag_free (MXFLocalTag * tag)
1647 {
1648   if (tag->g_slice)
1649     g_slice_free1 (tag->size, tag->data);
1650   else
1651     g_free (tag->data);
1652   g_slice_free (MXFLocalTag, tag);
1653 }
1654 
1655 gboolean
mxf_local_tag_add_to_hash_table(const MXFPrimerPack * primer,guint16 tag,const guint8 * tag_data,guint16 tag_size,GHashTable ** hash_table)1656 mxf_local_tag_add_to_hash_table (const MXFPrimerPack * primer,
1657     guint16 tag, const guint8 * tag_data, guint16 tag_size,
1658     GHashTable ** hash_table)
1659 {
1660   MXFLocalTag *local_tag;
1661   MXFUL *ul;
1662 
1663   g_return_val_if_fail (primer != NULL, FALSE);
1664   g_return_val_if_fail (tag_size == 0 || tag_data != NULL, FALSE);
1665   g_return_val_if_fail (hash_table != NULL, FALSE);
1666   g_return_val_if_fail (primer->mappings != NULL, FALSE);
1667 
1668   if (*hash_table == NULL)
1669     *hash_table =
1670         g_hash_table_new_full ((GHashFunc) mxf_ul_hash,
1671         (GEqualFunc) mxf_ul_is_equal, (GDestroyNotify) NULL,
1672         (GDestroyNotify) mxf_local_tag_free);
1673 
1674   g_return_val_if_fail (*hash_table != NULL, FALSE);
1675 
1676   ul = (MXFUL *) g_hash_table_lookup (primer->mappings,
1677       GUINT_TO_POINTER (((guint) tag)));
1678 
1679   if (ul) {
1680 #ifndef GST_DISABLE_GST_DEBUG
1681     gchar str[48];
1682 #endif
1683 
1684     GST_DEBUG ("Adding local tag 0x%04x with UL %s and size %u", tag,
1685         mxf_ul_to_string (ul, str), tag_size);
1686 
1687     local_tag = g_slice_new0 (MXFLocalTag);
1688     memcpy (&local_tag->ul, ul, sizeof (MXFUL));
1689     local_tag->size = tag_size;
1690     local_tag->data = tag_size == 0 ? NULL : g_memdup (tag_data, tag_size);
1691     local_tag->g_slice = FALSE;
1692 
1693     g_hash_table_insert (*hash_table, &local_tag->ul, local_tag);
1694   } else {
1695     GST_WARNING ("Local tag with no entry in primer pack: 0x%04x", tag);
1696   }
1697 
1698   return TRUE;
1699 }
1700 
1701 gboolean
mxf_local_tag_insert(MXFLocalTag * tag,GHashTable ** hash_table)1702 mxf_local_tag_insert (MXFLocalTag * tag, GHashTable ** hash_table)
1703 {
1704 #ifndef GST_DISABLE_GST_DEBUG
1705   gchar str[48];
1706 #endif
1707 
1708   g_return_val_if_fail (tag != NULL, FALSE);
1709   g_return_val_if_fail (hash_table != NULL, FALSE);
1710 
1711   if (*hash_table == NULL)
1712     *hash_table =
1713         g_hash_table_new_full ((GHashFunc) mxf_ul_hash,
1714         (GEqualFunc) mxf_ul_is_equal, (GDestroyNotify) NULL,
1715         (GDestroyNotify) mxf_local_tag_free);
1716 
1717   g_return_val_if_fail (*hash_table != NULL, FALSE);
1718 
1719   GST_DEBUG ("Adding local tag with UL %s and size %u",
1720       mxf_ul_to_string (&tag->ul, str), tag->size);
1721 
1722   g_hash_table_insert (*hash_table, &tag->ul, tag);
1723 
1724   return TRUE;
1725 }
1726