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.

352 lines
10KB

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