FFmpeg  2.1.1
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,
281 };
#define AV_WB24(p, d)
Definition: intreadwrite.h:442
const char * s
Definition: avisynth_c.h:668
Bytestream IO Context.
Definition: avio.h:68
#define PACKET_ID
Definition: ffm.h:32
int64_t dts
Definition: ffm.h:54
int size
int mpeg_quant
0-&gt; h263 quant 1-&gt; mpeg quant
Definition: avcodec.h:1434
int dct_algo
DCT algorithm, see FF_DCT_* below.
Definition: avcodec.h:2527
int frame_offset
Definition: ffm.h:53
float qblur
amount of qscale smoothing over time (0.0-1.0)
Definition: avcodec.h:2152
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.
Definition: utils.c:3922
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...
Definition: avcodec.h:1397
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
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:686
int num
numerator
Definition: rational.h:44
int size
Definition: avcodec.h:1064
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1342
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:173
int void avio_flush(AVIOContext *s)
Force flushing of buffered data to the output s.
Definition: aviobuf.c:193
#define FLAG_DTS
Definition: ffm.h:37
int frame_skip_cmp
frame skip comparison function
Definition: avcodec.h:2311
float i_quant_offset
qscale offset between P and I-frames
Definition: avcodec.h:1450
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1254
int scenechange_threshold
scene change detection threshold 0 is default, larger means fewer detected scene changes.
Definition: avcodec.h:1690
#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...
Definition: avcodec.h:1265
Format I/O context.
Definition: avformat.h:968
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.
Definition: avcodec.h:1212
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.
Definition: avcodec.h:1626
#define CODEC_FLAG_GLOBAL_HEADER
Place global headers in extradata instead of every keyframe.
Definition: avcodec.h:713
float b_quant_factor
qscale factor between IP and B-frames If &gt; 0 then the last P-frame quantizer will be used (q= lastp_q...
Definition: avcodec.h:1406
uint8_t * packet_end
Definition: ffm.h:55
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:291
int me_cmp
motion estimation comparison function
Definition: avcodec.h:1524
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:248
int coder_type
coder type
Definition: avcodec.h:2262
#define AV_WB32(p, darg)
Definition: intreadwrite.h:265
int duration
Duration of this packet in AVStream-&gt;time_base units, 0 if unknown.
Definition: avcodec.h:1085
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:1030
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1113
static int ffm_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: ffmenc.c:232
uint8_t * packet_ptr
Definition: ffm.h:55
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:219
void * priv_data
Format private data.
Definition: avformat.h:988
int qmax
maximum quantizer
Definition: avcodec.h:2166
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:151
int flags
CODEC_FLAG_*.
Definition: avcodec.h:1234
int rc_max_rate
maximum bitrate
Definition: avcodec.h:2213
float i_quant_factor
qscale factor between P and I-frames If &gt; 0 then the last p frame quantizer will be used (q= lastp_q*...
Definition: avcodec.h:1443
int first_packet
Definition: ffm.h:51
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1069
int rc_buffer_size
decoder bitstream buffer size
Definition: avcodec.h:2191
static int ffm_write_trailer(AVFormatContext *s)
Definition: ffmenc.c:259
int intra_dc_precision
precision of the intra DC coefficient - 8
Definition: avcodec.h:1720
unsigned int nb_streams
A list of all streams in the file.
Definition: avformat.h:1015
static void flush_packet(AVFormatContext *s)
Definition: ffmenc.c:30
int refs
number of reference frames
Definition: avcodec.h:1791
int bit_rate
the average bitrate
Definition: avcodec.h:1204
static int ffm_write_header(AVFormatContext *s)
Definition: ffmenc.c:96
ret
Definition: avfilter.c:961
int width
picture width / height.
Definition: avcodec.h:1314
const char * rc_eq
rate control equation
Definition: avcodec.h:2206
int64_t start_time
Definition: ffm.h:57
AVStream ** streams
Definition: avformat.h:1016
static int write_trailer(AVFormatContext *s1)
Definition: v4l2enc.c:93
char * value
Definition: dict.h:82
int b_frame_strategy
Definition: avcodec.h:1412
#define AVFMT_TS_NEGATIVE
Format allows muxing negative timestamps.
Definition: avformat.h:372
const char * name
Definition: avformat.h:395
int mb_decision
macroblock decision mode
Definition: avcodec.h:1665
int max_qdiff
maximum quantizer difference between frames
Definition: avcodec.h:2173
int packet_size
Definition: ffm.h:52
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:151
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
Definition: avcodec.h:2596
Stream structure.
Definition: avformat.h:667
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:1893
enum AVMediaType codec_type
Definition: avcodec.h:1154
enum AVCodecID codec_id
Definition: avcodec.h:1157
int sample_rate
samples per second
Definition: avcodec.h:1873
int debug
debug
Definition: avcodec.h:2442
Definition: ffm.h:44
main external API structure.
Definition: avcodec.h:1146
int qmin
minimum quantizer
Definition: avcodec.h:2159
void avio_wb64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:361
unsigned int codec_tag
fourcc (LSB first, so &quot;ABCD&quot; -&gt; (&#39;D&#39;&lt;&lt;24) + (&#39;C&#39;&lt;&lt;16) + (&#39;B&#39;&lt;&lt;8) + &#39;A&#39;).
Definition: avcodec.h:1172
AVIOContext * pb
I/O context.
Definition: avformat.h:1001
void * buf
Definition: avisynth_c.h:594
int extradata_size
Definition: avcodec.h:1255
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1018
#define MKTAG(a, b, c, d)
float rc_buffer_aggressivity
Definition: avcodec.h:2222
uint8_t * data
Definition: avcodec.h:1063
AVDictionary * metadata
Definition: avformat.h:1128
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:550
void avio_wb32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:299
float b_quant_offset
qscale offset between IP and B-frames
Definition: avcodec.h:1419
void avio_wb16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:373
#define AV_WB64(p, darg)
Definition: intreadwrite.h:303
#define MKBETAG(a, b, c, d)
float qcompress
amount of qscale change between easy &amp; hard scenes (0.0-1.0)
Definition: avcodec.h:2151
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
AVOutputFormat ff_ffm_muxer
Definition: ffmenc.c:270
static int flags
Definition: cpu.c:45
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
Definition: avcodec.h:1333
static float t
Definition: muxing.c:123
uint8_t packet[FFM_PACKET_SIZE]
Definition: ffm.h:56
int nsse_weight
noise vs.
Definition: avcodec.h:2671
Main libavformat public API header.
int den
denominator
Definition: rational.h:45
int avio_put_str(AVIOContext *s, const char *str)
Write a NULL-terminated string.
Definition: aviobuf.c:307
int len
int channels
number of audio channels
Definition: avcodec.h:1874
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:492
static av_always_inline uint64_t av_double2int(double f)
Reinterpret a double as a 64-bit integer.
Definition: intfloat.h:70
#define AVERROR(e)
int flags2
CODEC_FLAG2_*.
Definition: avcodec.h:1241
int64_t dts
Decompression timestamp in AVStream-&gt;time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1062
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
static AVPacket pkt
Definition: demuxing.c:52
int me_method
Motion estimation algorithm used for video coding.
Definition: avcodec.h:1351
int stream_index
Definition: avcodec.h:1065
int rc_min_rate
minimum bitrate
Definition: avcodec.h:2220
This structure stores compressed data.
Definition: avcodec.h:1040
static int write_packet(AVFormatContext *s1, AVPacket *pkt)
Definition: v4l2enc.c:85
int me_subpel_quality
subpel ME quality
Definition: avcodec.h:1600
#define FLAG_KEY_FRAME
Definition: ffm.h:36
int strict_std_compliance
strictly follow the standard (MPEG4, ...).
Definition: avcodec.h:2421
int64_t pts
Presentation timestamp in AVStream-&gt;time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1056
void avio_wl16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:367
#define FFM_PACKET_SIZE
Definition: ffm.h:31
int keyint_min
minimum GOP size
Definition: avcodec.h:1784