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.

256 lines
7.4KB

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