video_stream_theora.h 5.7 KB

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