Skip to content

Commit ba69eb5

Browse files
committed
utvideoenc: avoid writing into the input picture.
Reviewed-by: Derek Buitenhuis <derek.buitenhuis@gmail.com> Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
1 parent f92f493 commit ba69eb5

File tree

2 files changed

+41
-22
lines changed

2 files changed

+41
-22
lines changed

libavcodec/utvideo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ typedef struct UtvideoContext {
7575
int interlaced;
7676
int frame_pred;
7777

78-
uint8_t *slice_bits, *slice_buffer;
78+
uint8_t *slice_bits, *slice_buffer[4];
7979
int slice_bits_size;
8080
} UtvideoContext;
8181

libavcodec/utvideoenc.c

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,20 @@ static int huff_cmp_sym(const void *a, const void *b)
4444
static av_cold int utvideo_encode_close(AVCodecContext *avctx)
4545
{
4646
UtvideoContext *c = avctx->priv_data;
47+
int i;
4748

4849
av_freep(&avctx->coded_frame);
4950
av_freep(&c->slice_bits);
50-
av_freep(&c->slice_buffer);
51+
for (i = 0; i < 4; i++)
52+
av_freep(&c->slice_buffer[i]);
5153

5254
return 0;
5355
}
5456

5557
static av_cold int utvideo_encode_init(AVCodecContext *avctx)
5658
{
5759
UtvideoContext *c = avctx->priv_data;
58-
60+
int i;
5961
uint32_t original_format;
6062

6163
c->avctx = avctx;
@@ -142,13 +144,14 @@ static av_cold int utvideo_encode_init(AVCodecContext *avctx)
142144
return AVERROR(ENOMEM);
143145
}
144146

145-
c->slice_buffer = av_malloc(avctx->width * avctx->height +
146-
FF_INPUT_BUFFER_PADDING_SIZE);
147-
148-
if (!c->slice_buffer) {
149-
av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer 1.\n");
150-
utvideo_encode_close(avctx);
151-
return AVERROR(ENOMEM);
147+
for (i = 0; i < c->planes; i++) {
148+
c->slice_buffer[i] = av_malloc(avctx->width * (avctx->height + 1) +
149+
FF_INPUT_BUFFER_PADDING_SIZE);
150+
if (!c->slice_buffer[i]) {
151+
av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer 1.\n");
152+
utvideo_encode_close(avctx);
153+
return AVERROR(ENOMEM);
154+
}
152155
}
153156

154157
/*
@@ -193,17 +196,33 @@ static av_cold int utvideo_encode_init(AVCodecContext *avctx)
193196
return 0;
194197
}
195198

196-
static void mangle_rgb_planes(uint8_t *src, int step, int stride, int width,
197-
int height)
199+
static void mangle_rgb_planes(uint8_t *dst[4], uint8_t *src, int step,
200+
int stride, int width, int height)
198201
{
199202
int i, j;
203+
int k = width;
200204
unsigned g;
201205

202206
for (j = 0; j < height; j++) {
203-
for (i = 0; i < width * step; i += step) {
204-
g = src[i + 1] + 0x80;
205-
src[i] -= g;
206-
src[i + 2] -= g;
207+
if (step == 3) {
208+
for (i = 0; i < width * step; i += step) {
209+
g = src[i + 1];
210+
dst[0][k] = g;
211+
g += 0x80;
212+
dst[1][k] = src[i + 2] - g;
213+
dst[2][k] = src[i + 0] - g;
214+
k++;
215+
}
216+
} else {
217+
for (i = 0; i < width * step; i += step) {
218+
g = src[i + 1];
219+
dst[0][k] = g;
220+
g += 0x80;
221+
dst[1][k] = src[i + 2] - g;
222+
dst[2][k] = src[i + 0] - g;
223+
dst[3][k] = src[i + 3];
224+
k++;
225+
}
207226
}
208227
src += stride;
209228
}
@@ -528,16 +547,16 @@ static int utvideo_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
528547

529548
/* In case of RGB, mangle the planes to Ut Video's format */
530549
if (avctx->pix_fmt == PIX_FMT_RGBA || avctx->pix_fmt == PIX_FMT_RGB24)
531-
mangle_rgb_planes(pic->data[0], c->planes, pic->linesize[0], width,
532-
height);
550+
mangle_rgb_planes(c->slice_buffer, pic->data[0], c->planes,
551+
pic->linesize[0], width, height);
533552

534553
/* Deal with the planes */
535554
switch (avctx->pix_fmt) {
536555
case PIX_FMT_RGB24:
537556
case PIX_FMT_RGBA:
538557
for (i = 0; i < c->planes; i++) {
539-
ret = encode_plane(avctx, pic->data[0] + ff_ut_rgb_order[i],
540-
c->slice_buffer, c->planes, pic->linesize[0],
558+
ret = encode_plane(avctx, c->slice_buffer[i] + width,
559+
c->slice_buffer[i], 1, width,
541560
width, height, &pb);
542561

543562
if (ret) {
@@ -548,7 +567,7 @@ static int utvideo_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
548567
break;
549568
case PIX_FMT_YUV422P:
550569
for (i = 0; i < c->planes; i++) {
551-
ret = encode_plane(avctx, pic->data[i], c->slice_buffer, 1,
570+
ret = encode_plane(avctx, pic->data[i], c->slice_buffer[0], 1,
552571
pic->linesize[i], width >> !!i, height, &pb);
553572

554573
if (ret) {
@@ -559,7 +578,7 @@ static int utvideo_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
559578
break;
560579
case PIX_FMT_YUV420P:
561580
for (i = 0; i < c->planes; i++) {
562-
ret = encode_plane(avctx, pic->data[i], c->slice_buffer, 1,
581+
ret = encode_plane(avctx, pic->data[i], c->slice_buffer[0], 1,
563582
pic->linesize[i], width >> !!i, height >> !!i,
564583
&pb);
565584

0 commit comments

Comments
 (0)