Mercurial > libavformat.hg
annotate swfenc.c @ 3365:19bd4caf27a9 libavformat
consistency cosmetics: indices --> indexes
author | diego |
---|---|
date | Mon, 26 May 2008 23:14:25 +0000 |
parents | 7e144933facd |
children | 026b8ba58408 |
rev | line source |
---|---|
0 | 1 /* |
3356 | 2 * Flash Compatible Streaming Format muxer |
0 | 3 * Copyright (c) 2000 Fabrice Bellard. |
359 | 4 * Copyright (c) 2003 Tinic Uro. |
0 | 5 * |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1305
diff
changeset
|
6 * This file is part of FFmpeg. |
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1305
diff
changeset
|
7 * |
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1305
diff
changeset
|
8 * FFmpeg is free software; you can redistribute it and/or |
0 | 9 * modify it under the terms of the GNU Lesser General Public |
10 * License as published by the Free Software Foundation; either | |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1305
diff
changeset
|
11 * version 2.1 of the License, or (at your option) any later version. |
0 | 12 * |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1305
diff
changeset
|
13 * FFmpeg is distributed in the hope that it will be useful, |
0 | 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 | |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1305
diff
changeset
|
19 * License along with FFmpeg; if not, write to the Free Software |
896
edbe5c3717f9
Update licensing information: The FSF changed postal address.
diego
parents:
887
diff
changeset
|
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
0 | 21 */ |
3286 | 22 |
23 #include "libavcodec/bitstream.h" | |
0 | 24 #include "avformat.h" |
3302 | 25 #include "swf.h" |
0 | 26 |
27 static void put_swf_tag(AVFormatContext *s, int tag) | |
28 { | |
29 SWFContext *swf = s->priv_data; | |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2402
diff
changeset
|
30 ByteIOContext *pb = s->pb; |
0 | 31 |
32 swf->tag_pos = url_ftell(pb); | |
33 swf->tag = tag; | |
34 /* reserve some room for the tag */ | |
35 if (tag & TAG_LONG) { | |
36 put_le16(pb, 0); | |
37 put_le32(pb, 0); | |
38 } else { | |
39 put_le16(pb, 0); | |
40 } | |
41 } | |
42 | |
43 static void put_swf_end_tag(AVFormatContext *s) | |
44 { | |
45 SWFContext *swf = s->priv_data; | |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2402
diff
changeset
|
46 ByteIOContext *pb = s->pb; |
0 | 47 offset_t pos; |
48 int tag_len, tag; | |
49 | |
50 pos = url_ftell(pb); | |
51 tag_len = pos - swf->tag_pos - 2; | |
52 tag = swf->tag; | |
53 url_fseek(pb, swf->tag_pos, SEEK_SET); | |
54 if (tag & TAG_LONG) { | |
55 tag &= ~TAG_LONG; | |
56 put_le16(pb, (tag << 6) | 0x3f); | |
57 put_le32(pb, tag_len - 4); | |
58 } else { | |
59 assert(tag_len < 0x3f); | |
60 put_le16(pb, (tag << 6) | tag_len); | |
61 } | |
62 url_fseek(pb, pos, SEEK_SET); | |
63 } | |
64 | |
65 static inline void max_nbits(int *nbits_ptr, int val) | |
66 { | |
67 int n; | |
68 | |
69 if (val == 0) | |
70 return; | |
71 val = abs(val); | |
72 n = 1; | |
73 while (val != 0) { | |
74 n++; | |
75 val >>= 1; | |
76 } | |
77 if (n > *nbits_ptr) | |
78 *nbits_ptr = n; | |
79 } | |
80 | |
885 | 81 static void put_swf_rect(ByteIOContext *pb, |
0 | 82 int xmin, int xmax, int ymin, int ymax) |
83 { | |
84 PutBitContext p; | |
65 | 85 uint8_t buf[256]; |
0 | 86 int nbits, mask; |
87 | |
276 | 88 init_put_bits(&p, buf, sizeof(buf)); |
885 | 89 |
0 | 90 nbits = 0; |
91 max_nbits(&nbits, xmin); | |
92 max_nbits(&nbits, xmax); | |
93 max_nbits(&nbits, ymin); | |
94 max_nbits(&nbits, ymax); | |
95 mask = (1 << nbits) - 1; | |
96 | |
97 /* rectangle info */ | |
98 put_bits(&p, 5, nbits); | |
99 put_bits(&p, nbits, xmin & mask); | |
100 put_bits(&p, nbits, xmax & mask); | |
101 put_bits(&p, nbits, ymin & mask); | |
102 put_bits(&p, nbits, ymax & mask); | |
885 | 103 |
0 | 104 flush_put_bits(&p); |
105 put_buffer(pb, buf, pbBufPtr(&p) - p.buf); | |
106 } | |
107 | |
108 static void put_swf_line_edge(PutBitContext *pb, int dx, int dy) | |
109 { | |
110 int nbits, mask; | |
111 | |
112 put_bits(pb, 1, 1); /* edge */ | |
113 put_bits(pb, 1, 1); /* line select */ | |
114 nbits = 2; | |
115 max_nbits(&nbits, dx); | |
116 max_nbits(&nbits, dy); | |
117 | |
118 mask = (1 << nbits) - 1; | |
119 put_bits(pb, 4, nbits - 2); /* 16 bits precision */ | |
120 if (dx == 0) { | |
2308 | 121 put_bits(pb, 1, 0); |
122 put_bits(pb, 1, 1); | |
123 put_bits(pb, nbits, dy & mask); | |
0 | 124 } else if (dy == 0) { |
2308 | 125 put_bits(pb, 1, 0); |
126 put_bits(pb, 1, 0); | |
127 put_bits(pb, nbits, dx & mask); | |
0 | 128 } else { |
2308 | 129 put_bits(pb, 1, 1); |
130 put_bits(pb, nbits, dx & mask); | |
131 put_bits(pb, nbits, dy & mask); | |
0 | 132 } |
133 } | |
134 | |
135 #define FRAC_BITS 16 | |
136 | |
137 static void put_swf_matrix(ByteIOContext *pb, | |
138 int a, int b, int c, int d, int tx, int ty) | |
139 { | |
140 PutBitContext p; | |
65 | 141 uint8_t buf[256]; |
359 | 142 int nbits; |
0 | 143 |
276 | 144 init_put_bits(&p, buf, sizeof(buf)); |
885 | 145 |
0 | 146 put_bits(&p, 1, 1); /* a, d present */ |
359 | 147 nbits = 1; |
148 max_nbits(&nbits, a); | |
149 max_nbits(&nbits, d); | |
150 put_bits(&p, 5, nbits); /* nb bits */ | |
151 put_bits(&p, nbits, a); | |
152 put_bits(&p, nbits, d); | |
885 | 153 |
0 | 154 put_bits(&p, 1, 1); /* b, c present */ |
359 | 155 nbits = 1; |
156 max_nbits(&nbits, c); | |
157 max_nbits(&nbits, b); | |
158 put_bits(&p, 5, nbits); /* nb bits */ | |
159 put_bits(&p, nbits, c); | |
160 put_bits(&p, nbits, b); | |
0 | 161 |
359 | 162 nbits = 1; |
163 max_nbits(&nbits, tx); | |
164 max_nbits(&nbits, ty); | |
165 put_bits(&p, 5, nbits); /* nb bits */ | |
166 put_bits(&p, nbits, tx); | |
167 put_bits(&p, nbits, ty); | |
0 | 168 |
169 flush_put_bits(&p); | |
170 put_buffer(pb, buf, pbBufPtr(&p) - p.buf); | |
171 } | |
172 | |
173 static int swf_write_header(AVFormatContext *s) | |
174 { | |
1623 | 175 SWFContext *swf = s->priv_data; |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2402
diff
changeset
|
176 ByteIOContext *pb = s->pb; |
0 | 177 AVCodecContext *enc, *audio_enc, *video_enc; |
178 PutBitContext p; | |
65 | 179 uint8_t buf1[256]; |
85
25062c9b1f86
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
65
diff
changeset
|
180 int i, width, height, rate, rate_base; |
2955
b2d1cd7ab383
new avm2 (flash 9) muxer, patch by Paul Egan, paulegan at mail dot com
bcoudurier
parents:
2913
diff
changeset
|
181 int is_avm2; |
0 | 182 |
359 | 183 swf->audio_in_pos = 0; |
184 swf->sound_samples = 0; | |
185 swf->swf_frame_number = 0; | |
186 swf->video_frame_number = 0; | |
187 | |
0 | 188 video_enc = NULL; |
189 audio_enc = NULL; | |
190 for(i=0;i<s->nb_streams;i++) { | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
807
diff
changeset
|
191 enc = s->streams[i]->codec; |
1854 | 192 if (enc->codec_type == CODEC_TYPE_AUDIO) { |
193 if (enc->codec_id == CODEC_ID_MP3) { | |
194 if (!enc->frame_size) { | |
195 av_log(s, AV_LOG_ERROR, "audio frame size not set\n"); | |
196 return -1; | |
197 } | |
198 audio_enc = enc; | |
199 } else { | |
1865 | 200 av_log(s, AV_LOG_ERROR, "SWF muxer only supports MP3\n"); |
1854 | 201 return -1; |
202 } | |
203 } else { | |
2309 | 204 if (enc->codec_id == CODEC_ID_VP6F || |
205 enc->codec_id == CODEC_ID_FLV1 || | |
206 enc->codec_id == CODEC_ID_MJPEG) { | |
359 | 207 video_enc = enc; |
208 } else { | |
1865 | 209 av_log(s, AV_LOG_ERROR, "SWF muxer only supports VP6, FLV1 and MJPEG\n"); |
359 | 210 return -1; |
211 } | |
212 } | |
0 | 213 } |
214 | |
215 if (!video_enc) { | |
2289 | 216 /* currently, cannot work correctly if audio only */ |
359 | 217 swf->video_type = 0; |
0 | 218 width = 320; |
219 height = 200; | |
85
25062c9b1f86
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
65
diff
changeset
|
220 rate = 10; |
25062c9b1f86
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
65
diff
changeset
|
221 rate_base= 1; |
0 | 222 } else { |
359 | 223 swf->video_type = video_enc->codec_id; |
0 | 224 width = video_enc->width; |
225 height = video_enc->height; | |
743 | 226 rate = video_enc->time_base.den; |
227 rate_base = video_enc->time_base.num; | |
0 | 228 } |
229 | |
2309 | 230 if (!audio_enc) { |
359 | 231 swf->audio_type = 0; |
2309 | 232 swf->samples_per_frame = (44100. * rate_base) / rate; |
359 | 233 } else { |
234 swf->audio_type = audio_enc->codec_id; | |
2309 | 235 swf->samples_per_frame = (audio_enc->sample_rate * rate_base) / rate; |
359 | 236 } |
237 | |
2955
b2d1cd7ab383
new avm2 (flash 9) muxer, patch by Paul Egan, paulegan at mail dot com
bcoudurier
parents:
2913
diff
changeset
|
238 is_avm2 = !strcmp("avm2", s->oformat->name); |
b2d1cd7ab383
new avm2 (flash 9) muxer, patch by Paul Egan, paulegan at mail dot com
bcoudurier
parents:
2913
diff
changeset
|
239 |
0 | 240 put_tag(pb, "FWS"); |
2955
b2d1cd7ab383
new avm2 (flash 9) muxer, patch by Paul Egan, paulegan at mail dot com
bcoudurier
parents:
2913
diff
changeset
|
241 if (is_avm2) { |
b2d1cd7ab383
new avm2 (flash 9) muxer, patch by Paul Egan, paulegan at mail dot com
bcoudurier
parents:
2913
diff
changeset
|
242 put_byte(pb, 9); |
b2d1cd7ab383
new avm2 (flash 9) muxer, patch by Paul Egan, paulegan at mail dot com
bcoudurier
parents:
2913
diff
changeset
|
243 } else if (video_enc && video_enc->codec_id == CODEC_ID_VP6F) { |
1305 | 244 put_byte(pb, 8); /* version (version 8 and above support VP6 codec) */ |
2309 | 245 } else if (video_enc && video_enc->codec_id == CODEC_ID_FLV1) { |
359 | 246 put_byte(pb, 6); /* version (version 6 and above support FLV1 codec) */ |
247 } else { | |
248 put_byte(pb, 4); /* version (should use 4 for mpeg audio support) */ | |
249 } | |
885 | 250 put_le32(pb, DUMMY_FILE_SIZE); /* dummy size |
251 (will be patched if not streamed) */ | |
0 | 252 |
359 | 253 put_swf_rect(pb, 0, width * 20, 0, height * 20); |
85
25062c9b1f86
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
65
diff
changeset
|
254 put_le16(pb, (rate * 256) / rate_base); /* frame rate */ |
0 | 255 swf->duration_pos = url_ftell(pb); |
85
25062c9b1f86
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
65
diff
changeset
|
256 put_le16(pb, (uint16_t)(DUMMY_DURATION * (int64_t)rate / rate_base)); /* frame count */ |
885 | 257 |
2955
b2d1cd7ab383
new avm2 (flash 9) muxer, patch by Paul Egan, paulegan at mail dot com
bcoudurier
parents:
2913
diff
changeset
|
258 /* avm2/swf v9 (also v8?) files require a file attribute tag */ |
b2d1cd7ab383
new avm2 (flash 9) muxer, patch by Paul Egan, paulegan at mail dot com
bcoudurier
parents:
2913
diff
changeset
|
259 if (is_avm2) { |
b2d1cd7ab383
new avm2 (flash 9) muxer, patch by Paul Egan, paulegan at mail dot com
bcoudurier
parents:
2913
diff
changeset
|
260 put_swf_tag(s, TAG_FILEATTRIBUTES); |
b2d1cd7ab383
new avm2 (flash 9) muxer, patch by Paul Egan, paulegan at mail dot com
bcoudurier
parents:
2913
diff
changeset
|
261 put_le32(pb, 1<<3); /* set ActionScript v3/AVM2 flag */ |
b2d1cd7ab383
new avm2 (flash 9) muxer, patch by Paul Egan, paulegan at mail dot com
bcoudurier
parents:
2913
diff
changeset
|
262 put_swf_end_tag(s); |
b2d1cd7ab383
new avm2 (flash 9) muxer, patch by Paul Egan, paulegan at mail dot com
bcoudurier
parents:
2913
diff
changeset
|
263 } |
b2d1cd7ab383
new avm2 (flash 9) muxer, patch by Paul Egan, paulegan at mail dot com
bcoudurier
parents:
2913
diff
changeset
|
264 |
0 | 265 /* define a shape with the jpeg inside */ |
2309 | 266 if (video_enc && (video_enc->codec_id == CODEC_ID_VP6F || |
267 video_enc->codec_id == CODEC_ID_FLV1)) { | |
268 } else if (video_enc && video_enc->codec_id == CODEC_ID_MJPEG) { | |
359 | 269 put_swf_tag(s, TAG_DEFINESHAPE); |
0 | 270 |
359 | 271 put_le16(pb, SHAPE_ID); /* ID of shape */ |
272 /* bounding rectangle */ | |
273 put_swf_rect(pb, 0, width, 0, height); | |
274 /* style info */ | |
275 put_byte(pb, 1); /* one fill style */ | |
276 put_byte(pb, 0x41); /* clipped bitmap fill */ | |
277 put_le16(pb, BITMAP_ID); /* bitmap ID */ | |
278 /* position of the bitmap */ | |
885 | 279 put_swf_matrix(pb, (int)(1.0 * (1 << FRAC_BITS)), 0, |
2308 | 280 0, (int)(1.0 * (1 << FRAC_BITS)), 0, 0); |
359 | 281 put_byte(pb, 0); /* no line style */ |
885 | 282 |
359 | 283 /* shape drawing */ |
284 init_put_bits(&p, buf1, sizeof(buf1)); | |
285 put_bits(&p, 4, 1); /* one fill bit */ | |
286 put_bits(&p, 4, 0); /* zero line bit */ | |
885 | 287 |
359 | 288 put_bits(&p, 1, 0); /* not an edge */ |
289 put_bits(&p, 5, FLAG_MOVETO | FLAG_SETFILL0); | |
290 put_bits(&p, 5, 1); /* nbits */ | |
291 put_bits(&p, 1, 0); /* X */ | |
292 put_bits(&p, 1, 0); /* Y */ | |
293 put_bits(&p, 1, 1); /* set fill style 1 */ | |
885 | 294 |
359 | 295 /* draw the rectangle ! */ |
296 put_swf_line_edge(&p, width, 0); | |
297 put_swf_line_edge(&p, 0, height); | |
298 put_swf_line_edge(&p, -width, 0); | |
299 put_swf_line_edge(&p, 0, -height); | |
885 | 300 |
359 | 301 /* end of shape */ |
302 put_bits(&p, 1, 0); /* not an edge */ | |
303 put_bits(&p, 5, 0); | |
0 | 304 |
359 | 305 flush_put_bits(&p); |
306 put_buffer(pb, buf1, pbBufPtr(&p) - p.buf); | |
0 | 307 |
359 | 308 put_swf_end_tag(s); |
309 } | |
885 | 310 |
2309 | 311 if (audio_enc && audio_enc->codec_id == CODEC_ID_MP3) { |
0 | 312 int v; |
313 | |
314 /* start sound */ | |
359 | 315 put_swf_tag(s, TAG_STREAMHEAD2); |
0 | 316 |
317 v = 0; | |
318 switch(audio_enc->sample_rate) { | |
319 case 11025: | |
320 v |= 1 << 2; | |
321 break; | |
322 case 22050: | |
323 v |= 2 << 2; | |
324 break; | |
325 case 44100: | |
326 v |= 3 << 2; | |
327 break; | |
328 default: | |
329 /* not supported */ | |
2164 | 330 av_log(s, AV_LOG_ERROR, "swf does not support that sample rate, choose from (44100, 22050, 11025).\n"); |
0 | 331 return -1; |
332 } | |
359 | 333 v |= 0x02; /* 16 bit playback */ |
0 | 334 if (audio_enc->channels == 2) |
359 | 335 v |= 0x01; /* stereo playback */ |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2402
diff
changeset
|
336 put_byte(s->pb, v); |
0 | 337 v |= 0x20; /* mp3 compressed */ |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2402
diff
changeset
|
338 put_byte(s->pb, v); |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2402
diff
changeset
|
339 put_le16(s->pb, swf->samples_per_frame); /* avg samples per frame */ |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2402
diff
changeset
|
340 put_le16(s->pb, 0); |
885 | 341 |
0 | 342 put_swf_end_tag(s); |
343 } | |
344 | |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2402
diff
changeset
|
345 put_flush_packet(s->pb); |
0 | 346 return 0; |
347 } | |
348 | |
885 | 349 static int swf_write_video(AVFormatContext *s, |
241 | 350 AVCodecContext *enc, const uint8_t *buf, int size) |
0 | 351 { |
359 | 352 SWFContext *swf = s->priv_data; |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2402
diff
changeset
|
353 ByteIOContext *pb = s->pb; |
885 | 354 |
359 | 355 /* Flash Player limit */ |
2309 | 356 if (swf->swf_frame_number == 16000) { |
370
845f9de2c883
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
360
diff
changeset
|
357 av_log(enc, AV_LOG_INFO, "warning: Flash Player limit of 16000 frames reached\n"); |
359 | 358 } |
0 | 359 |
2309 | 360 if (swf->video_type == CODEC_ID_VP6F || |
361 swf->video_type == CODEC_ID_FLV1) { | |
362 if (swf->video_frame_number == 0) { | |
2308 | 363 /* create a new video object */ |
364 put_swf_tag(s, TAG_VIDEOSTREAM); | |
365 put_le16(pb, VIDEO_ID); | |
2309 | 366 put_le16(pb, 15000); /* hard flash player limit */ |
2308 | 367 put_le16(pb, enc->width); |
368 put_le16(pb, enc->height); | |
369 put_byte(pb, 0); | |
370 put_byte(pb,codec_get_tag(swf_codec_tags,swf->video_type)); | |
371 put_swf_end_tag(s); | |
885 | 372 |
2308 | 373 /* place the video object for the first time */ |
374 put_swf_tag(s, TAG_PLACEOBJECT2); | |
375 put_byte(pb, 0x36); | |
376 put_le16(pb, 1); | |
377 put_le16(pb, VIDEO_ID); | |
378 put_swf_matrix(pb, 1 << FRAC_BITS, 0, 0, 1 << FRAC_BITS, 0, 0); | |
2309 | 379 put_le16(pb, swf->video_frame_number); |
2308 | 380 put_byte(pb, 'v'); |
381 put_byte(pb, 'i'); | |
382 put_byte(pb, 'd'); | |
383 put_byte(pb, 'e'); | |
384 put_byte(pb, 'o'); | |
385 put_byte(pb, 0x00); | |
386 put_swf_end_tag(s); | |
387 } else { | |
388 /* mark the character for update */ | |
389 put_swf_tag(s, TAG_PLACEOBJECT2); | |
390 put_byte(pb, 0x11); | |
391 put_le16(pb, 1); | |
2309 | 392 put_le16(pb, swf->video_frame_number); |
2308 | 393 put_swf_end_tag(s); |
394 } | |
885 | 395 |
2308 | 396 /* set video frame data */ |
397 put_swf_tag(s, TAG_VIDEOFRAME | TAG_LONG); | |
398 put_le16(pb, VIDEO_ID); | |
2309 | 399 put_le16(pb, swf->video_frame_number++); |
2308 | 400 put_buffer(pb, buf, size); |
401 put_swf_end_tag(s); | |
2309 | 402 } else if (swf->video_type == CODEC_ID_MJPEG) { |
2308 | 403 if (swf->swf_frame_number > 0) { |
404 /* remove the shape */ | |
405 put_swf_tag(s, TAG_REMOVEOBJECT); | |
406 put_le16(pb, SHAPE_ID); /* shape ID */ | |
407 put_le16(pb, 1); /* depth */ | |
408 put_swf_end_tag(s); | |
885 | 409 |
2308 | 410 /* free the bitmap */ |
411 put_swf_tag(s, TAG_FREECHARACTER); | |
412 put_le16(pb, BITMAP_ID); | |
413 put_swf_end_tag(s); | |
414 } | |
885 | 415 |
2308 | 416 put_swf_tag(s, TAG_JPEG2 | TAG_LONG); |
885 | 417 |
2308 | 418 put_le16(pb, BITMAP_ID); /* ID of the image */ |
885 | 419 |
2308 | 420 /* a dummy jpeg header seems to be required */ |
421 put_byte(pb, 0xff); | |
422 put_byte(pb, 0xd8); | |
423 put_byte(pb, 0xff); | |
424 put_byte(pb, 0xd9); | |
425 /* write the jpeg image */ | |
426 put_buffer(pb, buf, size); | |
885 | 427 |
2308 | 428 put_swf_end_tag(s); |
885 | 429 |
2308 | 430 /* draw the shape */ |
885 | 431 |
2308 | 432 put_swf_tag(s, TAG_PLACEOBJECT); |
433 put_le16(pb, SHAPE_ID); /* shape ID */ | |
434 put_le16(pb, 1); /* depth */ | |
435 put_swf_matrix(pb, 20 << FRAC_BITS, 0, 0, 20 << FRAC_BITS, 0, 0); | |
436 put_swf_end_tag(s); | |
437 } else { | |
438 /* invalid codec */ | |
439 } | |
885 | 440 |
2308 | 441 swf->swf_frame_number ++; |
0 | 442 |
359 | 443 /* streaming sound always should be placed just before showframe tags */ |
1854 | 444 if (swf->audio_type && swf->audio_in_pos) { |
359 | 445 put_swf_tag(s, TAG_STREAMBLOCK | TAG_LONG); |
1854 | 446 put_le16(pb, swf->sound_samples); |
447 put_le16(pb, 0); // seek samples | |
448 put_buffer(pb, swf->audio_fifo, swf->audio_in_pos); | |
359 | 449 put_swf_end_tag(s); |
885 | 450 |
359 | 451 /* update FIFO */ |
1854 | 452 swf->sound_samples = 0; |
453 swf->audio_in_pos = 0; | |
359 | 454 } |
455 | |
0 | 456 /* output the frame */ |
457 put_swf_tag(s, TAG_SHOWFRAME); | |
458 put_swf_end_tag(s); | |
885 | 459 |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2402
diff
changeset
|
460 put_flush_packet(s->pb); |
885 | 461 |
0 | 462 return 0; |
463 } | |
464 | |
885 | 465 static int swf_write_audio(AVFormatContext *s, |
359 | 466 AVCodecContext *enc, const uint8_t *buf, int size) |
0 | 467 { |
359 | 468 SWFContext *swf = s->priv_data; |
469 | |
470 /* Flash Player limit */ | |
2309 | 471 if (swf->swf_frame_number == 16000) { |
370
845f9de2c883
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
360
diff
changeset
|
472 av_log(enc, AV_LOG_INFO, "warning: Flash Player limit of 16000 frames reached\n"); |
359 | 473 } |
0 | 474 |
1854 | 475 if (swf->audio_in_pos + size >= AUDIO_FIFO_SIZE) { |
476 av_log(s, AV_LOG_ERROR, "audio fifo too small to mux audio essence\n"); | |
477 return -1; | |
359 | 478 } |
0 | 479 |
1854 | 480 memcpy(swf->audio_fifo + swf->audio_in_pos, buf, size); |
481 swf->audio_in_pos += size; | |
482 swf->sound_samples += enc->frame_size; | |
483 | |
359 | 484 /* if audio only stream make sure we add swf frames */ |
2309 | 485 if (swf->video_type == 0) { |
359 | 486 swf_write_video(s, enc, 0, 0); |
487 } | |
488 | |
0 | 489 return 0; |
490 } | |
491 | |
468 | 492 static int swf_write_packet(AVFormatContext *s, AVPacket *pkt) |
0 | 493 { |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
807
diff
changeset
|
494 AVCodecContext *codec = s->streams[pkt->stream_index]->codec; |
0 | 495 if (codec->codec_type == CODEC_TYPE_AUDIO) |
468 | 496 return swf_write_audio(s, codec, pkt->data, pkt->size); |
0 | 497 else |
468 | 498 return swf_write_video(s, codec, pkt->data, pkt->size); |
0 | 499 } |
500 | |
501 static int swf_write_trailer(AVFormatContext *s) | |
502 { | |
503 SWFContext *swf = s->priv_data; | |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2402
diff
changeset
|
504 ByteIOContext *pb = s->pb; |
0 | 505 AVCodecContext *enc, *video_enc; |
506 int file_size, i; | |
507 | |
508 video_enc = NULL; | |
509 for(i=0;i<s->nb_streams;i++) { | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
807
diff
changeset
|
510 enc = s->streams[i]->codec; |
0 | 511 if (enc->codec_type == CODEC_TYPE_VIDEO) |
512 video_enc = enc; | |
513 } | |
514 | |
515 put_swf_tag(s, TAG_END); | |
516 put_swf_end_tag(s); | |
885 | 517 |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2402
diff
changeset
|
518 put_flush_packet(s->pb); |
0 | 519 |
520 /* patch file size and number of frames if not streamed */ | |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2402
diff
changeset
|
521 if (!url_is_streamed(s->pb) && video_enc) { |
0 | 522 file_size = url_ftell(pb); |
523 url_fseek(pb, 4, SEEK_SET); | |
524 put_le32(pb, file_size); | |
525 url_fseek(pb, swf->duration_pos, SEEK_SET); | |
526 put_le16(pb, video_enc->frame_number); | |
1643
20c25a594c49
seek back at the end of file after updating header
bcoudurier
parents:
1642
diff
changeset
|
527 url_fseek(pb, file_size, SEEK_SET); |
0 | 528 } |
529 return 0; | |
530 } | |
531 | |
1169 | 532 #ifdef CONFIG_SWF_MUXER |
533 AVOutputFormat swf_muxer = { | |
0 | 534 "swf", |
535 "Flash format", | |
536 "application/x-shockwave-flash", | |
537 "swf", | |
538 sizeof(SWFContext), | |
359 | 539 CODEC_ID_MP3, |
540 CODEC_ID_FLV1, | |
0 | 541 swf_write_header, |
542 swf_write_packet, | |
543 swf_write_trailer, | |
544 }; | |
1169 | 545 #endif |
2955
b2d1cd7ab383
new avm2 (flash 9) muxer, patch by Paul Egan, paulegan at mail dot com
bcoudurier
parents:
2913
diff
changeset
|
546 #ifdef CONFIG_AVM2_MUXER |
b2d1cd7ab383
new avm2 (flash 9) muxer, patch by Paul Egan, paulegan at mail dot com
bcoudurier
parents:
2913
diff
changeset
|
547 AVOutputFormat avm2_muxer = { |
b2d1cd7ab383
new avm2 (flash 9) muxer, patch by Paul Egan, paulegan at mail dot com
bcoudurier
parents:
2913
diff
changeset
|
548 "avm2", |
b2d1cd7ab383
new avm2 (flash 9) muxer, patch by Paul Egan, paulegan at mail dot com
bcoudurier
parents:
2913
diff
changeset
|
549 "Flash 9 (AVM2) format", |
b2d1cd7ab383
new avm2 (flash 9) muxer, patch by Paul Egan, paulegan at mail dot com
bcoudurier
parents:
2913
diff
changeset
|
550 "application/x-shockwave-flash", |
2959
1b7bf70aab74
unset extension, so code path, and guess format do not choose
bcoudurier
parents:
2955
diff
changeset
|
551 NULL, |
2955
b2d1cd7ab383
new avm2 (flash 9) muxer, patch by Paul Egan, paulegan at mail dot com
bcoudurier
parents:
2913
diff
changeset
|
552 sizeof(SWFContext), |
b2d1cd7ab383
new avm2 (flash 9) muxer, patch by Paul Egan, paulegan at mail dot com
bcoudurier
parents:
2913
diff
changeset
|
553 CODEC_ID_MP3, |
b2d1cd7ab383
new avm2 (flash 9) muxer, patch by Paul Egan, paulegan at mail dot com
bcoudurier
parents:
2913
diff
changeset
|
554 CODEC_ID_FLV1, |
b2d1cd7ab383
new avm2 (flash 9) muxer, patch by Paul Egan, paulegan at mail dot com
bcoudurier
parents:
2913
diff
changeset
|
555 swf_write_header, |
b2d1cd7ab383
new avm2 (flash 9) muxer, patch by Paul Egan, paulegan at mail dot com
bcoudurier
parents:
2913
diff
changeset
|
556 swf_write_packet, |
b2d1cd7ab383
new avm2 (flash 9) muxer, patch by Paul Egan, paulegan at mail dot com
bcoudurier
parents:
2913
diff
changeset
|
557 swf_write_trailer, |
b2d1cd7ab383
new avm2 (flash 9) muxer, patch by Paul Egan, paulegan at mail dot com
bcoudurier
parents:
2913
diff
changeset
|
558 }; |
b2d1cd7ab383
new avm2 (flash 9) muxer, patch by Paul Egan, paulegan at mail dot com
bcoudurier
parents:
2913
diff
changeset
|
559 #endif |