Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
* [FFmpeg-devel] [RFC PATCH 1/7] lavc/mpegvideo_enc: support AV_CODEC_CAP_ENCODER_RECON_FRAME
@ 2023-03-24  9:14 Anton Khirnov
  2023-03-24  9:14 ` [FFmpeg-devel] [PATCH 2/7] tools/decode_simple: always call process_frame(NULL) at the end Anton Khirnov
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: Anton Khirnov @ 2023-03-24  9:14 UTC (permalink / raw)
  To: ffmpeg-devel

---
Last time I tested this, the frames did not actually match when using
B-frames. As I'm not particularly interested in these encoders, I don't
want to spend time debugging the issue, so I don't intend to push this
patch with the rest. Volunteers welcome.
---
 libavcodec/flvenc.c        | 1 +
 libavcodec/h261enc.c       | 1 +
 libavcodec/ituh263enc.c    | 7 +++++--
 libavcodec/mjpegenc.c      | 6 ++++--
 libavcodec/mpeg12enc.c     | 6 ++++--
 libavcodec/mpeg4videoenc.c | 3 ++-
 libavcodec/mpegvideo_enc.c | 8 ++++++++
 libavcodec/msmpeg4enc.c    | 9 ++++++---
 libavcodec/rv10enc.c       | 1 +
 libavcodec/rv20enc.c       | 1 +
 libavcodec/speedhqenc.c    | 1 +
 libavcodec/wmv2enc.c       | 1 +
 12 files changed, 35 insertions(+), 10 deletions(-)

diff --git a/libavcodec/flvenc.c b/libavcodec/flvenc.c
index 6a96cb0f2f..53878722af 100644
--- a/libavcodec/flvenc.c
+++ b/libavcodec/flvenc.c
@@ -103,6 +103,7 @@ const FFCodec ff_flv_encoder = {
     FF_CODEC_ENCODE_CB(ff_mpv_encode_picture),
     .close          = ff_mpv_encode_end,
     .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
+    .p.capabilities = AV_CODEC_CAP_ENCODER_RECON_FRAME,
     .p.pix_fmts     = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,
                                                      AV_PIX_FMT_NONE},
     .p.capabilities = AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
diff --git a/libavcodec/h261enc.c b/libavcodec/h261enc.c
index 438ebb63d9..04dc34d08c 100644
--- a/libavcodec/h261enc.c
+++ b/libavcodec/h261enc.c
@@ -411,6 +411,7 @@ const FFCodec ff_h261_encoder = {
     FF_CODEC_ENCODE_CB(ff_mpv_encode_picture),
     .close          = ff_mpv_encode_end,
     .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
+    .p.capabilities = AV_CODEC_CAP_ENCODER_RECON_FRAME,
     .p.pix_fmts     = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,
                                                      AV_PIX_FMT_NONE },
     .p.capabilities = AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
diff --git a/libavcodec/ituh263enc.c b/libavcodec/ituh263enc.c
index c30ecad438..78c041b2cf 100644
--- a/libavcodec/ituh263enc.c
+++ b/libavcodec/ituh263enc.c
@@ -903,7 +903,8 @@ const FFCodec ff_h263_encoder = {
     .p.id           = AV_CODEC_ID_H263,
     .p.pix_fmts = (const enum AVPixelFormat[]){AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE},
     .p.priv_class   = &h263_class,
-    .p.capabilities = AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
+    .p.capabilities = AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE |
+                      AV_CODEC_CAP_ENCODER_RECON_FRAME,
     .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
     .priv_data_size = sizeof(MpegEncContext),
     .init           = ff_mpv_encode_init,
@@ -934,7 +935,9 @@ const FFCodec ff_h263p_encoder = {
     .p.id           = AV_CODEC_ID_H263P,
     .p.pix_fmts     = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
     .p.priv_class   = &h263p_class,
-    .p.capabilities = AV_CODEC_CAP_SLICE_THREADS | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
+    .p.capabilities = AV_CODEC_CAP_SLICE_THREADS |
+                      AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE |
+                      AV_CODEC_CAP_ENCODER_RECON_FRAME,
     .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
     .priv_data_size = sizeof(MpegEncContext),
     .init           = ff_mpv_encode_init,
diff --git a/libavcodec/mjpegenc.c b/libavcodec/mjpegenc.c
index 508772987f..47b3ca3141 100644
--- a/libavcodec/mjpegenc.c
+++ b/libavcodec/mjpegenc.c
@@ -652,7 +652,8 @@ const FFCodec ff_mjpeg_encoder = {
     FF_CODEC_ENCODE_CB(ff_mpv_encode_picture),
     .close          = mjpeg_encode_close,
     .p.capabilities = AV_CODEC_CAP_SLICE_THREADS | AV_CODEC_CAP_FRAME_THREADS |
-                      AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
+                      AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE |
+                      AV_CODEC_CAP_ENCODER_RECON_FRAME,
     .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP | FF_CODEC_CAP_ICC_PROFILES,
     .p.pix_fmts     = (const enum AVPixelFormat[]) {
         AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P,
@@ -686,6 +687,7 @@ const FFCodec ff_amv_encoder = {
         AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_NONE
     },
     .p.priv_class   = &amv_class,
-    .p.capabilities = AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
+    .p.capabilities = AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE |
+                      AV_CODEC_CAP_ENCODER_RECON_FRAME,
 };
 #endif
diff --git a/libavcodec/mpeg12enc.c b/libavcodec/mpeg12enc.c
index a932b59678..b120174efb 100644
--- a/libavcodec/mpeg12enc.c
+++ b/libavcodec/mpeg12enc.c
@@ -1246,7 +1246,8 @@ const FFCodec ff_mpeg1video_encoder = {
     .p.pix_fmts           = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,
                                                            AV_PIX_FMT_NONE },
     .p.capabilities       = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_SLICE_THREADS |
-                            AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
+                            AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE |
+                            AV_CODEC_CAP_ENCODER_RECON_FRAME,
     .caps_internal        = FF_CODEC_CAP_INIT_CLEANUP,
     .p.priv_class         = &mpeg1_class,
 };
@@ -1265,7 +1266,8 @@ const FFCodec ff_mpeg2video_encoder = {
                                                            AV_PIX_FMT_YUV422P,
                                                            AV_PIX_FMT_NONE },
     .p.capabilities       = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_SLICE_THREADS |
-                            AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
+                            AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE           |
+                            AV_CODEC_CAP_ENCODER_RECON_FRAME,
     .caps_internal        = FF_CODEC_CAP_INIT_CLEANUP,
     .p.priv_class         = &mpeg2_class,
 };
diff --git a/libavcodec/mpeg4videoenc.c b/libavcodec/mpeg4videoenc.c
index c3e9ebea45..93b92867a8 100644
--- a/libavcodec/mpeg4videoenc.c
+++ b/libavcodec/mpeg4videoenc.c
@@ -1404,7 +1404,8 @@ const FFCodec ff_mpeg4_encoder = {
     .close          = ff_mpv_encode_end,
     .p.pix_fmts     = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
     .p.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_SLICE_THREADS |
-                      AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
+                      AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE           |
+                      AV_CODEC_CAP_ENCODER_RECON_FRAME,
     .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
     .p.priv_class   = &mpeg4enc_class,
 };
diff --git a/libavcodec/mpegvideo_enc.c b/libavcodec/mpegvideo_enc.c
index 7d3c8875f2..ca3495fc8f 100644
--- a/libavcodec/mpegvideo_enc.c
+++ b/libavcodec/mpegvideo_enc.c
@@ -1865,6 +1865,14 @@ vbv_retry:
                                        (avctx->flags&AV_CODEC_FLAG_PSNR) ? MPEGVIDEO_MAX_PLANES : 0,
                                        s->pict_type);
 
+        if (avctx->flags & AV_CODEC_FLAG_RECON_FRAME) {
+            AVCodecInternal *avci = avctx->internal;
+            av_frame_unref(avci->recon_frame);
+            ret = av_frame_ref(avci->recon_frame, s->current_picture.f);
+            if (ret < 0)
+                return ret;
+        }
+
         if (avctx->flags & AV_CODEC_FLAG_PASS1)
             assert(put_bits_count(&s->pb) == s->header_bits + s->mv_bits +
                                              s->misc_bits + s->i_tex_bits +
diff --git a/libavcodec/msmpeg4enc.c b/libavcodec/msmpeg4enc.c
index 54121438a0..85b76c889f 100644
--- a/libavcodec/msmpeg4enc.c
+++ b/libavcodec/msmpeg4enc.c
@@ -684,7 +684,8 @@ const FFCodec ff_msmpeg4v2_encoder = {
     .p.id           = AV_CODEC_ID_MSMPEG4V2,
     .p.pix_fmts     = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
     .p.priv_class   = &ff_mpv_enc_class,
-    .p.capabilities = AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
+    .p.capabilities = AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE |
+                      AV_CODEC_CAP_ENCODER_RECON_FRAME,
     .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
     .priv_data_size = sizeof(MSMPEG4EncContext),
     .init           = ff_mpv_encode_init,
@@ -699,7 +700,8 @@ const FFCodec ff_msmpeg4v3_encoder = {
     .p.id           = AV_CODEC_ID_MSMPEG4V3,
     .p.pix_fmts     = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
     .p.priv_class   = &ff_mpv_enc_class,
-    .p.capabilities = AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
+    .p.capabilities = AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE |
+                      AV_CODEC_CAP_ENCODER_RECON_FRAME,
     .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
     .priv_data_size = sizeof(MSMPEG4EncContext),
     .init           = ff_mpv_encode_init,
@@ -714,7 +716,8 @@ const FFCodec ff_wmv1_encoder = {
     .p.id           = AV_CODEC_ID_WMV1,
     .p.pix_fmts     = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
     .p.priv_class   = &ff_mpv_enc_class,
-    .p.capabilities = AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
+    .p.capabilities = AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE |
+                      AV_CODEC_CAP_ENCODER_RECON_FRAME,
     .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
     .priv_data_size = sizeof(MSMPEG4EncContext),
     .init           = ff_mpv_encode_init,
diff --git a/libavcodec/rv10enc.c b/libavcodec/rv10enc.c
index 8a405b8686..90ba4c0a0b 100644
--- a/libavcodec/rv10enc.c
+++ b/libavcodec/rv10enc.c
@@ -77,5 +77,6 @@ const FFCodec ff_rv10_encoder = {
     FF_CODEC_ENCODE_CB(ff_mpv_encode_picture),
     .close          = ff_mpv_encode_end,
     .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
+    .p.capabilities = AV_CODEC_CAP_ENCODER_RECON_FRAME,
     .p.pix_fmts     = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
 };
diff --git a/libavcodec/rv20enc.c b/libavcodec/rv20enc.c
index dc26877d5e..aeea557e89 100644
--- a/libavcodec/rv20enc.c
+++ b/libavcodec/rv20enc.c
@@ -74,5 +74,6 @@ const FFCodec ff_rv20_encoder = {
     FF_CODEC_ENCODE_CB(ff_mpv_encode_picture),
     .close          = ff_mpv_encode_end,
     .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
+    .p.capabilities = AV_CODEC_CAP_ENCODER_RECON_FRAME,
     .p.pix_fmts     = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
 };
diff --git a/libavcodec/speedhqenc.c b/libavcodec/speedhqenc.c
index 5b4ff4c139..162bb77508 100644
--- a/libavcodec/speedhqenc.c
+++ b/libavcodec/speedhqenc.c
@@ -294,6 +294,7 @@ const FFCodec ff_speedhq_encoder = {
     FF_CODEC_ENCODE_CB(ff_mpv_encode_picture),
     .close          = ff_mpv_encode_end,
     .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
+    .p.capabilities = AV_CODEC_CAP_ENCODER_RECON_FRAME,
     .p.pix_fmts     = (const enum AVPixelFormat[]) {
         AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P,
         AV_PIX_FMT_NONE
diff --git a/libavcodec/wmv2enc.c b/libavcodec/wmv2enc.c
index 8eb56444a3..1a1df73ec2 100644
--- a/libavcodec/wmv2enc.c
+++ b/libavcodec/wmv2enc.c
@@ -248,6 +248,7 @@ const FFCodec ff_wmv2_encoder = {
     FF_CODEC_ENCODE_CB(ff_mpv_encode_picture),
     .close          = ff_mpv_encode_end,
     .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
+    .p.capabilities = AV_CODEC_CAP_ENCODER_RECON_FRAME,
     .p.pix_fmts     = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,
                                                      AV_PIX_FMT_NONE },
 };
-- 
2.39.1

_______________________________________________
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] 7+ messages in thread

* [FFmpeg-devel] [PATCH 2/7] tools/decode_simple: always call process_frame(NULL) at the end
  2023-03-24  9:14 [FFmpeg-devel] [RFC PATCH 1/7] lavc/mpegvideo_enc: support AV_CODEC_CAP_ENCODER_RECON_FRAME Anton Khirnov
@ 2023-03-24  9:14 ` Anton Khirnov
  2023-03-24  9:14 ` [FFmpeg-devel] [PATCH 3/7] tools/decode_simple: initialize decoder parameters with container info Anton Khirnov
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Anton Khirnov @ 2023-03-24  9:14 UTC (permalink / raw)
  To: ffmpeg-devel

Currently this would not be done if max_frames is triggered.

Makes no difference in either of the tools currently using
decode_simple, but may be important in future tools.
---
 tools/decode_simple.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/tools/decode_simple.c b/tools/decode_simple.c
index e02323064d..8a9e7c5f71 100644
--- a/tools/decode_simple.c
+++ b/tools/decode_simple.c
@@ -73,7 +73,7 @@ int ds_run(DecodeContext *dc)
     while (ret >= 0) {
         ret = av_read_frame(dc->demuxer, dc->pkt);
         if (ret < 0)
-            goto flush;
+            break;
         if (dc->pkt->stream_index != dc->stream->index) {
             av_packet_unref(dc->pkt);
             continue;
@@ -91,10 +91,9 @@ int ds_run(DecodeContext *dc)
             fprintf(stderr, "Error decoding: %d\n", ret);
             return ret;
         } else if (ret > 0)
-            return 0;
+            goto finish;
     }
 
-flush:
     avcodec_send_packet(dc->decoder, NULL);
     ret = decode_read(dc, 1);
     if (ret < 0) {
@@ -102,7 +101,8 @@ flush:
         return ret;
     }
 
-    return 0;
+finish:
+    return dc->process_frame(dc, NULL);
 }
 
 void ds_free(DecodeContext *dc)
-- 
2.39.1

_______________________________________________
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] 7+ messages in thread

* [FFmpeg-devel] [PATCH 3/7] tools/decode_simple: initialize decoder parameters with container info
  2023-03-24  9:14 [FFmpeg-devel] [RFC PATCH 1/7] lavc/mpegvideo_enc: support AV_CODEC_CAP_ENCODER_RECON_FRAME Anton Khirnov
  2023-03-24  9:14 ` [FFmpeg-devel] [PATCH 2/7] tools/decode_simple: always call process_frame(NULL) at the end Anton Khirnov
