Line | Branch | Exec | Source |
---|---|---|---|
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 | |||
23 | #include "config_components.h" | ||
24 | |||
25 | #include "libavcodec/put_bits.h" | ||
26 | #include "libavutil/avassert.h" | ||
27 | #include "libavutil/fifo.h" | ||
28 | #include "avformat.h" | ||
29 | #include "flv.h" | ||
30 | #include "mux.h" | ||
31 | #include "swf.h" | ||
32 | |||
33 | #define AUDIO_FIFO_SIZE 65536 | ||
34 | |||
35 | typedef struct SWFEncContext { | ||
36 | int64_t duration_pos; | ||
37 | int64_t tag_pos; | ||
38 | int64_t vframes_pos; | ||
39 | int samples_per_frame; | ||
40 | int sound_samples; | ||
41 | int swf_frame_number; | ||
42 | int video_frame_number; | ||
43 | int tag; | ||
44 | AVFifo *audio_fifo; | ||
45 | AVCodecParameters *audio_par, *video_par; | ||
46 | AVStream *video_st; | ||
47 | } SWFEncContext; | ||
48 | |||
49 | 77 | static void put_swf_tag(AVFormatContext *s, int tag) | |
50 | { | ||
51 | 77 | SWFEncContext *swf = s->priv_data; | |
52 | 77 | AVIOContext *pb = s->pb; | |
53 | |||
54 | 77 | swf->tag_pos = avio_tell(pb); | |
55 | 77 | swf->tag = tag; | |
56 | /* reserve some room for the tag */ | ||
57 |
2/2✓ Branch 0 taken 25 times.
✓ Branch 1 taken 52 times.
|
77 | if (tag & TAG_LONG) { |
58 | 25 | avio_wl16(pb, 0); | |
59 | 25 | avio_wl32(pb, 0); | |
60 | } else { | ||
61 | 52 | avio_wl16(pb, 0); | |
62 | } | ||
63 | 77 | } | |
64 | |||
65 | 77 | static void put_swf_end_tag(AVFormatContext *s) | |
66 | { | ||
67 | 77 | SWFEncContext *swf = s->priv_data; | |
68 | 77 | AVIOContext *pb = s->pb; | |
69 | int64_t pos; | ||
70 | int tag_len, tag; | ||
71 | |||
72 | 77 | pos = avio_tell(pb); | |
73 | 77 | tag_len = pos - swf->tag_pos - 2; | |
74 | 77 | tag = swf->tag; | |
75 | 77 | avio_seek(pb, swf->tag_pos, SEEK_SET); | |
76 |
2/2✓ Branch 0 taken 25 times.
✓ Branch 1 taken 52 times.
|
77 | if (tag & TAG_LONG) { |
77 | 25 | tag &= ~TAG_LONG; | |
78 | 25 | avio_wl16(pb, (tag << 6) | 0x3f); | |
79 | 25 | avio_wl32(pb, tag_len - 4); | |
80 | } else { | ||
81 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 52 times.
|
52 | av_assert0(tag_len < 0x3f); |
82 | 52 | avio_wl16(pb, (tag << 6) | tag_len); | |
83 | } | ||
84 | 77 | avio_seek(pb, pos, SEEK_SET); | |
85 | 77 | } | |
86 | |||
87 | 10 | static inline void max_nbits(int *nbits_ptr, int val) | |
88 | { | ||
89 | int n; | ||
90 | |||
91 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 4 times.
|
10 | if (val == 0) |
92 | 6 | return; | |
93 | 4 | val = FFABS(val); | |
94 | 4 | n = 1; | |
95 |
2/2✓ Branch 0 taken 60 times.
✓ Branch 1 taken 4 times.
|
64 | while (val != 0) { |
96 | 60 | n++; | |
97 | 60 | val >>= 1; | |
98 | } | ||
99 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
|
4 | if (n > *nbits_ptr) |
100 | 2 | *nbits_ptr = n; | |
101 | } | ||
102 | |||
103 | 1 | static void put_swf_rect(AVIOContext *pb, | |
104 | int xmin, int xmax, int ymin, int ymax) | ||
105 | { | ||
106 | PutBitContext p; | ||
107 | uint8_t buf[256]; | ||
108 | int nbits, mask; | ||
109 | |||
110 | 1 | init_put_bits(&p, buf, sizeof(buf)); | |
111 | |||
112 | 1 | nbits = 0; | |
113 | 1 | max_nbits(&nbits, xmin); | |
114 | 1 | max_nbits(&nbits, xmax); | |
115 | 1 | max_nbits(&nbits, ymin); | |
116 | 1 | max_nbits(&nbits, ymax); | |
117 | 1 | mask = (1 << nbits) - 1; | |
118 | |||
119 | /* rectangle info */ | ||
120 | 1 | put_bits(&p, 5, nbits); | |
121 | 1 | put_bits(&p, nbits, xmin & mask); | |
122 | 1 | put_bits(&p, nbits, xmax & mask); | |
123 | 1 | put_bits(&p, nbits, ymin & mask); | |
124 | 1 | put_bits(&p, nbits, ymax & mask); | |
125 | |||
126 | 1 | flush_put_bits(&p); | |
127 | 1 | avio_write(pb, buf, put_bits_ptr(&p) - p.buf); | |
128 | 1 | } | |
129 | |||
130 | ✗ | static void put_swf_line_edge(PutBitContext *pb, int dx, int dy) | |
131 | { | ||
132 | int nbits, mask; | ||
133 | |||
134 | ✗ | put_bits(pb, 1, 1); /* edge */ | |
135 | ✗ | put_bits(pb, 1, 1); /* line select */ | |
136 | ✗ | nbits = 2; | |
137 | ✗ | max_nbits(&nbits, dx); | |
138 | ✗ | max_nbits(&nbits, dy); | |
139 | |||
140 | ✗ | mask = (1 << nbits) - 1; | |
141 | ✗ | put_bits(pb, 4, nbits - 2); /* 16 bits precision */ | |
142 | ✗ | if (dx == 0) { | |
143 | ✗ | put_bits(pb, 1, 0); | |
144 | ✗ | put_bits(pb, 1, 1); | |
145 | ✗ | put_bits(pb, nbits, dy & mask); | |
146 | ✗ | } else if (dy == 0) { | |
147 | ✗ | put_bits(pb, 1, 0); | |
148 | ✗ | put_bits(pb, 1, 0); | |
149 | ✗ | put_bits(pb, nbits, dx & mask); | |
150 | } else { | ||
151 | ✗ | put_bits(pb, 1, 1); | |
152 | ✗ | put_bits(pb, nbits, dx & mask); | |
153 | ✗ | put_bits(pb, nbits, dy & mask); | |
154 | } | ||
155 | ✗ | } | |
156 | |||
157 | #define FRAC_BITS 16 | ||
158 | |||
159 | 1 | static void put_swf_matrix(AVIOContext *pb, | |
160 | int a, int b, int c, int d, int tx, int ty) | ||
161 | { | ||
162 | PutBitContext p; | ||
163 | uint8_t buf[256]; | ||
164 | int nbits; | ||
165 | |||
166 | 1 | init_put_bits(&p, buf, sizeof(buf)); | |
167 | |||
168 | 1 | put_bits(&p, 1, 1); /* a, d present */ | |
169 | 1 | nbits = 1; | |
170 | 1 | max_nbits(&nbits, a); | |
171 | 1 | max_nbits(&nbits, d); | |
172 | 1 | put_bits(&p, 5, nbits); /* nb bits */ | |
173 | 1 | put_bits(&p, nbits, a); | |
174 | 1 | put_bits(&p, nbits, d); | |
175 | |||
176 | 1 | put_bits(&p, 1, 1); /* b, c present */ | |
177 | 1 | nbits = 1; | |
178 | 1 | max_nbits(&nbits, c); | |
179 | 1 | max_nbits(&nbits, b); | |
180 | 1 | put_bits(&p, 5, nbits); /* nb bits */ | |
181 | 1 | put_bits(&p, nbits, c); | |
182 | 1 | put_bits(&p, nbits, b); | |
183 | |||
184 | 1 | nbits = 1; | |
185 | 1 | max_nbits(&nbits, tx); | |
186 | 1 | max_nbits(&nbits, ty); | |
187 | 1 | put_bits(&p, 5, nbits); /* nb bits */ | |
188 | 1 | put_bits(&p, nbits, tx); | |
189 | 1 | put_bits(&p, nbits, ty); | |
190 | |||
191 | 1 | flush_put_bits(&p); | |
192 | 1 | avio_write(pb, buf, put_bits_ptr(&p) - p.buf); | |
193 | 1 | } | |
194 | |||
195 | 1 | static int swf_write_header(AVFormatContext *s) | |
196 | { | ||
197 | 1 | SWFEncContext *swf = s->priv_data; | |
198 | 1 | AVIOContext *pb = s->pb; | |
199 | PutBitContext p; | ||
200 | uint8_t buf1[256]; | ||
201 | int i, width, height, rate, rate_base; | ||
202 | int version; | ||
203 | |||
204 | 1 | swf->sound_samples = 0; | |
205 | 1 | swf->swf_frame_number = 0; | |
206 | 1 | swf->video_frame_number = 0; | |
207 | |||
208 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | for(i=0;i<s->nb_streams;i++) { |
209 | 1 | AVCodecParameters *par = s->streams[i]->codecpar; | |
210 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (par->codec_type == AVMEDIA_TYPE_AUDIO) { |
211 | ✗ | if (par->codec_id == AV_CODEC_ID_MP3) { | |
212 | ✗ | swf->audio_par = par; | |
213 | ✗ | swf->audio_fifo = av_fifo_alloc2(AUDIO_FIFO_SIZE, 1, 0); | |
214 | ✗ | if (!swf->audio_fifo) | |
215 | ✗ | return AVERROR(ENOMEM); | |
216 | } else { | ||
217 | ✗ | av_log(s, AV_LOG_ERROR, "SWF muxer only supports MP3\n"); | |
218 | ✗ | return -1; | |
219 | } | ||
220 | } else { | ||
221 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if (ff_codec_get_tag(ff_swf_codec_tags, par->codec_id) || |
222 | ✗ | par->codec_id == AV_CODEC_ID_PNG || | |
223 | ✗ | par->codec_id == AV_CODEC_ID_MJPEG) { | |
224 | 1 | swf->video_st = s->streams[i]; | |
225 | 1 | swf->video_par = par; | |
226 | } else { | ||
227 | ✗ | av_log(s, AV_LOG_ERROR, "SWF muxer only supports VP6, FLV, Flash Screen Video, PNG and MJPEG\n"); | |
228 | ✗ | return -1; | |
229 | } | ||
230 | } | ||
231 | } | ||
232 | |||
233 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!swf->video_par) { |
234 | /* currently, cannot work correctly if audio only */ | ||
235 | ✗ | width = 320; | |
236 | ✗ | height = 200; | |
237 | ✗ | rate = 10; | |
238 | ✗ | rate_base= 1; | |
239 | } else { | ||
240 | 1 | width = swf->video_par->width; | |
241 | 1 | height = swf->video_par->height; | |
242 | // TODO: should be avg_frame_rate | ||
243 | 1 | rate = swf->video_st->time_base.den; | |
244 | 1 | rate_base = swf->video_st->time_base.num; | |
245 | } | ||
246 | |||
247 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (!swf->audio_par) |
248 | 1 | swf->samples_per_frame = (44100LL * rate_base) / rate; | |
249 | else | ||
250 | ✗ | swf->samples_per_frame = (swf->audio_par->sample_rate * rate_base) / rate; | |
251 | |||
252 | 1 | avio_write(pb, "FWS", 3); | |
253 | |||
254 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!strcmp("avm2", s->oformat->name)) |
255 | ✗ | version = 9; | |
256 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | else if (swf->video_par && (swf->video_par->codec_id == AV_CODEC_ID_VP6A || |
257 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | swf->video_par->codec_id == AV_CODEC_ID_VP6F || |
258 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | swf->video_par->codec_id == AV_CODEC_ID_PNG)) |
259 | ✗ | version = 8; /* version 8 and above support VP6 and PNG codec */ | |
260 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
1 | else if (swf->video_par && swf->video_par->codec_id == AV_CODEC_ID_FLASHSV) |
261 | ✗ | version = 7; /* version 7 and above support Flash Screen Video codec */ | |
262 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | else if (swf->video_par && swf->video_par->codec_id == AV_CODEC_ID_FLV1) |
263 | 1 | version = 6; /* version 6 and above support FLV1 codec */ | |
264 | else | ||
265 | ✗ | version = 4; /* version 4 for mpeg audio support */ | |
266 | 1 | avio_w8(pb, version); | |
267 | |||
268 | 1 | avio_wl32(pb, DUMMY_FILE_SIZE); /* dummy size | |
269 | (will be patched if not streamed) */ | ||
270 | |||
271 | 1 | put_swf_rect(pb, 0, width * 20, 0, height * 20); | |
272 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if ((rate * 256LL) / rate_base >= (1<<16)) { |
273 | ✗ | av_log(s, AV_LOG_ERROR, "Invalid (too large) frame rate %d/%d\n", rate, rate_base); | |
274 | ✗ | return AVERROR(EINVAL); | |
275 | } | ||
276 | 1 | avio_wl16(pb, (rate * 256LL) / rate_base); /* frame rate */ | |
277 | 1 | swf->duration_pos = avio_tell(pb); | |
278 | 1 | avio_wl16(pb, (uint16_t)(DUMMY_DURATION * (int64_t)rate / rate_base)); /* frame count */ | |
279 | |||
280 | /* swf v8 and later files require a file attribute tag */ | ||
281 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (version >= 8) { |
282 | ✗ | put_swf_tag(s, TAG_FILEATTRIBUTES); | |
283 | ✗ | avio_wl32(pb, (version >= 9) << 3); /* set ActionScript v3/AVM2 flag */ | |
284 | ✗ | put_swf_end_tag(s); | |
285 | } | ||
286 | |||
287 | /* define a shape with the jpeg inside */ | ||
288 |
3/6✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
|
1 | if (swf->video_par && (swf->video_par->codec_id == AV_CODEC_ID_MJPEG || swf->video_par->codec_id == AV_CODEC_ID_PNG)) { |
289 | ✗ | put_swf_tag(s, TAG_DEFINESHAPE); | |
290 | |||
291 | ✗ | avio_wl16(pb, SHAPE_ID); /* ID of shape */ | |
292 | /* bounding rectangle */ | ||
293 | ✗ | put_swf_rect(pb, 0, width, 0, height); | |
294 | /* style info */ | ||
295 | ✗ | avio_w8(pb, 1); /* one fill style */ | |
296 | ✗ | avio_w8(pb, 0x41); /* clipped bitmap fill */ | |
297 | ✗ | avio_wl16(pb, BITMAP_ID); /* bitmap ID */ | |
298 | /* position of the bitmap */ | ||
299 | ✗ | put_swf_matrix(pb, 1 << FRAC_BITS, 0, | |
300 | 0, 1 << FRAC_BITS, 0, 0); | ||
301 | ✗ | avio_w8(pb, 0); /* no line style */ | |
302 | |||
303 | /* shape drawing */ | ||
304 | ✗ | init_put_bits(&p, buf1, sizeof(buf1)); | |
305 | ✗ | put_bits(&p, 4, 1); /* one fill bit */ | |
306 | ✗ | put_bits(&p, 4, 0); /* zero line bit */ | |
307 | |||
308 | ✗ | put_bits(&p, 1, 0); /* not an edge */ | |
309 | ✗ | put_bits(&p, 5, FLAG_MOVETO | FLAG_SETFILL0); | |
310 | ✗ | put_bits(&p, 5, 1); /* nbits */ | |
311 | ✗ | put_bits(&p, 1, 0); /* X */ | |
312 | ✗ | put_bits(&p, 1, 0); /* Y */ | |
313 | ✗ | put_bits(&p, 1, 1); /* set fill style 1 */ | |
314 | |||
315 | /* draw the rectangle ! */ | ||
316 | ✗ | put_swf_line_edge(&p, width, 0); | |
317 | ✗ | put_swf_line_edge(&p, 0, height); | |
318 | ✗ | put_swf_line_edge(&p, -width, 0); | |
319 | ✗ | put_swf_line_edge(&p, 0, -height); | |
320 | |||
321 | /* end of shape */ | ||
322 | ✗ | put_bits(&p, 1, 0); /* not an edge */ | |
323 | ✗ | put_bits(&p, 5, 0); | |
324 | |||
325 | ✗ | flush_put_bits(&p); | |
326 | ✗ | avio_write(pb, buf1, put_bits_ptr(&p) - p.buf); | |
327 | |||
328 | ✗ | put_swf_end_tag(s); | |
329 | } | ||
330 | |||
331 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
1 | if (swf->audio_par && swf->audio_par->codec_id == AV_CODEC_ID_MP3) { |
332 | ✗ | int v = 0; | |
333 | |||
334 | /* start sound */ | ||
335 | ✗ | put_swf_tag(s, TAG_STREAMHEAD2); | |
336 | ✗ | switch(swf->audio_par->sample_rate) { | |
337 | ✗ | case 11025: v |= 1 << 2; break; | |
338 | ✗ | case 22050: v |= 2 << 2; break; | |
339 | ✗ | case 44100: v |= 3 << 2; break; | |
340 | ✗ | default: | |
341 | /* not supported */ | ||
342 | ✗ | av_log(s, AV_LOG_ERROR, "swf does not support that sample rate, choose from (44100, 22050, 11025).\n"); | |
343 | ✗ | return -1; | |
344 | } | ||
345 | ✗ | v |= 0x02; /* 16 bit playback */ | |
346 | ✗ | if (swf->audio_par->ch_layout.nb_channels == 2) | |
347 | ✗ | v |= 0x01; /* stereo playback */ | |
348 | ✗ | avio_w8(s->pb, v); | |
349 | ✗ | v |= 0x20; /* mp3 compressed */ | |
350 | ✗ | avio_w8(s->pb, v); | |
351 | ✗ | avio_wl16(s->pb, swf->samples_per_frame); /* avg samples per frame */ | |
352 | ✗ | avio_wl16(s->pb, 0); | |
353 | |||
354 | ✗ | put_swf_end_tag(s); | |
355 | } | ||
356 | |||
357 | 1 | return 0; | |
358 | } | ||
359 | |||
360 | ✗ | static int fifo_avio_wrapper(void *opaque, void *buf, size_t *nb_elems) | |
361 | { | ||
362 | ✗ | avio_write(opaque, buf, *nb_elems); | |
363 | ✗ | return 0; | |
364 | } | ||
365 | |||
366 | 25 | static int swf_write_video(AVFormatContext *s, | |
367 | AVCodecParameters *par, const uint8_t *buf, int size, unsigned pkt_flags) | ||
368 | { | ||
369 | 25 | SWFEncContext *swf = s->priv_data; | |
370 | 25 | AVIOContext *pb = s->pb; | |
371 | 25 | unsigned codec_tag = ff_codec_get_tag(ff_swf_codec_tags, par->codec_id); | |
372 | |||
373 | /* Flash Player limit */ | ||
374 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
|
25 | if (swf->swf_frame_number == 16000) |
375 | ✗ | av_log(s, AV_LOG_INFO, "warning: Flash Player limit of 16000 frames reached\n"); | |
376 | |||
377 |
1/2✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
|
25 | if (codec_tag) { |
378 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 24 times.
|
25 | if (swf->video_frame_number == 0) { |
379 | /* create a new video object */ | ||
380 | 1 | put_swf_tag(s, TAG_VIDEOSTREAM); | |
381 | 1 | avio_wl16(pb, VIDEO_ID); | |
382 | 1 | swf->vframes_pos = avio_tell(pb); | |
383 | 1 | avio_wl16(pb, 15000); /* hard flash player limit */ | |
384 | 1 | avio_wl16(pb, par->width); | |
385 | 1 | avio_wl16(pb, par->height); | |
386 | 1 | avio_w8(pb, 0); | |
387 | 1 | avio_w8(pb, codec_tag); | |
388 | 1 | put_swf_end_tag(s); | |
389 | |||
390 | /* place the video object for the first time */ | ||
391 | 1 | put_swf_tag(s, TAG_PLACEOBJECT2); | |
392 | 1 | avio_w8(pb, 0x36); | |
393 | 1 | avio_wl16(pb, 1); | |
394 | 1 | avio_wl16(pb, VIDEO_ID); | |
395 | 1 | put_swf_matrix(pb, 1 << FRAC_BITS, 0, 0, 1 << FRAC_BITS, 0, 0); | |
396 | 1 | avio_wl16(pb, swf->video_frame_number); | |
397 | 1 | avio_write(pb, "video", 5); | |
398 | 1 | avio_w8(pb, 0x00); | |
399 | 1 | put_swf_end_tag(s); | |
400 | } else { | ||
401 | /* mark the character for update */ | ||
402 | 24 | put_swf_tag(s, TAG_PLACEOBJECT2); | |
403 | 24 | avio_w8(pb, 0x11); | |
404 | 24 | avio_wl16(pb, 1); | |
405 | 24 | avio_wl16(pb, swf->video_frame_number); | |
406 | 24 | put_swf_end_tag(s); | |
407 | } | ||
408 | |||
409 | /* set video frame data */ | ||
410 | 25 | put_swf_tag(s, TAG_VIDEOFRAME | TAG_LONG); | |
411 | 25 | avio_wl16(pb, VIDEO_ID); | |
412 | 25 | avio_wl16(pb, swf->video_frame_number++); | |
413 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
|
25 | if (par->codec_id == AV_CODEC_ID_FLASHSV) { |
414 | /* FrameType and CodecId is needed here even if it is not documented correctly in the SWF specs */ | ||
415 | ✗ | int flags = codec_tag | (pkt_flags & AV_PKT_FLAG_KEY ? FLV_FRAME_KEY : FLV_FRAME_INTER); | |
416 | ✗ | avio_w8(pb, flags); | |
417 | } | ||
418 | 25 | avio_write(pb, buf, size); | |
419 | 25 | put_swf_end_tag(s); | |
420 | ✗ | } else if (par->codec_id == AV_CODEC_ID_MJPEG || par->codec_id == AV_CODEC_ID_PNG) { | |
421 | ✗ | if (swf->swf_frame_number > 0) { | |
422 | /* remove the shape */ | ||
423 | ✗ | put_swf_tag(s, TAG_REMOVEOBJECT); | |
424 | ✗ | avio_wl16(pb, SHAPE_ID); /* shape ID */ | |
425 | ✗ | avio_wl16(pb, 1); /* depth */ | |
426 | ✗ | put_swf_end_tag(s); | |
427 | |||
428 | /* free the bitmap */ | ||
429 | ✗ | put_swf_tag(s, TAG_FREECHARACTER); | |
430 | ✗ | avio_wl16(pb, BITMAP_ID); | |
431 | ✗ | put_swf_end_tag(s); | |
432 | } | ||
433 | |||
434 | ✗ | put_swf_tag(s, TAG_JPEG2 | TAG_LONG); | |
435 | |||
436 | ✗ | avio_wl16(pb, BITMAP_ID); /* ID of the image */ | |
437 | |||
438 | /* a dummy jpeg header seems to be required */ | ||
439 | ✗ | if (par->codec_id == AV_CODEC_ID_MJPEG) | |
440 | ✗ | avio_wb32(pb, 0xffd8ffd9); | |
441 | /* write the jpeg/png image */ | ||
442 | ✗ | avio_write(pb, buf, size); | |
443 | |||
444 | ✗ | put_swf_end_tag(s); | |
445 | |||
446 | /* draw the shape */ | ||
447 | |||
448 | ✗ | put_swf_tag(s, TAG_PLACEOBJECT); | |
449 | ✗ | avio_wl16(pb, SHAPE_ID); /* shape ID */ | |
450 | ✗ | avio_wl16(pb, 1); /* depth */ | |
451 | ✗ | put_swf_matrix(pb, 20 << FRAC_BITS, 0, 0, 20 << FRAC_BITS, 0, 0); | |
452 | ✗ | put_swf_end_tag(s); | |
453 | } | ||
454 | |||
455 | 25 | swf->swf_frame_number++; | |
456 | |||
457 | /* streaming sound always should be placed just before showframe tags */ | ||
458 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
25 | if (swf->audio_par && av_fifo_can_read(swf->audio_fifo)) { |
459 | ✗ | size_t frame_size = av_fifo_can_read(swf->audio_fifo); | |
460 | ✗ | put_swf_tag(s, TAG_STREAMBLOCK | TAG_LONG); | |
461 | ✗ | avio_wl16(pb, swf->sound_samples); | |
462 | ✗ | avio_wl16(pb, 0); // seek samples | |
463 | ✗ | av_fifo_read_to_cb(swf->audio_fifo, fifo_avio_wrapper, pb, &frame_size); | |
464 | ✗ | put_swf_end_tag(s); | |
465 | |||
466 | /* update FIFO */ | ||
467 | ✗ | swf->sound_samples = 0; | |
468 | } | ||
469 | |||
470 | /* output the frame */ | ||
471 | 25 | put_swf_tag(s, TAG_SHOWFRAME); | |
472 | 25 | put_swf_end_tag(s); | |
473 | |||
474 | 25 | return 0; | |
475 | } | ||
476 | |||
477 | ✗ | static int swf_write_audio(AVFormatContext *s, AVCodecParameters *par, | |
478 | const uint8_t *buf, int size) | ||
479 | { | ||
480 | ✗ | SWFEncContext *swf = s->priv_data; | |
481 | |||
482 | /* Flash Player limit */ | ||
483 | ✗ | if (swf->swf_frame_number == 16000) | |
484 | ✗ | av_log(s, AV_LOG_INFO, "warning: Flash Player limit of 16000 frames reached\n"); | |
485 | |||
486 | ✗ | if (av_fifo_can_write(swf->audio_fifo) < size) { | |
487 | ✗ | av_log(s, AV_LOG_ERROR, "audio fifo too small to mux audio essence\n"); | |
488 | ✗ | return -1; | |
489 | } | ||
490 | |||
491 | ✗ | av_fifo_write(swf->audio_fifo, buf, size); | |
492 | ✗ | swf->sound_samples += av_get_audio_frame_duration2(par, size); | |
493 | |||
494 | /* if audio only stream make sure we add swf frames */ | ||
495 | ✗ | if (!swf->video_par) | |
496 | ✗ | swf_write_video(s, par, 0, 0, 0); | |
497 | |||
498 | ✗ | return 0; | |
499 | } | ||
500 | |||
501 | 25 | static int swf_write_packet(AVFormatContext *s, AVPacket *pkt) | |
502 | { | ||
503 | 25 | AVCodecParameters *par = s->streams[pkt->stream_index]->codecpar; | |
504 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
|
25 | if (par->codec_type == AVMEDIA_TYPE_AUDIO) |
505 | ✗ | return swf_write_audio(s, par, pkt->data, pkt->size); | |
506 | else | ||
507 | 25 | return swf_write_video(s, par, pkt->data, pkt->size, pkt->flags); | |
508 | } | ||
509 | |||
510 | 1 | static int swf_write_trailer(AVFormatContext *s) | |
511 | { | ||
512 | 1 | SWFEncContext *swf = s->priv_data; | |
513 | 1 | AVIOContext *pb = s->pb; | |
514 | int file_size; | ||
515 | |||
516 | 1 | put_swf_tag(s, TAG_END); | |
517 | 1 | put_swf_end_tag(s); | |
518 | |||
519 | /* patch file size and number of frames if not streamed */ | ||
520 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | if ((s->pb->seekable & AVIO_SEEKABLE_NORMAL) && swf->video_par) { |
521 | 1 | file_size = avio_tell(pb); | |
522 | 1 | avio_seek(pb, 4, SEEK_SET); | |
523 | 1 | avio_wl32(pb, file_size); | |
524 | 1 | avio_seek(pb, swf->duration_pos, SEEK_SET); | |
525 | 1 | avio_wl16(pb, swf->video_frame_number); | |
526 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (swf->vframes_pos) { |
527 | 1 | avio_seek(pb, swf->vframes_pos, SEEK_SET); | |
528 | 1 | avio_wl16(pb, swf->video_frame_number); | |
529 | } | ||
530 | 1 | avio_seek(pb, file_size, SEEK_SET); | |
531 | } | ||
532 | 1 | return 0; | |
533 | } | ||
534 | |||
535 | 1 | static void swf_deinit(AVFormatContext *s) | |
536 | { | ||
537 | 1 | SWFEncContext *swf = s->priv_data; | |
538 | |||
539 | 1 | av_fifo_freep2(&swf->audio_fifo); | |
540 | 1 | } | |
541 | |||
542 | #if CONFIG_SWF_MUXER | ||
543 | const FFOutputFormat ff_swf_muxer = { | ||
544 | .p.name = "swf", | ||
545 | .p.long_name = NULL_IF_CONFIG_SMALL("SWF (ShockWave Flash)"), | ||
546 | .p.mime_type = "application/x-shockwave-flash", | ||
547 | .p.extensions = "swf", | ||
548 | .priv_data_size = sizeof(SWFEncContext), | ||
549 | .p.audio_codec = AV_CODEC_ID_MP3, | ||
550 | .p.video_codec = AV_CODEC_ID_FLV1, | ||
551 | .p.subtitle_codec = AV_CODEC_ID_NONE, | ||
552 | .write_header = swf_write_header, | ||
553 | .write_packet = swf_write_packet, | ||
554 | .write_trailer = swf_write_trailer, | ||
555 | .deinit = swf_deinit, | ||
556 | .p.flags = AVFMT_TS_NONSTRICT, | ||
557 | .flags_internal = FF_OFMT_FLAG_MAX_ONE_OF_EACH, | ||
558 | }; | ||
559 | #endif | ||
560 | #if CONFIG_AVM2_MUXER | ||
561 | const FFOutputFormat ff_avm2_muxer = { | ||
562 | .p.name = "avm2", | ||
563 | .p.long_name = NULL_IF_CONFIG_SMALL("SWF (ShockWave Flash) (AVM2)"), | ||
564 | .p.mime_type = "application/x-shockwave-flash", | ||
565 | .priv_data_size = sizeof(SWFEncContext), | ||
566 | .p.audio_codec = AV_CODEC_ID_MP3, | ||
567 | .p.video_codec = AV_CODEC_ID_FLV1, | ||
568 | .p.subtitle_codec = AV_CODEC_ID_NONE, | ||
569 | .write_header = swf_write_header, | ||
570 | .write_packet = swf_write_packet, | ||
571 | .write_trailer = swf_write_trailer, | ||
572 | .deinit = swf_deinit, | ||
573 | .p.flags = AVFMT_TS_NONSTRICT, | ||
574 | .flags_internal = FF_OFMT_FLAG_MAX_ONE_OF_EACH, | ||
575 | }; | ||
576 | #endif | ||
577 |