From 6bad007ad31c8466bcddd093c1d46cb69f75681f Mon Sep 17 00:00:00 2001
From: Carl Eugen Hoyos <ceffmpeg@gmail.com>
Date: Mon, 21 Aug 2017 17:10:35 +0200
Subject: [PATCH] lavc/put_bits: Remove usage of BITSTREAM_WRITER_LE.

Specific functions for little endian put_bits exist for the
little-endian G.726 encoder.
---
 libavcodec/g723_1enc.c  |   47 ++++++-------
 libavcodec/gif.c        |    5 +-
 libavcodec/put_bits.h   |   80 ++++++++-------------
 libavcodec/ttaenc.c     |   15 ++--
 libavcodec/vorbisenc.c  |  179 +++++++++++++++++++++++------------------------
 libavcodec/wavpackenc.c |   56 +++++++--------
 6 files changed, 178 insertions(+), 204 deletions(-)

diff --git a/libavcodec/g723_1enc.c b/libavcodec/g723_1enc.c
index 4a4525e..0d2c1fb 100644
--- a/libavcodec/g723_1enc.c
+++ b/libavcodec/g723_1enc.c
@@ -37,7 +37,6 @@
 #include "g723_1.h"
 #include "internal.h"
 
-#define BITSTREAM_WRITER_LE
 #include "put_bits.h"
 
 static av_cold int g723_1_encode_init(AVCodecContext *avctx)
@@ -1003,16 +1002,16 @@ static int pack_bitstream(G723_1_Context *p, AVPacket *avpkt)
 
     init_put_bits(&pb, avpkt->data, avpkt->size);
 
-    put_bits(&pb, 2, info_bits);
+    put_bits_le(&pb, 2, info_bits);
 
-    put_bits(&pb, 8, p->lsp_index[2]);
-    put_bits(&pb, 8, p->lsp_index[1]);
-    put_bits(&pb, 8, p->lsp_index[0]);
+    put_bits_le(&pb, 8, p->lsp_index[2]);
+    put_bits_le(&pb, 8, p->lsp_index[1]);
+    put_bits_le(&pb, 8, p->lsp_index[0]);
 
-    put_bits(&pb, 7, p->pitch_lag[0] - PITCH_MIN);
-    put_bits(&pb, 2, p->subframe[1].ad_cb_lag);
-    put_bits(&pb, 7, p->pitch_lag[1] - PITCH_MIN);
-    put_bits(&pb, 2, p->subframe[3].ad_cb_lag);
+    put_bits_le(&pb, 7, p->pitch_lag[0] - PITCH_MIN);
+    put_bits_le(&pb, 2, p->subframe[1].ad_cb_lag);
+    put_bits_le(&pb, 7, p->pitch_lag[1] - PITCH_MIN);
+    put_bits_le(&pb, 2, p->subframe[3].ad_cb_lag);
 
     /* Write 12 bit combined gain */
     for (i = 0; i < SUBFRAMES; i++) {
@@ -1020,13 +1019,13 @@ static int pack_bitstream(G723_1_Context *p, AVPacket *avpkt)
                p->subframe[i].amp_index;
         if (p->cur_rate == RATE_6300)
             temp += p->subframe[i].dirac_train << 11;
-        put_bits(&pb, 12, temp);
+        put_bits_le(&pb, 12, temp);
     }
 
-    put_bits(&pb, 1, p->subframe[0].grid_index);
-    put_bits(&pb, 1, p->subframe[1].grid_index);
-    put_bits(&pb, 1, p->subframe[2].grid_index);
-    put_bits(&pb, 1, p->subframe[3].grid_index);
+    put_bits_le(&pb, 1, p->subframe[0].grid_index);
+    put_bits_le(&pb, 1, p->subframe[1].grid_index);
+    put_bits_le(&pb, 1, p->subframe[2].grid_index);
+    put_bits_le(&pb, 1, p->subframe[3].grid_index);
 
     if (p->cur_rate == RATE_6300) {
         skip_put_bits(&pb, 1); /* reserved bit */
@@ -1036,20 +1035,20 @@ static int pack_bitstream(G723_1_Context *p, AVPacket *avpkt)
                (p->subframe[1].pulse_pos >> 14) *  90 +
                (p->subframe[2].pulse_pos >> 16) *   9 +
                (p->subframe[3].pulse_pos >> 14);
-        put_bits(&pb, 13, temp);
+        put_bits_le(&pb, 13, temp);
 
-        put_bits(&pb, 16, p->subframe[0].pulse_pos & 0xffff);
-        put_bits(&pb, 14, p->subframe[1].pulse_pos & 0x3fff);
-        put_bits(&pb, 16, p->subframe[2].pulse_pos & 0xffff);
-        put_bits(&pb, 14, p->subframe[3].pulse_pos & 0x3fff);
+        put_bits_le(&pb, 16, p->subframe[0].pulse_pos & 0xffff);
+        put_bits_le(&pb, 14, p->subframe[1].pulse_pos & 0x3fff);
+        put_bits_le(&pb, 16, p->subframe[2].pulse_pos & 0xffff);
+        put_bits_le(&pb, 14, p->subframe[3].pulse_pos & 0x3fff);
 
-        put_bits(&pb, 6, p->subframe[0].pulse_sign);
-        put_bits(&pb, 5, p->subframe[1].pulse_sign);
-        put_bits(&pb, 6, p->subframe[2].pulse_sign);
-        put_bits(&pb, 5, p->subframe[3].pulse_sign);
+        put_bits_le(&pb, 6, p->subframe[0].pulse_sign);
+        put_bits_le(&pb, 5, p->subframe[1].pulse_sign);
+        put_bits_le(&pb, 6, p->subframe[2].pulse_sign);
+        put_bits_le(&pb, 5, p->subframe[3].pulse_sign);
     }
 
