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.

244 lines
6.8KB

  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. int pktsize;
  50. int gmcsize;
  51. int sndsize;
  52. 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 -1;
  70. ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  71. ast->codec->codec_id = AV_CODEC_ID_PCM_U8;
  72. ast->codec->channels = 1;
  73. ast->codec->channel_layout = AV_CH_LAYOUT_MONO;
  74. ast->codec->bits_per_coded_sample = 8;
  75. ast->codec->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 -1;
  87. }
  88. if(avio_rb32(pb) != 32){
  89. av_log(s, AV_LOG_ERROR, "Header chunk size is incorrect\n");
  90. return -1;
  91. }
  92. if(avio_rl16(pb) != 1){
  93. av_log(s, AV_LOG_ERROR, "Incorrect header version\n");
  94. return -1;
  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 -1;
  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 -1;
  111. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  112. st->codec->codec_id = AV_CODEC_ID_VB;
  113. st->codec->codec_tag = MKTAG('V', 'B', 'V', '1');
  114. st->codec->width = width;
  115. st->codec->height = height;
  116. st->codec->pix_fmt = 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 && create_audio_stream(s, c) < 0)
  123. return -1;
  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 -1;
  131. }
  132. if(avio_rb32(pb) != 8){
  133. av_log(s, AV_LOG_ERROR, "Header chunk size is incorrect\n");
  134. return -1;
  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. if (avio_rl32(pb) != TAG_SIFF)
  148. return -1;
  149. avio_skip(pb, 4); //ignore size
  150. tag = avio_rl32(pb);
  151. if (tag != TAG_VBV1 && tag != TAG_SOUN){
  152. av_log(s, AV_LOG_ERROR, "Not a VBV file\n");
  153. return -1;
  154. }
  155. if (tag == TAG_VBV1 && siff_parse_vbv1(s, c, pb) < 0)
  156. return -1;
  157. if (tag == TAG_SOUN && siff_parse_soun(s, c, pb) < 0)
  158. return -1;
  159. if (avio_rl32(pb) != MKTAG('B', 'O', 'D', 'Y')){
  160. av_log(s, AV_LOG_ERROR, "'BODY' chunk is missing\n");
  161. return -1;
  162. }
  163. avio_skip(pb, 4); //ignore size
  164. return 0;
  165. }
  166. static int siff_read_packet(AVFormatContext *s, AVPacket *pkt)
  167. {
  168. SIFFContext *c = s->priv_data;
  169. int size;
  170. if (c->has_video){
  171. if (c->cur_frame >= c->frames)
  172. return AVERROR(EIO);
  173. if (c->curstrm == -1){
  174. c->pktsize = avio_rl32(s->pb) - 4;
  175. c->flags = avio_rl16(s->pb);
  176. c->gmcsize = (c->flags & VB_HAS_GMC) ? 4 : 0;
  177. if (c->gmcsize)
  178. avio_read(s->pb, c->gmc, c->gmcsize);
  179. c->sndsize = (c->flags & VB_HAS_AUDIO) ? avio_rl32(s->pb): 0;
  180. c->curstrm = !!(c->flags & VB_HAS_AUDIO);
  181. }
  182. if (!c->curstrm){
  183. size = c->pktsize - c->sndsize;
  184. if (av_new_packet(pkt, size) < 0)
  185. return AVERROR(ENOMEM);
  186. AV_WL16(pkt->data, c->flags);
  187. if (c->gmcsize)
  188. memcpy(pkt->data + 2, c->gmc, c->gmcsize);
  189. avio_read(s->pb, pkt->data + 2 + c->gmcsize, size - c->gmcsize - 2);
  190. pkt->stream_index = 0;
  191. c->curstrm = -1;
  192. }else{
  193. if ((size = av_get_packet(s->pb, pkt, c->sndsize - 4)) < 0)
  194. return AVERROR(EIO);
  195. pkt->stream_index = 1;
  196. pkt->duration = size;
  197. c->curstrm = 0;
  198. }
  199. if(!c->cur_frame || c->curstrm)
  200. pkt->flags |= AV_PKT_FLAG_KEY;
  201. if (c->curstrm == -1)
  202. c->cur_frame++;
  203. }else{
  204. size = av_get_packet(s->pb, pkt, c->block_align);
  205. if(size <= 0)
  206. return AVERROR(EIO);
  207. pkt->duration = size;
  208. }
  209. return pkt->size;
  210. }
  211. AVInputFormat ff_siff_demuxer = {
  212. .name = "siff",
  213. .long_name = NULL_IF_CONFIG_SMALL("Beam Software SIFF"),
  214. .priv_data_size = sizeof(SIFFContext),
  215. .read_probe = siff_probe,
  216. .read_header = siff_read_header,
  217. .read_packet = siff_read_packet,
  218. .extensions = "vb,son",
  219. };