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.

245 lines
6.1KB

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