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.

230 lines
6.5KB

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