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.

253 lines
7.4KB

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