]> git.sesse.net Git - ffmpeg/blob - libavformat/mpc.c
Change all functions referenced in the mxf_metadata_read_table to use the same
[ffmpeg] / libavformat / mpc.c
1 /*
2  * Musepack demuxer
3  * Copyright (c) 2006 Konstantin Shishkov
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 "id3v2.h"
25 #include "apetag.h"
26
27 #define MPC_FRAMESIZE  1152
28 #define DELAY_FRAMES   32
29
30 static const int mpc_rate[4] = { 44100, 48000, 37800, 32000 };
31 typedef struct {
32     int64_t pos;
33     int size, skip;
34 }MPCFrame;
35
36 typedef struct {
37     int ver;
38     uint32_t curframe, lastframe;
39     uint32_t fcount;
40     MPCFrame *frames;
41     int curbits;
42     int frames_noted;
43 } MPCContext;
44
45 static int mpc_probe(AVProbeData *p)
46 {
47     const uint8_t *d = p->buf;
48     if (ff_id3v2_match(d, ID3v2_DEFAULT_MAGIC)) {
49         d += ff_id3v2_tag_len(d);
50     }
51     if (d+3 < p->buf+p->buf_size)
52     if (d[0] == 'M' && d[1] == 'P' && d[2] == '+' && (d[3] == 0x17 || d[3] == 0x7))
53         return AVPROBE_SCORE_MAX;
54     return 0;
55 }
56
57 static int mpc_read_header(AVFormatContext *s, AVFormatParameters *ap)
58 {
59     MPCContext *c = s->priv_data;
60     AVStream *st;
61     int t, ret;
62     int64_t pos = url_ftell(s->pb);
63
64     t = get_le24(s->pb);
65     if(t != MKTAG('M', 'P', '+', 0)){
66         uint8_t buf[ID3v2_HEADER_SIZE];
67         if (url_fseek(s->pb, pos, SEEK_SET) < 0)
68             return -1;
69         ret = get_buffer(s->pb, buf, ID3v2_HEADER_SIZE);
70         if (ret != ID3v2_HEADER_SIZE || !ff_id3v2_match(buf, ID3v2_DEFAULT_MAGIC)) {
71             av_log(s, AV_LOG_ERROR, "Not a Musepack file\n");
72             return -1;
73         }
74         /* skip ID3 tags and try again */
75         t = ff_id3v2_tag_len(buf) - ID3v2_HEADER_SIZE;
76         av_log(s, AV_LOG_DEBUG, "Skipping %d(%X) bytes of ID3 data\n", t, t);
77         url_fskip(s->pb, t);
78         if(get_le24(s->pb) != MKTAG('M', 'P', '+', 0)){
79             av_log(s, AV_LOG_ERROR, "Not a Musepack file\n");
80             return -1;
81         }
82         /* read ID3 tags */
83         if (url_fseek(s->pb, pos, SEEK_SET) < 0)
84             return -1;
85         ff_id3v2_read(s, ID3v2_DEFAULT_MAGIC);
86         get_le24(s->pb);
87     }
88     c->ver = get_byte(s->pb);
89     if(c->ver != 0x07 && c->ver != 0x17){
90         av_log(s, AV_LOG_ERROR, "Can demux Musepack SV7, got version %02X\n", c->ver);
91         return -1;
92     }
93     c->fcount = get_le32(s->pb);
94     if((int64_t)c->fcount * sizeof(MPCFrame) >= UINT_MAX){
95         av_log(s, AV_LOG_ERROR, "Too many frames, seeking is not possible\n");
96         return -1;
97     }
98     c->frames = av_malloc(c->fcount * sizeof(MPCFrame));
99     c->curframe = 0;
100     c->lastframe = -1;
101     c->curbits = 8;
102     c->frames_noted = 0;
103
104     st = av_new_stream(s, 0);
105     if (!st)
106         return AVERROR(ENOMEM);
107     st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
108     st->codec->codec_id = CODEC_ID_MUSEPACK7;
109     st->codec->channels = 2;
110     st->codec->bits_per_coded_sample = 16;
111
112     st->codec->extradata_size = 16;
113     st->codec->extradata = av_mallocz(st->codec->extradata_size+FF_INPUT_BUFFER_PADDING_SIZE);
114     get_buffer(s->pb, st->codec->extradata, 16);
115     st->codec->sample_rate = mpc_rate[st->codec->extradata[2] & 3];
116     av_set_pts_info(st, 32, MPC_FRAMESIZE, st->codec->sample_rate);
117     /* scan for seekpoints */
118     st->start_time = 0;
119     st->duration = c->fcount;
120
121     /* try to read APE tags */
122     if (!url_is_streamed(s->pb)) {
123         int64_t pos = url_ftell(s->pb);
124         ff_ape_parse_tag(s);
125         url_fseek(s->pb, pos, SEEK_SET);
126     }
127
128     return 0;
129 }
130
131 static int mpc_read_packet(AVFormatContext *s, AVPacket *pkt)
132 {
133     MPCContext *c = s->priv_data;
134     int ret, size, size2, curbits, cur = c->curframe;
135     int64_t tmp, pos;
136
137     if (c->curframe >= c->fcount)
138         return -1;
139
140     if(c->curframe != c->lastframe + 1){
141         url_fseek(s->pb, c->frames[c->curframe].pos, SEEK_SET);
142         c->curbits = c->frames[c->curframe].skip;
143     }
144     c->lastframe = c->curframe;
145     c->curframe++;
146     curbits = c->curbits;
147     pos = url_ftell(s->pb);
148     tmp = get_le32(s->pb);
149     if(curbits <= 12){
150         size2 = (tmp >> (12 - curbits)) & 0xFFFFF;
151     }else{
152         tmp = (tmp << 32) | get_le32(s->pb);
153         size2 = (tmp >> (44 - curbits)) & 0xFFFFF;
154     }
155     curbits += 20;
156     url_fseek(s->pb, pos, SEEK_SET);
157
158     size = ((size2 + curbits + 31) & ~31) >> 3;
159     if(cur == c->frames_noted){
160         c->frames[cur].pos = pos;
161         c->frames[cur].size = size;
162         c->frames[cur].skip = curbits - 20;
163         av_add_index_entry(s->streams[0], cur, cur, size, 0, AVINDEX_KEYFRAME);
164         c->frames_noted++;
165     }
166     c->curbits = (curbits + size2) & 0x1F;
167
168     if (av_new_packet(pkt, size) < 0)
169         return AVERROR(EIO);
170
171     pkt->data[0] = curbits;
172     pkt->data[1] = (c->curframe > c->fcount);
173     pkt->data[2] = 0;
174     pkt->data[3] = 0;
175
176     pkt->stream_index = 0;
177     pkt->pts = cur;
178     ret = get_buffer(s->pb, pkt->data + 4, size);
179     if(c->curbits)
180         url_fseek(s->pb, -4, SEEK_CUR);
181     if(ret < size){
182         av_free_packet(pkt);
183         return AVERROR(EIO);
184     }
185     pkt->size = ret + 4;
186
187     return 0;
188 }
189
190 static int mpc_read_close(AVFormatContext *s)
191 {
192     MPCContext *c = s->priv_data;
193
194     av_freep(&c->frames);
195     return 0;
196 }
197
198 /**
199  * Seek to the given position
200  * If position is unknown but is within the limits of file
201  * then packets are skipped unless desired position is reached
202  *
203  * Also this function makes use of the fact that timestamp == frameno
204  */
205 static int mpc_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
206 {
207     AVStream *st = s->streams[stream_index];
208     MPCContext *c = s->priv_data;
209     AVPacket pkt1, *pkt = &pkt1;
210     int ret;
211     int index = av_index_search_timestamp(st, timestamp - DELAY_FRAMES, flags);
212     uint32_t lastframe;
213
214     /* if found, seek there */
215     if (index >= 0){
216         c->curframe = st->index_entries[index].pos;
217         return 0;
218     }
219     /* if timestamp is out of bounds, return error */
220     if(timestamp < 0 || timestamp >= c->fcount)
221         return -1;
222     timestamp -= DELAY_FRAMES;
223     /* seek to the furthest known position and read packets until
224        we reach desired position */
225     lastframe = c->curframe;
226     if(c->frames_noted) c->curframe = c->frames_noted - 1;
227     while(c->curframe < timestamp){
228         ret = av_read_frame(s, pkt);
229         if (ret < 0){
230             c->curframe = lastframe;
231             return -1;
232         }
233         av_free_packet(pkt);
234     }
235     return 0;
236 }
237
238
239 AVInputFormat mpc_demuxer = {
240     "mpc",
241     NULL_IF_CONFIG_SMALL("Musepack"),
242     sizeof(MPCContext),
243     mpc_probe,
244     mpc_read_header,
245     mpc_read_packet,
246     mpc_read_close,
247     mpc_read_seek,
248     .extensions = "mpc",
249 };