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.

328 lines
9.7KB

  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 {
  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. float time; /**< Set by a private option. */
  37. int size; /**< Set by a private option. */
  38. int wrap; /**< Set by a private option. */
  39. int individual_header_trailer; /**< Set by a private option. */
  40. int write_header_trailer; /**< Set by a private option. */
  41. int64_t offset_time;
  42. int64_t recording_time;
  43. int has_video;
  44. AVIOContext *pb;
  45. } SegmentContext;
  46. static int segment_mux_init(AVFormatContext *s)
  47. {
  48. SegmentContext *seg = s->priv_data;
  49. AVFormatContext *oc;
  50. int i;
  51. seg->avf = oc = avformat_alloc_context();
  52. if (!oc)
  53. return AVERROR(ENOMEM);
  54. oc->oformat = seg->oformat;
  55. oc->interrupt_callback = s->interrupt_callback;
  56. for (i = 0; i < s->nb_streams; i++) {
  57. AVStream *st;
  58. if (!(st = avformat_new_stream(oc, NULL)))
  59. return AVERROR(ENOMEM);
  60. avcodec_copy_context(st->codec, s->streams[i]->codec);
  61. st->sample_aspect_ratio = s->streams[i]->sample_aspect_ratio;
  62. }
  63. return 0;
  64. }
  65. static int segment_start(AVFormatContext *s, int write_header)
  66. {
  67. SegmentContext *c = s->priv_data;
  68. AVFormatContext *oc = c->avf;
  69. int err = 0;
  70. if (write_header) {
  71. avformat_free_context(oc);
  72. c->avf = NULL;
  73. if ((err = segment_mux_init(s)) < 0)
  74. return err;
  75. oc = c->avf;
  76. }
  77. if (c->wrap)
  78. c->number %= c->wrap;
  79. if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
  80. s->filename, c->number++) < 0)
  81. return AVERROR(EINVAL);
  82. if ((err = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
  83. &s->interrupt_callback, NULL)) < 0)
  84. return err;
  85. if (oc->oformat->priv_class && oc->priv_data)
  86. av_opt_set(oc->priv_data, "resend_headers", "1", 0); /* mpegts specific */
  87. if (write_header) {
  88. if ((err = avformat_write_header(oc, NULL)) < 0)
  89. return err;
  90. }
  91. return 0;
  92. }
  93. static int segment_end(AVFormatContext *oc, int write_trailer)
  94. {
  95. int ret = 0;
  96. av_write_frame(oc, NULL); /* Flush any buffered data (fragmented mp4) */
  97. if (write_trailer)
  98. av_write_trailer(oc);
  99. avio_close(oc->pb);
  100. return ret;
  101. }
  102. static int open_null_ctx(AVIOContext **ctx)
  103. {
  104. int buf_size = 32768;
  105. uint8_t *buf = av_malloc(buf_size);
  106. if (!buf)
  107. return AVERROR(ENOMEM);
  108. *ctx = avio_alloc_context(buf, buf_size, AVIO_FLAG_WRITE, NULL, NULL, NULL, NULL);
  109. if (!*ctx) {
  110. av_free(buf);
  111. return AVERROR(ENOMEM);
  112. }
  113. return 0;
  114. }
  115. static void close_null_ctx(AVIOContext *pb)
  116. {
  117. av_free(pb->buffer);
  118. av_free(pb);
  119. }
  120. static int seg_write_header(AVFormatContext *s)
  121. {
  122. SegmentContext *seg = s->priv_data;
  123. AVFormatContext *oc = NULL;
  124. int ret, i;
  125. seg->number = 0;
  126. seg->offset_time = 0;
  127. seg->recording_time = seg->time * 1000000;
  128. if (!seg->write_header_trailer)
  129. seg->individual_header_trailer = 0;
  130. if (seg->list)
  131. if ((ret = avio_open2(&seg->pb, seg->list, AVIO_FLAG_WRITE,
  132. &s->interrupt_callback, NULL)) < 0)
  133. goto fail;
  134. for (i = 0; i < s->nb_streams; i++)
  135. seg->has_video +=
  136. (s->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO);
  137. if (seg->has_video > 1)
  138. av_log(s, AV_LOG_WARNING,
  139. "More than a single video stream present, "
  140. "expect issues decoding it.\n");
  141. seg->oformat = av_guess_format(seg->format, s->filename, NULL);
  142. if (!seg->oformat) {
  143. ret = AVERROR_MUXER_NOT_FOUND;
  144. goto fail;
  145. }
  146. if (seg->oformat->flags & AVFMT_NOFILE) {
  147. av_log(s, AV_LOG_ERROR, "format %s not supported.\n",
  148. oc->oformat->name);
  149. ret = AVERROR(EINVAL);
  150. goto fail;
  151. }
  152. if ((ret = segment_mux_init(s)) < 0)
  153. goto fail;
  154. oc = seg->avf;
  155. if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
  156. s->filename, seg->number++) < 0) {
  157. ret = AVERROR(EINVAL);
  158. goto fail;
  159. }
  160. if (seg->write_header_trailer) {
  161. if ((ret = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
  162. &s->interrupt_callback, NULL)) < 0)
  163. goto fail;
  164. } else {
  165. if ((ret = open_null_ctx(&oc->pb)) < 0)
  166. goto fail;
  167. }
  168. if ((ret = avformat_write_header(oc, NULL)) < 0) {
  169. avio_close(oc->pb);
  170. goto fail;
  171. }
  172. if (!seg->write_header_trailer) {
  173. close_null_ctx(oc->pb);
  174. if ((ret = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
  175. &s->interrupt_callback, NULL)) < 0)
  176. goto fail;
  177. }
  178. if (seg->list) {
  179. avio_printf(seg->pb, "%s\n", oc->filename);
  180. avio_flush(seg->pb);
  181. }
  182. fail:
  183. if (ret) {
  184. if (seg->list)
  185. avio_close(seg->pb);
  186. if (seg->avf)
  187. avformat_free_context(seg->avf);
  188. }
  189. return ret;
  190. }
  191. static int seg_write_packet(AVFormatContext *s, AVPacket *pkt)
  192. {
  193. SegmentContext *seg = s->priv_data;
  194. AVFormatContext *oc = seg->avf;
  195. AVStream *st = s->streams[pkt->stream_index];
  196. int64_t end_pts = seg->recording_time * seg->number;
  197. int ret;
  198. if ((seg->has_video && st->codec->codec_type == AVMEDIA_TYPE_VIDEO) &&
  199. av_compare_ts(pkt->pts, st->time_base,
  200. end_pts, AV_TIME_BASE_Q) >= 0 &&
  201. pkt->flags & AV_PKT_FLAG_KEY) {
  202. av_log(s, AV_LOG_DEBUG, "Next segment starts at %d %"PRId64"\n",
  203. pkt->stream_index, pkt->pts);
  204. ret = segment_end(oc, seg->individual_header_trailer);
  205. if (!ret)
  206. ret = segment_start(s, seg->individual_header_trailer);
  207. if (ret)
  208. goto fail;
  209. oc = seg->avf;
  210. if (seg->list) {
  211. avio_printf(seg->pb, "%s\n", oc->filename);
  212. avio_flush(seg->pb);
  213. if (seg->size && !(seg->number % seg->size)) {
  214. avio_close(seg->pb);
  215. if ((ret = avio_open2(&seg->pb, seg->list, AVIO_FLAG_WRITE,
  216. &s->interrupt_callback, NULL)) < 0)
  217. goto fail;
  218. }
  219. }
  220. }
  221. ret = ff_write_chained(oc, pkt->stream_index, pkt, s);
  222. fail:
  223. if (ret < 0) {
  224. if (seg->list)
  225. avio_close(seg->pb);
  226. avformat_free_context(oc);
  227. }
  228. return ret;
  229. }
  230. static int seg_write_trailer(struct AVFormatContext *s)
  231. {
  232. SegmentContext *seg = s->priv_data;
  233. AVFormatContext *oc = seg->avf;
  234. int ret;
  235. if (!seg->write_header_trailer) {
  236. ret = segment_end(oc, 0);
  237. open_null_ctx(&oc->pb);
  238. av_write_trailer(oc);
  239. close_null_ctx(oc->pb);
  240. } else {
  241. ret = segment_end(oc, 1);
  242. }
  243. if (seg->list)
  244. avio_close(seg->pb);
  245. avformat_free_context(oc);
  246. return ret;
  247. }
  248. #define OFFSET(x) offsetof(SegmentContext, x)
  249. #define E AV_OPT_FLAG_ENCODING_PARAM
  250. static const AVOption options[] = {
  251. { "segment_format", "container format used for the segments", OFFSET(format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
  252. { "segment_time", "segment length in seconds", OFFSET(time), AV_OPT_TYPE_FLOAT, {.dbl = 2}, 0, FLT_MAX, E },
  253. { "segment_list", "output the segment list", OFFSET(list), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
  254. { "segment_list_size", "maximum number of playlist entries", OFFSET(size), AV_OPT_TYPE_INT, {.i64 = 5}, 0, INT_MAX, E },
  255. { "segment_wrap", "number after which the index wraps", OFFSET(wrap), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E },
  256. { "individual_header_trailer", "write header/trailer to each segment", OFFSET(individual_header_trailer), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, E },
  257. { "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 },
  258. { NULL },
  259. };
  260. static const AVClass seg_class = {
  261. .class_name = "segment muxer",
  262. .item_name = av_default_item_name,
  263. .option = options,
  264. .version = LIBAVUTIL_VERSION_INT,
  265. };
  266. AVOutputFormat ff_segment_muxer = {
  267. .name = "segment",
  268. .long_name = NULL_IF_CONFIG_SMALL("segment"),
  269. .priv_data_size = sizeof(SegmentContext),
  270. .flags = AVFMT_GLOBALHEADER | AVFMT_NOFILE,
  271. .write_header = seg_write_header,
  272. .write_packet = seg_write_packet,
  273. .write_trailer = seg_write_trailer,
  274. .priv_class = &seg_class,
  275. };