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.

367 lines
11KB

  1. /*
  2. * 4X Technologies .4xm File Demuxer (no muxer)
  3. * Copyright (c) 2003 The ffmpeg Project
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. /**
  20. * @file 4xm.c
  21. * 4X Technologies file demuxer
  22. * by Mike Melanson (melanson@pcisys.net)
  23. * for more information on the .4xm file format, visit:
  24. * http://www.pcisys.net/~melanson/codecs/
  25. */
  26. #include "avformat.h"
  27. #define RIFF_TAG MKTAG('R', 'I', 'F', 'F')
  28. #define _4XMV_TAG MKTAG('4', 'X', 'M', 'V')
  29. #define LIST_TAG MKTAG('L', 'I', 'S', 'T')
  30. #define HEAD_TAG MKTAG('H', 'E', 'A', 'D')
  31. #define TRK__TAG MKTAG('T', 'R', 'K', '_')
  32. #define MOVI_TAG MKTAG('M', 'O', 'V', 'I')
  33. #define VTRK_TAG MKTAG('V', 'T', 'R', 'K')
  34. #define STRK_TAG MKTAG('S', 'T', 'R', 'K')
  35. #define std__TAG MKTAG('s', 't', 'd', '_')
  36. #define name_TAG MKTAG('n', 'a', 'm', 'e')
  37. #define vtrk_TAG MKTAG('v', 't', 'r', 'k')
  38. #define strk_TAG MKTAG('s', 't', 'r', 'k')
  39. #define ifrm_TAG MKTAG('i', 'f', 'r', 'm')
  40. #define pfrm_TAG MKTAG('p', 'f', 'r', 'm')
  41. #define cfrm_TAG MKTAG('c', 'f', 'r', 'm')
  42. #define snd__TAG MKTAG('s', 'n', 'd', '_')
  43. #define vtrk_SIZE 0x44
  44. #define strk_SIZE 0x28
  45. #define GET_LIST_HEADER() \
  46. fourcc_tag = get_le32(pb); \
  47. size = get_le32(pb); \
  48. if (fourcc_tag != LIST_TAG) \
  49. return AVERROR_INVALIDDATA; \
  50. fourcc_tag = get_le32(pb);
  51. typedef struct AudioTrack {
  52. int sample_rate;
  53. int bits;
  54. int channels;
  55. int stream_index;
  56. int adpcm;
  57. } AudioTrack;
  58. typedef struct FourxmDemuxContext {
  59. int width;
  60. int height;
  61. int video_stream_index;
  62. int track_count;
  63. AudioTrack *tracks;
  64. int selected_track;
  65. int64_t audio_pts;
  66. int64_t video_pts;
  67. int video_pts_inc;
  68. float fps;
  69. } FourxmDemuxContext;
  70. static float get_le_float(unsigned char *buffer)
  71. {
  72. float f;
  73. unsigned char *float_buffer = (unsigned char *)&f;
  74. #ifdef WORDS_BIGENDIAN
  75. float_buffer[0] = buffer[3];
  76. float_buffer[1] = buffer[2];
  77. float_buffer[2] = buffer[1];
  78. float_buffer[3] = buffer[0];
  79. #else
  80. float_buffer[0] = buffer[0];
  81. float_buffer[1] = buffer[1];
  82. float_buffer[2] = buffer[2];
  83. float_buffer[3] = buffer[3];
  84. #endif
  85. return f;
  86. }
  87. static int fourxm_probe(AVProbeData *p)
  88. {
  89. if (p->buf_size < 12)
  90. return 0;
  91. if ((LE_32(&p->buf[0]) != RIFF_TAG) ||
  92. (LE_32(&p->buf[8]) != _4XMV_TAG))
  93. return 0;
  94. return AVPROBE_SCORE_MAX;
  95. }
  96. static int fourxm_read_header(AVFormatContext *s,
  97. AVFormatParameters *ap)
  98. {
  99. ByteIOContext *pb = &s->pb;
  100. unsigned int fourcc_tag;
  101. unsigned int size;
  102. int header_size;
  103. FourxmDemuxContext *fourxm = (FourxmDemuxContext *)s->priv_data;
  104. unsigned char *header;
  105. int i;
  106. int current_track = -1;
  107. AVStream *st;
  108. fourxm->track_count = 0;
  109. fourxm->tracks = NULL;
  110. fourxm->selected_track = 0;
  111. fourxm->fps = 1.0;
  112. /* skip the first 3 32-bit numbers */
  113. url_fseek(pb, 12, SEEK_CUR);
  114. /* check for LIST-HEAD */
  115. GET_LIST_HEADER();
  116. header_size = size - 4;
  117. if (fourcc_tag != HEAD_TAG)
  118. return AVERROR_INVALIDDATA;
  119. /* allocate space for the header and load the whole thing */
  120. header = av_malloc(header_size);
  121. if (!header)
  122. return AVERROR_NOMEM;
  123. if (get_buffer(pb, header, header_size) != header_size)
  124. return AVERROR_IO;
  125. /* take the lazy approach and search for any and all vtrk and strk chunks */
  126. for (i = 0; i < header_size - 8; i++) {
  127. fourcc_tag = LE_32(&header[i]);
  128. size = LE_32(&header[i + 4]);
  129. if (fourcc_tag == std__TAG) {
  130. fourxm->fps = get_le_float(&header[i + 12]);
  131. fourxm->video_pts_inc = (int)(90000.0 / fourxm->fps);
  132. } else if (fourcc_tag == vtrk_TAG) {
  133. /* check that there is enough data */
  134. if (size != vtrk_SIZE) {
  135. av_free(header);
  136. return AVERROR_INVALIDDATA;
  137. }
  138. fourxm->width = LE_32(&header[i + 36]);
  139. fourxm->height = LE_32(&header[i + 40]);
  140. i += 8 + size;
  141. /* allocate a new AVStream */
  142. st = av_new_stream(s, 0);
  143. if (!st)
  144. return AVERROR_NOMEM;
  145. av_set_pts_info(st, 33, 1, 90000);
  146. fourxm->video_stream_index = st->index;
  147. st->codec.frame_rate = fourxm->fps;
  148. st->codec.frame_rate_base = 1.0;
  149. st->codec.codec_type = CODEC_TYPE_VIDEO;
  150. st->codec.codec_id = CODEC_ID_4XM;
  151. st->codec.codec_tag = 0; /* no fourcc */
  152. st->codec.width = fourxm->width;
  153. st->codec.height = fourxm->height;
  154. } else if (fourcc_tag == strk_TAG) {
  155. /* check that there is enough data */
  156. if (size != strk_SIZE) {
  157. av_free(header);
  158. return AVERROR_INVALIDDATA;
  159. }
  160. current_track = LE_32(&header[i + 8]);
  161. if (current_track + 1 > fourxm->track_count) {
  162. fourxm->track_count = current_track + 1;
  163. if((unsigned)fourxm->track_count >= UINT_MAX / sizeof(AudioTrack))
  164. return -1;
  165. fourxm->tracks = av_realloc(fourxm->tracks,
  166. fourxm->track_count * sizeof(AudioTrack));
  167. if (!fourxm->tracks) {
  168. av_free(header);
  169. return AVERROR_NOMEM;
  170. }
  171. }
  172. fourxm->tracks[current_track].adpcm = LE_32(&header[i + 12]);
  173. fourxm->tracks[current_track].channels = LE_32(&header[i + 36]);
  174. fourxm->tracks[current_track].sample_rate = LE_32(&header[i + 40]);
  175. fourxm->tracks[current_track].bits = LE_32(&header[i + 44]);
  176. i += 8 + size;
  177. /* allocate a new AVStream */
  178. st = av_new_stream(s, current_track);
  179. if (!st)
  180. return AVERROR_NOMEM;
  181. /* set the pts reference (1 pts = 1/90000) */
  182. av_set_pts_info(st, 33, 1, 90000);
  183. fourxm->tracks[current_track].stream_index = st->index;
  184. st->codec.codec_type = CODEC_TYPE_AUDIO;
  185. st->codec.codec_tag = 1;
  186. st->codec.channels = fourxm->tracks[current_track].channels;
  187. st->codec.sample_rate = fourxm->tracks[current_track].sample_rate;
  188. st->codec.bits_per_sample = fourxm->tracks[current_track].bits;
  189. st->codec.bit_rate = st->codec.channels * st->codec.sample_rate *
  190. st->codec.bits_per_sample;
  191. st->codec.block_align = st->codec.channels * st->codec.bits_per_sample;
  192. if (fourxm->tracks[current_track].adpcm)
  193. st->codec.codec_id = CODEC_ID_ADPCM_4XM;
  194. else if (st->codec.bits_per_sample == 8)
  195. st->codec.codec_id = CODEC_ID_PCM_U8;
  196. else
  197. st->codec.codec_id = CODEC_ID_PCM_S16LE;
  198. }
  199. }
  200. av_free(header);
  201. /* skip over the LIST-MOVI chunk (which is where the stream should be */
  202. GET_LIST_HEADER();
  203. if (fourcc_tag != MOVI_TAG)
  204. return AVERROR_INVALIDDATA;
  205. /* initialize context members */
  206. fourxm->video_pts = -fourxm->video_pts_inc; /* first frame will push to 0 */
  207. fourxm->audio_pts = 0;
  208. return 0;
  209. }
  210. static int fourxm_read_packet(AVFormatContext *s,
  211. AVPacket *pkt)
  212. {
  213. FourxmDemuxContext *fourxm = s->priv_data;
  214. ByteIOContext *pb = &s->pb;
  215. unsigned int fourcc_tag;
  216. unsigned int size, out_size;
  217. int ret = 0;
  218. int track_number;
  219. int packet_read = 0;
  220. unsigned char header[8];
  221. int64_t pts_inc;
  222. int audio_frame_count;
  223. while (!packet_read) {
  224. if ((ret = get_buffer(&s->pb, header, 8)) < 0)
  225. return ret;
  226. fourcc_tag = LE_32(&header[0]);
  227. size = LE_32(&header[4]);
  228. if (url_feof(pb))
  229. return AVERROR_IO;
  230. switch (fourcc_tag) {
  231. case LIST_TAG:
  232. /* this is a good time to bump the video pts */
  233. fourxm->video_pts += fourxm->video_pts_inc;
  234. /* skip the LIST-* tag and move on to the next fourcc */
  235. get_le32(pb);
  236. break;
  237. case ifrm_TAG:
  238. case pfrm_TAG:
  239. case cfrm_TAG:{
  240. /* allocate 8 more bytes than 'size' to account for fourcc
  241. * and size */
  242. if (size + 8 < size || av_new_packet(pkt, size + 8))
  243. return AVERROR_IO;
  244. pkt->stream_index = fourxm->video_stream_index;
  245. pkt->pts = fourxm->video_pts;
  246. memcpy(pkt->data, header, 8);
  247. ret = get_buffer(&s->pb, &pkt->data[8], size);
  248. if (ret < 0)
  249. av_free_packet(pkt);
  250. else
  251. packet_read = 1;
  252. break;
  253. }
  254. case snd__TAG:
  255. track_number = get_le32(pb);
  256. out_size= get_le32(pb);
  257. size-=8;
  258. if (track_number == fourxm->selected_track) {
  259. if (av_new_packet(pkt, size))
  260. return AVERROR_IO;
  261. pkt->stream_index =
  262. fourxm->tracks[fourxm->selected_track].stream_index;
  263. pkt->pts = fourxm->audio_pts;
  264. ret = get_buffer(&s->pb, pkt->data, size);
  265. if (ret < 0)
  266. av_free_packet(pkt);
  267. else
  268. packet_read = 1;
  269. /* pts accounting */
  270. audio_frame_count = size;
  271. if (fourxm->tracks[fourxm->selected_track].adpcm)
  272. audio_frame_count -=
  273. 2 * (fourxm->tracks[fourxm->selected_track].channels);
  274. audio_frame_count /=
  275. fourxm->tracks[fourxm->selected_track].channels;
  276. if (fourxm->tracks[fourxm->selected_track].adpcm)
  277. audio_frame_count *= 2;
  278. else
  279. audio_frame_count /=
  280. (fourxm->tracks[fourxm->selected_track].bits / 8);
  281. pts_inc = audio_frame_count;
  282. pts_inc *= 90000;
  283. pts_inc /= fourxm->tracks[fourxm->selected_track].sample_rate;
  284. fourxm->audio_pts += pts_inc;
  285. } else {
  286. url_fseek(pb, size, SEEK_CUR);
  287. }
  288. break;
  289. default:
  290. url_fseek(pb, size, SEEK_CUR);
  291. break;
  292. }
  293. }
  294. return ret;
  295. }
  296. static int fourxm_read_close(AVFormatContext *s)
  297. {
  298. FourxmDemuxContext *fourxm = (FourxmDemuxContext *)s->priv_data;
  299. av_free(fourxm->tracks);
  300. return 0;
  301. }
  302. static AVInputFormat fourxm_iformat = {
  303. "4xm",
  304. "4X Technologies format",
  305. sizeof(FourxmDemuxContext),
  306. fourxm_probe,
  307. fourxm_read_header,
  308. fourxm_read_packet,
  309. fourxm_read_close,
  310. };
  311. int fourxm_init(void)
  312. {
  313. av_register_input_format(&fourxm_iformat);
  314. return 0;
  315. }