summaryrefslogtreecommitdiffstats
path: root/meta/packages/gstreamer/gst-plugins-bad-0.10.3/ivorbisdec.patch
blob: 7e7afc3f6cf1c42eb7c7dee3bd9da99cf369fa2d (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
--- ../gst-plugins-bad0.10-0.10.3+cvs20060918/configure.ac	2006-09-18 18:35:13.000000000 +0100
+++ ./configure.ac	2006-09-21 14:55:05.000000000 +0100
@@ -526,7 +526,12 @@
   IVORBIS_CFLAGS=
   AC_CHECK_LIB(vorbisidec, vorbis_block_init,
     [IVORBIS_LIBS=-lvorbisidec
-     HAVE_IVORBIS=yes],
+     HAVE_IVORBIS=yes
+     case $host in 
+     arm-*-*)
+       IVORBIS_CFLAGS="-D_ARM_ASSEM_ $IVORBIS_CFLAGS"
+     esac
+    ],
     HAVE_IVORBIS=no)
   AC_SUBST(IVORBIS_LIBS)
   AC_SUBST(IVORBIS_CFLAGS)
diff -urNd ../gst-plugins-bad0.10-0.10.3+cvs20060918/ext/ivorbis/Makefile.am ext/ivorbis/Makefile.am
--- ../gst-plugins-bad0.10-0.10.3+cvs20060918/ext/ivorbis/Makefile.am	2005-11-21 03:21:04.000000000 +0000
+++ ext/ivorbis/Makefile.am	2006-09-20 15:41:30.000000000 +0100
@@ -1,7 +1,7 @@
 
 plugin_LTLIBRARIES = libgstivorbis.la
 
-libgstivorbis_la_SOURCES = vorbis.c vorbisfile.c
+libgstivorbis_la_SOURCES = vorbis.c vorbisfile.c vorbisdec.c
 libgstivorbis_la_CFLAGS = $(GST_CFLAGS) $(IVORBIS_CFLAGS) $(GST_PLUGINS_BASE_CFLAGS)
 libgstivorbis_la_LIBADD = $(IVORBIS_LIBS)		\
 			  $(IVORBISFILE_LIBS)		\
diff -urNd ../gst-plugins-bad0.10-0.10.3+cvs20060918/ext/ivorbis/vorbis.c ext/ivorbis/vorbis.c
--- ../gst-plugins-bad0.10-0.10.3+cvs20060918/ext/ivorbis/vorbis.c	2006-04-01 11:09:05.000000000 +0100
+++ ext/ivorbis/vorbis.c	2006-09-20 18:47:11.000000000 +0100
@@ -23,9 +23,11 @@
 
 #include <gst/gst.h>
 #include <tremor/ivorbiscodec.h>
+#include "vorbisdec.h"
 
-extern GType ivorbisfile_get_type (void);
+GST_DEBUG_CATEGORY (vorbisdec_debug);
 
+extern GType ivorbisfile_get_type (void);
 
 static gboolean
 plugin_init (GstPlugin * plugin)
@@ -34,6 +36,13 @@
           ivorbisfile_get_type ()))
     return FALSE;
 
+  if (!gst_element_register (plugin, "ivorbisdec", GST_RANK_SECONDARY,
+          gst_ivorbis_dec_get_type ()))
+    return FALSE;
+
+  GST_DEBUG_CATEGORY_INIT (vorbisdec_debug, "ivorbisdec", 0,
+      "vorbis decoding element (integer decoder)");
+
   return TRUE;
 }
 
