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.

365 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. fourxm->video_stream_index = st->index;
  146. st->codec.frame_rate = fourxm->fps;
  147. st->codec.frame_rate_base = 1.0;
  148. st->codec.codec_type = CODEC_TYPE_VIDEO;
  149. st->codec.codec_id = CODEC_ID_4XM;
  150. st->codec.codec_tag = 0; /* no fourcc */
  151. st->codec.width = fourxm->width;
  152. st->codec.height = fourxm->height;
  153. } else if (fourcc_tag == strk_TAG) {
  154. /* check that there is enough data */
  155. if (size != strk_SIZE) {
  156. av_free(header);
  157. return AVERROR_INVALIDDATA;
  158. }
  159. current_track = LE_32(&header[i + 8]);
  160. if (current_track + 1 > fourxm->track_count) {
  161. fourxm->track_count = current_track + 1;
  162. fourxm->tracks = av_realloc(fourxm->tracks,
  163. fourxm->track_count * sizeof(AudioTrack));
  164. if (!fourxm->tracks) {
  165. av_free(header);
  166. return AVERROR_NOMEM;
  167. }
  168. }
  169. fourxm->tracks[current_track].adpcm = LE_32(&header[i + 12]);
  170. fourxm->tracks[current_track].channels = LE_32(&header[i + 36]);
  171. fourxm->tracks[current_track].sample_rate = LE_32(&header[i + 40]);
  172. fourxm->tracks[current_track].bits = LE_32(&header[i + 44]);
  173. i += 8 + size;
  174. /* allocate a new AVStream */
  175. st = av_new_stream(s, current_track);
  176. if (!st)
  177. return AVERROR_NOMEM;
  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 = -fourxm->video_pts_inc; /* first frame will push to 0 */
  202. fourxm->audio_pts = 0;
  203. /* set the pts reference (1 pts = 1/90000) */
  204. s->pts_num = 1;
  205. s->pts_den = 90000;
  206. return 0;
  207. }
  208. static int fourxm_read_packet(AVFormatContext *s,
  209. AVPacket *pkt)
  210. {
  211. FourxmDemuxContext *fourxm = s->priv_data;
  212. ByteIOContext *pb = &s->pb;
  213. unsigned int fourcc_tag;
  214. unsigned int size, out_size;
  215. int ret = 0;
  216. int track_number;
  217. int packet_read = 0;
  218. unsigned char header[8];
  219. int64_t pts_inc;
  220. int audio_frame_count;
  221. while (!packet_read) {
  222. if ((ret = get_buffer(&s->pb, header, 8)) < 0)
  223. return ret;
  224. fourcc_tag = LE_32(&header[0]);
  225. size = LE_32(&header[4]);
  226. if (url_feof(pb))
  227. return -EIO;
  228. switch (fourcc_tag) {
  229. case LIST_TAG:
  230. /* this is a good time to bump the video pts */
  231. fourxm->video_pts += fourxm->video_pts_inc;
  232. /* skip the LIST-* tag and move on to the next fourcc */
  233. get_le32(pb);
  234. break;
  235. case ifrm_TAG:
  236. case pfrm_TAG:
  237. case cfrm_TAG:{
  238. /* allocate 8 more bytes than 'size' to account for fourcc
  239. * and size */
  240. if (av_new_packet(pkt, size + 8))
  241. return -EIO;
  242. pkt->stream_index = fourxm->video_stream_index;
  243. pkt->pts = fourxm->video_pts;
  244. memcpy(pkt->data, header, 8);
  245. ret = get_buffer(&s->pb, &pkt->data[8], size);
  246. if (ret < 0)
  247. av_free_packet(pkt);
  248. else
  249. packet_read = 1;
  250. break;
  251. }
  252. case snd__TAG:
  253. track_number = get_le32(pb);
  254. out_size= get_le32(pb);
  255. size-=8;
  256. if (track_number == fourxm->selected_track) {
  257. if (av_new_packet(pkt, size))
  258. return -EIO;
  259. pkt->stream_index =
  260. fourxm->tracks[fourxm->selected_track].stream_index;
  261. pkt->pts = fourxm->audio_pts;
  262. ret = get_buffer(&s->pb, pkt->data, size);
  263. if (ret < 0)
  264. av_free_packet(pkt);
  265. else
  266. packet_read = 1;
  267. /* pts accounting */
  268. audio_frame_count = size;
  269. if (fourxm->tracks[fourxm->selected_track].adpcm)
  270. audio_frame_count -=
  271. 2 * (fourxm->tracks[fourxm->selected_track].channels);
  272. audio_frame_count /=
  273. fourxm->tracks[fourxm->selected_track].channels;
  274. if (fourxm->tracks[fourxm->selected_track].adpcm)
  275. audio_frame_count *= 2;
  276. else
  277. audio_frame_count /=
  278. (fourxm->tracks[fourxm->selected_track].bits / 8);
  279. pts_inc = audio_frame_count;
  280. pts_inc *= 90000;
  281. pts_inc /= fourxm->tracks[fourxm->selected_track].sample_rate;
  282. fourxm->audio_pts += pts_inc;
  283. } else {
  284. url_fseek(pb, size, SEEK_CUR);
  285. }
  286. break;
  287. default:
  288. url_fseek(pb, size, SEEK_CUR);
  289. break;
  290. }
  291. }
  292. return ret;
  293. }
  294. static int fourxm_read_close(AVFormatContext *s)
  295. {
  296. FourxmDemuxContext *fourxm = (FourxmDemuxContext *)s->priv_data;
  297. av_free(fourxm->tracks);
  298. return 0;
  299. }
  300. static AVInputFormat fourxm_iformat = {
  301. "4xm",
  302. "4X Technologies format",
  303. sizeof(FourxmDemuxContext),
  304. fourxm_probe,
  305. fourxm_read_header,
  306. fourxm_read_packet,
  307. fourxm_read_close,
  308. };
  309. int fourxm_init(void)
  310. {
  311. av_register_input_format(&fourxm_iformat);
  312. return 0;
  313. }