Lines Matching defs:venc
211 static int ready_residue(vorbis_enc_residue *rc, vorbis_enc_context *venc)
226 cb = &venc->codebooks[rc->books[i][j]];
250 static av_cold int dsp_init(AVCodecContext *avctx, vorbis_enc_context *venc)
254 venc->fdsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT);
255 if (!venc->fdsp)
259 venc->win[0] = ff_vorbis_vwin[venc->log2_blocksize[0] - 6];
260 venc->win[1] = ff_vorbis_vwin[venc->log2_blocksize[1] - 6];
262 if ((ret = ff_mdct_init(&venc->mdct[0], venc->log2_blocksize[0], 0, 1.0)) < 0)
264 if ((ret = ff_mdct_init(&venc->mdct[1], venc->log2_blocksize[1], 0, 1.0)) < 0)
270 static int create_vorbis_context(vorbis_enc_context *venc,
279 venc->channels = avctx->ch_layout.nb_channels;
280 venc->sample_rate = avctx->sample_rate;
281 venc->log2_blocksize[0] = venc->log2_blocksize[1] = 11;
283 venc->ncodebooks = FF_ARRAY_ELEMS(cvectors);
284 venc->codebooks = av_mallocz(sizeof(vorbis_enc_codebook) * venc->ncodebooks);
285 if (!venc->codebooks)
293 for (book = 0; book < venc->ncodebooks; book++) {
294 vorbis_enc_codebook *cb = &venc->codebooks[book];
325 venc->nfloors = 1;
326 venc->floors = av_mallocz(sizeof(vorbis_enc_floor) * venc->nfloors);
327 if (!venc->floors)
331 fc = &venc->floors[0];
360 fc->rangebits = venc->log2_blocksize[1] - 1;
382 venc->nresidues = 1;
383 venc->residues = av_mallocz(sizeof(vorbis_enc_residue) * venc->nresidues);
384 if (!venc->residues)
388 rc = &venc->residues[0];
413 if ((ret = ready_residue(rc, venc)) < 0)
416 venc->nmappings = 1;
417 venc->mappings = av_mallocz(sizeof(vorbis_enc_mapping) * venc->nmappings);
418 if (!venc->mappings)
422 mc = &venc->mappings[0];
424 mc->mux = av_malloc(sizeof(int) * venc->channels);
427 for (i = 0; i < venc->channels; i++)
437 mc->coupling_steps = venc->channels == 2 ? 1 : 0;
447 venc->nmodes = 2;
448 venc->modes = av_malloc(sizeof(vorbis_enc_mode) * venc->nmodes);
449 if (!venc->modes)
453 venc->modes[0].blockflag = 0;
454 venc->modes[0].mapping = 0;
456 venc->modes[1].blockflag = 1;
457 venc->modes[1].mapping = 0;
459 venc->have_saved = 0;
460 venc->saved = av_malloc_array(sizeof(float) * venc->channels, (1 << venc->log2_blocksize[1]) / 2);
461 venc->samples = av_malloc_array(sizeof(float) * venc->channels, (1 << venc->log2_blocksize[1]));
462 venc->floor = av_malloc_array(sizeof(float) * venc->channels, (1 << venc->log2_blocksize[1]) / 2);
463 venc->coeffs = av_malloc_array(sizeof(float) * venc->channels, (1 << venc->log2_blocksize[1]) / 2);
464 venc->scratch = av_malloc_array(sizeof(float) * venc->channels, (1 << venc->log2_blocksize[1]));
466 if (!venc->saved || !venc->samples || !venc->floor || !venc->coeffs || !venc->scratch)
469 if ((ret = dsp_init(avctx, venc)) < 0)
619 static int put_main_header(vorbis_enc_context *venc, uint8_t **out)
635 put_bits(&pb, 8, venc->channels);
636 put_bits32(&pb, venc->sample_rate);
640 put_bits(&pb, 4, venc->log2_blocksize[0]);
641 put_bits(&pb, 4, venc->log2_blocksize[1]);
670 put_bits(&pb, 8, venc->ncodebooks - 1);
671 for (i = 0; i < venc->ncodebooks; i++)
672 put_codebook_header(&pb, &venc->codebooks[i]);
679 put_bits(&pb, 6, venc->nfloors - 1);
680 for (i = 0; i < venc->nfloors; i++)
681 put_floor_header(&pb, &venc->floors[i]);
684 put_bits(&pb, 6, venc->nresidues - 1);
685 for (i = 0; i < venc->nresidues; i++)
686 put_residue_header(&pb, &venc->residues[i]);
689 put_bits(&pb, 6, venc->nmappings - 1);
690 for (i = 0; i < venc->nmappings; i++) {
691 vorbis_enc_mapping *mc = &venc->mappings[i];
703 put_bits(&pb, ilog(venc->channels - 1), mc->magnitude[j]);
704 put_bits(&pb, ilog(venc->channels - 1), mc->angle[j]);
711 for (j = 0; j < venc->channels; j++)
722 put_bits(&pb, 6, venc->nmodes - 1);
723 for (i = 0; i < venc->nmodes; i++) {
724 put_bits(&pb, 1, venc->modes[i].blockflag);
727 put_bits(&pb, 8, venc->modes[i].mapping);
766 static void floor_fit(vorbis_enc_context *venc, vorbis_enc_floor *fc,
778 tot_average /= venc->quality;
798 static int floor_encode(vorbis_enc_context *venc, vorbis_enc_floor *fc,
849 vorbis_enc_codebook * book = &venc->codebooks[c->masterbook];
856 maxval = venc->codebooks[c->books[l]].nentries;
876 if (put_codeword(pb, &venc->codebooks[book], entry))
910 static int residue_encode(vorbis_enc_context *venc, vorbis_enc_residue *rc,
919 int classwords = venc->codebooks[rc->classbook].ndimensions;
942 vorbis_enc_codebook * book = &venc->codebooks[rc->classbook];
954 vorbis_enc_codebook * book = &venc->codebooks[nbook];
1005 static int apply_window_and_mdct(vorbis_enc_context *venc)
1008 const float * win = venc->win[1];
1009 int window_len = 1 << (venc->log2_blocksize[1] - 1);
1010 float n = (float)(1 << venc->log2_blocksize[1]) / 4.0;
1011 AVFloatDSPContext *fdsp = venc->fdsp;
1013 for (channel = 0; channel < venc->channels; channel++) {
1014 float *offset = venc->samples + channel * window_len * 2;
1024 venc->mdct[1].mdct_calc(&venc->mdct[1], venc->coeffs + channel * window_len,
1025 venc->samples + channel * window_len * 2);
1057 static void move_audio(vorbis_enc_context *venc, int sf_size)
1060 int frame_size = 1 << (venc->log2_blocksize[1] - 1);
1065 if (venc->have_saved)
1066 for (ch = 0; ch < venc->channels; ch++)
1067 memcpy(venc->samples + 2 * ch * frame_size,
1068 venc->saved + ch * frame_size, sizeof(float) * frame_size);
1070 for (ch = 0; ch < venc->channels; ch++)
1071 memset(venc->samples + 2 * ch * frame_size, 0, sizeof(float) * frame_size);
1074 cur = ff_bufqueue_get(&venc->bufqueue);
1076 for (ch = 0; ch < venc->channels; ch++) {
1077 float *offset = venc->samples + 2 * ch * frame_size + frame_size;
1078 float *save = venc->saved + ch * frame_size;
1087 venc->have_saved = 1;
1088 memcpy(venc->scratch, venc->samples, 2 * venc->channels * frame_size);
1094 vorbis_enc_context *venc = avctx->priv_data;
1096 int frame_size = 1 << (venc->log2_blocksize[1] - 1);
1103 if ((ret = ff_af_queue_add(&venc->afq, frame)) < 0)
1108 ff_bufqueue_add(avctx, &venc->bufqueue, clone);
1110 if (!venc->afq.remaining_samples)
1113 need_more = venc->bufqueue.available * avctx->frame_size < frame_size;
1120 if (venc->bufqueue.available * avctx->frame_size < frame_size) {
1121 int frames_needed = (frame_size/avctx->frame_size) - venc->bufqueue.available;
1125 AVFrame *empty = spawn_empty_frame(avctx, venc->channels);
1129 ff_bufqueue_add(avctx, &venc->bufqueue, empty);
1134 move_audio(venc, avctx->frame_size);
1136 if (!apply_window_and_mdct(venc))
1146 put_bits(&pb, ilog(venc->nmodes - 1), 1); // Mode for current frame
1148 mode = &venc->modes[1];
1149 mapping = &venc->mappings[mode->mapping];
1155 for (i = 0; i < venc->channels; i++) {
1156 vorbis_enc_floor *fc = &venc->floors[mapping->floor[mapping->mux[i]]];
1158 floor_fit(venc, fc, &venc->coeffs[i * frame_size], posts, frame_size);
1159 if (floor_encode(venc, fc, &pb, posts, &venc->floor[i * frame_size], frame_size)) {
1165 for (i = 0; i < venc->channels * frame_size; i++)
1166 venc->coeffs[i] /= venc->floor[i];
1169 float *mag = venc->coeffs + mapping->magnitude[i] * frame_size;
1170 float *ang = venc->coeffs + mapping->angle[i] * frame_size;
1182 if (residue_encode(venc, &venc->residues[mapping->residue[mapping->mux[0]]],
1183 &pb, venc->coeffs, frame_size, venc->channels)) {
1191 ff_af_queue_remove(&venc->afq, frame_size, &avpkt->pts, &avpkt->duration);
1207 vorbis_enc_context *venc = avctx->priv_data;
1210 if (venc->codebooks)
1211 for (i = 0; i < venc->ncodebooks; i++) {
1212 av_freep(&venc->codebooks[i].lens);
1213 av_freep(&venc->codebooks[i].codewords);
1214 av_freep(&venc->codebooks[i].quantlist);
1215 av_freep(&venc->codebooks[i].dimensions);
1216 av_freep(&venc->codebooks[i].pow2);
1218 av_freep(&venc->codebooks);
1220 if (venc->floors)
1221 for (i = 0; i < venc->nfloors; i++) {
1223 if (venc->floors[i].classes)
1224 for (j = 0; j < venc->floors[i].nclasses; j++)
1225 av_freep(&venc->floors[i].classes[j].books);
1226 av_freep(&venc->floors[i].classes);
1227 av_freep(&venc->floors[i].partition_to_class);
1228 av_freep(&venc->floors[i].list);
1230 av_freep(&venc->floors);
1232 if (venc->residues)
1233 for (i = 0; i < venc->nresidues; i++) {
1234 av_freep(&venc->residues[i].books);
1235 av_freep(&venc->residues[i].maxes);
1237 av_freep(&venc->residues);
1239 if (venc->mappings)
1240 for (i = 0; i < venc->nmappings; i++) {
1241 av_freep(&venc->mappings[i].mux);
1242 av_freep(&venc->mappings[i].floor);
1243 av_freep(&venc->mappings[i].residue);
1244 av_freep(&venc->mappings[i].magnitude);
1245 av_freep(&venc->mappings[i].angle);
1247 av_freep(&venc->mappings);
1249 av_freep(&venc->modes);
1251 av_freep(&venc->saved);
1252 av_freep(&venc->samples);
1253 av_freep(&venc->floor);
1254 av_freep(&venc->coeffs);
1255 av_freep(&venc->scratch);
1256 av_freep(&venc->fdsp);
1258 ff_mdct_end(&venc->mdct[0]);
1259 ff_mdct_end(&venc->mdct[1]);
1260 ff_af_queue_close(&venc->afq);
1261 ff_bufqueue_discard_all(&venc->bufqueue);
1268 vorbis_enc_context *venc = avctx->priv_data;
1276 if ((ret = create_vorbis_context(venc, avctx)) < 0)
1281 venc->quality = avctx->global_quality / (float)FF_QP2LAMBDA;
1283 venc->quality = 8;
1284 venc->quality *= venc->quality;
1286 if ((ret = put_main_header(venc, (uint8_t**)&avctx->extradata)) < 0)
1291 avctx->initial_padding = 1 << (venc->log2_blocksize[1] - 1);
1293 ff_af_queue_init(avctx, &venc->afq);