Libav
tta.c
Go to the documentation of this file.
1 /*
2  * TTA demuxer
3  * Copyright (c) 2006 Alex Beregszaszi
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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/dict.h"
23 #include "libavutil/intreadwrite.h"
24 
25 #include "avformat.h"
26 #include "internal.h"
27 #include "id3v1.h"
28 
29 typedef struct TTAContext {
33 } TTAContext;
34 
35 static int tta_probe(AVProbeData *p)
36 {
37  const uint8_t *d = p->buf;
38 
39  if (d[0] == 'T' && d[1] == 'T' && d[2] == 'A' && d[3] == '1')
40  return AVPROBE_SCORE_EXTENSION + 30;
41  return 0;
42 }
43 
45 {
46  TTAContext *c = s->priv_data;
47  AVStream *st;
48  int i, channels, bps, samplerate, datalen;
49  int64_t framepos, start_offset;
50 
52  ff_id3v1_read(s);
53 
54  start_offset = avio_tell(s->pb);
55  if (start_offset < 0)
56  return start_offset;
57  if (avio_rl32(s->pb) != AV_RL32("TTA1"))
58  return -1; // not tta file
59 
60  avio_skip(s->pb, 2); // FIXME: flags
61  channels = avio_rl16(s->pb);
62  bps = avio_rl16(s->pb);
63  samplerate = avio_rl32(s->pb);
64  if(samplerate <= 0 || samplerate > 1000000){
65  av_log(s, AV_LOG_ERROR, "nonsense samplerate\n");
66  return -1;
67  }
68 
69  datalen = avio_rl32(s->pb);
70  if(datalen < 0){
71  av_log(s, AV_LOG_ERROR, "nonsense datalen\n");
72  return -1;
73  }
74 
75  avio_skip(s->pb, 4); // header crc
76 
77  c->frame_size = samplerate * 256 / 245;
78  c->last_frame_size = datalen % c->frame_size;
79  if (!c->last_frame_size)
81  c->totalframes = datalen / c->frame_size + (c->last_frame_size < c->frame_size);
82  c->currentframe = 0;
83 
84  if(c->totalframes >= UINT_MAX/sizeof(uint32_t)){
85  av_log(s, AV_LOG_ERROR, "totalframes too large\n");
86  return -1;
87  }
88 
89  st = avformat_new_stream(s, NULL);
90  if (!st)
91  return AVERROR(ENOMEM);
92 
93  avpriv_set_pts_info(st, 64, 1, samplerate);
94  st->start_time = 0;
95  st->duration = datalen;
96 
97  framepos = avio_tell(s->pb);
98  if (framepos < 0)
99  return framepos;
100  framepos += 4 * c->totalframes + 4;
101 
102  for (i = 0; i < c->totalframes; i++) {
103  uint32_t size = avio_rl32(s->pb);
104  av_add_index_entry(st, framepos, i * c->frame_size, size, 0,
106  framepos += size;
107  }
108  avio_skip(s->pb, 4); // seektable crc
109 
112  st->codecpar->channels = channels;
113  st->codecpar->sample_rate = samplerate;
115 
116  st->codecpar->extradata_size = avio_tell(s->pb) - start_offset;
118  //this check is redundant as avio_read should fail
119  av_log(s, AV_LOG_ERROR, "extradata_size too large\n");
120  return -1;
121  }
123  if (!st->codecpar->extradata) {
124  st->codecpar->extradata_size = 0;
125  return AVERROR(ENOMEM);
126  }
127  avio_seek(s->pb, start_offset, SEEK_SET);
129 
130  return 0;
131 }
132 
134 {
135  TTAContext *c = s->priv_data;
136  AVStream *st = s->streams[0];
137  int size, ret;
138 
139  // FIXME!
140  if (c->currentframe >= c->totalframes)
141  return AVERROR_EOF;
142 
143  size = st->index_entries[c->currentframe].size;
144 
145  ret = av_get_packet(s->pb, pkt, size);
146  pkt->dts = st->index_entries[c->currentframe++].timestamp;
147  pkt->duration = c->currentframe == c->totalframes ? c->last_frame_size :
148  c->frame_size;
149  return ret;
150 }
151 
152 static int tta_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
153 {
154  TTAContext *c = s->priv_data;
155  AVStream *st = s->streams[stream_index];
156  int index = av_index_search_timestamp(st, timestamp, flags);
157  if (index < 0)
158  return -1;
159  if (avio_seek(s->pb, st->index_entries[index].pos, SEEK_SET) < 0)
160  return -1;
161 
162  c->currentframe = index;
163 
164  return 0;
165 }
166 
168  .name = "tta",
169  .long_name = NULL_IF_CONFIG_SMALL("TTA (True Audio)"),
170  .priv_data_size = sizeof(TTAContext),
175  .extensions = "tta",
176 };
int size
int av_add_index_entry(AVStream *st, int64_t pos, int64_t timestamp, int size, int distance, int flags)
Add an index entry into a sorted list.
Definition: utils.c:1214
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:2986
int64_t pos
Definition: avformat.h:664
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition: libcdio.c:153
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3483
static int tta_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: tta.c:133
void ff_id3v1_read(AVFormatContext *s)
Read an ID3v1 tag.
Definition: id3v1.c:227
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:242
AVIndexEntry * index_entries
Only used if the format does not support seeking natively.
Definition: avformat.h:890
Definition: tta.c:60
int bps
Definition: tta.c:65
Format I/O context.
Definition: avformat.h:940
Public dictionary API.
uint8_t
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1364
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2648
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1008
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:38
static int flags
Definition: log.c:50
#define AVERROR_EOF
End of file.
Definition: error.h:51
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition: utils.c:117
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:295
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:545
int frame_size
Definition: tta.c:31
#define AVINDEX_KEYFRAME
Definition: avformat.h:666
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1148
int av_index_search_timestamp(AVStream *st, int64_t timestamp, int flags)
Get the index for a specific timestamp.
Definition: utils.c:1255
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:665
#define AVERROR(e)
Definition: error.h:43
int64_t timestamp
Definition: avformat.h:665
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:148
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3479
int totalframes
Definition: tta.c:30
int extradata_size
Size of the extradata content in bytes.
Definition: avcodec.h:3501
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:400
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
static int tta_probe(AVProbeData *p)
Definition: tta.c:35
static av_always_inline int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: avio.h:286
#define AV_RL32
Definition: intreadwrite.h:146
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:546
Stream structure.
Definition: avformat.h:705
NULL
Definition: eval.c:55
AVIOContext * pb
I/O context.
Definition: avformat.h:982
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:114
int index
Definition: gxfenc.c:72
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:405
This structure contains the data a format has to probe a file.
Definition: avformat.h:398
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:757
int sample_rate
Audio only.
Definition: avcodec.h:3564
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:649
int channels
Definition: tta.c:65
Main libavformat public API header.
static int tta_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition: tta.c:152
static int tta_read_header(AVFormatContext *s)
Definition: tta.c:44
int64_t start_time
Decoding: pts of the first frame of the stream, in stream time base.
Definition: avformat.h:750
AVInputFormat ff_tta_demuxer
Definition: tta.c:167
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:638
void * priv_data
Format private data.
Definition: avformat.h:968
int bits_per_coded_sample
Definition: avcodec.h:3514
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: avcodec.h:3497
int channels
Audio only.
Definition: avcodec.h:3560
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1345
int last_frame_size
Definition: tta.c:32
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:529
#define AV_DICT_IGNORE_SUFFIX
Definition: dict.h:60
AVCodecParameters * codecpar
Definition: avformat.h:831
int currentframe
Definition: tta.c:30
This structure stores compressed data.
Definition: avcodec.h:1323
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:211