video_stream_theora.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  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-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "globals.h"
  32. #include "os/os.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. /*
  72. int y_offset, uv_offset;
  73. y_offset=(ti.pic_x&~1)+yuv[0].stride*(ti.pic_y&~1);
  74. {
  75. int pixels = size.x * size.y;
  76. frame_data.resize(pixels * 4);
  77. DVector<uint8_t>::Write w = frame_data.write();
  78. char* dst = (char*)w.ptr();
  79. int p = 0;
  80. for (int i=0; i<size.y; i++) {
  81. char *in_y = (char *)yuv[0].data+y_offset+yuv[0].stride*i;
  82. char *out = dst + (int)size.x * 4 * i;
  83. for (int j=0;j<size.x;j++) {
  84. dst[p++] = in_y[j];
  85. dst[p++] = in_y[j];
  86. dst[p++] = in_y[j];
  87. dst[p++] = 255;
  88. };
  89. }
  90. format = Image::FORMAT_RGBA;
  91. }
  92. // */
  93. //*
  94. int pitch = 4;
  95. frame_data.resize(size.x * size.y * pitch);
  96. {
  97. DVector<uint8_t>::Write w = frame_data.write();
  98. char *dst = (char *)w.ptr();
  99. //uv_offset=(ti.pic_x/2)+(yuv[1].stride)*(ti.pic_y/2);
  100. if (px_fmt == TH_PF_444) {
  101. 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);
  102. } else if (px_fmt == TH_PF_422) {
  103. 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);
  104. } else if (px_fmt == TH_PF_420) {
  105. 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);
  106. };
  107. format = Image::FORMAT_RGBA;
  108. }
  109. Image img(size.x, size.y, 0, Image::FORMAT_RGBA, frame_data); //zero copy image creation
  110. texture->set_data(img); //zero copy send to visual server
  111. /*
  112. if (px_fmt == TH_PF_444) {
  113. int pitch = 3;
  114. frame_data.resize(size.x * size.y * pitch);
  115. DVector<uint8_t>::Write w = frame_data.write();
  116. char* dst = (char*)w.ptr();
  117. for(int i=0;i<size.y;i++) {
  118. char *in_y = (char *)yuv[0].data+y_offset+yuv[0].stride*i;
  119. char *out = dst + (int)size.x * pitch * i;
  120. char *in_u = (char *)yuv[1].data+uv_offset+yuv[1].stride*i;
  121. char *in_v = (char *)yuv[2].data+uv_offset+yuv[2].stride*i;
  122. for (int j=0;j<size.x;j++) {
  123. out[j*3+0] = in_y[j];
  124. out[j*3+1] = in_u[j];
  125. out[j*3+2] = in_v[j];
  126. };
  127. }
  128. format = Image::FORMAT_YUV_444;
  129. } else {
  130. int div;
  131. if (px_fmt!=TH_PF_422) {
  132. div = 2;
  133. }
  134. bool rgba = true;
  135. if (rgba) {
  136. int pitch = 4;
  137. frame_data.resize(size.x * size.y * pitch);
  138. DVector<uint8_t>::Write w = frame_data.write();
  139. char* dst = (char*)w.ptr();
  140. uv_offset=(ti.pic_x/2)+(yuv[1].stride)*(ti.pic_y / div);
  141. for(int i=0;i<size.y;i++) {
  142. char *in_y = (char *)yuv[0].data+y_offset+yuv[0].stride*i;
  143. char *in_u = (char *)yuv[1].data+uv_offset+yuv[1].stride*(i/div);
  144. char *in_v = (char *)yuv[2].data+uv_offset+yuv[2].stride*(i/div);
  145. uint8_t *out = (uint8_t*)dst + (int)size.x * pitch * i;
  146. int ofs = 0;
  147. for (int j=0;j<size.x;j++) {
  148. uint8_t y, u, v;
  149. y = in_y[j];
  150. u = in_u[j/2];
  151. v = in_v[j/2];
  152. int32_t r = Math::fast_ftoi(1.164 * (y - 16) + 1.596 * (v - 128));
  153. int32_t g = Math::fast_ftoi(1.164 * (y - 16) - 0.813 * (v - 128) - 0.391 * (u - 128));
  154. int32_t b = Math::fast_ftoi(1.164 * (y - 16) + 2.018 * (u - 128));
  155. out[ofs++] = CLAMP(r, 0, 255);
  156. out[ofs++] = CLAMP(g, 0, 255);
  157. out[ofs++] = CLAMP(b, 0, 255);
  158. out[ofs++] = 255;
  159. }
  160. }
  161. format = Image::FORMAT_RGBA;
  162. } else {
  163. int pitch = 2;
  164. frame_data.resize(size.x * size.y * pitch);
  165. DVector<uint8_t>::Write w = frame_data.write();
  166. char* dst = (char*)w.ptr();
  167. uv_offset=(ti.pic_x/2)+(yuv[1].stride)*(ti.pic_y / div);
  168. for(int i=0;i<size.y;i++) {
  169. char *in_y = (char *)yuv[0].data+y_offset+yuv[0].stride*i;
  170. char *out = dst + (int)size.x * pitch * i;
  171. for (int j=0;j<size.x;j++)
  172. out[j*2] = in_y[j];
  173. char *in_u = (char *)yuv[1].data+uv_offset+yuv[1].stride*(i/div);
  174. char *in_v = (char *)yuv[2].data+uv_offset+yuv[2].stride*(i/div);
  175. for (int j=0;j<(int)size.x>>1;j++) {
  176. out[j*4+1] = in_u[j];
  177. out[j*4+3] = in_v[j];
  178. }
  179. }
  180. format = Image::FORMAT_YUV_422;
  181. };
  182. };
  183. // */
  184. frames_pending = 1;
  185. }
  186. void VideoStreamPlaybackTheora::clear() {
  187. if (!file)
  188. return;
  189. if (vorbis_p) {
  190. ogg_stream_clear(&vo);
  191. if (vorbis_p >= 3) {
  192. vorbis_block_clear(&vb);
  193. vorbis_dsp_clear(&vd);
  194. };
  195. vorbis_comment_clear(&vc);
  196. vorbis_info_clear(&vi);
  197. vorbis_p = 0;
  198. }
  199. if (theora_p) {
  200. ogg_stream_clear(&to);
  201. th_decode_free(td);
  202. th_comment_clear(&tc);
  203. th_info_clear(&ti);
  204. theora_p = 0;
  205. }
  206. ogg_sync_clear(&oy);
  207. #ifdef THEORA_USE_THREAD_STREAMING
  208. thread_exit = true;
  209. thread_sem->post(); //just in case
  210. Thread::wait_to_finish(thread);
  211. memdelete(thread);
  212. thread = NULL;
  213. ring_buffer.clear();
  214. #endif
  215. //file_name = "";
  216. theora_p = 0;
  217. vorbis_p = 0;
  218. videobuf_ready = 0;
  219. frames_pending = 0;
  220. videobuf_time = 0;
  221. theora_eos = false;
  222. vorbis_eos = false;
  223. if (file) {
  224. memdelete(file);
  225. }
  226. file = NULL;
  227. playing = false;
  228. };
  229. void VideoStreamPlaybackTheora::set_file(const String &p_file) {
  230. ERR_FAIL_COND(playing);
  231. ogg_packet op;
  232. th_setup_info *ts = NULL;
  233. file_name = p_file;
  234. if (file) {
  235. memdelete(file);
  236. }
  237. file = FileAccess::open(p_file, FileAccess::READ);
  238. ERR_FAIL_COND(!file);
  239. #ifdef THEORA_USE_THREAD_STREAMING
  240. thread_exit = false;
  241. thread_eof = false;
  242. //pre-fill buffer
  243. int to_read = ring_buffer.space_left();
  244. int read = file->get_buffer(read_buffer.ptr(), to_read);
  245. ring_buffer.write(read_buffer.ptr(), read);
  246. thread = Thread::create(_streaming_thread, this);
  247. #endif
  248. ogg_sync_init(&oy);
  249. /* init supporting Vorbis structures needed in header parsing */
  250. vorbis_info_init(&vi);
  251. vorbis_comment_init(&vc);
  252. /* init supporting Theora structures needed in header parsing */
  253. th_comment_init(&tc);
  254. th_info_init(&ti);
  255. theora_eos = false;
  256. vorbis_eos = false;
  257. /* Ogg file open; parse the headers */
  258. /* Only interested in Vorbis/Theora streams */
  259. int stateflag = 0;
  260. int audio_track_skip = audio_track;
  261. while (!stateflag) {
  262. int ret = buffer_data();
  263. if (ret == 0) break;
  264. while (ogg_sync_pageout(&oy, &og) > 0) {
  265. ogg_stream_state test;
  266. /* is this a mandated initial header? If not, stop parsing */
  267. if (!ogg_page_bos(&og)) {
  268. /* don't leak the page; get it into the appropriate stream */
  269. queue_page(&og);
  270. stateflag = 1;
  271. break;
  272. }
  273. ogg_stream_init(&test, ogg_page_serialno(&og));
  274. ogg_stream_pagein(&test, &og);
  275. ogg_stream_packetout(&test, &op);
  276. /* identify the codec: try theora */
  277. if (!theora_p && th_decode_headerin(&ti, &tc, &ts, &op) >= 0) {
  278. /* it is theora */
  279. copymem(&to, &test, sizeof(test));
  280. theora_p = 1;
  281. } else if (!vorbis_p && vorbis_synthesis_headerin(&vi, &vc, &op) >= 0) {
  282. /* it is vorbis */
  283. if (audio_track_skip) {
  284. vorbis_info_clear(&vi);
  285. vorbis_comment_clear(&vc);
  286. ogg_stream_clear(&test);
  287. vorbis_info_init(&vi);
  288. vorbis_comment_init(&vc);
  289. audio_track_skip--;
  290. } else {
  291. copymem(&vo, &test, sizeof(test));
  292. vorbis_p = 1;
  293. }
  294. } else {
  295. /* whatever it is, we don't care about it */
  296. ogg_stream_clear(&test);
  297. }
  298. }
  299. /* fall through to non-bos page parsing */
  300. }
  301. /* we're expecting more header packets. */
  302. while ((theora_p && theora_p < 3) || (vorbis_p && vorbis_p < 3)) {
  303. int ret;
  304. /* look for further theora headers */
  305. while (theora_p && (theora_p < 3) && (ret = ogg_stream_packetout(&to, &op))) {
  306. if (ret < 0) {
  307. fprintf(stderr, "Error parsing Theora stream headers; "
  308. "corrupt stream?\n");
  309. clear();
  310. return;
  311. }
  312. if (!th_decode_headerin(&ti, &tc, &ts, &op)) {
  313. fprintf(stderr, "Error parsing Theora stream headers; "
  314. "corrupt stream?\n");
  315. clear();
  316. return;
  317. }
  318. theora_p++;
  319. }
  320. /* look for more vorbis header packets */
  321. while (vorbis_p && (vorbis_p < 3) && (ret = ogg_stream_packetout(&vo, &op))) {
  322. if (ret < 0) {
  323. fprintf(stderr, "Error parsing Vorbis stream headers; corrupt stream?\n");
  324. clear();
  325. return;
  326. }
  327. ret = vorbis_synthesis_headerin(&vi, &vc, &op);
  328. if (ret) {
  329. fprintf(stderr, "Error parsing Vorbis stream headers; corrupt stream?\n");
  330. clear();
  331. return;
  332. }
  333. vorbis_p++;
  334. if (vorbis_p == 3) break;
  335. }
  336. /* The header pages/packets will arrive before anything else we
  337. care about, or the stream is not obeying spec */
  338. if (ogg_sync_pageout(&oy, &og) > 0) {
  339. queue_page(&og); /* demux into the appropriate stream */
  340. } else {
  341. int ret = buffer_data(); /* someone needs more data */
  342. if (ret == 0) {
  343. fprintf(stderr, "End of file while searching for codec headers.\n");
  344. clear();
  345. return;
  346. }
  347. }
  348. }
  349. /* and now we have it all. initialize decoders */
  350. if (theora_p) {
  351. td = th_decode_alloc(&ti, ts);
  352. printf("Ogg logical stream %lx is Theora %dx%d %.02f fps",
  353. to.serialno, ti.pic_width, ti.pic_height,
  354. (double)ti.fps_numerator / ti.fps_denominator);
  355. px_fmt = ti.pixel_fmt;
  356. switch (ti.pixel_fmt) {
  357. case TH_PF_420: printf(" 4:2:0 video\n"); break;
  358. case TH_PF_422: printf(" 4:2:2 video\n"); break;
  359. case TH_PF_444: printf(" 4:4:4 video\n"); break;
  360. case TH_PF_RSVD:
  361. default:
  362. printf(" video\n (UNKNOWN Chroma sampling!)\n");
  363. break;
  364. }
  365. if (ti.pic_width != ti.frame_width || ti.pic_height != ti.frame_height)
  366. printf(" Frame content is %dx%d with offset (%d,%d).\n",
  367. ti.frame_width, ti.frame_height, ti.pic_x, ti.pic_y);
  368. th_decode_ctl(td, TH_DECCTL_GET_PPLEVEL_MAX, &pp_level_max,
  369. sizeof(pp_level_max));
  370. pp_level = pp_level_max;
  371. pp_level = 0;
  372. th_decode_ctl(td, TH_DECCTL_SET_PPLEVEL, &pp_level, sizeof(pp_level));
  373. pp_inc = 0;
  374. /*{
  375. int arg = 0xffff;
  376. th_decode_ctl(td,TH_DECCTL_SET_TELEMETRY_MBMODE,&arg,sizeof(arg));
  377. th_decode_ctl(td,TH_DECCTL_SET_TELEMETRY_MV,&arg,sizeof(arg));
  378. th_decode_ctl(td,TH_DECCTL_SET_TELEMETRY_QI,&arg,sizeof(arg));
  379. arg=10;
  380. th_decode_ctl(td,TH_DECCTL_SET_TELEMETRY_BITS,&arg,sizeof(arg));
  381. }*/
  382. int w;
  383. int h;
  384. w = (ti.pic_x + ti.frame_width + 1 & ~1) - (ti.pic_x & ~1);
  385. h = (ti.pic_y + ti.frame_height + 1 & ~1) - (ti.pic_y & ~1);
  386. size.x = w;
  387. size.y = h;
  388. texture->create(w, h, Image::FORMAT_RGBA, Texture::FLAG_FILTER | Texture::FLAG_VIDEO_SURFACE);
  389. } else {
  390. /* tear down the partial theora setup */
  391. th_info_clear(&ti);
  392. th_comment_clear(&tc);
  393. }
  394. th_setup_free(ts);
  395. if (vorbis_p) {
  396. vorbis_synthesis_init(&vd, &vi);
  397. vorbis_block_init(&vd, &vb);
  398. fprintf(stderr, "Ogg logical stream %lx is Vorbis %d channel %ld Hz audio.\n",
  399. vo.serialno, vi.channels, vi.rate);
  400. //_setup(vi.channels, vi.rate);
  401. } else {
  402. /* tear down the partial vorbis setup */
  403. vorbis_info_clear(&vi);
  404. vorbis_comment_clear(&vc);
  405. }
  406. playing = false;
  407. buffering = true;
  408. time = 0;
  409. audio_frames_wrote = 0;
  410. };
  411. float VideoStreamPlaybackTheora::get_time() const {
  412. //print_line("total: "+itos(get_total())+" todo: "+itos(get_todo()));
  413. //return MAX(0,time-((get_total())/(float)vi.rate));
  414. return time - AudioServer::get_singleton()->get_output_delay() - delay_compensation; //-((get_total())/(float)vi.rate);
  415. };
  416. Ref<Texture> VideoStreamPlaybackTheora::get_texture() {
  417. return texture;
  418. }
  419. void VideoStreamPlaybackTheora::update(float p_delta) {
  420. if (!file)
  421. return;
  422. if (!playing || paused) {
  423. //printf("not playing\n");
  424. return;
  425. };
  426. #ifdef THEORA_USE_THREAD_STREAMING
  427. thread_sem->post();
  428. #endif
  429. //double ctime =AudioServer::get_singleton()->get_mix_time();
  430. //print_line("play "+rtos(p_delta));
  431. time += p_delta;
  432. if (videobuf_time > get_time()) {
  433. return; //no new frames need to be produced
  434. }
  435. bool frame_done = false;
  436. bool audio_done = !vorbis_p;
  437. while (!frame_done || (!audio_done && !vorbis_eos)) {
  438. //a frame needs to be produced
  439. ogg_packet op;
  440. bool no_theora = false;
  441. while (vorbis_p) {
  442. int ret;
  443. float **pcm;
  444. bool buffer_full = false;
  445. /* if there's pending, decoded audio, grab it */
  446. if ((ret = vorbis_synthesis_pcmout(&vd, &pcm)) > 0) {
  447. const int AUXBUF_LEN = 4096;
  448. int to_read = ret;
  449. int16_t aux_buffer[AUXBUF_LEN];
  450. while (to_read) {
  451. int m = MIN(AUXBUF_LEN / vi.channels, to_read);
  452. int count = 0;
  453. for (int j = 0; j < m; j++) {
  454. for (int i = 0; i < vi.channels; i++) {
  455. int val = Math::fast_ftoi(pcm[i][j] * 32767.f);
  456. if (val > 32767) val = 32767;
  457. if (val < -32768) val = -32768;
  458. aux_buffer[count++] = val;
  459. }
  460. }
  461. if (mix_callback) {
  462. int mixed = mix_callback(mix_udata, aux_buffer, m);
  463. to_read -= mixed;
  464. if (mixed != m) { //could mix no more
  465. buffer_full = true;
  466. break;
  467. }
  468. } else {
  469. to_read -= m; //just pretend we sent the audio
  470. }
  471. }
  472. int tr = vorbis_synthesis_read(&vd, ret - to_read);
  473. if (vd.granulepos >= 0) {
  474. // print_line("wrote: "+itos(audio_frames_wrote)+" gpos: "+itos(vd.granulepos));
  475. }
  476. //print_line("mix audio!");
  477. audio_frames_wrote += ret - to_read;
  478. //print_line("AGP: "+itos(vd.granulepos)+" added "+itos(ret-to_read));
  479. } else {
  480. /* no pending audio; is there a pending packet to decode? */
  481. if (ogg_stream_packetout(&vo, &op) > 0) {
  482. if (vorbis_synthesis(&vb, &op) == 0) { /* test for success! */
  483. vorbis_synthesis_blockin(&vd, &vb);
  484. }
  485. } else { /* we need more data; break out to suck in another page */
  486. //printf("need moar data\n");
  487. break;
  488. };
  489. }
  490. audio_done = videobuf_time < (audio_frames_wrote / float(vi.rate));
  491. if (buffer_full)
  492. break;
  493. }
  494. while (theora_p && !frame_done) {
  495. /* theora is one in, one out... */
  496. if (ogg_stream_packetout(&to, &op) > 0) {
  497. if (false && pp_inc) {
  498. pp_level += pp_inc;
  499. th_decode_ctl(td, TH_DECCTL_SET_PPLEVEL, &pp_level,
  500. sizeof(pp_level));
  501. pp_inc = 0;
  502. }
  503. /*HACK: This should be set after a seek or a gap, but we might not have
  504. a granulepos for the first packet (we only have them for the last
  505. packet on a page), so we just set it as often as we get it.
  506. To do this right, we should back-track from the last packet on the
  507. page and compute the correct granulepos for the first packet after
  508. a seek or a gap.*/
  509. if (op.granulepos >= 0) {
  510. th_decode_ctl(td, TH_DECCTL_SET_GRANPOS, &op.granulepos,
  511. sizeof(op.granulepos));
  512. }
  513. ogg_int64_t videobuf_granulepos;
  514. if (th_decode_packetin(td, &op, &videobuf_granulepos) == 0) {
  515. videobuf_time = th_granule_time(td, videobuf_granulepos);
  516. //printf("frame time %f, play time %f, ready %i\n", (float)videobuf_time, get_time(), videobuf_ready);
  517. /* is it already too old to be useful? This is only actually
  518. useful cosmetically after a SIGSTOP. Note that we have to
  519. decode the frame even if we don't show it (for now) due to
  520. keyframing. Soon enough libtheora will be able to deal
  521. with non-keyframe seeks. */
  522. if (videobuf_time >= get_time()) {
  523. frame_done = true;
  524. } else {
  525. /*If we are too slow, reduce the pp level.*/
  526. pp_inc = pp_level > 0 ? -1 : 0;
  527. }
  528. } else {
  529. }
  530. } else {
  531. no_theora = true;
  532. break;
  533. }
  534. }
  535. //print_line("no theora: "+itos(no_theora)+" theora eos: "+itos(theora_eos)+" frame done "+itos(frame_done));
  536. #ifdef THEORA_USE_THREAD_STREAMING
  537. if (file && thread_eof && no_theora && theora_eos && ring_buffer.data_left() == 0) {
  538. #else
  539. if (file && /*!videobuf_ready && */ no_theora && theora_eos) {
  540. #endif
  541. printf("video done, stopping\n");
  542. stop();
  543. return;
  544. };
  545. #if 0
  546. if (!videobuf_ready || audio_todo > 0){
  547. /* no data yet for somebody. Grab another page */
  548. buffer_data();
  549. while(ogg_sync_pageout(&oy,&og)>0){
  550. queue_page(&og);
  551. }
  552. }
  553. #else
  554. if (!frame_done || !audio_done) {
  555. //what's the point of waiting for audio to grab a page?
  556. buffer_data();
  557. while (ogg_sync_pageout(&oy, &og) > 0) {
  558. queue_page(&og);
  559. }
  560. }
  561. #endif
  562. /* If playback has begun, top audio buffer off immediately. */
  563. //if(stateflag) audio_write_nonblocking();
  564. /* are we at or past time for this video frame? */
  565. if (videobuf_ready && videobuf_time <= get_time()) {
  566. //video_write();
  567. //videobuf_ready=0;
  568. } else {
  569. //printf("frame at %f not ready (time %f), ready %i\n", (float)videobuf_time, get_time(), videobuf_ready);
  570. }
  571. float tdiff = videobuf_time - get_time();
  572. /*If we have lots of extra time, increase the post-processing level.*/
  573. if (tdiff > ti.fps_denominator * 0.25 / ti.fps_numerator) {
  574. pp_inc = pp_level < pp_level_max ? 1 : 0;
  575. } else if (tdiff < ti.fps_denominator * 0.05 / ti.fps_numerator) {
  576. pp_inc = pp_level > 0 ? -1 : 0;
  577. }
  578. }
  579. video_write();
  580. };
  581. void VideoStreamPlaybackTheora::play() {
  582. if (!playing)
  583. time = 0;
  584. else {
  585. stop();
  586. }
  587. playing = true;
  588. delay_compensation = Globals::get_singleton()->get("audio/video_delay_compensation_ms");
  589. delay_compensation /= 1000.0;
  590. };
  591. void VideoStreamPlaybackTheora::stop() {
  592. if (playing) {
  593. clear();
  594. set_file(file_name); //reset
  595. }
  596. playing = false;
  597. time = 0;
  598. };
  599. bool VideoStreamPlaybackTheora::is_playing() const {
  600. return playing;
  601. };
  602. void VideoStreamPlaybackTheora::set_paused(bool p_paused) {
  603. paused = p_paused;
  604. //pau = !p_paused;
  605. };
  606. bool VideoStreamPlaybackTheora::is_paused(bool p_paused) const {
  607. return paused;
  608. };
  609. void VideoStreamPlaybackTheora::set_loop(bool p_enable){
  610. };
  611. bool VideoStreamPlaybackTheora::has_loop() const {
  612. return false;
  613. };
  614. float VideoStreamPlaybackTheora::get_length() const {
  615. return 0;
  616. };
  617. String VideoStreamPlaybackTheora::get_stream_name() const {
  618. return "";
  619. };
  620. int VideoStreamPlaybackTheora::get_loop_count() const {
  621. return 0;
  622. };
  623. float VideoStreamPlaybackTheora::get_pos() const {
  624. return get_time();
  625. };
  626. void VideoStreamPlaybackTheora::seek_pos(float p_time){
  627. // no
  628. };
  629. void VideoStreamPlaybackTheora::set_mix_callback(AudioMixCallback p_callback, void *p_userdata) {
  630. mix_callback = p_callback;
  631. mix_udata = p_userdata;
  632. }
  633. int VideoStreamPlaybackTheora::get_channels() const {
  634. return vi.channels;
  635. }
  636. void VideoStreamPlaybackTheora::set_audio_track(int p_idx) {
  637. audio_track = p_idx;
  638. }
  639. int VideoStreamPlaybackTheora::get_mix_rate() const {
  640. return vi.rate;
  641. }
  642. #ifdef THEORA_USE_THREAD_STREAMING
  643. void VideoStreamPlaybackTheora::_streaming_thread(void *ud) {
  644. VideoStreamPlaybackTheora *vs = (VideoStreamPlaybackTheora *)ud;
  645. while (!vs->thread_exit) {
  646. //just fill back the buffer
  647. if (!vs->thread_eof) {
  648. int to_read = vs->ring_buffer.space_left();
  649. if (to_read) {
  650. int read = vs->file->get_buffer(vs->read_buffer.ptr(), to_read);
  651. vs->ring_buffer.write(vs->read_buffer.ptr(), read);
  652. vs->thread_eof = vs->file->eof_reached();
  653. }
  654. }
  655. vs->thread_sem->wait();
  656. }
  657. }
  658. #endif
  659. VideoStreamPlaybackTheora::VideoStreamPlaybackTheora() {
  660. file = NULL;
  661. theora_p = 0;
  662. vorbis_p = 0;
  663. videobuf_ready = 0;
  664. playing = false;
  665. frames_pending = 0;
  666. videobuf_time = 0;
  667. paused = false;
  668. buffering = false;
  669. texture = Ref<ImageTexture>(memnew(ImageTexture));
  670. mix_callback = NULL;
  671. mix_udata = NULL;
  672. audio_track = 0;
  673. delay_compensation = 0;
  674. audio_frames_wrote = 0;
  675. #ifdef THEORA_USE_THREAD_STREAMING
  676. int rb_power = nearest_shift(RB_SIZE_KB * 1024);
  677. ring_buffer.resize(rb_power);
  678. read_buffer.resize(RB_SIZE_KB * 1024);
  679. thread_sem = Semaphore::create();
  680. thread = NULL;
  681. thread_exit = false;
  682. thread_eof = false;
  683. #endif
  684. };
  685. VideoStreamPlaybackTheora::~VideoStreamPlaybackTheora() {
  686. #ifdef THEORA_USE_THREAD_STREAMING
  687. memdelete(thread_sem);
  688. #endif
  689. clear();
  690. if (file)
  691. memdelete(file);
  692. };
  693. RES ResourceFormatLoaderVideoStreamTheora::load(const String &p_path, const String &p_original_path, Error *r_error) {
  694. if (r_error)
  695. *r_error = ERR_FILE_CANT_OPEN;
  696. VideoStreamTheora *stream = memnew(VideoStreamTheora);
  697. stream->set_file(p_path);
  698. if (r_error)
  699. *r_error = OK;
  700. return Ref<VideoStreamTheora>(stream);
  701. }
  702. void ResourceFormatLoaderVideoStreamTheora::get_recognized_extensions(List<String> *p_extensions) const {
  703. p_extensions->push_back("ogm");
  704. p_extensions->push_back("ogv");
  705. }
  706. bool ResourceFormatLoaderVideoStreamTheora::handles_type(const String &p_type) const {
  707. return (p_type == "VideoStream" || p_type == "VideoStreamTheora");
  708. }
  709. String ResourceFormatLoaderVideoStreamTheora::get_resource_type(const String &p_path) const {
  710. String exl = p_path.extension().to_lower();
  711. if (exl == "ogm" || exl == "ogv")
  712. return "VideoStreamTheora";
  713. return "";
  714. }