Avoid intermediate bitcount for number of bytes in PutBitContext

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
This commit is contained in:
Andreas Rheinhardt 2021-03-25 12:52:56 +01:00 committed by Andreas Rheinhardt
parent 73fb1b8a9b
commit c81b8e04aa
34 changed files with 55 additions and 59 deletions

View File

@ -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;
}

View File

@ -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)

View File

@ -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,

View File

@ -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;

View File

@ -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);

View File

@ -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;

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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);

View File

@ -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);
}

View File

@ -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;
}

View File

@ -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;

View File

@ -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++) {

View File

@ -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++)

View File

@ -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++){

View File

@ -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;

View File

@ -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

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;

View File

@ -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,

View File

@ -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

View File

@ -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)

View File

@ -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);

View File

@ -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;

View File

@ -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);
}
/**

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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');

View File

@ -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)

View File

@ -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;

View File

@ -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;

View File

@ -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");