~ubuntu-branches/ubuntu/edgy/gstreamer0.10-ffmpeg/edgy

« back to all changes in this revision

Viewing changes to gst-libs/ext/ffmpeg/libavcodec/faad.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastian Dröge
  • Date: 2006-04-01 16:13:43 UTC
  • mto: This revision was merged to the branch mainline in revision 5.
  • Revision ID: james.westby@ubuntu.com-20060401161343-n621cgjlujio0otg
Tags: upstream-0.10.1
Import upstream version 0.10.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
 *
16
16
 * You should have received a copy of the GNU Lesser General Public
17
17
 * License along with this library; if not, write to the Free Software
18
 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
19
 */
20
20
 
21
21
/**
47
47
#endif
48
48
 
49
49
typedef struct {
50
 
    void* handle;               /* dlopen handle */
51
 
    void* faac_handle;          /* FAAD library handle */
52
 
    int frame_size;
 
50
    void* handle;               /* dlopen handle */
 
51
    void* faac_handle;          /* FAAD library handle */
53
52
    int sample_size;
54
 
    int flags;
 
53
    int init;
55
54
 
56
55
    /* faad calls */
57
56
    faacDecHandle FAADAPI (*faacDecOpen)(void);
58
57
    faacDecConfigurationPtr FAADAPI (*faacDecGetCurrentConfiguration)(faacDecHandle hDecoder);
