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.

194 lines
6.4KB

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