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 "internal.h"
00030 #include "mathops.h"
00031
00032
00033 typedef struct WNV1Context{
00034 AVCodecContext *avctx;
00035 AVFrame pic;
00036
00037 int shift;
00038 GetBitContext gb;
00039 } WNV1Context;
00040
00041 static const uint16_t code_tab[16][2]={
00042 {0x1FD,9}, {0xFD,8}, {0x7D,7}, {0x3D,6}, {0x1D,5}, {0x0D,4}, {0x005,3},
00043 {0x000,1},
00044 {0x004,3}, {0x0C,4}, {0x1C,5}, {0x3C,6}, {0x7C,7}, {0xFC,8}, {0x1FC,9}, {0xFF,8}
00045 };
00046
00047 #define CODE_VLC_BITS 9
00048 static VLC code_vlc;
00049
00050
00051 static inline int wnv1_get_code(WNV1Context *w, int base_value)
00052 {
00053 int v = get_vlc2(&w->gb, code_vlc.table, CODE_VLC_BITS, 1);
00054
00055 if(v==15)
00056 return ff_reverse[ get_bits(&w->gb, 8 - w->shift) ];
00057 else
00058 return base_value + ((v - 7)<<w->shift);
00059 }
00060
00061 static int decode_frame(AVCodecContext *avctx,
00062 void *data, int *got_frame,
00063 AVPacket *avpkt)
00064 {
00065 const uint8_t *buf = avpkt->data;
00066 int buf_size = avpkt->size;
00067 WNV1Context * const l = avctx->priv_data;
00068 AVFrame * const p = &l->pic;
00069 unsigned char *Y,*U,*V;
00070 int i, j, ret;
00071 int prev_y = 0, prev_u = 0, prev_v = 0;
00072 uint8_t *rbuf;
00073
00074 if(buf_size<=8) {
00075 av_log(avctx, AV_LOG_ERROR, "buf_size %d is too small\n", buf_size);
00076 return AVERROR_INVALIDDATA;
00077 }
00078
00079 rbuf = av_malloc(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
00080 if(!rbuf){
00081 av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer\n");
00082 return AVERROR(ENOMEM);
00083 }
00084
00085 if(p->data[0])
00086 avctx->release_buffer(avctx, p);
00087
00088 p->reference = 0;
00089 if ((ret = ff_get_buffer(avctx, p)) < 0) {
00090 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00091 av_free(rbuf);
00092 return ret;
00093 }
00094 p->key_frame = 1;
00095
00096 for(i=8; i<buf_size; i++)
00097 rbuf[i]= ff_reverse[ buf[i] ];
00098 init_get_bits(&l->gb, rbuf+8, (buf_size-8)*8);
00099
00100 if (buf[2] >> 4 == 6)
00101 l->shift = 2;
00102 else {
00103 l->shift = 8 - (buf[2] >> 4);
00104 if (l->shift > 4) {
00105 av_log_ask_for_sample(avctx, "Unknown WNV1 frame header value %i\n",
00106 buf[2] >> 4);
00107 l->shift = 4;
00108 }
00109 if (l->shift < 1) {
00110 av_log_ask_for_sample(avctx, "Unknown WNV1 frame header value %i\n",
00111 buf[2] >> 4);
00112 l->shift = 1;
00113 }
00114 }
00115
00116 Y = p->data[0];
00117 U = p->data[1];
00118 V = p->data[2];
00119 for (j = 0; j < avctx->height; j++) {
00120 for (i = 0; i < avctx->width / 2; i++) {
00121 Y[i * 2] = wnv1_get_code(l, prev_y);
00122 prev_u = U[i] = wnv1_get_code(l, prev_u);
00123 prev_y = Y[(i * 2) + 1] = wnv1_get_code(l, Y[i * 2]);
00124 prev_v = V[i] = wnv1_get_code(l, prev_v);
00125 }
00126 Y += p->linesize[0];
00127 U += p->linesize[1];
00128 V += p->linesize[2];
00129 }
00130
00131
00132 *got_frame = 1;
00133 *(AVFrame*)data = l->pic;
00134 av_free(rbuf);
00135
00136 return buf_size;
00137 }
00138
00139 static av_cold int decode_init(AVCodecContext *avctx){
00140 WNV1Context * const l = avctx->priv_data;
00141 static VLC_TYPE code_table[1 << CODE_VLC_BITS][2];
00142
00143 l->avctx = avctx;
00144 avctx->pix_fmt = AV_PIX_FMT_YUV422P;
00145 avcodec_get_frame_defaults(&l->pic);
00146
00147 code_vlc.table = code_table;
00148 code_vlc.table_allocated = 1 << CODE_VLC_BITS;
00149 init_vlc(&code_vlc, CODE_VLC_BITS, 16,
00150 &code_tab[0][1], 4, 2,
00151 &code_tab[0][0], 4, 2, INIT_VLC_USE_NEW_STATIC);
00152
00153 return 0;
00154 }
00155
00156 static av_cold int decode_end(AVCodecContext *avctx){
00157 WNV1Context * const l = avctx->priv_data;
00158 AVFrame *pic = &l->pic;
00159
00160 if (pic->data[0])
00161 avctx->release_buffer(avctx, pic);
00162
00163 return 0;
00164 }
00165
00166 AVCodec ff_wnv1_decoder = {
00167 .name = "wnv1",
00168 .type = AVMEDIA_TYPE_VIDEO,
00169 .id = AV_CODEC_ID_WNV1,
00170 .priv_data_size = sizeof(WNV1Context),
00171 .init = decode_init,
00172 .close = decode_end,
00173 .decode = decode_frame,
00174 .capabilities = CODEC_CAP_DR1,
00175 .long_name = NULL_IF_CONFIG_SMALL("Winnov WNV1"),
00176 };