mp4_plugin.c 10.2 KB
Newer Older
1
/* the Music Player Daemon (MPD)
2
 * Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * This project's homepage is: http://www.musicpd.org
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

19
#include "../decoder_api.h"
20 21 22

#ifdef HAVE_FAAD

Warren Dukes's avatar
Warren Dukes committed
23 24
#include "../utils.h"
#include "../log.h"
25

26 27
#include "../mp4ff/mp4ff.h"

28
#include <faad.h>
Warren Dukes's avatar
Warren Dukes committed
29 30
/* all code here is either based on or copied from FAAD2's frontend code */

Avuton Olrich's avatar
Avuton Olrich committed
31 32
static int mp4_getAACTrack(mp4ff_t * infile)
{
33 34 35 36 37 38
	/* find AAC track */
	int i, rc;
	int numTracks = mp4ff_total_tracks(infile);

	for (i = 0; i < numTracks; i++) {
		unsigned char *buff = NULL;
39
		unsigned int buff_size = 0;
40
#ifdef HAVE_MP4AUDIOSPECIFICCONFIG
41
		mp4AudioSpecificConfig mp4ASC;
42 43
#else
		unsigned long dummy1_32;
44
		unsigned char dummy2_8, dummy3_8, dummy4_8, dummy5_8, dummy6_8,
Avuton Olrich's avatar
Avuton Olrich committed
45
		    dummy7_8, dummy8_8;
46
#endif
Avuton Olrich's avatar
Avuton Olrich committed
47

48
		mp4ff_get_decoder_config(infile, i, &buff, &buff_size);
49 50

		if (buff) {
51
#ifdef HAVE_MP4AUDIOSPECIFICCONFIG
52
			rc = AudioSpecificConfig(buff, buff_size, &mp4ASC);
53 54
#else
			rc = AudioSpecificConfig(buff, &dummy1_32, &dummy2_8,
Avuton Olrich's avatar
Avuton Olrich committed
55 56 57
						 &dummy3_8, &dummy4_8,
						 &dummy5_8, &dummy6_8,
						 &dummy7_8, &dummy8_8);
58
#endif
59
			free(buff);
Avuton Olrich's avatar
Avuton Olrich committed
60 61
			if (rc < 0)
				continue;
62
			return i;
63 64 65 66 67 68 69
		}
	}

	/* can't decode this */
	return -1;
}

70
static uint32_t mp4_inputStreamReadCallback(void *inStream, void *buffer,
Avuton Olrich's avatar
Avuton Olrich committed
71
					    uint32_t length)
72
{
Avuton Olrich's avatar
Avuton Olrich committed
73
	return readFromInputStream((InputStream *) inStream, buffer, 1, length);
74
}
75

Avuton Olrich's avatar
Avuton Olrich committed
76 77
static uint32_t mp4_inputStreamSeekCallback(void *inStream, uint64_t position)
{
78
	return seekInputStream((InputStream *) inStream, position, SEEK_SET);
79 80
}

