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.

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