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.

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