swfenc.c 16 KB

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