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.

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