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
56typedef struct Slice {56typedef struct Slice {
57 int width;57 int width;
58 int height;58 int height;
59 int encode_raw;
59 unsigned pos;60 unsigned pos;
60 unsigned size;61 unsigned size;
61 uint8_t *slice;62 uint8_t *slice;
@@ -425,7 +426,7 @@ static int encode_table(AVCodecContext *avctx, · 363 unmodified lines
425 return 0;426 return 0;
426}427}
427 428
428static int encode_plane_slice_raw(const uint8_t *src, uint8_t *dst, unsigned dst_size,429static int encode_plane_slice_raw(const uint8_t *src, uint8_t *dst,
429 int width, int height, int prediction)430 int width, int height, int prediction)
430{431{
431 unsigned count = width * height;432 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
442 return count;443 return count;
443}444}
444 445
445static int encode_plane_slice(const uint8_t *src, uint8_t *dst, unsigned dst_size,446static void encode_plane_slice(const uint8_t *src, uint8_t *dst, unsigned dst_size,
446 int width, int height, HuffEntry *he, int prediction)447 int width, int height, HuffEntry *he, int prediction)
447{448{
448 const uint8_t *osrc = src;
449 PutBitContext pb;449 PutBitContext pb;
450 int count;450 int count;
451 451
@@ -458,8 +458,6 @@ static int encode_plane_slice(const uint8_t *src, uint8_t *dst, unsigned dst_siz · 6 unmodified lines
458 for (int i = 0; i < width; i++) {458 for (int i = 0; i < width; i++) {
459 const int idx = src[i];459 const int idx = src[i];
460 const int len = he[idx].len;460 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);
463 put_bits(&pb, len, he[idx].code);461 put_bits(&pb, len, he[idx].code);
464 }462 }
465 463
@@ -472,8 +470,7 @@ static int encode_plane_slice(const uint8_t *src, uint8_t *dst, unsigned dst_siz · 6 unmodified lines
472 put_bits(&pb, 32 - count, 0);470 put_bits(&pb, 32 - count, 0);
473 471
474 flush_put_bits(&pb);472 flush_put_bits(&pb);
475 473 av_assert1(put_bytes_left(&pb, 0) == 0);
476 return put_bytes_output(&pb);
477}474}
478 475
479static int encode_slice(AVCodecContext *avctx, void *tdata,476static int encode_slice(AVCodecContext *avctx, void *tdata,
@@ -484,10 +481,13 @@ static int encode_slice(AVCodecContext *avctx, void *tdata, · 4 unmodified lines
484 for (int i = 0; i < s->planes; i++) {481 for (int i = 0; i < s->planes; i++) {
485 Slice *sl = &s->slices[n * s->planes + i];482 Slice *sl = &s->slices[n * s->planes + i];
486 483
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
488 encode_plane_slice(sl->slice,488 encode_plane_slice(sl->slice,
489 sl->bitslice,489 sl->bitslice,
490 s->bitslice_size,490 sl->size,
491 sl->width, sl->height,491 sl->width, sl->height,
492 s->he[i], s->frame_pred);492 s->he[i], s->frame_pred);
493 }493 }
@@ -612,6 +612,20 @@ static int magy_encode_frame(AVCodecContext *avctx, AVPacket *pkt, · 118 unmodified lines
612 tables_size = put_bytes_count(&pbit, 1);612 tables_size = put_bytes_count(&pbit, 1);
613 bytestream2_skip_p(&pb, tables_size);613 bytestream2_skip_p(&pb, tables_size);
614 614
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 }
615 avctx->execute2(avctx, encode_slice, NULL, NULL, s->nb_slices);629 avctx->execute2(avctx, encode_slice, NULL, NULL, s->nb_slices);
616 630
617 for (int n = 0; n < s->nb_slices; n++) {631 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