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.

272 lines
7.5KB

  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 int mpc8_probe(AVProbeData *p)
  45. {
  46. if (p->buf_size < 16)
  47. return 0;
  48. if (AV_RL32(p->buf) != TAG_MPCK)
  49. return 0;
  50. if (p->buf[4] == 'S' && p->buf[5] == 'H') {
  51. int size = p->buf[6];
  52. if (size < 12 || size > 30)
  53. return 0;
  54. if (!AV_RL32(&p->buf[7])) //zero CRC is invalid
  55. return 0;
  56. //check whether some tag follows stream header or not
  57. if (p->buf[4 + size] < 'A' || p->buf[4 + size] > 'Z')
  58. return 0;
  59. if (p->buf[5 + size] < 'A' || p->buf[5 + size] > 'Z')
  60. return 0;
  61. if (p->buf[6 + size] < 3)
  62. return 0;
  63. return AVPROBE_SCORE_MAX;
  64. }
  65. /* file magic number should be followed by tag name which consists of
  66. two uppercase letters */
  67. if (p->buf[4] < 'A' || p->buf[4] > 'Z' || p->buf[5] < 'A' || p->buf[5] > 'Z')
  68. return 0;
  69. // tag size should be >= 3
  70. if (p->buf[6] < 3)
  71. return 0;
  72. // if first tag is not stream header, that's suspicious
  73. return AVPROBE_SCORE_MAX / 4;
  74. }
  75. static inline int64_t gb_get_v(GetBitContext *gb)
  76. {
  77. int64_t v = 0;
  78. int bits = 0;
  79. while(get_bits1(gb) && bits < 64-7){
  80. v <<= 7;
  81. v |= get_bits(gb, 7);
  82. bits += 7;
  83. }
  84. v <<= 7;
  85. v |= get_bits(gb, 7);
  86. return v;
  87. }
  88. static void mpc8_get_chunk_header(ByteIOContext *pb, int *tag, int64_t *size)
  89. {
  90. int64_t pos;
  91. pos = url_ftell(pb);
  92. *tag = get_le16(pb);
  93. *size = ff_get_v(pb);
  94. *size -= url_ftell(pb) - pos;
  95. }
  96. static void mpc8_parse_seektable(AVFormatContext *s, int64_t off)
  97. {
  98. MPCContext *c = s->priv_data;
  99. int tag;
  100. int64_t size, pos, ppos[2];
  101. uint8_t *buf;
  102. int i, t, seekd;
  103. GetBitContext gb;
  104. url_fseek(s->pb, off, SEEK_SET);
  105. mpc8_get_chunk_header(s->pb, &tag, &size);
  106. if(tag != TAG_SEEKTABLE){
  107. av_log(s, AV_LOG_ERROR, "No seek table at given position\n");
  108. return;
  109. }
  110. if(!(buf = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE)))
  111. return;
  112. get_buffer(s->pb, buf, size);
  113. init_get_bits(&gb, buf, size * 8);
  114. size = gb_get_v(&gb);
  115. if(size > UINT_MAX/4 || size > c->samples/1152){
  116. av_log(s, AV_LOG_ERROR, "Seek table is too big\n");
  117. return;
  118. }
  119. seekd = get_bits(&gb, 4);
  120. for(i = 0; i < 2; i++){
  121. pos = gb_get_v(&gb) + c->header_pos;
  122. ppos[1 - i] = pos;
  123. av_add_index_entry(s->streams[0], pos, i, 0, 0, AVINDEX_KEYFRAME);
  124. }
  125. for(; i < size; i++){
  126. t = get_unary(&gb, 1, 33) << 12;
  127. t += get_bits(&gb, 12);
  128. if(t & 1)
  129. t = -(t & ~1);
  130. pos = (t >> 1) + ppos[0]*2 - ppos[1];
  131. av_add_index_entry(s->streams[0], pos, i << seekd, 0, 0, AVINDEX_KEYFRAME);
  132. ppos[1] = ppos[0];
  133. ppos[0] = pos;
  134. }
  135. av_free(buf);
  136. }
  137. static void mpc8_handle_chunk(AVFormatContext *s, int tag, int64_t chunk_pos, int64_t size)
  138. {
  139. ByteIOContext *pb = s->pb;
  140. int64_t pos, off;
  141. switch(tag){
  142. case TAG_SEEKTBLOFF:
  143. pos = url_ftell(pb) + size;
  144. off = ff_get_v(pb);
  145. mpc8_parse_seektable(s, chunk_pos + off);
  146. url_fseek(pb, pos, SEEK_SET);
  147. break;
  148. default:
  149. url_fskip(pb, size);
  150. }
  151. }
  152. static int mpc8_read_header(AVFormatContext *s, AVFormatParameters *ap)
  153. {
  154. MPCContext *c = s->priv_data;
  155. ByteIOContext *pb = s->pb;
  156. AVStream *st;
  157. int tag = 0;
  158. int64_t size, pos;
  159. c->header_pos = url_ftell(pb);
  160. if(get_le32(pb) != TAG_MPCK){
  161. av_log(s, AV_LOG_ERROR, "Not a Musepack8 file\n");
  162. return -1;
  163. }
  164. while(!url_feof(pb)){
  165. pos = url_ftell(pb);
  166. mpc8_get_chunk_header(pb, &tag, &size);
  167. if(tag == TAG_STREAMHDR)
  168. break;
  169. mpc8_handle_chunk(s, tag, pos, size);
  170. }
  171. if(tag != TAG_STREAMHDR){
  172. av_log(s, AV_LOG_ERROR, "Stream header not found\n");
  173. return -1;
  174. }
  175. pos = url_ftell(pb);
  176. url_fskip(pb, 4); //CRC
  177. c->ver = get_byte(pb);
  178. if(c->ver != 8){
  179. av_log(s, AV_LOG_ERROR, "Unknown stream version %d\n", c->ver);
  180. return -1;
  181. }
  182. c->samples = ff_get_v(pb);
  183. ff_get_v(pb); //silence samples at the beginning
  184. st = av_new_stream(s, 0);
  185. if (!st)
  186. return AVERROR(ENOMEM);
  187. st->codec->codec_type = CODEC_TYPE_AUDIO;
  188. st->codec->codec_id = CODEC_ID_MUSEPACK8;
  189. st->codec->bits_per_coded_sample = 16;
  190. st->codec->extradata_size = 2;
  191. st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
  192. get_buffer(pb, st->codec->extradata, st->codec->extradata_size);
  193. st->codec->channels = (st->codec->extradata[1] >> 4) + 1;
  194. st->codec->sample_rate = mpc8_rate[st->codec->extradata[0] >> 5];
  195. av_set_pts_info(st, 32, 1152 << (st->codec->extradata[1]&3)*2, st->codec->sample_rate);
  196. st->duration = c->samples / (1152 << (st->codec->extradata[1]&3)*2);
  197. size -= url_ftell(pb) - pos;
  198. return 0;
  199. }
  200. static int mpc8_read_packet(AVFormatContext *s, AVPacket *pkt)
  201. {
  202. MPCContext *c = s->priv_data;
  203. int tag;
  204. int64_t pos, size;
  205. while(!url_feof(s->pb)){
  206. pos = url_ftell(s->pb);
  207. mpc8_get_chunk_header(s->pb, &tag, &size);
  208. if(tag == TAG_AUDIOPACKET){
  209. if(av_get_packet(s->pb, pkt, size) < 0)
  210. return AVERROR(ENOMEM);
  211. pkt->stream_index = 0;
  212. pkt->pts = c->frame;
  213. return 0;
  214. }
  215. if(tag == TAG_STREAMEND)
  216. return AVERROR(EIO);
  217. mpc8_handle_chunk(s, tag, pos, size);
  218. }
  219. return 0;
  220. }
  221. static int mpc8_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
  222. {
  223. AVStream *st = s->streams[stream_index];
  224. MPCContext *c = s->priv_data;
  225. int index = av_index_search_timestamp(st, timestamp, flags);
  226. if(index < 0) return -1;
  227. url_fseek(s->pb, st->index_entries[index].pos, SEEK_SET);
  228. c->frame = st->index_entries[index].timestamp;
  229. return 0;
  230. }
  231. AVInputFormat mpc8_demuxer = {
  232. "mpc8",
  233. NULL_IF_CONFIG_SMALL("Musepack SV8"),
  234. sizeof(MPCContext),
  235. mpc8_probe,
  236. mpc8_read_header,
  237. mpc8_read_packet,
  238. NULL,
  239. mpc8_read_seek,
  240. };