• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavcodec/cyuv.c

Go to the documentation of this file.
00001 /*
00002  * Creative YUV (CYUV) Video Decoder
00003  *   by Mike Melanson (melanson@pcisys.net)
00004  * based on "Creative YUV (CYUV) stream format for AVI":
00005  *   http://www.csse.monash.edu.au/~timf/videocodec/cyuv.txt
00006  *
00007  * Copyright (C) 2003 the ffmpeg project
00008  *
00009  * This file is part of FFmpeg.
00010  *
00011  * FFmpeg is free software; you can redistribute it and/or
00012  * modify it under the terms of the GNU Lesser General Public
00013  * License as published by the Free Software Foundation; either
00014  * version 2.1 of the License, or (at your option) any later version.
00015  *
00016  * FFmpeg is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019  * Lesser General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU Lesser General Public
00022  * License along with FFmpeg; if not, write to the Free Software
00023  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00024  */
00025 
00031 #include <stdio.h>
00032 #include <stdlib.h>
00033 #include <string.h>
00034 #include <unistd.h>
00035 
00036 #include "avcodec.h"
00037 #include "dsputil.h"
00038 
00039 
00040 typedef struct CyuvDecodeContext {
00041     AVCodecContext *avctx;
00042     int width, height;
00043     AVFrame frame;
00044 } CyuvDecodeContext;
00045 
00046 static av_cold int cyuv_decode_init(AVCodecContext *avctx)
00047 {
00048     CyuvDecodeContext *s = avctx->priv_data;
00049 
00050     s->avctx = avctx;
00051     s->width = avctx->width;
00052     /* width needs to be divisible by 4 for this codec to work */
00053     if (s->width & 0x3)
00054         return -1;
00055     s->height = avctx->height;
00056     avctx->pix_fmt = PIX_FMT_YUV411P;
00057 
00058     return 0;
00059 }
00060 
00061 static int cyuv_decode_frame(AVCodecContext *avctx,
00062                              void *data, int *data_size,
00063                              const uint8_t *buf, int buf_size)
00064 {
00065     CyuvDecodeContext *s=avctx->priv_data;
00066 
00067     unsigned char *y_plane;
00068     unsigned char *u_plane;
00069     unsigned char *v_plane;
00070     int y_ptr;
00071     int u_ptr;
00072     int v_ptr;
00073 
00074     /* prediction error tables (make it clear that they are signed values) */
00075     const signed char *y_table = (const signed char*)buf +  0;
00076     const signed char *u_table = (const signed char*)buf + 16;
00077     const signed char *v_table = (const signed char*)buf + 32;
00078 
00079     unsigned char y_pred, u_pred, v_pred;
00080     int stream_ptr;
00081     unsigned char cur_byte;
00082     int pixel_groups;
00083 
00084     /* sanity check the buffer size: A buffer has 3x16-bytes tables
00085      * followed by (height) lines each with 3 bytes to represent groups
00086      * of 4 pixels. Thus, the total size of the buffer ought to be:
00087      *    (3 * 16) + height * (width * 3 / 4) */
00088     if (buf_size != 48 + s->height * (s->width * 3 / 4)) {
00089       av_log(avctx, AV_LOG_ERROR, "ffmpeg: cyuv: got a buffer with %d bytes when %d were expected\n",
00090         buf_size,
00091         48 + s->height * (s->width * 3 / 4));
00092       return -1;
00093     }
00094 
00095     /* pixel data starts 48 bytes in, after 3x16-byte tables */
00096     stream_ptr = 48;
00097 
00098     if(s->frame.data[0])
00099         avctx->release_buffer(avctx, &s->frame);
00100 
00101     s->frame.buffer_hints = FF_BUFFER_HINTS_VALID;
00102     s->frame.reference = 0;
00103     if(avctx->get_buffer(avctx, &s->frame) < 0) {
00104         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00105         return -1;
00106     }
00107 
00108     y_plane = s->frame.data[0];
00109     u_plane = s->frame.data[1];
00110     v_plane = s->frame.data[2];
00111 
00112     /* iterate through each line in the height */
00113     for (y_ptr = 0, u_ptr = 0, v_ptr = 0;
00114          y_ptr < (s->height * s->frame.linesize[0]);
00115          y_ptr += s->frame.linesize[0] - s->width,
00116          u_ptr += s->frame.linesize[1] - s->width / 4,
00117          v_ptr += s->frame.linesize[2] - s->width / 4) {
00118 
00119         /* reset predictors */
00120         cur_byte = buf[stream_ptr++];
00121         u_plane[u_ptr++] = u_pred = cur_byte & 0xF0;
00122         y_plane[y_ptr++] = y_pred = (cur_byte & 0x0F) << 4;
00123 
00124         cur_byte = buf[stream_ptr++];
00125         v_plane[v_ptr++] = v_pred = cur_byte & 0xF0;
00126         y_pred += y_table[cur_byte & 0x0F];
00127         y_plane[y_ptr++] = y_pred;
00128 
00129         cur_byte = buf[stream_ptr++];
00130         y_pred += y_table[cur_byte & 0x0F];
00131         y_plane[y_ptr++] = y_pred;
00132         y_pred += y_table[(cur_byte & 0xF0) >> 4];
00133         y_plane[y_ptr++] = y_pred;
00134 
00135         /* iterate through the remaining pixel groups (4 pixels/group) */
00136         pixel_groups = s->width / 4 - 1;
00137         while (pixel_groups--) {
00138 
00139             cur_byte = buf[stream_ptr++];
00140             u_pred += u_table[(cur_byte & 0xF0) >> 4];
00141             u_plane[u_ptr++] = u_pred;
00142             y_pred += y_table[cur_byte & 0x0F];
00143             y_plane[y_ptr++] = y_pred;
00144 
00145             cur_byte = buf[stream_ptr++];
00146             v_pred += v_table[(cur_byte & 0xF0) >> 4];
00147             v_plane[v_ptr++] = v_pred;
00148             y_pred += y_table[cur_byte & 0x0F];
00149             y_plane[y_ptr++] = y_pred;
00150 
00151             cur_byte = buf[stream_ptr++];
00152             y_pred += y_table[cur_byte & 0x0F];
00153             y_plane[y_ptr++] = y_pred;
00154             y_pred += y_table[(cur_byte & 0xF0) >> 4];
00155             y_plane[y_ptr++] = y_pred;
00156 
00157         }
00158     }
00159 
00160     *data_size=sizeof(AVFrame);
00161     *(AVFrame*)data= s->frame;
00162 
00163     return buf_size;
00164 }
00165 
00166 AVCodec cyuv_decoder = {
00167     "cyuv",
00168     CODEC_TYPE_VIDEO,
00169     CODEC_ID_CYUV,
00170     sizeof(CyuvDecodeContext),
00171     cyuv_decode_init,
00172     NULL,
00173     NULL,
00174     cyuv_decode_frame,
00175     CODEC_CAP_DR1,
00176     NULL,
00177     .long_name = NULL_IF_CONFIG_SMALL("Creative YUV (CYUV)"),
00178 };
00179 

Generated on Sat Feb 16 2013 09:23:11 for ffmpeg by  doxygen 1.7.1