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.

301 lines
8.4KB

  1. /**
  2. * @file oggvorbis.c
  3. * Ogg Vorbis codec support via libvorbisenc.
  4. * @author Mark Hills <mark@pogo.org.uk>
  5. */
  6. #include <vorbis/vorbisenc.h>
  7. #include "avcodec.h"
  8. #undef NDEBUG
  9. #include <assert.h>
  10. #define OGGVORBIS_FRAME_SIZE 64
  11. #define BUFFER_SIZE (1024*64)
  12. typedef struct OggVorbisContext {
  13. vorbis_info vi ;
  14. vorbis_dsp_state vd ;
  15. vorbis_block vb ;
  16. uint8_t buffer[BUFFER_SIZE];
  17. int buffer_index;
  18. /* decoder */
  19. vorbis_comment vc ;
  20. ogg_packet op;
  21. } OggVorbisContext ;
  22. static int oggvorbis_init_encoder(vorbis_info *vi, AVCodecContext *avccontext) {
  23. #ifdef OGGVORBIS_VBR_BY_ESTIMATE
  24. /* variable bitrate by estimate */
  25. return (vorbis_encode_setup_managed(vi, avccontext->channels,
  26. avccontext->sample_rate, -1, avccontext->bit_rate, -1) ||
  27. vorbis_encode_ctl(vi, OV_ECTL_RATEMANAGE_AVG, NULL) ||
  28. vorbis_encode_setup_init(vi)) ;
  29. #else
  30. /* constant bitrate */
  31. return vorbis_encode_init(vi, avccontext->channels,
  32. avccontext->sample_rate, -1, avccontext->bit_rate, -1) ;
  33. #endif
  34. }
  35. static int oggvorbis_encode_init(AVCodecContext *avccontext) {
  36. OggVorbisContext *context = avccontext->priv_data ;
  37. ogg_packet header, header_comm, header_code;
  38. uint8_t *p;
  39. vorbis_info_init(&context->vi) ;
  40. if(oggvorbis_init_encoder(&context->vi, avccontext) < 0) {
  41. av_log(avccontext, AV_LOG_ERROR, "oggvorbis_encode_init: init_encoder failed") ;
  42. return -1 ;
  43. }
  44. vorbis_analysis_init(&context->vd, &context->vi) ;
  45. vorbis_block_init(&context->vd, &context->vb) ;
  46. vorbis_comment_init(&context->vc);
  47. vorbis_comment_add_tag(&context->vc, "encoder", LIBAVCODEC_IDENT) ;
  48. vorbis_analysis_headerout(&context->vd, &context->vc, &header,
  49. &header_comm, &header_code);
  50. avccontext->extradata_size= 3*2 + header.bytes + header_comm.bytes + header_code.bytes;
  51. p= avccontext->extradata= av_mallocz(avccontext->extradata_size);
  52. *(p++) = header.bytes>>8;
  53. *(p++) = header.bytes&0xFF;
  54. memcpy(p, header.packet, header.bytes);
  55. p += header.bytes;
  56. *(p++) = header_comm.bytes>>8;
  57. *(p++) = header_comm.bytes&0xFF;
  58. memcpy(p, header_comm.packet, header_comm.bytes);
  59. p += header_comm.bytes;
  60. *(p++) = header_code.bytes>>8;
  61. *(p++) = header_code.bytes&0xFF;
  62. memcpy(p, header_code.packet, header_code.bytes);
  63. /* vorbis_block_clear(&context->vb);
  64. vorbis_dsp_clear(&context->vd);
  65. vorbis_info_clear(&context->vi);*/
  66. vorbis_comment_clear(&context->vc);
  67. avccontext->frame_size = OGGVORBIS_FRAME_SIZE ;
  68. avccontext->coded_frame= avcodec_alloc_frame();
  69. avccontext->coded_frame->key_frame= 1;
  70. return 0 ;
  71. }
  72. static int oggvorbis_encode_frame(AVCodecContext *avccontext,
  73. unsigned char *packets,
  74. int buf_size, void *data)
  75. {
  76. OggVorbisContext *context = avccontext->priv_data ;
  77. float **buffer ;
  78. ogg_packet op ;
  79. signed char *audio = data ;
  80. int l, samples = data ? OGGVORBIS_FRAME_SIZE : 0;
  81. buffer = vorbis_analysis_buffer(&context->vd, samples) ;
  82. if(context->vi.channels == 1) {
  83. for(l = 0 ; l < samples ; l++)
  84. buffer[0][l]=((audio[l*2+1]<<8)|(0x00ff&(int)audio[l*2]))/32768.f;
  85. } else {
  86. for(l = 0 ; l < samples ; l++){
  87. buffer[0][l]=((audio[l*4+1]<<8)|(0x00ff&(int)audio[l*4]))/32768.f;
  88. buffer[1][l]=((audio[l*4+3]<<8)|(0x00ff&(int)audio[l*4+2]))/32768.f;
  89. }
  90. }
  91. vorbis_analysis_wrote(&context->vd, samples) ;
  92. while(vorbis_analysis_blockout(&context->vd, &context->vb) == 1) {
  93. vorbis_analysis(&context->vb, NULL);
  94. vorbis_bitrate_addblock(&context->vb) ;
  95. while(vorbis_bitrate_flushpacket(&context->vd, &op)) {
  96. if(op.bytes==1) //id love to say this is a hack, bad sadly its not, appearently the end of stream decission is in libogg
  97. continue;
  98. memcpy(context->buffer + context->buffer_index, &op, sizeof(ogg_packet));
  99. context->buffer_index += sizeof(ogg_packet);
  100. memcpy(context->buffer + context->buffer_index, op.packet, op.bytes);
  101. context->buffer_index += op.bytes;
  102. // av_log(avccontext, AV_LOG_DEBUG, "e%d / %d\n", context->buffer_index, op.bytes);
  103. }
  104. }
  105. l=0;
  106. if(context->buffer_index){
  107. ogg_packet *op2= (ogg_packet*)context->buffer;
  108. op2->packet = context->buffer + sizeof(ogg_packet);
  109. l= op2->bytes;
  110. avccontext->coded_frame->pts= av_rescale(op2->granulepos, AV_TIME_BASE, avccontext->sample_rate);
  111. memcpy(packets, op2->packet, l);
  112. context->buffer_index -= l + sizeof(ogg_packet);
  113. memcpy(context->buffer, context->buffer + l + sizeof(ogg_packet), context->buffer_index);
  114. // av_log(avccontext, AV_LOG_DEBUG, "E%d\n", l);
  115. }
  116. return l;
  117. }
  118. static int oggvorbis_encode_close(AVCodecContext *avccontext) {
  119. OggVorbisContext *context = avccontext->priv_data ;
  120. /* ogg_packet op ; */
  121. vorbis_analysis_wrote(&context->vd, 0) ; /* notify vorbisenc this is EOF */
  122. vorbis_block_clear(&context->vb);
  123. vorbis_dsp_clear(&context->vd);
  124. vorbis_info_clear(&context->vi);
  125. av_freep(&avccontext->coded_frame);
  126. av_freep(&avccontext->extradata);
  127. return 0 ;
  128. }
  129. AVCodec oggvorbis_encoder = {
  130. "vorbis",
  131. CODEC_TYPE_AUDIO,
  132. CODEC_ID_VORBIS,
  133. sizeof(OggVorbisContext),
  134. oggvorbis_encode_init,
  135. oggvorbis_encode_frame,
  136. oggvorbis_encode_close,
  137. .capabilities= CODEC_CAP_DELAY,
  138. } ;
  139. static int oggvorbis_decode_init(AVCodecContext *avccontext) {
  140. OggVorbisContext *context = avccontext->priv_data ;
  141. uint8_t *p= avccontext->extradata;
  142. int i;
  143. vorbis_info_init(&context->vi) ;
  144. vorbis_comment_init(&context->vc) ;
  145. for(i=0; i<3; i++){
  146. context->op.b_o_s= i==0;
  147. context->op.bytes= *(p++)<<8;
  148. context->op.bytes+=*(p++);
  149. context->op.packet= p;
  150. p += context->op.bytes;
  151. if(vorbis_synthesis_headerin(&context->vi, &context->vc, &context->op)<0){
  152. av_log(avccontext, AV_LOG_ERROR, "%d. vorbis header damaged\n", i+1);
  153. return -1;
  154. }
  155. }
  156. avccontext->channels = context->vi.channels;
  157. avccontext->sample_rate = context->vi.rate;
  158. vorbis_synthesis_init(&context->vd, &context->vi);
  159. vorbis_block_init(&context->vd, &context->vb);
  160. return 0 ;
  161. }
  162. static inline int conv(int samples, float **pcm, char *buf, int channels) {
  163. int i, j, val ;
  164. ogg_int16_t *ptr, *data = (ogg_int16_t*)buf ;
  165. float *mono ;
  166. for(i = 0 ; i < channels ; i++){
  167. ptr = &data[i];
  168. mono = pcm[i] ;
  169. for(j = 0 ; j < samples ; j++) {
  170. val = mono[j] * 32767.f;
  171. if(val > 32767) val = 32767 ;
  172. if(val < -32768) val = -32768 ;
  173. *ptr = val ;
  174. ptr += channels;
  175. }
  176. }
  177. return 0 ;
  178. }
  179. static int oggvorbis_decode_frame(AVCodecContext *avccontext,
  180. void *data, int *data_size,
  181. uint8_t *buf, int buf_size)
  182. {
  183. OggVorbisContext *context = avccontext->priv_data ;
  184. float **pcm ;
  185. ogg_packet *op= &context->op;
  186. int samples, total_samples, total_bytes,i;
  187. if(!buf_size){
  188. //FIXME flush
  189. return 0;
  190. }
  191. op->packet = buf;
  192. op->bytes = buf_size;
  193. // av_log(avccontext, AV_LOG_DEBUG, "%d %d %d %lld %lld %d %d\n", op->bytes, op->b_o_s, op->e_o_s, op->granulepos, op->packetno, buf_size, context->vi.rate);
  194. /* for(i=0; i<op->bytes; i++)
  195. av_log(avccontext, AV_LOG_DEBUG, "%02X ", op->packet[i]);
  196. av_log(avccontext, AV_LOG_DEBUG, "\n");*/
  197. if(vorbis_synthesis(&context->vb, op) == 0)
  198. vorbis_synthesis_blockin(&context->vd, &context->vb) ;
  199. total_samples = 0 ;
  200. total_bytes = 0 ;
  201. while((samples = vorbis_synthesis_pcmout(&context->vd, &pcm)) > 0) {
  202. conv(samples, pcm, (char*)data + total_bytes, context->vi.channels) ;
  203. total_bytes += samples * 2 * context->vi.channels ;
  204. total_samples += samples ;
  205. vorbis_synthesis_read(&context->vd, samples) ;
  206. }
  207. *data_size = total_bytes ;
  208. return buf_size ;
  209. }
  210. static int oggvorbis_decode_close(AVCodecContext *avccontext) {
  211. OggVorbisContext *context = avccontext->priv_data ;
  212. vorbis_info_clear(&context->vi) ;
  213. vorbis_comment_clear(&context->vc) ;
  214. return 0 ;
  215. }
  216. AVCodec oggvorbis_decoder = {
  217. "vorbis",
  218. CODEC_TYPE_AUDIO,
  219. CODEC_ID_VORBIS,
  220. sizeof(OggVorbisContext),
  221. oggvorbis_decode_init,
  222. NULL,
  223. oggvorbis_decode_close,
  224. oggvorbis_decode_frame,
  225. .capabilities= CODEC_CAP_DELAY,
  226. } ;