video_stream_webm.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /*************************************************************************/
  2. /* av_stream_webm.cpp.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "video_stream_webm.h"
  31. #include "OpusVorbisDecoder.hpp"
  32. #include "VPXDecoder.hpp"
  33. #include "mkvparser/mkvparser.h"
  34. #include "global_config.h"
  35. #include "os/file_access.h"
  36. #include "thirdparty/misc/yuv2rgb.h"
  37. #include <string.h>
  38. class MkvReader : public mkvparser::IMkvReader {
  39. public:
  40. MkvReader(const String &p_file) {
  41. file = FileAccess::open(p_file, FileAccess::READ);
  42. ERR_FAIL_COND(!file);
  43. }
  44. ~MkvReader() {
  45. if (file)
  46. memdelete(file);
  47. }
  48. virtual int Read(long long pos, long len, unsigned char *buf) {
  49. if (file) {
  50. if (file->get_pos() != (size_t)pos)
  51. file->seek(pos);
  52. if (file->get_buffer(buf, len) == len)
  53. return 0;
  54. }
  55. return -1;
  56. }
  57. virtual int Length(long long *total, long long *available) {
  58. if (file) {
  59. const size_t len = file->get_len();
  60. if (total)
  61. *total = len;
  62. if (available)
  63. *available = len;
  64. return 0;
  65. }
  66. return -1;
  67. }
  68. private:
  69. FileAccess *file;
  70. };
  71. /**/
  72. VideoStreamPlaybackWebm::VideoStreamPlaybackWebm()
  73. : audio_track(0),
  74. webm(NULL),
  75. video(NULL),
  76. audio(NULL),
  77. video_frames(NULL), audio_frame(NULL),
  78. video_frames_pos(0), video_frames_capacity(0),
  79. num_decoded_samples(0), samples_offset(-1),
  80. mix_callback(NULL),
  81. mix_udata(NULL),
  82. playing(false), paused(false),
  83. delay_compensation(0.0),
  84. time(0.0), video_frame_delay(0.0), video_pos(0.0),
  85. texture(memnew(ImageTexture)),
  86. pcm(NULL) {}
  87. VideoStreamPlaybackWebm::~VideoStreamPlaybackWebm() {
  88. delete_pointers();
  89. }
  90. bool VideoStreamPlaybackWebm::open_file(const String &p_file) {
  91. file_name = p_file;
  92. webm = memnew(WebMDemuxer(new MkvReader(file_name), 0, audio_track));
  93. if (webm->isOpen()) {
  94. video = memnew(VPXDecoder(*webm, 8)); //TODO: Detect CPU threads
  95. if (video->isOpen()) {
  96. audio = memnew(OpusVorbisDecoder(*webm));
  97. if (audio->isOpen()) {
  98. audio_frame = memnew(WebMFrame);
  99. pcm = (int16_t *)memalloc(sizeof(int16_t) * audio->getBufferSamples() * webm->getChannels());
  100. } else {
  101. memdelete(audio);
  102. audio = NULL;
  103. }
  104. frame_data.resize((webm->getWidth() * webm->getHeight()) << 2);
  105. texture->create(webm->getWidth(), webm->getHeight(), Image::FORMAT_RGBA8, Texture::FLAG_FILTER | Texture::FLAG_VIDEO_SURFACE);
  106. return true;
  107. }
  108. memdelete(video);
  109. video = NULL;
  110. }
  111. memdelete(webm);
  112. webm = NULL;
  113. return false;
  114. }
  115. void VideoStreamPlaybackWebm::stop() {
  116. if (playing) {
  117. delete_pointers();
  118. pcm = NULL;
  119. audio_frame = NULL;
  120. video_frames = NULL;
  121. video = NULL;
  122. audio = NULL;
  123. open_file(file_name); //Should not fail here...
  124. video_frames_capacity = video_frames_pos = 0;
  125. num_decoded_samples = 0;
  126. samples_offset = -1;
  127. video_frame_delay = video_pos = 0.0;
  128. }
  129. time = 0.0;
  130. playing = false;
  131. }
  132. void VideoStreamPlaybackWebm::play() {
  133. stop();
  134. delay_compensation = GlobalConfig::get_singleton()->get("audio/video_delay_compensation_ms");
  135. delay_compensation /= 1000.0;
  136. playing = true;
  137. }
  138. bool VideoStreamPlaybackWebm::is_playing() const {
  139. return playing;
  140. }
  141. void VideoStreamPlaybackWebm::set_paused(bool p_paused) {
  142. paused = p_paused;
  143. }
  144. bool VideoStreamPlaybackWebm::is_paused(bool p_paused) const {
  145. return paused;
  146. }
  147. void VideoStreamPlaybackWebm::set_loop(bool p_enable) {
  148. //Empty
  149. }
  150. bool VideoStreamPlaybackWebm::has_loop() const {
  151. return false;
  152. }
  153. float VideoStreamPlaybackWebm::get_length() const {
  154. if (webm)
  155. return webm->getLength();
  156. return 0.0f;
  157. }
  158. float VideoStreamPlaybackWebm::get_pos() const {
  159. return video_pos;
  160. }
  161. void VideoStreamPlaybackWebm::seek_pos(float p_time) {
  162. //Not implemented
  163. }
  164. void VideoStreamPlaybackWebm::set_audio_track(int p_idx) {
  165. audio_track = p_idx;
  166. }
  167. Ref<Texture> VideoStreamPlaybackWebm::get_texture() {
  168. return texture;
  169. }
  170. void VideoStreamPlaybackWebm::update(float p_delta) {
  171. if ((!playing || paused) || !video)
  172. return;
  173. bool audio_buffer_full = false;
  174. if (samples_offset > -1) {
  175. //Mix remaining samples
  176. const int to_read = num_decoded_samples - samples_offset;
  177. const int mixed = mix_callback(mix_udata, pcm + samples_offset * webm->getChannels(), to_read);
  178. if (mixed != to_read) {
  179. samples_offset += mixed;
  180. audio_buffer_full = true;
  181. } else {
  182. samples_offset = -1;
  183. }
  184. }
  185. const bool hasAudio = (audio && mix_callback);
  186. while ((hasAudio && (!audio_buffer_full || !has_enough_video_frames())) || (!hasAudio && video_frames_pos == 0)) {
  187. if (hasAudio && !audio_buffer_full && audio_frame->isValid() && audio->getPCMS16(*audio_frame, pcm, num_decoded_samples) && num_decoded_samples > 0) {
  188. const int mixed = mix_callback(mix_udata, pcm, num_decoded_samples);
  189. if (mixed != num_decoded_samples) {
  190. samples_offset = mixed;
  191. audio_buffer_full = true;
  192. }
  193. }
  194. WebMFrame *video_frame;
  195. if (video_frames_pos >= video_frames_capacity) {
  196. WebMFrame **video_frames_new = (WebMFrame **)memrealloc(video_frames, ++video_frames_capacity * sizeof(void *));
  197. ERR_FAIL_COND(!video_frames_new); //Out of memory
  198. (video_frames = video_frames_new)[video_frames_capacity - 1] = memnew(WebMFrame);
  199. }
  200. video_frame = video_frames[video_frames_pos];
  201. if (!webm->readFrame(video_frame, audio_frame)) //This will invalidate frames
  202. break; //Can't demux, EOS?
  203. if (video_frame->isValid())
  204. ++video_frames_pos;
  205. };
  206. const double video_delay = video->getFramesDelay() * video_frame_delay;
  207. bool want_this_frame = false;
  208. while (video_frames_pos > 0 && !want_this_frame) {
  209. WebMFrame *video_frame = video_frames[0];
  210. if (video_frame->time <= time + video_delay) {
  211. if (video->decode(*video_frame)) {
  212. VPXDecoder::IMAGE_ERROR err;
  213. VPXDecoder::Image image;
  214. while ((err = video->getImage(image)) != VPXDecoder::NO_FRAME) {
  215. want_this_frame = (time - video_frame->time <= video_frame_delay);
  216. if (want_this_frame) {
  217. if (err == VPXDecoder::NO_ERROR && image.w == webm->getWidth() && image.h == webm->getHeight()) {
  218. PoolVector<uint8_t>::Write w = frame_data.write();
  219. bool converted = false;
  220. if (image.chromaShiftW == 1 && image.chromaShiftH == 1) {
  221. yuv420_2_rgb8888(w.ptr(), image.planes[0], image.planes[2], image.planes[1], image.w, image.h, image.linesize[0], image.linesize[1], image.w << 2, 0);
  222. // libyuv::I420ToARGB(image.planes[0], image.linesize[0], image.planes[2], image.linesize[2], image.planes[1], image.linesize[1], w.ptr(), image.w << 2, image.w, image.h);
  223. converted = true;
  224. } else if (image.chromaShiftW == 1 && image.chromaShiftH == 0) {
  225. yuv422_2_rgb8888(w.ptr(), image.planes[0], image.planes[2], image.planes[1], image.w, image.h, image.linesize[0], image.linesize[1], image.w << 2, 0);
  226. // libyuv::I422ToARGB(image.planes[0], image.linesize[0], image.planes[2], image.linesize[2], image.planes[1], image.linesize[1], w.ptr(), image.w << 2, image.w, image.h);
  227. converted = true;
  228. } else if (image.chromaShiftW == 0 && image.chromaShiftH == 0) {
  229. yuv444_2_rgb8888(w.ptr(), image.planes[0], image.planes[2], image.planes[1], image.w, image.h, image.linesize[0], image.linesize[1], image.w << 2, 0);
  230. // libyuv::I444ToARGB(image.planes[0], image.linesize[0], image.planes[2], image.linesize[2], image.planes[1], image.linesize[1], w.ptr(), image.w << 2, image.w, image.h);
  231. converted = true;
  232. } else if (image.chromaShiftW == 2 && image.chromaShiftH == 0) {
  233. // libyuv::I411ToARGB(image.planes[0], image.linesize[0], image.planes[2], image.linesize[2], image.planes[1], image.linesize[1], w.ptr(), image.w << 2, image.w, image.h);
  234. // converted = true;
  235. }
  236. if (converted)
  237. texture->set_data(Image(image.w, image.h, 0, Image::FORMAT_RGBA8, frame_data)); //Zero copy send to visual server
  238. }
  239. break;
  240. }
  241. }
  242. }
  243. video_frame_delay = video_frame->time - video_pos;
  244. video_pos = video_frame->time;
  245. memmove(video_frames, video_frames + 1, (--video_frames_pos) * sizeof(void *));
  246. video_frames[video_frames_pos] = video_frame;
  247. } else {
  248. break;
  249. }
  250. }
  251. time += p_delta;
  252. if (video_frames_pos == 0 && webm->isEOS())
  253. stop();
  254. }
  255. void VideoStreamPlaybackWebm::set_mix_callback(VideoStreamPlayback::AudioMixCallback p_callback, void *p_userdata) {
  256. mix_callback = p_callback;
  257. mix_udata = p_userdata;
  258. }
  259. int VideoStreamPlaybackWebm::get_channels() const {
  260. if (audio)
  261. return webm->getChannels();
  262. return 0;
  263. }
  264. int VideoStreamPlaybackWebm::get_mix_rate() const {
  265. if (audio)
  266. return webm->getSampleRate();
  267. return 0;
  268. }
  269. inline bool VideoStreamPlaybackWebm::has_enough_video_frames() const {
  270. if (video_frames_pos > 0) {
  271. const double audio_delay = AudioServer::get_singleton()->get_output_delay();
  272. const double video_time = video_frames[video_frames_pos - 1]->time;
  273. return video_time >= time + audio_delay + delay_compensation;
  274. }
  275. return false;
  276. }
  277. void VideoStreamPlaybackWebm::delete_pointers() {
  278. if (pcm)
  279. memfree(pcm);
  280. if (audio_frame)
  281. memdelete(audio_frame);
  282. for (int i = 0; i < video_frames_capacity; ++i)
  283. memdelete(video_frames[i]);
  284. if (video_frames)
  285. memfree(video_frames);
  286. if (video)
  287. memdelete(video);
  288. if (audio)
  289. memdelete(audio);
  290. if (webm)
  291. memdelete(webm);
  292. }
  293. /**/
  294. RES ResourceFormatLoaderVideoStreamWebm::load(const String &p_path, const String &p_original_path, Error *r_error) {
  295. Ref<VideoStreamWebm> stream = memnew(VideoStreamWebm);
  296. stream->set_file(p_path);
  297. if (r_error)
  298. *r_error = OK;
  299. return stream;
  300. }
  301. void ResourceFormatLoaderVideoStreamWebm::get_recognized_extensions(List<String> *p_extensions) const {
  302. p_extensions->push_back("webm");
  303. }
  304. bool ResourceFormatLoaderVideoStreamWebm::handles_type(const String &p_type) const {
  305. return (p_type == "VideoStream" || p_type == "VideoStreamWebm");
  306. }
  307. String ResourceFormatLoaderVideoStreamWebm::get_resource_type(const String &p_path) const {
  308. const String exl = p_path.get_extension().to_lower();
  309. if (exl == "webm")
  310. return "VideoStreamWebm";
  311. return "";
  312. }
  313. /**/
  314. VideoStreamWebm::VideoStreamWebm()
  315. : audio_track(0) {}
  316. Ref<VideoStreamPlayback> VideoStreamWebm::instance_playback() {
  317. Ref<VideoStreamPlaybackWebm> pb = memnew(VideoStreamPlaybackWebm);
  318. pb->set_audio_track(audio_track);
  319. if (pb->open_file(file))
  320. return pb;
  321. return NULL;
  322. }
  323. void VideoStreamWebm::set_file(const String &p_file) {
  324. file = p_file;
  325. }
  326. void VideoStreamWebm::set_audio_track(int p_track) {
  327. audio_track = p_track;
  328. }