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.

337 lines
9.7KB

  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. unsigned int offset, len;
  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. len = header.bytes + header_comm.bytes + header_code.bytes;
  52. avccontext->extradata_size= 64 + len + len/255;
  53. p = avccontext->extradata= av_mallocz(avccontext->extradata_size);
  54. p[0] = 2;
  55. offset = 1;
  56. offset += av_xiphlacing(&p[offset], header.bytes);
  57. offset += av_xiphlacing(&p[offset], header_comm.bytes);
  58. memcpy(&p[offset], header.packet, header.bytes);
  59. offset += header.bytes;
  60. memcpy(&p[offset], header_comm.packet, header_comm.bytes);
  61. offset += header_comm.bytes;
  62. memcpy(&p[offset], header_code.packet, header_code.bytes);
  63. offset += header_code.bytes;
  64. avccontext->extradata_size = offset;
  65. avccontext->extradata= av_realloc(avccontext->extradata, avccontext->extradata_size);
  66. /* vorbis_block_clear(&context->vb);
  67. vorbis_dsp_clear(&context->vd);
  68. vorbis_info_clear(&context->vi);*/
  69. vorbis_comment_clear(&context->vc);
  70. avccontext->frame_size = OGGVORBIS_FRAME_SIZE ;
  71. avccontext->coded_frame= avcodec_alloc_frame();
  72. avccontext->coded_frame->key_frame= 1;
  73. return 0 ;
  74. }
  75. static int oggvorbis_encode_frame(AVCodecContext *avccontext,
  76. unsigned char *packets,
  77. int buf_size, void *data)
  78. {
  79. OggVorbisContext *context = avccontext->priv_data ;
  80. float **buffer ;
  81. ogg_packet op ;
  82. signed short *audio = data ;
  83. int l, samples = data ? OGGVORBIS_FRAME_SIZE : 0;
  84. buffer = vorbis_analysis_buffer(&context->vd, samples) ;
  85. if(context->vi.channels == 1) {
  86. for(l = 0 ; l < samples ; l++)
  87. buffer[0][l]=audio[l]/32768.f;
  88. } else {
  89. for(l = 0 ; l < samples ; l++){
  90. buffer[0][l]=audio[l*2]/32768.f;
  91. buffer[1][l]=audio[l*2+1]/32768.f;
  92. }
  93. }
  94. vorbis_analysis_wrote(&context->vd, samples) ;
  95. while(vorbis_analysis_blockout(&context->vd, &context->vb) == 1) {
  96. vorbis_analysis(&context->vb, NULL);
  97. vorbis_bitrate_addblock(&context->vb) ;
  98. while(vorbis_bitrate_flushpacket(&context->vd, &op)) {
  99. 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
  100. continue;
  101. memcpy(context->buffer + context->buffer_index, &op, sizeof(ogg_packet));
  102. context->buffer_index += sizeof(ogg_packet);
  103. memcpy(context->buffer + context->buffer_index, op.packet, op.bytes);
  104. context->buffer_index += op.bytes;
  105. // av_log(avccontext, AV_LOG_DEBUG, "e%d / %d\n", context->buffer_index, op.bytes);
  106. }
  107. }
  108. l=0;
  109. if(context->buffer_index){
  110. ogg_packet *op2= (ogg_packet*)context->buffer;
  111. op2->packet = context->buffer + sizeof(ogg_packet);
  112. l= op2->bytes;
  113. avccontext->coded_frame->pts= op2->granulepos;
  114. memcpy(packets, op2->packet, l);
  115. context->buffer_index -= l + sizeof(ogg_packet);
  116. memcpy(context->buffer, context->buffer + l + sizeof(ogg_packet), context->buffer_index);
  117. // av_log(avccontext, AV_LOG_DEBUG, "E%d\n", l);
  118. }
  119. return l;
  120. }
  121. static int oggvorbis_encode_close(AVCodecContext *avccontext) {
  122. OggVorbisContext *context = avccontext->priv_data ;
  123. /* ogg_packet op ; */
  124. vorbis_analysis_wrote(&context->vd, 0) ; /* notify vorbisenc this is EOF */
  125. vorbis_block_clear(&context->vb);
  126. vorbis_dsp_clear(&context->vd);
  127. vorbis_info_clear(&context->vi);
  128. av_freep(&avccontext->coded_frame);
  129. av_freep(&avccontext->extradata);
  130. return 0 ;
  131. }
  132. AVCodec oggvorbis_encoder = {
  133. "vorbis",
  134. CODEC_TYPE_AUDIO,
  135. CODEC_ID_VORBIS,
  136. sizeof(OggVorbisContext),
  137. oggvorbis_encode_init,
  138. oggvorbis_encode_frame,
  139. oggvorbis_encode_close,
  140. .capabilities= CODEC_CAP_DELAY,
  141. } ;
  142. static int oggvorbis_decode_init(AVCodecContext *avccontext) {
  143. OggVorbisContext *context = avccontext->priv_data ;
  144. uint8_t *p= avccontext->extradata;
  145. int i, hsizes[3];
  146. unsigned char *headers[3], *extradata = avccontext->extradata;
  147. unsigned int offset;
  148. vorbis_info_init(&context->vi) ;
  149. vorbis_comment_init(&context->vc) ;
  150. if(! avccontext->extradata_size || ! p) {
  151. av_log(avccontext, AV_LOG_ERROR, "vorbis extradata absent\n");
  152. return -1;
  153. }
  154. if(*p != 2) {
  155. av_log(avccontext, AV_LOG_ERROR,
  156. "vorbis initial header len is wrong: %d\n", *p);
  157. return -1;
  158. }
  159. offset = 1;
  160. p++;
  161. for(i=0; i<2; i++) {
  162. hsizes[i] = 0;
  163. while((*p == 0xFF) && (offset < avccontext->extradata_size)) {
  164. hsizes[i] += 0xFF;
  165. offset++;
  166. p++;
  167. }
  168. if(offset >= avccontext->extradata_size - 1) {
  169. av_log(avccontext, AV_LOG_ERROR, "vorbis header sizes damaged\n");
  170. return -1;
  171. }
  172. hsizes[i] += *p;
  173. offset++;
  174. p++;
  175. }
  176. hsizes[2] = avccontext->extradata_size - hsizes[0] - hsizes[1] - offset;
  177. #if 0
  178. av_log(avccontext, AV_LOG_DEBUG,
  179. "vorbis header sizes: %d, %d, %d, / extradata_len is %d \n",
  180. hsizes[0], hsizes[1], hsizes[2], avccontext->extradata_size);
  181. #endif
  182. headers[0] = extradata + offset;
  183. headers[1] = extradata + offset + hsizes[0];
  184. headers[2] = extradata + offset + hsizes[0] + hsizes[1];
  185. for(i=0; i<3; i++){
  186. context->op.b_o_s= i==0;
  187. context->op.bytes = hsizes[i];
  188. context->op.packet = headers[i];
  189. if(vorbis_synthesis_headerin(&context->vi, &context->vc, &context->op)<0){
  190. av_log(avccontext, AV_LOG_ERROR, "%d. vorbis header damaged\n", i+1);
  191. return -1;
  192. }
  193. }
  194. avccontext->channels = context->vi.channels;
  195. avccontext->sample_rate = context->vi.rate;
  196. avccontext->time_base= (AVRational){1, avccontext->sample_rate};
  197. vorbis_synthesis_init(&context->vd, &context->vi);
  198. vorbis_block_init(&context->vd, &context->vb);
  199. return 0 ;
  200. }
  201. static inline int conv(int samples, float **pcm, char *buf, int channels) {
  202. int i, j, val ;
  203. ogg_int16_t *ptr, *data = (ogg_int16_t*)buf ;
  204. float *mono ;
  205. for(i = 0 ; i < channels ; i++){
  206. ptr = &data[i];
  207. mono = pcm[i] ;
  208. for(j = 0 ; j < samples ; j++) {
  209. val = mono[j] * 32767.f;
  210. if(val > 32767) val = 32767 ;
  211. if(val < -32768) val = -32768 ;
  212. *ptr = val ;
  213. ptr += channels;
  214. }
  215. }
  216. return 0 ;
  217. }
  218. static int oggvorbis_decode_frame(AVCodecContext *avccontext,
  219. void *data, int *data_size,
  220. uint8_t *buf, int buf_size)
  221. {
  222. OggVorbisContext *context = avccontext->priv_data ;
  223. float **pcm ;
  224. ogg_packet *op= &context->op;
  225. int samples, total_samples, total_bytes,i;
  226. if(!buf_size){
  227. //FIXME flush
  228. return 0;
  229. }
  230. op->packet = buf;
  231. op->bytes = buf_size;
  232. // 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);
  233. /* for(i=0; i<op->bytes; i++)
  234. av_log(avccontext, AV_LOG_DEBUG, "%02X ", op->packet[i]);
  235. av_log(avccontext, AV_LOG_DEBUG, "\n");*/
  236. if(vorbis_synthesis(&context->vb, op) == 0)
  237. vorbis_synthesis_blockin(&context->vd, &context->vb) ;
  238. total_samples = 0 ;
  239. total_bytes = 0 ;
  240. while((samples = vorbis_synthesis_pcmout(&context->vd, &pcm)) > 0) {
  241. conv(samples, pcm, (char*)data + total_bytes, context->vi.channels) ;
  242. total_bytes += samples * 2 * context->vi.channels ;
  243. total_samples += samples ;
  244. vorbis_synthesis_read(&context->vd, samples) ;
  245. }
  246. *data_size = total_bytes ;
  247. return buf_size ;
  248. }
  249. static int oggvorbis_decode_close(AVCodecContext *avccontext) {
  250. OggVorbisContext *context = avccontext->priv_data ;
  251. vorbis_info_clear(&context->vi) ;
  252. vorbis_comment_clear(&context->vc) ;
  253. return 0 ;
  254. }
  255. AVCodec oggvorbis_decoder = {
  256. "vorbis",
  257. CODEC_TYPE_AUDIO,
  258. CODEC_ID_VORBIS,
  259. sizeof(OggVorbisContext),
  260. oggvorbis_decode_init,
  261. NULL,
  262. oggvorbis_decode_close,
  263. oggvorbis_decode_frame,
  264. .capabilities= CODEC_CAP_DELAY,
  265. } ;