@ 2023-03-24  9:14 ` Anton Khirnov
  2023-03-24  9:14 ` [FFmpeg-devel] [PATCH 4/7] tools: add an AV_CODEC_CAP_ENCODER_RECON_FRAME test tool Anton Khirnov
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Anton Khirnov @ 2023-03-24  9:14 UTC (permalink / raw)
  To: ffmpeg-devel

---
 tools/decode_simple.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/tools/decode_simple.c b/tools/decode_simple.c
index 8a9e7c5f71..6532e368d4 100644
--- a/tools/decode_simple.c
+++ b/tools/decode_simple.c
@@ -149,6 +149,10 @@ int ds_open(DecodeContext *dc, const char *url, int stream_idx)
     if (!dc->decoder)
         return AVERROR(ENOMEM);
 
+    ret = avcodec_parameters_to_context(dc->decoder, dc->stream->codecpar);
+    if (ret < 0)
+        goto fail;
+
     return 0;
 
 fail:
-- 
2.39.1

_______________________________________________
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] 7+ messages in thread

* [FFmpeg-devel] [PATCH 4/7] tools: add an AV_CODEC_CAP_ENCODER_RECON_FRAME test tool
  2023-03-24  9:14 [FFmpeg-devel] [RFC PATCH 1/7] lavc/mpegvideo_enc: support AV_CODEC_CAP_ENCODER_RECON_FRAME Anton Khirnov
  2023-03-24  9:14 ` [FFmpeg-devel] [PATCH 2/7] tools/decode_simple: always call process_frame(NULL) at the end Anton Khirnov
  2023-03-24  9:14 ` [FFmpeg-devel] [PATCH 3/7] tools/decode_simple: initialize decoder parameters with container info Anton Khirnov
