FFmpeg
Loading...
Searching...
No Matches
vbndec.c
Go to the documentation of this file.
1/*
2 * Vizrt Binary Image decoder
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21/**
22 * @file
23 * Vizrt Binary Image decoder
24 */
25
26#include "avcodec.h"
27#include "bytestream.h"
28#include "codec_internal.h"
29#include "decode.h"
30#include "texturedsp.h"
31#include "vbn.h"
32#include "libavutil/imgutils.h"
33#include "libavutil/mem.h"
34
39
41{
42 VBNContext *ctx = avctx->priv_data;
43 ff_texturedsp_init(&ctx->texdsp);
44 return 0;
45}
46
48 int compression, uint8_t **outbuf)
49{
50 if (compression == VBN_COMPRESSION_NONE) // outbuf is left NULL because gb->buf can be used directly
52
53 av_log(avctx, AV_LOG_ERROR, "Unsupported VBN compression: 0x%08x\n", compression);
55}
56
58 AVFrame *frame, int *got_frame,
59 AVPacket *avpkt)
60{
61 VBNContext *ctx = avctx->priv_data;
62 GetByteContext gb0, *const gb = &gb0;
63 uint8_t *image_buf = NULL;
64 int image_len;
65 int width, height, components, format, compression, pix_fmt, linesize, data_size;
66 int ret;
67
68 bytestream2_init(gb, avpkt->data, avpkt->size);
69
71 av_log(avctx, AV_LOG_ERROR, "VBN header truncated\n");
73 }
74
75 if (bytestream2_get_le32u(gb) != VBN_MAGIC ||
76 bytestream2_get_le32u(gb) != VBN_MAJOR ||
77 bytestream2_get_le32u(gb) != VBN_MINOR) {
78 av_log(avctx, AV_LOG_ERROR, "Invalid VBN header\n");
80 }
81
82 width = bytestream2_get_le32u(gb);
83 height = bytestream2_get_le32u(gb);
84 components = bytestream2_get_le32u(gb);
85 format = bytestream2_get_le32u(gb);
86 pix_fmt = bytestream2_get_le32u(gb);
87 bytestream2_get_le32u(gb); // mipmaps
88 data_size = bytestream2_get_le32u(gb);
89 bytestream2_seek(gb, VBN_HEADER_SIZE, SEEK_SET);
90
91 compression = format & 0xffffff00;
92 format = format & 0xff;
93
94 if (data_size != bytestream2_get_bytes_left(gb)) {
95 av_log(avctx, AV_LOG_ERROR, "Truncated packet\n");
97 }
98
100 av_log(avctx, AV_LOG_ERROR, "Unsupported pixel format: 0x%08x\n", pix_fmt);
102 }
103
104 ret = ff_set_dimensions(avctx, width, height);
105 if (ret < 0)
106 return ret;
107
108 if (format == VBN_FORMAT_RAW) {
109 if (pix_fmt == VBN_PIX_RGB && components == 3) {
110 avctx->pix_fmt = AV_PIX_FMT_RGB24;
111 linesize = avctx->width * 3;
112 } else if (pix_fmt == VBN_PIX_RGBA && components == 4) {
113 avctx->pix_fmt = AV_PIX_FMT_RGBA;
114 linesize = avctx->width * 4;
115 } else {
116 av_log(avctx, AV_LOG_ERROR, "Unsupported number of components: %d\n", components);
118 }
119 } else if (format == VBN_FORMAT_DXT1 || format == VBN_FORMAT_DXT5) {
120 if (avctx->width % TEXTURE_BLOCK_W || avctx->height % TEXTURE_BLOCK_H) {
121 av_log(avctx, AV_LOG_ERROR, "DXTx compression only supports 4 pixel aligned resolutions\n");
122 return AVERROR_INVALIDDATA;
123 }
124
125 avctx->pix_fmt = AV_PIX_FMT_RGBA;
126 if (format == VBN_FORMAT_DXT1) {
127 ctx->dec.tex_funct = ctx->texdsp.dxt1_block;
128 ctx->dec.tex_ratio = 8;
129 linesize = avctx->coded_width / 2;
130 } else {
131 ctx->dec.tex_funct = ctx->texdsp.dxt5_block;
132 ctx->dec.tex_ratio = 16;
133 linesize = avctx->coded_width;
134 }
135 } else {
136 av_log(avctx, AV_LOG_ERROR, "Unsupported VBN format: 0x%02x\n", format);
138 }
139
140 image_len = decompress(avctx, gb, compression, &image_buf);
141 if (image_len < 0)
142 return image_len;
143
144 if (image_len < linesize * avctx->coded_height) {
145 av_log(avctx, AV_LOG_ERROR, "Insufficient data\n");
147 goto out;
148 }
149
150 ret = ff_get_buffer(avctx, frame, 0);
151 if (ret < 0)
152 goto out;
153
154 if (format == VBN_FORMAT_RAW) {
155 uint8_t *flipped = frame->data[0] + frame->linesize[0] * (frame->height - 1);
156 av_image_copy_plane(flipped, -frame->linesize[0], image_buf ? image_buf : gb->buffer, linesize, linesize, frame->height);
157 } else {
158 ctx->dec.slice_count = av_clip(avctx->thread_count, 1, avctx->coded_height / TEXTURE_BLOCK_H);
159 ctx->dec.tex_data.in = image_buf ? image_buf : gb->buffer;
160 ctx->dec.raw_ratio = 16;
161 ctx->dec.frame_data.out = frame->data[0] + frame->linesize[0] * (frame->height - 1);
162 ctx->dec.stride = -frame->linesize[0];
163 ctx->dec.width = avctx->coded_width;
164 ctx->dec.height = avctx->coded_height;
166 }
167
168 *got_frame = 1;
169 ret = avpkt->size;
170
171out:
172 av_freep(&image_buf);
173 return ret;
174}
175
177 .p.name = "vbn",
178 CODEC_LONG_NAME("Vizrt Binary Image"),
179 .p.type = AVMEDIA_TYPE_VIDEO,
180 .p.id = AV_CODEC_ID_VBN,
181 .init = vbn_init,
183 .priv_data_size = sizeof(VBNContext),
185};
static const char *const format[]
Definition af_aiir.c:444
const FFCodec ff_vbn_decoder
Definition vbndec.c:176
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
static av_always_inline int bytestream2_seek(GetByteContext *g, int offset, int whence)
Definition bytestream.h:212
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
#define av_clip
Definition common.h:100
#define NULL
Definition coverity.c:32
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition decode.c:1777
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Definition utils.c:91
static enum AVPixelFormat pix_fmt
static AVFrame * frame
#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_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
Definition codec.h:102
@ AV_CODEC_ID_VBN
Definition codec_id.h:307
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition error.h:64
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
void av_image_copy_plane(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize, int bytewidth, int height)
Copy image plane from src to dst.
Definition imgutils.c:374
misc image utilities
#define av_cold
Definition attributes.h:117
Memory handling functions.
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition pixfmt.h:75
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition pixfmt.h:100
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
int thread_count
thread count is used to decide how many independent tasks should be passed to execute()
Definition avcodec.h:1579
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition avcodec.h:619
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
const uint8_t * buffer
Definition bytestream.h:34
TextureDSPThreadContext dec
Definition vbndec.c:37
TextureDSPContext texdsp
Definition vbndec.c:36
#define av_freep(p)
#define av_log(a,...)
static FILE * out
Definition movenc.c:55
static AVFormatContext * ctx
Definition movenc.c:49
#define height
Definition dsp.h:89
#define width
Definition dsp.h:89
av_cold void ff_texturedsp_init(TextureDSPContext *c)
Definition texturedsp.c:640
Texture block (4x4) module.
int ff_texturedsp_exec_decompress_threads(struct AVCodecContext *avctx, TextureDSPThreadContext *ctx)
#define TEXTURE_BLOCK_W
Definition texturedsp.h:42
#define TEXTURE_BLOCK_H
Definition texturedsp.h:43
VBN format definitions.
#define VBN_FORMAT_RAW
Definition vbn.h:35
#define VBN_FORMAT_DXT1
Definition vbn.h:37
#define VBN_MAJOR
Definition vbn.h:30
#define VBN_PIX_RGB
Definition vbn.h:46
#define VBN_COMPRESSION_NONE
Definition vbn.h:40
#define VBN_HEADER_SIZE
Definition vbn.h:33
#define VBN_MINOR
Definition vbn.h:31
#define VBN_PIX_RGBA
Definition vbn.h:47
#define VBN_FORMAT_DXT5
Definition vbn.h:38
#define VBN_MAGIC
Definition vbn.h:29
static int decompress(AVCodecContext *avctx, GetByteContext *gb, int compression, uint8_t **outbuf)
Definition vbndec.c:47
static av_cold int vbn_init(AVCodecContext *avctx)
Definition vbndec.c:40
static int vbn_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *avpkt)
Definition vbndec.c:57