Mercurial > libavformat.hg
annotate swf.c @ 1631:c47ab7c4a49a libavformat
Fix error message, it's package ref that can not be found, not track in this case
author | reimar |
---|---|
date | Sat, 13 Jan 2007 18:21:55 +0000 |
parents | bdb50e314f29 |
children | 59d2c40950ca |
rev | line source |
---|---|
0 | 1 /* |
2 * Flash Compatible Streaming Format | |
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 */ |
22 #include "avformat.h" | |
628 | 23 #include "bitstream.h" |
1305 | 24 #include "riff.h" /* for CodecTag */ |
0 | 25 |
26 /* should have a generic way to indicate probable size */ | |
27 #define DUMMY_FILE_SIZE (100 * 1024 * 1024) | |
28 #define DUMMY_DURATION 600 /* in seconds */ | |
29 | |
30 #define TAG_END 0 | |
31 #define TAG_SHOWFRAME 1 | |
32 #define TAG_DEFINESHAPE 2 | |
33 #define TAG_FREECHARACTER 3 | |
34 #define TAG_PLACEOBJECT 4 | |
35 #define TAG_REMOVEOBJECT 5 | |
807 | 36 #define TAG_STREAMHEAD 18 |
0 | 37 #define TAG_STREAMBLOCK 19 |
38 #define TAG_JPEG2 21 | |
359 | 39 #define TAG_PLACEOBJECT2 26 |
40 #define TAG_STREAMHEAD2 45 | |
887 | 41 #define TAG_VIDEOSTREAM 60 |
359 | 42 #define TAG_VIDEOFRAME 61 |
0 | 43 |
44 #define TAG_LONG 0x100 | |
45 | |
46 /* flags for shape definition */ | |
47 #define FLAG_MOVETO 0x01 | |
48 #define FLAG_SETFILL0 0x02 | |
49 #define FLAG_SETFILL1 0x04 | |
50 | |
359 | 51 #define AUDIO_FIFO_SIZE 65536 |
52 | |
0 | 53 /* character id used */ |
54 #define BITMAP_ID 0 | |
359 | 55 #define VIDEO_ID 0 |
0 | 56 #define SHAPE_ID 1 |
57 | |
516 | 58 #undef NDEBUG |
59 #include <assert.h> | |
359 | 60 |
0 | 61 typedef struct { |
359 | 62 |
0 | 63 offset_t duration_pos; |
64 offset_t tag_pos; | |
885 | 65 |
359 | 66 int samples_per_frame; |
67 int sound_samples; | |
68 int video_samples; | |
69 int swf_frame_number; | |
70 int video_frame_number; | |
71 int ms_per_frame; | |
72 int ch_id; | |
0 | 73 int tag; |
359 | 74 |
75 uint8_t *audio_fifo; | |
76 int audio_in_pos; | |
77 int audio_out_pos; | |
78 int audio_size; | |
79 | |
80 int video_type; | |
81 int audio_type; | |
0 | 82 } SWFContext; |
83 | |
1305 | 84 static const CodecTag swf_codec_tags[] = { |
85 {CODEC_ID_FLV1, 0x02}, | |
86 {CODEC_ID_VP6F, 0x04}, | |
87 {0, 0}, | |
88 }; | |
89 | |
359 | 90 static const int sSampleRates[3][4] = { |
91 {44100, 48000, 32000, 0}, | |
92 {22050, 24000, 16000, 0}, | |
93 {11025, 12000, 8000, 0}, | |
94 }; | |
95 | |
96 static const int sBitRates[2][3][15] = { | |
97 { { 0, 32, 64, 96,128,160,192,224,256,288,320,352,384,416,448}, | |
98 { 0, 32, 48, 56, 64, 80, 96,112,128,160,192,224,256,320,384}, | |
99 { 0, 32, 40, 48, 56, 64, 80, 96,112,128,160,192,224,256,320} | |
100 }, | |
101 { { 0, 32, 48, 56, 64, 80, 96,112,128,144,160,176,192,224,256}, | |
102 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96,112,128,144,160}, | |
103 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96,112,128,144,160} | |
104 }, | |
105 }; | |
106 | |
107 static const int sSamplesPerFrame[3][3] = | |
108 { | |
109 { 384, 1152, 1152 }, | |
110 { 384, 1152, 576 }, | |
111 { 384, 1152, 576 } | |
112 }; | |
113 | |
114 static const int sBitsPerSlot[3] = { | |
115 32, | |
116 8, | |
117 8 | |
118 }; | |
119 | |
120 static int swf_mp3_info(void *data, int *byteSize, int *samplesPerFrame, int *sampleRate, int *isMono ) | |
121 { | |
122 uint8_t *dataTmp = (uint8_t *)data; | |
123 uint32_t header = ( (uint32_t)dataTmp[0] << 24 ) | ( (uint32_t)dataTmp[1] << 16 ) | ( (uint32_t)dataTmp[2] << 8 ) | (uint32_t)dataTmp[3]; | |
124 int layerID = 3 - ((header >> 17) & 0x03); | |
125 int bitRateID = ((header >> 12) & 0x0f); | |
126 int sampleRateID = ((header >> 10) & 0x03); | |
127 int bitRate = 0; | |
128 int bitsPerSlot = sBitsPerSlot[layerID]; | |
129 int isPadded = ((header >> 9) & 0x01); | |
885 | 130 |
359 | 131 if ( (( header >> 21 ) & 0x7ff) != 0x7ff ) { |
132 return 0; | |
133 } | |
134 | |
135 *isMono = ((header >> 6) & 0x03) == 0x03; | |
136 | |
137 if ( (header >> 19 ) & 0x01 ) { | |
138 *sampleRate = sSampleRates[0][sampleRateID]; | |
139 bitRate = sBitRates[0][layerID][bitRateID] * 1000; | |
140 *samplesPerFrame = sSamplesPerFrame[0][layerID]; | |
141 } else { | |
142 if ( (header >> 20) & 0x01 ) { | |
143 *sampleRate = sSampleRates[1][sampleRateID]; | |
144 bitRate = sBitRates[1][layerID][bitRateID] * 1000; | |
145 *samplesPerFrame = sSamplesPerFrame[1][layerID]; | |
146 } else { | |
147 *sampleRate = sSampleRates[2][sampleRateID]; | |
148 bitRate = sBitRates[1][layerID][bitRateID] * 1000; | |
149 *samplesPerFrame = sSamplesPerFrame[2][layerID]; | |
150 } | |
151 } | |
152 | |
153 *byteSize = ( ( ( ( *samplesPerFrame * (bitRate / bitsPerSlot) ) / *sampleRate ) + isPadded ) ); | |
154 | |
155 return 1; | |
156 } | |
157 | |
858
66cc656ea404
Replace CONFIG_ENCODERS/CONFIG_DECODERS with CONFIG_MUXERS/CONFIG_DEMUXERS
diego
parents:
820
diff
changeset
|
158 #ifdef CONFIG_MUXERS |
0 | 159 static void put_swf_tag(AVFormatContext *s, int tag) |
160 { | |
161 SWFContext *swf = s->priv_data; | |
162 ByteIOContext *pb = &s->pb; | |
163 | |
164 swf->tag_pos = url_ftell(pb); | |
165 swf->tag = tag; | |
166 /* reserve some room for the tag */ | |
167 if (tag & TAG_LONG) { | |
168 put_le16(pb, 0); | |
169 put_le32(pb, 0); | |
170 } else { | |
171 put_le16(pb, 0); | |
172 } | |
173 } | |
174 | |
175 static void put_swf_end_tag(AVFormatContext *s) | |
176 { | |
177 SWFContext *swf = s->priv_data; | |
178 ByteIOContext *pb = &s->pb; | |
179 offset_t pos; | |
180 int tag_len, tag; | |
181 | |
182 pos = url_ftell(pb); | |
183 tag_len = pos - swf->tag_pos - 2; | |
184 tag = swf->tag; | |
185 url_fseek(pb, swf->tag_pos, SEEK_SET); | |
186 if (tag & TAG_LONG) { | |
187 tag &= ~TAG_LONG; | |
188 put_le16(pb, (tag << 6) | 0x3f); | |
189 put_le32(pb, tag_len - 4); | |
190 } else { | |
191 assert(tag_len < 0x3f); | |
192 put_le16(pb, (tag << 6) | tag_len); | |
193 } | |
194 url_fseek(pb, pos, SEEK_SET); | |
195 } | |
196 | |
197 static inline void max_nbits(int *nbits_ptr, int val) | |
198 { | |
199 int n; | |
200 | |
201 if (val == 0) | |
202 return; | |
203 val = abs(val); | |
204 n = 1; | |
205 while (val != 0) { | |
206 n++; | |
207 val >>= 1; | |
208 } | |
209 if (n > *nbits_ptr) | |
210 *nbits_ptr = n; | |
211 } | |
212 | |
885 | 213 static void put_swf_rect(ByteIOContext *pb, |
0 | 214 int xmin, int xmax, int ymin, int ymax) |
215 { | |
216 PutBitContext p; | |
65 | 217 uint8_t buf[256]; |
0 | 218 int nbits, mask; |
219 | |
276 | 220 init_put_bits(&p, buf, sizeof(buf)); |
885 | 221 |
0 | 222 nbits = 0; |
223 max_nbits(&nbits, xmin); | |
224 max_nbits(&nbits, xmax); | |
225 max_nbits(&nbits, ymin); | |
226 max_nbits(&nbits, ymax); | |
227 mask = (1 << nbits) - 1; | |
228 | |
229 /* rectangle info */ | |
230 put_bits(&p, 5, nbits); | |
231 put_bits(&p, nbits, xmin & mask); | |
232 put_bits(&p, nbits, xmax & mask); | |
233 put_bits(&p, nbits, ymin & mask); | |
234 put_bits(&p, nbits, ymax & mask); | |
885 | 235 |
0 | 236 flush_put_bits(&p); |
237 put_buffer(pb, buf, pbBufPtr(&p) - p.buf); | |
238 } | |
239 | |
240 static void put_swf_line_edge(PutBitContext *pb, int dx, int dy) | |
241 { | |
242 int nbits, mask; | |
243 | |
244 put_bits(pb, 1, 1); /* edge */ | |
245 put_bits(pb, 1, 1); /* line select */ | |
246 nbits = 2; | |
247 max_nbits(&nbits, dx); | |
248 max_nbits(&nbits, dy); | |
249 | |
250 mask = (1 << nbits) - 1; | |
251 put_bits(pb, 4, nbits - 2); /* 16 bits precision */ | |
252 if (dx == 0) { | |
885 | 253 put_bits(pb, 1, 0); |
254 put_bits(pb, 1, 1); | |
0 | 255 put_bits(pb, nbits, dy & mask); |
256 } else if (dy == 0) { | |
885 | 257 put_bits(pb, 1, 0); |
258 put_bits(pb, 1, 0); | |
0 | 259 put_bits(pb, nbits, dx & mask); |
260 } else { | |
885 | 261 put_bits(pb, 1, 1); |
0 | 262 put_bits(pb, nbits, dx & mask); |
263 put_bits(pb, nbits, dy & mask); | |
264 } | |
265 } | |
266 | |
267 #define FRAC_BITS 16 | |
268 | |
359 | 269 /* put matrix */ |
0 | 270 static void put_swf_matrix(ByteIOContext *pb, |
271 int a, int b, int c, int d, int tx, int ty) | |
272 { | |
273 PutBitContext p; | |
65 | 274 uint8_t buf[256]; |
359 | 275 int nbits; |
0 | 276 |
276 | 277 init_put_bits(&p, buf, sizeof(buf)); |
885 | 278 |
0 | 279 put_bits(&p, 1, 1); /* a, d present */ |
359 | 280 nbits = 1; |
281 max_nbits(&nbits, a); | |
282 max_nbits(&nbits, d); | |
283 put_bits(&p, 5, nbits); /* nb bits */ | |
284 put_bits(&p, nbits, a); | |
285 put_bits(&p, nbits, d); | |
885 | 286 |
0 | 287 put_bits(&p, 1, 1); /* b, c present */ |
359 | 288 nbits = 1; |
289 max_nbits(&nbits, c); | |
290 max_nbits(&nbits, b); | |
291 put_bits(&p, 5, nbits); /* nb bits */ | |
292 put_bits(&p, nbits, c); | |
293 put_bits(&p, nbits, b); | |
0 | 294 |
359 | 295 nbits = 1; |
296 max_nbits(&nbits, tx); | |
297 max_nbits(&nbits, ty); | |
298 put_bits(&p, 5, nbits); /* nb bits */ | |
299 put_bits(&p, nbits, tx); | |
300 put_bits(&p, nbits, ty); | |
0 | 301 |
302 flush_put_bits(&p); | |
303 put_buffer(pb, buf, pbBufPtr(&p) - p.buf); | |
304 } | |
305 | |
359 | 306 /* */ |
0 | 307 static int swf_write_header(AVFormatContext *s) |
308 { | |
1623 | 309 SWFContext *swf = s->priv_data; |
0 | 310 ByteIOContext *pb = &s->pb; |
311 AVCodecContext *enc, *audio_enc, *video_enc; | |
312 PutBitContext p; | |
65 | 313 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
|
314 int i, width, height, rate, rate_base; |
0 | 315 |
359 | 316 swf->ch_id = -1; |
317 swf->audio_in_pos = 0; | |
318 swf->audio_out_pos = 0; | |
319 swf->audio_size = 0; | |
320 swf->audio_fifo = av_malloc(AUDIO_FIFO_SIZE); | |
321 swf->sound_samples = 0; | |
322 swf->video_samples = 0; | |
323 swf->swf_frame_number = 0; | |
324 swf->video_frame_number = 0; | |
325 | |
0 | 326 video_enc = NULL; |
327 audio_enc = NULL; | |
328 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
|
329 enc = s->streams[i]->codec; |
0 | 330 if (enc->codec_type == CODEC_TYPE_AUDIO) |
331 audio_enc = enc; | |
359 | 332 else { |
1305 | 333 if ( enc->codec_id == CODEC_ID_VP6F || |
334 enc->codec_id == CODEC_ID_FLV1 || | |
335 enc->codec_id == CODEC_ID_MJPEG ) { | |
359 | 336 video_enc = enc; |
337 } else { | |
1305 | 338 av_log(enc, AV_LOG_ERROR, "SWF only supports VP6, FLV1 and MJPEG\n"); |
359 | 339 return -1; |
340 } | |
341 } | |
0 | 342 } |
343 | |
344 if (!video_enc) { | |
345 /* currenty, cannot work correctly if audio only */ | |
359 | 346 swf->video_type = 0; |
0 | 347 width = 320; |
348 height = 200; | |
85
25062c9b1f86
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
65
diff
changeset
|
349 rate = 10; |
25062c9b1f86
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
65
diff
changeset
|
350 rate_base= 1; |
0 | 351 } else { |
359 | 352 swf->video_type = video_enc->codec_id; |
0 | 353 width = video_enc->width; |
354 height = video_enc->height; | |
743 | 355 rate = video_enc->time_base.den; |
356 rate_base = video_enc->time_base.num; | |
0 | 357 } |
358 | |
359 | 359 if (!audio_enc ) { |
360 swf->audio_type = 0; | |
361 swf->samples_per_frame = ( 44100. * rate_base ) / rate; | |
362 } else { | |
363 swf->audio_type = audio_enc->codec_id; | |
364 swf->samples_per_frame = ( ( audio_enc->sample_rate ) * rate_base ) / rate; | |
365 } | |
366 | |
0 | 367 put_tag(pb, "FWS"); |
1305 | 368 if ( video_enc && video_enc->codec_id == CODEC_ID_VP6F ) { |
369 put_byte(pb, 8); /* version (version 8 and above support VP6 codec) */ | |
370 } else if ( video_enc && video_enc->codec_id == CODEC_ID_FLV1 ) { | |
359 | 371 put_byte(pb, 6); /* version (version 6 and above support FLV1 codec) */ |
372 } else { | |
373 put_byte(pb, 4); /* version (should use 4 for mpeg audio support) */ | |
374 } | |
885 | 375 put_le32(pb, DUMMY_FILE_SIZE); /* dummy size |
376 (will be patched if not streamed) */ | |
0 | 377 |
359 | 378 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
|
379 put_le16(pb, (rate * 256) / rate_base); /* frame rate */ |
0 | 380 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
|
381 put_le16(pb, (uint16_t)(DUMMY_DURATION * (int64_t)rate / rate_base)); /* frame count */ |
885 | 382 |
0 | 383 /* define a shape with the jpeg inside */ |
1305 | 384 if ( video_enc && (video_enc->codec_id == CODEC_ID_VP6F || |
385 video_enc->codec_id == CODEC_ID_FLV1 )) { | |
359 | 386 } else if ( video_enc && video_enc->codec_id == CODEC_ID_MJPEG ) { |
387 put_swf_tag(s, TAG_DEFINESHAPE); | |
0 | 388 |
359 | 389 put_le16(pb, SHAPE_ID); /* ID of shape */ |
390 /* bounding rectangle */ | |
391 put_swf_rect(pb, 0, width, 0, height); | |
392 /* style info */ | |
393 put_byte(pb, 1); /* one fill style */ | |
394 put_byte(pb, 0x41); /* clipped bitmap fill */ | |
395 put_le16(pb, BITMAP_ID); /* bitmap ID */ | |
396 /* position of the bitmap */ | |
885 | 397 put_swf_matrix(pb, (int)(1.0 * (1 << FRAC_BITS)), 0, |
359 | 398 0, (int)(1.0 * (1 << FRAC_BITS)), 0, 0); |
399 put_byte(pb, 0); /* no line style */ | |
885 | 400 |
359 | 401 /* shape drawing */ |
402 init_put_bits(&p, buf1, sizeof(buf1)); | |
403 put_bits(&p, 4, 1); /* one fill bit */ | |
404 put_bits(&p, 4, 0); /* zero line bit */ | |
885 | 405 |
359 | 406 put_bits(&p, 1, 0); /* not an edge */ |
407 put_bits(&p, 5, FLAG_MOVETO | FLAG_SETFILL0); | |
408 put_bits(&p, 5, 1); /* nbits */ | |
409 put_bits(&p, 1, 0); /* X */ | |
410 put_bits(&p, 1, 0); /* Y */ | |
411 put_bits(&p, 1, 1); /* set fill style 1 */ | |
885 | 412 |
359 | 413 /* draw the rectangle ! */ |
414 put_swf_line_edge(&p, width, 0); | |
415 put_swf_line_edge(&p, 0, height); | |
416 put_swf_line_edge(&p, -width, 0); | |
417 put_swf_line_edge(&p, 0, -height); | |
885 | 418 |
359 | 419 /* end of shape */ |
420 put_bits(&p, 1, 0); /* not an edge */ | |
421 put_bits(&p, 5, 0); | |
0 | 422 |
359 | 423 flush_put_bits(&p); |
424 put_buffer(pb, buf1, pbBufPtr(&p) - p.buf); | |
0 | 425 |
359 | 426 put_swf_end_tag(s); |
427 } | |
885 | 428 |
359 | 429 if (audio_enc && audio_enc->codec_id == CODEC_ID_MP3 ) { |
0 | 430 int v; |
431 | |
432 /* start sound */ | |
359 | 433 put_swf_tag(s, TAG_STREAMHEAD2); |
0 | 434 |
435 v = 0; | |
436 switch(audio_enc->sample_rate) { | |
437 case 11025: | |
438 v |= 1 << 2; | |
439 break; | |
440 case 22050: | |
441 v |= 2 << 2; | |
442 break; | |
443 case 44100: | |
444 v |= 3 << 2; | |
445 break; | |
446 default: | |
447 /* not supported */ | |
1519
f57c7614ff98
print error when unsupported sample rate is used with swf
bcoudurier
parents:
1417
diff
changeset
|
448 av_log(s, AV_LOG_ERROR, "swf doesnt support that sample rate, choose from (44100, 22050, 11025)\n"); |
359 | 449 av_free(swf->audio_fifo); |
0 | 450 return -1; |
451 } | |
359 | 452 v |= 0x02; /* 16 bit playback */ |
0 | 453 if (audio_enc->channels == 2) |
359 | 454 v |= 0x01; /* stereo playback */ |
455 put_byte(&s->pb, v); | |
0 | 456 v |= 0x20; /* mp3 compressed */ |
457 put_byte(&s->pb, v); | |
359 | 458 put_le16(&s->pb, swf->samples_per_frame); /* avg samples per frame */ |
459 put_le16(&s->pb, 0); | |
885 | 460 |
0 | 461 put_swf_end_tag(s); |
462 } | |
463 | |
464 put_flush_packet(&s->pb); | |
465 return 0; | |
466 } | |
467 | |
885 | 468 static int swf_write_video(AVFormatContext *s, |
241 | 469 AVCodecContext *enc, const uint8_t *buf, int size) |
0 | 470 { |
359 | 471 SWFContext *swf = s->priv_data; |
0 | 472 ByteIOContext *pb = &s->pb; |
359 | 473 int c = 0; |
474 int outSize = 0; | |
475 int outSamples = 0; | |
885 | 476 |
359 | 477 /* Flash Player limit */ |
360 | 478 if ( swf->swf_frame_number == 16000 ) { |
370
845f9de2c883
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
360
diff
changeset
|
479 av_log(enc, AV_LOG_INFO, "warning: Flash Player limit of 16000 frames reached\n"); |
359 | 480 } |
0 | 481 |
359 | 482 if ( swf->audio_type ) { |
483 /* Prescan audio data for this swf frame */ | |
484 retry_swf_audio_packet: | |
485 if ( ( swf->audio_size-outSize ) >= 4 ) { | |
486 int mp3FrameSize = 0; | |
487 int mp3SampleRate = 0; | |
488 int mp3IsMono = 0; | |
489 int mp3SamplesPerFrame = 0; | |
885 | 490 |
359 | 491 /* copy out mp3 header from ring buffer */ |
492 uint8_t header[4]; | |
493 for (c=0; c<4; c++) { | |
494 header[c] = swf->audio_fifo[(swf->audio_in_pos+outSize+c) % AUDIO_FIFO_SIZE]; | |
495 } | |
885 | 496 |
359 | 497 if ( swf_mp3_info(header,&mp3FrameSize,&mp3SamplesPerFrame,&mp3SampleRate,&mp3IsMono) ) { |
498 if ( ( swf->audio_size-outSize ) >= mp3FrameSize ) { | |
499 outSize += mp3FrameSize; | |
500 outSamples += mp3SamplesPerFrame; | |
501 if ( ( swf->sound_samples + outSamples + swf->samples_per_frame ) < swf->video_samples ) { | |
502 goto retry_swf_audio_packet; | |
503 } | |
504 } | |
505 } else { | |
506 /* invalid mp3 data, skip forward | |
885 | 507 we need to do this since the Flash Player |
359 | 508 does not like custom headers */ |
509 swf->audio_in_pos ++; | |
510 swf->audio_size --; | |
511 swf->audio_in_pos %= AUDIO_FIFO_SIZE; | |
512 goto retry_swf_audio_packet; | |
513 } | |
514 } | |
885 | 515 |
359 | 516 /* audio stream is behind video stream, bail */ |
517 if ( ( swf->sound_samples + outSamples + swf->samples_per_frame ) < swf->video_samples ) { | |
518 return 0; | |
519 } | |
0 | 520 } |
521 | |
1305 | 522 if ( swf->video_type == CODEC_ID_VP6F || |
523 swf->video_type == CODEC_ID_FLV1 ) { | |
359 | 524 if ( swf->video_frame_number == 0 ) { |
525 /* create a new video object */ | |
526 put_swf_tag(s, TAG_VIDEOSTREAM); | |
527 put_le16(pb, VIDEO_ID); | |
528 put_le16(pb, 15000 ); /* hard flash player limit */ | |
529 put_le16(pb, enc->width); | |
530 put_le16(pb, enc->height); | |
531 put_byte(pb, 0); | |
1305 | 532 put_byte(pb,codec_get_tag(swf_codec_tags,swf->video_type)); |
359 | 533 put_swf_end_tag(s); |
885 | 534 |
359 | 535 /* place the video object for the first time */ |
536 put_swf_tag(s, TAG_PLACEOBJECT2); | |
537 put_byte(pb, 0x36); | |
538 put_le16(pb, 1); | |
539 put_le16(pb, VIDEO_ID); | |
540 put_swf_matrix(pb, 1 << FRAC_BITS, 0, 0, 1 << FRAC_BITS, 0, 0); | |
541 put_le16(pb, swf->video_frame_number ); | |
542 put_byte(pb, 'v'); | |
543 put_byte(pb, 'i'); | |
544 put_byte(pb, 'd'); | |
545 put_byte(pb, 'e'); | |
546 put_byte(pb, 'o'); | |
547 put_byte(pb, 0x00); | |
548 put_swf_end_tag(s); | |
549 } else { | |
550 /* mark the character for update */ | |
551 put_swf_tag(s, TAG_PLACEOBJECT2); | |
552 put_byte(pb, 0x11); | |
553 put_le16(pb, 1); | |
554 put_le16(pb, swf->video_frame_number ); | |
555 put_swf_end_tag(s); | |
556 } | |
885 | 557 |
359 | 558 /* set video frame data */ |
559 put_swf_tag(s, TAG_VIDEOFRAME | TAG_LONG); | |
885 | 560 put_le16(pb, VIDEO_ID); |
359 | 561 put_le16(pb, swf->video_frame_number++ ); |
516 | 562 put_buffer(pb, buf, size); |
359 | 563 put_swf_end_tag(s); |
564 } else if ( swf->video_type == CODEC_ID_MJPEG ) { | |
565 if (swf->swf_frame_number > 0) { | |
566 /* remove the shape */ | |
567 put_swf_tag(s, TAG_REMOVEOBJECT); | |
568 put_le16(pb, SHAPE_ID); /* shape ID */ | |
569 put_le16(pb, 1); /* depth */ | |
570 put_swf_end_tag(s); | |
885 | 571 |
359 | 572 /* free the bitmap */ |
573 put_swf_tag(s, TAG_FREECHARACTER); | |
574 put_le16(pb, BITMAP_ID); | |
575 put_swf_end_tag(s); | |
576 } | |
885 | 577 |
359 | 578 put_swf_tag(s, TAG_JPEG2 | TAG_LONG); |
885 | 579 |
359 | 580 put_le16(pb, BITMAP_ID); /* ID of the image */ |
885 | 581 |
359 | 582 /* a dummy jpeg header seems to be required */ |
885 | 583 put_byte(pb, 0xff); |
359 | 584 put_byte(pb, 0xd8); |
585 put_byte(pb, 0xff); | |
586 put_byte(pb, 0xd9); | |
587 /* write the jpeg image */ | |
516 | 588 put_buffer(pb, buf, size); |
885 | 589 |
359 | 590 put_swf_end_tag(s); |
885 | 591 |
359 | 592 /* draw the shape */ |
885 | 593 |
359 | 594 put_swf_tag(s, TAG_PLACEOBJECT); |
595 put_le16(pb, SHAPE_ID); /* shape ID */ | |
596 put_le16(pb, 1); /* depth */ | |
597 put_swf_matrix(pb, 20 << FRAC_BITS, 0, 0, 20 << FRAC_BITS, 0, 0); | |
598 put_swf_end_tag(s); | |
599 } else { | |
600 /* invalid codec */ | |
601 } | |
885 | 602 |
359 | 603 swf->swf_frame_number ++; |
0 | 604 |
359 | 605 swf->video_samples += swf->samples_per_frame; |
0 | 606 |
359 | 607 /* streaming sound always should be placed just before showframe tags */ |
608 if ( outSize > 0 ) { | |
609 put_swf_tag(s, TAG_STREAMBLOCK | TAG_LONG); | |
610 put_le16(pb, outSamples); | |
611 put_le16(pb, 0); | |
612 for (c=0; c<outSize; c++) { | |
613 put_byte(pb,swf->audio_fifo[(swf->audio_in_pos+c) % AUDIO_FIFO_SIZE]); | |
614 } | |
615 put_swf_end_tag(s); | |
885 | 616 |
359 | 617 /* update FIFO */ |
618 swf->sound_samples += outSamples; | |
619 swf->audio_in_pos += outSize; | |
620 swf->audio_size -= outSize; | |
621 swf->audio_in_pos %= AUDIO_FIFO_SIZE; | |
622 } | |
623 | |
0 | 624 /* output the frame */ |
625 put_swf_tag(s, TAG_SHOWFRAME); | |
626 put_swf_end_tag(s); | |
885 | 627 |
0 | 628 put_flush_packet(&s->pb); |
885 | 629 |
0 | 630 return 0; |
631 } | |
632 | |
885 | 633 static int swf_write_audio(AVFormatContext *s, |
359 | 634 AVCodecContext *enc, const uint8_t *buf, int size) |
0 | 635 { |
359 | 636 SWFContext *swf = s->priv_data; |
637 int c = 0; | |
638 | |
639 /* Flash Player limit */ | |
360 | 640 if ( swf->swf_frame_number == 16000 ) { |
370
845f9de2c883
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
360
diff
changeset
|
641 av_log(enc, AV_LOG_INFO, "warning: Flash Player limit of 16000 frames reached\n"); |
359 | 642 } |
0 | 643 |
359 | 644 if (enc->codec_id == CODEC_ID_MP3 ) { |
645 for (c=0; c<size; c++) { | |
646 swf->audio_fifo[(swf->audio_out_pos+c)%AUDIO_FIFO_SIZE] = buf[c]; | |
647 } | |
648 swf->audio_size += size; | |
649 swf->audio_out_pos += size; | |
650 swf->audio_out_pos %= AUDIO_FIFO_SIZE; | |
651 } | |
0 | 652 |
359 | 653 /* if audio only stream make sure we add swf frames */ |
654 if ( swf->video_type == 0 ) { | |
655 swf_write_video(s, enc, 0, 0); | |
656 } | |
657 | |
0 | 658 return 0; |
659 } | |
660 | |
468 | 661 static int swf_write_packet(AVFormatContext *s, AVPacket *pkt) |
0 | 662 { |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
807
diff
changeset
|
663 AVCodecContext *codec = s->streams[pkt->stream_index]->codec; |
0 | 664 if (codec->codec_type == CODEC_TYPE_AUDIO) |
468 | 665 return swf_write_audio(s, codec, pkt->data, pkt->size); |
0 | 666 else |
468 | 667 return swf_write_video(s, codec, pkt->data, pkt->size); |
0 | 668 } |
669 | |
670 static int swf_write_trailer(AVFormatContext *s) | |
671 { | |
672 SWFContext *swf = s->priv_data; | |
673 ByteIOContext *pb = &s->pb; | |
674 AVCodecContext *enc, *video_enc; | |
675 int file_size, i; | |
676 | |
677 video_enc = NULL; | |
678 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
|
679 enc = s->streams[i]->codec; |
0 | 680 if (enc->codec_type == CODEC_TYPE_VIDEO) |
681 video_enc = enc; | |
682 } | |
683 | |
684 put_swf_tag(s, TAG_END); | |
685 put_swf_end_tag(s); | |
885 | 686 |
0 | 687 put_flush_packet(&s->pb); |
688 | |
689 /* patch file size and number of frames if not streamed */ | |
690 if (!url_is_streamed(&s->pb) && video_enc) { | |
691 file_size = url_ftell(pb); | |
692 url_fseek(pb, 4, SEEK_SET); | |
693 put_le32(pb, file_size); | |
694 url_fseek(pb, swf->duration_pos, SEEK_SET); | |
695 put_le16(pb, video_enc->frame_number); | |
696 } | |
885 | 697 |
359 | 698 av_free(swf->audio_fifo); |
699 | |
0 | 700 return 0; |
701 } | |
858
66cc656ea404
Replace CONFIG_ENCODERS/CONFIG_DECODERS with CONFIG_MUXERS/CONFIG_DEMUXERS
diego
parents:
820
diff
changeset
|
702 #endif //CONFIG_MUXERS |
0 | 703 |
359 | 704 /*********************************************/ |
705 /* Extract FLV encoded frame and MP3 from swf | |
706 Note that the detection of the real frame | |
707 is inaccurate at this point as it can be | |
885 | 708 quite tricky to determine, you almost certainly |
359 | 709 will get a bad audio/video sync */ |
0 | 710 |
711 static int get_swf_tag(ByteIOContext *pb, int *len_ptr) | |
712 { | |
713 int tag, len; | |
885 | 714 |
0 | 715 if (url_feof(pb)) |
716 return -1; | |
717 | |
718 tag = get_le16(pb); | |
719 len = tag & 0x3f; | |
720 tag = tag >> 6; | |
721 if (len == 0x3f) { | |
722 len = get_le32(pb); | |
723 } | |
806 | 724 // av_log(NULL, AV_LOG_DEBUG, "Tag: %d - Len: %d\n", tag, len); |
0 | 725 *len_ptr = len; |
726 return tag; | |
727 } | |
728 | |
729 | |
730 static int swf_probe(AVProbeData *p) | |
731 { | |
732 /* check file header */ | |
733 if (p->buf_size <= 16) | |
734 return 0; | |
806 | 735 if ((p->buf[0] == 'F' || p->buf[0] == 'C') && p->buf[1] == 'W' && |
0 | 736 p->buf[2] == 'S') |
737 return AVPROBE_SCORE_MAX; | |
738 else | |
739 return 0; | |
740 } | |
741 | |
742 static int swf_read_header(AVFormatContext *s, AVFormatParameters *ap) | |
743 { | |
359 | 744 SWFContext *swf = 0; |
0 | 745 ByteIOContext *pb = &s->pb; |
746 int nbits, len, frame_rate, tag, v; | |
359 | 747 offset_t firstTagOff; |
748 AVStream *ast = 0; | |
749 AVStream *vst = 0; | |
750 | |
751 swf = av_malloc(sizeof(SWFContext)); | |
752 if (!swf) | |
753 return -1; | |
754 s->priv_data = swf; | |
0 | 755 |
806 | 756 tag = get_be32(pb) & 0xffffff00; |
757 | |
758 if (tag == MKBETAG('C', 'W', 'S', 0)) | |
759 { | |
887 | 760 av_log(s, AV_LOG_ERROR, "Compressed SWF format not supported\n"); |
806 | 761 return AVERROR_IO; |
762 } | |
763 if (tag != MKBETAG('F', 'W', 'S', 0)) | |
482 | 764 return AVERROR_IO; |
0 | 765 get_le32(pb); |
766 /* skip rectangle size */ | |
767 nbits = get_byte(pb) >> 3; | |
768 len = (4 * nbits - 3 + 7) / 8; | |
769 url_fskip(pb, len); | |
770 frame_rate = get_le16(pb); | |
771 get_le16(pb); /* frame count */ | |
885 | 772 |
773 /* The Flash Player converts 8.8 frame rates | |
774 to milliseconds internally. Do the same to get | |
359 | 775 a correct framerate */ |
776 swf->ms_per_frame = ( 1000 * 256 ) / frame_rate; | |
777 swf->samples_per_frame = 0; | |
778 swf->ch_id = -1; | |
0 | 779 |
359 | 780 firstTagOff = url_ftell(pb); |
0 | 781 for(;;) { |
782 tag = get_swf_tag(pb, &len); | |
783 if (tag < 0) { | |
359 | 784 if ( ast || vst ) { |
785 if ( vst && ast ) { | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
807
diff
changeset
|
786 vst->codec->time_base.den = ast->codec->sample_rate / swf->samples_per_frame; |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
807
diff
changeset
|
787 vst->codec->time_base.num = 1; |
359 | 788 } |
789 break; | |
790 } | |
370
845f9de2c883
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
360
diff
changeset
|
791 av_log(s, AV_LOG_ERROR, "No media found in SWF\n"); |
482 | 792 return AVERROR_IO; |
0 | 793 } |
359 | 794 if ( tag == TAG_VIDEOSTREAM && !vst) { |
1305 | 795 int codec_id; |
359 | 796 swf->ch_id = get_le16(pb); |
797 get_le16(pb); | |
798 get_le16(pb); | |
799 get_le16(pb); | |
800 get_byte(pb); | |
801 /* Check for FLV1 */ | |
1305 | 802 codec_id = codec_get_id(swf_codec_tags, get_byte(pb)); |
803 if ( codec_id ) { | |
359 | 804 vst = av_new_stream(s, 0); |
462
b69898ffc92a
move time_base (pts_num/pts_den) from AVFormatContext -> AVStream
michael
parents:
370
diff
changeset
|
805 av_set_pts_info(vst, 24, 1, 1000); /* 24 bit pts in ms */ |
885 | 806 |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
807
diff
changeset
|
807 vst->codec->codec_type = CODEC_TYPE_VIDEO; |
1305 | 808 vst->codec->codec_id = codec_id; |
359 | 809 if ( swf->samples_per_frame ) { |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
807
diff
changeset
|
810 vst->codec->time_base.den = 1000. / swf->ms_per_frame; |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
807
diff
changeset
|
811 vst->codec->time_base.num = 1; |
359 | 812 } |
813 } | |
814 } else if ( ( tag == TAG_STREAMHEAD || tag == TAG_STREAMHEAD2 ) && !ast) { | |
0 | 815 /* streaming found */ |
816 get_byte(pb); | |
817 v = get_byte(pb); | |
359 | 818 swf->samples_per_frame = get_le16(pb); |
1619
b95cdbfc2d16
dont skip too many bytes if tag is TAG_STREAMHEAD2 and mp3
bcoudurier
parents:
1519
diff
changeset
|
819 if (len > 4) |
325
e1d4300bf783
SWF sanity check patch by (Glyn Kennington <glyn dot kennington at ox dot compsoc dot net>)
michael
parents:
277
diff
changeset
|
820 url_fskip(pb,len-4); |
0 | 821 /* if mp3 streaming found, OK */ |
822 if ((v & 0x20) != 0) { | |
359 | 823 ast = av_new_stream(s, 1); |
824 if (!ast) | |
0 | 825 return -ENOMEM; |
1412 | 826 av_set_pts_info(ast, 24, 1, 1000); /* 24 bit pts in ms */ |
5 | 827 |
1620 | 828 ast->codec->channels = 1 + (v&1); |
0 | 829 |
830 switch((v>> 2) & 0x03) { | |
831 case 1: | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
807
diff
changeset
|
832 ast->codec->sample_rate = 11025; |
0 | 833 break; |
834 case 2: | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
807
diff
changeset
|
835 ast->codec->sample_rate = 22050; |
0 | 836 break; |
837 case 3: | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
807
diff
changeset
|
838 ast->codec->sample_rate = 44100; |
0 | 839 break; |
840 default: | |
359 | 841 av_free(ast); |
482 | 842 return AVERROR_IO; |
0 | 843 } |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
807
diff
changeset
|
844 ast->codec->codec_type = CODEC_TYPE_AUDIO; |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
807
diff
changeset
|
845 ast->codec->codec_id = CODEC_ID_MP3; |
1417 | 846 ast->need_parsing = 1; |
0 | 847 } |
848 } else { | |
849 url_fskip(pb, len); | |
850 } | |
851 } | |
359 | 852 url_fseek(pb, firstTagOff, SEEK_SET); |
885 | 853 |
0 | 854 return 0; |
855 } | |
856 | |
857 static int swf_read_packet(AVFormatContext *s, AVPacket *pkt) | |
858 { | |
359 | 859 SWFContext *swf = s->priv_data; |
0 | 860 ByteIOContext *pb = &s->pb; |
359 | 861 AVStream *st = 0; |
862 int tag, len, i, frame; | |
885 | 863 |
0 | 864 for(;;) { |
865 tag = get_swf_tag(pb, &len); | |
885 | 866 if (tag < 0) |
482 | 867 return AVERROR_IO; |
359 | 868 if (tag == TAG_VIDEOFRAME) { |
869 for( i=0; i<s->nb_streams; i++ ) { | |
887 | 870 st = s->streams[i]; |
359 | 871 if (st->id == 0) { |
872 if ( get_le16(pb) == swf->ch_id ) { | |
873 frame = get_le16(pb); | |
775 | 874 av_get_packet(pb, pkt, len-4); |
359 | 875 pkt->pts = frame * swf->ms_per_frame; |
876 pkt->stream_index = st->index; | |
877 return pkt->size; | |
878 } else { | |
879 url_fskip(pb, len-2); | |
880 continue; | |
881 } | |
882 } | |
885 | 883 } |
359 | 884 url_fskip(pb, len); |
885 } else if (tag == TAG_STREAMBLOCK) { | |
886 for( i=0; i<s->nb_streams; i++ ) { | |
887 | 887 st = s->streams[i]; |
359 | 888 if (st->id == 1) { |
1417 | 889 url_fskip(pb, 4); |
890 av_get_packet(pb, pkt, len-4); | |
359 | 891 pkt->stream_index = st->index; |
892 return pkt->size; | |
893 } | |
894 } | |
895 url_fskip(pb, len); | |
0 | 896 } else { |
897 url_fskip(pb, len); | |
898 } | |
899 } | |
900 return 0; | |
901 } | |
902 | |
903 static int swf_read_close(AVFormatContext *s) | |
904 { | |
905 return 0; | |
906 } | |
907 | |
1169 | 908 #ifdef CONFIG_SWF_DEMUXER |
909 AVInputFormat swf_demuxer = { | |
0 | 910 "swf", |
911 "Flash format", | |
359 | 912 sizeof(SWFContext), |
0 | 913 swf_probe, |
914 swf_read_header, | |
915 swf_read_packet, | |
916 swf_read_close, | |
917 }; | |
1169 | 918 #endif |
919 #ifdef CONFIG_SWF_MUXER | |
920 AVOutputFormat swf_muxer = { | |
0 | 921 "swf", |
922 "Flash format", | |
923 "application/x-shockwave-flash", | |
924 "swf", | |
925 sizeof(SWFContext), | |
359 | 926 CODEC_ID_MP3, |
927 CODEC_ID_FLV1, | |
0 | 928 swf_write_header, |
929 swf_write_packet, | |
930 swf_write_trailer, | |
931 }; | |
1169 | 932 #endif |