Re: [FFmpeg-devel] [PATCH v2 1/1] libavdevice/decklink: extend available actions on signal loss

2024-05-14 Thread Marton Balint




On Tue, 14 May 2024, Michael Riedl wrote:




Deprecate the option 'draw_bars' in favor of the new option 
'signal_loss_action',
which controls the behavior when the input signal is not available
(including the behavior previously available through draw_bars).
The default behavior remains unchanged to be backwards compatible.
The new option is more flexible for extending now and in the future.

The new value 'repeat' repeats the last video frame.
This is useful for very short dropouts and was not available before.


As far as I see, you are overriding frameBytes for a repeated frame, that seems 
wrong. pkt.data (frameBytes) must be associated with the videoFrame which is 
passed to av_buffer_create() later on.

Every AVFrame returned by the decklink device has an AVBuffer set up which
keeps a reference to the original DeckLink frame. This allows the use of the 
DeckLink frame's raw buffer directly. But you cannot use the raw buffer of 
another DeckLink frame for which the AVBuffer of the AVFrame does not keep a 
reference.


Thank you for your feedback!

I took another look at the code and revisited the DeckLink documentation 
to ensure my understanding was correct. It seems that frameBytes is a 
pointer to the buffer of an IDeckLinkVideoFrame, and it remains valid as 
long as the videoFrame is not released.


That is just it. You are releasing the repeated frame as soon as a valid 
frame comes in. The AVPacket data you previously returned will still point 
to the now released frameBytes. As I wrote above, the decklink frame 
corresponding to the returned frameBytes must be released in the 
destructor of the AVPacket buffer.


Regards,
Marton
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


Re: [FFmpeg-devel] [PATCH v2 1/1] libavdevice/decklink: extend available actions on signal loss

2024-05-14 Thread Michael Riedl
>
>> Deprecate the option 'draw_bars' in favor of the new option 
>> 'signal_loss_action',
>> which controls the behavior when the input signal is not available
>> (including the behavior previously available through draw_bars).
>> The default behavior remains unchanged to be backwards compatible.
>> The new option is more flexible for extending now and in the future.
>>
>> The new value 'repeat' repeats the last video frame.
>> This is useful for very short dropouts and was not available before.
>
> As far as I see, you are overriding frameBytes for a repeated frame, that 
> seems wrong. pkt.data (frameBytes) must be associated with the videoFrame 
> which is passed to av_buffer_create() later on.
>
> Every AVFrame returned by the decklink device has an AVBuffer set up which
> keeps a reference to the original DeckLink frame. This allows the use of the 
> DeckLink frame's raw buffer directly. But you cannot use the raw buffer of 
> another DeckLink frame for which the AVBuffer of the AVFrame does not keep a 
> reference.

Thank you for your feedback!

I took another look at the code and revisited the DeckLink documentation to 
ensure my understanding was correct. It seems that frameBytes is a pointer to 
the buffer of an IDeckLinkVideoFrame, and it remains valid as long as the 
videoFrame is not released. To handle this, I add a reference to the DeckLink 
videoFrame to keep it valid and then release it (decreasing the reference 
counter) when it's no longer needed. Updating frameBytes multiple times should 
be okay since it just points to the raw frame buffer.

I ran some tests using Valgrind with and without the repeat option. I found a 
memory leak introduced by the patch because the destructor of 
decklink_input_callback is never called, which leaves one last video frame 
unreleased.

I'll work on fixing this memory leak and send an update soon. If you have any 
more comments or concerns, please let me know.

Best regards,
Michael Riedl

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


Re: [FFmpeg-devel] [PATCH v2 1/1] libavdevice/decklink: extend available actions on signal loss

2024-05-07 Thread Marton Balint




On Tue, 23 Apr 2024, Michael Riedl wrote:


Deprecate the option 'draw_bars' in favor of the new option 
'signal_loss_action',
which controls the behavior when the input signal is not available
(including the behavior previously available through draw_bars).
The default behavior remains unchanged to be backwards compatible.
The new option is more flexible for extending now and in the future.

The new value 'repeat' repeats the last video frame.
This is useful for very short dropouts and was not available before.


As far as I see, you are overriding frameBytes for a repeated frame, 
that seems wrong. pkt.data (frameBytes) must be associated with the 
videoFrame which is passed to av_buffer_create() later on.


Every AVFrame returned by the decklink device has an AVBuffer set up which
keeps a reference to the original DeckLink frame. This allows the use 
of the DeckLink frame's raw buffer directly. But you cannot use the raw 
buffer of another DeckLink frame for which the AVBuffer of the AVFrame 
does not keep a reference.


Regards,
Marton




Signed-off-by: Michael Riedl 
---
doc/indevs.texi | 16 
libavdevice/decklink_common.h   |  1 +
libavdevice/decklink_common_c.h |  7 +++
libavdevice/decklink_dec.cpp| 23 ++-
libavdevice/decklink_dec_c.c|  6 +-
5 files changed, 51 insertions(+), 2 deletions(-)