-    flush_put_bits(&pb);
+    flush_put_bits_le(&pb);
     return frame_size[info_bits];
 }
 
diff --git a/libavcodec/gif.c b/libavcodec/gif.c
index d9c99d5..2cb1f73 100644
--- a/libavcodec/gif.c
+++ b/libavcodec/gif.c
@@ -28,7 +28,6 @@
  * @see http://www.w3.org/Graphics/GIF/spec-gif89a.txt
  */
 
-#define BITSTREAM_WRITER_LE
 #include "libavutil/opt.h"
 #include "libavutil/imgutils.h"
 #include "avcodec.h"
@@ -176,7 +175,7 @@ static int gif_image_write_image(AVCodecContext *avctx,
     bytestream_put_byte(bytestream, 0x08);
 
     ff_lzw_encode_init(s->lzw, s->buf, s->buf_size,
-                       12, FF_LZW_GIF, put_bits);
+                       12, FF_LZW_GIF, put_bits_le);
 
     ptr = buf + y_start*linesize + x_start;
     if (honor_transparency) {
@@ -198,7 +197,7 @@ static int gif_image_write_image(AVCodecContext *avctx,
             ptr += linesize;
         }
     }
-    len += ff_lzw_encode_flush(s->lzw, flush_put_bits);
+    len += ff_lzw_encode_flush(s->lzw, flush_put_bits_le);
 
     ptr = s->buf;
     while (len > 0) {
diff --git a/libavcodec/put_bits.h b/libavcodec/put_bits.h
index 1ceb1cc..2ea3c6b 100644
--- a/libavcodec/put_bits.h
+++ b/libavcodec/put_bits.h
@@ -100,19 +100,12 @@ static inline int put_bits_left(PutBitContext* s)
  */
 static inline void flush_put_bits(PutBitContext *s)
 {
-#ifndef BITSTREAM_WRITER_LE
     if (s->bit_left < 32)
         s->bit_buf <<= s->bit_left;
-#endif
     while (s->bit_left < 32) {
         av_assert0(s->buf_ptr < s->buf_end);
-#ifdef BITSTREAM_WRITER_LE
-        *s->buf_ptr++ = s->bit_buf;
-        s->bit_buf  >>= 8;
-#else
         *s->buf_ptr++ = s->bit_buf >> 24;
         s->bit_buf  <<= 8;
-#endif
         s->bit_left  += 8;
     }
     s->bit_left = 32;
@@ -131,11 +124,6 @@ static inline void flush_put_bits_le(PutBitContext *s)
     s->bit_buf  = 0;
 }
 
-#ifdef BITSTREAM_WRITER_LE
-#define avpriv_align_put_bits align_put_bits_unsupported_here
-#define avpriv_put_string ff_put_string_unsupported_here
-#define avpriv_copy_bits avpriv_copy_bits_unsupported_here
-#else
 /**
  * Pad the bitstream with zeros up to the next byte boundary.
  */
@@ -155,7 +143,6 @@ void avpriv_put_string(PutBitContext *pb, const char *string,
  * @param length the number of bits of src to copy
  */
 void avpriv_copy_bits(PutBitContext *pb, const uint8_t *src, int length);
-#endif
 
 /**
  * Write up to 31 bits into a bitstream.
@@ -172,21 +159,6 @@ static inline void put_bits(PutBitContext *s, int n, unsigned int value)
     bit_left = s->bit_left;
 
     /* XXX: optimize */
-#ifdef BITSTREAM_WRITER_LE
-    bit_buf |= value << (32 - bit_left);
-    if (n >= bit_left) {
-        if (3 < s->buf_end - s->buf_ptr) {
-            AV_WL32(s->buf_ptr, bit_buf);
-            s->buf_ptr += 4;
-        } else {
-            av_log(NULL, AV_LOG_ERROR, "Internal error, put_bits buffer too small\n");
-            av_assert2(0);
-        }
-        bit_buf     = value >> bit_left;
-        bit_left   += 32;
-    }
-    bit_left -= n;
-#else
     if (n < bit_left) {
         bit_buf     = (bit_buf << n) | value;
         bit_left   -= n;
@@ -203,7 +175,6 @@ static inline void put_bits(PutBitContext *s, int n, unsigned int value)
         bit_left   += 32 - n;
         bit_buf     = value;
     }
-#endif
 
     s->bit_buf  = bit_buf;
     s->bit_left = bit_left;
@@ -244,6 +215,13 @@ static inline void put_sbits(PutBitContext *pb, int n, int32_t value)
     put_bits(pb, n, av_mod_uintp2(value, n));
 }
 
+static inline void put_sbits_le(PutBitContext *pb, int n, int32_t value)
+{
+    av_assert2(n >= 0 && n <= 31);
+
+    put_bits_le(pb, n, av_mod_uintp2(value, n));
+}
+
 /**
  * Write exactly 32 bits into a bitstream.
  */
@@ -255,28 +233,41 @@ static void av_unused put_bits32(PutBitContext *s, uint32_t value)
     bit_buf  = s->bit_buf;
     bit_left = s->bit_left;
 
-#ifdef BITSTREAM_WRITER_LE
-    bit_buf |= value << (32 - bit_left);
+    bit_buf     = (uint64_t)bit_buf << bit_left;
+    bit_buf    |= value >> (32 - bit_left);
     if (3 < s->buf_end - s->buf_ptr) {
-        AV_WL32(s->buf_ptr, bit_buf);
+        AV_WB32(s->buf_ptr, bit_buf);
         s->buf_ptr += 4;
     } else {
         av_log(NULL, AV_LOG_ERROR, "Internal error, put_bits buffer too small\n");
         av_assert2(0);
     }
-    bit_buf     = (uint64_t)value >> bit_left;
-#else
-    bit_buf     = (uint64_t)bit_buf << bit_left;
-    bit_buf    |= value >> (32 - bit_left);
+    bit_buf     = value;
+
+    s->bit_buf  = bit_buf;
+    s->bit_left = bit_left;
+}
+
+/**
+ * Write exactly 32 bits into a bitstream.
+ */
+static void av_unused put_bits32_le(PutBitContext *s, uint32_t value)
+{
+    unsigned int bit_buf;
+    int bit_left;
+
+    bit_buf  = s->bit_buf;
+    bit_left = s->bit_left;
+
+    bit_buf |= value << (32 - bit_left);
     if (3 < s->buf_end - s->buf_ptr) {
-        AV_WB32(s->buf_ptr, bit_buf);
+        AV_WL32(s->buf_ptr, bit_buf);
         s->buf_ptr += 4;
     } else {
         av_log(NULL, AV_LOG_ERROR, "Internal error, put_bits buffer too small\n");
         av_assert2(0);
     }
-    bit_buf     = value;
-#endif
+    bit_buf     = (uint64_t)value >> bit_left;
 
     s->bit_buf  = bit_buf;
     s->bit_left = bit_left;
@@ -296,24 +287,13 @@ static inline void put_bits64(PutBitContext *s, int n, uint64_t value)
     else if (n < 64) {
         uint32_t lo = value & 0xffffffff;
         uint32_t hi = value >> 32;
-#ifdef BITSTREAM_WRITER_LE
-        put_bits32(s, lo);
-        put_bits(s, n - 32, hi);
-#else
         put_bits(s, n - 32, hi);
         put_bits32(s, lo);
-#endif
     } else {
         uint32_t lo = value & 0xffffffff;
         uint32_t hi = value >> 32;
-#ifdef BITSTREAM_WRITER_LE
-        put_bits32(s, lo);
-        put_bits32(s, hi);
-#else
         put_bits32(s, hi);
         put_bits32(s, lo);
-#endif
-
     }
 }
 
