avcodec/jpeglsenc: Allocate buffers together

Having only one allocation that is not automatically freed in particular
means that one does not need to free the already allocated buffers
when allocating another one fails.

Reviewed-by: Paul B Mahol <onemda@gmail.com>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
This commit is contained in:
Andreas Rheinhardt 2020-09-03 19:50:03 +02:00
parent 017baa8106
commit e3c4e1f0c4
1 changed files with 7 additions and 18 deletions

View File

@ -276,7 +276,6 @@ static int encode_picture_ls(AVCodecContext *avctx, AVPacket *pkt,
PutByteContext pb; PutByteContext pb;
PutBitContext pb2; PutBitContext pb2;
GetBitContext gb; GetBitContext gb;
uint8_t *buf2 = NULL;
const uint8_t *in; const uint8_t *in;
uint8_t *last = NULL; uint8_t *last = NULL;
JLSState state = { 0 }; JLSState state = { 0 };
@ -300,12 +299,13 @@ FF_ENABLE_DEPRECATION_WARNINGS
AV_INPUT_BUFFER_MIN_SIZE, 0)) < 0) AV_INPUT_BUFFER_MIN_SIZE, 0)) < 0)
return ret; return ret;
buf2 = av_malloc(pkt->size); last = av_malloc((unsigned)pkt->size + FFABS(p->linesize[0]));
if (!buf2) if (!last)
goto memfail; return AVERROR(ENOMEM);
memset(last, 0, FFABS(p->linesize[0]));
bytestream2_init_writer(&pb, pkt->data, pkt->size); bytestream2_init_writer(&pb, pkt->data, pkt->size);
init_put_bits(&pb2, buf2, pkt->size); init_put_bits(&pb2, last + FFABS(p->linesize[0]), pkt->size);
/* write our own JPEG header, can't use mjpeg_picture_header */ /* write our own JPEG header, can't use mjpeg_picture_header */
put_marker_byteu(&pb, SOI); put_marker_byteu(&pb, SOI);
@ -340,10 +340,6 @@ FF_ENABLE_DEPRECATION_WARNINGS
ls_store_lse(&state, &pb); ls_store_lse(&state, &pb);
last = av_mallocz(FFABS(p->linesize[0]));
if (!last)
goto memfail;
in = p->data[0]; in = p->data[0];
if (avctx->pix_fmt == AV_PIX_FMT_GRAY8) { if (avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
int t = 0; int t = 0;
@ -393,8 +389,6 @@ FF_ENABLE_DEPRECATION_WARNINGS
} }
} }
av_freep(&last);
/* the specification says that after doing 0xff escaping unused bits in /* the specification says that after doing 0xff escaping unused bits in
* the last byte must be set to 0, so just append 7 "optional" zero bits * the last byte must be set to 0, so just append 7 "optional" zero bits
* to avoid special-casing. */ * to avoid special-casing. */
@ -402,7 +396,7 @@ FF_ENABLE_DEPRECATION_WARNINGS
size = put_bits_count(&pb2); size = put_bits_count(&pb2);
flush_put_bits(&pb2); flush_put_bits(&pb2);
/* do escape coding */ /* do escape coding */
init_get_bits(&gb, buf2, size); init_get_bits(&gb, pb2.buf, size);
size -= 7; size -= 7;
while (get_bits_count(&gb) < size) { while (get_bits_count(&gb) < size) {
int v; int v;
@ -413,7 +407,7 @@ FF_ENABLE_DEPRECATION_WARNINGS
bytestream2_put_byte(&pb, v); bytestream2_put_byte(&pb, v);
} }
} }
av_freep(&buf2); av_freep(&last);
/* End of image */ /* End of image */
put_marker_byte(&pb, EOI); put_marker_byte(&pb, EOI);
@ -424,11 +418,6 @@ FF_ENABLE_DEPRECATION_WARNINGS
pkt->flags |= AV_PKT_FLAG_KEY; pkt->flags |= AV_PKT_FLAG_KEY;
*got_packet = 1; *got_packet = 1;
return 0; return 0;
memfail:
av_freep(&buf2);
av_freep(&last);
return AVERROR(ENOMEM);
} }
static av_cold int encode_init_ls(AVCodecContext *ctx) static av_cold int encode_init_ls(AVCodecContext *ctx)