avcodec/pnmenc: Avoid intermediate buffer, allow user-supplied buffers

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.

Reviewed-by: James Almer <jamrial@gmail.com>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
pull/362/head
Andreas Rheinhardt 4 years ago
parent ea2ec1dfae
commit 981c4caed3
  1. 10
      libavcodec/pnmenc.c

@ -23,6 +23,7 @@
#include "libavutil/imgutils.h" #include "libavutil/imgutils.h"
#include "libavutil/pixdesc.h" #include "libavutil/pixdesc.h"
#include "avcodec.h" #include "avcodec.h"
#include "encode.h"
#include "internal.h" #include "internal.h"
static int pnm_encode_frame(AVCodecContext *avctx, AVPacket *pkt, 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, int size = av_image_get_buffer_size(avctx->pix_fmt,
avctx->width, avctx->height, 1); 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; return ret;
bytestream_start = bytestream_start =
@ -141,8 +142,8 @@ static int pnm_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
ptr2 += p->linesize[2]; ptr2 += p->linesize[2];
} }
} }
pkt->size = bytestream - bytestream_start;
pkt->flags |= AV_PKT_FLAG_KEY; pkt->flags |= AV_PKT_FLAG_KEY;
av_shrink_packet(pkt, bytestream - bytestream_start);
*got_packet = 1; *got_packet = 1;
return 0; return 0;
@ -154,6 +155,7 @@ const AVCodec ff_pgm_encoder = {
.long_name = NULL_IF_CONFIG_SMALL("PGM (Portable GrayMap) image"), .long_name = NULL_IF_CONFIG_SMALL("PGM (Portable GrayMap) image"),
.type = AVMEDIA_TYPE_VIDEO, .type = AVMEDIA_TYPE_VIDEO,
.id = AV_CODEC_ID_PGM, .id = AV_CODEC_ID_PGM,
.capabilities = AV_CODEC_CAP_DR1,
.encode2 = pnm_encode_frame, .encode2 = pnm_encode_frame,
.pix_fmts = (const enum AVPixelFormat[]){ .pix_fmts = (const enum AVPixelFormat[]){
AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_NONE 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"), .long_name = NULL_IF_CONFIG_SMALL("PGMYUV (Portable GrayMap YUV) image"),
.type = AVMEDIA_TYPE_VIDEO, .type = AVMEDIA_TYPE_VIDEO,
.id = AV_CODEC_ID_PGMYUV, .id = AV_CODEC_ID_PGMYUV,
.capabilities = AV_CODEC_CAP_DR1,
.encode2 = pnm_encode_frame, .encode2 = pnm_encode_frame,
.pix_fmts = (const enum AVPixelFormat[]){ .pix_fmts = (const enum AVPixelFormat[]){
AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV420P16BE, AV_PIX_FMT_NONE 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"), .long_name = NULL_IF_CONFIG_SMALL("PPM (Portable PixelMap) image"),
.type = AVMEDIA_TYPE_VIDEO, .type = AVMEDIA_TYPE_VIDEO,
.id = AV_CODEC_ID_PPM, .id = AV_CODEC_ID_PPM,
.capabilities = AV_CODEC_CAP_DR1,
.encode2 = pnm_encode_frame, .encode2 = pnm_encode_frame,
.pix_fmts = (const enum AVPixelFormat[]){ .pix_fmts = (const enum AVPixelFormat[]){
AV_PIX_FMT_RGB24, AV_PIX_FMT_RGB48BE, AV_PIX_FMT_NONE 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"), .long_name = NULL_IF_CONFIG_SMALL("PBM (Portable BitMap) image"),
.type = AVMEDIA_TYPE_VIDEO, .type = AVMEDIA_TYPE_VIDEO,
.id = AV_CODEC_ID_PBM, .id = AV_CODEC_ID_PBM,
.capabilities = AV_CODEC_CAP_DR1,
.encode2 = pnm_encode_frame, .encode2 = pnm_encode_frame,
.pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_MONOWHITE, .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_MONOWHITE,
AV_PIX_FMT_NONE }, AV_PIX_FMT_NONE },
@ -209,6 +214,7 @@ const AVCodec ff_pfm_encoder = {
.long_name = NULL_IF_CONFIG_SMALL("PFM (Portable FloatMap) image"), .long_name = NULL_IF_CONFIG_SMALL("PFM (Portable FloatMap) image"),
.type = AVMEDIA_TYPE_VIDEO, .type = AVMEDIA_TYPE_VIDEO,
.id = AV_CODEC_ID_PFM, .id = AV_CODEC_ID_PFM,
.capabilities = AV_CODEC_CAP_DR1,
.encode2 = pnm_encode_frame, .encode2 = pnm_encode_frame,
.pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_GBRPF32, .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_GBRPF32,
AV_PIX_FMT_NONE }, AV_PIX_FMT_NONE },

Loading…
Cancel
Save