File: mp4.c

package info (click to toggle)
cmus 2.2.0-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 1,196 kB
  • ctags: 2,714
  • sloc: ansic: 24,078; sh: 1,024; makefile: 209; python: 26
file content (403 lines) | stat: -rw-r--r-- 9,707 bytes parent folder | download
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
/*
 * Copyright 2006 dnk <dnk@bjum.net>
 *
 * 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.
 */

#include "ip.h"
#include "xmalloc.h"
#include "debug.h"
#include "file.h"

#include <mp4.h>
#include <faad.h>

#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>

struct mp4_private {
	char *overflow_buf;
	int overflow_buf_len;

	unsigned char channels;
	unsigned long sample_rate;

	faacDecHandle decoder;		/* typedef void * */

	struct {
		MP4FileHandle handle;	/* typedef void * */

		MP4TrackId track;
		MP4SampleId sample;
		MP4SampleId num_samples;
	} mp4;
};


static MP4TrackId mp4_get_track(MP4FileHandle *handle)
{
	MP4TrackId num_tracks;
	const char *track_type;
	uint8_t obj_type;
	MP4TrackId i;

	num_tracks = MP4GetNumberOfTracks(handle, NULL, 0);

	for (i = 1; i <= num_tracks; i++) {
		track_type = MP4GetTrackType(handle, i);
		if (!track_type)
			continue;

		if (!MP4_IS_AUDIO_TRACK_TYPE(track_type))
			continue;

		/* MP4GetTrackAudioType */
		obj_type = MP4GetTrackEsdsObjectTypeId(handle, i);
		if (obj_type == MP4_INVALID_AUDIO_TYPE)
			continue;

		if (obj_type == MP4_MPEG4_AUDIO_TYPE) {
			obj_type = MP4GetTrackAudioMpeg4Type(handle, i);

			if (MP4_IS_MPEG4_AAC_AUDIO_TYPE(obj_type))
				return i;
		} else {
			if (MP4_IS_AAC_AUDIO_TYPE(obj_type))
				return i;
		}
	}

	return MP4_INVALID_TRACK_ID;
}

static int mp4_open(struct input_plugin_data *ip_data)
{
	struct mp4_private *priv;
	faacDecConfigurationPtr neaac_cfg;
	unsigned char *buf;
	unsigned int buf_size;


	/* http://sourceforge.net/forum/message.php?msg_id=3578887 */
	if (ip_data->remote)
		return -IP_ERROR_FUNCTION_NOT_SUPPORTED;

	/* init private struct */
	priv = xnew0(struct mp4_private, 1);
	ip_data->private = priv;

	priv->decoder = faacDecOpen();

	/* set decoder config */
	neaac_cfg = faacDecGetCurrentConfiguration(priv->decoder);
	neaac_cfg->outputFormat = FAAD_FMT_16BIT;	/* force 16 bit audio */
	neaac_cfg->downMatrix = 1;			/* 5.1 -> stereo */
	faacDecSetConfiguration(priv->decoder, neaac_cfg);

	/* open mpeg-4 file */
	priv->mp4.handle = MP4Read(ip_data->filename, 0);
	if (!priv->mp4.handle) {
		d_print("MP4Read failed\n");
		goto out;
	}

	/* find aac audio track */
	priv->mp4.track = mp4_get_track(priv->mp4.handle);
	if (priv->mp4.track == MP4_INVALID_TRACK_ID) {
		d_print("MP4FindTrackId failed\n");
		goto out;
	}

	priv->mp4.num_samples = MP4GetTrackNumberOfSamples(priv->mp4.handle, priv->mp4.track);

	priv->mp4.sample = 1;

	buf = NULL;
	buf_size = 0;
	if (!MP4GetTrackESConfiguration(priv->mp4.handle, priv->mp4.track, &buf, &buf_size)) {
		/* failed to get mpeg-4 audio config... this is ok.
		 * faacDecInit2() will simply use default values instead.
		 */
		buf = NULL;
		buf_size = 0;
	}

	/* init decoder according to mpeg-4 audio config */
	if (faacDecInit2(priv->decoder, buf, buf_size, &priv->sample_rate, &priv->channels) < 0) {
		free(buf);
		goto out;
	}

	free(buf);

	d_print("sample rate %luhz, channels %u\n", priv->sample_rate, priv->channels);

	ip_data->sf = sf_rate(priv->sample_rate) | sf_channels(priv->channels) | sf_bits(16) | sf_signed(1);
#if defined(WORDS_BIGENDIAN)
	ip_data->sf |= sf_bigendian(1);
#endif

	return 0;

out:
	if (priv->mp4.handle)
		MP4Close(priv->mp4.handle);
	if (priv->decoder)
		faacDecClose(priv->decoder);
	free(priv);
	return -IP_ERROR_FILE_FORMAT;
}

