Libav
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 <assert.h>
23 
24 #include "libavutil/intreadwrite.h"
25 #include "libavutil/intfloat.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  assert(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 
87 {
88  FFMContext *ffm = s->priv_data;
89  AVStream *st;
90  AVIOContext *pb = s->pb;
91  AVCodecContext *codec;
92  int bit_rate, i;
93 
95 
96  /* header */
97  avio_wl32(pb, MKTAG('F', 'F', 'M', '1'));
98  avio_wb32(pb, ffm->packet_size);
99  avio_wb64(pb, 0); /* current write position */
100 
101  avio_wb32(pb, s->nb_streams);
102  bit_rate = 0;
103  for(i=0;i<s->nb_streams;i++) {
104  st = s->streams[i];
105  bit_rate += st->codec->bit_rate;
106  }
107  avio_wb32(pb, bit_rate);
108 
109  /* list of streams */
110  for(i=0;i<s->nb_streams;i++) {
111  st = s->streams[i];
112  avpriv_set_pts_info(st, 64, 1, 1000000);
113 
114  codec = st->codec;
115  /* generic info */
116  avio_wb32(pb, codec->codec_id);
117  avio_w8(pb, codec->codec_type);
118  avio_wb32(pb, codec->bit_rate);
119  avio_wb32(pb, codec->flags);
120  avio_wb32(pb, codec->flags2);
121  avio_wb32(pb, codec->debug);
122  /* specific info */
123  switch(codec->codec_type) {
124  case AVMEDIA_TYPE_VIDEO:
125  avio_wb32(pb, codec->time_base.num);
126  avio_wb32(pb, codec->time_base.den);
127  avio_wb16(pb, codec->width);
128  avio_wb16(pb, codec->height);
129  avio_wb16(pb, codec->gop_size);
130  avio_wb32(pb, codec->pix_fmt);
131  avio_w8(pb, codec->qmin);
132  avio_w8(pb, codec->qmax);
133  avio_w8(pb, codec->max_qdiff);
134  avio_wb16(pb, (int) (codec->qcompress * 10000.0));
135  avio_wb16(pb, (int) (codec->qblur * 10000.0));
136  avio_wb32(pb, codec->bit_rate_tolerance);
137  avio_put_str(pb, codec->rc_eq ? codec->rc_eq : "tex^qComp");
138  avio_wb32(pb, codec->rc_max_rate);
139  avio_wb32(pb, codec->rc_min_rate);
140  avio_wb32(pb, codec->rc_buffer_size);
145  avio_wb32(pb, codec->dct_algo);
146  avio_wb32(pb, codec->strict_std_compliance);
147  avio_wb32(pb, codec->max_b_frames);
148  avio_wb32(pb, codec->mpeg_quant);
149  avio_wb32(pb, codec->intra_dc_precision);
150  avio_wb32(pb, codec->me_method);
151  avio_wb32(pb, codec->mb_decision);
152  avio_wb32(pb, codec->nsse_weight);
153  avio_wb32(pb, codec->frame_skip_cmp);
155  avio_wb32(pb, codec->codec_tag);
156  avio_w8(pb, codec->thread_count);
157  avio_wb32(pb, codec->coder_type);
158  avio_wb32(pb, codec->me_cmp);
159  avio_wb32(pb, codec->me_subpel_quality);
160  avio_wb32(pb, codec->me_range);
161  avio_wb32(pb, codec->keyint_min);
162  avio_wb32(pb, codec->scenechange_threshold);
163  avio_wb32(pb, codec->b_frame_strategy);
164  avio_wb64(pb, av_double2int(codec->qcompress));
165  avio_wb64(pb, av_double2int(codec->qblur));
166  avio_wb32(pb, codec->max_qdiff);
167  avio_wb32(pb, codec->refs);
168  break;
169  case AVMEDIA_TYPE_AUDIO:
170  avio_wb32(pb, codec->sample_rate);
171  avio_wl16(pb, codec->channels);
172  avio_wl16(pb, codec->frame_size);
173  break;
174  default:
175  return -1;
176  }
177  if (codec->flags & CODEC_FLAG_GLOBAL_HEADER) {
178  avio_wb32(pb, codec->extradata_size);
179  avio_write(pb, codec->extradata, codec->extradata_size);
180  }
181  }
182 
183  /* flush until end of block reached */
184  while ((avio_tell(pb) % ffm->packet_size) != 0)
185  avio_w8(pb, 0);
186 
187  avio_flush(pb);
188 
189  /* init packet mux */
190  ffm->packet_ptr = ffm->packet;
191  ffm->packet_end = ffm->packet + ffm->packet_size - FFM_HEADER_SIZE;
192  assert(ffm->packet_end >= ffm->packet);
193  ffm->frame_offset = 0;
194  ffm->dts = 0;
195  ffm->first_packet = 1;
196 
197  return 0;
198 }
199 
201 {
202  int64_t dts;
203  uint8_t header[FRAME_HEADER_SIZE+4];
204  int header_size = FRAME_HEADER_SIZE;
205 
206  dts = pkt->dts;
207  /* packet size & key_frame */
208  header[0] = pkt->stream_index;
209  header[1] = 0;
210  if (pkt->flags & AV_PKT_FLAG_KEY)
211  header[1] |= FLAG_KEY_FRAME;
212  AV_WB24(header+2, pkt->size);
213  AV_WB24(header+5, pkt->duration);
214  AV_WB64(header+8, pkt->pts);
215  if (pkt->pts != pkt->dts) {
216  header[1] |= FLAG_DTS;
217  AV_WB32(header+16, pkt->pts - pkt->dts);
218  header_size += 4;
219  }
220  ffm_write_data(s, header, header_size, dts, 1);
221  ffm_write_data(s, pkt->data, pkt->size, dts, 0);
222 
223  return 0;
224 }
225 
227 {
228  FFMContext *ffm = s->priv_data;
229 
230  /* flush packets */
231  if (ffm->packet_ptr > ffm->packet)
232  flush_packet(s);
233 
234  return 0;
235 }
236 
238  .name = "ffm",
239  .long_name = NULL_IF_CONFIG_SMALL("FFM (AVserver live feed)"),
240  .mime_type = "",
241  .extensions = "ffm",
242  .priv_data_size = sizeof(FFMContext),
243  .audio_codec = AV_CODEC_ID_MP2,
244  .video_codec = AV_CODEC_ID_MPEG1VIDEO,
249 };
#define FRAME_HEADER_SIZE
Definition: asfdec.c:99
void avio_wb64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:330
void avio_wl16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:336
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-> h263 quant 1-> mpeg quant
Definition: avcodec.h:1339
int dct_algo
DCT algorithm, see FF_DCT_* below.
Definition: avcodec.h:2443
int frame_offset
Definition: ffm.h:53
float qblur
amount of qscale smoothing over time (0.0-1.0)
Definition: avcodec.h:2054
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:3208
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:1302
static int write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: assenc.c:58
int num
numerator
Definition: rational.h:44
int size
Definition: avcodec.h:974
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1247
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
int frame_skip_cmp
frame skip comparison function
Definition: avcodec.h:2213
float i_quant_offset
qscale offset between P and I-frames
Definition: avcodec.h:1355
int scenechange_threshold
scene change detection threshold 0 is default, larger means fewer detected scene changes.
Definition: avcodec.h:1596
#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:1173
Format I/O context.
Definition: avformat.h:871
int bit_rate_tolerance
number of bits the bitstream is allowed to diverge from the reference.
Definition: avcodec.h:1120
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:260
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 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:1529
#define CODEC_FLAG_GLOBAL_HEADER
Place global headers in extradata instead of every keyframe.
Definition: avcodec.h:684
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:1311
uint8_t * packet_end
Definition: ffm.h:55
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1162
int me_cmp
motion estimation comparison function
Definition: avcodec.h:1429
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:935
int coder_type
coder type
Definition: avcodec.h:2164
uint8_t * data
Definition: avcodec.h:973
static int flags
Definition: log.c:44
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:219
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:165
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:995
static int write_trailer(AVFormatContext *s)
Definition: assenc.c:64
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1023
static int ffm_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: ffmenc.c:200
uint8_t * packet_ptr
Definition: ffm.h:55
int qmax
maximum quantizer
Definition: avcodec.h:2068
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:142
#define AV_WB64(p, d)
Definition: intreadwrite.h:275
int flags
CODEC_FLAG_*.
Definition: avcodec.h:1142
int rc_max_rate
maximum bitrate
Definition: avcodec.h:2115
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:1348
const char * rc_eq
rate control equation
Definition: avcodec.h:2108
int first_packet
Definition: ffm.h:51
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:979
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:702
int rc_buffer_size
decoder bitstream buffer size
Definition: avcodec.h:2093
static int ffm_write_trailer(AVFormatContext *s)
Definition: ffmenc.c:226
int intra_dc_precision
precision of the intra DC coefficient - 8
Definition: avcodec.h:1626
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:923
static void flush_packet(AVFormatContext *s)
Definition: ffmenc.c:30
int refs
number of reference frames
Definition: avcodec.h:1697
int bit_rate
the average bitrate
Definition: avcodec.h:1112
int void avio_flush(AVIOContext *s)
Definition: aviobuf.c:180
static int ffm_write_header(AVFormatContext *s)
Definition: ffmenc.c:86
int width
picture width / height.
Definition: avcodec.h:1217
const char * name
Definition: avformat.h:437
int b_frame_strategy
Definition: avcodec.h:1317
#define AV_WB24(p, d)
Definition: intreadwrite.h:412
int mb_decision
macroblock decision mode
Definition: avcodec.h:1571
int max_qdiff
maximum quantizer difference between frames
Definition: avcodec.h:2075
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:2514
int avio_put_str(AVIOContext *s, const char *str)
Write a NULL-terminated string.
Definition: aviobuf.c:276
Stream structure.
Definition: avformat.h:683
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:1799
enum AVMediaType codec_type
Definition: avcodec.h:1062
enum AVCodecID codec_id
Definition: avcodec.h:1065
int sample_rate
samples per second
Definition: avcodec.h:1779
AVIOContext * pb
I/O context.
Definition: avformat.h:913
int debug
debug
Definition: avcodec.h:2348
Definition: ffm.h:44
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:144
main external API structure.
Definition: avcodec.h:1054
int qmin
minimum quantizer
Definition: avcodec.h:2061
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1080
int extradata_size
Definition: avcodec.h:1163
float rc_buffer_aggressivity
Definition: avcodec.h:2124
float b_quant_offset
qscale offset between IP and B-frames
Definition: avcodec.h:1324
float qcompress
amount of qscale change between easy & hard scenes (0.0-1.0)
Definition: avcodec.h:2053
AVOutputFormat ff_ffm_muxer
Definition: ffmenc.c:237
void avio_wb16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:342
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
Definition: avcodec.h:1238
Main libavformat public API header.
uint8_t packet[FFM_PACKET_SIZE]
Definition: ffm.h:56
int nsse_weight
noise vs.
Definition: avcodec.h:2589
int den
denominator
Definition: rational.h:45
int len
int channels
number of audio channels
Definition: avcodec.h:1780
void * priv_data
Format private data.
Definition: avformat.h:899
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:380
int flags2
CODEC_FLAG2_*.
Definition: avcodec.h:1149
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:972
void avio_wb32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:268
int me_method
Motion estimation algorithm used for video coding.
Definition: avcodec.h:1256
int stream_index
Definition: avcodec.h:975
int rc_min_rate
minimum bitrate
Definition: avcodec.h:2122
#define MKTAG(a, b, c, d)
Definition: common.h:238
#define AVFMT_TS_NEGATIVE
Format allows muxing negative timestamps.
Definition: avformat.h:422
This structure stores compressed data.
Definition: avcodec.h:950
int me_subpel_quality
subpel ME quality
Definition: avcodec.h:1503
#define FLAG_KEY_FRAME
Definition: ffm.h:36
int strict_std_compliance
strictly follow the standard (MPEG4, ...).
Definition: avcodec.h:2327
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:966
#define FFM_PACKET_SIZE
Definition: ffm.h:31
int keyint_min
minimum GOP size
Definition: avcodec.h:1690