]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/swfenc.c
avformat/swfenc: add support for muxing png images
[ffmpeg] / libavformat / swfenc.c
index f53db0fb2b3ff41d93151fbb6afc70a74933b920..a0b761562921eea7e74d8d07ae3a8ed8193ba64a 100644 (file)
 
 #include "libavcodec/put_bits.h"
 #include "libavutil/avassert.h"
+#include "libavutil/fifo.h"
 #include "avformat.h"
 #include "swf.h"
 
+#define AUDIO_FIFO_SIZE 65536
+
+typedef struct SWFEncContext {
+    int64_t duration_pos;
+    int64_t tag_pos;
+    int64_t vframes_pos;
+    int samples_per_frame;
+    int sound_samples;
+    int swf_frame_number;
+    int video_frame_number;
+    int tag;
+    AVFifoBuffer *audio_fifo;
+    AVCodecParameters *audio_par, *video_par;
+    AVStream *video_st;
+} SWFEncContext;
+
 static void put_swf_tag(AVFormatContext *s, int tag)
 {
-    SWFContext *swf = s->priv_data;
+    SWFEncContext *swf = s->priv_data;
     AVIOContext *pb = s->pb;
 
     swf->tag_pos = avio_tell(pb);
@@ -43,7 +60,7 @@ static void put_swf_tag(AVFormatContext *s, int tag)
 
 static void put_swf_end_tag(AVFormatContext *s)
 {
-    SWFContext *swf = s->priv_data;
+    SWFEncContext *swf = s->priv_data;
     AVIOContext *pb = s->pb;
     int64_t pos;
     int tag_len, tag;
@@ -173,7 +190,7 @@ static void put_swf_matrix(AVIOContext *pb,
 
 static int swf_write_header(AVFormatContext *s)
 {
-    SWFContext *swf = s->priv_data;
+    SWFEncContext *swf = s->priv_data;
     AVIOContext *pb = s->pb;
     PutBitContext p;
     uint8_t buf1[256];
@@ -207,11 +224,12 @@ static int swf_write_header(AVFormatContext *s)
             }
             if (par->codec_id == AV_CODEC_ID_VP6F ||
                 par->codec_id == AV_CODEC_ID_FLV1 ||
+                par->codec_id == AV_CODEC_ID_PNG ||
                 par->codec_id == AV_CODEC_ID_MJPEG) {
                 swf->video_st  = s->streams[i];
                 swf->video_par = par;
             } else {
-                av_log(s, AV_LOG_ERROR, "SWF muxer only supports VP6, FLV1 and MJPEG\n");
+                av_log(s, AV_LOG_ERROR, "SWF muxer only supports VP6, FLV1, PNG and MJPEG\n");
                 return -1;
             }
         }
@@ -240,7 +258,7 @@ static int swf_write_header(AVFormatContext *s)
 
     if (!strcmp("avm2", s->oformat->name))
         version = 9;
-    else if (swf->video_par && swf->video_par->codec_id == AV_CODEC_ID_VP6F)
+    else if (swf->video_par && (swf->video_par->codec_id == AV_CODEC_ID_VP6F || swf->video_par->codec_id == AV_CODEC_ID_PNG))
         version = 8; /* version 8 and above support VP6 codec */
     else if (swf->video_par && swf->video_par->codec_id == AV_CODEC_ID_FLV1)
         version = 6; /* version 6 and above support FLV1 codec */
@@ -256,19 +274,19 @@ static int swf_write_header(AVFormatContext *s)
         av_log(s, AV_LOG_ERROR, "Invalid (too large) frame rate %d/%d\n", rate, rate_base);
         return AVERROR(EINVAL);
     }
-    avio_wl16(pb, (rate * 256) / rate_base); /* frame rate */
+    avio_wl16(pb, (rate * 256LL) / rate_base); /* frame rate */
     swf->duration_pos = avio_tell(pb);
     avio_wl16(pb, (uint16_t)(DUMMY_DURATION * (int64_t)rate / rate_base)); /* frame count */
 
-    /* avm2/swf v9 (also v8?) files require a file attribute tag */
-    if (version == 9) {
+    /* swf v8 and later files require a file attribute tag */
+    if (version >= 8) {
         put_swf_tag(s, TAG_FILEATTRIBUTES);
-        avio_wl32(pb, 1<<3); /* set ActionScript v3/AVM2 flag */
+        avio_wl32(pb, (version >= 9) << 3); /* set ActionScript v3/AVM2 flag */
         put_swf_end_tag(s);
     }
 
     /* define a shape with the jpeg inside */
-    if (swf->video_par && swf->video_par->codec_id == AV_CODEC_ID_MJPEG) {
+    if (swf->video_par && (swf->video_par->codec_id == AV_CODEC_ID_MJPEG || swf->video_par->codec_id == AV_CODEC_ID_PNG)) {
         put_swf_tag(s, TAG_DEFINESHAPE);
 
         avio_wl16(pb, SHAPE_ID); /* ID of shape */
@@ -337,14 +355,13 @@ static int swf_write_header(AVFormatContext *s)
         put_swf_end_tag(s);
     }
 
-    avio_flush(s->pb);
     return 0;
 }
 
 static int swf_write_video(AVFormatContext *s,
                            AVCodecParameters *par, const uint8_t *buf, int size)
 {
-    SWFContext *swf = s->priv_data;
+    SWFEncContext *swf = s->priv_data;
     AVIOContext *pb = s->pb;
 
     /* Flash Player limit */
@@ -390,7 +407,7 @@ static int swf_write_video(AVFormatContext *s,
         avio_wl16(pb, swf->video_frame_number++);
         avio_write(pb, buf, size);
         put_swf_end_tag(s);
-    } else if (par->codec_id == AV_CODEC_ID_MJPEG) {
+    } else if (par->codec_id == AV_CODEC_ID_MJPEG || par->codec_id == AV_CODEC_ID_PNG) {
         if (swf->swf_frame_number > 0) {
             /* remove the shape */
             put_swf_tag(s, TAG_REMOVEOBJECT);
@@ -409,8 +426,9 @@ static int swf_write_video(AVFormatContext *s,
         avio_wl16(pb, BITMAP_ID); /* ID of the image */
 
         /* a dummy jpeg header seems to be required */
-        avio_wb32(pb, 0xffd8ffd9);
-        /* write the jpeg image */
+        if (par->codec_id == AV_CODEC_ID_MJPEG)
+            avio_wb32(pb, 0xffd8ffd9);
+        /* write the jpeg/png image */
         avio_write(pb, buf, size);
 
         put_swf_end_tag(s);
@@ -449,7 +467,7 @@ static int swf_write_video(AVFormatContext *s,
 static int swf_write_audio(AVFormatContext *s,
                            AVCodecParameters *par, uint8_t *buf, int size)
 {
-    SWFContext *swf = s->priv_data;
+    SWFEncContext *swf = s->priv_data;
 
     /* Flash Player limit */
     if (swf->swf_frame_number == 16000)
@@ -481,26 +499,15 @@ static int swf_write_packet(AVFormatContext *s, AVPacket *pkt)
 
 static int swf_write_trailer(AVFormatContext *s)
 {
-    SWFContext *swf = s->priv_data;
+    SWFEncContext *swf = s->priv_data;
     AVIOContext *pb = s->pb;
-    AVCodecParameters *par, *video_par;
-    int file_size, i;
-
-    video_par = NULL;
-    for(i=0;i<s->nb_streams;i++) {
-        par = s->streams[i]->codecpar;
-        if (par->codec_type == AVMEDIA_TYPE_VIDEO)
-            video_par = par;
-        else {
-            av_fifo_freep(&swf->audio_fifo);
-        }
-    }
+    int file_size;
 
     put_swf_tag(s, TAG_END);
     put_swf_end_tag(s);
 
     /* patch file size and number of frames if not streamed */
-    if ((s->pb->seekable & AVIO_SEEKABLE_NORMAL) && video_par) {
+    if ((s->pb->seekable & AVIO_SEEKABLE_NORMAL) && swf->video_par) {
         file_size = avio_tell(pb);
         avio_seek(pb, 4, SEEK_SET);
         avio_wl32(pb, file_size);
@@ -515,18 +522,26 @@ static int swf_write_trailer(AVFormatContext *s)
     return 0;
 }
 
+static void swf_deinit(AVFormatContext *s)
+{
+    SWFEncContext *swf = s->priv_data;
+
+    av_fifo_freep(&swf->audio_fifo);
+}
+
 #if CONFIG_SWF_MUXER
 AVOutputFormat ff_swf_muxer = {
     .name              = "swf",
     .long_name         = NULL_IF_CONFIG_SMALL("SWF (ShockWave Flash)"),
     .mime_type         = "application/x-shockwave-flash",
     .extensions        = "swf",
-    .priv_data_size    = sizeof(SWFContext),
+    .priv_data_size    = sizeof(SWFEncContext),
     .audio_codec       = AV_CODEC_ID_MP3,
     .video_codec       = AV_CODEC_ID_FLV1,
     .write_header      = swf_write_header,
     .write_packet      = swf_write_packet,
     .write_trailer     = swf_write_trailer,
+    .deinit            = swf_deinit,
     .flags             = AVFMT_TS_NONSTRICT,
 };
 #endif
@@ -535,12 +550,13 @@ AVOutputFormat ff_avm2_muxer = {
     .name              = "avm2",
     .long_name         = NULL_IF_CONFIG_SMALL("SWF (ShockWave Flash) (AVM2)"),
     .mime_type         = "application/x-shockwave-flash",
-    .priv_data_size    = sizeof(SWFContext),
+    .priv_data_size    = sizeof(SWFEncContext),
     .audio_codec       = AV_CODEC_ID_MP3,
     .video_codec       = AV_CODEC_ID_FLV1,
     .write_header      = swf_write_header,
     .write_packet      = swf_write_packet,
     .write_trailer     = swf_write_trailer,
+    .deinit            = swf_deinit,
     .flags             = AVFMT_TS_NONSTRICT,
 };
 #endif