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
23 #include "codec_internal.h"
27 typedef struct AvsContext
{
40 AVS_P_FRAME_3X3
= 0x01,
41 AVS_P_FRAME_2X2
= 0x02,
42 AVS_P_FRAME_2X3
= 0x03,
46 static int avs_decode_frame(AVCodecContext
* avctx
, AVFrame
*picture
,
47 int *got_frame
, AVPacket
*avpkt
)
49 const uint8_t *buf
= avpkt
->data
;
50 const uint8_t *buf_end
= avpkt
->data
+ avpkt
->size
;
51 int buf_size
= avpkt
->size
;
52 AvsContext
*const avs
= avctx
->priv_data
;
53 AVFrame
*const p
= avs
->frame
;
54 const uint8_t *table
, *vect
;
56 int i
, j
, x
, y
, stride
, ret
, vect_w
= 3, vect_h
= 3;
57 AvsVideoSubType sub_type
;
59 GetBitContext change_map
= {0}; //init to silence warning
61 if ((ret
= ff_reget_buffer(avctx
, p
, 0)) < 0)
63 p
->pict_type
= AV_PICTURE_TYPE_P
;
64 p
->flags
&= ~AV_FRAME_FLAG_KEY
;
67 stride
= p
->linesize
[0];
69 if (buf_end
- buf
< 4)
70 return AVERROR_INVALIDDATA
;
75 if (type
== AVS_PALETTE
) {
77 uint32_t *pal
= (uint32_t *) p
->data
[1];
80 last
= first
+ AV_RL16(buf
+ 2);
81 if (first
>= 256 || last
> 256 || buf_end
- buf
< 4 + 4 + 3 * (last
- first
))
82 return AVERROR_INVALIDDATA
;
84 for (i
=first
; i
<last
; i
++, buf
+=3) {
85 pal
[i
] = (buf
[0] << 18) | (buf
[1] << 10) | (buf
[2] << 2);
86 pal
[i
] |= 0xFFU
<< 24 | (pal
[i
] >> 6) & 0x30303;
94 if (type
!= AVS_VIDEO
)
95 return AVERROR_INVALIDDATA
;
99 p
->pict_type
= AV_PICTURE_TYPE_I
;
100 p
->flags
|= AV_FRAME_FLAG_KEY
;
101 case AVS_P_FRAME_3X3
:
106 case AVS_P_FRAME_2X2
:
111 case AVS_P_FRAME_2X3
:
117 return AVERROR_INVALIDDATA
;
120 if (buf_end
- buf
< 256 * vect_w
* vect_h
)
121 return AVERROR_INVALIDDATA
;
122 table
= buf
+ (256 * vect_w
* vect_h
);
123 if (sub_type
!= AVS_I_FRAME
) {
124 int map_size
= ((318 / vect_w
+ 7) / 8) * (198 / vect_h
);
125 if (buf_end
- table
< map_size
)
126 return AVERROR_INVALIDDATA
;
127 init_get_bits(&change_map
, table
, map_size
* 8);
131 for (y
=0; y
<198; y
+=vect_h
) {
132 for (x
=0; x
<318; x
+=vect_w
) {
133 if (sub_type
== AVS_I_FRAME
|| get_bits1(&change_map
)) {
134 if (buf_end
- table
< 1)
135 return AVERROR_INVALIDDATA
;
136 vect
= &buf
[*table
++ * (vect_w
* vect_h
)];
137 for (j
=0; j
<vect_w
; j
++) {
138 out
[(y
+ 0) * stride
+ x
+ j
] = vect
[(0 * vect_w
) + j
];
139 out
[(y
+ 1) * stride
+ x
+ j
] = vect
[(1 * vect_w
) + j
];
141 out
[(y
+ 2) * stride
+ x
+ j
] =
142 vect
[(2 * vect_w
) + j
];
146 if (sub_type
!= AVS_I_FRAME
)
147 align_get_bits(&change_map
);
150 if ((ret
= av_frame_ref(picture
, p
)) < 0)
157 static av_cold
int avs_decode_init(AVCodecContext
* avctx
)
159 AvsContext
*s
= avctx
->priv_data
;
161 s
->frame
= av_frame_alloc();
163 return AVERROR(ENOMEM
);
165 avctx
->pix_fmt
= AV_PIX_FMT_PAL8
;
167 return ff_set_dimensions(avctx
, 318, 198);
170 static av_cold
int avs_decode_end(AVCodecContext
*avctx
)
172 AvsContext
*s
= avctx
->priv_data
;
173 av_frame_free(&s
->frame
);
178 const FFCodec ff_avs_decoder
= {
180 CODEC_LONG_NAME("AVS (Audio Video Standard) video"),
181 .p
.type
= AVMEDIA_TYPE_VIDEO
,
182 .p
.id
= AV_CODEC_ID_AVS
,
183 .priv_data_size
= sizeof(AvsContext
),
184 .init
= avs_decode_init
,
185 FF_CODEC_DECODE_CB(avs_decode_frame
),
186 .close
= avs_decode_end
,
187 .p
.capabilities
= AV_CODEC_CAP_DR1
,