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.

283 lines
8.3KB

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