Merge remote-tracking branch 'qatar/master'
[ffmpeg.git] / libavformat / ffmenc.c
index 858bd2f..522945e 100644 (file)
  */
 
 #include "libavutil/intreadwrite.h"
+#include "libavutil/intfloat.h"
+#include "libavutil/avassert.h"
+#include "libavutil/parseutils.h"
 #include "avformat.h"
+#include "internal.h"
 #include "ffm.h"
 
 static void flush_packet(AVFormatContext *s)
@@ -32,19 +36,18 @@ static void flush_packet(AVFormatContext *s)
     fill_size = ffm->packet_end - ffm->packet_ptr;
     memset(ffm->packet_ptr, 0, fill_size);
 
-    if (url_ftell(pb) % ffm->packet_size)
-        av_abort();
+    av_assert1(avio_tell(pb) % ffm->packet_size == 0);
 
     /* put header */
-    put_be16(pb, PACKET_ID);
-    put_be16(pb, fill_size);
-    put_be64(pb, ffm->dts);
+    avio_wb16(pb, PACKET_ID);
+    avio_wb16(pb, fill_size);
+    avio_wb64(pb, ffm->dts);
     h = ffm->frame_offset;
     if (ffm->first_packet)
         h |= 0x8000;
-    put_be16(pb, h);
-    put_buffer(pb, ffm->packet, ffm->packet_end - ffm->packet);
-    put_flush_packet(pb);
+    avio_wb16(pb, h);
+    avio_write(pb, ffm->packet, ffm->packet_end - ffm->packet);
+    avio_flush(pb);
 
     /* prepare next packet */
     ffm->frame_offset = 0; /* no key frame */
@@ -80,119 +83,145 @@ static void ffm_write_data(AVFormatContext *s,
     }
 }
 