@ 2023-03-24  9:14 ` Anton Khirnov
  2023-03-24  9:14 ` [FFmpeg-devel] [PATCH 5/7] lavc/avcodec: fix documentation typo Anton Khirnov
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Anton Khirnov @ 2023-03-24  9:14 UTC (permalink / raw)
  To: ffmpeg-devel

---
 Makefile                     |   2 +
 tools/Makefile               |   3 +-
 tools/enc_recon_frame_test.c | 393 +++++++++++++++++++++++++++++++++++
 3 files changed, 397 insertions(+), 1 deletion(-)
 create mode 100644 tools/enc_recon_frame_test.c

diff --git a/Makefile b/Makefile
index 1fb742f390..bf1b69f96b 100644
--- a/Makefile
+++ b/Makefile
@@ -67,6 +67,8 @@ tools/target_io_dem_fuzzer$(EXESUF): tools/target_io_dem_fuzzer.o $(FF_DEP_LIBS)
 
 tools/enum_options$(EXESUF): ELIBS = $(FF_EXTRALIBS)
 tools/enum_options$(EXESUF): $(FF_DEP_LIBS)
+tools/enc_recon_frame_test$(EXESUF): $(FF_DEP_LIBS)
+tools/enc_recon_frame_test$(EXESUF): ELIBS = $(FF_EXTRALIBS)
 tools/scale_slice_test$(EXESUF): $(FF_DEP_LIBS)
 tools/scale_slice_test$(EXESUF): ELIBS = $(FF_EXTRALIBS)
 tools/sofa2wavs$(EXESUF): ELIBS = $(FF_EXTRALIBS)