diff -urNd ../gst-plugins-bad0.10-0.10.3+cvs20060918/ext/ivorbis/vorbisdec.c ext/ivorbis/vorbisdec.c
--- ../gst-plugins-bad0.10-0.10.3+cvs20060918/ext/ivorbis/vorbisdec.c	1970-01-01 01:00:00.000000000 +0100
+++ ext/ivorbis/vorbisdec.c	2006-09-21 14:51:25.000000000 +0100
@@ -0,0 +1,1212 @@
+/* GStreamer
+ * Copyright (C) 2004 Benjamin Otte <in7y118@public.uni-hamburg.de>
+ *
+ * Tremor modifications <2006>:
+ *   Chris Lord, OpenedHand Ltd. <chris@openedhand.com>, http://www.o-hand.com/
+ *
+ * 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., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+/**
+ * SECTION:element-vorbisdec
+ * @short_description: a decoder that decodes Vorbis to raw audio
+ * @see_also: vorbisenc, oggdemux
+ *
+ * <refsect2>
+ * <para>
+ * This element decodes a Vorbis stream to raw int audio.
+ * <ulink url="http://www.vorbis.com/">Vorbis</ulink> is a royalty-free
+ * audio codec maintained by the <ulink url="http://www.xiph.org/">Xiph.org
+ * Foundation</ulink>.
+ * </para>
+ * <title>Example pipelines</title>
+ * <para>
+ * <programlisting>
+ * gst-launch -v filesrc location=sine.ogg ! oggdemux ! vorbisdec ! audioconvert ! alsasink
+ * </programlisting>
+ * Decode an Ogg/Vorbis. To create an Ogg/Vorbis file refer to the documentation of vorbisenc.
+ * </para>
+ * </refsect2>
+ *
+ * Last reviewed on 2006-03-01 (0.10.4)
+ */
+
+#ifdef HAVE_CONFIG_H
+#  include "config.h"
+#endif
+
+#include "vorbisdec.h"
+#include <string.h>
+#include <gst/audio/audio.h>
+#include <gst/tag/tag.h>
+#include <gst/audio/multichannel.h>
+
+GST_DEBUG_CATEGORY_EXTERN (vorbisdec_debug);
+#define GST_CAT_DEFAULT vorbisdec_debug
+
+static const GstElementDetails vorbis_dec_details =
+GST_ELEMENT_DETAILS ("Vorbis audio decoder",
+    "Codec/Decoder/Audio",
+    "decode raw vorbis streams to integer audio",
+    "Benjamin Otte <in7y118@public.uni-hamburg.de>\n"
+    "Chris Lord <chris@openedhand.com>");
+
+static GstStaticPadTemplate vorbis_dec_src_factory =
+GST_STATIC_PAD_TEMPLATE ("src",
+    GST_PAD_SRC,
+    GST_PAD_ALWAYS,
+    GST_STATIC_CAPS ("audio/x-raw-int, "
+        "rate = (int) [ 1, MAX ], "
+        "channels = (int) [ 1, 6 ], "
+        "endianness = (int) BYTE_ORDER, "
+        "width = (int) 32, "
+        "depth = (int) 16, "
+        "signed = (boolean) true")
+    );
+
+static GstStaticPadTemplate vorbis_dec_sink_factory =
+GST_STATIC_PAD_TEMPLATE ("sink",
+    GST_PAD_SINK,
+    GST_PAD_ALWAYS,
+    GST_STATIC_CAPS ("audio/x-vorbis")
+    );
+
+GST_BOILERPLATE (GstIVorbisDec, gst_ivorbis_dec, GstElement, GST_TYPE_ELEMENT);
+
+static void vorbis_dec_finalize (GObject * object);
+static gboolean vorbis_dec_sink_event (GstPad * pad, GstEvent * event);
+static GstFlowReturn vorbis_dec_chain (GstPad * pad, GstBuffer * buffer);
+static GstStateChangeReturn vorbis_dec_change_state (GstElement * element,
+    GstStateChange transition);
+
+static gboolean vorbis_dec_src_event (GstPad * pad, GstEvent * event);
+static gboolean vorbis_dec_src_query (GstPad * pad, GstQuery * query);
+static gboolean vorbis_dec_convert (GstPad * pad,
+    GstFormat src_format, gint64 src_value,
+    GstFormat * dest_format, gint64 * dest_value);
+
+static gboolean vorbis_dec_sink_query (GstPad * pad, GstQuery * query);
+
+static void
+gst_ivorbis_dec_base_init (gpointer g_class)
+{
+  GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
+  GstPadTemplate *src_template, *sink_template;
+
+  src_template = gst_static_pad_template_get (&vorbis_dec_src_factory);
+  gst_element_class_add_pad_template (element_class, src_template);
+
+  sink_template = gst_static_pad_template_get (&vorbis_dec_sink_factory);
+  gst_element_class_add_pad_template (element_class, sink_template);
+
+  gst_element_class_set_details (element_class, &vorbis_dec_details);
+}
+
+static void
+gst_ivorbis_dec_class_init (GstIVorbisDecClass * klass)
+{
+  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
+  GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
+
+  gobject_class->finalize = vorbis_dec_finalize;
+
+  gstelement_class->change_state = GST_DEBUG_FUNCPTR (vorbis_dec_change_state);
+}
+
+static const GstQueryType *
+vorbis_get_query_types (GstPad * pad)
+{
+  static const GstQueryType vorbis_dec_src_query_types[] = {
+    GST_QUERY_POSITION,
+    GST_QUERY_DURATION,
+    GST_QUERY_CONVERT,
+    0
+  };
+
+  return vorbis_dec_src_query_types;
+}
+
+static void
+gst_ivorbis_dec_init (GstIVorbisDec * dec, GstIVorbisDecClass * g_class)
+{
+  dec->sinkpad = gst_pad_new_from_static_template (&vorbis_dec_sink_factory,
+      "sink");
+
+  gst_pad_set_event_function (dec->sinkpad,
+      GST_DEBUG_FUNCPTR (vorbis_dec_sink_event));
+  gst_pad_set_chain_function (dec->sinkpad,
+      GST_DEBUG_FUNCPTR (vorbis_dec_chain));
+  gst_pad_set_query_function (dec->sinkpad,
+      GST_DEBUG_FUNCPTR (vorbis_dec_sink_query));
+  gst_element_add_pad (GST_ELEMENT (dec), dec->sinkpad);
+
+  dec->srcpad = gst_pad_new_from_static_template (&vorbis_dec_src_factory,
+      "src");
+
+  gst_pad_set_event_function (dec->srcpad,
+      GST_DEBUG_FUNCPTR (vorbis_dec_src_event));
+  gst_pad_set_query_type_function (dec->srcpad,
+      GST_DEBUG_FUNCPTR (vorbis_get_query_types));
+  gst_pad_set_query_function (dec->srcpad,
+      GST_DEBUG_FUNCPTR (vorbis_dec_src_query));
+  gst_pad_use_fixed_caps (dec->srcpad);
+  gst_element_add_pad (GST_ELEMENT (dec), dec->srcpad);
+
+  dec->queued = NULL;
+  dec->pendingevents = NULL;
+  dec->taglist = NULL;
+}
+
+static void
+vorbis_dec_finalize (GObject * object)
+{
+  /* Release any possibly allocated libvorbis data.
+   * _clear functions can safely be called multiple times
+   */
+  GstIVorbisDec *vd = GST_IVORBIS_DEC (object);
+
+  vorbis_block_clear (&vd->vb);
+  vorbis_dsp_clear (&vd->vd);
+  vorbis_comment_clear (&vd->vc);
+  vorbis_info_clear (&vd->vi);
+
+  G_OBJECT_CLASS (parent_class)->finalize (object);
+}
+
+static void
+gst_ivorbis_dec_reset (GstIVorbisDec * dec)
+{
+  GList *walk;
+
+  dec->cur_timestamp = GST_CLOCK_TIME_NONE;
+  dec->prev_timestamp = GST_CLOCK_TIME_NONE;
+  dec->granulepos = -1;
+  dec->discont = TRUE;
+  gst_segment_init (&dec->segment, GST_FORMAT_TIME);
+
+  for (walk = dec->queued; walk; walk = g_list_next (walk)) {
+    gst_buffer_unref (GST_BUFFER_CAST (walk->data));
+  }
+  g_list_free (dec->queued);
+  dec->queued = NULL;
+
+  for (walk = dec->pendingevents; walk; walk = g_list_next (walk)) {
+    gst_event_unref (GST_EVENT_CAST (walk->data));
+  }
+  g_list_free (dec->pendingevents);
+  dec->pendingevents = NULL;
+
+  if (dec->taglist)
+    gst_tag_list_free (dec->taglist);
+  dec->taglist = NULL;
+}
+
+
+static gboolean
+vorbis_dec_convert (GstPad * pad,
+    GstFormat src_format, gint64 src_value,
+    GstFormat * dest_format, gint64 * dest_value)
+{
+  gboolean res = TRUE;
+  GstIVorbisDec *dec;
+  guint64 scale = 1;
+
+  if (src_format == *dest_format) {
+    *dest_value = src_value;
+    return TRUE;
+  }
+
+  dec = GST_IVORBIS_DEC (gst_pad_get_parent (pad));
+
+  if (!dec->initialized)
+    goto no_header;
+
+  if (dec->sinkpad == pad &&
+      (src_format == GST_FORMAT_BYTES || *dest_format == GST_FORMAT_BYTES))
+    goto no_format;
+
+  switch (src_format) {
+    case GST_FORMAT_TIME:
+      switch (*dest_format) {
+        case GST_FORMAT_BYTES:
+          scale = sizeof (gint32) * dec->vi.channels;
+        case GST_FORMAT_DEFAULT:
+          *dest_value =
+              scale * gst_util_uint64_scale_int (src_value, dec->vi.rate,
+              GST_SECOND);
+          break;
+        default:
+          res = FALSE;
+      }
+      break;
+    case GST_FORMAT_DEFAULT:
+      switch (*dest_format) {
+        case GST_FORMAT_BYTES:
+          *dest_value = src_value * sizeof (gint32) * dec->vi.channels;
+          break;
+        case GST_FORMAT_TIME:
+          *dest_value =
+              gst_util_uint64_scale_int (src_value, GST_SECOND, dec->vi.rate);
+          break;
+        default:
+          res = FALSE;
+      }
+      break;
+    case GST_FORMAT_BYTES:
+      switch (*dest_format) {
+        case GST_FORMAT_DEFAULT:
+          *dest_value = src_value / (sizeof (gint32) * dec->vi.channels);
+          break;
+        case GST_FORMAT_TIME:
+          *dest_value = gst_util_uint64_scale_int (src_value, GST_SECOND,
+              dec->vi.rate * sizeof (gint32) * dec->vi.channels);
+          break;
+        default:
+          res = FALSE;
+      }
+      break;
+    default:
+      res = FALSE;
+  }
+done:
+  gst_object_unref (dec);
+
+  return res;
+
+  /* ERRORS */
+no_header:
+  {
+    GST_DEBUG_OBJECT (dec, "no header packets received");
+    res = FALSE;
+    goto done;
+  }
+no_format:
+  {
+    GST_DEBUG_OBJECT (dec, "formats unsupported");
+    res = FALSE;
+    goto done;
+  }
+}
+
+static gboolean
+vorbis_dec_src_query (GstPad * pad, GstQuery * query)
+{
+  GstIVorbisDec *dec;
+  gboolean res = FALSE;
+
+  dec = GST_IVORBIS_DEC (gst_pad_get_parent (pad));
+
+  switch (GST_QUERY_TYPE (query)) {
+    case GST_QUERY_POSITION:
+    {
+      gint64 granulepos, value;
+      GstFormat my_format, format;
+      gint64 time;
+
+      /* we start from the last seen granulepos */
+      granulepos = dec->granulepos;
+
+      gst_query_parse_position (query, &format, NULL);
+
+      /* and convert to the final format in two steps with time as the
+       * intermediate step */
+      my_format = GST_FORMAT_TIME;
+      if (!(res =
+              vorbis_dec_convert (pad, GST_FORMAT_DEFAULT, granulepos,
+                  &my_format, &time)))
+        goto error;
+
+      /* correct for the segment values */
+      time = gst_segment_to_stream_time (&dec->segment, GST_FORMAT_TIME, time);
+
+      GST_LOG_OBJECT (dec,
+          "query %p: our time: %" GST_TIME_FORMAT, query, GST_TIME_ARGS (time));
+
+      /* and convert to the final format */
+      if (!(res = vorbis_dec_convert (pad, my_format, time, &format, &value)))
+        goto error;
+
+      gst_query_set_position (query, format, value);
+
+      GST_LOG_OBJECT (dec,
+          "query %p: we return %lld (format %u)", query, value, format);
+
+      break;
+    }
+    case GST_QUERY_DURATION:
+    {
+      GstPad *peer;
+
+      if (!(peer = gst_pad_get_peer (dec->sinkpad))) {
+        GST_WARNING_OBJECT (dec, "sink pad %" GST_PTR_FORMAT " is not linked",
+            dec->sinkpad);
+        goto error;
+      }
+
+      res = gst_pad_query (peer, query);
+      gst_object_unref (peer);
+      if (!res)
+        goto error;
+
+      break;
+    }
+    case GST_QUERY_CONVERT:
+    {
+      GstFormat src_fmt, dest_fmt;
+      gint64 src_val, dest_val;
+
+      gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
+      if (!(res =
+              vorbis_dec_convert (pad, src_fmt, src_val, &dest_fmt, &dest_val)))
+        goto error;
+      gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
+      break;
+    }
+    default:
+      res = gst_pad_query_default (pad, query);
+      break;
+  }
+done:
+  gst_object_unref (dec);
+
+  return res;
+
+  /* ERRORS */
+error:
+  {
+    GST_WARNING_OBJECT (dec, "error handling query");
+    goto done;
+  }
+}
+
+static gboolean
+vorbis_dec_sink_query (GstPad * pad, GstQuery * query)
+{
+  GstIVorbisDec *dec;
+  gboolean res;
+
+  dec = GST_IVORBIS_DEC (gst_pad_get_parent (pad));
+
+  switch (GST_QUERY_TYPE (query)) {
+    case GST_QUERY_CONVERT:
+    {
+      GstFormat src_fmt, dest_fmt;
+      gint64 src_val, dest_val;
+
+      gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
+      if (!(res =
+              vorbis_dec_convert (pad, src_fmt, src_val, &dest_fmt, &dest_val)))
+        goto error;
+      gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
+      break;
+    }
+    default:
+      res = gst_pad_query_default (pad, query);
+      break;
+  }
+
+done:
+  gst_object_unref (dec);
+
+  return res;
+
+  /* ERRORS */
+error:
+  {
+    GST_DEBUG_OBJECT (dec, "error converting value");
+    goto done;
+  }
+}
+
+static gboolean
+vorbis_dec_src_event (GstPad * pad, GstEvent * event)
+{
+  gboolean res = TRUE;
+  GstIVorbisDec *dec;
+
+  dec = GST_IVORBIS_DEC (gst_pad_get_parent (pad));
+
+  switch (GST_EVENT_TYPE (event)) {
+    case GST_EVENT_SEEK:
+    {
+      GstFormat format, tformat;
+      gdouble rate;
+      GstEvent *real_seek;
+      GstSeekFlags flags;
+      GstSeekType cur_type, stop_type;
+      gint64 cur, stop;
+      gint64 tcur, tstop;
+
+      gst_event_parse_seek (event, &rate, &format, &flags, &cur_type, &cur,
+          &stop_type, &stop);
+      gst_event_unref (event);
+
+      /* we have to ask our peer to seek to time here as we know
+       * nothing about how to generate a granulepos from the src
+       * formats or anything.
+       *
+       * First bring the requested format to time
+       */
+      tformat = GST_FORMAT_TIME;
+      if (!(res = vorbis_dec_convert (pad, format, cur, &tformat, &tcur)))
+        goto convert_error;
+      if (!(res = vorbis_dec_convert (pad, format, stop, &tformat, &tstop)))
+        goto convert_error;
+
+      /* then seek with time on the peer */
+      real_seek = gst_event_new_seek (rate, GST_FORMAT_TIME,
+          flags, cur_type, tcur, stop_type, tstop);
+
+      res = gst_pad_push_event (dec->sinkpad, real_seek);
+
+      break;
+    }
+    default:
+      res = gst_pad_push_event (dec->sinkpad, event);
+      break;
+  }
+done:
+  gst_object_unref (dec);
+
+  return res;
+
+  /* ERRORS */
+convert_error:
+  {
+    GST_DEBUG_OBJECT (dec, "cannot convert start/stop for seek");
+    goto done;
+  }
+}
+
+static gboolean
+vorbis_dec_sink_event (GstPad * pad, GstEvent * event)
+{
+  gboolean ret = FALSE;
+  GstIVorbisDec *dec;
+
+  dec = GST_IVORBIS_DEC (gst_pad_get_parent (pad));
+
+  GST_LOG_OBJECT (dec, "handling event");
+  switch (GST_EVENT_TYPE (event)) {
+    case GST_EVENT_EOS:
+      ret = gst_pad_push_event (dec->srcpad, event);
+      break;
+    case GST_EVENT_FLUSH_START:
+      ret = gst_pad_push_event (dec->srcpad, event);
+      break;
+    case GST_EVENT_FLUSH_STOP:
+      /* here we must clean any state in the decoder */
+#ifdef HAVE_VORBIS_SYNTHESIS_RESTART
+      vorbis_synthesis_restart (&dec->vd);
+#endif
+      gst_ivorbis_dec_reset (dec);
+      ret = gst_pad_push_event (dec->srcpad, event);
+      break;
+    case GST_EVENT_NEWSEGMENT:
+    {
+      GstFormat format;
+      gdouble rate, arate;
+      gint64 start, stop, time;
+      gboolean update;
+
+      gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
+          &start, &stop, &time);
+
+      /* we need time and a positive rate for now */
+      if (format != GST_FORMAT_TIME)
+        goto newseg_wrong_format;
+
+      if (rate <= 0.0)
+        goto newseg_wrong_rate;
+
+      GST_DEBUG_OBJECT (dec,
+          "newsegment: update %d, rate %g, arate %g, start %" GST_TIME_FORMAT
+          ", stop %" GST_TIME_FORMAT ", time %" GST_TIME_FORMAT,
+          update, rate, arate, GST_TIME_ARGS (start), GST_TIME_ARGS (stop),
+          GST_TIME_ARGS (time));
+
+      /* now configure the values */
+      gst_segment_set_newsegment_full (&dec->segment, update,
+          rate, arate, format, start, stop, time);
+
+      if (dec->initialized)
+        /* and forward */
+        ret = gst_pad_push_event (dec->srcpad, event);
+      else {
+        /* store it to send once we're initialized */
+        dec->pendingevents = g_list_append (dec->pendingevents, event);
+        ret = TRUE;
+      }
+      break;
+    }
+    default:
+      ret = gst_pad_push_event (dec->srcpad, event);
+      break;
+  }
+done:
+  gst_object_unref (dec);
+
+  return ret;
+
+  /* ERRORS */
+newseg_wrong_format:
+  {
+    GST_DEBUG_OBJECT (dec, "received non TIME newsegment");
+    goto done;
+  }
+newseg_wrong_rate:
+  {
+    GST_DEBUG_OBJECT (dec, "negative rates not supported yet");
+    goto done;
+  }
+}
+
+static GstFlowReturn
+vorbis_handle_identification_packet (GstIVorbisDec * vd)
+{
+  GstCaps *caps;
+  const GstAudioChannelPosition *pos = NULL;
+
+  switch (vd->vi.channels) {
+    case 1:
+    case 2:
+      /* nothing */
+      break;
+    case 3:{
+      static const GstAudioChannelPosition pos3[] = {
+        GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
+        GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
+        GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT
+      };
+      pos = pos3;
+      break;
+    }
+    case 4:{
+      static const GstAudioChannelPosition pos4[] = {
+        GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
+        GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
+        GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
+        GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT
+      };
+      pos = pos4;
+      break;
+    }
+    case 5:{
+      static const GstAudioChannelPosition pos5[] = {
+        GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
+        GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
+        GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
+        GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
+        GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT
+      };
+      pos = pos5;
+      break;
+    }
+    case 6:{
+      static const GstAudioChannelPosition pos6[] = {
+        GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
+        GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
+        GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
+        GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
+        GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT,
+        GST_AUDIO_CHANNEL_POSITION_LFE
+      };
+      pos = pos6;
+      break;
+    }
+    default:
+      goto channel_count_error;
+  }
+
+  caps = gst_caps_new_simple ("audio/x-raw-int",
+      "rate", G_TYPE_INT, vd->vi.rate,
+      "channels", G_TYPE_INT, vd->vi.channels,
+      "endianness", G_TYPE_INT, G_BYTE_ORDER, "width", G_TYPE_INT, 32,
+      "depth", G_TYPE_INT, 16, "signed", G_TYPE_BOOLEAN, TRUE, NULL);
+
+  if (pos) {
+    gst_audio_set_channel_positions (gst_caps_get_structure (caps, 0), pos);
+  }
+  gst_pad_set_caps (vd->srcpad, caps);
+  gst_caps_unref (caps);
+
+  return GST_FLOW_OK;
+
+  /* ERROR */
+channel_count_error:
+  {
+    GST_ELEMENT_ERROR (vd, STREAM, NOT_IMPLEMENTED, (NULL),
+        ("Unsupported channel count %d", vd->vi.channels));
+    return GST_FLOW_ERROR;
+  }
+}
+
+static GstFlowReturn
+vorbis_handle_comment_packet (GstIVorbisDec * vd, ogg_packet * packet)
+{
+#if 0
+  guint bitrate = 0;
+  gchar *encoder = NULL;
+  GstTagList *list;
+  GstBuffer *buf;
+
+  GST_DEBUG_OBJECT (vd, "parsing comment packet");
+
+  buf = gst_buffer_new_and_alloc (packet->bytes);
+  GST_BUFFER_DATA (buf) = packet->packet;
+
+  list =
+      gst_tag_list_from_vorbiscomment_buffer (buf, (guint8 *) "\003vorbis", 7,
+      &encoder);
+
+  vd->taglist = gst_tag_list_merge (vd->taglist, list, GST_TAG_MERGE_REPLACE);
+
+  gst_tag_list_free (list);
+  gst_buffer_unref (buf);
+
+  if (!vd->taglist) {
+    GST_ERROR_OBJECT (vd, "couldn't decode comments");
+    vd->taglist = gst_tag_list_new ();
+  }
+  if (encoder) {
+    gst_tag_list_add (vd->taglist, GST_TAG_MERGE_REPLACE,
+        GST_TAG_ENCODER, encoder, NULL);
+    g_free (encoder);
+  }
+  gst_tag_list_add (vd->taglist, GST_TAG_MERGE_REPLACE,
+      GST_TAG_ENCODER_VERSION, vd->vi.version,
+      GST_TAG_AUDIO_CODEC, "Vorbis", NULL);
+  if (vd->vi.bitrate_nominal > 0) {
+    gst_tag_list_add (vd->taglist, GST_TAG_MERGE_REPLACE,
+        GST_TAG_NOMINAL_BITRATE, (guint) vd->vi.bitrate_nominal, NULL);
+    bitrate = vd->vi.bitrate_nominal;
+  }
+  if (vd->vi.bitrate_upper > 0) {
+    gst_tag_list_add (vd->taglist, GST_TAG_MERGE_REPLACE,
+        GST_TAG_MAXIMUM_BITRATE, (guint) vd->vi.bitrate_upper, NULL);
+    if (!bitrate)
+      bitrate = vd->vi.bitrate_upper;
+  }
+  if (vd->vi.bitrate_lower > 0) {
+    gst_tag_list_add (vd->taglist, GST_TAG_MERGE_REPLACE,
+        GST_TAG_MINIMUM_BITRATE, (guint) vd->vi.bitrate_lower, NULL);
+    if (!bitrate)
+      bitrate = vd->vi.bitrate_lower;
+  }
+  if (bitrate) {
+    gst_tag_list_add (vd->taglist, GST_TAG_MERGE_REPLACE,
+        GST_TAG_BITRATE, (guint) bitrate, NULL);
+  }
+
+  if (vd->initialized) {
+    gst_element_found_tags_for_pad (GST_ELEMENT_CAST (vd), vd->srcpad,
+        vd->taglist);
+    vd->taglist = NULL;
+  } else {
+    /* Only post them as messages for the time being. *
+     * They will be pushed on the pad once the decoder is initialized */
+    gst_element_post_message (GST_ELEMENT_CAST (vd),
+        gst_message_new_tag (GST_OBJECT (vd), gst_tag_list_copy (vd->taglist)));
+  }
+#endif
+  return GST_FLOW_OK;
+}
+
+static GstFlowReturn
+vorbis_handle_type_packet (GstIVorbisDec * vd)
+{
+  GList *walk;
+
+  g_assert (vd->initialized == FALSE);
+
+  vorbis_synthesis_init (&vd->vd, &vd->vi);
+  vorbis_block_init (&vd->vd, &vd->vb);
+  vd->initialized = TRUE;
+
+  if (vd->pendingevents) {
+    for (walk = vd->pendingevents; walk; walk = g_list_next (walk))
+      gst_pad_push_event (vd->srcpad, GST_EVENT_CAST (walk->data));
+    g_list_free (vd->pendingevents);
+    vd->pendingevents = NULL;
+  }
+
+  if (vd->taglist) {
+    /* The tags have already been sent on the bus as messages. */
+    gst_pad_push_event (vd->srcpad, gst_event_new_tag (vd->taglist));
+    vd->taglist = NULL;
+  }
+
+  return GST_FLOW_OK;
+}
+
+static GstFlowReturn
+vorbis_handle_header_packet (GstIVorbisDec * vd, ogg_packet * packet)
+{
+  GstFlowReturn res;
+
+  GST_DEBUG_OBJECT (vd, "parsing header packet");
+
+  /* Packetno = 0 if the first byte is exactly 0x01 */
+  packet->b_o_s = packet->packet->length ?
+    ((packet->packet->buffer->data[0] == 0x1) ? 1 : 0) : 0;
+
+  if (vorbis_synthesis_headerin (&vd->vi, &vd->vc, packet))
+    goto header_read_error;
+
+  switch (packet->packet->length ? packet->packet->buffer->data[0] : 0x0) {
+    case 0x01:
+      res = vorbis_handle_identification_packet (vd);
+      break;
+    case 0x03:
+      res = vorbis_handle_comment_packet (vd, packet);
+      break;
+    case 0x05:
+      res = vorbis_handle_type_packet (vd);
+      break;
+    default:
+      /* ignore */
+      g_warning ("unknown vorbis header packet found");
+      res = GST_FLOW_OK;
+      break;
+  }
+  return res;
+
+  /* ERRORS */
+header_read_error:
+  {
+    GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
+        (NULL), ("couldn't read header packet"));
+    return GST_FLOW_ERROR;
+  }
+}
+
+/* Taken from Tremor, misc.h */
+#ifdef _ARM_ASSEM_
+static inline ogg_int32_t CLIP_TO_15(ogg_int32_t x) {
+  int tmp;
+  asm volatile("subs	%1, %0, #32768\n\t"
+	       "movpl	%0, #0x7f00\n\t"
+	       "orrpl	%0, %0, #0xff\n"
+	       "adds	%1, %0, #32768\n\t"
+	       "movmi	%0, #0x8000"
+	       : "+r"(x),"=r"(tmp)
+	       :
+	       : "cc");
+  return(x);
+}
+#else
+static inline ogg_int32_t CLIP_TO_15(ogg_int32_t x) {
+  int ret=x;
+  ret-= ((x<=32767)-1)&(x-32767);
+  ret-= ((x>=-32768)-1)&(x+32768);
+  return(ret);
+}
+#endif
+
+static void
+copy_samples (gint32 *out, ogg_int32_t **in, guint samples, gint channels)
+{
+  gint i, j;
+
+  for (j = 0; j < samples; j++) {
+    for (i = 0; i < channels; i++) {
+      *out++ = CLIP_TO_15 (in[i][j]>>9);
+    }
+  }
+}
+
+/* clip output samples to the segment boundaries
+ */
+static gboolean
+vorbis_do_clip (GstIVorbisDec * dec, GstBuffer * buf)
+{
+  gint64 start, stop, cstart, cstop, diff;
+
+  start = GST_BUFFER_TIMESTAMP (buf);
+  stop = start + GST_BUFFER_DURATION (buf);
+
+  if (!gst_segment_clip (&dec->segment, GST_FORMAT_TIME,
+          start, stop, &cstart, &cstop))
+    goto clipped;
+
+  /* see if some clipping happened */
+  diff = cstart - start;
+  if (diff > 0) {
+    GST_BUFFER_TIMESTAMP (buf) = cstart;
+    GST_BUFFER_DURATION (buf) -= diff;
+
+    /* bring clipped time to samples */
+    diff = gst_util_uint64_scale_int (diff, dec->vi.rate, GST_SECOND);
+    /* samples to bytes */
+    diff *= (sizeof (gint32) * dec->vi.channels);
+    GST_DEBUG_OBJECT (dec, "clipping start to %" GST_TIME_FORMAT " %"
+        G_GUINT64_FORMAT " bytes", GST_TIME_ARGS (cstart), diff);
+    GST_BUFFER_DATA (buf) += diff;
+    GST_BUFFER_SIZE (buf) -= diff;
+  }
+  diff = stop - cstop;
+  if (diff > 0) {
+    GST_BUFFER_DURATION (buf) -= diff;
+
+    /* bring clipped time to samples and then to bytes */
+    diff = gst_util_uint64_scale_int (diff, dec->vi.rate, GST_SECOND);
+    diff *= (sizeof (gint32) * dec->vi.channels);
+    GST_DEBUG_OBJECT (dec, "clipping stop to %" GST_TIME_FORMAT " %"
+        G_GUINT64_FORMAT " bytes", GST_TIME_ARGS (cstop), diff);
+    GST_BUFFER_SIZE (buf) -= diff;
+  }
+
+  return FALSE;
+
+  /* dropped buffer */
+clipped:
+  {
+    GST_DEBUG_OBJECT (dec, "clipped buffer");
+    gst_buffer_unref (buf);
+    return TRUE;
+  }
+}
+
+static GstFlowReturn
+vorbis_dec_push (GstIVorbisDec * dec, GstBuffer * buf)
+{
+  GstFlowReturn result;
+  gint64 outoffset = GST_BUFFER_OFFSET (buf);
+
+  if (outoffset == -1) {
+    dec->queued = g_list_append (dec->queued, buf);
+    GST_DEBUG_OBJECT (dec, "queued buffer");
+    result = GST_FLOW_OK;
+  } else {
+    if (G_UNLIKELY (dec->queued)) {
+      gint64 size;
+      GList *walk;
+
+      GST_DEBUG_OBJECT (dec, "first buffer with offset %lld", outoffset);
+
+      size = g_list_length (dec->queued);
+      for (walk = g_list_last (dec->queued); walk;
+          walk = g_list_previous (walk)) {
+        GstBuffer *buffer = GST_BUFFER (walk->data);
+
+        outoffset -=
+            GST_BUFFER_SIZE (buffer) / (sizeof (gint32) * dec->vi.channels);
+
+        GST_BUFFER_OFFSET (buffer) = outoffset;
+        GST_BUFFER_TIMESTAMP (buffer) =
+            gst_util_uint64_scale_int (outoffset, GST_SECOND, dec->vi.rate);
+        GST_DEBUG_OBJECT (dec, "patch buffer %" G_GUINT64_FORMAT
+            " offset %" G_GUINT64_FORMAT, size, outoffset);
+        size--;
+      }
+      for (walk = dec->queued; walk; walk = g_list_next (walk)) {
+        GstBuffer *buffer = GST_BUFFER (walk->data);
+
+        /* clips or returns FALSE with buffer unreffed when completely
+         * clipped */
+        if (vorbis_do_clip (dec, buffer))
+          continue;
+
+        if (dec->discont) {
+          GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
+          dec->discont = FALSE;
+        }
+        /* ignore the result */
+        gst_pad_push (dec->srcpad, buffer);
+      }
+      g_list_free (dec->queued);
+      dec->queued = NULL;
+    }
+
+    /* clip */
+    if (vorbis_do_clip (dec, buf))
+      return GST_FLOW_OK;
+
+    if (dec->discont) {
+      GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
+      dec->discont = FALSE;
+    }
+    result = gst_pad_push (dec->srcpad, buf);
+  }
+
+  return result;
+}
+
+static GstFlowReturn
+vorbis_handle_data_packet (GstIVorbisDec * vd, ogg_packet * packet)
+{
+  ogg_int32_t **pcm;
+  guint sample_count;
+  GstBuffer *out;
+  GstFlowReturn result;
+  gint size;
+
+  if (!vd->initialized)
+    goto not_initialized;
+
+  /* FIXME, we should queue undecoded packets here until we get
+   * a timestamp, then we reverse timestamp the queued packets and
+   * clip them, then we decode only the ones we want and don't
+   * keep decoded data in memory.
+   * Ideally, of course, the demuxer gives us a valid timestamp on
+   * the first packet.
+   */
+
+  /* normal data packet */
+  /* FIXME, we can skip decoding if the packet is outside of the
+   * segment, this is however not very trivial as we need a previous
+   * packet to decode the current one so we must be carefull not to
+   * throw away too much. For now we decode everything and clip right
+   * before pushing data. */
+  if (G_UNLIKELY (vorbis_synthesis (&vd->vb, packet,1)))
+    goto could_not_read;
+
+  if (G_UNLIKELY (vorbis_synthesis_blockin (&vd->vd, &vd->vb) < 0))
+    goto not_accepted;
+
+  /* assume all goes well here */
+  result = GST_FLOW_OK;
+
+  /* count samples ready for reading */
+  if ((sample_count = vorbis_synthesis_pcmout (&vd->vd, NULL)) == 0)
+    goto done;
+
+  size = sample_count * vd->vi.channels * sizeof (gint32);
+
+  /* alloc buffer for it */
+  result =
+      gst_pad_alloc_buffer_and_set_caps (vd->srcpad, GST_BUFFER_OFFSET_NONE,
+      size, GST_PAD_CAPS (vd->srcpad), &out);
+  if (G_UNLIKELY (result != GST_FLOW_OK))
+    goto done;
+
+  /* get samples ready for reading now, should be sample_count */
+  if (G_UNLIKELY ((vorbis_synthesis_pcmout (&vd->vd, &pcm)) != sample_count))
+    goto wrong_samples;
+
+  /* copy samples in buffer */
+  copy_samples ((gint32 *) GST_BUFFER_DATA (out), pcm, sample_count,
+      vd->vi.channels);
+
+  GST_BUFFER_SIZE (out) = size;
+  GST_BUFFER_OFFSET (out) = vd->granulepos;
+  if (vd->granulepos != -1) {
+    GST_BUFFER_OFFSET_END (out) = vd->granulepos + sample_count;
+    GST_BUFFER_TIMESTAMP (out) =
+        gst_util_uint64_scale_int (vd->granulepos, GST_SECOND, vd->vi.rate);
+  } else {
+    GST_BUFFER_TIMESTAMP (out) = -1;
+  }
+  /* this should not overflow */
+  GST_BUFFER_DURATION (out) = sample_count * GST_SECOND / vd->vi.rate;
+
+  if (vd->cur_timestamp != GST_CLOCK_TIME_NONE) {
+    GST_BUFFER_TIMESTAMP (out) = vd->cur_timestamp;
+    GST_DEBUG_OBJECT (vd,
+        "cur_timestamp: %" GST_TIME_FORMAT " + %" GST_TIME_FORMAT " = % "
+        GST_TIME_FORMAT, GST_TIME_ARGS (vd->cur_timestamp),
+        GST_TIME_ARGS (GST_BUFFER_DURATION (out)),
+        GST_TIME_ARGS (vd->cur_timestamp + GST_BUFFER_DURATION (out)));
+    vd->cur_timestamp += GST_BUFFER_DURATION (out);
+    GST_BUFFER_OFFSET (out) = GST_CLOCK_TIME_TO_FRAMES (vd->cur_timestamp,
+        vd->vi.rate);
+    GST_BUFFER_OFFSET_END (out) = GST_BUFFER_OFFSET (out) + sample_count;
+  }
+
+  if (vd->granulepos != -1)
+    vd->granulepos += sample_count;
+
+  result = vorbis_dec_push (vd, out);
+
+done:
+  vorbis_synthesis_read (&vd->vd, sample_count);
+
+  /* granulepos is the last sample in the packet */
+  if (packet->granulepos != -1)
+    vd->granulepos = packet->granulepos;
+
+  return result;
+
+  /* ERRORS */
+not_initialized:
+  {
+    GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
+        (NULL), ("no header sent yet"));
+    return GST_FLOW_ERROR;
+  }
+could_not_read:
+  {
+    GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
+        (NULL), ("couldn't read data packet"));
+    return GST_FLOW_ERROR;
+  }
+not_accepted:
+  {
+    GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
+        (NULL), ("vorbis decoder did not accept data packet"));
+    return GST_FLOW_ERROR;
+  }
+wrong_samples:
+  {
+    gst_buffer_unref (out);
+    GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
+        (NULL), ("vorbis decoder reported wrong number of samples"));
+    return GST_FLOW_ERROR;
+  }
+}
+
+static GstFlowReturn
+vorbis_dec_chain (GstPad * pad, GstBuffer * buffer)
+{
+  GstIVorbisDec *vd;
+  ogg_packet packet;
+  ogg_reference ref;
+  ogg_buffer buf;
+  GstFlowReturn result = GST_FLOW_OK;
+  GstClockTime timestamp;
+  guint64 offset_end;
+
+  vd = GST_IVORBIS_DEC (gst_pad_get_parent (pad));
+
+  /* resync on DISCONT */
+  if (G_UNLIKELY (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DISCONT))) {
+    GST_DEBUG_OBJECT (vd, "received DISCONT buffer");
+    vd->granulepos = -1;
+    vd->cur_timestamp = GST_CLOCK_TIME_NONE;
+    vd->prev_timestamp = GST_CLOCK_TIME_NONE;
+#ifdef HAVE_VORBIS_SYNTHESIS_RESTART
+    vorbis_synthesis_restart (&vd->vd);
+#endif
+    vd->discont = TRUE;
+  }
+
+  timestamp = GST_BUFFER_TIMESTAMP (buffer);
+  offset_end = GST_BUFFER_OFFSET_END (buffer);
+
+  /* only ogg has granulepos, demuxers of other container formats 
+   * might provide us with timestamps instead (e.g. matroskademux) */
+  if (offset_end == GST_BUFFER_OFFSET_NONE && timestamp != GST_CLOCK_TIME_NONE) {
+    /* we might get multiple consecutive buffers with the same timestamp */
+    if (timestamp != vd->prev_timestamp) {
+      vd->cur_timestamp = timestamp;
+      vd->prev_timestamp = timestamp;
+    }
+  } else {
+    vd->cur_timestamp = GST_CLOCK_TIME_NONE;
+    vd->prev_timestamp = GST_CLOCK_TIME_NONE;
+  }
+
+  /* make ogg_packet out of the buffer */
+  buf.data = GST_BUFFER_DATA (buffer);
+  buf.size = GST_BUFFER_SIZE (buffer);
+  buf.refcount = 1;
+  buf.ptr.owner = NULL;
+  buf.ptr.next = NULL;
+  
+  ref.buffer = &buf;
+  ref.begin = 0;
+  ref.length = buf.size;
+  ref.next = NULL;
+  
+  packet.packet = &ref;
+  packet.bytes = ref.length;
+  packet.granulepos = offset_end;
+  packet.packetno = 0;          /* we don't care */
+  /*
+   * FIXME. Is there anyway to know that this is the last packet and
+   * set e_o_s??
+   * Yes there is, keep one packet at all times and only push out when
+   * you receive a new one.  Implement this.
+   */
+  packet.e_o_s = 0;
+
+  if (G_UNLIKELY (packet.bytes < 1))
+    goto wrong_size;
+
+  GST_DEBUG_OBJECT (vd, "vorbis granule: %" G_GINT64_FORMAT,
+      (gint64) packet.granulepos);
+
+  /* switch depending on packet type */
+  if (buf.data[0] & 1) {
+    if (vd->initialized) {
+      GST_WARNING_OBJECT (vd, "Already initialized, so ignoring header packet");
+      goto done;
+    }
+    result = vorbis_handle_header_packet (vd, &packet);
+  } else {
+    result = vorbis_handle_data_packet (vd, &packet);
+  }
+
+  GST_DEBUG_OBJECT (vd, "offset end: %" G_GUINT64_FORMAT, offset_end);
+
+done:
+  gst_buffer_unref (buffer);
+  gst_object_unref (vd);
+
+  return result;
+
+  /* ERRORS */
+wrong_size:
+  {
+    GST_ELEMENT_ERROR (vd, STREAM, DECODE, (NULL), ("empty buffer received"));
+    result = GST_FLOW_ERROR;
+    vd->discont = TRUE;
+    goto done;
+  }
+}
+
+static GstStateChangeReturn
+vorbis_dec_change_state (GstElement * element, GstStateChange transition)
+{
+  GstIVorbisDec *vd = GST_IVORBIS_DEC (element);
+  GstStateChangeReturn res;
+
+  switch (transition) {
+    case GST_STATE_CHANGE_NULL_TO_READY:
+      break;
+    case GST_STATE_CHANGE_READY_TO_PAUSED:
+      vorbis_info_init (&vd->vi);
+      vorbis_comment_init (&vd->vc);
+      vd->initialized = FALSE;
+      gst_ivorbis_dec_reset (vd);
+      break;
+    case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
+      break;
+    default:
+      break;
+  }
+
+  res = parent_class->change_state (element, transition);
+
+  switch (transition) {
+    case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
+      break;
+    case GST_STATE_CHANGE_PAUSED_TO_READY:
+      GST_DEBUG_OBJECT (vd, "PAUSED -> READY, clearing vorbis structures");
+      vorbis_block_clear (&vd->vb);
+      vorbis_dsp_clear (&vd->vd);
+      vorbis_comment_clear (&vd->vc);
+      vorbis_info_clear (&vd->vi);
+      gst_ivorbis_dec_reset (vd);
+      break;
+    case GST_STATE_CHANGE_READY_TO_NULL:
+      break;
+    default:
+      break;
+  }
+
+  return res;
+}
diff -urNd ../gst-plugins-bad0.10-0.10.3+cvs20060918/ext/ivorbis/vorbisdec.h ext/ivorbis/vorbisdec.h
--- ../gst-plugins-bad0.10-0.10.3+cvs20060918/ext/ivorbis/vorbisdec.h	1970-01-01 01:00:00.000000000 +0100
+++ ext/ivorbis/vorbisdec.h	2006-09-20 17:26:46.000000000 +0100
@@ -0,0 +1,87 @@
+/* -*- c-basic-offset: 2 -*-
+ * GStreamer
+ * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
+ *
+ * Tremor modifications <2006>:
+ *   Chris Lord, OpenedHand Ltd. <chris@openedhand.com>, http://www.o-hand.com/
+ *
+ * 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., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+
+#ifndef __GST_IVORBIS_DEC_H__
+#define __GST_IVORBIS_DEC_H__
+
+
+#include <gst/gst.h>
+#include <tremor/ivorbiscodec.h>
+
+G_BEGIN_DECLS
+
+#define GST_TYPE_IVORBIS_DEC \
+  (gst_ivorbis_dec_get_type())
+#define GST_IVORBIS_DEC(obj) \
+  (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_IVORBIS_DEC,GstIVorbisDec))
+#define GST_IVORBIS_DEC_CLASS(klass) \
+  (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_IVORBIS_DEC,GstIVorbisDecClass))
+#define GST_IS_IVORBIS_DEC(obj) \
+  (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_IVORBIS_DEC))
+#define GST_IS_IVORBIS_DEC_CLASS(klass) \
+  (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_IVORBIS_DEC))
+
+typedef struct _GstIVorbisDec GstIVorbisDec;
+typedef struct _GstIVorbisDecClass GstIVorbisDecClass;
+
+/**
+ * GstIVorbisDec:
+ *
+ * Opaque data structure.
+ */
+struct _GstIVorbisDec {
+  GstElement            element;
+
+  GstPad *              sinkpad;
+  GstPad *              srcpad;
+
+  vorbis_dsp_state      vd;
+  vorbis_info           vi;
+  vorbis_comment        vc;
+  vorbis_block          vb;
+  guint64               granulepos;
+
+  gboolean              initialized;
+
+  GList                 *queued;
+
+  GstSegment		segment;
+  gboolean		discont;
+
+  GstClockTime          cur_timestamp; /* only used with non-ogg container formats */
+  GstClockTime          prev_timestamp; /* only used with non-ogg container formats */
+
+  GList			*pendingevents;
+  GstTagList		*taglist;
+};
+
+struct _GstIVorbisDecClass {
+  GstElementClass parent_class;
+};
+
+GType gst_ivorbis_dec_get_type(void);
+
+G_END_DECLS
+
+#endif /* __GST_IVORBIS_DEC_H__ */