dnxhddata: Add tables for missing DNx100 profiles
[libav.git] / libavformat / hlsenc.c
CommitLineData
22a0827d
LB
1/*
2 * Apple HTTP Live Streaming segmenter
3 * Copyright (c) 2012, 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>
8f8bc923 23#include <stdint.h>
22a0827d
LB
24
25#include "libavutil/mathematics.h"
26#include "libavutil/parseutils.h"
27#include "libavutil/avstring.h"
28#include "libavutil/opt.h"
29#include "libavutil/log.h"
30
31#include "avformat.h"
32#include "internal.h"
33
34typedef struct ListEntry {
35 char name[1024];
72839fce 36 int64_t duration; // segment duration in AV_TIME_BASE units
22a0827d
LB
37 struct ListEntry *next;
38} ListEntry;
39
40typedef struct HLSContext {
41 const AVClass *class; // Class for private options.
30a76487 42 unsigned number;
27a15e0a 43 int64_t sequence;
c7603b3c 44 int64_t start_sequence;
22a0827d
LB
45 AVOutputFormat *oformat;
46 AVFormatContext *avf;
47 float time; // Set by a private option.
48 int size; // Set by a private option.
49 int wrap; // Set by a private option.
55f03d87 50 int allowcache;
22a0827d
LB
51 int64_t recording_time;
52 int has_video;
72839fce 53 // The following timestamps are in AV_TIME_BASE units.
22a0827d
LB
54 int64_t start_pts;
55 int64_t end_pts;
72839fce 56 int64_t duration; // last segment duration computed so far.
0448f26c 57 int nb_entries;
22a0827d
LB
58 ListEntry *list;
59 ListEntry *end_list;
60 char *basename;
5a70a783 61 char *baseurl;
22a0827d
LB
62} HLSContext;
63
64static int hls_mux_init(AVFormatContext *s)
65{
66 HLSContext *hls = s->priv_data;
67 AVFormatContext *oc;
68 int i;
69
70 hls->avf = oc = avformat_alloc_context();
71 if (!oc)
72 return AVERROR(ENOMEM);
73
74 oc->oformat = hls->oformat;
75 oc->interrupt_callback = s->interrupt_callback;
76
77 for (i = 0; i < s->nb_streams; i++) {
78 AVStream *st;
79 if (!(st = avformat_new_stream(oc, NULL)))
80 return AVERROR(ENOMEM);
81 avcodec_copy_context(st->codec, s->streams[i]->codec);
82 st->sample_aspect_ratio = s->streams[i]->sample_aspect_ratio;
28816050 83 st->time_base = s->streams[i]->time_base;
22a0827d
LB
84 }
85
86 return 0;
87}
88
72839fce 89static int append_entry(HLSContext *hls, int64_t duration)
22a0827d
LB
90{
91 ListEntry *en = av_malloc(sizeof(*en));
92
93 if (!en)
94 return AVERROR(ENOMEM);
95
ae85d6c9 96 av_strlcpy(en->name, av_basename(hls->avf->filename), sizeof(en->name));
22a0827d
LB
97
98 en->duration = duration;
99 en->next = NULL;
100
101 if (!hls->list)
102 hls->list = en;
103 else
104 hls->end_list->next = en;
105
106 hls->end_list = en;
107
0448f26c 108 if (hls->nb_entries >= hls->size) {
22a0827d
LB
109 en = hls->list;
110 hls->list = en->next;
111 av_free(en);
0448f26c
LB
112 } else
113 hls->nb_entries++;
22a0827d 114
27a15e0a
K
115 hls->sequence++;
116
22a0827d
LB
117 return 0;
118}
119
120static void free_entries(HLSContext *hls)
121{
122 ListEntry *p = hls->list, *en;
123
124 while(p) {
125 en = p;
126 p = p->next;
127 av_free(en);
128 }
129}
130
131static int hls_window(AVFormatContext *s, int last)
132{
133 HLSContext *hls = s->priv_data;
134 ListEntry *en;
72839fce 135 int64_t target_duration = 0;
22a0827d 136 int ret = 0;
ff48ae94 137 AVIOContext *out = NULL;
3fe26bb6 138 char temp_filename[1024];
a1643103 139 int64_t sequence = FFMAX(hls->start_sequence, hls->sequence - hls->size);
22a0827d 140
3fe26bb6
HL
141 snprintf(temp_filename, sizeof(temp_filename), "%s.tmp", s->filename);
142 if ((ret = avio_open2(&out, temp_filename, AVIO_FLAG_WRITE,
22a0827d
LB
143 &s->interrupt_callback, NULL)) < 0)
144 goto fail;
145
7e98956e
LB
146 for (en = hls->list; en; en = en->next) {
147 if (target_duration < en->duration)
148 target_duration = en->duration;
149 }
150
ff48ae94
HL
151 avio_printf(out, "#EXTM3U\n");
152 avio_printf(out, "#EXT-X-VERSION:3\n");
55f03d87 153 if (hls->allowcache == 0 || hls->allowcache == 1) {
ff48ae94 154 avio_printf(out, "#EXT-X-ALLOW-CACHE:%s\n", hls->allowcache == 0 ? "NO" : "YES");
55f03d87 155 }
72839fce
LB
156 avio_printf(out, "#EXT-X-TARGETDURATION:%"PRId64"\n",
157 av_rescale(target_duration, 1, AV_TIME_BASE));
ff48ae94 158 avio_printf(out, "#EXT-X-MEDIA-SEQUENCE:%"PRId64"\n", sequence);
22a0827d 159
344f7b5a 160 av_log(s, AV_LOG_VERBOSE, "EXT-X-MEDIA-SEQUENCE:%"PRId64"\n",
a1643103 161 sequence);
344f7b5a 162
22a0827d 163 for (en = hls->list; en; en = en->next) {
72839fce
LB
164 avio_printf(out, "#EXTINF:%"PRId64",\n",
165 av_rescale(en->duration, 1, AV_TIME_BASE));
5a70a783 166 if (hls->baseurl)
ff48ae94
HL
167 avio_printf(out, "%s", hls->baseurl);
168 avio_printf(out, "%s\n", en->name);
22a0827d
LB
169 }
170
171 if (last)
ff48ae94 172 avio_printf(out, "#EXT-X-ENDLIST\n");
22a0827d
LB
173
174fail:
ff48ae94 175 avio_closep(&out);
3fe26bb6
HL
176 if (ret >= 0)
177 ff_rename(temp_filename, s->filename);
22a0827d
LB
178 return ret;
179}
180
181static int hls_start(AVFormatContext *s)
182{
183 HLSContext *c = s->priv_data;
184 AVFormatContext *oc = c->avf;
185 int err = 0;
186
22a0827d 187 if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
7266e24f 188 c->basename, c->wrap ? c->sequence % c->wrap : c->sequence) < 0)
22a0827d 189 return AVERROR(EINVAL);
9d86bfc2 190 c->number++;
22a0827d
LB
191
192 if ((err = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
193 &s->interrupt_callback, NULL)) < 0)
194 return err;
195
196 if (oc->oformat->priv_class && oc->priv_data)
197 av_opt_set(oc->priv_data, "mpegts_flags", "resend_headers", 0);
198
199 return 0;
200}
201
202static int hls_write_header(AVFormatContext *s)
203{
204 HLSContext *hls = s->priv_data;
205 int ret, i;
206 char *p;
207 const char *pattern = "%d.ts";
3193b13a 208 int basename_size = strlen(s->filename) + strlen(pattern) + 1;
22a0827d 209
c7603b3c 210 hls->sequence = hls->start_sequence;
0d8cc7a3 211 hls->recording_time = hls->time * AV_TIME_BASE;
22a0827d
LB
212 hls->start_pts = AV_NOPTS_VALUE;
213
214 for (i = 0; i < s->nb_streams; i++)
215 hls->has_video +=
216 s->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO;
217
218 if (hls->has_video > 1)
219 av_log(s, AV_LOG_WARNING,
220 "More than a single video stream present, "
221 "expect issues decoding it.\n");
222
223 hls->oformat = av_guess_format("mpegts", NULL, NULL);
224
225 if (!hls->oformat) {
226 ret = AVERROR_MUXER_NOT_FOUND;
227 goto fail;
228 }
229
230 hls->basename = av_malloc(basename_size);
231
232 if (!hls->basename) {
233 ret = AVERROR(ENOMEM);
234 goto fail;
235 }
236
237 strcpy(hls->basename, s->filename);
238
239 p = strrchr(hls->basename, '.');
240
241 if (p)
242 *p = '\0';
243
4a9f7d2b 244 av_strlcat(hls->basename, pattern, basename_size);
22a0827d
LB
245
246 if ((ret = hls_mux_init(s)) < 0)
247 goto fail;
248
249 if ((ret = hls_start(s)) < 0)
250 goto fail;
251
252 if ((ret = avformat_write_header(hls->avf, NULL)) < 0)
253 return ret;
254
255
256fail:
257 if (ret) {
258 av_free(hls->basename);
259 if (hls->avf)
260 avformat_free_context(hls->avf);
261 }
262 return ret;
263}
264
265static int hls_write_packet(AVFormatContext *s, AVPacket *pkt)
266{
267 HLSContext *hls = s->priv_data;
268 AVFormatContext *oc = hls->avf;
269 AVStream *st = s->streams[pkt->stream_index];
270 int64_t end_pts = hls->recording_time * hls->number;
72839fce 271 int64_t pts = av_rescale_q(pkt->pts, st->time_base, AV_TIME_BASE_Q);
cf679b94 272 int ret, can_split = 1;
22a0827d
LB
273
274 if (hls->start_pts == AV_NOPTS_VALUE) {
72839fce
LB
275 hls->start_pts = pts;
276 hls->end_pts = pts;
22a0827d 277 }
22a0827d 278
cf679b94
AK
279 if (hls->has_video) {
280 can_split = st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
281 pkt->flags & AV_PKT_FLAG_KEY;
282 }
09c93b1b
SS
283 if (pkt->pts == AV_NOPTS_VALUE)
284 can_split = 0;
285 else
72839fce 286 hls->duration = pts - hls->end_pts;
22a0827d 287
72839fce 288 if (can_split && pts - hls->start_pts >= end_pts) {
09c93b1b 289 ret = append_entry(hls, hls->duration);
6dd93ee6
LB
290 if (ret)
291 return ret;
292
72839fce 293 hls->end_pts = pts;
09c93b1b 294 hls->duration = 0;
22a0827d
LB
295
296 av_write_frame(oc, NULL); /* Flush any buffered data */
297 avio_close(oc->pb);
298
299 ret = hls_start(s);
300
301 if (ret)
302 return ret;
303
304 oc = hls->avf;
305
306 if ((ret = hls_window(s, 0)) < 0)
307 return ret;
308 }
309
310 ret = ff_write_chained(oc, pkt->stream_index, pkt, s);
311
312 return ret;
313}
314
315static int hls_write_trailer(struct AVFormatContext *s)
316{
317 HLSContext *hls = s->priv_data;
318 AVFormatContext *oc = hls->avf;
319
320 av_write_trailer(oc);
321 avio_closep(&oc->pb);
322 avformat_free_context(oc);
323 av_free(hls->basename);
09c93b1b 324 append_entry(hls, hls->duration);
22a0827d
LB
325 hls_window(s, 1);
326
327 free_entries(hls);
22a0827d
LB
328 return 0;
329}
330
331#define OFFSET(x) offsetof(HLSContext, x)
332#define E AV_OPT_FLAG_ENCODING_PARAM
333static const AVOption options[] = {
c7603b3c 334 {"start_number", "first number in the sequence", OFFSET(start_sequence),AV_OPT_TYPE_INT64, {.i64 = 0}, 0, INT64_MAX, E},
22a0827d
LB
335 {"hls_time", "segment length in seconds", OFFSET(time), AV_OPT_TYPE_FLOAT, {.dbl = 2}, 0, FLT_MAX, E},
336 {"hls_list_size", "maximum number of playlist entries", OFFSET(size), AV_OPT_TYPE_INT, {.i64 = 5}, 0, INT_MAX, E},
337 {"hls_wrap", "number after which the index wraps", OFFSET(wrap), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E},
55f03d87 338 {"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},
5a70a783 339 {"hls_base_url", "url to prepend to each playlist entry", OFFSET(baseurl), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E},
22a0827d
LB
340 { NULL },
341};
342
343static const AVClass hls_class = {
344 .class_name = "hls muxer",
345 .item_name = av_default_item_name,
346 .option = options,
347 .version = LIBAVUTIL_VERSION_INT,
348};
349
350
351AVOutputFormat ff_hls_muxer = {
352 .name = "hls",
adbe0307 353 .long_name = NULL_IF_CONFIG_SMALL("Apple HTTP Live Streaming"),
22a0827d
LB
354 .extensions = "m3u8",
355 .priv_data_size = sizeof(HLSContext),
34e2ce5d
MS
356 .audio_codec = AV_CODEC_ID_AAC,
357 .video_codec = AV_CODEC_ID_H264,
22a0827d
LB
358 .flags = AVFMT_NOFILE | AVFMT_ALLOW_FLUSH,
359 .write_header = hls_write_header,
360 .write_packet = hls_write_packet,
361 .write_trailer = hls_write_trailer,
362 .priv_class = &hls_class,
363};