video_stream_theora.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  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 "scene/resources/image_texture.h"
  34. #include "thirdparty/misc/yuv2rgb.h"
  35. int VideoStreamPlaybackTheora::buffer_data() {
  36. char *buffer = ogg_sync_buffer(&oy, 4096);
  37. uint64_t bytes = file->get_buffer((uint8_t *)buffer, 4096);
  38. ogg_sync_wrote(&oy, bytes);
  39. return bytes;
  40. }
  41. int VideoStreamPlaybackTheora::queue_page(ogg_page *page) {
  42. ogg_stream_pagein(&to, page);
  43. if (to.e_o_s) {
  44. theora_eos = true;
  45. }
  46. if (has_audio) {
  47. ogg_stream_pagein(&vo, page);
  48. if (vo.e_o_s) {
  49. vorbis_eos = true;
  50. }
  51. }
  52. return 0;
  53. }
  54. int VideoStreamPlaybackTheora::read_page(ogg_page *page) {
  55. int ret = 0;
  56. while (ret <= 0) {
  57. ret = ogg_sync_pageout(&oy, page);
  58. if (ret <= 0) {
  59. int bytes = buffer_data();
  60. if (bytes == 0) {
  61. return 0;
  62. }
  63. }
  64. }
  65. return ret;
  66. }
  67. double VideoStreamPlaybackTheora::get_page_time(ogg_page *page) {
  68. uint64_t granulepos = ogg_page_granulepos(page);
  69. int page_serialno = ogg_page_serialno(page);
  70. double page_time = -1;
  71. if (page_serialno == to.serialno) {
  72. page_time = th_granule_time(td, granulepos);
  73. }
  74. if (has_audio && page_serialno == vo.serialno) {
  75. page_time = vorbis_granule_time(&vd, granulepos);
  76. }
  77. return page_time;
  78. }
  79. // Read one buffer worth of pages and feed them to the streams.
  80. int VideoStreamPlaybackTheora::feed_pages() {
  81. int pages = 0;
  82. ogg_page og;
  83. while (pages == 0) {
  84. while (ogg_sync_pageout(&oy, &og) > 0) {
  85. queue_page(&og);
  86. pages++;
  87. }
  88. if (pages == 0) {
  89. int bytes = buffer_data();
  90. if (bytes == 0) {
  91. break;
  92. }
  93. }
  94. }
  95. return pages;
  96. }
  97. // Seek the video and audio streams simultaneously to find the granulepos where we should start decoding.
  98. // It will return the position where we should start reading pages, and the video and audio granulepos.
  99. int64_t VideoStreamPlaybackTheora::seek_streams(double p_time, int64_t &cur_video_granulepos, int64_t &cur_audio_granulepos) {
  100. // Backtracking less than this is probably a waste of time.
  101. const int64_t min_seek = 512 * 1024;
  102. int64_t target_video_granulepos;
  103. int64_t target_audio_granulepos;
  104. double target_time = 0;
  105. int64_t seek_pos;
  106. // Make a guess where we should start reading in the file, and scan from there.
  107. // We base the guess on the mean bitrate of the streams. It would be theoretically faster to use the bisect method but
  108. // in practice there's a lot of linear scanning to do to find the right pages.
  109. // We want to catch the previous keyframe to the seek time. Since we only know the max GOP, we use that.
  110. if (p_time == -1) { // This is a special case to find the last packets and calculate the video length.
  111. seek_pos = MAX(stream_data_size - min_seek, stream_data_offset);
  112. target_video_granulepos = INT64_MAX;
  113. target_audio_granulepos = INT64_MAX;
  114. } else {
  115. int64_t video_frame = (int64_t)(p_time / frame_duration);
  116. target_video_granulepos = MAX(1LL, video_frame - (1LL << ti.keyframe_granule_shift)) << ti.keyframe_granule_shift;
  117. target_audio_granulepos = 0;
  118. seek_pos = MAX(((target_video_granulepos >> ti.keyframe_granule_shift) - 1) * frame_duration * stream_data_size / stream_length, stream_data_offset);
  119. target_time = th_granule_time(td, target_video_granulepos);
  120. if (has_audio) {
  121. target_audio_granulepos = video_frame * frame_duration * vi.rate;
  122. target_time = MIN(target_time, vorbis_granule_time(&vd, target_audio_granulepos));
  123. }
  124. }
  125. int64_t video_seek_pos = seek_pos;
  126. int64_t audio_seek_pos = seek_pos;
  127. double backtrack_time = 0;
  128. bool video_catch = false;
  129. bool audio_catch = false;
  130. int64_t last_video_granule_seek_pos = seek_pos;
  131. int64_t last_audio_granule_seek_pos = seek_pos;
  132. cur_video_granulepos = -1;
  133. cur_audio_granulepos = -1;
  134. while (!video_catch || (has_audio && !audio_catch)) { // Backtracking loop
  135. if (seek_pos < stream_data_offset) {
  136. seek_pos = stream_data_offset;
  137. }
  138. file->seek(seek_pos);
  139. ogg_sync_reset(&oy);
  140. backtrack_time = 0;
  141. last_video_granule_seek_pos = seek_pos;
  142. last_audio_granule_seek_pos = seek_pos;
  143. while (!video_catch || (has_audio && !audio_catch)) { // Page scanning loop
  144. ogg_page page;
  145. uint64_t last_seek_pos = file->get_position() - oy.fill + oy.returned;
  146. int ret = read_page(&page);
  147. if (ret <= 0) { // End of file.
  148. if (seek_pos < stream_data_offset) { // We've already searched the whole file
  149. return -1;
  150. }
  151. seek_pos -= min_seek;
  152. break;
  153. }
  154. int64_t cur_granulepos = ogg_page_granulepos(&page);
  155. if (cur_granulepos >= 0) {
  156. int page_serialno = ogg_page_serialno(&page);
  157. if (!video_catch && page_serialno == to.serialno) {
  158. if (cur_granulepos >= target_video_granulepos) {
  159. video_catch = true;
  160. if (cur_video_granulepos < 0) {
  161. // Adding 1s helps catching the start of the page and avoids backtrack_time = 0.
  162. backtrack_time = MAX(backtrack_time, 1 + th_granule_time(td, cur_granulepos) - target_time);
  163. }
  164. } else {
  165. video_seek_pos = last_video_granule_seek_pos;
  166. cur_video_granulepos = cur_granulepos;
  167. }
  168. last_video_granule_seek_pos = last_seek_pos;
  169. }
  170. if ((has_audio && !audio_catch) && page_serialno == vo.serialno) {
  171. if (cur_granulepos >= target_audio_granulepos) {
  172. audio_catch = true;
  173. if (cur_audio_granulepos < 0) {
  174. // Adding 1s helps catching the start of the page and avoids backtrack_time = 0.
  175. backtrack_time = MAX(backtrack_time, 1 + vorbis_granule_time(&vd, cur_granulepos) - target_time);
  176. }
  177. } else {
  178. audio_seek_pos = last_audio_granule_seek_pos;
  179. cur_audio_granulepos = cur_granulepos;
  180. }
  181. last_audio_granule_seek_pos = last_seek_pos;
  182. }
  183. }
  184. }
  185. if (backtrack_time > 0) {
  186. if (seek_pos <= stream_data_offset) {
  187. break;
  188. }
  189. int64_t delta_seek = MAX(backtrack_time * stream_data_size / stream_length, min_seek);
  190. seek_pos -= delta_seek;
  191. }
  192. video_catch = cur_video_granulepos != -1;
  193. audio_catch = cur_audio_granulepos != -1;
  194. }
  195. if (cur_video_granulepos < (1LL << ti.keyframe_granule_shift)) {
  196. video_seek_pos = stream_data_offset;
  197. cur_video_granulepos = 1LL << ti.keyframe_granule_shift;
  198. }
  199. if (has_audio) {
  200. if (cur_audio_granulepos == -1) {
  201. audio_seek_pos = stream_data_offset;
  202. cur_audio_granulepos = 0;
  203. }
  204. seek_pos = MIN(video_seek_pos, audio_seek_pos);
  205. } else {
  206. seek_pos = video_seek_pos;
  207. }
  208. return seek_pos;
  209. }
  210. void VideoStreamPlaybackTheora::video_write(th_ycbcr_buffer yuv) {
  211. uint8_t *w = frame_data.ptrw();
  212. char *dst = (char *)w;
  213. uint32_t y_offset = region.position.y * yuv[0].stride + region.position.x;
  214. uint32_t uv_offset = 0;
  215. if (px_fmt == TH_PF_444) {
  216. uv_offset += region.position.y * yuv[1].stride + region.position.x;
  217. yuv444_2_rgb8888((uint8_t *)dst, (uint8_t *)yuv[0].data + y_offset, (uint8_t *)yuv[1].data + uv_offset, (uint8_t *)yuv[2].data + uv_offset, region.size.x, region.size.y, yuv[0].stride, yuv[1].stride, region.size.x << 2);
  218. } else if (px_fmt == TH_PF_422) {
  219. uv_offset += region.position.y * yuv[1].stride + region.position.x / 2;
  220. yuv422_2_rgb8888((uint8_t *)dst, (uint8_t *)yuv[0].data + y_offset, (uint8_t *)yuv[1].data + uv_offset, (uint8_t *)yuv[2].data + uv_offset, region.size.x, region.size.y, yuv[0].stride, yuv[1].stride, region.size.x << 2);
  221. } else if (px_fmt == TH_PF_420) {
  222. uv_offset += region.position.y * yuv[1].stride / 2 + region.position.x / 2;
  223. yuv420_2_rgb8888((uint8_t *)dst, (uint8_t *)yuv[0].data + y_offset, (uint8_t *)yuv[1].data + uv_offset, (uint8_t *)yuv[2].data + uv_offset, region.size.x, region.size.y, yuv[0].stride, yuv[1].stride, region.size.x << 2);
  224. }
  225. Ref<Image> img;
  226. img.instantiate(region.size.x, region.size.y, false, Image::FORMAT_RGBA8, frame_data); //zero copy image creation
  227. texture->update(img); // Zero-copy send to rendering server.
  228. }
  229. void VideoStreamPlaybackTheora::clear() {
  230. if (!file.is_null()) {
  231. file.unref();
  232. }
  233. if (has_audio) {
  234. vorbis_block_clear(&vb);
  235. vorbis_dsp_clear(&vd);
  236. vorbis_comment_clear(&vc);
  237. vorbis_info_clear(&vi);
  238. ogg_stream_clear(&vo);
  239. if (audio_buffer_size) {
  240. memdelete_arr(audio_buffer);
  241. }
  242. }
  243. if (has_video) {
  244. th_decode_free(td);
  245. th_comment_clear(&tc);
  246. th_info_clear(&ti);
  247. ogg_stream_clear(&to);
  248. ogg_sync_clear(&oy);
  249. }
  250. audio_buffer = nullptr;
  251. playing = false;
  252. has_video = false;
  253. has_audio = false;
  254. theora_eos = false;
  255. vorbis_eos = false;
  256. }
  257. void VideoStreamPlaybackTheora::find_streams(th_setup_info *&ts) {
  258. ogg_stream_state test;
  259. ogg_packet op;
  260. ogg_page og;
  261. int stateflag = 0;
  262. int audio_track_skip = audio_track;
  263. /* Only interested in Vorbis/Theora streams */
  264. while (!stateflag) {
  265. int ret = buffer_data();
  266. if (!ret) {
  267. break;
  268. }
  269. while (ogg_sync_pageout(&oy, &og) > 0) {
  270. /* is this a mandated initial header? If not, stop parsing */
  271. if (!ogg_page_bos(&og)) {
  272. /* don't leak the page; get it into the appropriate stream */
  273. queue_page(&og);
  274. stateflag = 1;
  275. break;
  276. }
  277. ogg_stream_init(&test, ogg_page_serialno(&og));
  278. ogg_stream_pagein(&test, &og);
  279. ogg_stream_packetout(&test, &op);
  280. /* identify the codec: try theora */
  281. if (!has_video && th_decode_headerin(&ti, &tc, &ts, &op) >= 0) {
  282. /* it is theora */
  283. memcpy(&to, &test, sizeof(test));
  284. has_video = true;
  285. } else if (!has_audio && vorbis_synthesis_headerin(&vi, &vc, &op) >= 0) {
  286. /* it is vorbis */
  287. if (audio_track_skip) {
  288. vorbis_info_clear(&vi);
  289. vorbis_comment_clear(&vc);
  290. ogg_stream_clear(&test);
  291. vorbis_info_init(&vi);
  292. vorbis_comment_init(&vc);
  293. audio_track_skip--;
  294. } else {
  295. memcpy(&vo, &test, sizeof(test));
  296. has_audio = true;
  297. }
  298. } else {
  299. /* whatever it is, we don't care about it */
  300. ogg_stream_clear(&test);
  301. }
  302. }
  303. }
  304. }
  305. void VideoStreamPlaybackTheora::read_headers(th_setup_info *&ts) {
  306. ogg_packet op;
  307. int theora_header_packets = 1;
  308. int vorbis_header_packets = 1;
  309. /* we're expecting more header packets. */
  310. while (theora_header_packets < 3 || (has_audio && vorbis_header_packets < 3)) {
  311. /* look for further theora headers */
  312. // The API says there can be more than three but only three are mandatory.
  313. while (theora_header_packets < 3 && ogg_stream_packetout(&to, &op) > 0) {
  314. if (th_decode_headerin(&ti, &tc, &ts, &op) > 0) {
  315. theora_header_packets++;
  316. }
  317. }
  318. /* look for more vorbis header packets */
  319. while (has_audio && vorbis_header_packets < 3 && ogg_stream_packetout(&vo, &op) > 0) {
  320. if (!vorbis_synthesis_headerin(&vi, &vc, &op)) {
  321. vorbis_header_packets++;
  322. }
  323. }
  324. /* The header pages/packets will arrive before anything else we care about, or the stream is not obeying spec */
  325. if (theora_header_packets < 3 || (has_audio && vorbis_header_packets < 3)) {
  326. ogg_page page;
  327. if (read_page(&page)) {
  328. queue_page(&page);
  329. } else {
  330. fprintf(stderr, "End of file while searching for codec headers.\n");
  331. break;
  332. }
  333. }
  334. }
  335. has_video = theora_header_packets == 3;
  336. has_audio = vorbis_header_packets == 3;
  337. }
  338. void VideoStreamPlaybackTheora::set_file(const String &p_file) {
  339. ERR_FAIL_COND(playing);
  340. th_setup_info *ts = nullptr;
  341. clear();
  342. file = FileAccess::open(p_file, FileAccess::READ);
  343. ERR_FAIL_COND_MSG(file.is_null(), "Cannot open file '" + p_file + "'.");
  344. file_name = p_file;
  345. ogg_sync_init(&oy);
  346. /* init supporting Vorbis structures needed in header parsing */
  347. vorbis_info_init(&vi);
  348. vorbis_comment_init(&vc);
  349. /* init supporting Theora structures needed in header parsing */
  350. th_comment_init(&tc);
  351. th_info_init(&ti);
  352. /* Zero stream state structs so they can be checked later. */
  353. memset(&to, 0, sizeof(to));
  354. memset(&vo, 0, sizeof(vo));
  355. /* Ogg file open; parse the headers */
  356. find_streams(ts);
  357. read_headers(ts);
  358. if (!has_audio) {
  359. vorbis_comment_clear(&vc);
  360. vorbis_info_clear(&vi);
  361. if (!ogg_stream_check(&vo)) {
  362. ogg_stream_clear(&vo);
  363. }
  364. }
  365. // One video stream is mandatory.
  366. if (!has_video) {
  367. th_setup_free(ts);
  368. th_comment_clear(&tc);
  369. th_info_clear(&ti);
  370. if (!ogg_stream_check(&to)) {
  371. ogg_stream_clear(&to);
  372. }
  373. file.unref();
  374. return;
  375. }
  376. /* And now we have it all. Initialize decoders. */
  377. td = th_decode_alloc(&ti, ts);
  378. th_setup_free(ts);
  379. px_fmt = ti.pixel_fmt;
  380. switch (ti.pixel_fmt) {
  381. case TH_PF_420:
  382. case TH_PF_422:
  383. case TH_PF_444:
  384. break;
  385. default:
  386. WARN_PRINT(" video\n (UNKNOWN Chroma sampling!)\n");
  387. break;
  388. }
  389. th_decode_ctl(td, TH_DECCTL_GET_PPLEVEL_MAX, &pp_level_max, sizeof(pp_level_max));
  390. pp_level = 0;
  391. th_decode_ctl(td, TH_DECCTL_SET_PPLEVEL, &pp_level, sizeof(pp_level));
  392. pp_inc = 0;
  393. size.x = ti.frame_width;
  394. size.y = ti.frame_height;
  395. region.position.x = ti.pic_x;
  396. region.position.y = ti.pic_y;
  397. region.size.x = ti.pic_width;
  398. region.size.y = ti.pic_height;
  399. Ref<Image> img = Image::create_empty(region.size.x, region.size.y, false, Image::FORMAT_RGBA8);
  400. texture->set_image(img);
  401. frame_data.resize(region.size.x * region.size.y * 4);
  402. frame_duration = (double)ti.fps_denominator / ti.fps_numerator;
  403. if (has_audio) {
  404. vorbis_synthesis_init(&vd, &vi);
  405. vorbis_block_init(&vd, &vb);
  406. audio_buffer_size = MIN(vi.channels, 8) * 1024;
  407. audio_buffer = memnew_arr(float, audio_buffer_size);
  408. }
  409. stream_data_offset = file->get_position() - oy.fill + oy.returned;
  410. stream_data_size = file->get_length() - stream_data_offset;
  411. // Sync to last page to find video length.
  412. int64_t seek_pos = MAX(stream_data_offset, (int64_t)file->get_length() - 64 * 1024);
  413. int64_t video_granulepos = INT64_MAX;
  414. int64_t audio_granulepos = INT64_MAX;
  415. file->seek(seek_pos);
  416. seek_pos = seek_streams(-1, video_granulepos, audio_granulepos);
  417. file->seek(seek_pos);
  418. ogg_sync_reset(&oy);
  419. stream_length = 0;
  420. ogg_page page;
  421. while (read_page(&page) > 0) {
  422. // Use MAX because, even though pages are ordered, page time can be -1
  423. // for pages without full frames. Streams could be truncated too.
  424. stream_length = MAX(stream_length, get_page_time(&page));
  425. }
  426. seek(0);
  427. }
  428. double VideoStreamPlaybackTheora::get_time() const {
  429. // FIXME: AudioServer output latency was fixed in af9bb0e, previously it used to
  430. // systematically return 0. Now that it gives a proper latency, it broke this
  431. // code where the delay compensation likely never really worked.
  432. return time - /* AudioServer::get_singleton()->get_output_latency() - */ delay_compensation;
  433. }
  434. Ref<Texture2D> VideoStreamPlaybackTheora::get_texture() const {
  435. return texture;
  436. }
  437. void VideoStreamPlaybackTheora::update(double p_delta) {
  438. if (file.is_null()) {
  439. return;
  440. }
  441. if (!playing || paused) {
  442. return;
  443. }
  444. time += p_delta;
  445. double comp_time = get_time();
  446. bool audio_ready = false;
  447. // Read data until we fill the audio buffer and get a new video frame.
  448. while ((!audio_ready && !audio_done) || (!video_ready && !video_done)) {
  449. ogg_packet op;
  450. while (!audio_ready && !audio_done) {
  451. // Send remaining frames
  452. if (!send_audio()) {
  453. audio_ready = true;
  454. break;
  455. }
  456. float **pcm;
  457. int ret = vorbis_synthesis_pcmout(&vd, &pcm);
  458. if (ret > 0) {
  459. int frames_read = 0;
  460. while (frames_read < ret) {
  461. int m = MIN(audio_buffer_size / vi.channels, ret - frames_read);
  462. int count = 0;
  463. for (int j = 0; j < m; j++) {
  464. for (int i = 0; i < vi.channels; i++) {
  465. audio_buffer[count++] = pcm[i][frames_read + j];
  466. }
  467. }
  468. frames_read += m;
  469. audio_ptr_end = m;
  470. if (!send_audio()) {
  471. audio_ready = true;
  472. break;
  473. }
  474. }
  475. vorbis_synthesis_read(&vd, frames_read);
  476. } else {
  477. /* no pending audio; is there a pending packet to decode? */
  478. if (ogg_stream_packetout(&vo, &op) > 0) {
  479. if (vorbis_synthesis(&vb, &op) == 0) { /* test for success! */
  480. vorbis_synthesis_blockin(&vd, &vb);
  481. }
  482. } else { /* we need more data; break out to suck in another page */
  483. audio_done = vorbis_eos;
  484. break;
  485. }
  486. }
  487. }
  488. while (!video_ready && !video_done) {
  489. if (ogg_stream_packetout(&to, &op) > 0) {
  490. if (op.granulepos >= 0) {
  491. th_decode_ctl(td, TH_DECCTL_SET_GRANPOS, &op.granulepos, sizeof(op.granulepos));
  492. }
  493. int64_t videobuf_granulepos;
  494. int ret = th_decode_packetin(td, &op, &videobuf_granulepos);
  495. if (ret == 0 || ret == TH_DUPFRAME) {
  496. next_frame_time = th_granule_time(td, videobuf_granulepos);
  497. if (next_frame_time > comp_time) {
  498. dup_frame = (ret == TH_DUPFRAME);
  499. video_ready = true;
  500. } else {
  501. /*If we are too slow, reduce the pp level.*/
  502. pp_inc = pp_level > 0 ? -1 : 0;
  503. }
  504. }
  505. } else { /* we need more data; break out to suck in another page */
  506. video_done = theora_eos;
  507. break;
  508. }
  509. }
  510. if (!video_ready || !audio_ready) {
  511. int ret = feed_pages();
  512. if (ret == 0) {
  513. vorbis_eos = true;
  514. theora_eos = true;
  515. break;
  516. }
  517. }
  518. double tdiff = next_frame_time - comp_time;
  519. /*If we have lots of extra time, increase the post-processing level.*/
  520. if (tdiff > ti.fps_denominator * 0.25 / ti.fps_numerator) {
  521. pp_inc = pp_level < pp_level_max ? 1 : 0;
  522. } else if (tdiff < ti.fps_denominator * 0.05 / ti.fps_numerator) {
  523. pp_inc = pp_level > 0 ? -1 : 0;
  524. }
  525. }
  526. if (!video_ready && video_done && audio_done) {
  527. stop();
  528. return;
  529. }
  530. // Wait for the last frame to end before rendering the next one.
  531. if (video_ready && comp_time >= current_frame_time) {
  532. if (!dup_frame) {
  533. th_ycbcr_buffer yuv;
  534. th_decode_ycbcr_out(td, yuv);
  535. video_write(yuv);
  536. }
  537. dup_frame = false;
  538. video_ready = false;
  539. current_frame_time = next_frame_time;
  540. }
  541. }
  542. void VideoStreamPlaybackTheora::play() {
  543. if (playing) {
  544. return;
  545. }
  546. playing = true;
  547. delay_compensation = GLOBAL_GET("audio/video/video_delay_compensation_ms");
  548. delay_compensation /= 1000.0;
  549. }
  550. void VideoStreamPlaybackTheora::stop() {
  551. playing = false;
  552. seek(0);
  553. }
  554. bool VideoStreamPlaybackTheora::is_playing() const {
  555. return playing;
  556. }
  557. void VideoStreamPlaybackTheora::set_paused(bool p_paused) {
  558. paused = p_paused;
  559. }
  560. bool VideoStreamPlaybackTheora::is_paused() const {
  561. return paused;
  562. }
  563. double VideoStreamPlaybackTheora::get_length() const {
  564. return stream_length;
  565. }
  566. double VideoStreamPlaybackTheora::get_playback_position() const {
  567. return get_time();
  568. }
  569. void VideoStreamPlaybackTheora::seek(double p_time) {
  570. if (file.is_null()) {
  571. return;
  572. }
  573. if (p_time >= stream_length) {
  574. return;
  575. }
  576. video_ready = false;
  577. next_frame_time = 0;
  578. current_frame_time = -1;
  579. dup_frame = false;
  580. video_done = false;
  581. audio_done = !has_audio;
  582. theora_eos = false;
  583. vorbis_eos = false;
  584. audio_ptr_start = 0;
  585. audio_ptr_end = 0;
  586. ogg_stream_reset(&to);
  587. if (has_audio) {
  588. ogg_stream_reset(&vo);
  589. vorbis_synthesis_restart(&vd);
  590. }
  591. int64_t seek_pos;
  592. int64_t video_granulepos;
  593. int64_t audio_granulepos;
  594. // Find the granules we need so we can start playing at the seek time.
  595. seek_pos = seek_streams(p_time, video_granulepos, audio_granulepos);
  596. if (seek_pos < 0) {
  597. return;
  598. }
  599. file->seek(seek_pos);
  600. ogg_sync_reset(&oy);
  601. time = p_time;
  602. double last_audio_time = 0;
  603. double last_video_time = 0;
  604. bool first_frame_decoded = false;
  605. bool start_audio = (audio_granulepos == 0);
  606. bool start_video = (video_granulepos == (1LL << ti.keyframe_granule_shift));
  607. bool keyframe_found = false;
  608. uint64_t current_frame = 0;
  609. // Read from the streams skipping pages until we reach the granules we want. We won't skip pages from both video and
  610. // audio streams, only one of them, until decoding of both starts.
  611. // video_granulepos and audio_granulepos are guaranteed to be found by checking the granulepos in the packets, no
  612. // need to keep track of packets with granulepos == -1 until decoding starts.
  613. while ((has_audio && last_audio_time < p_time) || (last_video_time <= p_time)) {
  614. ogg_packet op;
  615. if (feed_pages() == 0) {
  616. break;
  617. }
  618. while (has_audio && last_audio_time < p_time && ogg_stream_packetout(&vo, &op) > 0) {
  619. if (start_audio) {
  620. if (vorbis_synthesis(&vb, &op) == 0) { /* test for success! */
  621. vorbis_synthesis_blockin(&vd, &vb);
  622. float **pcm;
  623. int samples_left = ceil((p_time - last_audio_time) * vi.rate);
  624. int samples_read = vorbis_synthesis_pcmout(&vd, &pcm);
  625. int samples_consumed = MIN(samples_left, samples_read);
  626. vorbis_synthesis_read(&vd, samples_consumed);
  627. last_audio_time += (double)samples_consumed / vi.rate;
  628. }
  629. } else if (op.granulepos >= audio_granulepos) {
  630. last_audio_time = vorbis_granule_time(&vd, op.granulepos);
  631. // Start tracking audio now. This won't produce any samples but will update the decoder state.
  632. if (vorbis_synthesis_trackonly(&vb, &op) == 0) {
  633. vorbis_synthesis_blockin(&vd, &vb);
  634. }
  635. start_audio = true;
  636. }
  637. }
  638. while (last_video_time <= p_time && ogg_stream_packetout(&to, &op) > 0) {
  639. if (!start_video && (op.granulepos >= video_granulepos || video_granulepos == (1LL << ti.keyframe_granule_shift))) {
  640. if (op.granulepos > 0) {
  641. current_frame = th_granule_frame(td, op.granulepos);
  642. }
  643. start_video = true;
  644. }
  645. // Don't start decoding until a keyframe is found, but count frames.
  646. if (start_video) {
  647. if (!keyframe_found && th_packet_iskeyframe(&op)) {
  648. keyframe_found = true;
  649. int64_t cur_granulepos = (current_frame + 1) << ti.keyframe_granule_shift;
  650. th_decode_ctl(td, TH_DECCTL_SET_GRANPOS, &cur_granulepos, sizeof(cur_granulepos));
  651. }
  652. if (keyframe_found) {
  653. int64_t videobuf_granulepos;
  654. if (op.granulepos >= 0) {
  655. th_decode_ctl(td, TH_DECCTL_SET_GRANPOS, &op.granulepos, sizeof(op.granulepos));
  656. }
  657. int ret = th_decode_packetin(td, &op, &videobuf_granulepos);
  658. if (ret == 0 || ret == TH_DUPFRAME) {
  659. last_video_time = th_granule_time(td, videobuf_granulepos);
  660. first_frame_decoded = true;
  661. }
  662. } else {
  663. current_frame++;
  664. }
  665. }
  666. }
  667. }
  668. if (first_frame_decoded) {
  669. if (is_playing()) {
  670. // Draw the current frame.
  671. th_ycbcr_buffer yuv;
  672. th_decode_ycbcr_out(td, yuv);
  673. video_write(yuv);
  674. current_frame_time = last_video_time;
  675. } else {
  676. next_frame_time = current_frame_time;
  677. video_ready = true;
  678. }
  679. }
  680. }
  681. int VideoStreamPlaybackTheora::get_channels() const {
  682. return vi.channels;
  683. }
  684. void VideoStreamPlaybackTheora::set_audio_track(int p_idx) {
  685. audio_track = p_idx;
  686. }
  687. int VideoStreamPlaybackTheora::get_mix_rate() const {
  688. return vi.rate;
  689. }
  690. VideoStreamPlaybackTheora::VideoStreamPlaybackTheora() {
  691. texture.instantiate();
  692. }
  693. VideoStreamPlaybackTheora::~VideoStreamPlaybackTheora() {
  694. clear();
  695. }
  696. void VideoStreamTheora::_bind_methods() {}
  697. 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) {
  698. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
  699. if (f.is_null()) {
  700. if (r_error) {
  701. *r_error = ERR_CANT_OPEN;
  702. }
  703. return Ref<Resource>();
  704. }
  705. VideoStreamTheora *stream = memnew(VideoStreamTheora);
  706. stream->set_file(p_path);
  707. Ref<VideoStreamTheora> ogv_stream = Ref<VideoStreamTheora>(stream);
  708. if (r_error) {
  709. *r_error = OK;
  710. }
  711. return ogv_stream;
  712. }
  713. void ResourceFormatLoaderTheora::get_recognized_extensions(List<String> *p_extensions) const {
  714. p_extensions->push_back("ogv");
  715. }
  716. bool ResourceFormatLoaderTheora::handles_type(const String &p_type) const {
  717. return ClassDB::is_parent_class(p_type, "VideoStream");
  718. }
  719. String ResourceFormatLoaderTheora::get_resource_type(const String &p_path) const {
  720. String el = p_path.get_extension().to_lower();
  721. if (el == "ogv") {
  722. return "VideoStreamTheora";
  723. }
  724. return "";
  725. }