File: faad2.c

package info (click to toggle)
libquicktime 2%3A1.2.4-7
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 5,848 kB
  • sloc: ansic: 55,288; sh: 10,976; makefile: 456; sed: 16
file content (412 lines) | stat: -rw-r--r-- 11,834 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*******************************************************************************
 faad2.c

 libquicktime - A library for reading and writing quicktime/avi/mp4 files.
 http://libquicktime.sourceforge.net

 Copyright (C) 2002 Heroine Virtual Ltd.
 Copyright (C) 2002-2011 Members of the libquicktime project.

 This library is free software; you can redistribute it and/or modify it under
 the terms of the GNU Lesser General Public License as published by the Free
 Software Foundation; either version 2.1 of the License, or (at your option)
 any later version.

 This library is distributed in the hope that it will be useful, but WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 details.

 You should have received a copy of the GNU Lesser General Public License along
 with this library; if not, write to the Free Software Foundation, Inc., 51
 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*******************************************************************************/ 

#include "lqt_private.h"
#define LQT_LIBQUICKTIME
#include <quicktime/lqt_codecapi.h>

#include "faad2.h"

#include <string.h>
#include <stdlib.h>

#ifdef HAVE_NEAACDEC_H
#include <neaacdec.h>
/*
 *  Backwards compatibility names (currently in neaacdec.h,
 *  but might be removed in future versions)
 */
#ifndef faacDecHandle
/* structs */
#define faacDecHandle                  NeAACDecHandle
#define faacDecConfiguration           NeAACDecConfiguration
#define faacDecConfigurationPtr        NeAACDecConfigurationPtr
#define faacDecFrameInfo               NeAACDecFrameInfo
/* functions */
#define faacDecGetErrorMessage         NeAACDecGetErrorMessage
#define faacDecSetConfiguration        NeAACDecSetConfiguration
#define faacDecGetCurrentConfiguration NeAACDecGetCurrentConfiguration
#define faacDecInit                    NeAACDecInit
#define faacDecInit2                   NeAACDecInit2
#define faacDecInitDRM                 NeAACDecInitDRM
#define faacDecPostSeekReset           NeAACDecPostSeekReset
#define faacDecOpen                    NeAACDecOpen
#define faacDecClose                   NeAACDecClose
#define faacDecDecode                  NeAACDecDecode
#define AudioSpecificConfig            NeAACDecAudioSpecificConfig
#endif

#else
#include <faad.h>
#endif

#define LOG_DOMAIN "faad2"

typedef struct
  {
  faacDecHandle dec;

  /* Start and end positions of the sample buffer */
  
  int64_t sample_buffer_start;
  int64_t sample_buffer_end;
    
  uint8_t * data;
  int data_alloc;
  
  float * sample_buffer;
  int sample_buffer_alloc;

  int upsample;
  
  } quicktime_faad2_codec_t;

static int delete_codec(quicktime_codec_t *codec_base)
  {
  quicktime_faad2_codec_t *codec = codec_base->priv;

  if(codec->dec)
    faacDecClose(codec->dec);

  if(codec->sample_buffer)
    free(codec->sample_buffer);

  if(codec->data)
    free(codec->data);
      
  
  free(codec);
  return 0;
  }

/* Channel IDs */

static struct
  {
  int faad_ch;
  lqt_channel_t lqt_ch;
  }
channels[] = 
  {
    { FRONT_CHANNEL_CENTER, LQT_CHANNEL_FRONT_CENTER },
    { FRONT_CHANNEL_LEFT,   LQT_CHANNEL_FRONT_LEFT },
    { FRONT_CHANNEL_RIGHT,  LQT_CHANNEL_FRONT_RIGHT },
    { SIDE_CHANNEL_LEFT,    LQT_CHANNEL_SIDE_LEFT },
    { SIDE_CHANNEL_RIGHT,   LQT_CHANNEL_SIDE_RIGHT },
    { BACK_CHANNEL_LEFT,    LQT_CHANNEL_BACK_LEFT },
    { BACK_CHANNEL_RIGHT,   LQT_CHANNEL_BACK_RIGHT },
    { BACK_CHANNEL_CENTER,  LQT_CHANNEL_BACK_CENTER },
    { LFE_CHANNEL,          LQT_CHANNEL_LFE },
    { UNKNOWN_CHANNEL,      LQT_CHANNEL_UNKNOWN }
  };
  
