#include "avformat.h"
#include "allformats.h"
#include "opt.h"
+#include "avstring.h"
+#include "riff.h"
+#include <sys/time.h>
+#include <time.h>
#undef NDEBUG
#include <assert.h>
pkt->data = NULL; pkt->size = 0;
}
+void av_init_packet(AVPacket *pkt)
+{
+ pkt->pts = AV_NOPTS_VALUE;
+ pkt->dts = AV_NOPTS_VALUE;
+ pkt->pos = -1;
+ pkt->duration = 0;
+ pkt->flags = 0;
+ pkt->stream_index = 0;
+ pkt->destruct= av_destruct_packet_nofree;
+}
+
int av_new_packet(AVPacket *pkt, int size)
{
uint8_t *data;
if((unsigned)size > (unsigned)size + FF_INPUT_BUFFER_PADDING_SIZE)
- return AVERROR_NOMEM;
+ return AVERROR(ENOMEM);
data = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
if (!data)
- return AVERROR_NOMEM;
+ return AVERROR(ENOMEM);
memset(data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
av_init_packet(pkt);
/* we duplicate the packet and don't forget to put the padding
again */
if((unsigned)pkt->size > (unsigned)pkt->size + FF_INPUT_BUFFER_PADDING_SIZE)
- return AVERROR_NOMEM;
+ return AVERROR(ENOMEM);
data = av_malloc(pkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
if (!data) {
- return AVERROR_NOMEM;
+ return AVERROR(ENOMEM);
}
memcpy(data, pkt->data, pkt->size);
memset(data + pkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
}
#define OFFSET(x) offsetof(AVFormatContext,x)
-#define DEFAULT 0 //should be NAN but it doesnt work as its not a constant in glibc as required by ANSI/ISO C
+#define DEFAULT 0 //should be NAN but it does not work as it is not a constant in glibc as required by ANSI/ISO C
//these names are too long to be readable
#define E AV_OPT_FLAG_ENCODING_PARAM
#define D AV_OPT_FLAG_DECODING_PARAM
else
ic = *ic_ptr;
if (!ic) {
- err = AVERROR_NOMEM;
+ err = AVERROR(ENOMEM);
goto fail;
}
ic->iformat = fmt;
ic->pb = *pb;
ic->duration = AV_NOPTS_VALUE;
ic->start_time = AV_NOPTS_VALUE;
- pstrcpy(ic->filename, sizeof(ic->filename), filename);
+ 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_NOMEM;
+ err = AVERROR(ENOMEM);
goto fail;
}
} else {
must_open_file = 1;
if (fmt && (fmt->flags & AVFMT_NOFILE)) {
must_open_file = 0;
- pb= NULL; //FIXME this or memset(pb, 0, sizeof(ByteIOContext)); otherwise its uninitalized
+ pb= NULL; //FIXME this or memset(pb, 0, sizeof(ByteIOContext)); otherwise it is uninitialized
}
if (!fmt || must_open_file) {
url_fclose(pb);
if (url_fopen(pb, filename, URL_RDONLY) < 0) {
file_opened = 0;
- err = AVERROR_IO;
+ err = AVERROR(EIO);
goto fail;
}
}
return 0;
}
-static int64_t lsb2full(int64_t lsb, int64_t last_ts, int lsb_bits){
- int64_t mask = lsb_bits < 64 ? (1LL<<lsb_bits)-1 : -1LL;
- int64_t delta= last_ts - mask/2;
- return ((lsb - delta)&mask) + delta;
+static void update_initial_timestamps(AVFormatContext *s, int stream_index, int64_t dts){
+ AVStream *st= s->streams[stream_index];
+ AVPacketList *pktl= s->packet_buffer;
+
+ if(st->first_dts != AV_NOPTS_VALUE || dts == AV_NOPTS_VALUE)
+ return;
+
+ st->first_dts= dts - st->cur_dts;
+ st->cur_dts= dts;
+
+ for(; pktl; pktl= pktl->next){
+ if(pktl->pkt.stream_index != stream_index)
+ continue;
+ //FIXME think more about this check
+ if(pktl->pkt.pts != AV_NOPTS_VALUE && pktl->pkt.pts == pktl->pkt.dts)
+ pktl->pkt.pts += st->first_dts;
+
+ if(pktl->pkt.dts != AV_NOPTS_VALUE)
+ pktl->pkt.dts += st->first_dts;
+
+ if(st->start_time == AV_NOPTS_VALUE && pktl->pkt.pts != AV_NOPTS_VALUE)
+ st->start_time= pktl->pkt.pts;
+ }
}
static void compute_pkt_fields(AVFormatContext *s, AVStream *st,
{
int num, den, presentation_delayed, delay, i;
int64_t offset;
- /* handle wrapping */
- if(st->cur_dts != AV_NOPTS_VALUE){
- if(pkt->pts != AV_NOPTS_VALUE)
- pkt->pts= lsb2full(pkt->pts, st->cur_dts, st->pts_wrap_bits);
- if(pkt->dts != AV_NOPTS_VALUE)
- pkt->dts= lsb2full(pkt->dts, st->cur_dts, st->pts_wrap_bits);
+
+ 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*/){
+ pkt->dts -= 1LL<<st->pts_wrap_bits;
}
if (pkt->duration == 0) {
if (delay &&
pc && pc->pict_type != FF_B_TYPE)
presentation_delayed = 1;
- /* this may be redundant, but it shouldnt hurt */
+ /* This may be redundant, but it should not hurt. */
if(pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE && pkt->pts > pkt->dts)
presentation_delayed = 1;
if(st->cur_dts == AV_NOPTS_VALUE){
- st->cur_dts = -delay * pkt->duration;
+ st->cur_dts = 0; //FIXME maybe set it to 0 during init
}
// 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);
/* PTS = presentation time stamp */
if (pkt->dts == AV_NOPTS_VALUE)
pkt->dts = st->last_IP_pts;
+ update_initial_timestamps(s, pkt->stream_index, pkt->dts);
if (pkt->dts == AV_NOPTS_VALUE)
pkt->dts = st->cur_dts;
/* presentation is not delayed : PTS and DTS are the same */
if(pkt->pts == AV_NOPTS_VALUE)
pkt->pts = pkt->dts;
+ update_initial_timestamps(s, pkt->stream_index, pkt->pts);
if(pkt->pts == AV_NOPTS_VALUE)
pkt->pts = st->cur_dts;
pkt->dts = pkt->pts;
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(delay>1){
+ update_initial_timestamps(s, pkt->stream_index, pkt->dts); // this should happen on the first packet
+ }
if(pkt->dts > st->cur_dts)
st->cur_dts = pkt->dts;
}
return 0;
}
+static AVPacket *add_to_pktbuf(AVFormatContext *s, AVPacket *pkt){
+ AVPacketList *pktl= s->packet_buffer;
+ AVPacketList **plast_pktl= &s->packet_buffer;
+
+ while(*plast_pktl) plast_pktl= &(*plast_pktl)->next; //FIXME maybe maintain pointer to the last?
+
+ pktl = av_mallocz(sizeof(AVPacketList));
+ if (!pktl)
+ return NULL;
+
+ /* add the packet in the buffered packet list */
+ *plast_pktl = pktl;
+ pktl->pkt= *pkt;
+ return &pktl->pkt;
+}
+
int av_read_frame(AVFormatContext *s, AVPacket *pkt)
{
AVPacketList *pktl;
}
}
if(genpts){
- AVPacketList **plast_pktl= &s->packet_buffer;
int ret= av_read_frame_internal(s, pkt);
if(ret<0){
if(pktl && ret != AVERROR(EAGAIN)){
return ret;
}
- /* duplicate the packet */
- if (av_dup_packet(pkt) < 0)
- return AVERROR_NOMEM;
-
- while(*plast_pktl) plast_pktl= &(*plast_pktl)->next; //FIXME maybe maintain pointer to the last?
-
- pktl = av_mallocz(sizeof(AVPacketList));
- if (!pktl)
- return AVERROR_NOMEM;
-
- /* add the packet in the buffered packet list */
- *plast_pktl = pktl;
- pktl->pkt= *pkt;
+ if(av_dup_packet(add_to_pktbuf(s, pkt)) < 0)
+ return AVERROR(ENOMEM);
}else{
assert(!s->packet_buffer);
return av_read_frame_internal(s, pkt);
return -1;
memmove(entries + index + 1, entries + index, sizeof(AVIndexEntry)*(st->nb_index_entries - index));
st->nb_index_entries++;
- }else if(ie->pos == pos && distance < ie->min_distance) //dont reduce the distance
+ }else if(ie->pos == pos && distance < ie->min_distance) //do not reduce the distance
distance= ie->min_distance;
}
return index;
}
-/**
- * build an index for raw streams using a parser.
- */
-static void av_build_index_raw(AVFormatContext *s)
-{
- AVPacket pkt1, *pkt = &pkt1;
- int ret;
- AVStream *st;
-
- st = s->streams[0];
- av_read_frame_flush(s);
- url_fseek(&s->pb, s->data_offset, SEEK_SET);
-
- for(;;) {
- ret = av_read_frame(s, pkt);
- if (ret < 0)
- break;
- if (pkt->stream_index == 0 && st->parser &&
- (pkt->flags & PKT_FLAG_KEY)) {
- av_add_index_entry(st, st->parser->frame_offset, pkt->dts,
- 0, 0, AVINDEX_KEYFRAME);
- }
- av_free_packet(pkt);
- }
-}
-
-/**
- * Returns TRUE if we deal with a raw stream.
- *
- * Raw codec data and parsing needed.
- */
-static int is_raw_stream(AVFormatContext *s)
-{
- AVStream *st;
-
- if (s->nb_streams != 1)
- return 0;
- st = s->streams[0];
- if (!st->need_parsing)
- return 0;
- return 1;
-}
-
int av_index_search_timestamp(AVStream *st, int64_t wanted_timestamp,
int flags)
{
ts_max=
ts_min= AV_NOPTS_VALUE;
- pos_limit= -1; //gcc falsely says it may be uninitalized
+ pos_limit= -1; //gcc falsely says it may be uninitialized
st= s->streams[stream_index];
if(st->index_entries){
return !!fmt;
}
+unsigned int codec_get_tag(const AVCodecTag *tags, int id)
+{
+ while (tags->id != CODEC_ID_NONE) {
+ if (tags->id == id)
+ return tags->tag;
+ tags++;
+ }
+ return 0;
+}
+
+enum CodecID codec_get_id(const AVCodecTag *tags, unsigned int tag)
+{
+ int i;
+ for(i=0; tags[i].id != CODEC_ID_NONE;i++) {
+ if(tag == tags[i].tag)
+ return tags[i].id;
+ }
+ for(i=0; tags[i].id != CODEC_ID_NONE; i++) {
+ if( toupper((tag >> 0)&0xFF) == toupper((tags[i].tag >> 0)&0xFF)
+ && toupper((tag >> 8)&0xFF) == toupper((tags[i].tag >> 8)&0xFF)
+ && toupper((tag >>16)&0xFF) == toupper((tags[i].tag >>16)&0xFF)
+ && toupper((tag >>24)&0xFF) == toupper((tags[i].tag >>24)&0xFF))
+ return tags[i].id;
+ }
+ return CODEC_ID_NONE;
+}
+
+unsigned int av_codec_get_tag(const AVCodecTag *tags[4], enum CodecID id)
+{
+ int i;
+ for(i=0; tags && tags[i]; i++){
+ int tag= codec_get_tag(tags[i], id);
+ if(tag) return tag;
+ }
+ return 0;
+}
+
+enum CodecID av_codec_get_id(const AVCodecTag *tags[4], unsigned int tag)
+{
+ int i;
+ for(i=0; tags && tags[i]; i++){
+ enum CodecID id= codec_get_id(tags[i], tag);
+ if(id!=CODEC_ID_NONE) return id;
+ }
+ return CODEC_ID_NONE;
+}
+
/* absolute maximum size we read until we abort */
#define MAX_READ_SIZE 5000000
int i, count, ret, read_size, j;
AVStream *st;
AVPacket pkt1, *pkt;
- AVPacketList *pktl=NULL, **ppktl;
int64_t last_dts[MAX_STREAMS];
int duration_count[MAX_STREAMS]={0};
double (*duration_error)[MAX_STD_TIMEBASES];
int codec_identified[MAX_STREAMS]={0};
duration_error = av_mallocz(MAX_STREAMS * sizeof(*duration_error));
- if (!duration_error) return AVERROR_NOMEM;
+ if (!duration_error) return AVERROR(ENOMEM);
for(i=0;i<ic->nb_streams;i++) {
st = ic->streams[i];
memset(probe_data, 0, sizeof(probe_data));
count = 0;
read_size = 0;
- ppktl = &ic->packet_buffer;
for(;;) {
/* check if one codec still needs to be handled */
for(i=0;i<ic->nb_streams;i++) {
if (st->codec->codec_type == CODEC_TYPE_AUDIO &&
st->codec->codec_id == CODEC_ID_NONE)
break;
+ if(st->first_dts == AV_NOPTS_VALUE)
+ break;
}
if (i == ic->nb_streams) {
/* NOTE: if the format has no header, then we need to read
break;
}
- pktl = av_mallocz(sizeof(AVPacketList));
- if (!pktl) {
- ret = AVERROR_NOMEM;
- break;
- }
-
- /* add the packet in the buffered packet list */
- *ppktl = pktl;
- ppktl = &pktl->next;
-
- pkt = &pktl->pkt;
- *pkt = pkt1;
-
- /* duplicate the packet */
- if (av_dup_packet(pkt) < 0) {
- ret = AVERROR_NOMEM;
- break;
- }
+ pkt= add_to_pktbuf(ic, &pkt1);
+ if(av_dup_packet(pkt) < 0)
+ return AVERROR(ENOMEM);
read_size += pkt->size;
(st->codec->codec_id == CODEC_ID_MPEG4 && !st->need_parsing))*/)
try_decode_frame(st, pkt->data, pkt->size);
- if (av_rescale_q(codec_info_duration[st->index], st->time_base, AV_TIME_BASE_Q) >= ic->max_analyze_duration) {
+ 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) {
break;
}
count++;
}
}
}else if(st->codec->codec_type == CODEC_TYPE_AUDIO) {
- if (st->codec->codec_id == CODEC_ID_NONE) {
- codec_identified[st->index] = set_codec_from_probe_data(st, &(probe_data[st->index]), 0);
+ if (st->codec->codec_id == CODEC_ID_NONE && probe_data[st->index].buf_size > 0) {
+ codec_identified[st->index] = set_codec_from_probe_data(st, &(probe_data[st->index]), 1);
if (codec_identified[st->index]) {
st->need_parsing = AVSTREAM_PARSE_FULL;
}
for(i=0;i<ic->nb_streams;i++) {
st = ic->streams[i];
- if (codec_identified[st->index]) {
- av_read_frame_flush(ic);
- av_seek_frame(ic, st->index, 0.0, 0);
- url_fseek(&ic->pb, ic->data_offset, SEEK_SET);
+ if (codec_identified[st->index])
+ break;
+ }
+ //FIXME this is a mess
+ if(i!=ic->nb_streams){
+ av_read_frame_flush(ic);
+ for(i=0;i<ic->nb_streams;i++) {
+ st = ic->streams[i];
+ if (codec_identified[st->index]) {
+ av_seek_frame(ic, st->index, 0.0, 0);
+ }
+ st->cur_dts= st->first_dts;
}
+ url_fseek(&ic->pb, ic->data_offset, SEEK_SET);
}
#if 0
int av_read_play(AVFormatContext *s)
{
if (!s->iformat->read_play)
- return AVERROR_NOTSUPP;
+ return AVERROR(ENOSYS);
return s->iformat->read_play(s);
}
int av_read_pause(AVFormatContext *s)
{
if (!s->iformat->read_pause)
- return AVERROR_NOTSUPP;
+ return AVERROR(ENOSYS);
return s->iformat->read_pause(s);
}
st->start_time = AV_NOPTS_VALUE;
st->duration = AV_NOPTS_VALUE;
st->cur_dts = AV_NOPTS_VALUE;
+ st->first_dts = AV_NOPTS_VALUE;
/* default pts settings is MPEG like */
av_set_pts_info(st, 33, 1, 90000);
if (s->oformat->priv_data_size > 0) {
s->priv_data = av_mallocz(s->oformat->priv_data_size);
if (!s->priv_data)
- return AVERROR_NOMEM;
+ return AVERROR(ENOMEM);
} else
s->priv_data = NULL;
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_NOMEM;
+ return AVERROR(ENOMEM);
}
if(s->oformat->write_header){
}
}
-typedef struct {
- const char *abv;
- int width, height;
- int frame_rate, frame_rate_base;
-} AbvEntry;
-
-static AbvEntry frame_abvs[] = {
- { "ntsc", 720, 480, 30000, 1001 },
- { "pal", 720, 576, 25, 1 },
- { "qntsc", 352, 240, 30000, 1001 }, /* VCD compliant ntsc */
- { "qpal", 352, 288, 25, 1 }, /* VCD compliant pal */
- { "sntsc", 640, 480, 30000, 1001 }, /* square pixel ntsc */
- { "spal", 768, 576, 25, 1 }, /* square pixel pal */
- { "film", 352, 240, 24, 1 },
- { "ntsc-film", 352, 240, 24000, 1001 },
- { "sqcif", 128, 96, 0, 0 },
- { "qcif", 176, 144, 0, 0 },
- { "cif", 352, 288, 0, 0 },
- { "4cif", 704, 576, 0, 0 },
- { "qqvga", 160, 120, 0, 0 },
- { "qvga", 320, 240, 0, 0 },
- { "vga", 640, 480, 0, 0 },
- { "svga", 800, 600, 0, 0 },
- { "xga", 1024, 768, 0, 0 },
- { "uxga", 1600,1200, 0, 0 },
- { "qxga", 2048,1536, 0, 0 },
- { "sxga", 1280,1024, 0, 0 },
- { "qsxga", 2560,2048, 0, 0 },
- { "hsxga", 5120,4096, 0, 0 },
- { "wvga", 852, 480, 0, 0 },
- { "wxga", 1366, 768, 0, 0 },
- { "wsxga", 1600,1024, 0, 0 },
- { "wuxga", 1920,1200, 0, 0 },
- { "woxga", 2560,1600, 0, 0 },
- { "wqsxga", 3200,2048, 0, 0 },
- { "wquxga", 3840,2400, 0, 0 },
- { "whsxga", 6400,4096, 0, 0 },
- { "whuxga", 7680,4800, 0, 0 },
- { "cga", 320, 200, 0, 0 },
- { "ega", 640, 350, 0, 0 },
- { "hd480", 852, 480, 0, 0 },
- { "hd720", 1280, 720, 0, 0 },
- { "hd1080", 1920,1080, 0, 0 },
-};
-
int parse_image_size(int *width_ptr, int *height_ptr, const char *str)
{
- int i;
- int n = sizeof(frame_abvs) / sizeof(AbvEntry);
- const char *p;
- int frame_width = 0, frame_height = 0;
+ return av_parse_video_frame_size(width_ptr, height_ptr, str);
+}
- for(i=0;i<n;i++) {
- if (!strcmp(frame_abvs[i].abv, str)) {
- frame_width = frame_abvs[i].width;
- frame_height = frame_abvs[i].height;
- break;
- }
- }
- if (i == n) {
- p = str;
- frame_width = strtol(p, (char **)&p, 10);
- if (*p)
- p++;
- frame_height = strtol(p, (char **)&p, 10);
- }
- if (frame_width <= 0 || frame_height <= 0)
- return -1;
- *width_ptr = frame_width;
- *height_ptr = frame_height;
- return 0;
+int parse_frame_rate(int *frame_rate_num, int *frame_rate_den, const char *arg)
+{
+ AVRational frame_rate;
+ int ret = av_parse_video_frame_rate(&frame_rate, arg);
+ *frame_rate_num= frame_rate.num;
+ *frame_rate_den= frame_rate.den;
+ return ret;
}
-int parse_frame_rate(int *frame_rate, int *frame_rate_base, const char *arg)
+/**
+ * gets the current time in micro seconds.
+ */
+int64_t av_gettime(void)
{
- int i;
- char* cp;
-
- /* First, we check our abbreviation table */
- for (i = 0; i < sizeof(frame_abvs)/sizeof(*frame_abvs); ++i)
- if (!strcmp(frame_abvs[i].abv, arg)) {
- *frame_rate = frame_abvs[i].frame_rate;
- *frame_rate_base = frame_abvs[i].frame_rate_base;
- return 0;
- }
-
- /* Then, we try to parse it as fraction */
- cp = strchr(arg, '/');
- if (!cp)
- cp = strchr(arg, ':');
- if (cp) {
- char* cpp;
- *frame_rate = strtol(arg, &cpp, 10);
- if (cpp != arg || cpp == cp)
- *frame_rate_base = strtol(cp+1, &cpp, 10);
- else
- *frame_rate = 0;
- }
- else {
- /* Finally we give up and parse it as double */
- AVRational time_base = av_d2q(strtod(arg, 0), DEFAULT_FRAME_RATE_BASE);
- *frame_rate_base = time_base.den;
- *frame_rate = time_base.num;
- }
- if (!*frame_rate || !*frame_rate_base)
- return -1;
- else
- return 0;
+ struct timeval tv;
+ gettimeofday(&tv,NULL);
+ return (int64_t)tv.tv_sec * 1000000 + tv.tv_usec;
}
int64_t parse_date(const char *datestr, int duration)
}
if (port_ptr)
*port_ptr = port;
- pstrcpy(path, path_size, p);
+ av_strlcpy(path, p, path_size);
}
void av_set_pts_info(AVStream *s, int pts_wrap_bits,