You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

623 lines
21KB

  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/log.h"
  29. #include "libavutil/opt.h"
  30. #include "libavutil/avstring.h"
  31. #include "libavutil/parseutils.h"
  32. #include "libavutil/mathematics.h"
  33. #include "libavutil/timestamp.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. AVOutputFormat *oformat;
  50. AVFormatContext *avf;
  51. char *format; ///< format to use for output segment files
  52. char *list; ///< filename for the segment list file
  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 individual_header_trailer; /**< Set by a private option. */
  66. int write_header_trailer; /**< Set by a private option. */
  67. int reset_timestamps; ///< reset timestamps at the begin of each segment
  68. int has_video;
  69. double start_time, end_time;
  70. int64_t start_pts, start_dts;
  71. } SegmentContext;
  72. static void print_csv_escaped_str(AVIOContext *ctx, const char *str)
  73. {
  74. int needs_quoting = !!str[strcspn(str, "\",\n\r")];
  75. if (needs_quoting)
  76. avio_w8(ctx, '"');
  77. for (; *str; str++) {
  78. if (*str == '"')
  79. avio_w8(ctx, '"');
  80. avio_w8(ctx, *str);
  81. }
  82. if (needs_quoting)
  83. avio_w8(ctx, '"');
  84. }
  85. static int segment_mux_init(AVFormatContext *s)
  86. {
  87. SegmentContext *seg = s->priv_data;
  88. AVFormatContext *oc;
  89. int i;
  90. seg->avf = oc = avformat_alloc_context();
  91. if (!oc)
  92. return AVERROR(ENOMEM);
  93. oc->oformat = seg->oformat;
  94. oc->interrupt_callback = s->interrupt_callback;
  95. for (i = 0; i < s->nb_streams; i++) {
  96. AVStream *st;
  97. AVCodecContext *icodec, *ocodec;
  98. if (!(st = avformat_new_stream(oc, NULL)))
  99. return AVERROR(ENOMEM);
  100. icodec = s->streams[i]->codec;
  101. ocodec = st->codec;
  102. avcodec_copy_context(ocodec, icodec);
  103. if (!oc->oformat->codec_tag ||
  104. av_codec_get_id (oc->oformat->codec_tag, icodec->codec_tag) == ocodec->codec_id ||
  105. av_codec_get_tag(oc->oformat->codec_tag, icodec->codec_id) <= 0) {
  106. ocodec->codec_tag = icodec->codec_tag;
  107. } else {
  108. ocodec->codec_tag = 0;
  109. }
  110. st->sample_aspect_ratio = s->streams[i]->sample_aspect_ratio;
  111. }
  112. return 0;
  113. }
  114. static int set_segment_filename(AVFormatContext *s)
  115. {
  116. SegmentContext *seg = s->priv_data;
  117. AVFormatContext *oc = seg->avf;
  118. if (seg->segment_idx_wrap)
  119. seg->segment_idx %= seg->segment_idx_wrap;
  120. if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
  121. s->filename, seg->segment_idx) < 0) {
  122. av_log(oc, AV_LOG_ERROR, "Invalid segment filename template '%s'\n", s->filename);
  123. return AVERROR(EINVAL);
  124. }
  125. return 0;
  126. }
  127. static int segment_start(AVFormatContext *s, int write_header)
  128. {
  129. SegmentContext *seg = s->priv_data;
  130. AVFormatContext *oc = seg->avf;
  131. int err = 0;
  132. if (write_header) {
  133. avformat_free_context(oc);
  134. seg->avf = NULL;
  135. if ((err = segment_mux_init(s)) < 0)
  136. return err;
  137. oc = seg->avf;
  138. }
  139. seg->segment_idx++;
  140. if ((err = set_segment_filename(s)) < 0)
  141. return err;
  142. seg->segment_count++;
  143. if ((err = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
  144. &s->interrupt_callback, NULL)) < 0)
  145. return err;
  146. if (oc->oformat->priv_class && oc->priv_data)
  147. av_opt_set(oc->priv_data, "resend_headers", "1", 0); /* mpegts specific */
  148. if (write_header) {
  149. if ((err = avformat_write_header(oc, NULL)) < 0)
  150. return err;
  151. }
  152. return 0;
  153. }
  154. static int segment_list_open(AVFormatContext *s)
  155. {
  156. SegmentContext *seg = s->priv_data;
  157. int ret;
  158. ret = avio_open2(&seg->list_pb, seg->list, AVIO_FLAG_WRITE,
  159. &s->interrupt_callback, NULL);
  160. if (ret < 0)
  161. return ret;
  162. seg->list_max_segment_time = 0;
  163. if (seg->list_type == LIST_TYPE_M3U8) {
  164. avio_printf(seg->list_pb, "#EXTM3U\n");
  165. avio_printf(seg->list_pb, "#EXT-X-VERSION:3\n");
  166. avio_printf(seg->list_pb, "#EXT-X-MEDIA-SEQUENCE:%d\n", seg->segment_idx);
  167. avio_printf(seg->list_pb, "#EXT-X-ALLOWCACHE:%d\n",
  168. !!(seg->list_flags & SEGMENT_LIST_FLAG_CACHE));
  169. if (seg->list_flags & SEGMENT_LIST_FLAG_LIVE)
  170. avio_printf(seg->list_pb,
  171. "#EXT-X-TARGETDURATION:%"PRId64"\n", seg->time / 1000000);
  172. }
  173. return ret;
  174. }
  175. static void segment_list_close(AVFormatContext *s)
  176. {
  177. SegmentContext *seg = s->priv_data;
  178. if (seg->list_type == LIST_TYPE_M3U8) {
  179. if (!(seg->list_flags & SEGMENT_LIST_FLAG_LIVE))
  180. avio_printf(seg->list_pb, "#EXT-X-TARGETDURATION:%d\n",
  181. (int)ceil(seg->list_max_segment_time));
  182. avio_printf(seg->list_pb, "#EXT-X-ENDLIST\n");
  183. }
  184. avio_close(seg->list_pb);
  185. }
  186. static int segment_end(AVFormatContext *s, int write_trailer)
  187. {
  188. SegmentContext *seg = s->priv_data;
  189. AVFormatContext *oc = seg->avf;
  190. int ret = 0;
  191. av_write_frame(oc, NULL); /* Flush any buffered data (fragmented mp4) */
  192. if (write_trailer)
  193. ret = av_write_trailer(oc);
  194. if (ret < 0)
  195. av_log(s, AV_LOG_ERROR, "Failure occurred when ending segment '%s'\n",
  196. oc->filename);
  197. if (seg->list) {
  198. if (seg->list_size && !(seg->segment_count % seg->list_size)) {
  199. segment_list_close(s);
  200. if ((ret = segment_list_open(s)) < 0)
  201. goto end;
  202. }
  203. if (seg->list_type == LIST_TYPE_FLAT) {
  204. avio_printf(seg->list_pb, "%s\n", oc->filename);
  205. } else if (seg->list_type == LIST_TYPE_CSV || seg->list_type == LIST_TYPE_EXT) {
  206. print_csv_escaped_str(seg->list_pb, oc->filename);
  207. avio_printf(seg->list_pb, ",%f,%f\n", seg->start_time, seg->end_time);
  208. } else if (seg->list_type == LIST_TYPE_M3U8) {
  209. avio_printf(seg->list_pb, "#EXTINF:%f,\n%s\n",
  210. seg->end_time - seg->start_time, oc->filename);
  211. }
  212. seg->list_max_segment_time = FFMAX(seg->end_time - seg->start_time, seg->list_max_segment_time);
  213. avio_flush(seg->list_pb);
  214. }
  215. end:
  216. avio_close(oc->pb);
  217. return ret;
  218. }
  219. static int parse_times(void *log_ctx, int64_t **times, int *nb_times,
  220. const char *times_str)
  221. {
  222. char *p;
  223. int i, ret = 0;
  224. char *times_str1 = av_strdup(times_str);
  225. char *saveptr = NULL;
  226. if (!times_str1)
  227. return AVERROR(ENOMEM);
  228. #define FAIL(err) ret = err; goto end
  229. *nb_times = 1;
  230. for (p = times_str1; *p; p++)
  231. if (*p == ',')
  232. (*nb_times)++;
  233. *times = av_malloc(sizeof(**times) * *nb_times);
  234. if (!*times) {
  235. av_log(log_ctx, AV_LOG_ERROR, "Could not allocate forced times array\n");
  236. FAIL(AVERROR(ENOMEM));
  237. }
  238. p = times_str1;
  239. for (i = 0; i < *nb_times; i++) {
  240. int64_t t;
  241. char *tstr = av_strtok(p, ",", &saveptr);
  242. p = NULL;
  243. if (!tstr || !tstr[0]) {
  244. av_log(log_ctx, AV_LOG_ERROR, "Empty time specification in times list %s\n",
  245. times_str);
  246. FAIL(AVERROR(EINVAL));
  247. }
  248. ret = av_parse_time(&t, tstr, 1);
  249. if (ret < 0) {
  250. av_log(log_ctx, AV_LOG_ERROR,
  251. "Invalid time duration specification '%s' in times list %s\n", tstr, times_str);
  252. FAIL(AVERROR(EINVAL));
  253. }
  254. (*times)[i] = t;
  255. /* check on monotonicity */
  256. if (i && (*times)[i-1] > (*times)[i]) {
  257. av_log(log_ctx, AV_LOG_ERROR,
  258. "Specified time %f is greater than the following time %f\n",
  259. (float)((*times)[i])/1000000, (float)((*times)[i-1])/1000000);
  260. FAIL(AVERROR(EINVAL));
  261. }
  262. }
  263. end:
  264. av_free(times_str1);
  265. return ret;
  266. }
  267. static int open_null_ctx(AVIOContext **ctx)
  268. {
  269. int buf_size = 32768;
  270. uint8_t *buf = av_malloc(buf_size);
  271. if (!buf)
  272. return AVERROR(ENOMEM);
  273. *ctx = avio_alloc_context(buf, buf_size, AVIO_FLAG_WRITE, NULL, NULL, NULL, NULL);
  274. if (!*ctx) {
  275. av_free(buf);
  276. return AVERROR(ENOMEM);
  277. }
  278. return 0;
  279. }
  280. static void close_null_ctx(AVIOContext *pb)
  281. {
  282. av_free(pb->buffer);
  283. av_free(pb);
  284. }
  285. static int seg_write_header(AVFormatContext *s)
  286. {
  287. SegmentContext *seg = s->priv_data;
  288. AVFormatContext *oc = NULL;
  289. int ret, i;
  290. seg->segment_count = 0;
  291. if (!seg->write_header_trailer)
  292. seg->individual_header_trailer = 0;
  293. if (seg->time_str && seg->times_str) {
  294. av_log(s, AV_LOG_ERROR,
  295. "segment_time and segment_times options are mutually exclusive, select just one of them\n");
  296. return AVERROR(EINVAL);
  297. }
  298. if ((seg->list_flags & SEGMENT_LIST_FLAG_LIVE) && seg->times_str) {
  299. av_log(s, AV_LOG_ERROR,
  300. "segment_flags +live and segment_times options are mutually exclusive:"
  301. "specify -segment_time if you want a live-friendly list\n");
  302. return AVERROR(EINVAL);
  303. }
  304. if (seg->times_str) {
  305. if ((ret = parse_times(s, &seg->times, &seg->nb_times, seg->times_str)) < 0)
  306. return ret;
  307. } else {
  308. /* set default value if not specified */
  309. if (!seg->time_str)
  310. seg->time_str = av_strdup("2");
  311. if ((ret = av_parse_time(&seg->time, seg->time_str, 1)) < 0) {
  312. av_log(s, AV_LOG_ERROR,
  313. "Invalid time duration specification '%s' for segment_time option\n",
  314. seg->time_str);
  315. return ret;
  316. }
  317. }
  318. if (seg->time_delta_str) {
  319. if ((ret = av_parse_time(&seg->time_delta, seg->time_delta_str, 1)) < 0) {
  320. av_log(s, AV_LOG_ERROR,
  321. "Invalid time duration specification '%s' for delta option\n",
  322. seg->time_delta_str);
  323. return ret;
  324. }
  325. }
  326. if (seg->list) {
  327. if (seg->list_type == LIST_TYPE_UNDEFINED) {
  328. if (av_match_ext(seg->list, "csv" )) seg->list_type = LIST_TYPE_CSV;
  329. else if (av_match_ext(seg->list, "ext" )) seg->list_type = LIST_TYPE_EXT;
  330. else if (av_match_ext(seg->list, "m3u8")) seg->list_type = LIST_TYPE_M3U8;
  331. else seg->list_type = LIST_TYPE_FLAT;
  332. }
  333. if ((ret = segment_list_open(s)) < 0)
  334. goto fail;
  335. }
  336. if (seg->list_type == LIST_TYPE_EXT)
  337. av_log(s, AV_LOG_WARNING, "'ext' list type option is deprecated in favor of 'csv'\n");
  338. for (i = 0; i < s->nb_streams; i++)
  339. seg->has_video +=
  340. (s->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO);
  341. if (seg->has_video > 1)
  342. av_log(s, AV_LOG_WARNING,
  343. "More than a single video stream present, "
  344. "expect issues decoding it.\n");
  345. seg->oformat = av_guess_format(seg->format, s->filename, NULL);
  346. if (!seg->oformat) {
  347. ret = AVERROR_MUXER_NOT_FOUND;
  348. goto fail;
  349. }
  350. if (seg->oformat->flags & AVFMT_NOFILE) {
  351. av_log(s, AV_LOG_ERROR, "format %s not supported.\n",
  352. seg->oformat->name);
  353. ret = AVERROR(EINVAL);
  354. goto fail;
  355. }
  356. if ((ret = segment_mux_init(s)) < 0)
  357. goto fail;
  358. oc = seg->avf;
  359. if ((ret = set_segment_filename(s)) < 0)
  360. goto fail;
  361. seg->segment_count++;
  362. if (seg->write_header_trailer) {
  363. if ((ret = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
  364. &s->interrupt_callback, NULL)) < 0)
  365. goto fail;
  366. } else {
  367. if ((ret = open_null_ctx(&oc->pb)) < 0)
  368. goto fail;
  369. }
  370. if ((ret = avformat_write_header(oc, NULL)) < 0) {
  371. avio_close(oc->pb);
  372. goto fail;
  373. }
  374. if (oc->avoid_negative_ts > 0 && s->avoid_negative_ts < 0)
  375. s->avoid_negative_ts = 1;
  376. if (!seg->write_header_trailer) {
  377. close_null_ctx(oc->pb);
  378. if ((ret = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
  379. &s->interrupt_callback, NULL)) < 0)
  380. goto fail;
  381. }
  382. fail:
  383. if (ret) {
  384. if (seg->list)
  385. segment_list_close(s);
  386. if (seg->avf)
  387. avformat_free_context(seg->avf);
  388. }
  389. return ret;
  390. }
  391. static int seg_write_packet(AVFormatContext *s, AVPacket *pkt)
  392. {
  393. SegmentContext *seg = s->priv_data;
  394. AVFormatContext *oc = seg->avf;
  395. AVStream *st = s->streams[pkt->stream_index];
  396. int64_t end_pts;
  397. int ret;
  398. if (seg->times) {
  399. end_pts = seg->segment_count <= seg->nb_times ?
  400. seg->times[seg->segment_count-1] : INT64_MAX;
  401. } else {
  402. end_pts = seg->time * seg->segment_count;
  403. }
  404. /* if the segment has video, start a new segment *only* with a key video frame */
  405. if ((st->codec->codec_type == AVMEDIA_TYPE_VIDEO || !seg->has_video) &&
  406. pkt->pts != AV_NOPTS_VALUE &&
  407. av_compare_ts(pkt->pts, st->time_base,
  408. end_pts-seg->time_delta, AV_TIME_BASE_Q) >= 0 &&
  409. pkt->flags & AV_PKT_FLAG_KEY) {
  410. av_log(s, AV_LOG_DEBUG, "Next segment starts with packet stream:%d pts:%"PRId64" pts_time:%f\n",
  411. pkt->stream_index, pkt->pts, pkt->pts * av_q2d(st->time_base));
  412. ret = segment_end(s, seg->individual_header_trailer);
  413. if (!ret)
  414. ret = segment_start(s, seg->individual_header_trailer);
  415. if (ret)
  416. goto fail;
  417. oc = seg->avf;
  418. seg->start_time = (double)pkt->pts * av_q2d(st->time_base);
  419. seg->start_pts = av_rescale_q(pkt->pts, st->time_base, AV_TIME_BASE_Q);
  420. seg->start_dts = pkt->dts != AV_NOPTS_VALUE ?
  421. av_rescale_q(pkt->dts, st->time_base, AV_TIME_BASE_Q) : seg->start_pts;
  422. } else if (pkt->pts != AV_NOPTS_VALUE) {
  423. seg->end_time = FFMAX(seg->end_time,
  424. (double)(pkt->pts + pkt->duration) * av_q2d(st->time_base));
  425. }
  426. if (seg->reset_timestamps) {
  427. av_log(s, AV_LOG_DEBUG, "start_pts:%s pts:%s start_dts:%s dts:%s",
  428. av_ts2timestr(seg->start_pts, &AV_TIME_BASE_Q), av_ts2timestr(pkt->pts, &st->time_base),
  429. av_ts2timestr(seg->start_dts, &AV_TIME_BASE_Q), av_ts2timestr(pkt->dts, &st->time_base));
  430. /* compute new timestamps */
  431. if (pkt->pts != AV_NOPTS_VALUE)
  432. pkt->pts -= av_rescale_q(seg->start_pts, AV_TIME_BASE_Q, st->time_base);
  433. if (pkt->dts != AV_NOPTS_VALUE)
  434. pkt->dts -= av_rescale_q(seg->start_dts, AV_TIME_BASE_Q, st->time_base);
  435. av_log(s, AV_LOG_DEBUG, " -> pts:%s dts:%s\n",
  436. av_ts2timestr(pkt->pts, &st->time_base), av_ts2timestr(pkt->dts, &st->time_base));
  437. }
  438. ret = ff_write_chained(oc, pkt->stream_index, pkt, s);
  439. fail:
  440. if (ret < 0) {
  441. if (seg->list)
  442. avio_close(seg->list_pb);
  443. avformat_free_context(oc);
  444. }
  445. return ret;
  446. }
  447. static int seg_write_trailer(struct AVFormatContext *s)
  448. {
  449. SegmentContext *seg = s->priv_data;
  450. AVFormatContext *oc = seg->avf;
  451. int ret;
  452. if (!seg->write_header_trailer) {
  453. if ((ret = segment_end(s, 0)) < 0)
  454. goto fail;
  455. open_null_ctx(&oc->pb);
  456. ret = av_write_trailer(oc);
  457. close_null_ctx(oc->pb);
  458. } else {
  459. ret = segment_end(s, 1);
  460. }
  461. fail:
  462. if (seg->list)
  463. segment_list_close(s);
  464. av_opt_free(seg);
  465. av_freep(&seg->times);
  466. avformat_free_context(oc);
  467. return ret;
  468. }
  469. #define OFFSET(x) offsetof(SegmentContext, x)
  470. #define E AV_OPT_FLAG_ENCODING_PARAM
  471. static const AVOption options[] = {
  472. { "segment_format", "set container format used for the segments", OFFSET(format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
  473. { "segment_list", "set the segment list filename", OFFSET(list), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
  474. { "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"},
  475. { "cache", "allow list caching", 0, AV_OPT_TYPE_CONST, {.i64 = SEGMENT_LIST_FLAG_CACHE }, INT_MIN, INT_MAX, E, "list_flags"},
  476. { "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"},
  477. { "segment_list_size", "set the maximum number of playlist entries", OFFSET(list_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E },
  478. { "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" },
  479. { "flat", "flat format", 0, AV_OPT_TYPE_CONST, {.i64=LIST_TYPE_FLAT }, INT_MIN, INT_MAX, 0, "list_type" },
  480. { "csv", "csv format", 0, AV_OPT_TYPE_CONST, {.i64=LIST_TYPE_CSV }, INT_MIN, INT_MAX, 0, "list_type" },
  481. { "ext", "extended format", 0, AV_OPT_TYPE_CONST, {.i64=LIST_TYPE_EXT }, INT_MIN, INT_MAX, 0, "list_type" },
  482. { "m3u8", "M3U8 format", 0, AV_OPT_TYPE_CONST, {.i64=LIST_TYPE_M3U8 }, INT_MIN, INT_MAX, 0, "list_type" },
  483. { "hls", "Apple HTTP Live Streaming compatible", 0, AV_OPT_TYPE_CONST, {.i64=LIST_TYPE_M3U8 }, INT_MIN, INT_MAX, 0, "list_type" },
  484. { "segment_time", "set segment duration", OFFSET(time_str),AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
  485. { "segment_time_delta","set approximation value used for the segment times", OFFSET(time_delta_str), AV_OPT_TYPE_STRING, {.str = "0"}, 0, 0, E },
  486. { "segment_times", "set segment split time points", OFFSET(times_str),AV_OPT_TYPE_STRING,{.str = NULL}, 0, 0, E },
  487. { "segment_wrap", "set number after which the index wraps", OFFSET(segment_idx_wrap), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E },
  488. { "segment_start_number", "set the sequence number of the first segment", OFFSET(segment_idx), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E },
  489. { "individual_header_trailer", "write header/trailer to each segment", OFFSET(individual_header_trailer), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, E },
  490. { "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 },
  491. { "reset_timestamps", "reset timestamps at the begin of each segment", OFFSET(reset_timestamps), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, E },
  492. { NULL },
  493. };
  494. static const AVClass seg_class = {
  495. .class_name = "segment muxer",
  496. .item_name = av_default_item_name,
  497. .option = options,
  498. .version = LIBAVUTIL_VERSION_INT,
  499. };
  500. AVOutputFormat ff_segment_muxer = {
  501. .name = "segment",
  502. .long_name = NULL_IF_CONFIG_SMALL("segment"),
  503. .priv_data_size = sizeof(SegmentContext),
  504. .flags = AVFMT_NOFILE|AVFMT_GLOBALHEADER,
  505. .write_header = seg_write_header,
  506. .write_packet = seg_write_packet,
  507. .write_trailer = seg_write_trailer,
  508. .priv_class = &seg_class,
  509. };
  510. static const AVClass sseg_class = {
  511. .class_name = "stream_segment muxer",
  512. .item_name = av_default_item_name,
  513. .option = options,
  514. .version = LIBAVUTIL_VERSION_INT,
  515. };
  516. AVOutputFormat ff_stream_segment_muxer = {
  517. .name = "stream_segment,ssegment",
  518. .long_name = NULL_IF_CONFIG_SMALL("streaming segment muxer"),
  519. .priv_data_size = sizeof(SegmentContext),
  520. .flags = AVFMT_NOFILE,
  521. .write_header = seg_write_header,
  522. .write_packet = seg_write_packet,
  523. .write_trailer = seg_write_trailer,
  524. .priv_class = &sseg_class,
  525. };