1 /*
2 * Copyright (c) 2012 Nicolas George
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public License
8 * as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with FFmpeg; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
30
35
40
53
54 typedef struct {
67
69 {
70 return memcmp(probe->
buf,
"ffconcat version 1.0", 20) ?
72 }
73
75 {
76 char *ret = *cursor += strspn(*cursor,
SPACE_CHARS);
78 if (**cursor) {
79 *((*cursor)++) = 0;
81 }
82 return ret;
83 }
84
86 {
87 const char *
start = f;
88
89 for (; *f; f++) {
90 /* A-Za-z0-9_- */
91 if (!((unsigned)((*f | 32) - 'a') < 26 ||
92 (unsigned)(*f - '0') < 10 || *f == '_' || *f == '-')) {
93 if (f == start)
94 return 0;
95 else if (*f == '/')
96 start = f + 1;
97 else if (*f != '.')
98 return 0;
99 }
100 }
101 return 1;
102 }
103
104 #define FAIL(retcode) do { ret = (retcode); goto fail; } while(0)
105
107 unsigned *nb_files_alloc)
108 {
112 const char *proto;
113 size_t url_len, proto_len;
114 int ret;
115
119 }
120
122 proto_len = proto ? strlen(proto) : 0;
123 if (!memcmp(filename, proto, proto_len) &&
124 (filename[proto_len] == ':' || filename[proto_len] == ',')) {
125 url = filename;
127 } else {
128 url_len = strlen(avf->
filename) + strlen(filename) + 16;
133 }
134
135 if (cat->
nb_files >= *nb_files_alloc) {
136 size_t n =
FFMAX(*nb_files_alloc * 2, 16);
138 if (n <= cat->nb_files || n > SIZE_MAX /
sizeof(*cat->
files) ||
141 cat->
files = new_files;
143 }
144
146 memset(file, 0, sizeof(*file));
147 *rfile = file;
148
154
155 return 0;
156
160 return ret;
161 }
162
164 {
165 int ret;
166
171 if (ret < 0)
172 return ret;
173 }
176 return 0;
177 }
179 return ret;
184
186 return 0;
187 }
188
190 {
195
199 "Auto-inserting h264_mp4toannexb bitstream filter\n");
202 "required for H.264 streams\n");
204 }
206 }
207 return 0;
208 }
209
211 {
214 int i, ret;
215
219 } else {
222 }
224 return ret;
226 }
227 return 0;
228 }
229
231 {
234 int i, j, ret;
235
241 "Match slave stream #%d with stream #%d id 0x%x\n",
244 return ret;
246 }
247 }
248 }
249 return 0;
250 }
251
253 {
256 int i, ret;
257
259 return 0;
262 if (!map)
267
273 break;
276 break;
277 default:
279 }
280 if (ret < 0)
281 return ret;
284 return ret;
286 return 0;
287 }
288
290 {
293 int ret;
294
297
301
303
305 return ret;
306
311 return ret;
312 }
322
327 }
328
330 return ret;
333 return ret;
334 }
335 return 0;
336 }
337
339 {
341 unsigned i;
342
345 for (i = 0; i < cat->
nb_files; i++) {
349 }
351 return 0;
352 }
353
355 {
359 int ret,
line = 0, i;
360 unsigned nb_files_alloc = 0;
362 int64_t time = 0;
363
364 while (1) {
366 break;
367 line++;
370 if (!*keyword || *keyword == '#')
371 continue;
372
373 if (!strcmp(keyword, "file")) {
375 if (!filename) {
378 }
379 if ((ret =
add_file(avf, filename, &file, &nb_files_alloc)) < 0)
381 } else if (!strcmp(keyword, "duration") || !strcmp(keyword, "inpoint") || !strcmp(keyword, "outpoint")) {
383 int64_t dur;
384 if (!file) {
386 line, keyword);
388 }
391 line, keyword, dur_str);
393 }
394 if (!strcmp(keyword, "duration"))
396 else if (!strcmp(keyword, "inpoint"))
398 else if (!strcmp(keyword, "outpoint"))
400 } else if (!strcmp(keyword, "file_packet_metadata")) {
401 char *metadata;
402 if (!file) {
404 line, keyword);
406 }
408 if (!metadata) {
411 }
416 }
418 } else if (!strcmp(keyword, "stream")) {
421 } else if (!strcmp(keyword, "exact_stream_id")) {
424 line);
426 }
429 } else if (!strcmp(keyword, "ffconcat")) {
432 if (strcmp(ver_kw, "version") || strcmp(ver_val, "1.0")) {
435 }
438 } else {
440 line, keyword);
442 }
443 }
444 if (ret < 0)
448
449 for (i = 0; i < cat->
nb_files; i++) {
452 else
456 break;
458 }
460 }
464 }
465
470 return 0;
471
474 return ret;
475 }
476
478 {
481
484
488 }
490 }
491
493 {
497 int ret;
498
500 for (bsf = cs->
bsf; bsf; bsf = bsf->
next) {
506 if (ret < 0) {
508 return ret;
509 }
511 if (ret == 0 && pkt2.
data != pkt->
data) {
514 return ret;
515 }
516 ret = 1;
517 }
518 if (ret > 0) {
525 }
526 }
527 *pkt = pkt2;
528 }
529 return 0;
530 }
531
532 /* Returns true if the packet dts is greater or equal to the specified outpoint. */
534 {
538 }
539 return 0;
540 }
541
543 {
545 int ret;
549
552
555
556 while (1) {
560 return ret;
561 continue;
562 }
563 if (ret < 0)
564 return ret;
567 return ret;
568 }
572 return ret;
573 continue;
574 }
578 continue;
579 }
581 break;
582 }
584 return ret;
585
591
604 int metadata_len;
606 if (!packed_metadata)
611 }
612 memcpy(metadata, packed_metadata, metadata_len);
614 }
615 return ret;
616 }
617
619 int64_t *min_ts, int64_t *ts, int64_t *max_ts)
620 {
626 }
627
629 int64_t min_ts, int64_t ts, int64_t max_ts,
int flags)
630 {
633
635 min_ts = min_ts == INT64_MIN ? INT64_MIN : min_ts -
t0;
636 max_ts = max_ts == INT64_MAX ? INT64_MAX : max_ts -
t0;
637 if (stream >= 0) {
641 &min_ts, &ts, &max_ts);
642 }
644 }
645
647 int64_t min_ts, int64_t ts, int64_t max_ts,
int flags)
648 {
650 int ret, left, right;
651
652 if (stream >= 0) {
656 &min_ts, &ts, &max_ts);
657 }
658
659 left = 0;
661 while (right - left > 1) {
662 int mid = (left + right) / 2;
664 right = mid;
665 else
666 left = mid;
667 }
668
670 return ret;
671
672 ret =
try_seek(avf, stream, min_ts, ts, max_ts, flags);
673 if (ret < 0 &&
674 left < cat->nb_files - 1 &&
676 if ((ret =
open_file(avf, left + 1)) < 0)
677 return ret;
678 ret =
try_seek(avf, stream, min_ts, ts, max_ts, flags);
679 }
680 return ret;
681 }
682
684 int64_t min_ts, int64_t ts, int64_t max_ts,
int flags)
685 {
689 int ret;
690
692 return AVERROR(ESPIPE);
/* XXX: can we use it? */
696 if ((ret =
real_seek(avf, stream, min_ts, ts, max_ts, flags)) < 0) {
699 cat->
avf = cur_avf_saved;
701 } else {
704 }
705 return ret;
706 }
707
708 #define OFFSET(x) offsetof(ConcatContext, x)
709 #define DEC AV_OPT_FLAG_DECODING_PARAM
710
712 { "safe", "enable safe mode",
714 { "auto_convert", "automatically convert bitstream format",
716 { "segment_time_metadata", "output file segment start time and duration as packet metadata",
719 };
720
726 };
727
728
738 .priv_class = &concat_class,
739 };
void ff_make_absolute_url(char *buf, int size, const char *base, const char *rel)
Convert a relative url into an absolute url, given a base url.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
AVIOInterruptCB interrupt_callback
Custom interrupt callbacks for the I/O layer.
static int concat_read_close(AVFormatContext *avf)
#define LIBAVUTIL_VERSION_INT
int av_parse_time(int64_t *timeval, const char *timestr, int duration)
Parse timestr and return in *time a corresponding number of microseconds.
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
static void rescale_interval(AVRational tb_in, AVRational tb_out, int64_t *min_ts, int64_t *ts, int64_t *max_ts)
int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
static const AVOption options[]
int avcodec_copy_context(AVCodecContext *dest, const AVCodecContext *src)
Copy the settings of the source AVCodecContext into the destination AVCodecContext.
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
#define av_assert0(cond)
assert() equivalent, that is always enabled.
static int concat_seek(AVFormatContext *avf, int stream, int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
static int64_t start_time
static int match_streams(AVFormatContext *avf)
timestamp utils, mostly useful for debugging/logging purposes
int id
Format-specific stream ID.
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
AVStream ** streams
A list of all streams in the file.
AVFormatContext * avformat_alloc_context(void)
Allocate an AVFormatContext.
AVInputFormat ff_concat_demuxer
#define AVERROR_EOF
End of file.
static av_cold int read_close(AVFormatContext *ctx)
#define AV_LOG_VERBOSE
Detailed information.
static int probe(AVProbeData *p)
struct AVBitStreamFilterContext * next
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
static int concat_read_header(AVFormatContext *avf)
void av_buffer_default_free(void *opaque, uint8_t *data)
Default free callback, which calls av_free() on the buffer data.
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
int segment_time_metadata
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
#define av_ts2timestr(ts, tb)
Convenience macro, the return value should be used only directly in function arguments but never stan...
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
static int concat_probe(AVProbeData *probe)
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values. ...
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
simple assert() macros that are a bit more flexible than ISO C assert().
AVBufferRef * av_buffer_create(uint8_t *data, int size, void(*free)(void *opaque, uint8_t *data), void *opaque, int flags)
Create an AVBuffer from an existing array.
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...
int flags
A combination of AV_PKT_FLAG values.
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.
AVCodecContext * codec
Codec context associated with this stream.
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
static int match_streams_one_to_one(AVFormatContext *avf)
int64_t av_rescale_q_rnd(int64_t a, AVRational bq, AVRational cq, enum AVRounding rnd)
Rescale a 64-bit integer by 2 rational numbers with specified rounding.
static int try_seek(AVFormatContext *avf, int stream, int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
char filename[1024]
input or output filename
static int packet_after_outpoint(ConcatContext *cat, AVPacket *pkt)
AVBitStreamFilterContext * av_bitstream_filter_init(const char *name)
Create and initialize a bitstream filter context given a bitstream filter name.
static int filter_packet(AVFormatContext *avf, ConcatStream *cs, AVPacket *pkt)
static const AVClass concat_class
static int open_file(AVFormatContext *avf, unsigned fileno)
#define AVERROR_BSF_NOT_FOUND
Bitstream filter not found.
int ff_get_line(AVIOContext *s, char *buf, int maxlen)
Read a whole line of text from AVIOContext.
ConcatMatchMode stream_match_mode
AVBitStreamFilterContext * bsf
static int read_header(FFV1Context *f)
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
int av_dict_parse_string(AVDictionary **pm, const char *str, const char *key_val_sep, const char *pairs_sep, int flags)
Parse the key/value pairs list and add the parsed entries to a dictionary.
#define AV_LOG_INFO
Standard information.
int av_bitstream_filter_filter(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args, uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size, int keyframe)
Filter bitstream.
A list of zero terminated key/value strings.
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
AVIOContext * pb
I/O context.
uint8_t * av_packet_pack_dictionary(AVDictionary *dict, int *size)
Pack a dictionary for use in side_data.
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
int av_copy_packet(AVPacket *dst, const AVPacket *src)
Copy packet, including contents.
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Describe the class of an AVClass context structure.
rational number numerator/denominator
static int match_streams_exact_id(AVFormatContext *avf)
This structure contains the data a format has to probe a file.
int av_read_frame(AVFormatContext *s, AVPacket *pkt)
Return the next frame of a stream.
int avformat_seek_file(AVFormatContext *s, int stream_index, int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
Seek to timestamp ts.
const char * avio_find_protocol_name(const char *url)
Return the name of the protocol that will handle the passed URL.
static int concat_read_packet(AVFormatContext *avf, AVPacket *pkt)
int64_t start_time
Position of the first frame of the component, in AV_TIME_BASE fractional seconds. ...
static char * get_keyword(uint8_t **cursor)
void * av_realloc(void *ptr, size_t size)
Allocate or reallocate a block of memory.
int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
Read packets of a media file to get stream information.
int av_dict_set_int(AVDictionary **pm, const char *key, int64_t value, int flags)
Convenience wrapper for av_dict_set that converts the value to a string and stores it...
void avformat_close_input(AVFormatContext **s)
Close an opened input AVFormatContext.
Flag to pass INT64_MIN/MAX through instead of rescaling, this avoids special cases for AV_NOPTS_VALUE...
static int open_next_file(AVFormatContext *avf)
static int copy_stream_props(AVStream *st, AVStream *source_st)
#define av_ts2str(ts)
Convenience macro, the return value should be used only directly in function arguments but never stan...
static int real_seek(AVFormatContext *avf, int stream, int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
void * priv_data
Format private data.
int avformat_open_input(AVFormatContext **ps, const char *url, AVInputFormat *fmt, AVDictionary **options)
Open an input stream and read the header.
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
int64_t duration
Duration of the stream, in AV_TIME_BASE fractional seconds.
static int add_file(AVFormatContext *avf, char *filename, ConcatFile **rfile, unsigned *nb_files_alloc)
unbuffered private I/O API
static int detect_stream_specific(AVFormatContext *avf, int idx)
static int safe_filename(const char *f)
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int size)
Allocate new information of a packet.
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.
This structure stores compressed data.
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
#define AV_NOPTS_VALUE
Undefined timestamp value.