static lqt_channel_t get_channel(int channel)
  {
  int i;
  for(i = 0; i < sizeof(channels)/sizeof(channels[0]); i++)
    {
    if(channels[i].faad_ch == channel)
      return channels[i].lqt_ch;
    }
  return LQT_CHANNEL_UNKNOWN;
  }
  
static int decode_chunk(quicktime_t *file, int track)
  {
  int i, j, num_packets, num_samples, packet_size;
  float * samples;
  faacDecFrameInfo frame_info;

  quicktime_audio_map_t *track_map = &file->atracks[track];
  
  quicktime_faad2_codec_t *codec =
    file->atracks[track].codec->priv;
  
  num_packets = lqt_audio_num_vbr_packets(file, track,
                                          track_map->cur_chunk,
                                          &num_samples);

  // fprintf(stderr, "Num packets %ld, %d\n", track_map->cur_chunk, num_packets);

  if(!num_packets)
    return 0;

  if(codec->upsample)
    num_samples *= 2;
  
  
  if(codec->sample_buffer_alloc <
     codec->sample_buffer_end - codec->sample_buffer_start + num_samples)
    {
    codec->sample_buffer_alloc =
      codec->sample_buffer_end - codec->sample_buffer_start +
      num_samples + 1024;

    codec->sample_buffer = realloc(codec->sample_buffer,
                                   codec->sample_buffer_alloc *
                                   track_map->channels * sizeof(float));
    }
  
  for(i = 0; i < num_packets; i++)
    {
    packet_size = lqt_audio_read_vbr_packet(file, track,
                                            track_map->cur_chunk, i,
                                            &codec->data,
                                            &codec->data_alloc, &num_samples);

    // fprintf(stderr, "Packet size %d, %d\n", i, packet_size);
    
    if(codec->upsample)
      num_samples *= 2;

    samples = faacDecDecode(codec->dec, &frame_info,
                            codec->data, packet_size);
    if(!samples)
      {
      lqt_log(file, LQT_LOG_ERROR, LOG_DOMAIN, "faacDecDecode failed %s",
              faacDecGetErrorMessage(frame_info.error));
      
      return 0;
      }

    /* Set up channel map */
    if(!track_map->channel_setup)
      {
      track_map->channel_setup = calloc(track_map->channels,
                                        sizeof(*(track_map->channel_setup)));
      for(i = 0; i < track_map->channels; i++)
        {
        track_map->channel_setup[i] =
          get_channel(frame_info.channel_position[i]);
        }

      }
    
      
    
    
    if((track_map->channels == 1) && (frame_info.channels == 2))
      {
            
      for(j = 0; j < frame_info.samples/2; j++)
        samples[j] = samples[2*j];
      frame_info.samples/=2;
      }
    
    memcpy(&codec->sample_buffer[(codec->sample_buffer_end -
                                   codec->sample_buffer_start)*track_map->channels],
           samples, frame_info.samples * sizeof(float));
    codec->sample_buffer_end += frame_info.samples / track_map->channels;
    }
  
  track_map->cur_chunk++;
  return 1;
  }

