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