~ubuntu-branches/ubuntu/hoary/kdemultimedia/hoary

« back to all changes in this revision

Viewing changes to mpeglib/lib/oggvorbis/vorbisDecoder.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Martin Schulze
  • Date: 2003-01-22 15:00:51 UTC
  • Revision ID: james.westby@ubuntu.com-20030122150051-uihwkdoxf15mi1tn
Tags: upstream-2.2.2
ImportĀ upstreamĀ versionĀ 2.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  converts ogg frames into audioFrames
 
3
  Copyright (C) 2001  Martin Vogt
 
4
 
 
5
  This program is free software; you can redistribute it and/or modify
 
6
  it under the terms of the GNU Library General Public License as published by
 
7
  the Free Software Foundation.
 
8
 
 
9
  For more information look at the file COPYRIGHT in this package
 
10
 
 
11
 */
 
12
 
 
13
 
 
14
#include "vorbisDecoder.h"
 
15
 
 
16
#ifdef OGG_VORBIS
 
17
 
 
18
#define _VORBIS_NEED_SYNTHHEADER_1     1
 
19
#define _VORBIS_NEED_SYNTHHEADER_2     2
 
20
#define _VORBIS_NEED_SYNTHHEADER_3     3
 
21
 
 
22
#define _VORBIS_DECODE_SETUP           4
 
23
#define _VORBIS_DECODE_LOOP            5
 
24
 
 
25
 
 
26
 
 
27
VorbisDecoder::VorbisDecoder() {
 
28
  vorbis_info_init(&vi);
 
29
  vorbis_comment_init(&vc);
 
30
  reset();
 
31
}
 
32
 
 
33
 
 
34
VorbisDecoder::~VorbisDecoder() {
 
35
}
 
36
 
 
37
int VorbisDecoder::hasHeader() {
 
38
  return (initState>=_VORBIS_DECODE_LOOP);
 
39
}
 
40
 
 
41
int VorbisDecoder::decode(RawFrame* rawFrame,AudioFrame* dest) {
 
42
  
 
43
  if ((rawFrame == NULL) || (dest == NULL)) {
 
44
    cout << "VorbisDecoder::decode NULL pointer!"<<endl;
 
45
    exit(-1);
 
46
  }
 
47
  if (rawFrame->getFrameType() != _FRAME_RAW_OGG) {
 
48
    cout << "VorbisDecoder::decode not _FRAME_RAW_OGG"<<endl;
 
49
    exit(-1);
 
50
  }
 
51
 
 
52
  ogg_packet* op=(ogg_packet*) rawFrame->getData();
 
53
  switch(initState) {
 
54
  case _VORBIS_NEED_SYNTHHEADER_1:
 
55
  case _VORBIS_NEED_SYNTHHEADER_2:
 
56
  case _VORBIS_NEED_SYNTHHEADER_3:
 
57
    cout << "_VORBIS_NEED_SYNTHHEADER:"<<initState<<endl;
 
58
    if(vorbis_synthesis_headerin(&vi,&vc,op)<0){ 
 
59
      /* error case; not a vorbis header */
 
60
      fprintf(stderr,"This Ogg bitstream does not contain Vorbis "
 
61
              "audio data.\n");
 
62
      exit(1);
 
63
    }
 
64
    initState++;
 
65
    break;
 
66
  case _VORBIS_DECODE_SETUP:
 
67
    cout << "_VORBIS_DECODE_SETUP"<<endl;
 
68
    vorbis_synthesis_init(&vd,&vi); /* central decode state */
 
69
    vorbis_block_init(&vd,&vb);     /* local state for most of the decode
 
70
                                       so multiple block decodes can
 
71
                                       proceed in parallel.  We could init
 
72
                                       multiple vorbis_block structures   
 
73
                                       for vd here */
 
74
    initState=_VORBIS_DECODE_LOOP;
 
75
    // yes right, we must decode the packet!
 
76
    // so there is no break here.
 
77
  case _VORBIS_DECODE_LOOP: {
 
78
    if(vorbis_synthesis(&vb,op)==0) {/* test for success! */
 
79
      vorbis_synthesis_blockin(&vd,&vb);
 
80
    } else {
 
81
      cout << "vorbis_synthesis error"<<endl;
 
82
      exit(0);
 
83
    }
 
84
    float **pcm;
 
85
    /*
 
86
 
 
87
    **pcm is a multichannel float vector.  In stereo, for
 
88
    example, pcm[0] is left, and pcm[1] is right.  samples is
 
89
    the size of each channel.  Convert the float values
 
90
    (-1.<=range<=1.) to whatever PCM format and write it out 
 
91
 
 
92
    */
 
93
    int samples=vorbis_synthesis_pcmout(&vd,&pcm);
 
94
    if (samples > 0) {
 
95
      int maxSamples=dest->getSize();
 
96
      if (samples > maxSamples) {
 
97
        cout << "more samples in vorbis than we can store"<<endl;
 
98
        exit(0);
 
99
      }
 
100
      dest->clearrawdata();
 
101
      dest->setFrameFormat(vi.channels-1,vi.rate);
 
102
      
 
103
      if (vi.channels == 2) {
 
104
        dest->putFloatData(pcm[0],pcm[1],samples);
 
105
      } else {
 
106
        dest->putFloatData(pcm[0],NULL,samples);
 
107
      }
 
108
 
 
109
      vorbis_synthesis_read(&vd,samples); /* tell libvorbis how
 
110
                                             many samples we
 
111
                                             actually consumed */
 
112
      return true;
 
113
    }
 
114
    
 
115
    return false;
 
116
  }
 
117
  default:
 
118
    cout << "unknown state in vorbis decoder"<<endl;
 
119
    exit(0);
 
120
  }
 
121
  return false;
 
122
}
 
123
 
 
124
void VorbisDecoder::reset() {
 
125
  initState=_VORBIS_NEED_SYNTHHEADER_1;
 
126
}
 
127
 
 
128
void VorbisDecoder::config(const char* ,const char* ,void* ) {
 
129
  
 
130
}
 
131
 
 
132
#endif