libavcodec/cyuv.c
f70f7c6d
 /*
7b94177e
  * Creative YUV (CYUV) Video Decoder
  *   by Mike Melanson (melanson@pcisys.net)
  * based on "Creative YUV (CYUV) stream format for AVI":
  *   http://www.csse.monash.edu.au/~timf/videocodec/cyuv.txt
f70f7c6d
  *
41ed7ab4
  * Copyright (C) 2003 The FFmpeg project
f70f7c6d
  *
b78e7197
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
f70f7c6d
  * 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.
f70f7c6d
  *
b78e7197
  * FFmpeg is distributed in the hope that it will be useful,
f70f7c6d
  * 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
f70f7c6d
  */
 
983e3246
 /**
ba87f080
  * @file
983e3246
  * Creative YUV (CYUV) Video Decoder.
  */
115329f1
 
f70f7c6d
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
 #include "avcodec.h"
594d4d5d
 #include "internal.h"
1d9c2dc8
 #include "libavutil/internal.h"
f70f7c6d
 
 
 typedef struct CyuvDecodeContext {
     AVCodecContext *avctx;
     int width, height;
 } CyuvDecodeContext;
 
98a6fff9
 static av_cold int cyuv_decode_init(AVCodecContext *avctx)
f70f7c6d
 {
     CyuvDecodeContext *s = avctx->priv_data;
 
     s->avctx = avctx;
     s->width = avctx->width;
6b892a42
     /* width needs to be divisible by 4 for this codec to work */
     if (s->width & 0x3)
0859eaa0
         return AVERROR_INVALIDDATA;
f70f7c6d
     s->height = avctx->height;
 
     return 0;
 }
 
115329f1
 static int cyuv_decode_frame(AVCodecContext *avctx,
df9b9567
                              void *data, int *got_frame,
7a00bbad
                              AVPacket *avpkt)
