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
31
36
41
55
56 typedef struct {
69
71 {
72 return memcmp(probe->
buf,
"ffconcat version 1.0", 20) ?
74 }
75
77 {
78 char *ret = *cursor += strspn(*cursor,
SPACE_CHARS);
80 if (**cursor) {
81 *((*cursor)++) = 0;
83 }
84 return ret;
85 }
86
88 {
89 const char *
start = f;
90
91 for (; *f; f++) {
92 /* A-Za-z0-9_- */
93 if (!((unsigned)((*f | 32) - 'a') < 26 ||
94 (unsigned)(*f - '0') < 10 || *f == '_' || *f == '-')) {
95 if (f == start)
96 return 0;
97 else if (*f == '/')
98 start = f + 1;
99 else if (*f != '.')
100 return 0;
101 }
102 }
103 return 1;
104 }
105
106 #define FAIL(retcode) do { ret = (retcode); goto fail; } while(0)
107
109 unsigned *nb_files_alloc)
110 {
114 const char *proto;
115 size_t url_len, proto_len;
116 int ret;
117
121 }
122
124 proto_len = proto ? strlen(proto) : 0;
125 if (proto && !memcmp(filename, proto, proto_len) &&
126 (filename[proto_len] == ':' || filename[proto_len] == ',')) {
127 url = filename;
129 } else {
130 url_len = strlen(avf->
url) + strlen(filename) + 16;
135 }
136
137 if (cat->
nb_files >= *nb_files_alloc) {
138 size_t n =
FFMAX(*nb_files_alloc * 2, 16);
140 if (n <= cat->nb_files || n > SIZE_MAX /
sizeof(*cat->
files) ||
143 cat->
files = new_files;
145 }
146
148 memset(file, 0, sizeof(*file));
149 *rfile = file;
150
157
158 return 0;
159
163 return ret;
164 }
165
167 {
168 int ret;
169
175 }
178 if (ret < 0)
179 return ret;
180 }
183 return 0;
184 }
186 return ret;
191
193 return 0;
194 }
195
197 {
203 int ret;
204
209 return 0;
211 "Auto-inserting h264_mp4toannexb bitstream filter\n");
213 if (!filter) {
215 "required for H.264 streams\n");
217 }
219 if (ret < 0)
220 return ret;
222
224 if (ret < 0)
225 return ret;
226
228 if (ret < 0)
229 return ret;
230
232 if (ret < 0)
233 return ret;
234 }
235 return 0;
236 }
237
239 {
242 int i, ret;
243
247 } else {
250 }
252 return ret;
254 }
255 return 0;
256 }
257
259 {
262 int i, j, ret;
263
269 "Match slave stream #%d with stream #%d id 0x%x\n",
272 return ret;
274 }
275 }
276 }
277 return 0;
278 }
279
281 {
284 int i, ret;
285
287 return 0;
290 if (!map)
295
299 return ret;
300 }
304 break;
307 break;
308 default:
310 }
311 if (ret < 0)
312 return ret;
314 return 0;
315 }
316
318 {
321 int ret;
322
325
329
332
334 return ret;
335
340 return ret;
341 }
351
356 }
357
359 return ret;
362 return ret;
363 }
364 return 0;
365 }
366
368 {
370 unsigned i, j;
371
372 for (i = 0; i < cat->
nb_files; i++) {
377 }
380 }
384 return 0;
385 }
386
388 {
390 AVBPrint bp;
393 unsigned nb_files_alloc = 0;
395 int64_t ret, time = 0;
396
398
400 line++;
401 cursor = bp.str;
403 if (!*keyword || *keyword == '#')
404 continue;
405
406 if (!strcmp(keyword, "file")) {
408 if (!filename) {
411 }
412 if ((ret =
add_file(avf, filename, &file, &nb_files_alloc)) < 0)
414 } else if (!strcmp(keyword, "duration") || !strcmp(keyword, "inpoint") || !strcmp(keyword, "outpoint")) {
416 int64_t dur;
417 if (!file) {
419 line, keyword);
421 }
424 line, keyword, dur_str);
426 }
427 if (!strcmp(keyword, "duration"))
429 else if (!strcmp(keyword, "inpoint"))
431 else if (!strcmp(keyword, "outpoint"))
433 } else if (!strcmp(keyword, "file_packet_metadata")) {
434 char *metadata;
435 if (!file) {
437 line, keyword);
439 }
441 if (!metadata) {
444 }
449 }
451 } else if (!strcmp(keyword, "stream")) {
454 } else if (!strcmp(keyword, "exact_stream_id")) {
457 line);
459 }
462 } else if (!strcmp(keyword, "ffconcat")) {
465 if (strcmp(ver_kw, "version") || strcmp(ver_val, "1.0")) {
468 }
471 } else {
473 line, keyword);
475 }
476 }
481
482 for (i = 0; i < cat->
nb_files; i++) {
485 else
489 break;
491 }
493 }
497 }
498
504 return 0;
505
509 return ret;
510 }
511
513 {
516
520 } else {
522 }
524 }
525
529 }
531 }
532
534 {
535 int ret;
536
539 if (ret < 0) {
541 "failed to send input packet\n");
543 return ret;
544 }
545
546 while (!ret)
548
551 "failed to receive output packet\n");
552 return ret;
553 }
554 }
555 return 0;
556 }
557
558 /* Returns true if the packet dts is greater or equal to the specified outpoint. */
560 {
564 }
565 return 0;
566 }
567
569 {
571 int ret;
575
578
581
582 while (1) {
586 return ret;
587 continue;
588 }
589 if (ret < 0)
590 return ret;
593 return ret;
594 }
598 return ret;
599 continue;
600 }
604 continue;
605 }
606 break;
607 }
609 return ret;
610
616
629 int metadata_len;
631 if (!packed_metadata)
636 }
637 memcpy(metadata, packed_metadata, metadata_len);
639 }
640
645 }
646 }
647
649 return ret;
650 }
651
653 int64_t *min_ts, int64_t *ts, int64_t *max_ts)
654 {
660 }
661
663 int64_t min_ts, int64_t ts, int64_t max_ts,
int flags)
664 {
667
669 min_ts = min_ts == INT64_MIN ? INT64_MIN : min_ts -
t0;
670 max_ts = max_ts == INT64_MAX ? INT64_MAX : max_ts -
t0;
671 if (stream >= 0) {
675 &min_ts, &ts, &max_ts);
676 }
678 }
679
682 {
684 int ret, left, right;
685
686 if (stream >= 0) {
690 &min_ts, &ts, &max_ts);
691 }
692
693 left = 0;
695 while (right - left > 1) {
696 int mid = (left + right) / 2;
698 right = mid;
699 else
700 left = mid;
701 }
702
705 return ret;
706 } else {
708 }
709
710 ret =
try_seek(avf, stream, min_ts, ts, max_ts, flags);
711 if (ret < 0 &&
712 left < cat->nb_files - 1 &&
716 if ((ret =
open_file(avf, left + 1)) < 0)
717 return ret;
718 ret =
try_seek(avf, stream, min_ts, ts, max_ts, flags);
719 }
720 return ret;
721 }
722
724 int64_t min_ts, int64_t ts, int64_t max_ts,
int flags)
725 {
729 int ret;
730
732 return AVERROR(ESPIPE);
/* XXX: can we use it? */
736 if ((ret =
real_seek(avf, stream, min_ts, ts, max_ts, flags, cur_avf_saved)) < 0) {
737 if (cat->
cur_file != cur_file_saved) {
740 }
741 cat->
avf = cur_avf_saved;
743 } else {
744 if (cat->
cur_file != cur_file_saved) {
746 }
748 }
749 return ret;
750 }
751
752 #define OFFSET(x) offsetof(ConcatContext, x)
753 #define DEC AV_OPT_FLAG_DECODING_PARAM
754
756 { "safe", "enable safe mode",
758 { "auto_convert", "automatically convert bitstream format",
760 { "segment_time_metadata", "output file segment start time and duration as packet metadata",
763 };
764
770 };
771
772
782 .priv_class = &concat_class,
783 };
void av_bsf_free(AVBSFContext **ctx)
Free a bitstream filter context and everything associated with it; write NULL into the supplied point...
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.
AVCodecParameters * par_out
Parameters of the output stream.
AVIOInterruptCB interrupt_callback
Custom interrupt callbacks for the I/O layer.
void * av_realloc(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory.
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.
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
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)
The bitstream filter state.
const char * av_default_item_name(void *ptr)
Return the context name.
const AVBitStreamFilter * av_bsf_get_by_name(const char *name)
int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
static const AVOption options[]
int av_bsf_init(AVBSFContext *ctx)
Prepare the filter for use, after all the parameters and options have been set.
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
int av_bsf_alloc(const AVBitStreamFilter *filter, AVBSFContext **ctx)
Allocate a context for a given bitstream filter.
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
int av_bsf_receive_packet(AVBSFContext *ctx, AVPacket *pkt)
Retrieve a filtered packet.
static void filter(int16_t *output, ptrdiff_t out_stride, int16_t *low, ptrdiff_t low_stride, int16_t *high, ptrdiff_t high_stride, int len, int clip)
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.
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
int flags
Flags modifying the (de)muxer behaviour.
#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)
static int concat_read_header(AVFormatContext *avf)
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
int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
Copy the contents of src to dst.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
#define AV_BPRINT_SIZE_UNLIMITED
#define av_ts2timestr(ts, tb)
Convenience macro, the return value should be used only directly in function arguments but never stan...
int64_t ff_read_line_to_bprint_overwrite(AVIOContext *s, AVBPrint *bp)
Read a whole line of text from AVIOContext to an AVBPrint buffer overwriting its contents.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
char * url
input or output URL.
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. ...
simple assert() macros that are a bit more flexible than ISO C assert().
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 av_compare_ts(int64_t ts_a, AVRational tb_a, int64_t ts_b, AVRational tb_b)
Compare two timestamps each in its own time base.
int extradata_size
Size of the extradata content in bytes.
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)
static int try_seek(AVFormatContext *avf, int stream, int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
static int packet_after_outpoint(ConcatContext *cat, AVPacket *pkt)
static int filter_packet(AVFormatContext *avf, ConcatStream *cs, AVPacket *pkt)
static const AVClass concat_class
int av_bsf_send_packet(AVBSFContext *ctx, AVPacket *pkt)
Submit a packet for filtering.
static int open_file(AVFormatContext *avf, unsigned fileno)
#define AVERROR_BSF_NOT_FOUND
Bitstream filter not found.
ConcatMatchMode stream_match_mode
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.
A list of zero terminated key/value strings.
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_WB32 unsigned int_TMPL AV_RB24
#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.
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Describe the class of an AVClass context structure.
Rational number (pair of numerator and denominator).
const VDPAUPixFmtMap * map
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)
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 telling rescaling functions to pass INT64_MIN/MAX through unchanged, avoiding special cases for ...
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...
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.
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
static int real_seek(AVFormatContext *avf, int stream, int64_t min_ts, int64_t ts, int64_t max_ts, int flags, AVFormatContext *cur_avf)
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
AVCodecParameters * codecpar
Codec parameters associated with this stream.
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 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.
AVCodecParameters * par_in
Parameters of the input stream.
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.