ffmenc.c
Go to the documentation of this file.
1 /*
2  * FFM (ffserver live feed) muxer
3  * Copyright (c) 2001 Fabrice Bellard
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "libavutil/intreadwrite.h"
23 #include "libavutil/intfloat.h"
24 #include "libavutil/avassert.h"
25 #include "libavutil/parseutils.h"
26 #include "avformat.h"
27 #include "internal.h"
28 #include "ffm.h"
29 
31 {
32  FFMContext *ffm = s->priv_data;
33  int fill_size, h;
34  AVIOContext *pb = s->pb;
35 
36  fill_size = ffm->packet_end - ffm->packet_ptr;
37  memset(ffm->packet_ptr, 0, fill_size);
38 
39  av_assert1(avio_tell(pb) % ffm->packet_size == 0);
40 
41  /* put header */
42  avio_wb16(pb, PACKET_ID);
43  avio_wb16(pb, fill_size);
44  avio_wb64(pb, ffm->dts);
45  h = ffm->frame_offset;
46  if (ffm->first_packet)
47  h |= 0x8000;
48  avio_wb16(pb, h);
49  avio_write(pb, ffm->packet, ffm->packet_end - ffm->packet);
50  avio_flush(pb);
51 
52  /* prepare next packet */
53  ffm->frame_offset = 0; /* no key frame */
54  ffm->packet_ptr = ffm->packet;
55  ffm->first_packet = 0;
56 }
57 
58 /* 'first' is true if first data of a frame */
60  const uint8_t *buf, int size,
61  int64_t dts, int header)
62 {
63  FFMContext *ffm = s->priv_data;
64  int len;
65 
66  if (header && ffm->frame_offset == 0) {
67  ffm->frame_offset = ffm->packet_ptr - ffm->packet + FFM_HEADER_SIZE;
68  ffm->dts = dts;
69  }
70 
71  /* write as many packets as needed */
72  while (size > 0) {
73  len = ffm->packet_end - ffm->packet_ptr;
74  if (len > size)
75  len = size;
76  memcpy(ffm->packet_ptr, buf, len);
77 
78  ffm->packet_ptr += len;
79  buf += len;
80  size -= len;
81  if (ffm->packet_ptr >= ffm->packet_end)
82  flush_packet(s);
83  }
84 }
85 
86 static void write_header_chunk(AVIOContext *pb, AVIOContext *dpb, unsigned id)
87 {
88  uint8_t *dyn_buf;
89  int dyn_size= avio_close_dyn_buf(dpb, &dyn_buf);
90  avio_wb32(pb, id);
91  avio_wb32(pb, dyn_size);
92  avio_write(pb, dyn_buf, dyn_size);
93  av_free(dyn_buf);
94 }
95 
97 {
98  FFMContext *ffm = s->priv_data;
100  AVStream *st;
101  AVIOContext *pb = s->pb;
102  AVCodecContext *codec;
103  int bit_rate, i;
104 
105  if (t = av_dict_get(s->metadata, "creation_time", NULL, 0)) {
106  int ret = av_parse_time(&ffm->start_time, t->value, 0);
107  if (ret < 0)
108  return ret;
109  }
110 
112 
113  /* header */
114  avio_wl32(pb, MKTAG('F', 'F', 'M', '2'));
115  avio_wb32(pb, ffm->packet_size);
116  avio_wb64(pb, 0); /* current write position */
117 
118  if(avio_open_dyn_buf(&pb) < 0)
119  return AVERROR(ENOMEM);
120 
121  avio_wb32(pb, s->nb_streams);
122  bit_rate = 0;
123  for(i=0;i<s->nb_streams;i++) {
124  st = s->streams[i];
125  bit_rate += st->codec->bit_rate;
126  }
127  avio_wb32(pb, bit_rate);
128 
129  write_header_chunk(s->pb, pb, MKBETAG('M', 'A', 'I', 'N'));
130 
131  /* list of streams */
132  for(i=0;i<s->nb_streams;i++) {
133  st = s->streams[i];
134  avpriv_set_pts_info(st, 64, 1, 1000000);
135  if(avio_open_dyn_buf(&pb) < 0)
136  return AVERROR(ENOMEM);
137 
138  codec = st->codec;
139  /* generic info */
140  avio_wb32(pb, codec->codec_id);
141  avio_w8(pb, codec->codec_type);
142  avio_wb32(pb, codec->bit_rate);
143  avio_wb32(pb, codec->flags);
144  avio_wb32(pb, codec->flags2);
145  avio_wb32(pb, codec->debug);
146  if (codec->flags & CODEC_FLAG_GLOBAL_HEADER) {
147  avio_wb32(pb, codec->extradata_size);
148  avio_write(pb, codec->extradata, codec->extradata_size);
149  }
150  write_header_chunk(s->pb, pb, MKBETAG('C', 'O', 'M', 'M'));
151  if(avio_open_dyn_buf(&pb) < 0)
152  return AVERROR(ENOMEM);
153  /* specific info */
154  switch(codec->codec_type) {
155  case AVMEDIA_TYPE_VIDEO:
156  avio_wb32(pb, codec->time_base.num);
157  avio_wb32(pb, codec->time_base.den);
158  avio_wb16(pb, codec->width);
159  avio_wb16(pb, codec->height);
160  avio_wb16(pb, codec->gop_size);
161  avio_wb32(pb, codec->pix_fmt);
162  avio_w8(pb, codec->qmin);
163  avio_w8(pb, codec->qmax);
164  avio_w8(pb, codec->max_qdiff);
165  avio_wb16(pb, (int) (codec->qcompress * 10000.0));
166  avio_wb16(pb, (int) (codec->qblur * 10000.0));
167  avio_wb32(pb, codec->bit_rate_tolerance);
168  avio_put_str(pb, codec->rc_eq ? codec->rc_eq : "tex^qComp");
169  avio_wb32(pb, codec->rc_max_rate);
170  avio_wb32(pb, codec->rc_min_rate);
171  avio_wb32(pb, codec->rc_buffer_size);
176  avio_wb32(pb, codec->dct_algo);
177  avio_wb32(pb, codec->strict_std_compliance);
178  avio_wb32(pb, codec->max_b_frames);
179  avio_wb32(pb, codec->mpeg_quant);
180  avio_wb32(pb, codec->intra_dc_precision);
181  avio_wb32(pb, codec->me_method);
182  avio_wb32(pb, codec->mb_decision);
183  avio_wb32(pb, codec->nsse_weight);
184  avio_wb32(pb, codec->frame_skip_cmp);
186  avio_wb32(pb, codec->codec_tag);
187  avio_w8(pb, codec->thread_count);
188  avio_wb32(pb, codec->coder_type);
189  avio_wb32(pb, codec->me_cmp);
190  avio_wb32(pb, codec->me_subpel_quality);
191  avio_wb32(pb, codec->me_range);
192  avio_wb32(pb, codec->keyint_min);
193  avio_wb32(pb, codec->scenechange_threshold);
194  avio_wb32(pb, codec->b_frame_strategy);
195  avio_wb64(pb, av_double2int(codec->qcompress));
196  avio_wb64(pb, av_double2int(codec->qblur));
197  avio_wb32(pb, codec->max_qdiff);
198  avio_wb32(pb, codec->refs);
199  write_header_chunk(s->pb, pb, MKBETAG('S', 'T', 'V', 'I'));
200  break;
201  case AVMEDIA_TYPE_AUDIO:
202  avio_wb32(pb, codec->sample_rate);
203  avio_wl16(pb, codec->channels);
204  avio_wl16(pb, codec->frame_size);
205  write_header_chunk(s->pb, pb, MKBETAG('S', 'T', 'A', 'U'));
206  break;
207  default:
208  return -1;
209  }
210  }
211  pb = s->pb;
212 
213  avio_wb64(pb, 0); // end of header
214 
215  /* flush until end of block reached */
216  while ((avio_tell(pb) % ffm->packet_size) != 0)
217  avio_w8(pb, 0);
218 
219  avio_flush(pb);
220 
221  /* init packet mux */
222  ffm->packet_ptr = ffm->packet;
223  ffm->packet_end = ffm->packet + ffm->packet_size - FFM_HEADER_SIZE;
224  av_assert0(ffm->packet_end >= ffm->packet);
225  ffm->frame_offset = 0;
226  ffm->dts = 0;
227  ffm->first_packet = 1;
228 
229  return 0;
230 }
231 
233 {
234  FFMContext *ffm = s->priv_data;
235  int64_t dts;
236  uint8_t header[FRAME_HEADER_SIZE+4];
237  int header_size = FRAME_HEADER_SIZE;
238 
239  dts = ffm->start_time + pkt->dts;
240  /* packet size & key_frame */
241  header[0] = pkt->stream_index;
242  header[1] = 0;
243  if (pkt->flags & AV_PKT_FLAG_KEY)
244  header[1] |= FLAG_KEY_FRAME;
245  AV_WB24(header+2, pkt->size);
246  AV_WB24(header+5, pkt->duration);
247  AV_WB64(header+8, ffm->start_time + pkt->pts);
248  if (pkt->pts != pkt->dts) {
249  header[1] |= FLAG_DTS;
250  AV_WB32(header+16, pkt->pts - pkt->dts);
251  header_size += 4;
252  }
253  ffm_write_data(s, header, header_size, dts, 1);
254  ffm_write_data(s, pkt->data, pkt->size, dts, 0);
255 
256  return 0;
257 }
258 
260 {
261  FFMContext *ffm = s->priv_data;
262 
263  /* flush packets */
264  if (ffm->packet_ptr > ffm->packet)
265  flush_packet(s);
266 
267  return 0;
268 }
269 
271  .name = "ffm",
272  .long_name = NULL_IF_CONFIG_SMALL("FFM (FFserver live feed)"),
273  .extensions = "ffm",
274  .priv_data_size = sizeof(FFMContext),
275  .audio_codec = AV_CODEC_ID_MP2,
276  .video_codec = AV_CODEC_ID_MPEG1VIDEO,
280 };
void avio_wb64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:361
void avio_wl16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:367
const char * s
Definition: avisynth_c.h:668
Bytestream IO Context.
Definition: avio.h:68
#define PACKET_ID
Definition: ffm.h:32
struct FFMContext FFMContext
int64_t dts
Definition: ffm.h:54
int mpeg_quant
0-> h263 quant 1-> mpeg quant
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:988
int dct_algo
DCT algorithm, see FF_DCT_* below.
int frame_offset
Definition: ffm.h:53
float qblur
amount of qscale smoothing over time (0.0-1.0)
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
int av_parse_time(int64_t *timeval, const char *timestr, int duration)
Parse timestr and return in *time a corresponding number of microseconds.
Definition: parseutils.c:530
int max_b_frames
maximum number of B-frames between non-B-frames Note: The output will be delayed by max_b_frames+1 re...
static int write_packet(AVFormatContext *s, AVPacket *pkt)
int num
numerator
Definition: rational.h:44
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
static av_always_inline uint64_t av_double2int(double f)
Reinterpret a double as a 64-bit integer.
Definition: intfloat.h:70
#define FLAG_DTS
Definition: ffm.h:37
AVDictionaryEntry * av_dict_get(AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:39
int frame_skip_cmp
frame skip comparison function
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:976
float i_quant_offset
qscale offset between P and I-frames
int scenechange_threshold
scene change detection threshold 0 is default, larger means fewer detected scene changes.
#define FFM_HEADER_SIZE
Definition: ffm.h:30
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Format I/O context.
Definition: avformat.h:944
#define AV_WB32(p, darg)
Definition: intreadwrite.h:265
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
static void write_header_chunk(AVIOContext *pb, AVIOContext *dpb, unsigned id)
Definition: ffmenc.c:86
int bit_rate_tolerance
number of bits the bitstream is allowed to diverge from the reference.
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:291
uint8_t
static void ffm_write_data(AVFormatContext *s, const uint8_t *buf, int size, int64_t dts, int header)
Definition: ffmenc.c:59
#define FRAME_HEADER_SIZE
Definition: cpia.c:30
int me_range
maximum motion estimation search range in subpel units If 0 then no limit.
#define CODEC_FLAG_GLOBAL_HEADER
Place global headers in extradata instead of every keyframe.
static AVPacket pkt
Definition: demuxing.c:56
float b_quant_factor
qscale factor between IP and B-frames If > 0 then the last P-frame quantizer will be used (q= lastp_q...
uint8_t * packet_end
Definition: ffm.h:55
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
int me_cmp
motion estimation comparison function
AVStream ** streams
Definition: avformat.h:992
int coder_type
coder type
uint8_t * data
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:248
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:173
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
static int write_trailer(AVFormatContext *s)
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
static int ffm_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: ffmenc.c:232
uint8_t * packet_ptr
Definition: ffm.h:55
AVDictionary * metadata
Definition: avformat.h:1092
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:183
int qmax
maximum quantizer
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
int flags
CODEC_FLAG_*.
int rc_max_rate
maximum bitrate
simple assert() macros that are a bit more flexible than ISO C assert().
float i_quant_factor
qscale factor between P and I-frames If > 0 then the last p frame quantizer will be used (q= lastp_q*...
const char * rc_eq
rate control equation
int size
int first_packet
Definition: ffm.h:51
int flags
A combination of AV_PKT_FLAG values.
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:662
int rc_buffer_size
decoder bitstream buffer size
static int ffm_write_trailer(AVFormatContext *s)
Definition: ffmenc.c:259
int intra_dc_precision
precision of the intra DC coefficient - 8
unsigned int nb_streams
A list of all streams in the file.
Definition: avformat.h:991
static void flush_packet(AVFormatContext *s)
Definition: ffmenc.c:30
int refs
number of reference frames
int bit_rate
the average bitrate
int void avio_flush(AVIOContext *s)
Force flushing of buffered data to the output s.
Definition: aviobuf.c:193
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
static int ffm_write_header(AVFormatContext *s)
Definition: ffmenc.c:96
ret
Definition: avfilter.c:821
int width
picture width / height.
int64_t start_time
Definition: ffm.h:57
t
Definition: genspecsines3.m:6
const char * name
Definition: avformat.h:378
#define AV_WB24(p, d)
Definition: intreadwrite.h:442
int mb_decision
macroblock decision mode
int max_qdiff
maximum quantizer difference between frames
int packet_size
Definition: ffm.h:52
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
int avio_put_str(AVIOContext *s, const char *str)
Write a NULL-terminated string.
Definition: aviobuf.c:307
Stream structure.
Definition: avformat.h:643
int frame_size
Number of samples per channel in an audio frame.
NULL
Definition: eval.c:55
enum AVMediaType codec_type
enum AVCodecID codec_id
int sample_rate
samples per second
AVIOContext * pb
I/O context.
Definition: avformat.h:977
Definition: ffm.h:44
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:151
main external API structure.
int qmin
minimum quantizer
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> (&#39;D&#39;<<24) + (&#39;C&#39;<<16) + (&#39;B&#39;<<8) + &#39;A&#39;).
void * buf
Definition: avisynth_c.h:594
synthesis window for stochastic i
float b_quant_offset
qscale offset between IP and B-frames
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Buffer references ownership and permissions
misc parsing utilities
float qcompress
amount of qscale change between easy & hard scenes (0.0-1.0)
#define AV_WB64(p, darg)
Definition: intreadwrite.h:303
AVOutputFormat ff_ffm_muxer
Definition: ffmenc.c:270
void avio_wb16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:373
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
Main libavformat public API header.
uint8_t packet[FFM_PACKET_SIZE]
Definition: ffm.h:56
int nsse_weight
noise vs.
int den
denominator
Definition: rational.h:45
#define MKBETAG(a, b, c, d)
Definition: common.h:283
char * value
Definition: dict.h:82
int len
int channels
number of audio channels
void * priv_data
Format private data.
Definition: avformat.h:964
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:470
int flags2
CODEC_FLAG2_*.
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
void avio_wb32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:299
int me_method
Motion estimation algorithm used for video coding.
int rc_min_rate
minimum bitrate
#define MKTAG(a, b, c, d)
Definition: common.h:282
This structure stores compressed data.
int me_subpel_quality
subpel ME quality
#define FLAG_KEY_FRAME
Definition: ffm.h:36
int strict_std_compliance
strictly follow the standard (MPEG4, ...).
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
#define FFM_PACKET_SIZE
Definition: ffm.h:31
int keyint_min
minimum GOP size