| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Interplay C93 demuxer | ||
| 3 | * Copyright (c) 2007 Anssi Hannula <anssi.hannula@gmail.com> | ||
| 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 | #include "avformat.h" | ||
| 23 | #include "avio_internal.h" | ||
| 24 | #include "demux.h" | ||
| 25 | #include "internal.h" | ||
| 26 | #include "voc.h" | ||
| 27 | #include "libavutil/intreadwrite.h" | ||
| 28 | |||
| 29 | typedef struct C93BlockRecord { | ||
| 30 | uint16_t index; | ||
| 31 | uint8_t length; | ||
| 32 | uint8_t frames; | ||
| 33 | } C93BlockRecord; | ||
| 34 | |||
| 35 | typedef struct C93DemuxContext { | ||
| 36 | VocDecContext voc; | ||
| 37 | |||
| 38 | C93BlockRecord block_records[512]; | ||
| 39 | int current_block; | ||
| 40 | |||
| 41 | uint32_t frame_offsets[32]; | ||
| 42 | int current_frame; | ||
| 43 | int next_pkt_is_audio; | ||
| 44 | |||
| 45 | AVStream *audio; | ||
| 46 | } C93DemuxContext; | ||
| 47 | |||
| 48 | 7480 | static int probe(const AVProbeData *p) | |
| 49 | { | ||
| 50 | int i; | ||
| 51 | 7480 | int index = 1; | |
| 52 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7480 times.
|
7480 | if (p->buf_size < 16) |
| 53 | ✗ | return 0; | |
| 54 |
2/2✓ Branch 0 taken 7484 times.
✓ Branch 1 taken 1 times.
|
7485 | for (i = 0; i < 16; i += 4) { |
| 55 |
5/6✓ Branch 0 taken 15 times.
✓ Branch 1 taken 7469 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 10 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 5 times.
|
7484 | if (AV_RL16(p->buf + i) != index || !p->buf[i + 2] || !p->buf[i + 3]) |
| 56 | 7479 | return 0; | |
| 57 | 5 | index += p->buf[i + 2]; | |
| 58 | } | ||
| 59 | 1 | return AVPROBE_SCORE_MAX; | |
| 60 | } | ||
| 61 | |||
| 62 | 1 | static int read_header(AVFormatContext *s) | |
| 63 | { | ||
| 64 | AVStream *video; | ||
| 65 | 1 | AVIOContext *pb = s->pb; | |
| 66 | 1 | C93DemuxContext *c93 = s->priv_data; | |
| 67 | int i; | ||
| 68 | 1 | int framecount = 0; | |
| 69 | |||
| 70 |
2/2✓ Branch 0 taken 512 times.
✓ Branch 1 taken 1 times.
|
513 | for (i = 0; i < 512; i++) { |
| 71 | 512 | c93->block_records[i].index = avio_rl16(pb); | |
| 72 | 512 | c93->block_records[i].length = avio_r8(pb); | |
| 73 | 512 | c93->block_records[i].frames = avio_r8(pb); | |
| 74 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 512 times.
|
512 | if (c93->block_records[i].frames > 32) { |
| 75 | ✗ | av_log(s, AV_LOG_ERROR, "too many frames in block\n"); | |
| 76 | ✗ | return AVERROR_INVALIDDATA; | |
| 77 | } | ||
| 78 | 512 | framecount += c93->block_records[i].frames; | |
| 79 | } | ||
| 80 | |||
| 81 | /* Audio streams are added if audio packets are found */ | ||
| 82 | 1 | s->ctx_flags |= AVFMTCTX_NOHEADER; | |
| 83 | |||
| 84 | 1 | video = avformat_new_stream(s, NULL); | |
| 85 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!video) |
| 86 | ✗ | return AVERROR(ENOMEM); | |
| 87 | |||
| 88 | 1 | video->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; | |
| 89 | 1 | video->codecpar->codec_id = AV_CODEC_ID_C93; | |
| 90 | 1 | video->codecpar->width = 320; | |
| 91 | 1 | video->codecpar->height = 192; | |
| 92 | /* 4:3 320x200 with 8 empty lines */ | ||
| 93 | 1 | video->sample_aspect_ratio = (AVRational) { 5, 6 }; | |
| 94 | 1 | avpriv_set_pts_info(video, 64, 2, 25); | |
| 95 | 1 | video->nb_frames = framecount; | |
| 96 | 1 | video->duration = framecount; | |
| 97 | 1 | video->start_time = 0; | |
| 98 | |||
| 99 | 1 | c93->current_block = 0; | |
| 100 | 1 | c93->current_frame = 0; | |
| 101 | 1 | c93->next_pkt_is_audio = 0; | |
| 102 | 1 | return 0; | |
| 103 | } | ||
| 104 | |||
| 105 | #define C93_HAS_PALETTE 0x01 | ||
| 106 | #define C93_FIRST_FRAME 0x02 | ||
| 107 | |||
| 108 | 72 | static int read_packet(AVFormatContext *s, AVPacket *pkt) | |
| 109 | { | ||
| 110 | 72 | AVIOContext *pb = s->pb; | |
| 111 | 72 | C93DemuxContext *c93 = s->priv_data; | |
| 112 | 72 | C93BlockRecord *br = &c93->block_records[c93->current_block]; | |
| 113 | int datasize; | ||
| 114 | int ret, i; | ||
| 115 | |||
| 116 |
2/2✓ Branch 0 taken 65 times.
✓ Branch 1 taken 7 times.
|
72 | if (c93->next_pkt_is_audio) { |
| 117 | 65 | c93->current_frame++; | |
| 118 | 65 | c93->next_pkt_is_audio = 0; | |
| 119 | 65 | datasize = avio_rl16(pb); | |
| 120 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 59 times.
|
65 | if (datasize > 42) { |
| 121 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 5 times.
|
6 | if (!c93->audio) { |
| 122 | 1 | c93->audio = avformat_new_stream(s, NULL); | |
| 123 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!c93->audio) |
| 124 | ✗ | return AVERROR(ENOMEM); | |
| 125 | 1 | c93->audio->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; | |
| 126 | } | ||
| 127 | 6 | avio_skip(pb, 26); /* VOC header */ | |
| 128 | 6 | ret = ff_voc_get_packet(s, pkt, c93->audio, datasize - 26); | |
| 129 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | if (ret > 0) { |
| 130 | 6 | pkt->stream_index = 1; | |
| 131 | 6 | pkt->flags |= AV_PKT_FLAG_KEY; | |
| 132 | 6 | return ret; | |
| 133 | } | ||
| 134 | } | ||
| 135 | } | ||
| 136 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 61 times.
|
66 | if (c93->current_frame >= br->frames) { |
| 137 |
2/4✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5 times.
|
5 | if (c93->current_block >= 511 || !br[1].length) |
| 138 | ✗ | return AVERROR_EOF; | |
| 139 | 5 | br++; | |
| 140 | 5 | c93->current_block++; | |
| 141 | 5 | c93->current_frame = 0; | |
| 142 | } | ||
| 143 | |||
| 144 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 60 times.
|
66 | if (c93->current_frame == 0) { |
| 145 | 6 | avio_seek(pb, br->index * 2048, SEEK_SET); | |
| 146 |
2/2✓ Branch 0 taken 192 times.
✓ Branch 1 taken 6 times.
|
198 | for (i = 0; i < 32; i++) { |
| 147 | 192 | c93->frame_offsets[i] = avio_rl32(pb); | |
| 148 | } | ||
| 149 | } | ||
| 150 | |||
| 151 | 66 | avio_seek(pb,br->index * 2048 + | |
| 152 | 66 | c93->frame_offsets[c93->current_frame], SEEK_SET); | |
| 153 | 66 | datasize = avio_rl16(pb); /* video frame size */ | |
| 154 | |||
| 155 | 66 | ret = av_new_packet(pkt, datasize + 768 + 1); | |
| 156 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 66 times.
|
66 | if (ret < 0) |
| 157 | ✗ | return ret; | |
| 158 | 66 | pkt->data[0] = 0; | |
| 159 | 66 | pkt->size = datasize + 1; | |
| 160 | |||
| 161 | 66 | ret = ffio_read_size(pb, pkt->data + 1, datasize); | |
| 162 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 66 times.
|
66 | if (ret < 0) { |
| 163 | ✗ | return ret; | |
| 164 | } | ||
| 165 | |||
| 166 | 66 | datasize = avio_rl16(pb); /* palette size */ | |
| 167 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 65 times.
|
66 | if (datasize) { |
| 168 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (datasize != 768) { |
| 169 | ✗ | av_log(s, AV_LOG_ERROR, "invalid palette size %u\n", datasize); | |
| 170 | ✗ | return AVERROR_INVALIDDATA; | |
| 171 | } | ||
| 172 | 1 | pkt->data[0] |= C93_HAS_PALETTE; | |
| 173 | 1 | ret = ffio_read_size(pb, pkt->data + pkt->size, datasize); | |
| 174 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (ret < 0) { |
| 175 | ✗ | return ret; | |
| 176 | } | ||
| 177 | 1 | pkt->size += 768; | |
| 178 | } | ||
| 179 | 66 | pkt->stream_index = 0; | |
| 180 | 66 | c93->next_pkt_is_audio = 1; | |
| 181 | |||
| 182 | /* only the first frame is guaranteed to not reference previous frames */ | ||
| 183 |
4/4✓ Branch 0 taken 11 times.
✓ Branch 1 taken 55 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 10 times.
|
66 | if (c93->current_block == 0 && c93->current_frame == 0) { |
| 184 | 1 | pkt->flags |= AV_PKT_FLAG_KEY; | |
| 185 | 1 | pkt->data[0] |= C93_FIRST_FRAME; | |
| 186 | } | ||
| 187 | 66 | return 0; | |
| 188 | } | ||
| 189 | |||
| 190 | const FFInputFormat ff_c93_demuxer = { | ||
| 191 | .p.name = "c93", | ||
| 192 | .p.long_name = NULL_IF_CONFIG_SMALL("Interplay C93"), | ||
| 193 | .priv_data_size = sizeof(C93DemuxContext), | ||
| 194 | .read_probe = probe, | ||
| 195 | .read_header = read_header, | ||
| 196 | .read_packet = read_packet, | ||
| 197 | }; | ||
| 198 |