mpc.c 6.6 KB

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