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.

378 lines
12KB

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