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

« back to all changes in this revision

Viewing changes to mpeglib/lib/decoder/tplayPlugin.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
  tplay player plugin
 
3
  Copyright (C) 1999  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 "tplayPlugin.h"
 
15
 
 
16
#include "../tplay/tplayfunctions.h"
 
17
 
 
18
 
 
19
 
 
20
TplayPlugin::TplayPlugin() {
 
21
  info=new info_struct();
 
22
 
 
23
  info->progname = NULL;
 
24
  info->loop = FALSE;
 
25
  info->in_seconds = FALSE;
 
26
  info->speed = DEFAULT_SPEED;
 
27
  info->bits = DEFAULT_BITS;
 
28
  info->channels = DEFAULT_CHANNELS;
 
29
  info->buffer_size = BUFFER_SIZE;
 
30
  info->show_usage = FALSE;
 
31
  info->swap = FALSE;
 
32
  info->forceraw = FALSE;
 
33
  info->force = FALSE;
 
34
  info->device = NULL;
 
35
  info->verbose = FALSE;
 
36
  info->optind = 0;
 
37
  info->buffer = NULL;
 
38
  info->firstblock = NULL;
 
39
  info->number_of_blocks = 0;
 
40
  info->readblock = 0;
 
41
  info->writeblock = 0;
 
42
  info->readcount = 0;
 
43
  info->writecount = 0;
 
44
  info->alldone = FALSE;
 
45
  info->overflow = FALSE;
 
46
  info->underflow = FALSE;
 
47
  info->audioset = FALSE;
 
48
  info->headerskip = 0;
 
49
  info->blocksize = 4096;
 
50
  info->bytes_on_last_block = 0;
 
51
 
 
52
  startStamp=new TimeStamp();
 
53
  endStamp=new TimeStamp();
 
54
}
 
55
 
 
56
 
 
57
TplayPlugin::~TplayPlugin() {
 
58
  delete startStamp;
 
59
  delete endStamp;
 
60
  delete info;
 
61
}
 
62
 
 
63
 
 
64
 
 
65
void TplayPlugin::decoder_loop() {
 
66
  int bytesread, count, stereo;
 
67
  char *p, *bufferp;
 
68
 
 
69
  if (input == NULL) {
 
70
    cout << "TplayPlugin::decoder_loop input is NULL"<<endl;
 
71
    exit(0);
 
72
  }
 
73
  if (output == NULL) {
 
74
    cout << "TplayPlugin::decoder_loop output is NULL"<<endl;
 
75
    exit(0);
 
76
  }
 
77
 
 
78
  TimeStamp* stamp;
 
79
  long pos;
 
80
 
 
81
  info->buffer = (char*) malloc(info->buffer_size * sizeof(char));
 
82
 
 
83
  while(runCheck()) {
 
84
    switch(streamState) {
 
85
    case _STREAM_STATE_FIRST_INIT : {
 
86
       read_header();
 
87
       
 
88
       if (info->channels == 1)
 
89
         stereo = FALSE;
 
90
       else
 
91
         stereo = TRUE;
 
92
 
 
93
       info->number_of_blocks = 0;
 
94
       bufferp = info->buffer;
 
95
       pluginInfo->setLength(getTotalLength());
 
96
       output->writeInfo(pluginInfo);
 
97
       lhasLength=true;
 
98
 
 
99
       setStreamState(_STREAM_STATE_INIT);
 
100
       break;
 
101
    }
 
102
    case _STREAM_STATE_INIT :
 
103
      setStreamState(_STREAM_STATE_PLAY);
 
104
      cout << "audioSetup call"<<endl;
 
105
      output->audioOpen();
 
106
      output->audioSetup(info->speed, stereo, 1, 0, info->bits);
 
107
      break;   
 
108
    case _STREAM_STATE_PLAY :
 
109
      bytesread = 0;
 
110
      count = 0;
 
111
      p = bufferp;
 
112
      while ((bytesread < info->blocksize) && (count != -1) &&
 
113
             ((count = input->read(p,info->blocksize-bytesread))!=0)){
 
114
        p += count;
 
115
        bytesread += count;
 
116
      }
 
117
      if (info->swap) {
 
118
        swap_block(bufferp, bytesread);
 
119
      }
 
120
      
 
121
      if (bytesread > 0) {
 
122
        pos=input->getBytePosition();
 
123
        stamp=input->getTimeStamp(pos-bytesread);
 
124
        output->audioPlay(stamp,endStamp,(char *) bufferp, bytesread);
 
125
      }
 
126
 
 
127
      if (bytesread < info->blocksize) {
 
128
        info->alldone = TRUE;
 
129
 
 
130
      }
 
131
      break;
 
132
    case _STREAM_STATE_WAIT_FOR_END:
 
133
      // exit while loop
 
134
      lDecoderLoop=false;
 
135
      break;
 
136
    default:
 
137
      cout << "unknown stream state:"<<streamState<<endl;
 
138
    }
 
139
  }
 
140
  cout << "tplay exit"<<endl;
 
141
  free(info->buffer);
 
142
  info->buffer = NULL;
 
143
  output->audioFlush();
 
144
}
 
