segment.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 "libavutil/avstring.h"
  22. #include "libavutil/log.h"
  23. #include "libavutil/opt.h"
  24. #include "libavutil/mathematics.h"
  25. #include "libavutil/parseutils.h"
  26. #include "avformat.h"
  27. #include "internal.h"
  28. #include <strings.h>
  29. #include <float.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 *pattern; /**< Set by a private option. */
  36. char *path; /**< Set by a private option. */
  37. float time; /**< Set by a private option. */
  38. int64_t offset_time;
  39. int64_t recording_time;
  40. } SegmentContext;
  41. #if CONFIG_SEGMENT_MUXER
  42. static int segment_header(SegmentContext *s)
  43. {
  44. AVFormatContext *oc = s->avf;
  45. int err = 0;
  46. av_strlcpy(oc->filename, s->path, sizeof(oc->filename));
  47. av_strlcatf(oc->filename, sizeof(oc->filename),
  48. s->pattern, s->number++);
  49. if ((err = avio_open(&oc->pb, oc->filename, AVIO_FLAG_WRITE)) < 0) {
  50. return err;
  51. }
  52. if (!oc->priv_data && oc->oformat->priv_data_size > 0) {
  53. oc->priv_data = av_mallocz(oc->oformat->priv_data_size);
  54. if (!oc->priv_data) {
  55. avio_close(oc->pb);
  56. return AVERROR(ENOMEM);
  57. }
  58. }
  59. if ((err = oc->oformat->write_header(oc)) < 0) {
  60. avio_close(oc->pb);
  61. av_freep(&oc->priv_data);
  62. }
  63. return err;
  64. }
  65. static int segment_trailer(AVFormatContext *oc)
  66. {
  67. int ret = 0;
  68. if(oc->oformat->write_trailer)
  69. ret = oc->oformat->write_trailer(oc);
  70. avio_close(oc->pb);
  71. av_freep(&oc->priv_data);
  72. return ret;
  73. }
  74. static int seg_write_header(AVFormatContext *s)
  75. {
  76. SegmentContext *seg = s->priv_data;
  77. AVFormatContext *oc;
  78. int ret;
  79. seg->number = 0;
  80. seg->recording_time = seg->time*1000000;
  81. seg->offset_time = 0;
  82. if (!seg->path) {
  83. char *t;
  84. seg->path = av_strdup(s->filename);
  85. t = strrchr(seg->path, '.');
  86. if (t) *t = '\0';
  87. }
  88. oc = avformat_alloc_context();
  89. if (!oc) {
  90. return AVERROR(ENOMEM);
  91. }
  92. oc->oformat = av_guess_format(seg->format, NULL, NULL);
  93. if (!oc->oformat) {
  94. avformat_free_context(oc);
  95. return AVERROR_MUXER_NOT_FOUND;
  96. }
  97. seg->avf = oc;
  98. oc->streams = s->streams;
  99. oc->nb_streams = s->nb_streams;
  100. av_strlcpy(oc->filename, seg->path, sizeof(oc->filename));
  101. av_strlcatf(oc->filename, sizeof(oc->filename),
  102. seg->pattern, seg->number++);
  103. if ((ret = avio_open(&oc->pb, oc->filename, AVIO_FLAG_WRITE)) < 0) {
  104. avformat_free_context(oc);
  105. return ret;
  106. }
  107. if ((ret = avformat_write_header(oc, NULL)) < 0) {
  108. avio_close(oc->pb);
  109. }
  110. if (ret)
  111. avformat_free_context(oc);
  112. avio_printf(s->pb, "%s\n", oc->filename);
  113. avio_flush(s->pb);
  114. return ret;
  115. }
  116. static int seg_write_packet(AVFormatContext *s, AVPacket *pkt)
  117. {
  118. SegmentContext *seg = s->priv_data;
  119. AVFormatContext *oc = seg->avf;
  120. AVStream *st = oc->streams[pkt->stream_index];
  121. int ret;
  122. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
  123. av_compare_ts(pkt->pts, st->time_base,
  124. seg->recording_time*seg->number,
  125. (AVRational){1, 1000000}) >= 0 &&
  126. pkt->flags & AV_PKT_FLAG_KEY) {
  127. av_log(s, AV_LOG_INFO, "I'd reset at %d %"PRId64"\n",
  128. pkt->stream_index, pkt->pts);
  129. ret = segment_trailer(oc);
  130. if (!ret)
  131. ret = segment_header(seg);
  132. if (ret) {
  133. avformat_free_context(oc);
  134. return ret;
  135. }
  136. avio_printf(s->pb, "%s\n", oc->filename);
  137. avio_flush(s->pb);
  138. }
  139. ret = oc->oformat->write_packet(oc, pkt);
  140. return ret;
  141. }
  142. static int seg_write_trailer(struct AVFormatContext *s)
  143. {
  144. SegmentContext *seg = s->priv_data;
  145. AVFormatContext *oc = seg->avf;
  146. return segment_trailer(oc);
  147. }
  148. #endif /* CONFIG_SEGMENT_MUXER */
  149. #define OFFSET(x) offsetof(SegmentContext, x)
  150. #define E AV_OPT_FLAG_ENCODING_PARAM
  151. static const AVOption options[] = {
  152. { "container_format", "container format used for the segments", OFFSET(format), AV_OPT_TYPE_STRING, {.str = "nut"}, 0, 0, E },
  153. { "segment_time", "segment lenght in seconds", OFFSET(time), AV_OPT_TYPE_FLOAT, {.dbl = 2}, 0, FLT_MAX, E },
  154. { "segment_pattern", "pattern to use in segment files", OFFSET(pattern),AV_OPT_TYPE_STRING, {.str = "%03d"}, 0, 0, E },
  155. { "segment_basename", "basename to use in segment files", OFFSET(path ),AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
  156. { NULL },
  157. };
  158. static const AVClass seg_class = {
  159. .class_name = "segment muxer",
  160. .item_name = av_default_item_name,
  161. .option = options,
  162. .version = LIBAVUTIL_VERSION_INT,
  163. };
  164. /* input
  165. #if CONFIG_IMAGE2_DEMUXER
  166. AVInputFormat ff_image2_demuxer = {
  167. .name = "image2",
  168. .long_name = NULL_IF_CONFIG_SMALL("image2 sequence"),
  169. .priv_data_size = sizeof(VideoData),
  170. .read_probe = read_probe,
  171. .read_header = read_header,
  172. .read_packet = read_packet,
  173. .flags = AVFMT_NOFILE,
  174. .priv_class = &img2_class,
  175. };
  176. #endif
  177. */
  178. /* output */
  179. #if CONFIG_SEGMENT_MUXER
  180. AVOutputFormat ff_segment_muxer = {
  181. .name = "segment",
  182. .long_name = NULL_IF_CONFIG_SMALL("segment muxer"),
  183. .extensions = "m3u8",
  184. .priv_data_size = sizeof(SegmentContext),
  185. .flags = AVFMT_GLOBALHEADER,
  186. .write_header = seg_write_header,
  187. .write_packet = seg_write_packet,
  188. .write_trailer = seg_write_trailer,
  189. .priv_class = &seg_class,
  190. };
  191. #endif