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.

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