59
58
#ifndef FAAD2_VERSION
60
 
        int FAADAPI (*faacDecSetConfiguration)(faacDecHandle hDecoder,
 
59
        int FAADAPI (*faacDecSetConfiguration)(faacDecHandle hDecoder,
61
60
                                           faacDecConfigurationPtr config);
62
 
        int FAADAPI (*faacDecInit)(faacDecHandle hDecoder,
63
 
                                unsigned char *buffer,
64
 
                                unsigned long *samplerate,
65
 
                                unsigned long *channels);
66
 
        int FAADAPI (*faacDecInit2)(faacDecHandle hDecoder, unsigned char *pBuffer,
 
61
        int FAADAPI (*faacDecInit)(faacDecHandle hDecoder,
 
62
                                unsigned char *buffer,
 
63
                                unsigned long *samplerate,
 
64
                                unsigned long *channels);
 
65
        int FAADAPI (*faacDecInit2)(faacDecHandle hDecoder, unsigned char *pBuffer,
67
66
                                unsigned long SizeOfDecoderSpecificInfo,
68
67
                                unsigned long *samplerate, unsigned long *channels);
69
 
        int FAADAPI (*faacDecDecode)(faacDecHandle hDecoder,
70
 
                                unsigned char *buffer,
71
 
                                unsigned long *bytesconsumed,
72
 
                                short *sample_buffer,
 
68
        int FAADAPI (*faacDecDecode)(faacDecHandle hDecoder,
 
69
                                unsigned char *buffer,
 
70
                                unsigned long *bytesconsumed,
 
71
                                short *sample_buffer,
73
72
                                unsigned long *samples);
74
73
#else
75
 
        unsigned char FAADAPI (*faacDecSetConfiguration)(faacDecHandle hDecoder,
 
74
        unsigned char FAADAPI (*faacDecSetConfiguration)(faacDecHandle hDecoder,
76
75
                                                     faacDecConfigurationPtr config);
77
 
        long FAADAPI (*faacDecInit)(faacDecHandle hDecoder,
78
 
                                 unsigned char *buffer,
79
 
                                 unsigned long buffer_size,
80
 
                                 unsigned long *samplerate,
81
 
                                 unsigned char *channels);
82
 
        char FAADAPI (*faacDecInit2)(faacDecHandle hDecoder, unsigned char *pBuffer,
 
76
        long FAADAPI (*faacDecInit)(faacDecHandle hDecoder,
 
77
                                   unsigned char *buffer,
 
78
                                 unsigned long buffer_size,
 
79
                                 unsigned long *samplerate,
 
80
                                 unsigned char *channels);
 
81
        char FAADAPI (*faacDecInit2)(faacDecHandle hDecoder, unsigned char *pBuffer,
83
82
                                 unsigned long SizeOfDecoderSpecificInfo,
84
83
                                 unsigned long *samplerate, unsigned char *channels);
85
 
        void *FAADAPI (*faacDecDecode)(faacDecHandle hDecoder,
86
 
                                         faacDecFrameInfo *hInfo,
87
 
                                         unsigned char *buffer,
88
 
                                                                 unsigned long buffer_size);
89
 
        char* FAADAPI (*faacDecGetErrorMessage)(unsigned char errcode);
 
84
        void *FAADAPI (*faacDecDecode)(faacDecHandle hDecoder,
 
85
                                         faacDecFrameInfo *hInfo,
 
86
                                         unsigned char *buffer,
 
87
                                                                 unsigned long buffer_size);
 
88
        char* FAADAPI (*faacDecGetErrorMessage)(unsigned char errcode);
90
89
#endif
91
 
    
 
90
 
92
91
    void FAADAPI (*faacDecClose)(faacDecHandle hDecoder);
93
 
    
94
 
    
 
92
 
 
93
 
95
94
} FAACContext;
96
95
 
97
96
static const unsigned long faac_srates[] =
111
110
#endif
112
111
    int r = 0;
113
112
 
114
 
    if (avctx->extradata)
115
 
        r = s->faacDecInit2(s->faac_handle, (uint8_t*) avctx->extradata,
116
 
                            avctx->extradata_size,
117
 
                            &samplerate, &channels);
118
 
    // else r = s->faacDecInit(s->faac_handle ... );
119
 
 
120
 
    if (r < 0)
121
 
        av_log(avctx, AV_LOG_ERROR, "faacDecInit2 failed r:%d   sr:%ld  ch:%ld  s:%d\n",
122
 
                r, samplerate, (long)channels, avctx->extradata_size);
123
 
    avctx->sample_rate = samplerate;
124
 
    avctx->channels = channels;
 
113
    if (avctx->extradata){
 
114
        r = s->faacDecInit2(s->faac_handle, (uint8_t*) avctx->extradata,
 
115
                            avctx->extradata_size,
 
116
                            &samplerate, &channels);
 
117
        if (r < 0){
 
118
            av_log(avctx, AV_LOG_ERROR,
 
119
                   "faacDecInit2 failed r:%d   sr:%ld  ch:%ld  s:%d\n",
 
120
                   r, samplerate, (long)channels, avctx->extradata_size);
 
121
        } else {
 
122
            avctx->sample_rate = samplerate;
 
123
            avctx->channels = channels;
 
124
            s->init = 1;
 
125
        }
 
126
    }
125
127
 
126
128
    return r;
127
129
}
141
143
    void *out;
142
144
#endif
143
145
    if(buf_size == 0)
144
 
        return 0;
 
146
        return 0;
145
147
#ifndef FAAD2_VERSION
146
 
    out = s->faacDecDecode(s->faac_handle, 
147
 
                           (unsigned char*)buf, 
148
 
                           &bytesconsumed, 
149
 
                           data, 
 
148
    out = s->faacDecDecode(s->faac_handle,
 
149
                           (unsigned char*)buf,
 
150
                           &bytesconsumed,
 
151
                           data,
150
152
                           &samples);
151
153
    samples *= s->sample_size;
152
154
    if (data_size)
153
 
        *data_size = samples;
 
155
        *data_size = samples;
154
156
    return (buf_size < (int)bytesconsumed)
155
 
        ? buf_size : (int)bytesconsumed;
 
157
        ? buf_size : (int)bytesconsumed;
156
158
#else
157
 
        
 
159
 
 
160
    if(!s->init){
 
161
        unsigned long srate;
 
162
        unsigned char channels;
 
163
        int r = faacDecInit(s->faac_handle, buf, buf_size, &srate, &channels);
 
164
        if(r < 0){
 
165
            av_log(avctx, AV_LOG_ERROR, "faac: codec init failed: %s\n",
 
166
                   s->faacDecGetErrorMessage(frame_info.error));
 
167
            return 0;
 
168
        }
 
169
        avctx->sample_rate = srate;
 
170
        avctx->channels = channels;
 
171
        s->init = 1;
 
172
    }
 
173
 
158
174
    out = s->faacDecDecode(s->faac_handle, &frame_info, (unsigned char*)buf, (unsigned long)buf_size);
159
175
 
160
176
    if (frame_info.error > 0) {
161
 
        av_log(avctx, AV_LOG_ERROR, "faac: frame decoding failed: %s\n",
162
 
                s->faacDecGetErrorMessage(frame_info.error));
 
177
        av_log(avctx, AV_LOG_ERROR, "faac: frame decoding failed: %s\n",
 
178
                s->faacDecGetErrorMessage(frame_info.error));
163
179
        return 0;
164
180
    }
165
181
 
167
183
    memcpy(data, out, frame_info.samples); // CHECKME - can we cheat this one
168
184
 
169
185
    if (data_size)
170
 
        *data_size = frame_info.samples;
 
186
        *data_size = frame_info.samples;
171
187
 
172
188
    return (buf_size < (int)frame_info.bytesconsumed)
173
 
        ? buf_size : (int)frame_info.bytesconsumed;
 
189
        ? buf_size : (int)frame_info.bytesconsumed;
174
190
#endif
175
191
}
176
192
 
196
212
    s->handle = dlopen(libfaadname, RTLD_LAZY);
197
213
    if (!s->handle)
198
214
    {
199
 
        av_log(avctx, AV_LOG_ERROR, "FAAD library: %s could not be opened! \n%s\n",
200
 
                libfaadname, dlerror());
 
215
        av_log(avctx, AV_LOG_ERROR, "FAAD library: %s could not be opened! \n%s\n",
 
216
                libfaadname, dlerror());
201
217
        return -1;
202
218
    }
203
219
#define dfaac(a, b) \
209
225
#endif /* CONFIG_FAADBIN */
210
226
 
211
227
        // resolve all needed function calls
212
 
        dfaac(Open, (faacDecHandle FAADAPI (*)(void)));
213
 
        dfaac(GetCurrentConfiguration, (faacDecConfigurationPtr
214
 
                                        FAADAPI (*)(faacDecHandle)));
 
228
        dfaac(Open, (faacDecHandle FAADAPI (*)(void)));
 
229
        dfaac(GetCurrentConfiguration, (faacDecConfigurationPtr
 
230
                                        FAADAPI (*)(faacDecHandle)));
215
231
#ifndef FAAD2_VERSION
216
 
        dfaac(SetConfiguration, (int FAADAPI (*)(faacDecHandle,
217
 
                                                           faacDecConfigurationPtr)));
 
232
        dfaac(SetConfiguration, (int FAADAPI (*)(faacDecHandle,
 
233
                                                           faacDecConfigurationPtr)));
218
234
 
219
 
        dfaac(Init, (int FAADAPI (*)(faacDecHandle, unsigned char*,
220
 
                                     unsigned long*, unsigned long*)));
 
235
        dfaac(Init, (int FAADAPI (*)(faacDecHandle, unsigned char*,
 
236
                                     unsigned long*, unsigned long*)));
221
237
    dfaac(Init2, (int FAADAPI (*)(faacDecHandle, unsigned char*,
222
 
                                       unsigned long, unsigned long*,
223
 
                                       unsigned long*)));
 
238
                                       unsigned long, unsigned long*,
 
239
                                       unsigned long*)));
