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.

1131 lines
41KB

  1. /*
  2. * MPEG-DASH ISO BMFF segmenter
  3. * Copyright (c) 2014 Martin Storsjo
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "config.h"
  22. #if HAVE_UNISTD_H
  23. #include <unistd.h>
  24. #endif
  25. #include "libavutil/avassert.h"
  26. #include "libavutil/avutil.h"
  27. #include "libavutil/avstring.h"
  28. #include "libavutil/intreadwrite.h"
  29. #include "libavutil/mathematics.h"
  30. #include "libavutil/opt.h"
  31. #include "libavutil/rational.h"
  32. #include "libavutil/time_internal.h"
  33. #include "avc.h"
  34. #include "avformat.h"
  35. #include "avio_internal.h"
  36. #include "internal.h"
  37. #include "isom.h"
  38. #include "os_support.h"
  39. #include "url.h"
  40. #include "dash.h"
  41. typedef struct Segment {
  42. char file[1024];
  43. int64_t start_pos;
  44. int range_length, index_length;
  45. int64_t time;
  46. int duration;
  47. int n;
  48. } Segment;
  49. typedef struct AdaptationSet {
  50. char id[10];
  51. enum AVMediaType media_type;
  52. } AdaptationSet;
  53. typedef struct OutputStream {
  54. AVFormatContext *ctx;
  55. int ctx_inited, as_idx;
  56. uint8_t iobuf[32768];
  57. AVIOContext *out;
  58. int packets_written;
  59. char initfile[1024];
  60. int64_t init_start_pos;
  61. int init_range_length;
  62. int nb_segments, segments_size, segment_index;
  63. Segment **segments;
  64. int64_t first_pts, start_pts, max_pts;
  65. int64_t last_dts;
  66. int bit_rate;
  67. char bandwidth_str[64];
  68. char codec_str[100];
  69. } OutputStream;
  70. typedef struct DASHContext {
  71. const AVClass *class; /* Class for private options. */
  72. char *adaptation_sets;
  73. AdaptationSet *as;
  74. int nb_as;
  75. int window_size;
  76. int extra_window_size;
  77. int min_seg_duration;
  78. int remove_at_exit;
  79. int use_template;
  80. int use_timeline;
  81. int single_file;
  82. OutputStream *streams;
  83. int has_video;
  84. int64_t last_duration;
  85. int64_t total_duration;
  86. char availability_start_time[100];
  87. char dirname[1024];
  88. const char *single_file_name;
  89. const char *init_seg_name;
  90. const char *media_seg_name;
  91. AVRational min_frame_rate, max_frame_rate;
  92. int ambiguous_frame_rate;
  93. const char *utc_timing_url;
  94. } DASHContext;
  95. static int dash_write(void *opaque, uint8_t *buf, int buf_size)
  96. {
  97. OutputStream *os = opaque;
  98. if (os->out)
  99. avio_write(os->out, buf, buf_size);
  100. return buf_size;
  101. }
  102. // RFC 6381
  103. static void set_codec_str(AVFormatContext *s, AVCodecParameters *par,
  104. char *str, int size)
  105. {
  106. const AVCodecTag *tags[2] = { NULL, NULL };
  107. uint32_t tag;
  108. if (par->codec_type == AVMEDIA_TYPE_VIDEO)
  109. tags[0] = ff_codec_movvideo_tags;
  110. else if (par->codec_type == AVMEDIA_TYPE_AUDIO)
  111. tags[0] = ff_codec_movaudio_tags;
  112. else
  113. return;
  114. tag = av_codec_get_tag(tags, par->codec_id);
  115. if (!tag)
  116. return;
  117. if (size < 5)
  118. return;
  119. AV_WL32(str, tag);
  120. str[4] = '\0';
  121. if (!strcmp(str, "mp4a") || !strcmp(str, "mp4v")) {
  122. uint32_t oti;
  123. tags[0] = ff_mp4_obj_type;
  124. oti = av_codec_get_tag(tags, par->codec_id);
  125. if (oti)
  126. av_strlcatf(str, size, ".%02"PRIx32, oti);
  127. else
  128. return;
  129. if (tag == MKTAG('m', 'p', '4', 'a')) {
  130. if (par->extradata_size >= 2) {
  131. int aot = par->extradata[0] >> 3;
  132. if (aot == 31)
  133. aot = ((AV_RB16(par->extradata) >> 5) & 0x3f) + 32;
  134. av_strlcatf(str, size, ".%d", aot);
  135. }
  136. } else if (tag == MKTAG('m', 'p', '4', 'v')) {
  137. // Unimplemented, should output ProfileLevelIndication as a decimal number
  138. av_log(s, AV_LOG_WARNING, "Incomplete RFC 6381 codec string for mp4v\n");
  139. }
  140. } else if (!strcmp(str, "avc1")) {
  141. uint8_t *tmpbuf = NULL;
  142. uint8_t *extradata = par->extradata;
  143. int extradata_size = par->extradata_size;
  144. if (!extradata_size)
  145. return;
  146. if (extradata[0] != 1) {
  147. AVIOContext *pb;
  148. if (avio_open_dyn_buf(&pb) < 0)
  149. return;
  150. if (ff_isom_write_avcc(pb, extradata, extradata_size) < 0) {
  151. ffio_free_dyn_buf(&pb);
  152. return;
  153. }
  154. extradata_size = avio_close_dyn_buf(pb, &extradata);
  155. tmpbuf = extradata;
  156. }
  157. if (extradata_size >= 4)
  158. av_strlcatf(str, size, ".%02x%02x%02x",
  159. extradata[1], extradata[2], extradata[3]);
  160. av_free(tmpbuf);
  161. }
  162. }
  163. static void dash_free(AVFormatContext *s)
  164. {
  165. DASHContext *c = s->priv_data;
  166. int i, j;
  167. if (c->as) {
  168. av_freep(&c->as);
  169. c->nb_as = 0;
  170. }
  171. if (!c->streams)
  172. return;
  173. for (i = 0; i < s->nb_streams; i++) {
  174. OutputStream *os = &c->streams[i];
  175. if (os->ctx && os->ctx_inited)
  176. av_write_trailer(os->ctx);
  177. if (os->ctx && os->ctx->pb)
  178. av_free(os->ctx->pb);
  179. ff_format_io_close(s, &os->out);
  180. if (os->ctx)
  181. avformat_free_context(os->ctx);
  182. for (j = 0; j < os->nb_segments; j++)
  183. av_free(os->segments[j]);
  184. av_free(os->segments);
  185. }
  186. av_freep(&c->streams);
  187. }
  188. static void output_segment_list(OutputStream *os, AVIOContext *out, DASHContext *c)
  189. {
  190. int i, start_index = 0, start_number = 1;
  191. if (c->window_size) {
  192. start_index = FFMAX(os->nb_segments - c->window_size, 0);
  193. start_number = FFMAX(os->segment_index - c->window_size, 1);
  194. }
  195. if (c->use_template) {
  196. int timescale = c->use_timeline ? os->ctx->streams[0]->time_base.den : AV_TIME_BASE;
  197. avio_printf(out, "\t\t\t\t<SegmentTemplate timescale=\"%d\" ", timescale);
  198. if (!c->use_timeline)
  199. avio_printf(out, "duration=\"%"PRId64"\" ", c->last_duration);
  200. avio_printf(out, "initialization=\"%s\" media=\"%s\" startNumber=\"%d\">\n", c->init_seg_name, c->media_seg_name, c->use_timeline ? start_number : 1);
  201. if (c->use_timeline) {
  202. int64_t cur_time = 0;
  203. avio_printf(out, "\t\t\t\t\t<SegmentTimeline>\n");
  204. for (i = start_index; i < os->nb_segments; ) {
  205. Segment *seg = os->segments[i];
  206. int repeat = 0;
  207. avio_printf(out, "\t\t\t\t\t\t<S ");
  208. if (i == start_index || seg->time != cur_time) {
  209. cur_time = seg->time;
  210. avio_printf(out, "t=\"%"PRId64"\" ", seg->time);
  211. }
  212. avio_printf(out, "d=\"%d\" ", seg->duration);
  213. while (i + repeat + 1 < os->nb_segments &&
  214. os->segments[i + repeat + 1]->duration == seg->duration &&
  215. os->segments[i + repeat + 1]->time == os->segments[i + repeat]->time + os->segments[i + repeat]->duration)
  216. repeat++;
  217. if (repeat > 0)
  218. avio_printf(out, "r=\"%d\" ", repeat);
  219. avio_printf(out, "/>\n");
  220. i += 1 + repeat;
  221. cur_time += (1 + repeat) * seg->duration;
  222. }
  223. avio_printf(out, "\t\t\t\t\t</SegmentTimeline>\n");
  224. }
  225. avio_printf(out, "\t\t\t\t</SegmentTemplate>\n");
  226. } else if (c->single_file) {
  227. avio_printf(out, "\t\t\t\t<BaseURL>%s</BaseURL>\n", os->initfile);
  228. avio_printf(out, "\t\t\t\t<SegmentList timescale=\"%d\" duration=\"%"PRId64"\" startNumber=\"%d\">\n", AV_TIME_BASE, c->last_duration, start_number);
  229. avio_printf(out, "\t\t\t\t\t<Initialization range=\"%"PRId64"-%"PRId64"\" />\n", os->init_start_pos, os->init_start_pos + os->init_range_length - 1);
  230. for (i = start_index; i < os->nb_segments; i++) {
  231. Segment *seg = os->segments[i];
  232. avio_printf(out, "\t\t\t\t\t<SegmentURL mediaRange=\"%"PRId64"-%"PRId64"\" ", seg->start_pos, seg->start_pos + seg->range_length - 1);
  233. if (seg->index_length)
  234. avio_printf(out, "indexRange=\"%"PRId64"-%"PRId64"\" ", seg->start_pos, seg->start_pos + seg->index_length - 1);
  235. avio_printf(out, "/>\n");
  236. }
  237. avio_printf(out, "\t\t\t\t</SegmentList>\n");
  238. } else {
  239. avio_printf(out, "\t\t\t\t<SegmentList timescale=\"%d\" duration=\"%"PRId64"\" startNumber=\"%d\">\n", AV_TIME_BASE, c->last_duration, start_number);
  240. avio_printf(out, "\t\t\t\t\t<Initialization sourceURL=\"%s\" />\n", os->initfile);
  241. for (i = start_index; i < os->nb_segments; i++) {
  242. Segment *seg = os->segments[i];
  243. avio_printf(out, "\t\t\t\t\t<SegmentURL media=\"%s\" />\n", seg->file);
  244. }
  245. avio_printf(out, "\t\t\t\t</SegmentList>\n");
  246. }
  247. }
  248. static char *xmlescape(const char *str) {
  249. int outlen = strlen(str)*3/2 + 6;
  250. char *out = av_realloc(NULL, outlen + 1);
  251. int pos = 0;
  252. if (!out)
  253. return NULL;
  254. for (; *str; str++) {
  255. if (pos + 6 > outlen) {
  256. char *tmp;
  257. outlen = 2 * outlen + 6;
  258. tmp = av_realloc(out, outlen + 1);
  259. if (!tmp) {
  260. av_free(out);
  261. return NULL;
  262. }
  263. out = tmp;
  264. }
  265. if (*str == '&') {
  266. memcpy(&out[pos], "&amp;", 5);
  267. pos += 5;
  268. } else if (*str == '<') {
  269. memcpy(&out[pos], "&lt;", 4);
  270. pos += 4;
  271. } else if (*str == '>') {
  272. memcpy(&out[pos], "&gt;", 4);
  273. pos += 4;
  274. } else if (*str == '\'') {
  275. memcpy(&out[pos], "&apos;", 6);
  276. pos += 6;
  277. } else if (*str == '\"') {
  278. memcpy(&out[pos], "&quot;", 6);
  279. pos += 6;
  280. } else {
  281. out[pos++] = *str;
  282. }
  283. }
  284. out[pos] = '\0';
  285. return out;
  286. }
  287. static void write_time(AVIOContext *out, int64_t time)
  288. {
  289. int seconds = time / AV_TIME_BASE;
  290. int fractions = time % AV_TIME_BASE;
  291. int minutes = seconds / 60;
  292. int hours = minutes / 60;
  293. seconds %= 60;
  294. minutes %= 60;
  295. avio_printf(out, "PT");
  296. if (hours)
  297. avio_printf(out, "%dH", hours);
  298. if (hours || minutes)
  299. avio_printf(out, "%dM", minutes);
  300. avio_printf(out, "%d.%dS", seconds, fractions / (AV_TIME_BASE / 10));
  301. }
  302. static void format_date_now(char *buf, int size)
  303. {
  304. time_t t = time(NULL);
  305. struct tm *ptm, tmbuf;
  306. ptm = gmtime_r(&t, &tmbuf);
  307. if (ptm) {
  308. if (!strftime(buf, size, "%Y-%m-%dT%H:%M:%SZ", ptm))
  309. buf[0] = '\0';
  310. }
  311. }
  312. static int write_adaptation_set(AVFormatContext *s, AVIOContext *out, int as_index)
  313. {
  314. DASHContext *c = s->priv_data;
  315. AdaptationSet *as = &c->as[as_index];
  316. int i;
  317. avio_printf(out, "\t\t<AdaptationSet id=\"%s\" contentType=\"%s\" segmentAlignment=\"true\" bitstreamSwitching=\"true\"",
  318. as->id, as->media_type == AVMEDIA_TYPE_VIDEO ? "video" : "audio");
  319. if (as->media_type == AVMEDIA_TYPE_VIDEO && c->max_frame_rate.num && !c->ambiguous_frame_rate)
  320. avio_printf(out, " %s=\"%d/%d\"", (av_cmp_q(c->min_frame_rate, c->max_frame_rate) < 0) ? "maxFrameRate" : "frameRate", c->max_frame_rate.num, c->max_frame_rate.den);
  321. avio_printf(out, ">\n");
  322. for (i = 0; i < s->nb_streams; i++) {
  323. OutputStream *os = &c->streams[i];
  324. if (os->as_idx - 1 != as_index)
  325. continue;
  326. if (as->media_type == AVMEDIA_TYPE_VIDEO) {
  327. AVStream *st = s->streams[i];
  328. avio_printf(out, "\t\t\t<Representation id=\"%d\" mimeType=\"video/mp4\" codecs=\"%s\"%s width=\"%d\" height=\"%d\"",
  329. i, os->codec_str, os->bandwidth_str, s->streams[i]->codecpar->width, s->streams[i]->codecpar->height);
  330. if (st->avg_frame_rate.num)
  331. avio_printf(out, " frameRate=\"%d/%d\"", st->avg_frame_rate.num, st->avg_frame_rate.den);
  332. avio_printf(out, ">\n");
  333. } else {
  334. avio_printf(out, "\t\t\t<Representation id=\"%d\" mimeType=\"audio/mp4\" codecs=\"%s\"%s audioSamplingRate=\"%d\">\n",
  335. i, os->codec_str, os->bandwidth_str, s->streams[i]->codecpar->sample_rate);
  336. avio_printf(out, "\t\t\t\t<AudioChannelConfiguration schemeIdUri=\"urn:mpeg:dash:23003:3:audio_channel_configuration:2011\" value=\"%d\" />\n",
  337. s->streams[i]->codecpar->channels);
  338. }
  339. output_segment_list(os, out, c);
  340. avio_printf(out, "\t\t\t</Representation>\n");
  341. }
  342. avio_printf(out, "\t\t</AdaptationSet>\n");
  343. return 0;
  344. }
  345. static int add_adaptation_set(AVFormatContext *s, AdaptationSet **as, enum AVMediaType type)
  346. {
  347. DASHContext *c = s->priv_data;
  348. void *mem = av_realloc(c->as, sizeof(*c->as) * (c->nb_as + 1));
  349. if (!mem)
  350. return AVERROR(ENOMEM);
  351. c->as = mem;
  352. ++c->nb_as;
  353. *as = &c->as[c->nb_as - 1];
  354. memset(*as, 0, sizeof(**as));
  355. (*as)->media_type = type;
  356. return 0;
  357. }
  358. static int adaptation_set_add_stream(AVFormatContext *s, int as_idx, int i)
  359. {
  360. DASHContext *c = s->priv_data;
  361. AdaptationSet *as = &c->as[as_idx - 1];
  362. OutputStream *os = &c->streams[i];
  363. if (as->media_type != s->streams[i]->codecpar->codec_type) {
  364. av_log(s, AV_LOG_ERROR, "Codec type of stream %d doesn't match AdaptationSet's media type\n", i);
  365. return AVERROR(EINVAL);
  366. } else if (os->as_idx) {
  367. av_log(s, AV_LOG_ERROR, "Stream %d is already assigned to an AdaptationSet\n", i);
  368. return AVERROR(EINVAL);
  369. }
  370. os->as_idx = as_idx;
  371. return 0;
  372. }
  373. static int parse_adaptation_sets(AVFormatContext *s)
  374. {
  375. DASHContext *c = s->priv_data;
  376. const char *p = c->adaptation_sets;
  377. enum { new_set, parse_id, parsing_streams } state;
  378. AdaptationSet *as;
  379. int i, n, ret;
  380. enum AVMediaType types[] = { AVMEDIA_TYPE_VIDEO, AVMEDIA_TYPE_AUDIO, AVMEDIA_TYPE_UNKNOWN };
  381. // default: one AdaptationSet for each media type
  382. if (!p) {
  383. for (n = 0; types[n] != AVMEDIA_TYPE_UNKNOWN; n++) {
  384. int as_idx = 0;
  385. for (i = 0; i < s->nb_streams; i++) {
  386. if (s->streams[i]->codecpar->codec_type != types[n])
  387. continue;
  388. if (!as_idx) {
  389. if ((ret = add_adaptation_set(s, &as, types[n])) < 0)
  390. return ret;
  391. as_idx = c->nb_as;
  392. snprintf(as->id, sizeof(as->id), "%d", i);
  393. }
  394. c->streams[i].as_idx = as_idx;
  395. }
  396. }
  397. goto end;
  398. }
  399. // syntax id=0,streams=0,1,2 id=1,streams=3,4 and so on
  400. state = new_set;
  401. while (*p) {
  402. if (*p == ' ') {
  403. p++;
  404. continue;
  405. } else if (state == new_set && av_strstart(p, "id=", &p)) {
  406. if ((ret = add_adaptation_set(s, &as, AVMEDIA_TYPE_UNKNOWN)) < 0)
  407. return ret;
  408. n = strcspn(p, ",");
  409. snprintf(as->id, sizeof(as->id), "%.*s", n, p);
  410. p += n;
  411. if (*p)
  412. p++;
  413. state = parse_id;
  414. } else if (state == parse_id && av_strstart(p, "streams=", &p)) {
  415. state = parsing_streams;
  416. } else if (state == parsing_streams) {
  417. AdaptationSet *as = &c->as[c->nb_as - 1];
  418. char idx_str[8], *end_str;
  419. n = strcspn(p, " ,");
  420. snprintf(idx_str, sizeof(idx_str), "%.*s", n, p);
  421. p += n;
  422. // if value is "a" or "v", map all streams of that type
  423. if (as->media_type == AVMEDIA_TYPE_UNKNOWN && (idx_str[0] == 'v' || idx_str[0] == 'a')) {
  424. enum AVMediaType type = (idx_str[0] == 'v') ? AVMEDIA_TYPE_VIDEO : AVMEDIA_TYPE_AUDIO;
  425. av_log(s, AV_LOG_DEBUG, "Map all streams of type %s\n", idx_str);
  426. for (i = 0; i < s->nb_streams; i++) {
  427. if (s->streams[i]->codecpar->codec_type != type)
  428. continue;
  429. as->media_type = s->streams[i]->codecpar->codec_type;
  430. if ((ret = adaptation_set_add_stream(s, c->nb_as, i)) < 0)
  431. return ret;
  432. }
  433. } else { // select single stream
  434. i = strtol(idx_str, &end_str, 10);
  435. if (idx_str == end_str || i < 0 || i >= s->nb_streams) {
  436. av_log(s, AV_LOG_ERROR, "Selected stream \"%s\" not found!\n", idx_str);
  437. return AVERROR(EINVAL);
  438. }
  439. av_log(s, AV_LOG_DEBUG, "Map stream %d\n", i);
  440. if (as->media_type == AVMEDIA_TYPE_UNKNOWN) {
  441. as->media_type = s->streams[i]->codecpar->codec_type;
  442. }
  443. if ((ret = adaptation_set_add_stream(s, c->nb_as, i)) < 0)
  444. return ret;
  445. }
  446. if (*p == ' ')
  447. state = new_set;
  448. if (*p)
  449. p++;
  450. } else {
  451. return AVERROR(EINVAL);
  452. }
  453. }
  454. end:
  455. // check for unassigned streams
  456. for (i = 0; i < s->nb_streams; i++) {
  457. OutputStream *os = &c->streams[i];
  458. if (!os->as_idx) {
  459. av_log(s, AV_LOG_ERROR, "Stream %d is not mapped to an AdaptationSet\n", i);
  460. return AVERROR(EINVAL);
  461. }
  462. }
  463. return 0;
  464. }
  465. static int write_manifest(AVFormatContext *s, int final)
  466. {
  467. DASHContext *c = s->priv_data;
  468. AVIOContext *out;
  469. char temp_filename[1024];
  470. int ret, i;
  471. const char *proto = avio_find_protocol_name(s->filename);
  472. int use_rename = proto && !strcmp(proto, "file");
  473. static unsigned int warned_non_file = 0;
  474. AVDictionaryEntry *title = av_dict_get(s->metadata, "title", NULL, 0);
  475. if (!use_rename && !warned_non_file++)
  476. av_log(s, AV_LOG_ERROR, "Cannot use rename on non file protocol, this may lead to races and temporary partial files\n");
  477. snprintf(temp_filename, sizeof(temp_filename), use_rename ? "%s.tmp" : "%s", s->filename);
  478. ret = s->io_open(s, &out, temp_filename, AVIO_FLAG_WRITE, NULL);
  479. if (ret < 0) {
  480. av_log(s, AV_LOG_ERROR, "Unable to open %s for writing\n", temp_filename);
  481. return ret;
  482. }
  483. avio_printf(out, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
  484. avio_printf(out, "<MPD xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
  485. "\txmlns=\"urn:mpeg:dash:schema:mpd:2011\"\n"
  486. "\txmlns:xlink=\"http://www.w3.org/1999/xlink\"\n"
  487. "\txsi:schemaLocation=\"urn:mpeg:DASH:schema:MPD:2011 http://standards.iso.org/ittf/PubliclyAvailableStandards/MPEG-DASH_schema_files/DASH-MPD.xsd\"\n"
  488. "\tprofiles=\"urn:mpeg:dash:profile:isoff-live:2011\"\n"
  489. "\ttype=\"%s\"\n", final ? "static" : "dynamic");
  490. if (final) {
  491. avio_printf(out, "\tmediaPresentationDuration=\"");
  492. write_time(out, c->total_duration);
  493. avio_printf(out, "\"\n");
  494. } else {
  495. int64_t update_period = c->last_duration / AV_TIME_BASE;
  496. char now_str[100];
  497. if (c->use_template && !c->use_timeline)
  498. update_period = 500;
  499. avio_printf(out, "\tminimumUpdatePeriod=\"PT%"PRId64"S\"\n", update_period);
  500. avio_printf(out, "\tsuggestedPresentationDelay=\"PT%"PRId64"S\"\n", c->last_duration / AV_TIME_BASE);
  501. if (!c->availability_start_time[0] && s->nb_streams > 0 && c->streams[0].nb_segments > 0) {
  502. format_date_now(c->availability_start_time, sizeof(c->availability_start_time));
  503. }
  504. if (c->availability_start_time[0])
  505. avio_printf(out, "\tavailabilityStartTime=\"%s\"\n", c->availability_start_time);
  506. format_date_now(now_str, sizeof(now_str));
  507. if (now_str[0])
  508. avio_printf(out, "\tpublishTime=\"%s\"\n", now_str);
  509. if (c->window_size && c->use_template) {
  510. avio_printf(out, "\ttimeShiftBufferDepth=\"");
  511. write_time(out, c->last_duration * c->window_size);
  512. avio_printf(out, "\"\n");
  513. }
  514. }
  515. avio_printf(out, "\tminBufferTime=\"");
  516. write_time(out, c->last_duration * 2);
  517. avio_printf(out, "\">\n");
  518. avio_printf(out, "\t<ProgramInformation>\n");
  519. if (title) {
  520. char *escaped = xmlescape(title->value);
  521. avio_printf(out, "\t\t<Title>%s</Title>\n", escaped);
  522. av_free(escaped);
  523. }
  524. avio_printf(out, "\t</ProgramInformation>\n");
  525. if (c->utc_timing_url)
  526. avio_printf(out, "\t<UTCTiming schemeIdUri=\"urn:mpeg:dash:utc:http-xsdate:2014\" value=\"%s\"/>\n", c->utc_timing_url);
  527. if (c->window_size && s->nb_streams > 0 && c->streams[0].nb_segments > 0 && !c->use_template) {
  528. OutputStream *os = &c->streams[0];
  529. int start_index = FFMAX(os->nb_segments - c->window_size, 0);
  530. int64_t start_time = av_rescale_q(os->segments[start_index]->time, s->streams[0]->time_base, AV_TIME_BASE_Q);
  531. avio_printf(out, "\t<Period id=\"0\" start=\"");
  532. write_time(out, start_time);
  533. avio_printf(out, "\">\n");
  534. } else {
  535. avio_printf(out, "\t<Period id=\"0\" start=\"PT0.0S\">\n");
  536. }
  537. for (i = 0; i < c->nb_as; i++) {
  538. if ((ret = write_adaptation_set(s, out, i)) < 0)
  539. return ret;
  540. }
  541. avio_printf(out, "\t</Period>\n");
  542. avio_printf(out, "</MPD>\n");
  543. avio_flush(out);
  544. ff_format_io_close(s, &out);
  545. if (use_rename)
  546. return avpriv_io_move(temp_filename, s->filename);
  547. return 0;
  548. }
  549. static int dash_init(AVFormatContext *s)
  550. {
  551. DASHContext *c = s->priv_data;
  552. int ret = 0, i;
  553. AVOutputFormat *oformat;
  554. char *ptr;
  555. char basename[1024];
  556. if (c->single_file_name)
  557. c->single_file = 1;
  558. if (c->single_file)
  559. c->use_template = 0;
  560. c->ambiguous_frame_rate = 0;
  561. av_strlcpy(c->dirname, s->filename, sizeof(c->dirname));
  562. ptr = strrchr(c->dirname, '/');
  563. if (ptr) {
  564. av_strlcpy(basename, &ptr[1], sizeof(basename));
  565. ptr[1] = '\0';
  566. } else {
  567. c->dirname[0] = '\0';
  568. av_strlcpy(basename, s->filename, sizeof(basename));
  569. }
  570. ptr = strrchr(basename, '.');
  571. if (ptr)
  572. *ptr = '\0';
  573. oformat = av_guess_format("mp4", NULL, NULL);
  574. if (!oformat)
  575. return AVERROR_MUXER_NOT_FOUND;
  576. c->streams = av_mallocz(sizeof(*c->streams) * s->nb_streams);
  577. if (!c->streams)
  578. return AVERROR(ENOMEM);
  579. if ((ret = parse_adaptation_sets(s)) < 0)
  580. return ret;
  581. for (i = 0; i < s->nb_streams; i++) {
  582. OutputStream *os = &c->streams[i];
  583. AVFormatContext *ctx;
  584. AVStream *st;
  585. AVDictionary *opts = NULL;
  586. char filename[1024];
  587. os->bit_rate = s->streams[i]->codecpar->bit_rate;
  588. if (os->bit_rate) {
  589. snprintf(os->bandwidth_str, sizeof(os->bandwidth_str),
  590. " bandwidth=\"%d\"", os->bit_rate);
  591. } else {
  592. int level = s->strict_std_compliance >= FF_COMPLIANCE_STRICT ?
  593. AV_LOG_ERROR : AV_LOG_WARNING;
  594. av_log(s, level, "No bit rate set for stream %d\n", i);
  595. if (s->strict_std_compliance >= FF_COMPLIANCE_STRICT)
  596. return AVERROR(EINVAL);
  597. }
  598. ctx = avformat_alloc_context();
  599. if (!ctx)
  600. return AVERROR(ENOMEM);
  601. os->ctx = ctx;
  602. ctx->oformat = oformat;
  603. ctx->interrupt_callback = s->interrupt_callback;
  604. ctx->opaque = s->opaque;
  605. ctx->io_close = s->io_close;
  606. ctx->io_open = s->io_open;
  607. if (!(st = avformat_new_stream(ctx, NULL)))
  608. return AVERROR(ENOMEM);
  609. avcodec_parameters_copy(st->codecpar, s->streams[i]->codecpar);
  610. st->sample_aspect_ratio = s->streams[i]->sample_aspect_ratio;
  611. st->time_base = s->streams[i]->time_base;
  612. ctx->avoid_negative_ts = s->avoid_negative_ts;
  613. ctx->flags = s->flags;
  614. ctx->pb = avio_alloc_context(os->iobuf, sizeof(os->iobuf), AVIO_FLAG_WRITE, os, NULL, dash_write, NULL);
  615. if (!ctx->pb)
  616. return AVERROR(ENOMEM);
  617. if (c->single_file) {
  618. if (c->single_file_name)
  619. ff_dash_fill_tmpl_params(os->initfile, sizeof(os->initfile), c->single_file_name, i, 0, os->bit_rate, 0);
  620. else
  621. snprintf(os->initfile, sizeof(os->initfile), "%s-stream%d.m4s", basename, i);
  622. } else {
  623. ff_dash_fill_tmpl_params(os->initfile, sizeof(os->initfile), c->init_seg_name, i, 0, os->bit_rate, 0);
  624. }
  625. snprintf(filename, sizeof(filename), "%s%s", c->dirname, os->initfile);
  626. ret = s->io_open(s, &os->out, filename, AVIO_FLAG_WRITE, NULL);
  627. if (ret < 0)
  628. return ret;
  629. os->init_start_pos = 0;
  630. av_dict_set(&opts, "movflags", "frag_custom+dash+delay_moov", 0);
  631. if ((ret = avformat_init_output(ctx, &opts)) < 0)
  632. return ret;
  633. os->ctx_inited = 1;
  634. avio_flush(ctx->pb);
  635. av_dict_free(&opts);
  636. av_log(s, AV_LOG_VERBOSE, "Representation %d init segment will be written to: %s\n", i, filename);
  637. s->streams[i]->time_base = st->time_base;
  638. // If the muxer wants to shift timestamps, request to have them shifted
  639. // already before being handed to this muxer, so we don't have mismatches
  640. // between the MPD and the actual segments.
  641. s->avoid_negative_ts = ctx->avoid_negative_ts;
  642. if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
  643. AVRational avg_frame_rate = s->streams[i]->avg_frame_rate;
  644. if (avg_frame_rate.num > 0) {
  645. if (av_cmp_q(avg_frame_rate, c->min_frame_rate) < 0)
  646. c->min_frame_rate = avg_frame_rate;
  647. if (av_cmp_q(c->max_frame_rate, avg_frame_rate) < 0)
  648. c->max_frame_rate = avg_frame_rate;
  649. } else {
  650. c->ambiguous_frame_rate = 1;
  651. }
  652. c->has_video = 1;
  653. }
  654. set_codec_str(s, st->codecpar, os->codec_str, sizeof(os->codec_str));
  655. os->first_pts = AV_NOPTS_VALUE;
  656. os->max_pts = AV_NOPTS_VALUE;
  657. os->last_dts = AV_NOPTS_VALUE;
  658. os->segment_index = 1;
  659. }
  660. if (!c->has_video && c->min_seg_duration <= 0) {
  661. av_log(s, AV_LOG_WARNING, "no video stream and no min seg duration set\n");
  662. return AVERROR(EINVAL);
  663. }
  664. return 0;
  665. }
  666. static int dash_write_header(AVFormatContext *s)
  667. {
  668. DASHContext *c = s->priv_data;
  669. int i, ret;
  670. for (i = 0; i < s->nb_streams; i++) {
  671. OutputStream *os = &c->streams[i];
  672. if ((ret = avformat_write_header(os->ctx, NULL)) < 0) {
  673. dash_free(s);
  674. return ret;
  675. }
  676. }
  677. ret = write_manifest(s, 0);
  678. if (!ret)
  679. av_log(s, AV_LOG_VERBOSE, "Manifest written to: %s\n", s->filename);
  680. return ret;
  681. }
  682. static int add_segment(OutputStream *os, const char *file,
  683. int64_t time, int duration,
  684. int64_t start_pos, int64_t range_length,
  685. int64_t index_length)
  686. {
  687. int err;
  688. Segment *seg;
  689. if (os->nb_segments >= os->segments_size) {
  690. os->segments_size = (os->segments_size + 1) * 2;
  691. if ((err = av_reallocp(&os->segments, sizeof(*os->segments) *
  692. os->segments_size)) < 0) {
  693. os->segments_size = 0;
  694. os->nb_segments = 0;
  695. return err;
  696. }
  697. }
  698. seg = av_mallocz(sizeof(*seg));
  699. if (!seg)
  700. return AVERROR(ENOMEM);
  701. av_strlcpy(seg->file, file, sizeof(seg->file));
  702. seg->time = time;
  703. seg->duration = duration;
  704. if (seg->time < 0) { // If pts<0, it is expected to be cut away with an edit list
  705. seg->duration += seg->time;
  706. seg->time = 0;
  707. }
  708. seg->start_pos = start_pos;
  709. seg->range_length = range_length;
  710. seg->index_length = index_length;
  711. os->segments[os->nb_segments++] = seg;
  712. os->segment_index++;
  713. return 0;
  714. }
  715. static void write_styp(AVIOContext *pb)
  716. {
  717. avio_wb32(pb, 24);
  718. ffio_wfourcc(pb, "styp");
  719. ffio_wfourcc(pb, "msdh");
  720. avio_wb32(pb, 0); /* minor */
  721. ffio_wfourcc(pb, "msdh");
  722. ffio_wfourcc(pb, "msix");
  723. }
  724. static void find_index_range(AVFormatContext *s, const char *full_path,
  725. int64_t pos, int *index_length)
  726. {
  727. uint8_t buf[8];
  728. AVIOContext *pb;
  729. int ret;
  730. ret = s->io_open(s, &pb, full_path, AVIO_FLAG_READ, NULL);
  731. if (ret < 0)
  732. return;
  733. if (avio_seek(pb, pos, SEEK_SET) != pos) {
  734. ff_format_io_close(s, &pb);
  735. return;
  736. }
  737. ret = avio_read(pb, buf, 8);
  738. ff_format_io_close(s, &pb);
  739. if (ret < 8)
  740. return;
  741. if (AV_RL32(&buf[4]) != MKTAG('s', 'i', 'd', 'x'))
  742. return;
  743. *index_length = AV_RB32(&buf[0]);
  744. }
  745. static int update_stream_extradata(AVFormatContext *s, OutputStream *os,
  746. AVCodecParameters *par)
  747. {
  748. uint8_t *extradata;
  749. if (os->ctx->streams[0]->codecpar->extradata_size || !par->extradata_size)
  750. return 0;
  751. extradata = av_malloc(par->extradata_size);
  752. if (!extradata)
  753. return AVERROR(ENOMEM);
  754. memcpy(extradata, par->extradata, par->extradata_size);
  755. os->ctx->streams[0]->codecpar->extradata = extradata;
  756. os->ctx->streams[0]->codecpar->extradata_size = par->extradata_size;
  757. set_codec_str(s, par, os->codec_str, sizeof(os->codec_str));
  758. return 0;
  759. }
  760. static int dash_flush(AVFormatContext *s, int final, int stream)
  761. {
  762. DASHContext *c = s->priv_data;
  763. int i, ret = 0;
  764. const char *proto = avio_find_protocol_name(s->filename);
  765. int use_rename = proto && !strcmp(proto, "file");
  766. int cur_flush_segment_index = 0;
  767. if (stream >= 0)
  768. cur_flush_segment_index = c->streams[stream].segment_index;
  769. for (i = 0; i < s->nb_streams; i++) {
  770. OutputStream *os = &c->streams[i];
  771. char filename[1024] = "", full_path[1024], temp_path[1024];
  772. int64_t start_pos;
  773. int range_length, index_length = 0;
  774. if (!os->packets_written)
  775. continue;
  776. // Flush the single stream that got a keyframe right now.
  777. // Flush all audio streams as well, in sync with video keyframes,
  778. // but not the other video streams.
  779. if (stream >= 0 && i != stream) {
  780. if (s->streams[i]->codecpar->codec_type != AVMEDIA_TYPE_AUDIO)
  781. continue;
  782. // Make sure we don't flush audio streams multiple times, when
  783. // all video streams are flushed one at a time.
  784. if (c->has_video && os->segment_index > cur_flush_segment_index)
  785. continue;
  786. }
  787. if (!os->init_range_length) {
  788. av_write_frame(os->ctx, NULL);
  789. os->init_range_length = avio_tell(os->ctx->pb);
  790. if (!c->single_file)
  791. ff_format_io_close(s, &os->out);
  792. }
  793. start_pos = avio_tell(os->ctx->pb);
  794. if (!c->single_file) {
  795. ff_dash_fill_tmpl_params(filename, sizeof(filename), c->media_seg_name, i, os->segment_index, os->bit_rate, os->start_pts);
  796. snprintf(full_path, sizeof(full_path), "%s%s", c->dirname, filename);
  797. snprintf(temp_path, sizeof(temp_path), use_rename ? "%s.tmp" : "%s", full_path);
  798. ret = s->io_open(s, &os->out, temp_path, AVIO_FLAG_WRITE, NULL);
  799. if (ret < 0)
  800. break;
  801. write_styp(os->ctx->pb);
  802. } else {
  803. snprintf(full_path, sizeof(full_path), "%s%s", c->dirname, os->initfile);
  804. }
  805. av_write_frame(os->ctx, NULL);
  806. avio_flush(os->ctx->pb);
  807. os->packets_written = 0;
  808. range_length = avio_tell(os->ctx->pb) - start_pos;
  809. if (c->single_file) {
  810. find_index_range(s, full_path, start_pos, &index_length);
  811. } else {
  812. ff_format_io_close(s, &os->out);
  813. if (use_rename) {
  814. ret = avpriv_io_move(temp_path, full_path);
  815. if (ret < 0)
  816. break;
  817. }
  818. }
  819. if (!os->bit_rate) {
  820. // calculate average bitrate of first segment
  821. int64_t bitrate = (int64_t) range_length * 8 * AV_TIME_BASE / (os->max_pts - os->start_pts);
  822. if (bitrate >= 0) {
  823. os->bit_rate = bitrate;
  824. snprintf(os->bandwidth_str, sizeof(os->bandwidth_str),
  825. " bandwidth=\"%d\"", os->bit_rate);
  826. }
  827. }
  828. add_segment(os, filename, os->start_pts, os->max_pts - os->start_pts, start_pos, range_length, index_length);
  829. av_log(s, AV_LOG_VERBOSE, "Representation %d media segment %d written to: %s\n", i, os->segment_index, full_path);
  830. }
  831. if (c->window_size || (final && c->remove_at_exit)) {
  832. for (i = 0; i < s->nb_streams; i++) {
  833. OutputStream *os = &c->streams[i];
  834. int j;
  835. int remove = os->nb_segments - c->window_size - c->extra_window_size;
  836. if (final && c->remove_at_exit)
  837. remove = os->nb_segments;
  838. if (remove > 0) {
  839. for (j = 0; j < remove; j++) {
  840. char filename[1024];
  841. snprintf(filename, sizeof(filename), "%s%s", c->dirname, os->segments[j]->file);
  842. unlink(filename);
  843. av_free(os->segments[j]);
  844. }
  845. os->nb_segments -= remove;
  846. memmove(os->segments, os->segments + remove, os->nb_segments * sizeof(*os->segments));
  847. }
  848. }
  849. }
  850. if (ret >= 0)
  851. ret = write_manifest(s, final);
  852. return ret;
  853. }
  854. static int dash_write_packet(AVFormatContext *s, AVPacket *pkt)
  855. {
  856. DASHContext *c = s->priv_data;
  857. AVStream *st = s->streams[pkt->stream_index];
  858. OutputStream *os = &c->streams[pkt->stream_index];
  859. int ret;
  860. ret = update_stream_extradata(s, os, st->codecpar);
  861. if (ret < 0)
  862. return ret;
  863. // Fill in a heuristic guess of the packet duration, if none is available.
  864. // The mp4 muxer will do something similar (for the last packet in a fragment)
  865. // if nothing is set (setting it for the other packets doesn't hurt).
  866. // By setting a nonzero duration here, we can be sure that the mp4 muxer won't
  867. // invoke its heuristic (this doesn't have to be identical to that algorithm),
  868. // so that we know the exact timestamps of fragments.
  869. if (!pkt->duration && os->last_dts != AV_NOPTS_VALUE)
  870. pkt->duration = pkt->dts - os->last_dts;
  871. os->last_dts = pkt->dts;
  872. // If forcing the stream to start at 0, the mp4 muxer will set the start
  873. // timestamps to 0. Do the same here, to avoid mismatches in duration/timestamps.
  874. if (os->first_pts == AV_NOPTS_VALUE &&
  875. s->avoid_negative_ts == AVFMT_AVOID_NEG_TS_MAKE_ZERO) {
  876. pkt->pts -= pkt->dts;
  877. pkt->dts = 0;
  878. }
  879. if (os->first_pts == AV_NOPTS_VALUE)
  880. os->first_pts = pkt->pts;
  881. if ((!c->has_video || st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) &&
  882. pkt->flags & AV_PKT_FLAG_KEY && os->packets_written &&
  883. av_compare_ts(pkt->pts - os->start_pts, st->time_base,
  884. c->min_seg_duration, AV_TIME_BASE_Q) >= 0) {
  885. int64_t prev_duration = c->last_duration;
  886. c->last_duration = av_rescale_q(pkt->pts - os->start_pts,
  887. st->time_base,
  888. AV_TIME_BASE_Q);
  889. c->total_duration = av_rescale_q(pkt->pts - os->first_pts,
  890. st->time_base,
  891. AV_TIME_BASE_Q);
  892. if ((!c->use_timeline || !c->use_template) && prev_duration) {
  893. if (c->last_duration < prev_duration*9/10 ||
  894. c->last_duration > prev_duration*11/10) {
  895. av_log(s, AV_LOG_WARNING,
  896. "Segment durations differ too much, enable use_timeline "
  897. "and use_template, or keep a stricter keyframe interval\n");
  898. }
  899. }
  900. if ((ret = dash_flush(s, 0, pkt->stream_index)) < 0)
  901. return ret;
  902. }
  903. if (!os->packets_written) {
  904. // If we wrote a previous segment, adjust the start time of the segment
  905. // to the end of the previous one (which is the same as the mp4 muxer
  906. // does). This avoids gaps in the timeline.
  907. if (os->max_pts != AV_NOPTS_VALUE)
  908. os->start_pts = os->max_pts;
  909. else
  910. os->start_pts = pkt->pts;
  911. }
  912. if (os->max_pts == AV_NOPTS_VALUE)
  913. os->max_pts = pkt->pts + pkt->duration;
  914. else
  915. os->max_pts = FFMAX(os->max_pts, pkt->pts + pkt->duration);
  916. os->packets_written++;
  917. return ff_write_chained(os->ctx, 0, pkt, s, 0);
  918. }
  919. static int dash_write_trailer(AVFormatContext *s)
  920. {
  921. DASHContext *c = s->priv_data;
  922. if (s->nb_streams > 0) {
  923. OutputStream *os = &c->streams[0];
  924. // If no segments have been written so far, try to do a crude
  925. // guess of the segment duration
  926. if (!c->last_duration)
  927. c->last_duration = av_rescale_q(os->max_pts - os->start_pts,
  928. s->streams[0]->time_base,
  929. AV_TIME_BASE_Q);
  930. c->total_duration = av_rescale_q(os->max_pts - os->first_pts,
  931. s->streams[0]->time_base,
  932. AV_TIME_BASE_Q);
  933. }
  934. dash_flush(s, 1, -1);
  935. if (c->remove_at_exit) {
  936. char filename[1024];
  937. int i;
  938. for (i = 0; i < s->nb_streams; i++) {
  939. OutputStream *os = &c->streams[i];
  940. snprintf(filename, sizeof(filename), "%s%s", c->dirname, os->initfile);
  941. unlink(filename);
  942. }
  943. unlink(s->filename);
  944. }
  945. return 0;
  946. }
  947. static int dash_check_bitstream(struct AVFormatContext *s, const AVPacket *avpkt)
  948. {
  949. DASHContext *c = s->priv_data;
  950. OutputStream *os = &c->streams[avpkt->stream_index];
  951. AVFormatContext *oc = os->ctx;
  952. if (oc->oformat->check_bitstream) {
  953. int ret;
  954. AVPacket pkt = *avpkt;
  955. pkt.stream_index = 0;
  956. ret = oc->oformat->check_bitstream(oc, &pkt);
  957. if (ret == 1) {
  958. AVStream *st = s->streams[avpkt->stream_index];
  959. AVStream *ost = oc->streams[0];
  960. st->internal->bsfcs = ost->internal->bsfcs;
  961. st->internal->nb_bsfcs = ost->internal->nb_bsfcs;
  962. ost->internal->bsfcs = NULL;
  963. ost->internal->nb_bsfcs = 0;
  964. }
  965. return ret;
  966. }
  967. return 1;
  968. }
  969. #define OFFSET(x) offsetof(DASHContext, x)
  970. #define E AV_OPT_FLAG_ENCODING_PARAM
  971. static const AVOption options[] = {
  972. { "adaptation_sets", "Adaptation sets. Syntax: id=0,streams=0,1,2 id=1,streams=3,4 and so on", OFFSET(adaptation_sets), AV_OPT_TYPE_STRING, { 0 }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
  973. { "window_size", "number of segments kept in the manifest", OFFSET(window_size), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, E },
  974. { "extra_window_size", "number of segments kept outside of the manifest before removing from disk", OFFSET(extra_window_size), AV_OPT_TYPE_INT, { .i64 = 5 }, 0, INT_MAX, E },
  975. { "min_seg_duration", "minimum segment duration (in microseconds)", OFFSET(min_seg_duration), AV_OPT_TYPE_INT64, { .i64 = 5000000 }, 0, INT_MAX, E },
  976. { "remove_at_exit", "remove all segments when finished", OFFSET(remove_at_exit), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, E },
  977. { "use_template", "Use SegmentTemplate instead of SegmentList", OFFSET(use_template), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, E },
  978. { "use_timeline", "Use SegmentTimeline in SegmentTemplate", OFFSET(use_timeline), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, E },
  979. { "single_file", "Store all segments in one file, accessed using byte ranges", OFFSET(single_file), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, E },
  980. { "single_file_name", "DASH-templated name to be used for baseURL. Implies storing all segments in one file, accessed using byte ranges", OFFSET(single_file_name), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, E },
  981. { "init_seg_name", "DASH-templated name to used for the initialization segment", OFFSET(init_seg_name), AV_OPT_TYPE_STRING, {.str = "init-stream$RepresentationID$.m4s"}, 0, 0, E },
  982. { "media_seg_name", "DASH-templated name to used for the media segments", OFFSET(media_seg_name), AV_OPT_TYPE_STRING, {.str = "chunk-stream$RepresentationID$-$Number%05d$.m4s"}, 0, 0, E },
  983. { "utc_timing_url", "URL of the page that will return the UTC timestamp in ISO format", OFFSET(utc_timing_url), AV_OPT_TYPE_STRING, { 0 }, 0, 0, E },
  984. { NULL },
  985. };
  986. static const AVClass dash_class = {
  987. .class_name = "dash muxer",
  988. .item_name = av_default_item_name,
  989. .option = options,
  990. .version = LIBAVUTIL_VERSION_INT,
  991. };
  992. AVOutputFormat ff_dash_muxer = {
  993. .name = "dash",
  994. .long_name = NULL_IF_CONFIG_SMALL("DASH Muxer"),
  995. .priv_data_size = sizeof(DASHContext),
  996. .audio_codec = AV_CODEC_ID_AAC,
  997. .video_codec = AV_CODEC_ID_H264,
  998. .flags = AVFMT_GLOBALHEADER | AVFMT_NOFILE | AVFMT_TS_NEGATIVE,
  999. .init = dash_init,
  1000. .write_header = dash_write_header,
  1001. .write_packet = dash_write_packet,
  1002. .write_trailer = dash_write_trailer,
  1003. .deinit = dash_free,
  1004. .check_bitstream = dash_check_bitstream,
  1005. .priv_class = &dash_class,
  1006. };