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.

1104 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. av_write_frame(oc, NULL); /* Flush any buffered data (fragmented mp4) */
  309. if (write_trailer)
  310. ret = av_write_trailer(oc);
  311. if (ret < 0)
  312. av_log(s, AV_LOG_ERROR, "Failure occurred when ending segment '%s'\n",
  313. oc->filename);
  314. if (seg->list) {
  315. if (seg->list_size || seg->list_type == LIST_TYPE_M3U8) {
  316. SegmentListEntry *entry = av_mallocz(sizeof(*entry));
  317. if (!entry) {
  318. ret = AVERROR(ENOMEM);
  319. goto end;
  320. }
  321. /* append new element */
  322. memcpy(entry, &seg->cur_entry, sizeof(*entry));
  323. entry->filename = av_strdup(entry->filename);
  324. if (!seg->segment_list_entries)
  325. seg->segment_list_entries = seg->segment_list_entries_end = entry;
  326. else
  327. seg->segment_list_entries_end->next = entry;
  328. seg->segment_list_entries_end = entry;
  329. /* drop first item */
  330. if (seg->list_size && seg->segment_count >= seg->list_size) {
  331. entry = seg->segment_list_entries;
  332. seg->segment_list_entries = seg->segment_list_entries->next;
  333. av_freep(&entry->filename);
  334. av_freep(&entry);
  335. }
  336. if ((ret = segment_list_open(s)) < 0)
  337. goto end;
  338. for (entry = seg->segment_list_entries; entry; entry = entry->next)
  339. segment_list_print_entry(seg->list_pb, seg->list_type, entry, s);
  340. if (seg->list_type == LIST_TYPE_M3U8 && is_last)
  341. avio_printf(seg->list_pb, "#EXT-X-ENDLIST\n");
  342. ff_format_io_close(s, &seg->list_pb);
  343. if (seg->use_rename)
  344. ff_rename(seg->temp_list_filename, seg->list, s);
  345. } else {
  346. segment_list_print_entry(seg->list_pb, seg->list_type, &seg->cur_entry, s);
  347. avio_flush(seg->list_pb);
  348. }
  349. }
  350. av_log(s, AV_LOG_VERBOSE, "segment:'%s' count:%d ended\n",
  351. seg->avf->filename, seg->segment_count);
  352. seg->segment_count++;
  353. if (seg->increment_tc) {
  354. tcr = av_dict_get(s->metadata, "timecode", NULL, 0);
  355. if (tcr) {
  356. /* search the first video stream */
  357. for (i = 0; i < s->nb_streams; i++) {
  358. if (s->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
  359. rate = s->streams[i]->avg_frame_rate;/* Get fps from the video stream */
  360. err = av_timecode_init_from_string(&tc, rate, tcr->value, s);
  361. if (err < 0) {
  362. av_log(s, AV_LOG_WARNING, "Could not increment timecode, error occurred during timecode creation.");
  363. break;
  364. }
  365. tc.start += (int)((seg->cur_entry.end_time - seg->cur_entry.start_time) * av_q2d(rate));/* increment timecode */
  366. av_dict_set(&s->metadata, "timecode",
  367. av_timecode_make_string(&tc, buf, 0), 0);
  368. break;
  369. }
  370. }
  371. } else {
  372. av_log(s, AV_LOG_WARNING, "Could not increment timecode, no timecode metadata found");
  373. }
  374. }
  375. end:
  376. ff_format_io_close(oc, &oc->pb);
  377. return ret;
  378. }
  379. static int parse_times(void *log_ctx, int64_t **times, int *nb_times,
  380. const char *times_str)
  381. {
  382. char *p;
  383. int i, ret = 0;
  384. char *times_str1 = av_strdup(times_str);
  385. char *saveptr = NULL;
  386. if (!times_str1)
  387. return AVERROR(ENOMEM);
  388. #define FAIL(err) ret = err; goto end
  389. *nb_times = 1;
  390. for (p = times_str1; *p; p++)
  391. if (*p == ',')
  392. (*nb_times)++;
  393. *times = av_malloc_array(*nb_times, sizeof(**times));
  394. if (!*times) {
  395. av_log(log_ctx, AV_LOG_ERROR, "Could not allocate forced times array\n");
  396. FAIL(AVERROR(ENOMEM));
  397. }
  398. p = times_str1;
  399. for (i = 0; i < *nb_times; i++) {
  400. int64_t t;
  401. char *tstr = av_strtok(p, ",", &saveptr);
  402. p = NULL;
  403. if (!tstr || !tstr[0]) {
  404. av_log(log_ctx, AV_LOG_ERROR, "Empty time specification in times list %s\n",
  405. times_str);
  406. FAIL(AVERROR(EINVAL));
  407. }
  408. ret = av_parse_time(&t, tstr, 1);
  409. if (ret < 0) {
  410. av_log(log_ctx, AV_LOG_ERROR,
  411. "Invalid time duration specification '%s' in times list %s\n", tstr, times_str);
  412. FAIL(AVERROR(EINVAL));
  413. }
  414. (*times)[i] = t;
  415. /* check on monotonicity */
  416. if (i && (*times)[i-1] > (*times)[i]) {
  417. av_log(log_ctx, AV_LOG_ERROR,
  418. "Specified time %f is greater than the following time %f\n",
  419. (float)((*times)[i])/1000000, (float)((*times)[i-1])/1000000);
  420. FAIL(AVERROR(EINVAL));
  421. }
  422. }
  423. end:
  424. av_free(times_str1);
  425. return ret;
  426. }
  427. static int parse_frames(void *log_ctx, int **frames, int *nb_frames,
  428. const char *frames_str)
  429. {
  430. char *p;
  431. int i, ret = 0;
  432. char *frames_str1 = av_strdup(frames_str);
  433. char *saveptr = NULL;
  434. if (!frames_str1)
  435. return AVERROR(ENOMEM);
  436. #define FAIL(err) ret = err; goto end
  437. *nb_frames = 1;
  438. for (p = frames_str1; *p; p++)
  439. if (*p == ',')
  440. (*nb_frames)++;
  441. *frames = av_malloc_array(*nb_frames, sizeof(**frames));
  442. if (!*frames) {
  443. av_log(log_ctx, AV_LOG_ERROR, "Could not allocate forced frames array\n");
  444. FAIL(AVERROR(ENOMEM));
  445. }
  446. p = frames_str1;
  447. for (i = 0; i < *nb_frames; i++) {
  448. long int f;
  449. char *tailptr;
  450. char *fstr = av_strtok(p, ",", &saveptr);
  451. p = NULL;
  452. if (!fstr) {
  453. av_log(log_ctx, AV_LOG_ERROR, "Empty frame specification in frame list %s\n",
  454. frames_str);
  455. FAIL(AVERROR(EINVAL));
  456. }
  457. f = strtol(fstr, &tailptr, 10);
  458. if (*tailptr || f <= 0 || f >= INT_MAX) {
  459. av_log(log_ctx, AV_LOG_ERROR,
  460. "Invalid argument '%s', must be a positive integer <= INT64_MAX\n",
  461. fstr);
  462. FAIL(AVERROR(EINVAL));
  463. }
  464. (*frames)[i] = f;
  465. /* check on monotonicity */
  466. if (i && (*frames)[i-1] > (*frames)[i]) {
  467. av_log(log_ctx, AV_LOG_ERROR,
  468. "Specified frame %d is greater than the following frame %d\n",
  469. (*frames)[i], (*frames)[i-1]);
  470. FAIL(AVERROR(EINVAL));
  471. }
  472. }
  473. end:
  474. av_free(frames_str1);
  475. return ret;
  476. }
  477. static int open_null_ctx(AVIOContext **ctx)
  478. {
  479. int buf_size = 32768;
  480. uint8_t *buf = av_malloc(buf_size);
  481. if (!buf)
  482. return AVERROR(ENOMEM);
  483. *ctx = avio_alloc_context(buf, buf_size, AVIO_FLAG_WRITE, NULL, NULL, NULL, NULL);
  484. if (!*ctx) {
  485. av_free(buf);
  486. return AVERROR(ENOMEM);
  487. }
  488. return 0;
  489. }
  490. static void close_null_ctxp(AVIOContext **pb)
  491. {
  492. av_freep(&(*pb)->buffer);
  493. av_freep(pb);
  494. }
  495. static int select_reference_stream(AVFormatContext *s)
  496. {
  497. SegmentContext *seg = s->priv_data;
  498. int ret, i;
  499. seg->reference_stream_index = -1;
  500. if (!strcmp(seg->reference_stream_specifier, "auto")) {
  501. /* select first index of type with highest priority */
  502. int type_index_map[AVMEDIA_TYPE_NB];
  503. static const enum AVMediaType type_priority_list[] = {
  504. AVMEDIA_TYPE_VIDEO,
  505. AVMEDIA_TYPE_AUDIO,
  506. AVMEDIA_TYPE_SUBTITLE,
  507. AVMEDIA_TYPE_DATA,
  508. AVMEDIA_TYPE_ATTACHMENT
  509. };
  510. enum AVMediaType type;
  511. for (i = 0; i < AVMEDIA_TYPE_NB; i++)
  512. type_index_map[i] = -1;
  513. /* select first index for each type */
  514. for (i = 0; i < s->nb_streams; i++) {
  515. type = s->streams[i]->codecpar->codec_type;
  516. if ((unsigned)type < AVMEDIA_TYPE_NB && type_index_map[type] == -1
  517. /* ignore attached pictures/cover art streams */
  518. && !(s->streams[i]->disposition & AV_DISPOSITION_ATTACHED_PIC))
  519. type_index_map[type] = i;
  520. }
  521. for (i = 0; i < FF_ARRAY_ELEMS(type_priority_list); i++) {
  522. type = type_priority_list[i];
  523. if ((seg->reference_stream_index = type_index_map[type]) >= 0)
  524. break;
  525. }
  526. } else {
  527. for (i = 0; i < s->nb_streams; i++) {
  528. ret = avformat_match_stream_specifier(s, s->streams[i],
  529. seg->reference_stream_specifier);
  530. if (ret < 0)
  531. return ret;
  532. if (ret > 0) {
  533. seg->reference_stream_index = i;
  534. break;
  535. }
  536. }
  537. }
  538. if (seg->reference_stream_index < 0) {
  539. av_log(s, AV_LOG_ERROR, "Could not select stream matching identifier '%s'\n",
  540. seg->reference_stream_specifier);
  541. return AVERROR(EINVAL);
  542. }
  543. return 0;
  544. }
  545. static void seg_free(AVFormatContext *s)
  546. {
  547. SegmentContext *seg = s->priv_data;
  548. ff_format_io_close(seg->avf, &seg->list_pb);
  549. avformat_free_context(seg->avf);
  550. seg->avf = NULL;
  551. }
  552. static int seg_init(AVFormatContext *s)
  553. {
  554. SegmentContext *seg = s->priv_data;
  555. AVFormatContext *oc = seg->avf;
  556. AVDictionary *options = NULL;
  557. int ret;
  558. int i;
  559. seg->segment_count = 0;
  560. if (!seg->write_header_trailer)
  561. seg->individual_header_trailer = 0;
  562. if (seg->header_filename) {
  563. seg->write_header_trailer = 1;
  564. seg->individual_header_trailer = 0;
  565. }
  566. if (seg->initial_offset > 0) {
  567. av_log(s, AV_LOG_WARNING, "NOTE: the option initial_offset is deprecated,"
  568. "you can use output_ts_offset instead of it\n");
  569. }
  570. if (!!seg->time_str + !!seg->times_str + !!seg->frames_str > 1) {
  571. av_log(s, AV_LOG_ERROR,
  572. "segment_time, segment_times, and segment_frames options "
  573. "are mutually exclusive, select just one of them\n");
  574. return AVERROR(EINVAL);
  575. }
  576. if (seg->times_str) {
  577. if ((ret = parse_times(s, &seg->times, &seg->nb_times, seg->times_str)) < 0)
  578. return ret;
  579. } else if (seg->frames_str) {
  580. if ((ret = parse_frames(s, &seg->frames, &seg->nb_frames, seg->frames_str)) < 0)
  581. return ret;
  582. } else {
  583. /* set default value if not specified */
  584. if (!seg->time_str)
  585. seg->time_str = av_strdup("2");
  586. if ((ret = av_parse_time(&seg->time, seg->time_str, 1)) < 0) {
  587. av_log(s, AV_LOG_ERROR,
  588. "Invalid time duration specification '%s' for segment_time option\n",
  589. seg->time_str);
  590. return ret;
  591. }
  592. if (seg->use_clocktime) {
  593. if (seg->time <= 0) {
  594. av_log(s, AV_LOG_ERROR, "Invalid negative segment_time with segment_atclocktime option set\n");
  595. return AVERROR(EINVAL);
  596. }
  597. seg->clocktime_offset = seg->time - (seg->clocktime_offset % seg->time);
  598. }
  599. }
  600. if (seg->format_options_str) {
  601. ret = av_dict_parse_string(&seg->format_options, seg->format_options_str, "=", ":", 0);
  602. if (ret < 0) {
  603. av_log(s, AV_LOG_ERROR, "Could not parse format options list '%s'\n",
  604. seg->format_options_str);
  605. return ret;
  606. }
  607. }
  608. if (seg->list) {
  609. if (seg->list_type == LIST_TYPE_UNDEFINED) {
  610. if (av_match_ext(seg->list, "csv" )) seg->list_type = LIST_TYPE_CSV;
  611. else if (av_match_ext(seg->list, "ext" )) seg->list_type = LIST_TYPE_EXT;
  612. else if (av_match_ext(seg->list, "m3u8")) seg->list_type = LIST_TYPE_M3U8;
  613. else if (av_match_ext(seg->list, "ffcat,ffconcat")) seg->list_type = LIST_TYPE_FFCONCAT;
  614. else seg->list_type = LIST_TYPE_FLAT;
  615. }
  616. if (!seg->list_size && seg->list_type != LIST_TYPE_M3U8) {
  617. if ((ret = segment_list_open(s)) < 0)
  618. return ret;
  619. } else {
  620. const char *proto = avio_find_protocol_name(seg->list);
  621. seg->use_rename = proto && !strcmp(proto, "file");
  622. }
  623. }
  624. if (seg->list_type == LIST_TYPE_EXT)
  625. av_log(s, AV_LOG_WARNING, "'ext' list type option is deprecated in favor of 'csv'\n");
  626. if ((ret = select_reference_stream(s)) < 0)
  627. return ret;
  628. av_log(s, AV_LOG_VERBOSE, "Selected stream id:%d type:%s\n",
  629. seg->reference_stream_index,
  630. av_get_media_type_string(s->streams[seg->reference_stream_index]->codecpar->codec_type));
  631. seg->oformat = av_guess_format(seg->format, s->filename, NULL);
  632. if (!seg->oformat)
  633. return AVERROR_MUXER_NOT_FOUND;
  634. if (seg->oformat->flags & AVFMT_NOFILE) {
  635. av_log(s, AV_LOG_ERROR, "format %s not supported.\n",
  636. seg->oformat->name);
  637. return AVERROR(EINVAL);
  638. }
  639. if ((ret = segment_mux_init(s)) < 0)
  640. return ret;
  641. if ((ret = set_segment_filename(s)) < 0)
  642. return ret;
  643. oc = seg->avf;
  644. if (seg->write_header_trailer) {
  645. if ((ret = s->io_open(s, &oc->pb,
  646. seg->header_filename ? seg->header_filename : oc->filename,
  647. AVIO_FLAG_WRITE, NULL)) < 0) {
  648. av_log(s, AV_LOG_ERROR, "Failed to open segment '%s'\n", oc->filename);
  649. return ret;
  650. }
  651. if (!seg->individual_header_trailer)
  652. oc->pb->seekable = 0;
  653. } else {
  654. if ((ret = open_null_ctx(&oc->pb)) < 0)
  655. return ret;
  656. }
  657. av_dict_copy(&options, seg->format_options, 0);
  658. av_dict_set(&options, "fflags", "-autobsf", 0);
  659. ret = avformat_init_output(oc, &options);
  660. if (av_dict_count(options)) {
  661. av_log(s, AV_LOG_ERROR,
  662. "Some of the provided format options in '%s' are not recognized\n", seg->format_options_str);
  663. av_dict_free(&options);
  664. return AVERROR(EINVAL);
  665. }
  666. av_dict_free(&options);
  667. if (ret < 0) {
  668. ff_format_io_close(oc, &oc->pb);
  669. return ret;
  670. }
  671. seg->segment_frame_count = 0;
  672. av_assert0(s->nb_streams == oc->nb_streams);
  673. if (ret == AVSTREAM_INIT_IN_WRITE_HEADER) {
  674. ret = avformat_write_header(oc, NULL);
  675. if (ret < 0)
  676. return ret;
  677. seg->header_written = 1;
  678. }
  679. for (i = 0; i < s->nb_streams; i++) {
  680. AVStream *inner_st = oc->streams[i];
  681. AVStream *outer_st = s->streams[i];
  682. avpriv_set_pts_info(outer_st, inner_st->pts_wrap_bits, inner_st->time_base.num, inner_st->time_base.den);
  683. }
  684. if (oc->avoid_negative_ts > 0 && s->avoid_negative_ts < 0)
  685. s->avoid_negative_ts = 1;
  686. return ret;
  687. }
  688. static int seg_write_header(AVFormatContext *s)
  689. {
  690. SegmentContext *seg = s->priv_data;
  691. AVFormatContext *oc = seg->avf;
  692. int ret, i;
  693. if (!seg->header_written) {
  694. for (i = 0; i < s->nb_streams; i++) {
  695. AVStream *st = oc->streams[i];
  696. AVCodecParameters *ipar, *opar;
  697. ipar = s->streams[i]->codecpar;
  698. opar = oc->streams[i]->codecpar;
  699. avcodec_parameters_copy(opar, ipar);
  700. if (!oc->oformat->codec_tag ||
  701. av_codec_get_id (oc->oformat->codec_tag, ipar->codec_tag) == opar->codec_id ||
  702. av_codec_get_tag(oc->oformat->codec_tag, ipar->codec_id) <= 0) {
  703. opar->codec_tag = ipar->codec_tag;
  704. } else {
  705. opar->codec_tag = 0;
  706. }
  707. st->sample_aspect_ratio = s->streams[i]->sample_aspect_ratio;
  708. st->time_base = s->streams[i]->time_base;
  709. }
  710. ret = avformat_write_header(oc, NULL);
  711. if (ret < 0)
  712. return ret;
  713. }
  714. if (!seg->write_header_trailer || seg->header_filename) {
  715. if (seg->header_filename) {
  716. av_write_frame(oc, NULL);
  717. ff_format_io_close(oc, &oc->pb);
  718. } else {
  719. close_null_ctxp(&oc->pb);
  720. }
  721. if ((ret = oc->io_open(oc, &oc->pb, oc->filename, AVIO_FLAG_WRITE, NULL)) < 0)
  722. return ret;
  723. if (!seg->individual_header_trailer)
  724. oc->pb->seekable = 0;
  725. }
  726. return 0;
  727. }
  728. static int seg_write_packet(AVFormatContext *s, AVPacket *pkt)
  729. {
  730. SegmentContext *seg = s->priv_data;
  731. AVStream *st = s->streams[pkt->stream_index];
  732. int64_t end_pts = INT64_MAX, offset;
  733. int start_frame = INT_MAX;
  734. int ret;
  735. struct tm ti;
  736. int64_t usecs;
  737. int64_t wrapped_val;
  738. if (!seg->avf)
  739. return AVERROR(EINVAL);
  740. calc_times:
  741. if (seg->times) {
  742. end_pts = seg->segment_count < seg->nb_times ?
  743. seg->times[seg->segment_count] : INT64_MAX;
  744. } else if (seg->frames) {
  745. start_frame = seg->segment_count < seg->nb_frames ?
  746. seg->frames[seg->segment_count] : INT_MAX;
  747. } else {
  748. if (seg->use_clocktime) {
  749. int64_t avgt = av_gettime();
  750. time_t sec = avgt / 1000000;
  751. localtime_r(&sec, &ti);
  752. usecs = (int64_t)(ti.tm_hour * 3600 + ti.tm_min * 60 + ti.tm_sec) * 1000000 + (avgt % 1000000);
  753. wrapped_val = (usecs + seg->clocktime_offset) % seg->time;
  754. if (seg->last_cut != usecs && wrapped_val < seg->last_val && wrapped_val < seg->clocktime_wrap_duration) {
  755. seg->cut_pending = 1;
  756. seg->last_cut = usecs;
  757. }
  758. seg->last_val = wrapped_val;
  759. } else {
  760. end_pts = seg->time * (seg->segment_count + 1);
  761. }
  762. }
  763. ff_dlog(s, "packet stream:%d pts:%s pts_time:%s duration_time:%s is_key:%d frame:%d\n",
  764. pkt->stream_index, av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &st->time_base),
  765. av_ts2timestr(pkt->duration, &st->time_base),
  766. pkt->flags & AV_PKT_FLAG_KEY,
  767. pkt->stream_index == seg->reference_stream_index ? seg->frame_count : -1);
  768. if (pkt->stream_index == seg->reference_stream_index &&
  769. (pkt->flags & AV_PKT_FLAG_KEY || seg->break_non_keyframes) &&
  770. (seg->segment_frame_count > 0 || seg->write_empty) &&
  771. (seg->cut_pending || seg->frame_count >= start_frame ||
  772. (pkt->pts != AV_NOPTS_VALUE &&
  773. av_compare_ts(pkt->pts, st->time_base,
  774. end_pts - seg->time_delta, AV_TIME_BASE_Q) >= 0))) {
  775. /* sanitize end time in case last packet didn't have a defined duration */
  776. if (seg->cur_entry.last_duration == 0)
  777. seg->cur_entry.end_time = (double)pkt->pts * av_q2d(st->time_base);
  778. if ((ret = segment_end(s, seg->individual_header_trailer, 0)) < 0)
  779. goto fail;
  780. if ((ret = segment_start(s, seg->individual_header_trailer)) < 0)
  781. goto fail;
  782. seg->cut_pending = 0;
  783. seg->cur_entry.index = seg->segment_idx + seg->segment_idx_wrap * seg->segment_idx_wrap_nb;
  784. seg->cur_entry.start_time = (double)pkt->pts * av_q2d(st->time_base);
  785. seg->cur_entry.start_pts = av_rescale_q(pkt->pts, st->time_base, AV_TIME_BASE_Q);
  786. seg->cur_entry.end_time = seg->cur_entry.start_time;
  787. if (seg->times || (!seg->frames && !seg->use_clocktime) && seg->write_empty)
  788. goto calc_times;
  789. }
  790. if (pkt->stream_index == seg->reference_stream_index) {
  791. if (pkt->pts != AV_NOPTS_VALUE)
  792. seg->cur_entry.end_time =
  793. FFMAX(seg->cur_entry.end_time, (double)(pkt->pts + pkt->duration) * av_q2d(st->time_base));
  794. seg->cur_entry.last_duration = pkt->duration;
  795. }
  796. if (seg->segment_frame_count == 0) {
  797. av_log(s, AV_LOG_VERBOSE, "segment:'%s' starts with packet stream:%d pts:%s pts_time:%s frame:%d\n",
  798. seg->avf->filename, pkt->stream_index,
  799. av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &st->time_base), seg->frame_count);
  800. }
  801. av_log(s, AV_LOG_DEBUG, "stream:%d start_pts_time:%s pts:%s pts_time:%s dts:%s dts_time:%s",
  802. pkt->stream_index,
  803. av_ts2timestr(seg->cur_entry.start_pts, &AV_TIME_BASE_Q),
  804. av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &st->time_base),
  805. av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &st->time_base));
  806. /* compute new timestamps */
  807. offset = av_rescale_q(seg->initial_offset - (seg->reset_timestamps ? seg->cur_entry.start_pts : 0),
  808. AV_TIME_BASE_Q, st->time_base);
  809. if (pkt->pts != AV_NOPTS_VALUE)
  810. pkt->pts += offset;
  811. if (pkt->dts != AV_NOPTS_VALUE)
  812. pkt->dts += offset;
  813. av_log(s, AV_LOG_DEBUG, " -> pts:%s pts_time:%s dts:%s dts_time:%s\n",
  814. av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &st->time_base),
  815. av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &st->time_base));
  816. ret = ff_write_chained(seg->avf, pkt->stream_index, pkt, s, seg->initial_offset || seg->reset_timestamps);
  817. fail:
  818. if (pkt->stream_index == seg->reference_stream_index) {
  819. seg->frame_count++;
  820. seg->segment_frame_count++;
  821. }
  822. return ret;
  823. }
  824. static int seg_write_trailer(struct AVFormatContext *s)
  825. {
  826. SegmentContext *seg = s->priv_data;
  827. AVFormatContext *oc = seg->avf;
  828. SegmentListEntry *cur, *next;
  829. int ret = 0;
  830. if (!oc)
  831. goto fail;
  832. if (!seg->write_header_trailer) {
  833. if ((ret = segment_end(s, 0, 1)) < 0)
  834. goto fail;
  835. if ((ret = open_null_ctx(&oc->pb)) < 0)
  836. goto fail;
  837. ret = av_write_trailer(oc);
  838. close_null_ctxp(&oc->pb);
  839. } else {
  840. ret = segment_end(s, 1, 1);
  841. }
  842. fail:
  843. if (seg->list)
  844. ff_format_io_close(s, &seg->list_pb);
  845. av_dict_free(&seg->format_options);
  846. av_opt_free(seg);
  847. av_freep(&seg->times);
  848. av_freep(&seg->frames);
  849. av_freep(&seg->cur_entry.filename);
  850. cur = seg->segment_list_entries;
  851. while (cur) {
  852. next = cur->next;
  853. av_freep(&cur->filename);
  854. av_free(cur);
  855. cur = next;
  856. }
  857. avformat_free_context(oc);
  858. seg->avf = NULL;
  859. return ret;
  860. }
  861. static int seg_check_bitstream(struct AVFormatContext *s, const AVPacket *pkt)
  862. {
  863. SegmentContext *seg = s->priv_data;
  864. AVFormatContext *oc = seg->avf;
  865. if (oc->oformat->check_bitstream) {
  866. int ret = oc->oformat->check_bitstream(oc, pkt);
  867. if (ret == 1) {
  868. AVStream *st = s->streams[pkt->stream_index];
  869. AVStream *ost = oc->streams[pkt->stream_index];
  870. st->internal->bsfcs = ost->internal->bsfcs;
  871. st->internal->nb_bsfcs = ost->internal->nb_bsfcs;
  872. ost->internal->bsfcs = NULL;
  873. ost->internal->nb_bsfcs = 0;
  874. }
  875. return ret;
  876. }
  877. return 1;
  878. }
  879. #define OFFSET(x) offsetof(SegmentContext, x)
  880. #define E AV_OPT_FLAG_ENCODING_PARAM
  881. static const AVOption options[] = {
  882. { "reference_stream", "set reference stream", OFFSET(reference_stream_specifier), AV_OPT_TYPE_STRING, {.str = "auto"}, CHAR_MIN, CHAR_MAX, E },
  883. { "segment_format", "set container format used for the segments", OFFSET(format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
  884. { "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 },
  885. { "segment_list", "set the segment list filename", OFFSET(list), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
  886. { "segment_header_filename", "write a single file containing the header", OFFSET(header_filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
  887. { "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"},
  888. { "cache", "allow list caching", 0, AV_OPT_TYPE_CONST, {.i64 = SEGMENT_LIST_FLAG_CACHE }, INT_MIN, INT_MAX, E, "list_flags"},
  889. { "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"},
  890. { "segment_list_size", "set the maximum number of playlist entries", OFFSET(list_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E },
  891. { "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" },
  892. { "flat", "flat format", 0, AV_OPT_TYPE_CONST, {.i64=LIST_TYPE_FLAT }, INT_MIN, INT_MAX, E, "list_type" },
  893. { "csv", "csv format", 0, AV_OPT_TYPE_CONST, {.i64=LIST_TYPE_CSV }, INT_MIN, INT_MAX, E, "list_type" },
  894. { "ext", "extended format", 0, AV_OPT_TYPE_CONST, {.i64=LIST_TYPE_EXT }, INT_MIN, INT_MAX, E, "list_type" },
  895. { "ffconcat", "ffconcat format", 0, AV_OPT_TYPE_CONST, {.i64=LIST_TYPE_FFCONCAT }, INT_MIN, INT_MAX, E, "list_type" },
  896. { "m3u8", "M3U8 format", 0, AV_OPT_TYPE_CONST, {.i64=LIST_TYPE_M3U8 }, INT_MIN, INT_MAX, E, "list_type" },
  897. { "hls", "Apple HTTP Live Streaming compatible", 0, AV_OPT_TYPE_CONST, {.i64=LIST_TYPE_M3U8 }, INT_MIN, INT_MAX, E, "list_type" },
  898. { "segment_atclocktime", "set segment to be cut at clocktime", OFFSET(use_clocktime), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, E},
  899. { "segment_clocktime_offset", "set segment clocktime offset", OFFSET(clocktime_offset), AV_OPT_TYPE_DURATION, {.i64 = 0}, 0, 86400000000LL, E},
  900. { "segment_clocktime_wrap_duration", "set segment clocktime wrapping duration", OFFSET(clocktime_wrap_duration), AV_OPT_TYPE_DURATION, {.i64 = INT64_MAX}, 0, INT64_MAX, E},
  901. { "segment_time", "set segment duration", OFFSET(time_str),AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
  902. { "segment_time_delta","set approximation value used for the segment times", OFFSET(time_delta), AV_OPT_TYPE_DURATION, {.i64 = 0}, 0, 0, E },
  903. { "segment_times", "set segment split time points", OFFSET(times_str),AV_OPT_TYPE_STRING,{.str = NULL}, 0, 0, E },
  904. { "segment_frames", "set segment split frame numbers", OFFSET(frames_str),AV_OPT_TYPE_STRING,{.str = NULL}, 0, 0, E },
  905. { "segment_wrap", "set number after which the index wraps", OFFSET(segment_idx_wrap), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E },
  906. { "segment_list_entry_prefix", "set base url prefix for segments", OFFSET(entry_prefix), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
  907. { "segment_start_number", "set the sequence number of the first segment", OFFSET(segment_idx), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E },
  908. { "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 },
  909. { "strftime", "set filename expansion with strftime at segment creation", OFFSET(use_strftime), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, E },
  910. { "increment_tc", "increment timecode between each segment", OFFSET(increment_tc), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, E },
  911. { "break_non_keyframes", "allow breaking segments on non-keyframes", OFFSET(break_non_keyframes), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, E },
  912. { "individual_header_trailer", "write header/trailer to each segment", OFFSET(individual_header_trailer), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, E },
  913. { "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 },
  914. { "reset_timestamps", "reset timestamps at the begin of each segment", OFFSET(reset_timestamps), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, E },
  915. { "initial_offset", "set initial timestamp offset", OFFSET(initial_offset), AV_OPT_TYPE_DURATION, {.i64 = 0}, -INT64_MAX, INT64_MAX, E },
  916. { "write_empty_segments", "allow writing empty 'filler' segments", OFFSET(write_empty), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, E },
  917. { NULL },
  918. };
  919. static const AVClass seg_class = {
  920. .class_name = "segment muxer",
  921. .item_name = av_default_item_name,
  922. .option = options,
  923. .version = LIBAVUTIL_VERSION_INT,
  924. };
  925. AVOutputFormat ff_segment_muxer = {
  926. .name = "segment",
  927. .long_name = NULL_IF_CONFIG_SMALL("segment"),
  928. .priv_data_size = sizeof(SegmentContext),
  929. .flags = AVFMT_NOFILE|AVFMT_GLOBALHEADER,
  930. .init = seg_init,
  931. .write_header = seg_write_header,
  932. .write_packet = seg_write_packet,
  933. .write_trailer = seg_write_trailer,
  934. .deinit = seg_free,
  935. .check_bitstream = seg_check_bitstream,
  936. .priv_class = &seg_class,
  937. };
  938. static const AVClass sseg_class = {
  939. .class_name = "stream_segment muxer",
  940. .item_name = av_default_item_name,
  941. .option = options,
  942. .version = LIBAVUTIL_VERSION_INT,
  943. };
  944. AVOutputFormat ff_stream_segment_muxer = {
  945. .name = "stream_segment,ssegment",
  946. .long_name = NULL_IF_CONFIG_SMALL("streaming segment muxer"),
  947. .priv_data_size = sizeof(SegmentContext),
  948. .flags = AVFMT_NOFILE,
  949. .init = seg_init,
  950. .write_header = seg_write_header,
  951. .write_packet = seg_write_packet,
  952. .write_trailer = seg_write_trailer,
  953. .deinit = seg_free,
  954. .check_bitstream = seg_check_bitstream,
  955. .priv_class = &sseg_class,
  956. };