FFmpeg  2.1.1
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 #undef NDEBUG
48 #include <assert.h>
49 
50 /**
51  * @file
52  * muxing functions for use within libavformat
53  */
54 
55 /* fraction handling */
56 
57 /**
58  * f = val + (num / den) + 0.5.
59  *
60  * 'num' is normalized so that it is such as 0 <= num < den.
61  *
62  * @param f fractional number
63  * @param val integer value
64  * @param num must be >= 0
65  * @param den must be >= 1
66  */
67 static void frac_init(AVFrac *f, int64_t val, int64_t num, int64_t den)
68 {
69  num += (den >> 1);
70  if (num >= den) {
71  val += num / den;
72  num = num % den;
73  }
74  f->val = val;
75  f->num = num;
76  f->den = den;
77 }
78 
79 /**
80  * Fractional addition to f: f = f + (incr / f->den).
81  *
82  * @param f fractional number
83  * @param incr increment, can be positive or negative
84  */
85 static void frac_add(AVFrac *f, int64_t incr)
86 {
87  int64_t num, den;
88 
89  num = f->num + incr;
90  den = f->den;
91  if (num < 0) {
92  f->val += num / den;
93  num = num % den;
94  if (num < 0) {
95  num += den;
96  f->val--;
97  }
98  } else if (num >= den) {
99  f->val += num / den;
100  num = num % den;
101  }
102  f->num = num;
103 }
104 
106 {
107  AVRational q;
108  int j;
109 
110  if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
111  q = (AVRational){1, st->codec->sample_rate};
112  } else {
113  q = st->codec->time_base;
114  }
115  for (j=2; j<14; j+= 1+(j>2))
116  while (q.den / q.num < min_precission && q.num % j == 0)
117  q.num /= j;
118  while (q.den / q.num < min_precission && q.den < (1<<24))
119  q.den <<= 1;
120 
121  return q;
122 }
123 
125  const char *format, const char *filename)
126 {
128  int ret = 0;
129 
130  *avctx = NULL;
131  if (!s)
132  goto nomem;
133 
134  if (!oformat) {
135  if (format) {
136  oformat = av_guess_format(format, NULL, NULL);
137  if (!oformat) {
138  av_log(s, AV_LOG_ERROR, "Requested output format '%s' is not a suitable output format\n", format);
139  ret = AVERROR(EINVAL);
140  goto error;
141  }
142  } else {
143  oformat = av_guess_format(NULL, filename, NULL);
144  if (!oformat) {
145  ret = AVERROR(EINVAL);
146  av_log(s, AV_LOG_ERROR, "Unable to find a suitable output format for '%s'\n",
147  filename);
148  goto error;
149  }
150  }
151  }
152 
153  s->oformat = oformat;
154  if (s->oformat->priv_data_size > 0) {
156  if (!s->priv_data)
157  goto nomem;
158  if (s->oformat->priv_class) {
159  *(const AVClass**)s->priv_data= s->oformat->priv_class;
161  }
162  } else
163  s->priv_data = NULL;
164 
165  if (filename)
166  av_strlcpy(s->filename, filename, sizeof(s->filename));
167  *avctx = s;
168  return 0;
169 nomem:
170  av_log(s, AV_LOG_ERROR, "Out of memory\n");
171  ret = AVERROR(ENOMEM);
172 error:
174  return ret;
175 }
176 
177 #if FF_API_ALLOC_OUTPUT_CONTEXT
178 AVFormatContext *avformat_alloc_output_context(const char *format,
179  AVOutputFormat *oformat, const char *filename)
180 {
181  AVFormatContext *avctx;
182  int ret = avformat_alloc_output_context2(&avctx, oformat, format, filename);
183  return ret < 0 ? NULL : avctx;
184 }
185 #endif
186 
188 {
189  const AVCodecTag *avctag;
190  int n;
191  enum AVCodecID id = AV_CODEC_ID_NONE;
192  unsigned int tag = 0;
193 
194  /**
195  * Check that tag + id is in the table
196  * If neither is in the table -> OK
197  * If tag is in the table with another id -> FAIL
198  * If id is in the table with another tag -> FAIL unless strict < normal
199  */
200  for (n = 0; s->oformat->codec_tag[n]; n++) {
201  avctag = s->oformat->codec_tag[n];
202  while (avctag->id != AV_CODEC_ID_NONE) {
203  if (avpriv_toupper4(avctag->tag) == avpriv_toupper4(st->codec->codec_tag)) {
204  id = avctag->id;
205  if (id == st->codec->codec_id)
206  return 1;
207  }
208  if (avctag->id == st->codec->codec_id)
209  tag = avctag->tag;
210  avctag++;
211  }
212  }
213  if (id != AV_CODEC_ID_NONE)
214  return 0;
215  if (tag && (st->codec->strict_std_compliance >= FF_COMPLIANCE_NORMAL))
216  return 0;
217  return 1;
218 }
219 
220 
222 {
223  int ret = 0, i;
224  AVStream *st;
225  AVDictionary *tmp = NULL;
226  AVCodecContext *codec = NULL;
227  AVOutputFormat *of = s->oformat;
228 
229  if (options)
230  av_dict_copy(&tmp, *options, 0);
231 
232  if ((ret = av_opt_set_dict(s, &tmp)) < 0)
233  goto fail;
234  if (s->priv_data && s->oformat->priv_class && *(const AVClass**)s->priv_data==s->oformat->priv_class &&
235  (ret = av_opt_set_dict(s->priv_data, &tmp)) < 0)
236  goto fail;
237 
238  // some sanity checks
239  if (s->nb_streams == 0 && !(of->flags & AVFMT_NOSTREAMS)) {
240  av_log(s, AV_LOG_ERROR, "no streams\n");
241  ret = AVERROR(EINVAL);
242  goto fail;
243  }
244 
245  for (i = 0; i < s->nb_streams; i++) {
246  st = s->streams[i];
247  codec = st->codec;
248 
249  switch (codec->codec_type) {
250  case AVMEDIA_TYPE_AUDIO:
251  if (codec->sample_rate <= 0) {
252  av_log(s, AV_LOG_ERROR, "sample rate not set\n");
253  ret = AVERROR(EINVAL);
254  goto fail;
255  }
256  if (!codec->block_align)
257  codec->block_align = codec->channels *
258  av_get_bits_per_sample(codec->codec_id) >> 3;
259  break;
260  case AVMEDIA_TYPE_VIDEO:
261  if (codec->time_base.num <= 0 ||
262  codec->time_base.den <= 0) { //FIXME audio too?
263  av_log(s, AV_LOG_ERROR, "time base not set\n");
264  ret = AVERROR(EINVAL);
265  goto fail;
266  }
267 
268  if ((codec->width <= 0 || codec->height <= 0) &&
269  !(of->flags & AVFMT_NODIMENSIONS)) {
270  av_log(s, AV_LOG_ERROR, "dimensions not set\n");
271  ret = AVERROR(EINVAL);
272  goto fail;
273  }
276  ) {
277  if (st->sample_aspect_ratio.num != 0 &&
278  st->sample_aspect_ratio.den != 0 &&
279  codec->sample_aspect_ratio.den != 0 &&
280  codec->sample_aspect_ratio.den != 0) {
281  av_log(s, AV_LOG_ERROR, "Aspect ratio mismatch between muxer "
282  "(%d/%d) and encoder layer (%d/%d)\n",
284  codec->sample_aspect_ratio.num,
285  codec->sample_aspect_ratio.den);
286  ret = AVERROR(EINVAL);
287  goto fail;
288  }
289  }
290  break;
291  }
292 
293  if (of->codec_tag) {
294  if ( codec->codec_tag
295  && codec->codec_id == AV_CODEC_ID_RAWVIDEO
296  && ( av_codec_get_tag(of->codec_tag, codec->codec_id) == 0
297  || av_codec_get_tag(of->codec_tag, codec->codec_id) == MKTAG('r', 'a', 'w', ' '))
298  && !validate_codec_tag(s, st)) {
299  // the current rawvideo encoding system ends up setting
300  // the wrong codec_tag for avi/mov, we override it here
301  codec->codec_tag = 0;
302  }
303  if (codec->codec_tag) {
304  if (!validate_codec_tag(s, st)) {
305  char tagbuf[32], tagbuf2[32];
306  av_get_codec_tag_string(tagbuf, sizeof(tagbuf), codec->codec_tag);
307  av_get_codec_tag_string(tagbuf2, sizeof(tagbuf2), av_codec_get_tag(s->oformat->codec_tag, codec->codec_id));
308  av_log(s, AV_LOG_ERROR,
309  "Tag %s/0x%08x incompatible with output codec id '%d' (%s)\n",
310  tagbuf, codec->codec_tag, codec->codec_id, tagbuf2);
311  ret = AVERROR_INVALIDDATA;
312  goto fail;
313  }
314  } else
315  codec->codec_tag = av_codec_get_tag(of->codec_tag, codec->codec_id);
316  }
317 
318  if (of->flags & AVFMT_GLOBALHEADER &&
319  !(codec->flags & CODEC_FLAG_GLOBAL_HEADER))
321  "Codec for stream %d does not use global headers "
322  "but container format requires global headers\n", i);
323  }
324 
325  if (!s->priv_data && of->priv_data_size > 0) {
327  if (!s->priv_data) {
328  ret = AVERROR(ENOMEM);
329  goto fail;
330  }
331  if (of->priv_class) {
332  *(const AVClass **)s->priv_data = of->priv_class;
334  if ((ret = av_opt_set_dict(s->priv_data, &tmp)) < 0)
335  goto fail;
336  }
337  }
338 
339  /* set muxer identification string */
340  if (s->nb_streams && !(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT)) {
341  av_dict_set(&s->metadata, "encoder", LIBAVFORMAT_IDENT, 0);
342  }
343 
344  if (options) {
345  av_dict_free(options);
346  *options = tmp;
347  }
348 
349  return 0;
350 
351 fail:
352  av_dict_free(&tmp);
353  return ret;
354 }
355 
357 {
358  int i;
359  AVStream *st;
360 
361  /* init PTS generation */
362  for (i = 0; i < s->nb_streams; i++) {
363  int64_t den = AV_NOPTS_VALUE;
364  st = s->streams[i];
365 
366  switch (st->codec->codec_type) {
367  case AVMEDIA_TYPE_AUDIO:
368  den = (int64_t)st->time_base.num * st->codec->sample_rate;
369  break;
370  case AVMEDIA_TYPE_VIDEO:
371  den = (int64_t)st->time_base.num * st->codec->time_base.den;
372  break;
373  default:
374  break;
375  }
376  if (den != AV_NOPTS_VALUE) {
377  if (den <= 0)
378  return AVERROR_INVALIDDATA;
379 
380  frac_init(&st->pts, 0, 0, den);
381  }
382  }
383 
384  return 0;
385 }
386 
388 {
389  int ret = 0;
390 
391  if (ret = init_muxer(s, options))
392  return ret;
393 
394  if (s->oformat->write_header) {
395  ret = s->oformat->write_header(s);
396  if (ret >= 0 && s->pb && s->pb->error < 0)
397  ret = s->pb->error;
398  if (ret < 0)
399  return ret;
400  }
401 
402  if ((ret = init_pts(s)) < 0)
403  return ret;
404 
405  if (s->avoid_negative_ts < 0) {
407  s->avoid_negative_ts = 0;
408  } else
409  s->avoid_negative_ts = 1;
410  }
411 
412  return 0;
413 }
414 
415 //FIXME merge with compute_pkt_fields
417 {
418  int delay = FFMAX(st->codec->has_b_frames, st->codec->max_b_frames > 0);
419  int num, den, frame_size, i;
420 
421  av_dlog(s, "compute_pkt_fields2: pts:%s dts:%s cur_dts:%s b:%d size:%d st:%d\n",
422  av_ts2str(pkt->pts), av_ts2str(pkt->dts), av_ts2str(st->cur_dts), delay, pkt->size, pkt->stream_index);
423 
424  /* duration field */
425  if (pkt->duration == 0) {
426  ff_compute_frame_duration(&num, &den, st, NULL, pkt);
427  if (den && num) {
428  pkt->duration = av_rescale(1, num * (int64_t)st->time_base.den * st->codec->ticks_per_frame, den * (int64_t)st->time_base.num);
429  }
430  }
431 
432  if (pkt->pts == AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE && delay == 0)
433  pkt->pts = pkt->dts;
434 
435  //XXX/FIXME this is a temporary hack until all encoders output pts
436  if ((pkt->pts == 0 || pkt->pts == AV_NOPTS_VALUE) && pkt->dts == AV_NOPTS_VALUE && !delay) {
437  static int warned;
438  if (!warned) {
439  av_log(s, AV_LOG_WARNING, "Encoder did not produce proper pts, making some up.\n");
440  warned = 1;
441  }
442  pkt->dts =
443 // pkt->pts= st->cur_dts;
444  pkt->pts = st->pts.val;
445  }
446 
447  //calculate dts from pts
448  if (pkt->pts != AV_NOPTS_VALUE && pkt->dts == AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY) {
449  st->pts_buffer[0] = pkt->pts;
450  for (i = 1; i < delay + 1 && st->pts_buffer[i] == AV_NOPTS_VALUE; i++)
451  st->pts_buffer[i] = pkt->pts + (i - delay - 1) * pkt->duration;
452  for (i = 0; i<delay && st->pts_buffer[i] > st->pts_buffer[i + 1]; i++)
453  FFSWAP(int64_t, st->pts_buffer[i], st->pts_buffer[i + 1]);
454 
455  pkt->dts = st->pts_buffer[0];
456  }
457 
458  if (st->cur_dts && st->cur_dts != AV_NOPTS_VALUE &&
459  ((!(s->oformat->flags & AVFMT_TS_NONSTRICT) &&
460  st->cur_dts >= pkt->dts) || st->cur_dts > pkt->dts)) {
461  av_log(s, AV_LOG_ERROR,
462  "Application provided invalid, non monotonically increasing dts to muxer in stream %d: %s >= %s\n",
463  st->index, av_ts2str(st->cur_dts), av_ts2str(pkt->dts));
464  return AVERROR(EINVAL);
465  }
466  if (pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE && pkt->pts < pkt->dts) {
467  av_log(s, AV_LOG_ERROR, "pts (%s) < dts (%s) in stream %d\n",
468  av_ts2str(pkt->pts), av_ts2str(pkt->dts), st->index);
469  return AVERROR(EINVAL);
470  }
471 
472  av_dlog(s, "av_write_frame: pts2:%s dts2:%s\n",
473  av_ts2str(pkt->pts), av_ts2str(pkt->dts));
474  st->cur_dts = pkt->dts;
475  st->pts.val = pkt->dts;
476 
477  /* update pts */
478  switch (st->codec->codec_type) {
479  case AVMEDIA_TYPE_AUDIO:
480  frame_size = ff_get_audio_frame_size(st->codec, pkt->size, 1);
481 
482  /* HACK/FIXME, we skip the initial 0 size packets as they are most
483  * likely equal to the encoder delay, but it would be better if we
484  * had the real timestamps from the encoder */
485  if (frame_size >= 0 && (pkt->size || st->pts.num != st->pts.den >> 1 || st->pts.val)) {
486  frac_add(&st->pts, (int64_t)st->time_base.den * frame_size);
487  }
488  break;
489  case AVMEDIA_TYPE_VIDEO:
490  frac_add(&st->pts, (int64_t)st->time_base.den * st->codec->time_base.num);
491  break;
492  default:
493  break;
494  }
495  return 0;
496 }
497 
498 /**
499  * Make timestamps non negative, move side data from payload to internal struct, call muxer, and restore
500  * sidedata.
501  *
502  * FIXME: this function should NEVER get undefined pts/dts beside when the
503  * AVFMT_NOTIMESTAMPS is set.
504  * Those additional safety checks should be dropped once the correct checks
505  * are set in the callers.
506  */
508 {
509  int ret, did_split;
510 
511  if (s->avoid_negative_ts > 0) {
512  AVStream *st = s->streams[pkt->stream_index];
513  int64_t offset = st->mux_ts_offset;
514 
515  if (pkt->dts < 0 && pkt->dts != AV_NOPTS_VALUE && !s->offset) {
516  s->offset = -pkt->dts;
517  s->offset_timebase = st->time_base;
518  }
519 
520  if (s->offset && !offset) {
521  offset = st->mux_ts_offset =
523  s->offset_timebase,
524  st->time_base,
525  AV_ROUND_UP);
526  }
527 
528  if (pkt->dts != AV_NOPTS_VALUE)
529  pkt->dts += offset;
530  if (pkt->pts != AV_NOPTS_VALUE)
531  pkt->pts += offset;
532 
533  av_assert2(pkt->dts == AV_NOPTS_VALUE || pkt->dts >= 0);
534  }
535 
536  did_split = av_packet_split_side_data(pkt);
537  ret = s->oformat->write_packet(s, pkt);
538 
539  if (s->flush_packets && s->pb && ret >= 0 && s->flags & AVFMT_FLAG_FLUSH_PACKETS)
540  avio_flush(s->pb);
541 
542  if (did_split)
544 
545  return ret;
546 }
547 
549 {
550  int ret;
551 
552  if (!pkt) {
553  if (s->oformat->flags & AVFMT_ALLOW_FLUSH) {
554  ret = s->oformat->write_packet(s, NULL);
555  if (s->flush_packets && s->pb && s->pb->error >= 0)
556  avio_flush(s->pb);
557  if (ret >= 0 && s->pb && s->pb->error < 0)
558  ret = s->pb->error;
559  return ret;
560  }
561  return 1;
562  }
563 
564  ret = compute_pkt_fields2(s, s->streams[pkt->stream_index], pkt);
565 
566  if (ret < 0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
567  return ret;
568 
569  ret = write_packet(s, pkt);
570  if (ret >= 0 && s->pb && s->pb->error < 0)
571  ret = s->pb->error;
572 
573  if (ret >= 0)
574  s->streams[pkt->stream_index]->nb_frames++;
575  return ret;
576 }
577 
578 #define CHUNK_START 0x1000
579 
581  int (*compare)(AVFormatContext *, AVPacket *, AVPacket *))
582 {
583  AVPacketList **next_point, *this_pktl;
584  AVStream *st = s->streams[pkt->stream_index];
585  int chunked = s->max_chunk_size || s->max_chunk_duration;
586 
587  this_pktl = av_mallocz(sizeof(AVPacketList));
588  if (!this_pktl)
589  return AVERROR(ENOMEM);
590  this_pktl->pkt = *pkt;
591 #if FF_API_DESTRUCT_PACKET
593  pkt->destruct = NULL; // do not free original but only the copy
595 #endif
596  pkt->buf = NULL;
597  av_dup_packet(&this_pktl->pkt); // duplicate the packet if it uses non-allocated memory
598  av_copy_packet_side_data(&this_pktl->pkt, &this_pktl->pkt); // copy side data
599 
601  next_point = &(st->last_in_packet_buffer->next);
602  } else {
603  next_point = &s->packet_buffer;
604  }
605 
606  if (chunked) {
608  st->interleaver_chunk_size += pkt->size;
611  || (max && st->interleaver_chunk_duration > max)) {
612  st->interleaver_chunk_size = 0;
613  this_pktl->pkt.flags |= CHUNK_START;
614  if (max && st->interleaver_chunk_duration > max) {
615  int64_t syncoffset = (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)*max/2;
616  int64_t syncto = av_rescale(pkt->dts + syncoffset, 1, max)*max - syncoffset;
617 
618  st->interleaver_chunk_duration += (pkt->dts - syncto)/8 - max;
619  } else
621  }
622  }
623  if (*next_point) {
624  if (chunked && !(this_pktl->pkt.flags & CHUNK_START))
625  goto next_non_null;
626 
627  if (compare(s, &s->packet_buffer_end->pkt, pkt)) {
628  while ( *next_point
629  && ((chunked && !((*next_point)->pkt.flags&CHUNK_START))
630  || !compare(s, &(*next_point)->pkt, pkt)))
631  next_point = &(*next_point)->next;
632  if (*next_point)
633  goto next_non_null;
634  } else {
635  next_point = &(s->packet_buffer_end->next);
636  }
637  }
638  av_assert1(!*next_point);
639 
640  s->packet_buffer_end = this_pktl;
641 next_non_null:
642 
643  this_pktl->next = *next_point;
644 
646  *next_point = this_pktl;
647  return 0;
648 }
649 
651  AVPacket *pkt)
652 {
653  AVStream *st = s->streams[pkt->stream_index];
654  AVStream *st2 = s->streams[next->stream_index];
655  int comp = av_compare_ts(next->dts, st2->time_base, pkt->dts,
656  st->time_base);
658  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);
659  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);
660  if (ts == ts2) {
661  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
662  -( 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;
663  ts2=0;
664  }
665  comp= (ts>ts2) - (ts<ts2);
666  }
667 
668  if (comp == 0)
669  return pkt->stream_index < next->stream_index;
670  return comp > 0;
671 }
672 
674  AVPacket *pkt, int flush)
675 {
676  AVPacketList *pktl;
677  int stream_count = 0, noninterleaved_count = 0;
678  int64_t delta_dts_max = 0;
679  int i, ret;
680 
681  if (pkt) {
683  if (ret < 0)
684  return ret;
685  }
686 
687  for (i = 0; i < s->nb_streams; i++) {
688  if (s->streams[i]->last_in_packet_buffer) {
689  ++stream_count;
690  } else if (s->streams[i]->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
691  ++noninterleaved_count;
692  }
693  }
694 
695  if (s->nb_streams == stream_count) {
696  flush = 1;
697  } else if (!flush) {
698  for (i=0; i < s->nb_streams; i++) {
699  if (s->streams[i]->last_in_packet_buffer) {
700  int64_t delta_dts =
702  s->streams[i]->time_base,
703  AV_TIME_BASE_Q) -
707  delta_dts_max= FFMAX(delta_dts_max, delta_dts);
708  }
709  }
710  if (s->nb_streams == stream_count+noninterleaved_count &&
711  delta_dts_max > 20*AV_TIME_BASE) {
712  av_log(s, AV_LOG_DEBUG, "flushing with %d noninterleaved\n", noninterleaved_count);
713  flush = 1;
714  }
715  }
716  if (stream_count && flush) {
717  AVStream *st;
718  pktl = s->packet_buffer;
719  *out = pktl->pkt;
720  st = s->streams[out->stream_index];
721 
722  s->packet_buffer = pktl->next;
723  if (!s->packet_buffer)
724  s->packet_buffer_end = NULL;
725 
726  if (st->last_in_packet_buffer == pktl)
727  st->last_in_packet_buffer = NULL;
728  av_freep(&pktl);
729 
730  return 1;
731  } else {
732  av_init_packet(out);
733  return 0;
734  }
735 }
736 
737 /**
738  * Interleave an AVPacket correctly so it can be muxed.
739  * @param out the interleaved packet will be output here
740  * @param in the input packet
741  * @param flush 1 if no further packets are available as input and all
742  * remaining packets should be output
743  * @return 1 if a packet was output, 0 if no packet could be output,
744  * < 0 if an error occurred
745  */
747 {
748  if (s->oformat->interleave_packet) {
749  int ret = s->oformat->interleave_packet(s, out, in, flush);
750  if (in)
751  av_free_packet(in);
752  return ret;
753  } else
754  return ff_interleave_packet_per_dts(s, out, in, flush);
755 }
756 
758 {
759  int ret, flush = 0;
760 
761  if (pkt) {
762  AVStream *st = s->streams[pkt->stream_index];
763 
764  //FIXME/XXX/HACK drop zero sized packets
765  if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && pkt->size == 0)
766  return 0;
767 
768  av_dlog(s, "av_interleaved_write_frame size:%d dts:%s pts:%s\n",
769  pkt->size, av_ts2str(pkt->dts), av_ts2str(pkt->pts));
770  if ((ret = compute_pkt_fields2(s, st, pkt)) < 0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
771  return ret;
772 
773  if (pkt->dts == AV_NOPTS_VALUE && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
774  return AVERROR(EINVAL);
775  } else {
776  av_dlog(s, "av_interleaved_write_frame FLUSH\n");
777  flush = 1;
778  }
779 
780  for (;; ) {
781  AVPacket opkt;
782  int ret = interleave_packet(s, &opkt, pkt, flush);
783  if (ret <= 0) //FIXME cleanup needed for ret<0 ?
784  return ret;
785 
786  ret = write_packet(s, &opkt);
787  if (ret >= 0)
788  s->streams[opkt.stream_index]->nb_frames++;
789 
790  av_free_packet(&opkt);
791  pkt = NULL;
792 
793  if (ret < 0)
794  return ret;
795  if(s->pb && s->pb->error)
796  return s->pb->error;
797  }
798 }
799 
801 {
802  int ret, i;
803 
804  for (;; ) {
805  AVPacket pkt;
806  ret = interleave_packet(s, &pkt, NULL, 1);
807  if (ret < 0) //FIXME cleanup needed for ret<0 ?
808  goto fail;
809  if (!ret)
810  break;
811 
812  ret = write_packet(s, &pkt);
813  if (ret >= 0)
814  s->streams[pkt.stream_index]->nb_frames++;
815 
816  av_free_packet(&pkt);
817 
818  if (ret < 0)
819  goto fail;
820  if(s->pb && s->pb->error)
821  goto fail;
822  }
823 
824  if (s->oformat->write_trailer)
825  ret = s->oformat->write_trailer(s);
826 
827 fail:
828  if (s->pb)
829  avio_flush(s->pb);
830  if (ret == 0)
831  ret = s->pb ? s->pb->error : 0;
832  for (i = 0; i < s->nb_streams; i++) {
833  av_freep(&s->streams[i]->priv_data);
834  av_freep(&s->streams[i]->index_entries);
835  }
836  if (s->oformat->priv_class)
838  av_freep(&s->priv_data);
839  return ret;
840 }
841 
842 int av_get_output_timestamp(struct AVFormatContext *s, int stream,
843  int64_t *dts, int64_t *wall)
844 {
845  if (!s->oformat || !s->oformat->get_output_timestamp)
846  return AVERROR(ENOSYS);
847  s->oformat->get_output_timestamp(s, stream, dts, wall);
848  return 0;
849 }
850 
851 int ff_write_chained(AVFormatContext *dst, int dst_stream, AVPacket *pkt,
853 {
854  AVPacket local_pkt;
855 
856  local_pkt = *pkt;
857  local_pkt.stream_index = dst_stream;
858  if (pkt->pts != AV_NOPTS_VALUE)
859  local_pkt.pts = av_rescale_q(pkt->pts,
860  src->streams[pkt->stream_index]->time_base,
861  dst->streams[dst_stream]->time_base);
862  if (pkt->dts != AV_NOPTS_VALUE)
863  local_pkt.dts = av_rescale_q(pkt->dts,
864  src->streams[pkt->stream_index]->time_base,
865  dst->streams[dst_stream]->time_base);
866  if (pkt->duration)
867  local_pkt.duration = av_rescale_q(pkt->duration,
868  src->streams[pkt->stream_index]->time_base,
869  dst->streams[dst_stream]->time_base);
870  return av_write_frame(dst, &local_pkt);
871 }
int64_t interleaver_chunk_size
Definition: avformat.h:848
const char const char void * val
Definition: avisynth_c.h:671
int audio_preload
Audio preload in microseconds.
Definition: avformat.h:1181
const char * s
Definition: avisynth_c.h:668
enum AVCodecID id
Definition: internal.h:40
void av_free_packet(AVPacket *pkt)
Free a packet.
Definition: avpacket.c:279
#define AVFMT_FLAG_FLUSH_PACKETS
Flush the AVIOContext every packet.
Definition: avformat.h:1055
int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt)
Write a packet to an output media file ensuring correct interleaving.
Definition: mux.c:757
#define AVFMT_NODIMENSIONS
Format does not need width/height.
Definition: avformat.h:358
struct AVPacketList * packet_buffer_end
Definition: avformat.h:1284
#define AVFMT_TS_NONSTRICT
Format does not require strictly increasing timestamps, but they must still be monotonic.
Definition: avformat.h:365
int flush_packets
Flush the I/O context after each packet.
Definition: avformat.h:1259
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:387
int av_write_frame(AVFormatContext *s, AVPacket *pkt)
Write a packet to an output media file.
Definition: mux.c:548
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: avcodec.h:4153
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:1397
void av_opt_set_defaults(void *s)
Set the values of all AVOption fields to their default values.
Definition: opt.c:1064
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:686
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:733
int num
numerator
Definition: rational.h:44
int index
stream index in AVFormatContext
Definition: avformat.h:668
int size
Definition: avcodec.h:1064
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:1517
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
static int av_cmp_q(AVRational a, AVRational b)
Compare two rationals.
Definition: rational.h:55
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:842
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:2646
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)
int(* write_packet)(struct AVFormatContext *, AVPacket *pkt)
Write a packet.
Definition: avformat.h:446
int void avio_flush(AVIOContext *s)
Force flushing of buffered data to the output s.
Definition: aviobuf.c:193
int av_dup_packet(AVPacket *pkt)
Definition: avpacket.c:247
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
Definition: avcodec.h:1910
int ff_get_audio_frame_size(AVCodecContext *enc, int size, int mux)
Get the number of samples of an audio frame.
Definition: utils.c:752
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
Definition: avcodec.h:1046
const AVClass * priv_class
AVClass for the private context.
Definition: avformat.h:423
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1265
void av_freep(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:234
if((e=av_dict_get(options,"", NULL, AV_DICT_IGNORE_SUFFIX)))
Definition: avfilter.c:965
Format I/O context.
Definition: avformat.h:968
int64_t cur_dts
Definition: avformat.h:796
int64_t pts_buffer[MAX_REORDER_DELAY+1]
Definition: avformat.h:821
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:354
internal metadata API header see avformat.h or the public API!
#define CHUNK_START
Definition: mux.c:578
static void frac_init(AVFrac *f, int64_t val, int64_t num, int64_t den)
f = val + (num / den) + 0.5.
Definition: mux.c:67
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:414
Round toward +infinity.
Definition: mathematics.h:71
struct AVPacketList * last_in_packet_buffer
last packet in packet_buffer for this stream when muxing.
Definition: avformat.h:818
attribute_deprecated void(* destruct)(struct AVPacket *)
Definition: avcodec.h:1088
AVPacket pkt
Definition: avformat.h:1369
int priv_data_size
size of private data so that it can be allocated in the wrapper
Definition: avformat.h:436
#define CODEC_FLAG_GLOBAL_HEADER
Place global headers in extradata instead of every keyframe.
Definition: avcodec.h:713
static const uint8_t offset[511][2]
Definition: vf_uspp.c:58
static double av_q2d(AVRational a)
Convert rational to double.
Definition: rational.h:69
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:673
uint32_t tag
Definition: movenc.c:961
AVIndexEntry * index_entries
Only used if the format does not support seeking natively.
Definition: avformat.h:823
void * priv_data
Definition: avformat.h:687
#define CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition: avcodec.h:714
#define AVFMT_NOSTREAMS
Format does not require any streams.
Definition: avformat.h:359
#define LIBAVFORMAT_IDENT
Definition: version.h:44
#define FFSWAP(type, a, b)
Definition: avcodec.h:928
static int interleave_packet(AVFormatContext *s, AVPacket *out, AVPacket *in, int flush)
Interleave an AVPacket correctly so it can be muxed.
Definition: mux.c:746
int duration
Duration of this packet in AVStream-&gt;time_base units, 0 if unknown.
Definition: avcodec.h:1085
const OptionDef options[]
Definition: ffserver.c:4682
int64_t offset
Offset to remap timestamps to be non-negative.
Definition: avformat.h:1313
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq) av_const
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:130
static const uint8_t frame_size[4]
Definition: g723_1_data.h:58
void av_dict_copy(AVDictionary **dst, AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
Definition: dict.c:176
#define AVFMT_GLOBALHEADER
Format wants global header.
Definition: avformat.h:353
#define FF_COMPLIANCE_NORMAL
Definition: avcodec.h:2424
int av_copy_packet_side_data(AVPacket *dst, AVPacket *src)
Copy packet side data.
Definition: avpacket.c:223
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: avcodec.h:4147
int has_b_frames
Size of the frame reordering buffer in the decoder.
Definition: avcodec.h:1427
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition: utils.c:2928
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){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));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=av_sample_fmt_is_planar(in_fmt);out_planar=av_sample_fmt_is_planar(out_fmt);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);ff_audio_convert_init_arm(ac);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
static int validate_codec_tag(AVFormatContext *s, AVStream *st)
Definition: mux.c:187
AVRational offset_timebase
Timebase for the timestamp offset.
Definition: avformat.h:1318
void * priv_data
Format private data.
Definition: avformat.h:988
char filename[1024]
input or output filename
Definition: avformat.h:1018
#define AVFMT_ALLOW_FLUSH
Format allows flushing.
Definition: avformat.h:363
int(* write_header)(struct AVFormatContext *)
Definition: avformat.h:438
int ff_interleave_add_packet(AVFormatContext *s, AVPacket *pkt, int(*compare)(AVFormatContext *, AVPacket *, AVPacket *))
Add packet to AVFormatContext-&gt;packet_buffer list, determining its interleaved position using compare...
Definition: mux.c:580
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: avcodec.h:4168
void av_dict_free(AVDictionary **m)
Free all the memory allocated for an AVDictionary struct and all keys and values. ...
Definition: dict.c:162
int flags
CODEC_FLAG_*.
Definition: avcodec.h:1234
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:82
The exact value of the fractional number is: &#39;val + num / den&#39;.
Definition: avformat.h:322
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1069
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:135
int av_packet_merge_side_data(AVPacket *pkt)
Definition: avpacket.c:340
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:507
goto fail
Definition: avfilter.c:963
common internal API header
unsigned int nb_streams
A list of all streams in the file.
Definition: avformat.h:1015
int64_t av_rescale_q_rnd(int64_t a, AVRational bq, AVRational cq, enum AVRounding) av_const
Rescale a 64-bit integer by 2 rational numbers with specified rounding.
Definition: mathematics.c:122
int64_t av_rescale(int64_t a, int64_t b, int64_t c) av_const
Rescale a 64-bit integer with rounding to nearest.
Definition: mathematics.c:118
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avcodec.h:2284
ret
Definition: avfilter.c:961
int width
picture width / height.
Definition: avcodec.h:1314
AVStream ** streams
Definition: avformat.h:1016
internal header for RIFF based (de)muxers do NOT include this in end user applications ...
#define AVFMT_TS_NEGATIVE
Format allows muxing negative timestamps.
Definition: avformat.h:372
int avoid_negative_ts
Avoid negative timestamps during muxing.
Definition: avformat.h:1216
int n
Definition: avisynth_c.h:588
int ticks_per_frame
For some codecs, the time base is closer to the field rate than the frame rate.
Definition: avcodec.h:1274
static int init_muxer(AVFormatContext *s, AVDictionary **options)
Definition: mux.c:221
void(* get_output_timestamp)(struct AVFormatContext *s, int stream, int64_t *dts, int64_t *wall)
Definition: avformat.h:462
static void flush(AVCodecContext *avctx)
Definition: aacdec.c:498
int av_packet_split_side_data(AVPacket *pkt)
Definition: avpacket.c:380
Stream structure.
Definition: avformat.h:667
AVS_Value src
Definition: avisynth_c.h:523
enum AVMediaType codec_type
Definition: avcodec.h:1154
#define FFMAX(a, b)
Definition: avcodec.h:923
enum AVCodecID codec_id
Definition: avcodec.h:1157
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avcodec.h:2290
int av_opt_set_dict(void *obj, struct AVDictionary **options)
Set all the options from a given dictionary on an object.
Definition: opt.c:1326
int sample_rate
samples per second
Definition: avcodec.h:1873
void ff_compute_frame_duration(int *pnum, int *pden, AVStream *st, AVCodecParserContext *pc, AVPacket *pkt)
Return the frame duration in seconds.
Definition: utils.c:781
main external API structure.
Definition: avcodec.h:1146
unsigned int codec_tag
fourcc (LSB first, so &quot;ABCD&quot; -&gt; (&#39;D&#39;&lt;&lt;24) + (&#39;C&#39;&lt;&lt;16) + (&#39;B&#39;&lt;&lt;8) + &#39;A&#39;).
Definition: avcodec.h:1172
AVIOContext * pb
I/O context.
Definition: avformat.h:1001
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:62
Describe the class of an AVClass context structure.
Definition: log.h:50
#define MKTAG(a, b, c, d)
unsigned int avpriv_toupper4(unsigned int x)
Definition: utils.c:3265
rational number numerator/denominator
Definition: rational.h:43
AVDictionary * metadata
Definition: avformat.h:1128
int(* interleave_packet)(struct AVFormatContext *, AVPacket *out, AVPacket *in, int flush)
Currently only used to set pixel format if not YUV420P.
Definition: avformat.h:451
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Definition: utils.c:3271
int error
contains the error code or 0 if no error happened
Definition: avio.h:102
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
#define FFABS(a)
Definition: avcodec.h:920
int64_t val
Definition: avformat.h:323
int64_t num
Definition: avformat.h:323
AVRational ff_choose_timebase(AVFormatContext *s, AVStream *st, int min_precission)
Chooses a timebase for muxing the specified stream.
Definition: mux.c:105
unsigned int tag
Definition: internal.h:41
int64_t den
Definition: avformat.h:323
int64_t interleaver_chunk_duration
Definition: avformat.h:849
void av_opt_free(void *obj)
Free all string and binary options in obj.
Definition: opt.c:1318
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:78
common internal api header.
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:1197
int ff_write_chained(AVFormatContext *dst, int dst_stream, AVPacket *pkt, AVFormatContext *src)
Write a packet to another muxer than the one the user originally intended.
Definition: mux.c:851
int avformat_alloc_output_context2(AVFormatContext **ctx, AVOutputFormat *oformat, const char *format_name, const char *filename)
Allocate an AVFormatContext for an output format.
Definition: mux.c:124
Main libavformat public API header.
AVFormatContext * avformat_alloc_context(void)
Allocate an AVFormatContext.
Definition: options.c:106
static int init_pts(AVFormatContext *s)
Definition: mux.c:356
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:722
int den
denominator
Definition: rational.h:45
struct AVCodecTag *const * codec_tag
List of supported codec_id-codec_tag pairs, ordered by &quot;better choice first&quot;.
Definition: avformat.h:420
static void frac_add(AVFrac *f, int64_t incr)
Fractional addition to f: f = f + (incr / f-&gt;den).
Definition: mux.c:85
struct AVPacketList * packet_buffer
This buffer is only needed when packets were already buffered but not decoded, for example to get the...
Definition: avformat.h:1283
int64_t mux_ts_offset
Timestamp offset added to timestamps before muxing NOT PART OF PUBLIC API.
Definition: avformat.h:880
struct AVOutputFormat * oformat
Definition: avformat.h:982
#define AVERROR_INVALIDDATA
static int interleave_compare_dts(AVFormatContext *s, AVPacket *next, AVPacket *pkt)
Definition: mux.c:650
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:79
struct AVFrac pts
encoding: pts generation when outputting stream
Definition: avformat.h:692
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){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));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=av_sample_fmt_is_planar(in_fmt);out_planar=av_sample_fmt_is_planar(out_fmt);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);ff_audio_convert_init_arm(ac);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
int channels
number of audio channels
Definition: avcodec.h:1874
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:115
int max_chunk_duration
Max chunk time in microseconds.
Definition: avformat.h:1189
static int compute_pkt_fields2(AVFormatContext *s, AVStream *st, AVPacket *pkt)
Definition: mux.c:416
#define AVERROR(e)
int64_t dts
Decompression timestamp in AVStream-&gt;time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1062
int av_write_trailer(AVFormatContext *s)
Write the stream trailer to an output media file and free the file private data.
Definition: mux.c:800
static void comp(unsigned char *dst, int dst_stride, unsigned char *src, int src_stride, int add)
Definition: eamad.c:71
unbuffered private I/O API
static AVPacket pkt
Definition: demuxing.c:52
int stream_index
Definition: avcodec.h:1065
#define MAX_REORDER_DELAY
Definition: avformat.h:820
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:703
unsigned int av_codec_get_tag(const struct AVCodecTag *const *tags, enum AVCodecID id)
Get the codec tag for the given codec id id.
This structure stores compressed data.
Definition: avcodec.h:1040
int(* write_trailer)(struct AVFormatContext *)
Definition: avformat.h:447
int strict_std_compliance
strictly follow the standard (MPEG4, ...).
Definition: avcodec.h:2421
#define av_ts2str(ts)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: timestamp.h:50
int64_t pts
Presentation timestamp in AVStream-&gt;time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1056
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avcodec.h:2278
void * av_mallocz(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:241
struct AVPacketList * next
Definition: avformat.h:1370
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:63