2008-05-26 03:44:31 +00:00
|
|
|
/*
|
2011-07-26 19:56:56 +00:00
|
|
|
* FFM (avserver live feed) muxer
|
2009-01-19 15:46:40 +00:00
|
|
|
* Copyright (c) 2001 Fabrice Bellard
|
2008-05-26 03:44:31 +00:00
|
|
|
*
|
2011-03-18 17:35:10 +00:00
|
|
|
* This file is part of Libav.
|
2008-05-26 03:44:31 +00:00
|
|
|
*
|
2011-03-18 17:35:10 +00:00
|
|
|
* Libav is free software; you can redistribute it and/or
|
2008-05-26 03:44:31 +00:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
2011-03-18 17:35:10 +00:00
|
|
|
* Libav is distributed in the hope that it will be useful,
|
2008-05-26 03:44:31 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2011-03-18 17:35:10 +00:00
|
|
|
* License along with Libav; if not, write to the Free Software
|
2008-05-26 03:44:31 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
2012-08-08 21:57:12 +00:00
|
|
|
#include <assert.h>
|
|
|
|
|
2009-01-11 22:19:48 +00:00
|
|
|
#include "libavutil/intreadwrite.h"
|
2011-11-27 14:04:16 +00:00
|
|
|
#include "libavutil/intfloat.h"
|
2008-05-26 03:44:31 +00:00
|
|
|
#include "avformat.h"
|
2011-11-29 18:28:15 +00:00
|
|
|
#include "internal.h"
|
2008-05-26 03:44:31 +00:00
|
|
|
#include "ffm.h"
|
|
|
|
|
|
|
|
static void flush_packet(AVFormatContext *s)
|
|
|
|
{
|
|
|
|
FFMContext *ffm = s->priv_data;
|
|
|
|
int fill_size, h;
|
2011-02-20 10:04:12 +00:00
|
|
|
AVIOContext *pb = s->pb;
|
2008-05-26 03:44:31 +00:00
|
|
|
|
|
|
|
fill_size = ffm->packet_end - ffm->packet_ptr;
|
|
|
|
memset(ffm->packet_ptr, 0, fill_size);
|
|
|
|
|
2012-08-08 21:57:12 +00:00
|
|
|
assert(avio_tell(pb) % ffm->packet_size == 0);
|
2008-05-26 03:44:31 +00:00
|
|
|
|
|
|
|
/* put header */
|
2011-02-21 18:28:17 +00:00
|
|
|
avio_wb16(pb, PACKET_ID);
|
|
|
|
avio_wb16(pb, fill_size);
|
|
|
|
avio_wb64(pb, ffm->dts);
|
2008-05-26 03:44:31 +00:00
|
|
|
h = ffm->frame_offset;
|
|
|
|
if (ffm->first_packet)
|
|
|
|
h |= 0x8000;
|
2011-02-21 18:28:17 +00:00
|
|
|
avio_wb16(pb, h);
|
|
|
|
avio_write(pb, ffm->packet, ffm->packet_end - ffm->packet);
|
2011-03-14 19:39:06 +00:00
|
|
|
avio_flush(pb);
|
2008-05-26 03:44:31 +00:00
|
|
|
|
|
|
|
/* prepare next packet */
|
|
|
|
ffm->frame_offset = 0; /* no key frame */
|
|
|
|
ffm->packet_ptr = ffm->packet;
|
|
|
|
ffm->first_packet = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 'first' is true if first data of a frame */
|
|
|
|
static void ffm_write_data(AVFormatContext *s,
|
|
|
|
const uint8_t *buf, int size,
|
2008-06-22 08:53:44 +00:00
|
|
|
int64_t dts, int header)
|
2008-05-26 03:44:31 +00:00
|
|
|
{
|
|
|
|
FFMContext *ffm = s->priv_data;
|
|
|
|
int len;
|
|
|
|
|
2008-06-22 00:26:25 +00:00
|
|
|
if (header && ffm->frame_offset == 0) {
|
2008-05-26 03:44:31 +00:00
|
|
|
ffm->frame_offset = ffm->packet_ptr - ffm->packet + FFM_HEADER_SIZE;
|
2008-06-22 08:53:44 +00:00
|
|
|
ffm->dts = dts;
|
2008-06-22 00:26:25 +00:00
|
|
|
}
|
2008-05-26 03:44:31 +00:00
|
|
|
|
|
|
|
/* write as many packets as needed */
|
|
|
|
while (size > 0) {
|
|
|
|
len = ffm->packet_end - ffm->packet_ptr;
|
|
|
|
if (len > size)
|
|
|
|
len = size;
|
|
|
|
memcpy(ffm->packet_ptr, buf, len);
|
|
|
|
|
|
|
|
ffm->packet_ptr += len;
|
|
|
|
buf += len;
|
|
|
|
size -= len;
|
2008-06-22 08:44:58 +00:00
|
|
|
if (ffm->packet_ptr >= ffm->packet_end)
|
2008-05-26 03:44:31 +00:00
|
|
|
flush_packet(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ffm_write_header(AVFormatContext *s)
|
|
|
|
{
|
|
|
|
FFMContext *ffm = s->priv_data;
|
|
|
|
AVStream *st;
|
2011-02-20 10:04:12 +00:00
|
|
|
AVIOContext *pb = s->pb;
|
2008-05-26 03:44:31 +00:00
|
|
|
AVCodecContext *codec;
|
|
|
|
int bit_rate, i;
|
|
|
|
|
|
|
|
ffm->packet_size = FFM_PACKET_SIZE;
|
|
|
|
|
|
|
|
/* header */
|
2011-02-21 18:28:17 +00:00
|
|
|
avio_wl32(pb, MKTAG('F', 'F', 'M', '1'));
|
|
|
|
avio_wb32(pb, ffm->packet_size);
|
|
|
|
avio_wb64(pb, 0); /* current write position */
|
2008-05-26 03:44:31 +00:00
|
|
|
|
2011-02-21 18:28:17 +00:00
|
|
|
avio_wb32(pb, s->nb_streams);
|
2008-05-26 03:44:31 +00:00
|
|
|
bit_rate = 0;
|
|
|
|
for(i=0;i<s->nb_streams;i++) {
|
|
|
|
st = s->streams[i];
|
|
|
|
bit_rate += st->codec->bit_rate;
|
|
|
|
}
|
2011-02-21 18:28:17 +00:00
|
|
|
avio_wb32(pb, bit_rate);
|
2008-05-26 03:44:31 +00:00
|
|
|
|
|
|
|
/* list of streams */
|
|
|
|
for(i=0;i<s->nb_streams;i++) {
|
|
|
|
st = s->streams[i];
|
2011-11-29 18:28:15 +00:00
|
|
|
avpriv_set_pts_info(st, 64, 1, 1000000);
|
2008-05-26 03:44:31 +00:00
|
|
|
|
|
|
|
codec = st->codec;
|
|
|
|
/* generic info */
|
2011-02-21 18:28:17 +00:00
|
|
|
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);
|
2008-05-26 03:44:31 +00:00
|
|
|
/* specific info */
|
|
|
|
switch(codec->codec_type) {
|
2010-03-30 23:30:55 +00:00
|
|
|
case AVMEDIA_TYPE_VIDEO:
|
2011-02-21 18:28:17 +00:00
|
|
|
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);
|
2011-01-21 19:18:07 +00:00
|
|
|
avio_put_str(pb, codec->rc_eq ? codec->rc_eq : "tex^qComp");
|
2011-02-21 18:28:17 +00:00
|
|
|
avio_wb32(pb, codec->rc_max_rate);
|
|
|
|
avio_wb32(pb, codec->rc_min_rate);
|
|
|
|
avio_wb32(pb, codec->rc_buffer_size);
|
2011-11-27 14:04:16 +00:00
|
|
|
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));
|
2011-02-21 18:28:17 +00:00
|
|
|
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);
|
2011-11-27 14:04:16 +00:00
|
|
|
avio_wb64(pb, av_double2int(codec->rc_buffer_aggressivity));
|
2011-02-21 18:28:17 +00:00
|
|
|
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);
|
2011-11-27 14:04:16 +00:00
|
|
|
avio_wb64(pb, av_double2int(codec->qcompress));
|
|
|
|
avio_wb64(pb, av_double2int(codec->qblur));
|
2011-02-21 18:28:17 +00:00
|
|
|
avio_wb32(pb, codec->max_qdiff);
|
|
|
|
avio_wb32(pb, codec->refs);
|
2008-05-26 03:44:31 +00:00
|
|
|
break;
|
2010-03-30 23:30:55 +00:00
|
|
|
case AVMEDIA_TYPE_AUDIO:
|
2011-02-21 18:28:17 +00:00
|
|
|
avio_wb32(pb, codec->sample_rate);
|
|
|
|
avio_wl16(pb, codec->channels);
|
|
|
|
avio_wl16(pb, codec->frame_size);
|
2008-05-26 03:44:31 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
2008-06-09 00:36:27 +00:00
|
|
|
if (codec->flags & CODEC_FLAG_GLOBAL_HEADER) {
|
2011-02-21 18:28:17 +00:00
|
|
|
avio_wb32(pb, codec->extradata_size);
|
|
|
|
avio_write(pb, codec->extradata, codec->extradata_size);
|
2008-06-09 00:36:27 +00:00
|
|
|
}
|
2008-05-26 03:44:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* flush until end of block reached */
|
2011-03-03 19:11:45 +00:00
|
|
|
while ((avio_tell(pb) % ffm->packet_size) != 0)
|
2011-02-21 18:28:17 +00:00
|
|
|
avio_w8(pb, 0);
|
2008-05-26 03:44:31 +00:00
|
|
|
|
2011-03-14 19:39:06 +00:00
|
|
|
avio_flush(pb);
|
2008-05-26 03:44:31 +00:00
|
|
|
|
|
|
|
/* 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);
|
|
|
|
ffm->frame_offset = 0;
|
2008-06-22 08:53:44 +00:00
|
|
|
ffm->dts = 0;
|
2008-05-26 03:44:31 +00:00
|
|
|
ffm->first_packet = 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ffm_write_packet(AVFormatContext *s, AVPacket *pkt)
|
|
|
|
{
|
2008-06-22 08:50:57 +00:00
|
|
|
int64_t dts;
|
2008-06-27 21:42:08 +00:00
|
|
|
uint8_t header[FRAME_HEADER_SIZE+4];
|
2008-06-09 07:43:14 +00:00
|
|
|
int header_size = FRAME_HEADER_SIZE;
|
2008-05-26 03:44:31 +00:00
|
|
|
|
2012-01-12 08:38:13 +00:00
|
|
|
dts = pkt->dts;
|
2008-05-26 03:44:31 +00:00
|
|
|
/* packet size & key_frame */
|
|
|
|
header[0] = pkt->stream_index;
|
|
|
|
header[1] = 0;
|
2010-03-31 12:29:58 +00:00
|
|
|
if (pkt->flags & AV_PKT_FLAG_KEY)
|
2008-05-26 03:44:31 +00:00
|
|
|
header[1] |= FLAG_KEY_FRAME;
|
|
|
|
AV_WB24(header+2, pkt->size);
|
|
|
|
AV_WB24(header+5, pkt->duration);
|
2012-01-12 08:38:13 +00:00
|
|
|
AV_WB64(header+8, pkt->pts);
|
2008-06-09 07:43:14 +00:00
|
|
|
if (pkt->pts != pkt->dts) {
|
|
|
|
header[1] |= FLAG_DTS;
|
|
|
|
AV_WB32(header+16, pkt->pts - pkt->dts);
|
|
|
|
header_size += 4;
|
|
|
|
}
|
2008-06-22 08:50:57 +00:00
|
|
|
ffm_write_data(s, header, header_size, dts, 1);
|
|
|
|
ffm_write_data(s, pkt->data, pkt->size, dts, 0);
|
2008-05-26 03:44:31 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ffm_write_trailer(AVFormatContext *s)
|
|
|
|
{
|
|
|
|
FFMContext *ffm = s->priv_data;
|
|
|
|
|
|
|
|
/* flush packets */
|
|
|
|
if (ffm->packet_ptr > ffm->packet)
|
|
|
|
flush_packet(s);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-01-25 22:03:28 +00:00
|
|
|
AVOutputFormat ff_ffm_muxer = {
|
2011-07-16 20:18:12 +00:00
|
|
|
.name = "ffm",
|
2012-07-24 21:51:41 +00:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("FFM (AVserver live feed)"),
|
2011-07-16 20:18:12 +00:00
|
|
|
.mime_type = "",
|
|
|
|
.extensions = "ffm",
|
|
|
|
.priv_data_size = sizeof(FFMContext),
|
2012-08-05 09:11:04 +00:00
|
|
|
.audio_codec = AV_CODEC_ID_MP2,
|
|
|
|
.video_codec = AV_CODEC_ID_MPEG1VIDEO,
|
2011-07-16 20:18:12 +00:00
|
|
|
.write_header = ffm_write_header,
|
|
|
|
.write_packet = ffm_write_packet,
|
|
|
|
.write_trailer = ffm_write_trailer,
|
2008-05-26 03:44:31 +00:00
|
|
|
};
|