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.

1264 lines
42KB

  1. /*
  2. * muxing functions for use within FFmpeg
  3. * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
  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 "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 "libavutil/timestamp.h"
  30. #include "metadata.h"
  31. #include "id3v2.h"
  32. #include "libavutil/avassert.h"
  33. #include "libavutil/avstring.h"
  34. #include "libavutil/internal.h"
  35. #include "libavutil/mathematics.h"
  36. #include "libavutil/parseutils.h"
  37. #include "libavutil/time.h"
  38. #include "riff.h"
  39. #include "audiointerleave.h"
  40. #include "url.h"
  41. #include <stdarg.h>
  42. #if CONFIG_NETWORK
  43. #include "network.h"
  44. #endif
  45. /**
  46. * @file
  47. * muxing functions for use within libavformat
  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(FFFrac *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(FFFrac *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. AVRational ff_choose_timebase(AVFormatContext *s, AVStream *st, int min_precision)
  96. {
  97. AVRational q;
  98. int j;
  99. q = st->time_base;
  100. for (j=2; j<14; j+= 1+(j>2))
  101. while (q.den / q.num < min_precision && q.num % j == 0)
  102. q.num /= j;
  103. while (q.den / q.num < min_precision && q.den < (1<<24))
  104. q.den <<= 1;
  105. return q;
  106. }
  107. enum AVChromaLocation ff_choose_chroma_location(AVFormatContext *s, AVStream *st)
  108. {
  109. AVCodecParameters *par = st->codecpar;
  110. const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(par->format);
  111. if (par->chroma_location != AVCHROMA_LOC_UNSPECIFIED)
  112. return par->chroma_location;
  113. if (pix_desc) {
  114. if (pix_desc->log2_chroma_h == 0) {
  115. return AVCHROMA_LOC_TOPLEFT;
  116. } else if (pix_desc->log2_chroma_w == 1 && pix_desc->log2_chroma_h == 1) {
  117. if (par->field_order == AV_FIELD_UNKNOWN || par->field_order == AV_FIELD_PROGRESSIVE) {
  118. switch (par->codec_id) {
  119. case AV_CODEC_ID_MJPEG:
  120. case AV_CODEC_ID_MPEG1VIDEO: return AVCHROMA_LOC_CENTER;
  121. }
  122. }
  123. if (par->field_order == AV_FIELD_UNKNOWN || par->field_order != AV_FIELD_PROGRESSIVE) {
  124. switch (par->codec_id) {
  125. case AV_CODEC_ID_MPEG2VIDEO: return AVCHROMA_LOC_LEFT;
  126. }
  127. }
  128. }
  129. }
  130. return AVCHROMA_LOC_UNSPECIFIED;
  131. }
  132. int avformat_alloc_output_context2(AVFormatContext **avctx, AVOutputFormat *oformat,
  133. const char *format, const char *filename)
  134. {
  135. AVFormatContext *s = avformat_alloc_context();
  136. int ret = 0;
  137. *avctx = NULL;
  138. if (!s)
  139. goto nomem;
  140. if (!oformat) {
  141. if (format) {
  142. oformat = av_guess_format(format, NULL, NULL);
  143. if (!oformat) {
  144. av_log(s, AV_LOG_ERROR, "Requested output format '%s' is not a suitable output format\n", format);
  145. ret = AVERROR(EINVAL);
  146. goto error;
  147. }
  148. } else {
  149. oformat = av_guess_format(NULL, filename, NULL);
  150. if (!oformat) {
  151. ret = AVERROR(EINVAL);
  152. av_log(s, AV_LOG_ERROR, "Unable to find a suitable output format for '%s'\n",
  153. filename);
  154. goto error;
  155. }
  156. }
  157. }
  158. s->oformat = oformat;
  159. if (s->oformat->priv_data_size > 0) {
  160. s->priv_data = av_mallocz(s->oformat->priv_data_size);
  161. if (!s->priv_data)
  162. goto nomem;
  163. if (s->oformat->priv_class) {
  164. *(const AVClass**)s->priv_data= s->oformat->priv_class;
  165. av_opt_set_defaults(s->priv_data);
  166. }
  167. } else
  168. s->priv_data = NULL;
  169. if (filename)
  170. av_strlcpy(s->filename, filename, sizeof(s->filename));
  171. *avctx = s;
  172. return 0;
  173. nomem:
  174. av_log(s, AV_LOG_ERROR, "Out of memory\n");
  175. ret = AVERROR(ENOMEM);
  176. error:
  177. avformat_free_context(s);
  178. return ret;
  179. }
  180. static int validate_codec_tag(AVFormatContext *s, AVStream *st)
  181. {
  182. const AVCodecTag *avctag;
  183. int n;
  184. enum AVCodecID id = AV_CODEC_ID_NONE;
  185. int64_t tag = -1;
  186. /**
  187. * Check that tag + id is in the table
  188. * If neither is in the table -> OK
  189. * If tag is in the table with another id -> FAIL
  190. * If id is in the table with another tag -> FAIL unless strict < normal
  191. */
  192. for (n = 0; s->oformat->codec_tag[n]; n++) {
  193. avctag = s->oformat->codec_tag[n];
  194. while (avctag->id != AV_CODEC_ID_NONE) {
  195. if (avpriv_toupper4(avctag->tag) == avpriv_toupper4(st->codecpar->codec_tag)) {
  196. id = avctag->id;
  197. if (id == st->codecpar->codec_id)
  198. return 1;
  199. }
  200. if (avctag->id == st->codecpar->codec_id)
  201. tag = avctag->tag;
  202. avctag++;
  203. }
  204. }
  205. if (id != AV_CODEC_ID_NONE)
  206. return 0;
  207. if (tag >= 0 && (s->strict_std_compliance >= FF_COMPLIANCE_NORMAL))
  208. return 0;
  209. return 1;
  210. }
  211. static int init_muxer(AVFormatContext *s, AVDictionary **options)
  212. {
  213. int ret = 0, i;
  214. AVStream *st;
  215. AVDictionary *tmp = NULL;
  216. AVCodecParameters *par = NULL;
  217. AVOutputFormat *of = s->oformat;
  218. const AVCodecDescriptor *desc;
  219. AVDictionaryEntry *e;
  220. if (options)
  221. av_dict_copy(&tmp, *options, 0);
  222. if ((ret = av_opt_set_dict(s, &tmp)) < 0)
  223. goto fail;
  224. if (s->priv_data && s->oformat->priv_class && *(const AVClass**)s->priv_data==s->oformat->priv_class &&
  225. (ret = av_opt_set_dict2(s->priv_data, &tmp, AV_OPT_SEARCH_CHILDREN)) < 0)
  226. goto fail;
  227. #if FF_API_LAVF_AVCTX
  228. FF_DISABLE_DEPRECATION_WARNINGS
  229. if (s->nb_streams && s->streams[0]->codec->flags & AV_CODEC_FLAG_BITEXACT) {
  230. if (!(s->flags & AVFMT_FLAG_BITEXACT)) {
  231. #if FF_API_LAVF_BITEXACT
  232. av_log(s, AV_LOG_WARNING,
  233. "Setting the AVFormatContext to bitexact mode, because "
  234. "the AVCodecContext is in that mode. This behavior will "
  235. "change in the future. To keep the current behavior, set "
  236. "AVFormatContext.flags |= AVFMT_FLAG_BITEXACT.\n");
  237. s->flags |= AVFMT_FLAG_BITEXACT;
  238. #else
  239. av_log(s, AV_LOG_WARNING,
  240. "The AVFormatContext is not in set to bitexact mode, only "
  241. "the AVCodecContext. If this is not intended, set "
  242. "AVFormatContext.flags |= AVFMT_FLAG_BITEXACT.\n");
  243. #endif
  244. }
  245. }
  246. FF_ENABLE_DEPRECATION_WARNINGS
  247. #endif
  248. // some sanity checks
  249. if (s->nb_streams == 0 && !(of->flags & AVFMT_NOSTREAMS)) {
  250. av_log(s, AV_LOG_ERROR, "No streams to mux were specified\n");
  251. ret = AVERROR(EINVAL);
  252. goto fail;
  253. }
  254. for (i = 0; i < s->nb_streams; i++) {
  255. st = s->streams[i];
  256. par = st->codecpar;
  257. #if FF_API_LAVF_CODEC_TB
  258. FF_DISABLE_DEPRECATION_WARNINGS
  259. if (!st->time_base.num && st->codec->time_base.num) {
  260. av_log(s, AV_LOG_WARNING, "Using AVStream.codec.time_base as a "
  261. "timebase hint to the muxer is deprecated. Set "
  262. "AVStream.time_base instead.\n");
  263. avpriv_set_pts_info(st, 64, st->codec->time_base.num, st->codec->time_base.den);
  264. }
  265. FF_ENABLE_DEPRECATION_WARNINGS
  266. #endif
  267. #if FF_API_LAVF_AVCTX
  268. FF_DISABLE_DEPRECATION_WARNINGS
  269. if (st->codecpar->codec_type == AVMEDIA_TYPE_UNKNOWN &&
  270. st->codec->codec_type != AVMEDIA_TYPE_UNKNOWN) {
  271. av_log(s, AV_LOG_WARNING, "Using AVStream.codec to pass codec "
  272. "parameters to muxers is deprecated, use AVStream.codecpar "
  273. "instead.\n");
  274. ret = avcodec_parameters_from_context(st->codecpar, st->codec);
  275. if (ret < 0)
  276. goto fail;
  277. }
  278. FF_ENABLE_DEPRECATION_WARNINGS
  279. #endif
  280. /* update internal context from codecpar, old bsf api needs this
  281. * FIXME: remove when autobsf uses new bsf API */
  282. ret = avcodec_parameters_to_context(st->internal->avctx, st->codecpar);
  283. if (ret < 0)
  284. goto fail;
  285. if (!st->time_base.num) {
  286. /* fall back on the default timebase values */
  287. if (par->codec_type == AVMEDIA_TYPE_AUDIO && par->sample_rate)
  288. avpriv_set_pts_info(st, 64, 1, par->sample_rate);
  289. else
  290. avpriv_set_pts_info(st, 33, 1, 90000);
  291. }
  292. switch (par->codec_type) {
  293. case AVMEDIA_TYPE_AUDIO:
  294. if (par->sample_rate <= 0) {
  295. av_log(s, AV_LOG_ERROR, "sample rate not set\n");
  296. ret = AVERROR(EINVAL);
  297. goto fail;
  298. }
  299. if (!par->block_align)
  300. par->block_align = par->channels *
  301. av_get_bits_per_sample(par->codec_id) >> 3;
  302. break;
  303. case AVMEDIA_TYPE_VIDEO:
  304. if ((par->width <= 0 || par->height <= 0) &&
  305. !(of->flags & AVFMT_NODIMENSIONS)) {
  306. av_log(s, AV_LOG_ERROR, "dimensions not set\n");
  307. ret = AVERROR(EINVAL);
  308. goto fail;
  309. }
  310. if (av_cmp_q(st->sample_aspect_ratio, par->sample_aspect_ratio)
  311. && fabs(av_q2d(st->sample_aspect_ratio) - av_q2d(par->sample_aspect_ratio)) > 0.004*av_q2d(st->sample_aspect_ratio)
  312. ) {
  313. if (st->sample_aspect_ratio.num != 0 &&
  314. st->sample_aspect_ratio.den != 0 &&
  315. par->sample_aspect_ratio.num != 0 &&
  316. par->sample_aspect_ratio.den != 0) {
  317. av_log(s, AV_LOG_ERROR, "Aspect ratio mismatch between muxer "
  318. "(%d/%d) and encoder layer (%d/%d)\n",
  319. st->sample_aspect_ratio.num, st->sample_aspect_ratio.den,
  320. par->sample_aspect_ratio.num,
  321. par->sample_aspect_ratio.den);
  322. ret = AVERROR(EINVAL);
  323. goto fail;
  324. }
  325. }
  326. break;
  327. }
  328. desc = avcodec_descriptor_get(par->codec_id);
  329. if (desc && desc->props & AV_CODEC_PROP_REORDER)
  330. st->internal->reorder = 1;
  331. if (of->codec_tag) {
  332. if ( par->codec_tag
  333. && par->codec_id == AV_CODEC_ID_RAWVIDEO
  334. && ( av_codec_get_tag(of->codec_tag, par->codec_id) == 0
  335. || av_codec_get_tag(of->codec_tag, par->codec_id) == MKTAG('r', 'a', 'w', ' '))
  336. && !validate_codec_tag(s, st)) {
  337. // the current rawvideo encoding system ends up setting
  338. // the wrong codec_tag for avi/mov, we override it here
  339. par->codec_tag = 0;
  340. }
  341. if (par->codec_tag) {
  342. if (!validate_codec_tag(s, st)) {
  343. char tagbuf[32], tagbuf2[32];
  344. av_get_codec_tag_string(tagbuf, sizeof(tagbuf), par->codec_tag);
  345. av_get_codec_tag_string(tagbuf2, sizeof(tagbuf2), av_codec_get_tag(s->oformat->codec_tag, par->codec_id));
  346. av_log(s, AV_LOG_ERROR,
  347. "Tag %s/0x%08x incompatible with output codec id '%d' (%s)\n",
  348. tagbuf, par->codec_tag, par->codec_id, tagbuf2);
  349. ret = AVERROR_INVALIDDATA;
  350. goto fail;
  351. }
  352. } else
  353. par->codec_tag = av_codec_get_tag(of->codec_tag, par->codec_id);
  354. }
  355. if (par->codec_type != AVMEDIA_TYPE_ATTACHMENT)
  356. s->internal->nb_interleaved_streams++;
  357. }
  358. if (!s->priv_data && of->priv_data_size > 0) {
  359. s->priv_data = av_mallocz(of->priv_data_size);
  360. if (!s->priv_data) {
  361. ret = AVERROR(ENOMEM);
  362. goto fail;
  363. }
  364. if (of->priv_class) {
  365. *(const AVClass **)s->priv_data = of->priv_class;
  366. av_opt_set_defaults(s->priv_data);
  367. if ((ret = av_opt_set_dict2(s->priv_data, &tmp, AV_OPT_SEARCH_CHILDREN)) < 0)
  368. goto fail;
  369. }
  370. }
  371. /* set muxer identification string */
  372. if (!(s->flags & AVFMT_FLAG_BITEXACT)) {
  373. av_dict_set(&s->metadata, "encoder", LIBAVFORMAT_IDENT, 0);
  374. } else {
  375. av_dict_set(&s->metadata, "encoder", NULL, 0);
  376. }
  377. for (e = NULL; e = av_dict_get(s->metadata, "encoder-", e, AV_DICT_IGNORE_SUFFIX); ) {
  378. av_dict_set(&s->metadata, e->key, NULL, 0);
  379. }
  380. if (options) {
  381. av_dict_free(options);
  382. *options = tmp;
  383. }
  384. if (s->oformat->init && (ret = s->oformat->init(s)) < 0) {
  385. s->oformat->deinit(s);
  386. goto fail;
  387. }
  388. return 0;
  389. fail:
  390. av_dict_free(&tmp);
  391. return ret;
  392. }
  393. static int init_pts(AVFormatContext *s)
  394. {
  395. int i;
  396. AVStream *st;
  397. /* init PTS generation */
  398. for (i = 0; i < s->nb_streams; i++) {
  399. int64_t den = AV_NOPTS_VALUE;
  400. st = s->streams[i];
  401. switch (st->codecpar->codec_type) {
  402. case AVMEDIA_TYPE_AUDIO:
  403. den = (int64_t)st->time_base.num * st->codecpar->sample_rate;
  404. break;
  405. case AVMEDIA_TYPE_VIDEO:
  406. den = (int64_t)st->time_base.num * st->time_base.den;
  407. break;
  408. default:
  409. break;
  410. }
  411. if (!st->priv_pts)
  412. st->priv_pts = av_mallocz(sizeof(*st->priv_pts));
  413. if (!st->priv_pts)
  414. return AVERROR(ENOMEM);
  415. if (den != AV_NOPTS_VALUE) {
  416. if (den <= 0)
  417. return AVERROR_INVALIDDATA;
  418. frac_init(st->priv_pts, 0, 0, den);
  419. }
  420. }
  421. return 0;
  422. }
  423. int avformat_write_header(AVFormatContext *s, AVDictionary **options)
  424. {
  425. int ret = 0;
  426. if ((ret = init_muxer(s, options)) < 0)
  427. return ret;
  428. if (s->oformat->write_header && !s->oformat->check_bitstream) {
  429. ret = s->oformat->write_header(s);
  430. if (ret >= 0 && s->pb && s->pb->error < 0)
  431. ret = s->pb->error;
  432. if (ret < 0)
  433. return ret;
  434. if (s->flush_packets && s->pb && s->pb->error >= 0 && s->flags & AVFMT_FLAG_FLUSH_PACKETS)
  435. avio_flush(s->pb);
  436. s->internal->header_written = 1;
  437. }
  438. if ((ret = init_pts(s)) < 0)
  439. return ret;
  440. if (s->avoid_negative_ts < 0) {
  441. av_assert2(s->avoid_negative_ts == AVFMT_AVOID_NEG_TS_AUTO);
  442. if (s->oformat->flags & (AVFMT_TS_NEGATIVE | AVFMT_NOTIMESTAMPS)) {
  443. s->avoid_negative_ts = 0;
  444. } else
  445. s->avoid_negative_ts = AVFMT_AVOID_NEG_TS_MAKE_NON_NEGATIVE;
  446. }
  447. return 0;
  448. }
  449. #define AV_PKT_FLAG_UNCODED_FRAME 0x2000
  450. /* Note: using sizeof(AVFrame) from outside lavu is unsafe in general, but
  451. it is only being used internally to this file as a consistency check.
  452. The value is chosen to be very unlikely to appear on its own and to cause
  453. immediate failure if used anywhere as a real size. */
  454. #define UNCODED_FRAME_PACKET_SIZE (INT_MIN / 3 * 2 + (int)sizeof(AVFrame))
  455. #if FF_API_COMPUTE_PKT_FIELDS2
  456. FF_DISABLE_DEPRECATION_WARNINGS
  457. //FIXME merge with compute_pkt_fields
  458. static int compute_muxer_pkt_fields(AVFormatContext *s, AVStream *st, AVPacket *pkt)
  459. {
  460. int delay = FFMAX(st->codecpar->video_delay, st->internal->avctx->max_b_frames > 0);
  461. int num, den, i;
  462. int frame_size;
  463. if (!s->internal->missing_ts_warning &&
  464. !(s->oformat->flags & AVFMT_NOTIMESTAMPS) &&
  465. (pkt->pts == AV_NOPTS_VALUE || pkt->dts == AV_NOPTS_VALUE)) {
  466. av_log(s, AV_LOG_WARNING,
  467. "Timestamps are unset in a packet for stream %d. "
  468. "This is deprecated and will stop working in the future. "
  469. "Fix your code to set the timestamps properly\n", st->index);
  470. s->internal->missing_ts_warning = 1;
  471. }
  472. if (s->debug & FF_FDEBUG_TS)
  473. av_log(s, AV_LOG_TRACE, "compute_muxer_pkt_fields: pts:%s dts:%s cur_dts:%s b:%d size:%d st:%d\n",
  474. av_ts2str(pkt->pts), av_ts2str(pkt->dts), av_ts2str(st->cur_dts), delay, pkt->size, pkt->stream_index);
  475. if (pkt->duration < 0 && st->codec->codec_type != AVMEDIA_TYPE_SUBTITLE) {
  476. av_log(s, AV_LOG_WARNING, "Packet with invalid duration %"PRId64" in stream %d\n",
  477. pkt->duration, pkt->stream_index);
  478. pkt->duration = 0;
  479. }
  480. /* duration field */
  481. if (pkt->duration == 0) {
  482. ff_compute_frame_duration(s, &num, &den, st, NULL, pkt);
  483. if (den && num) {
  484. pkt->duration = av_rescale(1, num * (int64_t)st->time_base.den * st->codec->ticks_per_frame, den * (int64_t)st->time_base.num);
  485. }
  486. }
  487. if (pkt->pts == AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE && delay == 0)
  488. pkt->pts = pkt->dts;
  489. //XXX/FIXME this is a temporary hack until all encoders output pts
  490. if ((pkt->pts == 0 || pkt->pts == AV_NOPTS_VALUE) && pkt->dts == AV_NOPTS_VALUE && !delay) {
  491. static int warned;
  492. if (!warned) {
  493. av_log(s, AV_LOG_WARNING, "Encoder did not produce proper pts, making some up.\n");
  494. warned = 1;
  495. }
  496. pkt->dts =
  497. // pkt->pts= st->cur_dts;
  498. pkt->pts = st->priv_pts->val;
  499. }
  500. //calculate dts from pts
  501. if (pkt->pts != AV_NOPTS_VALUE && pkt->dts == AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY) {
  502. st->pts_buffer[0] = pkt->pts;
  503. for (i = 1; i < delay + 1 && st->pts_buffer[i] == AV_NOPTS_VALUE; i++)
  504. st->pts_buffer[i] = pkt->pts + (i - delay - 1) * pkt->duration;
  505. for (i = 0; i<delay && st->pts_buffer[i] > st->pts_buffer[i + 1]; i++)
  506. FFSWAP(int64_t, st->pts_buffer[i], st->pts_buffer[i + 1]);
  507. pkt->dts = st->pts_buffer[0];
  508. }
  509. if (st->cur_dts && st->cur_dts != AV_NOPTS_VALUE &&
  510. ((!(s->oformat->flags & AVFMT_TS_NONSTRICT) &&
  511. st->codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE &&
  512. st->codecpar->codec_type != AVMEDIA_TYPE_DATA &&
  513. st->cur_dts >= pkt->dts) || st->cur_dts > pkt->dts)) {
  514. av_log(s, AV_LOG_ERROR,
  515. "Application provided invalid, non monotonically increasing dts to muxer in stream %d: %s >= %s\n",
  516. st->index, av_ts2str(st->cur_dts), av_ts2str(pkt->dts));
  517. return AVERROR(EINVAL);
  518. }
  519. if (pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE && pkt->pts < pkt->dts) {
  520. av_log(s, AV_LOG_ERROR,
  521. "pts (%s) < dts (%s) in stream %d\n",
  522. av_ts2str(pkt->pts), av_ts2str(pkt->dts),
  523. st->index);
  524. return AVERROR(EINVAL);
  525. }
  526. if (s->debug & FF_FDEBUG_TS)
  527. av_log(s, AV_LOG_TRACE, "av_write_frame: pts2:%s dts2:%s\n",
  528. av_ts2str(pkt->pts), av_ts2str(pkt->dts));
  529. st->cur_dts = pkt->dts;
  530. st->priv_pts->val = pkt->dts;
  531. /* update pts */
  532. switch (st->codec->codec_type) {
  533. case AVMEDIA_TYPE_AUDIO:
  534. frame_size = (pkt->flags & AV_PKT_FLAG_UNCODED_FRAME) ?
  535. ((AVFrame *)pkt->data)->nb_samples :
  536. av_get_audio_frame_duration(st->codec, pkt->size);
  537. /* HACK/FIXME, we skip the initial 0 size packets as they are most
  538. * likely equal to the encoder delay, but it would be better if we
  539. * had the real timestamps from the encoder */
  540. if (frame_size >= 0 && (pkt->size || st->priv_pts->num != st->priv_pts->den >> 1 || st->priv_pts->val)) {
  541. frac_add(st->priv_pts, (int64_t)st->time_base.den * frame_size);
  542. }
  543. break;
  544. case AVMEDIA_TYPE_VIDEO:
  545. frac_add(st->priv_pts, (int64_t)st->time_base.den * st->time_base.num);
  546. break;
  547. }
  548. return 0;
  549. }
  550. FF_ENABLE_DEPRECATION_WARNINGS
  551. #endif
  552. /**
  553. * Make timestamps non negative, move side data from payload to internal struct, call muxer, and restore
  554. * sidedata.
  555. *
  556. * FIXME: this function should NEVER get undefined pts/dts beside when the
  557. * AVFMT_NOTIMESTAMPS is set.
  558. * Those additional safety checks should be dropped once the correct checks
  559. * are set in the callers.
  560. */
  561. static int write_packet(AVFormatContext *s, AVPacket *pkt)
  562. {
  563. int ret, did_split;
  564. if (s->output_ts_offset) {
  565. AVStream *st = s->streams[pkt->stream_index];
  566. int64_t offset = av_rescale_q(s->output_ts_offset, AV_TIME_BASE_Q, st->time_base);
  567. if (pkt->dts != AV_NOPTS_VALUE)
  568. pkt->dts += offset;
  569. if (pkt->pts != AV_NOPTS_VALUE)
  570. pkt->pts += offset;
  571. }
  572. if (s->avoid_negative_ts > 0) {
  573. AVStream *st = s->streams[pkt->stream_index];
  574. int64_t offset = st->mux_ts_offset;
  575. int64_t ts = s->internal->avoid_negative_ts_use_pts ? pkt->pts : pkt->dts;
  576. if (s->internal->offset == AV_NOPTS_VALUE && ts != AV_NOPTS_VALUE &&
  577. (ts < 0 || s->avoid_negative_ts == AVFMT_AVOID_NEG_TS_MAKE_ZERO)) {
  578. s->internal->offset = -ts;
  579. s->internal->offset_timebase = st->time_base;
  580. }
  581. if (s->internal->offset != AV_NOPTS_VALUE && !offset) {
  582. offset = st->mux_ts_offset =
  583. av_rescale_q_rnd(s->internal->offset,
  584. s->internal->offset_timebase,
  585. st->time_base,
  586. AV_ROUND_UP);
  587. }
  588. if (pkt->dts != AV_NOPTS_VALUE)
  589. pkt->dts += offset;
  590. if (pkt->pts != AV_NOPTS_VALUE)
  591. pkt->pts += offset;
  592. if (s->internal->avoid_negative_ts_use_pts) {
  593. if (pkt->pts != AV_NOPTS_VALUE && pkt->pts < 0) {
  594. av_log(s, AV_LOG_WARNING, "failed to avoid negative "
  595. "pts %s in stream %d.\n"
  596. "Try -avoid_negative_ts 1 as a possible workaround.\n",
  597. av_ts2str(pkt->dts),
  598. pkt->stream_index
  599. );
  600. }
  601. } else {
  602. av_assert2(pkt->dts == AV_NOPTS_VALUE || pkt->dts >= 0 || s->max_interleave_delta > 0);
  603. if (pkt->dts != AV_NOPTS_VALUE && pkt->dts < 0) {
  604. av_log(s, AV_LOG_WARNING,
  605. "Packets poorly interleaved, failed to avoid negative "
  606. "timestamp %s in stream %d.\n"
  607. "Try -max_interleave_delta 0 as a possible workaround.\n",
  608. av_ts2str(pkt->dts),
  609. pkt->stream_index
  610. );
  611. }
  612. }
  613. }
  614. did_split = av_packet_split_side_data(pkt);
  615. if (!s->internal->header_written && s->oformat->write_header) {
  616. ret = s->oformat->write_header(s);
  617. if (ret >= 0 && s->pb && s->pb->error < 0)
  618. ret = s->pb->error;
  619. if (ret < 0)
  620. goto fail;
  621. if (s->flush_packets && s->pb && s->pb->error >= 0 && s->flags & AVFMT_FLAG_FLUSH_PACKETS)
  622. avio_flush(s->pb);
  623. s->internal->header_written = 1;
  624. }
  625. if ((pkt->flags & AV_PKT_FLAG_UNCODED_FRAME)) {
  626. AVFrame *frame = (AVFrame *)pkt->data;
  627. av_assert0(pkt->size == UNCODED_FRAME_PACKET_SIZE);
  628. ret = s->oformat->write_uncoded_frame(s, pkt->stream_index, &frame, 0);
  629. av_frame_free(&frame);
  630. } else {
  631. ret = s->oformat->write_packet(s, pkt);
  632. }
  633. if (s->pb && ret >= 0) {
  634. if (s->flush_packets && s->flags & AVFMT_FLAG_FLUSH_PACKETS)
  635. avio_flush(s->pb);
  636. if (s->pb->error < 0)
  637. ret = s->pb->error;
  638. }
  639. fail:
  640. if (did_split)
  641. av_packet_merge_side_data(pkt);
  642. return ret;
  643. }
  644. static int check_packet(AVFormatContext *s, AVPacket *pkt)
  645. {
  646. if (!pkt)
  647. return 0;
  648. if (pkt->stream_index < 0 || pkt->stream_index >= s->nb_streams) {
  649. av_log(s, AV_LOG_ERROR, "Invalid packet stream index: %d\n",
  650. pkt->stream_index);
  651. return AVERROR(EINVAL);
  652. }
  653. if (s->streams[pkt->stream_index]->codecpar->codec_type == AVMEDIA_TYPE_ATTACHMENT) {
  654. av_log(s, AV_LOG_ERROR, "Received a packet for an attachment stream.\n");
  655. return AVERROR(EINVAL);
  656. }
  657. return 0;
  658. }
  659. static int prepare_input_packet(AVFormatContext *s, AVPacket *pkt)
  660. {
  661. int ret;
  662. ret = check_packet(s, pkt);
  663. if (ret < 0)
  664. return ret;
  665. #if !FF_API_COMPUTE_PKT_FIELDS2
  666. /* sanitize the timestamps */
  667. if (!(s->oformat->flags & AVFMT_NOTIMESTAMPS)) {
  668. AVStream *st = s->streams[pkt->stream_index];
  669. /* when there is no reordering (so dts is equal to pts), but
  670. * only one of them is set, set the other as well */
  671. if (!st->internal->reorder) {
  672. if (pkt->pts == AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE)
  673. pkt->pts = pkt->dts;
  674. if (pkt->dts == AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE)
  675. pkt->dts = pkt->pts;
  676. }
  677. /* check that the timestamps are set */
  678. if (pkt->pts == AV_NOPTS_VALUE || pkt->dts == AV_NOPTS_VALUE) {
  679. av_log(s, AV_LOG_ERROR,
  680. "Timestamps are unset in a packet for stream %d\n", st->index);
  681. return AVERROR(EINVAL);
  682. }
  683. /* check that the dts are increasing (or at least non-decreasing,
  684. * if the format allows it */
  685. if (st->cur_dts != AV_NOPTS_VALUE &&
  686. ((!(s->oformat->flags & AVFMT_TS_NONSTRICT) && st->cur_dts >= pkt->dts) ||
  687. st->cur_dts > pkt->dts)) {
  688. av_log(s, AV_LOG_ERROR,
  689. "Application provided invalid, non monotonically increasing "
  690. "dts to muxer in stream %d: %" PRId64 " >= %" PRId64 "\n",
  691. st->index, st->cur_dts, pkt->dts);
  692. return AVERROR(EINVAL);
  693. }
  694. if (pkt->pts < pkt->dts) {
  695. av_log(s, AV_LOG_ERROR, "pts %" PRId64 " < dts %" PRId64 " in stream %d\n",
  696. pkt->pts, pkt->dts, st->index);
  697. return AVERROR(EINVAL);
  698. }
  699. }
  700. #endif
  701. return 0;
  702. }
  703. int av_write_frame(AVFormatContext *s, AVPacket *pkt)
  704. {
  705. int ret;
  706. ret = prepare_input_packet(s, pkt);
  707. if (ret < 0)
  708. return ret;
  709. if (!pkt) {
  710. if (s->oformat->flags & AVFMT_ALLOW_FLUSH) {
  711. ret = s->oformat->write_packet(s, NULL);
  712. if (s->flush_packets && s->pb && s->pb->error >= 0 && s->flags & AVFMT_FLAG_FLUSH_PACKETS)
  713. avio_flush(s->pb);
  714. if (ret >= 0 && s->pb && s->pb->error < 0)
  715. ret = s->pb->error;
  716. return ret;
  717. }
  718. return 1;
  719. }
  720. #if FF_API_COMPUTE_PKT_FIELDS2
  721. ret = compute_muxer_pkt_fields(s, s->streams[pkt->stream_index], pkt);
  722. if (ret < 0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
  723. return ret;
  724. #endif
  725. ret = write_packet(s, pkt);
  726. if (ret >= 0 && s->pb && s->pb->error < 0)
  727. ret = s->pb->error;
  728. if (ret >= 0)
  729. s->streams[pkt->stream_index]->nb_frames++;
  730. return ret;
  731. }
  732. #define CHUNK_START 0x1000
  733. int ff_interleave_add_packet(AVFormatContext *s, AVPacket *pkt,
  734. int (*compare)(AVFormatContext *, AVPacket *, AVPacket *))
  735. {
  736. int ret;
  737. AVPacketList **next_point, *this_pktl;
  738. AVStream *st = s->streams[pkt->stream_index];
  739. int chunked = s->max_chunk_size || s->max_chunk_duration;
  740. this_pktl = av_mallocz(sizeof(AVPacketList));
  741. if (!this_pktl)
  742. return AVERROR(ENOMEM);
  743. if ((pkt->flags & AV_PKT_FLAG_UNCODED_FRAME)) {
  744. av_assert0(pkt->size == UNCODED_FRAME_PACKET_SIZE);
  745. av_assert0(((AVFrame *)pkt->data)->buf);
  746. this_pktl->pkt = *pkt;
  747. pkt->buf = NULL;
  748. pkt->side_data = NULL;
  749. pkt->side_data_elems = 0;
  750. } else {
  751. if ((ret = av_packet_ref(&this_pktl->pkt, pkt)) < 0) {
  752. av_free(this_pktl);
  753. return ret;
  754. }
  755. }
  756. if (s->streams[pkt->stream_index]->last_in_packet_buffer) {
  757. next_point = &(st->last_in_packet_buffer->next);
  758. } else {
  759. next_point = &s->internal->packet_buffer;
  760. }
  761. if (chunked) {
  762. uint64_t max= av_rescale_q_rnd(s->max_chunk_duration, AV_TIME_BASE_Q, st->time_base, AV_ROUND_UP);
  763. st->interleaver_chunk_size += pkt->size;
  764. st->interleaver_chunk_duration += pkt->duration;
  765. if ( (s->max_chunk_size && st->interleaver_chunk_size > s->max_chunk_size)
  766. || (max && st->interleaver_chunk_duration > max)) {
  767. st->interleaver_chunk_size = 0;
  768. this_pktl->pkt.flags |= CHUNK_START;
  769. if (max && st->interleaver_chunk_duration > max) {
  770. int64_t syncoffset = (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)*max/2;
  771. int64_t syncto = av_rescale(pkt->dts + syncoffset, 1, max)*max - syncoffset;
  772. st->interleaver_chunk_duration += (pkt->dts - syncto)/8 - max;
  773. } else
  774. st->interleaver_chunk_duration = 0;
  775. }
  776. }
  777. if (*next_point) {
  778. if (chunked && !(this_pktl->pkt.flags & CHUNK_START))
  779. goto next_non_null;
  780. if (compare(s, &s->internal->packet_buffer_end->pkt, pkt)) {
  781. while ( *next_point
  782. && ((chunked && !((*next_point)->pkt.flags&CHUNK_START))
  783. || !compare(s, &(*next_point)->pkt, pkt)))
  784. next_point = &(*next_point)->next;
  785. if (*next_point)
  786. goto next_non_null;
  787. } else {
  788. next_point = &(s->internal->packet_buffer_end->next);
  789. }
  790. }
  791. av_assert1(!*next_point);
  792. s->internal->packet_buffer_end = this_pktl;
  793. next_non_null:
  794. this_pktl->next = *next_point;
  795. s->streams[pkt->stream_index]->last_in_packet_buffer =
  796. *next_point = this_pktl;
  797. av_packet_unref(pkt);
  798. return 0;
  799. }
  800. static int interleave_compare_dts(AVFormatContext *s, AVPacket *next,
  801. AVPacket *pkt)
  802. {
  803. AVStream *st = s->streams[pkt->stream_index];
  804. AVStream *st2 = s->streams[next->stream_index];
  805. int comp = av_compare_ts(next->dts, st2->time_base, pkt->dts,
  806. st->time_base);
  807. if (s->audio_preload && ((st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) != (st2->codecpar->codec_type == AVMEDIA_TYPE_AUDIO))) {
  808. int64_t ts = av_rescale_q(pkt ->dts, st ->time_base, AV_TIME_BASE_Q) - s->audio_preload*(st ->codecpar->codec_type == AVMEDIA_TYPE_AUDIO);
  809. int64_t ts2= av_rescale_q(next->dts, st2->time_base, AV_TIME_BASE_Q) - s->audio_preload*(st2->codecpar->codec_type == AVMEDIA_TYPE_AUDIO);
  810. if (ts == ts2) {
  811. ts= ( pkt ->dts* st->time_base.num*AV_TIME_BASE - s->audio_preload*(int64_t)(st ->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)* st->time_base.den)*st2->time_base.den
  812. -( next->dts*st2->time_base.num*AV_TIME_BASE - s->audio_preload*(int64_t)(st2->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)*st2->time_base.den)* st->time_base.den;
  813. ts2=0;
  814. }
  815. comp= (ts>ts2) - (ts<ts2);
  816. }
  817. if (comp == 0)
  818. return pkt->stream_index < next->stream_index;
  819. return comp > 0;
  820. }
  821. int ff_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out,
  822. AVPacket *pkt, int flush)
  823. {
  824. AVPacketList *pktl;
  825. int stream_count = 0;
  826. int noninterleaved_count = 0;
  827. int i, ret;
  828. if (pkt) {
  829. if ((ret = ff_interleave_add_packet(s, pkt, interleave_compare_dts)) < 0)
  830. return ret;
  831. }
  832. for (i = 0; i < s->nb_streams; i++) {
  833. if (s->streams[i]->last_in_packet_buffer) {
  834. ++stream_count;
  835. } else if (s->streams[i]->codecpar->codec_type != AVMEDIA_TYPE_ATTACHMENT &&
  836. s->streams[i]->codecpar->codec_id != AV_CODEC_ID_VP8 &&
  837. s->streams[i]->codecpar->codec_id != AV_CODEC_ID_VP9) {
  838. ++noninterleaved_count;
  839. }
  840. }
  841. if (s->internal->nb_interleaved_streams == stream_count)
  842. flush = 1;
  843. if (s->max_interleave_delta > 0 &&
  844. s->internal->packet_buffer &&
  845. !flush &&
  846. s->internal->nb_interleaved_streams == stream_count+noninterleaved_count
  847. ) {
  848. AVPacket *top_pkt = &s->internal->packet_buffer->pkt;
  849. int64_t delta_dts = INT64_MIN;
  850. int64_t top_dts = av_rescale_q(top_pkt->dts,
  851. s->streams[top_pkt->stream_index]->time_base,
  852. AV_TIME_BASE_Q);
  853. for (i = 0; i < s->nb_streams; i++) {
  854. int64_t last_dts;
  855. const AVPacketList *last = s->streams[i]->last_in_packet_buffer;
  856. if (!last)
  857. continue;
  858. last_dts = av_rescale_q(last->pkt.dts,
  859. s->streams[i]->time_base,
  860. AV_TIME_BASE_Q);
  861. delta_dts = FFMAX(delta_dts, last_dts - top_dts);
  862. }
  863. if (delta_dts > s->max_interleave_delta) {
  864. av_log(s, AV_LOG_DEBUG,
  865. "Delay between the first packet and last packet in the "
  866. "muxing queue is %"PRId64" > %"PRId64": forcing output\n",
  867. delta_dts, s->max_interleave_delta);
  868. flush = 1;
  869. }
  870. }
  871. if (stream_count && flush) {
  872. AVStream *st;
  873. pktl = s->internal->packet_buffer;
  874. *out = pktl->pkt;
  875. st = s->streams[out->stream_index];
  876. s->internal->packet_buffer = pktl->next;
  877. if (!s->internal->packet_buffer)
  878. s->internal->packet_buffer_end = NULL;
  879. if (st->last_in_packet_buffer == pktl)
  880. st->last_in_packet_buffer = NULL;
  881. av_freep(&pktl);
  882. return 1;
  883. } else {
  884. av_init_packet(out);
  885. return 0;
  886. }
  887. }
  888. /**
  889. * Interleave an AVPacket correctly so it can be muxed.
  890. * @param out the interleaved packet will be output here
  891. * @param in the input packet
  892. * @param flush 1 if no further packets are available as input and all
  893. * remaining packets should be output
  894. * @return 1 if a packet was output, 0 if no packet could be output,
  895. * < 0 if an error occurred
  896. */
  897. static int interleave_packet(AVFormatContext *s, AVPacket *out, AVPacket *in, int flush)
  898. {
  899. if (s->oformat->interleave_packet) {
  900. int ret = s->oformat->interleave_packet(s, out, in, flush);
  901. if (in)
  902. av_packet_unref(in);
  903. return ret;
  904. } else
  905. return ff_interleave_packet_per_dts(s, out, in, flush);
  906. }
  907. int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt)
  908. {
  909. int ret, flush = 0;
  910. ret = prepare_input_packet(s, pkt);
  911. if (ret < 0)
  912. goto fail;
  913. if (pkt) {
  914. AVStream *st = s->streams[pkt->stream_index];
  915. if (s->oformat->check_bitstream) {
  916. if (!st->internal->bitstream_checked) {
  917. if ((ret = s->oformat->check_bitstream(s, pkt)) < 0)
  918. goto fail;
  919. else if (ret == 1)
  920. st->internal->bitstream_checked = 1;
  921. }
  922. }
  923. av_apply_bitstream_filters(st->internal->avctx, pkt, st->internal->bsfc);
  924. if (pkt->size == 0 && pkt->side_data_elems == 0)
  925. return 0;
  926. if (!st->codecpar->extradata && st->internal->avctx->extradata) {
  927. int eret = ff_alloc_extradata(st->codecpar, st->internal->avctx->extradata_size);
  928. if (eret < 0)
  929. return AVERROR(ENOMEM);
  930. st->codecpar->extradata_size = st->internal->avctx->extradata_size;
  931. memcpy(st->codecpar->extradata, st->internal->avctx->extradata, st->internal->avctx->extradata_size);
  932. }
  933. if (s->debug & FF_FDEBUG_TS)
  934. av_log(s, AV_LOG_TRACE, "av_interleaved_write_frame size:%d dts:%s pts:%s\n",
  935. pkt->size, av_ts2str(pkt->dts), av_ts2str(pkt->pts));
  936. #if FF_API_COMPUTE_PKT_FIELDS2
  937. if ((ret = compute_muxer_pkt_fields(s, st, pkt)) < 0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
  938. goto fail;
  939. #endif
  940. if (pkt->dts == AV_NOPTS_VALUE && !(s->oformat->flags & AVFMT_NOTIMESTAMPS)) {
  941. ret = AVERROR(EINVAL);
  942. goto fail;
  943. }
  944. } else {
  945. av_log(s, AV_LOG_TRACE, "av_interleaved_write_frame FLUSH\n");
  946. flush = 1;
  947. }
  948. for (;; ) {
  949. AVPacket opkt;
  950. int ret = interleave_packet(s, &opkt, pkt, flush);
  951. if (pkt) {
  952. memset(pkt, 0, sizeof(*pkt));
  953. av_init_packet(pkt);
  954. pkt = NULL;
  955. }
  956. if (ret <= 0) //FIXME cleanup needed for ret<0 ?
  957. return ret;
  958. ret = write_packet(s, &opkt);
  959. if (ret >= 0)
  960. s->streams[opkt.stream_index]->nb_frames++;
  961. av_packet_unref(&opkt);
  962. if (ret < 0)
  963. return ret;
  964. if(s->pb && s->pb->error)
  965. return s->pb->error;
  966. }
  967. fail:
  968. av_packet_unref(pkt);
  969. return ret;
  970. }
  971. int av_write_trailer(AVFormatContext *s)
  972. {
  973. int ret, i;
  974. for (;; ) {
  975. AVPacket pkt;
  976. ret = interleave_packet(s, &pkt, NULL, 1);
  977. if (ret < 0)
  978. goto fail;
  979. if (!ret)
  980. break;
  981. ret = write_packet(s, &pkt);
  982. if (ret >= 0)
  983. s->streams[pkt.stream_index]->nb_frames++;
  984. av_packet_unref(&pkt);
  985. if (ret < 0)
  986. goto fail;
  987. if(s->pb && s->pb->error)
  988. goto fail;
  989. }
  990. if (!s->internal->header_written && s->oformat->write_header) {
  991. ret = s->oformat->write_header(s);
  992. if (ret >= 0 && s->pb && s->pb->error < 0)
  993. ret = s->pb->error;
  994. if (ret < 0)
  995. goto fail;
  996. if (s->flush_packets && s->pb && s->pb->error >= 0 && s->flags & AVFMT_FLAG_FLUSH_PACKETS)
  997. avio_flush(s->pb);
  998. s->internal->header_written = 1;
  999. }
  1000. fail:
  1001. if ((s->internal->header_written || !s->oformat->write_header) && s->oformat->write_trailer)
  1002. if (ret >= 0) {
  1003. ret = s->oformat->write_trailer(s);
  1004. } else {
  1005. s->oformat->write_trailer(s);
  1006. }
  1007. if (s->oformat->deinit)
  1008. s->oformat->deinit(s);
  1009. if (s->pb)
  1010. avio_flush(s->pb);
  1011. if (ret == 0)
  1012. ret = s->pb ? s->pb->error : 0;
  1013. for (i = 0; i < s->nb_streams; i++) {
  1014. av_freep(&s->streams[i]->priv_data);
  1015. av_freep(&s->streams[i]->index_entries);
  1016. }
  1017. if (s->oformat->priv_class)
  1018. av_opt_free(s->priv_data);
  1019. av_freep(&s->priv_data);
  1020. return ret;
  1021. }
  1022. int av_get_output_timestamp(struct AVFormatContext *s, int stream,
  1023. int64_t *dts, int64_t *wall)
  1024. {
  1025. if (!s->oformat || !s->oformat->get_output_timestamp)
  1026. return AVERROR(ENOSYS);
  1027. s->oformat->get_output_timestamp(s, stream, dts, wall);
  1028. return 0;
  1029. }
  1030. int ff_write_chained(AVFormatContext *dst, int dst_stream, AVPacket *pkt,
  1031. AVFormatContext *src, int interleave)
  1032. {
  1033. AVPacket local_pkt;
  1034. int ret;
  1035. local_pkt = *pkt;
  1036. local_pkt.stream_index = dst_stream;
  1037. if (pkt->pts != AV_NOPTS_VALUE)
  1038. local_pkt.pts = av_rescale_q(pkt->pts,
  1039. src->streams[pkt->stream_index]->time_base,
  1040. dst->streams[dst_stream]->time_base);
  1041. if (pkt->dts != AV_NOPTS_VALUE)
  1042. local_pkt.dts = av_rescale_q(pkt->dts,
  1043. src->streams[pkt->stream_index]->time_base,
  1044. dst->streams[dst_stream]->time_base);
  1045. if (pkt->duration)
  1046. local_pkt.duration = av_rescale_q(pkt->duration,
  1047. src->streams[pkt->stream_index]->time_base,
  1048. dst->streams[dst_stream]->time_base);
  1049. if (interleave) ret = av_interleaved_write_frame(dst, &local_pkt);
  1050. else ret = av_write_frame(dst, &local_pkt);
  1051. pkt->buf = local_pkt.buf;
  1052. pkt->side_data = local_pkt.side_data;
  1053. pkt->side_data_elems = local_pkt.side_data_elems;
  1054. return ret;
  1055. }
  1056. static int av_write_uncoded_frame_internal(AVFormatContext *s, int stream_index,
  1057. AVFrame *frame, int interleaved)
  1058. {
  1059. AVPacket pkt, *pktp;
  1060. av_assert0(s->oformat);
  1061. if (!s->oformat->write_uncoded_frame)
  1062. return AVERROR(ENOSYS);
  1063. if (!frame) {
  1064. pktp = NULL;
  1065. } else {
  1066. pktp = &pkt;
  1067. av_init_packet(&pkt);
  1068. pkt.data = (void *)frame;
  1069. pkt.size = UNCODED_FRAME_PACKET_SIZE;
  1070. pkt.pts =
  1071. pkt.dts = frame->pts;
  1072. pkt.duration = av_frame_get_pkt_duration(frame);
  1073. pkt.stream_index = stream_index;
  1074. pkt.flags |= AV_PKT_FLAG_UNCODED_FRAME;
  1075. }
  1076. return interleaved ? av_interleaved_write_frame(s, pktp) :
  1077. av_write_frame(s, pktp);
  1078. }
  1079. int av_write_uncoded_frame(AVFormatContext *s, int stream_index,
  1080. AVFrame *frame)
  1081. {
  1082. return av_write_uncoded_frame_internal(s, stream_index, frame, 0);
  1083. }
  1084. int av_interleaved_write_uncoded_frame(AVFormatContext *s, int stream_index,
  1085. AVFrame *frame)
  1086. {
  1087. return av_write_uncoded_frame_internal(s, stream_index, frame, 1);
  1088. }
  1089. int av_write_uncoded_frame_query(AVFormatContext *s, int stream_index)
  1090. {
  1091. av_assert0(s->oformat);
  1092. if (!s->oformat->write_uncoded_frame)
  1093. return AVERROR(ENOSYS);
  1094. return s->oformat->write_uncoded_frame(s, stream_index, NULL,
  1095. AV_WRITE_UNCODED_FRAME_QUERY);
  1096. }