summaryrefslogtreecommitdiffstats
path: root/mpeglib/lib/oggvorbis/vorbisInfo.cpp
blob: b71f99c318d7d133985ff6adca22eecccbfaa60d (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
/*
  info about vorbis files.
  Copyright (C) 2001  Martin Vogt

  This program 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.

  For more information look at the file COPYRIGHT in this package

 */


#include "vorbisInfo.h"

#include <iostream>

using namespace std;

#ifdef OGG_VORBIS
#define CHUNKSIZE 4096

#define GETINPUT(stream,input)                                            \
                               VorbisInfo* info=(VorbisInfo*) stream;     \
                               FileAccess* input=info->getInput();   


size_t fread_func2(void *ptr, size_t size, size_t nmemb, void *stream) {
  GETINPUT(stream,input);

  size_t want=size*nmemb;
  size_t back=input->read((char*)ptr,want);
  return back;
}


int fseek_func2(void *stream, ogg_int64_t offset, int whence) {
  int ret;
  GETINPUT(stream,input);

  if (whence==SEEK_SET) {
    ret=input->seek(offset);
    info->setSeekPos(offset);
    return ret;
  }
  if (whence==SEEK_CUR) {
    ret=input->seek(input->getBytePosition()+offset);
    return ret;
  }  
  if (whence==SEEK_END) {
    ret=input->seek(input->getByteLength());
    return ret;
  }   
  cout << "hm, strange call"<<endl;
  return -1;
}


int fclose_func2 (void * stream) {
  cout << "fclose_func"<<endl;
  GETINPUT(stream,input);
  // its handled different in kmpg
  // we close the stream if the decoder signals eof.
  return true;

}


long ftell_func2  (void *stream) {
  GETINPUT(stream,input);
  return input->getBytePosition();
}




VorbisInfo::VorbisInfo(FileAccess* input) {
  this->input=input;
  vf=new OggVorbis_File();

  ov_callbacks callbacks;

  callbacks.read_func  = fread_func2;
  callbacks.seek_func  = fseek_func2;
  callbacks.close_func = fclose_func2;
  callbacks.tell_func  = ftell_func2;
 
  if(ov_open_callbacks(this, vf, NULL, 0, callbacks) < 0) {
    cout << "error ov_open_callbacks"<<endl;
  }
  
  // now init stream
  vi=ov_info(vf,-1);
  lastSeekPos=0;
}


VorbisInfo::~VorbisInfo() {
  delete vf;
  if (vi != NULL) {
    //?
  }
}


long VorbisInfo::getSeekPosition(int seconds) {
  int back=0;
  if (vi != NULL) {
    lastSeekPos=0;
    ov_time_seek(vf,seconds);
    back=lastSeekPos;
  }
  return back;
}


long VorbisInfo::getLength() {
  int back=0;
  if (vi != NULL) {
    back = (int) ov_time_total(vf, -1);
  }
  return back;
}




void VorbisInfo::print(const char* msg) {
  cout << "VorbisInfo:"<<msg<<endl;
  cout << "Length (sec):"<<getLength()<<endl;

}


void VorbisInfo::setSeekPos(long pos) {
  this->lastSeekPos=pos;
}


long VorbisInfo::getSeekPos() {
  return lastSeekPos;
}

FileAccess* VorbisInfo::getInput() {
  return input;
}



#endif