diff --git a/libavcodec/ttaenc.c b/libavcodec/ttaenc.c
index 3cc54d7..424ee6b 100644
--- a/libavcodec/ttaenc.c
+++ b/libavcodec/ttaenc.c
@@ -18,7 +18,6 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-#define BITSTREAM_WRITER_LE
 #include "ttadata.h"
 #include "ttaencdsp.h"
 #include "avcodec.h"
@@ -161,19 +160,19 @@ pkt_alloc:
             }
             do {
                 if (unary > 31) {
-                    put_bits(&pb, 31, 0x7FFFFFFF);
+                    put_bits_le(&pb, 31, 0x7FFFFFFF);
                     unary -= 31;
                 } else {
-                    put_bits(&pb, unary, (1 << unary) - 1);
+                    put_bits_le(&pb, unary, (1 << unary) - 1);
                     unary = 0;
                 }
             } while (unary);
         }
 
-        put_bits(&pb, 1, 0);
+        put_bits_le(&pb, 1, 0);
 
         if (k)
-            put_bits(&pb, k, outval & (ff_tta_shift_1[k] - 1));
+            put_bits_le(&pb, k, outval & (ff_tta_shift_1[k] - 1));
 
         if (cur_chan < avctx->channels - 1)
             cur_chan++;
@@ -181,10 +180,10 @@ pkt_alloc:
             cur_chan = 0;
     }
 
