FFmpeg  2.8.17
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules 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 {
28  vorbis_info vi; /**< vorbis_info used during init */
29  vorbis_dsp_state vd; /**< DSP state used for analysis */
30  vorbis_block vb; /**< vorbis_block used for analysis */
31  vorbis_comment vc; /**< VorbisComment info */
32  ogg_packet op; /**< ogg packet */
34 
35 static int oggvorbis_decode_init(AVCodecContext *avccontext) {
36  OggVorbisDecContext *context = avccontext->priv_data ;
37  uint8_t *p= avccontext->extradata;
38  int i, hsizes[3], ret;
39  unsigned char *headers[3], *extradata = avccontext->extradata;
40 
41  if(! avccontext->extradata_size || ! p) {
42  av_log(avccontext, AV_LOG_ERROR, "vorbis extradata absent\n");
43  return AVERROR(EINVAL);
44  }
45 
46  vorbis_info_init(&context->vi) ;
47  vorbis_comment_init(&context->vc) ;
48 
49  if(p[0] == 0 && p[1] == 30) {
50  int sizesum = 0;
51  for(i = 0; i < 3; i++){
52  hsizes[i] = bytestream_get_be16((const uint8_t **)&p);
53  sizesum += 2 + hsizes[i];
54  if (sizesum > avccontext->extradata_size) {
55  av_log(avccontext, AV_LOG_ERROR, "vorbis extradata too small\n");
56  ret = AVERROR_INVALIDDATA;
57  goto error;
58  }
59 
60  headers[i] = p;
61  p += hsizes[i];
62  }
63  } else if(*p == 2) {
64  unsigned int offset = 1;
65  unsigned int sizesum = 1;
66  p++;
67  for(i=0; i<2; i++) {
68  hsizes[i] = 0;
69  while((*p == 0xFF) && (sizesum < avccontext->extradata_size)) {
70  hsizes[i] += 0xFF;
71  offset++;
72  sizesum += 1 + 0xFF;
73  p++;
74  }
75  hsizes[i] += *p;
76  offset++;
77  sizesum += 1 + *p;
78  if(sizesum > avccontext->extradata_size) {
79  av_log(avccontext, AV_LOG_ERROR,
80  "vorbis header sizes damaged\n");
81  ret = AVERROR_INVALIDDATA;
82  goto error;
83  }
84  p++;
85  }
86  hsizes[2] = avccontext->extradata_size - hsizes[0]-hsizes[1]-offset;
87 #if 0
88  av_log(avccontext, AV_LOG_DEBUG,
89  "vorbis header sizes: %d, %d, %d, / extradata_len is %d \n",
90  hsizes[0], hsizes[1], hsizes[2], avccontext->extradata_size);
91 #endif
92  headers[0] = extradata + offset;
93  headers[1] = extradata + offset + hsizes[0];
94  headers[2] = extradata + offset + hsizes[0] + hsizes[1];
95  } else {
96  av_log(avccontext, AV_LOG_ERROR,
97  "vorbis initial header len is wrong: %d\n", *p);
98  ret = AVERROR_INVALIDDATA;
99  goto error;
100  }
101 
102  for(i=0; i<3; i++){
103  context->op.b_o_s= i==0;
104  context->op.bytes = hsizes[i];
105  context->op.packet = headers[i];
106  if(vorbis_synthesis_headerin(&context->vi, &context->vc, &context->op)<0){
107  av_log(avccontext, AV_LOG_ERROR, "%d. vorbis header damaged\n", i+1);
108  ret = AVERROR_INVALIDDATA;
109  goto error;
110  }
111  }
112 
113  avccontext->channels = context->vi.channels;
114  avccontext->sample_rate = context->vi.rate;
115  avccontext->sample_fmt = AV_SAMPLE_FMT_S16;
116  avccontext->time_base= (AVRational){1, avccontext->sample_rate};
117 
118  vorbis_synthesis_init(&context->vd, &context->vi);
119  vorbis_block_init(&context->vd, &context->vb);
120 
121  return 0 ;
122 
123  error:
124  vorbis_info_clear(&context->vi);
125  vorbis_comment_clear(&context->vc) ;
126  return ret;
127 }
128 
129 
130 static inline int conv(int samples, float **pcm, char *buf, int channels) {
131  int i, j;
132  ogg_int16_t *ptr, *data = (ogg_int16_t*)buf ;
133  float *mono ;
134 
135  for(i = 0 ; i < channels ; i++){
136  ptr = &data[i];
137  mono = pcm[i] ;
138 
139  for(j = 0 ; j < samples ; j++) {
140  *ptr = av_clip_int16(mono[j] * 32767.f);
141  ptr += channels;
142  }
143  }
144 
145  return 0 ;
146 }
147 
148 static int oggvorbis_decode_frame(AVCodecContext *avccontext, void *data,
149  int *got_frame_ptr, AVPacket *avpkt)
150 {
151  OggVorbisDecContext *context = avccontext->priv_data ;
152  AVFrame *frame = data;
153  float **pcm ;
154  ogg_packet *op= &context->op;
155  int samples, total_samples, total_bytes;
156  int ret;
157  int16_t *output;
158 
159  if(!avpkt->size){
160  //FIXME flush
161  return 0;
162  }
163 
164  frame->nb_samples = 8192*4;
165  if ((ret = ff_get_buffer(avccontext, frame, 0)) < 0)
166  return ret;
167  output = (int16_t *)frame->data[0];
168 
169 
170  op->packet = avpkt->data;
171  op->bytes = avpkt->size;
172 
173 // 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);
174 
175 /* for(i=0; i<op->bytes; i++)
176  av_log(avccontext, AV_LOG_DEBUG, "%02X ", op->packet[i]);
177  av_log(avccontext, AV_LOG_DEBUG, "\n");*/
178 
179  if(vorbis_synthesis(&context->vb, op) == 0)
180  vorbis_synthesis_blockin(&context->vd, &context->vb) ;
181 
182  total_samples = 0 ;
183  total_bytes = 0 ;
184 
185  while((samples = vorbis_synthesis_pcmout(&context->vd, &pcm)) > 0) {
186  conv(samples, pcm, (char*)output + total_bytes, context->vi.channels) ;
187  total_bytes += samples * 2 * context->vi.channels ;
188  total_samples += samples ;
189  vorbis_synthesis_read(&context->vd, samples) ;
190  }
191 
192  frame->nb_samples = total_samples;
193  *got_frame_ptr = total_samples > 0;
194  return avpkt->size;
195 }
196 
197 
198 static int oggvorbis_decode_close(AVCodecContext *avccontext) {
199  OggVorbisDecContext *context = avccontext->priv_data ;
200 
201  vorbis_info_clear(&context->vi) ;
202  vorbis_comment_clear(&context->vc) ;
203 
204  return 0 ;
205 }
206 
207 
209  .name = "libvorbis",
210  .long_name = NULL_IF_CONFIG_SMALL("libvorbis"),
211  .type = AVMEDIA_TYPE_AUDIO,
212  .id = AV_CODEC_ID_VORBIS,
213  .priv_data_size = sizeof(OggVorbisDecContext),
216  .close = oggvorbis_decode_close,
217  .capabilities = AV_CODEC_CAP_DELAY,
218 };
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
This structure describes decoded (raw) audio or video data.
Definition: frame.h:171
ogg_packet op
ogg packet
Definition: libvorbisdec.c:32
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
static int conv(int samples, float **pcm, char *buf, int channels)
Definition: libvorbisdec.c:130
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
vorbis_dsp_state vd
DSP state used for analysis.
Definition: libvorbisdec.c:29
int size
Definition: avcodec.h:1434
AVCodec.
Definition: avcodec.h:3482
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1641
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition: avcodec.h:882
vorbis_block vb
vorbis_block used for analysis
Definition: libvorbisdec.c:30
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:2280
uint8_t
static int oggvorbis_decode_init(AVCodecContext *avccontext)
Definition: libvorbisdec.c:35
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1627
static AVFrame * frame
uint8_t * data
Definition: avcodec.h:1433
#define av_log(a,...)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
vorbis_comment vc
VorbisComment info.
Definition: libvorbisdec.c:31
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:178
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
const char * name
Name of the codec implementation.
Definition: avcodec.h:3489
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
Libavcodec external API header.
vorbis_info vi
vorbis_info used during init
Definition: libvorbisdec.c:28
static int oggvorbis_decode_frame(AVCodecContext *avccontext, void *data, int *got_frame_ptr, AVPacket *avpkt)
Definition: libvorbisdec.c:148
int sample_rate
samples per second
Definition: avcodec.h:2272
static int ogg_packet(AVFormatContext *s, int *sid, int *dstart, int *dsize, int64_t *fpos)
find the next Ogg packet
Definition: oggdec.c:461
main external API structure.
Definition: avcodec.h:1512
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:1048
static int oggvorbis_decode_close(AVCodecContext *avccontext)
Definition: libvorbisdec.c:198
void * buf
Definition: avisynth_c.h:553
int extradata_size
Definition: avcodec.h:1628
rational number numerator/denominator
Definition: rational.h:43
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:182
static int decode(AVCodecContext *avctx, void *data, int *got_sub, AVPacket *avpkt)
Definition: ccaption_dec.c:521
static int op(uint8_t **dst, const uint8_t *dst_end, GetByteContext *gb, int pixel, int count, int *x, int width, int linesize)
Perform decode operation.
Definition: anm.c:78
common internal api header.
signed 16 bits
Definition: samplefmt.h:62
AVCodec ff_libvorbis_decoder
Definition: libvorbisdec.c:208
void * priv_data
Definition: avcodec.h:1554
int channels
number of audio channels
Definition: avcodec.h:2273
This structure stores compressed data.
Definition: avcodec.h:1410
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:225