#include "libavcodec/internal.h"
#include "libavutil/opt.h"
#include "libavutil/dict.h"
+#include "libavutil/pixdesc.h"
#include "metadata.h"
#include "id3v2.h"
#include "libavutil/avstring.h"
+#include "libavutil/mathematics.h"
#include "riff.h"
#include "audiointerleave.h"
#include "url.h"
* @param num must be >= 0
* @param den must be >= 1
*/
-static void av_frac_init(AVFrac *f, int64_t val, int64_t num, int64_t den)
+static void frac_init(AVFrac *f, int64_t val, int64_t num, int64_t den)
{
num += (den >> 1);
if (num >= den) {
* @param f fractional number
* @param incr increment, can be positive or negative
*/
-static void av_frac_add(AVFrac *f, int64_t incr)
+static void frac_add(AVFrac *f, int64_t incr)
{
int64_t num, den;
/************************************************************/
/* input media file */
+#if FF_API_FORMAT_PARAMETERS
+static AVDictionary *convert_format_parameters(AVFormatParameters *ap)
+{
+ char buf[1024];
+ AVDictionary *opts = NULL;
+
+ if (!ap)
+ return NULL;
+
+ if (ap->time_base.num) {
+ snprintf(buf, sizeof(buf), "%d/%d", ap->time_base.den, ap->time_base.num);
+ av_dict_set(&opts, "framerate", buf, 0);
+ }
+ if (ap->sample_rate) {
+ snprintf(buf, sizeof(buf), "%d", ap->sample_rate);
+ av_dict_set(&opts, "sample_rate", buf, 0);
+ }
+ if (ap->channels) {
+ snprintf(buf, sizeof(buf), "%d", ap->channels);
+ av_dict_set(&opts, "channels", buf, 0);
+ }
+ if (ap->width || ap->height) {
+ snprintf(buf, sizeof(buf), "%dx%d", ap->width, ap->height);
+ av_dict_set(&opts, "video_size", buf, 0);
+ }
+ if (ap->pix_fmt != PIX_FMT_NONE) {
+ av_dict_set(&opts, "pixel_format", av_get_pix_fmt_name(ap->pix_fmt), 0);
+ }
+ if (ap->channel) {
+ snprintf(buf, sizeof(buf), "%d", ap->channel);
+ av_dict_set(&opts, "channel", buf, 0);
+ }
+ if (ap->standard) {
+ av_dict_set(&opts, "standard", ap->standard, 0);
+ }
+ if (ap->mpeg2ts_compute_pcr) {
+ av_dict_set(&opts, "mpeg2ts_compute_pcr", "1", 0);
+ }
+ if (ap->initial_pause) {
+ av_dict_set(&opts, "initial_pause", "1", 0);
+ }
+ return opts;
+}
+
/**
* Open a media file from an IO stream. 'fmt' must be specified.
*/
AVInputFormat *fmt, AVFormatParameters *ap)
{
int err;
+ AVDictionary *opts;
AVFormatContext *ic;
AVFormatParameters default_ap;
ap=&default_ap;
memset(ap, 0, sizeof(default_ap));
}
+ opts = convert_format_parameters(ap);
if(!ap->prealloced_context)
ic = avformat_alloc_context();
err = AVERROR(ENOMEM);
goto fail;
}
- ic->iformat = fmt;
- ic->pb = pb;
- ic->duration = AV_NOPTS_VALUE;
- ic->start_time = AV_NOPTS_VALUE;
- av_strlcpy(ic->filename, filename, sizeof(ic->filename));
-
- /* allocate private data */
- if (fmt->priv_data_size > 0) {
- ic->priv_data = av_mallocz(fmt->priv_data_size);
- if (!ic->priv_data) {
- err = AVERROR(ENOMEM);
- goto fail;
- }
- if (fmt->priv_class) {
- *(const AVClass**)ic->priv_data = fmt->priv_class;
- av_opt_set_defaults(ic->priv_data);
- }
- } else {
- ic->priv_data = NULL;
- }
-
- // e.g. AVFMT_NOFILE formats will not have a AVIOContext
- if (ic->pb)
- ff_id3v2_read(ic, ID3v2_DEFAULT_MAGIC);
-
- if (ic->iformat->read_header) {
- err = ic->iformat->read_header(ic, ap);
- if (err < 0)
- goto fail;
- }
-
- if (pb && !ic->data_offset)
- ic->data_offset = avio_tell(ic->pb);
+ if (pb && fmt && fmt->flags & AVFMT_NOFILE)
+ av_log(ic, AV_LOG_WARNING, "Custom AVIOContext makes no sense and "
+ "will be ignored with AVFMT_NOFILE format.\n");
+ else
+ ic->pb = pb;
- ic->raw_packet_buffer_remaining_size = RAW_PACKET_BUFFER_SIZE;
+ if ((err = avformat_open_input(&ic, filename, fmt, &opts)) < 0)
+ goto fail;
+ ic->pb = ic->pb ? ic->pb : pb; // don't leak custom pb if it wasn't set above
+fail:
*ic_ptr = ic;
- return 0;
- fail:
- if (ic) {
- int i;
- av_freep(&ic->priv_data);
- for(i=0;i<ic->nb_streams;i++) {
- AVStream *st = ic->streams[i];
- if (st) {
- av_free(st->priv_data);
- av_free(st->codec->extradata);
- av_free(st->codec);
- av_free(st->info);
- }
- av_free(st);
- }
- }
- av_free(ic);
- *ic_ptr = NULL;
+ av_dict_free(&opts);
return err;
}
+#endif
/** size of probe buffer, for guessing file type from file contents */
#define PROBE_BUF_MIN 2048
return AVERROR(EINVAL);
}
- for(probe_size= PROBE_BUF_MIN; probe_size<=max_probe_size && !*fmt && ret >= 0;
+ for(probe_size= PROBE_BUF_MIN; probe_size<=max_probe_size && !*fmt;
probe_size = FFMIN(probe_size<<1, FFMAX(max_probe_size, probe_size+1))) {
- int ret, score = probe_size < max_probe_size ? AVPROBE_SCORE_MAX/4 : 0;
+ int score = probe_size < max_probe_size ? AVPROBE_SCORE_MAX/4 : 0;
int buf_offset = (probe_size == PROBE_BUF_MIN) ? 0 : probe_size>>1;
if (probe_size < offset) {
return ret;
}
+#if FF_API_FORMAT_PARAMETERS
int av_open_input_file(AVFormatContext **ic_ptr, const char *filename,
AVInputFormat *fmt,
int buf_size,
AVFormatParameters *ap)
{
int err;
- AVProbeData probe_data, *pd = &probe_data;
- AVIOContext *pb = NULL;
- void *logctx= ap && ap->prealloced_context ? *ic_ptr : NULL;
-
- pd->filename = "";
- if (filename)
- pd->filename = filename;
- pd->buf = NULL;
- pd->buf_size = 0;
-
- if (!fmt) {
- /* guess format if no file can be opened */
- fmt = av_probe_input_format(pd, 0);
- }
-
- /* Do not open file if the format does not need it. XXX: specific
- hack needed to handle RTSP/TCP */
- if (!fmt || !(fmt->flags & AVFMT_NOFILE)) {
- /* if no file needed do not try to open one */
- if ((err=avio_open(&pb, filename, AVIO_FLAG_READ)) < 0) {
- goto fail;
- }
- if (buf_size > 0) {
- ffio_set_buf_size(pb, buf_size);
- }
- if (!fmt && (err = av_probe_input_buffer(pb, &fmt, filename, logctx, 0, logctx ? (*ic_ptr)->probesize : 0)) < 0) {
- goto fail;
- }
- }
+ AVDictionary *opts = convert_format_parameters(ap);
- /* if still no format found, error */
- if (!fmt) {
- err = AVERROR_INVALIDDATA;
- goto fail;
- }
+ if (!ap || !ap->prealloced_context)
+ *ic_ptr = NULL;
- /* check filename in case an image number is expected */
- if (fmt->flags & AVFMT_NEEDNUMBER) {
- if (!av_filename_number_test(filename)) {
- err = AVERROR(EINVAL);
- goto fail;
- }
- }
- err = av_open_input_stream(ic_ptr, pb, filename, fmt, ap);
- if (err)
- goto fail;
- return 0;
- fail:
- av_freep(&pd->buf);
- if (pb)
- avio_close(pb);
- if (ap && ap->prealloced_context)
- av_free(*ic_ptr);
- *ic_ptr = NULL;
- return err;
+ err = avformat_open_input(ic_ptr, filename, fmt, &opts);
+ av_dict_free(&opts);
+ return err;
}
+#endif
/* open input file and probe the format if necessary */
static int init_input(AVFormatContext *s, const char *filename)
return 0;
if ((ret = avio_open(&s->pb, filename, AVIO_FLAG_READ)) < 0)
- return ret;
+ return ret;
if (s->iformat)
return 0;
return av_probe_input_buffer(s->pb, &s->iformat, filename, s, 0, 0);
{
AVFormatContext *s = *ps;
int ret = 0;
- AVFormatParameters ap = { 0 };
+ AVFormatParameters ap = { { 0 } };
AVDictionary *tmp = NULL;
if (!s && !(s = avformat_alloc_context()))
s->streams[i]->probe_packets = 0;
continue;
}
+
+ if ((s->flags & AVFMT_FLAG_DISCARD_CORRUPT) &&
+ (pkt->flags & AV_PKT_FLAG_CORRUPT)) {
+ av_log(s, AV_LOG_WARNING,
+ "Dropped corrupted packet (stream = %d)\n",
+ pkt->stream_index);
+ continue;
+ }
+
st= s->streams[pkt->stream_index];
switch(st->codec->codec_type){
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:
}
-static int av_read_frame_internal(AVFormatContext *s, AVPacket *pkt)
+static int read_frame_internal(AVFormatContext *s, AVPacket *pkt)
{
AVStream *st;
int len, ret, i;
st->cur_pkt.data = NULL;
assert(st->cur_len == 0);
}else{
- pkt->destruct = NULL;
+ pkt->destruct = NULL;
}
compute_pkt_fields(s, st, st->parser, pkt);
}
}
if(s->debug & FF_FDEBUG_TS)
- av_log(s, AV_LOG_DEBUG, "av_read_frame_internal stream=%d, pts=%"PRId64", dts=%"PRId64", size=%d, duration=%d, flags=%d\n",
+ av_log(s, AV_LOG_DEBUG, "read_frame_internal stream=%d, pts=%"PRId64", dts=%"PRId64", size=%d, duration=%d, flags=%d\n",
pkt->stream_index,
pkt->pts,
pkt->dts,
}
}
if(genpts){
- int ret= av_read_frame_internal(s, pkt);
+ int ret= read_frame_internal(s, pkt);
if(ret<0){
if(pktl && ret != AVERROR(EAGAIN)){
eof=1;
return AVERROR(ENOMEM);
}else{
assert(!s->packet_buffer);
- return av_read_frame_internal(s, pkt);
+ return read_frame_internal(s, pkt);
}
}
}
return pos;
}
-static int av_seek_frame_byte(AVFormatContext *s, int stream_index, int64_t pos, int flags){
+static int seek_frame_byte(AVFormatContext *s, int stream_index, int64_t pos, int flags){
int64_t pos_min, pos_max;
#if 0
AVStream *st;
return 0;
}
-static int av_seek_frame_generic(AVFormatContext *s,
+static int seek_frame_generic(AVFormatContext *s,
int stream_index, int64_t timestamp, int flags)
{
int index;
return -1;
if(index < 0 || index==st->nb_index_entries-1){
- int i;
AVPacket pkt;
if(st->nb_index_entries){
if ((ret = avio_seek(s->pb, s->data_offset, SEEK_SET)) < 0)
return ret;
}
- for(i=0;; i++) {
- int ret;
+ for (;;) {
+ int read_status;
do{
- ret = av_read_frame(s, &pkt);
- }while(ret == AVERROR(EAGAIN));
- if(ret<0)
+ read_status = av_read_frame(s, &pkt);
+ } while (read_status == AVERROR(EAGAIN));
+ if (read_status < 0)
break;
av_free_packet(&pkt);
if(stream_index == pkt.stream_index){
int ret;
AVStream *st;
- ff_read_frame_flush(s);
-
- if(flags & AVSEEK_FLAG_BYTE)
- return av_seek_frame_byte(s, stream_index, timestamp, flags);
+ if (flags & AVSEEK_FLAG_BYTE) {
+ if (s->iformat->flags & AVFMT_NO_BYTE_SEEK)
+ return -1;
+ ff_read_frame_flush(s);
+ return seek_frame_byte(s, stream_index, timestamp, flags);
+ }
if(stream_index < 0){
stream_index= av_find_default_stream_index(s);
return -1;
st= s->streams[stream_index];
- /* timestamp for default must be expressed in AV_TIME_BASE units */
+ /* timestamp for default must be expressed in AV_TIME_BASE units */
timestamp = av_rescale(timestamp, st->time_base.den, AV_TIME_BASE * (int64_t)st->time_base.num);
}
/* first, we try the format specific seek */
- if (s->iformat->read_seek)
+ if (s->iformat->read_seek) {
+ ff_read_frame_flush(s);
ret = s->iformat->read_seek(s, stream_index, timestamp, flags);
- else
+ } else
ret = -1;
if (ret >= 0) {
return 0;
}
- if(s->iformat->read_timestamp && !(s->iformat->flags & AVFMT_NOBINSEARCH))
+ if (s->iformat->read_timestamp && !(s->iformat->flags & AVFMT_NOBINSEARCH)) {
+ ff_read_frame_flush(s);
return av_seek_frame_binary(s, stream_index, timestamp, flags);
- else if (!(s->iformat->flags & AVFMT_NOGENSEARCH))
- return av_seek_frame_generic(s, stream_index, timestamp, flags);
+ } else if (!(s->iformat->flags & AVFMT_NOGENSEARCH)) {
+ ff_read_frame_flush(s);
+ return seek_frame_generic(s, stream_index, timestamp, flags);
+ }
else
return -1;
}
if(min_ts > ts || max_ts < ts)
return -1;
- ff_read_frame_flush(s);
-
- if (s->iformat->read_seek2)
+ if (s->iformat->read_seek2) {
+ ff_read_frame_flush(s);
return s->iformat->read_seek2(s, stream_index, min_ts, ts, max_ts, flags);
+ }
if(s->iformat->read_timestamp){
//try to seek via read_timestamp()
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));
- // try some generic seek like av_seek_frame_generic() but with new ts semantics
+ // try some generic seek like seek_frame_generic() but with new ts semantics
}
/*******************************************************/
*
* @return TRUE if the stream has accurate duration for at least one component.
*/
-static int av_has_duration(AVFormatContext *ic)
+static int has_duration(AVFormatContext *ic)
{
int i;
AVStream *st;
*
* Also computes the global bitrate if possible.
*/
-static void av_update_stream_timings(AVFormatContext *ic)
+static void update_stream_timings(AVFormatContext *ic)
{
int64_t start_time, start_time1, end_time, end_time1;
- int64_t duration, duration1;
+ int64_t duration, duration1, filesize;
int i;
AVStream *st;
}
if (duration != INT64_MIN) {
ic->duration = duration;
- if (ic->file_size > 0) {
+ if (ic->pb && (filesize = avio_size(ic->pb)) > 0) {
/* compute the bitrate */
- ic->bit_rate = (double)ic->file_size * 8.0 * AV_TIME_BASE /
+ ic->bit_rate = (double)filesize * 8.0 * AV_TIME_BASE /
(double)ic->duration;
}
}
int i;
AVStream *st;
- av_update_stream_timings(ic);
+ update_stream_timings(ic);
for(i = 0;i < ic->nb_streams; i++) {
st = ic->streams[i];
if (st->start_time == AV_NOPTS_VALUE) {
}
}
-static void av_estimate_timings_from_bit_rate(AVFormatContext *ic)
+static void estimate_timings_from_bit_rate(AVFormatContext *ic)
{
int64_t filesize, duration;
int bit_rate, i;
/* if duration is already set, we believe it */
if (ic->duration == AV_NOPTS_VALUE &&
- ic->bit_rate != 0 &&
- ic->file_size != 0) {
- filesize = ic->file_size;
+ ic->bit_rate != 0) {
+ filesize = ic->pb ? avio_size(ic->pb) : 0;
if (filesize > 0) {
for(i = 0; i < ic->nb_streams; i++) {
st = ic->streams[i];
#define DURATION_MAX_RETRY 3
/* only usable for MPEG-PS streams */
-static void av_estimate_timings_from_pts(AVFormatContext *ic, int64_t old_offset)
+static void estimate_timings_from_pts(AVFormatContext *ic, int64_t old_offset)
{
AVPacket pkt1, *pkt = &pkt1;
AVStream *st;
for (i=0; i<ic->nb_streams; i++) {
st = ic->streams[i];
if (st->start_time == AV_NOPTS_VALUE && st->first_dts == AV_NOPTS_VALUE)
- av_log(st->codec, AV_LOG_WARNING, "start time is not set in av_estimate_timings_from_pts\n");
+ av_log(st->codec, AV_LOG_WARNING, "start time is not set in estimate_timings_from_pts\n");
if (st->parser) {
av_parser_close(st->parser);
/* estimate the end time (duration) */
/* XXX: may need to support wrapping */
- filesize = ic->file_size;
+ filesize = ic->pb ? avio_size(ic->pb) : 0;
end_time = AV_NOPTS_VALUE;
do{
- offset = filesize - (DURATION_MAX_READ_SIZE<<retry);
- if (offset < 0)
- offset = 0;
+ offset = filesize - (DURATION_MAX_READ_SIZE<<retry);
+ if (offset < 0)
+ offset = 0;
- avio_seek(ic->pb, offset, SEEK_SET);
- read_size = 0;
- for(;;) {
- if (read_size >= DURATION_MAX_READ_SIZE<<(FFMAX(retry-1,0)))
- break;
+ avio_seek(ic->pb, offset, SEEK_SET);
+ read_size = 0;
+ for(;;) {
+ if (read_size >= DURATION_MAX_READ_SIZE<<(FFMAX(retry-1,0)))
+ break;
- do{
- ret = av_read_packet(ic, pkt);
- }while(ret == AVERROR(EAGAIN));
- if (ret != 0)
- break;
- read_size += pkt->size;
- st = ic->streams[pkt->stream_index];
- if (pkt->pts != AV_NOPTS_VALUE &&
- (st->start_time != AV_NOPTS_VALUE ||
- st->first_dts != AV_NOPTS_VALUE)) {
- duration = end_time = pkt->pts;
- if (st->start_time != AV_NOPTS_VALUE) duration -= st->start_time;
- else duration -= st->first_dts;
- if (duration < 0)
- duration += 1LL<<st->pts_wrap_bits;
- if (duration > 0) {
- if (st->duration == AV_NOPTS_VALUE ||
- st->duration < duration)
- st->duration = duration;
+ do {
+ ret = av_read_packet(ic, pkt);
+ } while(ret == AVERROR(EAGAIN));
+ if (ret != 0)
+ break;
+ read_size += pkt->size;
+ st = ic->streams[pkt->stream_index];
+ if (pkt->pts != AV_NOPTS_VALUE &&
+ (st->start_time != AV_NOPTS_VALUE ||
+ st->first_dts != AV_NOPTS_VALUE)) {
+ duration = end_time = pkt->pts;
+ if (st->start_time != AV_NOPTS_VALUE)
+ duration -= st->start_time;
+ else
+ duration -= st->first_dts;
+ if (duration < 0)
+ duration += 1LL<<st->pts_wrap_bits;
+ if (duration > 0) {
+ if (st->duration == AV_NOPTS_VALUE || st->duration < duration)
+ st->duration = duration;
+ }
}
+ av_free_packet(pkt);
}
- av_free_packet(pkt);
- }
}while( end_time==AV_NOPTS_VALUE
&& filesize > (DURATION_MAX_READ_SIZE<<retry)
&& ++retry <= DURATION_MAX_RETRY);
}
}
-static void av_estimate_timings(AVFormatContext *ic, int64_t old_offset)
+static void estimate_timings(AVFormatContext *ic, int64_t old_offset)
{
int64_t file_size;
if (file_size < 0)
file_size = 0;
}
- ic->file_size = file_size;
if ((!strcmp(ic->iformat->name, "mpeg") ||
!strcmp(ic->iformat->name, "mpegts")) &&
file_size && ic->pb->seekable) {
/* get accurate estimate from the PTSes */
- av_estimate_timings_from_pts(ic, old_offset);
- } else if (av_has_duration(ic)) {
+ estimate_timings_from_pts(ic, old_offset);
+ } else if (has_duration(ic)) {
/* at least one component has timings - we use them for all
the components */
fill_all_stream_timings(ic);
} else {
av_log(ic, AV_LOG_WARNING, "Estimating duration from bitrate, this may be inaccurate\n");
/* less precise: use bitrate info */
- av_estimate_timings_from_bit_rate(ic);
+ estimate_timings_from_bit_rate(ic);
}
- av_update_stream_timings(ic);
+ update_stream_timings(ic);
{
int i;
}
}
-static int has_codec_parameters(AVCodecContext *enc)
+static int has_codec_parameters(AVCodecContext *avctx)
{
int val;
- switch(enc->codec_type) {
+ switch (avctx->codec_type) {
case AVMEDIA_TYPE_AUDIO:
- val = enc->sample_rate && enc->channels && enc->sample_fmt != AV_SAMPLE_FMT_NONE;
- if(!enc->frame_size &&
- (enc->codec_id == CODEC_ID_VORBIS ||
- enc->codec_id == CODEC_ID_AAC ||
- enc->codec_id == CODEC_ID_MP1 ||
- enc->codec_id == CODEC_ID_MP2 ||
- enc->codec_id == CODEC_ID_MP3 ||
- enc->codec_id == CODEC_ID_SPEEX))
+ val = avctx->sample_rate && avctx->channels && avctx->sample_fmt != AV_SAMPLE_FMT_NONE;
+ if (!avctx->frame_size &&
+ (avctx->codec_id == CODEC_ID_VORBIS ||
+ avctx->codec_id == CODEC_ID_AAC ||
+ avctx->codec_id == CODEC_ID_MP1 ||
+ avctx->codec_id == CODEC_ID_MP2 ||
+ avctx->codec_id == CODEC_ID_MP3 ||
+ avctx->codec_id == CODEC_ID_CELT))
return 0;
break;
case AVMEDIA_TYPE_VIDEO:
- val = enc->width && enc->pix_fmt != PIX_FMT_NONE;
+ val = avctx->width && avctx->pix_fmt != PIX_FMT_NONE;
break;
default:
val = 1;
break;
}
- return enc->codec_id != CODEC_ID_NONE && val != 0;
+ return avctx->codec_id != CODEC_ID_NONE && val != 0;
}
static int has_decode_delay_been_guessed(AVStream *st)
st->codec_info_nb_frames >= 6 + st->codec->has_b_frames;
}
-static int try_decode_frame(AVStream *st, AVPacket *avpkt)
+static int try_decode_frame(AVStream *st, AVPacket *avpkt, AVDictionary **options)
{
int16_t *samples;
AVCodec *codec;
codec = avcodec_find_decoder(st->codec->codec_id);
if (!codec)
return -1;
- ret = avcodec_open(st->codec, codec);
+ ret = avcodec_open2(st->codec, codec, options);
if (ret < 0)
return ret;
}
- if(!has_codec_parameters(st->codec) || !has_decode_delay_been_guessed(st)){
+ if(!has_codec_parameters(st->codec) || !has_decode_delay_been_guessed(st) ||
+ (!st->codec_info_nb_frames && st->codec->codec->capabilities & CODEC_CAP_CHANNEL_CONF)) {
switch(st->codec->codec_type) {
case AVMEDIA_TYPE_VIDEO:
avcodec_get_frame_defaults(&picture);
return 0;
}
+#if FF_API_FORMAT_PARAMETERS
int av_find_stream_info(AVFormatContext *ic)
+{
+ return avformat_find_stream_info(ic, NULL);
+}
+#endif
+
+int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
{
int i, count, ret, read_size, j;
AVStream *st;
AVPacket pkt1, *pkt;
int64_t old_offset = avio_tell(ic->pb);
+ 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;
st = ic->streams[i];
- if (st->codec->codec_id == CODEC_ID_AAC) {
- st->codec->sample_rate = 0;
- st->codec->frame_size = 0;
- st->codec->channels = 0;
- }
+
if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO ||
st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
/* if(!st->time_base.num)
assert(!st->codec->codec);
codec = avcodec_find_decoder(st->codec->codec_id);
- /* Force decoding of at least one frame of codec data
- * this makes sure the codec initializes the channel configuration
- * and does not trust the values from the container.
- */
- if (codec && codec->capabilities & CODEC_CAP_CHANNEL_CONF)
- st->codec->channels = 0;
-
/* Ensure that subtitle_header is properly set. */
if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE
&& codec && !st->codec->codec)
- avcodec_open(st->codec, codec);
+ avcodec_open2(st->codec, codec, options ? &options[i] : NULL);
//try to just open decoders, in case this is enough to get parameters
if(!has_codec_parameters(st->codec)){
if (codec && !st->codec->codec)
- avcodec_open(st->codec, codec);
+ avcodec_open2(st->codec, codec, options ? &options[i] : NULL);
}
}
/* NOTE: a new stream can be added there if no header in file
(AVFMTCTX_NOHEADER) */
- ret = av_read_frame_internal(ic, &pkt1);
- if (ret < 0 && ret != AVERROR(EAGAIN)) {
+ ret = read_frame_internal(ic, &pkt1);
+ if (ret == AVERROR(EAGAIN))
+ continue;
+
+ if (ret < 0) {
/* EOF or error */
ret = -1; /* we could not have all the codec parameters before EOF */
for(i=0;i<ic->nb_streams;i++) {
break;
}
- if (ret == AVERROR(EAGAIN))
- continue;
-
pkt= add_to_pktbuf(&ic->packet_buffer, &pkt1, &ic->packet_buffer_end);
if ((ret = av_dup_packet(pkt)) < 0)
goto find_stream_info_err;
}
{
int64_t last = st->info->last_dts;
- int64_t duration= pkt->dts - last;
- if(pkt->dts != AV_NOPTS_VALUE && last != AV_NOPTS_VALUE && duration>0){
+ 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->codec->codec_type == AVMEDIA_TYPE_VIDEO)
/* if still no information, we try to open the codec and to
decompress the frame. We try to avoid that in most cases as
it takes longer and uses more memory. For MPEG-4, we need to
- decompress for QuickTime. */
- if (!has_codec_parameters(st->codec) || !has_decode_delay_been_guessed(st))
- try_decode_frame(st, pkt);
+ decompress for QuickTime.
+
+ If CODEC_CAP_CHANNEL_CONF is set this will force decoding of at
+ least one frame of codec data, this makes sure the codec initializes
+ the channel configuration and does not only trust the values from the container.
+ */
+ try_decode_frame(st, pkt, (options && i < orig_nb_streams )? &options[i] : NULL);
st->codec_info_nb_frames++;
count++;
(st->codec_info_nb_frames-2)*(int64_t)st->time_base.den,
st->info->codec_info_duration*(int64_t)st->time_base.num, 60000);
if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
- if(st->codec->codec_id == CODEC_ID_RAWVIDEO && !st->codec->codec_tag && !st->codec->bits_per_coded_sample)
- st->codec->codec_tag= avcodec_pix_fmt_to_codec_tag(st->codec->pix_fmt);
-
// 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.
}
}
- av_estimate_timings(ic, old_offset);
+ estimate_timings(ic, old_offset);
compute_chapters_end(ic);
avio_close(pb);
}
+#if FF_API_NEW_STREAM
AVStream *av_new_stream(AVFormatContext *s, int id)
+{
+ AVStream *st = avformat_new_stream(s, NULL);
+ if (st)
+ st->id = id;
+ return st;
+}
+#endif
+
+AVStream *avformat_new_stream(AVFormatContext *s, AVCodec *c)
{
AVStream *st;
int i;
return NULL;
}
- st->codec= avcodec_alloc_context();
+ st->codec = avcodec_alloc_context3(c);
if (s->iformat) {
/* no default bitrate if decoding */
st->codec->bit_rate = 0;
}
st->index = s->nb_streams;
- st->id = id;
st->start_time = AV_NOPTS_VALUE;
st->duration = AV_NOPTS_VALUE;
/* we set the current DTS to 0 so that formats without any timestamps
return program;
}
-AVChapter *ff_new_chapter(AVFormatContext *s, int id, AVRational time_base, int64_t start, int64_t end, const char *title)
+AVChapter *avpriv_new_chapter(AVFormatContext *s, int id, AVRational time_base, int64_t start, int64_t end, const char *title)
{
AVChapter *chapter = NULL;
int i;
/************************************************************/
/* output media file */
+#if FF_API_FORMAT_PARAMETERS
int av_set_parameters(AVFormatContext *s, AVFormatParameters *ap)
{
int ret;
}
return 0;
}
+#endif
static int validate_codec_tag(AVFormatContext *s, AVStream *st)
{
return 1;
}
+#if FF_API_FORMAT_PARAMETERS
int av_write_header(AVFormatContext *s)
{
- int ret, i;
+ return avformat_write_header(s, NULL);
+}
+#endif
+
+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");
- return AVERROR(EINVAL);
+ ret = AVERROR(EINVAL);
+ goto fail;
}
for(i=0;i<s->nb_streams;i++) {
case AVMEDIA_TYPE_AUDIO:
if(st->codec->sample_rate<=0){
av_log(s, AV_LOG_ERROR, "sample rate not set\n");
- return AVERROR(EINVAL);
+ ret = AVERROR(EINVAL);
+ goto fail;
}
if(!st->codec->block_align)
st->codec->block_align = st->codec->channels *
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");
- return AVERROR(EINVAL);
+ 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");
- return AVERROR(EINVAL);
+ 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 encoder and muxer layer\n");
- return AVERROR(EINVAL);
+ ret = AVERROR(EINVAL);
+ goto fail;
}
break;
}
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);
- return AVERROR_INVALIDDATA;
+ ret = AVERROR_INVALIDDATA;
+ goto fail;
}
}else
st->codec->codec_tag= av_codec_get_tag(s->oformat->codec_tag, st->codec->codec_id);
if (!s->priv_data && s->oformat->priv_data_size > 0) {
s->priv_data = av_mallocz(s->oformat->priv_data_size);
- if (!s->priv_data)
- return AVERROR(ENOMEM);
+ 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->oformat->write_header){
ret = s->oformat->write_header(s);
if (ret < 0)
- return ret;
+ goto fail;
}
/* init PTS generation */
break;
}
if (den != AV_NOPTS_VALUE) {
- if (den <= 0)
- return AVERROR_INVALIDDATA;
- av_frac_init(&st->pts, 0, 0, den);
+ 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
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)) {
- av_frac_add(&st->pts, (int64_t)st->time_base.den * frame_size);
+ frac_add(&st->pts, (int64_t)st->time_base.den * frame_size);
}
break;
case AVMEDIA_TYPE_VIDEO:
- av_frac_add(&st->pts, (int64_t)st->time_base.den * st->codec->time_base.num);
+ frac_add(&st->pts, (int64_t)st->time_base.den * st->codec->time_base.num);
break;
default:
break;
return ret;
ret= s->oformat->write_packet(s, pkt);
+
+ if (ret >= 0)
+ s->streams[pkt->stream_index]->nb_frames++;
return ret;
}
* @return 1 if a packet was output, 0 if no packet could be output,
* < 0 if an error occurred
*/
-static int av_interleave_packet(AVFormatContext *s, AVPacket *out, AVPacket *in, int flush){
+static int interleave_packet(AVFormatContext *s, AVPacket *out, AVPacket *in, int flush){
if(s->oformat->interleave_packet)
return s->oformat->interleave_packet(s, out, in, flush);
else
for(;;){
AVPacket opkt;
- int ret= av_interleave_packet(s, &opkt, pkt, 0);
+ int ret= interleave_packet(s, &opkt, pkt, 0);
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;
for(;;){
AVPacket pkt;
- ret= av_interleave_packet(s, &pkt, NULL, 1);
+ 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);
int is_output)
{
int i;
- uint8_t *printed = av_mallocz(ic->nb_streams);
+ uint8_t *printed = ic->nb_streams ? av_mallocz(ic->nb_streams) : NULL;
if (ic->nb_streams && !printed)
return;
}
av_strlcat(buf, rel, size);
}
+
+int64_t ff_iso8601_to_unix_time(const char *datestr)
+{
+#if HAVE_STRPTIME
+ struct tm time = {0};
+ strptime(datestr, "%Y - %m - %dT%T", &time);
+ return mktime(&time);
+#else
+ av_log(NULL, AV_LOG_WARNING, "strptime() unavailable on this system, cannot convert "
+ "the date string.\n");
+ return 0;
+#endif
+}
+
+int avformat_query_codec(AVOutputFormat *ofmt, enum CodecID codec_id, int std_compliance)
+{
+ if (ofmt) {
+ if (ofmt->query_codec)
+ return ofmt->query_codec(codec_id, std_compliance);
+ else if (ofmt->codec_tag)
+ return !!av_codec_get_tag(ofmt->codec_tag, codec_id);
+ else if (codec_id == ofmt->video_codec || codec_id == ofmt->audio_codec ||
+ codec_id == ofmt->subtitle_codec)
+ return 1;
+ }
+ return AVERROR_PATCHWELCOME;
+}