avcodec/encode: Always use intermediate buffer in ff_alloc_packet2()

Up until now, ff_alloc_packet2() has a min_size parameter:
It is supposed to be a lower bound on the final size of the packet
to allocate. If it is not too far from the upper bound (namely,
if it is at least half the upper bound), then ff_alloc_packet2()
already allocates the final, already refcounted packet; if it is
not, then the packet is not refcounted and its data only points to
a buffer owned by the AVCodecContext (in this case, the packet will
be made refcounted in encode_simple_internal() in libavcodec/encode.c).
The goal of this was to avoid data copies and intermediate buffers
if one has a precise lower bound.

Yet those encoders for which precise lower bounds exist have recently
been switched to ff_get_encode_buffer() (which automatically allocates
final buffers), leaving only two encoders to actually set the min_size
to something else than zero (namely aliaspixenc and hapenc). Both of
these encoders use a very low lower bound that is not helpful in any
nontrivial case.

This commit therefore removes the min_size parameter as well as the
codepath in ff_alloc_packet2() for the allocation of final buffers.
Furthermore, the function has been renamed to ff_alloc_packet() and
moved to encode.h alongside ff_get_encode_buffer().

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
This commit is contained in:
Andreas Rheinhardt 2021-05-11 15:17:13 +02:00
parent 4e5d2a819c
commit 56e9e0273a
54 changed files with 130 additions and 94 deletions

View File

@ -34,6 +34,7 @@
#include "libavutil/float_dsp.h"
#include "libavutil/opt.h"
#include "avcodec.h"
#include "encode.h"
#include "put_bits.h"
#include "internal.h"
#include "mpeg4audio.h"
@ -676,7 +677,7 @@ static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
}
start_ch += chans;
}
if ((ret = ff_alloc_packet2(avctx, avpkt, 8192 * s->channels, 0)) < 0)
if ((ret = ff_alloc_packet(avctx, avpkt, 8192 * s->channels)) < 0)
return ret;
frame_bits = its = 0;
do {

View File

@ -22,6 +22,7 @@
#include "libavutil/opt.h"
#include "avcodec.h"
#include "encode.h"
#include "put_bits.h"
#include "internal.h"
#include "lpc.h"
@ -589,7 +590,7 @@ static int alac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
else
max_frame_size = s->max_coded_frame_size;
if ((ret = ff_alloc_packet2(avctx, avpkt, 4 * max_frame_size, 0)) < 0)
if ((ret = ff_alloc_packet(avctx, avpkt, 4 * max_frame_size)) < 0)
return ret;
/* use verbatim mode for compression_level 0 */

View File

@ -23,6 +23,7 @@
#include "avcodec.h"
#include "bytestream.h"
#include "encode.h"
#include "internal.h"
#define ALIAS_HEADER_SIZE 10
@ -54,10 +55,8 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
}
length = ALIAS_HEADER_SIZE + 4 * width * height; // max possible
if ((ret = ff_alloc_packet2(avctx, pkt, length, ALIAS_HEADER_SIZE + height*2)) < 0) {
av_log(avctx, AV_LOG_ERROR, "Error getting output packet of size %d.\n", length);
if ((ret = ff_alloc_packet(avctx, pkt, length)) < 0)
return ret;
}
buf = pkt->data;

View File

@ -30,6 +30,7 @@
#include "asv.h"
#include "avcodec.h"
#include "dct.h"
#include "encode.h"
#include "fdctdsp.h"
#include "internal.h"
#include "mpeg12data.h"
@ -255,8 +256,8 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
return ret;
}
if ((ret = ff_alloc_packet2(avctx, pkt, a->mb_height * a->mb_width * MAX_MB_SIZE +
AV_INPUT_BUFFER_MIN_SIZE, 0)) < 0)
if ((ret = ff_alloc_packet(avctx, pkt, a->mb_height * a->mb_width * MAX_MB_SIZE +
AV_INPUT_BUFFER_MIN_SIZE)) < 0)
return ret;
init_put_bits(&a->pb, pkt->data, pkt->size);

View File

@ -29,6 +29,7 @@
#include "audio_frame_queue.h"
#include "avcodec.h"
#include "bytestream.h"
#include "encode.h"
#include "internal.h"
#include "libavformat/isom.h"
#include "libavutil/avassert.h"
@ -536,7 +537,7 @@ static int ffat_encode(AVCodecContext *avctx, AVPacket *avpkt,
at->eof = 1;
}
if ((ret = ff_alloc_packet2(avctx, avpkt, at->pkt_size, 0)) < 0)
if ((ret = ff_alloc_packet(avctx, avpkt, at->pkt_size)) < 0)
return ret;

View File

