diff options
Diffstat (limited to 'libavformat')
-rw-r--r-- | libavformat/Makefile | 4 | ||||
-rw-r--r-- | libavformat/apetag.c | 56 | ||||
-rw-r--r-- | libavformat/apetag.h | 4 | ||||
-rw-r--r-- | libavformat/matroskaenc.c | 73 | ||||
-rw-r--r-- | libavformat/version.h | 2 | ||||
-rw-r--r-- | libavformat/wvenc.c | 73 |
6 files changed, 173 insertions, 39 deletions
diff --git a/libavformat/Makefile b/libavformat/Makefile index c12f127c6c..7af1589019 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -195,7 +195,7 @@ OBJS-$(CONFIG_MATROSKA_DEMUXER) += matroskadec.o matroska.o \ isom.o rmsipr.o OBJS-$(CONFIG_MATROSKA_MUXER) += matroskaenc.o matroska.o \ isom.o avc.o \ - flacenc_header.o avlanguage.o + flacenc_header.o avlanguage.o wv.o OBJS-$(CONFIG_MD5_MUXER) += md5enc.o OBJS-$(CONFIG_MGSTS_DEMUXER) += mgsts.o OBJS-$(CONFIG_MICRODVD_DEMUXER) += microdvddec.o subtitles.o @@ -399,7 +399,7 @@ OBJS-$(CONFIG_WTV_DEMUXER) += wtvdec.o wtv.o asfdec.o asf.o asfcry avlanguage.o mpegts.o isom.o OBJS-$(CONFIG_WTV_MUXER) += wtvenc.o wtv.o asf.o asfenc.o OBJS-$(CONFIG_WV_DEMUXER) += wvdec.o wv.o apetag.o img2.o -OBJS-$(CONFIG_WV_MUXER) += wvenc.o apetagenc.o +OBJS-$(CONFIG_WV_MUXER) += wvenc.o wv.o apetag.o OBJS-$(CONFIG_XA_DEMUXER) += xa.o OBJS-$(CONFIG_XBIN_DEMUXER) += bintext.o sauce.o OBJS-$(CONFIG_XMV_DEMUXER) += xmv.o diff --git a/libavformat/apetag.c b/libavformat/apetag.c index a445c84aef..e74ed26347 100644 --- a/libavformat/apetag.c +++ b/libavformat/apetag.c @@ -23,10 +23,12 @@ #include "libavutil/intreadwrite.h" #include "libavutil/dict.h" #include "avformat.h" +#include "avio_internal.h" #include "apetag.h" #include "internal.h" #define APE_TAG_FLAG_CONTAINS_HEADER (1 << 31) +#define APE_TAG_FLAG_CONTAINS_FOOTER (1 << 30) #define APE_TAG_FLAG_IS_HEADER (1 << 29) #define APE_TAG_FLAG_IS_BINARY (1 << 1) @@ -167,3 +169,57 @@ int64_t ff_ape_parse_tag(AVFormatContext *s) return tag_start; } + +int ff_ape_write_tag(AVFormatContext *s) +{ + AVDictionaryEntry *e = NULL; + int64_t start, end; + int size, count = 0; + + if (!s->pb->seekable) + return 0; + + start = avio_tell(s->pb); + + // header + avio_write(s->pb, "APETAGEX", 8); // id + avio_wl32 (s->pb, APE_TAG_VERSION); // version + avio_wl32(s->pb, 0); // reserve space for size + avio_wl32(s->pb, 0); // reserve space for tag count + + // flags + avio_wl32(s->pb, APE_TAG_FLAG_CONTAINS_HEADER | APE_TAG_FLAG_CONTAINS_FOOTER | + APE_TAG_FLAG_IS_HEADER); + ffio_fill(s->pb, 0, 8); // reserved + + while ((e = av_dict_get(s->metadata, "", e, AV_DICT_IGNORE_SUFFIX))) { + int val_len = strlen(e->value); + + avio_wl32(s->pb, val_len); // value length + avio_wl32(s->pb, 0); // item flags + avio_put_str(s->pb, e->key); // key + avio_write(s->pb, e->value, val_len); // value + count++; + } + + size = avio_tell(s->pb) - start; + + // footer + avio_write(s->pb, "APETAGEX", 8); // id + avio_wl32 (s->pb, APE_TAG_VERSION); // version + avio_wl32(s->pb, size); // size + avio_wl32(s->pb, count); // tag count + + // flags + avio_wl32(s->pb, APE_TAG_FLAG_CONTAINS_HEADER | APE_TAG_FLAG_CONTAINS_FOOTER); + ffio_fill(s->pb, 0, 8); // reserved + + // update values in the header + end = avio_tell(s->pb); + avio_seek(s->pb, start + 12, SEEK_SET); + avio_wl32(s->pb, size); + avio_wl32(s->pb, count); + avio_seek(s->pb, end, SEEK_SET); + + return 0; +} diff --git a/libavformat/apetag.h b/libavformat/apetag.h index 0330c8986a..cf2a5f8ab4 100644 --- a/libavformat/apetag.h +++ b/libavformat/apetag.h @@ -37,8 +37,8 @@ int64_t ff_ape_parse_tag(AVFormatContext *s); /** - * Write an APEv2 tag + * Write an APE tag into a file. */ -void ff_ape_write(AVFormatContext *s); +int ff_ape_write_tag(AVFormatContext *s); #endif /* AVFORMAT_APETAG_H */ diff --git a/libavformat/matroskaenc.c b/libavformat/matroskaenc.c index 253ed9c778..99d648dcbc 100644 --- a/libavformat/matroskaenc.c +++ b/libavformat/matroskaenc.c @@ -27,6 +27,7 @@ #include "isom.h" #include "matroska.h" #include "riff.h" +#include "wv.h" #include "libavutil/avstring.h" #include "libavutil/dict.h" @@ -459,6 +460,15 @@ static int put_xiph_codecpriv(AVFormatContext *s, AVIOContext *pb, AVCodecContex return 0; } +static int put_wv_codecpriv(AVIOContext *pb, AVCodecContext *codec) +{ + if (codec->extradata && codec->extradata_size == 2) + avio_write(pb, codec->extradata, 2); + else + avio_wl16(pb, 0x403); // fallback to the version mentioned in matroska specs + return 0; +} + static void get_aac_sample_rates(AVFormatContext *s, AVCodecContext *codec, int *sample_rate, int *output_sample_rate) { MPEG4AudioConfig mp4ac; @@ -488,6 +498,8 @@ static int mkv_write_codecprivate(AVFormatContext *s, AVIOContext *pb, AVCodecCo ret = put_xiph_codecpriv(s, dyn_cp, codec); else if (codec->codec_id == AV_CODEC_ID_FLAC) ret = ff_flac_write_header(dyn_cp, codec, 1); + else if (codec->codec_id == AV_CODEC_ID_WAVPACK) + ret = put_wv_codecpriv(dyn_cp, codec); else if (codec->codec_id == AV_CODEC_ID_H264) ret = ff_isom_write_avcc(dyn_cp, codec->extradata, codec->extradata_size); else if (codec->codec_id == AV_CODEC_ID_ALAC) { @@ -1137,6 +1149,59 @@ static int mkv_write_ass_blocks(AVFormatContext *s, AVIOContext *pb, AVPacket *p } #endif +static int mkv_strip_wavpack(const uint8_t *src, uint8_t **pdst, int *size) +{ + uint8_t *dst; + int srclen = *size; + int offset = 0; + int ret; + + dst = av_malloc(srclen); + if (!dst) + return AVERROR(ENOMEM); + + while (srclen >= WV_HEADER_SIZE) { + WvHeader header; + + ret = ff_wv_parse_header(&header, src); + if (ret < 0) + goto fail; + src += WV_HEADER_SIZE; + srclen -= WV_HEADER_SIZE; + + if (srclen < header.blocksize) { + ret = AVERROR_INVALIDDATA; + goto fail; + } + + if (header.initial) { + AV_WL32(dst + offset, header.samples); + offset += 4; + } + AV_WL32(dst + offset, header.flags); + AV_WL32(dst + offset + 4, header.crc); + offset += 8; + + if (!(header.initial && header.final)) { + AV_WL32(dst + offset, header.blocksize); + offset += 4; + } + + memcpy(dst + offset, src, header.blocksize); + src += header.blocksize; + srclen -= header.blocksize; + offset += header.blocksize; + } + + *pdst = dst; + *size = offset; + + return 0; +fail: + av_freep(&dst); + return ret; +} + static void mkv_write_block(AVFormatContext *s, AVIOContext *pb, unsigned int blockid, AVPacket *pkt, int flags) { @@ -1154,7 +1219,13 @@ static void mkv_write_block(AVFormatContext *s, AVIOContext *pb, if (codec->codec_id == AV_CODEC_ID_H264 && codec->extradata_size > 0 && (AV_RB24(codec->extradata) == 1 || AV_RB32(codec->extradata) == 1)) ff_avc_parse_nal_units_buf(pkt->data, &data, &size); - else + else if (codec->codec_id == AV_CODEC_ID_WAVPACK) { + int ret = mkv_strip_wavpack(pkt->data, &data, &size); + if (ret < 0) { + av_log(s, AV_LOG_ERROR, "Error stripping a WavPack packet.\n"); + return; + } + } else data = pkt->data; if (codec->codec_id == AV_CODEC_ID_PRORES) { diff --git a/libavformat/version.h b/libavformat/version.h index e9fd748c7a..3c0afbb1fd 100644 --- a/libavformat/version.h +++ b/libavformat/version.h @@ -30,7 +30,7 @@ #include "libavutil/avutil.h" #define LIBAVFORMAT_VERSION_MAJOR 55 -#define LIBAVFORMAT_VERSION_MINOR 7 +#define LIBAVFORMAT_VERSION_MINOR 8 #define LIBAVFORMAT_VERSION_MICRO 100 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \ diff --git a/libavformat/wvenc.c b/libavformat/wvenc.c index f559da2578..7fed4a687d 100644 --- a/libavformat/wvenc.c +++ b/libavformat/wvenc.c @@ -1,5 +1,6 @@ /* * WavPack muxer + * Copyright (c) 2013 Kostya Shishkov <kostya.shishkov@gmail.com> * Copyright (c) 2012 Paul B Mahol * * This file is part of FFmpeg. @@ -19,68 +20,74 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#include "libavutil/intreadwrite.h" -#include "avformat.h" -#include "internal.h" + +#include "libavutil/attributes.h" + #include "apetag.h" +#include "avformat.h" +#include "wv.h" -typedef struct WVMuxContext { +typedef struct WvMuxContext { int64_t samples; -} WVMuxContext; +} WvMuxContext; -static int write_header(AVFormatContext *s) +static av_cold int wv_write_header(AVFormatContext *ctx) { - AVCodecContext *codec = s->streams[0]->codec; - - if (s->nb_streams > 1) { - av_log(s, AV_LOG_ERROR, "only one stream is supported\n"); - return AVERROR(EINVAL); - } - if (codec->codec_id != AV_CODEC_ID_WAVPACK) { - av_log(s, AV_LOG_ERROR, "unsupported codec\n"); + if (ctx->nb_streams > 1 || + ctx->streams[0]->codec->codec_id != AV_CODEC_ID_WAVPACK) { + av_log(ctx, AV_LOG_ERROR, "This muxer only supports a single WavPack stream.\n"); return AVERROR(EINVAL); } + return 0; } -static int write_packet(AVFormatContext *ctx, AVPacket *pkt) +static int wv_write_packet(AVFormatContext *ctx, AVPacket *pkt) { - WVMuxContext *s = ctx->priv_data; + WvMuxContext *s = ctx->priv_data; + WvHeader header; + int ret; + + if (pkt->size < WV_HEADER_SIZE || + (ret = ff_wv_parse_header(&header, pkt->data)) < 0) { + av_log(ctx, AV_LOG_ERROR, "Invalid WavPack packet.\n"); + return AVERROR(EINVAL); + } + s->samples += header.samples; - if (pkt->size >= 24) - s->samples += AV_RL32(pkt->data + 20); avio_write(ctx->pb, pkt->data, pkt->size); + avio_flush(ctx->pb); return 0; } -static int write_trailer(AVFormatContext *ctx) +static av_cold int wv_write_trailer(AVFormatContext *ctx) { - WVMuxContext *s = ctx->priv_data; - - ff_ape_write(ctx); + WvMuxContext *s = ctx->priv_data; - if (ctx->pb->seekable && s->samples) { + /* update total number of samples in the first block */ + if (ctx->pb->seekable && s->samples && + s->samples < UINT32_MAX) { + int64_t pos = avio_tell(ctx->pb); avio_seek(ctx->pb, 12, SEEK_SET); - if (s->samples < 0xFFFFFFFFu) - avio_wl32(ctx->pb, s->samples); - else - avio_wl32(ctx->pb, 0xFFFFFFFFu); - avio_flush(ctx->pb); + avio_wl32(ctx->pb, s->samples); + avio_seek(ctx->pb, pos, SEEK_SET); } + ff_ape_write_tag(ctx); return 0; } AVOutputFormat ff_wv_muxer = { .name = "wv", - .long_name = NULL_IF_CONFIG_SMALL("WavPack"), - .priv_data_size = sizeof(WVMuxContext), + .long_name = NULL_IF_CONFIG_SMALL("raw WavPack"), + .mime_type = "audio/x-wavpack", .extensions = "wv", + .priv_data_size = sizeof(WvMuxContext), .audio_codec = AV_CODEC_ID_WAVPACK, .video_codec = AV_CODEC_ID_NONE, - .write_header = write_header, - .write_packet = write_packet, - .write_trailer = write_trailer, + .write_header = wv_write_header, + .write_packet = wv_write_packet, + .write_trailer = wv_write_trailer, .flags = AVFMT_NOTIMESTAMPS, }; |