diff --git a/doc/indevs.texi b/doc/indevs.texi
index 734fc657523..cdf44a66382 100644
--- a/doc/indevs.texi
+++ b/doc/indevs.texi
@@ -396,6 +396,22 @@ Defaults to @samp{audio}.
@item draw_bars
If set to @samp{true}, color bars are drawn in the event of a signal loss.
Defaults to @samp{true}.
+This option is deprecated, please use the @code{signal_loss_action} option.
+
+@item signal_loss_action
+Sets the action to take in the event of a signal loss. Accepts one of the
+following values:
+
+@table @option
+@item 1, none
+Do nothing on signal loss. This usually results in black frames.
+@item 2, bars
+Draw color bars on signal loss. Only supported for 8-bit input signals.
+@item 3, repeat
+Repeat the last video frame on signal loss.
+@end table
+
+Defaults to @samp{bars}.

@item queue_size
Sets maximum input buffer size in bytes. If the buffering reaches this value,
diff --git a/libavdevice/decklink_common.h b/libavdevice/decklink_common.h
index c54a635876c..6b32dc2d09c 100644
--- a/libavdevice/decklink_common.h
+++ b/libavdevice/decklink_common.h
@@ -147,6 +147,7 @@ struct decklink_ctx {
DecklinkPtsSource video_pts_source;
int draw_bars;
BMDPixelFormat raw_format;
+DecklinkSignalLossAction signal_loss_action;

int frames_preroll;
int frames_buffer;
diff --git a/libavdevice/decklink_common_c.h b/libavdevice/decklink_common_c.h
index 9c55d891494..53d2c583e7e 100644
--- a/libavdevice/decklink_common_c.h
+++ b/libavdevice/decklink_common_c.h
@@ -37,6 +37,12 @@ typedef enum DecklinkPtsSource {
PTS_SRC_NB
} DecklinkPtsSource;

+typedef enum DecklinkSignalLossAction {
+SIGNAL_LOSS_NONE= 1,
+SIGNAL_LOSS_REPEAT  = 2,
+SIGNAL_LOSS_BARS= 3
+} DecklinkSignalLossAction;
+
struct decklink_cctx {
const AVClass *cclass;

@@ -68,6 +74,7 @@ struct decklink_cctx {
int64_t timestamp_align;
int timing_offset;
int wait_for_tc;
+DecklinkSignalLossAction signal_loss_action;
};

#endif /* AVDEVICE_DECKLINK_COMMON_C_H */
diff --git a/libavdevice/decklink_dec.cpp b/libavdevice/decklink_dec.cpp
index 671573853ba..1c2d08ea9fc 100644
--- a/libavdevice/decklink_dec.cpp
+++ b/libavdevice/decklink_dec.cpp
@@ -593,6 +593,7 @@ private:
int no_video;
int64_t initial_video_pts;
int64_t initial_audio_pts;
+IDeckLinkVideoInputFrame* last_video_frame;
};

decklink_input_callback::decklink_input_callback(AVFormatContext *_avctx) : 
_refs(1)
@@ -602,10 +603,13 @@ 
decklink_input_callback::decklink_input_callback(AVFormatContext *_avctx) : _ref
ctx = (struct decklink_ctx *)cctx->ctx;
no_video = 0;
initial_audio_pts = initial_video_pts = AV_NOPTS_VALUE;
+last_video_frame = nullptr;
}

decklink_input_callback::~decklink_input_callback()
{
+if (last_video_frame)
+last_video_frame->Release();
}

ULONG decklink_input_callback::AddRef(void)
@@ -773,7 +777,7 @@ HRESULT decklink_input_callback::VideoInputFrameArrived(
  ctx->video_st->time_base.den);

