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