2
* Copyright (C) 2003-2009 The Music Player Daemon Project
3
* http://www.musicpd.org
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.
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.
15
* You should have received a copy of the GNU General Public License along
16
* with this program; if not, write to the Free Software Foundation, Inc.,
17
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
#include "../decoder_api.h"
30
#include <sys/types.h>
34
#ifdef OLD_FFMPEG_INCLUDES
39
#include <libavcodec/avcodec.h>
40
#include <libavformat/avformat.h>
41
#include <libavformat/avio.h>
45
#define G_LOG_DOMAIN "ffmpeg"
47
struct ffmpeg_context {
49
AVFormatContext *format_context;
50
AVCodecContext *codec_context;
51
struct decoder *decoder;
52
struct input_stream *input;
56
struct mpd_ffmpeg_stream {
57
struct decoder *decoder;
58
struct input_stream *input;
61
unsigned char buffer[8192];
65
mpd_ffmpeg_stream_read(void *opaque, uint8_t *buf, int size)
67
struct mpd_ffmpeg_stream *stream = opaque;
69
return decoder_read(stream->decoder, stream->input,
74
mpd_ffmpeg_stream_seek(void *opaque, int64_t pos, int whence)
76
struct mpd_ffmpeg_stream *stream = opaque;
79
if (whence == AVSEEK_SIZE)
80
return stream->input->size;
82
ret = input_stream_seek(stream->input, pos, whence);
86
return stream->input->offset;
89
static struct mpd_ffmpeg_stream *
90
mpd_ffmpeg_stream_open(struct decoder *decoder, struct input_stream *input)
92
struct mpd_ffmpeg_stream *stream = g_new(struct mpd_ffmpeg_stream, 1);
93
stream->decoder = decoder;
94
stream->input = input;
95
stream->io = av_alloc_put_byte(stream->buffer, sizeof(stream->buffer),
97
mpd_ffmpeg_stream_read, NULL,
99
? mpd_ffmpeg_stream_seek : NULL);
100
if (stream->io == NULL) {
109
mpd_ffmpeg_stream_close(struct mpd_ffmpeg_stream *stream)
116
ffmpeg_init(G_GNUC_UNUSED const struct config_param *param)
123
ffmpeg_find_audio_stream(const AVFormatContext *format_context)
125
for (unsigned i = 0; i < format_context->nb_streams; ++i)
126
if (format_context->streams[i]->codec->codec_type ==
133
static AVInputFormat *
134
ffmpeg_probe(struct decoder *decoder, struct input_stream *is,
142
unsigned char *buffer = g_malloc(BUFFER_SIZE);
143
size_t nbytes = decoder_read(decoder, is, buffer, BUFFER_SIZE);
144
if (nbytes <= PADDING || !input_stream_seek(is, 0, SEEK_SET)) {
149
/* some ffmpeg parsers (e.g. ac3_parser.c) read a few bytes
150
beyond the declared buffer limit, which makes valgrind
151
angry; this workaround removes some padding from the buffer
161
AVInputFormat *format = av_probe_input_format(&avpd, true);
168
ffmpeg_helper(const char *uri,
169
struct decoder *decoder, struct input_stream *input,
170
bool (*callback)(struct ffmpeg_context *ctx),
171
struct ffmpeg_context *ctx)
173
AVInputFormat *input_format = ffmpeg_probe(decoder, input, uri);
174
if (input_format == NULL)
177
g_debug("detected input format '%s' (%s)",
178
input_format->name, input_format->long_name);
180
struct mpd_ffmpeg_stream *stream =
181
mpd_ffmpeg_stream_open(decoder, input);
182
if (stream == NULL) {
183
g_warning("Failed to open stream");
187
AVFormatContext *format_context;
188
AVCodecContext *codec_context;
193
//ffmpeg works with ours "fileops" helper
194
if (av_open_input_stream(&format_context, stream->io, uri,
195
input_format, NULL) != 0) {
196
g_warning("Open failed\n");
197
mpd_ffmpeg_stream_close(stream);
201
if (av_find_stream_info(format_context)<0) {
202
g_warning("Couldn't find stream info\n");
203
av_close_input_stream(format_context);
204
mpd_ffmpeg_stream_close(stream);
208
audio_stream = ffmpeg_find_audio_stream(format_context);
209
if (audio_stream == -1) {
210
g_warning("No audio stream inside\n");
211
av_close_input_stream(format_context);
212
mpd_ffmpeg_stream_close(stream);
216
codec_context = format_context->streams[audio_stream]->codec;
217
if (codec_context->codec_name[0] != 0)
218
g_debug("codec '%s'", codec_context->codec_name);
220
codec = avcodec_find_decoder(codec_context->codec_id);
223
g_warning("Unsupported audio codec\n");
224
av_close_input_stream(format_context);
225
mpd_ffmpeg_stream_close(stream);
229
if (avcodec_open(codec_context, codec)<0) {
230
g_warning("Could not open codec\n");
231
av_close_input_stream(format_context);
232
mpd_ffmpeg_stream_close(stream);
237
ctx->audio_stream = audio_stream;
238
ctx->format_context = format_context;
239
ctx->codec_context = codec_context;
245
avcodec_close(codec_context);
246
av_close_input_stream(format_context);
247
mpd_ffmpeg_stream_close(stream);
253
* On some platforms, libavcodec wants the output buffer aligned to 16
254
* bytes (because it uses SSE/Altivec internally). This function
255
* returns the aligned version of the specified buffer, and corrects
259
align16(void *p, size_t *length_p)
261
unsigned add = 16 - (size_t)p % 16;
264
return (char *)p + add;
267
static enum decoder_command
268
ffmpeg_send_packet(struct decoder *decoder, struct input_stream *is,
269
const AVPacket *packet,
270
AVCodecContext *codec_context,
271
const AVRational *time_base)
273
enum decoder_command cmd = DECODE_COMMAND_NONE;
275
uint8_t audio_buf[(AVCODEC_MAX_AUDIO_FRAME_SIZE * 3) / 2 + 16];
276
int16_t *aligned_buffer;
279
uint8_t *packet_data;
282
packet_data = packet->data;
283
packet_size = packet->size;
285
buffer_size = sizeof(audio_buf);
286
aligned_buffer = align16(audio_buf, &buffer_size);
288
while ((packet_size > 0) && (cmd == DECODE_COMMAND_NONE)) {
289
audio_size = buffer_size;
290
len = avcodec_decode_audio2(codec_context,
291
aligned_buffer, &audio_size,
292
packet_data, packet_size);
295
/* if error, we skip the frame */
296
g_message("decoding failed\n");
306
position = packet->pts != (int64_t)AV_NOPTS_VALUE
307
? av_rescale_q(packet->pts, *time_base,
311
cmd = decoder_data(decoder, is,
312
aligned_buffer, audio_size,
314
codec_context->bit_rate / 1000, NULL);
320
ffmpeg_decode_internal(struct ffmpeg_context *ctx)
322
struct decoder *decoder = ctx->decoder;
323
AVCodecContext *codec_context = ctx->codec_context;
324
AVFormatContext *format_context = ctx->format_context;
326
struct audio_format audio_format;
327
enum decoder_command cmd;
332
#if LIBAVCODEC_VERSION_INT >= ((51<<16)+(41<<8)+0)
333
audio_format.bits = (uint8_t) av_get_bits_per_sample_format(codec_context->sample_fmt);
335
/* XXX fixme 16-bit for older ffmpeg (13 Aug 2007) */
336
audio_format.bits = (uint8_t) 16;
338
audio_format.sample_rate = (unsigned int)codec_context->sample_rate;
339
audio_format.channels = codec_context->channels;
341
if (!audio_format_valid(&audio_format)) {
342
g_warning("Invalid audio format: %u:%u:%u\n",
343
audio_format.sample_rate, audio_format.bits,
344
audio_format.channels);
348
//there is some problem with this on some demux (mp3 at least)
349
if (format_context->duration != (int64_t)AV_NOPTS_VALUE) {
350
total_time = format_context->duration / AV_TIME_BASE;
353
decoder_initialized(decoder, &audio_format,
354
ctx->input->seekable, total_time);
357
if (av_read_frame(format_context, &packet) < 0)
361
if (packet.stream_index == ctx->audio_stream)
362
cmd = ffmpeg_send_packet(decoder, ctx->input,
363
&packet, codec_context,
364
&format_context->streams[ctx->audio_stream]->time_base);
366
cmd = decoder_get_command(decoder);
368
av_free_packet(&packet);
370
if (cmd == DECODE_COMMAND_SEEK) {
372
decoder_seek_where(decoder) * AV_TIME_BASE;
374
if (av_seek_frame(format_context, -1, where, 0) < 0)
375
decoder_seek_error(decoder);
377
decoder_command_finished(decoder);
379
} while (cmd != DECODE_COMMAND_STOP);
385
ffmpeg_decode(struct decoder *decoder, struct input_stream *input)
387
struct ffmpeg_context ctx;
390
ctx.decoder = decoder;
392
char *uri = decoder_get_uri(decoder);
393
ffmpeg_helper(uri, decoder, input,
394
ffmpeg_decode_internal, &ctx);
398
#if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(31<<8)+0)
400
ffmpeg_copy_metadata(struct tag *tag, AVMetadata *m,
401
enum tag_type type, const char *name)
403
AVMetadataTag *mt = av_metadata_get(m, name, NULL, 0);
405
tag_add_item(tag, type, mt->value);
410
static bool ffmpeg_tag_internal(struct ffmpeg_context *ctx)
412
struct tag *tag = (struct tag *) ctx->tag;
413
AVFormatContext *f = ctx->format_context;
416
if (f->duration != (int64_t)AV_NOPTS_VALUE)
417
tag->time = f->duration / AV_TIME_BASE;
419
#if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(31<<8)+0)
420
av_metadata_conv(f, NULL, f->iformat->metadata_conv);
422
ffmpeg_copy_metadata(tag, f->metadata, TAG_ITEM_TITLE, "title");
423
#if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(50<<8))
424
ffmpeg_copy_metadata(tag, f->metadata, TAG_ITEM_ARTIST, "artist");
425
ffmpeg_copy_metadata(tag, f->metadata, TAG_ITEM_DATE, "date");
427
ffmpeg_copy_metadata(tag, f->metadata, TAG_ITEM_ARTIST, "author");
428
ffmpeg_copy_metadata(tag, f->metadata, TAG_ITEM_DATE, "year");
430
ffmpeg_copy_metadata(tag, f->metadata, TAG_ITEM_ALBUM, "album");
431
ffmpeg_copy_metadata(tag, f->metadata, TAG_ITEM_COMMENT, "comment");
432
ffmpeg_copy_metadata(tag, f->metadata, TAG_ITEM_GENRE, "genre");
433
ffmpeg_copy_metadata(tag, f->metadata, TAG_ITEM_TRACK, "track");
434
ffmpeg_copy_metadata(tag, f->metadata, TAG_ITEM_ALBUM_ARTIST, "album_artist");
435
ffmpeg_copy_metadata(tag, f->metadata, TAG_ITEM_COMPOSER, "composer");
436
ffmpeg_copy_metadata(tag, f->metadata, TAG_ITEM_PERFORMER, "performer");
437
ffmpeg_copy_metadata(tag, f->metadata, TAG_ITEM_DISC, "disc");
440
tag_add_item(tag, TAG_ITEM_ARTIST, f->author);
442
tag_add_item(tag, TAG_ITEM_TITLE, f->title);
444
tag_add_item(tag, TAG_ITEM_ALBUM, f->album);
448
snprintf(buffer, sizeof(buffer), "%d", f->track);
449
tag_add_item(tag, TAG_ITEM_TRACK, buffer);
453
tag_add_item(tag, TAG_ITEM_COMMENT, f->comment);
455
tag_add_item(tag, TAG_ITEM_GENRE, f->genre);
458
snprintf(buffer, sizeof(buffer), "%d", f->year);
459
tag_add_item(tag, TAG_ITEM_DATE, buffer);
466
//no tag reading in ffmpeg, check if playable
467
static struct tag *ffmpeg_tag(const char *file)
469
struct input_stream input;
470
struct ffmpeg_context ctx;
473
if (!input_stream_open(&input, file)) {
474
g_warning("failed to open %s\n", file);
481
ret = ffmpeg_helper(file, NULL, &input, ffmpeg_tag_internal, &ctx);
487
input_stream_close(&input);
493
* A list of extensions found for the formats supported by ffmpeg.
494
* This list is current as of 02-23-09; To find out if there are more
495
* supported formats, check the ffmpeg changelog since this date for
498
static const char *const ffmpeg_suffixes[] = {
499
"16sv", "3g2", "3gp", "4xm", "8svx", "aa3", "aac", "ac3", "afc", "aif",
500
"aifc", "aiff", "al", "alaw", "amr", "anim", "apc", "ape", "asf",
501
"atrac", "au", "aud", "avi", "avm2", "avs", "bap", "bfi", "c93", "cak",
502
"cin", "cmv", "cpk", "daud", "dct", "divx", "dts", "dv", "dvd", "dxa",
503
"eac3", "film", "flac", "flc", "fli", "fll", "flx", "flv", "g726",
504
"gsm", "gxf", "iss", "m1v", "m2v", "m2t", "m2ts",
507
"mj2", "mjpeg", "mjpg", "mka", "mkv", "mlp", "mm", "mmf", "mov", "mp+",
508
"mp1", "mp2", "mp3", "mp4", "mpc", "mpeg", "mpg", "mpga", "mpp", "mpu",
509
"mve", "mvi", "mxf", "nc", "nsv", "nut", "nuv", "oga", "ogm", "ogv",
510
"ogx", "oma", "ogg", "omg", "psp", "pva", "qcp", "qt", "r3d", "ra",
511
"ram", "rl2", "rm", "rmvb", "roq", "rpl", "rvc", "shn", "smk", "snd",
512
"sol", "son", "spx", "str", "swf", "tgi", "tgq", "tgv", "thp", "ts",
513
"tsp", "tta", "xa", "xvid", "uv", "uv2", "vb", "vid", "vob", "voc",
514
"vp6", "vmd", "wav", "wma", "wmv", "wsaud", "wsvga", "wv", "wve",
518
static const char *const ffmpeg_mime_types[] = {
521
"application/octet-stream",
523
"application/x-ms-wmz",
524
"application/x-ms-wmd",
526
"application/x-shockwave-flash",
527
"application/x-shorten",
557
"audio/x-monkeys-audio",
564
"audio/x-vorbis+ogg",
565
"audio/x-pn-realaudio",
566
"audio/x-pn-multirate-realaudio",
598
const struct decoder_plugin ffmpeg_decoder_plugin = {
601
.stream_decode = ffmpeg_decode,
602
.tag_dup = ffmpeg_tag,
603
.suffixes = ffmpeg_suffixes,
604
.mime_types = ffmpeg_mime_types