History

avcodec/mpegaudioenc: Avoid intermediate buffer

We know the final size before encoding, so we can switch to ff_get_encode_buffer() which avoids an implicit memcpy().

Andreas Rheinhardt committed 1 year ago

Commit 87f3e201 parent 6f7ebef1 file · +7 −4Browse files

Unified · Split

libavcodec/mpegaudioenc.c+7−4
@@ -508,7 +508,7 @@ static void psycho_acoustic_model(MpegAudioContext *s, short smr[SBLIMIT]) · 507 unmodified lines
508508 /* Try to maximize the smr while using a number of bits inferior to
509509 the frame size. I tried to make the code simpler, faster and
510510 smaller than other encoders :-) */
511−static void compute_bit_allocation(MpegAudioContext *s,
511+static unsigned compute_bit_allocation(MpegAudioContext *s,
512512 short smr1[MPA_MAX_CHANNELS][SBLIMIT],
513513 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT],
514514 int *padding)
@@ -598,6 +598,7 @@ static void compute_bit_allocation(MpegAudioContext *s, · 83 unmodified lines
598598 }
599599 *padding = max_frame_size - current_frame_size;
600600 av_assert0(*padding >= 0);
601+ return max_frame_size / 8U;
601602 }
602603
603604 /// Quantization & write sub band samples
@@ -740,6 +741,8 @@ static void encode_frame(MpegAudioContext *s, · 136 unmodified lines
740741 encode_subbands(s, p, bit_alloc, 0);
741742 #endif
742743
744+ av_assert1(put_bits_left(p) == padding);
745+
743746 /* padding */
744747 for(i=0;i<padding;i++)
745748 put_bits(p, 1, 0);
@@ -765,9 +768,10 @@ static int MPA_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, · 19 unmodified lines
765768 for(i=0;i<s->nb_channels;i++) {
766769 psycho_acoustic_model(s, smr[i]);
767770 }
768− compute_bit_allocation(s, smr, bit_alloc, &padding);
771+ unsigned frame_size = compute_bit_allocation(s, smr, bit_alloc, &padding);
769772
770− if ((ret = ff_alloc_packet(avctx, avpkt, MPA_MAX_CODED_FRAME_SIZE)) < 0)
773+ ret = ff_get_encode_buffer(avctx, avpkt, frame_size, 0);
774+ if (ret < 0)
771775 return ret;
772776
773777 init_put_bits(&s->pb, avpkt->data, avpkt->size);
@@ -776,7 +780,6 @@ static int MPA_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, · 2 unmodified lines
776780
777781 /* flush */
778782 flush_put_bits(&s->pb);
779− avpkt->size = put_bytes_output(&s->pb);
780783
781784 if (frame->pts != AV_NOPTS_VALUE)
782785 avpkt->pts = frame->pts - ff_samples_to_time_base(avctx, avctx->initial_padding);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t