Mercurial > libavformat.hg
annotate mpc.c @ 5135:cdaab3d9f0d6 libavformat
Parse APE metadata tags in Musepack SV7 files
Patch by Matti Hamalainen (to get his mail address remove common endings from
"mhamalainen@studentsnen.oamknen.finen")
Thread: [PATCH]5/6 Add APE tag metadata reading support in Musepack SV7 demuxer
| author | kostya |
|---|---|
| date | Tue, 11 Aug 2009 17:18:10 +0000 |
| parents | 304a0ea063f0 |
| children | e56e03b13237 |
| rev | line source |
|---|---|
| 1602 | 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 | |
| 2217 | 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 1602 | 20 */ |
| 3286 | 21 |
| 4872 | 22 #include "libavcodec/get_bits.h" |
| 1602 | 23 #include "avformat.h" |
| 4254 | 24 #include "id3v2.h" |
| 5135 | 25 #include "apetag.h" |
| 1602 | 26 |
| 27 #define MPC_FRAMESIZE 1152 | |
|
1610
cde17266ad08
Decode previous 32 frames to avoid seeking artifacts in MPC
kostya
parents:
1609
diff
changeset
|
28 #define DELAY_FRAMES 32 |
| 1602 | 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; | |
| 4254 | 48 if (ff_id3v2_match(d)) { |
| 49 d += ff_id3v2_tag_len(d); | |
| 50 } | |
| 51 if (d+3 < p->buf+p->buf_size) | |
| 1602 | 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; | |
|
1609
9adbec516265
Make MPC demuxer deal with ID3 tags at the beginning
kostya
parents:
1605
diff
changeset
|
61 int t; |
| 1602 | 62 |
|
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2274
diff
changeset
|
63 t = get_le24(s->pb); |
|
1609
9adbec516265
Make MPC demuxer deal with ID3 tags at the beginning
kostya
parents:
1605
diff
changeset
|
64 if(t != MKTAG('M', 'P', '+', 0)){ |
|
9adbec516265
Make MPC demuxer deal with ID3 tags at the beginning
kostya
parents:
1605
diff
changeset
|
65 if(t != MKTAG('I', 'D', '3', 0)){ |
|
9adbec516265
Make MPC demuxer deal with ID3 tags at the beginning
kostya
parents:
1605
diff
changeset
|
66 av_log(s, AV_LOG_ERROR, "Not a Musepack file\n"); |
|
9adbec516265
Make MPC demuxer deal with ID3 tags at the beginning
kostya
parents:
1605
diff
changeset
|
67 return -1; |
|
9adbec516265
Make MPC demuxer deal with ID3 tags at the beginning
kostya
parents:
1605
diff
changeset
|
68 } |
|
9adbec516265
Make MPC demuxer deal with ID3 tags at the beginning
kostya
parents:
1605
diff
changeset
|
69 /* skip ID3 tags and try again */ |
|
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2274
diff
changeset
|
70 url_fskip(s->pb, 3); |
|
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2274
diff
changeset
|
71 t = get_byte(s->pb) << 21; |
|
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2274
diff
changeset
|
72 t |= get_byte(s->pb) << 14; |
|
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2274
diff
changeset
|
73 t |= get_byte(s->pb) << 7; |
|
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2274
diff
changeset
|
74 t |= get_byte(s->pb); |
|
1609
9adbec516265
Make MPC demuxer deal with ID3 tags at the beginning
kostya
parents:
1605
diff
changeset
|
75 av_log(s, AV_LOG_DEBUG, "Skipping %d(%X) bytes of ID3 data\n", t, t); |
|
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2274
diff
changeset
|
76 url_fskip(s->pb, t); |
|
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2274
diff
changeset
|
77 if(get_le24(s->pb) != MKTAG('M', 'P', '+', 0)){ |
|
1609
9adbec516265
Make MPC demuxer deal with ID3 tags at the beginning
kostya
parents:
1605
diff
changeset
|
78 av_log(s, AV_LOG_ERROR, "Not a Musepack file\n"); |
|
9adbec516265
Make MPC demuxer deal with ID3 tags at the beginning
kostya
parents:
1605
diff
changeset
|
79 return -1; |
|
9adbec516265
Make MPC demuxer deal with ID3 tags at the beginning
kostya
parents:
1605
diff
changeset
|
80 } |
| 1602 | 81 } |
|
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2274
diff
changeset
|
82 c->ver = get_byte(s->pb); |
| 1602 | 83 if(c->ver != 0x07 && c->ver != 0x17){ |
| 84 av_log(s, AV_LOG_ERROR, "Can demux Musepack SV7, got version %02X\n", c->ver); | |
| 85 return -1; | |
| 86 } | |
|
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2274
diff
changeset
|
87 c->fcount = get_le32(s->pb); |
| 1602 | 88 if((int64_t)c->fcount * sizeof(MPCFrame) >= UINT_MAX){ |
| 89 av_log(s, AV_LOG_ERROR, "Too many frames, seeking is not possible\n"); | |
| 90 return -1; | |
| 91 } | |
| 92 c->frames = av_malloc(c->fcount * sizeof(MPCFrame)); | |
| 93 c->curframe = 0; | |
| 94 c->lastframe = -1; | |
| 95 c->curbits = 8; | |
| 1605 | 96 c->frames_noted = 0; |
| 1602 | 97 |
| 98 st = av_new_stream(s, 0); | |
| 99 if (!st) | |
|
2273
7eb456c4ed8a
Replace all occurrences of AVERROR_NOMEM with AVERROR(ENOMEM).
takis
parents:
2217
diff
changeset
|
100 return AVERROR(ENOMEM); |
| 1602 | 101 st->codec->codec_type = CODEC_TYPE_AUDIO; |
| 102 st->codec->codec_id = CODEC_ID_MUSEPACK7; | |
| 103 st->codec->channels = 2; | |
|
3908
1d3d17de20ba
Bump Major version, this commit is almost just renaming bits_per_sample to
michael
parents:
3424
diff
changeset
|
104 st->codec->bits_per_coded_sample = 16; |
| 1602 | 105 |
| 106 st->codec->extradata_size = 16; | |
| 107 st->codec->extradata = av_mallocz(st->codec->extradata_size+FF_INPUT_BUFFER_PADDING_SIZE); | |
|
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2274
diff
changeset
|
108 get_buffer(s->pb, st->codec->extradata, 16); |
| 1602 | 109 st->codec->sample_rate = mpc_rate[st->codec->extradata[2] & 3]; |
| 110 av_set_pts_info(st, 32, MPC_FRAMESIZE, st->codec->sample_rate); | |
| 111 /* scan for seekpoints */ | |
| 112 s->start_time = 0; | |
| 113 s->duration = (int64_t)c->fcount * MPC_FRAMESIZE * AV_TIME_BASE / st->codec->sample_rate; | |
| 114 | |
| 5135 | 115 /* try to read APE tags */ |
| 116 if (!url_is_streamed(s->pb)) { | |
| 117 int64_t pos = url_ftell(s->pb); | |
| 118 ff_ape_parse_tag(s); | |
| 119 url_fseek(s->pb, pos, SEEK_SET); | |
| 120 } | |
| 121 | |
| 1602 | 122 return 0; |
| 123 } | |
| 124 | |
| 125 static int mpc_read_packet(AVFormatContext *s, AVPacket *pkt) | |
| 126 { | |
| 127 MPCContext *c = s->priv_data; | |
| 128 int ret, size, size2, curbits, cur = c->curframe; | |
| 129 int64_t tmp, pos; | |
| 130 | |
|
1645
9aa27a785d1f
10l, > vs. >= typo, caused crashes on last mpc frame
reimar
parents:
1610
diff
changeset
|
131 if (c->curframe >= c->fcount) |
| 1602 | 132 return -1; |
| 133 | |
| 134 if(c->curframe != c->lastframe + 1){ | |
|
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2274
diff
changeset
|
135 url_fseek(s->pb, c->frames[c->curframe].pos, SEEK_SET); |
| 1602 | 136 c->curbits = c->frames[c->curframe].skip; |
| 137 } | |
| 138 c->lastframe = c->curframe; | |
| 139 c->curframe++; | |
| 140 curbits = c->curbits; | |
|
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2274
diff
changeset
|
141 pos = url_ftell(s->pb); |
|
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2274
diff
changeset
|
142 tmp = get_le32(s->pb); |
| 1602 | 143 if(curbits <= 12){ |
| 144 size2 = (tmp >> (12 - curbits)) & 0xFFFFF; | |
| 145 }else{ | |
|
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2274
diff
changeset
|
146 tmp = (tmp << 32) | get_le32(s->pb); |
| 1602 | 147 size2 = (tmp >> (44 - curbits)) & 0xFFFFF; |
| 148 } | |
| 149 curbits += 20; | |
|
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2274
diff
changeset
|
150 url_fseek(s->pb, pos, SEEK_SET); |
| 1602 | 151 |
| 152 size = ((size2 + curbits + 31) & ~31) >> 3; | |
| 153 if(cur == c->frames_noted){ | |
| 154 c->frames[cur].pos = pos; | |
| 155 c->frames[cur].size = size; | |
| 156 c->frames[cur].skip = curbits - 20; | |
| 157 av_add_index_entry(s->streams[0], cur, cur, size, 0, AVINDEX_KEYFRAME); | |
| 158 c->frames_noted++; | |
| 159 } | |
| 160 c->curbits = (curbits + size2) & 0x1F; | |
| 161 | |
| 162 if (av_new_packet(pkt, size) < 0) | |
|
2274
b21c2af60bc9
Replace all occurrences of AVERROR_IO with AVERROR(EIO).
takis
parents:
2273
diff
changeset
|
163 return AVERROR(EIO); |
| 1602 | 164 |
| 165 pkt->data[0] = curbits; | |
| 166 pkt->data[1] = (c->curframe > c->fcount); | |
| 167 | |
| 168 pkt->stream_index = 0; | |
|
1610
cde17266ad08
Decode previous 32 frames to avoid seeking artifacts in MPC
kostya
parents:
1609
diff
changeset
|
169 pkt->pts = cur; |
|
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2274
diff
changeset
|
170 ret = get_buffer(s->pb, pkt->data + 4, size); |
| 1602 | 171 if(c->curbits) |
|
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2274
diff
changeset
|
172 url_fseek(s->pb, -4, SEEK_CUR); |
| 1602 | 173 if(ret < size){ |
| 174 av_free_packet(pkt); | |
|
2274
b21c2af60bc9
Replace all occurrences of AVERROR_IO with AVERROR(EIO).
takis
parents:
2273
diff
changeset
|
175 return AVERROR(EIO); |
| 1602 | 176 } |
| 177 pkt->size = ret + 4; | |
| 178 | |
| 179 return 0; | |
| 180 } | |
| 181 | |
| 182 static int mpc_read_close(AVFormatContext *s) | |
| 183 { | |
| 184 MPCContext *c = s->priv_data; | |
| 185 | |
| 186 av_freep(&c->frames); | |
| 187 return 0; | |
| 188 } | |
| 189 | |
| 1605 | 190 /** |
| 191 * Seek to the given position | |
| 192 * If position is unknown but is within the limits of file | |
| 193 * then packets are skipped unless desired position is reached | |
| 194 * | |
| 195 * Also this function makes use of the fact that timestamp == frameno | |
| 196 */ | |
| 1602 | 197 static int mpc_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags) |
| 198 { | |
| 199 AVStream *st = s->streams[stream_index]; | |
| 200 MPCContext *c = s->priv_data; | |
| 1605 | 201 AVPacket pkt1, *pkt = &pkt1; |
| 202 int ret; | |
|
1610
cde17266ad08
Decode previous 32 frames to avoid seeking artifacts in MPC
kostya
parents:
1609
diff
changeset
|
203 int index = av_index_search_timestamp(st, timestamp - DELAY_FRAMES, flags); |
| 1605 | 204 uint32_t lastframe; |
| 205 | |
| 206 /* if found, seek there */ | |
| 207 if (index >= 0){ | |
| 208 c->curframe = st->index_entries[index].pos; | |
| 209 return 0; | |
| 210 } | |
| 211 /* if timestamp is out of bounds, return error */ | |
| 212 if(timestamp < 0 || timestamp >= c->fcount) | |
| 1602 | 213 return -1; |
|
1610
cde17266ad08
Decode previous 32 frames to avoid seeking artifacts in MPC
kostya
parents:
1609
diff
changeset
|
214 timestamp -= DELAY_FRAMES; |
| 1605 | 215 /* seek to the furthest known position and read packets until |
| 216 we reach desired position */ | |
| 217 lastframe = c->curframe; | |
| 218 if(c->frames_noted) c->curframe = c->frames_noted - 1; | |
| 219 while(c->curframe < timestamp){ | |
| 220 ret = av_read_frame(s, pkt); | |
| 221 if (ret < 0){ | |
| 222 c->curframe = lastframe; | |
| 223 return -1; | |
| 224 } | |
| 225 av_free_packet(pkt); | |
| 226 } | |
| 1602 | 227 return 0; |
| 228 } | |
| 229 | |
| 230 | |
| 231 AVInputFormat mpc_demuxer = { | |
| 232 "mpc", | |
|
3424
7a0230981402
Make long_names in lavf/lavdev optional depending on CONFIG_SMALL.
diego
parents:
3286
diff
changeset
|
233 NULL_IF_CONFIG_SMALL("Musepack"), |
| 1602 | 234 sizeof(MPCContext), |
| 235 mpc_probe, | |
| 236 mpc_read_header, | |
| 237 mpc_read_packet, | |
| 238 mpc_read_close, | |
| 239 mpc_read_seek, | |
| 240 .extensions = "mpc", | |
| 241 }; |
