mpc8.c 7.8 KB

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