FFmpeg
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 
26 #include "apetag.h"
27 #include "avformat.h"
28 #include "avio_internal.h"
29 #include "internal.h"
30 #include "mux.h"
31 
32 typedef struct TTAMuxContext {
35  uint32_t nb_samples;
39 
41 {
42  TTAMuxContext *tta = s->priv_data;
43  AVCodecParameters *par;
44 
45  if (s->nb_streams != 1) {
46  av_log(s, AV_LOG_ERROR, "Only one stream is supported\n");
47  return AVERROR(EINVAL);
48  }
49  par = s->streams[0]->codecpar;
50 
51  if (par->codec_id != AV_CODEC_ID_TTA) {
52  av_log(s, AV_LOG_ERROR, "Unsupported codec\n");
53  return AVERROR(EINVAL);
54  }
55  if (par->extradata && par->extradata_size < 22) {
56  av_log(s, AV_LOG_ERROR, "Invalid TTA extradata\n");
57  return AVERROR_INVALIDDATA;
58  }
59 
60  /* Prevent overflow */
61  if (par->sample_rate > 0x7FFFFFu) {
62  av_log(s, AV_LOG_ERROR, "Sample rate too large\n");
63  return AVERROR(EINVAL);
64  }
65  tta->frame_size = par->sample_rate * 256 / 245;
66  avpriv_set_pts_info(s->streams[0], 64, 1, par->sample_rate);
67 
68  return 0;
69 }
70 
72 {
73  TTAMuxContext *tta = s->priv_data;
74  AVCodecParameters *par = s->streams[0]->codecpar;
75  int ret;
76 
77  if ((ret = avio_open_dyn_buf(&tta->seek_table)) < 0)
78  return ret;
79 
80  /* Ignore most extradata information if present. It can be innacurate
81  if for example remuxing from Matroska */
82  ffio_init_checksum(s->pb, ff_crcEDB88320_update, UINT32_MAX);
84  avio_write(s->pb, "TTA1", 4);
85  avio_wl16(s->pb, par->extradata ? AV_RL16(par->extradata + 4) : 1);
86  avio_wl16(s->pb, par->ch_layout.nb_channels);
87  avio_wl16(s->pb, par->bits_per_raw_sample);
88  avio_wl32(s->pb, par->sample_rate);
89 
90  return 0;
91 }
92 
94 {
95  TTAMuxContext *tta = s->priv_data;
96  int ret;
97 
98  ret = avpriv_packet_list_put(&tta->queue, pkt, NULL, 0);
99  if (ret < 0) {
100  return ret;
101  }
102  pkt = &tta->queue.tail->pkt;
103 
104  avio_wl32(tta->seek_table, pkt->size);
105  tta->nb_samples += pkt->duration;
106 
107  if (tta->frame_size != pkt->duration) {
108  if (tta->last_frame) {
109  /* Two frames with a different duration than the default frame
110  size means the TTA stream comes from a faulty container, and
111  there's no way the last frame duration will be correct. */
112  av_log(s, AV_LOG_ERROR, "Invalid frame durations\n");
113 
114  return AVERROR_INVALIDDATA;
115  }
116  /* First frame with a different duration than the default frame size.
117  Assume it's the last frame in the stream and continue. */
118  tta->last_frame++;
119  }
120 
121  return 0;
122 }
123 
125 {
126  TTAMuxContext *tta = s->priv_data;
127  AVPacket *const pkt = ffformatcontext(s)->pkt;
128 
129  while (tta->queue.head) {
131  avio_write(s->pb, pkt->data, pkt->size);
133  }
134 }
135 
137 {
138  TTAMuxContext *tta = s->priv_data;
139  uint8_t *ptr;
140  unsigned int crc;
141  int size;
142 
143  avio_wl32(s->pb, tta->nb_samples);
144  crc = ffio_get_checksum(s->pb) ^ UINT32_MAX;
145  avio_wl32(s->pb, crc);
146 
147  /* Write Seek table */
148  crc = ffio_get_checksum(tta->seek_table) ^ UINT32_MAX;
149  avio_wl32(tta->seek_table, crc);
150  size = avio_get_dyn_buf(tta->seek_table, &ptr);
151  avio_write(s->pb, ptr, size);
152 
153  /* Write audio data */
155 
157 
158  return 0;
159 }
160 
162 {
163  TTAMuxContext *tta = s->priv_data;
164 
167 }
168 
170  .p.name = "tta",
171  .p.long_name = NULL_IF_CONFIG_SMALL("TTA (True Audio)"),
172  .p.mime_type = "audio/x-tta",
173  .p.extensions = "tta",
174  .priv_data_size = sizeof(TTAMuxContext),
175  .p.audio_codec = AV_CODEC_ID_TTA,
176  .p.video_codec = AV_CODEC_ID_NONE,
177  .init = tta_init,
178  .deinit = tta_deinit,
179  .write_header = tta_write_header,
180  .write_packet = tta_write_packet,
181  .write_trailer = tta_write_trailer,
182 };
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:422
TTAMuxContext::frame_size
int frame_size
Definition: ttaenc.c:36
ff_tta_muxer
const FFOutputFormat ff_tta_muxer
Definition: ttaenc.c:169
avpriv_packet_list_put
int avpriv_packet_list_put(PacketList *packet_buffer, AVPacket *pkt, int(*copy)(AVPacket *dst, const AVPacket *src), int flags)
Append an AVPacket to the list.
Definition: avpacket.c:536
AVCodecParameters::extradata
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: codec_par.h:76
PacketList::head
PacketListEntry * head
Definition: packet_internal.h:32
avpriv_packet_list_get
int avpriv_packet_list_get(PacketList *pkt_buffer, AVPacket *pkt)
Remove the oldest AVPacket in the list and return it.
Definition: avpacket.c:575
AVOutputFormat::name
const char * name
Definition: avformat.h:508
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
tta_queue_flush
static void tta_queue_flush(AVFormatContext *s)
Definition: ttaenc.c:124
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: codec_par.h:54
ffformatcontext
static av_always_inline FFFormatContext * ffformatcontext(AVFormatContext *s)
Definition: internal.h:191
TTAMuxContext::queue
PacketList queue
Definition: ttaenc.c:34
apetag.h
AVPacket::data
uint8_t * data
Definition: packet.h:374
PacketList
Definition: packet_internal.h:31
TTAMuxContext::seek_table
AVIOContext * seek_table
Definition: ttaenc.c:33
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:392
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:311
avio_get_dyn_buf
int avio_get_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:1487
ffio_get_checksum
unsigned long ffio_get_checksum(AVIOContext *s)
Definition: aviobuf.c:604
FFOutputFormat::p
AVOutputFormat p
The public AVOutputFormat.
Definition: mux.h:34
tta_deinit
static void tta_deinit(AVFormatContext *s)
Definition: ttaenc.c:161
crc.h
avio_wl16
void avio_wl16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:458
avpriv_set_pts_info
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: avformat.c:771
AVCodecParameters::bits_per_raw_sample
int bits_per_raw_sample
This is the number of valid bits in each output sample.
Definition: codec_par.h:117
tta_init
static int tta_init(AVFormatContext *s)
Definition: ttaenc.c:40
tta_write_header
static int tta_write_header(AVFormatContext *s)
Definition: ttaenc.c:71
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
AV_CODEC_ID_TTA
@ AV_CODEC_ID_TTA
Definition: codec_id.h:460
avio_open_dyn_buf
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1475
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:256
AV_RL16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:94
ff_crcEDB88320_update
unsigned long ff_crcEDB88320_update(unsigned long checksum, const uint8_t *buf, unsigned int len)
Definition: aviobuf.c:592
PacketList::tail
PacketListEntry * tail
Definition: packet_internal.h:32
ff_ape_write_tag
int ff_ape_write_tag(AVFormatContext *s)
Write an APE tag into a file.
Definition: apetag.c:176
AVFormatContext
Format I/O context.
Definition: avformat.h:1104
internal.h
NULL
#define NULL
Definition: coverity.c:32
FFOutputFormat
Definition: mux.h:30
AVCodecParameters::ch_layout
AVChannelLayout ch_layout
Audio only.
Definition: codec_par.h:213
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:178
AVCodecParameters::extradata_size
int extradata_size
Size of the extradata content in bytes.
Definition: codec_par.h:80
AVIOContext
Bytestream IO Context.
Definition: avio.h:166
TTAMuxContext::nb_samples
uint32_t nb_samples
Definition: ttaenc.c:35
AVPacket::size
int size
Definition: packet.h:375
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:115
avpriv_packet_list_free
void avpriv_packet_list_free(PacketList *pkt_buf)
Wipe the list and unref all the packets in it.
Definition: avpacket.c:589
size
int size
Definition: twinvq_data.h:10344
tta_write_packet
static int tta_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: ttaenc.c:93
PacketListEntry::pkt
AVPacket pkt
Definition: packet_internal.h:28
ffio_init_checksum
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:612
tta_write_trailer
static int tta_write_trailer(AVFormatContext *s)
Definition: ttaenc.c:136
avio_write
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:222
avio_wl32
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:378
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: codec_id.h:50
avio_internal.h
ffio_free_dyn_buf
void ffio_free_dyn_buf(AVIOContext **s)
Free a dynamic buffer.
Definition: aviobuf.c:1548
ret
ret
Definition: filter_design.txt:187
avformat.h
TTAMuxContext
Definition: ttaenc.c:32
packet_internal.h
FFFormatContext::pkt
AVPacket * pkt
Used to hold temporary packets for the generic demuxing code.
Definition: internal.h:141
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:62
AVPacket
This structure stores compressed data.
Definition: packet.h:351
TTAMuxContext::last_frame
int last_frame
Definition: ttaenc.c:37
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
mux.h