-    flush_put_bits(&pb);
+    flush_put_bits_le(&pb);
     out_bytes = put_bits_count(&pb) >> 3;
-    put_bits32(&pb, av_crc(s->crc_table, UINT32_MAX, avpkt->data, out_bytes) ^ UINT32_MAX);
-    flush_put_bits(&pb);
+    put_bits32_le(&pb, av_crc(s->crc_table, UINT32_MAX, avpkt->data, out_bytes) ^ UINT32_MAX);
+    flush_put_bits_le(&pb);
 
     avpkt->pts      = frame->pts;
     avpkt->size     = out_bytes + 4;
diff --git a/libavcodec/vorbisenc.c b/libavcodec/vorbisenc.c
index bf21a3b..f3c41d0 100644
--- a/libavcodec/vorbisenc.c
+++ b/libavcodec/vorbisenc.c
@@ -37,7 +37,6 @@
 #include "audio_frame_queue.h"
 #include "libavfilter/bufferqueue.h"
 
-#define BITSTREAM_WRITER_LE
 #include "put_bits.h"
 
 #undef NDEBUG
@@ -157,7 +156,7 @@ static inline int put_codeword(PutBitContext *pb, vorbis_enc_codebook *cb,
     av_assert2(cb->lens[entry]);
     if (pb->size_in_bits - put_bits_count(pb) < cb->lens[entry])
         return AVERROR(EINVAL);
-    put_bits(pb, cb->lens[entry], cb->codewords[entry]);
+    put_bits_le(pb, cb->lens[entry], cb->codewords[entry]);
     return 0;
 }
 
@@ -478,7 +477,7 @@ static void put_float(PutBitContext *pb, float f)
         mant = -mant;
     }
     res |= mant | (exp << 21);
-    put_bits32(pb, res);
+    put_bits32_le(pb, res);
 }
 
 static void put_codebook_header(PutBitContext *pb, vorbis_enc_codebook *cb)
@@ -486,9 +485,9 @@ static void put_codebook_header(PutBitContext *pb, vorbis_enc_codebook *cb)
     int i;
     int ordered = 0;
 
-    put_bits(pb, 24, 0x564342); //magic
-    put_bits(pb, 16, cb->ndimensions);
-    put_bits(pb, 24, cb->nentries);
+    put_bits_le(pb, 24, 0x564342); //magic
+    put_bits_le(pb, 16, cb->ndimensions);
+    put_bits_le(pb, 24, cb->nentries);
 
     for (i = 1; i < cb->nentries; i++)
         if (cb->lens[i] < cb->lens[i-1])
@@ -496,17 +495,17 @@ static void put_codebook_header(PutBitContext *pb, vorbis_enc_codebook *cb)
     if (i == cb->nentries)
         ordered = 1;
 
-    put_bits(pb, 1, ordered);
+    put_bits_le(pb, 1, ordered);
     if (ordered) {
         int len = cb->lens[0];
-        put_bits(pb, 5, len - 1);
+        put_bits_le(pb, 5, len - 1);
         i = 0;
         while (i < cb->nentries) {
             int j;
             for (j = 0; j+i < cb->nentries; j++)
                 if (cb->lens[j+i] != len)
                     break;
-            put_bits(pb, ilog(cb->nentries - i), j);
+            put_bits_le(pb, ilog(cb->nentries - i), j);
             i += j;
             len++;
         }
@@ -517,17 +516,17 @@ static void put_codebook_header(PutBitContext *pb, vorbis_enc_codebook *cb)
                 break;
         if (i != cb->nentries)
             sparse = 1;
-        put_bits(pb, 1, sparse);
+        put_bits_le(pb, 1, sparse);
 
         for (i = 0; i < cb->nentries; i++) {
             if (sparse)
-                put_bits(pb, 1, !!cb->lens[i]);
+                put_bits_le(pb, 1, !!cb->lens[i]);
             if (cb->lens[i])
-                put_bits(pb, 5, cb->lens[i] - 1);
+                put_bits_le(pb, 5, cb->lens[i] - 1);
         }
     }
 
-    put_bits(pb, 4, cb->lookup);
+    put_bits_le(pb, 4, cb->lookup);
     if (cb->lookup) {
         int tmp  = cb_lookup_vals(cb->lookup, cb->ndimensions, cb->nentries);
         int bits = ilog(cb->quantlist[0]);
@@ -538,11 +537,11 @@ static void put_codebook_header(PutBitContext *pb, vorbis_enc_codebook *cb)
         put_float(pb, cb->min);
         put_float(pb, cb->delta);
 
-        put_bits(pb, 4, bits - 1);
-        put_bits(pb, 1, cb->seq_p);
+        put_bits_le(pb, 4, bits - 1);
+        put_bits_le(pb, 1, cb->seq_p);
 
         for (i = 0; i < tmp; i++)
-            put_bits(pb, bits, cb->quantlist[i]);
+            put_bits_le(pb, bits, cb->quantlist[i]);
     }
 }
 
