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.

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