2 * Copyright (C) 2008-2009 Sebastian Dröge <sebastian.droege@collabora.co.uk>
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.
25 #include <gst/base/gstbytewriter.h>
30 GST_DEBUG_CATEGORY_EXTERN (mxf_debug);
31 #define GST_CAT_DEFAULT mxf_debug
34 mxf_is_mxf_packet (const MXFUL * ul)
36 return mxf_ul_is_subclass (MXF_UL (SMPTE), ul);
39 /* SMPTE 377M 6.1: Check if this is a valid partition pack */
41 mxf_is_partition_pack (const MXFUL * ul)
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)
51 /* SMPTE 377M 6.2: header partition pack has byte 14 == 0x02 */
53 mxf_is_header_partition_pack (const MXFUL * ul)
55 if (mxf_is_partition_pack (ul) && ul->u[13] == 0x02)
61 /* SMPTE 377M 6.3: body partition pack has byte 14 == 0x03 */
63 mxf_is_body_partition_pack (const MXFUL * ul)
65 if (mxf_is_partition_pack (ul) && ul->u[13] == 0x03)
71 /* SMPTE 377M 6.4: footer partition pack has byte 14 == 0x04 */
73 mxf_is_footer_partition_pack (const MXFUL * ul)
75 if (mxf_is_partition_pack (ul) && ul->u[13] == 0x04)
82 mxf_is_fill (const MXFUL * ul)
84 return (mxf_ul_is_subclass (MXF_UL (FILL), ul));
88 mxf_is_primer_pack (const MXFUL * ul)
90 return (mxf_ul_is_subclass (MXF_UL (PRIMER_PACK), ul));
94 mxf_is_metadata (const MXFUL * ul)
96 return (mxf_ul_is_subclass (MXF_UL (METADATA), ul));
99 /* SMPTE 377M 8.7.3 */
101 mxf_is_descriptive_metadata (const MXFUL * ul)
103 return (mxf_ul_is_subclass (MXF_UL (DESCRIPTIVE_METADATA), ul));
107 mxf_is_random_index_pack (const MXFUL * ul)
109 return (mxf_ul_is_subclass (MXF_UL (RANDOM_INDEX_PACK), ul));
113 mxf_is_index_table_segment (const MXFUL * ul)
115 return (mxf_ul_is_subclass (MXF_UL (INDEX_TABLE_SEGMENT), ul));
118 /* SMPTE 379M 6.2.1 */
120 mxf_is_generic_container_system_item (const MXFUL * ul)
122 return (mxf_ul_is_subclass (MXF_UL (GENERIC_CONTAINER_SYSTEM_ITEM), ul) &&
123 (ul->u[12] == 0x04 || ul->u[12] == 0x14));
128 mxf_is_generic_container_essence_element (const MXFUL * ul)
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));
138 mxf_is_generic_container_essence_container_label (const MXFUL * ul)
140 return (mxf_ul_is_subclass (MXF_UL
141 (GENERIC_CONTAINER_ESSENCE_CONTAINER_LABEL), ul) && (ul->u[12] == 0x01
142 || ul->u[12] == 0x02));
145 /* Essence container label found in files generated by Avid */
147 mxf_is_avid_essence_container_label (const MXFUL * ul)
149 return (mxf_ul_is_subclass (MXF_UL (AVID_ESSENCE_CONTAINER_ESSENCE_LABEL),
153 /* Essence element key found in files generated by Avid */
155 mxf_is_avid_essence_container_essence_element (const MXFUL * ul)
157 return (mxf_ul_is_subclass (MXF_UL (AVID_ESSENCE_CONTAINER_ESSENCE_ELEMENT),
162 mxf_ber_encode_size (guint size, guint8 ber[9])
176 tmp[slen] = size & 0xff;
181 ber[0] = 0x80 | slen;
182 for (i = 0; i < slen; i++) {
183 ber[i + 1] = tmp[slen - i - 1];
190 mxf_fill_to_buffer (guint size)
197 slen = mxf_ber_encode_size (size, ber);
199 ret = gst_buffer_new_and_alloc (16 + slen + size);
200 gst_buffer_map (ret, &map, GST_MAP_WRITE);
202 memcpy (map.data, MXF_UL (FILL), 16);
203 memcpy (map.data + 16, &ber, slen);
204 memset (map.data + slen, 0, size);
206 gst_buffer_unmap (ret, &map);
212 mxf_uuid_init (MXFUUID * uuid, GHashTable * hashtable)
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)));
226 mxf_uuid_is_equal (const MXFUUID * a, const MXFUUID * b)
228 g_return_val_if_fail (a != NULL, FALSE);
229 g_return_val_if_fail (b != NULL, FALSE);
231 return (memcmp (a, b, 16) == 0);
235 mxf_uuid_is_zero (const MXFUUID * a)
237 static const guint8 zero[16] = { 0x00, };
239 g_return_val_if_fail (a != NULL, FALSE);
241 return (memcmp (a, zero, 16) == 0);
245 mxf_uuid_hash (const MXFUUID * uuid)
250 g_return_val_if_fail (uuid != NULL, 0);
252 for (i = 0; i < 4; i++)
253 ret ^= GST_READ_UINT32_BE (uuid->u + i * 4);
259 mxf_uuid_to_string (const MXFUUID * uuid, gchar str[48])
263 g_return_val_if_fail (uuid != NULL, NULL);
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]);
282 mxf_uuid_from_string (const gchar * str, MXFUUID * uuid)
288 g_return_val_if_fail (str != NULL, NULL);
292 GST_ERROR ("Invalid UUID string length %d, should be 47", len);
297 ret = g_new0 (MXFUUID, 1);
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);
311 ret->u[i] = (g_ascii_xdigit_value (str[j]) << 4) |
312 (g_ascii_xdigit_value (str[j + 1]));
319 mxf_uuid_array_parse (MXFUUID ** array, guint32 * count, const guint8 * data,
322 guint32 element_count, element_size;
325 g_return_val_if_fail (array != NULL, FALSE);
326 g_return_val_if_fail (count != NULL, FALSE);
331 g_return_val_if_fail (data != NULL, FALSE);
333 element_count = GST_READ_UINT32_BE (data);
337 if (element_count == 0) {
343 element_size = GST_READ_UINT32_BE (data);
347 if (element_size != 16) {
353 if (element_count > size / 16) {
359 *array = g_new (MXFUUID, element_count);
360 *count = element_count;
362 for (i = 0; i < element_count; i++) {
363 memcpy (&((*array)[i]), data, 16);
371 mxf_umid_is_equal (const MXFUMID * a, const MXFUMID * b)
373 return (memcmp (a, b, 32) == 0);
377 mxf_umid_is_zero (const MXFUMID * umid)
379 static const MXFUMID zero = { {0,} };
381 return (memcmp (umid, &zero, 32) == 0);
385 mxf_umid_to_string (const MXFUMID * umid, gchar str[96])
387 g_return_val_if_fail (umid != NULL, NULL);
388 g_return_val_if_fail (str != NULL, NULL);
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],
406 umid->u[26], umid->u[27], umid->u[28], umid->u[29], umid->u[30],
414 mxf_umid_from_string (const gchar * str, MXFUMID * umid)
419 g_return_val_if_fail (str != NULL, NULL);
422 memset (umid, 0, 32);
425 GST_ERROR ("Invalid UMID string length %d", len);
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);
438 (g_ascii_xdigit_value (str[j]) << 4) | (g_ascii_xdigit_value (str[j +
446 mxf_umid_init (MXFUMID * umid)
451 /* SMPTE S330M 5.1.1:
461 umid->u[7] = 0x05; /* version, see RP210 */
464 umid->u[10] = 0x0d; /* mixed group of components in a single container */
466 /* - UUID/UL method for material number
467 * - 24 bit PRG for instance number
469 umid->u[11] = 0x20 | 0x02;
471 /* Length of remaining data */
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;
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 ());
484 umid->u[16 + 6] &= 0x0f;
485 umid->u[16 + 6] |= 0x40;
487 umid->u[16 + 8] &= 0x3f;
488 umid->u[16 + 8] |= 0x80;
492 mxf_timestamp_parse (MXFTimestamp * timestamp, const guint8 * data, guint size)
494 g_return_val_if_fail (timestamp != NULL, FALSE);
496 memset (timestamp, 0, sizeof (MXFTimestamp));
501 g_return_val_if_fail (data != NULL, FALSE);
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;
514 /* SMPTE 377M 3.3: A value of 0 for every field means unknown timestamp */
516 mxf_timestamp_is_unknown (const MXFTimestamp * a)
518 static const MXFTimestamp unknown = { 0, 0, 0, 0, 0, 0, 0 };
520 return (memcmp (a, &unknown, sizeof (MXFTimestamp)) == 0);
524 mxf_timestamp_compare (const MXFTimestamp * a, const MXFTimestamp * b)
528 if ((diff = a->year - b->year) != 0)
530 else if ((diff = a->month - b->month) != 0)
532 else if ((diff = a->day - b->day) != 0)
534 else if ((diff = a->hour - b->hour) != 0)
536 else if ((diff = a->minute - b->minute) != 0)
538 else if ((diff = a->second - b->second) != 0)
540 else if ((diff = a->msecond - b->msecond) != 0)
547 mxf_timestamp_to_string (const MXFTimestamp * t, gchar 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);
556 mxf_timestamp_set_now (MXFTimestamp * timestamp)
566 now = g_get_real_time ();
567 t = now / G_USEC_PER_SEC;
570 tm = gmtime_r (&t, &tm_);
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 = now / 1000;
585 mxf_timestamp_write (const MXFTimestamp * timestamp, guint8 * data)
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);
597 mxf_fraction_parse (MXFFraction * fraction, const guint8 * data, guint size)
599 g_return_val_if_fail (fraction != NULL, FALSE);
601 memset (fraction, 0, sizeof (MXFFraction));
606 g_return_val_if_fail (data != NULL, FALSE);
608 fraction->n = GST_READ_UINT32_BE (data);
609 fraction->d = GST_READ_UINT32_BE (data + 4);
615 mxf_fraction_to_double (const MXFFraction * fraction)
617 return ((gdouble) fraction->n) / ((gdouble) fraction->d);
621 mxf_utf16_to_utf8 (const guint8 * data, guint size)
624 GError *error = NULL;
627 g_convert ((const gchar *) data, size, "UTF-8", "UTF-16BE", NULL, NULL,
631 GST_WARNING ("UTF-16-BE to UTF-8 conversion failed: %s", error->message);
632 g_error_free (error);
640 mxf_utf8_to_utf16 (const gchar * str, guint16 * size)
643 GError *error = NULL;
646 g_return_val_if_fail (size != NULL, NULL);
654 g_convert_with_fallback (str, -1, "UTF-16BE", "UTF-8", (char *) "*", NULL,
658 GST_WARNING ("UTF-16-BE to UTF-8 conversion failed: %s", error->message);
659 g_error_free (error);
665 return (guint8 *) ret;
669 mxf_product_version_parse (MXFProductVersion * product_version,
670 const guint8 * data, guint size)
672 g_return_val_if_fail (product_version != NULL, FALSE);
674 memset (product_version, 0, sizeof (MXFProductVersion));
679 g_return_val_if_fail (data != NULL, FALSE);
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);
686 /* Avid writes a 9 byte product version */
688 product_version->release = GST_READ_UINT8 (data + 8);
690 product_version->release = GST_READ_UINT16_BE (data + 8);
696 mxf_product_version_is_valid (const MXFProductVersion * version)
698 static const guint8 null[sizeof (MXFProductVersion)] = { 0, };
700 return (memcmp (version, &null, sizeof (MXFProductVersion)) == 0);
704 mxf_product_version_write (const MXFProductVersion * version, guint8 * data)
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);
714 mxf_op_set_atom (MXFUL * ul, gboolean single_sourceclip,
715 gboolean single_essence_track)
717 memcpy (&ul->u, MXF_UL (OPERATIONAL_PATTERN_IDENTIFICATION), 12);
721 if (!single_sourceclip)
724 if (!single_essence_track)
732 mxf_op_set_generalized (MXFUL * ul, MXFOperationalPattern pattern,
733 gboolean internal_essence, gboolean streamable, gboolean single_track)
735 g_return_if_fail (pattern >= MXF_OP_1a);
737 memcpy (&ul->u, MXF_UL (OPERATIONAL_PATTERN_IDENTIFICATION), 12);
739 if (pattern == MXF_OP_1a || pattern == MXF_OP_1b || pattern == MXF_OP_1c)
741 else if (pattern == MXF_OP_2a || pattern == MXF_OP_2b || pattern == MXF_OP_2c)
743 else if (pattern == MXF_OP_3a || pattern == MXF_OP_3b || pattern == MXF_OP_3c)
746 if (pattern == MXF_OP_1a || pattern == MXF_OP_2a || pattern == MXF_OP_3a)
748 else if (pattern == MXF_OP_1b || pattern == MXF_OP_2b || pattern == MXF_OP_3b)
750 else if (pattern == MXF_OP_1c || pattern == MXF_OP_2c || pattern == MXF_OP_3c)
754 if (!internal_essence)
764 /* SMPTE 377M 6.1, Table 2 */
766 mxf_partition_pack_parse (const MXFUL * ul, MXFPartitionPack * pack,
767 const guint8 * data, guint size)
769 #ifndef GST_DISABLE_GST_DEBUG
777 g_return_val_if_fail (data != NULL, FALSE);
779 memset (pack, 0, sizeof (MXFPartitionPack));
781 GST_DEBUG ("Parsing partition pack:");
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;
790 GST_DEBUG (" type = %s",
791 (pack->type == MXF_PARTITION_PACK_HEADER) ? "header" : (pack->type ==
792 MXF_PARTITION_PACK_BODY) ? "body" : "footer");
794 pack->closed = (ul->u[14] == 0x02 || ul->u[14] == 0x04);
795 pack->complete = (ul->u[14] == 0x03 || ul->u[14] == 0x04);
797 GST_DEBUG (" closed = %s, complete = %s", (pack->closed) ? "yes" : "no",
798 (pack->complete) ? "yes" : "no");
800 pack->major_version = GST_READ_UINT16_BE (data);
801 if (pack->major_version != 1)
806 pack->minor_version = GST_READ_UINT16_BE (data);
810 GST_DEBUG (" MXF version = %u.%u", pack->major_version, pack->minor_version);
812 pack->kag_size = GST_READ_UINT32_BE (data);
816 GST_DEBUG (" KAG size = %u", pack->kag_size);
818 pack->this_partition = GST_READ_UINT64_BE (data);
822 GST_DEBUG (" this partition offset = %" G_GUINT64_FORMAT,
823 pack->this_partition);
825 pack->prev_partition = GST_READ_UINT64_BE (data);
829 GST_DEBUG (" previous partition offset = %" G_GUINT64_FORMAT,
830 pack->prev_partition);
832 pack->footer_partition = GST_READ_UINT64_BE (data);
836 GST_DEBUG (" footer partition offset = %" G_GUINT64_FORMAT,
837 pack->footer_partition);
839 pack->header_byte_count = GST_READ_UINT64_BE (data);
843 GST_DEBUG (" header byte count = %" G_GUINT64_FORMAT,
844 pack->header_byte_count);
846 pack->index_byte_count = GST_READ_UINT64_BE (data);
850 pack->index_sid = GST_READ_UINT32_BE (data);
854 GST_DEBUG (" index sid = %u, size = %" G_GUINT64_FORMAT, pack->index_sid,
855 pack->index_byte_count);
857 pack->body_offset = GST_READ_UINT64_BE (data);
861 pack->body_sid = GST_READ_UINT32_BE (data);
865 GST_DEBUG (" body sid = %u, offset = %" G_GUINT64_FORMAT, pack->body_sid,
868 memcpy (&pack->operational_pattern, data, 16);
872 GST_DEBUG (" operational pattern = %s",
873 mxf_ul_to_string (&pack->operational_pattern, str));
875 if (!mxf_ul_array_parse (&pack->essence_containers,
876 &pack->n_essence_containers, data, size))
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));
892 GST_ERROR ("Invalid partition pack");
894 mxf_partition_pack_reset (pack);
899 mxf_partition_pack_reset (MXFPartitionPack * pack)
901 g_return_if_fail (pack != NULL);
903 g_free (pack->essence_containers);
905 memset (pack, 0, sizeof (MXFPartitionPack));
909 mxf_partition_pack_to_buffer (const MXFPartitionPack * pack)
918 8 + 16 * pack->n_essence_containers + 16 + 4 + 8 + 4 + 8 + 8 + 8 + 8 + 8 +
921 slen = mxf_ber_encode_size (size, ber);
923 ret = gst_buffer_new_and_alloc (16 + slen + size);
924 gst_buffer_map (ret, &map, GST_MAP_WRITE);
926 memcpy (map.data, MXF_UL (PARTITION_PACK), 13);
927 if (pack->type == MXF_PARTITION_PACK_HEADER)
929 else if (pack->type == MXF_PARTITION_PACK_BODY)
931 else if (pack->type == MXF_PARTITION_PACK_FOOTER)
935 map.data[14] |= 0x02;
937 map.data[14] |= 0x01;
940 memcpy (map.data + 16, &ber, slen);
942 data = map.data + 16 + slen;
944 GST_WRITE_UINT16_BE (data, pack->major_version);
945 GST_WRITE_UINT16_BE (data + 2, pack->minor_version);
948 GST_WRITE_UINT32_BE (data, pack->kag_size);
951 GST_WRITE_UINT64_BE (data, pack->this_partition);
954 GST_WRITE_UINT64_BE (data, pack->prev_partition);
957 GST_WRITE_UINT64_BE (data, pack->footer_partition);
960 GST_WRITE_UINT64_BE (data, pack->header_byte_count);
963 GST_WRITE_UINT64_BE (data, pack->index_byte_count);
966 GST_WRITE_UINT32_BE (data, pack->index_sid);
969 GST_WRITE_UINT64_BE (data, pack->body_offset);
972 GST_WRITE_UINT32_BE (data, pack->body_sid);
975 memcpy (data, &pack->operational_pattern, 16);
978 GST_WRITE_UINT32_BE (data, pack->n_essence_containers);
979 GST_WRITE_UINT32_BE (data + 4, 16);
982 for (i = 0; i < pack->n_essence_containers; i++)
983 memcpy (data + 16 * i, &pack->essence_containers[i], 16);
985 gst_buffer_unmap (ret, &map);
990 /* SMPTE 377M 11.1 */
992 mxf_random_index_pack_parse (const MXFUL * ul, const guint8 * data, guint size,
996 MXFRandomIndexPackEntry entry;
998 g_return_val_if_fail (array != NULL, FALSE);
1003 g_return_val_if_fail (data != NULL, FALSE);
1005 if ((size - 4) % 12 != 0)
1008 GST_DEBUG ("Parsing random index pack:");
1010 len = (size - 4) / 12;
1012 GST_DEBUG (" number of entries = %u", len);
1015 g_array_sized_new (FALSE, FALSE, sizeof (MXFRandomIndexPackEntry), len);
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);
1022 GST_DEBUG (" entry %u = body sid %u at offset %" G_GUINT64_FORMAT, i,
1023 entry.body_sid, entry.offset);
1025 g_array_append_val (*array, entry);
1032 mxf_random_index_pack_to_buffer (const GArray * array)
1034 MXFRandomIndexPackEntry *entry;
1038 guint8 slen, ber[9];
1042 if (array->len == 0)
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);
1050 memcpy (map.data, MXF_UL (RANDOM_INDEX_PACK), 16);
1051 memcpy (map.data + 16, ber, slen);
1053 data = map.data + 16 + slen;
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);
1061 GST_WRITE_UINT32_BE (data, gst_buffer_get_size (ret));
1063 gst_buffer_unmap (ret, &map);
1068 /* SMPTE 377M 10.2.3 */
1070 mxf_index_table_segment_parse (const MXFUL * ul,
1071 MXFIndexTableSegment * segment, const guint8 * data, guint size)
1073 #ifndef GST_DISABLE_GST_DEBUG
1076 guint16 tag, tag_size;
1077 const guint8 *tag_data;
1079 g_return_val_if_fail (ul != NULL, FALSE);
1081 memset (segment, 0, sizeof (MXFIndexTableSegment));
1086 g_return_val_if_fail (data != NULL, FALSE);
1088 GST_DEBUG ("Parsing index table segment:");
1090 while (mxf_local_tag_parse (data, size, &tag, &tag_size, &tag_data)) {
1091 data += 4 + tag_size;
1092 size -= 4 + tag_size;
1094 if (tag_size == 0 || tag == 0x0000)
1101 memcpy (&segment->instance_id, tag_data, 16);
1102 GST_DEBUG (" instance id = %s",
1103 mxf_uuid_to_string (&segment->instance_id, str));
1106 if (!mxf_fraction_parse (&segment->index_edit_rate, tag_data, tag_size))
1108 GST_DEBUG (" index edit rate = %d/%d", segment->index_edit_rate.n,
1109 segment->index_edit_rate.d);
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);
1121 segment->index_duration = GST_READ_UINT64_BE (tag_data);
1122 GST_DEBUG (" index duration = %" G_GINT64_FORMAT,
1123 segment->index_duration);
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);
1135 segment->index_sid = GST_READ_UINT32_BE (tag_data);
1136 GST_DEBUG (" index sid = %u", segment->index_sid);
1141 segment->body_sid = GST_READ_UINT32_BE (tag_data);
1142 GST_DEBUG (" body sid = %u", segment->body_sid);
1147 segment->slice_count = GST_READ_UINT8 (tag_data);
1148 GST_DEBUG (" slice count = %u", segment->slice_count);
1153 segment->pos_table_count = GST_READ_UINT8 (tag_data);
1154 GST_DEBUG (" pos table count = %u", segment->pos_table_count);
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);
1170 if (GST_READ_UINT32_BE (tag_data) != 6)
1176 if (tag_size / 6 < len)
1179 segment->delta_entries = g_new (MXFDeltaEntry, len);
1181 for (i = 0; i < len; i++) {
1182 GST_DEBUG (" delta entry %u:", i);
1184 segment->delta_entries[i].pos_table_index = GST_READ_UINT8 (tag_data);
1187 GST_DEBUG (" pos table index = %d",
1188 segment->delta_entries[i].pos_table_index);
1190 segment->delta_entries[i].slice = GST_READ_UINT8 (tag_data);
1193 GST_DEBUG (" slice = %u", segment->delta_entries[i].slice);
1195 segment->delta_entries[i].element_delta =
1196 GST_READ_UINT32_BE (tag_data);
1199 GST_DEBUG (" element delta = %u",
1200 segment->delta_entries[i].element_delta);
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);
1218 if (GST_READ_UINT32_BE (tag_data) !=
1219 (11 + 4 * segment->slice_count + 8 * segment->pos_table_count))
1225 if (tag_size / (11 + 4 * segment->slice_count +
1226 8 * segment->pos_table_count) < len)
1229 segment->index_entries = g_new0 (MXFIndexEntry, len);
1231 for (i = 0; i < len; i++) {
1232 MXFIndexEntry *entry = &segment->index_entries[i];
1234 GST_DEBUG (" index entry %u:", i);
1236 entry->temporal_offset = GST_READ_UINT8 (tag_data);
1239 GST_DEBUG (" temporal offset = %d", entry->temporal_offset);
1241 entry->key_frame_offset = GST_READ_UINT8 (tag_data);
1244 GST_DEBUG (" keyframe offset = %d", entry->key_frame_offset);
1246 entry->flags = GST_READ_UINT8 (tag_data);
1249 GST_DEBUG (" flags = 0x%02x (%s%s%s%s)", entry->flags,
1250 entry->flags & 0x80 ? "Random-Access " : "",
1251 entry->flags & 0x40 ? "Sequence-Header " : "",
1252 entry->flags & 0x20 ? "Forward-Prediction " : "",
1253 entry->flags & 0x10 ? "Backward-Prediction " : "");
1255 entry->stream_offset = GST_READ_UINT64_BE (tag_data);
1258 GST_DEBUG (" stream offset = %" G_GUINT64_FORMAT,
1259 entry->stream_offset);
1261 entry->slice_offset = g_new0 (guint32, segment->slice_count);
1262 for (j = 0; j < segment->slice_count; j++) {
1263 entry->slice_offset[j] = GST_READ_UINT32_BE (tag_data);
1266 GST_DEBUG (" slice %u offset = %u", j, entry->slice_offset[j]);
1269 entry->pos_table = g_new0 (MXFFraction, segment->pos_table_count);
1270 for (j = 0; j < segment->pos_table_count; j++) {
1271 if (!mxf_fraction_parse (&entry->pos_table[j], tag_data, tag_size))
1275 GST_DEBUG (" pos table %u = %d/%d", j, entry->pos_table[j].n,
1276 entry->pos_table[j].d);
1283 ("Unknown local tag 0x%04x of size %d in index table segment", tag,
1291 GST_ERROR ("Invalid index table segment");
1292 mxf_index_table_segment_reset (segment);
1297 mxf_index_table_segment_reset (MXFIndexTableSegment * segment)
1301 g_return_if_fail (segment != NULL);
1303 if (segment->index_entries) {
1304 for (i = 0; i < segment->n_index_entries; i++) {
1305 g_free (segment->index_entries[i].slice_offset);
1306 g_free (segment->index_entries[i].pos_table);
1310 g_free (segment->index_entries);
1311 g_free (segment->delta_entries);
1313 memset (segment, 0, sizeof (MXFIndexTableSegment));
1317 mxf_index_table_segment_to_buffer (const MXFIndexTableSegment * segment)
1325 g_return_val_if_fail (segment != NULL, NULL);
1326 g_return_val_if_fail (segment->n_delta_entries * 6 < G_MAXUINT16, NULL);
1327 g_return_val_if_fail (segment->n_index_entries * (11 +
1328 4 * segment->slice_count + 8 * segment->pos_table_count) <
1332 16 + 4 + 8 + 4 + 8 + 4 + 8 + 4 + 4 + 4 + 4 + 4 + 4 + 4 + 1 + 4 + 1 + 4 +
1333 4 + 8 + segment->n_delta_entries * 6 + 4 + 8 +
1334 segment->n_index_entries * (11 + 4 * segment->slice_count +
1335 8 * segment->pos_table_count);
1336 slen = mxf_ber_encode_size (len, ber);
1338 ret = gst_buffer_new_and_alloc (16 + slen + len);
1339 gst_buffer_map (ret, &map, GST_MAP_WRITE);
1341 gst_byte_writer_init_with_data (&bw, map.data, map.size, FALSE);
1343 gst_byte_writer_put_data_unchecked (&bw,
1344 (const guint8 *) MXF_UL (INDEX_TABLE_SEGMENT), 16);
1345 gst_byte_writer_put_data_unchecked (&bw, ber, slen);
1347 gst_byte_writer_put_uint16_be_unchecked (&bw, 0x3c0a);
1348 gst_byte_writer_put_uint16_be_unchecked (&bw, 16);
1349 gst_byte_writer_put_data_unchecked (&bw,
1350 (const guint8 *) &segment->instance_id, 16);
1352 gst_byte_writer_put_uint16_be_unchecked (&bw, 0x3f0b);
1353 gst_byte_writer_put_uint16_be_unchecked (&bw, 8);
1354 gst_byte_writer_put_uint32_be_unchecked (&bw, segment->index_edit_rate.n);
1355 gst_byte_writer_put_uint32_be_unchecked (&bw, segment->index_edit_rate.d);
1357 gst_byte_writer_put_uint16_be_unchecked (&bw, 0x3f0c);
1358 gst_byte_writer_put_uint16_be_unchecked (&bw, 8);
1359 gst_byte_writer_put_uint64_be_unchecked (&bw, segment->index_start_position);
1361 gst_byte_writer_put_uint16_be_unchecked (&bw, 0x3f0d);
1362 gst_byte_writer_put_uint16_be_unchecked (&bw, 8);
1363 gst_byte_writer_put_uint64_be_unchecked (&bw, segment->index_duration);
1365 gst_byte_writer_put_uint16_be_unchecked (&bw, 0x3f05);
1366 gst_byte_writer_put_uint16_be_unchecked (&bw, 4);
1367 gst_byte_writer_put_uint32_be_unchecked (&bw, segment->edit_unit_byte_count);
1369 gst_byte_writer_put_uint16_be_unchecked (&bw, 0x3f06);
1370 gst_byte_writer_put_uint16_be_unchecked (&bw, 4);
1371 gst_byte_writer_put_uint32_be_unchecked (&bw, segment->index_sid);
1373 gst_byte_writer_put_uint16_be_unchecked (&bw, 0x3f07);
1374 gst_byte_writer_put_uint16_be_unchecked (&bw, 4);
1375 gst_byte_writer_put_uint32_be_unchecked (&bw, segment->body_sid);
1377 gst_byte_writer_put_uint16_be_unchecked (&bw, 0x3f08);
1378 gst_byte_writer_put_uint16_be_unchecked (&bw, 1);
1379 gst_byte_writer_put_uint8 (&bw, segment->slice_count);
1381 gst_byte_writer_put_uint16_be_unchecked (&bw, 0x3f0e);
1382 gst_byte_writer_put_uint16_be_unchecked (&bw, 1);
1383 gst_byte_writer_put_uint8 (&bw, segment->pos_table_count);
1385 gst_byte_writer_put_uint16_be_unchecked (&bw, 0x3f09);
1386 gst_byte_writer_put_uint16_be_unchecked (&bw,
1387 8 + segment->n_delta_entries * 6);
1388 gst_byte_writer_put_uint32_be_unchecked (&bw, segment->n_delta_entries);
1389 gst_byte_writer_put_uint32_be_unchecked (&bw, 6);
1390 for (i = 0; i < segment->n_delta_entries; i++) {
1391 gst_byte_writer_put_uint8_unchecked (&bw,
1392 segment->delta_entries[i].pos_table_index);
1393 gst_byte_writer_put_uint8_unchecked (&bw, segment->delta_entries[i].slice);
1394 gst_byte_writer_put_uint32_be_unchecked (&bw,
1395 segment->delta_entries[i].element_delta);
1398 gst_byte_writer_put_uint16_be_unchecked (&bw, 0x3f0a);
1399 gst_byte_writer_put_uint16_be_unchecked (&bw,
1400 8 + segment->n_index_entries * (11 + 4 * segment->slice_count +
1401 8 * segment->pos_table_count));
1402 gst_byte_writer_put_uint32_be_unchecked (&bw, segment->n_index_entries);
1403 gst_byte_writer_put_uint32_be_unchecked (&bw,
1404 (11 + 4 * segment->slice_count + 8 * segment->pos_table_count));
1405 for (i = 0; i < segment->n_index_entries; i++) {
1408 gst_byte_writer_put_uint8_unchecked (&bw,
1409 segment->index_entries[i].temporal_offset);
1410 gst_byte_writer_put_uint8_unchecked (&bw,
1411 segment->index_entries[i].key_frame_offset);
1412 gst_byte_writer_put_uint8_unchecked (&bw, segment->index_entries[i].flags);
1413 gst_byte_writer_put_uint64_be_unchecked (&bw,
1414 segment->index_entries[i].stream_offset);
1416 for (j = 0; j < segment->slice_count; j++)
1417 gst_byte_writer_put_uint32_be_unchecked (&bw,
1418 segment->index_entries[i].slice_offset[j]);
1420 for (j = 0; j < segment->pos_table_count; j++) {
1421 gst_byte_writer_put_uint32_be_unchecked (&bw,
1422 segment->index_entries[i].pos_table[j].n);
1423 gst_byte_writer_put_uint32_be_unchecked (&bw,
1424 segment->index_entries[i].pos_table[j].d);
1428 g_assert (gst_byte_writer_get_pos (&bw) == map.size);
1430 gst_buffer_unmap (ret, &map);
1435 /* SMPTE 377M 8.2 Table 1 and 2 */
1438 _mxf_mapping_ul_free (MXFUL * ul)
1440 g_slice_free (MXFUL, ul);
1444 mxf_primer_pack_parse (const MXFUL * ul, MXFPrimerPack * pack,
1445 const guint8 * data, guint size)
1453 g_return_val_if_fail (data != NULL, FALSE);
1455 memset (pack, 0, sizeof (MXFPrimerPack));
1457 GST_DEBUG ("Parsing primer pack:");
1460 g_hash_table_new_full (g_direct_hash, g_direct_equal,
1461 (GDestroyNotify) NULL, (GDestroyNotify) _mxf_mapping_ul_free);
1463 n = GST_READ_UINT32_BE (data);
1467 GST_DEBUG (" number of mappings = %u", n);
1469 if (GST_READ_UINT32_BE (data) != 18)
1477 for (i = 0; i < n; i++) {
1479 #ifndef GST_DISABLE_GST_DEBUG
1484 local_tag = GST_READ_UINT16_BE (data);
1487 if (g_hash_table_lookup (pack->mappings, GUINT_TO_POINTER (local_tag)))
1490 uid = g_slice_new (MXFUL);
1491 memcpy (uid, data, 16);
1494 g_hash_table_insert (pack->mappings, GUINT_TO_POINTER (local_tag), uid);
1495 GST_DEBUG (" Adding mapping = 0x%04x -> %s", local_tag,
1496 mxf_ul_to_string (uid, str));
1502 GST_DEBUG ("Invalid primer pack");
1503 mxf_primer_pack_reset (pack);
1508 mxf_primer_pack_reset (MXFPrimerPack * pack)
1510 g_return_if_fail (pack != NULL);
1513 g_hash_table_destroy (pack->mappings);
1514 if (pack->reverse_mappings)
1515 g_hash_table_destroy (pack->reverse_mappings);
1517 memset (pack, 0, sizeof (MXFPrimerPack));
1519 pack->next_free_tag = 0x8000;
1523 mxf_primer_pack_add_mapping (MXFPrimerPack * primer, guint16 local_tag,
1527 #ifndef GST_DISABLE_GST_DEBUG
1530 guint ltag_tmp = local_tag;
1532 if (primer->mappings == NULL) {
1533 primer->mappings = g_hash_table_new_full (g_direct_hash, g_direct_equal,
1534 (GDestroyNotify) NULL, (GDestroyNotify) _mxf_mapping_ul_free);
1537 if (primer->reverse_mappings == NULL) {
1538 primer->reverse_mappings = g_hash_table_new_full ((GHashFunc) mxf_ul_hash,
1539 (GEqualFunc) mxf_ul_is_equal, (GDestroyNotify) _mxf_mapping_ul_free,
1540 (GDestroyNotify) NULL);
1543 if (primer->next_free_tag == 0xffff && ltag_tmp == 0) {
1544 GST_ERROR ("Used too many dynamic tags");
1548 if (ltag_tmp == 0) {
1551 tmp = GPOINTER_TO_UINT (g_hash_table_lookup (primer->reverse_mappings, ul));
1553 ltag_tmp = primer->next_free_tag;
1554 primer->next_free_tag++;
1557 if (g_hash_table_lookup (primer->mappings, GUINT_TO_POINTER (ltag_tmp)))
1561 g_assert (ltag_tmp != 0);
1563 uid = g_slice_new (MXFUL);
1564 memcpy (uid, ul, 16);
1566 GST_DEBUG ("Adding mapping = 0x%04x -> %s", ltag_tmp,
1567 mxf_ul_to_string (uid, str));
1568 g_hash_table_insert (primer->mappings, GUINT_TO_POINTER (ltag_tmp), uid);
1569 uid = g_slice_dup (MXFUL, uid);
1570 g_hash_table_insert (primer->reverse_mappings, uid,
1571 GUINT_TO_POINTER (ltag_tmp));
1577 mxf_primer_pack_to_buffer (const MXFPrimerPack * pack)
1587 n = g_hash_table_size (pack->mappings);
1591 slen = mxf_ber_encode_size (8 + 18 * n, ber);
1593 ret = gst_buffer_new_and_alloc (16 + slen + 8 + 18 * n);
1594 gst_buffer_map (ret, &map, GST_MAP_WRITE);
1596 memcpy (map.data, MXF_UL (PRIMER_PACK), 16);
1597 memcpy (map.data + 16, &ber, slen);
1599 data = map.data + 16 + slen;
1601 GST_WRITE_UINT32_BE (data, n);
1602 GST_WRITE_UINT32_BE (data + 4, 18);
1605 if (pack->mappings) {
1608 GHashTableIter iter;
1610 g_hash_table_iter_init (&iter, pack->mappings);
1612 while (g_hash_table_iter_next (&iter, &local_tag, (gpointer) & ul)) {
1613 GST_WRITE_UINT16_BE (data, GPOINTER_TO_UINT (local_tag));
1614 memcpy (data + 2, ul, 16);
1619 gst_buffer_unmap (ret, &map);
1624 /* structural metadata parsing */
1627 mxf_local_tag_parse (const guint8 * data, guint size, guint16 * tag,
1628 guint16 * tag_size, const guint8 ** tag_data)
1633 g_return_val_if_fail (data != NULL, FALSE);
1635 *tag = GST_READ_UINT16_BE (data);
1636 *tag_size = GST_READ_UINT16_BE (data + 2);
1641 if (size < *tag_size)
1650 mxf_local_tag_free (MXFLocalTag * tag)
1653 g_slice_free1 (tag->size, tag->data);
1656 g_slice_free (MXFLocalTag, tag);
1660 mxf_local_tag_add_to_hash_table (const MXFPrimerPack * primer,
1661 guint16 tag, const guint8 * tag_data, guint16 tag_size,
1662 GHashTable ** hash_table)
1664 MXFLocalTag *local_tag;
1667 g_return_val_if_fail (primer != NULL, FALSE);
1668 g_return_val_if_fail (tag_size == 0 || tag_data != NULL, FALSE);
1669 g_return_val_if_fail (hash_table != NULL, FALSE);
1670 g_return_val_if_fail (primer->mappings != NULL, FALSE);
1672 if (*hash_table == NULL)
1674 g_hash_table_new_full ((GHashFunc) mxf_ul_hash,
1675 (GEqualFunc) mxf_ul_is_equal, (GDestroyNotify) NULL,
1676 (GDestroyNotify) mxf_local_tag_free);
1678 g_return_val_if_fail (*hash_table != NULL, FALSE);
1680 ul = (MXFUL *) g_hash_table_lookup (primer->mappings,
1681 GUINT_TO_POINTER (((guint) tag)));
1684 #ifndef GST_DISABLE_GST_DEBUG
1688 GST_DEBUG ("Adding local tag 0x%04x with UL %s and size %u", tag,
1689 mxf_ul_to_string (ul, str), tag_size);
1691 local_tag = g_slice_new0 (MXFLocalTag);
1692 memcpy (&local_tag->ul, ul, sizeof (MXFUL));
1693 local_tag->size = tag_size;
1694 local_tag->data = tag_size == 0 ? NULL : g_memdup (tag_data, tag_size);
1695 local_tag->g_slice = FALSE;
1697 g_hash_table_insert (*hash_table, &local_tag->ul, local_tag);
1699 GST_WARNING ("Local tag with no entry in primer pack: 0x%04x", tag);
1706 mxf_local_tag_insert (MXFLocalTag * tag, GHashTable ** hash_table)
1708 #ifndef GST_DISABLE_GST_DEBUG
1712 g_return_val_if_fail (tag != NULL, FALSE);
1713 g_return_val_if_fail (hash_table != NULL, FALSE);
1715 if (*hash_table == NULL)
1717 g_hash_table_new_full ((GHashFunc) mxf_ul_hash,
1718 (GEqualFunc) mxf_ul_is_equal, (GDestroyNotify) NULL,
1719 (GDestroyNotify) mxf_local_tag_free);
1721 g_return_val_if_fail (*hash_table != NULL, FALSE);
1723 GST_DEBUG ("Adding local tag with UL %s and size %u",
1724 mxf_ul_to_string (&tag->ul, str), tag->size);
1726 g_hash_table_insert (*hash_table, &tag->ul, tag);