00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "libavutil/intreadwrite.h"
00025 #include "libavutil/dict.h"
00026 #include "avformat.h"
00027
00028 typedef struct {
00029 unsigned video_offsets_count;
00030 unsigned *video_offsets;
00031 unsigned rdvo_offset;
00032 } R3DContext;
00033
00034 typedef struct {
00035 unsigned size;
00036 uint32_t tag;
00037 uint64_t offset;
00038 } Atom;
00039
00040 static int read_atom(AVFormatContext *s, Atom *atom)
00041 {
00042 atom->offset = avio_tell(s->pb);
00043 atom->size = avio_rb32(s->pb);
00044 if (atom->size < 8)
00045 return -1;
00046 atom->tag = avio_rl32(s->pb);
00047 av_dlog(s, "atom %u %.4s offset %#"PRIx64"\n",
00048 atom->size, (char*)&atom->tag, atom->offset);
00049 return atom->size;
00050 }
00051
00052 static int r3d_read_red1(AVFormatContext *s)
00053 {
00054 AVStream *st = av_new_stream(s, 0);
00055 char filename[258];
00056 int tmp;
00057 int av_unused tmp2;
00058
00059 if (!st)
00060 return AVERROR(ENOMEM);
00061 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00062 st->codec->codec_id = CODEC_ID_JPEG2000;
00063
00064 tmp = avio_r8(s->pb);
00065 tmp2 = avio_r8(s->pb);
00066 av_dlog(s, "version %d.%d\n", tmp, tmp2);
00067
00068 tmp = avio_rb16(s->pb);
00069 av_dlog(s, "unknown1 %d\n", tmp);
00070
00071 tmp = avio_rb32(s->pb);
00072 av_set_pts_info(st, 32, 1, tmp);
00073
00074 tmp = avio_rb32(s->pb);
00075 av_dlog(s, "filenum %d\n", tmp);
00076
00077 avio_skip(s->pb, 32);
00078
00079 st->codec->width = avio_rb32(s->pb);
00080 st->codec->height = avio_rb32(s->pb);
00081
00082 tmp = avio_rb16(s->pb);
00083 av_dlog(s, "unknown2 %d\n", tmp);
00084
00085 st->codec->time_base.den = avio_rb16(s->pb);
00086 st->codec->time_base.num = avio_rb16(s->pb);
00087
00088 tmp = avio_r8(s->pb);
00089 av_dlog(s, "audio channels %d\n", tmp);
00090 if (tmp > 0) {
00091 AVStream *ast = av_new_stream(s, 1);
00092 if (!ast)
00093 return AVERROR(ENOMEM);
00094 ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00095 ast->codec->codec_id = CODEC_ID_PCM_S32BE;
00096 ast->codec->channels = tmp;
00097 av_set_pts_info(ast, 32, 1, st->time_base.den);
00098 }
00099
00100 avio_read(s->pb, filename, 257);
00101 filename[sizeof(filename)-1] = 0;
00102 av_dict_set(&st->metadata, "filename", filename, 0);
00103
00104 av_dlog(s, "filename %s\n", filename);
00105 av_dlog(s, "resolution %dx%d\n", st->codec->width, st->codec->height);
00106 av_dlog(s, "timescale %d\n", st->time_base.den);
00107 av_dlog(s, "frame rate %d/%d\n",
00108 st->codec->time_base.num, st->codec->time_base.den);
00109
00110 return 0;
00111 }
00112
00113 static int r3d_read_rdvo(AVFormatContext *s, Atom *atom)
00114 {
00115 R3DContext *r3d = s->priv_data;
00116 AVStream *st = s->streams[0];
00117 int i;
00118
00119 r3d->video_offsets_count = (atom->size - 8) / 4;
00120 r3d->video_offsets = av_malloc(atom->size);
00121 if (!r3d->video_offsets)
00122 return AVERROR(ENOMEM);
00123
00124 for (i = 0; i < r3d->video_offsets_count; i++) {
00125 r3d->video_offsets[i] = avio_rb32(s->pb);
00126 if (!r3d->video_offsets[i]) {
00127 r3d->video_offsets_count = i;
00128 break;
00129 }
00130 av_dlog(s, "video offset %d: %#x\n", i, r3d->video_offsets[i]);
00131 }
00132
00133 if (st->codec->time_base.den)
00134 st->duration = (uint64_t)r3d->video_offsets_count*
00135 st->time_base.den*st->codec->time_base.num/st->codec->time_base.den;
00136 av_dlog(s, "duration %"PRId64"\n", st->duration);
00137
00138 return 0;
00139 }
00140
00141 static void r3d_read_reos(AVFormatContext *s)
00142 {
00143 R3DContext *r3d = s->priv_data;
00144 int av_unused tmp;
00145
00146 r3d->rdvo_offset = avio_rb32(s->pb);
00147 avio_rb32(s->pb);
00148 avio_rb32(s->pb);
00149 avio_rb32(s->pb);
00150
00151 tmp = avio_rb32(s->pb);
00152 av_dlog(s, "num video chunks %d\n", tmp);
00153
00154 tmp = avio_rb32(s->pb);
00155 av_dlog(s, "num audio chunks %d\n", tmp);
00156
00157 avio_skip(s->pb, 6*4);
00158 }
00159
00160 static int r3d_read_header(AVFormatContext *s, AVFormatParameters *ap)
00161 {
00162 R3DContext *r3d = s->priv_data;
00163 Atom atom;
00164 int ret;
00165
00166 if (read_atom(s, &atom) < 0) {
00167 av_log(s, AV_LOG_ERROR, "error reading atom\n");
00168 return -1;
00169 }
00170 if (atom.tag == MKTAG('R','E','D','1')) {
00171 if ((ret = r3d_read_red1(s)) < 0) {
00172 av_log(s, AV_LOG_ERROR, "error parsing 'red1' atom\n");
00173 return ret;
00174 }
00175 } else {
00176 av_log(s, AV_LOG_ERROR, "could not find 'red1' atom\n");
00177 return -1;
00178 }
00179
00180 s->data_offset = avio_tell(s->pb);
00181 av_dlog(s, "data offset %#"PRIx64"\n", s->data_offset);
00182 if (!s->pb->seekable)
00183 return 0;
00184
00185 avio_seek(s->pb, avio_size(s->pb)-48-8, SEEK_SET);
00186 if (read_atom(s, &atom) < 0)
00187 av_log(s, AV_LOG_ERROR, "error reading end atom\n");
00188
00189 if (atom.tag != MKTAG('R','E','O','B') &&
00190 atom.tag != MKTAG('R','E','O','F') &&
00191 atom.tag != MKTAG('R','E','O','S'))
00192 goto out;
00193
00194 r3d_read_reos(s);
00195
00196 if (r3d->rdvo_offset) {
00197 avio_seek(s->pb, r3d->rdvo_offset, SEEK_SET);
00198 if (read_atom(s, &atom) < 0)
00199 av_log(s, AV_LOG_ERROR, "error reading 'rdvo' atom\n");
00200 if (atom.tag == MKTAG('R','D','V','O')) {
00201 if (r3d_read_rdvo(s, &atom) < 0)
00202 av_log(s, AV_LOG_ERROR, "error parsing 'rdvo' atom\n");
00203 }
00204 }
00205
00206 out:
00207 avio_seek(s->pb, s->data_offset, SEEK_SET);
00208 return 0;
00209 }
00210
00211 static int r3d_read_redv(AVFormatContext *s, AVPacket *pkt, Atom *atom)
00212 {
00213 AVStream *st = s->streams[0];
00214 int tmp;
00215 int av_unused tmp2;
00216 uint64_t pos = avio_tell(s->pb);
00217 unsigned dts;
00218 int ret;
00219
00220 dts = avio_rb32(s->pb);
00221
00222 tmp = avio_rb32(s->pb);
00223 av_dlog(s, "frame num %d\n", tmp);
00224
00225 tmp = avio_r8(s->pb);
00226 tmp2 = avio_r8(s->pb);
00227 av_dlog(s, "version %d.%d\n", tmp, tmp2);
00228
00229 tmp = avio_rb16(s->pb);
00230 av_dlog(s, "unknown %d\n", tmp);
00231
00232 if (tmp > 4) {
00233 tmp = avio_rb16(s->pb);
00234 av_dlog(s, "unknown %d\n", tmp);
00235
00236 tmp = avio_rb16(s->pb);
00237 av_dlog(s, "unknown %d\n", tmp);
00238
00239 tmp = avio_rb32(s->pb);
00240 av_dlog(s, "width %d\n", tmp);
00241 tmp = avio_rb32(s->pb);
00242 av_dlog(s, "height %d\n", tmp);
00243
00244 tmp = avio_rb32(s->pb);
00245 av_dlog(s, "metadata len %d\n", tmp);
00246 }
00247 tmp = atom->size - 8 - (avio_tell(s->pb) - pos);
00248 if (tmp < 0)
00249 return -1;
00250 ret = av_get_packet(s->pb, pkt, tmp);
00251 if (ret < 0) {
00252 av_log(s, AV_LOG_ERROR, "error reading video packet\n");
00253 return -1;
00254 }
00255
00256 pkt->stream_index = 0;
00257 pkt->dts = dts;
00258 if (st->codec->time_base.den)
00259 pkt->duration = (uint64_t)st->time_base.den*
00260 st->codec->time_base.num/st->codec->time_base.den;
00261 av_dlog(s, "pkt dts %"PRId64" duration %d\n", pkt->dts, pkt->duration);
00262
00263 return 0;
00264 }
00265
00266 static int r3d_read_reda(AVFormatContext *s, AVPacket *pkt, Atom *atom)
00267 {
00268 AVStream *st = s->streams[1];
00269 int av_unused tmp, tmp2;
00270 int samples, size;
00271 uint64_t pos = avio_tell(s->pb);
00272 unsigned dts;
00273 int ret;
00274
00275 dts = avio_rb32(s->pb);
00276
00277 st->codec->sample_rate = avio_rb32(s->pb);
00278
00279 samples = avio_rb32(s->pb);
00280
00281 tmp = avio_rb32(s->pb);
00282 av_dlog(s, "packet num %d\n", tmp);
00283
00284 tmp = avio_rb16(s->pb);
00285 av_dlog(s, "unknown %d\n", tmp);
00286
00287 tmp = avio_r8(s->pb);
00288 tmp2 = avio_r8(s->pb);
00289 av_dlog(s, "version %d.%d\n", tmp, tmp2);
00290
00291 tmp = avio_rb32(s->pb);
00292 av_dlog(s, "unknown %d\n", tmp);
00293
00294 size = atom->size - 8 - (avio_tell(s->pb) - pos);
00295 if (size < 0)
00296 return -1;
00297 ret = av_get_packet(s->pb, pkt, size);
00298 if (ret < 0) {
00299 av_log(s, AV_LOG_ERROR, "error reading audio packet\n");
00300 return ret;
00301 }
00302
00303 pkt->stream_index = 1;
00304 pkt->dts = dts;
00305 pkt->duration = av_rescale(samples, st->time_base.den, st->codec->sample_rate);
00306 av_dlog(s, "pkt dts %"PRId64" duration %d samples %d sample rate %d\n",
00307 pkt->dts, pkt->duration, samples, st->codec->sample_rate);
00308
00309 return 0;
00310 }
00311
00312 static int r3d_read_packet(AVFormatContext *s, AVPacket *pkt)
00313 {
00314 Atom atom;
00315 int err = 0;
00316
00317 while (!err) {
00318 if (read_atom(s, &atom) < 0) {
00319 err = -1;
00320 break;
00321 }
00322 switch (atom.tag) {
00323 case MKTAG('R','E','D','V'):
00324 if (s->streams[0]->discard == AVDISCARD_ALL)
00325 goto skip;
00326 if (!(err = r3d_read_redv(s, pkt, &atom)))
00327 return 0;
00328 break;
00329 case MKTAG('R','E','D','A'):
00330 if (s->nb_streams < 2)
00331 return -1;
00332 if (s->streams[1]->discard == AVDISCARD_ALL)
00333 goto skip;
00334 if (!(err = r3d_read_reda(s, pkt, &atom)))
00335 return 0;
00336 break;
00337 default:
00338 skip:
00339 avio_skip(s->pb, atom.size-8);
00340 }
00341 }
00342 return err;
00343 }
00344
00345 static int r3d_probe(AVProbeData *p)
00346 {
00347 if (AV_RL32(p->buf + 4) == MKTAG('R','E','D','1'))
00348 return AVPROBE_SCORE_MAX;
00349 return 0;
00350 }
00351
00352 static int r3d_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
00353 {
00354 AVStream *st = s->streams[0];
00355 R3DContext *r3d = s->priv_data;
00356 int frame_num;
00357
00358 if (!st->codec->time_base.num || !st->time_base.den)
00359 return -1;
00360
00361 frame_num = sample_time*st->codec->time_base.den/
00362 ((int64_t)st->codec->time_base.num*st->time_base.den);
00363 av_dlog(s, "seek frame num %d timestamp %"PRId64"\n",
00364 frame_num, sample_time);
00365
00366 if (frame_num < r3d->video_offsets_count) {
00367 avio_seek(s->pb, r3d->video_offsets_count, SEEK_SET);
00368 } else {
00369 av_log(s, AV_LOG_ERROR, "could not seek to frame %d\n", frame_num);
00370 return -1;
00371 }
00372
00373 return 0;
00374 }
00375
00376 static int r3d_close(AVFormatContext *s)
00377 {
00378 R3DContext *r3d = s->priv_data;
00379
00380 av_freep(&r3d->video_offsets);
00381
00382 return 0;
00383 }
00384
00385 AVInputFormat ff_r3d_demuxer = {
00386 "r3d",
00387 NULL_IF_CONFIG_SMALL("REDCODE R3D format"),
00388 sizeof(R3DContext),
00389 r3d_probe,
00390 r3d_read_header,
00391 r3d_read_packet,
00392 r3d_close,
00393 r3d_seek,
00394 };