FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ttaenc.c
Go to the documentation of this file.
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 {
33  uint32_t nb_samples;
37 
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) {
61  return ret;
62  }
63 
64  /* Ignore most extradata information if present. It can be innacurate
65  if for example remuxing from Matroska */
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);
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 
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 
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 
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,
149 };
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue)
Definition: ffmpeg.c:672
void avio_wl16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:458
const char * s
Definition: avisynth_c.h:768
Bytestream IO Context.
Definition: avio.h:155
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:1319
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4601
static int tta_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: ttaenc.c:84
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:4066
int size
Definition: avcodec.h:1658
static AVPacket pkt
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:87
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1290
This struct describes the properties of an encoded stream.
Definition: avcodec.h:4058
AVOutputFormat ff_tta_muxer
Definition: ttaenc.c:138
Format I/O context.
Definition: avformat.h:1349
static int tta_write_trailer(AVFormatContext *s)
Definition: ttaenc.c:109
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:358
uint8_t
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1675
unsigned long ff_crcEDB88320_update(unsigned long checksum, const uint8_t *buf, unsigned int len)
Definition: aviobuf.c:575
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1417
Public header for CRC hash function implementation.
uint8_t * data
Definition: avcodec.h:1657
ptrdiff_t size
Definition: opengl_enc.c:101
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:205
#define av_log(a,...)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:179
int extradata_size
Size of the extradata content in bytes.
Definition: avcodec.h:4084
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1405
void ffio_init_checksum(AVIOContext *s, unsigned long(*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len), unsigned long checksum)
Definition: aviobuf.c:595
int void avio_flush(AVIOContext *s)
Force flushing of buffered data.
Definition: aviobuf.c:225
static int tta_write_header(AVFormatContext *s)
Definition: ttaenc.c:38
AVIOContext * seek_table
Definition: ttaenc.c:31
static int write_trailer(AVFormatContext *s1)
Definition: v4l2enc.c:94
const char * name
Definition: avformat.h:524
void ffio_free_dyn_buf(AVIOContext **s)
Free a dynamic buffer.
Definition: aviobuf.c:1347
AVIOContext * pb
I/O context.
Definition: avformat.h:1391
uint32_t nb_samples
Definition: ttaenc.c:33
int ff_ape_write_tag(AVFormatContext *s)
Write an APE tag into a file.
Definition: apetag.c:185
unsigned long ffio_get_checksum(AVIOContext *s)
Definition: aviobuf.c:587
int bits_per_raw_sample
This is the number of valid bits in each output sample.
Definition: avcodec.h:4121
int sample_rate
Audio only.
Definition: avcodec.h:4176
Main libavformat public API header.
#define av_free(p)
void * priv_data
Format private data.
Definition: avformat.h:1377
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:337
int last_frame
Definition: ttaenc.c:35
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: avcodec.h:4080
int channels
Audio only.
Definition: avcodec.h:4172
int frame_size
Definition: ttaenc.c:34
AVIOContext * data
Definition: ttaenc.c:32
AVCodecParameters * codecpar
Definition: avformat.h:1252
This structure stores compressed data.
Definition: avcodec.h:1634