diff --git a/tools/Makefile b/tools/Makefile
index 4afa23342d..dee6a41668 100644
--- a/tools/Makefile
+++ b/tools/Makefile
@@ -1,4 +1,4 @@
-TOOLS = enum_options qt-faststart scale_slice_test trasher uncoded_frame
+TOOLS = enc_recon_frame_test enum_options qt-faststart scale_slice_test trasher uncoded_frame
 TOOLS-$(CONFIG_LIBMYSOFA) += sofa2wavs
 TOOLS-$(CONFIG_ZLIB) += cws2fws
 
@@ -17,6 +17,7 @@ tools/target_dem_fuzzer.o: tools/target_dem_fuzzer.c
 tools/target_io_dem_fuzzer.o: tools/target_dem_fuzzer.c
 	$(COMPILE_C) -DIO_FLAT=0
 
+tools/enc_recon_frame_test$(EXESUF): tools/decode_simple.o
 tools/venc_data_dump$(EXESUF): tools/decode_simple.o
 tools/scale_slice_test$(EXESUF): tools/decode_simple.o
 
diff --git a/tools/enc_recon_frame_test.c b/tools/enc_recon_frame_test.c
new file mode 100644
index 0000000000..d23accd49d
--- /dev/null
+++ b/tools/enc_recon_frame_test.c
@@ -0,0 +1,393 @@
+/*
+ * copyright (c) 2022 Anton Khirnov <anton@khirnov.net>
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/* A test for AV_CODEC_FLAG_RECON_FRAME
+ * TODO: dump reconstructed frames to disk */
+
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+
+#include "decode_simple.h"
+
+#include "libavutil/adler32.h"
+#include "libavutil/common.h"
+#include "libavutil/error.h"
+#include "libavutil/frame.h"
+#include "libavutil/imgutils.h"
+#include "libavutil/opt.h"
+
+#include "libavformat/avformat.h"
+
+#include "libavcodec/avcodec.h"
+#include "libavcodec/codec.h"
+
+#include "libswscale/swscale.h"
+
+typedef struct FrameChecksum {
+    int64_t  ts;
+    uint32_t checksum[4];
+} FrameChecksum;
+
+typedef struct PrivData {
+    AVCodecContext *enc;
+    AVCodecContext *dec;
+
+    int64_t pts_in;
+
+    AVPacket *pkt;
+    AVFrame  *frame, *frame_recon;
+
+    struct SwsContext *scaler;
+
+    FrameChecksum *checksums_decoded;
+    size_t      nb_checksums_decoded;
+    FrameChecksum *checksums_recon;
+    size_t      nb_checksums_recon;
+} PrivData;
+
+static int frame_hash(FrameChecksum **pc, size_t *nb_c, int64_t ts,
+                      const AVFrame *frame)
+{
+    FrameChecksum *c;
+    int shift_h[4] = { 0 }, shift_v[4] = { 0 };
+
+    c = av_realloc_array(*pc, *nb_c + 1, sizeof(*c));
+    if (!c)
+        return AVERROR(ENOMEM);
+    *pc = c;
+    (*nb_c)++;
+
+    c += *nb_c - 1;
+    memset(c, 0, sizeof(*c));
+
+    av_pix_fmt_get_chroma_sub_sample(frame->format, &shift_h[1], &shift_v[1]);
+    shift_h[2] = shift_h[1];
+    shift_v[2] = shift_v[1];
+
+    c->ts = ts;
+    for (int p = 0; frame->data[p]; p++) {
+        const uint8_t *data = frame->data[p];
+        int linesize = av_image_get_linesize(frame->format, frame->width, p);
+        uint32_t checksum = 0;
+
+        for (int j = 0; j < frame->height >> shift_v[p]; j++) {
+            checksum = av_adler32_update(checksum, data, linesize);
+            data += frame->linesize[p];
+        }
+
+        c->checksum[p] = checksum;
+    }
+
+    return 0;
+}
+
+static int recon_frame_process(PrivData *pd, const AVPacket *pkt)
+{
+    AVFrame *f = pd->frame_recon;
+    int ret;
+
+    ret = avcodec_receive_frame(pd->enc, f);
+    if (ret < 0) {
+        fprintf(stderr, "Error retrieving a reconstructed frame\n");
+        return ret;
+    }
+
+    // the encoder's internal format (in which the reconsturcted frames are
+    // exported) may be different from the user-facing pixel format
+    if (f->format != pd->enc->pix_fmt) {
+        if (!pd->scaler) {
+            pd->scaler = sws_getContext(f->width, f->height, f->format,
+                                        f->width, f->height, pd->enc->pix_fmt,
+                                        SWS_BITEXACT, NULL, NULL, NULL);
+            if (!pd->scaler)
+                return AVERROR(ENOMEM);
+        }
+
+        ret = sws_scale_frame(pd->scaler, pd->frame, f);
+        if (ret < 0) {
+            fprintf(stderr, "Error converting pixel formats\n");
+            return ret;
+        }
+
+        av_frame_unref(f);
+        f = pd->frame;
+    }
+
+    ret = frame_hash(&pd->checksums_recon, &pd->nb_checksums_recon,
+                     pkt->pts, f);
+    av_frame_unref(f);
+
+    return 0;
+}
+
+static int process_frame(DecodeContext *dc, AVFrame *frame)
+{
+    PrivData *pd = dc->opaque;
+    int ret;
+
+    if (!avcodec_is_open(pd->enc)) {
+        if (!frame) {
+            fprintf(stderr, "No input frames were decoded\n");
+            return AVERROR_INVALIDDATA;
+        }
+
+        pd->enc->width        = frame->width;
+        pd->enc->height       = frame->height;
+        pd->enc->pix_fmt      = frame->format;
+        pd->enc->thread_count = dc->decoder->thread_count;
+        pd->enc->thread_type  = dc->decoder->thread_type;
+
+        // real timestamps do not matter for this test, so we just
+        // pretend the input is 25fps CFR to avoid any timestamp issues
+        pd->enc->time_base    = (AVRational){ 1, 25 };
+
+        ret = avcodec_open2(pd->enc, NULL, NULL);
+        if (ret < 0) {
+            fprintf(stderr, "Error opening the encoder\n");
+            return ret;
+        }
+    }
+
+    if (frame) {
+        frame->pts       = pd->pts_in++;
+
+        // avoid forcing coded frame type
+        frame->pict_type = AV_PICTURE_TYPE_NONE;
+    }
+
+    ret = avcodec_send_frame(pd->enc, frame);
+    if (ret < 0) {
+        fprintf(stderr, "Error submitting a frame for encoding\n");
+        return ret;
+    }
+
+    while (1) {
+        AVPacket *pkt = pd->pkt;
+
+        ret = avcodec_receive_packet(pd->enc, pkt);
+        if (ret == AVERROR(EAGAIN))
+            break;
+        else if (ret == AVERROR_EOF)
+            pkt = NULL;
+        else if (ret < 0) {
+            fprintf(stderr, "Error receiving a frame from the encoder\n");
+            return ret;
+        }
+
+        if (pkt) {
+            ret = recon_frame_process(pd, pkt);
+            if (ret < 0)
+                return ret;
+        }
+
+        if (!avcodec_is_open(pd->dec)) {
+            if (!pkt) {
+                fprintf(stderr, "No packets were received from the encoder\n");
+                return AVERROR(EINVAL);
+            }
+
+            pd->dec->width        = pd->enc->width;
+            pd->dec->height       = pd->enc->height;
+            pd->dec->pix_fmt      = pd->enc->pix_fmt;
+            pd->dec->thread_count = dc->decoder->thread_count;
+            pd->dec->thread_type  = dc->decoder->thread_type;
+            if (pd->enc->extradata_size) {
+                pd->dec->extradata = av_memdup(pd->enc->extradata,
+                                               pd->enc->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
+                if (!pd->dec->extradata)
+                    return AVERROR(ENOMEM);
+            }
+
+            ret = avcodec_open2(pd->dec, NULL, NULL);
+            if (ret < 0) {
+                fprintf(stderr, "Error opening the decoder\n");
+                return ret;
+            }
+        }
+
+        ret = avcodec_send_packet(pd->dec, pkt);
+        if (ret < 0) {
+            fprintf(stderr, "Error sending a packet to decoder\n");
+            return ret;
+        }
+
+        while (1) {
+            ret = avcodec_receive_frame(pd->dec, pd->frame);
+            if (ret == AVERROR(EAGAIN))
+                break;
+            else if (ret == AVERROR_EOF)
+                return 0;
+            else if (ret < 0) {
+                fprintf(stderr, "Error receving a frame from decoder\n");
+                return ret;
+            }
+
+            ret = frame_hash(&pd->checksums_decoded, &pd->nb_checksums_decoded,
+                             pd->frame->pts, pd->frame);
+            av_frame_unref(pd->frame);
+            if (ret < 0)
+                return ret;
+        }
+
+    }
+
+    return 0;
+}
+
+static int frame_checksum_compare(const void *a, const void *b)
+{
+    const FrameChecksum *ca = a;
+    const FrameChecksum *cb = b;
+    if (ca->ts == cb->ts)
+        return 0;
+    return FFSIGN(ca->ts - cb->ts);
+}
+
+int main(int argc, char **argv)
+{
+    PrivData      pd;
+    DecodeContext dc;
+
+    const char *filename, *enc_name, *enc_opts, *thread_type = NULL, *nb_threads = NULL;
+    const AVCodec *enc, *dec;
+    int ret = 0, max_frames = 0;
+
+    if (argc < 4) {
+        fprintf(stderr,
+                "Usage: %s <input file> <encoder> <encoder options> "
+                "[<max frame count> [<thread count> <thread type>]\n",
+                argv[0]);
+        return 0;
+    }
+
+    filename     = argv[1];
+    enc_name     = argv[2];
+    enc_opts     = argv[3];
+    if (argc >= 5)
+        max_frames = strtol(argv[4], NULL, 0);
+    if (argc >= 6)
+        nb_threads = argv[5];
+    if (argc >= 7)
+        thread_type = argv[6];
+
+    memset(&dc, 0, sizeof(dc));
+    memset(&pd, 0, sizeof(pd));
+
+    enc = avcodec_find_encoder_by_name(enc_name);
+    if (!enc) {
+        fprintf(stderr, "No such encoder: %s\n", enc_name);
+        return 1;
+    }
+    if (!(enc->capabilities & AV_CODEC_CAP_ENCODER_RECON_FRAME)) {
+        fprintf(stderr, "Encoder '%s' cannot ouput reconstructed frames\n",
+                enc->name);
+        return 1;
+    }
+
+    dec = avcodec_find_decoder(enc->id);
+    if (!dec) {
+        fprintf(stderr, "No decoder for: %s\n", avcodec_get_name(enc->id));
+        return 1;
+    }
+
+    pd.enc = avcodec_alloc_context3(enc);
+    if (!pd.enc) {
+        fprintf(stderr, "Error allocating encoder\n");
+        return 1;
+    }
+
+    ret = av_set_options_string(pd.enc, enc_opts, "=", ",");
+    if (ret < 0) {
+        fprintf(stderr, "Error setting encoder options\n");
+        goto fail;
+    }
+    pd.enc->flags |= AV_CODEC_FLAG_RECON_FRAME | AV_CODEC_FLAG_BITEXACT;
+
+    pd.dec = avcodec_alloc_context3(dec);
+    if (!pd.dec) {
+        fprintf(stderr, "Error allocating decoder\n");
+        goto fail;
+    }
+
+    pd.dec->flags           |= AV_CODEC_FLAG_BITEXACT;
+    pd.dec->err_recognition |= AV_EF_CRCCHECK;
+
+    pd.frame       = av_frame_alloc();
+    pd.frame_recon = av_frame_alloc();
+    pd.pkt         = av_packet_alloc();
+    if (!pd.frame ||!pd.frame_recon || !pd.pkt) {
+        ret = 1;
+        goto fail;
+    }
+
+    ret = ds_open(&dc, filename, 0);
+    if (ret < 0) {
+        fprintf(stderr, "Error opening the file\n");
+        goto fail;
+    }
+
+    dc.process_frame = process_frame;
+    dc.opaque        = &pd;
+    dc.max_frames    = max_frames;
+
+    ret  = av_dict_set(&dc.decoder_opts, "threads",          nb_threads,    0);
+    ret |= av_dict_set(&dc.decoder_opts, "thread_type",      thread_type,   0);
+
+    ret = ds_run(&dc);
+    if (ret < 0)
+        goto fail;
+
+    if (pd.nb_checksums_decoded != pd.nb_checksums_recon) {
+        fprintf(stderr, "Mismatching frame counts: recon=%zu decoded=%zu\n",
+                pd.nb_checksums_recon, pd.nb_checksums_decoded);
+        ret = 1;
+        goto fail;
+    }
+
+    // reconstructed frames are in coded order, sort them by pts into presentation order
+    qsort(pd.checksums_recon, pd.nb_checksums_recon, sizeof(*pd.checksums_recon),
+          frame_checksum_compare);
+
+    for (size_t i = 0; i < pd.nb_checksums_decoded; i++) {
+        const FrameChecksum *d = &pd.checksums_decoded[i];
+        const FrameChecksum *r = &pd.checksums_recon[i];
+
+        for (int p = 0; p < FF_ARRAY_ELEMS(d->checksum); p++)
+            if (d->checksum[p] != r->checksum[p]) {
+                fprintf(stderr, "Checksum mismatch in frame ts=%"PRId64", plane %d\n",
+                        d->ts, p);
+                ret = 1;
+                goto fail;
+            }
+    }
+    fprintf(stderr, "All %zu encoded frames match\n", pd.nb_checksums_decoded);
+
+fail:
+    avcodec_free_context(&pd.enc);
+    avcodec_free_context(&pd.dec);
+    av_freep(&pd.checksums_decoded);
+    av_freep(&pd.checksums_recon);
+    av_frame_free(&pd.frame);
+    av_frame_free(&pd.frame_recon);
+    av_packet_free(&pd.pkt);
+    ds_free(&dc);
+    return !!ret;
+}
-- 
2.39.1

_______________________________________________
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] 7+ messages in thread

