FFmpeg
Loading...
Searching...
No Matches
bfi.c
Go to the documentation of this file.
1/*
2 * Brute Force & Ignorance (BFI) video decoder
3 * Copyright (c) 2008 Sisir Koppaka
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 * @brief Brute Force & Ignorance (.bfi) video decoder
25 * @author Sisir Koppaka ( sisir.koppaka at gmail dot com )
26 * @see http://wiki.multimedia.cx/index.php?title=BFI
27 */
28
29#include "libavutil/mem.h"
30#include "avcodec.h"
31#include "bytestream.h"
32#include "codec_internal.h"
33#include "decode.h"
34
35typedef struct BFIContext {
36 uint8_t *dst;
37 uint32_t pal[256];
39
41{
42 BFIContext *bfi = avctx->priv_data;
43 avctx->pix_fmt = AV_PIX_FMT_PAL8;
44 bfi->dst = av_mallocz(avctx->width * avctx->height);
45 if (!bfi->dst)
46 return AVERROR(ENOMEM);
47 return 0;
48}
49
51 int *got_frame, AVPacket *avpkt)
52{
54 int buf_size = avpkt->size;
55 BFIContext *bfi = avctx->priv_data;
56 uint8_t *dst = bfi->dst;
57 uint8_t *src, *dst_offset, colour1, colour2;
58 uint8_t *frame_end = bfi->dst + avctx->width * avctx->height;
59 uint32_t *pal;
60 int i, j, ret, height = avctx->height;
61
62 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
63 return ret;
64
65 bytestream2_init(&g, avpkt->data, buf_size);
66
67 /* Set frame parameters and palette, if necessary */
68 if (!avctx->frame_num) {
69 frame->pict_type = AV_PICTURE_TYPE_I;
70 frame->flags |= AV_FRAME_FLAG_KEY;
71 /* Setting the palette */
72 if (avctx->extradata_size > 768) {
73 av_log(avctx, AV_LOG_ERROR, "Palette is too large.\n");
75 }
76 pal = (uint32_t *)frame->data[1];
77 for (i = 0; i < avctx->extradata_size / 3; i++) {
78 int shift = 16;
79 *pal = 0xFFU << 24;
80 for (j = 0; j < 3; j++, shift -= 8)
81 *pal += ((avctx->extradata[i * 3 + j] << 2) |
82 (avctx->extradata[i * 3 + j] >> 4)) << shift;
83 pal++;
84 }
85 memcpy(bfi->pal, frame->data[1], sizeof(bfi->pal));
86 } else {
87 frame->pict_type = AV_PICTURE_TYPE_P;
88 frame->flags &= ~AV_FRAME_FLAG_KEY;
89 memcpy(frame->data[1], bfi->pal, sizeof(bfi->pal));
90 }
91
92 bytestream2_skip(&g, 4); // Unpacked size, not required.
93
94 while (dst != frame_end) {
95 static const uint8_t lentab[4] = { 0, 2, 0, 1 };
96 unsigned int byte = bytestream2_get_byte(&g), av_uninit(offset);
97 unsigned int code = byte >> 6;
98 unsigned int length = byte & ~0xC0;
99
101 av_log(avctx, AV_LOG_ERROR,
102 "Input resolution larger than actual frame.\n");
103 return AVERROR_INVALIDDATA;
104 }
105
106 /* Get length and offset (if required) */
107 if (length == 0) {
108 if (code == 1) {
109 length = bytestream2_get_byte(&g);
110 offset = bytestream2_get_le16(&g);
111 } else {
112 length = bytestream2_get_le16(&g);
113 if (code == 2 && length == 0)
114 break;
115 }
116 } else {
117 if (code == 1)
118 offset = bytestream2_get_byte(&g);
119 }
120
121 /* Do boundary check */
122 if (dst + (length << lentab[code]) > frame_end)
123 break;
124
125 switch (code) {
126 case 0: // normal chain
127 if (length >= bytestream2_get_bytes_left(&g)) {
128 av_log(avctx, AV_LOG_ERROR, "Frame larger than buffer.\n");
129 return AVERROR_INVALIDDATA;
130 }
131 bytestream2_get_buffer(&g, dst, length);
132 dst += length;
133 break;
134 case 1: // back chain
135 dst_offset = dst - offset;
136 length *= 4; // Convert dwords to bytes.
137 if (dst_offset < bfi->dst)
138 break;
139 while (length--)
140 *dst++ = *dst_offset++;
141 break;
142 case 2: // skip chain
143 dst += length;
144 break;
145 case 3: // fill chain
146 colour1 = bytestream2_get_byte(&g);
147 colour2 = bytestream2_get_byte(&g);
148 while (length--) {
149 *dst++ = colour1;
150 *dst++ = colour2;
151 }
152 break;
153 }
154 }
155
156 src = bfi->dst;
157 dst = frame->data[0];
158 while (height--) {
159 memcpy(dst, src, avctx->width);
160 src += avctx->width;
161 dst += frame->linesize[0];
162 }
163 *got_frame = 1;
164
165 return buf_size;
166}
167
169{
170 BFIContext *bfi = avctx->priv_data;
171 av_freep(&bfi->dst);
172 return 0;
173}
174
176 .p.name = "bfi",
177 CODEC_LONG_NAME("Brute Force & Ignorance"),
178 .p.type = AVMEDIA_TYPE_VIDEO,
179 .p.id = AV_CODEC_ID_BFI,
180 .priv_data_size = sizeof(BFIContext),
184 .p.capabilities = AV_CODEC_CAP_DR1,
185};
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_bfi_decoder
Definition bfi.c:175
static av_cold void close(AVCodecParserContext *s)
Definition apv_parser.c:197
Libavcodec external API header.
static av_always_inline unsigned int bytestream2_get_buffer(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition bytestream.h:267
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 void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition bytestream.h:168
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition decode.c:1777
static AVFrame * frame
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
@ AV_CODEC_ID_BFI
Definition codec_id.h:167
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
Definition frame.h:687
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
@ AV_PICTURE_TYPE_I
Intra.
Definition avutil.h:278
@ AV_PICTURE_TYPE_P
Predicted.
Definition avutil.h:279
unsigned offset
Definition libaomenc.c:763
static av_cold int bfi_decode_close(AVCodecContext *avctx)
Definition bfi.c:168
static int bfi_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *avpkt)
Definition bfi.c:50
static av_cold int bfi_decode_init(AVCodecContext *avctx)
Definition bfi.c:40
static int shift(int a, int b)
Definition bonk.c:261
#define av_uninit(x)
Definition attributes.h:187
#define av_cold
Definition attributes.h:117
Memory handling functions.
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition pixfmt.h:84
const uint8_t * code
Definition spdifenc.c:433
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
int64_t frame_num
Frame counter, set by libavcodec.
Definition avcodec.h:1883
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
Definition avcodec.h:526
int extradata_size
Definition avcodec.h:527
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
uint8_t * dst
Definition bfi.c:36
uint32_t pal[256]
Definition bfi.c:37
static int64_t frame_end(const SyncQueue *sq, SyncQueueFrame frame, int nb_samples)
Compute the end timestamp of a frame.
Definition sync_queue.c:118
#define av_mallocz(s)
#define av_freep(p)
#define av_log(a,...)
#define src
Definition vp8dsp.c:248
#define height
Definition dsp.h:89
const char * g
Definition vf_curves.c:128