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.

318 lines
8.8KB

  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. int64_t fake_pts; //pts which libavformat will guess, HACK FIXME
  19. /* decoder */
  20. vorbis_comment vc ;
  21. ogg_packet op;
  22. } OggVorbisContext ;
  23. static int oggvorbis_init_encoder(vorbis_info *vi, AVCodecContext *avccontext) {
  24. #ifdef OGGVORBIS_VBR_BY_ESTIMATE
  25. /* variable bitrate by estimate */
  26. return (vorbis_encode_setup_managed(vi, avccontext->channels,
  27. avccontext->sample_rate, -1, avccontext->bit_rate, -1) ||
  28. vorbis_encode_ctl(vi, OV_ECTL_RATEMANAGE_AVG, NULL) ||
  29. vorbis_encode_setup_init(vi)) ;
  30. #else
  31. /* constant bitrate */
  32. return vorbis_encode_init(vi, avccontext->channels,
  33. avccontext->sample_rate, -1, avccontext->bit_rate, -1) ;
  34. #endif
  35. }
  36. static int oggvorbis_encode_init(AVCodecContext *avccontext) {
  37. OggVorbisContext *context = avccontext->priv_data ;
  38. ogg_packet header, header_comm, header_code;
  39. uint8_t *p;
  40. vorbis_info_init(&context->vi) ;
  41. if(oggvorbis_init_encoder(&context->vi, avccontext) < 0) {
  42. av_log(avccontext, AV_LOG_ERROR, "oggvorbis_encode_init: init_encoder failed") ;
  43. return -1 ;
  44. }
  45. vorbis_analysis_init(&context->vd, &context->vi) ;
  46. vorbis_block_init(&context->vd, &context->vb) ;
  47. vorbis_comment_init(&context->vc);
  48. vorbis_comment_add_tag(&context->vc, "encoder", LIBAVCODEC_IDENT) ;
  49. vorbis_analysis_headerout(&context->vd, &context->vc, &header,
  50. &header_comm, &header_code);
  51. avccontext->extradata_size= 3*2 + header.bytes + header_comm.bytes + header_code.bytes;
  52. p= avccontext->extradata= av_mallocz(avccontext->extradata_size);
  53. *(p++) = header.bytes>>8;
  54. *(p++) = header.bytes&0xFF;
  55. memcpy(p, header.packet, header.bytes);
  56. p += header.bytes;
  57. *(p++) = header_comm.bytes>>8;
  58. *(p++) = header_comm.bytes&0xFF;
  59. memcpy(p, header_comm.packet, header_comm.bytes);
  60. p += header_comm.bytes;
  61. *(p++) = header_code.bytes>>8;
  62. *(p++) = header_code.bytes&0xFF;
  63. memcpy(p, header_code.packet, header_code.bytes);
  64. /* vorbis_block_clear(&context->vb);
  65. vorbis_dsp_clear(&context->vd);
  66. vorbis_info_clear(&context->vi);*/
  67. vorbis_comment_clear(&context->vc);
  68. avccontext->frame_size = OGGVORBIS_FRAME_SIZE ;
  69. avccontext->coded_frame= avcodec_alloc_frame();
  70. avccontext->coded_frame->key_frame= 1;
  71. return 0 ;
  72. }
  73. static int oggvorbis_encode_frame(AVCodecContext *avccontext,
  74. unsigned char *packets,
  75. int buf_size, void *data)
  76. {
  77. OggVorbisContext *context = avccontext->priv_data ;
  78. float **buffer ;
  79. ogg_packet op ;
  80. signed char *audio = data ;
  81. int l, samples = OGGVORBIS_FRAME_SIZE ;
  82. buffer = vorbis_analysis_buffer(&context->vd, samples) ;
  83. if(context->vi.channels == 1) {
  84. for(l = 0 ; l < samples ; l++)
  85. buffer[0][l]=((audio[l*2+1]<<8)|(0x00ff&(int)audio[l*2]))/32768.f;
  86. } else {
  87. for(l = 0 ; l < samples ; l++){
  88. buffer[0][l]=((audio[l*4+1]<<8)|(0x00ff&(int)audio[l*4]))/32768.f;
  89. buffer[1][l]=((audio[l*4+3]<<8)|(0x00ff&(int)audio[l*4+2]))/32768.f;
  90. }
  91. }
  92. vorbis_analysis_wrote(&context->vd, samples) ;
  93. while(vorbis_analysis_blockout(&context->vd, &context->vb) == 1) {
  94. vorbis_analysis(&context->vb, NULL);
  95. vorbis_bitrate_addblock(&context->vb) ;
  96. while(vorbis_bitrate_flushpacket(&context->vd, &op)) {
  97. memcpy(context->buffer + context->buffer_index, &op, sizeof(ogg_packet));
  98. context->buffer_index += sizeof(ogg_packet);
  99. memcpy(context->buffer + context->buffer_index, op.packet, op.bytes);
  100. context->buffer_index += op.bytes;
  101. // av_log(avccontext, AV_LOG_DEBUG, "e%d / %d\n", context->buffer_index, op.bytes);
  102. }
  103. }
  104. l=0;
  105. if(context->buffer_index){
  106. ogg_packet *op2= (ogg_packet*)context->buffer;
  107. op2->packet = context->buffer + sizeof(ogg_packet);
  108. if(op2->granulepos <= context->fake_pts /*&& (context->fake_pts || context->buffer_index > 4*1024)*/){
  109. assert(op2->granulepos == context->fake_pts);
  110. l= op2->bytes;
  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. }
  115. // av_log(avccontext, AV_LOG_DEBUG, "E%d\n", l);
  116. }
  117. if(l || context->fake_pts){
  118. context->fake_pts += avccontext->frame_size;
  119. }
  120. return l;
  121. }
  122. static int oggvorbis_encode_close(AVCodecContext *avccontext) {
  123. OggVorbisContext *context = avccontext->priv_data ;
  124. /* ogg_packet op ; */
  125. vorbis_analysis_wrote(&context->vd, 0) ; /* notify vorbisenc this is EOF */
  126. /* We need to write all the remaining packets into the stream
  127. * on closing */
  128. av_log(avccontext, AV_LOG_ERROR, "fixme: not all packets written on oggvorbis_encode_close()\n") ;
  129. /*
  130. while(vorbis_bitrate_flushpacket(&context->vd, &op)) {
  131. memcpy(packets + l, &op, sizeof(ogg_packet)) ;
  132. memcpy(packets + l + sizeof(ogg_packet), op.packet, op.bytes) ;
  133. l += sizeof(ogg_packet) + op.bytes ;
  134. }
  135. */
  136. vorbis_block_clear(&context->vb);
  137. vorbis_dsp_clear(&context->vd);
  138. vorbis_info_clear(&context->vi);
  139. av_freep(&avccontext->coded_frame);
  140. av_freep(&avccontext->extradata);
  141. return 0 ;
  142. }
  143. AVCodec oggvorbis_encoder = {
  144. "vorbis",
  145. CODEC_TYPE_AUDIO,
  146. CODEC_ID_VORBIS,
  147. sizeof(OggVorbisContext),
  148. oggvorbis_encode_init,
  149. oggvorbis_encode_frame,
  150. oggvorbis_encode_close
  151. } ;
  152. static int oggvorbis_decode_init(AVCodecContext *avccontext) {
  153. OggVorbisContext *context = avccontext->priv_data ;
  154. uint8_t *p= avccontext->extradata;
  155. int i;
  156. vorbis_info_init(&context->vi) ;
  157. vorbis_comment_init(&context->vc) ;
  158. for(i=0; i<3; i++){
  159. context->op.b_o_s= i==0;
  160. context->op.bytes= *(p++)<<8;
  161. context->op.bytes+=*(p++);
  162. context->op.packet= p;
  163. p += context->op.bytes;
  164. if(vorbis_synthesis_headerin(&context->vi, &context->vc, &context->op)<0){
  165. av_log(avccontext, AV_LOG_ERROR, "%d. vorbis header damaged\n", i+1);
  166. return -1;
  167. }
  168. }
  169. avccontext->channels = context->vi.channels;
  170. avccontext->sample_rate = context->vi.rate;
  171. vorbis_synthesis_init(&context->vd, &context->vi);
  172. vorbis_block_init(&context->vd, &context->vb);
  173. return 0 ;
  174. }
  175. static inline int conv(int samples, float **pcm, char *buf, int channels) {
  176. int i, j, val ;
  177. ogg_int16_t *ptr, *data = (ogg_int16_t*)buf ;
  178. float *mono ;
  179. for(i = 0 ; i < channels ; i++){
  180. ptr = &data[i];
  181. mono = pcm[i] ;
  182. for(j = 0 ; j < samples ; j++) {
  183. val = mono[j] * 32767.f;
  184. if(val > 32767) val = 32767 ;
  185. if(val < -32768) val = -32768 ;
  186. *ptr = val ;
  187. ptr += channels;
  188. }
  189. }
  190. return 0 ;
  191. }
  192. static int oggvorbis_decode_frame(AVCodecContext *avccontext,
  193. void *data, int *data_size,
  194. uint8_t *buf, int buf_size)
  195. {
  196. OggVorbisContext *context = avccontext->priv_data ;
  197. float **pcm ;
  198. ogg_packet *op= &context->op;
  199. int samples, total_samples, total_bytes,i;
  200. if(!buf_size){
  201. //FIXME flush
  202. *data_size=0;
  203. return 0;
  204. }
  205. op->packet = buf;
  206. op->bytes = buf_size;
  207. // 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);
  208. /* for(i=0; i<op->bytes; i++)
  209. av_log(avccontext, AV_LOG_DEBUG, "%02X ", op->packet[i]);
  210. av_log(avccontext, AV_LOG_DEBUG, "\n");*/
  211. if(vorbis_synthesis(&context->vb, op) == 0)
  212. vorbis_synthesis_blockin(&context->vd, &context->vb) ;
  213. total_samples = 0 ;
  214. total_bytes = 0 ;
  215. while((samples = vorbis_synthesis_pcmout(&context->vd, &pcm)) > 0) {
  216. conv(samples, pcm, (char*)data + total_bytes, context->vi.channels) ;
  217. total_bytes += samples * 2 * context->vi.channels ;
  218. total_samples += samples ;
  219. vorbis_synthesis_read(&context->vd, samples) ;
  220. }
  221. *data_size = total_bytes ;
  222. return buf_size ;
  223. }
  224. static int oggvorbis_decode_close(AVCodecContext *avccontext) {
  225. OggVorbisContext *context = avccontext->priv_data ;
  226. vorbis_info_clear(&context->vi) ;
  227. vorbis_comment_clear(&context->vc) ;
  228. return 0 ;
  229. }
  230. AVCodec oggvorbis_decoder = {
  231. "vorbis",
  232. CODEC_TYPE_AUDIO,
  233. CODEC_ID_VORBIS,
  234. sizeof(OggVorbisContext),
  235. oggvorbis_decode_init,
  236. NULL,
  237. oggvorbis_decode_close,
  238. oggvorbis_decode_frame,
  239. } ;