PageRenderTime 47ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/jni/libffmpeg/libavformat/iff.c

https://bitbucket.org/starup/android-rdp
C | 325 lines | 249 code | 45 blank | 31 comment | 37 complexity | 41b67c8e8e3099cc662909d3299209dc MD5 | raw file
  1. /*
  2. * IFF (.iff) file demuxer
  3. * Copyright (c) 2008 Jaikrishnan Menon <realityman@gmx.net>
  4. * Copyright (c) 2010 Peter Ross <pross@xvid.org>
  5. * Copyright (c) 2010 Sebastian Vater <cdgs.basty@googlemail.com>
  6. *
  7. * This file is part of FFmpeg.
  8. *
  9. * FFmpeg is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with FFmpeg; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. /**
  24. * @file
  25. * IFF file demuxer
  26. * by Jaikrishnan Menon
  27. * for more information on the .iff file format, visit:
  28. * http://wiki.multimedia.cx/index.php?title=IFF
  29. */
  30. #include "libavutil/intreadwrite.h"
  31. #include "libavcodec/iff.h"
  32. #include "avformat.h"
  33. #define ID_8SVX MKTAG('8','S','V','X')
  34. #define ID_VHDR MKTAG('V','H','D','R')
  35. #define ID_ATAK MKTAG('A','T','A','K')
  36. #define ID_RLSE MKTAG('R','L','S','E')
  37. #define ID_CHAN MKTAG('C','H','A','N')
  38. #define ID_PBM MKTAG('P','B','M',' ')
  39. #define ID_ILBM MKTAG('I','L','B','M')
  40. #define ID_BMHD MKTAG('B','M','H','D')
  41. #define ID_CMAP MKTAG('C','M','A','P')
  42. #define ID_FORM MKTAG('F','O','R','M')
  43. #define ID_ANNO MKTAG('A','N','N','O')
  44. #define ID_AUTH MKTAG('A','U','T','H')
  45. #define ID_CHRS MKTAG('C','H','R','S')
  46. #define ID_COPYRIGHT MKTAG('(','c',')',' ')
  47. #define ID_CSET MKTAG('C','S','E','T')
  48. #define ID_FVER MKTAG('F','V','E','R')
  49. #define ID_NAME MKTAG('N','A','M','E')
  50. #define ID_TEXT MKTAG('T','E','X','T')
  51. #define ID_BODY MKTAG('B','O','D','Y')
  52. #define ID_ANNO MKTAG('A','N','N','O')
  53. #define LEFT 2
  54. #define RIGHT 4
  55. #define STEREO 6
  56. #define PACKET_SIZE 1024
  57. typedef enum {
  58. COMP_NONE,
  59. COMP_FIB,
  60. COMP_EXP
  61. } svx8_compression_type;
  62. typedef enum {
  63. BITMAP_RAW,
  64. BITMAP_BYTERUN1
  65. } bitmap_compression_type;
  66. typedef struct {
  67. uint64_t body_pos;
  68. uint32_t body_size;
  69. uint32_t sent_bytes;
  70. uint32_t audio_frame_count;
  71. } IffDemuxContext;
  72. static void interleave_stereo(const uint8_t *src, uint8_t *dest, int size)
  73. {
  74. uint8_t *end = dest + size;
  75. size = size>>1;
  76. while(dest < end) {
  77. *dest++ = *src;
  78. *dest++ = *(src+size);
  79. src++;
  80. }
  81. }
  82. /* Metadata string read */
  83. static int get_metadata(AVFormatContext *s,
  84. const char *const tag,
  85. const unsigned data_size)
  86. {
  87. uint8_t *buf = ((data_size + 1) == 0) ? NULL : av_malloc(data_size + 1);
  88. if (!buf)
  89. return AVERROR(ENOMEM);
  90. if (get_buffer(s->pb, buf, data_size) < 0) {
  91. av_free(buf);
  92. return AVERROR(EIO);
  93. }
  94. buf[data_size] = 0;
  95. av_metadata_set2(&s->metadata, tag, buf, AV_METADATA_DONT_STRDUP_VAL);
  96. return 0;
  97. }
  98. static int iff_probe(AVProbeData *p)
  99. {
  100. const uint8_t *d = p->buf;
  101. if ( AV_RL32(d) == ID_FORM &&
  102. (AV_RL32(d+8) == ID_8SVX || AV_RL32(d+8) == ID_PBM || AV_RL32(d+8) == ID_ILBM) )
  103. return AVPROBE_SCORE_MAX;
  104. return 0;
  105. }
  106. static int iff_read_header(AVFormatContext *s,
  107. AVFormatParameters *ap)
  108. {
  109. IffDemuxContext *iff = s->priv_data;
  110. ByteIOContext *pb = s->pb;
  111. AVStream *st;
  112. uint32_t chunk_id, data_size;
  113. int compression = -1;
  114. st = av_new_stream(s, 0);
  115. if (!st)
  116. return AVERROR(ENOMEM);
  117. st->codec->channels = 1;
  118. url_fskip(pb, 8);
  119. // codec_tag used by ByteRun1 decoder to distinguish progressive (PBM) and interlaced (ILBM) content
  120. st->codec->codec_tag = get_le32(pb);
  121. while(!url_feof(pb)) {
  122. uint64_t orig_pos;
  123. int res;
  124. const char *metadata_tag = NULL;
  125. chunk_id = get_le32(pb);
  126. data_size = get_be32(pb);
  127. orig_pos = url_ftell(pb);
  128. switch(chunk_id) {
  129. case ID_VHDR:
  130. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  131. if (data_size < 14)
  132. return AVERROR_INVALIDDATA;
  133. url_fskip(pb, 12);
  134. st->codec->sample_rate = get_be16(pb);
  135. if (data_size >= 16) {
  136. url_fskip(pb, 1);
  137. compression = get_byte(pb);
  138. }
  139. break;
  140. case ID_BODY:
  141. iff->body_pos = url_ftell(pb);
  142. iff->body_size = data_size;
  143. break;
  144. case ID_CHAN:
  145. if (data_size < 4)
  146. return AVERROR_INVALIDDATA;
  147. st->codec->channels = (get_be32(pb) < 6) ? 1 : 2;
  148. break;
  149. case ID_CMAP:
  150. st->codec->extradata_size = data_size;
  151. st->codec->extradata = av_malloc(data_size);
  152. if (!st->codec->extradata)
  153. return AVERROR(ENOMEM);
  154. if (get_buffer(pb, st->codec->extradata, data_size) < 0)
  155. return AVERROR(EIO);
  156. break;
  157. case ID_BMHD:
  158. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  159. if (data_size <= 8)
  160. return AVERROR_INVALIDDATA;
  161. st->codec->width = get_be16(pb);
  162. st->codec->height = get_be16(pb);
  163. url_fskip(pb, 4); // x, y offset
  164. st->codec->bits_per_coded_sample = get_byte(pb);
  165. if (data_size >= 11) {
  166. url_fskip(pb, 1); // masking
  167. compression = get_byte(pb);
  168. }
  169. if (data_size >= 16) {
  170. url_fskip(pb, 3); // paddding, transparent
  171. st->sample_aspect_ratio.num = get_byte(pb);
  172. st->sample_aspect_ratio.den = get_byte(pb);
  173. }
  174. break;
  175. case ID_ANNO:
  176. case ID_TEXT:
  177. metadata_tag = "comment";
  178. break;
  179. case ID_AUTH:
  180. metadata_tag = "artist";
  181. break;
  182. case ID_COPYRIGHT:
  183. metadata_tag = "copyright";
  184. break;
  185. case ID_NAME:
  186. metadata_tag = "title";
  187. break;
  188. }
  189. if (metadata_tag) {
  190. if ((res = get_metadata(s, metadata_tag, data_size)) < 0) {
  191. av_log(s, AV_LOG_ERROR, "iff: cannot allocate metadata tag %s!", metadata_tag);
  192. return res;
  193. }
  194. }
  195. url_fskip(pb, data_size - (url_ftell(pb) - orig_pos) + (data_size & 1));
  196. }
  197. url_fseek(pb, iff->body_pos, SEEK_SET);
  198. switch(st->codec->codec_type) {
  199. case AVMEDIA_TYPE_AUDIO:
  200. av_set_pts_info(st, 32, 1, st->codec->sample_rate);
  201. switch(compression) {
  202. case COMP_NONE:
  203. st->codec->codec_id = CODEC_ID_PCM_S8;
  204. break;
  205. case COMP_FIB:
  206. st->codec->codec_id = CODEC_ID_8SVX_FIB;
  207. break;
  208. case COMP_EXP:
  209. st->codec->codec_id = CODEC_ID_8SVX_EXP;
  210. break;
  211. default:
  212. av_log(s, AV_LOG_ERROR, "iff: unknown compression method\n");
  213. return -1;
  214. }
  215. st->codec->bits_per_coded_sample = 8;
  216. st->codec->bit_rate = st->codec->channels * st->codec->sample_rate * st->codec->bits_per_coded_sample;
  217. st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
  218. break;
  219. case AVMEDIA_TYPE_VIDEO:
  220. switch (compression) {
  221. case BITMAP_RAW:
  222. st->codec->codec_id = CODEC_ID_IFF_ILBM;
  223. break;
  224. case BITMAP_BYTERUN1:
  225. st->codec->codec_id = CODEC_ID_IFF_BYTERUN1;
  226. break;
  227. default:
  228. av_log(s, AV_LOG_ERROR, "unknown compression method\n");
  229. return AVERROR_INVALIDDATA;
  230. }
  231. break;
  232. default:
  233. return -1;
  234. }
  235. return 0;
  236. }
  237. static int iff_read_packet(AVFormatContext *s,
  238. AVPacket *pkt)
  239. {
  240. IffDemuxContext *iff = s->priv_data;
  241. ByteIOContext *pb = s->pb;
  242. AVStream *st = s->streams[0];
  243. int ret;
  244. if(iff->sent_bytes >= iff->body_size)
  245. return AVERROR(EIO);
  246. if(st->codec->channels == 2) {
  247. uint8_t sample_buffer[PACKET_SIZE];
  248. ret = get_buffer(pb, sample_buffer, PACKET_SIZE);
  249. if(av_new_packet(pkt, PACKET_SIZE) < 0) {
  250. av_log(s, AV_LOG_ERROR, "iff: cannot allocate packet \n");
  251. return AVERROR(ENOMEM);
  252. }
  253. interleave_stereo(sample_buffer, pkt->data, PACKET_SIZE);
  254. } else if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  255. ret = av_get_packet(pb, pkt, iff->body_size);
  256. } else {
  257. ret = av_get_packet(pb, pkt, PACKET_SIZE);
  258. }
  259. if(iff->sent_bytes == 0)
  260. pkt->flags |= AV_PKT_FLAG_KEY;
  261. if(st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
  262. iff->sent_bytes += PACKET_SIZE;
  263. } else {
  264. iff->sent_bytes = iff->body_size;
  265. }
  266. pkt->stream_index = 0;
  267. if(st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
  268. pkt->pts = iff->audio_frame_count;
  269. iff->audio_frame_count += ret / st->codec->channels;
  270. }
  271. return ret;
  272. }
  273. AVInputFormat iff_demuxer = {
  274. "IFF",
  275. NULL_IF_CONFIG_SMALL("IFF format"),
  276. sizeof(IffDemuxContext),
  277. iff_probe,
  278. iff_read_header,
  279. iff_read_packet,
  280. };