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