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"
30 typedef struct QpegContext{
31 AVCodecContext *avctx;
34 GetByteContext buffer;
37 static void qpeg_decode_intra(QpegContext *qctx, uint8_t *dst,
38 int stride, int width, int height)
49 dst = dst + height * stride;
51 while ((bytestream2_get_bytes_left(&qctx->buffer) > 0) && (rows_to_go > 0)) {
52 code = bytestream2_get_byte(&qctx->buffer);
54 if(code == 0xFC) /* end-of-picture code */
56 if(code >= 0xF8) { /* very long run */
57 c0 = bytestream2_get_byte(&qctx->buffer);
58 c1 = bytestream2_get_byte(&qctx->buffer);
59 run = ((code & 0x7) << 16) + (c0 << 8) + c1 + 2;
60 } else if (code >= 0xF0) { /* long run */
61 c0 = bytestream2_get_byte(&qctx->buffer);
62 run = ((code & 0xF) << 8) + c0 + 2;
63 } else if (code >= 0xE0) { /* short run */
64 run = (code & 0x1F) + 2;
65 } else if (code >= 0xC0) { /* very long copy */
66 c0 = bytestream2_get_byte(&qctx->buffer);
67 c1 = bytestream2_get_byte(&qctx->buffer);
68 copy = ((code & 0x3F) << 16) + (c0 << 8) + c1 + 1;
69 } else if (code >= 0x80) { /* long copy */
70 c0 = bytestream2_get_byte(&qctx->buffer);
71 copy = ((code & 0x7F) << 8) + c0 + 1;
72 } else { /* short copy */
76 /* perform actual run or copy */
80 p = bytestream2_get_byte(&qctx->buffer);
81 for(i = 0; i < run; i++) {
83 if (filled >= width) {
92 for(i = 0; i < copy; i++) {
93 dst[filled++] = bytestream2_get_byte(&qctx->buffer);
94 if (filled >= width) {
106 static const int qpeg_table_h[16] =
107 { 0x00, 0x20, 0x20, 0x20, 0x18, 0x10, 0x10, 0x20, 0x10, 0x08, 0x18, 0x08, 0x08, 0x18, 0x10, 0x04};
108 static const int qpeg_table_w[16] =
109 { 0x00, 0x20, 0x18, 0x08, 0x18, 0x10, 0x20, 0x10, 0x08, 0x10, 0x20, 0x20, 0x08, 0x10, 0x18, 0x04};
111 /* Decodes delta frames */
112 static void qpeg_decode_inter(QpegContext *qctx, uint8_t *dst,
113 int stride, int width, int height,
114 int delta, const uint8_t *ctable,
125 /* copy prev frame */
126 for(i = 0; i < height; i++)
127 memcpy(dst + (i * stride), refdata + (i * stride), width);
129 orig_height = height;
131 dst = dst + height * stride;
133 while ((bytestream2_get_bytes_left(&qctx->buffer) > 0) && (height >= 0)) {
134 code = bytestream2_get_byte(&qctx->buffer);
137 /* motion compensation */
138 while(bytestream2_get_bytes_left(&qctx->buffer) > 0 && (code & 0xF0) == 0xF0) {
141 int me_w, me_h, me_x, me_y;
145 /* get block size by index */
147 me_w = qpeg_table_w[me_idx];
148 me_h = qpeg_table_h[me_idx];
150 /* extract motion vector */
151 corr = bytestream2_get_byte(&qctx->buffer);
163 /* check motion vector */
164 if ((me_x + filled < 0) || (me_x + me_w + filled > width) ||
165 (height - me_y - me_h < 0) || (height - me_y > orig_height) ||
166 (filled + me_w > width) || (height - me_h < 0))
167 av_log(NULL, AV_LOG_ERROR, "Bogus motion vector (%i,%i), block size %ix%i at %i,%i\n",
168 me_x, me_y, me_w, me_h, filled, height);
170 /* do motion compensation */
171 me_plane = refdata + (filled + me_x) + (height - me_y) * stride;
172 for(j = 0; j < me_h; j++) {
173 for(i = 0; i < me_w; i++)
174 dst[filled + i - (j * stride)] = me_plane[i - (j * stride)];
178 code = bytestream2_get_byte(&qctx->buffer);
182 if(code == 0xE0) /* end-of-picture code */
184 if(code > 0xE0) { /* run code: 0xE1..0xFF */
188 p = bytestream2_get_byte(&qctx->buffer);
189 for(i = 0; i <= code; i++) {
191 if(filled >= width) {
199 } else if(code >= 0xC0) { /* copy code: 0xC0..0xDF */
202 if(code + 1 > bytestream2_get_bytes_left(&qctx->buffer))
205 for(i = 0; i <= code; i++) {
206 dst[filled++] = bytestream2_get_byte(&qctx->buffer);
207 if(filled >= width) {
215 } else if(code >= 0x80) { /* skip code: 0x80..0xBF */
219 /* codes 0x80 and 0x81 are actually escape codes,
220 skip value minus constant is in the next byte */
222 skip = bytestream2_get_byte(&qctx->buffer) + 64;
224 skip = bytestream2_get_byte(&qctx->buffer) + 320;
228 while( filled >= width) {
236 /* zero code treated as one-pixel skip */
238 dst[filled++] = ctable[code & 0x7F];
242 if(filled >= width) {
251 static int decode_frame(AVCodecContext *avctx,
252 void *data, int *data_size,
256 QpegContext * const a = avctx->priv_data;
257 AVFrame * p = &a->pic;
258 AVFrame * ref= &a->ref;
261 const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
263 if (avpkt->size < 0x86) {
264 av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
265 return AVERROR_INVALIDDATA;
268 bytestream2_init(&a->buffer, avpkt->data, avpkt->size);
271 avctx->release_buffer(avctx, ref);
272 FFSWAP(AVFrame, *ref, *p);
275 if(avctx->get_buffer(avctx, p) < 0){
276 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
279 outdata = a->pic.data[0];
280 bytestream2_skip(&a->buffer, 4);
281 bytestream2_get_buffer(&a->buffer, ctable, 128);
282 bytestream2_skip(&a->buffer, 1);
284 delta = bytestream2_get_byte(&a->buffer);
286 qpeg_decode_intra(a, outdata, a->pic.linesize[0], avctx->width, avctx->height);
288 qpeg_decode_inter(a, outdata, a->pic.linesize[0], avctx->width, avctx->height, delta, ctable, a->ref.data[0]);
291 /* make the palette available on the way out */
293 a->pic.palette_has_changed = 1;
294 memcpy(a->pal, pal, AVPALETTE_SIZE);
296 memcpy(a->pic.data[1], a->pal, AVPALETTE_SIZE);
298 *data_size = sizeof(AVFrame);
299 *(AVFrame*)data = a->pic;
304 static av_cold int decode_init(AVCodecContext *avctx){
305 QpegContext * const a = avctx->priv_data;
307 avcodec_get_frame_defaults(&a->pic);
308 avcodec_get_frame_defaults(&a->ref);
310 avctx->pix_fmt= AV_PIX_FMT_PAL8;
315 static av_cold int decode_end(AVCodecContext *avctx){
316 QpegContext * const a = avctx->priv_data;
317 AVFrame * const p = &a->pic;
318 AVFrame * const ref= &a->ref;
321 avctx->release_buffer(avctx, p);
323 avctx->release_buffer(avctx, ref);
328 AVCodec ff_qpeg_decoder = {
330 .type = AVMEDIA_TYPE_VIDEO,
331 .id = AV_CODEC_ID_QPEG,
332 .priv_data_size = sizeof(QpegContext),
335 .decode = decode_frame,
336 .capabilities = CODEC_CAP_DR1,
337 .long_name = NULL_IF_CONFIG_SMALL("Q-team QPEG"),