libavcodec/qpeg.c
acfd8f0f
 /*
  * QPEG codec
  * Copyright (c) 2004 Konstantin Shishkov
  *
b78e7197
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
acfd8f0f
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
b78e7197
  * version 2.1 of the License, or (at your option) any later version.
acfd8f0f
  *
b78e7197
  * FFmpeg is distributed in the hope that it will be useful,
acfd8f0f
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
b78e7197
  * License along with FFmpeg; if not, write to the Free Software
5509bffa
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
acfd8f0f
  */
115329f1
 
acfd8f0f
 /**
ba87f080
  * @file
acfd8f0f
  * QPEG codec.
  */
115329f1
 
acfd8f0f
 #include "avcodec.h"
3e9cd8b4
 #include "bytestream.h"
874c5b02
 #include "internal.h"
acfd8f0f
 
 typedef struct QpegContext{
     AVCodecContext *avctx;
5219afc0
     AVFrame *pic, *ref;
2d8591c2
     uint32_t pal[256];
3e9cd8b4
     GetByteContext buffer;
acfd8f0f
 } QpegContext;
 
3e9cd8b4
 static void qpeg_decode_intra(QpegContext *qctx, uint8_t *dst,
                               int stride, int width, int height)
acfd8f0f
 {
     int i;
     int code;
     int c0, c1;
     int run, copy;
     int filled = 0;
f63166f8
     int rows_to_go;
115329f1
 
f63166f8
     rows_to_go = height;
acfd8f0f
     height--;
     dst = dst + height * stride;
115329f1
 
3e9cd8b4
     while ((bytestream2_get_bytes_left(&qctx->buffer) > 0) && (rows_to_go > 0)) {
         code = bytestream2_get_byte(&qctx->buffer);
bb270c08
         run = copy = 0;
         if(code == 0xFC) /* end-of-picture code */
             break;
         if(code >= 0xF8) { /* very long run */
3e9cd8b4
             c0 = bytestream2_get_byte(&qctx->buffer);
             c1 = bytestream2_get_byte(&qctx->buffer);
bb270c08
             run = ((code & 0x7) << 16) + (c0 << 8) + c1 + 2;
         } else if (code >= 0xF0) { /* long run */
3e9cd8b4
             c0 = bytestream2_get_byte(&qctx->buffer);
bb270c08
             run = ((code & 0xF) << 8) + c0 + 2;
         } else if (code >= 0xE0) { /* short run */
             run = (code & 0x1F) + 2;
         } else if (code >= 0xC0) { /* very long copy */
3e9cd8b4
             c0 = bytestream2_get_byte(&qctx->buffer);
             c1 = bytestream2_get_byte(&qctx->buffer);
bb270c08
             copy = ((code & 0x3F) << 16) + (c0 << 8) + c1 + 1;
         } else if (code >= 0x80) { /* long copy */
3e9cd8b4
             c0 = bytestream2_get_byte(&qctx->buffer);
bb270c08
             copy = ((code & 0x7F) << 8) + c0 + 1;
         } else { /* short copy */
             copy = code + 1;
         }
115329f1
 
bb270c08
         /* perform actual run or copy */
         if(run) {
             int p;
115329f1
 
3e9cd8b4
             p = bytestream2_get_byte(&qctx->buffer);
bb270c08
             for(i = 0; i < run; i++) {
                 dst[filled++] = p;
                 if (filled >= width) {
                     filled = 0;
                     dst -= stride;
f63166f8
                     rows_to_go--;
                     if(rows_to_go <= 0)
                         break;
bb270c08
                 }
             }
         } else {
             for(i = 0; i < copy; i++) {
3e9cd8b4
                 dst[filled++] = bytestream2_get_byte(&qctx->buffer);
bb270c08
                 if (filled >= width) {
                     filled = 0;
                     dst -= stride;
f63166f8
                     rows_to_go--;
                     if(rows_to_go <= 0)
                         break;
bb270c08
                 }
             }
         }
acfd8f0f
     }
 }
 
cf2baeb3
 static const int qpeg_table_h[16] =
acfd8f0f
  { 0x00, 0x20, 0x20, 0x20, 0x18, 0x10, 0x10, 0x20, 0x10, 0x08, 0x18, 0x08, 0x08, 0x18, 0x10, 0x04};
cf2baeb3
 static const int qpeg_table_w[16] =
acfd8f0f
  { 0x00, 0x20, 0x18, 0x08, 0x18, 0x10, 0x20, 0x10, 0x08, 0x10, 0x20, 0x20, 0x08, 0x10, 0x18, 0x04};
115329f1
 
acfd8f0f
 /* Decodes delta frames */
f13f139f
 static void av_noinline qpeg_decode_inter(QpegContext *qctx, uint8_t *dst,
3e9cd8b4
                               int stride, int width, int height,
                               int delta, const uint8_t *ctable,
                               uint8_t *refdata)
