* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
-/* #define DEBUG */
-
#include "avformat.h"
#include "avio_internal.h"
#include "internal.h"
#include "libavcodec/bytestream.h"
#include "libavutil/opt.h"
#include "libavutil/dict.h"
+#include "libavutil/internal.h"
#include "libavutil/pixdesc.h"
#include "metadata.h"
#include "id3v2.h"
return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
}
-/* fraction handling */
-
-/**
- * f = val + (num / den) + 0.5.
- *
- * 'num' is normalized so that it is such as 0 <= num < den.
- *
- * @param f fractional number
- * @param val integer value
- * @param num must be >= 0
- * @param den must be >= 1
- */
-static void frac_init(AVFrac *f, int64_t val, int64_t num, int64_t den)
-{
- num += (den >> 1);
- if (num >= den) {
- val += num / den;
- num = num % den;
- }
- f->val = val;
- f->num = num;
- f->den = den;
-}
+/* an arbitrarily chosen "sane" max packet size -- 50M */
+#define SANE_CHUNK_SIZE (50000000)
-/**
- * Fractional addition to f: f = f + (incr / f->den).
- *
- * @param f fractional number
- * @param incr increment, can be positive or negative
+/*
+ * Read the data in sane-sized chunks and append to pkt.
+ * Return the number of bytes read or an error.
*/
-static void frac_add(AVFrac *f, int64_t incr)
+static int append_packet_chunked(AVIOContext *s, AVPacket *pkt, int size)
{
- int64_t num, den;
-
- num = f->num + incr;
- den = f->den;
- if (num < 0) {
- f->val += num / den;
- num = num % den;
- if (num < 0) {
- num += den;
- f->val--;
- }
- } else if (num >= den) {
- f->val += num / den;
- num = num % den;
- }
- f->num = num;
-}
-
-/** head of registered input format linked list */
-static AVInputFormat *first_iformat = NULL;
-/** head of registered output format linked list */
-static AVOutputFormat *first_oformat = NULL;
-
-AVInputFormat *av_iformat_next(AVInputFormat *f)
-{
- if(f) return f->next;
- else return first_iformat;
-}
-
-AVOutputFormat *av_oformat_next(AVOutputFormat *f)
-{
- if(f) return f->next;
- else return first_oformat;
-}
-
-void av_register_input_format(AVInputFormat *format)
-{
- AVInputFormat **p;
- p = &first_iformat;
- while (*p != NULL) p = &(*p)->next;
- *p = format;
- format->next = NULL;
-}
-
-void av_register_output_format(AVOutputFormat *format)
-{
- AVOutputFormat **p;
- p = &first_oformat;
- while (*p != NULL) p = &(*p)->next;
- *p = format;
- format->next = NULL;
-}
-
-int av_match_ext(const char *filename, const char *extensions)
-{
- const char *ext, *p;
- char ext1[32], *q;
+ int64_t chunk_size = size;
+ int orig_pos = pkt->pos; // av_grow_packet might reset pos
+ int orig_size = pkt->size;
+ int ret = 0;
- if(!filename)
- return 0;
+ do {
+ int prev_size = pkt->size;
+ int read_size;
- ext = strrchr(filename, '.');
- if (ext) {
- ext++;
- p = extensions;
- for(;;) {
- q = ext1;
- while (*p != '\0' && *p != ',' && q-ext1<sizeof(ext1)-1)
- *q++ = *p++;
- *q = '\0';
- if (!av_strcasecmp(ext1, ext))
- return 1;
- if (*p == '\0')
- break;
- p++;
+ /*
+ * When the caller requests a lot of data, limit it to the amount left
+ * in file or SANE_CHUNK_SIZE when it is not known
+ */
+ if (size > SANE_CHUNK_SIZE) {
+ int64_t filesize = avio_size(s) - avio_tell(s);
+ chunk_size = FFMAX(filesize, SANE_CHUNK_SIZE);
}
- }
- return 0;
-}
-
-static int match_format(const char *name, const char *names)
-{
- const char *p;
- int len, namelen;
-
- if (!name || !names)
- return 0;
-
- namelen = strlen(name);
- while ((p = strchr(names, ','))) {
- len = FFMAX(p - names, namelen);
- if (!av_strncasecmp(name, names, len))
- return 1;
- names = p+1;
- }
- return !av_strcasecmp(name, names);
-}
+ read_size = FFMIN(size, chunk_size);
-AVOutputFormat *av_guess_format(const char *short_name, const char *filename,
- const char *mime_type)
-{
- AVOutputFormat *fmt = NULL, *fmt_found;
- int score_max, score;
+ ret = av_grow_packet(pkt, read_size);
+ if (ret < 0)
+ break;
- /* specific test for image sequences */
-#if CONFIG_IMAGE2_MUXER
- if (!short_name && filename &&
- av_filename_number_test(filename) &&
- ff_guess_image2_codec(filename) != CODEC_ID_NONE) {
- return av_guess_format("image2", NULL, NULL);
- }
-#endif
- /* Find the proper file type. */
- fmt_found = NULL;
- score_max = 0;
- while ((fmt = av_oformat_next(fmt))) {
- score = 0;
- if (fmt->name && short_name && !av_strcasecmp(fmt->name, short_name))
- score += 100;
- if (fmt->mime_type && mime_type && !strcmp(fmt->mime_type, mime_type))
- score += 10;
- if (filename && fmt->extensions &&
- av_match_ext(filename, fmt->extensions)) {
- score += 5;
- }
- if (score > score_max) {
- score_max = score;
- fmt_found = fmt;
+ ret = avio_read(s, pkt->data + prev_size, read_size);
+ if (ret != read_size) {
+ av_shrink_packet(pkt, prev_size + FFMAX(ret, 0));
+ break;
}
- }
- return fmt_found;
-}
-enum CodecID av_guess_codec(AVOutputFormat *fmt, const char *short_name,
- const char *filename, const char *mime_type, enum AVMediaType type){
- if(type == AVMEDIA_TYPE_VIDEO){
- enum CodecID codec_id= CODEC_ID_NONE;
-
-#if CONFIG_IMAGE2_MUXER
- if(!strcmp(fmt->name, "image2") || !strcmp(fmt->name, "image2pipe")){
- codec_id= ff_guess_image2_codec(filename);
- }
-#endif
- if(codec_id == CODEC_ID_NONE)
- codec_id= fmt->video_codec;
- return codec_id;
- }else if(type == AVMEDIA_TYPE_AUDIO)
- return fmt->audio_codec;
- else if (type == AVMEDIA_TYPE_SUBTITLE)
- return fmt->subtitle_codec;
- else
- return CODEC_ID_NONE;
-}
+ size -= read_size;
+ } while (size > 0);
-AVInputFormat *av_find_input_format(const char *short_name)
-{
- AVInputFormat *fmt = NULL;
- while ((fmt = av_iformat_next(fmt))) {
- if (match_format(short_name, fmt->name))
- return fmt;
- }
- return NULL;
+ pkt->pos = orig_pos;
+ if (!pkt->size)
+ av_free_packet(pkt);
+ return pkt->size > orig_size ? pkt->size - orig_size : ret;
}
-
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
{
- int ret= av_new_packet(pkt, size);
-
- if(ret<0)
- return ret;
-
- pkt->pos= avio_tell(s);
-
- ret= avio_read(s, pkt->data, size);
- if(ret<=0)
- av_free_packet(pkt);
- else
- av_shrink_packet(pkt, ret);
+ av_init_packet(pkt);
+ pkt->data = NULL;
+ pkt->size = 0;
+ pkt->pos = avio_tell(s);
- return ret;
+ return append_packet_chunked(s, pkt, size);
}
int av_append_packet(AVIOContext *s, AVPacket *pkt, int size)
{
- int ret;
- int old_size;
if (!pkt->size)
return av_get_packet(s, pkt, size);
- old_size = pkt->size;
- ret = av_grow_packet(pkt, size);
- if (ret < 0)
- return ret;
- ret = avio_read(s, pkt->data + old_size, size);
- av_shrink_packet(pkt, old_size + FFMAX(ret, 0));
- return ret;
+ return append_packet_chunked(s, pkt, size);
}
score = fmt1->read_probe(&lpd);
} else if (fmt1->extensions) {
if (av_match_ext(lpd.filename, fmt1->extensions)) {
- score = 50;
+ score = AVPROBE_SCORE_EXTENSION;
}
}
if (score > *score_max) {
}
/* a hack for files with huge id3v2 tags -- try to guess by file extension. */
- if (!fmt && is_opened && *score_max < AVPROBE_SCORE_MAX/4) {
+ if (!fmt && is_opened && *score_max < AVPROBE_SCORE_EXTENSION / 2) {
while ((fmt = av_iformat_next(fmt)))
if (fmt->extensions && av_match_ext(lpd.filename, fmt->extensions)) {
- *score_max = AVPROBE_SCORE_MAX/4;
+ *score_max = AVPROBE_SCORE_EXTENSION / 2;
break;
}
}
- if (!fmt && id3 && *score_max < AVPROBE_SCORE_MAX/4-1) {
+ if (!fmt && id3 && *score_max < AVPROBE_SCORE_EXTENSION / 2 - 1) {
while ((fmt = av_iformat_next(fmt)))
if (fmt->extensions && av_match_ext("mp3", fmt->extensions)) {
- *score_max = AVPROBE_SCORE_MAX/4-1;
+ *score_max = AVPROBE_SCORE_EXTENSION / 2 - 1;
break;
}
}
static int set_codec_from_probe_data(AVFormatContext *s, AVStream *st, AVProbeData *pd, int score)
{
static const struct {
- const char *name; enum CodecID id; enum AVMediaType type;
+ const char *name; enum AVCodecID id; enum AVMediaType type;
} fmt_id_type[] = {
- { "aac" , CODEC_ID_AAC , AVMEDIA_TYPE_AUDIO },
- { "ac3" , CODEC_ID_AC3 , AVMEDIA_TYPE_AUDIO },
- { "dts" , CODEC_ID_DTS , AVMEDIA_TYPE_AUDIO },
- { "eac3" , CODEC_ID_EAC3 , AVMEDIA_TYPE_AUDIO },
- { "h264" , CODEC_ID_H264 , AVMEDIA_TYPE_VIDEO },
- { "m4v" , CODEC_ID_MPEG4 , AVMEDIA_TYPE_VIDEO },
- { "mp3" , CODEC_ID_MP3 , AVMEDIA_TYPE_AUDIO },
- { "mpegvideo", CODEC_ID_MPEG2VIDEO, AVMEDIA_TYPE_VIDEO },
+ { "aac" , AV_CODEC_ID_AAC , AVMEDIA_TYPE_AUDIO },
+ { "ac3" , AV_CODEC_ID_AC3 , AVMEDIA_TYPE_AUDIO },
+ { "dts" , AV_CODEC_ID_DTS , AVMEDIA_TYPE_AUDIO },
+ { "eac3" , AV_CODEC_ID_EAC3 , AVMEDIA_TYPE_AUDIO },
+ { "h264" , AV_CODEC_ID_H264 , AVMEDIA_TYPE_VIDEO },
+ { "m4v" , AV_CODEC_ID_MPEG4 , AVMEDIA_TYPE_VIDEO },
+ { "mp3" , AV_CODEC_ID_MP3 , AVMEDIA_TYPE_AUDIO },
+ { "mpegvideo", AV_CODEC_ID_MPEG2VIDEO, AVMEDIA_TYPE_VIDEO },
{ 0 }
};
AVInputFormat *fmt = av_probe_input_format2(pd, 1, &score);
return &pktl->pkt;
}
-static void queue_attached_pictures(AVFormatContext *s)
+static int queue_attached_pictures(AVFormatContext *s)
{
int i;
for (i = 0; i < s->nb_streams; i++)
if (s->streams[i]->disposition & AV_DISPOSITION_ATTACHED_PIC &&
s->streams[i]->discard < AVDISCARD_ALL) {
AVPacket copy = s->streams[i]->attached_pic;
- copy.destruct = NULL;
+ copy.buf = av_buffer_ref(copy.buf);
+ if (!copy.buf)
+ return AVERROR(ENOMEM);
+
add_to_pktbuf(&s->raw_packet_buffer, ©, &s->raw_packet_buffer_end);
}
+ return 0;
}
int avformat_open_input(AVFormatContext **ps, const char *filename, AVInputFormat *fmt, AVDictionary **options)
goto fail;
ff_id3v2_free_extra_meta(&id3v2_extra_meta);
- queue_attached_pictures(s);
+ if ((ret = queue_attached_pictures(s)) < 0)
+ goto fail;
if (s->pb && !s->data_offset)
s->data_offset = avio_tell(s->pb);
/*******************************************************/
-static void probe_codec(AVFormatContext *s, AVStream *st, const AVPacket *pkt)
+static int probe_codec(AVFormatContext *s, AVStream *st, const AVPacket *pkt)
{
- if(st->codec->codec_id == CODEC_ID_PROBE){
+ if(st->codec->codec_id == AV_CODEC_ID_PROBE){
AVProbeData *pd = &st->probe_data;
av_log(s, AV_LOG_DEBUG, "probing stream %d\n", st->index);
--st->probe_packets;
if (pkt) {
- pd->buf = av_realloc(pd->buf, pd->buf_size+pkt->size+AVPROBE_PADDING_SIZE);
+ int err;
+ if ((err = av_reallocp(&pd->buf, pd->buf_size + pkt->size +
+ AVPROBE_PADDING_SIZE)) < 0)
+ return err;
memcpy(pd->buf+pd->buf_size, pkt->data, pkt->size);
pd->buf_size += pkt->size;
memset(pd->buf+pd->buf_size, 0, AVPROBE_PADDING_SIZE);
} else {
st->probe_packets = 0;
+ if (!pd->buf_size) {
+ av_log(s, AV_LOG_ERROR, "nothing to probe for stream %d\n",
+ st->index);
+ return 0;
+ }
}
if (!st->probe_packets ||
av_log2(pd->buf_size) != av_log2(pd->buf_size - pkt->size)) {
set_codec_from_probe_data(s, st, pd, st->probe_packets > 0 ? AVPROBE_SCORE_MAX/4 : 0);
- if(st->codec->codec_id != CODEC_ID_PROBE){
+ if(st->codec->codec_id != AV_CODEC_ID_PROBE){
pd->buf_size=0;
av_freep(&pd->buf);
av_log(s, AV_LOG_DEBUG, "probed stream %d\n", st->index);
}
}
}
+ return 0;
}
int ff_read_packet(AVFormatContext *s, AVPacket *pkt)
{
- int ret, i;
+ int ret, i, err;
AVStream *st;
for(;;){
if (pktl) {
*pkt = pktl->pkt;
st = s->streams[pkt->stream_index];
- if (st->codec->codec_id != CODEC_ID_PROBE || !st->probe_packets ||
+ if (st->codec->codec_id != AV_CODEC_ID_PROBE || !st->probe_packets ||
s->raw_packet_buffer_remaining_size < pkt->size) {
AVProbeData *pd;
if (st->probe_packets) {
- probe_codec(s, st, NULL);
+ if ((err = probe_codec(s, st, NULL)) < 0)
+ return err;
}
pd = &st->probe_data;
av_freep(&pd->buf);
}
}
+ pkt->data = NULL;
+ pkt->size = 0;
av_init_packet(pkt);
ret= s->iformat->read_packet(s, pkt);
if (ret < 0) {
for (i = 0; i < s->nb_streams; i++) {
st = s->streams[i];
if (st->probe_packets) {
- probe_codec(s, st, NULL);
+ if ((err = probe_codec(s, st, NULL)) < 0)
+ return err;
}
}
continue;
break;
}
- if(!pktl && (st->codec->codec_id != CODEC_ID_PROBE ||
+ if(!pktl && (st->codec->codec_id != AV_CODEC_ID_PROBE ||
!st->probe_packets))
return ret;
add_to_pktbuf(&s->raw_packet_buffer, pkt, &s->raw_packet_buffer_end);
s->raw_packet_buffer_remaining_size -= pkt->size;
- probe_codec(s, st, pkt);
+ if ((err = probe_codec(s, st, pkt)) < 0)
+ return err;
}
}
-#if FF_API_READ_PACKET
-int av_read_packet(AVFormatContext *s, AVPacket *pkt)
-{
- return ff_read_packet(s, pkt);
-}
-#endif
-
-
/**********************************************************/
/**
* Get the number of samples of an audio frame. Return -1 on error.
*/
-static int get_audio_frame_size(AVCodecContext *enc, int size, int mux)
+int ff_get_audio_frame_size(AVCodecContext *enc, int size, int mux)
{
int frame_size;
if ((frame_size = av_get_audio_frame_duration(enc, size)) > 0)
return frame_size;
- /* fallback to using frame_size if muxing */
+ /* Fall back on using frame_size if muxing. */
if (enc->frame_size > 1)
return enc->frame_size;
/**
* Return the frame duration in seconds. Return 0 if not available.
*/
-static void compute_frame_duration(int *pnum, int *pden, AVStream *st,
- AVCodecParserContext *pc, AVPacket *pkt)
+void ff_compute_frame_duration(int *pnum, int *pden, AVStream *st,
+ AVCodecParserContext *pc, AVPacket *pkt)
{
int frame_size;
*pnum = st->codec->time_base.num;
*pden = st->codec->time_base.den;
if (pc && pc->repeat_pict) {
- *pnum = (*pnum) * (1 + pc->repeat_pict);
+ if (*pnum > INT_MAX / (1 + pc->repeat_pict))
+ *pden /= 1 + pc->repeat_pict;
+ else
+ *pnum *= 1 + pc->repeat_pict;
}
//If this codec can be interlaced or progressive then we need a parser to compute duration of a packet
//Thus if we have no parser in such case leave duration undefined.
}
break;
case AVMEDIA_TYPE_AUDIO:
- frame_size = get_audio_frame_size(st->codec, pkt->size, 0);
+ frame_size = ff_get_audio_frame_size(st->codec, pkt->size, 0);
if (frame_size <= 0 || st->codec->sample_rate <= 0)
break;
*pnum = frame_size;
}
}
-static int is_intra_only(AVCodecContext *enc){
- if(enc->codec_type == AVMEDIA_TYPE_AUDIO){
- return 1;
- }else if(enc->codec_type == AVMEDIA_TYPE_VIDEO){
- switch(enc->codec_id){
- case CODEC_ID_MJPEG:
- case CODEC_ID_MJPEGB:
- case CODEC_ID_LJPEG:
- case CODEC_ID_PRORES:
- case CODEC_ID_RAWVIDEO:
- case CODEC_ID_DVVIDEO:
- case CODEC_ID_HUFFYUV:
- case CODEC_ID_FFVHUFF:
- case CODEC_ID_ASV1:
- case CODEC_ID_ASV2:
- case CODEC_ID_VCR1:
- case CODEC_ID_DNXHD:
- case CODEC_ID_JPEG2000:
- case CODEC_ID_MDEC:
- return 1;
- default: break;
- }
- }
- return 0;
+static int is_intra_only(enum AVCodecID id)
+{
+ const AVCodecDescriptor *d = avcodec_descriptor_get(id);
+ if (!d)
+ return 0;
+ if (d->type == AVMEDIA_TYPE_VIDEO && !(d->props & AV_CODEC_PROP_INTRA_ONLY))
+ return 0;
+ return 1;
}
static void update_initial_timestamps(AVFormatContext *s, int stream_index,
pc && pc->pict_type != AV_PICTURE_TYPE_B)
presentation_delayed = 1;
- if(pkt->pts != AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE && pkt->dts > pkt->pts && st->pts_wrap_bits<63
- /*&& pkt->dts-(1LL<<st->pts_wrap_bits) < pkt->pts*/){
+ if (pkt->pts != AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE &&
+ st->pts_wrap_bits < 63 &&
+ pkt->dts - (1LL << (st->pts_wrap_bits - 1)) > pkt->pts) {
pkt->dts -= 1LL<<st->pts_wrap_bits;
}
}
if (pkt->duration == 0 && st->codec->codec_type != AVMEDIA_TYPE_AUDIO) {
- compute_frame_duration(&num, &den, st, pc, pkt);
+ ff_compute_frame_duration(&num, &den, st, pc, pkt);
if (den && num) {
pkt->duration = av_rescale_rnd(1, num * (int64_t)st->time_base.den, den * (int64_t)st->time_base.num, AV_ROUND_DOWN);
if(pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE && pkt->pts > pkt->dts)
presentation_delayed = 1;
-// av_log(NULL, AV_LOG_DEBUG, "IN delayed:%d pts:%"PRId64", dts:%"PRId64" cur_dts:%"PRId64" st:%d pc:%p\n", presentation_delayed, pkt->pts, pkt->dts, st->cur_dts, pkt->stream_index, pc);
+ av_dlog(NULL,
+ "IN delayed:%d pts:%"PRId64", dts:%"PRId64" cur_dts:%"PRId64" st:%d pc:%p\n",
+ presentation_delayed, pkt->pts, pkt->dts, st->cur_dts,
+ pkt->stream_index, pc);
/* interpolate PTS and DTS if they are not present */
//We skip H264 currently because delay and has_b_frames are not reliably set
- if((delay==0 || (delay==1 && pc)) && st->codec->codec_id != CODEC_ID_H264){
+ if((delay==0 || (delay==1 && pc)) && st->codec->codec_id != AV_CODEC_ID_H264){
if (presentation_delayed) {
/* DTS = decompression timestamp */
/* PTS = presentation timestamp */
st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
int duration = pkt->duration;
if (!duration && st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
- compute_frame_duration(&num, &den, st, pc, pkt);
+ ff_compute_frame_duration(&num, &den, st, pc, pkt);
if (den && num) {
duration = av_rescale_rnd(1, num * (int64_t)st->time_base.den,
den * (int64_t)st->time_base.num,
FFSWAP(int64_t, st->pts_buffer[i], st->pts_buffer[i+1]);
if(pkt->dts == AV_NOPTS_VALUE)
pkt->dts= st->pts_buffer[0];
- if(st->codec->codec_id == CODEC_ID_H264){ // we skipped it above so we try here
+ if(st->codec->codec_id == AV_CODEC_ID_H264){ // we skipped it above so we try here
update_initial_timestamps(s, pkt->stream_index, pkt->dts, pkt->pts); // this should happen on the first packet
}
if(pkt->dts > st->cur_dts)
st->cur_dts = pkt->dts;
}
-// av_log(NULL, AV_LOG_ERROR, "OUTdelayed:%d/%d pts:%"PRId64", dts:%"PRId64" cur_dts:%"PRId64"\n", presentation_delayed, delay, pkt->pts, pkt->dts, st->cur_dts);
+ av_dlog(NULL,
+ "OUTdelayed:%d/%d pts:%"PRId64", dts:%"PRId64" cur_dts:%"PRId64"\n",
+ presentation_delayed, delay, pkt->pts, pkt->dts, st->cur_dts);
/* update flags */
- if(is_intra_only(st->codec))
+ if (is_intra_only(st->codec->codec_id))
pkt->flags |= AV_PKT_FLAG_KEY;
if (pc)
pkt->convergence_duration = pc->convergence_duration;
if (!out_pkt.size)
continue;
+ if (pkt->side_data) {
+ out_pkt.side_data = pkt->side_data;
+ out_pkt.side_data_elems = pkt->side_data_elems;
+ pkt->side_data = NULL;
+ pkt->side_data_elems = 0;
+ }
+
/* set the duration */
out_pkt.duration = 0;
if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
}
if (out_pkt.data == pkt->data && out_pkt.size == pkt->size) {
+ out_pkt.buf = pkt->buf;
+ pkt->buf = NULL;
+#if FF_API_DESTRUCT_PACKET
+FF_DISABLE_DEPRECATION_WARNINGS
out_pkt.destruct = pkt->destruct;
pkt->destruct = NULL;
+FF_ENABLE_DEPRECATION_WARNINGS
+#endif
}
if ((ret = av_dup_packet(&out_pkt)) < 0)
goto fail;
int ret = seek_frame_internal(s, stream_index, timestamp, flags);
if (ret >= 0)
- queue_attached_pictures(s);
+ ret = queue_attached_pictures(s);
return ret;
}
ret = s->iformat->read_seek2(s, stream_index, min_ts, ts, max_ts, flags);
if (ret >= 0)
- queue_attached_pictures(s);
+ ret = queue_attached_pictures(s);
return ret;
}
//try to seek via read_timestamp()
}
- //Fallback to old API if new is not implemented but old is
- //Note the old has somewat different sematics
+ // Fall back on old API if new is not implemented but old is.
+ // Note the old API has somewhat different semantics.
if(s->iformat->read_seek || 1)
- return av_seek_frame(s, stream_index, ts, flags | (ts - min_ts > (uint64_t)(max_ts - ts) ? AVSEEK_FLAG_BACKWARD : 0));
+ return av_seek_frame(s, stream_index, ts, flags | ((uint64_t)ts - min_ts > (uint64_t)max_ts - ts ? AVSEEK_FLAG_BACKWARD : 0));
// try some generic seek like seek_frame_generic() but with new ts semantics
}
static void estimate_timings_from_bit_rate(AVFormatContext *ic)
{
int64_t filesize, duration;
- int bit_rate, i;
+ int i;
AVStream *st;
/* if bit_rate is already set, we believe it */
if (ic->bit_rate <= 0) {
- bit_rate = 0;
+ int bit_rate = 0;
for(i=0;i<ic->nb_streams;i++) {
st = ic->streams[i];
- if (st->codec->bit_rate > 0)
- bit_rate += st->codec->bit_rate;
+ if (st->codec->bit_rate > 0) {
+ if (INT_MAX - st->codec->bit_rate < bit_rate) {
+ bit_rate = 0;
+ break;
+ }
+ bit_rate += st->codec->bit_rate;
+ }
}
ic->bit_rate = bit_rate;
}
break;
case AVMEDIA_TYPE_VIDEO:
val = avctx->width;
- if (st->info->found_decoder >= 0 && avctx->pix_fmt == PIX_FMT_NONE)
+ if (st->info->found_decoder >= 0 && avctx->pix_fmt == AV_PIX_FMT_NONE)
return 0;
break;
default:
val = 1;
break;
}
- return avctx->codec_id != CODEC_ID_NONE && val != 0;
+ return avctx->codec_id != AV_CODEC_ID_NONE && val != 0;
}
static int has_decode_delay_been_guessed(AVStream *st)
{
- return st->codec->codec_id != CODEC_ID_H264 ||
+ return st->codec->codec_id != AV_CODEC_ID_H264 ||
st->info->nb_decoded_frames >= 6;
}
/* returns 1 or 0 if or if not decoded data was returned, or a negative error */
static int try_decode_frame(AVStream *st, AVPacket *avpkt, AVDictionary **options)
{
- AVCodec *codec;
+ const AVCodec *codec;
int got_picture = 1, ret = 0;
- AVFrame picture;
+ AVFrame *frame = avcodec_alloc_frame();
AVPacket pkt = *avpkt;
+ if (!frame)
+ return AVERROR(ENOMEM);
+
if (!avcodec_is_open(st->codec) && !st->info->found_decoder) {
AVDictionary *thread_opt = NULL;
if (!codec) {
st->info->found_decoder = -1;
- return -1;
+ ret = -1;
+ goto fail;
}
/* force thread count to 1 since the h264 decoder will not extract SPS
av_dict_free(&thread_opt);
if (ret < 0) {
st->info->found_decoder = -1;
- return ret;
+ goto fail;
}
st->info->found_decoder = 1;
} else if (!st->info->found_decoder)
st->info->found_decoder = 1;
- if (st->info->found_decoder < 0)
- return -1;
+ if (st->info->found_decoder < 0) {
+ ret = -1;
+ goto fail;
+ }
while ((pkt.size > 0 || (!pkt.data && got_picture)) &&
ret >= 0 &&
!has_decode_delay_been_guessed(st) ||
(!st->codec_info_nb_frames && st->codec->codec->capabilities & CODEC_CAP_CHANNEL_CONF))) {
got_picture = 0;
- avcodec_get_frame_defaults(&picture);
+ avcodec_get_frame_defaults(frame);
switch(st->codec->codec_type) {
case AVMEDIA_TYPE_VIDEO:
- ret = avcodec_decode_video2(st->codec, &picture,
+ ret = avcodec_decode_video2(st->codec, frame,
&got_picture, &pkt);
break;
case AVMEDIA_TYPE_AUDIO:
- ret = avcodec_decode_audio4(st->codec, &picture, &got_picture, &pkt);
+ ret = avcodec_decode_audio4(st->codec, frame, &got_picture, &pkt);
break;
default:
break;
ret = got_picture;
}
}
+
+fail:
+ avcodec_free_frame(&frame);
return ret;
}
-unsigned int ff_codec_get_tag(const AVCodecTag *tags, enum CodecID id)
+unsigned int ff_codec_get_tag(const AVCodecTag *tags, enum AVCodecID id)
{
- while (tags->id != CODEC_ID_NONE) {
+ while (tags->id != AV_CODEC_ID_NONE) {
if (tags->id == id)
return tags->tag;
tags++;
return 0;
}
-enum CodecID ff_codec_get_id(const AVCodecTag *tags, unsigned int tag)
+enum AVCodecID ff_codec_get_id(const AVCodecTag *tags, unsigned int tag)
{
int i;
- for(i=0; tags[i].id != CODEC_ID_NONE;i++) {
+ for(i=0; tags[i].id != AV_CODEC_ID_NONE;i++) {
if(tag == tags[i].tag)
return tags[i].id;
}
- for(i=0; tags[i].id != CODEC_ID_NONE; i++) {
+ for(i=0; tags[i].id != AV_CODEC_ID_NONE; i++) {
if (avpriv_toupper4(tag) == avpriv_toupper4(tags[i].tag))
return tags[i].id;
}
- return CODEC_ID_NONE;
+ return AV_CODEC_ID_NONE;
}
-unsigned int av_codec_get_tag(const AVCodecTag * const *tags, enum CodecID id)
+enum AVCodecID ff_get_pcm_codec_id(int bps, int flt, int be, int sflags)
+{
+ if (flt) {
+ switch (bps) {
+ case 32: return be ? AV_CODEC_ID_PCM_F32BE : AV_CODEC_ID_PCM_F32LE;
+ case 64: return be ? AV_CODEC_ID_PCM_F64BE : AV_CODEC_ID_PCM_F64LE;
+ default: return AV_CODEC_ID_NONE;
+ }
+ } else {
+ bps >>= 3;
+ if (sflags & (1 << (bps - 1))) {
+ switch (bps) {
+ case 1: return AV_CODEC_ID_PCM_S8;
+ case 2: return be ? AV_CODEC_ID_PCM_S16BE : AV_CODEC_ID_PCM_S16LE;
+ case 3: return be ? AV_CODEC_ID_PCM_S24BE : AV_CODEC_ID_PCM_S24LE;
+ case 4: return be ? AV_CODEC_ID_PCM_S32BE : AV_CODEC_ID_PCM_S32LE;
+ default: return AV_CODEC_ID_NONE;
+ }
+ } else {
+ switch (bps) {
+ case 1: return AV_CODEC_ID_PCM_U8;
+ case 2: return be ? AV_CODEC_ID_PCM_U16BE : AV_CODEC_ID_PCM_U16LE;
+ case 3: return be ? AV_CODEC_ID_PCM_U24BE : AV_CODEC_ID_PCM_U24LE;
+ case 4: return be ? AV_CODEC_ID_PCM_U32BE : AV_CODEC_ID_PCM_U32LE;
+ default: return AV_CODEC_ID_NONE;
+ }
+ }
+ }
+}
+
+unsigned int av_codec_get_tag(const AVCodecTag * const *tags, enum AVCodecID id)
{
int i;
for(i=0; tags && tags[i]; i++){
return 0;
}
-enum CodecID av_codec_get_id(const AVCodecTag * const *tags, unsigned int tag)
+enum AVCodecID av_codec_get_id(const AVCodecTag * const *tags, unsigned int tag)
{
int i;
for(i=0; tags && tags[i]; i++){
- enum CodecID id= ff_codec_get_id(tags[i], tag);
- if(id!=CODEC_ID_NONE) return id;
+ enum AVCodecID id= ff_codec_get_id(tags[i], tag);
+ if(id!=AV_CODEC_ID_NONE) return id;
}
- return CODEC_ID_NONE;
+ return AV_CODEC_ID_NONE;
}
static void compute_chapters_end(AVFormatContext *s)
|| c->time_base.den < 5L*c->time_base.num
/* || c->codec_tag == AV_RL32("DIVX")
|| c->codec_tag == AV_RL32("XVID")*/
- || c->codec_id == CODEC_ID_MPEG2VIDEO
- || c->codec_id == CODEC_ID_H264
+ || c->codec_id == AV_CODEC_ID_MPEG2VIDEO
+ || c->codec_id == AV_CODEC_ID_H264
)
return 1;
return 0;
int orig_nb_streams = ic->nb_streams; // new streams might appear, no options for those
for(i=0;i<ic->nb_streams;i++) {
- AVCodec *codec;
+ const AVCodec *codec;
AVDictionary *thread_opt = NULL;
st = ic->streams[i];
}
for (i=0; i<ic->nb_streams; i++) {
-#if FF_API_R_FRAME_RATE
- ic->streams[i]->info->last_dts = AV_NOPTS_VALUE;
-#endif
ic->streams[i]->info->fps_first_dts = AV_NOPTS_VALUE;
ic->streams[i]->info->fps_last_dts = AV_NOPTS_VALUE;
}
st->info->fps_last_dts, st->codec_info_nb_frames, pkt->dts);
st->info->fps_first_dts = st->info->fps_last_dts = AV_NOPTS_VALUE;
}
+ /* check for a discontinuity in dts - if the difference in dts
+ * is more than 1000 times the average packet duration in the sequence,
+ * we treat it as a discontinuity */
+ if (st->info->fps_last_dts != AV_NOPTS_VALUE &&
+ st->info->fps_last_dts_idx > st->info->fps_first_dts_idx &&
+ (pkt->dts - st->info->fps_last_dts) / 1000 >
+ (st->info->fps_last_dts - st->info->fps_first_dts) / (st->info->fps_last_dts_idx - st->info->fps_first_dts_idx)) {
+ av_log(ic, AV_LOG_WARNING, "DTS discontinuity in stream %d: "
+ "packet %d with DTS %"PRId64", packet %d with DTS "
+ "%"PRId64"\n", st->index, st->info->fps_last_dts_idx,
+ st->info->fps_last_dts, st->codec_info_nb_frames, pkt->dts);
+ st->info->fps_first_dts = st->info->fps_last_dts = AV_NOPTS_VALUE;
+ }
/* update stored dts values */
if (st->info->fps_first_dts == AV_NOPTS_VALUE) {
break;
}
}
-#if FF_API_R_FRAME_RATE
- {
- int64_t last = st->info->last_dts;
-
- if(pkt->dts != AV_NOPTS_VALUE && last != AV_NOPTS_VALUE && pkt->dts > last){
- int64_t duration= pkt->dts - last;
- double dur= duration * av_q2d(st->time_base);
-
- if (st->info->duration_count < 2)
- memset(st->info->duration_error, 0, sizeof(st->info->duration_error));
- for (i=1; i<FF_ARRAY_ELEMS(st->info->duration_error); i++) {
- int framerate= get_std_framerate(i);
- int ticks= lrintf(dur*framerate/(1001*12));
- double error = dur - (double)ticks*1001*12 / framerate;
- st->info->duration_error[i] += error*error;
- }
- st->info->duration_count++;
- // ignore the first 4 values, they might have some random jitter
- if (st->info->duration_count > 3)
- st->info->duration_gcd = av_gcd(st->info->duration_gcd, duration);
- }
- if (last == AV_NOPTS_VALUE || st->info->duration_count <= 1)
- st->info->last_dts = pkt->dts;
- }
-#endif
if(st->parser && st->parser->parser->split && !st->codec->extradata){
int i= st->parser->parser->split(st->codec, pkt->data, pkt->size);
if (i > 0 && i < FF_MAX_EXTRADATA_SIZE) {
int best_fps = 0;
double best_error = 0.01;
+ if (delta_dts >= INT64_MAX / st->time_base.num ||
+ delta_packets >= INT64_MAX / st->time_base.den)
+ continue;
av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,
delta_packets*(int64_t)st->time_base.den,
delta_dts*(int64_t)st->time_base.num, 60000);
best_fps, 12*1001, INT_MAX);
}
}
-#if FF_API_R_FRAME_RATE
- // the check for tb_unreliable() is not completely correct, since this is not about handling
- // a unreliable/inexact time base, but a time base that is finer than necessary, as e.g.
- // ipmovie.c produces.
- if (tb_unreliable(st->codec) && st->info->duration_count > 15 && st->info->duration_gcd > 1 && !st->r_frame_rate.num)
- 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);
- if (st->info->duration_count && !st->r_frame_rate.num
- && tb_unreliable(st->codec)) {
- int num = 0;
- double best_error= 2*av_q2d(st->time_base);
- best_error = best_error*best_error*st->info->duration_count*1000*12*30;
-
- for (j=1; j<FF_ARRAY_ELEMS(st->info->duration_error); j++) {
- double error = st->info->duration_error[j] * get_std_framerate(j);
- if(error < best_error){
- best_error= error;
- num = get_std_framerate(j);
- }
- }
- // do not increase frame rate by more than 1 % in order to match a standard rate.
- if (num && (!st->r_frame_rate.num || (double)num/(12*1001) < 1.01 * av_q2d(st->r_frame_rate)))
- av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den, num, 12*1001, INT_MAX);
- }
-#endif
}else if(st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
if(!st->codec->bits_per_coded_sample)
st->codec->bits_per_coded_sample= av_get_bits_per_sample(st->codec->codec_id);
if (st->attached_pic.data)
av_free_packet(&st->attached_pic);
av_dict_free(&st->metadata);
+ av_freep(&st->probe_data.buf);
av_free(st->index_entries);
av_free(st->codec->extradata);
av_free(st->codec->subtitle_header);
av_free(s);
}
-#if FF_API_CLOSE_INPUT_FILE
-void av_close_input_file(AVFormatContext *s)
-{
- avformat_close_input(&s);
-}
-#endif
-
void avformat_close_input(AVFormatContext **ps)
{
AVFormatContext *s = *ps;
- AVIOContext *pb = (s->iformat->flags & AVFMT_NOFILE) || (s->flags & AVFMT_FLAG_CUSTOM_IO) ?
- NULL : s->pb;
+ AVIOContext *pb = s->pb;
+
+ if ((s->iformat && s->iformat->flags & AVFMT_NOFILE) ||
+ (s->flags & AVFMT_FLAG_CUSTOM_IO))
+ pb = NULL;
+
flush_packet_queue(s);
- if (s->iformat->read_close)
- s->iformat->read_close(s);
+
+ if (s->iformat) {
+ if (s->iformat->read_close)
+ s->iformat->read_close(s);
+ }
+
avformat_free_context(s);
+
*ps = NULL;
- if (pb)
- avio_close(pb);
+
+ avio_close(pb);
}
AVStream *avformat_new_stream(AVFormatContext *s, AVCodec *c)
{
AVStream *st;
int i;
- AVStream **streams;
- if (s->nb_streams >= INT_MAX/sizeof(*streams))
- return NULL;
- streams = av_realloc(s->streams, (s->nb_streams + 1) * sizeof(*streams));
- if (!streams)
+ if (av_reallocp_array(&s->streams, s->nb_streams + 1, sizeof(*s->streams)) < 0) {
+ s->nb_streams = 0;
return NULL;
- s->streams = streams;
+ }
st = av_mallocz(sizeof(AVStream));
if (!st)
st->sample_aspect_ratio = (AVRational){0,1};
+ st->info->fps_first_dts = AV_NOPTS_VALUE;
+ st->info->fps_last_dts = AV_NOPTS_VALUE;
+
s->streams[s->nb_streams++] = st;
return st;
}
return chapter;
}
-/************************************************************/
-/* output media file */
-
-static int validate_codec_tag(AVFormatContext *s, AVStream *st)
-{
- const AVCodecTag *avctag;
- int n;
- enum CodecID id = CODEC_ID_NONE;
- unsigned int tag = 0;
-
- /**
- * Check that tag + id is in the table
- * If neither is in the table -> OK
- * If tag is in the table with another id -> FAIL
- * If id is in the table with another tag -> FAIL unless strict < normal
- */
- for (n = 0; s->oformat->codec_tag[n]; n++) {
- avctag = s->oformat->codec_tag[n];
- while (avctag->id != CODEC_ID_NONE) {
- if (avpriv_toupper4(avctag->tag) == avpriv_toupper4(st->codec->codec_tag)) {
- id = avctag->id;
- if (id == st->codec->codec_id)
- return 1;
- }
- if (avctag->id == st->codec->codec_id)
- tag = avctag->tag;
- avctag++;
- }
- }
- if (id != CODEC_ID_NONE)
- return 0;
- if (tag && (st->codec->strict_std_compliance >= FF_COMPLIANCE_NORMAL))
- return 0;
- return 1;
-}
-
-int avformat_write_header(AVFormatContext *s, AVDictionary **options)
-{
- int ret = 0, i;
- AVStream *st;
- AVDictionary *tmp = NULL;
-
- if (options)
- av_dict_copy(&tmp, *options, 0);
- if ((ret = av_opt_set_dict(s, &tmp)) < 0)
- goto fail;
-
- // some sanity checks
- if (s->nb_streams == 0 && !(s->oformat->flags & AVFMT_NOSTREAMS)) {
- av_log(s, AV_LOG_ERROR, "no streams\n");
- ret = AVERROR(EINVAL);
- goto fail;
- }
-
- for(i=0;i<s->nb_streams;i++) {
- st = s->streams[i];
-
- switch (st->codec->codec_type) {
- case AVMEDIA_TYPE_AUDIO:
- if(st->codec->sample_rate<=0){
- av_log(s, AV_LOG_ERROR, "sample rate not set\n");
- ret = AVERROR(EINVAL);
- goto fail;
- }
- if(!st->codec->block_align)
- st->codec->block_align = st->codec->channels *
- av_get_bits_per_sample(st->codec->codec_id) >> 3;
- break;
- case AVMEDIA_TYPE_VIDEO:
- if(st->codec->time_base.num<=0 || st->codec->time_base.den<=0){ //FIXME audio too?
- av_log(s, AV_LOG_ERROR, "time base not set\n");
- ret = AVERROR(EINVAL);
- goto fail;
- }
- if((st->codec->width<=0 || st->codec->height<=0) && !(s->oformat->flags & AVFMT_NODIMENSIONS)){
- av_log(s, AV_LOG_ERROR, "dimensions not set\n");
- ret = AVERROR(EINVAL);
- goto fail;
- }
- if(av_cmp_q(st->sample_aspect_ratio, st->codec->sample_aspect_ratio)){
- av_log(s, AV_LOG_ERROR, "Aspect ratio mismatch between muxer "
- "(%d/%d) and encoder layer (%d/%d)\n",
- st->sample_aspect_ratio.num, st->sample_aspect_ratio.den,
- st->codec->sample_aspect_ratio.num,
- st->codec->sample_aspect_ratio.den);
- ret = AVERROR(EINVAL);
- goto fail;
- }
- break;
- }
-
- if(s->oformat->codec_tag){
- 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)){
- //the current rawvideo encoding system ends up setting the wrong codec_tag for avi, we override it here
- st->codec->codec_tag= 0;
- }
- if(st->codec->codec_tag){
- if (!validate_codec_tag(s, st)) {
- char tagbuf[32];
- av_get_codec_tag_string(tagbuf, sizeof(tagbuf), st->codec->codec_tag);
- av_log(s, AV_LOG_ERROR,
- "Tag %s/0x%08x incompatible with output codec id '%d'\n",
- tagbuf, st->codec->codec_tag, st->codec->codec_id);
- ret = AVERROR_INVALIDDATA;
- goto fail;
- }
- }else
- st->codec->codec_tag= av_codec_get_tag(s->oformat->codec_tag, st->codec->codec_id);
- }
-
- if(s->oformat->flags & AVFMT_GLOBALHEADER &&
- !(st->codec->flags & CODEC_FLAG_GLOBAL_HEADER))
- av_log(s, AV_LOG_WARNING, "Codec for stream %d does not use global headers but container format requires global headers\n", i);
- }
-
- if (!s->priv_data && s->oformat->priv_data_size > 0) {
- s->priv_data = av_mallocz(s->oformat->priv_data_size);
- if (!s->priv_data) {
- ret = AVERROR(ENOMEM);
- goto fail;
- }
- if (s->oformat->priv_class) {
- *(const AVClass**)s->priv_data= s->oformat->priv_class;
- av_opt_set_defaults(s->priv_data);
- if ((ret = av_opt_set_dict(s->priv_data, &tmp)) < 0)
- goto fail;
- }
- }
-
- /* set muxer identification string */
- if (s->nb_streams && !(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT)) {
- av_dict_set(&s->metadata, "encoder", LIBAVFORMAT_IDENT, 0);
- }
-
- if(s->oformat->write_header){
- ret = s->oformat->write_header(s);
- if (ret < 0)
- goto fail;
- }
-
- /* init PTS generation */
- for(i=0;i<s->nb_streams;i++) {
- int64_t den = AV_NOPTS_VALUE;
- st = s->streams[i];
-
- switch (st->codec->codec_type) {
- case AVMEDIA_TYPE_AUDIO:
- den = (int64_t)st->time_base.num * st->codec->sample_rate;
- break;
- case AVMEDIA_TYPE_VIDEO:
- den = (int64_t)st->time_base.num * st->codec->time_base.den;
- break;
- default:
- break;
- }
- if (den != AV_NOPTS_VALUE) {
- if (den <= 0) {
- ret = AVERROR_INVALIDDATA;
- goto fail;
- }
- frac_init(&st->pts, 0, 0, den);
- }
- }
-
- if (options) {
- av_dict_free(options);
- *options = tmp;
- }
- return 0;
-fail:
- av_dict_free(&tmp);
- return ret;
-}
-
-//FIXME merge with compute_pkt_fields
-static int compute_pkt_fields2(AVFormatContext *s, AVStream *st, AVPacket *pkt){
- int delay = FFMAX(st->codec->has_b_frames, !!st->codec->max_b_frames);
- int num, den, frame_size, i;
-
- av_dlog(s, "compute_pkt_fields2: 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);
-
-/* if(pkt->pts == AV_NOPTS_VALUE && pkt->dts == AV_NOPTS_VALUE)
- return AVERROR(EINVAL);*/
-
- /* duration field */
- if (pkt->duration == 0) {
- compute_frame_duration(&num, &den, st, NULL, pkt);
- if (den && num) {
- pkt->duration = av_rescale(1, num * (int64_t)st->time_base.den * st->codec->ticks_per_frame, den * (int64_t)st->time_base.num);
- }
- }
-
- if(pkt->pts == AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE && delay==0)
- pkt->pts= pkt->dts;
-
- //XXX/FIXME this is a temporary hack until all encoders output pts
- if((pkt->pts == 0 || pkt->pts == AV_NOPTS_VALUE) && pkt->dts == AV_NOPTS_VALUE && !delay){
- pkt->dts=
-// pkt->pts= st->cur_dts;
- pkt->pts= st->pts.val;
- }
-
- //calculate dts from pts
- if(pkt->pts != AV_NOPTS_VALUE && pkt->dts == AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY){
- st->pts_buffer[0]= pkt->pts;
- for(i=1; i<delay+1 && st->pts_buffer[i] == AV_NOPTS_VALUE; i++)
- st->pts_buffer[i]= pkt->pts + (i-delay-1) * pkt->duration;
- for(i=0; i<delay && st->pts_buffer[i] > st->pts_buffer[i+1]; i++)
- FFSWAP(int64_t, st->pts_buffer[i], st->pts_buffer[i+1]);
-
- pkt->dts= st->pts_buffer[0];
- }
-
- if (st->cur_dts && st->cur_dts != AV_NOPTS_VALUE &&
- ((!(s->oformat->flags & AVFMT_TS_NONSTRICT) &&
- st->cur_dts >= pkt->dts) || st->cur_dts > pkt->dts)) {
- av_log(s, AV_LOG_ERROR,
- "Application provided invalid, non monotonically increasing dts to muxer in stream %d: %"PRId64" >= %"PRId64"\n",
- st->index, st->cur_dts, pkt->dts);
- return AVERROR(EINVAL);
- }
- if(pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE && pkt->pts < pkt->dts){
- av_log(s, AV_LOG_ERROR, "pts < dts in stream %d\n", st->index);
- return AVERROR(EINVAL);
- }
-
-// av_log(s, AV_LOG_DEBUG, "av_write_frame: pts2:%"PRId64" dts2:%"PRId64"\n", pkt->pts, pkt->dts);
- st->cur_dts= pkt->dts;
- st->pts.val= pkt->dts;
-
- /* update pts */
- switch (st->codec->codec_type) {
- case AVMEDIA_TYPE_AUDIO:
- frame_size = get_audio_frame_size(st->codec, pkt->size, 1);
-
- /* HACK/FIXME, we skip the initial 0 size packets as they are most
- likely equal to the encoder delay, but it would be better if we
- had the real timestamps from the encoder */
- if (frame_size >= 0 && (pkt->size || st->pts.num!=st->pts.den>>1 || st->pts.val)) {
- frac_add(&st->pts, (int64_t)st->time_base.den * frame_size);
- }
- break;
- case AVMEDIA_TYPE_VIDEO:
- frac_add(&st->pts, (int64_t)st->time_base.den * st->codec->time_base.num);
- break;
- default:
- break;
- }
- return 0;
-}
-
-int av_write_frame(AVFormatContext *s, AVPacket *pkt)
-{
- int ret;
-
- if (!pkt) {
- if (s->oformat->flags & AVFMT_ALLOW_FLUSH)
- return s->oformat->write_packet(s, pkt);
- return 1;
- }
-
- ret = compute_pkt_fields2(s, s->streams[pkt->stream_index], pkt);
-
- if(ret<0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
- return ret;
-
- ret= s->oformat->write_packet(s, pkt);
-
- if (ret >= 0)
- s->streams[pkt->stream_index]->nb_frames++;
- return ret;
-}
-
-void ff_interleave_add_packet(AVFormatContext *s, AVPacket *pkt,
- int (*compare)(AVFormatContext *, AVPacket *, AVPacket *))
-{
- AVPacketList **next_point, *this_pktl;
-
- this_pktl = av_mallocz(sizeof(AVPacketList));
- this_pktl->pkt= *pkt;
- pkt->destruct= NULL; // do not free original but only the copy
- av_dup_packet(&this_pktl->pkt); // duplicate the packet if it uses non-alloced memory
-
- if(s->streams[pkt->stream_index]->last_in_packet_buffer){
- next_point = &(s->streams[pkt->stream_index]->last_in_packet_buffer->next);
- }else
- next_point = &s->packet_buffer;
-
- if(*next_point){
- if(compare(s, &s->packet_buffer_end->pkt, pkt)){
- while(!compare(s, &(*next_point)->pkt, pkt)){
- next_point= &(*next_point)->next;
- }
- goto next_non_null;
- }else{
- next_point = &(s->packet_buffer_end->next);
- }
- }
- assert(!*next_point);
-
- s->packet_buffer_end= this_pktl;
-next_non_null:
-
- this_pktl->next= *next_point;
-
- s->streams[pkt->stream_index]->last_in_packet_buffer=
- *next_point= this_pktl;
-}
-
-static int ff_interleave_compare_dts(AVFormatContext *s, AVPacket *next, AVPacket *pkt)
-{
- AVStream *st = s->streams[ pkt ->stream_index];
- AVStream *st2= s->streams[ next->stream_index];
- int comp = av_compare_ts(next->dts, st2->time_base, pkt->dts,
- st->time_base);
-
- if (comp == 0)
- return pkt->stream_index < next->stream_index;
- return comp > 0;
-}
-
-int ff_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out,
- AVPacket *pkt, int flush)
-{
- AVPacketList *pktl;
- int stream_count=0;
- int i;
-
- if(pkt){
- ff_interleave_add_packet(s, pkt, ff_interleave_compare_dts);
- }
-
- for(i=0; i < s->nb_streams; i++)
- stream_count+= !!s->streams[i]->last_in_packet_buffer;
-
- if(stream_count && (s->nb_streams == stream_count || flush)){
- pktl= s->packet_buffer;
- *out= pktl->pkt;
-
- s->packet_buffer= pktl->next;
- if(!s->packet_buffer)
- s->packet_buffer_end= NULL;
-
- if(s->streams[out->stream_index]->last_in_packet_buffer == pktl)
- s->streams[out->stream_index]->last_in_packet_buffer= NULL;
- av_freep(&pktl);
- return 1;
- }else{
- av_init_packet(out);
- return 0;
- }
-}
-
-#if FF_API_INTERLEAVE_PACKET
-int av_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out,
- AVPacket *pkt, int flush)
-{
- return ff_interleave_packet_per_dts(s, out, pkt, flush);
-}
-#endif
-
-/**
- * Interleave an AVPacket correctly so it can be muxed.
- * @param out the interleaved packet will be output here
- * @param in the input packet
- * @param flush 1 if no further packets are available as input and all
- * remaining packets should be output
- * @return 1 if a packet was output, 0 if no packet could be output,
- * < 0 if an error occurred
- */
-static int interleave_packet(AVFormatContext *s, AVPacket *out, AVPacket *in, int flush){
- if (s->oformat->interleave_packet) {
- int ret = s->oformat->interleave_packet(s, out, in, flush);
- if (in)
- av_free_packet(in);
- return ret;
- } else
- return ff_interleave_packet_per_dts(s, out, in, flush);
-}
-
-int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt){
- int ret, flush = 0;
-
- if (pkt) {
- AVStream *st= s->streams[ pkt->stream_index];
-
- //FIXME/XXX/HACK drop zero sized packets
- if(st->codec->codec_type == AVMEDIA_TYPE_AUDIO && pkt->size==0)
- return 0;
-
- av_dlog(s, "av_interleaved_write_frame size:%d dts:%"PRId64" pts:%"PRId64"\n",
- pkt->size, pkt->dts, pkt->pts);
- if((ret = compute_pkt_fields2(s, st, pkt)) < 0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
- return ret;
-
- if(pkt->dts == AV_NOPTS_VALUE && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
- return AVERROR(EINVAL);
- } else {
- av_dlog(s, "av_interleaved_write_frame FLUSH\n");
- flush = 1;
- }
-
- for(;;){
- AVPacket opkt;
- int ret= interleave_packet(s, &opkt, pkt, flush);
- if(ret<=0) //FIXME cleanup needed for ret<0 ?
- return ret;
-
- ret= s->oformat->write_packet(s, &opkt);
- if (ret >= 0)
- s->streams[opkt.stream_index]->nb_frames++;
-
- av_free_packet(&opkt);
- pkt= NULL;
-
- if(ret<0)
- return ret;
- }
-}
-
-int av_write_trailer(AVFormatContext *s)
-{
- int ret, i;
-
- for(;;){
- AVPacket pkt;
- ret= interleave_packet(s, &pkt, NULL, 1);
- if(ret<0) //FIXME cleanup needed for ret<0 ?
- goto fail;
- if(!ret)
- break;
-
- ret= s->oformat->write_packet(s, &pkt);
- if (ret >= 0)
- s->streams[pkt.stream_index]->nb_frames++;
-
- av_free_packet(&pkt);
-
- if(ret<0)
- goto fail;
- }
-
- if(s->oformat->write_trailer)
- ret = s->oformat->write_trailer(s);
-fail:
- for(i=0;i<s->nb_streams;i++) {
- av_freep(&s->streams[i]->priv_data);
- av_freep(&s->streams[i]->index_entries);
- }
- if (s->oformat->priv_class)
- av_opt_free(s->priv_data);
- av_freep(&s->priv_data);
- return ret;
-}
-
void ff_program_add_stream_index(AVFormatContext *ac, int progid, unsigned int idx)
{
int i, j;
AVProgram *program=NULL;
- void *tmp;
if (idx >= ac->nb_streams) {
av_log(ac, AV_LOG_ERROR, "stream index %d is not valid\n", idx);
if(program->stream_index[j] == idx)
return;
- tmp = av_realloc(program->stream_index, sizeof(unsigned int)*(program->nb_stream_indexes+1));
- if(!tmp)
+ if (av_reallocp_array(&program->stream_index,
+ program->nb_stream_indexes + 1,
+ sizeof(*program->stream_index)) < 0) {
+ program->nb_stream_indexes = 0;
return;
- program->stream_index = tmp;
+ }
program->stream_index[program->nb_stream_indexes++] = idx;
return;
}
static void dump_metadata(void *ctx, AVDictionary *m, const char *indent)
{
- if(m && !(m->count == 1 && av_dict_get(m, "language", NULL, 0))){
+ if(m && !(av_dict_count(m) == 1 && av_dict_get(m, "language", NULL, 0))){
AVDictionaryEntry *tag=NULL;
av_log(ctx, AV_LOG_INFO, "%sMetadata:\n", indent);
if(st->codec->codec_type == AVMEDIA_TYPE_VIDEO){
if(st->avg_frame_rate.den && st->avg_frame_rate.num)
print_fps(av_q2d(st->avg_frame_rate), "fps");
-#if FF_API_R_FRAME_RATE
- if(st->r_frame_rate.den && st->r_frame_rate.num)
- print_fps(av_q2d(st->r_frame_rate), "tbr");
-#endif
if(st->time_base.den && st->time_base.num)
print_fps(1/av_q2d(st->time_base), "tbn");
if(st->codec->time_base.den && st->codec->time_base.num)
av_free(printed);
}
-#if FF_API_AV_GETTIME && CONFIG_SHARED && HAVE_SYMVER
-FF_SYMVER(int64_t, av_gettime, (void), "LIBAVFORMAT_54")
-{
- return av_gettime();
-}
-#endif
-
uint64_t ff_ntp_time(void)
{
return (av_gettime() / 1000) * 1000 + NTP_OFFSET_US;
if (c == '%') {
do {
nd = 0;
- while (isdigit(*p)) {
+ while (av_isdigit(*p)) {
nd = nd * 10 + *p++ - '0';
}
c = *p++;
- } while (isdigit(c));
+ } while (av_isdigit(c));
switch(c) {
case '%':
return -1;
}
-static void hex_dump_internal(void *avcl, FILE *f, int level, uint8_t *buf, int size)
+static void hex_dump_internal(void *avcl, FILE *f, int level,
+ const uint8_t *buf, int size)
{
int len, i, j, c;
-#undef fprintf
#define PRINT(...) do { if (!f) av_log(avcl, level, __VA_ARGS__); else fprintf(f, __VA_ARGS__); } while(0)
for(i=0;i<size;i+=16) {
#undef PRINT
}
-void av_hex_dump(FILE *f, uint8_t *buf, int size)
+void av_hex_dump(FILE *f, const uint8_t *buf, int size)
{
hex_dump_internal(NULL, f, 0, buf, size);
}
-void av_hex_dump_log(void *avcl, int level, uint8_t *buf, int size)
+void av_hex_dump_log(void *avcl, int level, const uint8_t *buf, int size)
{
hex_dump_internal(avcl, NULL, level, buf, size);
}
static void pkt_dump_internal(void *avcl, FILE *f, int level, AVPacket *pkt, int dump_payload, AVRational time_base)
{
-#undef fprintf
#define PRINT(...) do { if (!f) av_log(avcl, level, __VA_ARGS__); else fprintf(f, __VA_ARGS__); } while(0)
PRINT("stream #%d:\n", pkt->stream_index);
PRINT(" keyframe=%d\n", ((pkt->flags & AV_PKT_FLAG_KEY) != 0));
p += strspn(p, SPACE_CHARS);
if (*p == '\0')
break;
- c = toupper((unsigned char) *p++);
+ c = av_toupper((unsigned char) *p++);
if (c >= '0' && c <= '9')
c = c - '0';
else if (c >= 'A' && c <= 'F')
s->pts_wrap_bits = pts_wrap_bits;
}
-int ff_url_join(char *str, int size, const char *proto,
- const char *authorization, const char *hostname,
- int port, const char *fmt, ...)
-{
-#if CONFIG_NETWORK
- struct addrinfo hints = { 0 }, *ai;
-#endif
-
- str[0] = '\0';
- if (proto)
- av_strlcatf(str, size, "%s://", proto);
- if (authorization && authorization[0])
- av_strlcatf(str, size, "%s@", authorization);
-#if CONFIG_NETWORK && defined(AF_INET6)
- /* Determine if hostname is a numerical IPv6 address,
- * properly escape it within [] in that case. */
- hints.ai_flags = AI_NUMERICHOST;
- if (!getaddrinfo(hostname, NULL, &hints, &ai)) {
- if (ai->ai_family == AF_INET6) {
- av_strlcat(str, "[", size);
- av_strlcat(str, hostname, size);
- av_strlcat(str, "]", size);
- } else {
- av_strlcat(str, hostname, size);
- }
- freeaddrinfo(ai);
- } else
-#endif
- /* Not an IPv6 address, just output the plain string. */
- av_strlcat(str, hostname, size);
-
- if (port >= 0)
- av_strlcatf(str, size, ":%d", port);
- if (fmt) {
- va_list vl;
- int len = strlen(str);
-
- va_start(vl, fmt);
- vsnprintf(str + len, size > len ? size - len : 0, fmt, vl);
- va_end(vl);
- }
- return strlen(str);
-}
-
-int ff_write_chained(AVFormatContext *dst, int dst_stream, AVPacket *pkt,
- AVFormatContext *src)
-{
- AVPacket local_pkt;
-
- local_pkt = *pkt;
- local_pkt.stream_index = dst_stream;
- if (pkt->pts != AV_NOPTS_VALUE)
- local_pkt.pts = av_rescale_q(pkt->pts,
- src->streams[pkt->stream_index]->time_base,
- dst->streams[dst_stream]->time_base);
- if (pkt->dts != AV_NOPTS_VALUE)
- local_pkt.dts = av_rescale_q(pkt->dts,
- src->streams[pkt->stream_index]->time_base,
- dst->streams[dst_stream]->time_base);
- return av_write_frame(dst, &local_pkt);
-}
-
void ff_parse_key_value(const char *str, ff_parse_key_val_cb callback_get_buf,
void *context)
{
int key_len, dest_len = 0;
/* Skip whitespace and potential commas. */
- while (*ptr && (isspace(*ptr) || *ptr == ','))
+ while (*ptr && (av_isspace(*ptr) || *ptr == ','))
ptr++;
if (!*ptr)
break;
if (*ptr == '\"')
ptr++;
} else {
- for (; *ptr && !(isspace(*ptr) || *ptr == ','); ptr++)
+ for (; *ptr && !(av_isspace(*ptr) || *ptr == ','); ptr++)
if (dest && dest < dest_end)
*dest++ = *ptr;
}
return -1;
}
-void ff_make_absolute_url(char *buf, int size, const char *base,
- const char *rel)
-{
- char *sep;
- /* Absolute path, relative to the current server */
- if (base && strstr(base, "://") && rel[0] == '/') {
- if (base != buf)
- av_strlcpy(buf, base, size);
- sep = strstr(buf, "://");
- if (sep) {
- sep += 3;
- sep = strchr(sep, '/');
- if (sep)
- *sep = '\0';
- }
- av_strlcat(buf, rel, size);
- return;
- }
- /* If rel actually is an absolute url, just copy it */
- if (!base || strstr(rel, "://") || rel[0] == '/') {
- av_strlcpy(buf, rel, size);
- return;
- }
- if (base != buf)
- av_strlcpy(buf, base, size);
- /* Remove the file name from the base url */
- sep = strrchr(buf, '/');
- if (sep)
- sep[1] = '\0';
- else
- buf[0] = '\0';
- while (av_strstart(rel, "../", NULL) && sep) {
- /* Remove the path delimiter at the end */
- sep[0] = '\0';
- sep = strrchr(buf, '/');
- /* If the next directory name to pop off is "..", break here */
- if (!strcmp(sep ? &sep[1] : buf, "..")) {
- /* Readd the slash we just removed */
- av_strlcat(buf, "/", size);
- break;
- }
- /* Cut off the directory name */
- if (sep)
- sep[1] = '\0';
- else
- buf[0] = '\0';
- rel += 3;
- }
- av_strlcat(buf, rel, size);
-}
-
int64_t ff_iso8601_to_unix_time(const char *datestr)
{
#if HAVE_STRPTIME
#endif
}
-int avformat_query_codec(AVOutputFormat *ofmt, enum CodecID codec_id, int std_compliance)
+int avformat_query_codec(AVOutputFormat *ofmt, enum AVCodecID codec_id, int std_compliance)
{
if (ofmt) {
if (ofmt->query_codec)
}
return 0;
}
-
-const struct AVCodecTag *avformat_get_riff_video_tags(void)
-{
- return ff_codec_bmp_tags;
-}
-const struct AVCodecTag *avformat_get_riff_audio_tags(void)
-{
- return ff_codec_wav_tags;
-}