video_stream_theora.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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. if (theora_p) {
  43. ogg_stream_pagein(&to, page);
  44. if (to.e_o_s) {
  45. theora_eos = true;
  46. }
  47. }
  48. if (vorbis_p) {
  49. ogg_stream_pagein(&vo, page);
  50. if (vo.e_o_s) {
  51. vorbis_eos = true;
  52. }
  53. }
  54. return 0;
  55. }
  56. void VideoStreamPlaybackTheora::video_write(th_ycbcr_buffer yuv) {
  57. uint8_t *w = frame_data.ptrw();
  58. char *dst = (char *)w;
  59. uint32_t y_offset = region.position.y * yuv[0].stride + region.position.x;
  60. uint32_t uv_offset = region.position.y * yuv[1].stride + region.position.x;
  61. if (px_fmt == TH_PF_444) {
  62. 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);
  63. } else if (px_fmt == TH_PF_422) {
  64. 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);
  65. } else if (px_fmt == TH_PF_420) {
  66. 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);
  67. }
  68. Ref<Image> img;
  69. img.instantiate(region.size.x, region.size.y, false, Image::FORMAT_RGBA8, frame_data); //zero copy image creation
  70. texture->update(img); //zero copy send to rendering server
  71. }
  72. void VideoStreamPlaybackTheora::clear() {
  73. if (file.is_null()) {
  74. return;
  75. }
  76. if (vorbis_p) {
  77. ogg_stream_clear(&vo);
  78. if (vorbis_p >= 3) {
  79. vorbis_block_clear(&vb);
  80. vorbis_dsp_clear(&vd);
  81. }
  82. vorbis_comment_clear(&vc);
  83. vorbis_info_clear(&vi);
  84. vorbis_p = 0;
  85. }
  86. if (theora_p) {
  87. ogg_stream_clear(&to);
  88. th_decode_free(td);
  89. th_comment_clear(&tc);
  90. th_info_clear(&ti);
  91. theora_p = 0;
  92. }
  93. ogg_sync_clear(&oy);
  94. theora_p = 0;
  95. vorbis_p = 0;
  96. next_frame_time = 0;
  97. current_frame_time = 0;
  98. theora_eos = false;
  99. vorbis_eos = false;
  100. video_ready = false;
  101. video_done = false;
  102. audio_done = false;
  103. file.unref();
  104. playing = false;
  105. }
  106. void VideoStreamPlaybackTheora::set_file(const String &p_file) {
  107. ERR_FAIL_COND(playing);
  108. ogg_packet op;
  109. th_setup_info *ts = nullptr;
  110. file_name = p_file;
  111. file = FileAccess::open(p_file, FileAccess::READ);
  112. ERR_FAIL_COND_MSG(file.is_null(), "Cannot open file '" + p_file + "'.");
  113. ogg_sync_init(&oy);
  114. /* init supporting Vorbis structures needed in header parsing */
  115. vorbis_info_init(&vi);
  116. vorbis_comment_init(&vc);
  117. /* init supporting Theora structures needed in header parsing */
  118. th_comment_init(&tc);
  119. th_info_init(&ti);
  120. theora_eos = false;
  121. vorbis_eos = false;
  122. /* Ogg file open; parse the headers */
  123. /* Only interested in Vorbis/Theora streams */
  124. int stateflag = 0;
  125. int audio_track_skip = audio_track;
  126. while (!stateflag) {
  127. int ret = buffer_data();
  128. if (ret == 0) {
  129. break;
  130. }
  131. while (ogg_sync_pageout(&oy, &og) > 0) {
  132. ogg_stream_state test;
  133. /* is this a mandated initial header? If not, stop parsing */
  134. if (!ogg_page_bos(&og)) {
  135. /* don't leak the page; get it into the appropriate stream */
  136. queue_page(&og);
  137. stateflag = 1;
  138. break;
  139. }
  140. ogg_stream_init(&test, ogg_page_serialno(&og));
  141. ogg_stream_pagein(&test, &og);
  142. ogg_stream_packetout(&test, &op);
  143. /* identify the codec: try theora */
  144. if (!theora_p && th_decode_headerin(&ti, &tc, &ts, &op) >= 0) {
  145. /* it is theora */
  146. memcpy(&to, &test, sizeof(test));
  147. theora_p = 1;
  148. } else if (!vorbis_p && vorbis_synthesis_headerin(&vi, &vc, &op) >= 0) {
  149. /* it is vorbis */
  150. if (audio_track_skip) {
  151. vorbis_info_clear(&vi);
  152. vorbis_comment_clear(&vc);
  153. ogg_stream_clear(&test);
  154. vorbis_info_init(&vi);
  155. vorbis_comment_init(&vc);
  156. audio_track_skip--;
  157. } else {
  158. memcpy(&vo, &test, sizeof(test));
  159. vorbis_p = 1;
  160. }
  161. } else {
  162. /* whatever it is, we don't care about it */
  163. ogg_stream_clear(&test);
  164. }
  165. }
  166. /* fall through to non-bos page parsing */
  167. }
  168. /* we're expecting more header packets. */
  169. while ((theora_p && theora_p < 3) || (vorbis_p && vorbis_p < 3)) {
  170. int ret = 0;
  171. /* look for further theora headers */
  172. if (theora_p && theora_p < 3) {
  173. ret = ogg_stream_packetout(&to, &op);
  174. }
  175. while (theora_p && theora_p < 3 && ret) {
  176. if (ret < 0) {
  177. fprintf(stderr, "Error parsing Theora stream headers; corrupt stream?\n");
  178. clear();
  179. return;
  180. }
  181. if (!th_decode_headerin(&ti, &tc, &ts, &op)) {
  182. fprintf(stderr, "Error parsing Theora stream headers; corrupt stream?\n");
  183. clear();
  184. return;
  185. }
  186. ret = ogg_stream_packetout(&to, &op);
  187. theora_p++;
  188. }
  189. /* look for more vorbis header packets */
  190. if (vorbis_p && vorbis_p < 3) {
  191. ret = ogg_stream_packetout(&vo, &op);
  192. }
  193. while (vorbis_p && vorbis_p < 3 && ret) {
  194. if (ret < 0) {
  195. fprintf(stderr, "Error parsing Vorbis stream headers; corrupt stream?\n");
  196. clear();
  197. return;
  198. }
  199. ret = vorbis_synthesis_headerin(&vi, &vc, &op);
  200. if (ret) {
  201. fprintf(stderr, "Error parsing Vorbis stream headers; corrupt stream?\n");
  202. clear();
  203. return;
  204. }
  205. vorbis_p++;
  206. if (vorbis_p == 3) {
  207. break;
  208. }
  209. ret = ogg_stream_packetout(&vo, &op);
  210. }
  211. /* The header pages/packets will arrive before anything else we
  212. care about, or the stream is not obeying spec */
  213. if (ogg_sync_pageout(&oy, &og) > 0) {
  214. queue_page(&og); /* demux into the appropriate stream */
  215. } else {
  216. int ret2 = buffer_data(); /* someone needs more data */
  217. if (ret2 == 0) {
  218. fprintf(stderr, "End of file while searching for codec headers.\n");
  219. clear();
  220. return;
  221. }
  222. }
  223. }
  224. /* And now we have it all. Initialize decoders. */
  225. if (theora_p) {
  226. td = th_decode_alloc(&ti, ts);
  227. px_fmt = ti.pixel_fmt;
  228. switch (ti.pixel_fmt) {
  229. case TH_PF_420:
  230. //printf(" 4:2:0 video\n");
  231. break;
  232. case TH_PF_422:
  233. //printf(" 4:2:2 video\n");
  234. break;
  235. case TH_PF_444:
  236. //printf(" 4:4:4 video\n");
  237. break;
  238. case TH_PF_RSVD:
  239. default:
  240. printf(" video\n (UNKNOWN Chroma sampling!)\n");
  241. break;
  242. }
  243. th_decode_ctl(td, TH_DECCTL_GET_PPLEVEL_MAX, &pp_level_max,
  244. sizeof(pp_level_max));
  245. pp_level = 0;
  246. th_decode_ctl(td, TH_DECCTL_SET_PPLEVEL, &pp_level, sizeof(pp_level));
  247. pp_inc = 0;
  248. size.x = ti.frame_width;
  249. size.y = ti.frame_height;
  250. region.position.x = ti.pic_x;
  251. region.position.y = ti.pic_y;
  252. region.size.x = ti.pic_width;
  253. region.size.y = ti.pic_height;
  254. Ref<Image> img = Image::create_empty(region.size.x, region.size.y, false, Image::FORMAT_RGBA8);
  255. texture->set_image(img);
  256. frame_data.resize(region.size.x * region.size.y * 4);
  257. frame_duration = (double)ti.fps_denominator / ti.fps_numerator;
  258. } else {
  259. /* tear down the partial theora setup */
  260. th_info_clear(&ti);
  261. th_comment_clear(&tc);
  262. }
  263. th_setup_free(ts);
  264. if (vorbis_p) {
  265. vorbis_synthesis_init(&vd, &vi);
  266. vorbis_block_init(&vd, &vb);
  267. //_setup(vi.channels, vi.rate);
  268. } else {
  269. /* tear down the partial vorbis setup */
  270. vorbis_info_clear(&vi);
  271. vorbis_comment_clear(&vc);
  272. }
  273. playing = false;
  274. buffering = true;
  275. time = 0;
  276. video_done = !theora_p;
  277. audio_done = !vorbis_p;
  278. }
  279. double VideoStreamPlaybackTheora::get_time() const {
  280. // FIXME: AudioServer output latency was fixed in af9bb0e, previously it used to
  281. // systematically return 0. Now that it gives a proper latency, it broke this
  282. // code where the delay compensation likely never really worked.
  283. return time - /* AudioServer::get_singleton()->get_output_latency() - */ delay_compensation;
  284. }
  285. Ref<Texture2D> VideoStreamPlaybackTheora::get_texture() const {
  286. return texture;
  287. }
  288. void VideoStreamPlaybackTheora::update(double p_delta) {
  289. if (file.is_null()) {
  290. return;
  291. }
  292. if (!playing || paused) {
  293. return;
  294. }
  295. time += p_delta;
  296. double comp_time = get_time();
  297. bool audio_ready = false;
  298. // Read data until we fill the audio buffer and get a new video frame.
  299. while ((!audio_ready && !audio_done) || (!video_ready && !video_done)) {
  300. ogg_packet op;
  301. while (!audio_ready && !audio_done) {
  302. float **pcm;
  303. int ret = vorbis_synthesis_pcmout(&vd, &pcm);
  304. if (ret > 0) {
  305. const int AUXBUF_LEN = 4096;
  306. int to_read = ret;
  307. float aux_buffer[AUXBUF_LEN];
  308. while (to_read) {
  309. int m = MIN(AUXBUF_LEN / vi.channels, to_read);
  310. int count = 0;
  311. for (int j = 0; j < m; j++) {
  312. for (int i = 0; i < vi.channels; i++) {
  313. aux_buffer[count++] = pcm[i][j];
  314. }
  315. }
  316. int mixed = mix_callback(mix_udata, aux_buffer, m);
  317. to_read -= mixed;
  318. if (mixed != m) { //could mix no more
  319. audio_ready = true;
  320. break;
  321. }
  322. }
  323. vorbis_synthesis_read(&vd, ret - to_read);
  324. } else {
  325. /* no pending audio; is there a pending packet to decode? */
  326. if (ogg_stream_packetout(&vo, &op) > 0) {
  327. if (vorbis_synthesis(&vb, &op) == 0) { /* test for success! */
  328. vorbis_synthesis_blockin(&vd, &vb);
  329. }
  330. } else { /* we need more data; break out to suck in another page */
  331. audio_done = vorbis_eos;
  332. break;
  333. }
  334. }
  335. }
  336. while (!video_ready && !video_done) {
  337. if (ogg_stream_packetout(&to, &op) > 0) {
  338. /*HACK: This should be set after a seek or a gap, but we might not have
  339. a granulepos for the first packet (we only have them for the last
  340. packet on a page), so we just set it as often as we get it.
  341. To do this right, we should back-track from the last packet on the
  342. page and compute the correct granulepos for the first packet after
  343. a seek or a gap.*/
  344. if (op.granulepos >= 0) {
  345. th_decode_ctl(td, TH_DECCTL_SET_GRANPOS, &op.granulepos,
  346. sizeof(op.granulepos));
  347. }
  348. ogg_int64_t videobuf_granulepos;
  349. int ret = th_decode_packetin(td, &op, &videobuf_granulepos);
  350. if (ret == 0 || ret == TH_DUPFRAME) {
  351. next_frame_time = th_granule_time(td, videobuf_granulepos);
  352. if (next_frame_time > comp_time) {
  353. dup_frame = (ret == TH_DUPFRAME);
  354. video_ready = true;
  355. } else {
  356. /*If we are too slow, reduce the pp level.*/
  357. pp_inc = pp_level > 0 ? -1 : 0;
  358. }
  359. }
  360. } else { /* we need more data; break out to suck in another page */
  361. video_done = theora_eos;
  362. break;
  363. }
  364. }
  365. if (!video_ready || !audio_ready) {
  366. int ret = buffer_data();
  367. if (ret > 0) {
  368. while (ogg_sync_pageout(&oy, &og) > 0) {
  369. queue_page(&og);
  370. }
  371. } else {
  372. vorbis_eos = true;
  373. theora_eos = true;
  374. break;
  375. }
  376. }
  377. double tdiff = next_frame_time - comp_time;
  378. /*If we have lots of extra time, increase the post-processing level.*/
  379. if (tdiff > ti.fps_denominator * 0.25 / ti.fps_numerator) {
  380. pp_inc = pp_level < pp_level_max ? 1 : 0;
  381. } else if (tdiff < ti.fps_denominator * 0.05 / ti.fps_numerator) {
  382. pp_inc = pp_level > 0 ? -1 : 0;
  383. }
  384. }
  385. if (!video_ready && video_done && audio_done) {
  386. stop();
  387. return;
  388. }
  389. // Wait for the last frame to end before rendering the next one.
  390. if (video_ready && comp_time >= current_frame_time) {
  391. if (!dup_frame) {
  392. th_ycbcr_buffer yuv;
  393. th_decode_ycbcr_out(td, yuv);
  394. video_write(yuv);
  395. }
  396. dup_frame = false;
  397. video_ready = false;
  398. current_frame_time = next_frame_time;
  399. }
  400. }
  401. void VideoStreamPlaybackTheora::play() {
  402. if (!playing) {
  403. time = 0;
  404. } else {
  405. stop();
  406. }
  407. playing = true;
  408. delay_compensation = GLOBAL_GET("audio/video/video_delay_compensation_ms");
  409. delay_compensation /= 1000.0;
  410. }
  411. void VideoStreamPlaybackTheora::stop() {
  412. if (playing) {
  413. clear();
  414. set_file(file_name); //reset
  415. }
  416. playing = false;
  417. time = 0;
  418. }
  419. bool VideoStreamPlaybackTheora::is_playing() const {
  420. return playing;
  421. }
  422. void VideoStreamPlaybackTheora::set_paused(bool p_paused) {
  423. paused = p_paused;
  424. }
  425. bool VideoStreamPlaybackTheora::is_paused() const {
  426. return paused;
  427. }
  428. double VideoStreamPlaybackTheora::get_length() const {
  429. return 0;
  430. }
  431. double VideoStreamPlaybackTheora::get_playback_position() const {
  432. return get_time();
  433. }
  434. void VideoStreamPlaybackTheora::seek(double p_time) {
  435. WARN_PRINT_ONCE("Seeking in Theora videos is not implemented yet (it's only supported for GDExtension-provided video streams).");
  436. }
  437. int VideoStreamPlaybackTheora::get_channels() const {
  438. return vi.channels;
  439. }
  440. void VideoStreamPlaybackTheora::set_audio_track(int p_idx) {
  441. audio_track = p_idx;
  442. }
  443. int VideoStreamPlaybackTheora::get_mix_rate() const {
  444. return vi.rate;
  445. }
  446. VideoStreamPlaybackTheora::VideoStreamPlaybackTheora() {
  447. texture.instantiate();
  448. }
  449. VideoStreamPlaybackTheora::~VideoStreamPlaybackTheora() {
  450. clear();
  451. }
  452. void VideoStreamTheora::_bind_methods() {}
  453. 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) {
  454. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
  455. if (f.is_null()) {
  456. if (r_error) {
  457. *r_error = ERR_CANT_OPEN;
  458. }
  459. return Ref<Resource>();
  460. }
  461. VideoStreamTheora *stream = memnew(VideoStreamTheora);
  462. stream->set_file(p_path);
  463. Ref<VideoStreamTheora> ogv_stream = Ref<VideoStreamTheora>(stream);
  464. if (r_error) {
  465. *r_error = OK;
  466. }
  467. return ogv_stream;
  468. }
  469. void ResourceFormatLoaderTheora::get_recognized_extensions(List<String> *p_extensions) const {
  470. p_extensions->push_back("ogv");
  471. }
  472. bool ResourceFormatLoaderTheora::handles_type(const String &p_type) const {
  473. return ClassDB::is_parent_class(p_type, "VideoStream");
  474. }
  475. String ResourceFormatLoaderTheora::get_resource_type(const String &p_path) const {
  476. String el = p_path.get_extension().to_lower();
  477. if (el == "ogv") {
  478. return "VideoStreamTheora";
  479. }
  480. return "";
  481. }