diff mbox series

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

Message ID HE1PR0301MB2154D569F057BF7C376355258F299@HE1PR0301MB2154.eurprd03.prod.outlook.com
State Accepted
Commit 981c4caed32df4a801608d72bb9d0243390a2e1a
Headers show
Series [FFmpeg-devel,01/39] avcodec/audiotoolboxenc: Remove AV_CODEC_CAP_DR1 | expand

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

Andreas Rheinhardt May 21, 2021, 9:17 a.m. UTC
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@outlook.com>
---
 libavcodec/pnmenc.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
diff mbox series

Patch

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 },