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_readwrite.h"
00024 #include "avformat.h"
00025 #include "internal.h"
00026 #include "ffm.h"
00027
00028 static void flush_packet(AVFormatContext *s)
00029 {
00030 FFMContext *ffm = s->priv_data;
00031 int fill_size, h;
00032 AVIOContext *pb = s->pb;
00033
00034 fill_size = ffm->packet_end - ffm->packet_ptr;
00035 memset(ffm->packet_ptr, 0, fill_size);
00036
00037 if (avio_tell(pb) % ffm->packet_size)
00038 av_abort();
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_dbl2int(codec->i_quant_factor));
00141 avio_wb64(pb, av_dbl2int(codec->b_quant_factor));
00142 avio_wb64(pb, av_dbl2int(codec->i_quant_offset));
00143 avio_wb64(pb, av_dbl2int(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->luma_elim_threshold);
00148 avio_wb32(pb, codec->chroma_elim_threshold);
00149 avio_wb32(pb, codec->mpeg_quant);
00150 avio_wb32(pb, codec->intra_dc_precision);
00151 avio_wb32(pb, codec->me_method);
00152 avio_wb32(pb, codec->mb_decision);
00153 avio_wb32(pb, codec->nsse_weight);
00154 avio_wb32(pb, codec->frame_skip_cmp);
00155 avio_wb64(pb, av_dbl2int(codec->rc_buffer_aggressivity));
00156 avio_wb32(pb, codec->codec_tag);
00157 avio_w8(pb, codec->thread_count);
00158 avio_wb32(pb, codec->coder_type);
00159 avio_wb32(pb, codec->me_cmp);
00160 avio_wb32(pb, codec->partitions);
00161 avio_wb32(pb, codec->me_subpel_quality);
00162 avio_wb32(pb, codec->me_range);
00163 avio_wb32(pb, codec->keyint_min);
00164 avio_wb32(pb, codec->scenechange_threshold);
00165 avio_wb32(pb, codec->b_frame_strategy);
00166 avio_wb64(pb, av_dbl2int(codec->qcompress));
00167 avio_wb64(pb, av_dbl2int(codec->qblur));
00168 avio_wb32(pb, codec->max_qdiff);
00169 avio_wb32(pb, codec->refs);
00170 avio_wb32(pb, codec->directpred);
00171 break;
00172 case AVMEDIA_TYPE_AUDIO:
00173 avio_wb32(pb, codec->sample_rate);
00174 avio_wl16(pb, codec->channels);
00175 avio_wl16(pb, codec->frame_size);
00176 avio_wl16(pb, codec->sample_fmt);
00177 break;
00178 default:
00179 return -1;
00180 }
00181 if (codec->flags & CODEC_FLAG_GLOBAL_HEADER) {
00182 avio_wb32(pb, codec->extradata_size);
00183 avio_write(pb, codec->extradata, codec->extradata_size);
00184 }
00185 }
00186
00187
00188 while ((avio_tell(pb) % ffm->packet_size) != 0)
00189 avio_w8(pb, 0);
00190
00191 avio_flush(pb);
00192
00193
00194 ffm->packet_ptr = ffm->packet;
00195 ffm->packet_end = ffm->packet + ffm->packet_size - FFM_HEADER_SIZE;
00196 assert(ffm->packet_end >= ffm->packet);
00197 ffm->frame_offset = 0;
00198 ffm->dts = 0;
00199 ffm->first_packet = 1;
00200
00201 return 0;
00202 }
00203
00204 static int ffm_write_packet(AVFormatContext *s, AVPacket *pkt)
00205 {
00206 int64_t dts;
00207 uint8_t header[FRAME_HEADER_SIZE+4];
00208 int header_size = FRAME_HEADER_SIZE;
00209
00210 dts = s->timestamp + pkt->dts;
00211
00212 header[0] = pkt->stream_index;
00213 header[1] = 0;
00214 if (pkt->flags & AV_PKT_FLAG_KEY)
00215 header[1] |= FLAG_KEY_FRAME;
00216 AV_WB24(header+2, pkt->size);
00217 AV_WB24(header+5, pkt->duration);
00218 AV_WB64(header+8, s->timestamp + pkt->pts);
00219 if (pkt->pts != pkt->dts) {
00220 header[1] |= FLAG_DTS;
00221 AV_WB32(header+16, pkt->pts - pkt->dts);
00222 header_size += 4;
00223 }
00224 ffm_write_data(s, header, header_size, dts, 1);
00225 ffm_write_data(s, pkt->data, pkt->size, dts, 0);
00226
00227 return 0;
00228 }
00229
00230 static int ffm_write_trailer(AVFormatContext *s)
00231 {
00232 AVIOContext *pb = s->pb;
00233 FFMContext *ffm = s->priv_data;
00234
00235
00236 if (ffm->packet_ptr > ffm->packet)
00237 flush_packet(s);
00238
00239 avio_flush(pb);
00240
00241 return 0;
00242 }
00243
00244 AVOutputFormat ff_ffm_muxer = {
00245 .name = "ffm",
00246 .long_name = NULL_IF_CONFIG_SMALL("FFM (FFserver live feed) format"),
00247 .mime_type = "",
00248 .extensions = "ffm",
00249 .priv_data_size = sizeof(FFMContext),
00250 .audio_codec = CODEC_ID_MP2,
00251 .video_codec = CODEC_ID_MPEG1VIDEO,
00252 .write_header = ffm_write_header,
00253 .write_packet = ffm_write_packet,
00254 .write_trailer = ffm_write_trailer,
00255 };