| 1 | /**************************************************************************/ | 
|---|
| 2 | /*  video_stream_theora.h                                                 */ | 
|---|
| 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 |  | 
|---|
| 31 | #ifndef VIDEO_STREAM_THEORA_H | 
|---|
| 32 | #define VIDEO_STREAM_THEORA_H | 
|---|
| 33 |  | 
|---|
| 34 | #include "core/io/file_access.h" | 
|---|
| 35 | #include "core/io/resource_loader.h" | 
|---|
| 36 | #include "core/os/semaphore.h" | 
|---|
| 37 | #include "core/os/thread.h" | 
|---|
| 38 | #include "core/templates/ring_buffer.h" | 
|---|
| 39 | #include "core/templates/safe_refcount.h" | 
|---|
| 40 | #include "scene/resources/video_stream.h" | 
|---|
| 41 | #include "servers/audio_server.h" | 
|---|
| 42 |  | 
|---|
| 43 | #include <theora/theoradec.h> | 
|---|
| 44 | #include <vorbis/codec.h> | 
|---|
| 45 |  | 
|---|
| 46 | class ImageTexture; | 
|---|
| 47 |  | 
|---|
| 48 | //#define THEORA_USE_THREAD_STREAMING | 
|---|
| 49 |  | 
|---|
| 50 | class VideoStreamPlaybackTheora : public VideoStreamPlayback { | 
|---|
| 51 | GDCLASS(VideoStreamPlaybackTheora, VideoStreamPlayback); | 
|---|
| 52 |  | 
|---|
| 53 | enum { | 
|---|
| 54 | MAX_FRAMES = 4, | 
|---|
| 55 | }; | 
|---|
| 56 |  | 
|---|
| 57 | //Image frames[MAX_FRAMES]; | 
|---|
| 58 | Image::Format format = Image::Format::FORMAT_L8; | 
|---|
| 59 | Vector<uint8_t> frame_data; | 
|---|
| 60 | int frames_pending = 0; | 
|---|
| 61 | Ref<FileAccess> file; | 
|---|
| 62 | String file_name; | 
|---|
| 63 | int audio_frames_wrote = 0; | 
|---|
| 64 | Point2i size; | 
|---|
| 65 |  | 
|---|
| 66 | int buffer_data(); | 
|---|
| 67 | int queue_page(ogg_page *page); | 
|---|
| 68 | void video_write(); | 
|---|
| 69 | double get_time() const; | 
|---|
| 70 |  | 
|---|
| 71 | bool theora_eos = false; | 
|---|
| 72 | bool vorbis_eos = false; | 
|---|
| 73 |  | 
|---|
| 74 | ogg_sync_state oy; | 
|---|
| 75 | ogg_page og; | 
|---|
| 76 | ogg_stream_state vo; | 
|---|
| 77 | ogg_stream_state to; | 
|---|
| 78 | th_info ti; | 
|---|
| 79 | th_comment tc; | 
|---|
| 80 | th_dec_ctx *td = nullptr; | 
|---|
| 81 | vorbis_info vi = {}; | 
|---|
| 82 | vorbis_dsp_state vd; | 
|---|
| 83 | vorbis_block vb; | 
|---|
| 84 | vorbis_comment vc; | 
|---|
| 85 | th_pixel_fmt px_fmt; | 
|---|
| 86 | double videobuf_time = 0; | 
|---|
| 87 | int pp_inc = 0; | 
|---|
| 88 |  | 
|---|
| 89 | int theora_p = 0; | 
|---|
| 90 | int vorbis_p = 0; | 
|---|
| 91 | int pp_level_max = 0; | 
|---|
| 92 | int pp_level = 0; | 
|---|
| 93 | int videobuf_ready = 0; | 
|---|
| 94 |  | 
|---|
| 95 | bool playing = false; | 
|---|
| 96 | bool buffering = false; | 
|---|
| 97 |  | 
|---|
| 98 | double last_update_time = 0; | 
|---|
| 99 | double time = 0; | 
|---|
| 100 | double delay_compensation = 0; | 
|---|
| 101 |  | 
|---|
| 102 | Ref<ImageTexture> texture; | 
|---|
| 103 |  | 
|---|
| 104 | bool paused = false; | 
|---|
| 105 |  | 
|---|
| 106 | #ifdef THEORA_USE_THREAD_STREAMING | 
|---|
| 107 |  | 
|---|
| 108 | enum { | 
|---|
| 109 | RB_SIZE_KB = 1024 | 
|---|
| 110 | }; | 
|---|
| 111 |  | 
|---|
| 112 | RingBuffer<uint8_t> ring_buffer; | 
|---|
| 113 | Vector<uint8_t> read_buffer; | 
|---|
| 114 | bool thread_eof = false; | 
|---|
| 115 | Semaphore *thread_sem = nullptr; | 
|---|
| 116 | Thread thread; | 
|---|
| 117 | SafeFlag thread_exit; | 
|---|
| 118 |  | 
|---|
| 119 | static void _streaming_thread(void *ud); | 
|---|
| 120 |  | 
|---|
| 121 | #endif | 
|---|
| 122 |  | 
|---|
| 123 | int audio_track = 0; | 
|---|
| 124 |  | 
|---|
| 125 | protected: | 
|---|
| 126 | void clear(); | 
|---|
| 127 |  | 
|---|
| 128 | public: | 
|---|
| 129 | virtual void play() override; | 
|---|
| 130 | virtual void stop() override; | 
|---|
| 131 | virtual bool is_playing() const override; | 
|---|
| 132 |  | 
|---|
| 133 | virtual void set_paused(bool p_paused) override; | 
|---|
| 134 | virtual bool is_paused() const override; | 
|---|
| 135 |  | 
|---|
| 136 | virtual double get_length() const override; | 
|---|
| 137 |  | 
|---|
| 138 | virtual double get_playback_position() const override; | 
|---|
| 139 | virtual void seek(double p_time) override; | 
|---|
| 140 |  | 
|---|
| 141 | void set_file(const String &p_file); | 
|---|
| 142 |  | 
|---|
| 143 | virtual Ref<Texture2D> get_texture() const override; | 
|---|
| 144 | virtual void update(double p_delta) override; | 
|---|
| 145 |  | 
|---|
| 146 | virtual int get_channels() const override; | 
|---|
| 147 | virtual int get_mix_rate() const override; | 
|---|
| 148 |  | 
|---|
| 149 | virtual void set_audio_track(int p_idx) override; | 
|---|
| 150 |  | 
|---|
| 151 | VideoStreamPlaybackTheora(); | 
|---|
| 152 | ~VideoStreamPlaybackTheora(); | 
|---|
| 153 | }; | 
|---|
| 154 |  | 
|---|
| 155 | class VideoStreamTheora : public VideoStream { | 
|---|
| 156 | GDCLASS(VideoStreamTheora, VideoStream); | 
|---|
| 157 |  | 
|---|
| 158 | protected: | 
|---|
| 159 | static void _bind_methods(); | 
|---|
| 160 |  | 
|---|
| 161 | public: | 
|---|
| 162 | Ref<VideoStreamPlayback> instantiate_playback() override { | 
|---|
| 163 | Ref<VideoStreamPlaybackTheora> pb = memnew(VideoStreamPlaybackTheora); | 
|---|
| 164 | pb->set_audio_track(audio_track); | 
|---|
| 165 | pb->set_file(file); | 
|---|
| 166 | return pb; | 
|---|
| 167 | } | 
|---|
| 168 |  | 
|---|
| 169 | void set_audio_track(int p_track) override { audio_track = p_track; } | 
|---|
| 170 |  | 
|---|
| 171 | VideoStreamTheora() { audio_track = 0; } | 
|---|
| 172 | }; | 
|---|
| 173 |  | 
|---|
| 174 | class ResourceFormatLoaderTheora : public ResourceFormatLoader { | 
|---|
| 175 | public: | 
|---|
| 176 | virtual Ref<Resource> load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr, bool p_use_sub_threads = false, float *r_progress = nullptr, CacheMode p_cache_mode = CACHE_MODE_REUSE); | 
|---|
| 177 | virtual void get_recognized_extensions(List<String> *p_extensions) const; | 
|---|
| 178 | virtual bool handles_type(const String &p_type) const; | 
|---|
| 179 | virtual String get_resource_type(const String &p_path) const; | 
|---|
| 180 | }; | 
|---|
| 181 |  | 
|---|
| 182 | #endif // VIDEO_STREAM_THEORA_H | 
|---|
| 183 |  | 
|---|