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.

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