| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * id RoQ (.roq) File Demuxer | ||
| 3 | * Copyright (c) 2003 The FFmpeg project | ||
| 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 | * id RoQ format file demuxer | ||
| 25 | * by Mike Melanson (melanson@pcisys.net) | ||
| 26 | * for more information on the .roq file format, visit: | ||
| 27 | * http://www.csse.monash.edu.au/~timf/ | ||
| 28 | */ | ||
| 29 | |||
| 30 | #include "libavutil/channel_layout.h" | ||
| 31 | #include "libavutil/intreadwrite.h" | ||
| 32 | #include "avformat.h" | ||
| 33 | #include "demux.h" | ||
| 34 | #include "internal.h" | ||
| 35 | #include "avio_internal.h" | ||
| 36 | |||
| 37 | #define RoQ_MAGIC_NUMBER 0x1084 | ||
| 38 | #define RoQ_CHUNK_PREAMBLE_SIZE 8 | ||
| 39 | #define RoQ_AUDIO_SAMPLE_RATE 22050 | ||
| 40 | #define RoQ_CHUNKS_TO_SCAN 30 | ||
| 41 | |||
| 42 | #define RoQ_INFO 0x1001 | ||
| 43 | #define RoQ_QUAD_CODEBOOK 0x1002 | ||
| 44 | #define RoQ_QUAD_VQ 0x1011 | ||
| 45 | #define RoQ_SOUND_MONO 0x1020 | ||
| 46 | #define RoQ_SOUND_STEREO 0x1021 | ||
| 47 | |||
| 48 | typedef struct RoqDemuxContext { | ||
| 49 | |||
| 50 | int frame_rate; | ||
| 51 | int width; | ||
| 52 | int height; | ||
| 53 | int audio_channels; | ||
| 54 | |||
| 55 | int video_stream_index; | ||
| 56 | int audio_stream_index; | ||
| 57 | |||
| 58 | int64_t video_pts; | ||
| 59 | unsigned int audio_frame_count; | ||
| 60 | |||
| 61 | } RoqDemuxContext; | ||
| 62 | |||
| 63 | 7279 | static int roq_probe(const AVProbeData *p) | |
| 64 | { | ||
| 65 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 7272 times.
|
7279 | if ((AV_RL16(&p->buf[0]) != RoQ_MAGIC_NUMBER) || |
| 66 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
|
7 | (AV_RL32(&p->buf[2]) != 0xFFFFFFFF)) |
| 67 | 7272 | return 0; | |
| 68 | |||
| 69 | 7 | return AVPROBE_SCORE_MAX; | |
| 70 | } | ||
| 71 | |||
| 72 | 7 | static int roq_read_header(AVFormatContext *s) | |
| 73 | { | ||
| 74 | 7 | RoqDemuxContext *roq = s->priv_data; | |
| 75 | 7 | AVIOContext *pb = s->pb; | |
| 76 | unsigned char preamble[RoQ_CHUNK_PREAMBLE_SIZE]; | ||
| 77 | |||
| 78 | /* get the main header */ | ||
| 79 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
|
7 | if (avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) != |
| 80 | RoQ_CHUNK_PREAMBLE_SIZE) | ||
| 81 | ✗ | return AVERROR(EIO); | |
| 82 | 7 | roq->frame_rate = AV_RL16(&preamble[6]); | |
| 83 | |||
| 84 | /* init private context parameters */ | ||
| 85 | 7 | roq->width = roq->height = roq->audio_channels = roq->video_pts = | |
| 86 | 7 | roq->audio_frame_count = 0; | |
| 87 | 7 | roq->audio_stream_index = -1; | |
| 88 | 7 | roq->video_stream_index = -1; | |
| 89 | |||
| 90 | 7 | s->ctx_flags |= AVFMTCTX_NOHEADER; | |
| 91 | |||
| 92 | 7 | return 0; | |
| 93 | } | ||
| 94 | |||
| 95 | 1113 | static int roq_read_packet(AVFormatContext *s, | |
| 96 | AVPacket *pkt) | ||
| 97 | { | ||
| 98 | 1113 | RoqDemuxContext *roq = s->priv_data; | |
| 99 | 1113 | AVIOContext *pb = s->pb; | |
| 100 | 1113 | int ret = 0; | |
| 101 | unsigned int chunk_size; | ||
| 102 | unsigned int chunk_type; | ||
| 103 | unsigned int codebook_size; | ||
| 104 | unsigned char preamble[RoQ_CHUNK_PREAMBLE_SIZE]; | ||
| 105 | 1113 | int packet_read = 0; | |
| 106 | int64_t codebook_offset; | ||
| 107 | |||
| 108 |
2/2✓ Branch 0 taken 1145 times.
✓ Branch 1 taken 1077 times.
|
2222 | while (!packet_read) { |
| 109 | |||
| 110 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 2 taken 1142 times.
|
1145 | if (avio_feof(s->pb)) |
| 111 | 3 | return AVERROR_EOF; | |
| 112 | |||
| 113 | /* get the next chunk preamble */ | ||
| 114 |
2/2✓ Branch 1 taken 33 times.
✓ Branch 2 taken 1109 times.
|
1142 | if ((ret = avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE)) != |
| 115 | RoQ_CHUNK_PREAMBLE_SIZE) | ||
| 116 | 33 | return AVERROR(EIO); | |
| 117 | |||
| 118 | 1109 | chunk_type = AV_RL16(&preamble[0]); | |
| 119 | 1109 | chunk_size = AV_RL32(&preamble[2]); | |
| 120 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1109 times.
|
1109 | if(chunk_size > INT_MAX) |
| 121 | ✗ | return AVERROR_INVALIDDATA; | |
| 122 | |||
| 123 | 1109 | chunk_size = ffio_limit(pb, chunk_size); | |
| 124 | |||
| 125 |
4/5✓ Branch 0 taken 32 times.
✓ Branch 1 taken 526 times.
✓ Branch 2 taken 507 times.
✓ Branch 3 taken 44 times.
✗ Branch 4 not taken.
|
1109 | switch (chunk_type) { |
| 126 | |||
| 127 | 32 | case RoQ_INFO: | |
| 128 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 26 times.
|
32 | if (roq->video_stream_index == -1) { |
| 129 | 6 | AVStream *st = avformat_new_stream(s, NULL); | |
| 130 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (!st) |
| 131 | ✗ | return AVERROR(ENOMEM); | |
| 132 | 6 | avpriv_set_pts_info(st, 63, 1, roq->frame_rate); | |
| 133 | 6 | roq->video_stream_index = st->index; | |
| 134 | 6 | st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; | |
| 135 | 6 | st->codecpar->codec_id = AV_CODEC_ID_ROQ; | |
| 136 | 6 | st->codecpar->codec_tag = 0; /* no fourcc */ | |
| 137 | |||
| 138 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
|
6 | if (avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) != RoQ_CHUNK_PREAMBLE_SIZE) |
| 139 | ✗ | return AVERROR(EIO); | |
| 140 | 6 | st->codecpar->width = roq->width = AV_RL16(preamble); | |
| 141 | 6 | st->codecpar->height = roq->height = AV_RL16(preamble + 2); | |
| 142 | 6 | break; | |
| 143 | } | ||
| 144 | /* don't care about this chunk anymore */ | ||
| 145 | 26 | avio_skip(pb, RoQ_CHUNK_PREAMBLE_SIZE); | |
| 146 | 26 | break; | |
| 147 | |||
| 148 | 526 | case RoQ_QUAD_CODEBOOK: | |
| 149 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 526 times.
|
526 | if (roq->video_stream_index < 0) |
| 150 | ✗ | return AVERROR_INVALIDDATA; | |
| 151 | /* packet needs to contain both this codebook and next VQ chunk */ | ||
| 152 | 526 | codebook_offset = avio_tell(pb) - RoQ_CHUNK_PREAMBLE_SIZE; | |
| 153 | 526 | codebook_size = chunk_size; | |
| 154 | 526 | avio_skip(pb, codebook_size); | |
| 155 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 526 times.
|
526 | if (avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) != |
| 156 | RoQ_CHUNK_PREAMBLE_SIZE) | ||
| 157 | ✗ | return AVERROR(EIO); | |
| 158 | 526 | chunk_size = AV_RL32(&preamble[2]) + RoQ_CHUNK_PREAMBLE_SIZE * 2 + | |
| 159 | codebook_size; | ||
| 160 | |||
| 161 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 526 times.
|
526 | if (chunk_size > INT_MAX) |
| 162 | ✗ | return AVERROR_INVALIDDATA; | |
| 163 | |||
| 164 | /* rewind */ | ||
| 165 | 526 | avio_seek(pb, codebook_offset, SEEK_SET); | |
| 166 | |||
| 167 | /* load up the packet */ | ||
| 168 | 526 | ret= av_get_packet(pb, pkt, chunk_size); | |
| 169 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 526 times.
|
526 | if (ret != chunk_size) |
| 170 | ✗ | return AVERROR(EIO); | |
| 171 | 526 | pkt->stream_index = roq->video_stream_index; | |
| 172 | 526 | pkt->pts = roq->video_pts++; | |
| 173 | |||
| 174 | 526 | packet_read = 1; | |
| 175 | 526 | break; | |
| 176 | |||
| 177 | 507 | case RoQ_SOUND_MONO: | |
| 178 | case RoQ_SOUND_STEREO: | ||
| 179 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 504 times.
|
507 | if (roq->audio_stream_index == -1) { |
| 180 | 3 | AVStream *st = avformat_new_stream(s, NULL); | |
| 181 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (!st) |
| 182 | ✗ | return AVERROR(ENOMEM); | |
| 183 | 3 | avpriv_set_pts_info(st, 32, 1, RoQ_AUDIO_SAMPLE_RATE); | |
| 184 | 3 | roq->audio_stream_index = st->index; | |
| 185 | 3 | st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; | |
| 186 | 3 | st->codecpar->codec_id = AV_CODEC_ID_ROQ_DPCM; | |
| 187 | 3 | st->codecpar->codec_tag = 0; /* no tag */ | |
| 188 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | if (chunk_type == RoQ_SOUND_STEREO) { |
| 189 | 3 | st->codecpar->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO; | |
| 190 | } else { | ||
| 191 | ✗ | st->codecpar->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO; | |
| 192 | } | ||
| 193 | 3 | roq->audio_channels = st->codecpar->ch_layout.nb_channels; | |
| 194 | 3 | st->codecpar->sample_rate = RoQ_AUDIO_SAMPLE_RATE; | |
| 195 | 3 | st->codecpar->bits_per_coded_sample = 16; | |
| 196 | 3 | st->codecpar->bit_rate = roq->audio_channels * st->codecpar->sample_rate * | |
| 197 | 3 | st->codecpar->bits_per_coded_sample; | |
| 198 | 3 | st->codecpar->block_align = roq->audio_channels * st->codecpar->bits_per_coded_sample; | |
| 199 | } | ||
| 200 | case RoQ_QUAD_VQ: | ||
| 201 |
2/2✓ Branch 0 taken 44 times.
✓ Branch 1 taken 507 times.
|
551 | if (chunk_type == RoQ_QUAD_VQ) { |
| 202 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 44 times.
|
44 | if (roq->video_stream_index < 0) |
| 203 | ✗ | return AVERROR_INVALIDDATA; | |
| 204 | } | ||
| 205 | |||
| 206 | /* load up the packet */ | ||
| 207 | 551 | ret = av_new_packet(pkt, chunk_size + RoQ_CHUNK_PREAMBLE_SIZE); | |
| 208 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 551 times.
|
551 | if (ret < 0) |
| 209 | ✗ | return ret; | |
| 210 | /* copy over preamble */ | ||
| 211 | 551 | memcpy(pkt->data, preamble, RoQ_CHUNK_PREAMBLE_SIZE); | |
| 212 | |||
| 213 |
2/2✓ Branch 0 taken 44 times.
✓ Branch 1 taken 507 times.
|
551 | if (chunk_type == RoQ_QUAD_VQ) { |
| 214 | 44 | pkt->stream_index = roq->video_stream_index; | |
| 215 | 44 | pkt->pts = roq->video_pts++; | |
| 216 | } else { | ||
| 217 | 507 | pkt->stream_index = roq->audio_stream_index; | |
| 218 | 507 | pkt->pts = roq->audio_frame_count; | |
| 219 | 507 | roq->audio_frame_count += (chunk_size / roq->audio_channels); | |
| 220 | } | ||
| 221 | |||
| 222 | 551 | pkt->pos= avio_tell(pb); | |
| 223 | 551 | ret = avio_read(pb, pkt->data + RoQ_CHUNK_PREAMBLE_SIZE, | |
| 224 | chunk_size); | ||
| 225 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 551 times.
|
551 | if (ret != chunk_size) { |
| 226 | ✗ | return AVERROR(EIO); | |
| 227 | } | ||
| 228 | |||
| 229 | 551 | packet_read = 1; | |
| 230 | 551 | break; | |
| 231 | |||
| 232 | ✗ | default: | |
| 233 | ✗ | av_log(s, AV_LOG_ERROR, " unknown RoQ chunk (%04X)\n", chunk_type); | |
| 234 | ✗ | return AVERROR_INVALIDDATA; | |
| 235 | } | ||
| 236 | } | ||
| 237 | |||
| 238 | 1077 | return ret; | |
| 239 | } | ||
| 240 | |||
| 241 | const FFInputFormat ff_roq_demuxer = { | ||
| 242 | .p.name = "roq", | ||
| 243 | .p.long_name = NULL_IF_CONFIG_SMALL("id RoQ"), | ||
| 244 | .priv_data_size = sizeof(RoqDemuxContext), | ||
| 245 | .read_probe = roq_probe, | ||
| 246 | .read_header = roq_read_header, | ||
| 247 | .read_packet = roq_read_packet, | ||
| 248 | }; | ||
| 249 |