4xm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * 4X Technologies .4xm File Demuxer (no muxer)
  3. * Copyright (c) 2003 The ffmpeg Project
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * 4X Technologies file demuxer
  24. * by Mike Melanson (melanson@pcisys.net)
  25. * for more information on the .4xm file format, visit:
  26. * http://www.pcisys.net/~melanson/codecs/
  27. */
  28. #include "libavutil/intreadwrite.h"
  29. #include "libavutil/intfloat_readwrite.h"
  30. #include "avformat.h"
  31. #define RIFF_TAG MKTAG('R', 'I', 'F', 'F')
  32. #define FOURXMV_TAG MKTAG('4', 'X', 'M', 'V')
  33. #define LIST_TAG MKTAG('L', 'I', 'S', 'T')
  34. #define HEAD_TAG MKTAG('H', 'E', 'A', 'D')
  35. #define TRK__TAG MKTAG('T', 'R', 'K', '_')
  36. #define MOVI_TAG MKTAG('M', 'O', 'V', 'I')
  37. #define VTRK_TAG MKTAG('V', 'T', 'R', 'K')
  38. #define STRK_TAG MKTAG('S', 'T', 'R', 'K')
  39. #define std__TAG MKTAG('s', 't', 'd', '_')
  40. #define name_TAG MKTAG('n', 'a', 'm', 'e')
  41. #define vtrk_TAG MKTAG('v', 't', 'r', 'k')
  42. #define strk_TAG MKTAG('s', 't', 'r', 'k')
  43. #define ifrm_TAG MKTAG('i', 'f', 'r', 'm')
  44. #define pfrm_TAG MKTAG('p', 'f', 'r', 'm')
  45. #define cfrm_TAG MKTAG('c', 'f', 'r', 'm')
  46. #define ifr2_TAG MKTAG('i', 'f', 'r', '2')
  47. #define pfr2_TAG MKTAG('p', 'f', 'r', '2')
  48. #define cfr2_TAG MKTAG('c', 'f', 'r', '2')
  49. #define snd__TAG MKTAG('s', 'n', 'd', '_')
  50. #define vtrk_SIZE 0x44
  51. #define strk_SIZE 0x28
  52. #define GET_LIST_HEADER() \
  53. fourcc_tag = avio_rl32(pb); \
  54. size = avio_rl32(pb); \
  55. if (fourcc_tag != LIST_TAG) \
  56. return AVERROR_INVALIDDATA; \
  57. fourcc_tag = avio_rl32(pb);
  58. typedef struct AudioTrack {
  59. int sample_rate;
  60. int bits;
  61. int channels;
  62. int stream_index;
  63. int adpcm;
  64. int64_t audio_pts;
  65. } AudioTrack;
  66. typedef struct FourxmDemuxContext {
  67. int width;
  68. int height;
  69. int video_stream_index;
  70. int track_count;
  71. AudioTrack *tracks;
  72. int64_t video_pts;
  73. float fps;
  74. } FourxmDemuxContext;
  75. static int fourxm_probe(AVProbeData *p)
  76. {
  77. if ((AV_RL32(&p->buf[0]) != RIFF_TAG) ||
  78. (AV_RL32(&p->buf[8]) != FOURXMV_TAG))
  79. return 0;
  80. return AVPROBE_SCORE_MAX;
  81. }
  82. static int fourxm_read_header(AVFormatContext *s,
  83. AVFormatParameters *ap)
  84. {
  85. AVIOContext *pb = s->pb;
  86. unsigned int fourcc_tag;
  87. unsigned int size;
  88. int header_size;
  89. FourxmDemuxContext *fourxm = s->priv_data;
  90. unsigned char *header;
  91. int i, ret;
  92. AVStream *st;
  93. fourxm->track_count = 0;
  94. fourxm->tracks = NULL;
  95. fourxm->fps = 1.0;
  96. /* skip the first 3 32-bit numbers */
  97. avio_skip(pb, 12);
  98. /* check for LIST-HEAD */
  99. GET_LIST_HEADER();
  100. header_size = size - 4;
  101. if (fourcc_tag != HEAD_TAG || header_size < 0)
  102. return AVERROR_INVALIDDATA;
  103. /* allocate space for the header and load the whole thing */
  104. header = av_malloc(header_size);
  105. if (!header)
  106. return AVERROR(ENOMEM);
  107. if (avio_read(pb, header, header_size) != header_size){
  108. av_free(header);
  109. return AVERROR(EIO);
  110. }
  111. /* take the lazy approach and search for any and all vtrk and strk chunks */
  112. for (i = 0; i < header_size - 8; i++) {
  113. fourcc_tag = AV_RL32(&header[i]);
  114. size = AV_RL32(&header[i + 4]);
  115. if (fourcc_tag == std__TAG) {
  116. fourxm->fps = av_int2flt(AV_RL32(&header[i + 12]));
  117. } else if (fourcc_tag == vtrk_TAG) {
  118. /* check that there is enough data */
  119. if (size != vtrk_SIZE) {
  120. ret= AVERROR_INVALIDDATA;
  121. goto fail;
  122. }
  123. fourxm->width = AV_RL32(&header[i + 36]);
  124. fourxm->height = AV_RL32(&header[i + 40]);
  125. /* allocate a new AVStream */
  126. st = av_new_stream(s, 0);
  127. if (!st){
  128. ret= AVERROR(ENOMEM);
  129. goto fail;
  130. }
  131. av_set_pts_info(st, 60, 1, fourxm->fps);
  132. fourxm->video_stream_index = st->index;
  133. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  134. st->codec->codec_id = CODEC_ID_4XM;
  135. st->codec->extradata_size = 4;
  136. st->codec->extradata = av_malloc(4);
  137. AV_WL32(st->codec->extradata, AV_RL32(&header[i + 16]));
  138. st->codec->width = fourxm->width;
  139. st->codec->height = fourxm->height;
  140. i += 8 + size;
  141. } else if (fourcc_tag == strk_TAG) {
  142. int current_track;
  143. /* check that there is enough data */
  144. if (size != strk_SIZE) {
  145. ret= AVERROR_INVALIDDATA;
  146. goto fail;
  147. }
  148. current_track = AV_RL32(&header[i + 8]);
  149. if((unsigned)current_track >= UINT_MAX / sizeof(AudioTrack) - 1){
  150. av_log(s, AV_LOG_ERROR, "current_track too large\n");
  151. ret= -1;
  152. goto fail;
  153. }
  154. if (current_track + 1 > fourxm->track_count) {
  155. fourxm->tracks = av_realloc_f(fourxm->tracks,
  156. sizeof(AudioTrack),
  157. current_track + 1);
  158. if (!fourxm->tracks) {
  159. ret= AVERROR(ENOMEM);
  160. goto fail;
  161. }
  162. memset(&fourxm->tracks[fourxm->track_count], 0,
  163. sizeof(AudioTrack) * (current_track + 1 - fourxm->track_count));
  164. fourxm->track_count = current_track + 1;
  165. }
  166. fourxm->tracks[current_track].adpcm = AV_RL32(&header[i + 12]);
  167. fourxm->tracks[current_track].channels = AV_RL32(&header[i + 36]);
  168. fourxm->tracks[current_track].sample_rate = AV_RL32(&header[i + 40]);
  169. fourxm->tracks[current_track].bits = AV_RL32(&header[i + 44]);
  170. fourxm->tracks[current_track].audio_pts = 0;
  171. if( fourxm->tracks[current_track].channels <= 0
  172. || fourxm->tracks[current_track].sample_rate <= 0
  173. || fourxm->tracks[current_track].bits < 0){
  174. av_log(s, AV_LOG_ERROR, "audio header invalid\n");
  175. ret= -1;
  176. goto fail;
  177. }
  178. i += 8 + size;
  179. /* allocate a new AVStream */
  180. st = av_new_stream(s, current_track);
  181. if (!st){
  182. ret= AVERROR(ENOMEM);
  183. goto fail;
  184. }
  185. av_set_pts_info(st, 60, 1, fourxm->tracks[current_track].sample_rate);
  186. fourxm->tracks[current_track].stream_index = st->index;
  187. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  188. st->codec->codec_tag = 0;
  189. st->codec->channels = fourxm->tracks[current_track].channels;
  190. st->codec->sample_rate = fourxm->tracks[current_track].sample_rate;
  191. st->codec->bits_per_coded_sample = fourxm->tracks[current_track].bits;
  192. st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
  193. st->codec->bits_per_coded_sample;
  194. st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
  195. if (fourxm->tracks[current_track].adpcm){
  196. st->codec->codec_id = CODEC_ID_ADPCM_4XM;
  197. }else if (st->codec->bits_per_coded_sample == 8){
  198. st->codec->codec_id = CODEC_ID_PCM_U8;
  199. }else
  200. st->codec->codec_id = CODEC_ID_PCM_S16LE;
  201. }
  202. }
  203. /* skip over the LIST-MOVI chunk (which is where the stream should be */
  204. GET_LIST_HEADER();
  205. if (fourcc_tag != MOVI_TAG){
  206. ret= AVERROR_INVALIDDATA;
  207. goto fail;
  208. }
  209. av_free(header);
  210. /* initialize context members */
  211. fourxm->video_pts = -1; /* first frame will push to 0 */
  212. return 0;
  213. fail:
  214. av_freep(&fourxm->tracks);
  215. av_free(header);
  216. return ret;
  217. }
  218. static int fourxm_read_packet(AVFormatContext *s,
  219. AVPacket *pkt)
  220. {
  221. FourxmDemuxContext *fourxm = s->priv_data;
  222. AVIOContext *pb = s->pb;
  223. unsigned int fourcc_tag;
  224. unsigned int size;
  225. int ret = 0;
  226. unsigned int track_number;
  227. int packet_read = 0;
  228. unsigned char header[8];
  229. int audio_frame_count;
  230. while (!packet_read) {
  231. if ((ret = avio_read(s->pb, header, 8)) < 0)
  232. return ret;
  233. fourcc_tag = AV_RL32(&header[0]);
  234. size = AV_RL32(&header[4]);
  235. if (url_feof(pb))
  236. return AVERROR(EIO);
  237. switch (fourcc_tag) {
  238. case LIST_TAG:
  239. /* this is a good time to bump the video pts */
  240. fourxm->video_pts ++;
  241. /* skip the LIST-* tag and move on to the next fourcc */
  242. avio_rl32(pb);
  243. break;
  244. case ifrm_TAG:
  245. case pfrm_TAG:
  246. case cfrm_TAG:
  247. case ifr2_TAG:
  248. case pfr2_TAG:
  249. case cfr2_TAG:
  250. /* allocate 8 more bytes than 'size' to account for fourcc
  251. * and size */
  252. if (size + 8 < size || av_new_packet(pkt, size + 8))
  253. return AVERROR(EIO);
  254. pkt->stream_index = fourxm->video_stream_index;
  255. pkt->pts = fourxm->video_pts;
  256. pkt->pos = avio_tell(s->pb);
  257. memcpy(pkt->data, header, 8);
  258. ret = avio_read(s->pb, &pkt->data[8], size);
  259. if (ret < 0){
  260. av_free_packet(pkt);
  261. }else
  262. packet_read = 1;
  263. break;
  264. case snd__TAG:
  265. track_number = avio_rl32(pb);
  266. avio_skip(pb, 4);
  267. size-=8;
  268. if (track_number < fourxm->track_count && fourxm->tracks[track_number].channels>0) {
  269. ret= av_get_packet(s->pb, pkt, size);
  270. if(ret<0)
  271. return AVERROR(EIO);
  272. pkt->stream_index =
  273. fourxm->tracks[track_number].stream_index;
  274. pkt->pts = fourxm->tracks[track_number].audio_pts;
  275. packet_read = 1;
  276. /* pts accounting */
  277. audio_frame_count = size;
  278. if (fourxm->tracks[track_number].adpcm)
  279. audio_frame_count -=
  280. 2 * (fourxm->tracks[track_number].channels);
  281. audio_frame_count /=
  282. fourxm->tracks[track_number].channels;
  283. if (fourxm->tracks[track_number].adpcm){
  284. audio_frame_count *= 2;
  285. }else
  286. audio_frame_count /=
  287. (fourxm->tracks[track_number].bits / 8);
  288. fourxm->tracks[track_number].audio_pts += audio_frame_count;
  289. } else {
  290. avio_skip(pb, size);
  291. }
  292. break;
  293. default:
  294. avio_skip(pb, size);
  295. break;
  296. }
  297. }
  298. return ret;
  299. }
  300. static int fourxm_read_close(AVFormatContext *s)
  301. {
  302. FourxmDemuxContext *fourxm = s->priv_data;
  303. av_freep(&fourxm->tracks);
  304. return 0;
  305. }
  306. AVInputFormat ff_fourxm_demuxer = {
  307. .name = "4xm",
  308. .long_name = NULL_IF_CONFIG_SMALL("4X Technologies format"),
  309. .priv_data_size = sizeof(FourxmDemuxContext),
  310. .read_probe = fourxm_probe,
  311. .read_header = fourxm_read_header,
  312. .read_packet = fourxm_read_packet,
  313. .read_close = fourxm_read_close,
  314. };