diff mbox series

[FFmpeg-devel] fftools/ffmpeg: Add new variant source_no_drop to the force_key_frames option

Message ID 39c63a4d-10c2-df1d-c5ce-eb3eb35525b9@mail.de
State Accepted
Commit b7266302a40ba48fea7a5644f08623159b3dcac7
Headers show
Series [FFmpeg-devel] fftools/ffmpeg: Add new variant source_no_drop to the force_key_frames option
Related show

Checks

Context Check Description
andriy/x86_make success Make finished
andriy/x86_make_fate success Make fate finished
andriy/PPC64_make success Make finished
andriy/PPC64_make_fate success Make fate finished

Commit Message

Thilo Borgmann June 20, 2021, 7:48 p.m. UTC
Hi,

adds a new variant to the -force_key_frames option "source_no_drop" to make sure 
whenever a key frame in the source is dropped, we choose the next frame in the 
output stream as a key frame.

-Thilo
From b37c854e7d48b538537b2c9c5e9651ba2ead3e52 Mon Sep 17 00:00:00 2001
From: Keyun Tong <ktong@fb.com>
Date: Sun, 20 Jun 2021 21:42:29 +0200
Subject: [PATCH] fftools/ffmpeg: Add new variant source_no_drop to the
 force_key_frames option

Suggested-By: ffmpeg@fb.com
---
 doc/ffmpeg.texi  | 7 +++++++
 fftools/ffmpeg.c | 6 ++++++
 fftools/ffmpeg.h | 1 +
 3 files changed, 14 insertions(+)

Comments

Thilo Borgmann July 4, 2021, 12:26 p.m. UTC | #1
Hi,
 
> adds a new variant to the -force_key_frames option "source_no_drop" to make sure whenever a key frame in the source is dropped, we choose the next frame in the output stream as a key frame.

ping for review.

-Thilo
Michael Niedermayer July 4, 2021, 7:23 p.m. UTC | #2
On Sun, Jun 20, 2021 at 09:48:29PM +0200, Thilo Borgmann wrote:
> Hi,
> 
> adds a new variant to the -force_key_frames option "source_no_drop" to make
> sure whenever a key frame in the source is dropped, we choose the next frame
> in the output stream as a key frame.
> 
> -Thilo

>  doc/ffmpeg.texi  |    7 +++++++
>  fftools/ffmpeg.c |    6 ++++++
>  fftools/ffmpeg.h |    1 +
>  3 files changed, 14 insertions(+)
> e8ffd04204a6cc243b2f98858ec1f1afe44ff8e8  0001-fftools-ffmpeg-Add-new-variant-source_no_drop-to-the.patch
> From b37c854e7d48b538537b2c9c5e9651ba2ead3e52 Mon Sep 17 00:00:00 2001
> From: Keyun Tong <ktong@fb.com>
> Date: Sun, 20 Jun 2021 21:42:29 +0200
> Subject: [PATCH] fftools/ffmpeg: Add new variant source_no_drop to the
>  force_key_frames option

LGTM if both variants have a use case 

thx

[...]
Thilo Borgmann July 16, 2021, 7:50 a.m. UTC | #3
Am 04.07.21 um 21:23 schrieb Michael Niedermayer:
> On Sun, Jun 20, 2021 at 09:48:29PM +0200, Thilo Borgmann wrote:
>> Hi,
>>
>> adds a new variant to the -force_key_frames option "source_no_drop" to make
>> sure whenever a key frame in the source is dropped, we choose the next frame
>> in the output stream as a key frame.
>>
>> -Thilo
> 
>>  doc/ffmpeg.texi  |    7 +++++++
>>  fftools/ffmpeg.c |    6 ++++++
>>  fftools/ffmpeg.h |    1 +
>>  3 files changed, 14 insertions(+)
>> e8ffd04204a6cc243b2f98858ec1f1afe44ff8e8  0001-fftools-ffmpeg-Add-new-variant-source_no_drop-to-the.patch
>> From b37c854e7d48b538537b2c9c5e9651ba2ead3e52 Mon Sep 17 00:00:00 2001
>> From: Keyun Tong <ktong@fb.com>
>> Date: Sun, 20 Jun 2021 21:42:29 +0200
>> Subject: [PATCH] fftools/ffmpeg: Add new variant source_no_drop to the
>>  force_key_frames option
> 
> LGTM if both variants have a use case 

Pushed.

Thanks,
Thilo
Mattias Wadman July 16, 2021, 8:31 p.m. UTC | #4
On Fri, Jul 16, 2021 at 9:51 AM Thilo Borgmann <thilo.borgmann@mail.de>
wrote:

> Am 04.07.21 um 21:23 schrieb Michael Niedermayer:
> > On Sun, Jun 20, 2021 at 09:48:29PM +0200, Thilo Borgmann wrote:
> >> Hi,
> >>
> >> adds a new variant to the -force_key_frames option "source_no_drop" to
> make
> >> sure whenever a key frame in the source is dropped, we choose the next
> frame
> >> in the output stream as a key frame.
> >>
> >> -Thilo
> >
> >>  doc/ffmpeg.texi  |    7 +++++++
> >>  fftools/ffmpeg.c |    6 ++++++
> >>  fftools/ffmpeg.h |    1 +
> >>  3 files changed, 14 insertions(+)
> >> e8ffd04204a6cc243b2f98858ec1f1afe44ff8e8
> 0001-fftools-ffmpeg-Add-new-variant-source_no_drop-to-the.patch
> >> From b37c854e7d48b538537b2c9c5e9651ba2ead3e52 Mon Sep 17 00:00:00 2001
> >> From: Keyun Tong <ktong@fb.com>
> >> Date: Sun, 20 Jun 2021 21:42:29 +0200
> >> Subject: [PATCH] fftools/ffmpeg: Add new variant source_no_drop to the
> >>  force_key_frames option
> >
> > LGTM if both variants have a use case
>
> Pushed.