81
static int mp4_decode(struct decoder * mpd_decoder, InputStream * inStream)
Avuton Olrich's avatar
Avuton Olrich committed
82 83 84
{
	mp4ff_t *mp4fh;
	mp4ff_callback_t *mp4cb;
85
	int32_t track;
86
	float file_time, total_time;
87
	int32_t scale;
88
	faacDecHandle decoder;
89
	faacDecFrameInfo frameInfo;
90
	faacDecConfigurationPtr config;
91
	struct audio_format audio_format;
Avuton Olrich's avatar
Avuton Olrich committed
92
	unsigned char *mp4Buffer;
93
	unsigned int mp4BufferSize;
94
	uint32_t sampleRate;
95 96 97
	unsigned char channels;
	long sampleId;
	long numSamples;
Warren Dukes's avatar
Warren Dukes committed
98 99
	long dur;
	unsigned int sampleCount;
Avuton Olrich's avatar
Avuton Olrich committed
100
	char *sampleBuffer;
Warren Dukes's avatar
Warren Dukes committed
101
	size_t sampleBufferLen;
Warren Dukes's avatar
Warren Dukes committed
102
	unsigned int initial = 1;
Avuton Olrich's avatar
Avuton Olrich committed
103
	float *seekTable;
Warren Dukes's avatar
Warren Dukes committed
104 105
	long seekTableEnd = -1;
	int seekPositionFound = 0;
106
	long offset;
107
	mpd_uint16 bitRate = 0;
108
	int seeking = 0;
109
	double seek_where = 0;
110
	int initialized = 0;
111

112
	mp4cb = xmalloc(sizeof(mp4ff_callback_t));
113 114
	mp4cb->read = mp4_inputStreamReadCallback;
	mp4cb->seek = mp4_inputStreamSeekCallback;
115
	mp4cb->user_data = inStream;
116 117

	mp4fh = mp4ff_open_read(mp4cb);
Avuton Olrich's avatar
Avuton Olrich committed
118
	if (!mp4fh) {
119 120 121 122 123 124
		ERROR("Input does not appear to be a mp4 stream.\n");
		free(mp4cb);
		return -1;
	}

	track = mp4_getAACTrack(mp4fh);
Avuton Olrich's avatar
Avuton Olrich committed
125
	if (track < 0) {
126 127 128 129 130 131
		ERROR("No AAC track found in mp4 stream.\n");
		mp4ff_close(mp4fh);
		free(mp4cb);
		return -1;
	}

132 133 134 135 136 137 138 139 140 141 142
	decoder = faacDecOpen();

	config = faacDecGetCurrentConfiguration(decoder);
	config->outputFormat = FAAD_FMT_16BIT;
#ifdef HAVE_FAACDECCONFIGURATION_DOWNMATRIX
	config->downMatrix = 1;
#endif
#ifdef HAVE_FAACDECCONFIGURATION_DONTUPSAMPLEIMPLICITSBR
	config->dontUpSampleImplicitSBR = 0;
#endif
	faacDecSetConfiguration(decoder, config);
143

144
	audio_format.bits = 16;
145 146 147

	mp4Buffer = NULL;
	mp4BufferSize = 0;
Avuton Olrich's avatar
Avuton Olrich committed
148
	mp4ff_get_decoder_config(mp4fh, track, &mp4Buffer, &mp4BufferSize);
149

Avuton Olrich's avatar
Avuton Olrich committed
150
	if (faacDecInit2
151
	    (decoder, mp4Buffer, mp4BufferSize, &sampleRate, &channels) < 0) {
Warren Dukes's avatar
Warren Dukes committed
152
		ERROR("Error not a AAC stream.\n");
153 154 155 156 157 158
		faacDecClose(decoder);
		mp4ff_close(mp4fh);
		free(mp4cb);
		return -1;
	}

159 160
	audio_format.sampleRate = sampleRate;
	audio_format.channels = channels;
161
	file_time = mp4ff_get_track_duration_use_offsets(mp4fh, track);
Avuton Olrich's avatar
Avuton Olrich committed
162
	scale = mp4ff_time_scale(mp4fh, track);
163

Avuton Olrich's avatar
Avuton Olrich committed
164 165
	if (mp4Buffer)
		free(mp4Buffer);
166

Avuton Olrich's avatar
Avuton Olrich committed
167
	if (scale < 0) {
168 169 170 171 172 173
		ERROR("Error getting audio format of mp4 AAC track.\n");
		faacDecClose(decoder);
		mp4ff_close(mp4fh);
		free(mp4cb);
		return -1;
	}
174
	total_time = ((float)file_time) / scale;
175

Avuton Olrich's avatar
Avuton Olrich committed
176
	numSamples = mp4ff_num_samples(mp4fh, track);
177 178 179 180 181 182 183
	if (numSamples > (long)(INT_MAX / sizeof(float))) {
		 ERROR("Integer overflow.\n");
		 faacDecClose(decoder);
		 mp4ff_close(mp4fh);
		 free(mp4cb);
		 return -1;
	}
184

185
	file_time = 0.0;
Warren Dukes's avatar
Warren Dukes committed
186

187
	seekTable = xmalloc(sizeof(float) * numSamples);
Warren Dukes's avatar
Warren Dukes committed
188

189
	for (sampleId = 0; sampleId < numSamples; sampleId++) {
190
		if (decoder_get_command(mpd_decoder) == DECODE_COMMAND_SEEK) {
Avuton Olrich's avatar
Avuton Olrich committed
191
			seeking = 1;
192 193
			seek_where = decoder_seek_where(mpd_decoder);
		}
194

Avuton Olrich's avatar
Avuton Olrich committed
195
		if (seeking && seekTableEnd > 1 &&
196
		    seekTable[seekTableEnd] >= seek_where) {
Warren Dukes's avatar
Warren Dukes committed
197
			int i = 2;
198
			while (seekTable[i] < seek_where)
Avuton Olrich's avatar
Avuton Olrich committed
199 200
				i++;
			sampleId = i - 1;
201
			file_time = seekTable[sampleId];
Warren Dukes's avatar
Warren Dukes committed
202 203
		}

Avuton Olrich's avatar
Avuton Olrich committed
204 205
		dur = mp4ff_get_sample_duration(mp4fh, track, sampleId);
		offset = mp4ff_get_sample_offset(mp4fh, track, sampleId);
Warren Dukes's avatar
Warren Dukes committed
206

Avuton Olrich's avatar
Avuton Olrich committed
207
		if (sampleId > seekTableEnd) {
208
			seekTable[sampleId] = file_time;
Warren Dukes's avatar
Warren Dukes committed
209 210 211
			seekTableEnd = sampleId;
		}

Avuton Olrich's avatar
Avuton Olrich committed
212 213 214 215 216 217
		if (sampleId == 0)
			dur = 0;
		if (offset > dur)
			dur = 0;
		else
			dur -= offset;
218
		file_time += ((float)dur) / scale;
Warren Dukes's avatar
Warren Dukes committed
219

220
		if (seeking && file_time > seek_where)
Avuton Olrich's avatar
Avuton Olrich committed
221
			seekPositionFound = 1;
Warren Dukes's avatar
Warren Dukes committed
222

Avuton Olrich's avatar
Avuton Olrich committed
223
		if (seeking && seekPositionFound) {
Warren Dukes's avatar
Warren Dukes committed
224
			seekPositionFound = 0;
225
			decoder_clear(mpd_decoder);
226
			seeking = 0;
227
			decoder_command_finished(mpd_decoder);
Warren Dukes's avatar
Warren Dukes committed
228
		}
229

Avuton Olrich's avatar
Avuton Olrich committed
230 231 232 233
		if (seeking)
			continue;

		if (mp4ff_read_sample(mp4fh, track, sampleId, &mp4Buffer,
234 235 236
				      &mp4BufferSize) == 0)
			break;

237
#ifdef HAVE_FAAD_BUFLEN_FUNCS
Avuton Olrich's avatar
Avuton Olrich committed
238 239
		sampleBuffer = faacDecDecode(decoder, &frameInfo, mp4Buffer,
					     mp4BufferSize);
240
#else
Avuton Olrich's avatar
Avuton Olrich committed
241
		sampleBuffer = faacDecDecode(decoder, &frameInfo, mp4Buffer);
242
#endif
Warren Dukes's avatar
Warren Dukes committed
243

Avuton Olrich's avatar
Avuton Olrich committed
244 245 246
		if (mp4Buffer)
			free(mp4Buffer);
		if (frameInfo.error > 0) {
247
			ERROR("faad2 error: %s\n",
Avuton Olrich's avatar
Avuton Olrich committed
248
			      faacDecGetErrorMessage(frameInfo.error));
249 250 251
			break;
		}

252
		if (!initialized) {
253
			channels = frameInfo.channels;
254
#ifdef HAVE_FAACDECFRAMEINFO_SAMPLERATE
255
			scale = frameInfo.samplerate;
256
#endif
257 258
			audio_format.sampleRate = scale;
			audio_format.channels = frameInfo.channels;
259 260
			decoder_initialized(mpd_decoder, &audio_format,
					    total_time);
261
			initialized = 1;
262 263
		}

264
		if (channels * (unsigned long)(dur + offset) > frameInfo.samples) {
Avuton Olrich's avatar
Avuton Olrich committed
265
			dur = frameInfo.samples / channels;
266 267
			offset = 0;
		}
268

Avuton Olrich's avatar
Avuton Olrich committed
269
		sampleCount = (unsigned long)(dur * channels);
Warren Dukes's avatar
Warren Dukes committed
270

Avuton Olrich's avatar
Avuton Olrich committed
271 272 273 274 275
		if (sampleCount > 0) {
			initial = 0;
			bitRate = frameInfo.bytesconsumed * 8.0 *
			    frameInfo.channels * scale /
			    frameInfo.samples / 1000 + 0.5;
276 277
		}

Avuton Olrich's avatar
Avuton Olrich committed
278
		sampleBufferLen = sampleCount * 2;
279

Avuton Olrich's avatar
Avuton Olrich committed
280
		sampleBuffer += offset * channels * 2;
281

Max Kellermann's avatar
Max Kellermann committed
282 283 284
		decoder_data(mpd_decoder, inStream, 1, sampleBuffer,
			     sampleBufferLen, file_time,
			     bitRate, NULL);
285
		if (decoder_get_command(mpd_decoder) == DECODE_COMMAND_STOP)
286
			break;
Warren Dukes's avatar
Warren Dukes committed
287
	}
288

Warren Dukes's avatar
Warren Dukes committed
289 290 291 292 293
	free(seekTable);
	faacDecClose(decoder);
	mp4ff_close(mp4fh);
	free(mp4cb);

294
	if (!initialized)
Avuton Olrich's avatar
Avuton Olrich committed
295
		return -1;
Warren Dukes's avatar
Warren Dukes committed
296

297
	if (decoder_get_command(mpd_decoder) == DECODE_COMMAND_SEEK && seeking) {
298
		decoder_clear(mpd_decoder);
299
		decoder_command_finished(mpd_decoder);
Avuton Olrich's avatar
Avuton Olrich committed
300
	}
301
	decoder_flush(mpd_decoder);
302 303 304 305

	return 0;
}

