Skip to content

Commit

Permalink
Revert "Switch to callable_mp instead of function pointers"
Browse files Browse the repository at this point in the history
This reverts commit 43dae27.
  • Loading branch information
rsubtil committed Apr 7, 2024
1 parent 21b924d commit 778168a
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 18 deletions.
26 changes: 21 additions & 5 deletions ffmpeg_frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,32 @@
#include <godot_cpp/variant/utility_functions.hpp>
#endif

void FFmpegFrame::_bind_methods() {
ADD_SIGNAL(MethodInfo("return_frame", PropertyInfo(Variant::OBJECT, "frame", PROPERTY_HINT_RESOURCE_TYPE, "FFmpegFrame")));
}

AVFrame *FFmpegFrame::get_frame() const {
return frame;
}

void FFmpegFrame::do_return() {
emit_signal("return_frame", this);
if (return_func && return_instance.is_valid()) {
Ref<RefCounted> instance_ref = return_instance->get_ref();
if (!instance_ref.is_valid()) {
return;
}
return_func(instance_ref, this);
}
}

FFmpegFrame::FFmpegFrame(Ref<RefCounted> p_return_func_instance, return_frame_callback_t p_return_func) {
if (p_return_func && p_return_func_instance.is_valid()) {
return_func = p_return_func;
#ifdef GDEXTENSION
return_instance = UtilityFunctions::weakref(p_return_func_instance);
#else
return_instance.instantiate();
return_instance->set_ref(p_return_func_instance);
return_instance = p_return_func_instance;
#endif
}
frame = av_frame_alloc();
}

FFmpegFrame::FFmpegFrame() {
Expand Down
7 changes: 5 additions & 2 deletions ffmpeg_frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,19 @@ extern "C" {
}

class FFmpegFrame : public RefCounted {
protected:
static void _bind_methods();
public:
typedef void (*return_frame_callback_t)(Ref<RefCounted> p_instance, Ref<FFmpegFrame> p_frame);

private:
AVFrame *frame = nullptr;
return_frame_callback_t return_func = nullptr;
Ref<WeakRef> return_instance;

public:
AVFrame *get_frame() const;
double get_time() const;
void do_return();
FFmpegFrame(Ref<RefCounted> p_return_func_instance, return_frame_callback_t p_return_func);
FFmpegFrame();
~FFmpegFrame();
};
Expand Down
24 changes: 15 additions & 9 deletions video_decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -422,8 +422,11 @@ void VideoDecoder::_read_decoded_frames(AVFrame *p_received_frame) {
}

if (!hw_transfer_frame.is_valid()) {
hw_transfer_frame.instantiate();
hw_transfer_frame->connect("return_frame", callable_mp(this, &VideoDecoder::_hw_transfer_frame_return));
// Leave me alone until i port this to callable_mp
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-function-type"
hw_transfer_frame = Ref<FFmpegFrame>(memnew(FFmpegFrame(Ref<VideoDecoder>(this), (FFmpegFrame::return_frame_callback_t)&VideoDecoder::_hw_transfer_frame_return)));
#pragma GCC diagnostic pop
}

int transfer_result = av_hwframe_transfer_data(hw_transfer_frame->get_frame(), p_received_frame, 0);
Expand All @@ -437,7 +440,7 @@ void VideoDecoder::_read_decoded_frames(AVFrame *p_received_frame) {
frame = hw_transfer_frame;
} else {
// copy data to a new AVFrame so that `receiveFrame` can be reused.
frame.instantiate();
frame = Ref<FFmpegFrame>(memnew(FFmpegFrame()));
av_frame_move_ref(frame->get_frame(), p_received_frame);
}

Expand Down Expand Up @@ -550,12 +553,12 @@ void VideoDecoder::_read_decoded_audio_frames(AVFrame *p_received_frame) {
}
}

void VideoDecoder::_hw_transfer_frame_return(Ref<FFmpegFrame> p_hw_frame) {
hw_transfer_frames.push_back(p_hw_frame);
void VideoDecoder::_hw_transfer_frame_return(Ref<VideoDecoder> p_decoder, Ref<FFmpegFrame> p_hw_frame) {
p_decoder->hw_transfer_frames.push_back(p_hw_frame);
}

void VideoDecoder::_scaler_frame_return(Ref<FFmpegFrame> p_scaler_frame) {
scaler_frames.push_back(p_scaler_frame);
void VideoDecoder::_scaler_frame_return(Ref<VideoDecoder> p_decoder, Ref<FFmpegFrame> p_scaler_frame) {
p_decoder->scaler_frames.push_back(p_scaler_frame);
}

Ref<FFmpegFrame> VideoDecoder::_ensure_frame_pixel_format(Ref<FFmpegFrame> p_frame, AVPixelFormat p_target_pixel_format) {
Expand All @@ -582,8 +585,11 @@ Ref<FFmpegFrame> VideoDecoder::_ensure_frame_pixel_format(Ref<FFmpegFrame> p_fra
}

if (!scaler_frame.is_valid()) {
scaler_frame.instantiate();
scaler_frame->connect("return_frame", callable_mp(this, &VideoDecoder::_scaler_frame_return));
// Leave me alone until i port this to callable_mp
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-function-type"
scaler_frame = Ref<FFmpegFrame>(memnew(FFmpegFrame(Ref<VideoDecoder>(this), (FFmpegFrame::return_frame_callback_t)&VideoDecoder::_scaler_frame_return)));
#pragma GCC diagnostic pop
}

// (re)initialize the scaler frame if needed.
Expand Down
4 changes: 2 additions & 2 deletions video_decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ class VideoDecoder : public RefCounted {
void _read_decoded_frames(AVFrame *p_received_frame);
void _read_decoded_audio_frames(AVFrame *p_received_frame);

void _hw_transfer_frame_return(Ref<FFmpegFrame> p_hw_frame);
void _scaler_frame_return(Ref<FFmpegFrame> p_hw_frame);
static void _hw_transfer_frame_return(Ref<VideoDecoder> p_decoder, Ref<FFmpegFrame> p_hw_frame);
static void _scaler_frame_return(Ref<VideoDecoder> p_decoder, Ref<FFmpegFrame> p_hw_frame);

Ref<FFmpegFrame> _ensure_frame_pixel_format(Ref<FFmpegFrame> p_frame, AVPixelFormat p_target_pixel_format);
AVFrame *_ensure_frame_audio_format(AVFrame *p_frame, AVSampleFormat p_target_audio_format);
Expand Down

0 comments on commit 778168a

Please sign in to comment.