]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/magicyuv.c
avcodec: Constify AVCodecs
[ffmpeg] / libavcodec / magicyuv.c
index 3413d8f298e9958c206f1413832e6289745549c2..594196063bd9eab3000360fa99897660b7f81689 100644 (file)
@@ -25,7 +25,6 @@
 #define CACHED_BITSTREAM_READER !ARCH_X86_32
 
 #include "libavutil/pixdesc.h"
-#include "libavutil/qsort.h"
 
 #include "avcodec.h"
 #include "bytestream.h"
@@ -47,9 +46,8 @@ typedef enum Prediction {
 } Prediction;
 
 typedef struct HuffEntry {
-    uint16_t sym;
     uint8_t  len;
-    uint32_t code;
+    uint16_t sym;
 } HuffEntry;
 
 typedef struct MagicYUVContext {
@@ -74,30 +72,22 @@ typedef struct MagicYUVContext {
     LLVidDSPContext   llviddsp;
 } MagicYUVContext;
 
-static int huff_cmp_len(const void *a, const void *b)
-{
-    const HuffEntry *aa = a, *bb = b;
-    return (aa->len - bb->len) * 4096 + bb->sym - aa->sym;
-}
-
-static int huff_build(HuffEntry he[], VLC *vlc, int nb_elems)
+static int huff_build(const uint8_t len[], uint16_t codes_pos[33],
+                      VLC *vlc, int nb_elems, void *logctx)
 {
-    uint32_t code;
-    int i;
+    HuffEntry he[4096];
 
-    AV_QSORT(he, nb_elems, HuffEntry, huff_cmp_len);
+    for (int i = 31; i > 0; i--)
+        codes_pos[i] += codes_pos[i + 1];
 
-    code = 1;
-    for (i = nb_elems - 1; i >= 0; i--) {
-        he[i].code = code >> (32 - he[i].len);
-        code += 0x80000000u >> (he[i].len - 1);
-    }
+    for (unsigned i = nb_elems; i-- > 0;)
+        he[--codes_pos[len[i]]] = (HuffEntry){ len[i], i };
 
     ff_free_vlc(vlc);
-    return ff_init_vlc_sparse(vlc, FFMIN(he[nb_elems - 1].len, 12), nb_elems,
-                              &he[0].len,  sizeof(he[0]), sizeof(he[0].len),
-                              &he[0].code, sizeof(he[0]), sizeof(he[0].code),
-                              &he[0].sym,  sizeof(he[0]), sizeof(he[0].sym),  0);
+    return ff_init_vlc_from_lengths(vlc, FFMIN(he[0].len, 12), nb_elems,
+                                    &he[0].len, sizeof(he[0]),
+                                    &he[0].sym, sizeof(he[0]), sizeof(he[0].sym),
+                                    0, 0, logctx);
 }
 
 static void magicyuv_median_pred16(uint16_t *dst, const uint16_t *src1,
@@ -389,7 +379,8 @@ static int build_huffman(AVCodecContext *avctx, const uint8_t *table,
 {
     MagicYUVContext *s = avctx->priv_data;
     GetByteContext gb;
-    HuffEntry he[4096];
+    uint8_t len[4096];
+    uint16_t length_count[33] = { 0 };
     int i = 0, j = 0, k;
 
     bytestream2_init(&gb, table, table_size);
@@ -410,14 +401,13 @@ static int build_huffman(AVCodecContext *avctx, const uint8_t *table,
             return AVERROR_INVALIDDATA;
         }
 
-        for (; j < k; j++) {
-            he[j].sym = j;
-            he[j].len = x;
-        }
+        length_count[x] += l;
+        for (; j < k; j++)
+            len[j] = x;
 
         if (j == max) {
             j = 0;
-            if (huff_build(he, &s->vlc[i], max)) {
+            if (huff_build(len, length_count, &s->vlc[i], max, avctx)) {
                 av_log(avctx, AV_LOG_ERROR, "Cannot build Huffman codes\n");
                 return AVERROR_INVALIDDATA;
             }
@@ -425,6 +415,7 @@ static int build_huffman(AVCodecContext *avctx, const uint8_t *table,
             if (i == s->planes) {
                 break;
             }
+            memset(length_count, 0, sizeof(length_count));
         }
     }
 
@@ -442,23 +433,26 @@ static int magy_decode_frame(AVCodecContext *avctx, void *data,
     MagicYUVContext *s = avctx->priv_data;
     ThreadFrame frame = { .f = data };
     AVFrame *p = data;
-    GetByteContext gbyte;
+    GetByteContext gb;
     uint32_t first_offset, offset, next_offset, header_size, slice_width;
     int width, height, format, version, table_size;
     int ret, i, j;
 
-    bytestream2_init(&gbyte, avpkt->data, avpkt->size);
-    if (bytestream2_get_le32(&gbyte) != MKTAG('M', 'A', 'G', 'Y'))
+    if (avpkt->size < 36)
+        return AVERROR_INVALIDDATA;
+
+    bytestream2_init(&gb, avpkt->data, avpkt->size);
+    if (bytestream2_get_le32u(&gb) != MKTAG('M', 'A', 'G', 'Y'))
         return AVERROR_INVALIDDATA;
 
-    header_size = bytestream2_get_le32(&gbyte);
+    header_size = bytestream2_get_le32u(&gb);
     if (header_size < 32 || header_size >= avpkt->size) {
         av_log(avctx, AV_LOG_ERROR,
                "header or packet too small %"PRIu32"\n", header_size);
         return AVERROR_INVALIDDATA;
     }
 
-    version = bytestream2_get_byte(&gbyte);
+    version = bytestream2_get_byteu(&gb);
     if (version != 7) {
         avpriv_request_sample(avctx, "Version %d", version);
         return AVERROR_PATCHWELCOME;
@@ -471,7 +465,7 @@ static int magy_decode_frame(AVCodecContext *avctx, void *data,
     s->decorrelate = 0;
     s->bps = 8;
 
-    format = bytestream2_get_byte(&gbyte);
+    format = bytestream2_get_byteu(&gb);
     switch (format) {
     case 0x65:
         avctx->pix_fmt = AV_PIX_FMT_GBRP;
@@ -552,34 +546,34 @@ static int magy_decode_frame(AVCodecContext *avctx, void *data,
     s->magy_decode_slice = s->bps == 8 ? magy_decode_slice : magy_decode_slice10;
     s->planes = av_pix_fmt_count_planes(avctx->pix_fmt);
 
-    bytestream2_skip(&gbyte, 1);
-    s->color_matrix = bytestream2_get_byte(&gbyte);
-    s->flags        = bytestream2_get_byte(&gbyte);
+    bytestream2_skipu(&gb, 1);
+    s->color_matrix = bytestream2_get_byteu(&gb);
+    s->flags        = bytestream2_get_byteu(&gb);
     s->interlaced   = !!(s->flags & 2);
-    bytestream2_skip(&gbyte, 3);
+    bytestream2_skipu(&gb, 3);
 
-    width  = bytestream2_get_le32(&gbyte);
-    height = bytestream2_get_le32(&gbyte);
+    width  = bytestream2_get_le32u(&gb);
+    height = bytestream2_get_le32u(&gb);
     ret = ff_set_dimensions(avctx, width, height);
     if (ret < 0)
         return ret;
 
-    slice_width = bytestream2_get_le32(&gbyte);
+    slice_width = bytestream2_get_le32u(&gb);
     if (slice_width != avctx->coded_width) {
         avpriv_request_sample(avctx, "Slice width %"PRIu32, slice_width);
         return AVERROR_PATCHWELCOME;
     }
-    s->slice_height = bytestream2_get_le32(&gbyte);
+    s->slice_height = bytestream2_get_le32u(&gb);
     if (s->slice_height <= 0 || s->slice_height > INT_MAX - avctx->coded_height) {
         av_log(avctx, AV_LOG_ERROR,
                "invalid slice height: %d\n", s->slice_height);
         return AVERROR_INVALIDDATA;
     }
 
-    bytestream2_skip(&gbyte, 4);
+    bytestream2_skipu(&gb, 4);
 
     s->nb_slices = (avctx->coded_height + s->slice_height - 1) / s->slice_height;
-    if (s->nb_slices > INT_MAX / sizeof(Slice)) {
+    if (s->nb_slices > INT_MAX / FFMAX(sizeof(Slice), 4 * 5)) {
         av_log(avctx, AV_LOG_ERROR,
                "invalid number of slices: %d\n", s->nb_slices);
         return AVERROR_INVALIDDATA;
@@ -596,12 +590,14 @@ static int magy_decode_frame(AVCodecContext *avctx, void *data,
         }
     }
 
+    if (bytestream2_get_bytes_left(&gb) <= s->nb_slices * s->planes * 5)
+        return AVERROR_INVALIDDATA;
     for (i = 0; i < s->planes; i++) {
         av_fast_malloc(&s->slices[i], &s->slices_size[i], s->nb_slices * sizeof(Slice));
         if (!s->slices[i])
             return AVERROR(ENOMEM);
 
-        offset = bytestream2_get_le32(&gbyte);
+        offset = bytestream2_get_le32u(&gb);
         if (offset >= avpkt->size - header_size)
             return AVERROR_INVALIDDATA;
 
@@ -611,7 +607,7 @@ static int magy_decode_frame(AVCodecContext *avctx, void *data,
         for (j = 0; j < s->nb_slices - 1; j++) {
             s->slices[i][j].start = offset + header_size;
 
-            next_offset = bytestream2_get_le32(&gbyte);
+            next_offset = bytestream2_get_le32u(&gb);
             if (next_offset <= offset || next_offset >= avpkt->size - header_size)
                 return AVERROR_INVALIDDATA;
 
@@ -623,18 +619,21 @@ static int magy_decode_frame(AVCodecContext *avctx, void *data,
 
         s->slices[i][j].start = offset + header_size;
         s->slices[i][j].size  = avpkt->size - s->slices[i][j].start;
+
+        if (s->slices[i][j].size < 2)
+            return AVERROR_INVALIDDATA;
     }
 
-    if (bytestream2_get_byte(&gbyte) != s->planes)
+    if (bytestream2_get_byteu(&gb) != s->planes)
         return AVERROR_INVALIDDATA;
 
-    bytestream2_skip(&gbyte, s->nb_slices * s->planes);
+    bytestream2_skipu(&gb, s->nb_slices * s->planes);
 
-    table_size = header_size + first_offset - bytestream2_tell(&gbyte);
+    table_size = header_size + first_offset - bytestream2_tell(&gb);
     if (table_size < 2)
         return AVERROR_INVALIDDATA;
 
-    ret = build_huffman(avctx, avpkt->data + bytestream2_tell(&gbyte),
+    ret = build_huffman(avctx, avpkt->data + bytestream2_tell(&gb),
                         table_size, s->max);
     if (ret < 0)
         return ret;
@@ -695,7 +694,7 @@ static av_cold int magy_decode_end(AVCodecContext *avctx)
     return 0;
 }
 
-AVCodec ff_magicyuv_decoder = {
+const AVCodec ff_magicyuv_decoder = {
     .name             = "magicyuv",
     .long_name        = NULL_IF_CONFIG_SMALL("MagicYUV video"),
     .type             = AVMEDIA_TYPE_VIDEO,