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.

348 lines
8.8KB

  1. /*
  2. * Flash Compatible Streaming Format
  3. * Copyright (c) 2000 Gerard Lantau.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <netinet/in.h>
  22. #include <string.h>
  23. #include "mpegenc.h"
  24. #include <assert.h>
  25. /* should have a generic way to indicate probable size */
  26. #define DUMMY_FILE_SIZE (100 * 1024 * 1024)
  27. #define DUMMY_DURATION 600 /* in seconds */
  28. #define TAG_END 0
  29. #define TAG_SHOWFRAME 1
  30. #define TAG_DEFINESHAPE 2
  31. #define TAG_FREECHARACTER 3
  32. #define TAG_PLACEOBJECT 4
  33. #define TAG_REMOVEOBJECT 5
  34. #define TAG_JPEG2 21
  35. #define TAG_LONG 0x100
  36. /* flags for shape definition */
  37. #define FLAG_MOVETO 0x01
  38. #define FLAG_SETFILL0 0x02
  39. #define FLAG_SETFILL1 0x04
  40. /* character id used */
  41. #define BITMAP_ID 0
  42. #define SHAPE_ID 1
  43. typedef struct {
  44. long long duration_pos;
  45. long long tag_pos;
  46. int tag;
  47. } SWFContext;
  48. static void put_swf_tag(AVFormatContext *s, int tag)
  49. {
  50. SWFContext *swf = s->priv_data;
  51. PutByteContext *pb = &s->pb;
  52. swf->tag_pos = put_pos(pb);
  53. swf->tag = tag;
  54. /* reserve some room for the tag */
  55. if (tag & TAG_LONG) {
  56. put_le16(pb, 0);
  57. put_le32(pb, 0);
  58. } else {
  59. put_le16(pb, 0);
  60. }
  61. }
  62. static void put_swf_end_tag(AVFormatContext *s)
  63. {
  64. SWFContext *swf = s->priv_data;
  65. PutByteContext *pb = &s->pb;
  66. long long pos;
  67. int tag_len, tag;
  68. pos = put_pos(pb);
  69. tag_len = pos - swf->tag_pos - 2;
  70. tag = swf->tag;
  71. put_seek(pb, swf->tag_pos, SEEK_SET);
  72. if (tag & TAG_LONG) {
  73. tag &= ~TAG_LONG;
  74. put_le16(pb, (tag << 6) | 0x3f);
  75. put_le32(pb, tag_len - 4);
  76. } else {
  77. assert(tag_len < 0x3f);
  78. put_le16(pb, (tag << 6) | tag_len);
  79. }
  80. put_seek(pb, pos, SEEK_SET);
  81. }
  82. static inline void max_nbits(int *nbits_ptr, int val)
  83. {
  84. int n;
  85. if (val == 0)
  86. return;
  87. val = abs(val);
  88. n = 1;
  89. while (val != 0) {
  90. n++;
  91. val >>= 1;
  92. }
  93. if (n > *nbits_ptr)
  94. *nbits_ptr = n;
  95. }
  96. static void put_swf_rect(PutByteContext *pb,
  97. int xmin, int xmax, int ymin, int ymax)
  98. {
  99. PutBitContext p;
  100. UINT8 buf[256];
  101. int nbits, mask;
  102. init_put_bits(&p, buf, sizeof(buf), NULL, NULL);
  103. nbits = 0;
  104. max_nbits(&nbits, xmin);
  105. max_nbits(&nbits, xmax);
  106. max_nbits(&nbits, ymin);
  107. max_nbits(&nbits, ymax);
  108. mask = (1 << nbits) - 1;
  109. /* rectangle info */
  110. put_bits(&p, 5, nbits);
  111. put_bits(&p, nbits, xmin & mask);
  112. put_bits(&p, nbits, xmax & mask);
  113. put_bits(&p, nbits, ymin & mask);
  114. put_bits(&p, nbits, ymax & mask);
  115. flush_put_bits(&p);
  116. put_buffer(pb, buf, p.buf_ptr - p.buf);
  117. }
  118. static void put_swf_line_edge(PutBitContext *pb, int dx, int dy)
  119. {
  120. int nbits, mask;
  121. put_bits(pb, 1, 1); /* edge */
  122. put_bits(pb, 1, 1); /* line select */
  123. nbits = 2;
  124. max_nbits(&nbits, dx);
  125. max_nbits(&nbits, dy);
  126. mask = (1 << nbits) - 1;
  127. put_bits(pb, 4, nbits - 2); /* 16 bits precision */
  128. if (dx == 0) {
  129. put_bits(pb, 1, 0);
  130. put_bits(pb, 1, 1);
  131. put_bits(pb, nbits, dy & mask);
  132. } else if (dy == 0) {
  133. put_bits(pb, 1, 0);
  134. put_bits(pb, 1, 0);
  135. put_bits(pb, nbits, dx & mask);
  136. } else {
  137. put_bits(pb, 1, 1);
  138. put_bits(pb, nbits, dx & mask);
  139. put_bits(pb, nbits, dy & mask);
  140. }
  141. }
  142. #define FRAC_BITS 16
  143. /* put matrix (not size optimized */
  144. static void put_swf_matrix(PutByteContext *pb,
  145. int a, int b, int c, int d, int tx, int ty)
  146. {
  147. PutBitContext p;
  148. UINT8 buf[256];
  149. init_put_bits(&p, buf, sizeof(buf), NULL, NULL);
  150. put_bits(&p, 1, 1); /* a, d present */
  151. put_bits(&p, 5, 20); /* nb bits */
  152. put_bits(&p, 20, a);
  153. put_bits(&p, 20, d);
  154. put_bits(&p, 1, 1); /* b, c present */
  155. put_bits(&p, 5, 20); /* nb bits */
  156. put_bits(&p, 20, c);
  157. put_bits(&p, 20, b);
  158. put_bits(&p, 5, 20); /* nb bits */
  159. put_bits(&p, 20, tx);
  160. put_bits(&p, 20, ty);
  161. flush_put_bits(&p);
  162. put_buffer(pb, buf, p.buf_ptr - p.buf);
  163. }
  164. static int swf_write_header(AVFormatContext *s)
  165. {
  166. SWFContext *swf;
  167. PutByteContext *pb = &s->pb;
  168. AVEncodeContext *enc = s->video_enc;
  169. PutBitContext p;
  170. UINT8 buf1[256];
  171. swf = malloc(sizeof(SWFContext));
  172. if (!swf)
  173. return -1;
  174. s->priv_data = swf;
  175. put_tag(pb, "FWS");
  176. put_byte(pb, 3); /* version (should use 4 for mpeg audio support) */
  177. put_le32(pb, DUMMY_FILE_SIZE); /* dummy size
  178. (will be patched if not streamed) */
  179. put_swf_rect(pb, 0, enc->width, 0, enc->height);
  180. put_le16(pb, enc->rate << 8); /* frame rate */
  181. swf->duration_pos = put_pos(pb);
  182. put_le16(pb, DUMMY_DURATION * enc->rate); /* frame count */
  183. /* define a shape with the jpeg inside */
  184. put_swf_tag(s, TAG_DEFINESHAPE);
  185. put_le16(pb, SHAPE_ID); /* ID of shape */
  186. /* bounding rectangle */
  187. put_swf_rect(pb, 0, enc->width, 0, enc->height);
  188. /* style info */
  189. put_byte(pb, 1); /* one fill style */
  190. put_byte(pb, 0x41); /* clipped bitmap fill */
  191. put_le16(pb, BITMAP_ID); /* bitmap ID */
  192. /* position of the bitmap */
  193. put_swf_matrix(pb, (int)(1.0 * (1 << FRAC_BITS)), 0,
  194. 0, (int)(1.0 * (1 << FRAC_BITS)), 0, 0);
  195. put_byte(pb, 0); /* no line style */
  196. /* shape drawing */
  197. init_put_bits(&p, buf1, sizeof(buf1), NULL, NULL);
  198. put_bits(&p, 4, 1); /* one fill bit */
  199. put_bits(&p, 4, 0); /* zero line bit */
  200. put_bits(&p, 1, 0); /* not an edge */
  201. put_bits(&p, 5, FLAG_MOVETO | FLAG_SETFILL0);
  202. put_bits(&p, 5, 1); /* nbits */
  203. put_bits(&p, 1, 0); /* X */
  204. put_bits(&p, 1, 0); /* Y */
  205. put_bits(&p, 1, 1); /* set fill style 1 */
  206. /* draw the rectangle ! */
  207. put_swf_line_edge(&p, enc->width, 0);
  208. put_swf_line_edge(&p, 0, enc->height);
  209. put_swf_line_edge(&p, -enc->width, 0);
  210. put_swf_line_edge(&p, 0, -enc->height);
  211. /* end of shape */
  212. put_bits(&p, 1, 0); /* not an edge */
  213. put_bits(&p, 5, 0);
  214. flush_put_bits(&p);
  215. put_buffer(pb, buf1, p.buf_ptr - p.buf);
  216. put_swf_end_tag(s);
  217. put_flush_packet(&s->pb);
  218. return 0;
  219. }
  220. static int swf_write_video(AVFormatContext *s, UINT8 *buf, int size)
  221. {
  222. PutByteContext *pb = &s->pb;
  223. AVEncodeContext *enc = s->video_enc;
  224. static int tag_id = 0;
  225. if (enc->frame_number > 1) {
  226. /* remove the shape */
  227. put_swf_tag(s, TAG_REMOVEOBJECT);
  228. put_le16(pb, SHAPE_ID); /* shape ID */
  229. put_le16(pb, 1); /* depth */
  230. put_swf_end_tag(s);
  231. /* free the bitmap */
  232. put_swf_tag(s, TAG_FREECHARACTER);
  233. put_le16(pb, BITMAP_ID);
  234. put_swf_end_tag(s);
  235. }
  236. put_swf_tag(s, TAG_JPEG2 | TAG_LONG);
  237. put_le16(pb, tag_id); /* ID of the image */
  238. /* a dummy jpeg header seems to be required */
  239. put_byte(pb, 0xff);
  240. put_byte(pb, 0xd8);
  241. put_byte(pb, 0xff);
  242. put_byte(pb, 0xd9);
  243. /* write the jpeg image */
  244. put_buffer(pb, buf, size);
  245. put_swf_end_tag(s);
  246. /* draw the shape */
  247. put_swf_tag(s, TAG_PLACEOBJECT);
  248. put_le16(pb, SHAPE_ID); /* shape ID */
  249. put_le16(pb, 1); /* depth */
  250. put_swf_matrix(pb, 1 << FRAC_BITS, 0, 0, 1 << FRAC_BITS, 0, 0);
  251. put_swf_end_tag(s);
  252. /* output the frame */
  253. put_swf_tag(s, TAG_SHOWFRAME);
  254. put_swf_end_tag(s);
  255. put_flush_packet(&s->pb);
  256. return 0;
  257. }
  258. static int swf_write_trailer(AVFormatContext *s)
  259. {
  260. SWFContext *swf = s->priv_data;
  261. PutByteContext *pb = &s->pb;
  262. int file_size;
  263. AVEncodeContext *enc = s->video_enc;
  264. put_swf_tag(s, TAG_END);
  265. put_swf_end_tag(s);
  266. put_flush_packet(&s->pb);
  267. /* patch file size and number of frames if not streamed */
  268. if (!s->is_streamed) {
  269. file_size = put_pos(pb);
  270. put_seek(pb, 4, SEEK_SET);
  271. put_le32(pb, file_size);
  272. put_seek(pb, swf->duration_pos, SEEK_SET);
  273. put_le16(pb, enc->frame_number);
  274. }
  275. free(swf);
  276. return 0;
  277. }
  278. AVFormat swf_format = {
  279. "swf",
  280. "Flash format",
  281. "application/x-shockwave-flash",
  282. "swf",
  283. CODEC_ID_NONE,
  284. CODEC_ID_MJPEG,
  285. swf_write_header,
  286. NULL,
  287. swf_write_video,
  288. swf_write_trailer,
  289. };