Libav
mux.c
Go to the documentation of this file.
1 /*
2  * muxing functions for use within Libav
3  * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4  *
5  * This file is part of Libav.
6  *
7  * Libav is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * Libav is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
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 "metadata.h"
31 #include "id3v2.h"
32 #include "libavutil/avassert.h"
33 #include "libavutil/avstring.h"
34 #include "libavutil/internal.h"
35 #include "libavutil/mathematics.h"
36 #include "libavutil/parseutils.h"
37 #include "libavutil/time.h"
38 #include "riff.h"
39 #include "audiointerleave.h"
40 #include "url.h"
41 #include <stdarg.h>
42 #if CONFIG_NETWORK
43 #include "network.h"
44 #endif
45 
46 #undef NDEBUG
47 #include <assert.h>
48 
55 {
56  const AVCodecTag *avctag;
57  int n;
58  enum AVCodecID id = AV_CODEC_ID_NONE;
59  unsigned int tag = 0;
60 
67  for (n = 0; s->oformat->codec_tag[n]; n++) {
68  avctag = s->oformat->codec_tag[n];
69  while (avctag->id != AV_CODEC_ID_NONE) {
70  if (avpriv_toupper4(avctag->tag) == avpriv_toupper4(st->codec->codec_tag)) {
71  id = avctag->id;
72  if (id == st->codec->codec_id)
73  return 1;
74  }
75  if (avctag->id == st->codec->codec_id)
76  tag = avctag->tag;
77  avctag++;
78  }
79  }
80  if (id != AV_CODEC_ID_NONE)
81  return 0;
83  return 0;
84  return 1;
85 }
86 
87 
89 {
90  int ret = 0, i;
91  AVStream *st;
92  AVDictionary *tmp = NULL;
93  AVCodecContext *codec = NULL;
94  AVOutputFormat *of = s->oformat;
95 
96  if (options)
97  av_dict_copy(&tmp, *options, 0);
98 
99  if ((ret = av_opt_set_dict(s, &tmp)) < 0)
100  goto fail;
101 
102 #if FF_API_LAVF_BITEXACT
103  if (s->nb_streams && s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT)
105 #endif
106 
107  // some sanity checks
108  if (s->nb_streams == 0 && !(of->flags & AVFMT_NOSTREAMS)) {
109  av_log(s, AV_LOG_ERROR, "no streams\n");
110  ret = AVERROR(EINVAL);
111  goto fail;
112  }
113 
114  for (i = 0; i < s->nb_streams; i++) {
115  st = s->streams[i];
116  codec = st->codec;
117 
118 #if FF_API_LAVF_CODEC_TB
120  if (!st->time_base.num && codec->time_base.num) {
121  av_log(s, AV_LOG_WARNING, "Using AVStream.codec.time_base as a "
122  "timebase hint to the muxer is deprecated. Set "
123  "AVStream.time_base instead.\n");
124  avpriv_set_pts_info(st, 64, codec->time_base.num, codec->time_base.den);
125  }
127 #endif
128 
129  if (!st->time_base.num) {
130  /* fall back on the default timebase values */
131  if (codec->codec_type == AVMEDIA_TYPE_AUDIO && codec->sample_rate)
132  avpriv_set_pts_info(st, 64, 1, codec->sample_rate);
133  else
134  avpriv_set_pts_info(st, 33, 1, 90000);
135  }
136 
137  switch (codec->codec_type) {
138  case AVMEDIA_TYPE_AUDIO:
139  if (codec->sample_rate <= 0) {
140  av_log(s, AV_LOG_ERROR, "sample rate not set\n");
141  ret = AVERROR(EINVAL);
142  goto fail;
143  }
144  if (!codec->block_align)
145  codec->block_align = codec->channels *
146  av_get_bits_per_sample(codec->codec_id) >> 3;
147  break;
148  case AVMEDIA_TYPE_VIDEO:
149  if ((codec->width <= 0 || codec->height <= 0) &&
150  !(of->flags & AVFMT_NODIMENSIONS)) {
151  av_log(s, AV_LOG_ERROR, "dimensions not set\n");
152  ret = AVERROR(EINVAL);
153  goto fail;
154  }
155 
157  codec->sample_aspect_ratio)) {
158  if (st->sample_aspect_ratio.num != 0 &&
159  st->sample_aspect_ratio.den != 0 &&
160  codec->sample_aspect_ratio.den != 0 &&
161  codec->sample_aspect_ratio.den != 0) {
162  av_log(s, AV_LOG_ERROR, "Aspect ratio mismatch between muxer "
163  "(%d/%d) and encoder layer (%d/%d)\n",
165  codec->sample_aspect_ratio.num,
166  codec->sample_aspect_ratio.den);
167  ret = AVERROR(EINVAL);
168  goto fail;
169  }
170  }
171  break;
172  }
173 
174  if (of->codec_tag) {
175  if (codec->codec_tag &&
176  codec->codec_id == AV_CODEC_ID_RAWVIDEO &&
177  !av_codec_get_tag(of->codec_tag, codec->codec_id) &&
178  !validate_codec_tag(s, st)) {
179  // the current rawvideo encoding system ends up setting
180  // the wrong codec_tag for avi, we override it here
181  codec->codec_tag = 0;
182  }
183  if (codec->codec_tag) {
184  if (!validate_codec_tag(s, st)) {
185  char tagbuf[32];
186  av_get_codec_tag_string(tagbuf, sizeof(tagbuf), codec->codec_tag);
187  av_log(s, AV_LOG_ERROR,
188  "Tag %s/0x%08x incompatible with output codec id '%d'\n",
189  tagbuf, codec->codec_tag, codec->codec_id);
190  ret = AVERROR_INVALIDDATA;
191  goto fail;
192  }
193  } else
194  codec->codec_tag = av_codec_get_tag(of->codec_tag, codec->codec_id);
195  }
196 
197  if (of->flags & AVFMT_GLOBALHEADER &&
198  !(codec->flags & CODEC_FLAG_GLOBAL_HEADER))
200  "Codec for stream %d does not use global headers "
201  "but container format requires global headers\n", i);
202 
203  if (codec->codec_type != AVMEDIA_TYPE_ATTACHMENT)
205  }
206 
207  if (!s->priv_data && of->priv_data_size > 0) {
209  if (!s->priv_data) {
210  ret = AVERROR(ENOMEM);
211  goto fail;
212  }
213  if (of->priv_class) {
214  *(const AVClass **)s->priv_data = of->priv_class;
216  if ((ret = av_opt_set_dict(s->priv_data, &tmp)) < 0)
217  goto fail;
218  }
219  }
220 
221  /* set muxer identification string */
222  if (!(s->flags & AVFMT_FLAG_BITEXACT)) {
223  av_dict_set(&s->metadata, "encoder", LIBAVFORMAT_IDENT, 0);
224  }
225 
226  if (options) {
227  av_dict_free(options);
228  *options = tmp;
229  }
230 
231  return 0;
232 
233 fail:
234  av_dict_free(&tmp);
235  return ret;
236 }
237 
239 {
240  int ret = 0;
241 
242  if (ret = init_muxer(s, options))
243  return ret;
244 
245  if (s->oformat->write_header) {
246  ret = s->oformat->write_header(s);
247  if (ret < 0)
248  return ret;
249  }
250 
251  return 0;
252 }
253 
254 //FIXME merge with compute_pkt_fields
256 {
257  int delay = FFMAX(st->codec->has_b_frames, !!st->codec->max_b_frames);
258  int num, den, i;
259 
260  av_dlog(s, "compute_pkt_fields2: pts:%" PRId64 " dts:%" PRId64 " cur_dts:%" PRId64 " b:%d size:%d st:%d\n",
261  pkt->pts, pkt->dts, st->cur_dts, delay, pkt->size, pkt->stream_index);
262 
263 /* if(pkt->pts == AV_NOPTS_VALUE && pkt->dts == AV_NOPTS_VALUE)
264  * return AVERROR(EINVAL);*/
265 
266  /* duration field */
267  if (pkt->duration == 0) {
268  ff_compute_frame_duration(&num, &den, st, NULL, pkt);
269  if (den && num) {
270  pkt->duration = av_rescale(1, num * (int64_t)st->time_base.den * st->codec->ticks_per_frame, den * (int64_t)st->time_base.num);
271  }
272  }
273 
274  if (pkt->pts == AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE && delay == 0)
275  pkt->pts = pkt->dts;
276 
277  //calculate dts from pts
278  if (pkt->pts != AV_NOPTS_VALUE && pkt->dts == AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY) {
279  st->pts_buffer[0] = pkt->pts;
280  for (i = 1; i < delay + 1 && st->pts_buffer[i] == AV_NOPTS_VALUE; i++)
281  st->pts_buffer[i] = pkt->pts + (i - delay - 1) * pkt->duration;
282  for (i = 0; i<delay && st->pts_buffer[i] > st->pts_buffer[i + 1]; i++)
283  FFSWAP(int64_t, st->pts_buffer[i], st->pts_buffer[i + 1]);
284 
285  pkt->dts = st->pts_buffer[0];
286  }
287 
288  if (st->cur_dts && st->cur_dts != AV_NOPTS_VALUE &&
289  ((!(s->oformat->flags & AVFMT_TS_NONSTRICT) &&
290  st->cur_dts >= pkt->dts) || st->cur_dts > pkt->dts)) {
291  av_log(s, AV_LOG_ERROR,
292  "Application provided invalid, non monotonically increasing dts to muxer in stream %d: %" PRId64 " >= %" PRId64 "\n",
293  st->index, st->cur_dts, pkt->dts);
294  return AVERROR(EINVAL);
295  }
296  if (pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE && pkt->pts < pkt->dts) {
297  av_log(s, AV_LOG_ERROR, "pts < dts in stream %d\n", st->index);
298  return AVERROR(EINVAL);
299  }
300 
301  av_dlog(s, "av_write_frame: pts2:%"PRId64" dts2:%"PRId64"\n",
302  pkt->pts, pkt->dts);
303  st->cur_dts = pkt->dts;
304 
305  return 0;
306 }
307 
308 /*
309  * FIXME: this function should NEVER get undefined pts/dts beside when the
310  * AVFMT_NOTIMESTAMPS is set.
311  * Those additional safety checks should be dropped once the correct checks
312  * are set in the callers.
313  */
314 
316 {
317  int ret;
319  AVRational time_base = s->streams[pkt->stream_index]->time_base;
320  int64_t offset = 0;
321 
322  if (!s->offset && pkt->dts != AV_NOPTS_VALUE && pkt->dts < 0) {
323  s->offset = -pkt->dts;
324  s->offset_timebase = time_base;
325  }
326  if (s->offset)
327  offset = av_rescale_q(s->offset, s->offset_timebase, time_base);
328 
329  if (pkt->dts != AV_NOPTS_VALUE)
330  pkt->dts += offset;
331  if (pkt->pts != AV_NOPTS_VALUE)
332  pkt->pts += offset;
333  }
334  ret = s->oformat->write_packet(s, pkt);
335 
336  if (s->pb && ret >= 0) {
338  avio_flush(s->pb);
339  if (s->pb->error < 0)
340  ret = s->pb->error;
341  }
342 
343  return ret;
344 }
345 
347 {
348  if (!pkt)
349  return 0;
350 
351  if (pkt->stream_index < 0 || pkt->stream_index >= s->nb_streams) {
352  av_log(s, AV_LOG_ERROR, "Invalid packet stream index: %d\n",
353  pkt->stream_index);
354  return AVERROR(EINVAL);
355  }
356 
358  av_log(s, AV_LOG_ERROR, "Received a packet for an attachment stream.\n");
359  return AVERROR(EINVAL);
360  }
361 
362  return 0;
363 }
364 
366 {
367  int ret;
368 
369  ret = check_packet(s, pkt);
370  if (ret < 0)
371  return ret;
372 
373  if (!pkt) {
374  if (s->oformat->flags & AVFMT_ALLOW_FLUSH)
375  return s->oformat->write_packet(s, pkt);
376  return 1;
377  }
378 
379  ret = compute_pkt_fields2(s, s->streams[pkt->stream_index], pkt);
380 
381  if (ret < 0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
382  return ret;
383 
384  ret = write_packet(s, pkt);
385 
386  if (ret >= 0)
387  s->streams[pkt->stream_index]->nb_frames++;
388  return ret;
389 }
390 
392  int (*compare)(AVFormatContext *, AVPacket *, AVPacket *))
393 {
394  int ret;
395  AVPacketList **next_point, *this_pktl;
396 
397  this_pktl = av_mallocz(sizeof(AVPacketList));
398  if (!this_pktl)
399  return AVERROR(ENOMEM);
400  this_pktl->pkt = *pkt;
401 #if FF_API_DESTRUCT_PACKET
403  pkt->destruct = NULL; // do not free original but only the copy
405 #endif
406  pkt->buf = NULL;
407  pkt->side_data = NULL;
408  pkt->side_data_elems = 0;
409  // Duplicate the packet if it uses non-allocated memory
410  if ((ret = av_dup_packet(&this_pktl->pkt)) < 0) {
411  av_free(this_pktl);
412  return ret;
413  }
414 
416  next_point = &(s->streams[pkt->stream_index]->last_in_packet_buffer->next);
417  } else
418  next_point = &s->packet_buffer;
419 
420  if (*next_point) {
421  if (compare(s, &s->packet_buffer_end->pkt, pkt)) {
422  while (!compare(s, &(*next_point)->pkt, pkt))
423  next_point = &(*next_point)->next;
424  goto next_non_null;
425  } else {
426  next_point = &(s->packet_buffer_end->next);
427  }
428  }
429  assert(!*next_point);
430 
431  s->packet_buffer_end = this_pktl;
432 next_non_null:
433 
434  this_pktl->next = *next_point;
435 
437  *next_point = this_pktl;
438 
439  return 0;
440 }
441 
443  AVPacket *pkt)
444 {
445  AVStream *st = s->streams[pkt->stream_index];
446  AVStream *st2 = s->streams[next->stream_index];
447  int comp = av_compare_ts(next->dts, st2->time_base, pkt->dts,
448  st->time_base);
449 
450  if (comp == 0)
451  return pkt->stream_index < next->stream_index;
452  return comp > 0;
453 }
454 
456  AVPacket *pkt, int flush)
457 {
458  AVPacketList *pktl;
459  int stream_count = 0;
460  int i, ret;
461 
462  if (pkt) {
463  if ((ret = ff_interleave_add_packet(s, pkt, interleave_compare_dts)) < 0)
464  return ret;
465  }
466 
467  if (s->max_interleave_delta > 0 && s->packet_buffer && !flush) {
468  AVPacket *top_pkt = &s->packet_buffer->pkt;
469  int64_t delta_dts = INT64_MIN;
470  int64_t top_dts = av_rescale_q(top_pkt->dts,
471  s->streams[top_pkt->stream_index]->time_base,
473 
474  for (i = 0; i < s->nb_streams; i++) {
475  int64_t last_dts;
476  const AVPacketList *last = s->streams[i]->last_in_packet_buffer;
477 
478  if (!last)
479  continue;
480 
481  last_dts = av_rescale_q(last->pkt.dts,
482  s->streams[i]->time_base,
484  delta_dts = FFMAX(delta_dts, last_dts - top_dts);
485  stream_count++;
486  }
487 
488  if (delta_dts > s->max_interleave_delta) {
489  av_log(s, AV_LOG_DEBUG,
490  "Delay between the first packet and last packet in the "
491  "muxing queue is %"PRId64" > %"PRId64": forcing output\n",
492  delta_dts, s->max_interleave_delta);
493  flush = 1;
494  }
495  } else {
496  for (i = 0; i < s->nb_streams; i++)
497  stream_count += !!s->streams[i]->last_in_packet_buffer;
498  }
499 
500 
501  if (stream_count && (s->internal->nb_interleaved_streams == stream_count || flush)) {
502  pktl = s->packet_buffer;
503  *out = pktl->pkt;
504 
505  s->packet_buffer = pktl->next;
506  if (!s->packet_buffer)
507  s->packet_buffer_end = NULL;
508 
509  if (s->streams[out->stream_index]->last_in_packet_buffer == pktl)
511  av_freep(&pktl);
512  return 1;
513  } else {
514  av_init_packet(out);
515  return 0;
516  }
517 }
518 
529 {
530  if (s->oformat->interleave_packet) {
531  int ret = s->oformat->interleave_packet(s, out, in, flush);
532  if (in)
533  av_free_packet(in);
534  return ret;
535  } else
536  return ff_interleave_packet_per_dts(s, out, in, flush);
537 }
538 
540 {
541  int ret, flush = 0;
542 
543  ret = check_packet(s, pkt);
544  if (ret < 0)
545  goto fail;
546 
547  if (pkt) {
548  AVStream *st = s->streams[pkt->stream_index];
549 
550  av_dlog(s, "av_interleaved_write_frame size:%d dts:%" PRId64 " pts:%" PRId64 "\n",
551  pkt->size, pkt->dts, pkt->pts);
552  if ((ret = compute_pkt_fields2(s, st, pkt)) < 0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
553  goto fail;
554 
555  if (pkt->dts == AV_NOPTS_VALUE && !(s->oformat->flags & AVFMT_NOTIMESTAMPS)) {
556  ret = AVERROR(EINVAL);
557  goto fail;
558  }
559  } else {
560  av_dlog(s, "av_interleaved_write_frame FLUSH\n");
561  flush = 1;
562  }
563 
564  for (;; ) {
565  AVPacket opkt;
566  int ret = interleave_packet(s, &opkt, pkt, flush);
567  if (pkt) {
568  memset(pkt, 0, sizeof(*pkt));
569  av_init_packet(pkt);
570  pkt = NULL;
571  }
572  if (ret <= 0) //FIXME cleanup needed for ret<0 ?
573  return ret;
574 
575  ret = write_packet(s, &opkt);
576  if (ret >= 0)
577  s->streams[opkt.stream_index]->nb_frames++;
578 
579  av_free_packet(&opkt);
580 
581  if (ret < 0)
582  return ret;
583  }
584 fail:
585  av_packet_unref(pkt);
586  return ret;
587 }
588 
590 {
591  int ret, i;
592 
593  for (;; ) {
594  AVPacket pkt;
595  ret = interleave_packet(s, &pkt, NULL, 1);
596  if (ret < 0) //FIXME cleanup needed for ret<0 ?
597  goto fail;
598  if (!ret)
599  break;
600 
601  ret = write_packet(s, &pkt);
602  if (ret >= 0)
603  s->streams[pkt.stream_index]->nb_frames++;
604 
605  av_free_packet(&pkt);
606 
607  if (ret < 0)
608  goto fail;
609  }
610 
611  if (s->oformat->write_trailer)
612  ret = s->oformat->write_trailer(s);
613 
614  if (!(s->oformat->flags & AVFMT_NOFILE))
615  avio_flush(s->pb);
616 
617 fail:
618  for (i = 0; i < s->nb_streams; i++) {
619  av_freep(&s->streams[i]->priv_data);
620  av_freep(&s->streams[i]->index_entries);
621  }
622  if (s->oformat->priv_class)
624  av_freep(&s->priv_data);
625  return ret;
626 }
627 
628 int ff_write_chained(AVFormatContext *dst, int dst_stream, AVPacket *pkt,
629  AVFormatContext *src)
630 {
631  AVPacket local_pkt;
632 
633  local_pkt = *pkt;
634  local_pkt.stream_index = dst_stream;
635  if (pkt->pts != AV_NOPTS_VALUE)
636  local_pkt.pts = av_rescale_q(pkt->pts,
637  src->streams[pkt->stream_index]->time_base,
638  dst->streams[dst_stream]->time_base);
639  if (pkt->dts != AV_NOPTS_VALUE)
640  local_pkt.dts = av_rescale_q(pkt->dts,
641  src->streams[pkt->stream_index]->time_base,
642  dst->streams[dst_stream]->time_base);
643  return av_write_frame(dst, &local_pkt);
644 }
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
static int check_packet(AVFormatContext *s, AVPacket *pkt)
Definition: mux.c:346
enum AVCodecID id
Definition: internal.h:36
void av_free_packet(AVPacket *pkt)
Free a packet.
Definition: avpacket.c:243
int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt)
Write a packet to an output media file ensuring correct interleaving.
Definition: mux.c:539
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:238
int av_write_frame(AVFormatContext *s, AVPacket *pkt)
Write a packet to an output media file.
Definition: mux.c:365
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:129
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:2829
int64_t pts_buffer[MAX_REORDER_DELAY+1]
Definition: avformat.h:876
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:1325
void av_opt_set_defaults(void *s)
Set the values of all AVOption fields to their default values.
Definition: opt.c:546
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:769
int num
numerator
Definition: rational.h:44
int index
stream index in AVFormatContext
Definition: avformat.h:700
int size
Definition: avcodec.h:974
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:455
AVIndexEntry * index_entries
Only used if the format does not support seeking natively.
Definition: avformat.h:878
AVFormatInternal * internal
An opaque field for libavformat internal usage.
Definition: avformat.h:1256
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:1445
static int av_cmp_q(AVRational a, AVRational b)
Compare two rationals.
Definition: rational.h:55
void * priv_data
Definition: avformat.h:719
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:1822
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) #define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac) { } void ff_audio_convert_free(AudioConvert **ac) { if(! *ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);} AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map) { AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;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);return NULL;} return ac;} 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;} else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->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);return ac;} int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in) { int use_generic=1;int len=in->nb_samples;int p;if(ac->dc) { av_dlog(ac->avr, "%d samples - audio_convert: %s to %s (dithered)\", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> out
int av_dup_packet(AVPacket *pkt)
Definition: avpacket.c:190
#define AVFMT_ALLOW_FLUSH
Format allows flushing.
Definition: avformat.h:425
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
Definition: avcodec.h:1844
#define AVFMT_TS_NONSTRICT
Format does not require strictly increasing timestamps, but they must still be monotonic.
Definition: avformat.h:426
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:1216
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1175
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
int(* interleave_packet)(struct AVFormatContext *, AVPacket *out, AVPacket *in, int flush)
Currently only used to set pixel format if not YUV420P.
Definition: avformat.h:502
Format I/O context.
Definition: avformat.h:922
av_dlog(ac->avr, "%d samples - audio_convert: %s to %s (%s)\, 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)
int64_t cur_dts
Definition: avformat.h:851
internal metadata API header see avformat.h or the public API!
Public dictionary API.
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:465
AVOptions.
AVPacket pkt
Definition: avformat.h:1260
int priv_data_size
size of private data so that it can be allocated in the wrapper
Definition: avformat.h:487
#define CODEC_FLAG_GLOBAL_HEADER
Place global headers in extradata instead of every keyframe.
Definition: avcodec.h:657
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:990
#define MAX_REORDER_DELAY
Definition: avformat.h:875
int flags
Flags modifying the (de)muxer behaviour.
Definition: avformat.h:1033
int64_t max_interleave_delta
Maximum buffering duration for interleaving.
Definition: avformat.h:1187
uint32_t tag
Definition: movenc.c:844
attribute_deprecated void(* destruct)(struct AVPacket *)
Definition: avcodec.h:994
#define CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition: avcodec.h:658
static int interleave_packet(AVFormatContext *s, AVPacket *out, AVPacket *in, int flush)
Interleave an AVPacket correctly so it can be muxed.
Definition: mux.c:528
#define AVFMT_FLAG_BITEXACT
When muxing, try to avoid writing any random/volatile data to the output.
Definition: avformat.h:1050
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:991
void av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
Definition: dict.c:184
int64_t offset
Offset to remap timestamps to be non-negative.
Definition: avformat.h:1245
struct AVOutputFormat * oformat
The output container format.
Definition: avformat.h:941
const OptionDef options[]
Definition: avconv_opt.c:2187
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:129
#define AVFMT_FLAG_FLUSH_PACKETS
Flush the AVIOContext every packet.
Definition: avformat.h:1043
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:105
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
int has_b_frames
Size of the frame reordering buffer in the decoder.
Definition: avcodec.h:1355
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1130
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition: utils.c:2043
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:186
static int validate_codec_tag(AVFormatContext *s, AVStream *st)
Definition: mux.c:54
AVRational offset_timebase
Timebase for the timestamp offset.
Definition: avformat.h:1250
#define AVERROR(e)
Definition: error.h:43
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) #define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac) { } void ff_audio_convert_free(AudioConvert **ac) { if(! *ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);} AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map) { AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;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);return NULL;} return ac;} 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;} else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->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);return ac;} int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in) { int use_generic=1;int len=in->nb_samples;int p;if(ac->dc) { av_dlog(ac->avr, "%d samples - audio_convert: %s to %s (dithered)\", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> in
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:144
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values. ...
Definition: dict.c:170
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
Definition: avcodec.h:956
int flags
CODEC_FLAG_*.
Definition: avcodec.h:1144
simple assert() macros that are a bit more flexible than ISO C assert().
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:169
int side_data_elems
Definition: avcodec.h:985
#define FFMAX(a, b)
Definition: common.h:55
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:134
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:718
static int write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: mux.c:315
common internal API header
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:978
#define LIBAVFORMAT_IDENT
Definition: version.h:44
int void avio_flush(AVIOContext *s)
Definition: aviobuf.c:180
int(* write_header)(struct AVFormatContext *)
Definition: avformat.h:489
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:116
int width
picture width / height.
Definition: avcodec.h:1229
#define AVFMT_GLOBALHEADER
Format wants global header.
Definition: avformat.h:415
internal header for RIFF based (de)muxers do NOT include this in end user applications ...
int ticks_per_frame
For some codecs, the time base is closer to the field rate than the frame rate.
Definition: avcodec.h:1184
static int init_muxer(AVFormatContext *s, AVDictionary **options)
Definition: mux.c:88
Opaque data information usually sparse.
Definition: avutil.h:191
const AVClass * priv_class
AVClass for the private context.
Definition: avformat.h:474
Stream structure.
Definition: avformat.h:699
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:416
NULL
Definition: eval.c:55
enum AVMediaType codec_type
Definition: avcodec.h:1058
enum AVCodecID codec_id
Definition: avcodec.h:1067
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:240
int av_opt_set_dict(void *obj, AVDictionary **options)
Definition: opt.c:679
int sample_rate
samples per second
Definition: avcodec.h:1807
AVIOContext * pb
I/O context.
Definition: avformat.h:964
void ff_compute_frame_duration(int *pnum, int *pden, AVStream *st, AVCodecParserContext *pc, AVPacket *pkt)
Return the frame duration in seconds.
Definition: utils.c:480
const struct AVCodecTag *const * codec_tag
List of supported codec_id-codec_tag pairs, ordered by "better choice first".
Definition: avformat.h:471
main external API structure.
Definition: avcodec.h:1050
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:346
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> (&#39;D&#39;<<24) + (&#39;C&#39;<<16) + (&#39;B&#39;<<8) + &#39;A&#39;).
Definition: avcodec.h:1082
int(* write_trailer)(struct AVFormatContext *)
Definition: avformat.h:498
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:68
int nb_interleaved_streams
Number of streams relevant for interleaving.
Definition: internal.h:50
Describe the class of an AVClass context structure.
Definition: log.h:33
#define FF_COMPLIANCE_NORMAL
Definition: avcodec.h:2360
unsigned int avpriv_toupper4(unsigned int x)
Definition: utils.c:2320
rational number numerator/denominator
Definition: rational.h:43
struct AVPacketList * packet_buffer_end
Definition: avformat.h:1217
int error
contains the error code or 0 if no error happened
Definition: avio.h:102
misc parsing utilities
unsigned int tag
Definition: internal.h:37
Main libavformat public API header.
AVPacketSideData * side_data
Additional packet data that can be provided by the container.
Definition: avcodec.h:984
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:391
void av_opt_free(void *obj)
Free all allocated objects in obj.
Definition: opt.c:659
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:76
common internal api header.
struct AVPacketList * next
Definition: avformat.h:1261
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:409
static av_cold void flush(AVCodecContext *avctx)
Flush (reset) the frame ID after seeking.
Definition: alsdec.c:1797
#define AVFMT_NOSTREAMS
Format does not require any streams.
Definition: avformat.h:421
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:47
int64_t nb_frames
number of frames in this stream if known or 0
Definition: avformat.h:758
int den
denominator
Definition: rational.h:45
static int interleave_compare_dts(AVFormatContext *s, AVPacket *next, AVPacket *pkt)
Definition: mux.c:442
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:77
int channels
number of audio channels
Definition: avcodec.h:1808
void * priv_data
Format private data.
Definition: avformat.h:950
#define AVFMT_NODIMENSIONS
Format does not need width/height.
Definition: avformat.h:420
static int compute_pkt_fields2(AVFormatContext *s, AVStream *st, AVPacket *pkt)
Definition: mux.c:255
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:628
int(* write_packet)(struct AVFormatContext *, AVPacket *pkt)
Write a packet.
Definition: avformat.h:497
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:972
int av_write_trailer(AVFormatContext *s)
Write the stream trailer to an output media file and free the file private data.
Definition: mux.c:589
static void comp(unsigned char *dst, int dst_stride, unsigned char *src, int src_stride, int add)
Definition: eamad.c:83
unbuffered private I/O API
#define FFSWAP(type, a, b)
Definition: common.h:60
int stream_index
Definition: avcodec.h:975
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:741
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:431
This structure stores compressed data.
Definition: avcodec.h:950
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:205
int strict_std_compliance
strictly follow the standard (MPEG4, ...).
Definition: avcodec.h:2357
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:966
struct AVPacketList * last_in_packet_buffer
last packet in packet_buffer for this stream when muxing.
Definition: avformat.h:873
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:228