summaryrefslogtreecommitdiffstats
path: root/mpeglib/lib/oggvorbis/ovFramer.cpp
blob: ca3506af491f2fa6de5eebbfa849e7fd678077ab (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
/*
  frames raw data into Ogg/Vorbis frames.
  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 "ovFramer.h"

#define OGG_SYNC_BUFF_SIZE      4096


#define _OV_SETSERIAL      1
#define _OV_STREAMIN       2
#define _OV_STREAMOUT      3

#include <iostream>

using namespace std;


#ifdef OGG_VORBIS

OVFramer::OVFramer(OGGFrame* dest):Framer(1) {
  if (dest == NULL) {
    cout << "OGGFrame NULL in OVFramer"<<endl;
    exit(-1);
  }
  this->dest=dest;
  /********** Decode setup ************/
  ogg_sync_init(&oy); /* Now we can read pages */

  vorbis_state=_OV_SETSERIAL;
}


OVFramer::~OVFramer() {
  /* OK, clean up the framer */
  ogg_sync_clear(&oy);
}


int OVFramer::find_frame(RawDataBuffer* input,RawDataBuffer* store) {
  while(input->eof()==true) {
    cout << "input eof"<<endl;
    return false;
  }
  
  if (vorbis_state == _OV_STREAMOUT) {
    if(ogg_stream_packetout(&os,(ogg_packet*)dest->getData())!=1){ 
      vorbis_state=_OV_STREAMIN;
      return false;
    }
    return true;
  }

  // do we have ogg packets in the ogg framer?
  if (ogg_sync_pageout(&oy,&og) == 0) {
    // no, ok insert some.
    int bytes=input->untilend();
    input->inc(bytes);
    store->inc(bytes);
    ogg_sync_wrote(&oy,bytes);
    // and setup the next buffer
    /* submit a 4k block to libvorbis' Ogg layer */
    buffer=ogg_sync_buffer(&oy,OGG_SYNC_BUFF_SIZE);
    /* override our own dummy buffer with size 1 */
    setRemoteFrameBuffer((unsigned char*)buffer,OGG_SYNC_BUFF_SIZE);
    return false;
  }
  // we have an ogg page
  // now try to build an ogg packet
  switch(vorbis_state) {
  case _OV_SETSERIAL:
    /* Get the serial number and set up the rest of decode. */
    /* serialno first; use it to set up a logical stream */
    ogg_stream_init(&os,ogg_page_serialno(&og));
    vorbis_state=_OV_STREAMIN;
    // yes we need to put this into the "pager"
    // no break.
  case _OV_STREAMIN:
    if(ogg_stream_pagein(&os,&og)<0){ 
      /* error; stream version mismatch perhaps */
      fprintf(stderr,"Error reading first page of Ogg bitstream data.\n");
      exit(1);
    }
    vorbis_state=_OV_STREAMOUT;
    break;
  default:
    cout << "unknow vorbis_state"<<endl;
    exit(-1);
  }
  
  return false;
}

int OVFramer::read_frame(RawDataBuffer* ,RawDataBuffer* ) {
  return true;
}
    
    

void OVFramer::unsync(RawDataBuffer* store,int lReset) {
  if (lReset) {
    store->setpos(0);
    ogg_sync_reset(&oy);
    /* submit a 4k block to libvorbis' Ogg layer */
    buffer=ogg_sync_buffer(&oy,OGG_SYNC_BUFF_SIZE);
    /* override our own dummy buffer with size 1 */
    setRemoteFrameBuffer((unsigned char*)buffer,OGG_SYNC_BUFF_SIZE);
  }

}


void OVFramer::printPrivateStates() {
  cout << "OVFramer::printPrivateStates"<<endl;
}


#endif