00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavutil/intreadwrite.h"
00023 #include "libavutil/intfloat.h"
00024 #include "libavutil/avassert.h"
00025 #include "avformat.h"
00026 #include "internal.h"
00027 #include "ffm.h"
00028
00029 static void flush_packet(AVFormatContext *s)
00030 {
00031 FFMContext *ffm = s->priv_data;
00032 int fill_size, h;
00033 AVIOContext *pb = s->pb;
00034
00035 fill_size = ffm->packet_end - ffm->packet_ptr;
00036 memset(ffm->packet_ptr, 0, fill_size);
00037
00038 av_assert1(avio_tell(pb) % ffm->packet_size == 0);
00039
00040
00041 avio_wb16(pb, PACKET_ID);
00042 avio_wb16(pb, fill_size);
00043 avio_wb64(pb, ffm->dts);
00044 h = ffm->frame_offset;
00045 if (ffm->first_packet)
00046 h |= 0x8000;
00047 avio_wb16(pb, h);
00048 avio_write(pb, ffm->packet, ffm->packet_end - ffm->packet);
00049 avio_flush(pb);
00050
00051
00052 ffm->frame_offset = 0;
00053 ffm->packet_ptr = ffm->packet;
00054 ffm->first_packet = 0;
00055 }
00056
00057
00058 static void ffm_write_data(AVFormatContext *s,
00059 const uint8_t *buf, int size,
00060 int64_t dts, int header)
00061 {
00062 FFMContext *ffm = s->priv_data;
00063 int len;
00064
00065 if (header && ffm->frame_offset == 0) {
00066 ffm->frame_offset = ffm->packet_ptr - ffm->packet + FFM_HEADER_SIZE;
00067 ffm->dts = dts;
00068 }
00069
00070
00071 while (size > 0) {
00072 len = ffm->packet_end - ffm->packet_ptr;
00073 if (len > size)
00074 len = size;
00075 memcpy(ffm->packet_ptr, buf, len);
00076
00077 ffm->packet_ptr += len;
00078 buf += len;
00079 size -= len;
00080 if (ffm->packet_ptr >= ffm->packet_end)
00081 flush_packet(s);
00082 }
00083 }
00084
00085 static int ffm_write_header(AVFormatContext *s)
00086 {
00087 FFMContext *ffm = s->priv_data;
00088 AVStream *st;
00089 AVIOContext *pb = s->pb;
00090 AVCodecContext *codec;
00091 int bit_rate, i;
00092
00093 ffm->packet_size = FFM_PACKET_SIZE;
00094
00095
00096 avio_wl32(pb, MKTAG('F', 'F', 'M', '1'));
00097 avio_wb32(pb, ffm->packet_size);
00098 avio_wb64(pb, 0);
00099
00100 avio_wb32(pb, s->nb_streams);
00101 bit_rate = 0;
00102 for(i=0;i<s->nb_streams;i++) {
00103 st = s->streams[i];
00104 bit_rate += st->codec->bit_rate;
00105 }
00106 avio_wb32(pb, bit_rate);
00107
00108
00109 for(i=0;i<s->nb_streams;i++) {
00110 st = s->streams[i];
00111 avpriv_set_pts_info(st, 64, 1, 1000000);
00112
00113 codec = st->codec;
00114
00115 avio_wb32(pb, codec->codec_id);
00116 avio_w8(pb, codec->codec_type);
00117 avio_wb32(pb, codec->bit_rate);
00118 avio_wb32(pb, codec->flags);
00119 avio_wb32(pb, codec->flags2);
00120 avio_wb32(pb, codec->debug);
00121
00122 switch(codec->codec_type) {
00123 case AVMEDIA_TYPE_VIDEO:
00124 avio_wb32(pb, codec->time_base.num);
00125 avio_wb32(pb, codec->time_base.den);
00126 avio_wb16(pb, codec->width);
00127 avio_wb16(pb, codec->height);
00128 avio_wb16(pb, codec->gop_size);
00129 avio_wb32(pb, codec->pix_fmt);
00130 avio_w8(pb, codec->qmin);
00131 avio_w8(pb, codec->qmax);
00132 avio_w8(pb, codec->max_qdiff);
00133 avio_wb16(pb, (int) (codec->qcompress * 10000.0));
00134 avio_wb16(pb, (int) (codec->qblur * 10000.0));
00135 avio_wb32(pb, codec->bit_rate_tolerance);
00136 avio_put_str(pb, codec->rc_eq ? codec->rc_eq : "tex^qComp");
00137 avio_wb32(pb, codec->rc_max_rate);
00138 avio_wb32(pb, codec->rc_min_rate);
00139 avio_wb32(pb, codec->rc_buffer_size);
00140 avio_wb64(pb, av_double2int(codec->i_quant_factor));
00141 avio_wb64(pb, av_double2int(codec->b_quant_factor));
00142 avio_wb64(pb, av_double2int(codec->i_quant_offset));
00143 avio_wb64(pb, av_double2int(codec->b_quant_offset));
00144 avio_wb32(pb, codec->dct_algo);
00145 avio_wb32(pb, codec->strict_std_compliance);
00146 avio_wb32(pb, codec->max_b_frames);
00147 avio_wb32(pb, codec->mpeg_quant);
00148 avio_wb32(pb, codec->intra_dc_precision);
00149 avio_wb32(pb, codec->me_method);
00150 avio_wb32(pb, codec->mb_decision);
00151 avio_wb32(pb, codec->nsse_weight);
00152 avio_wb32(pb, codec->frame_skip_cmp);
00153 avio_wb64(pb, av_double2int(codec->rc_buffer_aggressivity));
00154 avio_wb32(pb, codec->codec_tag);
00155 avio_w8(pb, codec->thread_count);
00156 avio_wb32(pb, codec->coder_type);
00157 avio_wb32(pb, codec->me_cmp);
00158 avio_wb32(pb, codec->me_subpel_quality);
00159 avio_wb32(pb, codec->me_range);
00160 avio_wb32(pb, codec->keyint_min);
00161 avio_wb32(pb, codec->scenechange_threshold);
00162 avio_wb32(pb, codec->b_frame_strategy);
00163 avio_wb64(pb, av_double2int(codec->qcompress));
00164 avio_wb64(pb, av_double2int(codec->qblur));
00165 avio_wb32(pb, codec->max_qdiff);
00166 avio_wb32(pb, codec->refs);
00167 break;
00168 case AVMEDIA_TYPE_AUDIO:
00169 avio_wb32(pb, codec->sample_rate);
00170 avio_wl16(pb, codec->channels);
00171 avio_wl16(pb, codec->frame_size);
00172 avio_wl16(pb, codec->sample_fmt);
00173 break;
00174 default:
00175 return -1;
00176 }
00177 if (codec->flags & CODEC_FLAG_GLOBAL_HEADER) {
00178 avio_wb32(pb, codec->extradata_size);
00179 avio_write(pb, codec->extradata, codec->extradata_size);
00180 }
00181 }
00182
00183
00184 while ((avio_tell(pb) % ffm->packet_size) != 0)
00185 avio_w8(pb, 0);
00186
00187 avio_flush(pb);
00188
00189
00190 ffm->packet_ptr = ffm->packet;
00191 ffm->packet_end = ffm->packet + ffm->packet_size - FFM_HEADER_SIZE;
00192 av_assert0(ffm->packet_end >= ffm->packet);
00193 ffm->frame_offset = 0;
00194 ffm->dts = 0;
00195 ffm->first_packet = 1;
00196
00197 return 0;
00198 }
00199
00200 static int ffm_write_packet(AVFormatContext *s, AVPacket *pkt)
00201 {
00202 int64_t dts;
00203 uint8_t header[FRAME_HEADER_SIZE+4];
00204 int header_size = FRAME_HEADER_SIZE;
00205
00206 dts = pkt->dts;
00207
00208 header[0] = pkt->stream_index;
00209 header[1] = 0;
00210 if (pkt->flags & AV_PKT_FLAG_KEY)
00211 header[1] |= FLAG_KEY_FRAME;
00212 AV_WB24(header+2, pkt->size);
00213 AV_WB24(header+5, pkt->duration);
00214 AV_WB64(header+8, pkt->pts);
00215 if (pkt->pts != pkt->dts) {
00216 header[1] |= FLAG_DTS;
00217 AV_WB32(header+16, pkt->pts - pkt->dts);
00218 header_size += 4;
00219 }
00220 ffm_write_data(s, header, header_size, dts, 1);
00221 ffm_write_data(s, pkt->data, pkt->size, dts, 0);
00222
00223 return 0;
00224 }
00225
00226 static int ffm_write_trailer(AVFormatContext *s)
00227 {
00228 FFMContext *ffm = s->priv_data;
00229
00230
00231 if (ffm->packet_ptr > ffm->packet)
00232 flush_packet(s);
00233
00234 return 0;
00235 }
00236
00237 AVOutputFormat ff_ffm_muxer = {
00238 .name = "ffm",
00239 .long_name = NULL_IF_CONFIG_SMALL("FFM (FFserver live feed)"),
00240 .extensions = "ffm",
00241 .priv_data_size = sizeof(FFMContext),
00242 .audio_codec = AV_CODEC_ID_MP2,
00243 .video_codec = AV_CODEC_ID_MPEG1VIDEO,
00244 .write_header = ffm_write_header,
00245 .write_packet = ffm_write_packet,
00246 .write_trailer = ffm_write_trailer,
00247 };