segment.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. /*
  2. * Copyright (c) 2011, Luca Barbato
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * Libav is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with Libav; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file generic segmenter
  22. * M3U8 specification can be find here:
  23. * @url{http://tools.ietf.org/id/draft-pantos-http-live-streaming-08.txt}
  24. */
  25. #include <float.h>
  26. #include "avformat.h"
  27. #include "internal.h"
  28. #include "libavutil/avassert.h"
  29. #include "libavutil/log.h"
  30. #include "libavutil/opt.h"
  31. #include "libavutil/avstring.h"
  32. #include "libavutil/parseutils.h"
  33. #include "libavutil/mathematics.h"
  34. typedef enum {
  35. LIST_TYPE_UNDEFINED = -1,
  36. LIST_TYPE_FLAT = 0,
  37. LIST_TYPE_CSV,
  38. LIST_TYPE_M3U8,
  39. LIST_TYPE_EXT, ///< deprecated
  40. LIST_TYPE_NB,
  41. } ListType;
  42. #define SEGMENT_LIST_FLAG_CACHE 1
  43. #define SEGMENT_LIST_FLAG_LIVE 2
  44. typedef struct {
  45. const AVClass *class; /**< Class for private options. */
  46. int segment_idx; ///< index of the segment file to write, starting from 0
  47. int segment_idx_wrap; ///< number after which the index wraps
  48. int segment_count; ///< number of segment files already written
  49. AVFormatContext *avf;
  50. char *format; ///< format to use for output segment files
  51. char *list; ///< filename for the segment list file
  52. int list_count; ///< list counter
  53. int list_flags; ///< flags affecting list generation
  54. int list_size; ///< number of entries for the segment list file
  55. double list_max_segment_time; ///< max segment time in the current list
  56. ListType list_type; ///< set the list type
  57. AVIOContext *list_pb; ///< list file put-byte context
  58. char *time_str; ///< segment duration specification string
  59. int64_t time; ///< segment duration
  60. char *times_str; ///< segment times specification string
  61. int64_t *times; ///< list of segment interval specification
  62. int nb_times; ///< number of elments in the times array
  63. char *time_delta_str; ///< approximation value duration used for the segment times
  64. int64_t time_delta;
  65. int has_video;
  66. double start_time, end_time;
  67. } SegmentContext;
  68. static void print_csv_escaped_str(AVIOContext *ctx, const char *str)
  69. {
  70. int needs_quoting = !!str[strcspn(str, "\",\n\r")];
  71. if (needs_quoting)
  72. avio_w8(ctx, '"');
  73. for (; *str; str++) {
  74. if (*str == '"')
  75. avio_w8(ctx, '"');
  76. avio_w8(ctx, *str);
  77. }
  78. if (needs_quoting)
  79. avio_w8(ctx, '"');
  80. }
  81. static int segment_start(AVFormatContext *s)
  82. {
  83. SegmentContext *seg = s->priv_data;
  84. AVFormatContext *oc = seg->avf;
  85. int err = 0;
  86. if (seg->segment_idx_wrap)
  87. seg->segment_idx %= seg->segment_idx_wrap;
  88. if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
  89. s->filename, seg->segment_idx++) < 0) {
  90. av_log(oc, AV_LOG_ERROR, "Invalid segment filename template '%s'\n", s->filename);
  91. return AVERROR(EINVAL);
  92. }
  93. seg->segment_count++;
  94. if ((err = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
  95. &s->interrupt_callback, NULL)) < 0)
  96. return err;
  97. if (!oc->priv_data && oc->oformat->priv_data_size > 0) {
  98. oc->priv_data = av_mallocz(oc->oformat->priv_data_size);
  99. if (!oc->priv_data) {
  100. avio_close(oc->pb);
  101. return AVERROR(ENOMEM);
  102. }
  103. if (oc->oformat->priv_class) {
  104. *(const AVClass**)oc->priv_data = oc->oformat->priv_class;
  105. av_opt_set_defaults(oc->priv_data);
  106. }
  107. }
  108. if ((err = oc->oformat->write_header(oc)) < 0) {
  109. goto fail;
  110. }
  111. return 0;
  112. fail:
  113. av_log(oc, AV_LOG_ERROR, "Failure occurred when starting segment '%s'\n",
  114. oc->filename);
  115. avio_close(oc->pb);
  116. av_freep(&oc->priv_data);
  117. return err;
  118. }
  119. static int segment_list_open(AVFormatContext *s)
  120. {
  121. SegmentContext *seg = s->priv_data;
  122. int ret;
  123. ret = avio_open2(&seg->list_pb, seg->list, AVIO_FLAG_WRITE,
  124. &s->interrupt_callback, NULL);
  125. if (ret < 0)
  126. return ret;
  127. seg->list_max_segment_time = 0;
  128. if (seg->list_type == LIST_TYPE_M3U8) {
  129. avio_printf(seg->list_pb, "#EXTM3U\n");
  130. avio_printf(seg->list_pb, "#EXT-X-VERSION:3\n");
  131. avio_printf(seg->list_pb, "#EXT-X-MEDIA-SEQUENCE:%d\n", seg->list_count);
  132. avio_printf(seg->list_pb, "#EXT-X-ALLOWCACHE:%d\n",
  133. !!(seg->list_flags & SEGMENT_LIST_FLAG_CACHE));
  134. if (seg->list_flags & SEGMENT_LIST_FLAG_LIVE)
  135. avio_printf(seg->list_pb,
  136. "#EXT-X-TARGETDURATION:%"PRId64"\n", seg->time / 1000000);
  137. }
  138. return ret;
  139. }
  140. static void segment_list_close(AVFormatContext *s)
  141. {
  142. SegmentContext *seg = s->priv_data;
  143. if (seg->list_type == LIST_TYPE_M3U8) {
  144. if (!(seg->list_flags & SEGMENT_LIST_FLAG_LIVE))
  145. avio_printf(seg->list_pb, "#EXT-X-TARGETDURATION:%d\n",
  146. (int)ceil(seg->list_max_segment_time));
  147. avio_printf(seg->list_pb, "#EXT-X-ENDLIST\n");
  148. }
  149. seg->list_count++;
  150. avio_close(seg->list_pb);
  151. }
  152. static int segment_end(AVFormatContext *s)
  153. {
  154. SegmentContext *seg = s->priv_data;
  155. AVFormatContext *oc = seg->avf;
  156. int ret = 0;
  157. if (oc->oformat->write_trailer)
  158. ret = oc->oformat->write_trailer(oc);
  159. if (ret < 0)
  160. av_log(s, AV_LOG_ERROR, "Failure occurred when ending segment '%s'\n",
  161. oc->filename);
  162. if (seg->list) {
  163. if (seg->list_size && !(seg->segment_count % seg->list_size)) {
  164. segment_list_close(s);
  165. if ((ret = segment_list_open(s)) < 0)
  166. goto end;
  167. }
  168. if (seg->list_type == LIST_TYPE_FLAT) {
  169. avio_printf(seg->list_pb, "%s\n", oc->filename);
  170. } else if (seg->list_type == LIST_TYPE_CSV || seg->list_type == LIST_TYPE_EXT) {
  171. print_csv_escaped_str(seg->list_pb, oc->filename);
  172. avio_printf(seg->list_pb, ",%f,%f\n", seg->start_time, seg->end_time);
  173. } else if (seg->list_type == LIST_TYPE_M3U8) {
  174. avio_printf(seg->list_pb, "#EXTINF:%f,\n%s\n",
  175. seg->end_time - seg->start_time, oc->filename);
  176. }
  177. seg->list_max_segment_time = FFMAX(seg->end_time - seg->start_time, seg->list_max_segment_time);
  178. avio_flush(seg->list_pb);
  179. }
  180. end:
  181. avio_close(oc->pb);
  182. if (oc->oformat->priv_class)
  183. av_opt_free(oc->priv_data);
  184. av_freep(&oc->priv_data);
  185. return ret;
  186. }
  187. static int parse_times(void *log_ctx, int64_t **times, int *nb_times,
  188. const char *times_str)
  189. {
  190. char *p;
  191. int i, ret = 0;
  192. char *times_str1 = av_strdup(times_str);
  193. char *saveptr = NULL;
  194. if (!times_str1)
  195. return AVERROR(ENOMEM);
  196. #define FAIL(err) ret = err; goto end
  197. *nb_times = 1;
  198. for (p = times_str1; *p; p++)
  199. if (*p == ',')
  200. (*nb_times)++;
  201. *times = av_malloc(sizeof(**times) * *nb_times);
  202. if (!*times) {
  203. av_log(log_ctx, AV_LOG_ERROR, "Could not allocate forced times array\n");
  204. FAIL(AVERROR(ENOMEM));
  205. }
  206. p = times_str1;
  207. for (i = 0; i < *nb_times; i++) {
  208. int64_t t;
  209. char *tstr = av_strtok(p, ",", &saveptr);
  210. av_assert0(tstr);
  211. p = NULL;
  212. ret = av_parse_time(&t, tstr, 1);
  213. if (ret < 0) {
  214. av_log(log_ctx, AV_LOG_ERROR,
  215. "Invalid time duration specification in %s\n", p);
  216. FAIL(AVERROR(EINVAL));
  217. }
  218. (*times)[i] = t;
  219. /* check on monotonicity */
  220. if (i && (*times)[i-1] > (*times)[i]) {
  221. av_log(log_ctx, AV_LOG_ERROR,
  222. "Specified time %f is greater than the following time %f\n",
  223. (float)((*times)[i])/1000000, (float)((*times)[i-1])/1000000);
  224. FAIL(AVERROR(EINVAL));
  225. }
  226. }
  227. end:
  228. av_free(times_str1);
  229. return ret;
  230. }
  231. static int seg_write_header(AVFormatContext *s)
  232. {
  233. SegmentContext *seg = s->priv_data;
  234. AVFormatContext *oc;
  235. int ret, i;
  236. seg->segment_count = 0;
  237. if (seg->time_str && seg->times_str) {
  238. av_log(s, AV_LOG_ERROR,
  239. "segment_time and segment_times options are mutually exclusive, select just one of them\n");
  240. return AVERROR(EINVAL);
  241. }
  242. if ((seg->list_flags & SEGMENT_LIST_FLAG_LIVE) && seg->times_str) {
  243. av_log(s, AV_LOG_ERROR,
  244. "segment_flags +live and segment_times options are mutually exclusive:"
  245. "specify -segment_time if you want a live-friendly list\n");
  246. return AVERROR(EINVAL);
  247. }
  248. if (seg->times_str) {
  249. if ((ret = parse_times(s, &seg->times, &seg->nb_times, seg->times_str)) < 0)
  250. return ret;
  251. } else {
  252. /* set default value if not specified */
  253. if (!seg->time_str)
  254. seg->time_str = av_strdup("2");
  255. if ((ret = av_parse_time(&seg->time, seg->time_str, 1)) < 0) {
  256. av_log(s, AV_LOG_ERROR,
  257. "Invalid time duration specification '%s' for segment_time option\n",
  258. seg->time_str);
  259. return ret;
  260. }
  261. }
  262. if (seg->time_delta_str) {
  263. if ((ret = av_parse_time(&seg->time_delta, seg->time_delta_str, 1)) < 0) {
  264. av_log(s, AV_LOG_ERROR,
  265. "Invalid time duration specification '%s' for delta option\n",
  266. seg->time_delta_str);
  267. return ret;
  268. }
  269. }
  270. oc = avformat_alloc_context();
  271. if (!oc)
  272. return AVERROR(ENOMEM);
  273. if (seg->list) {
  274. if (seg->list_type == LIST_TYPE_UNDEFINED) {
  275. if (av_match_ext(seg->list, "csv" )) seg->list_type = LIST_TYPE_CSV;
  276. else if (av_match_ext(seg->list, "ext" )) seg->list_type = LIST_TYPE_EXT;
  277. else if (av_match_ext(seg->list, "m3u8")) seg->list_type = LIST_TYPE_M3U8;
  278. else seg->list_type = LIST_TYPE_FLAT;
  279. }
  280. if ((ret = segment_list_open(s)) < 0)
  281. goto fail;
  282. }
  283. if (seg->list_type == LIST_TYPE_EXT)
  284. av_log(s, AV_LOG_WARNING, "'ext' list type option is deprecated in favor of 'csv'\n");
  285. for (i = 0; i< s->nb_streams; i++)
  286. seg->has_video +=
  287. (s->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO);
  288. if (seg->has_video > 1)
  289. av_log(s, AV_LOG_WARNING,
  290. "More than a single video stream present, "
  291. "expect issues decoding it.\n");
  292. oc->oformat = av_guess_format(seg->format, s->filename, NULL);
  293. if (!oc->oformat) {
  294. ret = AVERROR_MUXER_NOT_FOUND;
  295. goto fail;
  296. }
  297. if (oc->oformat->flags & AVFMT_NOFILE) {
  298. av_log(s, AV_LOG_ERROR, "format %s not supported.\n",
  299. oc->oformat->name);
  300. ret = AVERROR(EINVAL);
  301. goto fail;
  302. }
  303. seg->avf = oc;
  304. oc->streams = s->streams;
  305. oc->nb_streams = s->nb_streams;
  306. if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
  307. s->filename, seg->segment_idx++) < 0) {
  308. ret = AVERROR(EINVAL);
  309. goto fail;
  310. }
  311. seg->segment_count++;
  312. if ((ret = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
  313. &s->interrupt_callback, NULL)) < 0)
  314. goto fail;
  315. if ((ret = avformat_write_header(oc, NULL)) < 0) {
  316. avio_close(oc->pb);
  317. goto fail;
  318. }
  319. fail:
  320. if (ret) {
  321. if (oc) {
  322. oc->streams = NULL;
  323. oc->nb_streams = 0;
  324. avformat_free_context(oc);
  325. }
  326. if (seg->list)
  327. segment_list_close(s);
  328. }
  329. return ret;
  330. }
  331. static int seg_write_packet(AVFormatContext *s, AVPacket *pkt)
  332. {
  333. SegmentContext *seg = s->priv_data;
  334. AVFormatContext *oc = seg->avf;
  335. AVStream *st = oc->streams[pkt->stream_index];
  336. int64_t end_pts;
  337. int ret;
  338. if (seg->times) {
  339. end_pts = seg->segment_count <= seg->nb_times ?
  340. seg->times[seg->segment_count-1] : INT64_MAX;
  341. } else {
  342. end_pts = seg->time * seg->segment_count;
  343. }
  344. /* if the segment has video, start a new segment *only* with a key video frame */
  345. if ((st->codec->codec_type == AVMEDIA_TYPE_VIDEO || !seg->has_video) &&
  346. av_compare_ts(pkt->pts, st->time_base,
  347. end_pts-seg->time_delta, AV_TIME_BASE_Q) >= 0 &&
  348. pkt->flags & AV_PKT_FLAG_KEY) {
  349. av_log(s, AV_LOG_DEBUG, "Next segment starts with packet stream:%d pts:%"PRId64" pts_time:%f\n",
  350. pkt->stream_index, pkt->pts, pkt->pts * av_q2d(st->time_base));
  351. if ((ret = segment_end(s)) < 0 || (ret = segment_start(s)) < 0)
  352. goto fail;
  353. seg->start_time = (double)pkt->pts * av_q2d(st->time_base);
  354. } else if (pkt->pts != AV_NOPTS_VALUE) {
  355. seg->end_time = FFMAX(seg->end_time,
  356. (double)(pkt->pts + pkt->duration) * av_q2d(st->time_base));
  357. }
  358. ret = oc->oformat->write_packet(oc, pkt);
  359. fail:
  360. if (ret < 0) {
  361. oc->streams = NULL;
  362. oc->nb_streams = 0;
  363. if (seg->list)
  364. avio_close(seg->list_pb);
  365. avformat_free_context(oc);
  366. }
  367. return ret;
  368. }
  369. static int seg_write_trailer(struct AVFormatContext *s)
  370. {
  371. SegmentContext *seg = s->priv_data;
  372. AVFormatContext *oc = seg->avf;
  373. int ret = segment_end(s);
  374. if (seg->list)
  375. segment_list_close(s);
  376. av_opt_free(seg);
  377. av_freep(&seg->times);
  378. oc->streams = NULL;
  379. oc->nb_streams = 0;
  380. avformat_free_context(oc);
  381. return ret;
  382. }
  383. #define OFFSET(x) offsetof(SegmentContext, x)
  384. #define E AV_OPT_FLAG_ENCODING_PARAM
  385. static const AVOption options[] = {
  386. { "segment_format", "set container format used for the segments", OFFSET(format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
  387. { "segment_list", "set the segment list filename", OFFSET(list), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
  388. { "segment_list_flags","set flags affecting segment list generation", OFFSET(list_flags), AV_OPT_TYPE_FLAGS, {.i64 = SEGMENT_LIST_FLAG_CACHE }, 0, UINT_MAX, E, "list_flags"},
  389. { "cache", "allow list caching", 0, AV_OPT_TYPE_CONST, {.i64 = SEGMENT_LIST_FLAG_CACHE }, INT_MIN, INT_MAX, E, "list_flags"},
  390. { "live", "enable live-friendly list generation (useful for HLS)", 0, AV_OPT_TYPE_CONST, {.i64 = SEGMENT_LIST_FLAG_LIVE }, INT_MIN, INT_MAX, E, "list_flags"},
  391. { "segment_list_size", "set the maximum number of playlist entries", OFFSET(list_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E },
  392. { "segment_list_type", "set the segment list type", OFFSET(list_type), AV_OPT_TYPE_INT, {.i64 = LIST_TYPE_UNDEFINED}, -1, LIST_TYPE_NB-1, E, "list_type" },
  393. { "flat", "flat format", 0, AV_OPT_TYPE_CONST, {.i64=LIST_TYPE_FLAT }, INT_MIN, INT_MAX, 0, "list_type" },
  394. { "csv", "csv format", 0, AV_OPT_TYPE_CONST, {.i64=LIST_TYPE_CSV }, INT_MIN, INT_MAX, 0, "list_type" },
  395. { "ext", "extended format", 0, AV_OPT_TYPE_CONST, {.i64=LIST_TYPE_EXT }, INT_MIN, INT_MAX, 0, "list_type" },
  396. { "m3u8", "M3U8 format", 0, AV_OPT_TYPE_CONST, {.i64=LIST_TYPE_M3U8 }, INT_MIN, INT_MAX, 0, "list_type" },
  397. { "segment_time", "set segment duration", OFFSET(time_str),AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
  398. { "segment_time_delta","set approximation value used for the segment times", OFFSET(time_delta_str), AV_OPT_TYPE_STRING, {.str = "0"}, 0, 0, E },
  399. { "segment_times", "set segment split time points", OFFSET(times_str),AV_OPT_TYPE_STRING,{.str = NULL}, 0, 0, E },
  400. { "segment_wrap", "set number after which the index wraps", OFFSET(segment_idx_wrap), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E },
  401. { NULL },
  402. };
  403. static const AVClass seg_class = {
  404. .class_name = "segment muxer",
  405. .item_name = av_default_item_name,
  406. .option = options,
  407. .version = LIBAVUTIL_VERSION_INT,
  408. };
  409. AVOutputFormat ff_segment_muxer = {
  410. .name = "segment",
  411. .long_name = NULL_IF_CONFIG_SMALL("segment"),
  412. .priv_data_size = sizeof(SegmentContext),
  413. .flags = AVFMT_GLOBALHEADER | AVFMT_NOFILE,
  414. .write_header = seg_write_header,
  415. .write_packet = seg_write_packet,
  416. .write_trailer = seg_write_trailer,
  417. .priv_class = &seg_class,
  418. };
  419. static const AVClass sseg_class = {
  420. .class_name = "stream_segment muxer",
  421. .item_name = av_default_item_name,
  422. .option = options,
  423. .version = LIBAVUTIL_VERSION_INT,
  424. };
  425. AVOutputFormat ff_stream_segment_muxer = {
  426. .name = "stream_segment,ssegment",
  427. .long_name = NULL_IF_CONFIG_SMALL("streaming segment muxer"),
  428. .priv_data_size = sizeof(SegmentContext),
  429. .flags = AVFMT_NOFILE,
  430. .write_header = seg_write_header,
  431. .write_packet = seg_write_packet,
  432. .write_trailer = seg_write_trailer,
  433. .priv_class = &sseg_class,
  434. };