You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

310 lines
8.4KB

  1. /*
  2. * Musepack SV8 demuxer
  3. * Copyright (c) 2007 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 "libavcodec/unary.h"
  23. #include "apetag.h"
  24. #include "avformat.h"
  25. #include "internal.h"
  26. #include "avio_internal.h"
  27. /// Two-byte MPC tag
  28. #define MKMPCTAG(a, b) (a | (b << 8))
  29. #define TAG_MPCK MKTAG('M','P','C','K')
  30. /// Reserved MPC tags
  31. enum MPCPacketTags{
  32. TAG_STREAMHDR = MKMPCTAG('S','H'),
  33. TAG_STREAMEND = MKMPCTAG('S','E'),
  34. TAG_AUDIOPACKET = MKMPCTAG('A','P'),
  35. TAG_SEEKTBLOFF = MKMPCTAG('S','O'),
  36. TAG_SEEKTABLE = MKMPCTAG('S','T'),
  37. TAG_REPLAYGAIN = MKMPCTAG('R','G'),
  38. TAG_ENCINFO = MKMPCTAG('E','I'),
  39. };
  40. static const int mpc8_rate[8] = { 44100, 48000, 37800, 32000, -1, -1, -1, -1 };
  41. typedef struct {
  42. int ver;
  43. int frame;
  44. int64_t header_pos;
  45. int64_t samples;
  46. int64_t apetag_start;
  47. } MPCContext;
  48. static inline int64_t bs_get_v(uint8_t **bs)
  49. {
  50. int64_t v = 0;
  51. int br = 0;
  52. int c;
  53. do {
  54. c = **bs; (*bs)++;
  55. v <<= 7;
  56. v |= c & 0x7F;
  57. br++;
  58. if (br > 10)
  59. return -1;
  60. } while (c & 0x80);
  61. return v - br;
  62. }
  63. static int mpc8_probe(AVProbeData *p)
  64. {
  65. uint8_t *bs = p->buf + 4;
  66. uint8_t *bs_end = bs + p->buf_size;
  67. int64_t size;
  68. if (p->buf_size < 16)
  69. return 0;
  70. if (AV_RL32(p->buf) != TAG_MPCK)
  71. return 0;
  72. while (bs < bs_end + 3) {
  73. int header_found = (bs[0] == 'S' && bs[1] == 'H');
  74. if (bs[0] < 'A' || bs[0] > 'Z' || bs[1] < 'A' || bs[1] > 'Z')
  75. return 0;
  76. bs += 2;
  77. size = bs_get_v(&bs);
  78. if (size < 2)
  79. return 0;
  80. if (bs + size - 2 >= bs_end)
  81. return AVPROBE_SCORE_MAX / 4 - 1; //seems to be valid MPC but no header yet
  82. if (header_found) {
  83. if (size < 11 || size > 28)
  84. return 0;
  85. if (!AV_RL32(bs)) //zero CRC is invalid
  86. return 0;
  87. return AVPROBE_SCORE_MAX;
  88. } else {
  89. bs += size - 2;
  90. }
  91. }
  92. return 0;
  93. }
  94. static inline int64_t gb_get_v(GetBitContext *gb)
  95. {
  96. int64_t v = 0;
  97. int bits = 0;
  98. while(get_bits1(gb) && bits < 64-7){
  99. v <<= 7;
  100. v |= get_bits(gb, 7);
  101. bits += 7;
  102. }
  103. v <<= 7;
  104. v |= get_bits(gb, 7);
  105. return v;
  106. }
  107. static void mpc8_get_chunk_header(AVIOContext *pb, int *tag, int64_t *size)
  108. {
  109. int64_t pos;
  110. pos = avio_tell(pb);
  111. *tag = avio_rl16(pb);
  112. *size = ffio_read_varlen(pb);
  113. *size -= avio_tell(pb) - pos;
  114. }
  115. static void mpc8_parse_seektable(AVFormatContext *s, int64_t off)
  116. {
  117. MPCContext *c = s->priv_data;
  118. int tag;
  119. int64_t size, pos, ppos[2];
  120. uint8_t *buf;
  121. int i, t, seekd;
  122. GetBitContext gb;
  123. avio_seek(s->pb, off, SEEK_SET);
  124. mpc8_get_chunk_header(s->pb, &tag, &size);
  125. if(tag != TAG_SEEKTABLE){
  126. av_log(s, AV_LOG_ERROR, "No seek table at given position\n");
  127. return;
  128. }
  129. if(!(buf = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE)))
  130. return;
  131. avio_read(s->pb, buf, size);
  132. init_get_bits(&gb, buf, size * 8);
  133. size = gb_get_v(&gb);
  134. if(size > UINT_MAX/4 || size > c->samples/1152){
  135. av_log(s, AV_LOG_ERROR, "Seek table is too big\n");
  136. return;
  137. }
  138. seekd = get_bits(&gb, 4);
  139. for(i = 0; i < 2; i++){
  140. pos = gb_get_v(&gb) + c->header_pos;
  141. ppos[1 - i] = pos;
  142. av_add_index_entry(s->streams[0], pos, i, 0, 0, AVINDEX_KEYFRAME);
  143. }
  144. for(; i < size; i++){
  145. t = get_unary(&gb, 1, 33) << 12;
  146. t += get_bits(&gb, 12);
  147. if(t & 1)
  148. t = -(t & ~1);
  149. pos = (t >> 1) + ppos[0]*2 - ppos[1];
  150. av_add_index_entry(s->streams[0], pos, i << seekd, 0, 0, AVINDEX_KEYFRAME);
  151. ppos[1] = ppos[0];
  152. ppos[0] = pos;
  153. }
  154. av_free(buf);
  155. }
  156. static void mpc8_handle_chunk(AVFormatContext *s, int tag, int64_t chunk_pos, int64_t size)
  157. {
  158. AVIOContext *pb = s->pb;
  159. int64_t pos, off;
  160. switch(tag){
  161. case TAG_SEEKTBLOFF:
  162. pos = avio_tell(pb) + size;
  163. off = ffio_read_varlen(pb);
  164. mpc8_parse_seektable(s, chunk_pos + off);
  165. avio_seek(pb, pos, SEEK_SET);
  166. break;
  167. default:
  168. avio_skip(pb, size);
  169. }
  170. }
  171. static int mpc8_read_header(AVFormatContext *s)
  172. {
  173. MPCContext *c = s->priv_data;
  174. AVIOContext *pb = s->pb;
  175. AVStream *st;
  176. int tag = 0;
  177. int64_t size, pos;
  178. c->header_pos = avio_tell(pb);
  179. if(avio_rl32(pb) != TAG_MPCK){
  180. av_log(s, AV_LOG_ERROR, "Not a Musepack8 file\n");
  181. return AVERROR_INVALIDDATA;
  182. }
  183. while(!url_feof(pb)){
  184. pos = avio_tell(pb);
  185. mpc8_get_chunk_header(pb, &tag, &size);
  186. if(tag == TAG_STREAMHDR)
  187. break;
  188. mpc8_handle_chunk(s, tag, pos, size);
  189. }
  190. if(tag != TAG_STREAMHDR){
  191. av_log(s, AV_LOG_ERROR, "Stream header not found\n");
  192. return AVERROR_INVALIDDATA;
  193. }
  194. pos = avio_tell(pb);
  195. avio_skip(pb, 4); //CRC
  196. c->ver = avio_r8(pb);
  197. if(c->ver != 8){
  198. av_log(s, AV_LOG_ERROR, "Unknown stream version %d\n", c->ver);
  199. return AVERROR_PATCHWELCOME;
  200. }
  201. c->samples = ffio_read_varlen(pb);
  202. ffio_read_varlen(pb); //silence samples at the beginning
  203. st = avformat_new_stream(s, NULL);
  204. if (!st)
  205. return AVERROR(ENOMEM);
  206. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  207. st->codec->codec_id = CODEC_ID_MUSEPACK8;
  208. st->codec->bits_per_coded_sample = 16;
  209. st->codec->extradata_size = 2;
  210. st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
  211. avio_read(pb, st->codec->extradata, st->codec->extradata_size);
  212. st->codec->channels = (st->codec->extradata[1] >> 4) + 1;
  213. st->codec->sample_rate = mpc8_rate[st->codec->extradata[0] >> 5];
  214. avpriv_set_pts_info(st, 32, 1152 << (st->codec->extradata[1]&3)*2, st->codec->sample_rate);
  215. st->duration = c->samples / (1152 << (st->codec->extradata[1]&3)*2);
  216. size -= avio_tell(pb) - pos;
  217. if (size > 0)
  218. avio_skip(pb, size);
  219. if (pb->seekable) {
  220. int64_t pos = avio_tell(s->pb);
  221. c->apetag_start = ff_ape_parse_tag(s);
  222. avio_seek(s->pb, pos, SEEK_SET);
  223. }
  224. return 0;
  225. }
  226. static int mpc8_read_packet(AVFormatContext *s, AVPacket *pkt)
  227. {
  228. MPCContext *c = s->priv_data;
  229. int tag;
  230. int64_t pos, size;
  231. while(!url_feof(s->pb)){
  232. pos = avio_tell(s->pb);
  233. /* don't return bogus packets with the ape tag data */
  234. if (c->apetag_start && pos >= c->apetag_start)
  235. return AVERROR_EOF;
  236. mpc8_get_chunk_header(s->pb, &tag, &size);
  237. if (size < 0)
  238. return -1;
  239. if(tag == TAG_AUDIOPACKET){
  240. if(av_get_packet(s->pb, pkt, size) < 0)
  241. return AVERROR(ENOMEM);
  242. pkt->stream_index = 0;
  243. pkt->pts = c->frame;
  244. return 0;
  245. }
  246. if(tag == TAG_STREAMEND)
  247. return AVERROR(EIO);
  248. mpc8_handle_chunk(s, tag, pos, size);
  249. }
  250. return AVERROR_EOF;
  251. }
  252. static int mpc8_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
  253. {
  254. AVStream *st = s->streams[stream_index];
  255. MPCContext *c = s->priv_data;
  256. int index = av_index_search_timestamp(st, timestamp, flags);
  257. if(index < 0) return -1;
  258. if (avio_seek(s->pb, st->index_entries[index].pos, SEEK_SET) < 0)
  259. return -1;
  260. c->frame = st->index_entries[index].timestamp;
  261. return 0;
  262. }
  263. AVInputFormat ff_mpc8_demuxer = {
  264. .name = "mpc8",
  265. .long_name = NULL_IF_CONFIG_SMALL("Musepack SV8"),
  266. .priv_data_size = sizeof(MPCContext),
  267. .read_probe = mpc8_probe,
  268. .read_header = mpc8_read_header,
  269. .read_packet = mpc8_read_packet,
  270. .read_seek = mpc8_read_seek,
  271. };