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.

280 lines
8.2KB

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