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.

417 lines
13KB

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