History

avformat: add a TTA Muxer

James Almer committed 10 years ago

Commit 376d4b31 parent ce944e812 files · +174 −7Browse files

Unified · Split

12 files
  1. Changelog +1 −0
  2. doc/general.texi +1 −1
  3. libavformat/Makefile +1 −0
  4. libavformat/allformats.c +1 −1
  5. libavformat/ttaenc.c +149 −0
  6. libavformat/version.h +1 −1
  7. tests/fate/acodec.mak +2 −2
  8. tests/fate/avformat.mak +2 −0
  9. tests/lavf-regression.sh +8 −0
  10. tests/ref/acodec/tta +2 −2
  11. tests/ref/lavf/mka +3 −0
  12. tests/ref/lavf/tta +3 −0
Changelog+1−0
@@ -12,6 +12,7 @@ version <next>: · 11 unmodified lines
1212 - 16-bit support in selectivecolor filter
1313 - OpenH264 decoder wrapper
1414 - MediaCodec hwaccel
15+- True Audio (TTA) muxer
1516
1617
1718 version 3.1:
doc/general.texi+1−1
@@ -509,7 +509,7 @@ library: · 508 unmodified lines
509509 @tab Used on the Nintendo GameCube.
510510 @item Tiertex Limited SEQ @tab @tab X
511511 @tab Tiertex .seq files used in the DOS CD-ROM version of the game Flashback.
512−@item True Audio @tab @tab X
512+@item True Audio @tab X @tab X
513513 @item VAG @tab @tab X
514514 @tab Audio format used in many Sony PS2 games.
515515 @item VC-1 test bitstream @tab X @tab X
libavformat/Makefile+1−0
@@ -471,6 +471,7 @@ OBJS-$(CONFIG_TMV_DEMUXER) += tmv.o · 470 unmodified lines
471471 OBJS-$(CONFIG_TRUEHD_DEMUXER) += rawdec.o mlpdec.o
472472 OBJS-$(CONFIG_TRUEHD_MUXER) += rawenc.o
473473 OBJS-$(CONFIG_TTA_DEMUXER) += tta.o apetag.o img2.o
474+OBJS-$(CONFIG_TTA_MUXER) += ttaenc.o apetag.o img2.o
474475 OBJS-$(CONFIG_TTY_DEMUXER) += tty.o sauce.o
475476 OBJS-$(CONFIG_TXD_DEMUXER) += txd.o
476477 OBJS-$(CONFIG_UNCODEDFRAMECRC_MUXER) += uncodedframecrcenc.o framehash.o
libavformat/allformats.c+1−1
@@ -311,7 +311,7 @@ void av_register_all(void) · 310 unmodified lines
311311 REGISTER_MUXER (MKVTIMESTAMP_V2, mkvtimestamp_v2);
312312 REGISTER_DEMUXER (TMV, tmv);
313313 REGISTER_MUXDEMUX(TRUEHD, truehd);
314− REGISTER_DEMUXER (TTA, tta);
314+ REGISTER_MUXDEMUX(TTA, tta);
315315 REGISTER_DEMUXER (TXD, txd);
316316 REGISTER_DEMUXER (TTY, tty);
317317 REGISTER_MUXER (UNCODEDFRAMECRC, uncodedframecrc);
libavformat/ttaenc.c+149−0
@@ -0,0 +1,149 @@
1+/*
2+ * True Audio (TTA) muxer
3+ * Copyright (c) 2016 James Almer
4+ *
5+ * This file is part of FFmpeg.
6+ *
7+ * FFmpeg is free software; you can redistribute it and/or
8+ * modify it under the terms of the GNU Lesser General Public
9+ * License as published by the Free Software Foundation; either
10+ * version 2.1 of the License, or (at your option) any later version.
11+ *
12+ * FFmpeg is distributed in the hope that it will be useful,
13+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+ * Lesser General Public License for more details.
16+ *
17+ * You should have received a copy of the GNU Lesser General Public
18+ * License along with FFmpeg; if not, write to the Free Software
19+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20+ */
21+
22+#include "libavutil/crc.h"
23+#include "libavutil/intreadwrite.h"
24+
25+#include "apetag.h"
26+#include "avformat.h"
27+#include "avio_internal.h"
28+#include "internal.h"
29+
30+typedef struct TTAMuxContext {
31+ AVIOContext *seek_table;
32+ AVIOContext *data;
33+ uint32_t nb_samples;
34+ int frame_size;
35+ int last_frame;
36+} TTAMuxContext;
37+
38+static int tta_write_header(AVFormatContext *s)
39+{
40+ TTAMuxContext *tta = s->priv_data;
41+ AVCodecParameters *par = s->streams[0]->codecpar;
42+ int ret;
43+
44+ if (s->nb_streams != 1) {
45+ av_log(s, AV_LOG_ERROR, "Only one stream is supported\n");
46+ return AVERROR(EINVAL);
47+ }
48+ if (par->codec_id != AV_CODEC_ID_TTA) {
49+ av_log(s, AV_LOG_ERROR, "Unsupported codec\n");
50+ return AVERROR(EINVAL);
51+ }
52+ if (par->extradata && par->extradata_size < 22) {
53+ av_log(s, AV_LOG_ERROR, "Invalid TTA extradata\n");
54+ return AVERROR_INVALIDDATA;
55+ }
56+
57+ if ((ret = avio_open_dyn_buf(&tta->seek_table)) < 0)
58+ return ret;
59+ if ((ret = avio_open_dyn_buf(&tta->data)) < 0) {
60+ ffio_free_dyn_buf(&tta->seek_table);
61+ return ret;
62+ }
63+
64+ /* Ignore most extradata information if present. It can be innacurate
65+ if for example remuxing from Matroska */
66+ ffio_init_checksum(s->pb, ff_crcEDB88320_update, UINT32_MAX);
67+ ffio_init_checksum(tta->seek_table, ff_crcEDB88320_update, UINT32_MAX);
68+ avio_write(s->pb, "TTA1", 4);
69+ avio_wl16(s->pb, par->extradata ? AV_RL16(par->extradata + 4) : 1);
70+ avio_wl16(s->pb, par->channels);
71+ avio_wl16(s->pb, par->bits_per_raw_sample);
72+ avio_wl32(s->pb, par->sample_rate);
73+ /* Prevent overflow */
74+ if (par->sample_rate > 0x7FFFFFu) {
75+ av_log(s, AV_LOG_ERROR, "Sample rate too large\n");
76+ return AVERROR(EINVAL);
77+ }
78+ tta->frame_size = par->sample_rate * 256 / 245;
79+ avpriv_set_pts_info(s->streams[0], 64, 1, par->sample_rate);
80+
81+ return 0;
82+}
83+
84+static int tta_write_packet(AVFormatContext *s, AVPacket *pkt)
85+{
86+ TTAMuxContext *tta = s->priv_data;
87+
88+ avio_write(tta->data, pkt->data, pkt->size);
89+ avio_wl32(tta->seek_table, pkt->size);
90+ tta->nb_samples += pkt->duration;
91+
92+ if (tta->frame_size != pkt->duration) {
93+ if (tta->last_frame) {
94+ /* Two frames with a different duration than the default frame
95+ size means the TTA stream comes from a faulty container, and
96+ there's no way the last frame duration will be correct. */
97+ av_log(s, AV_LOG_ERROR, "Invalid frame durations\n");
98+
99+ return AVERROR_INVALIDDATA;
100+ }
101+ /* First frame with a different duration than the default frame size.
102+ Assume it's the last frame in the stream and continue. */
103+ tta->last_frame++;
104+ }
105+
106+ return 0;
107+}
108+
109+static int tta_write_trailer(AVFormatContext *s)
110+{
111+ TTAMuxContext *tta = s->priv_data;
112+ uint8_t *ptr;
113+ unsigned int crc;
114+ int size;
115+
116+ avio_wl32(s->pb, tta->nb_samples);
117+ crc = ffio_get_checksum(s->pb) ^ UINT32_MAX;
118+ avio_wl32(s->pb, crc);
119+
120+ /* Write Seek table */
121+ crc = ffio_get_checksum(tta->seek_table) ^ UINT32_MAX;
122+ avio_wl32(tta->seek_table, crc);
123+ size = avio_close_dyn_buf(tta->seek_table, &ptr);
124+ avio_write(s->pb, ptr, size);
125+ av_free(ptr);
126+
127+ /* Write audio data */
128+ size = avio_close_dyn_buf(tta->data, &ptr);
129+ avio_write(s->pb, ptr, size);
130+ av_free(ptr);
131+
132+ ff_ape_write_tag(s);
133+ avio_flush(s->pb);
134+
135+ return 0;
136+}
137+
138+AVOutputFormat ff_tta_muxer = {
139+ .name = "tta",
140+ .long_name = NULL_IF_CONFIG_SMALL("TTA (True Audio)"),
141+ .mime_type = "audio/x-tta",
142+ .extensions = "tta",
143+ .priv_data_size = sizeof(TTAMuxContext),
144+ .audio_codec = AV_CODEC_ID_TTA,
145+ .video_codec = AV_CODEC_ID_NONE,
146+ .write_header = tta_write_header,
147+ .write_packet = tta_write_packet,
148+ .write_trailer = tta_write_trailer,
149+};
libavformat/version.h+1−1
@@ -32,7 +32,7 @@ · 31 unmodified lines
3232 // Major bumping may affect Ticket5467, 5421, 5451(compatibility with Chromium)
3333 // Also please add any ticket numbers that you belive might be affected here
3434 #define LIBAVFORMAT_VERSION_MAJOR 57
35−#define LIBAVFORMAT_VERSION_MINOR 44
35+#define LIBAVFORMAT_VERSION_MINOR 45
3636 #define LIBAVFORMAT_VERSION_MICRO 100
3737
3838 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
tests/fate/acodec.mak+2−2
@@ -154,8 +154,8 @@ FATE_ACODEC-$(call ENCDEC, WAVPACK, WV) += fate-acodec-wavpack · 153 unmodified lines
154154 fate-acodec-wavpack: FMT = wv
155155 fate-acodec-wavpack: CODEC = wavpack -compression_level 1
156156
157−FATE_ACODEC-$(call ENCDEC, TTA, MATROSKA) += fate-acodec-tta
158−fate-acodec-tta: FMT = matroska
157+FATE_ACODEC-$(call ENCDEC, TTA, TTA) += fate-acodec-tta
158+fate-acodec-tta: FMT = tta
159159
160160 FATE_ACODEC += $(FATE_ACODEC-yes)
161161
tests/fate/avformat.mak+2−0
@@ -15,6 +15,7 @@ FATE_LAVF-$(call ENCDEC, GIF, IMAGE2) += gif · 14 unmodified lines
1515 FATE_LAVF-$(call ENCDEC2, MPEG2VIDEO, PCM_S16LE, GXF) += gxf
1616 FATE_LAVF-$(call ENCDEC, PCM_S16LE, IRCAM) += ircam
1717 FATE_LAVF-$(call ENCDEC, MJPEG, IMAGE2) += jpg
18+FATE_LAVF-$(call ENCMUX, TTA, MATROSKA_AUDIO) += mka
1819 FATE_LAVF-$(call ENCDEC2, MPEG4, MP2, MATROSKA) += mkv
1920 FATE_LAVF-$(call ENCDEC, ADPCM_YAMAHA, MMF) += mmf
2021 FATE_LAVF-$(call ENCDEC2, MPEG4, PCM_ALAW, MOV) += mov ismv
@@ -44,6 +45,7 @@ FATE_LAVF-$(call ENCDEC, FLV, SWF) += swf · 23 unmodified lines
4445 FATE_LAVF-$(call ENCDEC, TARGA, IMAGE2) += tga
4546 FATE_LAVF-$(call ENCDEC, TIFF, IMAGE2) += tiff
4647 FATE_LAVF-$(call ENCDEC2, MPEG2VIDEO, MP2, MPEGTS) += ts
48+FATE_LAVF-$(call ENCDEC, TTA, TTA) += tta
4749 FATE_LAVF-$(call ENCDEC, PCM_U8, VOC) += voc
4850 FATE_LAVF-$(call ENCDEC, PCM_S16LE, VOC) += voc_s16
4951 FATE_LAVF-$(call ENCDEC, PCM_S16LE, WAV) += wav
tests/lavf-regression.sh+8−0
@@ -145,6 +145,10 @@ if [ -n "$do_nut" ] ; then · 144 unmodified lines
145145 do_lavf nut "" "-acodec mp2 -ab 64k -ar 44100 -threads 1"
146146 fi
147147
148+if [ -n "$do_mka" ] ; then
149+do_audio_only mka "" "-c:a tta"
150+fi
151+
148152 if [ -n "$do_mkv" ] ; then
149153 do_lavf mkv "" "-acodec mp2 -ab 64k -vcodec mpeg4 \
150154 -attach ${raw_src%/*}/00.pgm -metadata:s:t mimetype=image/x-portable-greymap -threads 1"
@@ -350,6 +354,10 @@ if [ -n "$do_sox" ] ; then · 199 unmodified lines
350354 do_audio_only sox
351355 fi
352356
357+if [ -n "$do_tta" ] ; then
358+do_audio_only tta
359+fi
360+
353361 if [ -n "$do_caf" ] ; then
354362 do_audio_only caf
355363 fi
tests/ref/acodec/tta+2−2
@@ -1,4 +1,4 @@
1−6c260836d7a32e4bd714453a3546c0d5 *tests/data/fate/acodec-tta.matroska
2−331148 tests/data/fate/acodec-tta.matroska
1+847d065f082ac94825728b5f1af853eb *tests/data/fate/acodec-tta.tta
2+330583 tests/data/fate/acodec-tta.tta
33 95e54b261530a1bcf6de6fe3b21dc5f6 *tests/data/fate/acodec-tta.out.wav
44 stddev: 0.00 PSNR:999.99 MAXDIFF: 0 bytes: 1058400/ 1058400
tests/ref/lavf/mka+3−0
@@ -0,0 +1,3 @@
1+b2e3746787b885d0191a1a26f3faa58f *./tests/data/lavf/lavf.mka
2+43654 ./tests/data/lavf/lavf.mka
3+./tests/data/lavf/lavf.mka CRC=0x3a1da17e
tests/ref/lavf/tta+3−0
@@ -0,0 +1,3 @@
1+f2721d06704ac43d89fdd25835b43598 *./tests/data/lavf/lavf.tta
2+43200 ./tests/data/lavf/lavf.tta
3+./tests/data/lavf/lavf.tta CRC=0x3a1da17e

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t