FFmpeg  2.6.9
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules 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  const char *format, const char *filename)
120 {
122  int ret = 0;
123 
124  *avctx = NULL;
125  if (!s)
126  goto nomem;
127 
128  if (!oformat) {
129  if (format) {
130  oformat = av_guess_format(format, NULL, NULL);
131  if (!oformat) {
132  av_log(s, AV_LOG_ERROR, "Requested output format '%s' is not a suitable output format\n", format);
133  ret = AVERROR(EINVAL);
134  goto error;
135  }
136  } else {
137  oformat = av_guess_format(NULL, filename, NULL);
138  if (!oformat) {
139  ret = AVERROR(EINVAL);
140  av_log(s, AV_LOG_ERROR, "Unable to find a suitable output format for '%s'\n",
141  filename);
142  goto error;
143  }
144  }
145  }
146 
147  s->oformat = oformat;
148  if (s->oformat->priv_data_size > 0) {
150  if (!s->priv_data)
151  goto nomem;
152  if (s->oformat->priv_class) {
153  *(const AVClass**)s->priv_data= s->oformat->priv_class;
155  }
156  } else
157  s->priv_data = NULL;
158 
159  if (filename)
160  av_strlcpy(s->filename, filename, sizeof(s->filename));
161  *avctx = s;
162  return 0;
163 nomem:
164  av_log(s, AV_LOG_ERROR, "Out of memory\n");
165  ret = AVERROR(ENOMEM);
166 error:
168  return ret;
169 }
170 
172 {
173  const AVCodecTag *avctag;
174  int n;
175  enum AVCodecID id = AV_CODEC_ID_NONE;
176  int64_t tag = -1;
177 
178  /**
179  * Check that tag + id is in the table
180  * If neither is in the table -> OK
181  * If tag is in the table with another id -> FAIL
182  * If id is in the table with another tag -> FAIL unless strict < normal
183  */
184  for (n = 0; s->oformat->codec_tag[n]; n++) {
185  avctag = s->oformat->codec_tag[n];
186  while (avctag->id != AV_CODEC_ID_NONE) {
187  if (avpriv_toupper4(avctag->tag) == avpriv_toupper4(st->codec->codec_tag)) {
188  id = avctag->id;
189  if (id == st->codec->codec_id)
190  return 1;
191  }
192  if (avctag->id == st->codec->codec_id)
193  tag = avctag->tag;
194  avctag++;
195  }
196  }
197  if (id != AV_CODEC_ID_NONE)
198  return 0;
199  if (tag >= 0 && (s->strict_std_compliance >= FF_COMPLIANCE_NORMAL))
200  return 0;
201  return 1;
202 }
203 
204 
206 {
207  int ret = 0, i;
208  AVStream *st;
209  AVDictionary *tmp = NULL;
210  AVCodecContext *codec = NULL;
211  AVOutputFormat *of = s->oformat;
213 
214  if (options)
215  av_dict_copy(&tmp, *options, 0);
216 
217  if ((ret = av_opt_set_dict(s, &tmp)) < 0)
218  goto fail;
219  if (s->priv_data && s->oformat->priv_class && *(const AVClass**)s->priv_data==s->oformat->priv_class &&
220  (ret = av_opt_set_dict2(s->priv_data, &tmp, AV_OPT_SEARCH_CHILDREN)) < 0)
221  goto fail;
222 
223 #if FF_API_LAVF_BITEXACT
224  if (s->nb_streams && s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT)
226 #endif
227 
228  // some sanity checks
229  if (s->nb_streams == 0 && !(of->flags & AVFMT_NOSTREAMS)) {
230  av_log(s, AV_LOG_ERROR, "No streams to mux were specified\n");
231  ret = AVERROR(EINVAL);
232  goto fail;
233  }
234 
235  for (i = 0; i < s->nb_streams; i++) {
236  st = s->streams[i];
237  codec = st->codec;
238 
239 #if FF_API_LAVF_CODEC_TB
241  if (!st->time_base.num && codec->time_base.num) {
242  av_log(s, AV_LOG_WARNING, "Using AVStream.codec.time_base as a "
243  "timebase hint to the muxer is deprecated. Set "
244  "AVStream.time_base instead.\n");
245  avpriv_set_pts_info(st, 64, codec->time_base.num, codec->time_base.den);
246  }
248 #endif
249 
250  if (!st->time_base.num) {
251  /* fall back on the default timebase values */
252  if (codec->codec_type == AVMEDIA_TYPE_AUDIO && codec->sample_rate)
253  avpriv_set_pts_info(st, 64, 1, codec->sample_rate);
254  else
255  avpriv_set_pts_info(st, 33, 1, 90000);
256  }
257 
258  switch (codec->codec_type) {
259  case AVMEDIA_TYPE_AUDIO:
260  if (codec->sample_rate <= 0) {
261  av_log(s, AV_LOG_ERROR, "sample rate not set\n");
262  ret = AVERROR(EINVAL);
263  goto fail;
264  }
265  if (!codec->block_align)
266  codec->block_align = codec->channels *
267  av_get_bits_per_sample(codec->codec_id) >> 3;
268  break;
269  case AVMEDIA_TYPE_VIDEO:
270  if ((codec->width <= 0 || codec->height <= 0) &&
271  !(of->flags & AVFMT_NODIMENSIONS)) {
272  av_log(s, AV_LOG_ERROR, "dimensions not set\n");
273  ret = AVERROR(EINVAL);
274  goto fail;
275  }
278  ) {
279  if (st->sample_aspect_ratio.num != 0 &&
280  st->sample_aspect_ratio.den != 0 &&
281  codec->sample_aspect_ratio.num != 0 &&
282  codec->sample_aspect_ratio.den != 0) {
283  av_log(s, AV_LOG_ERROR, "Aspect ratio mismatch between muxer "
284  "(%d/%d) and encoder layer (%d/%d)\n",
286  codec->sample_aspect_ratio.num,
287  codec->sample_aspect_ratio.den);
288  ret = AVERROR(EINVAL);
289  goto fail;
290  }
291  }
292  break;
293  }
294 
295  if (of->codec_tag) {
296  if ( codec->codec_tag
297  && codec->codec_id == AV_CODEC_ID_RAWVIDEO
298  && ( av_codec_get_tag(of->codec_tag, codec->codec_id) == 0
299  || av_codec_get_tag(of->codec_tag, codec->codec_id) == MKTAG('r', 'a', 'w', ' '))
300  && !validate_codec_tag(s, st)) {
301  // the current rawvideo encoding system ends up setting
302  // the wrong codec_tag for avi/mov, we override it here
303  codec->codec_tag = 0;
304  }
305  if (codec->codec_tag) {
306  if (!validate_codec_tag(s, st)) {
307  char tagbuf[32], tagbuf2[32];
308  av_get_codec_tag_string(tagbuf, sizeof(tagbuf), codec->codec_tag);
309  av_get_codec_tag_string(tagbuf2, sizeof(tagbuf2), av_codec_get_tag(s->oformat->codec_tag, codec->codec_id));
310  av_log(s, AV_LOG_ERROR,
311  "Tag %s/0x%08x incompatible with output codec id '%d' (%s)\n",
312  tagbuf, codec->codec_tag, codec->codec_id, tagbuf2);
313  ret = AVERROR_INVALIDDATA;
314  goto fail;
315  }
316  } else
317  codec->codec_tag = av_codec_get_tag(of->codec_tag, codec->codec_id);
318  }
319 
320  if (of->flags & AVFMT_GLOBALHEADER &&
321  !(codec->flags & CODEC_FLAG_GLOBAL_HEADER))
323  "Codec for stream %d does not use global headers "
324  "but container format requires global headers\n", i);
325 
326  if (codec->codec_type != AVMEDIA_TYPE_ATTACHMENT)
328  }
329 
330  if (!s->priv_data && of->priv_data_size > 0) {
332  if (!s->priv_data) {
333  ret = AVERROR(ENOMEM);
334  goto fail;
335  }
336  if (of->priv_class) {
337  *(const AVClass **)s->priv_data = of->priv_class;
339  if ((ret = av_opt_set_dict2(s->priv_data, &tmp, AV_OPT_SEARCH_CHILDREN)) < 0)
340  goto fail;
341  }
342  }
343 
344  /* set muxer identification string */
345  if (!(s->flags & AVFMT_FLAG_BITEXACT)) {
346  av_dict_set(&s->metadata, "encoder", LIBAVFORMAT_IDENT, 0);
347  } else {
348  av_dict_set(&s->metadata, "encoder", NULL, 0);
349  }
350 
351  for (e = NULL; e = av_dict_get(s->metadata, "encoder-", e, AV_DICT_IGNORE_SUFFIX); ) {
352  av_dict_set(&s->metadata, e->key, NULL, 0);
353  }
354 
355  if (options) {
356  av_dict_free(options);
357  *options = tmp;
358  }
359 
360  return 0;
361 
362 fail:
363  av_dict_free(&tmp);
364  return ret;
365 }
366 
368 {
369  int i;
370  AVStream *st;
371 
372  /* init PTS generation */
373  for (i = 0; i < s->nb_streams; i++) {
374  int64_t den = AV_NOPTS_VALUE;
375  st = s->streams[i];
376 
377  switch (st->codec->codec_type) {
378  case AVMEDIA_TYPE_AUDIO:
379  den = (int64_t)st->time_base.num * st->codec->sample_rate;
380  break;
381  case AVMEDIA_TYPE_VIDEO:
382  den = (int64_t)st->time_base.num * st->codec->time_base.den;
383  break;
384  default:
385  break;
386  }
387  if (den != AV_NOPTS_VALUE) {
388  if (den <= 0)
389  return AVERROR_INVALIDDATA;
390 
391  frac_init(&st->pts, 0, 0, den);
392  }
393  }
394 
395  return 0;
396 }
397 
399 {
400  int ret = 0;
401 
402  if (ret = init_muxer(s, options))
403  return ret;
404 
405  if (s->oformat->write_header) {
406  ret = s->oformat->write_header(s);
407  if (ret >= 0 && s->pb && s->pb->error < 0)
408  ret = s->pb->error;
409  if (ret < 0)
410  return ret;
411  if (s->flush_packets && s->pb && s->pb->error >= 0 && s->flags & AVFMT_FLAG_FLUSH_PACKETS)
412  avio_flush(s->pb);
413  }
414 
415  if ((ret = init_pts(s)) < 0)
416  return ret;
417 
418  if (s->avoid_negative_ts < 0) {
421  s->avoid_negative_ts = 0;
422  } else
424  }
425 
426  return 0;
427 }
428 
429 #define AV_PKT_FLAG_UNCODED_FRAME 0x2000
430 
431 /* Note: using sizeof(AVFrame) from outside lavu is unsafe in general, but
432  it is only being used internally to this file as a consistency check.
433  The value is chosen to be very unlikely to appear on its own and to cause
434  immediate failure if used anywhere as a real size. */
435 #define UNCODED_FRAME_PACKET_SIZE (INT_MIN / 3 * 2 + (int)sizeof(AVFrame))
436 
437 
438 //FIXME merge with compute_pkt_fields
440 {
441  int delay = FFMAX(st->codec->has_b_frames, st->codec->max_b_frames > 0);
442  int num, den, i;
443  int frame_size;
444 
445  av_dlog(s, "compute_pkt_fields2: pts:%s dts:%s cur_dts:%s b:%d size:%d st:%d\n",
446  av_ts2str(pkt->pts), av_ts2str(pkt->dts), av_ts2str(st->cur_dts), delay, pkt->size, pkt->stream_index);
447 
448  if (pkt->duration < 0 && st->codec->codec_type != AVMEDIA_TYPE_SUBTITLE) {
449  av_log(s, AV_LOG_WARNING, "Packet with invalid duration %d in stream %d\n",
450  pkt->duration, pkt->stream_index);
451  pkt->duration = 0;
452  }
453 
454  /* duration field */
455  if (pkt->duration == 0) {
456  ff_compute_frame_duration(s, &num, &den, st, NULL, pkt);
457  if (den && num) {
458  pkt->duration = av_rescale(1, num * (int64_t)st->time_base.den * st->codec->ticks_per_frame, den * (int64_t)st->time_base.num);
459  }
460  }
461 
462  if (pkt->pts == AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE && delay == 0)
463  pkt->pts = pkt->dts;
464 
465  //XXX/FIXME this is a temporary hack until all encoders output pts
466  if ((pkt->pts == 0 || pkt->pts == AV_NOPTS_VALUE) && pkt->dts == AV_NOPTS_VALUE && !delay) {
467  static int warned;
468  if (!warned) {
469  av_log(s, AV_LOG_WARNING, "Encoder did not produce proper pts, making some up.\n");
470  warned = 1;
471  }
472  pkt->dts =
473 // pkt->pts= st->cur_dts;
474  pkt->pts = st->pts.val;
475  }
476 
477  //calculate dts from pts
478  if (pkt->pts != AV_NOPTS_VALUE && pkt->dts == AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY) {
479  st->pts_buffer[0] = pkt->pts;
480  for (i = 1; i < delay + 1 && st->pts_buffer[i] == AV_NOPTS_VALUE; i++)
481  st->pts_buffer[i] = pkt->pts + (i - delay - 1) * pkt->duration;
482  for (i = 0; i<delay && st->pts_buffer[i] > st->pts_buffer[i + 1]; i++)
483  FFSWAP(int64_t, st->pts_buffer[i], st->pts_buffer[i + 1]);
484 
485  pkt->dts = st->pts_buffer[0];
486  }
487 
488  if (st->cur_dts && st->cur_dts != AV_NOPTS_VALUE &&
489  ((!(s->oformat->flags & AVFMT_TS_NONSTRICT) &&
491  st->cur_dts >= pkt->dts) || st->cur_dts > pkt->dts)) {
492  av_log(s, AV_LOG_ERROR,
493  "Application provided invalid, non monotonically increasing dts to muxer in stream %d: %s >= %s\n",
494  st->index, av_ts2str(st->cur_dts), av_ts2str(pkt->dts));
495  return AVERROR(EINVAL);
496  }
497  if (pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE && pkt->pts < pkt->dts) {
498  av_log(s, AV_LOG_ERROR,
499  "pts (%s) < dts (%s) in stream %d\n",
500  av_ts2str(pkt->pts), av_ts2str(pkt->dts),
501  st->index);
502  return AVERROR(EINVAL);
503  }
504 
505  av_dlog(s, "av_write_frame: pts2:%s dts2:%s\n",
506  av_ts2str(pkt->pts), av_ts2str(pkt->dts));
507  st->cur_dts = pkt->dts;
508  st->pts.val = pkt->dts;
509 
510  /* update pts */
511  switch (st->codec->codec_type) {
512  case AVMEDIA_TYPE_AUDIO:
513  frame_size = (pkt->flags & AV_PKT_FLAG_UNCODED_FRAME) ?
514  ((AVFrame *)pkt->data)->nb_samples :
516 
517  /* HACK/FIXME, we skip the initial 0 size packets as they are most
518  * likely equal to the encoder delay, but it would be better if we
519  * had the real timestamps from the encoder */
520  if (frame_size >= 0 && (pkt->size || st->pts.num != st->pts.den >> 1 || st->pts.val)) {
521  frac_add(&st->pts, (int64_t)st->time_base.den * frame_size);
522  }
523  break;
524  case AVMEDIA_TYPE_VIDEO:
525  frac_add(&st->pts, (int64_t)st->time_base.den * st->codec->time_base.num);
526  break;
527  }
528  return 0;
529 }
530 
531 /**
532  * Make timestamps non negative, move side data from payload to internal struct, call muxer, and restore
533  * sidedata.
534  *
535  * FIXME: this function should NEVER get undefined pts/dts beside when the
536  * AVFMT_NOTIMESTAMPS is set.
537  * Those additional safety checks should be dropped once the correct checks
538  * are set in the callers.
539  */
541 {
542  int ret, did_split;
543 
544  if (s->output_ts_offset) {
545  AVStream *st = s->streams[pkt->stream_index];
547 
548  if (pkt->dts != AV_NOPTS_VALUE)
549  pkt->dts += offset;
550  if (pkt->pts != AV_NOPTS_VALUE)
551  pkt->pts += offset;
552  }
553 
554  if (s->avoid_negative_ts > 0) {
555  AVStream *st = s->streams[pkt->stream_index];
556  int64_t offset = st->mux_ts_offset;
557  int64_t ts = s->internal->avoid_negative_ts_use_pts ? pkt->pts : pkt->dts;
558 
559  if (s->internal->offset == AV_NOPTS_VALUE && ts != AV_NOPTS_VALUE &&
560  (ts < 0 || s->avoid_negative_ts == AVFMT_AVOID_NEG_TS_MAKE_ZERO)) {
561  s->internal->offset = -ts;
563  }
564 
565  if (s->internal->offset != AV_NOPTS_VALUE && !offset) {
566  offset = st->mux_ts_offset =
569  st->time_base,
570  AV_ROUND_UP);
571  }
572 
573  if (pkt->dts != AV_NOPTS_VALUE)
574  pkt->dts += offset;
575  if (pkt->pts != AV_NOPTS_VALUE)
576  pkt->pts += offset;
577 
579  if (pkt->pts != AV_NOPTS_VALUE && pkt->pts < 0) {
580  av_log(s, AV_LOG_WARNING, "failed to avoid negative "
581  "pts %s in stream %d.\n"
582  "Try -avoid_negative_ts 1 as a possible workaround.\n",
583  av_ts2str(pkt->dts),
584  pkt->stream_index
585  );
586  }
587  } else {
588  av_assert2(pkt->dts == AV_NOPTS_VALUE || pkt->dts >= 0 || s->max_interleave_delta > 0);
589  if (pkt->dts != AV_NOPTS_VALUE && pkt->dts < 0) {
591  "Packets poorly interleaved, failed to avoid negative "
592  "timestamp %s in stream %d.\n"
593  "Try -max_interleave_delta 0 as a possible workaround.\n",
594  av_ts2str(pkt->dts),
595  pkt->stream_index
596  );
597  }
598  }
599  }
600 
601  did_split = av_packet_split_side_data(pkt);
602  if ((pkt->flags & AV_PKT_FLAG_UNCODED_FRAME)) {
603  AVFrame *frame = (AVFrame *)pkt->data;
605  ret = s->oformat->write_uncoded_frame(s, pkt->stream_index, &frame, 0);
606  av_frame_free(&frame);
607  } else {
608  ret = s->oformat->write_packet(s, pkt);
609  }
610 
611  if (s->flush_packets && s->pb && ret >= 0 && s->flags & AVFMT_FLAG_FLUSH_PACKETS)
612  avio_flush(s->pb);
613 
614  if (did_split)
616 
617  return ret;
618 }
619 
621 {
622  if (!pkt)
623  return 0;
624 
625  if (pkt->stream_index < 0 || pkt->stream_index >= s->nb_streams) {
626  av_log(s, AV_LOG_ERROR, "Invalid packet stream index: %d\n",
627  pkt->stream_index);
628  return AVERROR(EINVAL);
629  }
630 
632  av_log(s, AV_LOG_ERROR, "Received a packet for an attachment stream.\n");
633  return AVERROR(EINVAL);
634  }
635 
636  return 0;
637 }
638 
640 {
641  int ret;
642 
643  ret = check_packet(s, pkt);
644  if (ret < 0)
645  return ret;
646 
647  if (!pkt) {
648  if (s->oformat->flags & AVFMT_ALLOW_FLUSH) {
649  ret = s->oformat->write_packet(s, NULL);
650  if (s->flush_packets && s->pb && s->pb->error >= 0 && s->flags & AVFMT_FLAG_FLUSH_PACKETS)
651  avio_flush(s->pb);
652  if (ret >= 0 && s->pb && s->pb->error < 0)
653  ret = s->pb->error;
654  return ret;
655  }
656  return 1;
657  }
658 
659  ret = compute_pkt_fields2(s, s->streams[pkt->stream_index], pkt);
660 
661  if (ret < 0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
662  return ret;
663 
664  ret = write_packet(s, pkt);
665  if (ret >= 0 && s->pb && s->pb->error < 0)
666  ret = s->pb->error;
667 
668  if (ret >= 0)
669  s->streams[pkt->stream_index]->nb_frames++;
670  return ret;
671 }
672 
673 #define CHUNK_START 0x1000
674 
676  int (*compare)(AVFormatContext *, AVPacket *, AVPacket *))
677 {
678  int ret;
679  AVPacketList **next_point, *this_pktl;
680  AVStream *st = s->streams[pkt->stream_index];
681  int chunked = s->max_chunk_size || s->max_chunk_duration;
682 
683  this_pktl = av_mallocz(sizeof(AVPacketList));
684  if (!this_pktl)
685  return AVERROR(ENOMEM);
686  this_pktl->pkt = *pkt;
687 #if FF_API_DESTRUCT_PACKET
689  pkt->destruct = NULL; // do not free original but only the copy
691 #endif
692  pkt->buf = NULL;
693  pkt->side_data = NULL;
694  pkt->side_data_elems = 0;
695  if ((pkt->flags & AV_PKT_FLAG_UNCODED_FRAME)) {
697  av_assert0(((AVFrame *)pkt->data)->buf);
698  } else {
699  // Duplicate the packet if it uses non-allocated memory
700  if ((ret = av_dup_packet(&this_pktl->pkt)) < 0) {
701  av_free(this_pktl);
702  return ret;
703  }
704  }
705 
707  next_point = &(st->last_in_packet_buffer->next);
708  } else {
709  next_point = &s->internal->packet_buffer;
710  }
711 
712  if (chunked) {
714  st->interleaver_chunk_size += pkt->size;
717  || (max && st->interleaver_chunk_duration > max)) {
718  st->interleaver_chunk_size = 0;
719  this_pktl->pkt.flags |= CHUNK_START;
720  if (max && st->interleaver_chunk_duration > max) {
721  int64_t syncoffset = (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)*max/2;
722  int64_t syncto = av_rescale(pkt->dts + syncoffset, 1, max)*max - syncoffset;
723 
724  st->interleaver_chunk_duration += (pkt->dts - syncto)/8 - max;
725  } else
727  }
728  }
729  if (*next_point) {
730  if (chunked && !(this_pktl->pkt.flags & CHUNK_START))
731  goto next_non_null;
732 
733  if (compare(s, &s->internal->packet_buffer_end->pkt, pkt)) {
734  while ( *next_point
735  && ((chunked && !((*next_point)->pkt.flags&CHUNK_START))
736  || !compare(s, &(*next_point)->pkt, pkt)))
737  next_point = &(*next_point)->next;
738  if (*next_point)
739  goto next_non_null;
740  } else {
741  next_point = &(s->internal->packet_buffer_end->next);
742  }
743  }
744  av_assert1(!*next_point);
745 
746  s->internal->packet_buffer_end = this_pktl;
747 next_non_null:
748 
749  this_pktl->next = *next_point;
750 
752  *next_point = this_pktl;
753 
754  return 0;
755 }
756 
758  AVPacket *pkt)
759 {
760  AVStream *st = s->streams[pkt->stream_index];
761  AVStream *st2 = s->streams[next->stream_index];
762  int comp = av_compare_ts(next->dts, st2->time_base, pkt->dts,
763  st->time_base);
765  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);
766  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);
767  if (ts == ts2) {
768  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
769  -( 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;
770  ts2=0;
771  }
772  comp= (ts>ts2) - (ts<ts2);
773  }
774 
775  if (comp == 0)
776  return pkt->stream_index < next->stream_index;
777  return comp > 0;
778 }
779 
781  AVPacket *pkt, int flush)
782 {
783  AVPacketList *pktl;
784  int stream_count = 0;
785  int noninterleaved_count = 0;
786  int i, ret;
787 
788  if (pkt) {
789  if ((ret = ff_interleave_add_packet(s, pkt, interleave_compare_dts)) < 0)
790  return ret;
791  }
792 
793  for (i = 0; i < s->nb_streams; i++) {
794  if (s->streams[i]->last_in_packet_buffer) {
795  ++stream_count;
796  } else if (s->streams[i]->codec->codec_type != AVMEDIA_TYPE_ATTACHMENT &&
797  s->streams[i]->codec->codec_id != AV_CODEC_ID_VP8 &&
798  s->streams[i]->codec->codec_id != AV_CODEC_ID_VP9) {
799  ++noninterleaved_count;
800  }
801  }
802 
803  if (s->internal->nb_interleaved_streams == stream_count)
804  flush = 1;
805 
806  if (s->max_interleave_delta > 0 &&
807  s->internal->packet_buffer &&
808  !flush &&
809  s->internal->nb_interleaved_streams == stream_count+noninterleaved_count
810  ) {
811  AVPacket *top_pkt = &s->internal->packet_buffer->pkt;
812  int64_t delta_dts = INT64_MIN;
813  int64_t top_dts = av_rescale_q(top_pkt->dts,
814  s->streams[top_pkt->stream_index]->time_base,
816 
817  for (i = 0; i < s->nb_streams; i++) {
818  int64_t last_dts;
819  const AVPacketList *last = s->streams[i]->last_in_packet_buffer;
820 
821  if (!last)
822  continue;
823 
824  last_dts = av_rescale_q(last->pkt.dts,
825  s->streams[i]->time_base,
827  delta_dts = FFMAX(delta_dts, last_dts - top_dts);
828  }
829 
830  if (delta_dts > s->max_interleave_delta) {
831  av_log(s, AV_LOG_DEBUG,
832  "Delay between the first packet and last packet in the "
833  "muxing queue is %"PRId64" > %"PRId64": forcing output\n",
834  delta_dts, s->max_interleave_delta);
835  flush = 1;
836  }
837  }
838 
839  if (stream_count && flush) {
840  AVStream *st;
841  pktl = s->internal->packet_buffer;
842  *out = pktl->pkt;
843  st = s->streams[out->stream_index];
844 
845  s->internal->packet_buffer = pktl->next;
846  if (!s->internal->packet_buffer)
848 
849  if (st->last_in_packet_buffer == pktl)
851  av_freep(&pktl);
852 
853  return 1;
854  } else {
855  av_init_packet(out);
856  return 0;
857  }
858 }
859 
860 /**
861  * Interleave an AVPacket correctly so it can be muxed.
862  * @param out the interleaved packet will be output here
863  * @param in the input packet
864  * @param flush 1 if no further packets are available as input and all
865  * remaining packets should be output
866  * @return 1 if a packet was output, 0 if no packet could be output,
867  * < 0 if an error occurred
868  */
870 {
871  if (s->oformat->interleave_packet) {
872  int ret = s->oformat->interleave_packet(s, out, in, flush);
873  if (in)
874  av_free_packet(in);
875  return ret;
876  } else
877  return ff_interleave_packet_per_dts(s, out, in, flush);
878 }
879 
881 {
882  int ret, flush = 0;
883 
884  ret = check_packet(s, pkt);
885  if (ret < 0)
886  goto fail;
887 
888  if (pkt) {
889  AVStream *st = s->streams[pkt->stream_index];
890 
891  av_dlog(s, "av_interleaved_write_frame size:%d dts:%s pts:%s\n",
892  pkt->size, av_ts2str(pkt->dts), av_ts2str(pkt->pts));
893  if ((ret = compute_pkt_fields2(s, st, pkt)) < 0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
894  goto fail;
895 
896  if (pkt->dts == AV_NOPTS_VALUE && !(s->oformat->flags & AVFMT_NOTIMESTAMPS)) {
897  ret = AVERROR(EINVAL);
898  goto fail;
899  }
900  } else {
901  av_dlog(s, "av_interleaved_write_frame FLUSH\n");
902  flush = 1;
903  }
904 
905  for (;; ) {
906  AVPacket opkt;
907  int ret = interleave_packet(s, &opkt, pkt, flush);
908  if (pkt) {
909  memset(pkt, 0, sizeof(*pkt));
910  av_init_packet(pkt);
911  pkt = NULL;
912  }
913  if (ret <= 0) //FIXME cleanup needed for ret<0 ?
914  return ret;
915 
916  ret = write_packet(s, &opkt);
917  if (ret >= 0)
918  s->streams[opkt.stream_index]->nb_frames++;
919 
920  av_free_packet(&opkt);
921 
922  if (ret < 0)
923  return ret;
924  if(s->pb && s->pb->error)
925  return s->pb->error;
926  }
927 fail:
928  av_packet_unref(pkt);
929  return ret;
930 }
931 
933 {
934  int ret, i;
935 
936  for (;; ) {
937  AVPacket pkt;
938  ret = interleave_packet(s, &pkt, NULL, 1);
939  if (ret < 0)
940  goto fail;
941  if (!ret)
942  break;
943 
944  ret = write_packet(s, &pkt);
945  if (ret >= 0)
946  s->streams[pkt.stream_index]->nb_frames++;
947 
948  av_free_packet(&pkt);
949 
950  if (ret < 0)
951  goto fail;
952  if(s->pb && s->pb->error)
953  goto fail;
954  }
955 
956 fail:
957  if (s->oformat->write_trailer)
958  if (ret >= 0) {
959  ret = s->oformat->write_trailer(s);
960  } else {
961  s->oformat->write_trailer(s);
962  }
963 
964  if (s->pb)
965  avio_flush(s->pb);
966  if (ret == 0)
967  ret = s->pb ? s->pb->error : 0;
968  for (i = 0; i < s->nb_streams; i++) {
969  av_freep(&s->streams[i]->priv_data);
970  av_freep(&s->streams[i]->index_entries);
971  }
972  if (s->oformat->priv_class)
974  av_freep(&s->priv_data);
975  return ret;
976 }
977 
978 int av_get_output_timestamp(struct AVFormatContext *s, int stream,
979  int64_t *dts, int64_t *wall)
980 {
981  if (!s->oformat || !s->oformat->get_output_timestamp)
982  return AVERROR(ENOSYS);
983  s->oformat->get_output_timestamp(s, stream, dts, wall);
984  return 0;
985 }
986 
987 int ff_write_chained(AVFormatContext *dst, int dst_stream, AVPacket *pkt,
989 {
990  AVPacket local_pkt;
991  int ret;
992 
993  local_pkt = *pkt;
994  local_pkt.stream_index = dst_stream;
995  if (pkt->pts != AV_NOPTS_VALUE)
996  local_pkt.pts = av_rescale_q(pkt->pts,
997  src->streams[pkt->stream_index]->time_base,
998  dst->streams[dst_stream]->time_base);
999  if (pkt->dts != AV_NOPTS_VALUE)
1000  local_pkt.dts = av_rescale_q(pkt->dts,
1001  src->streams[pkt->stream_index]->time_base,
1002  dst->streams[dst_stream]->time_base);
1003  if (pkt->duration)
1004  local_pkt.duration = av_rescale_q(pkt->duration,
1005  src->streams[pkt->stream_index]->time_base,
1006  dst->streams[dst_stream]->time_base);
1007 
1008  if (interleave) ret = av_interleaved_write_frame(dst, &local_pkt);
1009  else ret = av_write_frame(dst, &local_pkt);
1010  pkt->buf = local_pkt.buf;
1011  pkt->side_data = local_pkt.side_data;
1012  pkt->side_data_elems = local_pkt.side_data_elems;
1013  pkt->destruct = local_pkt.destruct;
1014  return ret;
1015 }
1016 
1017 static int av_write_uncoded_frame_internal(AVFormatContext *s, int stream_index,
1018  AVFrame *frame, int interleaved)
1019 {
1020  AVPacket pkt, *pktp;
1021 
1022  av_assert0(s->oformat);
1023  if (!s->oformat->write_uncoded_frame)
1024  return AVERROR(ENOSYS);
1025 
1026  if (!frame) {
1027  pktp = NULL;
1028  } else {
1029  pktp = &pkt;
1030  av_init_packet(&pkt);
1031  pkt.data = (void *)frame;
1033  pkt.pts =
1034  pkt.dts = frame->pts;
1035  pkt.duration = av_frame_get_pkt_duration(frame);
1036  pkt.stream_index = stream_index;
1038  }
1039 
1040  return interleaved ? av_interleaved_write_frame(s, pktp) :
1041  av_write_frame(s, pktp);
1042 }
1043 
1044 int av_write_uncoded_frame(AVFormatContext *s, int stream_index,
1045  AVFrame *frame)
1046 {
1047  return av_write_uncoded_frame_internal(s, stream_index, frame, 0);
1048 }
1049 
1051  AVFrame *frame)
1052 {
1053  return av_write_uncoded_frame_internal(s, stream_index, frame, 1);
1054 }
1055 
1057 {
1058  av_assert0(s->oformat);
1059  if (!s->oformat->write_uncoded_frame)
1060  return AVERROR(ENOSYS);
1061  return s->oformat->write_uncoded_frame(s, stream_index, NULL,
1063 }
int64_t interleaver_chunk_size
Definition: avformat.h:1036
#define NULL
Definition: coverity.c:32
const char const char void * val
Definition: avisynth_c.h:672
int audio_preload
Audio preload in microseconds.
Definition: avformat.h:1543
const char * s
Definition: avisynth_c.h:669
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int64_t av_frame_get_pkt_duration(const AVFrame *frame)
static int check_packet(AVFormatContext *s, AVPacket *pkt)
Definition: mux.c:620
enum AVCodecID id
Definition: internal.h:41
void av_free_packet(AVPacket *pkt)
Free a packet.
Definition: avpacket.c:281
This structure describes decoded (raw) audio or video data.
Definition: frame.h:163
int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt)
Write a packet to an output media file ensuring correct interleaving.
Definition: mux.c:880
int flush_packets
Flush the I/O context after each packet.
Definition: avformat.h:1610
int avformat_write_header(AVFormatContext *s, AVDictionary **options)
Allocate the stream private data and write the stream header to an output media file.
Definition: mux.c:398
int av_write_frame(AVFormatContext *s, AVPacket *pkt)
Write a packet to an output media file.
Definition: mux.c:639
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:181
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:3997
int64_t pts_buffer[MAX_REORDER_DELAY+1]
Definition: avformat.h:1009
int max_b_frames
maximum number of B-frames between non-B-frames Note: The output will be delayed by max_b_frames+1 re...
Definition: avcodec.h:1501
void av_opt_set_defaults(void *s)
Set the values of all AVOption fields to their default values.
Definition: opt.c:1177
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:879
int num
numerator
Definition: rational.h:44
int index
stream index in AVFormatContext
Definition: avformat.h:808
int size
Definition: avcodec.h:1161
int ff_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush)
Interleave a packet per dts in an output media file.
Definition: mux.c:780
AVIndexEntry * index_entries
Only used if the format does not support seeking natively.
Definition: avformat.h:1011
AVFormatInternal * internal
An opaque field for libavformat internal usage.
Definition: avformat.h:1648
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_dlog(ac->avr,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> in
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel...
Definition: avcodec.h:1621
static int av_cmp_q(AVRational a, AVRational b)
Compare two rationals.
Definition: rational.h:66
int av_get_output_timestamp(struct AVFormatContext *s, int stream, int64_t *dts, int64_t *wall)
Get timing information for the data currently output.
Definition: mux.c:978
int64_t offset
Offset to remap timestamps to be non-negative.
Definition: internal.h:92
void * priv_data
Definition: avformat.h:827
size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
Put a string representing the codec tag codec_tag in buf.
Definition: utils.c:2977
av_dlog(ac->avr,"%d samples - audio_convert: %s to %s (%s)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic?ac->func_descr_generic:ac->func_descr)
static av_always_inline void interleave(IDWTELEM *dst, IDWTELEM *src0, IDWTELEM *src1, int w2, int add, int shift)
Definition: dirac_dwt.c:40
int avoid_negative_ts_use_pts
Definition: internal.h:101
int(* write_packet)(struct AVFormatContext *, AVPacket *pkt)
Write a packet.
Definition: avformat.h:529
static AVPacket pkt
int av_write_uncoded_frame_query(AVFormatContext *s, int stream_index)
Test whether a muxer supports uncoded frame.
Definition: mux.c:1056
int av_dup_packet(AVPacket *pkt)
Definition: avpacket.c:249
#define AVFMT_ALLOW_FLUSH
Format allows flushing.
Definition: avformat.h:446
static int av_write_uncoded_frame_internal(AVFormatContext *s, int stream_index, AVFrame *frame, int interleaved)
Definition: mux.c:1017
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
Definition: avcodec.h:2020
int strict_std_compliance
Allow non-standard and experimental extension.
Definition: avformat.h:1503
#define AVFMT_TS_NONSTRICT
Format does not require strictly increasing timestamps, but they must still be monotonic.
Definition: avformat.h:448
int ff_write_chained(AVFormatContext *dst, int dst_stream, AVPacket *pkt, AVFormatContext *src, int interleave)
Write a packet to another muxer than the one the user originally intended.
Definition: mux.c:987
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1367
Format I/O context.
Definition: avformat.h:1226
int64_t output_ts_offset
Output timestamp offset, in microseconds.
Definition: avformat.h:1713
int64_t cur_dts
Definition: avformat.h:984
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
internal metadata API header see avformat.h or the public API!
#define CHUNK_START
Definition: mux.c:673
Public dictionary API.
if()
Definition: avfilter.c:975
static void frac_init(AVFrac *f, int64_t val, int64_t num, int64_t den)
f = val + (num / den) + 0.5.
Definition: mux.c:64
int flags
can use flags: AVFMT_NOFILE, AVFMT_NEEDNUMBER, AVFMT_RAWPICTURE, AVFMT_GLOBALHEADER, AVFMT_NOTIMESTAMPS, AVFMT_VARIABLE_FPS, AVFMT_NODIMENSIONS, AVFMT_NOSTREAMS, AVFMT_ALLOW_FLUSH, AVFMT_TS_NONSTRICT
Definition: avformat.h:497
Round toward +infinity.
Definition: mathematics.h:74
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:63
AVOptions.
timestamp utils, mostly useful for debugging/logging purposes
attribute_deprecated void(* destruct)(struct AVPacket *)
Definition: avcodec.h:1181
Query whether the feature is possible on this stream.
Definition: internal.h:481
AVPacket pkt
Definition: avformat.h:1779
int priv_data_size
size of private data so that it can be allocated in the wrapper
Definition: avformat.h:519
#define CODEC_FLAG_GLOBAL_HEADER
Place global headers in extradata instead of every keyframe.
Definition: avcodec.h:756
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:249
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1294
static AVFrame * frame
AVFormatContext * avformat_alloc_context(void)
Allocate an AVFormatContext.
Definition: options.c:107
#define MAX_REORDER_DELAY
Definition: avformat.h:1008
void ff_compute_frame_duration(AVFormatContext *s, int *pnum, int *pden, AVStream *st, AVCodecParserContext *pc, AVPacket *pkt)
Return the frame duration in seconds.
Definition: utils.c:746
static double av_q2d(AVRational a)
Convert rational to double.
Definition: rational.h:80
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:39
int flags
Flags modifying the (de)muxer behaviour.
Definition: avformat.h:1337
uint8_t * data
Definition: avcodec.h:1160
int64_t max_interleave_delta
Maximum buffering duration for interleaving.
Definition: avformat.h:1497
uint32_t tag
Definition: movenc.c:1332
#define CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition: avcodec.h:757
struct AVPacketList * packet_buffer
This buffer is only needed when packets were already buffered but not decoded, for example to get the...
Definition: internal.h:62
static int interleave_packet(AVFormatContext *s, AVPacket *out, AVPacket *in, int flush)
Interleave an AVPacket correctly so it can be muxed.
Definition: mux.c:869
#define AVFMT_FLAG_BITEXACT
When muxing, try to avoid writing any random/volatile data to the output.
Definition: avformat.h:1354
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1178
const OptionDef options[]
Definition: ffserver.c:3749
void av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
Definition: dict.c:208
int avformat_alloc_output_context2(AVFormatContext **avctx, AVOutputFormat *oformat, const char *format, const char *filename)
Allocate an AVFormatContext for an output format.
Definition: mux.c:118
#define av_log(a,...)
struct AVOutputFormat * oformat
The output container format.
Definition: avformat.h:1245
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:147
#define AVFMT_FLAG_FLUSH_PACKETS
Flush the AVIOContext every packet.
Definition: avformat.h:1347
int(* write_uncoded_frame)(struct AVFormatContext *, int stream_index, AVFrame **frame, unsigned flags)
Write an uncoded AVFrame.
Definition: avformat.h:561
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:102
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:175
int has_b_frames
Size of the frame reordering buffer in the decoder.
Definition: avcodec.h:1531
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1435
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition: utils.c:3299
static int validate_codec_tag(AVFormatContext *s, AVStream *st)
Definition: mux.c:171
#define AVERROR(e)
Definition: error.h:43
#define AV_PKT_FLAG_UNCODED_FRAME
Definition: mux.c:429
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:148
int(* write_header)(struct AVFormatContext *)
Definition: avformat.h:521
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:196
#define FF_COMPLIANCE_NORMAL
Definition: avcodec.h:2544
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values. ...
Definition: dict.c:194
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
Definition: avcodec.h:1143
int flags
CODEC_FLAG_*.
Definition: avcodec.h:1333
simple assert() macros that are a bit more flexible than ISO C assert().
int side_data_elems
Definition: avcodec.h:1172
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
#define FFMAX(a, b)
Definition: common.h:79
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
Definition: avstring.c:83
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1166
int av_compare_ts(int64_t ts_a, AVRational tb_a, int64_t ts_b, AVRational tb_b)
Compare 2 timestamps each in its own timebases.
Definition: mathematics.c:152
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:826
int av_packet_merge_side_data(AVPacket *pkt)
Definition: avpacket.c:342
static int write_packet(AVFormatContext *s, AVPacket *pkt)
Make timestamps non negative, move side data from payload to internal struct, call muxer...
Definition: mux.c:540
common internal API header
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1282
#define LIBAVFORMAT_IDENT
Definition: version.h:44
int64_t av_rescale_q_rnd(int64_t a, AVRational bq, AVRational cq, enum AVRounding rnd)
Rescale a 64-bit integer by 2 rational numbers with specified rounding.
Definition: mathematics.c:139
int void avio_flush(AVIOContext *s)
Force flushing of buffered data.
Definition: aviobuf.c:197
char filename[1024]
input or output filename
Definition: avformat.h:1302
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
Definition: mathematics.c:134
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:247
#define AV_OPT_SEARCH_CHILDREN
Search in possible children of the given object first.
Definition: opt.h:602
ret
Definition: avfilter.c:974
int width
picture width / height.
Definition: avcodec.h:1412
#define AVFMT_GLOBALHEADER
Format wants global header.
Definition: avformat.h:436
internal header for RIFF based (de)muxers do NOT include this in end user applications ...
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:68
int avoid_negative_ts
Avoid negative timestamps during muxing.
Definition: avformat.h:1526
int n
Definition: avisynth_c.h:589
AVOutputFormat * av_guess_format(const char *short_name, const char *filename, const char *mime_type)
Return the output format in the list of registered output formats which best matches the provided par...
Definition: format.c:94
int ticks_per_frame
For some codecs, the time base is closer to the field rate than the frame rate.
Definition: avcodec.h:1376
static int init_muxer(AVFormatContext *s, AVDictionary **options)
Definition: mux.c:205
Opaque data information usually sparse.
Definition: avutil.h:198
void(* get_output_timestamp)(struct AVFormatContext *s, int stream, int64_t *dts, int64_t *wall)
Definition: avformat.h:545
const AVClass * priv_class
AVClass for the private context.
Definition: avformat.h:506
int av_opt_set_dict2(void *obj, AVDictionary **options, int search_flags)
Set all the options from a given dictionary on an object.
Definition: opt.c:1454
static void flush(AVCodecContext *avctx)
Definition: aacdec.c:502
int av_packet_split_side_data(AVPacket *pkt)
Definition: avpacket.c:382
Stream structure.
Definition: avformat.h:807
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:437
int frame_size
Definition: mxfenc.c:1618
AVS_Value src
Definition: avisynth_c.h:524
enum AVMediaType codec_type
Definition: avcodec.h:1247
enum AVCodecID codec_id
Definition: avcodec.h:1256
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:253
int av_opt_set_dict(void *obj, AVDictionary **options)
Set all the options from a given dictionary on an object.
Definition: opt.c:1479
int sample_rate
samples per second
Definition: avcodec.h:1983
AVIOContext * pb
I/O context.
Definition: avformat.h:1268
const struct AVCodecTag *const * codec_tag
List of supported codec_id-codec_tag pairs, ordered by "better choice first".
Definition: avformat.h:503
main external API structure.
Definition: avcodec.h:1239
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:530
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1271
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:69
#define UNCODED_FRAME_PACKET_SIZE
Definition: mux.c:435
int nb_interleaved_streams
Number of streams relevant for interleaving.
Definition: internal.h:55
Describe the class of an AVClass context structure.
Definition: log.h:66
unsigned int avpriv_toupper4(unsigned int x)
Definition: utils.c:3652
rational number numerator/denominator
Definition: rational.h:43
int(* interleave_packet)(struct AVFormatContext *, AVPacket *out, AVPacket *in, int flush)
Currently only used to set pixel format if not YUV420P.
Definition: avformat.h:534
AVRational ff_choose_timebase(AVFormatContext *s, AVStream *st, int min_precision)
Chooses a timebase for muxing the specified stream.
Definition: mux.c:102
#define AVFMT_AVOID_NEG_TS_AUTO
Enabled when required by target format.
Definition: avformat.h:1527
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Definition: utils.c:3600
int error
contains the error code or 0 if no error happened
Definition: avio.h:102
int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
Return audio frame duration.
Definition: utils.c:3317
misc parsing utilities
unsigned int tag
Definition: internal.h:42
AVRational offset_timebase
Timebase for the timestamp offset.
Definition: internal.h:97
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_dlog(ac->avr,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> out
int64_t interleaver_chunk_duration
Definition: avformat.h:1037
#define AVFMT_AVOID_NEG_TS_MAKE_ZERO
Shift timestamps so that they start at 0.
Definition: avformat.h:1529
Main libavformat public API header.
AVPacketSideData * side_data
Additional packet data that can be provided by the container.
Definition: avcodec.h:1171
int ff_interleave_add_packet(AVFormatContext *s, AVPacket *pkt, int(*compare)(AVFormatContext *, AVPacket *, AVPacket *))
Add packet to AVFormatContext->packet_buffer list, determining its interleaved position using compare...
Definition: mux.c:675
void av_opt_free(void *obj)
Free all allocated objects in obj.
Definition: opt.c:1434
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:79
int av_interleaved_write_uncoded_frame(AVFormatContext *s, int stream_index, AVFrame *frame)
Write a uncoded frame to an output media file.
Definition: mux.c:1050
common internal api header.
struct AVPacketList * next
Definition: avformat.h:1780
#define AVFMT_NOSTREAMS
Format does not require any streams.
Definition: avformat.h:442
int max_chunk_size
Max chunk size in bytes Note, not all formats support this and unpredictable things may happen if it ...
Definition: avformat.h:1559
static int init_pts(AVFormatContext *s)
Definition: mux.c:367
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:49
int64_t nb_frames
number of frames in this stream if known or 0
Definition: avformat.h:868
char * key
Definition: dict.h:87
int den
denominator
Definition: rational.h:45
static void frac_add(AVFrac *f, int64_t incr)
Fractional addition to f: f = f + (incr / f->den).
Definition: mux.c:82
int av_write_uncoded_frame(AVFormatContext *s, int stream_index, AVFrame *frame)
Write a uncoded frame to an output media file.
Definition: mux.c:1044
int64_t mux_ts_offset
Timestamp offset added to timestamps before muxing NOT PART OF PUBLIC API.
Definition: avformat.h:1083
#define AVFMT_AVOID_NEG_TS_MAKE_NON_NEGATIVE
Shift timestamps so they are non negative.
Definition: avformat.h:1528
struct AVPacketList * packet_buffer_end
Definition: internal.h:63
#define av_free(p)
static int interleave_compare_dts(AVFormatContext *s, AVPacket *next, AVPacket *pkt)
Definition: mux.c:757
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:80
#define av_ts2str(ts)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: timestamp.h:54
int channels
number of audio channels
Definition: avcodec.h:1984
int max_chunk_duration
Max chunk time in microseconds.
Definition: avformat.h:1551
void * priv_data
Format private data.
Definition: avformat.h:1254
#define AVFMT_NODIMENSIONS
Format does not need width/height.
Definition: avformat.h:441
static int compute_pkt_fields2(AVFormatContext *s, AVStream *st, AVPacket *pkt)
Definition: mux.c:439
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1159
int av_write_trailer(AVFormatContext *s)
Write the stream trailer to an output media file and free the file private data.
Definition: mux.c:932
#define av_freep(p)
static void comp(unsigned char *dst, int dst_stride, unsigned char *src, int src_stride, int add)
Definition: eamad.c:83
#define AV_DICT_IGNORE_SUFFIX
Return first entry in a dictionary whose first part corresponds to the search key, ignoring the suffix of the found key string.
Definition: dict.h:72
unbuffered private I/O API
#define FFSWAP(type, a, b)
Definition: common.h:84
int stream_index
Definition: avcodec.h:1162
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:849
#define MKTAG(a, b, c, d)
Definition: common.h:319
unsigned int av_codec_get_tag(const struct AVCodecTag *const *tags, enum AVCodecID id)
Get the codec tag for the given codec id id.
#define AVFMT_TS_NEGATIVE
Format allows muxing negative timestamps.
Definition: avformat.h:455
This structure stores compressed data.
Definition: avcodec.h:1137
int(* write_trailer)(struct AVFormatContext *)
Definition: avformat.h:530
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:250
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1153
struct AVPacketList * last_in_packet_buffer
last packet in packet_buffer for this stream when muxing.
Definition: avformat.h:1006
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:241