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.
Unified · Split
| @@ -1044,20 +1044,117 @@ static int apply_window_and_mdct(vorbis_enc_context *venc, · 1043 unmodified lines | ||
| 1044 | 1044 | return 1; |
| 1045 | 1045 | } |
| 1046 | 1046 | |
| 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 | + | |
| 1047 | 1111 | static int vorbis_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, |
| 1048 | 1112 | const AVFrame *frame, int *got_packet_ptr) |
| 1049 | 1113 | { |
| 1050 | 1114 | 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); | |
| 1053 | 1118 | vorbis_enc_mode *mode; |
| 1054 | 1119 | vorbis_enc_mapping *mapping; |
| 1055 | 1120 | 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); | |
| 1057 | 1155 | |
| 1058 | 1156 | if (!apply_window_and_mdct(venc, audio, samples)) |
| 1059 | 1157 | return 0; |
| 1060 | − samples = 1 << (venc->log2_blocksize[0] - 1); | |
| 1061 | 1158 | |
| 1062 | 1159 | if ((ret = ff_alloc_packet2(avctx, avpkt, 8192, 0)) < 0) |
| 1063 | 1160 | return ret; |
| @@ -1116,16 +1213,11 @@ static int vorbis_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, · 52 unmodified lines | ||
| 1116 | 1213 | flush_put_bits(&pb); |
| 1117 | 1214 | avpkt->size = put_bits_count(&pb) >> 3; |
| 1118 | 1215 | |
| 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); | |
| 1128 | 1219 | |
| 1220 | + ff_af_queue_remove(&venc->afq, frame_size, &avpkt->pts, &avpkt->duration); | |
| 1129 | 1221 | *got_packet_ptr = 1; |
| 1130 | 1222 | return 0; |
| 1131 | 1223 | } |
| @@ -1217,7 +1309,7 @@ static av_cold int vorbis_encode_init(AVCodecContext *avctx) · 85 unmodified lines | ||
| 1217 | 1309 | goto error; |
| 1218 | 1310 | avctx->extradata_size = ret; |
| 1219 | 1311 | |
| 1220 | − avctx->frame_size = 1 << (venc->log2_blocksize[0] - 1); | |
| 1312 | + avctx->frame_size = 64; | |
| 1221 | 1313 | |
| 1222 | 1314 | ff_af_queue_init(avctx, &venc->afq); |
| 1223 | 1315 | |