FFmpeg
Loading...
Searching...
No Matches
rtv1.c
Go to the documentation of this file.
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
33{
34 TextureDSPContext *dsp = avctx->priv_data;
35 avctx->pix_fmt = AV_PIX_FMT_BGR0;
37 return 0;
38}
39
40static 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
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)
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
96static 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;
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
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,
149};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
const FFCodec ff_rtv1_decoder
Definition rtv1.c:140
Libavcodec external API header.
static av_always_inline int bytestream2_get_bytes_left(const GetByteContext *g)
Definition bytestream.h:158
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition bytestream.h:137
#define flag(name)
Definition cbs_h264.c:60
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
static int16_t block[64]
Definition dct.c:125
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Definition utils.c:91
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition codec.h:98
@ AV_CODEC_ID_RTV1
Definition codec_id.h:317
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
int a
#define b
Definition input.c:43
#define AV_WL32(p, v)
#define AV_WL16(p, v)
static av_cold int decode_init(AVCodecContext *avctx)
Definition 4xm.c:998
static int decode_frame(AVCodecContext *avctx, AVFrame *picture, int *got_frame, AVPacket *avpkt)
Definition 4xm.c:837
Multithreading API for decoders.
#define av_cold
Definition attributes.h:117
#define MKTAG(a, b, c, d)
Definition macros.h:55
#define FFALIGN(x, a)
Definition macros.h:78
@ AV_PIX_FMT_BGR0
packed BGR 8:8:8, 32bpp, BGRXBGRX... X=unused/undefined
Definition pixfmt.h:265
int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
static int decode_rtv1(GetByteContext *gb, uint8_t *dst, ptrdiff_t linesize, int width, int height, int flag, int(*dxt1_block)(uint8_t *dst, ptrdiff_t stride, const uint8_t *block))
Definition rtv1.c:40
static av_cold int decode_init(AVCodecContext *avctx)
Definition rtv1.c:32
static int decode_frame(AVCodecContext *avctx, AVFrame *p, int *got_frame, AVPacket *avpkt)
Definition rtv1.c:96
main external API structure.
Definition avcodec.h:443
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition avcodec.h:643
int width
picture width / height.
Definition avcodec.h:604
void * priv_data
Definition avcodec.h:470
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
This structure stores compressed data.
Definition packet.h:580
int size
Definition packet.h:604
uint8_t * data
Definition packet.h:603
int(* dxt1_block)(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
Definition texturedsp.h:46
Definition swscale.c:71
uint8_t run
Definition svq3.c:207
#define stride
#define height
Definition dsp.h:89
#define width
Definition dsp.h:89
av_cold void ff_texturedsp_init(TextureDSPContext *c)
Definition texturedsp.c:640
static int dxt1_block(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
Decompress one block of a DXT1 texture and store the resulting RGBA pixels in 'dst'.
Definition texturedsp.c:115
Texture block (4x4) module.