annotate liboggplayer-src/src/imp.hpp @ 2:105513a2e3c9

Import liboggplayer source.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 05 Aug 2013 13:50:20 +0300
parents
children 083c73ceb716
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1 #include <oggplayer.h>
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2 #include "SDL_audiocvt.hpp"
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3 #include <fstream>
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
4 #include <vector>
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
5 #include <theora/theora.h>
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
6 #include <vorbis/codec.h>
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
7 #include <boost/thread.hpp>
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
8 #include <boost/bind.hpp>
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
9 #include <boost/thread/mutex.hpp>
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
10 #include <boost/thread/locks.hpp>
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
11 #include <boost/thread/condition.hpp>
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
12 #include <boost/circular_buffer.hpp>
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
13 #include <boost/timer.hpp>
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
14
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
15 struct OggPlayer::Imp {
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
16
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
17 Imp() :
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
18 cirbuf(0),video_lock(video_mut, boost::defer_lock){
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
19 failbit = false;
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
20 playing = false;
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
21 need_close = false;
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
22 time_factor = 1;
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
23 refs = 1;
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
24 }
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
25
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
26 ~Imp() {
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
27 // open() tears down the partial setup on fail
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
28 if (!failbit)
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
29 close();
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
30 }
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
31 int queue_page(ogg_page * page);
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
32 bool buffer_data();
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
33 double get_time();
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
34 void close();
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
35 bool init_decoders();
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
36 bool parse_headers();
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
37 void open(std::string path, AudioFormat audio_format, int channels,
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
38 int rate, VideoFormat video_format);
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
39 bool decode_audio();
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
40 bool decode_video();
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
41 bool ready();
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
42 void play();
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
43 void play_loop();
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
44
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
45 bool failbit;
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
46 std::ifstream file_in;
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
47
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
48 int theora_p;
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
49 int vorbis_p;
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
50
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
51 ogg_sync_state o_sync;
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
52
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
53 vorbis_comment v_comment;
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
54 theora_info t_info;
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
55 theora_comment t_comment;
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
56 vorbis_info v_info;
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
57
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
58 ogg_page o_page;
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
59 ogg_packet o_packet;
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
60
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
61 ogg_stream_state o_tsstate;
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
62 ogg_stream_state o_vsstate;
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
63
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
64 theora_state t_state;
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
65 vorbis_dsp_state v_state;
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
66 vorbis_block v_block;
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
67
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
68 int pp_level_max;
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
69 int pp_level;
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
70 int pp_inc;
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
71
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
72 char* audio_buffer;
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
73 boost::circular_buffer<char> cirbuf;
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
74 unsigned int audio_buffer_size;
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
75 SDL_AudioCVT cvt;
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
76
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
77 bool playing;
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
78
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
79 boost::thread play_thread;
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
80 boost::mutex audio_mut;
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
81 boost::mutex video_mut;
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
82 boost::condition_variable audio_ready_cond;
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
83 boost::condition_variable video_ready_cond;
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
84
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
85 bool videobuf_ready;
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
86 double videobuf_time;
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
87
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
88 bool audio_cache_ready;
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
89
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
90 // This lock should only be used from the main thread
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
91 boost::unique_lock<boost::mutex> video_lock;
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
92 // These variables are used to identify the frames (not just count them, not a debug variable)
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
93 // see video_buffer_try_lock and video_read
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
94 int frame;
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
95 int last_frame_read;
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
96
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
97 int audio_bytes_played;
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
98 struct AudioGranulePos {
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
99 int pos;
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
100 double set_time;
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
101 };
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
102 std::deque<AudioGranulePos> audio_granule_poses;
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
103 double time_factor;
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
104
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
105 struct PixelFormat{
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
106 void set(int r,int g,int b,int bpp){
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
107 r_offset = r;
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
108 g_offset = g;
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
109 b_offset = b;
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
110 this->bpp =bpp;
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
111 }
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
112 int r_offset;
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
113 int g_offset;
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
114 int b_offset;
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
115 // if bpp=4 we have an alpha channel at offset 4
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
116 int bpp;
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
117
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
118 } pixel_format;
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
119 bool need_close;
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
120 boost::timer timer;
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
121 int refs;
105513a2e3c9 Import liboggplayer source.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
122 };