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.

248 lines
7.0KB

  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 -1;
  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 -1;
  88. }
  89. if(avio_rb32(pb) != 32){
  90. av_log(s, AV_LOG_ERROR, "Header chunk size is incorrect\n");
  91. return -1;
  92. }
  93. if(avio_rl16(pb) != 1){
  94. av_log(s, AV_LOG_ERROR, "Incorrect header version\n");
  95. return -1;
  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 -1;
  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 -1;
  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. avpriv_set_pts_info(st, 16, 1, 12);
  119. c->cur_frame = 0;
  120. c->has_video = 1;
  121. c->has_audio = !!c->rate;
  122. c->curstrm = -1;
  123. if (c->has_audio && create_audio_stream(s, c) < 0)
  124. return -1;
  125. return 0;
  126. }
  127. static int siff_parse_soun(AVFormatContext *s, SIFFContext *c, AVIOContext *pb)
  128. {
  129. if (avio_rl32(pb) != TAG_SHDR){
  130. av_log(s, AV_LOG_ERROR, "Header chunk is missing\n");
  131. return -1;
  132. }
  133. if(avio_rb32(pb) != 8){
  134. av_log(s, AV_LOG_ERROR, "Header chunk size is incorrect\n");
  135. return -1;
  136. }
  137. avio_skip(pb, 4); //unknown value
  138. c->rate = avio_rl16(pb);
  139. c->bits = avio_rl16(pb);
  140. c->block_align = c->rate * (c->bits >> 3);
  141. return create_audio_stream(s, c);
  142. }
  143. static int siff_read_header(AVFormatContext *s)
  144. {
  145. AVIOContext *pb = s->pb;
  146. SIFFContext *c = s->priv_data;
  147. uint32_t tag;
  148. if (avio_rl32(pb) != TAG_SIFF)
  149. return -1;
  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 -1;
  155. }
  156. if (tag == TAG_VBV1 && siff_parse_vbv1(s, c, pb) < 0)
  157. return -1;
  158. if (tag == TAG_SOUN && siff_parse_soun(s, c, pb) < 0)
  159. return -1;
  160. if (avio_rl32(pb) != MKTAG('B', 'O', 'D', 'Y')){
  161. av_log(s, AV_LOG_ERROR, "'BODY' chunk is missing\n");
  162. return -1;
  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. int size;
  171. if (c->has_video){
  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 - c->gmcsize - 2;
  185. size = ffio_limit(s->pb, size);
  186. if(size < 0 || c->pktsize < c->sndsize)
  187. return AVERROR_INVALIDDATA;
  188. if (av_new_packet(pkt, size + c->gmcsize + 2) < 0)
  189. return AVERROR(ENOMEM);
  190. AV_WL16(pkt->data, c->flags);
  191. if (c->gmcsize)
  192. memcpy(pkt->data + 2, c->gmc, c->gmcsize);
  193. avio_read(s->pb, pkt->data + 2 + c->gmcsize, size);
  194. pkt->stream_index = 0;
  195. c->curstrm = -1;
  196. }else{
  197. if ((size = av_get_packet(s->pb, pkt, c->sndsize - 4)) < 0)
  198. return AVERROR(EIO);
  199. pkt->stream_index = 1;
  200. pkt->duration = size;
  201. c->curstrm = 0;
  202. }
  203. if(!c->cur_frame || c->curstrm)
  204. pkt->flags |= AV_PKT_FLAG_KEY;
  205. if (c->curstrm == -1)
  206. c->cur_frame++;
  207. }else{
  208. size = av_get_packet(s->pb, pkt, c->block_align);
  209. if(size <= 0)
  210. return AVERROR(EIO);
  211. pkt->duration = size;
  212. }
  213. return pkt->size;
  214. }
  215. AVInputFormat ff_siff_demuxer = {
  216. .name = "siff",
  217. .long_name = NULL_IF_CONFIG_SMALL("Beam Software SIFF"),
  218. .priv_data_size = sizeof(SIFFContext),
  219. .read_probe = siff_probe,
  220. .read_header = siff_read_header,
  221. .read_packet = siff_read_packet,
  222. .extensions = "vb,son",
  223. };