@@ -550,64 +549,64 @@ static void put_floor_header(PutBitContext *pb, vorbis_enc_floor *fc)
 {
     int i;
 
-    put_bits(pb, 16, 1); // type, only floor1 is supported
+    put_bits_le(pb, 16, 1); // type, only floor1 is supported
 
-    put_bits(pb, 5, fc->partitions);
+    put_bits_le(pb, 5, fc->partitions);
 
     for (i = 0; i < fc->partitions; i++)
-        put_bits(pb, 4, fc->partition_to_class[i]);
+        put_bits_le(pb, 4, fc->partition_to_class[i]);
 
     for (i = 0; i < fc->nclasses; i++) {
         int j, books;
 
-        put_bits(pb, 3, fc->classes[i].dim - 1);
-        put_bits(pb, 2, fc->classes[i].subclass);
+        put_bits_le(pb, 3, fc->classes[i].dim - 1);
+        put_bits_le(pb, 2, fc->classes[i].subclass);
 
         if (fc->classes[i].subclass)
-            put_bits(pb, 8, fc->classes[i].masterbook);
+            put_bits_le(pb, 8, fc->classes[i].masterbook);
 
         books = (1 << fc->classes[i].subclass);
 
         for (j = 0; j < books; j++)
-            put_bits(pb, 8, fc->classes[i].books[j] + 1);
+            put_bits_le(pb, 8, fc->classes[i].books[j] + 1);
     }
 
-    put_bits(pb, 2, fc->multiplier - 1);
-    put_bits(pb, 4, fc->rangebits);
+    put_bits_le(pb, 2, fc->multiplier - 1);
+    put_bits_le(pb, 4, fc->rangebits);
 
     for (i = 2; i < fc->values; i++)
-        put_bits(pb, fc->rangebits, fc->list[i].x);
+        put_bits_le(pb, fc->rangebits, fc->list[i].x);
 }
 
 static void put_residue_header(PutBitContext *pb, vorbis_enc_residue *rc)
 {
     int i;
 
-    put_bits(pb, 16, rc->type);
+    put_bits_le(pb, 16, rc->type);
 
-    put_bits(pb, 24, rc->begin);
-    put_bits(pb, 24, rc->end);
-    put_bits(pb, 24, rc->partition_size - 1);
-    put_bits(pb, 6, rc->classifications - 1);
-    put_bits(pb, 8, rc->classbook);
+    put_bits_le(pb, 24, rc->begin);
+    put_bits_le(pb, 24, rc->end);
+    put_bits_le(pb, 24, rc->partition_size - 1);
+    put_bits_le(pb, 6, rc->classifications - 1);
+    put_bits_le(pb, 8, rc->classbook);
 
     for (i = 0; i < rc->classifications; i++) {
         int j, tmp = 0;
         for (j = 0; j < 8; j++)
             tmp |= (rc->books[i][j] != -1) << j;
 
-        put_bits(pb, 3, tmp & 7);
-        put_bits(pb, 1, tmp > 7);
+        put_bits_le(pb, 3, tmp & 7);
+        put_bits_le(pb, 1, tmp > 7);
 
         if (tmp > 7)
-            put_bits(pb, 5, tmp >> 3);
+            put_bits_le(pb, 5, tmp >> 3);
     }
 
     for (i = 0; i < rc->classifications; i++) {
         int j;
         for (j = 0; j < 8; j++)
             if (rc->books[i][j] != -1)
-                put_bits(pb, 8, rc->books[i][j]);
+                put_bits_le(pb, 8, rc->books[i][j]);
     }
 }
 
@@ -623,108 +622,108 @@ static int put_main_header(vorbis_enc_context *venc, uint8_t **out)
 
     // identification header
     init_put_bits(&pb, p, buffer_len);
-    put_bits(&pb, 8, 1); //magic
+    put_bits_le(&pb, 8, 1); //magic
     for (i = 0; "vorbis"[i]; i++)
-        put_bits(&pb, 8, "vorbis"[i]);
-    put_bits32(&pb, 0); // version
-    put_bits(&pb,  8, venc->channels);
-    put_bits32(&pb, venc->sample_rate);
-    put_bits32(&pb, 0); // bitrate
-    put_bits32(&pb, 0); // bitrate
-    put_bits32(&pb, 0); // bitrate
-    put_bits(&pb,  4, venc->log2_blocksize[0]);
-    put_bits(&pb,  4, venc->log2_blocksize[1]);
-    put_bits(&pb,  1, 1); // framing
-
-    flush_put_bits(&pb);
+        put_bits_le(&pb, 8, "vorbis"[i]);
+    put_bits32_le(&pb, 0); // version
+    put_bits_le(&pb,  8, venc->channels);
+    put_bits32_le(&pb, venc->sample_rate);
+    put_bits32_le(&pb, 0); // bitrate
+    put_bits32_le(&pb, 0); // bitrate
+    put_bits32_le(&pb, 0); // bitrate
+    put_bits_le(&pb,  4, venc->log2_blocksize[0]);
+    put_bits_le(&pb,  4, venc->log2_blocksize[1]);
+    put_bits_le(&pb,  1, 1); // framing
+
+    flush_put_bits_le(&pb);
     hlens[0] = put_bits_count(&pb) >> 3;
     buffer_len -= hlens[0];
     p += hlens[0];
 
     // comment header
     init_put_bits(&pb, p, buffer_len);
