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.

314 lines
9.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 enum {
  30. LIST_TYPE_FLAT = 0,
  31. LIST_TYPE_EXT,
  32. LIST_TYPE_NB,
  33. } ListType;
  34. typedef struct {
  35. const AVClass *class; /**< Class for private options. */
  36. int number;
  37. AVFormatContext *avf;
  38. char *format; ///< format to use for output segment files
  39. char *list; ///< filename for the segment list file
  40. int list_size; ///< number of entries for the segment list file
  41. ListType list_type; ///< set the list type
  42. AVIOContext *list_pb; ///< list file put-byte context
  43. float time; ///< segment duration
  44. int wrap; ///< number after which the index wraps
  45. int64_t recording_time;
  46. int has_video;
  47. double start_time, end_time;
  48. } SegmentContext;
  49. static int segment_start(AVFormatContext *s)
  50. {
  51. SegmentContext *seg = s->priv_data;
  52. AVFormatContext *oc = seg->avf;
  53. int err = 0;
  54. if (seg->wrap)
  55. seg->number %= seg->wrap;
  56. if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
  57. s->filename, seg->number++) < 0) {
  58. av_log(oc, AV_LOG_ERROR, "Invalid segment filename template '%s'\n", s->filename);
  59. return AVERROR(EINVAL);
  60. }
  61. if ((err = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
  62. &s->interrupt_callback, NULL)) < 0)
  63. return err;
  64. if (!oc->priv_data && oc->oformat->priv_data_size > 0) {
  65. oc->priv_data = av_mallocz(oc->oformat->priv_data_size);
  66. if (!oc->priv_data) {
  67. avio_close(oc->pb);
  68. return AVERROR(ENOMEM);
  69. }
  70. if (oc->oformat->priv_class) {
  71. *(const AVClass**)oc->priv_data = oc->oformat->priv_class;
  72. av_opt_set_defaults(oc->priv_data);
  73. }
  74. }
  75. if ((err = oc->oformat->write_header(oc)) < 0) {
  76. goto fail;
  77. }
  78. return 0;
  79. fail:
  80. av_log(oc, AV_LOG_ERROR, "Failure occurred when starting segment '%s'\n",
  81. oc->filename);
  82. avio_close(oc->pb);
  83. av_freep(&oc->priv_data);
  84. return err;
  85. }
  86. static int segment_end(AVFormatContext *s)
  87. {
  88. SegmentContext *seg = s->priv_data;
  89. AVFormatContext *oc = seg->avf;
  90. int ret = 0;
  91. if (oc->oformat->write_trailer)
  92. ret = oc->oformat->write_trailer(oc);
  93. if (ret < 0)
  94. av_log(s, AV_LOG_ERROR, "Failure occurred when ending segment '%s'\n",
  95. oc->filename);
  96. if (seg->list) {
  97. if (seg->list_size && !(seg->number % seg->list_size)) {
  98. avio_close(seg->list_pb);
  99. if ((ret = avio_open2(&seg->list_pb, seg->list, AVIO_FLAG_WRITE,
  100. &s->interrupt_callback, NULL)) < 0)
  101. goto end;
  102. }
  103. if (seg->list_type == LIST_TYPE_FLAT) {
  104. avio_printf(seg->list_pb, "%s\n", oc->filename);
  105. } else if (seg->list_type == LIST_TYPE_EXT) {
  106. avio_printf(seg->list_pb, "%s,%f,%f\n", oc->filename, seg->start_time, seg->end_time);
  107. }
  108. avio_flush(seg->list_pb);
  109. }
  110. end:
  111. avio_close(oc->pb);
  112. if (oc->oformat->priv_class)
  113. av_opt_free(oc->priv_data);
  114. av_freep(&oc->priv_data);
  115. return ret;
  116. }
  117. static int seg_write_header(AVFormatContext *s)
  118. {
  119. SegmentContext *seg = s->priv_data;
  120. AVFormatContext *oc;
  121. int ret, i;
  122. seg->number = 0;
  123. seg->recording_time = seg->time * 1000000;
  124. oc = avformat_alloc_context();
  125. if (!oc)
  126. return AVERROR(ENOMEM);
  127. if (seg->list)
  128. if ((ret = avio_open2(&seg->list_pb, seg->list, AVIO_FLAG_WRITE,
  129. &s->interrupt_callback, NULL)) < 0)
  130. goto fail;
  131. for (i = 0; i< s->nb_streams; i++)
  132. seg->has_video +=
  133. (s->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO);
  134. if (seg->has_video > 1)
  135. av_log(s, AV_LOG_WARNING,
  136. "More than a single video stream present, "
  137. "expect issues decoding it.\n");
  138. oc->oformat = av_guess_format(seg->format, s->filename, NULL);
  139. if (!oc->oformat) {
  140. ret = AVERROR_MUXER_NOT_FOUND;
  141. goto fail;
  142. }
  143. if (oc->oformat->flags & AVFMT_NOFILE) {
  144. av_log(s, AV_LOG_ERROR, "format %s not supported.\n",
  145. oc->oformat->name);
  146. ret = AVERROR(EINVAL);
  147. goto fail;
  148. }
  149. seg->avf = oc;
  150. oc->streams = s->streams;
  151. oc->nb_streams = s->nb_streams;
  152. if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
  153. s->filename, seg->number++) < 0) {
  154. ret = AVERROR(EINVAL);
  155. goto fail;
  156. }
  157. if ((ret = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
  158. &s->interrupt_callback, NULL)) < 0)
  159. goto fail;
  160. if ((ret = avformat_write_header(oc, NULL)) < 0) {
  161. avio_close(oc->pb);
  162. goto fail;
  163. }
  164. fail:
  165. if (ret) {
  166. if (oc) {
  167. oc->streams = NULL;
  168. oc->nb_streams = 0;
  169. avformat_free_context(oc);
  170. }
  171. if (seg->list)
  172. avio_close(seg->list_pb);
  173. }
  174. return ret;
  175. }
  176. static int seg_write_packet(AVFormatContext *s, AVPacket *pkt)
  177. {
  178. SegmentContext *seg = s->priv_data;
  179. AVFormatContext *oc = seg->avf;
  180. AVStream *st = oc->streams[pkt->stream_index];
  181. int64_t end_pts = seg->recording_time * seg->number;
  182. int ret;
  183. /* if the segment has video, start a new segment *only* with a key video frame */
  184. if ((st->codec->codec_type == AVMEDIA_TYPE_VIDEO || !seg->has_video) &&
  185. av_compare_ts(pkt->pts, st->time_base,
  186. end_pts, AV_TIME_BASE_Q) >= 0 &&
  187. pkt->flags & AV_PKT_FLAG_KEY) {
  188. av_log(s, AV_LOG_DEBUG, "Next segment starts with packet stream:%d pts:%"PRId64" pts_time:%f\n",
  189. pkt->stream_index, pkt->pts, pkt->pts * av_q2d(st->time_base));
  190. if ((ret = segment_end(s)) < 0 || (ret = segment_start(s)) < 0)
  191. goto fail;
  192. seg->start_time = (double)pkt->pts * av_q2d(st->time_base);
  193. } else if (pkt->pts != AV_NOPTS_VALUE) {
  194. seg->end_time = FFMAX(seg->end_time,
  195. (double)(pkt->pts + pkt->duration) * av_q2d(st->time_base));
  196. }
  197. ret = oc->oformat->write_packet(oc, pkt);
  198. fail:
  199. if (ret < 0) {
  200. oc->streams = NULL;
  201. oc->nb_streams = 0;
  202. if (seg->list)
  203. avio_close(seg->list_pb);
  204. avformat_free_context(oc);
  205. }
  206. return ret;
  207. }
  208. static int seg_write_trailer(struct AVFormatContext *s)
  209. {
  210. SegmentContext *seg = s->priv_data;
  211. AVFormatContext *oc = seg->avf;
  212. int ret = segment_end(s);
  213. if (seg->list)
  214. avio_close(seg->list_pb);
  215. oc->streams = NULL;
  216. oc->nb_streams = 0;
  217. avformat_free_context(oc);
  218. return ret;
  219. }
  220. #define OFFSET(x) offsetof(SegmentContext, x)
  221. #define E AV_OPT_FLAG_ENCODING_PARAM
  222. static const AVOption options[] = {
  223. { "segment_format", "set container format used for the segments", OFFSET(format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
  224. { "segment_time", "set segment length in seconds", OFFSET(time), AV_OPT_TYPE_FLOAT, {.dbl = 2}, 0, FLT_MAX, E },
  225. { "segment_list", "set the segment list filename", OFFSET(list), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
  226. { "segment_list_size", "set the maximum number of playlist entries", OFFSET(list_size), AV_OPT_TYPE_INT, {.dbl = 5}, 0, INT_MAX, E },
  227. { "segment_list_type", "set the segment list type", OFFSET(list_type), AV_OPT_TYPE_INT, {.dbl = LIST_TYPE_FLAT}, 0, LIST_TYPE_NB-1, E, "list_type" },
  228. { "flat", "flat format", 0, AV_OPT_TYPE_CONST, {.dbl=LIST_TYPE_FLAT }, INT_MIN, INT_MAX, 0, "list_type" },
  229. { "ext", "extended format", 0, AV_OPT_TYPE_CONST, {.dbl=LIST_TYPE_EXT }, INT_MIN, INT_MAX, 0, "list_type" },
  230. { "segment_wrap", "set number after which the index wraps", OFFSET(wrap), AV_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, E },
  231. { NULL },
  232. };
  233. static const AVClass seg_class = {
  234. .class_name = "segment muxer",
  235. .item_name = av_default_item_name,
  236. .option = options,
  237. .version = LIBAVUTIL_VERSION_INT,
  238. };
  239. AVOutputFormat ff_segment_muxer = {
  240. .name = "segment",
  241. .long_name = NULL_IF_CONFIG_SMALL("segment muxer"),
  242. .priv_data_size = sizeof(SegmentContext),
  243. .flags = AVFMT_GLOBALHEADER | AVFMT_NOFILE,
  244. .write_header = seg_write_header,
  245. .write_packet = seg_write_packet,
  246. .write_trailer = seg_write_trailer,
  247. .priv_class = &seg_class,
  248. };
  249. static const AVClass sseg_class = {
  250. .class_name = "stream_segment muxer",
  251. .item_name = av_default_item_name,
  252. .option = options,
  253. .version = LIBAVUTIL_VERSION_INT,
  254. };
  255. AVOutputFormat ff_stream_segment_muxer = {
  256. .name = "stream_segment,ssegment",
  257. .long_name = NULL_IF_CONFIG_SMALL("streaming segment muxer"),
  258. .priv_data_size = sizeof(SegmentContext),
  259. .flags = AVFMT_NOFILE,
  260. .write_header = seg_write_header,
  261. .write_packet = seg_write_packet,
  262. .write_trailer = seg_write_trailer,
  263. .priv_class = &sseg_class,
  264. };