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.

402 lines
13KB

  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/avassert.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 enum {
  31. LIST_TYPE_FLAT = 0,
  32. LIST_TYPE_EXT,
  33. LIST_TYPE_NB,
  34. } ListType;
  35. typedef struct {
  36. const AVClass *class; /**< Class for private options. */
  37. int number;
  38. AVFormatContext *avf;
  39. char *format; ///< format to use for output segment files
  40. char *list; ///< filename for the segment list file
  41. int list_size; ///< number of entries for the segment list file
  42. ListType list_type; ///< set the list type
  43. AVIOContext *list_pb; ///< list file put-byte context
  44. int wrap; ///< number after which the index wraps
  45. char *time_str; ///< segment duration specification string
  46. int64_t time; ///< segment duration
  47. char *times_str; ///< segment times specification string
  48. int64_t *times; ///< list of segment interval specification
  49. int nb_times; ///< number of elments in the times array
  50. int has_video;
  51. double start_time, end_time;
  52. } SegmentContext;
  53. static int segment_start(AVFormatContext *s)
  54. {
  55. SegmentContext *seg = s->priv_data;
  56. AVFormatContext *oc = seg->avf;
  57. int err = 0;
  58. if (seg->wrap)
  59. seg->number %= seg->wrap;
  60. if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
  61. s->filename, seg->number++) < 0) {
  62. av_log(oc, AV_LOG_ERROR, "Invalid segment filename template '%s'\n", s->filename);
  63. return AVERROR(EINVAL);
  64. }
  65. if ((err = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
  66. &s->interrupt_callback, NULL)) < 0)
  67. return err;
  68. if (!oc->priv_data && oc->oformat->priv_data_size > 0) {
  69. oc->priv_data = av_mallocz(oc->oformat->priv_data_size);
  70. if (!oc->priv_data) {
  71. avio_close(oc->pb);
  72. return AVERROR(ENOMEM);
  73. }
  74. if (oc->oformat->priv_class) {
  75. *(const AVClass**)oc->priv_data = oc->oformat->priv_class;
  76. av_opt_set_defaults(oc->priv_data);
  77. }
  78. }
  79. if ((err = oc->oformat->write_header(oc)) < 0) {
  80. goto fail;
  81. }
  82. return 0;
  83. fail:
  84. av_log(oc, AV_LOG_ERROR, "Failure occurred when starting segment '%s'\n",
  85. oc->filename);
  86. avio_close(oc->pb);
  87. av_freep(&oc->priv_data);
  88. return err;
  89. }
  90. static int segment_end(AVFormatContext *s)
  91. {
  92. SegmentContext *seg = s->priv_data;
  93. AVFormatContext *oc = seg->avf;
  94. int ret = 0;
  95. if (oc->oformat->write_trailer)
  96. ret = oc->oformat->write_trailer(oc);
  97. if (ret < 0)
  98. av_log(s, AV_LOG_ERROR, "Failure occurred when ending segment '%s'\n",
  99. oc->filename);
  100. if (seg->list) {
  101. if (seg->list_size && !(seg->number % seg->list_size)) {
  102. avio_close(seg->list_pb);
  103. if ((ret = avio_open2(&seg->list_pb, seg->list, AVIO_FLAG_WRITE,
  104. &s->interrupt_callback, NULL)) < 0)
  105. goto end;
  106. }
  107. if (seg->list_type == LIST_TYPE_FLAT) {
  108. avio_printf(seg->list_pb, "%s\n", oc->filename);
  109. } else if (seg->list_type == LIST_TYPE_EXT) {
  110. avio_printf(seg->list_pb, "%s,%f,%f\n", oc->filename, seg->start_time, seg->end_time);
  111. }
  112. avio_flush(seg->list_pb);
  113. }
  114. end:
  115. avio_close(oc->pb);
  116. if (oc->oformat->priv_class)
  117. av_opt_free(oc->priv_data);
  118. av_freep(&oc->priv_data);
  119. return ret;
  120. }
  121. static int parse_times(void *log_ctx, int64_t **times, int *nb_times,
  122. const char *times_str)
  123. {
  124. char *p;
  125. int i, ret = 0;
  126. char *times_str1 = av_strdup(times_str);
  127. char *saveptr = NULL;
  128. if (!times_str1)
  129. return AVERROR(ENOMEM);
  130. #define FAIL(err) ret = err; goto end
  131. *nb_times = 1;
  132. for (p = times_str1; *p; p++)
  133. if (*p == ',')
  134. (*nb_times)++;
  135. *times = av_malloc(sizeof(**times) * *nb_times);
  136. if (!*times) {
  137. av_log(log_ctx, AV_LOG_ERROR, "Could not allocate forced times array\n");
  138. FAIL(AVERROR(ENOMEM));
  139. }
  140. p = times_str1;
  141. for (i = 0; i < *nb_times; i++) {
  142. int64_t t;
  143. char *tstr = av_strtok(p, ",", &saveptr);
  144. av_assert0(tstr);
  145. p = NULL;
  146. ret = av_parse_time(&t, tstr, 1);
  147. if (ret < 0) {
  148. av_log(log_ctx, AV_LOG_ERROR,
  149. "Invalid time duration specification in %s\n", p);
  150. FAIL(AVERROR(EINVAL));
  151. }
  152. (*times)[i] = t;
  153. /* check on monotonicity */
  154. if (i && (*times)[i-1] > (*times)[i]) {
  155. av_log(log_ctx, AV_LOG_ERROR,
  156. "Specified time %f is greater than the following time %f\n",
  157. (float)((*times)[i])/1000000, (float)((*times)[i-1])/1000000);
  158. FAIL(AVERROR(EINVAL));
  159. }
  160. }
  161. end:
  162. av_free(times_str1);
  163. return ret;
  164. }
  165. static int seg_write_header(AVFormatContext *s)
  166. {
  167. SegmentContext *seg = s->priv_data;
  168. AVFormatContext *oc;
  169. int ret, i;
  170. seg->number = 0;
  171. if (seg->time_str && seg->times_str) {
  172. av_log(s, AV_LOG_ERROR,
  173. "segment_time and segment_times options are mutually exclusive, select just one of them\n");
  174. return AVERROR(EINVAL);
  175. }
  176. if (seg->times_str) {
  177. if ((ret = parse_times(s, &seg->times, &seg->nb_times, seg->times_str)) < 0)
  178. return ret;
  179. } else {
  180. /* set default value if not specified */
  181. if (!seg->time_str)
  182. seg->time_str = av_strdup("2");
  183. if ((ret = av_parse_time(&seg->time, seg->time_str, 1)) < 0) {
  184. av_log(s, AV_LOG_ERROR,
  185. "Invalid time duration specification '%s' for segment_time option\n",
  186. seg->time_str);
  187. return ret;
  188. }
  189. }
  190. oc = avformat_alloc_context();
  191. if (!oc)
  192. return AVERROR(ENOMEM);
  193. if (seg->list)
  194. if ((ret = avio_open2(&seg->list_pb, seg->list, AVIO_FLAG_WRITE,
  195. &s->interrupt_callback, NULL)) < 0)
  196. goto fail;
  197. for (i = 0; i< s->nb_streams; i++)
  198. seg->has_video +=
  199. (s->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO);
  200. if (seg->has_video > 1)
  201. av_log(s, AV_LOG_WARNING,
  202. "More than a single video stream present, "
  203. "expect issues decoding it.\n");
  204. oc->oformat = av_guess_format(seg->format, s->filename, NULL);
  205. if (!oc->oformat) {
  206. ret = AVERROR_MUXER_NOT_FOUND;
  207. goto fail;
  208. }
  209. if (oc->oformat->flags & AVFMT_NOFILE) {
  210. av_log(s, AV_LOG_ERROR, "format %s not supported.\n",
  211. oc->oformat->name);
  212. ret = AVERROR(EINVAL);
  213. goto fail;
  214. }
  215. seg->avf = oc;
  216. oc->streams = s->streams;
  217. oc->nb_streams = s->nb_streams;
  218. if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
  219. s->filename, seg->number++) < 0) {
  220. ret = AVERROR(EINVAL);
  221. goto fail;
  222. }
  223. if ((ret = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
  224. &s->interrupt_callback, NULL)) < 0)
  225. goto fail;
  226. if ((ret = avformat_write_header(oc, NULL)) < 0) {
  227. avio_close(oc->pb);
  228. goto fail;
  229. }
  230. fail:
  231. if (ret) {
  232. if (oc) {
  233. oc->streams = NULL;
  234. oc->nb_streams = 0;
  235. avformat_free_context(oc);
  236. }
  237. if (seg->list)
  238. avio_close(seg->list_pb);
  239. }
  240. return ret;
  241. }
  242. static int seg_write_packet(AVFormatContext *s, AVPacket *pkt)
  243. {
  244. SegmentContext *seg = s->priv_data;
  245. AVFormatContext *oc = seg->avf;
  246. AVStream *st = oc->streams[pkt->stream_index];
  247. int64_t end_pts;
  248. int ret;
  249. if (seg->times) {
  250. end_pts = seg->number <= seg->nb_times ? seg->times[seg->number-1] : INT64_MAX;
  251. } else {
  252. end_pts = seg->time * seg->number;
  253. }
  254. /* if the segment has video, start a new segment *only* with a key video frame */
  255. if ((st->codec->codec_type == AVMEDIA_TYPE_VIDEO || !seg->has_video) &&
  256. av_compare_ts(pkt->pts, st->time_base,
  257. end_pts, AV_TIME_BASE_Q) >= 0 &&
  258. pkt->flags & AV_PKT_FLAG_KEY) {
  259. av_log(s, AV_LOG_DEBUG, "Next segment starts with packet stream:%d pts:%"PRId64" pts_time:%f\n",
  260. pkt->stream_index, pkt->pts, pkt->pts * av_q2d(st->time_base));
  261. if ((ret = segment_end(s)) < 0 || (ret = segment_start(s)) < 0)
  262. goto fail;
  263. seg->start_time = (double)pkt->pts * av_q2d(st->time_base);
  264. } else if (pkt->pts != AV_NOPTS_VALUE) {
  265. seg->end_time = FFMAX(seg->end_time,
  266. (double)(pkt->pts + pkt->duration) * av_q2d(st->time_base));
  267. }
  268. ret = oc->oformat->write_packet(oc, pkt);
  269. fail:
  270. if (ret < 0) {
  271. oc->streams = NULL;
  272. oc->nb_streams = 0;
  273. if (seg->list)
  274. avio_close(seg->list_pb);
  275. avformat_free_context(oc);
  276. }
  277. return ret;
  278. }
  279. static int seg_write_trailer(struct AVFormatContext *s)
  280. {
  281. SegmentContext *seg = s->priv_data;
  282. AVFormatContext *oc = seg->avf;
  283. int ret = segment_end(s);
  284. if (seg->list)
  285. avio_close(seg->list_pb);
  286. av_opt_free(seg);
  287. av_freep(&seg->times);
  288. oc->streams = NULL;
  289. oc->nb_streams = 0;
  290. avformat_free_context(oc);
  291. return ret;
  292. }
  293. #define OFFSET(x) offsetof(SegmentContext, x)
  294. #define E AV_OPT_FLAG_ENCODING_PARAM
  295. static const AVOption options[] = {
  296. { "segment_format", "set container format used for the segments", OFFSET(format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
  297. { "segment_list", "set the segment list filename", OFFSET(list), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
  298. { "segment_list_size", "set the maximum number of playlist entries", OFFSET(list_size), AV_OPT_TYPE_INT, {.dbl = 5}, 0, INT_MAX, E },
  299. { "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" },
  300. { "flat", "flat format", 0, AV_OPT_TYPE_CONST, {.dbl=LIST_TYPE_FLAT }, INT_MIN, INT_MAX, 0, "list_type" },
  301. { "ext", "extended format", 0, AV_OPT_TYPE_CONST, {.dbl=LIST_TYPE_EXT }, INT_MIN, INT_MAX, 0, "list_type" },
  302. { "segment_time", "set segment duration", OFFSET(time_str),AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
  303. { "segment_times", "set segment split time points", OFFSET(times_str),AV_OPT_TYPE_STRING,{.str = NULL}, 0, 0, E },
  304. { "segment_wrap", "set number after which the index wraps", OFFSET(wrap), AV_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, E },
  305. { NULL },
  306. };
  307. static const AVClass seg_class = {
  308. .class_name = "segment muxer",
  309. .item_name = av_default_item_name,
  310. .option = options,
  311. .version = LIBAVUTIL_VERSION_INT,
  312. };
  313. AVOutputFormat ff_segment_muxer = {
  314. .name = "segment",
  315. .long_name = NULL_IF_CONFIG_SMALL("segment muxer"),
  316. .priv_data_size = sizeof(SegmentContext),
  317. .flags = AVFMT_GLOBALHEADER | AVFMT_NOFILE,
  318. .write_header = seg_write_header,
  319. .write_packet = seg_write_packet,
  320. .write_trailer = seg_write_trailer,
  321. .priv_class = &seg_class,
  322. };
  323. static const AVClass sseg_class = {
  324. .class_name = "stream_segment muxer",
  325. .item_name = av_default_item_name,
  326. .option = options,
  327. .version = LIBAVUTIL_VERSION_INT,
  328. };
  329. AVOutputFormat ff_stream_segment_muxer = {
  330. .name = "stream_segment,ssegment",
  331. .long_name = NULL_IF_CONFIG_SMALL("streaming segment muxer"),
  332. .priv_data_size = sizeof(SegmentContext),
  333. .flags = AVFMT_NOFILE,
  334. .write_header = seg_write_header,
  335. .write_packet = seg_write_packet,
  336. .write_trailer = seg_write_trailer,
  337. .priv_class = &sseg_class,
  338. };