X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavformat%2Fswfenc.c;h=193663f61e2a7dac7015ba7b4501090d77a1b765;hb=5b8cc860a1ba0096287d33eb5e015babd6f5dcdd;hp=33e4ca7a5c249ced7859763c9c1c08777f273e4a;hpb=58c37c30b4e23c3bac2d85ffd448a3192fa2fc1e;p=ffmpeg diff --git a/libavformat/swfenc.c b/libavformat/swfenc.c index 33e4ca7a5c2..193663f61e2 100644 --- a/libavformat/swfenc.c +++ b/libavformat/swfenc.c @@ -174,28 +174,25 @@ static int swf_write_header(AVFormatContext *s) { SWFContext *swf = s->priv_data; ByteIOContext *pb = s->pb; - AVCodecContext *enc, *audio_enc, *video_enc; PutBitContext p; uint8_t buf1[256]; int i, width, height, rate, rate_base; - int is_avm2; + int version; - swf->audio_in_pos = 0; swf->sound_samples = 0; swf->swf_frame_number = 0; swf->video_frame_number = 0; - video_enc = NULL; - audio_enc = NULL; for(i=0;inb_streams;i++) { - enc = s->streams[i]->codec; + AVCodecContext *enc = s->streams[i]->codec; if (enc->codec_type == CODEC_TYPE_AUDIO) { if (enc->codec_id == CODEC_ID_MP3) { if (!enc->frame_size) { av_log(s, AV_LOG_ERROR, "audio frame size not set\n"); return -1; } - audio_enc = enc; + swf->audio_enc = enc; + av_fifo_init(&swf->audio_fifo, AUDIO_FIFO_SIZE); } else { av_log(s, AV_LOG_ERROR, "SWF muxer only supports MP3\n"); return -1; @@ -204,7 +201,7 @@ static int swf_write_header(AVFormatContext *s) if (enc->codec_id == CODEC_ID_VP6F || enc->codec_id == CODEC_ID_FLV1 || enc->codec_id == CODEC_ID_MJPEG) { - video_enc = enc; + swf->video_enc = enc; } else { av_log(s, AV_LOG_ERROR, "SWF muxer only supports VP6, FLV1 and MJPEG\n"); return -1; @@ -212,41 +209,36 @@ static int swf_write_header(AVFormatContext *s) } } - if (!video_enc) { + if (!swf->video_enc) { /* currently, cannot work correctly if audio only */ - swf->video_type = 0; width = 320; height = 200; rate = 10; rate_base= 1; } else { - swf->video_type = video_enc->codec_id; - width = video_enc->width; - height = video_enc->height; - rate = video_enc->time_base.den; - rate_base = video_enc->time_base.num; + width = swf->video_enc->width; + height = swf->video_enc->height; + rate = swf->video_enc->time_base.den; + rate_base = swf->video_enc->time_base.num; } - if (!audio_enc) { - swf->audio_type = 0; + if (!swf->audio_enc) swf->samples_per_frame = (44100. * rate_base) / rate; - } else { - swf->audio_type = audio_enc->codec_id; - swf->samples_per_frame = (audio_enc->sample_rate * rate_base) / rate; - } - - is_avm2 = !strcmp("avm2", s->oformat->name); + else + swf->samples_per_frame = (swf->audio_enc->sample_rate * rate_base) / rate; put_tag(pb, "FWS"); - if (is_avm2) { - put_byte(pb, 9); - } else if (video_enc && video_enc->codec_id == CODEC_ID_VP6F) { - put_byte(pb, 8); /* version (version 8 and above support VP6 codec) */ - } else if (video_enc && video_enc->codec_id == CODEC_ID_FLV1) { - put_byte(pb, 6); /* version (version 6 and above support FLV1 codec) */ - } else { - put_byte(pb, 4); /* version (should use 4 for mpeg audio support) */ - } + + if (!strcmp("avm2", s->oformat->name)) + version = 9; + else if (swf->video_enc && swf->video_enc->codec_id == CODEC_ID_VP6F) + version = 8; /* version 8 and above support VP6 codec */ + else if (swf->video_enc && swf->video_enc->codec_id == CODEC_ID_FLV1) + version = 6; /* version 6 and above support FLV1 codec */ + else + version = 4; /* version 4 for mpeg audio support */ + put_byte(pb, version); + put_le32(pb, DUMMY_FILE_SIZE); /* dummy size (will be patched if not streamed) */ @@ -256,16 +248,14 @@ static int swf_write_header(AVFormatContext *s) put_le16(pb, (uint16_t)(DUMMY_DURATION * (int64_t)rate / rate_base)); /* frame count */ /* avm2/swf v9 (also v8?) files require a file attribute tag */ - if (is_avm2) { + if (version == 9) { put_swf_tag(s, TAG_FILEATTRIBUTES); put_le32(pb, 1<<3); /* set ActionScript v3/AVM2 flag */ put_swf_end_tag(s); } /* define a shape with the jpeg inside */ - if (video_enc && (video_enc->codec_id == CODEC_ID_VP6F || - video_enc->codec_id == CODEC_ID_FLV1)) { - } else if (video_enc && video_enc->codec_id == CODEC_ID_MJPEG) { + if (swf->video_enc && swf->video_enc->codec_id == CODEC_ID_MJPEG) { put_swf_tag(s, TAG_DEFINESHAPE); put_le16(pb, SHAPE_ID); /* ID of shape */ @@ -308,30 +298,22 @@ static int swf_write_header(AVFormatContext *s) put_swf_end_tag(s); } - if (audio_enc && audio_enc->codec_id == CODEC_ID_MP3) { - int v; + if (swf->audio_enc && swf->audio_enc->codec_id == CODEC_ID_MP3) { + int v = 0; /* start sound */ put_swf_tag(s, TAG_STREAMHEAD2); - - v = 0; - switch(audio_enc->sample_rate) { - case 11025: - v |= 1 << 2; - break; - case 22050: - v |= 2 << 2; - break; - case 44100: - v |= 3 << 2; - break; + switch(swf->audio_enc->sample_rate) { + case 11025: v |= 1 << 2; break; + case 22050: v |= 2 << 2; break; + case 44100: v |= 3 << 2; break; default: /* not supported */ av_log(s, AV_LOG_ERROR, "swf does not support that sample rate, choose from (44100, 22050, 11025).\n"); return -1; } v |= 0x02; /* 16 bit playback */ - if (audio_enc->channels == 2) + if (swf->audio_enc->channels == 2) v |= 0x01; /* stereo playback */ put_byte(s->pb, v); v |= 0x20; /* mp3 compressed */ @@ -353,12 +335,11 @@ static int swf_write_video(AVFormatContext *s, ByteIOContext *pb = s->pb; /* Flash Player limit */ - if (swf->swf_frame_number == 16000) { + if (swf->swf_frame_number == 16000) av_log(enc, AV_LOG_INFO, "warning: Flash Player limit of 16000 frames reached\n"); - } - if (swf->video_type == CODEC_ID_VP6F || - swf->video_type == CODEC_ID_FLV1) { + if (enc->codec_id == CODEC_ID_VP6F || + enc->codec_id == CODEC_ID_FLV1) { if (swf->video_frame_number == 0) { /* create a new video object */ put_swf_tag(s, TAG_VIDEOSTREAM); @@ -367,7 +348,7 @@ static int swf_write_video(AVFormatContext *s, put_le16(pb, enc->width); put_le16(pb, enc->height); put_byte(pb, 0); - put_byte(pb,codec_get_tag(swf_codec_tags,swf->video_type)); + put_byte(pb,codec_get_tag(swf_codec_tags,enc->codec_id)); put_swf_end_tag(s); /* place the video object for the first time */ @@ -377,11 +358,7 @@ static int swf_write_video(AVFormatContext *s, put_le16(pb, VIDEO_ID); put_swf_matrix(pb, 1 << FRAC_BITS, 0, 0, 1 << FRAC_BITS, 0, 0); put_le16(pb, swf->video_frame_number); - put_byte(pb, 'v'); - put_byte(pb, 'i'); - put_byte(pb, 'd'); - put_byte(pb, 'e'); - put_byte(pb, 'o'); + put_tag(pb, "video"); put_byte(pb, 0x00); put_swf_end_tag(s); } else { @@ -399,7 +376,7 @@ static int swf_write_video(AVFormatContext *s, put_le16(pb, swf->video_frame_number++); put_buffer(pb, buf, size); put_swf_end_tag(s); - } else if (swf->video_type == CODEC_ID_MJPEG) { + } else if (enc->codec_id == CODEC_ID_MJPEG) { if (swf->swf_frame_number > 0) { /* remove the shape */ put_swf_tag(s, TAG_REMOVEOBJECT); @@ -418,10 +395,7 @@ static int swf_write_video(AVFormatContext *s, put_le16(pb, BITMAP_ID); /* ID of the image */ /* a dummy jpeg header seems to be required */ - put_byte(pb, 0xff); - put_byte(pb, 0xd8); - put_byte(pb, 0xff); - put_byte(pb, 0xd9); + put_be32(pb, 0xffd8ffd9); /* write the jpeg image */ put_buffer(pb, buf, size); @@ -434,23 +408,21 @@ static int swf_write_video(AVFormatContext *s, put_le16(pb, 1); /* depth */ put_swf_matrix(pb, 20 << FRAC_BITS, 0, 0, 20 << FRAC_BITS, 0, 0); put_swf_end_tag(s); - } else { - /* invalid codec */ } - swf->swf_frame_number ++; + swf->swf_frame_number++; /* streaming sound always should be placed just before showframe tags */ - if (swf->audio_type && swf->audio_in_pos) { + if (swf->audio_enc && av_fifo_size(&swf->audio_fifo)) { + int frame_size = av_fifo_size(&swf->audio_fifo); put_swf_tag(s, TAG_STREAMBLOCK | TAG_LONG); put_le16(pb, swf->sound_samples); put_le16(pb, 0); // seek samples - put_buffer(pb, swf->audio_fifo, swf->audio_in_pos); + av_fifo_generic_read(&swf->audio_fifo, frame_size, &put_buffer, pb); put_swf_end_tag(s); /* update FIFO */ swf->sound_samples = 0; - swf->audio_in_pos = 0; } /* output the frame */ @@ -468,23 +440,20 @@ static int swf_write_audio(AVFormatContext *s, SWFContext *swf = s->priv_data; /* Flash Player limit */ - if (swf->swf_frame_number == 16000) { + if (swf->swf_frame_number == 16000) av_log(enc, AV_LOG_INFO, "warning: Flash Player limit of 16000 frames reached\n"); - } - if (swf->audio_in_pos + size >= AUDIO_FIFO_SIZE) { + if (av_fifo_size(&swf->audio_fifo) + size > AUDIO_FIFO_SIZE) { av_log(s, AV_LOG_ERROR, "audio fifo too small to mux audio essence\n"); return -1; } - memcpy(swf->audio_fifo + swf->audio_in_pos, buf, size); - swf->audio_in_pos += size; + av_fifo_generic_write(&swf->audio_fifo, buf, size, NULL); swf->sound_samples += enc->frame_size; /* if audio only stream make sure we add swf frames */ - if (swf->video_type == 0) { + if (!swf->video_enc) swf_write_video(s, enc, 0, 0); - } return 0; } @@ -510,6 +479,8 @@ static int swf_write_trailer(AVFormatContext *s) enc = s->streams[i]->codec; if (enc->codec_type == CODEC_TYPE_VIDEO) video_enc = enc; + else + av_fifo_free(&swf->audio_fifo); } put_swf_tag(s, TAG_END); @@ -523,7 +494,7 @@ static int swf_write_trailer(AVFormatContext *s) url_fseek(pb, 4, SEEK_SET); put_le32(pb, file_size); url_fseek(pb, swf->duration_pos, SEEK_SET); - put_le16(pb, video_enc->frame_number); + put_le16(pb, swf->video_frame_number); url_fseek(pb, file_size, SEEK_SET); } return 0; @@ -532,7 +503,7 @@ static int swf_write_trailer(AVFormatContext *s) #ifdef CONFIG_SWF_MUXER AVOutputFormat swf_muxer = { "swf", - "Flash format", + NULL_IF_CONFIG_SMALL("Flash format"), "application/x-shockwave-flash", "swf", sizeof(SWFContext), @@ -546,7 +517,7 @@ AVOutputFormat swf_muxer = { #ifdef CONFIG_AVM2_MUXER AVOutputFormat avm2_muxer = { "avm2", - "Flash 9 (AVM2) format", + NULL_IF_CONFIG_SMALL("Flash 9 (AVM2) format"), "application/x-shockwave-flash", NULL, sizeof(SWFContext),