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.

631 lines
22KB

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