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.

296 lines
9.2KB

  1. /*
  2. * Flash Compatible Streaming Format demuxer
  3. * Copyright (c) 2000 Fabrice Bellard
  4. * Copyright (c) 2003 Tinic Uro
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "libavutil/intreadwrite.h"
  23. #include "swf.h"
  24. static const AVCodecTag swf_audio_codec_tags[] = {
  25. { AV_CODEC_ID_PCM_S16LE, 0x00 },
  26. { AV_CODEC_ID_ADPCM_SWF, 0x01 },
  27. { AV_CODEC_ID_MP3, 0x02 },
  28. { AV_CODEC_ID_PCM_S16LE, 0x03 },
  29. // { AV_CODEC_ID_NELLYMOSER, 0x06 },
  30. { AV_CODEC_ID_NONE, 0 },
  31. };
  32. static int get_swf_tag(AVIOContext *pb, int *len_ptr)
  33. {
  34. int tag, len;
  35. if (url_feof(pb))
  36. return AVERROR_EOF;
  37. tag = avio_rl16(pb);
  38. len = tag & 0x3f;
  39. tag = tag >> 6;
  40. if (len == 0x3f) {
  41. len = avio_rl32(pb);
  42. }
  43. *len_ptr = len;
  44. return tag;
  45. }
  46. static int swf_probe(AVProbeData *p)
  47. {
  48. /* check file header */
  49. if ((p->buf[0] == 'F' || p->buf[0] == 'C') && p->buf[1] == 'W' &&
  50. p->buf[2] == 'S')
  51. return AVPROBE_SCORE_MAX;
  52. else
  53. return 0;
  54. }
  55. #if CONFIG_ZLIB
  56. static int zlib_refill(void *opaque, uint8_t *buf, int buf_size)
  57. {
  58. AVFormatContext *s = opaque;
  59. SWFContext *swf = s->priv_data;
  60. z_stream *z = &swf->zstream;
  61. int ret;
  62. retry:
  63. if (!z->avail_in) {
  64. int n = avio_read(s->pb, swf->zbuf_in, ZBUF_SIZE);
  65. if (n <= 0)
  66. return n;
  67. z->next_in = swf->zbuf_in;
  68. z->avail_in = n;
  69. }
  70. z->next_out = buf;
  71. z->avail_out = buf_size;
  72. ret = inflate(z, Z_NO_FLUSH);
  73. if (ret < 0)
  74. return AVERROR(EINVAL);
  75. if (ret == Z_STREAM_END)
  76. return AVERROR_EOF;
  77. if (buf_size - z->avail_out == 0)
  78. goto retry;
  79. return buf_size - z->avail_out;
  80. }
  81. #endif
  82. static int swf_read_header(AVFormatContext *s)
  83. {
  84. SWFContext *swf = s->priv_data;
  85. AVIOContext *pb = s->pb;
  86. int nbits, len, tag;
  87. tag = avio_rb32(pb) & 0xffffff00;
  88. avio_rl32(pb);
  89. if (tag == MKBETAG('C', 'W', 'S', 0)) {
  90. av_log(s, AV_LOG_INFO, "SWF compressed file detected\n");
  91. #if CONFIG_ZLIB
  92. swf->zbuf_in = av_malloc(ZBUF_SIZE);
  93. swf->zbuf_out = av_malloc(ZBUF_SIZE);
  94. swf->zpb = avio_alloc_context(swf->zbuf_out, ZBUF_SIZE, 0, s,
  95. zlib_refill, NULL, NULL);
  96. if (!swf->zbuf_in || !swf->zbuf_out || !swf->zpb)
  97. return AVERROR(ENOMEM);
  98. swf->zpb->seekable = 0;
  99. if (inflateInit(&swf->zstream) != Z_OK) {
  100. av_log(s, AV_LOG_ERROR, "Unable to init zlib context\n");
  101. return AVERROR(EINVAL);
  102. }
  103. pb = swf->zpb;
  104. #else
  105. av_log(s, AV_LOG_ERROR, "zlib support is required to read SWF compressed files\n");
  106. return AVERROR(EIO);
  107. #endif
  108. } else if (tag != MKBETAG('F', 'W', 'S', 0))
  109. return AVERROR(EIO);
  110. /* skip rectangle size */
  111. nbits = avio_r8(pb) >> 3;
  112. len = (4 * nbits - 3 + 7) / 8;
  113. avio_skip(pb, len);
  114. swf->frame_rate = avio_rl16(pb); /* 8.8 fixed */
  115. avio_rl16(pb); /* frame count */
  116. swf->samples_per_frame = 0;
  117. s->ctx_flags |= AVFMTCTX_NOHEADER;
  118. return 0;
  119. }
  120. static int swf_read_packet(AVFormatContext *s, AVPacket *pkt)
  121. {
  122. SWFContext *swf = s->priv_data;
  123. AVIOContext *pb = s->pb;
  124. AVStream *vst = NULL, *ast = NULL, *st = 0;
  125. int tag, len, i, frame, v, res;
  126. #if CONFIG_ZLIB
  127. if (swf->zpb)
  128. pb = swf->zpb;
  129. #endif
  130. for(;;) {
  131. uint64_t pos = avio_tell(pb);
  132. tag = get_swf_tag(pb, &len);
  133. if (tag < 0)
  134. return tag;
  135. if (tag == TAG_VIDEOSTREAM) {
  136. int ch_id = avio_rl16(pb);
  137. len -= 2;
  138. for (i=0; i<s->nb_streams; i++) {
  139. st = s->streams[i];
  140. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && st->id == ch_id)
  141. goto skip;
  142. }
  143. avio_rl16(pb);
  144. avio_rl16(pb);
  145. avio_rl16(pb);
  146. avio_r8(pb);
  147. /* Check for FLV1 */
  148. vst = avformat_new_stream(s, NULL);
  149. if (!vst)
  150. return -1;
  151. vst->id = ch_id;
  152. vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  153. vst->codec->codec_id = ff_codec_get_id(ff_swf_codec_tags, avio_r8(pb));
  154. avpriv_set_pts_info(vst, 16, 256, swf->frame_rate);
  155. len -= 8;
  156. } else if (tag == TAG_STREAMHEAD || tag == TAG_STREAMHEAD2) {
  157. /* streaming found */
  158. int sample_rate_code;
  159. for (i=0; i<s->nb_streams; i++) {
  160. st = s->streams[i];
  161. if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->id == -1)
  162. goto skip;
  163. }
  164. avio_r8(pb);
  165. v = avio_r8(pb);
  166. swf->samples_per_frame = avio_rl16(pb);
  167. ast = avformat_new_stream(s, NULL);
  168. if (!ast)
  169. return -1;
  170. ast->id = -1; /* -1 to avoid clash with video stream ch_id */
  171. ast->codec->channels = 1 + (v&1);
  172. ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  173. ast->codec->codec_id = ff_codec_get_id(swf_audio_codec_tags, (v>>4) & 15);
  174. ast->need_parsing = AVSTREAM_PARSE_FULL;
  175. sample_rate_code= (v>>2) & 3;
  176. ast->codec->sample_rate = 44100 >> (3 - sample_rate_code);
  177. avpriv_set_pts_info(ast, 64, 1, ast->codec->sample_rate);
  178. len -= 4;
  179. } else if (tag == TAG_VIDEOFRAME) {
  180. int ch_id = avio_rl16(pb);
  181. len -= 2;
  182. for(i=0; i<s->nb_streams; i++) {
  183. st = s->streams[i];
  184. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && st->id == ch_id) {
  185. frame = avio_rl16(pb);
  186. if ((res = av_get_packet(pb, pkt, len-2)) < 0)
  187. return res;
  188. pkt->pos = pos;
  189. pkt->pts = frame;
  190. pkt->stream_index = st->index;
  191. return pkt->size;
  192. }
  193. }
  194. } else if (tag == TAG_STREAMBLOCK) {
  195. for (i = 0; i < s->nb_streams; i++) {
  196. st = s->streams[i];
  197. if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->id == -1) {
  198. if (st->codec->codec_id == AV_CODEC_ID_MP3) {
  199. avio_skip(pb, 4);
  200. if ((res = av_get_packet(pb, pkt, len-4)) < 0)
  201. return res;
  202. } else { // ADPCM, PCM
  203. if ((res = av_get_packet(pb, pkt, len)) < 0)
  204. return res;
  205. }
  206. pkt->pos = pos;
  207. pkt->stream_index = st->index;
  208. return pkt->size;
  209. }
  210. }
  211. } else if (tag == TAG_JPEG2) {
  212. for (i=0; i<s->nb_streams; i++) {
  213. st = s->streams[i];
  214. if (st->codec->codec_id == AV_CODEC_ID_MJPEG && st->id == -2)
  215. break;
  216. }
  217. if (i == s->nb_streams) {
  218. vst = avformat_new_stream(s, NULL);
  219. if (!vst)
  220. return -1;
  221. vst->id = -2; /* -2 to avoid clash with video stream and audio stream */
  222. vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  223. vst->codec->codec_id = AV_CODEC_ID_MJPEG;
  224. avpriv_set_pts_info(vst, 64, 256, swf->frame_rate);
  225. st = vst;
  226. }
  227. avio_rl16(pb); /* BITMAP_ID */
  228. if ((res = av_new_packet(pkt, len-2)) < 0)
  229. return res;
  230. avio_read(pb, pkt->data, 4);
  231. if (AV_RB32(pkt->data) == 0xffd8ffd9 ||
  232. AV_RB32(pkt->data) == 0xffd9ffd8) {
  233. /* old SWF files containing SOI/EOI as data start */
  234. /* files created by swink have reversed tag */
  235. pkt->size -= 4;
  236. avio_read(pb, pkt->data, pkt->size);
  237. } else {
  238. avio_read(pb, pkt->data + 4, pkt->size - 4);
  239. }
  240. pkt->pos = pos;
  241. pkt->stream_index = st->index;
  242. return pkt->size;
  243. }
  244. skip:
  245. avio_skip(pb, len);
  246. }
  247. }
  248. #if CONFIG_ZLIB
  249. static av_cold int swf_read_close(AVFormatContext *avctx)
  250. {
  251. SWFContext *s = avctx->priv_data;
  252. inflateEnd(&s->zstream);
  253. av_freep(&s->zbuf_in);
  254. av_freep(&s->zbuf_out);
  255. av_freep(&s->zpb);
  256. return 0;
  257. }
  258. #endif
  259. AVInputFormat ff_swf_demuxer = {
  260. .name = "swf",
  261. .long_name = NULL_IF_CONFIG_SMALL("SWF (ShockWave Flash)"),
  262. .priv_data_size = sizeof(SWFContext),
  263. .read_probe = swf_probe,
  264. .read_header = swf_read_header,
  265. .read_packet = swf_read_packet,
  266. #if CONFIG_ZLIB
  267. .read_close = swf_read_close,
  268. #endif
  269. };