video_stream_theora.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. #pragma once
  31. #include "core/io/file_access.h"
  32. #include "core/io/resource_loader.h"
  33. #include "core/os/thread.h"
  34. #include "scene/resources/video_stream.h"
  35. #include <theora/theoradec.h>
  36. #include <vorbis/codec.h>
  37. class ImageTexture;
  38. class VideoStreamPlaybackTheora : public VideoStreamPlayback {
  39. GDCLASS(VideoStreamPlaybackTheora, VideoStreamPlayback);
  40. Image::Format format = Image::Format::FORMAT_L8;
  41. Vector<uint8_t> frame_data;
  42. int frames_pending = 0;
  43. Ref<FileAccess> file;
  44. String file_name;
  45. Point2i size;
  46. Rect2i region;
  47. float *audio_buffer = nullptr;
  48. int audio_buffer_size = 0;
  49. int audio_ptr_start = 0;
  50. int audio_ptr_end = 0;
  51. int buffer_data();
  52. int queue_page(ogg_page *page);
  53. int read_page(ogg_page *page);
  54. int feed_pages();
  55. double get_page_time(ogg_page *page);
  56. int64_t seek_streams(double p_time, int64_t &video_granulepos, int64_t &audio_granulepos);
  57. void find_streams(th_setup_info *&ts);
  58. void read_headers(th_setup_info *&ts);
  59. void video_write(th_ycbcr_buffer yuv);
  60. double get_time() const;
  61. bool theora_eos = false;
  62. bool vorbis_eos = false;
  63. ogg_sync_state oy;
  64. ogg_stream_state vo;
  65. ogg_stream_state to;
  66. th_info ti;
  67. th_comment tc;
  68. th_dec_ctx *td = nullptr;
  69. vorbis_info vi = {};
  70. vorbis_dsp_state vd;
  71. vorbis_block vb;
  72. vorbis_comment vc;
  73. th_pixel_fmt px_fmt;
  74. double frame_duration = 0;
  75. double stream_length = 0;
  76. int64_t stream_data_offset = 0;
  77. int64_t stream_data_size = 0;
  78. int pp_level_max = 0;
  79. int pp_level = 0;
  80. int pp_inc = 0;
  81. bool playing = false;
  82. bool paused = false;
  83. bool dup_frame = false;
  84. bool has_video = false;
  85. bool has_audio = false;
  86. bool video_ready = false;
  87. bool video_done = false;
  88. bool audio_done = false;
  89. double time = 0;
  90. double next_frame_time = 0;
  91. double current_frame_time = 0;
  92. double delay_compensation = 0;
  93. Ref<ImageTexture> texture;
  94. int audio_track = 0;
  95. protected:
  96. void clear();
  97. _FORCE_INLINE_ bool send_audio() {
  98. if (audio_ptr_end > 0) {
  99. int mixed = mix_callback(mix_udata, &audio_buffer[audio_ptr_start * vi.channels], audio_ptr_end - audio_ptr_start);
  100. audio_ptr_start += mixed;
  101. if (audio_ptr_start == audio_ptr_end) {
  102. audio_ptr_start = 0;
  103. audio_ptr_end = 0;
  104. } else {
  105. return false;
  106. }
  107. }
  108. return true;
  109. }
  110. public:
  111. virtual void play() override;
  112. virtual void stop() override;
  113. virtual bool is_playing() const override;
  114. virtual void set_paused(bool p_paused) override;
  115. virtual bool is_paused() const override;
  116. virtual double get_length() const override;
  117. virtual double get_playback_position() const override;
  118. virtual void seek(double p_time) override;
  119. void set_file(const String &p_file);
  120. virtual Ref<Texture2D> get_texture() const override;
  121. virtual void update(double p_delta) override;
  122. virtual int get_channels() const override;
  123. virtual int get_mix_rate() const override;
  124. virtual void set_audio_track(int p_idx) override;
  125. VideoStreamPlaybackTheora();
  126. ~VideoStreamPlaybackTheora();
  127. };
  128. class VideoStreamTheora : public VideoStream {
  129. GDCLASS(VideoStreamTheora, VideoStream);
  130. protected:
  131. static void _bind_methods();
  132. public:
  133. Ref<VideoStreamPlayback> instantiate_playback() override {
  134. Ref<VideoStreamPlaybackTheora> pb = memnew(VideoStreamPlaybackTheora);
  135. pb->set_audio_track(audio_track);
  136. pb->set_file(file);
  137. return pb;
  138. }
  139. void set_audio_track(int p_track) override { audio_track = p_track; }
  140. VideoStreamTheora() { audio_track = 0; }
  141. };
  142. class ResourceFormatLoaderTheora : public ResourceFormatLoader {
  143. public:
  144. 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;
  145. virtual void get_recognized_extensions(List<String> *p_extensions) const override;
  146. virtual bool handles_type(const String &p_type) const override;
  147. virtual String get_resource_type(const String &p_path) const override;
  148. };