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 "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
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 = &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 if(buf_size<=8) {
00074 av_log(avctx, AV_LOG_ERROR, "buf_size %d is too small\n", buf_size);
00075 return AVERROR_INVALIDDATA;
00076 }
00077
00078 rbuf = av_malloc(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
00079 if(!rbuf){
00080 av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer\n");
00081 return -1;
00082 }
00083
00084 if(p->data[0])
00085 avctx->release_buffer(avctx, p);
00086
00087 p->reference = 0;
00088 if(avctx->get_buffer(avctx, p) < 0){
00089 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00090 av_free(rbuf);
00091 return -1;
00092 }
00093 p->key_frame = 1;
00094
00095 for(i=8; i<buf_size; i++)
00096 rbuf[i]= av_reverse[ buf[i] ];
00097 init_get_bits(&l->gb, rbuf+8, (buf_size-8)*8);
00098
00099 if (buf[2] >> 4 == 6)
00100 l->shift = 2;
00101 else {
00102 l->shift = 8 - (buf[2] >> 4);
00103 if (l->shift > 4) {
00104 av_log_ask_for_sample(avctx, "Unknown WNV1 frame header value %i\n",
00105 buf[2] >> 4);
00106 l->shift = 4;
00107 }
00108 if (l->shift < 1) {
00109 av_log_ask_for_sample(avctx, "Unknown WNV1 frame header value %i\n",
00110 buf[2] >> 4);
00111 l->shift = 1;
00112 }
00113 }
00114
00115 Y = p->data[0];
00116 U = p->data[1];
00117 V = p->data[2];
00118 for (j = 0; j < avctx->height; j++) {
00119 for (i = 0; i < avctx->width / 2; i++) {
00120 Y[i * 2] = wnv1_get_code(l, prev_y);
00121 prev_u = U[i] = wnv1_get_code(l, prev_u);
00122 prev_y = Y[(i * 2) + 1] = wnv1_get_code(l, Y[i * 2]);
00123 prev_v = V[i] = wnv1_get_code(l, prev_v);
00124 }
00125 Y += p->linesize[0];
00126 U += p->linesize[1];
00127 V += p->linesize[2];
00128 }
00129
00130
00131 *data_size = sizeof(AVFrame);
00132 *(AVFrame*)data = l->pic;
00133 av_free(rbuf);
00134
00135 return buf_size;
00136 }
00137
00138 static av_cold int decode_init(AVCodecContext *avctx){
00139 WNV1Context * const l = avctx->priv_data;
00140 static VLC_TYPE code_table[1 << CODE_VLC_BITS][2];
00141
00142 l->avctx = avctx;
00143 avctx->pix_fmt = PIX_FMT_YUV422P;
00144 avcodec_get_frame_defaults(&l->pic);
00145
00146 code_vlc.table = code_table;
00147 code_vlc.table_allocated = 1 << CODE_VLC_BITS;
00148 init_vlc(&code_vlc, CODE_VLC_BITS, 16,
00149 &code_tab[0][1], 4, 2,
00150 &code_tab[0][0], 4, 2, INIT_VLC_USE_NEW_STATIC);
00151
00152 return 0;
00153 }
00154
00155 static av_cold int decode_end(AVCodecContext *avctx){
00156 WNV1Context * const l = avctx->priv_data;
00157 AVFrame *pic = &l->pic;
00158
00159 if (pic->data[0])
00160 avctx->release_buffer(avctx, pic);
00161
00162 return 0;
00163 }
00164
00165 AVCodec ff_wnv1_decoder = {
00166 .name = "wnv1",
00167 .type = AVMEDIA_TYPE_VIDEO,
00168 .id = CODEC_ID_WNV1,
00169 .priv_data_size = sizeof(WNV1Context),
00170 .init = decode_init,
00171 .close = decode_end,
00172 .decode = decode_frame,
00173 .capabilities = CODEC_CAP_DR1,
00174 .long_name = NULL_IF_CONFIG_SMALL("Winnov WNV1"),
00175 };