Initial revision 6759
[qball-mpd.git] / src / inputPlugins / .svn / text-base / mp4_plugin.c.svn-base
blob11d634ff2c5e17b66acda40a43791e8a15910c3d
1 /* the Music Player Daemon (MPD)
2  * Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
3  * This project's homepage is: http://www.musicpd.org
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
19 #include "../inputPlugin.h"
21 #ifdef HAVE_FAAD
23 #include "../utils.h"
24 #include "../audio.h"
25 #include "../log.h"
26 #include "../pcm_utils.h"
27 #include "../inputStream.h"
28 #include "../outputBuffer.h"
29 #include "../decode.h"
31 #include "../mp4ff/mp4ff.h"
33 #include <stdio.h>
34 #include <unistd.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <faad.h>
39 /* all code here is either based on or copied from FAAD2's frontend code */
41 static int mp4_getAACTrack(mp4ff_t * infile)
43         /* find AAC track */
44         int i, rc;
45         int numTracks = mp4ff_total_tracks(infile);
47         for (i = 0; i < numTracks; i++) {
48                 unsigned char *buff = NULL;
49                 unsigned int buff_size = 0;
50 #ifdef HAVE_MP4AUDIOSPECIFICCONFIG
51                 mp4AudioSpecificConfig mp4ASC;
52 #else
53                 unsigned long dummy1_32;
54                 unsigned char dummy2_8, dummy3_8, dummy4_8, dummy5_8, dummy6_8,
55                     dummy7_8, dummy8_8;
56 #endif
58                 mp4ff_get_decoder_config(infile, i, &buff, &buff_size);
60                 if (buff) {
61 #ifdef HAVE_MP4AUDIOSPECIFICCONFIG
62                         rc = AudioSpecificConfig(buff, buff_size, &mp4ASC);
63 #else
64                         rc = AudioSpecificConfig(buff, &dummy1_32, &dummy2_8,
65                                                  &dummy3_8, &dummy4_8,
66                                                  &dummy5_8, &dummy6_8,
67                                                  &dummy7_8, &dummy8_8);
68 #endif
69                         free(buff);
70                         if (rc < 0)
71                                 continue;
72                         return i;
73                 }
74         }
76         /* can't decode this */
77         return -1;
80 static uint32_t mp4_inputStreamReadCallback(void *inStream, void *buffer,
81                                             uint32_t length)
83         return readFromInputStream((InputStream *) inStream, buffer, 1, length);
86 static uint32_t mp4_inputStreamSeekCallback(void *inStream, uint64_t position)
88         return seekInputStream((InputStream *) inStream, position, SEEK_SET);
91 static int mp4_decode(OutputBuffer * cb, DecoderControl * dc,
92                       InputStream * inStream)
94         mp4ff_t *mp4fh;
95         mp4ff_callback_t *mp4cb;
96         int32_t track;
97         float time;
98         int32_t scale;
99         faacDecHandle decoder;
100         faacDecFrameInfo frameInfo;
101         faacDecConfigurationPtr config;
102         unsigned char *mp4Buffer;
103         unsigned int mp4BufferSize;
104         unsigned long sampleRate;
105         unsigned char channels;
106         long sampleId;
107         long numSamples;
108         int eof = 0;
109         long dur;
110         unsigned int sampleCount;
111         char *sampleBuffer;
112         size_t sampleBufferLen;
113         unsigned int initial = 1;
114         float *seekTable;
115         long seekTableEnd = -1;
116         int seekPositionFound = 0;
117         long offset;
118         mpd_uint16 bitRate = 0;
119         int seeking = 0;
121         mp4cb = xmalloc(sizeof(mp4ff_callback_t));
122         mp4cb->read = mp4_inputStreamReadCallback;
123         mp4cb->seek = mp4_inputStreamSeekCallback;
124         mp4cb->user_data = inStream;
126         mp4fh = mp4ff_open_read(mp4cb);
127         if (!mp4fh) {
128                 ERROR("Input does not appear to be a mp4 stream.\n");
129                 free(mp4cb);
130                 closeInputStream(inStream);
131                 return -1;
132         }
134         track = mp4_getAACTrack(mp4fh);
135         if (track < 0) {
136                 ERROR("No AAC track found in mp4 stream.\n");
137                 mp4ff_close(mp4fh);
138                 closeInputStream(inStream);
139                 free(mp4cb);
140                 return -1;
141         }
143         decoder = faacDecOpen();
145         config = faacDecGetCurrentConfiguration(decoder);
146         config->outputFormat = FAAD_FMT_16BIT;
147 #ifdef HAVE_FAACDECCONFIGURATION_DOWNMATRIX
148         config->downMatrix = 1;
149 #endif
150 #ifdef HAVE_FAACDECCONFIGURATION_DONTUPSAMPLEIMPLICITSBR
151         config->dontUpSampleImplicitSBR = 0;
152 #endif
153         faacDecSetConfiguration(decoder, config);
155         dc->audioFormat.bits = 16;
157         mp4Buffer = NULL;
158         mp4BufferSize = 0;
159         mp4ff_get_decoder_config(mp4fh, track, &mp4Buffer, &mp4BufferSize);
161         if (faacDecInit2
162             (decoder, mp4Buffer, mp4BufferSize, &sampleRate, &channels) < 0) {
163                 ERROR("Error not a AAC stream.\n");
164                 faacDecClose(decoder);
165                 mp4ff_close(mp4fh);
166                 free(mp4cb);
167                 closeInputStream(inStream);
168                 return -1;
169         }
171         dc->audioFormat.sampleRate = sampleRate;
172         dc->audioFormat.channels = channels;
173         time = mp4ff_get_track_duration_use_offsets(mp4fh, track);
174         scale = mp4ff_time_scale(mp4fh, track);
176         if (mp4Buffer)
177                 free(mp4Buffer);
179         if (scale < 0) {
180                 ERROR("Error getting audio format of mp4 AAC track.\n");
181                 faacDecClose(decoder);
182                 mp4ff_close(mp4fh);
183                 closeInputStream(inStream);
184                 free(mp4cb);
185                 return -1;
186         }
187         dc->totalTime = ((float)time) / scale;
189         numSamples = mp4ff_num_samples(mp4fh, track);
191         time = 0.0;
193         seekTable = xmalloc(sizeof(float) * numSamples);
195         for (sampleId = 0; sampleId < numSamples && !eof; sampleId++) {
196                 if (dc->seek)
197                         seeking = 1;
199                 if (seeking && seekTableEnd > 1 &&
200                     seekTable[seekTableEnd] >= dc->seekWhere) {
201                         int i = 2;
202                         while (seekTable[i] < dc->seekWhere)
203                                 i++;
204                         sampleId = i - 1;
205                         time = seekTable[sampleId];
206                 }
208                 dur = mp4ff_get_sample_duration(mp4fh, track, sampleId);
209                 offset = mp4ff_get_sample_offset(mp4fh, track, sampleId);
211                 if (sampleId > seekTableEnd) {
212                         seekTable[sampleId] = time;
213                         seekTableEnd = sampleId;
214                 }
216                 if (sampleId == 0)
217                         dur = 0;
218                 if (offset > dur)
219                         dur = 0;
220                 else
221                         dur -= offset;
222                 time += ((float)dur) / scale;
224                 if (seeking && time > dc->seekWhere)
225                         seekPositionFound = 1;
227                 if (seeking && seekPositionFound) {
228                         seekPositionFound = 0;
229                         clearOutputBuffer(cb);
230                         seeking = 0;
231                         dc->seek = 0;
232                 }
234                 if (seeking)
235                         continue;
237                 if (mp4ff_read_sample(mp4fh, track, sampleId, &mp4Buffer,
238                                       &mp4BufferSize) == 0) {
239                         eof = 1;
240                         continue;
241                 }
242 #ifdef HAVE_FAAD_BUFLEN_FUNCS
243                 sampleBuffer = faacDecDecode(decoder, &frameInfo, mp4Buffer,
244                                              mp4BufferSize);
245 #else
246                 sampleBuffer = faacDecDecode(decoder, &frameInfo, mp4Buffer);
247 #endif
249                 if (mp4Buffer)
250                         free(mp4Buffer);
251                 if (frameInfo.error > 0) {
252                         ERROR("faad2 error: %s\n",
253                               faacDecGetErrorMessage(frameInfo.error));
254                         eof = 1;
255                         break;
256                 }
258                 if (dc->state != DECODE_STATE_DECODE) {
259                         channels = frameInfo.channels;
260 #ifdef HAVE_FAACDECFRAMEINFO_SAMPLERATE
261                         scale = frameInfo.samplerate;
262 #endif
263                         dc->audioFormat.sampleRate = scale;
264                         dc->audioFormat.channels = frameInfo.channels;
265                         getOutputAudioFormat(&(dc->audioFormat),
266                                              &(cb->audioFormat));
267                         dc->state = DECODE_STATE_DECODE;
268                 }
270                 if (channels * (dur + offset) > frameInfo.samples) {
271                         dur = frameInfo.samples / channels;
272                         offset = 0;
273                 }
275                 sampleCount = (unsigned long)(dur * channels);
277                 if (sampleCount > 0) {
278                         initial = 0;
279                         bitRate = frameInfo.bytesconsumed * 8.0 *
280                             frameInfo.channels * scale /
281                             frameInfo.samples / 1000 + 0.5;
282                 }
284                 sampleBufferLen = sampleCount * 2;
286                 sampleBuffer += offset * channels * 2;
288                 sendDataToOutputBuffer(cb, NULL, dc, 1, sampleBuffer,
289                                        sampleBufferLen, time, bitRate, NULL);
290                 if (dc->stop) {
291                         eof = 1;
292                         break;
293                 }
294         }
296         free(seekTable);
297         faacDecClose(decoder);
298         mp4ff_close(mp4fh);
299         closeInputStream(inStream);
300         free(mp4cb);
302         if (dc->state != DECODE_STATE_DECODE)
303                 return -1;
305         if (dc->seek && seeking) {
306                 clearOutputBuffer(cb);
307                 dc->seek = 0;
308         }
309         flushOutputBuffer(cb);
311         if (dc->stop) {
312                 dc->state = DECODE_STATE_STOP;
313                 dc->stop = 0;
314         } else
315                 dc->state = DECODE_STATE_STOP;
317         return 0;
320 static MpdTag *mp4DataDup(char *file, int *mp4MetadataFound)
322         MpdTag *ret = NULL;
323         InputStream inStream;
324         mp4ff_t *mp4fh;
325         mp4ff_callback_t *cb;
326         int32_t track;
327         int32_t time;
328         int32_t scale;
329         int i;
331         *mp4MetadataFound = 0;
333         if (openInputStream(&inStream, file) < 0) {
334                 DEBUG("mp4DataDup: Failed to open file: %s\n", file);
335                 return NULL;
336         }
338         cb = xmalloc(sizeof(mp4ff_callback_t));
339         cb->read = mp4_inputStreamReadCallback;
340         cb->seek = mp4_inputStreamSeekCallback;
341         cb->user_data = &inStream;
343         mp4fh = mp4ff_open_read(cb);
344         if (!mp4fh) {
345                 free(cb);
346                 closeInputStream(&inStream);
347                 return NULL;
348         }
350         track = mp4_getAACTrack(mp4fh);
351         if (track < 0) {
352                 mp4ff_close(mp4fh);
353                 closeInputStream(&inStream);
354                 free(cb);
355                 return NULL;
356         }
358         ret = newMpdTag();
359         time = mp4ff_get_track_duration_use_offsets(mp4fh, track);
360         scale = mp4ff_time_scale(mp4fh, track);
361         if (scale < 0) {
362                 mp4ff_close(mp4fh);
363                 closeInputStream(&inStream);
364                 free(cb);
365                 freeMpdTag(ret);
366                 return NULL;
367         }
368         ret->time = ((float)time) / scale + 0.5;
370         for (i = 0; i < mp4ff_meta_get_num_items(mp4fh); i++) {
371                 char *item;
372                 char *value;
374                 mp4ff_meta_get_by_index(mp4fh, i, &item, &value);
376                 if (0 == strcasecmp("artist", item)) {
377                         addItemToMpdTag(ret, TAG_ITEM_ARTIST, value);
378                         *mp4MetadataFound = 1;
379                 } else if (0 == strcasecmp("title", item)) {
380                         addItemToMpdTag(ret, TAG_ITEM_TITLE, value);
381                         *mp4MetadataFound = 1;
382                 } else if (0 == strcasecmp("album", item)) {
383                         addItemToMpdTag(ret, TAG_ITEM_ALBUM, value);
384                         *mp4MetadataFound = 1;
385                 } else if (0 == strcasecmp("track", item)) {
386                         addItemToMpdTag(ret, TAG_ITEM_TRACK, value);
387                         *mp4MetadataFound = 1;
388                 } else if (0 == strcasecmp("disc", item)) {     /* Is that the correct id? */
389                         addItemToMpdTag(ret, TAG_ITEM_DISC, value);
390                         *mp4MetadataFound = 1;
391                 } else if (0 == strcasecmp("genre", item)) {
392                         addItemToMpdTag(ret, TAG_ITEM_GENRE, value);
393                         *mp4MetadataFound = 1;
394                 } else if (0 == strcasecmp("date", item)) {
395                         addItemToMpdTag(ret, TAG_ITEM_DATE, value);
396                         *mp4MetadataFound = 1;
397                 }
399                 free(item);
400                 free(value);
401         }
403         mp4ff_close(mp4fh);
404         closeInputStream(&inStream);
405         free(cb);
407         return ret;
410 static MpdTag *mp4TagDup(char *file)
412         MpdTag *ret = NULL;
413         int mp4MetadataFound = 0;
415         ret = mp4DataDup(file, &mp4MetadataFound);
416         if (!ret)
417                 return NULL;
418         if (!mp4MetadataFound) {
419                 MpdTag *temp = id3Dup(file);
420                 if (temp) {
421                         temp->time = ret->time;
422                         freeMpdTag(ret);
423                         ret = temp;
424                 }
425         }
427         return ret;
430 static char *mp4_suffixes[] = { "m4a", "mp4", NULL };
431 static char *mp4_mimeTypes[] = { "audio/mp4", "audio/m4a", NULL };
433 InputPlugin mp4Plugin = {
434         "mp4",
435         NULL,
436         NULL,
437         NULL,
438         mp4_decode,
439         NULL,
440         mp4TagDup,
441         INPUT_PLUGIN_STREAM_FILE | INPUT_PLUGIN_STREAM_URL,
442         mp4_suffixes,
443         mp4_mimeTypes
446 #else
448 InputPlugin mp4Plugin;
450 #endif /* HAVE_FAAD */