mpc8.c 8.3 KB

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