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