2 * copyright (c) 2006 Oded Shimon <ods15@ods15.dyndns.org>
4 * This file is part of Libav.
6 * Libav is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * Libav is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Libav; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * Native Vorbis encoder.
24 * @author Oded Shimon <ods15@ods15.dyndns.org>
33 #include "vorbis_enc_data.h"
35 #define BITSTREAM_WRITER_LE
53 } vorbis_enc_codebook
;
60 } vorbis_enc_floor_class
;
64 int *partition_to_class
;
66 vorbis_enc_floor_class
*classes
;
70 vorbis_floor1_entry
*list
;
102 int log2_blocksize
[2];
108 float *floor
; // also used for tmp values for mdct
109 float *coeffs
; // also used for residue after floor
113 vorbis_enc_codebook
*codebooks
;
116 vorbis_enc_floor
*floors
;
119 vorbis_enc_residue
*residues
;
122 vorbis_enc_mapping
*mappings
;
125 vorbis_enc_mode
*modes
;
128 } vorbis_enc_context
;
130 #define MAX_CHANNELS 2
131 #define MAX_CODEBOOK_DIM 8
133 #define MAX_FLOOR_CLASS_DIM 4
134 #define NUM_FLOOR_PARTITIONS 8
135 #define MAX_FLOOR_VALUES (MAX_FLOOR_CLASS_DIM*NUM_FLOOR_PARTITIONS+2)
137 #define RESIDUE_SIZE 1600
138 #define RESIDUE_PART_SIZE 32
139 #define NUM_RESIDUE_PARTITIONS (RESIDUE_SIZE/RESIDUE_PART_SIZE)
141 static inline int put_codeword(PutBitContext
*pb
, vorbis_enc_codebook
*cb
,
145 assert(entry
< cb
->nentries
);
146 assert(cb
->lens
[entry
]);
147 if (pb
->size_in_bits
- put_bits_count(pb
) < cb
->lens
[entry
])
148 return AVERROR(EINVAL
);
149 put_bits(pb
, cb
->lens
[entry
], cb
->codewords
[entry
]);
153 static int cb_lookup_vals(int lookup
, int dimensions
, int entries
)
156 return ff_vorbis_nth_root(entries
, dimensions
);
157 else if (lookup
== 2)
158 return dimensions
*entries
;
162 static int ready_codebook(vorbis_enc_codebook
*cb
)
166 ff_vorbis_len2vlc(cb
->lens
, cb
->codewords
, cb
->nentries
);
169 cb
->pow2
= cb
->dimensions
= NULL
;
171 int vals
= cb_lookup_vals(cb
->lookup
, cb
->ndimensions
, cb
->nentries
);
172 cb
->dimensions
= av_malloc(sizeof(float) * cb
->nentries
* cb
->ndimensions
);
173 cb
->pow2
= av_mallocz(sizeof(float) * cb
->nentries
);
174 if (!cb
->dimensions
|| !cb
->pow2
)
175 return AVERROR(ENOMEM
);
176 for (i
= 0; i
< cb
->nentries
; i
++) {
180 for (j
= 0; j
< cb
->ndimensions
; j
++) {
183 off
= (i
/ div
) % vals
; // lookup type 1
185 off
= i
* cb
->ndimensions
+ j
; // lookup type 2
187 cb
->dimensions
[i
* cb
->ndimensions
+ j
] = last
+ cb
->min
+ cb
->quantlist
[off
] * cb
->delta
;
189 last
= cb
->dimensions
[i
* cb
->ndimensions
+ j
];
190 cb
->pow2
[i
] += cb
->dimensions
[i
* cb
->ndimensions
+ j
] * cb
->dimensions
[i
* cb
->ndimensions
+ j
];
199 static int ready_residue(vorbis_enc_residue
*rc
, vorbis_enc_context
*venc
)
202 assert(rc
->type
== 2);
203 rc
->maxes
= av_mallocz(sizeof(float[2]) * rc
->classifications
);
205 return AVERROR(ENOMEM
);
206 for (i
= 0; i
< rc
->classifications
; i
++) {
208 vorbis_enc_codebook
* cb
;
209 for (j
= 0; j
< 8; j
++)
210 if (rc
->books
[i
][j
] != -1)
214 cb
= &venc
->codebooks
[rc
->books
[i
][j
]];
215 assert(cb
->ndimensions
>= 2);
218 for (j
= 0; j
< cb
->nentries
; j
++) {
222 a
= fabs(cb
->dimensions
[j
* cb
->ndimensions
]);
223 if (a
> rc
->maxes
[i
][0])
225 a
= fabs(cb
->dimensions
[j
* cb
->ndimensions
+ 1]);
226 if (a
> rc
->maxes
[i
][1])
231 for (i
= 0; i
< rc
->classifications
; i
++) {
232 rc
->maxes
[i
][0] += 0.8;
233 rc
->maxes
[i
][1] += 0.8;
238 static int create_vorbis_context(vorbis_enc_context
*venc
,
239 AVCodecContext
*avccontext
)
241 vorbis_enc_floor
*fc
;
242 vorbis_enc_residue
*rc
;
243 vorbis_enc_mapping
*mc
;
246 venc
->channels
= avccontext
->channels
;
247 venc
->sample_rate
= avccontext
->sample_rate
;
248 venc
->log2_blocksize
[0] = venc
->log2_blocksize
[1] = 11;
250 venc
->ncodebooks
= FF_ARRAY_ELEMS(cvectors
);
251 venc
->codebooks
= av_malloc(sizeof(vorbis_enc_codebook
) * venc
->ncodebooks
);
252 if (!venc
->codebooks
)
253 return AVERROR(ENOMEM
);
255 // codebook 0..14 - floor1 book, values 0..255
256 // codebook 15 residue masterbook
257 // codebook 16..29 residue
258 for (book
= 0; book
< venc
->ncodebooks
; book
++) {
259 vorbis_enc_codebook
*cb
= &venc
->codebooks
[book
];
261 cb
->ndimensions
= cvectors
[book
].dim
;
262 cb
->nentries
= cvectors
[book
].real_len
;
263 cb
->min
= cvectors
[book
].min
;
264 cb
->delta
= cvectors
[book
].delta
;
265 cb
->lookup
= cvectors
[book
].lookup
;
268 cb
->lens
= av_malloc(sizeof(uint8_t) * cb
->nentries
);
269 cb
->codewords
= av_malloc(sizeof(uint32_t) * cb
->nentries
);
270 if (!cb
->lens
|| !cb
->codewords
)
271 return AVERROR(ENOMEM
);
272 memcpy(cb
->lens
, cvectors
[book
].clens
, cvectors
[book
].len
);
273 memset(cb
->lens
+ cvectors
[book
].len
, 0, cb
->nentries
- cvectors
[book
].len
);
276 vals
= cb_lookup_vals(cb
->lookup
, cb
->ndimensions
, cb
->nentries
);
277 cb
->quantlist
= av_malloc(sizeof(int) * vals
);
279 return AVERROR(ENOMEM
);
280 for (i
= 0; i
< vals
; i
++)
281 cb
->quantlist
[i
] = cvectors
[book
].quant
[i
];
283 cb
->quantlist
= NULL
;
285 if ((ret
= ready_codebook(cb
)) < 0)
290 venc
->floors
= av_malloc(sizeof(vorbis_enc_floor
) * venc
->nfloors
);
292 return AVERROR(ENOMEM
);
295 fc
= &venc
->floors
[0];
296 fc
->partitions
= NUM_FLOOR_PARTITIONS
;
297 fc
->partition_to_class
= av_malloc(sizeof(int) * fc
->partitions
);
298 if (!fc
->partition_to_class
)
299 return AVERROR(ENOMEM
);
301 for (i
= 0; i
< fc
->partitions
; i
++) {
302 static const int a
[] = {0, 1, 2, 2, 3, 3, 4, 4};
303 fc
->partition_to_class
[i
] = a
[i
];
304 fc
->nclasses
= FFMAX(fc
->nclasses
, fc
->partition_to_class
[i
]);
307 fc
->classes
= av_malloc(sizeof(vorbis_enc_floor_class
) * fc
->nclasses
);
309 return AVERROR(ENOMEM
);
310 for (i
= 0; i
< fc
->nclasses
; i
++) {
311 vorbis_enc_floor_class
* c
= &fc
->classes
[i
];
313 c
->dim
= floor_classes
[i
].dim
;
314 c
->subclass
= floor_classes
[i
].subclass
;
315 c
->masterbook
= floor_classes
[i
].masterbook
;
316 books
= (1 << c
->subclass
);
317 c
->books
= av_malloc(sizeof(int) * books
);
319 return AVERROR(ENOMEM
);
320 for (j
= 0; j
< books
; j
++)
321 c
->books
[j
] = floor_classes
[i
].nbooks
[j
];
324 fc
->rangebits
= venc
->log2_blocksize
[0] - 1;
327 for (i
= 0; i
< fc
->partitions
; i
++)
328 fc
->values
+= fc
->classes
[fc
->partition_to_class
[i
]].dim
;
330 fc
->list
= av_malloc(sizeof(vorbis_floor1_entry
) * fc
->values
);
332 return AVERROR(ENOMEM
);
334 fc
->list
[1].x
= 1 << fc
->rangebits
;
335 for (i
= 2; i
< fc
->values
; i
++) {
336 static const int a
[] = {
337 93, 23,372, 6, 46,186,750, 14, 33, 65,
338 130,260,556, 3, 10, 18, 28, 39, 55, 79,
339 111,158,220,312,464,650,850
341 fc
->list
[i
].x
= a
[i
- 2];
343 if (ff_vorbis_ready_floor1_list(avccontext
, fc
->list
, fc
->values
))
347 venc
->residues
= av_malloc(sizeof(vorbis_enc_residue
) * venc
->nresidues
);
349 return AVERROR(ENOMEM
);
352 rc
= &venc
->residues
[0];
356 rc
->partition_size
= 32;
357 rc
->classifications
= 10;
359 rc
->books
= av_malloc(sizeof(*rc
->books
) * rc
->classifications
);
361 return AVERROR(ENOMEM
);
363 static const int8_t a
[10][8] = {
364 { -1, -1, -1, -1, -1, -1, -1, -1, },
365 { -1, -1, 16, -1, -1, -1, -1, -1, },
366 { -1, -1, 17, -1, -1, -1, -1, -1, },
367 { -1, -1, 18, -1, -1, -1, -1, -1, },
368 { -1, -1, 19, -1, -1, -1, -1, -1, },
369 { -1, -1, 20, -1, -1, -1, -1, -1, },
370 { -1, -1, 21, -1, -1, -1, -1, -1, },
371 { 22, 23, -1, -1, -1, -1, -1, -1, },
372 { 24, 25, -1, -1, -1, -1, -1, -1, },
373 { 26, 27, 28, -1, -1, -1, -1, -1, },
375 memcpy(rc
->books
, a
, sizeof a
);
377 if ((ret
= ready_residue(rc
, venc
)) < 0)
381 venc
->mappings
= av_malloc(sizeof(vorbis_enc_mapping
) * venc
->nmappings
);
383 return AVERROR(ENOMEM
);
386 mc
= &venc
->mappings
[0];
388 mc
->mux
= av_malloc(sizeof(int) * venc
->channels
);
390 return AVERROR(ENOMEM
);
391 for (i
= 0; i
< venc
->channels
; i
++)
393 mc
->floor
= av_malloc(sizeof(int) * mc
->submaps
);
394 mc
->residue
= av_malloc(sizeof(int) * mc
->submaps
);
395 if (!mc
->floor
|| !mc
->residue
)
396 return AVERROR(ENOMEM
);
397 for (i
= 0; i
< mc
->submaps
; i
++) {
401 mc
->coupling_steps
= venc
->channels
== 2 ? 1 : 0;
402 mc
->magnitude
= av_malloc(sizeof(int) * mc
->coupling_steps
);
403 mc
->angle
= av_malloc(sizeof(int) * mc
->coupling_steps
);
404 if (!mc
->magnitude
|| !mc
->angle
)
405 return AVERROR(ENOMEM
);
406 if (mc
->coupling_steps
) {
407 mc
->magnitude
[0] = 0;
412 venc
->modes
= av_malloc(sizeof(vorbis_enc_mode
) * venc
->nmodes
);
414 return AVERROR(ENOMEM
);
417 venc
->modes
[0].blockflag
= 0;
418 venc
->modes
[0].mapping
= 0;
420 venc
->have_saved
= 0;
421 venc
->saved
= av_malloc(sizeof(float) * venc
->channels
* (1 << venc
->log2_blocksize
[1]) / 2);
422 venc
->samples
= av_malloc(sizeof(float) * venc
->channels
* (1 << venc
->log2_blocksize
[1]));
423 venc
->floor
= av_malloc(sizeof(float) * venc
->channels
* (1 << venc
->log2_blocksize
[1]) / 2);
424 venc
->coeffs
= av_malloc(sizeof(float) * venc
->channels
* (1 << venc
->log2_blocksize
[1]) / 2);
425 if (!venc
->saved
|| !venc
->samples
|| !venc
->floor
|| !venc
->coeffs
)
426 return AVERROR(ENOMEM
);
428 venc
->win
[0] = ff_vorbis_vwin
[venc
->log2_blocksize
[0] - 6];
429 venc
->win
[1] = ff_vorbis_vwin
[venc
->log2_blocksize
[1] - 6];
431 if ((ret
= ff_mdct_init(&venc
->mdct
[0], venc
->log2_blocksize
[0], 0, 1.0)) < 0)
433 if ((ret
= ff_mdct_init(&venc
->mdct
[1], venc
->log2_blocksize
[1], 0, 1.0)) < 0)
439 static void put_float(PutBitContext
*pb
, float f
)
443 mant
= (int)ldexp(frexp(f
, &exp
), 20);
449 res
|= mant
| (exp
<< 21);
453 static void put_codebook_header(PutBitContext
*pb
, vorbis_enc_codebook
*cb
)
458 put_bits(pb
, 24, 0x564342); //magic
459 put_bits(pb
, 16, cb
->ndimensions
);
460 put_bits(pb
, 24, cb
->nentries
);
462 for (i
= 1; i
< cb
->nentries
; i
++)
463 if (cb
->lens
[i
] < cb
->lens
[i
-1])
465 if (i
== cb
->nentries
)
468 put_bits(pb
, 1, ordered
);
470 int len
= cb
->lens
[0];
471 put_bits(pb
, 5, len
- 1);
473 while (i
< cb
->nentries
) {
475 for (j
= 0; j
+i
< cb
->nentries
; j
++)
476 if (cb
->lens
[j
+i
] != len
)
478 put_bits(pb
, ilog(cb
->nentries
- i
), j
);
484 for (i
= 0; i
< cb
->nentries
; i
++)
487 if (i
!= cb
->nentries
)
489 put_bits(pb
, 1, sparse
);
491 for (i
= 0; i
< cb
->nentries
; i
++) {
493 put_bits(pb
, 1, !!cb
->lens
[i
]);
495 put_bits(pb
, 5, cb
->lens
[i
] - 1);
499 put_bits(pb
, 4, cb
->lookup
);
501 int tmp
= cb_lookup_vals(cb
->lookup
, cb
->ndimensions
, cb
->nentries
);
502 int bits
= ilog(cb
->quantlist
[0]);
504 for (i
= 1; i
< tmp
; i
++)
505 bits
= FFMAX(bits
, ilog(cb
->quantlist
[i
]));
507 put_float(pb
, cb
->min
);
508 put_float(pb
, cb
->delta
);
510 put_bits(pb
, 4, bits
- 1);
511 put_bits(pb
, 1, cb
->seq_p
);
513 for (i
= 0; i
< tmp
; i
++)
514 put_bits(pb
, bits
, cb
->quantlist
[i
]);
518 static void put_floor_header(PutBitContext
*pb
, vorbis_enc_floor
*fc
)
522 put_bits(pb
, 16, 1); // type, only floor1 is supported
524 put_bits(pb
, 5, fc
->partitions
);
526 for (i
= 0; i
< fc
->partitions
; i
++)
527 put_bits(pb
, 4, fc
->partition_to_class
[i
]);
529 for (i
= 0; i
< fc
->nclasses
; i
++) {
532 put_bits(pb
, 3, fc
->classes
[i
].dim
- 1);
533 put_bits(pb
, 2, fc
->classes
[i
].subclass
);
535 if (fc
->classes
[i
].subclass
)
536 put_bits(pb
, 8, fc
->classes
[i
].masterbook
);
538 books
= (1 << fc
->classes
[i
].subclass
);
540 for (j
= 0; j
< books
; j
++)
541 put_bits(pb
, 8, fc
->classes
[i
].books
[j
] + 1);
544 put_bits(pb
, 2, fc
->multiplier
- 1);
545 put_bits(pb
, 4, fc
->rangebits
);
547 for (i
= 2; i
< fc
->values
; i
++)
548 put_bits(pb
, fc
->rangebits
, fc
->list
[i
].x
);
551 static void put_residue_header(PutBitContext
*pb
, vorbis_enc_residue
*rc
)
555 put_bits(pb
, 16, rc
->type
);
557 put_bits(pb
, 24, rc
->begin
);
558 put_bits(pb
, 24, rc
->end
);
559 put_bits(pb
, 24, rc
->partition_size
- 1);
560 put_bits(pb
, 6, rc
->classifications
- 1);
561 put_bits(pb
, 8, rc
->classbook
);
563 for (i
= 0; i
< rc
->classifications
; i
++) {
565 for (j
= 0; j
< 8; j
++)
566 tmp
|= (rc
->books
[i
][j
] != -1) << j
;
568 put_bits(pb
, 3, tmp
& 7);
569 put_bits(pb
, 1, tmp
> 7);
572 put_bits(pb
, 5, tmp
>> 3);
575 for (i
= 0; i
< rc
->classifications
; i
++) {
577 for (j
= 0; j
< 8; j
++)
578 if (rc
->books
[i
][j
] != -1)
579 put_bits(pb
, 8, rc
->books
[i
][j
]);
583 static int put_main_header(vorbis_enc_context
*venc
, uint8_t **out
)
587 uint8_t buffer
[50000] = {0}, *p
= buffer
;
588 int buffer_len
= sizeof buffer
;
591 // identification header
592 init_put_bits(&pb
, p
, buffer_len
);
593 put_bits(&pb
, 8, 1); //magic
594 for (i
= 0; "vorbis"[i
]; i
++)
595 put_bits(&pb
, 8, "vorbis"[i
]);
596 put_bits32(&pb
, 0); // version
597 put_bits(&pb
, 8, venc
->channels
);
598 put_bits32(&pb
, venc
->sample_rate
);
599 put_bits32(&pb
, 0); // bitrate
600 put_bits32(&pb
, 0); // bitrate
601 put_bits32(&pb
, 0); // bitrate
602 put_bits(&pb
, 4, venc
->log2_blocksize
[0]);
603 put_bits(&pb
, 4, venc
->log2_blocksize
[1]);
604 put_bits(&pb
, 1, 1); // framing
607 hlens
[0] = put_bits_count(&pb
) >> 3;
608 buffer_len
-= hlens
[0];
612 init_put_bits(&pb
, p
, buffer_len
);
613 put_bits(&pb
, 8, 3); //magic
614 for (i
= 0; "vorbis"[i
]; i
++)
615 put_bits(&pb
, 8, "vorbis"[i
]);
616 put_bits32(&pb
, 0); // vendor length TODO
617 put_bits32(&pb
, 0); // amount of comments
618 put_bits(&pb
, 1, 1); // framing
621 hlens
[1] = put_bits_count(&pb
) >> 3;
622 buffer_len
-= hlens
[1];
626 init_put_bits(&pb
, p
, buffer_len
);
627 put_bits(&pb
, 8, 5); //magic
628 for (i
= 0; "vorbis"[i
]; i
++)
629 put_bits(&pb
, 8, "vorbis"[i
]);
632 put_bits(&pb
, 8, venc
->ncodebooks
- 1);
633 for (i
= 0; i
< venc
->ncodebooks
; i
++)
634 put_codebook_header(&pb
, &venc
->codebooks
[i
]);
636 // time domain, reserved, zero
638 put_bits(&pb
, 16, 0);
641 put_bits(&pb
, 6, venc
->nfloors
- 1);
642 for (i
= 0; i
< venc
->nfloors
; i
++)
643 put_floor_header(&pb
, &venc
->floors
[i
]);
646 put_bits(&pb
, 6, venc
->nresidues
- 1);
647 for (i
= 0; i
< venc
->nresidues
; i
++)
648 put_residue_header(&pb
, &venc
->residues
[i
]);
651 put_bits(&pb
, 6, venc
->nmappings
- 1);
652 for (i
= 0; i
< venc
->nmappings
; i
++) {
653 vorbis_enc_mapping
*mc
= &venc
->mappings
[i
];
655 put_bits(&pb
, 16, 0); // mapping type
657 put_bits(&pb
, 1, mc
->submaps
> 1);
659 put_bits(&pb
, 4, mc
->submaps
- 1);
661 put_bits(&pb
, 1, !!mc
->coupling_steps
);
662 if (mc
->coupling_steps
) {
663 put_bits(&pb
, 8, mc
->coupling_steps
- 1);
664 for (j
= 0; j
< mc
->coupling_steps
; j
++) {
665 put_bits(&pb
, ilog(venc
->channels
- 1), mc
->magnitude
[j
]);
666 put_bits(&pb
, ilog(venc
->channels
- 1), mc
->angle
[j
]);
670 put_bits(&pb
, 2, 0); // reserved
673 for (j
= 0; j
< venc
->channels
; j
++)
674 put_bits(&pb
, 4, mc
->mux
[j
]);
676 for (j
= 0; j
< mc
->submaps
; j
++) {
677 put_bits(&pb
, 8, 0); // reserved time configuration
678 put_bits(&pb
, 8, mc
->floor
[j
]);
679 put_bits(&pb
, 8, mc
->residue
[j
]);
684 put_bits(&pb
, 6, venc
->nmodes
- 1);
685 for (i
= 0; i
< venc
->nmodes
; i
++) {
686 put_bits(&pb
, 1, venc
->modes
[i
].blockflag
);
687 put_bits(&pb
, 16, 0); // reserved window type
688 put_bits(&pb
, 16, 0); // reserved transform type
689 put_bits(&pb
, 8, venc
->modes
[i
].mapping
);
692 put_bits(&pb
, 1, 1); // framing
695 hlens
[2] = put_bits_count(&pb
) >> 3;
697 len
= hlens
[0] + hlens
[1] + hlens
[2];
698 p
= *out
= av_mallocz(64 + len
+ len
/255);
700 return AVERROR(ENOMEM
);
703 p
+= av_xiphlacing(p
, hlens
[0]);
704 p
+= av_xiphlacing(p
, hlens
[1]);
706 for (i
= 0; i
< 3; i
++) {
707 memcpy(p
, buffer
+ buffer_len
, hlens
[i
]);
709 buffer_len
+= hlens
[i
];
715 static float get_floor_average(vorbis_enc_floor
* fc
, float *coeffs
, int i
)
717 int begin
= fc
->list
[fc
->list
[FFMAX(i
-1, 0)].sort
].x
;
718 int end
= fc
->list
[fc
->list
[FFMIN(i
+1, fc
->values
- 1)].sort
].x
;
722 for (j
= begin
; j
< end
; j
++)
723 average
+= fabs(coeffs
[j
]);
724 return average
/ (end
- begin
);
727 static void floor_fit(vorbis_enc_context
*venc
, vorbis_enc_floor
*fc
,
728 float *coeffs
, uint16_t *posts
, int samples
)
730 int range
= 255 / fc
->multiplier
+ 1;
732 float tot_average
= 0.;
733 float averages
[MAX_FLOOR_VALUES
];
734 for (i
= 0; i
< fc
->values
; i
++) {
735 averages
[i
] = get_floor_average(fc
, coeffs
, i
);
736 tot_average
+= averages
[i
];
738 tot_average
/= fc
->values
;
739 tot_average
/= venc
->quality
;
741 for (i
= 0; i
< fc
->values
; i
++) {
742 int position
= fc
->list
[fc
->list
[i
].sort
].x
;
743 float average
= averages
[i
];
746 average
= sqrt(tot_average
* average
) * pow(1.25f
, position
*0.005f
); // MAGIC!
747 for (j
= 0; j
< range
- 1; j
++)
748 if (ff_vorbis_floor1_inverse_db_table
[j
* fc
->multiplier
] > average
)
750 posts
[fc
->list
[i
].sort
] = j
;
754 static int render_point(int x0
, int y0
, int x1
, int y1
, int x
)
756 return y0
+ (x
- x0
) * (y1
- y0
) / (x1
- x0
);
759 static int floor_encode(vorbis_enc_context
*venc
, vorbis_enc_floor
*fc
,
760 PutBitContext
*pb
, uint16_t *posts
,
761 float *floor
, int samples
)
763 int range
= 255 / fc
->multiplier
+ 1;
764 int coded
[MAX_FLOOR_VALUES
]; // first 2 values are unused
767 if (pb
->size_in_bits
- put_bits_count(pb
) < 1 + 2 * ilog(range
- 1))
768 return AVERROR(EINVAL
);
769 put_bits(pb
, 1, 1); // non zero
770 put_bits(pb
, ilog(range
- 1), posts
[0]);
771 put_bits(pb
, ilog(range
- 1), posts
[1]);
772 coded
[0] = coded
[1] = 1;
774 for (i
= 2; i
< fc
->values
; i
++) {
775 int predicted
= render_point(fc
->list
[fc
->list
[i
].low
].x
,
776 posts
[fc
->list
[i
].low
],
777 fc
->list
[fc
->list
[i
].high
].x
,
778 posts
[fc
->list
[i
].high
],
780 int highroom
= range
- predicted
;
781 int lowroom
= predicted
;
782 int room
= FFMIN(highroom
, lowroom
);
783 if (predicted
== posts
[i
]) {
784 coded
[i
] = 0; // must be used later as flag!
787 if (!coded
[fc
->list
[i
].low
])
788 coded
[fc
->list
[i
].low
] = -1;
789 if (!coded
[fc
->list
[i
].high
])
790 coded
[fc
->list
[i
].high
] = -1;
792 if (posts
[i
] > predicted
) {
793 if (posts
[i
] - predicted
> room
)
794 coded
[i
] = posts
[i
] - predicted
+ lowroom
;
796 coded
[i
] = (posts
[i
] - predicted
) << 1;
798 if (predicted
- posts
[i
] > room
)
799 coded
[i
] = predicted
- posts
[i
] + highroom
- 1;
801 coded
[i
] = ((predicted
- posts
[i
]) << 1) - 1;
806 for (i
= 0; i
< fc
->partitions
; i
++) {
807 vorbis_enc_floor_class
* c
= &fc
->classes
[fc
->partition_to_class
[i
]];
808 int k
, cval
= 0, csub
= 1<<c
->subclass
;
810 vorbis_enc_codebook
* book
= &venc
->codebooks
[c
->masterbook
];
812 for (k
= 0; k
< c
->dim
; k
++) {
814 for (l
= 0; l
< csub
; l
++) {
816 if (c
->books
[l
] != -1)
817 maxval
= venc
->codebooks
[c
->books
[l
]].nentries
;
818 // coded could be -1, but this still works, cause that is 0
819 if (coded
[counter
+ k
] < maxval
)
824 cshift
+= c
->subclass
;
826 if (put_codeword(pb
, book
, cval
))
827 return AVERROR(EINVAL
);
829 for (k
= 0; k
< c
->dim
; k
++) {
830 int book
= c
->books
[cval
& (csub
-1)];
831 int entry
= coded
[counter
++];
832 cval
>>= c
->subclass
;
837 if (put_codeword(pb
, &venc
->codebooks
[book
], entry
))
838 return AVERROR(EINVAL
);
842 ff_vorbis_floor1_render_list(fc
->list
, fc
->values
, posts
, coded
,
843 fc
->multiplier
, floor
, samples
);
848 static float *put_vector(vorbis_enc_codebook
*book
, PutBitContext
*pb
,
852 float distance
= FLT_MAX
;
853 assert(book
->dimensions
);
854 for (i
= 0; i
< book
->nentries
; i
++) {
855 float * vec
= book
->dimensions
+ i
* book
->ndimensions
, d
= book
->pow2
[i
];
859 for (j
= 0; j
< book
->ndimensions
; j
++)
860 d
-= vec
[j
] * num
[j
];
866 if (put_codeword(pb
, book
, entry
))
868 return &book
->dimensions
[entry
* book
->ndimensions
];
871 static int residue_encode(vorbis_enc_context
*venc
, vorbis_enc_residue
*rc
,
872 PutBitContext
*pb
, float *coeffs
, int samples
,
875 int pass
, i
, j
, p
, k
;
876 int psize
= rc
->partition_size
;
877 int partitions
= (rc
->end
- rc
->begin
) / psize
;
878 int channels
= (rc
->type
== 2) ? 1 : real_ch
;
879 int classes
[MAX_CHANNELS
][NUM_RESIDUE_PARTITIONS
];
880 int classwords
= venc
->codebooks
[rc
->classbook
].ndimensions
;
882 assert(rc
->type
== 2);
883 assert(real_ch
== 2);
884 for (p
= 0; p
< partitions
; p
++) {
885 float max1
= 0., max2
= 0.;
886 int s
= rc
->begin
+ p
* psize
;
887 for (k
= s
; k
< s
+ psize
; k
+= 2) {
888 max1
= FFMAX(max1
, fabs(coeffs
[ k
/ real_ch
]));
889 max2
= FFMAX(max2
, fabs(coeffs
[samples
+ k
/ real_ch
]));
892 for (i
= 0; i
< rc
->classifications
- 1; i
++)
893 if (max1
< rc
->maxes
[i
][0] && max2
< rc
->maxes
[i
][1])
898 for (pass
= 0; pass
< 8; pass
++) {
900 while (p
< partitions
) {
902 for (j
= 0; j
< channels
; j
++) {
903 vorbis_enc_codebook
* book
= &venc
->codebooks
[rc
->classbook
];
905 for (i
= 0; i
< classwords
; i
++) {
906 entry
*= rc
->classifications
;
907 entry
+= classes
[j
][p
+ i
];
909 if (put_codeword(pb
, book
, entry
))
910 return AVERROR(EINVAL
);
912 for (i
= 0; i
< classwords
&& p
< partitions
; i
++, p
++) {
913 for (j
= 0; j
< channels
; j
++) {
914 int nbook
= rc
->books
[classes
[j
][p
]][pass
];
915 vorbis_enc_codebook
* book
= &venc
->codebooks
[nbook
];
916 float *buf
= coeffs
+ samples
*j
+ rc
->begin
+ p
*psize
;
920 assert(rc
->type
== 0 || rc
->type
== 2);
921 assert(!(psize
% book
->ndimensions
));
924 for (k
= 0; k
< psize
; k
+= book
->ndimensions
) {
926 float *a
= put_vector(book
, pb
, &buf
[k
]);
928 return AVERROR(EINVAL
);
929 for (l
= 0; l
< book
->ndimensions
; l
++)
933 int s
= rc
->begin
+ p
* psize
, a1
, b1
;
934 a1
= (s
% real_ch
) * samples
;
936 s
= real_ch
* samples
;
937 for (k
= 0; k
< psize
; k
+= book
->ndimensions
) {
938 int dim
, a2
= a1
, b2
= b1
;
939 float vec
[MAX_CODEBOOK_DIM
], *pv
= vec
;
940 for (dim
= book
->ndimensions
; dim
--; ) {
941 *pv
++ = coeffs
[a2
+ b2
];
942 if ((a2
+= samples
) == s
) {
947 pv
= put_vector(book
, pb
, vec
);
949 return AVERROR(EINVAL
);
950 for (dim
= book
->ndimensions
; dim
--; ) {
951 coeffs
[a1
+ b1
] -= *pv
++;
952 if ((a1
+= samples
) == s
) {
966 static int apply_window_and_mdct(vorbis_enc_context
*venc
,
967 float **audio
, int samples
)
970 const float * win
= venc
->win
[0];
971 int window_len
= 1 << (venc
->log2_blocksize
[0] - 1);
972 float n
= (float)(1 << venc
->log2_blocksize
[0]) / 4.;
975 if (!venc
->have_saved
&& !samples
)
978 if (venc
->have_saved
) {
979 for (channel
= 0; channel
< venc
->channels
; channel
++)
980 memcpy(venc
->samples
+ channel
* window_len
* 2,
981 venc
->saved
+ channel
* window_len
, sizeof(float) * window_len
);
983 for (channel
= 0; channel
< venc
->channels
; channel
++)
984 memset(venc
->samples
+ channel
* window_len
* 2, 0,
985 sizeof(float) * window_len
);
989 for (channel
= 0; channel
< venc
->channels
; channel
++) {
990 float * offset
= venc
->samples
+ channel
*window_len
*2 + window_len
;
991 for (i
= 0; i
< samples
; i
++)
992 offset
[i
] = audio
[channel
][i
] / n
* win
[window_len
- i
- 1];
995 for (channel
= 0; channel
< venc
->channels
; channel
++)
996 memset(venc
->samples
+ channel
* window_len
* 2 + window_len
,
997 0, sizeof(float) * window_len
);
1000 for (channel
= 0; channel
< venc
->channels
; channel
++)
1001 venc
->mdct
[0].mdct_calc(&venc
->mdct
[0], venc
->coeffs
+ channel
* window_len
,
1002 venc
->samples
+ channel
* window_len
* 2);
1005 for (channel
= 0; channel
< venc
->channels
; channel
++) {
1006 float *offset
= venc
->saved
+ channel
* window_len
;
1007 for (i
= 0; i
< samples
; i
++)
1008 offset
[i
] = audio
[channel
][i
] / n
* win
[i
];
1010 venc
->have_saved
= 1;
1012 venc
->have_saved
= 0;
1018 static int vorbis_encode_frame(AVCodecContext
*avccontext
, AVPacket
*avpkt
,
1019 const AVFrame
*frame
, int *got_packet_ptr
)
1021 vorbis_enc_context
*venc
= avccontext
->priv_data
;
1022 float **audio
= frame
? (float **)frame
->extended_data
: NULL
;
1023 int samples
= frame
? frame
->nb_samples
: 0;
1024 vorbis_enc_mode
*mode
;
1025 vorbis_enc_mapping
*mapping
;
1029 if (!apply_window_and_mdct(venc
, audio
, samples
))
1031 samples
= 1 << (venc
->log2_blocksize
[0] - 1);
1033 if ((ret
= ff_alloc_packet(avpkt
, 8192))) {
1034 av_log(avccontext
, AV_LOG_ERROR
, "Error getting output packet\n");
1038 init_put_bits(&pb
, avpkt
->data
, avpkt
->size
);
1040 if (pb
.size_in_bits
- put_bits_count(&pb
) < 1 + ilog(venc
->nmodes
- 1)) {
1041 av_log(avccontext
, AV_LOG_ERROR
, "output buffer is too small\n");
1042 return AVERROR(EINVAL
);
1045 put_bits(&pb
, 1, 0); // magic bit
1047 put_bits(&pb
, ilog(venc
->nmodes
- 1), 0); // 0 bits, the mode
1049 mode
= &venc
->modes
[0];
1050 mapping
= &venc
->mappings
[mode
->mapping
];
1051 if (mode
->blockflag
) {
1052 put_bits(&pb
, 1, 0);
1053 put_bits(&pb
, 1, 0);
1056 for (i
= 0; i
< venc
->channels
; i
++) {
1057 vorbis_enc_floor
*fc
= &venc
->floors
[mapping
->floor
[mapping
->mux
[i
]]];
1058 uint16_t posts
[MAX_FLOOR_VALUES
];
1059 floor_fit(venc
, fc
, &venc
->coeffs
[i
* samples
], posts
, samples
);
1060 if (floor_encode(venc
, fc
, &pb
, posts
, &venc
->floor
[i
* samples
], samples
)) {
1061 av_log(avccontext
, AV_LOG_ERROR
, "output buffer is too small\n");
1062 return AVERROR(EINVAL
);
1066 for (i
= 0; i
< venc
->channels
* samples
; i
++)
1067 venc
->coeffs
[i
] /= venc
->floor
[i
];
1069 for (i
= 0; i
< mapping
->coupling_steps
; i
++) {
1070 float *mag
= venc
->coeffs
+ mapping
->magnitude
[i
] * samples
;
1071 float *ang
= venc
->coeffs
+ mapping
->angle
[i
] * samples
;
1073 for (j
= 0; j
< samples
; j
++) {
1083 if (residue_encode(venc
, &venc
->residues
[mapping
->residue
[mapping
->mux
[0]]],
1084 &pb
, venc
->coeffs
, samples
, venc
->channels
)) {
1085 av_log(avccontext
, AV_LOG_ERROR
, "output buffer is too small\n");
1086 return AVERROR(EINVAL
);
1089 flush_put_bits(&pb
);
1090 avpkt
->size
= put_bits_count(&pb
) >> 3;
1092 avpkt
->duration
= ff_samples_to_time_base(avccontext
, avccontext
->frame_size
);
1094 if (frame
->pts
!= AV_NOPTS_VALUE
)
1095 avpkt
->pts
= ff_samples_to_time_base(avccontext
, frame
->pts
);
1097 avpkt
->pts
= venc
->next_pts
;
1098 if (avpkt
->pts
!= AV_NOPTS_VALUE
)
1099 venc
->next_pts
= avpkt
->pts
+ avpkt
->duration
;
1101 *got_packet_ptr
= 1;
1106 static av_cold
int vorbis_encode_close(AVCodecContext
*avccontext
)
1108 vorbis_enc_context
*venc
= avccontext
->priv_data
;
1111 if (venc
->codebooks
)
1112 for (i
= 0; i
< venc
->ncodebooks
; i
++) {
1113 av_freep(&venc
->codebooks
[i
].lens
);
1114 av_freep(&venc
->codebooks
[i
].codewords
);
1115 av_freep(&venc
->codebooks
[i
].quantlist
);
1116 av_freep(&venc
->codebooks
[i
].dimensions
);
1117 av_freep(&venc
->codebooks
[i
].pow2
);
1119 av_freep(&venc
->codebooks
);
1122 for (i
= 0; i
< venc
->nfloors
; i
++) {
1124 if (venc
->floors
[i
].classes
)
1125 for (j
= 0; j
< venc
->floors
[i
].nclasses
; j
++)
1126 av_freep(&venc
->floors
[i
].classes
[j
].books
);
1127 av_freep(&venc
->floors
[i
].classes
);
1128 av_freep(&venc
->floors
[i
].partition_to_class
);
1129 av_freep(&venc
->floors
[i
].list
);
1131 av_freep(&venc
->floors
);
1134 for (i
= 0; i
< venc
->nresidues
; i
++) {
1135 av_freep(&venc
->residues
[i
].books
);
1136 av_freep(&venc
->residues
[i
].maxes
);
1138 av_freep(&venc
->residues
);
1141 for (i
= 0; i
< venc
->nmappings
; i
++) {
1142 av_freep(&venc
->mappings
[i
].mux
);
1143 av_freep(&venc
->mappings
[i
].floor
);
1144 av_freep(&venc
->mappings
[i
].residue
);
1145 av_freep(&venc
->mappings
[i
].magnitude
);
1146 av_freep(&venc
->mappings
[i
].angle
);
1148 av_freep(&venc
->mappings
);
1150 av_freep(&venc
->modes
);
1152 av_freep(&venc
->saved
);
1153 av_freep(&venc
->samples
);
1154 av_freep(&venc
->floor
);
1155 av_freep(&venc
->coeffs
);
1157 ff_mdct_end(&venc
->mdct
[0]);
1158 ff_mdct_end(&venc
->mdct
[1]);
1160 #if FF_API_OLD_ENCODE_AUDIO
1161 av_freep(&avccontext
->coded_frame
);
1163 av_freep(&avccontext
->extradata
);
1168 static av_cold
int vorbis_encode_init(AVCodecContext
*avccontext
)
1170 vorbis_enc_context
*venc
= avccontext
->priv_data
;
1173 if (avccontext
->channels
!= 2) {
1174 av_log(avccontext
, AV_LOG_ERROR
, "Current Libav Vorbis encoder only supports 2 channels.\n");
1178 if ((ret
= create_vorbis_context(venc
, avccontext
)) < 0)
1181 avccontext
->bit_rate
= 0;
1182 if (avccontext
->flags
& CODEC_FLAG_QSCALE
)
1183 venc
->quality
= avccontext
->global_quality
/ (float)FF_QP2LAMBDA
;
1185 venc
->quality
= 3.0;
1186 venc
->quality
*= venc
->quality
;
1188 if ((ret
= put_main_header(venc
, (uint8_t**)&avccontext
->extradata
)) < 0)
1190 avccontext
->extradata_size
= ret
;
1192 avccontext
->frame_size
= 1 << (venc
->log2_blocksize
[0] - 1);
1194 #if FF_API_OLD_ENCODE_AUDIO
1195 avccontext
->coded_frame
= avcodec_alloc_frame();
1196 if (!avccontext
->coded_frame
) {
1197 ret
= AVERROR(ENOMEM
);
1204 vorbis_encode_close(avccontext
);
1208 AVCodec ff_vorbis_encoder
= {
1210 .type
= AVMEDIA_TYPE_AUDIO
,
1211 .id
= AV_CODEC_ID_VORBIS
,
1212 .priv_data_size
= sizeof(vorbis_enc_context
),
1213 .init
= vorbis_encode_init
,
1214 .encode2
= vorbis_encode_frame
,
1215 .close
= vorbis_encode_close
,
1216 .capabilities
= CODEC_CAP_DELAY
| CODEC_CAP_EXPERIMENTAL
,
1217 .sample_fmts
= (const enum AVSampleFormat
[]){ AV_SAMPLE_FMT_FLTP
,
1218 AV_SAMPLE_FMT_NONE
},
1219 .long_name
= NULL_IF_CONFIG_SMALL("Vorbis"),