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.

294 lines
8.9KB

  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 BE_16(x) ((((uint8_t*)(x))[0] << 8) | ((uint8_t*)(x))[1])
  33. #define BE_32(x) ((((uint8_t*)(x))[0] << 24) | \
  34. (((uint8_t*)(x))[1] << 16) | \
  35. (((uint8_t*)(x))[2] << 8) | \
  36. ((uint8_t*)(x))[3])
  37. #define FOURCC_TAG( ch0, ch1, ch2, ch3 ) \
  38. ( (long)(unsigned char)(ch3) | \
  39. ( (long)(unsigned char)(ch2) << 8 ) | \
  40. ( (long)(unsigned char)(ch1) << 16 ) | \
  41. ( (long)(unsigned char)(ch0) << 24 ) )
  42. #define RIFF_TAG FOURCC_TAG('R', 'I', 'F', 'F')
  43. #define _4XMV_TAG FOURCC_TAG('4', 'X', 'M', 'V')
  44. #define LIST_TAG FOURCC_TAG('L', 'I', 'S', 'T')
  45. #define HEAD_TAG FOURCC_TAG('H', 'E', 'A', 'D')
  46. #define TRK__TAG FOURCC_TAG('T', 'R', 'K', '_')
  47. #define MOVI_TAG FOURCC_TAG('M', 'O', 'V', 'I')
  48. #define VTRK_TAG FOURCC_TAG('V', 'T', 'R', 'K')
  49. #define STRK_TAG FOURCC_TAG('S', 'T', 'R', 'K')
  50. #define name_TAG FOURCC_TAG('n', 'a', 'm', 'e')
  51. #define vtrk_TAG FOURCC_TAG('v', 't', 'r', 'k')
  52. #define strk_TAG FOURCC_TAG('s', 't', 'r', 'k')
  53. #define ifrm_TAG FOURCC_TAG('i', 'f', 'r', 'm')
  54. #define pfrm_TAG FOURCC_TAG('p', 'f', 'r', 'm')
  55. #define cfrm_TAG FOURCC_TAG('c', 'f', 'r', 'm')
  56. #define snd__TAG FOURCC_TAG('s', 'n', 'd', '_')
  57. #define _TAG FOURCC_TAG('', '', '', '')
  58. #define vtrk_SIZE 0x44
  59. #define strk_SIZE 0x28
  60. #define GET_LIST_HEADER() \
  61. fourcc_tag = get_be32(pb); \
  62. size = get_le32(pb); \
  63. if (fourcc_tag != LIST_TAG) \
  64. return AVERROR_INVALIDDATA; \
  65. fourcc_tag = get_be32(pb);
  66. typedef struct AudioTrack {
  67. int sample_rate;
  68. int bits;
  69. int channels;
  70. } AudioTrack;
  71. typedef struct FourxmDemuxContext {
  72. int width;
  73. int height;
  74. int track_count;
  75. AudioTrack *tracks;
  76. int selected_track;
  77. } FourxmDemuxContext;
  78. static int fourxm_probe(AVProbeData *p)
  79. {
  80. if ((BE_32(&p->buf[0]) != RIFF_TAG) ||
  81. (BE_32(&p->buf[8]) != _4XMV_TAG))
  82. return 0;
  83. printf (" detected .4xm file\n");
  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 = BE_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. } else if (fourcc_tag == strk_TAG) {
  128. /* check that there is enough data */
  129. if (size != strk_SIZE) {
  130. av_free(header);
  131. return AVERROR_INVALIDDATA;
  132. }
  133. current_track = LE_32(&header[i + 8]);
  134. if (current_track + 1 > fourxm->track_count) {
  135. fourxm->track_count = current_track + 1;
  136. fourxm->tracks = av_realloc(fourxm->tracks,
  137. fourxm->track_count * sizeof(AudioTrack));
  138. if (!fourxm->tracks) {
  139. av_free(header);
  140. return AVERROR_NOMEM;
  141. }
  142. }
  143. fourxm->tracks[current_track].channels = LE_32(&header[i + 36]);
  144. fourxm->tracks[current_track].sample_rate = LE_32(&header[i + 40]);
  145. fourxm->tracks[current_track].bits = LE_32(&header[i + 44]);
  146. i += 8 + size;
  147. }
  148. }
  149. av_free(header);
  150. /* skip over the LIST-MOVI chunk (which is where the stream should be */
  151. GET_LIST_HEADER();
  152. if (fourcc_tag != MOVI_TAG)
  153. return AVERROR_INVALIDDATA;
  154. if (current_track > -1) {
  155. st = av_new_stream(s, 0);
  156. if (!st)
  157. return AVERROR_NOMEM;
  158. st->codec.codec_type = CODEC_TYPE_AUDIO;
  159. st->codec.codec_tag = 1;
  160. st->codec.channels = fourxm->tracks[current_track].channels;
  161. st->codec.sample_rate = fourxm->tracks[current_track].sample_rate;
  162. st->codec.bits_per_sample = fourxm->tracks[current_track].bits;
  163. st->codec.bit_rate = st->codec.channels * st->codec.sample_rate *
  164. st->codec.bits_per_sample;
  165. st->codec.block_align = st->codec.channels * st->codec.bits_per_sample;
  166. if (st->codec.bits_per_sample == 8)
  167. st->codec.codec_id = CODEC_ID_PCM_U8;
  168. else
  169. st->codec.codec_id = CODEC_ID_PCM_S16LE;
  170. }
  171. return 0;
  172. }
  173. static int fourxm_read_packet(AVFormatContext *s,
  174. AVPacket *pkt)
  175. {
  176. FourxmDemuxContext *fourxm = s->priv_data;
  177. ByteIOContext *pb = &s->pb;
  178. unsigned int fourcc_tag;
  179. unsigned int size;
  180. int ret = 0;
  181. int track_number;
  182. int packet_read = 0;
  183. while (!packet_read) {
  184. fourcc_tag = get_be32(pb);
  185. size = get_le32(pb);
  186. if (fourcc_tag == LIST_TAG) {
  187. /* skip the LIST-FRAM tag and get the next fourcc */
  188. get_be32(pb);
  189. fourcc_tag = get_be32(pb);
  190. size = get_le32(pb);
  191. }
  192. if (url_feof(pb))
  193. return -EIO;
  194. switch (fourcc_tag) {
  195. case ifrm_TAG:
  196. printf (" %cfrm chunk\n", (char)(fourcc_tag >> 24) & 0xFF);
  197. url_fseek(pb, size, SEEK_CUR);
  198. break;
  199. case pfrm_TAG:
  200. printf (" %cfrm chunk\n", (char)(fourcc_tag >> 24) & 0xFF);
  201. url_fseek(pb, size, SEEK_CUR);
  202. break;
  203. case cfrm_TAG:{
  204. int unknown= get_le32(pb);
  205. int id= get_le32(pb);
  206. int whole= get_le32(pb);
  207. static int stats[1000];
  208. stats[id] += size - 12;
  209. printf(" cfrm chunk id:%d size:%d whole:%d until now:%d\n", id, size, whole, stats[id]);
  210. url_fseek(pb, size-12, SEEK_CUR);
  211. break;
  212. }
  213. case snd__TAG:
  214. printf (" snd_ chunk, ");
  215. track_number = get_le32(pb);
  216. size = get_le32(pb);
  217. if (track_number == fourxm->selected_track) {
  218. printf ("correct track, dispatching...\n");
  219. if (av_new_packet(pkt, size))
  220. return -EIO;
  221. ret = get_buffer(&s->pb, pkt->data, size);
  222. if (ret < 0)
  223. av_free_packet(pkt);
  224. packet_read = 1;
  225. } else {
  226. printf ("wrong track, skipping...\n");
  227. url_fseek(pb, size, SEEK_CUR);
  228. }
  229. break;
  230. default:
  231. url_fseek(pb, size, SEEK_CUR);
  232. break;
  233. }
  234. }
  235. return ret;
  236. }
  237. static int fourxm_read_close(AVFormatContext *s)
  238. {
  239. FourxmDemuxContext *fourxm = (FourxmDemuxContext *)s->priv_data;
  240. av_free(fourxm->tracks);
  241. return 0;
  242. }
  243. static AVInputFormat fourxm_iformat = {
  244. "4xm",
  245. "4X Technologies format",
  246. sizeof(FourxmDemuxContext),
  247. fourxm_probe,
  248. fourxm_read_header,
  249. fourxm_read_packet,
  250. fourxm_read_close,
  251. };
  252. int fourxm_init(void)
  253. {
  254. av_register_input_format(&fourxm_iformat);
  255. return 0;
  256. }