Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
* [FFmpeg-devel] [PATCH 1/2] avcodec/libx264: add a flush callback
@ 2023-03-27 20:09 James Almer
  2023-03-27 20:09 ` [FFmpeg-devel] [PATCH 2/2] avcodec/libfdk-aaceenc: " James Almer
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: James Almer @ 2023-03-27 20:09 UTC (permalink / raw)
  To: ffmpeg-devel

Signed-off-by: James Almer <jamrial@gmail.com>
---
 libavcodec/libx264.c | 24 ++++++++++++++++++++++--
 1 file changed, 22 insertions(+), 2 deletions(-)

diff --git a/libavcodec/libx264.c b/libavcodec/libx264.c
index 92828fabc3..72c3b6cedc 100644
--- a/libavcodec/libx264.c
+++ b/libavcodec/libx264.c
@@ -178,8 +178,8 @@ static int encode_nals(AVCodecContext *ctx, AVPacket *pkt,
         memcpy(p, x4->sei, x4->sei_size);
         p += x4->sei_size;
         size -= x4->sei_size;
-        x4->sei_size = 0;
-        av_freep(&x4->sei);
+        /* Keep the value around in case of flush */
+        x4->sei_size = -x4->sei_size;
     }
 
     /* x264 guarantees the payloads of the NALs
@@ -648,6 +648,24 @@ FF_ENABLE_DEPRECATION_WARNINGS
     return 0;
 }
 
+static void X264_flush(AVCodecContext *avctx)
+{
+    X264Context *x4 = avctx->priv_data;
+    x264_nal_t *nal;
+    int nnal, ret;
+    x264_picture_t pic_out = {0};
+
+    do {
+        ret = x264_encoder_encode(x4->enc, &nal, &nnal, NULL, &pic_out);
+    } while (ret > 0 && x264_encoder_delayed_frames(x4->enc));
+
+    for (int i = 0; i < x4->nb_reordered_opaque; i++)
+        opaque_uninit(&x4->reordered_opaque[i]);
+
+    if (x4->sei_size < 0)
+        x4->sei_size = -x4->sei_size;
+}
+
 static av_cold int X264_close(AVCodecContext *avctx)
 {
     X264Context *x4 = avctx->priv_data;
@@ -1335,12 +1353,14 @@ FFCodec ff_libx264_encoder = {
     .p.capabilities   = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
                         AV_CODEC_CAP_OTHER_THREADS |
                         AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE |
+                        AV_CODEC_CAP_ENCODER_FLUSH |
                         AV_CODEC_CAP_ENCODER_RECON_FRAME,
     .p.priv_class     = &x264_class,
     .p.wrapper_name   = "libx264",
     .priv_data_size   = sizeof(X264Context),
     .init             = X264_init,
     FF_CODEC_ENCODE_CB(X264_frame),
+    .flush            = X264_flush,
     .close            = X264_close,
     .defaults         = x264_defaults,
 #if X264_BUILD < 153
-- 
2.40.0

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

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [FFmpeg-devel] [PATCH 2/2] avcodec/libfdk-aaceenc: add a flush callback
  2023-03-27 20:09 [FFmpeg-devel] [PATCH 1/2] avcodec/libx264: add a flush callback James Almer
@ 2023-03-27 20:09 ` James Almer
  2023-03-27 20:33 ` [FFmpeg-devel] [PATCH 1/2] avcodec/libx264: " Andreas Rheinhardt
  2023-03-28 21:12 ` Michael Niedermayer
  2 siblings, 0 replies; 6+ messages in thread
From: James Almer @ 2023-03-27 20:09 UTC (permalink / raw)
  To: ffmpeg-devel

Signed-off-by: James Almer <jamrial@gmail.com>
---
 libavcodec/libfdk-aacenc.c | 40 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)

diff --git a/libavcodec/libfdk-aacenc.c b/libavcodec/libfdk-aacenc.c
index eb97e0fb41..6d6462d951 100644
--- a/libavcodec/libfdk-aacenc.c
+++ b/libavcodec/libfdk-aacenc.c
@@ -132,6 +132,44 @@ static int aac_encode_close(AVCodecContext *avctx)
     return 0;
 }
 
