| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Delphine Software International CIN File Demuxer | ||
| 3 | * Copyright (c) 2006 Gregory Montoir (cyx@users.sourceforge.net) | ||
| 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 | * Delphine Software International CIN file demuxer | ||
| 25 | */ | ||
| 26 | |||
| 27 | #include "libavutil/channel_layout.h" | ||
| 28 | #include "libavutil/intreadwrite.h" | ||
| 29 | #include "avformat.h" | ||
| 30 | #include "demux.h" | ||
| 31 | #include "internal.h" | ||
| 32 | #include "avio_internal.h" | ||
| 33 | |||
| 34 | |||
| 35 | typedef struct CinFileHeader { | ||
| 36 | int video_frame_size; | ||
| 37 | int video_frame_width; | ||
| 38 | int video_frame_height; | ||
| 39 | int audio_frequency; | ||
| 40 | int audio_bits; | ||
| 41 | int audio_stereo; | ||
| 42 | int audio_frame_size; | ||
| 43 | } CinFileHeader; | ||
| 44 | |||
| 45 | typedef struct CinFrameHeader { | ||
| 46 | int audio_frame_type; | ||
| 47 | int video_frame_type; | ||
| 48 | int pal_colors_count; | ||
| 49 | int audio_frame_size; | ||
| 50 | int video_frame_size; | ||
| 51 | } CinFrameHeader; | ||
| 52 | |||
| 53 | typedef struct CinDemuxContext { | ||
| 54 | int audio_stream_index; | ||
| 55 | int video_stream_index; | ||
| 56 | CinFileHeader file_header; | ||
| 57 | int64_t audio_stream_pts; | ||
| 58 | int64_t video_stream_pts; | ||
| 59 | CinFrameHeader frame_header; | ||
| 60 | int audio_buffer_size; | ||
| 61 | } CinDemuxContext; | ||
| 62 | |||
| 63 | |||
| 64 | 7480 | static int cin_probe(const AVProbeData *p) | |
| 65 | { | ||
| 66 | /* header starts with this special marker */ | ||
| 67 |
2/2✓ Branch 0 taken 7478 times.
✓ Branch 1 taken 2 times.
|
7480 | if (AV_RL32(&p->buf[0]) != 0x55AA0000) |
| 68 | 7478 | return 0; | |
| 69 | |||
| 70 | /* for accuracy, check some header field values */ | ||
| 71 |
3/6✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
|
2 | if (AV_RL32(&p->buf[12]) != 22050 || p->buf[16] != 16 || p->buf[17] != 0) |
| 72 | ✗ | return 0; | |
| 73 | |||
| 74 | 2 | return AVPROBE_SCORE_MAX; | |
| 75 | } | ||
| 76 | |||
| 77 | 2 | static int cin_read_file_header(CinDemuxContext *cin, AVIOContext *pb) { | |
| 78 | 2 | CinFileHeader *hdr = &cin->file_header; | |
| 79 | |||
| 80 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
|
2 | if (avio_rl32(pb) != 0x55AA0000) |
| 81 | ✗ | return AVERROR_INVALIDDATA; | |
| 82 | |||
| 83 | 2 | hdr->video_frame_size = avio_rl32(pb); | |
| 84 | 2 | hdr->video_frame_width = avio_rl16(pb); | |
| 85 | 2 | hdr->video_frame_height = avio_rl16(pb); | |
| 86 | 2 | hdr->audio_frequency = avio_rl32(pb); | |
| 87 | 2 | hdr->audio_bits = avio_r8(pb); | |
| 88 | 2 | hdr->audio_stereo = avio_r8(pb); | |
| 89 | 2 | hdr->audio_frame_size = avio_rl16(pb); | |
| 90 | |||
| 91 |
3/6✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
|
2 | if (hdr->audio_frequency != 22050 || hdr->audio_bits != 16 || hdr->audio_stereo != 0) |
| 92 | ✗ | return AVERROR_INVALIDDATA; | |
| 93 | |||
| 94 | 2 | return 0; | |
| 95 | } | ||
| 96 | |||
| 97 | 2 | static int cin_read_header(AVFormatContext *s) | |
| 98 | { | ||
| 99 | int rc; | ||
| 100 | 2 | CinDemuxContext *cin = s->priv_data; | |
| 101 | 2 | CinFileHeader *hdr = &cin->file_header; | |
| 102 | 2 | AVIOContext *pb = s->pb; | |
| 103 | AVStream *st; | ||
| 104 | |||
| 105 | 2 | rc = cin_read_file_header(cin, pb); | |
| 106 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (rc) |
| 107 | ✗ | return rc; | |
| 108 | |||
| 109 | 2 | cin->video_stream_pts = 0; | |
| 110 | 2 | cin->audio_stream_pts = 0; | |
| 111 | 2 | cin->audio_buffer_size = 0; | |
| 112 | |||
| 113 | /* initialize the video decoder stream */ | ||
| 114 | 2 | st = avformat_new_stream(s, NULL); | |
| 115 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!st) |
| 116 | ✗ | return AVERROR(ENOMEM); | |
| 117 | |||
| 118 | 2 | avpriv_set_pts_info(st, 32, 1, 12); | |
| 119 | 2 | cin->video_stream_index = st->index; | |
| 120 | 2 | st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; | |
| 121 | 2 | st->codecpar->codec_id = AV_CODEC_ID_DSICINVIDEO; | |
| 122 | 2 | st->codecpar->codec_tag = 0; /* no fourcc */ | |
| 123 | 2 | st->codecpar->width = hdr->video_frame_width; | |
| 124 | 2 | st->codecpar->height = hdr->video_frame_height; | |
| 125 | |||
| 126 | /* initialize the audio decoder stream */ | ||
| 127 | 2 | st = avformat_new_stream(s, NULL); | |
| 128 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!st) |
| 129 | ✗ | return AVERROR(ENOMEM); | |
| 130 | |||
| 131 | 2 | avpriv_set_pts_info(st, 32, 1, 22050); | |
| 132 | 2 | cin->audio_stream_index = st->index; | |
| 133 | 2 | st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; | |
| 134 | 2 | st->codecpar->codec_id = AV_CODEC_ID_DSICINAUDIO; | |
| 135 | 2 | st->codecpar->codec_tag = 0; /* no tag */ | |
| 136 | 2 | st->codecpar->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO; | |
| 137 | 2 | st->codecpar->sample_rate = 22050; | |
| 138 | 2 | st->codecpar->bits_per_coded_sample = 8; | |
| 139 | 2 | st->codecpar->bit_rate = st->codecpar->sample_rate * | |
| 140 | 2 | st->codecpar->bits_per_coded_sample * | |
| 141 | 2 | st->codecpar->ch_layout.nb_channels; | |
| 142 | |||
| 143 | 2 | return 0; | |
| 144 | } | ||
| 145 | |||
| 146 | 184 | static int cin_read_frame_header(CinDemuxContext *cin, AVIOContext *pb) { | |
| 147 | 184 | CinFrameHeader *hdr = &cin->frame_header; | |
| 148 | |||
| 149 | 184 | hdr->video_frame_type = avio_r8(pb); | |
| 150 | 184 | hdr->audio_frame_type = avio_r8(pb); | |
| 151 | 184 | hdr->pal_colors_count = avio_rl16(pb); | |
| 152 | 184 | hdr->video_frame_size = avio_rl32(pb); | |
| 153 | 184 | hdr->audio_frame_size = avio_rl32(pb); | |
| 154 | |||
| 155 |
2/4✓ Branch 1 taken 184 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 184 times.
|
184 | if (avio_feof(pb) || pb->error) |
| 156 | ✗ | return pb->error ? pb->error : AVERROR_INVALIDDATA; | |
| 157 | |||
| 158 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 184 times.
|
184 | if (avio_rl32(pb) != 0xAA55AA55) |
| 159 | ✗ | return AVERROR_INVALIDDATA; | |
| 160 |
2/4✓ Branch 0 taken 184 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 184 times.
|
184 | if (hdr->video_frame_size < 0 || hdr->audio_frame_size < 0) |
| 161 | ✗ | return AVERROR_INVALIDDATA; | |
| 162 | |||
| 163 | 184 | return 0; | |
| 164 | } | ||
| 165 | |||
| 166 | 368 | static int cin_read_packet(AVFormatContext *s, AVPacket *pkt) | |
| 167 | { | ||
| 168 | 368 | CinDemuxContext *cin = s->priv_data; | |
| 169 | 368 | AVIOContext *pb = s->pb; | |
| 170 | 368 | CinFrameHeader *hdr = &cin->frame_header; | |
| 171 | int rc, palette_type; | ||
| 172 | int64_t pkt_size; | ||
| 173 | int ret; | ||
| 174 | |||
| 175 |
2/2✓ Branch 0 taken 184 times.
✓ Branch 1 taken 184 times.
|
368 | if (cin->audio_buffer_size == 0) { |
| 176 | 184 | rc = cin_read_frame_header(cin, pb); | |
| 177 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 184 times.
|
184 | if (rc) |
| 178 | ✗ | return rc; | |
| 179 | |||
| 180 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 184 times.
|
184 | if ((int16_t)hdr->pal_colors_count < 0) { |
| 181 | ✗ | hdr->pal_colors_count = -(int16_t)hdr->pal_colors_count; | |
| 182 | ✗ | palette_type = 1; | |
| 183 | } else { | ||
| 184 | 184 | palette_type = 0; | |
| 185 | } | ||
| 186 | |||
| 187 | /* palette and video packet */ | ||
| 188 | 184 | pkt_size = (palette_type + 3LL) * hdr->pal_colors_count + hdr->video_frame_size; | |
| 189 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 184 times.
|
184 | if (pkt_size + 4 > INT_MAX) |
| 190 | ✗ | return AVERROR_INVALIDDATA; | |
| 191 | |||
| 192 | 184 | pkt_size = ffio_limit(pb, pkt_size); | |
| 193 | |||
| 194 | 184 | ret = av_new_packet(pkt, 4 + pkt_size); | |
| 195 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 184 times.
|
184 | if (ret < 0) |
| 196 | ✗ | return ret; | |
| 197 | |||
| 198 | 184 | pkt->stream_index = cin->video_stream_index; | |
| 199 | 184 | pkt->pts = cin->video_stream_pts++; | |
| 200 | |||
| 201 | 184 | pkt->data[0] = palette_type; | |
| 202 | 184 | pkt->data[1] = hdr->pal_colors_count & 0xFF; | |
| 203 | 184 | pkt->data[2] = hdr->pal_colors_count >> 8; | |
| 204 | 184 | pkt->data[3] = hdr->video_frame_type; | |
| 205 | |||
| 206 | 184 | ret = avio_read(pb, &pkt->data[4], pkt_size); | |
| 207 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 184 times.
|
184 | if (ret < 0) { |
| 208 | ✗ | return ret; | |
| 209 | } | ||
| 210 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 184 times.
|
184 | if (ret < pkt_size) |
| 211 | ✗ | av_shrink_packet(pkt, 4 + ret); | |
| 212 | |||
| 213 | /* sound buffer will be processed on next read_packet() call */ | ||
| 214 | 184 | cin->audio_buffer_size = hdr->audio_frame_size; | |
| 215 | 184 | return 0; | |
| 216 | } | ||
| 217 | |||
| 218 | /* audio packet */ | ||
| 219 | 184 | ret = av_get_packet(pb, pkt, cin->audio_buffer_size); | |
| 220 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 182 times.
|
184 | if (ret < 0) |
| 221 | 2 | return ret; | |
| 222 | |||
| 223 | 182 | pkt->stream_index = cin->audio_stream_index; | |
| 224 | 182 | pkt->pts = cin->audio_stream_pts; | |
| 225 | 182 | pkt->duration = cin->audio_buffer_size - (pkt->pts == 0); | |
| 226 | 182 | cin->audio_stream_pts += pkt->duration; | |
| 227 | 182 | cin->audio_buffer_size = 0; | |
| 228 | 182 | return 0; | |
| 229 | } | ||
| 230 | |||
| 231 | const FFInputFormat ff_dsicin_demuxer = { | ||
| 232 | .p.name = "dsicin", | ||
| 233 | .p.long_name = NULL_IF_CONFIG_SMALL("Delphine Software International CIN"), | ||
| 234 | .priv_data_size = sizeof(CinDemuxContext), | ||
| 235 | .read_probe = cin_probe, | ||
| 236 | .read_header = cin_read_header, | ||
| 237 | .read_packet = cin_read_packet, | ||
| 238 | }; | ||
| 239 |