1
/*
2
 * Copyright 2008-2011 Various Authors
3
 * Copyright 2007 Kevin Ko <kevin.s.ko@gmail.com>
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License as
7
 * published by the Free Software Foundation; either version 2 of the
8
 * License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful, but
11
 * WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
#include "ip.h"
20
#include "xmalloc.h"
21
#include "debug.h"
22
#include "utils.h"
23
#include "comment.h"
24
#ifdef HAVE_CONFIG
25
#include "config/ffmpeg.h"
26
#endif
27
28
#include <stdio.h>
29
#ifdef HAVE_FFMPEG_AVCODEC_H
30
#include <ffmpeg/avcodec.h>
31
#include <ffmpeg/avformat.h>
32
#include <ffmpeg/avio.h>
33
#else
34
#include <libavcodec/avcodec.h>
35
#include <libavformat/avformat.h>
36
#include <libavformat/avio.h>
37
#ifndef AVUTIL_MATHEMATICS_H
38
#include <libavutil/mathematics.h>
39
#endif
40
#endif
41
42
#if (LIBAVFORMAT_VERSION_INT < ((52<<16)+(31<<8)+0))
43
# define NUM_FFMPEG_KEYS 8
44
#endif
45
46
#if (LIBAVCODEC_VERSION_INT < ((52<<16)+(64<<8)+0))
47
# define AVMEDIA_TYPE_AUDIO CODEC_TYPE_AUDIO
48
#endif
49
50
#if (LIBAVCODEC_VERSION_INT < ((52<<16)+(94<<8)+1))
51
#define AV_SAMPLE_FMT_U8   SAMPLE_FMT_U8
52
#define AV_SAMPLE_FMT_S16  SAMPLE_FMT_S16
53
#define AV_SAMPLE_FMT_S32  SAMPLE_FMT_S32
54
#define AV_SAMPLE_FMT_FLT  SAMPLE_FMT_FLT
55
#if (LIBAVCODEC_VERSION_INT > ((51<<16)+(64<<8)+0))
56
#define AV_SAMPLE_FMT_DBL  SAMPLE_FMT_DBL
57
#endif
58
#endif
59
60
#if (LIBAVUTIL_VERSION_INT < ((51<<16)+(5<<8)+0))
61
#define AV_DICT_IGNORE_SUFFIX AV_METADATA_IGNORE_SUFFIX
62
#define av_dict_get av_metadata_get
63
#define AVDictionaryEntry AVMetadataTag
64
#endif
65
66
struct ffmpeg_input {
67
	AVPacket pkt;
68
	int curr_pkt_size;
69
	uint8_t *curr_pkt_buf;
70
71
	unsigned long curr_size;
72
	unsigned long curr_duration;
73
};
74
75
struct ffmpeg_output {
76
	uint8_t *buffer;
77
	uint8_t *buffer_malloc;
78
	uint8_t *buffer_pos;	/* current buffer position */
79
	int buffer_used_len;
