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.

237 lines
5.9KB

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