if (videoFrame->GetFlags() & bmdFrameHasNoInputSource) {
-if (ctx->draw_bars && videoFrame->GetPixelFormat() == 
bmdFormat8BitYUV) {
+if (ctx->signal_loss_action == SIGNAL_LOSS_BARS && 
videoFrame->GetPixelFormat() == bmdFormat8BitYUV) {
unsigned bars[8] = {
0xEA80EA80, 0xD292D210, 0xA910A9A5, 0x90229035,
0x6ADD6ACA, 0x51EF515A, 0x286D28EF, 0x10801080 };
@@ -785,6 +789,8 @@ HRESULT decklink_input_callback::VideoInputFrameArrived(
for (int x = 0; x < width; x += 2)
   

[FFmpeg-devel] [PATCH v2 1/1] libavdevice/decklink: extend available actions on signal loss

2024-04-23 Thread Michael Riedl
Deprecate the option 'draw_bars' in favor of the new option 
'signal_loss_action',
which controls the behavior when the input signal is not available
(including the behavior previously available through draw_bars).
The default behavior remains unchanged to be backwards compatible.
The new option is more flexible for extending now and in the future.

The new value 'repeat' repeats the last video frame.
This is useful for very short dropouts and was not available before.

Signed-off-by: Michael Riedl 
---
 doc/indevs.texi | 16 
 libavdevice/decklink_common.h   |  1 +
 libavdevice/decklink_common_c.h |  7 +++
 libavdevice/decklink_dec.cpp| 23 ++-
 libavdevice/decklink_dec_c.c|  6 +-
 5 files changed, 51 insertions(+), 2 deletions(-)

diff --git a/doc/indevs.texi b/doc/indevs.texi
index 734fc657523..cdf44a66382 100644
--- a/doc/indevs.texi
+++ b/doc/indevs.texi
@@ -396,6 +396,22 @@ Defaults to @samp{audio}.
 @item draw_bars
 If set to @samp{true}, color bars are drawn in the event of a signal loss.
 Defaults to @samp{true}.
+This option is deprecated, please use the @code{signal_loss_action} option.
+
+@item signal_loss_action
+Sets the action to take in the event of a signal loss. Accepts one of the
+following values:
+
+@table @option
+@item 1, none
+Do nothing on signal loss. This usually results in black frames.
+@item 2, bars
+Draw color bars on signal loss. Only supported for 8-bit input signals.
+@item 3, repeat
+Repeat the last video frame on signal loss.
+@end table
+
+Defaults to @samp{bars}.
 
 @item queue_size
 Sets maximum input buffer size in bytes. If the buffering reaches this value,
diff --git a/libavdevice/decklink_common.h b/libavdevice/decklink_common.h
index c54a635876c..6b32dc2d09c 100644
--- a/libavdevice/decklink_common.h
+++ b/libavdevice/decklink_common.h
@@ -147,6 +147,7 @@ struct decklink_ctx {
 DecklinkPtsSource video_pts_source;
 int draw_bars;
 BMDPixelFormat raw_format;
+DecklinkSignalLossAction signal_loss_action;
 
 int frames_preroll;
 int frames_buffer;
diff --git a/libavdevice/decklink_common_c.h b/libavdevice/decklink_common_c.h
index 9c55d891494..53d2c583e7e 100644
--- a/libavdevice/decklink_common_c.h
+++ b/libavdevice/decklink_common_c.h
@@ -37,6 +37,12 @@ typedef enum DecklinkPtsSource {
 PTS_SRC_NB
 } DecklinkPtsSource;
 
+typedef enum DecklinkSignalLossAction {
+SIGNAL_LOSS_NONE= 1,
+SIGNAL_LOSS_REPEAT  = 2,
+SIGNAL_LOSS_BARS= 3
+} DecklinkSignalLossAction;
+
 struct decklink_cctx {
 const AVClass *cclass;
 
@@ -68,6 +74,7 @@ struct decklink_cctx {
 int64_t timestamp_align;
 int timing_offset;
 int wait_for_tc;
+DecklinkSignalLossAction signal_loss_action;
 };
 
 #endif /* AVDEVICE_DECKLINK_COMMON_C_H */
diff --git a/libavdevice/decklink_dec.cpp b/libavdevice/decklink_dec.cpp
index 671573853ba..1c2d08ea9fc 100644
--- a/libavdevice/decklink_dec.cpp
+++ b/libavdevice/decklink_dec.cpp
@@ -593,6 +593,7 @@ private:
 int no_video;
 int64_t initial_video_pts;
 int64_t initial_audio_pts;
+IDeckLinkVideoInputFrame* last_video_frame;
 };
 
 decklink_input_callback::decklink_input_callback(AVFormatContext *_avctx) : 
_refs(1)
@@ -602,10 +603,13 @@ 
decklink_input_callback::decklink_input_callback(AVFormatContext *_avctx) : _ref
 ctx = (struct decklink_ctx *)cctx->ctx;
 no_video = 0;
 initial_audio_pts = initial_video_pts = AV_NOPTS_VALUE;
+last_video_frame = nullptr;
 }
 
 decklink_input_callback::~decklink_input_callback()
 {
+if (last_video_frame)
+last_video_frame->Release();
 }
 
 ULONG decklink_input_callback::AddRef(void)
@@ -773,7 +777,7 @@ HRESULT decklink_input_callback::VideoInputFrameArrived(
   ctx->video_st->time_base.den);
 
 if (videoFrame->GetFlags() & bmdFrameHasNoInputSource) {
-if (ctx->draw_bars && videoFrame->GetPixelFormat() == 
bmdFormat8BitYUV) {
+if (ctx->signal_loss_action == SIGNAL_LOSS_BARS && 
videoFrame->GetPixelFormat() == bmdFormat8BitYUV) {
 unsigned bars[8] = {
 0xEA80EA80, 0xD292D210, 0xA910A9A5, 0x90229035,
 0x6ADD6ACA, 0x51EF515A, 0x286D28EF, 0x10801080 };
@@ -785,6 +789,8 @@ HRESULT decklink_input_callback::VideoInputFrameArrived(
 for (int x = 0; x < width; x += 2)
 *p++ = bars[(x * 8) / width];
 }
+} else if (ctx->signal_loss_action == SIGNAL_LOSS_REPEAT) {
+last_video_frame->GetBytes();
 }
 
 if (!no_video) {
@@ -793,6 +799,12 @@ HRESULT decklink_input_callback::VideoInputFrameArrived(
 }
 no_video = 1;
 } else {
+if (ctx->signal_loss_action == SIGNAL_LOSS_REPEAT) {
+if (last_video_frame)
+last_video_frame->Release();
+