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"
24 #include "libavutil/internal.h"
25 #include "libavutil/log.h"
26 #include "libavutil/mem.h"
27 #include "libavutil/parseutils.h"
28 #include "avformat.h"
29 #include "avio.h"
30 #include "internal.h"
31 #include "mux.h"
32 
34  int std_compliance)
35 {
36  if (ofmt) {
37  unsigned int codec_tag;
38  if (ffofmt(ofmt)->query_codec)
39  return ffofmt(ofmt)->query_codec(codec_id, std_compliance);
40  else if (ofmt->codec_tag)
41  return !!av_codec_get_tag2(ofmt->codec_tag, codec_id, &codec_tag);
42  else if (codec_id != AV_CODEC_ID_NONE &&
43  (codec_id == ofmt->video_codec ||
44  codec_id == ofmt->audio_codec ||
45  codec_id == ofmt->subtitle_codec))
46  return 1;
47  else if (ffofmt(ofmt)->flags_internal & FF_OFMT_FLAG_ONLY_DEFAULT_CODECS)
48  return 0;
49  else if (ffofmt(ofmt)->flags_internal & FF_OFMT_FLAG_MAX_ONE_OF_EACH) {
51  switch (type) {
52  case AVMEDIA_TYPE_AUDIO:
53  if (ofmt->audio_codec == AV_CODEC_ID_NONE)
54  return 0;
55  break;
56  case AVMEDIA_TYPE_VIDEO:
57  if (ofmt->video_codec == AV_CODEC_ID_NONE)
58  return 0;
59  break;
61  if (ofmt->subtitle_codec == AV_CODEC_ID_NONE)
62  return 0;
63  break;
64  default:
65  return 0;
66  }
67  }
68  }
69  return AVERROR_PATCHWELCOME;
70 }
71 
72 int ff_format_shift_data(AVFormatContext *s, int64_t read_start, int shift_size)
73 {
74  int ret;
75  int64_t pos, pos_end;
76  uint8_t *buf, *read_buf[2];
77  int read_buf_id = 0;
78  int read_size[2];
79  AVIOContext *read_pb;
80 
81  buf = av_malloc_array(shift_size, 2);
82  if (!buf)
83  return AVERROR(ENOMEM);
84  read_buf[0] = buf;
85  read_buf[1] = buf + shift_size;
86 
87  /* Shift the data: the AVIO context of the output can only be used for
88  * writing, so we re-open the same output, but for reading. It also avoids
89  * a read/seek/write/seek back and forth. */
90  avio_flush(s->pb);
91  ret = s->io_open(s, &read_pb, s->url, AVIO_FLAG_READ, NULL);
92  if (ret < 0) {
93  av_log(s, AV_LOG_ERROR, "Unable to re-open %s output file for shifting data\n", s->url);
94  goto end;
95  }
96 
97  /* mark the end of the shift to up to the last data we wrote, and get ready
98  * for writing */
99  pos_end = avio_tell(s->pb);
100  avio_seek(s->pb, read_start + shift_size, SEEK_SET);
101 
102  avio_seek(read_pb, read_start, SEEK_SET);
103  pos = avio_tell(read_pb);
104 
105 #define READ_BLOCK do { \
106  read_size[read_buf_id] = avio_read(read_pb, read_buf[read_buf_id], shift_size); \
107  read_buf_id ^= 1; \
108 } while (0)
109 
110  /* shift data by chunk of at most shift_size */
111  READ_BLOCK;
112  do {
113  int n;
114  READ_BLOCK;
115  n = read_size[read_buf_id];
116  if (n <= 0)
117  break;
118  avio_write(s->pb, read_buf[read_buf_id], n);
119  pos += n;
120  } while (pos < pos_end);
121  ret = ff_format_io_close(s, &read_pb);
122 
123 end:
124  av_free(buf);
125  return ret;
126 }
127 
129 {
130  if (!s->oformat)
131  return AVERROR(EINVAL);
132 
133  if (!(s->oformat->flags & AVFMT_NOFILE))
134  return s->io_open(s, &s->pb, url, AVIO_FLAG_WRITE, options);
135  return 0;
136 }
137 
138 int ff_parse_creation_time_metadata(AVFormatContext *s, int64_t *timestamp, int return_seconds)
139 {
141  int64_t parsed_timestamp;
142  int ret;
143  if ((entry = av_dict_get(s->metadata, "creation_time", NULL, 0))) {
144  if ((ret = av_parse_time(&parsed_timestamp, entry->value, 0)) >= 0) {
145  *timestamp = return_seconds ? parsed_timestamp / 1000000 : parsed_timestamp;
146  return 1;
147  } else {
148  av_log(s, AV_LOG_WARNING, "Failed to parse creation_time %s\n", entry->value);
149  return ret;
150  }
151  }
152  return 0;
153 }
154 
156 {
157  int64_t timestamp;
158  int ret = ff_parse_creation_time_metadata(s, &timestamp, 0);
159  if (ret == 1)
160  return avpriv_dict_set_timestamp(&s->metadata, "creation_time", timestamp);
161  return ret;
162 }
AVMEDIA_TYPE_SUBTITLE
@ AVMEDIA_TYPE_SUBTITLE
Definition: avutil.h:204
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:215
entry
#define entry
Definition: aom_film_grain_template.c:66
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
avcodec_get_type
enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
Get the type of the given codec.
Definition: codec_desc.c:3784
int64_t
long long int64_t
Definition: coverity.c:34
dict_internal.h
AVDictionary
Definition: dict.c:34
FF_OFMT_FLAG_ONLY_DEFAULT_CODECS
#define FF_OFMT_FLAG_ONLY_DEFAULT_CODECS
If this flag is set, then the only permitted audio/video/subtitle codec ids are AVOutputFormat....
Definition: mux.h:59
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:494
query_codec
static int query_codec(enum AVCodecID id, int std_compliance)
Definition: img2enc.c:243
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
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.
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
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:62
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:33
s
#define s(width, name)
Definition: cbs_vp9.c:198
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
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:618
ffofmt
static const FFOutputFormat * ffofmt(const AVOutputFormat *fmt)
Definition: mux.h:167
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:410
avio_flush
void avio_flush(AVIOContext *s)
Force flushing of buffered data.
Definition: aviobuf.c:223
AVFormatContext
Format I/O context.
Definition: avformat.h:1300
internal.h
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:155
parseutils.h
options
Definition: swscale.c:42
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:592
AVCodecID
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:49
AVIOContext
Bytestream IO Context.
Definition: avio.h:160
AVMediaType
AVMediaType
Definition: avutil.h:199
FFOutputFormat::query_codec
int(* query_codec)(enum AVCodecID id, int std_compliance)
Test if the given codec can be stored in this container.
Definition: mux.h:111
avio.h
AVFMT_NOFILE
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:468
ff_format_io_close
int ff_format_io_close(AVFormatContext *s, AVIOContext **pb)
Definition: avformat.c:958
avio_write
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:201
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:128
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:138
log.h
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: codec_id.h:50
AVOutputFormat
Definition: avformat.h:509
internal.h
av_malloc_array
#define av_malloc_array(a, b)
Definition: tableprint_vlc.h:31
FF_OFMT_FLAG_MAX_ONE_OF_EACH
#define FF_OFMT_FLAG_MAX_ONE_OF_EACH
If this flag is set, it indicates that for each codec type whose corresponding default codec (i....
Definition: mux.h:50
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:72
READ_BLOCK
#define READ_BLOCK
ret
ret
Definition: filter_design.txt:187
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:231
pos
unsigned int pos
Definition: spdifenc.c:414
avformat.h
dict.h
AVOutputFormat::video_codec
enum AVCodecID video_codec
default video codec
Definition: avformat.h:521
AVIO_FLAG_READ
#define AVIO_FLAG_READ
read-only
Definition: avio.h:617
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
mem.h
av_free
#define av_free(p)
Definition: tableprint_vlc.h:33
AVDictionaryEntry
Definition: dict.h:89
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
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:278
mux.h