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.

201 lines
6.4KB

  1. /*
  2. * Copyright (c) 2002 Mark Hills <mark@pogo.org.uk>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <vorbis/vorbisenc.h>
  21. #include "avcodec.h"
  22. #include "bytestream.h"
  23. typedef struct OggVorbisDecContext {
  24. AVFrame frame;
  25. vorbis_info vi; /**< vorbis_info used during init */
  26. vorbis_dsp_state vd; /**< DSP state used for analysis */
  27. vorbis_block vb; /**< vorbis_block used for analysis */
  28. vorbis_comment vc; /**< VorbisComment info */
  29. ogg_packet op; /**< ogg packet */
  30. } OggVorbisDecContext;
  31. static int oggvorbis_decode_init(AVCodecContext *avccontext) {
  32. OggVorbisDecContext *context = avccontext->priv_data ;
  33. uint8_t *p= avccontext->extradata;
  34. int i, hsizes[3];
  35. unsigned char *headers[3], *extradata = avccontext->extradata;
  36. vorbis_info_init(&context->vi) ;
  37. vorbis_comment_init(&context->vc) ;
  38. if(! avccontext->extradata_size || ! p) {
  39. av_log(avccontext, AV_LOG_ERROR, "vorbis extradata absent\n");
  40. return -1;
  41. }
  42. if(p[0] == 0 && p[1] == 30) {
  43. for(i = 0; i < 3; i++){
  44. hsizes[i] = bytestream_get_be16(&p);
  45. headers[i] = p;
  46. p += hsizes[i];
  47. }
  48. } else if(*p == 2) {
  49. unsigned int offset = 1;
  50. p++;
  51. for(i=0; i<2; i++) {
  52. hsizes[i] = 0;
  53. while((*p == 0xFF) && (offset < avccontext->extradata_size)) {
  54. hsizes[i] += 0xFF;
  55. offset++;
  56. p++;
  57. }
  58. if(offset >= avccontext->extradata_size - 1) {
  59. av_log(avccontext, AV_LOG_ERROR,
  60. "vorbis header sizes damaged\n");
  61. return -1;
  62. }
  63. hsizes[i] += *p;
  64. offset++;
  65. p++;
  66. }
  67. hsizes[2] = avccontext->extradata_size - hsizes[0]-hsizes[1]-offset;
  68. #if 0
  69. av_log(avccontext, AV_LOG_DEBUG,
  70. "vorbis header sizes: %d, %d, %d, / extradata_len is %d \n",
  71. hsizes[0], hsizes[1], hsizes[2], avccontext->extradata_size);
  72. #endif
  73. headers[0] = extradata + offset;
  74. headers[1] = extradata + offset + hsizes[0];
  75. headers[2] = extradata + offset + hsizes[0] + hsizes[1];
  76. } else {
  77. av_log(avccontext, AV_LOG_ERROR,
  78. "vorbis initial header len is wrong: %d\n", *p);
  79. return -1;
  80. }
  81. for(i=0; i<3; i++){
  82. context->op.b_o_s= i==0;
  83. context->op.bytes = hsizes[i];
  84. context->op.packet = headers[i];
  85. if(vorbis_synthesis_headerin(&context->vi, &context->vc, &context->op)<0){
  86. av_log(avccontext, AV_LOG_ERROR, "%d. vorbis header damaged\n", i+1);
  87. return -1;
  88. }
  89. }
  90. avccontext->channels = context->vi.channels;
  91. avccontext->sample_rate = context->vi.rate;
  92. avccontext->time_base= (AVRational){1, avccontext->sample_rate};
  93. vorbis_synthesis_init(&context->vd, &context->vi);
  94. vorbis_block_init(&context->vd, &context->vb);
  95. return 0 ;
  96. }
  97. static inline int conv(int samples, float **pcm, char *buf, int channels) {
  98. int i, j;
  99. ogg_int16_t *ptr, *data = (ogg_int16_t*)buf ;
  100. float *mono ;
  101. for(i = 0 ; i < channels ; i++){
  102. ptr = &data[i];
  103. mono = pcm[i] ;
  104. for(j = 0 ; j < samples ; j++) {
  105. *ptr = av_clip_int16(mono[j] * 32767.f);
  106. ptr += channels;
  107. }
  108. }
  109. return 0 ;
  110. }
  111. static int oggvorbis_decode_frame(AVCodecContext *avccontext, void *data,
  112. int *got_frame_ptr, AVPacket *avpkt)
  113. {
  114. OggVorbisDecContext *context = avccontext->priv_data ;
  115. float **pcm ;
  116. ogg_packet *op= &context->op;
  117. int samples, total_samples, total_bytes;
  118. int ret;
  119. int16_t *output;
  120. if(!avpkt->size){
  121. //FIXME flush
  122. return 0;
  123. }
  124. context->frame.nb_samples = 8192*4;
  125. if ((ret = avccontext->get_buffer(avccontext, &context->frame)) < 0) {
  126. av_log(avccontext, AV_LOG_ERROR, "get_buffer() failed\n");
  127. return ret;
  128. }
  129. output = (int16_t *)context->frame.data[0];
  130. op->packet = avpkt->data;
  131. op->bytes = avpkt->size;
  132. // av_log(avccontext, AV_LOG_DEBUG, "%d %d %d %"PRId64" %"PRId64" %d %d\n", op->bytes, op->b_o_s, op->e_o_s, op->granulepos, op->packetno, buf_size, context->vi.rate);
  133. /* for(i=0; i<op->bytes; i++)
  134. av_log(avccontext, AV_LOG_DEBUG, "%02X ", op->packet[i]);
  135. av_log(avccontext, AV_LOG_DEBUG, "\n");*/
  136. if(vorbis_synthesis(&context->vb, op) == 0)
  137. vorbis_synthesis_blockin(&context->vd, &context->vb) ;
  138. total_samples = 0 ;
  139. total_bytes = 0 ;
  140. while((samples = vorbis_synthesis_pcmout(&context->vd, &pcm)) > 0) {
  141. conv(samples, pcm, (char*)output + total_bytes, context->vi.channels) ;
  142. total_bytes += samples * 2 * context->vi.channels ;
  143. total_samples += samples ;
  144. vorbis_synthesis_read(&context->vd, samples) ;
  145. }
  146. context->frame.nb_samples = total_samples;
  147. *got_frame_ptr = 1;
  148. *(AVFrame *)data = context->frame;
  149. return avpkt->size;
  150. }
  151. static int oggvorbis_decode_close(AVCodecContext *avccontext) {
  152. OggVorbisDecContext *context = avccontext->priv_data ;
  153. vorbis_info_clear(&context->vi) ;
  154. vorbis_comment_clear(&context->vc) ;
  155. return 0 ;
  156. }
  157. AVCodec ff_libvorbis_decoder = {
  158. .name = "libvorbis",
  159. .type = AVMEDIA_TYPE_AUDIO,
  160. .id = AV_CODEC_ID_VORBIS,
  161. .priv_data_size = sizeof(OggVorbisDecContext),
  162. .init = oggvorbis_decode_init,
  163. .decode = oggvorbis_decode_frame,
  164. .close = oggvorbis_decode_close,
  165. .capabilities = CODEC_CAP_DELAY,
  166. .long_name = NULL_IF_CONFIG_SMALL("libvorbis"),
  167. };