FFmpeg
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 "codec_internal.h"
26 #include "decode.h"
27 
28 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_close(AVCodecContext *avccontext);
37 
39 {
40  OggVorbisDecContext *context = avccontext->priv_data ;
41  uint8_t *p= avccontext->extradata;
42  int i, hsizes[3], ret;
43  unsigned char *headers[3], *extradata = avccontext->extradata;
44 
45  if(! avccontext->extradata_size || ! p) {
46  av_log(avccontext, AV_LOG_ERROR, "vorbis extradata absent\n");
47  return AVERROR(EINVAL);
48  }
49 
50  vorbis_info_init(&context->vi) ;
51  vorbis_comment_init(&context->vc) ;
52 
53  if(p[0] == 0 && p[1] == 30) {
54  int sizesum = 0;
55  for(i = 0; i < 3; i++){
56  hsizes[i] = bytestream_get_be16((const uint8_t **)&p);
57  sizesum += 2 + hsizes[i];
58  if (sizesum > avccontext->extradata_size) {
59  av_log(avccontext, AV_LOG_ERROR, "vorbis extradata too small\n");
61  goto error;
62  }
63 
64  headers[i] = p;
65  p += hsizes[i];
66  }
67  } else if(*p == 2) {
68  unsigned int offset = 1;
69  unsigned int sizesum = 1;
70  p++;
71  for(i=0; i<2; i++) {
72  hsizes[i] = 0;
73  while((*p == 0xFF) && (sizesum < avccontext->extradata_size)) {
74  hsizes[i] += 0xFF;
75  offset++;
76  sizesum += 1 + 0xFF;
77  p++;
78  }
79  hsizes[i] += *p;
80  offset++;
81  sizesum += 1 + *p;
82  if(sizesum > avccontext->extradata_size) {
83  av_log(avccontext, AV_LOG_ERROR,
84  "vorbis header sizes damaged\n");
86  goto error;
87  }
88  p++;
89  }
90  hsizes[2] = avccontext->extradata_size - hsizes[0]-hsizes[1]-offset;
91 #if 0
92  av_log(avccontext, AV_LOG_DEBUG,
93  "vorbis header sizes: %d, %d, %d, / extradata_len is %d \n",
94  hsizes[0], hsizes[1], hsizes[2], avccontext->extradata_size);
95 #endif
96  headers[0] = extradata + offset;
97  headers[1] = extradata + offset + hsizes[0];
98  headers[2] = extradata + offset + hsizes[0] + hsizes[1];
99  } else {
100  av_log(avccontext, AV_LOG_ERROR,
101  "vorbis initial header len is wrong: %d\n", *p);
103  goto error;
104  }
105 
106  for(i=0; i<3; i++){
107  context->op.b_o_s= i==0;
108  context->op.bytes = hsizes[i];
109  context->op.packet = headers[i];
110  if(vorbis_synthesis_headerin(&context->vi, &context->vc, &context->op)<0){
111  av_log(avccontext, AV_LOG_ERROR, "%d. vorbis header damaged\n", i+1);
113  goto error;
114  }
115  }
116 
117  if (context->vi.rate <= 0 || context->vi.rate > INT_MAX) {
118  av_log(avccontext, AV_LOG_ERROR, "vorbis rate is invalid\n");
120  goto error;
121  }
122 
123  av_channel_layout_uninit(&avccontext->ch_layout);
125  avccontext->ch_layout.nb_channels = context->vi.channels;
126  avccontext->sample_rate = context->vi.rate;
127  avccontext->sample_fmt = AV_SAMPLE_FMT_S16;
128  avccontext->time_base= (AVRational){1, avccontext->sample_rate};
129 
130  vorbis_synthesis_init(&context->vd, &context->vi);
131  vorbis_block_init(&context->vd, &context->vb);
132 
133  return 0 ;
134 
135  error:
136  oggvorbis_decode_close(avccontext);
137  return ret;
138 }
139 
140 
141 static inline int conv(int samples, float **pcm, char *buf, int channels) {
142  int i, j;
143  ogg_int16_t *ptr, *data = (ogg_int16_t*)buf ;
144  float *mono ;
145 
146  for(i = 0 ; i < channels ; i++){
147  ptr = &data[i];
148  mono = pcm[i] ;
149 
150  for(j = 0 ; j < samples ; j++) {
151  *ptr = av_clip_int16(mono[j] * 32767.f);
152  ptr += channels;
153  }
154  }
155 
156  return 0 ;
157 }
158 
160  int *got_frame_ptr, AVPacket *avpkt)
161 {
162  OggVorbisDecContext *context = avccontext->priv_data ;
163  float **pcm ;
164  ogg_packet *op= &context->op;
165  int samples, total_samples, total_bytes;
166  int ret;
167  int16_t *output;
168 
169  if(!avpkt->size){
170  //FIXME flush
171  return 0;
172  }
173 
174  frame->nb_samples = 8192*4;
175  if ((ret = ff_get_buffer(avccontext, frame, 0)) < 0)
176  return ret;
177  output = (int16_t *)frame->data[0];
178 
179 
180  op->packet = avpkt->data;
181  op->bytes = avpkt->size;
182 
183 // 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);
184 
185 /* for(i=0; i<op->bytes; i++)
186  av_log(avccontext, AV_LOG_DEBUG, "%02X ", op->packet[i]);
187  av_log(avccontext, AV_LOG_DEBUG, "\n");*/
188 
189  if(vorbis_synthesis(&context->vb, op) == 0)
190  vorbis_synthesis_blockin(&context->vd, &context->vb) ;
191 
192  total_samples = 0 ;
193  total_bytes = 0 ;
194 
195  while((samples = vorbis_synthesis_pcmout(&context->vd, &pcm)) > 0) {
196  conv(samples, pcm, (char*)output + total_bytes, context->vi.channels) ;
197  total_bytes += samples * 2 * context->vi.channels ;
198  total_samples += samples ;
199  vorbis_synthesis_read(&context->vd, samples) ;
200  }
201 
202  frame->nb_samples = total_samples;
203  *got_frame_ptr = total_samples > 0;
204  return avpkt->size;
205 }
206 
207 
209 {
210  OggVorbisDecContext *context = avccontext->priv_data ;
211 
212  vorbis_block_clear(&context->vb);
213  vorbis_dsp_clear(&context->vd);
214  vorbis_info_clear(&context->vi) ;
215  vorbis_comment_clear(&context->vc) ;
216 
217  return 0 ;
218 }
219 
220 
222  .p.name = "libvorbis",
223  CODEC_LONG_NAME("libvorbis"),
224  .p.type = AVMEDIA_TYPE_AUDIO,
225  .p.id = AV_CODEC_ID_VORBIS,
226  .p.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_CHANNEL_CONF,
227  .caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE,
228  .priv_data_size = sizeof(OggVorbisDecContext),
231  .close = oggvorbis_decode_close,
232 };
error
static void error(const char *err)
Definition: target_bsf_fuzzer.c:32
OggVorbisDecContext::vd
vorbis_dsp_state vd
DSP state used for analysis
Definition: libvorbisdec.c:30
AVERROR
Filter the word β€œframe” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:1024
oggvorbis_decode_close
static int oggvorbis_decode_close(AVCodecContext *avccontext)
Definition: libvorbisdec.c:208
output
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce output
Definition: filter_design.txt:226
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:427
AVPacket::data
uint8_t * data
Definition: packet.h:558
data
const char data[16]
Definition: mxf.c:149
FF_CODEC_CAP_NOT_INIT_THREADSAFE
#define FF_CODEC_CAP_NOT_INIT_THREADSAFE
The codec is not known to be init-threadsafe (i.e.
Definition: codec_internal.h:34
FFCodec
Definition: codec_internal.h:127
AVChannelLayout::order
enum AVChannelOrder order
Channel order used in this layout.
Definition: channel_layout.h:324
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:329
OggVorbisDecContext::vi
vorbis_info vi
vorbis_info used during init
Definition: libvorbisdec.c:29
OggVorbisDecContext::vb
vorbis_block vb
vorbis_block used for analysis
Definition: libvorbisdec.c:31
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:1039
ogg_packet
static int ogg_packet(AVFormatContext *s, int *sid, int *dstart, int *dsize, int64_t *fpos)
find the next Ogg packet
Definition: oggdec.c:497
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:210
av_cold
#define av_cold
Definition: attributes.h:90
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:515
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:346
OggVorbisDecContext::op
ogg_packet op
ogg packet
Definition: libvorbisdec.c:33
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:201
op
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:76
AV_CHANNEL_ORDER_UNSPEC
@ AV_CHANNEL_ORDER_UNSPEC
Only the channel count is specified, without any further information about the channel order.
Definition: channel_layout.h:119
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:231
channels
channels
Definition: aptx.h:31
decode.h
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:331
conv
static int conv(int samples, float **pcm, char *buf, int channels)
Definition: libvorbisdec.c:141
context
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default minimum maximum flags name is the option keep it simple and lowercase description are in without and describe what they for example set the foo of the bar offset is the offset of the field in your context
Definition: writing_filters.txt:91
av_clip_int16
#define av_clip_int16
Definition: common.h:115
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
oggvorbis_decode_frame
static int oggvorbis_decode_frame(AVCodecContext *avccontext, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Definition: libvorbisdec.c:159
AV_CODEC_CAP_CHANNEL_CONF
#define AV_CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
Definition: codec.h:91
AVCodecContext::time_base
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition: avcodec.h:535
f
f
Definition: af_crystalizer.c:122
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1720
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:368
AVPacket::size
int size
Definition: packet.h:559
ff_libvorbis_decoder
const FFCodec ff_libvorbis_decoder
Definition: libvorbisdec.c:221
codec_internal.h
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1031
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
OggVorbisDecContext::vc
vorbis_comment vc
VorbisComment info
Definition: libvorbisdec.c:32
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
AVCodecContext::extradata
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
Definition: avcodec.h:514
AV_SAMPLE_FMT_S16
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:58
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:179
avcodec.h
ret
ret
Definition: filter_design.txt:187
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:265
AVCodecContext
main external API structure.
Definition: avcodec.h:431
av_channel_layout_uninit
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
Definition: channel_layout.c:442
AV_CODEC_CAP_DELAY
#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: codec.h:76
samples
Filter the word β€œframe” indicates either a video frame or a group of audio samples
Definition: filter_design.txt:8
OggVorbisDecContext
Definition: libvorbisdec.c:28
headers
FFmpeg currently uses a custom build this text attempts to document some of its obscure features and options Makefile the full command issued by make and its output will be shown on the screen DBG Preprocess x86 external assembler files to a dbg asm file in the object which then gets compiled Helps in developing those assembler files DESTDIR Destination directory for the install useful to prepare packages or install FFmpeg in cross environments GEN Set to β€˜1’ to generate the missing or mismatched references Makefile builds all the libraries and the executables fate Run the fate test note that you must have installed it fate list List all fate regression test targets fate list failing List the fate tests that failed the last time they were executed fate clear reports Remove the test reports from previous test libraries and programs examples Build all examples located in doc examples checkheaders Check headers dependencies alltools Build all tools in tools directory config Reconfigure the project with the current configuration tools target_dec_< decoder > _fuzzer Build fuzzer to fuzz the specified decoder tools target_bsf_< filter > _fuzzer Build fuzzer to fuzz the specified bitstream filter Useful standard make this is useful to reduce unneeded rebuilding when changing headers
Definition: build_system.txt:64
AVPacket
This structure stores compressed data.
Definition: packet.h:535
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:458
oggvorbis_decode_init
static av_cold int oggvorbis_decode_init(AVCodecContext *avccontext)
Definition: libvorbisdec.c:38
bytestream.h
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
AV_CODEC_ID_VORBIS
@ AV_CODEC_ID_VORBIS
Definition: codec_id.h:456