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.

323 lines
10KB

  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. int wrap; ///< number after which the index wraps
  44. char *time_str; ///< segment duration specification string
  45. int64_t time; ///< segment duration
  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. if ((ret = av_parse_time(&seg->time, seg->time_str, 1)) < 0) {
  124. av_log(s, AV_LOG_ERROR,
  125. "Invalid time duration specification '%s' for segment_time option\n",
  126. seg->time_str);
  127. return ret;
  128. }
  129. oc = avformat_alloc_context();
  130. if (!oc)
  131. return AVERROR(ENOMEM);
  132. if (seg->list)
  133. if ((ret = avio_open2(&seg->list_pb, seg->list, AVIO_FLAG_WRITE,
  134. &s->interrupt_callback, NULL)) < 0)
  135. goto fail;
  136. for (i = 0; i< s->nb_streams; i++)
  137. seg->has_video +=
  138. (s->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO);
  139. if (seg->has_video > 1)
  140. av_log(s, AV_LOG_WARNING,
  141. "More than a single video stream present, "
  142. "expect issues decoding it.\n");
  143. oc->oformat = av_guess_format(seg->format, s->filename, NULL);
  144. if (!oc->oformat) {
  145. ret = AVERROR_MUXER_NOT_FOUND;
  146. goto fail;
  147. }
  148. if (oc->oformat->flags & AVFMT_NOFILE) {
  149. av_log(s, AV_LOG_ERROR, "format %s not supported.\n",
  150. oc->oformat->name);
  151. ret = AVERROR(EINVAL);
  152. goto fail;
  153. }
  154. seg->avf = oc;
  155. oc->streams = s->streams;
  156. oc->nb_streams = s->nb_streams;
  157. if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
  158. s->filename, seg->number++) < 0) {
  159. ret = AVERROR(EINVAL);
  160. goto fail;
  161. }
  162. if ((ret = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
  163. &s->interrupt_callback, NULL)) < 0)
  164. goto fail;
  165. if ((ret = avformat_write_header(oc, NULL)) < 0) {
  166. avio_close(oc->pb);
  167. goto fail;
  168. }
  169. fail:
  170. if (ret) {
  171. if (oc) {
  172. oc->streams = NULL;
  173. oc->nb_streams = 0;
  174. avformat_free_context(oc);
  175. }
  176. if (seg->list)
  177. avio_close(seg->list_pb);
  178. }
  179. return ret;
  180. }
  181. static int seg_write_packet(AVFormatContext *s, AVPacket *pkt)
  182. {
  183. SegmentContext *seg = s->priv_data;
  184. AVFormatContext *oc = seg->avf;
  185. AVStream *st = oc->streams[pkt->stream_index];
  186. int64_t end_pts = seg->time * seg->number;
  187. int ret;
  188. /* if the segment has video, start a new segment *only* with a key video frame */
  189. if ((st->codec->codec_type == AVMEDIA_TYPE_VIDEO || !seg->has_video) &&
  190. av_compare_ts(pkt->pts, st->time_base,
  191. end_pts, AV_TIME_BASE_Q) >= 0 &&
  192. pkt->flags & AV_PKT_FLAG_KEY) {
  193. av_log(s, AV_LOG_DEBUG, "Next segment starts with packet stream:%d pts:%"PRId64" pts_time:%f\n",
  194. pkt->stream_index, pkt->pts, pkt->pts * av_q2d(st->time_base));
  195. if ((ret = segment_end(s)) < 0 || (ret = segment_start(s)) < 0)
  196. goto fail;
  197. seg->start_time = (double)pkt->pts * av_q2d(st->time_base);
  198. } else if (pkt->pts != AV_NOPTS_VALUE) {
  199. seg->end_time = FFMAX(seg->end_time,
  200. (double)(pkt->pts + pkt->duration) * av_q2d(st->time_base));
  201. }
  202. ret = oc->oformat->write_packet(oc, pkt);
  203. fail:
  204. if (ret < 0) {
  205. oc->streams = NULL;
  206. oc->nb_streams = 0;
  207. if (seg->list)
  208. avio_close(seg->list_pb);
  209. avformat_free_context(oc);
  210. }
  211. return ret;
  212. }
  213. static int seg_write_trailer(struct AVFormatContext *s)
  214. {
  215. SegmentContext *seg = s->priv_data;
  216. AVFormatContext *oc = seg->avf;
  217. int ret = segment_end(s);
  218. if (seg->list)
  219. avio_close(seg->list_pb);
  220. av_opt_free(seg);
  221. oc->streams = NULL;
  222. oc->nb_streams = 0;
  223. avformat_free_context(oc);
  224. return ret;
  225. }
  226. #define OFFSET(x) offsetof(SegmentContext, x)
  227. #define E AV_OPT_FLAG_ENCODING_PARAM
  228. static const AVOption options[] = {
  229. { "segment_format", "set container format used for the segments", OFFSET(format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
  230. { "segment_list", "set the segment list filename", OFFSET(list), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
  231. { "segment_list_size", "set the maximum number of playlist entries", OFFSET(list_size), AV_OPT_TYPE_INT, {.dbl = 5}, 0, INT_MAX, E },
  232. { "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" },
  233. { "flat", "flat format", 0, AV_OPT_TYPE_CONST, {.dbl=LIST_TYPE_FLAT }, INT_MIN, INT_MAX, 0, "list_type" },
  234. { "ext", "extended format", 0, AV_OPT_TYPE_CONST, {.dbl=LIST_TYPE_EXT }, INT_MIN, INT_MAX, 0, "list_type" },
  235. { "segment_time", "set segment duration", OFFSET(time_str),AV_OPT_TYPE_STRING, {.str = "2"}, 0, 0, E },
  236. { "segment_wrap", "set number after which the index wraps", OFFSET(wrap), AV_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, E },
  237. { NULL },
  238. };
  239. static const AVClass seg_class = {
  240. .class_name = "segment muxer",
  241. .item_name = av_default_item_name,
  242. .option = options,
  243. .version = LIBAVUTIL_VERSION_INT,
  244. };
  245. AVOutputFormat ff_segment_muxer = {
  246. .name = "segment",
  247. .long_name = NULL_IF_CONFIG_SMALL("segment muxer"),
  248. .priv_data_size = sizeof(SegmentContext),
  249. .flags = AVFMT_GLOBALHEADER | AVFMT_NOFILE,
  250. .write_header = seg_write_header,
  251. .write_packet = seg_write_packet,
  252. .write_trailer = seg_write_trailer,
  253. .priv_class = &seg_class,
  254. };
  255. static const AVClass sseg_class = {
  256. .class_name = "stream_segment muxer",
  257. .item_name = av_default_item_name,
  258. .option = options,
  259. .version = LIBAVUTIL_VERSION_INT,
  260. };
  261. AVOutputFormat ff_stream_segment_muxer = {
  262. .name = "stream_segment,ssegment",
  263. .long_name = NULL_IF_CONFIG_SMALL("streaming segment muxer"),
  264. .priv_data_size = sizeof(SegmentContext),
  265. .flags = AVFMT_NOFILE,
  266. .write_header = seg_write_header,
  267. .write_packet = seg_write_packet,
  268. .write_trailer = seg_write_trailer,
  269. .priv_class = &sseg_class,
  270. };