-    put_bits(&pb, 8, 3); //magic
+    put_bits_le(&pb, 8, 3); //magic
     for (i = 0; "vorbis"[i]; i++)
-        put_bits(&pb, 8, "vorbis"[i]);
-    put_bits32(&pb, 0); // vendor length TODO
-    put_bits32(&pb, 0); // amount of comments
-    put_bits(&pb,  1, 1); // framing
+        put_bits_le(&pb, 8, "vorbis"[i]);
+    put_bits32_le(&pb, 0); // vendor length TODO
+    put_bits32_le(&pb, 0); // amount of comments
+    put_bits_le(&pb,  1, 1); // framing
 
-    flush_put_bits(&pb);
+    flush_put_bits_le(&pb);
     hlens[1] = put_bits_count(&pb) >> 3;
     buffer_len -= hlens[1];
     p += hlens[1];
 
     // setup header
     init_put_bits(&pb, p, buffer_len);
-    put_bits(&pb, 8, 5); //magic
+    put_bits_le(&pb, 8, 5); //magic
     for (i = 0; "vorbis"[i]; i++)
-        put_bits(&pb, 8, "vorbis"[i]);
+        put_bits_le(&pb, 8, "vorbis"[i]);
 
     // codebooks
-    put_bits(&pb, 8, venc->ncodebooks - 1);
+    put_bits_le(&pb, 8, venc->ncodebooks - 1);
     for (i = 0; i < venc->ncodebooks; i++)
         put_codebook_header(&pb, &venc->codebooks[i]);
 
     // time domain, reserved, zero
-    put_bits(&pb,  6, 0);
-    put_bits(&pb, 16, 0);
+    put_bits_le(&pb,  6, 0);
+    put_bits_le(&pb, 16, 0);
 
     // floors
-    put_bits(&pb, 6, venc->nfloors - 1);
+    put_bits_le(&pb, 6, venc->nfloors - 1);
     for (i = 0; i < venc->nfloors; i++)
         put_floor_header(&pb, &venc->floors[i]);
 
     // residues
-    put_bits(&pb, 6, venc->nresidues - 1);
+    put_bits_le(&pb, 6, venc->nresidues - 1);
     for (i = 0; i < venc->nresidues; i++)
         put_residue_header(&pb, &venc->residues[i]);
 
     // mappings
-    put_bits(&pb, 6, venc->nmappings - 1);
+    put_bits_le(&pb, 6, venc->nmappings - 1);
     for (i = 0; i < venc->nmappings; i++) {
         vorbis_enc_mapping *mc = &venc->mappings[i];
         int j;
-        put_bits(&pb, 16, 0); // mapping type
+        put_bits_le(&pb, 16, 0); // mapping type
 
-        put_bits(&pb, 1, mc->submaps > 1);
+        put_bits_le(&pb, 1, mc->submaps > 1);
         if (mc->submaps > 1)
-            put_bits(&pb, 4, mc->submaps - 1);
+            put_bits_le(&pb, 4, mc->submaps - 1);
 
-        put_bits(&pb, 1, !!mc->coupling_steps);
+        put_bits_le(&pb, 1, !!mc->coupling_steps);
         if (mc->coupling_steps) {
-            put_bits(&pb, 8, mc->coupling_steps - 1);
+            put_bits_le(&pb, 8, mc->coupling_steps - 1);
             for (j = 0; j < mc->coupling_steps; j++) {
-                put_bits(&pb, ilog(venc->channels - 1), mc->magnitude[j]);
-                put_bits(&pb, ilog(venc->channels - 1), mc->angle[j]);
+                put_bits_le(&pb, ilog(venc->channels - 1), mc->magnitude[j]);
+                put_bits_le(&pb, ilog(venc->channels - 1), mc->angle[j]);
             }
         }
 
-        put_bits(&pb, 2, 0); // reserved
+        put_bits_le(&pb, 2, 0); // reserved
 
         if (mc->submaps > 1)
             for (j = 0; j < venc->channels; j++)
-                put_bits(&pb, 4, mc->mux[j]);
+                put_bits_le(&pb, 4, mc->mux[j]);
 
         for (j = 0; j < mc->submaps; j++) {
-            put_bits(&pb, 8, 0); // reserved time configuration
-            put_bits(&pb, 8, mc->floor[j]);
-            put_bits(&pb, 8, mc->residue[j]);
+            put_bits_le(&pb, 8, 0); // reserved time configuration
+            put_bits_le(&pb, 8, mc->floor[j]);
+            put_bits_le(&pb, 8, mc->residue[j]);
         }
     }
 
     // modes
