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.

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