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.

1511 lines
55KB

  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. typedef struct {
  29. int nentries;
  30. uint8_t * lens;
  31. uint32_t * codewords;
  32. int ndimentions;
  33. float min;
  34. float delta;
  35. int seq_p;
  36. int lookup;
  37. int * quantlist;
  38. float * dimentions;
  39. } codebook_t;
  40. typedef struct {
  41. int dim;
  42. int subclass;
  43. int masterbook;
  44. int * books;
  45. } floor_class_t;
  46. typedef struct {
  47. int partitions;
  48. int * partition_to_class;
  49. int nclasses;
  50. floor_class_t * classes;
  51. int multiplier;
  52. int rangebits;
  53. int values;
  54. floor1_entry_t * list;
  55. } floor_t;
  56. typedef struct {
  57. int type;
  58. int begin;
  59. int end;
  60. int partition_size;
  61. int classifications;
  62. int classbook;
  63. int8_t (*books)[8];
  64. float (*maxes)[2];
  65. } residue_t;
  66. typedef struct {
  67. int submaps;
  68. int * mux;
  69. int * floor;
  70. int * residue;
  71. int coupling_steps;
  72. int * magnitude;
  73. int * angle;
  74. } mapping_t;
  75. typedef struct {
  76. int blockflag;
  77. int mapping;
  78. } vorbis_mode_t;
  79. typedef struct {
  80. int channels;
  81. int sample_rate;
  82. int blocksize[2]; // in (1<<n) format
  83. MDCTContext mdct[2];
  84. const float * win[2];
  85. int have_saved;
  86. float * saved;
  87. float * samples;
  88. float * floor; // also used for tmp values for mdct
  89. float * coeffs; // also used for residue after floor
  90. float quality;
  91. int ncodebooks;
  92. codebook_t * codebooks;
  93. int nfloors;
  94. floor_t * floors;
  95. int nresidues;
  96. residue_t * residues;
  97. int nmappings;
  98. mapping_t * mappings;
  99. int nmodes;
  100. vorbis_mode_t * modes;
  101. } venc_context_t;
  102. typedef struct {
  103. int total;
  104. int total_pos;
  105. int pos;
  106. uint8_t * buf_ptr;
  107. } PutBitContext;
  108. static const uint8_t codebook0[] = {
  109. 2, 10, 8, 14, 7, 12, 11, 14, 1, 5, 3, 7, 4, 9, 7,
  110. 13,
  111. };
  112. static const uint8_t codebook1[] = {
  113. 1, 4, 2, 6, 3, 7, 5, 7,
  114. };
  115. static const uint8_t codebook2[] = {
  116. 1, 5, 7, 21, 5, 8, 9, 21, 10, 9, 12, 20, 20, 16, 20,
  117. 20, 4, 8, 9, 20, 6, 8, 9, 20, 11, 11, 13, 20, 20, 15,
  118. 17, 20, 9, 11, 14, 20, 8, 10, 15, 20, 11, 13, 15, 20, 20,
  119. 20, 20, 20, 20, 20, 20, 20, 13, 20, 20, 20, 18, 18, 20, 20,
  120. 20, 20, 20, 20, 3, 6, 8, 20, 6, 7, 9, 20, 10, 9, 12,
  121. 20, 20, 20, 20, 20, 5, 7, 9, 20, 6, 6, 9, 20, 10, 9,
  122. 12, 20, 20, 20, 20, 20, 8, 10, 13, 20, 8, 9, 12, 20, 11,
  123. 10, 12, 20, 20, 20, 20, 20, 18, 20, 20, 20, 15, 17, 18, 20,
  124. 18, 17, 18, 20, 20, 20, 20, 20, 7, 10, 12, 20, 8, 9, 11,
  125. 20, 14, 13, 14, 20, 20, 20, 20, 20, 6, 9, 12, 20, 7, 8,
  126. 11, 20, 12, 11, 13, 20, 20, 20, 20, 20, 9, 11, 15, 20, 8,
  127. 10, 14, 20, 12, 11, 14, 20, 20, 20, 20, 20, 20, 20, 20, 20,
  128. 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 11, 16, 18,
  129. 20, 15, 15, 17, 20, 20, 17, 20, 20, 20, 20, 20, 20, 9, 14,
  130. 16, 20, 12, 12, 15, 20, 17, 15, 18, 20, 20, 20, 20, 20, 16,
  131. 19, 18, 20, 15, 16, 20, 20, 17, 17, 20, 20, 20, 20, 20, 20,
  132. 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
  133. 20,
  134. };
  135. static const uint8_t codebook3[] = {
  136. 2, 3, 7, 13, 4, 4, 7, 15, 8, 6, 9, 17, 21, 16, 15,
  137. 21, 2, 5, 7, 11, 5, 5, 7, 14, 9, 7, 10, 16, 17, 15,
  138. 16, 21, 4, 7, 10, 17, 7, 7, 9, 15, 11, 9, 11, 16, 21,
  139. 18, 15, 21, 18, 21, 21, 21, 15, 17, 17, 19, 21, 19, 18, 20,
  140. 21, 21, 21, 20,
  141. };
  142. static const uint8_t codebook4[] = {
  143. 5, 5, 5, 5, 6, 5, 6, 5, 6, 5, 6, 5, 6, 5, 6,
  144. 5, 6, 5, 6, 5, 6, 5, 6, 5, 7, 5, 7, 5, 7, 5,
  145. 7, 5, 8, 6, 8, 6, 8, 6, 9, 6, 9, 6, 10, 6, 10,
  146. 6, 11, 6, 11, 7, 11, 7, 12, 7, 12, 7, 12, 7, 12, 7,
  147. 12, 7, 12, 7, 12, 7, 12, 8, 13, 8, 12, 8, 12, 8, 13,
  148. 8, 13, 9, 13, 9, 13, 9, 13, 9, 12, 10, 12, 10, 13, 10,
  149. 14, 11, 14, 12, 14, 13, 14, 13, 14, 14, 15, 16, 15, 15, 15,
  150. 14, 15, 17, 21, 22, 22, 21, 22, 22, 22, 22, 22, 22, 21, 21,
  151. 21, 21, 21, 21, 21, 21, 21, 21,
  152. };
  153. static const uint8_t codebook5[] = {
  154. 2, 5, 5, 4, 5, 4, 5, 4, 5, 4, 6, 5, 6, 5, 6,
  155. 5, 6, 5, 7, 5, 7, 6, 8, 6, 8, 6, 8, 6, 9, 6,
  156. 9, 6,
  157. };
  158. static const uint8_t codebook6[] = {
  159. 8, 5, 8, 4, 9, 4, 9, 4, 9, 4, 9, 4, 9, 4, 9,
  160. 4, 9, 4, 9, 4, 9, 4, 8, 4, 8, 4, 9, 5, 9, 5,
  161. 9, 5, 9, 5, 9, 6, 10, 6, 10, 7, 10, 8, 11, 9, 11,
  162. 11, 12, 13, 12, 14, 13, 15, 13, 15, 14, 16, 14, 17, 15, 17,
  163. 15, 15, 16, 16, 15, 16, 16, 16, 15, 18, 16, 15, 17, 17, 19,
  164. 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
  165. 19, 19, 19, 19, 19, 19,
  166. };
  167. static const uint8_t codebook7[] = {
  168. 1, 5, 5, 5, 5, 5, 5, 5, 6, 5, 6, 5, 6, 5, 6,
  169. 5, 6, 6, 7, 7, 7, 7, 8, 7, 8, 8, 9, 8, 10, 9,
  170. 10, 9,
  171. };
  172. static const uint8_t codebook8[] = {
  173. 4, 3, 4, 3, 4, 4, 5, 4, 5, 4, 5, 5, 6, 5, 6,
  174. 5, 7, 5, 7, 6, 7, 6, 8, 7, 8, 7, 8, 7, 9, 8,
  175. 9, 9, 9, 9, 10, 10, 10, 11, 9, 12, 9, 12, 9, 15, 10,
  176. 14, 9, 13, 10, 13, 10, 12, 10, 12, 10, 13, 10, 12, 11, 13,
  177. 11, 14, 12, 13, 13, 14, 14, 13, 14, 15, 14, 16, 13, 13, 14,
  178. 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
  179. 16, 16, 16, 16, 15, 15,
  180. };
  181. static const uint8_t codebook9[] = {
  182. 4, 5, 4, 5, 3, 5, 3, 5, 3, 5, 4, 4, 4, 4, 5,
  183. 5, 5,
  184. };
  185. static const uint8_t codebook10[] = {
  186. 3, 3, 4, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 5,
  187. 7, 5, 8, 6, 8, 6, 9, 7, 10, 7, 10, 8, 10, 8, 11,
  188. 9, 11,
  189. };
  190. static const uint8_t codebook11[] = {
  191. 3, 7, 3, 8, 3, 10, 3, 8, 3, 9, 3, 8, 4, 9, 4,
  192. 9, 5, 9, 6, 10, 6, 9, 7, 11, 7, 12, 9, 13, 10, 13,
  193. 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
  194. 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
  195. 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
  196. 12, 12, 12,
  197. };
  198. static const uint8_t codebook12[] = {
  199. 4, 5, 4, 5, 4, 5, 4, 5, 3, 5, 3, 5, 3, 5, 4,
  200. 5, 4,
  201. };
  202. static const uint8_t codebook13[] = {
  203. 4, 2, 4, 2, 5, 3, 5, 4, 6, 6, 6, 7, 7, 8, 7,
  204. 8, 7, 8, 7, 9, 8, 9, 8, 9, 8, 10, 8, 11, 9, 12,
  205. 9, 12,
  206. };
  207. static const uint8_t codebook14[] = {
  208. 2, 5, 2, 6, 3, 6, 4, 7, 4, 7, 5, 9, 5, 11, 6,
  209. 11, 6, 11, 7, 11, 6, 11, 6, 11, 9, 11, 8, 11, 11, 11,
  210. 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
  211. 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
  212. 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 10, 10, 10,
  213. 10, 10, 10,
  214. };
  215. static const uint8_t codebook15[] = {
  216. 5, 6, 11, 11, 11, 11, 10, 10, 12, 11, 5, 2, 11, 5, 6,
  217. 6, 7, 9, 11, 13, 13, 10, 7, 11, 6, 7, 8, 9, 10, 12,
  218. 11, 5, 11, 6, 8, 7, 9, 11, 14, 15, 11, 6, 6, 8, 4,
  219. 5, 7, 8, 10, 13, 10, 5, 7, 7, 5, 5, 6, 8, 10, 11,
  220. 10, 7, 7, 8, 6, 5, 5, 7, 9, 9, 11, 8, 8, 11, 8,
  221. 7, 6, 6, 7, 9, 12, 11, 10, 13, 9, 9, 7, 7, 7, 9,
  222. 11, 13, 12, 15, 12, 11, 9, 8, 8, 8,
  223. };
  224. static const uint8_t codebook16[] = {
  225. 2, 4, 4, 0, 0, 0, 0, 0, 0, 5, 6, 6, 0, 0, 0,
  226. 0, 0, 0, 5, 6, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  227. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  228. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  229. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  230. 0, 0, 0, 0, 0, 0, 5, 7, 7, 0, 0, 0, 0, 0, 0,
  231. 7, 8, 8, 0, 0, 0, 0, 0, 0, 6, 7, 8, 0, 0, 0,
  232. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  233. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  234. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  235. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 7, 7,
  236. 0, 0, 0, 0, 0, 0, 6, 8, 7, 0, 0, 0, 0, 0, 0,
  237. 7, 8, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  238. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  239. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  370. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 4, 6, 6,
  371. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  372. 0, 0, 0, 0, 0, 4, 4, 4, 6, 6, 0, 0, 0, 0, 0,
  373. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  374. 6, 6, 6, 9, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  375. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 6, 7, 9, 9,
  376. };
  377. static const uint8_t codebook19[] = {
  378. 2, 3, 3, 6, 6, 0, 0, 0, 0, 0, 4, 4, 6, 6, 0,
  379. 0, 0, 0, 0, 4, 4, 6, 6, 0, 0, 0, 0, 0, 5, 5,
  380. 6, 6, 0, 0, 0, 0, 0, 0, 0, 6, 6, 0, 0, 0, 0,
  381. 0, 0, 0, 7, 8, 0, 0, 0, 0, 0, 0, 0, 7, 7, 0,
  382. 0, 0, 0, 0, 0, 0, 9, 9,
  383. };
  384. static const uint8_t codebook20[] = {
  385. 1, 3, 4, 6, 6, 7, 7, 9, 9, 0, 5, 5, 7, 7, 7,
  386. 8, 9, 9, 0, 5, 5, 7, 7, 8, 8, 9, 9, 0, 7, 7,
  387. 8, 8, 8, 8, 10, 10, 0, 0, 0, 8, 8, 8, 8, 10, 10,
  388. 0, 0, 0, 9, 9, 9, 9, 10, 10, 0, 0, 0, 9, 9, 9,
  389. 9, 10, 10, 0, 0, 0, 10, 10, 10, 10, 11, 11, 0, 0, 0,
  390. 0, 0, 10, 10, 11, 11,
  391. };
  392. static const uint8_t codebook21[] = {
  393. 2, 3, 3, 6, 6, 7, 7, 8, 8, 8, 8, 9, 9, 10, 10,
  394. 11, 10, 0, 5, 5, 7, 7, 8, 8, 9, 9, 9, 9, 10, 10,
  395. 10, 10, 11, 11, 0, 5, 5, 7, 7, 8, 8, 9, 9, 9, 9,
  396. 10, 10, 10, 10, 11, 11, 0, 6, 6, 7, 7, 8, 8, 9, 9,
  397. 9, 9, 10, 10, 11, 11, 11, 11, 0, 0, 0, 7, 7, 8, 8,
  398. 9, 9, 9, 9, 10, 10, 11, 11, 11, 12, 0, 0, 0, 8, 8,
  399. 8, 8, 9, 9, 9, 9, 10, 10, 11, 11, 12, 12, 0, 0, 0,
  400. 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 11, 11, 12, 12, 0,
  401. 0, 0, 9, 9, 9, 9, 10, 10, 10, 10, 11, 10, 11, 11, 12,
  402. 12, 0, 0, 0, 0, 0, 9, 9, 10, 10, 10, 10, 11, 11, 11,
  403. 11, 12, 12, 0, 0, 0, 0, 0, 9, 8, 9, 9, 10, 10, 11,
  404. 11, 12, 12, 12, 12, 0, 0, 0, 0, 0, 8, 8, 9, 9, 10,
  405. 10, 11, 11, 12, 11, 12, 12, 0, 0, 0, 0, 0, 9, 10, 10,
  406. 10, 11, 11, 11, 11, 12, 12, 13, 13, 0, 0, 0, 0, 0, 0,
  407. 0, 10, 10, 10, 10, 11, 11, 12, 12, 13, 13, 0, 0, 0, 0,
  408. 0, 0, 0, 11, 11, 11, 11, 12, 12, 12, 12, 13, 13, 0, 0,
  409. 0, 0, 0, 0, 0, 11, 11, 11, 11, 12, 12, 12, 12, 13, 13,
  410. 0, 0, 0, 0, 0, 0, 0, 11, 11, 12, 12, 12, 12, 13, 13,
  411. 13, 13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 12, 12, 12,
  412. 13, 13, 13, 13,
  413. };
  414. static const uint8_t codebook22[] = {
  415. 1, 4, 4, 7, 6, 6, 7, 6, 6, 4, 7, 7, 10, 9, 9,
  416. 11, 9, 9, 4, 7, 7, 10, 9, 9, 11, 9, 9, 7, 10, 10,
  417. 11, 11, 10, 12, 11, 11, 6, 9, 9, 11, 10, 10, 11, 10, 10,
  418. 6, 9, 9, 11, 10, 10, 11, 10, 10, 7, 11, 11, 11, 11, 11,
  419. 12, 11, 11, 6, 9, 9, 11, 10, 10, 11, 10, 10, 6, 9, 9,
  420. 11, 10, 10, 11, 10, 10,
  421. };
  422. static const uint8_t codebook23[] = {
  423. 2, 4, 4, 6, 6, 7, 7, 7, 7, 8, 8, 10, 5, 5, 6,
  424. 6, 7, 7, 8, 8, 8, 8, 10, 5, 5, 6, 6, 7, 7, 8,
  425. 8, 8, 8, 10, 6, 6, 7, 7, 8, 8, 8, 8, 8, 8, 10,
  426. 10, 10, 7, 7, 8, 7, 8, 8, 8, 8, 10, 10, 10, 8, 8,
  427. 8, 8, 8, 8, 8, 8, 10, 10, 10, 7, 8, 8, 8, 8, 8,
  428. 8, 8, 10, 10, 10, 8, 8, 8, 8, 8, 8, 8, 8, 10, 10,
  429. 10, 10, 10, 8, 8, 8, 8, 8, 8, 10, 10, 10, 10, 10, 9,
  430. 9, 8, 8, 9, 8, 10, 10, 10, 10, 10, 8, 8, 8, 8, 8,
  431. 8,
  432. };
  433. static const uint8_t codebook24[] = {
  434. 1, 4, 4, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 6, 5,
  435. 5, 7, 7, 8, 8, 8, 8, 9, 9, 10, 10, 7, 5, 5, 7,
  436. 7, 8, 8, 8, 8, 9, 9, 11, 10, 0, 8, 8, 8, 8, 9,
  437. 9, 9, 9, 10, 10, 11, 11, 0, 8, 8, 8, 8, 9, 9, 9,
  438. 9, 10, 10, 11, 11, 0, 12, 12, 9, 9, 10, 10, 10, 10, 11,
  439. 11, 11, 12, 0, 13, 13, 9, 9, 10, 10, 10, 10, 11, 11, 12,
  440. 12, 0, 0, 0, 10, 10, 10, 10, 11, 11, 12, 12, 12, 12, 0,
  441. 0, 0, 10, 10, 10, 10, 11, 11, 12, 12, 12, 12, 0, 0, 0,
  442. 14, 14, 11, 11, 11, 11, 12, 12, 13, 13, 0, 0, 0, 14, 14,
  443. 11, 11, 11, 11, 12, 12, 13, 13, 0, 0, 0, 0, 0, 12, 12,
  444. 12, 12, 13, 13, 14, 13, 0, 0, 0, 0, 0, 13, 13, 12, 12,
  445. 13, 12, 14, 13,
  446. };
  447. static const uint8_t codebook25[] = {
  448. 2, 4, 4, 5, 5, 6, 5, 5, 5, 5, 6, 4, 5, 5, 5,
  449. 6, 5, 5, 5, 5, 6, 6, 6, 5, 5,
  450. };
  451. static const uint8_t codebook26[] = {
  452. 1, 4, 4, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 4, 9,
  453. 8, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 2, 9, 7, 12,
  454. 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
  455. 12, 12, 12, 12, 12, 12, 12, 12, 12, 11, 12, 12, 12, 12, 12,
  456. 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
  457. 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
  458. 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
  459. 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
  460. 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
  461. 12, 12, 12, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
  462. 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
  463. 11, 11, 11, 11,
  464. };
  465. static const uint8_t codebook27[] = {
  466. 1, 4, 4, 6, 6, 7, 7, 8, 7, 9, 9, 10, 10, 10, 10,
  467. 6, 5, 5, 7, 7, 8, 8, 10, 8, 11, 10, 12, 12, 13, 13,
  468. 6, 5, 5, 7, 7, 8, 8, 10, 9, 11, 11, 12, 12, 13, 12,
  469. 18, 8, 8, 8, 8, 9, 9, 10, 9, 11, 10, 12, 12, 13, 13,
  470. 18, 8, 8, 8, 8, 9, 9, 10, 10, 11, 11, 13, 12, 14, 13,
  471. 18, 11, 11, 9, 9, 10, 10, 11, 11, 11, 12, 13, 12, 13, 14,
  472. 18, 11, 11, 9, 8, 11, 10, 11, 11, 11, 11, 12, 12, 14, 13,
  473. 18, 18, 18, 10, 11, 10, 11, 12, 12, 12, 12, 13, 12, 14, 13,
  474. 18, 18, 18, 10, 11, 11, 9, 12, 11, 12, 12, 12, 13, 13, 13,
  475. 18, 18, 17, 14, 14, 11, 11, 12, 12, 13, 12, 14, 12, 14, 13,
  476. 18, 18, 18, 14, 14, 11, 10, 12, 9, 12, 13, 13, 13, 13, 13,
  477. 18, 18, 17, 16, 18, 13, 13, 12, 12, 13, 11, 14, 12, 14, 14,
  478. 17, 18, 18, 17, 18, 13, 12, 13, 10, 12, 11, 14, 14, 14, 14,
  479. 17, 18, 18, 18, 18, 15, 16, 12, 12, 13, 10, 14, 12, 14, 15,
  480. 18, 18, 18, 16, 17, 16, 14, 12, 11, 13, 10, 13, 13, 14, 15,
  481. };
  482. static const uint8_t codebook28[] = {
  483. 2, 5, 5, 6, 6, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8,
  484. 8, 8, 10, 6, 6, 7, 7, 8, 7, 8, 8, 8, 8, 8, 9,
  485. 9, 9, 9, 9, 10, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8,
  486. 9, 9, 9, 9, 9, 9, 10, 7, 7, 7, 7, 8, 8, 8, 8,
  487. 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 7, 7, 8, 8,
  488. 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 11, 11, 11, 8, 8,
  489. 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10,
  490. 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10,
  491. 10, 10, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10,
  492. 9, 10, 10, 10, 11, 11, 9, 9, 9, 9, 9, 9, 9, 9, 9,
  493. 9, 9, 9, 11, 10, 11, 11, 11, 9, 9, 9, 9, 9, 9, 10,
  494. 10, 9, 9, 10, 9, 11, 10, 11, 11, 11, 9, 9, 9, 9, 9,
  495. 9, 9, 9, 10, 10, 10, 9, 11, 11, 11, 11, 11, 9, 9, 9,
  496. 9, 10, 10, 9, 9, 9, 9, 10, 9, 11, 11, 11, 11, 11, 11,
  497. 11, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11,
  498. 11, 11, 11, 10, 9, 10, 10, 9, 10, 9, 9, 10, 9, 11, 10,
  499. 10, 11, 11, 11, 11, 9, 10, 9, 9, 9, 9, 10, 10, 10, 10,
  500. 11, 11, 11, 11, 11, 11, 10, 10, 10, 9, 9, 10, 9, 10, 9,
  501. 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 9, 9, 9, 9,
  502. 9, 10, 10, 10,
  503. };
  504. static const struct {
  505. int dim;
  506. int len;
  507. int real_len;
  508. const uint8_t * clens;
  509. int lookup;
  510. float min;
  511. float delta;
  512. const uint8_t * quant;
  513. } cvectors[] = {
  514. { 2, 16, 16, codebook0, 0 },
  515. { 2, 8, 8, codebook1, 0 },
  516. { 2, 256, 256, codebook2, 0 },
  517. { 2, 64, 64, codebook3, 0 },
  518. { 2, 128, 128, codebook4, 0 },
  519. { 2, 32, 32, codebook5, 0 },
  520. { 2, 96, 96, codebook6, 0 },
  521. { 2, 32, 32, codebook7, 0 },
  522. { 2, 96, 96, codebook8, 0 },
  523. { 2, 17, 17, codebook9, 0 },
  524. { 2, 32, 32, codebook10, 0 },
  525. { 2, 78, 78, codebook11, 0 },
  526. { 2, 17, 17, codebook12, 0 },
  527. { 2, 32, 32, codebook13, 0 },
  528. { 2, 78, 78, codebook14, 0 },
  529. { 2, 100, 100, codebook15, 0 },
  530. { 8, 1641, 6561, codebook16, 1, -1.0, 1.0, (const uint8_t[]){ 1, 0, 2, } },
  531. { 4, 443, 625, codebook17, 1, -2.0, 1.0, (const uint8_t[]){ 2, 1, 3, 0, 4, } },
  532. { 4, 105, 625, codebook18, 1, -2.0, 1.0, (const uint8_t[]){ 2, 1, 3, 0, 4, } },
  533. { 2, 68, 81, codebook19, 1, -4.0, 1.0, (const uint8_t[]){ 4, 3, 5, 2, 6, 1, 7, 0, 8, } },
  534. { 2, 81, 81, codebook20, 1, -4.0, 1.0, (const uint8_t[]){ 4, 3, 5, 2, 6, 1, 7, 0, 8, } },
  535. { 2, 289, 289, codebook21, 1, -8.0, 1.0, (const uint8_t[]){ 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15, 0, 16, } },
  536. { 4, 81, 81, codebook22, 1, -11.0, 11.0, (const uint8_t[]){ 1, 0, 2, } },
  537. { 2, 121, 121, codebook23, 1, -5.0, 1.0, (const uint8_t[]){ 5, 4, 6, 3, 7, 2, 8, 1, 9, 0, 10, } },
  538. { 2, 169, 169, codebook24, 1, -30.0, 5.0, (const uint8_t[]){ 6, 5, 7, 4, 8, 3, 9, 2, 10, 1, 11, 0, 12, } },
  539. { 2, 25, 25, codebook25, 1, -2.0, 1.0, (const uint8_t[]){ 2, 1, 3, 0, 4, } },
  540. { 2, 169, 169, codebook26, 1, -1530.0, 255.0, (const uint8_t[]){ 6, 5, 7, 4, 8, 3, 9, 2, 10, 1, 11, 0, 12, } },
  541. { 2, 225, 225, codebook27, 1, -119.0, 17.0, (const uint8_t[]){ 7, 6, 8, 5, 9, 4, 10, 3, 11, 2, 12, 1, 13, 0, 14, } },
  542. { 2, 289, 289, codebook28, 1, -8.0, 1.0, (const uint8_t[]){ 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15, 0, 16, } },
  543. };
  544. static inline void init_put_bits(PutBitContext * pb, uint8_t * buf, int buffer_len) {
  545. pb->total = buffer_len * 8;
  546. pb->total_pos = 0;
  547. pb->pos = 0;
  548. pb->buf_ptr = buf;
  549. }
  550. static void put_bits(PutBitContext * pb, int bits, uint64_t val) {
  551. if ((pb->total_pos += bits) >= pb->total) return;
  552. if (!bits) return;
  553. if (pb->pos) {
  554. if (pb->pos > bits) {
  555. *pb->buf_ptr |= val << (8 - pb->pos);
  556. pb->pos -= bits;
  557. bits = 0;
  558. } else {
  559. *pb->buf_ptr++ |= (val << (8 - pb->pos)) & 0xFF;
  560. val >>= pb->pos;
  561. bits -= pb->pos;
  562. pb->pos = 0;
  563. }
  564. }
  565. for (; bits >= 8; bits -= 8) {
  566. *pb->buf_ptr++ = val & 0xFF;
  567. val >>= 8;
  568. }
  569. if (bits) {
  570. *pb->buf_ptr = val;
  571. pb->pos = 8 - bits;
  572. }
  573. }
  574. static inline void flush_put_bits(PutBitContext * pb) {
  575. }
  576. static inline int put_bits_count(PutBitContext * pb) {
  577. return pb->total_pos;
  578. }
  579. static inline void put_codeword(PutBitContext * pb, codebook_t * cb, int entry) {
  580. assert(entry >= 0);
  581. assert(entry < cb->nentries);
  582. assert(cb->lens[entry]);
  583. put_bits(pb, cb->lens[entry], cb->codewords[entry]);
  584. }
  585. static int cb_lookup_vals(int lookup, int dimentions, int entries) {
  586. if (lookup == 1) return ff_vorbis_nth_root(entries, dimentions);
  587. else if (lookup == 2) return dimentions * entries;
  588. return 0;
  589. }
  590. static void ready_codebook(codebook_t * cb) {
  591. int i;
  592. ff_vorbis_len2vlc(cb->lens, cb->codewords, cb->nentries);
  593. if (!cb->lookup) cb->dimentions = NULL;
  594. else {
  595. int vals = cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries);
  596. cb->dimentions = av_malloc(sizeof(float) * cb->nentries * cb->ndimentions);
  597. for (i = 0; i < cb->nentries; i++) {
  598. float last = 0;
  599. int j;
  600. int div = 1;
  601. for (j = 0; j < cb->ndimentions; j++) {
  602. int off;
  603. if (cb->lookup == 1) off = (i / div) % vals; // lookup type 1
  604. else off = i * cb->ndimentions + j; // lookup type 2
  605. cb->dimentions[i * cb->ndimentions + j] = last + cb->min + cb->quantlist[off] * cb->delta;
  606. if (cb->seq_p) last = cb->dimentions[i * cb->ndimentions + j];
  607. div *= vals;
  608. }
  609. }
  610. }
  611. }
  612. static void ready_residue(residue_t * rc, venc_context_t * venc) {
  613. int i;
  614. assert(rc->type == 2);
  615. rc->maxes = av_mallocz(sizeof(float[2]) * rc->classifications);
  616. for (i = 0; i < rc->classifications; i++) {
  617. int j;
  618. codebook_t * cb;
  619. for (j = 0; j < 8; j++) if (rc->books[i][j] != -1) break;
  620. if (j == 8) continue; // zero
  621. cb = &venc->codebooks[rc->books[i][j]];
  622. assert(cb->ndimentions >= 2);
  623. assert(cb->lookup);
  624. for (j = 0; j < cb->nentries; j++) {
  625. float a;
  626. if (!cb->lens[j]) continue;
  627. a = fabs(cb->dimentions[j * cb->ndimentions]);
  628. if (a > rc->maxes[i][0]) rc->maxes[i][0] = a;
  629. a = fabs(cb->dimentions[j * cb->ndimentions + 1]);
  630. if (a > rc->maxes[i][1]) rc->maxes[i][1] = a;
  631. }
  632. }
  633. // small bias
  634. for (i = 0; i < rc->classifications; i++) {
  635. rc->maxes[i][0] += 0.8;
  636. rc->maxes[i][1] += 0.8;
  637. }
  638. }
  639. static void create_vorbis_context(venc_context_t * venc, AVCodecContext * avccontext) {
  640. floor_t * fc;
  641. residue_t * rc;
  642. mapping_t * mc;
  643. int i, book;
  644. venc->channels = avccontext->channels;
  645. venc->sample_rate = avccontext->sample_rate;
  646. venc->blocksize[0] = venc->blocksize[1] = 11;
  647. venc->ncodebooks = sizeof(cvectors)/sizeof(cvectors[0]);
  648. venc->codebooks = av_malloc(sizeof(codebook_t) * venc->ncodebooks);
  649. // codebook 0..14 - floor1 book, values 0..255
  650. // codebook 15 residue masterbook
  651. // codebook 16..29 residue
  652. for (book = 0; book < venc->ncodebooks; book++) {
  653. codebook_t * cb = &venc->codebooks[book];
  654. int vals;
  655. cb->ndimentions = cvectors[book].dim;
  656. cb->nentries = cvectors[book].real_len;
  657. cb->min = cvectors[book].min;
  658. cb->delta = cvectors[book].delta;
  659. cb->lookup = cvectors[book].lookup;
  660. cb->seq_p = 0;
  661. cb->lens = av_malloc(sizeof(uint8_t) * cb->nentries);
  662. cb->codewords = av_malloc(sizeof(uint32_t) * cb->nentries);
  663. memcpy(cb->lens, cvectors[book].clens, cvectors[book].len);
  664. memset(cb->lens + cvectors[book].len, 0, cb->nentries - cvectors[book].len);
  665. if (cb->lookup) {
  666. vals = cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries);
  667. cb->quantlist = av_malloc(sizeof(int) * vals);
  668. for (i = 0; i < vals; i++) cb->quantlist[i] = cvectors[book].quant[i];
  669. } else {
  670. cb->quantlist = NULL;
  671. }
  672. ready_codebook(cb);
  673. }
  674. venc->nfloors = 1;
  675. venc->floors = av_malloc(sizeof(floor_t) * venc->nfloors);
  676. // just 1 floor
  677. fc = &venc->floors[0];
  678. fc->partitions = 8;
  679. fc->partition_to_class = av_malloc(sizeof(int) * fc->partitions);
  680. fc->nclasses = 0;
  681. for (i = 0; i < fc->partitions; i++) {
  682. int a[] = {0,1,2,2,3,3,4,4};
  683. fc->partition_to_class[i] = a[i];
  684. fc->nclasses = FFMAX(fc->nclasses, fc->partition_to_class[i]);
  685. }
  686. fc->nclasses++;
  687. fc->classes = av_malloc(sizeof(floor_class_t) * fc->nclasses);
  688. for (i = 0; i < fc->nclasses; i++) {
  689. floor_class_t * c = &fc->classes[i];
  690. int j, books;
  691. int dim[] = {3,4,3,4,3};
  692. int subclass[] = {0,1,1,2,2};
  693. int masterbook[] = {0/*none*/,0,1,2,3};
  694. int * nbooks[] = {
  695. (int[]){ 4 },
  696. (int[]){ 5, 6 },
  697. (int[]){ 7, 8 },
  698. (int[]){ -1, 9, 10, 11 },
  699. (int[]){ -1, 12, 13, 14 },
  700. };
  701. c->dim = dim[i];
  702. c->subclass = subclass[i];
  703. c->masterbook = masterbook[i];
  704. books = (1 << c->subclass);
  705. c->books = av_malloc(sizeof(int) * books);
  706. for (j = 0; j < books; j++) c->books[j] = nbooks[i][j];
  707. }
  708. fc->multiplier = 2;
  709. fc->rangebits = venc->blocksize[0] - 1;
  710. fc->values = 2;
  711. for (i = 0; i < fc->partitions; i++)
  712. fc->values += fc->classes[fc->partition_to_class[i]].dim;
  713. fc->list = av_malloc(sizeof(floor1_entry_t) * fc->values);
  714. fc->list[0].x = 0;
  715. fc->list[1].x = 1 << fc->rangebits;
  716. for (i = 2; i < fc->values; i++) {
  717. static const int a[] = {
  718. 93, 23,372, 6, 46,186,750, 14, 33, 65,
  719. 130,260,556, 3, 10, 18, 28, 39, 55, 79,
  720. 111,158,220,312,464,650,850
  721. };
  722. fc->list[i].x = a[i - 2];
  723. }
  724. ff_vorbis_ready_floor1_list(fc->list, fc->values);
  725. venc->nresidues = 1;
  726. venc->residues = av_malloc(sizeof(residue_t) * venc->nresidues);
  727. // single residue
  728. rc = &venc->residues[0];
  729. rc->type = 2;
  730. rc->begin = 0;
  731. rc->end = 1600;
  732. rc->partition_size = 32;
  733. rc->classifications = 10;
  734. rc->classbook = 15;
  735. rc->books = av_malloc(sizeof(*rc->books) * rc->classifications);
  736. {
  737. static const int8_t a[10][8] = {
  738. { -1, -1, -1, -1, -1, -1, -1, -1, },
  739. { -1, -1, 16, -1, -1, -1, -1, -1, },
  740. { -1, -1, 17, -1, -1, -1, -1, -1, },
  741. { -1, -1, 18, -1, -1, -1, -1, -1, },
  742. { -1, -1, 19, -1, -1, -1, -1, -1, },
  743. { -1, -1, 20, -1, -1, -1, -1, -1, },
  744. { -1, -1, 21, -1, -1, -1, -1, -1, },
  745. { 22, 23, -1, -1, -1, -1, -1, -1, },
  746. { 24, 25, -1, -1, -1, -1, -1, -1, },
  747. { 26, 27, 28, -1, -1, -1, -1, -1, },
  748. };
  749. memcpy(rc->books, a, sizeof a);
  750. }
  751. ready_residue(rc, venc);
  752. venc->nmappings = 1;
  753. venc->mappings = av_malloc(sizeof(mapping_t) * venc->nmappings);
  754. // single mapping
  755. mc = &venc->mappings[0];
  756. mc->submaps = 1;
  757. mc->mux = av_malloc(sizeof(int) * venc->channels);
  758. for (i = 0; i < venc->channels; i++) mc->mux[i] = 0;
  759. mc->floor = av_malloc(sizeof(int) * mc->submaps);
  760. mc->residue = av_malloc(sizeof(int) * mc->submaps);
  761. for (i = 0; i < mc->submaps; i++) {
  762. mc->floor[i] = 0;
  763. mc->residue[i] = 0;
  764. }
  765. mc->coupling_steps = venc->channels == 2 ? 1 : 0;
  766. mc->magnitude = av_malloc(sizeof(int) * mc->coupling_steps);
  767. mc->angle = av_malloc(sizeof(int) * mc->coupling_steps);
  768. if (mc->coupling_steps) {
  769. mc->magnitude[0] = 0;
  770. mc->angle[0] = 1;
  771. }
  772. venc->nmodes = 1;
  773. venc->modes = av_malloc(sizeof(vorbis_mode_t) * venc->nmodes);
  774. // single mode
  775. venc->modes[0].blockflag = 0;
  776. venc->modes[0].mapping = 0;
  777. venc->have_saved = 0;
  778. venc->saved = av_malloc(sizeof(float) * venc->channels * (1 << venc->blocksize[1]) / 2);
  779. venc->samples = av_malloc(sizeof(float) * venc->channels * (1 << venc->blocksize[1]));
  780. venc->floor = av_malloc(sizeof(float) * venc->channels * (1 << venc->blocksize[1]) / 2);
  781. venc->coeffs = av_malloc(sizeof(float) * venc->channels * (1 << venc->blocksize[1]) / 2);
  782. venc->win[0] = ff_vorbis_vwin[venc->blocksize[0] - 6];
  783. venc->win[1] = ff_vorbis_vwin[venc->blocksize[1] - 6];
  784. ff_mdct_init(&venc->mdct[0], venc->blocksize[0], 0);
  785. ff_mdct_init(&venc->mdct[1], venc->blocksize[1], 0);
  786. }
  787. static void put_float(PutBitContext * pb, float f) {
  788. int exp, mant;
  789. uint32_t res = 0;
  790. mant = (int)ldexp(frexp(f, &exp), 20);
  791. exp += 788 - 20;
  792. if (mant < 0) { res |= (1 << 31); mant = -mant; }
  793. res |= mant | (exp << 21);
  794. put_bits(pb, 32, res);
  795. }
  796. static void put_codebook_header(PutBitContext * pb, codebook_t * cb) {
  797. int i;
  798. int ordered = 0;
  799. put_bits(pb, 24, 0x564342); //magic
  800. put_bits(pb, 16, cb->ndimentions);
  801. put_bits(pb, 24, cb->nentries);
  802. for (i = 1; i < cb->nentries; i++) if (cb->lens[i] < cb->lens[i-1]) break;
  803. if (i == cb->nentries) ordered = 1;
  804. put_bits(pb, 1, ordered);
  805. if (ordered) {
  806. int len = cb->lens[0];
  807. put_bits(pb, 5, len - 1);
  808. i = 0;
  809. while (i < cb->nentries) {
  810. int j;
  811. for (j = 0; j+i < cb->nentries; j++) if (cb->lens[j+i] != len) break;
  812. put_bits(pb, ilog(cb->nentries - i), j);
  813. i += j;
  814. len++;
  815. }
  816. } else {
  817. int sparse = 0;
  818. for (i = 0; i < cb->nentries; i++) if (!cb->lens[i]) break;
  819. if (i != cb->nentries) sparse = 1;
  820. put_bits(pb, 1, sparse);
  821. for (i = 0; i < cb->nentries; i++) {
  822. if (sparse) put_bits(pb, 1, !!cb->lens[i]);
  823. if (cb->lens[i]) put_bits(pb, 5, cb->lens[i] - 1);
  824. }
  825. }
  826. put_bits(pb, 4, cb->lookup);
  827. if (cb->lookup) {
  828. int tmp = cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries);
  829. int bits = ilog(cb->quantlist[0]);
  830. for (i = 1; i < tmp; i++) bits = FFMAX(bits, ilog(cb->quantlist[i]));
  831. put_float(pb, cb->min);
  832. put_float(pb, cb->delta);
  833. put_bits(pb, 4, bits - 1);
  834. put_bits(pb, 1, cb->seq_p);
  835. for (i = 0; i < tmp; i++) put_bits(pb, bits, cb->quantlist[i]);
  836. }
  837. }
  838. static void put_floor_header(PutBitContext * pb, floor_t * fc) {
  839. int i;
  840. put_bits(pb, 16, 1); // type, only floor1 is supported
  841. put_bits(pb, 5, fc->partitions);
  842. for (i = 0; i < fc->partitions; i++) put_bits(pb, 4, fc->partition_to_class[i]);
  843. for (i = 0; i < fc->nclasses; i++) {
  844. int j, books;
  845. put_bits(pb, 3, fc->classes[i].dim - 1);
  846. put_bits(pb, 2, fc->classes[i].subclass);
  847. if (fc->classes[i].subclass) put_bits(pb, 8, fc->classes[i].masterbook);
  848. books = (1 << fc->classes[i].subclass);
  849. for (j = 0; j < books; j++) put_bits(pb, 8, fc->classes[i].books[j] + 1);
  850. }
  851. put_bits(pb, 2, fc->multiplier - 1);
  852. put_bits(pb, 4, fc->rangebits);
  853. for (i = 2; i < fc->values; i++) put_bits(pb, fc->rangebits, fc->list[i].x);
  854. }
  855. static void put_residue_header(PutBitContext * pb, residue_t * rc) {
  856. int i;
  857. put_bits(pb, 16, rc->type);
  858. put_bits(pb, 24, rc->begin);
  859. put_bits(pb, 24, rc->end);
  860. put_bits(pb, 24, rc->partition_size - 1);
  861. put_bits(pb, 6, rc->classifications - 1);
  862. put_bits(pb, 8, rc->classbook);
  863. for (i = 0; i < rc->classifications; i++) {
  864. int j, tmp = 0;
  865. for (j = 0; j < 8; j++) tmp |= (rc->books[i][j] != -1) << j;
  866. put_bits(pb, 3, tmp & 7);
  867. put_bits(pb, 1, tmp > 7);
  868. if (tmp > 7) put_bits(pb, 5, tmp >> 3);
  869. }
  870. for (i = 0; i < rc->classifications; i++) {
  871. int j;
  872. for (j = 0; j < 8; j++)
  873. if (rc->books[i][j] != -1)
  874. put_bits(pb, 8, rc->books[i][j]);
  875. }
  876. }
  877. static int put_main_header(venc_context_t * venc, uint8_t ** out) {
  878. int i;
  879. PutBitContext pb;
  880. uint8_t buffer[50000] = {0}, * p = buffer;
  881. int buffer_len = sizeof buffer;
  882. int len, hlens[3];
  883. // identification header
  884. init_put_bits(&pb, p, buffer_len);
  885. put_bits(&pb, 8, 1); //magic
  886. for (i = 0; "vorbis"[i]; i++) put_bits(&pb, 8, "vorbis"[i]);
  887. put_bits(&pb, 32, 0); // version
  888. put_bits(&pb, 8, venc->channels);
  889. put_bits(&pb, 32, venc->sample_rate);
  890. put_bits(&pb, 32, 0); // bitrate
  891. put_bits(&pb, 32, 0); // bitrate
  892. put_bits(&pb, 32, 0); // bitrate
  893. put_bits(&pb, 4, venc->blocksize[0]);
  894. put_bits(&pb, 4, venc->blocksize[1]);
  895. put_bits(&pb, 1, 1); // framing
  896. flush_put_bits(&pb);
  897. hlens[0] = (put_bits_count(&pb) + 7) / 8;
  898. buffer_len -= hlens[0];
  899. p += hlens[0];
  900. // comment header
  901. init_put_bits(&pb, p, buffer_len);
  902. put_bits(&pb, 8, 3); //magic
  903. for (i = 0; "vorbis"[i]; i++) put_bits(&pb, 8, "vorbis"[i]);
  904. put_bits(&pb, 32, 0); // vendor length TODO
  905. put_bits(&pb, 32, 0); // amount of comments
  906. put_bits(&pb, 1, 1); // framing
  907. flush_put_bits(&pb);
  908. hlens[1] = (put_bits_count(&pb) + 7) / 8;
  909. buffer_len -= hlens[1];
  910. p += hlens[1];
  911. // setup header
  912. init_put_bits(&pb, p, buffer_len);
  913. put_bits(&pb, 8, 5); //magic
  914. for (i = 0; "vorbis"[i]; i++) put_bits(&pb, 8, "vorbis"[i]);
  915. // codebooks
  916. put_bits(&pb, 8, venc->ncodebooks - 1);
  917. for (i = 0; i < venc->ncodebooks; i++) put_codebook_header(&pb, &venc->codebooks[i]);
  918. // time domain, reserved, zero
  919. put_bits(&pb, 6, 0);
  920. put_bits(&pb, 16, 0);
  921. // floors
  922. put_bits(&pb, 6, venc->nfloors - 1);
  923. for (i = 0; i < venc->nfloors; i++) put_floor_header(&pb, &venc->floors[i]);
  924. // residues
  925. put_bits(&pb, 6, venc->nresidues - 1);
  926. for (i = 0; i < venc->nresidues; i++) put_residue_header(&pb, &venc->residues[i]);
  927. // mappings
  928. put_bits(&pb, 6, venc->nmappings - 1);
  929. for (i = 0; i < venc->nmappings; i++) {
  930. mapping_t * mc = &venc->mappings[i];
  931. int j;
  932. put_bits(&pb, 16, 0); // mapping type
  933. put_bits(&pb, 1, mc->submaps > 1);
  934. if (mc->submaps > 1) put_bits(&pb, 4, mc->submaps - 1);
  935. put_bits(&pb, 1, !!mc->coupling_steps);
  936. if (mc->coupling_steps) {
  937. put_bits(&pb, 8, mc->coupling_steps - 1);
  938. for (j = 0; j < mc->coupling_steps; j++) {
  939. put_bits(&pb, ilog(venc->channels - 1), mc->magnitude[j]);
  940. put_bits(&pb, ilog(venc->channels - 1), mc->angle[j]);
  941. }
  942. }
  943. put_bits(&pb, 2, 0); // reserved
  944. if (mc->submaps > 1) for (j = 0; j < venc->channels; j++) put_bits(&pb, 4, mc->mux[j]);
  945. for (j = 0; j < mc->submaps; j++) {
  946. put_bits(&pb, 8, 0); // reserved time configuration
  947. put_bits(&pb, 8, mc->floor[j]);
  948. put_bits(&pb, 8, mc->residue[j]);
  949. }
  950. }
  951. // modes
  952. put_bits(&pb, 6, venc->nmodes - 1);
  953. for (i = 0; i < venc->nmodes; i++) {
  954. put_bits(&pb, 1, venc->modes[i].blockflag);
  955. put_bits(&pb, 16, 0); // reserved window type
  956. put_bits(&pb, 16, 0); // reserved transform type
  957. put_bits(&pb, 8, venc->modes[i].mapping);
  958. }
  959. put_bits(&pb, 1, 1); // framing
  960. flush_put_bits(&pb);
  961. hlens[2] = (put_bits_count(&pb) + 7) / 8;
  962. len = hlens[0] + hlens[1] + hlens[2];
  963. p = *out = av_mallocz(64 + len + len/255);
  964. *p++ = 2;
  965. p += av_xiphlacing(p, hlens[0]);
  966. p += av_xiphlacing(p, hlens[1]);
  967. buffer_len = 0;
  968. for (i = 0; i < 3; i++) {
  969. memcpy(p, buffer + buffer_len, hlens[i]);
  970. p += hlens[i];
  971. buffer_len += hlens[i];
  972. }
  973. return p - *out;
  974. }
  975. static float get_floor_average(floor_t * fc, float * coeffs, int i) {
  976. int begin = fc->list[fc->list[FFMAX(i-1, 0)].sort].x;
  977. int end = fc->list[fc->list[FFMIN(i+1, fc->values - 1)].sort].x;
  978. int j;
  979. float average = 0;
  980. for (j = begin; j < end; j++) average += fabs(coeffs[j]);
  981. return average / (end - begin);
  982. }
  983. static void floor_fit(venc_context_t * venc, floor_t * fc, float * coeffs, int * posts, int samples) {
  984. int range = 255 / fc->multiplier + 1;
  985. int i;
  986. float tot_average = 0.;
  987. float averages[fc->values];
  988. for (i = 0; i < fc->values; i++) tot_average += averages[i] = get_floor_average(fc, coeffs, i);
  989. tot_average /= fc->values;
  990. tot_average /= venc->quality;
  991. for (i = 0; i < fc->values; i++) {
  992. int position = fc->list[fc->list[i].sort].x;
  993. float average = averages[i];
  994. int j;
  995. average /= pow(average, 0.5) / tot_average * pow(0.8, position/200.); // MAGIC!
  996. for (j = 0; j < range - 1; j++) if (ff_vorbis_floor1_inverse_db_table[j * fc->multiplier] > average) break;
  997. posts[fc->list[i].sort] = j;
  998. }
  999. }
  1000. static int render_point(int x0, int y0, int x1, int y1, int x) {
  1001. return y0 + (x - x0) * (y1 - y0) / (x1 - x0);
  1002. }
  1003. static void floor_encode(venc_context_t * venc, floor_t * fc, PutBitContext * pb, int * posts, float * floor, int samples) {
  1004. int range = 255 / fc->multiplier + 1;
  1005. int coded[fc->values]; // first 2 values are unused
  1006. int i, counter;
  1007. int lx, ly;
  1008. put_bits(pb, 1, 1); // non zero
  1009. put_bits(pb, ilog(range - 1), posts[0]);
  1010. put_bits(pb, ilog(range - 1), posts[1]);
  1011. coded[0] = coded[1] = 1;
  1012. for (i = 2; i < fc->values; i++) {
  1013. int predicted = render_point(fc->list[fc->list[i].low].x,
  1014. posts[fc->list[i].low],
  1015. fc->list[fc->list[i].high].x,
  1016. posts[fc->list[i].high],
  1017. fc->list[i].x);
  1018. int highroom = range - predicted;
  1019. int lowroom = predicted;
  1020. int room = FFMIN(highroom, lowroom);
  1021. if (predicted == posts[i]) {
  1022. coded[i] = 0; // must be used later as flag!
  1023. continue;
  1024. } else {
  1025. if (!coded[fc->list[i].low]) coded[fc->list[i].low] = -1;
  1026. if (!coded[fc->list[i].high]) coded[fc->list[i].high] = -1;
  1027. }
  1028. if (posts[i] > predicted) {
  1029. if (posts[i] - predicted > room) coded[i] = posts[i] - predicted + lowroom;
  1030. else coded[i] = (posts[i] - predicted) << 1;
  1031. } else {
  1032. if (predicted - posts[i] > room) coded[i] = predicted - posts[i] + highroom - 1;
  1033. else coded[i] = ((predicted - posts[i]) << 1) - 1;
  1034. }
  1035. }
  1036. counter = 2;
  1037. for (i = 0; i < fc->partitions; i++) {
  1038. floor_class_t * c = &fc->classes[fc->partition_to_class[i]];
  1039. int k, cval = 0, csub = 1<<c->subclass;
  1040. if (c->subclass) {
  1041. codebook_t * book = &venc->codebooks[c->masterbook];
  1042. int cshift = 0;
  1043. for (k = 0; k < c->dim; k++) {
  1044. int l;
  1045. for (l = 0; l < csub; l++) {
  1046. int maxval = 1;
  1047. if (c->books[l] != -1) maxval = venc->codebooks[c->books[l]].nentries;
  1048. // coded could be -1, but this still works, cause thats 0
  1049. if (coded[counter + k] < maxval) break;
  1050. }
  1051. assert(l != csub);
  1052. cval |= l << cshift;
  1053. cshift += c->subclass;
  1054. }
  1055. put_codeword(pb, book, cval);
  1056. }
  1057. for (k = 0; k < c->dim; k++) {
  1058. int book = c->books[cval & (csub-1)];
  1059. int entry = coded[counter++];
  1060. cval >>= c->subclass;
  1061. if (book == -1) continue;
  1062. if (entry == -1) entry = 0;
  1063. put_codeword(pb, &venc->codebooks[book], entry);
  1064. }
  1065. }
  1066. lx = 0;
  1067. ly = posts[0] * fc->multiplier; // sorted 0 is still 0
  1068. for (i = 1; i < fc->values; i++) {
  1069. int pos = fc->list[i].sort;
  1070. if (coded[pos]) {
  1071. render_line(lx, ly, fc->list[pos].x, posts[pos] * fc->multiplier, floor, samples);
  1072. lx = fc->list[pos].x;
  1073. ly = posts[pos] * fc->multiplier;
  1074. }
  1075. if (lx >= samples) break;
  1076. }
  1077. if (lx < samples) render_line(lx, ly, samples, ly, floor, samples);
  1078. }
  1079. static float * put_vector(codebook_t * book, PutBitContext * pb, float * num) {
  1080. int i;
  1081. int entry = -1;
  1082. float distance = 0;
  1083. assert(book->dimentions);
  1084. for (i = 0; i < book->nentries; i++) {
  1085. float d = 0.;
  1086. int j;
  1087. if (!book->lens[i]) continue;
  1088. for (j = 0; j < book->ndimentions; j++) {
  1089. float a = (book->dimentions[i * book->ndimentions + j] - num[j]);
  1090. d += a*a;
  1091. }
  1092. if (entry == -1 || distance > d) {
  1093. entry = i;
  1094. distance = d;
  1095. }
  1096. }
  1097. put_codeword(pb, book, entry);
  1098. return &book->dimentions[entry * book->ndimentions];
  1099. }
  1100. static void residue_encode(venc_context_t * venc, residue_t * rc, PutBitContext * pb, float * coeffs, int samples, int real_ch) {
  1101. int pass, i, j, p, k;
  1102. int psize = rc->partition_size;
  1103. int partitions = (rc->end - rc->begin) / psize;
  1104. int channels = (rc->type == 2) ? 1 : real_ch;
  1105. int classes[channels][partitions];
  1106. int classwords = venc->codebooks[rc->classbook].ndimentions;
  1107. assert(rc->type == 2);
  1108. assert(real_ch == 2);
  1109. for (p = 0; p < partitions; p++) {
  1110. float max1 = 0., max2 = 0.;
  1111. int s = rc->begin + p * psize;
  1112. for (k = s; k < s + psize; k += 2) {
  1113. max1 = FFMAX(max1, fabs(coeffs[ k / real_ch]));
  1114. max2 = FFMAX(max2, fabs(coeffs[samples + k / real_ch]));
  1115. }
  1116. for (i = 0; i < rc->classifications - 1; i++) {
  1117. if (max1 < rc->maxes[i][0] && max2 < rc->maxes[i][1]) break;
  1118. }
  1119. classes[0][p] = i;
  1120. }
  1121. for (pass = 0; pass < 8; pass++) {
  1122. p = 0;
  1123. while (p < partitions) {
  1124. if (pass == 0) for (j = 0; j < channels; j++) {
  1125. codebook_t * book = &venc->codebooks[rc->classbook];
  1126. int entry = 0;
  1127. for (i = 0; i < classwords; i++) {
  1128. entry *= rc->classifications;
  1129. entry += classes[j][p + i];
  1130. }
  1131. put_codeword(pb, book, entry);
  1132. }
  1133. for (i = 0; i < classwords && p < partitions; i++, p++) {
  1134. for (j = 0; j < channels; j++) {
  1135. int nbook = rc->books[classes[j][p]][pass];
  1136. codebook_t * book = &venc->codebooks[nbook];
  1137. float * buf = coeffs + samples*j + rc->begin + p*psize;
  1138. if (nbook == -1) continue;
  1139. assert(rc->type == 0 || rc->type == 2);
  1140. assert(!(psize % book->ndimentions));
  1141. if (rc->type == 0) {
  1142. for (k = 0; k < psize; k += book->ndimentions) {
  1143. float * a = put_vector(book, pb, &buf[k]);
  1144. int l;
  1145. for (l = 0; l < book->ndimentions; l++) buf[k + l] -= a[l];
  1146. }
  1147. } else {
  1148. for (k = 0; k < psize; k += book->ndimentions) {
  1149. int dim = book->ndimentions, s = rc->begin + p * psize + k, l;
  1150. float vec[dim], * a = vec;
  1151. for (l = s; l < s + dim; l++)
  1152. *a++ = coeffs[(l % real_ch) * samples + l / real_ch];
  1153. a = put_vector(book, pb, vec);
  1154. for (l = s; l < s + dim; l++)
  1155. coeffs[(l % real_ch) * samples + l / real_ch] -= *a++;
  1156. }
  1157. }
  1158. }
  1159. }
  1160. }
  1161. }
  1162. }
  1163. static int window(venc_context_t * venc, signed short * audio, int samples) {
  1164. int i, j, channel;
  1165. const float * win = venc->win[0];
  1166. int window_len = 1 << (venc->blocksize[0] - 1);
  1167. float n = (float)(1 << venc->blocksize[0]) / 4.;
  1168. // FIXME use dsp
  1169. if (!venc->have_saved && !samples) return 0;
  1170. if (venc->have_saved) {
  1171. for (channel = 0; channel < venc->channels; channel++) {
  1172. memcpy(venc->samples + channel*window_len*2, venc->saved + channel*window_len, sizeof(float)*window_len);
  1173. }
  1174. } else {
  1175. for (channel = 0; channel < venc->channels; channel++) {
  1176. memset(venc->samples + channel*window_len*2, 0, sizeof(float)*window_len);
  1177. }
  1178. }
  1179. if (samples) {
  1180. for (channel = 0; channel < venc->channels; channel++) {
  1181. float * offset = venc->samples + channel*window_len*2 + window_len;
  1182. j = channel;
  1183. for (i = 0; i < samples; i++, j += venc->channels)
  1184. offset[i] = audio[j] / 32768. / n * win[window_len - i - 1];
  1185. }
  1186. } else {
  1187. for (channel = 0; channel < venc->channels; channel++) {
  1188. memset(venc->samples + channel*window_len*2 + window_len, 0, sizeof(float)*window_len);
  1189. }
  1190. }
  1191. for (channel = 0; channel < venc->channels; channel++) {
  1192. ff_mdct_calc(&venc->mdct[0], venc->coeffs + channel*window_len, venc->samples + channel*window_len*2, venc->floor/*tmp*/);
  1193. }
  1194. if (samples) {
  1195. for (channel = 0; channel < venc->channels; channel++) {
  1196. float * offset = venc->saved + channel*window_len;
  1197. j = channel;
  1198. for (i = 0; i < samples; i++, j += venc->channels)
  1199. offset[i] = audio[j] / 32768. / n * win[i];
  1200. }
  1201. venc->have_saved = 1;
  1202. } else {
  1203. venc->have_saved = 0;
  1204. }
  1205. return 1;
  1206. }
  1207. static int vorbis_encode_init(AVCodecContext * avccontext)
  1208. {
  1209. venc_context_t * venc = avccontext->priv_data;
  1210. if (avccontext->channels != 2) return -1;
  1211. create_vorbis_context(venc, avccontext);
  1212. if (avccontext->flags & CODEC_FLAG_QSCALE) venc->quality = avccontext->global_quality / (float)FF_QP2LAMBDA / 100.;
  1213. else venc->quality = 0.17;
  1214. avccontext->extradata_size = put_main_header(venc, (uint8_t**)&avccontext->extradata);
  1215. avccontext->frame_size = 1 << (venc->blocksize[0] - 1);
  1216. avccontext->coded_frame = avcodec_alloc_frame();
  1217. avccontext->coded_frame->key_frame = 1;
  1218. return 0;
  1219. }
  1220. static int vorbis_encode_frame(AVCodecContext * avccontext, unsigned char * packets, int buf_size, void *data)
  1221. {
  1222. venc_context_t * venc = avccontext->priv_data;
  1223. signed short * audio = data;
  1224. int samples = data ? avccontext->frame_size : 0;
  1225. vorbis_mode_t * mode;
  1226. mapping_t * mapping;
  1227. PutBitContext pb;
  1228. int i;
  1229. if (!window(venc, audio, samples)) return 0;
  1230. samples = 1 << (venc->blocksize[0] - 1);
  1231. init_put_bits(&pb, packets, buf_size);
  1232. put_bits(&pb, 1, 0); // magic bit
  1233. put_bits(&pb, ilog(venc->nmodes - 1), 0); // 0 bits, the mode
  1234. mode = &venc->modes[0];
  1235. mapping = &venc->mappings[mode->mapping];
  1236. if (mode->blockflag) {
  1237. put_bits(&pb, 1, 0);
  1238. put_bits(&pb, 1, 0);
  1239. }
  1240. for (i = 0; i < venc->channels; i++) {
  1241. floor_t * fc = &venc->floors[mapping->floor[mapping->mux[i]]];
  1242. int posts[fc->values];
  1243. floor_fit(venc, fc, &venc->coeffs[i * samples], posts, samples);
  1244. floor_encode(venc, fc, &pb, posts, &venc->floor[i * samples], samples);
  1245. }
  1246. for (i = 0; i < venc->channels; i++) {
  1247. int j;
  1248. for (j = 0; j < samples; j++) {
  1249. venc->coeffs[i * samples + j] /= venc->floor[i * samples + j];
  1250. }
  1251. }
  1252. for (i = 0; i < mapping->coupling_steps; i++) {
  1253. float * mag = venc->coeffs + mapping->magnitude[i] * samples;
  1254. float * ang = venc->coeffs + mapping->angle[i] * samples;
  1255. int j;
  1256. for (j = 0; j < samples; j++) {
  1257. float m = mag[j];
  1258. float a = ang[j];
  1259. if (m > 0) {
  1260. ang[j] = m - a;
  1261. if (a > m) mag[j] = a;
  1262. else mag[j] = m;
  1263. } else {
  1264. ang[j] = a - m;
  1265. if (a > m) mag[j] = m;
  1266. else mag[j] = a;
  1267. }
  1268. }
  1269. }
  1270. residue_encode(venc, &venc->residues[mapping->residue[mapping->mux[0]]], &pb, venc->coeffs, samples, venc->channels);
  1271. flush_put_bits(&pb);
  1272. return (put_bits_count(&pb) + 7) / 8;
  1273. }
  1274. static int vorbis_encode_close(AVCodecContext * avccontext)
  1275. {
  1276. venc_context_t * venc = avccontext->priv_data;
  1277. int i;
  1278. if (venc->codebooks) for (i = 0; i < venc->ncodebooks; i++) {
  1279. av_freep(&venc->codebooks[i].lens);
  1280. av_freep(&venc->codebooks[i].codewords);
  1281. av_freep(&venc->codebooks[i].quantlist);
  1282. av_freep(&venc->codebooks[i].dimentions);
  1283. }
  1284. av_freep(&venc->codebooks);
  1285. if (venc->floors) for (i = 0; i < venc->nfloors; i++) {
  1286. int j;
  1287. av_freep(&venc->floors[i].classes);
  1288. if (venc->floors[i].classes)
  1289. for (j = 0; j < venc->floors[i].nclasses; j++)
  1290. av_freep(&venc->floors[i].classes[j].books);
  1291. av_freep(&venc->floors[i].partition_to_class);
  1292. av_freep(&venc->floors[i].list);
  1293. }
  1294. av_freep(&venc->floors);
  1295. if (venc->residues) for (i = 0; i < venc->nresidues; i++) {
  1296. av_freep(&venc->residues[i].books);
  1297. av_freep(&venc->residues[i].maxes);
  1298. }
  1299. av_freep(&venc->residues);
  1300. if (venc->mappings) for (i = 0; i < venc->nmappings; i++) {
  1301. av_freep(&venc->mappings[i].mux);
  1302. av_freep(&venc->mappings[i].floor);
  1303. av_freep(&venc->mappings[i].residue);
  1304. }
  1305. av_freep(&venc->mappings);
  1306. av_freep(&venc->modes);
  1307. av_freep(&venc->saved);
  1308. av_freep(&venc->samples);
  1309. av_freep(&venc->floor);
  1310. av_freep(&venc->coeffs);
  1311. ff_mdct_end(&venc->mdct[0]);
  1312. ff_mdct_end(&venc->mdct[1]);
  1313. av_freep(&avccontext->coded_frame);
  1314. av_freep(&avccontext->extradata);
  1315. return 0 ;
  1316. }
  1317. AVCodec vorbis_encoder = {
  1318. "vorbis",
  1319. CODEC_TYPE_AUDIO,
  1320. CODEC_ID_VORBIS,
  1321. sizeof(venc_context_t),
  1322. vorbis_encode_init,
  1323. vorbis_encode_frame,
  1324. vorbis_encode_close,
  1325. .capabilities= CODEC_CAP_DELAY,
  1326. };