swfdec.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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(ByteIOContext *pb, int *len_ptr)
  25. {
  26. int tag, len;
  27. if (url_feof(pb))
  28. return -1;
  29. tag = get_le16(pb);
  30. len = tag & 0x3f;
  31. tag = tag >> 6;
  32. if (len == 0x3f) {
  33. len = get_le32(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. static int swf_read_header(AVFormatContext *s, AVFormatParameters *ap)
  49. {
  50. SWFContext *swf = s->priv_data;
  51. ByteIOContext *pb = s->pb;
  52. int nbits, len, tag;
  53. tag = get_be32(pb) & 0xffffff00;
  54. if (tag == MKBETAG('C', 'W', 'S', 0)) {
  55. av_log(s, AV_LOG_ERROR, "Compressed SWF format not supported\n");
  56. return AVERROR(EIO);
  57. }
  58. if (tag != MKBETAG('F', 'W', 'S', 0))
  59. return AVERROR(EIO);
  60. get_le32(pb);
  61. /* skip rectangle size */
  62. nbits = get_byte(pb) >> 3;
  63. len = (4 * nbits - 3 + 7) / 8;
  64. url_fskip(pb, len);
  65. swf->frame_rate = get_le16(pb); /* 8.8 fixed */
  66. get_le16(pb); /* frame count */
  67. swf->samples_per_frame = 0;
  68. s->ctx_flags |= AVFMTCTX_NOHEADER;
  69. return 0;
  70. }
  71. static int swf_read_packet(AVFormatContext *s, AVPacket *pkt)
  72. {
  73. SWFContext *swf = s->priv_data;
  74. ByteIOContext *pb = s->pb;
  75. AVStream *vst = NULL, *ast = NULL, *st = 0;
  76. int tag, len, i, frame, v;
  77. for(;;) {
  78. tag = get_swf_tag(pb, &len);
  79. if (tag < 0)
  80. return AVERROR(EIO);
  81. if (tag == TAG_VIDEOSTREAM && !vst) {
  82. int ch_id = get_le16(pb);
  83. get_le16(pb);
  84. get_le16(pb);
  85. get_le16(pb);
  86. get_byte(pb);
  87. /* Check for FLV1 */
  88. vst = av_new_stream(s, ch_id);
  89. if (!vst)
  90. return -1;
  91. vst->codec->codec_type = CODEC_TYPE_VIDEO;
  92. vst->codec->codec_id = codec_get_id(swf_codec_tags, get_byte(pb));
  93. av_set_pts_info(vst, 64, 256, swf->frame_rate);
  94. vst->codec->time_base = (AVRational){ 256, swf->frame_rate };
  95. len -= 10;
  96. } else if ((tag == TAG_STREAMHEAD || tag == TAG_STREAMHEAD2) && !ast) {
  97. /* streaming found */
  98. int sample_rate_code;
  99. get_byte(pb);
  100. v = get_byte(pb);
  101. swf->samples_per_frame = get_le16(pb);
  102. ast = av_new_stream(s, -1); /* -1 to avoid clash with video stream ch_id */
  103. if (!ast)
  104. return -1;
  105. swf->audio_stream_index = ast->index;
  106. ast->codec->channels = 1 + (v&1);
  107. ast->codec->codec_type = CODEC_TYPE_AUDIO;
  108. ast->codec->codec_id = codec_get_id(swf_audio_codec_tags, (v>>4) & 15);
  109. ast->need_parsing = AVSTREAM_PARSE_FULL;
  110. sample_rate_code= (v>>2) & 3;
  111. if (!sample_rate_code)
  112. return AVERROR(EIO);
  113. ast->codec->sample_rate = 11025 << (sample_rate_code-1);
  114. av_set_pts_info(ast, 64, 1, ast->codec->sample_rate);
  115. len -= 4;
  116. } else if (tag == TAG_VIDEOFRAME) {
  117. int ch_id = get_le16(pb);
  118. len -= 2;
  119. for(i=0; i<s->nb_streams; i++) {
  120. st = s->streams[i];
  121. if (st->codec->codec_type == CODEC_TYPE_VIDEO && st->id == ch_id) {
  122. frame = get_le16(pb);
  123. av_get_packet(pb, pkt, len-2);
  124. pkt->pts = frame;
  125. pkt->stream_index = st->index;
  126. return pkt->size;
  127. }
  128. }
  129. } else if (tag == TAG_STREAMBLOCK) {
  130. st = s->streams[swf->audio_stream_index];
  131. if (st->codec->codec_id == CODEC_ID_MP3) {
  132. url_fskip(pb, 4);
  133. av_get_packet(pb, pkt, len-4);
  134. } else { // ADPCM, PCM
  135. av_get_packet(pb, pkt, len);
  136. }
  137. pkt->stream_index = st->index;
  138. return pkt->size;
  139. } else if (tag == TAG_JPEG2) {
  140. for (i=0; i<s->nb_streams; i++) {
  141. st = s->streams[i];
  142. if (st->id == -2)
  143. break;
  144. }
  145. if (i == s->nb_streams) {
  146. vst = av_new_stream(s, -2); /* -2 to avoid clash with video stream and audio stream */
  147. if (!vst)
  148. return -1;
  149. vst->codec->codec_type = CODEC_TYPE_VIDEO;
  150. vst->codec->codec_id = CODEC_ID_MJPEG;
  151. av_set_pts_info(vst, 64, 256, swf->frame_rate);
  152. vst->codec->time_base = (AVRational){ 256, swf->frame_rate };
  153. st = vst;
  154. }
  155. get_le16(pb); /* BITMAP_ID */
  156. av_new_packet(pkt, len-2);
  157. get_buffer(pb, pkt->data, 4);
  158. if (AV_RB32(pkt->data) == 0xffd8ffd9 ||
  159. AV_RB32(pkt->data) == 0xffd9ffd8) {
  160. /* old SWF files containing SOI/EOI as data start */
  161. /* files created by swink have reversed tag */
  162. pkt->size -= 4;
  163. get_buffer(pb, pkt->data, pkt->size);
  164. } else {
  165. get_buffer(pb, pkt->data + 4, pkt->size - 4);
  166. }
  167. pkt->stream_index = st->index;
  168. return pkt->size;
  169. }
  170. url_fskip(pb, len);
  171. }
  172. return 0;
  173. }
  174. AVInputFormat swf_demuxer = {
  175. "swf",
  176. NULL_IF_CONFIG_SMALL("Flash format"),
  177. sizeof(SWFContext),
  178. swf_probe,
  179. swf_read_header,
  180. swf_read_packet,
  181. };