2
|
1 #include "imp.hpp"
|
|
2
|
|
3 void OggPlayer::Imp::close() {
|
|
4 if(!need_close) return;
|
|
5
|
|
6 if (vorbis_p) {
|
|
7 ogg_stream_clear(&o_vsstate);
|
|
8 vorbis_block_clear(&v_block);
|
|
9 vorbis_dsp_clear(&v_state);
|
|
10 }
|
|
11 vorbis_comment_clear(&v_comment);
|
|
12 vorbis_info_clear(&v_info);
|
|
13
|
|
14 if (theora_p) {
|
|
15 ogg_stream_clear(&o_tsstate);
|
|
16 theora_clear(&t_state);
|
|
17 }
|
|
18 theora_comment_clear(&t_comment);
|
|
19 theora_info_clear(&t_info);
|
|
20
|
|
21 ogg_sync_clear(&o_sync);
|
|
22 file_in.close();
|
|
23 if (audio_buffer){
|
|
24 delete[] audio_buffer;
|
|
25 audio_buffer = NULL;
|
|
26 }
|
|
27 need_close = false;
|
|
28 }
|
|
29 bool OggPlayer::Imp::parse_headers() {
|
|
30 int stateflag = 0;
|
|
31 while (!stateflag) {
|
|
32 if (!buffer_data())
|
|
33 break;
|
|
34 while (ogg_sync_pageout(&o_sync, &o_page) > 0) {
|
|
35 ogg_stream_state test;
|
|
36
|
|
37 /* is this a mandated initial header? If not, stop parsing */
|
|
38 if (!ogg_page_bos(&o_page)) {
|
|
39 /* don't leak the page; get it into the appropriate stream */
|
|
40 queue_page(&o_page);
|
|
41 stateflag = 1;
|
|
42 break;
|
|
43 }
|
|
44
|
|
45 ogg_stream_init(&test, ogg_page_serialno(&o_page));
|
|
46 ogg_stream_pagein(&test, &o_page);
|
|
47 ogg_stream_packetout(&test, &o_packet);
|
|
48
|
|
49 /* identify the codec: try theora */
|
|
50 if (!theora_p && theora_decode_header(&t_info, &t_comment,
|
|
51 &o_packet) >= 0) {
|
|
52 /* it is theora */
|
|
53 memcpy(&o_tsstate, &test, sizeof(test));
|
|
54 theora_p = 1;
|
|
55 } else if (!vorbis_p && vorbis_synthesis_headerin(&v_info,
|
|
56 &v_comment, &o_packet) >= 0) {
|
|
57 /* it is vorbis */
|
|
58 memcpy(&o_vsstate, &test, sizeof(test));
|
|
59 vorbis_p = 1;
|
|
60 } else {
|
|
61 /* whatever it is, we don't care about it */
|
|
62 ogg_stream_clear(&test);
|
|
63 }
|
|
64 }
|
|
65 }
|
|
66
|
|
67 // we're expecting more header packets.
|
|
68 while ((theora_p && theora_p < 3) || (vorbis_p && vorbis_p < 3)) {
|
|
69 int ret;
|
|
70
|
|
71 // look for further theora headers
|
|
72 while (theora_p && (theora_p < 3) && (ret = ogg_stream_packetout(
|
|
73 &o_tsstate, &o_packet))) {
|
|
74 if (ret < 0) {
|
|
75 return false;
|
|
76 }
|
|
77 if (theora_decode_header(&t_info, &t_comment, &o_packet)) {
|
|
78 return false;
|
|
79 }
|
|
80 theora_p++;
|
|
81 }
|
|
82
|
|
83 // look for more vorbis header packets
|
|
84 while (vorbis_p && (vorbis_p < 3) && (ret = ogg_stream_packetout(
|
|
85 &o_vsstate, &o_packet))) {
|
|
86 if (ret < 0) {
|
|
87 return false;
|
|
88 }
|
|
89 if (vorbis_synthesis_headerin(&v_info, &v_comment, &o_packet)) {
|
|
90 return false;
|
|
91 }
|
|
92 vorbis_p++;
|
|
93 if (vorbis_p == 3)
|
|
94 break;
|
|
95 }
|
|
96
|
|
97 // The header pages/packets will arrive before anything else we
|
|
98 // care about, or the stream is not obeying spec
|
|
99
|
|
100 if (ogg_sync_pageout(&o_sync, &o_page) > 0) {
|
|
101 queue_page(&o_page); /* demux into the appropriate stream */
|
|
102 } else {
|
|
103 if (!buffer_data()) /* someone needs more data */{
|
|
104 return false;
|
|
105 }
|
|
106 }
|
|
107 }
|
|
108 return true;
|
|
109
|
|
110 }
|
|
111
|
|
112 bool OggPlayer::Imp::init_decoders() {
|
|
113 if (theora_p) {
|
|
114 theora_decode_init(&t_state, &t_info);
|
|
115 theora_control(&t_state, TH_DECCTL_GET_PPLEVEL_MAX, &pp_level_max,
|
|
116 sizeof(pp_level_max));
|
|
117 pp_level = pp_level_max;
|
|
118 theora_control(&t_state, TH_DECCTL_SET_PPLEVEL, &pp_level,
|
|
119 sizeof(pp_level));
|
|
120 pp_inc = 0;
|
|
121 } else {
|
|
122 return false;
|
|
123 }
|
|
124 if (vorbis_p) {
|
|
125 vorbis_synthesis_init(&v_state, &v_info);
|
|
126 vorbis_block_init(&v_state, &v_block);
|
|
127 } else {
|
|
128 return false;
|
|
129 }
|
|
130 return true;
|
|
131 }
|
|
132
|
|
133 void OggPlayer::Imp::open(std::string path, AudioFormat audio_format, int channels,
|
|
134 int rate, VideoFormat video_format) {
|
|
135
|
|
136 theora_p = 0;
|
|
137 vorbis_p = 0;
|
|
138 videobuf_ready = false;
|
|
139 frame=0;
|
|
140 last_frame_read=0;
|
|
141
|
|
142 // 1) open the input file
|
|
143 file_in.open(path.c_str(), std::ios::binary);
|
|
144 if (file_in.fail()) {
|
|
145 failbit = true;
|
|
146 return;
|
|
147 }
|
|
148
|
|
149 // 2) init some structs
|
|
150 ogg_sync_init(&o_sync);
|
|
151 vorbis_info_init(&v_info);
|
|
152 vorbis_comment_init(&v_comment);
|
|
153 theora_info_init(&t_info);
|
|
154 theora_comment_init(&t_comment);
|
|
155
|
|
156 // 3) parse headers
|
|
157 if (!failbit && !parse_headers()) {
|
|
158 failbit = true;
|
|
159 }
|
|
160
|
|
161 // 3) init decoders
|
|
162 if (!failbit && !init_decoders()) {
|
|
163 failbit = true;
|
|
164 }
|
|
165
|
|
166 // 4) if fail, tear down the partial setup
|
|
167 if (failbit) {
|
|
168 theora_info_clear(&t_info);
|
|
169 theora_comment_clear(&t_comment);
|
|
170 vorbis_info_clear(&v_info);
|
|
171 vorbis_comment_clear(&v_comment);
|
|
172 ogg_sync_clear(&o_sync);
|
|
173 file_in.close();
|
|
174 return;
|
|
175 }
|
|
176
|
|
177 // 5) init audio conversion
|
|
178 int ret = SDL_BuildAudioCVT(&cvt, AUDIO_S16, v_info.channels,
|
|
179 v_info.rate, audio_format, channels, rate);
|
|
180 if (-1 == ret) {
|
|
181 failbit = true;
|
|
182 return;
|
|
183 }
|
|
184 // play() will allocate memory when needed
|
|
185 audio_buffer_size = 0;
|
|
186 audio_buffer = NULL;
|
|
187
|
|
188 switch(video_format){
|
|
189 //pixel_format.set(r_offset,g_offset,b_offset,bpp not a_offset);
|
|
190 case VF_RGB:
|
|
191 pixel_format.set(0,1,2,3);
|
|
192 break;
|
|
193 case VF_BGR:
|
|
194 pixel_format.set(2,1,0,3);
|
|
195 break;
|
|
196 case VF_RGBA:
|
|
197 pixel_format.set(0,1,2,4);
|
|
198 break;
|
|
199 case VF_BGRA:
|
|
200 pixel_format.set(2,1,0,4);
|
|
201 break;
|
|
202 }
|
|
203 need_close = true;
|
|
204 }
|
|
205
|