@ -34,6 +34,7 @@
#include "bytestream.h"
#include "cfhd.h"
#include "cfhdencdsp.h"
#include "encode.h"
#include "put_bits.h"
#include "internal.h"
#include "thread.h"
@ -547,7 +548,7 @@ static int cfhd_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
width, height * 2);
}
ret = ff_alloc_packet2(avctx, pkt, 64LL + s->planes * (2LL * avctx->width * avctx->height + 1000LL), 0);
ret = ff_alloc_packet(avctx, pkt, 64LL + s->planes * (2LL * avctx->width * avctx->height + 1000LL));
if (ret < 0)
return ret;

View File

@ -45,6 +45,7 @@
#include "avcodec.h"
#include "elbg.h"
#include "encode.h"
#include "internal.h"
#define CVID_HEADER_SIZE 10
@ -1140,7 +1141,7 @@ static int cinepak_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
s->lambda = frame->quality ? frame->quality - 1 : 2 * FF_LAMBDA_SCALE;
if ((ret = ff_alloc_packet2(avctx, pkt, s->frame_buf_size, 0)) < 0)
if ((ret = ff_alloc_packet(avctx, pkt, s->frame_buf_size)) < 0)
return ret;
ret = rd_frame(s, frame, (s->curframe == 0), pkt->data, s->frame_buf_size);
pkt->size = ret;

View File

@ -30,7 +30,7 @@
#include "frame_thread_encoder.h"
#include "internal.h"
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
{
if (size < 0 || size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
av_log(avctx, AV_LOG_ERROR, "Invalid minimum required packet size %"PRId64" (max allowed is %d)\n",
@ -40,18 +40,14 @@ int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64
av_assert0(!avpkt->data);
if (avctx && 2*min_size < size) { // FIXME The factor needs to be finetuned
av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
avpkt->data = avctx->internal->byte_buffer;
avpkt->size = size;
}
av_fast_padded_malloc(&avctx->internal->byte_buffer,
&avctx->internal->byte_buffer_size, size);
avpkt->data = avctx->internal->byte_buffer;
if (!avpkt->data) {
int ret = av_new_packet(avpkt, size);
if (ret < 0)
av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %"PRId64"\n", size);
return ret;
av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %"PRId64"\n", size);
return AVERROR(ENOMEM);
}
avpkt->size = size;
return 0;
}

View File

@ -44,6 +44,21 @@ int ff_encode_get_frame(AVCodecContext *avctx, AVFrame *frame);
*/
int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags);
/**
* Check AVPacket size and allocate data.
*
* Encoders supporting AVCodec.encode2() can use this as a convenience to
* obtain a big enough buffer for the encoded bitstream.
*
* @param avctx the AVCodecContext of the encoder
* @param avpkt The AVPacket: on success, avpkt->data will point to a buffer
* of size at least `size`; the packet will not be refcounted.
* This packet must be initially blank.
* @param size an upper bound of the size of the packet to encode
* @return non negative on success, negative error code on failure
*/
int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size);
/*
* Perform encoder initialization and validation.
* Called when opening the encoder, before the AVCodec.init() call.

View File

@ -33,6 +33,7 @@
#include "libavutil/pixdesc.h"
#include "avcodec.h"
#include "encode.h"
#include "internal.h"
#include "put_bits.h"
#include "rangecoder.h"
@ -1159,7 +1160,7 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
maxsize = INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE - 32;
}
if ((ret = ff_alloc_packet2(avctx, pkt, maxsize, 0)) < 0)
if ((ret = ff_alloc_packet(avctx, pkt, maxsize)) < 0)
return ret;
ff_init_range_encoder(c, pkt->data, pkt->size);

View File

@ -48,6 +48,7 @@
#include "libavutil/imgutils.h"
#include "avcodec.h"
#include "encode.h"
#include "internal.h"
#include "put_bits.h"
#include "bytestream.h"
@ -844,7 +845,7 @@ static int flashsv2_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
int res;
int keyframe = 0;
if ((res = ff_alloc_packet2(avctx, pkt, s->frame_size + AV_INPUT_BUFFER_MIN_SIZE, 0)) < 0)
if ((res = ff_alloc_packet(avctx, pkt, s->frame_size + AV_INPUT_BUFFER_MIN_SIZE)) < 0)
return res;
/* First frame needs to be a keyframe */

View File

