mpc8.c 7.7 KB

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