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.

410 lines
12KB

  1. /*
  2. * Generic segmenter
  3. * Copyright (c) 2011, Luca Barbato
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <float.h>
  22. #include "avformat.h"
  23. #include "internal.h"
  24. #include "libavutil/log.h"
  25. #include "libavutil/opt.h"
  26. #include "libavutil/avstring.h"
  27. #include "libavutil/parseutils.h"
  28. #include "libavutil/mathematics.h"
  29. typedef struct SegmentContext {
  30. const AVClass *class; /**< Class for private options. */
  31. int number;
  32. AVOutputFormat *oformat;
  33. AVFormatContext *avf;
  34. char *format; /**< Set by a private option. */
  35. char *list; /**< Set by a private option. */
  36. char *entry_prefix; /**< Set by a private option. */
  37. int list_type; /**< Set by a private option. */
  38. float time; /**< Set by a private option. */
  39. int size; /**< Set by a private option. */
  40. int wrap; /**< Set by a private option. */
  41. int individual_header_trailer; /**< Set by a private option. */
  42. int write_header_trailer; /**< Set by a private option. */
  43. int64_t offset_time;
  44. int64_t recording_time;
  45. int has_video;
  46. AVIOContext *pb;
  47. } SegmentContext;
  48. enum {
  49. LIST_FLAT,
  50. LIST_HLS
  51. };
  52. static int segment_mux_init(AVFormatContext *s)
  53. {
  54. SegmentContext *seg = s->priv_data;
  55. AVFormatContext *oc;
  56. int i;
  57. seg->avf = oc = avformat_alloc_context();
  58. if (!oc)
  59. return AVERROR(ENOMEM);
  60. oc->oformat = seg->oformat;
  61. oc->interrupt_callback = s->interrupt_callback;
  62. oc->opaque = s->opaque;
  63. oc->io_close = s->io_close;
  64. oc->io_open = s->io_open;
  65. for (i = 0; i < s->nb_streams; i++) {
  66. AVStream *st;
  67. if (!(st = avformat_new_stream(oc, NULL)))
  68. return AVERROR(ENOMEM);
  69. avcodec_parameters_copy(st->codecpar, s->streams[i]->codecpar);
  70. st->sample_aspect_ratio = s->streams[i]->sample_aspect_ratio;
  71. st->time_base = s->streams[i]->time_base;
  72. }
  73. return 0;
  74. }
  75. static int segment_hls_window(AVFormatContext *s, int last)
  76. {
  77. SegmentContext *seg = s->priv_data;
  78. int i, ret = 0;
  79. char buf[1024];
  80. if ((ret = s->io_open(s, &seg->pb, seg->list, AVIO_FLAG_WRITE, NULL)) < 0)
  81. goto fail;
  82. avio_printf(seg->pb, "#EXTM3U\n");
  83. avio_printf(seg->pb, "#EXT-X-VERSION:3\n");
  84. avio_printf(seg->pb, "#EXT-X-TARGETDURATION:%d\n", (int)seg->time);
  85. avio_printf(seg->pb, "#EXT-X-MEDIA-SEQUENCE:%d\n",
  86. FFMAX(0, seg->number - seg->size));
  87. av_log(s, AV_LOG_VERBOSE, "EXT-X-MEDIA-SEQUENCE:%d\n",
  88. FFMAX(0, seg->number - seg->size));
  89. for (i = FFMAX(0, seg->number - seg->size);
  90. i < seg->number; i++) {
  91. avio_printf(seg->pb, "#EXTINF:%d,\n", (int)seg->time);
  92. if (seg->entry_prefix) {
  93. avio_printf(seg->pb, "%s", seg->entry_prefix);
  94. }
  95. ret = av_get_frame_filename(buf, sizeof(buf), s->filename, i);
  96. if (ret < 0) {
  97. ret = AVERROR(EINVAL);
  98. goto fail;
  99. }
  100. avio_printf(seg->pb, "%s\n", buf);
  101. }
  102. if (last)
  103. avio_printf(seg->pb, "#EXT-X-ENDLIST\n");
  104. fail:
  105. ff_format_io_close(s, &seg->pb);
  106. return ret;
  107. }
  108. static int segment_start(AVFormatContext *s, int write_header)
  109. {
  110. SegmentContext *c = s->priv_data;
  111. AVFormatContext *oc = c->avf;
  112. int err = 0;
  113. if (write_header) {
  114. avformat_free_context(oc);
  115. c->avf = NULL;
  116. if ((err = segment_mux_init(s)) < 0)
  117. return err;
  118. oc = c->avf;
  119. }
  120. if (c->wrap)
  121. c->number %= c->wrap;
  122. if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
  123. s->filename, c->number++) < 0)
  124. return AVERROR(EINVAL);
  125. if ((err = s->io_open(s, &oc->pb, oc->filename, AVIO_FLAG_WRITE, NULL)) < 0)
  126. return err;
  127. if (oc->oformat->priv_class && oc->priv_data)
  128. av_opt_set(oc->priv_data, "resend_headers", "1", 0); /* mpegts specific */
  129. if (write_header) {
  130. if ((err = avformat_write_header(oc, NULL)) < 0)
  131. return err;
  132. }
  133. return 0;
  134. }
  135. static int segment_end(AVFormatContext *oc, int write_trailer)
  136. {
  137. int ret = 0;
  138. av_write_frame(oc, NULL); /* Flush any buffered data (fragmented mp4) */
  139. if (write_trailer)
  140. av_write_trailer(oc);
  141. ff_format_io_close(oc, &oc->pb);
  142. return ret;
  143. }
  144. static int open_null_ctx(AVIOContext **ctx)
  145. {
  146. int buf_size = 32768;
  147. uint8_t *buf = av_malloc(buf_size);
  148. if (!buf)
  149. return AVERROR(ENOMEM);
  150. *ctx = avio_alloc_context(buf, buf_size, AVIO_FLAG_WRITE, NULL, NULL, NULL, NULL);
  151. if (!*ctx) {
  152. av_free(buf);
  153. return AVERROR(ENOMEM);
  154. }
  155. return 0;
  156. }
  157. static void close_null_ctx(AVIOContext **pb)
  158. {
  159. av_free((*pb)->buffer);
  160. avio_context_free(pb);
  161. }
  162. static void seg_free_context(SegmentContext *seg)
  163. {
  164. ff_format_io_close(seg->avf, &seg->pb);
  165. avformat_free_context(seg->avf);
  166. seg->avf = NULL;
  167. }
  168. static int seg_write_header(AVFormatContext *s)
  169. {
  170. SegmentContext *seg = s->priv_data;
  171. AVFormatContext *oc = NULL;
  172. int ret, i;
  173. seg->number = 0;
  174. seg->offset_time = 0;
  175. seg->recording_time = seg->time * 1000000;
  176. if (!seg->write_header_trailer)
  177. seg->individual_header_trailer = 0;
  178. if (seg->list && seg->list_type != LIST_HLS)
  179. if ((ret = s->io_open(s, &seg->pb, seg->list, AVIO_FLAG_WRITE, NULL)) < 0)
  180. goto fail;
  181. for (i = 0; i < s->nb_streams; i++)
  182. seg->has_video +=
  183. (s->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO);
  184. if (seg->has_video > 1)
  185. av_log(s, AV_LOG_WARNING,
  186. "More than a single video stream present, "
  187. "expect issues decoding it.\n");
  188. seg->oformat = av_guess_format(seg->format, s->filename, NULL);
  189. if (!seg->oformat) {
  190. ret = AVERROR_MUXER_NOT_FOUND;
  191. goto fail;
  192. }
  193. if (seg->oformat->flags & AVFMT_NOFILE) {
  194. av_log(s, AV_LOG_ERROR, "format %s not supported.\n",
  195. seg->oformat->name);
  196. ret = AVERROR(EINVAL);
  197. goto fail;
  198. }
  199. if ((ret = segment_mux_init(s)) < 0)
  200. goto fail;
  201. oc = seg->avf;
  202. if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
  203. s->filename, seg->number++) < 0) {
  204. ret = AVERROR(EINVAL);
  205. goto fail;
  206. }
  207. if (seg->write_header_trailer) {
  208. if ((ret = s->io_open(s, &oc->pb, oc->filename, AVIO_FLAG_WRITE, NULL)) < 0)
  209. goto fail;
  210. } else {
  211. if ((ret = open_null_ctx(&oc->pb)) < 0)
  212. goto fail;
  213. }
  214. if ((ret = avformat_write_header(oc, NULL)) < 0) {
  215. ff_format_io_close(oc, &oc->pb);
  216. goto fail;
  217. }
  218. if (!seg->write_header_trailer) {
  219. close_null_ctx(&oc->pb);
  220. if ((ret = s->io_open(s, &oc->pb, oc->filename, AVIO_FLAG_WRITE, NULL)) < 0)
  221. goto fail;
  222. }
  223. if (seg->list) {
  224. if (seg->list_type == LIST_HLS) {
  225. if ((ret = segment_hls_window(s, 0)) < 0)
  226. goto fail;
  227. } else {
  228. avio_printf(seg->pb, "%s\n", oc->filename);
  229. avio_flush(seg->pb);
  230. }
  231. }
  232. fail:
  233. if (ret < 0)
  234. seg_free_context(seg);
  235. return ret;
  236. }
  237. static int seg_write_packet(AVFormatContext *s, AVPacket *pkt)
  238. {
  239. SegmentContext *seg = s->priv_data;
  240. AVFormatContext *oc = seg->avf;
  241. AVStream *st = s->streams[pkt->stream_index];
  242. int64_t end_pts = seg->recording_time * seg->number;
  243. int ret, can_split = 1;
  244. if (!oc)
  245. return AVERROR(EINVAL);
  246. if (seg->has_video) {
  247. can_split = st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
  248. pkt->flags & AV_PKT_FLAG_KEY;
  249. }
  250. if (can_split && av_compare_ts(pkt->pts, st->time_base, end_pts,
  251. AV_TIME_BASE_Q) >= 0) {
  252. av_log(s, AV_LOG_DEBUG, "Next segment starts at %d %"PRId64"\n",
  253. pkt->stream_index, pkt->pts);
  254. ret = segment_end(oc, seg->individual_header_trailer);
  255. if (!ret)
  256. ret = segment_start(s, seg->individual_header_trailer);
  257. if (ret)
  258. goto fail;
  259. oc = seg->avf;
  260. if (seg->list) {
  261. if (seg->list_type == LIST_HLS) {
  262. if ((ret = segment_hls_window(s, 0)) < 0)
  263. goto fail;
  264. } else {
  265. avio_printf(seg->pb, "%s\n", oc->filename);
  266. avio_flush(seg->pb);
  267. if (seg->size && !(seg->number % seg->size)) {
  268. ff_format_io_close(s, &seg->pb);
  269. if ((ret = s->io_open(s, &seg->pb, seg->list,
  270. AVIO_FLAG_WRITE, NULL)) < 0)
  271. goto fail;
  272. }
  273. }
  274. }
  275. }
  276. ret = ff_write_chained(oc, pkt->stream_index, pkt, s);
  277. fail:
  278. if (ret < 0)
  279. seg_free_context(seg);
  280. return ret;
  281. }
  282. static int seg_write_trailer(struct AVFormatContext *s)
  283. {
  284. SegmentContext *seg = s->priv_data;
  285. AVFormatContext *oc = seg->avf;
  286. int ret = 0;
  287. if (!oc)
  288. goto fail;
  289. if (!seg->write_header_trailer) {
  290. if ((ret = segment_end(oc, 0)) < 0)
  291. goto fail;
  292. if ((ret = open_null_ctx(&oc->pb)) < 0)
  293. goto fail;
  294. ret = av_write_trailer(oc);
  295. close_null_ctx(&oc->pb);
  296. } else {
  297. ret = segment_end(oc, 1);
  298. }
  299. if (ret < 0)
  300. goto fail;
  301. if (seg->list && seg->list_type == LIST_HLS) {
  302. if ((ret = segment_hls_window(s, 1) < 0))
  303. goto fail;
  304. }
  305. fail:
  306. ff_format_io_close(s, &seg->pb);
  307. avformat_free_context(oc);
  308. return ret;
  309. }
  310. #define OFFSET(x) offsetof(SegmentContext, x)
  311. #define E AV_OPT_FLAG_ENCODING_PARAM
  312. static const AVOption options[] = {
  313. { "segment_format", "container format used for the segments", OFFSET(format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
  314. { "segment_time", "segment length in seconds", OFFSET(time), AV_OPT_TYPE_FLOAT, {.dbl = 2}, 0, FLT_MAX, E },
  315. { "segment_list", "output the segment list", OFFSET(list), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
  316. { "segment_list_size", "maximum number of playlist entries", OFFSET(size), AV_OPT_TYPE_INT, {.i64 = 5}, 0, INT_MAX, E },
  317. { "segment_list_type", "segment list format", OFFSET(list_type), AV_OPT_TYPE_INT, {.i64 = LIST_FLAT}, 0, 2, E, "list_type" },
  318. { "flat", "plain list (default)", 0, AV_OPT_TYPE_CONST, {.i64 = LIST_FLAT}, 0, 0, E, "list_type" },
  319. { "hls", "Apple HTTP Live Streaming compatible", 0, AV_OPT_TYPE_CONST, {.i64 = LIST_HLS}, 0, 0, E, "list_type" },
  320. { "segment_wrap", "number after which the index wraps", OFFSET(wrap), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E },
  321. { "segment_list_entry_prefix", "base url prefix for segments", OFFSET(entry_prefix), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
  322. { "individual_header_trailer", "write header/trailer to each segment", OFFSET(individual_header_trailer), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, E },
  323. { "write_header_trailer", "write a header to the first segment and a trailer to the last one", OFFSET(write_header_trailer), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, E },
  324. { NULL },
  325. };
  326. static const AVClass seg_class = {
  327. .class_name = "segment muxer",
  328. .item_name = av_default_item_name,
  329. .option = options,
  330. .version = LIBAVUTIL_VERSION_INT,
  331. };
  332. AVOutputFormat ff_segment_muxer = {
  333. .name = "segment",
  334. .long_name = NULL_IF_CONFIG_SMALL("segment"),
  335. .priv_data_size = sizeof(SegmentContext),
  336. .flags = AVFMT_NOFILE,
  337. .write_header = seg_write_header,
  338. .write_packet = seg_write_packet,
  339. .write_trailer = seg_write_trailer,
  340. .priv_class = &seg_class,
  341. };