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