+static void aac_encode_flush(AVCodecContext *avctx)
+{
+    AACContext *s = avctx->priv_data;
+    AACENC_BufDesc in_buf   = { 0 }, out_buf = { 0 };
+    AACENC_InArgs  in_args  = { 0 };
+    AACENC_OutArgs out_args;
+    int64_t pts, duration;
+    uint8_t dummy_in[1], dummy_out[1];
+    int in_buffer_identifiers[] = { IN_AUDIO_DATA, IN_METADATA_SETUP };
+    int in_buffer_element_sizes[] = { 2, sizeof(AACENC_MetaData) };
+    int in_buffer_sizes[] = { 0, sizeof(s->metaDataSetup) };
+    int out_buffer_identifier = OUT_BITSTREAM_DATA;
+    int out_buffer_size = sizeof(dummy_out), out_buffer_element_size = 1;
+    void* inBuffer[] = { dummy_in, &s->metaDataSetup };
+    void *out_ptr = dummy_out;
+    AACENC_ERROR err;
+
+    ff_af_queue_remove(&s->afq, s->afq.frame_count, &pts, &duration);
+
+    in_buf.bufs              = (void **)inBuffer;
+    in_buf.numBufs           = s->metadata_mode == 0 ? 1 : 2;
+    in_buf.bufferIdentifiers = in_buffer_identifiers;
+    in_buf.bufSizes          = in_buffer_sizes;
+    in_buf.bufElSizes        = in_buffer_element_sizes;
+
+    out_buf.numBufs           = 1;
+    out_buf.bufs              = &out_ptr;
+    out_buf.bufferIdentifiers = &out_buffer_identifier;
+    out_buf.bufSizes          = &out_buffer_size;
+    out_buf.bufElSizes        = &out_buffer_element_size;
+
+    err = aacEncEncode(s->handle, &in_buf, &out_buf, &in_args, &out_args);
+    if (err != AACENC_OK) {
+        av_log(avctx, AV_LOG_ERROR, "Unexpected error while flushing: %s\n",
+               aac_get_error(err));
+    }
+}
+
 static av_cold int aac_encode_init(AVCodecContext *avctx)
 {
     AACContext *s = avctx->priv_data;
@@ -561,11 +599,13 @@ const FFCodec ff_libfdk_aac_encoder = {
     .p.type                = AVMEDIA_TYPE_AUDIO,
     .p.id                  = AV_CODEC_ID_AAC,
     .p.capabilities        = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
+                             AV_CODEC_CAP_ENCODER_FLUSH |
                              AV_CODEC_CAP_SMALL_LAST_FRAME,
     .caps_internal         = FF_CODEC_CAP_NOT_INIT_THREADSAFE,
     .priv_data_size        = sizeof(AACContext),
     .init                  = aac_encode_init,
     FF_CODEC_ENCODE_CB(aac_encode_frame),
+    .flush                 = aac_encode_flush,
     .close                 = aac_encode_close,
     .p.sample_fmts         = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
                                                             AV_SAMPLE_FMT_NONE },
-- 
2.40.0

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

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [FFmpeg-devel] [PATCH 1/2] avcodec/libx264: add a flush callback
  2023-03-27 20:09 [FFmpeg-devel] [PATCH 1/2] avcodec/libx264: add a flush callback James Almer
  2023-03-27 20:09 ` [FFmpeg-devel] [PATCH 2/2] avcodec/libfdk-aaceenc: " James Almer
@ 2023-03-27 20:33 ` Andreas Rheinhardt
  2023-03-27 20:40   ` James Almer
  2023-03-28 21:12 ` Michael Niedermayer
  2 siblings, 1 reply; 6+ messages in thread
From: Andreas Rheinhardt @ 2023-03-27 20:33 UTC (permalink / raw)
  To: ffmpeg-devel

James Almer:
> Signed-off-by: James Almer <jamrial@gmail.com>
> ---
>  libavcodec/libx264.c | 24 ++++++++++++++++++++++--
>  1 file changed, 22 insertions(+), 2 deletions(-)
> 
> diff --git a/libavcodec/libx264.c b/libavcodec/libx264.c
> index 92828fabc3..72c3b6cedc 100644
> --- a/libavcodec/libx264.c
> +++ b/libavcodec/libx264.c
> @@ -178,8 +178,8 @@ static int encode_nals(AVCodecContext *ctx, AVPacket *pkt,
>          memcpy(p, x4->sei, x4->sei_size);
>          p += x4->sei_size;
>          size -= x4->sei_size;
> -        x4->sei_size = 0;
> -        av_freep(&x4->sei);
> +        /* Keep the value around in case of flush */
> +        x4->sei_size = -x4->sei_size;
>      }
>  
>      /* x264 guarantees the payloads of the NALs
> @@ -648,6 +648,24 @@ FF_ENABLE_DEPRECATION_WARNINGS
>      return 0;
>  }
>  
> +static void X264_flush(AVCodecContext *avctx)
> +{
> +    X264Context *x4 = avctx->priv_data;
> +    x264_nal_t *nal;
> +    int nnal, ret;
> +    x264_picture_t pic_out = {0};
> +
> +    do {
> +        ret = x264_encoder_encode(x4->enc, &nal, &nnal, NULL, &pic_out);
> +    } while (ret > 0 && x264_encoder_delayed_frames(x4->enc));
> +
> +    for (int i = 0; i < x4->nb_reordered_opaque; i++)
> +        opaque_uninit(&x4->reordered_opaque[i]);
> +
> +    if (x4->sei_size < 0)
> +        x4->sei_size = -x4->sei_size;
> +}
> +
>  static av_cold int X264_close(AVCodecContext *avctx)
>  {
>      X264Context *x4 = avctx->priv_data;
> @@ -1335,12 +1353,14 @@ FFCodec ff_libx264_encoder = {
>      .p.capabilities   = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
>                          AV_CODEC_CAP_OTHER_THREADS |
>                          AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE |
> +                        AV_CODEC_CAP_ENCODER_FLUSH |
>                          AV_CODEC_CAP_ENCODER_RECON_FRAME,
>      .p.priv_class     = &x264_class,
>      .p.wrapper_name   = "libx264",
>      .priv_data_size   = sizeof(X264Context),
>      .init             = X264_init,
>      FF_CODEC_ENCODE_CB(X264_frame),
> +    .flush            = X264_flush,
>      .close            = X264_close,
>      .defaults         = x264_defaults,
>  #if X264_BUILD < 153

Wasn't AV_CODEC_CAP_ENCODER_FLUSH supposed to be only implemented for
hardware encoders where closing and reopening an encoder is costly?

- Andreas

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

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [FFmpeg-devel] [PATCH 1/2] avcodec/libx264: add a flush callback
  2023-03-27 20:33 ` [FFmpeg-devel] [PATCH 1/2] avcodec/libx264: " Andreas Rheinhardt
@ 2023-03-27 20:40   ` James Almer
  0 siblings, 0 replies; 6+ messages in thread
From: James Almer @ 2023-03-27 20:40 UTC (permalink / raw)
  To: ffmpeg-devel

On 3/27/2023 5:33 PM, Andreas Rheinhardt wrote:
> James Almer:
>> Signed-off-by: James Almer <jamrial@gmail.com>
>> ---
>>   libavcodec/libx264.c | 24 ++++++++++++++++++++++--
>>   1 file changed, 22 insertions(+), 2 deletions(-)
>>
>> diff --git a/libavcodec/libx264.c b/libavcodec/libx264.c
>> index 92828fabc3..72c3b6cedc 100644
>> --- a/libavcodec/libx264.c
>> +++ b/libavcodec/libx264.c
>> @@ -178,8 +178,8 @@ static int encode_nals(AVCodecContext *ctx, AVPacket *pkt,
>>           memcpy(p, x4->sei, x4->sei_size);
>>           p += x4->sei_size;
>>           size -= x4->sei_size;
>> -        x4->sei_size = 0;
>> -        av_freep(&x4->sei);
>> +        /* Keep the value around in case of flush */
>> +        x4->sei_size = -x4->sei_size;
>>       }
>>   
>>       /* x264 guarantees the payloads of the NALs
>> @@ -648,6 +648,24 @@ FF_ENABLE_DEPRECATION_WARNINGS
>>       return 0;
>>   }
>>   
>> +static void X264_flush(AVCodecContext *avctx)
>> +{
>> +    X264Context *x4 = avctx->priv_data;
>> +    x264_nal_t *nal;
>> +    int nnal, ret;
>> +    x264_picture_t pic_out = {0};
>> +
>> +    do {
>> +        ret = x264_encoder_encode(x4->enc, &nal, &nnal, NULL, &pic_out);
>> +    } while (ret > 0 && x264_encoder_delayed_frames(x4->enc));
>> +
>> +    for (int i = 0; i < x4->nb_reordered_opaque; i++)
>> +        opaque_uninit(&x4->reordered_opaque[i]);
>> +
>> +    if (x4->sei_size < 0)
>> +        x4->sei_size = -x4->sei_size;
>> +}
>> +
>>   static av_cold int X264_close(AVCodecContext *avctx)
>>   {
>>       X264Context *x4 = avctx->priv_data;
>> @@ -1335,12 +1353,14 @@ FFCodec ff_libx264_encoder = {
>>       .p.capabilities   = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
>>                           AV_CODEC_CAP_OTHER_THREADS |
>>                           AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE |
>> +                        AV_CODEC_CAP_ENCODER_FLUSH |
>>                           AV_CODEC_CAP_ENCODER_RECON_FRAME,
>>       .p.priv_class     = &x264_class,
>>       .p.wrapper_name   = "libx264",
>>       .priv_data_size   = sizeof(X264Context),
>>       .init             = X264_init,
>>       FF_CODEC_ENCODE_CB(X264_frame),
>> +    .flush            = X264_flush,
>>       .close            = X264_close,
>>       .defaults         = x264_defaults,
>>   #if X264_BUILD < 153
> 
> Wasn't AV_CODEC_CAP_ENCODER_FLUSH supposed to be only implemented for
> hardware encoders where closing and reopening an encoder is costly?
> 
> - Andreas

It was introduced thinking on those for the reason you mentioned, but 
there's nothing limiting its usage for other codecs.
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [FFmpeg-devel] [PATCH 1/2] avcodec/libx264: add a flush callback
  2023-03-27 20:09 [FFmpeg-devel] [PATCH 1/2] avcodec/libx264: add a flush callback James Almer
  2023-03-27 20:09 ` [FFmpeg-devel] [PATCH 2/2] avcodec/libfdk-aaceenc: " James Almer
  2023-03-27 20:33 ` [FFmpeg-devel] [PATCH 1/2] avcodec/libx264: " Andreas Rheinhardt
@ 2023-03-28 21:12 ` Michael Niedermayer
  2023-03-28 21:25   ` [FFmpeg-devel] [PATCH v2 " James Almer
  2 siblings, 1 reply; 6+ messages in thread
From: Michael Niedermayer @ 2023-03-28 21:12 UTC (permalink / raw)
  To: FFmpeg development discussions and patches


[-- Attachment #1.1: Type: text/plain, Size: 713 bytes --]

On Mon, Mar 27, 2023 at 05:09:55PM -0300, James Almer wrote:
> Signed-off-by: James Almer <jamrial@gmail.com>
> ---
>  libavcodec/libx264.c | 24 ++++++++++++++++++++++--
>  1 file changed, 22 insertions(+), 2 deletions(-)

breaks:

./ffmpeg -i mm-short.mpg -vcodec libx264 -x264-params     level=30:bframes=0:weightp=0:cabac=0:ref=1:vbv-maxrate=768:vbv-bufsize=2000:analyse=all:me=umh:no-fast-pskip=1:subq=6:8x8dct=0:trellis=0    -vframes 15 -an -bitexact -y x264p1.nut

[vost#0:0/libx264 @ 0x55c290b11680] video encoding failed


thx

[...]
-- 
Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

Those who are best at talking, realize last or never when they are wrong.

[-- Attachment #1.2: signature.asc --]
[-- Type: application/pgp-signature, Size: 195 bytes --]

[-- Attachment #2: Type: text/plain, Size: 251 bytes --]

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

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [FFmpeg-devel] [PATCH v2 1/2] avcodec/libx264: add a flush callback
  2023-03-28 21:12 ` Michael Niedermayer
@ 2023-03-28 21:25   ` James Almer
  0 siblings, 0 replies; 6+ messages in thread
From: James Almer @ 2023-03-28 21:25 UTC (permalink / raw)
  To: ffmpeg-devel

Signed-off-by: James Almer <jamrial@gmail.com>
---
 libavcodec/libx264.c | 26 +++++++++++++++++++++++---
 1 file changed, 23 insertions(+), 3 deletions(-)

diff --git a/libavcodec/libx264.c b/libavcodec/libx264.c
index 92828fabc3..cfdd422236 100644
--- a/libavcodec/libx264.c
+++ b/libavcodec/libx264.c
@@ -151,7 +151,7 @@ static int encode_nals(AVCodecContext *ctx, AVPacket *pkt,
 {
     X264Context *x4 = ctx->priv_data;
     uint8_t *p;
-    uint64_t size = x4->sei_size;
+    uint64_t size = FFMAX(x4->sei_size, 0);
     int ret;
 
     if (!nnal)
@@ -178,8 +178,8 @@ static int encode_nals(AVCodecContext *ctx, AVPacket *pkt,
         memcpy(p, x4->sei, x4->sei_size);
         p += x4->sei_size;
         size -= x4->sei_size;
-        x4->sei_size = 0;
-        av_freep(&x4->sei);
+        /* Keep the value around in case of flush */
+        x4->sei_size = -x4->sei_size;
     }
 
     /* x264 guarantees the payloads of the NALs
@@ -648,6 +648,24 @@ FF_ENABLE_DEPRECATION_WARNINGS
     return 0;
 }
 
+static void X264_flush(AVCodecContext *avctx)
+{
+    X264Context *x4 = avctx->priv_data;
+    x264_nal_t *nal;
+    int nnal, ret;
+    x264_picture_t pic_out = {0};
+
+    do {
+        ret = x264_encoder_encode(x4->enc, &nal, &nnal, NULL, &pic_out);
+    } while (ret > 0 && x264_encoder_delayed_frames(x4->enc));
+
+    for (int i = 0; i < x4->nb_reordered_opaque; i++)
+        opaque_uninit(&x4->reordered_opaque[i]);
+
+    if (x4->sei_size < 0)
+        x4->sei_size = -x4->sei_size;
+}
+
 static av_cold int X264_close(AVCodecContext *avctx)
 {
     X264Context *x4 = avctx->priv_data;
@@ -1335,12 +1353,14 @@ FFCodec ff_libx264_encoder = {
     .p.capabilities   = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
                         AV_CODEC_CAP_OTHER_THREADS |
                         AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE |
+                        AV_CODEC_CAP_ENCODER_FLUSH |
                         AV_CODEC_CAP_ENCODER_RECON_FRAME,
     .p.priv_class     = &x264_class,
     .p.wrapper_name   = "libx264",
     .priv_data_size   = sizeof(X264Context),
     .init             = X264_init,
     FF_CODEC_ENCODE_CB(X264_frame),
+    .flush            = X264_flush,
     .close            = X264_close,
     .defaults         = x264_defaults,
 #if X264_BUILD < 153
-- 
2.40.0

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

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2023-03-28 21:26 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-27 20:09 [FFmpeg-devel] [PATCH 1/2] avcodec/libx264: add a flush callback James Almer
2023-03-27 20:09 ` [FFmpeg-devel] [PATCH 2/2] avcodec/libfdk-aaceenc: " James Almer
2023-03-27 20:33 ` [FFmpeg-devel] [PATCH 1/2] avcodec/libx264: " Andreas Rheinhardt
2023-03-27 20:40   ` James Almer
2023-03-28 21:12 ` Michael Niedermayer
2023-03-28 21:25   ` [FFmpeg-devel] [PATCH v2 " James Almer

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git