History

avcodec/vorbisenc: Use a bufqueue in encoding with smaller lengths

Switching the vorbis encoder to use a buffer queue for input frames allows saving lookahead samples more easily and safely for psychoacoustic systems, requiring less pointer arithmetic in the case of transient windows.

Tyler Jones committed 9 years ago

Commit 29c13fe1 parent 25260b51 file · +106 −14Browse files

Unified · Split

libavcodec/vorbisenc.c+106−14
@@ -1044,20 +1044,117 @@ static int apply_window_and_mdct(vorbis_enc_context *venc, · 1043 unmodified lines
10441044 return 1;
10451045 }
10461046
1047+/* Used for padding the last encoded packet */
1048+static AVFrame *spawn_empty_frame(AVCodecContext *avctx, int channels)
1049+{
1050+ AVFrame *f = av_frame_alloc();
1051+
1052+ if (!f)
1053+ return NULL;
1054+
1055+ f->format = avctx->sample_fmt;
1056+ f->nb_samples = avctx->frame_size;
1057+ f->channel_layout = avctx->channel_layout;
1058+
1059+ if (av_frame_get_buffer(f, 4)) {
1060+ av_frame_free(&f);
1061+ return NULL;
1062+ }
1063+
1064+ for (int ch = 0; ch < channels; ch++) {
1065+ size_t bps = av_get_bytes_per_sample(f->format);
1066+ memset(f->extended_data[ch], 0, bps * f->nb_samples);
1067+ }
1068+ return f;
1069+}
1070+
1071+static float **alloc_audio_arrays(int channels, int frame_size)
1072+{
1073+ float **audio = av_mallocz_array(channels, sizeof(float *));
1074+ if (!audio)
1075+ return NULL;
1076+
1077+ for (int ch = 0; ch < channels; ch++) {
1078+ audio[ch] = av_mallocz_array(frame_size, sizeof(float));
1079+ if (!audio[ch]) {
1080+ // alloc has failed, free everything allocated thus far
1081+ for (ch--; ch >= 0; ch--)
1082+ av_free(audio[ch]);
1083+ av_free(audio);
1084+ return NULL;
1085+ }
1086+ }
1087+
1088+ return audio;
1089+}
1090+
1091+/* Concatenate audio frames into an appropriately sized array of samples */
1092+static void move_audio(vorbis_enc_context *venc, float **audio, int *samples, int sf_size)
1093+{
1094+ AVFrame *cur = NULL;
1095+ int frame_size = 1 << (venc->log2_blocksize[1] - 1);
1096+ int subframes = frame_size / sf_size;
1097+
1098+ for (int sf = 0; sf < subframes; sf++) {
1099+ cur = ff_bufqueue_get(&venc->bufqueue);
1100+ *samples += cur->nb_samples;
1101+
1102+ for (int ch = 0; ch < venc->channels; ch++) {
1103+ const float *input = (float *) cur->extended_data[ch];
1104+ const size_t len = cur->nb_samples * sizeof(float);
1105+ memcpy(&audio[ch][sf*sf_size], input, len);
1106+ }
1107+ av_frame_free(&cur);
1108+ }
1109+}
1110+
10471111 static int vorbis_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
10481112 const AVFrame *frame, int *got_packet_ptr)
10491113 {
10501114 vorbis_enc_context *venc = avctx->priv_data;
1051− float **audio = frame ? (float **)frame->extended_data : NULL;
1052− int samples = frame ? frame->nb_samples : 0;
1115+ float **audio = NULL;
1116+ int i, ret, need_more;
1117+ int samples = 0, frame_size = 1 << (venc->log2_blocksize[1] - 1);
10531118 vorbis_enc_mode *mode;
10541119 vorbis_enc_mapping *mapping;
10551120 PutBitContext pb;
1056− int i, ret;
1121+
1122+ if (frame) {
1123+ if ((ret = ff_af_queue_add(&venc->afq, frame)) < 0)
1124+ return ret;
1125+ ff_bufqueue_add(avctx, &venc->bufqueue, av_frame_clone(frame));
1126+ } else
1127+ if (!venc->afq.remaining_samples)
1128+ return 0;
1129+
1130+ need_more = venc->bufqueue.available * avctx->frame_size < frame_size;
1131+ need_more = frame && need_more;
1132+ if (need_more)
1133+ return 0;
1134+
1135+ audio = alloc_audio_arrays(venc->channels, frame_size);
1136+ if (!audio)
1137+ return AVERROR(ENOMEM);
1138+
1139+ /* Pad the bufqueue with empty frames for encoding the last packet. */
1140+ if (!frame) {
1141+ if (venc->bufqueue.available * avctx->frame_size < frame_size) {
1142+ int frames_needed = (frame_size/avctx->frame_size) - venc->bufqueue.available;
1143+
1144+ for (int i = 0; i < frames_needed; i++) {
1145+ AVFrame *empty = spawn_empty_frame(avctx, venc->channels);
1146+ if (!empty)
1147+ return AVERROR(ENOMEM);
1148+
1149+ ff_bufqueue_add(avctx, &venc->bufqueue, empty);
1150+ }
1151+ }
1152+ }
1153+
1154+ move_audio(venc, audio, &samples, avctx->frame_size);
10571155
10581156 if (!apply_window_and_mdct(venc, audio, samples))
10591157 return 0;
1060− samples = 1 << (venc->log2_blocksize[0] - 1);
10611158
10621159 if ((ret = ff_alloc_packet2(avctx, avpkt, 8192, 0)) < 0)
10631160 return ret;
@@ -1116,16 +1213,11 @@ static int vorbis_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, · 52 unmodified lines
11161213 flush_put_bits(&pb);
11171214 avpkt->size = put_bits_count(&pb) >> 3;
11181215
1119− avpkt->duration = ff_samples_to_time_base(avctx, avctx->frame_size);
1120− if (frame) {
1121− if (frame->pts != AV_NOPTS_VALUE)
1122− avpkt->pts = ff_samples_to_time_base(avctx, frame->pts);
1123− } else {
1124− avpkt->pts = venc->next_pts;
1125− }
1126− if (avpkt->pts != AV_NOPTS_VALUE)
1127− venc->next_pts = avpkt->pts + avpkt->duration;
1216+ for (int ch = 0; ch < venc->channels; ch++)
1217+ av_free(audio[ch]);
1218+ av_free(audio);
11281219
1220+ ff_af_queue_remove(&venc->afq, frame_size, &avpkt->pts, &avpkt->duration);
11291221 *got_packet_ptr = 1;
11301222 return 0;
11311223 }
@@ -1217,7 +1309,7 @@ static av_cold int vorbis_encode_init(AVCodecContext *avctx) · 85 unmodified lines
12171309 goto error;
12181310 avctx->extradata_size = ret;
12191311
1220− avctx->frame_size = 1 << (venc->log2_blocksize[0] - 1);
1312+ avctx->frame_size = 64;
12211313
12221314 ff_af_queue_init(avctx, &venc->afq);
12231315

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