3 * Copyright (c) 2004 Konstantin Shishkov
5 * This file is part of Libav.
7 * Libav 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 * Libav 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 Libav; 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;
36 GetByteContext buffer;
39 static void qpeg_decode_intra(QpegContext *qctx, uint8_t *dst,
40 int stride, int width, int height)
51 dst = dst + height * stride;
53 while ((bytestream2_get_bytes_left(&qctx->buffer) > 0) && (rows_to_go > 0)) {
54 code = bytestream2_get_byte(&qctx->buffer);
56 if(code == 0xFC) /* end-of-picture code */
58 if(code >= 0xF8) { /* very long run */
59 c0 = bytestream2_get_byte(&qctx->buffer);
60 c1 = bytestream2_get_byte(&qctx->buffer);
61 run = ((code & 0x7) << 16) + (c0 << 8) + c1 + 2;
62 } else if (code >= 0xF0) { /* long run */
63 c0 = bytestream2_get_byte(&qctx->buffer);
64 run = ((code & 0xF) << 8) + c0 + 2;
65 } else if (code >= 0xE0) { /* short run */
66 run = (code & 0x1F) + 2;
67 } else if (code >= 0xC0) { /* very long copy */
68 c0 = bytestream2_get_byte(&qctx->buffer);
69 c1 = bytestream2_get_byte(&qctx->buffer);
70 copy = ((code & 0x3F) << 16) + (c0 << 8) + c1 + 1;
71 } else if (code >= 0x80) { /* long copy */
72 c0 = bytestream2_get_byte(&qctx->buffer);
73 copy = ((code & 0x7F) << 8) + c0 + 1;
74 } else { /* short copy */
78 /* perform actual run or copy */
82 p = bytestream2_get_byte(&qctx->buffer);
83 for(i = 0; i < run; i++) {
85 if (filled >= width) {
94 for(i = 0; i < copy; i++) {
95 dst[filled++] = bytestream2_get_byte(&qctx->buffer);
96 if (filled >= width) {
108 static const int qpeg_table_h[16] =
109 { 0x00, 0x20, 0x20, 0x20, 0x18, 0x10, 0x10, 0x20, 0x10, 0x08, 0x18, 0x08, 0x08, 0x18, 0x10, 0x04};
110 static const int qpeg_table_w[16] =
111 { 0x00, 0x20, 0x18, 0x08, 0x18, 0x10, 0x20, 0x10, 0x08, 0x10, 0x20, 0x20, 0x08, 0x10, 0x18, 0x04};
113 /* Decodes delta frames */
114 static void qpeg_decode_inter(QpegContext *qctx, uint8_t *dst,
115 int stride, int width, int height,
116 int delta, const uint8_t *ctable,
124 /* copy prev frame */
125 for(i = 0; i < height; i++)
126 memcpy(refdata + (i * width), dst + (i * stride), width);
128 orig_height = height;
130 dst = dst + height * stride;
132 while ((bytestream2_get_bytes_left(&qctx->buffer) > 0) && (height >= 0)) {
133 code = bytestream2_get_byte(&qctx->buffer);
136 /* motion compensation */
137 while((code & 0xF0) == 0xF0) {
140 int me_w, me_h, me_x, me_y;
144 /* get block size by index */
146 me_w = qpeg_table_w[me_idx];
147 me_h = qpeg_table_h[me_idx];
149 /* extract motion vector */
150 corr = bytestream2_get_byte(&qctx->buffer);
162 /* check motion vector */
163 if ((me_x + filled < 0) || (me_x + me_w + filled > width) ||
164 (height - me_y - me_h < 0) || (height - me_y > orig_height) ||
165 (filled + me_w > width) || (height - me_h < 0))
166 av_log(NULL, AV_LOG_ERROR, "Bogus motion vector (%i,%i), block size %ix%i at %i,%i\n",
167 me_x, me_y, me_w, me_h, filled, height);
169 /* do motion compensation */
170 me_plane = refdata + (filled + me_x) + (height - me_y) * width;
171 for(j = 0; j < me_h; j++) {
172 for(i = 0; i < me_w; i++)
173 dst[filled + i - (j * stride)] = me_plane[i - (j * width)];
177 code = bytestream2_get_byte(&qctx->buffer);
181 if(code == 0xE0) /* end-of-picture code */
183 if(code > 0xE0) { /* run code: 0xE1..0xFF */
187 p = bytestream2_get_byte(&qctx->buffer);
188 for(i = 0; i <= code; i++) {
190 if(filled >= width) {
198 } else if(code >= 0xC0) { /* copy code: 0xC0..0xDF */
201 for(i = 0; i <= code; i++) {
202 dst[filled++] = bytestream2_get_byte(&qctx->buffer);
203 if(filled >= width) {
211 } else if(code >= 0x80) { /* skip code: 0x80..0xBF */
215 /* codes 0x80 and 0x81 are actually escape codes,
216 skip value minus constant is in the next byte */
218 skip = bytestream2_get_byte(&qctx->buffer) + 64;
220 skip = bytestream2_get_byte(&qctx->buffer) + 320;
224 while( filled >= width) {
232 /* zero code treated as one-pixel skip */
234 dst[filled++] = ctable[code & 0x7F];
238 if(filled >= width) {
247 static int decode_frame(AVCodecContext *avctx,
248 void *data, int *got_frame,
252 QpegContext * const a = avctx->priv_data;
253 AVFrame * const p = a->pic;
256 const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
258 if (avpkt->size < 0x86) {
259 av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
260 return AVERROR_INVALIDDATA;
263 bytestream2_init(&a->buffer, avpkt->data, avpkt->size);
264 if ((ret = ff_reget_buffer(avctx, p)) < 0) {
265 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
268 outdata = p->data[0];
269 bytestream2_skip(&a->buffer, 4);
270 bytestream2_get_buffer(&a->buffer, ctable, 128);
271 bytestream2_skip(&a->buffer, 1);
273 delta = bytestream2_get_byte(&a->buffer);
275 qpeg_decode_intra(a, outdata, p->linesize[0], avctx->width, avctx->height);
277 qpeg_decode_inter(a, outdata, p->linesize[0], avctx->width, avctx->height, delta, ctable, a->refdata);
280 /* make the palette available on the way out */
282 p->palette_has_changed = 1;
283 memcpy(a->pal, pal, AVPALETTE_SIZE);
285 memcpy(p->data[1], a->pal, AVPALETTE_SIZE);
287 if ((ret = av_frame_ref(data, p)) < 0)
295 static av_cold int decode_end(AVCodecContext *avctx)
297 QpegContext * const a = avctx->priv_data;
299 av_frame_free(&a->pic);
305 static av_cold int decode_init(AVCodecContext *avctx){
306 QpegContext * const a = avctx->priv_data;
309 avctx->pix_fmt= AV_PIX_FMT_PAL8;
310 a->refdata = av_malloc(avctx->width * avctx->height);
312 a->pic = av_frame_alloc();
315 return AVERROR(ENOMEM);
321 AVCodec ff_qpeg_decoder = {
323 .long_name = NULL_IF_CONFIG_SMALL("Q-team QPEG"),
324 .type = AVMEDIA_TYPE_VIDEO,
325 .id = AV_CODEC_ID_QPEG,
326 .priv_data_size = sizeof(QpegContext),
329 .decode = decode_frame,
330 .capabilities = CODEC_CAP_DR1,