diff mbox series

[FFmpeg-devel,11/12] Avoid intermediate bitcount for number of bytes in PutBitContext

Message ID 20210325154956.2405162-11-andreas.rheinhardt@gmail.com
State Accepted
Commit c81b8e04aa0952a7aec1e08940f29ae501fb6bfd
Headers show
Series [FFmpeg-devel,01/12] avcodec/vorbisenc: Remove always-false check
Related show

Checks

Context Check Description
andriy/x86_make success Make finished
andriy/x86_make_fate success Make fate finished
andriy/PPC64_make success Make finished
andriy/PPC64_make_fate success Make fate finished

Commit Message

Andreas Rheinhardt March 25, 2021, 3:49 p.m. UTC
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
---
 libavcodec/aacenc.c                |  4 ++--
 libavcodec/alacenc.c               |  2 +-
 libavcodec/asvenc.c                |  2 +-
 libavcodec/cbs.c                   |  2 +-
 libavcodec/cfhdenc.c               |  2 +-
 libavcodec/cljrenc.c               |  2 +-
 libavcodec/dca.c                   |  2 +-
 libavcodec/dcaenc.c                |  2 +-
 libavcodec/ffv1enc.c               |  2 +-
 libavcodec/flacenc.c               |  6 +++---
 libavcodec/hevc_ps_enc.c           |  2 +-
 libavcodec/lzwenc.c                |  2 +-
 libavcodec/magicyuvenc.c           |  6 ++----
 libavcodec/mjpegenc.c              |  2 +-
 libavcodec/mjpegenc_common.c       |  7 ++-----
 libavcodec/mlpenc.c                |  8 ++++----
 libavcodec/mpeg12enc.c             |  2 +-
 libavcodec/mpeg4videoenc.c         |  2 +-
 libavcodec/mpegaudioenc_template.c |  8 ++++----
 libavcodec/mpegvideo_enc.c         | 11 ++++++-----
 libavcodec/proresenc_anatoliy.c    |  2 +-
 libavcodec/rpzaenc.c               |  2 +-
 libavcodec/sbcenc.c                |  2 +-
 libavcodec/sonic.c                 |  2 +-
 libavcodec/svq1enc.c               |  2 +-
 libavcodec/tests/cabac.c           |  2 +-
 libavcodec/ttaenc.c                |  2 +-
 libavcodec/vorbisenc.c             |  8 ++++----
 libavcodec/wavpackenc.c            |  4 ++--
 libavcodec/wmaenc.c                |  2 +-
 libavcodec/xsubenc.c               |  4 ++--
 libavfilter/vf_signature.c         |  2 +-
 libavformat/latmenc.c              |  2 +-
 libavformat/movenc.c               |  2 +-
 34 files changed, 55 insertions(+), 59 deletions(-)
diff mbox series

Patch

diff --git a/libavcodec/aacenc.c b/libavcodec/aacenc.c
index 070a2e706a..6dc68eb9b0 100644
--- a/libavcodec/aacenc.c
+++ b/libavcodec/aacenc.c
@@ -115,7 +115,7 @@  static int put_audio_specific_config(AVCodecContext *avctx)
     put_bits(&pb, 5,  AOT_SBR);
     put_bits(&pb, 1,  0);
     flush_put_bits(&pb);
-    avctx->extradata_size = put_bits_count(&pb) >> 3;
+    avctx->extradata_size = put_bytes_output(&pb);
 
     return 0;
 }
@@ -881,6 +881,7 @@  static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
     flush_put_bits(&s->pb);
 
     s->last_frame_pb_count = put_bits_count(&s->pb);
+    avpkt->size            = put_bytes_output(&s->pb);
 
     s->lambda_sum += s->lambda;
     s->lambda_count++;
@@ -888,7 +889,6 @@  static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
     ff_af_queue_remove(&s->afq, avctx->frame_size, &avpkt->pts,
                        &avpkt->duration);
 
-    avpkt->size = put_bits_count(&s->pb) >> 3;
     *got_packet_ptr = 1;
     return 0;
 }
