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.

302 lines
7.9KB

  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. #undef NDEBUG
  25. #include <assert.h>
  26. #define ALT_BITSTREAM_READER_LE
  27. #include "bitstream.h"
  28. #define VORBIS_FRAME_SIZE 64
  29. #define BUFFER_SIZE (1024*64)
  30. typedef struct {
  31. int len;
  32. uint32_t codeword;
  33. } entry_t;
  34. typedef struct {
  35. int nentries;
  36. entry_t * entries;
  37. int ndimentions;
  38. float min;
  39. float delta;
  40. int seq_p;
  41. int lookup;
  42. //float * dimentions;
  43. int * quantlist;
  44. } codebook_t;
  45. typedef struct {
  46. } floor_t;
  47. typedef struct {
  48. } residue_t;
  49. typedef struct {
  50. } mapping_t;
  51. typedef struct {
  52. int channels;
  53. int sample_rate;
  54. int blocksize[2];
  55. int ncodebooks;
  56. codebook_t * codebooks;
  57. int nfloors;
  58. floor_t * floors;
  59. int nresidues;
  60. residue_t * residues;
  61. int nmappings;
  62. mapping_t * mappings;
  63. } venc_context_t;
  64. static inline int ilog(unsigned int a) {
  65. int i;
  66. for (i = 0; a >> i; i++);
  67. return i;
  68. }
  69. static void put_float(PutBitContext * pb, float f) {
  70. int exp, mant;
  71. uint32_t res = 0;
  72. mant = (int)ldexp(frexp(f, &exp), 20);
  73. exp += 788 - 20;
  74. if (mant < 0) { res |= (1 << 31); mant = -mant; }
  75. res |= mant | (exp << 21);
  76. put_bits(pb, 32, res);
  77. }
  78. static void put_codebook_header(PutBitContext * pb, codebook_t * cb) {
  79. int i;
  80. int ordered = 0;
  81. put_bits(pb, 24, 0x564342); //magic
  82. put_bits(pb, 16, cb->ndimentions);
  83. put_bits(pb, 24, cb->nentries);
  84. for (i = 1; i < cb->nentries; i++) if (cb->entries[i].len < cb->entries[i-1].len) break;
  85. if (i == cb->nentries) ordered = 1;
  86. put_bits(pb, 1, ordered);
  87. if (ordered) {
  88. int len = cb->entries[0].len;
  89. put_bits(pb, 5, len);
  90. i = 0;
  91. while (i < cb->nentries) {
  92. int j;
  93. for (j = 0; j+i < cb->nentries; j++) if (cb->entries[j+i].len != len) break;
  94. put_bits(pb, 5, j);
  95. i += j;
  96. len++;
  97. }
  98. } else {
  99. int sparse = 0;
  100. for (i = 0; i < cb->nentries; i++) if (!cb->entries[i].len) break;
  101. if (i != cb->nentries) sparse = 1;
  102. put_bits(pb, 1, sparse);
  103. for (i = 0; i < cb->nentries; i++) {
  104. if (sparse) put_bits(pb, 1, !!cb->entries[i].len);
  105. if (cb->entries[i].len) put_bits(pb, 5, cb->entries[i].len);
  106. }
  107. }
  108. put_bits(pb, 4, cb->lookup);
  109. if (cb->lookup) {
  110. int tmp, bits = ilog(cb->quantlist[0]);
  111. if (cb->lookup == 1) {
  112. for (tmp = 0; ; tmp++) {
  113. int n = 1;
  114. for (i = 0; i < cb->ndimentions; i++) n *= tmp;
  115. if (n > cb->nentries) break;
  116. }
  117. tmp--;
  118. } else tmp = cb->ndimentions * cb->nentries;
  119. for (i = 1; i < tmp; i++) bits = FFMIN(bits, ilog(cb->quantlist[i]));
  120. put_float(pb, cb->min);
  121. put_float(pb, cb->delta);
  122. put_bits(pb, 4, bits - 1);
  123. put_bits(pb, 1, cb->seq_p);
  124. for (i = 0; i < tmp; i++) put_bits(pb, bits, cb->quantlist[i]);
  125. }
  126. }
  127. static void put_floor_header(PutBitContext * pb, floor_t * fl) {
  128. }
  129. static void put_residue_header(PutBitContext * pb, residue_t * r) {
  130. }
  131. static int put_main_header(venc_context_t * venc, uint8_t ** out) {
  132. int i;
  133. PutBitContext pb;
  134. uint8_t buffer[50000] = {0}, * p = buffer;
  135. int buffer_len = sizeof buffer;
  136. int len, hlens[3];
  137. // identification header
  138. init_put_bits(&pb, p, buffer_len);
  139. put_bits(&pb, 8, 1); //magic
  140. for (i = 0; "vorbis"[i]; i++) put_bits(&pb, 8, "vorbis"[i]);
  141. put_bits(&pb, 32, 0); // version
  142. put_bits(&pb, 8, venc->channels);
  143. put_bits(&pb, 32, venc->sample_rate);
  144. put_bits(&pb, 32, 0); // bitrate
  145. put_bits(&pb, 32, 0); // bitrate
  146. put_bits(&pb, 32, 0); // bitrate
  147. put_bits(&pb, 4, venc->blocksize[0]);
  148. put_bits(&pb, 4, venc->blocksize[1]);
  149. put_bits(&pb, 1, 1); // framing
  150. flush_put_bits(&pb);
  151. hlens[0] = (put_bits_count(&pb) + 7) / 8;
  152. buffer_len -= hlens[0];
  153. p += hlens[0];
  154. // comment header
  155. init_put_bits(&pb, p, buffer_len);
  156. put_bits(&pb, 8, 3); //magic
  157. for (i = 0; "vorbis"[i]; i++) put_bits(&pb, 8, "vorbis"[i]);
  158. put_bits(&pb, 32, 0); // vendor length TODO
  159. put_bits(&pb, 32, 0); // amount of comments
  160. put_bits(&pb, 1, 1); // framing
  161. flush_put_bits(&pb);
  162. hlens[1] = (put_bits_count(&pb) + 7) / 8;
  163. buffer_len -= hlens[1];
  164. p += hlens[1];
  165. // setup header
  166. init_put_bits(&pb, p, buffer_len);
  167. put_bits(&pb, 8, 5); //magic
  168. for (i = 0; "vorbis"[i]; i++) put_bits(&pb, 8, "vorbis"[i]);
  169. // codebooks
  170. put_bits(&pb, 8, venc->ncodebooks - 1);
  171. for (i = 0; i < venc->ncodebooks; i++) put_codebook_header(&pb, &venc->codebooks[0]);
  172. // time domain, reserved, zero
  173. put_bits(&pb, 6, 0);
  174. put_bits(&pb, 16, 0);
  175. // floors
  176. put_bits(&pb, 6, venc->nfloors - 1);
  177. for (i = 0; i < venc->nfloors; i++) put_floor_header(&pb, &venc->floors[0]);
  178. // residues
  179. put_bits(&pb, 6, venc->nresidues - 1);
  180. for (i = 0; i < venc->nresidues; i++) put_residue_header(&pb, &venc->residues[0]);
  181. // mappings
  182. put_bits(&pb, 6, venc->nmappings - 1);
  183. for (i = 0; i < venc->nmappings; i++) {
  184. }
  185. flush_put_bits(&pb);
  186. hlens[2] = (put_bits_count(&pb) + 7) / 8;
  187. len = hlens[0] + hlens[1] + hlens[2];
  188. p = *out = av_mallocz(64 + len + len/255);
  189. *p++ = 2;
  190. p += av_xiphlacing(p, hlens[0]);
  191. p += av_xiphlacing(p, hlens[1]);
  192. buffer_len = 0;
  193. for (i = 0; i < 3; i++) {
  194. memcpy(p, buffer + buffer_len, hlens[i]);
  195. p += hlens[i];
  196. buffer_len += hlens[i];
  197. }
  198. return p - *out;
  199. }
  200. static int vorbis_encode_init(AVCodecContext * avccontext)
  201. {
  202. venc_context_t * venc = avccontext->priv_data;
  203. venc->channels = avccontext->channels;
  204. venc->sample_rate = avccontext->sample_rate;
  205. //if (avccontext->flags & CODEC_FLAG_QSCALE) avccontext->global_quality / (float)FF_QP2LAMBDA); else avccontext->bit_rate;
  206. //if(avccontext->cutoff > 0) cfreq = avccontext->cutoff / 1000.0;
  207. avccontext->extradata_size = put_main_header(venc, (uint8_t**)&avccontext->extradata);
  208. avccontext->frame_size = VORBIS_FRAME_SIZE;
  209. avccontext->coded_frame = avcodec_alloc_frame();
  210. avccontext->coded_frame->key_frame = 1;
  211. return 0;
  212. }
  213. static int vorbis_encode_frame(AVCodecContext * avccontext, unsigned char * packets, int buf_size, void *data)
  214. {
  215. #if 0
  216. venc_context_t * venc = avccontext->priv_data;
  217. signed short * audio = data;
  218. int samples = data ? VORBIS_FRAME_SIZE : 0;
  219. avccontext->coded_frame->pts = av_rescale_q(op2->granulepos, (AVRational){1, avccontext->sample_rate}, avccontext->time_base);
  220. memcpy(packets, compressed_frame, l);
  221. #endif
  222. return 0;
  223. }
  224. static int vorbis_encode_close(AVCodecContext * avccontext)
  225. {
  226. venc_context_t * venc = avccontext->priv_data;
  227. av_freep(&avccontext->coded_frame);
  228. av_freep(&avccontext->extradata);
  229. return 0 ;
  230. }
  231. AVCodec oggvorbis_encoder = {
  232. "vorbis",
  233. CODEC_TYPE_AUDIO,
  234. CODEC_ID_VORBIS,
  235. sizeof(venc_context_t),
  236. vorbis_encode_init,
  237. vorbis_encode_frame,
  238. vorbis_encode_close,
  239. .capabilities= CODEC_CAP_DELAY,
  240. };