3 * Copyright (c) 2004 Konstantin Shishkov
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 * @file libavcodec/qpeg.c
29 typedef struct QpegContext
{
30 AVCodecContext
*avctx
;
35 static void qpeg_decode_intra(const uint8_t *src
, uint8_t *dst
, int size
,
36 int stride
, int width
, int height
)
47 dst
= dst
+ height
* stride
;
49 while((size
> 0) && (rows_to_go
> 0)) {
53 if(code
== 0xFC) /* end-of-picture code */
55 if(code
>= 0xF8) { /* very long run */
59 run
= ((code
& 0x7) << 16) + (c0
<< 8) + c1
+ 2;
60 } else if (code
>= 0xF0) { /* long run */
63 run
= ((code
& 0xF) << 8) + c0
+ 2;
64 } else if (code
>= 0xE0) { /* short run */
65 run
= (code
& 0x1F) + 2;
66 } else if (code
>= 0xC0) { /* very long copy */
70 copy
= ((code
& 0x3F) << 16) + (c0
<< 8) + c1
+ 1;
71 } else if (code
>= 0x80) { /* long copy */
74 copy
= ((code
& 0x7F) << 8) + c0
+ 1;
75 } else { /* short copy */
79 /* perform actual run or copy */
85 for(i
= 0; i
< run
; i
++) {
87 if (filled
>= width
) {
97 for(i
= 0; i
< copy
; i
++) {
98 dst
[filled
++] = *src
++;
99 if (filled
>= width
) {
111 static const int qpeg_table_h
[16] =
112 { 0x00, 0x20, 0x20, 0x20, 0x18, 0x10, 0x10, 0x20, 0x10, 0x08, 0x18, 0x08, 0x08, 0x18, 0x10, 0x04};
113 static const int qpeg_table_w
[16] =
114 { 0x00, 0x20, 0x18, 0x08, 0x18, 0x10, 0x20, 0x10, 0x08, 0x10, 0x20, 0x20, 0x08, 0x10, 0x18, 0x04};
116 /* Decodes delta frames */
117 static void qpeg_decode_inter(const uint8_t *src
, uint8_t *dst
, int size
,
118 int stride
, int width
, int height
,
119 int delta
, const uint8_t *ctable
, uint8_t *refdata
)
126 /* copy prev frame */
127 for(i
= 0; i
< height
; i
++)
128 memcpy(refdata
+ (i
* width
), dst
+ (i
* stride
), width
);
130 orig_height
= height
;
132 dst
= dst
+ height
* stride
;
134 while((size
> 0) && (height
>= 0)) {
139 /* motion compensation */
140 while((code
& 0xF0) == 0xF0) {
143 int me_w
, me_h
, me_x
, me_y
;
147 /* get block size by index */
149 me_w
= qpeg_table_w
[me_idx
];
150 me_h
= qpeg_table_h
[me_idx
];
152 /* extract motion vector */
166 /* check motion vector */
167 if ((me_x
+ filled
< 0) || (me_x
+ me_w
+ filled
> width
) ||
168 (height
- me_y
- me_h
< 0) || (height
- me_y
> orig_height
) ||
169 (filled
+ me_w
> width
) || (height
- me_h
< 0))
170 av_log(NULL
, AV_LOG_ERROR
, "Bogus motion vector (%i,%i), block size %ix%i at %i,%i\n",
171 me_x
, me_y
, me_w
, me_h
, filled
, height
);
173 /* do motion compensation */
174 me_plane
= refdata
+ (filled
+ me_x
) + (height
- me_y
) * width
;
175 for(j
= 0; j
< me_h
; j
++) {
176 for(i
= 0; i
< me_w
; i
++)
177 dst
[filled
+ i
- (j
* stride
)] = me_plane
[i
- (j
* width
)];
186 if(code
== 0xE0) /* end-of-picture code */
188 if(code
> 0xE0) { /* run code: 0xE1..0xFF */
194 for(i
= 0; i
<= code
; i
++) {
196 if(filled
>= width
) {
202 } else if(code
>= 0xC0) { /* copy code: 0xC0..0xDF */
205 for(i
= 0; i
<= code
; i
++) {
206 dst
[filled
++] = *src
++;
207 if(filled
>= width
) {
214 } else if(code
>= 0x80) { /* skip code: 0x80..0xBF */
218 /* codes 0x80 and 0x81 are actually escape codes,
219 skip value minus constant is in the next byte */
221 skip
= (*src
++) + 64;
223 skip
= (*src
++) + 320;
227 while( filled
>= width
) {
235 /* zero code treated as one-pixel skip */
237 dst
[filled
++] = ctable
[code
& 0x7F];
240 if(filled
>= width
) {
249 static int decode_frame(AVCodecContext
*avctx
,
250 void *data
, int *data_size
,
251 const uint8_t *buf
, int buf_size
)
253 QpegContext
* const a
= avctx
->priv_data
;
254 AVFrame
* const p
= (AVFrame
*)&a
->pic
;
259 avctx
->release_buffer(avctx
, p
);
262 if(avctx
->get_buffer(avctx
, p
) < 0){
263 av_log(avctx
, AV_LOG_ERROR
, "get_buffer() failed\n");
266 outdata
= a
->pic
.data
[0];
267 if(buf
[0x85] == 0x10) {
268 qpeg_decode_intra(buf
+0x86, outdata
, buf_size
- 0x86, a
->pic
.linesize
[0], avctx
->width
, avctx
->height
);
271 qpeg_decode_inter(buf
+0x86, outdata
, buf_size
- 0x86, a
->pic
.linesize
[0], avctx
->width
, avctx
->height
, delta
, buf
+ 4, a
->refdata
);
274 /* make the palette available on the way out */
275 memcpy(a
->pic
.data
[1], a
->avctx
->palctrl
->palette
, AVPALETTE_SIZE
);
276 if (a
->avctx
->palctrl
->palette_changed
) {
277 a
->pic
.palette_has_changed
= 1;
278 a
->avctx
->palctrl
->palette_changed
= 0;
281 *data_size
= sizeof(AVFrame
);
282 *(AVFrame
*)data
= a
->pic
;
287 static av_cold
int decode_init(AVCodecContext
*avctx
){
288 QpegContext
* const a
= avctx
->priv_data
;
291 avctx
->pix_fmt
= PIX_FMT_PAL8
;
292 a
->pic
.data
[0] = NULL
;
293 a
->refdata
= av_malloc(avctx
->width
* avctx
->height
);
298 static av_cold
int decode_end(AVCodecContext
*avctx
){
299 QpegContext
* const a
= avctx
->priv_data
;
300 AVFrame
* const p
= (AVFrame
*)&a
->pic
;
303 avctx
->release_buffer(avctx
, p
);
309 AVCodec qpeg_decoder
= {
319 .long_name
= NULL_IF_CONFIG_SMALL("Q-team QPEG"),