Skip to content

Commit

Permalink
Implement post-processing for VideoStreamPlayer
Browse files Browse the repository at this point in the history
  • Loading branch information
berarma committed Feb 4, 2025
1 parent 426b9fd commit 40c80dd
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 4 deletions.
7 changes: 7 additions & 0 deletions doc/classes/VideoStreamPlayback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,12 @@
Render [param num_frames] audio frames (of [method _get_channels] floats each) from [param buffer], starting from index [param offset] in the array. Returns the number of audio frames rendered, or -1 on error.
</description>
</method>
<method name="_set_pp_level" qualifiers="virtual">
<return type="void" />
<param index="0" name="pp_level" type="int" />
<description>
Set the post-processing level [param pp_level]. Called when playback starts, and in response to the [member VideoStreamPlayer.pp_level] setter.
</description>
</method>
</methods>
</class>
3 changes: 3 additions & 0 deletions doc/classes/VideoStreamPlayer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@
<member name="volume_db" type="float" setter="set_volume_db" getter="get_volume_db" default="0.0">
Audio volume in dB.
</member>
<member name="pp_level" type="int" setter="set_pp_level" getter="get_pp_level" default="0">
Maximum post-processing level to use. Zero is disabled, the higher the more CPU is used.
</member>
</members>
<signals>
<signal name="finished">
Expand Down
23 changes: 20 additions & 3 deletions modules/theora/video_stream_theora.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,11 @@ void VideoStreamPlaybackTheora::update(double p_delta) {

while (!video_ready && !video_done) {
if (ogg_stream_packetout(&to, &op) > 0) {
if (pp_inc) {
pp_level += pp_inc;
th_decode_ctl(td, TH_DECCTL_SET_PPLEVEL, &pp_level, sizeof(pp_level));
pp_inc = 0;
}
if (op.granulepos >= 0) {
th_decode_ctl(td, TH_DECCTL_SET_GRANPOS, &op.granulepos, sizeof(op.granulepos));
}
Expand Down Expand Up @@ -555,9 +560,9 @@ void VideoStreamPlaybackTheora::update(double p_delta) {

double tdiff = next_frame_time - comp_time;
/*If we have lots of extra time, increase the post-processing level.*/
if (tdiff > ti.fps_denominator * 0.25 / ti.fps_numerator) {
pp_inc = pp_level < pp_level_max ? 1 : 0;
} else if (tdiff < ti.fps_denominator * 0.05 / ti.fps_numerator) {
if (tdiff > frame_duration * 0.25) {
pp_inc = pp_level < pp_level_max && pp_level < pp_level_requested ? 1 : 0;
} else if (tdiff < frame_duration * 0.05) {
pp_inc = pp_level > 0 ? -1 : 0;
}
}
Expand Down Expand Up @@ -678,6 +683,10 @@ void VideoStreamPlaybackTheora::seek(double p_time) {
int64_t granulepos = 1;
th_decode_ctl(td, TH_DECCTL_SET_GRANPOS, &granulepos, sizeof(granulepos));

// Set post-processing to lowest to be faster
pp_level = 0;
th_decode_ctl(td, TH_DECCTL_SET_PPLEVEL, &pp_level, sizeof(pp_level));

double last_audio_time = 0;
double last_video_time = 0;
bool first_frame_decoded = false;
Expand Down Expand Up @@ -736,6 +745,10 @@ void VideoStreamPlaybackTheora::seek(double p_time) {
th_decode_ycbcr_out(td, yuv);
video_write(yuv);
}

pp_level = pp_level_requested;
th_decode_ctl(td, TH_DECCTL_SET_PPLEVEL, &pp_level, sizeof(pp_level));
pp_inc = 0;
}

int VideoStreamPlaybackTheora::get_channels() const {
Expand All @@ -750,6 +763,10 @@ int VideoStreamPlaybackTheora::get_mix_rate() const {
return vi.rate;
}

void VideoStreamPlaybackTheora::set_pp_level(int p_pp_level) {
pp_level_requested = p_pp_level;
}

VideoStreamPlaybackTheora::VideoStreamPlaybackTheora() {
texture.instantiate();
}
Expand Down
9 changes: 8 additions & 1 deletion modules/theora/video_stream_theora.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class VideoStreamPlaybackTheora : public VideoStreamPlayback {
int pp_level_max = 0;
int pp_level = 0;
int pp_inc = 0;
int pp_level_requested = 0;

bool playing = false;
bool paused = false;
Expand Down Expand Up @@ -130,6 +131,7 @@ class VideoStreamPlaybackTheora : public VideoStreamPlayback {
virtual int get_mix_rate() const override;

virtual void set_audio_track(int p_idx) override;
virtual void set_pp_level(int p_pp_level) override;

VideoStreamPlaybackTheora();
~VideoStreamPlaybackTheora();
Expand All @@ -145,13 +147,18 @@ class VideoStreamTheora : public VideoStream {
Ref<VideoStreamPlayback> instantiate_playback() override {
Ref<VideoStreamPlaybackTheora> pb = memnew(VideoStreamPlaybackTheora);
pb->set_audio_track(audio_track);
pb->set_pp_level(pp_level);
pb->set_file(file);
return pb;
}

void set_audio_track(int p_track) override { audio_track = p_track; }
void set_pp_level(int p_pp_level) override { pp_level = p_pp_level; }

VideoStreamTheora() { audio_track = 0; }
VideoStreamTheora() {
audio_track = 0;
pp_level = 0;
}
};

class ResourceFormatLoaderTheora : public ResourceFormatLoader {
Expand Down
19 changes: 19 additions & 0 deletions scene/gui/video_stream_player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ void VideoStreamPlayer::set_stream(const Ref<VideoStream> &p_stream) {
stream = p_stream;
if (stream.is_valid()) {
stream->set_audio_track(audio_track);
stream->set_pp_level(pp_level);
playback = stream->instantiate_playback();
} else {
playback = Ref<VideoStreamPlayback>();
Expand Down Expand Up @@ -473,6 +474,20 @@ StringName VideoStreamPlayer::get_bus() const {
return SceneStringName(Master);
}

void VideoStreamPlayer::set_pp_level(int p_pp_level) {
pp_level = p_pp_level;
if (stream.is_valid()) {
stream->set_pp_level(pp_level);
}
if (playback.is_valid()) {
playback->set_pp_level(pp_level);
}
}

int VideoStreamPlayer::get_pp_level() const {
return pp_level;
}

void VideoStreamPlayer::_validate_property(PropertyInfo &p_property) const {
if (p_property.name == "bus") {
String options;
Expand Down Expand Up @@ -532,6 +547,9 @@ void VideoStreamPlayer::_bind_methods() {

ClassDB::bind_method(D_METHOD("get_video_texture"), &VideoStreamPlayer::get_video_texture);

ClassDB::bind_method(D_METHOD("set_pp_level", "pp_level"), &VideoStreamPlayer::set_pp_level);
ClassDB::bind_method(D_METHOD("get_pp_level"), &VideoStreamPlayer::get_pp_level);

ADD_SIGNAL(MethodInfo("finished"));

ADD_PROPERTY(PropertyInfo(Variant::INT, "audio_track", PROPERTY_HINT_RANGE, "0,128,1"), "set_audio_track", "get_audio_track");
Expand All @@ -546,6 +564,7 @@ void VideoStreamPlayer::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "stream_position", PROPERTY_HINT_RANGE, "0,1280000,0.1", PROPERTY_USAGE_NONE), "set_stream_position", "get_stream_position");

ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "bus", PROPERTY_HINT_ENUM, ""), "set_bus", "get_bus");
ADD_PROPERTY(PropertyInfo(Variant::INT, "pp_level", PROPERTY_HINT_RANGE, "0,256,1"), "set_pp_level", "get_pp_level");
}

VideoStreamPlayer::VideoStreamPlayer() {}
Expand Down
4 changes: 4 additions & 0 deletions scene/gui/video_stream_player.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class VideoStreamPlayer : public Control {
int buffering_ms = 500;
int audio_track = 0;
int bus_index = 0;
int pp_level = 0;

StringName bus;

Expand Down Expand Up @@ -123,6 +124,9 @@ class VideoStreamPlayer : public Control {
void set_bus(const StringName &p_bus);
StringName get_bus() const;

void set_pp_level(int p_pp_level);
int get_pp_level() const;

VideoStreamPlayer();
~VideoStreamPlayer();
};
Expand Down
10 changes: 10 additions & 0 deletions scene/resources/video_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ void VideoStreamPlayback::_bind_methods() {
GDVIRTUAL_BIND(_get_playback_position);
GDVIRTUAL_BIND(_seek, "time");
GDVIRTUAL_BIND(_set_audio_track, "idx");
GDVIRTUAL_BIND(_set_pp_level, "pp_level");
GDVIRTUAL_BIND(_get_texture);
GDVIRTUAL_BIND(_update, "delta");
GDVIRTUAL_BIND(_get_channels);
Expand Down Expand Up @@ -107,6 +108,10 @@ void VideoStreamPlayback::set_audio_track(int p_idx) {
GDVIRTUAL_CALL(_set_audio_track, p_idx);
}

void VideoStreamPlayback::set_pp_level(int p_pp_level) {
GDVIRTUAL_CALL(_set_pp_level, p_pp_level);
}

Ref<Texture2D> VideoStreamPlayback::get_texture() const {
Ref<Texture2D> ret;
if (GDVIRTUAL_CALL(_get_texture, ret)) {
Expand Down Expand Up @@ -160,6 +165,7 @@ Ref<VideoStreamPlayback> VideoStream::instantiate_playback() {
if (GDVIRTUAL_CALL(_instantiate_playback, ret)) {
ERR_FAIL_COND_V_MSG(ret.is_null(), nullptr, "Plugin returned null playback");
ret->set_audio_track(audio_track);
ret->set_pp_level(pp_level);
return ret;
}
return nullptr;
Expand Down Expand Up @@ -192,3 +198,7 @@ VideoStream::~VideoStream() {
void VideoStream::set_audio_track(int p_track) {
audio_track = p_track;
}

void VideoStream::set_pp_level(int p_pp_level) {
pp_level = p_pp_level;
}
5 changes: 5 additions & 0 deletions scene/resources/video_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class VideoStreamPlayback : public Resource {
GDVIRTUAL0RC(double, _get_playback_position);
GDVIRTUAL1(_seek, double);
GDVIRTUAL1(_set_audio_track, int);
GDVIRTUAL1(_set_pp_level, int);
GDVIRTUAL0RC(Ref<Texture2D>, _get_texture);
GDVIRTUAL1_REQUIRED(_update, double);
GDVIRTUAL0RC(int, _get_channels);
Expand All @@ -80,6 +81,8 @@ class VideoStreamPlayback : public Resource {

virtual void set_audio_track(int p_idx);

virtual void set_pp_level(int p_pp_level);

virtual Ref<Texture2D> get_texture() const;
virtual void update(double p_delta);

Expand All @@ -99,12 +102,14 @@ class VideoStream : public Resource {

String file;
int audio_track = 0;
int pp_level = 0;

public:
void set_file(const String &p_file);
String get_file();

virtual void set_audio_track(int p_track);
virtual void set_pp_level(int p_pp_level);
virtual Ref<VideoStreamPlayback> instantiate_playback();

VideoStream();
Expand Down

0 comments on commit 40c80dd

Please sign in to comment.