Libav
segment.c
Go to the documentation of this file.
1 /*
2  * Generic segmenter
3  * Copyright (c) 2011, Luca Barbato
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 <float.h>
23 
24 #include "avformat.h"
25 #include "internal.h"
26 
27 #include "libavutil/log.h"
28 #include "libavutil/opt.h"
29 #include "libavutil/avstring.h"
30 #include "libavutil/parseutils.h"
31 #include "libavutil/mathematics.h"
32 
33 typedef struct SegmentContext {
34  const AVClass *class;
35  int number;
38  char *format;
39  char *list;
40  char *entry_prefix;
41  int list_type;
42  float time;
43  int size;
44  int wrap;
47  int64_t offset_time;
48  int64_t recording_time;
49  int has_video;
52 
53 enum {
56 };
57 
59 {
60  SegmentContext *seg = s->priv_data;
61  AVFormatContext *oc;
62  int i;
63 
64  seg->avf = oc = avformat_alloc_context();
65  if (!oc)
66  return AVERROR(ENOMEM);
67 
68  oc->oformat = seg->oformat;
70  oc->opaque = s->opaque;
71  oc->io_close = s->io_close;
72  oc->io_open = s->io_open;
73 
74  for (i = 0; i < s->nb_streams; i++) {
75  AVStream *st;
76  if (!(st = avformat_new_stream(oc, NULL)))
77  return AVERROR(ENOMEM);
80  st->time_base = s->streams[i]->time_base;
81  }
82 
83  return 0;
84 }
85 
86 static int segment_hls_window(AVFormatContext *s, int last)
87 {
88  SegmentContext *seg = s->priv_data;
89  int i, ret = 0;
90  char buf[1024];
91 
92  if ((ret = s->io_open(s, &seg->pb, seg->list, AVIO_FLAG_WRITE, NULL)) < 0)
93  goto fail;
94 
95  avio_printf(seg->pb, "#EXTM3U\n");
96  avio_printf(seg->pb, "#EXT-X-VERSION:3\n");
97  avio_printf(seg->pb, "#EXT-X-TARGETDURATION:%d\n", (int)seg->time);
98  avio_printf(seg->pb, "#EXT-X-MEDIA-SEQUENCE:%d\n",
99  FFMAX(0, seg->number - seg->size));
100 
101  av_log(s, AV_LOG_VERBOSE, "EXT-X-MEDIA-SEQUENCE:%d\n",
102  FFMAX(0, seg->number - seg->size));
103 
104  for (i = FFMAX(0, seg->number - seg->size);
105  i < seg->number; i++) {
106  avio_printf(seg->pb, "#EXTINF:%d,\n", (int)seg->time);
107  if (seg->entry_prefix) {
108  avio_printf(seg->pb, "%s", seg->entry_prefix);
109  }
110  ret = av_get_frame_filename(buf, sizeof(buf), s->filename, i);
111  if (ret < 0) {
112  ret = AVERROR(EINVAL);
113  goto fail;
114  }
115  avio_printf(seg->pb, "%s\n", buf);
116  }
117 
118  if (last)
119  avio_printf(seg->pb, "#EXT-X-ENDLIST\n");
120 fail:
121  ff_format_io_close(s, &seg->pb);
122 
123  return ret;
124 }
125 
127 {
128  SegmentContext *c = s->priv_data;
129  AVFormatContext *oc = c->avf;
130  int err = 0;
131 
132  if (write_header) {
134  c->avf = NULL;
135  if ((err = segment_mux_init(s)) < 0)
136  return err;
137  oc = c->avf;
138  }
139 
140  if (c->wrap)
141  c->number %= c->wrap;
142 
143  if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
144  s->filename, c->number++) < 0)
145  return AVERROR(EINVAL);
146 
147  if ((err = s->io_open(s, &oc->pb, oc->filename, AVIO_FLAG_WRITE, NULL)) < 0)
148  return err;
149 
150  if (oc->oformat->priv_class && oc->priv_data)
151  av_opt_set(oc->priv_data, "resend_headers", "1", 0); /* mpegts specific */
152 
153  if (write_header) {
154  if ((err = avformat_write_header(oc, NULL)) < 0)
155  return err;
156  }
157 
158  return 0;
159 }
160 
162 {
163  int ret = 0;
164 
165  av_write_frame(oc, NULL); /* Flush any buffered data (fragmented mp4) */
166  if (write_trailer)
167  av_write_trailer(oc);
168  ff_format_io_close(oc, &oc->pb);
169 
170  return ret;
171 }
172 
174 {
175  int buf_size = 32768;
176  uint8_t *buf = av_malloc(buf_size);
177  if (!buf)
178  return AVERROR(ENOMEM);
179  *ctx = avio_alloc_context(buf, buf_size, AVIO_FLAG_WRITE, NULL, NULL, NULL, NULL);
180  if (!*ctx) {
181  av_free(buf);
182  return AVERROR(ENOMEM);
183  }
184  return 0;
185 }
186 
188 {
189  av_free(pb->buffer);
190  av_free(pb);
191 }
192 
194 {
195  ff_format_io_close(seg->avf, &seg->pb);
197  seg->avf = NULL;
198 }
199 
201 {
202  SegmentContext *seg = s->priv_data;
203  AVFormatContext *oc = NULL;
204  int ret, i;
205 
206  seg->number = 0;
207  seg->offset_time = 0;
208  seg->recording_time = seg->time * 1000000;
209  if (!seg->write_header_trailer)
210  seg->individual_header_trailer = 0;
211 
212  if (seg->list && seg->list_type != LIST_HLS)
213  if ((ret = s->io_open(s, &seg->pb, seg->list, AVIO_FLAG_WRITE, NULL)) < 0)
214  goto fail;
215 
216  for (i = 0; i < s->nb_streams; i++)
217  seg->has_video +=
219 
220  if (seg->has_video > 1)
222  "More than a single video stream present, "
223  "expect issues decoding it.\n");
224 
225  seg->oformat = av_guess_format(seg->format, s->filename, NULL);
226 
227  if (!seg->oformat) {
229  goto fail;
230  }
231  if (seg->oformat->flags & AVFMT_NOFILE) {
232  av_log(s, AV_LOG_ERROR, "format %s not supported.\n",
233  seg->oformat->name);
234  ret = AVERROR(EINVAL);
235  goto fail;
236  }
237 
238  if ((ret = segment_mux_init(s)) < 0)
239  goto fail;
240  oc = seg->avf;
241 
242  if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
243  s->filename, seg->number++) < 0) {
244  ret = AVERROR(EINVAL);
245  goto fail;
246  }
247 
248  if (seg->write_header_trailer) {
249  if ((ret = s->io_open(s, &oc->pb, oc->filename, AVIO_FLAG_WRITE, NULL)) < 0)
250  goto fail;
251  } else {
252  if ((ret = open_null_ctx(&oc->pb)) < 0)
253  goto fail;
254  }
255 
256  if ((ret = avformat_write_header(oc, NULL)) < 0) {
257  ff_format_io_close(oc, &oc->pb);
258  goto fail;
259  }
260 
261  if (!seg->write_header_trailer) {
262  close_null_ctx(oc->pb);
263  if ((ret = s->io_open(s, &oc->pb, oc->filename, AVIO_FLAG_WRITE, NULL)) < 0)
264  goto fail;
265  }
266 
267  if (seg->list) {
268  if (seg->list_type == LIST_HLS) {
269  if ((ret = segment_hls_window(s, 0)) < 0)
270  goto fail;
271  } else {
272  avio_printf(seg->pb, "%s\n", oc->filename);
273  avio_flush(seg->pb);
274  }
275  }
276 
277 fail:
278  if (ret < 0)
279  seg_free_context(seg);
280 
281  return ret;
282 }
283 
285 {
286  SegmentContext *seg = s->priv_data;
287  AVFormatContext *oc = seg->avf;
288  AVStream *st = s->streams[pkt->stream_index];
289  int64_t end_pts = seg->recording_time * seg->number;
290  int ret, can_split = 1;
291 
292  if (!oc)
293  return AVERROR(EINVAL);
294 
295  if (seg->has_video) {
296  can_split = st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
297  pkt->flags & AV_PKT_FLAG_KEY;
298  }
299 
300  if (can_split && av_compare_ts(pkt->pts, st->time_base, end_pts,
301  AV_TIME_BASE_Q) >= 0) {
302  av_log(s, AV_LOG_DEBUG, "Next segment starts at %d %"PRId64"\n",
303  pkt->stream_index, pkt->pts);
304 
305  ret = segment_end(oc, seg->individual_header_trailer);
306 
307  if (!ret)
309 
310  if (ret)
311  goto fail;
312 
313  oc = seg->avf;
314 
315  if (seg->list) {
316  if (seg->list_type == LIST_HLS) {
317  if ((ret = segment_hls_window(s, 0)) < 0)
318  goto fail;
319  } else {
320  avio_printf(seg->pb, "%s\n", oc->filename);
321  avio_flush(seg->pb);
322  if (seg->size && !(seg->number % seg->size)) {
323  ff_format_io_close(s, &seg->pb);
324  if ((ret = s->io_open(s, &seg->pb, seg->list,
325  AVIO_FLAG_WRITE, NULL)) < 0)
326  goto fail;
327  }
328  }
329  }
330  }
331 
332  ret = ff_write_chained(oc, pkt->stream_index, pkt, s);
333 
334 fail:
335  if (ret < 0)
336  seg_free_context(seg);
337 
338  return ret;
339 }
340 
341 static int seg_write_trailer(struct AVFormatContext *s)
342 {
343  SegmentContext *seg = s->priv_data;
344  AVFormatContext *oc = seg->avf;
345  int ret = 0;
346 
347  if (!oc)
348  goto fail;
349 
350  if (!seg->write_header_trailer) {
351  if ((ret = segment_end(oc, 0)) < 0)
352  goto fail;
353  if ((ret = open_null_ctx(&oc->pb)) < 0)
354  goto fail;
355  ret = av_write_trailer(oc);
356  close_null_ctx(oc->pb);
357  } else {
358  ret = segment_end(oc, 1);
359  }
360 
361  if (ret < 0)
362  goto fail;
363 
364  if (seg->list && seg->list_type == LIST_HLS) {
365  if ((ret = segment_hls_window(s, 1) < 0))
366  goto fail;
367  }
368 
369 fail:
370  ff_format_io_close(s, &seg->pb);
372  return ret;
373 }
374 
375 #define OFFSET(x) offsetof(SegmentContext, x)
376 #define E AV_OPT_FLAG_ENCODING_PARAM
377 static const AVOption options[] = {
378  { "segment_format", "container format used for the segments", OFFSET(format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
379  { "segment_time", "segment length in seconds", OFFSET(time), AV_OPT_TYPE_FLOAT, {.dbl = 2}, 0, FLT_MAX, E },
380  { "segment_list", "output the segment list", OFFSET(list), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
381  { "segment_list_size", "maximum number of playlist entries", OFFSET(size), AV_OPT_TYPE_INT, {.i64 = 5}, 0, INT_MAX, E },
382  { "segment_list_type", "segment list format", OFFSET(list_type), AV_OPT_TYPE_INT, {.i64 = LIST_FLAT}, 0, 2, E, "list_type" },
383  { "flat", "plain list (default)", 0, AV_OPT_TYPE_CONST, {.i64 = LIST_FLAT}, 0, 0, E, "list_type" },
384  { "hls", "Apple HTTP Live Streaming compatible", 0, AV_OPT_TYPE_CONST, {.i64 = LIST_HLS}, 0, 0, E, "list_type" },
385  { "segment_wrap", "number after which the index wraps", OFFSET(wrap), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E },
386  { "segment_list_entry_prefix", "base url prefix for segments", OFFSET(entry_prefix), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
387  { "individual_header_trailer", "write header/trailer to each segment", OFFSET(individual_header_trailer), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, E },
388  { "write_header_trailer", "write a header to the first segment and a trailer to the last one", OFFSET(write_header_trailer), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, E },
389  { NULL },
390 };
391 
392 static const AVClass seg_class = {
393  .class_name = "segment muxer",
394  .item_name = av_default_item_name,
395  .option = options,
396  .version = LIBAVUTIL_VERSION_INT,
397 };
398 
399 
401  .name = "segment",
402  .long_name = NULL_IF_CONFIG_SMALL("segment"),
403  .priv_data_size = sizeof(SegmentContext),
404  .flags = AVFMT_NOFILE,
408  .priv_class = &seg_class,
409 };
int(* io_open)(struct AVFormatContext *s, AVIOContext **pb, const char *url, int flags, AVDictionary **options)
A callback for opening new IO streams.
Definition: avformat.h:1270
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
AVFormatContext * avf
Definition: segment.c:37
Bytestream IO Context.
Definition: avio.h:104
AVIOInterruptCB interrupt_callback
Custom interrupt callbacks for the I/O layer.
Definition: avformat.h:1181
AVOption.
Definition: opt.h:234
int avformat_write_header(AVFormatContext *s, AVDictionary **options)
Allocate the stream private data and write the stream header to an output media file.
Definition: mux.c:252
int av_write_frame(AVFormatContext *s, AVPacket *pkt)
Write a packet to an output media file.
Definition: mux.c:470
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:130
char * entry_prefix
Set by a private option.
Definition: segment.c:40
static int segment_start(AVFormatContext *s, int write_header)
Definition: segment.c:126
int size
Set by a private option.
Definition: segment.c:43
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:770
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
#define AVIO_FLAG_WRITE
write-only
Definition: avio.h:369
unsigned char * buffer
Start of the buffer.
Definition: avio.h:118
static int seg_write_header(AVFormatContext *s)
Definition: segment.c:200
static void close_null_ctx(AVIOContext *pb)
Definition: segment.c:187
static int segment_mux_init(AVFormatContext *s)
Definition: segment.c:58
Format I/O context.
Definition: avformat.h:940
static int segment_hls_window(AVFormatContext *s, int last)
Definition: segment.c:86
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:39
AVIOContext * pb
Definition: segment.c:50
int flags
can use flags: AVFMT_NOFILE, AVFMT_NEEDNUMBER, AVFMT_GLOBALHEADER, AVFMT_NOTIMESTAMPS, AVFMT_VARIABLE_FPS, AVFMT_NODIMENSIONS, AVFMT_NOSTREAMS, AVFMT_ALLOW_FLUSH, AVFMT_TS_NONSTRICT, AVFMT_TS_NEGATIVE
Definition: avformat.h:469
uint8_t
int wrap
Set by a private option.
Definition: segment.c:44
AVOptions.
void ff_format_io_close(AVFormatContext *s, AVIOContext **pb)
A wrapper around AVFormatContext.io_close that should be used instead of calling the pointer directly...
Definition: utils.c:3317
static int seg_write_trailer(struct AVFormatContext *s)
Definition: segment.c:341
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2648
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1008
AVFormatContext * avformat_alloc_context(void)
Allocate an AVFormatContext.
Definition: options.c:136
int64_t offset_time
Definition: segment.c:47
static int flags
Definition: log.c:50
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:140
char * format
Set by a private option.
Definition: segment.c:38
static int write_trailer(AVFormatContext *s)
Definition: assenc.c:64
struct AVOutputFormat * oformat
The output container format.
Definition: avformat.h:959
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1378
AVIOContext * avio_alloc_context(unsigned char *buffer, int buffer_size, int write_flag, void *opaque, int(*read_packet)(void *opaque, uint8_t *buf, int buf_size), int(*write_packet)(void *opaque, uint8_t *buf, int buf_size), int64_t(*seek)(void *opaque, int64_t offset, int whence))
Allocate and initialize an AVIOContext for buffered I/O.
Definition: aviobuf.c:151
int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
Copy the contents of src to dst.
Definition: utils.c:2819
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:190
int individual_header_trailer
Set by a private option.
Definition: segment.c:45
#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:148
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:145
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3479
#define FFMAX(a, b)
Definition: common.h:64
#define fail()
Definition: checkasm.h:80
#define OFFSET(x)
Definition: segment.c:375
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1352
int av_compare_ts(int64_t ts_a, AVRational tb_a, int64_t ts_b, AVRational tb_b)
Compare 2 timestamps each in its own timebases.
Definition: mathematics.c:104
void * opaque
Arbitrary user data set by the caller.
Definition: avformat.h:1248
float time
Set by a private option.
Definition: segment.c:42
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:996
int void avio_flush(AVIOContext *s)
Definition: aviobuf.c:236
char filename[1024]
input or output filename
Definition: avformat.h:1016
static int segment_end(AVFormatContext *oc, int write_trailer)
Definition: segment.c:161
const char * name
Definition: avformat.h:450
AVFormatContext * ctx
Definition: movenc.c:48
AVOutputFormat * av_guess_format(const char *short_name, const char *filename, const char *mime_type)
Return the output format in the list of registered output formats which best matches the provided par...
Definition: format.c:104
const AVClass * priv_class
AVClass for the private context.
Definition: avformat.h:478
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
int av_get_frame_filename(char *buf, int buf_size, const char *path, int number)
Return in &#39;buf&#39; the path with &#39;d&#39; replaced by a number.
Definition: utils.c:2817
static int seg_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: segment.c:284
Stream structure.
Definition: avformat.h:705
char * list
Set by a private option.
Definition: segment.c:39
NULL
Definition: eval.c:55
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:247
AVIOContext * pb
I/O context.
Definition: avformat.h:982
av_default_item_name
Definition: dnxhdenc.c:55
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost)
Definition: avconv.c:278
void(* io_close)(struct AVFormatContext *s, AVIOContext *pb)
A callback for closing the streams opened with AVFormatContext.io_open().
Definition: avformat.h:1276
int list_type
Set by a private option.
Definition: segment.c:41
AVOutputFormat ff_segment_muxer
Definition: segment.c:400
Describe the class of an AVClass context structure.
Definition: log.h:34
int has_video
Definition: segment.c:49
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Definition: utils.c:2594
misc parsing utilities
int write_header_trailer
Set by a private option.
Definition: segment.c:46
static const AVClass seg_class
Definition: segment.c:392
Main libavformat public API header.
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:412
int ff_write_chained(AVFormatContext *dst, int dst_stream, AVPacket *pkt, AVFormatContext *src)
Write a packet to another muxer than the one the user originally intended.
Definition: mux.c:755
#define E
Definition: segment.c:376
int64_t recording_time
Definition: segment.c:48
void * priv_data
Format private data.
Definition: avformat.h:968
static const AVOption options[]
Definition: segment.c:377
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:373
int av_write_trailer(AVFormatContext *s)
Write the stream trailer to an output media file and free the file private data.
Definition: mux.c:714
static int open_null_ctx(AVIOContext **ctx)
Definition: segment.c:173
#define AVERROR_MUXER_NOT_FOUND
Muxer not found.
Definition: error.h:55
AVCodecParameters * codecpar
Definition: avformat.h:831
static void seg_free_context(SegmentContext *seg)
Definition: segment.c:193
int stream_index
Definition: avcodec.h:1348
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:742
This structure stores compressed data.
Definition: avcodec.h:1323
int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
Definition: opt.c:247
AVOutputFormat * oformat
Definition: segment.c:36
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1339
int avio_printf(AVIOContext *s, const char *fmt,...) av_printf_format(2