| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Bitmap Brothers JV demuxer | ||
| 3 | * Copyright (c) 2005, 2011 Peter Ross <pross@xvid.org> | ||
| 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 | * Bitmap Brothers JV demuxer | ||
| 25 | * @author Peter Ross <pross@xvid.org> | ||
| 26 | */ | ||
| 27 | |||
| 28 | #include "libavutil/channel_layout.h" | ||
| 29 | #include "libavutil/intreadwrite.h" | ||
| 30 | #include "libavutil/mem.h" | ||
| 31 | |||
| 32 | #include "avformat.h" | ||
| 33 | #include "demux.h" | ||
| 34 | #include "internal.h" | ||
| 35 | |||
| 36 | #define JV_PREAMBLE_SIZE 5 | ||
| 37 | |||
| 38 | typedef struct JVFrame { | ||
| 39 | int audio_size; /**< audio packet size (bytes) */ | ||
| 40 | int video_size; /**< video packet size (bytes) */ | ||
| 41 | uint16_t palette_size; /**< palette size (bytes) */ | ||
| 42 | uint8_t video_type; /**< per-frame video compression type */ | ||
| 43 | } JVFrame; | ||
| 44 | |||
| 45 | typedef struct JVDemuxContext { | ||
| 46 | JVFrame *frames; | ||
| 47 | enum { | ||
| 48 | JV_AUDIO = 0, | ||
| 49 | JV_VIDEO, | ||
| 50 | JV_PADDING | ||
| 51 | } state; | ||
| 52 | int64_t pts; | ||
| 53 | } JVDemuxContext; | ||
| 54 | |||
| 55 | #define MAGIC " Compression by John M Phillips Copyright (C) 1995 The Bitmap Brothers Ltd." | ||
| 56 | |||
| 57 | 7480 | static int read_probe(const AVProbeData *pd) | |
| 58 | { | ||
| 59 |
4/6✓ Branch 0 taken 2 times.
✓ Branch 1 taken 7478 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
|
7480 | if (pd->buf[0] == 'J' && pd->buf[1] == 'V' && strlen(MAGIC) + 4 <= pd->buf_size && |
| 60 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | !memcmp(pd->buf + 4, MAGIC, strlen(MAGIC))) |
| 61 | 2 | return AVPROBE_SCORE_MAX; | |
| 62 | 7478 | return 0; | |
| 63 | } | ||
| 64 | |||
| 65 | 2 | static int read_close(AVFormatContext *s) | |
| 66 | { | ||
| 67 | 2 | JVDemuxContext *jv = s->priv_data; | |
| 68 | |||
| 69 | 2 | av_freep(&jv->frames); | |
| 70 | |||
| 71 | 2 | return 0; | |
| 72 | } | ||
| 73 | |||
| 74 | 2 | static int read_header(AVFormatContext *s) | |
| 75 | { | ||
| 76 | 2 | JVDemuxContext *jv = s->priv_data; | |
| 77 | 2 | AVIOContext *pb = s->pb; | |
| 78 | AVStream *vst, *ast; | ||
| 79 | FFStream *asti; | ||
| 80 | 2 | int64_t audio_pts = 0; | |
| 81 | int64_t offset; | ||
| 82 | |||
| 83 | 2 | avio_skip(pb, 80); | |
| 84 | |||
| 85 | 2 | ast = avformat_new_stream(s, NULL); | |
| 86 | 2 | vst = avformat_new_stream(s, NULL); | |
| 87 |
2/4✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
|
2 | if (!ast || !vst) |
| 88 | ✗ | return AVERROR(ENOMEM); | |
| 89 | 2 | asti = ffstream(ast); | |
| 90 | |||
| 91 | 2 | vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; | |
| 92 | 2 | vst->codecpar->codec_id = AV_CODEC_ID_JV; | |
| 93 | 2 | vst->codecpar->codec_tag = 0; /* no fourcc */ | |
| 94 | 2 | vst->codecpar->width = avio_rl16(pb); | |
| 95 | 2 | vst->codecpar->height = avio_rl16(pb); | |
| 96 | 2 | vst->duration = | |
| 97 | 2 | vst->nb_frames = | |
| 98 | 2 | asti->nb_index_entries = avio_rl16(pb); | |
| 99 | 2 | avpriv_set_pts_info(vst, 64, avio_rl16(pb), 1000); | |
| 100 | |||
| 101 | 2 | avio_skip(pb, 4); | |
| 102 | |||
| 103 | 2 | ast->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; | |
| 104 | 2 | ast->codecpar->codec_id = AV_CODEC_ID_PCM_U8; | |
| 105 | 2 | ast->codecpar->codec_tag = 0; /* no fourcc */ | |
| 106 | 2 | ast->codecpar->sample_rate = avio_rl16(pb); | |
| 107 | 2 | ast->codecpar->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO; | |
| 108 | 2 | avpriv_set_pts_info(ast, 64, 1, ast->codecpar->sample_rate); | |
| 109 | |||
| 110 | 2 | avio_skip(pb, 10); | |
| 111 | |||
| 112 | 2 | asti->index_entries = av_malloc(asti->nb_index_entries * | |
| 113 | sizeof(*asti->index_entries)); | ||
| 114 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!asti->index_entries) |
| 115 | ✗ | return AVERROR(ENOMEM); | |
| 116 | |||
| 117 | 2 | jv->frames = av_malloc(asti->nb_index_entries * sizeof(*jv->frames)); | |
| 118 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!jv->frames) |
| 119 | ✗ | return AVERROR(ENOMEM); | |
| 120 | 2 | offset = 0x68 + asti->nb_index_entries * 16; | |
| 121 |
2/2✓ Branch 0 taken 4020 times.
✓ Branch 1 taken 2 times.
|
4022 | for (int i = 0; i < asti->nb_index_entries; i++) { |
| 122 | 4020 | AVIndexEntry *e = asti->index_entries + i; | |
| 123 | 4020 | JVFrame *jvf = jv->frames + i; | |
| 124 | |||
| 125 | /* total frame size including audio, video, palette data and padding */ | ||
| 126 | 4020 | e->size = avio_rl32(pb); | |
| 127 | 4020 | e->timestamp = i; | |
| 128 | 4020 | e->pos = offset; | |
| 129 | 4020 | offset += e->size; | |
| 130 | |||
| 131 | 4020 | jvf->audio_size = avio_rl32(pb); | |
| 132 | 4020 | jvf->video_size = avio_rl32(pb); | |
| 133 |
2/2✓ Branch 1 taken 48 times.
✓ Branch 2 taken 3972 times.
|
4020 | jvf->palette_size = avio_r8(pb) ? 768 : 0; |
| 134 | |||
| 135 |
1/2✓ Branch 0 taken 4020 times.
✗ Branch 1 not taken.
|
4020 | if ((jvf->video_size | jvf->audio_size) & ~0xFFFFFF || |
| 136 | 4020 | e->size - jvf->audio_size | |
| 137 | 4020 | - jvf->video_size | |
| 138 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4020 times.
|
4020 | - jvf->palette_size < 0) { |
| 139 | ✗ | if (s->error_recognition & AV_EF_EXPLODE) | |
| 140 | ✗ | return AVERROR_INVALIDDATA; | |
| 141 | ✗ | jvf->audio_size = | |
| 142 | ✗ | jvf->video_size = | |
| 143 | ✗ | jvf->palette_size = 0; | |
| 144 | } | ||
| 145 | |||
| 146 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 4020 times.
|
4020 | if (avio_r8(pb)) |
| 147 | ✗ | av_log(s, AV_LOG_WARNING, "unsupported audio codec\n"); | |
| 148 | |||
| 149 | 4020 | jvf->video_type = avio_r8(pb); | |
| 150 | 4020 | avio_skip(pb, 1); | |
| 151 | |||
| 152 |
1/2✓ Branch 0 taken 4020 times.
✗ Branch 1 not taken.
|
4020 | e->timestamp = jvf->audio_size ? audio_pts : AV_NOPTS_VALUE; |
| 153 | 4020 | audio_pts += jvf->audio_size; | |
| 154 | |||
| 155 | 4020 | e->flags = jvf->video_type != 1 ? AVINDEX_KEYFRAME : 0; | |
| 156 | } | ||
| 157 | |||
| 158 | 2 | jv->state = JV_AUDIO; | |
| 159 | 2 | return 0; | |
| 160 | } | ||
| 161 | |||
| 162 | 38 | static int read_packet(AVFormatContext *s, AVPacket *pkt) | |
| 163 | { | ||
| 164 | 38 | JVDemuxContext *jv = s->priv_data; | |
| 165 | 38 | AVIOContext *pb = s->pb; | |
| 166 | 38 | AVStream *ast = s->streams[0]; | |
| 167 | 38 | FFStream *const asti = ffstream(ast); | |
| 168 | int ret; | ||
| 169 | |||
| 170 |
3/4✓ Branch 1 taken 52 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 52 times.
✗ Branch 4 not taken.
|
92 | while (!avio_feof(s->pb) && jv->pts < asti->nb_index_entries) { |
| 171 | 52 | const AVIndexEntry *const e = asti->index_entries + jv->pts; | |
| 172 | 52 | const JVFrame *jvf = jv->frames + jv->pts; | |
| 173 | |||
| 174 |
3/4✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
✓ Branch 2 taken 18 times.
✓ Branch 3 taken 16 times.
|
52 | switch (jv->state) { |
| 175 | 18 | case JV_AUDIO: | |
| 176 | 18 | jv->state++; | |
| 177 |
1/2✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
|
18 | if (jvf->audio_size) { |
| 178 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
|
18 | if ((ret = av_get_packet(s->pb, pkt, jvf->audio_size)) < 0) |
| 179 | ✗ | return ret; | |
| 180 | 18 | pkt->stream_index = 0; | |
| 181 | 18 | pkt->pts = e->timestamp; | |
| 182 | 18 | pkt->flags |= AV_PKT_FLAG_KEY; | |
| 183 | 18 | return 0; | |
| 184 | } | ||
| 185 | case JV_VIDEO: | ||
| 186 | 18 | jv->state++; | |
| 187 |
3/4✓ Branch 0 taken 2 times.
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
18 | if (jvf->video_size || jvf->palette_size) { |
| 188 | 18 | int size = jvf->video_size + jvf->palette_size; | |
| 189 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
|
18 | if ((ret = av_new_packet(pkt, size + JV_PREAMBLE_SIZE)) < 0) |
| 190 | ✗ | return ret; | |
| 191 | |||
| 192 | 18 | AV_WL32(pkt->data, jvf->video_size); | |
| 193 | 18 | pkt->data[4] = jvf->video_type; | |
| 194 | 18 | ret = avio_read(pb, pkt->data + JV_PREAMBLE_SIZE, size); | |
| 195 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
|
18 | if (ret < 0) |
| 196 | ✗ | return ret; | |
| 197 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 16 times.
|
18 | if (ret < size) { |
| 198 | 2 | memset(pkt->data + JV_PREAMBLE_SIZE + ret, 0, | |
| 199 | AV_INPUT_BUFFER_PADDING_SIZE); | ||
| 200 | 2 | pkt->flags |= AV_PKT_FLAG_CORRUPT; | |
| 201 | } | ||
| 202 | 18 | pkt->size = ret + JV_PREAMBLE_SIZE; | |
| 203 | 18 | pkt->stream_index = 1; | |
| 204 | 18 | pkt->pts = jv->pts; | |
| 205 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 14 times.
|
18 | if (jvf->video_type != 1) |
| 206 | 4 | pkt->flags |= AV_PKT_FLAG_KEY; | |
| 207 | 18 | return 0; | |
| 208 | } | ||
| 209 | case JV_PADDING: | ||
| 210 | 16 | avio_skip(pb, FFMAX(e->size - jvf->audio_size - jvf->video_size | |
| 211 | - jvf->palette_size, 0)); | ||
| 212 | 16 | jv->state = JV_AUDIO; | |
| 213 | 16 | jv->pts++; | |
| 214 | } | ||
| 215 | } | ||
| 216 | |||
| 217 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | if (s->pb->eof_reached) |
| 218 | 2 | return AVERROR_EOF; | |
| 219 | |||
| 220 | ✗ | return AVERROR_INVALIDDATA; | |
| 221 | } | ||
| 222 | |||
| 223 | ✗ | static int read_seek(AVFormatContext *s, int stream_index, | |
| 224 | int64_t ts, int flags) | ||
| 225 | { | ||
| 226 | ✗ | JVDemuxContext *jv = s->priv_data; | |
| 227 | ✗ | AVStream *ast = s->streams[0]; | |
| 228 | ✗ | FFStream *const asti = ffstream(ast); | |
| 229 | int i; | ||
| 230 | |||
| 231 | ✗ | if (flags & (AVSEEK_FLAG_BYTE | AVSEEK_FLAG_FRAME)) | |
| 232 | ✗ | return AVERROR(ENOSYS); | |
| 233 | |||
| 234 | ✗ | switch (stream_index) { | |
| 235 | ✗ | case 0: | |
| 236 | ✗ | i = av_index_search_timestamp(ast, ts, flags); | |
| 237 | ✗ | break; | |
| 238 | ✗ | case 1: | |
| 239 | ✗ | i = ts; | |
| 240 | ✗ | break; | |
| 241 | ✗ | default: | |
| 242 | ✗ | return 0; | |
| 243 | } | ||
| 244 | |||
| 245 | ✗ | if (i < 0 || i >= asti->nb_index_entries) | |
| 246 | ✗ | return 0; | |
| 247 | ✗ | if (avio_seek(s->pb, asti->index_entries[i].pos, SEEK_SET) < 0) | |
| 248 | ✗ | return -1; | |
| 249 | |||
| 250 | ✗ | jv->state = JV_AUDIO; | |
| 251 | ✗ | jv->pts = i; | |
| 252 | ✗ | return 0; | |
| 253 | } | ||
| 254 | |||
| 255 | const FFInputFormat ff_jv_demuxer = { | ||
| 256 | .p.name = "jv", | ||
| 257 | .p.long_name = NULL_IF_CONFIG_SMALL("Bitmap Brothers JV"), | ||
| 258 | .priv_data_size = sizeof(JVDemuxContext), | ||
| 259 | .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP, | ||
| 260 | .read_probe = read_probe, | ||
| 261 | .read_header = read_header, | ||
| 262 | .read_packet = read_packet, | ||
| 263 | .read_seek = read_seek, | ||
| 264 | .read_close = read_close, | ||
| 265 | }; | ||
| 266 |