2
* various utility functions for use within FFmpeg
2
* various utility functions for use within Libav
3
3
* Copyright (c) 2000, 2001, 2002 Fabrice Bellard
5
* This file is part of FFmpeg.
5
* This file is part of Libav.
7
* FFmpeg is free software; you can redistribute it and/or
7
* Libav is free software; you can redistribute it and/or
8
8
* modify it under the terms of the GNU Lesser General Public
9
9
* License as published by the Free Software Foundation; either
10
10
* version 2.1 of the License, or (at your option) any later version.
12
* FFmpeg is distributed in the hope that it will be useful,
12
* Libav is distributed in the hope that it will be useful,
13
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
15
* Lesser General Public License for more details.
17
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
18
* License along with Libav; if not, write to the Free Software
19
19
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
24
#include "avformat.h"
25
#include "avio_internal.h"
22
26
#include "internal.h"
23
#include "libavcodec/opt.h"
27
#include "libavcodec/internal.h"
28
#include "libavutil/opt.h"
24
29
#include "metadata.h"
25
31
#include "libavutil/avstring.h"
27
33
#include "audiointerleave.h"
28
35
#include <sys/time.h>
30
37
#include <strings.h>
198
211
AVOutputFormat *av_guess_format(const char *short_name, const char *filename,
199
212
const char *mime_type)
201
AVOutputFormat *fmt, *fmt_found;
214
AVOutputFormat *fmt = NULL, *fmt_found;
202
215
int score_max, score;
204
217
/* specific test for image sequences */
205
218
#if CONFIG_IMAGE2_MUXER
206
219
if (!short_name && filename &&
207
220
av_filename_number_test(filename) &&
208
av_guess_image2_codec(filename) != CODEC_ID_NONE) {
221
ff_guess_image2_codec(filename) != CODEC_ID_NONE) {
209
222
return av_guess_format("image2", NULL, NULL);
212
225
/* Find the proper file type. */
213
226
fmt_found = NULL;
216
while (fmt != NULL) {
228
while ((fmt = av_oformat_next(fmt))) {
218
230
if (fmt->name && short_name && !strcmp(fmt->name, short_name))
269
280
}else if(type == AVMEDIA_TYPE_AUDIO)
270
281
return fmt->audio_codec;
282
else if (type == AVMEDIA_TYPE_SUBTITLE)
283
return fmt->subtitle_codec;
272
285
return CODEC_ID_NONE;
275
288
AVInputFormat *av_find_input_format(const char *short_name)
278
for(fmt = first_iformat; fmt != NULL; fmt = fmt->next) {
290
AVInputFormat *fmt = NULL;
291
while ((fmt = av_iformat_next(fmt))) {
279
292
if (match_format(short_name, fmt->name))
285
#if LIBAVFORMAT_VERSION_MAJOR < 53 && CONFIG_SHARED && HAVE_SYMVER
298
#if FF_API_SYMVER && CONFIG_SHARED && HAVE_SYMVER
286
299
FF_SYMVER(void, av_destruct_packet_nofree, (AVPacket *pkt), "LIBAVFORMAT_52")
288
301
av_destruct_packet_nofree(pkt);
349
int av_append_packet(AVIOContext *s, AVPacket *pkt, int size)
354
return av_get_packet(s, pkt, size);
355
old_size = pkt->size;
356
ret = av_grow_packet(pkt, size);
359
ret = avio_read(s, pkt->data + old_size, size);
360
av_shrink_packet(pkt, old_size + FFMAX(ret, 0));
337
365
int av_filename_number_test(const char *filename)
343
371
AVInputFormat *av_probe_input_format2(AVProbeData *pd, int is_opened, int *score_max)
345
AVInputFormat *fmt1, *fmt;
373
AVProbeData lpd = *pd;
374
AVInputFormat *fmt1 = NULL, *fmt;
377
if (lpd.buf_size > 10 && ff_id3v2_match(lpd.buf, ID3v2_DEFAULT_MAGIC)) {
378
int id3len = ff_id3v2_tag_len(lpd.buf);
379
if (lpd.buf_size > id3len + 16) {
381
lpd.buf_size -= id3len;
349
for(fmt1 = first_iformat; fmt1 != NULL; fmt1 = fmt1->next) {
387
while ((fmt1 = av_iformat_next(fmt1))) {
350
388
if (!is_opened == !(fmt1->flags & AVFMT_NOFILE))
353
391
if (fmt1->read_probe) {
354
score = fmt1->read_probe(pd);
392
score = fmt1->read_probe(&lpd);
355
393
} else if (fmt1->extensions) {
356
if (av_match_ext(pd->filename, fmt1->extensions)) {
394
if (av_match_ext(lpd.filename, fmt1->extensions)) {
374
422
static int set_codec_from_probe_data(AVFormatContext *s, AVStream *st, AVProbeData *pd, int score)
377
fmt = av_probe_input_format2(pd, 1, &score);
424
static const struct {
425
const char *name; enum CodecID id; enum AVMediaType type;
427
{ "aac" , CODEC_ID_AAC , AVMEDIA_TYPE_AUDIO },
428
{ "ac3" , CODEC_ID_AC3 , AVMEDIA_TYPE_AUDIO },
429
{ "dts" , CODEC_ID_DTS , AVMEDIA_TYPE_AUDIO },
430
{ "eac3" , CODEC_ID_EAC3 , AVMEDIA_TYPE_AUDIO },
431
{ "h264" , CODEC_ID_H264 , AVMEDIA_TYPE_VIDEO },
432
{ "m4v" , CODEC_ID_MPEG4 , AVMEDIA_TYPE_VIDEO },
433
{ "mp3" , CODEC_ID_MP3 , AVMEDIA_TYPE_AUDIO },
434
{ "mpegvideo", CODEC_ID_MPEG2VIDEO, AVMEDIA_TYPE_VIDEO },
437
AVInputFormat *fmt = av_probe_input_format2(pd, 1, &score);
380
441
av_log(s, AV_LOG_DEBUG, "Probe with size=%d, packets=%d detected %s with score=%d\n",
381
442
pd->buf_size, MAX_PROBE_PACKETS - st->probe_packets, fmt->name, score);
382
if (!strcmp(fmt->name, "mp3")) {
383
st->codec->codec_id = CODEC_ID_MP3;
384
st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
385
} else if (!strcmp(fmt->name, "ac3")) {
386
st->codec->codec_id = CODEC_ID_AC3;
387
st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
388
} else if (!strcmp(fmt->name, "eac3")) {
389
st->codec->codec_id = CODEC_ID_EAC3;
390
st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
391
} else if (!strcmp(fmt->name, "mpegvideo")) {
392
st->codec->codec_id = CODEC_ID_MPEG2VIDEO;
393
st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
394
} else if (!strcmp(fmt->name, "m4v")) {
395
st->codec->codec_id = CODEC_ID_MPEG4;
396
st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
397
} else if (!strcmp(fmt->name, "h264")) {
398
st->codec->codec_id = CODEC_ID_H264;
399
st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
400
} else if (!strcmp(fmt->name, "dts")) {
401
st->codec->codec_id = CODEC_ID_DTS;
402
st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
403
} else if (!strcmp(fmt->name, "aac")) {
404
st->codec->codec_id = CODEC_ID_AAC;
405
st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
443
for (i = 0; fmt_id_type[i].name; i++) {
444
if (!strcmp(fmt->name, fmt_id_type[i].name)) {
445
st->codec->codec_id = fmt_id_type[i].id;
446
st->codec->codec_type = fmt_id_type[i].type;
583
632
hack needed to handle RTSP/TCP */
584
633
if (!fmt || !(fmt->flags & AVFMT_NOFILE)) {
585
634
/* if no file needed do not try to open one */
586
if ((err=url_fopen(&pb, filename, URL_RDONLY)) < 0) {
635
if ((err=avio_open(&pb, filename, AVIO_RDONLY)) < 0) {
589
638
if (buf_size > 0) {
590
url_setbufsize(pb, buf_size);
639
ffio_set_buf_size(pb, buf_size);
592
if (!fmt && (err = ff_probe_input_buffer(&pb, &fmt, filename, logctx, 0, logctx ? (*ic_ptr)->probesize : 0)) < 0) {
641
if (!fmt && (err = av_probe_input_buffer(pb, &fmt, filename, logctx, 0, logctx ? (*ic_ptr)->probesize : 0)) < 0) {
1078
1133
pkt->pts = st->parser->pts;
1079
1134
pkt->dts = st->parser->dts;
1080
1135
pkt->pos = st->parser->pos;
1136
if(pkt->data == st->cur_pkt.data && pkt->size == st->cur_pkt.size){
1138
pkt->destruct= st->cur_pkt.destruct;
1139
st->cur_pkt.destruct= NULL;
1140
st->cur_pkt.data = NULL;
1141
assert(st->cur_len == 0);
1081
1143
pkt->destruct = NULL;
1082
1145
compute_pkt_fields(s, st, st->parser, pkt);
1084
1147
if((s->iformat->flags & AVFMT_GENERIC_INDEX) && pkt->flags & AV_PKT_FLAG_KEY){
1183
1244
AVPacket *next_pkt= &pktl->pkt;
1185
1246
if(genpts && next_pkt->dts != AV_NOPTS_VALUE){
1247
int wrap_bits = s->streams[next_pkt->stream_index]->pts_wrap_bits;
1186
1248
while(pktl && next_pkt->pts == AV_NOPTS_VALUE){
1187
1249
if( pktl->pkt.stream_index == next_pkt->stream_index
1188
&& next_pkt->dts < pktl->pkt.dts
1189
&& pktl->pkt.pts != pktl->pkt.dts //not b frame
1190
/*&& pktl->pkt.dts != AV_NOPTS_VALUE*/){
1250
&& (0 > av_compare_mod(next_pkt->dts, pktl->pkt.dts, 2LL << (wrap_bits - 1)))
1251
&& av_compare_mod(pktl->pkt.pts, pktl->pkt.dts, 2LL << (wrap_bits - 1))) { //not b frame
1191
1252
next_pkt->pts= pktl->pkt.dts;
1193
1254
pktl= pktl->next;
1334
int av_add_index_entry(AVStream *st,
1335
int64_t pos, int64_t timestamp, int size, int distance, int flags)
1395
int ff_add_index_entry(AVIndexEntry **index_entries,
1396
int *nb_index_entries,
1397
unsigned int *index_entries_allocated_size,
1398
int64_t pos, int64_t timestamp, int size, int distance, int flags)
1337
1400
AVIndexEntry *entries, *ie;
1340
if((unsigned)st->nb_index_entries + 1 >= UINT_MAX / sizeof(AVIndexEntry))
1403
if((unsigned)*nb_index_entries + 1 >= UINT_MAX / sizeof(AVIndexEntry))
1343
entries = av_fast_realloc(st->index_entries,
1344
&st->index_entries_allocated_size,
1345
(st->nb_index_entries + 1) *
1406
entries = av_fast_realloc(*index_entries,
1407
index_entries_allocated_size,
1408
(*nb_index_entries + 1) *
1346
1409
sizeof(AVIndexEntry));
1350
st->index_entries= entries;
1413
*index_entries= entries;
1352
index= av_index_search_timestamp(st, timestamp, AVSEEK_FLAG_ANY);
1415
index= ff_index_search_timestamp(*index_entries, *nb_index_entries, timestamp, AVSEEK_FLAG_ANY);
1355
index= st->nb_index_entries++;
1418
index= (*nb_index_entries)++;
1356
1419
ie= &entries[index];
1357
1420
assert(index==0 || ie[-1].timestamp < timestamp);
1378
int av_index_search_timestamp(AVStream *st, int64_t wanted_timestamp,
1381
AVIndexEntry *entries= st->index_entries;
1382
int nb_entries= st->nb_index_entries;
1441
int av_add_index_entry(AVStream *st,
1442
int64_t pos, int64_t timestamp, int size, int distance, int flags)
1444
return ff_add_index_entry(&st->index_entries, &st->nb_index_entries,
1445
&st->index_entries_allocated_size, pos,
1446
timestamp, size, distance, flags);
1449
int ff_index_search_timestamp(const AVIndexEntry *entries, int nb_entries,
1450
int64_t wanted_timestamp, int flags)
1384
1453
int64_t timestamp;
1608
1684
pos_min = s->data_offset;
1609
pos_max = url_fsize(s->pb) - 1;
1685
pos_max = avio_size(s->pb) - 1;
1611
1687
if (pos < pos_min) pos= pos_min;
1612
1688
else if(pos > pos_max) pos= pos_max;
1614
url_fseek(s->pb, pos, SEEK_SET);
1690
avio_seek(s->pb, pos, SEEK_SET);
1617
1693
av_update_cur_dts(s, st, ts);
1641
1717
if(st->nb_index_entries){
1642
1718
assert(st->index_entries);
1643
1719
ie= &st->index_entries[st->nb_index_entries-1];
1644
if ((ret = url_fseek(s->pb, ie->pos, SEEK_SET)) < 0)
1720
if ((ret = avio_seek(s->pb, ie->pos, SEEK_SET)) < 0)
1646
1722
av_update_cur_dts(s, st, ie->timestamp);
1648
if ((ret = url_fseek(s->pb, s->data_offset, SEEK_SET)) < 0)
1724
if ((ret = avio_seek(s->pb, s->data_offset, SEEK_SET)) < 0)
1651
1727
for(i=0;; i++) {
1913
1986
read_size += pkt->size;
1914
1987
st = ic->streams[pkt->stream_index];
1915
1988
if (pkt->pts != AV_NOPTS_VALUE &&
1916
start_time[pkt->stream_index] != AV_NOPTS_VALUE) {
1917
end_time = pkt->pts;
1918
duration = end_time - start_time[pkt->stream_index];
1989
(st->start_time != AV_NOPTS_VALUE ||
1990
st->first_dts != AV_NOPTS_VALUE)) {
1991
duration = end_time = pkt->pts;
1992
if (st->start_time != AV_NOPTS_VALUE) duration -= st->start_time;
1993
else duration -= st->first_dts;
1919
1994
if (duration < 0)
1920
1995
duration += 1LL<<st->pts_wrap_bits;
1921
1996
if (duration > 0) {
2103
2181
static void compute_chapters_end(AVFormatContext *s)
2184
int64_t max_time = s->duration + (s->start_time == AV_NOPTS_VALUE) ? 0 : s->start_time;
2107
for (i=0; i+1<s->nb_chapters; i++)
2186
for (i = 0; i < s->nb_chapters; i++)
2108
2187
if (s->chapters[i]->end == AV_NOPTS_VALUE) {
2109
assert(s->chapters[i]->start <= s->chapters[i+1]->start);
2110
assert(!av_cmp_q(s->chapters[i]->time_base, s->chapters[i+1]->time_base));
2111
s->chapters[i]->end = s->chapters[i+1]->start;
2188
AVChapter *ch = s->chapters[i];
2189
int64_t end = max_time ? av_rescale_q(max_time, AV_TIME_BASE_Q, ch->time_base)
2192
for (j = 0; j < s->nb_chapters; j++) {
2193
AVChapter *ch1 = s->chapters[j];
2194
int64_t next_start = av_rescale_q(ch1->start, ch1->time_base, ch->time_base);
2195
if (j != i && next_start > ch->start && next_start < end)
2198
ch->end = (end == INT64_MAX) ? ch->start : end;
2114
if (s->nb_chapters && s->chapters[i]->end == AV_NOPTS_VALUE) {
2115
assert(s->start_time != AV_NOPTS_VALUE);
2116
assert(s->duration > 0);
2117
s->chapters[i]->end = av_rescale_q(s->start_time + s->duration,
2119
s->chapters[i]->time_base);
2123
#define MAX_STD_TIMEBASES (60*12+5)
2124
2202
static int get_std_framerate(int i){
2125
2203
if(i<60*12) return i*1001;
2126
2204
else return ((const int[]){24,30,60,12,15})[i-60*12]*1000*12;
2151
2229
int i, count, ret, read_size, j;
2153
2231
AVPacket pkt1, *pkt;
2154
int64_t last_dts[MAX_STREAMS];
2155
int64_t duration_gcd[MAX_STREAMS]={0};
2156
int duration_count[MAX_STREAMS]={0};
2157
double (*duration_error)[MAX_STD_TIMEBASES];
2158
int64_t old_offset = url_ftell(ic->pb);
2159
int64_t codec_info_duration[MAX_STREAMS]={0};
2161
duration_error = av_mallocz(MAX_STREAMS * sizeof(*duration_error));
2162
if (!duration_error) return AVERROR(ENOMEM);
2232
int64_t old_offset = avio_tell(ic->pb);
2164
2234
for(i=0;i<ic->nb_streams;i++) {
2165
2236
st = ic->streams[i];
2166
2237
if (st->codec->codec_id == CODEC_ID_AAC) {
2167
2238
st->codec->sample_rate = 0;
2168
2239
st->codec->frame_size = 0;
2169
2240
st->codec->channels = 0;
2171
if(st->codec->codec_type == AVMEDIA_TYPE_VIDEO){
2242
if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO ||
2243
st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
2172
2244
/* if(!st->time_base.num)
2173
2245
st->time_base= */
2174
2246
if(!st->codec->time_base.num)
2184
2256
assert(!st->codec->codec);
2257
codec = avcodec_find_decoder(st->codec->codec_id);
2259
/* Force decoding of at least one frame of codec data
2260
* this makes sure the codec initializes the channel configuration
2261
* and does not trust the values from the container.
2263
if (codec && codec->capabilities & CODEC_CAP_CHANNEL_CONF)
2264
st->codec->channels = 0;
2266
/* Ensure that subtitle_header is properly set. */
2267
if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE
2268
&& codec && !st->codec->codec)
2269
avcodec_open(st->codec, codec);
2185
2271
//try to just open decoders, in case this is enough to get parameters
2186
2272
if(!has_codec_parameters(st->codec)){
2187
AVCodec *codec = avcodec_find_decoder(st->codec->codec_id);
2273
if (codec && !st->codec->codec)
2189
2274
avcodec_open(st->codec, codec);
2193
for(i=0;i<MAX_STREAMS;i++){
2194
last_dts[i]= AV_NOPTS_VALUE;
2278
for (i=0; i<ic->nb_streams; i++) {
2279
ic->streams[i]->info->last_dts = AV_NOPTS_VALUE;
2200
2285
if(url_interrupt_cb()){
2201
ret= AVERROR(EINTR);
2202
2287
av_log(ic, AV_LOG_DEBUG, "interrupted\n");
2342
if (ret == AVERROR(EAGAIN))
2259
2345
pkt= add_to_pktbuf(&ic->packet_buffer, &pkt1, &ic->packet_buffer_end);
2260
if(av_dup_packet(pkt) < 0) {
2261
av_free(duration_error);
2262
return AVERROR(ENOMEM);
2346
if ((ret = av_dup_packet(pkt)) < 0)
2347
goto find_stream_info_err;
2265
2349
read_size += pkt->size;
2267
2351
st = ic->streams[pkt->stream_index];
2268
if(st->codec_info_nb_frames>1) {
2269
if (st->time_base.den > 0 && av_rescale_q(codec_info_duration[st->index], st->time_base, AV_TIME_BASE_Q) >= ic->max_analyze_duration){
2352
if (st->codec_info_nb_frames>1) {
2353
if (st->time_base.den > 0 && av_rescale_q(st->info->codec_info_duration, st->time_base, AV_TIME_BASE_Q) >= ic->max_analyze_duration) {
2270
2354
av_log(ic, AV_LOG_WARNING, "max_analyze_duration reached\n");
2273
codec_info_duration[st->index] += pkt->duration;
2357
st->info->codec_info_duration += pkt->duration;
2275
st->codec_info_nb_frames++;
2278
int index= pkt->stream_index;
2279
int64_t last= last_dts[index];
2360
int64_t last = st->info->last_dts;
2280
2361
int64_t duration= pkt->dts - last;
2282
2363
if(pkt->dts != AV_NOPTS_VALUE && last != AV_NOPTS_VALUE && duration>0){
2285
2366
// if(st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
2286
2367
// av_log(NULL, AV_LOG_ERROR, "%f\n", dur);
2287
if(duration_count[index] < 2)
2288
memset(duration_error[index], 0, sizeof(*duration_error));
2289
for(i=1; i<MAX_STD_TIMEBASES; i++){
2368
if (st->info->duration_count < 2)
2369
memset(st->info->duration_error, 0, sizeof(st->info->duration_error));
2370
for (i=1; i<FF_ARRAY_ELEMS(st->info->duration_error); i++) {
2290
2371
int framerate= get_std_framerate(i);
2291
2372
int ticks= lrintf(dur*framerate/(1001*12));
2292
2373
double error= dur - ticks*1001*12/(double)framerate;
2293
duration_error[index][i] += error*error;
2374
st->info->duration_error[i] += error*error;
2295
duration_count[index]++;
2376
st->info->duration_count++;
2296
2377
// ignore the first 4 values, they might have some random jitter
2297
if (duration_count[index] > 3)
2298
duration_gcd[index] = av_gcd(duration_gcd[index], duration);
2378
if (st->info->duration_count > 3)
2379
st->info->duration_gcd = av_gcd(st->info->duration_gcd, duration);
2300
if(last == AV_NOPTS_VALUE || duration_count[index]<=1)
2301
last_dts[pkt->stream_index]= pkt->dts;
2381
if (last == AV_NOPTS_VALUE || st->info->duration_count <= 1)
2382
st->info->last_dts = pkt->dts;
2303
2384
if(st->parser && st->parser->parser->split && !st->codec->extradata){
2304
2385
int i= st->parser->parser->split(st->codec, pkt->data, pkt->size);
2329
2411
for(i=0;i<ic->nb_streams;i++) {
2330
2412
st = ic->streams[i];
2331
if(st->codec_info_nb_frames>2 && !st->avg_frame_rate.num && codec_info_duration[i])
2413
if (st->codec_info_nb_frames>2 && !st->avg_frame_rate.num && st->info->codec_info_duration)
2332
2414
av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,
2333
2415
(st->codec_info_nb_frames-2)*(int64_t)st->time_base.den,
2334
codec_info_duration[i] *(int64_t)st->time_base.num, 60000);
2416
st->info->codec_info_duration*(int64_t)st->time_base.num, 60000);
2335
2417
if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
2336
2418
if(st->codec->codec_id == CODEC_ID_RAWVIDEO && !st->codec->codec_tag && !st->codec->bits_per_coded_sample)
2337
2419
st->codec->codec_tag= avcodec_pix_fmt_to_codec_tag(st->codec->pix_fmt);
2339
2421
// the check for tb_unreliable() is not completely correct, since this is not about handling
2340
2422
// a unreliable/inexact time base, but a time base that is finer than necessary, as e.g.
2341
2423
// ipmovie.c produces.
2342
if (tb_unreliable(st->codec) && duration_count[i] > 15 && duration_gcd[i] > 1 && !st->r_frame_rate.num)
2343
av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den, st->time_base.den, st->time_base.num * duration_gcd[i], INT_MAX);
2344
if(duration_count[i] && !st->r_frame_rate.num
2424
if (tb_unreliable(st->codec) && st->info->duration_count > 15 && st->info->duration_gcd > 1 && !st->r_frame_rate.num)
2425
av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den, st->time_base.den, st->time_base.num * st->info->duration_gcd, INT_MAX);
2426
if (st->info->duration_count && !st->r_frame_rate.num
2345
2427
&& tb_unreliable(st->codec) /*&&
2346
2428
//FIXME we should not special-case MPEG-2, but this needs testing with non-MPEG-2 ...
2347
st->time_base.num*duration_sum[i]/duration_count[i]*101LL > st->time_base.den*/){
2429
st->time_base.num*duration_sum[i]/st->info->duration_count*101LL > st->time_base.den*/){
2349
2431
double best_error= 2*av_q2d(st->time_base);
2350
best_error= best_error*best_error*duration_count[i]*1000*12*30;
2432
best_error = best_error*best_error*st->info->duration_count*1000*12*30;
2352
for(j=1; j<MAX_STD_TIMEBASES; j++){
2353
double error= duration_error[i][j] * get_std_framerate(j);
2434
for (j=1; j<FF_ARRAY_ELEMS(st->info->duration_error); j++) {
2435
double error = st->info->duration_error[j] * get_std_framerate(j);
2354
2436
// if(st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
2355
2437
// av_log(NULL, AV_LOG_ERROR, "%f %f\n", get_std_framerate(j) / 12.0/1001, error);
2356
2438
if(error < best_error){
2376
2458
}else if(st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
2377
2459
if(!st->codec->bits_per_coded_sample)
2378
2460
st->codec->bits_per_coded_sample= av_get_bits_per_sample(st->codec->codec_id);
2461
// set stream disposition based on audio service type
2462
switch (st->codec->audio_service_type) {
2463
case AV_AUDIO_SERVICE_TYPE_EFFECTS:
2464
st->disposition = AV_DISPOSITION_CLEAN_EFFECTS; break;
2465
case AV_AUDIO_SERVICE_TYPE_VISUALLY_IMPAIRED:
2466
st->disposition = AV_DISPOSITION_VISUAL_IMPAIRED; break;
2467
case AV_AUDIO_SERVICE_TYPE_HEARING_IMPAIRED:
2468
st->disposition = AV_DISPOSITION_HEARING_IMPAIRED; break;
2469
case AV_AUDIO_SERVICE_TYPE_COMMENTARY:
2470
st->disposition = AV_DISPOSITION_COMMENT; break;
2471
case AV_AUDIO_SERVICE_TYPE_KARAOKE:
2472
st->disposition = AV_DISPOSITION_KARAOKE; break;
2411
av_free(duration_error);
2506
find_stream_info_err:
2507
for (i=0; i < ic->nb_streams; i++)
2508
av_freep(&ic->streams[i]->info);
2512
static AVProgram *find_program_from_stream(AVFormatContext *ic, int s)
2516
for (i = 0; i < ic->nb_programs; i++)
2517
for (j = 0; j < ic->programs[i]->nb_stream_indexes; j++)
2518
if (ic->programs[i]->stream_index[j] == s)
2519
return ic->programs[i];
2523
int av_find_best_stream(AVFormatContext *ic,
2524
enum AVMediaType type,
2525
int wanted_stream_nb,
2527
AVCodec **decoder_ret,
2530
int i, nb_streams = ic->nb_streams, stream_number = 0;
2531
int ret = AVERROR_STREAM_NOT_FOUND, best_count = -1;
2532
unsigned *program = NULL;
2533
AVCodec *decoder = NULL, *best_decoder = NULL;
2535
if (related_stream >= 0 && wanted_stream_nb < 0) {
2536
AVProgram *p = find_program_from_stream(ic, related_stream);
2538
program = p->stream_index;
2539
nb_streams = p->nb_stream_indexes;
2542
for (i = 0; i < nb_streams; i++) {
2543
AVStream *st = ic->streams[program ? program[i] : i];
2544
AVCodecContext *avctx = st->codec;
2545
if (avctx->codec_type != type)
2547
if (wanted_stream_nb >= 0 && stream_number++ != wanted_stream_nb)
2549
if (st->disposition & (AV_DISPOSITION_HEARING_IMPAIRED|AV_DISPOSITION_VISUAL_IMPAIRED))
2552
decoder = avcodec_find_decoder(ic->streams[i]->codec->codec_id);
2555
ret = AVERROR_DECODER_NOT_FOUND;
2559
if (best_count >= st->codec_info_nb_frames)
2561
best_count = st->codec_info_nb_frames;
2562
ret = program ? program[i] : i;
2563
best_decoder = decoder;
2564
if (program && i == nb_streams - 1 && ret < 0) {
2566
nb_streams = ic->nb_streams;
2567
i = 0; /* no related stream found, try again with everything */
2571
*decoder_ret = best_decoder;
2429
2588
if (s->iformat->read_pause)
2430
2589
return s->iformat->read_pause(s);
2432
return av_url_read_fpause(s->pb, 1);
2591
return avio_pause(s->pb, 1);
2433
2592
return AVERROR(ENOSYS);
2436
2595
void av_close_input_stream(AVFormatContext *s)
2597
flush_packet_queue(s);
2441
2598
if (s->iformat->read_close)
2442
2599
s->iformat->read_close(s);
2600
avformat_free_context(s);
2603
void avformat_free_context(AVFormatContext *s)
2443
2608
for(i=0;i<s->nb_streams;i++) {
2444
2609
/* free all data in a stream component */
2445
2610
st = s->streams[i];
2796
static int validate_codec_tag(AVFormatContext *s, AVStream *st)
2798
const AVCodecTag *avctag;
2800
enum CodecID id = CODEC_ID_NONE;
2801
unsigned int tag = 0;
2804
* Check that tag + id is in the table
2805
* If neither is in the table -> OK
2806
* If tag is in the table with another id -> FAIL
2807
* If id is in the table with another tag -> FAIL unless strict < normal
2809
for (n = 0; s->oformat->codec_tag[n]; n++) {
2810
avctag = s->oformat->codec_tag[n];
2811
while (avctag->id != CODEC_ID_NONE) {
2812
if (ff_toupper4(avctag->tag) == ff_toupper4(st->codec->codec_tag)) {
2814
if (id == st->codec->codec_id)
2817
if (avctag->id == st->codec->codec_id)
2822
if (id != CODEC_ID_NONE)
2824
if (tag && (st->codec->strict_std_compliance >= FF_COMPLIANCE_NORMAL))
2608
2829
int av_write_header(AVFormatContext *s)
2613
2834
// some sanity checks
2614
if (s->nb_streams == 0) {
2835
if (s->nb_streams == 0 && !(s->oformat->flags & AVFMT_NOSTREAMS)) {
2615
2836
av_log(s, AV_LOG_ERROR, "no streams\n");
2837
return AVERROR(EINVAL);
2619
2840
for(i=0;i<s->nb_streams;i++) {
2632
2853
case AVMEDIA_TYPE_VIDEO:
2633
2854
if(st->codec->time_base.num<=0 || st->codec->time_base.den<=0){ //FIXME audio too?
2634
2855
av_log(s, AV_LOG_ERROR, "time base not set\n");
2856
return AVERROR(EINVAL);
2637
2858
if((st->codec->width<=0 || st->codec->height<=0) && !(s->oformat->flags & AVFMT_NODIMENSIONS)){
2638
2859
av_log(s, AV_LOG_ERROR, "dimensions not set\n");
2860
return AVERROR(EINVAL);
2641
2862
if(av_cmp_q(st->sample_aspect_ratio, st->codec->sample_aspect_ratio)){
2642
2863
av_log(s, AV_LOG_ERROR, "Aspect ratio mismatch between encoder and muxer layer\n");
2864
return AVERROR(EINVAL);
2648
2869
if(s->oformat->codec_tag){
2870
if(st->codec->codec_tag && st->codec->codec_id == CODEC_ID_RAWVIDEO && av_codec_get_tag(s->oformat->codec_tag, st->codec->codec_id) == 0 && !validate_codec_tag(s, st)){
2871
//the current rawvideo encoding system ends up setting the wrong codec_tag for avi, we override it here
2872
st->codec->codec_tag= 0;
2649
2874
if(st->codec->codec_tag){
2651
//check that tag + id is in the table
2652
//if neither is in the table -> OK
2653
//if tag is in the table with another id -> FAIL
2654
//if id is in the table with another tag -> FAIL unless strict < ?
2875
if (!validate_codec_tag(s, st)) {
2877
av_get_codec_tag_string(tagbuf, sizeof(tagbuf), st->codec->codec_tag);
2878
av_log(s, AV_LOG_ERROR,
2879
"Tag %s/0x%08x incompatible with output codec id '%d'\n",
2880
tagbuf, st->codec->codec_tag, st->codec->codec_id);
2881
return AVERROR_INVALIDDATA;
2656
2884
st->codec->codec_tag= av_codec_get_tag(s->oformat->codec_tag, st->codec->codec_id);
2667
2895
return AVERROR(ENOMEM);
2670
#if LIBAVFORMAT_VERSION_MAJOR < 53
2898
#if FF_API_OLD_METADATA
2671
2899
ff_metadata_mux_compat(s);
2674
2902
/* set muxer identification string */
2675
if (!(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT)) {
2679
if (!(m = av_mallocz(sizeof(AVMetadata))))
2680
return AVERROR(ENOMEM);
2681
av_metadata_set2(&m, "encoder", LIBAVFORMAT_IDENT, 0);
2682
metadata_conv(&m, s->oformat->metadata_conv, NULL);
2683
if ((t = av_metadata_get(m, "", NULL, AV_METADATA_IGNORE_SUFFIX)))
2684
av_metadata_set2(&s->metadata, t->key, t->value, 0);
2685
av_metadata_free(&m);
2903
if (s->nb_streams && !(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT)) {
2904
av_metadata_set2(&s->metadata, "encoder", LIBAVFORMAT_IDENT, 0);
2688
2907
if(s->oformat->write_header){
2720
2939
int delay = FFMAX(st->codec->has_b_frames, !!st->codec->max_b_frames);
2721
2940
int num, den, frame_size, i;
2723
// av_log(s, AV_LOG_DEBUG, "av_write_frame: pts:%"PRId64" dts:%"PRId64" cur_dts:%"PRId64" b:%d size:%d st:%d\n", pkt->pts, pkt->dts, st->cur_dts, delay, pkt->size, pkt->stream_index);
2942
av_dlog(s, "av_write_frame: pts:%"PRId64" dts:%"PRId64" cur_dts:%"PRId64" b:%d size:%d st:%d\n",
2943
pkt->pts, pkt->dts, st->cur_dts, delay, pkt->size, pkt->stream_index);
2725
2945
/* if(pkt->pts == AV_NOPTS_VALUE && pkt->dts == AV_NOPTS_VALUE)
2757
2977
if(st->cur_dts && st->cur_dts != AV_NOPTS_VALUE && st->cur_dts >= pkt->dts){
2758
2978
av_log(s, AV_LOG_ERROR,
2759
"st:%d error, non monotone timestamps %"PRId64" >= %"PRId64"\n",
2979
"Application provided invalid, non monotonically increasing dts to muxer in stream %d: %"PRId64" >= %"PRId64"\n",
2760
2980
st->index, st->cur_dts, pkt->dts);
2763
2983
if(pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE && pkt->pts < pkt->dts){
2764
av_log(s, AV_LOG_ERROR, "st:%d error, pts < dts\n", st->index);
2984
av_log(s, AV_LOG_ERROR, "pts < dts in stream %d\n", st->index);
2839
3057
*next_point= this_pktl;
2842
int ff_interleave_compare_dts(AVFormatContext *s, AVPacket *next, AVPacket *pkt)
3060
static int ff_interleave_compare_dts(AVFormatContext *s, AVPacket *next, AVPacket *pkt)
2844
3062
AVStream *st = s->streams[ pkt ->stream_index];
2845
3063
AVStream *st2= s->streams[ next->stream_index];
2846
3064
int64_t a= st2->time_base.num * (int64_t)st ->time_base.den;
2847
3065
int64_t b= st ->time_base.num * (int64_t)st2->time_base.den;
2848
return av_rescale_rnd(pkt->dts, b, a, AV_ROUND_DOWN) < next->dts;
3066
int64_t dts1 = av_rescale_rnd(pkt->dts, b, a, AV_ROUND_DOWN);
3067
if (dts1 == next->dts)
3068
return pkt->stream_index < next->stream_index;
3069
return dts1 < next->dts;
2851
3072
int av_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush){
2897
3118
int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt){
2898
3119
AVStream *st= s->streams[ pkt->stream_index];
2900
3122
//FIXME/XXX/HACK drop zero sized packets
2901
3123
if(st->codec->codec_type == AVMEDIA_TYPE_AUDIO && pkt->size==0)
2904
//av_log(NULL, AV_LOG_DEBUG, "av_interleaved_write_frame %d %"PRId64" %"PRId64"\n", pkt->size, pkt->dts, pkt->pts);
2905
if(compute_pkt_fields2(s, st, pkt) < 0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
3126
av_dlog(s, "av_interleaved_write_frame size:%d dts:%"PRId64" pts:%"PRId64"\n",
3127
pkt->size, pkt->dts, pkt->pts);
3128
if((ret = compute_pkt_fields2(s, st, pkt)) < 0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
2908
3131
if(pkt->dts == AV_NOPTS_VALUE && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
3132
return AVERROR(EINVAL);
3048
3265
if(st->codec->time_base.den && st->codec->time_base.num)
3049
3266
print_fps(1/av_q2d(st->codec->time_base), "tbc");
3268
if (st->disposition & AV_DISPOSITION_DEFAULT)
3269
av_log(NULL, AV_LOG_INFO, " (default)");
3270
if (st->disposition & AV_DISPOSITION_DUB)
3271
av_log(NULL, AV_LOG_INFO, " (dub)");
3272
if (st->disposition & AV_DISPOSITION_ORIGINAL)
3273
av_log(NULL, AV_LOG_INFO, " (original)");
3274
if (st->disposition & AV_DISPOSITION_COMMENT)
3275
av_log(NULL, AV_LOG_INFO, " (comment)");
3276
if (st->disposition & AV_DISPOSITION_LYRICS)
3277
av_log(NULL, AV_LOG_INFO, " (lyrics)");
3278
if (st->disposition & AV_DISPOSITION_KARAOKE)
3279
av_log(NULL, AV_LOG_INFO, " (karaoke)");
3280
if (st->disposition & AV_DISPOSITION_FORCED)
3281
av_log(NULL, AV_LOG_INFO, " (forced)");
3282
if (st->disposition & AV_DISPOSITION_HEARING_IMPAIRED)
3283
av_log(NULL, AV_LOG_INFO, " (hearing impaired)");
3284
if (st->disposition & AV_DISPOSITION_VISUAL_IMPAIRED)
3285
av_log(NULL, AV_LOG_INFO, " (visual impaired)");
3286
if (st->disposition & AV_DISPOSITION_CLEAN_EFFECTS)
3287
av_log(NULL, AV_LOG_INFO, " (clean effects)");
3051
3288
av_log(NULL, AV_LOG_INFO, "\n");
3052
3289
dump_metadata(NULL, st->metadata, " ");
3292
#if FF_API_DUMP_FORMAT
3055
3293
void dump_format(AVFormatContext *ic,
3057
3295
const char *url,
3298
av_dump_format(ic, index, url, is_output);
3302
void av_dump_format(AVFormatContext *ic,
3061
3308
uint8_t *printed = av_mallocz(ic->nb_streams);
3062
3309
if (ic->nb_streams && !printed)
3088
3335
av_log(NULL, AV_LOG_INFO, ", start: ");
3089
3336
secs = ic->start_time / AV_TIME_BASE;
3090
us = ic->start_time % AV_TIME_BASE;
3337
us = abs(ic->start_time % AV_TIME_BASE);
3091
3338
av_log(NULL, AV_LOG_INFO, "%d.%06d",
3092
3339
secs, (int)av_rescale(us, 1000000, AV_TIME_BASE));
3131
3378
av_free(printed);
3134
#if LIBAVFORMAT_VERSION_MAJOR < 53
3381
#if FF_API_PARSE_FRAME_PARAM
3382
#include "libavutil/parseutils.h"
3135
3384
int parse_image_size(int *width_ptr, int *height_ptr, const char *str)
3137
return av_parse_video_frame_size(width_ptr, height_ptr, str);
3386
return av_parse_video_size(width_ptr, height_ptr, str);
3140
3389
int parse_frame_rate(int *frame_rate_num, int *frame_rate_den, const char *arg)
3142
3391
AVRational frame_rate;
3143
int ret = av_parse_video_frame_rate(&frame_rate, arg);
3392
int ret = av_parse_video_rate(&frame_rate, arg);
3144
3393
*frame_rate_num= frame_rate.num;
3145
3394
*frame_rate_den= frame_rate.den;
3159
3408
return (av_gettime() / 1000) * 1000 + NTP_OFFSET_US;
3162
int64_t parse_date(const char *datestr, int duration)
3411
#if FF_API_PARSE_DATE
3412
#include "libavutil/parseutils.h"
3414
int64_t parse_date(const char *timestr, int duration)
3168
static const char * const date_fmt[] = {
3172
static const char * const time_fmt[] = {
3182
time_t now = time(0);
3184
len = strlen(datestr);
3186
lastch = datestr[len - 1];
3189
is_utc = (lastch == 'z' || lastch == 'Z');
3191
memset(&dt, 0, sizeof(dt));
3196
if (!strncasecmp(datestr, "now", len))
3197
return (int64_t) now * 1000000;
3199
/* parse the year-month-day part */
3200
for (i = 0; i < FF_ARRAY_ELEMS(date_fmt); i++) {
3201
q = small_strptime(p, date_fmt[i], &dt);
3207
/* if the year-month-day part is missing, then take the
3208
* current year-month-day time */
3213
dt = *localtime(&now);
3215
dt.tm_hour = dt.tm_min = dt.tm_sec = 0;
3220
if (*p == 'T' || *p == 't' || *p == ' ')
3223
/* parse the hour-minute-second part */
3224
for (i = 0; i < FF_ARRAY_ELEMS(time_fmt); i++) {
3225
q = small_strptime(p, time_fmt[i], &dt);
3231
/* parse datestr as a duration */
3236
/* parse datestr as HH:MM:SS */
3237
q = small_strptime(p, time_fmt[0], &dt);
3239
/* parse datestr as S+ */
3240
dt.tm_sec = strtol(p, (char **)&q, 10);
3242
/* the parsing didn't succeed */
3249
/* Now we have all the fields that we can get */
3255
t = dt.tm_hour * 3600 + dt.tm_min * 60 + dt.tm_sec;
3257
dt.tm_isdst = -1; /* unknown */
3267
/* parse the .m... part */
3271
for (val = 0, n = 100000; n >= 1; n /= 10, q++) {
3274
val += n * (*q - '0');
3278
return negative ? -t : t;
3417
av_parse_time(&timeval, timestr, duration);
3422
#if FF_API_FIND_INFO_TAG
3423
#include "libavutil/parseutils.h"
3281
3425
int find_info_tag(char *arg, int arg_size, const char *tag1, const char *info)
3291
while (*p != '\0' && *p != '=' && *p != '&') {
3292
if ((q - tag) < sizeof(tag) - 1)
3300
while (*p != '&' && *p != '\0') {
3301
if ((q - arg) < arg_size - 1) {
3311
if (!strcmp(tag, tag1))
3427
return av_find_info_tag(arg, arg_size, tag1, info);
3320
3431
int av_get_frame_filename(char *buf, int buf_size,
3321
3432
const char *path, int number)
3411
3522
hex_dump_internal(avcl, NULL, level, buf, size);
3414
//FIXME needs to know the time_base
3415
static void pkt_dump_internal(void *avcl, FILE *f, int level, AVPacket *pkt, int dump_payload)
3525
static void pkt_dump_internal(void *avcl, FILE *f, int level, AVPacket *pkt, int dump_payload, AVRational time_base)
3418
3528
#define PRINT(...) do { if (!f) av_log(avcl, level, __VA_ARGS__); else fprintf(f, __VA_ARGS__); } while(0)
3419
3529
PRINT("stream #%d:\n", pkt->stream_index);
3420
3530
PRINT(" keyframe=%d\n", ((pkt->flags & AV_PKT_FLAG_KEY) != 0));
3421
PRINT(" duration=%0.3f\n", (double)pkt->duration / AV_TIME_BASE);
3531
PRINT(" duration=%0.3f\n", pkt->duration * av_q2d(time_base));
3422
3532
/* DTS is _always_ valid after av_read_frame() */
3423
3533
PRINT(" dts=");
3424
3534
if (pkt->dts == AV_NOPTS_VALUE)
3427
PRINT("%0.3f", (double)pkt->dts / AV_TIME_BASE);
3537
PRINT("%0.3f", pkt->dts * av_q2d(time_base));
3428
3538
/* PTS may not be known if B-frames are present. */
3429
3539
PRINT(" pts=");
3430
3540
if (pkt->pts == AV_NOPTS_VALUE)
3433
PRINT("%0.3f", (double)pkt->pts / AV_TIME_BASE);
3543
PRINT("%0.3f", pkt->pts * av_q2d(time_base));
3435
3545
PRINT(" size=%d\n", pkt->size);
3438
3548
av_hex_dump(f, pkt->data, pkt->size);
3441
3552
void av_pkt_dump(FILE *f, AVPacket *pkt, int dump_payload)
3443
pkt_dump_internal(NULL, f, 0, pkt, dump_payload);
3554
AVRational tb = { 1, AV_TIME_BASE };
3555
pkt_dump_internal(NULL, f, 0, pkt, dump_payload, tb);
3559
void av_pkt_dump2(FILE *f, AVPacket *pkt, int dump_payload, AVStream *st)
3561
pkt_dump_internal(NULL, f, 0, pkt, dump_payload, st->time_base);
3446
3565
void av_pkt_dump_log(void *avcl, int level, AVPacket *pkt, int dump_payload)
3448
pkt_dump_internal(avcl, NULL, level, pkt, dump_payload);
3567
AVRational tb = { 1, AV_TIME_BASE };
3568
pkt_dump_internal(avcl, NULL, level, pkt, dump_payload, tb);
3572
void av_pkt_dump_log2(void *avcl, int level, AVPacket *pkt, int dump_payload,
3575
pkt_dump_internal(avcl, NULL, level, pkt, dump_payload, st->time_base);
3578
#if FF_API_URL_SPLIT
3579
attribute_deprecated
3451
3580
void ff_url_split(char *proto, int proto_size,
3452
3581
char *authorization, int authorization_size,
3453
3582
char *hostname, int hostname_size,
3455
3584
char *path, int path_size,
3456
3585
const char *url)
3587
av_url_split(proto, proto_size,
3588
authorization, authorization_size,
3589
hostname, hostname_size,
3596
void av_url_split(char *proto, int proto_size,
3597
char *authorization, int authorization_size,
3598
char *hostname, int hostname_size,
3600
char *path, int path_size,
3458
3603
const char *p, *ls, *at, *col, *brk;
3460
3605
if (port_ptr) *port_ptr = -1;
3678
int ff_hex_to_data(uint8_t *data, const char *p)
3685
p += strspn(p, SPACE_CHARS);
3688
c = toupper((unsigned char) *p++);
3689
if (c >= '0' && c <= '9')
3691
else if (c >= 'A' && c <= 'F')
3533
3706
void av_set_pts_info(AVStream *s, int pts_wrap_bits,
3534
3707
unsigned int pts_num, unsigned int pts_den)
3710
if(av_reduce(&new_tb.num, &new_tb.den, pts_num, pts_den, INT_MAX)){
3711
if(new_tb.num != pts_num)
3712
av_log(NULL, AV_LOG_DEBUG, "st:%d removing common factor %d from timebase\n", s->index, pts_num/new_tb.num);
3714
av_log(NULL, AV_LOG_WARNING, "st:%d has too large timebase, reducing\n", s->index);
3716
if(new_tb.num <= 0 || new_tb.den <= 0) {
3717
av_log(NULL, AV_LOG_ERROR, "Ignoring attempt to set invalid timebase for st:%d\n", s->index);
3720
s->time_base = new_tb;
3536
3721
s->pts_wrap_bits = pts_wrap_bits;
3538
if(av_reduce(&s->time_base.num, &s->time_base.den, pts_num, pts_den, INT_MAX)){
3539
if(s->time_base.num != pts_num)
3540
av_log(NULL, AV_LOG_DEBUG, "st:%d removing common factor %d from timebase\n", s->index, pts_num/s->time_base.num);
3542
av_log(NULL, AV_LOG_WARNING, "st:%d has too large timebase, reducing\n", s->index);
3544
if(!s->time_base.num || !s->time_base.den)
3545
s->time_base.num= s->time_base.den= 0;
3548
3724
int ff_url_join(char *str, int size, const char *proto,
3590
3766
return strlen(str);
3769
int ff_write_chained(AVFormatContext *dst, int dst_stream, AVPacket *pkt,
3770
AVFormatContext *src)
3775
local_pkt.stream_index = dst_stream;
3776
if (pkt->pts != AV_NOPTS_VALUE)
3777
local_pkt.pts = av_rescale_q(pkt->pts,
3778
src->streams[pkt->stream_index]->time_base,
3779
dst->streams[dst_stream]->time_base);
3780
if (pkt->dts != AV_NOPTS_VALUE)
3781
local_pkt.dts = av_rescale_q(pkt->dts,
3782
src->streams[pkt->stream_index]->time_base,
3783
dst->streams[dst_stream]->time_base);
3784
return av_write_frame(dst, &local_pkt);
3787
void ff_parse_key_value(const char *str, ff_parse_key_val_cb callback_get_buf,
3790
const char *ptr = str;
3792
/* Parse key=value pairs. */
3795
char *dest = NULL, *dest_end;
3796
int key_len, dest_len = 0;
3798
/* Skip whitespace and potential commas. */
3799
while (*ptr && (isspace(*ptr) || *ptr == ','))
3806
if (!(ptr = strchr(key, '=')))
3809
key_len = ptr - key;
3811
callback_get_buf(context, key, key_len, &dest, &dest_len);
3812
dest_end = dest + dest_len - 1;
3816
while (*ptr && *ptr != '\"') {
3820
if (dest && dest < dest_end)
3824
if (dest && dest < dest_end)
3832
for (; *ptr && !(isspace(*ptr) || *ptr == ','); ptr++)
3833
if (dest && dest < dest_end)
3841
int ff_find_stream_index(AVFormatContext *s, int id)
3844
for (i = 0; i < s->nb_streams; i++) {
3845
if (s->streams[i]->id == id)
3851
void ff_make_absolute_url(char *buf, int size, const char *base,
3855
/* Absolute path, relative to the current server */
3856
if (base && strstr(base, "://") && rel[0] == '/') {
3858
av_strlcpy(buf, base, size);
3859
sep = strstr(buf, "://");
3862
sep = strchr(sep, '/');
3866
av_strlcat(buf, rel, size);
3869
/* If rel actually is an absolute url, just copy it */
3870
if (!base || strstr(rel, "://") || rel[0] == '/') {
3871
av_strlcpy(buf, rel, size);
3875
av_strlcpy(buf, base, size);
3876
/* Remove the file name from the base url */
3877
sep = strrchr(buf, '/');
3882
while (av_strstart(rel, "../", NULL) && sep) {
3883
/* Remove the path delimiter at the end */
3885
sep = strrchr(buf, '/');
3886
/* If the next directory name to pop off is "..", break here */
3887
if (!strcmp(sep ? &sep[1] : buf, "..")) {
3888
/* Readd the slash we just removed */
3889
av_strlcat(buf, "/", size);
3892
/* Cut off the directory name */
3899
av_strlcat(buf, rel, size);