static int decode(quicktime_t *file, 
                  void * output,
                  long samples, 
                  int track) 
  {
  int64_t chunk_sample;
  int samples_copied = 0;
  int samples_to_skip;
  int samples_to_move;
  int samples_decoded;
  
  quicktime_audio_map_t *track_map = &file->atracks[track];
  quicktime_faad2_codec_t *codec = track_map->codec->priv;

  /* TODO Check whether seeking happened */

  if(!output)
    {
    /* HACK: Sometimes mp4 files don't set this correctly */
    lqt_init_vbr_audio(file, track); 

    /* Set channel setup */
    decode_chunk(file, track);
    return 0;
    }
  
  if(track_map->last_position != track_map->current_position)
    {
    /* Get the next chunk */
    
    if(codec->upsample)
      {
      lqt_chunk_of_sample_vbr(&chunk_sample,
                              &track_map->cur_chunk,
                              track_map->track,
                              track_map->current_position / 2);
      chunk_sample *= 2;
      }
    else
      lqt_chunk_of_sample_vbr(&chunk_sample,
                              &track_map->cur_chunk,
                              track_map->track,
                              track_map->current_position);
    
    if(track_map->cur_chunk >= track_map->track->mdia.minf.stbl.stco.total_entries - 1)
      {
      return 0;
      }
    
    codec->sample_buffer_start = chunk_sample;
    codec->sample_buffer_end   = chunk_sample;

    /* Decode frames until we have enough */

    while(codec->sample_buffer_end < track_map->current_position + samples)
      {
      if(!decode_chunk(file, track))
        break;
      }
    }
  
  /* Flush unneeded samples */
  
  if(track_map->current_position > codec->sample_buffer_start)
    {
    samples_to_skip = track_map->current_position - codec->sample_buffer_start;

    samples_to_move = codec->sample_buffer_end - track_map->current_position;

    if(samples_to_move > 0)
      {
      memmove(codec->sample_buffer,
              codec->sample_buffer + samples_to_skip * track_map->channels,
              samples_to_move * track_map->channels * sizeof(float));
      }
    codec->sample_buffer_start = track_map->current_position;
    if(samples_to_move > 0)
      codec->sample_buffer_end = codec->sample_buffer_start + samples_to_move;
    else
      codec->sample_buffer_end = codec->sample_buffer_start;
    }

  /* Decode new chunks until we have enough samples */

  while(codec->sample_buffer_end < codec->sample_buffer_start + samples)
    {
    if(!decode_chunk(file, track))
      break;
    }

  samples_decoded = codec->sample_buffer_end - codec->sample_buffer_start;

  samples_copied = (samples_decoded > samples) ? samples : samples_decoded;
  
  memcpy(output, codec->sample_buffer, samples_copied * track_map->channels * sizeof(float));

  track_map->last_position = track_map->current_position + samples_copied;
  
  return samples_copied;
  }

static int set_parameter(quicktime_t *file, 
		int track, 
		const char *key, 
		const void *value)
  {
  return 0;
  }

void quicktime_init_codec_faad2(quicktime_codec_t * codec_base,
                                quicktime_audio_map_t *atrack,
                                quicktime_video_map_t *vtrack)
  {
  uint8_t * extradata = (uint8_t *)0;
  int extradata_size = 0;
  quicktime_stsd_t * stsd;
  unsigned long samplerate = 0;
  unsigned char channels;

  faacDecConfigurationPtr cfg;
  
  quicktime_faad2_codec_t *codec;

  codec = calloc(1, sizeof(*codec));
  
  /* Init public items */
  codec_base->priv = codec;
  codec_base->delete_codec = delete_codec;
  codec_base->decode_audio = decode;
  codec_base->set_parameter = set_parameter;
  
  /* Ok, usually, we initialize decoders during the first
     decode() call. But in this case, we might need to
     set the correct samplerate, which should be known before */

  codec->dec = faacDecOpen();

  if(!atrack)
    return;
  
  atrack->sample_format = LQT_SAMPLE_FLOAT;
  stsd = &atrack->track->mdia.minf.stbl.stsd;
  
  if(stsd->table[0].has_esds)
    {
    extradata = stsd->table[0].esds.decoderConfig;
    extradata_size =
      stsd->table[0].esds.decoderConfigLen;
    }
  else if(stsd->table[0].has_wave &&
          stsd->table[0].wave.has_esds)
    {
    extradata = stsd->table[0].wave.esds.decoderConfig;
    extradata_size = stsd->table[0].wave.esds.decoderConfigLen;
    }
  else
    {
    lqt_log(NULL, LQT_LOG_ERROR, LOG_DOMAIN,
            "No extradata found, decoding is doomed to failure");
    }

  cfg = faacDecGetCurrentConfiguration(codec->dec);
  cfg->outputFormat = FAAD_FMT_FLOAT;
  
  faacDecSetConfiguration(codec->dec, cfg);
  
  faacDecInit2(codec->dec, extradata, extradata_size,
               &samplerate, &channels);

  atrack->ci.id = LQT_COMPRESSION_AAC;

  lqt_compression_info_set_header(&atrack->ci,
                                  extradata,
                                  extradata_size);
  
  if(atrack->samplerate != samplerate)
    {
    atrack->samplerate = samplerate;
    codec->upsample = 1;
    atrack->total_samples *= 2;
    atrack->ci.flags |= LQT_COMPRESSION_SBR;
    }
  stsd->table[0].channels = channels;
  atrack->channels = channels;
  
  }