X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavformat%2Fr3d.c;h=bcea4623061934c2f759dde4eee79781b50bb37d;hb=c201069fac9a76e6604f9d84d76a172434d62200;hp=73e73986ffb2c2f8704ec89d858bdabdc4732d50;hpb=c3f9ebf74371b63fba0e7491e61904bbd165cd0f;p=ffmpeg diff --git a/libavformat/r3d.c b/libavformat/r3d.c index 73e73986ffb..bcea4623061 100644 --- a/libavformat/r3d.c +++ b/libavformat/r3d.c @@ -19,21 +19,19 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -//#define DEBUG - #include "libavutil/intreadwrite.h" #include "libavutil/dict.h" #include "libavutil/mathematics.h" #include "avformat.h" #include "internal.h" -typedef struct { +typedef struct R3DContext { unsigned video_offsets_count; unsigned *video_offsets; unsigned rdvo_offset; } R3DContext; -typedef struct { +typedef struct Atom { unsigned size; uint32_t tag; uint64_t offset; @@ -57,11 +55,12 @@ static int r3d_read_red1(AVFormatContext *s) char filename[258]; int tmp; int av_unused tmp2; + AVRational framerate; if (!st) return AVERROR(ENOMEM); st->codec->codec_type = AVMEDIA_TYPE_VIDEO; - st->codec->codec_id = CODEC_ID_JPEG2000; + st->codec->codec_id = AV_CODEC_ID_JPEG2000; tmp = avio_r8(s->pb); // major version tmp2 = avio_r8(s->pb); // minor version @@ -84,8 +83,11 @@ static int r3d_read_red1(AVFormatContext *s) tmp = avio_rb16(s->pb); // unknown av_dlog(s, "unknown2 %d\n", tmp); - st->codec->time_base.den = avio_rb16(s->pb); - st->codec->time_base.num = avio_rb16(s->pb); + framerate.num = avio_rb16(s->pb); + framerate.den = avio_rb16(s->pb); + if (framerate.num > 0 && framerate.den > 0) { + st->avg_frame_rate = framerate; + } tmp = avio_r8(s->pb); // audio channels av_dlog(s, "audio channels %d\n", tmp); @@ -94,7 +96,7 @@ static int r3d_read_red1(AVFormatContext *s) if (!ast) return AVERROR(ENOMEM); ast->codec->codec_type = AVMEDIA_TYPE_AUDIO; - ast->codec->codec_id = CODEC_ID_PCM_S32BE; + ast->codec->codec_id = AV_CODEC_ID_PCM_S32BE; ast->codec->channels = tmp; avpriv_set_pts_info(ast, 32, 1, st->time_base.den); } @@ -107,7 +109,7 @@ static int r3d_read_red1(AVFormatContext *s) av_dlog(s, "resolution %dx%d\n", st->codec->width, st->codec->height); av_dlog(s, "timescale %d\n", st->time_base.den); av_dlog(s, "frame rate %d/%d\n", - st->codec->time_base.num, st->codec->time_base.den); + framerate.num, framerate.den); return 0; } @@ -132,9 +134,11 @@ static int r3d_read_rdvo(AVFormatContext *s, Atom *atom) av_dlog(s, "video offset %d: %#x\n", i, r3d->video_offsets[i]); } - if (st->codec->time_base.den) - st->duration = (uint64_t)r3d->video_offsets_count* - st->time_base.den*st->codec->time_base.num/st->codec->time_base.den; + if (st->avg_frame_rate.num) + st->duration = av_rescale_q(r3d->video_offsets_count, + (AVRational){st->avg_frame_rate.den, + st->avg_frame_rate.num}, + st->time_base); av_dlog(s, "duration %"PRId64"\n", st->duration); return 0; @@ -159,7 +163,7 @@ static void r3d_read_reos(AVFormatContext *s) avio_skip(s->pb, 6*4); } -static int r3d_read_header(AVFormatContext *s, AVFormatParameters *ap) +static int r3d_read_header(AVFormatContext *s) { R3DContext *r3d = s->priv_data; Atom atom; @@ -179,8 +183,8 @@ static int r3d_read_header(AVFormatContext *s, AVFormatParameters *ap) return -1; } - s->data_offset = avio_tell(s->pb); - av_dlog(s, "data offset %#"PRIx64"\n", s->data_offset); + s->internal->data_offset = avio_tell(s->pb); + av_dlog(s, "data offset %#"PRIx64"\n", s->internal->data_offset); if (!s->pb->seekable) return 0; // find REOB/REOF/REOS to load index @@ -206,7 +210,7 @@ static int r3d_read_header(AVFormatContext *s, AVFormatParameters *ap) } out: - avio_seek(s->pb, s->data_offset, SEEK_SET); + avio_seek(s->pb, s->internal->data_offset, SEEK_SET); return 0; } @@ -215,7 +219,7 @@ static int r3d_read_redv(AVFormatContext *s, AVPacket *pkt, Atom *atom) AVStream *st = s->streams[0]; int tmp; int av_unused tmp2; - uint64_t pos = avio_tell(s->pb); + int64_t pos = avio_tell(s->pb); unsigned dts; int ret; @@ -257,9 +261,9 @@ static int r3d_read_redv(AVFormatContext *s, AVPacket *pkt, Atom *atom) pkt->stream_index = 0; pkt->dts = dts; - if (st->codec->time_base.den) + if (st->avg_frame_rate.num) pkt->duration = (uint64_t)st->time_base.den* - st->codec->time_base.num/st->codec->time_base.den; + st->avg_frame_rate.den/st->avg_frame_rate.num; av_dlog(s, "pkt dts %"PRId64" duration %d\n", pkt->dts, pkt->duration); return 0; @@ -270,20 +274,24 @@ static int r3d_read_reda(AVFormatContext *s, AVPacket *pkt, Atom *atom) AVStream *st = s->streams[1]; int av_unused tmp, tmp2; int samples, size; - uint64_t pos = avio_tell(s->pb); + int64_t pos = avio_tell(s->pb); unsigned dts; int ret; dts = avio_rb32(s->pb); st->codec->sample_rate = avio_rb32(s->pb); + if (st->codec->sample_rate <= 0) { + av_log(s, AV_LOG_ERROR, "Bad sample rate\n"); + return AVERROR_INVALIDDATA; + } samples = avio_rb32(s->pb); tmp = avio_rb32(s->pb); av_dlog(s, "packet num %d\n", tmp); - tmp = avio_rb16(s->pb); // unkown + tmp = avio_rb16(s->pb); // unknown av_dlog(s, "unknown %d\n", tmp); tmp = avio_r8(s->pb); // major version @@ -357,16 +365,17 @@ static int r3d_seek(AVFormatContext *s, int stream_index, int64_t sample_time, i R3DContext *r3d = s->priv_data; int frame_num; - if (!st->codec->time_base.num || !st->time_base.den) + if (!st->avg_frame_rate.num) return -1; - frame_num = sample_time*st->codec->time_base.den/ - ((int64_t)st->codec->time_base.num*st->time_base.den); + frame_num = av_rescale_q(sample_time, st->time_base, + (AVRational){st->avg_frame_rate.den, st->avg_frame_rate.num}); av_dlog(s, "seek frame num %d timestamp %"PRId64"\n", frame_num, sample_time); if (frame_num < r3d->video_offsets_count) { - avio_seek(s->pb, r3d->video_offsets_count, SEEK_SET); + if (avio_seek(s->pb, r3d->video_offsets_count, SEEK_SET) < 0) + return -1; } else { av_log(s, AV_LOG_ERROR, "could not seek to frame %d\n", frame_num); return -1; @@ -386,7 +395,7 @@ static int r3d_close(AVFormatContext *s) AVInputFormat ff_r3d_demuxer = { .name = "r3d", - .long_name = NULL_IF_CONFIG_SMALL("REDCODE R3D format"), + .long_name = NULL_IF_CONFIG_SMALL("REDCODE R3D"), .priv_data_size = sizeof(R3DContext), .read_probe = r3d_probe, .read_header = r3d_read_header,