Debian Codegraph @

gst/mxf/mxfdemux.c

raw symbols SCIP
1 /* GStreamer
2 * Copyright (C) 2008-2009 Sebastian Dröge <[email protected]>
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 /**
21 * SECTION:element-mxfdemux
22 * @title: mxfdemux
23 *
24 * mxfdemux demuxes an MXF file into the different contained streams.
25 *
26 * ## Example launch line
27 * |[
28 * gst-launch-1.0 -v filesrc location=/path/to/mxf ! mxfdemux ! audioconvert ! autoaudiosink
29 * ]| This pipeline demuxes an MXF file and outputs one of the contained raw audio streams.
30 *
31 */
32
33 /* TODO:
34 * - Handle timecode tracks correctly (where is this documented?)
35 * - Handle drop-frame field of timecode tracks
36 * - Handle Generic container system items
37 * - Post structural metadata and descriptive metadata trees as a message on the bus
38 * and send them downstream as event.
39 * - Multichannel audio needs channel layouts, define them (SMPTE S320M?).
40 * - Correctly handle the different rectangles and aspect-ratio for video
41 * - Add more support for non-standard MXF used by Avid (bug #561922).
42 * - Fix frame layout stuff, i.e. interlaced/progressive
43 * - In pull mode first find the first buffer for every pad before pushing
44 * to prevent jumpy playback in the beginning due to resynchronization.
45 *
46 * - Implement SMPTE D11 essence and the digital cinema/MXF specs
47 */
48
49 #ifdef HAVE_CONFIG_H
50 #include "config.h"
51 #endif
52
53 #include "gstmxfelements.h"
54 #include "mxfdemux.h"
55 #include "mxfessence.h"
56
57 #include <string.h>
58
59 static GstStaticPadTemplate mxf_sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
60 GST_PAD_SINK,
61 GST_PAD_ALWAYS,
62 GST_STATIC_CAPS ("application/mxf")
63 );
64
65 static GstStaticPadTemplate mxf_src_template =
66 GST_STATIC_PAD_TEMPLATE ("track_%u",
67 GST_PAD_SRC,
68 GST_PAD_SOMETIMES,
69 GST_STATIC_CAPS_ANY);
70
71 GST_DEBUG_CATEGORY_STATIC (mxfdemux_debug);
72 #define GST_CAT_DEFAULT mxfdemux_debug
73
74 /* Fill klv for the given offset, does not download the data */
75 static GstFlowReturn
76 gst_mxf_demux_peek_klv_packet (GstMXFDemux * demux, guint64 offset,
77 GstMXFKLV * klv);
78
79 /* Ensures the klv data is present. Pulls it if needed */
80 static GstFlowReturn
81 gst_mxf_demux_fill_klv (GstMXFDemux * demux, GstMXFKLV * klv);
82
83 /* Call when done with a klv. Will release the buffer (if any) and will update
84 * the demuxer offset position */
85 static void gst_mxf_demux_consume_klv (GstMXFDemux * demux, GstMXFKLV * klv);
86
87 static GstFlowReturn
88 gst_mxf_demux_handle_index_table_segment (GstMXFDemux * demux, GstMXFKLV * klv);
89
90 static void collect_index_table_segments (GstMXFDemux * demux);
91 static gboolean find_entry_for_offset (GstMXFDemux * demux,
92 GstMXFDemuxEssenceTrack * etrack, guint64 offset,
93 GstMXFDemuxIndex * retentry);
94
95 static GstClockTime gst_mxf_demux_pad_get_current_time (GstMXFDemux * demux,
96 GstMXFDemuxPad * p);
97
98 GType gst_mxf_demux_pad_get_type (void);
99 G_DEFINE_TYPE (GstMXFDemuxPad, gst_mxf_demux_pad, GST_TYPE_PAD);
100
101 static void
102 gst_mxf_demux_pad_finalize (GObject * object)
103 {
104 GstMXFDemuxPad *pad = GST_MXF_DEMUX_PAD (object);
105
106 if (pad->tags) {
107 gst_tag_list_unref (pad->tags);
108 pad->tags = NULL;
109 }
110
111 G_OBJECT_CLASS (gst_mxf_demux_pad_parent_class)->finalize (object);
112 }
113
114 static void
115 gst_mxf_demux_pad_class_init (GstMXFDemuxPadClass * klass)
116 {
117 GObjectClass *gobject_class = (GObjectClass *) klass;
118
119 gobject_class->finalize = gst_mxf_demux_pad_finalize;
120 }
121
122 static void
123 gst_mxf_demux_pad_init (GstMXFDemuxPad * pad)
124 {
125 pad->current_material_track_position = 0;
126 }
127
128 #define DEFAULT_MAX_DRIFT 100 * GST_MSECOND
129
130 enum
131 {
132 PROP_0,
133 PROP_PACKAGE,
134 PROP_MAX_DRIFT,
135 PROP_STRUCTURE
136 };
137
138 static gboolean gst_mxf_demux_sink_event (GstPad * pad, GstObject * parent,
139 GstEvent * event);
140 static gboolean gst_mxf_demux_src_event (GstPad * pad, GstObject * parent,
141 GstEvent * event);
142 static gboolean gst_mxf_demux_src_query (GstPad * pad, GstObject * parent,
143 GstQuery * query);
144
145 #define gst_mxf_demux_parent_class parent_class
146 G_DEFINE_TYPE (GstMXFDemux, gst_mxf_demux, GST_TYPE_ELEMENT);
147 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (mxfdemux, "mxfdemux", GST_RANK_PRIMARY,
148 GST_TYPE_MXF_DEMUX, mxf_element_init (plugin));
149
150 static void
151 gst_mxf_demux_remove_pad (GstMXFDemuxPad * pad, GstMXFDemux * demux)
152 {
153 gst_flow_combiner_remove_pad (demux->flowcombiner, GST_PAD_CAST (pad));
154 gst_element_remove_pad (GST_ELEMENT (demux), GST_PAD_CAST (pad));
155 }
156
157 static void
158 gst_mxf_demux_remove_pads (GstMXFDemux * demux)
159 {
160 g_ptr_array_foreach (demux->src, (GFunc) gst_mxf_demux_remove_pad, demux);
161 g_ptr_array_foreach (demux->src, (GFunc) gst_object_unref, NULL);
162 g_ptr_array_set_size (demux->src, 0);
163 }
164
165 static void
166 gst_mxf_demux_partition_free (GstMXFDemuxPartition * partition)
167 {
168 mxf_partition_pack_reset (&partition->partition);
169 mxf_primer_pack_reset (&partition->primer);
170
171 g_free (partition);
172 }
173
174 static void
175 gst_mxf_demux_essence_track_free (GstMXFDemuxEssenceTrack * t)
176 {
177 if (t->offsets)
178 g_array_free (t->offsets, TRUE);
179
180 g_free (t->mapping_data);
181
182 if (t->tags)
183 gst_tag_list_unref (t->tags);
184
185 if (t->caps)
186 gst_caps_unref (t->caps);
187
188 g_free (t);
189 }
190
191 static void
192 gst_mxf_demux_reset_mxf_state (GstMXFDemux * demux)
193 {
194 GST_DEBUG_OBJECT (demux, "Resetting MXF state");
195
196 g_list_foreach (demux->partitions, (GFunc) gst_mxf_demux_partition_free,
197 NULL);
198 g_list_free (demux->partitions);
199 demux->partitions = NULL;
200
201 demux->current_partition = NULL;
202 g_ptr_array_set_size (demux->essence_tracks, 0);
203 }
204
205 static void
206 gst_mxf_demux_reset_linked_metadata (GstMXFDemux * demux)
207 {
208 guint i;
209
210 for (i = 0; i < demux->src->len; i++) {
211 GstMXFDemuxPad *pad = g_ptr_array_index (demux->src, i);
212
213 pad->material_track = NULL;
214 pad->material_package = NULL;
215 pad->current_component = NULL;
216 }
217
218 for (i = 0; i < demux->essence_tracks->len; i++) {
219 GstMXFDemuxEssenceTrack *track =
220 g_ptr_array_index (demux->essence_tracks, i);
221
222 track->source_package = NULL;
223 track->delta_id = -1;
224 track->source_track = NULL;
225 }
226
227 demux->current_package = NULL;
228 }
229
230 static void
231 gst_mxf_demux_reset_metadata (GstMXFDemux * demux)
232 {
233 GST_DEBUG_OBJECT (demux, "Resetting metadata");
234
235 g_rw_lock_writer_lock (&demux->metadata_lock);
236
237 demux->update_metadata = TRUE;
238 demux->metadata_resolved = FALSE;
239
240 gst_mxf_demux_reset_linked_metadata (demux);
241
242 demux->preface = NULL;
243
244 if (demux->metadata) {
245 g_hash_table_destroy (demux->metadata);
246 }
247 demux->metadata = mxf_metadata_hash_table_new ();
248
249 if (demux->tags) {
250 gst_tag_list_unref (demux->tags);
251 demux->tags = NULL;
252 }
253
254 g_rw_lock_writer_unlock (&demux->metadata_lock);
255 }
256
257 static void
258 gst_mxf_demux_reset (GstMXFDemux * demux)
259 {
260 GST_DEBUG_OBJECT (demux, "cleaning up MXF demuxer");
261
262 demux->flushing = FALSE;
263
264 demux->state = GST_MXF_DEMUX_STATE_UNKNOWN;
265
266 demux->footer_partition_pack_offset = 0;
267 demux->offset = 0;
268
269 demux->pull_footer_metadata = TRUE;
270
271 demux->run_in = -1;
272
273 memset (&demux->current_package_uid, 0, sizeof (MXFUMID));
274
275 gst_segment_init (&demux->segment, GST_FORMAT_TIME);
276
277 gst_adapter_clear (demux->adapter);
278
279 gst_mxf_demux_remove_pads (demux);
280
281 if (demux->random_index_pack) {
282 g_array_free (demux->random_index_pack, TRUE);
283 demux->random_index_pack = NULL;
284 }
285
286 if (demux->pending_index_table_segments) {
287 GList *l;
288
289 for (l = demux->pending_index_table_segments; l; l = l->next) {
290 MXFIndexTableSegment *s = l->data;
291 mxf_index_table_segment_reset (s);
292 g_free (s);
293 }
294 g_list_free (demux->pending_index_table_segments);
295 demux->pending_index_table_segments = NULL;
296 }
297
298 if (demux->index_tables) {
299 GList *l;
300
301 for (l = demux->index_tables; l; l = l->next) {
302 GstMXFDemuxIndexTable *t = l->data;
303 g_array_free (t->segments, TRUE);
304 g_array_free (t->reverse_temporal_offsets, TRUE);
305 g_free (t);
306 }
307 g_list_free (demux->index_tables);
308 demux->index_tables = NULL;
309 }
310
311 demux->index_table_segments_collected = FALSE;
312
313 gst_mxf_demux_reset_mxf_state (demux);
314 gst_mxf_demux_reset_metadata (demux);
315
316 demux->have_group_id = FALSE;
317 demux->group_id = G_MAXUINT;
318 }
319
320 static GstFlowReturn
321 gst_mxf_demux_pull_range (GstMXFDemux * demux, guint64 offset,
322 guint size, GstBuffer ** buffer)
323 {
324 GstFlowReturn ret;
325
326 ret = gst_pad_pull_range (demux->sinkpad, offset, size, buffer);
327 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
328 GST_WARNING_OBJECT (demux,
329 "failed when pulling %u bytes from offset %" G_GUINT64_FORMAT ": %s",
330 size, offset, gst_flow_get_name (ret));
331 *buffer = NULL;
332 return ret;
333 }
334
335 if (G_UNLIKELY (*buffer && gst_buffer_get_size (*buffer) != size)) {
336 GST_WARNING_OBJECT (demux,
337 "partial pull got %" G_GSIZE_FORMAT " when expecting %u from offset %"
338 G_GUINT64_FORMAT, gst_buffer_get_size (*buffer), size, offset);
339 gst_buffer_unref (*buffer);
340 ret = GST_FLOW_EOS;
341 *buffer = NULL;
342 return ret;
343 }
344
345 return ret;
346 }
347
348 static gboolean
349 gst_mxf_demux_eos_single_stream (GstMXFDemux * demux, GstMXFDemuxPad * pad)
350 {
351 gboolean ret;
352 GstEvent *e;
353
354 if (pad->need_segment) {
355 GstEvent *e = gst_event_new_segment (&demux->segment);
356 GST_DEBUG_OBJECT (pad, "Sending unsent %" GST_PTR_FORMAT, e);
357 gst_event_set_seqnum (e, demux->seqnum);
358 gst_pad_push_event (GST_PAD_CAST (pad), e);
359 pad->need_segment = FALSE;
360 }
361
362 pad->eos = TRUE;
363
364 if (demux->segment.flags & GST_SEEK_FLAG_SEGMENT) {
365 GstClockTime time = gst_mxf_demux_pad_get_current_time (demux, pad);
366 GST_DEBUG_OBJECT (pad, "Segment Done for track");
367 e = gst_event_new_segment_done (GST_FORMAT_TIME, time);
368 } else {
369 GST_DEBUG_OBJECT (pad, "EOS for track");
370 e = gst_event_new_eos ();
371 }
372
373 gst_event_set_seqnum (e, demux->seqnum);
374 ret = gst_pad_push_event (GST_PAD_CAST (pad), e);
375
376 return ret;
377 }
378
379 static gboolean
380 gst_mxf_demux_push_src_event (GstMXFDemux * demux, GstEvent * event)
381 {
382 gboolean ret = TRUE;
383 guint i;
384
385 GST_DEBUG_OBJECT (demux, "Pushing '%s' event downstream",
386 GST_EVENT_TYPE_NAME (event));
387
388 for (i = 0; i < demux->src->len; i++) {
389 GstMXFDemuxPad *pad = GST_MXF_DEMUX_PAD (g_ptr_array_index (demux->src, i));
390
391 if (GST_EVENT_TYPE (event) == GST_EVENT_EOS
392 || GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT_DONE) {
393 if (pad->eos)
394 continue;
395
396 if (pad->need_segment) {
397 GstEvent *e = gst_event_new_segment (&demux->segment);
398 GST_DEBUG_OBJECT (pad, "Sending unsent %" GST_PTR_FORMAT, e);
399 gst_event_set_seqnum (e, demux->seqnum);
400 gst_pad_push_event (GST_PAD_CAST (pad), e);
401 pad->need_segment = FALSE;
402 }
403 }
404
405 ret |= gst_pad_push_event (GST_PAD_CAST (pad), gst_event_ref (event));
406 }
407
408 gst_event_unref (event);
409
410 return ret;
411 }
412
413 static GstMXFDemuxPad *
414 gst_mxf_demux_get_earliest_pad (GstMXFDemux * demux)
415 {
416 guint i;
417 GstClockTime earliest = GST_CLOCK_TIME_NONE;
418 GstMXFDemuxPad *pad = NULL;
419
420 for (i = 0; i < demux->src->len; i++) {
421 GstMXFDemuxPad *p = g_ptr_array_index (demux->src, i);
422 GstClockTime time = gst_mxf_demux_pad_get_current_time (demux, p);
423
424 if (!p->eos && time < earliest) {
425 earliest = time;
426 pad = p;
427 }
428 }
429
430 return pad;
431 }
432
433 static gint
434 gst_mxf_demux_partition_compare (GstMXFDemuxPartition * a,
435 GstMXFDemuxPartition * b)
436 {
437 if (a->partition.this_partition < b->partition.this_partition)
438 return -1;
439 else if (a->partition.this_partition > b->partition.this_partition)
440 return 1;
441 else
442 return 0;
443 }
444
445 /* Final checks and variable calculation for tracks and partition. This function
446 * can be called repeatedly without any side-effect.
447 */
448 static void
449 gst_mxf_demux_partition_postcheck (GstMXFDemux * demux,
450 GstMXFDemuxPartition * partition)
451 {
452 guint i;
453 GstMXFDemuxPartition *old_partition = demux->current_partition;
454
455 /* If we already handled this partition or it doesn't contain any essence, skip */
456 if (partition->single_track || !partition->partition.body_sid)
457 return;
458
459 for (i = 0; i < demux->essence_tracks->len; i++) {
460 GstMXFDemuxEssenceTrack *cand =
461 g_ptr_array_index (demux->essence_tracks, i);
462
463 if (cand->body_sid != partition->partition.body_sid)
464 continue;
465
466 if (!cand->source_package->is_interleaved) {
467 GST_DEBUG_OBJECT (demux,
468 "Assigning single track %u (0x%08x) to partition at offset %"
469 G_GUINT64_FORMAT, cand->track_id, cand->track_number,
470 partition->partition.this_partition);
471
472 partition->single_track = cand;
473
474 if (partition->essence_container_offset != 0
475 && cand->wrapping != MXF_ESSENCE_WRAPPING_FRAME_WRAPPING) {
476 GstMXFKLV essence_klv;
477 GstMXFDemuxIndex entry;
478 /* Update the essence container offset for the fact that the index
479 * stream offset is relative to the essence position and not to the
480 * KLV position */
481 if (gst_mxf_demux_peek_klv_packet (demux,
482 partition->partition.this_partition +
483 partition->essence_container_offset,
484 &essence_klv) == GST_FLOW_OK) {
485 partition->essence_container_offset += essence_klv.data_offset;
486 /* And keep a copy of the clip/custom klv for this partition */
487 partition->clip_klv = essence_klv;
488 GST_DEBUG_OBJECT (demux,
489 "Non-frame wrapping, updated essence_container_offset to %"
490 G_GUINT64_FORMAT, partition->essence_container_offset);
491
492 /* And match it against index table, this will also update the track delta_id (if needed) */
493 demux->current_partition = partition;
494 find_entry_for_offset (demux, cand,
495 essence_klv.offset + essence_klv.data_offset, &entry);
496 demux->current_partition = old_partition;
497 }
498 }
499
500 break;
501 }
502 }
503 }
504
505 static GstFlowReturn
506 gst_mxf_demux_handle_partition_pack (GstMXFDemux * demux, GstMXFKLV * klv)
507 {
508 MXFPartitionPack partition;
509 GList *l;
510 GstMXFDemuxPartition *p = NULL;
511 GstMapInfo map;
512 gboolean ret;
513 GstFlowReturn flowret;
514
515 GST_DEBUG_OBJECT (demux,
516 "Handling partition pack of size %" G_GSIZE_FORMAT " at offset %"
517 G_GUINT64_FORMAT, klv->length, klv->offset);
518
519 for (l = demux->partitions; l; l = l->next) {
520 GstMXFDemuxPartition *tmp = l->data;
521
522 if (tmp->partition.this_partition + demux->run_in == demux->offset &&
523 tmp->partition.major_version == 0x0001) {
524 GST_DEBUG_OBJECT (demux, "Partition already parsed");
525 p = tmp;
526 goto out;
527 }
528 }
529
530 flowret = gst_mxf_demux_fill_klv (demux, klv);
531 if (flowret != GST_FLOW_OK)
532 return flowret;
533
534 gst_buffer_map (klv->data, &map, GST_MAP_READ);
535 ret = mxf_partition_pack_parse (&klv->key, &partition, map.data, map.size);
536 gst_buffer_unmap (klv->data, &map);
537 if (!ret) {
538 GST_ERROR_OBJECT (demux, "Parsing partition pack failed");
539 return GST_FLOW_ERROR;
540 }
541
542 if (partition.this_partition != demux->offset + demux->run_in) {
543 GST_WARNING_OBJECT (demux,
544 "Partition with incorrect offset (this %" G_GUINT64_FORMAT
545 " demux offset %" G_GUINT64_FORMAT " run_in:%" G_GUINT64_FORMAT ")",
546 partition.this_partition, demux->offset, demux->run_in);
547 partition.this_partition = demux->offset + demux->run_in;
548 }
549
550 if (partition.type == MXF_PARTITION_PACK_HEADER)
551 demux->footer_partition_pack_offset = partition.footer_partition;
552
553 for (l = demux->partitions; l; l = l->next) {
554 GstMXFDemuxPartition *tmp = l->data;
555
556 if (tmp->partition.this_partition + demux->run_in == demux->offset) {
557 p = tmp;
558 break;
559 }
560 }
561
562 if (p) {
563 mxf_partition_pack_reset (&p->partition);
564 memcpy (&p->partition, &partition, sizeof (MXFPartitionPack));
565 } else {
566 p = g_new0 (GstMXFDemuxPartition, 1);
567 memcpy (&p->partition, &partition, sizeof (MXFPartitionPack));
568 demux->partitions =
569 g_list_insert_sorted (demux->partitions, p,
570 (GCompareFunc) gst_mxf_demux_partition_compare);
571 }
572
573 gst_mxf_demux_partition_postcheck (demux, p);
574
575 for (l = demux->partitions; l; l = l->next) {
576 GstMXFDemuxPartition *a, *b;
577
578 if (l->next == NULL)
579 break;
580
581 a = l->data;
582 b = l->next->data;
583
584 b->partition.prev_partition = a->partition.this_partition;
585 }
586
587 out:
588 GST_DEBUG_OBJECT (demux,
589 "Current partition now %p (body_sid:%u index_sid:%u this_partition:%"
590 G_GUINT64_FORMAT ")", p, p->partition.body_sid, p->partition.index_sid,
591 p->partition.this_partition);
592 demux->current_partition = p;
593
594 return GST_FLOW_OK;
595 }
596
597 static GstFlowReturn
598 gst_mxf_demux_handle_primer_pack (GstMXFDemux * demux, GstMXFKLV * klv)
599 {
600 GstMapInfo map;
601 gboolean ret;
602 GstFlowReturn flowret;
603
604 GST_DEBUG_OBJECT (demux,
605 "Handling primer pack of size %" G_GSIZE_FORMAT " at offset %"
606 G_GUINT64_FORMAT, klv->length, klv->offset);
607
608 if (G_UNLIKELY (!demux->current_partition)) {
609 GST_ERROR_OBJECT (demux, "Primer pack before partition pack");
610 return GST_FLOW_ERROR;
611 }
612
613 if (G_UNLIKELY (demux->current_partition->primer.mappings)) {
614 GST_DEBUG_OBJECT (demux, "Primer pack already exists");
615 return GST_FLOW_OK;
616 }
617
618 flowret = gst_mxf_demux_fill_klv (demux, klv);
619 if (flowret != GST_FLOW_OK)
620 return flowret;
621
622 gst_buffer_map (klv->data, &map, GST_MAP_READ);
623 ret = mxf_primer_pack_parse (&klv->key, &demux->current_partition->primer,
624 map.data, map.size);
625 gst_buffer_unmap (klv->data, &map);
626 if (!ret) {
627 GST_ERROR_OBJECT (demux, "Parsing primer pack failed");
628 return GST_FLOW_ERROR;
629 }
630
631 demux->current_partition->primer.offset = demux->offset;
632
633 return GST_FLOW_OK;
634 }
635
636 static GstFlowReturn
637 gst_mxf_demux_resolve_references (GstMXFDemux * demux)
638 {
639 GstFlowReturn ret = GST_FLOW_OK;
640 GHashTableIter iter;
641 MXFMetadataBase *m = NULL;
642 GstStructure *structure;
643 guint i;
644
645 g_rw_lock_writer_lock (&demux->metadata_lock);
646
647 GST_DEBUG_OBJECT (demux, "Resolve metadata references");
648 demux->update_metadata = FALSE;
649
650 if (!demux->metadata) {
651 GST_ERROR_OBJECT (demux, "No metadata yet");
652 g_rw_lock_writer_unlock (&demux->metadata_lock);
653 return GST_FLOW_ERROR;
654 }
655
656 g_hash_table_iter_init (&iter, demux->metadata);
657 while (g_hash_table_iter_next (&iter, NULL, (gpointer) & m)) {
658 m->resolved = MXF_METADATA_BASE_RESOLVE_STATE_NONE;
659 }
660
661 g_hash_table_iter_init (&iter, demux->metadata);
662 while (g_hash_table_iter_next (&iter, NULL, (gpointer) & m)) {
663 gboolean resolved;
664
665 resolved = mxf_metadata_base_resolve (m, demux->metadata);
666
667 /* Resolving can fail for anything but the preface, as the preface
668 * will resolve everything required */
669 if (!resolved && MXF_IS_METADATA_PREFACE (m)) {
670 ret = GST_FLOW_ERROR;
671 goto error;
672 }
673 }
674
675 demux->metadata_resolved = TRUE;
676
677 structure =
678 mxf_metadata_base_to_structure (MXF_METADATA_BASE (demux->preface));
679 if (!demux->tags)
680 demux->tags = gst_tag_list_new_empty ();
681
682 gst_tag_list_add (demux->tags, GST_TAG_MERGE_REPLACE, GST_TAG_MXF_STRUCTURE,
683 structure, NULL);
684
685 gst_structure_free (structure);
686
687 /* Check for quirks */
688 for (i = 0; i < demux->preface->n_identifications; i++) {
689 MXFMetadataIdentification *identification =
690 demux->preface->identifications[i];
691
692 GST_DEBUG_OBJECT (demux, "product:'%s' company:'%s'",
693 identification->product_name, identification->company_name);
694 if (!g_strcmp0 (identification->product_name, "MXFTk Advanced") &&
695 !g_strcmp0 (identification->company_name, "OpenCube") &&
696 identification->product_version.major <= 2 &&
697 identification->product_version.minor <= 0) {
698 GST_WARNING_OBJECT (demux,
699 "Setting up quirk for misuse of temporal_order field");
700 demux->temporal_order_misuse = TRUE;
701 }
702 }
703
704 g_rw_lock_writer_unlock (&demux->metadata_lock);
705
706 return ret;
707
708 error:
709 demux->metadata_resolved = FALSE;
710 g_rw_lock_writer_unlock (&demux->metadata_lock);
711
712 return ret;
713 }
714
715 static MXFMetadataGenericPackage *
716 gst_mxf_demux_find_package (GstMXFDemux * demux, const MXFUMID * umid)
717 {
718 MXFMetadataGenericPackage *ret = NULL;
719 guint i;
720
721 if (demux->preface->content_storage
722 && demux->preface->content_storage->packages) {
723 for (i = 0; i < demux->preface->content_storage->n_packages; i++) {
724 MXFMetadataGenericPackage *p =
725 demux->preface->content_storage->packages[i];
726
727 if (!p)
728 continue;
729
730 if (mxf_umid_is_equal (&p->package_uid, umid)) {
731 ret = p;
732 break;
733 }
734 }
735 }
736
737 return ret;
738 }
739
740 static MXFMetadataGenericPackage *
741 gst_mxf_demux_choose_package (GstMXFDemux * demux)
742 {
743 MXFMetadataGenericPackage *ret = NULL;
744 guint i;
745
746 if (demux->requested_package_string) {
747 MXFUMID umid = { {0,}
748 };
749
750 if (!mxf_umid_from_string (demux->requested_package_string, &umid)) {
751 GST_ERROR_OBJECT (demux, "Invalid requested package");
752 }
753 g_free (demux->requested_package_string);
754 demux->requested_package_string = NULL;
755
756 ret = gst_mxf_demux_find_package (demux, &umid);
757 }
758
759 if (!ret && !mxf_umid_is_zero (&demux->current_package_uid))
760 ret = gst_mxf_demux_find_package (demux, &demux->current_package_uid);
761
762 if (ret && (MXF_IS_METADATA_MATERIAL_PACKAGE (ret)
763 || (MXF_IS_METADATA_SOURCE_PACKAGE (ret)
764 && MXF_METADATA_SOURCE_PACKAGE (ret)->top_level)))
765 goto done;
766 else if (ret)
767 GST_WARNING_OBJECT (demux,
768 "Current package is not a material package or top-level source package, choosing the first best");
769 else if (!mxf_umid_is_zero (&demux->current_package_uid))
770 GST_WARNING_OBJECT (demux,
771 "Current package not found, choosing the first best");
772
773 ret = demux->preface->primary_package;
774 if (ret && (MXF_IS_METADATA_MATERIAL_PACKAGE (ret)
775 || (MXF_IS_METADATA_SOURCE_PACKAGE (ret)
776 && MXF_METADATA_SOURCE_PACKAGE (ret)->top_level)))
777 goto done;
778 ret = NULL;
779
780 for (i = 0; i < demux->preface->content_storage->n_packages; i++) {
781 if (demux->preface->content_storage->packages[i] &&
782 MXF_IS_METADATA_MATERIAL_PACKAGE (demux->preface->content_storage->
783 packages[i])) {
784 ret =
785 MXF_METADATA_GENERIC_PACKAGE (demux->preface->content_storage->
786 packages[i]);
787 break;
788 }
789 }
790
791 if (!ret) {
792 GST_ERROR_OBJECT (demux, "No material package");
793 return NULL;
794 }
795
796 done:
797 if (mxf_umid_is_equal (&ret->package_uid, &demux->current_package_uid)) {
798 gchar current_package_string[96];
799
800 gst_mxf_demux_remove_pads (demux);
801 memcpy (&demux->current_package_uid, &ret->package_uid, 32);
802
803 mxf_umid_to_string (&ret->package_uid, current_package_string);
804 demux->current_package_string = g_strdup (current_package_string);
805 g_object_notify (G_OBJECT (demux), "package");
806
807 if (!demux->tags)
808 demux->tags = gst_tag_list_new_empty ();
809 gst_tag_list_add (demux->tags, GST_TAG_MERGE_REPLACE, GST_TAG_MXF_UMID,
810 demux->current_package_string, NULL);
811 }
812 demux->current_package = ret;
813
814 return ret;
815 }
816
817 static GstFlowReturn
818 gst_mxf_demux_update_essence_tracks (GstMXFDemux * demux)
819 {
820 guint i, j, k;
821
822 g_return_val_if_fail (demux->preface->content_storage, GST_FLOW_ERROR);
823 g_return_val_if_fail (demux->preface->content_storage->essence_container_data,
824 GST_FLOW_ERROR);
825
826 for (i = 0; i < demux->preface->content_storage->n_essence_container_data;
827 i++) {
828 MXFMetadataEssenceContainerData *edata;
829 MXFMetadataSourcePackage *package;
830 MXFFraction common_rate = { 0, 0 };
831
832 if (demux->preface->content_storage->essence_container_data[i] == NULL)
833 continue;
834
835 edata = demux->preface->content_storage->essence_container_data[i];
836
837 if (!edata->linked_package) {
838 GST_WARNING_OBJECT (demux, "Linked package not resolved");
839 continue;
840 }
841
842 package = edata->linked_package;
843
844 if (!package->parent.tracks) {
845 GST_WARNING_OBJECT (demux, "Linked package with no resolved tracks");
846 continue;
847 }
848
849 for (j = 0; j < package->parent.n_tracks; j++) {
850 MXFMetadataTimelineTrack *track;
851 GstMXFDemuxEssenceTrack *etrack = NULL;
852 GstCaps *caps = NULL;
853 gboolean new = FALSE;
854
855 if (!package->parent.tracks[j]
856 || !MXF_IS_METADATA_TIMELINE_TRACK (package->parent.tracks[j]))
857 continue;
858
859 track = MXF_METADATA_TIMELINE_TRACK (package->parent.tracks[j]);
860 if ((track->parent.type & 0xf0) != 0x30) {
861 GST_DEBUG_OBJECT (demux,
862 "Skipping track of type 0x%02x (id:%u number:0x%08x)",
863 track->parent.type, track->parent.track_id,
864 track->parent.track_number);
865 continue;
866 }
867
868 if (track->edit_rate.n <= 0 || track->edit_rate.d <= 0) {
869 GST_WARNING_OBJECT (demux, "Invalid edit rate");
870 continue;
871 }
872
873 if (package->is_interleaved) {
874 /*
875 * S377-1:2019 "9.4.2 The MXF timing model"
876 *
877 * The value of Edit Rate shall be identical for every timeline Essence
878 * Track of the Top-Level File Package.
879 *
880 * The value of Edit Rate of the timeline Essence Tracks of one
881 * Top-Level File Package need not match the Edit Rate of the Essence
882 * Tracks of the other Top-Level File Packages.
883 *
884 * S377-1:2019 "9.5.5 Top-Level File Packages"
885 *
886 *12. All Essence Tracks of a Top-Level File Package **shall** have the
887 * same value of Edit Rate. All other Tracks of a Top-Level File
888 * Package **should** have the same value of Edit Rate as the
889 * Essence Tracks.
890 */
891 if (common_rate.n == 0 && common_rate.d == 0) {
892 common_rate = track->edit_rate;
893 } else if (common_rate.n * track->edit_rate.d !=
894 common_rate.d * track->edit_rate.n) {
895 GST_ELEMENT_ERROR (demux, STREAM, WRONG_TYPE, (NULL),
896 ("Interleaved File Package doesn't have identical edit rate on all tracks."));
897 return GST_FLOW_ERROR;
898 }
899 }
900
901 for (k = 0; k < demux->essence_tracks->len; k++) {
902 GstMXFDemuxEssenceTrack *tmp =
903 g_ptr_array_index (demux->essence_tracks, k);
904
905 if (tmp->track_number == track->parent.track_number &&
906 tmp->body_sid == edata->body_sid) {
907 if (tmp->track_id != track->parent.track_id ||
908 !mxf_umid_is_equal (&tmp->source_package_uid,
909 &package->parent.package_uid)) {
910 GST_ERROR_OBJECT (demux, "There already exists a different track "
911 "with this track number and body sid but a different source "
912 "or source track id -- ignoring");
913 continue;
914 }
915 etrack = tmp;
916 break;
917 }
918 }
919
920 if (!etrack) {
921 GstMXFDemuxEssenceTrack *tmp = g_new0 (GstMXFDemuxEssenceTrack, 1);
922
923 tmp->body_sid = edata->body_sid;
924 tmp->index_sid = edata->index_sid;
925 tmp->track_number = track->parent.track_number;
926 tmp->track_id = track->parent.track_id;
927 memcpy (&tmp->source_package_uid, &package->parent.package_uid, 32);
928
929 if (demux->current_partition->partition.body_sid == edata->body_sid &&
930 demux->current_partition->partition.body_offset == 0)
931 tmp->position = 0;
932 else
933 tmp->position = -1;
934
935 g_ptr_array_add (demux->essence_tracks, tmp);
936 etrack =
937 g_ptr_array_index (demux->essence_tracks,
938 demux->essence_tracks->len - 1);
939 new = TRUE;
940 }
941
942 etrack->source_package = NULL;
943 etrack->source_track = NULL;
944 etrack->delta_id = -1;
945
946 if (!track->parent.sequence) {
947 GST_WARNING_OBJECT (demux, "Source track has no sequence");
948 goto next;
949 }
950
951 if (track->parent.n_descriptor == 0) {
952 GST_WARNING_OBJECT (demux, "Source track has no descriptors");
953 goto next;
954 }
955
956 if (track->parent.sequence->duration > etrack->duration)
957 etrack->duration = track->parent.sequence->duration;
958
959 g_free (etrack->mapping_data);
960 etrack->mapping_data = NULL;
961 etrack->handler = NULL;
962 etrack->handle_func = NULL;
963 if (etrack->tags)
964 gst_tag_list_unref (etrack->tags);
965 etrack->tags = NULL;
966
967 etrack->handler = mxf_essence_element_handler_find (track);
968 if (!etrack->handler) {
969 gchar essence_container[48];
970 gchar essence_compression[48];
971 gchar *name;
972
973 GST_WARNING_OBJECT (demux,
974 "No essence element handler for track %u found", i);
975
976 mxf_ul_to_string (&track->parent.descriptor[0]->essence_container,
977 essence_container);
978
979 if (track->parent.type == MXF_METADATA_TRACK_PICTURE_ESSENCE) {
980 if (MXF_IS_METADATA_GENERIC_PICTURE_ESSENCE_DESCRIPTOR (track->parent.
981 descriptor[0]))
982 mxf_ul_to_string (&MXF_METADATA_GENERIC_PICTURE_ESSENCE_DESCRIPTOR
983 (track->parent.descriptor[0])->picture_essence_coding,
984 essence_compression);
985
986 name =
987 g_strdup_printf ("video/x-mxf-%s-%s", essence_container,
988 essence_compression);
989 } else if (track->parent.type == MXF_METADATA_TRACK_SOUND_ESSENCE) {
990 if (MXF_IS_METADATA_GENERIC_SOUND_ESSENCE_DESCRIPTOR (track->parent.
991 descriptor[0]))
992 mxf_ul_to_string (&MXF_METADATA_GENERIC_SOUND_ESSENCE_DESCRIPTOR
993 (track->parent.descriptor[0])->sound_essence_compression,
994 essence_compression);
995
996 name =
997 g_strdup_printf ("audio/x-mxf-%s-%s", essence_container,
998 essence_compression);
999 } else if (track->parent.type == MXF_METADATA_TRACK_DATA_ESSENCE) {
1000 if (MXF_IS_METADATA_GENERIC_DATA_ESSENCE_DESCRIPTOR (track->parent.
1001 descriptor[0]))
1002 mxf_ul_to_string (&MXF_METADATA_GENERIC_DATA_ESSENCE_DESCRIPTOR
1003 (track->parent.descriptor[0])->data_essence_coding,
1004 essence_compression);
1005
1006 name =
1007 g_strdup_printf ("application/x-mxf-%s-%s", essence_container,
1008 essence_compression);
1009 } else {
1010 name = NULL;
1011 g_assert_not_reached ();
1012 }
1013
1014 caps = gst_caps_new_empty_simple (name);
1015 g_free (name);
1016 etrack->intra_only = FALSE;
1017 } else {
1018 caps =
1019 etrack->handler->create_caps (track, &etrack->tags,
1020 &etrack->intra_only, &etrack->handle_func, &etrack->mapping_data);
1021 }
1022
1023 GST_DEBUG_OBJECT (demux, "Created caps %" GST_PTR_FORMAT, caps);
1024
1025 if (!caps && new) {
1026 GST_WARNING_OBJECT (demux, "No caps created, ignoring stream");
1027 g_free (etrack->mapping_data);
1028 etrack->mapping_data = NULL;
1029 if (etrack->tags)
1030 gst_tag_list_unref (etrack->tags);
1031 etrack->tags = NULL;
1032 goto next;
1033 } else if (!caps) {
1034 GST_WARNING_OBJECT (demux, "Couldn't create updated caps for stream");
1035 } else if (!etrack->caps || !gst_caps_is_equal (etrack->caps, caps)) {
1036 if (etrack->caps)
1037 gst_caps_unref (etrack->caps);
1038 etrack->caps = caps;
1039 } else {
1040 gst_caps_unref (caps);
1041 caps = NULL;
1042 }
1043
1044 etrack->min_edit_units = 1;
1045 /* Ensure we don't output one buffer per sample for audio */
1046 if (gst_util_uint64_scale (GST_SECOND, track->edit_rate.d,
1047 track->edit_rate.n) < 10 * GST_MSECOND) {
1048 GstStructure *s = gst_caps_get_structure (etrack->caps, 0);
1049 const gchar *name = gst_structure_get_name (s);
1050 if (g_str_has_prefix (name, "audio/x-raw")) {
1051 etrack->min_edit_units =
1052 gst_util_uint64_scale (25 * GST_MSECOND, track->edit_rate.n,
1053 track->edit_rate.d * GST_SECOND);
1054 GST_DEBUG_OBJECT (demux, "Seting miminum number of edit units to %u",
1055 etrack->min_edit_units);
1056 }
1057 }
1058
1059 /* FIXME : We really should just abort/ignore the stream completely if we
1060 * don't have a handler for it */
1061 if (etrack->handler != NULL)
1062 etrack->wrapping = etrack->handler->get_track_wrapping (track);
1063 else
1064 etrack->wrapping = MXF_ESSENCE_WRAPPING_UNKNOWN_WRAPPING;
1065
1066 if (package->is_interleaved) {
1067 GST_DEBUG_OBJECT (demux,
1068 "track comes from interleaved source package with %u track(s), setting delta_id to -1",
1069 package->parent.n_tracks);
1070 if (etrack->wrapping != MXF_ESSENCE_WRAPPING_FRAME_WRAPPING) {
1071 GST_ELEMENT_ERROR (demux, STREAM, WRONG_TYPE, (NULL),
1072 ("Non-frame-wrapping is not allowed in interleaved File Package."));
1073 return GST_FLOW_ERROR;
1074 }
1075 etrack->delta_id = MXF_INDEX_DELTA_ID_UNKNOWN;
1076 } else {
1077 etrack->delta_id = MXF_INDEX_DELTA_ID_UNKNOWN;
1078 }
1079 etrack->source_package = package;
1080 etrack->source_track = track;
1081 continue;
1082
1083 next:
1084 if (new) {
1085 g_ptr_array_remove_index (demux->essence_tracks,
1086 demux->essence_tracks->len - 1);
1087 }
1088 }
1089 }
1090
1091 if (demux->essence_tracks->len == 0) {
1092 GST_ERROR_OBJECT (demux, "No valid essence tracks in this file");
1093 return GST_FLOW_ERROR;
1094 }
1095
1096 for (i = 0; i < demux->essence_tracks->len; i++) {
1097 GstMXFDemuxEssenceTrack *etrack =
1098 g_ptr_array_index (demux->essence_tracks, i);
1099
1100 if (!etrack->source_package || !etrack->source_track || !etrack->caps) {
1101 GST_ERROR_OBJECT (demux, "Failed to update essence track %u", i);
1102 return GST_FLOW_ERROR;
1103 }
1104
1105 }
1106
1107 return GST_FLOW_OK;
1108 }
1109
1110 static MXFMetadataEssenceContainerData *
1111 essence_container_for_source_package (MXFMetadataContentStorage * storage,
1112 MXFMetadataSourcePackage * package)
1113 {
1114 guint i;
1115
1116 for (i = 0; i < storage->n_essence_container_data; i++) {
1117 MXFMetadataEssenceContainerData *cont = storage->essence_container_data[i];
1118 if (cont && cont->linked_package == package)
1119 return cont;
1120 }
1121
1122 return NULL;
1123 }
1124
1125 static void
1126 gst_mxf_demux_show_topology (GstMXFDemux * demux)
1127 {
1128 GList *material_packages = NULL;
1129 GList *file_packages = NULL;
1130 GList *tmp;
1131 MXFMetadataContentStorage *storage = demux->preface->content_storage;
1132 guint i;
1133 gchar str[96];
1134
1135 /* Show the topology starting from the preface */
1136 GST_DEBUG_OBJECT (demux, "Topology");
1137
1138 for (i = 0; i < storage->n_packages; i++) {
1139 MXFMetadataGenericPackage *pack = storage->packages[i];
1140 if (MXF_IS_METADATA_MATERIAL_PACKAGE (pack))
1141 material_packages = g_list_append (material_packages, pack);
1142 else if (MXF_IS_METADATA_SOURCE_PACKAGE (pack))
1143 file_packages = g_list_append (file_packages, pack);
1144 else
1145 GST_DEBUG_OBJECT (demux, "Unknown package type");
1146 }
1147
1148 GST_DEBUG_OBJECT (demux, "Number of Material Package (i.e. output) : %u",
1149 g_list_length (material_packages));
1150 for (tmp = material_packages; tmp; tmp = tmp->next) {
1151 MXFMetadataMaterialPackage *pack = (MXFMetadataMaterialPackage *) tmp->data;
1152 GST_DEBUG_OBJECT (demux, " Package with %u tracks , UID:%s",
1153 pack->n_tracks, mxf_umid_to_string (&pack->package_uid, str));
1154 for (i = 0; i < pack->n_tracks; i++) {
1155 MXFMetadataTrack *track = pack->tracks[i];
1156 if (track == NULL) {
1157 GST_DEBUG_OBJECT (demux, " Unknown/Unhandled track UUID %s",
1158 mxf_uuid_to_string (&pack->tracks_uids[i], str));
1159 } else if (MXF_IS_METADATA_TIMELINE_TRACK (track)) {
1160 MXFMetadataTimelineTrack *mtrack = (MXFMetadataTimelineTrack *) track;
1161 GST_DEBUG_OBJECT (demux,
1162 " Timeline Track id:%u number:0x%08x name:`%s` edit_rate:%d/%d origin:%"
1163 G_GUINT64_FORMAT, track->track_id, track->track_number,
1164 track->track_name, mtrack->edit_rate.n, mtrack->edit_rate.d,
1165 mtrack->origin);
1166 } else {
1167 GST_DEBUG_OBJECT (demux,
1168 " Non-Timeline-Track id:%u number:0x%08x name:`%s`",
1169 track->track_id, track->track_number, track->track_name);
1170 }
1171 if (track) {
1172 MXFMetadataSequence *sequence = track->sequence;
1173 guint si;
1174 GST_DEBUG_OBJECT (demux,
1175 " Sequence duration:%" G_GUINT64_FORMAT
1176 " n_structural_components:%u", sequence->duration,
1177 sequence->n_structural_components);
1178 for (si = 0; si < sequence->n_structural_components; si++) {
1179 MXFMetadataStructuralComponent *comp =
1180 sequence->structural_components[si];
1181 GST_DEBUG_OBJECT (demux,
1182 " Component #%u duration:%" G_GUINT64_FORMAT, si,
1183 comp->duration);
1184 if (MXF_IS_METADATA_SOURCE_CLIP (comp)) {
1185 MXFMetadataSourceClip *clip = (MXFMetadataSourceClip *) comp;
1186 GST_DEBUG_OBJECT (demux,
1187 " Clip start_position:%" G_GUINT64_FORMAT
1188 " source_track_id:%u source_package_id:%s",
1189 clip->start_position, clip->source_track_id,
1190 mxf_umid_to_string (&clip->source_package_id, str));
1191 }
1192 }
1193
1194 }
1195 }
1196 }
1197
1198 GST_DEBUG_OBJECT (demux, "Number of File Packages (i.e. input) : %u",
1199 g_list_length (file_packages));
1200 for (tmp = file_packages; tmp; tmp = tmp->next) {
1201 MXFMetadataMaterialPackage *pack = (MXFMetadataMaterialPackage *) tmp->data;
1202 MXFMetadataSourcePackage *src = (MXFMetadataSourcePackage *) pack;
1203 #ifndef GST_DISABLE_GST_DEBUG
1204 MXFMetadataEssenceContainerData *econt =
1205 essence_container_for_source_package (storage, src);
1206 GST_DEBUG_OBJECT (demux,
1207 " Package (body_sid:%u index_sid:%u top_level:%u) with %u tracks , UID:%s",
1208 econt ? econt->body_sid : 0, econt ? econt->index_sid : 0,
1209 src->top_level, pack->n_tracks, mxf_umid_to_string (&pack->package_uid,
1210 str));
1211 #endif
1212 GST_DEBUG_OBJECT (demux, " Package descriptor : %s",
1213 g_type_name (G_OBJECT_TYPE (src->descriptor)));
1214 for (i = 0; i < pack->n_tracks; i++) {
1215 MXFMetadataTrack *track = pack->tracks[i];
1216 if (!track)
1217 continue;
1218 MXFMetadataSequence *sequence = track->sequence;
1219 guint di, si;
1220 if (MXF_IS_METADATA_TIMELINE_TRACK (track)) {
1221 MXFMetadataTimelineTrack *mtrack = (MXFMetadataTimelineTrack *) track;
1222 GST_DEBUG_OBJECT (demux,
1223 " Timeline Track id:%u number:0x%08x name:`%s` edit_rate:%d/%d origin:%"
1224 G_GUINT64_FORMAT, track->track_id, track->track_number,
1225 track->track_name, mtrack->edit_rate.n, mtrack->edit_rate.d,
1226 mtrack->origin);
1227 } else {
1228 GST_DEBUG_OBJECT (demux,
1229 " Non-Timeline-Track id:%u number:0x%08x name:`%s` type:0x%x",
1230 track->track_id, track->track_number, track->track_name,
1231 track->type);
1232 }
1233 for (di = 0; di < track->n_descriptor; di++) {
1234 MXFMetadataFileDescriptor *desc = track->descriptor[di];
1235 MXFMetadataGenericDescriptor *generic =
1236 (MXFMetadataGenericDescriptor *) desc;
1237 guint subdi;
1238
1239 GST_DEBUG_OBJECT (demux, " Descriptor %s %s",
1240 g_type_name (G_OBJECT_TYPE (desc)),
1241 mxf_ul_to_string (&desc->essence_container, str));
1242 for (subdi = 0; subdi < generic->n_sub_descriptors; subdi++) {
1243 MXFMetadataGenericDescriptor *subdesc =
1244 generic->sub_descriptors[subdi];
1245 if (subdesc) {
1246 GST_DEBUG_OBJECT (demux, " Sub-Descriptor %s",
1247 g_type_name (G_OBJECT_TYPE (subdesc)));
1248 }
1249 }
1250 }
1251 GST_DEBUG_OBJECT (demux,
1252 " Sequence duration:%" G_GUINT64_FORMAT
1253 " n_structural_components:%u", sequence->duration,
1254 sequence->n_structural_components);
1255 for (si = 0; si < sequence->n_structural_components; si++) {
1256 MXFMetadataStructuralComponent *comp =
1257 sequence->structural_components[si];
1258 GST_DEBUG_OBJECT (demux,
1259 " Component #%u duration:%" G_GUINT64_FORMAT, si,
1260 comp->duration);
1261 }
1262 }
1263 }
1264
1265 g_list_free (material_packages);
1266 g_list_free (file_packages);
1267 }
1268
1269 static GstFlowReturn
1270 gst_mxf_demux_update_tracks (GstMXFDemux * demux)
1271 {
1272 MXFMetadataGenericPackage *current_package = NULL;
1273 guint i, j, k;
1274 gboolean first_run;
1275 guint component_index;
1276 GstFlowReturn ret;
1277 GList *pads = NULL, *l;
1278 GstVideoTimeCode start_timecode = GST_VIDEO_TIME_CODE_INIT;
1279
1280 g_rw_lock_writer_lock (&demux->metadata_lock);
1281 GST_DEBUG_OBJECT (demux, "Updating tracks");
1282
1283 gst_mxf_demux_show_topology (demux);
1284
1285 if ((ret = gst_mxf_demux_update_essence_tracks (demux)) != GST_FLOW_OK) {
1286 goto error;
1287 }
1288
1289 current_package = gst_mxf_demux_choose_package (demux);
1290
1291 if (!current_package) {
1292 GST_ERROR_OBJECT (demux, "Unable to find current package");
1293 ret = GST_FLOW_ERROR;
1294 goto error;
1295 } else if (!current_package->tracks) {
1296 GST_ERROR_OBJECT (demux, "Current package has no (resolved) tracks");
1297 ret = GST_FLOW_ERROR;
1298 goto error;
1299 } else if (!current_package->n_essence_tracks) {
1300 GST_ERROR_OBJECT (demux, "Current package has no essence tracks");
1301 ret = GST_FLOW_ERROR;
1302 goto error;
1303 }
1304
1305 first_run = (demux->src->len == 0);
1306
1307 /* For material packages, there must be one timecode track with one
1308 * continuous timecode. For source packages there might be multiple,
1309 * discontinuous timecode components.
1310 * TODO: Support multiple timecode components
1311 */
1312 for (i = 0; i < current_package->n_tracks; i++) {
1313 MXFMetadataTimelineTrack *track = NULL;
1314 MXFMetadataSequence *sequence = NULL;
1315 MXFMetadataTimecodeComponent *component = NULL;
1316
1317 if (!current_package->tracks[i]) {
1318 GST_WARNING_OBJECT (demux, "Unresolved track");
1319 continue;
1320 }
1321
1322 if (!MXF_IS_METADATA_TIMELINE_TRACK (current_package->tracks[i])) {
1323 GST_DEBUG_OBJECT (demux, "Skipping Non-timeline track");
1324 continue;
1325 }
1326
1327
1328 track = MXF_METADATA_TIMELINE_TRACK (current_package->tracks[i]);
1329
1330 if (!track->parent.sequence)
1331 continue;
1332 sequence = track->parent.sequence;
1333 if (sequence->n_structural_components != 1 ||
1334 !sequence->structural_components[0]
1335 ||
1336 !MXF_IS_METADATA_TIMECODE_COMPONENT (sequence->structural_components
1337 [0]))
1338 continue;
1339
1340 component =
1341 MXF_METADATA_TIMECODE_COMPONENT (sequence->structural_components[0]);
1342
1343 /* Not a timecode track */
1344 if (track->parent.type && (track->parent.type & 0xf0) != 0x10)
1345 continue;
1346
1347 /* Main timecode track must have id 1, all others must be 0 */
1348 if (track->parent.track_id != 1)
1349 continue;
1350
1351 gst_video_time_code_init (&start_timecode, track->edit_rate.n,
1352 track->edit_rate.d, NULL, (component->drop_frame
1353 ?
1354 GST_VIDEO_TIME_CODE_FLAGS_DROP_FRAME
1355 : GST_VIDEO_TIME_CODE_FLAGS_NONE), 0, 0, 0, 0, 0);
1356 gst_video_time_code_add_frames (&start_timecode, track->origin);
1357 gst_video_time_code_add_frames (&start_timecode, component->start_timecode);
1358 break;
1359 }
1360
1361 for (i = 0; i < current_package->n_tracks; i++) {
1362 MXFMetadataTimelineTrack *track = NULL;
1363 MXFMetadataSequence *sequence;
1364 MXFMetadataSourceClip *component = NULL;
1365 MXFMetadataSourcePackage *source_package = NULL;
1366 MXFMetadataTimelineTrack *source_track = NULL;
1367 GstMXFDemuxEssenceTrack *etrack = NULL;
1368 GstMXFDemuxPad *pad = NULL;
1369 GstCaps *pad_caps;
1370
1371 GST_DEBUG_OBJECT (demux, "Handling track %u", i);
1372
1373 if (!current_package->tracks[i]) {
1374 GST_WARNING_OBJECT (demux, "Unresolved track");
1375 continue;
1376 }
1377
1378 if (!MXF_IS_METADATA_TIMELINE_TRACK (current_package->tracks[i])) {
1379 GST_DEBUG_OBJECT (demux, "No timeline track");
1380 continue;
1381 }
1382
1383 track = MXF_METADATA_TIMELINE_TRACK (current_package->tracks[i]);
1384
1385 if (!first_run) {
1386 /* Find pad from track_id */
1387 for (j = 0; j < demux->src->len; j++) {
1388 GstMXFDemuxPad *tmp = g_ptr_array_index (demux->src, j);
1389
1390 if (tmp->track_id == track->parent.track_id) {
1391 pad = tmp;
1392 break;
1393 }
1394 }
1395 }
1396
1397 if (pad)
1398 component_index = pad->current_component_index;
1399 else
1400 component_index = 0;
1401
1402 if (!track->parent.sequence) {
1403 GST_WARNING_OBJECT (demux, "Track with no sequence");
1404 if (!pad) {
1405 continue;
1406 } else {
1407 ret = GST_FLOW_ERROR;
1408 goto error;
1409 }
1410 }
1411
1412 sequence = track->parent.sequence;
1413
1414 if (MXF_IS_METADATA_SOURCE_PACKAGE (current_package)) {
1415 GST_DEBUG_OBJECT (demux, "Playing source package");
1416
1417 component = NULL;
1418 source_package = MXF_METADATA_SOURCE_PACKAGE (current_package);
1419 source_track = track;
1420 } else if (sequence->structural_components
1421 &&
1422 MXF_IS_METADATA_SOURCE_CLIP (sequence->structural_components
1423 [component_index])) {
1424 GST_DEBUG_OBJECT (demux, "Playing material package");
1425
1426 component =
1427 MXF_METADATA_SOURCE_CLIP (sequence->structural_components
1428 [component_index]);
1429 if (!component) {
1430 GST_WARNING_OBJECT (demux, "NULL component in non-source package");
1431 if (!pad) {
1432 continue;
1433 } else {
1434 ret = GST_FLOW_ERROR;
1435 goto error;
1436 }
1437 }
1438
1439 if (component->source_package && component->source_package->top_level &&
1440 MXF_METADATA_GENERIC_PACKAGE (component->source_package)->tracks) {
1441 MXFMetadataGenericPackage *tmp_pkg =
1442 MXF_METADATA_GENERIC_PACKAGE (component->source_package);
1443
1444 source_package = component->source_package;
1445
1446 for (k = 0; k < tmp_pkg->n_tracks; k++) {
1447 MXFMetadataTrack *tmp = tmp_pkg->tracks[k];
1448
1449 if (tmp->track_id == component->source_track_id) {
1450 source_track = MXF_METADATA_TIMELINE_TRACK (tmp);
1451 break;
1452 }
1453 }
1454 }
1455 }
1456
1457 if (track->parent.type && (track->parent.type & 0xf0) != 0x30) {
1458 GST_DEBUG_OBJECT (demux,
1459 "No essence track. type:0x%02x track_id:%u track_number:0x%08x",
1460 track->parent.type, track->parent.track_id,
1461 track->parent.track_number);
1462 if (!pad) {
1463 continue;
1464 } else {
1465 ret = GST_FLOW_ERROR;
1466 goto error;
1467 }
1468 }
1469
1470 if (!source_package || track->parent.type == MXF_METADATA_TRACK_UNKNOWN
1471 || !source_track) {
1472 GST_WARNING_OBJECT (demux,
1473 "No source package or track type for track found");
1474 if (!pad) {
1475 continue;
1476 } else {
1477 ret = GST_FLOW_ERROR;
1478 goto error;
1479 }
1480 }
1481
1482 for (k = 0; k < demux->essence_tracks->len; k++) {
1483 GstMXFDemuxEssenceTrack *tmp =
1484 g_ptr_array_index (demux->essence_tracks, k);
1485
1486 if (tmp->source_package == source_package &&
1487 tmp->source_track == source_track) {
1488 etrack = tmp;
1489 break;
1490 }
1491 }
1492
1493 if (!etrack) {
1494 GST_WARNING_OBJECT (demux, "No essence track for this track found");
1495 if (!pad) {
1496 continue;
1497 } else {
1498 ret = GST_FLOW_ERROR;
1499 goto error;
1500 }
1501 }
1502
1503 if (track->edit_rate.n <= 0 || track->edit_rate.d <= 0 ||
1504 source_track->edit_rate.n <= 0 || source_track->edit_rate.d <= 0) {
1505 GST_WARNING_OBJECT (demux, "Track has an invalid edit rate");
1506 if (!pad) {
1507 continue;
1508 } else {
1509 ret = GST_FLOW_ERROR;
1510 goto error;
1511 }
1512 }
1513
1514 if (MXF_IS_METADATA_MATERIAL_PACKAGE (current_package) && !component) {
1515 GST_WARNING_OBJECT (demux,
1516 "Playing material package but found no component for track");
1517 if (!pad) {
1518 continue;
1519 } else {
1520 ret = GST_FLOW_ERROR;
1521 goto error;
1522 }
1523 }
1524
1525 if (!source_package->descriptor) {
1526 GST_WARNING_OBJECT (demux, "Source package has no descriptors");
1527 if (!pad) {
1528 continue;
1529 } else {
1530 ret = GST_FLOW_ERROR;
1531 goto error;
1532 }
1533 }
1534
1535 if (!source_track->parent.descriptor) {
1536 GST_WARNING_OBJECT (demux, "No descriptor found for track");
1537 if (!pad) {
1538 continue;
1539 } else {
1540 ret = GST_FLOW_ERROR;
1541 goto error;
1542 }
1543 }
1544
1545 if (!pad && first_run) {
1546 GstPadTemplate *templ;
1547 gchar *pad_name;
1548
1549 templ =
1550 gst_element_class_get_pad_template (GST_ELEMENT_GET_CLASS (demux),
1551 "track_%u");
1552 pad_name = g_strdup_printf ("track_%u", track->parent.track_id);
1553
1554 g_assert (templ != NULL);
1555
1556 /* Create pad */
1557 pad = (GstMXFDemuxPad *) g_object_new (GST_TYPE_MXF_DEMUX_PAD,
1558 "name", pad_name, "direction", GST_PAD_SRC, "template", templ, NULL);
1559 pad->need_segment = TRUE;
1560 pad->eos = FALSE;
1561 g_free (pad_name);
1562
1563 if (demux->tags)
1564 pad->tags = gst_tag_list_copy (demux->tags);
1565 }
1566
1567 if (!pad) {
1568 GST_WARNING_OBJECT (demux,
1569 "Not the first pad addition run, ignoring new track");
1570 continue;
1571 }
1572
1573 /* Update pad */
1574 pad->track_id = track->parent.track_id;
1575
1576 pad->material_package = current_package;
1577 pad->material_track = track;
1578
1579 pad->start_timecode = start_timecode;
1580
1581 /* If we just added the pad initialize for the current component */
1582 if (first_run && MXF_IS_METADATA_MATERIAL_PACKAGE (current_package)) {
1583 pad->current_component_index = 0;
1584 pad->current_component_start = source_track->origin;
1585 pad->current_component_start_position = 0;
1586
1587 pad->current_component_duration = component->parent.duration;
1588
1589 if (track->edit_rate.n != source_track->edit_rate.n ||
1590 track->edit_rate.d != source_track->edit_rate.d) {
1591 pad->current_component_start +=
1592 gst_util_uint64_scale (component->start_position,
1593 source_track->edit_rate.n * track->edit_rate.d,
1594 source_track->edit_rate.d * track->edit_rate.n);
1595
1596 if (pad->current_component_duration != -1)
1597 pad->current_component_duration =
1598 gst_util_uint64_scale (pad->current_component_duration,
1599 source_track->edit_rate.n * track->edit_rate.d,
1600 source_track->edit_rate.d * track->edit_rate.n);
1601 } else {
1602 pad->current_component_start += component->start_position;
1603 }
1604 pad->current_essence_track_position = pad->current_component_start;
1605 }
1606
1607 /* NULL iff playing a source package */
1608 pad->current_component = component;
1609
1610 pad->current_essence_track = etrack;
1611
1612 if (etrack->tags) {
1613 if (pad->tags)
1614 gst_tag_list_insert (pad->tags, etrack->tags, GST_TAG_MERGE_REPLACE);
1615 else
1616 pad->tags = gst_tag_list_copy (etrack->tags);
1617 }
1618
1619 pad_caps = gst_pad_get_current_caps (GST_PAD_CAST (pad));
1620 if (pad_caps && !gst_caps_is_equal (pad_caps, etrack->caps)) {
1621 gst_pad_set_caps (GST_PAD_CAST (pad), etrack->caps);
1622 } else if (!pad_caps) {
1623 GstEvent *event;
1624 gchar *stream_id;
1625
1626 gst_pad_set_event_function (GST_PAD_CAST (pad),
1627 GST_DEBUG_FUNCPTR (gst_mxf_demux_src_event));
1628
1629 gst_pad_set_query_function (GST_PAD_CAST (pad),
1630 GST_DEBUG_FUNCPTR (gst_mxf_demux_src_query));
1631
1632 gst_pad_use_fixed_caps (GST_PAD_CAST (pad));
1633 gst_pad_set_active (GST_PAD_CAST (pad), TRUE);
1634
1635 stream_id =
1636 gst_pad_create_stream_id_printf (GST_PAD_CAST (pad),
1637 GST_ELEMENT_CAST (demux), "%03u", pad->track_id);
1638
1639 event =
1640 gst_pad_get_sticky_event (demux->sinkpad, GST_EVENT_STREAM_START, 0);
1641 if (event) {
1642 if (gst_event_parse_group_id (event, &demux->group_id))
1643 demux->have_group_id = TRUE;
1644 else
1645 demux->have_group_id = FALSE;
1646 gst_event_unref (event);
1647 } else if (!demux->have_group_id) {
1648 demux->have_group_id = TRUE;
1649 demux->group_id = gst_util_group_id_next ();
1650 }
1651 event = gst_event_new_stream_start (stream_id);
1652 if (demux->have_group_id)
1653 gst_event_set_group_id (event, demux->group_id);
1654
1655 gst_pad_push_event (GST_PAD_CAST (pad), event);
1656 g_free (stream_id);
1657
1658 gst_pad_set_caps (GST_PAD_CAST (pad), etrack->caps);
1659
1660 pads = g_list_prepend (pads, gst_object_ref (pad));
1661
1662 g_ptr_array_add (demux->src, pad);
1663 pad->discont = TRUE;
1664 }
1665 if (pad_caps)
1666 gst_caps_unref (pad_caps);
1667 }
1668
1669 if (demux->src->len > 0) {
1670 for (i = 0; i < demux->src->len; i++) {
1671 GstMXFDemuxPad *pad = g_ptr_array_index (demux->src, i);
1672
1673 if (!pad->material_track || !pad->material_package) {
1674 GST_ERROR_OBJECT (demux, "Unable to update existing pad");
1675 ret = GST_FLOW_ERROR;
1676 goto error;
1677 }
1678 }
1679 } else {
1680 GST_ERROR_OBJECT (demux, "Couldn't create any streams");
1681 ret = GST_FLOW_ERROR;
1682 goto error;
1683 }
1684
1685 g_rw_lock_writer_unlock (&demux->metadata_lock);
1686
1687 for (l = pads; l; l = l->next) {
1688 gst_flow_combiner_add_pad (demux->flowcombiner, l->data);
1689 gst_element_add_pad (GST_ELEMENT_CAST (demux), l->data);
1690 }
1691 g_list_free (pads);
1692
1693 if (first_run)
1694 gst_element_no_more_pads (GST_ELEMENT_CAST (demux));
1695
1696 /* Re-check all existing partitions for source package linking in case the
1697 * header partition contains data (allowed in early MXF versions) */
1698 for (l = demux->partitions; l; l = l->next)
1699 gst_mxf_demux_partition_postcheck (demux, (GstMXFDemuxPartition *) l->data);
1700
1701 return GST_FLOW_OK;
1702
1703 error:
1704 g_rw_lock_writer_unlock (&demux->metadata_lock);
1705 return ret;
1706 }
1707
1708 static GstFlowReturn
1709 gst_mxf_demux_handle_metadata (GstMXFDemux * demux, GstMXFKLV * klv)
1710 {
1711 guint16 type;
1712 MXFMetadata *metadata = NULL, *old = NULL;
1713 GstMapInfo map;
1714 GstFlowReturn ret = GST_FLOW_OK;
1715
1716 type = GST_READ_UINT16_BE (&klv->key.u[13]);
1717
1718 GST_DEBUG_OBJECT (demux,
1719 "Handling metadata of size %" G_GSIZE_FORMAT " at offset %"
1720 G_GUINT64_FORMAT " of type 0x%04x", klv->length, klv->offset, type);
1721
1722 if (G_UNLIKELY (!demux->current_partition)) {
1723 GST_ERROR_OBJECT (demux, "Partition pack doesn't exist");
1724 return GST_FLOW_ERROR;
1725 }
1726
1727 if (G_UNLIKELY (!demux->current_partition->primer.mappings)) {
1728 GST_ERROR_OBJECT (demux, "Primer pack doesn't exists");
1729 return GST_FLOW_ERROR;
1730 }
1731
1732 if (demux->current_partition->parsed_metadata) {
1733 GST_DEBUG_OBJECT (demux, "Metadata of this partition was already parsed");
1734 return GST_FLOW_OK;
1735 }
1736
1737 if (klv->length == 0)
1738 return GST_FLOW_OK;
1739 ret = gst_mxf_demux_fill_klv (demux, klv);
1740 if (ret != GST_FLOW_OK)
1741 return ret;
1742
1743 gst_buffer_map (klv->data, &map, GST_MAP_READ);
1744 metadata =
1745 mxf_metadata_new (type, &demux->current_partition->primer, demux->offset,
1746 map.data, map.size);
1747 gst_buffer_unmap (klv->data, &map);
1748
1749 if (!metadata) {
1750 GST_WARNING_OBJECT (demux,
1751 "Unknown or unhandled metadata of type 0x%04x", type);
1752 return GST_FLOW_OK;
1753 }
1754
1755 old =
1756 g_hash_table_lookup (demux->metadata,
1757 &MXF_METADATA_BASE (metadata)->instance_uid);
1758
1759 if (old && G_TYPE_FROM_INSTANCE (old) != G_TYPE_FROM_INSTANCE (metadata)) {
1760 #ifndef GST_DISABLE_GST_DEBUG
1761 gchar str[48];
1762 #endif
1763
1764 GST_DEBUG_OBJECT (demux,
1765 "Metadata with instance uid %s already exists and has different type '%s',"
1766 " expected '%s'",
1767 mxf_uuid_to_string (&MXF_METADATA_BASE (metadata)->instance_uid, str),
1768 g_type_name (G_TYPE_FROM_INSTANCE (old)),
1769 g_type_name (G_TYPE_FROM_INSTANCE (metadata)));
1770 g_object_unref (metadata);
1771 return GST_FLOW_ERROR;
1772 } else if (old
1773 && MXF_METADATA_BASE (old)->offset >=
1774 MXF_METADATA_BASE (metadata)->offset) {
1775 #ifndef GST_DISABLE_GST_DEBUG
1776 gchar str[48];
1777 #endif
1778
1779 GST_DEBUG_OBJECT (demux,
1780 "Metadata with instance uid %s already exists and is newer",
1781 mxf_uuid_to_string (&MXF_METADATA_BASE (metadata)->instance_uid, str));
1782 g_object_unref (metadata);
1783 return GST_FLOW_OK;
1784 }
1785
1786 g_rw_lock_writer_lock (&demux->metadata_lock);
1787 demux->update_metadata = TRUE;
1788
1789 if (MXF_IS_METADATA_PREFACE (metadata)) {
1790 demux->preface = MXF_METADATA_PREFACE (metadata);
1791 }
1792
1793 gst_mxf_demux_reset_linked_metadata (demux);
1794
1795 g_hash_table_replace (demux->metadata,
1796 &MXF_METADATA_BASE (metadata)->instance_uid, metadata);
1797 g_rw_lock_writer_unlock (&demux->metadata_lock);
1798
1799 return ret;
1800 }
1801
1802 static GstFlowReturn
1803 gst_mxf_demux_handle_descriptive_metadata (GstMXFDemux * demux, GstMXFKLV * klv)
1804 {
1805 guint32 type;
1806 guint8 scheme;
1807 GstMapInfo map;
1808 GstFlowReturn ret = GST_FLOW_OK;
1809 MXFDescriptiveMetadata *m = NULL, *old = NULL;
1810
1811 scheme = GST_READ_UINT8 (&klv->key.u[12]);
1812 type = GST_READ_UINT24_BE (&klv->key.u[13]);
1813
1814 GST_DEBUG_OBJECT (demux,
1815 "Handling descriptive metadata of size %" G_GSIZE_FORMAT " at offset %"
1816 G_GUINT64_FORMAT " with scheme 0x%02x and type 0x%06x",
1817 klv->length, klv->offset, scheme, type);
1818
1819 if (G_UNLIKELY (!demux->current_partition)) {
1820 GST_ERROR_OBJECT (demux, "Partition pack doesn't exist");
1821 return GST_FLOW_ERROR;
1822 }
1823
1824 if (G_UNLIKELY (!demux->current_partition->primer.mappings)) {
1825 GST_ERROR_OBJECT (demux, "Primer pack doesn't exists");
1826 return GST_FLOW_ERROR;
1827 }
1828
1829 if (demux->current_partition->parsed_metadata) {
1830 GST_DEBUG_OBJECT (demux, "Metadata of this partition was already parsed");
1831 return GST_FLOW_OK;
1832 }
1833
1834 ret = gst_mxf_demux_fill_klv (demux, klv);
1835 if (ret != GST_FLOW_OK)
1836 return ret;
1837
1838 gst_buffer_map (klv->data, &map, GST_MAP_READ);
1839 m = mxf_descriptive_metadata_new (scheme, type,
1840 &demux->current_partition->primer, demux->offset, map.data, map.size);
1841 gst_buffer_unmap (klv->data, &map);
1842
1843 if (!m) {
1844 GST_WARNING_OBJECT (demux,
1845 "Unknown or unhandled descriptive metadata of scheme 0x%02x and type 0x%06x",
1846 scheme, type);
1847 return GST_FLOW_OK;
1848 }
1849
1850 old =
1851 g_hash_table_lookup (demux->metadata,
1852 &MXF_METADATA_BASE (m)->instance_uid);
1853
1854 if (old && G_TYPE_FROM_INSTANCE (old) != G_TYPE_FROM_INSTANCE (m)) {
1855 #ifndef GST_DISABLE_GST_DEBUG
1856 gchar str[48];
1857 #endif
1858
1859 GST_DEBUG_OBJECT (demux,
1860 "Metadata with instance uid %s already exists and has different type '%s',"
1861 " expected '%s'",
1862 mxf_uuid_to_string (&MXF_METADATA_BASE (m)->instance_uid, str),
1863 g_type_name (G_TYPE_FROM_INSTANCE (old)),
1864 g_type_name (G_TYPE_FROM_INSTANCE (m)));
1865 g_object_unref (m);
1866 return GST_FLOW_ERROR;
1867 } else if (old
1868 && MXF_METADATA_BASE (old)->offset >= MXF_METADATA_BASE (m)->offset) {
1869 #ifndef GST_DISABLE_GST_DEBUG
1870 gchar str[48];
1871 #endif
1872
1873 GST_DEBUG_OBJECT (demux,
1874 "Metadata with instance uid %s already exists and is newer",
1875 mxf_uuid_to_string (&MXF_METADATA_BASE (m)->instance_uid, str));
1876 g_object_unref (m);
1877 return GST_FLOW_OK;
1878 }
1879
1880 g_rw_lock_writer_lock (&demux->metadata_lock);
1881
1882 demux->update_metadata = TRUE;
1883 gst_mxf_demux_reset_linked_metadata (demux);
1884
1885 g_hash_table_replace (demux->metadata, &MXF_METADATA_BASE (m)->instance_uid,
1886 m);
1887
1888 g_rw_lock_writer_unlock (&demux->metadata_lock);
1889
1890 return ret;
1891 }
1892
1893 static GstFlowReturn
1894 gst_mxf_demux_handle_generic_container_system_item (GstMXFDemux * demux,
1895 GstMXFKLV * klv)
1896 {
1897 GST_DEBUG_OBJECT (demux,
1898 "Handling generic container system item of size %" G_GSIZE_FORMAT
1899 " at offset %" G_GUINT64_FORMAT, klv->length, klv->offset);
1900
1901 if (demux->current_partition->essence_container_offset == 0)
1902 demux->current_partition->essence_container_offset =
1903 demux->offset - demux->current_partition->partition.this_partition -
1904 demux->run_in;
1905
1906 /* TODO: parse this */
1907 return GST_FLOW_OK;
1908 }
1909
1910 static GstFlowReturn
1911 gst_mxf_demux_pad_set_component (GstMXFDemux * demux, GstMXFDemuxPad * pad,
1912 guint i)
1913 {
1914 GstFlowReturn ret = GST_FLOW_OK;
1915 GstCaps *pad_caps;
1916 MXFMetadataSequence *sequence;
1917 guint k;
1918 MXFMetadataSourcePackage *source_package = NULL;
1919 MXFMetadataTimelineTrack *source_track = NULL;
1920 gboolean update = (pad->current_component_index != i);
1921
1922 pad->current_component_index = i;
1923
1924 sequence = pad->material_track->parent.sequence;
1925
1926 if (pad->current_component_index >= sequence->n_structural_components) {
1927 GST_DEBUG_OBJECT (demux, "After last structural component");
1928 pad->current_component_index = sequence->n_structural_components - 1;
1929 ret = GST_FLOW_EOS;
1930 }
1931
1932 GST_DEBUG_OBJECT (demux, "Switching to component %u",
1933 pad->current_component_index);
1934
1935 pad->current_component =
1936 MXF_METADATA_SOURCE_CLIP (sequence->structural_components[pad->
1937 current_component_index]);
1938 if (pad->current_component == NULL) {
1939 GST_ERROR_OBJECT (demux, "No such structural component");
1940 return GST_FLOW_ERROR;
1941 }
1942
1943 if (!pad->current_component->source_package
1944 || !pad->current_component->source_package->top_level
1945 || !MXF_METADATA_GENERIC_PACKAGE (pad->current_component->
1946 source_package)->tracks) {
1947 GST_ERROR_OBJECT (demux, "Invalid component");
1948 return GST_FLOW_ERROR;
1949 }
1950
1951 source_package = pad->current_component->source_package;
1952
1953 for (k = 0; k < source_package->parent.n_tracks; k++) {
1954 MXFMetadataTrack *tmp = source_package->parent.tracks[k];
1955
1956 if (tmp->track_id == pad->current_component->source_track_id) {
1957 source_track = MXF_METADATA_TIMELINE_TRACK (tmp);
1958 break;
1959 }
1960 }
1961
1962 if (!source_track) {
1963 GST_ERROR_OBJECT (demux, "No source track found");
1964 return GST_FLOW_ERROR;
1965 }
1966
1967 pad->current_essence_track = NULL;
1968
1969 for (k = 0; k < demux->essence_tracks->len; k++) {
1970 GstMXFDemuxEssenceTrack *tmp = g_ptr_array_index (demux->essence_tracks, k);
1971
1972 if (tmp->source_package == source_package &&
1973 tmp->source_track == source_track) {
1974 pad->current_essence_track = tmp;
1975 break;
1976 }
1977 }
1978
1979 if (!pad->current_essence_track) {
1980 GST_ERROR_OBJECT (demux, "No corresponding essence track found");
1981 return GST_FLOW_ERROR;
1982 }
1983
1984 if (!source_package->descriptor) {
1985 GST_ERROR_OBJECT (demux, "Source package has no descriptors");
1986 return GST_FLOW_ERROR;
1987 }
1988
1989 if (!source_track->parent.descriptor) {
1990 GST_ERROR_OBJECT (demux, "No descriptor found for track");
1991 return GST_FLOW_ERROR;
1992 }
1993
1994 if (source_track->edit_rate.n <= 0 || source_track->edit_rate.d <= 0) {
1995 GST_ERROR_OBJECT (demux, "Source track has invalid edit rate");
1996 return GST_FLOW_ERROR;
1997 }
1998
1999 pad->current_component_start_position = 0;
2000 for (k = 0; k < i; k++) {
2001 pad->current_component_start_position +=
2002 MXF_METADATA_SOURCE_CLIP (sequence->structural_components[k])->
2003 parent.duration;
2004 }
2005
2006 pad->current_component_duration = pad->current_component->parent.duration;
2007
2008 if (pad->material_track->edit_rate.n != source_track->edit_rate.n ||
2009 pad->material_track->edit_rate.d != source_track->edit_rate.d) {
2010 pad->current_component_start +=
2011 gst_util_uint64_scale (pad->current_component->start_position,
2012 source_track->edit_rate.n * pad->material_track->edit_rate.d,
2013 source_track->edit_rate.d * pad->material_track->edit_rate.n);
2014
2015 if (pad->current_component_duration != -1)
2016 pad->current_component_duration =
2017 gst_util_uint64_scale (pad->current_component_duration,
2018 source_track->edit_rate.n * pad->material_track->edit_rate.d,
2019 source_track->edit_rate.d * pad->material_track->edit_rate.n);
2020 } else {
2021 pad->current_component_start += pad->current_component->start_position;
2022 }
2023 pad->current_essence_track_position = pad->current_component_start;
2024
2025 pad_caps = gst_pad_get_current_caps (GST_PAD_CAST (pad));
2026 if (!pad_caps
2027 || !gst_caps_is_equal (pad_caps, pad->current_essence_track->caps)) {
2028 gst_pad_set_caps (GST_PAD_CAST (pad), pad->current_essence_track->caps);
2029 }
2030 if (pad_caps)
2031 gst_caps_unref (pad_caps);
2032
2033 if (update) {
2034 if (pad->tags) {
2035 if (pad->current_essence_track->tags)
2036 gst_tag_list_insert (pad->tags, pad->current_essence_track->tags,
2037 GST_TAG_MERGE_REPLACE);
2038 } else {
2039 if (pad->current_essence_track->tags)
2040 pad->tags = gst_tag_list_copy (pad->current_essence_track->tags);
2041 }
2042 }
2043
2044 if (ret == GST_FLOW_EOS) {
2045 pad->current_essence_track_position += pad->current_component_duration;
2046 }
2047
2048 return ret;
2049 }
2050
2051 /*
2052 * Find the partition containing the stream offset of the given track
2053 * */
2054 static GstMXFDemuxPartition *
2055 get_partition_for_stream_offset (GstMXFDemux * demux,
2056 GstMXFDemuxEssenceTrack * etrack, guint64 stream_offset)
2057 {
2058 GList *tmp;
2059 GstMXFDemuxPartition *offset_partition = NULL, *next_partition = NULL;
2060
2061 for (tmp = demux->partitions; tmp; tmp = tmp->next) {
2062 GstMXFDemuxPartition *partition = tmp->data;
2063
2064 if (!next_partition && offset_partition)
2065 next_partition = partition;
2066
2067 if (partition->partition.body_sid != etrack->body_sid)
2068 continue;
2069 if (partition->partition.body_offset > stream_offset)
2070 break;
2071
2072 offset_partition = partition;
2073 next_partition = NULL;
2074 }
2075
2076 if (offset_partition
2077 && stream_offset < offset_partition->partition.body_offset)
2078 return NULL;
2079
2080 GST_DEBUG_OBJECT (demux,
2081 "Found this_partition:%" G_GUINT64_FORMAT " body_offset:%"
2082 G_GUINT64_FORMAT, offset_partition->partition.this_partition,
2083 offset_partition->partition.body_offset);
2084
2085 /* Are we overriding into the next partition ? */
2086 if (next_partition) {
2087 guint64 partition_essence_size =
2088 next_partition->partition.this_partition -
2089 offset_partition->partition.this_partition +
2090 offset_partition->essence_container_offset;
2091 guint64 in_partition =
2092 stream_offset - offset_partition->partition.body_offset;
2093 GST_DEBUG_OBJECT (demux,
2094 "Followed by this_partition:%" G_GUINT64_FORMAT " body_offset:%"
2095 G_GUINT64_FORMAT, next_partition->partition.this_partition,
2096 next_partition->partition.body_offset);
2097
2098 if (in_partition >= partition_essence_size) {
2099 GST_WARNING_OBJECT (demux,
2100 "stream_offset %" G_GUINT64_FORMAT
2101 " in track body_sid:% index_sid:%u leaks into next unrelated partition (body_sid:%u / index_sid:%u)",
2102 stream_offset, etrack->body_sid, etrack->index_sid,
2103 next_partition->partition.body_sid,
2104 next_partition->partition.index_sid);
2105 return NULL;
2106 }
2107 }
2108 return offset_partition;
2109 }
2110
2111 static GstMXFDemuxIndexTable *
2112 get_track_index_table (GstMXFDemux * demux, GstMXFDemuxEssenceTrack * etrack)
2113 {
2114 GList *l;
2115
2116 /* Look in the indextables */
2117 for (l = demux->index_tables; l; l = l->next) {
2118 GstMXFDemuxIndexTable *tmp = l->data;
2119
2120 if (tmp->body_sid == etrack->body_sid
2121 && tmp->index_sid == etrack->index_sid) {
2122 return tmp;
2123 }
2124 }
2125
2126 return NULL;
2127 }
2128
2129 static guint32
2130 get_track_max_temporal_offset (GstMXFDemux * demux,
2131 GstMXFDemuxEssenceTrack * etrack)
2132 {
2133 GstMXFDemuxIndexTable *table;
2134
2135 if (etrack->intra_only)
2136 return 0;
2137
2138 table = get_track_index_table (demux, etrack);
2139
2140 if (table)
2141 return table->max_temporal_offset;
2142 return 0;
2143 }
2144
2145 static guint64
2146 find_offset (GArray * offsets, guint64 * position, gboolean keyframe)
2147 {
2148 GstMXFDemuxIndex *idx;
2149 guint64 current_offset = -1;
2150 guint64 current_position = *position;
2151
2152 if (!offsets || offsets->len <= *position)
2153 return -1;
2154
2155 idx = &g_array_index (offsets, GstMXFDemuxIndex, *position);
2156 if (idx->offset != 0 && (!keyframe || idx->keyframe)) {
2157 current_offset = idx->offset;
2158 } else if (idx->offset != 0 && current_position > 0) {
2159 current_position--;
2160 while (TRUE) {
2161 GST_LOG ("current_position %" G_GUINT64_FORMAT, current_position);
2162 idx = &g_array_index (offsets, GstMXFDemuxIndex, current_position);
2163 if (idx->offset == 0) {
2164 GST_LOG ("breaking offset 0");
2165 break;
2166 } else if (!idx->keyframe && current_position > 0) {
2167 current_position--;
2168 continue;
2169 } else {
2170 GST_LOG ("Breaking found offset");
2171 current_offset = idx->offset;
2172 break;
2173 }
2174 }
2175 }
2176
2177 if (current_offset == -1)
2178 return -1;
2179
2180 *position = current_position;
2181 return current_offset;
2182 }
2183
2184 /**
2185 * find_edit_entry:
2186 * @demux: The demuxer
2187 * @etrack: The target essence track
2188 * @position: An edit unit position
2189 * @keyframe: if TRUE search for supporting keyframe
2190 * @entry: (out): Will be filled with the matching entry information
2191 *
2192 * Finds the edit entry of @etrack for the given edit unit @position and fill
2193 * @entry with the information about that edit entry. If @keyframe is TRUE, the
2194 * supporting entry (i.e. keyframe) for the given position will be searched for.
2195 *
2196 * For frame-wrapped contents, the returned offset will be the position of the
2197 * KLV of the content. For clip-wrapped content, the returned offset will be the
2198 * position of the essence (i.e. without KLV header) and the entry will specify
2199 * the size (in bytes).
2200 *
2201 * The returned entry will also specify the duration (in edit units) of the
2202 * content, which can be different from 1 for special cases (such as raw audio
2203 * where multiple samples could be aggregated).
2204 *
2205 * Returns: TRUE if the entry was found and @entry was properly filled, else
2206 * FALSE.
2207 */
2208 static gboolean
2209 find_edit_entry (GstMXFDemux * demux, GstMXFDemuxEssenceTrack * etrack,
2210 guint64 position, gboolean keyframe, GstMXFDemuxIndex * entry)
2211 {
2212 GstMXFDemuxIndexTable *index_table = NULL;
2213 guint i;
2214 MXFIndexTableSegment *segment = NULL;
2215 GstMXFDemuxPartition *offset_partition = NULL;
2216 guint64 stream_offset = G_MAXUINT64, absolute_offset;
2217
2218 GST_DEBUG_OBJECT (demux,
2219 "track %u body_sid:%u index_sid:%u delta_id:%d position:%"
2220 G_GUINT64_FORMAT " keyframe:%d", etrack->track_id, etrack->body_sid,
2221 etrack->index_sid, etrack->delta_id, position, keyframe);
2222
2223 /* Default values */
2224 entry->duration = 1;
2225 /* By default every entry is a keyframe unless specified otherwise */
2226 entry->keyframe = TRUE;
2227
2228 /* Look in the track offsets */
2229 if (etrack->offsets && etrack->offsets->len > position) {
2230 if (find_offset (etrack->offsets, &position, keyframe) != -1) {
2231 *entry = g_array_index (etrack->offsets, GstMXFDemuxIndex, position);
2232 GST_LOG_OBJECT (demux, "Found entry in track offsets");
2233 return TRUE;
2234 } else
2235 GST_LOG_OBJECT (demux, "Didn't find entry in track offsets");
2236 }
2237
2238 /* Look in the indextables */
2239 index_table = get_track_index_table (demux, etrack);
2240
2241 if (!index_table) {
2242 GST_DEBUG_OBJECT (demux,
2243 "Couldn't find index table for body_sid:%u index_sid:%u",
2244 etrack->body_sid, etrack->index_sid);
2245 return FALSE;
2246 }
2247
2248 GST_DEBUG_OBJECT (demux,
2249 "Looking for position %" G_GUINT64_FORMAT
2250 " in index table (max temporal offset %u)",
2251 etrack->position, index_table->max_temporal_offset);
2252
2253 /* Searching for a position in index tables works in 3 steps:
2254 *
2255 * 1. Figure out the table segment containing that position
2256 * 2. Figure out the "stream offset" (and additional flags/timing) of that
2257 * position from the table segment.
2258 * 3. Figure out the "absolute offset" of that "stream offset" using partitions
2259 */
2260
2261 search_in_segment:
2262
2263 /* Find matching index segment */
2264 GST_DEBUG_OBJECT (demux, "Look for entry in %u segments",
2265 index_table->segments->len);
2266 for (i = 0; i < index_table->segments->len; i++) {
2267 MXFIndexTableSegment *cand =
2268 &g_array_index (index_table->segments, MXFIndexTableSegment, i);
2269 if (position >= cand->index_start_position && (cand->index_duration == 0
2270 || position <
2271 (cand->index_start_position + cand->index_duration))) {
2272 GST_DEBUG_OBJECT (demux,
2273 "Entry is in Segment #%u , start: %" G_GUINT64_FORMAT " , duration: %"
2274 G_GUINT64_FORMAT, i, cand->index_start_position,
2275 cand->index_duration);
2276 segment = cand;
2277 break;
2278 }
2279 }
2280 if (!segment) {
2281 GST_DEBUG_OBJECT (demux,
2282 "Didn't find index table segment for position %" G_GUINT64_FORMAT,
2283 position);
2284 return FALSE;
2285 }
2286
2287 /* Were we asked for a keyframe ? */
2288 if (keyframe) {
2289 if (segment->edit_unit_byte_count && !segment->n_index_entries) {
2290 GST_LOG_OBJECT (demux,
2291 "Index table without entries, directly using requested position for keyframe search");
2292 } else {
2293 guint64 candidate;
2294 GST_LOG_OBJECT (demux, "keyframe search");
2295 /* Search backwards for keyframe */
2296 for (candidate = position; candidate >= segment->index_start_position;
2297 candidate--) {
2298 MXFIndexEntry *segment_index_entry =
2299 &segment->index_entries[candidate - segment->index_start_position];
2300
2301 /* Match */
2302 if (segment_index_entry->flags & 0x80) {
2303 GST_LOG_OBJECT (demux,
2304 "Found keyframe at position %" G_GUINT64_FORMAT, candidate);
2305 position = candidate;
2306 break;
2307 }
2308
2309 /* If a keyframe offset is specified and valid, use that */
2310 if (segment_index_entry->key_frame_offset
2311 && !(segment_index_entry->flags & 0x08)) {
2312 GST_DEBUG_OBJECT (demux, "Using keyframe offset %d",
2313 segment_index_entry->key_frame_offset);
2314 position = candidate + segment_index_entry->key_frame_offset;
2315 if (position < segment->index_start_position) {
2316 GST_DEBUG_OBJECT (demux, "keyframe info is in previous segment");
2317 goto search_in_segment;
2318 }
2319 break;
2320 }
2321
2322 /* If we reached the beginning, use that */
2323 if (candidate == 0) {
2324 GST_LOG_OBJECT (demux,
2325 "Reached position 0 while searching for keyframe");
2326 position = 0;
2327 break;
2328 }
2329
2330 /* If we looped past the beginning of this segment, go to the previous one */
2331 if (candidate == segment->index_start_position) {
2332 position = candidate - 1;
2333 GST_LOG_OBJECT (demux, "Looping with new position %" G_GUINT64_FORMAT,
2334 position);
2335 goto search_in_segment;
2336 }
2337
2338 /* loop back to check previous entry */
2339 }
2340 }
2341 }
2342
2343 /* Figure out the stream offset (also called "body offset" in specification) */
2344 if (segment->edit_unit_byte_count && !segment->n_index_entries) {
2345 /* Constant entry table. */
2346 stream_offset = position * segment->edit_unit_byte_count;
2347 if (etrack->delta_id >= 0) {
2348 MXFDeltaEntry *delta_entry = &segment->delta_entries[etrack->delta_id];
2349 GST_LOG_OBJECT (demux,
2350 "Using delta %d pos_table_index:%d slice:%u element_delta:%u",
2351 etrack->delta_id, delta_entry->pos_table_index, delta_entry->slice,
2352 delta_entry->element_delta);
2353 stream_offset += delta_entry->element_delta;
2354 } else if (etrack->min_edit_units != 1) {
2355 GST_LOG_OBJECT (demux, "Handling minimum edit unit %u",
2356 etrack->min_edit_units);
2357 entry->duration =
2358 MIN (etrack->min_edit_units,
2359 (segment->index_start_position + segment->index_duration) - position);
2360 entry->size = segment->edit_unit_byte_count * entry->duration;
2361
2362 if (entry->size > G_MAXUINT32) {
2363 GST_ERROR_OBJECT (demux,
2364 "Suspisciously large entry size %" G_GUINT64_FORMAT
2365 " = edit_unit_byte_count %" G_GUINT32_FORMAT " * entry duration %"
2366 G_GUINT64_FORMAT ", exceeds pullable size => not proceeding",
2367 entry->size, segment->edit_unit_byte_count, entry->duration);
2368 entry = NULL;
2369 return FALSE;
2370 }
2371 } else {
2372 entry->size = segment->edit_unit_byte_count;
2373 }
2374 } else if (segment->n_index_entries) {
2375 MXFIndexEntry *segment_index_entry;
2376 MXFDeltaEntry *delta_entry = NULL;
2377 g_assert (position <=
2378 segment->index_start_position + segment->n_index_entries);
2379 segment_index_entry =
2380 &segment->index_entries[position - segment->index_start_position];
2381 stream_offset = segment_index_entry->stream_offset;
2382
2383 if (segment->n_delta_entries > 0)
2384 delta_entry = &segment->delta_entries[etrack->delta_id];
2385
2386 if (delta_entry) {
2387 GST_LOG_OBJECT (demux,
2388 "Using delta %d pos_table_index:%d slice:%u element_delta:%u",
2389 etrack->delta_id, delta_entry->pos_table_index, delta_entry->slice,
2390 delta_entry->element_delta);
2391
2392 /* Apply offset from slice/delta if needed */
2393 if (delta_entry->slice)
2394 stream_offset +=
2395 segment_index_entry->slice_offset[delta_entry->slice - 1];
2396 stream_offset += delta_entry->element_delta;
2397 if (delta_entry->pos_table_index == -1) {
2398 entry->keyframe = (segment_index_entry->flags & 0x80) == 0x80;
2399 }
2400 /* FIXME : Handle fractional offset position (delta_entry->pos_table_offset > 0) */
2401 }
2402
2403 /* Apply reverse temporal reordering if present */
2404 if (index_table->reordered_delta_entry == etrack->delta_id) {
2405 if (demux->temporal_order_misuse) {
2406 GST_DEBUG_OBJECT (demux, "Handling temporal order misuse");
2407 entry->pts = position + segment_index_entry->temporal_offset;
2408 } else if (position >= index_table->reverse_temporal_offsets->len) {
2409 GST_WARNING_OBJECT (demux,
2410 "Can't apply temporal offset for position %" G_GUINT64_FORMAT
2411 " (max:%u)", position, index_table->reverse_temporal_offsets->len);
2412 } else {
2413 entry->pts =
2414 position + g_array_index (index_table->reverse_temporal_offsets,
2415 gint8, position);
2416 GST_LOG_OBJECT (demux,
2417 "Applied temporal offset. dts:%" G_GUINT64_FORMAT " pts:%"
2418 G_GUINT64_FORMAT, position, entry->pts);
2419 }
2420 } else
2421 entry->pts = position;
2422 } else {
2423 /* Note : This should have been handled in the parser */
2424 GST_WARNING_OBJECT (demux,
2425 "Can't handle index tables without entries nor constant edit unit byte count");
2426 return FALSE;
2427 }
2428
2429 /* Find the partition containing the stream offset for this track */
2430 offset_partition =
2431 get_partition_for_stream_offset (demux, etrack, stream_offset);
2432
2433 if (!offset_partition) {
2434 GST_WARNING_OBJECT (demux,
2435 "Couldn't find matching partition for stream offset %" G_GUINT64_FORMAT,
2436 stream_offset);
2437 return FALSE;
2438 } else {
2439 GST_DEBUG_OBJECT (demux, "Entry is in partition %" G_GUINT64_FORMAT,
2440 offset_partition->partition.this_partition);
2441 }
2442
2443 /* Convert stream offset to absolute offset using matching partition */
2444 absolute_offset =
2445 offset_partition->partition.this_partition +
2446 offset_partition->essence_container_offset + (stream_offset -
2447 offset_partition->partition.body_offset);
2448
2449 GST_LOG_OBJECT (demux,
2450 "track %u position:%" G_GUINT64_FORMAT " stream_offset %" G_GUINT64_FORMAT
2451 " matches to absolute offset %" G_GUINT64_FORMAT, etrack->track_id,
2452 position, stream_offset, absolute_offset);
2453 entry->initialized = TRUE;
2454 entry->offset = absolute_offset;
2455 entry->dts = position;
2456
2457 return TRUE;
2458 }
2459
2460 /**
2461 * find_entry_for_offset:
2462 * @demux: The demuxer
2463 * @etrack: The target essence track
2464 * @offset: An absolute byte offset (excluding run_in)
2465 * @entry: (out): Will be filled with the matching entry information
2466 *
2467 * Find the entry located at the given absolute byte offset.
2468 *
2469 * Note: the offset requested should be in the current partition !
2470 *
2471 * Returns: TRUE if the entry was found and @entry was properly filled, else
2472 * FALSE.
2473 */
2474 static gboolean
2475 find_entry_for_offset (GstMXFDemux * demux, GstMXFDemuxEssenceTrack * etrack,
2476 guint64 offset, GstMXFDemuxIndex * retentry)
2477 {
2478 GstMXFDemuxIndexTable *index_table = get_track_index_table (demux, etrack);
2479 guint i;
2480 MXFIndexTableSegment *index_segment = NULL;
2481 GstMXFDemuxPartition *partition = demux->current_partition;
2482 guint64 original_offset = offset;
2483 guint64 cp_offset = 0; /* Offset in Content Package */
2484 MXFIndexEntry *index_entry = NULL;
2485 MXFDeltaEntry *delta_entry = NULL;
2486 guint64 position = 0;
2487
2488 GST_DEBUG_OBJECT (demux,
2489 "track %u body_sid:%u index_sid:%u offset:%" G_GUINT64_FORMAT,
2490 etrack->track_id, etrack->body_sid, etrack->index_sid, offset);
2491
2492 /* Default value */
2493 retentry->duration = 1;
2494 retentry->keyframe = TRUE;
2495
2496 /* Index-less search */
2497 if (etrack->offsets) {
2498 for (i = 0; i < etrack->offsets->len; i++) {
2499 GstMXFDemuxIndex *idx =
2500 &g_array_index (etrack->offsets, GstMXFDemuxIndex, i);
2501
2502 if (idx->initialized && idx->offset != 0 && idx->offset == offset) {
2503 *retentry = *idx;
2504 GST_DEBUG_OBJECT (demux,
2505 "Found in track index. Position:%" G_GUINT64_FORMAT, idx->dts);
2506 return TRUE;
2507 }
2508 }
2509 }
2510
2511 /* Actual index search */
2512 if (!index_table || !index_table->segments->len) {
2513 GST_WARNING_OBJECT (demux, "No index table or entries to search in");
2514 return FALSE;
2515 }
2516
2517 if (!partition) {
2518 GST_WARNING_OBJECT (demux, "No current partition for search");
2519 return FALSE;
2520 }
2521
2522 /* Searching for a stream position from an absolute offset works in 3 steps:
2523 *
2524 * 1. Convert the absolute offset to a "stream offset" based on the partition
2525 * information.
2526 * 2. Find the segment for that "stream offset"
2527 * 3. Match the entry within that segment
2528 */
2529
2530 /* Convert to stream offset */
2531 GST_LOG_OBJECT (demux,
2532 "offset %" G_GUINT64_FORMAT " this_partition:%" G_GUINT64_FORMAT
2533 " essence_container_offset:%" G_GUINT64_FORMAT " partition body offset %"
2534 G_GUINT64_FORMAT, offset, partition->partition.this_partition,
2535 partition->essence_container_offset, partition->partition.body_offset);
2536 offset =
2537 offset - partition->partition.this_partition -
2538 partition->essence_container_offset + partition->partition.body_offset;
2539
2540 GST_LOG_OBJECT (demux, "stream offset %" G_GUINT64_FORMAT, offset);
2541
2542 /* Find the segment that covers the given stream offset (the highest one that
2543 * covers that offset) */
2544 for (i = index_table->segments->len; i > 0; i--) {
2545 index_segment =
2546 &g_array_index (index_table->segments, MXFIndexTableSegment, i - 1);
2547 GST_DEBUG_OBJECT (demux,
2548 "Checking segment #%u (essence_offset %" G_GUINT64_FORMAT ")", i - 1,
2549 index_segment->segment_start_offset);
2550 /* Not in the right segment yet */
2551 if (offset >= index_segment->segment_start_offset) {
2552 GST_LOG_OBJECT (demux, "Found");
2553 break;
2554 }
2555 }
2556 if (!index_segment) {
2557 GST_WARNING_OBJECT (demux,
2558 "Couldn't find index table segment for given offset");
2559 return FALSE;
2560 }
2561
2562 /* In the right segment, figure out:
2563 * * the offset in the content package,
2564 * * the position in edit units
2565 * * the matching entry (if the table has entries)
2566 */
2567 if (index_segment->edit_unit_byte_count) {
2568 cp_offset = offset % index_segment->edit_unit_byte_count;
2569 position = offset / index_segment->edit_unit_byte_count;
2570 /* Boundary check */
2571 if ((position < index_segment->index_start_position)
2572 || (index_segment->index_duration
2573 && position >
2574 (index_segment->index_start_position +
2575 index_segment->index_duration))) {
2576 GST_WARNING_OBJECT (demux,
2577 "Invalid offset, exceeds table segment limits");
2578 return FALSE;
2579 }
2580 if (etrack->min_edit_units != 1) {
2581 retentry->duration = MIN (etrack->min_edit_units,
2582 (index_segment->index_start_position +
2583 index_segment->index_duration) - position);
2584 retentry->size = index_segment->edit_unit_byte_count * retentry->duration;
2585
2586 if (retentry->size > G_MAXUINT32) {
2587 GST_ERROR_OBJECT (demux,
2588 "Suspisciously large entry size %" G_GUINT64_FORMAT
2589 " = edit_unit_byte_count %" G_GUINT32_FORMAT " * entry duration %"
2590 G_GUINT64_FORMAT ", exceeds pullable size => not proceeding",
2591 retentry->size, index_segment->edit_unit_byte_count,
2592 retentry->duration);
2593 retentry = NULL;
2594 return FALSE;
2595 }
2596 } else {
2597 retentry->size = index_segment->edit_unit_byte_count;
2598 }
2599 } else {
2600 /* Find the content package entry containing this offset */
2601 guint cpidx;
2602 for (cpidx = 0; cpidx < index_segment->n_index_entries; cpidx++) {
2603 index_entry = &index_segment->index_entries[cpidx];
2604 GST_DEBUG_OBJECT (demux,
2605 "entry #%u offset:%" G_GUINT64_FORMAT " stream_offset:%"
2606 G_GUINT64_FORMAT, cpidx, offset, index_entry->stream_offset);
2607 if (index_entry->stream_offset == offset) {
2608 index_entry = &index_segment->index_entries[cpidx];
2609 /* exactly on the entry */
2610 cp_offset = offset - index_entry->stream_offset;
2611 position = index_segment->index_start_position + cpidx;
2612 break;
2613 }
2614 if (index_entry->stream_offset > offset && cpidx > 0) {
2615 index_entry = &index_segment->index_entries[cpidx - 1];
2616 /* One too far, result is in previous entry */
2617 cp_offset = offset - index_entry->stream_offset;
2618 position = index_segment->index_start_position + cpidx - 1;
2619 break;
2620 }
2621 }
2622 if (cpidx == index_segment->n_index_entries) {
2623 GST_WARNING_OBJECT (demux,
2624 "offset exceeds maximum number of entries in table segment");
2625 return FALSE;
2626 }
2627 }
2628
2629 /* If the track comes from an interleaved essence container and doesn't have a
2630 * delta_id set, figure it out now */
2631 if (G_UNLIKELY (etrack->delta_id == MXF_INDEX_DELTA_ID_UNKNOWN)) {
2632 guint delta;
2633 GST_DEBUG_OBJECT (demux,
2634 "Unknown delta_id for track. Attempting to resolve it");
2635
2636 if (index_segment->n_delta_entries == 0) {
2637 /* No delta entries, nothing we can do about this */
2638 GST_DEBUG_OBJECT (demux, "Index table has no delta entries, ignoring");
2639 etrack->delta_id = MXF_INDEX_DELTA_ID_IGNORE;
2640 } else if (!index_entry) {
2641 for (delta = 0; delta < index_segment->n_delta_entries; delta++) {
2642 /* No entry, therefore no slices */
2643 GST_LOG_OBJECT (demux,
2644 "delta #%u offset %" G_GUINT64_FORMAT " cp_offs:%" G_GUINT64_FORMAT
2645 " element_delta:%u", delta, offset, cp_offset,
2646 index_segment->delta_entries[delta].element_delta);
2647 if (cp_offset == index_segment->delta_entries[delta].element_delta) {
2648 GST_DEBUG_OBJECT (demux, "Matched to delta %u", delta);
2649 etrack->delta_id = delta;
2650 delta_entry = &index_segment->delta_entries[delta];
2651 break;
2652 }
2653 }
2654 } else {
2655 for (delta = 0; delta < index_segment->n_delta_entries; delta++) {
2656 guint64 delta_offs = 0;
2657 /* If we are not in the first slice, take that offset into account */
2658 if (index_segment->delta_entries[delta].slice)
2659 delta_offs =
2660 index_entry->slice_offset[index_segment->
2661 delta_entries[delta].slice - 1];
2662 /* Add the offset for this delta */
2663 delta_offs += index_segment->delta_entries[delta].element_delta;
2664 if (cp_offset == delta_offs) {
2665 GST_DEBUG_OBJECT (demux, "Matched to delta %u", delta);
2666 etrack->delta_id = delta;
2667 delta_entry = &index_segment->delta_entries[delta];
2668 break;
2669 }
2670 }
2671
2672 }
2673 /* If we didn't managed to match, ignore it from now on */
2674 if (etrack->delta_id == MXF_INDEX_DELTA_ID_UNKNOWN) {
2675 GST_WARNING_OBJECT (demux,
2676 "Couldn't match delta id, ignoring it from now on");
2677 etrack->delta_id = MXF_INDEX_DELTA_ID_IGNORE;
2678 }
2679 } else if (index_segment->n_delta_entries > 0) {
2680 delta_entry = &index_segment->delta_entries[etrack->delta_id];
2681 }
2682
2683 if (index_entry && delta_entry && delta_entry->pos_table_index == -1) {
2684 retentry->keyframe = (index_entry->flags & 0x80) == 0x80;
2685 if (demux->temporal_order_misuse) {
2686 retentry->pts = position + index_entry->temporal_offset;
2687 } else if (position >= index_table->reverse_temporal_offsets->len) {
2688 GST_WARNING_OBJECT (demux,
2689 "Can't apply temporal offset for position %" G_GUINT64_FORMAT
2690 " (max:%u)", position, index_table->reverse_temporal_offsets->len);
2691 } else {
2692 retentry->pts =
2693 position + g_array_index (index_table->reverse_temporal_offsets,
2694 gint8, position);
2695 }
2696 GST_LOG_OBJECT (demux,
2697 "Applied temporal offset. dts:%" G_GUINT64_FORMAT " pts:%"
2698 G_GUINT64_FORMAT, position, retentry->pts);
2699 } else
2700 retentry->pts = position;
2701
2702 /* FIXME : check if position and cp_offs matches the table */
2703 GST_LOG_OBJECT (demux, "Found in index table. position:%" G_GUINT64_FORMAT,
2704 position);
2705 retentry->initialized = TRUE;
2706 retentry->offset = original_offset;
2707 retentry->dts = position;
2708
2709 return TRUE;
2710 }
2711
2712 static GstFlowReturn
2713 gst_mxf_demux_handle_generic_container_essence_element (GstMXFDemux * demux,
2714 GstMXFKLV * klv, gboolean peek)
2715 {
2716 GstFlowReturn ret = GST_FLOW_OK;
2717 guint32 track_number;
2718 guint i;
2719 GstBuffer *inbuf = NULL;
2720 GstBuffer *outbuf = NULL;
2721 GstMXFDemuxEssenceTrack *etrack = NULL;
2722 /* As in GstMXFDemuxIndex */
2723 guint64 pts = G_MAXUINT64;
2724 guint32 max_temporal_offset = 0;
2725 GstMXFDemuxIndex index_entry = { 0, };
2726 guint64 offset;
2727
2728 GST_DEBUG_OBJECT (demux,
2729 "Handling generic container essence element of size %" G_GSIZE_FORMAT
2730 " at offset %" G_GUINT64_FORMAT, klv->length,
2731 klv->offset + klv->consumed);
2732
2733 GST_DEBUG_OBJECT (demux, " type = 0x%02x", klv->key.u[12]);
2734 GST_DEBUG_OBJECT (demux, " essence element count = 0x%02x", klv->key.u[13]);
2735 GST_DEBUG_OBJECT (demux, " essence element type = 0x%02x", klv->key.u[14]);
2736 GST_DEBUG_OBJECT (demux, " essence element number = 0x%02x", klv->key.u[15]);
2737
2738 if (demux->current_partition->essence_container_offset == 0) {
2739 demux->current_partition->essence_container_offset =
2740 demux->offset - demux->current_partition->partition.this_partition -
2741 demux->run_in;
2742 if (demux->current_partition->single_track
2743 && demux->current_partition->single_track->wrapping !=
2744 MXF_ESSENCE_WRAPPING_FRAME_WRAPPING) {
2745 demux->current_partition->essence_container_offset += klv->data_offset;
2746 demux->current_partition->clip_klv = *klv;
2747 /* "consume" the initial bytes of the KLV */
2748 klv->consumed = klv->data_offset;
2749 GST_DEBUG_OBJECT (demux,
2750 "Non-frame wrapping, updated essence_container_offset to %"
2751 G_GUINT64_FORMAT, demux->current_partition->essence_container_offset);
2752 }
2753 }
2754
2755 if (!demux->current_package) {
2756 GST_ERROR_OBJECT (demux, "No package selected yet");
2757 return GST_FLOW_ERROR;
2758 }
2759
2760 if (demux->src->len == 0) {
2761 GST_ERROR_OBJECT (demux, "No streams created yet");
2762 return GST_FLOW_ERROR;
2763 }
2764
2765 if (demux->essence_tracks->len == 0) {
2766 GST_ERROR_OBJECT (demux, "No essence streams found in the metadata");
2767 return GST_FLOW_ERROR;
2768 }
2769
2770 /* Identify and fetch the essence track */
2771 track_number = GST_READ_UINT32_BE (&klv->key.u[12]);
2772
2773 etrack = demux->current_partition->single_track;
2774 if (!etrack) {
2775 for (i = 0; i < demux->essence_tracks->len; i++) {
2776 GstMXFDemuxEssenceTrack *tmp =
2777 g_ptr_array_index (demux->essence_tracks, i);
2778
2779 if (tmp->body_sid == demux->current_partition->partition.body_sid &&
2780 (tmp->track_number == track_number || tmp->track_number == 0)) {
2781 etrack = tmp;
2782 break;
2783 }
2784 }
2785
2786 if (!etrack) {
2787 GST_DEBUG_OBJECT (demux,
2788 "No essence track for this essence element found");
2789 return GST_FLOW_OK;
2790 }
2791 }
2792
2793 GST_DEBUG_OBJECT (demux,
2794 "Handling generic container essence (track %u , position:%"
2795 G_GUINT64_FORMAT ", number: 0x%08x , frame-wrapped:%d)", etrack->track_id,
2796 etrack->position, track_number,
2797 etrack->wrapping == MXF_ESSENCE_WRAPPING_FRAME_WRAPPING);
2798
2799 /* Fetch the current entry.
2800 *
2801 * 1. If we don't have a current position, use find_entry_for_offset()
2802 * 2. If we do have a position, use find_edit_entry()
2803 *
2804 * 3. If we are dealing with frame-wrapped content, pull the corresponding
2805 * data from upstream (because it wasn't provided). If we didn't find an
2806 * entry, error out because we can't deal with a frame-wrapped stream
2807 * without index.
2808 */
2809
2810 offset = klv->offset + klv->consumed;
2811
2812 /* Update the track position (in case of resyncs) */
2813 if (etrack->position == -1) {
2814 GST_DEBUG_OBJECT (demux,
2815 "Unknown essence track position, looking into index");
2816 if (!find_entry_for_offset (demux, etrack, offset - demux->run_in,
2817 &index_entry)) {
2818 GST_WARNING_OBJECT (demux, "Essence track position not in index");
2819 return GST_FLOW_OK;
2820 }
2821 /* Update track position */
2822 etrack->position = index_entry.dts;
2823 } else if (etrack->delta_id == MXF_INDEX_DELTA_ID_UNKNOWN) {
2824 GST_DEBUG_OBJECT (demux,
2825 "Unknown essence track delta_id, looking into index");
2826 if (!find_entry_for_offset (demux, etrack, offset - demux->run_in,
2827 &index_entry)) {
2828 /* Non-fatal, fallback to legacy mode */
2829 GST_WARNING_OBJECT (demux, "Essence track position not in index");
2830 } else if (etrack->position != index_entry.dts) {
2831 GST_ERROR_OBJECT (demux,
2832 "track position doesn't match %" G_GUINT64_FORMAT " entry dts %"
2833 G_GUINT64_FORMAT, etrack->position, index_entry.dts);
2834 return GST_FLOW_ERROR;
2835 }
2836 } else {
2837 if (!find_edit_entry (demux, etrack, etrack->position, FALSE, &index_entry)) {
2838 GST_DEBUG_OBJECT (demux, "Couldn't find entry");
2839 } else if (etrack->wrapping == MXF_ESSENCE_WRAPPING_FRAME_WRAPPING) {
2840 if (etrack->delta_id != MXF_INDEX_DELTA_ID_IGNORE
2841 && index_entry.offset != offset) {
2842 GST_ERROR_OBJECT (demux,
2843 "demux offset doesn't match %" G_GUINT64_FORMAT " entry offset %"
2844 G_GUINT64_FORMAT, offset, index_entry.offset);
2845 return GST_FLOW_ERROR;
2846 }
2847 } else if (index_entry.offset != klv->offset + klv->consumed &&
2848 index_entry.offset != klv->offset + klv->data_offset) {
2849 GST_ERROR_OBJECT (demux,
2850 "KLV offset doesn't match %" G_GUINT64_FORMAT " entry offset %"
2851 G_GUINT64_FORMAT, klv->offset + klv->consumed, index_entry.offset);
2852 return GST_FLOW_ERROR;
2853 }
2854 }
2855
2856 if (etrack->wrapping != MXF_ESSENCE_WRAPPING_FRAME_WRAPPING) {
2857 /* We need entry information to deal with non-frame-wrapped content */
2858 if (!index_entry.initialized) {
2859 GST_ELEMENT_ERROR (demux, STREAM, WRONG_TYPE, (NULL),
2860 ("Essence with non-frame-wrapping require an index table to be present"));
2861 return GST_FLOW_ERROR;
2862 }
2863 /* We cannot deal with non-frame-wrapping in push mode for now */
2864 if (!demux->random_access) {
2865 GST_ELEMENT_ERROR (demux, STREAM, WRONG_TYPE, (NULL),
2866 ("Non-frame-wrapping is not support in push mode"));
2867 return GST_FLOW_ERROR;
2868 }
2869 }
2870
2871 /* FIXME : If we're peeking and don't need to actually parse the data, we
2872 * should avoid pulling the content from upstream */
2873 if (etrack->wrapping != MXF_ESSENCE_WRAPPING_FRAME_WRAPPING) {
2874 g_assert (index_entry.size);
2875 GST_DEBUG_OBJECT (demux, "Should only grab %" G_GUINT64_FORMAT " bytes",
2876 index_entry.size);
2877 ret =
2878 gst_mxf_demux_pull_range (demux, index_entry.offset, index_entry.size,
2879 &inbuf);
2880 if (ret != GST_FLOW_OK)
2881 return ret;
2882 if (klv->consumed == 0)
2883 klv->consumed = klv->data_offset + index_entry.size;
2884 else
2885 klv->consumed += index_entry.size;
2886 if (klv != &demux->current_partition->clip_klv)
2887 demux->current_partition->clip_klv = *klv;
2888 GST_LOG_OBJECT (demux,
2889 "klv data_offset:%" G_GUINT64_FORMAT " length:%" G_GSIZE_FORMAT
2890 " consumed:%" G_GUINT64_FORMAT, klv->data_offset, klv->length,
2891 klv->consumed);
2892 /* Switch back to KLV mode if we're done with this one */
2893 if (klv->length + klv->data_offset == klv->consumed)
2894 demux->state = GST_MXF_DEMUX_STATE_KLV;
2895 else
2896 demux->state = GST_MXF_DEMUX_STATE_ESSENCE;
2897 } else {
2898
2899 ret = gst_mxf_demux_fill_klv (demux, klv);
2900 if (ret != GST_FLOW_OK)
2901 return ret;
2902
2903 /* Create subbuffer to be able to change metadata */
2904 inbuf =
2905 gst_buffer_copy_region (klv->data, GST_BUFFER_COPY_ALL, 0,
2906 gst_buffer_get_size (klv->data));
2907
2908 }
2909
2910 if (index_entry.initialized) {
2911 GST_DEBUG_OBJECT (demux, "Got entry dts:%" G_GUINT64_FORMAT " keyframe:%d",
2912 index_entry.dts, index_entry.keyframe);
2913 }
2914 if (index_entry.initialized && !index_entry.keyframe)
2915 GST_BUFFER_FLAG_SET (inbuf, GST_BUFFER_FLAG_DELTA_UNIT);
2916
2917 if (etrack->handle_func) {
2918 /* Takes ownership of inbuf */
2919 ret =
2920 etrack->handle_func (&klv->key, inbuf, etrack->caps,
2921 etrack->source_track, etrack->mapping_data, &outbuf);
2922 inbuf = NULL;
2923 } else {
2924 outbuf = inbuf;
2925 inbuf = NULL;
2926 ret = GST_FLOW_OK;
2927 }
2928
2929 if (ret != GST_FLOW_OK) {
2930 GST_ERROR_OBJECT (demux, "Failed to handle essence element");
2931 if (outbuf) {
2932 gst_buffer_unref (outbuf);
2933 outbuf = NULL;
2934 }
2935 return ret;
2936 }
2937
2938 if (!etrack->offsets)
2939 etrack->offsets = g_array_new (FALSE, TRUE, sizeof (GstMXFDemuxIndex));
2940
2941 if (!index_entry.initialized) {
2942 /* This can happen when doing scanning without entry tables */
2943 index_entry.duration = 1;
2944 index_entry.offset = demux->offset - demux->run_in;
2945 index_entry.dts = etrack->position;
2946 index_entry.pts = etrack->intra_only ? etrack->position : G_MAXUINT64;
2947 index_entry.keyframe =
2948 !GST_BUFFER_FLAG_IS_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
2949 index_entry.initialized = TRUE;
2950 GST_DEBUG_OBJECT (demux,
2951 "Storing newly discovered information on track %u. dts: %"
2952 G_GUINT64_FORMAT " offset:%" G_GUINT64_FORMAT " keyframe:%d",
2953 etrack->track_id, index_entry.dts, index_entry.offset,
2954 index_entry.keyframe);
2955
2956 /* We only ever append to the track offset entry. */
2957 g_assert (etrack->position <= etrack->offsets->len);
2958 g_array_insert_val (etrack->offsets, etrack->position, index_entry);
2959 } else if (etrack->position == etrack->offsets->len) {
2960 g_array_insert_val (etrack->offsets, etrack->position, index_entry);
2961 }
2962
2963 if (peek)
2964 goto out;
2965
2966 if (!outbuf) {
2967 GST_DEBUG_OBJECT (demux, "No output buffer created");
2968 goto out;
2969 }
2970
2971 inbuf = outbuf;
2972 outbuf = NULL;
2973
2974 max_temporal_offset = get_track_max_temporal_offset (demux, etrack);
2975
2976 for (i = 0; i < demux->src->len; i++) {
2977 GstMXFDemuxPad *pad = g_ptr_array_index (demux->src, i);
2978
2979 if (pad->current_essence_track != etrack)
2980 continue;
2981
2982 if (pad->eos) {
2983 GST_DEBUG_OBJECT (pad, "Pad is already EOS");
2984 continue;
2985 }
2986
2987 if (etrack->position < pad->current_essence_track_position) {
2988 GST_DEBUG_OBJECT (pad,
2989 "Not at current component's position (track:%" G_GUINT64_FORMAT
2990 " essence:%" G_GUINT64_FORMAT ")", etrack->position,
2991 pad->current_essence_track_position);
2992 continue;
2993 }
2994
2995 guint64 current_edit_unit =
2996 pad->current_essence_track_position - pad->current_component_start;
2997 GstClockTime component_start_time =
2998 gst_util_uint64_scale (pad->current_component_start_position,
2999 pad->material_track->edit_rate.d * GST_SECOND,
3000 pad->material_track->edit_rate.n);
3001 GstClockTime time =
3002 component_start_time + gst_util_uint64_scale (current_edit_unit,
3003 pad->current_essence_track->source_track->edit_rate.d * GST_SECOND,
3004 pad->current_essence_track->source_track->edit_rate.n);
3005 GstClockTime time_end = component_start_time +
3006 gst_util_uint64_scale (current_edit_unit + index_entry.duration,
3007 pad->current_essence_track->source_track->edit_rate.d * GST_SECOND,
3008 pad->current_essence_track->source_track->edit_rate.n);
3009
3010 {
3011 GstMXFDemuxPad *earliest = gst_mxf_demux_get_earliest_pad (demux);
3012 GstClockTime earliest_time =
3013 gst_mxf_demux_pad_get_current_time (demux, earliest);
3014
3015 if (earliest && earliest != pad && earliest_time < time &&
3016 time - earliest_time > demux->max_drift) {
3017 GST_DEBUG_OBJECT (earliest,
3018 "Pad is too far ahead of time (%" GST_TIME_FORMAT " vs earliest:%"
3019 GST_TIME_FORMAT ")", GST_TIME_ARGS (earliest_time),
3020 GST_TIME_ARGS (time));
3021 continue;
3022 }
3023 }
3024
3025 /* Create another subbuffer to have writable metadata */
3026 outbuf =
3027 gst_buffer_copy_region (inbuf, GST_BUFFER_COPY_ALL, 0,
3028 gst_buffer_get_size (inbuf));
3029
3030 pts = index_entry.pts;
3031
3032 GST_BUFFER_DTS (outbuf) = time;
3033 if (etrack->intra_only) {
3034 GST_BUFFER_PTS (outbuf) = time;
3035 } else if (pts != G_MAXUINT64) {
3036 GST_BUFFER_PTS (outbuf) =
3037 component_start_time + gst_util_uint64_scale (pts,
3038 pad->current_essence_track->source_track->edit_rate.d * GST_SECOND,
3039 pad->current_essence_track->source_track->edit_rate.n);
3040 /* We are dealing with reordered data, the PTS is shifted forward by the
3041 * maximum temporal reordering (the DTS remain as-is). */
3042 GST_BUFFER_PTS (outbuf) +=
3043 gst_util_uint64_scale_ceil (max_temporal_offset,
3044 pad->current_essence_track->source_track->edit_rate.d * GST_SECOND,
3045 pad->current_essence_track->source_track->edit_rate.n);
3046 } else {
3047 GST_BUFFER_PTS (outbuf) = GST_CLOCK_TIME_NONE;
3048 }
3049
3050 GST_BUFFER_DURATION (outbuf) = time_end - time;
3051 GST_BUFFER_OFFSET (outbuf) = GST_BUFFER_OFFSET_NONE;
3052 GST_BUFFER_OFFSET_END (outbuf) = GST_BUFFER_OFFSET_NONE;
3053
3054 if (pad->material_track->parent.type == MXF_METADATA_TRACK_PICTURE_ESSENCE
3055 && pad->start_timecode.config.fps_n != 0
3056 && pad->start_timecode.config.fps_d != 0) {
3057 if (etrack->intra_only) {
3058 GstVideoTimeCode timecode = pad->start_timecode;
3059
3060 gst_video_time_code_add_frames (&timecode,
3061 pad->current_material_track_position);
3062 gst_buffer_add_video_time_code_meta (outbuf, &timecode);
3063 } else if (pts != G_MAXUINT64) {
3064 GstVideoTimeCode timecode = pad->start_timecode;
3065
3066 gst_video_time_code_add_frames (&timecode,
3067 pad->current_component_start_position);
3068 gst_video_time_code_add_frames (&timecode,
3069 gst_util_uint64_scale (pts,
3070 pad->material_track->edit_rate.n *
3071 pad->current_essence_track->source_track->edit_rate.d,
3072 pad->material_track->edit_rate.d *
3073 pad->current_essence_track->source_track->edit_rate.n));
3074 gst_buffer_add_video_time_code_meta (outbuf, &timecode);
3075 }
3076
3077 }
3078
3079 if (pad->need_segment) {
3080 GstEvent *e;
3081 GstSegment shifted_segment;
3082
3083 /* Handle maximum temporal offset. We are shifting all output PTS for
3084 * this stream by the greatest temporal reordering that can occur. In
3085 * order not to change the stream/running time we shift the segment
3086 * start and stop values accordingly */
3087 gst_segment_copy_into (&demux->segment, &shifted_segment);
3088 if (GST_CLOCK_TIME_IS_VALID (shifted_segment.start))
3089 shifted_segment.start +=
3090 gst_util_uint64_scale_ceil (max_temporal_offset,
3091 pad->current_essence_track->source_track->edit_rate.d *
3092 GST_SECOND, pad->current_essence_track->source_track->edit_rate.n);
3093 if (GST_CLOCK_TIME_IS_VALID (shifted_segment.stop))
3094 shifted_segment.stop +=
3095 gst_util_uint64_scale_ceil (max_temporal_offset,
3096 pad->current_essence_track->source_track->edit_rate.d *
3097 GST_SECOND, pad->current_essence_track->source_track->edit_rate.n);
3098 e = gst_event_new_segment (&shifted_segment);
3099 GST_DEBUG_OBJECT (pad, "Sending segment %" GST_PTR_FORMAT, e);
3100 gst_event_set_seqnum (e, demux->seqnum);
3101 gst_pad_push_event (GST_PAD_CAST (pad), e);
3102 pad->need_segment = FALSE;
3103 }
3104
3105 if (pad->tags) {
3106 gst_pad_push_event (GST_PAD_CAST (pad), gst_event_new_tag (pad->tags));
3107 pad->tags = NULL;
3108 }
3109
3110 pad->current_material_track_position += index_entry.duration;
3111
3112 if (pad->discont) {
3113 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
3114 pad->discont = FALSE;
3115 }
3116
3117 /* Handlers can provide empty GAP buffers to indicate that the parsed
3118 * content was valid but that nothing meaningful needs to be outputted. In
3119 * such cases we send out a GAP event instead */
3120 if (GST_BUFFER_FLAG_IS_SET (outbuf, GST_BUFFER_FLAG_GAP) &&
3121 gst_buffer_get_size (outbuf) == 0) {
3122 GstEvent *gap = gst_event_new_gap (GST_BUFFER_DTS (outbuf),
3123 GST_BUFFER_DURATION (outbuf));
3124 gst_buffer_unref (outbuf);
3125 GST_DEBUG_OBJECT (pad,
3126 "Replacing empty gap buffer with gap event %" GST_PTR_FORMAT, gap);
3127 gst_pad_push_event (GST_PAD_CAST (pad), gap);
3128 } else {
3129 GST_DEBUG_OBJECT (pad,
3130 "Pushing buffer of size %" G_GSIZE_FORMAT " for track %u: pts %"
3131 GST_TIME_FORMAT " dts %" GST_TIME_FORMAT " duration %" GST_TIME_FORMAT
3132 " position %" G_GUINT64_FORMAT, gst_buffer_get_size (outbuf),
3133 pad->material_track->parent.track_id,
3134 GST_TIME_ARGS (GST_BUFFER_PTS (outbuf)),
3135 GST_TIME_ARGS (GST_BUFFER_DTS (outbuf)),
3136 GST_TIME_ARGS (GST_BUFFER_DURATION (outbuf)),
3137 pad->current_essence_track_position);
3138
3139 ret = gst_pad_push (GST_PAD_CAST (pad), outbuf);
3140 }
3141 outbuf = NULL;
3142 ret = gst_flow_combiner_update_flow (demux->flowcombiner, ret);
3143 GST_LOG_OBJECT (pad, "combined return %s", gst_flow_get_name (ret));
3144
3145 if (time_end > demux->segment.position)
3146 demux->segment.position = time_end;
3147
3148 if (ret != GST_FLOW_OK)
3149 goto out;
3150
3151 pad->current_essence_track_position += index_entry.duration;
3152
3153 if (pad->current_component) {
3154 if ((pad->current_component_duration > 0
3155 && pad->current_component_duration != -1)
3156 && pad->current_essence_track_position -
3157 pad->current_component_start >= pad->current_component_duration) {
3158 GST_DEBUG_OBJECT (demux, "Switching to next component");
3159
3160 ret =
3161 gst_mxf_demux_pad_set_component (demux, pad,
3162 pad->current_component_index + 1);
3163 if (ret == GST_FLOW_OK) {
3164 pad->current_essence_track->position =
3165 pad->current_essence_track_position;
3166 } else if (ret != GST_FLOW_EOS) {
3167 GST_ERROR_OBJECT (demux, "Switching component failed");
3168 }
3169 } else if ((etrack->duration > 0 && etrack->duration != -1)
3170 && pad->current_essence_track_position >= etrack->duration) {
3171 GST_DEBUG_OBJECT (demux,
3172 "Current component position after end of essence track");
3173 ret = GST_FLOW_EOS;
3174 }
3175 } else if ((etrack->duration > 0 && etrack->duration != -1)
3176 && pad->current_essence_track_position == etrack->duration) {
3177 GST_DEBUG_OBJECT (demux, "At the end of the essence track");
3178 ret = GST_FLOW_EOS;
3179 }
3180
3181 if (ret == GST_FLOW_EOS) {
3182 gst_mxf_demux_eos_single_stream (demux, pad);
3183 ret = GST_FLOW_OK;
3184 }
3185
3186 if (ret != GST_FLOW_OK)
3187 goto out;
3188 }
3189
3190 out:
3191 if (inbuf)
3192 gst_buffer_unref (inbuf);
3193
3194 if (outbuf)
3195 gst_buffer_unref (outbuf);
3196
3197 etrack->position += index_entry.duration;
3198
3199 return ret;
3200 }
3201
3202 /*
3203 * Called when analyzing the (RIP) Random Index Pack.
3204 *
3205 * FIXME : If a file doesn't have a RIP, we should iterate the partition headers
3206 * to collect as much information as possible.
3207 *
3208 * This function collects as much information as possible from the partition headers:
3209 * * Store partition information in the list of partitions
3210 * * Handle any index table segment present
3211 */
3212 static void
3213 read_partition_header (GstMXFDemux * demux)
3214 {
3215 GstMXFKLV klv;
3216
3217 if (gst_mxf_demux_peek_klv_packet (demux, demux->offset, &klv) != GST_FLOW_OK
3218 || !mxf_is_partition_pack (&klv.key)) {
3219 return;
3220 }
3221
3222 if (gst_mxf_demux_handle_partition_pack (demux, &klv) != GST_FLOW_OK) {
3223 if (klv.data)
3224 gst_buffer_unref (klv.data);
3225 return;
3226 }
3227 gst_mxf_demux_consume_klv (demux, &klv);
3228
3229 if (gst_mxf_demux_peek_klv_packet (demux, demux->offset, &klv) != GST_FLOW_OK)
3230 return;
3231
3232 while (mxf_is_fill (&klv.key)) {
3233 gst_mxf_demux_consume_klv (demux, &klv);
3234 if (gst_mxf_demux_peek_klv_packet (demux, demux->offset,
3235 &klv) != GST_FLOW_OK)
3236 return;
3237 }
3238
3239 if (!mxf_is_index_table_segment (&klv.key)
3240 && demux->current_partition->partition.header_byte_count) {
3241 demux->offset += demux->current_partition->partition.header_byte_count;
3242 if (gst_mxf_demux_peek_klv_packet (demux, demux->offset,
3243 &klv) != GST_FLOW_OK)
3244 return;
3245 }
3246
3247 while (mxf_is_fill (&klv.key)) {
3248 gst_mxf_demux_consume_klv (demux, &klv);
3249 if (gst_mxf_demux_peek_klv_packet (demux, demux->offset,
3250 &klv) != GST_FLOW_OK)
3251 return;
3252 }
3253
3254 if (demux->current_partition->partition.index_byte_count
3255 && mxf_is_index_table_segment (&klv.key)) {
3256 guint64 index_end_offset =
3257 demux->offset + demux->current_partition->partition.index_byte_count;
3258
3259 while (demux->offset < index_end_offset) {
3260 if (mxf_is_index_table_segment (&klv.key))
3261 gst_mxf_demux_handle_index_table_segment (demux, &klv);
3262 gst_mxf_demux_consume_klv (demux, &klv);
3263
3264 if (gst_mxf_demux_peek_klv_packet (demux, demux->offset,
3265 &klv) != GST_FLOW_OK)
3266 return;
3267 }
3268 }
3269
3270 while (mxf_is_fill (&klv.key)) {
3271 gst_mxf_demux_consume_klv (demux, &klv);
3272 if (gst_mxf_demux_peek_klv_packet (demux, demux->offset,
3273 &klv) != GST_FLOW_OK)
3274 return;
3275 }
3276
3277 if (mxf_is_generic_container_system_item (&klv.key) ||
3278 mxf_is_generic_container_essence_element (&klv.key) ||
3279 mxf_is_avid_essence_container_essence_element (&klv.key)) {
3280 if (demux->current_partition->essence_container_offset == 0)
3281 demux->current_partition->essence_container_offset =
3282 demux->offset - demux->current_partition->partition.this_partition -
3283 demux->run_in;
3284 }
3285 }
3286
3287 static GstFlowReturn
3288 gst_mxf_demux_handle_random_index_pack (GstMXFDemux * demux, GstMXFKLV * klv)
3289 {
3290 guint i;
3291 GList *l;
3292 GstMapInfo map;
3293 gboolean ret;
3294 GstFlowReturn flowret;
3295
3296 GST_DEBUG_OBJECT (demux,
3297 "Handling random index pack of size %" G_GSIZE_FORMAT " at offset %"
3298 G_GUINT64_FORMAT, klv->length, klv->offset);
3299
3300 if (demux->random_index_pack) {
3301 GST_DEBUG_OBJECT (demux, "Already parsed random index pack");
3302 return GST_FLOW_OK;
3303 }
3304
3305 flowret = gst_mxf_demux_fill_klv (demux, klv);
3306 if (flowret != GST_FLOW_OK)
3307 return flowret;
3308
3309 gst_buffer_map (klv->data, &map, GST_MAP_READ);
3310 ret =
3311 mxf_random_index_pack_parse (&klv->key, map.data, map.size,
3312 &demux->random_index_pack);
3313 gst_buffer_unmap (klv->data, &map);
3314
3315 if (!ret) {
3316 GST_ERROR_OBJECT (demux, "Parsing random index pack failed");
3317 return GST_FLOW_ERROR;
3318 }
3319
3320 for (i = 0; i < demux->random_index_pack->len; i++) {
3321 GstMXFDemuxPartition *p = NULL;
3322 MXFRandomIndexPackEntry *e =
3323 &g_array_index (demux->random_index_pack, MXFRandomIndexPackEntry, i);
3324
3325 if (e->offset < demux->run_in) {
3326 GST_ERROR_OBJECT (demux, "Invalid random index pack entry");
3327 return GST_FLOW_ERROR;
3328 }
3329
3330 for (l = demux->partitions; l; l = l->next) {
3331 GstMXFDemuxPartition *tmp = l->data;
3332
3333 if (tmp->partition.this_partition + demux->run_in == e->offset) {
3334 p = tmp;
3335 break;
3336 }
3337 }
3338
3339 if (!p) {
3340 p = g_new0 (GstMXFDemuxPartition, 1);
3341 p->partition.this_partition = e->offset - demux->run_in;
3342 p->partition.body_sid = e->body_sid;
3343 demux->partitions =
3344 g_list_insert_sorted (demux->partitions, p,
3345 (GCompareFunc) gst_mxf_demux_partition_compare);
3346 }
3347 }
3348
3349 for (l = demux->partitions; l; l = l->next) {
3350 GstMXFDemuxPartition *a, *b;
3351
3352 if (l->next == NULL)
3353 break;
3354
3355 a = l->data;
3356 b = l->next->data;
3357
3358 b->partition.prev_partition = a->partition.this_partition;
3359 }
3360
3361 return GST_FLOW_OK;
3362 }
3363
3364 static gint
3365 compare_index_table_segment (MXFIndexTableSegment * sa,
3366 MXFIndexTableSegment * sb)
3367 {
3368 if (sa->body_sid != sb->body_sid)
3369 return (sa->body_sid < sb->body_sid) ? -1 : 1;
3370 if (sa->index_sid != sb->index_sid)
3371 return (sa->index_sid < sb->index_sid) ? -1 : 1;
3372 if (sa->index_start_position != sb->index_start_position)
3373 return (sa->index_start_position < sb->index_start_position) ? -1 : 1;
3374
3375 /* If all the above are equal ... the index table segments are only equal if
3376 * their instance ID are equal. Until March 2022 the FFmpeg MXF muxer would
3377 * write the same instance id for the various (different) index table
3378 * segments, we therefore only check instance ID *after* all the above
3379 * properties to make sure they are really different. */
3380 if (mxf_uuid_is_equal (&sa->instance_id, &sb->instance_id))
3381 return 0;
3382
3383 return 1;
3384 }
3385
3386 static GstFlowReturn
3387 gst_mxf_demux_handle_index_table_segment (GstMXFDemux * demux, GstMXFKLV * klv)
3388 {
3389 MXFIndexTableSegment *segment;
3390 GstMapInfo map;
3391 gboolean ret;
3392 GList *tmp;
3393 GstFlowReturn flowret;
3394
3395 flowret = gst_mxf_demux_fill_klv (demux, klv);
3396 if (flowret != GST_FLOW_OK)
3397 return flowret;
3398
3399 GST_DEBUG_OBJECT (demux,
3400 "Handling index table segment of size %" G_GSIZE_FORMAT " at offset %"
3401 G_GUINT64_FORMAT, klv->length, klv->offset);
3402
3403 segment = g_new0 (MXFIndexTableSegment, 1);
3404
3405 gst_buffer_map (klv->data, &map, GST_MAP_READ);
3406 ret = mxf_index_table_segment_parse (&klv->key, segment, map.data, map.size);
3407 gst_buffer_unmap (klv->data, &map);
3408
3409 if (!ret) {
3410 GST_ERROR_OBJECT (demux, "Parsing index table segment failed");
3411 g_free (segment);
3412 return GST_FLOW_ERROR;
3413 }
3414
3415 /* Drop it if we already saw it. Ideally we should be able to do this before
3416 parsing (by checking instance UID) */
3417 if (g_list_find_custom (demux->pending_index_table_segments, segment,
3418 (GCompareFunc) compare_index_table_segment)) {
3419 GST_DEBUG_OBJECT (demux, "Already in pending list");
3420 mxf_index_table_segment_reset (segment);
3421 g_free (segment);
3422 return GST_FLOW_OK;
3423 }
3424 for (tmp = demux->index_tables; tmp; tmp = tmp->next) {
3425 GstMXFDemuxIndexTable *table = (GstMXFDemuxIndexTable *) tmp->data;
3426 if (g_array_binary_search (table->segments, segment,
3427 (GCompareFunc) compare_index_table_segment, NULL)) {
3428 GST_DEBUG_OBJECT (demux, "Already handled");
3429 mxf_index_table_segment_reset (segment);
3430 g_free (segment);
3431 return GST_FLOW_OK;
3432 }
3433 }
3434
3435 demux->pending_index_table_segments =
3436 g_list_insert_sorted (demux->pending_index_table_segments, segment,
3437 (GCompareFunc) compare_index_table_segment);
3438
3439 return GST_FLOW_OK;
3440 }
3441
3442 static GstFlowReturn
3443 gst_mxf_demux_peek_klv_packet (GstMXFDemux * demux, guint64 offset,
3444 GstMXFKLV * klv)
3445 {
3446 GstBuffer *buffer = NULL;
3447 const guint8 *data;
3448 GstFlowReturn ret = GST_FLOW_OK;
3449 GstMapInfo map;
3450 #ifndef GST_DISABLE_GST_DEBUG
3451 gchar str[48];
3452 #endif
3453
3454 memset (klv, 0, sizeof (GstMXFKLV));
3455 klv->offset = offset;
3456
3457 /* Pull 16 byte key and first byte of BER encoded length */
3458 if ((ret =
3459 gst_mxf_demux_pull_range (demux, offset, 17, &buffer)) != GST_FLOW_OK)
3460 goto beach;
3461
3462 gst_buffer_map (buffer, &map, GST_MAP_READ);
3463
3464 memcpy (&klv->key, map.data, 16);
3465
3466 /* Decode BER encoded packet length */
3467 if ((map.data[16] & 0x80) == 0) {
3468 klv->length = map.data[16];
3469 klv->data_offset = 17;
3470 } else {
3471 guint slen = map.data[16] & 0x7f;
3472
3473 klv->data_offset = 16 + 1 + slen;
3474
3475 gst_buffer_unmap (buffer, &map);
3476 gst_buffer_unref (buffer);
3477 buffer = NULL;
3478
3479 /* Must be at most 8 according to SMPTE-379M 5.3.4 */
3480 if (slen > 8) {
3481 GST_ERROR_OBJECT (demux, "Invalid KLV packet length: %u", slen);
3482 ret = GST_FLOW_ERROR;
3483 goto beach;
3484 }
3485
3486 /* Now pull the length of the packet */
3487 if ((ret = gst_mxf_demux_pull_range (demux, offset + 17, slen,
3488 &buffer)) != GST_FLOW_OK)
3489 goto beach;
3490
3491 gst_buffer_map (buffer, &map, GST_MAP_READ);
3492
3493 data = map.data;
3494 klv->length = 0;
3495 while (slen) {
3496 klv->length = (klv->length << 8) | *data;
3497 data++;
3498 slen--;
3499 }
3500 }
3501
3502 gst_buffer_unmap (buffer, &map);
3503 gst_buffer_unref (buffer);
3504 buffer = NULL;
3505
3506 /* GStreamer's buffer sizes are stored in a guint so we
3507 * limit ourself to G_MAXUINT large buffers */
3508 if (klv->length > G_MAXUINT) {
3509 GST_ERROR_OBJECT (demux,
3510 "Unsupported KLV packet length: %" G_GSIZE_FORMAT, klv->length);
3511 ret = GST_FLOW_ERROR;
3512 goto beach;
3513 }
3514
3515 GST_DEBUG_OBJECT (demux,
3516 "Found KLV packet at offset %" G_GUINT64_FORMAT " with key %s and length "
3517 "%" G_GSIZE_FORMAT, offset, mxf_ul_to_string (&klv->key, str),
3518 klv->length);
3519
3520 beach:
3521 if (buffer)
3522 gst_buffer_unref (buffer);
3523
3524 return ret;
3525 }
3526
3527 static GstFlowReturn
3528 gst_mxf_demux_fill_klv (GstMXFDemux * demux, GstMXFKLV * klv)
3529 {
3530 if (klv->data)
3531 return GST_FLOW_OK;
3532 GST_DEBUG_OBJECT (demux,
3533 "Pulling %" G_GSIZE_FORMAT " bytes from offset %" G_GUINT64_FORMAT,
3534 klv->length, klv->offset + klv->data_offset);
3535 return gst_mxf_demux_pull_range (demux, klv->offset + klv->data_offset,
3536 klv->length, &klv->data);
3537 }
3538
3539 /* Call when done with a klv. Will release the buffer (if any) and will update
3540 * the demuxer offset position. Do *NOT* call if you do not want the demuxer
3541 * offset to be updated */
3542 static void
3543 gst_mxf_demux_consume_klv (GstMXFDemux * demux, GstMXFKLV * klv)
3544 {
3545 if (klv->data) {
3546 gst_buffer_unref (klv->data);
3547 klv->data = NULL;
3548 }
3549 GST_DEBUG_OBJECT (demux,
3550 "Consuming KLV offset:%" G_GUINT64_FORMAT " data_offset:%"
3551 G_GUINT64_FORMAT " length:%" G_GSIZE_FORMAT " consumed:%"
3552 G_GUINT64_FORMAT, klv->offset, klv->data_offset, klv->length,
3553 klv->consumed);
3554 if (klv->consumed)
3555 demux->offset = klv->offset + klv->consumed;
3556 else
3557 demux->offset += klv->data_offset + klv->length;
3558 }
3559
3560 static void
3561 gst_mxf_demux_pull_random_index_pack (GstMXFDemux * demux)
3562 {
3563 GstBuffer *buffer;
3564 guint64 filesize = -1;
3565 GstFormat fmt = GST_FORMAT_BYTES;
3566 guint32 pack_size;
3567 guint64 old_offset = demux->offset;
3568 GstMapInfo map;
3569 GstFlowReturn flow_ret;
3570 GstMXFKLV klv;
3571
3572 if (!gst_pad_peer_query_duration (demux->sinkpad, fmt, (gint64 *) & filesize)
3573 || fmt != GST_FORMAT_BYTES || filesize == -1) {
3574 GST_DEBUG_OBJECT (demux, "Can't query upstream size");
3575 return;
3576 }
3577
3578 g_assert (filesize > 4);
3579
3580 buffer = NULL;
3581 if (gst_mxf_demux_pull_range (demux, filesize - 4, 4, &buffer) != GST_FLOW_OK) {
3582 GST_DEBUG_OBJECT (demux, "Failed pulling last 4 bytes");
3583 return;
3584 }
3585
3586 gst_buffer_map (buffer, &map, GST_MAP_READ);
3587 pack_size = GST_READ_UINT32_BE (map.data);
3588 gst_buffer_unmap (buffer, &map);
3589
3590 gst_buffer_unref (buffer);
3591
3592 if (pack_size < 20) {
3593 GST_DEBUG_OBJECT (demux, "Too small pack size (%u bytes)", pack_size);
3594 return;
3595 } else if (pack_size > filesize - 20) {
3596 GST_DEBUG_OBJECT (demux, "Too large pack size (%u bytes)", pack_size);
3597 return;
3598 }
3599
3600 /* Peek for klv at filesize - pack_size */
3601 if (gst_mxf_demux_peek_klv_packet (demux, filesize - pack_size,
3602 &klv) != GST_FLOW_OK) {
3603 GST_DEBUG_OBJECT (demux, "Failed pulling random index pack key");
3604 return;
3605 }
3606
3607 if (!mxf_is_random_index_pack (&klv.key)) {
3608 GST_DEBUG_OBJECT (demux, "No random index pack");
3609 return;
3610 }
3611
3612 demux->offset = filesize - pack_size;
3613 flow_ret = gst_mxf_demux_handle_random_index_pack (demux, &klv);
3614 if (klv.data)
3615 gst_buffer_unref (klv.data);
3616 demux->offset = old_offset;
3617
3618 if (flow_ret == GST_FLOW_OK && !demux->index_table_segments_collected) {
3619 collect_index_table_segments (demux);
3620 demux->index_table_segments_collected = TRUE;
3621 }
3622 }
3623
3624 static void
3625 gst_mxf_demux_parse_footer_metadata (GstMXFDemux * demux)
3626 {
3627 guint64 old_offset = demux->offset;
3628 GstMXFKLV klv;
3629 GstFlowReturn flow = GST_FLOW_OK;
3630 GstMXFDemuxPartition *old_partition = demux->current_partition;
3631
3632 GST_DEBUG_OBJECT (demux, "Parsing footer metadata");
3633
3634 demux->current_partition = NULL;
3635
3636 gst_mxf_demux_reset_metadata (demux);
3637
3638 if (demux->footer_partition_pack_offset != 0) {
3639 demux->offset = demux->run_in + demux->footer_partition_pack_offset;
3640 } else {
3641 MXFRandomIndexPackEntry *entry =
3642 &g_array_index (demux->random_index_pack, MXFRandomIndexPackEntry,
3643 demux->random_index_pack->len - 1);
3644 demux->offset = entry->offset;
3645 }
3646
3647 next_try:
3648 GST_LOG_OBJECT (demux, "Peeking partition pack at offset %" G_GUINT64_FORMAT,
3649 demux->offset);
3650
3651 /* Process Partition Pack */
3652 flow = gst_mxf_demux_peek_klv_packet (demux, demux->offset, &klv);
3653 if (G_UNLIKELY (flow != GST_FLOW_OK))
3654 goto out;
3655
3656 if (!mxf_is_partition_pack (&klv.key))
3657 goto out;
3658
3659 if (gst_mxf_demux_handle_partition_pack (demux, &klv) != GST_FLOW_OK) {
3660 if (klv.data)
3661 gst_buffer_unref (klv.data);
3662 goto out;
3663 }
3664
3665 gst_mxf_demux_consume_klv (demux, &klv);
3666
3667 /* If there's no Header Metadata in this partition, jump to the previous
3668 * one */
3669 if (demux->current_partition->partition.header_byte_count == 0) {
3670 /* Reached the first partition, bail out */
3671 if (demux->current_partition->partition.this_partition == 0)
3672 goto out;
3673
3674 demux->offset =
3675 demux->run_in + demux->current_partition->partition.prev_partition;
3676 goto next_try;
3677 }
3678
3679 /* Next up should be an optional fill pack followed by a primer pack */
3680 while (TRUE) {
3681 flow = gst_mxf_demux_peek_klv_packet (demux, demux->offset, &klv);
3682 if (G_UNLIKELY (flow != GST_FLOW_OK)) {
3683 /* If ever we can't get the next KLV, jump to the previous partition */
3684 if (!demux->current_partition->partition.prev_partition)
3685 goto out;
3686 demux->offset =
3687 demux->run_in + demux->current_partition->partition.prev_partition;
3688 goto next_try;
3689 }
3690
3691 if (mxf_is_fill (&klv.key)) {
3692 gst_mxf_demux_consume_klv (demux, &klv);
3693 } else if (mxf_is_primer_pack (&klv.key)) {
3694 /* Update primer mapping if present (jump to previous if it failed) */
3695 if (!demux->current_partition->primer.mappings) {
3696 if (gst_mxf_demux_handle_primer_pack (demux, &klv) != GST_FLOW_OK) {
3697 gst_mxf_demux_consume_klv (demux, &klv);
3698 if (!demux->current_partition->partition.prev_partition)
3699 goto out;
3700 demux->offset =
3701 demux->run_in +
3702 demux->current_partition->partition.prev_partition;
3703 goto next_try;
3704 }
3705 }
3706 gst_mxf_demux_consume_klv (demux, &klv);
3707 break;
3708 } else {
3709 if (!demux->current_partition->partition.prev_partition)
3710 goto out;
3711 demux->offset =
3712 demux->run_in + demux->current_partition->partition.prev_partition;
3713 goto next_try;
3714 }
3715 }
3716
3717 /* parse metadata for this partition */
3718 while (demux->offset <
3719 demux->run_in + demux->current_partition->primer.offset +
3720 demux->current_partition->partition.header_byte_count) {
3721 flow = gst_mxf_demux_peek_klv_packet (demux, demux->offset, &klv);
3722 if (G_UNLIKELY (flow != GST_FLOW_OK)) {
3723 if (!demux->current_partition->partition.prev_partition)
3724 goto out;
3725 demux->offset =
3726 demux->run_in + demux->current_partition->partition.prev_partition;
3727 goto next_try;
3728 }
3729
3730 if (mxf_is_metadata (&klv.key)) {
3731 flow = gst_mxf_demux_handle_metadata (demux, &klv);
3732 gst_mxf_demux_consume_klv (demux, &klv);
3733
3734 if (G_UNLIKELY (flow != GST_FLOW_OK)) {
3735 gst_mxf_demux_reset_metadata (demux);
3736 if (!demux->current_partition->partition.prev_partition)
3737 goto out;
3738 demux->offset =
3739 demux->run_in + demux->current_partition->partition.prev_partition;
3740 goto next_try;
3741 }
3742 } else if (mxf_is_descriptive_metadata (&klv.key)) {
3743 gst_mxf_demux_handle_descriptive_metadata (demux, &klv);
3744 gst_mxf_demux_consume_klv (demux, &klv);
3745 } else {
3746 gst_mxf_demux_consume_klv (demux, &klv);
3747 }
3748 }
3749
3750 /* resolve references etc */
3751 if (!demux->preface || gst_mxf_demux_resolve_references (demux) !=
3752 GST_FLOW_OK || gst_mxf_demux_update_tracks (demux) != GST_FLOW_OK) {
3753 /* Don't attempt to parse metadata from this partition again */
3754 demux->current_partition->parsed_metadata = TRUE;
3755 /* Skip to previous partition or bail out */
3756 if (!demux->current_partition->partition.prev_partition)
3757 goto out;
3758 demux->offset =
3759 demux->run_in + demux->current_partition->partition.prev_partition;
3760 goto next_try;
3761 }
3762
3763 out:
3764 demux->offset = old_offset;
3765 demux->current_partition = old_partition;
3766 }
3767
3768 static GstFlowReturn
3769 gst_mxf_demux_handle_klv_packet (GstMXFDemux * demux, GstMXFKLV * klv,
3770 gboolean peek)
3771 {
3772 MXFUL *key = &klv->key;
3773 #ifndef GST_DISABLE_GST_DEBUG
3774 gchar key_str[48];
3775 #endif
3776 GstFlowReturn ret = GST_FLOW_OK;
3777
3778 if (demux->update_metadata
3779 && demux->preface
3780 && (demux->offset >=
3781 demux->run_in + demux->current_partition->primer.offset +
3782 demux->current_partition->partition.header_byte_count ||
3783 mxf_is_generic_container_system_item (key) ||
3784 mxf_is_generic_container_essence_element (key) ||
3785 mxf_is_avid_essence_container_essence_element (key))) {
3786 demux->current_partition->parsed_metadata = TRUE;
3787 if ((ret = gst_mxf_demux_resolve_references (demux)) != GST_FLOW_OK ||
3788 (ret = gst_mxf_demux_update_tracks (demux)) != GST_FLOW_OK) {
3789 goto beach;
3790 }
3791 } else if (demux->metadata_resolved && demux->requested_package_string) {
3792 if ((ret = gst_mxf_demux_update_tracks (demux)) != GST_FLOW_OK) {
3793 goto beach;
3794 }
3795 }
3796
3797 if (!mxf_is_mxf_packet (key)) {
3798 GST_WARNING_OBJECT (demux,
3799 "Skipping non-MXF packet of size %" G_GSIZE_FORMAT " at offset %"
3800 G_GUINT64_FORMAT ", key: %s", klv->length,
3801 demux->offset, mxf_ul_to_string (key, key_str));
3802 } else if (mxf_is_partition_pack (key)) {
3803 ret = gst_mxf_demux_handle_partition_pack (demux, klv);
3804 } else if (mxf_is_primer_pack (key)) {
3805 ret = gst_mxf_demux_handle_primer_pack (demux, klv);
3806 } else if (mxf_is_metadata (key)) {
3807 ret = gst_mxf_demux_handle_metadata (demux, klv);
3808 } else if (mxf_is_descriptive_metadata (key)) {
3809 ret = gst_mxf_demux_handle_descriptive_metadata (demux, klv);
3810 } else if (mxf_is_generic_container_system_item (key)) {
3811 if (demux->pending_index_table_segments)
3812 collect_index_table_segments (demux);
3813 ret = gst_mxf_demux_handle_generic_container_system_item (demux, klv);
3814 } else if (mxf_is_generic_container_essence_element (key) ||
3815 mxf_is_avid_essence_container_essence_element (key)) {
3816 if (demux->pending_index_table_segments)
3817 collect_index_table_segments (demux);
3818 ret =
3819 gst_mxf_demux_handle_generic_container_essence_element (demux, klv,
3820 peek);
3821 } else if (mxf_is_random_index_pack (key)) {
3822 ret = gst_mxf_demux_handle_random_index_pack (demux, klv);
3823
3824 if (ret == GST_FLOW_OK && demux->random_access
3825 && !demux->index_table_segments_collected) {
3826 collect_index_table_segments (demux);
3827 demux->index_table_segments_collected = TRUE;
3828 }
3829 } else if (mxf_is_index_table_segment (key)) {
3830 ret = gst_mxf_demux_handle_index_table_segment (demux, klv);
3831 } else if (mxf_is_fill (key)) {
3832 GST_DEBUG_OBJECT (demux,
3833 "Skipping filler packet of size %" G_GSIZE_FORMAT " at offset %"
3834 G_GUINT64_FORMAT, klv->length, demux->offset);
3835 } else {
3836 GST_DEBUG_OBJECT (demux,
3837 "Skipping unknown packet of size %" G_GSIZE_FORMAT " at offset %"
3838 G_GUINT64_FORMAT ", key: %s", klv->length,
3839 demux->offset, mxf_ul_to_string (key, key_str));
3840 }
3841
3842 beach:
3843 return ret;
3844 }
3845
3846 static void
3847 gst_mxf_demux_set_partition_for_offset (GstMXFDemux * demux, guint64 offset)
3848 {
3849 GList *l;
3850
3851 GST_LOG_OBJECT (demux, "offset %" G_GUINT64_FORMAT, offset);
3852
3853 /* This partition will already be parsed, otherwise
3854 * the position wouldn't be in the index */
3855 for (l = demux->partitions; l; l = l->next) {
3856 GstMXFDemuxPartition *p = l->data;
3857
3858 if (p->partition.this_partition + demux->run_in <= offset)
3859 demux->current_partition = p;
3860 }
3861 if (demux->current_partition)
3862 GST_DEBUG_OBJECT (demux,
3863 "Current partition now %p (body_sid:%u index_sid:%u this_partition:%"
3864 G_GUINT64_FORMAT ")", demux->current_partition,
3865 demux->current_partition->partition.body_sid,
3866 demux->current_partition->partition.index_sid,
3867 demux->current_partition->partition.this_partition);
3868 else
3869 GST_DEBUG_OBJECT (demux, "Haven't found partition for offset yet");
3870 }
3871
3872 static guint64
3873 find_closest_offset (GArray * offsets, guint64 * position, gboolean keyframe)
3874 {
3875 GstMXFDemuxIndex *idx;
3876 guint64 current_position = *position;
3877
3878 if (!offsets || offsets->len == 0)
3879 return -1;
3880
3881 current_position = MIN (current_position, offsets->len - 1);
3882
3883 idx = &g_array_index (offsets, GstMXFDemuxIndex, current_position);
3884 while ((idx->offset == 0 || (keyframe && !idx->keyframe))
3885 && current_position > 0) {
3886 current_position--;
3887 idx = &g_array_index (offsets, GstMXFDemuxIndex, current_position);
3888 }
3889
3890 if (idx->offset != 0 && (!keyframe || idx->keyframe)) {
3891 *position = current_position;
3892 return idx->offset;
3893 }
3894
3895 return -1;
3896 }
3897
3898 static guint64
3899 gst_mxf_demux_find_essence_element (GstMXFDemux * demux,
3900 GstMXFDemuxEssenceTrack * etrack, guint64 * position, gboolean keyframe)
3901 {
3902 GstFlowReturn ret = GST_FLOW_OK;
3903 guint64 old_offset = demux->offset;
3904 GstMXFDemuxPartition *old_partition = demux->current_partition;
3905 gint i;
3906 guint64 offset;
3907 guint64 requested_position = *position, index_start_position;
3908 GstMXFDemuxIndex index_entry = { 0, };
3909
3910 GST_DEBUG_OBJECT (demux, "Trying to find essence element %" G_GUINT64_FORMAT
3911 " of track 0x%08x with body_sid %u (keyframe %d)", *position,
3912 etrack->track_number, etrack->body_sid, keyframe);
3913
3914 /* Get entry from index table if present */
3915 if (find_edit_entry (demux, etrack, *position, keyframe, &index_entry)) {
3916 GST_DEBUG_OBJECT (demux,
3917 "Got position %" G_GUINT64_FORMAT " at offset %" G_GUINT64_FORMAT,
3918 index_entry.dts, index_entry.offset);
3919 *position = index_entry.dts;
3920 return index_entry.offset;
3921 }
3922
3923 GST_DEBUG_OBJECT (demux, "Not found in index table");
3924
3925 /* Fallback to track offsets */
3926
3927 if (!demux->random_access) {
3928 /* Best effort for push mode */
3929 offset = find_closest_offset (etrack->offsets, position, keyframe);
3930 if (offset != -1)
3931 GST_DEBUG_OBJECT (demux,
3932 "Starting with edit unit %" G_GUINT64_FORMAT " for %" G_GUINT64_FORMAT
3933 " in generated index at offset %" G_GUINT64_FORMAT, *position,
3934 requested_position, offset);
3935 return offset;
3936 }
3937
3938 if ((etrack->duration > 0 && etrack->duration != -1)
3939 && *position >= etrack->duration) {
3940 GST_DEBUG_OBJECT (demux, "Position after end of essence track");
3941 return -1;
3942 }
3943
3944 from_track_offset:
3945
3946 index_start_position = *position;
3947
3948 demux->offset = demux->run_in;
3949
3950 offset = find_closest_offset (etrack->offsets, &index_start_position, FALSE);
3951 if (offset != -1) {
3952 demux->offset = offset + demux->run_in;
3953 GST_DEBUG_OBJECT (demux,
3954 "Starting with edit unit %" G_GUINT64_FORMAT " for %" G_GUINT64_FORMAT
3955 " in generated index at offset %" G_GUINT64_FORMAT,
3956 index_start_position, requested_position, offset);
3957 } else {
3958 index_start_position = -1;
3959 }
3960
3961 gst_mxf_demux_set_partition_for_offset (demux, demux->offset);
3962
3963 for (i = 0; i < demux->essence_tracks->len; i++) {
3964 GstMXFDemuxEssenceTrack *t = g_ptr_array_index (demux->essence_tracks, i);
3965
3966 if (index_start_position != -1 && t == etrack)
3967 t->position = index_start_position;
3968 else
3969 t->position = (demux->offset == demux->run_in) ? 0 : -1;
3970 GST_LOG_OBJECT (demux, "Setting track %u position to %" G_GUINT64_FORMAT,
3971 t->track_id, t->position);
3972 }
3973
3974 /* Else peek at all essence elements and complete our
3975 * index until we find the requested element
3976 */
3977 while (ret == GST_FLOW_OK) {
3978 GstMXFKLV klv;
3979
3980 GST_LOG_OBJECT (demux, "Pulling from offset %" G_GUINT64_FORMAT,
3981 demux->offset);
3982 ret = gst_mxf_demux_peek_klv_packet (demux, demux->offset, &klv);
3983
3984 if (ret == GST_FLOW_EOS) {
3985 /* Handle EOS */
3986 for (i = 0; i < demux->essence_tracks->len; i++) {
3987 GstMXFDemuxEssenceTrack *t =
3988 g_ptr_array_index (demux->essence_tracks, i);
3989
3990 if (t->position > 0 && t->position != -1)
3991 t->duration = t->position;
3992 }
3993 /* For the searched track this is really our position */
3994 etrack->duration = etrack->position;
3995
3996 for (i = 0; i < demux->src->len; i++) {
3997 GstMXFDemuxPad *p = g_ptr_array_index (demux->src, i);
3998
3999 if (!p->eos
4000 && p->current_essence_track_position >=
4001 p->current_essence_track->duration) {
4002 gst_mxf_demux_eos_single_stream (demux, p);
4003 }
4004 }
4005 }
4006
4007 GST_LOG_OBJECT (demux,
4008 "pulling gave flow:%s track->position:%" G_GUINT64_FORMAT,
4009 gst_flow_get_name (ret), etrack->position);
4010 if (G_UNLIKELY (ret != GST_FLOW_OK) && etrack->position <= *position) {
4011 demux->offset = old_offset;
4012 demux->current_partition = old_partition;
4013 break;
4014 } else if (G_UNLIKELY (ret == GST_FLOW_OK)) {
4015 ret = gst_mxf_demux_handle_klv_packet (demux, &klv, TRUE);
4016 gst_mxf_demux_consume_klv (demux, &klv);
4017 }
4018
4019 GST_LOG_OBJECT (demux,
4020 "Handling gave flow:%s track->position:%" G_GUINT64_FORMAT
4021 " looking for %" G_GUINT64_FORMAT, gst_flow_get_name (ret),
4022 etrack->position, *position);
4023
4024 /* If we found the position read it from the index again */
4025 if (((ret == GST_FLOW_OK && etrack->position == *position + 1) ||
4026 (ret == GST_FLOW_EOS && etrack->position == *position + 1))
4027 && etrack->offsets && etrack->offsets->len > *position
4028 && g_array_index (etrack->offsets, GstMXFDemuxIndex,
4029 *position).offset != 0) {
4030 GST_DEBUG_OBJECT (demux, "Found at offset %" G_GUINT64_FORMAT,
4031 demux->offset);
4032 demux->offset = old_offset;
4033 demux->current_partition = old_partition;
4034 if (find_edit_entry (demux, etrack, *position, keyframe, &index_entry)) {
4035 GST_DEBUG_OBJECT (demux,
4036 "Got position %" G_GUINT64_FORMAT " at offset %" G_GUINT64_FORMAT,
4037 index_entry.dts, index_entry.offset);
4038 *position = index_entry.dts;
4039 return index_entry.offset;
4040 }
4041 goto from_track_offset;
4042 }
4043 }
4044 demux->offset = old_offset;
4045 demux->current_partition = old_partition;
4046
4047 GST_DEBUG_OBJECT (demux, "Not found in this file");
4048
4049 return -1;
4050 }
4051
4052 static GstFlowReturn
4053 gst_mxf_demux_pull_and_handle_klv_packet (GstMXFDemux * demux)
4054 {
4055 GstMXFKLV klv;
4056 GstFlowReturn ret = GST_FLOW_OK;
4057 gboolean force_switch = FALSE;
4058
4059 if (demux->src->len > 0) {
4060 if (!gst_mxf_demux_get_earliest_pad (demux)) {
4061 ret = GST_FLOW_EOS;
4062 GST_DEBUG_OBJECT (demux, "All tracks are EOS");
4063 goto beach;
4064 }
4065 }
4066
4067 if (demux->state == GST_MXF_DEMUX_STATE_ESSENCE) {
4068 g_assert (demux->current_partition->single_track
4069 && demux->current_partition->single_track->wrapping !=
4070 MXF_ESSENCE_WRAPPING_FRAME_WRAPPING);
4071 /* Feeding essence directly (i.e. in the middle of a custom/clip KLV) */
4072 ret =
4073 gst_mxf_demux_handle_generic_container_essence_element (demux,
4074 &demux->current_partition->clip_klv, FALSE);
4075 gst_mxf_demux_consume_klv (demux, &demux->current_partition->clip_klv);
4076 if (ret == GST_FLOW_OK
4077 && demux->current_partition->single_track->position >=
4078 demux->current_partition->single_track->duration) {
4079 /* We are done with the contents of this clip/custom wrapping, force the
4080 * switch to the next non-EOS track */
4081 GST_DEBUG_OBJECT (demux, "Single track EOS, switch");
4082 force_switch = TRUE;
4083 }
4084
4085 } else {
4086
4087 ret = gst_mxf_demux_peek_klv_packet (demux, demux->offset, &klv);
4088
4089 /* FIXME
4090 *
4091 * Move this EOS handling to a separate function
4092 */
4093 if (ret == GST_FLOW_EOS && demux->src->len > 0) {
4094 guint i;
4095 GstMXFDemuxPad *p = NULL;
4096
4097 GST_DEBUG_OBJECT (demux, "EOS HANDLING");
4098
4099 for (i = 0; i < demux->src->len; i++) {
4100 GstMXFDemuxPad *p = g_ptr_array_index (demux->src, i);
4101
4102 GST_DEBUG_OBJECT (p,
4103 "eos:%d current_essence_track_position:%" G_GUINT64_FORMAT
4104 " position:%" G_GUINT64_FORMAT " duration:%" G_GUINT64_FORMAT,
4105 p->eos, p->current_essence_track_position,
4106 p->current_essence_track->position,
4107 p->current_essence_track->duration);
4108 if (!p->eos
4109 && p->current_essence_track->position >=
4110 p->current_essence_track->duration) {
4111 gst_mxf_demux_eos_single_stream (demux, p);
4112 }
4113 }
4114
4115 while ((p = gst_mxf_demux_get_earliest_pad (demux))) {
4116 guint64 offset;
4117 guint64 position;
4118
4119 GST_DEBUG_OBJECT (p, "Trying on earliest");
4120
4121 position = p->current_essence_track_position;
4122
4123 offset =
4124 gst_mxf_demux_find_essence_element (demux, p->current_essence_track,
4125 &position, FALSE);
4126 if (offset == -1) {
4127 GST_ERROR_OBJECT (demux, "Failed to find offset for essence track");
4128 gst_mxf_demux_eos_single_stream (demux, p);
4129 continue;
4130 }
4131
4132 demux->offset = offset + demux->run_in;
4133 gst_mxf_demux_set_partition_for_offset (demux, demux->offset);
4134 if (p->current_essence_track->wrapping !=
4135 MXF_ESSENCE_WRAPPING_FRAME_WRAPPING) {
4136 demux->state = GST_MXF_DEMUX_STATE_ESSENCE;
4137 demux->current_partition->clip_klv.consumed =
4138 offset - demux->current_partition->clip_klv.offset;
4139 } else
4140 demux->state = GST_MXF_DEMUX_STATE_KLV;
4141 p->current_essence_track->position = position;
4142
4143 ret = GST_FLOW_OK;
4144 goto beach;
4145 }
4146 }
4147 if (G_UNLIKELY (ret != GST_FLOW_OK))
4148 goto beach;
4149
4150 ret = gst_mxf_demux_handle_klv_packet (demux, &klv, FALSE);
4151 gst_mxf_demux_consume_klv (demux, &klv);
4152
4153 /* We entered a new partition */
4154 if (ret == GST_FLOW_OK && mxf_is_partition_pack (&klv.key)) {
4155 GstMXFDemuxPartition *partition = demux->current_partition;
4156 gboolean partition_done = FALSE;
4157
4158 /* Grab footer metadata if needed */
4159 if (demux->pull_footer_metadata
4160 && partition->partition.type == MXF_PARTITION_PACK_HEADER
4161 && (!partition->partition.closed || !partition->partition.complete)
4162 && (demux->footer_partition_pack_offset != 0
4163 || demux->random_index_pack)) {
4164 GST_DEBUG_OBJECT (demux,
4165 "Open or incomplete header partition, trying to get final metadata from the last partitions");
4166 gst_mxf_demux_parse_footer_metadata (demux);
4167 demux->pull_footer_metadata = FALSE;
4168 }
4169
4170 /* If the partition has some content, do post-checks */
4171 if (partition->partition.body_sid != 0) {
4172 guint64 lowest_offset = G_MAXUINT64;
4173 GST_DEBUG_OBJECT (demux,
4174 "Entered partition (body_sid:%u index_sid:%u body_offset:%"
4175 G_GUINT64_FORMAT "), checking positions",
4176 partition->partition.body_sid, partition->partition.index_sid,
4177 partition->partition.body_offset);
4178
4179 if (partition->single_track) {
4180 /* Fast-path for single track partition */
4181 if (partition->single_track->position == -1
4182 && partition->partition.body_offset == 0) {
4183 GST_DEBUG_OBJECT (demux,
4184 "First time in partition, setting track position to 0");
4185 partition->single_track->position = 0;
4186 } else if (partition->single_track->position == -1) {
4187 GST_ERROR_OBJECT (demux,
4188 "Unknown track position, consuming data from first partition entry");
4189 lowest_offset =
4190 partition->partition.this_partition +
4191 partition->essence_container_offset;
4192 partition->clip_klv.consumed = 0;
4193 } else if (partition->single_track->position != 0) {
4194 GstMXFDemuxIndex entry;
4195 GST_DEBUG_OBJECT (demux,
4196 "Track already at another position : %" G_GUINT64_FORMAT,
4197 partition->single_track->position);
4198 if (find_edit_entry (demux, partition->single_track,
4199 partition->single_track->position, FALSE, &entry)) {
4200 lowest_offset = entry.offset;
4201 } else if (partition->single_track->position >=
4202 partition->single_track->duration) {
4203 GST_DEBUG_OBJECT (demux, "Track fully consumed, partition done");
4204 partition_done = TRUE;
4205 }
4206 }
4207 } else {
4208 guint i;
4209 for (i = 0; i < demux->essence_tracks->len; i++) {
4210 GstMXFDemuxEssenceTrack *etrack =
4211 g_ptr_array_index (demux->essence_tracks, i);
4212
4213 if (etrack->body_sid != partition->partition.body_sid)
4214 continue;
4215 if (etrack->position == -1 && partition->partition.body_offset == 0) {
4216 GST_DEBUG_OBJECT (demux, "Resetting track %u to position 0",
4217 etrack->track_id);
4218
4219 etrack->position = 0;
4220 } else if (etrack->position != 0) {
4221 GstMXFDemuxIndex entry;
4222 if (find_edit_entry (demux, etrack,
4223 etrack->position, FALSE, &entry)) {
4224 if (lowest_offset == G_MAXUINT64
4225 || entry.offset < lowest_offset)
4226 lowest_offset = entry.offset;
4227 }
4228 }
4229 }
4230 }
4231
4232 if (partition_done || lowest_offset != G_MAXUINT64) {
4233 GstMXFDemuxPartition *next_partition = NULL;
4234 GList *cur_part = g_list_find (demux->partitions, partition);
4235 if (cur_part && cur_part->next)
4236 next_partition = (GstMXFDemuxPartition *) cur_part->next->data;
4237
4238 /* If we have completely processed this partition, skip to next partition */
4239 if (partition_done
4240 || lowest_offset > next_partition->partition.this_partition) {
4241 GST_DEBUG_OBJECT (demux,
4242 "Partition entirely processed, skipping to next one");
4243 demux->offset = next_partition->partition.this_partition;
4244 } else {
4245 GST_DEBUG_OBJECT (demux,
4246 "Skipping to demuxer offset %" G_GUINT64_FORMAT " (from %"
4247 G_GUINT64_FORMAT ")", lowest_offset, demux->offset);
4248 demux->offset = lowest_offset;
4249 if (partition->single_track
4250 && partition->single_track->wrapping !=
4251 MXF_ESSENCE_WRAPPING_FRAME_WRAPPING) {
4252 demux->state = GST_MXF_DEMUX_STATE_ESSENCE;
4253 demux->current_partition->clip_klv.consumed =
4254 demux->offset - demux->current_partition->clip_klv.offset;
4255 }
4256 }
4257 }
4258 }
4259 }
4260 }
4261
4262 if (ret == GST_FLOW_OK && demux->src->len > 0
4263 && demux->essence_tracks->len > 0) {
4264 GstMXFDemuxPad *earliest = NULL;
4265 /* We allow time drifts of at most 500ms */
4266 while ((earliest = gst_mxf_demux_get_earliest_pad (demux)) && (force_switch
4267 || demux->segment.position -
4268 gst_mxf_demux_pad_get_current_time (demux,
4269 earliest) > demux->max_drift)) {
4270 guint64 offset;
4271 guint64 position;
4272
4273 GST_DEBUG_OBJECT (demux,
4274 "Found synchronization issue -- trying to solve");
4275
4276 position = earliest->current_essence_track_position;
4277
4278 /* FIXME: This can probably be improved by using the
4279 * offset of position-1 if it's in the same partition
4280 * or the start of the position otherwise.
4281 * This way we won't skip elements from the same essence
4282 * container as etrack->position
4283 */
4284 offset =
4285 gst_mxf_demux_find_essence_element (demux,
4286 earliest->current_essence_track, &position, FALSE);
4287 if (offset == -1) {
4288 GST_WARNING_OBJECT (demux,
4289 "Failed to find offset for late essence track");
4290 gst_mxf_demux_eos_single_stream (demux, earliest);
4291
4292 continue;
4293 }
4294
4295 demux->offset = offset + demux->run_in;
4296 gst_mxf_demux_set_partition_for_offset (demux, demux->offset);
4297 GST_DEBUG_OBJECT (demux,
4298 "Switching to offset %" G_GUINT64_FORMAT " for position %"
4299 G_GUINT64_FORMAT " on track %u (body_sid:%u index_sid:%u)",
4300 demux->offset, position, earliest->current_essence_track->track_id,
4301 earliest->current_essence_track->body_sid,
4302 earliest->current_essence_track->index_sid);
4303 if (demux->current_partition->single_track
4304 && demux->current_partition->single_track->wrapping !=
4305 MXF_ESSENCE_WRAPPING_FRAME_WRAPPING) {
4306 demux->state = GST_MXF_DEMUX_STATE_ESSENCE;
4307 demux->current_partition->clip_klv.consumed =
4308 offset - demux->current_partition->clip_klv.offset;
4309 } else
4310 demux->state = GST_MXF_DEMUX_STATE_KLV;
4311
4312 earliest->current_essence_track->position = position;
4313 GST_DEBUG_OBJECT (earliest, "Switching to this pad");
4314 break;
4315 }
4316 }
4317
4318 beach:
4319 return ret;
4320 }
4321
4322 static void
4323 gst_mxf_demux_loop (GstPad * pad)
4324 {
4325 GstMXFDemux *demux = NULL;
4326 GstFlowReturn flow = GST_FLOW_OK;
4327
4328 demux = GST_MXF_DEMUX (gst_pad_get_parent (pad));
4329
4330 if (demux->state == GST_MXF_DEMUX_STATE_UNKNOWN) {
4331 GstMXFKLV klv;
4332
4333 /* Skip run-in, which is at most 64K and is finished
4334 * by a header partition pack */
4335 while (demux->offset < 64 * 1024) {
4336 if ((flow =
4337 gst_mxf_demux_peek_klv_packet (demux, demux->offset,
4338 &klv)) != GST_FLOW_OK)
4339 goto pause;
4340
4341 if (mxf_is_header_partition_pack (&klv.key)) {
4342 GST_DEBUG_OBJECT (demux,
4343 "Found header partition pack at offset %" G_GUINT64_FORMAT,
4344 demux->offset);
4345 demux->state = GST_MXF_DEMUX_STATE_KLV;
4346 demux->run_in = demux->offset;
4347 break;
4348 }
4349 demux->offset++;
4350 }
4351
4352 if (G_UNLIKELY (demux->run_in == -1)) {
4353 GST_ERROR_OBJECT (demux, "No valid header partition pack found");
4354 flow = GST_FLOW_ERROR;
4355 goto pause;
4356 }
4357
4358 /* Grab the RIP at the end of the file (if present) */
4359 gst_mxf_demux_pull_random_index_pack (demux);
4360 }
4361
4362 /* Now actually do something */
4363 flow = gst_mxf_demux_pull_and_handle_klv_packet (demux);
4364
4365 /* pause if something went wrong */
4366 if (G_UNLIKELY (flow != GST_FLOW_OK))
4367 goto pause;
4368
4369 /* check EOS condition */
4370 if ((demux->segment.stop != -1) &&
4371 (demux->segment.position >= demux->segment.stop)) {
4372 guint i;
4373 gboolean eos = TRUE;
4374
4375 for (i = 0; i < demux->src->len; i++) {
4376 GstMXFDemuxPad *p = g_ptr_array_index (demux->src, i);
4377
4378 if (!p->eos
4379 && gst_mxf_demux_pad_get_current_time (demux,
4380 p) < demux->segment.stop) {
4381 eos = FALSE;
4382 break;
4383 }
4384 }
4385
4386 if (eos) {
4387 flow = GST_FLOW_EOS;
4388 goto pause;
4389 }
4390 }
4391
4392 gst_object_unref (demux);
4393
4394 return;
4395
4396 pause:
4397 {
4398 const gchar *reason = gst_flow_get_name (flow);
4399
4400 GST_LOG_OBJECT (demux, "pausing task, reason %s", reason);
4401 gst_pad_pause_task (pad);
4402
4403 if (flow == GST_FLOW_EOS) {
4404 /* perform EOS logic */
4405 if (demux->src->len == 0) {
4406 GST_ELEMENT_ERROR (demux, STREAM, WRONG_TYPE,
4407 ("This stream contains no data."),
4408 ("got eos and didn't find any streams"));
4409 } else if (demux->segment.flags & GST_SEEK_FLAG_SEGMENT) {
4410 gint64 stop;
4411 GstMessage *m;
4412 GstEvent *e;
4413
4414 /* for segment playback we need to post when (in stream time)
4415 * we stopped, this is either stop (when set) or the duration. */
4416 if ((stop = demux->segment.stop) == -1)
4417 stop = demux->segment.duration;
4418
4419 GST_LOG_OBJECT (demux, "Sending segment done, at end of segment");
4420 m = gst_message_new_segment_done (GST_OBJECT_CAST (demux),
4421 GST_FORMAT_TIME, stop);
4422 gst_message_set_seqnum (m, demux->seqnum);
4423 gst_element_post_message (GST_ELEMENT_CAST (demux), m);
4424 e = gst_event_new_segment_done (GST_FORMAT_TIME, stop);
4425 gst_event_set_seqnum (e, demux->seqnum);
4426 gst_mxf_demux_push_src_event (demux, e);
4427 } else {
4428 GstEvent *e;
4429
4430 /* normal playback, send EOS to all linked pads */
4431 GST_LOG_OBJECT (demux, "Sending EOS, at end of stream");
4432 e = gst_event_new_eos ();
4433 gst_event_set_seqnum (e, demux->seqnum);
4434 if (!gst_mxf_demux_push_src_event (demux, e)) {
4435 GST_WARNING_OBJECT (demux, "failed pushing EOS on streams");
4436 }
4437 }
4438 } else if (flow == GST_FLOW_NOT_LINKED || flow < GST_FLOW_EOS) {
4439 GstEvent *e;
4440
4441 GST_ELEMENT_FLOW_ERROR (demux, flow);
4442 e = gst_event_new_eos ();
4443 gst_event_set_seqnum (e, demux->seqnum);
4444 gst_mxf_demux_push_src_event (demux, e);
4445 }
4446 gst_object_unref (demux);
4447 return;
4448 }
4449 }
4450
4451 static GstFlowReturn
4452 gst_mxf_demux_chain (GstPad * pad, GstObject * parent, GstBuffer * inbuf)
4453 {
4454 GstFlowReturn ret = GST_FLOW_OK;
4455 GstMXFDemux *demux = NULL;
4456 const guint8 *data = NULL;
4457 gboolean res;
4458 GstMXFKLV klv;
4459 #ifndef GST_DISABLE_GST_DEBUG
4460 gchar str[48];
4461 #endif
4462
4463 demux = GST_MXF_DEMUX (parent);
4464
4465 GST_LOG_OBJECT (demux,
4466 "received buffer of %" G_GSIZE_FORMAT " bytes at offset %"
4467 G_GUINT64_FORMAT, gst_buffer_get_size (inbuf), GST_BUFFER_OFFSET (inbuf));
4468
4469 if (demux->src->len > 0) {
4470 if (!gst_mxf_demux_get_earliest_pad (demux)) {
4471 ret = GST_FLOW_EOS;
4472 GST_DEBUG_OBJECT (demux, "All tracks are EOS");
4473 return ret;
4474 }
4475 }
4476
4477 if (G_UNLIKELY (GST_BUFFER_OFFSET (inbuf) == 0)) {
4478 GST_DEBUG_OBJECT (demux, "beginning of file, expect header");
4479 demux->run_in = -1;
4480 demux->offset = 0;
4481 demux->state = GST_MXF_DEMUX_STATE_UNKNOWN;
4482 }
4483
4484 if (G_UNLIKELY (demux->offset == 0 && GST_BUFFER_OFFSET (inbuf) != 0)) {
4485 GST_DEBUG_OBJECT (demux, "offset was zero, synchronizing with buffer's");
4486 if (GST_BUFFER_OFFSET_IS_VALID (inbuf))
4487 demux->offset = GST_BUFFER_OFFSET (inbuf);
4488 gst_mxf_demux_set_partition_for_offset (demux, demux->offset);
4489 } else if (demux->current_partition == NULL) {
4490 gst_mxf_demux_set_partition_for_offset (demux, demux->offset);
4491 }
4492
4493 gst_adapter_push (demux->adapter, inbuf);
4494 inbuf = NULL;
4495
4496 while (ret == GST_FLOW_OK) {
4497 if (G_UNLIKELY (demux->flushing)) {
4498 GST_DEBUG_OBJECT (demux, "we are now flushing, exiting parser loop");
4499 ret = GST_FLOW_FLUSHING;
4500 break;
4501 }
4502
4503 if (gst_adapter_available (demux->adapter) < 16)
4504 break;
4505
4506 if (demux->state == GST_MXF_DEMUX_STATE_UNKNOWN) {
4507 /* Skip run-in, which is at most 64K and is finished
4508 * by a header partition pack */
4509
4510 while (demux->offset < 64 * 1024
4511 && gst_adapter_available (demux->adapter) >= 16) {
4512 data = gst_adapter_map (demux->adapter, 16);
4513 res = mxf_is_header_partition_pack ((const MXFUL *) data);
4514 gst_adapter_unmap (demux->adapter);
4515
4516 if (res) {
4517 GST_DEBUG_OBJECT (demux,
4518 "Found header partition pack at offset %" G_GUINT64_FORMAT,
4519 demux->offset);
4520 demux->run_in = demux->offset;
4521 demux->state = GST_MXF_DEMUX_STATE_KLV;
4522 break;
4523 }
4524 gst_adapter_flush (demux->adapter, 1);
4525 demux->offset++;
4526 }
4527 } else if (demux->offset < demux->run_in) {
4528 guint64 flush = MIN (gst_adapter_available (demux->adapter),
4529 demux->run_in - demux->offset);
4530 gst_adapter_flush (demux->adapter, flush);
4531 demux->offset += flush;
4532 continue;
4533 }
4534
4535 if (demux->state == GST_MXF_DEMUX_STATE_UNKNOWN) {
4536 /* Need more data */
4537 if (demux->offset < 64 * 1024)
4538 break;
4539
4540 GST_ERROR_OBJECT (demux, "No valid header partition pack found");
4541 ret = GST_FLOW_ERROR;
4542 break;
4543 }
4544
4545 if (gst_adapter_available (demux->adapter) < 17)
4546 break;
4547
4548 /* FIXME : Handle non-klv state */
4549 g_assert (demux->state == GST_MXF_DEMUX_STATE_KLV);
4550
4551 /* Now actually do something */
4552 memset (&klv, 0, sizeof (GstMXFKLV));
4553
4554 /* Pull 16 byte key and first byte of BER encoded length */
4555 data = gst_adapter_map (demux->adapter, 17);
4556
4557 memcpy (&klv.key, data, 16);
4558
4559 GST_DEBUG_OBJECT (demux, "Got KLV packet with key %s",
4560 mxf_ul_to_string (&klv.key, str));
4561
4562 /* Decode BER encoded packet length */
4563 if ((data[16] & 0x80) == 0) {
4564 klv.length = data[16];
4565 klv.data_offset = 17;
4566 } else {
4567 guint slen = data[16] & 0x7f;
4568
4569 klv.data_offset = 16 + 1 + slen;
4570
4571 gst_adapter_unmap (demux->adapter);
4572
4573 /* Must be at most 8 according to SMPTE-379M 5.3.4 and
4574 * GStreamer buffers can only have a 4 bytes length */
4575 if (slen > 8) {
4576 GST_ERROR_OBJECT (demux, "Invalid KLV packet length: %u", slen);
4577 ret = GST_FLOW_ERROR;
4578 break;
4579 }
4580
4581 if (gst_adapter_available (demux->adapter) < 17 + slen)
4582 break;
4583
4584 data = gst_adapter_map (demux->adapter, 17 + slen);
4585 data += 17;
4586
4587 klv.length = 0;
4588 while (slen) {
4589 klv.length = (klv.length << 8) | *data;
4590 data++;
4591 slen--;
4592 }
4593 }
4594
4595 gst_adapter_unmap (demux->adapter);
4596
4597 /* GStreamer's buffer sizes are stored in a guint so we
4598 * limit ourself to G_MAXUINT large buffers */
4599 if (klv.length > G_MAXUINT) {
4600 GST_ERROR_OBJECT (demux,
4601 "Unsupported KLV packet length: %" G_GSIZE_FORMAT, klv.length);
4602 ret = GST_FLOW_ERROR;
4603 break;
4604 }
4605
4606 GST_DEBUG_OBJECT (demux, "KLV packet with key %s has length "
4607 "%" G_GSIZE_FORMAT, mxf_ul_to_string (&klv.key, str), klv.length);
4608
4609 if (gst_adapter_available (demux->adapter) < klv.data_offset + klv.length)
4610 break;
4611
4612 gst_adapter_flush (demux->adapter, klv.data_offset);
4613
4614 if (klv.length > 0) {
4615 klv.data = gst_adapter_take_buffer (demux->adapter, klv.length);
4616
4617 ret = gst_mxf_demux_handle_klv_packet (demux, &klv, FALSE);
4618 }
4619 gst_mxf_demux_consume_klv (demux, &klv);
4620 }
4621
4622 return ret;
4623 }
4624
4625 /* Given a stream time for an output pad, figure out:
4626 * * The Essence track for that stream time
4627 * * The position on that track
4628 */
4629 static gboolean
4630 gst_mxf_demux_pad_to_track_and_position (GstMXFDemux * demux,
4631 GstMXFDemuxPad * pad, GstClockTime streamtime,
4632 GstMXFDemuxEssenceTrack ** etrack, guint64 * position)
4633 {
4634 guint64 material_position;
4635 guint64 sum = 0;
4636 guint i;
4637 MXFMetadataSourceClip *clip = NULL;
4638 gchar str[96];
4639
4640 /* Convert to material position */
4641 material_position =
4642 gst_util_uint64_scale (streamtime, pad->material_track->edit_rate.n,
4643 pad->material_track->edit_rate.d * GST_SECOND);
4644
4645 GST_DEBUG_OBJECT (pad,
4646 "streamtime %" GST_TIME_FORMAT " position %" G_GUINT64_FORMAT,
4647 GST_TIME_ARGS (streamtime), material_position);
4648
4649
4650 /* Find sequence component covering that position */
4651 for (i = 0; i < pad->material_track->parent.sequence->n_structural_components;
4652 i++) {
4653 clip =
4654 MXF_METADATA_SOURCE_CLIP (pad->material_track->parent.sequence->
4655 structural_components[i]);
4656 GST_LOG_OBJECT (pad,
4657 "clip %u start_position:%" G_GUINT64_FORMAT " duration %"
4658 G_GUINT64_FORMAT, clip->source_track_id, clip->start_position,
4659 clip->parent.duration);
4660 if (clip->parent.duration <= 0)
4661 break;
4662 if ((sum + clip->parent.duration) > material_position)
4663 break;
4664 sum += clip->parent.duration;
4665 }
4666
4667 if (i == pad->material_track->parent.sequence->n_structural_components) {
4668 GST_WARNING_OBJECT (pad, "Requested position beyond the last clip");
4669 /* Outside of current components. Setting to the end of the last clip */
4670 material_position = sum;
4671 sum -= clip->parent.duration;
4672 }
4673
4674 GST_DEBUG_OBJECT (pad, "Looking for essence track for track_id:%u umid:%s",
4675 clip->source_track_id, mxf_umid_to_string (&clip->source_package_id,
4676 str));
4677
4678 /* Get the corresponding essence track for the given source package and stream id */
4679 for (i = 0; i < demux->essence_tracks->len; i++) {
4680 GstMXFDemuxEssenceTrack *track =
4681 g_ptr_array_index (demux->essence_tracks, i);
4682 GST_LOG_OBJECT (pad, "Looking at essence track body_sid:%u index_sid:%u",
4683 track->body_sid, track->index_sid);
4684 if (clip->source_track_id == 0 || (track->track_id == clip->source_track_id
4685 && mxf_umid_is_equal (&clip->source_package_id,
4686 &track->source_package_uid))) {
4687 GST_DEBUG_OBJECT (pad,
4688 "Found matching essence track body_sid:%u index_sid:%u",
4689 track->body_sid, track->index_sid);
4690 *etrack = track;
4691 *position = material_position - sum;
4692 return TRUE;
4693 }
4694 }
4695
4696 return FALSE;
4697 }
4698
4699 /* Given a track+position for a given pad, figure out the resulting stream time */
4700 static gboolean
4701 gst_mxf_demux_pad_get_stream_time (GstMXFDemux * demux,
4702 GstMXFDemuxPad * pad, GstMXFDemuxEssenceTrack * etrack,
4703 guint64 position, GstClockTime * stream_time)
4704 {
4705 guint i;
4706 guint64 sum = 0;
4707 MXFMetadataSourceClip *clip = NULL;
4708
4709 /* Find the component for that */
4710 /* Find sequence component covering that position */
4711 for (i = 0; i < pad->material_track->parent.sequence->n_structural_components;
4712 i++) {
4713 clip =
4714 MXF_METADATA_SOURCE_CLIP (pad->material_track->parent.sequence->
4715 structural_components[i]);
4716 GST_LOG_OBJECT (pad,
4717 "clip %u start_position:%" G_GUINT64_FORMAT " duration %"
4718 G_GUINT64_FORMAT, clip->source_track_id, clip->start_position,
4719 clip->parent.duration);
4720 if (etrack->track_id == clip->source_track_id
4721 && mxf_umid_is_equal (&clip->source_package_id,
4722 &etrack->source_package_uid)) {
4723 /* This is the clip */
4724 break;
4725 }
4726 /* Fetch in the next one */
4727 sum += clip->parent.duration;
4728 }
4729
4730 /* Theoretically impossible */
4731 if (i == pad->material_track->parent.sequence->n_structural_components) {
4732 /* Outside of current components ?? */
4733 return FALSE;
4734 }
4735
4736 *stream_time =
4737 gst_util_uint64_scale (sum,
4738 pad->material_track->edit_rate.d * GST_SECOND,
4739 pad->material_track->edit_rate.n)
4740 + gst_util_uint64_scale (position,
4741 etrack->source_track->edit_rate.d * GST_SECOND,
4742 etrack->source_track->edit_rate.n);
4743
4744 return TRUE;
4745 }
4746
4747 static void
4748 gst_mxf_demux_pad_set_position (GstMXFDemux * demux, GstMXFDemuxPad * p,
4749 GstClockTime start)
4750 {
4751 guint i;
4752 guint64 sum = 0;
4753 MXFMetadataSourceClip *clip = NULL;
4754
4755 if (!p->current_component) {
4756 p->current_essence_track_position =
4757 gst_util_uint64_scale (start, p->material_track->edit_rate.n,
4758 p->material_track->edit_rate.d * GST_SECOND);
4759
4760 if (p->current_essence_track_position >= p->current_essence_track->duration
4761 && p->current_essence_track->duration > 0) {
4762 p->current_essence_track_position = p->current_essence_track->duration;
4763 }
4764 p->current_material_track_position = p->current_essence_track_position;
4765
4766 return;
4767 }
4768
4769 for (i = 0; i < p->material_track->parent.sequence->n_structural_components;
4770 i++) {
4771 clip =
4772 MXF_METADATA_SOURCE_CLIP (p->material_track->parent.sequence->
4773 structural_components[i]);
4774
4775 if (clip->parent.duration <= 0)
4776 break;
4777
4778 sum += clip->parent.duration;
4779
4780 if (gst_util_uint64_scale (sum, p->material_track->edit_rate.d * GST_SECOND,
4781 p->material_track->edit_rate.n) > start)
4782 break;
4783 }
4784
4785 if (i == p->material_track->parent.sequence->n_structural_components) {
4786 p->current_material_track_position = sum;
4787
4788 gst_mxf_demux_pad_set_component (demux, p, i);
4789 return;
4790 }
4791
4792 if (clip->parent.duration > 0 && clip->parent.duration != -1)
4793 sum -= clip->parent.duration;
4794
4795 start -=
4796 gst_util_uint64_scale (sum, p->material_track->edit_rate.d * GST_SECOND,
4797 p->material_track->edit_rate.n);
4798
4799 gst_mxf_demux_pad_set_component (demux, p, i);
4800
4801 {
4802 guint64 essence_offset = gst_util_uint64_scale (start,
4803 p->current_essence_track->source_track->edit_rate.n,
4804 p->current_essence_track->source_track->edit_rate.d * GST_SECOND);
4805
4806 p->current_essence_track_position += essence_offset;
4807 p->current_material_track_position = sum + essence_offset;
4808 }
4809
4810 if ((p->current_essence_track->duration > 0
4811 && p->current_essence_track->duration != -1)
4812 && p->current_essence_track_position >=
4813 p->current_essence_track->duration) {
4814 p->current_essence_track_position = p->current_essence_track->duration;
4815 p->current_material_track_position =
4816 sum + p->current_component->parent.duration;
4817 }
4818 }
4819
4820 static GstClockTime
4821 gst_mxf_demux_pad_get_current_time (GstMXFDemux * demux, GstMXFDemuxPad * p)
4822 {
4823 if (!p->current_essence_track || !p->material_track)
4824 return GST_CLOCK_TIME_NONE;
4825
4826 guint64 current_edit_unit =
4827 p->current_essence_track_position - p->current_component_start;
4828 GstClockTime component_start_time =
4829 gst_util_uint64_scale (p->current_component_start_position,
4830 p->material_track->edit_rate.d * GST_SECOND,
4831 p->material_track->edit_rate.n);
4832 GstClockTime time =
4833 component_start_time + gst_util_uint64_scale (current_edit_unit,
4834 p->current_essence_track->source_track->edit_rate.d * GST_SECOND,
4835 p->current_essence_track->source_track->edit_rate.n);
4836
4837 return time;
4838 }
4839
4840 static gboolean
4841 gst_mxf_demux_seek_push (GstMXFDemux * demux, GstEvent * event)
4842 {
4843 GstFormat format;
4844 GstSeekFlags flags;
4845 GstSeekType start_type, stop_type;
4846 guint64 start, stop;
4847 gdouble rate;
4848 gboolean update, flush, keyframe;
4849 GstSegment seeksegment;
4850 guint i;
4851 guint32 seqnum;
4852
4853 gst_event_parse_seek (event, &rate, &format, &flags,
4854 &start_type, (gint64 *) & start, &stop_type, (gint64 *) & stop);
4855 seqnum = gst_event_get_seqnum (event);
4856
4857 if (rate <= 0.0)
4858 goto wrong_rate;
4859
4860 if (format != GST_FORMAT_TIME)
4861 goto wrong_format;
4862
4863 flush = !!(flags & GST_SEEK_FLAG_FLUSH);
4864 keyframe = !!(flags & GST_SEEK_FLAG_KEY_UNIT);
4865
4866 /* Work on a copy until we are sure the seek succeeded. */
4867 memcpy (&seeksegment, &demux->segment, sizeof (GstSegment));
4868
4869 GST_DEBUG_OBJECT (demux, "segment before configure %" GST_SEGMENT_FORMAT,
4870 &demux->segment);
4871
4872 /* Apply the seek to our segment */
4873 gst_segment_do_seek (&seeksegment, rate, format, flags,
4874 start_type, start, stop_type, stop, &update);
4875
4876 GST_DEBUG_OBJECT (demux, "segment configured %" GST_SEGMENT_FORMAT,
4877 &seeksegment);
4878
4879 if (flush || seeksegment.position != demux->segment.position) {
4880 gboolean ret;
4881 guint64 new_offset = -1;
4882 GstEvent *e;
4883
4884 if (!demux->metadata_resolved || demux->update_metadata) {
4885 if (gst_mxf_demux_resolve_references (demux) != GST_FLOW_OK ||
4886 gst_mxf_demux_update_tracks (demux) != GST_FLOW_OK) {
4887 goto unresolved_metadata;
4888 }
4889 }
4890
4891 /* Do the actual seeking */
4892 for (i = 0; i < demux->src->len; i++) {
4893 GstMXFDemuxPad *p = g_ptr_array_index (demux->src, i);
4894 guint64 position;
4895 guint64 off;
4896
4897 /* Reset EOS flag on all pads */
4898 p->eos = FALSE;
4899 gst_mxf_demux_pad_set_position (demux, p, start);
4900
4901 position = p->current_essence_track_position;
4902 off = gst_mxf_demux_find_essence_element (demux, p->current_essence_track,
4903 &position, keyframe);
4904 new_offset = MIN (off, new_offset);
4905 p->discont = TRUE;
4906 }
4907
4908 if (new_offset == -1)
4909 goto no_new_offset;
4910
4911 new_offset += demux->run_in;
4912
4913 GST_DEBUG_OBJECT (demux, "generating an upstream seek at position %"
4914 G_GUINT64_FORMAT, new_offset);
4915 e = gst_event_new_seek (seeksegment.rate, GST_FORMAT_BYTES,
4916 seeksegment.flags | GST_SEEK_FLAG_ACCURATE, GST_SEEK_TYPE_SET,
4917 new_offset, GST_SEEK_TYPE_NONE, 0);
4918 gst_event_set_seqnum (e, seqnum);
4919 ret = gst_pad_push_event (demux->sinkpad, e);
4920
4921 if (G_UNLIKELY (!ret)) {
4922 goto seek_failed;
4923 }
4924 }
4925
4926 /* Tell all the stream a new segment is needed */
4927 for (i = 0; i < demux->src->len; i++) {
4928 GstMXFDemuxPad *p = g_ptr_array_index (demux->src, i);
4929 p->need_segment = TRUE;
4930 }
4931
4932 for (i = 0; i < demux->essence_tracks->len; i++) {
4933 GstMXFDemuxEssenceTrack *t = g_ptr_array_index (demux->essence_tracks, i);
4934 t->position = -1;
4935 }
4936
4937 /* Ok seek succeeded, take the newly configured segment */
4938 memcpy (&demux->segment, &seeksegment, sizeof (GstSegment));
4939
4940 return TRUE;
4941
4942 /* ERRORS */
4943 wrong_format:
4944 {
4945 GST_WARNING_OBJECT (demux, "seeking only supported in TIME format");
4946 return gst_pad_push_event (demux->sinkpad, gst_event_ref (event));
4947 }
4948 wrong_rate:
4949 {
4950 GST_WARNING_OBJECT (demux, "only rates > 0.0 are allowed");
4951 return FALSE;
4952 }
4953 unresolved_metadata:
4954 {
4955 GST_WARNING_OBJECT (demux, "metadata can't be resolved");
4956 return gst_pad_push_event (demux->sinkpad, gst_event_ref (event));
4957 }
4958 seek_failed:
4959 {
4960 GST_WARNING_OBJECT (demux, "upstream seek failed");
4961 return gst_pad_push_event (demux->sinkpad, gst_event_ref (event));
4962 }
4963 no_new_offset:
4964 {
4965 GST_WARNING_OBJECT (demux, "can't find new offset");
4966 return gst_pad_push_event (demux->sinkpad, gst_event_ref (event));
4967 }
4968 }
4969
4970 static void
4971 collect_index_table_segments (GstMXFDemux * demux)
4972 {
4973 GList *l;
4974 guint i;
4975 guint64 old_offset = demux->offset;
4976 GstMXFDemuxPartition *old_partition = demux->current_partition;
4977
4978 /* This function can also be called when a RIP is not present. This can happen
4979 * if index table segments were discovered while scanning the file */
4980 if (demux->random_index_pack) {
4981 for (i = 0; i < demux->random_index_pack->len; i++) {
4982 MXFRandomIndexPackEntry *e =
4983 &g_array_index (demux->random_index_pack, MXFRandomIndexPackEntry, i);
4984
4985 if (e->offset < demux->run_in) {
4986 GST_ERROR_OBJECT (demux, "Invalid random index pack entry");
4987 return;
4988 }
4989
4990 demux->offset = e->offset;
4991 read_partition_header (demux);
4992 }
4993
4994 demux->offset = old_offset;
4995 demux->current_partition = old_partition;
4996 }
4997
4998 if (demux->pending_index_table_segments == NULL) {
4999 GST_DEBUG_OBJECT (demux, "No pending index table segments to collect");
5000 return;
5001 }
5002
5003 GST_LOG_OBJECT (demux, "Collecting pending index table segments");
5004
5005 for (l = demux->pending_index_table_segments; l; l = l->next) {
5006 MXFIndexTableSegment *segment = l->data;
5007 GstMXFDemuxIndexTable *t = NULL;
5008 GList *k;
5009 guint didx;
5010 #ifndef GST_DISABLE_GST_DEBUG
5011 gchar str[48];
5012 #endif
5013
5014 GST_LOG_OBJECT (demux,
5015 "Collecting from segment bodySID:%u indexSID:%u instance_id: %s",
5016 segment->body_sid, segment->index_sid,
5017 mxf_uuid_to_string (&segment->instance_id, str));
5018
5019 for (k = demux->index_tables; k; k = k->next) {
5020 GstMXFDemuxIndexTable *tmp = k->data;
5021
5022 if (tmp->body_sid == segment->body_sid
5023 && tmp->index_sid == segment->index_sid) {
5024 t = tmp;
5025 break;
5026 }
5027 }
5028
5029 if (!t) {
5030 t = g_new0 (GstMXFDemuxIndexTable, 1);
5031 t->body_sid = segment->body_sid;
5032 t->index_sid = segment->index_sid;
5033 t->max_temporal_offset = 0;
5034 t->segments = g_array_new (FALSE, TRUE, sizeof (MXFIndexTableSegment));
5035 g_array_set_clear_func (t->segments,
5036 (GDestroyNotify) mxf_index_table_segment_reset);
5037 t->reordered_delta_entry = -1;
5038 t->reverse_temporal_offsets = g_array_new (FALSE, TRUE, 1);
5039 demux->index_tables = g_list_prepend (demux->index_tables, t);
5040 }
5041
5042 /* Store index segment */
5043 g_array_append_val (t->segments, *segment);
5044
5045 /* Check if temporal reordering tables should be pre-calculated */
5046 for (didx = 0; didx < segment->n_delta_entries; didx++) {
5047 MXFDeltaEntry *delta = &segment->delta_entries[didx];
5048 if (delta->pos_table_index == -1) {
5049 if (t->reordered_delta_entry != -1 && didx != t->reordered_delta_entry)
5050 GST_WARNING_OBJECT (demux,
5051 "Index Table specifies more than one stream using temporal reordering (%u and %d)",
5052 didx, t->reordered_delta_entry);
5053 else
5054 t->reordered_delta_entry = didx;
5055 } else if (delta->pos_table_index > 0)
5056 GST_WARNING_OBJECT (delta,
5057 "Index Table uses fractional offset, please file a bug");
5058 }
5059
5060 }
5061
5062 /* Handle temporal offset if present and needed */
5063 for (l = demux->index_tables; l; l = l->next) {
5064 GstMXFDemuxIndexTable *table = l->data;
5065 guint segidx;
5066
5067 /* No reordered entries, skip */
5068 if (table->reordered_delta_entry == -1)
5069 continue;
5070
5071 GST_DEBUG_OBJECT (demux,
5072 "bodySID:%u indexSID:%u Calculating reverse temporal offset table",
5073 table->body_sid, table->index_sid);
5074
5075 for (segidx = 0; segidx < table->segments->len; segidx++) {
5076 MXFIndexTableSegment *s =
5077 &g_array_index (table->segments, MXFIndexTableSegment, segidx);
5078 guint64 start = s->index_start_position;
5079 guint64 stop =
5080 s->index_duration ? start + s->index_duration : start +
5081 s->n_index_entries;
5082 guint entidx = 0;
5083
5084 if (stop > table->reverse_temporal_offsets->len)
5085 g_array_set_size (table->reverse_temporal_offsets, stop);
5086
5087 for (entidx = 0; entidx < s->n_index_entries; entidx++) {
5088 MXFIndexEntry *entry = &s->index_entries[entidx];
5089 gint8 offs = -entry->temporal_offset;
5090 /* Check we don't exceed boundaries */
5091 if (start + entidx + entry->temporal_offset >=
5092 table->reverse_temporal_offsets->len) {
5093 GST_ERROR_OBJECT (demux,
5094 "Temporal offset exceeds boundaries. entry:%" G_GUINT64_FORMAT
5095 " offset:%d max:%u", start + entidx, entry->temporal_offset,
5096 table->reverse_temporal_offsets->len);
5097 } else {
5098 /* Applying the temporal offset gives us the entry that should contain this PTS.
5099 * We store the reverse temporal offset on that entry, i.e. the value it should apply
5100 * to go from DTS to PTS. (i.e. entry.pts = entry.dts + rto[idx]) */
5101 g_array_index (table->reverse_temporal_offsets, gint8,
5102 start + entidx + entry->temporal_offset) = offs;
5103 if (entry->temporal_offset > (gint) table->max_temporal_offset) {
5104 GST_LOG_OBJECT (demux,
5105 "Updating max temporal offset to %d (was %d)",
5106 entry->temporal_offset, table->max_temporal_offset);
5107 table->max_temporal_offset = entry->temporal_offset;
5108 }
5109 }
5110 }
5111 }
5112 }
5113
5114 g_list_free_full (demux->pending_index_table_segments, g_free);
5115 demux->pending_index_table_segments = NULL;
5116
5117 GST_DEBUG_OBJECT (demux, "Done collecting segments");
5118 }
5119
5120 static gboolean
5121 gst_mxf_demux_seek_pull (GstMXFDemux * demux, GstEvent * event)
5122 {
5123 GstClockTime keyunit_ts;
5124 GstFormat format;
5125 GstSeekFlags flags;
5126 GstSeekType start_type, stop_type;
5127 guint64 start, stop;
5128 gdouble rate;
5129 gboolean update, flush, keyframe;
5130 GstSegment seeksegment;
5131 guint i;
5132 gboolean ret = TRUE;
5133 guint32 seqnum;
5134
5135 gst_event_parse_seek (event, &rate, &format, &flags,
5136 &start_type, (gint64 *) & start, &stop_type, (gint64 *) & stop);
5137 seqnum = gst_event_get_seqnum (event);
5138
5139 if (seqnum == demux->seqnum) {
5140 GST_DEBUG_OBJECT (demux, "Already handled requested seek");
5141 return TRUE;
5142 }
5143
5144 GST_DEBUG_OBJECT (demux, "Seek %" GST_PTR_FORMAT, event);
5145
5146 if (format != GST_FORMAT_TIME)
5147 goto wrong_format;
5148
5149 if (rate <= 0.0)
5150 goto wrong_rate;
5151
5152 flush = !!(flags & GST_SEEK_FLAG_FLUSH);
5153 keyframe = !!(flags & GST_SEEK_FLAG_KEY_UNIT);
5154
5155 if (!demux->index_table_segments_collected) {
5156 collect_index_table_segments (demux);
5157 demux->index_table_segments_collected = TRUE;
5158 }
5159
5160 if (flush) {
5161 GstEvent *e;
5162
5163 /* Flush start up and downstream to make sure data flow and loops are
5164 idle */
5165 e = gst_event_new_flush_start ();
5166 gst_event_set_seqnum (e, seqnum);
5167 gst_mxf_demux_push_src_event (demux, gst_event_ref (e));
5168 gst_pad_push_event (demux->sinkpad, e);
5169 } else {
5170 /* Pause the pulling task */
5171 gst_pad_pause_task (demux->sinkpad);
5172 }
5173
5174 /* Take the stream lock */
5175 GST_PAD_STREAM_LOCK (demux->sinkpad);
5176
5177 if (flush) {
5178 GstEvent *e;
5179
5180 /* Stop flushing upstream we need to pull */
5181 e = gst_event_new_flush_stop (TRUE);
5182 gst_event_set_seqnum (e, seqnum);
5183 gst_pad_push_event (demux->sinkpad, e);
5184 }
5185
5186 /* Work on a copy until we are sure the seek succeeded. */
5187 memcpy (&seeksegment, &demux->segment, sizeof (GstSegment));
5188
5189 GST_DEBUG_OBJECT (demux, "segment before configure %" GST_SEGMENT_FORMAT,
5190 &demux->segment);
5191
5192 /* Apply the seek to our segment */
5193 gst_segment_do_seek (&seeksegment, rate, format, flags,
5194 start_type, start, stop_type, stop, &update);
5195
5196 GST_DEBUG_OBJECT (demux,
5197 "segment initially configured to %" GST_SEGMENT_FORMAT, &seeksegment);
5198
5199 /* Initialize and reset ourselves if needed */
5200 if (flush || seeksegment.position != demux->segment.position) {
5201 GList *tmp;
5202 if (!demux->metadata_resolved || demux->update_metadata) {
5203 if (gst_mxf_demux_resolve_references (demux) != GST_FLOW_OK ||
5204 gst_mxf_demux_update_tracks (demux) != GST_FLOW_OK) {
5205 goto unresolved_metadata;
5206 }
5207 }
5208
5209 /* Reset all single-track KLV tracking */
5210 for (tmp = demux->partitions; tmp; tmp = tmp->next) {
5211 GstMXFDemuxPartition *partition = (GstMXFDemuxPartition *) tmp->data;
5212 if (partition->single_track) {
5213 partition->clip_klv.consumed = 0;
5214 }
5215 }
5216 }
5217
5218 keyunit_ts = seeksegment.position;
5219
5220 /* Do a first round without changing positions. This is needed to figure out
5221 * the supporting keyframe position (if any) */
5222 for (i = 0; i < demux->src->len; i++) {
5223 GstMXFDemuxPad *p = g_ptr_array_index (demux->src, i);
5224 GstMXFDemuxEssenceTrack *etrack;
5225 guint64 track_pos, seeked_pos;
5226
5227 /* Get track and track position for requested time, handles out of bound internally */
5228 if (!gst_mxf_demux_pad_to_track_and_position (demux, p,
5229 seeksegment.position, &etrack, &track_pos))
5230 goto invalid_position;
5231
5232 GST_LOG_OBJECT (p,
5233 "track %u (body_sid:%u index_sid:%u), position %" G_GUINT64_FORMAT,
5234 etrack->track_id, etrack->body_sid, etrack->index_sid, track_pos);
5235
5236 /* Find supporting keyframe entry */
5237 seeked_pos = track_pos;
5238 if (gst_mxf_demux_find_essence_element (demux, etrack, &seeked_pos,
5239 TRUE) == -1) {
5240 /* Couldn't find entry, ignore */
5241 break;
5242 }
5243
5244 GST_LOG_OBJECT (p,
5245 "track %u (body_sid:%u index_sid:%u), position %" G_GUINT64_FORMAT
5246 " entry position %" G_GUINT64_FORMAT, etrack->track_id,
5247 etrack->body_sid, etrack->index_sid, track_pos, seeked_pos);
5248
5249 if (seeked_pos != track_pos) {
5250 GstClockTime stream_time;
5251 if (!gst_mxf_demux_pad_get_stream_time (demux, p, etrack, seeked_pos,
5252 &stream_time))
5253 goto invalid_position;
5254 GST_LOG_OBJECT (p, "Need to seek to stream time %" GST_TIME_FORMAT,
5255 GST_TIME_ARGS (stream_time));
5256 keyunit_ts = MIN (seeksegment.position, stream_time);
5257 }
5258 }
5259
5260 if (keyframe && keyunit_ts != seeksegment.position) {
5261 GST_INFO_OBJECT (demux, "key unit seek, adjusting segment start to "
5262 "%" GST_TIME_FORMAT, GST_TIME_ARGS (keyunit_ts));
5263 gst_segment_do_seek (&seeksegment, rate, format, flags,
5264 start_type, keyunit_ts, stop_type, stop, &update);
5265 }
5266
5267 /* Finally set the position to the calculated position */
5268 if (flush || keyunit_ts != demux->segment.position) {
5269 guint64 new_offset = -1;
5270
5271 /* Do the actual seeking */
5272 for (i = 0; i < demux->src->len; i++) {
5273 GstMXFDemuxPad *p = g_ptr_array_index (demux->src, i);
5274 guint64 position;
5275 guint64 off;
5276
5277 /* Reset EOS flag on all pads */
5278 p->eos = FALSE;
5279 gst_mxf_demux_pad_set_position (demux, p, seeksegment.position);
5280
5281 /* we always want to send data starting with a key unit */
5282 position = p->current_essence_track_position;
5283 off =
5284 gst_mxf_demux_find_essence_element (demux, p->current_essence_track,
5285 &position, TRUE);
5286 if (off == -1) {
5287 GST_DEBUG_OBJECT (demux, "Unable to find offset for pad %s",
5288 GST_PAD_NAME (p));
5289 p->current_essence_track_position = p->current_essence_track->duration;
5290 } else {
5291 new_offset = MIN (off, new_offset);
5292 if (position != p->current_essence_track_position) {
5293 p->current_material_track_position -=
5294 gst_util_uint64_scale (p->current_essence_track_position -
5295 position,
5296 p->material_track->edit_rate.n *
5297 p->current_essence_track->source_track->edit_rate.d,
5298 p->material_track->edit_rate.d *
5299 p->current_essence_track->source_track->edit_rate.n);
5300 }
5301 p->current_essence_track_position = position;
5302 }
5303 p->current_essence_track->position = p->current_essence_track_position;
5304 p->discont = TRUE;
5305 }
5306 gst_flow_combiner_reset (demux->flowcombiner);
5307 if (new_offset == -1) {
5308 GST_WARNING_OBJECT (demux, "No new offset found");
5309 ret = FALSE;
5310 } else {
5311 demux->offset = new_offset + demux->run_in;
5312 }
5313 gst_mxf_demux_set_partition_for_offset (demux, demux->offset);
5314 /* Reset the state accordingly */
5315 if (demux->current_partition->single_track
5316 && demux->current_partition->single_track->wrapping !=
5317 MXF_ESSENCE_WRAPPING_FRAME_WRAPPING)
5318 demux->state = GST_MXF_DEMUX_STATE_ESSENCE;
5319 else
5320 demux->state = GST_MXF_DEMUX_STATE_KLV;
5321 }
5322
5323 if (flush) {
5324 GstEvent *e;
5325
5326 /* Stop flushing, the sinks are at time 0 now */
5327 e = gst_event_new_flush_stop (TRUE);
5328 gst_event_set_seqnum (e, seqnum);
5329 gst_mxf_demux_push_src_event (demux, e);
5330 }
5331
5332 /* Ok seek succeeded, take the newly configured segment */
5333 memcpy (&demux->segment, &seeksegment, sizeof (GstSegment));
5334
5335 /* Notify about the start of a new segment */
5336 if (demux->segment.flags & GST_SEEK_FLAG_SEGMENT) {
5337 GstMessage *m;
5338
5339 m = gst_message_new_segment_start (GST_OBJECT (demux),
5340 demux->segment.format, demux->segment.position);
5341 gst_message_set_seqnum (m, seqnum);
5342 gst_element_post_message (GST_ELEMENT (demux), m);
5343 }
5344
5345 /* Tell all the stream a new segment is needed */
5346 for (i = 0; i < demux->src->len; i++) {
5347 GstMXFDemuxPad *p = g_ptr_array_index (demux->src, i);
5348 p->need_segment = TRUE;
5349 }
5350
5351 for (i = 0; i < demux->essence_tracks->len; i++) {
5352 GstMXFDemuxEssenceTrack *t = g_ptr_array_index (demux->essence_tracks, i);
5353 t->position = -1;
5354 }
5355
5356 demux->seqnum = seqnum;
5357
5358 gst_pad_start_task (demux->sinkpad,
5359 (GstTaskFunction) gst_mxf_demux_loop, demux->sinkpad, NULL);
5360
5361 GST_PAD_STREAM_UNLOCK (demux->sinkpad);
5362
5363 return ret;
5364
5365 /* ERRORS */
5366 wrong_format:
5367 {
5368 GST_WARNING_OBJECT (demux, "seeking only supported in TIME format");
5369 return FALSE;
5370 }
5371 wrong_rate:
5372 {
5373 GST_WARNING_OBJECT (demux, "only rates > 0.0 are allowed");
5374 return FALSE;
5375 }
5376 unresolved_metadata:
5377 {
5378 gst_pad_start_task (demux->sinkpad,
5379 (GstTaskFunction) gst_mxf_demux_loop, demux->sinkpad, NULL);
5380 GST_PAD_STREAM_UNLOCK (demux->sinkpad);
5381 GST_WARNING_OBJECT (demux, "metadata can't be resolved");
5382 return FALSE;
5383 }
5384
5385 invalid_position:
5386 {
5387 if (flush) {
5388 GstEvent *e;
5389
5390 /* Stop flushing, the sinks are at time 0 now */
5391 e = gst_event_new_flush_stop (TRUE);
5392 gst_event_set_seqnum (e, seqnum);
5393 gst_mxf_demux_push_src_event (demux, e);
5394 }
5395 gst_pad_start_task (demux->sinkpad,
5396 (GstTaskFunction) gst_mxf_demux_loop, demux->sinkpad, NULL);
5397 GST_PAD_STREAM_UNLOCK (demux->sinkpad);
5398 GST_WARNING_OBJECT (demux, "Requested seek position is not valid");
5399 return FALSE;
5400 }
5401 }
5402
5403 static gboolean
5404 gst_mxf_demux_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
5405 {
5406 GstMXFDemux *demux = GST_MXF_DEMUX (parent);
5407 gboolean ret;
5408
5409 GST_DEBUG_OBJECT (pad, "handling event %s", GST_EVENT_TYPE_NAME (event));
5410
5411 switch (GST_EVENT_TYPE (event)) {
5412 case GST_EVENT_SEEK:
5413 if (demux->random_access)
5414 ret = gst_mxf_demux_seek_pull (demux, event);
5415 else
5416 ret = gst_mxf_demux_seek_push (demux, event);
5417 gst_event_unref (event);
5418 break;
5419 default:
5420 ret = gst_pad_push_event (demux->sinkpad, event);
5421 break;
5422 }
5423
5424 return ret;
5425 }
5426
5427 static gboolean
5428 gst_mxf_demux_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
5429 {
5430 GstMXFDemux *demux = GST_MXF_DEMUX (parent);
5431 gboolean ret = FALSE;
5432 GstMXFDemuxPad *mxfpad = GST_MXF_DEMUX_PAD (pad);
5433
5434 GST_DEBUG_OBJECT (pad, "handling query %s",
5435 gst_query_type_get_name (GST_QUERY_TYPE (query)));
5436
5437 switch (GST_QUERY_TYPE (query)) {
5438 case GST_QUERY_POSITION:
5439 {
5440 GstFormat format;
5441 guint64 pos;
5442
5443 gst_query_parse_position (query, &format, NULL);
5444 if (format != GST_FORMAT_TIME && format != GST_FORMAT_DEFAULT)
5445 goto error;
5446
5447 pos =
5448 format ==
5449 GST_FORMAT_DEFAULT ? mxfpad->current_material_track_position :
5450 gst_mxf_demux_pad_get_current_time (demux, mxfpad);
5451
5452 GST_DEBUG_OBJECT (pad,
5453 "Returning position %" G_GUINT64_FORMAT " in format %s", pos,
5454 gst_format_get_name (format));
5455
5456 gst_query_set_position (query, format, pos);
5457 ret = TRUE;
5458
5459 break;
5460 }
5461 case GST_QUERY_DURATION:{
5462 guint64 duration;
5463 GstFormat format;
5464
5465 gst_query_parse_duration (query, &format, NULL);
5466 if (format != GST_FORMAT_TIME && format != GST_FORMAT_DEFAULT)
5467 goto error;
5468
5469 g_rw_lock_reader_lock (&demux->metadata_lock);
5470 if (!mxfpad->material_track || !mxfpad->material_track->parent.sequence) {
5471 g_rw_lock_reader_unlock (&demux->metadata_lock);
5472 goto error;
5473 }
5474
5475 duration = mxfpad->material_track->parent.sequence->duration;
5476
5477 if (duration != -1 && format == GST_FORMAT_TIME) {
5478 if (mxfpad->material_track->edit_rate.n == 0 ||
5479 mxfpad->material_track->edit_rate.d == 0) {
5480 g_rw_lock_reader_unlock (&demux->metadata_lock);
5481 goto error;
5482 }
5483
5484 duration =
5485 gst_util_uint64_scale (duration,
5486 GST_SECOND * mxfpad->material_track->edit_rate.d,
5487 mxfpad->material_track->edit_rate.n);
5488 }
5489 g_rw_lock_reader_unlock (&demux->metadata_lock);
5490
5491 GST_DEBUG_OBJECT (pad,
5492 "Returning duration %" G_GUINT64_FORMAT " in format %s", duration,
5493 gst_format_get_name (format));
5494
5495 gst_query_set_duration (query, format, duration);
5496 ret = TRUE;
5497 break;
5498 }
5499 case GST_QUERY_SEEKING:{
5500 GstFormat fmt;
5501 guint64 duration;
5502
5503 ret = TRUE;
5504 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
5505 if (fmt != GST_FORMAT_TIME) {
5506 gst_query_set_seeking (query, fmt, FALSE, -1, -1);
5507 goto done;
5508 }
5509
5510 if (!gst_pad_query_duration (pad, GST_FORMAT_TIME, (gint64 *) & duration)) {
5511 gst_query_set_seeking (query, fmt, FALSE, -1, -1);
5512 goto done;
5513 }
5514
5515 if (demux->random_access) {
5516 gst_query_set_seeking (query, GST_FORMAT_TIME, TRUE, 0, duration);
5517 } else {
5518 GstQuery *peerquery = gst_query_new_seeking (GST_FORMAT_BYTES);
5519 gboolean seekable;
5520
5521 seekable = gst_pad_peer_query (demux->sinkpad, peerquery);
5522 if (seekable)
5523 gst_query_parse_seeking (peerquery, NULL, &seekable, NULL, NULL);
5524 if (seekable)
5525 gst_query_set_seeking (query, GST_FORMAT_TIME, TRUE, 0, duration);
5526 else
5527 gst_query_set_seeking (query, GST_FORMAT_TIME, FALSE, -1, -1);
5528
5529 gst_query_unref (peerquery);
5530 }
5531
5532 break;
5533 }
5534 case GST_QUERY_SEGMENT:{
5535 GstFormat format;
5536 guint64 start, stop;
5537
5538 format = demux->segment.format;
5539
5540 start =
5541 gst_segment_to_stream_time (&demux->segment, format,
5542 demux->segment.start);
5543 if ((stop = demux->segment.stop) == -1)
5544 stop = demux->segment.duration;
5545 else
5546 stop = gst_segment_to_stream_time (&demux->segment, format, stop);
5547
5548 gst_query_set_segment (query, demux->segment.rate, format, start, stop);
5549 ret = TRUE;
5550 break;
5551 }
5552 default:
5553 ret = gst_pad_query_default (pad, parent, query);
5554 break;
5555 }
5556
5557 done:
5558 return ret;
5559
5560 /* ERRORS */
5561 error:
5562 {
5563 GST_DEBUG_OBJECT (pad, "query failed");
5564 goto done;
5565 }
5566 }
5567
5568 static gboolean
5569 gst_mxf_demux_sink_activate (GstPad * sinkpad, GstObject * parent)
5570 {
5571 GstQuery *query;
5572 GstPadMode mode = GST_PAD_MODE_PUSH;
5573
5574 query = gst_query_new_scheduling ();
5575
5576 if (gst_pad_peer_query (sinkpad, query)) {
5577 if (gst_query_has_scheduling_mode_with_flags (query,
5578 GST_PAD_MODE_PULL, GST_SCHEDULING_FLAG_SEEKABLE)) {
5579 GstSchedulingFlags flags;
5580 gst_query_parse_scheduling (query, &flags, NULL, NULL, NULL);
5581 if (!(flags & GST_SCHEDULING_FLAG_SEQUENTIAL))
5582 mode = GST_PAD_MODE_PULL;
5583 }
5584 }
5585 gst_query_unref (query);
5586
5587 return gst_pad_activate_mode (sinkpad, mode, TRUE);
5588 }
5589
5590 static gboolean
5591 gst_mxf_demux_sink_activate_mode (GstPad * sinkpad, GstObject * parent,
5592 GstPadMode mode, gboolean active)
5593 {
5594 GstMXFDemux *demux;
5595
5596 demux = GST_MXF_DEMUX (parent);
5597
5598 if (mode == GST_PAD_MODE_PUSH) {
5599 demux->random_access = FALSE;
5600 } else {
5601 if (active) {
5602 demux->random_access = TRUE;
5603 return gst_pad_start_task (sinkpad, (GstTaskFunction) gst_mxf_demux_loop,
5604 sinkpad, NULL);
5605 } else {
5606 demux->random_access = FALSE;
5607 return gst_pad_stop_task (sinkpad);
5608 }
5609 }
5610
5611 return TRUE;
5612 }
5613
5614 static gboolean
5615 gst_mxf_demux_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
5616 {
5617 GstMXFDemux *demux;
5618 gboolean ret = FALSE;
5619
5620 demux = GST_MXF_DEMUX (parent);
5621
5622 GST_DEBUG_OBJECT (pad, "handling event %s", GST_EVENT_TYPE_NAME (event));
5623
5624 switch (GST_EVENT_TYPE (event)) {
5625 case GST_EVENT_FLUSH_START:
5626 demux->flushing = TRUE;
5627 ret = gst_pad_event_default (pad, parent, event);
5628 break;
5629 case GST_EVENT_FLUSH_STOP:
5630 GST_DEBUG_OBJECT (demux, "flushing queued data in the MXF demuxer");
5631
5632 gst_adapter_clear (demux->adapter);
5633 demux->flushing = FALSE;
5634 demux->offset = 0;
5635 ret = gst_pad_event_default (pad, parent, event);
5636 break;
5637 case GST_EVENT_EOS:{
5638 GstMXFDemuxPad *p = NULL;
5639 guint i;
5640
5641 if (demux->src->len == 0) {
5642 GST_ELEMENT_ERROR (demux, STREAM, WRONG_TYPE,
5643 ("This stream contains no data."),
5644 ("got eos and didn't find any streams"));
5645 }
5646
5647 for (i = 0; i < demux->essence_tracks->len; i++) {
5648 GstMXFDemuxEssenceTrack *t =
5649 g_ptr_array_index (demux->essence_tracks, i);
5650
5651 if (t->position > 0 && t->position != -1)
5652 t->duration = t->position;
5653 }
5654
5655 for (i = 0; i < demux->src->len; i++) {
5656 GstMXFDemuxPad *p = g_ptr_array_index (demux->src, i);
5657
5658 if (!p->eos
5659 && p->current_essence_track_position >=
5660 p->current_essence_track->duration)
5661 gst_mxf_demux_eos_single_stream (demux, p);
5662 }
5663
5664 while ((p = gst_mxf_demux_get_earliest_pad (demux))) {
5665 guint64 offset;
5666 guint64 position;
5667
5668 position = p->current_essence_track_position;
5669
5670 offset =
5671 gst_mxf_demux_find_essence_element (demux, p->current_essence_track,
5672 &position, FALSE);
5673 if (offset == -1) {
5674 GST_ERROR_OBJECT (demux, "Failed to find offset for essence track");
5675 gst_mxf_demux_eos_single_stream (demux, p);
5676 continue;
5677 }
5678
5679 if (gst_pad_push_event (demux->sinkpad,
5680 gst_event_new_seek (demux->segment.rate, GST_FORMAT_BYTES,
5681 demux->segment.flags | GST_SEEK_FLAG_ACCURATE,
5682 GST_SEEK_TYPE_SET, offset + demux->run_in,
5683 GST_SEEK_TYPE_NONE, 0))) {
5684
5685 for (i = 0; i < demux->essence_tracks->len; i++) {
5686 GstMXFDemuxEssenceTrack *etrack =
5687 g_ptr_array_index (demux->essence_tracks, i);
5688 etrack->position = -1;
5689 }
5690 ret = TRUE;
5691 goto out;
5692 } else {
5693 GST_WARNING_OBJECT (demux,
5694 "Seek to remaining part of the file failed");
5695 gst_mxf_demux_eos_single_stream (demux, p);
5696 continue;
5697 }
5698 }
5699
5700 /* and one more time for good measure apparently? */
5701 gst_pad_event_default (pad, parent, event);
5702 ret = (demux->src->len > 0);
5703 break;
5704 }
5705 case GST_EVENT_SEGMENT:{
5706 guint i;
5707
5708 for (i = 0; i < demux->essence_tracks->len; i++) {
5709 GstMXFDemuxEssenceTrack *t =
5710 g_ptr_array_index (demux->essence_tracks, i);
5711 t->position = -1;
5712 }
5713 demux->current_partition = NULL;
5714 demux->seqnum = gst_event_get_seqnum (event);
5715 gst_event_unref (event);
5716 ret = TRUE;
5717 break;
5718 }
5719 default:
5720 ret = gst_pad_event_default (pad, parent, event);
5721 break;
5722 }
5723
5724 out:
5725
5726 return ret;
5727 }
5728
5729 static gboolean
5730 gst_mxf_demux_query (GstElement * element, GstQuery * query)
5731 {
5732 GstMXFDemux *demux = GST_MXF_DEMUX (element);
5733 gboolean ret = FALSE;
5734
5735 GST_DEBUG_OBJECT (demux, "handling query %s",
5736 gst_query_type_get_name (GST_QUERY_TYPE (query)));
5737
5738 switch (GST_QUERY_TYPE (query)) {
5739 case GST_QUERY_POSITION:
5740 {
5741 GstFormat format;
5742 guint64 pos;
5743
5744 gst_query_parse_position (query, &format, NULL);
5745 if (format != GST_FORMAT_TIME)
5746 goto error;
5747
5748 pos = demux->segment.position;
5749
5750 GST_DEBUG_OBJECT (demux,
5751 "Returning position %" G_GUINT64_FORMAT " in format %s", pos,
5752 gst_format_get_name (format));
5753
5754 gst_query_set_position (query, format, pos);
5755 ret = TRUE;
5756
5757 break;
5758 }
5759 case GST_QUERY_DURATION:{
5760 guint64 duration = -1;
5761 GstFormat format;
5762 guint i;
5763
5764 gst_query_parse_duration (query, &format, NULL);
5765 if (format != GST_FORMAT_TIME)
5766 goto error;
5767
5768 if (demux->src->len == 0)
5769 goto done;
5770
5771 g_rw_lock_reader_lock (&demux->metadata_lock);
5772 for (i = 0; i < demux->src->len; i++) {
5773 GstMXFDemuxPad *pad = g_ptr_array_index (demux->src, i);
5774 guint64 pdur = -1;
5775
5776 if (!pad->material_track || !pad->material_track->parent.sequence)
5777 continue;
5778
5779 pdur = pad->material_track->parent.sequence->duration;
5780 if (pad->material_track->edit_rate.n == 0 ||
5781 pad->material_track->edit_rate.d == 0 || pdur == -1)
5782 continue;
5783
5784 pdur =
5785 gst_util_uint64_scale (pdur,
5786 GST_SECOND * pad->material_track->edit_rate.d,
5787 pad->material_track->edit_rate.n);
5788 duration = MAX (duration, pdur);
5789 }
5790 g_rw_lock_reader_unlock (&demux->metadata_lock);
5791
5792 if (duration == -1) {
5793 GST_DEBUG_OBJECT (demux, "No duration known (yet)");
5794 goto done;
5795 }
5796
5797 GST_DEBUG_OBJECT (demux,
5798 "Returning duration %" G_GUINT64_FORMAT " in format %s", duration,
5799 gst_format_get_name (format));
5800
5801 gst_query_set_duration (query, format, duration);
5802 ret = TRUE;
5803 break;
5804 }
5805 case GST_QUERY_SEEKING:{
5806 GstFormat fmt;
5807
5808 ret = TRUE;
5809 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
5810 if (fmt != GST_FORMAT_TIME) {
5811 gst_query_set_seeking (query, fmt, FALSE, -1, -1);
5812 goto done;
5813 }
5814
5815 if (demux->random_access) {
5816 gst_query_set_seeking (query, GST_FORMAT_TIME, TRUE, 0, -1);
5817 } else {
5818 GstQuery *peerquery = gst_query_new_seeking (GST_FORMAT_BYTES);
5819 gboolean seekable;
5820
5821 seekable = gst_pad_peer_query (demux->sinkpad, peerquery);
5822 if (seekable)
5823 gst_query_parse_seeking (peerquery, NULL, &seekable, NULL, NULL);
5824 if (seekable)
5825 gst_query_set_seeking (query, GST_FORMAT_TIME, TRUE, 0, -1);
5826 else
5827 gst_query_set_seeking (query, GST_FORMAT_TIME, FALSE, -1, -1);
5828 }
5829
5830 break;
5831 }
5832 case GST_QUERY_SEGMENT:{
5833 GstFormat format;
5834 guint64 start, stop;
5835
5836 format = demux->segment.format;
5837
5838 start =
5839 gst_segment_to_stream_time (&demux->segment, format,
5840 demux->segment.start);
5841 if ((stop = demux->segment.stop) == -1)
5842 stop = demux->segment.duration;
5843 else
5844 stop = gst_segment_to_stream_time (&demux->segment, format, stop);
5845
5846 gst_query_set_segment (query, demux->segment.rate, format, start, stop);
5847 ret = TRUE;
5848 break;
5849 }
5850 default:
5851 /* else forward upstream */
5852 ret = gst_pad_peer_query (demux->sinkpad, query);
5853 break;
5854 }
5855
5856 done:
5857 return ret;
5858
5859 /* ERRORS */
5860 error:
5861 {
5862 GST_DEBUG_OBJECT (demux, "query failed");
5863 goto done;
5864 }
5865 }
5866
5867 static GstStateChangeReturn
5868 gst_mxf_demux_change_state (GstElement * element, GstStateChange transition)
5869 {
5870 GstMXFDemux *demux = GST_MXF_DEMUX (element);
5871 GstStateChangeReturn ret;
5872
5873 switch (transition) {
5874 case GST_STATE_CHANGE_READY_TO_PAUSED:
5875 demux->seqnum = gst_util_seqnum_next ();
5876 break;
5877 default:
5878 break;
5879 }
5880
5881 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
5882 if (ret == GST_STATE_CHANGE_FAILURE)
5883 return ret;
5884
5885 switch (transition) {
5886 case GST_STATE_CHANGE_PAUSED_TO_READY:
5887 gst_mxf_demux_reset (demux);
5888 break;
5889 default:
5890 break;
5891 }
5892
5893 return ret;
5894 }
5895
5896 static void
5897 gst_mxf_demux_set_property (GObject * object, guint prop_id,
5898 const GValue * value, GParamSpec * pspec)
5899 {
5900 GstMXFDemux *demux = GST_MXF_DEMUX (object);
5901
5902 switch (prop_id) {
5903 case PROP_PACKAGE:
5904 g_free (demux->requested_package_string);
5905 demux->requested_package_string = g_value_dup_string (value);
5906 break;
5907 case PROP_MAX_DRIFT:
5908 demux->max_drift = g_value_get_uint64 (value);
5909 break;
5910 default:
5911 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5912 break;
5913 }
5914 }
5915
5916 static void
5917 gst_mxf_demux_get_property (GObject * object, guint prop_id,
5918 GValue * value, GParamSpec * pspec)
5919 {
5920 GstMXFDemux *demux = GST_MXF_DEMUX (object);
5921
5922 switch (prop_id) {
5923 case PROP_PACKAGE:
5924 g_value_set_string (value, demux->current_package_string);
5925 break;
5926 case PROP_MAX_DRIFT:
5927 g_value_set_uint64 (value, demux->max_drift);
5928 break;
5929 case PROP_STRUCTURE:{
5930 GstStructure *s;
5931
5932 g_rw_lock_reader_lock (&demux->metadata_lock);
5933 if (demux->preface &&
5934 MXF_METADATA_BASE (demux->preface)->resolved ==
5935 MXF_METADATA_BASE_RESOLVE_STATE_SUCCESS)
5936 s = mxf_metadata_base_to_structure (MXF_METADATA_BASE (demux->preface));
5937 else
5938 s = NULL;
5939
5940 gst_value_set_structure (value, s);
5941
5942 if (s)
5943 gst_structure_free (s);
5944
5945 g_rw_lock_reader_unlock (&demux->metadata_lock);
5946 break;
5947 }
5948 default:
5949 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5950 break;
5951 }
5952 }
5953
5954 static void
5955 gst_mxf_demux_finalize (GObject * object)
5956 {
5957 GstMXFDemux *demux = GST_MXF_DEMUX (object);
5958
5959 gst_mxf_demux_reset (demux);
5960
5961 if (demux->adapter) {
5962 g_object_unref (demux->adapter);
5963 demux->adapter = NULL;
5964 }
5965
5966 if (demux->flowcombiner) {
5967 gst_flow_combiner_free (demux->flowcombiner);
5968 demux->flowcombiner = NULL;
5969 }
5970
5971 g_free (demux->current_package_string);
5972 demux->current_package_string = NULL;
5973 g_free (demux->requested_package_string);
5974 demux->requested_package_string = NULL;
5975
5976 g_ptr_array_free (demux->src, TRUE);
5977 demux->src = NULL;
5978 g_ptr_array_free (demux->essence_tracks, TRUE);
5979 demux->essence_tracks = NULL;
5980
5981 g_hash_table_destroy (demux->metadata);
5982
5983 g_rw_lock_clear (&demux->metadata_lock);
5984
5985 G_OBJECT_CLASS (parent_class)->finalize (object);
5986 }
5987
5988 static void
5989 gst_mxf_demux_class_init (GstMXFDemuxClass * klass)
5990 {
5991 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
5992 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
5993
5994 GST_DEBUG_CATEGORY_INIT (mxfdemux_debug, "mxfdemux", 0, "MXF demuxer");
5995
5996 parent_class = g_type_class_peek_parent (klass);
5997
5998 gobject_class->finalize = gst_mxf_demux_finalize;
5999 gobject_class->set_property = gst_mxf_demux_set_property;
6000 gobject_class->get_property = gst_mxf_demux_get_property;
6001
6002 g_object_class_install_property (gobject_class, PROP_PACKAGE,
6003 g_param_spec_string ("package", "Package",
6004 "Material or Source package to use for playback", NULL,
6005 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
6006
6007 g_object_class_install_property (gobject_class, PROP_MAX_DRIFT,
6008 g_param_spec_uint64 ("max-drift", "Maximum drift",
6009 "Maximum number of nanoseconds by which tracks can differ",
6010 100 * GST_MSECOND, G_MAXUINT64, DEFAULT_MAX_DRIFT,
6011 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
6012
6013 g_object_class_install_property (gobject_class, PROP_STRUCTURE,
6014 g_param_spec_boxed ("structure", "Structure",
6015 "Structural metadata of the MXF file",
6016 GST_TYPE_STRUCTURE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
6017
6018 gstelement_class->change_state =
6019 GST_DEBUG_FUNCPTR (gst_mxf_demux_change_state);
6020 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_mxf_demux_query);
6021
6022 gst_element_class_add_static_pad_template (gstelement_class,
6023 &mxf_sink_template);
6024 gst_element_class_add_static_pad_template (gstelement_class,
6025 &mxf_src_template);
6026 gst_element_class_set_static_metadata (gstelement_class, "MXF Demuxer",
6027 "Codec/Demuxer", "Demux MXF files",
6028 "Sebastian Dröge <[email protected]>");
6029 }
6030
6031 static void
6032 gst_mxf_demux_init (GstMXFDemux * demux)
6033 {
6034 demux->sinkpad =
6035 gst_pad_new_from_static_template (&mxf_sink_template, "sink");
6036
6037 gst_pad_set_event_function (demux->sinkpad,
6038 GST_DEBUG_FUNCPTR (gst_mxf_demux_sink_event));
6039 gst_pad_set_chain_function (demux->sinkpad,
6040 GST_DEBUG_FUNCPTR (gst_mxf_demux_chain));
6041 gst_pad_set_activate_function (demux->sinkpad,
6042 GST_DEBUG_FUNCPTR (gst_mxf_demux_sink_activate));
6043 gst_pad_set_activatemode_function (demux->sinkpad,
6044 GST_DEBUG_FUNCPTR (gst_mxf_demux_sink_activate_mode));
6045
6046 gst_element_add_pad (GST_ELEMENT (demux), demux->sinkpad);
6047
6048 demux->max_drift = DEFAULT_MAX_DRIFT;
6049
6050 demux->adapter = gst_adapter_new ();
6051 demux->flowcombiner = gst_flow_combiner_new ();
6052 g_rw_lock_init (&demux->metadata_lock);
6053
6054 demux->src = g_ptr_array_new ();
6055 demux->essence_tracks = g_ptr_array_new_with_free_func ((GDestroyNotify)
6056 gst_mxf_demux_essence_track_free);
6057
6058 gst_segment_init (&demux->segment, GST_FORMAT_TIME);
6059
6060 gst_mxf_demux_reset (demux);
6061 }