#include "config.h"
#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/stream.h"
#include "demuxer.h"
#include "stheader.h"
#include "libavutil/intreadwrite.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
/* 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;
vorbis_info vi;
int vi_initialized;
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;
} 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 char *dvdsub_lang, *audio_lang;
extern int dvdsub_id;
//-------- 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;
//FILE* subout;
void demux_ogg_add_sub (ogg_stream_t* os,ogg_packet* pack) {
int lcv;
char *packet = pack->packet;
if (pack->bytes < 4)
return;
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]);
if (((unsigned char)packet[0]) == 0x88) { // some subtitle text
// Find data start
double endpts = MP_NOPTS_VALUE;
int32_t duration = 0;
int16_t hdrlen = (*packet & PACKET_LEN_BITS01)>>6, i;
hdrlen |= (*packet & PACKET_LEN_BITS2) <<1;
lcv = 1 + hdrlen;
if (pack->bytes < lcv)
return;
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;
endpts = 1.0 + pts + (float)duration/1000.0;
}
sub_clear_text(&ogg_sub, MP_NOPTS_VALUE);
sub_add_text(&ogg_sub, &packet[lcv], pack->bytes - lcv, endpts);
}
mp_msg(MSGT_DEMUX,MSGL_DBG2,"Ogg sub lines: %d first: '%s'\n",
ogg_sub.lines, ogg_sub.text[0]);
#ifdef USE_ICONV
subcp_recode(&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
|