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.

1102 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 begin 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. if (seg->segment_idx_wrap)
  170. seg->segment_idx %= seg->segment_idx_wrap;
  171. if (seg->use_strftime) {
  172. time_t now0;
  173. struct tm *tm, tmpbuf;
  174. time(&now0);
  175. tm = localtime_r(&now0, &tmpbuf);
  176. if (!strftime(oc->filename, sizeof(oc->filename), s->filename, tm)) {
  177. av_log(oc, AV_LOG_ERROR, "Could not get segment filename with strftime\n");
  178. return AVERROR(EINVAL);
  179. }
  180. } else if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
  181. s->filename, seg->segment_idx) < 0) {
  182. av_log(oc, AV_LOG_ERROR, "Invalid segment filename template '%s'\n", s->filename);
  183. return AVERROR(EINVAL);
  184. }
  185. /* copy modified name in list entry */
  186. size = strlen(av_basename(oc->filename)) + 1;
  187. if (seg->entry_prefix)
  188. size += strlen(seg->entry_prefix);
  189. if ((ret = av_reallocp(&seg->cur_entry.filename, size)) < 0)
  190. return ret;
  191. snprintf(seg->cur_entry.filename, size, "%s%s",
  192. seg->entry_prefix ? seg->entry_prefix : "",
  193. av_basename(oc->filename));
  194. return 0;
  195. }
  196. static int segment_start(AVFormatContext *s, int write_header)
  197. {
  198. SegmentContext *seg = s->priv_data;
  199. AVFormatContext *oc = seg->avf;
  200. int err = 0;
  201. if (write_header) {
  202. avformat_free_context(oc);
  203. seg->avf = NULL;
  204. if ((err = segment_mux_init(s)) < 0)
  205. return err;
  206. oc = seg->avf;
  207. }
  208. seg->segment_idx++;
  209. if ((seg->segment_idx_wrap) && (seg->segment_idx % seg->segment_idx_wrap == 0))
  210. seg->segment_idx_wrap_nb++;
  211. if ((err = set_segment_filename(s)) < 0)
  212. return err;
  213. if ((err = s->io_open(s, &oc->pb, oc->filename, AVIO_FLAG_WRITE, NULL)) < 0) {
  214. av_log(s, AV_LOG_ERROR, "Failed to open segment '%s'\n", oc->filename);
  215. return err;
  216. }
  217. if (!seg->individual_header_trailer)
  218. oc->pb->seekable = 0;
  219. if (oc->oformat->priv_class && oc->priv_data)
  220. av_opt_set(oc->priv_data, "mpegts_flags", "+resend_headers", 0);
  221. if (write_header) {
  222. AVDictionary *options = NULL;
  223. av_dict_copy(&options, seg->format_options, 0);
  224. av_dict_set(&options, "fflags", "-autobsf", 0);
  225. err = avformat_write_header(oc, &options);
  226. av_dict_free(&options);
  227. if (err < 0)
  228. return err;
  229. }
  230. seg->segment_frame_count = 0;
  231. return 0;
  232. }
  233. static int segment_list_open(AVFormatContext *s)
  234. {
  235. SegmentContext *seg = s->priv_data;
  236. int ret;
  237. snprintf(seg->temp_list_filename, sizeof(seg->temp_list_filename), seg->use_rename ? "%s.tmp" : "%s", seg->list);
  238. ret = s->io_open(s, &seg->list_pb, seg->temp_list_filename, AVIO_FLAG_WRITE, NULL);
  239. if (ret < 0) {
  240. av_log(s, AV_LOG_ERROR, "Failed to open segment list '%s'\n", seg->list);
  241. return ret;
  242. }
  243. if (seg->list_type == LIST_TYPE_M3U8 && seg->segment_list_entries) {
  244. SegmentListEntry *entry;
  245. double max_duration = 0;
  246. avio_printf(seg->list_pb, "#EXTM3U\n");
  247. avio_printf(seg->list_pb, "#EXT-X-VERSION:3\n");
  248. avio_printf(seg->list_pb, "#EXT-X-MEDIA-SEQUENCE:%d\n", seg->segment_list_entries->index);
  249. avio_printf(seg->list_pb, "#EXT-X-ALLOW-CACHE:%s\n",
  250. seg->list_flags & SEGMENT_LIST_FLAG_CACHE ? "YES" : "NO");
  251. av_log(s, AV_LOG_VERBOSE, "EXT-X-MEDIA-SEQUENCE:%d\n",
  252. seg->segment_list_entries->index);
  253. for (entry = seg->segment_list_entries; entry; entry = entry->next)
  254. max_duration = FFMAX(max_duration, entry->end_time - entry->start_time);
  255. avio_printf(seg->list_pb, "#EXT-X-TARGETDURATION:%"PRId64"\n", (int64_t)ceil(max_duration));
  256. } else if (seg->list_type == LIST_TYPE_FFCONCAT) {
  257. avio_printf(seg->list_pb, "ffconcat version 1.0\n");
  258. }
  259. return ret;
  260. }
  261. static void segment_list_print_entry(AVIOContext *list_ioctx,
  262. ListType list_type,
  263. const SegmentListEntry *list_entry,
  264. void *log_ctx)
  265. {
  266. switch (list_type) {
  267. case LIST_TYPE_FLAT:
  268. avio_printf(list_ioctx, "%s\n", list_entry->filename);
  269. break;
  270. case LIST_TYPE_CSV:
  271. case LIST_TYPE_EXT:
  272. print_csv_escaped_str(list_ioctx, list_entry->filename);
  273. avio_printf(list_ioctx, ",%f,%f\n", list_entry->start_time, list_entry->end_time);
  274. break;
  275. case LIST_TYPE_M3U8:
  276. avio_printf(list_ioctx, "#EXTINF:%f,\n%s\n",
  277. list_entry->end_time - list_entry->start_time, list_entry->filename);
  278. break;
  279. case LIST_TYPE_FFCONCAT:
  280. {
  281. char *buf;
  282. if (av_escape(&buf, list_entry->filename, NULL, AV_ESCAPE_MODE_AUTO, AV_ESCAPE_FLAG_WHITESPACE) < 0) {
  283. av_log(log_ctx, AV_LOG_WARNING,
  284. "Error writing list entry '%s' in list file\n", list_entry->filename);
  285. return;
  286. }
  287. avio_printf(list_ioctx, "file %s\n", buf);
  288. av_free(buf);
  289. break;
  290. }
  291. default:
  292. av_assert0(!"Invalid list type");
  293. }
  294. }
  295. static int segment_end(AVFormatContext *s, int write_trailer, int is_last)
  296. {
  297. SegmentContext *seg = s->priv_data;
  298. AVFormatContext *oc = seg->avf;
  299. int ret = 0;
  300. AVTimecode tc;
  301. AVRational rate;
  302. AVDictionaryEntry *tcr;
  303. char buf[AV_TIMECODE_STR_SIZE];
  304. int i;
  305. int err;
  306. if (!oc || !oc->pb)
  307. return AVERROR(EINVAL);
  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. avio_context_free(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 || !seg->avf->pb)
  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 (wrapped_val < seg->last_val && wrapped_val < seg->clocktime_wrap_duration)
  755. seg->cut_pending = 1;
  756. seg->last_val = wrapped_val;
  757. } else {
  758. end_pts = seg->time * (seg->segment_count + 1);
  759. }
  760. }
  761. ff_dlog(s, "packet stream:%d pts:%s pts_time:%s duration_time:%s is_key:%d frame:%d\n",
  762. pkt->stream_index, av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &st->time_base),
  763. av_ts2timestr(pkt->duration, &st->time_base),
  764. pkt->flags & AV_PKT_FLAG_KEY,
  765. pkt->stream_index == seg->reference_stream_index ? seg->frame_count : -1);
  766. if (pkt->stream_index == seg->reference_stream_index &&
  767. (pkt->flags & AV_PKT_FLAG_KEY || seg->break_non_keyframes) &&
  768. (seg->segment_frame_count > 0 || seg->write_empty) &&
  769. (seg->cut_pending || seg->frame_count >= start_frame ||
  770. (pkt->pts != AV_NOPTS_VALUE &&
  771. av_compare_ts(pkt->pts, st->time_base,
  772. end_pts - seg->time_delta, AV_TIME_BASE_Q) >= 0))) {
  773. /* sanitize end time in case last packet didn't have a defined duration */
  774. if (seg->cur_entry.last_duration == 0)
  775. seg->cur_entry.end_time = (double)pkt->pts * av_q2d(st->time_base);
  776. if ((ret = segment_end(s, seg->individual_header_trailer, 0)) < 0)
  777. goto fail;
  778. if ((ret = segment_start(s, seg->individual_header_trailer)) < 0)
  779. goto fail;
  780. seg->cut_pending = 0;
  781. seg->cur_entry.index = seg->segment_idx + seg->segment_idx_wrap * seg->segment_idx_wrap_nb;
  782. seg->cur_entry.start_time = (double)pkt->pts * av_q2d(st->time_base);
  783. seg->cur_entry.start_pts = av_rescale_q(pkt->pts, st->time_base, AV_TIME_BASE_Q);
  784. seg->cur_entry.end_time = seg->cur_entry.start_time;
  785. if (seg->times || (!seg->frames && !seg->use_clocktime) && seg->write_empty)
  786. goto calc_times;
  787. }
  788. if (pkt->stream_index == seg->reference_stream_index) {
  789. if (pkt->pts != AV_NOPTS_VALUE)
  790. seg->cur_entry.end_time =
  791. FFMAX(seg->cur_entry.end_time, (double)(pkt->pts + pkt->duration) * av_q2d(st->time_base));
  792. seg->cur_entry.last_duration = pkt->duration;
  793. }
  794. if (seg->segment_frame_count == 0) {
  795. av_log(s, AV_LOG_VERBOSE, "segment:'%s' starts with packet stream:%d pts:%s pts_time:%s frame:%d\n",
  796. seg->avf->filename, pkt->stream_index,
  797. av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &st->time_base), seg->frame_count);
  798. }
  799. av_log(s, AV_LOG_DEBUG, "stream:%d start_pts_time:%s pts:%s pts_time:%s dts:%s dts_time:%s",
  800. pkt->stream_index,
  801. av_ts2timestr(seg->cur_entry.start_pts, &AV_TIME_BASE_Q),
  802. av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &st->time_base),
  803. av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &st->time_base));
  804. /* compute new timestamps */
  805. offset = av_rescale_q(seg->initial_offset - (seg->reset_timestamps ? seg->cur_entry.start_pts : 0),
  806. AV_TIME_BASE_Q, st->time_base);
  807. if (pkt->pts != AV_NOPTS_VALUE)
  808. pkt->pts += offset;
  809. if (pkt->dts != AV_NOPTS_VALUE)
  810. pkt->dts += offset;
  811. av_log(s, AV_LOG_DEBUG, " -> pts:%s pts_time:%s dts:%s dts_time:%s\n",
  812. av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &st->time_base),
  813. av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &st->time_base));
  814. ret = ff_write_chained(seg->avf, pkt->stream_index, pkt, s, seg->initial_offset || seg->reset_timestamps);
  815. fail:
  816. if (pkt->stream_index == seg->reference_stream_index) {
  817. seg->frame_count++;
  818. seg->segment_frame_count++;
  819. }
  820. return ret;
  821. }
  822. static int seg_write_trailer(struct AVFormatContext *s)
  823. {
  824. SegmentContext *seg = s->priv_data;
  825. AVFormatContext *oc = seg->avf;
  826. SegmentListEntry *cur, *next;
  827. int ret = 0;
  828. if (!oc)
  829. goto fail;
  830. if (!seg->write_header_trailer) {
  831. if ((ret = segment_end(s, 0, 1)) < 0)
  832. goto fail;
  833. if ((ret = open_null_ctx(&oc->pb)) < 0)
  834. goto fail;
  835. ret = av_write_trailer(oc);
  836. close_null_ctxp(&oc->pb);
  837. } else {
  838. ret = segment_end(s, 1, 1);
  839. }
  840. fail:
  841. if (seg->list)
  842. ff_format_io_close(s, &seg->list_pb);
  843. av_dict_free(&seg->format_options);
  844. av_opt_free(seg);
  845. av_freep(&seg->times);
  846. av_freep(&seg->frames);
  847. av_freep(&seg->cur_entry.filename);
  848. cur = seg->segment_list_entries;
  849. while (cur) {
  850. next = cur->next;
  851. av_freep(&cur->filename);
  852. av_free(cur);
  853. cur = next;
  854. }
  855. avformat_free_context(oc);
  856. seg->avf = NULL;
  857. return ret;
  858. }
  859. static int seg_check_bitstream(struct AVFormatContext *s, const AVPacket *pkt)
  860. {
  861. SegmentContext *seg = s->priv_data;
  862. AVFormatContext *oc = seg->avf;
  863. if (oc->oformat->check_bitstream) {
  864. int ret = oc->oformat->check_bitstream(oc, pkt);
  865. if (ret == 1) {
  866. AVStream *st = s->streams[pkt->stream_index];
  867. AVStream *ost = oc->streams[pkt->stream_index];
  868. st->internal->bsfcs = ost->internal->bsfcs;
  869. st->internal->nb_bsfcs = ost->internal->nb_bsfcs;
  870. ost->internal->bsfcs = NULL;
  871. ost->internal->nb_bsfcs = 0;
  872. }
  873. return ret;
  874. }
  875. return 1;
  876. }
  877. #define OFFSET(x) offsetof(SegmentContext, x)
  878. #define E AV_OPT_FLAG_ENCODING_PARAM
  879. static const AVOption options[] = {
  880. { "reference_stream", "set reference stream", OFFSET(reference_stream_specifier), AV_OPT_TYPE_STRING, {.str = "auto"}, CHAR_MIN, CHAR_MAX, E },
  881. { "segment_format", "set container format used for the segments", OFFSET(format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
  882. { "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 },
  883. { "segment_list", "set the segment list filename", OFFSET(list), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
  884. { "segment_header_filename", "write a single file containing the header", OFFSET(header_filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
  885. { "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"},
  886. { "cache", "allow list caching", 0, AV_OPT_TYPE_CONST, {.i64 = SEGMENT_LIST_FLAG_CACHE }, INT_MIN, INT_MAX, E, "list_flags"},
  887. { "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"},
  888. { "segment_list_size", "set the maximum number of playlist entries", OFFSET(list_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E },
  889. { "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" },
  890. { "flat", "flat format", 0, AV_OPT_TYPE_CONST, {.i64=LIST_TYPE_FLAT }, INT_MIN, INT_MAX, E, "list_type" },
  891. { "csv", "csv format", 0, AV_OPT_TYPE_CONST, {.i64=LIST_TYPE_CSV }, INT_MIN, INT_MAX, E, "list_type" },
  892. { "ext", "extended format", 0, AV_OPT_TYPE_CONST, {.i64=LIST_TYPE_EXT }, INT_MIN, INT_MAX, E, "list_type" },
  893. { "ffconcat", "ffconcat format", 0, AV_OPT_TYPE_CONST, {.i64=LIST_TYPE_FFCONCAT }, INT_MIN, INT_MAX, E, "list_type" },
  894. { "m3u8", "M3U8 format", 0, AV_OPT_TYPE_CONST, {.i64=LIST_TYPE_M3U8 }, INT_MIN, INT_MAX, E, "list_type" },
  895. { "hls", "Apple HTTP Live Streaming compatible", 0, AV_OPT_TYPE_CONST, {.i64=LIST_TYPE_M3U8 }, INT_MIN, INT_MAX, E, "list_type" },
  896. { "segment_atclocktime", "set segment to be cut at clocktime", OFFSET(use_clocktime), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, E},
  897. { "segment_clocktime_offset", "set segment clocktime offset", OFFSET(clocktime_offset), AV_OPT_TYPE_DURATION, {.i64 = 0}, 0, 86400000000LL, E},
  898. { "segment_clocktime_wrap_duration", "set segment clocktime wrapping duration", OFFSET(clocktime_wrap_duration), AV_OPT_TYPE_DURATION, {.i64 = INT64_MAX}, 0, INT64_MAX, E},
  899. { "segment_time", "set segment duration", OFFSET(time_str),AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
  900. { "segment_time_delta","set approximation value used for the segment times", OFFSET(time_delta), AV_OPT_TYPE_DURATION, {.i64 = 0}, 0, 0, E },
  901. { "segment_times", "set segment split time points", OFFSET(times_str),AV_OPT_TYPE_STRING,{.str = NULL}, 0, 0, E },
  902. { "segment_frames", "set segment split frame numbers", OFFSET(frames_str),AV_OPT_TYPE_STRING,{.str = NULL}, 0, 0, E },
  903. { "segment_wrap", "set number after which the index wraps", OFFSET(segment_idx_wrap), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E },
  904. { "segment_list_entry_prefix", "set base url prefix for segments", OFFSET(entry_prefix), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
  905. { "segment_start_number", "set the sequence number of the first segment", OFFSET(segment_idx), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E },
  906. { "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 },
  907. { "strftime", "set filename expansion with strftime at segment creation", OFFSET(use_strftime), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, E },
  908. { "increment_tc", "increment timecode between each segment", OFFSET(increment_tc), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, E },
  909. { "break_non_keyframes", "allow breaking segments on non-keyframes", OFFSET(break_non_keyframes), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, E },
  910. { "individual_header_trailer", "write header/trailer to each segment", OFFSET(individual_header_trailer), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, E },
  911. { "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 },
  912. { "reset_timestamps", "reset timestamps at the begin of each segment", OFFSET(reset_timestamps), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, E },
  913. { "initial_offset", "set initial timestamp offset", OFFSET(initial_offset), AV_OPT_TYPE_DURATION, {.i64 = 0}, -INT64_MAX, INT64_MAX, E },
  914. { "write_empty_segments", "allow writing empty 'filler' segments", OFFSET(write_empty), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, E },
  915. { NULL },
  916. };
  917. static const AVClass seg_class = {
  918. .class_name = "segment muxer",
  919. .item_name = av_default_item_name,
  920. .option = options,
  921. .version = LIBAVUTIL_VERSION_INT,
  922. };
  923. AVOutputFormat ff_segment_muxer = {
  924. .name = "segment",
  925. .long_name = NULL_IF_CONFIG_SMALL("segment"),
  926. .priv_data_size = sizeof(SegmentContext),
  927. .flags = AVFMT_NOFILE|AVFMT_GLOBALHEADER,
  928. .init = seg_init,
  929. .write_header = seg_write_header,
  930. .write_packet = seg_write_packet,
  931. .write_trailer = seg_write_trailer,
  932. .deinit = seg_free,
  933. .check_bitstream = seg_check_bitstream,
  934. .priv_class = &seg_class,
  935. };
  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. };