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.

346 lines
10.0KB

  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. vorbis_info_init(&context->vi) ;
  148. vorbis_comment_init(&context->vc) ;
  149. if(! avccontext->extradata_size || ! p) {
  150. av_log(avccontext, AV_LOG_ERROR, "vorbis extradata absent\n");
  151. return -1;
  152. }
  153. if(p[0] == 0 && p[1] == 30) {
  154. for(i = 0; i < 3; i++){
  155. hsizes[i] = *p++ << 8;
  156. hsizes[i] += *p++;
  157. headers[i] = p;
  158. p += hsizes[i];
  159. }
  160. } else if(*p == 2) {
  161. unsigned int offset = 1;
  162. p++;
  163. for(i=0; i<2; i++) {
  164. hsizes[i] = 0;
  165. while((*p == 0xFF) && (offset < avccontext->extradata_size)) {
  166. hsizes[i] += 0xFF;
  167. offset++;
  168. p++;
  169. }
  170. if(offset >= avccontext->extradata_size - 1) {
  171. av_log(avccontext, AV_LOG_ERROR,
  172. "vorbis header sizes damaged\n");
  173. return -1;
  174. }
  175. hsizes[i] += *p;
  176. offset++;
  177. p++;
  178. }
  179. hsizes[2] = avccontext->extradata_size - hsizes[0]-hsizes[1]-offset;
  180. #if 0
  181. av_log(avccontext, AV_LOG_DEBUG,
  182. "vorbis header sizes: %d, %d, %d, / extradata_len is %d \n",
  183. hsizes[0], hsizes[1], hsizes[2], avccontext->extradata_size);
  184. #endif
  185. headers[0] = extradata + offset;
  186. headers[1] = extradata + offset + hsizes[0];
  187. headers[2] = extradata + offset + hsizes[0] + hsizes[1];
  188. } else {
  189. av_log(avccontext, AV_LOG_ERROR,
  190. "vorbis initial header len is wrong: %d\n", *p);
  191. return -1;
  192. }
  193. for(i=0; i<3; i++){
  194. context->op.b_o_s= i==0;
  195. context->op.bytes = hsizes[i];
  196. context->op.packet = headers[i];
  197. if(vorbis_synthesis_headerin(&context->vi, &context->vc, &context->op)<0){
  198. av_log(avccontext, AV_LOG_ERROR, "%d. vorbis header damaged\n", i+1);
  199. return -1;
  200. }
  201. }
  202. avccontext->channels = context->vi.channels;
  203. avccontext->sample_rate = context->vi.rate;
  204. avccontext->time_base= (AVRational){1, avccontext->sample_rate};
  205. vorbis_synthesis_init(&context->vd, &context->vi);
  206. vorbis_block_init(&context->vd, &context->vb);
  207. return 0 ;
  208. }
  209. static inline int conv(int samples, float **pcm, char *buf, int channels) {
  210. int i, j, val ;
  211. ogg_int16_t *ptr, *data = (ogg_int16_t*)buf ;
  212. float *mono ;
  213. for(i = 0 ; i < channels ; i++){
  214. ptr = &data[i];
  215. mono = pcm[i] ;
  216. for(j = 0 ; j < samples ; j++) {
  217. val = mono[j] * 32767.f;
  218. if(val > 32767) val = 32767 ;
  219. if(val < -32768) val = -32768 ;
  220. *ptr = val ;
  221. ptr += channels;
  222. }
  223. }
  224. return 0 ;
  225. }
  226. static int oggvorbis_decode_frame(AVCodecContext *avccontext,
  227. void *data, int *data_size,
  228. uint8_t *buf, int buf_size)
  229. {
  230. OggVorbisContext *context = avccontext->priv_data ;
  231. float **pcm ;
  232. ogg_packet *op= &context->op;
  233. int samples, total_samples, total_bytes,i;
  234. if(!buf_size){
  235. //FIXME flush
  236. return 0;
  237. }
  238. op->packet = buf;
  239. op->bytes = buf_size;
  240. // 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);
  241. /* for(i=0; i<op->bytes; i++)
  242. av_log(avccontext, AV_LOG_DEBUG, "%02X ", op->packet[i]);
  243. av_log(avccontext, AV_LOG_DEBUG, "\n");*/
  244. if(vorbis_synthesis(&context->vb, op) == 0)
  245. vorbis_synthesis_blockin(&context->vd, &context->vb) ;
  246. total_samples = 0 ;
  247. total_bytes = 0 ;
  248. while((samples = vorbis_synthesis_pcmout(&context->vd, &pcm)) > 0) {
  249. conv(samples, pcm, (char*)data + total_bytes, context->vi.channels) ;
  250. total_bytes += samples * 2 * context->vi.channels ;
  251. total_samples += samples ;
  252. vorbis_synthesis_read(&context->vd, samples) ;
  253. }
  254. *data_size = total_bytes ;
  255. return buf_size ;
  256. }
  257. static int oggvorbis_decode_close(AVCodecContext *avccontext) {
  258. OggVorbisContext *context = avccontext->priv_data ;
  259. vorbis_info_clear(&context->vi) ;
  260. vorbis_comment_clear(&context->vc) ;
  261. return 0 ;
  262. }
  263. AVCodec oggvorbis_decoder = {
  264. "vorbis",
  265. CODEC_TYPE_AUDIO,
  266. CODEC_ID_VORBIS,
  267. sizeof(OggVorbisContext),
  268. oggvorbis_decode_init,
  269. NULL,
  270. oggvorbis_decode_close,
  271. oggvorbis_decode_frame,
  272. .capabilities= CODEC_CAP_DELAY,
  273. } ;