]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/bitstream.c
d3d11va: make av_d3d11va_alloc_context() available at all times
[ffmpeg] / libavcodec / bitstream.c
index f9f2d3a664b5f1fb1b4a2335d5a153caf8633ecb..93047a2b9109b648161a380ce2a49dd523106b7b 100644 (file)
@@ -29,6 +29,7 @@
  */
 
 #include "avcodec.h"
+#include "internal.h"
 #include "mathops.h"
 #include "get_bits.h"
 #include "put_bits.h"
@@ -106,12 +107,17 @@ static int alloc_table(VLC *vlc, int size, int use_static)
 
     vlc->table_size += size;
     if (vlc->table_size > vlc->table_allocated) {
+        int err;
         if (use_static)
-            abort(); // cannot do anything, init_vlc() is used with too little memory
+            return AVERROR_BUG;
         vlc->table_allocated += (1 << vlc->bits);
-        vlc->table = av_realloc(vlc->table, sizeof(VLC_TYPE) * 2 * vlc->table_allocated);
-        if (!vlc->table)
-            return -1;
+        if ((err = av_reallocp(&vlc->table,
+                               sizeof(VLC_TYPE) * 2 *
+                               vlc->table_allocated)) < 0) {
+            vlc->table_allocated = 0;
+            vlc->table_size = 0;
+            return err;
+        }
     }
     return index;
 }
@@ -124,7 +130,7 @@ static av_always_inline uint32_t bitswap_32(uint32_t x)
            (uint32_t)ff_reverse[ x >> 24];
 }
 
-typedef struct {
+typedef struct VLCcode {
     uint8_t bits;
     uint16_t symbol;
     /** codeword, with the first bit-to-be-read in the msb
@@ -161,9 +167,9 @@ static int build_table(VLC *vlc, int table_nb_bits, int nb_codes,
 
     table_size = 1 << table_nb_bits;
     table_index = alloc_table(vlc, table_size, flags & INIT_VLC_USE_NEW_STATIC);
-    av_dlog(NULL, "new table index=%d size=%d\n", table_index, table_size);
+    ff_dlog(NULL, "new table index=%d size=%d\n", table_index, table_size);
     if (table_index < 0)
-        return -1;
+        return table_index;
     table = &vlc->table[table_index];
 
     for (i = 0; i < table_size; i++) {
@@ -176,7 +182,7 @@ static int build_table(VLC *vlc, int table_nb_bits, int nb_codes,
         n      = codes[i].bits;
         code   = codes[i].code;
         symbol = codes[i].symbol;
-        av_dlog(NULL, "i=%d n=%d code=0x%x\n", i, n, code);
+        ff_dlog(NULL, "i=%d n=%d code=0x%x\n", i, n, code);
         if (n <= table_nb_bits) {
             /* no need to add another table */
             j = code >> (32 - table_nb_bits);
@@ -187,10 +193,10 @@ static int build_table(VLC *vlc, int table_nb_bits, int nb_codes,
                 inc = 1 << n;
             }
             for (k = 0; k < nb; k++) {
-                av_dlog(NULL, "%4x: code=%d n=%d\n", j, i, n);
+                ff_dlog(NULL, "%4x: code=%d n=%d\n", j, i, n);
                 if (table[j][1] /*bits*/ != 0) {
                     av_log(NULL, AV_LOG_ERROR, "incorrect codes\n");
-                    return -1;
+                    return AVERROR_INVALIDDATA;
                 }
                 table[j][1] = n; //bits
                 table[j][0] = symbol;
@@ -217,11 +223,11 @@ static int build_table(VLC *vlc, int table_nb_bits, int nb_codes,
             subtable_bits = FFMIN(subtable_bits, table_nb_bits);
             j = (flags & INIT_VLC_LE) ? bitswap_32(code_prefix) >> (32 - table_nb_bits) : code_prefix;
             table[j][1] = -subtable_bits;
-            av_dlog(NULL, "%4x: n=%d (subtable)\n",
+            ff_dlog(NULL, "%4x: n=%d (subtable)\n",
                     j, codes[i].bits + table_nb_bits);
             index = build_table(vlc, subtable_bits, k-i, codes+i, flags);
             if (index < 0)
-                return -1;
+                return index;
             /* note: realloc has been done, so reload tables */
             table = &vlc->table[table_index];
             table[j][0] = index; //code
@@ -272,7 +278,7 @@ int ff_init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes,
         if (vlc->table_size && vlc->table_size == vlc->table_allocated) {
             return 0;
         } else if (vlc->table_size) {
-            abort(); // fatal error, we are called on a partially initialized table
+            return AVERROR_BUG;
         }
     } else {
         vlc->table           = NULL;
@@ -280,9 +286,11 @@ int ff_init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes,
         vlc->table_size      = 0;
     }
 
-    av_dlog(NULL, "build table nb_codes=%d\n", nb_codes);
+    ff_dlog(NULL, "build table nb_codes=%d\n", nb_codes);
 
     buf = av_malloc((nb_codes + 1) * sizeof(VLCcode));
+    if (!buf)
+        return AVERROR(ENOMEM);
 
     assert(symbols_size <= 2 || !symbols);
     j = 0;
@@ -313,7 +321,7 @@ int ff_init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes,
     av_free(buf);
     if (ret < 0) {
         av_freep(&vlc->table);
-        return -1;
+        return ret;
     }
     if ((flags & INIT_VLC_USE_NEW_STATIC) &&
         vlc->table_size != vlc->table_allocated)