acfd8f0f
 {
     int i, j;
     int code;
     int filled = 0;
f63166f8
     int orig_height;
115329f1
 
92170669
     if (refdata) {
         /* copy prev frame */
         for (i = 0; i < height; i++)
             memcpy(dst + (i * stride), refdata + (i * stride), width);
     } else {
         refdata = dst;
     }
115329f1
 
f63166f8
     orig_height = height;
acfd8f0f
     height--;
     dst = dst + height * stride;
 
3e9cd8b4
     while ((bytestream2_get_bytes_left(&qctx->buffer) > 0) && (height >= 0)) {
         code = bytestream2_get_byte(&qctx->buffer);
115329f1
 
bb270c08
         if(delta) {
             /* motion compensation */
2af8f2ce
             while(bytestream2_get_bytes_left(&qctx->buffer) > 0 && (code & 0xF0) == 0xF0) {
bb270c08
                 if(delta == 1) {
                     int me_idx;
                     int me_w, me_h, me_x, me_y;
                     uint8_t *me_plane;
                     int corr, val;
115329f1
 
bb270c08
                     /* get block size by index */
                     me_idx = code & 0xF;
                     me_w = qpeg_table_w[me_idx];
                     me_h = qpeg_table_h[me_idx];
115329f1
 
bb270c08
                     /* extract motion vector */
3e9cd8b4
                     corr = bytestream2_get_byte(&qctx->buffer);
acfd8f0f
 
bb270c08
                     val = corr >> 4;
                     if(val > 7)
                         val -= 16;
                     me_x = val;
115329f1
 
bb270c08
                     val = corr & 0xF;
                     if(val > 7)
                         val -= 16;
                     me_y = val;
115329f1
 
f63166f8
                     /* check motion vector */
                     if ((me_x + filled < 0) || (me_x + me_w + filled > width) ||
dd3bfe3c
                        (height - me_y - me_h < 0) || (height - me_y >= orig_height) ||
f63166f8
                        (filled + me_w > width) || (height - me_h < 0))
bba9d8bd
                         av_log(qctx->avctx, AV_LOG_ERROR, "Bogus motion vector (%i,%i), block size %ix%i at %i,%i\n",
f63166f8
                                me_x, me_y, me_w, me_h, filled, height);
                     else {
                         /* do motion compensation */
a0db7424
                         me_plane = refdata + (filled + me_x) + (height - me_y) * stride;
f63166f8
                         for(j = 0; j < me_h; j++) {
                             for(i = 0; i < me_w; i++)
a0db7424
                                 dst[filled + i - (j * stride)] = me_plane[i - (j * stride)];
f63166f8
                         }
bb270c08
                     }
                 }
3e9cd8b4
                 code = bytestream2_get_byte(&qctx->buffer);
bb270c08
             }
         }
115329f1
 
bb270c08
         if(code == 0xE0) /* end-of-picture code */
             break;
         if(code > 0xE0) { /* run code: 0xE1..0xFF */
             int p;
acfd8f0f
 
bb270c08
             code &= 0x1F;
3e9cd8b4
             p = bytestream2_get_byte(&qctx->buffer);
bb270c08
             for(i = 0; i <= code; i++) {
                 dst[filled++] = p;
                 if(filled >= width) {
                     filled = 0;
                     dst -= stride;
                     height--;
7a5a5572
                     if (height < 0)
4299dfa5
                         break;
bb270c08
                 }
             }
         } else if(code >= 0xC0) { /* copy code: 0xC0..0xDF */
             code &= 0x1F;
115329f1
 
2af8f2ce
             if(code + 1 > bytestream2_get_bytes_left(&qctx->buffer))
81d4b3af
                 break;
 
bb270c08
             for(i = 0; i <= code; i++) {
3e9cd8b4
                 dst[filled++] = bytestream2_get_byte(&qctx->buffer);
bb270c08
                 if(filled >= width) {
                     filled = 0;
                     dst -= stride;
                     height--;
7a5a5572
                     if (height < 0)
4299dfa5
                         break;
bb270c08
                 }
             }
         } else if(code >= 0x80) { /* skip code: 0x80..0xBF */
             int skip;
115329f1
 
bb270c08
             code &= 0x3F;
             /* codes 0x80 and 0x81 are actually escape codes,
                skip value minus constant is in the next byte */
             if(!code)
3e9cd8b4
                 skip = bytestream2_get_byte(&qctx->buffer) +  64;
bb270c08
             else if(code == 1)
3e9cd8b4
                 skip = bytestream2_get_byte(&qctx->buffer) + 320;
bb270c08
             else
                 skip = code;
             filled += skip;
             while( filled >= width) {
                 filled -= width;
                 dst -= stride;
                 height--;
f63166f8
                 if(height < 0)
                     break;
bb270c08
             }
         } else {
             /* zero code treated as one-pixel skip */
3e9cd8b4
             if(code) {
bb270c08
                 dst[filled++] = ctable[code & 0x7F];
3e9cd8b4
             }
bb270c08
             else
                 filled++;
             if(filled >= width) {
                 filled = 0;
                 dst -= stride;
                 height--;
             }
         }
acfd8f0f
     }
 }
 
