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.

562 lines
16KB

  1. /*
  2. * Flash Compatible Streaming Format muxer.
  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 "libavcodec/bitstream.h"
  23. #include "avformat.h"
  24. #include "swf.h"
  25. static void put_swf_tag(AVFormatContext *s, int tag)
  26. {
  27. SWFContext *swf = s->priv_data;
  28. ByteIOContext *pb = s->pb;
  29. swf->tag_pos = url_ftell(pb);
  30. swf->tag = tag;
  31. /* reserve some room for the tag */
  32. if (tag & TAG_LONG) {
  33. put_le16(pb, 0);
  34. put_le32(pb, 0);
  35. } else {
  36. put_le16(pb, 0);
  37. }
  38. }
  39. static void put_swf_end_tag(AVFormatContext *s)
  40. {
  41. SWFContext *swf = s->priv_data;
  42. ByteIOContext *pb = s->pb;
  43. offset_t pos;
  44. int tag_len, tag;
  45. pos = url_ftell(pb);
  46. tag_len = pos - swf->tag_pos - 2;
  47. tag = swf->tag;
  48. url_fseek(pb, swf->tag_pos, SEEK_SET);
  49. if (tag & TAG_LONG) {
  50. tag &= ~TAG_LONG;
  51. put_le16(pb, (tag << 6) | 0x3f);
  52. put_le32(pb, tag_len - 4);
  53. } else {
  54. assert(tag_len < 0x3f);
  55. put_le16(pb, (tag << 6) | tag_len);
  56. }
  57. url_fseek(pb, pos, SEEK_SET);
  58. }
  59. static inline void max_nbits(int *nbits_ptr, int val)
  60. {
  61. int n;
  62. if (val == 0)
  63. return;
  64. val = abs(val);
  65. n = 1;
  66. while (val != 0) {
  67. n++;
  68. val >>= 1;
  69. }
  70. if (n > *nbits_ptr)
  71. *nbits_ptr = n;
  72. }
  73. static void put_swf_rect(ByteIOContext *pb,
  74. int xmin, int xmax, int ymin, int ymax)
  75. {
  76. PutBitContext p;
  77. uint8_t buf[256];
  78. int nbits, mask;
  79. init_put_bits(&p, buf, sizeof(buf));
  80. nbits = 0;
  81. max_nbits(&nbits, xmin);
  82. max_nbits(&nbits, xmax);
  83. max_nbits(&nbits, ymin);
  84. max_nbits(&nbits, ymax);
  85. mask = (1 << nbits) - 1;
  86. /* rectangle info */
  87. put_bits(&p, 5, nbits);
  88. put_bits(&p, nbits, xmin & mask);
  89. put_bits(&p, nbits, xmax & mask);
  90. put_bits(&p, nbits, ymin & mask);
  91. put_bits(&p, nbits, ymax & mask);
  92. flush_put_bits(&p);
  93. put_buffer(pb, buf, pbBufPtr(&p) - p.buf);
  94. }
  95. static void put_swf_line_edge(PutBitContext *pb, int dx, int dy)
  96. {
  97. int nbits, mask;
  98. put_bits(pb, 1, 1); /* edge */
  99. put_bits(pb, 1, 1); /* line select */
  100. nbits = 2;
  101. max_nbits(&nbits, dx);
  102. max_nbits(&nbits, dy);
  103. mask = (1 << nbits) - 1;
  104. put_bits(pb, 4, nbits - 2); /* 16 bits precision */
  105. if (dx == 0) {
  106. put_bits(pb, 1, 0);
  107. put_bits(pb, 1, 1);
  108. put_bits(pb, nbits, dy & mask);
  109. } else if (dy == 0) {
  110. put_bits(pb, 1, 0);
  111. put_bits(pb, 1, 0);
  112. put_bits(pb, nbits, dx & mask);
  113. } else {
  114. put_bits(pb, 1, 1);
  115. put_bits(pb, nbits, dx & mask);
  116. put_bits(pb, nbits, dy & mask);
  117. }
  118. }
  119. #define FRAC_BITS 16
  120. /* put matrix */
  121. static void put_swf_matrix(ByteIOContext *pb,
  122. int a, int b, int c, int d, int tx, int ty)
  123. {
  124. PutBitContext p;
  125. uint8_t buf[256];
  126. int nbits;
  127. init_put_bits(&p, buf, sizeof(buf));
  128. put_bits(&p, 1, 1); /* a, d present */
  129. nbits = 1;
  130. max_nbits(&nbits, a);
  131. max_nbits(&nbits, d);
  132. put_bits(&p, 5, nbits); /* nb bits */
  133. put_bits(&p, nbits, a);
  134. put_bits(&p, nbits, d);
  135. put_bits(&p, 1, 1); /* b, c present */
  136. nbits = 1;
  137. max_nbits(&nbits, c);
  138. max_nbits(&nbits, b);
  139. put_bits(&p, 5, nbits); /* nb bits */
  140. put_bits(&p, nbits, c);
  141. put_bits(&p, nbits, b);
  142. nbits = 1;
  143. max_nbits(&nbits, tx);
  144. max_nbits(&nbits, ty);
  145. put_bits(&p, 5, nbits); /* nb bits */
  146. put_bits(&p, nbits, tx);
  147. put_bits(&p, nbits, ty);
  148. flush_put_bits(&p);
  149. put_buffer(pb, buf, pbBufPtr(&p) - p.buf);
  150. }
  151. /* */
  152. static int swf_write_header(AVFormatContext *s)
  153. {
  154. SWFContext *swf = s->priv_data;
  155. ByteIOContext *pb = s->pb;
  156. AVCodecContext *enc, *audio_enc, *video_enc;
  157. PutBitContext p;
  158. uint8_t buf1[256];
  159. int i, width, height, rate, rate_base;
  160. int is_avm2;
  161. swf->audio_in_pos = 0;
  162. swf->sound_samples = 0;
  163. swf->swf_frame_number = 0;
  164. swf->video_frame_number = 0;
  165. video_enc = NULL;
  166. audio_enc = NULL;
  167. for(i=0;i<s->nb_streams;i++) {
  168. enc = s->streams[i]->codec;
  169. if (enc->codec_type == CODEC_TYPE_AUDIO) {
  170. if (enc->codec_id == CODEC_ID_MP3) {
  171. if (!enc->frame_size) {
  172. av_log(s, AV_LOG_ERROR, "audio frame size not set\n");
  173. return -1;
  174. }
  175. audio_enc = enc;
  176. } else {
  177. av_log(s, AV_LOG_ERROR, "SWF muxer only supports MP3\n");
  178. return -1;
  179. }
  180. } else {
  181. if (enc->codec_id == CODEC_ID_VP6F ||
  182. enc->codec_id == CODEC_ID_FLV1 ||
  183. enc->codec_id == CODEC_ID_MJPEG) {
  184. video_enc = enc;
  185. } else {
  186. av_log(s, AV_LOG_ERROR, "SWF muxer only supports VP6, FLV1 and MJPEG\n");
  187. return -1;
  188. }
  189. }
  190. }
  191. if (!video_enc) {
  192. /* currently, cannot work correctly if audio only */
  193. swf->video_type = 0;
  194. width = 320;
  195. height = 200;
  196. rate = 10;
  197. rate_base= 1;
  198. } else {
  199. swf->video_type = video_enc->codec_id;
  200. width = video_enc->width;
  201. height = video_enc->height;
  202. rate = video_enc->time_base.den;
  203. rate_base = video_enc->time_base.num;
  204. }
  205. if (!audio_enc) {
  206. swf->audio_type = 0;
  207. swf->samples_per_frame = (44100. * rate_base) / rate;
  208. } else {
  209. swf->audio_type = audio_enc->codec_id;
  210. swf->samples_per_frame = (audio_enc->sample_rate * rate_base) / rate;
  211. }
  212. is_avm2 = !strcmp("avm2", s->oformat->name);
  213. put_tag(pb, "FWS");
  214. if (is_avm2) {
  215. put_byte(pb, 9);
  216. } else if (video_enc && video_enc->codec_id == CODEC_ID_VP6F) {
  217. put_byte(pb, 8); /* version (version 8 and above support VP6 codec) */
  218. } else if (video_enc && video_enc->codec_id == CODEC_ID_FLV1) {
  219. put_byte(pb, 6); /* version (version 6 and above support FLV1 codec) */
  220. } else {
  221. put_byte(pb, 4); /* version (should use 4 for mpeg audio support) */
  222. }
  223. put_le32(pb, DUMMY_FILE_SIZE); /* dummy size
  224. (will be patched if not streamed) */
  225. put_swf_rect(pb, 0, width * 20, 0, height * 20);
  226. put_le16(pb, (rate * 256) / rate_base); /* frame rate */
  227. swf->duration_pos = url_ftell(pb);
  228. put_le16(pb, (uint16_t)(DUMMY_DURATION * (int64_t)rate / rate_base)); /* frame count */
  229. /* avm2/swf v9 (also v8?) files require a file attribute tag */
  230. if (is_avm2) {
  231. put_swf_tag(s, TAG_FILEATTRIBUTES);
  232. put_le32(pb, 1<<3); /* set ActionScript v3/AVM2 flag */
  233. put_swf_end_tag(s);
  234. }
  235. /* define a shape with the jpeg inside */
  236. if (video_enc && (video_enc->codec_id == CODEC_ID_VP6F ||
  237. video_enc->codec_id == CODEC_ID_FLV1)) {
  238. } else if (video_enc && video_enc->codec_id == CODEC_ID_MJPEG) {
  239. put_swf_tag(s, TAG_DEFINESHAPE);
  240. put_le16(pb, SHAPE_ID); /* ID of shape */
  241. /* bounding rectangle */
  242. put_swf_rect(pb, 0, width, 0, height);
  243. /* style info */
  244. put_byte(pb, 1); /* one fill style */
  245. put_byte(pb, 0x41); /* clipped bitmap fill */
  246. put_le16(pb, BITMAP_ID); /* bitmap ID */
  247. /* position of the bitmap */
  248. put_swf_matrix(pb, (int)(1.0 * (1 << FRAC_BITS)), 0,
  249. 0, (int)(1.0 * (1 << FRAC_BITS)), 0, 0);
  250. put_byte(pb, 0); /* no line style */
  251. /* shape drawing */
  252. init_put_bits(&p, buf1, sizeof(buf1));
  253. put_bits(&p, 4, 1); /* one fill bit */
  254. put_bits(&p, 4, 0); /* zero line bit */
  255. put_bits(&p, 1, 0); /* not an edge */
  256. put_bits(&p, 5, FLAG_MOVETO | FLAG_SETFILL0);
  257. put_bits(&p, 5, 1); /* nbits */
  258. put_bits(&p, 1, 0); /* X */
  259. put_bits(&p, 1, 0); /* Y */
  260. put_bits(&p, 1, 1); /* set fill style 1 */
  261. /* draw the rectangle ! */
  262. put_swf_line_edge(&p, width, 0);
  263. put_swf_line_edge(&p, 0, height);
  264. put_swf_line_edge(&p, -width, 0);
  265. put_swf_line_edge(&p, 0, -height);
  266. /* end of shape */
  267. put_bits(&p, 1, 0); /* not an edge */
  268. put_bits(&p, 5, 0);
  269. flush_put_bits(&p);
  270. put_buffer(pb, buf1, pbBufPtr(&p) - p.buf);
  271. put_swf_end_tag(s);
  272. }
  273. if (audio_enc && audio_enc->codec_id == CODEC_ID_MP3) {
  274. int v;
  275. /* start sound */
  276. put_swf_tag(s, TAG_STREAMHEAD2);
  277. v = 0;
  278. switch(audio_enc->sample_rate) {
  279. case 11025:
  280. v |= 1 << 2;
  281. break;
  282. case 22050:
  283. v |= 2 << 2;
  284. break;
  285. case 44100:
  286. v |= 3 << 2;
  287. break;
  288. default:
  289. /* not supported */
  290. av_log(s, AV_LOG_ERROR, "swf does not support that sample rate, choose from (44100, 22050, 11025).\n");
  291. return -1;
  292. }
  293. v |= 0x02; /* 16 bit playback */
  294. if (audio_enc->channels == 2)
  295. v |= 0x01; /* stereo playback */
  296. put_byte(s->pb, v);
  297. v |= 0x20; /* mp3 compressed */
  298. put_byte(s->pb, v);
  299. put_le16(s->pb, swf->samples_per_frame); /* avg samples per frame */
  300. put_le16(s->pb, 0);
  301. put_swf_end_tag(s);
  302. }
  303. put_flush_packet(s->pb);
  304. return 0;
  305. }
  306. static int swf_write_video(AVFormatContext *s,
  307. AVCodecContext *enc, const uint8_t *buf, int size)
  308. {
  309. SWFContext *swf = s->priv_data;
  310. ByteIOContext *pb = s->pb;
  311. /* Flash Player limit */
  312. if (swf->swf_frame_number == 16000) {
  313. av_log(enc, AV_LOG_INFO, "warning: Flash Player limit of 16000 frames reached\n");
  314. }
  315. if (swf->video_type == CODEC_ID_VP6F ||
  316. swf->video_type == CODEC_ID_FLV1) {
  317. if (swf->video_frame_number == 0) {
  318. /* create a new video object */
  319. put_swf_tag(s, TAG_VIDEOSTREAM);
  320. put_le16(pb, VIDEO_ID);
  321. put_le16(pb, 15000); /* hard flash player limit */
  322. put_le16(pb, enc->width);
  323. put_le16(pb, enc->height);
  324. put_byte(pb, 0);
  325. put_byte(pb,codec_get_tag(swf_codec_tags,swf->video_type));
  326. put_swf_end_tag(s);
  327. /* place the video object for the first time */
  328. put_swf_tag(s, TAG_PLACEOBJECT2);
  329. put_byte(pb, 0x36);
  330. put_le16(pb, 1);
  331. put_le16(pb, VIDEO_ID);
  332. put_swf_matrix(pb, 1 << FRAC_BITS, 0, 0, 1 << FRAC_BITS, 0, 0);
  333. put_le16(pb, swf->video_frame_number);
  334. put_byte(pb, 'v');
  335. put_byte(pb, 'i');
  336. put_byte(pb, 'd');
  337. put_byte(pb, 'e');
  338. put_byte(pb, 'o');
  339. put_byte(pb, 0x00);
  340. put_swf_end_tag(s);
  341. } else {
  342. /* mark the character for update */
  343. put_swf_tag(s, TAG_PLACEOBJECT2);
  344. put_byte(pb, 0x11);
  345. put_le16(pb, 1);
  346. put_le16(pb, swf->video_frame_number);
  347. put_swf_end_tag(s);
  348. }
  349. /* set video frame data */
  350. put_swf_tag(s, TAG_VIDEOFRAME | TAG_LONG);
  351. put_le16(pb, VIDEO_ID);
  352. put_le16(pb, swf->video_frame_number++);
  353. put_buffer(pb, buf, size);
  354. put_swf_end_tag(s);
  355. } else if (swf->video_type == CODEC_ID_MJPEG) {
  356. if (swf->swf_frame_number > 0) {
  357. /* remove the shape */
  358. put_swf_tag(s, TAG_REMOVEOBJECT);
  359. put_le16(pb, SHAPE_ID); /* shape ID */
  360. put_le16(pb, 1); /* depth */
  361. put_swf_end_tag(s);
  362. /* free the bitmap */
  363. put_swf_tag(s, TAG_FREECHARACTER);
  364. put_le16(pb, BITMAP_ID);
  365. put_swf_end_tag(s);
  366. }
  367. put_swf_tag(s, TAG_JPEG2 | TAG_LONG);
  368. put_le16(pb, BITMAP_ID); /* ID of the image */
  369. /* a dummy jpeg header seems to be required */
  370. put_byte(pb, 0xff);
  371. put_byte(pb, 0xd8);
  372. put_byte(pb, 0xff);
  373. put_byte(pb, 0xd9);
  374. /* write the jpeg image */
  375. put_buffer(pb, buf, size);
  376. put_swf_end_tag(s);
  377. /* draw the shape */
  378. put_swf_tag(s, TAG_PLACEOBJECT);
  379. put_le16(pb, SHAPE_ID); /* shape ID */
  380. put_le16(pb, 1); /* depth */
  381. put_swf_matrix(pb, 20 << FRAC_BITS, 0, 0, 20 << FRAC_BITS, 0, 0);
  382. put_swf_end_tag(s);
  383. } else {
  384. /* invalid codec */
  385. }
  386. swf->swf_frame_number ++;
  387. /* streaming sound always should be placed just before showframe tags */
  388. if (swf->audio_type && swf->audio_in_pos) {
  389. put_swf_tag(s, TAG_STREAMBLOCK | TAG_LONG);
  390. put_le16(pb, swf->sound_samples);
  391. put_le16(pb, 0); // seek samples
  392. put_buffer(pb, swf->audio_fifo, swf->audio_in_pos);
  393. put_swf_end_tag(s);
  394. /* update FIFO */
  395. swf->sound_samples = 0;
  396. swf->audio_in_pos = 0;
  397. }
  398. /* output the frame */
  399. put_swf_tag(s, TAG_SHOWFRAME);
  400. put_swf_end_tag(s);
  401. put_flush_packet(s->pb);
  402. return 0;
  403. }
  404. static int swf_write_audio(AVFormatContext *s,
  405. AVCodecContext *enc, const uint8_t *buf, int size)
  406. {
  407. SWFContext *swf = s->priv_data;
  408. /* Flash Player limit */
  409. if (swf->swf_frame_number == 16000) {
  410. av_log(enc, AV_LOG_INFO, "warning: Flash Player limit of 16000 frames reached\n");
  411. }
  412. if (swf->audio_in_pos + size >= AUDIO_FIFO_SIZE) {
  413. av_log(s, AV_LOG_ERROR, "audio fifo too small to mux audio essence\n");
  414. return -1;
  415. }
  416. memcpy(swf->audio_fifo + swf->audio_in_pos, buf, size);
  417. swf->audio_in_pos += size;
  418. swf->sound_samples += enc->frame_size;
  419. /* if audio only stream make sure we add swf frames */
  420. if (swf->video_type == 0) {
  421. swf_write_video(s, enc, 0, 0);
  422. }
  423. return 0;
  424. }
  425. static int swf_write_packet(AVFormatContext *s, AVPacket *pkt)
  426. {
  427. AVCodecContext *codec = s->streams[pkt->stream_index]->codec;
  428. if (codec->codec_type == CODEC_TYPE_AUDIO)
  429. return swf_write_audio(s, codec, pkt->data, pkt->size);
  430. else
  431. return swf_write_video(s, codec, pkt->data, pkt->size);
  432. }
  433. static int swf_write_trailer(AVFormatContext *s)
  434. {
  435. SWFContext *swf = s->priv_data;
  436. ByteIOContext *pb = s->pb;
  437. AVCodecContext *enc, *video_enc;
  438. int file_size, i;
  439. video_enc = NULL;
  440. for(i=0;i<s->nb_streams;i++) {
  441. enc = s->streams[i]->codec;
  442. if (enc->codec_type == CODEC_TYPE_VIDEO)
  443. video_enc = enc;
  444. }
  445. put_swf_tag(s, TAG_END);
  446. put_swf_end_tag(s);
  447. put_flush_packet(s->pb);
  448. /* patch file size and number of frames if not streamed */
  449. if (!url_is_streamed(s->pb) && video_enc) {
  450. file_size = url_ftell(pb);
  451. url_fseek(pb, 4, SEEK_SET);
  452. put_le32(pb, file_size);
  453. url_fseek(pb, swf->duration_pos, SEEK_SET);
  454. put_le16(pb, video_enc->frame_number);
  455. url_fseek(pb, file_size, SEEK_SET);
  456. }
  457. return 0;
  458. }
  459. #ifdef CONFIG_SWF_MUXER
  460. AVOutputFormat swf_muxer = {
  461. "swf",
  462. "Flash format",
  463. "application/x-shockwave-flash",
  464. "swf",
  465. sizeof(SWFContext),
  466. CODEC_ID_MP3,
  467. CODEC_ID_FLV1,
  468. swf_write_header,
  469. swf_write_packet,
  470. swf_write_trailer,
  471. };
  472. #endif
  473. #ifdef CONFIG_AVM2_MUXER
  474. AVOutputFormat avm2_muxer = {
  475. "avm2",
  476. "Flash 9 (AVM2) format",
  477. "application/x-shockwave-flash",
  478. NULL,
  479. sizeof(SWFContext),
  480. CODEC_ID_MP3,
  481. CODEC_ID_FLV1,
  482. swf_write_header,
  483. swf_write_packet,
  484. swf_write_trailer,
  485. };
  486. #endif