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
28 #include "bytestream.h"
31 typedef struct QpegContext{
32 AVCodecContext *avctx;
35 GetByteContext buffer;
38 static void qpeg_decode_intra(QpegContext *qctx, uint8_t *dst,
39 int stride, int width, int height)
50 dst = dst + height * stride;
52 while ((bytestream2_get_bytes_left(&qctx->buffer) > 0) && (rows_to_go > 0)) {
53 code = bytestream2_get_byte(&qctx->buffer);
55 if(code == 0xFC) /* end-of-picture code */
57 if(code >= 0xF8) { /* very long run */
58 c0 = bytestream2_get_byte(&qctx->buffer);
59 c1 = bytestream2_get_byte(&qctx->buffer);
60 run = ((code & 0x7) << 16) + (c0 << 8) + c1 + 2;
61 } else if (code >= 0xF0) { /* long run */
62 c0 = bytestream2_get_byte(&qctx->buffer);
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 */
67 c0 = bytestream2_get_byte(&qctx->buffer);
68 c1 = bytestream2_get_byte(&qctx->buffer);
69 copy = ((code & 0x3F) << 16) + (c0 << 8) + c1 + 1;
70 } else if (code >= 0x80) { /* long copy */
71 c0 = bytestream2_get_byte(&qctx->buffer);
72 copy = ((code & 0x7F) << 8) + c0 + 1;
73 } else { /* short copy */
77 /* perform actual run or copy */
81 p = bytestream2_get_byte(&qctx->buffer);
82 for(i = 0; i < run; i++) {
83 int step = FFMIN(run - i, width - filled);
84 memset(dst+filled, p, step);
87 if (filled >= width) {
91 while (run - i > width && rows_to_go > 0) {
92 memset(dst, p, width);
102 if (bytestream2_get_bytes_left(&qctx->buffer) < copy)
103 copy = bytestream2_get_bytes_left(&qctx->buffer);
105 int step = FFMIN(copy, width - filled);
106 bytestream2_get_bufferu(&qctx->buffer, dst + filled, step);
109 if (filled >= width) {
121 static const uint8_t qpeg_table_h[16] =
122 { 0x00, 0x20, 0x20, 0x20, 0x18, 0x10, 0x10, 0x20, 0x10, 0x08, 0x18, 0x08, 0x08, 0x18, 0x10, 0x04};
123 static const uint8_t qpeg_table_w[16] =
124 { 0x00, 0x20, 0x18, 0x08, 0x18, 0x10, 0x20, 0x10, 0x08, 0x10, 0x20, 0x20, 0x08, 0x10, 0x18, 0x04};
126 /* Decodes delta frames */
127 static void av_noinline qpeg_decode_inter(QpegContext *qctx, uint8_t *dst,
128 int stride, int width, int height,
129 int delta, const uint8_t *ctable,
138 /* copy prev frame */
139 for (i = 0; i < height; i++)
140 memcpy(dst + (i * stride), refdata + (i * stride), width);
145 orig_height = height;
147 dst = dst + height * stride;
149 while ((bytestream2_get_bytes_left(&qctx->buffer) > 0) && (height >= 0)) {
150 code = bytestream2_get_byte(&qctx->buffer);
153 /* motion compensation */
154 while(bytestream2_get_bytes_left(&qctx->buffer) > 0 && (code & 0xF0) == 0xF0) {
157 int me_w, me_h, me_x, me_y;
161 /* get block size by index */
163 me_w = qpeg_table_w[me_idx];
164 me_h = qpeg_table_h[me_idx];
166 /* extract motion vector */
167 corr = bytestream2_get_byte(&qctx->buffer);
179 /* check motion vector */
180 if ((me_x + filled < 0) || (me_x + me_w + filled > width) ||
181 (height - me_y - me_h < 0) || (height - me_y >= orig_height) ||
182 (filled + me_w > width) || (height - me_h < 0))
183 av_log(qctx->avctx, AV_LOG_ERROR, "Bogus motion vector (%i,%i), block size %ix%i at %i,%i\n",
184 me_x, me_y, me_w, me_h, filled, height);
186 /* do motion compensation */
187 me_plane = refdata + (filled + me_x) + (height - me_y) * stride;
188 for(j = 0; j < me_h; j++) {
189 for(i = 0; i < me_w; i++)
190 dst[filled + i - (j * stride)] = me_plane[i - (j * stride)];
194 code = bytestream2_get_byte(&qctx->buffer);
198 if(code == 0xE0) /* end-of-picture code */
200 if(code > 0xE0) { /* run code: 0xE1..0xFF */
204 p = bytestream2_get_byte(&qctx->buffer);
205 for(i = 0; i <= code; i++) {
207 if(filled >= width) {
215 } else if(code >= 0xC0) { /* copy code: 0xC0..0xDF */
218 if(code + 1 > bytestream2_get_bytes_left(&qctx->buffer))
221 for(i = 0; i <= code; i++) {
222 dst[filled++] = bytestream2_get_byte(&qctx->buffer);
223 if(filled >= width) {
231 } else if(code >= 0x80) { /* skip code: 0x80..0xBF */
235 /* codes 0x80 and 0x81 are actually escape codes,
236 skip value minus constant is in the next byte */
238 skip = bytestream2_get_byte(&qctx->buffer) + 64;
240 skip = bytestream2_get_byte(&qctx->buffer) + 320;
244 while( filled >= width) {
252 /* zero code treated as one-pixel skip */
254 dst[filled++] = ctable[code & 0x7F];
258 if(filled >= width) {
267 static int decode_frame(AVCodecContext *avctx,
268 void *data, int *got_frame,
272 QpegContext * const a = avctx->priv_data;
273 AVFrame * const p = data;
274 AVFrame * const ref = a->ref;
276 int delta, intra, ret;
278 const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, &pal_size);
280 if (avpkt->size < 0x86) {
281 av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
282 return AVERROR_INVALIDDATA;
285 bytestream2_init(&a->buffer, avpkt->data, avpkt->size);
287 if ((ret = ff_get_buffer(avctx, p, AV_GET_BUFFER_FLAG_REF)) < 0)
289 outdata = p->data[0];
290 bytestream2_skip(&a->buffer, 4);
291 bytestream2_get_buffer(&a->buffer, ctable, 128);
292 bytestream2_skip(&a->buffer, 1);
294 delta = bytestream2_get_byte(&a->buffer);
295 intra = delta == 0x10;
297 qpeg_decode_intra(a, outdata, p->linesize[0], avctx->width, avctx->height);
299 qpeg_decode_inter(a, outdata, p->linesize[0], avctx->width, avctx->height, delta, ctable, ref->data[0]);
302 /* make the palette available on the way out */
303 if (pal && pal_size == AVPALETTE_SIZE) {
304 p->palette_has_changed = 1;
305 memcpy(a->pal, pal, AVPALETTE_SIZE);
307 av_log(avctx, AV_LOG_ERROR, "Palette size %d is wrong\n", pal_size);
309 memcpy(p->data[1], a->pal, AVPALETTE_SIZE);
312 if ((ret = av_frame_ref(ref, p)) < 0)
315 p->key_frame = intra;
316 p->pict_type = intra ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
323 static void decode_flush(AVCodecContext *avctx){
324 QpegContext * const a = avctx->priv_data;
326 const uint8_t *pal_src;
328 av_frame_unref(a->ref);
330 pal_size = FFMIN(1024U, avctx->extradata_size);
331 pal_src = avctx->extradata + avctx->extradata_size - pal_size;
333 for (i=0; i<pal_size/4; i++)
334 a->pal[i] = 0xFFU<<24 | AV_RL32(pal_src+4*i);
337 static av_cold int decode_end(AVCodecContext *avctx)
339 QpegContext * const a = avctx->priv_data;
341 av_frame_free(&a->ref);
346 static av_cold int decode_init(AVCodecContext *avctx){
347 QpegContext * const a = avctx->priv_data;
350 avctx->pix_fmt= AV_PIX_FMT_PAL8;
352 a->ref = av_frame_alloc();
354 return AVERROR(ENOMEM);
361 AVCodec ff_qpeg_decoder = {
363 .long_name = NULL_IF_CONFIG_SMALL("Q-team QPEG"),
364 .type = AVMEDIA_TYPE_VIDEO,
365 .id = AV_CODEC_ID_QPEG,
366 .priv_data_size = sizeof(QpegContext),
369 .decode = decode_frame,
370 .flush = decode_flush,
371 .capabilities = AV_CODEC_CAP_DR1,
372 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
373 FF_CODEC_CAP_INIT_CLEANUP,