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.

202 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. #include "internal.h"
  24. typedef struct OggVorbisDecContext {
  25. AVFrame frame;
  26. vorbis_info vi; /**< vorbis_info used during init */
  27. vorbis_dsp_state vd; /**< DSP state used for analysis */
  28. vorbis_block vb; /**< vorbis_block used for analysis */
  29. vorbis_comment vc; /**< VorbisComment info */
  30. ogg_packet op; /**< ogg packet */
  31. } OggVorbisDecContext;
  32. static int oggvorbis_decode_init(AVCodecContext *avccontext) {
  33. OggVorbisDecContext *context = avccontext->priv_data ;
  34. uint8_t *p= avccontext->extradata;
  35. int i, hsizes[3];
  36. unsigned char *headers[3], *extradata = avccontext->extradata;
  37. vorbis_info_init(&context->vi) ;
  38. vorbis_comment_init(&context->vc) ;
  39. if(! avccontext->extradata_size || ! p) {
  40. av_log(avccontext, AV_LOG_ERROR, "vorbis extradata absent\n");
  41. return -1;
  42. }
  43. if(p[0] == 0 && p[1] == 30) {
  44. for(i = 0; i < 3; i++){
  45. hsizes[i] = bytestream_get_be16((const uint8_t **)&p);
  46. headers[i] = p;
  47. p += hsizes[i];
  48. }
  49. } else if(*p == 2) {
  50. unsigned int offset = 1;
  51. p++;
  52. for(i=0; i<2; i++) {
  53. hsizes[i] = 0;
  54. while((*p == 0xFF) && (offset < avccontext->extradata_size)) {
  55. hsizes[i] += 0xFF;
  56. offset++;
  57. p++;
  58. }
  59. if(offset >= avccontext->extradata_size - 1) {
  60. av_log(avccontext, AV_LOG_ERROR,
  61. "vorbis header sizes damaged\n");
  62. return -1;
  63. }
  64. hsizes[i] += *p;
  65. offset++;
  66. p++;
  67. }
  68. hsizes[2] = avccontext->extradata_size - hsizes[0]-hsizes[1]-offset;
  69. #if 0
  70. av_log(avccontext, AV_LOG_DEBUG,
  71. "vorbis header sizes: %d, %d, %d, / extradata_len is %d \n",
  72. hsizes[0], hsizes[1], hsizes[2], avccontext->extradata_size);
  73. #endif
  74. headers[0] = extradata + offset;
  75. headers[1] = extradata + offset + hsizes[0];
  76. headers[2] = extradata + offset + hsizes[0] + hsizes[1];
  77. } else {
  78. av_log(avccontext, AV_LOG_ERROR,
  79. "vorbis initial header len is wrong: %d\n", *p);
  80. return -1;
  81. }
  82. for(i=0; i<3; i++){
  83. context->op.b_o_s= i==0;
  84. context->op.bytes = hsizes[i];
  85. context->op.packet = headers[i];
  86. if(vorbis_synthesis_headerin(&context->vi, &context->vc, &context->op)<0){
  87. av_log(avccontext, AV_LOG_ERROR, "%d. vorbis header damaged\n", i+1);
  88. return -1;
  89. }
  90. }
  91. avccontext->channels = context->vi.channels;
  92. avccontext->sample_rate = context->vi.rate;
  93. avccontext->time_base= (AVRational){1, avccontext->sample_rate};
  94. vorbis_synthesis_init(&context->vd, &context->vi);
  95. vorbis_block_init(&context->vd, &context->vb);
  96. return 0 ;
  97. }
  98. static inline int conv(int samples, float **pcm, char *buf, int channels) {
  99. int i, j;
  100. ogg_int16_t *ptr, *data = (ogg_int16_t*)buf ;
  101. float *mono ;
  102. for(i = 0 ; i < channels ; i++){
  103. ptr = &data[i];
  104. mono = pcm[i] ;
  105. for(j = 0 ; j < samples ; j++) {
  106. *ptr = av_clip_int16(mono[j] * 32767.f);
  107. ptr += channels;
  108. }
  109. }
  110. return 0 ;
  111. }
  112. static int oggvorbis_decode_frame(AVCodecContext *avccontext, void *data,
  113. int *got_frame_ptr, AVPacket *avpkt)
  114. {
  115. OggVorbisDecContext *context = avccontext->priv_data ;
  116. float **pcm ;
  117. ogg_packet *op= &context->op;
  118. int samples, total_samples, total_bytes;
  119. int ret;
  120. int16_t *output;
  121. if(!avpkt->size){
  122. //FIXME flush
  123. return 0;
  124. }
  125. context->frame.nb_samples = 8192*4;
  126. if ((ret = ff_get_buffer(avccontext, &context->frame)) < 0) {
  127. av_log(avccontext, AV_LOG_ERROR, "get_buffer() failed\n");
  128. return ret;
  129. }
  130. output = (int16_t *)context->frame.data[0];
  131. op->packet = avpkt->data;
  132. op->bytes = avpkt->size;
  133. // 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);
  134. /* for(i=0; i<op->bytes; i++)
  135. av_log(avccontext, AV_LOG_DEBUG, "%02X ", op->packet[i]);
  136. av_log(avccontext, AV_LOG_DEBUG, "\n");*/
  137. if(vorbis_synthesis(&context->vb, op) == 0)
  138. vorbis_synthesis_blockin(&context->vd, &context->vb) ;
  139. total_samples = 0 ;
  140. total_bytes = 0 ;
  141. while((samples = vorbis_synthesis_pcmout(&context->vd, &pcm)) > 0) {
  142. conv(samples, pcm, (char*)output + total_bytes, context->vi.channels) ;
  143. total_bytes += samples * 2 * context->vi.channels ;
  144. total_samples += samples ;
  145. vorbis_synthesis_read(&context->vd, samples) ;
  146. }
  147. context->frame.nb_samples = total_samples;
  148. *got_frame_ptr = 1;
  149. *(AVFrame *)data = context->frame;
  150. return avpkt->size;
  151. }
  152. static int oggvorbis_decode_close(AVCodecContext *avccontext) {
  153. OggVorbisDecContext *context = avccontext->priv_data ;
  154. vorbis_info_clear(&context->vi) ;
  155. vorbis_comment_clear(&context->vc) ;
  156. return 0 ;
  157. }
  158. AVCodec ff_libvorbis_decoder = {
  159. .name = "libvorbis",
  160. .type = AVMEDIA_TYPE_AUDIO,
  161. .id = AV_CODEC_ID_VORBIS,
  162. .priv_data_size = sizeof(OggVorbisDecContext),
  163. .init = oggvorbis_decode_init,
  164. .decode = oggvorbis_decode_frame,
  165. .close = oggvorbis_decode_close,
  166. .capabilities = CODEC_CAP_DELAY,
  167. .long_name = NULL_IF_CONFIG_SMALL("libvorbis"),
  168. };