FFmpeg  2.6.3
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
hlsenc.c
Go to the documentation of this file.
1 /*
2  * Apple HTTP Live Streaming segmenter
3  * Copyright (c) 2012, Luca Barbato
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 "config.h"
23 #include <float.h>
24 #include <stdint.h>
25 #if HAVE_UNISTD_H
26 #include <unistd.h>
27 #endif
28 
29 #include "libavutil/avassert.h"
30 #include "libavutil/mathematics.h"
31 #include "libavutil/parseutils.h"
32 #include "libavutil/avstring.h"
33 #include "libavutil/opt.h"
34 #include "libavutil/log.h"
35 
36 #include "avformat.h"
37 #include "internal.h"
38 #include "os_support.h"
39 
40 typedef struct HLSSegment {
41  char filename[1024];
42  double duration; /* in seconds */
43  int64_t pos;
44  int64_t size;
45 
46  struct HLSSegment *next;
47 } HLSSegment;
48 
49 typedef enum HLSFlags {
50  // Generate a single media file and use byte ranges in the playlist.
51  HLS_SINGLE_FILE = (1 << 0),
52  HLS_DELETE_SEGMENTS = (1 << 1),
53 } HLSFlags;
54 
55 typedef struct HLSContext {
56  const AVClass *class; // Class for private options.
57  unsigned number;
58  int64_t sequence;
59  int64_t start_sequence;
61 
63 
64  float time; // Set by a private option.
65  int max_nb_segments; // Set by a private option.
66  int wrap; // Set by a private option.
67  uint32_t flags; // enum HLSFlags
69 
71  int64_t recording_time;
72  int has_video;
73  int64_t start_pts;
74  int64_t end_pts;
75  double duration; // last segment duration computed so far, in seconds
76  int64_t start_pos; // last segment starting position
77  int64_t size; // last segment size
79 
83 
84  char *basename;
85  char *baseurl;
88 } HLSContext;
89 
91 
92  HLSSegment *segment, *previous_segment = NULL;
93  float playlist_duration = 0.0f;
94  int ret = 0, path_size;
95  char *dirname = NULL, *p, *path;
96 
97  segment = hls->segments;
98  while (segment) {
99  playlist_duration += segment->duration;
100  segment = segment->next;
101  }
102 
103  segment = hls->old_segments;
104  while (segment) {
105  playlist_duration -= segment->duration;
106  previous_segment = segment;
107  segment = previous_segment->next;
108  if (playlist_duration <= -previous_segment->duration) {
109  previous_segment->next = NULL;
110  break;
111  }
112  }
113 
114  if (segment) {
115  if (hls->segment_filename) {
116  dirname = av_strdup(hls->segment_filename);
117  } else {
118  dirname = av_strdup(hls->avf->filename);
119  }
120  if (!dirname) {
121  ret = AVERROR(ENOMEM);
122  goto fail;
123  }
124  p = (char *)av_basename(dirname);
125  *p = '\0';
126  }
127 
128  while (segment) {
129  av_log(hls, AV_LOG_DEBUG, "deleting old segment %s\n",
130  segment->filename);
131  path_size = strlen(dirname) + strlen(segment->filename) + 1;
132  path = av_malloc(path_size);
133  if (!path) {
134  ret = AVERROR(ENOMEM);
135  goto fail;
136  }
137  av_strlcpy(path, dirname, path_size);
138  av_strlcat(path, segment->filename, path_size);
139  if (unlink(path) < 0) {
140  av_log(hls, AV_LOG_ERROR, "failed to delete old segment %s: %s\n",
141  path, strerror(errno));
142  }
143  av_free(path);
144  previous_segment = segment;
145  segment = previous_segment->next;
146  av_free(previous_segment);
147  }
148 
149 fail:
150  av_free(dirname);
151 
152  return ret;
153 }
154 
156 {
157  HLSContext *hls = s->priv_data;
158  AVFormatContext *oc;
159  int i, ret;
160 
161  ret = avformat_alloc_output_context2(&hls->avf, hls->oformat, NULL, NULL);
162  if (ret < 0)
163  return ret;
164  oc = hls->avf;
165 
166  oc->oformat = hls->oformat;
168  oc->max_delay = s->max_delay;
169  av_dict_copy(&oc->metadata, s->metadata, 0);
170 
171  for (i = 0; i < s->nb_streams; i++) {
172  AVStream *st;
173  if (!(st = avformat_new_stream(oc, NULL)))
174  return AVERROR(ENOMEM);
177  st->time_base = s->streams[i]->time_base;
178  }
179  hls->start_pos = 0;
180 
181  return 0;
182 }
183 
184 /* Create a new segment and append it to the segment list */
185 static int hls_append_segment(HLSContext *hls, double duration, int64_t pos,
186  int64_t size)
187 {
188  HLSSegment *en = av_malloc(sizeof(*en));
189  int ret;
190 
191  if (!en)
192  return AVERROR(ENOMEM);
193 
194  av_strlcpy(en->filename, av_basename(hls->avf->filename), sizeof(en->filename));
195 
196  en->duration = duration;
197  en->pos = pos;
198  en->size = size;
199  en->next = NULL;
200 
201  if (!hls->segments)
202  hls->segments = en;
203  else
204  hls->last_segment->next = en;
205 
206  hls->last_segment = en;
207 
208  if (hls->max_nb_segments && hls->nb_entries >= hls->max_nb_segments) {
209  en = hls->segments;
210  hls->segments = en->next;
211  if (en && hls->flags & HLS_DELETE_SEGMENTS &&
212  !(hls->flags & HLS_SINGLE_FILE || hls->wrap)) {
213  en->next = hls->old_segments;
214  hls->old_segments = en;
215  if ((ret = hls_delete_old_segments(hls)) < 0)
216  return ret;
217  } else
218  av_free(en);
219  } else
220  hls->nb_entries++;
221 
222  hls->sequence++;
223 
224  return 0;
225 }
226 
228 {
229  HLSSegment *en;
230 
231  while(p) {
232  en = p;
233  p = p->next;
234  av_free(en);
235  }
236 }
237 
238 static int hls_window(AVFormatContext *s, int last)
239 {
240  HLSContext *hls = s->priv_data;
241  HLSSegment *en;
242  int target_duration = 0;
243  int ret = 0;
244  AVIOContext *out = NULL;
245  char temp_filename[1024];
246  int64_t sequence = FFMAX(hls->start_sequence, hls->sequence - hls->nb_entries);
247  int version = hls->flags & HLS_SINGLE_FILE ? 4 : 3;
248  const char *proto = avio_find_protocol_name(s->filename);
249  int use_rename = proto && !strcmp(proto, "file");
250  static unsigned warned_non_file;
251 
252  if (!use_rename && !warned_non_file++)
253  av_log(s, AV_LOG_ERROR, "Cannot use rename on non file protocol, this may lead to races and temporarly partial files\n");
254 
255  snprintf(temp_filename, sizeof(temp_filename), use_rename ? "%s.tmp" : "%s", s->filename);
256  if ((ret = avio_open2(&out, temp_filename, AVIO_FLAG_WRITE,
257  &s->interrupt_callback, NULL)) < 0)
258  goto fail;
259 
260  for (en = hls->segments; en; en = en->next) {
261  if (target_duration < en->duration)
262  target_duration = ceil(en->duration);
263  }
264 
265  avio_printf(out, "#EXTM3U\n");
266  avio_printf(out, "#EXT-X-VERSION:%d\n", version);
267  if (hls->allowcache == 0 || hls->allowcache == 1) {
268  avio_printf(out, "#EXT-X-ALLOW-CACHE:%s\n", hls->allowcache == 0 ? "NO" : "YES");
269  }
270  avio_printf(out, "#EXT-X-TARGETDURATION:%d\n", target_duration);
271  avio_printf(out, "#EXT-X-MEDIA-SEQUENCE:%"PRId64"\n", sequence);
272 
273  av_log(s, AV_LOG_VERBOSE, "EXT-X-MEDIA-SEQUENCE:%"PRId64"\n",
274  sequence);
275 
276  for (en = hls->segments; en; en = en->next) {
277  avio_printf(out, "#EXTINF:%f,\n", en->duration);
278  if (hls->flags & HLS_SINGLE_FILE)
279  avio_printf(out, "#EXT-X-BYTERANGE:%"PRIi64"@%"PRIi64"\n",
280  en->size, en->pos);
281  if (hls->baseurl)
282  avio_printf(out, "%s", hls->baseurl);
283  avio_printf(out, "%s\n", en->filename);
284  }
285 
286  if (last)
287  avio_printf(out, "#EXT-X-ENDLIST\n");
288 
289 fail:
290  avio_closep(&out);
291  if (ret >= 0 && use_rename)
292  ff_rename(temp_filename, s->filename, s);
293  return ret;
294 }
295 
297 {
298  HLSContext *c = s->priv_data;
299  AVFormatContext *oc = c->avf;
300  int err = 0;
301 
302  if (c->flags & HLS_SINGLE_FILE)
303  av_strlcpy(oc->filename, c->basename,
304  sizeof(oc->filename));
305  else
306  if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
307  c->basename, c->wrap ? c->sequence % c->wrap : c->sequence) < 0) {
308  av_log(oc, AV_LOG_ERROR, "Invalid segment filename template '%s'\n", c->basename);
309  return AVERROR(EINVAL);
310  }
311  c->number++;
312 
313  if ((err = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
314  &s->interrupt_callback, NULL)) < 0)
315  return err;
316 
317  if (oc->oformat->priv_class && oc->priv_data)
318  av_opt_set(oc->priv_data, "mpegts_flags", "resend_headers", 0);
319 
320  return 0;
321 }
322 
324 {
325  HLSContext *hls = s->priv_data;
326  int ret, i;
327  char *p;
328  const char *pattern = "%d.ts";
330  int basename_size;
331 
332  hls->sequence = hls->start_sequence;
333  hls->recording_time = hls->time * AV_TIME_BASE;
334  hls->start_pts = AV_NOPTS_VALUE;
335 
336  if (hls->format_options_str) {
337  ret = av_dict_parse_string(&hls->format_options, hls->format_options_str, "=", ":", 0);
338  if (ret < 0) {
339  av_log(s, AV_LOG_ERROR, "Could not parse format options list '%s'\n", hls->format_options_str);
340  goto fail;
341  }
342  }
343 
344  for (i = 0; i < s->nb_streams; i++)
345  hls->has_video +=
347 
348  if (hls->has_video > 1)
350  "More than a single video stream present, "
351  "expect issues decoding it.\n");
352 
353  hls->oformat = av_guess_format("mpegts", NULL, NULL);
354 
355  if (!hls->oformat) {
357  goto fail;
358  }
359 
360  if (hls->segment_filename) {
361  hls->basename = av_strdup(hls->segment_filename);
362  if (!hls->basename) {
363  ret = AVERROR(ENOMEM);
364  goto fail;
365  }
366  } else {
367  if (hls->flags & HLS_SINGLE_FILE)
368  pattern = ".ts";
369 
370  basename_size = strlen(s->filename) + strlen(pattern) + 1;
371  hls->basename = av_malloc(basename_size);
372  if (!hls->basename) {
373  ret = AVERROR(ENOMEM);
374  goto fail;
375  }
376 
377  av_strlcpy(hls->basename, s->filename, basename_size);
378 
379  p = strrchr(hls->basename, '.');
380  if (p)
381  *p = '\0';
382  av_strlcat(hls->basename, pattern, basename_size);
383  }
384 
385  if ((ret = hls_mux_init(s)) < 0)
386  goto fail;
387 
388  if ((ret = hls_start(s)) < 0)
389  goto fail;
390 
391  av_dict_copy(&options, hls->format_options, 0);
392  ret = avformat_write_header(hls->avf, &options);
393  if (av_dict_count(options)) {
394  av_log(s, AV_LOG_ERROR, "Some of provided format options in '%s' are not recognized\n", hls->format_options_str);
395  ret = AVERROR(EINVAL);
396  goto fail;
397  }
398  av_assert0(s->nb_streams == hls->avf->nb_streams);
399  for (i = 0; i < s->nb_streams; i++) {
400  AVStream *inner_st = hls->avf->streams[i];
401  AVStream *outer_st = s->streams[i];
402  avpriv_set_pts_info(outer_st, inner_st->pts_wrap_bits, inner_st->time_base.num, inner_st->time_base.den);
403  }
404 fail:
405 
406  av_dict_free(&options);
407  if (ret < 0) {
408  av_freep(&hls->basename);
409  if (hls->avf)
411  }
412  return ret;
413 }
414 
416 {
417  HLSContext *hls = s->priv_data;
418  AVFormatContext *oc = hls->avf;
419  AVStream *st = s->streams[pkt->stream_index];
420  int64_t end_pts = hls->recording_time * hls->number;
421  int is_ref_pkt = 1;
422  int ret, can_split = 1;
423 
424  if (hls->start_pts == AV_NOPTS_VALUE) {
425  hls->start_pts = pkt->pts;
426  hls->end_pts = pkt->pts;
427  }
428 
429  if (hls->has_video) {
430  can_split = st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
431  pkt->flags & AV_PKT_FLAG_KEY;
432  is_ref_pkt = st->codec->codec_type == AVMEDIA_TYPE_VIDEO;
433  }
434  if (pkt->pts == AV_NOPTS_VALUE)
435  is_ref_pkt = can_split = 0;
436 
437  if (is_ref_pkt)
438  hls->duration = (double)(pkt->pts - hls->end_pts)
439  * st->time_base.num / st->time_base.den;
440 
441  if (can_split && av_compare_ts(pkt->pts - hls->start_pts, st->time_base,
442  end_pts, AV_TIME_BASE_Q) >= 0) {
443  int64_t new_start_pos;
444  av_write_frame(oc, NULL); /* Flush any buffered data */
445 
446  new_start_pos = avio_tell(hls->avf->pb);
447  hls->size = new_start_pos - hls->start_pos;
448  ret = hls_append_segment(hls, hls->duration, hls->start_pos, hls->size);
449  hls->start_pos = new_start_pos;
450  if (ret < 0)
451  return ret;
452 
453  hls->end_pts = pkt->pts;
454  hls->duration = 0;
455 
456  if (hls->flags & HLS_SINGLE_FILE) {
457  if (hls->avf->oformat->priv_class && hls->avf->priv_data)
458  av_opt_set(hls->avf->priv_data, "mpegts_flags", "resend_headers", 0);
459  hls->number++;
460  } else {
461  avio_closep(&oc->pb);
462 
463  ret = hls_start(s);
464  }
465 
466  if (ret < 0)
467  return ret;
468 
469  oc = hls->avf;
470 
471  if ((ret = hls_window(s, 0)) < 0)
472  return ret;
473  }
474 
475  ret = ff_write_chained(oc, pkt->stream_index, pkt, s, 0);
476 
477  return ret;
478 }
479 
481 {
482  HLSContext *hls = s->priv_data;
483  AVFormatContext *oc = hls->avf;
484 
485  av_write_trailer(oc);
486  if (oc->pb) {
487  hls->size = avio_tell(hls->avf->pb) - hls->start_pos;
488  avio_closep(&oc->pb);
489  hls_append_segment(hls, hls->duration, hls->start_pos, hls->size);
490  }
491  av_freep(&hls->basename);
493  hls->avf = NULL;
494  hls_window(s, 1);
495 
498  return 0;
499 }
500 
501 #define OFFSET(x) offsetof(HLSContext, x)
502 #define E AV_OPT_FLAG_ENCODING_PARAM
503 static const AVOption options[] = {
504  {"start_number", "set first number in the sequence", OFFSET(start_sequence),AV_OPT_TYPE_INT64, {.i64 = 0}, 0, INT64_MAX, E},
505  {"hls_time", "set segment length in seconds", OFFSET(time), AV_OPT_TYPE_FLOAT, {.dbl = 2}, 0, FLT_MAX, E},
506  {"hls_list_size", "set maximum number of playlist entries", OFFSET(max_nb_segments), AV_OPT_TYPE_INT, {.i64 = 5}, 0, INT_MAX, E},
507  {"hls_ts_options","set hls mpegts list of options for the container format used for hls", OFFSET(format_options_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E},
508  {"hls_wrap", "set number after which the index wraps", OFFSET(wrap), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E},
509  {"hls_allow_cache", "explicitly set whether the client MAY (1) or MUST NOT (0) cache media segments", OFFSET(allowcache), AV_OPT_TYPE_INT, {.i64 = -1}, INT_MIN, INT_MAX, E},
510  {"hls_base_url", "url to prepend to each playlist entry", OFFSET(baseurl), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E},
511  {"hls_segment_filename", "filename template for segment files", OFFSET(segment_filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E},
512  {"hls_flags", "set flags affecting HLS playlist and media file generation", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = 0 }, 0, UINT_MAX, E, "flags"},
513  {"single_file", "generate a single media file indexed with byte ranges", 0, AV_OPT_TYPE_CONST, {.i64 = HLS_SINGLE_FILE }, 0, UINT_MAX, E, "flags"},
514  {"delete_segments", "delete segment files that are no longer part of the playlist", 0, AV_OPT_TYPE_CONST, {.i64 = HLS_DELETE_SEGMENTS }, 0, UINT_MAX, E, "flags"},
515 
516  { NULL },
517 };
518 
519 static const AVClass hls_class = {
520  .class_name = "hls muxer",
521  .item_name = av_default_item_name,
522  .option = options,
523  .version = LIBAVUTIL_VERSION_INT,
524 };
525 
526 
528  .name = "hls",
529  .long_name = NULL_IF_CONFIG_SMALL("Apple HTTP Live Streaming"),
530  .extensions = "m3u8",
531  .priv_data_size = sizeof(HLSContext),
532  .audio_codec = AV_CODEC_ID_AAC,
533  .video_codec = AV_CODEC_ID_H264,
538  .priv_class = &hls_class,
539 };
float time
Definition: hlsenc.c:64
#define NULL
Definition: coverity.c:32
int wrap
Definition: hlsenc.c:66
const char * s
Definition: avisynth_c.h:669
Bytestream IO Context.
Definition: avio.h:68
char * basename
Definition: hlsenc.c:84
AVIOInterruptCB interrupt_callback
Custom interrupt callbacks for the I/O layer.
Definition: avformat.h:1461
AVOption.
Definition: opt.h:255
static int hls_write_trailer(struct AVFormatContext *s)
Definition: hlsenc.c:480
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:398
int av_write_frame(AVFormatContext *s, AVPacket *pkt)
Write a packet to an output media file.
Definition: mux.c:639
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:181
#define LIBAVUTIL_VERSION_INT
Definition: version.h:62
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:3993
double duration
Definition: hlsenc.c:42
int ff_write_chained(AVFormatContext *dst, int dst_stream, AVPacket *pkt, AVFormatContext *src, int interleave)
Write a packet to another muxer than the one the user originally intended.
Definition: mux.c:987
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:867
int64_t size
Definition: hlsenc.c:44
int num
numerator
Definition: rational.h:44
int av_dict_count(const AVDictionary *m)
Get number of entries in dictionary.
Definition: dict.c:34
#define AVIO_FLAG_WRITE
write-only
Definition: avio.h:369
int version
Definition: avisynth_c.h:667
static AVPacket pkt
#define AVFMT_ALLOW_FLUSH
Format allows flushing.
Definition: avformat.h:434
int avcodec_copy_context(AVCodecContext *dest, const AVCodecContext *src)
Copy the settings of the source AVCodecContext into the destination AVCodecContext.
Definition: options.c:180
static int hls_window(AVFormatContext *s, int last)
Definition: hlsenc.c:238
Format I/O context.
Definition: avformat.h:1214
int max_nb_segments
Definition: hlsenc.c:65
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:71
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
const char * av_basename(const char *path)
Thread safe basename.
Definition: avstring.c:234
if()
Definition: avfilter.c:975
#define av_malloc(s)
AVOptions.
miscellaneous OS support macros and functions.
static void hls_free_segments(HLSSegment *p)
Definition: hlsenc.c:227
HLSSegment * old_segments
Definition: hlsenc.c:82
int64_t end_pts
Definition: hlsenc.c:74
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:3659
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1282
char * format_options_str
Definition: hlsenc.c:86
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:191
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:273
static int64_t duration
Definition: ffplay.c:320
void av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
Definition: dict.c:208
#define av_log(a,...)
struct AVOutputFormat * oformat
The output container format.
Definition: avformat.h:1233
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1206
int avformat_alloc_output_context2(AVFormatContext **ctx, AVOutputFormat *oformat, const char *format_name, const char *filename)
Allocate an AVFormatContext for an output format.
Definition: mux.c:118
static const AVOption options[]
Definition: hlsenc.c:503
static int hls_write_header(AVFormatContext *s)
Definition: hlsenc.c:323
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:175
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1423
#define OFFSET(x)
Definition: hlsenc.c:501
av_default_item_name
#define AVERROR(e)
Definition: error.h:43
struct HLSSegment * next
Definition: hlsenc.c:46
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:180
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:196
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values. ...
Definition: dict.c:194
#define wrap(func)
Definition: neontest.h:62
simple assert() macros that are a bit more flexible than ISO C assert().
int has_video
Definition: hlsenc.c:72
double duration
Definition: hlsenc.c:75
int64_t recording_time
Definition: hlsenc.c:71
#define FFMAX(a, b)
Definition: common.h:64
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
Definition: avstring.c:83
int64_t pos
Definition: hlsenc.c:43
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1166
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:145
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:814
char * baseurl
Definition: hlsenc.c:85
Definition: hls.c:68
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1270
unsigned number
Definition: hlsenc.c:57
char filename[1024]
input or output filename
Definition: avformat.h:1290
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:247
ret
Definition: avfilter.c:974
int64_t start_pts
Definition: hlsenc.c:73
static int write_trailer(AVFormatContext *s1)
Definition: v4l2enc.c:94
static int hls_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: hlsenc.c:415
const char * name
Definition: avformat.h:466
HLSSegment * last_segment
Definition: hlsenc.c:81
#define E
Definition: hlsenc.c:502
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:94
HLSSegment * segments
Definition: hlsenc.c:80
AVDictionary * format_options
Definition: hlsenc.c:87
const AVClass * priv_class
AVClass for the private context.
Definition: avformat.h:494
int av_get_frame_filename(char *buf, int buf_size, const char *path, int number)
Return in 'buf' the path with 'd' replaced by a number.
Definition: utils.c:3818
static int hls_delete_old_segments(HLSContext *hls)
Definition: hlsenc.c:90
Stream structure.
Definition: avformat.h:795
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:171
int64_t start_sequence
Definition: hlsenc.c:59
enum AVMediaType codec_type
Definition: avcodec.h:1247
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:253
char * av_strdup(const char *s)
Duplicate the string s.
Definition: mem.c:265
AVIOContext * pb
I/O context.
Definition: avformat.h:1256
AVOutputFormat * oformat
Definition: hlsenc.c:60
int allowcache
Definition: hlsenc.c:70
HLSFlags
Definition: hlsenc.c:49
static int ff_rename(const char *oldpath, const char *newpath, void *logctx)
Wrap errno on rename() error.
Definition: internal.h:426
Describe the class of an AVClass context structure.
Definition: log.h:66
int avio_open2(AVIOContext **s, const char *url, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options)
Create and initialize a AVIOContext for accessing the resource indicated by url.
Definition: aviobuf.c:902
#define snprintf
Definition: snprintf.h:34
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Definition: utils.c:3596
int64_t sequence
Definition: hlsenc.c:58
misc parsing utilities
size_t av_strlcat(char *dst, const char *src, size_t size)
Append the string src to the string dst, but to a total length of no more than size - 1 bytes...
Definition: avstring.c:93
const char * avio_find_protocol_name(const char *url)
Return the name of the protocol that will handle the passed URL.
Definition: avio.c:393
static int flags
Definition: cpu.c:47
static int hls_mux_init(AVFormatContext *s)
Definition: hlsenc.c:155
int64_t size
Definition: hlsenc.c:77
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_dlog(ac->avr,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> out
static int hls_append_segment(HLSContext *hls, double duration, int64_t pos, int64_t size)
Definition: hlsenc.c:185
int nb_entries
Definition: hlsenc.c:78
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:418
static double c[64]
uint32_t flags
Definition: hlsenc.c:67
int pts_wrap_bits
number of bits in pts (used for wrapping control)
Definition: avformat.h:961
int den
denominator
Definition: rational.h:45
int64_t start_pos
Definition: hlsenc.c:76
char * segment_filename
Definition: hlsenc.c:68
#define av_free(p)
char filename[1024]
Definition: hlsenc.c:41
static int hls_start(AVFormatContext *s)
Definition: hlsenc.c:296
void * priv_data
Format private data.
Definition: avformat.h:1242
static const uint8_t start_sequence[]
Definition: rtpdec_h264.c:65
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:493
AVFormatContext * avf
Definition: hlsenc.c:62
AVOutputFormat ff_hls_muxer
Definition: hlsenc.c:527
int av_write_trailer(AVFormatContext *s)
Write the stream trailer to an output media file and free the file private data.
Definition: mux.c:932
#define av_freep(p)
#define AVERROR_MUXER_NOT_FOUND
Muxer not found.
Definition: error.h:60
int stream_index
Definition: avcodec.h:1162
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:837
static const AVClass hls_class
Definition: hlsenc.c:519
This structure stores compressed data.
Definition: avcodec.h:1137
int avio_closep(AVIOContext **s)
Close the resource accessed by the AVIOContext *s, free it and set the pointer pointing to it to NULL...
Definition: aviobuf.c:937
static int write_packet(AVFormatContext *s1, AVPacket *pkt)
Definition: v4l2enc.c:86
int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
Definition: opt.c:368
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1153
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:241
int avio_printf(AVIOContext *s, const char *fmt,...) av_printf_format(2