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.

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