annotate mpc.c @ 1602:6238b40b7aef libavformat

Musepack SV7 decoding support
author kostya
date Sun, 24 Dec 2006 04:51:43 +0000
parents
children 6d66c6325e0d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1602
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
1 /*
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
2 * Musepack demuxer
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
3 * Copyright (c) 2006 Konstantin Shishkov
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
4 *
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
5 * This file is part of FFmpeg.
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
6 *
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
9 * License as published by the Free Software Foundation; either
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
11 *
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
15 * Lesser General Public License for more details.
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
16 *
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
20 */
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
21 #include "avformat.h"
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
22 #include "bitstream.h"
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
23
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
24 #define MPC_FRAMESIZE 1152
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
25
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
26 static const int mpc_rate[4] = { 44100, 48000, 37800, 32000 };
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
27 typedef struct {
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
28 int64_t pos;
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
29 int size, skip;
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
30 }MPCFrame;
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
31
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
32 typedef struct {
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
33 int ver;
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
34 uint32_t curframe, lastframe;
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
35 uint32_t fcount;
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
36 MPCFrame *frames;
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
37 int curbits;
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
38 int frames_noted;
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
39 } MPCContext;
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
40
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
41 static int mpc_probe(AVProbeData *p)
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
42 {
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
43 const uint8_t *d = p->buf;
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
44 if (p->buf_size < 32)
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
45 return 0;
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
46 if (d[0] == 'M' && d[1] == 'P' && d[2] == '+' && (d[3] == 0x17 || d[3] == 0x7))
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
47 return AVPROBE_SCORE_MAX;
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
48 return 0;
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
49 }
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
50
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
51 static int mpc_read_header(AVFormatContext *s, AVFormatParameters *ap)
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
52 {
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
53 MPCContext *c = s->priv_data;
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
54 AVStream *st;
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
55
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
56 if(get_le24(&s->pb) != MKTAG('M', 'P', '+', 0)){
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
57 av_log(s, AV_LOG_ERROR, "Not a Musepack file\n");
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
58 return -1;
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
59 }
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
60 c->ver = get_byte(&s->pb);
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
61 if(c->ver != 0x07 && c->ver != 0x17){
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
62 av_log(s, AV_LOG_ERROR, "Can demux Musepack SV7, got version %02X\n", c->ver);
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
63 return -1;
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
64 }
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
65 c->fcount = get_le32(&s->pb);
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
66 if((int64_t)c->fcount * sizeof(MPCFrame) >= UINT_MAX){
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
67 av_log(s, AV_LOG_ERROR, "Too many frames, seeking is not possible\n");
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
68 return -1;
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
69 }
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
70 c->frames = av_malloc(c->fcount * sizeof(MPCFrame));
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
71 c->curframe = 0;
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
72 c->lastframe = -1;
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
73 c->curbits = 8;
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
74
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
75 st = av_new_stream(s, 0);
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
76 if (!st)
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
77 return AVERROR_NOMEM;
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
78 st->codec->codec_type = CODEC_TYPE_AUDIO;
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
79 st->codec->codec_id = CODEC_ID_MUSEPACK7;
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
80 st->codec->channels = 2;
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
81 st->codec->bits_per_sample = 16;
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
82
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
83 st->codec->extradata_size = 16;
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
84 st->codec->extradata = av_mallocz(st->codec->extradata_size+FF_INPUT_BUFFER_PADDING_SIZE);
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
85 get_buffer(&s->pb, st->codec->extradata, 16);
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
86 st->codec->sample_rate = mpc_rate[st->codec->extradata[2] & 3];
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
87 av_set_pts_info(st, 32, MPC_FRAMESIZE, st->codec->sample_rate);
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
88 /* scan for seekpoints */
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
89 s->start_time = 0;
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
90 s->duration = (int64_t)c->fcount * MPC_FRAMESIZE * AV_TIME_BASE / st->codec->sample_rate;
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
91
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
92 return 0;
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
93 }
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
94
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
95 static int mpc_read_packet(AVFormatContext *s, AVPacket *pkt)
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
96 {
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
97 MPCContext *c = s->priv_data;
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
98 int ret, size, size2, curbits, cur = c->curframe;
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
99 int64_t tmp, pos;
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
100
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
101 if (c->curframe > c->fcount)
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
102 return -1;
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
103
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
104 if(c->curframe != c->lastframe + 1){
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
105 url_fseek(&s->pb, c->frames[c->curframe].pos, SEEK_SET);
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
106 c->curbits = c->frames[c->curframe].skip;
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
107 }
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
108 c->lastframe = c->curframe;
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
109 c->curframe++;
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
110 curbits = c->curbits;
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
111 pos = url_ftell(&s->pb);
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
112 tmp = get_le32(&s->pb);
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
113 if(curbits <= 12){
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
114 size2 = (tmp >> (12 - curbits)) & 0xFFFFF;
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
115 }else{
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
116 tmp = (tmp << 32) | get_le32(&s->pb);
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
117 size2 = (tmp >> (44 - curbits)) & 0xFFFFF;
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
118 }
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
119 curbits += 20;
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
120 url_fseek(&s->pb, pos, SEEK_SET);
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
121
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
122 size = ((size2 + curbits + 31) & ~31) >> 3;
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
123 if(cur == c->frames_noted){
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
124 c->frames[cur].pos = pos;
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
125 c->frames[cur].size = size;
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
126 c->frames[cur].skip = curbits - 20;
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
127 av_add_index_entry(s->streams[0], cur, cur, size, 0, AVINDEX_KEYFRAME);
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
128 c->frames_noted++;
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
129 }
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
130 c->curbits = (curbits + size2) & 0x1F;
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
131
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
132 if (av_new_packet(pkt, size) < 0)
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
133 return AVERROR_IO;
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
134
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
135 pkt->data[0] = curbits;
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
136 pkt->data[1] = (c->curframe > c->fcount);
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
137
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
138 pkt->stream_index = 0;
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
139 ret = get_buffer(&s->pb, pkt->data + 4, size);
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
140 if(c->curbits)
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
141 url_fseek(&s->pb, -4, SEEK_CUR);
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
142 if(ret < size){
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
143 av_free_packet(pkt);
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
144 return AVERROR_IO;
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
145 }
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
146 pkt->size = ret + 4;
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
147
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
148 return 0;
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
149 }
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
150
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
151 static int mpc_read_close(AVFormatContext *s)
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
152 {
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
153 MPCContext *c = s->priv_data;
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
154
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
155 av_freep(&c->frames);
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
156 return 0;
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
157 }
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
158
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
159 static int mpc_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
160 {
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
161 AVStream *st = s->streams[stream_index];
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
162 MPCContext *c = s->priv_data;
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
163 int index = av_index_search_timestamp(st, timestamp, flags);
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
164 if (index < 0)
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
165 return -1;
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
166 c->curframe = st->index_entries[index].pos;
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
167
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
168 return 0;
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
169 }
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
170
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
171
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
172 AVInputFormat mpc_demuxer = {
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
173 "mpc",
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
174 "musepack",
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
175 sizeof(MPCContext),
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
176 mpc_probe,
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
177 mpc_read_header,
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
178 mpc_read_packet,
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
179 mpc_read_close,
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
180 mpc_read_seek,
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
181 .extensions = "mpc",
6238b40b7aef Musepack SV7 decoding support
kostya
parents:
diff changeset
182 };