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.

222 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. unsigned int sizesum = 1;
  59. p++;
  60. for(i=0; i<2; i++) {
  61. hsizes[i] = 0;
  62. while((*p == 0xFF) && (sizesum < avccontext->extradata_size)) {
  63. hsizes[i] += 0xFF;
  64. offset++;
  65. sizesum += 1 + 0xFF;
  66. p++;
  67. }
  68. hsizes[i] += *p;
  69. offset++;
  70. sizesum += 1 + *p;
  71. if(sizesum > avccontext->extradata_size) {
  72. av_log(avccontext, AV_LOG_ERROR,
  73. "vorbis header sizes damaged\n");
  74. ret = AVERROR_INVALIDDATA;
  75. goto error;
  76. }
  77. p++;
  78. }
  79. hsizes[2] = avccontext->extradata_size - hsizes[0]-hsizes[1]-offset;
  80. #if 0
  81. av_log(avccontext, AV_LOG_DEBUG,
  82. "vorbis header sizes: %d, %d, %d, / extradata_len is %d \n",
  83. hsizes[0], hsizes[1], hsizes[2], avccontext->extradata_size);
  84. #endif
  85. headers[0] = extradata + offset;
  86. headers[1] = extradata + offset + hsizes[0];
  87. headers[2] = extradata + offset + hsizes[0] + hsizes[1];
  88. } else {
  89. av_log(avccontext, AV_LOG_ERROR,
  90. "vorbis initial header len is wrong: %d\n", *p);
  91. ret = AVERROR_INVALIDDATA;
  92. goto error;
  93. }
  94. for(i=0; i<3; i++){
  95. context->op.b_o_s= i==0;
  96. context->op.bytes = hsizes[i];
  97. context->op.packet = headers[i];
  98. if(vorbis_synthesis_headerin(&context->vi, &context->vc, &context->op)<0){
  99. av_log(avccontext, AV_LOG_ERROR, "%d. vorbis header damaged\n", i+1);
  100. ret = AVERROR_INVALIDDATA;
  101. goto error;
  102. }
  103. }
  104. avccontext->channels = context->vi.channels;
  105. avccontext->sample_rate = context->vi.rate;
  106. avccontext->sample_fmt = AV_SAMPLE_FMT_S16;
  107. avccontext->time_base= (AVRational){1, avccontext->sample_rate};
  108. vorbis_synthesis_init(&context->vd, &context->vi);
  109. vorbis_block_init(&context->vd, &context->vb);
  110. return 0 ;
  111. error:
  112. oggvorbis_decode_close(avccontext);
  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_block_clear(&context->vb);
  170. vorbis_dsp_clear(&context->vd);
  171. vorbis_info_clear(&context->vi) ;
  172. vorbis_comment_clear(&context->vc) ;
  173. return 0 ;
  174. }
  175. AVCodec ff_libvorbis_decoder = {
  176. .name = "libvorbis",
  177. .long_name = NULL_IF_CONFIG_SMALL("libvorbis"),
  178. .type = AVMEDIA_TYPE_AUDIO,
  179. .id = AV_CODEC_ID_VORBIS,
  180. .priv_data_size = sizeof(OggVorbisDecContext),
  181. .init = oggvorbis_decode_init,
  182. .decode = oggvorbis_decode_frame,
  183. .close = oggvorbis_decode_close,
  184. .capabilities = AV_CODEC_CAP_DELAY,
  185. };