[FFmpeg-devel] [PATCH 19/39] avcodec/pnmenc: Avoid intermediate buffer, allow user-supplied buffers

Andreas Rheinhardt andreas.rheinhardt at outlook.com
Fri May 21 12:17:22 EEST 2021


For all p*m encoders a very sharp upper bound for the size of the
output packets is available before the packet is allocated. This can
be used to avoid an intermediate buffer when encoding by using
ff_get_encode_buffer() instead of ff_alloc_packet2() (without min_size);
this also adds support for user-supplied buffers.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at outlook.com>
---
 libavcodec/pnmenc.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/libavcodec/pnmenc.c b/libavcodec/pnmenc.c
index 80c7b66332..2e8b6372a3 100644
--- a/libavcodec/pnmenc.c
+++ b/libavcodec/pnmenc.c
@@ -23,6 +23,7 @@
 #include "libavutil/imgutils.h"
 #include "libavutil/pixdesc.h"
 #include "avcodec.h"
+#include "encode.h"
 #include "internal.h"
 
 static int pnm_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
@@ -34,7 +35,7 @@ static int pnm_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
     int size = av_image_get_buffer_size(avctx->pix_fmt,
                                         avctx->width, avctx->height, 1);
 
-    if ((ret = ff_alloc_packet2(avctx, pkt, size + 200, 0)) < 0)
+    if ((ret = ff_get_encode_buffer(avctx, pkt, size + 200, 0)) < 0)
         return ret;
 
     bytestream_start =
@@ -141,8 +142,8 @@ static int pnm_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
                 ptr2 += p->linesize[2];
         }
     }
-    pkt->size   = bytestream - bytestream_start;
     pkt->flags |= AV_PKT_FLAG_KEY;
+    av_shrink_packet(pkt, bytestream - bytestream_start);
     *got_packet = 1;
 
     return 0;
@@ -154,6 +155,7 @@ const AVCodec ff_pgm_encoder = {
     .long_name      = NULL_IF_CONFIG_SMALL("PGM (Portable GrayMap) image"),
     .type           = AVMEDIA_TYPE_VIDEO,
     .id             = AV_CODEC_ID_PGM,
+    .capabilities   = AV_CODEC_CAP_DR1,
     .encode2        = pnm_encode_frame,
     .pix_fmts       = (const enum AVPixelFormat[]){
         AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_NONE
@@ -168,6 +170,7 @@ const AVCodec ff_pgmyuv_encoder = {
     .long_name      = NULL_IF_CONFIG_SMALL("PGMYUV (Portable GrayMap YUV) image"),
     .type           = AVMEDIA_TYPE_VIDEO,
     .id             = AV_CODEC_ID_PGMYUV,
+    .capabilities   = AV_CODEC_CAP_DR1,
     .encode2        = pnm_encode_frame,
     .pix_fmts       = (const enum AVPixelFormat[]){
         AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV420P16BE, AV_PIX_FMT_NONE
@@ -182,6 +185,7 @@ const AVCodec ff_ppm_encoder = {
     .long_name      = NULL_IF_CONFIG_SMALL("PPM (Portable PixelMap) image"),
     .type           = AVMEDIA_TYPE_VIDEO,
     .id             = AV_CODEC_ID_PPM,
+    .capabilities   = AV_CODEC_CAP_DR1,
     .encode2        = pnm_encode_frame,
     .pix_fmts       = (const enum AVPixelFormat[]){
         AV_PIX_FMT_RGB24, AV_PIX_FMT_RGB48BE, AV_PIX_FMT_NONE
@@ -196,6 +200,7 @@ const AVCodec ff_pbm_encoder = {
     .long_name      = NULL_IF_CONFIG_SMALL("PBM (Portable BitMap) image"),
     .type           = AVMEDIA_TYPE_VIDEO,
     .id             = AV_CODEC_ID_PBM,
+    .capabilities   = AV_CODEC_CAP_DR1,
     .encode2        = pnm_encode_frame,
     .pix_fmts       = (const enum AVPixelFormat[]){ AV_PIX_FMT_MONOWHITE,
                                                   AV_PIX_FMT_NONE },
@@ -209,6 +214,7 @@ const AVCodec ff_pfm_encoder = {
     .long_name      = NULL_IF_CONFIG_SMALL("PFM (Portable FloatMap) image"),
     .type           = AVMEDIA_TYPE_VIDEO,
     .id             = AV_CODEC_ID_PFM,
+    .capabilities   = AV_CODEC_CAP_DR1,
     .encode2        = pnm_encode_frame,
     .pix_fmts       = (const enum AVPixelFormat[]){ AV_PIX_FMT_GBRPF32,
                                                     AV_PIX_FMT_NONE },
-- 
2.27.0



More information about the ffmpeg-devel mailing list