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.

221 lines
7.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 "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. static int swf_read_header(AVFormatContext *s, AVFormatParameters *ap)
  49. {
  50. SWFContext *swf = s->priv_data;
  51. AVIOContext *pb = s->pb;
  52. int nbits, len, tag;
  53. tag = avio_rb32(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. avio_rl32(pb);
  61. /* skip rectangle size */
  62. nbits = avio_r8(pb) >> 3;
  63. len = (4 * nbits - 3 + 7) / 8;
  64. avio_skip(pb, len);
  65. swf->frame_rate = avio_rl16(pb); /* 8.8 fixed */
  66. avio_rl16(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. AVIOContext *pb = s->pb;
  75. AVStream *vst = NULL, *ast = NULL, *st = 0;
  76. int tag, len, i, frame, v;
  77. for(;;) {
  78. uint64_t pos = avio_tell(pb);
  79. tag = get_swf_tag(pb, &len);
  80. if (tag < 0)
  81. return tag;
  82. if (tag == TAG_VIDEOSTREAM) {
  83. int ch_id = avio_rl16(pb);
  84. len -= 2;
  85. for (i=0; i<s->nb_streams; i++) {
  86. st = s->streams[i];
  87. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && st->id == ch_id)
  88. goto skip;
  89. }
  90. avio_rl16(pb);
  91. avio_rl16(pb);
  92. avio_rl16(pb);
  93. avio_r8(pb);
  94. /* Check for FLV1 */
  95. vst = avformat_new_stream(s, NULL);
  96. if (!vst)
  97. return -1;
  98. vst->id = ch_id;
  99. vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  100. vst->codec->codec_id = ff_codec_get_id(swf_codec_tags, avio_r8(pb));
  101. avpriv_set_pts_info(vst, 16, 256, swf->frame_rate);
  102. vst->codec->time_base = (AVRational){ 256, swf->frame_rate };
  103. len -= 8;
  104. } else if (tag == TAG_STREAMHEAD || tag == TAG_STREAMHEAD2) {
  105. /* streaming found */
  106. int sample_rate_code;
  107. for (i=0; i<s->nb_streams; i++) {
  108. st = s->streams[i];
  109. if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->id == -1)
  110. goto skip;
  111. }
  112. avio_r8(pb);
  113. v = avio_r8(pb);
  114. swf->samples_per_frame = avio_rl16(pb);
  115. ast = avformat_new_stream(s, NULL);
  116. if (!ast)
  117. return -1;
  118. ast->id = -1; /* -1 to avoid clash with video stream ch_id */
  119. ast->codec->channels = 1 + (v&1);
  120. ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  121. ast->codec->codec_id = ff_codec_get_id(swf_audio_codec_tags, (v>>4) & 15);
  122. ast->need_parsing = AVSTREAM_PARSE_FULL;
  123. sample_rate_code= (v>>2) & 3;
  124. if (!sample_rate_code)
  125. ast->codec->sample_rate = 5512;
  126. else
  127. ast->codec->sample_rate = 11025 << (sample_rate_code-1);
  128. avpriv_set_pts_info(ast, 64, 1, ast->codec->sample_rate);
  129. len -= 4;
  130. } else if (tag == TAG_VIDEOFRAME) {
  131. int ch_id = avio_rl16(pb);
  132. len -= 2;
  133. for(i=0; i<s->nb_streams; i++) {
  134. st = s->streams[i];
  135. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && st->id == ch_id) {
  136. frame = avio_rl16(pb);
  137. av_get_packet(pb, pkt, len-2);
  138. pkt->pos = pos;
  139. pkt->pts = frame;
  140. pkt->stream_index = st->index;
  141. return pkt->size;
  142. }
  143. }
  144. } else if (tag == TAG_STREAMBLOCK) {
  145. for (i = 0; i < s->nb_streams; i++) {
  146. st = s->streams[i];
  147. if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->id == -1) {
  148. if (st->codec->codec_id == CODEC_ID_MP3) {
  149. avio_skip(pb, 4);
  150. av_get_packet(pb, pkt, len-4);
  151. } else { // ADPCM, PCM
  152. av_get_packet(pb, pkt, len);
  153. }
  154. pkt->pos = pos;
  155. pkt->stream_index = st->index;
  156. return pkt->size;
  157. }
  158. }
  159. } else if (tag == TAG_JPEG2) {
  160. for (i=0; i<s->nb_streams; i++) {
  161. st = s->streams[i];
  162. if (st->codec->codec_id == CODEC_ID_MJPEG && st->id == -2)
  163. break;
  164. }
  165. if (i == s->nb_streams) {
  166. vst = avformat_new_stream(s, NULL);
  167. if (!vst)
  168. return -1;
  169. vst->id = -2; /* -2 to avoid clash with video stream and audio stream */
  170. vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  171. vst->codec->codec_id = CODEC_ID_MJPEG;
  172. avpriv_set_pts_info(vst, 64, 256, swf->frame_rate);
  173. vst->codec->time_base = (AVRational){ 256, swf->frame_rate };
  174. st = vst;
  175. }
  176. avio_rl16(pb); /* BITMAP_ID */
  177. av_new_packet(pkt, len-2);
  178. avio_read(pb, pkt->data, 4);
  179. if (AV_RB32(pkt->data) == 0xffd8ffd9 ||
  180. AV_RB32(pkt->data) == 0xffd9ffd8) {
  181. /* old SWF files containing SOI/EOI as data start */
  182. /* files created by swink have reversed tag */
  183. pkt->size -= 4;
  184. avio_read(pb, pkt->data, pkt->size);
  185. } else {
  186. avio_read(pb, pkt->data + 4, pkt->size - 4);
  187. }
  188. pkt->pos = pos;
  189. pkt->stream_index = st->index;
  190. return pkt->size;
  191. }
  192. skip:
  193. avio_skip(pb, len);
  194. }
  195. }
  196. AVInputFormat ff_swf_demuxer = {
  197. .name = "swf",
  198. .long_name = NULL_IF_CONFIG_SMALL("Flash format"),
  199. .priv_data_size = sizeof(SWFContext),
  200. .read_probe = swf_probe,
  201. .read_header = swf_read_header,
  202. .read_packet = swf_read_packet,
  203. };