FFmpeg
Loading...
Searching...
No Matches
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"
27#include "avformat.h"
28#include "avio.h"
29#include "internal.h"
30#include "mux.h"
31
33 int std_compliance)
34{
35 if (ofmt) {
36 unsigned int codec_tag;
37 if (ffofmt(ofmt)->query_codec)
38 return ffofmt(ofmt)->query_codec(codec_id, std_compliance);
39 else if (ofmt->codec_tag)
40 return !!av_codec_get_tag2(ofmt->codec_tag, codec_id, &codec_tag);
41 else if (codec_id != AV_CODEC_ID_NONE &&
42 (codec_id == ofmt->video_codec ||
43 codec_id == ofmt->audio_codec ||
44 codec_id == ofmt->subtitle_codec))
45 return 1;
46 else if (ffofmt(ofmt)->flags_internal & FF_OFMT_FLAG_ONLY_DEFAULT_CODECS)
47 return 0;
48 else if (ffofmt(ofmt)->flags_internal & FF_OFMT_FLAG_MAX_ONE_OF_EACH) {
50 switch (type) {
52 if (ofmt->audio_codec == AV_CODEC_ID_NONE)
53 return 0;
54 break;
56 if (ofmt->video_codec == AV_CODEC_ID_NONE)
57 return 0;
58 break;
61 return 0;
62 break;
63 default:
64 return 0;
65 }
66 }
67 }
69}
70
71int ff_format_shift_data(AVFormatContext *s, int64_t read_start, int shift_size)
72{
73 int ret;
74 int64_t pos, pos_end;
75 uint8_t *buf, *read_buf[2];
76 int read_buf_id = 0;
77 int read_size[2];
78 AVIOContext *read_pb;
79
80 buf = av_malloc_array(shift_size, 2);
81 if (!buf)
82 return AVERROR(ENOMEM);
83 read_buf[0] = buf;
84 read_buf[1] = buf + shift_size;
85
86 /* Shift the data: the AVIO context of the output can only be used for
87 * writing, so we re-open the same output, but for reading. It also avoids
88 * a read/seek/write/seek back and forth. */
89 avio_flush(s->pb);
90 ret = s->io_open(s, &read_pb, s->url, AVIO_FLAG_READ, NULL);
91 if (ret < 0) {
92 av_log(s, AV_LOG_ERROR, "Unable to re-open %s output file for shifting data\n", s->url);
93 goto end;
94 }
95
96 /* mark the end of the shift to up to the last data we wrote, and get ready
97 * for writing */
98 pos_end = avio_tell(s->pb);
99 avio_seek(s->pb, read_start + shift_size, SEEK_SET);
100
101 avio_seek(read_pb, read_start, SEEK_SET);
102 pos = avio_tell(read_pb);
103
104#define READ_BLOCK do { \
105 read_size[read_buf_id] = avio_read(read_pb, read_buf[read_buf_id], shift_size); \
106 read_buf_id ^= 1; \
107} while (0)
108
109 /* shift data by chunk of at most shift_size */
111 do {
112 int n;
114 n = read_size[read_buf_id];
115 if (n <= 0)
116 break;
117 avio_write(s->pb, read_buf[read_buf_id], n);
118 pos += n;
119 } while (pos < pos_end);
120 ret = ff_format_io_close(s, &read_pb);
121
122end:
123 av_free(buf);
124 return ret;
125}
126
128{
129 if (!s->oformat)
130 return AVERROR(EINVAL);
131
132 if (!(s->oformat->flags & AVFMT_NOFILE))
133 return s->io_open(s, &s->pb, url, AVIO_FLAG_WRITE, options);
134 return 0;
135}
136
137int ff_parse_creation_time_metadata(AVFormatContext *s, int64_t *timestamp, int return_seconds)
138{
140 int64_t parsed_timestamp;
141 int ret;
142 if ((entry = av_dict_get(s->metadata, "creation_time", NULL, 0))) {
143 if ((ret = av_parse_time(&parsed_timestamp, entry->value, 0)) >= 0) {
144 *timestamp = return_seconds ? parsed_timestamp / 1000000 : parsed_timestamp;
145 return 1;
146 } else {
147 av_log(s, AV_LOG_WARNING, "Failed to parse creation_time %s\n", entry->value);
148 return ret;
149 }
150 }
151 return 0;
152}
153
155{
156 int64_t timestamp;
157 int ret = ff_parse_creation_time_metadata(s, &timestamp, 0);
158 if (ret == 1)
159 return ff_dict_set_timestamp(&s->metadata, "creation_time", timestamp);
160 return ret;
161}
#define entry
int ff_format_io_close(AVFormatContext *s, AVIOContext **pb)
Definition avformat.c:961
Main libavformat public API header.
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition avformat.h:488
Buffered I/O operations.
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition aviobuf.c:236
#define AVIO_FLAG_READ
read-only
Definition avio.h:617
#define AVIO_FLAG_WRITE
write-only
Definition avio.h:618
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition avio.h:494
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition aviobuf.c:206
void avio_flush(AVIOContext *s)
Force flushing of buffered data.
Definition aviobuf.c:228
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
Public dictionary API.
enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
Get the type of the given codec.
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition codec_id.h:47
@ AV_CODEC_ID_NONE
Definition codec_id.h:48
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:32
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.
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:60
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition error.h:64
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
AVMediaType
Definition avutil.h:198
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AVMEDIA_TYPE_SUBTITLE
Definition avutil.h:203
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
cl_device_type type
static int query_codec(enum AVCodecID id, int std_compliance)
Definition img2enc.c:361
int ff_dict_set_timestamp(AVDictionary **dict, const char *key, int64_t timestamp)
Set a dictionary value to an ISO-8601 compliant timestamp string.
Definition utils.c:616
common internal API header
Memory handling functions.
#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
#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
static const FFOutputFormat * ffofmt(const AVOutputFormat *fmt)
Definition mux.h:167
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:127
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:71
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:137
int ff_standardize_creation_time(AVFormatContext *s)
Standardize creation_time metadata in AVFormatContext to an ISO-8601 timestamp string.
Definition mux_utils.c:154
#define READ_BLOCK
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
misc parsing utilities
unsigned int pos
Definition spdifenc.c:431
Format I/O context.
Definition avformat.h:1333
Bytestream IO Context.
Definition avio.h:160
enum AVCodecID video_codec
default video codec
Definition avformat.h:538
enum AVCodecID audio_codec
default audio codec
Definition avformat.h:537
const struct AVCodecTag *const * codec_tag
List of supported codec_id-codec_tag pairs, ordered by "betterchoice first".
Definition avformat.h:552
enum AVCodecID subtitle_codec
default subtitle codec
Definition avformat.h:539
int(* query_codec)(enum AVCodecID id, int std_compliance)
Test if the given codec can be stored in this container.
Definition mux.h:111
#define av_free(p)
#define av_malloc_array(a, b)
#define av_log(a,...)
enum AVCodecID codec_id