static int mp4_close(struct input_plugin_data *ip_data)
{
	struct mp4_private *priv;

	priv = ip_data->private;

	if (priv->mp4.handle)
		MP4Close(priv->mp4.handle);

	if (priv->decoder)
		faacDecClose(priv->decoder);

	free(priv);
	ip_data->private = NULL;

	return 0;
}

/* returns -1 on fatal errors
 * returns -2 on non-fatal errors
 * 0 on eof
 * number of bytes put in 'buffer' on success */
static int decode_one_frame(struct input_plugin_data *ip_data, void *buffer, int count)
{
	struct mp4_private *priv;
	unsigned char *aac_data = NULL;
	unsigned int aac_data_len = 0;
	faacDecFrameInfo frame_info;
	char *sample_buf;
	int bytes;

	priv = ip_data->private;

	BUG_ON(priv->overflow_buf_len);

	if (priv->mp4.sample > priv->mp4.num_samples)
		return 0; /* EOF */

	if (MP4ReadSample(priv->mp4.handle, priv->mp4.track, priv->mp4.sample,
		&aac_data, &aac_data_len, NULL, NULL, NULL, NULL) == 0) {
		d_print("error reading mp4 sample %d\n", priv->mp4.sample);
		errno = EINVAL;
		return -1;
	}

	priv->mp4.sample++;

	if (!aac_data) {
		d_print("aac_data == NULL\n");
		errno = EINVAL;
		return -1;
	}

	sample_buf = faacDecDecode(priv->decoder, &frame_info, aac_data, aac_data_len);

	free(aac_data);

	if (!sample_buf || frame_info.bytesconsumed <= 0) {
		d_print("fatal error: %s\n", faacDecGetErrorMessage(frame_info.error));
		errno = EINVAL;
		return -1;
	}

	if (frame_info.error != 0) {
		d_print("frame error: %s\n", faacDecGetErrorMessage(frame_info.error));
		return -2;
	}

	if (frame_info.samples <= 0)
		return -2;

	if (frame_info.channels != priv->channels || frame_info.samplerate != priv->sample_rate) {
		d_print("invalid channel or sample_rate count\n");
		return -2;
	}

	/* 16-bit samples */
	bytes = frame_info.samples * 2;

	if (bytes > count) {
		/* decoded too much; keep overflow. */
		priv->overflow_buf = sample_buf + count;
		priv->overflow_buf_len = bytes - count;
		memcpy(buffer, sample_buf, count);
		return count;
	} else {
		memcpy(buffer, sample_buf, bytes);
	}

	return bytes;
}

static int mp4_read(struct input_plugin_data *ip_data, char *buffer, int count)
{
	struct mp4_private *priv;
	int rc;

	priv = ip_data->private;

	/* use overflow from previous call (if any) */
	if (priv->overflow_buf_len > 0) {
		int len = priv->overflow_buf_len;

		if (len > count)
			len = count;

		memcpy(buffer, priv->overflow_buf, len);
		priv->overflow_buf += len;
		priv->overflow_buf_len -= len;

		return len;
	}

	do {
		rc = decode_one_frame(ip_data, buffer, count);
	} while (rc == -2);

	return rc;
}