+static void write_header_chunk(AVIOContext *pb, AVIOContext *dpb, unsigned id)
+{
+    uint8_t *dyn_buf;
+    int dyn_size= avio_close_dyn_buf(dpb, &dyn_buf);
+    avio_wb32(pb, id);
+    avio_wb32(pb, dyn_size);
+    avio_write(pb, dyn_buf, dyn_size);
+    av_free(dyn_buf);
+}
+
 static int ffm_write_header(AVFormatContext *s)
 {
     FFMContext *ffm = s->priv_data;
+    AVDictionaryEntry *t;
     AVStream *st;
     AVIOContext *pb = s->pb;
     AVCodecContext *codec;
     int bit_rate, i;
 
+    if (t = av_dict_get(s->metadata, "creation_time", NULL, 0)) {
+        int ret = av_parse_time(&ffm->start_time, t->value, 0);
+        if (ret < 0)
+            return ret;
+    }
+
     ffm->packet_size = FFM_PACKET_SIZE;
 
     /* header */
-    put_le32(pb, MKTAG('F', 'F', 'M', '1'));
-    put_be32(pb, ffm->packet_size);
-    put_be64(pb, 0); /* current write position */
+    avio_wl32(pb, MKTAG('F', 'F', 'M', '2'));
+    avio_wb32(pb, ffm->packet_size);
+    avio_wb64(pb, 0); /* current write position */
+
+    if(avio_open_dyn_buf(&pb) < 0)
+        return AVERROR(ENOMEM);
 
-    put_be32(pb, s->nb_streams);
+    avio_wb32(pb, s->nb_streams);
     bit_rate = 0;
     for(i=0;i<s->nb_streams;i++) {
         st = s->streams[i];
         bit_rate += st->codec->bit_rate;
     }
-    put_be32(pb, bit_rate);
+    avio_wb32(pb, bit_rate);
+
+    write_header_chunk(s->pb, pb, MKBETAG('M', 'A', 'I', 'N'));
 
     /* list of streams */
     for(i=0;i<s->nb_streams;i++) {
         st = s->streams[i];
-        av_set_pts_info(st, 64, 1, 1000000);
+        avpriv_set_pts_info(st, 64, 1, 1000000);
+        if(avio_open_dyn_buf(&pb) < 0)
+            return AVERROR(ENOMEM);
 
         codec = st->codec;
         /* generic info */
-        put_be32(pb, codec->codec_id);
-        put_byte(pb, codec->codec_type);
-        put_be32(pb, codec->bit_rate);
-        put_be32(pb, st->quality);
-        put_be32(pb, codec->flags);
-        put_be32(pb, codec->flags2);
-        put_be32(pb, codec->debug);
+        avio_wb32(pb, codec->codec_id);
+        avio_w8(pb, codec->codec_type);
+        avio_wb32(pb, codec->bit_rate);
+        avio_wb32(pb, codec->flags);
+        avio_wb32(pb, codec->flags2);
+        avio_wb32(pb, codec->debug);
+        if (codec->flags & CODEC_FLAG_GLOBAL_HEADER) {
+            avio_wb32(pb, codec->extradata_size);
+            avio_write(pb, codec->extradata, codec->extradata_size);
+        }
+        write_header_chunk(s->pb, pb, MKBETAG('C', 'O', 'M', 'M'));
+        if(avio_open_dyn_buf(&pb) < 0)
+            return AVERROR(ENOMEM);
         /* specific info */
         switch(codec->codec_type) {
         case AVMEDIA_TYPE_VIDEO:
-            put_be32(pb, codec->time_base.num);
-            put_be32(pb, codec->time_base.den);
-            put_be16(pb, codec->width);
-            put_be16(pb, codec->height);
-            put_be16(pb, codec->gop_size);
-            put_be32(pb, codec->pix_fmt);
-            put_byte(pb, codec->qmin);
-            put_byte(pb, codec->qmax);
-            put_byte(pb, codec->max_qdiff);
-            put_be16(pb, (int) (codec->qcompress * 10000.0));
-            put_be16(pb, (int) (codec->qblur * 10000.0));
-            put_be32(pb, codec->bit_rate_tolerance);
+            avio_wb32(pb, codec->time_base.num);
+            avio_wb32(pb, codec->time_base.den);
+            avio_wb16(pb, codec->width);
+            avio_wb16(pb, codec->height);
+            avio_wb16(pb, codec->gop_size);
+            avio_wb32(pb, codec->pix_fmt);
+            avio_w8(pb, codec->qmin);
+            avio_w8(pb, codec->qmax);
+            avio_w8(pb, codec->max_qdiff);
+            avio_wb16(pb, (int) (codec->qcompress * 10000.0));
+            avio_wb16(pb, (int) (codec->qblur * 10000.0));
+            avio_wb32(pb, codec->bit_rate_tolerance);
             avio_put_str(pb, codec->rc_eq ? codec->rc_eq : "tex^qComp");
-            put_be32(pb, codec->rc_max_rate);
-            put_be32(pb, codec->rc_min_rate);
-            put_be32(pb, codec->rc_buffer_size);
-            put_be64(pb, av_dbl2int(codec->i_quant_factor));
-            put_be64(pb, av_dbl2int(codec->b_quant_factor));
-            put_be64(pb, av_dbl2int(codec->i_quant_offset));
-            put_be64(pb, av_dbl2int(codec->b_quant_offset));
-            put_be32(pb, codec->dct_algo);
-            put_be32(pb, codec->strict_std_compliance);
-            put_be32(pb, codec->max_b_frames);
-            put_be32(pb, codec->luma_elim_threshold);
-            put_be32(pb, codec->chroma_elim_threshold);
-            put_be32(pb, codec->mpeg_quant);
-            put_be32(pb, codec->intra_dc_precision);
-            put_be32(pb, codec->me_method);
-            put_be32(pb, codec->mb_decision);
-            put_be32(pb, codec->nsse_weight);
-            put_be32(pb, codec->frame_skip_cmp);
-            put_be64(pb, av_dbl2int(codec->rc_buffer_aggressivity));
-            put_be32(pb, codec->codec_tag);
-            put_byte(pb, codec->thread_count);
-            put_be32(pb, codec->coder_type);
-            put_be32(pb, codec->me_cmp);
-            put_be32(pb, codec->partitions);
-            put_be32(pb, codec->me_subpel_quality);
-            put_be32(pb, codec->me_range);
-            put_be32(pb, codec->keyint_min);
-            put_be32(pb, codec->scenechange_threshold);
-            put_be32(pb, codec->b_frame_strategy);
-            put_be64(pb, av_dbl2int(codec->qcompress));
-            put_be64(pb, av_dbl2int(codec->qblur));
-            put_be32(pb, codec->max_qdiff);
-            put_be32(pb, codec->refs);
-            put_be32(pb, codec->directpred);
+            avio_wb32(pb, codec->rc_max_rate);
+            avio_wb32(pb, codec->rc_min_rate);
+            avio_wb32(pb, codec->rc_buffer_size);
+            avio_wb64(pb, av_double2int(codec->i_quant_factor));
+            avio_wb64(pb, av_double2int(codec->b_quant_factor));
+            avio_wb64(pb, av_double2int(codec->i_quant_offset));
+            avio_wb64(pb, av_double2int(codec->b_quant_offset));
+            avio_wb32(pb, codec->dct_algo);
+            avio_wb32(pb, codec->strict_std_compliance);
+            avio_wb32(pb, codec->max_b_frames);
+            avio_wb32(pb, codec->mpeg_quant);
+            avio_wb32(pb, codec->intra_dc_precision);
+            avio_wb32(pb, codec->me_method);
+            avio_wb32(pb, codec->mb_decision);
+            avio_wb32(pb, codec->nsse_weight);
+            avio_wb32(pb, codec->frame_skip_cmp);
+            avio_wb64(pb, av_double2int(codec->rc_buffer_aggressivity));
+            avio_wb32(pb, codec->codec_tag);
+            avio_w8(pb, codec->thread_count);
+            avio_wb32(pb, codec->coder_type);
+            avio_wb32(pb, codec->me_cmp);
+            avio_wb32(pb, codec->me_subpel_quality);
+            avio_wb32(pb, codec->me_range);
+            avio_wb32(pb, codec->keyint_min);
+            avio_wb32(pb, codec->scenechange_threshold);
+            avio_wb32(pb, codec->b_frame_strategy);
+            avio_wb64(pb, av_double2int(codec->qcompress));
+            avio_wb64(pb, av_double2int(codec->qblur));
+            avio_wb32(pb, codec->max_qdiff);
+            avio_wb32(pb, codec->refs);
+            write_header_chunk(s->pb, pb, MKBETAG('S', 'T', 'V', 'I'));
             break;
         case AVMEDIA_TYPE_AUDIO:
-            put_be32(pb, codec->sample_rate);
-            put_le16(pb, codec->channels);
-            put_le16(pb, codec->frame_size);
-            put_le16(pb, codec->sample_fmt);
+            avio_wb32(pb, codec->sample_rate);
+            avio_wl16(pb, codec->channels);
+            avio_wl16(pb, codec->frame_size);
+            write_header_chunk(s->pb, pb, MKBETAG('S', 'T', 'A', 'U'));
             break;
         default:
             return -1;
         }
-        if (codec->flags & CODEC_FLAG_GLOBAL_HEADER) {
-            put_be32(pb, codec->extradata_size);
-            put_buffer(pb, codec->extradata, codec->extradata_size);
-        }
     }
+    pb = s->pb;
+
+    avio_wb64(pb, 0); // end of header
 
     /* flush until end of block reached */
-    while ((url_ftell(pb) % ffm->packet_size) != 0)
-        put_byte(pb, 0);
+    while ((avio_tell(pb) % ffm->packet_size) != 0)
+        avio_w8(pb, 0);
 
-    put_flush_packet(pb);
+    avio_flush(pb);
 
     /* init packet mux */
     ffm->packet_ptr = ffm->packet;
     ffm->packet_end = ffm->packet + ffm->packet_size - FFM_HEADER_SIZE;
-    assert(ffm->packet_end >= ffm->packet);
+    av_assert0(ffm->packet_end >= ffm->packet);
     ffm->frame_offset = 0;
     ffm->dts = 0;
     ffm->first_packet = 1;
@@ -202,11 +231,12 @@ static int ffm_write_header(AVFormatContext *s)
 
 static int ffm_write_packet(AVFormatContext *s, AVPacket *pkt)
 {
+    FFMContext *ffm = s->priv_data;
     int64_t dts;
     uint8_t header[FRAME_HEADER_SIZE+4];
     int header_size = FRAME_HEADER_SIZE;
 
-    dts = s->timestamp + pkt->dts;
+    dts = ffm->start_time + pkt->dts;
     /* packet size & key_frame */
     header[0] = pkt->stream_index;
     header[1] = 0;
@@ -214,7 +244,7 @@ static int ffm_write_packet(AVFormatContext *s, AVPacket *pkt)
         header[1] |= FLAG_KEY_FRAME;
     AV_WB24(header+2, pkt->size);
     AV_WB24(header+5, pkt->duration);
-    AV_WB64(header+8, s->timestamp + pkt->pts);
+    AV_WB64(header+8, ffm->start_time + pkt->pts);
     if (pkt->pts != pkt->dts) {
         header[1] |= FLAG_DTS;
         AV_WB32(header+16, pkt->pts - pkt->dts);
@@ -228,28 +258,23 @@ static int ffm_write_packet(AVFormatContext *s, AVPacket *pkt)
 
 static int ffm_write_trailer(AVFormatContext *s)
 {
-    AVIOContext *pb = s->pb;
     FFMContext *ffm = s->priv_data;
 
     /* flush packets */
     if (ffm->packet_ptr > ffm->packet)
         flush_packet(s);
 
-    put_flush_packet(pb);
-
     return 0;
 }
 
 AVOutputFormat ff_ffm_muxer = {
-    "ffm",
-    NULL_IF_CONFIG_SMALL("FFM (FFserver live feed) format"),
-    "",
-    "ffm",
-    sizeof(FFMContext),
-    /* not really used */
-    CODEC_ID_MP2,
-    CODEC_ID_MPEG1VIDEO,
-    ffm_write_header,
-    ffm_write_packet,
-    ffm_write_trailer,
+    .name              = "ffm",
+    .long_name         = NULL_IF_CONFIG_SMALL("FFM (FFserver live feed)"),
+    .extensions        = "ffm",
+    .priv_data_size    = sizeof(FFMContext),
+    .audio_codec       = AV_CODEC_ID_MP2,
+    .video_codec       = AV_CODEC_ID_MPEG1VIDEO,
+    .write_header      = ffm_write_header,
+    .write_packet      = ffm_write_packet,
+    .write_trailer     = ffm_write_trailer,
 };