1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2025-08-04 22:03:09 +02:00

avcodec/magicyuvenc: Check in advance whether to encode a slice raw

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
This commit is contained in:
Andreas Rheinhardt
2025-04-17 06:52:13 +02:00
parent 05bda9d3df
commit 6e6e4e0e7b

View File

@ -56,6 +56,7 @@ typedef struct PTable {
typedef struct Slice { typedef struct Slice {
int width; int width;
int height; int height;
int encode_raw;
unsigned pos; unsigned pos;
unsigned size; unsigned size;
uint8_t *slice; uint8_t *slice;
@ -425,7 +426,7 @@ static int encode_table(AVCodecContext *avctx,
return 0; return 0;
} }
static int encode_plane_slice_raw(const uint8_t *src, uint8_t *dst, unsigned dst_size, static int encode_plane_slice_raw(const uint8_t *src, uint8_t *dst,
int width, int height, int prediction) int width, int height, int prediction)
{ {
unsigned count = width * height; unsigned count = width * height;
@ -442,10 +443,9 @@ static int encode_plane_slice_raw(const uint8_t *src, uint8_t *dst, unsigned dst
return count; return count;
} }
static int encode_plane_slice(const uint8_t *src, uint8_t *dst, unsigned dst_size, static void encode_plane_slice(const uint8_t *src, uint8_t *dst, unsigned dst_size,
int width, int height, HuffEntry *he, int prediction) int width, int height, HuffEntry *he, int prediction)
{ {
const uint8_t *osrc = src;
PutBitContext pb; PutBitContext pb;
int count; int count;
@ -458,8 +458,6 @@ static int encode_plane_slice(const uint8_t *src, uint8_t *dst, unsigned dst_siz
for (int i = 0; i < width; i++) { for (int i = 0; i < width; i++) {
const int idx = src[i]; const int idx = src[i];
const int len = he[idx].len; const int len = he[idx].len;
if (put_bits_left(&pb) < len + 32)
return encode_plane_slice_raw(osrc, dst, dst_size, width, height, prediction);
put_bits(&pb, len, he[idx].code); put_bits(&pb, len, he[idx].code);
} }
@ -472,8 +470,7 @@ static int encode_plane_slice(const uint8_t *src, uint8_t *dst, unsigned dst_siz
put_bits(&pb, 32 - count, 0); put_bits(&pb, 32 - count, 0);
flush_put_bits(&pb); flush_put_bits(&pb);
av_assert1(put_bytes_left(&pb, 0) == 0);
return put_bytes_output(&pb);
} }
static int encode_slice(AVCodecContext *avctx, void *tdata, static int encode_slice(AVCodecContext *avctx, void *tdata,
@ -484,10 +481,13 @@ static int encode_slice(AVCodecContext *avctx, void *tdata,
for (int i = 0; i < s->planes; i++) { for (int i = 0; i < s->planes; i++) {
Slice *sl = &s->slices[n * s->planes + i]; Slice *sl = &s->slices[n * s->planes + i];
sl->size = if (sl->encode_raw)
encode_plane_slice_raw(sl->slice, sl->bitslice,
sl->width, sl->height, s->frame_pred);
else
encode_plane_slice(sl->slice, encode_plane_slice(sl->slice,
sl->bitslice, sl->bitslice,
s->bitslice_size, sl->size,
sl->width, sl->height, sl->width, sl->height,
s->he[i], s->frame_pred); s->he[i], s->frame_pred);
} }
@ -612,6 +612,20 @@ static int magy_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
tables_size = put_bytes_count(&pbit, 1); tables_size = put_bytes_count(&pbit, 1);
bytestream2_skip_p(&pb, tables_size); bytestream2_skip_p(&pb, tables_size);
for (int i = 0; i < s->planes; ++i) {
for (int j = 0; j < s->nb_slices; ++j) {
Slice *const sl = &s->slices[j * s->planes + i];
int64_t size = 0;
for (size_t k = 0; k < FF_ARRAY_ELEMS(sl->counts); ++k)
size += sl->counts[k] * s->he[i][k].len;
size = AV_CEIL_RSHIFT(size, 3);
sl->encode_raw = size >= sl->width * sl->height;
if (sl->encode_raw)
size = sl->width * sl->height;
sl->size = FFALIGN(size + 2, 4);
}
}
avctx->execute2(avctx, encode_slice, NULL, NULL, s->nb_slices); avctx->execute2(avctx, encode_slice, NULL, NULL, s->nb_slices);
for (int n = 0; n < s->nb_slices; n++) { for (int n = 0; n < s->nb_slices; n++) {