224
240
    dfaac(Close, (void FAADAPI (*)(faacDecHandle hDecoder)));
225
 
        dfaac(Decode, (int FAADAPI (*)(faacDecHandle, unsigned char*,
226
 
                             unsigned long*, short*, unsigned long*)));
 
241
        dfaac(Decode, (int FAADAPI (*)(faacDecHandle, unsigned char*,
 
242
                             unsigned long*, short*, unsigned long*)));
227
243
#else
228
 
        dfaac(SetConfiguration, (unsigned char FAADAPI (*)(faacDecHandle,
229
 
                                                           faacDecConfigurationPtr)));
230
 
        dfaac(Init, (long FAADAPI (*)(faacDecHandle, unsigned char*,
231
 
                                     unsigned long, unsigned long*, unsigned char*)));
232
 
        dfaac(Init2, (char FAADAPI (*)(faacDecHandle, unsigned char*,
233
 
                                       unsigned long, unsigned long*,
234
 
                                       unsigned char*)));
235
 
        dfaac(Decode, (void *FAADAPI (*)(faacDecHandle, faacDecFrameInfo*,
236
 
                             unsigned char*, unsigned long)));
237
 
        dfaac(GetErrorMessage, (char* FAADAPI (*)(unsigned char)));
 
244
        dfaac(SetConfiguration, (unsigned char FAADAPI (*)(faacDecHandle,
 
245
                                                           faacDecConfigurationPtr)));
 
