You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1203 lines
54KB

  1. /*
  2. * copyright (c) 2006 Oded Shimon <ods15@ods15.dyndns.org>
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. /**
  19. * @file vorbis_enc.c
  20. * Native Vorbis encoder.
  21. * @author Oded Shimon <ods15@ods15.dyndns.org>
  22. */
  23. #include "avcodec.h"
  24. #include "dsputil.h"
  25. #include "vorbis.h"
  26. #undef NDEBUG
  27. #include <assert.h>
  28. //#define ALT_BITSTREAM_WRITER
  29. //#include "bitstream.h"
  30. typedef struct {
  31. int len;
  32. uint32_t codeword;
  33. } cb_entry_t;
  34. typedef struct {
  35. int nentries;
  36. cb_entry_t * entries;
  37. int ndimentions;
  38. float min;
  39. float delta;
  40. int seq_p;
  41. int lookup;
  42. int * quantlist;
  43. float * dimentions;
  44. } codebook_t;
  45. typedef struct {
  46. int dim;
  47. int subclass;
  48. int masterbook;
  49. int * books;
  50. } floor_class_t;
  51. typedef struct {
  52. int x;
  53. int low;
  54. int high;
  55. int sort;
  56. } floor_entry_t;
  57. typedef struct {
  58. int partitions;
  59. int * partition_to_class;
  60. int nclasses;
  61. floor_class_t * classes;
  62. int multiplier;
  63. int rangebits;
  64. int values;
  65. floor_entry_t * list;
  66. } floor_t;
  67. typedef struct {
  68. int type;
  69. int begin;
  70. int end;
  71. int partition_size;
  72. int classifications;
  73. int classbook;
  74. int (*books)[8];
  75. float (*maxes)[2];
  76. } residue_t;
  77. typedef struct {
  78. int submaps;
  79. int * mux;
  80. int * floor;
  81. int * residue;
  82. int coupling_steps;
  83. int * magnitude;
  84. int * angle;
  85. } mapping_t;
  86. typedef struct {
  87. int blockflag;
  88. int mapping;
  89. } vorbis_mode_t;
  90. typedef struct {
  91. int channels;
  92. int sample_rate;
  93. int blocksize[2]; // in (1<<n) format
  94. MDCTContext mdct[2];
  95. const float * win[2];
  96. int have_saved;
  97. float * saved;
  98. float * samples;
  99. float * floor; // also used for tmp values for mdct
  100. float * coeffs; // also used for residue after floor
  101. float quality;
  102. int ncodebooks;
  103. codebook_t * codebooks;
  104. int nfloors;
  105. floor_t * floors;
  106. int nresidues;
  107. residue_t * residues;
  108. int nmappings;
  109. mapping_t * mappings;
  110. int nmodes;
  111. vorbis_mode_t * modes;
  112. } venc_context_t;
  113. typedef struct {
  114. int total;
  115. int total_pos;
  116. int pos;
  117. uint8_t * buf_ptr;
  118. } PutBitContext;
  119. #define ilog(i) av_log2(2*(i))
  120. static inline void init_put_bits(PutBitContext * pb, uint8_t * buf, int buffer_len) {
  121. pb->total = buffer_len * 8;
  122. pb->total_pos = 0;
  123. pb->pos = 0;
  124. pb->buf_ptr = buf;
  125. }
  126. static void put_bits(PutBitContext * pb, int bits, uint64_t val) {
  127. if ((pb->total_pos += bits) >= pb->total) return;
  128. if (!bits) return;
  129. if (pb->pos) {
  130. if (pb->pos > bits) {
  131. *pb->buf_ptr |= val << (8 - pb->pos);
  132. pb->pos -= bits;
  133. bits = 0;
  134. } else {
  135. *pb->buf_ptr++ |= (val << (8 - pb->pos)) & 0xFF;
  136. val >>= pb->pos;
  137. bits -= pb->pos;
  138. pb->pos = 0;
  139. }
  140. }
  141. for (; bits >= 8; bits -= 8) {
  142. *pb->buf_ptr++ = val & 0xFF;
  143. val >>= 8;
  144. }
  145. if (bits) {
  146. *pb->buf_ptr = val;
  147. pb->pos = 8 - bits;
  148. }
  149. }
  150. static inline void flush_put_bits(PutBitContext * pb) {
  151. }
  152. static inline int put_bits_count(PutBitContext * pb) {
  153. return pb->total_pos;
  154. }
  155. static int cb_lookup_vals(int lookup, int dimentions, int entries) {
  156. if (lookup == 1) {
  157. int tmp, i;
  158. for (tmp = 0; ; tmp++) {
  159. int n = 1;
  160. for (i = 0; i < dimentions; i++) n *= tmp;
  161. if (n > entries) break;
  162. }
  163. return tmp - 1;
  164. } else if (lookup == 2) return dimentions * entries;
  165. return 0;
  166. }
  167. static void ready_codebook(codebook_t * cb) {
  168. int h[33] = { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
  169. int i;
  170. for (i = 0; i < cb->nentries; i++) {
  171. cb_entry_t * e = &cb->entries[i];
  172. int j = 0;
  173. if (!e->len) continue;
  174. if (h[0]) h[0] = 0;
  175. else {
  176. for (j = e->len; j; j--)
  177. if (h[j]) break;
  178. assert(j);
  179. }
  180. e->codeword = h[j];
  181. h[j] = 0;
  182. for (j++; j <= e->len; j++) h[j] = e->codeword | (1 << (j - 1));
  183. }
  184. for (i = 0; i < 33; i++) assert(!h[i]);
  185. if (!cb->lookup) cb->dimentions = NULL;
  186. else {
  187. int vals = cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries);
  188. cb->dimentions = av_malloc(sizeof(float) * cb->nentries * cb->ndimentions);
  189. for (i = 0; i < cb->nentries; i++) {
  190. float last = 0;
  191. int j;
  192. int div = 1;
  193. for (j = 0; j < cb->ndimentions; j++) {
  194. int off;
  195. if (cb->lookup == 1) off = (i / div) % vals; // lookup type 1
  196. else off = i * cb->ndimentions + j; // lookup type 2
  197. cb->dimentions[i * cb->ndimentions + j] = last + cb->min + cb->quantlist[off] * cb->delta;
  198. if (cb->seq_p) last = cb->dimentions[i * cb->ndimentions + j];
  199. div *= vals;
  200. }
  201. }
  202. }
  203. }
  204. static void ready_floor(floor_t * fc) {
  205. int i;
  206. fc->list[0].sort = 0;
  207. fc->list[1].sort = 1;
  208. for (i = 2; i < fc->values; i++) {
  209. int j;
  210. fc->list[i].low = 0;
  211. fc->list[i].high = 1;
  212. fc->list[i].sort = i;
  213. for (j = 2; j < i; j++) {
  214. int tmp = fc->list[j].x;
  215. if (tmp < fc->list[i].x) {
  216. if (tmp > fc->list[fc->list[i].low].x) fc->list[i].low = j;
  217. } else {
  218. if (tmp < fc->list[fc->list[i].high].x) fc->list[i].high = j;
  219. }
  220. }
  221. }
  222. for (i = 0; i < fc->values - 1; i++) {
  223. int j;
  224. for (j = i + 1; j < fc->values; j++) {
  225. if (fc->list[fc->list[i].sort].x > fc->list[fc->list[j].sort].x) {
  226. int tmp = fc->list[i].sort;
  227. fc->list[i].sort = fc->list[j].sort;
  228. fc->list[j].sort = tmp;
  229. }
  230. }
  231. }
  232. }
  233. static void ready_residue(residue_t * rc, venc_context_t * venc) {
  234. int i;
  235. assert(rc->type == 2);
  236. rc->maxes = av_mallocz(sizeof(float[2]) * rc->classifications);
  237. for (i = 0; i < rc->classifications; i++) {
  238. int j;
  239. codebook_t * cb;
  240. for (j = 0; j < 8; j++) if (rc->books[i][j] != -1) break;
  241. if (j == 8) continue; // zero
  242. cb = &venc->codebooks[rc->books[i][j]];
  243. assert(cb->ndimentions >= 2);
  244. assert(cb->lookup);
  245. for (j = 0; j < cb->nentries; j++) {
  246. float a;
  247. if (!cb->entries[j].len) continue;
  248. a = fabs(cb->dimentions[j * cb->ndimentions]);
  249. if (a > rc->maxes[i][0]) rc->maxes[i][0] = a;
  250. a = fabs(cb->dimentions[j * cb->ndimentions + 1]);
  251. if (a > rc->maxes[i][1]) rc->maxes[i][1] = a;
  252. }
  253. }
  254. // small bias
  255. for (i = 0; i < rc->classifications; i++) {
  256. rc->maxes[i][0] += 0.8;
  257. rc->maxes[i][1] += 0.8;
  258. }
  259. }
  260. static void create_vorbis_context(venc_context_t * venc, AVCodecContext * avccontext) {
  261. codebook_t * cb;
  262. floor_t * fc;
  263. residue_t * rc;
  264. mapping_t * mc;
  265. int i, book;
  266. int codebook0[] = { 2, 10, 8, 14, 7, 12, 11, 14, 1, 5, 3, 7, 4, 9, 7, 13, };
  267. int codebook1[] = { 1, 4, 2, 6, 3, 7, 5, 7, };
  268. int codebook2[] = { 1, 5, 7, 21, 5, 8, 9, 21, 10, 9, 12, 20, 20, 16, 20, 20, 4, 8, 9, 20, 6, 8, 9, 20, 11, 11, 13, 20, 20, 15, 17, 20, 9, 11, 14, 20, 8, 10, 15, 20, 11, 13, 15, 20, 20, 20, 20, 20, 20, 20, 20, 20, 13, 20, 20, 20, 18, 18, 20, 20, 20, 20, 20, 20, 3, 6, 8, 20, 6, 7, 9, 20, 10, 9, 12, 20, 20, 20, 20, 20, 5, 7, 9, 20, 6, 6, 9, 20, 10, 9, 12, 20, 20, 20, 20, 20, 8, 10, 13, 20, 8, 9, 12, 20, 11, 10, 12, 20, 20, 20, 20, 20, 18, 20, 20, 20, 15, 17, 18, 20, 18, 17, 18, 20, 20, 20, 20, 20, 7, 10, 12, 20, 8, 9, 11, 20, 14, 13, 14, 20, 20, 20, 20, 20, 6, 9, 12, 20, 7, 8, 11, 20, 12, 11, 13, 20, 20, 20, 20, 20, 9, 11, 15, 20, 8, 10, 14, 20, 12, 11, 14, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 11, 16, 18, 20, 15, 15, 17, 20, 20, 17, 20, 20, 20, 20, 20, 20, 9, 14, 16, 20, 12, 12, 15, 20, 17, 15, 18, 20, 20, 20, 20, 20, 16, 19, 18, 20, 15, 16, 20, 20, 17, 17, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, };
  269. int codebook3[] = { 2, 3, 7, 13, 4, 4, 7, 15, 8, 6, 9, 17, 21, 16, 15, 21, 2, 5, 7, 11, 5, 5, 7, 14, 9, 7, 10, 16, 17, 15, 16, 21, 4, 7, 10, 17, 7, 7, 9, 15, 11, 9, 11, 16, 21, 18, 15, 21, 18, 21, 21, 21, 15, 17, 17, 19, 21, 19, 18, 20, 21, 21, 21, 20, };
  270. int codebook4[] = { 5, 5, 5, 5, 6, 5, 6, 5, 6, 5, 6, 5, 6, 5, 6, 5, 6, 5, 6, 5, 6, 5, 6, 5, 7, 5, 7, 5, 7, 5, 7, 5, 8, 6, 8, 6, 8, 6, 9, 6, 9, 6, 10, 6, 10, 6, 11, 6, 11, 7, 11, 7, 12, 7, 12, 7, 12, 7, 12, 7, 12, 7, 12, 7, 12, 7, 12, 8, 13, 8, 12, 8, 12, 8, 13, 8, 13, 9, 13, 9, 13, 9, 13, 9, 12, 10, 12, 10, 13, 10, 14, 11, 14, 12, 14, 13, 14, 13, 14, 14, 15, 16, 15, 15, 15, 14, 15, 17, 21, 22, 22, 21, 22, 22, 22, 22, 22, 22, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, };
  271. int codebook5[] = { 2, 5, 5, 4, 5, 4, 5, 4, 5, 4, 6, 5, 6, 5, 6, 5, 6, 5, 7, 5, 7, 6, 8, 6, 8, 6, 8, 6, 9, 6, 9, 6, };
  272. int codebook6[] = { 8, 5, 8, 4, 9, 4, 9, 4, 9, 4, 9, 4, 9, 4, 9, 4, 9, 4, 9, 4, 9, 4, 8, 4, 8, 4, 9, 5, 9, 5, 9, 5, 9, 5, 9, 6, 10, 6, 10, 7, 10, 8, 11, 9, 11, 11, 12, 13, 12, 14, 13, 15, 13, 15, 14, 16, 14, 17, 15, 17, 15, 15, 16, 16, 15, 16, 16, 16, 15, 18, 16, 15, 17, 17, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, };
  273. int codebook7[] = { 1, 5, 5, 5, 5, 5, 5, 5, 6, 5, 6, 5, 6, 5, 6, 5, 6, 6, 7, 7, 7, 7, 8, 7, 8, 8, 9, 8, 10, 9, 10, 9, };
  274. int codebook8[] = { 4, 3, 4, 3, 4, 4, 5, 4, 5, 4, 5, 5, 6, 5, 6, 5, 7, 5, 7, 6, 7, 6, 8, 7, 8, 7, 8, 7, 9, 8, 9, 9, 9, 9, 10, 10, 10, 11, 9, 12, 9, 12, 9, 15, 10, 14, 9, 13, 10, 13, 10, 12, 10, 12, 10, 13, 10, 12, 11, 13, 11, 14, 12, 13, 13, 14, 14, 13, 14, 15, 14, 16, 13, 13, 14, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 15, 15, };
  275. int codebook9[] = { 4, 5, 4, 5, 3, 5, 3, 5, 3, 5, 4, 4, 4, 4, 5, 5, 5, };
  276. int codebook10[] = { 3, 3, 4, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 5, 7, 5, 8, 6, 8, 6, 9, 7, 10, 7, 10, 8, 10, 8, 11, 9, 11, };
  277. int codebook11[] = { 3, 7, 3, 8, 3, 10, 3, 8, 3, 9, 3, 8, 4, 9, 4, 9, 5, 9, 6, 10, 6, 9, 7, 11, 7, 12, 9, 13, 10, 13, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, };
  278. int codebook12[] = { 4, 5, 4, 5, 4, 5, 4, 5, 3, 5, 3, 5, 3, 5, 4, 5, 4, };
  279. int codebook13[] = { 4, 2, 4, 2, 5, 3, 5, 4, 6, 6, 6, 7, 7, 8, 7, 8, 7, 8, 7, 9, 8, 9, 8, 9, 8, 10, 8, 11, 9, 12, 9, 12, };
  280. int codebook14[] = { 2, 5, 2, 6, 3, 6, 4, 7, 4, 7, 5, 9, 5, 11, 6, 11, 6, 11, 7, 11, 6, 11, 6, 11, 9, 11, 8, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 10, 10, 10, 10, 10, 10, };
  281. int codebook15[] = { 5, 6, 11, 11, 11, 11, 10, 10, 12, 11, 5, 2, 11, 5, 6, 6, 7, 9, 11, 13, 13, 10, 7, 11, 6, 7, 8, 9, 10, 12, 11, 5, 11, 6, 8, 7, 9, 11, 14, 15, 11, 6, 6, 8, 4, 5, 7, 8, 10, 13, 10, 5, 7, 7, 5, 5, 6, 8, 10, 11, 10, 7, 7, 8, 6, 5, 5, 7, 9, 9, 11, 8, 8, 11, 8, 7, 6, 6, 7, 9, 12, 11, 10, 13, 9, 9, 7, 7, 7, 9, 11, 13, 12, 15, 12, 11, 9, 8, 8, 8, };
  282. int codebook16[] = { 2, 4, 4, 0, 0, 0, 0, 0, 0, 5, 6, 6, 0, 0, 0, 0, 0, 0, 5, 6, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 7, 7, 0, 0, 0, 0, 0, 0, 7, 8, 8, 0, 0, 0, 0, 0, 0, 6, 7, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 7, 7, 0, 0, 0, 0, 0, 0, 6, 8, 7, 0, 0, 0, 0, 0, 0, 7, 8, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,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 0, 0, 0, 0, 0, 7, 8, 0, 0, 0, 0, 0, 0, 0, 7, 7, 0, 0, 0, 0, 0, 0, 0, 9, 9, };
  286. int codebook20[] = { 1, 3, 4, 6, 6, 7, 7, 9, 9, 0, 5, 5, 7, 7, 7, 8, 9, 9, 0, 5, 5, 7, 7, 8, 8, 9, 9, 0, 7, 7, 8, 8, 8, 8, 10, 10, 0, 0, 0, 8, 8, 8, 8, 10, 10, 0, 0, 0, 9, 9, 9, 9, 10, 10, 0, 0, 0, 9, 9, 9, 9, 10, 10, 0, 0, 0, 10, 10, 10, 10, 11, 11, 0, 0, 0, 0, 0, 10, 10, 11, 11, };
  287. int codebook21[] = { 2, 3, 3, 6, 6, 7, 7, 8, 8, 8, 8, 9, 9, 10, 10, 11, 10, 0, 5, 5, 7, 7, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 0, 5, 5, 7, 7, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 0, 6, 6, 7, 7, 8, 8, 9, 9, 9, 9, 10, 10, 11, 11, 11, 11, 0, 0, 0, 7, 7, 8, 8, 9, 9, 9, 9, 10, 10, 11, 11, 11, 12, 0, 0, 0, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 11, 11, 12, 12, 0, 0, 0, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 11, 11, 12, 12, 0, 0, 0, 9, 9, 9, 9, 10, 10, 10, 10, 11, 10, 11, 11, 12, 12, 0, 0, 0, 0, 0, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11, 12, 12, 0, 0, 0, 0, 0, 9, 8, 9, 9, 10, 10, 11, 11, 12, 12, 12, 12, 0, 0, 0, 0, 0, 8, 8, 9, 9, 10, 10, 11, 11, 12, 11, 12, 12, 0, 0, 0, 0, 0, 9, 10, 10, 10, 11, 11, 11, 11, 12, 12, 13, 13, 0, 0, 0, 0, 0, 0, 0, 10, 10, 10, 10, 11, 11, 12, 12, 13, 13, 0, 0, 0, 0, 0, 0, 0, 11, 11, 11, 11, 12, 12, 12, 12, 13, 13, 0, 0, 0, 0, 0, 0, 0, 11, 11, 11, 11, 12, 12, 12, 12, 13, 13, 0, 0, 0, 0, 0, 0, 0, 11, 11, 12, 12, 12, 12, 13, 13, 13, 13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 12, 12, 12, 13, 13, 13, 13, };
  288. int codebook22[] = { 1, 4, 4, 7, 6, 6, 7, 6, 6, 4, 7, 7, 10, 9, 9, 11, 9, 9, 4, 7, 7, 10, 9, 9, 11, 9, 9, 7, 10, 10, 11, 11, 10, 12, 11, 11, 6, 9, 9, 11, 10, 10, 11, 10, 10, 6, 9, 9, 11, 10, 10, 11, 10, 10, 7, 11, 11, 11, 11, 11, 12, 11, 11, 6, 9, 9, 11, 10, 10, 11, 10, 10, 6, 9, 9, 11, 10, 10, 11, 10, 10, };
  289. int codebook23[] = { 2, 4, 4, 6, 6, 7, 7, 7, 7, 8, 8, 10, 5, 5, 6, 6, 7, 7, 8, 8, 8, 8, 10, 5, 5, 6, 6, 7, 7, 8, 8, 8, 8, 10, 6, 6, 7, 7, 8, 8, 8, 8, 8, 8, 10, 10, 10, 7, 7, 8, 7, 8, 8, 8, 8, 10, 10, 10, 8, 8, 8, 8, 8, 8, 8, 8, 10, 10, 10, 7, 8, 8, 8, 8, 8, 8, 8, 10, 10, 10, 8, 8, 8, 8, 8, 8, 8, 8, 10, 10, 10, 10, 10, 8, 8, 8, 8, 8, 8, 10, 10, 10, 10, 10, 9, 9, 8, 8, 9, 8, 10, 10, 10, 10, 10, 8, 8, 8, 8, 8, 8, };
  290. int codebook24[] = { 1, 4, 4, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 6, 5, 5, 7, 7, 8, 8, 8, 8, 9, 9, 10, 10, 7, 5, 5, 7, 7, 8, 8, 8, 8, 9, 9, 11, 10, 0, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 11, 11, 0, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 11, 11, 0, 12, 12, 9, 9, 10, 10, 10, 10, 11, 11, 11, 12, 0, 13, 13, 9, 9, 10, 10, 10, 10, 11, 11, 12, 12, 0, 0, 0, 10, 10, 10, 10, 11, 11, 12, 12, 12, 12, 0, 0, 0, 10, 10, 10, 10, 11, 11, 12, 12, 12, 12, 0, 0, 0, 14, 14, 11, 11, 11, 11, 12, 12, 13, 13, 0, 0, 0, 14, 14, 11, 11, 11, 11, 12, 12, 13, 13, 0, 0, 0, 0, 0, 12, 12, 12, 12, 13, 13, 14, 13, 0, 0, 0, 0, 0, 13, 13, 12, 12, 13, 12, 14, 13, };
  291. int codebook25[] = { 2, 4, 4, 5, 5, 6, 5, 5, 5, 5, 6, 4, 5, 5, 5, 6, 5, 5, 5, 5, 6, 6, 6, 5, 5, };
  292. int codebook26[] = { 1, 4, 4, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 4, 9, 8, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 2, 9, 7, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, };
  293. int codebook27[] = { 1, 4, 4, 6, 6, 7, 7, 8, 7, 9, 9, 10, 10, 10, 10, 6, 5, 5, 7, 7, 8, 8, 10, 8, 11, 10, 12, 12, 13, 13, 6, 5, 5, 7, 7, 8, 8, 10, 9, 11, 11, 12, 12, 13, 12, 18, 8, 8, 8, 8, 9, 9, 10, 9, 11, 10, 12, 12, 13, 13, 18, 8, 8, 8, 8, 9, 9, 10, 10, 11, 11, 13, 12, 14, 13, 18, 11, 11, 9, 9, 10, 10, 11, 11, 11, 12, 13, 12, 13, 14, 18, 11, 11, 9, 8, 11, 10, 11, 11, 11, 11, 12, 12, 14, 13, 18, 18, 18, 10, 11, 10, 11, 12, 12, 12, 12, 13, 12, 14, 13, 18, 18, 18, 10, 11, 11, 9, 12, 11, 12, 12, 12, 13, 13, 13, 18, 18, 17, 14, 14, 11, 11, 12, 12, 13, 12, 14, 12, 14, 13, 18, 18, 18, 14, 14, 11, 10, 12, 9, 12, 13, 13, 13, 13, 13, 18, 18, 17, 16, 18, 13, 13, 12, 12, 13, 11, 14, 12, 14, 14, 17, 18, 18, 17, 18, 13, 12, 13, 10, 12, 11, 14, 14, 14, 14, 17, 18, 18, 18, 18, 15, 16, 12, 12, 13, 10, 14, 12, 14, 15, 18, 18, 18, 16, 17, 16, 14, 12, 11, 13, 10, 13, 13, 14, 15, };
  294. int codebook28[] = { 2, 5, 5, 6, 6, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 10, 6, 6, 7, 7, 8, 7, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 10, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 10, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 7, 7, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 11, 11, 11, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 9, 10, 10, 10, 11, 11, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 11, 10, 11, 11, 11, 9, 9, 9, 9, 9, 9, 10, 10, 9, 9, 10, 9, 11, 10, 11, 11, 11, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 9, 11, 11, 11, 11, 11, 9, 9, 9, 9, 10, 10, 9, 9, 9, 9, 10, 9, 11, 11, 11, 11, 11, 11, 11, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 10, 9, 10, 10, 9, 10, 9, 9, 10, 9, 11, 10, 10, 11, 11, 11, 11, 9, 10, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 10, 10, 10, 9, 9, 10, 9, 10, 9, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 9, 9, 9, 9, 9, 10, 10, 10, };
  295. int codebook_sizes[] = { 16, 8, 256, 64, 128, 32, 96, 32, 96, 17, 32, 78, 17, 32, 78, 100, 1641, 443, 105, 68, 81, 289, 81, 121, 169, 25, 169, 225, 289, };
  296. int * codebook_lens[] = { codebook0, codebook1, codebook2, codebook3, codebook4, codebook5, codebook6, codebook7,
  297. codebook8, codebook9, codebook10, codebook11, codebook12, codebook13, codebook14, codebook15,
  298. codebook16, codebook17, codebook18, codebook19, codebook20, codebook21, codebook22, codebook23,
  299. codebook24, codebook25, codebook26, codebook27, codebook28, };
  300. struct {
  301. int lookup;
  302. int dim;
  303. float min;
  304. float delta;
  305. int real_len;
  306. int * quant;
  307. } cvectors[] = {
  308. { 1, 8, -1.0, 1.0, 6561,(int[]){ 1, 0, 2, } },
  309. { 1, 4, -2.0, 1.0, 625, (int[]){ 2, 1, 3, 0, 4, } },
  310. { 1, 4, -2.0, 1.0, 625, (int[]){ 2, 1, 3, 0, 4, } },
  311. { 1, 2, -4.0, 1.0, 81, (int[]){ 4, 3, 5, 2, 6, 1, 7, 0, 8, } },
  312. { 1, 2, -4.0, 1.0, 81, (int[]){ 4, 3, 5, 2, 6, 1, 7, 0, 8, } },
  313. { 1, 2, -8.0, 1.0, 289, (int[]){ 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15, 0, 16, } },
  314. { 1, 4, -11.0, 11.0, 81, (int[]){ 1, 0, 2, } },
  315. { 1, 2, -5.0, 1.0, 121, (int[]){ 5, 4, 6, 3, 7, 2, 8, 1, 9, 0, 10, } },
  316. { 1, 2, -30.0, 5.0, 169, (int[]){ 6, 5, 7, 4, 8, 3, 9, 2, 10, 1, 11, 0, 12, } },
  317. { 1, 2, -2.0, 1.0, 25, (int[]){ 2, 1, 3, 0, 4, } },
  318. { 1, 2, -1530.0, 255.0, 169, (int[]){ 6, 5, 7, 4, 8, 3, 9, 2, 10, 1, 11, 0, 12, } },
  319. { 1, 2, -119.0, 17.0, 225, (int[]){ 7, 6, 8, 5, 9, 4, 10, 3, 11, 2, 12, 1, 13, 0, 14, } },
  320. { 1, 2, -8.0, 1.0, 289, (int[]){ 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15, 0, 16, } },
  321. };
  322. venc->channels = avccontext->channels;
  323. venc->sample_rate = avccontext->sample_rate;
  324. venc->blocksize[0] = venc->blocksize[1] = 11;
  325. venc->ncodebooks = 29;
  326. venc->codebooks = av_malloc(sizeof(codebook_t) * venc->ncodebooks);
  327. // codebook 0..14 - floor1 book, values 0..255
  328. // codebook 15 residue masterbook
  329. // codebook 16..29 residue
  330. for (book = 0; book < venc->ncodebooks; book++) {
  331. cb = &venc->codebooks[book];
  332. cb->nentries = codebook_sizes[book];
  333. if (book < 16) {
  334. cb->ndimentions = 2;
  335. cb->min = 0.;
  336. cb->delta = 0.;
  337. cb->seq_p = 0;
  338. cb->lookup = 0;
  339. cb->quantlist = NULL;
  340. } else {
  341. int vals;
  342. cb->seq_p = 0;
  343. cb->nentries = cvectors[book - 16].real_len;
  344. cb->ndimentions = cvectors[book - 16].dim;
  345. cb->min = cvectors[book - 16].min;
  346. cb->delta = cvectors[book - 16].delta;
  347. cb->lookup = cvectors[book - 16].lookup;
  348. vals = cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries);
  349. cb->quantlist = av_malloc(sizeof(int) * vals);
  350. for (i = 0; i < vals; i++) cb->quantlist[i] = cvectors[book - 16].quant[i];
  351. }
  352. cb->entries = av_malloc(sizeof(cb_entry_t) * cb->nentries);
  353. for (i = 0; i < cb->nentries; i++) {
  354. if (i < codebook_sizes[book]) cb->entries[i].len = codebook_lens[book][i];
  355. else cb->entries[i].len = 0;
  356. }
  357. ready_codebook(cb);
  358. }
  359. venc->nfloors = 1;
  360. venc->floors = av_malloc(sizeof(floor_t) * venc->nfloors);
  361. // just 1 floor
  362. fc = &venc->floors[0];
  363. fc->partitions = 8;
  364. fc->partition_to_class = av_malloc(sizeof(int) * fc->partitions);
  365. fc->nclasses = 0;
  366. for (i = 0; i < fc->partitions; i++) {
  367. int a[] = {0,1,2,2,3,3,4,4};
  368. fc->partition_to_class[i] = a[i];
  369. fc->nclasses = FFMAX(fc->nclasses, fc->partition_to_class[i]);
  370. }
  371. fc->nclasses++;
  372. fc->classes = av_malloc(sizeof(floor_class_t) * fc->nclasses);
  373. for (i = 0; i < fc->nclasses; i++) {
  374. floor_class_t * c = &fc->classes[i];
  375. int j, books;
  376. int dim[] = {3,4,3,4,3};
  377. int subclass[] = {0,1,1,2,2};
  378. int masterbook[] = {0/*none*/,0,1,2,3};
  379. int * nbooks[] = {
  380. (int[]){ 4 },
  381. (int[]){ 5, 6 },
  382. (int[]){ 7, 8 },
  383. (int[]){ -1, 9, 10, 11 },
  384. (int[]){ -1, 12, 13, 14 },
  385. };
  386. c->dim = dim[i];
  387. c->subclass = subclass[i];
  388. c->masterbook = masterbook[i];
  389. books = (1 << c->subclass);
  390. c->books = av_malloc(sizeof(int) * books);
  391. for (j = 0; j < books; j++) c->books[j] = nbooks[i][j];
  392. }
  393. fc->multiplier = 2;
  394. fc->rangebits = venc->blocksize[0] - 1;
  395. fc->values = 2;
  396. for (i = 0; i < fc->partitions; i++)
  397. fc->values += fc->classes[fc->partition_to_class[i]].dim;
  398. fc->list = av_malloc(sizeof(floor_entry_t) * fc->values);
  399. fc->list[0].x = 0;
  400. fc->list[1].x = 1 << fc->rangebits;
  401. for (i = 2; i < fc->values; i++) {
  402. /*int a = i - 1;
  403. int g = ilog(a);
  404. assert(g <= fc->rangebits);
  405. a ^= 1 << (g-1);
  406. g = 1 << (fc->rangebits - g);
  407. fc->list[i].x = g + a*2*g;*/
  408. //int a[] = {14, 4, 58, 2, 8, 28, 90};
  409. int a[] = {93,23,372,6,46,186,750,14,33,65,130,260,556,3,10,18,28,39,55,79,111,158,220,312,464,650,850};
  410. fc->list[i].x = a[i - 2];
  411. }
  412. ready_floor(fc);
  413. venc->nresidues = 1;
  414. venc->residues = av_malloc(sizeof(residue_t) * venc->nresidues);
  415. // single residue
  416. rc = &venc->residues[0];
  417. rc->type = 2;
  418. rc->begin = 0;
  419. rc->end = 1600;
  420. rc->partition_size = 32;
  421. rc->classifications = 10;
  422. rc->classbook = 15;
  423. rc->books = av_malloc(sizeof(int[8]) * rc->classifications);
  424. for (i = 0; i < rc->classifications; i++) {
  425. int a[10][8] = {
  426. { -1, -1, -1, -1, -1, -1, -1, -1, },
  427. { -1, -1, 16, -1, -1, -1, -1, -1, },
  428. { -1, -1, 17, -1, -1, -1, -1, -1, },
  429. { -1, -1, 18, -1, -1, -1, -1, -1, },
  430. { -1, -1, 19, -1, -1, -1, -1, -1, },
  431. { -1, -1, 20, -1, -1, -1, -1, -1, },
  432. { -1, -1, 21, -1, -1, -1, -1, -1, },
  433. { 22, 23, -1, -1, -1, -1, -1, -1, },
  434. { 24, 25, -1, -1, -1, -1, -1, -1, },
  435. { 26, 27, 28, -1, -1, -1, -1, -1, },
  436. };
  437. int j;
  438. for (j = 0; j < 8; j++) rc->books[i][j] = a[i][j];
  439. }
  440. ready_residue(rc, venc);
  441. venc->nmappings = 1;
  442. venc->mappings = av_malloc(sizeof(mapping_t) * venc->nmappings);
  443. // single mapping
  444. mc = &venc->mappings[0];
  445. mc->submaps = 1;
  446. mc->mux = av_malloc(sizeof(int) * venc->channels);
  447. for (i = 0; i < venc->channels; i++) mc->mux[i] = 0;
  448. mc->floor = av_malloc(sizeof(int) * mc->submaps);
  449. mc->residue = av_malloc(sizeof(int) * mc->submaps);
  450. for (i = 0; i < mc->submaps; i++) {
  451. mc->floor[i] = 0;
  452. mc->residue[i] = 0;
  453. }
  454. mc->coupling_steps = venc->channels == 2 ? 1 : 0;
  455. mc->magnitude = av_malloc(sizeof(int) * mc->coupling_steps);
  456. mc->angle = av_malloc(sizeof(int) * mc->coupling_steps);
  457. if (mc->coupling_steps) {
  458. mc->magnitude[0] = 0;
  459. mc->angle[0] = 1;
  460. }
  461. venc->nmodes = 1;
  462. venc->modes = av_malloc(sizeof(vorbis_mode_t) * venc->nmodes);
  463. // single mode
  464. venc->modes[0].blockflag = 0;
  465. venc->modes[0].mapping = 0;
  466. venc->have_saved = 0;
  467. venc->saved = av_malloc(sizeof(float) * venc->channels * (1 << venc->blocksize[1]) / 2);
  468. venc->samples = av_malloc(sizeof(float) * venc->channels * (1 << venc->blocksize[1]));
  469. venc->floor = av_malloc(sizeof(float) * venc->channels * (1 << venc->blocksize[1]) / 2);
  470. venc->coeffs = av_malloc(sizeof(float) * venc->channels * (1 << venc->blocksize[1]) / 2);
  471. venc->win[0] = ff_vorbis_vwin[venc->blocksize[0] - 6];
  472. venc->win[1] = ff_vorbis_vwin[venc->blocksize[1] - 6];
  473. ff_mdct_init(&venc->mdct[0], venc->blocksize[0], 0);
  474. ff_mdct_init(&venc->mdct[1], venc->blocksize[1], 0);
  475. }
  476. static void put_float(PutBitContext * pb, float f) {
  477. int exp, mant;
  478. uint32_t res = 0;
  479. mant = (int)ldexp(frexp(f, &exp), 20);
  480. exp += 788 - 20;
  481. if (mant < 0) { res |= (1 << 31); mant = -mant; }
  482. res |= mant | (exp << 21);
  483. put_bits(pb, 32, res);
  484. }
  485. static void put_codebook_header(PutBitContext * pb, codebook_t * cb) {
  486. int i;
  487. int ordered = 0;
  488. put_bits(pb, 24, 0x564342); //magic
  489. put_bits(pb, 16, cb->ndimentions);
  490. put_bits(pb, 24, cb->nentries);
  491. for (i = 1; i < cb->nentries; i++) if (cb->entries[i].len < cb->entries[i-1].len) break;
  492. if (i == cb->nentries) ordered = 1;
  493. put_bits(pb, 1, ordered);
  494. if (ordered) {
  495. int len = cb->entries[0].len;
  496. put_bits(pb, 5, len - 1);
  497. i = 0;
  498. while (i < cb->nentries) {
  499. int j;
  500. for (j = 0; j+i < cb->nentries; j++) if (cb->entries[j+i].len != len) break;
  501. put_bits(pb, ilog(cb->nentries - i), j);
  502. i += j;
  503. len++;
  504. }
  505. } else {
  506. int sparse = 0;
  507. for (i = 0; i < cb->nentries; i++) if (!cb->entries[i].len) break;
  508. if (i != cb->nentries) sparse = 1;
  509. put_bits(pb, 1, sparse);
  510. for (i = 0; i < cb->nentries; i++) {
  511. if (sparse) put_bits(pb, 1, !!cb->entries[i].len);
  512. if (cb->entries[i].len) put_bits(pb, 5, cb->entries[i].len - 1);
  513. }
  514. }
  515. put_bits(pb, 4, cb->lookup);
  516. if (cb->lookup) {
  517. int tmp = cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries);
  518. int bits = ilog(cb->quantlist[0]);
  519. for (i = 1; i < tmp; i++) bits = FFMAX(bits, ilog(cb->quantlist[i]));
  520. put_float(pb, cb->min);
  521. put_float(pb, cb->delta);
  522. put_bits(pb, 4, bits - 1);
  523. put_bits(pb, 1, cb->seq_p);
  524. for (i = 0; i < tmp; i++) put_bits(pb, bits, cb->quantlist[i]);
  525. }
  526. }
  527. static void put_floor_header(PutBitContext * pb, floor_t * fc) {
  528. int i;
  529. put_bits(pb, 16, 1); // type, only floor1 is supported
  530. put_bits(pb, 5, fc->partitions);
  531. for (i = 0; i < fc->partitions; i++) put_bits(pb, 4, fc->partition_to_class[i]);
  532. for (i = 0; i < fc->nclasses; i++) {
  533. int j, books;
  534. put_bits(pb, 3, fc->classes[i].dim - 1);
  535. put_bits(pb, 2, fc->classes[i].subclass);
  536. if (fc->classes[i].subclass) put_bits(pb, 8, fc->classes[i].masterbook);
  537. books = (1 << fc->classes[i].subclass);
  538. for (j = 0; j < books; j++) put_bits(pb, 8, fc->classes[i].books[j] + 1);
  539. }
  540. put_bits(pb, 2, fc->multiplier - 1);
  541. put_bits(pb, 4, fc->rangebits);
  542. for (i = 2; i < fc->values; i++) put_bits(pb, fc->rangebits, fc->list[i].x);
  543. }
  544. static void put_residue_header(PutBitContext * pb, residue_t * rc) {
  545. int i;
  546. put_bits(pb, 16, rc->type);
  547. put_bits(pb, 24, rc->begin);
  548. put_bits(pb, 24, rc->end);
  549. put_bits(pb, 24, rc->partition_size - 1);
  550. put_bits(pb, 6, rc->classifications - 1);
  551. put_bits(pb, 8, rc->classbook);
  552. for (i = 0; i < rc->classifications; i++) {
  553. int j, tmp = 0;
  554. for (j = 0; j < 8; j++) tmp |= (rc->books[i][j] != -1) << j;
  555. put_bits(pb, 3, tmp & 7);
  556. put_bits(pb, 1, tmp > 7);
  557. if (tmp > 7) put_bits(pb, 5, tmp >> 3);
  558. }
  559. for (i = 0; i < rc->classifications; i++) {
  560. int j;
  561. for (j = 0; j < 8; j++)
  562. if (rc->books[i][j] != -1)
  563. put_bits(pb, 8, rc->books[i][j]);
  564. }
  565. }
  566. static int put_main_header(venc_context_t * venc, uint8_t ** out) {
  567. int i;
  568. PutBitContext pb;
  569. uint8_t buffer[50000] = {0}, * p = buffer;
  570. int buffer_len = sizeof buffer;
  571. int len, hlens[3];
  572. // identification header
  573. init_put_bits(&pb, p, buffer_len);
  574. put_bits(&pb, 8, 1); //magic
  575. for (i = 0; "vorbis"[i]; i++) put_bits(&pb, 8, "vorbis"[i]);
  576. put_bits(&pb, 32, 0); // version
  577. put_bits(&pb, 8, venc->channels);
  578. put_bits(&pb, 32, venc->sample_rate);
  579. put_bits(&pb, 32, 0); // bitrate
  580. put_bits(&pb, 32, 0); // bitrate
  581. put_bits(&pb, 32, 0); // bitrate
  582. put_bits(&pb, 4, venc->blocksize[0]);
  583. put_bits(&pb, 4, venc->blocksize[1]);
  584. put_bits(&pb, 1, 1); // framing
  585. flush_put_bits(&pb);
  586. hlens[0] = (put_bits_count(&pb) + 7) / 8;
  587. buffer_len -= hlens[0];
  588. p += hlens[0];
  589. // comment header
  590. init_put_bits(&pb, p, buffer_len);
  591. put_bits(&pb, 8, 3); //magic
  592. for (i = 0; "vorbis"[i]; i++) put_bits(&pb, 8, "vorbis"[i]);
  593. put_bits(&pb, 32, 0); // vendor length TODO
  594. put_bits(&pb, 32, 0); // amount of comments
  595. put_bits(&pb, 1, 1); // framing
  596. flush_put_bits(&pb);
  597. hlens[1] = (put_bits_count(&pb) + 7) / 8;
  598. buffer_len -= hlens[1];
  599. p += hlens[1];
  600. // setup header
  601. init_put_bits(&pb, p, buffer_len);
  602. put_bits(&pb, 8, 5); //magic
  603. for (i = 0; "vorbis"[i]; i++) put_bits(&pb, 8, "vorbis"[i]);
  604. // codebooks
  605. put_bits(&pb, 8, venc->ncodebooks - 1);
  606. for (i = 0; i < venc->ncodebooks; i++) put_codebook_header(&pb, &venc->codebooks[i]);
  607. // time domain, reserved, zero
  608. put_bits(&pb, 6, 0);
  609. put_bits(&pb, 16, 0);
  610. // floors
  611. put_bits(&pb, 6, venc->nfloors - 1);
  612. for (i = 0; i < venc->nfloors; i++) put_floor_header(&pb, &venc->floors[i]);
  613. // residues
  614. put_bits(&pb, 6, venc->nresidues - 1);
  615. for (i = 0; i < venc->nresidues; i++) put_residue_header(&pb, &venc->residues[i]);
  616. // mappings
  617. put_bits(&pb, 6, venc->nmappings - 1);
  618. for (i = 0; i < venc->nmappings; i++) {
  619. mapping_t * mc = &venc->mappings[i];
  620. int j;
  621. put_bits(&pb, 16, 0); // mapping type
  622. put_bits(&pb, 1, mc->submaps > 1);
  623. if (mc->submaps > 1) put_bits(&pb, 4, mc->submaps - 1);
  624. put_bits(&pb, 1, !!mc->coupling_steps);
  625. if (mc->coupling_steps) {
  626. put_bits(&pb, 8, mc->coupling_steps - 1);
  627. for (j = 0; j < mc->coupling_steps; j++) {
  628. put_bits(&pb, ilog(venc->channels - 1), mc->magnitude[j]);
  629. put_bits(&pb, ilog(venc->channels - 1), mc->angle[j]);
  630. }
  631. }
  632. put_bits(&pb, 2, 0); // reserved
  633. if (mc->submaps > 1) for (j = 0; j < venc->channels; j++) put_bits(&pb, 4, mc->mux[j]);
  634. for (j = 0; j < mc->submaps; j++) {
  635. put_bits(&pb, 8, 0); // reserved time configuration
  636. put_bits(&pb, 8, mc->floor[j]);
  637. put_bits(&pb, 8, mc->residue[j]);
  638. }
  639. }
  640. // modes
  641. put_bits(&pb, 6, venc->nmodes - 1);
  642. for (i = 0; i < venc->nmodes; i++) {
  643. put_bits(&pb, 1, venc->modes[i].blockflag);
  644. put_bits(&pb, 16, 0); // reserved window type
  645. put_bits(&pb, 16, 0); // reserved transform type
  646. put_bits(&pb, 8, venc->modes[i].mapping);
  647. }
  648. put_bits(&pb, 1, 1); // framing
  649. flush_put_bits(&pb);
  650. hlens[2] = (put_bits_count(&pb) + 7) / 8;
  651. len = hlens[0] + hlens[1] + hlens[2];
  652. p = *out = av_mallocz(64 + len + len/255);
  653. *p++ = 2;
  654. p += av_xiphlacing(p, hlens[0]);
  655. p += av_xiphlacing(p, hlens[1]);
  656. buffer_len = 0;
  657. for (i = 0; i < 3; i++) {
  658. memcpy(p, buffer + buffer_len, hlens[i]);
  659. p += hlens[i];
  660. buffer_len += hlens[i];
  661. }
  662. return p - *out;
  663. }
  664. static float get_floor_average(floor_t * fc, float * coeffs, int i) {
  665. int begin = fc->list[fc->list[FFMAX(i-1, 0)].sort].x;
  666. int end = fc->list[fc->list[FFMIN(i+1, fc->values - 1)].sort].x;
  667. int j;
  668. float average = 0;
  669. for (j = begin; j < end; j++) average += fabs(coeffs[j]);
  670. return average / (end - begin);
  671. }
  672. static void floor_fit(venc_context_t * venc, floor_t * fc, float * coeffs, int * posts, int samples) {
  673. int range = 255 / fc->multiplier + 1;
  674. int i;
  675. float tot_average = 0.;
  676. for (i = 0; i < fc->values; i++) tot_average += get_floor_average(fc, coeffs, i);
  677. tot_average /= fc->values;
  678. tot_average /= venc->quality;
  679. for (i = 0; i < fc->values; i++) {
  680. int position = fc->list[fc->list[i].sort].x;
  681. float average = get_floor_average(fc, coeffs, i);
  682. int j;
  683. average /= pow(average, 0.5) / tot_average * pow(0.8, position/200.); // MAGIC!
  684. for (j = 0; j < range - 1; j++) if (ff_vorbis_floor1_inverse_db_table[j * fc->multiplier] > average) break;
  685. posts[fc->list[i].sort] = j;
  686. }
  687. }
  688. static int render_point(int x0, int y0, int x1, int y1, int x) {
  689. return y0 + (x - x0) * (y1 - y0) / (x1 - x0);
  690. }
  691. static void render_line(int x0, int y0, int x1, int y1, float * buf, int n) {
  692. int dy = y1 - y0;
  693. int adx = x1 - x0;
  694. int ady = FFMAX(dy, -dy);
  695. int base = dy / adx;
  696. int x = x0;
  697. int y = y0;
  698. int err = 0;
  699. int sy;
  700. if (dy < 0) sy = base - 1;
  701. else sy = base + 1;
  702. ady = ady - FFMAX(base, -base) * adx;
  703. if (x >= n) return;
  704. buf[x] = ff_vorbis_floor1_inverse_db_table[y];
  705. for (x = x0 + 1; x < x1; x++) {
  706. if (x >= n) return;
  707. err += ady;
  708. if (err >= adx) {
  709. err -= adx;
  710. y += sy;
  711. } else {
  712. y += base;
  713. }
  714. buf[x] = ff_vorbis_floor1_inverse_db_table[y];
  715. }
  716. }
  717. static void floor_encode(venc_context_t * venc, floor_t * fc, PutBitContext * pb, int * posts, float * floor, int samples) {
  718. int range = 255 / fc->multiplier + 1;
  719. int coded[fc->values]; // first 2 values are unused
  720. int i, counter;
  721. int lx, ly;
  722. put_bits(pb, 1, 1); // non zero
  723. put_bits(pb, ilog(range - 1), posts[0]);
  724. put_bits(pb, ilog(range - 1), posts[1]);
  725. coded[0] = coded[1] = 1;
  726. for (i = 2; i < fc->values; i++) {
  727. int predicted = render_point(fc->list[fc->list[i].low].x,
  728. posts[fc->list[i].low],
  729. fc->list[fc->list[i].high].x,
  730. posts[fc->list[i].high],
  731. fc->list[i].x);
  732. int highroom = range - predicted;
  733. int lowroom = predicted;
  734. int room = FFMIN(highroom, lowroom);
  735. if (predicted == posts[i]) {
  736. coded[i] = 0; // must be used later as flag!
  737. continue;
  738. } else {
  739. if (!coded[fc->list[i].low]) coded[fc->list[i].low] = -1;
  740. if (!coded[fc->list[i].high]) coded[fc->list[i].high] = -1;
  741. }
  742. if (posts[i] > predicted) {
  743. if (posts[i] - predicted > room) coded[i] = posts[i] - predicted + lowroom;
  744. else coded[i] = (posts[i] - predicted) << 1;
  745. } else {
  746. if (predicted - posts[i] > room) coded[i] = predicted - posts[i] + highroom - 1;
  747. else coded[i] = ((predicted - posts[i]) << 1) - 1;
  748. }
  749. }
  750. counter = 2;
  751. for (i = 0; i < fc->partitions; i++) {
  752. floor_class_t * c = &fc->classes[fc->partition_to_class[i]];
  753. int k, cval = 0, csub = 1<<c->subclass;
  754. if (c->subclass) {
  755. codebook_t * book = &venc->codebooks[c->masterbook];
  756. int cshift = 0;
  757. for (k = 0; k < c->dim; k++) {
  758. int l;
  759. for (l = 0; l < csub; l++) {
  760. int maxval = 1;
  761. if (c->books[l] != -1) maxval = venc->codebooks[c->books[l]].nentries;
  762. // coded could be -1, but this still works, cause thats 0
  763. if (coded[counter + k] < maxval) break;
  764. }
  765. assert(l != csub);
  766. cval |= l << cshift;
  767. cshift += c->subclass;
  768. }
  769. assert(cval < book->nentries);
  770. put_bits(pb, book->entries[cval].len, book->entries[cval].codeword);
  771. }
  772. for (k = 0; k < c->dim; k++) {
  773. int book = c->books[cval & (csub-1)];
  774. int entry = coded[counter++];
  775. cval >>= c->subclass;
  776. if (book == -1) continue;
  777. if (entry == -1) entry = 0;
  778. assert(entry < venc->codebooks[book].nentries);
  779. assert(entry >= 0);
  780. put_bits(pb, venc->codebooks[book].entries[entry].len, venc->codebooks[book].entries[entry].codeword);
  781. }
  782. }
  783. lx = 0;
  784. ly = posts[0] * fc->multiplier; // sorted 0 is still 0
  785. for (i = 1; i < fc->values; i++) {
  786. int pos = fc->list[i].sort;
  787. if (coded[pos]) {
  788. render_line(lx, ly, fc->list[pos].x, posts[pos] * fc->multiplier, floor, samples);
  789. lx = fc->list[pos].x;
  790. ly = posts[pos] * fc->multiplier;
  791. }
  792. if (lx >= samples) break;
  793. }
  794. if (lx < samples) render_line(lx, ly, samples, ly, floor, samples);
  795. }
  796. static float * put_vector(codebook_t * book, PutBitContext * pb, float * num) {
  797. int i;
  798. int entry = -1;
  799. float distance = 0;
  800. assert(book->dimentions);
  801. for (i = 0; i < book->nentries; i++) {
  802. float d = 0.;
  803. int j;
  804. if (!book->entries[i].len) continue;
  805. for (j = 0; j < book->ndimentions; j++) {
  806. float a = (book->dimentions[i * book->ndimentions + j] - num[j]);
  807. d += a*a;
  808. }
  809. if (entry == -1 || distance > d) {
  810. entry = i;
  811. distance = d;
  812. }
  813. }
  814. put_bits(pb, book->entries[entry].len, book->entries[entry].codeword);
  815. return &book->dimentions[entry * book->ndimentions];
  816. }
  817. static void residue_encode(venc_context_t * venc, residue_t * rc, PutBitContext * pb, float * coeffs, int samples, int real_ch) {
  818. int pass, i, j, p, k;
  819. int psize = rc->partition_size;
  820. int partitions = (rc->end - rc->begin) / psize;
  821. int channels = (rc->type == 2) ? 1 : real_ch;
  822. int classes[channels][partitions];
  823. int classwords = venc->codebooks[rc->classbook].ndimentions;
  824. assert(rc->type == 2);
  825. assert(real_ch == 2);
  826. for (p = 0; p < partitions; p++) {
  827. float max1 = 0., max2 = 0.;
  828. int s = rc->begin + p * psize;
  829. for (k = s; k < s + psize; k += 2) {
  830. if (fabs(coeffs[k / real_ch]) > max1) max1 = fabs(coeffs[k / real_ch]);
  831. if (fabs(coeffs[samples + k / real_ch]) > max2) max2 = fabs(coeffs[samples + k / real_ch]);
  832. }
  833. for (i = 0; i < rc->classifications - 1; i++) {
  834. if (max1 < rc->maxes[i][0] && max2 < rc->maxes[i][1]) break;
  835. }
  836. classes[0][p] = i;
  837. }
  838. for (pass = 0; pass < 8; pass++) {
  839. p = 0;
  840. while (p < partitions) {
  841. if (pass == 0) for (j = 0; j < channels; j++) {
  842. codebook_t * book = &venc->codebooks[rc->classbook];
  843. int entry = 0;
  844. for (i = 0; i < classwords; i++) {
  845. entry *= rc->classifications;
  846. entry += classes[j][p + i];
  847. }
  848. assert(entry < book->nentries);
  849. assert(entry >= 0);
  850. put_bits(pb, book->entries[entry].len, book->entries[entry].codeword);
  851. }
  852. for (i = 0; i < classwords && p < partitions; i++, p++) {
  853. for (j = 0; j < channels; j++) {
  854. int nbook = rc->books[classes[j][p]][pass];
  855. codebook_t * book = &venc->codebooks[nbook];
  856. float * buf = coeffs + samples*j + rc->begin + p*psize;
  857. if (nbook == -1) continue;
  858. assert(rc->type == 0 || rc->type == 2);
  859. assert(!(psize % book->ndimentions));
  860. if (rc->type == 0) {
  861. for (k = 0; k < psize; k += book->ndimentions) {
  862. float * a = put_vector(book, pb, &buf[k]);
  863. int l;
  864. for (l = 0; l < book->ndimentions; l++) buf[k + l] -= a[l];
  865. }
  866. } else {
  867. for (k = 0; k < psize; k += book->ndimentions) {
  868. int dim = book->ndimentions, s = rc->begin + p * psize + k, l;
  869. float vec[dim], * a = vec;
  870. for (l = s; l < s + dim; l++)
  871. *a++ = coeffs[(l % real_ch) * samples + l / real_ch];
  872. a = put_vector(book, pb, vec);
  873. for (l = s; l < s + dim; l++)
  874. coeffs[(l % real_ch) * samples + l / real_ch] -= *a++;
  875. }
  876. }
  877. }
  878. }
  879. }
  880. }
  881. }
  882. static int window(venc_context_t * venc, signed short * audio, int samples) {
  883. int i, j, channel;
  884. const float * win = venc->win[0];
  885. int window_len = 1 << (venc->blocksize[0] - 1);
  886. float n = (float)(1 << venc->blocksize[0]) / 4.;
  887. // FIXME use dsp
  888. if (!venc->have_saved && !samples) return 0;
  889. if (venc->have_saved) {
  890. for (channel = 0; channel < venc->channels; channel++) {
  891. memcpy(venc->samples + channel*window_len*2, venc->saved + channel*window_len, sizeof(float)*window_len);
  892. }
  893. } else {
  894. for (channel = 0; channel < venc->channels; channel++) {
  895. memset(venc->samples + channel*window_len*2, 0, sizeof(float)*window_len);
  896. }
  897. }
  898. if (samples) {
  899. for (channel = 0; channel < venc->channels; channel++) {
  900. float * offset = venc->samples + channel*window_len*2 + window_len;
  901. j = channel;
  902. for (i = 0; i < samples; i++, j += venc->channels)
  903. offset[i] = audio[j] / 32768. / n * win[window_len - i - 1];
  904. }
  905. } else {
  906. for (channel = 0; channel < venc->channels; channel++) {
  907. memset(venc->samples + channel*window_len*2 + window_len, 0, sizeof(float)*window_len);
  908. }
  909. }
  910. for (channel = 0; channel < venc->channels; channel++) {
  911. ff_mdct_calc(&venc->mdct[0], venc->coeffs + channel*window_len, venc->samples + channel*window_len*2, venc->floor/*tmp*/);
  912. }
  913. if (samples) {
  914. for (channel = 0; channel < venc->channels; channel++) {
  915. float * offset = venc->saved + channel*window_len;
  916. j = channel;
  917. for (i = 0; i < samples; i++, j += venc->channels)
  918. offset[i] = audio[j] / 32768. / n * win[i];
  919. }
  920. venc->have_saved = 1;
  921. } else {
  922. venc->have_saved = 0;
  923. }
  924. return 1;
  925. }
  926. static int vorbis_encode_init(AVCodecContext * avccontext)
  927. {
  928. venc_context_t * venc = avccontext->priv_data;
  929. create_vorbis_context(venc, avccontext);
  930. if (avccontext->flags & CODEC_FLAG_QSCALE) venc->quality = avccontext->global_quality / (float)FF_QP2LAMBDA;
  931. else venc->quality = 0.17;
  932. //if(avccontext->cutoff > 0) cfreq = avccontext->cutoff / 1000.0;
  933. avccontext->extradata_size = put_main_header(venc, (uint8_t**)&avccontext->extradata);
  934. avccontext->frame_size = 1 << (venc->blocksize[0] - 1);
  935. avccontext->coded_frame = avcodec_alloc_frame();
  936. avccontext->coded_frame->key_frame = 1;
  937. return 0;
  938. }
  939. static int vorbis_encode_frame(AVCodecContext * avccontext, unsigned char * packets, int buf_size, void *data)
  940. {
  941. venc_context_t * venc = avccontext->priv_data;
  942. signed short * audio = data;
  943. int samples = data ? avccontext->frame_size : 0;
  944. vorbis_mode_t * mode;
  945. mapping_t * mapping;
  946. PutBitContext pb;
  947. int i;
  948. if (!window(venc, audio, samples)) return 0;
  949. samples = 1 << (venc->blocksize[0] - 1);
  950. init_put_bits(&pb, packets, buf_size);
  951. put_bits(&pb, 1, 0); // magic bit
  952. put_bits(&pb, ilog(venc->nmodes - 1), 0); // 0 bits, the mode
  953. mode = &venc->modes[0];
  954. mapping = &venc->mappings[mode->mapping];
  955. if (mode->blockflag) {
  956. put_bits(&pb, 1, 0);
  957. put_bits(&pb, 1, 0);
  958. }
  959. for (i = 0; i < venc->channels; i++) {
  960. floor_t * fc = &venc->floors[mapping->floor[mapping->mux[i]]];
  961. int posts[fc->values];
  962. floor_fit(venc, fc, &venc->coeffs[i * samples], posts, samples);
  963. floor_encode(venc, fc, &pb, posts, &venc->floor[i * samples], samples);
  964. }
  965. for (i = 0; i < venc->channels; i++) {
  966. int j;
  967. for (j = 0; j < samples; j++) {
  968. venc->coeffs[i * samples + j] /= venc->floor[i * samples + j];
  969. }
  970. }
  971. for (i = 0; i < mapping->coupling_steps; i++) {
  972. float * mag = venc->coeffs + mapping->magnitude[i] * samples;
  973. float * ang = venc->coeffs + mapping->angle[i] * samples;
  974. int j;
  975. for (j = 0; j < samples; j++) {
  976. float m = mag[j];
  977. float a = ang[j];
  978. if (m > 0) {
  979. ang[j] = m - a;
  980. if (a > m) mag[j] = a;
  981. else mag[j] = m;
  982. } else {
  983. ang[j] = a - m;
  984. if (a > m) mag[j] = m;
  985. else mag[j] = a;
  986. }
  987. }
  988. }
  989. residue_encode(venc, &venc->residues[mapping->residue[mapping->mux[0]]], &pb, venc->coeffs, samples, venc->channels);
  990. return (put_bits_count(&pb) + 7) / 8;
  991. }
  992. static int vorbis_encode_close(AVCodecContext * avccontext)
  993. {
  994. venc_context_t * venc = avccontext->priv_data;
  995. int i;
  996. if (venc->codebooks) for (i = 0; i < venc->ncodebooks; i++) {
  997. av_freep(&venc->codebooks[i].entries);
  998. av_freep(&venc->codebooks[i].quantlist);
  999. av_freep(&venc->codebooks[i].dimentions);
  1000. }
  1001. av_freep(&venc->codebooks);
  1002. if (venc->floors) for (i = 0; i < venc->nfloors; i++) {
  1003. int j;
  1004. av_freep(&venc->floors[i].classes);
  1005. if (venc->floors[i].classes)
  1006. for (j = 0; j < venc->floors[i].nclasses; j++)
  1007. av_freep(&venc->floors[i].classes[j].books);
  1008. av_freep(&venc->floors[i].partition_to_class);
  1009. av_freep(&venc->floors[i].list);
  1010. }
  1011. av_freep(&venc->floors);
  1012. if (venc->residues) for (i = 0; i < venc->nresidues; i++) {
  1013. av_freep(&venc->residues[i].books);
  1014. av_freep(&venc->residues[i].maxes);
  1015. }
  1016. av_freep(&venc->residues);
  1017. if (venc->mappings) for (i = 0; i < venc->nmappings; i++) {
  1018. av_freep(&venc->mappings[i].mux);
  1019. av_freep(&venc->mappings[i].floor);
  1020. av_freep(&venc->mappings[i].residue);
  1021. }
  1022. av_freep(&venc->mappings);
  1023. av_freep(&venc->modes);
  1024. av_freep(&venc->saved);
  1025. av_freep(&venc->samples);
  1026. av_freep(&venc->floor);
  1027. av_freep(&venc->coeffs);
  1028. ff_mdct_end(&venc->mdct[0]);
  1029. ff_mdct_end(&venc->mdct[1]);
  1030. av_freep(&avccontext->coded_frame);
  1031. av_freep(&avccontext->extradata);
  1032. return 0 ;
  1033. }
  1034. AVCodec vorbis_encoder = {
  1035. "vorbis",
  1036. CODEC_TYPE_AUDIO,
  1037. CODEC_ID_VORBIS,
  1038. sizeof(venc_context_t),
  1039. vorbis_encode_init,
  1040. vorbis_encode_frame,
  1041. vorbis_encode_close,
  1042. .capabilities= CODEC_CAP_DELAY,
  1043. };