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 "internal.h"
29 #include "ffm.h"
30 
32 {
33  FFMContext *ffm = s->priv_data;
34  int fill_size, h;
35  AVIOContext *pb = s->pb;
36 
37  fill_size = ffm->packet_end - ffm->packet_ptr;
38  memset(ffm->packet_ptr, 0, fill_size);
39 
40  av_assert1(avio_tell(pb) % ffm->packet_size == 0);
41 
42  /* put header */
43  avio_wb16(pb, PACKET_ID);
44  avio_wb16(pb, fill_size);
45  avio_wb64(pb, ffm->dts);
46  h = ffm->frame_offset;
47  if (ffm->first_packet)
48  h |= 0x8000;
49  avio_wb16(pb, h);
50  avio_write(pb, ffm->packet, ffm->packet_end - ffm->packet);
51  avio_flush(pb);
52 
53  /* prepare next packet */
54  ffm->frame_offset = 0; /* no key frame */
55  ffm->packet_ptr = ffm->packet;
56  ffm->first_packet = 0;
57 }
58 
59 /* 'first' is true if first data of a frame */
61  const uint8_t *buf, int size,
62  int64_t dts, int header)
63 {
64  FFMContext *ffm = s->priv_data;
65  int len;
66 
67  if (header && ffm->frame_offset == 0) {
68  ffm->frame_offset = ffm->packet_ptr - ffm->packet + FFM_HEADER_SIZE;
69  ffm->dts = dts;
70  }
71 
72  /* write as many packets as needed */
73  while (size > 0) {
74  len = ffm->packet_end - ffm->packet_ptr;
75  if (len > size)
76  len = size;
77  memcpy(ffm->packet_ptr, buf, len);
78 
79  ffm->packet_ptr += len;
80  buf += len;
81  size -= len;
82  if (ffm->packet_ptr >= ffm->packet_end)
83  flush_packet(s);
84  }
85 }
86 
87 static void write_header_chunk(AVIOContext *pb, AVIOContext *dpb, unsigned id)
88 {
89  uint8_t *dyn_buf;
90  int dyn_size= avio_close_dyn_buf(dpb, &dyn_buf);
91  avio_wb32(pb, id);
92  avio_wb32(pb, dyn_size);
93  avio_write(pb, dyn_buf, dyn_size);
94  av_free(dyn_buf);
95 }
96 
98 {
99  AVIOContext *tmp;
100  char *buf = NULL;
101  int ret;
102  const AVCodec *enc = ctx->codec ? ctx->codec : avcodec_find_encoder(ctx->codec_id);
103 
104  if (!enc)
105  return AVERROR(EINVAL);
106  if (ctx->priv_data && enc->priv_class && enc->priv_data_size) {
107  if ((ret = av_opt_serialize(ctx->priv_data, AV_OPT_FLAG_ENCODING_PARAM | type,
108  AV_OPT_SERIALIZE_SKIP_DEFAULTS, &buf, '=', ',')) < 0)
109  return ret;
110  if (buf && strlen(buf)) {
111  if (avio_open_dyn_buf(&tmp) < 0) {
112  av_free(buf);
113  return AVERROR(ENOMEM);
114  }
115  avio_put_str(tmp, buf);
116  write_header_chunk(pb, tmp, MKBETAG('C', 'P', 'R', 'V'));
117  }
118  av_free(buf);
119  }
120  return 0;
121 }
122 
123 static int ffm_write_header_codec_ctx(AVIOContext *pb, AVCodecContext *ctx, unsigned tag, int type)
124 {
125  AVIOContext *tmp;
126  char *buf = NULL;
127  uint8_t *p = NULL;
128  int ret, need_coma = 0;
129 
130 #define SKIP_DEFAULTS AV_OPT_SERIALIZE_SKIP_DEFAULTS
131 #define OPT_FLAGS_EXACT AV_OPT_SERIALIZE_OPT_FLAGS_EXACT
132 #define ENC AV_OPT_FLAG_ENCODING_PARAM
133 
134  if (avio_open_dyn_buf(&tmp) < 0)
135  return AVERROR(ENOMEM);
136  if ((ret = av_opt_serialize(ctx, ENC | type, SKIP_DEFAULTS, &buf, '=', ',')) < 0)
137  goto fail;
138  if (buf && strlen(buf)) {
139  avio_write(tmp, buf, strlen(buf));
140  av_free(buf);
141  need_coma = 1;
142  }
143  if ((ret = av_opt_serialize(ctx, 0, SKIP_DEFAULTS | OPT_FLAGS_EXACT, &buf, '=', ',')) < 0)
144  goto fail;
145  if (buf && strlen(buf)) {
146  if (need_coma)
147  avio_w8(tmp, ',');
148  avio_write(tmp, buf, strlen(buf));
149  av_free(buf);
150  }
151  avio_w8(tmp, 0);
152  write_header_chunk(pb, tmp, tag);
153  return 0;
154  fail:
155  av_free(buf);
156  avio_close_dyn_buf(tmp, &p);
157  av_free(p);
158  return ret;
159 
160 #undef SKIP_DEFAULTS
161 #undef OPT_FLAGS_EXACT
162 #undef ENC
163 }
164 
166 {
167  FFMContext *ffm = s->priv_data;
169  AVStream *st;
170  AVIOContext *pb = s->pb;
171  AVCodecContext *codec;
172  int bit_rate, i, ret;
173 
174  if (t = av_dict_get(s->metadata, "creation_time", NULL, 0)) {
175  ret = av_parse_time(&ffm->start_time, t->value, 0);
176  if (ret < 0)
177  return ret;
178  }
179 
181 
182  /* header */
183  avio_wl32(pb, MKTAG('F', 'F', 'M', '2'));
184  avio_wb32(pb, ffm->packet_size);
185  avio_wb64(pb, 0); /* current write position */
186 
187  if(avio_open_dyn_buf(&pb) < 0)
188  return AVERROR(ENOMEM);
189 
190  avio_wb32(pb, s->nb_streams);
191  bit_rate = 0;
192  for(i=0;i<s->nb_streams;i++) {
193  st = s->streams[i];
194  bit_rate += st->codec->bit_rate;
195  }
196  avio_wb32(pb, bit_rate);
197 
198  write_header_chunk(s->pb, pb, MKBETAG('M', 'A', 'I', 'N'));
199 
200  /* list of streams */
201  for(i=0;i<s->nb_streams;i++) {
202  st = s->streams[i];
203  avpriv_set_pts_info(st, 64, 1, 1000000);
204  if(avio_open_dyn_buf(&pb) < 0)
205  return AVERROR(ENOMEM);
206 
207  codec = st->codec;
208  /* generic info */
209  avio_wb32(pb, codec->codec_id);
210  avio_w8(pb, codec->codec_type);
211  avio_wb32(pb, codec->bit_rate);
212  avio_wb32(pb, codec->flags);
213  avio_wb32(pb, codec->flags2);
214  avio_wb32(pb, codec->debug);
215  if (codec->flags & CODEC_FLAG_GLOBAL_HEADER) {
216  avio_wb32(pb, codec->extradata_size);
217  avio_write(pb, codec->extradata, codec->extradata_size);
218  }
219  write_header_chunk(s->pb, pb, MKBETAG('C', 'O', 'M', 'M'));
220  /* specific info */
221  switch(codec->codec_type) {
222  case AVMEDIA_TYPE_VIDEO:
223  if ((ret = ffm_write_header_codec_ctx(s->pb, codec, MKBETAG('S', '2', 'V', 'I'), AV_OPT_FLAG_VIDEO_PARAM)) < 0 ||
225  return ret;
226  break;
227  case AVMEDIA_TYPE_AUDIO:
228  if ((ret = ffm_write_header_codec_ctx(s->pb, codec, MKBETAG('S', '2', 'A', 'U'), AV_OPT_FLAG_AUDIO_PARAM)) < 0 ||
230  return ret;
231  break;
232  default:
233  return -1;
234  }
235  }
236  pb = s->pb;
237 
238  avio_wb64(pb, 0); // end of header
239 
240  /* flush until end of block reached */
241  while ((avio_tell(pb) % ffm->packet_size) != 0)
242  avio_w8(pb, 0);
243 
244  avio_flush(pb);
245 
246  /* init packet mux */
247  ffm->packet_ptr = ffm->packet;
248  ffm->packet_end = ffm->packet + ffm->packet_size - FFM_HEADER_SIZE;
249  av_assert0(ffm->packet_end >= ffm->packet);
250  ffm->frame_offset = 0;
251  ffm->dts = 0;
252  ffm->first_packet = 1;
253 
254  return 0;
255 }
256 
258 {
259  FFMContext *ffm = s->priv_data;
260  int64_t dts;
262  int header_size = FRAME_HEADER_SIZE;
263 
264  dts = ffm->start_time + pkt->dts;
265  /* packet size & key_frame */
266  header[0] = pkt->stream_index;
267  header[1] = 0;
268  if (pkt->flags & AV_PKT_FLAG_KEY)
269  header[1] |= FLAG_KEY_FRAME;
270  AV_WB24(header+2, pkt->size);
271  AV_WB24(header+5, pkt->duration);
272  AV_WB64(header+8, ffm->start_time + pkt->pts);
273  if (pkt->pts != pkt->dts) {
274  header[1] |= FLAG_DTS;
275  AV_WB32(header+16, pkt->pts - pkt->dts);
276  header_size += 4;
277  }
278  ffm_write_data(s, header, header_size, dts, 1);
279  ffm_write_data(s, pkt->data, pkt->size, dts, 0);
280 
281  return 0;
282 }
283 
285 {
286  FFMContext *ffm = s->priv_data;
287 
288  /* flush packets */
289  if (ffm->packet_ptr > ffm->packet)
290  flush_packet(s);
291 
292  return 0;
293 }
294 
296  .name = "ffm",
297  .long_name = NULL_IF_CONFIG_SMALL("FFM (FFserver live feed)"),
298  .extensions = "ffm",
299  .priv_data_size = sizeof(FFMContext),
300  .audio_codec = AV_CODEC_ID_MP2,
301  .video_codec = AV_CODEC_ID_MPEG1VIDEO,
306 };