00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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 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
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 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
00069 if(p->data[0])
00070 avctx->release_buffer(avctx, p);
00071
00072 p->reference = 0;
00073 if(avctx->get_buffer(avctx, p) < 0){
00074 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00075 return -1;
00076 }
00077 p->key_frame = 1;
00078
00079 for(i=8; i<buf_size; i++)
00080 buf[i]= ff_reverse[ buf[i] ];
00081 init_get_bits(&l->gb, buf+8, (buf_size-8)*8);
00082
00083 if (buf[2] >> 4 == 6)
00084 l->shift = 2;
00085 else {
00086 l->shift = 8 - (buf[2] >> 4);
00087 if (l->shift > 4) {
00088 av_log(avctx, AV_LOG_ERROR, "Unknown WNV1 frame header value %i, please upload file for study\n", buf[2] >> 4);
00089 l->shift = 4;
00090 }
00091 if (l->shift < 1) {
00092 av_log(avctx, AV_LOG_ERROR, "Unknown WNV1 frame header value %i, please upload file for study\n", buf[2] >> 4);
00093 l->shift = 1;
00094 }
00095 }
00096
00097 Y = p->data[0];
00098 U = p->data[1];
00099 V = p->data[2];
00100 for (j = 0; j < avctx->height; j++) {
00101 for (i = 0; i < avctx->width / 2; i++) {
00102 Y[i * 2] = wnv1_get_code(l, prev_y);
00103 prev_u = U[i] = wnv1_get_code(l, prev_u);
00104 prev_y = Y[(i * 2) + 1] = wnv1_get_code(l, Y[i * 2]);
00105 prev_v = V[i] = wnv1_get_code(l, prev_v);
00106 }
00107 Y += p->linesize[0];
00108 U += p->linesize[1];
00109 V += p->linesize[2];
00110 }
00111
00112
00113 *data_size = sizeof(AVFrame);
00114 *(AVFrame*)data = l->pic;
00115
00116 return buf_size;
00117 }
00118
00119 static int decode_init(AVCodecContext *avctx){
00120 WNV1Context * const l = avctx->priv_data;
00121
00122 l->avctx = avctx;
00123 avctx->pix_fmt = PIX_FMT_YUV422P;
00124
00125 if(!code_vlc.table){
00126 init_vlc(&code_vlc, CODE_VLC_BITS, 16,
00127 &code_tab[0][1], 4, 2,
00128 &code_tab[0][0], 4, 2, 1);
00129 }
00130
00131 return 0;
00132 }
00133
00134 AVCodec wnv1_decoder = {
00135 "wnv1",
00136 CODEC_TYPE_VIDEO,
00137 CODEC_ID_WNV1,
00138 sizeof(WNV1Context),
00139 decode_init,
00140 NULL,
00141 NULL,
00142 decode_frame,
00143 CODEC_CAP_DR1,
00144 };