/* * 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 "tag_handler.h" #include "tag_table.h" #include #include #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; /* offset to be added to timestamps so they start at 0 */ int64_t pts_offset; /* pts of the last frame sent to the player */ int64_t last_pts; } LibavDecContext; static const struct tag_table libav_tags[] = { { "year", TAG_DATE }, { "author-sort", TAG_ARTIST_SORT }, { "album_artist", TAG_ALBUM_ARTIST }, { "album_artist-sort", TAG_ALBUM_ARTIST_SORT }, /* sentinel */ { NULL, TAG_NUM_OF_ITEM_TYPES } }; static void libav_copy_metadata(enum tag_type type, AVDictionary *m, const char *name, const struct tag_handler *handler, void *handler_ctx) { AVDictionaryEntry *mt = NULL; while ((mt = av_dict_get(m, name, mt, 0)) != NULL) tag_handler_invoke_tag(handler, handler_ctx, type, mt->value); } static void libav_scan_pairs(AVDictionary *dict, const struct tag_handler *handler, void *handler_ctx) { AVDictionaryEntry *i = NULL; while ((i = av_dict_get(dict, "", i, AV_DICT_IGNORE_SUFFIX)) != NULL) tag_handler_invoke_pair(handler, handler_ctx, i->key, i->value); } static void libav_scan_dictionary(AVDictionary *dict, const struct tag_handler *handler, void *handler_ctx) { for (int i = 0; i < TAG_NUM_OF_ITEM_TYPES; i++) libav_copy_metadata(i, dict, tag_item_names[i], handler, handler_ctx); for (const struct tag_table *i = libav_tags; i->name != NULL; i++) libav_copy_metadata(i->type, dict, i->name, handler, handler_ctx); if (handler->pair != NULL) libav_scan_pairs(dict, handler, handler_ctx); } 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 int64_t time_to_libav(double t, const AVRational time_base) { return av_rescale_q((int64_t)(t * 1024), (AVRational){1, 1024}, time_base); } static enum decoder_command libav_send_packet(LibavDecContext *s, const AVPacket *packet) { AVPacket packet2 = *packet; enum decoder_command cmd = DECODE_COMMAND_NONE; while (packet2.size > 0 && cmd == DECODE_COMMAND_NONE) { AVFrame *frame = av_frame_alloc(); int got_frame = 0; int ret; if (!frame) { g_warning("Error allocating a frame for decoding.\n"); break; } ret = avcodec_decode_audio4(s->ast->codec, frame, &got_frame, &packet2); if (ret < 0) { g_message("decoding failed, frame skipped\n"); break; } packet2.data += ret; packet2.size -= ret; packet2.pts = packet2.dts = AV_NOPTS_VALUE; if (!got_frame) { if (!ret) break; continue; } if (s->pts_offset == AV_NOPTS_VALUE) { s->pts_offset = (frame->pkt_pts == AV_NOPTS_VALUE) ? 0 : -frame->pkt_pts; } if (frame->pkt_pts != AV_NOPTS_VALUE) { int64_t timestamp = frame->pkt_pts + s->pts_offset; if (timestamp < s->last_pts) { g_warning("Non-monotonous timestamps: previous %"PRId64 ", current %"PRId64"\n", s->last_pts, timestamp); } else { decoder_timestamp(s->decoder, timestamp * av_q2d(s->ast->time_base)); s->last_pts = timestamp; } } cmd = decoder_data(s->decoder, s->input, frame, s->ast->codec->bit_rate / 1000); } return cmd; } static void libav_decode(struct decoder *decoder, struct input_stream *input) { LibavDecContext s = { .decoder = decoder, .pts_offset = AV_NOPTS_VALUE }; 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, dec->sample_fmt, 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() */ av_opt_set_int(dec, "refcounted_frames", 1, 0); 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(&s, &packet); 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 { s.last_pts = 0; 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", .stream_decode = libav_decode, .scan_stream = libav_scan_stream, .suffixes = libav_suffixes, .mime_types = libav_mime_types };