mpc.c 6.3 KB

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