video_stream_theora.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  1. /*************************************************************************/
  2. /* video_stream_theora.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "video_stream_theora.h"
  31. #include "os/os.h"
  32. #include "project_settings.h"
  33. #include "thirdparty/misc/yuv2rgb.h"
  34. int VideoStreamPlaybackTheora::buffer_data() {
  35. char *buffer = ogg_sync_buffer(&oy, 4096);
  36. #ifdef THEORA_USE_THREAD_STREAMING
  37. int read;
  38. do {
  39. thread_sem->post();
  40. read = MIN(ring_buffer.data_left(), 4096);
  41. if (read) {
  42. ring_buffer.read((uint8_t *)buffer, read);
  43. ogg_sync_wrote(&oy, read);
  44. } else {
  45. OS::get_singleton()->delay_usec(100);
  46. }
  47. } while (read == 0);
  48. return read;
  49. #else
  50. int bytes = file->get_buffer((uint8_t *)buffer, 4096);
  51. ogg_sync_wrote(&oy, bytes);
  52. return (bytes);
  53. #endif
  54. }
  55. int VideoStreamPlaybackTheora::queue_page(ogg_page *page) {
  56. if (theora_p) {
  57. ogg_stream_pagein(&to, page);
  58. if (to.e_o_s)
  59. theora_eos = true;
  60. }
  61. if (vorbis_p) {
  62. ogg_stream_pagein(&vo, page);
  63. if (vo.e_o_s)
  64. vorbis_eos = true;
  65. }
  66. return 0;
  67. }
  68. void VideoStreamPlaybackTheora::video_write(void) {
  69. th_ycbcr_buffer yuv;
  70. th_decode_ycbcr_out(td, yuv);
  71. int pitch = 4;
  72. frame_data.resize(size.x * size.y * pitch);
  73. {
  74. PoolVector<uint8_t>::Write w = frame_data.write();
  75. char *dst = (char *)w.ptr();
  76. //uv_offset=(ti.pic_x/2)+(yuv[1].stride)*(ti.pic_y/2);
  77. if (px_fmt == TH_PF_444) {
  78. yuv444_2_rgb8888((uint8_t *)dst, (uint8_t *)yuv[0].data, (uint8_t *)yuv[1].data, (uint8_t *)yuv[2].data, size.x, size.y, yuv[0].stride, yuv[1].stride, size.x << 2, 0);
  79. } else if (px_fmt == TH_PF_422) {
  80. yuv422_2_rgb8888((uint8_t *)dst, (uint8_t *)yuv[0].data, (uint8_t *)yuv[1].data, (uint8_t *)yuv[2].data, size.x, size.y, yuv[0].stride, yuv[1].stride, size.x << 2, 0);
  81. } else if (px_fmt == TH_PF_420) {
  82. yuv420_2_rgb8888((uint8_t *)dst, (uint8_t *)yuv[0].data, (uint8_t *)yuv[2].data, (uint8_t *)yuv[1].data, size.x, size.y, yuv[0].stride, yuv[1].stride, size.x << 2, 0);
  83. };
  84. format = Image::FORMAT_RGBA8;
  85. }
  86. Ref<Image> img = memnew(Image(size.x, size.y, 0, Image::FORMAT_RGBA8, frame_data)); //zero copy image creation
  87. texture->set_data(img); //zero copy send to visual server
  88. frames_pending = 1;
  89. }
  90. void VideoStreamPlaybackTheora::clear() {
  91. if (!file)
  92. return;
  93. if (vorbis_p) {
  94. ogg_stream_clear(&vo);
  95. if (vorbis_p >= 3) {
  96. vorbis_block_clear(&vb);
  97. vorbis_dsp_clear(&vd);
  98. };
  99. vorbis_comment_clear(&vc);
  100. vorbis_info_clear(&vi);
  101. vorbis_p = 0;
  102. }
  103. if (theora_p) {
  104. ogg_stream_clear(&to);
  105. th_decode_free(td);
  106. th_comment_clear(&tc);
  107. th_info_clear(&ti);
  108. theora_p = 0;
  109. }
  110. ogg_sync_clear(&oy);
  111. #ifdef THEORA_USE_THREAD_STREAMING
  112. thread_exit = true;
  113. thread_sem->post(); //just in case
  114. Thread::wait_to_finish(thread);
  115. memdelete(thread);
  116. thread = NULL;
  117. ring_buffer.clear();
  118. #endif
  119. //file_name = "";
  120. theora_p = 0;
  121. vorbis_p = 0;
  122. videobuf_ready = 0;
  123. frames_pending = 0;
  124. videobuf_time = 0;
  125. theora_eos = false;
  126. vorbis_eos = false;
  127. if (file) {
  128. memdelete(file);
  129. }
  130. file = NULL;
  131. playing = false;
  132. };
  133. void VideoStreamPlaybackTheora::set_file(const String &p_file) {
  134. ERR_FAIL_COND(playing);
  135. ogg_packet op;
  136. th_setup_info *ts = NULL;
  137. file_name = p_file;
  138. if (file) {
  139. memdelete(file);
  140. }
  141. file = FileAccess::open(p_file, FileAccess::READ);
  142. ERR_FAIL_COND(!file);
  143. #ifdef THEORA_USE_THREAD_STREAMING
  144. thread_exit = false;
  145. thread_eof = false;
  146. //pre-fill buffer
  147. int to_read = ring_buffer.space_left();
  148. int read = file->get_buffer(read_buffer.ptr(), to_read);
  149. ring_buffer.write(read_buffer.ptr(), read);
  150. thread = Thread::create(_streaming_thread, this);
  151. #endif
  152. ogg_sync_init(&oy);
  153. /* init supporting Vorbis structures needed in header parsing */
  154. vorbis_info_init(&vi);
  155. vorbis_comment_init(&vc);
  156. /* init supporting Theora structures needed in header parsing */
  157. th_comment_init(&tc);
  158. th_info_init(&ti);
  159. theora_eos = false;
  160. vorbis_eos = false;
  161. /* Ogg file open; parse the headers */
  162. /* Only interested in Vorbis/Theora streams */
  163. int stateflag = 0;
  164. int audio_track_skip = audio_track;
  165. while (!stateflag) {
  166. int ret = buffer_data();
  167. if (ret == 0) break;
  168. while (ogg_sync_pageout(&oy, &og) > 0) {
  169. ogg_stream_state test;
  170. /* is this a mandated initial header? If not, stop parsing */
  171. if (!ogg_page_bos(&og)) {
  172. /* don't leak the page; get it into the appropriate stream */
  173. queue_page(&og);
  174. stateflag = 1;
  175. break;
  176. }
  177. ogg_stream_init(&test, ogg_page_serialno(&og));
  178. ogg_stream_pagein(&test, &og);
  179. ogg_stream_packetout(&test, &op);
  180. /* identify the codec: try theora */
  181. if (!theora_p && th_decode_headerin(&ti, &tc, &ts, &op) >= 0) {
  182. /* it is theora */
  183. copymem(&to, &test, sizeof(test));
  184. theora_p = 1;
  185. } else if (!vorbis_p && vorbis_synthesis_headerin(&vi, &vc, &op) >= 0) {
  186. /* it is vorbis */
  187. if (audio_track_skip) {
  188. vorbis_info_clear(&vi);
  189. vorbis_comment_clear(&vc);
  190. ogg_stream_clear(&test);
  191. vorbis_info_init(&vi);
  192. vorbis_comment_init(&vc);
  193. audio_track_skip--;
  194. } else {
  195. copymem(&vo, &test, sizeof(test));
  196. vorbis_p = 1;
  197. }
  198. } else {
  199. /* whatever it is, we don't care about it */
  200. ogg_stream_clear(&test);
  201. }
  202. }
  203. /* fall through to non-bos page parsing */
  204. }
  205. /* we're expecting more header packets. */
  206. while ((theora_p && theora_p < 3) || (vorbis_p && vorbis_p < 3)) {
  207. int ret;
  208. /* look for further theora headers */
  209. while (theora_p && (theora_p < 3) && (ret = ogg_stream_packetout(&to, &op))) {
  210. if (ret < 0) {
  211. fprintf(stderr, "Error parsing Theora stream headers; "
  212. "corrupt stream?\n");
  213. clear();
  214. return;
  215. }
  216. if (!th_decode_headerin(&ti, &tc, &ts, &op)) {
  217. fprintf(stderr, "Error parsing Theora stream headers; "
  218. "corrupt stream?\n");
  219. clear();
  220. return;
  221. }
  222. theora_p++;
  223. }
  224. /* look for more vorbis header packets */
  225. while (vorbis_p && (vorbis_p < 3) && (ret = ogg_stream_packetout(&vo, &op))) {
  226. if (ret < 0) {
  227. fprintf(stderr, "Error parsing Vorbis stream headers; corrupt stream?\n");
  228. clear();
  229. return;
  230. }
  231. ret = vorbis_synthesis_headerin(&vi, &vc, &op);
  232. if (ret) {
  233. fprintf(stderr, "Error parsing Vorbis stream headers; corrupt stream?\n");
  234. clear();
  235. return;
  236. }
  237. vorbis_p++;
  238. if (vorbis_p == 3) break;
  239. }
  240. /* The header pages/packets will arrive before anything else we
  241. care about, or the stream is not obeying spec */
  242. if (ogg_sync_pageout(&oy, &og) > 0) {
  243. queue_page(&og); /* demux into the appropriate stream */
  244. } else {
  245. int ret = buffer_data(); /* someone needs more data */
  246. if (ret == 0) {
  247. fprintf(stderr, "End of file while searching for codec headers.\n");
  248. clear();
  249. return;
  250. }
  251. }
  252. }
  253. /* and now we have it all. initialize decoders */
  254. if (theora_p) {
  255. td = th_decode_alloc(&ti, ts);
  256. printf("Ogg logical stream %lx is Theora %dx%d %.02f fps",
  257. to.serialno, ti.pic_width, ti.pic_height,
  258. (double)ti.fps_numerator / ti.fps_denominator);
  259. px_fmt = ti.pixel_fmt;
  260. switch (ti.pixel_fmt) {
  261. case TH_PF_420: printf(" 4:2:0 video\n"); break;
  262. case TH_PF_422: printf(" 4:2:2 video\n"); break;
  263. case TH_PF_444: printf(" 4:4:4 video\n"); break;
  264. case TH_PF_RSVD:
  265. default:
  266. printf(" video\n (UNKNOWN Chroma sampling!)\n");
  267. break;
  268. }
  269. if (ti.pic_width != ti.frame_width || ti.pic_height != ti.frame_height)
  270. printf(" Frame content is %dx%d with offset (%d,%d).\n",
  271. ti.frame_width, ti.frame_height, ti.pic_x, ti.pic_y);
  272. th_decode_ctl(td, TH_DECCTL_GET_PPLEVEL_MAX, &pp_level_max,
  273. sizeof(pp_level_max));
  274. pp_level = 0;
  275. th_decode_ctl(td, TH_DECCTL_SET_PPLEVEL, &pp_level, sizeof(pp_level));
  276. pp_inc = 0;
  277. int w;
  278. int h;
  279. w = (ti.pic_x + ti.frame_width + 1 & ~1) - (ti.pic_x & ~1);
  280. h = (ti.pic_y + ti.frame_height + 1 & ~1) - (ti.pic_y & ~1);
  281. size.x = w;
  282. size.y = h;
  283. texture->create(w, h, Image::FORMAT_RGBA8, Texture::FLAG_FILTER | Texture::FLAG_VIDEO_SURFACE);
  284. } else {
  285. /* tear down the partial theora setup */
  286. th_info_clear(&ti);
  287. th_comment_clear(&tc);
  288. }
  289. th_setup_free(ts);
  290. if (vorbis_p) {
  291. vorbis_synthesis_init(&vd, &vi);
  292. vorbis_block_init(&vd, &vb);
  293. fprintf(stderr, "Ogg logical stream %lx is Vorbis %d channel %ld Hz audio.\n",
  294. vo.serialno, vi.channels, vi.rate);
  295. //_setup(vi.channels, vi.rate);
  296. } else {
  297. /* tear down the partial vorbis setup */
  298. vorbis_info_clear(&vi);
  299. vorbis_comment_clear(&vc);
  300. }
  301. playing = false;
  302. buffering = true;
  303. time = 0;
  304. audio_frames_wrote = 0;
  305. };
  306. float VideoStreamPlaybackTheora::get_time() const {
  307. return time - AudioServer::get_singleton()->get_output_delay() - delay_compensation; //-((get_total())/(float)vi.rate);
  308. };
  309. Ref<Texture> VideoStreamPlaybackTheora::get_texture() {
  310. return texture;
  311. }
  312. void VideoStreamPlaybackTheora::update(float p_delta) {
  313. if (!file)
  314. return;
  315. if (!playing || paused) {
  316. //printf("not playing\n");
  317. return;
  318. };
  319. #ifdef THEORA_USE_THREAD_STREAMING
  320. thread_sem->post();
  321. #endif
  322. //print_line("play "+rtos(p_delta));
  323. time += p_delta;
  324. if (videobuf_time > get_time()) {
  325. return; //no new frames need to be produced
  326. }
  327. bool frame_done = false;
  328. bool audio_done = !vorbis_p;
  329. while (!frame_done || (!audio_done && !vorbis_eos)) {
  330. //a frame needs to be produced
  331. ogg_packet op;
  332. bool no_theora = false;
  333. while (vorbis_p) {
  334. int ret;
  335. float **pcm;
  336. bool buffer_full = false;
  337. /* if there's pending, decoded audio, grab it */
  338. ret = vorbis_synthesis_pcmout(&vd, &pcm);
  339. if (ret > 0) {
  340. const int AUXBUF_LEN = 4096;
  341. int to_read = ret;
  342. int16_t aux_buffer[AUXBUF_LEN];
  343. while (to_read) {
  344. int m = MIN(AUXBUF_LEN / vi.channels, to_read);
  345. int count = 0;
  346. for (int j = 0; j < m; j++) {
  347. for (int i = 0; i < vi.channels; i++) {
  348. int val = Math::fast_ftoi(pcm[i][j] * 32767.f);
  349. if (val > 32767) val = 32767;
  350. if (val < -32768) val = -32768;
  351. aux_buffer[count++] = val;
  352. }
  353. }
  354. if (mix_callback) {
  355. int mixed = mix_callback(mix_udata, aux_buffer, m);
  356. to_read -= mixed;
  357. if (mixed != m) { //could mix no more
  358. buffer_full = true;
  359. break;
  360. }
  361. } else {
  362. to_read -= m; //just pretend we sent the audio
  363. }
  364. }
  365. int tr = vorbis_synthesis_read(&vd, ret - to_read);
  366. if (vd.granulepos >= 0) {
  367. //print_line("wrote: "+itos(audio_frames_wrote)+" gpos: "+itos(vd.granulepos));
  368. }
  369. //print_line("mix audio!");
  370. audio_frames_wrote += ret - to_read;
  371. //print_line("AGP: "+itos(vd.granulepos)+" added "+itos(ret-to_read));
  372. } else {
  373. /* no pending audio; is there a pending packet to decode? */
  374. if (ogg_stream_packetout(&vo, &op) > 0) {
  375. if (vorbis_synthesis(&vb, &op) == 0) { /* test for success! */
  376. vorbis_synthesis_blockin(&vd, &vb);
  377. }
  378. } else { /* we need more data; break out to suck in another page */
  379. //printf("need moar data\n");
  380. break;
  381. };
  382. }
  383. audio_done = videobuf_time < (audio_frames_wrote / float(vi.rate));
  384. if (buffer_full)
  385. break;
  386. }
  387. while (theora_p && !frame_done) {
  388. /* theora is one in, one out... */
  389. if (ogg_stream_packetout(&to, &op) > 0) {
  390. if (false && pp_inc) {
  391. pp_level += pp_inc;
  392. th_decode_ctl(td, TH_DECCTL_SET_PPLEVEL, &pp_level,
  393. sizeof(pp_level));
  394. pp_inc = 0;
  395. }
  396. /*HACK: This should be set after a seek or a gap, but we might not have
  397. a granulepos for the first packet (we only have them for the last
  398. packet on a page), so we just set it as often as we get it.
  399. To do this right, we should back-track from the last packet on the
  400. page and compute the correct granulepos for the first packet after
  401. a seek or a gap.*/
  402. if (op.granulepos >= 0) {
  403. th_decode_ctl(td, TH_DECCTL_SET_GRANPOS, &op.granulepos,
  404. sizeof(op.granulepos));
  405. }
  406. ogg_int64_t videobuf_granulepos;
  407. if (th_decode_packetin(td, &op, &videobuf_granulepos) == 0) {
  408. videobuf_time = th_granule_time(td, videobuf_granulepos);
  409. //printf("frame time %f, play time %f, ready %i\n", (float)videobuf_time, get_time(), videobuf_ready);
  410. /* is it already too old to be useful? This is only actually
  411. useful cosmetically after a SIGSTOP. Note that we have to
  412. decode the frame even if we don't show it (for now) due to
  413. keyframing. Soon enough libtheora will be able to deal
  414. with non-keyframe seeks. */
  415. if (videobuf_time >= get_time()) {
  416. frame_done = true;
  417. } else {
  418. /*If we are too slow, reduce the pp level.*/
  419. pp_inc = pp_level > 0 ? -1 : 0;
  420. }
  421. } else {
  422. }
  423. } else {
  424. no_theora = true;
  425. break;
  426. }
  427. }
  428. #ifdef THEORA_USE_THREAD_STREAMING
  429. if (file && thread_eof && no_theora && theora_eos && ring_buffer.data_left() == 0) {
  430. #else
  431. if (file && /*!videobuf_ready && */ no_theora && theora_eos) {
  432. #endif
  433. printf("video done, stopping\n");
  434. stop();
  435. return;
  436. };
  437. if (!frame_done || !audio_done) {
  438. //what's the point of waiting for audio to grab a page?
  439. buffer_data();
  440. while (ogg_sync_pageout(&oy, &og) > 0) {
  441. queue_page(&og);
  442. }
  443. }
  444. /* If playback has begun, top audio buffer off immediately. */
  445. //if(stateflag) audio_write_nonblocking();
  446. /* are we at or past time for this video frame? */
  447. if (videobuf_ready && videobuf_time <= get_time()) {
  448. //video_write();
  449. //videobuf_ready=0;
  450. } else {
  451. //printf("frame at %f not ready (time %f), ready %i\n", (float)videobuf_time, get_time(), videobuf_ready);
  452. }
  453. float tdiff = videobuf_time - get_time();
  454. /*If we have lots of extra time, increase the post-processing level.*/
  455. if (tdiff > ti.fps_denominator * 0.25 / ti.fps_numerator) {
  456. pp_inc = pp_level < pp_level_max ? 1 : 0;
  457. } else if (tdiff < ti.fps_denominator * 0.05 / ti.fps_numerator) {
  458. pp_inc = pp_level > 0 ? -1 : 0;
  459. }
  460. }
  461. video_write();
  462. };
  463. void VideoStreamPlaybackTheora::play() {
  464. if (!playing)
  465. time = 0;
  466. else {
  467. stop();
  468. }
  469. playing = true;
  470. delay_compensation = ProjectSettings::get_singleton()->get("audio/video_delay_compensation_ms");
  471. delay_compensation /= 1000.0;
  472. };
  473. void VideoStreamPlaybackTheora::stop() {
  474. if (playing) {
  475. clear();
  476. set_file(file_name); //reset
  477. }
  478. playing = false;
  479. time = 0;
  480. };
  481. bool VideoStreamPlaybackTheora::is_playing() const {
  482. return playing;
  483. };
  484. void VideoStreamPlaybackTheora::set_paused(bool p_paused) {
  485. paused = p_paused;
  486. //pau = !p_paused;
  487. };
  488. bool VideoStreamPlaybackTheora::is_paused(bool p_paused) const {
  489. return paused;
  490. };
  491. void VideoStreamPlaybackTheora::set_loop(bool p_enable){
  492. };
  493. bool VideoStreamPlaybackTheora::has_loop() const {
  494. return false;
  495. };
  496. float VideoStreamPlaybackTheora::get_length() const {
  497. return 0;
  498. };
  499. String VideoStreamPlaybackTheora::get_stream_name() const {
  500. return "";
  501. };
  502. int VideoStreamPlaybackTheora::get_loop_count() const {
  503. return 0;
  504. };
  505. float VideoStreamPlaybackTheora::get_playback_position() const {
  506. return get_time();
  507. };
  508. void VideoStreamPlaybackTheora::seek(float p_time){
  509. // no
  510. };
  511. void VideoStreamPlaybackTheora::set_mix_callback(AudioMixCallback p_callback, void *p_userdata) {
  512. mix_callback = p_callback;
  513. mix_udata = p_userdata;
  514. }
  515. int VideoStreamPlaybackTheora::get_channels() const {
  516. return vi.channels;
  517. }
  518. void VideoStreamPlaybackTheora::set_audio_track(int p_idx) {
  519. audio_track = p_idx;
  520. }
  521. int VideoStreamPlaybackTheora::get_mix_rate() const {
  522. return vi.rate;
  523. }
  524. #ifdef THEORA_USE_THREAD_STREAMING
  525. void VideoStreamPlaybackTheora::_streaming_thread(void *ud) {
  526. VideoStreamPlaybackTheora *vs = (VideoStreamPlaybackTheora *)ud;
  527. while (!vs->thread_exit) {
  528. //just fill back the buffer
  529. if (!vs->thread_eof) {
  530. int to_read = vs->ring_buffer.space_left();
  531. if (to_read) {
  532. int read = vs->file->get_buffer(vs->read_buffer.ptr(), to_read);
  533. vs->ring_buffer.write(vs->read_buffer.ptr(), read);
  534. vs->thread_eof = vs->file->eof_reached();
  535. }
  536. }
  537. vs->thread_sem->wait();
  538. }
  539. }
  540. #endif
  541. VideoStreamPlaybackTheora::VideoStreamPlaybackTheora() {
  542. file = NULL;
  543. theora_p = 0;
  544. vorbis_p = 0;
  545. videobuf_ready = 0;
  546. playing = false;
  547. frames_pending = 0;
  548. videobuf_time = 0;
  549. paused = false;
  550. buffering = false;
  551. texture = Ref<ImageTexture>(memnew(ImageTexture));
  552. mix_callback = NULL;
  553. mix_udata = NULL;
  554. audio_track = 0;
  555. delay_compensation = 0;
  556. audio_frames_wrote = 0;
  557. #ifdef THEORA_USE_THREAD_STREAMING
  558. int rb_power = nearest_shift(RB_SIZE_KB * 1024);
  559. ring_buffer.resize(rb_power);
  560. read_buffer.resize(RB_SIZE_KB * 1024);
  561. thread_sem = Semaphore::create();
  562. thread = NULL;
  563. thread_exit = false;
  564. thread_eof = false;
  565. #endif
  566. };
  567. VideoStreamPlaybackTheora::~VideoStreamPlaybackTheora() {
  568. #ifdef THEORA_USE_THREAD_STREAMING
  569. memdelete(thread_sem);
  570. #endif
  571. clear();
  572. if (file)
  573. memdelete(file);
  574. };
  575. RES ResourceFormatLoaderVideoStreamTheora::load(const String &p_path, const String &p_original_path, Error *r_error) {
  576. if (r_error)
  577. *r_error = ERR_FILE_CANT_OPEN;
  578. VideoStreamTheora *stream = memnew(VideoStreamTheora);
  579. stream->set_file(p_path);
  580. if (r_error)
  581. *r_error = OK;
  582. return Ref<VideoStreamTheora>(stream);
  583. }
  584. void ResourceFormatLoaderVideoStreamTheora::get_recognized_extensions(List<String> *p_extensions) const {
  585. p_extensions->push_back("ogm");
  586. p_extensions->push_back("ogv");
  587. }
  588. bool ResourceFormatLoaderVideoStreamTheora::handles_type(const String &p_type) const {
  589. return (p_type == "VideoStream" || p_type == "VideoStreamTheora");
  590. }
  591. String ResourceFormatLoaderVideoStreamTheora::get_resource_type(const String &p_path) const {
  592. String exl = p_path.get_extension().to_lower();
  593. if (exl == "ogm" || exl == "ogv")
  594. return "VideoStreamTheora";
  595. return "";
  596. }