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

libavcodec/wnv1.c

Go to the documentation of this file.
00001 /*
00002  * Winnov WNV1 codec
00003  * Copyright (c) 2005 Konstantin Shishkov
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00027 #include "avcodec.h"
00028 #include "get_bits.h"
00029 #include "libavutil/common.h"
00030 
00031 
00032 typedef struct WNV1Context{
00033     AVCodecContext *avctx;
00034     AVFrame pic;
00035 
00036     int shift;
00037     GetBitContext gb;
00038 } WNV1Context;
00039 
00040 static const uint16_t code_tab[16][2]={
00041 {0x1FD,9}, {0xFD,8}, {0x7D,7}, {0x3D,6}, {0x1D,5}, {0x0D,4}, {0x005,3},
00042 {0x000,1},
00043 {0x004,3}, {0x0C,4}, {0x1C,5}, {0x3C,6}, {0x7C,7}, {0xFC,8}, {0x1FC,9}, {0xFF,8}
00044 };
00045 
00046 #define CODE_VLC_BITS 9
00047 static VLC code_vlc;
00048 
00049 /* returns modified base_value */
00050 static inline int wnv1_get_code(WNV1Context *w, int base_value)
00051 {
00052     int v = get_vlc2(&w->gb, code_vlc.table, CODE_VLC_BITS, 1);
00053 
00054     if(v==15)
00055         return av_reverse[ get_bits(&w->gb, 8 - w->shift) ];
00056     else
00057         return base_value + ((v - 7)<<w->shift);
00058 }
00059 
00060 static int decode_frame(AVCodecContext *avctx,
00061                         void *data, int *data_size,
00062                         AVPacket *avpkt)
00063 {
00064     const uint8_t *buf = avpkt->data;
00065     int buf_size = avpkt->size;
00066     WNV1Context * const l = avctx->priv_data;
00067     AVFrame * const p= (AVFrame*)&l->pic;
00068     unsigned char *Y,*U,*V;
00069     int i, j;
00070     int prev_y = 0, prev_u = 0, prev_v = 0;
00071     uint8_t *rbuf;
00072 
00073     rbuf = av_malloc(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
00074     if(!rbuf){
00075         av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer\n");
00076         return -1;
00077     }
00078 
00079     if(p->data[0])
00080         avctx->release_buffer(avctx, p);
00081 
00082     p->reference = 0;
00083     if(avctx->get_buffer(avctx, p) < 0){
00084         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00085         av_free(rbuf);
00086         return -1;
00087     }
00088     p->key_frame = 1;
00089 
00090     for(i=8; i<buf_size; i++)
00091         rbuf[i]= av_reverse[ buf[i] ];
00092     init_get_bits(&l->gb, rbuf+8, (buf_size-8)*8);
00093 
00094     if (buf[2] >> 4 == 6)
00095         l->shift = 2;
00096     else {
00097         l->shift = 8 - (buf[2] >> 4);
00098         if (l->shift > 4) {
00099             av_log(avctx, AV_LOG_ERROR, "Unknown WNV1 frame header value %i, please upload file for study\n", buf[2] >> 4);
00100             l->shift = 4;
00101         }
00102         if (l->shift < 1) {
00103             av_log(avctx, AV_LOG_ERROR, "Unknown WNV1 frame header value %i, please upload file for study\n", buf[2] >> 4);
00104             l->shift = 1;
00105         }
00106     }
00107 
00108     Y = p->data[0];
00109     U = p->data[1];
00110     V = p->data[2];
00111     for (j = 0; j < avctx->height; j++) {
00112         for (i = 0; i < avctx->width / 2; i++) {
00113             Y[i * 2] = wnv1_get_code(l, prev_y);
00114             prev_u = U[i] = wnv1_get_code(l, prev_u);
00115             prev_y = Y[(i * 2) + 1] = wnv1_get_code(l, Y[i * 2]);
00116             prev_v = V[i] = wnv1_get_code(l, prev_v);
00117         }
00118         Y += p->linesize[0];
00119         U += p->linesize[1];
00120         V += p->linesize[2];
00121     }
00122 
00123 
00124     *data_size = sizeof(AVFrame);
00125     *(AVFrame*)data = l->pic;
00126     av_free(rbuf);
00127 
00128     return buf_size;
00129 }
00130 
00131 static av_cold int decode_init(AVCodecContext *avctx){
00132     WNV1Context * const l = avctx->priv_data;
00133     static VLC_TYPE code_table[1 << CODE_VLC_BITS][2];
00134 
00135     l->avctx = avctx;
00136     avctx->pix_fmt = PIX_FMT_YUV422P;
00137 
00138     code_vlc.table = code_table;
00139     code_vlc.table_allocated = 1 << CODE_VLC_BITS;
00140     init_vlc(&code_vlc, CODE_VLC_BITS, 16,
00141              &code_tab[0][1], 4, 2,
00142              &code_tab[0][0], 4, 2, INIT_VLC_USE_NEW_STATIC);
00143 
00144     return 0;
00145 }
00146 
00147 static av_cold int decode_end(AVCodecContext *avctx){
00148     WNV1Context * const l = avctx->priv_data;
00149     AVFrame *pic = &l->pic;
00150 
00151     if (pic->data[0])
00152         avctx->release_buffer(avctx, pic);
00153 
00154     return 0;
00155 }
00156 
00157 AVCodec wnv1_decoder = {
00158     "wnv1",
00159     AVMEDIA_TYPE_VIDEO,
00160     CODEC_ID_WNV1,
00161     sizeof(WNV1Context),
00162     decode_init,
00163     NULL,
00164     decode_end,
00165     decode_frame,
00166     CODEC_CAP_DR1,
00167     .long_name = NULL_IF_CONFIG_SMALL("Winnov WNV1"),
00168 };

Generated on Fri Sep 16 2011 17:17:46 for FFmpeg by  doxygen 1.7.1