mpc.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. if(c->fcount){
  67. c->frames = av_malloc(c->fcount * sizeof(MPCFrame));
  68. if(!c->frames){
  69. av_log(s, AV_LOG_ERROR, "Cannot allocate seektable\n");
  70. return AVERROR(ENOMEM);
  71. }
  72. }else{
  73. av_log(s, AV_LOG_WARNING, "Container reports no frames\n");
  74. }
  75. c->curframe = 0;
  76. c->lastframe = -1;
  77. c->curbits = 8;
  78. c->frames_noted = 0;
  79. st = av_new_stream(s, 0);
  80. if (!st)
  81. return AVERROR(ENOMEM);
  82. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  83. st->codec->codec_id = CODEC_ID_MUSEPACK7;
  84. st->codec->channels = 2;
  85. st->codec->bits_per_coded_sample = 16;
  86. st->codec->extradata_size = 16;
  87. st->codec->extradata = av_mallocz(st->codec->extradata_size+FF_INPUT_BUFFER_PADDING_SIZE);
  88. avio_read(s->pb, st->codec->extradata, 16);
  89. st->codec->sample_rate = mpc_rate[st->codec->extradata[2] & 3];
  90. av_set_pts_info(st, 32, MPC_FRAMESIZE, st->codec->sample_rate);
  91. /* scan for seekpoints */
  92. st->start_time = 0;
  93. st->duration = c->fcount;
  94. /* try to read APE tags */
  95. if (s->pb->seekable) {
  96. int64_t pos = avio_tell(s->pb);
  97. ff_ape_parse_tag(s);
  98. if (!av_dict_get(s->metadata, "", NULL, AV_DICT_IGNORE_SUFFIX))
  99. ff_id3v1_read(s);
  100. avio_seek(s->pb, pos, SEEK_SET);
  101. }
  102. return 0;
  103. }
  104. static int mpc_read_packet(AVFormatContext *s, AVPacket *pkt)
  105. {
  106. MPCContext *c = s->priv_data;
  107. int ret, size, size2, curbits, cur = c->curframe;
  108. int64_t tmp, pos;
  109. if (c->curframe >= c->fcount && c->fcount)
  110. return -1;
  111. if(c->curframe != c->lastframe + 1){
  112. avio_seek(s->pb, c->frames[c->curframe].pos, SEEK_SET);
  113. c->curbits = c->frames[c->curframe].skip;
  114. }
  115. c->lastframe = c->curframe;
  116. c->curframe++;
  117. curbits = c->curbits;
  118. pos = avio_tell(s->pb);
  119. tmp = avio_rl32(s->pb);
  120. if(curbits <= 12){
  121. size2 = (tmp >> (12 - curbits)) & 0xFFFFF;
  122. }else{
  123. tmp = (tmp << 32) | avio_rl32(s->pb);
  124. size2 = (tmp >> (44 - curbits)) & 0xFFFFF;
  125. }
  126. curbits += 20;
  127. avio_seek(s->pb, pos, SEEK_SET);
  128. size = ((size2 + curbits + 31) & ~31) >> 3;
  129. if(cur == c->frames_noted && c->fcount){
  130. c->frames[cur].pos = pos;
  131. c->frames[cur].size = size;
  132. c->frames[cur].skip = curbits - 20;
  133. av_add_index_entry(s->streams[0], cur, cur, size, 0, AVINDEX_KEYFRAME);
  134. c->frames_noted++;
  135. }
  136. c->curbits = (curbits + size2) & 0x1F;
  137. if (av_new_packet(pkt, size) < 0)
  138. return AVERROR(EIO);
  139. pkt->data[0] = curbits;
  140. pkt->data[1] = (c->curframe > c->fcount) && c->fcount;
  141. pkt->data[2] = 0;
  142. pkt->data[3] = 0;
  143. pkt->stream_index = 0;
  144. pkt->pts = cur;
  145. ret = avio_read(s->pb, pkt->data + 4, size);
  146. if(c->curbits)
  147. avio_seek(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 ff_mpc_demuxer = {
  200. .name = "mpc",
  201. .long_name = NULL_IF_CONFIG_SMALL("Musepack"),
  202. .priv_data_size = sizeof(MPCContext),
  203. .read_probe = mpc_probe,
  204. .read_header = mpc_read_header,
  205. .read_packet = mpc_read_packet,
  206. .read_close = mpc_read_close,
  207. .read_seek = mpc_read_seek,
  208. .extensions = "mpc",
  209. };