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.

208 lines
6.6KB

  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. 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], ret;
  35. unsigned char *headers[3], *extradata = avccontext->extradata;
  36. if(! avccontext->extradata_size || ! p) {
  37. av_log(avccontext, AV_LOG_ERROR, "vorbis extradata absent\n");
  38. return AVERROR(EINVAL);
  39. }
  40. vorbis_info_init(&context->vi) ;
  41. vorbis_comment_init(&context->vc) ;
  42. if(p[0] == 0 && p[1] == 30) {
  43. for(i = 0; i < 3; i++){
  44. hsizes[i] = bytestream_get_be16((const uint8_t **)&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. ret = AVERROR_INVALIDDATA;
  62. goto error;
  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. ret = AVERROR_INVALIDDATA;
  81. goto error;
  82. }
  83. for(i=0; i<3; i++){
  84. context->op.b_o_s= i==0;
  85. context->op.bytes = hsizes[i];
  86. context->op.packet = headers[i];
  87. if(vorbis_synthesis_headerin(&context->vi, &context->vc, &context->op)<0){
  88. av_log(avccontext, AV_LOG_ERROR, "%d. vorbis header damaged\n", i+1);
  89. ret = AVERROR_INVALIDDATA;
  90. goto error;
  91. }
  92. }
  93. avccontext->channels = context->vi.channels;
  94. avccontext->sample_rate = context->vi.rate;
  95. avccontext->sample_fmt = AV_SAMPLE_FMT_S16;
  96. avccontext->time_base= (AVRational){1, avccontext->sample_rate};
  97. vorbis_synthesis_init(&context->vd, &context->vi);
  98. vorbis_block_init(&context->vd, &context->vb);
  99. return 0 ;
  100. error:
  101. vorbis_info_clear(&context->vi);
  102. vorbis_comment_clear(&context->vc) ;
  103. return ret;
  104. }
  105. static inline int conv(int samples, float **pcm, char *buf, int channels) {
  106. int i, j;
  107. ogg_int16_t *ptr, *data = (ogg_int16_t*)buf ;
  108. float *mono ;
  109. for(i = 0 ; i < channels ; i++){
  110. ptr = &data[i];
  111. mono = pcm[i] ;
  112. for(j = 0 ; j < samples ; j++) {
  113. *ptr = av_clip_int16(mono[j] * 32767.f);
  114. ptr += channels;
  115. }
  116. }
  117. return 0 ;
  118. }
  119. static int oggvorbis_decode_frame(AVCodecContext *avccontext, void *data,
  120. int *got_frame_ptr, AVPacket *avpkt)
  121. {
  122. OggVorbisDecContext *context = avccontext->priv_data ;
  123. AVFrame *frame = data;
  124. float **pcm ;
  125. ogg_packet *op= &context->op;
  126. int samples, total_samples, total_bytes;
  127. int ret;
  128. int16_t *output;
  129. if(!avpkt->size){
  130. //FIXME flush
  131. return 0;
  132. }
  133. frame->nb_samples = 8192*4;
  134. if ((ret = ff_get_buffer(avccontext, frame, 0)) < 0)
  135. return ret;
  136. output = (int16_t *)frame->data[0];
  137. op->packet = avpkt->data;
  138. op->bytes = avpkt->size;
  139. // 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);
  140. /* for(i=0; i<op->bytes; i++)
  141. av_log(avccontext, AV_LOG_DEBUG, "%02X ", op->packet[i]);
  142. av_log(avccontext, AV_LOG_DEBUG, "\n");*/
  143. if(vorbis_synthesis(&context->vb, op) == 0)
  144. vorbis_synthesis_blockin(&context->vd, &context->vb) ;
  145. total_samples = 0 ;
  146. total_bytes = 0 ;
  147. while((samples = vorbis_synthesis_pcmout(&context->vd, &pcm)) > 0) {
  148. conv(samples, pcm, (char*)output + total_bytes, context->vi.channels) ;
  149. total_bytes += samples * 2 * context->vi.channels ;
  150. total_samples += samples ;
  151. vorbis_synthesis_read(&context->vd, samples) ;
  152. }
  153. frame->nb_samples = total_samples;
  154. *got_frame_ptr = total_samples > 0;
  155. return avpkt->size;
  156. }
  157. static int oggvorbis_decode_close(AVCodecContext *avccontext) {
  158. OggVorbisDecContext *context = avccontext->priv_data ;
  159. vorbis_info_clear(&context->vi) ;
  160. vorbis_comment_clear(&context->vc) ;
  161. return 0 ;
  162. }
  163. AVCodec ff_libvorbis_decoder = {
  164. .name = "libvorbis",
  165. .long_name = NULL_IF_CONFIG_SMALL("libvorbis"),
  166. .type = AVMEDIA_TYPE_AUDIO,
  167. .id = AV_CODEC_ID_VORBIS,
  168. .priv_data_size = sizeof(OggVorbisDecContext),
  169. .init = oggvorbis_decode_init,
  170. .decode = oggvorbis_decode_frame,
  171. .close = oggvorbis_decode_close,
  172. .capabilities = AV_CODEC_CAP_DELAY,
  173. };