145
 
 
146
 
 
147
int TplayPlugin::seek_impl(int second) {
 
148
  int bytes_per_second;
 
149
  int seconds;
 
150
  int back;
 
151
  
 
152
 
 
153
  bytes_per_second = info->speed * info->channels * (info->bits / 8);
 
154
 
 
155
 
 
156
  
 
157
  seconds = bytes_per_second * second;
 
158
  back=input->seek(seconds);
 
159
 
 
160
  return back;
 
161
}
 
162
 
 
163
 
 
164
void TplayPlugin::config(const char* key,const char* value,void* user_data) {
 
165
  DecoderPlugin::config(key,value,user_data);
 
166
}
 
167
 
 
168
 
 
169
void TplayPlugin::swap_block(char * buffer, int blocksize) {
 
170
  register int i;
 
171
  char c, *p;
 
172
 
 
173
  p = buffer;
 
174
  for (i = 0; i < (blocksize / 2); i++) {
 
175
    c = *p;
 
176
    *p = *(p + 1);
 
177
    *++p = c;
 
178
    p++;
 
179
  }
 
180
}
 
181
 
 
182
 
 
183
void TplayPlugin::read_header() {
 
184
  int bytesread, count;
 
185
  char *p, *bufferp;
 
186
 
 
187
  info->firstblock = (char*) malloc(info->blocksize * sizeof(char));
 
188
  bufferp = info->firstblock;
 
189
  if (!info->forceraw) {
 
190
    bytesread = 0;
 
191
    count = 0;
 
192
    p = bufferp;
 
193
    while ((bytesread < info->blocksize) && (count != -1) &&
 
194
           ((count=input->read(p,info->blocksize-bytesread)) != 0)){
 
195
      p += count;
 
196
      bytesread += count;
 
197
    }
 
198
 
 
199
    if (bytesread < SUN_HDRSIZE)
 
200
      cout << "Sample size is too small"<<endl;
 
201
    
 
202
    if (read_au(info,info->firstblock) && read_wav(info,info->firstblock)) {
 
203
      if (info->verbose)
 
204
        printf("Playing raw data: %ld samples/s, %d bits, %d channels.\n",
 
205
               info->speed, info->bits, info->channels);
 
206
    }
 
207
    
 
208
    if (info->swap)
 
209
      swap_block(bufferp, bytesread);
 
210
    
 
211
    if (bytesread < info->blocksize) {
 
212
      info->alldone = TRUE;
 
213
      info->bytes_on_last_block = bytesread;
 
214
      return;
 
215
    }
 
216
    
 
217
    if (info->headerskip) {
 
218
      count = 0;
 
219
      bytesread = info->blocksize - info->headerskip;
 
220
      p = info->firstblock + (info->blocksize - info->headerskip);
 
221
      while ((bytesread < info->blocksize) && (count != -1) &&
 
222
             ((count = input->read(p,info->blocksize - bytesread)) != 0)) {
 
223
        p += count;
 
224
        bytesread += count;
 
225
      }
 
226
    }
 
227
    
 
228
    info->writeblock++;
 
229
    info->writecount++;
 
230
  }
 
231
  else {
 
232
    if (info->verbose)
 
233
      printf("Playing raw data: %ld samples/s, %d bits, %d channels\n",
 
234
             info->speed, info->bits, info->channels);
 
235
  }
 
236
}
 
237
 
 
238
/**
 
239
   Nobody is perfect I'm too tired to fix this race
 
240
*/
 
241
 
 
242
int TplayPlugin::getTotalLength() {
 
243
  float wavfilesize = input->getByteLength();
 
244
  int back=0;
 
245
 
 
246
 
 
247
  float frequence=info->speed;
 
248
 
 
249
  int bits=info->bits;
 
250
 
 
251
 
 
252
  if (bits == 16) {
 
253
    wavfilesize=wavfilesize/2;
 
254
  }
 
255
  if (info->channels==2) {
 
256
    wavfilesize=wavfilesize/2;
 
257
  }
 
258
  if (frequence != 0) {
 
259
    back=(int)(wavfilesize/frequence);
 
260
  }
 
261
  return back;
 
262
}