mpc.c 6.3 KB

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