We know the final size before encoding, so we can switch to ff_get_encode_buffer() which avoids an implicit memcpy().
Showing the changes to libavcodec (1 of 1 file). Show all
Unified · Split
| @@ -508,7 +508,7 @@ static void psycho_acoustic_model(MpegAudioContext *s, short smr[SBLIMIT]) · 507 unmodified lines | |||
| 508 | /* Try to maximize the smr while using a number of bits inferior to | 508 | /* Try to maximize the smr while using a number of bits inferior to |
| 509 | the frame size. I tried to make the code simpler, faster and | 509 | the frame size. I tried to make the code simpler, faster and |
| 510 | smaller than other encoders :-) */ | 510 | smaller than other encoders :-) */ |
| 511 | static void compute_bit_allocation(MpegAudioContext *s, | 511 | static unsigned compute_bit_allocation(MpegAudioContext *s, |
| 512 | short smr1[MPA_MAX_CHANNELS][SBLIMIT], | 512 | short smr1[MPA_MAX_CHANNELS][SBLIMIT], |
| 513 | unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT], | 513 | unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT], |
| 514 | int *padding) | 514 | int *padding) |
| @@ -598,6 +598,7 @@ static void compute_bit_allocation(MpegAudioContext *s, · 83 unmodified lines | |||
| 598 | } | 598 | } |
| 599 | *padding = max_frame_size - current_frame_size; | 599 | *padding = max_frame_size - current_frame_size; |
| 600 | av_assert0(*padding >= 0); | 600 | av_assert0(*padding >= 0); |
| 601 | return max_frame_size / 8U; | ||
| 601 | } | 602 | } |
| 602 | 603 | ||
| 603 | /// Quantization & write sub band samples | 604 | /// Quantization & write sub band samples |
| @@ -740,6 +741,8 @@ static void encode_frame(MpegAudioContext *s, · 136 unmodified lines | |||
| 740 | encode_subbands(s, p, bit_alloc, 0); | 741 | encode_subbands(s, p, bit_alloc, 0); |
| 741 | #endif | 742 | #endif |
| 742 | 743 | ||
| 744 | av_assert1(put_bits_left(p) == padding); | ||
| 745 | |||
| 743 | /* padding */ | 746 | /* padding */ |
| 744 | for(i=0;i<padding;i++) | 747 | for(i=0;i<padding;i++) |
| 745 | put_bits(p, 1, 0); | 748 | put_bits(p, 1, 0); |
| @@ -765,9 +768,10 @@ static int MPA_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, · 19 unmodified lines | |||
| 765 | for(i=0;i<s->nb_channels;i++) { | 768 | for(i=0;i<s->nb_channels;i++) { |
| 766 | psycho_acoustic_model(s, smr[i]); | 769 | psycho_acoustic_model(s, smr[i]); |
| 767 | } | 770 | } |
| 768 | compute_bit_allocation(s, smr, bit_alloc, &padding); | 771 | unsigned frame_size = compute_bit_allocation(s, smr, bit_alloc, &padding); |
| 769 | 772 | ||
| 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) | ||
| 771 | return ret; | 775 | return ret; |
| 772 | 776 | ||
| 773 | init_put_bits(&s->pb, avpkt->data, avpkt->size); | 777 | 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 | |||
| 776 | 780 | ||
| 777 | /* flush */ | 781 | /* flush */ |
| 778 | flush_put_bits(&s->pb); | 782 | flush_put_bits(&s->pb); |
| 779 | avpkt->size = put_bytes_output(&s->pb); | ||
| 780 | 783 | ||
| 781 | if (frame->pts != AV_NOPTS_VALUE) | 784 | if (frame->pts != AV_NOPTS_VALUE) |
| 782 | avpkt->pts = frame->pts - ff_samples_to_time_base(avctx, avctx->initial_padding); | 785 | avpkt->pts = frame->pts - ff_samples_to_time_base(avctx, avctx->initial_padding); |