Mercurial > libavformat.hg
annotate mpc.c @ 1838:d1af40ab8ea5 libavformat
write DTS, based on patch by Luca Abeni, lucabe72 email it
author | mru |
---|---|
date | Fri, 02 Mar 2007 21:47:24 +0000 |
parents | 9aa27a785d1f |
children | 1a3c9056982a |
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 | |
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
20 */ | |
21 #include "avformat.h" | |
22 #include "bitstream.h" | |
23 | |
24 #define MPC_FRAMESIZE 1152 | |
1610
cde17266ad08
Decode previous 32 frames to avoid seeking artifacts in MPC
kostya
parents:
1609
diff
changeset
|
25 #define DELAY_FRAMES 32 |
1602 | 26 |
27 static const int mpc_rate[4] = { 44100, 48000, 37800, 32000 }; | |
28 typedef struct { | |
29 int64_t pos; | |
30 int size, skip; | |
31 }MPCFrame; | |
32 | |
33 typedef struct { | |
34 int ver; | |
35 uint32_t curframe, lastframe; | |
36 uint32_t fcount; | |
37 MPCFrame *frames; | |
38 int curbits; | |
39 int frames_noted; | |
40 } MPCContext; | |
41 | |
42 static int mpc_probe(AVProbeData *p) | |
43 { | |
44 const uint8_t *d = p->buf; | |
45 if (p->buf_size < 32) | |
46 return 0; | |
47 if (d[0] == 'M' && d[1] == 'P' && d[2] == '+' && (d[3] == 0x17 || d[3] == 0x7)) | |
48 return AVPROBE_SCORE_MAX; | |
1609
9adbec516265
Make MPC demuxer deal with ID3 tags at the beginning
kostya
parents:
1605
diff
changeset
|
49 if (d[0] == 'I' && d[1] == 'D' && d[2] == '3') |
9adbec516265
Make MPC demuxer deal with ID3 tags at the beginning
kostya
parents:
1605
diff
changeset
|
50 return AVPROBE_SCORE_MAX / 2; |
1602 | 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; | |
1609
9adbec516265
Make MPC demuxer deal with ID3 tags at the beginning
kostya
parents:
1605
diff
changeset
|
58 int t; |
1602 | 59 |
1609
9adbec516265
Make MPC demuxer deal with ID3 tags at the beginning
kostya
parents:
1605
diff
changeset
|
60 t = get_le24(&s->pb); |
9adbec516265
Make MPC demuxer deal with ID3 tags at the beginning
kostya
parents:
1605
diff
changeset
|
61 if(t != MKTAG('M', 'P', '+', 0)){ |
9adbec516265
Make MPC demuxer deal with ID3 tags at the beginning
kostya
parents:
1605
diff
changeset
|
62 if(t != MKTAG('I', 'D', '3', 0)){ |
9adbec516265
Make MPC demuxer deal with ID3 tags at the beginning
kostya
parents:
1605
diff
changeset
|
63 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
|
64 return -1; |
9adbec516265
Make MPC demuxer deal with ID3 tags at the beginning
kostya
parents:
1605
diff
changeset
|
65 } |
9adbec516265
Make MPC demuxer deal with ID3 tags at the beginning
kostya
parents:
1605
diff
changeset
|
66 /* skip ID3 tags and try again */ |
9adbec516265
Make MPC demuxer deal with ID3 tags at the beginning
kostya
parents:
1605
diff
changeset
|
67 url_fskip(&s->pb, 3); |
9adbec516265
Make MPC demuxer deal with ID3 tags at the beginning
kostya
parents:
1605
diff
changeset
|
68 t = get_byte(&s->pb) << 21; |
9adbec516265
Make MPC demuxer deal with ID3 tags at the beginning
kostya
parents:
1605
diff
changeset
|
69 t |= get_byte(&s->pb) << 14; |
9adbec516265
Make MPC demuxer deal with ID3 tags at the beginning
kostya
parents:
1605
diff
changeset
|
70 t |= get_byte(&s->pb) << 7; |
9adbec516265
Make MPC demuxer deal with ID3 tags at the beginning
kostya
parents:
1605
diff
changeset
|
71 t |= get_byte(&s->pb); |
9adbec516265
Make MPC demuxer deal with ID3 tags at the beginning
kostya
parents:
1605
diff
changeset
|
72 av_log(s, AV_LOG_DEBUG, "Skipping %d(%X) bytes of ID3 data\n", t, t); |
9adbec516265
Make MPC demuxer deal with ID3 tags at the beginning
kostya
parents:
1605
diff
changeset
|
73 url_fskip(&s->pb, t); |
9adbec516265
Make MPC demuxer deal with ID3 tags at the beginning
kostya
parents:
1605
diff
changeset
|
74 if(get_le24(&s->pb) != MKTAG('M', 'P', '+', 0)){ |
9adbec516265
Make MPC demuxer deal with ID3 tags at the beginning
kostya
parents:
1605
diff
changeset
|
75 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
|
76 return -1; |
9adbec516265
Make MPC demuxer deal with ID3 tags at the beginning
kostya
parents:
1605
diff
changeset
|
77 } |
1602 | 78 } |
79 c->ver = get_byte(&s->pb); | |
80 if(c->ver != 0x07 && c->ver != 0x17){ | |
81 av_log(s, AV_LOG_ERROR, "Can demux Musepack SV7, got version %02X\n", c->ver); | |
82 return -1; | |
83 } | |
84 c->fcount = get_le32(&s->pb); | |
85 if((int64_t)c->fcount * sizeof(MPCFrame) >= UINT_MAX){ | |
86 av_log(s, AV_LOG_ERROR, "Too many frames, seeking is not possible\n"); | |
87 return -1; | |
88 } | |
89 c->frames = av_malloc(c->fcount * sizeof(MPCFrame)); | |
90 c->curframe = 0; | |
91 c->lastframe = -1; | |
92 c->curbits = 8; | |
1605 | 93 c->frames_noted = 0; |
1602 | 94 |
95 st = av_new_stream(s, 0); | |
96 if (!st) | |
97 return AVERROR_NOMEM; | |
98 st->codec->codec_type = CODEC_TYPE_AUDIO; | |
99 st->codec->codec_id = CODEC_ID_MUSEPACK7; | |
100 st->codec->channels = 2; | |
101 st->codec->bits_per_sample = 16; | |
102 | |
103 st->codec->extradata_size = 16; | |
104 st->codec->extradata = av_mallocz(st->codec->extradata_size+FF_INPUT_BUFFER_PADDING_SIZE); | |
105 get_buffer(&s->pb, st->codec->extradata, 16); | |
106 st->codec->sample_rate = mpc_rate[st->codec->extradata[2] & 3]; | |
107 av_set_pts_info(st, 32, MPC_FRAMESIZE, st->codec->sample_rate); | |
108 /* scan for seekpoints */ | |
109 s->start_time = 0; | |
110 s->duration = (int64_t)c->fcount * MPC_FRAMESIZE * AV_TIME_BASE / st->codec->sample_rate; | |
111 | |
112 return 0; | |
113 } | |
114 | |
115 static int mpc_read_packet(AVFormatContext *s, AVPacket *pkt) | |
116 { | |
117 MPCContext *c = s->priv_data; | |
118 int ret, size, size2, curbits, cur = c->curframe; | |
119 int64_t tmp, pos; | |
120 | |
1645
9aa27a785d1f
10l, > vs. >= typo, caused crashes on last mpc frame
reimar
parents:
1610
diff
changeset
|
121 if (c->curframe >= c->fcount) |
1602 | 122 return -1; |
123 | |
124 if(c->curframe != c->lastframe + 1){ | |
125 url_fseek(&s->pb, c->frames[c->curframe].pos, SEEK_SET); | |
126 c->curbits = c->frames[c->curframe].skip; | |
127 } | |
128 c->lastframe = c->curframe; | |
129 c->curframe++; | |
130 curbits = c->curbits; | |
131 pos = url_ftell(&s->pb); | |
132 tmp = get_le32(&s->pb); | |
133 if(curbits <= 12){ | |
134 size2 = (tmp >> (12 - curbits)) & 0xFFFFF; | |
135 }else{ | |
136 tmp = (tmp << 32) | get_le32(&s->pb); | |
137 size2 = (tmp >> (44 - curbits)) & 0xFFFFF; | |
138 } | |
139 curbits += 20; | |
140 url_fseek(&s->pb, pos, SEEK_SET); | |
141 | |
142 size = ((size2 + curbits + 31) & ~31) >> 3; | |
143 if(cur == c->frames_noted){ | |
144 c->frames[cur].pos = pos; | |
145 c->frames[cur].size = size; | |
146 c->frames[cur].skip = curbits - 20; | |
147 av_add_index_entry(s->streams[0], cur, cur, size, 0, AVINDEX_KEYFRAME); | |
148 c->frames_noted++; | |
149 } | |
150 c->curbits = (curbits + size2) & 0x1F; | |
151 | |
152 if (av_new_packet(pkt, size) < 0) | |
153 return AVERROR_IO; | |
154 | |
155 pkt->data[0] = curbits; | |
156 pkt->data[1] = (c->curframe > c->fcount); | |
157 | |
158 pkt->stream_index = 0; | |
1610
cde17266ad08
Decode previous 32 frames to avoid seeking artifacts in MPC
kostya
parents:
1609
diff
changeset
|
159 pkt->pts = cur; |
1602 | 160 ret = get_buffer(&s->pb, pkt->data + 4, size); |
161 if(c->curbits) | |
162 url_fseek(&s->pb, -4, SEEK_CUR); | |
163 if(ret < size){ | |
164 av_free_packet(pkt); | |
165 return AVERROR_IO; | |
166 } | |
167 pkt->size = ret + 4; | |
168 | |
169 return 0; | |
170 } | |
171 | |
172 static int mpc_read_close(AVFormatContext *s) | |
173 { | |
174 MPCContext *c = s->priv_data; | |
175 | |
176 av_freep(&c->frames); | |
177 return 0; | |
178 } | |
179 | |
1605 | 180 /** |
181 * Seek to the given position | |
182 * If position is unknown but is within the limits of file | |
183 * then packets are skipped unless desired position is reached | |
184 * | |
185 * Also this function makes use of the fact that timestamp == frameno | |
186 */ | |
1602 | 187 static int mpc_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags) |
188 { | |
189 AVStream *st = s->streams[stream_index]; | |
190 MPCContext *c = s->priv_data; | |
1605 | 191 AVPacket pkt1, *pkt = &pkt1; |
192 int ret; | |
1610
cde17266ad08
Decode previous 32 frames to avoid seeking artifacts in MPC
kostya
parents:
1609
diff
changeset
|
193 int index = av_index_search_timestamp(st, timestamp - DELAY_FRAMES, flags); |
1605 | 194 uint32_t lastframe; |
195 | |
196 /* if found, seek there */ | |
197 if (index >= 0){ | |
198 c->curframe = st->index_entries[index].pos; | |
199 return 0; | |
200 } | |
201 /* if timestamp is out of bounds, return error */ | |
202 if(timestamp < 0 || timestamp >= c->fcount) | |
1602 | 203 return -1; |
1610
cde17266ad08
Decode previous 32 frames to avoid seeking artifacts in MPC
kostya
parents:
1609
diff
changeset
|
204 timestamp -= DELAY_FRAMES; |
1605 | 205 /* seek to the furthest known position and read packets until |
206 we reach desired position */ | |
207 lastframe = c->curframe; | |
208 if(c->frames_noted) c->curframe = c->frames_noted - 1; | |
209 while(c->curframe < timestamp){ | |
210 ret = av_read_frame(s, pkt); | |
211 if (ret < 0){ | |
212 c->curframe = lastframe; | |
213 return -1; | |
214 } | |
215 av_free_packet(pkt); | |
216 } | |
1602 | 217 return 0; |
218 } | |
219 | |
220 | |
221 AVInputFormat mpc_demuxer = { | |
222 "mpc", | |
223 "musepack", | |
224 sizeof(MPCContext), | |
225 mpc_probe, | |
226 mpc_read_header, | |
227 mpc_read_packet, | |
228 mpc_read_close, | |
229 mpc_read_seek, | |
230 .extensions = "mpc", | |
231 }; |