swfenc.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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. int64_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. static void put_swf_matrix(ByteIOContext *pb,
  121. int a, int b, int c, int d, int tx, int ty)
  122. {
  123. PutBitContext p;
  124. uint8_t buf[256];
  125. int nbits;
  126. init_put_bits(&p, buf, sizeof(buf));
  127. put_bits(&p, 1, 1); /* a, d present */
  128. nbits = 1;
  129. max_nbits(&nbits, a);
  130. max_nbits(&nbits, d);
  131. put_bits(&p, 5, nbits); /* nb bits */
  132. put_bits(&p, nbits, a);
  133. put_bits(&p, nbits, d);
  134. put_bits(&p, 1, 1); /* b, c present */
  135. nbits = 1;
  136. max_nbits(&nbits, c);
  137. max_nbits(&nbits, b);
  138. put_bits(&p, 5, nbits); /* nb bits */
  139. put_bits(&p, nbits, c);
  140. put_bits(&p, nbits, b);
  141. nbits = 1;
  142. max_nbits(&nbits, tx);
  143. max_nbits(&nbits, ty);
  144. put_bits(&p, 5, nbits); /* nb bits */
  145. put_bits(&p, nbits, tx);
  146. put_bits(&p, nbits, ty);
  147. flush_put_bits(&p);
  148. put_buffer(pb, buf, pbBufPtr(&p) - p.buf);
  149. }
  150. static int swf_write_header(AVFormatContext *s)
  151. {
  152. SWFContext *swf = s->priv_data;
  153. ByteIOContext *pb = s->pb;
  154. PutBitContext p;
  155. uint8_t buf1[256];
  156. int i, width, height, rate, rate_base;
  157. int version;
  158. swf->sound_samples = 0;
  159. swf->swf_frame_number = 0;
  160. swf->video_frame_number = 0;
  161. for(i=0;i<s->nb_streams;i++) {
  162. AVCodecContext *enc = s->streams[i]->codec;
  163. if (enc->codec_type == CODEC_TYPE_AUDIO) {
  164. if (enc->codec_id == CODEC_ID_MP3) {
  165. if (!enc->frame_size) {
  166. av_log(s, AV_LOG_ERROR, "audio frame size not set\n");
  167. return -1;
  168. }
  169. swf->audio_enc = enc;
  170. av_fifo_init(&swf->audio_fifo, AUDIO_FIFO_SIZE);
  171. } else {
  172. av_log(s, AV_LOG_ERROR, "SWF muxer only supports MP3\n");
  173. return -1;
  174. }
  175. } else {
  176. if (enc->codec_id == CODEC_ID_VP6F ||
  177. enc->codec_id == CODEC_ID_FLV1 ||
  178. enc->codec_id == CODEC_ID_MJPEG) {
  179. swf->video_enc = enc;
  180. } else {
  181. av_log(s, AV_LOG_ERROR, "SWF muxer only supports VP6, FLV1 and MJPEG\n");
  182. return -1;
  183. }
  184. }
  185. }
  186. if (!swf->video_enc) {
  187. /* currently, cannot work correctly if audio only */
  188. width = 320;
  189. height = 200;
  190. rate = 10;
  191. rate_base= 1;
  192. } else {
  193. width = swf->video_enc->width;
  194. height = swf->video_enc->height;
  195. rate = swf->video_enc->time_base.den;
  196. rate_base = swf->video_enc->time_base.num;
  197. }
  198. if (!swf->audio_enc)
  199. swf->samples_per_frame = (44100. * rate_base) / rate;
  200. else
  201. swf->samples_per_frame = (swf->audio_enc->sample_rate * rate_base) / rate;
  202. put_tag(pb, "FWS");
  203. if (!strcmp("avm2", s->oformat->name))
  204. version = 9;
  205. else if (swf->video_enc && swf->video_enc->codec_id == CODEC_ID_VP6F)
  206. version = 8; /* version 8 and above support VP6 codec */
  207. else if (swf->video_enc && swf->video_enc->codec_id == CODEC_ID_FLV1)
  208. version = 6; /* version 6 and above support FLV1 codec */
  209. else
  210. version = 4; /* version 4 for mpeg audio support */
  211. put_byte(pb, version);
  212. put_le32(pb, DUMMY_FILE_SIZE); /* dummy size
  213. (will be patched if not streamed) */
  214. put_swf_rect(pb, 0, width * 20, 0, height * 20);
  215. put_le16(pb, (rate * 256) / rate_base); /* frame rate */
  216. swf->duration_pos = url_ftell(pb);
  217. put_le16(pb, (uint16_t)(DUMMY_DURATION * (int64_t)rate / rate_base)); /* frame count */
  218. /* avm2/swf v9 (also v8?) files require a file attribute tag */
  219. if (version == 9) {
  220. put_swf_tag(s, TAG_FILEATTRIBUTES);
  221. put_le32(pb, 1<<3); /* set ActionScript v3/AVM2 flag */
  222. put_swf_end_tag(s);
  223. }
  224. /* define a shape with the jpeg inside */
  225. if (swf->video_enc && swf->video_enc->codec_id == CODEC_ID_MJPEG) {
  226. put_swf_tag(s, TAG_DEFINESHAPE);
  227. put_le16(pb, SHAPE_ID); /* ID of shape */
  228. /* bounding rectangle */
  229. put_swf_rect(pb, 0, width, 0, height);
  230. /* style info */
  231. put_byte(pb, 1); /* one fill style */
  232. put_byte(pb, 0x41); /* clipped bitmap fill */
  233. put_le16(pb, BITMAP_ID); /* bitmap ID */
  234. /* position of the bitmap */
  235. put_swf_matrix(pb, (int)(1.0 * (1 << FRAC_BITS)), 0,
  236. 0, (int)(1.0 * (1 << FRAC_BITS)), 0, 0);
  237. put_byte(pb, 0); /* no line style */
  238. /* shape drawing */
  239. init_put_bits(&p, buf1, sizeof(buf1));
  240. put_bits(&p, 4, 1); /* one fill bit */
  241. put_bits(&p, 4, 0); /* zero line bit */
  242. put_bits(&p, 1, 0); /* not an edge */
  243. put_bits(&p, 5, FLAG_MOVETO | FLAG_SETFILL0);
  244. put_bits(&p, 5, 1); /* nbits */
  245. put_bits(&p, 1, 0); /* X */
  246. put_bits(&p, 1, 0); /* Y */
  247. put_bits(&p, 1, 1); /* set fill style 1 */
  248. /* draw the rectangle ! */
  249. put_swf_line_edge(&p, width, 0);
  250. put_swf_line_edge(&p, 0, height);
  251. put_swf_line_edge(&p, -width, 0);
  252. put_swf_line_edge(&p, 0, -height);
  253. /* end of shape */
  254. put_bits(&p, 1, 0); /* not an edge */
  255. put_bits(&p, 5, 0);
  256. flush_put_bits(&p);
  257. put_buffer(pb, buf1, pbBufPtr(&p) - p.buf);
  258. put_swf_end_tag(s);
  259. }
  260. if (swf->audio_enc && swf->audio_enc->codec_id == CODEC_ID_MP3) {
  261. int v = 0;
  262. /* start sound */
  263. put_swf_tag(s, TAG_STREAMHEAD2);
  264. switch(swf->audio_enc->sample_rate) {
  265. case 11025: v |= 1 << 2; break;
  266. case 22050: v |= 2 << 2; break;
  267. case 44100: v |= 3 << 2; break;
  268. default:
  269. /* not supported */
  270. av_log(s, AV_LOG_ERROR, "swf does not support that sample rate, choose from (44100, 22050, 11025).\n");
  271. return -1;
  272. }
  273. v |= 0x02; /* 16 bit playback */
  274. if (swf->audio_enc->channels == 2)
  275. v |= 0x01; /* stereo playback */
  276. put_byte(s->pb, v);
  277. v |= 0x20; /* mp3 compressed */
  278. put_byte(s->pb, v);
  279. put_le16(s->pb, swf->samples_per_frame); /* avg samples per frame */
  280. put_le16(s->pb, 0);
  281. put_swf_end_tag(s);
  282. }
  283. put_flush_packet(s->pb);
  284. return 0;
  285. }
  286. static int swf_write_video(AVFormatContext *s,
  287. AVCodecContext *enc, const uint8_t *buf, int size)
  288. {
  289. SWFContext *swf = s->priv_data;
  290. ByteIOContext *pb = s->pb;
  291. /* Flash Player limit */
  292. if (swf->swf_frame_number == 16000)
  293. av_log(enc, AV_LOG_INFO, "warning: Flash Player limit of 16000 frames reached\n");
  294. if (enc->codec_id == CODEC_ID_VP6F ||
  295. enc->codec_id == CODEC_ID_FLV1) {
  296. if (swf->video_frame_number == 0) {
  297. /* create a new video object */
  298. put_swf_tag(s, TAG_VIDEOSTREAM);
  299. put_le16(pb, VIDEO_ID);
  300. swf->vframes_pos = url_ftell(pb);
  301. put_le16(pb, 15000); /* hard flash player limit */
  302. put_le16(pb, enc->width);
  303. put_le16(pb, enc->height);
  304. put_byte(pb, 0);
  305. put_byte(pb,codec_get_tag(swf_codec_tags,enc->codec_id));
  306. put_swf_end_tag(s);
  307. /* place the video object for the first time */
  308. put_swf_tag(s, TAG_PLACEOBJECT2);
  309. put_byte(pb, 0x36);
  310. put_le16(pb, 1);
  311. put_le16(pb, VIDEO_ID);
  312. put_swf_matrix(pb, 1 << FRAC_BITS, 0, 0, 1 << FRAC_BITS, 0, 0);
  313. put_le16(pb, swf->video_frame_number);
  314. put_tag(pb, "video");
  315. put_byte(pb, 0x00);
  316. put_swf_end_tag(s);
  317. } else {
  318. /* mark the character for update */
  319. put_swf_tag(s, TAG_PLACEOBJECT2);
  320. put_byte(pb, 0x11);
  321. put_le16(pb, 1);
  322. put_le16(pb, swf->video_frame_number);
  323. put_swf_end_tag(s);
  324. }
  325. /* set video frame data */
  326. put_swf_tag(s, TAG_VIDEOFRAME | TAG_LONG);
  327. put_le16(pb, VIDEO_ID);
  328. put_le16(pb, swf->video_frame_number++);
  329. put_buffer(pb, buf, size);
  330. put_swf_end_tag(s);
  331. } else if (enc->codec_id == CODEC_ID_MJPEG) {
  332. if (swf->swf_frame_number > 0) {
  333. /* remove the shape */
  334. put_swf_tag(s, TAG_REMOVEOBJECT);
  335. put_le16(pb, SHAPE_ID); /* shape ID */
  336. put_le16(pb, 1); /* depth */
  337. put_swf_end_tag(s);
  338. /* free the bitmap */
  339. put_swf_tag(s, TAG_FREECHARACTER);
  340. put_le16(pb, BITMAP_ID);
  341. put_swf_end_tag(s);
  342. }
  343. put_swf_tag(s, TAG_JPEG2 | TAG_LONG);
  344. put_le16(pb, BITMAP_ID); /* ID of the image */
  345. /* a dummy jpeg header seems to be required */
  346. put_be32(pb, 0xffd8ffd9);
  347. /* write the jpeg image */
  348. put_buffer(pb, buf, size);
  349. put_swf_end_tag(s);
  350. /* draw the shape */
  351. put_swf_tag(s, TAG_PLACEOBJECT);
  352. put_le16(pb, SHAPE_ID); /* shape ID */
  353. put_le16(pb, 1); /* depth */
  354. put_swf_matrix(pb, 20 << FRAC_BITS, 0, 0, 20 << FRAC_BITS, 0, 0);
  355. put_swf_end_tag(s);
  356. }
  357. swf->swf_frame_number++;
  358. /* streaming sound always should be placed just before showframe tags */
  359. if (swf->audio_enc && av_fifo_size(&swf->audio_fifo)) {
  360. int frame_size = av_fifo_size(&swf->audio_fifo);
  361. put_swf_tag(s, TAG_STREAMBLOCK | TAG_LONG);
  362. put_le16(pb, swf->sound_samples);
  363. put_le16(pb, 0); // seek samples
  364. av_fifo_generic_read(&swf->audio_fifo, frame_size, &put_buffer, pb);
  365. put_swf_end_tag(s);
  366. /* update FIFO */
  367. swf->sound_samples = 0;
  368. }
  369. /* output the frame */
  370. put_swf_tag(s, TAG_SHOWFRAME);
  371. put_swf_end_tag(s);
  372. put_flush_packet(s->pb);
  373. return 0;
  374. }
  375. static int swf_write_audio(AVFormatContext *s,
  376. AVCodecContext *enc, uint8_t *buf, int size)
  377. {
  378. SWFContext *swf = s->priv_data;
  379. /* Flash Player limit */
  380. if (swf->swf_frame_number == 16000)
  381. av_log(enc, AV_LOG_INFO, "warning: Flash Player limit of 16000 frames reached\n");
  382. if (av_fifo_size(&swf->audio_fifo) + size > AUDIO_FIFO_SIZE) {
  383. av_log(s, AV_LOG_ERROR, "audio fifo too small to mux audio essence\n");
  384. return -1;
  385. }
  386. av_fifo_generic_write(&swf->audio_fifo, buf, size, NULL);
  387. swf->sound_samples += enc->frame_size;
  388. /* if audio only stream make sure we add swf frames */
  389. if (!swf->video_enc)
  390. swf_write_video(s, enc, 0, 0);
  391. return 0;
  392. }
  393. static int swf_write_packet(AVFormatContext *s, AVPacket *pkt)
  394. {
  395. AVCodecContext *codec = s->streams[pkt->stream_index]->codec;
  396. if (codec->codec_type == CODEC_TYPE_AUDIO)
  397. return swf_write_audio(s, codec, pkt->data, pkt->size);
  398. else
  399. return swf_write_video(s, codec, pkt->data, pkt->size);
  400. }
  401. static int swf_write_trailer(AVFormatContext *s)
  402. {
  403. SWFContext *swf = s->priv_data;
  404. ByteIOContext *pb = s->pb;
  405. AVCodecContext *enc, *video_enc;
  406. int file_size, i;
  407. video_enc = NULL;
  408. for(i=0;i<s->nb_streams;i++) {
  409. enc = s->streams[i]->codec;
  410. if (enc->codec_type == CODEC_TYPE_VIDEO)
  411. video_enc = enc;
  412. else
  413. av_fifo_free(&swf->audio_fifo);
  414. }
  415. put_swf_tag(s, TAG_END);
  416. put_swf_end_tag(s);
  417. put_flush_packet(s->pb);
  418. /* patch file size and number of frames if not streamed */
  419. if (!url_is_streamed(s->pb) && video_enc) {
  420. file_size = url_ftell(pb);
  421. url_fseek(pb, 4, SEEK_SET);
  422. put_le32(pb, file_size);
  423. url_fseek(pb, swf->duration_pos, SEEK_SET);
  424. put_le16(pb, swf->video_frame_number);
  425. if (swf->vframes_pos) {
  426. url_fseek(pb, swf->vframes_pos, SEEK_SET);
  427. put_le16(pb, swf->video_frame_number);
  428. }
  429. url_fseek(pb, file_size, SEEK_SET);
  430. }
  431. return 0;
  432. }
  433. #if CONFIG_SWF_MUXER
  434. AVOutputFormat swf_muxer = {
  435. "swf",
  436. NULL_IF_CONFIG_SMALL("Flash format"),
  437. "application/x-shockwave-flash",
  438. "swf",
  439. sizeof(SWFContext),
  440. CODEC_ID_MP3,
  441. CODEC_ID_FLV1,
  442. swf_write_header,
  443. swf_write_packet,
  444. swf_write_trailer,
  445. };
  446. #endif
  447. #if CONFIG_AVM2_MUXER
  448. AVOutputFormat avm2_muxer = {
  449. "avm2",
  450. NULL_IF_CONFIG_SMALL("Flash 9 (AVM2) format"),
  451. "application/x-shockwave-flash",
  452. NULL,
  453. sizeof(SWFContext),
  454. CODEC_ID_MP3,
  455. CODEC_ID_FLV1,
  456. swf_write_header,
  457. swf_write_packet,
  458. swf_write_trailer,
  459. };
  460. #endif