libavformat/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 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 "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 80;
40  return 0;
41 }
42 
44 {
45  TTAContext *c = s->priv_data;
46  AVStream *st;
47  int i, channels, bps, samplerate;
48  uint64_t framepos, start_offset;
49  uint32_t datalen;
50 
52  ff_id3v1_read(s);
53 
54  start_offset = avio_tell(s->pb);
55  if (avio_rl32(s->pb) != AV_RL32("TTA1"))
56  return -1; // not tta file
57 
58  avio_skip(s->pb, 2); // FIXME: flags
59  channels = avio_rl16(s->pb);
60  bps = avio_rl16(s->pb);
61  samplerate = avio_rl32(s->pb);
62  if(samplerate <= 0 || samplerate > 1000000){
63  av_log(s, AV_LOG_ERROR, "nonsense samplerate\n");
64  return -1;
65  }
66 
67  datalen = avio_rl32(s->pb);
68  if (!datalen) {
69  av_log(s, AV_LOG_ERROR, "invalid datalen\n");
70  return AVERROR_INVALIDDATA;
71  }
72 
73  avio_skip(s->pb, 4); // header crc
74 
75  c->frame_size = samplerate * 256 / 245;
76  c->last_frame_size = datalen % c->frame_size;
77  if (!c->last_frame_size)
79  c->totalframes = datalen / c->frame_size + (c->last_frame_size < c->frame_size);
80  c->currentframe = 0;
81 
82  if(c->totalframes >= UINT_MAX/sizeof(uint32_t) || c->totalframes <= 0){
83  av_log(s, AV_LOG_ERROR, "totalframes %d invalid\n", c->totalframes);
84  return -1;
85  }
86 
87  st = avformat_new_stream(s, NULL);
88  if (!st)
89  return AVERROR(ENOMEM);
90 
91  avpriv_set_pts_info(st, 64, 1, samplerate);
92  st->start_time = 0;
93  st->duration = datalen;
94 
95  framepos = avio_tell(s->pb) + 4*c->totalframes + 4;
96 
97  for (i = 0; i < c->totalframes; i++) {
98  uint32_t size = avio_rl32(s->pb);
99  av_add_index_entry(st, framepos, i * c->frame_size, size, 0,
101  framepos += size;
102  }
103  avio_skip(s->pb, 4); // seektable crc
104 
107  st->codec->channels = channels;
108  st->codec->sample_rate = samplerate;
110 
111  st->codec->extradata_size = avio_tell(s->pb) - start_offset;
113  //this check is redundant as avio_read should fail
114  av_log(s, AV_LOG_ERROR, "extradata_size too large\n");
115  return -1;
116  }
118  if (!st->codec->extradata) {
119  st->codec->extradata_size = 0;
120  return AVERROR(ENOMEM);
121  }
122  avio_seek(s->pb, start_offset, SEEK_SET);
124 
125  return 0;
126 }
127 
129 {
130  TTAContext *c = s->priv_data;
131  AVStream *st = s->streams[0];
132  int size, ret;
133 
134  // FIXME!
135  if (c->currentframe >= c->totalframes)
136  return AVERROR_EOF;
137 
138  size = st->index_entries[c->currentframe].size;
139 
140  ret = av_get_packet(s->pb, pkt, size);
141  pkt->dts = st->index_entries[c->currentframe++].timestamp;
142  pkt->duration = c->currentframe == c->totalframes ? c->last_frame_size :
143  c->frame_size;
144  return ret;
145 }
146 
147 static int tta_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
148 {
149  TTAContext *c = s->priv_data;
150  AVStream *st = s->streams[stream_index];
151  int index = av_index_search_timestamp(st, timestamp, flags);
152  if (index < 0)
153  return -1;
154  if (avio_seek(s->pb, st->index_entries[index].pos, SEEK_SET) < 0)
155  return -1;
156 
157  c->currentframe = index;
158 
159  return 0;
160 }
161 
163  .name = "tta",
164  .long_name = NULL_IF_CONFIG_SMALL("TTA (True Audio)"),
165  .priv_data_size = sizeof(TTAContext),
170  .extensions = "tta",
171 };
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
const char * s
Definition: avisynth_c.h:668
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
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.
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.
int64_t pos
Definition: avformat.h:592
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)
void ff_id3v1_read(AVFormatContext *s)
Read an ID3v1 tag.
Definition: id3v1.c:228
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:199
AVIndexEntry * index_entries
Only used if the format does not support seeking natively.
Definition: avformat.h:822
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:256
AVDictionaryEntry * av_dict_get(AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:39
set threshold d
Format I/O context.
Definition: avformat.h:944
Public dictionary API.
uint8_t
static AVPacket pkt
Definition: demuxing.c:56
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
AVStream ** streams
Definition: avformat.h:992
#define AVERROR_EOF
End of file.
Definition: error.h:55
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.
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:248
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:478
#define AVINDEX_KEYFRAME
Definition: avformat.h:599
AVDictionary * metadata
Definition: avformat.h:1092
int av_index_search_timestamp(AVStream *st, int64_t timestamp, int flags)
Get the index for a specific timestamp.
struct TTAContext TTAContext
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:579
int64_t timestamp
Timestamp in AVStream.time_base units, preferably the time from which on correctly decoded frames are...
Definition: avformat.h:593
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
int size
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:662
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:336
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
static int read_probe(AVProbeData *pd)
ret
Definition: avfilter.c:821
static int tta_probe(AVProbeData *p)
#define AV_RL32
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:517
Stream structure.
Definition: avformat.h:643
NULL
Definition: eval.c:55
enum AVMediaType codec_type
enum AVCodecID codec_id
int sample_rate
samples per second
AVIOContext * pb
I/O context.
Definition: avformat.h:977
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:114
int index
Definition: gxfenc.c:89
synthesis window for stochastic i
This structure contains the data a format has to probe a file.
Definition: avformat.h:334
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer 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.Buffer references ownership and permissions
static int flags
Definition: cpu.c:23
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:696
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:563
Main libavformat public API header.
static int tta_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
static int tta_read_header(AVFormatContext *s)
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base...
Definition: avformat.h:689
static double c[64]
AVInputFormat ff_tta_demuxer
unsigned bps
Definition: movenc.c:895
int channels
number of audio channels
void * priv_data
Format private data.
Definition: avformat.h:964
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:461
#define AV_DICT_IGNORE_SUFFIX
Definition: dict.h:68
This structure stores compressed data.