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 "libavcodec/get_bits.h"
23 #include "avformat.h"
24 #include "internal.h"
25 #include "id3v1.h"
26 #include "libavutil/dict.h"
27 
28 typedef struct {
29  int totalframes, currentframe;
32 } TTAContext;
33 
34 static int tta_probe(AVProbeData *p)
35 {
36  const uint8_t *d = p->buf;
37 
38  if (d[0] == 'T' && d[1] == 'T' && d[2] == 'A' && d[3] == '1')
39  return AVPROBE_SCORE_EXTENSION + 30;
40  return 0;
41 }
42 
44 {
45  TTAContext *c = s->priv_data;
46  AVStream *st;
47  int i, channels, bps, samplerate, datalen;
48  uint64_t framepos, start_offset;
49 
51  ff_id3v1_read(s);
52 
53  start_offset = avio_tell(s->pb);
54  if (avio_rl32(s->pb) != AV_RL32("TTA1"))
55  return -1; // not tta file
56 
57  avio_skip(s->pb, 2); // FIXME: flags
58  channels = avio_rl16(s->pb);
59  bps = avio_rl16(s->pb);
60  samplerate = avio_rl32(s->pb);
61  if(samplerate <= 0 || samplerate > 1000000){
62  av_log(s, AV_LOG_ERROR, "nonsense samplerate\n");
63  return -1;
64  }
65 
66  datalen = avio_rl32(s->pb);
67  if(datalen < 0){
68  av_log(s, AV_LOG_ERROR, "nonsense datalen\n");
69  return -1;
70  }
71 
72  avio_skip(s->pb, 4); // header crc
73 
74  c->frame_size = samplerate * 256 / 245;
75  c->last_frame_size = datalen % c->frame_size;
76  if (!c->last_frame_size)
78  c->totalframes = datalen / c->frame_size + (c->last_frame_size < c->frame_size);
79  c->currentframe = 0;
80 
81  if(c->totalframes >= UINT_MAX/sizeof(uint32_t)){
82  av_log(s, AV_LOG_ERROR, "totalframes too large\n");
83  return -1;
84  }
85 
86  st = avformat_new_stream(s, NULL);
87  if (!st)
88  return AVERROR(ENOMEM);
89 
90  avpriv_set_pts_info(st, 64, 1, samplerate);
91  st->start_time = 0;
92  st->duration = datalen;
93 
94  framepos = avio_tell(s->pb) + 4*c->totalframes + 4;
95 
96  for (i = 0; i < c->totalframes; i++) {
97  uint32_t size = avio_rl32(s->pb);
98  av_add_index_entry(st, framepos, i * c->frame_size, size, 0,
100  framepos += size;
101  }
102  avio_skip(s->pb, 4); // seektable crc
103 
106  st->codec->channels = channels;
107  st->codec->sample_rate = samplerate;
109 
110  st->codec->extradata_size = avio_tell(s->pb) - start_offset;
112  //this check is redundant as avio_read should fail
113  av_log(s, AV_LOG_ERROR, "extradata_size too large\n");
114  return -1;
115  }
117  if (!st->codec->extradata) {
118  st->codec->extradata_size = 0;
119  return AVERROR(ENOMEM);
120  }
121  avio_seek(s->pb, start_offset, SEEK_SET);
123 
124  return 0;
125 }
126 
128 {
129  TTAContext *c = s->priv_data;
130  AVStream *st = s->streams[0];
131  int size, ret;
132 
133  // FIXME!
134  if (c->currentframe >= c->totalframes)
135  return AVERROR_EOF;
136 
137  size = st->index_entries[c->currentframe].size;
138 
139  ret = av_get_packet(s->pb, pkt, size);
140  pkt->dts = st->index_entries[c->currentframe++].timestamp;
141  pkt->duration = c->currentframe == c->totalframes ? c->last_frame_size :
142  c->frame_size;
143  return ret;
144 }
145 
146 static int tta_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
147 {
148  TTAContext *c = s->priv_data;
149  AVStream *st = s->streams[stream_index];
150  int index = av_index_search_timestamp(st, timestamp, flags);
151  if (index < 0)
152  return -1;
153  if (avio_seek(s->pb, st->index_entries[index].pos, SEEK_SET) < 0)
154  return -1;
155 
156  c->currentframe = index;
157 
158  return 0;
159 }
160 
162  .name = "tta",
163  .long_name = NULL_IF_CONFIG_SMALL("TTA (True Audio)"),
164  .priv_data_size = sizeof(TTAContext),
169  .extensions = "tta",
170 };
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:1181
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:2821
int64_t pos
Definition: avformat.h:660
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition: libcdio.c:153
static int tta_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: tta.c:127
void ff_id3v1_read(AVFormatContext *s)
Read an ID3v1 tag.
Definition: id3v1.c:227
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:186
AVIndexEntry * index_entries
Only used if the format does not support seeking natively.
Definition: avformat.h:878
Definition: tta.c:58
Format I/O context.
Definition: avformat.h:922
Public dictionary API.
uint8_t
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1164
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2518
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:990
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:44
#define AVERROR_EOF
End of file.
Definition: error.h:51
bitstream reader API header.
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:219
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:2507
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:991
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:452
int frame_size
Definition: tta.c:30
#define AVINDEX_KEYFRAME
Definition: avformat.h:662
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1130
int av_index_search_timestamp(AVStream *st, int64_t timestamp, int flags)
Get the index for a specific timestamp.
Definition: utils.c:1222
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:564
#define AVERROR(e)
Definition: error.h:43
int64_t timestamp
Definition: avformat.h:661
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:150
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:168
int totalframes
Definition: tta.c:29
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:718
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:397
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:531
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
static int tta_probe(AVProbeData *p)
Definition: tta.c:34
static av_always_inline int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: avio.h:210
#define AV_RL32
Definition: intreadwrite.h:146
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:544
Stream structure.
Definition: avformat.h:699
NULL
Definition: eval.c:55
enum AVMediaType codec_type
Definition: avcodec.h:1058
enum AVCodecID codec_id
Definition: avcodec.h:1067
int sample_rate
samples per second
Definition: avcodec.h:1791
AVIOContext * pb
I/O context.
Definition: avformat.h:964
int extradata_size
Definition: avcodec.h:1165
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:402
This structure contains the data a format has to probe a file.
Definition: avformat.h:395
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:756
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:548
Main libavformat public API header.
static int tta_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition: tta.c:146
static int tta_read_header(AVFormatContext *s)
Definition: tta.c:43
int64_t start_time
Decoding: pts of the first frame of the stream, in stream time base.
Definition: avformat.h:749
AVInputFormat ff_tta_demuxer
Definition: tta.c:161
unsigned bps
Definition: movenc.c:845
int channels
number of audio channels
Definition: avcodec.h:1792
void * priv_data
Format private data.
Definition: avformat.h:950
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:972
int last_frame_size
Definition: tta.c:31
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:525
#define AV_DICT_IGNORE_SUFFIX
Definition: dict.h:62
int currentframe
Definition: tta.c:29
This structure stores compressed data.
Definition: avcodec.h:950
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:205