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 "avformat.h"
00024 #include "ffm.h"
00025
00026 static void flush_packet(AVFormatContext *s)
00027 {
00028 FFMContext *ffm = s->priv_data;
00029 int fill_size, h;
00030 ByteIOContext *pb = s->pb;
00031
00032 fill_size = ffm->packet_end - ffm->packet_ptr;
00033 memset(ffm->packet_ptr, 0, fill_size);
00034
00035 if (url_ftell(pb) % ffm->packet_size)
00036 av_abort();
00037
00038
00039 put_be16(pb, PACKET_ID);
00040 put_be16(pb, fill_size);
00041 put_be64(pb, ffm->dts);
00042 h = ffm->frame_offset;
00043 if (ffm->first_packet)
00044 h |= 0x8000;
00045 put_be16(pb, h);
00046 put_buffer(pb, ffm->packet, ffm->packet_end - ffm->packet);
00047 put_flush_packet(pb);
00048
00049
00050 ffm->frame_offset = 0;
00051 ffm->packet_ptr = ffm->packet;
00052 ffm->first_packet = 0;
00053 }
00054
00055
00056 static void ffm_write_data(AVFormatContext *s,
00057 const uint8_t *buf, int size,
00058 int64_t dts, int header)
00059 {
00060 FFMContext *ffm = s->priv_data;
00061 int len;
00062
00063 if (header && ffm->frame_offset == 0) {
00064 ffm->frame_offset = ffm->packet_ptr - ffm->packet + FFM_HEADER_SIZE;
00065 ffm->dts = dts;
00066 }
00067
00068
00069 while (size > 0) {
00070 len = ffm->packet_end - ffm->packet_ptr;
00071 if (len > size)
00072 len = size;
00073 memcpy(ffm->packet_ptr, buf, len);
00074
00075 ffm->packet_ptr += len;
00076 buf += len;
00077 size -= len;
00078 if (ffm->packet_ptr >= ffm->packet_end)
00079 flush_packet(s);
00080 }
00081 }
00082
00083 static int ffm_write_header(AVFormatContext *s)
00084 {
00085 FFMContext *ffm = s->priv_data;
00086 AVStream *st;
00087 ByteIOContext *pb = s->pb;
00088 AVCodecContext *codec;
00089 int bit_rate, i;
00090
00091 ffm->packet_size = FFM_PACKET_SIZE;
00092
00093
00094 put_le32(pb, MKTAG('F', 'F', 'M', '1'));
00095 put_be32(pb, ffm->packet_size);
00096
00097 put_be64(pb, ffm->packet_size);
00098
00099 put_be32(pb, s->nb_streams);
00100 bit_rate = 0;
00101 for(i=0;i<s->nb_streams;i++) {
00102 st = s->streams[i];
00103 bit_rate += st->codec->bit_rate;
00104 }
00105 put_be32(pb, bit_rate);
00106
00107
00108 for(i=0;i<s->nb_streams;i++) {
00109 st = s->streams[i];
00110 av_set_pts_info(st, 64, 1, 1000000);
00111
00112 codec = st->codec;
00113
00114 put_be32(pb, codec->codec_id);
00115 put_byte(pb, codec->codec_type);
00116 put_be32(pb, codec->bit_rate);
00117 put_be32(pb, st->quality);
00118 put_be32(pb, codec->flags);
00119 put_be32(pb, codec->flags2);
00120 put_be32(pb, codec->debug);
00121
00122 switch(codec->codec_type) {
00123 case CODEC_TYPE_VIDEO:
00124 put_be32(pb, codec->time_base.num);
00125 put_be32(pb, codec->time_base.den);
00126 put_be16(pb, codec->width);
00127 put_be16(pb, codec->height);
00128 put_be16(pb, codec->gop_size);
00129 put_be32(pb, codec->pix_fmt);
00130 put_byte(pb, codec->qmin);
00131 put_byte(pb, codec->qmax);
00132 put_byte(pb, codec->max_qdiff);
00133 put_be16(pb, (int) (codec->qcompress * 10000.0));
00134 put_be16(pb, (int) (codec->qblur * 10000.0));
00135 put_be32(pb, codec->bit_rate_tolerance);
00136 put_strz(pb, codec->rc_eq ? codec->rc_eq : "tex^qComp");
00137 put_be32(pb, codec->rc_max_rate);
00138 put_be32(pb, codec->rc_min_rate);
00139 put_be32(pb, codec->rc_buffer_size);
00140 put_be64(pb, av_dbl2int(codec->i_quant_factor));
00141 put_be64(pb, av_dbl2int(codec->b_quant_factor));
00142 put_be64(pb, av_dbl2int(codec->i_quant_offset));
00143 put_be64(pb, av_dbl2int(codec->b_quant_offset));
00144 put_be32(pb, codec->dct_algo);
00145 put_be32(pb, codec->strict_std_compliance);
00146 put_be32(pb, codec->max_b_frames);
00147 put_be32(pb, codec->luma_elim_threshold);
00148 put_be32(pb, codec->chroma_elim_threshold);
00149 put_be32(pb, codec->mpeg_quant);
00150 put_be32(pb, codec->intra_dc_precision);
00151 put_be32(pb, codec->me_method);
00152 put_be32(pb, codec->mb_decision);
00153 put_be32(pb, codec->nsse_weight);
00154 put_be32(pb, codec->frame_skip_cmp);
00155 put_be64(pb, av_dbl2int(codec->rc_buffer_aggressivity));
00156 put_be32(pb, codec->codec_tag);
00157 put_byte(pb, codec->thread_count);
00158 break;
00159 case CODEC_TYPE_AUDIO:
00160 put_be32(pb, codec->sample_rate);
00161 put_le16(pb, codec->channels);
00162 put_le16(pb, codec->frame_size);
00163 break;
00164 default:
00165 return -1;
00166 }
00167 if (codec->flags & CODEC_FLAG_GLOBAL_HEADER) {
00168 put_be32(pb, codec->extradata_size);
00169 put_buffer(pb, codec->extradata, codec->extradata_size);
00170 }
00171 }
00172
00173
00174 while ((url_ftell(pb) % ffm->packet_size) != 0)
00175 put_byte(pb, 0);
00176
00177 put_flush_packet(pb);
00178
00179
00180 ffm->packet_ptr = ffm->packet;
00181 ffm->packet_end = ffm->packet + ffm->packet_size - FFM_HEADER_SIZE;
00182 assert(ffm->packet_end >= ffm->packet);
00183 ffm->frame_offset = 0;
00184 ffm->dts = 0;
00185 ffm->first_packet = 1;
00186
00187 return 0;
00188 }
00189
00190 static int ffm_write_packet(AVFormatContext *s, AVPacket *pkt)
00191 {
00192 int64_t dts;
00193 uint8_t header[FRAME_HEADER_SIZE+4];
00194 int header_size = FRAME_HEADER_SIZE;
00195
00196 dts = s->timestamp + pkt->dts;
00197
00198 header[0] = pkt->stream_index;
00199 header[1] = 0;
00200 if (pkt->flags & PKT_FLAG_KEY)
00201 header[1] |= FLAG_KEY_FRAME;
00202 AV_WB24(header+2, pkt->size);
00203 AV_WB24(header+5, pkt->duration);
00204 AV_WB64(header+8, s->timestamp + pkt->pts);
00205 if (pkt->pts != pkt->dts) {
00206 header[1] |= FLAG_DTS;
00207 AV_WB32(header+16, pkt->pts - pkt->dts);
00208 header_size += 4;
00209 }
00210 ffm_write_data(s, header, header_size, dts, 1);
00211 ffm_write_data(s, pkt->data, pkt->size, dts, 0);
00212
00213 return 0;
00214 }
00215
00216 static int ffm_write_trailer(AVFormatContext *s)
00217 {
00218 ByteIOContext *pb = s->pb;
00219 FFMContext *ffm = s->priv_data;
00220
00221
00222 if (ffm->packet_ptr > ffm->packet)
00223 flush_packet(s);
00224
00225 put_flush_packet(pb);
00226
00227 if (!url_is_streamed(pb)) {
00228 int64_t size;
00229
00230 size = url_ftell(pb);
00231 url_fseek(pb, 8, SEEK_SET);
00232 put_be64(pb, size);
00233 put_flush_packet(pb);
00234 }
00235
00236 return 0;
00237 }
00238
00239 AVOutputFormat ffm_muxer = {
00240 "ffm",
00241 NULL_IF_CONFIG_SMALL("FFM (FFserver live feed) format"),
00242 "",
00243 "ffm",
00244 sizeof(FFMContext),
00245
00246 CODEC_ID_MP2,
00247 CODEC_ID_MPEG1VIDEO,
00248 ffm_write_header,
00249 ffm_write_packet,
00250 ffm_write_trailer,
00251 };