video_stream_theora.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. /**************************************************************************/
  2. /* video_stream_theora.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 "core/config/project_settings.h"
  32. #include "core/io/image.h"
  33. #include "core/os/os.h"
  34. #include "scene/resources/image_texture.h"
  35. #ifdef _MSC_VER
  36. #pragma warning(push)
  37. #pragma warning(disable : 4127)
  38. #endif
  39. #include "thirdparty/misc/yuv2rgb.h"
  40. #ifdef _MSC_VER
  41. #pragma warning(pop)
  42. #endif
  43. int VideoStreamPlaybackTheora::buffer_data() {
  44. char *buffer = ogg_sync_buffer(&oy, 4096);
  45. #ifdef THEORA_USE_THREAD_STREAMING
  46. int read;
  47. do {
  48. thread_sem->post();
  49. read = MIN(ring_buffer.data_left(), 4096);
  50. if (read) {
  51. ring_buffer.read((uint8_t *)buffer, read);
  52. ogg_sync_wrote(&oy, read);
  53. } else {
  54. OS::get_singleton()->delay_usec(100);
  55. }
  56. } while (read == 0);
  57. return read;
  58. #else
  59. uint64_t bytes = file->get_buffer((uint8_t *)buffer, 4096);
  60. ogg_sync_wrote(&oy, bytes);
  61. return (bytes);
  62. #endif
  63. }
  64. int VideoStreamPlaybackTheora::queue_page(ogg_page *page) {
  65. if (theora_p) {
  66. ogg_stream_pagein(&to, page);
  67. if (to.e_o_s) {
  68. theora_eos = true;
  69. }
  70. }
  71. if (vorbis_p) {
  72. ogg_stream_pagein(&vo, page);
  73. if (vo.e_o_s) {
  74. vorbis_eos = true;
  75. }
  76. }
  77. return 0;
  78. }
  79. void VideoStreamPlaybackTheora::video_write() {
  80. th_ycbcr_buffer yuv;
  81. th_decode_ycbcr_out(td, yuv);
  82. int pitch = 4;
  83. frame_data.resize(size.x * size.y * pitch);
  84. {
  85. uint8_t *w = frame_data.ptrw();
  86. char *dst = (char *)w;
  87. if (px_fmt == TH_PF_444) {
  88. 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);
  89. } else if (px_fmt == TH_PF_422) {
  90. 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);
  91. } else if (px_fmt == TH_PF_420) {
  92. yuv420_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);
  93. }
  94. format = Image::FORMAT_RGBA8;
  95. }
  96. Ref<Image> img = memnew(Image(size.x, size.y, false, Image::FORMAT_RGBA8, frame_data)); //zero copy image creation
  97. texture->update(img); //zero copy send to rendering server
  98. frames_pending = 1;
  99. }
  100. void VideoStreamPlaybackTheora::clear() {
  101. if (file.is_null()) {
  102. return;
  103. }
  104. if (vorbis_p) {
  105. ogg_stream_clear(&vo);
  106. if (vorbis_p >= 3) {
  107. vorbis_block_clear(&vb);
  108. vorbis_dsp_clear(&vd);
  109. }
  110. vorbis_comment_clear(&vc);
  111. vorbis_info_clear(&vi);
  112. vorbis_p = 0;
  113. }
  114. if (theora_p) {
  115. ogg_stream_clear(&to);
  116. th_decode_free(td);
  117. th_comment_clear(&tc);
  118. th_info_clear(&ti);
  119. theora_p = 0;
  120. }
  121. ogg_sync_clear(&oy);
  122. #ifdef THEORA_USE_THREAD_STREAMING
  123. thread_exit = true;
  124. thread_sem->post(); //just in case
  125. thread.wait_to_finish();
  126. ring_buffer.clear();
  127. #endif
  128. theora_p = 0;
  129. vorbis_p = 0;
  130. videobuf_ready = 0;
  131. frames_pending = 0;
  132. videobuf_time = 0;
  133. theora_eos = false;
  134. vorbis_eos = false;
  135. file.unref();
  136. playing = false;
  137. }
  138. void VideoStreamPlaybackTheora::set_file(const String &p_file) {
  139. ERR_FAIL_COND(playing);
  140. ogg_packet op;
  141. th_setup_info *ts = nullptr;
  142. file_name = p_file;
  143. file = FileAccess::open(p_file, FileAccess::READ);
  144. ERR_FAIL_COND_MSG(file.is_null(), "Cannot open file '" + p_file + "'.");
  145. #ifdef THEORA_USE_THREAD_STREAMING
  146. thread_exit = false;
  147. thread_eof = false;
  148. //pre-fill buffer
  149. int to_read = ring_buffer.space_left();
  150. uint64_t read = file->get_buffer(read_buffer.ptr(), to_read);
  151. ring_buffer.write(read_buffer.ptr(), read);
  152. thread.start(_streaming_thread, this);
  153. #endif
  154. ogg_sync_init(&oy);
  155. /* init supporting Vorbis structures needed in header parsing */
  156. vorbis_info_init(&vi);
  157. vorbis_comment_init(&vc);
  158. /* init supporting Theora structures needed in header parsing */
  159. th_comment_init(&tc);
  160. th_info_init(&ti);
  161. theora_eos = false;
  162. vorbis_eos = false;
  163. /* Ogg file open; parse the headers */
  164. /* Only interested in Vorbis/Theora streams */
  165. int stateflag = 0;
  166. int audio_track_skip = audio_track;
  167. while (!stateflag) {
  168. int ret = buffer_data();
  169. if (ret == 0) {
  170. break;
  171. }
  172. while (ogg_sync_pageout(&oy, &og) > 0) {
  173. ogg_stream_state test;
  174. /* is this a mandated initial header? If not, stop parsing */
  175. if (!ogg_page_bos(&og)) {
  176. /* don't leak the page; get it into the appropriate stream */
  177. queue_page(&og);
  178. stateflag = 1;
  179. break;
  180. }
  181. ogg_stream_init(&test, ogg_page_serialno(&og));
  182. ogg_stream_pagein(&test, &og);
  183. ogg_stream_packetout(&test, &op);
  184. /* identify the codec: try theora */
  185. if (!theora_p && th_decode_headerin(&ti, &tc, &ts, &op) >= 0) {
  186. /* it is theora */
  187. memcpy(&to, &test, sizeof(test));
  188. theora_p = 1;
  189. } else if (!vorbis_p && vorbis_synthesis_headerin(&vi, &vc, &op) >= 0) {
  190. /* it is vorbis */
  191. if (audio_track_skip) {
  192. vorbis_info_clear(&vi);
  193. vorbis_comment_clear(&vc);
  194. ogg_stream_clear(&test);
  195. vorbis_info_init(&vi);
  196. vorbis_comment_init(&vc);
  197. audio_track_skip--;
  198. } else {
  199. memcpy(&vo, &test, sizeof(test));
  200. vorbis_p = 1;
  201. }
  202. } else {
  203. /* whatever it is, we don't care about it */
  204. ogg_stream_clear(&test);
  205. }
  206. }
  207. /* fall through to non-bos page parsing */
  208. }
  209. /* we're expecting more header packets. */
  210. while ((theora_p && theora_p < 3) || (vorbis_p && vorbis_p < 3)) {
  211. int ret = 0;
  212. /* look for further theora headers */
  213. if (theora_p && theora_p < 3) {
  214. ret = ogg_stream_packetout(&to, &op);
  215. }
  216. while (theora_p && theora_p < 3 && ret) {
  217. if (ret < 0) {
  218. fprintf(stderr, "Error parsing Theora stream headers; corrupt stream?\n");
  219. clear();
  220. return;
  221. }
  222. if (!th_decode_headerin(&ti, &tc, &ts, &op)) {
  223. fprintf(stderr, "Error parsing Theora stream headers; corrupt stream?\n");
  224. clear();
  225. return;
  226. }
  227. ret = ogg_stream_packetout(&to, &op);
  228. theora_p++;
  229. }
  230. /* look for more vorbis header packets */
  231. if (vorbis_p && vorbis_p < 3) {
  232. ret = ogg_stream_packetout(&vo, &op);
  233. }
  234. while (vorbis_p && vorbis_p < 3 && ret) {
  235. if (ret < 0) {
  236. fprintf(stderr, "Error parsing Vorbis stream headers; corrupt stream?\n");
  237. clear();
  238. return;
  239. }
  240. ret = vorbis_synthesis_headerin(&vi, &vc, &op);
  241. if (ret) {
  242. fprintf(stderr, "Error parsing Vorbis stream headers; corrupt stream?\n");
  243. clear();
  244. return;
  245. }
  246. vorbis_p++;
  247. if (vorbis_p == 3) {
  248. break;
  249. }
  250. ret = ogg_stream_packetout(&vo, &op);
  251. }
  252. /* The header pages/packets will arrive before anything else we
  253. care about, or the stream is not obeying spec */
  254. if (ogg_sync_pageout(&oy, &og) > 0) {
  255. queue_page(&og); /* demux into the appropriate stream */
  256. } else {
  257. int ret2 = buffer_data(); /* someone needs more data */
  258. if (ret2 == 0) {
  259. fprintf(stderr, "End of file while searching for codec headers.\n");
  260. clear();
  261. return;
  262. }
  263. }
  264. }
  265. /* And now we have it all. Initialize decoders. */
  266. if (theora_p) {
  267. td = th_decode_alloc(&ti, ts);
  268. px_fmt = ti.pixel_fmt;
  269. switch (ti.pixel_fmt) {
  270. case TH_PF_420:
  271. //printf(" 4:2:0 video\n");
  272. break;
  273. case TH_PF_422:
  274. //printf(" 4:2:2 video\n");
  275. break;
  276. case TH_PF_444:
  277. //printf(" 4:4:4 video\n");
  278. break;
  279. case TH_PF_RSVD:
  280. default:
  281. printf(" video\n (UNKNOWN Chroma sampling!)\n");
  282. break;
  283. }
  284. th_decode_ctl(td, TH_DECCTL_GET_PPLEVEL_MAX, &pp_level_max,
  285. sizeof(pp_level_max));
  286. pp_level = 0;
  287. th_decode_ctl(td, TH_DECCTL_SET_PPLEVEL, &pp_level, sizeof(pp_level));
  288. pp_inc = 0;
  289. int w;
  290. int h;
  291. w = ((ti.pic_x + ti.frame_width + 1) & ~1) - (ti.pic_x & ~1);
  292. h = ((ti.pic_y + ti.frame_height + 1) & ~1) - (ti.pic_y & ~1);
  293. size.x = w;
  294. size.y = h;
  295. Ref<Image> img = Image::create_empty(w, h, false, Image::FORMAT_RGBA8);
  296. texture->set_image(img);
  297. } else {
  298. /* tear down the partial theora setup */
  299. th_info_clear(&ti);
  300. th_comment_clear(&tc);
  301. }
  302. th_setup_free(ts);
  303. if (vorbis_p) {
  304. vorbis_synthesis_init(&vd, &vi);
  305. vorbis_block_init(&vd, &vb);
  306. //_setup(vi.channels, vi.rate);
  307. } else {
  308. /* tear down the partial vorbis setup */
  309. vorbis_info_clear(&vi);
  310. vorbis_comment_clear(&vc);
  311. }
  312. playing = false;
  313. buffering = true;
  314. time = 0;
  315. audio_frames_wrote = 0;
  316. }
  317. double VideoStreamPlaybackTheora::get_time() const {
  318. // FIXME: AudioServer output latency was fixed in af9bb0e, previously it used to
  319. // systematically return 0. Now that it gives a proper latency, it broke this
  320. // code where the delay compensation likely never really worked.
  321. return time - /* AudioServer::get_singleton()->get_output_latency() - */ delay_compensation;
  322. }
  323. Ref<Texture2D> VideoStreamPlaybackTheora::get_texture() const {
  324. return texture;
  325. }
  326. void VideoStreamPlaybackTheora::update(double p_delta) {
  327. if (file.is_null()) {
  328. return;
  329. }
  330. if (!playing || paused) {
  331. //printf("not playing\n");
  332. return;
  333. }
  334. #ifdef THEORA_USE_THREAD_STREAMING
  335. thread_sem->post();
  336. #endif
  337. time += p_delta;
  338. if (videobuf_time > get_time()) {
  339. return; //no new frames need to be produced
  340. }
  341. bool frame_done = false;
  342. bool audio_done = !vorbis_p;
  343. while (!frame_done || (!audio_done && !vorbis_eos)) {
  344. //a frame needs to be produced
  345. ogg_packet op;
  346. bool no_theora = false;
  347. bool buffer_full = false;
  348. while (vorbis_p && !audio_done && !buffer_full) {
  349. int ret;
  350. float **pcm;
  351. /* if there's pending, decoded audio, grab it */
  352. ret = vorbis_synthesis_pcmout(&vd, &pcm);
  353. if (ret > 0) {
  354. const int AUXBUF_LEN = 4096;
  355. int to_read = ret;
  356. float aux_buffer[AUXBUF_LEN];
  357. while (to_read) {
  358. int m = MIN(AUXBUF_LEN / vi.channels, to_read);
  359. int count = 0;
  360. for (int j = 0; j < m; j++) {
  361. for (int i = 0; i < vi.channels; i++) {
  362. aux_buffer[count++] = pcm[i][j];
  363. }
  364. }
  365. if (mix_callback) {
  366. int mixed = mix_callback(mix_udata, aux_buffer, m);
  367. to_read -= mixed;
  368. if (mixed != m) { //could mix no more
  369. buffer_full = true;
  370. break;
  371. }
  372. } else {
  373. to_read -= m; //just pretend we sent the audio
  374. }
  375. }
  376. vorbis_synthesis_read(&vd, ret - to_read);
  377. audio_frames_wrote += ret - to_read;
  378. } else {
  379. /* no pending audio; is there a pending packet to decode? */
  380. if (ogg_stream_packetout(&vo, &op) > 0) {
  381. if (vorbis_synthesis(&vb, &op) == 0) { /* test for success! */
  382. vorbis_synthesis_blockin(&vd, &vb);
  383. }
  384. } else { /* we need more data; break out to suck in another page */
  385. break;
  386. }
  387. }
  388. audio_done = videobuf_time < (audio_frames_wrote / float(vi.rate));
  389. if (buffer_full) {
  390. break;
  391. }
  392. }
  393. while (theora_p && !frame_done) {
  394. /* theora is one in, one out... */
  395. if (ogg_stream_packetout(&to, &op) > 0) {
  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. }
  422. } else {
  423. no_theora = true;
  424. break;
  425. }
  426. }
  427. #ifdef THEORA_USE_THREAD_STREAMING
  428. if (file.is_valid() && thread_eof && no_theora && theora_eos && ring_buffer.data_left() == 0) {
  429. #else
  430. if (file.is_valid() && /*!videobuf_ready && */ no_theora && theora_eos) {
  431. #endif
  432. //printf("video done, stopping\n");
  433. stop();
  434. return;
  435. }
  436. if (!frame_done || !audio_done) {
  437. //what's the point of waiting for audio to grab a page?
  438. buffer_data();
  439. while (ogg_sync_pageout(&oy, &og) > 0) {
  440. queue_page(&og);
  441. }
  442. }
  443. /* If playback has begun, top audio buffer off immediately. */
  444. //if(stateflag) audio_write_nonblocking();
  445. /* are we at or past time for this video frame? */
  446. if (videobuf_ready && videobuf_time <= get_time()) {
  447. //video_write();
  448. //videobuf_ready=0;
  449. } else {
  450. //printf("frame at %f not ready (time %f), ready %i\n", (float)videobuf_time, get_time(), videobuf_ready);
  451. }
  452. double tdiff = videobuf_time - get_time();
  453. /*If we have lots of extra time, increase the post-processing level.*/
  454. if (tdiff > ti.fps_denominator * 0.25 / ti.fps_numerator) {
  455. pp_inc = pp_level < pp_level_max ? 1 : 0;
  456. } else if (tdiff < ti.fps_denominator * 0.05 / ti.fps_numerator) {
  457. pp_inc = pp_level > 0 ? -1 : 0;
  458. }
  459. }
  460. video_write();
  461. }
  462. void VideoStreamPlaybackTheora::play() {
  463. if (!playing) {
  464. time = 0;
  465. } else {
  466. stop();
  467. }
  468. playing = true;
  469. delay_compensation = GLOBAL_GET("audio/video/video_delay_compensation_ms");
  470. delay_compensation /= 1000.0;
  471. }
  472. void VideoStreamPlaybackTheora::stop() {
  473. if (playing) {
  474. clear();
  475. set_file(file_name); //reset
  476. }
  477. playing = false;
  478. time = 0;
  479. }
  480. bool VideoStreamPlaybackTheora::is_playing() const {
  481. return playing;
  482. }
  483. void VideoStreamPlaybackTheora::set_paused(bool p_paused) {
  484. paused = p_paused;
  485. }
  486. bool VideoStreamPlaybackTheora::is_paused() const {
  487. return paused;
  488. }
  489. double VideoStreamPlaybackTheora::get_length() const {
  490. return 0;
  491. }
  492. double VideoStreamPlaybackTheora::get_playback_position() const {
  493. return get_time();
  494. }
  495. void VideoStreamPlaybackTheora::seek(double p_time) {
  496. WARN_PRINT_ONCE("Seeking in Theora videos is not implemented yet (it's only supported for GDExtension-provided video streams).");
  497. }
  498. int VideoStreamPlaybackTheora::get_channels() const {
  499. return vi.channels;
  500. }
  501. void VideoStreamPlaybackTheora::set_audio_track(int p_idx) {
  502. audio_track = p_idx;
  503. }
  504. int VideoStreamPlaybackTheora::get_mix_rate() const {
  505. return vi.rate;
  506. }
  507. #ifdef THEORA_USE_THREAD_STREAMING
  508. void VideoStreamPlaybackTheora::_streaming_thread(void *ud) {
  509. VideoStreamPlaybackTheora *vs = static_cast<VideoStreamPlaybackTheora *>(ud);
  510. while (!vs->thread_exit) {
  511. //just fill back the buffer
  512. if (!vs->thread_eof) {
  513. int to_read = vs->ring_buffer.space_left();
  514. if (to_read > 0) {
  515. uint64_t read = vs->file->get_buffer(vs->read_buffer.ptr(), to_read);
  516. vs->ring_buffer.write(vs->read_buffer.ptr(), read);
  517. vs->thread_eof = vs->file->eof_reached();
  518. }
  519. }
  520. vs->thread_sem->wait();
  521. }
  522. }
  523. #endif
  524. VideoStreamPlaybackTheora::VideoStreamPlaybackTheora() {
  525. texture.instantiate();
  526. #ifdef THEORA_USE_THREAD_STREAMING
  527. int rb_power = nearest_shift(RB_SIZE_KB * 1024);
  528. ring_buffer.resize(rb_power);
  529. read_buffer.resize(RB_SIZE_KB * 1024);
  530. thread_sem = Semaphore::create();
  531. #endif
  532. }
  533. VideoStreamPlaybackTheora::~VideoStreamPlaybackTheora() {
  534. #ifdef THEORA_USE_THREAD_STREAMING
  535. memdelete(thread_sem);
  536. #endif
  537. clear();
  538. }
  539. void VideoStreamTheora::_bind_methods() {}
  540. Ref<Resource> ResourceFormatLoaderTheora::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode) {
  541. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
  542. if (f.is_null()) {
  543. if (r_error) {
  544. *r_error = ERR_CANT_OPEN;
  545. }
  546. return Ref<Resource>();
  547. }
  548. VideoStreamTheora *stream = memnew(VideoStreamTheora);
  549. stream->set_file(p_path);
  550. Ref<VideoStreamTheora> ogv_stream = Ref<VideoStreamTheora>(stream);
  551. if (r_error) {
  552. *r_error = OK;
  553. }
  554. return ogv_stream;
  555. }
  556. void ResourceFormatLoaderTheora::get_recognized_extensions(List<String> *p_extensions) const {
  557. p_extensions->push_back("ogv");
  558. }
  559. bool ResourceFormatLoaderTheora::handles_type(const String &p_type) const {
  560. return ClassDB::is_parent_class(p_type, "VideoStream");
  561. }
  562. String ResourceFormatLoaderTheora::get_resource_type(const String &p_path) const {
  563. String el = p_path.get_extension().to_lower();
  564. if (el == "ogv") {
  565. return "VideoStreamTheora";
  566. }
  567. return "";
  568. }