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.

400 lines
13KB

  1. /*
  2. * 4X Technologies .4xm File Demuxer (no muxer)
  3. * Copyright (c) 2003 The FFmpeg project
  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. /**
  22. * @file
  23. * 4X Technologies file demuxer
  24. * by Mike Melanson (melanson@pcisys.net)
  25. * for more information on the .4xm file format, visit:
  26. * http://www.pcisys.net/~melanson/codecs/
  27. */
  28. #include "libavutil/intreadwrite.h"
  29. #include "libavutil/intfloat.h"
  30. #include "libavcodec/internal.h"
  31. #include "avformat.h"
  32. #include "internal.h"
  33. #define RIFF_TAG MKTAG('R', 'I', 'F', 'F')
  34. #define FOURXMV_TAG MKTAG('4', 'X', 'M', 'V')
  35. #define LIST_TAG MKTAG('L', 'I', 'S', 'T')
  36. #define HEAD_TAG MKTAG('H', 'E', 'A', 'D')
  37. #define TRK__TAG MKTAG('T', 'R', 'K', '_')
  38. #define MOVI_TAG MKTAG('M', 'O', 'V', 'I')
  39. #define VTRK_TAG MKTAG('V', 'T', 'R', 'K')
  40. #define STRK_TAG MKTAG('S', 'T', 'R', 'K')
  41. #define std__TAG MKTAG('s', 't', 'd', '_')
  42. #define name_TAG MKTAG('n', 'a', 'm', 'e')
  43. #define vtrk_TAG MKTAG('v', 't', 'r', 'k')
  44. #define strk_TAG MKTAG('s', 't', 'r', 'k')
  45. #define ifrm_TAG MKTAG('i', 'f', 'r', 'm')
  46. #define pfrm_TAG MKTAG('p', 'f', 'r', 'm')
  47. #define cfrm_TAG MKTAG('c', 'f', 'r', 'm')
  48. #define ifr2_TAG MKTAG('i', 'f', 'r', '2')
  49. #define pfr2_TAG MKTAG('p', 'f', 'r', '2')
  50. #define cfr2_TAG MKTAG('c', 'f', 'r', '2')
  51. #define snd__TAG MKTAG('s', 'n', 'd', '_')
  52. #define vtrk_SIZE 0x44
  53. #define strk_SIZE 0x28
  54. #define GET_LIST_HEADER() \
  55. fourcc_tag = avio_rl32(pb); \
  56. size = avio_rl32(pb); \
  57. if (fourcc_tag != LIST_TAG) \
  58. return AVERROR_INVALIDDATA; \
  59. fourcc_tag = avio_rl32(pb);
  60. typedef struct AudioTrack {
  61. int sample_rate;
  62. int bits;
  63. int channels;
  64. int stream_index;
  65. int adpcm;
  66. int64_t audio_pts;
  67. } AudioTrack;
  68. typedef struct FourxmDemuxContext {
  69. int video_stream_index;
  70. int track_count;
  71. AudioTrack *tracks;
  72. int64_t video_pts;
  73. AVRational fps;
  74. } FourxmDemuxContext;
  75. static int fourxm_probe(const AVProbeData *p)
  76. {
  77. if ((AV_RL32(&p->buf[0]) != RIFF_TAG) ||
  78. (AV_RL32(&p->buf[8]) != FOURXMV_TAG))
  79. return 0;
  80. return AVPROBE_SCORE_MAX;
  81. }
  82. static int parse_vtrk(AVFormatContext *s,
  83. FourxmDemuxContext *fourxm, uint8_t *buf, int size,
  84. int left)
  85. {
  86. AVStream *st;
  87. /* check that there is enough data */
  88. if (size != vtrk_SIZE || left < size + 8) {
  89. return AVERROR_INVALIDDATA;
  90. }
  91. /* allocate a new AVStream */
  92. st = avformat_new_stream(s, NULL);
  93. if (!st)
  94. return AVERROR(ENOMEM);
  95. avpriv_set_pts_info(st, 60, fourxm->fps.den, fourxm->fps.num);
  96. fourxm->video_stream_index = st->index;
  97. st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  98. st->codecpar->codec_id = AV_CODEC_ID_4XM;
  99. st->codecpar->extradata = av_mallocz(4 + AV_INPUT_BUFFER_PADDING_SIZE);
  100. if (!st->codecpar->extradata)
  101. return AVERROR(ENOMEM);
  102. st->codecpar->extradata_size = 4;
  103. AV_WL32(st->codecpar->extradata, AV_RL32(buf + 16));
  104. st->codecpar->width = AV_RL32(buf + 36);
  105. st->codecpar->height = AV_RL32(buf + 40);
  106. return 0;
  107. }
  108. static int parse_strk(AVFormatContext *s,
  109. FourxmDemuxContext *fourxm, uint8_t *buf, int size,
  110. int left)
  111. {
  112. AVStream *st;
  113. int track;
  114. /* check that there is enough data */
  115. if (size != strk_SIZE || left < size + 8)
  116. return AVERROR_INVALIDDATA;
  117. track = AV_RL32(buf + 8);
  118. if ((unsigned)track >= UINT_MAX / sizeof(AudioTrack) - 1) {
  119. av_log(s, AV_LOG_ERROR, "current_track too large\n");
  120. return AVERROR_INVALIDDATA;
  121. }
  122. if (track + 1 > fourxm->track_count) {
  123. if (av_reallocp_array(&fourxm->tracks, track + 1, sizeof(AudioTrack)))
  124. return AVERROR(ENOMEM);
  125. memset(&fourxm->tracks[fourxm->track_count], 0,
  126. sizeof(AudioTrack) * (track + 1 - fourxm->track_count));
  127. fourxm->track_count = track + 1;
  128. }
  129. fourxm->tracks[track].adpcm = AV_RL32(buf + 12);
  130. fourxm->tracks[track].channels = AV_RL32(buf + 36);
  131. fourxm->tracks[track].sample_rate = AV_RL32(buf + 40);
  132. fourxm->tracks[track].bits = AV_RL32(buf + 44);
  133. fourxm->tracks[track].audio_pts = 0;
  134. if (fourxm->tracks[track].channels <= 0 ||
  135. fourxm->tracks[track].channels > FF_SANE_NB_CHANNELS ||
  136. fourxm->tracks[track].sample_rate <= 0 ||
  137. fourxm->tracks[track].bits <= 0 ||
  138. fourxm->tracks[track].bits > INT_MAX / FF_SANE_NB_CHANNELS) {
  139. av_log(s, AV_LOG_ERROR, "audio header invalid\n");
  140. return AVERROR_INVALIDDATA;
  141. }
  142. if (!fourxm->tracks[track].adpcm && fourxm->tracks[track].bits<8) {
  143. av_log(s, AV_LOG_ERROR, "bits unspecified for non ADPCM\n");
  144. return AVERROR_INVALIDDATA;
  145. }
  146. if (fourxm->tracks[track].sample_rate > INT64_MAX / fourxm->tracks[track].bits / fourxm->tracks[track].channels) {
  147. av_log(s, AV_LOG_ERROR, "Overflow during bit rate calculation %d * %d * %d\n",
  148. fourxm->tracks[track].sample_rate, fourxm->tracks[track].bits, fourxm->tracks[track].channels);
  149. return AVERROR_INVALIDDATA;
  150. }
  151. /* allocate a new AVStream */
  152. st = avformat_new_stream(s, NULL);
  153. if (!st)
  154. return AVERROR(ENOMEM);
  155. st->id = track;
  156. avpriv_set_pts_info(st, 60, 1, fourxm->tracks[track].sample_rate);
  157. fourxm->tracks[track].stream_index = st->index;
  158. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  159. st->codecpar->codec_tag = 0;
  160. st->codecpar->channels = fourxm->tracks[track].channels;
  161. st->codecpar->sample_rate = fourxm->tracks[track].sample_rate;
  162. st->codecpar->bits_per_coded_sample = fourxm->tracks[track].bits;
  163. st->codecpar->bit_rate = (int64_t)st->codecpar->channels *
  164. st->codecpar->sample_rate *
  165. st->codecpar->bits_per_coded_sample;
  166. st->codecpar->block_align = st->codecpar->channels *
  167. st->codecpar->bits_per_coded_sample;
  168. if (fourxm->tracks[track].adpcm){
  169. st->codecpar->codec_id = AV_CODEC_ID_ADPCM_4XM;
  170. } else if (st->codecpar->bits_per_coded_sample == 8) {
  171. st->codecpar->codec_id = AV_CODEC_ID_PCM_U8;
  172. } else
  173. st->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE;
  174. return 0;
  175. }
  176. static int fourxm_read_header(AVFormatContext *s)
  177. {
  178. AVIOContext *pb = s->pb;
  179. unsigned int fourcc_tag;
  180. unsigned int size;
  181. int header_size;
  182. FourxmDemuxContext *fourxm = s->priv_data;
  183. unsigned char *header;
  184. int i, ret;
  185. fourxm->track_count = 0;
  186. fourxm->tracks = NULL;
  187. fourxm->fps = (AVRational){1,1};
  188. /* skip the first 3 32-bit numbers */
  189. avio_skip(pb, 12);
  190. /* check for LIST-HEAD */
  191. GET_LIST_HEADER();
  192. header_size = size - 4;
  193. if (fourcc_tag != HEAD_TAG || header_size < 0)
  194. return AVERROR_INVALIDDATA;
  195. /* allocate space for the header and load the whole thing */
  196. header = av_malloc(header_size);
  197. if (!header)
  198. return AVERROR(ENOMEM);
  199. if (avio_read(pb, header, header_size) != header_size) {
  200. av_free(header);
  201. return AVERROR(EIO);
  202. }
  203. /* take the lazy approach and search for any and all vtrk and strk chunks */
  204. for (i = 0; i < header_size - 8; i++) {
  205. fourcc_tag = AV_RL32(&header[i]);
  206. size = AV_RL32(&header[i + 4]);
  207. if (size > header_size - i - 8 && (fourcc_tag == vtrk_TAG || fourcc_tag == strk_TAG)) {
  208. av_log(s, AV_LOG_ERROR, "chunk larger than array %d>%d\n", size, header_size - i - 8);
  209. ret = AVERROR_INVALIDDATA;
  210. goto fail;
  211. }
  212. if (fourcc_tag == std__TAG) {
  213. if (header_size - i < 16) {
  214. av_log(s, AV_LOG_ERROR, "std TAG truncated\n");
  215. ret = AVERROR_INVALIDDATA;
  216. goto fail;
  217. }
  218. fourxm->fps = av_d2q(av_int2float(AV_RL32(&header[i + 12])), 10000);
  219. } else if (fourcc_tag == vtrk_TAG) {
  220. if ((ret = parse_vtrk(s, fourxm, header + i, size,
  221. header_size - i)) < 0)
  222. goto fail;
  223. i += 8 + size;
  224. } else if (fourcc_tag == strk_TAG) {
  225. if ((ret = parse_strk(s, fourxm, header + i, size,
  226. header_size - i)) < 0)
  227. goto fail;
  228. i += 8 + size;
  229. }
  230. }
  231. /* skip over the LIST-MOVI chunk (which is where the stream should be */
  232. GET_LIST_HEADER();
  233. if (fourcc_tag != MOVI_TAG) {
  234. ret = AVERROR_INVALIDDATA;
  235. goto fail;
  236. }
  237. av_free(header);
  238. /* initialize context members */
  239. fourxm->video_pts = -1; /* first frame will push to 0 */
  240. return 0;
  241. fail:
  242. av_freep(&fourxm->tracks);
  243. av_free(header);
  244. return ret;
  245. }
  246. static int fourxm_read_packet(AVFormatContext *s,
  247. AVPacket *pkt)
  248. {
  249. FourxmDemuxContext *fourxm = s->priv_data;
  250. AVIOContext *pb = s->pb;
  251. unsigned int fourcc_tag;
  252. unsigned int size;
  253. int ret = 0;
  254. unsigned int track_number;
  255. int packet_read = 0;
  256. unsigned char header[8];
  257. int audio_frame_count;
  258. while (!packet_read) {
  259. if ((ret = avio_read(s->pb, header, 8)) < 0)
  260. return ret;
  261. fourcc_tag = AV_RL32(&header[0]);
  262. size = AV_RL32(&header[4]);
  263. if (avio_feof(pb))
  264. return AVERROR(EIO);
  265. switch (fourcc_tag) {
  266. case LIST_TAG:
  267. /* this is a good time to bump the video pts */
  268. fourxm->video_pts++;
  269. /* skip the LIST-* tag and move on to the next fourcc */
  270. avio_rl32(pb);
  271. break;
  272. case ifrm_TAG:
  273. case pfrm_TAG:
  274. case cfrm_TAG:
  275. case ifr2_TAG:
  276. case pfr2_TAG:
  277. case cfr2_TAG:
  278. /* allocate 8 more bytes than 'size' to account for fourcc
  279. * and size */
  280. if (size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE - 8)
  281. return AVERROR_INVALIDDATA;
  282. if ((ret = av_new_packet(pkt, size + 8)) < 0)
  283. return ret;
  284. pkt->stream_index = fourxm->video_stream_index;
  285. pkt->pts = fourxm->video_pts;
  286. pkt->pos = avio_tell(s->pb);
  287. memcpy(pkt->data, header, 8);
  288. ret = avio_read(s->pb, &pkt->data[8], size);
  289. if (ret < 0) {
  290. av_packet_unref(pkt);
  291. } else {
  292. packet_read = 1;
  293. av_shrink_packet(pkt, ret + 8);
  294. }
  295. break;
  296. case snd__TAG:
  297. track_number = avio_rl32(pb);
  298. avio_skip(pb, 4);
  299. size -= 8;
  300. if (track_number < fourxm->track_count &&
  301. fourxm->tracks[track_number].channels > 0) {
  302. ret = av_get_packet(s->pb, pkt, size);
  303. if (ret < 0)
  304. return ret;
  305. pkt->stream_index =
  306. fourxm->tracks[track_number].stream_index;
  307. pkt->pts = fourxm->tracks[track_number].audio_pts;
  308. packet_read = 1;
  309. /* pts accounting */
  310. audio_frame_count = size;
  311. if (fourxm->tracks[track_number].adpcm)
  312. audio_frame_count -= 2 * (fourxm->tracks[track_number].channels);
  313. audio_frame_count /= fourxm->tracks[track_number].channels;
  314. if (fourxm->tracks[track_number].adpcm) {
  315. audio_frame_count *= 2;
  316. } else
  317. audio_frame_count /=
  318. (fourxm->tracks[track_number].bits / 8);
  319. fourxm->tracks[track_number].audio_pts += audio_frame_count;
  320. } else {
  321. avio_skip(pb, size);
  322. }
  323. break;
  324. default:
  325. avio_skip(pb, size);
  326. break;
  327. }
  328. }
  329. return ret;
  330. }
  331. static int fourxm_read_close(AVFormatContext *s)
  332. {
  333. FourxmDemuxContext *fourxm = s->priv_data;
  334. av_freep(&fourxm->tracks);
  335. return 0;
  336. }
  337. AVInputFormat ff_fourxm_demuxer = {
  338. .name = "4xm",
  339. .long_name = NULL_IF_CONFIG_SMALL("4X Technologies"),
  340. .priv_data_size = sizeof(FourxmDemuxContext),
  341. .read_probe = fourxm_probe,
  342. .read_header = fourxm_read_header,
  343. .read_packet = fourxm_read_packet,
  344. .read_close = fourxm_read_close,
  345. };