mpc.c 6.7 KB

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