/* * Copyright (C) 2003-2011 The Music Player Daemon Project * 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., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #include "config.h" #include "decoder_api.h" #include "audio_check.h" #include "libav_metadata.h" #include "tag_handler.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #undef G_LOG_DOMAIN #define G_LOG_DOMAIN "libav" typedef struct LibavDecContext { struct decoder *decoder; struct input_stream *input; AVIOContext *io; AVFormatContext *fmt_ctx; AVStream *ast; } LibavDecContext; static GLogLevelFlags level_libav_to_glib(int level) { if (level <= AV_LOG_FATAL) return G_LOG_LEVEL_CRITICAL; if (level <= AV_LOG_ERROR) return G_LOG_LEVEL_WARNING; if (level <= AV_LOG_INFO) return G_LOG_LEVEL_MESSAGE; return G_LOG_LEVEL_DEBUG; } static void mpd_libav_log_callback(void *ptr, int level, const char *fmt, va_list vl) { const AVClass * cls = NULL; if (ptr != NULL) cls = *(const AVClass *const*)ptr; if (cls != NULL) { char *domain = g_strconcat(G_LOG_DOMAIN, "/", cls->item_name(ptr), NULL); g_logv(domain, level_libav_to_glib(level), fmt, vl); g_free(domain); } } static int mpd_libav_stream_read(void *opaque, uint8_t *buf, int size) { LibavDecContext *stream = opaque; return decoder_read(stream->decoder, stream->input, (void *)buf, size); } static int64_t mpd_libav_stream_seek(void *opaque, int64_t pos, int whence) { LibavDecContext *stream = opaque; if (whence == AVSEEK_SIZE) return stream->input->size; if (!input_stream_lock_seek(stream->input, pos, whence, NULL)) return -1; return stream->input->offset; } static void libav_close(LibavDecContext *s) { if (s->fmt_ctx) avformat_close_input(&s->fmt_ctx); if (s->io) av_freep(&s->io->buffer); av_freep(&s->io); } #define INPUT_BUFFER_SIZE 16384 static int libav_open(LibavDecContext *s, struct input_stream *input) { const char *filename = input->uri; uint8_t *buf; int ret = 0; s->input = input; /* init IO context */ buf = av_malloc(INPUT_BUFFER_SIZE); if (!buf) return AVERROR(ENOMEM); s->io = avio_alloc_context(buf, INPUT_BUFFER_SIZE, 0, s, mpd_libav_stream_read, NULL, input->seekable ? mpd_libav_stream_seek : NULL); if (!s->io) { av_freep(&buf); return AVERROR(ENOMEM); } /* open the input stream */ s->fmt_ctx = avformat_alloc_context(); if (!s->fmt_ctx) { ret = AVERROR(ENOMEM); goto fail; } s->fmt_ctx->pb = s->io; ret = avformat_open_input(&s->fmt_ctx, filename, NULL, NULL); if (ret < 0) goto fail; g_debug("Successfully opened input stream '%s', detected input format '%s' (%s)\n", filename, s->fmt_ctx->iformat->name, s->fmt_ctx->iformat->long_name); ret = avformat_find_stream_info(s->fmt_ctx, NULL); if (ret < 0) { g_warning("Couldn't find stream info\n"); goto fail; } for (int i = 0; i < s->fmt_ctx->nb_streams; i++) if (s->fmt_ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO) { s->ast = s->fmt_ctx->streams[i]; break; } if (!s->ast) { g_warning("No audio stream in the file.\n"); ret = AVERROR(EINVAL); goto fail; } return 0; fail: g_warning("Failed to open input stream '%s'.\n", filename); libav_close(s); return ret; } static bool libav_init(const struct config_param *param) { av_log_set_callback(mpd_libav_log_callback); av_register_all(); return true; } static double time_from_libav(int64_t t, const AVRational time_base) { assert(t != (int64_t)AV_NOPTS_VALUE); return (double)av_rescale_q(t, time_base, (AVRational){1, 1024}) / (double)1024; } static int64_t time_to_libav(double t, const AVRational time_base) { return av_rescale_q((int64_t)(t * 1024), (AVRational){1, 1024}, time_base); } /** * Copy PCM data from a AVFrame to an interleaved buffer. */ static int copy_interleave_frame(const AVCodecContext *codec_context, const AVFrame *frame, uint8_t *buffer, size_t buffer_size) { int plane_size; const int data_size = av_samples_get_buffer_size(&plane_size, codec_context->channels, frame->nb_samples, codec_context->sample_fmt, 1); uint16_t *dst = (uint16_t*)buffer; if (buffer_size < (size_t)data_size) /* buffer is too small - shouldn't happen */ return AVERROR(EINVAL); if (av_sample_fmt_is_planar(codec_context->sample_fmt) && codec_context->channels > 1) { for (int i = 0; i < frame->nb_samples; i++) { for (int j = 0; j < codec_context->channels; j++) dst[i * codec_context->channels + j] = ((uint16_t*)frame->extended_data[j])[i]; } } else { memcpy(buffer, frame->extended_data[0], data_size); } return data_size; } #define AVCODEC_MAX_AUDIO_FRAME_SIZE 50000 static enum decoder_command libav_send_packet(struct decoder *decoder, struct input_stream *is, const AVPacket *packet, AVCodecContext *codec_context, const AVRational *time_base) { AVPacket packet2 = *packet; uint8_t aligned_buffer[(AVCODEC_MAX_AUDIO_FRAME_SIZE * 3) / 2 + 16]; const size_t buffer_size = sizeof(aligned_buffer); enum decoder_command cmd = DECODE_COMMAND_NONE; if (packet->pts != (int64_t)AV_NOPTS_VALUE) decoder_timestamp(decoder, time_from_libav(packet->pts, *time_base)); while (packet2.size > 0 && cmd == DECODE_COMMAND_NONE) { int audio_size = buffer_size; AVFrame frame; int got_frame = 0; int len = avcodec_decode_audio4(codec_context, &frame, &got_frame, &packet2); if (len >= 0 && got_frame) { audio_size = copy_interleave_frame(codec_context, &frame, aligned_buffer, buffer_size); if (audio_size < 0) len = audio_size; } else if (len >= 0) len = -1; if (len < 0) { /* if error, we skip the frame */ g_message("decoding failed, frame skipped\n"); break; } packet2.data += len; packet2.size -= len; if (audio_size <= 0) continue; cmd = decoder_data(decoder, is, aligned_buffer, audio_size, codec_context->bit_rate / 1000); } return cmd; } static enum sample_format libav_sample_format(const AVCodecContext *codec_context) { switch (codec_context->sample_fmt) { case AV_SAMPLE_FMT_S16: case AV_SAMPLE_FMT_S16P: return SAMPLE_FORMAT_S16; case AV_SAMPLE_FMT_S32: return SAMPLE_FORMAT_S32; default: g_warning("Unsupported libavcodec SampleFormat value: %d", codec_context->sample_fmt); return SAMPLE_FORMAT_UNDEFINED; } } static void libav_decode(struct decoder *decoder, struct input_stream *input) { LibavDecContext s = { .decoder = decoder }; AVCodecContext *dec = NULL; const AVCodec *codec; struct audio_format audio_format; enum decoder_command cmd; GError *error = NULL; int ret, total_time; ret = libav_open(&s, input); if (ret < 0) return; dec = s.ast->codec; codec = avcodec_find_decoder(dec->codec_id); if (!codec) { g_warning("Unsupported audio codec\n"); goto finish; } g_debug("Selected audio decoder %s\n", codec->name); if (!audio_format_init_checked(&audio_format, dec->sample_rate, libav_sample_format(dec), dec->channels, &error)) { g_warning("%s", error->message); g_error_free(error); goto finish; } /* the audio format must be read from AVCodecContext by now, because avcodec_open() has been demonstrated to fill bogus values into AVCodecContext.channels - a change that will be reverted later by avcodec_decode_audio3() */ ret = avcodec_open2(dec, codec, NULL); if (ret < 0) { g_warning("Could not open codec\n"); goto finish; } total_time = s.fmt_ctx->duration != (int64_t)AV_NOPTS_VALUE ? s.fmt_ctx->duration / AV_TIME_BASE : 0; decoder_initialized(decoder, &audio_format, input->seekable, total_time); do { AVPacket packet; if (av_read_frame(s.fmt_ctx, &packet) < 0) /* end of file */ break; if (packet.stream_index == s.ast->index) cmd = libav_send_packet(decoder, input, &packet, dec, &s.ast->time_base); else cmd = decoder_get_command(decoder); av_free_packet(&packet); if (cmd == DECODE_COMMAND_SEEK) { int64_t where = time_to_libav(decoder_seek_where(decoder), s.ast->time_base); if (av_seek_frame(s.fmt_ctx, s.ast->index, where, AV_TIME_BASE) < 0) decoder_seek_error(decoder); else { avcodec_flush_buffers(dec); decoder_command_finished(decoder); } } } while (cmd != DECODE_COMMAND_STOP); finish: if (dec) avcodec_close(dec); libav_close(&s); } //no tag reading in ffmpeg, check if playable static bool libav_scan_stream(struct input_stream *is, const struct tag_handler *handler, void *handler_ctx) { LibavDecContext s = { NULL }; int ret; ret = libav_open(&s, is); if (ret < 0) return 0; if (s.fmt_ctx->duration != (int64_t)AV_NOPTS_VALUE) tag_handler_invoke_duration(handler, handler_ctx, s.fmt_ctx->duration / AV_TIME_BASE); libav_scan_dictionary(s.fmt_ctx->metadata, handler, handler_ctx); libav_scan_dictionary(s.ast->metadata, handler, handler_ctx); libav_close(&s); return true; } /** * 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. */ static const char *const libav_suffixes[] = { "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", "m4b", "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", "webm", "wma", "wmv", "wsaud", "wsvga", "wv", "wve", NULL }; static const char *const libav_mime_types[] = { "application/m4a", "application/mp4", "application/octet-stream", "application/ogg", "application/x-ms-wmz", "application/x-ms-wmd", "application/x-ogg", "application/x-shockwave-flash", "application/x-shorten", "audio/8svx", "audio/16sv", "audio/aac", "audio/ac3", "audio/aiff" "audio/amr", "audio/basic", "audio/flac", "audio/m4a", "audio/mp4", "audio/mpeg", "audio/musepack", "audio/ogg", "audio/qcelp", "audio/vorbis", "audio/vorbis+ogg", "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-matroska", "audio/x-monkeys-audio", "audio/x-mpeg", "audio/x-ms-wma", "audio/x-ms-wax", "audio/x-musepack", "audio/x-ogg", "audio/x-vorbis", "audio/x-vorbis+ogg", "audio/x-pn-realaudio", "audio/x-pn-multirate-realaudio", "audio/x-speex", "audio/x-tta" "audio/x-voc", "audio/x-wav", "audio/x-wma", "audio/x-wv", "video/anim", "video/quicktime", "video/msvideo", "video/ogg", "video/theora", "video/webm", "video/x-dv", "video/x-flv", "video/x-matroska", "video/x-mjpeg", "video/x-mpeg", "video/x-ms-asf", "video/x-msvideo", "video/x-ms-wmv", "video/x-ms-wvx", "video/x-ms-wm", "video/x-ms-wmx", "video/x-nut", "video/x-pva", "video/x-theora", "video/x-vid", "video/x-wmv", "video/x-xvid", /* special value for the "ffmpeg" input plugin: all streams by the "ffmpeg" input plugin shall be decoded by this plugin */ "audio/x-mpd-libav", NULL }; const struct decoder_plugin libav_decoder_plugin = { .name = "libav", .init = libav_init, .stream_decode = libav_decode, .scan_stream = libav_scan_stream, .suffixes = libav_suffixes, .mime_types = libav_mime_types };