FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 "libavutil/opt.h"
27 #include "avformat.h"
28 #include "avio_internal.h"
29 #include "internal.h"
30 #include "ffm.h"
31 
33 {
34  FFMContext *ffm = s->priv_data;
35  int fill_size, h;
36  AVIOContext *pb = s->pb;
37 
38  fill_size = ffm->packet_end - ffm->packet_ptr;
39  memset(ffm->packet_ptr, 0, fill_size);
40 
41  av_assert1(avio_tell(pb) % ffm->packet_size == 0);
42 
43  /* put header */
44  avio_wb16(pb, PACKET_ID);
45  avio_wb16(pb, fill_size);
46  avio_wb64(pb, ffm->dts);
47  h = ffm->frame_offset;
48  if (ffm->first_packet)
49  h |= 0x8000;
50  avio_wb16(pb, h);
51  avio_write(pb, ffm->packet, ffm->packet_end - ffm->packet);
52  avio_flush(pb);
53 
54  /* prepare next packet */
55  ffm->frame_offset = 0; /* no key frame */
56  ffm->packet_ptr = ffm->packet;
57  ffm->first_packet = 0;
58 }
59 
60 /* 'first' is true if first data of a frame */
62  const uint8_t *buf, int size,
63  int64_t dts, int header)
64 {
65  FFMContext *ffm = s->priv_data;
66  int len;
67 
68  if (header && ffm->frame_offset == 0) {
69  ffm->frame_offset = ffm->packet_ptr - ffm->packet + FFM_HEADER_SIZE;
70  ffm->dts = dts;
71  }
72 
73  /* write as many packets as needed */
74  while (size > 0) {
75  len = ffm->packet_end - ffm->packet_ptr;
76  if (len > size)
77  len = size;
78  memcpy(ffm->packet_ptr, buf, len);
79 
80  ffm->packet_ptr += len;
81  buf += len;
82  size -= len;
83  if (ffm->packet_ptr >= ffm->packet_end)
84  flush_packet(s);
85  }
86 }
87 
88 static void write_header_chunk(AVIOContext *pb, AVIOContext *dpb, unsigned id)
89 {
90  uint8_t *dyn_buf;
91  int dyn_size= avio_close_dyn_buf(dpb, &dyn_buf);
92  avio_wb32(pb, id);
93  avio_wb32(pb, dyn_size);
94  avio_write(pb, dyn_buf, dyn_size);
95  av_free(dyn_buf);
96 }
97 
99 {
100  AVIOContext *pb = s->pb;
101  AVIOContext *tmp;
102  char *buf = NULL;
103  int ret;
104  const AVCodec *enc = ctx->codec ? ctx->codec : avcodec_find_encoder(ctx->codec_id);
105 
106  if (!enc) {
107  av_log(s, AV_LOG_WARNING, "Stream codec is not found. Codec private options are not stored.\n");
108  return 0;
109  }
110  if (ctx->priv_data && enc->priv_class && enc->priv_data_size) {
111  if ((ret = av_opt_serialize(ctx->priv_data, AV_OPT_FLAG_ENCODING_PARAM | type,
112  AV_OPT_SERIALIZE_SKIP_DEFAULTS, &buf, '=', ',')) < 0)
113  return ret;
114  if (buf && strlen(buf)) {
115  if (avio_open_dyn_buf(&tmp) < 0) {
116  av_free(buf);
117  return AVERROR(ENOMEM);
118  }
119  avio_put_str(tmp, buf);
120  write_header_chunk(pb, tmp, MKBETAG('C', 'P', 'R', 'V'));
121  }
122  av_free(buf);
123  }
124  return 0;
125 }
126 
127 static int ffm_write_header_codec_ctx(AVIOContext *pb, AVCodecContext *ctx, unsigned tag, int type)
128 {
129  AVIOContext *tmp;
130  char *buf = NULL;
131  int ret, need_coma = 0;
132 
133 #define SKIP_DEFAULTS AV_OPT_SERIALIZE_SKIP_DEFAULTS
134 #define OPT_FLAGS_EXACT AV_OPT_SERIALIZE_OPT_FLAGS_EXACT
135 #define ENC AV_OPT_FLAG_ENCODING_PARAM
136 
137  if (avio_open_dyn_buf(&tmp) < 0)
138  return AVERROR(ENOMEM);
139  if ((ret = av_opt_serialize(ctx, ENC | type, SKIP_DEFAULTS, &buf, '=', ',')) < 0)
140  goto fail;
141  if (buf && strlen(buf)) {
142  avio_write(tmp, buf, strlen(buf));
143  av_freep(&buf);
144  need_coma = 1;
145  }
146  if ((ret = av_opt_serialize(ctx, 0, SKIP_DEFAULTS | OPT_FLAGS_EXACT, &buf, '=', ',')) < 0)
147  goto fail;
148  if (buf && strlen(buf)) {
149  if (need_coma)
150  avio_w8(tmp, ',');
151  avio_write(tmp, buf, strlen(buf));
152  }
153  av_freep(&buf);
154  avio_w8(tmp, 0);
155  write_header_chunk(pb, tmp, tag);
156  return 0;
157  fail:
158  av_free(buf);
159  ffio_free_dyn_buf(&tmp);
160  return ret;
161 
162 #undef SKIP_DEFAULTS
163 #undef OPT_FLAGS_EXACT
164 #undef ENC
165 }
166 
168  const char *configuration)
169 {
170  int ret;
171  const AVCodec *enc = ctx->codec ? ctx->codec : avcodec_find_encoder(ctx->codec_id);
172  AVIOContext *tmp;
173  AVDictionaryEntry *t = NULL;
174  AVDictionary *all = NULL, *comm = NULL, *prv = NULL;
175  char *buf = NULL;
176 
177  if (!enc || !enc->priv_class || !enc->priv_data_size) {
178  /* codec is not known/has no private options, so save everything as common options */
179  if (avio_open_dyn_buf(&tmp) < 0)
180  return AVERROR(ENOMEM);
181  avio_put_str(tmp, configuration);
182  write_header_chunk(pb, tmp, tag);
183  return 0;
184  }
185 
186  if ((ret = av_dict_parse_string(&all, configuration, "=", ",", 0)) < 0)
187  return ret;
188 
189  while ((t = av_dict_get(all, "", t, AV_DICT_IGNORE_SUFFIX))) {
190  if (av_opt_find((void *)&enc->priv_class, t->key, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ)) {
191  if ((ret = av_dict_set(&prv, t->key, t->value, 0)) < 0)
192  goto fail;
193  } else if ((ret = av_dict_set(&comm, t->key, t->value, 0)) < 0)
194  goto fail;
195  }
196 
197  if (comm) {
198  if ((ret = av_dict_get_string(comm, &buf, '=', ',')) < 0 ||
199  (ret = avio_open_dyn_buf(&tmp)) < 0)
200  goto fail;
201  avio_put_str(tmp, buf);
202  av_freep(&buf);
203  write_header_chunk(pb, tmp, tag);
204  }
205  if (prv) {
206  if ((ret = av_dict_get_string(prv, &buf, '=', ',')) < 0 ||
207  (ret = avio_open_dyn_buf(&tmp)) < 0)
208  goto fail;
209  avio_put_str(tmp, buf);
210  write_header_chunk(pb, tmp, MKBETAG('C', 'P', 'R', 'V'));
211  }
212 
213  fail:
214  av_free(buf);
215  av_dict_free(&all);
216  av_dict_free(&comm);
217  av_dict_free(&prv);
218  return ret;
219 }
220 
222 {
223  FFMContext *ffm = s->priv_data;
225  AVStream *st;
226  AVIOContext *pb = s->pb;
227  AVCodecContext *codec;
228  int bit_rate, i, ret;
229 
230  if (t = av_dict_get(s->metadata, "creation_time", NULL, 0)) {
231  ret = av_parse_time(&ffm->start_time, t->value, 0);
232  if (ret < 0)
233  return ret;
234  }
235 
237 
238  /* header */
239  avio_wl32(pb, MKTAG('F', 'F', 'M', '2'));
240  avio_wb32(pb, ffm->packet_size);
241  avio_wb64(pb, 0); /* current write position */
242 
243  if(avio_open_dyn_buf(&pb) < 0)
244  return AVERROR(ENOMEM);
245 
246  avio_wb32(pb, s->nb_streams);
247  bit_rate = 0;
248  for(i=0;i<s->nb_streams;i++) {
249  st = s->streams[i];
250  bit_rate += st->codec->bit_rate;
251  }
252  avio_wb32(pb, bit_rate);
253 
254  write_header_chunk(s->pb, pb, MKBETAG('M', 'A', 'I', 'N'));
255 
256  /* list of streams */
257  for(i=0;i<s->nb_streams;i++) {
258  st = s->streams[i];
259  avpriv_set_pts_info(st, 64, 1, 1000000);
260  if(avio_open_dyn_buf(&pb) < 0)
261  return AVERROR(ENOMEM);
262 
263  codec = st->codec;
264  /* generic info */
265  avio_wb32(pb, codec->codec_id);
266  avio_w8(pb, codec->codec_type);
267  avio_wb32(pb, codec->bit_rate);
268  avio_wb32(pb, codec->flags);
269  avio_wb32(pb, codec->flags2);
270  avio_wb32(pb, codec->debug);
271  if (codec->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
272  avio_wb32(pb, codec->extradata_size);
273  avio_write(pb, codec->extradata, codec->extradata_size);
274  }
275  write_header_chunk(s->pb, pb, MKBETAG('C', 'O', 'M', 'M'));
276  /* specific info */
277  switch(codec->codec_type) {
278  case AVMEDIA_TYPE_VIDEO:
280  av_log(NULL, AV_LOG_DEBUG, "writing recommended configuration: %s\n",
282  if ((ret = ffm_write_recommended_config(s->pb, codec, MKBETAG('S', '2', 'V', 'I'),
284  return ret;
285  } else if ((ret = ffm_write_header_codec_ctx(s->pb, codec, MKBETAG('S', '2', 'V', 'I'), AV_OPT_FLAG_VIDEO_PARAM)) < 0 ||
287  return ret;
288  break;
289  case AVMEDIA_TYPE_AUDIO:
291  av_log(NULL, AV_LOG_DEBUG, "writing recommended configuration: %s\n",
293  if ((ret = ffm_write_recommended_config(s->pb, codec, MKBETAG('S', '2', 'A', 'U'),
295  return ret;
296  } else if ((ret = ffm_write_header_codec_ctx(s->pb, codec, MKBETAG('S', '2', 'A', 'U'), AV_OPT_FLAG_AUDIO_PARAM)) < 0 ||
298  return ret;
299  break;
300  default:
301  return -1;
302  }
303  }
304  pb = s->pb;
305 
306  avio_wb64(pb, 0); // end of header
307 
308  /* flush until end of block reached */
309  while ((avio_tell(pb) % ffm->packet_size) != 0)
310  avio_w8(pb, 0);
311 
312  avio_flush(pb);
313 
314  /* init packet mux */
315  ffm->packet_ptr = ffm->packet;
316  ffm->packet_end = ffm->packet + ffm->packet_size - FFM_HEADER_SIZE;
317  av_assert0(ffm->packet_end >= ffm->packet);
318  ffm->frame_offset = 0;
319  ffm->dts = 0;
320  ffm->first_packet = 1;
321 
322  return 0;
323 }
324 
326 {
327  FFMContext *ffm = s->priv_data;
328  int64_t dts;
330  int header_size = FRAME_HEADER_SIZE;
331 
332  dts = ffm->start_time + pkt->dts;
333  /* packet size & key_frame */
334  header[0] = pkt->stream_index;
335  header[1] = 0;
336  if (pkt->flags & AV_PKT_FLAG_KEY)
337  header[1] |= FLAG_KEY_FRAME;
338  AV_WB24(header+2, pkt->size);
339  AV_WB24(header+5, pkt->duration);
340  AV_WB64(header+8, ffm->start_time + pkt->pts);
341  if (pkt->pts != pkt->dts) {
342  header[1] |= FLAG_DTS;
343  AV_WB32(header+16, pkt->pts - pkt->dts);
344  header_size += 4;
345  }
346  ffm_write_data(s, header, header_size, dts, 1);
347  ffm_write_data(s, pkt->data, pkt->size, dts, 0);
348 
349  return 0;
350 }
351 
353 {
354  FFMContext *ffm = s->priv_data;
355 
356  /* flush packets */
357  if (ffm->packet_ptr > ffm->packet)
358  flush_packet(s);
359 
360  return 0;
361 }
362 
364  .name = "ffm",
365  .long_name = NULL_IF_CONFIG_SMALL("FFM (FFserver live feed)"),
366  .extensions = "ffm",
367  .priv_data_size = sizeof(FFMContext),
368  .audio_codec = AV_CODEC_ID_MP2,
369  .video_codec = AV_CODEC_ID_MPEG1VIDEO,
374 };
void avio_wb64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:411
#define NULL
Definition: coverity.c:32
const struct AVCodec * codec
Definition: avcodec.h:1511
const char * s
Definition: avisynth_c.h:631
Bytestream IO Context.
Definition: avio.h:111
#define PACKET_ID
Definition: ffm.h:32
int64_t dts
Definition: ffm.h:54
char * recommended_encoder_configuration
String containing paris of key and values describing recommended encoder configuration.
Definition: avformat.h:1173
AVCodec * avcodec_find_encoder(enum AVCodecID id)
Find a registered encoder with a matching codec ID.
Definition: utils.c:2997
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:1146
int frame_offset
Definition: ffm.h:53
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
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:4083
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:558
#define AV_OPT_FLAG_AUDIO_PARAM
Definition: opt.h:290
int size
Definition: avcodec.h:1424
int av_dict_get_string(const AVDictionary *m, char **buffer, const char key_val_sep, const char pairs_sep)
Get dictionary entries as a string.
Definition: dict.c:221
static AVPacket pkt
int av_opt_serialize(void *obj, int opt_flags, int flags, char **buffer, const char key_val_sep, const char pairs_sep)
Serialize object's options.
Definition: opt.c:1846
#define FLAG_DTS
Definition: ffm.h:37
AVCodec.
Definition: avcodec.h:3472
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1134
#define FFM_HEADER_SIZE
Definition: ffm.h:30
Format I/O context.
Definition: avformat.h:1273
#define AV_WB64(p, v)
Definition: intreadwrite.h:433
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
#define OPT_FLAGS_EXACT
static void write_header_chunk(AVIOContext *pb, AVIOContext *dpb, unsigned id)
Definition: ffmenc.c:88
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:319
uint8_t
static void ffm_write_data(AVFormatContext *s, const uint8_t *buf, int size, int64_t dts, int header)
Definition: ffmenc.c:61
AVOptions.
#define FRAME_HEADER_SIZE
Definition: cpia.c:30
uint8_t * packet_end
Definition: ffm.h:55
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1617
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1341
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:39
uint8_t * data
Definition: avcodec.h:1423
uint32_t tag
Definition: movenc.c:1334
#define ENC
ptrdiff_t size
Definition: opengl_enc.c:101
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:390
static const uint8_t header[24]
Definition: sdr2.c:67
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:178
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1441
#define av_log(a,...)
#define AV_OPT_FLAG_ENCODING_PARAM
a generic parameter which can be set by the user for muxing or encoding
Definition: opt.h:285
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1469
static int ffm_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: ffmenc.c:325
uint8_t * packet_ptr
Definition: ffm.h:55
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1485
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:175
static int ffm_write_header_codec_ctx(AVIOContext *pb, AVCodecContext *ctx, unsigned tag, int type)
Definition: ffmenc.c:127
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values. ...
Definition: dict.c:199
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:1597
simple assert() macros that are a bit more flexible than ISO C assert().
static int ffm_write_recommended_config(AVIOContext *pb, AVCodecContext *ctx, unsigned tag, const char *configuration)
Definition: ffmenc.c:167
#define fail()
Definition: checkasm.h:57
int first_packet
Definition: ffm.h:51
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1429
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:861
static int ffm_write_trailer(AVFormatContext *s)
Definition: ffmenc.c:352
const AVOption * av_opt_find(void *obj, const char *name, const char *unit, int opt_flags, int search_flags)
Look for an option in an object.
Definition: opt.c:1481
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1329
static void flush_packet(AVFormatContext *s)
Definition: ffmenc.c:32
int bit_rate
the average bitrate
Definition: avcodec.h:1567
int void avio_flush(AVIOContext *s)
Force flushing of buffered data.
Definition: aviobuf.c:198
#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:221
int64_t start_time
Definition: ffm.h:57
int priv_data_size
Definition: avcodec.h:3508
static int write_trailer(AVFormatContext *s1)
Definition: v4l2enc.c:94
const char * name
Definition: avformat.h:513
#define SKIP_DEFAULTS
#define AV_WB24(p, d)
Definition: intreadwrite.h:450
int packet_size
Definition: ffm.h:52
void ffio_free_dyn_buf(AVIOContext **s)
Free a dynamic buffer.
Definition: aviobuf.c:1174
int avio_put_str(AVIOContext *s, const char *str)
Write a NULL-terminated string.
Definition: aviobuf.c:335
Stream structure.
Definition: avformat.h:842
int av_dict_parse_string(AVDictionary **pm, const char *str, const char *key_val_sep, const char *pairs_sep, int flags)
Parse the key/value pairs list and add the parsed entries to a dictionary.
Definition: dict.c:176
enum AVMediaType codec_type
Definition: avcodec.h:1510
enum AVCodecID codec_id
Definition: avcodec.h:1519
AVIOContext * pb
I/O context.
Definition: avformat.h:1315
int debug
debug
Definition: avcodec.h:2842
Definition: ffm.h:44
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:156
#define AV_OPT_FLAG_VIDEO_PARAM
Definition: opt.h:291
main external API structure.
Definition: avcodec.h:1502
void * buf
Definition: avisynth_c.h:553
GLint GLenum type
Definition: opengl_enc.c:105
int extradata_size
Definition: avcodec.h:1618
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:69
#define AV_WB32(p, v)
Definition: intreadwrite.h:419
misc parsing utilities
AVOutputFormat ff_ffm_muxer
Definition: ffmenc.c:363
void avio_wb16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:423
static int flags
Definition: cpu.c:47
const AVClass * priv_class
AVClass for the private context.
Definition: avcodec.h:3498
#define AV_CODEC_FLAG_GLOBAL_HEADER
Place global headers in extradata instead of every keyframe.
Definition: avcodec.h:784
Main libavformat public API header.
uint8_t packet[FFM_PACKET_SIZE]
Definition: ffm.h:56
#define AV_OPT_SEARCH_FAKE_OBJ
The obj passed to av_opt_find() is fake – only a double pointer to AVClass instead of a required poin...
Definition: opt.h:620
char * key
Definition: dict.h:87
#define MKBETAG(a, b, c, d)
Definition: common.h:331
void * priv_data
Definition: avcodec.h:1544
#define av_free(p)
#define AV_OPT_SERIALIZE_SKIP_DEFAULTS
Serialize options that are not set to default values only.
Definition: opt.h:884
char * value
Definition: dict.h:88
int len
void * priv_data
Format private data.
Definition: avformat.h:1301
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:493
int flags2
AV_CODEC_FLAG2_*.
Definition: avcodec.h:1604
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1422
void avio_wb32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:327
#define av_freep(p)
#define AV_DICT_IGNORE_SUFFIX
Return first entry in a dictionary whose first part corresponds to the search key, ignoring the suffix of the found key string.
Definition: dict.h:72
static int ffm_write_header_codec_private_ctx(AVFormatContext *s, AVCodecContext *ctx, int type)
Definition: ffmenc.c:98
int stream_index
Definition: avcodec.h:1425
#define MKTAG(a, b, c, d)
Definition: common.h:330
#define AVFMT_TS_NEGATIVE
Format allows muxing negative timestamps.
Definition: avformat.h:490
This structure stores compressed data.
Definition: avcodec.h:1400
static int write_packet(AVFormatContext *s1, AVPacket *pkt)
Definition: v4l2enc.c:86
#define FLAG_KEY_FRAME
Definition: ffm.h:36
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1416
#define FFM_PACKET_SIZE
Definition: ffm.h:31