| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * DXA demuxer | ||
| 3 | * Copyright (c) 2007 Konstantin Shishkov | ||
| 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 <inttypes.h> | ||
| 23 | |||
| 24 | #include "libavutil/intreadwrite.h" | ||
| 25 | #include "avformat.h" | ||
| 26 | #include "demux.h" | ||
| 27 | #include "internal.h" | ||
| 28 | #include "riff.h" | ||
| 29 | |||
| 30 | #define DXA_EXTRA_SIZE 9 | ||
| 31 | |||
| 32 | typedef struct DXAContext { | ||
| 33 | int frames; | ||
| 34 | int has_sound; | ||
| 35 | int bpc; | ||
| 36 | uint32_t bytes_left; | ||
| 37 | int64_t wavpos, vidpos; | ||
| 38 | int readvid; | ||
| 39 | }DXAContext; | ||
| 40 | |||
| 41 | 7279 | static int dxa_probe(const AVProbeData *p) | |
| 42 | { | ||
| 43 | int w, h; | ||
| 44 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7279 times.
|
7279 | if (p->buf_size < 15) |
| 45 | ✗ | return 0; | |
| 46 | 7279 | w = AV_RB16(p->buf + 11); | |
| 47 | 7279 | h = AV_RB16(p->buf + 13); | |
| 48 | /* check file header */ | ||
| 49 |
4/4✓ Branch 0 taken 144 times.
✓ Branch 1 taken 7135 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 141 times.
|
7279 | if (p->buf[0] == 'D' && p->buf[1] == 'E' && |
| 50 |
3/6✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
|
3 | p->buf[2] == 'X' && p->buf[3] == 'A' && |
| 51 |
3/6✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
|
3 | w && w <= 2048 && h && h <= 2048) |
| 52 | 3 | return AVPROBE_SCORE_MAX; | |
| 53 | else | ||
| 54 | 7276 | return 0; | |
| 55 | } | ||
| 56 | |||
| 57 | 3 | static int dxa_read_header(AVFormatContext *s) | |
| 58 | { | ||
| 59 | 3 | AVIOContext *pb = s->pb; | |
| 60 | 3 | DXAContext *c = s->priv_data; | |
| 61 | AVStream *st, *ast; | ||
| 62 | uint32_t tag; | ||
| 63 | int32_t fps; | ||
| 64 | int w, h; | ||
| 65 | int num, den; | ||
| 66 | int flags; | ||
| 67 | int ret; | ||
| 68 | |||
| 69 | 3 | tag = avio_rl32(pb); | |
| 70 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (tag != MKTAG('D', 'E', 'X', 'A')) |
| 71 | ✗ | return AVERROR_INVALIDDATA; | |
| 72 | 3 | flags = avio_r8(pb); | |
| 73 | 3 | c->frames = avio_rb16(pb); | |
| 74 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if(!c->frames){ |
| 75 | ✗ | av_log(s, AV_LOG_ERROR, "File contains no frames ???\n"); | |
| 76 | ✗ | return AVERROR_INVALIDDATA; | |
| 77 | } | ||
| 78 | |||
| 79 | 3 | fps = avio_rb32(pb); | |
| 80 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if(fps > 0){ |
| 81 | ✗ | den = 1000; | |
| 82 | ✗ | num = fps; | |
| 83 |
2/4✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
3 | }else if (fps < 0 && fps > INT_MIN){ |
| 84 | 3 | den = 100000; | |
| 85 | 3 | num = -fps; | |
| 86 | }else{ | ||
| 87 | ✗ | den = 10; | |
| 88 | ✗ | num = 1; | |
| 89 | } | ||
| 90 | 3 | w = avio_rb16(pb); | |
| 91 | 3 | h = avio_rb16(pb); | |
| 92 | 3 | c->has_sound = 0; | |
| 93 | |||
| 94 | 3 | st = avformat_new_stream(s, NULL); | |
| 95 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (!st) |
| 96 | ✗ | return AVERROR(ENOMEM); | |
| 97 | |||
| 98 | // Parse WAV data header | ||
| 99 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
|
3 | if(avio_rl32(pb) == MKTAG('W', 'A', 'V', 'E')){ |
| 100 | uint32_t size, fsize; | ||
| 101 | 2 | c->has_sound = 1; | |
| 102 | 2 | size = avio_rb32(pb); | |
| 103 | 2 | c->vidpos = avio_tell(pb) + size; | |
| 104 | 2 | avio_skip(pb, 16); | |
| 105 | 2 | fsize = avio_rl32(pb); | |
| 106 | |||
| 107 | 2 | ast = avformat_new_stream(s, NULL); | |
| 108 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!ast) |
| 109 | ✗ | return AVERROR(ENOMEM); | |
| 110 | 2 | ret = ff_get_wav_header(s, pb, ast->codecpar, fsize, 0); | |
| 111 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (ret < 0) |
| 112 | ✗ | return ret; | |
| 113 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | if (ast->codecpar->sample_rate > 0) |
| 114 | 2 | avpriv_set_pts_info(ast, 64, 1, ast->codecpar->sample_rate); | |
| 115 | // find 'data' chunk | ||
| 116 |
2/4✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
|
4 | while(avio_tell(pb) < c->vidpos && !avio_feof(pb)){ |
| 117 | 4 | tag = avio_rl32(pb); | |
| 118 | 4 | fsize = avio_rl32(pb); | |
| 119 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
|
4 | if(tag == MKTAG('d', 'a', 't', 'a')) break; |
| 120 | 2 | avio_skip(pb, fsize); | |
| 121 | } | ||
| 122 | 2 | c->bpc = (fsize + (int64_t)c->frames - 1) / c->frames; | |
| 123 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (c->bpc < 0) |
| 124 | ✗ | return AVERROR_INVALIDDATA; | |
| 125 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | if(ast->codecpar->block_align) { |
| 126 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (c->bpc > INT_MAX - ast->codecpar->block_align + 1) |
| 127 | ✗ | return AVERROR_INVALIDDATA; | |
| 128 | 2 | c->bpc = ((c->bpc - 1 + ast->codecpar->block_align) / ast->codecpar->block_align) * ast->codecpar->block_align; | |
| 129 | } | ||
| 130 | 2 | c->bytes_left = fsize; | |
| 131 | 2 | c->wavpos = avio_tell(pb); | |
| 132 | 2 | avio_seek(pb, c->vidpos, SEEK_SET); | |
| 133 | } | ||
| 134 | |||
| 135 | /* now we are ready: build format streams */ | ||
| 136 | 3 | st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; | |
| 137 | 3 | st->codecpar->codec_id = AV_CODEC_ID_DXA; | |
| 138 | 3 | st->codecpar->width = w; | |
| 139 | 3 | st->codecpar->height = h; | |
| 140 | 3 | av_reduce(&den, &num, den, num, (1UL<<31)-1); | |
| 141 | 3 | avpriv_set_pts_info(st, 33, num, den); | |
| 142 | /* flags & 0x80 means that image is interlaced, | ||
| 143 | * flags & 0x40 means that image has double height | ||
| 144 | * either way set true height | ||
| 145 | */ | ||
| 146 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
|
3 | if(flags & 0xC0){ |
| 147 | 1 | st->codecpar->height >>= 1; | |
| 148 | } | ||
| 149 | 3 | c->readvid = !c->has_sound; | |
| 150 | 3 | c->vidpos = avio_tell(pb); | |
| 151 | 3 | s->start_time = 0; | |
| 152 | 3 | s->duration = av_rescale(c->frames, AV_TIME_BASE * (int64_t)num, den); | |
| 153 | 3 | av_log(s, AV_LOG_DEBUG, "%d frame(s)\n",c->frames); | |
| 154 | |||
| 155 | 3 | return 0; | |
| 156 | } | ||
| 157 | |||
| 158 | 172 | static int dxa_read_packet(AVFormatContext *s, AVPacket *pkt) | |
| 159 | { | ||
| 160 | 172 | DXAContext *c = s->priv_data; | |
| 161 | int ret; | ||
| 162 | uint32_t size; | ||
| 163 | uint8_t buf[DXA_EXTRA_SIZE], pal[768+4]; | ||
| 164 | 172 | int pal_size = 0; | |
| 165 | |||
| 166 |
5/6✓ Branch 0 taken 93 times.
✓ Branch 1 taken 79 times.
✓ Branch 2 taken 80 times.
✓ Branch 3 taken 13 times.
✓ Branch 4 taken 80 times.
✗ Branch 5 not taken.
|
172 | if(!c->readvid && c->has_sound && c->bytes_left){ |
| 167 | 80 | c->readvid = 1; | |
| 168 | 80 | avio_seek(s->pb, c->wavpos, SEEK_SET); | |
| 169 | 80 | size = FFMIN(c->bytes_left, c->bpc); | |
| 170 | 80 | ret = av_get_packet(s->pb, pkt, size); | |
| 171 | 80 | pkt->stream_index = 1; | |
| 172 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 80 times.
|
80 | if(ret != size) |
| 173 | ✗ | return AVERROR(EIO); | |
| 174 | 80 | c->bytes_left -= size; | |
| 175 | 80 | c->wavpos = avio_tell(s->pb); | |
| 176 | 80 | return 0; | |
| 177 | } | ||
| 178 | 92 | avio_seek(s->pb, c->vidpos, SEEK_SET); | |
| 179 |
2/4✓ Branch 1 taken 95 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 95 times.
✗ Branch 4 not taken.
|
95 | while(!avio_feof(s->pb) && c->frames){ |
| 180 | uint32_t tag; | ||
| 181 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 95 times.
|
95 | if ((ret = avio_read(s->pb, buf, 4)) != 4) { |
| 182 | ✗ | av_log(s, AV_LOG_ERROR, "failed reading chunk type\n"); | |
| 183 | ✗ | return ret < 0 ? ret : AVERROR_INVALIDDATA; | |
| 184 | } | ||
| 185 | 95 | tag = AV_RL32(buf); | |
| 186 |
3/4✓ Branch 0 taken 44 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 48 times.
✗ Branch 3 not taken.
|
95 | switch (tag) { |
| 187 | 44 | case MKTAG('N', 'U', 'L', 'L'): | |
| 188 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 44 times.
|
44 | if ((ret = av_new_packet(pkt, 4 + pal_size)) < 0) |
| 189 | 92 | return ret; | |
| 190 | 44 | pkt->stream_index = 0; | |
| 191 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 44 times.
|
44 | if(pal_size) memcpy(pkt->data, pal, pal_size); |
| 192 | 44 | memcpy(pkt->data + pal_size, buf, 4); | |
| 193 | 44 | c->frames--; | |
| 194 | 44 | c->vidpos = avio_tell(s->pb); | |
| 195 | 44 | c->readvid = 0; | |
| 196 | 44 | return 0; | |
| 197 | 3 | case MKTAG('C', 'M', 'A', 'P'): | |
| 198 | 3 | pal_size = 768+4; | |
| 199 | 3 | memcpy(pal, buf, 4); | |
| 200 | 3 | avio_read(s->pb, pal + 4, 768); | |
| 201 | 3 | break; | |
| 202 | 48 | case MKTAG('F', 'R', 'A', 'M'): | |
| 203 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 48 times.
|
48 | if ((ret = avio_read(s->pb, buf + 4, DXA_EXTRA_SIZE - 4)) != DXA_EXTRA_SIZE - 4) { |
| 204 | ✗ | av_log(s, AV_LOG_ERROR, "failed reading dxa_extra\n"); | |
| 205 | ✗ | return ret < 0 ? ret : AVERROR_INVALIDDATA; | |
| 206 | } | ||
| 207 | 48 | size = AV_RB32(buf + 5); | |
| 208 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
|
48 | if(size > 0xFFFFFF){ |
| 209 | ✗ | av_log(s, AV_LOG_ERROR, "Frame size is too big: %"PRIu32"\n", | |
| 210 | size); | ||
| 211 | ✗ | return AVERROR_INVALIDDATA; | |
| 212 | } | ||
| 213 | 48 | ret = av_new_packet(pkt, size + DXA_EXTRA_SIZE + pal_size); | |
| 214 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
|
48 | if (ret < 0) |
| 215 | ✗ | return ret; | |
| 216 | 48 | memcpy(pkt->data + pal_size, buf, DXA_EXTRA_SIZE); | |
| 217 | 48 | ret = avio_read(s->pb, pkt->data + DXA_EXTRA_SIZE + pal_size, size); | |
| 218 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 46 times.
|
48 | if(ret != size){ |
| 219 | 2 | return AVERROR(EIO); | |
| 220 | } | ||
| 221 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 43 times.
|
46 | if(pal_size) memcpy(pkt->data, pal, pal_size); |
| 222 | 46 | pkt->stream_index = 0; | |
| 223 | 46 | c->frames--; | |
| 224 | 46 | c->vidpos = avio_tell(s->pb); | |
| 225 | 46 | c->readvid = 0; | |
| 226 | 46 | return 0; | |
| 227 | ✗ | default: | |
| 228 | ✗ | av_log(s, AV_LOG_ERROR, "Unknown tag %s\n", av_fourcc2str(tag)); | |
| 229 | ✗ | return AVERROR_INVALIDDATA; | |
| 230 | } | ||
| 231 | } | ||
| 232 | ✗ | return AVERROR_EOF; | |
| 233 | } | ||
| 234 | |||
| 235 | const FFInputFormat ff_dxa_demuxer = { | ||
| 236 | .p.name = "dxa", | ||
| 237 | .p.long_name = NULL_IF_CONFIG_SMALL("DXA"), | ||
| 238 | .priv_data_size = sizeof(DXAContext), | ||
| 239 | .read_probe = dxa_probe, | ||
| 240 | .read_header = dxa_read_header, | ||
| 241 | .read_packet = dxa_read_packet, | ||
| 242 | }; | ||
| 243 |