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.

362 lines
11KB

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