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.

647 lines
20KB

  1. /*
  2. * muxing functions for use within Libav
  3. * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
  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 "avformat.h"
  22. #include "avio_internal.h"
  23. #include "internal.h"
  24. #include "libavcodec/internal.h"
  25. #include "libavcodec/bytestream.h"
  26. #include "libavutil/opt.h"
  27. #include "libavutil/dict.h"
  28. #include "libavutil/pixdesc.h"
  29. #include "metadata.h"
  30. #include "id3v2.h"
  31. #include "libavutil/avassert.h"
  32. #include "libavutil/avstring.h"
  33. #include "libavutil/mathematics.h"
  34. #include "libavutil/parseutils.h"
  35. #include "libavutil/time.h"
  36. #include "riff.h"
  37. #include "audiointerleave.h"
  38. #include "url.h"
  39. #include <stdarg.h>
  40. #if CONFIG_NETWORK
  41. #include "network.h"
  42. #endif
  43. #undef NDEBUG
  44. #include <assert.h>
  45. /**
  46. * @file
  47. * muxing functions for use within Libav
  48. */
  49. /* fraction handling */
  50. /**
  51. * f = val + (num / den) + 0.5.
  52. *
  53. * 'num' is normalized so that it is such as 0 <= num < den.
  54. *
  55. * @param f fractional number
  56. * @param val integer value
  57. * @param num must be >= 0
  58. * @param den must be >= 1
  59. */
  60. static void frac_init(AVFrac *f, int64_t val, int64_t num, int64_t den)
  61. {
  62. num += (den >> 1);
  63. if (num >= den) {
  64. val += num / den;
  65. num = num % den;
  66. }
  67. f->val = val;
  68. f->num = num;
  69. f->den = den;
  70. }
  71. /**
  72. * Fractional addition to f: f = f + (incr / f->den).
  73. *
  74. * @param f fractional number
  75. * @param incr increment, can be positive or negative
  76. */
  77. static void frac_add(AVFrac *f, int64_t incr)
  78. {
  79. int64_t num, den;
  80. num = f->num + incr;
  81. den = f->den;
  82. if (num < 0) {
  83. f->val += num / den;
  84. num = num % den;
  85. if (num < 0) {
  86. num += den;
  87. f->val--;
  88. }
  89. } else if (num >= den) {
  90. f->val += num / den;
  91. num = num % den;
  92. }
  93. f->num = num;
  94. }
  95. static int validate_codec_tag(AVFormatContext *s, AVStream *st)
  96. {
  97. const AVCodecTag *avctag;
  98. int n;
  99. enum AVCodecID id = AV_CODEC_ID_NONE;
  100. unsigned int tag = 0;
  101. /**
  102. * Check that tag + id is in the table
  103. * If neither is in the table -> OK
  104. * If tag is in the table with another id -> FAIL
  105. * If id is in the table with another tag -> FAIL unless strict < normal
  106. */
  107. for (n = 0; s->oformat->codec_tag[n]; n++) {
  108. avctag = s->oformat->codec_tag[n];
  109. while (avctag->id != AV_CODEC_ID_NONE) {
  110. if (avpriv_toupper4(avctag->tag) == avpriv_toupper4(st->codec->codec_tag)) {
  111. id = avctag->id;
  112. if (id == st->codec->codec_id)
  113. return 1;
  114. }
  115. if (avctag->id == st->codec->codec_id)
  116. tag = avctag->tag;
  117. avctag++;
  118. }
  119. }
  120. if (id != AV_CODEC_ID_NONE)
  121. return 0;
  122. if (tag && (st->codec->strict_std_compliance >= FF_COMPLIANCE_NORMAL))
  123. return 0;
  124. return 1;
  125. }
  126. static int init_muxer(AVFormatContext *s, AVDictionary **options)
  127. {
  128. int ret = 0, i;
  129. AVStream *st;
  130. AVDictionary *tmp = NULL;
  131. AVCodecContext *codec = NULL;
  132. AVOutputFormat *of = s->oformat;
  133. if (options)
  134. av_dict_copy(&tmp, *options, 0);
  135. if ((ret = av_opt_set_dict(s, &tmp)) < 0)
  136. goto fail;
  137. // some sanity checks
  138. if (s->nb_streams == 0 && !(of->flags & AVFMT_NOSTREAMS)) {
  139. av_log(s, AV_LOG_ERROR, "no streams\n");
  140. ret = AVERROR(EINVAL);
  141. goto fail;
  142. }
  143. for (i = 0; i < s->nb_streams; i++) {
  144. st = s->streams[i];
  145. codec = st->codec;
  146. switch (codec->codec_type) {
  147. case AVMEDIA_TYPE_AUDIO:
  148. if (codec->sample_rate <= 0) {
  149. av_log(s, AV_LOG_ERROR, "sample rate not set\n");
  150. ret = AVERROR(EINVAL);
  151. goto fail;
  152. }
  153. if (!codec->block_align)
  154. codec->block_align = codec->channels *
  155. av_get_bits_per_sample(codec->codec_id) >> 3;
  156. break;
  157. case AVMEDIA_TYPE_VIDEO:
  158. if (codec->time_base.num <= 0 ||
  159. codec->time_base.den <= 0) { //FIXME audio too?
  160. av_log(s, AV_LOG_ERROR, "time base not set\n");
  161. ret = AVERROR(EINVAL);
  162. goto fail;
  163. }
  164. if ((codec->width <= 0 || codec->height <= 0) &&
  165. !(of->flags & AVFMT_NODIMENSIONS)) {
  166. av_log(s, AV_LOG_ERROR, "dimensions not set\n");
  167. ret = AVERROR(EINVAL);
  168. goto fail;
  169. }
  170. if (av_cmp_q(st->sample_aspect_ratio,
  171. codec->sample_aspect_ratio)) {
  172. if (st->sample_aspect_ratio.num != 0 &&
  173. st->sample_aspect_ratio.den != 0 &&
  174. codec->sample_aspect_ratio.den != 0 &&
  175. codec->sample_aspect_ratio.den != 0) {
  176. av_log(s, AV_LOG_ERROR, "Aspect ratio mismatch between muxer "
  177. "(%d/%d) and encoder layer (%d/%d)\n",
  178. st->sample_aspect_ratio.num, st->sample_aspect_ratio.den,
  179. codec->sample_aspect_ratio.num,
  180. codec->sample_aspect_ratio.den);
  181. ret = AVERROR(EINVAL);
  182. goto fail;
  183. }
  184. }
  185. break;
  186. }
  187. if (of->codec_tag) {
  188. if (codec->codec_tag &&
  189. codec->codec_id == AV_CODEC_ID_RAWVIDEO &&
  190. !av_codec_get_tag(of->codec_tag, codec->codec_id) &&
  191. !validate_codec_tag(s, st)) {
  192. // the current rawvideo encoding system ends up setting
  193. // the wrong codec_tag for avi, we override it here
  194. codec->codec_tag = 0;
  195. }
  196. if (codec->codec_tag) {
  197. if (!validate_codec_tag(s, st)) {
  198. char tagbuf[32];
  199. av_get_codec_tag_string(tagbuf, sizeof(tagbuf), codec->codec_tag);
  200. av_log(s, AV_LOG_ERROR,
  201. "Tag %s/0x%08x incompatible with output codec id '%d'\n",
  202. tagbuf, codec->codec_tag, codec->codec_id);
  203. ret = AVERROR_INVALIDDATA;
  204. goto fail;
  205. }
  206. } else
  207. codec->codec_tag = av_codec_get_tag(of->codec_tag, codec->codec_id);
  208. }
  209. if (of->flags & AVFMT_GLOBALHEADER &&
  210. !(codec->flags & CODEC_FLAG_GLOBAL_HEADER))
  211. av_log(s, AV_LOG_WARNING,
  212. "Codec for stream %d does not use global headers "
  213. "but container format requires global headers\n", i);
  214. }
  215. if (!s->priv_data && of->priv_data_size > 0) {
  216. s->priv_data = av_mallocz(of->priv_data_size);
  217. if (!s->priv_data) {
  218. ret = AVERROR(ENOMEM);
  219. goto fail;
  220. }
  221. if (of->priv_class) {
  222. *(const AVClass **)s->priv_data = of->priv_class;
  223. av_opt_set_defaults(s->priv_data);
  224. if ((ret = av_opt_set_dict(s->priv_data, &tmp)) < 0)
  225. goto fail;
  226. }
  227. }
  228. /* set muxer identification string */
  229. if (s->nb_streams && !(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT)) {
  230. av_dict_set(&s->metadata, "encoder", LIBAVFORMAT_IDENT, 0);
  231. }
  232. if (options) {
  233. av_dict_free(options);
  234. *options = tmp;
  235. }
  236. return 0;
  237. fail:
  238. av_dict_free(&tmp);
  239. return ret;
  240. }
  241. static int init_pts(AVFormatContext *s)
  242. {
  243. int i;
  244. AVStream *st;
  245. /* init PTS generation */
  246. for (i = 0; i < s->nb_streams; i++) {
  247. int64_t den = AV_NOPTS_VALUE;
  248. st = s->streams[i];
  249. switch (st->codec->codec_type) {
  250. case AVMEDIA_TYPE_AUDIO:
  251. den = (int64_t)st->time_base.num * st->codec->sample_rate;
  252. break;
  253. case AVMEDIA_TYPE_VIDEO:
  254. den = (int64_t)st->time_base.num * st->codec->time_base.den;
  255. break;
  256. default:
  257. break;
  258. }
  259. if (den != AV_NOPTS_VALUE) {
  260. if (den <= 0)
  261. return AVERROR_INVALIDDATA;
  262. frac_init(&st->pts, 0, 0, den);
  263. }
  264. }
  265. return 0;
  266. }
  267. int avformat_write_header(AVFormatContext *s, AVDictionary **options)
  268. {
  269. int ret = 0;
  270. if (ret = init_muxer(s, options))
  271. return ret;
  272. if (s->oformat->write_header) {
  273. ret = s->oformat->write_header(s);
  274. if (ret < 0)
  275. return ret;
  276. }
  277. if ((ret = init_pts(s)) < 0)
  278. return ret;
  279. return 0;
  280. }
  281. //FIXME merge with compute_pkt_fields
  282. static int compute_pkt_fields2(AVFormatContext *s, AVStream *st, AVPacket *pkt)
  283. {
  284. int delay = FFMAX(st->codec->has_b_frames, !!st->codec->max_b_frames);
  285. int num, den, frame_size, i;
  286. av_dlog(s, "compute_pkt_fields2: pts:%" PRId64 " dts:%" PRId64 " cur_dts:%" PRId64 " b:%d size:%d st:%d\n",
  287. pkt->pts, pkt->dts, st->cur_dts, delay, pkt->size, pkt->stream_index);
  288. /* if(pkt->pts == AV_NOPTS_VALUE && pkt->dts == AV_NOPTS_VALUE)
  289. * return AVERROR(EINVAL);*/
  290. /* duration field */
  291. if (pkt->duration == 0) {
  292. ff_compute_frame_duration(&num, &den, st, NULL, pkt);
  293. if (den && num) {
  294. pkt->duration = av_rescale(1, num * (int64_t)st->time_base.den * st->codec->ticks_per_frame, den * (int64_t)st->time_base.num);
  295. }
  296. }
  297. if (pkt->pts == AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE && delay == 0)
  298. pkt->pts = pkt->dts;
  299. //XXX/FIXME this is a temporary hack until all encoders output pts
  300. if ((pkt->pts == 0 || pkt->pts == AV_NOPTS_VALUE) && pkt->dts == AV_NOPTS_VALUE && !delay) {
  301. pkt->dts =
  302. // pkt->pts= st->cur_dts;
  303. pkt->pts = st->pts.val;
  304. }
  305. //calculate dts from pts
  306. if (pkt->pts != AV_NOPTS_VALUE && pkt->dts == AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY) {
  307. st->pts_buffer[0] = pkt->pts;
  308. for (i = 1; i < delay + 1 && st->pts_buffer[i] == AV_NOPTS_VALUE; i++)
  309. st->pts_buffer[i] = pkt->pts + (i - delay - 1) * pkt->duration;
  310. for (i = 0; i<delay && st->pts_buffer[i] > st->pts_buffer[i + 1]; i++)
  311. FFSWAP(int64_t, st->pts_buffer[i], st->pts_buffer[i + 1]);
  312. pkt->dts = st->pts_buffer[0];
  313. }
  314. if (st->cur_dts && st->cur_dts != AV_NOPTS_VALUE &&
  315. ((!(s->oformat->flags & AVFMT_TS_NONSTRICT) &&
  316. st->cur_dts >= pkt->dts) || st->cur_dts > pkt->dts)) {
  317. av_log(s, AV_LOG_ERROR,
  318. "Application provided invalid, non monotonically increasing dts to muxer in stream %d: %" PRId64 " >= %" PRId64 "\n",
  319. st->index, st->cur_dts, pkt->dts);
  320. return AVERROR(EINVAL);
  321. }
  322. if (pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE && pkt->pts < pkt->dts) {
  323. av_log(s, AV_LOG_ERROR, "pts < dts in stream %d\n", st->index);
  324. return AVERROR(EINVAL);
  325. }
  326. av_dlog(s, "av_write_frame: pts2:%"PRId64" dts2:%"PRId64"\n",
  327. pkt->pts, pkt->dts);
  328. st->cur_dts = pkt->dts;
  329. st->pts.val = pkt->dts;
  330. /* update pts */
  331. switch (st->codec->codec_type) {
  332. case AVMEDIA_TYPE_AUDIO:
  333. frame_size = ff_get_audio_frame_size(st->codec, pkt->size, 1);
  334. /* HACK/FIXME, we skip the initial 0 size packets as they are most
  335. * likely equal to the encoder delay, but it would be better if we
  336. * had the real timestamps from the encoder */
  337. if (frame_size >= 0 && (pkt->size || st->pts.num != st->pts.den >> 1 || st->pts.val)) {
  338. frac_add(&st->pts, (int64_t)st->time_base.den * frame_size);
  339. }
  340. break;
  341. case AVMEDIA_TYPE_VIDEO:
  342. frac_add(&st->pts, (int64_t)st->time_base.den * st->codec->time_base.num);
  343. break;
  344. default:
  345. break;
  346. }
  347. return 0;
  348. }
  349. /*
  350. * FIXME: this function should NEVER get undefined pts/dts beside when the
  351. * AVFMT_NOTIMESTAMPS is set.
  352. * Those additional safety checks should be dropped once the correct checks
  353. * are set in the callers.
  354. */
  355. static int write_packet(AVFormatContext *s, AVPacket *pkt)
  356. {
  357. if (!(s->oformat->flags & (AVFMT_TS_NEGATIVE | AVFMT_NOTIMESTAMPS))) {
  358. AVRational time_base = s->streams[pkt->stream_index]->time_base;
  359. int64_t offset = 0;
  360. if (!s->offset && pkt->dts != AV_NOPTS_VALUE && pkt->dts < 0) {
  361. s->offset = -pkt->dts;
  362. s->offset_timebase = time_base;
  363. }
  364. if (s->offset)
  365. offset = av_rescale_q(s->offset, s->offset_timebase, time_base);
  366. if (pkt->dts != AV_NOPTS_VALUE)
  367. pkt->dts += offset;
  368. if (pkt->pts != AV_NOPTS_VALUE)
  369. pkt->pts += offset;
  370. }
  371. return s->oformat->write_packet(s, pkt);
  372. }
  373. int av_write_frame(AVFormatContext *s, AVPacket *pkt)
  374. {
  375. int ret;
  376. if (!pkt) {
  377. if (s->oformat->flags & AVFMT_ALLOW_FLUSH)
  378. return s->oformat->write_packet(s, pkt);
  379. return 1;
  380. }
  381. ret = compute_pkt_fields2(s, s->streams[pkt->stream_index], pkt);
  382. if (ret < 0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
  383. return ret;
  384. ret = write_packet(s, pkt);
  385. if (ret >= 0)
  386. s->streams[pkt->stream_index]->nb_frames++;
  387. return ret;
  388. }
  389. void ff_interleave_add_packet(AVFormatContext *s, AVPacket *pkt,
  390. int (*compare)(AVFormatContext *, AVPacket *, AVPacket *))
  391. {
  392. AVPacketList **next_point, *this_pktl;
  393. this_pktl = av_mallocz(sizeof(AVPacketList));
  394. this_pktl->pkt = *pkt;
  395. #if FF_API_DESTRUCT_PACKET
  396. pkt->destruct = NULL; // do not free original but only the copy
  397. #endif
  398. pkt->buf = NULL;
  399. av_dup_packet(&this_pktl->pkt); // duplicate the packet if it uses non-alloced memory
  400. if (s->streams[pkt->stream_index]->last_in_packet_buffer) {
  401. next_point = &(s->streams[pkt->stream_index]->last_in_packet_buffer->next);
  402. } else
  403. next_point = &s->packet_buffer;
  404. if (*next_point) {
  405. if (compare(s, &s->packet_buffer_end->pkt, pkt)) {
  406. while (!compare(s, &(*next_point)->pkt, pkt))
  407. next_point = &(*next_point)->next;
  408. goto next_non_null;
  409. } else {
  410. next_point = &(s->packet_buffer_end->next);
  411. }
  412. }
  413. assert(!*next_point);
  414. s->packet_buffer_end = this_pktl;
  415. next_non_null:
  416. this_pktl->next = *next_point;
  417. s->streams[pkt->stream_index]->last_in_packet_buffer =
  418. *next_point = this_pktl;
  419. }
  420. static int interleave_compare_dts(AVFormatContext *s, AVPacket *next,
  421. AVPacket *pkt)
  422. {
  423. AVStream *st = s->streams[pkt->stream_index];
  424. AVStream *st2 = s->streams[next->stream_index];
  425. int comp = av_compare_ts(next->dts, st2->time_base, pkt->dts,
  426. st->time_base);
  427. if (comp == 0)
  428. return pkt->stream_index < next->stream_index;
  429. return comp > 0;
  430. }
  431. int ff_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out,
  432. AVPacket *pkt, int flush)
  433. {
  434. AVPacketList *pktl;
  435. int stream_count = 0;
  436. int i;
  437. if (pkt) {
  438. ff_interleave_add_packet(s, pkt, interleave_compare_dts);
  439. }
  440. for (i = 0; i < s->nb_streams; i++)
  441. stream_count += !!s->streams[i]->last_in_packet_buffer;
  442. if (stream_count && (s->nb_streams == stream_count || flush)) {
  443. pktl = s->packet_buffer;
  444. *out = pktl->pkt;
  445. s->packet_buffer = pktl->next;
  446. if (!s->packet_buffer)
  447. s->packet_buffer_end = NULL;
  448. if (s->streams[out->stream_index]->last_in_packet_buffer == pktl)
  449. s->streams[out->stream_index]->last_in_packet_buffer = NULL;
  450. av_freep(&pktl);
  451. return 1;
  452. } else {
  453. av_init_packet(out);
  454. return 0;
  455. }
  456. }
  457. /**
  458. * Interleave an AVPacket correctly so it can be muxed.
  459. * @param out the interleaved packet will be output here
  460. * @param in the input packet
  461. * @param flush 1 if no further packets are available as input and all
  462. * remaining packets should be output
  463. * @return 1 if a packet was output, 0 if no packet could be output,
  464. * < 0 if an error occurred
  465. */
  466. static int interleave_packet(AVFormatContext *s, AVPacket *out, AVPacket *in, int flush)
  467. {
  468. if (s->oformat->interleave_packet) {
  469. int ret = s->oformat->interleave_packet(s, out, in, flush);
  470. if (in)
  471. av_free_packet(in);
  472. return ret;
  473. } else
  474. return ff_interleave_packet_per_dts(s, out, in, flush);
  475. }
  476. int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt)
  477. {
  478. int ret, flush = 0;
  479. if (pkt) {
  480. AVStream *st = s->streams[pkt->stream_index];
  481. //FIXME/XXX/HACK drop zero sized packets
  482. if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && pkt->size == 0)
  483. return 0;
  484. av_dlog(s, "av_interleaved_write_frame size:%d dts:%" PRId64 " pts:%" PRId64 "\n",
  485. pkt->size, pkt->dts, pkt->pts);
  486. if ((ret = compute_pkt_fields2(s, st, pkt)) < 0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
  487. return ret;
  488. if (pkt->dts == AV_NOPTS_VALUE && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
  489. return AVERROR(EINVAL);
  490. } else {
  491. av_dlog(s, "av_interleaved_write_frame FLUSH\n");
  492. flush = 1;
  493. }
  494. for (;; ) {
  495. AVPacket opkt;
  496. int ret = interleave_packet(s, &opkt, pkt, flush);
  497. if (ret <= 0) //FIXME cleanup needed for ret<0 ?
  498. return ret;
  499. ret = write_packet(s, &opkt);
  500. if (ret >= 0)
  501. s->streams[opkt.stream_index]->nb_frames++;
  502. av_free_packet(&opkt);
  503. pkt = NULL;
  504. if (ret < 0)
  505. return ret;
  506. }
  507. }
  508. int av_write_trailer(AVFormatContext *s)
  509. {
  510. int ret, i;
  511. for (;; ) {
  512. AVPacket pkt;
  513. ret = interleave_packet(s, &pkt, NULL, 1);
  514. if (ret < 0) //FIXME cleanup needed for ret<0 ?
  515. goto fail;
  516. if (!ret)
  517. break;
  518. ret = write_packet(s, &pkt);
  519. if (ret >= 0)
  520. s->streams[pkt.stream_index]->nb_frames++;
  521. av_free_packet(&pkt);
  522. if (ret < 0)
  523. goto fail;
  524. }
  525. if (s->oformat->write_trailer)
  526. ret = s->oformat->write_trailer(s);
  527. if (!(s->oformat->flags & AVFMT_NOFILE))
  528. avio_flush(s->pb);
  529. fail:
  530. for (i = 0; i < s->nb_streams; i++) {
  531. av_freep(&s->streams[i]->priv_data);
  532. av_freep(&s->streams[i]->index_entries);
  533. }
  534. if (s->oformat->priv_class)
  535. av_opt_free(s->priv_data);
  536. av_freep(&s->priv_data);
  537. return ret;
  538. }
  539. int ff_write_chained(AVFormatContext *dst, int dst_stream, AVPacket *pkt,
  540. AVFormatContext *src)
  541. {
  542. AVPacket local_pkt;
  543. local_pkt = *pkt;
  544. local_pkt.stream_index = dst_stream;
  545. if (pkt->pts != AV_NOPTS_VALUE)
  546. local_pkt.pts = av_rescale_q(pkt->pts,
  547. src->streams[pkt->stream_index]->time_base,
  548. dst->streams[dst_stream]->time_base);
  549. if (pkt->dts != AV_NOPTS_VALUE)
  550. local_pkt.dts = av_rescale_q(pkt->dts,
  551. src->streams[pkt->stream_index]->time_base,
  552. dst->streams[dst_stream]->time_base);
  553. return av_write_frame(dst, &local_pkt);
  554. }