-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathplogg.cpp
568 lines (494 loc) · 15.8 KB
/
plogg.cpp
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
// Copyright (C) 2009, Chris Double. All Rights Reserved.
// See the license at the end of this file.
#include <cassert>
#include <cmath>
#include <map>
#include <iostream>
#include <fstream>
#include <ogg/ogg.h>
#include <theora/theora.h>
#include <theora/theoradec.h>
#include <vorbis/codec.h>
#include <SDL/SDL.h>
extern "C" {
#include <sydney_audio.h>
}
using namespace std;
enum StreamType {
TYPE_VORBIS,
TYPE_THEORA,
TYPE_UNKNOWN
};
class OggStream;
class TheoraDecode {
public:
th_info mInfo;
th_comment mComment;
th_setup_info *mSetup;
th_dec_ctx* mCtx;
public:
TheoraDecode() :
mSetup(0),
mCtx(0)
{
th_info_init(&mInfo);
th_comment_init(&mComment);
}
void initForData(OggStream* stream);
~TheoraDecode() {
th_setup_free(mSetup);
th_decode_free(mCtx);
}
};
class VorbisDecode {
public:
vorbis_info mInfo;
vorbis_comment mComment;
vorbis_dsp_state mDsp;
vorbis_block mBlock;
public:
VorbisDecode()
{
vorbis_info_init(&mInfo);
vorbis_comment_init(&mComment);
}
void initForData(OggStream* stream);
};
class OggStream
{
public:
int mSerial;
ogg_stream_state mState;
StreamType mType;
bool mActive;
TheoraDecode mTheora;
VorbisDecode mVorbis;
public:
OggStream(int serial = -1) :
mSerial(serial),
mType(TYPE_UNKNOWN),
mActive(true)
{
}
~OggStream() {
int ret = ogg_stream_clear(&mState);
assert(ret == 0);
}
};
void TheoraDecode::initForData(OggStream* stream) {
stream->mTheora.mCtx =
th_decode_alloc(&stream->mTheora.mInfo,
stream->mTheora.mSetup);
assert(stream->mTheora.mCtx != NULL);
int ppmax = 0;
int ret = th_decode_ctl(stream->mTheora.mCtx,
TH_DECCTL_GET_PPLEVEL_MAX,
&ppmax,
sizeof(ppmax));
assert(ret == 0);
// Set to a value between 0 and ppmax inclusive to experiment with
// this parameter.
ppmax = 0;
ret = th_decode_ctl(stream->mTheora.mCtx,
TH_DECCTL_SET_PPLEVEL,
&ppmax,
sizeof(ppmax));
assert(ret == 0);
}
void VorbisDecode::initForData(OggStream* stream) {
int ret = vorbis_synthesis_init(&stream->mVorbis.mDsp, &stream->mVorbis.mInfo);
assert(ret == 0);
ret = vorbis_block_init(&stream->mVorbis.mDsp, &stream->mVorbis.mBlock);
assert(ret == 0);
}
typedef map<int, OggStream*> StreamMap;
class OggDecoder
{
public:
StreamMap mStreams;
SDL_Surface* mSurface;
SDL_Overlay* mOverlay;
sa_stream_t* mAudio;
ogg_int64_t mGranulepos;
private:
bool handle_theora_header(OggStream* stream, ogg_packet* packet);
bool handle_vorbis_header(OggStream* stream, ogg_packet* packet);
void read_headers(istream& stream, ogg_sync_state* state);
bool read_page(istream& stream, ogg_sync_state* state, ogg_page* page);
bool read_packet(istream& is, ogg_sync_state* state, OggStream* stream, ogg_packet* packet);
void handle_theora_data(OggStream* stream, ogg_packet* packet);
public:
OggDecoder() :
mSurface(0),
mOverlay(0),
mAudio(0),
mGranulepos(0)
{
}
~OggDecoder() {
if (mAudio) {
sa_stream_drain(mAudio);
sa_stream_destroy(mAudio);
}
if (mSurface) {
SDL_FreeSurface(mSurface);
mSurface = 0;
}
}
void play(istream& stream);
};
bool OggDecoder::read_page(istream& stream, ogg_sync_state* state, ogg_page* page) {
int ret = 0;
// If we've hit end of file we still need to continue processing
// any remaining pages that we've got buffered.
if (!stream.good())
return ogg_sync_pageout(state, page) == 1;
while((ret = ogg_sync_pageout(state, page)) != 1) {
// Returns a buffer that can be written too
// with the given size. This buffer is stored
// in the ogg synchronisation structure.
char* buffer = ogg_sync_buffer(state, 4096);
assert(buffer);
// Read from the file into the buffer
stream.read(buffer, 4096);
int bytes = stream.gcount();
if (bytes == 0) {
// End of file.
continue;
}
// Update the synchronisation layer with the number
// of bytes written to the buffer
ret = ogg_sync_wrote(state, bytes);
assert(ret == 0);
}
return true;
}
bool OggDecoder::read_packet(istream& is, ogg_sync_state* state, OggStream* stream, ogg_packet* packet) {
int ret = 0;
while ((ret = ogg_stream_packetout(&stream->mState, packet)) != 1) {
ogg_page page;
if (!read_page(is, state, &page))
return false;
int serial = ogg_page_serialno(&page);
assert(mStreams.find(serial) != mStreams.end());
OggStream* pageStream = mStreams[serial];
// Drop data for streams we're not interested in.
if (stream->mActive) {
ret = ogg_stream_pagein(&pageStream->mState, &page);
assert(ret == 0);
}
}
return true;
}
void OggDecoder::read_headers(istream& stream, ogg_sync_state* state) {
ogg_page page;
bool headersDone = false;
while (!headersDone && read_page(stream, state, &page)) {
int ret = 0;
int serial = ogg_page_serialno(&page);
OggStream* stream = 0;
if(ogg_page_bos(&page)) {
// At the beginning of the stream, read headers
// Initialize the stream, giving it the serial
// number of the stream for this page.
stream = new OggStream(serial);
ret = ogg_stream_init(&stream->mState, serial);
assert(ret == 0);
mStreams[serial] = stream;
}
assert(mStreams.find(serial) != mStreams.end());
stream = mStreams[serial];
// Add a complete page to the bitstream
ret = ogg_stream_pagein(&stream->mState, &page);
assert(ret == 0);
// Process all available header packets in the stream. When we hit
// the first data stream we don't decode it, instead we
// return. The caller can then choose to process whatever data
// streams it wants to deal with.
ogg_packet packet;
while (!headersDone &&
(ret = ogg_stream_packetpeek(&stream->mState, &packet)) != 0) {
assert(ret == 1);
// A packet is available. If it is not a header packet we exit.
// If it is a header packet, process it as normal.
headersDone = headersDone || handle_theora_header(stream, &packet);
headersDone = headersDone || handle_vorbis_header(stream, &packet);
if (!headersDone) {
// Consume the packet
ret = ogg_stream_packetout(&stream->mState, &packet);
assert(ret == 1);
}
}
}
}
void OggDecoder::play(istream& is) {
ogg_sync_state state;
int ret = ogg_sync_init(&state);
assert(ret == 0);
// Read headers for all streams
read_headers(is, &state);
// Find and initialize the first theora and vorbis
// streams. According to the Theora spec these can be considered the
// 'primary' streams for playback.
OggStream* video = 0;
OggStream* audio = 0;
for(StreamMap::iterator it = mStreams.begin(); it != mStreams.end(); ++it) {
OggStream* stream = (*it).second;
if (!video && stream->mType == TYPE_THEORA) {
video = stream;
video->mTheora.initForData(video);
}
else if (!audio && stream->mType == TYPE_VORBIS) {
audio = stream;
audio->mVorbis.initForData(audio);
}
else
stream->mActive = false;
}
assert(audio);
if (video) {
cout << "Video stream is "
<< video->mSerial << " "
<< video->mTheora.mInfo.frame_width << "x" << video->mTheora.mInfo.frame_height
<< endl;
}
cout << "Audio stream is "
<< audio->mSerial << " "
<< audio->mVorbis.mInfo.channels << " channels "
<< audio->mVorbis.mInfo.rate << "KHz"
<< endl;
ret = sa_stream_create_pcm(&mAudio,
NULL,
SA_MODE_WRONLY,
SA_PCM_FORMAT_S16_NE,
audio->mVorbis.mInfo.rate,
audio->mVorbis.mInfo.channels);
assert(ret == SA_SUCCESS);
ret = sa_stream_open(mAudio);
assert(ret == SA_SUCCESS);
// Read audio packets, sending audio data to the sound hardware.
// When it's time to display a frame, decode the frame and display it.
ogg_packet packet;
while (read_packet(is, &state, audio, &packet)) {
if (vorbis_synthesis(&audio->mVorbis.mBlock, &packet) == 0) {
ret = vorbis_synthesis_blockin(&audio->mVorbis.mDsp, &audio->mVorbis.mBlock);
assert(ret == 0);
}
float** pcm = 0;
int samples = 0;
while ((samples = vorbis_synthesis_pcmout(&audio->mVorbis.mDsp, &pcm)) > 0) {
if (mAudio) {
if (samples > 0) {
size_t size = samples * audio->mVorbis.mInfo.channels;
short* buffer = new short[size];
short* p = buffer;
for (int i=0;i < samples; ++i) {
for(int j=0; j < audio->mVorbis.mInfo.channels; ++j) {
int v = static_cast<int>(floorf(0.5 + pcm[j][i]*32767.0));
if (v > 32767) v = 32767;
if (v <-32768) v = -32768;
*p++ = v;
}
}
ret = sa_stream_write(mAudio, buffer, sizeof(*buffer)*size);
assert(ret == SA_SUCCESS);
delete[] buffer;
}
ret = vorbis_synthesis_read(&audio->mVorbis.mDsp, samples);
assert(ret == 0);
}
// At this point we've written some audio data to the sound
// system. Now we check to see if it's time to display a video
// frame.
//
// The granule position of a video frame represents the time
// that that frame should be displayed up to. So we get the
// current time, compare it to the last granule position read.
// If the time is greater than that it's time to display a new
// video frame.
//
// The time is obtained from the audio system - this represents
// the time of the audio data that the user is currently
// listening to. In this way the video frame should be synced up
// to the audio the user is hearing.
//
if (video) {
ogg_int64_t position = 0;
sa_position_t positionType = SA_POSITION_WRITE_SOFTWARE;
#if defined(WIN32)
positionType = SA_POSITION_WRITE_HARDWARE;
#endif
int ret = sa_stream_get_position(mAudio, positionType, &position);
assert(ret == SA_SUCCESS);
float audio_time =
float(position) /
float(audio->mVorbis.mInfo.rate) /
float(audio->mVorbis.mInfo.channels) /
sizeof(short);
float video_time = th_granule_time(video->mTheora.mCtx, mGranulepos);
if (audio_time > video_time) {
// Decode one frame and display it. If no frame is available we
// don't do anything.
ogg_packet packet;
if (read_packet(is, &state, video, &packet)) {
handle_theora_data(video, &packet);
video_time = th_granule_time(video->mTheora.mCtx, mGranulepos);
}
}
}
}
// Check for SDL events to exit
SDL_Event event;
if (SDL_PollEvent(&event) == 1) {
if (event.type == SDL_KEYDOWN &&
event.key.keysym.sym == SDLK_ESCAPE)
break;
if (event.type == SDL_KEYDOWN &&
event.key.keysym.sym == SDLK_SPACE)
SDL_WM_ToggleFullScreen(mSurface);
}
}
// Cleanup
ret = ogg_sync_clear(&state);
assert(ret == 0);
}
bool OggDecoder::handle_theora_header(OggStream* stream, ogg_packet* packet) {
int ret = th_decode_headerin(&stream->mTheora.mInfo,
&stream->mTheora.mComment,
&stream->mTheora.mSetup,
packet);
if (ret == TH_ENOTFORMAT)
return false; // Not a theora header
if (ret > 0) {
// This is a theora header packet
stream->mType = TYPE_THEORA;
return false;
}
// Any other return value is treated as a fatal error
assert(ret == 0);
// This is not a header packet. It is the first
// video data packet.
return true;
}
void OggDecoder::handle_theora_data(OggStream* stream, ogg_packet* packet) {
// The granulepos for a packet gives the time of the end of the
// display interval of the frame in the packet. We keep the
// granulepos of the frame we've decoded and use this to know the
// time when to display the next frame.
int ret = th_decode_packetin(stream->mTheora.mCtx,
packet,
&mGranulepos);
assert(ret == 0 || ret == TH_DUPFRAME);
// If the return code is TH_DUPFRAME then we don't need to
// get the YUV data and display it since it's the same as
// the previous frame.
if (1/*ret == 0*/) {
// We have a frame. Get the YUV data
th_ycbcr_buffer buffer;
ret = th_decode_ycbcr_out(stream->mTheora.mCtx, buffer);
assert(ret == 0);
// Create an SDL surface to display if we haven't
// already got one.
if (!mSurface) {
int r = SDL_Init(SDL_INIT_VIDEO);
assert(r == 0);
mSurface = SDL_SetVideoMode(buffer[0].width,
buffer[0].height,
32,
SDL_SWSURFACE);
assert(mSurface);
}
// Create a YUV overlay to do the YUV to RGB conversion
if (!mOverlay) {
mOverlay = SDL_CreateYUVOverlay(buffer[0].width,
buffer[0].height,
SDL_YV12_OVERLAY,
mSurface);
assert(mOverlay);
}
SDL_Rect rect;
rect.x = 0;
rect.y = 0;
rect.w = buffer[0].width;
rect.h = buffer[0].height;
SDL_LockYUVOverlay(mOverlay);
for (int i=0; i < buffer[0].height; ++i)
memcpy(mOverlay->pixels[0]+(mOverlay->pitches[0]*i),
buffer[0].data+(buffer[0].stride*i),
mOverlay->pitches[0]);
for (int i=0; i < buffer[2].height; ++i)
memcpy(mOverlay->pixels[2]+(mOverlay->pitches[2]*i),
buffer[1].data+(buffer[1].stride*i),
mOverlay->pitches[2]);
for (int i=0; i < buffer[1].height; ++i)
memcpy(mOverlay->pixels[1]+(mOverlay->pitches[1]*i),
buffer[2].data+(buffer[2].stride*i),
mOverlay->pitches[1]);
SDL_UnlockYUVOverlay(mOverlay);
SDL_DisplayYUVOverlay(mOverlay, &rect);
}
}
bool OggDecoder::handle_vorbis_header(OggStream* stream, ogg_packet* packet) {
int ret = vorbis_synthesis_headerin(&stream->mVorbis.mInfo,
&stream->mVorbis.mComment,
packet);
// Unlike libtheora, libvorbis does not provide a return value to
// indicate that we've finished loading the headers and got the
// first data packet. To detect this I check if I already know the
// stream type and if the vorbis_synthesis_headerin call failed.
if (stream->mType == TYPE_VORBIS && ret == OV_ENOTVORBIS) {
// First data packet
return true;
}
else if (ret == 0) {
stream->mType = TYPE_VORBIS;
}
return false;
}
void usage() {
cout << "Usage: plogg <filename>" << endl;
}
int main(int argc, char* argv[]) {
if (argc != 2) {
usage();
return 0;
}
ifstream file(argv[1], ios::in | ios::binary);
if (file) {
OggDecoder decoder;
decoder.play(file);
file.close();
for(StreamMap::iterator it = decoder.mStreams.begin();
it != decoder.mStreams.end();
++it) {
OggStream* stream = (*it).second;
delete stream;
}
}
SDL_Quit();
return 0;
}
// Copyright (C) 2009 Chris Double. All Rights Reserved.
// The original author of this code can be contacted at: chris.double@double.co.nz
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//