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.

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