swfdec.c 9.0 KB

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