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.

232 lines
5.8KB

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