306
static struct tag *mp4DataDup(char *file, int *mp4MetadataFound)
Avuton Olrich's avatar
Avuton Olrich committed
307
{
308
	struct tag *ret = NULL;
Warren Dukes's avatar
Warren Dukes committed
309
	InputStream inStream;
Avuton Olrich's avatar
Avuton Olrich committed
310
	mp4ff_t *mp4fh;
311
	mp4ff_callback_t *callback;
Warren Dukes's avatar
Warren Dukes committed
312
	int32_t track;
313
	int32_t file_time;
Warren Dukes's avatar
Warren Dukes committed
314
	int32_t scale;
315
	int i;
Warren Dukes's avatar
Warren Dukes committed
316 317

	*mp4MetadataFound = 0;
Avuton Olrich's avatar
Avuton Olrich committed
318 319

	if (openInputStream(&inStream, file) < 0) {
320 321 322
		DEBUG("mp4DataDup: Failed to open file: %s\n", file);
		return NULL;
	}
Warren Dukes's avatar
Warren Dukes committed
323

324 325 326 327
	callback = xmalloc(sizeof(mp4ff_callback_t));
	callback->read = mp4_inputStreamReadCallback;
	callback->seek = mp4_inputStreamSeekCallback;
	callback->user_data = &inStream;
Warren Dukes's avatar
Warren Dukes committed
328

329
	mp4fh = mp4ff_open_read(callback);
Avuton Olrich's avatar
Avuton Olrich committed
330
	if (!mp4fh) {
331
		free(callback);
Warren Dukes's avatar
Warren Dukes committed
332 333 334 335 336
		closeInputStream(&inStream);
		return NULL;
	}

	track = mp4_getAACTrack(mp4fh);
Avuton Olrich's avatar
Avuton Olrich committed
337
	if (track < 0) {
Warren Dukes's avatar
Warren Dukes committed
338 339
		mp4ff_close(mp4fh);
		closeInputStream(&inStream);
340
		free(callback);
Warren Dukes's avatar
Warren Dukes committed
341 342 343
		return NULL;
	}

344
	ret = tag_new();
345
	file_time = mp4ff_get_track_duration_use_offsets(mp4fh, track);
Avuton Olrich's avatar
Avuton Olrich committed
346 347
	scale = mp4ff_time_scale(mp4fh, track);
	if (scale < 0) {
Warren Dukes's avatar
Warren Dukes committed
348 349
		mp4ff_close(mp4fh);
		closeInputStream(&inStream);
350
		free(callback);
351
		tag_free(ret);
Warren Dukes's avatar
Warren Dukes committed
352 353
		return NULL;
	}
354
	ret->time = ((float)file_time) / scale + 0.5;
Warren Dukes's avatar
Warren Dukes committed
355

Avuton Olrich's avatar
Avuton Olrich committed
356 357 358
	for (i = 0; i < mp4ff_meta_get_num_items(mp4fh); i++) {
		char *item;
		char *value;
Warren Dukes's avatar
Warren Dukes committed
359

360
		mp4ff_meta_get_by_index(mp4fh, i, &item, &value);
Warren Dukes's avatar
Warren Dukes committed
361

Avuton Olrich's avatar
Avuton Olrich committed
362
		if (0 == strcasecmp("artist", item)) {
363
			tag_add_item(ret, TAG_ITEM_ARTIST, value);
364
			*mp4MetadataFound = 1;
Avuton Olrich's avatar
Avuton Olrich committed
365
		} else if (0 == strcasecmp("title", item)) {
366
			tag_add_item(ret, TAG_ITEM_TITLE, value);
367
			*mp4MetadataFound = 1;
Avuton Olrich's avatar
Avuton Olrich committed
368
		} else if (0 == strcasecmp("album", item)) {
369
			tag_add_item(ret, TAG_ITEM_ALBUM, value);
370
			*mp4MetadataFound = 1;
Avuton Olrich's avatar
Avuton Olrich committed
371
		} else if (0 == strcasecmp("track", item)) {
372
			tag_add_item(ret, TAG_ITEM_TRACK, value);
373
			*mp4MetadataFound = 1;
Avuton Olrich's avatar
Avuton Olrich committed
374
		} else if (0 == strcasecmp("disc", item)) {	/* Is that the correct id? */
375
			tag_add_item(ret, TAG_ITEM_DISC, value);
376
			*mp4MetadataFound = 1;
Avuton Olrich's avatar
Avuton Olrich committed
377
		} else if (0 == strcasecmp("genre", item)) {
378
			tag_add_item(ret, TAG_ITEM_GENRE, value);
379
			*mp4MetadataFound = 1;
Avuton Olrich's avatar
Avuton Olrich committed
380
		} else if (0 == strcasecmp("date", item)) {
381
			tag_add_item(ret, TAG_ITEM_DATE, value);
382 383
			*mp4MetadataFound = 1;
		}
Warren Dukes's avatar
Warren Dukes committed
384

385 386
		free(item);
		free(value);
Warren Dukes's avatar
Warren Dukes committed
387 388 389 390 391 392 393 394
	}

	mp4ff_close(mp4fh);
	closeInputStream(&inStream);

	return ret;
}