115329f1
 static int decode_frame(AVCodecContext *avctx,
df9b9567
                         void *data, int *got_frame,
7a00bbad
                         AVPacket *avpkt)
acfd8f0f
 {
3e9cd8b4
     uint8_t ctable[128];
acfd8f0f
     QpegContext * const a = avctx->priv_data;
55f954e7
     AVFrame * const p = a->pic;
5219afc0
     AVFrame * const ref = a->ref;
acfd8f0f
     uint8_t* outdata;
62d96552
     int delta, ret;
16793504
     int pal_size;
     const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, &pal_size);
115329f1
 
3e9cd8b4
     if (avpkt->size < 0x86) {
         av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
         return AVERROR_INVALIDDATA;
     }
 
     bytestream2_init(&a->buffer, avpkt->data, avpkt->size);
2af8f2ce
 
80e9e63c
     av_frame_unref(ref);
     av_frame_move_ref(ref, p);
90bcbc79
 
1ec94b0f
     if ((ret = ff_get_buffer(avctx, p, AV_GET_BUFFER_FLAG_REF)) < 0)
62d96552
         return ret;
55f954e7
     outdata = p->data[0];
3e9cd8b4
     bytestream2_skip(&a->buffer, 4);
     bytestream2_get_buffer(&a->buffer, ctable, 128);
     bytestream2_skip(&a->buffer, 1);
 
     delta = bytestream2_get_byte(&a->buffer);
     if(delta == 0x10) {
55f954e7
         qpeg_decode_intra(a, outdata, p->linesize[0], avctx->width, avctx->height);
acfd8f0f
     } else {
5219afc0
         qpeg_decode_inter(a, outdata, p->linesize[0], avctx->width, avctx->height, delta, ctable, ref->data[0]);
acfd8f0f
     }
 
     /* make the palette available on the way out */
16793504
     if (pal && pal_size == AVPALETTE_SIZE) {
55f954e7
         p->palette_has_changed = 1;
2d8591c2
         memcpy(a->pal, pal, AVPALETTE_SIZE);
16793504
     } else if (pal) {
         av_log(avctx, AV_LOG_ERROR, "Palette size %d is wrong\n", pal_size);
acfd8f0f
     }
55f954e7
     memcpy(p->data[1], a->pal, AVPALETTE_SIZE);
acfd8f0f
 
55f954e7
     if ((ret = av_frame_ref(data, p)) < 0)
759001c5
         return ret;
 
df9b9567
     *got_frame      = 1;
115329f1
 
3e9cd8b4
     return avpkt->size;
acfd8f0f
 }
 
2c923983
 static void decode_flush(AVCodecContext *avctx){
     QpegContext * const a = avctx->priv_data;
     int i, pal_size;
     const uint8_t *pal_src;
 
     pal_size = FFMIN(1024U, avctx->extradata_size);
     pal_src = avctx->extradata + avctx->extradata_size - pal_size;
 
     for (i=0; i<pal_size/4; i++)
         a->pal[i] = 0xFFU<<24 | AV_RL32(pal_src+4*i);
 }
 
55f954e7
 static av_cold int decode_end(AVCodecContext *avctx)
 {
acfd8f0f
     QpegContext * const a = avctx->priv_data;
115329f1
 
55f954e7
     av_frame_free(&a->pic);
5219afc0
     av_frame_free(&a->ref);
3b199d29
 
acfd8f0f
     return 0;
 }
 
98a6fff9
 static av_cold int decode_init(AVCodecContext *avctx){
acfd8f0f
     QpegContext * const a = avctx->priv_data;
115329f1
 
acfd8f0f
     a->avctx = avctx;
716d413c
     avctx->pix_fmt= AV_PIX_FMT_PAL8;
acfd8f0f
 
2c923983
     decode_flush(avctx);
 
55f954e7
     a->pic = av_frame_alloc();
5219afc0
     a->ref = av_frame_alloc();
     if (!a->pic || !a->ref) {
55f954e7
         decode_end(avctx);
         return AVERROR(ENOMEM);
     }
acfd8f0f
 
     return 0;
 }
 
e7e2df27
 AVCodec ff_qpeg_decoder = {
ec6402b7
     .name           = "qpeg",
b2bed932
     .long_name      = NULL_IF_CONFIG_SMALL("Q-team QPEG"),
ec6402b7
     .type           = AVMEDIA_TYPE_VIDEO,
36ef5369
     .id             = AV_CODEC_ID_QPEG,
ec6402b7
     .priv_data_size = sizeof(QpegContext),
     .init           = decode_init,
     .close          = decode_end,
     .decode         = decode_frame,
2c923983
     .flush          = decode_flush,
def97856
     .capabilities   = AV_CODEC_CAP_DR1,
acfd8f0f
 };