diff --git a/libavcodec/alacenc.c b/libavcodec/alacenc.c
index 9d135d1350..ecdd46cac3 100644
--- a/libavcodec/alacenc.c
+++ b/libavcodec/alacenc.c
@@ -485,7 +485,7 @@  static int write_frame(AlacEncodeContext *s, AVPacket *avpkt,
     put_bits(pb, 3, TYPE_END);
     flush_put_bits(pb);
 
-    return put_bits_count(pb) >> 3;
+    return put_bytes_output(pb);
 }
 
 static av_always_inline int get_max_frame_size(int frame_size, int ch, int bps)
diff --git a/libavcodec/asvenc.c b/libavcodec/asvenc.c
index 2d8c310521..c33a3b4681 100644
--- a/libavcodec/asvenc.c
+++ b/libavcodec/asvenc.c
@@ -290,7 +290,7 @@  static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
     else
         flush_put_bits_le(&a->pb);
     AV_WN32(put_bits_ptr(&a->pb), 0);
-    size = (put_bits_count(&a->pb) + 31) / 32;
+    size = (put_bytes_output(&a->pb) + 3) / 4;
 
     if (avctx->codec_id == AV_CODEC_ID_ASV1) {
         a->bbdsp.bswap_buf((uint32_t *) pkt->data,
diff --git a/libavcodec/cbs.c b/libavcodec/cbs.c
index c7f69845fb..bbfafb6530 100644
--- a/libavcodec/cbs.c
+++ b/libavcodec/cbs.c
@@ -346,7 +346,7 @@  static int cbs_write_unit_data(CodedBitstreamContext *ctx,
 
     flush_put_bits(&pbc);
 
-    ret = ff_cbs_alloc_unit_data(unit, put_bits_count(&pbc) / 8);
+    ret = ff_cbs_alloc_unit_data(unit, put_bytes_output(&pbc));
     if (ret < 0)
         return ret;
 
diff --git a/libavcodec/cfhdenc.c b/libavcodec/cfhdenc.c
index 42bbf99c96..0369164fab 100644
--- a/libavcodec/cfhdenc.c
+++ b/libavcodec/cfhdenc.c
@@ -766,7 +766,7 @@  static int cfhd_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
                 put_bits(pb, cb[512].size, cb[512].bits);
 
                 flush_put_bits(pb);
-                bytestream2_skip_p(pby, put_bits_count(pb) >> 3);
+                bytestream2_skip_p(pby, put_bytes_output(pb));
                 padd = (4 - (bytestream2_tell_p(pby) & 3)) & 3;
                 while (padd--)
                     bytestream2_put_byte(pby, 0);
diff --git a/libavcodec/cljrenc.c b/libavcodec/cljrenc.c
index a3718259d1..fe58f50f09 100644
--- a/libavcodec/cljrenc.c
+++ b/libavcodec/cljrenc.c
@@ -89,7 +89,7 @@  static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
 
     flush_put_bits(&pb);
 
-    pkt->size   = put_bits_count(&pb) / 8;
+    pkt->size   = put_bytes_output(&pb);
     pkt->flags |= AV_PKT_FLAG_KEY;
     *got_packet = 1;
     return 0;
diff --git a/libavcodec/dca.c b/libavcodec/dca.c
index a0729e61ab..b2152524a5 100644
--- a/libavcodec/dca.c
+++ b/libavcodec/dca.c
@@ -82,7 +82,7 @@  int avpriv_dca_convert_bitstream(const uint8_t *src, int src_size, uint8_t *dst,
             put_bits(&pb, 14, tmp);
         }
         flush_put_bits(&pb);
-        return (put_bits_count(&pb) + 7) >> 3;
+        return put_bytes_output(&pb);
     default:
         return AVERROR_INVALIDDATA;
     }
diff --git a/libavcodec/dcaenc.c b/libavcodec/dcaenc.c
index a40d2e0ca0..63e6dfb780 100644
--- a/libavcodec/dcaenc.c
+++ b/libavcodec/dcaenc.c
@@ -1213,8 +1213,8 @@  static int encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
     flush_put_bits(&c->pb);
 
     avpkt->pts      = frame->pts;
+    avpkt->size     = put_bytes_output(&c->pb);
     avpkt->duration = ff_samples_to_time_base(avctx, frame->nb_samples);
-    avpkt->size     = put_bits_count(&c->pb) >> 3;
     *got_packet_ptr = 1;
     return 0;
 }
diff --git a/libavcodec/ffv1enc.c b/libavcodec/ffv1enc.c
index 611b250e96..cdad65a4f9 100644
--- a/libavcodec/ffv1enc.c
+++ b/libavcodec/ffv1enc.c
@@ -1239,7 +1239,7 @@  FF_ENABLE_DEPRECATION_WARNINGS
             bytes = ff_rac_terminate(&fs->c, 1);
         } else {
             flush_put_bits(&fs->pb); // FIXME: nicer padding
-            bytes = fs->ac_byte_count + (put_bits_count(&fs->pb) + 7) / 8;
+            bytes = fs->ac_byte_count + put_bytes_output(&fs->pb);
         }
         if (i > 0 || f->version > 2) {
             av_assert0(bytes < pkt->size / f->slice_count);
diff --git a/libavcodec/flacenc.c b/libavcodec/flacenc.c
index 8e7dbc52b4..34fcd95795 100644
--- a/libavcodec/flacenc.c
+++ b/libavcodec/flacenc.c
@@ -1234,7 +1234,7 @@  static void write_frame_header(FlacEncodeContext *s)
 
     flush_put_bits(&s->pb);
     crc = av_crc(av_crc_get_table(AV_CRC_8_ATM), 0, s->pb.buf,
-                 put_bits_count(&s->pb) >> 3);
+                 put_bytes_output(&s->pb));
     put_bits(&s->pb, 8, crc);
 }
 