Sorry for late review, i just noticed this line:
"!strncmp(ost->forced_keyframes, "source_no_drop", 6)"
shouldn't it be:
!strncmp(ost->forced_keyframes, "source_no_drop", 14)
?
Thilo Borgmann July 19, 2021, 7:23 p.m. UTC | #5
Am 16.07.21 um 22:31 schrieb Mattias Wadman:
> On Fri, Jul 16, 2021 at 9:51 AM Thilo Borgmann <thilo.borgmann@mail.de>
> wrote:
> 
>> Am 04.07.21 um 21:23 schrieb Michael Niedermayer:
>>> On Sun, Jun 20, 2021 at 09:48:29PM +0200, Thilo Borgmann wrote:
>>>> Hi,
>>>>
>>>> adds a new variant to the -force_key_frames option "source_no_drop" to
>> make
>>>> sure whenever a key frame in the source is dropped, we choose the next
>> frame
>>>> in the output stream as a key frame.
>>>>
>>>> -Thilo
>>>
>>>>  doc/ffmpeg.texi  |    7 +++++++
>>>>  fftools/ffmpeg.c |    6 ++++++
>>>>  fftools/ffmpeg.h |    1 +
>>>>  3 files changed, 14 insertions(+)
>>>> e8ffd04204a6cc243b2f98858ec1f1afe44ff8e8
>> 0001-fftools-ffmpeg-Add-new-variant-source_no_drop-to-the.patch
>>>> From b37c854e7d48b538537b2c9c5e9651ba2ead3e52 Mon Sep 17 00:00:00 2001
>>>> From: Keyun Tong <ktong@fb.com>
>>>> Date: Sun, 20 Jun 2021 21:42:29 +0200
>>>> Subject: [PATCH] fftools/ffmpeg: Add new variant source_no_drop to the
>>>>  force_key_frames option
>>>
>>> LGTM if both variants have a use case
>>
>> Pushed.
> 
> 
> Sorry for late review, i just noticed this line:
> "!strncmp(ost->forced_keyframes, "source_no_drop", 6)"
> shouldn't it be:
> !strncmp(ost->forced_keyframes, "source_no_drop", 14)
> ?

Absolutely! Fixed and pushed, thanks!

-Thilo
diff mbox series

Patch

diff --git a/doc/ffmpeg.texi b/doc/ffmpeg.texi
index 9feabe6..ab98a5c 100644
--- a/doc/ffmpeg.texi
+++ b/doc/ffmpeg.texi
@@ -996,6 +996,7 @@  Deprecated see -bsf
 @item -force_key_frames[:@var{stream_specifier}] @var{time}[,@var{time}...] (@emph{output,per-stream})
 @item -force_key_frames[:@var{stream_specifier}] expr:@var{expr} (@emph{output,per-stream})
 @item -force_key_frames[:@var{stream_specifier}] source (@emph{output,per-stream})
+@item -force_key_frames[:@var{stream_specifier}] source_no_drop (@emph{output,per-stream})
 
 @var{force_key_frames} can take arguments of the following form:
 
@@ -1057,6 +1058,12 @@  starting from second 13:
 If the argument is @code{source}, ffmpeg will force a key frame if
 the current frame being encoded is marked as a key frame in its source.
 
+@item source_no_drop
+If the argument is @code{source_no_drop}, ffmpeg will force a key frame if
+the current frame being encoded is marked as a key frame in its source.
+In cases where this particular source frame has to be dropped,
+enforce the next available frame to become a key frame instead.
+
 @end table
 
 Note that forcing too many keyframes is very harmful for the lookahead
diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 04ddc9e..57c46e4 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -1287,6 +1287,7 @@  static void do_video_out(OutputFile *of,
         }
     }
     ost->last_dropped = nb_frames == nb0_frames && next_picture;
+    ost->dropped_keyframe = ost->last_dropped && next_picture && next_picture->key_frame;
 
     /* duplicates frame if needed */
     for (i = 0; i < nb_frames; i++) {
@@ -1347,6 +1348,11 @@  static void do_video_out(OutputFile *of,
                    && in_picture->key_frame==1
                    && !i) {
             forced_keyframe = 1;
+        } else if (   ost->forced_keyframes
+                   && !strncmp(ost->forced_keyframes, "source_no_drop", 6)
+                   && !i) {
+            forced_keyframe = (in_picture->key_frame == 1) || ost->dropped_keyframe;
+            ost->dropped_keyframe = 0;
         }
 
         if (forced_keyframe) {
diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index e9d30fb..3cfb4c4 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -504,6 +504,7 @@  typedef struct OutputStream {
     char *forced_keyframes;
     AVExpr *forced_keyframes_pexpr;
     double forced_keyframes_expr_const_values[FKF_NB];
+    int dropped_keyframe;
 
     /* audio only */
     int *audio_channels_map;             /* list of the channels id to pick from the source stream */