avcodec/lclenc: Use ff_deflate_init/end() wrappers

They return nicer error messages on error; furthermore,
they also use our allocation functions. It also stops
calling deflateEnd() on a z_stream that might not have been
successfully initialized.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
This commit is contained in:
Andreas Rheinhardt 2022-03-15 18:28:54 +01:00
parent 1692a97251
commit c3c2b5423d
2 changed files with 15 additions and 23 deletions

2
configure vendored
View File

@ -2991,7 +2991,7 @@ xma2_decoder_select="wmapro_decoder"
ylc_decoder_select="bswapdsp"
zerocodec_decoder_select="inflate_wrapper"
zlib_decoder_select="inflate_wrapper"
zlib_encoder_deps="zlib"
zlib_encoder_select="deflate_wrapper"
zmbv_decoder_select="inflate_wrapper"
zmbv_encoder_select="deflate_wrapper"

View File

@ -45,6 +45,7 @@
#include "encode.h"
#include "internal.h"
#include "lcl.h"
#include "zlib_wrapper.h"
#include "libavutil/internal.h"
#include "libavutil/mem.h"
@ -60,16 +61,17 @@ typedef struct LclEncContext {
int compression;
// Flags
int flags;
z_stream zstream;
FFZStream zstream;
} LclEncContext;
static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
const AVFrame *p, int *got_packet)
{
LclEncContext *c = avctx->priv_data;
z_stream *const zstream = &c->zstream.zstream;
int i, ret;
int zret; // Zlib return code
int max_size = deflateBound(&c->zstream, avctx->width * avctx->height * 3);
int max_size = deflateBound(zstream, avctx->width * avctx->height * 3);
if ((ret = ff_alloc_packet(avctx, pkt, max_size)) < 0)
return ret;
@ -79,30 +81,30 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
return -1;
}
zret = deflateReset(&c->zstream);
zret = deflateReset(zstream);
if (zret != Z_OK) {
av_log(avctx, AV_LOG_ERROR, "Deflate reset error: %d\n", zret);
return -1;
}
c->zstream.next_out = pkt->data;
c->zstream.avail_out = pkt->size;
zstream->next_out = pkt->data;
zstream->avail_out = pkt->size;
for(i = avctx->height - 1; i >= 0; i--) {
c->zstream.next_in = p->data[0]+p->linesize[0]*i;
c->zstream.avail_in = avctx->width*3;
zret = deflate(&c->zstream, Z_NO_FLUSH);
zstream->next_in = p->data[0] + p->linesize[0] * i;
zstream->avail_in = avctx->width * 3;
zret = deflate(zstream, Z_NO_FLUSH);
if (zret != Z_OK) {
av_log(avctx, AV_LOG_ERROR, "Deflate error: %d\n", zret);
return -1;
}
}
zret = deflate(&c->zstream, Z_FINISH);
zret = deflate(zstream, Z_FINISH);
if (zret != Z_STREAM_END) {
av_log(avctx, AV_LOG_ERROR, "Deflate error: %d\n", zret);
return -1;
}
pkt->size = c->zstream.total_out;
pkt->size = zstream->total_out;
*got_packet = 1;
return 0;
@ -111,7 +113,6 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
static av_cold int encode_init(AVCodecContext *avctx)
{
LclEncContext *c = avctx->priv_data;
int zret; // Zlib return code
c->avctx= avctx;
@ -138,23 +139,14 @@ static av_cold int encode_init(AVCodecContext *avctx)
avctx->extradata[7]= CODEC_ZLIB;
c->avctx->extradata_size= 8;
c->zstream.zalloc = Z_NULL;
c->zstream.zfree = Z_NULL;
c->zstream.opaque = Z_NULL;
zret = deflateInit(&c->zstream, c->compression);
if (zret != Z_OK) {
av_log(avctx, AV_LOG_ERROR, "Deflate init error: %d\n", zret);
return AVERROR_UNKNOWN;
}
return 0;
return ff_deflate_init(&c->zstream, c->compression, avctx);
}
static av_cold int encode_end(AVCodecContext *avctx)
{
LclEncContext *c = avctx->priv_data;
deflateEnd(&c->zstream);
ff_deflate_end(&c->zstream);
return 0;
}