• 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 "bitstream.h"
00029 
00030 
00031 typedef struct WNV1Context{
00032     AVCodecContext *avctx;
00033     AVFrame pic;
00034 
00035     int shift;
00036     GetBitContext gb;
00037 } WNV1Context;
00038 
00039 static const uint16_t code_tab[16][2]={
00040 {0x1FD,9}, {0xFD,8}, {0x7D,7}, {0x3D,6}, {0x1D,5}, {0x0D,4}, {0x005,3},
00041 {0x000,1},
00042 {0x004,3}, {0x0C,4}, {0x1C,5}, {0x3C,6}, {0x7C,7}, {0xFC,8}, {0x1FC,9}, {0xFF,8}
00043 };
00044 
00045 #define CODE_VLC_BITS 9
00046 static VLC code_vlc;
00047 
00048 /* returns modified base_value */
00049 static inline int wnv1_get_code(WNV1Context *w, int base_value)
00050 {
00051     int v = get_vlc2(&w->gb, code_vlc.table, CODE_VLC_BITS, 1);
00052 
00053     if(v==15)
00054         return ff_reverse[ get_bits(&w->gb, 8 - w->shift) ];
00055     else
00056         return base_value + ((v - 7)<<w->shift);
00057 }
00058 
00059 static int decode_frame(AVCodecContext *avctx,
00060                         void *data, int *data_size,
00061                         const uint8_t *buf, int buf_size)
00062 {
00063     WNV1Context * const l = avctx->priv_data;
00064     AVFrame * const p= (AVFrame*)&l->pic;
00065     unsigned char *Y,*U,*V;
00066     int i, j;
00067     int prev_y = 0, prev_u = 0, prev_v = 0;
00068     uint8_t *rbuf;
00069 
00070     rbuf = av_malloc(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
00071     if(!rbuf){
00072         av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer\n");
00073         return -1;
00074     }
00075 
00076     if(p->data[0])
00077         avctx->release_buffer(avctx, p);
00078 
00079     p->reference = 0;
00080     if(avctx->get_buffer(avctx, p) < 0){
00081         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00082         av_free(rbuf);
00083         return -1;
00084     }
00085     p->key_frame = 1;
00086 
00087     for(i=8; i<buf_size; i++)
00088         rbuf[i]= ff_reverse[ buf[i] ];
00089     init_get_bits(&l->gb, rbuf+8, (buf_size-8)*8);
00090 
00091     if (buf[2] >> 4 == 6)
00092         l->shift = 2;
00093     else {
00094         l->shift = 8 - (buf[2] >> 4);
00095         if (l->shift > 4) {
00096             av_log(avctx, AV_LOG_ERROR, "Unknown WNV1 frame header value %i, please upload file for study\n", buf[2] >> 4);
00097             l->shift = 4;
00098         }
00099         if (l->shift < 1) {
00100             av_log(avctx, AV_LOG_ERROR, "Unknown WNV1 frame header value %i, please upload file for study\n", buf[2] >> 4);
00101             l->shift = 1;
00102         }
00103     }
00104 
00105     Y = p->data[0];
00106     U = p->data[1];
00107     V = p->data[2];
00108     for (j = 0; j < avctx->height; j++) {
00109         for (i = 0; i < avctx->width / 2; i++) {
00110             Y[i * 2] = wnv1_get_code(l, prev_y);
00111             prev_u = U[i] = wnv1_get_code(l, prev_u);
00112             prev_y = Y[(i * 2) + 1] = wnv1_get_code(l, Y[i * 2]);
00113             prev_v = V[i] = wnv1_get_code(l, prev_v);
00114         }
00115         Y += p->linesize[0];
00116         U += p->linesize[1];
00117         V += p->linesize[2];
00118     }
00119 
00120 
00121     *data_size = sizeof(AVFrame);
00122     *(AVFrame*)data = l->pic;
00123     av_free(rbuf);
00124 
00125     return buf_size;
00126 }
00127 
00128 static av_cold int decode_init(AVCodecContext *avctx){
00129     WNV1Context * const l = avctx->priv_data;
00130 
00131     l->avctx = avctx;
00132     avctx->pix_fmt = PIX_FMT_YUV422P;
00133 
00134     if(!code_vlc.table){
00135         init_vlc(&code_vlc, CODE_VLC_BITS, 16,
00136                     &code_tab[0][1], 4, 2,
00137                     &code_tab[0][0], 4, 2, 1);
00138     }
00139 
00140     return 0;
00141 }
00142 
00143 AVCodec wnv1_decoder = {
00144     "wnv1",
00145     CODEC_TYPE_VIDEO,
00146     CODEC_ID_WNV1,
00147     sizeof(WNV1Context),
00148     decode_init,
00149     NULL,
00150     NULL,
00151     decode_frame,
00152     CODEC_CAP_DR1,
00153     .long_name = NULL_IF_CONFIG_SMALL("Winnov WNV1"),
00154 };

Generated on Tue Nov 4 2014 12:59:23 for ffmpeg by  doxygen 1.7.1