You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

250 lines
7.1KB

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