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.

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