FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
libvorbisdec.c
Go to the documentation of this file.
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 
21 #include <vorbis/vorbisenc.h>
22 
23 #include "avcodec.h"
24 #include "bytestream.h"
25 #include "internal.h"
26 
27 typedef struct OggVorbisDecContext {
29  vorbis_info vi; /**< vorbis_info used during init */
30  vorbis_dsp_state vd; /**< DSP state used for analysis */
31  vorbis_block vb; /**< vorbis_block used for analysis */
32  vorbis_comment vc; /**< VorbisComment info */
33  ogg_packet op; /**< ogg packet */
35 
36 static int oggvorbis_decode_init(AVCodecContext *avccontext) {
37  OggVorbisDecContext *context = avccontext->priv_data ;
38  uint8_t *p= avccontext->extradata;
39  int i, hsizes[3];
40  unsigned char *headers[3], *extradata = avccontext->extradata;
41 
42  vorbis_info_init(&context->vi) ;
43  vorbis_comment_init(&context->vc) ;
44 
45  if(! avccontext->extradata_size || ! p) {
46  av_log(avccontext, AV_LOG_ERROR, "vorbis extradata absent\n");
47  return -1;
48  }
49 
50  if(p[0] == 0 && p[1] == 30) {
51  for(i = 0; i < 3; i++){
52  hsizes[i] = bytestream_get_be16((const uint8_t **)&p);
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  return -1;
70  }
71  hsizes[i] += *p;
72  offset++;
73  p++;
74  }
75  hsizes[2] = avccontext->extradata_size - hsizes[0]-hsizes[1]-offset;
76 #if 0
77  av_log(avccontext, AV_LOG_DEBUG,
78  "vorbis header sizes: %d, %d, %d, / extradata_len is %d \n",
79  hsizes[0], hsizes[1], hsizes[2], avccontext->extradata_size);
80 #endif
81  headers[0] = extradata + offset;
82  headers[1] = extradata + offset + hsizes[0];
83  headers[2] = extradata + offset + hsizes[0] + hsizes[1];
84  } else {
85  av_log(avccontext, AV_LOG_ERROR,
86  "vorbis initial header len is wrong: %d\n", *p);
87  return -1;
88  }
89 
90  for(i=0; i<3; i++){
91  context->op.b_o_s= i==0;
92  context->op.bytes = hsizes[i];
93  context->op.packet = headers[i];
94  if(vorbis_synthesis_headerin(&context->vi, &context->vc, &context->op)<0){
95  av_log(avccontext, AV_LOG_ERROR, "%d. vorbis header damaged\n", i+1);
96  return -1;
97  }
98  }
99 
100  avccontext->channels = context->vi.channels;
101  avccontext->sample_rate = context->vi.rate;
102  avccontext->time_base= (AVRational){1, avccontext->sample_rate};
103 
104  vorbis_synthesis_init(&context->vd, &context->vi);
105  vorbis_block_init(&context->vd, &context->vb);
106 
107  return 0 ;
108 }
109 
110 
111 static inline int conv(int samples, float **pcm, char *buf, int channels) {
112  int i, j;
113  ogg_int16_t *ptr, *data = (ogg_int16_t*)buf ;
114  float *mono ;
115 
116  for(i = 0 ; i < channels ; i++){
117  ptr = &data[i];
118  mono = pcm[i] ;
119 
120  for(j = 0 ; j < samples ; j++) {
121  *ptr = av_clip_int16(mono[j] * 32767.f);
122  ptr += channels;
123  }
124  }
125 
126  return 0 ;
127 }
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  float **pcm ;
134  ogg_packet *op= &context->op;
135  int samples, total_samples, total_bytes;
136  int ret;
137  int16_t *output;
138 
139  if(!avpkt->size){
140  //FIXME flush
141  return 0;
142  }
143 
144  context->frame.nb_samples = 8192*4;
145  if ((ret = ff_get_buffer(avccontext, &context->frame)) < 0) {
146  av_log(avccontext, AV_LOG_ERROR, "get_buffer() failed\n");
147  return ret;
148  }
149  output = (int16_t *)context->frame.data[0];
150 
151 
152  op->packet = avpkt->data;
153  op->bytes = avpkt->size;
154 
155 // 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);
156 
157 /* for(i=0; i<op->bytes; i++)
158  av_log(avccontext, AV_LOG_DEBUG, "%02X ", op->packet[i]);
159  av_log(avccontext, AV_LOG_DEBUG, "\n");*/
160 
161  if(vorbis_synthesis(&context->vb, op) == 0)
162  vorbis_synthesis_blockin(&context->vd, &context->vb) ;
163 
164  total_samples = 0 ;
165  total_bytes = 0 ;
166 
167  while((samples = vorbis_synthesis_pcmout(&context->vd, &pcm)) > 0) {
168  conv(samples, pcm, (char*)output + total_bytes, context->vi.channels) ;
169  total_bytes += samples * 2 * context->vi.channels ;
170  total_samples += samples ;
171  vorbis_synthesis_read(&context->vd, samples) ;
172  }
173 
174  context->frame.nb_samples = total_samples;
175  *got_frame_ptr = 1;
176  *(AVFrame *)data = context->frame;
177  return avpkt->size;
178 }
179 
180 
181 static int oggvorbis_decode_close(AVCodecContext *avccontext) {
182  OggVorbisDecContext *context = avccontext->priv_data ;
183 
184  vorbis_info_clear(&context->vi) ;
185  vorbis_comment_clear(&context->vc) ;
186 
187  return 0 ;
188 }
189 
190 
192  .name = "libvorbis",
193  .type = AVMEDIA_TYPE_AUDIO,
194  .id = AV_CODEC_ID_VORBIS,
195  .priv_data_size = sizeof(OggVorbisDecContext),
199  .capabilities = CODEC_CAP_DELAY,
200  .long_name = NULL_IF_CONFIG_SMALL("libvorbis"),
201 };