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.

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