2009-03-13 18:43:16 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2003-2009 The Music Player Daemon Project
|
|
|
|
* http://www.musicpd.org
|
2008-10-17 22:27:33 +02:00
|
|
|
*
|
|
|
|
* 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.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* 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.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2008-10-17 22:27:33 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "../decoder_api.h"
|
2009-01-08 21:37:02 +01:00
|
|
|
#include "config.h"
|
2008-10-17 22:27:33 +02:00
|
|
|
|
2009-01-01 18:09:24 +01:00
|
|
|
#include <glib.h>
|
|
|
|
|
2008-10-30 19:03:38 +01:00
|
|
|
#include <assert.h>
|
2008-10-17 22:27:33 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2008-10-21 08:34:19 +02:00
|
|
|
#ifdef OLD_FFMPEG_INCLUDES
|
2008-10-17 22:27:33 +02:00
|
|
|
#include <avcodec.h>
|
|
|
|
#include <avformat.h>
|
|
|
|
#include <avio.h>
|
2008-10-21 08:34:19 +02:00
|
|
|
#else
|
|
|
|
#include <libavcodec/avcodec.h>
|
|
|
|
#include <libavformat/avformat.h>
|
|
|
|
#include <libavformat/avio.h>
|
|
|
|
#endif
|
2008-10-17 22:27:33 +02:00
|
|
|
|
2008-11-21 20:13:41 +01:00
|
|
|
#undef G_LOG_DOMAIN
|
|
|
|
#define G_LOG_DOMAIN "ffmpeg"
|
|
|
|
|
2008-11-04 16:55:11 +01:00
|
|
|
struct ffmpeg_context {
|
|
|
|
int audio_stream;
|
|
|
|
AVFormatContext *format_context;
|
|
|
|
AVCodecContext *codec_context;
|
2008-10-17 22:27:33 +02:00
|
|
|
struct decoder *decoder;
|
2008-10-26 19:54:57 +01:00
|
|
|
struct input_stream *input;
|
2008-10-17 22:27:33 +02:00
|
|
|
struct tag *tag;
|
2008-11-04 16:55:11 +01:00
|
|
|
};
|
2008-10-17 22:27:33 +02:00
|
|
|
|
2008-11-04 16:55:11 +01:00
|
|
|
struct ffmpeg_stream {
|
|
|
|
/** hack - see url_to_struct() */
|
2008-10-21 18:12:56 +02:00
|
|
|
char url[8];
|
|
|
|
|
2008-10-17 22:27:33 +02:00
|
|
|
struct decoder *decoder;
|
2008-10-26 19:54:57 +01:00
|
|
|
struct input_stream *input;
|
2008-11-04 16:55:11 +01:00
|
|
|
};
|
2008-10-17 22:27:33 +02:00
|
|
|
|
2008-10-21 18:12:56 +02:00
|
|
|
/**
|
2008-11-04 16:55:11 +01:00
|
|
|
* Convert a faked mpd:// URL to a ffmpeg_stream structure. This is a
|
2008-10-21 18:12:56 +02:00
|
|
|
* hack because ffmpeg does not provide a nice API for passing a
|
|
|
|
* user-defined pointer to mpdurl_open().
|
|
|
|
*/
|
2008-11-04 16:55:11 +01:00
|
|
|
static struct ffmpeg_stream *url_to_struct(const char *url)
|
2008-10-17 22:27:33 +02:00
|
|
|
{
|
2008-10-21 18:12:56 +02:00
|
|
|
union {
|
|
|
|
const char *in;
|
2008-11-04 16:55:11 +01:00
|
|
|
struct ffmpeg_stream *out;
|
2008-10-21 18:12:56 +02:00
|
|
|
} u = { .in = url };
|
|
|
|
return u.out;
|
|
|
|
}
|
|
|
|
|
2008-11-04 16:55:11 +01:00
|
|
|
static int mpd_ffmpeg_open(URLContext *h, const char *filename,
|
2009-01-01 18:09:24 +01:00
|
|
|
G_GNUC_UNUSED int flags)
|
2008-10-21 18:12:56 +02:00
|
|
|
{
|
2008-11-04 16:55:11 +01:00
|
|
|
struct ffmpeg_stream *stream = url_to_struct(filename);
|
|
|
|
h->priv_data = stream;
|
|
|
|
h->is_streamed = stream->input->seekable ? 0 : 1;
|
2008-10-21 18:12:56 +02:00
|
|
|
return 0;
|
2008-10-17 22:27:33 +02:00
|
|
|
}
|
|
|
|
|
2008-11-04 16:55:11 +01:00
|
|
|
static int mpd_ffmpeg_read(URLContext *h, unsigned char *buf, int size)
|
2008-10-17 22:27:33 +02:00
|
|
|
{
|
2008-11-04 16:55:11 +01:00
|
|
|
struct ffmpeg_stream *stream = (struct ffmpeg_stream *) h->priv_data;
|
2008-10-30 19:02:01 +01:00
|
|
|
|
2008-11-04 16:55:12 +01:00
|
|
|
return decoder_read(stream->decoder, stream->input,
|
|
|
|
(void *)buf, size);
|
2008-10-17 22:27:33 +02:00
|
|
|
}
|
|
|
|
|
2008-11-04 16:55:11 +01:00
|
|
|
static int64_t mpd_ffmpeg_seek(URLContext *h, int64_t pos, int whence)
|
2008-10-17 22:27:33 +02:00
|
|
|
{
|
2008-11-04 16:55:11 +01:00
|
|
|
struct ffmpeg_stream *stream = (struct ffmpeg_stream *) h->priv_data;
|
2008-11-16 20:25:31 +01:00
|
|
|
bool ret;
|
|
|
|
|
|
|
|
if (whence == AVSEEK_SIZE)
|
|
|
|
return stream->input->size;
|
|
|
|
|
|
|
|
ret = input_stream_seek(stream->input, pos, whence);
|
|
|
|
if (!ret)
|
|
|
|
return -1;
|
|
|
|
|
2008-11-04 16:55:11 +01:00
|
|
|
return stream->input->offset;
|
2008-10-17 22:27:33 +02:00
|
|
|
}
|
|
|
|
|
2008-11-04 16:55:11 +01:00
|
|
|
static int mpd_ffmpeg_close(URLContext *h)
|
2008-10-17 22:27:33 +02:00
|
|
|
{
|
2008-12-24 11:49:37 +01:00
|
|
|
h->priv_data = NULL;
|
2008-10-17 22:27:33 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-11-04 16:55:11 +01:00
|
|
|
static URLProtocol mpd_ffmpeg_fileops = {
|
2008-10-18 08:08:13 +02:00
|
|
|
.name = "mpd",
|
2008-11-04 16:55:11 +01:00
|
|
|
.url_open = mpd_ffmpeg_open,
|
|
|
|
.url_read = mpd_ffmpeg_read,
|
|
|
|
.url_seek = mpd_ffmpeg_seek,
|
|
|
|
.url_close = mpd_ffmpeg_close,
|
2008-10-18 08:08:13 +02:00
|
|
|
};
|
|
|
|
|
2009-02-15 18:34:14 +01:00
|
|
|
static bool
|
|
|
|
ffmpeg_init(G_GNUC_UNUSED const struct config_param *param)
|
2008-10-17 22:27:33 +02:00
|
|
|
{
|
|
|
|
av_register_all();
|
2008-11-04 16:55:11 +01:00
|
|
|
register_protocol(&mpd_ffmpeg_fileops);
|
2008-10-30 08:38:54 +01:00
|
|
|
return true;
|
2008-10-17 22:27:33 +02:00
|
|
|
}
|
|
|
|
|
2008-11-04 16:55:12 +01:00
|
|
|
static int
|
|
|
|
ffmpeg_find_audio_stream(const AVFormatContext *format_context)
|
|
|
|
{
|
|
|
|
for (unsigned i = 0; i < format_context->nb_streams; ++i)
|
|
|
|
if (format_context->streams[i]->codec->codec_type ==
|
|
|
|
CODEC_TYPE_AUDIO)
|
|
|
|
return i;
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-10-30 19:01:23 +01:00
|
|
|
static bool
|
2008-11-04 16:55:11 +01:00
|
|
|
ffmpeg_helper(struct input_stream *input,
|
|
|
|
bool (*callback)(struct ffmpeg_context *ctx),
|
|
|
|
struct ffmpeg_context *ctx)
|
2008-10-17 22:27:33 +02:00
|
|
|
{
|
2008-11-04 16:55:11 +01:00
|
|
|
AVFormatContext *format_context;
|
|
|
|
AVCodecContext *codec_context;
|
|
|
|
AVCodec *codec;
|
|
|
|
int audio_stream;
|
|
|
|
struct ffmpeg_stream stream = {
|
2008-10-21 18:12:56 +02:00
|
|
|
.url = "mpd://X", /* only the mpd:// prefix matters */
|
|
|
|
};
|
2008-10-30 19:01:23 +01:00
|
|
|
bool ret;
|
2008-10-17 22:27:33 +02:00
|
|
|
|
2008-11-04 16:55:11 +01:00
|
|
|
stream.input = input;
|
|
|
|
if (ctx && ctx->decoder) {
|
|
|
|
stream.decoder = ctx->decoder; //are we in decoding loop ?
|
2008-10-17 22:27:33 +02:00
|
|
|
} else {
|
2008-11-04 16:55:11 +01:00
|
|
|
stream.decoder = NULL;
|
2008-10-17 22:27:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//ffmpeg works with ours "fileops" helper
|
2008-11-04 16:55:11 +01:00
|
|
|
if (av_open_input_file(&format_context, stream.url, NULL, 0, NULL) != 0) {
|
2008-11-21 20:13:41 +01:00
|
|
|
g_warning("Open failed\n");
|
2008-10-30 19:01:23 +01:00
|
|
|
return false;
|
2008-10-17 22:27:33 +02:00
|
|
|
}
|
|
|
|
|
2008-11-04 16:55:11 +01:00
|
|
|
if (av_find_stream_info(format_context)<0) {
|
2008-11-21 20:13:41 +01:00
|
|
|
g_warning("Couldn't find stream info\n");
|
2008-10-30 19:01:23 +01:00
|
|
|
return false;
|
2008-10-17 22:27:33 +02:00
|
|
|
}
|
|
|
|
|
2008-11-04 16:55:12 +01:00
|
|
|
audio_stream = ffmpeg_find_audio_stream(format_context);
|
2008-11-04 16:55:11 +01:00
|
|
|
if (audio_stream == -1) {
|
2008-11-21 20:13:41 +01:00
|
|
|
g_warning("No audio stream inside\n");
|
2008-10-30 19:01:23 +01:00
|
|
|
return false;
|
2008-10-17 22:27:33 +02:00
|
|
|
}
|
|
|
|
|
2008-11-04 16:55:11 +01:00
|
|
|
codec_context = format_context->streams[audio_stream]->codec;
|
2009-02-03 21:44:02 +01:00
|
|
|
if (codec_context->codec_name[0] != 0)
|
|
|
|
g_debug("codec '%s'", codec_context->codec_name);
|
|
|
|
|
2008-11-04 16:55:11 +01:00
|
|
|
codec = avcodec_find_decoder(codec_context->codec_id);
|
2008-10-17 22:27:33 +02:00
|
|
|
|
2008-11-04 16:55:11 +01:00
|
|
|
if (!codec) {
|
2008-11-21 20:13:41 +01:00
|
|
|
g_warning("Unsupported audio codec\n");
|
2008-10-30 19:01:23 +01:00
|
|
|
return false;
|
2008-10-17 22:27:33 +02:00
|
|
|
}
|
|
|
|
|
2008-11-04 16:55:11 +01:00
|
|
|
if (avcodec_open(codec_context, codec)<0) {
|
2008-11-21 20:13:41 +01:00
|
|
|
g_warning("Could not open codec\n");
|
2008-10-30 19:01:23 +01:00
|
|
|
return false;
|
2008-10-17 22:27:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (callback) {
|
2008-11-04 16:55:11 +01:00
|
|
|
ctx->audio_stream = audio_stream;
|
|
|
|
ctx->format_context = format_context;
|
|
|
|
ctx->codec_context = codec_context;
|
2008-10-17 22:27:33 +02:00
|
|
|
|
2008-11-04 16:55:11 +01:00
|
|
|
ret = callback(ctx);
|
2008-10-30 19:01:18 +01:00
|
|
|
} else
|
2008-10-30 19:01:23 +01:00
|
|
|
ret = true;
|
2008-10-17 22:27:33 +02:00
|
|
|
|
2008-11-04 16:55:11 +01:00
|
|
|
avcodec_close(codec_context);
|
|
|
|
av_close_input_file(format_context);
|
2008-10-17 22:27:33 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-10-30 19:03:41 +01:00
|
|
|
static enum decoder_command
|
2008-10-30 19:09:20 +01:00
|
|
|
ffmpeg_send_packet(struct decoder *decoder, struct input_stream *is,
|
|
|
|
const AVPacket *packet,
|
2008-10-30 19:03:20 +01:00
|
|
|
AVCodecContext *codec_context,
|
|
|
|
const AVRational *time_base)
|
|
|
|
{
|
2008-12-15 18:35:34 +01:00
|
|
|
enum decoder_command cmd = DECODE_COMMAND_NONE;
|
2008-10-30 19:03:20 +01:00
|
|
|
int position;
|
|
|
|
uint8_t audio_buf[(AVCODEC_MAX_AUDIO_FRAME_SIZE * 3) / 2];
|
|
|
|
int len, audio_size;
|
2008-12-15 18:35:34 +01:00
|
|
|
uint8_t *packet_data;
|
|
|
|
int packet_size;
|
|
|
|
|
|
|
|
packet_data = packet->data;
|
|
|
|
packet_size = packet->size;
|
|
|
|
|
|
|
|
while ((packet_size > 0) && (cmd == DECODE_COMMAND_NONE)) {
|
|
|
|
audio_size = sizeof(audio_buf);
|
|
|
|
len = avcodec_decode_audio2(codec_context,
|
|
|
|
(int16_t *)audio_buf,
|
|
|
|
&audio_size,
|
|
|
|
packet_data, packet_size);
|
|
|
|
|
|
|
|
if (len < 0) {
|
|
|
|
/* if error, we skip the frame */
|
|
|
|
g_message("decoding failed\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
packet_data += len;
|
|
|
|
packet_size -= len;
|
|
|
|
|
2009-02-03 21:44:14 +01:00
|
|
|
if (audio_size <= 0)
|
2008-12-15 18:35:34 +01:00
|
|
|
continue;
|
2009-02-03 21:44:14 +01:00
|
|
|
|
2009-02-03 21:55:28 +01:00
|
|
|
position = packet->pts != (int64_t)AV_NOPTS_VALUE
|
|
|
|
? av_rescale_q(packet->pts, *time_base,
|
|
|
|
(AVRational){1, 1})
|
|
|
|
: 0;
|
|
|
|
|
2008-12-15 18:35:34 +01:00
|
|
|
cmd = decoder_data(decoder, is,
|
|
|
|
audio_buf, audio_size,
|
|
|
|
position,
|
|
|
|
codec_context->bit_rate / 1000, NULL);
|
2008-10-30 19:03:20 +01:00
|
|
|
}
|
2008-12-15 18:35:34 +01:00
|
|
|
return cmd;
|
2008-10-30 19:03:20 +01:00
|
|
|
}
|
|
|
|
|
2008-10-30 19:01:23 +01:00
|
|
|
static bool
|
2008-11-04 16:55:11 +01:00
|
|
|
ffmpeg_decode_internal(struct ffmpeg_context *ctx)
|
2008-10-17 22:27:33 +02:00
|
|
|
{
|
2008-11-04 16:55:11 +01:00
|
|
|
struct decoder *decoder = ctx->decoder;
|
|
|
|
AVCodecContext *codec_context = ctx->codec_context;
|
|
|
|
AVFormatContext *format_context = ctx->format_context;
|
2008-10-17 22:27:33 +02:00
|
|
|
AVPacket packet;
|
|
|
|
struct audio_format audio_format;
|
2008-10-30 19:03:41 +01:00
|
|
|
enum decoder_command cmd;
|
2009-02-03 22:46:00 +01:00
|
|
|
int total_time;
|
2009-07-19 17:24:43 +02:00
|
|
|
uint8_t bits;
|
2008-10-17 22:27:33 +02:00
|
|
|
|
|
|
|
total_time = 0;
|
|
|
|
|
2008-11-04 16:55:11 +01:00
|
|
|
if (codec_context->channels > 2) {
|
|
|
|
codec_context->channels = 2;
|
2008-10-17 22:27:33 +02:00
|
|
|
}
|
|
|
|
|
2009-03-03 19:02:20 +01:00
|
|
|
#if LIBAVCODEC_VERSION_INT >= ((51<<16)+(41<<8)+0)
|
2009-07-19 17:24:43 +02:00
|
|
|
bits = (uint8_t) av_get_bits_per_sample_format(codec_context->sample_fmt);
|
2009-03-02 20:07:19 +01:00
|
|
|
#else
|
|
|
|
/* XXX fixme 16-bit for older ffmpeg (13 Aug 2007) */
|
2009-07-19 17:24:43 +02:00
|
|
|
bits = (uint8_t) 16;
|
2009-03-02 20:07:19 +01:00
|
|
|
#endif
|
2009-07-19 17:24:43 +02:00
|
|
|
audio_format_init(&audio_format, codec_context->sample_rate, bits,
|
|
|
|
codec_context->channels);
|
2008-10-17 22:27:33 +02:00
|
|
|
|
2008-11-21 20:27:30 +01:00
|
|
|
if (!audio_format_valid(&audio_format)) {
|
|
|
|
g_warning("Invalid audio format: %u:%u:%u\n",
|
|
|
|
audio_format.sample_rate, audio_format.bits,
|
|
|
|
audio_format.channels);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-10-17 22:27:33 +02:00
|
|
|
//there is some problem with this on some demux (mp3 at least)
|
2008-12-24 11:56:53 +01:00
|
|
|
if (format_context->duration != (int64_t)AV_NOPTS_VALUE) {
|
2008-11-04 16:55:11 +01:00
|
|
|
total_time = format_context->duration / AV_TIME_BASE;
|
2008-10-17 22:27:33 +02:00
|
|
|
}
|
|
|
|
|
2008-11-02 17:01:51 +01:00
|
|
|
decoder_initialized(decoder, &audio_format,
|
2008-11-04 16:55:11 +01:00
|
|
|
ctx->input->seekable, total_time);
|
2008-10-17 22:27:33 +02:00
|
|
|
|
|
|
|
do {
|
2008-11-04 16:55:11 +01:00
|
|
|
if (av_read_frame(format_context, &packet) < 0)
|
2008-10-30 19:03:41 +01:00
|
|
|
/* end of file */
|
|
|
|
break;
|
|
|
|
|
2008-11-04 16:55:11 +01:00
|
|
|
if (packet.stream_index == ctx->audio_stream)
|
|
|
|
cmd = ffmpeg_send_packet(decoder, ctx->input,
|
|
|
|
&packet, codec_context,
|
|
|
|
&format_context->streams[ctx->audio_stream]->time_base);
|
2008-10-30 19:03:41 +01:00
|
|
|
else
|
|
|
|
cmd = decoder_get_command(decoder);
|
|
|
|
|
|
|
|
av_free_packet(&packet);
|
2008-10-17 22:27:33 +02:00
|
|
|
|
2008-10-30 19:03:41 +01:00
|
|
|
if (cmd == DECODE_COMMAND_SEEK) {
|
2009-02-03 22:46:00 +01:00
|
|
|
int64_t where =
|
|
|
|
decoder_seek_where(decoder) * AV_TIME_BASE;
|
2008-10-17 22:27:33 +02:00
|
|
|
|
2009-02-03 22:46:00 +01:00
|
|
|
if (av_seek_frame(format_context, -1, where, 0) < 0)
|
2008-10-30 19:02:38 +01:00
|
|
|
decoder_seek_error(decoder);
|
|
|
|
else
|
|
|
|
decoder_command_finished(decoder);
|
2008-10-17 22:27:33 +02:00
|
|
|
}
|
2008-10-30 19:03:41 +01:00
|
|
|
} while (cmd != DECODE_COMMAND_STOP);
|
2008-10-17 22:27:33 +02:00
|
|
|
|
2008-10-30 19:01:23 +01:00
|
|
|
return true;
|
2008-10-17 22:27:33 +02:00
|
|
|
}
|
|
|
|
|
2008-11-11 17:13:44 +01:00
|
|
|
static void
|
2008-10-26 19:54:57 +01:00
|
|
|
ffmpeg_decode(struct decoder *decoder, struct input_stream *input)
|
2008-10-17 22:27:33 +02:00
|
|
|
{
|
2008-11-04 16:55:11 +01:00
|
|
|
struct ffmpeg_context ctx;
|
2008-10-17 22:27:33 +02:00
|
|
|
|
2008-11-04 16:55:11 +01:00
|
|
|
ctx.input = input;
|
|
|
|
ctx.decoder = decoder;
|
2008-10-17 22:27:33 +02:00
|
|
|
|
2008-11-11 17:13:44 +01:00
|
|
|
ffmpeg_helper(input, ffmpeg_decode_internal, &ctx);
|
2008-10-17 22:27:33 +02:00
|
|
|
}
|
|
|
|
|
2009-06-08 08:45:54 +02:00
|
|
|
#if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(31<<8)+0)
|
2009-09-30 15:41:43 +02:00
|
|
|
static bool
|
2009-06-08 08:45:54 +02:00
|
|
|
ffmpeg_copy_metadata(struct tag *tag, AVMetadata *m,
|
|
|
|
enum tag_type type, const char *name)
|
|
|
|
{
|
2009-06-25 08:43:59 +02:00
|
|
|
AVMetadataTag *mt = NULL;
|
|
|
|
|
|
|
|
while ((mt = av_metadata_get(m, name, mt, 0)) != NULL)
|
2009-06-08 08:45:54 +02:00
|
|
|
tag_add_item(tag, type, mt->value);
|
2009-09-30 15:41:43 +02:00
|
|
|
return mt != NULL;
|
2009-06-08 08:45:54 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-11-04 16:55:11 +01:00
|
|
|
static bool ffmpeg_tag_internal(struct ffmpeg_context *ctx)
|
2008-10-17 22:27:33 +02:00
|
|
|
{
|
2008-11-04 16:55:11 +01:00
|
|
|
struct tag *tag = (struct tag *) ctx->tag;
|
2009-08-03 17:40:22 +02:00
|
|
|
AVFormatContext *f = ctx->format_context;
|
2008-11-18 20:03:00 +01:00
|
|
|
|
|
|
|
tag->time = 0;
|
2008-12-24 11:56:53 +01:00
|
|
|
if (f->duration != (int64_t)AV_NOPTS_VALUE)
|
2008-11-18 20:03:00 +01:00
|
|
|
tag->time = f->duration / AV_TIME_BASE;
|
2009-03-03 18:58:54 +01:00
|
|
|
|
|
|
|
#if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(31<<8)+0)
|
2009-08-03 17:40:22 +02:00
|
|
|
av_metadata_conv(f, NULL, f->iformat->metadata_conv);
|
|
|
|
|
2009-06-08 08:45:54 +02:00
|
|
|
ffmpeg_copy_metadata(tag, f->metadata, TAG_ITEM_TITLE, "title");
|
2009-09-30 15:41:43 +02:00
|
|
|
if (!ffmpeg_copy_metadata(tag, f->metadata, TAG_ITEM_ARTIST, "author"))
|
|
|
|
ffmpeg_copy_metadata(tag, f->metadata,
|
|
|
|
TAG_ITEM_ARTIST, "artist");
|
2009-06-08 08:45:54 +02:00
|
|
|
ffmpeg_copy_metadata(tag, f->metadata, TAG_ITEM_ALBUM, "album");
|
|
|
|
ffmpeg_copy_metadata(tag, f->metadata, TAG_ITEM_COMMENT, "comment");
|
|
|
|
ffmpeg_copy_metadata(tag, f->metadata, TAG_ITEM_GENRE, "genre");
|
|
|
|
ffmpeg_copy_metadata(tag, f->metadata, TAG_ITEM_TRACK, "track");
|
|
|
|
ffmpeg_copy_metadata(tag, f->metadata, TAG_ITEM_DATE, "year");
|
2009-03-03 18:58:54 +01:00
|
|
|
#else
|
2008-11-18 20:03:00 +01:00
|
|
|
if (f->author[0])
|
|
|
|
tag_add_item(tag, TAG_ITEM_ARTIST, f->author);
|
|
|
|
if (f->title[0])
|
|
|
|
tag_add_item(tag, TAG_ITEM_TITLE, f->title);
|
|
|
|
if (f->album[0])
|
|
|
|
tag_add_item(tag, TAG_ITEM_ALBUM, f->album);
|
|
|
|
|
|
|
|
if (f->track > 0) {
|
|
|
|
char buffer[16];
|
|
|
|
snprintf(buffer, sizeof(buffer), "%d", f->track);
|
|
|
|
tag_add_item(tag, TAG_ITEM_TRACK, buffer);
|
2008-10-17 22:27:33 +02:00
|
|
|
}
|
2008-10-30 19:01:23 +01:00
|
|
|
|
2009-01-30 09:02:09 +01:00
|
|
|
if (f->comment[0])
|
|
|
|
tag_add_item(tag, TAG_ITEM_COMMENT, f->comment);
|
|
|
|
if (f->genre[0])
|
|
|
|
tag_add_item(tag, TAG_ITEM_GENRE, f->genre);
|
|
|
|
if (f->year > 0) {
|
|
|
|
char buffer[16];
|
|
|
|
snprintf(buffer, sizeof(buffer), "%d", f->year);
|
|
|
|
tag_add_item(tag, TAG_ITEM_DATE, buffer);
|
|
|
|
}
|
|
|
|
|
2009-03-03 18:58:54 +01:00
|
|
|
#endif
|
2008-10-30 19:01:23 +01:00
|
|
|
return true;
|
2008-10-17 22:27:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//no tag reading in ffmpeg, check if playable
|
2008-10-31 15:56:43 +01:00
|
|
|
static struct tag *ffmpeg_tag(const char *file)
|
2008-10-17 22:27:33 +02:00
|
|
|
{
|
2008-10-26 19:54:57 +01:00
|
|
|
struct input_stream input;
|
2008-11-04 16:55:11 +01:00
|
|
|
struct ffmpeg_context ctx;
|
2008-10-30 19:01:23 +01:00
|
|
|
bool ret;
|
2008-10-17 22:27:33 +02:00
|
|
|
|
2008-10-26 20:56:46 +01:00
|
|
|
if (!input_stream_open(&input, file)) {
|
2008-11-21 20:13:41 +01:00
|
|
|
g_warning("failed to open %s\n", file);
|
2008-10-17 22:27:33 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-11-04 16:55:11 +01:00
|
|
|
ctx.decoder = NULL;
|
|
|
|
ctx.tag = tag_new();
|
2008-10-17 22:27:33 +02:00
|
|
|
|
2008-11-04 16:55:11 +01:00
|
|
|
ret = ffmpeg_helper(&input, ffmpeg_tag_internal, &ctx);
|
2008-11-02 17:32:40 +01:00
|
|
|
if (!ret) {
|
2008-11-04 16:55:11 +01:00
|
|
|
tag_free(ctx.tag);
|
|
|
|
ctx.tag = NULL;
|
2008-10-17 22:27:33 +02:00
|
|
|
}
|
|
|
|
|
2008-10-26 20:34:47 +01:00
|
|
|
input_stream_close(&input);
|
2008-10-17 22:27:33 +02:00
|
|
|
|
2008-11-04 16:55:11 +01:00
|
|
|
return ctx.tag;
|
2008-10-17 22:27:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-02-23 19:48:43 +01:00
|
|
|
* A list of extensions found for the formats supported by ffmpeg.
|
|
|
|
* This list is current as of 02-23-09; To find out if there are more
|
|
|
|
* supported formats, check the ffmpeg changelog since this date for
|
|
|
|
* more formats.
|
2008-10-17 22:27:33 +02:00
|
|
|
*/
|
2008-11-04 16:55:11 +01:00
|
|
|
static const char *const ffmpeg_suffixes[] = {
|
2009-02-23 19:48:43 +01:00
|
|
|
"16sv", "3g2", "3gp", "4xm", "8svx", "aa3", "aac", "ac3", "afc", "aif",
|
|
|
|
"aifc", "aiff", "al", "alaw", "amr", "anim", "apc", "ape", "asf",
|
|
|
|
"atrac", "au", "aud", "avi", "avm2", "avs", "bap", "bfi", "c93", "cak",
|
|
|
|
"cin", "cmv", "cpk", "daud", "dct", "divx", "dts", "dv", "dvd", "dxa",
|
|
|
|
"eac3", "film", "flac", "flc", "fli", "fll", "flx", "flv", "g726",
|
|
|
|
"gsm", "gxf", "iss", "m1v", "m2v", "m2t", "m2ts", "m4a", "m4v", "mad",
|
|
|
|
"mj2", "mjpeg", "mjpg", "mka", "mkv", "mlp", "mm", "mmf", "mov", "mp+",
|
|
|
|
"mp1", "mp2", "mp3", "mp4", "mpc", "mpeg", "mpg", "mpga", "mpp", "mpu",
|
|
|
|
"mve", "mvi", "mxf", "nc", "nsv", "nut", "nuv", "oga", "ogm", "ogv",
|
|
|
|
"ogx", "oma", "ogg", "omg", "psp", "pva", "qcp", "qt", "r3d", "ra",
|
|
|
|
"ram", "rl2", "rm", "rmvb", "roq", "rpl", "rvc", "shn", "smk", "snd",
|
|
|
|
"sol", "son", "spx", "str", "swf", "tgi", "tgq", "tgv", "thp", "ts",
|
|
|
|
"tsp", "tta", "xa", "xvid", "uv", "uv2", "vb", "vid", "vob", "voc",
|
|
|
|
"vp6", "vmd", "wav", "wma", "wmv", "wsaud", "wsvga", "wv", "wve",
|
2008-10-17 22:27:33 +02:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2008-11-04 16:55:11 +01:00
|
|
|
static const char *const ffmpeg_mime_types[] = {
|
2009-02-23 19:48:43 +01:00
|
|
|
"application/mp4",
|
|
|
|
"application/octet-stream",
|
|
|
|
"application/ogg",
|
|
|
|
"application/x-ms-wmz",
|
|
|
|
"application/x-ms-wmd",
|
|
|
|
"application/x-shockwave-flash",
|
|
|
|
"application/x-shorten",
|
|
|
|
"audio/8svx",
|
|
|
|
"audio/16sv",
|
|
|
|
"audio/aac",
|
|
|
|
"audio/ac3",
|
|
|
|
"audio/amr",
|
|
|
|
"audio/basic",
|
|
|
|
"audio/flac",
|
|
|
|
"audio/mpeg",
|
|
|
|
"audio/musepack",
|
|
|
|
"audio/ogg",
|
|
|
|
"audio/qcelp",
|
|
|
|
"audio/vorbis",
|
|
|
|
"audio/x-8svx",
|
|
|
|
"audio/x-16sv",
|
|
|
|
"audio/x-aac",
|
|
|
|
"audio/x-ac3",
|
|
|
|
"audio/x-aiff"
|
|
|
|
"audio/x-alaw",
|
|
|
|
"audio/x-au",
|
|
|
|
"audio/x-dca",
|
|
|
|
"audio/x-eac3",
|
|
|
|
"audio/x-flac",
|
|
|
|
"audio/x-gsm",
|
|
|
|
"audio/x-mace",
|
|
|
|
"audio/x-monkeys-audio",
|
|
|
|
"audio/x-mpeg",
|
2008-10-17 22:27:33 +02:00
|
|
|
"audio/x-ms-wma",
|
|
|
|
"audio/x-ms-wax",
|
2009-02-23 19:48:43 +01:00
|
|
|
"audio/x-musepack",
|
|
|
|
"audio/x-pn-realaudio",
|
|
|
|
"audio/x-pn-multirate-realaudio",
|
|
|
|
"audio/x-speex",
|
|
|
|
"audio/x-tta"
|
|
|
|
"audio/x-wav",
|
|
|
|
"audio/x-wma",
|
|
|
|
"audio/x-wv",
|
|
|
|
"video/anim",
|
|
|
|
"video/quicktime",
|
|
|
|
"video/msvideo",
|
|
|
|
"video/ogg",
|
|
|
|
"video/theora",
|
|
|
|
"video/x-dv",
|
|
|
|
"video/x-flv",
|
|
|
|
"video/x-matroska",
|
|
|
|
"video/x-mjpeg",
|
|
|
|
"video/x-mpeg",
|
|
|
|
"video/x-ms-asf",
|
|
|
|
"video/x-msvideo",
|
2008-10-17 22:27:33 +02:00
|
|
|
"video/x-ms-wmv",
|
|
|
|
"video/x-ms-wvx",
|
|
|
|
"video/x-ms-wm",
|
|
|
|
"video/x-ms-wmx",
|
2009-02-23 19:48:43 +01:00
|
|
|
"video/x-nut",
|
|
|
|
"video/x-pva",
|
|
|
|
"video/x-theora",
|
|
|
|
"video/x-vid",
|
|
|
|
"video/x-wmv",
|
|
|
|
"video/x-xvid",
|
2008-10-17 22:27:33 +02:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2009-04-03 00:52:13 +02:00
|
|
|
const struct decoder_plugin ffmpeg_decoder_plugin = {
|
2008-10-17 22:27:33 +02:00
|
|
|
.name = "ffmpeg",
|
|
|
|
.init = ffmpeg_init,
|
|
|
|
.stream_decode = ffmpeg_decode,
|
|
|
|
.tag_dup = ffmpeg_tag,
|
2008-11-04 16:55:11 +01:00
|
|
|
.suffixes = ffmpeg_suffixes,
|
|
|
|
.mime_types = ffmpeg_mime_types
|
2008-10-17 22:27:33 +02:00
|
|
|
};
|