aboutsummaryrefslogtreecommitdiffhomepage
path: root/libmpdemux/demux_ogg.c
blob: 8c4046d3fd35fd2698123562cba97b2ba0a532d9 (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
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691

#include "config.h"

unsigned int store_ughvlc(unsigned char *s, unsigned int v)
{
  unsigned int n = 0;

  while(v >= 0xff)
  {
    *s++ = 0xff;
    v -= 0xff;
    n++;
  }
  *s = v;
  n++;

  return n;
}

#ifdef HAVE_OGGVORBIS

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <math.h>
#include <inttypes.h>

#include "mp_msg.h"
#include "help_mp.h"
#include "stream.h"
#include "demuxer.h"
#include "stheader.h"

#define FOURCC_VORBIS mmioFOURCC('v', 'r', 'b', 's')
#define FOURCC_SPEEX  mmioFOURCC('s', 'p', 'x', ' ')
#define FOURCC_THEORA mmioFOURCC('t', 'h', 'e', 'o')

#ifdef TREMOR
#include <tremor/ogg.h>
#include <tremor/ivorbiscodec.h>
#else
#include <ogg/ogg.h>
#include <vorbis/codec.h>
#endif

#ifdef HAVE_OGGTHEORA
#include <theora/theora.h>
extern int _ilog (unsigned int); /* defined in many places in theora/lib/ */
#endif

#define BLOCK_SIZE 4096

/// Vorbis decoder context : we need the vorbis_info for vorbis timestamping
/// Shall we put this struct def in a common header ?
typedef struct ov_struct_st {
  vorbis_info      vi; /* struct that stores all the static vorbis bitstream
			  settings */
  vorbis_comment   vc; /* struct that stores all the bitstream user comments */
  vorbis_dsp_state vd; /* central working state for the packet->PCM decoder */
  vorbis_block     vb; /* local working space for packet->PCM decode */
  float            rg_scale; /* replaygain scale */
#ifdef TREMOR
  int              rg_scale_int;
#endif
} ov_struct_t;

/* Theora decoder context : we won't be able to interpret granule positions
 * without using theora_granule_time with the theora_state of the stream.
 * This is duplicated in `vd_theora.c'; put this in a common header?
 */
#ifdef HAVE_OGGTHEORA
typedef struct theora_struct_st {
    theora_state st;
    theora_comment cc;
    theora_info inf;
} theora_struct_t;
#endif

//// OggDS headers
// Header for the new header format
typedef struct stream_header_video
{
	ogg_int32_t	width;
	ogg_int32_t	height;
} stream_header_video;
	
typedef struct stream_header_audio
{
	ogg_int16_t	channels;
	ogg_int16_t	blockalign;
	ogg_int32_t	avgbytespersec;
} stream_header_audio;

typedef struct __attribute__((__packed__)) stream_header
{
	char	streamtype[8];
	char	subtype[4];

	ogg_int32_t	size;				// size of the structure

	ogg_int64_t	time_unit;			// in reference time
	ogg_int64_t	samples_per_unit;
	ogg_int32_t default_len;		// in media time

	ogg_int32_t buffersize;
	ogg_int16_t	bits_per_sample;

	ogg_int16_t padding;

	union
	{
		// Video specific
		stream_header_video	video;
		// Audio specific
		stream_header_audio	audio;
	} sh;
} stream_header;

/// Our private datas

typedef struct ogg_syncpoint {
  int64_t granulepos;
  off_t page_pos;
} ogg_syncpoint_t;

/// A logical stream
typedef struct ogg_stream {
  /// Timestamping stuff
  float samplerate; /// granulpos 2 time
  int64_t lastpos;
  int32_t lastsize;

  // Logical stream state
  ogg_stream_state stream;
  int hdr_packets;
  int vorbis;
  int speex;
  int theora;
  int flac;
  int text;
  int id;

  void *ogg_d;
} ogg_stream_t;

typedef struct ogg_demuxer {
  /// Physical stream state
  ogg_sync_state sync;
  /// Current page
  ogg_page page;
  /// Logical streams
  ogg_stream_t *subs;
  int num_sub;
  ogg_syncpoint_t* syncpoints;
  int num_syncpoint;
  off_t pos, last_size;
  int64_t final_granulepos;

  /* Used for subtitle switching. */
  int n_text;
  int *text_ids;
  char **text_langs;

  vorbis_info      vi;
  vorbis_comment   vc;
  int vi_inited;
} ogg_demuxer_t;

#define NUM_VORBIS_HDR_PACKETS 3

/// Some defines from OggDS
#define PACKET_TYPE_HEADER   0x01
#define PACKET_TYPE_BITS         0x07
#define PACKET_LEN_BITS01       0xc0
#define PACKET_LEN_BITS2         0x02
#define PACKET_IS_SYNCPOINT  0x08

extern int index_mode;

extern char *dvdsub_lang, *audio_lang;
extern int dvdsub_id;
extern int demux_aid_vid_mismatch;

//-------- subtitle support - should be moved to decoder layer, and queue
//                          - subtitles up in demuxer buffer...

#include "subreader.h"
#include "libvo/sub.h"
#define OGG_SUB_MAX_LINE 128

static subtitle ogg_sub;
extern subtitle* vo_sub;
static float clear_sub;
//FILE* subout;

static
uint16_t get_uint16 (const void *buf)
{
  uint16_t      ret;
  unsigned char *tmp;

  tmp = (unsigned char *) buf;

  ret = tmp[1] & 0xff;
  ret = (ret << 8) + (tmp[0] & 0xff);

  return (ret);
}

static
uint32_t get_uint32 (const void *buf)
{
  uint32_t      ret;
  unsigned char *tmp;

  tmp = (unsigned char *) buf;

  ret = tmp[3] & 0xff;
  ret = (ret << 8) + (tmp[2] & 0xff);
  ret = (ret << 8) + (tmp[1] & 0xff);
  ret = (ret << 8) + (tmp[0] & 0xff);

  return (ret);
}

static
uint64_t get_uint64 (const void *buf)
{
  uint64_t      ret;
  unsigned char *tmp;

  tmp = (unsigned char *) buf;

  ret = tmp[7] & 0xff;
  ret = (ret << 8) + (tmp[6] & 0xff);
  ret = (ret << 8) + (tmp[5] & 0xff);
  ret = (ret << 8) + (tmp[4] & 0xff);
  ret = (ret << 8) + (tmp[3] & 0xff);
  ret = (ret << 8) + (tmp[2] & 0xff);
  ret = (ret << 8) + (tmp[1] & 0xff);
  ret = (ret << 8) + (tmp[0] & 0xff);

  return (ret);
}

void demux_ogg_init_sub (void) {
  int lcv;
  if(!ogg_sub.text[0]) // not yet allocated
  for (lcv = 0; lcv < SUB_MAX_TEXT; lcv++) {
    ogg_sub.text[lcv] = (char*)malloc(OGG_SUB_MAX_LINE);
  }
}

void demux_ogg_add_sub (ogg_stream_t* os,ogg_packet* pack) {
  int lcv;
  int line_pos = 0;
  int ignoring = 0;
  char *packet = pack->packet;

  mp_msg(MSGT_DEMUX,MSGL_DBG2,"\ndemux_ogg_add_sub %02X %02X %02X '%s'\n",
      (unsigned char)packet[0],
      (unsigned char)packet[1],
      (unsigned char)packet[2],
      &packet[3]);

  ogg_sub.lines = 0;
  if (((unsigned char)packet[0]) == 0x88) { // some subtitle text
    // Find data start
    int32_t duration = 0;
    int16_t hdrlen = (*packet & PACKET_LEN_BITS01)>>6, i;
    hdrlen |= (*packet & PACKET_LEN_BITS2) <<1;
    lcv = 1 + hdrlen;
    for (i = hdrlen; i > 0; i--) {
      duration <<= 8;
      duration |= (unsigned char)packet[i];
    }
    if ((hdrlen > 0) && (duration > 0)) {
      float pts;
      if(pack->granulepos == -1)
        pack->granulepos = os->lastpos + os->lastsize;
      pts = (float)pack->granulepos/(float)os->samplerate;
      clear_sub = 1.0 + pts + (float)duration/1000.0;
    }
    while (1) {
      int c = packet[lcv++];
      if(c=='\n' || c==0 || line_pos >= OGG_SUB_MAX_LINE-1){
	  ogg_sub.text[ogg_sub.lines][line_pos] = 0; // close sub
          if(line_pos) ogg_sub.lines++;
	  if(!c || ogg_sub.lines>=SUB_MAX_TEXT) break; // EOL or TooMany
          line_pos = 0;
      }
      switch (c) {
        case '\r':
        case '\n': // just ignore linefeeds for now
                   // their placement seems rather haphazard
          break;
        case '<': // some html markup, ignore for now
          ignoring = 1;
          break;
        case '>':
          ignoring = 0;
          break;
        default:
          if(!ignoring) 
	  ogg_sub.text[ogg_sub.lines][line_pos++] = c;
          break;
      }
    }
  }

  mp_msg(MSGT_DEMUX,MSGL_DBG2,"Ogg sub lines: %d  first: '%s'\n",
      ogg_sub.lines, ogg_sub.text[0]);
#ifdef USE_ICONV
  subcp_recode1(&ogg_sub);
#endif
  vo_sub = &ogg_sub;
  vo_osd_changed(OSDTYPE_SUBTITLE);
}


// get the logical stream of the current page
// fill os if non NULL and return the stream id
static  int demux_ogg_get_page_stream(ogg_demuxer_t* ogg_d,ogg_stream_state** os) {
  int id,s_no;
  ogg_page* page = &ogg_d->page;

  s_no = ogg_page_serialno(page);

  for(id= 0; id < ogg_d->num_sub ; id++) {
    if(s_no == ogg_d->subs[id].stream.serialno)
      break;
  }

  if(id == ogg_d->num_sub) {
    // If we have only one vorbis stream allow the stream id to change
    // it's normal on radio stream (each song have an different id).
    // But we (or the codec?) should check that the samplerate, etc
    // doesn't change (for radio stream it's ok)
    if(ogg_d->num_sub == 1 && ogg_d->subs[0].vorbis) {
      ogg_stream_reset(&ogg_d->subs[0].stream);
      ogg_stream_init(&ogg_d->subs[0].stream,s_no);
      id = 0;
    } else
      return -1;
  }
  
  if(os)
    *os = &ogg_d->subs[id].stream;

  return id;

}

static unsigned char* demux_ogg_read_packet(ogg_stream_t* os,ogg_packet* pack,void *context,float* pts,int* flags, int samplesize) {
  unsigned char* data = pack->packet;
  ogg_demuxer_t *ogg_d = os->ogg_d;

  *pts = 0;
  *flags = 0;

  if(os->vorbis) {
    if(*pack->packet & PACKET_TYPE_HEADER)
      os->hdr_packets++;
    else if (ogg_d->vi_inited)
    {
       vorbis_info *vi;
       int32_t blocksize;
       
       // When we dump the audio, there is no vi, but we don't care of timestamp in this case
       vi = &(ogg_d->vi);
       blocksize = vorbis_packet_blocksize(vi,pack) / samplesize;
       // Calculate the timestamp if the packet don't have any
       if(pack->granulepos == -1) {
	  pack->granulepos = os->lastpos;
	  if(os->lastsize > 0)
	     pack->granulepos += os->lastsize;
       }
       *pts = pack->granulepos / (float)vi->rate;
       os->lastsize = blocksize;
       os->lastpos = pack->granulepos;
    }
  } else if (os->speex) {
    // whole packet (default)
# ifdef HAVE_OGGTHEORA
  } else if (os->theora) {
     /* we pass complete packets to theora, mustn't strip the header! */
     os->lastsize = 1;
     
     /* header packets beginn on 1-bit: thus check (*data&0x80).  We don't
	have theora_state st, until all header packets were passed to the
	decoder. */
     if (context != NULL && !(*data&0x80))
     {
        theora_info *thi = ((theora_struct_t*)context)->st.i;
        int keyframe_granule_shift=_ilog(thi->keyframe_frequency_force-1);
        int64_t iframemask = (1 << keyframe_granule_shift) - 1;

	if (pack->granulepos >= 0)
	{
	   os->lastpos = pack->granulepos >> keyframe_granule_shift;
	   os->lastpos += pack->granulepos & iframemask;
	   *flags = ((pack->granulepos & iframemask) == 0);
	}
	else
	{
	   os->lastpos++;
	}
	pack->granulepos = os->lastpos;
	*pts = (double)os->lastpos / (double)os->samplerate;
     }
#endif /* HAVE_OGGTHEORA */
# ifdef HAVE_FLAC
  } else if (os->flac) {
     /* we pass complete packets to flac, mustn't strip the header! */
#endif /* HAVE_FLAC */
  } else {
    if(*pack->packet & PACKET_TYPE_HEADER)
      os->hdr_packets++;
    else {
    // Find data start
    int16_t hdrlen = (*pack->packet & PACKET_LEN_BITS01)>>6;
    hdrlen |= (*pack->packet & PACKET_LEN_BITS2) <<1;
    data = pack->packet + 1 + hdrlen;
    // Calculate the timestamp
    if(pack->granulepos == -1)
      pack->granulepos = os->lastpos + (os->lastsize ? os->lastsize : 1);
    // If we alredy have a timestamp it can be a syncpoint
    if(*pack->packet & PACKET_IS_SYNCPOINT)
      *flags = 1;
    *pts =  pack->granulepos/os->samplerate;
    // Save the packet length and timestamp
    os->lastsize = 0;
    while(hdrlen) {
      os->lastsize <<= 8;
      os->lastsize |= pack->packet[hdrlen];
      hdrlen--;
    }
    os->lastpos = pack->granulepos;
  }
  }
  return data;
}

// check if clang has substring from comma separated langlist
static int demux_ogg_check_lang(char *clang, char *langlist)
{
  char *c;

  if (!langlist || !*langlist)
    return 0;
  while ((c = strchr(langlist, ',')))
  {
    if (!strncasecmp(clang, langlist, c - langlist))
      return 1;
    langlist = &c[1];
  }
  if (!strncasecmp(clang, langlist, strlen(langlist)))
    return 1;
  return 0;
}

static int demux_ogg_sub_reverse_id(demuxer_t *demuxer, int id);

/// Try to print out comments and also check for LANGUAGE= tag
static void demux_ogg_check_comments(demuxer_t *d, ogg_stream_t *os, int id, vorbis_comment *vc)
{
  char *hdr, *val;
  char **cmt = vc->user_comments;
  int index, i;
  ogg_demuxer_t *ogg_d = (ogg_demuxer_t *)d->priv;
  struct table {
    char *ogg;
    char *mp;
  } table[] = {
    { "ENCODED_USING", "Software" },
    { "ENCODER_URL", "Encoder URL" },
    { "TITLE", "Name" },
    { "ARTIST", "Artist" },
    { "COMMENT", "Comments" },
    { "DATE", "Creation Date" },
    { "GENRE", "Genre" },
    { "ALBUM", "Album" },
    { "TRACKNUMBER", "Track" },
    { NULL, NULL },
  };

  while(*cmt)
  {
    hdr = NULL;
    if (!strncasecmp(*cmt, "LANGUAGE=", 9))
    {
      val = *cmt + 9;
      if (ogg_d->subs[id].text)
        mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SID_%d_LANG=%s\n", ogg_d->subs[id].id, val);
      else if (id != d->video->id)
        mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AID_%d_LANG=%s\n", ogg_d->subs[id].id, val);
      if (ogg_d->subs[id].text)
        mp_msg(MSGT_DEMUX, MSGL_INFO, "[Ogg] Language for -sid %d is '-slang \"%s\"'\n", ogg_d->subs[id].id, val);
      // copy this language name into the array
      index = demux_ogg_sub_reverse_id(d, id);
      if (index >= 0) {
	// in case of malicious files with more than one lang per track:
	if (ogg_d->text_langs[index]) free(ogg_d->text_langs[index]);
	ogg_d->text_langs[index] = strdup(val);
      }
      // check for -slang if subs are uninitialized yet
      if (os->text && d->sub->id == -1 && demux_ogg_check_lang(val, dvdsub_lang))
      {
	d->sub->id = id;
	dvdsub_id = index;
        mp_msg(MSGT_DEMUX, MSGL_V, "Ogg demuxer: Displaying subtitle stream id %d which matched -slang %s\n", id, val);
      }
      else
	hdr = "Language";
    }
    else {
	for (i = 0; table[i].ogg; i++)
	{
	    if (!strncasecmp(*cmt, table[i].ogg, strlen(table[i].ogg)))
	    {
		hdr = table[i].mp;
		val = *cmt + strlen(table[i].ogg) + 1;
	    }
	}
    }
    if (hdr)
      demux_info_add(d, hdr, val);
      mp_dbg(MSGT_DEMUX, MSGL_DBG2, " %s: %s\n", hdr, val);
    cmt++;
  }
}

/// Calculate the timestamp and add the packet to the demux stream
// return 1 if the packet was added, 0 otherwise
static int demux_ogg_add_packet(demux_stream_t* ds,ogg_stream_t* os,int id,ogg_packet* pack) {
  demuxer_t* d = ds->demuxer;
  demux_packet_t* dp;
  unsigned char* data;
  float pts = 0;
  int flags = 0;
  void *context = NULL;
  int samplesize = 1;

  // If packet is an comment header then we try to get comments at first
  if (pack->bytes >= 7 && !memcmp(pack->packet, "\003vorbis", 7))
  {
    vorbis_info vi;
    vorbis_comment vc;

    vorbis_info_init(&vi);
    vorbis_comment_init(&vc);
    vi.rate = 1L; // it's checked by vorbis_synthesis_headerin()
    if(vorbis_synthesis_headerin(&vi, &vc, pack) == 0) // if no errors
      demux_ogg_check_comments(d, os, id, &vc);
    vorbis_comment_clear(&vc);
    vorbis_info_clear(&vi);
  }
  if (os->text) {
    if (id == d->sub->id) // don't want to add subtitles to the demuxer for now
      demux_ogg_add_sub(os,pack);
    return 0;
  }
  if (os->speex) {
    // discard first two packets, they contain the header and comment
    if (os->hdr_packets < 2) {
      os->hdr_packets++;
      return 0;
    }
  } else
  // If packet is an header we jump it except for vorbis and theora
  // (PACKET_TYPE_HEADER bit doesn't even exist for theora ?!)
  // We jump nothing for FLAC. Ain't this great? Packet contents have to be
  // handled differently for each and every stream type. The joy! The joy!
  if(!os->flac && ((*pack->packet & PACKET_TYPE_HEADER) && 
     (ds != d->audio || ( ((sh_audio_t*)ds->sh)->format != FOURCC_VORBIS || os->hdr_packets >= NUM_VORBIS_HDR_PACKETS ) ) &&
     (ds != d->video || (((sh_video_t*)ds->sh)->format != FOURCC_THEORA))))
    return 0;

  // For vorbis packet the packet is the data, for other codec we must jump
  // the header
  if(ds == d->audio && ((sh_audio_t*)ds->sh)->format == FOURCC_VORBIS) {
     context = ((sh_audio_t *)ds->sh)->context;
     samplesize = ((sh_audio_t *)ds->sh)->samplesize;
  }
  if (ds == d->video && ((sh_audio_t*)ds->sh)->format == FOURCC_THEORA)
     context = ((sh_video_t *)ds->sh)->context;
  data = demux_ogg_read_packet(os,pack,context,&pts,&flags,samplesize);

  /// Clear subtitles if necessary (for broken files)
  if ((clear_sub > 0) && (pts >= clear_sub)) {
    ogg_sub.lines = 0;
    vo_sub = &ogg_sub;
    vo_osd_changed(OSDTYPE_SUBTITLE);
    clear_sub = -1;
  }
  /// Send the packet
  dp = new_demux_packet(pack->bytes-(data-pack->packet));
  memcpy(dp->buffer,data,pack->bytes-(data-pack->packet));
  dp->pts = pts;
  dp->flags = flags;
  ds_add_packet(ds,dp);
  mp_msg(MSGT_DEMUX,MSGL_DBG2,"New dp: %p  ds=%p  pts=%5.3f  len=%d  flag=%d  \n",
      dp, ds, pts, dp->len, flags);
  return 1;
}

/// if -forceidx build a table of all syncpoints to make seeking easier
/// otherwise try to get at least the final_granulepos
void demux_ogg_scan_stream(demuxer_t* demuxer) {
  ogg_demuxer_t* ogg_d = demuxer->priv;
  stream_t *s = demuxer->stream;
  ogg_sync_state* sync = &ogg_d->sync;
  ogg_page* page= &ogg_d->page;
  ogg_stream_state* oss;
  ogg_stream_t* os;
  ogg_packet op;
  int np,sid,p,samplesize=1;
  void *context = NULL;
  off_t pos, last_pos;
  pos = last_pos = demuxer->movi_start;

  // Reset the stream
  if(index_mode == 2) {
  stream_seek(s,demuxer->movi_start);
  } else {
    //the 270000 are just a wild guess
    stream_seek(s,max(ogg_d->pos,demuxer->movi_end-270000));
  }
  ogg_sync_reset(sync);

  // Get the serial number of the stream we use
  if(demuxer->video->id >= 0) {
    sid = demuxer->video->id;
    /* demux_ogg_read_packet needs decoder context for Theora streams */
    if (((sh_video_t*)demuxer->video->sh)->format == FOURCC_THEORA)
      context = ((sh_video_t*)demuxer->video->sh)->context;
  }
  else if(demuxer->audio->id >= 0) {
    sid = demuxer->audio->id;
    /* demux_ogg_read_packet needs decoder context for Vorbis streams */
    if(((sh_audio_t*)demuxer->audio->sh)->format == FOURCC_VORBIS) {
      context = ((sh_audio_t*)demuxer->audio->sh)->context;
      samplesize = ((sh_audio_t*)demuxer->audio->sh)->samplesize;
    }
  }
  else return;
  os = &ogg_d->subs[sid];
  oss = &os->stream;

  while(1) {
    np = ogg_sync_pageseek(sync,page);
    if(np < 0) { // We had to skip some bytes
      if(index_mode == 2) mp_msg(MSGT_DEMUX,MSGL_ERR,"Bad page sync while building syncpoints table (%d)\n",-np);
      pos += -np;
      continue;
    }
    if(np <= 0) { // We need more data
      char* buf = ogg_sync_buffer(sync,BLOCK_SIZE);
      int len = stream_read(s,buf,BLOCK_SIZE);
      if(len == 0 && s->eof)
	break;
      ogg_sync_wrote(sync,len);
      continue;
    }
    // The page is ready
    //ogg_sync_pageout(sync,page);
    if(ogg_page_serialno(page) != os->stream.serialno) { // It isn't a page from the stream we want
      pos += np;
      continue;
    }
    if(ogg_stream_pagein(oss,page) != 0) {
      mp_msg(MSGT_DEMUX,MSGL_ERR,"Pagein error ????\n");
      pos += np;
      continue;
    }
    p = 0;
    while(ogg_stream_packetout(oss,&op) == 1) {
      float pts;
      int flags;
      demux_ogg_read_packet(os,&op,context,&pts,&flags,samplesize);
      if(op.granulepos >= 0) ogg_d->final_granulepos = op.granulepos;
      if(index_mode == 2 && (flags || (os->vorbis && op.granulepos >= 0))) {
	if (ogg_d->num_syncpoint > SIZE_MAX / sizeof(ogg_syncpoint_t) - 1) break;
	ogg_d->syncpoints = realloc_struct(ogg_d->syncpoints,(ogg_d->num_syncpoint+1), sizeof(ogg_syncpoint_t));
	ogg_d->syncpoints[ogg_d->num_syncpoint].granulepos = op.granulepos;
	ogg_d->syncpoints[ogg_d->num_syncpoint].page_pos = (ogg_page_continued(page) && p == 0) ? last_pos : pos;
	ogg_d->num_syncpoint++;
      }
      p++;
    }
    if(p > 1 || (p == 1 && ! ogg_page_continued(page)))
      last_pos = pos;
    pos += np;
    if(index_mode == 2) mp_msg(MSGT_DEMUX,MSGL_INFO,"Building syncpoint table %d%%\r",(int)(pos*100/s->end_pos));
  }
  if(index_mode == 2) mp_msg(MSGT_DEMUX,MSGL_INFO,"\n");

  if(index_mode == 2) mp_msg(MSGT_DEMUX,MSGL_V,"Ogg syncpoints table builed: %d syncpoints\n",ogg_d->num_syncpoint);
  mp_msg(MSGT_DEMUX,MSGL_V,"Ogg stream length (granulepos): %"PRId64"\n",ogg_d->final_granulepos);

  stream_reset(s);
  stream_seek(s,demuxer->movi_start);
  ogg_sync_reset(sync);
  for(np = 0 ; np < ogg_d->num_sub ; np++) {
    ogg_stream_reset(&ogg_d->subs[np].stream);
    ogg_d->subs[np].lastpos = ogg_d->subs[np].lastsize = ogg_d->subs[np].hdr_packets = 0;
  }

  
  // Get the first page
  while(1) {
    np = ogg_sync_pageout(sync,page);
    if(np <= 0) { // We need more data
      char* buf = ogg_sync_buffer(sync,BLOCK_SIZE);
      int len = stream_read(s,buf,BLOCK_SIZE);
      if(len == 0 && s->eof) {
	mp_msg(MSGT_DEMUX,MSGL_ERR,"EOF while trying to get the first page !!!!\n");
	break;
      }
      
      ogg_sync_wrote(sync,len);
      continue;
    }
    demux_ogg_get_page_stream(ogg_d,&oss);
    ogg_stream_pagein(oss,page);
    break;
  }
  
}

extern void print_wave_header(WAVEFORMATEX *h, int verbose_level);
extern void print_video_header(BITMAPINFOHEADER *h, int verbose_level);

/** \brief Return the number of subtitle tracks in the file.

  \param demuxer The demuxer for which the number of subtitle tracks
  should be returned.
*/
int demux_ogg_num_subs(demuxer_t *demuxer) {
  ogg_demuxer_t *ogg_d = (ogg_demuxer_t *)demuxer->priv;
  return ogg_d->n_text;
}

/** \brief Change the current subtitle stream and return its ID.

  \param demuxer The demuxer whose subtitle stream will be changed.
  \param new_num The number of the new subtitle track. The number must be
  between 0 and ogg_d->n_text - 1.

  \returns The Ogg stream number ( = page serial number) of the newly selected
  track.
*/
int demux_ogg_sub_id(demuxer_t *demuxer, int index) {
  ogg_demuxer_t *ogg_d = (ogg_demuxer_t *)demuxer->priv;
  return (index < 0) ? index : (index >= ogg_d->n_text) ? -1 : ogg_d->text_ids[index];
}

/** \brief Translate the ogg track number into the subtitle number.
 *  \param demuxer The demuxer about whose subtitles we are inquiring.
 *  \param id The ogg track number of the subtitle track.
 */
static int demux_ogg_sub_reverse_id(demuxer_t *demuxer, int id) {
  ogg_demuxer_t *ogg_d = (ogg_demuxer_t *)demuxer->priv;
  int i;
  for (i = 0; i < ogg_d->n_text; i++)
    if (ogg_d->text_ids[i] == id) return i;
  return -1;
}

/** \brief Lookup the subtitle language by the subtitle number.  Returns NULL on out-of-bounds input.
 *  \param demuxer The demuxer about whose subtitles we are inquiring.
 *  \param index The subtitle number.
 */
char *demux_ogg_sub_lang(demuxer_t *demuxer, int index) {
  ogg_demuxer_t *ogg_d = (ogg_demuxer_t *)demuxer->priv;
  return (index < 0) ? NULL : (index >= ogg_d->n_text) ? NULL : ogg_d->text_langs[index];
}

static void demux_close_ogg(demuxer_t* demuxer);

static void fixup_vorbis_wf(sh_audio_t *sh, ogg_demuxer_t *od)
{
  int i, offset;
  int ris, init_error = 0;
  ogg_packet op[3];
  unsigned char *buf[3];
  unsigned char *ptr;
  unsigned int len;

  vorbis_info_init(&od->vi);
  vorbis_comment_init(&od->vc);
  for(i = 0; i < 3; i++) {
    op[i].bytes = ds_get_packet(sh->ds, &(op[i].packet));
    mp_msg(MSGT_DEMUX,MSGL_V, "fixup_vorbis_wf: i=%d, size=%ld\n", i, op[i].bytes);
    if(op[i].bytes < 0) {
       mp_msg(MSGT_DEMUX,MSGL_ERR,"Ogg demuxer error!, fixup_vorbis_wf: bad packet n. %d\n", i);
       return;
    }
    buf[i] = malloc(op[i].bytes);
    if(!buf[i])
      return;
    memcpy(buf[i], op[i].packet, op[i].bytes);

    op[i].b_o_s = (i==0);
    ris = vorbis_synthesis_headerin(&(od->vi),&(od->vc),&(op[i]));
    if(ris < 0) {
      init_error = 1;
      mp_msg(MSGT_DECAUDIO,MSGL_ERR,"DEMUX_OGG: header n. %d broken! len=%ld, code: %d\n", i, op[i].bytes, ris);
    }
  }
  if(!init_error)
    od->vi_inited = 1;

  len = op[0].bytes + op[1].bytes + op[2].bytes;
  sh->wf = (WAVEFORMATEX*)calloc(1, sizeof(WAVEFORMATEX) + len + len/255 + 64);
  ptr = (unsigned char*) (sh->wf+1);

  ptr[0] = 2;
  offset = 1;
  offset += store_ughvlc(&ptr[offset], op[0].bytes);
  mp_msg(MSGT_DEMUX,MSGL_V,"demux_ogg, offset after 1st len = %u\n", offset);
  offset += store_ughvlc(&ptr[offset], op[1].bytes);
  mp_msg(MSGT_DEMUX,MSGL_V,"demux_ogg, offset after 2nd len = %u\n", offset);
  for(i = 0; i < 3; i++) {
    mp_msg(MSGT_DEMUX,MSGL_V,"demux_ogg, i=%d, bytes: %ld, offset: %u\n", i, op[i].bytes, offset);
    memcpy(&ptr[offset], buf[i], op[i].bytes);
    offset += op[i].bytes;
  }
  sh->wf->cbSize = offset;
  mp_msg(MSGT_DEMUX,MSGL_V, "demux_ogg, extradata size: %d\n", sh->wf->cbSize);
  sh->wf = (WAVEFORMATEX*)realloc(sh->wf, sizeof(WAVEFORMATEX) + sh->wf->cbSize);

  if(op[0].bytes >= 29) {
    unsigned int br, nombr, minbr, maxbr;
    ptr = buf[0];
    sh->channels = ptr[11];
    sh->samplerate = sh->wf->nSamplesPerSec = get_uint32(&ptr[12]);
    maxbr = get_uint32(&ptr[16]);  //max
    nombr = get_uint32(&ptr[20]);  //nominal
    minbr = get_uint32(&ptr[24]);  //minimum
    br = maxbr / 8;
    if(!br)
      br = nombr / 8;
    if(!br)
      br = minbr / 8;
    sh->wf->nAvgBytesPerSec = br;
    sh->wf->wBitsPerSample = 16;
    sh->samplesize = (sh->wf->wBitsPerSample+7)/8;

    mp_msg(MSGT_DEMUX,MSGL_V,"demux_ogg, vorbis stream features are: channels: %d, srate: %d, bitrate: %d, max: %u, nominal: %u, min: %u\n", 
      sh->channels, sh->samplerate, sh->wf->nAvgBytesPerSec, maxbr, nombr, minbr);
  }
  free(buf[2]);
  free(buf[1]);
  free(buf[0]);
}


/// Open an ogg physical stream
// Not static because it's used also in demuxer_avi.c
int demux_ogg_open(demuxer_t* demuxer) {
  ogg_demuxer_t* ogg_d;
  stream_t *s;
  char* buf;
  int np,s_no, n_audio = 0, n_video = 0;
  int audio_id = -1, video_id = -1, text_id = -1;
  ogg_sync_state* sync;
  ogg_page* page;
  ogg_packet pack;
  sh_audio_t* sh_a;
  sh_video_t* sh_v;

#ifdef USE_ICONV
  subcp_open(NULL);
#endif

  clear_sub = -1;
  s = demuxer->stream;

  demuxer->priv =
  ogg_d = (ogg_demuxer_t*)calloc(1,sizeof(ogg_demuxer_t));
  sync = &ogg_d->sync;
  page = &ogg_d->page;

  ogg_sync_init(sync);

  while(1) {
    /// Try to get a page
    ogg_d->pos += ogg_d->last_size;
    np = ogg_sync_pageseek(sync,page);
    /// Error
    if(np < 0) {
      mp_msg(MSGT_DEMUX,MSGL_DBG2,"Ogg demuxer : Bad page sync\n");
      goto err_out;
    }
    /// Need some more data
    if(np == 0) {
      int len;
      buf = ogg_sync_buffer(sync,BLOCK_SIZE);
      len = stream_read(s,buf,BLOCK_SIZE);      
      if(len == 0 && s->eof) {
	goto err_out;
      }
      ogg_sync_wrote(sync,len);
      continue;
    }
    ogg_d->last_size = np;
    // We got one page now

    if( ! ogg_page_bos(page) ) { // It's not a begining page
      // Header parsing end here, we need to get the page otherwise it will be lost
      int id = demux_ogg_get_page_stream(ogg_d,NULL);
      if(id >= 0)
	ogg_stream_pagein(&ogg_d->subs[id].stream,page);
      else
	mp_msg(MSGT_DEMUX,MSGL_ERR,"Ogg : Warning found none bos page from unknown stream %d\n",ogg_page_serialno(page));
      break;
    }

    /// Init  the data structure needed for a logical stream
    ogg_d->subs = (ogg_stream_t*)realloc(ogg_d->subs,(ogg_d->num_sub+1)*sizeof(ogg_stream_t));
    memset(&ogg_d->subs[ogg_d->num_sub],0,sizeof(ogg_stream_t));
    /// Get the stream serial number
    s_no = ogg_page_serialno(page);
    ogg_stream_init(&ogg_d->subs[ogg_d->num_sub].stream,s_no);
    mp_msg(MSGT_DEMUX,MSGL_DBG2,"Ogg : Found a stream with serial=%d\n",s_no);
    // Take the first page
    ogg_stream_pagein(&ogg_d->subs[ogg_d->num_sub].stream,page);
    // Get first packet of the page
    ogg_stream_packetout(&ogg_d->subs[ogg_d->num_sub].stream,&pack);

    // Reset our vars
    sh_a = NULL;
    sh_v = NULL;

    demux_aid_vid_mismatch = 1; // don't identify in new_sh_* since ids don't match
    ogg_d->subs[ogg_d->num_sub].ogg_d = ogg_d;

    // Check for Vorbis
    if(pack.bytes >= 7 && ! strncmp(&pack.packet[1],"vorbis", 6) ) {
      sh_a = new_sh_audio(demuxer,ogg_d->num_sub);
      sh_a->format = FOURCC_VORBIS;
      ogg_d->subs[ogg_d->num_sub].vorbis = 1;
      mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AUDIO_ID=%d\n", n_audio);
      ogg_d->subs[ogg_d->num_sub].id = n_audio;
      n_audio++;
      mp_msg(MSGT_DEMUX,MSGL_INFO,"[Ogg] stream %d: audio (Vorbis), -aid %d\n",ogg_d->num_sub,n_audio-1);
    } else if (pack.bytes >= 80 && !strncmp(pack.packet,"Speex", 5)) {
      sh_a = new_sh_audio(demuxer, ogg_d->num_sub);
      sh_a->wf = (WAVEFORMATEX*)calloc(1, sizeof(WAVEFORMATEX) + pack.bytes);
      sh_a->format = FOURCC_SPEEX;
      sh_a->samplerate = sh_a->wf->nSamplesPerSec = get_uint32(&pack.packet[36]);
      sh_a->channels = sh_a->wf->nChannels = get_uint32(&pack.packet[48]);
      sh_a->wf->wFormatTag = sh_a->format;
      sh_a->wf->nAvgBytesPerSec = get_uint32(&pack.packet[52]);
      sh_a->wf->nBlockAlign = 0;
      sh_a->wf->wBitsPerSample = 16;
      sh_a->samplesize = 2;
      sh_a->wf->cbSize = pack.bytes;
      memcpy(&sh_a->wf[1], pack.packet, pack.bytes);

      ogg_d->subs[ogg_d->num_sub].samplerate = sh_a->samplerate;
      ogg_d->subs[ogg_d->num_sub].speex = 1;
      mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AUDIO_ID=%d\n", n_audio);
      ogg_d->subs[ogg_d->num_sub].id = n_audio;
      n_audio++;
      mp_msg(MSGT_DEMUX,MSGL_INFO,"[Ogg] stream %d: audio (Speex), -aid %d\n",ogg_d->num_sub,n_audio-1);

      // check for Theora
#   ifdef HAVE_OGGTHEORA
    } else if (pack.bytes >= 7 && !strncmp (&pack.packet[1], "theora", 6)) {
	int errorCode = 0;
	theora_info inf;
	theora_comment cc;
	
	theora_info_init (&inf);
	theora_comment_init (&cc);
	
	errorCode = theora_decode_header (&inf, &cc, &pack);
	if (errorCode)
	    mp_msg(MSGT_DEMUX,MSGL_ERR,"Theora header parsing failed: %i \n",
		   errorCode);
	else
	{
	    sh_v = new_sh_video(demuxer,ogg_d->num_sub);

	    sh_v->context = NULL;
	    sh_v->bih = (BITMAPINFOHEADER*)calloc(1,sizeof(BITMAPINFOHEADER));
	    sh_v->bih->biSize=sizeof(BITMAPINFOHEADER);
	    sh_v->bih->biCompression= sh_v->format = FOURCC_THEORA;
	    sh_v->fps = ((double)inf.fps_numerator)/
		(double)inf.fps_denominator;
	    sh_v->frametime = ((double)inf.fps_denominator)/
		(double)inf.fps_numerator;
	    sh_v->disp_w = sh_v->bih->biWidth = inf.frame_width;
	    sh_v->disp_h = sh_v->bih->biHeight = inf.frame_height;
	    sh_v->bih->biBitCount = 24;
	    sh_v->bih->biPlanes = 3;
	    sh_v->bih->biSizeImage = ((sh_v->bih->biBitCount/8) * 
				      sh_v->bih->biWidth*sh_v->bih->biHeight);
	    ogg_d->subs[ogg_d->num_sub].samplerate = sh_v->fps;
	    ogg_d->subs[ogg_d->num_sub].theora = 1;
	    mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_ID=%d\n", n_video);
	    ogg_d->subs[ogg_d->num_sub].id = n_video;
	    n_video++;
	    mp_msg(MSGT_DEMUX,MSGL_INFO,
		   "[Ogg] stream %d: video (Theora v%d.%d.%d), -vid %d\n",
		   ogg_d->num_sub,
		   (int)inf.version_major, 
		   (int)inf.version_minor, 
		   (int)inf.version_subminor, 
		   n_video - 1);
	    if( mp_msg_test(MSGT_HEADER,MSGL_V) ) print_video_header(sh_v->bih,MSGL_V);
	}
#   endif /* HAVE_OGGTHEORA */
#   ifdef HAVE_FLAC
    } else if (pack.bytes >= 4 && !strncmp (&pack.packet[0], "fLaC", 4)) {
	sh_a = new_sh_audio(demuxer,ogg_d->num_sub);
	sh_a->format =  mmioFOURCC('f', 'L', 'a', 'C');
	mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AUDIO_ID=%d\n", n_audio);
	ogg_d->subs[ogg_d->num_sub].id = n_audio;
	n_audio++;
	ogg_d->subs[ogg_d->num_sub].flac = 1;
	sh_a->wf = NULL;
	mp_msg(MSGT_DEMUX,MSGL_INFO,"[Ogg] stream %d: audio (FLAC), -aid %d\n",ogg_d->num_sub,n_audio-1);
#   endif /* HAVE_FLAC */

      /// Check for old header
    } else if(pack.bytes >= 142 && ! strncmp(&pack.packet[1],"Direct Show Samples embedded in Ogg",35) ) {

       // Old video header
      if(get_uint32 (pack.packet+96) == 0x05589f80 && pack.bytes >= 184) {
	sh_v = new_sh_video(demuxer,ogg_d->num_sub);
	sh_v->bih = (BITMAPINFOHEADER*)calloc(1,sizeof(BITMAPINFOHEADER));
	sh_v->bih->biSize=sizeof(BITMAPINFOHEADER);
	sh_v->bih->biCompression=
	sh_v->format = mmioFOURCC(pack.packet[68],pack.packet[69],
				pack.packet[70],pack.packet[71]);
	sh_v->frametime = get_uint64(pack.packet+164)*0.0000001;
	sh_v->fps = 1/sh_v->frametime;
	sh_v->disp_w = sh_v->bih->biWidth = get_uint32(pack.packet+176);
	sh_v->disp_h = sh_v->bih->biHeight = get_uint32(pack.packet+180);
	sh_v->bih->biBitCount = get_uint16(pack.packet+182);
	if(!sh_v->bih->biBitCount) sh_v->bih->biBitCount=24; // hack, FIXME
	sh_v->bih->biPlanes=1;
	sh_v->bih->biSizeImage=(sh_v->bih->biBitCount>>3)*sh_v->bih->biWidth*sh_v->bih->biHeight;

	ogg_d->subs[ogg_d->num_sub].samplerate = sh_v->fps;
	mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_ID=%d\n", n_video);
	ogg_d->subs[ogg_d->num_sub].id = n_video;
	n_video++;
	mp_msg(MSGT_DEMUX,MSGL_INFO,"[Ogg] stream %d: video (FOURCC %c%c%c%c), -vid %d\n",
	       ogg_d->num_sub,pack.packet[68],pack.packet[69],pack.packet[70],pack.packet[71],n_video-1);
	if( mp_msg_test(MSGT_HEADER,MSGL_V) ) print_video_header(sh_v->bih,MSGL_V);
	// Old audio header
      } else if(get_uint32(pack.packet+96) == 0x05589F81) {
	unsigned int extra_size;
	sh_a = new_sh_audio(demuxer,ogg_d->num_sub);
	extra_size = get_uint16(pack.packet+140);
	sh_a->wf = (WAVEFORMATEX*)calloc(1,sizeof(WAVEFORMATEX)+extra_size);
	sh_a->format = sh_a->wf->wFormatTag = get_uint16(pack.packet+124);
	sh_a->channels = sh_a->wf->nChannels = get_uint16(pack.packet+126);
	sh_a->samplerate = sh_a->wf->nSamplesPerSec = get_uint32(pack.packet+128);
	sh_a->wf->nAvgBytesPerSec = get_uint32(pack.packet+132);
	sh_a->wf->nBlockAlign = get_uint16(pack.packet+136);
	sh_a->wf->wBitsPerSample = get_uint16(pack.packet+138);
	sh_a->samplesize = (sh_a->wf->wBitsPerSample+7)/8;
	sh_a->wf->cbSize = extra_size;
	if(extra_size > 0)
	  memcpy(((char *)sh_a->wf)+sizeof(WAVEFORMATEX),pack.packet+142,extra_size);

	ogg_d->subs[ogg_d->num_sub].samplerate = sh_a->samplerate; // * sh_a->channels;
	mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AUDIO_ID=%d\n", n_audio);
	ogg_d->subs[ogg_d->num_sub].id = n_audio;
	n_audio++;
	mp_msg(MSGT_DEMUX,MSGL_INFO,"[Ogg] stream %d: audio (format 0x%04x), -aid %d\n",ogg_d->num_sub,sh_a->format,n_audio-1);
	if( mp_msg_test(MSGT_HEADER,MSGL_V) ) print_wave_header(sh_a->wf,MSGL_V);
      } else
	mp_msg(MSGT_DEMUX,MSGL_WARN,"Ogg stream %d contains an old header but the header type is unknown\n",ogg_d->num_sub);

        // Check new header
    } else if ( (*pack.packet & PACKET_TYPE_BITS ) == PACKET_TYPE_HEADER && 
	      pack.bytes >= (int)sizeof(stream_header)+1) {
      stream_header *st = (stream_header*)(pack.packet+1);
      /// New video header
      if(strncmp(st->streamtype,"video",5) == 0) {
	sh_v = new_sh_video(demuxer,ogg_d->num_sub);
	sh_v->bih = (BITMAPINFOHEADER*)calloc(1,sizeof(BITMAPINFOHEADER));
	sh_v->bih->biSize=sizeof(BITMAPINFOHEADER);
	sh_v->bih->biCompression=
	sh_v->format = mmioFOURCC(st->subtype[0],st->subtype[1],
				  st->subtype[2],st->subtype[3]);
	sh_v->frametime = get_uint64(&st->time_unit)*0.0000001;
	sh_v->fps = 1.0/sh_v->frametime;
	sh_v->bih->biBitCount = get_uint16(&st->bits_per_sample);
	sh_v->disp_w = sh_v->bih->biWidth = get_uint32(&st->sh.video.width);
	sh_v->disp_h = sh_v->bih->biHeight = get_uint32(&st->sh.video.height);
	if(!sh_v->bih->biBitCount) sh_v->bih->biBitCount=24; // hack, FIXME
	sh_v->bih->biPlanes=1;
	sh_v->bih->biSizeImage=(sh_v->bih->biBitCount>>3)*sh_v->bih->biWidth*sh_v->bih->biHeight;

	ogg_d->subs[ogg_d->num_sub].samplerate= sh_v->fps;
	mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_ID=%d\n", n_video);
	ogg_d->subs[ogg_d->num_sub].id = n_video;
	n_video++;
	mp_msg(MSGT_DEMUX,MSGL_INFO,"[Ogg] stream %d: video (FOURCC %c%c%c%c), -vid %d\n",
	       ogg_d->num_sub,st->subtype[0],st->subtype[1],st->subtype[2],st->subtype[3],n_video-1);
	if( mp_msg_test(MSGT_HEADER,MSGL_V) ) print_video_header(sh_v->bih,MSGL_V);
	/// New audio header
      } else if(strncmp(st->streamtype,"audio",5) == 0) {
	char buffer[5];
	unsigned int extra_size = get_uint32 (&st->size) - sizeof(stream_header);
	unsigned int extra_offset = 0;

	memcpy(buffer,st->subtype,4);
	buffer[4] = '\0';

        /* Nasty workaround. stream_header.size seems not to contain the real
           size in all cases. There are four extra bytes that are unaccounted
           for in front of the real codec initialization data _at least_ for
           AAC. So far I've only seen those bytes being all 0, so we can
           just skip them here. */
	if ((strtol(buffer, NULL, 16) == 0xff) && (extra_size >= 4)) {
	  extra_size -= 4;
	  extra_offset = 4;
	}

	sh_a = new_sh_audio(demuxer,ogg_d->num_sub);
	sh_a->wf = (WAVEFORMATEX*)calloc(1,sizeof(WAVEFORMATEX)+extra_size);
	sh_a->format =  sh_a->wf->wFormatTag = strtol(buffer, NULL, 16);
	sh_a->channels = sh_a->wf->nChannels = get_uint16(&st->sh.audio.channels);
	sh_a->samplerate = sh_a->wf->nSamplesPerSec = get_uint64(&st->samples_per_unit);
	sh_a->wf->nAvgBytesPerSec = get_uint32(&st->sh.audio.avgbytespersec);
	sh_a->wf->nBlockAlign = get_uint16(&st->sh.audio.blockalign);
	sh_a->wf->wBitsPerSample = get_uint16(&st->bits_per_sample);
	sh_a->samplesize = (sh_a->wf->wBitsPerSample+7)/8;
	sh_a->wf->cbSize = extra_size;
	if(extra_size)
	  memcpy(((char *)sh_a->wf)+sizeof(WAVEFORMATEX),((char *)(st+1))+extra_offset,extra_size);

	ogg_d->subs[ogg_d->num_sub].samplerate = sh_a->samplerate; // * sh_a->channels;
	mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AUDIO_ID=%d\n", n_audio);
	ogg_d->subs[ogg_d->num_sub].id = n_audio;
	n_audio++;
	mp_msg(MSGT_DEMUX,MSGL_INFO,"[Ogg] stream %d: audio (format 0x%04x), -aid %d\n",ogg_d->num_sub,sh_a->format,n_audio-1);
	if( mp_msg_test(MSGT_HEADER,MSGL_V) ) print_wave_header(sh_a->wf,MSGL_V);

	/// Check for text (subtitles) header
      } else if (strncmp(st->streamtype, "text", 4) == 0) {
          mp_msg(MSGT_DEMUX, MSGL_INFO, "[Ogg] stream %d: subtitles (SRT-like text subtitles), -sid %d\n", ogg_d->num_sub, ogg_d->n_text);
	  ogg_d->subs[ogg_d->num_sub].samplerate= get_uint64(&st->time_unit)/10;
	  ogg_d->subs[ogg_d->num_sub].text = 1;
	  mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SUBTITLE_ID=%d\n", ogg_d->n_text);
          ogg_d->subs[ogg_d->num_sub].id = ogg_d->n_text;
          if (demuxer->sub->id == ogg_d->n_text)
            text_id = ogg_d->num_sub;
          ogg_d->n_text++;
          ogg_d->text_ids = (int *)realloc(ogg_d->text_ids, sizeof(int) * ogg_d->n_text);
          ogg_d->text_ids[ogg_d->n_text - 1] = ogg_d->num_sub;
          ogg_d->text_langs = (char **)realloc(ogg_d->text_langs, sizeof(char *) * ogg_d->n_text);
          ogg_d->text_langs[ogg_d->n_text - 1] = NULL;
          demux_ogg_init_sub();
	//// Unknown header type
      } else
	mp_msg(MSGT_DEMUX,MSGL_ERR,"Ogg stream %d has a header marker but is of an unknown type\n",ogg_d->num_sub);
      /// Unknown (invalid ?) header
    } else
      mp_msg(MSGT_DEMUX,MSGL_ERR,"Ogg stream %d is of an unknown type\n",ogg_d->num_sub);

    if(sh_a || sh_v) {
      demux_stream_t* ds = NULL;
      if(sh_a) {
	// If the audio stream is not defined we took the first one
	if(demuxer->audio->id == -1) {
	  demuxer->audio->id = n_audio - 1;
//	  if(sh_a->wf) print_wave_header(sh_a->wf,MSGL_INFO);
	}
	/// Is it the stream we want
	if(demuxer->audio->id == (n_audio - 1)) {
	  demuxer->audio->sh = sh_a;
	  sh_a->ds = demuxer->audio;
	  ds = demuxer->audio;
          audio_id = ogg_d->num_sub;
	}
      }
      if(sh_v) {
	/// Also for video
	if(demuxer->video->id == -1) {
	  demuxer->video->id = n_video - 1;
//	  if(sh_v->bih) print_video_header(sh_v->bih,MSGL_INFO);
	}
	if(demuxer->video->id == (n_video - 1)) {
	  demuxer->video->sh = sh_v;
	  sh_v->ds = demuxer->video;
	  ds = demuxer->video;
          video_id = ogg_d->num_sub;
	}
      }
      /// Add the header packets if the stream isn't seekable
      if(ds && !s->end_pos) {
	/// Finish the page, otherwise packets will be lost
	do {
	  demux_ogg_add_packet(ds,&ogg_d->subs[ogg_d->num_sub],ogg_d->num_sub,&pack);
	} while(ogg_stream_packetout(&ogg_d->subs[ogg_d->num_sub].stream,&pack) == 1);
      }
    }
    ogg_d->num_sub++;      
  }

  if(!n_video && !n_audio) {
    goto err_out;
  }

  if(!n_video || (video_id < 0))
    demuxer->video->id = -2;
  else
    demuxer->video->id = video_id;
  if(!n_audio || (audio_id < 0))
    demuxer->audio->id = -2;
  else
    demuxer->audio->id = audio_id;
  /* Disable the subs only if there are no text streams at all.
     Otherwise the stream to display might be chosen later when the comment
     packet is encountered and the user used -slang instead of -sid. */
  if(!ogg_d->n_text)
    demuxer->sub->id = -2;
  else if (text_id >= 0) {
    demuxer->sub->id = text_id;
    mp_msg(MSGT_DEMUX, MSGL_V, "Ogg demuxer: Displaying subtitle stream id %d\n", text_id);
  }

  ogg_d->final_granulepos=0;
  if(!s->end_pos)
    demuxer->seekable = 0;
  else {
    demuxer->movi_start = s->start_pos; // Needed for XCD (Ogg written in MODE2)
    demuxer->movi_end = s->end_pos;
    demuxer->seekable = 1;
    demux_ogg_scan_stream(demuxer);
  }

  mp_msg(MSGT_DEMUX,MSGL_V,"Ogg demuxer : found %d audio stream%s, %d video stream%s and %d text stream%s\n",n_audio,n_audio>1?"s":"",n_video,n_video>1?"s":"",ogg_d->n_text,ogg_d->n_text>1?"s":"");

  sh_a = demuxer->audio->sh;
  if(sh_a)
    if(sh_a->format == FOURCC_VORBIS)
      fixup_vorbis_wf(sh_a, ogg_d);

  return DEMUXER_TYPE_OGG;

err_out:
  return 0;
}


static int demux_ogg_fill_buffer(demuxer_t *d, demux_stream_t *dsds) {
  ogg_demuxer_t* ogg_d;
  stream_t *s;
  demux_stream_t *ds;
  ogg_sync_state* sync;
  ogg_stream_state* os;
  ogg_page* page;
  ogg_packet pack;
  int np = 0, id=0;

  s = d->stream;
  ogg_d = d->priv;
  sync = &ogg_d->sync;
  page = &ogg_d->page;

  /// Find the stream we are working on
  if ( (id = demux_ogg_get_page_stream(ogg_d,&os)) < 0) {
      mp_msg(MSGT_DEMUX,MSGL_ERR,"Ogg demuxer : can't get current stream\n");
      return 0;
  }

  while(1) {
    np = 0;
    ds = NULL;
    /// Try to get some packet from the current page
    while( (np = ogg_stream_packetout(os,&pack)) != 1) {
      /// No packet we go the next page
      if(np == 0) {
	while(1) {
	  int pa,len;
	  char *buf;
	  ogg_d->pos += ogg_d->last_size;
	  /// Get the next page from the physical stream
	  while( (pa = ogg_sync_pageseek(sync,page)) <= 0) {
	    /// Error : we skip some bytes
	    if(pa < 0) {
	      mp_msg(MSGT_DEMUX,MSGL_WARN,"Ogg : Page out not synced, we skip some bytes\n");
	      ogg_d->pos -= pa;
	      continue;
	    }
	    /// We need more data
	    buf = ogg_sync_buffer(sync,BLOCK_SIZE);	    
	    len = stream_read(s,buf,BLOCK_SIZE);
	    if(len == 0 && s->eof) {
	      mp_msg(MSGT_DEMUX,MSGL_DBG2,"Ogg : Stream EOF !!!!\n");
	      return 0;
	    }
	    ogg_sync_wrote(sync,len);
	  } /// Page loop
	  ogg_d->last_size = pa;
	  /// Find the page's logical stream
	  if( (id = demux_ogg_get_page_stream(ogg_d,&os)) < 0) {
	    mp_msg(MSGT_DEMUX,MSGL_ERR,"Ogg demuxer error : we met an unknown stream\n");
	    return 0;
	  }
	  /// Take the page
	  if(ogg_stream_pagein(os,page) == 0)
	    break;
	  /// Page was invalid => retry
	  mp_msg(MSGT_DEMUX,MSGL_WARN,"Ogg demuxer : got invalid page !!!!!\n");
	  ogg_d->pos += ogg_d->last_size;
	}
      } else /// Packet was corrupted
	mp_msg(MSGT_DEMUX,MSGL_WARN,"Ogg : bad packet in stream %d\n",id);
    } /// Packet loop
    
    /// Is the actual logical stream in use ?
    if(id == d->audio->id)
      ds = d->audio;
    else if(id == d->video->id)
      ds = d->video;
    else if (ogg_d->subs[id].text)
      ds = d->sub;

    if(ds) {
      if(!demux_ogg_add_packet(ds,&ogg_d->subs[id],id,&pack))
	continue; /// Unuseful packet, get another
      d->filepos = ogg_d->pos;
      return 1;
    }

  } /// while(1)

}

/// For avi with Ogg audio stream we have to create an ogg demuxer for this
// stream, then we join the avi and ogg demuxer with a demuxers demuxer
demuxer_t* init_avi_with_ogg(demuxer_t* demuxer) {
  demuxer_t  *od;
  ogg_demuxer_t *ogg_d;
  stream_t* s;
  uint32_t hdrsizes[3];
  demux_packet_t *dp;
  sh_audio_t *sh_audio = demuxer->audio->sh;
  int np;
  unsigned char *p = NULL,*buf;
  int plen;

  /// Check that the cbSize is enouth big for the following reads
  if(sh_audio->wf->cbSize < 22+3*sizeof(uint32_t)) {
    mp_msg(MSGT_DEMUX,MSGL_ERR,"AVI Ogg : Initial audio header is too small !!!!!\n");
    goto fallback;
  }
  /// Get the size of the 3 header packet
  memcpy(hdrsizes, ((unsigned char*)sh_audio->wf)+22+sizeof(WAVEFORMATEX), 3*sizeof(uint32_t));
//  printf("\n!!!!!! hdr sizes: %d %d %d   \n",hdrsizes[0],hdrsizes[1],hdrsizes[2]);

  /// Check the size
  if(sh_audio->wf->cbSize < 22+3*sizeof(uint32_t)+hdrsizes[0]+hdrsizes[1] + hdrsizes[2]) {
    mp_msg(MSGT_DEMUX,MSGL_ERR,"AVI Ogg : Audio header is too small !!!!!\n");
    goto fallback;
  }

  // Build the ogg demuxer private datas
  ogg_d = (ogg_demuxer_t*)calloc(1,sizeof(ogg_demuxer_t));
  ogg_d->num_sub = 1;
  ogg_d->subs = (ogg_stream_t*)malloc(sizeof(ogg_stream_t));
  ogg_d->subs[0].vorbis = 1;

   // Init the ogg physical stream
  ogg_sync_init(&ogg_d->sync);

  // Get the first page of the stream : we assume there only 1 logical stream
  while((np = ogg_sync_pageout(&ogg_d->sync,&ogg_d->page)) <= 0 ) {
    if(np < 0) {
      mp_msg(MSGT_DEMUX,MSGL_ERR,"AVI Ogg error : Can't init using first stream packets\n");
      free(ogg_d);
      goto fallback;
    }
    // Add some data
    plen = ds_get_packet(demuxer->audio,&p);    
    buf = ogg_sync_buffer(&ogg_d->sync,plen);
    memcpy(buf,p,plen);
    ogg_sync_wrote(&ogg_d->sync,plen);
  }
  // Init the logical stream
  mp_msg(MSGT_DEMUX,MSGL_DBG2,"AVI Ogg found page with serial %d\n",ogg_page_serialno(&ogg_d->page));
  ogg_stream_init(&ogg_d->subs[0].stream,ogg_page_serialno(&ogg_d->page));
  // Write the page
  ogg_stream_pagein(&ogg_d->subs[0].stream,&ogg_d->page);

  // Create the ds_stream and the ogg demuxer
  s = new_ds_stream(demuxer->audio);
  od = new_demuxer(s,DEMUXER_TYPE_OGG,0,-2,-2,NULL);

  /// Add the header packets in the ogg demuxer audio stream
  // Initial header
  dp = new_demux_packet(hdrsizes[0]);
  memcpy(dp->buffer,((unsigned char*)sh_audio->wf)+22+sizeof(WAVEFORMATEX)+3*sizeof(uint32_t),hdrsizes[0]);
  ds_add_packet(od->audio,dp);
  /// Comments
  dp = new_demux_packet(hdrsizes[1]);
  memcpy(dp->buffer,((unsigned char*)sh_audio->wf)+22+sizeof(WAVEFORMATEX)+3*sizeof(uint32_t)+hdrsizes[0],hdrsizes[1]);
  ds_add_packet(od->audio,dp);
  /// Code book
  dp = new_demux_packet(hdrsizes[2]);
  memcpy(dp->buffer,((unsigned char*)sh_audio->wf)+22+sizeof(WAVEFORMATEX)+3*sizeof(uint32_t)+hdrsizes[0]+hdrsizes[1],hdrsizes[2]);
  ds_add_packet(od->audio,dp);

  // Finish setting up the ogg demuxer
  od->priv = ogg_d;
  sh_audio = new_sh_audio(od,0);
  od->audio->id = 0;
  od->video->id = -2;
  od->audio->sh = sh_audio;
  sh_audio->ds = od->audio;
  sh_audio->format = FOURCC_VORBIS;

  /// Return the joined demuxers
  return new_demuxers_demuxer(demuxer,od,demuxer);

 fallback:
  demuxer->audio->id = -2;
  return demuxer;

}

static void demux_ogg_seek(demuxer_t *demuxer,float rel_seek_secs,float audio_delay,int flags) {
  ogg_demuxer_t* ogg_d = demuxer->priv;
  ogg_sync_state* sync = &ogg_d->sync;
  ogg_page* page= &ogg_d->page;
  ogg_stream_state* oss;
  ogg_stream_t* os;
  demux_stream_t* ds;
  sh_audio_t* sh_audio = demuxer->audio->sh;
  ogg_packet op;
  float rate;
  int i,sp,first,precision=1,do_seek=1;
  vorbis_info* vi = NULL;
  int64_t gp = 0, old_gp;
  void *context = NULL;
  off_t pos, old_pos;
  int np;
  int is_gp_valid;
  float pts;
  int is_keyframe;
  int samplesize=1;

  if(demuxer->video->id >= 0) {
    ds = demuxer->video;
    /* demux_ogg_read_packet needs decoder context for Theora streams */
    if (((sh_video_t*)demuxer->video->sh)->format == FOURCC_THEORA)
      context = ((sh_video_t*)demuxer->video->sh)->context;
    rate = ogg_d->subs[ds->id].samplerate;
  } else {
    ds = demuxer->audio;
    os = &ogg_d->subs[ds->id];
    /* demux_ogg_read_packet needs decoder context for Vorbis streams */
    if(((sh_audio_t*)demuxer->audio->sh)->format == FOURCC_VORBIS)
      context = ((sh_audio_t*)demuxer->audio->sh)->context;
    vi = &(ogg_d->vi);
    rate = (float)vi->rate;
    samplesize = ((sh_audio_t*)ds->sh)->samplesize;
  }

  os = &ogg_d->subs[ds->id];
  oss = &os->stream;

  old_gp = os->lastpos;
  old_pos = ogg_d->pos;

  //calculate the granulepos to seek to
    gp = flags & 1 ? 0 : os->lastpos;
  if(flags & 2) {
    if (ogg_d->final_granulepos > 0)
      gp += ogg_d->final_granulepos * rel_seek_secs;
      else
      gp += rel_seek_secs * (demuxer->movi_end - demuxer->movi_start) * os->lastpos / ogg_d->pos;
  } else
      gp += rel_seek_secs * rate;
  if (gp < 0) gp = 0;

  //calculate the filepos to seek to
  if(ogg_d->syncpoints) {
    for(sp = 0; sp < ogg_d->num_syncpoint ; sp++) {
      if(ogg_d->syncpoints[sp].granulepos >= gp) break;
    }

    if(sp >= ogg_d->num_syncpoint) return;
    if (sp > 0 && ogg_d->syncpoints[sp].granulepos - gp > gp - ogg_d->syncpoints[sp-1].granulepos)
      sp--;
    if (ogg_d->syncpoints[sp].granulepos == os->lastpos) {
      if (sp > 0 && gp < os->lastpos) sp--;
      if (sp < ogg_d->num_syncpoint-1 && gp > os->lastpos) sp++;
    }
    pos = ogg_d->syncpoints[sp].page_pos;
    precision = 0;
  } else {
    pos = flags & 1 ? 0 : ogg_d->pos;
    if(flags & 2)
      pos += (demuxer->movi_end - demuxer->movi_start) * rel_seek_secs;
    else {
      if (ogg_d->final_granulepos > 0) {
        pos += rel_seek_secs * (demuxer->movi_end - demuxer->movi_start) / (ogg_d->final_granulepos / rate);
      } else if (os->lastpos > 0) {
      pos += rel_seek_secs * ogg_d->pos / (os->lastpos / rate);
    }
  }
    if (pos < 0) pos = 0;
    if (pos > (demuxer->movi_end - demuxer->movi_start))
      pos = demuxer->movi_end - demuxer->movi_start;
  } // if(ogg_d->syncpoints)

  while(1) {
    if (do_seek) {
  stream_seek(demuxer->stream,pos+demuxer->movi_start);
  ogg_sync_reset(sync);
  for(i = 0 ; i < ogg_d->num_sub ; i++) {
    ogg_stream_reset(&ogg_d->subs[i].stream);
    ogg_d->subs[i].lastpos = ogg_d->subs[i].lastsize = 0;
  }
  ogg_d->pos = pos;
  ogg_d->last_size = 0;
      /* we just guess that we reached correct granulepos, in case a
         subsequent search occurs before we read a valid granulepos */
      os->lastpos = gp;
      first = !(ogg_d->syncpoints);
      do_seek=0;
    }
    ogg_d->pos += ogg_d->last_size;
    ogg_d->last_size = 0;
    np = ogg_sync_pageseek(sync,page);

    if(np < 0)
      ogg_d->pos -= np;
    if(np <= 0) { // We need more data
      char* buf = ogg_sync_buffer(sync,BLOCK_SIZE);
      int len = stream_read(demuxer->stream,buf,BLOCK_SIZE);
       if(len == 0 && demuxer->stream->eof) {
	mp_msg(MSGT_DEMUX,MSGL_V,"EOF while trying to seek !!!!\n");
	return;
      }
      ogg_sync_wrote(sync,len);
      continue;
    }
    ogg_d->last_size = np;
    if(ogg_page_serialno(page) != oss->serialno)
      continue;

    if(ogg_stream_pagein(oss,page) != 0)
      continue;

     while(1) {
      np = ogg_stream_packetout(oss,&op);
      if(np < 0)
	continue;
      else if(np == 0)
	break;
      if (first) { /* Discard the first packet as it's probably broken,
           and we don't have any other means to decide whether it is
           complete or not. */
        first = 0;
        break;
      }
      is_gp_valid = (op.granulepos >= 0);
      demux_ogg_read_packet(os,&op,context,&pts,&is_keyframe,samplesize);
      if (precision && is_gp_valid) {
        precision--;
        if (abs(gp - op.granulepos) > rate && (op.granulepos != old_gp)) {
          //prepare another seek because we are off by more than 1s
          pos += (gp - op.granulepos) * (pos - old_pos) / (op.granulepos - old_gp);
          if (pos < 0) pos = 0;
          if (pos < (demuxer->movi_end - demuxer->movi_start)) {
            do_seek=1;
            break;
          }
        }
      }
      if (is_gp_valid && (pos > 0) && (old_gp > gp)
          && (2 * (old_gp - op.granulepos) < old_gp - gp)) {
        /* prepare another seek because looking for a syncpoint
           destroyed the backward search */
        pos = old_pos - 1.5 * (old_pos - pos);
        if (pos < 0) pos = 0;
        if (pos < (demuxer->movi_end - demuxer->movi_start)) {
          do_seek=1;
          break;
        }
      }
      if(!precision && (is_keyframe || os->vorbis || os->speex) ) {
        ogg_sub.lines = 0;
        vo_sub = &ogg_sub;
        vo_osd_changed(OSDTYPE_SUBTITLE);
        clear_sub = -1;
	demux_ogg_add_packet(ds,os,ds->id,&op);
        if (demuxer->video->id < 0)
          sh_audio->delay = pts;
	return;
      }
     }
  }

  mp_msg(MSGT_DEMUX,MSGL_ERR,"Can't find the good packet :(\n");  

}

static void demux_close_ogg(demuxer_t* demuxer) {
  ogg_demuxer_t* ogg_d = demuxer->priv;
  int i;

  if(!ogg_d)
    return;

#ifdef USE_ICONV
  subcp_close();
#endif

  ogg_sync_clear(&ogg_d->sync);
  if(ogg_d->subs)
  {
    for (i = 0; i < ogg_d->num_sub; i++)
      ogg_stream_clear(&ogg_d->subs[i].stream);
    free(ogg_d->subs);
  }
  if(ogg_d->syncpoints)
    free(ogg_d->syncpoints);
  if (ogg_d->text_ids)
    free(ogg_d->text_ids);
  if (ogg_d->text_langs) {
    for (i = 0; i < ogg_d->n_text; i++)
      if (ogg_d->text_langs[i]) free(ogg_d->text_langs[i]);
    free(ogg_d->text_langs);
  }
  free(ogg_d);
}

static int demux_ogg_control(demuxer_t *demuxer,int cmd, void *arg){
  ogg_demuxer_t* ogg_d = demuxer->priv;
  ogg_stream_t* os;
  float rate;

  if(demuxer->video->id >= 0) {
    os = &ogg_d->subs[demuxer->video->id];
    rate = os->samplerate;
  } else {
    os = &ogg_d->subs[demuxer->audio->id];
    rate = (float)((ov_struct_t*)((sh_audio_t*)demuxer->audio->sh)->context)->vi.rate;
  }


    switch(cmd) {
	case DEMUXER_CTRL_GET_TIME_LENGTH:
	    if (ogg_d->final_granulepos<=0) return DEMUXER_CTRL_DONTKNOW;
	    *((double *)arg)=(double)ogg_d->final_granulepos / rate;
	    return DEMUXER_CTRL_GUESS;

	case DEMUXER_CTRL_GET_PERCENT_POS:
	    if (ogg_d->final_granulepos<=0) return DEMUXER_CTRL_DONTKNOW;
	    *((int *)arg)=(int)( (os->lastpos*100) / ogg_d->final_granulepos);
	    return DEMUXER_CTRL_OK;

	default:
	    return DEMUXER_CTRL_NOTIMPL;
    }
}



demuxer_desc_t demuxer_desc_ogg = {
  "Ogg demuxer",
  "ogg",
  "Ogg",
  "?",
  "",
  DEMUXER_TYPE_OGG,
  1, // safe autodetect
  demux_ogg_open,
  demux_ogg_fill_buffer,
  NULL,
  demux_close_ogg,
  demux_ogg_seek,
  demux_ogg_control
};

#endif