/
gstoggdemux.c
5326 lines (4512 loc) · 160 KB
/
gstoggdemux.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* GStreamer
* Copyright (C) 2004 Wim Taymans <wim@fluendo.com>
*
* gstoggdemux.c: ogg stream demuxer
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
/**
* SECTION:element-oggdemux
* @title: oggdemux
* @see_also: <link linkend="gst-plugins-base-plugins-oggmux">oggmux</link>
*
* This element demuxes ogg files into their encoded audio and video components.
*
* ## Example pipelines
* |[
* gst-launch-1.0 -v filesrc location=test.ogg ! oggdemux ! vorbisdec ! audioconvert ! audioresample ! autoaudiosink
* ]|
* Decodes a vorbis audio stream stored inside an ogg container and plays it.
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <string.h>
#include <gst/gst-i18n-plugin.h>
#include <gst/tag/tag.h>
#include <gst/audio/audio.h>
#include "gstoggelements.h"
#include "gstoggdemux.h"
#define CHUNKSIZE (8500) /* this is out of vorbisfile */
/* we hope we get a granpos within this many bytes off the end */
#define DURATION_CHUNK_OFFSET (128*1024)
/* An Ogg page can not be larger than 255 segments of 255 bytes, plus
26 bytes of header */
#define MAX_OGG_PAGE_SIZE (255 * 255 + 26)
#define GST_FLOW_LIMIT GST_FLOW_CUSTOM_ERROR
#define GST_FLOW_SKIP_PUSH GST_FLOW_CUSTOM_SUCCESS_1
#define SEEK_GIVE_UP_THRESHOLD (3*GST_SECOND)
#define GST_CHAIN_LOCK(ogg) g_mutex_lock(&(ogg)->chain_lock)
#define GST_CHAIN_UNLOCK(ogg) g_mutex_unlock(&(ogg)->chain_lock)
#define GST_PUSH_LOCK(ogg) \
do { \
GST_TRACE_OBJECT(ogg, "Push lock"); \
g_mutex_lock(&(ogg)->push_lock); \
} while(0)
#define GST_PUSH_UNLOCK(ogg) \
do { \
GST_TRACE_OBJECT(ogg, "Push unlock"); \
g_mutex_unlock(&(ogg)->push_lock); \
} while(0)
GST_DEBUG_CATEGORY (gst_ogg_demux_debug);
GST_DEBUG_CATEGORY (gst_ogg_demux_setup_debug);
#define GST_CAT_DEFAULT gst_ogg_demux_debug
static ogg_packet *
_ogg_packet_copy (const ogg_packet * packet)
{
ogg_packet *ret = g_slice_new (ogg_packet);
*ret = *packet;
ret->packet = g_memdup (packet->packet, packet->bytes);
return ret;
}
static void
_ogg_packet_free (ogg_packet * packet)
{
g_free (packet->packet);
g_slice_free (ogg_packet, packet);
}
static ogg_page *
gst_ogg_page_copy (ogg_page * page)
{
ogg_page *p = g_slice_new (ogg_page);
/* make a copy of the page */
p->header = g_memdup (page->header, page->header_len);
p->header_len = page->header_len;
p->body = g_memdup (page->body, page->body_len);
p->body_len = page->body_len;
return p;
}
static void
gst_ogg_page_free (ogg_page * page)
{
g_free (page->header);
g_free (page->body);
g_slice_free (ogg_page, page);
}
static gboolean gst_ogg_demux_collect_chain_info (GstOggDemux * ogg,
GstOggChain * chain);
static gboolean gst_ogg_demux_activate_chain (GstOggDemux * ogg,
GstOggChain * chain, GstEvent * event);
static void gst_ogg_pad_mark_discont (GstOggPad * pad);
static void gst_ogg_chain_mark_discont (GstOggChain * chain);
static gboolean gst_ogg_demux_perform_seek (GstOggDemux * ogg,
GstEvent * event);
static gboolean gst_ogg_demux_receive_event (GstElement * element,
GstEvent * event);
static void gst_ogg_pad_dispose (GObject * object);
static void gst_ogg_pad_finalize (GObject * object);
static gboolean gst_ogg_pad_src_query (GstPad * pad, GstObject * parent,
GstQuery * query);
static gboolean gst_ogg_pad_event (GstPad * pad, GstObject * parent,
GstEvent * event);
static GstOggPad *gst_ogg_chain_get_stream (GstOggChain * chain,
guint32 serialno);
static GstFlowReturn gst_ogg_demux_combine_flows (GstOggDemux * ogg,
GstOggPad * pad, GstFlowReturn ret);
static void gst_ogg_demux_sync_streams (GstOggDemux * ogg);
static GstCaps *gst_ogg_demux_set_header_on_caps (GstOggDemux * ogg,
GstCaps * caps, GList * headers);
static gboolean gst_ogg_demux_send_event (GstOggDemux * ogg, GstEvent * event);
static gboolean gst_ogg_demux_perform_seek_push (GstOggDemux * ogg,
GstEvent * event);
static gboolean gst_ogg_demux_check_duration_push (GstOggDemux * ogg,
GstSeekFlags flags, GstEvent * event);
GType gst_ogg_pad_get_type (void);
G_DEFINE_TYPE (GstOggPad, gst_ogg_pad, GST_TYPE_PAD);
static void
gst_ogg_pad_class_init (GstOggPadClass * klass)
{
GObjectClass *gobject_class;
gobject_class = (GObjectClass *) klass;
gobject_class->dispose = gst_ogg_pad_dispose;
gobject_class->finalize = gst_ogg_pad_finalize;
}
static void
gst_ogg_pad_init (GstOggPad * pad)
{
gst_pad_set_event_function (GST_PAD (pad),
GST_DEBUG_FUNCPTR (gst_ogg_pad_event));
gst_pad_set_query_function (GST_PAD (pad),
GST_DEBUG_FUNCPTR (gst_ogg_pad_src_query));
gst_pad_use_fixed_caps (GST_PAD (pad));
pad->current_granule = -1;
pad->prev_granule = -1;
pad->keyframe_granule = -1;
pad->start_time = GST_CLOCK_TIME_NONE;
pad->position = GST_CLOCK_TIME_NONE;
pad->have_type = FALSE;
pad->continued = NULL;
pad->map.headers = NULL;
pad->map.queued = NULL;
pad->map.granulerate_n = 0;
pad->map.granulerate_d = 0;
pad->map.granuleshift = -1;
}
static void
gst_ogg_pad_dispose (GObject * object)
{
GstOggPad *pad = GST_OGG_PAD (object);
pad->chain = NULL;
pad->ogg = NULL;
g_list_foreach (pad->map.headers, (GFunc) _ogg_packet_free, NULL);
g_list_free (pad->map.headers);
pad->map.headers = NULL;
g_list_foreach (pad->map.queued, (GFunc) _ogg_packet_free, NULL);
g_list_free (pad->map.queued);
pad->map.queued = NULL;
g_free (pad->map.index);
pad->map.index = NULL;
/* clear continued pages */
g_list_foreach (pad->continued, (GFunc) gst_ogg_page_free, NULL);
g_list_free (pad->continued);
pad->continued = NULL;
if (pad->map.caps) {
gst_caps_unref (pad->map.caps);
pad->map.caps = NULL;
}
if (pad->map.taglist) {
gst_tag_list_unref (pad->map.taglist);
pad->map.taglist = NULL;
}
ogg_stream_reset (&pad->map.stream);
G_OBJECT_CLASS (gst_ogg_pad_parent_class)->dispose (object);
}
static void
gst_ogg_pad_finalize (GObject * object)
{
GstOggPad *pad = GST_OGG_PAD (object);
ogg_stream_clear (&pad->map.stream);
G_OBJECT_CLASS (gst_ogg_pad_parent_class)->finalize (object);
}
static gboolean
gst_ogg_pad_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
{
gboolean res = TRUE;
GstOggDemux *ogg;
ogg = GST_OGG_DEMUX (parent);
switch (GST_QUERY_TYPE (query)) {
case GST_QUERY_POSITION:
{
GstFormat format;
GstOggPad *ogg_pad = GST_OGG_PAD (pad);
gst_query_parse_position (query, &format, NULL);
/* can only get position in time */
if (format != GST_FORMAT_TIME)
goto wrong_format;
gst_query_set_position (query, format, ogg_pad->position);
break;
}
case GST_QUERY_DURATION:
{
GstFormat format;
gint64 total_time = -1;
gst_query_parse_duration (query, &format, NULL);
/* can only get duration in time */
if (format != GST_FORMAT_TIME)
goto wrong_format;
if (ogg->total_time != -1) {
/* we can return the total length */
total_time = ogg->total_time;
} else {
gint bitrate = ogg->bitrate;
/* try with length and bitrate */
if (bitrate > 0) {
GstQuery *uquery;
/* ask upstream for total length in bytes */
uquery = gst_query_new_duration (GST_FORMAT_BYTES);
if (gst_pad_peer_query (ogg->sinkpad, uquery)) {
gint64 length;
gst_query_parse_duration (uquery, NULL, &length);
/* estimate using the bitrate */
total_time =
gst_util_uint64_scale (length, 8 * GST_SECOND, bitrate);
GST_LOG_OBJECT (ogg,
"length: %" G_GINT64_FORMAT ", bitrate %d, total_time %"
GST_TIME_FORMAT, length, bitrate, GST_TIME_ARGS (total_time));
}
gst_query_unref (uquery);
}
}
gst_query_set_duration (query, GST_FORMAT_TIME, total_time);
break;
}
case GST_QUERY_SEEKING:
{
GstFormat format;
gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
if (format == GST_FORMAT_TIME) {
gboolean seekable = FALSE;
gint64 stop = -1;
GST_CHAIN_LOCK (ogg);
if (ogg->pullmode) {
seekable = TRUE;
stop = ogg->total_time;
} else if (ogg->push_disable_seeking) {
seekable = FALSE;
} else if (ogg->current_chain == NULL) {
GstQuery *squery;
/* assume we can seek if upstream is seekable in BYTES format */
GST_LOG_OBJECT (ogg, "no current chain, check upstream seekability");
squery = gst_query_new_seeking (GST_FORMAT_BYTES);
if (gst_pad_peer_query (ogg->sinkpad, squery))
gst_query_parse_seeking (squery, NULL, &seekable, NULL, NULL);
else
seekable = FALSE;
gst_query_unref (squery);
} else if (ogg->current_chain->streams->len) {
gint i;
seekable = FALSE;
for (i = 0; i < ogg->current_chain->streams->len; i++) {
GstOggPad *pad =
g_array_index (ogg->current_chain->streams, GstOggPad *, i);
seekable = TRUE;
if (pad->map.index != NULL && pad->map.n_index != 0) {
GstOggIndex *idx;
GstClockTime idx_time;
idx = &pad->map.index[pad->map.n_index - 1];
idx_time =
gst_util_uint64_scale (idx->timestamp, GST_SECOND,
pad->map.kp_denom);
if (stop == -1)
stop = idx_time;
else
stop = MAX (idx_time, stop);
} else {
stop = ogg->push_time_length;
if (stop == -1)
stop = ogg->total_time;
}
}
}
gst_query_set_seeking (query, GST_FORMAT_TIME, seekable, 0, stop);
GST_CHAIN_UNLOCK (ogg);
} else {
res = FALSE;
}
break;
}
case GST_QUERY_SEGMENT:{
GstFormat format;
gint64 start, stop;
format = ogg->segment.format;
start =
gst_segment_to_stream_time (&ogg->segment, format,
ogg->segment.start);
if ((stop = ogg->segment.stop) == -1)
stop = ogg->segment.duration;
else
stop = gst_segment_to_stream_time (&ogg->segment, format, stop);
gst_query_set_segment (query, ogg->segment.rate, format, start, stop);
res = TRUE;
break;
}
default:
res = gst_pad_query_default (pad, parent, query);
break;
}
done:
return res;
/* ERRORS */
wrong_format:
{
GST_DEBUG_OBJECT (ogg, "only query position/duration on TIME is supported");
res = FALSE;
goto done;
}
}
static gboolean
gst_ogg_demux_receive_event (GstElement * element, GstEvent * event)
{
gboolean res;
GstOggDemux *ogg;
ogg = GST_OGG_DEMUX (element);
switch (GST_EVENT_TYPE (event)) {
case GST_EVENT_SEEK:
/* now do the seek */
res = gst_ogg_demux_perform_seek (ogg, event);
gst_event_unref (event);
break;
default:
GST_DEBUG_OBJECT (ogg, "We only handle seek events here");
goto error;
}
return res;
/* ERRORS */
error:
{
GST_DEBUG_OBJECT (ogg, "error handling event");
gst_event_unref (event);
return FALSE;
}
}
static gboolean
gst_ogg_pad_event (GstPad * pad, GstObject * parent, GstEvent * event)
{
gboolean res;
GstOggDemux *ogg;
ogg = GST_OGG_DEMUX (parent);
switch (GST_EVENT_TYPE (event)) {
case GST_EVENT_SEEK:
/* now do the seek */
res = gst_ogg_demux_perform_seek (ogg, event);
gst_event_unref (event);
break;
case GST_EVENT_RECONFIGURE:
GST_OGG_PAD (pad)->last_ret = GST_FLOW_OK;
res = gst_pad_event_default (pad, parent, event);
break;
default:
res = gst_pad_event_default (pad, parent, event);
break;
}
return res;
}
static void
gst_ogg_pad_reset (GstOggPad * pad)
{
ogg_stream_reset (&pad->map.stream);
GST_DEBUG_OBJECT (pad, "doing reset");
/* clear continued pages */
g_list_foreach (pad->continued, (GFunc) gst_ogg_page_free, NULL);
g_list_free (pad->continued);
pad->continued = NULL;
pad->last_ret = GST_FLOW_OK;
pad->position = GST_CLOCK_TIME_NONE;
pad->current_granule = -1;
pad->prev_granule = -1;
pad->keyframe_granule = -1;
pad->is_eos = FALSE;
}
/* queue data, basically takes the packet, puts it in a buffer and store the
* buffer in the queued list. */
static GstFlowReturn
gst_ogg_demux_queue_data (GstOggPad * pad, ogg_packet * packet)
{
#ifndef GST_DISABLE_GST_DEBUG
GstOggDemux *ogg = pad->ogg;
#endif
GST_DEBUG_OBJECT (ogg, "%p queueing data serial %08x",
pad, pad->map.serialno);
pad->map.queued = g_list_append (pad->map.queued, _ogg_packet_copy (packet));
/* we are ok now */
return GST_FLOW_OK;
}
static GstFlowReturn
gst_ogg_demux_chain_peer (GstOggPad * pad, ogg_packet * packet,
gboolean push_headers)
{
GstBuffer *buf = NULL;
GstFlowReturn ret, cret;
GstOggDemux *ogg = pad->ogg;
gint64 current_time;
GstOggChain *chain;
gint64 duration;
gint offset;
gint trim;
GstClockTime out_timestamp, out_duration;
guint64 out_offset, out_offset_end;
gboolean delta_unit = FALSE;
gboolean is_header;
guint64 clip_start = 0, clip_end = 0;
ret = cret = GST_FLOW_OK;
GST_DEBUG_OBJECT (pad, "Chaining %d %d %" GST_TIME_FORMAT " %d %p",
ogg->pullmode, ogg->push_state, GST_TIME_ARGS (ogg->push_time_length),
ogg->push_disable_seeking, ogg->building_chain);
if (G_UNLIKELY (pad->is_eos)) {
GST_DEBUG_OBJECT (pad, "Skipping packet on pad that is eos");
ret = GST_FLOW_EOS;
goto combine;
}
GST_PUSH_LOCK (ogg);
if (!ogg->pullmode && ogg->push_state == PUSH_PLAYING
&& ogg->push_time_length == GST_CLOCK_TIME_NONE
&& !ogg->push_disable_seeking) {
if (!ogg->building_chain) {
/* we got all headers, now try to get duration */
if (!gst_ogg_demux_check_duration_push (ogg, GST_SEEK_FLAG_FLUSH, NULL)) {
GST_PUSH_UNLOCK (ogg);
return GST_FLOW_OK;
}
}
GST_PUSH_UNLOCK (ogg);
return GST_FLOW_OK;
}
GST_PUSH_UNLOCK (ogg);
GST_DEBUG_OBJECT (ogg,
"%p streaming to peer serial %08x", pad, pad->map.serialno);
gst_ogg_stream_update_stats (&pad->map, packet);
if (pad->map.is_ogm) {
const guint8 *data;
long bytes;
data = packet->packet;
bytes = packet->bytes;
if (bytes < 1)
goto empty_packet;
if ((data[0] & 1) || (data[0] & 3 && pad->map.is_ogm_text)) {
/* We don't push header packets for OGM */
goto done;
}
offset = 1 + (((data[0] & 0xc0) >> 6) | ((data[0] & 0x02) << 1));
delta_unit = (((data[0] & 0x08) >> 3) == 0);
trim = 0;
/* Strip trailing \0 for subtitles */
if (pad->map.is_ogm_text) {
while (bytes && data[bytes - 1] == 0) {
trim++;
bytes--;
}
}
} else if (pad->map.is_vp8) {
if ((packet->bytes >= 7 && memcmp (packet->packet, "OVP80\2 ", 7) == 0) ||
packet->b_o_s ||
(packet->bytes >= 5 && memcmp (packet->packet, "OVP80", 5) == 0)) {
/* We don't push header packets for VP8 */
goto done;
}
offset = 0;
trim = 0;
delta_unit = !gst_ogg_stream_packet_is_key_frame (&pad->map, packet);
} else {
offset = 0;
trim = 0;
delta_unit = !gst_ogg_stream_packet_is_key_frame (&pad->map, packet);
}
/* get timing info for the packet */
is_header = gst_ogg_stream_packet_is_header (&pad->map, packet);
if (is_header) {
duration = 0;
GST_DEBUG_OBJECT (ogg, "packet is header");
} else {
duration = gst_ogg_stream_get_packet_duration (&pad->map, packet);
GST_DEBUG_OBJECT (ogg, "packet duration %" G_GUINT64_FORMAT, duration);
}
/* If we get a hole at start, it might be we're catching a stream
* partway through. In that case, if the stream has an index, the
* index might be mooted. However, as it's totally valid to index
* a stream with a hole at start (ie, capturing a live stream and
* then index it), we now check whether the index references some
* offset beyond the byte length (if known). If this is the case,
* we can be reasonably sure we're getting a stream partway, with
* its index being now useless since we don't know how many bytes
* were skipped, preventing us from patching the index offsets to
* match the hole size. */
if (!is_header && ogg->check_index_overflow) {
GstQuery *query;
GstFormat format;
int i;
gint64 length;
gboolean beyond;
if (ogg->current_chain) {
query = gst_query_new_duration (GST_FORMAT_BYTES);
if (gst_pad_peer_query (ogg->sinkpad, query)) {
gst_query_parse_duration (query, &format, &length);
if (format == GST_FORMAT_BYTES && length >= 0) {
for (i = 0; i < ogg->current_chain->streams->len; i++) {
GstOggPad *ipad =
g_array_index (ogg->current_chain->streams, GstOggPad *, i);
if (!ipad->map.index)
continue;
beyond = ipad->map.n_index
&& ipad->map.index[ipad->map.n_index - 1].offset >= length;
if (beyond) {
GST_WARNING_OBJECT (pad, "Index offsets beyond byte length");
if (ipad->discont) {
/* hole - the index is most likely screwed up */
GST_WARNING_OBJECT (ogg, "Discarding entire index");
g_free (ipad->map.index);
ipad->map.index = NULL;
ipad->map.n_index = 0;
} else {
/* no hole - we can just clip the index if needed */
GST_WARNING_OBJECT (ogg, "Clipping index");
while (ipad->map.n_index > 0
&& ipad->map.index[ipad->map.n_index - 1].offset >= length)
ipad->map.n_index--;
if (ipad->map.n_index == 0) {
GST_WARNING_OBJECT (ogg, "The entire index was clipped");
g_free (ipad->map.index);
ipad->map.index = NULL;
}
}
/* We can't trust the total time if the index goes beyond */
ipad->map.total_time = -1;
} else {
/* use total time to update the total ogg time */
if (ogg->total_time == -1) {
ogg->total_time = ipad->map.total_time;
} else if (ipad->map.total_time > 0) {
ogg->total_time = MAX (ogg->total_time, ipad->map.total_time);
}
}
}
}
}
gst_query_unref (query);
}
ogg->check_index_overflow = FALSE;
}
if (packet->b_o_s) {
out_timestamp = GST_CLOCK_TIME_NONE;
out_duration = GST_CLOCK_TIME_NONE;
out_offset = 0;
out_offset_end = -1;
} else {
if (packet->granulepos > -1) {
gint64 granule = gst_ogg_stream_granulepos_to_granule (&pad->map,
packet->granulepos);
if (granule < 0) {
GST_ERROR_OBJECT (ogg,
"granulepos %" G_GINT64_FORMAT " yielded granule %" G_GINT64_FORMAT,
(gint64) packet->granulepos, (gint64) granule);
return GST_FLOW_ERROR;
}
pad->current_granule = granule;
pad->keyframe_granule =
gst_ogg_stream_granulepos_to_key_granule (&pad->map,
packet->granulepos);
GST_DEBUG_OBJECT (ogg, "new granule %" G_GUINT64_FORMAT,
pad->current_granule);
} else if (pad->current_granule != -1) {
pad->current_granule += duration;
if (!delta_unit) {
pad->keyframe_granule = pad->current_granule;
}
GST_DEBUG_OBJECT (ogg, "interpolating granule %" G_GUINT64_FORMAT,
pad->current_granule);
}
if (ogg->segment.rate < 0.0 && pad->current_granule == -1) {
/* negative rates, allow output of packets with no timestamp, let downstream reconstruct */
out_timestamp = -1;
out_duration = -1;
out_offset = -1;
out_offset_end = -1;
pad->prev_granule = -1;
} else {
/* we only push buffers after we have a valid granule. This is done so that
* we nicely skip packets without a timestamp after a seek. This is ok
* because we base our seek on the packet after the page with the smaller
* timestamp. */
if (pad->current_granule == -1) {
pad->prev_granule = -1;
goto no_timestamp;
}
if (pad->map.is_ogm) {
out_timestamp = gst_ogg_stream_granule_to_time (&pad->map,
pad->current_granule);
out_duration = gst_util_uint64_scale (duration,
GST_SECOND * pad->map.granulerate_d, pad->map.granulerate_n);
} else if (pad->map.is_sparse) {
out_timestamp = gst_ogg_stream_granule_to_time (&pad->map,
pad->current_granule);
if (duration == GST_CLOCK_TIME_NONE) {
out_duration = GST_CLOCK_TIME_NONE;
} else {
out_duration = gst_util_uint64_scale (duration,
GST_SECOND * pad->map.granulerate_d, pad->map.granulerate_n);
}
} else {
/* The last packet may be clipped. This will be represented
by the last granule being smaller than what it would otherwise
have been, had no content been clipped. In that case, we
cannot calculate the PTS of the audio from the packet length
and granule. */
if (packet->e_o_s) {
if (pad->prev_granule >= 0)
out_timestamp = gst_ogg_stream_granule_to_time (&pad->map,
pad->prev_granule);
else
out_timestamp = GST_CLOCK_TIME_NONE;
if (pad->map.audio_clipping
&& pad->current_granule < pad->prev_granule + duration) {
clip_end = pad->prev_granule + duration - pad->current_granule;
}
if (pad->map.audio_clipping
&& pad->current_granule - duration < -pad->map.granule_offset) {
if (pad->current_granule >= -pad->map.granule_offset) {
guint64 already_removed =
pad->current_granule >
duration ? pad->current_granule - duration : 0;
clip_start =
already_removed >
-pad->map.granule_offset ? 0 : -pad->map.granule_offset -
already_removed;
} else
clip_start = pad->current_granule;
}
} else {
out_timestamp = gst_ogg_stream_granule_to_time (&pad->map,
pad->current_granule - duration);
if (pad->map.audio_clipping
&& pad->current_granule - duration < -pad->map.granule_offset) {
if (pad->current_granule >= -pad->map.granule_offset) {
guint64 already_removed =
pad->current_granule >
duration ? pad->current_granule - duration : 0;
clip_start =
already_removed >
-pad->map.granule_offset ? 0 : -pad->map.granule_offset -
already_removed;
} else
clip_start = pad->current_granule;
}
}
out_duration =
gst_ogg_stream_granule_to_time (&pad->map,
pad->current_granule) - out_timestamp;
}
out_offset_end =
gst_ogg_stream_granule_to_granulepos (&pad->map,
pad->current_granule, pad->keyframe_granule);
out_offset =
gst_ogg_stream_granule_to_time (&pad->map, pad->current_granule);
}
pad->prev_granule = pad->current_granule;
}
if (G_UNLIKELY (offset + trim > packet->bytes))
goto invalid_packet;
else if (pad->map.is_ogm_text) {
/* check for invalid buffer sizes */
if (G_UNLIKELY (offset + trim >= packet->bytes))
goto empty_packet;
}
if (!pad->added)
goto not_added;
buf = gst_buffer_new_and_alloc (packet->bytes - offset - trim);
if (pad->map.audio_clipping && (clip_start || clip_end)) {
GST_DEBUG_OBJECT (pad,
"Clipping %" G_GUINT64_FORMAT " %" G_GUINT64_FORMAT " (%"
G_GUINT64_FORMAT " / %" G_GUINT64_FORMAT ")", clip_start, clip_end,
clip_start + clip_end, duration);
gst_buffer_add_audio_clipping_meta (buf, GST_FORMAT_DEFAULT, clip_start,
clip_end);
}
/* set delta flag for OGM content */
if (delta_unit)
GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
/* set header flag for buffers that are also in the streamheaders */
if (is_header)
GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_HEADER);
if (packet->packet != NULL) {
/* copy packet in buffer */
gst_buffer_fill (buf, 0, packet->packet + offset,
packet->bytes - offset - trim);
}
GST_BUFFER_TIMESTAMP (buf) = out_timestamp;
GST_BUFFER_DURATION (buf) = out_duration;
GST_BUFFER_OFFSET (buf) = out_offset;
GST_BUFFER_OFFSET_END (buf) = out_offset_end;
/* Mark discont on the buffer */
if (pad->discont) {
GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
if (ogg->segment.rate < 0.0 || GST_BUFFER_TIMESTAMP_IS_VALID (buf))
pad->discont = FALSE;
}
/* don't push the header packets when we are asked to skip them */
if (!packet->b_o_s || push_headers) {
if (pad->last_ret == GST_FLOW_OK) {
GST_LOG_OBJECT (ogg, "Pushing buf %" GST_PTR_FORMAT, buf);
ret = gst_pad_push (GST_PAD_CAST (pad), buf);
} else {
GST_DEBUG_OBJECT (ogg, "not pushing buffer on error pad");
ret = pad->last_ret;
gst_buffer_unref (buf);
}
buf = NULL;
}
/* we're done with skeleton stuff */
if (pad->map.is_skeleton)
goto combine;
/* check if valid granulepos, then we can calculate the current
* position. We know the granule for each packet but we only want to update
* the position when we have a valid granulepos on the packet because else
* our time jumps around for the different streams. */
if (packet->granulepos < 0)
goto combine;
/* convert to time */
current_time = gst_ogg_stream_get_end_time_for_granulepos (&pad->map,
packet->granulepos);
/* convert to stream time */
if ((chain = pad->chain)) {
gint64 chain_start = 0;
if (chain->segment_start != GST_CLOCK_TIME_NONE)
chain_start = chain->segment_start;
current_time = current_time - chain_start + chain->begin_time;
}
/* and store as the current position */
ogg->segment.position = current_time;
GST_DEBUG_OBJECT (ogg, "ogg current time %" GST_TIME_FORMAT
" (%" G_GINT64_FORMAT ")", GST_TIME_ARGS (current_time), current_time);
pad->position = ogg->segment.position;
/* check stream eos */
if (!pad->is_eos && !delta_unit &&
((ogg->segment.rate > 0.0 &&
ogg->segment.stop != GST_CLOCK_TIME_NONE &&
current_time >= ogg->segment.stop) ||
(ogg->segment.rate < 0.0 && current_time <= ogg->segment.start))) {
GST_DEBUG_OBJECT (ogg, "marking pad %p EOS", pad);
pad->is_eos = TRUE;
if (ret == GST_FLOW_OK) {
ret = GST_FLOW_EOS;
}
}
combine:
/* combine flows */
cret = gst_ogg_demux_combine_flows (ogg, pad, ret);
done:
if (buf)
gst_buffer_unref (buf);
/* return combined flow result */
return cret;
/* special cases */
empty_packet:
{
GST_DEBUG_OBJECT (ogg, "Skipping empty packet");
goto done;
}
invalid_packet:
{
GST_DEBUG_OBJECT (ogg, "Skipping invalid packet");
goto done;
}
no_timestamp:
{
GST_DEBUG_OBJECT (ogg, "skipping packet: no valid granule found yet");
goto done;
}
not_added:
{
GST_DEBUG_OBJECT (ogg, "pad not added yet");
goto done;
}
}
static guint64
gst_ogg_demux_collect_start_time (GstOggDemux * ogg, GstOggChain * chain)
{
gint i;
guint64 start_time = G_MAXUINT64;
for (i = 0; i < chain->streams->len; i++) {
GstOggPad *pad = g_array_index (chain->streams, GstOggPad *, i);
if (pad->map.is_skeleton)
continue;
/* can do this if the pad start time is not defined */
GST_DEBUG_OBJECT (ogg, "Pad %08x (%s) start time is %" GST_TIME_FORMAT,
pad->map.serialno, gst_ogg_stream_get_media_type (&pad->map),
GST_TIME_ARGS (pad->start_time));
if (pad->start_time == GST_CLOCK_TIME_NONE) {
if (!pad->map.is_sparse) {
start_time = G_MAXUINT64;
break;
}
} else {
start_time = MIN (start_time, pad->start_time);
}
}
return start_time;
}
static GstClockTime
gst_ogg_demux_collect_sync_time (GstOggDemux * ogg, GstOggChain * chain)
{
gint i;
GstClockTime sync_time = GST_CLOCK_TIME_NONE;
if (!chain) {
GST_WARNING_OBJECT (ogg, "No chain!");
return GST_CLOCK_TIME_NONE;
}
for (i = 0; i < chain->streams->len; i++) {
GstOggPad *pad = g_array_index (chain->streams, GstOggPad *, i);
if (pad->map.is_sparse)
continue;
if (pad->push_sync_time == GST_CLOCK_TIME_NONE) {
sync_time = GST_CLOCK_TIME_NONE;
break;
} else {
if (sync_time == GST_CLOCK_TIME_NONE)
sync_time = pad->push_sync_time;
else
sync_time = MAX (sync_time, pad->push_sync_time);
}
}
return sync_time;
}
/* submit a packet to the oggpad, this function will run the type detection
* code for the pad if this is the first packet for this stream
*/
static GstFlowReturn
gst_ogg_pad_submit_packet (GstOggPad * pad, ogg_packet * packet)