80
};
81
82
struct ffmpeg_private {
83
	AVCodecContext *codec_context;
84
	AVFormatContext *input_context;
85
	AVCodec *codec;
86
	int stream_index;
87
88
	struct ffmpeg_input *input;
89
	struct ffmpeg_output *output;
90
};
91
92
static struct ffmpeg_input *ffmpeg_input_create(void)
93
{
94
	struct ffmpeg_input *input = xnew(struct ffmpeg_input, 1);
95
96
	if (av_new_packet(&input->pkt, 0) != 0) {
97
		free(input);
98
		return NULL;
99
	}
100
	input->curr_pkt_size = 0;
101
	input->curr_pkt_buf = input->pkt.data;
102
	return input;
103
}
104
105
static void ffmpeg_input_free(struct ffmpeg_input *input)
106
{
107
	av_free_packet(&input->pkt);
108
	free(input);
109
}
110
111
static struct ffmpeg_output *ffmpeg_output_create(void)
112
{
113
	struct ffmpeg_output *output = xnew(struct ffmpeg_output, 1);
114
115
	output->buffer_malloc = xnew(uint8_t, AVCODEC_MAX_AUDIO_FRAME_SIZE + 15);
116
	output->buffer = output->buffer_malloc;
117
	/* align to 16 bytes so avcodec can SSE/Altivec/etc */
118
	while ((intptr_t) output->buffer % 16)
119
		output->buffer += 1;
120
	output->buffer_pos = output->buffer;
121
	output->buffer_used_len = 0;
122
	return output;
123
}
124
125
static void ffmpeg_output_free(struct ffmpeg_output *output)
126
{
127
	free(output->buffer_malloc);
128
	output->buffer_malloc = NULL;
129
	output->buffer = NULL;
130
	free(output);
131
}
132
133
static inline void ffmpeg_buffer_flush(struct ffmpeg_output *output)
134
{
135
	output->buffer_pos = output->buffer;
136
	output->buffer_used_len = 0;
137
}
138
139
static void ffmpeg_init(void)
140
{
141
	static int inited = 0;
142
143
	if (inited != 0)
144
		return;
145
	inited = 1;
146
147
	av_log_set_level(AV_LOG_QUIET);
148
149
#if (LIBAVFORMAT_VERSION_INT <= ((50<<16) + (4<<8) + 0))
150
	avcodec_init();
151
	register_avcodec(&wmav1_decoder);
152
	register_avcodec(&wmav2_decoder);
153
154
	/* libavformat versions <= 50.4.0 have asf_init().  From SVN revision
155
	 * 5697->5707 of asf.c, this function was removed, preferring the use of
156
	 * explicit calls.  Note that version 50.5.0 coincides with SVN revision
157
	 * 5729, so there is a window of incompatibility for revisions 5707 and 5720
158
	 * of asf.c.
159
	 */
160
	asf_init();
161
162
	/* Uncomment this for shorten (.shn) support.
163
	   register_avcodec(&shorten_decoder);
164
	   raw_init();
165
	 */
166
167
	register_protocol(&file_protocol);
168
#else
169
	/* We could register decoders explicitly to save memory, but we have to
170
	 * be careful about compatibility. */
171
	av_register_all();
172
#endif
173
}
174
175
static int ffmpeg_open(struct input_plugin_data *ip_data)
176
{
177
	struct ffmpeg_private *priv;
178
	int err = 0;
179
	int i;
180
	int stream_index = -1;
181
	int64_t channel_layout = 0;
182
	AVCodec *codec;
183
	AVCodecContext *cc = NULL;
184
	AVFormatContext *ic = NULL;
185
186
	ffmpeg_init();
187
188
#if (LIBAVFORMAT_VERSION_INT <= ((53<<16)+(2<<8)+0))
189
	err = av_open_input_file(&ic, ip_data->filename, NULL, 0, NULL);
190
#else
191
	err = avformat_open_input(&ic, ip_data->filename, NULL, NULL);
192
#endif
193
	if (err < 0) {
194
		d_print("av_open failed: %d\n", err);
195
		return -IP_ERROR_FILE_FORMAT;
196
	}
197
198
	do {
199
#if (LIBAVFORMAT_VERSION_INT <= ((53<<16)+(5<<8)+0))
200
		err = av_find_stream_info(ic);
201
#else
202
		err = avformat_find_stream_info(ic, NULL);
203
#endif
204
		if (err < 0) {
205
			d_print("unable to find stream info: %d\n", err);
206
			err = -IP_ERROR_FILE_FORMAT;
207
			break;
208
		}
209
210
		for (i = 0; i < ic->nb_streams; i++) {
211
			cc = ic->streams[i]->codec;
212
			if (cc->codec_type == AVMEDIA_TYPE_AUDIO) {
213
				stream_index = i;
214
				break;
215
			}
216
		}
217
218
		if (stream_index == -1) {
219
			d_print("could not find audio stream\n");
220
			err = -IP_ERROR_FILE_FORMAT;
221
			break;
222
		}
223
224
		codec = avcodec_find_decoder(cc->codec_id);
225
		if (!codec) {
226
			d_print("codec not found: %d, %s\n", cc->codec_id, cc->codec_name);
227
			err = -IP_ERROR_UNSUPPORTED_FILE_TYPE;
228
			break;
229
		}
230
231
		if (codec->capabilities & CODEC_CAP_TRUNCATED)
232
			cc->flags |= CODEC_FLAG_TRUNCATED;
233
234
#if (LIBAVCODEC_VERSION_INT < ((53<<16)+(8<<8)+0))
235
		if (avcodec_open(cc, codec) < 0) {
236
#else
237
		if (avcodec_open2(cc, codec, NULL) < 0) {
238
#endif
239
			d_print("could not open codec: %d, %s\n", cc->codec_id, cc->codec_name);
240
			err = -IP_ERROR_UNSUPPORTED_FILE_TYPE;
241
			break;
242
		}
243
244
#if (LIBAVCODEC_VERSION_INT > ((51<<16)+(64<<8)+0))
245
		if (cc->sample_fmt == AV_SAMPLE_FMT_FLT || cc->sample_fmt == AV_SAMPLE_FMT_DBL) {
246
#else
247
		if (cc->sample_fmt == AV_SAMPLE_FMT_FLT) {
248
#endif
249
			err = -IP_ERROR_SAMPLE_FORMAT;
250
			break;
251
		}
252
		/* We assume below that no more errors follow. */
253
	} while (0);
254
255
	if (err < 0) {
256
		/* Clean up.  cc is never opened at this point.  (See above assumption.) */
257
		av_close_input_file(ic);
258
		return err;
259
	}
260
261
	priv = xnew(struct ffmpeg_private, 1);
262
	priv->codec_context = cc;
263
	priv->input_context = ic;
264
	priv->codec = codec;
265
	priv->stream_index = stream_index;
266
	priv->input = ffmpeg_input_create();
267
	if (priv->input == NULL) {
268
		avcodec_close(cc);
269
		av_close_input_file(ic);
270
		free(priv);
271
		return -IP_ERROR_INTERNAL;
272
	}
273
	priv->output = ffmpeg_output_create();
274
275
	ip_data->private = priv;
276
	ip_data->sf = sf_rate(cc->sample_rate) | sf_channels(cc->channels);
277
	switch (cc->sample_fmt) {
278
	case AV_SAMPLE_FMT_U8:
279
		ip_data->sf |= sf_bits(8) | sf_signed(0);
280
		break;
281
	case AV_SAMPLE_FMT_S32:
282
		ip_data->sf |= sf_bits(32) | sf_signed(1);
283
		break;
284
	/* AV_SAMPLE_FMT_S16 */
285
	default:
286
		ip_data->sf |= sf_bits(16) | sf_signed(1);
287
		break;
288
	}
289
#ifdef WORDS_BIGENDIAN
290
	ip_data->sf |= sf_bigendian(1);
291
#endif
292
#if (LIBAVCODEC_VERSION_INT > ((52<<16)+(1<<8)+0))
293
	channel_layout = cc->channel_layout;
294
#endif
295
	channel_map_init_waveex(cc->channels, channel_layout, ip_data->channel_map);
296
	return 0;
297
}
298
299
static int ffmpeg_close(struct input_plugin_data *ip_data)
300
{
301
	struct ffmpeg_private *priv = ip_data->private;
302
303
	avcodec_close(priv->codec_context);
304
	av_close_input_file(priv->input_context);
305
	ffmpeg_input_free(priv->input);
306
	ffmpeg_output_free(priv->output);
307
	free(priv);
308
	ip_data->private = NULL;
309
	return 0;
310
}
311
312
/*
313
 * This returns the number of bytes added to the buffer.
314
 * It returns < 0 on error.  0 on EOF.
315
 */
316
static int ffmpeg_fill_buffer(AVFormatContext *ic, AVCodecContext *cc, struct ffmpeg_input *input,
317
			      struct ffmpeg_output *output)
318
{
319
	while (1) {
320
		/* frame_size specifies the size of output->buffer for
321
		 * avcodec_decode_audio2. */
322
		int frame_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
323
		int len;
324
325
		if (input->curr_pkt_size <= 0) {
326
			av_free_packet(&input->pkt);
327
			if (av_read_frame(ic, &input->pkt) < 0) {
328
				/* Force EOF once we can read no longer. */
329
				return 0;
330
			}
331
			input->curr_pkt_size = input->pkt.size;
332
			input->curr_pkt_buf = input->pkt.data;
333
			input->curr_size += input->pkt.size;
334
			input->curr_duration += input->pkt.duration;
335
			continue;
336
		}
337
338
		/* The change to avcodec_decode_audio2 occurred between
339
		 * 51.28.0 and 51.29.0 */
340
#if (LIBAVCODEC_VERSION_INT <= ((51<<16) + (28<<8) + 0))
341
		len = avcodec_decode_audio(cc, (int16_t *)output->buffer, &frame_size,
342
				input->curr_pkt_buf, input->curr_pkt_size);
343
		/* The change to avcodec_decode_audio3 occurred between
344
		 * 52.25.0 and 52.26.0 */
345
#elif (LIBAVCODEC_VERSION_INT <= ((52<<16) + (25<<8) + 0))
346
		len = avcodec_decode_audio2(cc, (int16_t *) output->buffer, &frame_size,
347
				input->curr_pkt_buf, input->curr_pkt_size);
348
#else
349
		{
350
			AVPacket avpkt;
351
			av_init_packet(&avpkt);
352
			avpkt.data = input->curr_pkt_buf;
353
			avpkt.size = input->curr_pkt_size;
354
			len = avcodec_decode_audio3(cc, (int16_t *) output->buffer, &frame_size, &avpkt);
355
			av_free_packet(&avpkt);
356
		}
357
#endif
358
		if (len < 0) {
359
			/* this is often reached when seeking, not sure why */
360
			input->curr_pkt_size = 0;
361
			continue;
362
		}
363
		input->curr_pkt_size -= len;
364
		input->curr_pkt_buf += len;
365
		if (frame_size > 0) {
366
			output->buffer_pos = output->buffer;
367
			output->buffer_used_len = frame_size;
368
			return frame_size;
369
		}
370
	}
371
	/* This should never get here. */
372
	return -IP_ERROR_INTERNAL;
373
}
374
375
static int ffmpeg_read(struct input_plugin_data *ip_data, char *buffer, int count)
376
{
377
	struct ffmpeg_private *priv = ip_data->private;
378
	struct ffmpeg_output *output = priv->output;
379
	int rc;
380
	int out_size;
381
382
	if (output->buffer_used_len == 0) {
383
		rc = ffmpeg_fill_buffer(priv->input_context, priv->codec_context, priv->input, priv->output);
384
		if (rc <= 0) {
385
			return rc;
386
		}
387
	}
388
	out_size = min(output->buffer_used_len, count);
389
	memcpy(buffer, output->buffer_pos, out_size);
390
	output->buffer_used_len -= out_size;
391
	output->buffer_pos += out_size;
392
	return out_size;
393
}
394
395
static int ffmpeg_seek(struct input_plugin_data *ip_data, double offset)
396
{
397
	struct ffmpeg_private *priv = ip_data->private;
398
	AVStream *st = priv->input_context->streams[priv->stream_index];
399
	int ret;
400
401
	/* There is a bug that was fixed in ffmpeg revision 5099 that affects seeking.
402
	 * Apparently, the stream's timebase was not used consistently in asf.c.
403
	 * Prior to 5099, ASF seeking assumed seconds as inputs.  There is a
404
	 * window of incompatibility, since avformat's version was not updated at
405
	 * the same time.  Instead, the transition to 50.3.0 occurred at
406
	 * revision 5028. */
407
#if (LIBAVFORMAT_VERSION_INT < ((50<<16)+(3<<8)+0))
408
	int64_t pts = (int64_t) offset;
409
#else
410
	int64_t pts = av_rescale_q(offset * AV_TIME_BASE, AV_TIME_BASE_Q, st->time_base);
411
#endif
412
413
	ret = av_seek_frame(priv->input_context, priv->stream_index, pts, 0);
414
415
	if (ret < 0) {
416
		return -IP_ERROR_FUNCTION_NOT_SUPPORTED;
417
	} else {
418
		ffmpeg_buffer_flush(priv->output);
419
		return 0;
420
	}
421
}
422
423
#if (LIBAVFORMAT_VERSION_INT < ((52<<16)+(31<<8)+0))
424
/* Return new i. */
425
static int set_comment(struct keyval *comment, int i, const char *key, const char *val)
426
{
427
	if (val[0] == 0) {
428
		return i;
429
	}
430
	comment[i].key = xstrdup(key);
431
	comment[i].val = xstrdup(val);
432
	return i + 1;
433
}
434
#endif
435
436
static int ffmpeg_read_comments(struct input_plugin_data *ip_data, struct keyval **comments)
437
{
438
	struct ffmpeg_private *priv = ip_data->private;
439
	AVFormatContext *ic = priv->input_context;
440
441
#if (LIBAVFORMAT_VERSION_INT < ((52<<16)+(31<<8)+0))
442
	char buff[16];
443
	int i = 0;
444
445
	*comments = keyvals_new(NUM_FFMPEG_KEYS);
446
447
	i = set_comment(*comments, i, "artist", ic->author);
448
	i = set_comment(*comments, i, "album", ic->album);
449
	i = set_comment(*comments, i, "title", ic->title);
450
	i = set_comment(*comments, i, "genre", ic->genre);
451
452
	if (ic->year != 0) {
453
		snprintf(buff, sizeof(buff), "%d", ic->year);
454
		i = set_comment(*comments, i, "date", buff);
455
	}
456
457
	if (ic->track != 0) {
458
		snprintf(buff, sizeof(buff), "%d", ic->track);
459
		i = set_comment(*comments, i, "tracknumber", buff);
460
	}
461
#else
462
	GROWING_KEYVALS(c);
463
	AVDictionaryEntry *tag = NULL;
464
 
465
	while ((tag = av_dict_get(ic->metadata, "", tag, AV_DICT_IGNORE_SUFFIX))) {
466
		if (tag && tag->value[0])
467
			comments_add_const(&c, tag->key, tag->value);
468
	}
469
470
	keyvals_terminate(&c);
471
	*comments = c.keyvals;
472
#endif
473
474
	return 0;
475
}
476
477
static int ffmpeg_duration(struct input_plugin_data *ip_data)
478
{
479
	struct ffmpeg_private *priv = ip_data->private;
480
	return priv->input_context->duration / AV_TIME_BASE;
481
}
482
483
static long ffmpeg_bitrate(struct input_plugin_data *ip_data)
484
{
485
	struct ffmpeg_private *priv = ip_data->private;
486
	long bitrate = priv->input_context->bit_rate;
487
	return bitrate ? bitrate : -IP_ERROR_FUNCTION_NOT_SUPPORTED;
488
}
489
490
static long ffmpeg_current_bitrate(struct input_plugin_data *ip_data)
491
{
492
	struct ffmpeg_private *priv = ip_data->private;
493
	AVStream *st = priv->input_context->streams[priv->stream_index];
494
	long bitrate = -1;
495
#if (LIBAVFORMAT_VERSION_INT > ((51<<16)+(43<<8)+0))
496
	/* ape codec returns silly numbers */
497
	if (priv->codec->id == CODEC_ID_APE)
498
		return -1;
499
#endif
500
	if (priv->input->curr_duration > 0) {
501
		double seconds = priv->input->curr_duration * av_q2d(st->time_base);
502
		bitrate = (8 * priv->input->curr_size) / seconds;
503
		priv->input->curr_size = 0;
504
		priv->input->curr_duration = 0;
505
	}
506
	return bitrate;
507
}
508
509
static char *ffmpeg_codec(struct input_plugin_data *ip_data)
510
{
511
	struct ffmpeg_private *priv = ip_data->private;
512
	return xstrdup(priv->codec->name);
513
}
514
515
#if (LIBAVCODEC_VERSION_INT < ((52<<16)+(104<<8)+0))
516
static const char *codec_profile_to_str(int profile)
517
{
518
#if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(41<<8)+0))
519
	switch (profile) {
520
	case FF_PROFILE_AAC_MAIN:	return "Main";
521
	case FF_PROFILE_AAC_LOW:	return "LC";
522
	case FF_PROFILE_AAC_SSR:	return "SSR";
523
	case FF_PROFILE_AAC_LTP:	return "LTP";
524
	}
525
#endif
526
	return NULL;
527
}
528
#endif
529
530
static char *ffmpeg_codec_profile(struct input_plugin_data *ip_data)
531
{
532
	struct ffmpeg_private *priv = ip_data->private;
533
	const char *profile;
534
535
#if (LIBAVCODEC_VERSION_INT < ((52<<16)+(104<<8)+0))
536
	profile = codec_profile_to_str(priv->codec_context->profile);
537
#else
538
	profile = av_get_profile_name(priv->codec, priv->codec_context->profile);
539
#endif
540
541
	return profile ? xstrdup(profile) : NULL;
542
}
543
544
const struct input_plugin_ops ip_ops = {
545
	.open = ffmpeg_open,
546
	.close = ffmpeg_close,
547
	.read = ffmpeg_read,
548
	.seek = ffmpeg_seek,
549
	.read_comments = ffmpeg_read_comments,
550
	.duration = ffmpeg_duration,
551
	.bitrate = ffmpeg_bitrate,
552
	.bitrate_current = ffmpeg_current_bitrate,
553
	.codec = ffmpeg_codec,
554
	.codec_profile = ffmpeg_codec_profile
555
};
556
557
const int ip_priority = 30;
558
const char *const ip_extensions[] = {
559
	"ac3", "aif", "aifc", "aiff", "ape", "au", "mka", "shn", "tta", "wma",
560
	/* also supported by other plugins */
561
	"aac", "fla", "flac", "m4a", "m4b", "mp+", "mp2", "mp3", "mp4", "mpc",
562
	"mpp", "ogg", "wav", "wv",
563
#ifdef USE_FALLBACK_IP
564
	"*",
565
#endif
566
	NULL
567
};
568
const char *const ip_mime_types[] = { NULL };
569
const char * const ip_options[] = { NULL };