summaryrefslogtreecommitdiff
path: root/libavformat
diff options
context:
space:
mode:
authorJames Almer <jamrial@gmail.com>2016-08-04 16:21:25 -0300
committerJames Almer <jamrial@gmail.com>2016-08-04 16:25:40 -0300
commit376d4b3c0d9d779baef3e2a72ac1ffe77e8508b8 (patch)
treef3ce1d9feb7f9f6f8352a9966722ef546fb03f06 /libavformat
parentce944e84892f1ba21602bd83b1a5d532dfdd1eda (diff)
avformat: add a TTA Muxer
Reviewed-by: Michael Niedermayer <michael@niedermayer.cc> Signed-off-by: James Almer <jamrial@gmail.com>
Diffstat (limited to 'libavformat')
-rw-r--r--libavformat/Makefile1
-rw-r--r--libavformat/allformats.c2
-rw-r--r--libavformat/ttaenc.c149
-rw-r--r--libavformat/version.h2
4 files changed, 152 insertions, 2 deletions
diff --git a/libavformat/Makefile b/libavformat/Makefile
index e2cb474396..fda1e17c83 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -471,6 +471,7 @@ OBJS-$(CONFIG_TMV_DEMUXER) += tmv.o
OBJS-$(CONFIG_TRUEHD_DEMUXER) += rawdec.o mlpdec.o
OBJS-$(CONFIG_TRUEHD_MUXER) += rawenc.o
OBJS-$(CONFIG_TTA_DEMUXER) += tta.o apetag.o img2.o
+OBJS-$(CONFIG_TTA_MUXER) += ttaenc.o apetag.o img2.o
OBJS-$(CONFIG_TTY_DEMUXER) += tty.o sauce.o
OBJS-$(CONFIG_TXD_DEMUXER) += txd.o
OBJS-$(CONFIG_UNCODEDFRAMECRC_MUXER) += uncodedframecrcenc.o framehash.o
diff --git a/libavformat/allformats.c b/libavformat/allformats.c
index 10c9bcce7f..a69195e6fe 100644
--- a/libavformat/allformats.c
+++ b/libavformat/allformats.c
@@ -311,7 +311,7 @@ void av_register_all(void)
REGISTER_MUXER (MKVTIMESTAMP_V2, mkvtimestamp_v2);
REGISTER_DEMUXER (TMV, tmv);
REGISTER_MUXDEMUX(TRUEHD, truehd);
- REGISTER_DEMUXER (TTA, tta);
+ REGISTER_MUXDEMUX(TTA, tta);
REGISTER_DEMUXER (TXD, txd);
REGISTER_DEMUXER (TTY, tty);
REGISTER_MUXER (UNCODEDFRAMECRC, uncodedframecrc);
diff --git a/libavformat/ttaenc.c b/libavformat/ttaenc.c
new file mode 100644
index 0000000000..fdce1e3dc1
--- /dev/null
+++ b/libavformat/ttaenc.c
@@ -0,0 +1,149 @@
+/*
+ * True Audio (TTA) muxer
+ * Copyright (c) 2016 James Almer
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "libavutil/crc.h"
+#include "libavutil/intreadwrite.h"
+
+#include "apetag.h"
+#include "avformat.h"
+#include "avio_internal.h"
+#include "internal.h"
+
+typedef struct TTAMuxContext {
+ AVIOContext *seek_table;
+ AVIOContext *data;
+ uint32_t nb_samples;
+ int frame_size;
+ int last_frame;
+} TTAMuxContext;
+
+static int tta_write_header(AVFormatContext *s)
+{
+ TTAMuxContext *tta = s->priv_data;
+ AVCodecParameters *par = s->streams[0]->codecpar;
+ int ret;
+
+ if (s->nb_streams != 1) {
+ av_log(s, AV_LOG_ERROR, "Only one stream is supported\n");
+ return AVERROR(EINVAL);
+ }
+ if (par->codec_id != AV_CODEC_ID_TTA) {
+ av_log(s, AV_LOG_ERROR, "Unsupported codec\n");
+ return AVERROR(EINVAL);
+ }
+ if (par->extradata && par->extradata_size < 22) {
+ av_log(s, AV_LOG_ERROR, "Invalid TTA extradata\n");
+ return AVERROR_INVALIDDATA;
+ }
+
+ if ((ret = avio_open_dyn_buf(&tta->seek_table)) < 0)
+ return ret;
+ if ((ret = avio_open_dyn_buf(&tta->data)) < 0) {
+ ffio_free_dyn_buf(&tta->seek_table);
+ return ret;
+ }
+
+ /* Ignore most extradata information if present. It can be innacurate
+ if for example remuxing from Matroska */
+ ffio_init_checksum(s->pb, ff_crcEDB88320_update, UINT32_MAX);
+ ffio_init_checksum(tta->seek_table, ff_crcEDB88320_update, UINT32_MAX);
+ avio_write(s->pb, "TTA1", 4);
+ avio_wl16(s->pb, par->extradata ? AV_RL16(par->extradata + 4) : 1);
+ avio_wl16(s->pb, par->channels);
+ avio_wl16(s->pb, par->bits_per_raw_sample);
+ avio_wl32(s->pb, par->sample_rate);
+ /* Prevent overflow */
+ if (par->sample_rate > 0x7FFFFFu) {
+ av_log(s, AV_LOG_ERROR, "Sample rate too large\n");
+ return AVERROR(EINVAL);
+ }
+ tta->frame_size = par->sample_rate * 256 / 245;
+ avpriv_set_pts_info(s->streams[0], 64, 1, par->sample_rate);
+
+ return 0;
+}
+
+static int tta_write_packet(AVFormatContext *s, AVPacket *pkt)
+{
+ TTAMuxContext *tta = s->priv_data;
+
+ avio_write(tta->data, pkt->data, pkt->size);
+ avio_wl32(tta->seek_table, pkt->size);
+ tta->nb_samples += pkt->duration;
+
+ if (tta->frame_size != pkt->duration) {
+ if (tta->last_frame) {
+ /* Two frames with a different duration than the default frame
+ size means the TTA stream comes from a faulty container, and
+ there's no way the last frame duration will be correct. */
+ av_log(s, AV_LOG_ERROR, "Invalid frame durations\n");
+
+ return AVERROR_INVALIDDATA;
+ }
+ /* First frame with a different duration than the default frame size.
+ Assume it's the last frame in the stream and continue. */
+ tta->last_frame++;
+ }
+
+ return 0;
+}
+
+static int tta_write_trailer(AVFormatContext *s)
+{
+ TTAMuxContext *tta = s->priv_data;
+ uint8_t *ptr;
+ unsigned int crc;
+ int size;
+
+ avio_wl32(s->pb, tta->nb_samples);
+ crc = ffio_get_checksum(s->pb) ^ UINT32_MAX;
+ avio_wl32(s->pb, crc);
+
+ /* Write Seek table */
+ crc = ffio_get_checksum(tta->seek_table) ^ UINT32_MAX;
+ avio_wl32(tta->seek_table, crc);
+ size = avio_close_dyn_buf(tta->seek_table, &ptr);
+ avio_write(s->pb, ptr, size);
+ av_free(ptr);
+
+ /* Write audio data */
+ size = avio_close_dyn_buf(tta->data, &ptr);
+ avio_write(s->pb, ptr, size);
+ av_free(ptr);
+
+ ff_ape_write_tag(s);
+ avio_flush(s->pb);
+
+ return 0;
+}
+
+AVOutputFormat ff_tta_muxer = {
+ .name = "tta",
+ .long_name = NULL_IF_CONFIG_SMALL("TTA (True Audio)"),
+ .mime_type = "audio/x-tta",
+ .extensions = "tta",
+ .priv_data_size = sizeof(TTAMuxContext),
+ .audio_codec = AV_CODEC_ID_TTA,
+ .video_codec = AV_CODEC_ID_NONE,
+ .write_header = tta_write_header,
+ .write_packet = tta_write_packet,
+ .write_trailer = tta_write_trailer,
+};
diff --git a/libavformat/version.h b/libavformat/version.h
index 56308088b0..8899bfde0c 100644
--- a/libavformat/version.h
+++ b/libavformat/version.h
@@ -32,7 +32,7 @@
// Major bumping may affect Ticket5467, 5421, 5451(compatibility with Chromium)
// Also please add any ticket numbers that you belive might be affected here
#define LIBAVFORMAT_VERSION_MAJOR 57
-#define LIBAVFORMAT_VERSION_MINOR 44
+#define LIBAVFORMAT_VERSION_MINOR 45
#define LIBAVFORMAT_VERSION_MICRO 100
#define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \