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