]> git.sesse.net Git - ffmpeg/blob - libavformat/tta.c
change codec id if sample size field is set to 24 in stsd, fix Sony-mx5p.mov
[ffmpeg] / libavformat / tta.c
1 /*
2  * TTA demuxer
3  * Copyright (c) 2006 Alex Beregszaszi
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  */
19 #include "avformat.h"
20 #include "bitstream.h"
21
22 typedef struct {
23     int totalframes, currentframe;
24     uint32_t *seektable;
25 } TTAContext;
26
27 static int tta_probe(AVProbeData *p)
28 {
29     const uint8_t *d = p->buf;
30     if (p->buf_size < 4)
31         return 0;
32     if (d[0] == 'T' && d[1] == 'T' && d[2] == 'A' && d[3] == '1')
33         return 80;
34     return 0;
35 }
36
37 static int tta_read_header(AVFormatContext *s, AVFormatParameters *ap)
38 {
39     TTAContext *c = s->priv_data;
40     AVStream *st;
41     int i, channels, bps, samplerate, datalen, framelen, start;
42
43     start = url_ftell(&s->pb);
44
45     if (get_le32(&s->pb) != ff_get_fourcc("TTA1"))
46         return -1; // not tta file
47
48     url_fskip(&s->pb, 2); // FIXME: flags
49     channels = get_le16(&s->pb);
50     bps = get_le16(&s->pb);
51     samplerate = get_le32(&s->pb);
52     if(samplerate <= 0 || samplerate > 1000000){
53         av_log(s, AV_LOG_ERROR, "nonsense samplerate\n");
54         return -1;
55     }
56
57     datalen = get_le32(&s->pb);
58     if(datalen < 0){
59         av_log(s, AV_LOG_ERROR, "nonsense datalen\n");
60         return -1;
61     }
62
63     url_fskip(&s->pb, 4); // header crc
64
65     framelen = 1.04489795918367346939 * samplerate;
66     c->totalframes = datalen / framelen + ((datalen % framelen) ? 1 : 0);
67     c->currentframe = 0;
68
69     if(c->totalframes >= UINT_MAX/sizeof(uint32_t)){
70         av_log(s, AV_LOG_ERROR, "totalframes too large\n");
71         return -1;
72     }
73     c->seektable = av_mallocz(sizeof(uint32_t)*c->totalframes);
74     if (!c->seektable)
75         return AVERROR_NOMEM;
76
77     for (i = 0; i < c->totalframes; i++)
78             c->seektable[i] = get_le32(&s->pb);
79     url_fskip(&s->pb, 4); // seektable crc
80
81     st = av_new_stream(s, 0);
82 //    av_set_pts_info(st, 32, 1, 1000);
83     if (!st)
84         return AVERROR_NOMEM;
85     st->codec->codec_type = CODEC_TYPE_AUDIO;
86     st->codec->codec_id = CODEC_ID_TTA;
87     st->codec->channels = channels;
88     st->codec->sample_rate = samplerate;
89     st->codec->bits_per_sample = bps;
90
91     st->codec->extradata_size = url_ftell(&s->pb) - start;
92     if(st->codec->extradata_size+FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)st->codec->extradata_size){
93         //this check is redundant as get_buffer should fail
94         av_log(s, AV_LOG_ERROR, "extradata_size too large\n");
95         return -1;
96     }
97     st->codec->extradata = av_mallocz(st->codec->extradata_size+FF_INPUT_BUFFER_PADDING_SIZE);
98     url_fseek(&s->pb, start, SEEK_SET); // or SEEK_CUR and -size ? :)
99     get_buffer(&s->pb, st->codec->extradata, st->codec->extradata_size);
100
101     return 0;
102 }
103
104 static int tta_read_packet(AVFormatContext *s, AVPacket *pkt)
105 {
106     TTAContext *c = s->priv_data;
107     int ret, size;
108
109     // FIXME!
110     if (c->currentframe > c->totalframes)
111         size = 0;
112     else
113         size = c->seektable[c->currentframe];
114
115     c->currentframe++;
116
117     if (av_new_packet(pkt, size) < 0)
118         return AVERROR_IO;
119
120     pkt->pos = url_ftell(&s->pb);
121     pkt->stream_index = 0;
122     ret = get_buffer(&s->pb, pkt->data, size);
123     if (ret <= 0) {
124         av_free_packet(pkt);
125         return AVERROR_IO;
126     }
127     pkt->size = ret;
128 //    av_log(s, AV_LOG_INFO, "TTA packet #%d desired size: %d read size: %d at pos %d\n",
129 //        c->currentframe, size, ret, pkt->pos);
130     return 0; //ret;
131 }
132
133 static int tta_read_close(AVFormatContext *s)
134 {
135     TTAContext *c = s->priv_data;
136     if (c->seektable)
137         av_free(c->seektable);
138     return 0;
139 }
140
141 AVInputFormat tta_demuxer = {
142     "tta",
143     "true-audio",
144     sizeof(TTAContext),
145     tta_probe,
146     tta_read_header,
147     tta_read_packet,
148     tta_read_close,
149     .extensions = "tta",
150 };