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.

380 lines
11KB

  1. /*
  2. * copyright (c) 2002 Mark Hills <mark@pogo.org.uk>
  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 oggvorbis.c
  20. * Ogg Vorbis codec support via libvorbisenc.
  21. * @author Mark Hills <mark@pogo.org.uk>
  22. */
  23. #include <vorbis/vorbisenc.h>
  24. #include "avcodec.h"
  25. #undef NDEBUG
  26. #include <assert.h>
  27. #define OGGVORBIS_FRAME_SIZE 64
  28. #define BUFFER_SIZE (1024*64)
  29. typedef struct OggVorbisContext {
  30. vorbis_info vi ;
  31. vorbis_dsp_state vd ;
  32. vorbis_block vb ;
  33. uint8_t buffer[BUFFER_SIZE];
  34. int buffer_index;
  35. /* decoder */
  36. vorbis_comment vc ;
  37. ogg_packet op;
  38. } OggVorbisContext ;
  39. static int oggvorbis_init_encoder(vorbis_info *vi, AVCodecContext *avccontext) {
  40. double cfreq;
  41. if(avccontext->flags & CODEC_FLAG_QSCALE) {
  42. /* variable bitrate */
  43. if(vorbis_encode_setup_vbr(vi, avccontext->channels,
  44. avccontext->sample_rate,
  45. avccontext->global_quality / (float)FF_QP2LAMBDA))
  46. return -1;
  47. } else {
  48. /* constant bitrate */
  49. if(vorbis_encode_setup_managed(vi, avccontext->channels,
  50. avccontext->sample_rate, -1, avccontext->bit_rate, -1))
  51. return -1;
  52. #ifdef OGGVORBIS_VBR_BY_ESTIMATE
  53. /* variable bitrate by estimate */
  54. if(vorbis_encode_ctl(vi, OV_ECTL_RATEMANAGE_AVG, NULL))
  55. return -1;
  56. #endif
  57. }
  58. /* cutoff frequency */
  59. if(avccontext->cutoff > 0) {
  60. cfreq = avccontext->cutoff / 1000.0;
  61. if(vorbis_encode_ctl(vi, OV_ECTL_LOWPASS_SET, &cfreq))
  62. return -1;
  63. }
  64. return vorbis_encode_setup_init(vi);
  65. }
  66. static int oggvorbis_encode_init(AVCodecContext *avccontext) {
  67. OggVorbisContext *context = avccontext->priv_data ;
  68. ogg_packet header, header_comm, header_code;
  69. uint8_t *p;
  70. unsigned int offset, len;
  71. vorbis_info_init(&context->vi) ;
  72. if(oggvorbis_init_encoder(&context->vi, avccontext) < 0) {
  73. av_log(avccontext, AV_LOG_ERROR, "oggvorbis_encode_init: init_encoder failed") ;
  74. return -1 ;
  75. }
  76. vorbis_analysis_init(&context->vd, &context->vi) ;
  77. vorbis_block_init(&context->vd, &context->vb) ;
  78. vorbis_comment_init(&context->vc);
  79. vorbis_comment_add_tag(&context->vc, "encoder", LIBAVCODEC_IDENT) ;
  80. vorbis_analysis_headerout(&context->vd, &context->vc, &header,
  81. &header_comm, &header_code);
  82. len = header.bytes + header_comm.bytes + header_code.bytes;
  83. avccontext->extradata_size= 64 + len + len/255;
  84. p = avccontext->extradata= av_mallocz(avccontext->extradata_size);
  85. p[0] = 2;
  86. offset = 1;
  87. offset += av_xiphlacing(&p[offset], header.bytes);
  88. offset += av_xiphlacing(&p[offset], header_comm.bytes);
  89. memcpy(&p[offset], header.packet, header.bytes);
  90. offset += header.bytes;
  91. memcpy(&p[offset], header_comm.packet, header_comm.bytes);
  92. offset += header_comm.bytes;
  93. memcpy(&p[offset], header_code.packet, header_code.bytes);
  94. offset += header_code.bytes;
  95. avccontext->extradata_size = offset;
  96. avccontext->extradata= av_realloc(avccontext->extradata, avccontext->extradata_size);
  97. /* vorbis_block_clear(&context->vb);
  98. vorbis_dsp_clear(&context->vd);
  99. vorbis_info_clear(&context->vi);*/
  100. vorbis_comment_clear(&context->vc);
  101. avccontext->frame_size = OGGVORBIS_FRAME_SIZE ;
  102. avccontext->coded_frame= avcodec_alloc_frame();
  103. avccontext->coded_frame->key_frame= 1;
  104. return 0 ;
  105. }
  106. static int oggvorbis_encode_frame(AVCodecContext *avccontext,
  107. unsigned char *packets,
  108. int buf_size, void *data)
  109. {
  110. OggVorbisContext *context = avccontext->priv_data ;
  111. float **buffer ;
  112. ogg_packet op ;
  113. signed short *audio = data ;
  114. int l, samples = data ? OGGVORBIS_FRAME_SIZE : 0;
  115. buffer = vorbis_analysis_buffer(&context->vd, samples) ;
  116. if(context->vi.channels == 1) {
  117. for(l = 0 ; l < samples ; l++)
  118. buffer[0][l]=audio[l]/32768.f;
  119. } else {
  120. for(l = 0 ; l < samples ; l++){
  121. buffer[0][l]=audio[l*2]/32768.f;
  122. buffer[1][l]=audio[l*2+1]/32768.f;
  123. }
  124. }
  125. vorbis_analysis_wrote(&context->vd, samples) ;
  126. while(vorbis_analysis_blockout(&context->vd, &context->vb) == 1) {
  127. vorbis_analysis(&context->vb, NULL);
  128. vorbis_bitrate_addblock(&context->vb) ;
  129. while(vorbis_bitrate_flushpacket(&context->vd, &op)) {
  130. if(op.bytes==1) //id love to say this is a hack, bad sadly its not, appearently the end of stream decission is in libogg
  131. continue;
  132. memcpy(context->buffer + context->buffer_index, &op, sizeof(ogg_packet));
  133. context->buffer_index += sizeof(ogg_packet);
  134. memcpy(context->buffer + context->buffer_index, op.packet, op.bytes);
  135. context->buffer_index += op.bytes;
  136. // av_log(avccontext, AV_LOG_DEBUG, "e%d / %d\n", context->buffer_index, op.bytes);
  137. }
  138. }
  139. l=0;
  140. if(context->buffer_index){
  141. ogg_packet *op2= (ogg_packet*)context->buffer;
  142. op2->packet = context->buffer + sizeof(ogg_packet);
  143. l= op2->bytes;
  144. avccontext->coded_frame->pts= av_rescale_q(op2->granulepos, (AVRational){1, avccontext->sample_rate}, avccontext->time_base);
  145. //FIXME we should reorder the user supplied pts and not assume that they are spaced by 1/sample_rate
  146. memcpy(packets, op2->packet, l);
  147. context->buffer_index -= l + sizeof(ogg_packet);
  148. memcpy(context->buffer, context->buffer + l + sizeof(ogg_packet), context->buffer_index);
  149. // av_log(avccontext, AV_LOG_DEBUG, "E%d\n", l);
  150. }
  151. return l;
  152. }
  153. static int oggvorbis_encode_close(AVCodecContext *avccontext) {
  154. OggVorbisContext *context = avccontext->priv_data ;
  155. /* ogg_packet op ; */
  156. vorbis_analysis_wrote(&context->vd, 0) ; /* notify vorbisenc this is EOF */
  157. vorbis_block_clear(&context->vb);
  158. vorbis_dsp_clear(&context->vd);
  159. vorbis_info_clear(&context->vi);
  160. av_freep(&avccontext->coded_frame);
  161. av_freep(&avccontext->extradata);
  162. return 0 ;
  163. }
  164. AVCodec oggvorbis_encoder = {
  165. "vorbis",
  166. CODEC_TYPE_AUDIO,
  167. CODEC_ID_VORBIS,
  168. sizeof(OggVorbisContext),
  169. oggvorbis_encode_init,
  170. oggvorbis_encode_frame,
  171. oggvorbis_encode_close,
  172. .capabilities= CODEC_CAP_DELAY,
  173. } ;
  174. static int oggvorbis_decode_init(AVCodecContext *avccontext) {
  175. OggVorbisContext *context = avccontext->priv_data ;
  176. uint8_t *p= avccontext->extradata;
  177. int i, hsizes[3];
  178. unsigned char *headers[3], *extradata = avccontext->extradata;
  179. vorbis_info_init(&context->vi) ;
  180. vorbis_comment_init(&context->vc) ;
  181. if(! avccontext->extradata_size || ! p) {
  182. av_log(avccontext, AV_LOG_ERROR, "vorbis extradata absent\n");
  183. return -1;
  184. }
  185. if(p[0] == 0 && p[1] == 30) {
  186. for(i = 0; i < 3; i++){
  187. hsizes[i] = *p++ << 8;
  188. hsizes[i] += *p++;
  189. headers[i] = p;
  190. p += hsizes[i];
  191. }
  192. } else if(*p == 2) {
  193. unsigned int offset = 1;
  194. p++;
  195. for(i=0; i<2; i++) {
  196. hsizes[i] = 0;
  197. while((*p == 0xFF) && (offset < avccontext->extradata_size)) {
  198. hsizes[i] += 0xFF;
  199. offset++;
  200. p++;
  201. }
  202. if(offset >= avccontext->extradata_size - 1) {
  203. av_log(avccontext, AV_LOG_ERROR,
  204. "vorbis header sizes damaged\n");
  205. return -1;
  206. }
  207. hsizes[i] += *p;
  208. offset++;
  209. p++;
  210. }
  211. hsizes[2] = avccontext->extradata_size - hsizes[0]-hsizes[1]-offset;
  212. #if 0
  213. av_log(avccontext, AV_LOG_DEBUG,
  214. "vorbis header sizes: %d, %d, %d, / extradata_len is %d \n",
  215. hsizes[0], hsizes[1], hsizes[2], avccontext->extradata_size);
  216. #endif
  217. headers[0] = extradata + offset;
  218. headers[1] = extradata + offset + hsizes[0];
  219. headers[2] = extradata + offset + hsizes[0] + hsizes[1];
  220. } else {
  221. av_log(avccontext, AV_LOG_ERROR,
  222. "vorbis initial header len is wrong: %d\n", *p);
  223. return -1;
  224. }
  225. for(i=0; i<3; i++){
  226. context->op.b_o_s= i==0;
  227. context->op.bytes = hsizes[i];
  228. context->op.packet = headers[i];
  229. if(vorbis_synthesis_headerin(&context->vi, &context->vc, &context->op)<0){
  230. av_log(avccontext, AV_LOG_ERROR, "%d. vorbis header damaged\n", i+1);
  231. return -1;
  232. }
  233. }
  234. avccontext->channels = context->vi.channels;
  235. avccontext->sample_rate = context->vi.rate;
  236. avccontext->time_base= (AVRational){1, avccontext->sample_rate};
  237. vorbis_synthesis_init(&context->vd, &context->vi);
  238. vorbis_block_init(&context->vd, &context->vb);
  239. return 0 ;
  240. }
  241. static inline int conv(int samples, float **pcm, char *buf, int channels) {
  242. int i, j, val ;
  243. ogg_int16_t *ptr, *data = (ogg_int16_t*)buf ;
  244. float *mono ;
  245. for(i = 0 ; i < channels ; i++){
  246. ptr = &data[i];
  247. mono = pcm[i] ;
  248. for(j = 0 ; j < samples ; j++) {
  249. val = mono[j] * 32767.f;
  250. if(val > 32767) val = 32767 ;
  251. if(val < -32768) val = -32768 ;
  252. *ptr = val ;
  253. ptr += channels;
  254. }
  255. }
  256. return 0 ;
  257. }
  258. static int oggvorbis_decode_frame(AVCodecContext *avccontext,
  259. void *data, int *data_size,
  260. uint8_t *buf, int buf_size)
  261. {
  262. OggVorbisContext *context = avccontext->priv_data ;
  263. float **pcm ;
  264. ogg_packet *op= &context->op;
  265. int samples, total_samples, total_bytes;
  266. if(!buf_size){
  267. //FIXME flush
  268. return 0;
  269. }
  270. op->packet = buf;
  271. op->bytes = buf_size;
  272. // av_log(avccontext, AV_LOG_DEBUG, "%d %d %d %lld %lld %d %d\n", op->bytes, op->b_o_s, op->e_o_s, op->granulepos, op->packetno, buf_size, context->vi.rate);
  273. /* for(i=0; i<op->bytes; i++)
  274. av_log(avccontext, AV_LOG_DEBUG, "%02X ", op->packet[i]);
  275. av_log(avccontext, AV_LOG_DEBUG, "\n");*/
  276. if(vorbis_synthesis(&context->vb, op) == 0)
  277. vorbis_synthesis_blockin(&context->vd, &context->vb) ;
  278. total_samples = 0 ;
  279. total_bytes = 0 ;
  280. while((samples = vorbis_synthesis_pcmout(&context->vd, &pcm)) > 0) {
  281. conv(samples, pcm, (char*)data + total_bytes, context->vi.channels) ;
  282. total_bytes += samples * 2 * context->vi.channels ;
  283. total_samples += samples ;
  284. vorbis_synthesis_read(&context->vd, samples) ;
  285. }
  286. *data_size = total_bytes ;
  287. return buf_size ;
  288. }
  289. static int oggvorbis_decode_close(AVCodecContext *avccontext) {
  290. OggVorbisContext *context = avccontext->priv_data ;
  291. vorbis_info_clear(&context->vi) ;
  292. vorbis_comment_clear(&context->vc) ;
  293. return 0 ;
  294. }
  295. AVCodec oggvorbis_decoder = {
  296. "vorbis",
  297. CODEC_TYPE_AUDIO,
  298. CODEC_ID_VORBIS,
  299. sizeof(OggVorbisContext),
  300. oggvorbis_decode_init,
  301. NULL,
  302. oggvorbis_decode_close,
  303. oggvorbis_decode_frame,
  304. .capabilities= CODEC_CAP_DELAY,
  305. } ;