1 /*
2 * Tee pseudo-muxer
3 * Copyright (c) 2012 Nicolas George
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 License
9 * 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
15 * GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with FFmpeg; if not, write to the Free Software * Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22
29
31
35
36 /** map from input to output streams indexes,
37 * disabled output streams are set to -1 */
40
46
53
58 };
59
62 {
63 const char *p;
65 int ret;
66
68 *filename = slave;
69 return 0;
70 }
71 p = slave + 1;
73 *filename = (char *)p + 1;
74 return 0;
75 }
76 while (1) {
78 if (ret < 0) {
81 }
84 if (ret < 0)
87 break;
88 p++;
89 }
90 *filename = (char *)p + 1;
91 return 0;
92
95 return ret;
96 }
97
98 /**
99 * Parse list of bitstream filters and add them to the list of filters
100 * pointed to by bsfs.
101 *
102 * The list must be specified in the form:
103 * BSFS ::= BSF[,BSFS]
104 */
107 {
108 char *bsf_name, *
buf, *dup, *saveptr;
109 int ret = 0;
110
113
114 while (bsf_name =
av_strtok(buf,
",", &saveptr)) {
116
117 if (!bsf) {
119 "Cannot initialize bitstream filter with name '%s', "
120 "unknown filter or internal error happened\n",
121 bsf_name);
124 }
125
126 /* append bsf context to the list of bsf contexts */
127 *bsfs = bsf;
129
131 }
132
135 return ret;
136 }
137
139 {
140 int i, ret;
143 char *filename;
147 int stream_count;
148 int fullret;
149 char *subselect =
NULL, *next_subselect =
NULL, *first_subselect =
NULL, *tmp_select =
NULL;
150
152 return ret;
153
154 #define STEAL_OPTION(option, field) do { \
155 if ((entry = av_dict_get(options, option, NULL, 0))) { \
156 field = entry->value; \
157 entry->value = NULL; /* prevent it from being freed */ \
158 av_dict_set(&options, option, NULL, 0); \
159 } \
160 } while (0)
161
164
166 if (ret < 0)
172
177 }
178
179 stream_count = 0;
182 if (select) {
183 tmp_select =
av_strdup(select);
// av_strtok is destructive so we regenerate it in each loop
184 if (!tmp_select) {
187 }
188 fullret = 0;
189 first_subselect = tmp_select;
190 next_subselect =
NULL;
192 first_subselect =
NULL;
193
195 if (ret < 0) {
197 "Invalid stream specifier '%s' for output '%s'\n",
198 subselect, slave);
200 }
201 if (ret != 0) {
202 fullret = 1; // match
203 break;
204 }
205 }
207
208 if (fullret == 0) { /* no match */
210 continue;
211 }
212 }
214
218 }
231 }
232
238 }
239 }
240
245 }
246
247 tee_slave->
avf = avf2;
249 if (!tee_slave->
bsfs) {
252 }
253
256 const char *spec = entry->
key + strlen(
"bsfs");
257 if (*spec) {
260 "Specifier separator in '%s' is '%c', but only characters '%s' "
263 }
264 spec++; /* consume separator */
265 }
266
269 if (ret < 0) {
271 "Invalid stream specifier '%s' in bsfs option '%s' for slave "
272 "output '%s'\n", spec, entry->
key, filename);
274 }
275
276 if (ret > 0) {
278 "output '%s'\n", spec, entry->
value, i, filename);
279 if (tee_slave->
bsfs[i]) {
281 "Duplicate bsfs specification associated to stream %d of slave "
282 "output '%s', filters will be ignored\n", i, filename);
283 continue;
284 }
286 if (ret < 0) {
288 "Error parsing bitstream filter sequence '%s' associated to "
289 "stream %d of slave output '%s'\n", entry->
value, i, filename);
291 }
292 }
293 }
294
296 }
297
298 if (options) {
304 }
305
311 return ret;
312 }
313
315 {
318 unsigned i, j;
319
322
325 while (bsf) {
326 bsf_next = bsf->
next;
328 bsf = bsf_next;
329 }
330 }
333
337 }
338 }
339
341 {
342 int i;
343 av_log(log_ctx, log_level,
"filename:'%s' format:%s\n",
348
349 av_log(log_ctx, log_level,
" stream:%d codec:%s type:%s",
352 if (bsf) {
353 av_log(log_ctx, log_level,
" bsfs:");
354 while (bsf) {
355 av_log(log_ctx, log_level,
"%s%s",
358 }
359 }
360 av_log(log_ctx, log_level,
"\n");
361 }
362 }
363
365 {
367 unsigned nb_slaves = 0, i;
368 const char *filename = avf->
filename;
370 int ret;
371
372 while (*filename) {
378 }
382 }
384 filename++;
385 }
386
387 for (i = 0; i < nb_slaves; i++) {
392 }
393
395
397 int j, mapped = 0;
402 "to any slave.\n", i);
403 }
404 return 0;
405
407 for (i = 0; i < nb_slaves; i++)
410 return ret;
411 }
412
414 {
417 int ret_all = 0, ret;
418 unsigned i;
419
423 if (!ret_all)
424 ret_all = ret;
427 }
429 return ret_all;
430 }
431
433 {
437 int ret_all = 0, ret;
441
446 if (s2 < 0)
447 continue;
448
451 if (!ret_all) {
452 ret_all = ret;
453 continue;
454 }
461
465 if (!ret_all)
466 ret_all = ret;
467 }
468 return ret_all;
469 }
470
478 .priv_class = &tee_muxer_class,
480 };
const char const char void * val
static int tee_write_packet(AVFormatContext *avf, AVPacket *pkt)
int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt)
Write a packet to an output media file ensuring correct interleaving.
AVBitStreamFilterContext ** bsfs
bitstream filters per stream
static const char *const slave_opt_delim
#define AV_LOG_WARNING
Something somehow does not look correct.
#define LIBAVUTIL_VERSION_INT
static int tee_write_trailer(AVFormatContext *avf)
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
#define AVIO_FLAG_WRITE
write-only
static const char *const slave_bsfs_spec_sep
static void log_slave(TeeSlave *slave, void *log_ctx, int log_level)
int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
int avcodec_copy_context(AVCodecContext *dest, const AVCodecContext *src)
Copy the settings of the source AVCodecContext into the destination AVCodecContext.
static const char *const slave_delim
#define AV_DICT_DONT_STRDUP_KEY
Take ownership of a key that's been allocated with av_malloc() or another memory allocation function...
static const char *const slave_opt_close
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
AVOutputFormat ff_tee_muxer
static av_cold int end(AVCodecContext *avctx)
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
int id
Format-specific stream ID.
static void close_slaves(AVFormatContext *avf)
int avformat_match_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
Check if the stream st contained in s is matched by the stream specifier spec.
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
AVStream ** streams
A list of all streams in the file.
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
#define AV_LOG_VERBOSE
Detailed information.
const OptionDef options[]
struct AVBitStreamFilterContext * next
static int open_slave(AVFormatContext *avf, char *slave, TeeSlave *tee_slave)
static int parse_slave_options(void *log, char *slave, AVDictionary **options, char **filename)
static const char *const slave_select_sep
struct AVOutputFormat * oformat
The output container format.
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
AVDictionary * metadata
Metadata that applies to the whole file.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
attribute_deprecated int av_dup_packet(AVPacket *pkt)
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
void av_bitstream_filter_close(AVBitStreamFilterContext *bsf)
Release bitstream filter context.
int * stream_map
map from input to output streams indexes, disabled output streams are set to -1
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values. ...
AVRational avg_frame_rate
Average framerate.
char * av_get_token(const char **buf, const char *term)
Unescape the given string until a non escaped terminating char, and return the token corresponding to...
AVCodecContext * codec
Codec context associated with this stream.
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
char filename[1024]
input or output filename
av_warn_unused_result int avformat_write_header(AVFormatContext *s, AVDictionary **options)
Allocate the stream private data and write the stream header to an output media file.
static const AVClass tee_muxer_class
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that's been allocated with av_malloc() or another memory allocation functio...
AVBitStreamFilterContext * av_bitstream_filter_init(const char *name)
Create and initialize a bitstream filter context given a bitstream filter name.
static int write_trailer(AVFormatContext *s1)
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
TeeSlave slaves[MAX_SLAVES]
const char * avcodec_get_name(enum AVCodecID id)
Get the name of a codec.
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
enum AVMediaType codec_type
char * av_strdup(const char *s)
Duplicate the string s.
AVIOContext * pb
I/O context.
static const char *const slave_opt_open
static const char * format
int av_copy_packet(AVPacket *dst, const AVPacket *src)
Copy packet, including contents.
int av_apply_bitstream_filters(AVCodecContext *codec, AVPacket *pkt, AVBitStreamFilterContext *bsfc)
Apply a list of bitstream filters to a packet.
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
struct AVBitStreamFilter * filter
Describe the class of an AVClass context structure.
rational number numerator/denominator
static int parse_bsfs(void *log_ctx, const char *bsfs_spec, AVBitStreamFilterContext **bsfs)
Parse list of bitstream filters and add them to the list of filters pointed to by bsfs...
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
void * av_calloc(size_t nmemb, size_t size)
Allocate a block of nmemb * size bytes with alignment suitable for all memory accesses (including vec...
const char * av_get_media_type_string(enum AVMediaType media_type)
Return a string describing the media_type enum, NULL if media_type is unknown.
char * av_strtok(char *s, const char *delim, char **saveptr)
Split the string into several tokens which can be accessed by successive calls to av_strtok()...
int64_t duration
Decoding: duration of the stream, in stream time base.
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base...
int disposition
AV_DISPOSITION_* bit field.
int64_t nb_frames
number of frames in this stream if known or 0
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
#define AVERROR_OPTION_NOT_FOUND
Option not found.
int av_opt_get_key_value(const char **ropts, const char *key_val_sep, const char *pairs_sep, unsigned flags, char **rkey, char **rval)
Extract a key-value pair from the beginning of a string.
void * priv_data
Format private data.
static void write_header(FFV1Context *f)
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
int av_write_trailer(AVFormatContext *s)
Write the stream trailer to an output media file and free the file private data.
#define AV_DICT_IGNORE_SUFFIX
Return first entry in a dictionary whose first part corresponds to the search key, ignoring the suffix of the found key string.
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
AVRational r_frame_rate
Real base framerate of the stream.
int(* io_open)(struct AVFormatContext *s, AVIOContext **pb, const char *url, int flags, AVDictionary **options)
This structure stores compressed data.
static int write_packet(AVFormatContext *s1, AVPacket *pkt)
void(* io_close)(struct AVFormatContext *s, AVIOContext *pb)
A callback for closing the streams opened with AVFormatContext.io_open().
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
static int tee_write_header(AVFormatContext *avf)
#define STEAL_OPTION(option, field)