libavcodec/vcr1.c
be3564ed
 /*
  * ATI VCR1 codec
  * Copyright (c) 2003 Michael Niedermayer
  *
b78e7197
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
be3564ed
  * 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.
be3564ed
  *
b78e7197
  * FFmpeg is distributed in the hope that it will be useful,
be3564ed
  * 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
be3564ed
  */
115329f1
 
be3564ed
 /**
ba87f080
  * @file
db1e403c
  * ATI VCR1 codec
be3564ed
  */
115329f1
 
be3564ed
 #include "avcodec.h"
594d4d5d
 #include "internal.h"
164b67ca
 #include "libavutil/avassert.h"
1d9c2dc8
 #include "libavutil/internal.h"
be3564ed
 
db1e403c
 typedef struct VCR1Context {
be3564ed
     int delta[16];
     int offset[4];
 } VCR1Context;
 
100c70b0
 static av_cold int vcr1_decode_init(AVCodecContext *avctx)
51c4d870
 {
716d413c
     avctx->pix_fmt = AV_PIX_FMT_YUV410P;
51c4d870
 
845724c8
     if (avctx->width % 8 || avctx->height%4) {
8ba683e6
         avpriv_request_sample(avctx, "odd dimensions (%d x %d) support", avctx->width, avctx->height);
8aba7968
         return AVERROR_INVALIDDATA;
845724c8
     }
8aba7968
 
51c4d870
     return 0;
 }
 
100c70b0
 static int vcr1_decode_frame(AVCodecContext *avctx, void *data,
df9b9567
                              int *got_frame, AVPacket *avpkt)
be3564ed
 {
db1e403c
     VCR1Context *const a      = avctx->priv_data;
759001c5
     AVFrame *const p          = data;
f4e9c768
     const uint8_t *bytestream = avpkt->data;
84b6451d
     const uint8_t *bytestream_end = bytestream + avpkt->size;
74a9a624
     int i, x, y, ret;
be3564ed
 
77ef5388
     if(avpkt->size < 32 + avctx->height + avctx->width*avctx->height*5/8){
88c27193
         av_log(avctx, AV_LOG_ERROR, "Insufficient input data. %d < %d\n", avpkt->size ,  32 + avctx->height + avctx->width*avctx->height*5/8);
8e09482e
         return AVERROR(EINVAL);
     }
 
1ec94b0f
     if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
74a9a624
         return ret;
db1e403c
     p->pict_type = AV_PICTURE_TYPE_I;
     p->key_frame = 1;
be3564ed
 
db1e403c
     for (i = 0; i < 16; i++) {
         a->delta[i] = *bytestream++;
be3564ed
         bytestream++;
     }
115329f1
 
db1e403c
     for (y = 0; y < avctx->height; y++) {
be3564ed
         int offset;
759001c5
         uint8_t *luma = &p->data[0][y * p->linesize[0]];
be3564ed
 
db1e403c
         if ((y & 3) == 0) {
759001c5
             uint8_t *cb = &p->data[1][(y >> 2) * p->linesize[1]];
             uint8_t *cr = &p->data[2][(y >> 2) * p->linesize[2]];
be3564ed
 
84b6451d
             av_assert0 (bytestream_end - bytestream >= 4 + avctx->width);
8aba7968
 
db1e403c
             for (i = 0; i < 4; i++)
                 a->offset[i] = *bytestream++;
be3564ed
 
db1e403c
             offset = a->offset[0] - a->delta[bytestream[2] & 0xF];
             for (x = 0; x < avctx->width; x += 4) {
                 luma[0]     = offset += a->delta[bytestream[2] & 0xF];
                 luma[1]     = offset += a->delta[bytestream[2] >>  4];
                 luma[2]     = offset += a->delta[bytestream[0] & 0xF];
                 luma[3]     = offset += a->delta[bytestream[0] >>  4];
                 luma       += 4;
115329f1
 
db1e403c
                 *cb++       = bytestream[3];
                 *cr++       = bytestream[1];
115329f1
 
db1e403c
                 bytestream += 4;
be3564ed
             }
db1e403c
         } else {
84b6451d
             av_assert0 (bytestream_end - bytestream >= avctx->width / 2);
8aba7968
 
db1e403c
             offset = a->offset[y & 3] - a->delta[bytestream[2] & 0xF];
 
             for (x = 0; x < avctx->width; x += 8) {
                 luma[0]     = offset += a->delta[bytestream[2] & 0xF];
                 luma[1]     = offset += a->delta[bytestream[2] >>  4];
                 luma[2]     = offset += a->delta[bytestream[3] & 0xF];
                 luma[3]     = offset += a->delta[bytestream[3] >>  4];
                 luma[4]     = offset += a->delta[bytestream[0] & 0xF];
                 luma[5]     = offset += a->delta[bytestream[0] >>  4];
                 luma[6]     = offset += a->delta[bytestream[1] & 0xF];
                 luma[7]     = offset += a->delta[bytestream[1] >>  4];
                 luma       += 8;
                 bytestream += 4;
be3564ed
             }
         }
     }
 
df9b9567
     *got_frame = 1;
be3564ed
 
a4e70918
     return bytestream - avpkt->data;
be3564ed
 }
 
e7e2df27
 AVCodec ff_vcr1_decoder = {
ec6402b7
     .name           = "vcr1",
b2bed932
     .long_name      = NULL_IF_CONFIG_SMALL("ATI VCR1"),
ec6402b7
     .type           = AVMEDIA_TYPE_VIDEO,
36ef5369
     .id             = AV_CODEC_ID_VCR1,
ec6402b7
     .priv_data_size = sizeof(VCR1Context),
100c70b0
     .init           = vcr1_decode_init,
     .decode         = vcr1_decode_frame,
def97856
     .capabilities   = AV_CODEC_CAP_DR1,
be3564ed
 };