@@ -1304,7 +1304,7 @@  static void write_frame_footer(FlacEncodeContext *s)
     int crc;
     flush_put_bits(&s->pb);
     crc = av_bswap16(av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0, s->pb.buf,
-                            put_bits_count(&s->pb)>>3));
+                            put_bytes_output(&s->pb)));
     put_bits(&s->pb, 16, crc);
     flush_put_bits(&s->pb);
 }
@@ -1316,7 +1316,7 @@  static int write_frame(FlacEncodeContext *s, AVPacket *avpkt)
     write_frame_header(s);
     write_subframes(s);
     write_frame_footer(s);
-    return put_bits_count(&s->pb) >> 3;
+    return put_bytes_output(&s->pb);
 }
 
 
diff --git a/libavcodec/hevc_ps_enc.c b/libavcodec/hevc_ps_enc.c
index 5449f81c82..47f252dd2c 100644
--- a/libavcodec/hevc_ps_enc.c
+++ b/libavcodec/hevc_ps_enc.c
@@ -115,7 +115,7 @@  int ff_hevc_encode_nal_vps(HEVCVPS *vps, unsigned int id,
     put_bits(&pb, 1, 1);    // stop bit
     flush_put_bits(&pb);
 
-    data_size = put_bits_count(&pb) / 8;
+    data_size = put_bytes_output(&pb);
 
     return data_size;
 }
diff --git a/libavcodec/lzwenc.c b/libavcodec/lzwenc.c
index e693d74272..6c45f7cc37 100644
--- a/libavcodec/lzwenc.c
+++ b/libavcodec/lzwenc.c
@@ -189,7 +189,7 @@  static void clearTable(LZWEncodeState * s)
  * @return Number of bytes written
  */
 static int writtenBytes(LZWEncodeState *s){
-    int ret = put_bits_count(&s->pb) >> 3;
+    int ret = put_bytes_count(&s->pb, 0);
     ret -= s->output_bytes;
     s->output_bytes += ret;
     return ret;
diff --git a/libavcodec/magicyuvenc.c b/libavcodec/magicyuvenc.c
index d53fe6f328..7ec3c963bf 100644
--- a/libavcodec/magicyuvenc.c
+++ b/libavcodec/magicyuvenc.c
@@ -400,11 +400,9 @@  static int encode_slice(uint8_t *src, uint8_t *dst, int dst_size,
     if (count)
         put_bits(&pb, 32 - count, 0);
 
-    count = put_bits_count(&pb);
-
     flush_put_bits(&pb);
 
-    return count >> 3;
+    return put_bytes_output(&pb);
 }
 
 static int magy_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
@@ -499,7 +497,7 @@  static int magy_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
                      AV_CEIL_RSHIFT(frame->height, s->vshift[i]),
                      &s->pb, s->he[i]);
     }
