video_stream_theora.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. int buffer_data();
  48. int queue_page(ogg_page *page);
  49. void video_write(th_ycbcr_buffer yuv);
  50. double get_time() const;
  51. bool theora_eos = false;
  52. bool vorbis_eos = false;
  53. ogg_sync_state oy;
  54. ogg_page og;
  55. ogg_stream_state vo;
  56. ogg_stream_state to;
  57. th_info ti;
  58. th_comment tc;
  59. th_dec_ctx *td = nullptr;
  60. vorbis_info vi = {};
  61. vorbis_dsp_state vd;
  62. vorbis_block vb;
  63. vorbis_comment vc;
  64. th_pixel_fmt px_fmt;
  65. double frame_duration;
  66. int theora_p = 0;
  67. int vorbis_p = 0;
  68. int pp_level_max = 0;
  69. int pp_level = 0;
  70. int pp_inc = 0;
  71. bool playing = false;
  72. bool buffering = false;
  73. bool paused = false;
  74. bool dup_frame = false;
  75. bool video_ready = false;
  76. bool video_done = false;
  77. bool audio_done = false;
  78. double time = 0;
  79. double next_frame_time = 0;
  80. double current_frame_time = 0;
  81. double delay_compensation = 0;
  82. Ref<ImageTexture> texture;
  83. int audio_track = 0;
  84. protected:
  85. void clear();
  86. public:
  87. virtual void play() override;
  88. virtual void stop() override;
  89. virtual bool is_playing() const override;
  90. virtual void set_paused(bool p_paused) override;
  91. virtual bool is_paused() const override;
  92. virtual double get_length() const override;
  93. virtual double get_playback_position() const override;
  94. virtual void seek(double p_time) override;
  95. void set_file(const String &p_file);
  96. virtual Ref<Texture2D> get_texture() const override;
  97. virtual void update(double p_delta) override;
  98. virtual int get_channels() const override;
  99. virtual int get_mix_rate() const override;
  100. virtual void set_audio_track(int p_idx) override;
  101. VideoStreamPlaybackTheora();
  102. ~VideoStreamPlaybackTheora();
  103. };
  104. class VideoStreamTheora : public VideoStream {
  105. GDCLASS(VideoStreamTheora, VideoStream);
  106. protected:
  107. static void _bind_methods();
  108. public:
  109. Ref<VideoStreamPlayback> instantiate_playback() override {
  110. Ref<VideoStreamPlaybackTheora> pb = memnew(VideoStreamPlaybackTheora);
  111. pb->set_audio_track(audio_track);
  112. pb->set_file(file);
  113. return pb;
  114. }
  115. void set_audio_track(int p_track) override { audio_track = p_track; }
  116. VideoStreamTheora() { audio_track = 0; }
  117. };
  118. class ResourceFormatLoaderTheora : public ResourceFormatLoader {
  119. public:
  120. 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;
  121. virtual void get_recognized_extensions(List<String> *p_extensions) const override;
  122. virtual bool handles_type(const String &p_type) const override;
  123. virtual String get_resource_type(const String &p_path) const override;
  124. };