Mercurial > hg > forks > bilotrip-mj12
annotate liboggplayer-src/src/open_close.cpp @ 7:26ee741f8c08
Add some primitive debugging in the liboggplayer.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 05 Aug 2013 14:33:35 +0300 |
parents | 105513a2e3c9 |
children | 3b6cea0d1980 |
rev | line source |
---|---|
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) { | |
7
26ee741f8c08
Add some primitive debugging in the liboggplayer.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
75 fprintf(stderr, "X: 0\n"); |
2 | 76 return false; |
77 } | |
78 if (theora_decode_header(&t_info, &t_comment, &o_packet)) { | |
7
26ee741f8c08
Add some primitive debugging in the liboggplayer.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
79 fprintf(stderr, "X: 1\n"); |
2 | 80 return false; |
81 } | |
82 theora_p++; | |
83 } | |
84 | |
85 // look for more vorbis header packets | |
86 while (vorbis_p && (vorbis_p < 3) && (ret = ogg_stream_packetout( | |
87 &o_vsstate, &o_packet))) { | |
88 if (ret < 0) { | |
7
26ee741f8c08
Add some primitive debugging in the liboggplayer.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
89 fprintf(stderr, "X: 2\n"); |
2 | 90 return false; |
91 } | |
92 if (vorbis_synthesis_headerin(&v_info, &v_comment, &o_packet)) { | |
7
26ee741f8c08
Add some primitive debugging in the liboggplayer.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
93 fprintf(stderr, "X: 3\n"); |
2 | 94 return false; |
95 } | |
96 vorbis_p++; | |
97 if (vorbis_p == 3) | |
98 break; | |
99 } | |
100 | |
101 // The header pages/packets will arrive before anything else we | |
102 // care about, or the stream is not obeying spec | |
103 | |
104 if (ogg_sync_pageout(&o_sync, &o_page) > 0) { | |
105 queue_page(&o_page); /* demux into the appropriate stream */ | |
106 } else { | |
107 if (!buffer_data()) /* someone needs more data */{ | |
7
26ee741f8c08
Add some primitive debugging in the liboggplayer.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
108 fprintf(stderr, "X: 4\n"); |
2 | 109 return false; |
110 } | |
111 } | |
112 } | |
113 return true; | |
114 | |
115 } | |
116 | |
117 bool OggPlayer::Imp::init_decoders() { | |
118 if (theora_p) { | |
119 theora_decode_init(&t_state, &t_info); | |
120 theora_control(&t_state, TH_DECCTL_GET_PPLEVEL_MAX, &pp_level_max, | |
121 sizeof(pp_level_max)); | |
122 pp_level = pp_level_max; | |
123 theora_control(&t_state, TH_DECCTL_SET_PPLEVEL, &pp_level, | |
124 sizeof(pp_level)); | |
125 pp_inc = 0; | |
126 } else { | |
7
26ee741f8c08
Add some primitive debugging in the liboggplayer.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
127 fprintf(stderr, "DDD: 0\n"); |
2 | 128 return false; |
129 } | |
130 if (vorbis_p) { | |
131 vorbis_synthesis_init(&v_state, &v_info); | |
132 vorbis_block_init(&v_state, &v_block); | |
133 } else { | |
7
26ee741f8c08
Add some primitive debugging in the liboggplayer.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
134 fprintf(stderr, "DDD: 1\n"); |
2 | 135 return false; |
136 } | |
137 return true; | |
138 } | |
139 | |
140 void OggPlayer::Imp::open(std::string path, AudioFormat audio_format, int channels, | |
141 int rate, VideoFormat video_format) { | |
142 | |
143 theora_p = 0; | |
144 vorbis_p = 0; | |
145 videobuf_ready = false; | |
146 frame=0; | |
147 last_frame_read=0; | |
148 | |
149 // 1) open the input file | |
150 file_in.open(path.c_str(), std::ios::binary); | |
151 if (file_in.fail()) { | |
7
26ee741f8c08
Add some primitive debugging in the liboggplayer.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
152 fprintf(stderr, "XXX: 0\n"); |
2 | 153 failbit = true; |
154 return; | |
155 } | |
156 | |
157 // 2) init some structs | |
158 ogg_sync_init(&o_sync); | |
159 vorbis_info_init(&v_info); | |
160 vorbis_comment_init(&v_comment); | |
161 theora_info_init(&t_info); | |
162 theora_comment_init(&t_comment); | |
163 | |
164 // 3) parse headers | |
165 if (!failbit && !parse_headers()) { | |
7
26ee741f8c08
Add some primitive debugging in the liboggplayer.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
166 fprintf(stderr, "XXX: 1\n"); |
2 | 167 failbit = true; |
168 } | |
169 | |
170 // 3) init decoders | |
171 if (!failbit && !init_decoders()) { | |
7
26ee741f8c08
Add some primitive debugging in the liboggplayer.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
172 fprintf(stderr, "XXX: 2\n"); |
2 | 173 failbit = true; |
174 } | |
175 | |
176 // 4) if fail, tear down the partial setup | |
177 if (failbit) { | |
178 theora_info_clear(&t_info); | |
179 theora_comment_clear(&t_comment); | |
180 vorbis_info_clear(&v_info); | |
181 vorbis_comment_clear(&v_comment); | |
182 ogg_sync_clear(&o_sync); | |
183 file_in.close(); | |
7
26ee741f8c08
Add some primitive debugging in the liboggplayer.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
184 fprintf(stderr, "XXX: done\n"); |
2 | 185 return; |
186 } | |
187 | |
188 // 5) init audio conversion | |
189 int ret = SDL_BuildAudioCVT(&cvt, AUDIO_S16, v_info.channels, | |
190 v_info.rate, audio_format, channels, rate); | |
191 if (-1 == ret) { | |
192 failbit = true; | |
193 return; | |
194 } | |
195 // play() will allocate memory when needed | |
196 audio_buffer_size = 0; | |
197 audio_buffer = NULL; | |
198 | |
199 switch(video_format){ | |
200 //pixel_format.set(r_offset,g_offset,b_offset,bpp not a_offset); | |
201 case VF_RGB: | |
202 pixel_format.set(0,1,2,3); | |
203 break; | |
204 case VF_BGR: | |
205 pixel_format.set(2,1,0,3); | |
206 break; | |
207 case VF_RGBA: | |
208 pixel_format.set(0,1,2,4); | |
209 break; | |
210 case VF_BGRA: | |
211 pixel_format.set(2,1,0,4); | |
212 break; | |
213 } | |
214 need_close = true; | |
215 } | |
216 |