-    s->tables_size = (put_bits_count(&s->pb) + 7) >> 3;
+    s->tables_size = put_bytes_count(&s->pb, 1);
     bytestream2_skip_p(&pb, s->tables_size);
 
     for (i = 0; i < s->planes; i++) {
diff --git a/libavcodec/mjpegenc.c b/libavcodec/mjpegenc.c
index 22af094da7..df3aaaf26b 100644
--- a/libavcodec/mjpegenc.c
+++ b/libavcodec/mjpegenc.c
@@ -218,7 +218,7 @@  int ff_mjpeg_encode_stuffing(MpegEncContext *s)
 
     if ((s->avctx->active_thread_type & FF_THREAD_SLICE) && mb_y < s->mb_height - 1)
         put_marker(pbc, RST0 + (mb_y&7));
-    s->esc_pos = put_bits_count(pbc) >> 3;
+    s->esc_pos = put_bytes_count(pbc, 0);
 
 fail:
     for (int i = 0; i < 3; i++)
diff --git a/libavcodec/mjpegenc_common.c b/libavcodec/mjpegenc_common.c
index 12dd7be2e8..3eae9b7d0f 100644
--- a/libavcodec/mjpegenc_common.c
+++ b/libavcodec/mjpegenc_common.c
@@ -325,7 +325,7 @@  end:
         MpegEncContext *s = avctx->priv_data;
         av_assert0(avctx->codec->priv_data_size == sizeof(MpegEncContext));
 
-        s->esc_pos = put_bits_count(pb) >> 3;
+        s->esc_pos = put_bytes_count(pb, 0);
         for(i=1; i<s->slice_context_count; i++)
             s->thread_context[i]->esc_pos = 0;
     }
@@ -343,10 +343,7 @@  void ff_mjpeg_escape_FF(PutBitContext *pb, int start)
         put_bits(pb, pad, (1<<pad)-1);
 
     flush_put_bits(pb);
-    size = put_bits_count(pb) - start * 8;
-
-    av_assert1((size&7) == 0);
-    size >>= 3;
+    size = put_bytes_output(pb) - start;
 
     ff_count=0;
     for(i=0; i<size && i<align; i++){
diff --git a/libavcodec/mlpenc.c b/libavcodec/mlpenc.c
index 40db76f350..8684056cab 100644
--- a/libavcodec/mlpenc.c
+++ b/libavcodec/mlpenc.c
@@ -1121,18 +1121,18 @@  static uint8_t *write_substrs(MLPEncodeContext *ctx, uint8_t *buf, int buf_size,
          * notice that we already are word-aligned here. */
         flush_put_bits(&pb);
 
-        parity   = ff_mlp_calculate_parity(buf, put_bits_count(&pb) >> 3) ^ 0xa9;
-        checksum = ff_mlp_checksum8       (buf, put_bits_count(&pb) >> 3);
+        parity   = ff_mlp_calculate_parity(buf, put_bytes_output(&pb)) ^ 0xa9;
+        checksum = ff_mlp_checksum8       (buf, put_bytes_output(&pb));
 
         put_bits(&pb, 8, parity  );
         put_bits(&pb, 8, checksum);
 
         flush_put_bits(&pb);
 
-        end += put_bits_count(&pb) >> 3;
+        end += put_bytes_output(&pb);
         substream_data_len[substr] = end;
 
-        buf += put_bits_count(&pb) >> 3;
+        buf += put_bytes_output(&pb);
     }
 
     ctx->major_cur_subblock_index += ctx->major_filter_state_subblock + 1;
diff --git a/libavcodec/mpeg12enc.c b/libavcodec/mpeg12enc.c
index a05c2db6cb..5676caef87 100644
--- a/libavcodec/mpeg12enc.c
+++ b/libavcodec/mpeg12enc.c
@@ -439,7 +439,7 @@  void ff_mpeg1_encode_picture_header(MpegEncContext *s, int picture_number)
              (s->picture_number - s->gop_picture_number) & 0x3ff);
     put_bits(&s->pb, 3, s->pict_type);
 
