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.

246 lines
6.1KB

  1. /**
  2. * @file oggvorbis.c
  3. * Ogg Vorbis codec support via libvorbisenc.
  4. * @author Mark Hills <mark@pogo.org.uk>
  5. */
  6. #include <time.h>
  7. #include <vorbis/vorbisenc.h>
  8. #include "avcodec.h"
  9. #include "oggvorbis.h"
  10. #define OGGVORBIS_FRAME_SIZE 1024
  11. typedef struct OggVorbisContext {
  12. vorbis_info vi ;
  13. vorbis_dsp_state vd ;
  14. vorbis_block vb ;
  15. /* decoder */
  16. vorbis_comment vc ;
  17. } OggVorbisContext ;
  18. int oggvorbis_init_encoder(vorbis_info *vi, AVCodecContext *avccontext) {
  19. #ifdef OGGVORBIS_VBR_BY_ESTIMATE
  20. /* variable bitrate by estimate */
  21. return (vorbis_encode_setup_managed(vi, avccontext->channels,
  22. avccontext->sample_rate, -1, avccontext->bit_rate, -1) ||
  23. vorbis_encode_ctl(vi, OV_ECTL_RATEMANAGE_AVG, NULL) ||
  24. vorbis_encode_setup_init(vi)) ;
  25. #else
  26. /* constant bitrate */
  27. return vorbis_encode_init(vi, avccontext->channels,
  28. avccontext->sample_rate, -1, avccontext->bit_rate, -1) ;
  29. #endif
  30. }
  31. static int oggvorbis_encode_init(AVCodecContext *avccontext) {
  32. OggVorbisContext *context = avccontext->priv_data ;
  33. vorbis_info_init(&context->vi) ;
  34. if(oggvorbis_init_encoder(&context->vi, avccontext) < 0) {
  35. fprintf(stderr, "oggvorbis_encode_init: init_encoder failed") ;
  36. return -1 ;
  37. }
  38. vorbis_analysis_init(&context->vd, &context->vi) ;
  39. vorbis_block_init(&context->vd, &context->vb) ;
  40. avccontext->frame_size = OGGVORBIS_FRAME_SIZE ;
  41. avccontext->coded_frame= avcodec_alloc_frame();
  42. avccontext->coded_frame->key_frame= 1;
  43. return 0 ;
  44. }
  45. static int oggvorbis_encode_frame(AVCodecContext *avccontext,
  46. unsigned char *packets,
  47. int buf_size, void *data)
  48. {
  49. OggVorbisContext *context = avccontext->priv_data ;
  50. float **buffer ;
  51. ogg_packet op ;
  52. signed char *audio = data ;
  53. int l, samples = OGGVORBIS_FRAME_SIZE ;
  54. buffer = vorbis_analysis_buffer(&context->vd, samples) ;
  55. if(context->vi.channels == 1) {
  56. for(l = 0 ; l < samples ; l++)
  57. buffer[0][l]=((audio[l*2+1]<<8)|(0x00ff&(int)audio[l*2]))/32768.f;
  58. } else {
  59. for(l = 0 ; l < samples ; l++){
  60. buffer[0][l]=((audio[l*4+1]<<8)|(0x00ff&(int)audio[l*4]))/32768.f;
  61. buffer[1][l]=((audio[l*4+3]<<8)|(0x00ff&(int)audio[l*4+2]))/32768.f;
  62. }
  63. }
  64. vorbis_analysis_wrote(&context->vd, samples) ;
  65. l = 0 ;
  66. while(vorbis_analysis_blockout(&context->vd, &context->vb) == 1) {
  67. vorbis_analysis(&context->vb, NULL);
  68. vorbis_bitrate_addblock(&context->vb) ;
  69. while(vorbis_bitrate_flushpacket(&context->vd, &op)) {
  70. memcpy(packets + l, &op, sizeof(ogg_packet)) ;
  71. memcpy(packets + l + sizeof(ogg_packet), op.packet, op.bytes) ;
  72. l += sizeof(ogg_packet) + op.bytes ;
  73. }
  74. }
  75. return l ;
  76. }
  77. static int oggvorbis_encode_close(AVCodecContext *avccontext) {
  78. OggVorbisContext *context = avccontext->priv_data ;
  79. /* ogg_packet op ; */
  80. vorbis_analysis_wrote(&context->vd, 0) ; /* notify vorbisenc this is EOF */
  81. /* We need to write all the remaining packets into the stream
  82. * on closing */
  83. fprintf(stderr, "fixme: not all packets written on oggvorbis_encode_close()\n") ;
  84. /*
  85. while(vorbis_bitrate_flushpacket(&context->vd, &op)) {
  86. memcpy(packets + l, &op, sizeof(ogg_packet)) ;
  87. memcpy(packets + l + sizeof(ogg_packet), op.packet, op.bytes) ;
  88. l += sizeof(ogg_packet) + op.bytes ;
  89. }
  90. */
  91. vorbis_block_clear(&context->vb);
  92. vorbis_dsp_clear(&context->vd);
  93. vorbis_info_clear(&context->vi);
  94. av_freep(&avccontext->coded_frame);
  95. return 0 ;
  96. }
  97. AVCodec oggvorbis_encoder = {
  98. "vorbis",
  99. CODEC_TYPE_AUDIO,
  100. CODEC_ID_VORBIS,
  101. sizeof(OggVorbisContext),
  102. oggvorbis_encode_init,
  103. oggvorbis_encode_frame,
  104. oggvorbis_encode_close
  105. } ;
  106. static int oggvorbis_decode_init(AVCodecContext *avccontext) {
  107. OggVorbisContext *context = avccontext->priv_data ;
  108. vorbis_info_init(&context->vi) ;
  109. vorbis_comment_init(&context->vc) ;
  110. return 0 ;
  111. }
  112. static inline int conv(int samples, float **pcm, char *buf, int channels) {
  113. int i, j, val ;
  114. ogg_int16_t *ptr, *data = (ogg_int16_t*)buf ;
  115. float *mono ;
  116. for(i = 0 ; i < channels ; i++){
  117. ptr = &data[i];
  118. mono = pcm[i] ;
  119. for(j = 0 ; j < samples ; j++) {
  120. val = mono[j] * 32767.f;
  121. if(val > 32767) val = 32767 ;
  122. if(val < -32768) val = -32768 ;
  123. *ptr = val ;
  124. ptr += channels;
  125. }
  126. }
  127. return 0 ;
  128. }
  129. static int oggvorbis_decode_frame(AVCodecContext *avccontext,
  130. void *data, int *data_size,
  131. uint8_t *buf, int buf_size)
  132. {
  133. OggVorbisContext *context = avccontext->priv_data ;
  134. ogg_packet *op = (ogg_packet*)buf ;
  135. float **pcm ;
  136. int samples, total_samples, total_bytes ;
  137. op->packet = (char*)op + sizeof(ogg_packet) ; /* correct data pointer */
  138. if(op->packetno < 3) {
  139. vorbis_synthesis_headerin(&context->vi, &context->vc, op) ;
  140. return buf_size ;
  141. }
  142. if(op->packetno == 3) {
  143. fprintf(stderr, "vorbis_decode: %d channel, %ldHz, encoder `%s'\n",
  144. context->vi.channels, context->vi.rate, context->vc.vendor);
  145. avccontext->channels = context->vi.channels ;
  146. avccontext->sample_rate = context->vi.rate ;
  147. vorbis_synthesis_init(&context->vd, &context->vi) ;
  148. vorbis_block_init(&context->vd, &context->vb);
  149. }
  150. if(vorbis_synthesis(&context->vb, op) == 0)
  151. vorbis_synthesis_blockin(&context->vd, &context->vb) ;
  152. total_samples = 0 ;
  153. total_bytes = 0 ;
  154. while((samples = vorbis_synthesis_pcmout(&context->vd, &pcm)) > 0) {
  155. conv(samples, pcm, (char*)data + total_bytes, context->vi.channels) ;
  156. total_bytes += samples * 2 * context->vi.channels ;
  157. total_samples += samples ;
  158. vorbis_synthesis_read(&context->vd, samples) ;
  159. }
  160. *data_size = total_bytes ;
  161. return buf_size ;
  162. }
  163. static int oggvorbis_decode_close(AVCodecContext *avccontext) {
  164. OggVorbisContext *context = avccontext->priv_data ;
  165. vorbis_info_clear(&context->vi) ;
  166. vorbis_comment_clear(&context->vc) ;
  167. return 0 ;
  168. }
  169. AVCodec oggvorbis_decoder = {
  170. "vorbis",
  171. CODEC_TYPE_AUDIO,
  172. CODEC_ID_VORBIS,
  173. sizeof(OggVorbisContext),
  174. oggvorbis_decode_init,
  175. NULL,
  176. oggvorbis_decode_close,
  177. oggvorbis_decode_frame,
  178. } ;