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.

271 lines
8.1KB

  1. /*
  2. * Copyright (c) 2015, Vignesh Venkatasubramanian
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg 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. * FFmpeg 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 FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file WebM Chunk Muxer
  22. * The chunk muxer enables writing WebM Live chunks where there is a header
  23. * chunk, followed by data chunks where each Cluster is written out as a Chunk.
  24. */
  25. #include <float.h>
  26. #include <time.h>
  27. #include "avformat.h"
  28. #include "avio.h"
  29. #include "avio_internal.h"
  30. #include "internal.h"
  31. #include "libavutil/avassert.h"
  32. #include "libavutil/log.h"
  33. #include "libavutil/opt.h"
  34. #include "libavutil/avstring.h"
  35. #include "libavutil/parseutils.h"
  36. #include "libavutil/mathematics.h"
  37. #include "libavutil/time.h"
  38. #include "libavutil/time_internal.h"
  39. #include "libavutil/timestamp.h"
  40. #define MAX_FILENAME_SIZE 1024
  41. typedef struct WebMChunkContext {
  42. const AVClass *class;
  43. int chunk_start_index;
  44. char *header_filename;
  45. int chunk_duration;
  46. int chunk_index;
  47. uint64_t duration_written;
  48. int prev_pts;
  49. AVOutputFormat *oformat;
  50. AVFormatContext *avf;
  51. } WebMChunkContext;
  52. static int chunk_mux_init(AVFormatContext *s)
  53. {
  54. WebMChunkContext *wc = s->priv_data;
  55. AVFormatContext *oc;
  56. int ret;
  57. ret = avformat_alloc_output_context2(&wc->avf, wc->oformat, NULL, NULL);
  58. if (ret < 0)
  59. return ret;
  60. oc = wc->avf;
  61. oc->interrupt_callback = s->interrupt_callback;
  62. oc->max_delay = s->max_delay;
  63. av_dict_copy(&oc->metadata, s->metadata, 0);
  64. *(const AVClass**)oc->priv_data = oc->oformat->priv_class;
  65. av_opt_set_defaults(oc->priv_data);
  66. av_opt_set_int(oc->priv_data, "dash", 1, 0);
  67. av_opt_set_int(oc->priv_data, "cluster_time_limit", wc->chunk_duration, 0);
  68. av_opt_set_int(oc->priv_data, "live", 1, 0);
  69. oc->streams = s->streams;
  70. oc->nb_streams = s->nb_streams;
  71. return 0;
  72. }
  73. static int get_chunk_filename(AVFormatContext *s, int is_header, char *filename)
  74. {
  75. WebMChunkContext *wc = s->priv_data;
  76. AVFormatContext *oc = wc->avf;
  77. if (!filename) {
  78. return AVERROR(EINVAL);
  79. }
  80. if (is_header) {
  81. if (!wc->header_filename) {
  82. av_log(oc, AV_LOG_ERROR, "No header filename provided\n");
  83. return AVERROR(EINVAL);
  84. }
  85. av_strlcpy(filename, wc->header_filename, strlen(wc->header_filename) + 1);
  86. } else {
  87. if (av_get_frame_filename(filename, MAX_FILENAME_SIZE,
  88. s->filename, wc->chunk_index - 1) < 0) {
  89. av_log(oc, AV_LOG_ERROR, "Invalid chunk filename template '%s'\n", s->filename);
  90. return AVERROR(EINVAL);
  91. }
  92. }
  93. return 0;
  94. }
  95. static int webm_chunk_write_header(AVFormatContext *s)
  96. {
  97. WebMChunkContext *wc = s->priv_data;
  98. AVFormatContext *oc = NULL;
  99. int ret;
  100. int i;
  101. // DASH Streams can only have either one track per file.
  102. if (s->nb_streams != 1) { return AVERROR_INVALIDDATA; }
  103. wc->chunk_index = wc->chunk_start_index;
  104. wc->oformat = av_guess_format("webm", s->filename, "video/webm");
  105. if (!wc->oformat)
  106. return AVERROR_MUXER_NOT_FOUND;
  107. ret = chunk_mux_init(s);
  108. if (ret < 0)
  109. return ret;
  110. oc = wc->avf;
  111. ret = get_chunk_filename(s, 1, oc->filename);
  112. if (ret < 0)
  113. return ret;
  114. ret = s->io_open(s, &oc->pb, oc->filename, AVIO_FLAG_WRITE, NULL);
  115. if (ret < 0)
  116. return ret;
  117. oc->pb->seekable = 0;
  118. ret = oc->oformat->write_header(oc);
  119. if (ret < 0)
  120. return ret;
  121. ff_format_io_close(s, &oc->pb);
  122. for (i = 0; i < s->nb_streams; i++) {
  123. // ms precision is the de-facto standard timescale for mkv files.
  124. avpriv_set_pts_info(s->streams[i], 64, 1, 1000);
  125. }
  126. return 0;
  127. }
  128. static int chunk_start(AVFormatContext *s)
  129. {
  130. WebMChunkContext *wc = s->priv_data;
  131. AVFormatContext *oc = wc->avf;
  132. int ret;
  133. ret = avio_open_dyn_buf(&oc->pb);
  134. if (ret < 0)
  135. return ret;
  136. wc->chunk_index++;
  137. return 0;
  138. }
  139. static int chunk_end(AVFormatContext *s)
  140. {
  141. WebMChunkContext *wc = s->priv_data;
  142. AVFormatContext *oc = wc->avf;
  143. int ret;
  144. int buffer_size;
  145. uint8_t *buffer;
  146. AVIOContext *pb;
  147. char filename[MAX_FILENAME_SIZE];
  148. if (wc->chunk_start_index == wc->chunk_index)
  149. return 0;
  150. // Flush the cluster in WebM muxer.
  151. oc->oformat->write_packet(oc, NULL);
  152. buffer_size = avio_close_dyn_buf(oc->pb, &buffer);
  153. ret = get_chunk_filename(s, 0, filename);
  154. if (ret < 0)
  155. goto fail;
  156. ret = s->io_open(s, &pb, filename, AVIO_FLAG_WRITE, NULL);
  157. if (ret < 0)
  158. goto fail;
  159. avio_write(pb, buffer, buffer_size);
  160. ff_format_io_close(s, &pb);
  161. oc->pb = NULL;
  162. fail:
  163. av_free(buffer);
  164. return (ret < 0) ? ret : 0;
  165. }
  166. static int webm_chunk_write_packet(AVFormatContext *s, AVPacket *pkt)
  167. {
  168. WebMChunkContext *wc = s->priv_data;
  169. AVFormatContext *oc = wc->avf;
  170. AVStream *st = s->streams[pkt->stream_index];
  171. int ret;
  172. if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
  173. wc->duration_written += av_rescale_q(pkt->pts - wc->prev_pts,
  174. st->time_base,
  175. (AVRational) {1, 1000});
  176. wc->prev_pts = pkt->pts;
  177. }
  178. // For video, a new chunk is started only on key frames. For audio, a new
  179. // chunk is started based on chunk_duration.
  180. if ((st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
  181. (pkt->flags & AV_PKT_FLAG_KEY)) ||
  182. (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
  183. (pkt->pts == 0 || wc->duration_written >= wc->chunk_duration))) {
  184. wc->duration_written = 0;
  185. if ((ret = chunk_end(s)) < 0 || (ret = chunk_start(s)) < 0) {
  186. goto fail;
  187. }
  188. }
  189. ret = oc->oformat->write_packet(oc, pkt);
  190. if (ret < 0)
  191. goto fail;
  192. fail:
  193. if (ret < 0) {
  194. oc->streams = NULL;
  195. oc->nb_streams = 0;
  196. avformat_free_context(oc);
  197. }
  198. return ret;
  199. }
  200. static int webm_chunk_write_trailer(AVFormatContext *s)
  201. {
  202. WebMChunkContext *wc = s->priv_data;
  203. AVFormatContext *oc = wc->avf;
  204. oc->oformat->write_trailer(oc);
  205. chunk_end(s);
  206. oc->streams = NULL;
  207. oc->nb_streams = 0;
  208. avformat_free_context(oc);
  209. return 0;
  210. }
  211. #define OFFSET(x) offsetof(WebMChunkContext, x)
  212. static const AVOption options[] = {
  213. { "chunk_start_index", "start index of the chunk", OFFSET(chunk_start_index), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
  214. { "header", "filename of the header where the initialization data will be written", OFFSET(header_filename), AV_OPT_TYPE_STRING, { 0 }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
  215. { "audio_chunk_duration", "duration of each chunk in milliseconds", OFFSET(chunk_duration), AV_OPT_TYPE_INT, {.i64 = 5000}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
  216. { NULL },
  217. };
  218. #if CONFIG_WEBM_CHUNK_MUXER
  219. static const AVClass webm_chunk_class = {
  220. .class_name = "WebM Chunk Muxer",
  221. .item_name = av_default_item_name,
  222. .option = options,
  223. .version = LIBAVUTIL_VERSION_INT,
  224. };
  225. AVOutputFormat ff_webm_chunk_muxer = {
  226. .name = "webm_chunk",
  227. .long_name = NULL_IF_CONFIG_SMALL("WebM Chunk Muxer"),
  228. .mime_type = "video/webm",
  229. .extensions = "chk",
  230. .flags = AVFMT_NOFILE | AVFMT_GLOBALHEADER | AVFMT_NEEDNUMBER |
  231. AVFMT_TS_NONSTRICT,
  232. .priv_data_size = sizeof(WebMChunkContext),
  233. .write_header = webm_chunk_write_header,
  234. .write_packet = webm_chunk_write_packet,
  235. .write_trailer = webm_chunk_write_trailer,
  236. .priv_class = &webm_chunk_class,
  237. };
  238. #endif