-    put_bits(&pb, 6, venc->nmodes - 1);
+    put_bits_le(&pb, 6, venc->nmodes - 1);
     for (i = 0; i < venc->nmodes; i++) {
-        put_bits(&pb, 1, venc->modes[i].blockflag);
-        put_bits(&pb, 16, 0); // reserved window type
-        put_bits(&pb, 16, 0); // reserved transform type
-        put_bits(&pb, 8, venc->modes[i].mapping);
+        put_bits_le(&pb, 1, venc->modes[i].blockflag);
+        put_bits_le(&pb, 16, 0); // reserved window type
+        put_bits_le(&pb, 16, 0); // reserved transform type
+        put_bits_le(&pb, 8, venc->modes[i].mapping);
     }
 
-    put_bits(&pb, 1, 1); // framing
+    put_bits_le(&pb, 1, 1); // framing
 
-    flush_put_bits(&pb);
+    flush_put_bits_le(&pb);
     hlens[2] = put_bits_count(&pb) >> 3;
 
     len = hlens[0] + hlens[1] + hlens[2];
@@ -800,9 +799,9 @@ static int floor_encode(vorbis_enc_context *venc, vorbis_enc_floor *fc,
 
     if (pb->size_in_bits - put_bits_count(pb) < 1 + 2 * ilog(range - 1))
         return AVERROR(EINVAL);
-    put_bits(pb, 1, 1); // non zero
-    put_bits(pb, ilog(range - 1), posts[0]);
-    put_bits(pb, ilog(range - 1), posts[1]);
+    put_bits_le(pb, 1, 1); // non zero
+    put_bits_le(pb, ilog(range - 1), posts[0]);
+    put_bits_le(pb, ilog(range - 1), posts[1]);
     coded[0] = coded[1] = 1;
 
     for (i = 2; i < fc->values; i++) {
@@ -1134,15 +1133,15 @@ static int vorbis_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
         return AVERROR(EINVAL);
     }
 
-    put_bits(&pb, 1, 0); // magic bit
+    put_bits_le(&pb, 1, 0); // magic bit
 
-    put_bits(&pb, ilog(venc->nmodes - 1), 1); // Mode for current frame
+    put_bits_le(&pb, ilog(venc->nmodes - 1), 1); // Mode for current frame
 
     mode    = &venc->modes[1];
     mapping = &venc->mappings[mode->mapping];
     if (mode->blockflag) {
-        put_bits(&pb, 1, 1); // Previous windowflag
-        put_bits(&pb, 1, 1); // Next windowflag
+        put_bits_le(&pb, 1, 1); // Previous windowflag
+        put_bits_le(&pb, 1, 1); // Next windowflag
     }
 
     for (i = 0; i < venc->channels; i++) {
@@ -1178,7 +1177,7 @@ static int vorbis_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
         return AVERROR(EINVAL);
     }
 
-    flush_put_bits(&pb);
+    flush_put_bits_le(&pb);
     avpkt->size = put_bits_count(&pb) >> 3;
 
     ff_af_queue_remove(&venc->afq, frame_size, &avpkt->pts, &avpkt->duration);
diff --git a/libavcodec/wavpackenc.c b/libavcodec/wavpackenc.c
index 979b921..9115c9e 100644
--- a/libavcodec/wavpackenc.c
+++ b/libavcodec/wavpackenc.c
@@ -18,8 +18,6 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-#define BITSTREAM_WRITER_LE
-
 #include "libavutil/intreadwrite.h"
 #include "libavutil/opt.h"
 #include "avcodec.h"
@@ -1987,18 +1985,18 @@ static void encode_flush(WavPackEncodeContext *s)
 
         do {
             if (cbits > 31) {
-                put_bits(pb, 31, 0x7FFFFFFF);
+                put_bits_le(pb, 31, 0x7FFFFFFF);
                 cbits -= 31;
             } else {
-                put_bits(pb, cbits, (1 << cbits) - 1);
+                put_bits_le(pb, cbits, (1 << cbits) - 1);
                 cbits = 0;
             }
         } while (cbits);
 
-        put_bits(pb, 1, 0);
+        put_bits_le(pb, 1, 0);
 
         while (w->zeros_acc > 1) {
-            put_bits(pb, 1, w->zeros_acc & 1);
+            put_bits_le(pb, 1, w->zeros_acc & 1);
             w->zeros_acc >>= 1;
         }
 
@@ -2009,43 +2007,43 @@ static void encode_flush(WavPackEncodeContext *s)
         if (w->holding_one >= 16) {
             int cbits;
 
-            put_bits(pb, 16, (1 << 16) - 1);
-            put_bits(pb, 1, 0);
+            put_bits_le(pb, 16, (1 << 16) - 1);
+            put_bits_le(pb, 1, 0);
             w->holding_one -= 16;
             cbits = count_bits(w->holding_one);
 
             do {
                 if (cbits > 31) {
-                    put_bits(pb, 31, 0x7FFFFFFF);
+                    put_bits_le(pb, 31, 0x7FFFFFFF);
                     cbits -= 31;
                 } else {
-                    put_bits(pb, cbits, (1 << cbits) - 1);
+                    put_bits_le(pb, cbits, (1 << cbits) - 1);
                     cbits = 0;
                 }
             } while (cbits);
 
-            put_bits(pb, 1, 0);
+            put_bits_le(pb, 1, 0);
 
             while (w->holding_one > 1) {
-                put_bits(pb, 1, w->holding_one & 1);
+                put_bits_le(pb, 1, w->holding_one & 1);
                 w->holding_one >>= 1;
             }
 
             w->holding_zero = 0;
         } else {
-            put_bits(pb, w->holding_one, (1 << w->holding_one) - 1);
+            put_bits_le(pb, w->holding_one, (1 << w->holding_one) - 1);
         }
 
         w->holding_one = 0;
     }
 
     if (w->holding_zero) {
-        put_bits(pb, 1, 0);
+        put_bits_le(pb, 1, 0);
         w->holding_zero = 0;
     }
 
     if (w->pend_count) {
-        put_bits(pb, w->pend_count, w->pend_data);
+        put_bits_le(pb, w->pend_count, w->pend_data);
         w->pend_data = w->pend_count = 0;
     }
 }
