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.

277 lines
8.0KB

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