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