| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Gremlin Digital Video demuxer | ||
| 3 | * Copyright (c) 2017 Paul B Mahol | ||
| 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 "libavutil/intreadwrite.h" | ||
| 23 | |||
| 24 | #include "avformat.h" | ||
| 25 | #include "avio.h" | ||
| 26 | #include "demux.h" | ||
| 27 | #include "internal.h" | ||
| 28 | |||
| 29 | typedef struct GDVContext { | ||
| 30 | int is_first_video; | ||
| 31 | int is_audio; | ||
| 32 | int audio_size; | ||
| 33 | int audio_stream_index; | ||
| 34 | int video_stream_index; | ||
| 35 | unsigned pal[256]; | ||
| 36 | } GDVContext; | ||
| 37 | |||
| 38 | 7474 | static int gdv_read_probe(const AVProbeData *p) | |
| 39 | { | ||
| 40 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7474 times.
|
7474 | if (AV_RL32(p->buf) == 0x29111994) |
| 41 | ✗ | return AVPROBE_SCORE_MAX; | |
| 42 | |||
| 43 | 7474 | return 0; | |
| 44 | } | ||
| 45 | |||
| 46 | static struct { | ||
| 47 | uint16_t id; | ||
| 48 | uint16_t width; | ||
| 49 | uint16_t height; | ||
| 50 | } FixedSize[] = { | ||
| 51 | { 0, 320, 200}, | ||
| 52 | { 1, 640, 200}, | ||
| 53 | { 2, 320, 167}, | ||
| 54 | { 3, 320, 180}, | ||
| 55 | { 4, 320, 400}, | ||
| 56 | { 5, 320, 170}, | ||
| 57 | { 6, 160, 85}, | ||
| 58 | { 7, 160, 83}, | ||
| 59 | { 8, 160, 90}, | ||
| 60 | { 9, 280, 128}, | ||
| 61 | {10, 320, 240}, | ||
| 62 | {11, 320, 201}, | ||
| 63 | {16, 640, 400}, | ||
| 64 | {17, 640, 200}, | ||
| 65 | {18, 640, 180}, | ||
| 66 | {19, 640, 167}, | ||
| 67 | {20, 640, 170}, | ||
| 68 | {21, 320, 240} | ||
| 69 | }; | ||
| 70 | |||
| 71 | ✗ | static int gdv_read_header(AVFormatContext *ctx) | |
| 72 | { | ||
| 73 | ✗ | GDVContext *gdv = ctx->priv_data; | |
| 74 | ✗ | AVIOContext *pb = ctx->pb; | |
| 75 | AVStream *vst, *ast; | ||
| 76 | unsigned fps, snd_flags, vid_depth, size_id; | ||
| 77 | |||
| 78 | ✗ | avio_skip(pb, 4); | |
| 79 | ✗ | size_id = avio_rl16(pb); | |
| 80 | |||
| 81 | ✗ | vst = avformat_new_stream(ctx, 0); | |
| 82 | ✗ | if (!vst) | |
| 83 | ✗ | return AVERROR(ENOMEM); | |
| 84 | |||
| 85 | ✗ | vst->start_time = 0; | |
| 86 | ✗ | vst->duration = | |
| 87 | ✗ | vst->nb_frames = avio_rl16(pb); | |
| 88 | |||
| 89 | ✗ | fps = avio_rl16(pb); | |
| 90 | ✗ | if (!fps) | |
| 91 | ✗ | return AVERROR_INVALIDDATA; | |
| 92 | |||
| 93 | ✗ | snd_flags = avio_rl16(pb); | |
| 94 | ✗ | if (snd_flags & 1) { | |
| 95 | ✗ | ast = avformat_new_stream(ctx, 0); | |
| 96 | ✗ | if (!ast) | |
| 97 | ✗ | return AVERROR(ENOMEM); | |
| 98 | |||
| 99 | ✗ | ast->start_time = 0; | |
| 100 | ✗ | ast->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; | |
| 101 | ✗ | ast->codecpar->codec_tag = 0; | |
| 102 | ✗ | ast->codecpar->sample_rate = avio_rl16(pb); | |
| 103 | ✗ | ast->codecpar->ch_layout.nb_channels = 1 + !!(snd_flags & 2); | |
| 104 | ✗ | if (snd_flags & 8) { | |
| 105 | ✗ | ast->codecpar->codec_id = AV_CODEC_ID_GREMLIN_DPCM; | |
| 106 | } else { | ||
| 107 | ✗ | ast->codecpar->codec_id = (snd_flags & 4) ? AV_CODEC_ID_PCM_S16LE : AV_CODEC_ID_PCM_U8; | |
| 108 | } | ||
| 109 | |||
| 110 | ✗ | avpriv_set_pts_info(ast, 64, 1, ast->codecpar->sample_rate); | |
| 111 | ✗ | gdv->audio_size = (ast->codecpar->sample_rate / fps) * | |
| 112 | ✗ | ast->codecpar->ch_layout.nb_channels * | |
| 113 | ✗ | (1 + !!(snd_flags & 4)) / (1 + !!(snd_flags & 8)); | |
| 114 | ✗ | gdv->is_audio = 1; | |
| 115 | } else { | ||
| 116 | ✗ | avio_skip(pb, 2); | |
| 117 | } | ||
| 118 | ✗ | vid_depth = avio_rl16(pb); | |
| 119 | ✗ | avio_skip(pb, 4); | |
| 120 | |||
| 121 | ✗ | vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; | |
| 122 | ✗ | vst->codecpar->codec_id = AV_CODEC_ID_GDV; | |
| 123 | ✗ | vst->codecpar->codec_tag = 0; | |
| 124 | ✗ | vst->codecpar->width = avio_rl16(pb); | |
| 125 | ✗ | vst->codecpar->height = avio_rl16(pb); | |
| 126 | |||
| 127 | ✗ | if (vst->codecpar->width == 0 || vst->codecpar->height == 0) { | |
| 128 | int i; | ||
| 129 | |||
| 130 | ✗ | for (i = 0; i < FF_ARRAY_ELEMS(FixedSize) - 1; i++) { | |
| 131 | ✗ | if (FixedSize[i].id == size_id) | |
| 132 | ✗ | break; | |
| 133 | } | ||
| 134 | |||
| 135 | ✗ | vst->codecpar->width = FixedSize[i].width; | |
| 136 | ✗ | vst->codecpar->height = FixedSize[i].height; | |
| 137 | } | ||
| 138 | |||
| 139 | ✗ | avpriv_set_pts_info(vst, 64, 1, fps); | |
| 140 | |||
| 141 | ✗ | if (vid_depth & 1) { | |
| 142 | int i; | ||
| 143 | |||
| 144 | ✗ | for (i = 0; i < 256; i++) { | |
| 145 | ✗ | unsigned r = avio_r8(pb); | |
| 146 | ✗ | unsigned g = avio_r8(pb); | |
| 147 | ✗ | unsigned b = avio_r8(pb); | |
| 148 | ✗ | gdv->pal[i] = 0xFFU << 24 | r << 18 | g << 10 | b << 2; | |
| 149 | } | ||
| 150 | } | ||
| 151 | |||
| 152 | ✗ | gdv->is_first_video = 1; | |
| 153 | |||
| 154 | ✗ | return 0; | |
| 155 | } | ||
| 156 | |||
| 157 | ✗ | static int gdv_read_packet(AVFormatContext *ctx, AVPacket *pkt) | |
| 158 | { | ||
| 159 | ✗ | GDVContext *gdv = ctx->priv_data; | |
| 160 | ✗ | AVIOContext *pb = ctx->pb; | |
| 161 | int ret; | ||
| 162 | |||
| 163 | ✗ | if (avio_feof(pb)) | |
| 164 | ✗ | return pb->error ? pb->error : AVERROR_EOF; | |
| 165 | |||
| 166 | ✗ | if (gdv->audio_size && gdv->is_audio) { | |
| 167 | ✗ | ret = av_get_packet(pb, pkt, gdv->audio_size); | |
| 168 | ✗ | if (ret < 0) | |
| 169 | ✗ | return ret; | |
| 170 | ✗ | pkt->stream_index = 1; | |
| 171 | ✗ | gdv->is_audio = 0; | |
| 172 | } else { | ||
| 173 | uint8_t *pal; | ||
| 174 | |||
| 175 | ✗ | if (avio_rl16(pb) != 0x1305) | |
| 176 | ✗ | return AVERROR_INVALIDDATA; | |
| 177 | ✗ | ret = av_get_packet(pb, pkt, 4 + avio_rl16(pb)); | |
| 178 | ✗ | if (ret < 0) | |
| 179 | ✗ | return ret; | |
| 180 | ✗ | pkt->stream_index = 0; | |
| 181 | ✗ | gdv->is_audio = 1; | |
| 182 | |||
| 183 | ✗ | if (gdv->is_first_video) { | |
| 184 | ✗ | pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE, | |
| 185 | AVPALETTE_SIZE); | ||
| 186 | ✗ | if (!pal) { | |
| 187 | ✗ | return AVERROR(ENOMEM); | |
| 188 | } | ||
| 189 | ✗ | memcpy(pal, gdv->pal, AVPALETTE_SIZE); | |
| 190 | ✗ | pkt->flags |= AV_PKT_FLAG_KEY; | |
| 191 | ✗ | gdv->is_first_video = 0; | |
| 192 | } | ||
| 193 | } | ||
| 194 | |||
| 195 | ✗ | return 0; | |
| 196 | } | ||
| 197 | |||
| 198 | const FFInputFormat ff_gdv_demuxer = { | ||
| 199 | .p.name = "gdv", | ||
| 200 | .p.long_name = NULL_IF_CONFIG_SMALL("Gremlin Digital Video"), | ||
| 201 | .priv_data_size = sizeof(GDVContext), | ||
| 202 | .read_probe = gdv_read_probe, | ||
| 203 | .read_header = gdv_read_header, | ||
| 204 | .read_packet = gdv_read_packet, | ||
| 205 | }; | ||
| 206 |