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.

275 lines
7.9KB

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