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.

276 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. oc->interrupt_callback = s->interrupt_callback;
  121. seg->avf = oc;
  122. oc->streams = s->streams;
  123. oc->nb_streams = s->nb_streams;
  124. if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
  125. s->filename, seg->number++) < 0) {
  126. ret = AVERROR(EINVAL);
  127. goto fail;
  128. }
  129. if ((ret = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
  130. &s->interrupt_callback, NULL)) < 0)
  131. goto fail;
  132. if ((ret = avformat_write_header(oc, NULL)) < 0) {
  133. avio_close(oc->pb);
  134. goto fail;
  135. }
  136. if (seg->list) {
  137. avio_printf(seg->pb, "%s\n", oc->filename);
  138. avio_flush(seg->pb);
  139. }
  140. fail:
  141. if (ret) {
  142. oc->streams = NULL;
  143. oc->nb_streams = 0;
  144. if (seg->list)
  145. avio_close(seg->pb);
  146. avformat_free_context(oc);
  147. }
  148. return ret;
  149. }
  150. static int seg_write_packet(AVFormatContext *s, AVPacket *pkt)
  151. {
  152. SegmentContext *seg = s->priv_data;
  153. AVFormatContext *oc = seg->avf;
  154. AVStream *st = oc->streams[pkt->stream_index];
  155. int64_t end_pts = seg->recording_time * seg->number;
  156. int ret;
  157. if ((seg->has_video && st->codec->codec_type == AVMEDIA_TYPE_VIDEO) &&
  158. av_compare_ts(pkt->pts, st->time_base,
  159. end_pts, AV_TIME_BASE_Q) >= 0 &&
  160. pkt->flags & AV_PKT_FLAG_KEY) {
  161. av_log(s, AV_LOG_DEBUG, "Next segment starts at %d %"PRId64"\n",
  162. pkt->stream_index, pkt->pts);
  163. ret = segment_end(oc);
  164. if (!ret)
  165. ret = segment_start(s);
  166. if (ret)
  167. goto fail;
  168. if (seg->list) {
  169. avio_printf(seg->pb, "%s\n", oc->filename);
  170. avio_flush(seg->pb);
  171. if (seg->size && !(seg->number % seg->size)) {
  172. avio_close(seg->pb);
  173. if ((ret = avio_open2(&seg->pb, seg->list, AVIO_FLAG_WRITE,
  174. &s->interrupt_callback, NULL)) < 0)
  175. goto fail;
  176. }
  177. }
  178. }
  179. ret = oc->oformat->write_packet(oc, pkt);
  180. fail:
  181. if (ret < 0) {
  182. oc->streams = NULL;
  183. oc->nb_streams = 0;
  184. if (seg->list)
  185. avio_close(seg->pb);
  186. avformat_free_context(oc);
  187. }
  188. return ret;
  189. }
  190. static int seg_write_trailer(struct AVFormatContext *s)
  191. {
  192. SegmentContext *seg = s->priv_data;
  193. AVFormatContext *oc = seg->avf;
  194. int ret = segment_end(oc);
  195. if (seg->list)
  196. avio_close(seg->pb);
  197. oc->streams = NULL;
  198. oc->nb_streams = 0;
  199. avformat_free_context(oc);
  200. return ret;
  201. }
  202. #define OFFSET(x) offsetof(SegmentContext, x)
  203. #define E AV_OPT_FLAG_ENCODING_PARAM
  204. static const AVOption options[] = {
  205. { "segment_format", "container format used for the segments", OFFSET(format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
  206. { "segment_time", "segment length in seconds", OFFSET(time), AV_OPT_TYPE_FLOAT, {.dbl = 2}, 0, FLT_MAX, E },
  207. { "segment_list", "output the segment list", OFFSET(list), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
  208. { "segment_list_size", "maximum number of playlist entries", OFFSET(size), AV_OPT_TYPE_INT, {.i64 = 5}, 0, INT_MAX, E },
  209. { "segment_wrap", "number after which the index wraps", OFFSET(wrap), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E },
  210. { NULL },
  211. };
  212. static const AVClass seg_class = {
  213. .class_name = "segment muxer",
  214. .item_name = av_default_item_name,
  215. .option = options,
  216. .version = LIBAVUTIL_VERSION_INT,
  217. };
  218. AVOutputFormat ff_segment_muxer = {
  219. .name = "segment",
  220. .long_name = NULL_IF_CONFIG_SMALL("segment"),
  221. .priv_data_size = sizeof(SegmentContext),
  222. .flags = AVFMT_GLOBALHEADER | AVFMT_NOFILE,
  223. .write_header = seg_write_header,
  224. .write_packet = seg_write_packet,
  225. .write_trailer = seg_write_trailer,
  226. .priv_class = &seg_class,
  227. };