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.

219 lines
7.0KB

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