static int mp4_seek(struct input_plugin_data *ip_data, double offset)
{
	struct mp4_private *priv;
	MP4SampleId sample;
	uint32_t scale;

	priv = ip_data->private;

	scale = MP4GetTrackTimeScale(priv->mp4.handle, priv->mp4.track);
	if (scale == 0)
		return -IP_ERROR_INTERNAL;

	sample = MP4GetSampleIdFromTime(priv->mp4.handle, priv->mp4.track,
		(MP4Timestamp)(offset * (double)scale), 0);
	if (sample == MP4_INVALID_SAMPLE_ID)
		return -IP_ERROR_INTERNAL;

	priv->mp4.sample = sample;

	d_print("seeking to sample %d\n", sample);

	return 0;
}

static int mp4_read_comments(struct input_plugin_data *ip_data,
		struct keyval **comments)
{
	struct mp4_private *priv;
	uint16_t meta_num, meta_total;
	uint8_t val;
	/*uint8_t *ustr;
	uint32_t size;*/
	char *str;
	GROWING_KEYVALS(c);

	priv = ip_data->private;

	/* MP4GetMetadata* provides malloced pointers, and the data
	 * is in UTF-8 (or at least it should be). */
	if (MP4GetMetadataArtist(priv->mp4.handle, &str))
		comments_add(&c, "artist", str);
	if (MP4GetMetadataAlbum(priv->mp4.handle, &str))
		comments_add(&c, "album", str);
	if (MP4GetMetadataName(priv->mp4.handle, &str))
		comments_add(&c, "title", str);
	if (MP4GetMetadataGenre(priv->mp4.handle, &str))
		comments_add(&c, "genre", str);
	if (MP4GetMetadataYear(priv->mp4.handle, &str))
		comments_add(&c, "date", str);

	if (MP4GetMetadataCompilation(priv->mp4.handle, &val))
		comments_add_const(&c, "compilation", val ? "yes" : "no");
#if 0
	if (MP4GetBytesProperty(priv->mp4.handle, "moov.udta.meta.ilst.aART.data", &ustr, &size)) {
		char *xstr;

		/* What's this?
		 * This is the result from lack of documentation.
		 * It's supposed to return just a string, but it
		 * returns an additional 16 bytes of junk at the
		 * beginning. Could be a bug. Could be intentional.
		 * Hopefully this works around it:
		 */
		if (ustr[0] == 0 && size > 16) {
			ustr += 16;
			size -= 16;
		}
		xstr = xmalloc(size + 1);
		memcpy(xstr, ustr, size);
		xstr[size] = 0;
		comments_add(&c, "albumartist", xstr);
		free(xstr);
	}
#endif
	if (MP4GetMetadataTrack(priv->mp4.handle, &meta_num, &meta_total)) {
		char buf[6];
		snprintf(buf, 6, "%u", meta_num);
		comments_add_const(&c, "tracknumber", buf);
	}
	if (MP4GetMetadataDisk(priv->mp4.handle, &meta_num, &meta_total)) {
		char buf[6];
		snprintf(buf, 6, "%u", meta_num);
		comments_add_const(&c, "discnumber", buf);
	}

	comments_terminate(&c);
	*comments = c.comments;
	return 0;
}

static int mp4_duration(struct input_plugin_data *ip_data)
{
	struct mp4_private *priv;
	uint32_t scale;
	uint64_t duration;

	priv = ip_data->private;

	scale = MP4GetTrackTimeScale(priv->mp4.handle, priv->mp4.track);
	if (scale == 0)
		return 0;

	duration = MP4GetTrackDuration(priv->mp4.handle, priv->mp4.track);

	return duration / scale;
}

const struct input_plugin_ops ip_ops = {
	.open = mp4_open,
	.close = mp4_close,
	.read = mp4_read,
	.seek = mp4_seek,
	.read_comments = mp4_read_comments,
	.duration = mp4_duration
};

const char * const ip_extensions[] = { "mp4", "m4a", "m4b", NULL };
const char * const ip_mime_types[] = { /*"audio/mp4", "audio/mp4a-latm",*/ NULL };