246
        dfaac(Init, (long FAADAPI (*)(faacDecHandle, unsigned char*,
 
247
                                     unsigned long, unsigned long*, unsigned char*)));
 
248
        dfaac(Init2, (char FAADAPI (*)(faacDecHandle, unsigned char*,
 
249
                                       unsigned long, unsigned long*,
 
250
                                       unsigned char*)));
 
251
        dfaac(Decode, (void *FAADAPI (*)(faacDecHandle, faacDecFrameInfo*,
 
252
                             unsigned char*, unsigned long)));
 
253
        dfaac(GetErrorMessage, (char* FAADAPI (*)(unsigned char)));
238
254
#endif
239
255
#undef dfacc
240
256
 
243
259
    }
244
260
    if (err) {
245
261
        dlclose(s->handle);
246
 
        av_log(avctx, AV_LOG_ERROR, "FAAD library: cannot resolve %s in %s!\n",
247
 
                err, libfaadname);
 
262
        av_log(avctx, AV_LOG_ERROR, "FAAD library: cannot resolve %s in %s!\n",
 
263
                err, libfaadname);
248
264
        return -1;
249
265
    }
250
266
#endif
260
276
    faac_cfg = s->faacDecGetCurrentConfiguration(s->faac_handle);
261
277
 
262
278
    if (faac_cfg) {
263
 
        switch (avctx->bits_per_sample) {
264
 
        case 8: av_log(avctx, AV_LOG_ERROR, "FAADlib unsupported bps %d\n", avctx->bits_per_sample); break;
265
 
        default:
266
 
        case 16:
267
 
#ifdef FAAD2_VERSION
268
 
            faac_cfg->outputFormat = FAAD_FMT_16BIT;
269
 
#endif
270
 
            s->sample_size = 2;
271
 
            break;
272
 
        case 24:
273
 
#ifdef FAAD2_VERSION
274
 
            faac_cfg->outputFormat = FAAD_FMT_24BIT;
275
 
#endif
276
 
            s->sample_size = 3;
277
 
            break;
278
 
        case 32:
279
 
#ifdef FAAD2_VERSION
280
 
            faac_cfg->outputFormat = FAAD_FMT_32BIT;
281
 
#endif
282
 
            s->sample_size = 4;
283
 
            break;
284
 
        }
 
279
        switch (avctx->bits_per_sample) {
 
280
        case 8: av_log(avctx, AV_LOG_ERROR, "FAADlib unsupported bps %d\n", avctx->bits_per_sample); break;
 
281
        default:
 
282
        case 16:
 
283
#ifdef FAAD2_VERSION
 
284
            faac_cfg->outputFormat = FAAD_FMT_16BIT;
 
285
#endif
 
286
            s->sample_size = 2;
 
287
            break;
 
288
        case 24:
 
289
#ifdef FAAD2_VERSION
 
290
            faac_cfg->outputFormat = FAAD_FMT_24BIT;
 
291
#endif
 
292
            s->sample_size = 3;
 
293
            break;
 
294
        case 32:
 
295
#ifdef FAAD2_VERSION
 
296
            faac_cfg->outputFormat = FAAD_FMT_32BIT;
 
297
#endif
 
298
            s->sample_size = 4;
 
299
            break;
 
300
        }
285
301
 
286
 
        faac_cfg->defSampleRate = (!avctx->sample_rate) ? 44100 : avctx->sample_rate;
287
 
        faac_cfg->defObjectType = LC;
 
302
        faac_cfg->defSampleRate = (!avctx->sample_rate) ? 44100 : avctx->sample_rate;
 
303
        faac_cfg->defObjectType = LC;
288
304
    }
289
305
 
290
306
    s->faacDecSetConfiguration(s->faac_handle, faac_cfg);