* [FFmpeg-devel] [PATCH 5/7] lavc/avcodec: fix documentation typo
  2023-03-24  9:14 [FFmpeg-devel] [RFC PATCH 1/7] lavc/mpegvideo_enc: support AV_CODEC_CAP_ENCODER_RECON_FRAME Anton Khirnov
                   ` (2 preceding siblings ...)
  2023-03-24  9:14 ` [FFmpeg-devel] [PATCH 4/7] tools: add an AV_CODEC_CAP_ENCODER_RECON_FRAME test tool Anton Khirnov
@ 2023-03-24  9:14 ` Anton Khirnov
  2023-03-24  9:14 ` [FFmpeg-devel] [PATCH 6/7] lavc: expand doxy for AV_CODEC_FLAG_RECON_FRAME Anton Khirnov
  2023-03-24  9:14 ` [FFmpeg-devel] [PATCH 7/7] lavc: turn mentions of AV_CODEC_FLAG_RECON_FRAME in doxy into links Anton Khirnov
  5 siblings, 0 replies; 7+ messages in thread
From: Anton Khirnov @ 2023-03-24  9:14 UTC (permalink / raw)
  To: ffmpeg-devel

---
 libavcodec/avcodec.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index 30f1d312f4..d1bcb3026c 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -243,7 +243,7 @@ typedef struct RcOverride{
 #define AV_CODEC_FLAG_RECON_FRAME     (1 <<  6)
 /**
  * @par decoding
- * Request the decoder to propagate each packets AVPacket.opaque and
+ * Request the decoder to propagate each packet's AVPacket.opaque and
  * AVPacket.opaque_ref to its corresponding output AVFrame.
  *
  * @par encoding:
-- 
2.39.1

_______________________________________________
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] 7+ messages in thread

* [FFmpeg-devel] [PATCH 6/7] lavc: expand doxy for AV_CODEC_FLAG_RECON_FRAME
  2023-03-24  9:14 [FFmpeg-devel] [RFC PATCH 1/7] lavc/mpegvideo_enc: support AV_CODEC_CAP_ENCODER_RECON_FRAME Anton Khirnov
                   ` (3 preceding siblings ...)
  2023-03-24  9:14 ` [FFmpeg-devel] [PATCH 5/7] lavc/avcodec: fix documentation typo Anton Khirnov
@ 2023-03-24  9:14 ` Anton Khirnov
  2023-03-24  9:14 ` [FFmpeg-devel] [PATCH 7/7] lavc: turn mentions of AV_CODEC_FLAG_RECON_FRAME in doxy into links Anton Khirnov
  5 siblings, 0 replies; 7+ messages in thread
From: Anton Khirnov @ 2023-03-24  9:14 UTC (permalink / raw)
  To: ffmpeg-devel

---
 libavcodec/avcodec.h | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index d1bcb3026c..7a6eb4c0e2 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -239,6 +239,15 @@ typedef struct RcOverride{
  *
  * Should only be used with encoders flagged with the
  * @ref AV_CODEC_CAP_ENCODER_RECON_FRAME capability.
+ *
+ * @note
+ * Each reconstructed frame returned by the encoder corresponds to the last
+ * encoded packet, i.e. the frames are returned in coded order rather than
+ * presentation order.
+ *
+ * @note
+ * Frame parameters (like pixel format or dimensions) do not have to match the
+ * AVCodecContext values. Make sure to use the values from the returned frame.
  */
 #define AV_CODEC_FLAG_RECON_FRAME     (1 <<  6)
 /**
-- 
2.39.1

_______________________________________________
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] 7+ messages in thread

* [FFmpeg-devel] [PATCH 7/7] lavc: turn mentions of AV_CODEC_FLAG_RECON_FRAME in doxy into links
  2023-03-24  9:14 [FFmpeg-devel] [RFC PATCH 1/7] lavc/mpegvideo_enc: support AV_CODEC_CAP_ENCODER_RECON_FRAME Anton Khirnov
                   ` (4 preceding siblings ...)
  2023-03-24  9:14 ` [FFmpeg-devel] [PATCH 6/7] lavc: expand doxy for AV_CODEC_FLAG_RECON_FRAME Anton Khirnov
@ 2023-03-24  9:14 ` Anton Khirnov
  5 siblings, 0 replies; 7+ messages in thread
