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.

211 lines
5.9KB

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