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