History

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

Andreas Rheinhardt committed 1 year ago

Commit 6e6e4e01 parent 05bda9d1 file · +24 −10Browse files

Showing the changes to libavcodec (1 of 1 file). Show all

Unified · Split

libavcodec/magicyuvenc.c+24−10
@@ -56,6 +56,7 @@ typedef struct PTable { · 55 unmodified lines
5656 typedef struct Slice {
5757 int width;
5858 int height;
59+ int encode_raw;
5960 unsigned pos;
6061 unsigned size;
6162 uint8_t *slice;
@@ -425,7 +426,7 @@ static int encode_table(AVCodecContext *avctx, · 363 unmodified lines
425426 return 0;
426427 }
427428
428−static int encode_plane_slice_raw(const uint8_t *src, uint8_t *dst, unsigned dst_size,
429+static int encode_plane_slice_raw(const uint8_t *src, uint8_t *dst,
429430 int width, int height, int prediction)
430431 {
431432 unsigned count = width * height;
@@ -442,10 +443,9 @@ static int encode_plane_slice_raw(const uint8_t *src, uint8_t *dst, unsigned dst · 10 unmodified lines
442443 return count;
443444 }
444445
445−static int encode_plane_slice(const uint8_t *src, uint8_t *dst, unsigned dst_size,
446− int width, int height, HuffEntry *he, int prediction)
446+static void encode_plane_slice(const uint8_t *src, uint8_t *dst, unsigned dst_size,
447+ int width, int height, HuffEntry *he, int prediction)
447448 {
448− const uint8_t *osrc = src;
449449 PutBitContext pb;
450450 int count;
451451
@@ -458,8 +458,6 @@ static int encode_plane_slice(const uint8_t *src, uint8_t *dst, unsigned dst_siz · 6 unmodified lines
458458 for (int i = 0; i < width; i++) {
459459 const int idx = src[i];
460460 const int len = he[idx].len;
461− if (put_bits_left(&pb) < len + 32)
462− return encode_plane_slice_raw(osrc, dst, dst_size, width, height, prediction);
463461 put_bits(&pb, len, he[idx].code);
464462 }
465463
@@ -472,8 +470,7 @@ static int encode_plane_slice(const uint8_t *src, uint8_t *dst, unsigned dst_siz · 6 unmodified lines
472470 put_bits(&pb, 32 - count, 0);
473471
474472 flush_put_bits(&pb);
475−
476− return put_bytes_output(&pb);
473+ av_assert1(put_bytes_left(&pb, 0) == 0);
477474 }
478475
479476 static int encode_slice(AVCodecContext *avctx, void *tdata,
@@ -484,10 +481,13 @@ static int encode_slice(AVCodecContext *avctx, void *tdata, · 4 unmodified lines
484481 for (int i = 0; i < s->planes; i++) {
485482 Slice *sl = &s->slices[n * s->planes + i];
486483
487− sl->size =
484+ if (sl->encode_raw)
485+ encode_plane_slice_raw(sl->slice, sl->bitslice,
486+ sl->width, sl->height, s->frame_pred);
487+ else
488488 encode_plane_slice(sl->slice,
489489 sl->bitslice,
490− s->bitslice_size,
490+ sl->size,
491491 sl->width, sl->height,
492492 s->he[i], s->frame_pred);
493493 }
@@ -612,6 +612,20 @@ static int magy_encode_frame(AVCodecContext *avctx, AVPacket *pkt, · 118 unmodified lines
612612 tables_size = put_bytes_count(&pbit, 1);
613613 bytestream2_skip_p(&pb, tables_size);
614614
615+ for (int i = 0; i < s->planes; ++i) {
616+ for (int j = 0; j < s->nb_slices; ++j) {
617+ Slice *const sl = &s->slices[j * s->planes + i];
618+ int64_t size = 0;
619+
620+ for (size_t k = 0; k < FF_ARRAY_ELEMS(sl->counts); ++k)
621+ size += sl->counts[k] * s->he[i][k].len;
622+ size = AV_CEIL_RSHIFT(size, 3);
623+ sl->encode_raw = size >= sl->width * sl->height;
624+ if (sl->encode_raw)
625+ size = sl->width * sl->height;
626+ sl->size = FFALIGN(size + 2, 4);
627+ }
628+ }
615629 avctx->execute2(avctx, encode_slice, NULL, NULL, s->nb_slices);
616630
617631 for (int n = 0; n < s->nb_slices; n++) {

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t