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.

141 lines
3.6KB

  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. } OggVorbisContext ;
  15. int oggvorbis_init_encoder(vorbis_info *vi, AVCodecContext *avccontext) {
  16. if(avccontext->quality) { /* VBR requested */
  17. fprintf(stderr, "init_encode: channels=%d quality=%d\n",
  18. avccontext->channels, avccontext->quality) ;
  19. return vorbis_encode_init_vbr(vi, avccontext->channels,
  20. avccontext->sample_rate, (float)avccontext->quality / 1000) ;
  21. }
  22. fprintf(stderr, "init_encoder: channels=%d bitrate=%d tolerance=%d\n",
  23. avccontext->channels, avccontext->bit_rate,
  24. avccontext->bit_rate_tolerance) ;
  25. return vorbis_encode_init(vi, avccontext->channels,
  26. avccontext->sample_rate, -1, avccontext->bit_rate, -1) ;
  27. }
  28. static int oggvorbis_encode_init(AVCodecContext *avccontext) {
  29. OggVorbisContext *context = avccontext->priv_data ;
  30. fprintf(stderr, "oggvorbis_encode_init\n") ;
  31. vorbis_info_init(&context->vi) ;
  32. if(oggvorbis_init_encoder(&context->vi, avccontext) < 0) {
  33. fprintf(stderr, "oggvorbis_encode_init: init_encoder failed") ;
  34. return -1 ;
  35. }
  36. vorbis_analysis_init(&context->vd, &context->vi) ;
  37. vorbis_block_init(&context->vd, &context->vb) ;
  38. avccontext->frame_size = OGGVORBIS_FRAME_SIZE ;
  39. return 0 ;
  40. }
  41. int oggvorbis_encode_frame(AVCodecContext *avccontext, unsigned char *packets,
  42. int buf_size, void *data)
  43. {
  44. OggVorbisContext *context = avccontext->priv_data ;
  45. float **buffer ;
  46. ogg_packet op ;
  47. signed char *audio = data ;
  48. int l, samples = buf_size / 16 ; /* samples = OGGVORBIS_FRAME_SIZE */ ;
  49. buffer = vorbis_analysis_buffer(&context->vd, samples) ;
  50. if(context->vi.channels == 1) {
  51. for(l = 0 ; l < samples ; l++)
  52. buffer[0][l]=((audio[l*2+1]<<8)|(0x00ff&(int)audio[l*2]))/32768.f;
  53. } else {
  54. for(l = 0 ; l < samples ; l++){
  55. buffer[0][l]=((audio[l*4+1]<<8)|(0x00ff&(int)audio[l*4]))/32768.f;
  56. buffer[1][l]=((audio[l*4+3]<<8)|(0x00ff&(int)audio[l*4+2]))/32768.f;
  57. }
  58. }
  59. vorbis_analysis_wrote(&context->vd, samples) ;
  60. l = 0 ;
  61. while(vorbis_analysis_blockout(&context->vd, &context->vb) == 1) {
  62. vorbis_analysis(&context->vb, NULL);
  63. vorbis_bitrate_addblock(&context->vb) ;
  64. while(vorbis_bitrate_flushpacket(&context->vd, &op)) {
  65. memcpy(packets + l, &op, sizeof(ogg_packet)) ;
  66. memcpy(packets + l + sizeof(ogg_packet), op.packet, op.bytes) ;
  67. l += sizeof(ogg_packet) + op.bytes ;
  68. }
  69. }
  70. return l ;
  71. }
  72. int oggvorbis_encode_close(AVCodecContext *avccontext) {
  73. OggVorbisContext *context = avccontext->priv_data ;
  74. /* ogg_packet op ; */
  75. fprintf(stderr, "oggvorbis_encode_close\n") ;
  76. vorbis_analysis_wrote(&context->vd, 0) ; /* notify vorbisenc this is EOF */
  77. /* We need to write all the remaining packets into the stream
  78. * on closing */
  79. /*
  80. while(vorbis_bitrate_flushpacket(&context->vd, &op)) {
  81. memcpy(packets + l, &op, sizeof(ogg_packet)) ;
  82. memcpy(packets + l + sizeof(ogg_packet), op.packet, op.bytes) ;
  83. l += sizeof(ogg_packet) + op.bytes ;
  84. }
  85. */
  86. vorbis_block_clear(&context->vb);
  87. vorbis_dsp_clear(&context->vd);
  88. vorbis_info_clear(&context->vi);
  89. return 0 ;
  90. }
  91. AVCodec oggvorbis_encoder = {
  92. "vorbis",
  93. CODEC_TYPE_AUDIO,
  94. CODEC_ID_VORBIS,
  95. sizeof(OggVorbisContext),
  96. oggvorbis_encode_init,
  97. oggvorbis_encode_frame,
  98. oggvorbis_encode_close
  99. };