segment.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 <strings.h>
  22. #include <float.h>
  23. #include "avformat.h"
  24. #include "internal.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 struct {
  31. const AVClass *class; /**< Class for private options. */
  32. int number;
  33. AVFormatContext *avf;
  34. char *format; /**< Set by a private option. */
  35. char *list; /**< Set by a private option. */
  36. float time; /**< Set by a private option. */
  37. int size; /**< Set by a private option. */
  38. int64_t offset_time;
  39. int64_t recording_time;
  40. int has_video;
  41. AVIOContext *pb;
  42. } SegmentContext;
  43. static int segment_start(AVFormatContext *s)
  44. {
  45. SegmentContext *c = s->priv_data;
  46. AVFormatContext *oc = c->avf;
  47. int err = 0;
  48. if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
  49. s->filename, c->number++) < 0)
  50. return AVERROR(EINVAL);
  51. if ((err = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
  52. &s->interrupt_callback, NULL)) < 0)
  53. return err;
  54. if (!oc->priv_data && oc->oformat->priv_data_size > 0) {
  55. oc->priv_data = av_mallocz(oc->oformat->priv_data_size);
  56. if (!oc->priv_data) {
  57. avio_close(oc->pb);
  58. return AVERROR(ENOMEM);
  59. }
  60. if (oc->oformat->priv_class) {
  61. *(const AVClass**)oc->priv_data = oc->oformat->priv_class;
  62. av_opt_set_defaults(oc->priv_data);
  63. }
  64. }
  65. if ((err = oc->oformat->write_header(oc)) < 0) {
  66. goto fail;
  67. }
  68. return 0;
  69. fail:
  70. avio_close(oc->pb);
  71. av_freep(&oc->priv_data);
  72. return err;
  73. }
  74. static int segment_end(AVFormatContext *oc)
  75. {
  76. int ret = 0;
  77. if (oc->oformat->write_trailer)
  78. ret = oc->oformat->write_trailer(oc);
  79. avio_close(oc->pb);
  80. if (oc->oformat->priv_class)
  81. av_opt_free(oc->priv_data);
  82. av_freep(&oc->priv_data);
  83. return ret;
  84. }
  85. static int seg_write_header(AVFormatContext *s)
  86. {
  87. SegmentContext *seg = s->priv_data;
  88. AVFormatContext *oc;
  89. int ret, i;
  90. seg->number = 0;
  91. seg->offset_time = 0;
  92. seg->recording_time = seg->time * 1000000;
  93. if (seg->list)
  94. if ((ret = avio_open2(&seg->pb, seg->list, AVIO_FLAG_WRITE,
  95. &s->interrupt_callback, NULL)) < 0)
  96. return ret;
  97. for (i = 0; i< s->nb_streams; i++)
  98. seg->has_video +=
  99. (s->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO);
  100. if (seg->has_video > 1)
  101. av_log(s, AV_LOG_WARNING,
  102. "More than a single video stream present, "
  103. "expect issues decoding it.\n");
  104. oc = avformat_alloc_context();
  105. if (!oc) {
  106. ret = AVERROR(ENOMEM);
  107. goto fail;
  108. }
  109. oc->oformat = av_guess_format(seg->format, s->filename, NULL);
  110. if (!oc->oformat) {
  111. ret = AVERROR_MUXER_NOT_FOUND;
  112. goto fail;
  113. }
  114. if (oc->oformat->flags & AVFMT_NOFILE) {
  115. av_log(s, AV_LOG_ERROR, "format %s not supported.\n",
  116. oc->oformat->name);
  117. ret = AVERROR(EINVAL);
  118. goto fail;
  119. }
  120. seg->avf = oc;
  121. oc->streams = s->streams;
  122. oc->nb_streams = s->nb_streams;
  123. if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
  124. s->filename, seg->number++) < 0) {
  125. ret = AVERROR(EINVAL);
  126. goto fail;
  127. }
  128. if ((ret = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
  129. &s->interrupt_callback, NULL)) < 0)
  130. goto fail;
  131. if ((ret = avformat_write_header(oc, NULL)) < 0) {
  132. avio_close(oc->pb);
  133. goto fail;
  134. }
  135. if (seg->list) {
  136. avio_printf(seg->pb, "%s\n", oc->filename);
  137. avio_flush(seg->pb);
  138. }
  139. fail:
  140. if (ret) {
  141. oc->streams = NULL;
  142. oc->nb_streams = 0;
  143. if (seg->list)
  144. avio_close(seg->pb);
  145. avformat_free_context(oc);
  146. }
  147. return ret;
  148. }
  149. static int seg_write_packet(AVFormatContext *s, AVPacket *pkt)
  150. {
  151. SegmentContext *seg = s->priv_data;
  152. AVFormatContext *oc = seg->avf;
  153. AVStream *st = oc->streams[pkt->stream_index];
  154. int64_t end_pts = seg->recording_time * seg->number;
  155. int ret;
  156. if ((seg->has_video && st->codec->codec_type == AVMEDIA_TYPE_VIDEO) &&
  157. av_compare_ts(pkt->pts, st->time_base,
  158. end_pts, AV_TIME_BASE_Q) >= 0 &&
  159. pkt->flags & AV_PKT_FLAG_KEY) {
  160. av_log(s, AV_LOG_DEBUG, "Next segment starts at %d %"PRId64"\n",
  161. pkt->stream_index, pkt->pts);
  162. ret = segment_end(oc);
  163. if (!ret)
  164. ret = segment_start(s);
  165. if (ret)
  166. goto fail;
  167. if (seg->list) {
  168. avio_printf(seg->pb, "%s\n", oc->filename);
  169. avio_flush(seg->pb);
  170. if (seg->size && !(seg->number % seg->size)) {
  171. avio_close(seg->pb);
  172. if ((ret = avio_open2(&seg->pb, seg->list, AVIO_FLAG_WRITE,
  173. &s->interrupt_callback, NULL)) < 0)
  174. goto fail;
  175. }
  176. }
  177. }
  178. ret = oc->oformat->write_packet(oc, pkt);
  179. fail:
  180. if (ret < 0) {
  181. oc->streams = NULL;
  182. oc->nb_streams = 0;
  183. if (seg->list)
  184. avio_close(seg->pb);
  185. avformat_free_context(oc);
  186. }
  187. return ret;
  188. }
  189. static int seg_write_trailer(struct AVFormatContext *s)
  190. {
  191. SegmentContext *seg = s->priv_data;
  192. AVFormatContext *oc = seg->avf;
  193. int ret = segment_end(oc);
  194. if (seg->list)
  195. avio_close(seg->pb);
  196. oc->streams = NULL;
  197. oc->nb_streams = 0;
  198. avformat_free_context(oc);
  199. return ret;
  200. }
  201. #define OFFSET(x) offsetof(SegmentContext, x)
  202. #define E AV_OPT_FLAG_ENCODING_PARAM
  203. static const AVOption options[] = {
  204. { "segment_format", "container format used for the segments", OFFSET(format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
  205. { "segment_time", "segment length in seconds", OFFSET(time), AV_OPT_TYPE_FLOAT, {.dbl = 2}, 0, FLT_MAX, E },
  206. { "segment_list", "output the segment list", OFFSET(list), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
  207. { "segment_list_size", "maximum number of playlist entries", OFFSET(size), AV_OPT_TYPE_INT, {.dbl = 5}, 0, INT_MAX, E },
  208. { NULL },
  209. };
  210. static const AVClass seg_class = {
  211. .class_name = "segment muxer",
  212. .item_name = av_default_item_name,
  213. .option = options,
  214. .version = LIBAVUTIL_VERSION_INT,
  215. };
  216. AVOutputFormat ff_segment_muxer = {
  217. .name = "segment",
  218. .long_name = NULL_IF_CONFIG_SMALL("segment muxer"),
  219. .priv_data_size = sizeof(SegmentContext),
  220. .flags = AVFMT_GLOBALHEADER | AVFMT_NOFILE,
  221. .write_header = seg_write_header,
  222. .write_packet = seg_write_packet,
  223. .write_trailer = seg_write_trailer,
  224. .priv_class = &seg_class,
  225. };