@ -49,6 +49,7 @@
#include <zlib.h>
#include "avcodec.h"
#include "encode.h"
#include "internal.h"
#include "put_bits.h"
#include "bytestream.h"
@ -229,7 +230,7 @@ static int flashsv_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
I_frame = 1;
}
if ((res = ff_alloc_packet2(avctx, pkt, s->image_width * s->image_height * 3, 0)) < 0)
if ((res = ff_alloc_packet(avctx, pkt, s->image_width * s->image_height * 3)) < 0)
return res;
pkt->size = encode_bitstream(s, p, pkt->data, pkt->size, opt_w * 16, opt_h * 16,

View File

@ -35,6 +35,7 @@
#include "libavutil/imgutils.h"
#include "avcodec.h"
#include "bytestream.h"
#include "encode.h"
#include "internal.h"
#include "lzw.h"
#include "gif.h"
@ -480,7 +481,7 @@ static int gif_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
const uint32_t *palette = NULL;
int ret;
if ((ret = ff_alloc_packet2(avctx, pkt, avctx->width*avctx->height*7/5 + AV_INPUT_BUFFER_MIN_SIZE, 0)) < 0)
if ((ret = ff_alloc_packet(avctx, pkt, avctx->width*avctx->height*7/5 + AV_INPUT_BUFFER_MIN_SIZE)) < 0)
return ret;
outbuf_ptr = pkt->data;
end = pkt->data + pkt->size;

View File

@ -39,6 +39,7 @@
#include "avcodec.h"
#include "bytestream.h"
#include "encode.h"
#include "hap.h"
#include "internal.h"
#include "texturedsp.h"
@ -200,7 +201,7 @@ static int hap_encode(AVCodecContext *avctx, AVPacket *pkt,
int pktsize = FFMAX(ctx->tex_size, ctx->max_snappy * ctx->chunk_count) + header_length;
/* Allocate maximum size packet, shrink later. */
ret = ff_alloc_packet2(avctx, pkt, pktsize, header_length);
ret = ff_alloc_packet(avctx, pkt, pktsize);
if (ret < 0)
return ret;

View File

@ -29,6 +29,7 @@
*/
#include "avcodec.h"
#include "encode.h"
#include "huffyuv.h"
#include "huffman.h"
#include "huffyuvencdsp.h"
@ -728,7 +729,7 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
const AVFrame * const p = pict;
int i, j, size = 0, ret;
if ((ret = ff_alloc_packet2(avctx, pkt, width * height * 3 * 4 + AV_INPUT_BUFFER_MIN_SIZE, 0)) < 0)
if ((ret = ff_alloc_packet(avctx, pkt, width * height * 3 * 4 + AV_INPUT_BUFFER_MIN_SIZE)) < 0)
return ret;
if (s->context) {

View File

@ -228,30 +228,6 @@ void ff_color_frame(AVFrame *frame, const int color[4]);
*/
#define FF_MAX_EXTRADATA_SIZE ((1 << 28) - AV_INPUT_BUFFER_PADDING_SIZE)
/**
* Check AVPacket size and allocate data.
*
* Encoders supporting AVCodec.encode2() can use this as a convenience to
* obtain a big enough buffer for the encoded bitstream.
*
* @param avctx the AVCodecContext of the encoder
* @param avpkt The AVPacket: on success, avpkt->data will point to a buffer
* of size at least `size`; the packet will not be refcounted.
* This packet must be initially blank.
* @param size an upper bound of the size of the packet to encode
* @param min_size This is a hint to the allocation algorithm, which indicates
* to what minimal size the caller might later shrink the packet
* to. Encoders often allocate packets which are larger than the
* amount of data that is written into them as the exact amount is
* not known at the time of allocation. min_size represents the
* size a packet might be shrunk to by the caller. Can be set to
* 0. setting this roughly correctly allows the allocation code
* to choose between several allocation strategies to improve
* speed slightly.
* @return non negative on success, negative error code on failure
*/
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size);
/**
* Rescale from sample rate to AVCodecContext.time_base.
*/

View File

@ -66,6 +66,7 @@
#include <float.h>
#include "avcodec.h"
#include "encode.h"
#include "internal.h"
#include "bytestream.h"
#include "jpeg2000.h"
@ -1534,7 +1535,7 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
Jpeg2000EncoderContext *s = avctx->priv_data;
uint8_t *chunkstart, *jp2cstart, *jp2hstart;
if ((ret = ff_alloc_packet2(avctx, pkt, avctx->width*avctx->height*9 + AV_INPUT_BUFFER_MIN_SIZE, 0)) < 0)
if ((ret = ff_alloc_packet(avctx, pkt, avctx->width*avctx->height*9 + AV_INPUT_BUFFER_MIN_SIZE)) < 0)
return ret;
// init:

View File

@ -42,6 +42,7 @@
#include "libavutil/avassert.h"
#include "avcodec.h"
#include "encode.h"
#include "internal.h"
#include "lcl.h"
#include "libavutil/internal.h"
@ -70,7 +71,7 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
int zret; // Zlib return code
int max_size = deflateBound(&c->zstream, avctx->width * avctx->height * 3);
if ((ret = ff_alloc_packet2(avctx, pkt, max_size, 0)) < 0)
if ((ret = ff_alloc_packet(avctx, pkt, max_size)) < 0)
return ret;
if(avctx->pix_fmt != AV_PIX_FMT_BGR24){

View File

@ -24,6 +24,7 @@
#include "libavutil/opt.h"
#include "avcodec.h"
#include "audio_frame_queue.h"
#include "encode.h"
#include "internal.h"
#include "profiles.h"
@ -391,7 +392,8 @@ static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
in_buf.bufElSizes = &in_buffer_element_size;
/* The maximum packet size is 6144 bits aka 768 bytes per channel. */
if ((ret = ff_alloc_packet2(avctx, avpkt, FFMAX(8192, 768 * avctx->channels), 0)) < 0)
ret = ff_alloc_packet(avctx, avpkt, FFMAX(8192, 768 * avctx->channels));
if (ret < 0)
return ret;
out_ptr = avpkt->data;

View File

@ -25,6 +25,7 @@
#include "libavutil/common.h"
#include "libavutil/opt.h"
#include "avcodec.h"
#include "encode.h"
#include "internal.h"
#ifndef LIBILBC_VERSION_MAJOR
@ -183,7 +184,7 @@ static int ilbc_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
ILBCEncContext *s = avctx->priv_data;
int ret;
if ((ret = ff_alloc_packet2(avctx, avpkt, 50, 0)) < 0)
if ((ret = ff_alloc_packet(avctx, avpkt, 50)) < 0)
return ret;
WebRtcIlbcfix_EncodeImpl((uint16_t *) avpkt->data, (const int16_t *) frame->data[0], &s->encoder);

View File

@ -27,6 +27,7 @@
#include "libavutil/opt.h"
#include "avcodec.h"
#include "audio_frame_queue.h"
#include "encode.h"
#include "internal.h"
#if CONFIG_LIBOPENCORE_AMRNB_DECODER || CONFIG_LIBOPENCORE_AMRWB_DECODER
@ -243,7 +244,7 @@ static int amr_nb_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
s->enc_bitrate = avctx->bit_rate;
}
if ((ret = ff_alloc_packet2(avctx, avpkt, 32, 0)) < 0)
if ((ret = ff_alloc_packet(avctx, avpkt, 32)) < 0)
return ret;
if (frame) {

View File

@ -30,6 +30,7 @@
#include "libavutil/intreadwrite.h"
#include "libavutil/opt.h"
#include "avcodec.h"
#include "encode.h"
#include "internal.h"
#include <openjpeg.h>
@ -661,9 +662,8 @@ static int libopenjpeg_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
goto done;
}
if ((ret = ff_alloc_packet2(avctx, pkt, 1024, 0)) < 0) {
if ((ret = ff_alloc_packet(avctx, pkt, 1024)) < 0)
goto done;
}
compress = opj_create_compress(ctx->format);
if (!compress) {

View File

@ -25,6 +25,7 @@
#include "libavutil/opt.h"
#include "avcodec.h"
#include "bytestream.h"
#include "encode.h"
#include "internal.h"
#include "libopus.h"
#include "vorbis.h"
@ -483,7 +484,7 @@ static int libopus_encode(AVCodecContext *avctx, AVPacket *avpkt,
/* Maximum packet size taken from opusenc in opus-tools. 120ms packets
* consist of 6 frames in one packet. The maximum frame size is 1275
* bytes along with the largest possible packet header of 7 bytes. */
if ((ret = ff_alloc_packet2(avctx, avpkt, (1275 * 6 + 7) * opus->stream_count, 0)) < 0)
if ((ret = ff_alloc_packet(avctx, avpkt, (1275 * 6 + 7) * opus->stream_count)) < 0)
return ret;
if (avctx->sample_fmt == AV_SAMPLE_FMT_FLT)

View File

@ -89,6 +89,7 @@
#include "libavutil/common.h"
#include "libavutil/opt.h"
#include "avcodec.h"
#include "encode.h"
#include "internal.h"
#include "audio_frame_queue.h"
@ -294,7 +295,7 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
/* write output if all frames for the packet have been encoded */
if (s->pkt_frame_count == s->frames_per_packet) {
s->pkt_frame_count = 0;
if ((ret = ff_alloc_packet2(avctx, avpkt, speex_bits_nbytes(&s->bits), 0)) < 0)
if ((ret = ff_alloc_packet(avctx, avpkt, speex_bits_nbytes(&s->bits))) < 0)
return ret;
ret = speex_bits_write(&s->bits, avpkt->data, avpkt->size);
speex_bits_reset(&s->bits);

View File

@ -30,6 +30,7 @@
#include "libavutil/opt.h"
#include "avcodec.h"
#include "encode.h"
#include "internal.h"
#include "mpegaudio.h"
@ -110,7 +111,7 @@ static int twolame_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
TWOLAMEContext *s = avctx->priv_data;
int ret;
if ((ret = ff_alloc_packet2(avctx, avpkt, MPA_MAX_CODED_FRAME_SIZE, 0)) < 0)
if ((ret = ff_alloc_packet(avctx, avpkt, MPA_MAX_CODED_FRAME_SIZE)) < 0)
return ret;
if (frame) {

View File

@ -28,6 +28,7 @@
#include "libavutil/mem.h"
#include "libavutil/opt.h"
#include "avcodec.h"
#include "encode.h"
#include "internal.h"
#define MAX_PACKET_SIZE (1 + (477 + 7) / 8)
@ -118,7 +119,7 @@ static int amr_wb_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
const int16_t *samples = (const int16_t *)frame->data[0];
int size, ret;
if ((ret = ff_alloc_packet2(avctx, avpkt, MAX_PACKET_SIZE, 0)) < 0)
if ((ret = ff_alloc_packet(avctx, avpkt, MAX_PACKET_SIZE)) < 0)
return ret;
if (s->last_bitrate != avctx->bit_rate) {

View File

@ -39,6 +39,7 @@
#include "libavutil/opt.h"
#include "avcodec.h"
#include "encode.h"
#include "internal.h"
#include "mpegutils.h"
#include "packet_internal.h"
@ -740,7 +741,7 @@ static int xvid_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
xvid_enc_frame_t xvid_enc_frame = { 0 };
xvid_enc_stats_t xvid_enc_stats = { 0 };
if ((ret = ff_alloc_packet2(avctx, pkt, mb_width*(int64_t)mb_height*MAX_MB_BYTES + AV_INPUT_BUFFER_MIN_SIZE, 0)) < 0)
if ((ret = ff_alloc_packet(avctx, pkt, mb_width*(int64_t)mb_height*MAX_MB_BYTES + AV_INPUT_BUFFER_MIN_SIZE)) < 0)
return ret;
/* Start setting up the frame */

View File

@ -36,6 +36,7 @@
#include "libavutil/pixdesc.h"
#include "avcodec.h"
#include "encode.h"
#include "idctdsp.h"
#include "internal.h"
#include "jpegtables.h"
@ -232,7 +233,7 @@ static int ljpeg_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
* s->hsample[0] * s->vsample[0];
}
if ((ret = ff_alloc_packet2(avctx, pkt, max_pkt_size, 0)) < 0)
if ((ret = ff_alloc_packet(avctx, pkt, max_pkt_size)) < 0)
return ret;
init_put_bits(&pb, pkt->data, pkt->size);

View File

@ -28,6 +28,7 @@
#include "avcodec.h"
#include "bytestream.h"
#include "encode.h"
#include "put_bits.h"
#include "internal.h"
#include "thread.h"
@ -413,8 +414,8 @@ static int magy_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
const int width = avctx->width, height = avctx->height;
int pos, slice, i, j, ret = 0;
ret = ff_alloc_packet2(avctx, pkt, (256 + 4 * s->nb_slices + width * height) *
s->planes + 256, 0);
ret = ff_alloc_packet(avctx, pkt, (256 + 4 * s->nb_slices + width * height) *
s->planes + 256);
if (ret < 0)
return ret;

View File

@ -21,6 +21,7 @@
*/
#include "avcodec.h"
#include "encode.h"
#include "internal.h"
#include "put_bits.h"
#include "audio_frame_queue.h"
@ -2214,7 +2215,7 @@ static int mlp_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
int restart_frame, ret;
uint8_t *data;
if ((ret = ff_alloc_packet2(avctx, avpkt, 87500 * avctx->channels, 0)) < 0)
if ((ret = ff_alloc_packet(avctx, avpkt, 87500 * avctx->channels)) < 0)
return ret;
/* add current frame to queue */

View File

@ -27,6 +27,7 @@
#include "libavutil/channel_layout.h"
#include "avcodec.h"
#include "encode.h"
#include "internal.h"
#include "put_bits.h"
@ -760,7 +761,7 @@ static int MPA_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
}
compute_bit_allocation(s, smr, bit_alloc, &padding);
if ((ret = ff_alloc_packet2(avctx, avpkt, MPA_MAX_CODED_FRAME_SIZE, 0)) < 0)
if ((ret = ff_alloc_packet(avctx, avpkt, MPA_MAX_CODED_FRAME_SIZE)) < 0)
return ret;
init_put_bits(&s->pb, avpkt->data, avpkt->size);

View File

@ -42,6 +42,7 @@
#include "libavutil/thread.h"
#include "avcodec.h"
#include "dct.h"
#include "encode.h"
#include "idctdsp.h"
#include "mpeg12.h"
#include "mpegvideo.h"
@ -1721,7 +1722,7 @@ int ff_mpv_encode_picture(AVCodecContext *avctx, AVPacket *pkt,
int pkt_size = growing_buffer ? FFMAX(s->mb_width*s->mb_height*64+10000, avctx->internal->byte_buffer_size) - AV_INPUT_BUFFER_PADDING_SIZE
:
s->mb_width*s->mb_height*(MAX_MB_BYTES+100)+10000;
if ((ret = ff_alloc_packet2(avctx, pkt, pkt_size, 0)) < 0)
if ((ret = ff_alloc_packet(avctx, pkt, pkt_size)) < 0)
return ret;
if (s->mb_info) {
s->mb_info_ptr = av_packet_new_side_data(pkt,

View File

@ -25,6 +25,7 @@
*/
#include "avcodec.h"
#include "encode.h"
#include "internal.h"
#include "bytestream.h"
#include "libavutil/lfg.h"
@ -76,7 +77,7 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
int skips = 0;
int quality = 24;
if ((ret = ff_alloc_packet2(avctx, pkt, avctx->width*avctx->height*9 + AV_INPUT_BUFFER_MIN_SIZE, 0)) < 0)
if ((ret = ff_alloc_packet(avctx, pkt, avctx->width*avctx->height*9 + AV_INPUT_BUFFER_MIN_SIZE)) < 0)
return ret;
dst= buf= pkt->data;

View File

@ -19,6 +19,7 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "encode.h"
#include "opusenc.h"
#include "opus_pvq.h"
#include "opusenc_psy.h"
@ -577,7 +578,7 @@ static int opus_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
/* Worst case toc + the frame lengths if needed */
alloc_size += 2 + s->packet.frames*2;
if ((ret = ff_alloc_packet2(avctx, avpkt, alloc_size, 0)) < 0)
if ((ret = ff_alloc_packet(avctx, avpkt, alloc_size)) < 0)
return ret;
/* Assemble packet */

View File

@ -29,6 +29,7 @@
#include "avcodec.h"
#include "bytestream.h"
#include "libavutil/imgutils.h"
#include "encode.h"
#include "internal.h"
static const uint32_t monoblack_pal[16] = { 0x000000, 0xFFFFFF };
@ -133,7 +134,7 @@ static int pcx_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
line_bytes = (line_bytes + 1) & ~1;
max_pkt_size = 128 + avctx->height * 2 * line_bytes * nplanes + (pal ? 256*3 + 1 : 0);
if ((ret = ff_alloc_packet2(avctx, pkt, max_pkt_size, 0)) < 0)
if ((ret = ff_alloc_packet(avctx, pkt, max_pkt_size)) < 0)
return ret;
buf = pkt->data;
buf_end = pkt->data + pkt->size;

View File

@ -539,7 +539,7 @@ static int encode_png(AVCodecContext *avctx, AVPacket *pkt,
);
if (max_packet_size > INT_MAX)
return AVERROR(ENOMEM);
ret = ff_alloc_packet2(avctx, pkt, max_packet_size, 0);
ret = ff_alloc_packet(avctx, pkt, max_packet_size);
if (ret < 0)
return ret;

View File

@ -31,6 +31,7 @@
#include "libavutil/opt.h"
#include "avcodec.h"
#include "dct.h"
#include "encode.h"
#include "internal.h"
#include "profiles.h"
#include "proresdata.h"
@ -727,7 +728,7 @@ static int prores_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
int frame_size = FFALIGN(avctx->width, 16) * FFALIGN(avctx->height, 16)*16 + 500 + AV_INPUT_BUFFER_MIN_SIZE; //FIXME choose tighter limit
if ((ret = ff_alloc_packet2(avctx, pkt, frame_size + AV_INPUT_BUFFER_MIN_SIZE, 0)) < 0)
if ((ret = ff_alloc_packet(avctx, pkt, frame_size + AV_INPUT_BUFFER_MIN_SIZE)) < 0)
return ret;
buf = pkt->data;

View File

@ -27,6 +27,7 @@
#include "libavutil/opt.h"
#include "libavutil/pixdesc.h"
#include "avcodec.h"
#include "encode.h"
#include "fdctdsp.h"
#include "put_bits.h"
#include "profiles.h"
@ -998,7 +999,7 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
ctx->pic = pic;
pkt_size = ctx->frame_size_upper_bound;
if ((ret = ff_alloc_packet2(avctx, pkt, pkt_size + AV_INPUT_BUFFER_MIN_SIZE, 0)) < 0)
if ((ret = ff_alloc_packet(avctx, pkt, pkt_size + AV_INPUT_BUFFER_MIN_SIZE)) < 0)
return ret;
orig_buf = pkt->data;

View File

@ -25,6 +25,7 @@
#include "libavutil/imgutils.h"
#include "avcodec.h"
#include "bytestream.h"
#include "encode.h"
#include "internal.h"
/** Maximum RLE code for bulk copy */
@ -369,7 +370,7 @@ static int qtrle_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
QtrleEncContext * const s = avctx->priv_data;
int ret;
if ((ret = ff_alloc_packet2(avctx, pkt, s->max_buf_size, 0)) < 0)
if ((ret = ff_alloc_packet(avctx, pkt, s->max_buf_size)) < 0)
return ret;
if (avctx->gop_size == 0 || !s->previous_frame->data[0] ||

View File

@ -62,6 +62,7 @@
#include "roqvideo.h"
#include "bytestream.h"
#include "elbg.h"
#include "encode.h"
#include "internal.h"
#include "mathops.h"
@ -1071,7 +1072,7 @@ static int roq_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
/* 138 bits max per 8x8 block +
* 256 codebooks*(6 bytes 2x2 + 4 bytes 4x4) + 8 bytes frame header */
size = ((roq->width * roq->height / 64) * 138 + 7) / 8 + 256 * (6 + 4) + 8;
if ((ret = ff_alloc_packet2(avctx, pkt, size, 0)) < 0)
if ((ret = ff_alloc_packet(avctx, pkt, size)) < 0)
return ret;
enc->out_buf = pkt->data;

View File

@ -28,6 +28,7 @@
#include "libavutil/opt.h"
#include "avcodec.h"
#include "encode.h"
#include "internal.h"
#include "put_bits.h"
@ -776,9 +777,9 @@ static int rpza_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
RpzaContext *s = avctx->priv_data;
const AVFrame *pict = frame;
uint8_t *buf;
int ret;
int ret = ff_alloc_packet(avctx, pkt, 6LL * avctx->height * avctx->width);
if ((ret = ff_alloc_packet2(avctx, pkt, 6LL * avctx->height * avctx->width, 0)) < 0)
if (ret < 0)
return ret;
init_put_bits(&s->pb, pkt->data, pkt->size);

View File

@ -23,6 +23,7 @@
#include "avcodec.h"
#include "bytestream.h"
#include "encode.h"
#include "internal.h"
#include "sgi.h"
#include "rle.h"
@ -154,7 +155,7 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
else // assume sgi_rle_encode() produces at most 2x size of input
length += tablesize * 2 + depth * height * (2 * width + 1);
if ((ret = ff_alloc_packet2(avctx, pkt, bytes_per_channel * length, 0)) < 0)
if ((ret = ff_alloc_packet(avctx, pkt, bytes_per_channel * length)) < 0)
return ret;
bytestream2_init_writer(&pbc, pkt->data, pkt->size);

View File

@ -24,6 +24,7 @@
#include "libavutil/opt.h"
#include "libavutil/pixdesc.h"
#include "avcodec.h"
#include "encode.h"
#include "internal.h"
#include "packet_internal.h"
#include "snow_dwt.h"
@ -1569,7 +1570,7 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
uint8_t rc_header_bak[sizeof(s->header_state)];
uint8_t rc_block_bak[sizeof(s->block_state)];
if ((ret = ff_alloc_packet2(avctx, pkt, s->b_width*s->b_height*MB_SIZE*MB_SIZE*3 + AV_INPUT_BUFFER_MIN_SIZE, 0)) < 0)
if ((ret = ff_alloc_packet(avctx, pkt, s->b_width*s->b_height*MB_SIZE*MB_SIZE*3 + AV_INPUT_BUFFER_MIN_SIZE)) < 0)
return ret;
ff_init_range_encoder(c, pkt->data, pkt->size);

View File

@ -19,6 +19,7 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "avcodec.h"
#include "encode.h"
#include "get_bits.h"
#include "golomb.h"
#include "internal.h"
@ -721,7 +722,7 @@ static int sonic_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
const short *samples = (const int16_t*)frame->data[0];
uint8_t state[32];
if ((ret = ff_alloc_packet2(avctx, avpkt, s->frame_size * 5 + 1000, 0)) < 0)
if ((ret = ff_alloc_packet(avctx, avpkt, s->frame_size * 5 + 1000)) < 0)
return ret;
ff_init_range_encoder(&c, avpkt->data, avpkt->size);

View File

@ -23,6 +23,7 @@
#include "avcodec.h"
#include "bytestream.h"
#include "encode.h"
#include "internal.h"
#include "sunrast.h"
@ -175,7 +176,7 @@ static int sunrast_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
SUNRASTContext *s = avctx->priv_data;
int ret;
if ((ret = ff_alloc_packet2(avctx, avpkt, s->size, 0)) < 0)
if ((ret = ff_alloc_packet(avctx, avpkt, s->size)) < 0)
return ret;
bytestream2_init_writer(&s->p, avpkt->data, avpkt->size);

View File

@ -27,6 +27,7 @@
*/
#include "avcodec.h"
#include "encode.h"
#include "hpeldsp.h"
#include "me_cmp.h"
#include "mpegvideo.h"
@ -581,8 +582,9 @@ static int svq1_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
SVQ1EncContext *const s = avctx->priv_data;
int i, ret;
if ((ret = ff_alloc_packet2(avctx, pkt, s->y_block_width * s->y_block_height *
MAX_MB_BYTES*3 + AV_INPUT_BUFFER_MIN_SIZE, 0)) < 0)
ret = ff_alloc_packet(avctx, pkt, s->y_block_width * s->y_block_height *
MAX_MB_BYTES * 3 + AV_INPUT_BUFFER_MIN_SIZE);
if (ret < 0)
return ret;
if (avctx->pix_fmt != AV_PIX_FMT_YUV410P) {

View File

@ -27,6 +27,7 @@
#include "libavutil/opt.h"
#include "libavutil/pixdesc.h"
#include "avcodec.h"
#include "encode.h"
#include "internal.h"
#include "rle.h"
#include "targa.h"
@ -91,7 +92,7 @@ static int targa_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
picsize = av_image_get_buffer_size(avctx->pix_fmt,
avctx->width, avctx->height, 1);
if ((ret = ff_alloc_packet2(avctx, pkt, picsize + 45, 0)) < 0)
if ((ret = ff_alloc_packet(avctx, pkt, picsize + 45)) < 0)
return ret;
/* zero out the header and only set applicable fields */

View File

@ -36,6 +36,7 @@
#include "libavutil/pixdesc.h"
#include "avcodec.h"
#include "bytestream.h"
#include "encode.h"
#include "internal.h"
#include "lzw.h"
#include "put_bits.h"
@ -334,7 +335,7 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
packet_size = avctx->height * bytes_per_row * 2 +
avctx->height * 4 + AV_INPUT_BUFFER_MIN_SIZE;
if ((ret = ff_alloc_packet2(avctx, pkt, packet_size, 0)) < 0)
if ((ret = ff_alloc_packet(avctx, pkt, packet_size)) < 0)
return ret;
ptr = pkt->data;
s->buf_start = pkt->data;

View File

@ -22,6 +22,7 @@
#include "ttadata.h"
#include "ttaencdsp.h"
#include "avcodec.h"
#include "encode.h"
#include "put_bits.h"
#include "internal.h"
#include "libavutil/crc.h"
@ -92,7 +93,7 @@ static int tta_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
pkt_alloc:
cur_chan = 0, res = 0, samples = 0;
if ((ret = ff_alloc_packet2(avctx, avpkt, pkt_size, 0)) < 0)
if ((ret = ff_alloc_packet(avctx, avpkt, pkt_size)) < 0)
return ret;
init_put_bits(&pb, avpkt->data, avpkt->size);

View File

@ -29,6 +29,7 @@
#include "libavutil/opt.h"
#include "avcodec.h"
#include "encode.h"
#include "internal.h"
#include "bswapdsp.h"
#include "bytestream.h"
@ -530,8 +531,8 @@ static int utvideo_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
int i, ret = 0;
/* Allocate a new packet if needed, and set it to the pointer dst */
ret = ff_alloc_packet2(avctx, pkt, (256 + 4 * c->slices + width * height) *
c->planes + 4, 0);
ret = ff_alloc_packet(avctx, pkt, (256 + 4 * c->slices + width * height)
* c->planes + 4);
if (ret < 0)
return ret;

View File

@ -28,6 +28,7 @@
#include "libavutil/float_dsp.h"
#include "avcodec.h"
#include "encode.h"
#include "internal.h"
#include "fft.h"
#include "mathops.h"
@ -1134,7 +1135,7 @@ static int vorbis_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
if (!apply_window_and_mdct(venc))
return 0;
if ((ret = ff_alloc_packet2(avctx, avpkt, 8192, 0)) < 0)
if ((ret = ff_alloc_packet(avctx, avpkt, 8192)) < 0)
return ret;
init_put_bits(&pb, avpkt->data, avpkt->size);

View File

@ -23,6 +23,7 @@
#include "libavutil/intreadwrite.h"
#include "libavutil/opt.h"
#include "avcodec.h"
#include "encode.h"
#include "internal.h"
#include "put_bits.h"
#include "bytestream.h"
@ -2869,7 +2870,7 @@ static int wavpack_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
buf_size = s->block_samples * avctx->channels * 8
+ 200 * avctx->channels /* for headers */;
if ((ret = ff_alloc_packet2(avctx, avpkt, buf_size, 0)) < 0)
if ((ret = ff_alloc_packet(avctx, avpkt, buf_size)) < 0)
return ret;
buf = avpkt->data;

View File

@ -23,6 +23,7 @@
#include "libavutil/ffmath.h"
#include "avcodec.h"
#include "encode.h"
#include "internal.h"
#include "wma.h"
#include "libavutil/avassert.h"
@ -392,7 +393,7 @@ static int encode_superframe(AVCodecContext *avctx, AVPacket *avpkt,
}
}
if ((ret = ff_alloc_packet2(avctx, avpkt, 2 * MAX_CODED_SUPERFRAME_SIZE, 0)) < 0)
if ((ret = ff_alloc_packet(avctx, avpkt, 2 * MAX_CODED_SUPERFRAME_SIZE)) < 0)
return ret;
total_gain = 128;

View File

@ -21,6 +21,7 @@
*/
#include "avcodec.h"
#include "encode.h"
#include "internal.h"
#include "mathops.h"
@ -43,7 +44,7 @@ static int xbm_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
}
size = rowsout * (lineout * 6 + 1) + 106;
if ((ret = ff_alloc_packet2(avctx, pkt, size, 0)) < 0)
if ((ret = ff_alloc_packet(avctx, pkt, size)) < 0)
return ret;
buf = pkt->data;