[FFmpeg-cvslog] avcodec/magicyuvenc: add support for encoding raw slice

Paul B Mahol git at videolan.org
Sun Sep 10 18:49:42 EEST 2023


ffmpeg | branch: master | Paul B Mahol <onemda at gmail.com> | Sun Sep 10 17:49:14 2023 +0200| [1a87a9d82a21794c997086ce56701f1d1598ba4e] | committer: Paul B Mahol

avcodec/magicyuvenc: add support for encoding raw slice

Switched to raw slice encoding only if huffman encoding size of slice
is bigger than raw one.

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=1a87a9d82a21794c997086ce56701f1d1598ba4e
---

 libavcodec/magicyuvenc.c | 32 ++++++++++++++++++++++++++------
 1 file changed, 26 insertions(+), 6 deletions(-)

diff --git a/libavcodec/magicyuvenc.c b/libavcodec/magicyuvenc.c
index 432a875a0a..ccd6a54936 100644
--- a/libavcodec/magicyuvenc.c
+++ b/libavcodec/magicyuvenc.c
@@ -217,12 +217,12 @@ static av_cold int magy_encode_init(AVCodecContext *avctx)
         s->decorrelate_buf[1] = s->decorrelate_buf[0] + (s->nb_slices * s->slice_height) * FFALIGN(avctx->width, av_cpu_max_align());
     }
 
-    s->bitslice_size = avctx->width * (s->slice_height + 2) + AV_INPUT_BUFFER_PADDING_SIZE;
+    s->bitslice_size = avctx->width * s->slice_height + 2;
     for (int n = 0; n < s->nb_slices; n++) {
         for (int i = 0; i < s->planes; i++) {
             Slice *sl = &s->slices[n * s->planes + i];
 
-            sl->bitslice = av_malloc(s->bitslice_size);
+            sl->bitslice = av_malloc(s->bitslice_size + AV_INPUT_BUFFER_PADDING_SIZE);
             sl->slice = av_malloc(avctx->width * (s->slice_height + 2) +
                                                      AV_INPUT_BUFFER_PADDING_SIZE);
             if (!sl->slice || !sl->bitslice) {
@@ -416,11 +416,28 @@ static int encode_table(AVCodecContext *avctx,
     return 0;
 }
 
+static int encode_plane_slice_raw(const uint8_t *src, uint8_t *dst, unsigned dst_size,
+                                  int width, int height, int prediction)
+{
+    unsigned count = width * height;
+
+    dst[0] = 1;
+    dst[1] = prediction;
+
+    memcpy(dst + 2, src, count);
+    count += 2;
+    AV_WN32(dst + count, 0);
+    if (count & 3)
+        count += 4 - (count & 3);
+
+    return count;
+}
+
 static int encode_plane_slice(const uint8_t *src, uint8_t *dst, unsigned dst_size,
                               int width, int height, HuffEntry *he, int prediction)
 {
+    const uint8_t *osrc = src;
     PutBitContext pb;
-    int i, j;
     int count;
 
     init_put_bits(&pb, dst, dst_size);
@@ -428,10 +445,13 @@ static int encode_plane_slice(const uint8_t *src, uint8_t *dst, unsigned dst_siz
     put_bits(&pb, 8, 0);
     put_bits(&pb, 8, prediction);
 
-    for (j = 0; j < height; j++) {
-        for (i = 0; i < width; i++) {
+    for (int j = 0; j < height; j++) {
+        for (int i = 0; i < width; i++) {
             const int idx = src[i];
-            put_bits(&pb, he[idx].len, he[idx].code);
+            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);
         }
 
         src += width;



More information about the ffmpeg-cvslog mailing list