| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * RTV1 decoder | ||
| 3 | * Copyright (c) 2023 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 <stdio.h> | ||
| 23 | #include <string.h> | ||
| 24 | |||
| 25 | #include "avcodec.h" | ||
| 26 | #include "bytestream.h" | ||
| 27 | #include "codec_internal.h" | ||
| 28 | #include "decode.h" | ||
| 29 | #include "texturedsp.h" | ||
| 30 | #include "thread.h" | ||
| 31 | |||
| 32 | ✗ | static av_cold int decode_init(AVCodecContext *avctx) | |
| 33 | { | ||
| 34 | ✗ | TextureDSPContext *dsp = avctx->priv_data; | |
| 35 | ✗ | avctx->pix_fmt = AV_PIX_FMT_BGR0; | |
| 36 | ✗ | ff_texturedsp_init(dsp); | |
| 37 | ✗ | return 0; | |
| 38 | } | ||
| 39 | |||
| 40 | ✗ | static int decode_rtv1(GetByteContext *gb, uint8_t *dst, ptrdiff_t linesize, | |
| 41 | int width, int height, int flag, | ||
| 42 | int (*dxt1_block)(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)) | ||
| 43 | { | ||
| 44 | ✗ | uint8_t block[8] = { 0 }; | |
| 45 | ✗ | int run = 0; | |
| 46 | |||
| 47 | ✗ | for (int y = 0; y < height; y += 4) { | |
| 48 | ✗ | for (int x = 0; x < width * 4; x += 16) { | |
| 49 | ✗ | int mode = 0; | |
| 50 | |||
| 51 | ✗ | if (run && --run > 0) { | |
| 52 | ✗ | dxt1_block(dst + x, linesize, block); | |
| 53 | } else { | ||
| 54 | int a, b; | ||
| 55 | |||
| 56 | ✗ | if (bytestream2_get_bytes_left(gb) < 4) | |
| 57 | ✗ | return AVERROR_INVALIDDATA; | |
| 58 | |||
| 59 | ✗ | a = bytestream2_get_le16u(gb); | |
| 60 | ✗ | b = bytestream2_get_le16u(gb); | |
| 61 | ✗ | if (a == b && flag) { | |
| 62 | ✗ | AV_WL32(block + 4, 0); | |
| 63 | ✗ | } else if (a == 1 && b == 0xffff) { | |
| 64 | ✗ | mode = 1; | |
| 65 | ✗ | } else if (b && a == 0) { | |
| 66 | ✗ | run = b; | |
| 67 | } else { | ||
| 68 | ✗ | AV_WL16(block, a); | |
| 69 | ✗ | AV_WL16(block + 2, b); | |
| 70 | ✗ | AV_WL32(block + 4, bytestream2_get_le32(gb)); | |
| 71 | } | ||
| 72 | ✗ | if (run && !mode) { | |
| 73 | ✗ | dxt1_block(dst + x, linesize, block); | |
| 74 | ✗ | } else if (!mode) { | |
| 75 | ✗ | AV_WL16(block, a); | |
| 76 | ✗ | AV_WL16(block + 2, b); | |
| 77 | ✗ | dxt1_block(dst + x, linesize, block); | |
| 78 | } else { | ||
| 79 | ✗ | if (bytestream2_get_bytes_left(gb) < 12 * 4) | |
| 80 | ✗ | return AVERROR_INVALIDDATA; | |
| 81 | |||
| 82 | ✗ | for (int by = 0; by < 4; by++) { | |
| 83 | ✗ | for (int bx = 0; bx < 4; bx++) | |
| 84 | ✗ | AV_WL32(dst + x + bx * 4 + by * linesize, bytestream2_get_le24u(gb)); | |
| 85 | } | ||
| 86 | } | ||
| 87 | } | ||
| 88 | } | ||
| 89 | |||
| 90 | ✗ | dst += linesize * 4; | |
| 91 | } | ||
| 92 | |||
| 93 | ✗ | return 0; | |
| 94 | } | ||
| 95 | |||
| 96 | ✗ | static int decode_frame(AVCodecContext *avctx, AVFrame *p, | |
| 97 | int *got_frame, AVPacket *avpkt) | ||
| 98 | { | ||
| 99 | int ret, width, height, flags; | ||
| 100 | ✗ | TextureDSPContext *dsp = avctx->priv_data; | |
| 101 | GetByteContext gb; | ||
| 102 | ptrdiff_t linesize; | ||
| 103 | uint8_t *dst; | ||
| 104 | |||
| 105 | ✗ | if (avpkt->size < 22) | |
| 106 | ✗ | return AVERROR_INVALIDDATA; | |
| 107 | |||
| 108 | ✗ | bytestream2_init(&gb, avpkt->data, avpkt->size); | |
| 109 | |||
| 110 | ✗ | if (bytestream2_get_le32(&gb) != MKTAG('D','X','T','1')) | |
| 111 | ✗ | return AVERROR_INVALIDDATA; | |
| 112 | ✗ | flags = bytestream2_get_le32(&gb); | |
| 113 | |||
| 114 | ✗ | width = bytestream2_get_le32(&gb); | |
| 115 | ✗ | height = bytestream2_get_le32(&gb); | |
| 116 | ✗ | if (width > INT_MAX-4U || height > INT_MAX-4U) | |
| 117 | ✗ | return AVERROR_INVALIDDATA; | |
| 118 | ✗ | ret = ff_set_dimensions(avctx, FFALIGN(width, 4), FFALIGN(height, 4)); | |
| 119 | ✗ | if (ret < 0) | |
| 120 | ✗ | return ret; | |
| 121 | |||
| 122 | ✗ | avctx->width = width; | |
| 123 | ✗ | avctx->height = height; | |
| 124 | |||
| 125 | ✗ | if ((ret = ff_thread_get_buffer(avctx, p, 0)) < 0) | |
| 126 | ✗ | return ret; | |
| 127 | |||
| 128 | ✗ | dst = p->data[0] + p->linesize[0] * (avctx->coded_height - 1); | |
| 129 | ✗ | linesize = -p->linesize[0]; | |
| 130 | |||
| 131 | ✗ | ret = decode_rtv1(&gb, dst, linesize, width, height, flags, dsp->dxt1_block); | |
| 132 | ✗ | if (ret < 0) | |
| 133 | ✗ | return ret; | |
| 134 | |||
| 135 | ✗ | *got_frame = 1; | |
| 136 | |||
| 137 | ✗ | return avpkt->size; | |
| 138 | } | ||
| 139 | |||
| 140 | const FFCodec ff_rtv1_decoder = { | ||
| 141 | .p.name = "rtv1", | ||
| 142 | CODEC_LONG_NAME("RTV1 (RivaTuner Video)"), | ||
| 143 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
| 144 | .p.id = AV_CODEC_ID_RTV1, | ||
| 145 | .priv_data_size = sizeof(TextureDSPContext), | ||
| 146 | .init = decode_init, | ||
| 147 | FF_CODEC_DECODE_CB(decode_frame), | ||
| 148 | .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS, | ||
| 149 | }; | ||
| 150 |