Simplify if() in copy_and_dup()
[ffmpeg-lucabe.git] / libavcodec / avs.c
blobc60fe63e33923909dd0ef86ac5a2b5ad8f7a74b1
1 /*
2 * AVS video decoder.
3 * Copyright (c) 2006 Aurelien Jacobs <aurel@gnuage.org>
5 * This file is part of FFmpeg.
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.
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.
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
22 #include "avcodec.h"
23 #include "bitstream.h"
26 typedef struct {
27 AVFrame picture;
28 } avs_context_t;
30 typedef enum {
31 AVS_VIDEO = 0x01,
32 AVS_AUDIO = 0x02,
33 AVS_PALETTE = 0x03,
34 AVS_GAME_DATA = 0x04,
35 } avs_block_type_t;
37 typedef enum {
38 AVS_I_FRAME = 0x00,
39 AVS_P_FRAME_3X3 = 0x01,
40 AVS_P_FRAME_2X2 = 0x02,
41 AVS_P_FRAME_2X3 = 0x03,
42 } avs_video_sub_type_t;
45 static int
46 avs_decode_frame(AVCodecContext * avctx,
47 void *data, int *data_size, const uint8_t * buf, int buf_size)
49 avs_context_t *const avs = avctx->priv_data;
50 AVFrame *picture = data;
51 AVFrame *const p = (AVFrame *) & avs->picture;
52 const uint8_t *table, *vect;
53 uint8_t *out;
54 int i, j, x, y, stride, vect_w = 3, vect_h = 3;
55 int sub_type;
56 avs_block_type_t type;
57 GetBitContext change_map;
59 if (avctx->reget_buffer(avctx, p)) {
60 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
61 return -1;
63 p->reference = 1;
64 p->pict_type = FF_P_TYPE;
65 p->key_frame = 0;
67 out = avs->picture.data[0];
68 stride = avs->picture.linesize[0];
70 sub_type = buf[0];
71 type = buf[1];
72 buf += 4;
74 if (type == AVS_PALETTE) {
75 int first, last;
76 uint32_t *pal = (uint32_t *) avs->picture.data[1];
78 first = AV_RL16(buf);
79 last = first + AV_RL16(buf + 2);
80 buf += 4;
81 for (i=first; i<last; i++, buf+=3)
82 pal[i] = (buf[0] << 18) | (buf[1] << 10) | (buf[2] << 2);
84 sub_type = buf[0];
85 type = buf[1];
86 buf += 4;
89 if (type != AVS_VIDEO)
90 return -1;
92 switch (sub_type) {
93 case AVS_I_FRAME:
94 p->pict_type = FF_I_TYPE;
95 p->key_frame = 1;
96 case AVS_P_FRAME_3X3:
97 vect_w = 3;
98 vect_h = 3;
99 break;
101 case AVS_P_FRAME_2X2:
102 vect_w = 2;
103 vect_h = 2;
104 break;
106 case AVS_P_FRAME_2X3:
107 vect_w = 2;
108 vect_h = 3;
109 break;
111 default:
112 return -1;
115 table = buf + (256 * vect_w * vect_h);
116 if (sub_type != AVS_I_FRAME) {
117 int map_size = ((318 / vect_w + 7) / 8) * (198 / vect_h);
118 init_get_bits(&change_map, table, map_size);
119 table += map_size;
122 for (y=0; y<198; y+=vect_h) {
123 for (x=0; x<318; x+=vect_w) {
124 if (sub_type == AVS_I_FRAME || get_bits1(&change_map)) {
125 vect = &buf[*table++ * (vect_w * vect_h)];
126 for (j=0; j<vect_w; j++) {
127 out[(y + 0) * stride + x + j] = vect[(0 * vect_w) + j];
128 out[(y + 1) * stride + x + j] = vect[(1 * vect_w) + j];
129 if (vect_h == 3)
130 out[(y + 2) * stride + x + j] =
131 vect[(2 * vect_w) + j];
135 if (sub_type != AVS_I_FRAME)
136 align_get_bits(&change_map);
139 *picture = *(AVFrame *) & avs->picture;
140 *data_size = sizeof(AVPicture);
142 return buf_size;
145 static av_cold int avs_decode_init(AVCodecContext * avctx)
147 avctx->pix_fmt = PIX_FMT_PAL8;
148 return 0;
151 AVCodec avs_decoder = {
152 "avs",
153 CODEC_TYPE_VIDEO,
154 CODEC_ID_AVS,
155 sizeof(avs_context_t),
156 avs_decode_init,
157 NULL,
158 NULL,
159 avs_decode_frame,
160 CODEC_CAP_DR1,
161 .long_name = NULL_IF_CONFIG_SMALL("AVS (Audio Video Standard) video"),