From: Anton Khirnov @ 2023-03-24  9:14 UTC (permalink / raw)
  To: ffmpeg-devel

---
 libavcodec/avcodec.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index 7a6eb4c0e2..18ca0e2494 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -2665,7 +2665,7 @@ int avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt);
 
 /**
  * Return decoded output data from a decoder or encoder (when the
- * AV_CODEC_FLAG_RECON_FRAME flag is used).
+ * @ref AV_CODEC_FLAG_RECON_FRAME flag is used).
  *
  * @param avctx codec context
  * @param frame This will be set to a reference-counted video or audio
@@ -2679,7 +2679,7 @@ int avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt);
  * @retval AVERROR_EOF      the codec has been fully flushed, and there will be
  *                          no more output frames
  * @retval AVERROR(EINVAL)  codec not opened, or it is an encoder without the
- *                          AV_CODEC_FLAG_RECON_FRAME flag enabled
+ *                          @ref AV_CODEC_FLAG_RECON_FRAME flag enabled
  * @retval AVERROR_INPUT_CHANGED current decoded frame has changed parameters with
  *                          respect to first decoded frame. Applicable when flag
  *                          AV_CODEC_FLAG_DROPCHANGED is set.
-- 
2.39.1

_______________________________________________
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] 7+ messages in thread

end of thread, other threads:[~2023-03-24  9:16 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-24  9:14 [FFmpeg-devel] [RFC PATCH 1/7] lavc/mpegvideo_enc: support AV_CODEC_CAP_ENCODER_RECON_FRAME Anton Khirnov
2023-03-24  9:14 ` [FFmpeg-devel] [PATCH 2/7] tools/decode_simple: always call process_frame(NULL) at the end Anton Khirnov
2023-03-24  9:14 ` [FFmpeg-devel] [PATCH 3/7] tools/decode_simple: initialize decoder parameters with container info Anton Khirnov
2023-03-24  9:14 ` [FFmpeg-devel] [PATCH 4/7] tools: add an AV_CODEC_CAP_ENCODER_RECON_FRAME test tool Anton Khirnov
2023-03-24  9:14 ` [FFmpeg-devel] [PATCH 5/7] lavc/avcodec: fix documentation typo Anton Khirnov
2023-03-24  9:14 ` [FFmpeg-devel] [PATCH 6/7] lavc: expand doxy for AV_CODEC_FLAG_RECON_FRAME Anton Khirnov
2023-03-24  9:14 ` [FFmpeg-devel] [PATCH 7/7] lavc: turn mentions of AV_CODEC_FLAG_RECON_FRAME in doxy into links Anton Khirnov

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