1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
|
/*
* Magic Lantern Video (MLV) demuxer
* Copyright (c) 2014 Peter Ross
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file
* Magic Lantern Video (MLV) demuxer
*/
#include <time.h>
#include "libavcodec/bytestream.h"
#include "libavcodec/tiff.h"
#include "libavcodec/tiff_common.h"
#include "libavutil/imgutils.h"
#include "libavutil/intreadwrite.h"
#include "libavutil/mem.h"
#include "libavutil/rational.h"
#include "avformat.h"
#include "demux.h"
#include "internal.h"
#include "riff.h"
#define MLV_VERSION "v2.0"
#define MLV_VIDEO_CLASS_RAW 1
#define MLV_VIDEO_CLASS_YUV 2
#define MLV_VIDEO_CLASS_JPEG 3
#define MLV_VIDEO_CLASS_H264 4
#define MLV_AUDIO_CLASS_WAV 1
#define MLV_CLASS_FLAG_LJ92 0x20
#define MLV_CLASS_FLAG_DELTA 0x40
#define MLV_CLASS_FLAG_LZMA 0x80
typedef struct {
AVIOContext *pb[101];
int class[2];
int stream_index;
uint64_t pts;
int black_level;
int white_level;
int color_matrix1[9][2];
} MlvContext;
static int probe(const AVProbeData *p)
{
if (AV_RL32(p->buf) == MKTAG('M','L','V','I') &&
AV_RL32(p->buf + 4) >= 52 &&
!memcmp(p->buf + 8, MLV_VERSION, 5))
return AVPROBE_SCORE_MAX;
return 0;
}
static int check_file_header(AVIOContext *pb, uint64_t guid)
{
unsigned int size;
uint8_t version[8];
avio_skip(pb, 4);
size = avio_rl32(pb);
if (size < 52)
return AVERROR_INVALIDDATA;
avio_read(pb, version, 8);
if (memcmp(version, MLV_VERSION, 5) || avio_rl64(pb) != guid)
return AVERROR_INVALIDDATA;
avio_skip(pb, size - 24);
return 0;
}
static void read_string(AVFormatContext *avctx, AVIOContext *pb, const char *tag, unsigned size)
{
char * value = av_malloc(size + 1);
int ret;
if (!value) {
avio_skip(pb, size);
return;
}
ret = avio_read(pb, value, size);
if (ret != size || !value[0]) {
av_free(value);
return;
}
value[size] = 0;
av_dict_set(&avctx->metadata, tag, value, AV_DICT_DONT_STRDUP_VAL);
}
static void read_uint8(AVFormatContext *avctx, AVIOContext *pb, const char *tag, const char *fmt)
{
av_dict_set_int(&avctx->metadata, tag, avio_r8(pb), 0);
}
static void read_uint16(AVFormatContext *avctx, AVIOContext *pb, const char *tag, const char *fmt)
{
av_dict_set_int(&avctx->metadata, tag, avio_rl16(pb), 0);
}
static void read_uint32(AVFormatContext *avctx, AVIOContext *pb, const char *tag, const char *fmt)
{
av_dict_set_int(&avctx->metadata, tag, avio_rl32(pb), 0);
}
static void read_uint64(AVFormatContext *avctx, AVIOContext *pb, const char *tag, const char *fmt)
{
av_dict_set_int(&avctx->metadata, tag, avio_rl64(pb), 0);
}
static int scan_file(AVFormatContext *avctx, AVStream *vst, AVStream *ast, int file)
{
FFStream *const vsti = ffstream(vst), *const asti = ffstream(ast);
MlvContext *mlv = avctx->priv_data;
AVIOContext *pb = mlv->pb[file];
int ret;
while (!avio_feof(pb)) {
int type;
unsigned int size;
type = avio_rl32(pb);
size = avio_rl32(pb);
avio_skip(pb, 8); //timestamp
if (size < 16)
break;
size -= 16;
if (vst && type == MKTAG('R','A','W','I') && size >= 164) {
unsigned width = avio_rl16(pb);
unsigned height = avio_rl16(pb);
unsigned bits_per_coded_sample;
ret = av_image_check_size(width, height, 0, avctx);
if (ret < 0)
return ret;
if (avio_rl32(pb) != 1)
avpriv_request_sample(avctx, "raw api version");
avio_skip(pb, 20); // pointer, width, height, pitch, frame_size
bits_per_coded_sample = avio_rl32(pb);
if (bits_per_coded_sample > (INT_MAX - 7) / (width * height)) {
av_log(avctx, AV_LOG_ERROR,
"invalid bits_per_coded_sample %u (size: %ux%u)\n",
bits_per_coded_sample, width, height);
return AVERROR_INVALIDDATA;
}
vst->codecpar->width = width;
vst->codecpar->height = height;
vst->codecpar->bits_per_coded_sample = bits_per_coded_sample;
mlv->black_level = avio_rl32(pb);
mlv->white_level = avio_rl32(pb);
avio_skip(pb, 16 + 24); // xywh, active_area, exposure_bias
if (avio_rl32(pb) != 0x2010100) /* RGGB */
avpriv_request_sample(avctx, "cfa_pattern");
avio_skip(pb, 4); // calibration_illuminant1,
for (int i = 0; i < 9; i++) {
mlv->color_matrix1[i][0] = avio_rl32(pb);
mlv->color_matrix1[i][1] = avio_rl32(pb);
}
avio_skip(pb, 4); // dynamic_range
vst->codecpar->format = AV_PIX_FMT_BAYER_RGGB16LE;
vst->codecpar->codec_tag = MKTAG('B', 'I', 'T', 16);
size -= 164;
} else if (ast && type == MKTAG('W', 'A', 'V', 'I') && size >= 16) {
ret = ff_get_wav_header(avctx, pb, ast->codecpar, 16, 0);
if (ret < 0)
return ret;
size -= 16;
} else if (type == MKTAG('I','N','F','O')) {
if (size > 0)
read_string(avctx, pb, "info", size);
continue;
} else if (type == MKTAG('I','D','N','T') && size >= 36) {
read_string(avctx, pb, "cameraName", 32);
read_uint32(avctx, pb, "cameraModel", "0x%"PRIx32);
size -= 36;
if (size >= 32) {
read_string(avctx, pb, "cameraSerial", 32);
size -= 32;
}
} else if (type == MKTAG('L','E','N','S') && size >= 48) {
read_uint16(avctx, pb, "focalLength", "%i");
read_uint16(avctx, pb, "focalDist", "%i");
read_uint16(avctx, pb, "aperture", "%i");
read_uint8(avctx, pb, "stabilizerMode", "%i");
read_uint8(avctx, pb, "autofocusMode", "%i");
read_uint32(avctx, pb, "flags", "0x%"PRIx32);
read_uint32(avctx, pb, "lensID", "%"PRIi32);
read_string(avctx, pb, "lensName", 32);
size -= 48;
if (size >= 32) {
read_string(avctx, pb, "lensSerial", 32);
size -= 32;
}
} else if (vst && type == MKTAG('V', 'I', 'D', 'F') && size >= 4) {
uint64_t pts = avio_rl32(pb);
ff_add_index_entry(&vsti->index_entries, &vsti->nb_index_entries,
&vsti->index_entries_allocated_size,
avio_tell(pb) - 20, pts, file, 0, AVINDEX_KEYFRAME);
size -= 4;
} else if (ast && type == MKTAG('A', 'U', 'D', 'F') && size >= 4) {
uint64_t pts = avio_rl32(pb);
ff_add_index_entry(&asti->index_entries, &asti->nb_index_entries,
&asti->index_entries_allocated_size,
avio_tell(pb) - 20, pts, file, 0, AVINDEX_KEYFRAME);
size -= 4;
} else if (vst && type == MKTAG('W','B','A','L') && size >= 28) {
read_uint32(avctx, pb, "wb_mode", "%"PRIi32);
read_uint32(avctx, pb, "kelvin", "%"PRIi32);
read_uint32(avctx, pb, "wbgain_r", "%"PRIi32);
read_uint32(avctx, pb, "wbgain_g", "%"PRIi32);
read_uint32(avctx, pb, "wbgain_b", "%"PRIi32);
read_uint32(avctx, pb, "wbs_gm", "%"PRIi32);
read_uint32(avctx, pb, "wbs_ba", "%"PRIi32);
size -= 28;
} else if (type == MKTAG('R','T','C','I') && size >= 20) {
char str[32];
struct tm time = { 0 };
time.tm_sec = avio_rl16(pb);
time.tm_min = avio_rl16(pb);
time.tm_hour = avio_rl16(pb);
time.tm_mday = avio_rl16(pb);
time.tm_mon = avio_rl16(pb);
time.tm_year = avio_rl16(pb);
time.tm_wday = avio_rl16(pb);
time.tm_yday = avio_rl16(pb);
time.tm_isdst = avio_rl16(pb);
avio_skip(pb, 2);
if (strftime(str, sizeof(str), "%Y-%m-%d %H:%M:%S", &time))
av_dict_set(&avctx->metadata, "time", str, 0);
size -= 20;
} else if (type == MKTAG('E','X','P','O') && size >= 16) {
av_dict_set(&avctx->metadata, "isoMode", avio_rl32(pb) ? "auto" : "manual", 0);
read_uint32(avctx, pb, "isoValue", "%"PRIi32);
read_uint32(avctx, pb, "isoAnalog", "%"PRIi32);
read_uint32(avctx, pb, "digitalGain", "%"PRIi32);
size -= 16;
if (size >= 8) {
read_uint64(avctx, pb, "shutterValue", "%"PRIi64);
size -= 8;
}
} else if (type == MKTAG('S','T','Y','L') && size >= 36) {
read_uint32(avctx, pb, "picStyleId", "%"PRIi32);
read_uint32(avctx, pb, "contrast", "%"PRIi32);
read_uint32(avctx, pb, "sharpness", "%"PRIi32);
read_uint32(avctx, pb, "saturation", "%"PRIi32);
read_uint32(avctx, pb, "colortone", "%"PRIi32);
read_string(avctx, pb, "picStyleName", 16);
size -= 36;
} else if (type == MKTAG('V','E','R','S') && size >= 4) {
unsigned int length = avio_rl32(pb);
read_string(avctx, pb, "version", length);
size -= length + 4;
} else if (type == MKTAG('D','A','R','K')) {
} else if (type == MKTAG('D','I','S','O')) {
} else if (type == MKTAG('M','A','R','K')) {
} else if (type == MKTAG('N','U','L','L')) {
} else if (type == MKTAG('M','L','V','I')) { /* occurs when MLV and Mnn files are concatenated */
} else if (type == MKTAG('R','A','W','C')) {
} else {
av_log(avctx, AV_LOG_INFO, "unsupported tag %s, size %u\n",
av_fourcc2str(type), size);
}
avio_skip(pb, size);
}
return 0;
}
static int read_header(AVFormatContext *avctx)
{
MlvContext *mlv = avctx->priv_data;
AVIOContext *pb = avctx->pb;
AVStream *vst = NULL, *ast = NULL;
FFStream *vsti = NULL, *asti = NULL;
int size, ret;
unsigned nb_video_frames, nb_audio_frames;
uint64_t guid;
char guidstr[32];
avio_skip(pb, 4);
size = avio_rl32(pb);
if (size < 52)
return AVERROR_INVALIDDATA;
avio_skip(pb, 8);
guid = avio_rl64(pb);
snprintf(guidstr, sizeof(guidstr), "0x%"PRIx64, guid);
av_dict_set(&avctx->metadata, "guid", guidstr, 0);
avio_skip(pb, 8); //fileNum, fileCount, fileFlags
mlv->class[0] = avio_rl16(pb);
mlv->class[1] = avio_rl16(pb);
nb_video_frames = avio_rl32(pb);
nb_audio_frames = avio_rl32(pb);
if (nb_video_frames && mlv->class[0]) {
vst = avformat_new_stream(avctx, NULL);
if (!vst)
return AVERROR(ENOMEM);
vsti = ffstream(vst);
vst->id = 0;
vst->nb_frames = nb_video_frames;
if ((mlv->class[0] & (MLV_CLASS_FLAG_DELTA|MLV_CLASS_FLAG_LZMA)))
avpriv_request_sample(avctx, "compression");
vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
switch (mlv->class[0] & ~(MLV_CLASS_FLAG_DELTA|MLV_CLASS_FLAG_LZMA)) {
case MLV_VIDEO_CLASS_RAW:
vst->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
break;
case MLV_VIDEO_CLASS_YUV:
vst->codecpar->format = AV_PIX_FMT_YUV420P;
vst->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
vst->codecpar->codec_tag = 0;
break;
case MLV_CLASS_FLAG_LJ92|MLV_VIDEO_CLASS_RAW:
vst->codecpar->codec_id = AV_CODEC_ID_TIFF;
break;
case MLV_VIDEO_CLASS_JPEG:
vst->codecpar->codec_id = AV_CODEC_ID_MJPEG;
vst->codecpar->codec_tag = 0;
break;
case MLV_VIDEO_CLASS_H264:
vst->codecpar->codec_id = AV_CODEC_ID_H264;
vst->codecpar->codec_tag = 0;
break;
default:
avpriv_request_sample(avctx, "unknown video class");
}
}
if (nb_audio_frames && mlv->class[1]) {
ast = avformat_new_stream(avctx, NULL);
if (!ast)
return AVERROR(ENOMEM);
asti = ffstream(ast);
ast->id = 1;
ast->nb_frames = nb_audio_frames;
if ((mlv->class[1] & MLV_CLASS_FLAG_LZMA))
avpriv_request_sample(avctx, "compression");
if ((mlv->class[1] & ~MLV_CLASS_FLAG_LZMA) != MLV_AUDIO_CLASS_WAV)
avpriv_request_sample(avctx, "unknown audio class");
ast->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
avpriv_set_pts_info(ast, 33, 1, ast->codecpar->sample_rate);
}
if (vst) {
AVRational framerate;
framerate.num = avio_rl32(pb);
framerate.den = avio_rl32(pb);
avpriv_set_pts_info(vst, 64, framerate.den, framerate.num);
} else
avio_skip(pb, 8);
avio_skip(pb, size - 52);
/* scan primary file */
mlv->pb[100] = avctx->pb;
ret = scan_file(avctx, vst, ast, 100);
if (ret < 0)
return ret;
/* scan secondary files */
if (strlen(avctx->url) > 2) {
int i;
char *filename = av_strdup(avctx->url);
if (!filename)
return AVERROR(ENOMEM);
for (i = 0; i < 100; i++) {
snprintf(filename + strlen(filename) - 2, 3, "%02d", i);
if (avctx->io_open(avctx, &mlv->pb[i], filename, AVIO_FLAG_READ, NULL) < 0)
break;
if (check_file_header(mlv->pb[i], guid) < 0) {
av_log(avctx, AV_LOG_WARNING, "ignoring %s; bad format or guid mismatch\n", filename);
ff_format_io_close(avctx, &mlv->pb[i]);
continue;
}
av_log(avctx, AV_LOG_INFO, "scanning %s\n", filename);
ret = scan_file(avctx, vst, ast, i);
if (ret < 0) {
av_log(avctx, AV_LOG_WARNING, "ignoring %s; %s\n", filename, av_err2str(ret));
ff_format_io_close(avctx, &mlv->pb[i]);
continue;
}
}
av_free(filename);
}
if (vst)
vst->duration = vsti->nb_index_entries;
if (ast)
ast->duration = asti->nb_index_entries;
if ((vst && !vsti->nb_index_entries) || (ast && !asti->nb_index_entries)) {
av_log(avctx, AV_LOG_ERROR, "no index entries found\n");
return AVERROR_INVALIDDATA;
}
if (vst && ast)
avio_seek(pb, FFMIN(vsti->index_entries[0].pos, asti->index_entries[0].pos), SEEK_SET);
else if (vst)
avio_seek(pb, vsti->index_entries[0].pos, SEEK_SET);
else if (ast)
avio_seek(pb, asti->index_entries[0].pos, SEEK_SET);
return 0;
}
static void write_tiff_short(PutByteContext *pb, int tag, int value)
{
bytestream2_put_le16(pb, tag);
bytestream2_put_le16(pb, TIFF_SHORT);
bytestream2_put_le32(pb, 1);
bytestream2_put_le16(pb, value);
bytestream2_put_le16(pb, 0);
}
static void write_tiff_short2(PutByteContext *pb, int tag, int v1, int v2)
{
bytestream2_put_le16(pb, tag);
bytestream2_put_le16(pb, TIFF_SHORT);
bytestream2_put_le32(pb, 2);
bytestream2_put_le16(pb, v1);
bytestream2_put_le16(pb, v2);
}
static void write_tiff_long(PutByteContext *pb, int tag, int value)
{
bytestream2_put_le16(pb, tag);
bytestream2_put_le16(pb, TIFF_LONG);
bytestream2_put_le32(pb, 1);
bytestream2_put_le32(pb, value);
}
static void write_tiff_byte4(PutByteContext *pb, int tag, int v1, int v2, int v3, int v4)
{
bytestream2_put_le16(pb, tag);
bytestream2_put_le16(pb, TIFF_BYTE);
bytestream2_put_le32(pb, 4);
bytestream2_put_byte(pb, v1);
bytestream2_put_byte(pb, v2);
bytestream2_put_byte(pb, v3);
bytestream2_put_byte(pb, v4);
}
static int get_packet_lj92(AVFormatContext *avctx, AVStream *st, AVIOContext *pbio, AVPacket *pkt, int64_t size)
{
MlvContext *mlv = avctx->priv_data;
PutByteContext pbctx, *pb = &pbctx;
int ret, header_size;
uint8_t *stripofs, *matrixofs;
#define MAX_HEADER_SIZE 2048
if ((uint64_t)size > INT32_MAX - MAX_HEADER_SIZE)
return AVERROR_PATCHWELCOME;
if ((ret = av_new_packet(pkt, size + MAX_HEADER_SIZE)) < 0)
return ret;
bytestream2_init_writer(pb, pkt->data, MAX_HEADER_SIZE);
bytestream2_put_le16(pb, 0x4949);
bytestream2_put_le16(pb, 42);
bytestream2_put_le32(pb, 8);
bytestream2_put_le16(pb, 18); /* nb_entries */
write_tiff_long(pb, TIFF_SUBFILE, 0);
write_tiff_long(pb, TIFF_WIDTH, st->codecpar->width);
write_tiff_long(pb, TIFF_HEIGHT, st->codecpar->height);
write_tiff_long(pb, TIFF_BPP, st->codecpar->bits_per_coded_sample);
write_tiff_short(pb, TIFF_COMPR, TIFF_NEWJPEG);
write_tiff_short(pb, TIFF_PHOTOMETRIC, TIFF_PHOTOMETRIC_CFA);
write_tiff_short(pb, TIFF_FILL_ORDER, 1);
write_tiff_long(pb, TIFF_STRIP_OFFS, 0); /* stripofs */
stripofs = pb->buffer - 4;
write_tiff_long(pb, TIFF_SAMPLES_PER_PIXEL, 1);
write_tiff_short(pb, TIFF_ROWSPERSTRIP, st->codecpar->height);
write_tiff_long(pb, TIFF_STRIP_SIZE, size);
write_tiff_short(pb, TIFF_PLANAR, 1);
write_tiff_short2(pb, TIFF_CFA_PATTERN_DIM, 2, 2);
write_tiff_byte4(pb, TIFF_CFA_PATTERN, 0, 1, 1, 2);
write_tiff_byte4(pb, DNG_VERSION, 1, 4, 0, 0);
write_tiff_long(pb, DNG_BLACK_LEVEL, mlv->black_level);
write_tiff_long(pb, DNG_WHITE_LEVEL, mlv->white_level);
bytestream2_put_le16(pb, DNG_COLOR_MATRIX1);
bytestream2_put_le16(pb, TIFF_SRATIONAL);
bytestream2_put_le32(pb, 9);
bytestream2_put_le32(pb, 0); /* matrixofs */
matrixofs = pb->buffer - 4;
bytestream2_put_le32(pb, 0);
AV_WL32(matrixofs, bytestream2_tell_p(pb));
for (int i = 0; i < 9; i++) {
bytestream2_put_le32(pb, mlv->color_matrix1[i][0]);
bytestream2_put_le32(pb, mlv->color_matrix1[i][1]);
}
header_size = bytestream2_tell_p(pb);
AV_WL32(stripofs, header_size);
ret = avio_read(pbio, pkt->data + header_size, size);
if (ret < 0)
return ret;
return 0;
}
static int read_packet(AVFormatContext *avctx, AVPacket *pkt)
{
MlvContext *mlv = avctx->priv_data;
AVIOContext *pb;
AVStream *st;
FFStream *sti;
int index, ret;
unsigned int size, space;
if (!avctx->nb_streams)
return AVERROR_EOF;
st = avctx->streams[mlv->stream_index];
sti = ffstream(st);
if (mlv->pts >= st->duration)
return AVERROR_EOF;
index = av_index_search_timestamp(st, mlv->pts, AVSEEK_FLAG_ANY);
if (index < 0) {
av_log(avctx, AV_LOG_ERROR, "could not find index entry for frame %"PRId64"\n", mlv->pts);
return AVERROR(EIO);
}
pb = mlv->pb[sti->index_entries[index].size];
if (!pb) {
ret = FFERROR_REDO;
goto next_packet;
}
avio_seek(pb, sti->index_entries[index].pos, SEEK_SET);
avio_skip(pb, 4); // blockType
size = avio_rl32(pb);
if (size < 16)
return AVERROR_INVALIDDATA;
avio_skip(pb, 12); //timestamp, frameNumber
size -= 12;
if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
if (size < 8)
return AVERROR_INVALIDDATA;
avio_skip(pb, 8); // cropPosX, cropPosY, panPosX, panPosY
size -= 8;
}
space = avio_rl32(pb);
if (size < space + 4LL)
return AVERROR_INVALIDDATA;
avio_skip(pb, space);
size -= space;
if ((mlv->class[st->id] & (MLV_CLASS_FLAG_DELTA|MLV_CLASS_FLAG_LZMA))) {
ret = AVERROR_PATCHWELCOME;
} else if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
if (st->codecpar->codec_id == AV_CODEC_ID_TIFF)
ret = get_packet_lj92(avctx, st, pb, pkt, size);
else
ret = av_get_packet(pb, pkt, (st->codecpar->width * st->codecpar->height * st->codecpar->bits_per_coded_sample + 7) >> 3);
} else { // AVMEDIA_TYPE_AUDIO
ret = av_get_packet(pb, pkt, size - 4);
}
if (ret < 0)
return ret;
pkt->stream_index = mlv->stream_index;
pkt->pts = mlv->pts;
ret = 0;
next_packet:
mlv->stream_index++;
if (mlv->stream_index == avctx->nb_streams) {
mlv->stream_index = 0;
mlv->pts++;
}
return ret;
}
static int read_seek(AVFormatContext *avctx, int stream_index, int64_t timestamp, int flags)
{
MlvContext *mlv = avctx->priv_data;
if ((flags & AVSEEK_FLAG_FRAME) || (flags & AVSEEK_FLAG_BYTE))
return AVERROR(ENOSYS);
if (!(avctx->pb->seekable & AVIO_SEEKABLE_NORMAL))
return AVERROR(EIO);
mlv->pts = timestamp;
return 0;
}
static int read_close(AVFormatContext *s)
{
MlvContext *mlv = s->priv_data;
int i;
for (i = 0; i < 100; i++)
ff_format_io_close(s, &mlv->pb[i]);
return 0;
}
const FFInputFormat ff_mlv_demuxer = {
.p.name = "mlv",
.p.long_name = NULL_IF_CONFIG_SMALL("Magic Lantern Video (MLV)"),
.priv_data_size = sizeof(MlvContext),
.flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
.read_probe = probe,
.read_header = read_header,
.read_packet = read_packet,
.read_close = read_close,
.read_seek = read_seek,
};
|