wav: remove a use of deprecated AV_METADATA_ macro
[libav.git] / libavformat / mpc.c
CommitLineData
185c7b6b
KS
1/*
2 * Musepack demuxer
3 * Copyright (c) 2006 Konstantin Shishkov
4 *
2912e87a 5 * This file is part of Libav.
185c7b6b 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
185c7b6b
KS
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 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
185c7b6b
KS
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
2912e87a 18 * License along with Libav; if not, write to the Free Software
e5a389a1 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
185c7b6b 20 */
245976da 21
9106a698 22#include "libavcodec/get_bits.h"
185c7b6b 23#include "avformat.h"
311f96a8 24#include "apetag.h"
3694ac24 25#include "id3v1.h"
d2d67e42 26#include "libavutil/dict.h"
185c7b6b
KS
27
28#define MPC_FRAMESIZE 1152
aac88b53 29#define DELAY_FRAMES 32
185c7b6b
KS
30
31static const int mpc_rate[4] = { 44100, 48000, 37800, 32000 };
32typedef struct {
33 int64_t pos;
34 int size, skip;
35}MPCFrame;
36
37typedef 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
46static int mpc_probe(AVProbeData *p)
47{
48 const uint8_t *d = p->buf;
185c7b6b
KS
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
54static int mpc_read_header(AVFormatContext *s, AVFormatParameters *ap)
55{
56 MPCContext *c = s->priv_data;
57 AVStream *st;
58
b7effd4e 59 if(avio_rl24(s->pb) != MKTAG('M', 'P', '+', 0)){
6612d8cf
RD
60 av_log(s, AV_LOG_ERROR, "Not a Musepack file\n");
61 return -1;
185c7b6b 62 }
b7effd4e 63 c->ver = avio_r8(s->pb);
185c7b6b
KS
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 }
b7effd4e 68 c->fcount = avio_rl32(s->pb);
185c7b6b
KS
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 }
0cdd1208
KS
73 if(c->fcount){
74 c->frames = av_malloc(c->fcount * sizeof(MPCFrame));
75 if(!c->frames){
76 av_log(s, AV_LOG_ERROR, "Cannot allocate seektable\n");
77 return AVERROR(ENOMEM);
78 }
79 }else{
80 av_log(s, AV_LOG_WARNING, "Container reports no frames\n");
81 }
185c7b6b
KS
82 c->curframe = 0;
83 c->lastframe = -1;
84 c->curbits = 8;
e3113632 85 c->frames_noted = 0;
185c7b6b
KS
86
87 st = av_new_stream(s, 0);
88 if (!st)
769e10f0 89 return AVERROR(ENOMEM);
72415b2a 90 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
185c7b6b
KS
91 st->codec->codec_id = CODEC_ID_MUSEPACK7;
92 st->codec->channels = 2;
dd1c8f3e 93 st->codec->bits_per_coded_sample = 16;
185c7b6b
KS
94
95 st->codec->extradata_size = 16;
96 st->codec->extradata = av_mallocz(st->codec->extradata_size+FF_INPUT_BUFFER_PADDING_SIZE);
b7effd4e 97 avio_read(s->pb, st->codec->extradata, 16);
185c7b6b
KS
98 st->codec->sample_rate = mpc_rate[st->codec->extradata[2] & 3];
99 av_set_pts_info(st, 32, MPC_FRAMESIZE, st->codec->sample_rate);
100 /* scan for seekpoints */
0fc07ad9
DC
101 st->start_time = 0;
102 st->duration = c->fcount;
185c7b6b 103
311f96a8 104 /* try to read APE tags */
8978feda 105 if (s->pb->seekable) {
a2704c97 106 int64_t pos = avio_tell(s->pb);
311f96a8 107 ff_ape_parse_tag(s);
d2d67e42 108 if (!av_dict_get(s->metadata, "", NULL, AV_DICT_IGNORE_SUFFIX))
3694ac24 109 ff_id3v1_read(s);
6b4aa5da 110 avio_seek(s->pb, pos, SEEK_SET);
311f96a8
MH
111 }
112
185c7b6b
KS
113 return 0;
114}
115
116static int mpc_read_packet(AVFormatContext *s, AVPacket *pkt)
117{
118 MPCContext *c = s->priv_data;
119 int ret, size, size2, curbits, cur = c->curframe;
120 int64_t tmp, pos;
121
0cdd1208 122 if (c->curframe >= c->fcount && c->fcount)
185c7b6b
KS
123 return -1;
124
125 if(c->curframe != c->lastframe + 1){
6b4aa5da 126 avio_seek(s->pb, c->frames[c->curframe].pos, SEEK_SET);
185c7b6b
KS
127 c->curbits = c->frames[c->curframe].skip;
128 }
129 c->lastframe = c->curframe;
130 c->curframe++;
131 curbits = c->curbits;
a2704c97 132 pos = avio_tell(s->pb);
b7effd4e 133 tmp = avio_rl32(s->pb);
185c7b6b
KS
134 if(curbits <= 12){
135 size2 = (tmp >> (12 - curbits)) & 0xFFFFF;
136 }else{
b7effd4e 137 tmp = (tmp << 32) | avio_rl32(s->pb);
185c7b6b
KS
138 size2 = (tmp >> (44 - curbits)) & 0xFFFFF;
139 }
140 curbits += 20;
6b4aa5da 141 avio_seek(s->pb, pos, SEEK_SET);
185c7b6b
KS
142
143 size = ((size2 + curbits + 31) & ~31) >> 3;
0cdd1208 144 if(cur == c->frames_noted && c->fcount){
185c7b6b
KS
145 c->frames[cur].pos = pos;
146 c->frames[cur].size = size;
147 c->frames[cur].skip = curbits - 20;
148 av_add_index_entry(s->streams[0], cur, cur, size, 0, AVINDEX_KEYFRAME);
149 c->frames_noted++;
150 }
151 c->curbits = (curbits + size2) & 0x1F;
152
153 if (av_new_packet(pkt, size) < 0)
6f3e0b21 154 return AVERROR(EIO);
185c7b6b
KS
155
156 pkt->data[0] = curbits;
0cdd1208 157 pkt->data[1] = (c->curframe > c->fcount) && c->fcount;
2e519564
VS
158 pkt->data[2] = 0;
159 pkt->data[3] = 0;
185c7b6b
KS
160
161 pkt->stream_index = 0;
aac88b53 162 pkt->pts = cur;
b7effd4e 163 ret = avio_read(s->pb, pkt->data + 4, size);
185c7b6b 164 if(c->curbits)
6b4aa5da 165 avio_seek(s->pb, -4, SEEK_CUR);
185c7b6b
KS
166 if(ret < size){
167 av_free_packet(pkt);
6f3e0b21 168 return AVERROR(EIO);
185c7b6b
KS
169 }
170 pkt->size = ret + 4;
171
172 return 0;
173}
174
175static int mpc_read_close(AVFormatContext *s)
176{
177 MPCContext *c = s->priv_data;
178
179 av_freep(&c->frames);
180 return 0;
181}
182
e3113632
KS
183/**
184 * Seek to the given position
185 * If position is unknown but is within the limits of file
186 * then packets are skipped unless desired position is reached
187 *
188 * Also this function makes use of the fact that timestamp == frameno
189 */
185c7b6b
KS
190static int mpc_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
191{
192 AVStream *st = s->streams[stream_index];
193 MPCContext *c = s->priv_data;
e3113632
KS
194 AVPacket pkt1, *pkt = &pkt1;
195 int ret;
aac88b53 196 int index = av_index_search_timestamp(st, timestamp - DELAY_FRAMES, flags);
e3113632 197 uint32_t lastframe;
185c7b6b 198
e3113632
KS
199 /* if found, seek there */
200 if (index >= 0){
201 c->curframe = st->index_entries[index].pos;
202 return 0;
203 }
204 /* if timestamp is out of bounds, return error */
205 if(timestamp < 0 || timestamp >= c->fcount)
206 return -1;
aac88b53 207 timestamp -= DELAY_FRAMES;
e3113632
KS
208 /* seek to the furthest known position and read packets until
209 we reach desired position */
210 lastframe = c->curframe;
211 if(c->frames_noted) c->curframe = c->frames_noted - 1;
212 while(c->curframe < timestamp){
213 ret = av_read_frame(s, pkt);
214 if (ret < 0){
215 c->curframe = lastframe;
216 return -1;
217 }
218 av_free_packet(pkt);
219 }
185c7b6b
KS
220 return 0;
221}
222
223
c6610a21 224AVInputFormat ff_mpc_demuxer = {
185c7b6b 225 "mpc",
bde15e74 226 NULL_IF_CONFIG_SMALL("Musepack"),
185c7b6b
KS
227 sizeof(MPCContext),
228 mpc_probe,
229 mpc_read_header,
230 mpc_read_packet,
231 mpc_read_close,
232 mpc_read_seek,
233 .extensions = "mpc",
234};