FFmpeg
Loading...
Searching...
No Matches
avs.c
Go to the documentation of this file.
1/*
2 * AVS video decoder.
3 * Copyright (c) 2006 Aurelien Jacobs <aurel@gnuage.org>
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 "avcodec.h"
23#include "codec_internal.h"
24#include "decode.h"
25#include "get_bits.h"
27
28typedef struct AvsContext {
31
32typedef enum {
33 AVS_VIDEO = 0x01,
34 AVS_AUDIO = 0x02,
38
45
46
47static int avs_decode_frame(AVCodecContext * avctx, AVFrame *picture,
48 int *got_frame, AVPacket *avpkt)
49{
50 const uint8_t *buf = avpkt->data;
51 const uint8_t *buf_end = avpkt->data + avpkt->size;
52 int buf_size = avpkt->size;
53 AvsContext *const avs = avctx->priv_data;
54 AVFrame *const p = avs->frame;
55 const uint8_t *table, *vect;
56 uint8_t *out;
57 int i, j, x, y, stride, ret, vect_w = 3, vect_h = 3;
58 AvsVideoSubType sub_type;
60 GetBitContext change_map = {0}; //init to silence warning
61
62 if ((ret = ff_reget_buffer(avctx, p, 0)) < 0)
63 return ret;
64 p->pict_type = AV_PICTURE_TYPE_P;
65 p->flags &= ~AV_FRAME_FLAG_KEY;
66
67 out = p->data[0];
68 stride = p->linesize[0];
69
70 if (buf_end - buf < 4)
72 sub_type = buf[0];
73 type = buf[1];
74 buf += 4;
75
76 if (type == AVS_PALETTE) {
77 int first, last;
78 uint32_t *pal = (uint32_t *) p->data[1];
79
80 first = AV_RL16(buf);
81 last = first + AV_RL16(buf + 2);
82 if (first >= 256 || last > 256 || buf_end - buf < 4 + 4 + 3 * (last - first))
84 buf += 4;
85 for (i=first; i<last; i++, buf+=3) {
86 pal[i] = (buf[0] << 18) | (buf[1] << 10) | (buf[2] << 2);
87 pal[i] |= 0xFFU << 24 | (pal[i] >> 6) & 0x30303;
88 }
89
90 sub_type = buf[0];
91 type = buf[1];
92 buf += 4;
93 }
94
95 if (type != AVS_VIDEO)
97
98 switch (sub_type) {
99 case AVS_I_FRAME:
100 p->pict_type = AV_PICTURE_TYPE_I;
101 p->flags |= AV_FRAME_FLAG_KEY;
103 case AVS_P_FRAME_3X3:
104 vect_w = 3;
105 vect_h = 3;
106 break;
107
108 case AVS_P_FRAME_2X2:
109 vect_w = 2;
110 vect_h = 2;
111 break;
112
113 case AVS_P_FRAME_2X3:
114 vect_w = 2;
115 vect_h = 3;
116 break;
117
118 default:
119 return AVERROR_INVALIDDATA;
120 }
121
122 if (buf_end - buf < 256 * vect_w * vect_h)
123 return AVERROR_INVALIDDATA;
124 table = buf + (256 * vect_w * vect_h);
125 if (sub_type != AVS_I_FRAME) {
126 int map_size = ((318 / vect_w + 7) / 8) * (198 / vect_h);
127 if (buf_end - table < map_size)
128 return AVERROR_INVALIDDATA;
129 init_get_bits(&change_map, table, map_size * 8);
130 table += map_size;
131 }
132
133 for (y=0; y<198; y+=vect_h) {
134 for (x=0; x<318; x+=vect_w) {
135 if (sub_type == AVS_I_FRAME || get_bits1(&change_map)) {
136 if (buf_end - table < 1)
137 return AVERROR_INVALIDDATA;
138 vect = &buf[*table++ * (vect_w * vect_h)];
139 for (j=0; j<vect_w; j++) {
140 out[(y + 0) * stride + x + j] = vect[(0 * vect_w) + j];
141 out[(y + 1) * stride + x + j] = vect[(1 * vect_w) + j];
142 if (vect_h == 3)
143 out[(y + 2) * stride + x + j] =
144 vect[(2 * vect_w) + j];
145 }
146 }
147 }
148 if (sub_type != AVS_I_FRAME)
149 align_get_bits(&change_map);
150 }
151
152 if ((ret = av_frame_ref(picture, p)) < 0)
153 return ret;
154 *got_frame = 1;
155
156 return buf_size;
157}
158
160{
161 AvsContext *s = avctx->priv_data;
162
163 s->frame = av_frame_alloc();
164 if (!s->frame)
165 return AVERROR(ENOMEM);
166
167 avctx->pix_fmt = AV_PIX_FMT_PAL8;
168
169 return ff_set_dimensions(avctx, 318, 198);
170}
171
173{
174 AvsContext *s = avctx->priv_data;
175 av_frame_free(&s->frame);
176 return 0;
177}
178
179
181 .p.name = "avs",
182 CODEC_LONG_NAME("AVS (Audio Video Standard) video"),
183 .p.type = AVMEDIA_TYPE_VIDEO,
184 .p.id = AV_CODEC_ID_AVS,
185 .priv_data_size = sizeof(AvsContext),
188 .close = avs_decode_end,
189 .p.capabilities = AV_CODEC_CAP_DR1,
190};
const FFCodec ff_avs_decoder
Definition avs.c:180
static FILE * out
Libavcodec external API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Identical in function to ff_get_buffer(), except it reuses the existing buffer if available.
Definition decode.c:1906
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Definition utils.c:91
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
bitstream reader API header.
static unsigned int get_bits1(GetBitContext *s)
Definition get_bits.h:391
static const uint8_t * align_get_bits(GetBitContext *s)
Definition get_bits.h:560
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition get_bits.h:517
#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_AVS
Definition codec_id.h:132
#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
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition frame.c:278
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition frame.c:52
@ 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
cl_device_type type
#define AV_RL16(p)
AvsBlockType
Definition avs.c:32
@ AVS_PALETTE
Definition avs.c:35
@ AVS_AUDIO
Definition avs.c:34
@ AVS_GAME_DATA
Definition avs.c:36
@ AVS_VIDEO
Definition avs.c:33
static av_cold int avs_decode_init(AVCodecContext *avctx)
Definition avs.c:159
AvsVideoSubType
Definition avs.c:39
@ AVS_P_FRAME_2X2
Definition avs.c:42
@ AVS_I_FRAME
Definition avs.c:40
@ AVS_P_FRAME_3X3
Definition avs.c:41
@ AVS_P_FRAME_2X3
Definition avs.c:43
static int avs_decode_frame(AVCodecContext *avctx, AVFrame *picture, int *got_frame, AVPacket *avpkt)
Definition avs.c:47
static av_cold int avs_decode_end(AVCodecContext *avctx)
Definition avs.c:172
Macro definitions for various function/variable attributes.
#define av_fallthrough
Definition attributes.h:67
#define av_cold
Definition attributes.h:117
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition pixfmt.h:84
static const uint16_t table[]
Definition prosumer.c:203
main external API structure.
Definition avcodec.h:443
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition avcodec.h:643
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
AVFrame * frame
Definition avs.c:29
#define stride