-    s->vbv_delay_ptr = s->pb.buf + put_bits_count(&s->pb) / 8;
+    s->vbv_delay_ptr = s->pb.buf + put_bytes_count(&s->pb, 0);
     put_bits(&s->pb, 16, 0xFFFF);               /* vbv_delay */
 
     // RAL: Forward f_code also needed for B-frames
diff --git a/libavcodec/mpeg4videoenc.c b/libavcodec/mpeg4videoenc.c
index a879d2fd18..2bf65305ad 100644
--- a/libavcodec/mpeg4videoenc.c
+++ b/libavcodec/mpeg4videoenc.c
@@ -1314,7 +1314,7 @@  static av_cold int encode_init(AVCodecContext *avctx)
 
 //            ff_mpeg4_stuffing(&s->pb); ?
         flush_put_bits(&s->pb);
-        s->avctx->extradata_size = (put_bits_count(&s->pb) + 7) >> 3;
+        s->avctx->extradata_size = put_bytes_output(&s->pb);
     }
     return 0;
 }
diff --git a/libavcodec/mpegaudioenc_template.c b/libavcodec/mpegaudioenc_template.c
index 12f7a098e6..8e6e20c358 100644
--- a/libavcodec/mpegaudioenc_template.c
+++ b/libavcodec/mpegaudioenc_template.c
@@ -736,9 +736,6 @@  static void encode_frame(MpegAudioContext *s,
     /* padding */
     for(i=0;i<padding;i++)
         put_bits(p, 1, 0);
-
-    /* flush */
-    flush_put_bits(p);
 }
 
 static int MPA_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
@@ -770,10 +767,13 @@  static int MPA_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
 
     encode_frame(s, bit_alloc, padding);
 
+    /* flush */
+    flush_put_bits(&s->pb);
+    avpkt->size = put_bytes_output(&s->pb);
+
     if (frame->pts != AV_NOPTS_VALUE)
         avpkt->pts = frame->pts - ff_samples_to_time_base(avctx, avctx->initial_padding);
 
-    avpkt->size = put_bits_count(&s->pb) / 8;
     *got_packet_ptr = 1;
     return 0;
 }
