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
29 typedef struct QpegContext{
30 AVCodecContext *avctx;
36 static void qpeg_decode_intra(const uint8_t *src, uint8_t *dst, int size,
37 int stride, int width, int height)
48 dst = dst + height * stride;
50 while((size > 0) && (rows_to_go > 0)) {
54 if(code == 0xFC) /* end-of-picture code */
56 if(code >= 0xF8) { /* very long run */
60 run = ((code & 0x7) << 16) + (c0 << 8) + c1 + 2;
61 } else if (code >= 0xF0) { /* long run */
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 */
71 copy = ((code & 0x3F) << 16) + (c0 << 8) + c1 + 1;
72 } else if (code >= 0x80) { /* long copy */
75 copy = ((code & 0x7F) << 8) + c0 + 1;
76 } else { /* short copy */
80 /* perform actual run or copy */
86 for(i = 0; i < run; i++) {
88 if (filled >= width) {
98 for(i = 0; i < copy; i++) {
99 dst[filled++] = *src++;
100 if (filled >= width) {
112 static const int qpeg_table_h[16] =
113 { 0x00, 0x20, 0x20, 0x20, 0x18, 0x10, 0x10, 0x20, 0x10, 0x08, 0x18, 0x08, 0x08, 0x18, 0x10, 0x04};
114 static const int qpeg_table_w[16] =
115 { 0x00, 0x20, 0x18, 0x08, 0x18, 0x10, 0x20, 0x10, 0x08, 0x10, 0x20, 0x20, 0x08, 0x10, 0x18, 0x04};
117 /* Decodes delta frames */
118 static void qpeg_decode_inter(const uint8_t *src, uint8_t *dst, int size,
119 int stride, int width, int height,
120 int delta, const uint8_t *ctable, uint8_t *refdata)
127 /* copy prev frame */
128 for(i = 0; i < height; i++)
129 memcpy(refdata + (i * width), dst + (i * stride), width);
131 orig_height = height;
133 dst = dst + height * stride;
135 while((size > 0) && (height >= 0)) {
140 /* motion compensation */
141 while((code & 0xF0) == 0xF0) {
144 int me_w, me_h, me_x, me_y;
148 /* get block size by index */
150 me_w = qpeg_table_w[me_idx];
151 me_h = qpeg_table_h[me_idx];
153 /* extract motion vector */
167 /* check motion vector */
168 if ((me_x + filled < 0) || (me_x + me_w + filled > width) ||
169 (height - me_y - me_h < 0) || (height - me_y > orig_height) ||
170 (filled + me_w > width) || (height - me_h < 0))
171 av_log(NULL, AV_LOG_ERROR, "Bogus motion vector (%i,%i), block size %ix%i at %i,%i\n",
172 me_x, me_y, me_w, me_h, filled, height);
174 /* do motion compensation */
175 me_plane = refdata + (filled + me_x) + (height - me_y) * width;
176 for(j = 0; j < me_h; j++) {
177 for(i = 0; i < me_w; i++)
178 dst[filled + i - (j * stride)] = me_plane[i - (j * width)];
187 if(code == 0xE0) /* end-of-picture code */
189 if(code > 0xE0) { /* run code: 0xE1..0xFF */
195 for(i = 0; i <= code; i++) {
197 if(filled >= width) {
203 } else if(code >= 0xC0) { /* copy code: 0xC0..0xDF */
206 for(i = 0; i <= code; i++) {
207 dst[filled++] = *src++;
208 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 = (*src++) + 64;
224 skip = (*src++) + 320;
228 while( filled >= width) {
236 /* zero code treated as one-pixel skip */
238 dst[filled++] = ctable[code & 0x7F];
241 if(filled >= width) {
250 static int decode_frame(AVCodecContext *avctx,
251 void *data, int *data_size,
254 const uint8_t *buf = avpkt->data;
255 int buf_size = avpkt->size;
256 QpegContext * const a = avctx->priv_data;
257 AVFrame * const p= (AVFrame*)&a->pic;
260 const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
263 if (avctx->reget_buffer(avctx, p) < 0) {
264 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
267 outdata = a->pic.data[0];
268 if(buf[0x85] == 0x10) {
269 qpeg_decode_intra(buf+0x86, outdata, buf_size - 0x86, a->pic.linesize[0], avctx->width, avctx->height);
272 qpeg_decode_inter(buf+0x86, outdata, buf_size - 0x86, a->pic.linesize[0], avctx->width, avctx->height, delta, buf + 4, a->refdata);
275 /* make the palette available on the way out */
277 a->pic.palette_has_changed = 1;
278 memcpy(a->pal, pal, AVPALETTE_SIZE);
280 memcpy(a->pic.data[1], a->pal, AVPALETTE_SIZE);
282 *data_size = sizeof(AVFrame);
283 *(AVFrame*)data = a->pic;
288 static av_cold int decode_init(AVCodecContext *avctx){
289 QpegContext * const a = avctx->priv_data;
292 avctx->pix_fmt= PIX_FMT_PAL8;
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 ff_qpeg_decoder = {
311 .type = AVMEDIA_TYPE_VIDEO,
313 .priv_data_size = sizeof(QpegContext),
316 .decode = decode_frame,
317 .capabilities = CODEC_CAP_DR1,
318 .long_name = NULL_IF_CONFIG_SMALL("Q-team QPEG"),