395
static struct tag *mp4TagDup(char *file)
Avuton Olrich's avatar
Avuton Olrich committed
396
{
397
	struct tag *ret = NULL;
Warren Dukes's avatar
Warren Dukes committed
398 399 400
	int mp4MetadataFound = 0;

	ret = mp4DataDup(file, &mp4MetadataFound);
Avuton Olrich's avatar
Avuton Olrich committed
401 402 403
	if (!ret)
		return NULL;
	if (!mp4MetadataFound) {
404
		struct tag *temp = tag_id3_load(file);
Avuton Olrich's avatar
Avuton Olrich committed
405
		if (temp) {
Warren Dukes's avatar
Warren Dukes committed
406
			temp->time = ret->time;
407
			tag_free(ret);
Warren Dukes's avatar
Warren Dukes committed
408 409 410 411 412 413 414
			ret = temp;
		}
	}

	return ret;
}

Max Kellermann's avatar
Max Kellermann committed
415 416
static const char *mp4_suffixes[] = { "m4a", "mp4", NULL };
static const char *mp4_mimeTypes[] = { "audio/mp4", "audio/m4a", NULL };
Warren Dukes's avatar
Warren Dukes committed
417

418
struct decoder_plugin mp4Plugin = {
419
	"mp4",
420 421
	NULL,
	NULL,
422
	NULL,
423
	mp4_decode,
424
	NULL,
425
	mp4TagDup,
426
	INPUT_PLUGIN_STREAM_FILE | INPUT_PLUGIN_STREAM_URL,
427 428
	mp4_suffixes,
	mp4_mimeTypes
Warren Dukes's avatar
Warren Dukes committed
429 430 431 432
};

#else

433
struct decoder_plugin mp4Plugin;
Warren Dukes's avatar
Warren Dukes committed
434

Avuton Olrich's avatar
Avuton Olrich committed
435
#endif /* HAVE_FAAD */