swfdec.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. // av_log(NULL, AV_LOG_DEBUG, "Tag: %d - Len: %d\n", tag, len);
  44. *len_ptr = len;
  45. return tag;
  46. }
  47. static int swf_probe(AVProbeData *p)
  48. {
  49. /* check file header */
  50. if ((p->buf[0] == 'F' || p->buf[0] == 'C') && p->buf[1] == 'W' &&
  51. p->buf[2] == 'S')
  52. return AVPROBE_SCORE_MAX;
  53. else
  54. return 0;
  55. }
  56. #if CONFIG_ZLIB
  57. static int zlib_refill(void *opaque, uint8_t *buf, int buf_size)
  58. {
  59. AVFormatContext *s = opaque;
  60. SWFContext *swf = s->priv_data;
  61. z_stream *z = &swf->zstream;
  62. int ret;
  63. retry:
  64. if (!z->avail_in) {
  65. int n = avio_read(s->pb, swf->zbuf_in, ZBUF_SIZE);
  66. if (n < 0)
  67. return n;
  68. z->next_in = swf->zbuf_in;
  69. z->avail_in = n;
  70. }
  71. z->next_out = buf;
  72. z->avail_out = buf_size;
  73. ret = inflate(z, Z_NO_FLUSH);
  74. if (ret < 0)
  75. return AVERROR(EINVAL);
  76. if (ret == Z_STREAM_END)
  77. return AVERROR_EOF;
  78. if (buf_size - z->avail_out == 0)
  79. goto retry;
  80. return buf_size - z->avail_out;
  81. }
  82. #endif
  83. static int swf_read_header(AVFormatContext *s)
  84. {
  85. SWFContext *swf = s->priv_data;
  86. AVIOContext *pb = s->pb;
  87. int nbits, len, tag;
  88. tag = avio_rb32(pb) & 0xffffff00;
  89. avio_rl32(pb);
  90. if (tag == MKBETAG('C', 'W', 'S', 0)) {
  91. av_log(s, AV_LOG_INFO, "SWF compressed file detected\n");
  92. #if CONFIG_ZLIB
  93. swf->zbuf_in = av_malloc(ZBUF_SIZE);
  94. swf->zbuf_out = av_malloc(ZBUF_SIZE);
  95. swf->zpb = avio_alloc_context(swf->zbuf_out, ZBUF_SIZE, 0, s,
  96. zlib_refill, NULL, NULL);
  97. if (!swf->zbuf_in || !swf->zbuf_out || !swf->zpb)
  98. return AVERROR(ENOMEM);
  99. swf->zpb->seekable = 0;
  100. if (inflateInit(&swf->zstream) != Z_OK) {
  101. av_log(s, AV_LOG_ERROR, "Unable to init zlib context\n");
  102. return AVERROR(EINVAL);
  103. }
  104. pb = swf->zpb;
  105. #else
  106. av_log(s, AV_LOG_ERROR, "zlib support is required to read SWF compressed files\n");
  107. return AVERROR(EIO);
  108. #endif
  109. } else if (tag != MKBETAG('F', 'W', 'S', 0))
  110. return AVERROR(EIO);
  111. /* skip rectangle size */
  112. nbits = avio_r8(pb) >> 3;
  113. len = (4 * nbits - 3 + 7) / 8;
  114. avio_skip(pb, len);
  115. swf->frame_rate = avio_rl16(pb); /* 8.8 fixed */
  116. avio_rl16(pb); /* frame count */
  117. swf->samples_per_frame = 0;
  118. s->ctx_flags |= AVFMTCTX_NOHEADER;
  119. return 0;
  120. }
  121. static int swf_read_packet(AVFormatContext *s, AVPacket *pkt)
  122. {
  123. SWFContext *swf = s->priv_data;
  124. AVIOContext *pb = s->pb;
  125. AVStream *vst = NULL, *ast = NULL, *st = 0;
  126. int tag, len, i, frame, v, res;
  127. #if CONFIG_ZLIB
  128. if (swf->zpb)
  129. pb = swf->zpb;
  130. #endif
  131. for(;;) {
  132. uint64_t pos = avio_tell(pb);
  133. tag = get_swf_tag(pb, &len);
  134. if (tag < 0)
  135. return tag;
  136. if (len < 0) {
  137. av_log(s, AV_LOG_ERROR, "invalid tag length: %d\n", len);
  138. return AVERROR_INVALIDDATA;
  139. }
  140. if (tag == TAG_VIDEOSTREAM) {
  141. int ch_id = avio_rl16(pb);
  142. len -= 2;
  143. for (i=0; i<s->nb_streams; i++) {
  144. st = s->streams[i];
  145. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && st->id == ch_id)
  146. goto skip;
  147. }
  148. avio_rl16(pb);
  149. avio_rl16(pb);
  150. avio_rl16(pb);
  151. avio_r8(pb);
  152. /* Check for FLV1 */
  153. vst = avformat_new_stream(s, NULL);
  154. if (!vst)
  155. return -1;
  156. vst->id = ch_id;
  157. vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  158. vst->codec->codec_id = ff_codec_get_id(ff_swf_codec_tags, avio_r8(pb));
  159. avpriv_set_pts_info(vst, 16, 256, swf->frame_rate);
  160. len -= 8;
  161. } else if (tag == TAG_STREAMHEAD || tag == TAG_STREAMHEAD2) {
  162. /* streaming found */
  163. int sample_rate_code;
  164. for (i=0; i<s->nb_streams; i++) {
  165. st = s->streams[i];
  166. if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->id == -1)
  167. goto skip;
  168. }
  169. avio_r8(pb);
  170. v = avio_r8(pb);
  171. swf->samples_per_frame = avio_rl16(pb);
  172. ast = avformat_new_stream(s, NULL);
  173. if (!ast)
  174. return -1;
  175. ast->id = -1; /* -1 to avoid clash with video stream ch_id */
  176. ast->codec->channels = 1 + (v&1);
  177. ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  178. ast->codec->codec_id = ff_codec_get_id(swf_audio_codec_tags, (v>>4) & 15);
  179. ast->need_parsing = AVSTREAM_PARSE_FULL;
  180. sample_rate_code= (v>>2) & 3;
  181. ast->codec->sample_rate = 44100 >> (3 - sample_rate_code);
  182. avpriv_set_pts_info(ast, 64, 1, ast->codec->sample_rate);
  183. len -= 4;
  184. } else if (tag == TAG_VIDEOFRAME) {
  185. int ch_id = avio_rl16(pb);
  186. len -= 2;
  187. for(i=0; i<s->nb_streams; i++) {
  188. st = s->streams[i];
  189. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && st->id == ch_id) {
  190. frame = avio_rl16(pb);
  191. len -= 2;
  192. if (len <= 0)
  193. goto skip;
  194. if ((res = av_get_packet(pb, pkt, len)) < 0)
  195. return res;
  196. pkt->pos = pos;
  197. pkt->pts = frame;
  198. pkt->stream_index = st->index;
  199. return pkt->size;
  200. }
  201. }
  202. } else if (tag == TAG_STREAMBLOCK) {
  203. for (i = 0; i < s->nb_streams; i++) {
  204. st = s->streams[i];
  205. if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->id == -1) {
  206. if (st->codec->codec_id == AV_CODEC_ID_MP3) {
  207. avio_skip(pb, 4);
  208. len -= 4;
  209. if (len <= 0)
  210. goto skip;
  211. if ((res = av_get_packet(pb, pkt, len)) < 0)
  212. return res;
  213. } else { // ADPCM, PCM
  214. if (len <= 0)
  215. goto skip;
  216. if ((res = av_get_packet(pb, pkt, len)) < 0)
  217. return res;
  218. }
  219. pkt->pos = pos;
  220. pkt->stream_index = st->index;
  221. return pkt->size;
  222. }
  223. }
  224. } else if (tag == TAG_JPEG2) {
  225. for (i=0; i<s->nb_streams; i++) {
  226. st = s->streams[i];
  227. if (st->codec->codec_id == AV_CODEC_ID_MJPEG && st->id == -2)
  228. break;
  229. }
  230. if (i == s->nb_streams) {
  231. vst = avformat_new_stream(s, NULL);
  232. if (!vst)
  233. return -1;
  234. vst->id = -2; /* -2 to avoid clash with video stream and audio stream */
  235. vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  236. vst->codec->codec_id = AV_CODEC_ID_MJPEG;
  237. avpriv_set_pts_info(vst, 64, 256, swf->frame_rate);
  238. st = vst;
  239. }
  240. avio_rl16(pb); /* BITMAP_ID */
  241. len -= 2;
  242. if (len < 4)
  243. goto skip;
  244. if ((res = av_new_packet(pkt, len)) < 0)
  245. return res;
  246. avio_read(pb, pkt->data, 4);
  247. if (AV_RB32(pkt->data) == 0xffd8ffd9 ||
  248. AV_RB32(pkt->data) == 0xffd9ffd8) {
  249. /* old SWF files containing SOI/EOI as data start */
  250. /* files created by swink have reversed tag */
  251. pkt->size -= 4;
  252. avio_read(pb, pkt->data, pkt->size);
  253. } else {
  254. avio_read(pb, pkt->data + 4, pkt->size - 4);
  255. }
  256. pkt->pos = pos;
  257. pkt->stream_index = st->index;
  258. return pkt->size;
  259. }
  260. skip:
  261. len = FFMAX(0, len);
  262. avio_skip(pb, len);
  263. }
  264. }
  265. #if CONFIG_ZLIB
  266. static av_cold int swf_read_close(AVFormatContext *avctx)
  267. {
  268. SWFContext *s = avctx->priv_data;
  269. inflateEnd(&s->zstream);
  270. av_freep(&s->zbuf_in);
  271. av_freep(&s->zbuf_out);
  272. av_freep(&s->zpb);
  273. return 0;
  274. }
  275. #endif
  276. AVInputFormat ff_swf_demuxer = {
  277. .name = "swf",
  278. .long_name = NULL_IF_CONFIG_SMALL("SWF (ShockWave Flash)"),
  279. .priv_data_size = sizeof(SWFContext),
  280. .read_probe = swf_probe,
  281. .read_header = swf_read_header,
  282. .read_packet = swf_read_packet,
  283. #if CONFIG_ZLIB
  284. .read_close = swf_read_close,
  285. #endif
  286. };