FFmpeg
mux_utils.c
Go to the documentation of this file.
1 /*
2  * Various muxing utility functions
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 "libavutil/dict.h"
23 #include "libavutil/internal.h"
24 #include "libavutil/log.h"
25 #include "libavutil/mem.h"
26 #include "libavutil/parseutils.h"
27 #include "avformat.h"
28 #include "avio.h"
29 #include "internal.h"
30 #include "mux.h"
31 
32 int64_t av_stream_get_end_pts(const AVStream *st)
33 {
34  if (cffstream(st)->priv_pts) {
35  return cffstream(st)->priv_pts->val;
36  } else
37  return AV_NOPTS_VALUE;
38 }
39 
41  int std_compliance)
42 {
43  if (ofmt) {
44  unsigned int codec_tag;
45  if (ofmt->query_codec)
46  return ofmt->query_codec(codec_id, std_compliance);
47  else if (ofmt->codec_tag)
48  return !!av_codec_get_tag2(ofmt->codec_tag, codec_id, &codec_tag);
49  else if (codec_id == ofmt->video_codec ||
50  codec_id == ofmt->audio_codec ||
51  codec_id == ofmt->subtitle_codec ||
52  codec_id == ofmt->data_codec)
53  return 1;
54  }
55  return AVERROR_PATCHWELCOME;
56 }
57 
58 int ff_format_shift_data(AVFormatContext *s, int64_t read_start, int shift_size)
59 {
60  int ret;
61  int64_t pos, pos_end;
62  uint8_t *buf, *read_buf[2];
63  int read_buf_id = 0;
64  int read_size[2];
65  AVIOContext *read_pb;
66 
67  buf = av_malloc_array(shift_size, 2);
68  if (!buf)
69  return AVERROR(ENOMEM);
70  read_buf[0] = buf;
71  read_buf[1] = buf + shift_size;
72 
73  /* Shift the data: the AVIO context of the output can only be used for
74  * writing, so we re-open the same output, but for reading. It also avoids
75  * a read/seek/write/seek back and forth. */
76  avio_flush(s->pb);
77  ret = s->io_open(s, &read_pb, s->url, AVIO_FLAG_READ, NULL);
78  if (ret < 0) {
79  av_log(s, AV_LOG_ERROR, "Unable to re-open %s output file for shifting data\n", s->url);
80  goto end;
81  }
82 
83  /* mark the end of the shift to up to the last data we wrote, and get ready
84  * for writing */
85  pos_end = avio_tell(s->pb);
86  avio_seek(s->pb, read_start + shift_size, SEEK_SET);
87 
88  avio_seek(read_pb, read_start, SEEK_SET);
89  pos = avio_tell(read_pb);
90 
91 #define READ_BLOCK do { \
92  read_size[read_buf_id] = avio_read(read_pb, read_buf[read_buf_id], shift_size); \
93  read_buf_id ^= 1; \
94 } while (0)
95 
96  /* shift data by chunk of at most shift_size */
97  READ_BLOCK;
98  do {
99  int n;
100  READ_BLOCK;
101  n = read_size[read_buf_id];
102  if (n <= 0)
103  break;
104  avio_write(s->pb, read_buf[read_buf_id], n);
105  pos += n;
106  } while (pos < pos_end);
107  ret = ff_format_io_close(s, &read_pb);
108 
109 end:
110  av_free(buf);
111  return ret;
112 }
113 
115 {
116  if (!s->oformat)
117  return AVERROR(EINVAL);
118 
119  if (!(s->oformat->flags & AVFMT_NOFILE))
120  return s->io_open(s, &s->pb, url, AVIO_FLAG_WRITE, options);
121  return 0;
122 }
123 
125 {
126  int ret;
127 
128  dst->id = src->id;
129  dst->time_base = src->time_base;
130  dst->nb_frames = src->nb_frames;
131  dst->disposition = src->disposition;
132  dst->sample_aspect_ratio = src->sample_aspect_ratio;
133  dst->avg_frame_rate = src->avg_frame_rate;
134  dst->r_frame_rate = src->r_frame_rate;
135 
136  av_dict_free(&dst->metadata);
137  ret = av_dict_copy(&dst->metadata, src->metadata, 0);
138  if (ret < 0)
139  return ret;
140 
141  ret = avcodec_parameters_copy(dst->codecpar, src->codecpar);
142  if (ret < 0)
143  return ret;
144 
146  if (ret < 0)
147  return ret;
148 
149  return 0;
150 }
151 
152 int ff_parse_creation_time_metadata(AVFormatContext *s, int64_t *timestamp, int return_seconds)
153 {
154  AVDictionaryEntry *entry;
155  int64_t parsed_timestamp;
156  int ret;
157  if ((entry = av_dict_get(s->metadata, "creation_time", NULL, 0))) {
158  if ((ret = av_parse_time(&parsed_timestamp, entry->value, 0)) >= 0) {
159  *timestamp = return_seconds ? parsed_timestamp / 1000000 : parsed_timestamp;
160  return 1;
161  } else {
162  av_log(s, AV_LOG_WARNING, "Failed to parse creation_time %s\n", entry->value);
163  return ret;
164  }
165  }
166  return 0;
167 }
168 
170 {
171  int64_t timestamp;
172  int ret = ff_parse_creation_time_metadata(s, &timestamp, 0);
173  if (ret == 1)
174  return avpriv_dict_set_timestamp(&s->metadata, "creation_time", timestamp);
175  return ret;
176 }
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AVStream::avg_frame_rate
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:1028
AVDictionary
Definition: dict.c:30
cffstream
static const av_always_inline FFStream * cffstream(const AVStream *st)
Definition: internal.h:412
AVOutputFormat::subtitle_codec
enum AVCodecID subtitle_codec
default subtitle codec
Definition: avformat.h:522
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:505
av_codec_get_tag2
int av_codec_get_tag2(const struct AVCodecTag *const *tags, enum AVCodecID id, unsigned int *tag)
Get the codec tag for the given codec id.
FFStream::priv_pts
FFFrac * priv_pts
Definition: internal.h:245
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
AVOutputFormat::data_codec
enum AVCodecID data_codec
default data codec
Definition: avformat.h:617
av_dict_get
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:40
avformat_query_codec
int avformat_query_codec(const AVOutputFormat *ofmt, enum AVCodecID codec_id, int std_compliance)
Test if the given container can store a codec.
Definition: mux_utils.c:40
s
#define s(width, name)
Definition: cbs_vp9.c:256
AVOutputFormat::audio_codec
enum AVCodecID audio_codec
default audio codec
Definition: avformat.h:520
AVIO_FLAG_WRITE
#define AVIO_FLAG_WRITE
write-only
Definition: avio.h:629
AVOutputFormat::codec_tag
const struct AVCodecTag *const * codec_tag
List of supported codec_id-codec_tag pairs, ordered by "better choice first".
Definition: avformat.h:535
codec_id
enum AVCodecID codec_id
Definition: vaapi_decode.c:371
avio_flush
void avio_flush(AVIOContext *s)
Force flushing of buffered data.
Definition: aviobuf.c:252
AVFormatContext
Format I/O context.
Definition: avformat.h:1213
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1108
AVStream::time_base
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition: avformat.h:978
NULL
#define NULL
Definition: coverity.c:32
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
ff_standardize_creation_time
int ff_standardize_creation_time(AVFormatContext *s)
Standardize creation_time metadata in AVFormatContext to an ISO-8601 timestamp string.
Definition: mux_utils.c:169
parseutils.h
av_stream_get_end_pts
int64_t av_stream_get_end_pts(const AVStream *st)
Returns the pts of the last muxed packet + its duration.
Definition: mux_utils.c:32
AVStream::metadata
AVDictionary * metadata
Definition: avformat.h:1019
av_parse_time
int av_parse_time(int64_t *timeval, const char *timestr, int duration)
Parse timestr and return in *time a corresponding number of microseconds.
Definition: parseutils.c:589
AVStream::nb_frames
int64_t nb_frames
number of frames in this stream if known or 0
Definition: avformat.h:1000
AVCodecID
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:47
options
const OptionDef options[]
AVIOContext
Bytestream IO Context.
Definition: avio.h:162
FFFrac::val
int64_t val
Definition: internal.h:68
avio.h
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
AVFMT_NOFILE
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:470
ff_format_io_close
int ff_format_io_close(AVFormatContext *s, AVIOContext **pb)
Definition: avformat.c:779
AVStream::sample_aspect_ratio
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:1017
avio_write
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:232
ff_format_output_open
int ff_format_output_open(AVFormatContext *s, const char *url, AVDictionary **options)
Utility function to open IO stream of output format.
Definition: mux_utils.c:114
av_dict_free
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values.
Definition: dict.c:203
ff_parse_creation_time_metadata
int ff_parse_creation_time_metadata(AVFormatContext *s, int64_t *timestamp, int return_seconds)
Parse creation_time in AVFormatContext metadata if exists and warn if the parsing fails.
Definition: mux_utils.c:152
avcodec_parameters_copy
int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
Copy the contents of src to dst.
Definition: codec_par.c:74
log.h
AVOutputFormat
Definition: avformat.h:509
internal.h
av_malloc_array
#define av_malloc_array(a, b)
Definition: tableprint_vlc.h:31
ff_format_shift_data
int ff_format_shift_data(AVFormatContext *s, int64_t read_start, int shift_size)
Make shift_size amount of space at read_start by shifting data in the output at read_start until the ...
Definition: mux_utils.c:58
READ_BLOCK
#define READ_BLOCK
AVStream::disposition
int disposition
Stream disposition - a combination of AV_DISPOSITION_* flags.
Definition: avformat.h:1008
AVStream::id
int id
Format-specific stream ID.
Definition: avformat.h:962
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:948
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:260
pos
unsigned int pos
Definition: spdifenc.c:412
avformat.h
dict.h
ff_stream_encode_params_copy
int ff_stream_encode_params_copy(AVStream *dst, const AVStream *src)
Copy encoding parameters from source to destination stream.
Definition: mux_utils.c:124
ff_stream_side_data_copy
int ff_stream_side_data_copy(AVStream *dst, const AVStream *src)
Copy side data from source to destination stream.
Definition: avformat.c:208
AVOutputFormat::video_codec
enum AVCodecID video_codec
default video codec
Definition: avformat.h:521
AVStream::r_frame_rate
AVRational r_frame_rate
Real base framerate of the stream.
Definition: avformat.h:1097
AVIO_FLAG_READ
#define AVIO_FLAG_READ
read-only
Definition: avio.h:628
mem.h
av_free
#define av_free(p)
Definition: tableprint_vlc.h:33
AVDictionaryEntry
Definition: dict.h:79
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
av_dict_copy
int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
Definition: dict.c:217
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVDictionaryEntry::value
char * value
Definition: dict.h:81
AVOutputFormat::query_codec
int(* query_codec)(enum AVCodecID id, int std_compliance)
Test if the given codec can be stored in this container.
Definition: avformat.h:592
avpriv_dict_set_timestamp
int avpriv_dict_set_timestamp(AVDictionary **dict, const char *key, int64_t timestamp)
Set a dictionary value to an ISO-8601 compliant timestamp string.
Definition: dict.c:258
mux.h