diff --git a/libavcodec/mpegvideo_enc.c b/libavcodec/mpegvideo_enc.c
index 8731b0ad31..81f832fe4b 100644
--- a/libavcodec/mpegvideo_enc.c
+++ b/libavcodec/mpegvideo_enc.c
@@ -2889,12 +2889,12 @@  static void update_mb_info(MpegEncContext *s, int startcode)
 {
     if (!s->mb_info)
         return;
-    if (put_bits_count(&s->pb) - s->prev_mb_info*8 >= s->mb_info*8) {
+    if (put_bytes_count(&s->pb, 0) - s->prev_mb_info >= s->mb_info) {
         s->mb_info_size += 12;
         s->prev_mb_info = s->last_mb_info;
     }
     if (startcode) {
-        s->prev_mb_info = put_bits_count(&s->pb)/8;
+        s->prev_mb_info = put_bytes_count(&s->pb, 0);
         /* This might have incremented mb_info_size above, and we return without
          * actually writing any info into that slot yet. But in that case,
          * this will be called again at the start of the after writing the
@@ -2902,7 +2902,7 @@  static void update_mb_info(MpegEncContext *s, int startcode)
         return;
     }
 
-    s->last_mb_info = put_bits_count(&s->pb)/8;
+    s->last_mb_info = put_bytes_count(&s->pb, 0);
     if (!s->mb_info_size)
         s->mb_info_size += 12;
     write_mb_info(s);
@@ -3057,7 +3057,8 @@  static int encode_thread(AVCodecContext *c, void *arg){
             if(s->rtp_mode){
                 int current_packet_size, is_gob_start;
 
-                current_packet_size= ((put_bits_count(&s->pb)+7)>>3) - (s->ptr_lastgob - s->pb.buf);
+                current_packet_size = put_bytes_count(&s->pb, 1)
+                                      - (s->ptr_lastgob - s->pb.buf);
 
                 is_gob_start = s->rtp_payload_size &&
                                current_packet_size >= s->rtp_payload_size &&
@@ -3094,7 +3095,7 @@  static int encode_thread(AVCodecContext *c, void *arg){
                     current_packet_size= put_bits_ptr(&s->pb) - s->ptr_lastgob;
 
                     if (s->error_rate && s->resync_mb_x + s->resync_mb_y > 0) {
-                        int r= put_bits_count(&s->pb)/8 + s->picture_number + 16 + s->mb_x + s->mb_y;
+                        int r = put_bytes_count(&s->pb, 0) + s->picture_number + 16 + s->mb_x + s->mb_y;
                         int d = 100 / s->error_rate;
                         if(r % d == 0){
                             current_packet_size=0;
diff --git a/libavcodec/proresenc_anatoliy.c b/libavcodec/proresenc_anatoliy.c
index c34e55f873..588df5984c 100644
--- a/libavcodec/proresenc_anatoliy.c
+++ b/libavcodec/proresenc_anatoliy.c
@@ -460,7 +460,7 @@  static av_always_inline int encode_alpha_slice_data(AVCodecContext *avctx, int8_
     if (run)
         put_alpha_run(&pb, run);
     flush_put_bits(&pb);
-    *a_data_size = put_bits_count(&pb) >> 3;
+    *a_data_size = put_bytes_output(&pb);
 
     if (put_bits_left(&pb) < 0) {
         av_log(avctx, AV_LOG_ERROR,
diff --git a/libavcodec/rpzaenc.c b/libavcodec/rpzaenc.c
index baf067c205..eeb82716c9 100644
--- a/libavcodec/rpzaenc.c
+++ b/libavcodec/rpzaenc.c
@@ -802,7 +802,7 @@  static int rpza_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
 
     flush_put_bits(&s->pb);
 
-    av_shrink_packet(pkt, put_bits_count(&s->pb) >> 3);
+    av_shrink_packet(pkt, put_bytes_output(&s->pb));
     buf = pkt->data;
 
     // write header opcode
diff --git a/libavcodec/sbcenc.c b/libavcodec/sbcenc.c
index 47a136e4e5..33de8edab4 100644
--- a/libavcodec/sbcenc.c
+++ b/libavcodec/sbcenc.c
@@ -188,7 +188,7 @@  static size_t sbc_pack_frame(AVPacket *avpkt, struct sbc_frame *frame,
 
     flush_put_bits(&pb);
 
-    return (put_bits_count(&pb) + 7) / 8;
+    return put_bytes_output(&pb);
 }
 
 static int sbc_encode_init(AVCodecContext *avctx)
diff --git a/libavcodec/sonic.c b/libavcodec/sonic.c
index 8339799dd3..c2a0a9953e 100644
--- a/libavcodec/sonic.c
+++ b/libavcodec/sonic.c
@@ -687,7 +687,7 @@  static av_cold int sonic_encode_init(AVCodecContext *avctx)
     put_bits(&pb, 1, 0); // XXX FIXME: no custom tap quant table
 
     flush_put_bits(&pb);
-    avctx->extradata_size = put_bits_count(&pb)/8;
+    avctx->extradata_size = put_bytes_output(&pb);
 
     av_log(avctx, AV_LOG_INFO, "Sonic: ver: %d.%d ls: %d dr: %d taps: %d block: %d frame: %d downsamp: %d\n",
         s->version, s->minor_version, s->lossless, s->decorrelation, s->num_taps, s->block_align, s->frame_size, s->downsampling);
diff --git a/libavcodec/svq1enc.c b/libavcodec/svq1enc.c
index 53f0b40a21..2b74f009e0 100644
--- a/libavcodec/svq1enc.c
+++ b/libavcodec/svq1enc.c
@@ -652,7 +652,7 @@  FF_ENABLE_DEPRECATION_WARNINGS
 
     flush_put_bits(&s->pb);
 
-    pkt->size = put_bits_count(&s->pb) / 8;
+    pkt->size = put_bytes_output(&s->pb);
     if (s->pict_type == AV_PICTURE_TYPE_I)
         pkt->flags |= AV_PKT_FLAG_KEY;
     *got_packet = 1;
diff --git a/libavcodec/tests/cabac.c b/libavcodec/tests/cabac.c
index a8bd131c95..b84a96ce81 100644
--- a/libavcodec/tests/cabac.c
+++ b/libavcodec/tests/cabac.c
@@ -120,7 +120,7 @@  static int put_cabac_terminate(CABACTestContext *c, int bit)
         flush_put_bits(&c->pb); //FIXME FIXME FIXME XXX wrong
     }
 
-    return (put_bits_count(&c->pb)+7)>>3;
+    return put_bytes_count(&c->pb, 1);
 }
 
 /**
diff --git a/libavcodec/ttaenc.c b/libavcodec/ttaenc.c
index ac8a432873..bb4734e013 100644
--- a/libavcodec/ttaenc.c
+++ b/libavcodec/ttaenc.c
@@ -182,7 +182,7 @@  pkt_alloc:
     }
 
     flush_put_bits(&pb);
-    out_bytes = put_bits_count(&pb) >> 3;
+    out_bytes = put_bytes_output(&pb);
     put_bits32(&pb, av_crc(s->crc_table, UINT32_MAX, avpkt->data, out_bytes) ^ UINT32_MAX);
     flush_put_bits(&pb);
 
diff --git a/libavcodec/vorbisenc.c b/libavcodec/vorbisenc.c
index 080017e2bf..1b0ab3cadb 100644
--- a/libavcodec/vorbisenc.c
+++ b/libavcodec/vorbisenc.c
@@ -637,7 +637,7 @@  static int put_main_header(vorbis_enc_context *venc, uint8_t **out)
     put_bits(&pb,  1, 1); // framing
 
     flush_put_bits(&pb);
-    hlens[0] = put_bits_count(&pb) >> 3;
+    hlens[0] = put_bytes_output(&pb);
     buffer_len -= hlens[0];
     p += hlens[0];
 
@@ -651,7 +651,7 @@  static int put_main_header(vorbis_enc_context *venc, uint8_t **out)
     put_bits(&pb,  1, 1); // framing
 
     flush_put_bits(&pb);
-    hlens[1] = put_bits_count(&pb) >> 3;
+    hlens[1] = put_bytes_output(&pb);
     buffer_len -= hlens[1];
     p += hlens[1];
 
@@ -725,7 +725,7 @@  static int put_main_header(vorbis_enc_context *venc, uint8_t **out)
     put_bits(&pb, 1, 1); // framing
 
     flush_put_bits(&pb);
-    hlens[2] = put_bits_count(&pb) >> 3;
+    hlens[2] = put_bytes_output(&pb);
 
     len = hlens[0] + hlens[1] + hlens[2];
     p = *out = av_mallocz(64 + len + len/255);
@@ -1180,7 +1180,7 @@  static int vorbis_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
     }
 
     flush_put_bits(&pb);
-    avpkt->size = put_bits_count(&pb) >> 3;
+    avpkt->size = put_bytes_output(&pb);
 
     ff_af_queue_remove(&venc->afq, frame_size, &avpkt->pts, &avpkt->duration);
 
diff --git a/libavcodec/wavpackenc.c b/libavcodec/wavpackenc.c
index 0a798438bc..0c82633a72 100644
--- a/libavcodec/wavpackenc.c
+++ b/libavcodec/wavpackenc.c
@@ -2777,7 +2777,7 @@  static int wavpack_encode_block(WavPackEncodeContext *s,
     }
     encode_flush(s);
     flush_put_bits(&s->pb);
-    data_size = put_bits_count(&s->pb) >> 3;
+    data_size = put_bytes_output(&s->pb);
     bytestream2_put_le24(&pb, (data_size + 1) >> 1);
     bytestream2_skip_p(&pb, data_size);
     if (data_size & 1)
@@ -2791,7 +2791,7 @@  static int wavpack_encode_block(WavPackEncodeContext *s,
         else
             pack_int32(s, s->orig_l, s->orig_r, nb_samples);
         flush_put_bits(&s->pb);
-        data_size = put_bits_count(&s->pb) >> 3;
+        data_size = put_bytes_output(&s->pb);
         bytestream2_put_le24(&pb, (data_size + 5) >> 1);
         bytestream2_put_le32(&pb, s->crc_x);
         bytestream2_skip_p(&pb, data_size);
diff --git a/libavcodec/wmaenc.c b/libavcodec/wmaenc.c
index 6a7e23d016..4ad37cc70b 100644
--- a/libavcodec/wmaenc.c
+++ b/libavcodec/wmaenc.c
@@ -408,7 +408,7 @@  static int encode_superframe(AVCodecContext *avctx, AVPacket *avpkt,
         return AVERROR(EINVAL);
     }
     av_assert0((put_bits_count(&s->pb) & 7) == 0);
-    i= avctx->block_align - (put_bits_count(&s->pb)+7)/8;
+    i = avctx->block_align - put_bytes_count(&s->pb, 0);
     av_assert0(i>=0);
     while(i--)
         put_bits(&s->pb, 8, 'N');
diff --git a/libavcodec/xsubenc.c b/libavcodec/xsubenc.c
index 36c5ab7223..9139159c98 100644
--- a/libavcodec/xsubenc.c
+++ b/libavcodec/xsubenc.c
@@ -197,7 +197,7 @@  FF_ENABLE_DEPRECATION_WARNINGS
                         h->rects[0]->linesize[0] * 2,
                         h->rects[0]->w, (h->rects[0]->h + 1) >> 1))
         return AVERROR_BUFFER_TOO_SMALL;
-    bytestream_put_le16(&rlelenptr, put_bits_count(&pb) >> 3); // Length of first field
+    bytestream_put_le16(&rlelenptr, put_bytes_count(&pb, 0)); // Length of first field
 
     if (xsub_encode_rle(&pb, h->rects[0]->data[0] + h->rects[0]->linesize[0],
                         h->rects[0]->linesize[0] * 2,
@@ -211,7 +211,7 @@  FF_ENABLE_DEPRECATION_WARNINGS
 
     flush_put_bits(&pb);
 
-    return hdr - buf + put_bits_count(&pb)/8;
+    return hdr - buf + put_bytes_output(&pb);
 }
 
 static av_cold int xsub_encoder_init(AVCodecContext *avctx)
diff --git a/libavfilter/vf_signature.c b/libavfilter/vf_signature.c
index 32a6405e14..bf5973b867 100644
--- a/libavfilter/vf_signature.c
+++ b/libavfilter/vf_signature.c
@@ -561,7 +561,7 @@  static int binary_export(AVFilterContext *ctx, StreamContext *sc, const char* fi
     }
 
     flush_put_bits(&buf);
-    fwrite(buffer, 1, put_bits_count(&buf)/8, f);
+    fwrite(buffer, 1, put_bytes_output(&buf), f);
     fclose(f);
     av_freep(&buffer);
     return 0;
diff --git a/libavformat/latmenc.c b/libavformat/latmenc.c
index 91e96e90b0..5488361bde 100644
--- a/libavformat/latmenc.c
+++ b/libavformat/latmenc.c
@@ -225,7 +225,7 @@  static int latm_write_packet(AVFormatContext *s, AVPacket *pkt)
 
     flush_put_bits(&bs);
 
-    len = put_bits_count(&bs) >> 3;
+    len = put_bytes_output(&bs);
 
     if (len > 0x1fff)
         goto too_large;
diff --git a/libavformat/movenc.c b/libavformat/movenc.c
index 6790fe6c45..66ac34dfb6 100644
--- a/libavformat/movenc.c
+++ b/libavformat/movenc.c
@@ -579,7 +579,7 @@  static int mov_write_eac3_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *tra
         }
     }
     flush_put_bits(&pbc);
-    size = put_bits_count(&pbc) >> 3;
+    size = put_bytes_output(&pbc);
 
     avio_wb32(pb, size + 8);
     ffio_wfourcc(pb, "dec3");