@@ -2065,7 +2063,7 @@ static void wavpack_encode_sample(WavPackEncodeContext *s, WvChannel *c, int32_t
                 return;
             }
         } else if (sample) {
-            put_bits(&s->pb, 1, 0);
+            put_bits_le(&s->pb, 1, 0);
         } else {
             CLEAR(s->w.c[0].median);
             CLEAR(s->w.c[1].median);
@@ -2176,10 +2174,10 @@ static void pack_float_sample(WavPackEncodeContext *s, int32_t *sample)
 
     if (get_exponent(*sample) == 255) {
         if (get_mantissa(*sample)) {
-            put_bits(pb, 1, 1);
-            put_bits(pb, 23, get_mantissa(*sample));
+            put_bits_le(pb, 1, 1);
+            put_bits_le(pb, 23, get_mantissa(*sample));
         } else {
-            put_bits(pb, 1, 0);
+            put_bits_le(pb, 1, 0);
         }
 
         value = 0x1000000;
@@ -2200,25 +2198,25 @@ static void pack_float_sample(WavPackEncodeContext *s, int32_t *sample)
     if (!value) {
         if (s->float_flags & FLOAT_ZEROS_SENT) {
             if (get_exponent(*sample) || get_mantissa(*sample)) {
-                put_bits(pb, 1, 1);
-                put_bits(pb, 23, get_mantissa(*sample));
+                put_bits_le(pb, 1, 1);
+                put_bits_le(pb, 23, get_mantissa(*sample));
 
                 if (max_exp >= 25)
-                    put_bits(pb, 8, get_exponent(*sample));
+                    put_bits_le(pb, 8, get_exponent(*sample));
 
-                put_bits(pb, 1, get_sign(*sample));
+                put_bits_le(pb, 1, get_sign(*sample));
             } else {
-                put_bits(pb, 1, 0);
+                put_bits_le(pb, 1, 0);
 
                 if (s->float_flags & FLOAT_NEG_ZEROS)
-                    put_bits(pb, 1, get_sign(*sample));
+                    put_bits_le(pb, 1, get_sign(*sample));
             }
         }
     } else if (shift_count) {
         if (s->float_flags & FLOAT_SHIFT_SENT) {
-            put_sbits(pb, shift_count, get_mantissa(*sample));
+            put_sbits_le(pb, shift_count, get_mantissa(*sample));
         } else if (s->float_flags & FLOAT_SHIFT_SAME) {
-            put_bits(pb, 1, get_mantissa(*sample) & 1);
+            put_bits_le(pb, 1, get_mantissa(*sample) & 1);
         }
     }
 }
@@ -2790,7 +2788,7 @@ static int wavpack_encode_block(WavPackEncodeContext *s,
         }
     }
     encode_flush(s);
-    flush_put_bits(&s->pb);
+    flush_put_bits_le(&s->pb);
     data_size = put_bits_count(&s->pb) >> 3;
     bytestream2_put_le24(&pb, (data_size + 1) >> 1);
     bytestream2_skip_p(&pb, data_size);
@@ -2804,7 +2802,7 @@ static int wavpack_encode_block(WavPackEncodeContext *s,
             pack_float(s, s->orig_l, s->orig_r, nb_samples);
         else
             pack_int32(s, s->orig_l, s->orig_r, nb_samples);
-        flush_put_bits(&s->pb);
+        flush_put_bits_le(&s->pb);
         data_size = put_bits_count(&s->pb) >> 3;
         bytestream2_put_le24(&pb, (data_size + 5) >> 1);
         bytestream2_put_le32(&pb, s->crc_x);
-- 
1.7.10.4

