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