ffmenc.c
Go to the documentation of this file.
1 /*
2  * FFM (avserver live feed) muxer
3  * Copyright (c) 2001 Fabrice Bellard
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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 "avformat.h"
25 #include "internal.h"
26 #include "ffm.h"
27 
29 {
30  FFMContext *ffm = s->priv_data;
31  int fill_size, h;
32  AVIOContext *pb = s->pb;
33 
34  fill_size = ffm->packet_end - ffm->packet_ptr;
35  memset(ffm->packet_ptr, 0, fill_size);
36 
37  if (avio_tell(pb) % ffm->packet_size)
38  av_abort();
39 
40  /* put header */
41  avio_wb16(pb, PACKET_ID);
42  avio_wb16(pb, fill_size);
43  avio_wb64(pb, ffm->dts);
44  h = ffm->frame_offset;
45  if (ffm->first_packet)
46  h |= 0x8000;
47  avio_wb16(pb, h);
48  avio_write(pb, ffm->packet, ffm->packet_end - ffm->packet);
49  avio_flush(pb);
50 
51  /* prepare next packet */
52  ffm->frame_offset = 0; /* no key frame */
53  ffm->packet_ptr = ffm->packet;
54  ffm->first_packet = 0;
55 }
56 
57 /* 'first' is true if first data of a frame */
59  const uint8_t *buf, int size,
60  int64_t dts, int header)
61 {
62  FFMContext *ffm = s->priv_data;
63  int len;
64 
65  if (header && ffm->frame_offset == 0) {
66  ffm->frame_offset = ffm->packet_ptr - ffm->packet + FFM_HEADER_SIZE;
67  ffm->dts = dts;
68  }
69 
70  /* write as many packets as needed */
71  while (size > 0) {
72  len = ffm->packet_end - ffm->packet_ptr;
73  if (len > size)
74  len = size;
75  memcpy(ffm->packet_ptr, buf, len);
76 
77  ffm->packet_ptr += len;
78  buf += len;
79  size -= len;
80  if (ffm->packet_ptr >= ffm->packet_end)
81  flush_packet(s);
82  }
83 }
84 
86 {
87  FFMContext *ffm = s->priv_data;
88  AVStream *st;
89  AVIOContext *pb = s->pb;
90  AVCodecContext *codec;
91  int bit_rate, i;
92 
94 
95  /* header */
96  avio_wl32(pb, MKTAG('F', 'F', 'M', '1'));
97  avio_wb32(pb, ffm->packet_size);
98  avio_wb64(pb, 0); /* current write position */
99 
100  avio_wb32(pb, s->nb_streams);
101  bit_rate = 0;
102  for(i=0;i<s->nb_streams;i++) {
103  st = s->streams[i];
104  bit_rate += st->codec->bit_rate;
105  }
106  avio_wb32(pb, bit_rate);
107 
108  /* list of streams */
109  for(i=0;i<s->nb_streams;i++) {
110  st = s->streams[i];
111  avpriv_set_pts_info(st, 64, 1, 1000000);
112 
113  codec = st->codec;
114  /* generic info */
115  avio_wb32(pb, codec->codec_id);
116  avio_w8(pb, codec->codec_type);
117  avio_wb32(pb, codec->bit_rate);
118  avio_wb32(pb, codec->flags);
119  avio_wb32(pb, codec->flags2);
120  avio_wb32(pb, codec->debug);
121  /* specific info */
122  switch(codec->codec_type) {
123  case AVMEDIA_TYPE_VIDEO:
124  avio_wb32(pb, codec->time_base.num);
125  avio_wb32(pb, codec->time_base.den);
126  avio_wb16(pb, codec->width);
127  avio_wb16(pb, codec->height);
128  avio_wb16(pb, codec->gop_size);
129  avio_wb32(pb, codec->pix_fmt);
130  avio_w8(pb, codec->qmin);
131  avio_w8(pb, codec->qmax);
132  avio_w8(pb, codec->max_qdiff);
133  avio_wb16(pb, (int) (codec->qcompress * 10000.0));
134  avio_wb16(pb, (int) (codec->qblur * 10000.0));
135  avio_wb32(pb, codec->bit_rate_tolerance);
136  avio_put_str(pb, codec->rc_eq ? codec->rc_eq : "tex^qComp");
137  avio_wb32(pb, codec->rc_max_rate);
138  avio_wb32(pb, codec->rc_min_rate);
139  avio_wb32(pb, codec->rc_buffer_size);
144  avio_wb32(pb, codec->dct_algo);
145  avio_wb32(pb, codec->strict_std_compliance);
146  avio_wb32(pb, codec->max_b_frames);
147  avio_wb32(pb, codec->luma_elim_threshold);
148  avio_wb32(pb, codec->chroma_elim_threshold);
149  avio_wb32(pb, codec->mpeg_quant);
150  avio_wb32(pb, codec->intra_dc_precision);
151  avio_wb32(pb, codec->me_method);
152  avio_wb32(pb, codec->mb_decision);
153  avio_wb32(pb, codec->nsse_weight);
154  avio_wb32(pb, codec->frame_skip_cmp);
156  avio_wb32(pb, codec->codec_tag);
157  avio_w8(pb, codec->thread_count);
158  avio_wb32(pb, codec->coder_type);
159  avio_wb32(pb, codec->me_cmp);
160  avio_wb32(pb, codec->me_subpel_quality);
161  avio_wb32(pb, codec->me_range);
162  avio_wb32(pb, codec->keyint_min);
163  avio_wb32(pb, codec->scenechange_threshold);
164  avio_wb32(pb, codec->b_frame_strategy);
165  avio_wb64(pb, av_double2int(codec->qcompress));
166  avio_wb64(pb, av_double2int(codec->qblur));
167  avio_wb32(pb, codec->max_qdiff);
168  avio_wb32(pb, codec->refs);
169  break;
170  case AVMEDIA_TYPE_AUDIO:
171  avio_wb32(pb, codec->sample_rate);
172  avio_wl16(pb, codec->channels);
173  avio_wl16(pb, codec->frame_size);
174  avio_wl16(pb, codec->sample_fmt);
175  break;
176  default:
177  return -1;
178  }
179  if (codec->flags & CODEC_FLAG_GLOBAL_HEADER) {
180  avio_wb32(pb, codec->extradata_size);
181  avio_write(pb, codec->extradata, codec->extradata_size);
182  }
183  }
184 
185  /* flush until end of block reached */
186  while ((avio_tell(pb) % ffm->packet_size) != 0)
187  avio_w8(pb, 0);
188 
189  avio_flush(pb);
190 
191  /* init packet mux */
192  ffm->packet_ptr = ffm->packet;
193  ffm->packet_end = ffm->packet + ffm->packet_size - FFM_HEADER_SIZE;
194  assert(ffm->packet_end >= ffm->packet);
195  ffm->frame_offset = 0;
196  ffm->dts = 0;
197  ffm->first_packet = 1;
198 
199  return 0;
200 }
201 
203 {
204  int64_t dts;
205  uint8_t header[FRAME_HEADER_SIZE+4];
206  int header_size = FRAME_HEADER_SIZE;
207 
208  dts = pkt->dts;
209  /* packet size & key_frame */
210  header[0] = pkt->stream_index;
211  header[1] = 0;
212  if (pkt->flags & AV_PKT_FLAG_KEY)
213  header[1] |= FLAG_KEY_FRAME;
214  AV_WB24(header+2, pkt->size);
215  AV_WB24(header+5, pkt->duration);
216  AV_WB64(header+8, pkt->pts);
217  if (pkt->pts != pkt->dts) {
218  header[1] |= FLAG_DTS;
219  AV_WB32(header+16, pkt->pts - pkt->dts);
220  header_size += 4;
221  }
222  ffm_write_data(s, header, header_size, dts, 1);
223  ffm_write_data(s, pkt->data, pkt->size, dts, 0);
224 
225  return 0;
226 }
227 
229 {
230  AVIOContext *pb = s->pb;
231  FFMContext *ffm = s->priv_data;
232 
233  /* flush packets */
234  if (ffm->packet_ptr > ffm->packet)
235  flush_packet(s);
236 
237  avio_flush(pb);
238 
239  return 0;
240 }
241 
243  .name = "ffm",
244  .long_name = NULL_IF_CONFIG_SMALL("FFM (AVserver live feed) format"),
245  .mime_type = "",
246  .extensions = "ffm",
247  .priv_data_size = sizeof(FFMContext),
248  .audio_codec = CODEC_ID_MP2,
249  .video_codec = CODEC_ID_MPEG1VIDEO,
253 };
#define FRAME_HEADER_SIZE
Definition: asfdec.c:82
void avio_wb64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:521
static int write_header(AVFormatContext *s)
Definition: assenc.c:28
enum PixelFormat pix_fmt
Pixel format, see PIX_FMT_xxx.
Definition: avcodec.h:1426
void avio_wl16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:527
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 size
int mpeg_quant
0-> h263 quant 1-> mpeg quant
Definition: avcodec.h:1768
int dct_algo
DCT algorithm, see FF_DCT_* below.
Definition: avcodec.h:1861
int frame_offset
Definition: ffm.h:53
float qblur
amount of qscale smoothing over time (0.0-1.0)
Definition: avcodec.h:1483
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the pts for a given stream.
Definition: utils.c:3828
int chroma_elim_threshold
chroma single coeff elimination threshold
Definition: avcodec.h:1628
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:1512
static int write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: assenc.c:58
int num
numerator
Definition: rational.h:44
int size
Definition: avcodec.h:909
static av_always_inline uint64_t av_double2int(double f)
Reinterpret a double as a 64-bit integer.
Definition: intfloat.h:67
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:954
#define FLAG_DTS
Definition: ffm.h:37
int frame_skip_cmp
frame skip comparison function
Definition: avcodec.h:2571
float i_quant_offset
qscale offset between P and I-frames
Definition: avcodec.h:1847
int scenechange_threshold
scene change detection threshold 0 is default, larger means fewer detected scene changes.
Definition: avcodec.h:2292
#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:1398
Format I/O context.
Definition: avformat.h:863
int bit_rate_tolerance
number of bits the bitstream is allowed to diverge from the reference.
Definition: avcodec.h:1348
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:316
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1464
static void ffm_write_data(AVFormatContext *s, const uint8_t *buf, int size, int64_t dts, int header)
Definition: ffmenc.c:58
#define AV_WB32(p, d)
Definition: intreadwrite.h:239
int me_range
maximum motion estimation search range in subpel units If 0 then no limit.
Definition: avcodec.h:2161
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...
Definition: avcodec.h:1521
uint8_t * packet_end
Definition: ffm.h:55
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1387
int me_cmp
motion estimation comparison function
Definition: avcodec.h:2048
AVStream ** streams
Definition: avformat.h:908
int coder_type
coder type
Definition: avcodec.h:2220
uint8_t * data
Definition: avcodec.h:908
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:492
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:190
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:930
static int write_trailer(AVFormatContext *s)
Definition: assenc.c:67
#define av_abort()
Definition: internal.h:109
static int ffm_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: ffmenc.c:202
uint8_t * packet_ptr
Definition: ffm.h:55
int qmax
maximum quantizer
Definition: avcodec.h:1497
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:191
#define AV_WB64(p, d)
Definition: intreadwrite.h:275
int flags
CODEC_FLAG_*.
Definition: avcodec.h:1355
int rc_max_rate
maximum bitrate
Definition: avcodec.h:1816
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*...
Definition: avcodec.h:1840
const char * rc_eq
rate control equation
Definition: avcodec.h:1809
int first_packet
Definition: ffm.h:51
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:914
AVCodecContext * codec
codec context
Definition: avformat.h:623
int rc_buffer_size
decoder bitstream buffer size
Definition: avcodec.h:1830
static int ffm_write_trailer(AVFormatContext *s)
Definition: ffmenc.c:228
int intra_dc_precision
precision of the intra DC coefficient - 8
Definition: avcodec.h:2434
unsigned int nb_streams
A list of all streams in the file.
Definition: avformat.h:907
static void flush_packet(AVFormatContext *s)
Definition: ffmenc.c:28
int refs
number of reference frames
Definition: avcodec.h:2667
int bit_rate
the average bitrate
Definition: avcodec.h:1340
int void avio_flush(AVIOContext *s)
Definition: aviobuf.c:205
static int ffm_write_header(AVFormatContext *s)
Definition: ffmenc.c:85
int width
picture width / height.
Definition: avcodec.h:1408
const char * name
Definition: avformat.h:390
int b_frame_strategy
Definition: avcodec.h:1527
#define AV_WB24(p, d)
Definition: intreadwrite.h:412
int mb_decision
macroblock decision mode
Definition: avcodec.h:2259
int max_qdiff
maximum quantizer difference between frames
Definition: avcodec.h:1504
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() ...
Definition: avcodec.h:2392
int avio_put_str(AVIOContext *s, const char *str)
Write a NULL-terminated string.
Definition: aviobuf.c:469
Stream structure.
Definition: avformat.h:620
int frame_size
Samples per packet, initialized when calling 'init'.
Definition: avcodec.h:1470
enum AVMediaType codec_type
Definition: avcodec.h:1574
int sample_rate
samples per second
Definition: avcodec.h:1456
AVIOContext * pb
Definition: avformat.h:896
int debug
debug
Definition: avcodec.h:2007
Definition: ffm.h:44
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:169
main external API structure.
Definition: avcodec.h:1329
int qmin
minimum quantizer
Definition: avcodec.h:1490
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1590
int extradata_size
Definition: avcodec.h:1388
#define CODEC_FLAG_GLOBAL_HEADER
Place global headers in extradata instead of every keyframe.
Definition: avcodec.h:648
float rc_buffer_aggressivity
Definition: avcodec.h:1831
float b_quant_offset
qscale offset between IP and B-frames
Definition: avcodec.h:1654
float qcompress
amount of qscale change between easy & hard scenes (0.0-1.0)
Definition: avcodec.h:1482
AVOutputFormat ff_ffm_muxer
Definition: ffmenc.c:242
void avio_wb16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:533
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
Definition: avcodec.h:1417
Main libavformat public API header.
uint8_t packet[FFM_PACKET_SIZE]
Definition: ffm.h:56
int nsse_weight
noise vs.
Definition: avcodec.h:2441
int den
denominator
Definition: rational.h:45
int len
int channels
number of audio channels
Definition: avcodec.h:1457
void * priv_data
Format private data.
Definition: avformat.h:883
int flags2
CODEC_FLAG2_*.
Definition: avcodec.h:2357
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:907
void avio_wb32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:324
int me_method
Motion estimation algorithm used for video coding.
Definition: avcodec.h:1374
int stream_index
Definition: avcodec.h:910
int rc_min_rate
minimum bitrate
Definition: avcodec.h:1823
#define MKTAG(a, b, c, d)
Definition: common.h:231
int me_subpel_quality
subpel ME quality
Definition: avcodec.h:2124
#define FLAG_KEY_FRAME
Definition: ffm.h:36
int strict_std_compliance
strictly follow the standard (MPEG4, ...).
Definition: avcodec.h:1642
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:901
#define FFM_PACKET_SIZE
Definition: ffm.h:31
int luma_elim_threshold
luma single coefficient elimination threshold
Definition: avcodec.h:1621
enum CodecID codec_id
Definition: avcodec.h:1575
int keyint_min
minimum GOP size
Definition: avcodec.h:2660