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.

1107 lines
41KB

  1. /*
  2. * Copyright (c) 2011, Luca Barbato
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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}
  24. */
  25. /* #define DEBUG */
  26. #include <float.h>
  27. #include <time.h>
  28. #include "avformat.h"
  29. #include "avio_internal.h"
  30. #include "internal.h"
  31. #include "libavutil/avassert.h"
  32. #include "libavutil/internal.h"
  33. #include "libavutil/log.h"
  34. #include "libavutil/opt.h"
  35. #include "libavutil/avstring.h"
  36. #include "libavutil/parseutils.h"
  37. #include "libavutil/mathematics.h"
  38. #include "libavutil/time.h"
  39. #include "libavutil/timecode.h"
  40. #include "libavutil/time_internal.h"
  41. #include "libavutil/timestamp.h"
  42. typedef struct SegmentListEntry {
  43. int index;
  44. double start_time, end_time;
  45. int64_t start_pts;
  46. int64_t offset_pts;
  47. char *filename;
  48. struct SegmentListEntry *next;
  49. int64_t last_duration;
  50. } SegmentListEntry;
  51. typedef enum {
  52. LIST_TYPE_UNDEFINED = -1,
  53. LIST_TYPE_FLAT = 0,
  54. LIST_TYPE_CSV,
  55. LIST_TYPE_M3U8,
  56. LIST_TYPE_EXT, ///< deprecated
  57. LIST_TYPE_FFCONCAT,
  58. LIST_TYPE_NB,
  59. } ListType;
  60. #define SEGMENT_LIST_FLAG_CACHE 1
  61. #define SEGMENT_LIST_FLAG_LIVE 2
  62. typedef struct SegmentContext {
  63. const AVClass *class; /**< Class for private options. */
  64. int segment_idx; ///< index of the segment file to write, starting from 0
  65. int segment_idx_wrap; ///< number after which the index wraps
  66. int segment_idx_wrap_nb; ///< number of time the index has wraped
  67. int segment_count; ///< number of segment files already written
  68. AVOutputFormat *oformat;
  69. AVFormatContext *avf;
  70. char *format; ///< format to use for output segment files
  71. char *format_options_str; ///< format options to use for output segment files
  72. AVDictionary *format_options;
  73. char *list; ///< filename for the segment list file
  74. int list_flags; ///< flags affecting list generation
  75. int list_size; ///< number of entries for the segment list file
  76. int use_clocktime; ///< flag to cut segments at regular clock time
  77. int64_t clocktime_offset; //< clock offset for cutting the segments at regular clock time
  78. int64_t clocktime_wrap_duration; //< wrapping duration considered for starting a new segment
  79. int64_t last_val; ///< remember last time for wrap around detection
  80. int64_t last_cut; ///< remember last cut
  81. int cut_pending;
  82. int header_written; ///< whether we've already called avformat_write_header
  83. char *entry_prefix; ///< prefix to add to list entry filenames
  84. int list_type; ///< set the list type
  85. AVIOContext *list_pb; ///< list file put-byte context
  86. char *time_str; ///< segment duration specification string
  87. int64_t time; ///< segment duration
  88. int use_strftime; ///< flag to expand filename with strftime
  89. int increment_tc; ///< flag to increment timecode if found
  90. char *times_str; ///< segment times specification string
  91. int64_t *times; ///< list of segment interval specification
  92. int nb_times; ///< number of elments in the times array
  93. char *frames_str; ///< segment frame numbers specification string
  94. int *frames; ///< list of frame number specification
  95. int nb_frames; ///< number of elments in the frames array
  96. int frame_count; ///< total number of reference frames
  97. int segment_frame_count; ///< number of reference frames in the segment
  98. int64_t time_delta;
  99. int individual_header_trailer; /**< Set by a private option. */
  100. int write_header_trailer; /**< Set by a private option. */
  101. char *header_filename; ///< filename to write the output header to
  102. int reset_timestamps; ///< reset timestamps at the begin of each segment
  103. int64_t initial_offset; ///< initial timestamps offset, expressed in microseconds
  104. char *reference_stream_specifier; ///< reference stream specifier
  105. int reference_stream_index;
  106. int break_non_keyframes;
  107. int write_empty;
  108. int use_rename;
  109. char temp_list_filename[1024];
  110. SegmentListEntry cur_entry;
  111. SegmentListEntry *segment_list_entries;
  112. SegmentListEntry *segment_list_entries_end;
  113. } SegmentContext;
  114. static void print_csv_escaped_str(AVIOContext *ctx, const char *str)
  115. {
  116. int needs_quoting = !!str[strcspn(str, "\",\n\r")];
  117. if (needs_quoting)
  118. avio_w8(ctx, '"');
  119. for (; *str; str++) {
  120. if (*str == '"')
  121. avio_w8(ctx, '"');
  122. avio_w8(ctx, *str);
  123. }
  124. if (needs_quoting)
  125. avio_w8(ctx, '"');
  126. }
  127. static int segment_mux_init(AVFormatContext *s)
  128. {
  129. SegmentContext *seg = s->priv_data;
  130. AVFormatContext *oc;
  131. int i;
  132. int ret;
  133. ret = avformat_alloc_output_context2(&seg->avf, seg->oformat, NULL, NULL);
  134. if (ret < 0)
  135. return ret;
  136. oc = seg->avf;
  137. oc->interrupt_callback = s->interrupt_callback;
  138. oc->max_delay = s->max_delay;
  139. av_dict_copy(&oc->metadata, s->metadata, 0);
  140. oc->opaque = s->opaque;
  141. oc->io_close = s->io_close;
  142. oc->io_open = s->io_open;
  143. oc->flags = s->flags;
  144. for (i = 0; i < s->nb_streams; i++) {
  145. AVStream *st;
  146. AVCodecParameters *ipar, *opar;
  147. if (!(st = avformat_new_stream(oc, NULL)))
  148. return AVERROR(ENOMEM);
  149. ipar = s->streams[i]->codecpar;
  150. opar = st->codecpar;
  151. avcodec_parameters_copy(opar, ipar);
  152. if (!oc->oformat->codec_tag ||
  153. av_codec_get_id (oc->oformat->codec_tag, ipar->codec_tag) == opar->codec_id ||
  154. av_codec_get_tag(oc->oformat->codec_tag, ipar->codec_id) <= 0) {
  155. opar->codec_tag = ipar->codec_tag;
  156. } else {
  157. opar->codec_tag = 0;
  158. }
  159. st->sample_aspect_ratio = s->streams[i]->sample_aspect_ratio;
  160. st->time_base = s->streams[i]->time_base;
  161. av_dict_copy(&st->metadata, s->streams[i]->metadata, 0);
  162. }
  163. return 0;
  164. }
  165. static int set_segment_filename(AVFormatContext *s)
  166. {
  167. SegmentContext *seg = s->priv_data;
  168. AVFormatContext *oc = seg->avf;
  169. size_t size;
  170. int ret;
  171. if (seg->segment_idx_wrap)
  172. seg->segment_idx %= seg->segment_idx_wrap;
  173. if (seg->use_strftime) {
  174. time_t now0;
  175. struct tm *tm, tmpbuf;
  176. time(&now0);
  177. tm = localtime_r(&now0, &tmpbuf);
  178. if (!strftime(oc->filename, sizeof(oc->filename), s->filename, tm)) {
  179. av_log(oc, AV_LOG_ERROR, "Could not get segment filename with strftime\n");
  180. return AVERROR(EINVAL);
  181. }
  182. } else if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
  183. s->filename, seg->segment_idx) < 0) {
  184. av_log(oc, AV_LOG_ERROR, "Invalid segment filename template '%s'\n", s->filename);
  185. return AVERROR(EINVAL);
  186. }
  187. /* copy modified name in list entry */
  188. size = strlen(av_basename(oc->filename)) + 1;
  189. if (seg->entry_prefix)
  190. size += strlen(seg->entry_prefix);
  191. if ((ret = av_reallocp(&seg->cur_entry.filename, size)) < 0)
  192. return ret;
  193. snprintf(seg->cur_entry.filename, size, "%s%s",
  194. seg->entry_prefix ? seg->entry_prefix : "",
  195. av_basename(oc->filename));
  196. return 0;
  197. }
  198. static int segment_start(AVFormatContext *s, int write_header)
  199. {
  200. SegmentContext *seg = s->priv_data;
  201. AVFormatContext *oc = seg->avf;
  202. int err = 0;
  203. if (write_header) {
  204. avformat_free_context(oc);
  205. seg->avf = NULL;
  206. if ((err = segment_mux_init(s)) < 0)
  207. return err;
  208. oc = seg->avf;
  209. }
  210. seg->segment_idx++;
  211. if ((seg->segment_idx_wrap) && (seg->segment_idx % seg->segment_idx_wrap == 0))
  212. seg->segment_idx_wrap_nb++;
  213. if ((err = set_segment_filename(s)) < 0)
  214. return err;
  215. if ((err = s->io_open(s, &oc->pb, oc->filename, AVIO_FLAG_WRITE, NULL)) < 0) {
  216. av_log(s, AV_LOG_ERROR, "Failed to open segment '%s'\n", oc->filename);
  217. return err;
  218. }
  219. if (!seg->individual_header_trailer)
  220. oc->pb->seekable = 0;
  221. if (oc->oformat->priv_class && oc->priv_data)
  222. av_opt_set(oc->priv_data, "mpegts_flags", "+resend_headers", 0);
  223. if (write_header) {
  224. AVDictionary *options = NULL;
  225. av_dict_copy(&options, seg->format_options, 0);
  226. av_dict_set(&options, "fflags", "-autobsf", 0);
  227. err = avformat_write_header(oc, &options);
  228. av_dict_free(&options);
  229. if (err < 0)
  230. return err;
  231. }
  232. seg->segment_frame_count = 0;
  233. return 0;
  234. }
  235. static int segment_list_open(AVFormatContext *s)
  236. {
  237. SegmentContext *seg = s->priv_data;
  238. int ret;
  239. snprintf(seg->temp_list_filename, sizeof(seg->temp_list_filename), seg->use_rename ? "%s.tmp" : "%s", seg->list);
  240. ret = s->io_open(s, &seg->list_pb, seg->temp_list_filename, AVIO_FLAG_WRITE, NULL);
  241. if (ret < 0) {
  242. av_log(s, AV_LOG_ERROR, "Failed to open segment list '%s'\n", seg->list);
  243. return ret;
  244. }
  245. if (seg->list_type == LIST_TYPE_M3U8 && seg->segment_list_entries) {
  246. SegmentListEntry *entry;
  247. double max_duration = 0;
  248. avio_printf(seg->list_pb, "#EXTM3U\n");
  249. avio_printf(seg->list_pb, "#EXT-X-VERSION:3\n");
  250. avio_printf(seg->list_pb, "#EXT-X-MEDIA-SEQUENCE:%d\n", seg->segment_list_entries->index);
  251. avio_printf(seg->list_pb, "#EXT-X-ALLOW-CACHE:%s\n",
  252. seg->list_flags & SEGMENT_LIST_FLAG_CACHE ? "YES" : "NO");
  253. av_log(s, AV_LOG_VERBOSE, "EXT-X-MEDIA-SEQUENCE:%d\n",
  254. seg->segment_list_entries->index);
  255. for (entry = seg->segment_list_entries; entry; entry = entry->next)
  256. max_duration = FFMAX(max_duration, entry->end_time - entry->start_time);
  257. avio_printf(seg->list_pb, "#EXT-X-TARGETDURATION:%"PRId64"\n", (int64_t)ceil(max_duration));
  258. } else if (seg->list_type == LIST_TYPE_FFCONCAT) {
  259. avio_printf(seg->list_pb, "ffconcat version 1.0\n");
  260. }
  261. return ret;
  262. }
  263. static void segment_list_print_entry(AVIOContext *list_ioctx,
  264. ListType list_type,
  265. const SegmentListEntry *list_entry,
  266. void *log_ctx)
  267. {
  268. switch (list_type) {
  269. case LIST_TYPE_FLAT:
  270. avio_printf(list_ioctx, "%s\n", list_entry->filename);
  271. break;
  272. case LIST_TYPE_CSV:
  273. case LIST_TYPE_EXT:
  274. print_csv_escaped_str(list_ioctx, list_entry->filename);
  275. avio_printf(list_ioctx, ",%f,%f\n", list_entry->start_time, list_entry->end_time);
  276. break;
  277. case LIST_TYPE_M3U8:
  278. avio_printf(list_ioctx, "#EXTINF:%f,\n%s\n",
  279. list_entry->end_time - list_entry->start_time, list_entry->filename);
  280. break;
  281. case LIST_TYPE_FFCONCAT:
  282. {
  283. char *buf;
  284. if (av_escape(&buf, list_entry->filename, NULL, AV_ESCAPE_MODE_AUTO, AV_ESCAPE_FLAG_WHITESPACE) < 0) {
  285. av_log(log_ctx, AV_LOG_WARNING,
  286. "Error writing list entry '%s' in list file\n", list_entry->filename);
  287. return;
  288. }
  289. avio_printf(list_ioctx, "file %s\n", buf);
  290. av_free(buf);
  291. break;
  292. }
  293. default:
  294. av_assert0(!"Invalid list type");
  295. }
  296. }
  297. static int segment_end(AVFormatContext *s, int write_trailer, int is_last)
  298. {
  299. SegmentContext *seg = s->priv_data;
  300. AVFormatContext *oc = seg->avf;
  301. int ret = 0;
  302. AVTimecode tc;
  303. AVRational rate;
  304. AVDictionaryEntry *tcr;
  305. char buf[AV_TIMECODE_STR_SIZE];
  306. int i;
  307. int err;
  308. if (!oc || !oc->pb)
  309. return AVERROR(EINVAL);
  310. av_write_frame(oc, NULL); /* Flush any buffered data (fragmented mp4) */
  311. if (write_trailer)
  312. ret = av_write_trailer(oc);
  313. if (ret < 0)
  314. av_log(s, AV_LOG_ERROR, "Failure occurred when ending segment '%s'\n",
  315. oc->filename);
  316. if (seg->list) {
  317. if (seg->list_size || seg->list_type == LIST_TYPE_M3U8) {
  318. SegmentListEntry *entry = av_mallocz(sizeof(*entry));
  319. if (!entry) {
  320. ret = AVERROR(ENOMEM);
  321. goto end;
  322. }
  323. /* append new element */
  324. memcpy(entry, &seg->cur_entry, sizeof(*entry));
  325. entry->filename = av_strdup(entry->filename);
  326. if (!seg->segment_list_entries)
  327. seg->segment_list_entries = seg->segment_list_entries_end = entry;
  328. else
  329. seg->segment_list_entries_end->next = entry;
  330. seg->segment_list_entries_end = entry;
  331. /* drop first item */
  332. if (seg->list_size && seg->segment_count >= seg->list_size) {
  333. entry = seg->segment_list_entries;
  334. seg->segment_list_entries = seg->segment_list_entries->next;
  335. av_freep(&entry->filename);
  336. av_freep(&entry);
  337. }
  338. if ((ret = segment_list_open(s)) < 0)
  339. goto end;
  340. for (entry = seg->segment_list_entries; entry; entry = entry->next)
  341. segment_list_print_entry(seg->list_pb, seg->list_type, entry, s);
  342. if (seg->list_type == LIST_TYPE_M3U8 && is_last)
  343. avio_printf(seg->list_pb, "#EXT-X-ENDLIST\n");
  344. ff_format_io_close(s, &seg->list_pb);
  345. if (seg->use_rename)
  346. ff_rename(seg->temp_list_filename, seg->list, s);
  347. } else {
  348. segment_list_print_entry(seg->list_pb, seg->list_type, &seg->cur_entry, s);
  349. avio_flush(seg->list_pb);
  350. }
  351. }
  352. av_log(s, AV_LOG_VERBOSE, "segment:'%s' count:%d ended\n",
  353. seg->avf->filename, seg->segment_count);
  354. seg->segment_count++;
  355. if (seg->increment_tc) {
  356. tcr = av_dict_get(s->metadata, "timecode", NULL, 0);
  357. if (tcr) {
  358. /* search the first video stream */
  359. for (i = 0; i < s->nb_streams; i++) {
  360. if (s->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
  361. rate = s->streams[i]->avg_frame_rate;/* Get fps from the video stream */
  362. err = av_timecode_init_from_string(&tc, rate, tcr->value, s);
  363. if (err < 0) {
  364. av_log(s, AV_LOG_WARNING, "Could not increment timecode, error occurred during timecode creation.");
  365. break;
  366. }
  367. tc.start += (int)((seg->cur_entry.end_time - seg->cur_entry.start_time) * av_q2d(rate));/* increment timecode */
  368. av_dict_set(&s->metadata, "timecode",
  369. av_timecode_make_string(&tc, buf, 0), 0);
  370. break;
  371. }
  372. }
  373. } else {
  374. av_log(s, AV_LOG_WARNING, "Could not increment timecode, no timecode metadata found");
  375. }
  376. }
  377. end:
  378. ff_format_io_close(oc, &oc->pb);
  379. return ret;
  380. }
  381. static int parse_times(void *log_ctx, int64_t **times, int *nb_times,
  382. const char *times_str)
  383. {
  384. char *p;
  385. int i, ret = 0;
  386. char *times_str1 = av_strdup(times_str);
  387. char *saveptr = NULL;
  388. if (!times_str1)
  389. return AVERROR(ENOMEM);
  390. #define FAIL(err) ret = err; goto end
  391. *nb_times = 1;
  392. for (p = times_str1; *p; p++)
  393. if (*p == ',')
  394. (*nb_times)++;
  395. *times = av_malloc_array(*nb_times, sizeof(**times));
  396. if (!*times) {
  397. av_log(log_ctx, AV_LOG_ERROR, "Could not allocate forced times array\n");
  398. FAIL(AVERROR(ENOMEM));
  399. }
  400. p = times_str1;
  401. for (i = 0; i < *nb_times; i++) {
  402. int64_t t;
  403. char *tstr = av_strtok(p, ",", &saveptr);
  404. p = NULL;
  405. if (!tstr || !tstr[0]) {
  406. av_log(log_ctx, AV_LOG_ERROR, "Empty time specification in times list %s\n",
  407. times_str);
  408. FAIL(AVERROR(EINVAL));
  409. }
  410. ret = av_parse_time(&t, tstr, 1);
  411. if (ret < 0) {
  412. av_log(log_ctx, AV_LOG_ERROR,
  413. "Invalid time duration specification '%s' in times list %s\n", tstr, times_str);
  414. FAIL(AVERROR(EINVAL));
  415. }
  416. (*times)[i] = t;
  417. /* check on monotonicity */
  418. if (i && (*times)[i-1] > (*times)[i]) {
  419. av_log(log_ctx, AV_LOG_ERROR,
  420. "Specified time %f is greater than the following time %f\n",
  421. (float)((*times)[i])/1000000, (float)((*times)[i-1])/1000000);
  422. FAIL(AVERROR(EINVAL));
  423. }
  424. }
  425. end:
  426. av_free(times_str1);
  427. return ret;
  428. }
  429. static int parse_frames(void *log_ctx, int **frames, int *nb_frames,
  430. const char *frames_str)
  431. {
  432. char *p;
  433. int i, ret = 0;
  434. char *frames_str1 = av_strdup(frames_str);
  435. char *saveptr = NULL;
  436. if (!frames_str1)
  437. return AVERROR(ENOMEM);
  438. #define FAIL(err) ret = err; goto end
  439. *nb_frames = 1;
  440. for (p = frames_str1; *p; p++)
  441. if (*p == ',')
  442. (*nb_frames)++;
  443. *frames = av_malloc_array(*nb_frames, sizeof(**frames));
  444. if (!*frames) {
  445. av_log(log_ctx, AV_LOG_ERROR, "Could not allocate forced frames array\n");
  446. FAIL(AVERROR(ENOMEM));
  447. }
  448. p = frames_str1;
  449. for (i = 0; i < *nb_frames; i++) {
  450. long int f;
  451. char *tailptr;
  452. char *fstr = av_strtok(p, ",", &saveptr);
  453. p = NULL;
  454. if (!fstr) {
  455. av_log(log_ctx, AV_LOG_ERROR, "Empty frame specification in frame list %s\n",
  456. frames_str);
  457. FAIL(AVERROR(EINVAL));
  458. }
  459. f = strtol(fstr, &tailptr, 10);
  460. if (*tailptr || f <= 0 || f >= INT_MAX) {
  461. av_log(log_ctx, AV_LOG_ERROR,
  462. "Invalid argument '%s', must be a positive integer <= INT64_MAX\n",
  463. fstr);
  464. FAIL(AVERROR(EINVAL));
  465. }
  466. (*frames)[i] = f;
  467. /* check on monotonicity */
  468. if (i && (*frames)[i-1] > (*frames)[i]) {
  469. av_log(log_ctx, AV_LOG_ERROR,
  470. "Specified frame %d is greater than the following frame %d\n",
  471. (*frames)[i], (*frames)[i-1]);
  472. FAIL(AVERROR(EINVAL));
  473. }
  474. }
  475. end:
  476. av_free(frames_str1);
  477. return ret;
  478. }
  479. static int open_null_ctx(AVIOContext **ctx)
  480. {
  481. int buf_size = 32768;
  482. uint8_t *buf = av_malloc(buf_size);
  483. if (!buf)
  484. return AVERROR(ENOMEM);
  485. *ctx = avio_alloc_context(buf, buf_size, AVIO_FLAG_WRITE, NULL, NULL, NULL, NULL);
  486. if (!*ctx) {
  487. av_free(buf);
  488. return AVERROR(ENOMEM);
  489. }
  490. return 0;
  491. }
  492. static void close_null_ctxp(AVIOContext **pb)
  493. {
  494. av_freep(&(*pb)->buffer);
  495. av_freep(pb);
  496. }
  497. static int select_reference_stream(AVFormatContext *s)
  498. {
  499. SegmentContext *seg = s->priv_data;
  500. int ret, i;
  501. seg->reference_stream_index = -1;
  502. if (!strcmp(seg->reference_stream_specifier, "auto")) {
  503. /* select first index of type with highest priority */
  504. int type_index_map[AVMEDIA_TYPE_NB];
  505. static const enum AVMediaType type_priority_list[] = {
  506. AVMEDIA_TYPE_VIDEO,
  507. AVMEDIA_TYPE_AUDIO,
  508. AVMEDIA_TYPE_SUBTITLE,
  509. AVMEDIA_TYPE_DATA,
  510. AVMEDIA_TYPE_ATTACHMENT
  511. };
  512. enum AVMediaType type;
  513. for (i = 0; i < AVMEDIA_TYPE_NB; i++)
  514. type_index_map[i] = -1;
  515. /* select first index for each type */
  516. for (i = 0; i < s->nb_streams; i++) {
  517. type = s->streams[i]->codecpar->codec_type;
  518. if ((unsigned)type < AVMEDIA_TYPE_NB && type_index_map[type] == -1
  519. /* ignore attached pictures/cover art streams */
  520. && !(s->streams[i]->disposition & AV_DISPOSITION_ATTACHED_PIC))
  521. type_index_map[type] = i;
  522. }
  523. for (i = 0; i < FF_ARRAY_ELEMS(type_priority_list); i++) {
  524. type = type_priority_list[i];
  525. if ((seg->reference_stream_index = type_index_map[type]) >= 0)
  526. break;
  527. }
  528. } else {
  529. for (i = 0; i < s->nb_streams; i++) {
  530. ret = avformat_match_stream_specifier(s, s->streams[i],
  531. seg->reference_stream_specifier);
  532. if (ret < 0)
  533. return ret;
  534. if (ret > 0) {
  535. seg->reference_stream_index = i;
  536. break;
  537. }
  538. }
  539. }
  540. if (seg->reference_stream_index < 0) {
  541. av_log(s, AV_LOG_ERROR, "Could not select stream matching identifier '%s'\n",
  542. seg->reference_stream_specifier);
  543. return AVERROR(EINVAL);
  544. }
  545. return 0;
  546. }
  547. static void seg_free(AVFormatContext *s)
  548. {
  549. SegmentContext *seg = s->priv_data;
  550. ff_format_io_close(seg->avf, &seg->list_pb);
  551. avformat_free_context(seg->avf);
  552. seg->avf = NULL;
  553. }
  554. static int seg_init(AVFormatContext *s)
  555. {
  556. SegmentContext *seg = s->priv_data;
  557. AVFormatContext *oc = seg->avf;
  558. AVDictionary *options = NULL;
  559. int ret;
  560. int i;
  561. seg->segment_count = 0;
  562. if (!seg->write_header_trailer)
  563. seg->individual_header_trailer = 0;
  564. if (seg->header_filename) {
  565. seg->write_header_trailer = 1;
  566. seg->individual_header_trailer = 0;
  567. }
  568. if (seg->initial_offset > 0) {
  569. av_log(s, AV_LOG_WARNING, "NOTE: the option initial_offset is deprecated,"
  570. "you can use output_ts_offset instead of it\n");
  571. }
  572. if (!!seg->time_str + !!seg->times_str + !!seg->frames_str > 1) {
  573. av_log(s, AV_LOG_ERROR,
  574. "segment_time, segment_times, and segment_frames options "
  575. "are mutually exclusive, select just one of them\n");
  576. return AVERROR(EINVAL);
  577. }
  578. if (seg->times_str) {
  579. if ((ret = parse_times(s, &seg->times, &seg->nb_times, seg->times_str)) < 0)
  580. return ret;
  581. } else if (seg->frames_str) {
  582. if ((ret = parse_frames(s, &seg->frames, &seg->nb_frames, seg->frames_str)) < 0)
  583. return ret;
  584. } else {
  585. /* set default value if not specified */
  586. if (!seg->time_str)
  587. seg->time_str = av_strdup("2");
  588. if ((ret = av_parse_time(&seg->time, seg->time_str, 1)) < 0) {
  589. av_log(s, AV_LOG_ERROR,
  590. "Invalid time duration specification '%s' for segment_time option\n",
  591. seg->time_str);
  592. return ret;
  593. }
  594. if (seg->use_clocktime) {
  595. if (seg->time <= 0) {
  596. av_log(s, AV_LOG_ERROR, "Invalid negative segment_time with segment_atclocktime option set\n");
  597. return AVERROR(EINVAL);
  598. }
  599. seg->clocktime_offset = seg->time - (seg->clocktime_offset % seg->time);
  600. }
  601. }
  602. if (seg->format_options_str) {
  603. ret = av_dict_parse_string(&seg->format_options, seg->format_options_str, "=", ":", 0);
  604. if (ret < 0) {
  605. av_log(s, AV_LOG_ERROR, "Could not parse format options list '%s'\n",
  606. seg->format_options_str);
  607. return ret;
  608. }
  609. }
  610. if (seg->list) {
  611. if (seg->list_type == LIST_TYPE_UNDEFINED) {
  612. if (av_match_ext(seg->list, "csv" )) seg->list_type = LIST_TYPE_CSV;
  613. else if (av_match_ext(seg->list, "ext" )) seg->list_type = LIST_TYPE_EXT;
  614. else if (av_match_ext(seg->list, "m3u8")) seg->list_type = LIST_TYPE_M3U8;
  615. else if (av_match_ext(seg->list, "ffcat,ffconcat")) seg->list_type = LIST_TYPE_FFCONCAT;
  616. else seg->list_type = LIST_TYPE_FLAT;
  617. }
  618. if (!seg->list_size && seg->list_type != LIST_TYPE_M3U8) {
  619. if ((ret = segment_list_open(s)) < 0)
  620. return ret;
  621. } else {
  622. const char *proto = avio_find_protocol_name(seg->list);
  623. seg->use_rename = proto && !strcmp(proto, "file");
  624. }
  625. }
  626. if (seg->list_type == LIST_TYPE_EXT)
  627. av_log(s, AV_LOG_WARNING, "'ext' list type option is deprecated in favor of 'csv'\n");
  628. if ((ret = select_reference_stream(s)) < 0)
  629. return ret;
  630. av_log(s, AV_LOG_VERBOSE, "Selected stream id:%d type:%s\n",
  631. seg->reference_stream_index,
  632. av_get_media_type_string(s->streams[seg->reference_stream_index]->codecpar->codec_type));
  633. seg->oformat = av_guess_format(seg->format, s->filename, NULL);
  634. if (!seg->oformat)
  635. return AVERROR_MUXER_NOT_FOUND;
  636. if (seg->oformat->flags & AVFMT_NOFILE) {
  637. av_log(s, AV_LOG_ERROR, "format %s not supported.\n",
  638. seg->oformat->name);
  639. return AVERROR(EINVAL);
  640. }
  641. if ((ret = segment_mux_init(s)) < 0)
  642. return ret;
  643. if ((ret = set_segment_filename(s)) < 0)
  644. return ret;
  645. oc = seg->avf;
  646. if (seg->write_header_trailer) {
  647. if ((ret = s->io_open(s, &oc->pb,
  648. seg->header_filename ? seg->header_filename : oc->filename,
  649. AVIO_FLAG_WRITE, NULL)) < 0) {
  650. av_log(s, AV_LOG_ERROR, "Failed to open segment '%s'\n", oc->filename);
  651. return ret;
  652. }
  653. if (!seg->individual_header_trailer)
  654. oc->pb->seekable = 0;
  655. } else {
  656. if ((ret = open_null_ctx(&oc->pb)) < 0)
  657. return ret;
  658. }
  659. av_dict_copy(&options, seg->format_options, 0);
  660. av_dict_set(&options, "fflags", "-autobsf", 0);
  661. ret = avformat_init_output(oc, &options);
  662. if (av_dict_count(options)) {
  663. av_log(s, AV_LOG_ERROR,
  664. "Some of the provided format options in '%s' are not recognized\n", seg->format_options_str);
  665. av_dict_free(&options);
  666. return AVERROR(EINVAL);
  667. }
  668. av_dict_free(&options);
  669. if (ret < 0) {
  670. ff_format_io_close(oc, &oc->pb);
  671. return ret;
  672. }
  673. seg->segment_frame_count = 0;
  674. av_assert0(s->nb_streams == oc->nb_streams);
  675. if (ret == AVSTREAM_INIT_IN_WRITE_HEADER) {
  676. ret = avformat_write_header(oc, NULL);
  677. if (ret < 0)
  678. return ret;
  679. seg->header_written = 1;
  680. }
  681. for (i = 0; i < s->nb_streams; i++) {
  682. AVStream *inner_st = oc->streams[i];
  683. AVStream *outer_st = s->streams[i];
  684. avpriv_set_pts_info(outer_st, inner_st->pts_wrap_bits, inner_st->time_base.num, inner_st->time_base.den);
  685. }
  686. if (oc->avoid_negative_ts > 0 && s->avoid_negative_ts < 0)
  687. s->avoid_negative_ts = 1;
  688. return ret;
  689. }
  690. static int seg_write_header(AVFormatContext *s)
  691. {
  692. SegmentContext *seg = s->priv_data;
  693. AVFormatContext *oc = seg->avf;
  694. int ret, i;
  695. if (!seg->header_written) {
  696. for (i = 0; i < s->nb_streams; i++) {
  697. AVStream *st = oc->streams[i];
  698. AVCodecParameters *ipar, *opar;
  699. ipar = s->streams[i]->codecpar;
  700. opar = oc->streams[i]->codecpar;
  701. avcodec_parameters_copy(opar, ipar);
  702. if (!oc->oformat->codec_tag ||
  703. av_codec_get_id (oc->oformat->codec_tag, ipar->codec_tag) == opar->codec_id ||
  704. av_codec_get_tag(oc->oformat->codec_tag, ipar->codec_id) <= 0) {
  705. opar->codec_tag = ipar->codec_tag;
  706. } else {
  707. opar->codec_tag = 0;
  708. }
  709. st->sample_aspect_ratio = s->streams[i]->sample_aspect_ratio;
  710. st->time_base = s->streams[i]->time_base;
  711. }
  712. ret = avformat_write_header(oc, NULL);
  713. if (ret < 0)
  714. return ret;
  715. }
  716. if (!seg->write_header_trailer || seg->header_filename) {
  717. if (seg->header_filename) {
  718. av_write_frame(oc, NULL);
  719. ff_format_io_close(oc, &oc->pb);
  720. } else {
  721. close_null_ctxp(&oc->pb);
  722. }
  723. if ((ret = oc->io_open(oc, &oc->pb, oc->filename, AVIO_FLAG_WRITE, NULL)) < 0)
  724. return ret;
  725. if (!seg->individual_header_trailer)
  726. oc->pb->seekable = 0;
  727. }
  728. return 0;
  729. }
  730. static int seg_write_packet(AVFormatContext *s, AVPacket *pkt)
  731. {
  732. SegmentContext *seg = s->priv_data;
  733. AVStream *st = s->streams[pkt->stream_index];
  734. int64_t end_pts = INT64_MAX, offset;
  735. int start_frame = INT_MAX;
  736. int ret;
  737. struct tm ti;
  738. int64_t usecs;
  739. int64_t wrapped_val;
  740. if (!seg->avf || !seg->avf->pb)
  741. return AVERROR(EINVAL);
  742. calc_times:
  743. if (seg->times) {
  744. end_pts = seg->segment_count < seg->nb_times ?
  745. seg->times[seg->segment_count] : INT64_MAX;
  746. } else if (seg->frames) {
  747. start_frame = seg->segment_count < seg->nb_frames ?
  748. seg->frames[seg->segment_count] : INT_MAX;
  749. } else {
  750. if (seg->use_clocktime) {
  751. int64_t avgt = av_gettime();
  752. time_t sec = avgt / 1000000;
  753. localtime_r(&sec, &ti);
  754. usecs = (int64_t)(ti.tm_hour * 3600 + ti.tm_min * 60 + ti.tm_sec) * 1000000 + (avgt % 1000000);
  755. wrapped_val = (usecs + seg->clocktime_offset) % seg->time;
  756. if (seg->last_cut != usecs && wrapped_val < seg->last_val && wrapped_val < seg->clocktime_wrap_duration) {
  757. seg->cut_pending = 1;
  758. seg->last_cut = usecs;
  759. }
  760. seg->last_val = wrapped_val;
  761. } else {
  762. end_pts = seg->time * (seg->segment_count + 1);
  763. }
  764. }
  765. ff_dlog(s, "packet stream:%d pts:%s pts_time:%s duration_time:%s is_key:%d frame:%d\n",
  766. pkt->stream_index, av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &st->time_base),
  767. av_ts2timestr(pkt->duration, &st->time_base),
  768. pkt->flags & AV_PKT_FLAG_KEY,
  769. pkt->stream_index == seg->reference_stream_index ? seg->frame_count : -1);
  770. if (pkt->stream_index == seg->reference_stream_index &&
  771. (pkt->flags & AV_PKT_FLAG_KEY || seg->break_non_keyframes) &&
  772. (seg->segment_frame_count > 0 || seg->write_empty) &&
  773. (seg->cut_pending || seg->frame_count >= start_frame ||
  774. (pkt->pts != AV_NOPTS_VALUE &&
  775. av_compare_ts(pkt->pts, st->time_base,
  776. end_pts - seg->time_delta, AV_TIME_BASE_Q) >= 0))) {
  777. /* sanitize end time in case last packet didn't have a defined duration */
  778. if (seg->cur_entry.last_duration == 0)
  779. seg->cur_entry.end_time = (double)pkt->pts * av_q2d(st->time_base);
  780. if ((ret = segment_end(s, seg->individual_header_trailer, 0)) < 0)
  781. goto fail;
  782. if ((ret = segment_start(s, seg->individual_header_trailer)) < 0)
  783. goto fail;
  784. seg->cut_pending = 0;
  785. seg->cur_entry.index = seg->segment_idx + seg->segment_idx_wrap * seg->segment_idx_wrap_nb;
  786. seg->cur_entry.start_time = (double)pkt->pts * av_q2d(st->time_base);
  787. seg->cur_entry.start_pts = av_rescale_q(pkt->pts, st->time_base, AV_TIME_BASE_Q);
  788. seg->cur_entry.end_time = seg->cur_entry.start_time;
  789. if (seg->times || (!seg->frames && !seg->use_clocktime) && seg->write_empty)
  790. goto calc_times;
  791. }
  792. if (pkt->stream_index == seg->reference_stream_index) {
  793. if (pkt->pts != AV_NOPTS_VALUE)
  794. seg->cur_entry.end_time =
  795. FFMAX(seg->cur_entry.end_time, (double)(pkt->pts + pkt->duration) * av_q2d(st->time_base));
  796. seg->cur_entry.last_duration = pkt->duration;
  797. }
  798. if (seg->segment_frame_count == 0) {
  799. av_log(s, AV_LOG_VERBOSE, "segment:'%s' starts with packet stream:%d pts:%s pts_time:%s frame:%d\n",
  800. seg->avf->filename, pkt->stream_index,
  801. av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &st->time_base), seg->frame_count);
  802. }
  803. av_log(s, AV_LOG_DEBUG, "stream:%d start_pts_time:%s pts:%s pts_time:%s dts:%s dts_time:%s",
  804. pkt->stream_index,
  805. av_ts2timestr(seg->cur_entry.start_pts, &AV_TIME_BASE_Q),
  806. av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &st->time_base),
  807. av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &st->time_base));
  808. /* compute new timestamps */
  809. offset = av_rescale_q(seg->initial_offset - (seg->reset_timestamps ? seg->cur_entry.start_pts : 0),
  810. AV_TIME_BASE_Q, st->time_base);
  811. if (pkt->pts != AV_NOPTS_VALUE)
  812. pkt->pts += offset;
  813. if (pkt->dts != AV_NOPTS_VALUE)
  814. pkt->dts += offset;
  815. av_log(s, AV_LOG_DEBUG, " -> pts:%s pts_time:%s dts:%s dts_time:%s\n",
  816. av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &st->time_base),
  817. av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &st->time_base));
  818. ret = ff_write_chained(seg->avf, pkt->stream_index, pkt, s, seg->initial_offset || seg->reset_timestamps);
  819. fail:
  820. if (pkt->stream_index == seg->reference_stream_index) {
  821. seg->frame_count++;
  822. seg->segment_frame_count++;
  823. }
  824. return ret;
  825. }
  826. static int seg_write_trailer(struct AVFormatContext *s)
  827. {
  828. SegmentContext *seg = s->priv_data;
  829. AVFormatContext *oc = seg->avf;
  830. SegmentListEntry *cur, *next;
  831. int ret = 0;
  832. if (!oc)
  833. goto fail;
  834. if (!seg->write_header_trailer) {
  835. if ((ret = segment_end(s, 0, 1)) < 0)
  836. goto fail;
  837. if ((ret = open_null_ctx(&oc->pb)) < 0)
  838. goto fail;
  839. ret = av_write_trailer(oc);
  840. close_null_ctxp(&oc->pb);
  841. } else {
  842. ret = segment_end(s, 1, 1);
  843. }
  844. fail:
  845. if (seg->list)
  846. ff_format_io_close(s, &seg->list_pb);
  847. av_dict_free(&seg->format_options);
  848. av_opt_free(seg);
  849. av_freep(&seg->times);
  850. av_freep(&seg->frames);
  851. av_freep(&seg->cur_entry.filename);
  852. cur = seg->segment_list_entries;
  853. while (cur) {
  854. next = cur->next;
  855. av_freep(&cur->filename);
  856. av_free(cur);
  857. cur = next;
  858. }
  859. avformat_free_context(oc);
  860. seg->avf = NULL;
  861. return ret;
  862. }
  863. static int seg_check_bitstream(struct AVFormatContext *s, const AVPacket *pkt)
  864. {
  865. SegmentContext *seg = s->priv_data;
  866. AVFormatContext *oc = seg->avf;
  867. if (oc->oformat->check_bitstream) {
  868. int ret = oc->oformat->check_bitstream(oc, pkt);
  869. if (ret == 1) {
  870. AVStream *st = s->streams[pkt->stream_index];
  871. AVStream *ost = oc->streams[pkt->stream_index];
  872. st->internal->bsfcs = ost->internal->bsfcs;
  873. st->internal->nb_bsfcs = ost->internal->nb_bsfcs;
  874. ost->internal->bsfcs = NULL;
  875. ost->internal->nb_bsfcs = 0;
  876. }
  877. return ret;
  878. }
  879. return 1;
  880. }
  881. #define OFFSET(x) offsetof(SegmentContext, x)
  882. #define E AV_OPT_FLAG_ENCODING_PARAM
  883. static const AVOption options[] = {
  884. { "reference_stream", "set reference stream", OFFSET(reference_stream_specifier), AV_OPT_TYPE_STRING, {.str = "auto"}, CHAR_MIN, CHAR_MAX, E },
  885. { "segment_format", "set container format used for the segments", OFFSET(format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
  886. { "segment_format_options", "set list of options for the container format used for the segments", OFFSET(format_options_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
  887. { "segment_list", "set the segment list filename", OFFSET(list), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
  888. { "segment_header_filename", "write a single file containing the header", OFFSET(header_filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
  889. { "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"},
  890. { "cache", "allow list caching", 0, AV_OPT_TYPE_CONST, {.i64 = SEGMENT_LIST_FLAG_CACHE }, INT_MIN, INT_MAX, E, "list_flags"},
  891. { "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"},
  892. { "segment_list_size", "set the maximum number of playlist entries", OFFSET(list_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E },
  893. { "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" },
  894. { "flat", "flat format", 0, AV_OPT_TYPE_CONST, {.i64=LIST_TYPE_FLAT }, INT_MIN, INT_MAX, E, "list_type" },
  895. { "csv", "csv format", 0, AV_OPT_TYPE_CONST, {.i64=LIST_TYPE_CSV }, INT_MIN, INT_MAX, E, "list_type" },
  896. { "ext", "extended format", 0, AV_OPT_TYPE_CONST, {.i64=LIST_TYPE_EXT }, INT_MIN, INT_MAX, E, "list_type" },
  897. { "ffconcat", "ffconcat format", 0, AV_OPT_TYPE_CONST, {.i64=LIST_TYPE_FFCONCAT }, INT_MIN, INT_MAX, E, "list_type" },
  898. { "m3u8", "M3U8 format", 0, AV_OPT_TYPE_CONST, {.i64=LIST_TYPE_M3U8 }, INT_MIN, INT_MAX, E, "list_type" },
  899. { "hls", "Apple HTTP Live Streaming compatible", 0, AV_OPT_TYPE_CONST, {.i64=LIST_TYPE_M3U8 }, INT_MIN, INT_MAX, E, "list_type" },
  900. { "segment_atclocktime", "set segment to be cut at clocktime", OFFSET(use_clocktime), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, E},
  901. { "segment_clocktime_offset", "set segment clocktime offset", OFFSET(clocktime_offset), AV_OPT_TYPE_DURATION, {.i64 = 0}, 0, 86400000000LL, E},
  902. { "segment_clocktime_wrap_duration", "set segment clocktime wrapping duration", OFFSET(clocktime_wrap_duration), AV_OPT_TYPE_DURATION, {.i64 = INT64_MAX}, 0, INT64_MAX, E},
  903. { "segment_time", "set segment duration", OFFSET(time_str),AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
  904. { "segment_time_delta","set approximation value used for the segment times", OFFSET(time_delta), AV_OPT_TYPE_DURATION, {.i64 = 0}, 0, 0, E },
  905. { "segment_times", "set segment split time points", OFFSET(times_str),AV_OPT_TYPE_STRING,{.str = NULL}, 0, 0, E },
  906. { "segment_frames", "set segment split frame numbers", OFFSET(frames_str),AV_OPT_TYPE_STRING,{.str = NULL}, 0, 0, E },
  907. { "segment_wrap", "set number after which the index wraps", OFFSET(segment_idx_wrap), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E },
  908. { "segment_list_entry_prefix", "set base url prefix for segments", OFFSET(entry_prefix), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
  909. { "segment_start_number", "set the sequence number of the first segment", OFFSET(segment_idx), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E },
  910. { "segment_wrap_number", "set the number of wrap before the first segment", OFFSET(segment_idx_wrap_nb), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E },
  911. { "strftime", "set filename expansion with strftime at segment creation", OFFSET(use_strftime), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, E },
  912. { "increment_tc", "increment timecode between each segment", OFFSET(increment_tc), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, E },
  913. { "break_non_keyframes", "allow breaking segments on non-keyframes", OFFSET(break_non_keyframes), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, E },
  914. { "individual_header_trailer", "write header/trailer to each segment", OFFSET(individual_header_trailer), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, E },
  915. { "write_header_trailer", "write a header to the first segment and a trailer to the last one", OFFSET(write_header_trailer), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, E },
  916. { "reset_timestamps", "reset timestamps at the begin of each segment", OFFSET(reset_timestamps), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, E },
  917. { "initial_offset", "set initial timestamp offset", OFFSET(initial_offset), AV_OPT_TYPE_DURATION, {.i64 = 0}, -INT64_MAX, INT64_MAX, E },
  918. { "write_empty_segments", "allow writing empty 'filler' segments", OFFSET(write_empty), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, E },
  919. { NULL },
  920. };
  921. static const AVClass seg_class = {
  922. .class_name = "segment muxer",
  923. .item_name = av_default_item_name,
  924. .option = options,
  925. .version = LIBAVUTIL_VERSION_INT,
  926. };
  927. AVOutputFormat ff_segment_muxer = {
  928. .name = "segment",
  929. .long_name = NULL_IF_CONFIG_SMALL("segment"),
  930. .priv_data_size = sizeof(SegmentContext),
  931. .flags = AVFMT_NOFILE|AVFMT_GLOBALHEADER,
  932. .init = seg_init,
  933. .write_header = seg_write_header,
  934. .write_packet = seg_write_packet,
  935. .write_trailer = seg_write_trailer,
  936. .deinit = seg_free,
  937. .check_bitstream = seg_check_bitstream,
  938. .priv_class = &seg_class,
  939. };
  940. static const AVClass sseg_class = {
  941. .class_name = "stream_segment muxer",
  942. .item_name = av_default_item_name,
  943. .option = options,
  944. .version = LIBAVUTIL_VERSION_INT,
  945. };
  946. AVOutputFormat ff_stream_segment_muxer = {
  947. .name = "stream_segment,ssegment",
  948. .long_name = NULL_IF_CONFIG_SMALL("streaming segment muxer"),
  949. .priv_data_size = sizeof(SegmentContext),
  950. .flags = AVFMT_NOFILE,
  951. .init = seg_init,
  952. .write_header = seg_write_header,
  953. .write_packet = seg_write_packet,
  954. .write_trailer = seg_write_trailer,
  955. .deinit = seg_free,
  956. .check_bitstream = seg_check_bitstream,
  957. .priv_class = &sseg_class,
  958. };