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.

247 lines
7.2KB

  1. /*
  2. * Beam Software SIFF demuxer
  3. * Copyright (c) 2007 Konstantin Shishkov
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/channel_layout.h"
  22. #include "libavutil/intreadwrite.h"
  23. #include "avformat.h"
  24. #include "internal.h"
  25. enum SIFFTags {
  26. TAG_SIFF = MKTAG('S', 'I', 'F', 'F'),
  27. TAG_BODY = MKTAG('B', 'O', 'D', 'Y'),
  28. TAG_VBHD = MKTAG('V', 'B', 'H', 'D'),
  29. TAG_SHDR = MKTAG('S', 'H', 'D', 'R'),
  30. TAG_VBV1 = MKTAG('V', 'B', 'V', '1'),
  31. TAG_SOUN = MKTAG('S', 'O', 'U', 'N'),
  32. };
  33. enum VBFlags {
  34. VB_HAS_GMC = 0x01,
  35. VB_HAS_AUDIO = 0x04,
  36. VB_HAS_VIDEO = 0x08,
  37. VB_HAS_PALETTE = 0x10,
  38. VB_HAS_LENGTH = 0x20
  39. };
  40. typedef struct SIFFContext {
  41. int frames;
  42. int cur_frame;
  43. int rate;
  44. int bits;
  45. int block_align;
  46. int has_video;
  47. int has_audio;
  48. int curstrm;
  49. unsigned int pktsize;
  50. int gmcsize;
  51. int sndsize;
  52. unsigned int flags;
  53. uint8_t gmc[4];
  54. } SIFFContext;
  55. static int siff_probe(AVProbeData *p)
  56. {
  57. uint32_t tag = AV_RL32(p->buf + 8);
  58. /* check file header */
  59. if (AV_RL32(p->buf) != TAG_SIFF ||
  60. (tag != TAG_VBV1 && tag != TAG_SOUN))
  61. return 0;
  62. return AVPROBE_SCORE_MAX;
  63. }
  64. static int create_audio_stream(AVFormatContext *s, SIFFContext *c)
  65. {
  66. AVStream *ast;
  67. ast = avformat_new_stream(s, NULL);
  68. if (!ast)
  69. return AVERROR(ENOMEM);
  70. ast->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  71. ast->codecpar->codec_id = AV_CODEC_ID_PCM_U8;
  72. ast->codecpar->channels = 1;
  73. ast->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
  74. ast->codecpar->bits_per_coded_sample = 8;
  75. ast->codecpar->sample_rate = c->rate;
  76. avpriv_set_pts_info(ast, 16, 1, c->rate);
  77. ast->start_time = 0;
  78. return 0;
  79. }
  80. static int siff_parse_vbv1(AVFormatContext *s, SIFFContext *c, AVIOContext *pb)
  81. {
  82. AVStream *st;
  83. int width, height;
  84. if (avio_rl32(pb) != TAG_VBHD) {
  85. av_log(s, AV_LOG_ERROR, "Header chunk is missing\n");
  86. return AVERROR_INVALIDDATA;
  87. }
  88. if (avio_rb32(pb) != 32) {
  89. av_log(s, AV_LOG_ERROR, "Header chunk size is incorrect\n");
  90. return AVERROR_INVALIDDATA;
  91. }
  92. if (avio_rl16(pb) != 1) {
  93. av_log(s, AV_LOG_ERROR, "Incorrect header version\n");
  94. return AVERROR_INVALIDDATA;
  95. }
  96. width = avio_rl16(pb);
  97. height = avio_rl16(pb);
  98. avio_skip(pb, 4);
  99. c->frames = avio_rl16(pb);
  100. if (!c->frames) {
  101. av_log(s, AV_LOG_ERROR, "File contains no frames ???\n");
  102. return AVERROR_INVALIDDATA;
  103. }
  104. c->bits = avio_rl16(pb);
  105. c->rate = avio_rl16(pb);
  106. c->block_align = c->rate * (c->bits >> 3);
  107. avio_skip(pb, 16); // zeroes
  108. st = avformat_new_stream(s, NULL);
  109. if (!st)
  110. return AVERROR(ENOMEM);
  111. st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  112. st->codecpar->codec_id = AV_CODEC_ID_VB;
  113. st->codecpar->codec_tag = MKTAG('V', 'B', 'V', '1');
  114. st->codecpar->width = width;
  115. st->codecpar->height = height;
  116. st->codecpar->format = AV_PIX_FMT_PAL8;
  117. avpriv_set_pts_info(st, 16, 1, 12);
  118. c->cur_frame = 0;
  119. c->has_video = 1;
  120. c->has_audio = !!c->rate;
  121. c->curstrm = -1;
  122. if (c->has_audio)
  123. return create_audio_stream(s, c);
  124. return 0;
  125. }
  126. static int siff_parse_soun(AVFormatContext *s, SIFFContext *c, AVIOContext *pb)
  127. {
  128. if (avio_rl32(pb) != TAG_SHDR) {
  129. av_log(s, AV_LOG_ERROR, "Header chunk is missing\n");
  130. return AVERROR_INVALIDDATA;
  131. }
  132. if (avio_rb32(pb) != 8) {
  133. av_log(s, AV_LOG_ERROR, "Header chunk size is incorrect\n");
  134. return AVERROR_INVALIDDATA;
  135. }
  136. avio_skip(pb, 4); // unknown value
  137. c->rate = avio_rl16(pb);
  138. c->bits = avio_rl16(pb);
  139. c->block_align = c->rate * (c->bits >> 3);
  140. return create_audio_stream(s, c);
  141. }
  142. static int siff_read_header(AVFormatContext *s)
  143. {
  144. AVIOContext *pb = s->pb;
  145. SIFFContext *c = s->priv_data;
  146. uint32_t tag;
  147. int ret;
  148. if (avio_rl32(pb) != TAG_SIFF)
  149. return AVERROR_INVALIDDATA;
  150. avio_skip(pb, 4); // ignore size
  151. tag = avio_rl32(pb);
  152. if (tag != TAG_VBV1 && tag != TAG_SOUN) {
  153. av_log(s, AV_LOG_ERROR, "Not a VBV file\n");
  154. return AVERROR_INVALIDDATA;
  155. }
  156. if (tag == TAG_VBV1 && (ret = siff_parse_vbv1(s, c, pb)) < 0)
  157. return ret;
  158. if (tag == TAG_SOUN && (ret = siff_parse_soun(s, c, pb)) < 0)
  159. return ret;
  160. if (avio_rl32(pb) != MKTAG('B', 'O', 'D', 'Y')) {
  161. av_log(s, AV_LOG_ERROR, "'BODY' chunk is missing\n");
  162. return AVERROR_INVALIDDATA;
  163. }
  164. avio_skip(pb, 4); // ignore size
  165. return 0;
  166. }
  167. static int siff_read_packet(AVFormatContext *s, AVPacket *pkt)
  168. {
  169. SIFFContext *c = s->priv_data;
  170. if (c->has_video) {
  171. unsigned int size;
  172. if (c->cur_frame >= c->frames)
  173. return AVERROR(EIO);
  174. if (c->curstrm == -1) {
  175. c->pktsize = avio_rl32(s->pb) - 4;
  176. c->flags = avio_rl16(s->pb);
  177. c->gmcsize = (c->flags & VB_HAS_GMC) ? 4 : 0;
  178. if (c->gmcsize)
  179. avio_read(s->pb, c->gmc, c->gmcsize);
  180. c->sndsize = (c->flags & VB_HAS_AUDIO) ? avio_rl32(s->pb) : 0;
  181. c->curstrm = !!(c->flags & VB_HAS_AUDIO);
  182. }
  183. if (!c->curstrm) {
  184. size = c->pktsize - c->sndsize;
  185. if (av_new_packet(pkt, size) < 0)
  186. return AVERROR(ENOMEM);
  187. AV_WL16(pkt->data, c->flags);
  188. if (c->gmcsize)
  189. memcpy(pkt->data + 2, c->gmc, c->gmcsize);
  190. avio_read(s->pb, pkt->data + 2 + c->gmcsize, size - c->gmcsize - 2);
  191. pkt->stream_index = 0;
  192. c->curstrm = -1;
  193. } else {
  194. int pktsize = av_get_packet(s->pb, pkt, c->sndsize - 4);
  195. if (pktsize < 0)
  196. return AVERROR(EIO);
  197. pkt->stream_index = 1;
  198. pkt->duration = pktsize;
  199. c->curstrm = 0;
  200. }
  201. if (!c->cur_frame || c->curstrm)
  202. pkt->flags |= AV_PKT_FLAG_KEY;
  203. if (c->curstrm == -1)
  204. c->cur_frame++;
  205. } else {
  206. int pktsize = av_get_packet(s->pb, pkt, c->block_align);
  207. if (pktsize <= 0)
  208. return AVERROR(EIO);
  209. pkt->duration = pktsize;
  210. }
  211. return pkt->size;
  212. }
  213. AVInputFormat ff_siff_demuxer = {
  214. .name = "siff",
  215. .long_name = NULL_IF_CONFIG_SMALL("Beam Software SIFF"),
  216. .priv_data_size = sizeof(SIFFContext),
  217. .read_probe = siff_probe,
  218. .read_header = siff_read_header,
  219. .read_packet = siff_read_packet,
  220. .extensions = "vb,son",
  221. };