mpc8.c 6.6 KB

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