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 "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;
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 };