3 * Copyright (c) 2004 Konstantin Shishkov
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include "mpegvideo.h"
29 typedef struct QpegContext{
30 AVCodecContext *avctx;
35 static void qpeg_decode_intra(uint8_t *src, uint8_t *dst, int size,
36 int stride, int width, int height)
45 dst = dst + height * stride;
51 if(code == 0xFC) /* end-of-picture code */
53 if(code >= 0xF8) { /* very long run */
57 run = ((code & 0x7) << 16) + (c0 << 8) + c1 + 2;
58 } else if (code >= 0xF0) { /* long run */
61 run = ((code & 0xF) << 8) + c0 + 2;
62 } else if (code >= 0xE0) { /* short run */
63 run = (code & 0x1F) + 2;
64 } else if (code >= 0xC0) { /* very long copy */
68 copy = ((code & 0x3F) << 16) + (c0 << 8) + c1 + 1;
69 } else if (code >= 0x80) { /* long copy */
72 copy = ((code & 0x7F) << 8) + c0 + 1;
73 } else { /* short copy */
77 /* perform actual run or copy */
83 for(i = 0; i < run; i++) {
85 if (filled >= width) {
91 for(i = 0; i < copy; i++) {
92 dst[filled++] = *src++;
93 if (filled >= width) {
103 static int qpeg_table_h[16] =
104 { 0x00, 0x20, 0x20, 0x20, 0x18, 0x10, 0x10, 0x20, 0x10, 0x08, 0x18, 0x08, 0x08, 0x18, 0x10, 0x04};
105 static int qpeg_table_w[16] =
106 { 0x00, 0x20, 0x18, 0x08, 0x18, 0x10, 0x20, 0x10, 0x08, 0x10, 0x20, 0x20, 0x08, 0x10, 0x18, 0x04};
108 /* Decodes delta frames */
109 static void qpeg_decode_inter(uint8_t *src, uint8_t *dst, int size,
110 int stride, int width, int height,
111 int delta, uint8_t *ctable, uint8_t *refdata)
118 /* copy prev frame */
119 for(i = 0; i < height; i++)
120 memcpy(refdata + (i * width), dst + (i * stride), width);
122 blkdata = src - 0x86;
124 dst = dst + height * stride;
131 /* motion compensation */
132 while((code & 0xF0) == 0xF0) {
135 int me_w, me_h, me_x, me_y;
139 /* get block size by index */
141 me_w = qpeg_table_w[me_idx];
142 me_h = qpeg_table_h[me_idx];
144 /* extract motion vector */
158 /* do motion compensation */
159 me_plane = refdata + (filled + me_x) + (height - me_y) * width;
160 for(j = 0; j < me_h; j++) {
161 for(i = 0; i < me_w; i++)
162 dst[filled + i - (j * stride)] = me_plane[i - (j * width)];
170 if(code == 0xE0) /* end-of-picture code */
172 if(code > 0xE0) { /* run code: 0xE1..0xFF */
178 for(i = 0; i <= code; i++) {
180 if(filled >= width) {
186 } else if(code >= 0xC0) { /* copy code: 0xC0..0xDF */
189 for(i = 0; i <= code; i++) {
190 dst[filled++] = *src++;
191 if(filled >= width) {
198 } else if(code >= 0x80) { /* skip code: 0x80..0xBF */
202 /* codes 0x80 and 0x81 are actually escape codes,
203 skip value minus constant is in the next byte */
205 skip = (*src++) + 64;
207 skip = (*src++) + 320;
211 while( filled >= width) {
217 /* zero code treated as one-pixel skip */
219 dst[filled++] = ctable[code & 0x7F];
222 if(filled >= width) {
231 static int decode_frame(AVCodecContext *avctx,
232 void *data, int *data_size,
233 uint8_t *buf, int buf_size)
235 QpegContext * const a = avctx->priv_data;
236 AVFrame * const p= (AVFrame*)&a->pic;
241 avctx->release_buffer(avctx, p);
244 if(avctx->get_buffer(avctx, p) < 0){
245 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
248 outdata = a->pic.data[0];
249 if(buf[0x85] == 0x10) {
250 qpeg_decode_intra(buf+0x86, outdata, buf_size - 0x86, a->pic.linesize[0], avctx->width, avctx->height);
253 qpeg_decode_inter(buf+0x86, outdata, buf_size - 0x86, a->pic.linesize[0], avctx->width, avctx->height, delta, buf + 4, a->refdata);
256 /* make the palette available on the way out */
257 memcpy(a->pic.data[1], a->avctx->palctrl->palette, AVPALETTE_SIZE);
258 if (a->avctx->palctrl->palette_changed) {
259 a->pic.palette_has_changed = 1;
260 a->avctx->palctrl->palette_changed = 0;
263 *data_size = sizeof(AVFrame);
264 *(AVFrame*)data = a->pic;
269 static int decode_init(AVCodecContext *avctx){
270 QpegContext * const a = avctx->priv_data;
273 avctx->pix_fmt= PIX_FMT_PAL8;
274 avctx->has_b_frames = 0;
275 a->pic.data[0] = NULL;
276 a->refdata = av_malloc(avctx->width * avctx->height);
281 static int decode_end(AVCodecContext *avctx){
282 QpegContext * const a = avctx->priv_data;
283 AVFrame * const p= (AVFrame*)&a->pic;
286 avctx->release_buffer(avctx, p);
292 AVCodec qpeg_decoder = {