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"
fbd4293d
 #include "dsputil.h"
be3564ed
 
db1e403c
 typedef struct VCR1Context {
be3564ed
     AVFrame picture;
     int delta[16];
     int offset[4];
 } VCR1Context;
 
100c70b0
 static av_cold int vcr1_common_init(AVCodecContext *avctx)
51c4d870
 {
     VCR1Context *const a = avctx->priv_data;
 
     avctx->coded_frame = &a->picture;
01590329
     avcodec_get_frame_defaults(&a->picture);
a761e595
 
     return 0;
51c4d870
 }
 
100c70b0
 static av_cold int vcr1_decode_init(AVCodecContext *avctx)
51c4d870
 {
100c70b0
     vcr1_common_init(avctx);
51c4d870
 
     avctx->pix_fmt = PIX_FMT_YUV410P;
 
     return 0;
 }
 
100c70b0
 static av_cold int vcr1_decode_end(AVCodecContext *avctx)
51c4d870
 {
     VCR1Context *s = avctx->priv_data;
 
     if (s->picture.data[0])
         avctx->release_buffer(avctx, &s->picture);
 
     return 0;
 }
 
100c70b0
 static int vcr1_decode_frame(AVCodecContext *avctx, void *data,
                              int *data_size, AVPacket *avpkt)
be3564ed
 {
db1e403c
     const uint8_t *buf        = avpkt->data;
     int buf_size              = avpkt->size;
     VCR1Context *const a      = avctx->priv_data;
     AVFrame *picture          = data;
     AVFrame *const p          = &a->picture;
     const uint8_t *bytestream = buf;
be3564ed
     int i, x, y;
 
db1e403c
     if (p->data[0])
be3564ed
         avctx->release_buffer(avctx, p);
 
8e09482e
     if(buf_size < 16 + avctx->height + avctx->width*avctx->height*5/8){
         av_log(avctx, AV_LOG_ERROR, "Insufficient input data.\n");
         return AVERROR(EINVAL);
     }
 
db1e403c
     p->reference = 0;
     if (avctx->get_buffer(avctx, p) < 0) {
9b879566
         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
be3564ed
         return -1;
     }
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;
db1e403c
         uint8_t *luma = &a->picture.data[0][y * a->picture.linesize[0]];
be3564ed
 
db1e403c
         if ((y & 3) == 0) {
             uint8_t *cb = &a->picture.data[1][(y >> 2) * a->picture.linesize[1]];
             uint8_t *cr = &a->picture.data[2][(y >> 2) * a->picture.linesize[2]];
be3564ed
 
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 {
             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
             }
         }
     }
 
324deaa2
     *picture   = a->picture;
be3564ed
     *data_size = sizeof(AVPicture);
 
     return buf_size;
 }
 
e7e2df27
 AVCodec ff_vcr1_decoder = {
ec6402b7
     .name           = "vcr1",
     .type           = AVMEDIA_TYPE_VIDEO,
     .id             = CODEC_ID_VCR1,
     .priv_data_size = sizeof(VCR1Context),
100c70b0
     .init           = vcr1_decode_init,
     .close          = vcr1_decode_end,
     .decode         = vcr1_decode_frame,
ec6402b7
     .capabilities   = CODEC_CAP_DR1,
00c3b67b
     .long_name      = NULL_IF_CONFIG_SMALL("ATI VCR1"),
be3564ed
 };
c7ac9449
 
51c4d870
 /* Disable the encoder. */
 #undef CONFIG_VCR1_ENCODER
 #define CONFIG_VCR1_ENCODER 0
 
b250f9c6
 #if CONFIG_VCR1_ENCODER
55da88c5
 
 #include "put_bits.h"
 
100c70b0
 static int vcr1_encode_frame(AVCodecContext *avctx, unsigned char *buf,
                              int buf_size, void *data)
db1e403c
 {
     VCR1Context *const a = avctx->priv_data;
     AVFrame *pict        = data;
     AVFrame *const p     = &a->picture;
be3564ed
     int size;
 
db1e403c
     *p           = *pict;
     p->pict_type = AV_PICTURE_TYPE_I;
     p->key_frame = 1;
be3564ed
 
9f51c682
     avpriv_align_put_bits(&a->pb);
55da88c5
     flush_put_bits(&a->pb);
115329f1
 
55da88c5
     size = put_bits_count(&a->pb) / 32;
115329f1
 
db1e403c
     return size * 4;
be3564ed
 }
cc5d4f4c
 
e7e2df27
 AVCodec ff_vcr1_encoder = {
ec6402b7
     .name           = "vcr1",
     .type           = AVMEDIA_TYPE_VIDEO,
     .id             = CODEC_ID_VCR1,
     .priv_data_size = sizeof(VCR1Context),
100c70b0
     .init           = vcr1_common_init,
     .encode         = vcr1_encode_frame,
00c3b67b
     .long_name      = NULL_IF_CONFIG_SMALL("ATI VCR1"),
be3564ed
 };
db1e403c
 #endif /* CONFIG_VCR1_ENCODER */