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