f70f7c6d
 {
7a00bbad
     const uint8_t *buf = avpkt->data;
     int buf_size = avpkt->size;
f70f7c6d
     CyuvDecodeContext *s=avctx->priv_data;
759001c5
     AVFrame *frame = data;
f70f7c6d
 
     unsigned char *y_plane;
     unsigned char *u_plane;
     unsigned char *v_plane;
     int y_ptr;
     int u_ptr;
     int v_ptr;
 
     /* prediction error tables (make it clear that they are signed values) */
75739ba2
     const signed char *y_table = (const signed char*)buf +  0;
     const signed char *u_table = (const signed char*)buf + 16;
     const signed char *v_table = (const signed char*)buf + 32;
f70f7c6d
 
     unsigned char y_pred, u_pred, v_pred;
     int stream_ptr;
     unsigned char cur_byte;
     int pixel_groups;
01aa664f
     int rawsize = s->height * FFALIGN(s->width,2) * 2;
0859eaa0
     int ret;
f70f7c6d
 
36ef5369
     if (avctx->codec_id == AV_CODEC_ID_AURA) {
588f8cd8
         y_table = u_table;
         u_table = v_table;
     }
f70f7c6d
     /* sanity check the buffer size: A buffer has 3x16-bytes tables
      * followed by (height) lines each with 3 bytes to represent groups
      * of 4 pixels. Thus, the total size of the buffer ought to be:
      *    (3 * 16) + height * (width * 3 / 4) */
01aa664f
     if (buf_size == 48 + s->height * (s->width * 3 / 4)) {
ac627b3d
         avctx->pix_fmt = AV_PIX_FMT_YUV411P;
01aa664f
     } else if(buf_size == rawsize ) {
ac627b3d
         avctx->pix_fmt = AV_PIX_FMT_UYVY422;
01aa664f
     } else {
c5879106
         av_log(avctx, AV_LOG_ERROR, "got a buffer with %d bytes when %d were expected\n",
55903423
                buf_size, 48 + s->height * (s->width * 3 / 4));
0859eaa0
         return AVERROR_INVALIDDATA;
f70f7c6d
     }
 
     /* pixel data starts 48 bytes in, after 3x16-byte tables */
     stream_ptr = 48;
 
1ec94b0f
     if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
0859eaa0
         return ret;
f70f7c6d
 
759001c5
     y_plane = frame->data[0];
     u_plane = frame->data[1];
     v_plane = frame->data[2];
f70f7c6d
 
01aa664f
     if (buf_size == rawsize) {
         int linesize = FFALIGN(s->width,2) * 2;
80e9e63c
         y_plane += frame->linesize[0] * s->height;
01aa664f
         for (stream_ptr = 0; stream_ptr < rawsize; stream_ptr += linesize) {
80e9e63c
             y_plane -= frame->linesize[0];
01aa664f
             memcpy(y_plane, buf+stream_ptr, linesize);
         }
     } else {
 
f70f7c6d
     /* iterate through each line in the height */
     for (y_ptr = 0, u_ptr = 0, v_ptr = 0;
759001c5
          y_ptr < (s->height * frame->linesize[0]);
          y_ptr += frame->linesize[0] - s->width,
          u_ptr += frame->linesize[1] - s->width / 4,
          v_ptr += frame->linesize[2] - s->width / 4) {
f70f7c6d
 
         /* reset predictors */
         cur_byte = buf[stream_ptr++];
         u_plane[u_ptr++] = u_pred = cur_byte & 0xF0;
         y_plane[y_ptr++] = y_pred = (cur_byte & 0x0F) << 4;
 
         cur_byte = buf[stream_ptr++];
         v_plane[v_ptr++] = v_pred = cur_byte & 0xF0;
         y_pred += y_table[cur_byte & 0x0F];
         y_plane[y_ptr++] = y_pred;
 
         cur_byte = buf[stream_ptr++];
         y_pred += y_table[cur_byte & 0x0F];
         y_plane[y_ptr++] = y_pred;
         y_pred += y_table[(cur_byte & 0xF0) >> 4];
         y_plane[y_ptr++] = y_pred;
 
         /* iterate through the remaining pixel groups (4 pixels/group) */
         pixel_groups = s->width / 4 - 1;
         while (pixel_groups--) {
 
             cur_byte = buf[stream_ptr++];
             u_pred += u_table[(cur_byte & 0xF0) >> 4];
             u_plane[u_ptr++] = u_pred;
             y_pred += y_table[cur_byte & 0x0F];
             y_plane[y_ptr++] = y_pred;
 
             cur_byte = buf[stream_ptr++];
             v_pred += v_table[(cur_byte & 0xF0) >> 4];
             v_plane[v_ptr++] = v_pred;
             y_pred += y_table[cur_byte & 0x0F];
             y_plane[y_ptr++] = y_pred;
 
             cur_byte = buf[stream_ptr++];
             y_pred += y_table[cur_byte & 0x0F];
             y_plane[y_ptr++] = y_pred;
             y_pred += y_table[(cur_byte & 0xF0) >> 4];
             y_plane[y_ptr++] = y_pred;
 
         }
     }
01aa664f
     }
f70f7c6d
 
df9b9567
     *got_frame = 1;
f70f7c6d
 
     return buf_size;
 }
 
588f8cd8
 #if CONFIG_AURA_DECODER
e7e2df27
 AVCodec ff_aura_decoder = {
ec6402b7
     .name           = "aura",
b2bed932
     .long_name      = NULL_IF_CONFIG_SMALL("Auravision AURA"),
ec6402b7
     .type           = AVMEDIA_TYPE_VIDEO,
36ef5369
     .id             = AV_CODEC_ID_AURA,
ec6402b7
     .priv_data_size = sizeof(CyuvDecodeContext),
     .init           = cyuv_decode_init,
     .decode         = cyuv_decode_frame,
def97856
     .capabilities   = AV_CODEC_CAP_DR1,
588f8cd8
 };
 #endif
 
 #if CONFIG_CYUV_DECODER
e7e2df27
 AVCodec ff_cyuv_decoder = {
ec6402b7
     .name           = "cyuv",
b2bed932
     .long_name      = NULL_IF_CONFIG_SMALL("Creative YUV (CYUV)"),
ec6402b7
     .type           = AVMEDIA_TYPE_VIDEO,
36ef5369
     .id             = AV_CODEC_ID_CYUV,
ec6402b7
     .priv_data_size = sizeof(CyuvDecodeContext),
     .init           = cyuv_decode_init,
     .decode         = cyuv_decode_frame,
def97856
     .capabilities   = AV_CODEC_CAP_DR1,
f70f7c6d
 };
588f8cd8
 #endif