annotate wnv1.c @ 4356:c8bc01fee409 libavcodec

set data_size to 0 so that in case we return without setting it nothing funny can happen
author michael
date Tue, 16 Jan 2007 23:28:41 +0000
parents c8c591fe26f8
children f99e40a7155b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2577
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
1 /*
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
2 * Winnov WNV1 codec
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
3 * Copyright (c) 2005 Konstantin Shishkov
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
4 *
3947
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3036
diff changeset
5 * This file is part of FFmpeg.
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3036
diff changeset
6 *
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3036
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
2577
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
9 * License as published by the Free Software Foundation; either
3947
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3036
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
2577
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
11 *
3947
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3036
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
2577
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
15 * Lesser General Public License for more details.
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
16 *
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
3947
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3036
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
3036
0b546eab515d Update licensing information: The FSF changed postal address.
diego
parents: 2967
diff changeset
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2577
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
20 *
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
21 */
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2579
diff changeset
22
2577
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
23 /**
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
24 * @file wnv1.c
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
25 * Winnov WNV1 codec.
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
26 */
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2579
diff changeset
27
2577
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
28 #include "avcodec.h"
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
29 #include "common.h"
2579
e027765cf410 replace custom bitstream reader with get_bits/vlc()
michael
parents: 2577
diff changeset
30 #include "bitstream.h"
2577
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
31
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
32
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
33 typedef struct WNV1Context{
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
34 AVCodecContext *avctx;
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
35 AVFrame pic;
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
36
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
37 int shift;
2579
e027765cf410 replace custom bitstream reader with get_bits/vlc()
michael
parents: 2577
diff changeset
38 GetBitContext gb;
2577
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
39 } WNV1Context;
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
40
2579
e027765cf410 replace custom bitstream reader with get_bits/vlc()
michael
parents: 2577
diff changeset
41 static uint16_t code_tab[16][2]={
e027765cf410 replace custom bitstream reader with get_bits/vlc()
michael
parents: 2577
diff changeset
42 {0x1FD,9}, {0xFD,8}, {0x7D,7}, {0x3D,6}, {0x1D,5}, {0x0D,4}, {0x005,3},
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2579
diff changeset
43 {0x000,1},
2579
e027765cf410 replace custom bitstream reader with get_bits/vlc()
michael
parents: 2577
diff changeset
44 {0x004,3}, {0x0C,4}, {0x1C,5}, {0x3C,6}, {0x7C,7}, {0xFC,8}, {0x1FC,9}, {0xFF,8}
e027765cf410 replace custom bitstream reader with get_bits/vlc()
michael
parents: 2577
diff changeset
45 };
e027765cf410 replace custom bitstream reader with get_bits/vlc()
michael
parents: 2577
diff changeset
46
e027765cf410 replace custom bitstream reader with get_bits/vlc()
michael
parents: 2577
diff changeset
47 #define CODE_VLC_BITS 9
e027765cf410 replace custom bitstream reader with get_bits/vlc()
michael
parents: 2577
diff changeset
48 static VLC code_vlc;
e027765cf410 replace custom bitstream reader with get_bits/vlc()
michael
parents: 2577
diff changeset
49
2577
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
50 /* returns modified base_value */
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
51 static inline int wnv1_get_code(WNV1Context *w, int base_value)
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
52 {
2579
e027765cf410 replace custom bitstream reader with get_bits/vlc()
michael
parents: 2577
diff changeset
53 int v = get_vlc2(&w->gb, code_vlc.table, CODE_VLC_BITS, 1);
2577
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
54
2579
e027765cf410 replace custom bitstream reader with get_bits/vlc()
michael
parents: 2577
diff changeset
55 if(v==15)
e027765cf410 replace custom bitstream reader with get_bits/vlc()
michael
parents: 2577
diff changeset
56 return ff_reverse[ get_bits(&w->gb, 8 - w->shift) ];
e027765cf410 replace custom bitstream reader with get_bits/vlc()
michael
parents: 2577
diff changeset
57 else
e027765cf410 replace custom bitstream reader with get_bits/vlc()
michael
parents: 2577
diff changeset
58 return base_value + ((v - 7)<<w->shift);
2577
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
59 }
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
60
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2579
diff changeset
61 static int decode_frame(AVCodecContext *avctx,
2577
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
62 void *data, int *data_size,
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
63 uint8_t *buf, int buf_size)
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
64 {
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
65 WNV1Context * const l = avctx->priv_data;
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
66 AVFrame * const p= (AVFrame*)&l->pic;
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
67 unsigned char *Y,*U,*V;
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
68 int i, j;
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
69 int prev_y = 0, prev_u = 0, prev_v = 0;
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
70
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
71 if(p->data[0])
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
72 avctx->release_buffer(avctx, p);
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
73
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
74 p->reference = 0;
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
75 if(avctx->get_buffer(avctx, p) < 0){
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
76 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
77 return -1;
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
78 }
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
79 p->key_frame = 1;
2579
e027765cf410 replace custom bitstream reader with get_bits/vlc()
michael
parents: 2577
diff changeset
80
e027765cf410 replace custom bitstream reader with get_bits/vlc()
michael
parents: 2577
diff changeset
81 for(i=8; i<buf_size; i++)
e027765cf410 replace custom bitstream reader with get_bits/vlc()
michael
parents: 2577
diff changeset
82 buf[i]= ff_reverse[ buf[i] ]; //FIXME ensure that the buffer is modifyable or use a temp one
e027765cf410 replace custom bitstream reader with get_bits/vlc()
michael
parents: 2577
diff changeset
83 init_get_bits(&l->gb, buf+8, (buf_size-8)*8);
e027765cf410 replace custom bitstream reader with get_bits/vlc()
michael
parents: 2577
diff changeset
84
2577
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
85 if (buf[2] >> 4 == 6)
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
86 l->shift = 2;
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
87 else {
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
88 l->shift = 8 - (buf[2] >> 4);
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
89 if (l->shift > 4) {
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
90 av_log(avctx, AV_LOG_ERROR, "Unknown WNV1 frame header value %i, please upload file for study\n", buf[2] >> 4);
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
91 l->shift = 4;
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
92 }
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
93 if (l->shift < 1) {
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
94 av_log(avctx, AV_LOG_ERROR, "Unknown WNV1 frame header value %i, please upload file for study\n", buf[2] >> 4);
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
95 l->shift = 1;
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
96 }
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
97 }
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2579
diff changeset
98
2577
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
99 Y = p->data[0];
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
100 U = p->data[1];
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
101 V = p->data[2];
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
102 for (j = 0; j < avctx->height; j++) {
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
103 for (i = 0; i < avctx->width / 2; i++) {
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
104 Y[i * 2] = wnv1_get_code(l, prev_y);
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
105 prev_u = U[i] = wnv1_get_code(l, prev_u);
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
106 prev_y = Y[(i * 2) + 1] = wnv1_get_code(l, Y[i * 2]);
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
107 prev_v = V[i] = wnv1_get_code(l, prev_v);
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
108 }
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
109 Y += p->linesize[0];
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
110 U += p->linesize[1];
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
111 V += p->linesize[2];
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
112 }
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
113
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2579
diff changeset
114
2577
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
115 *data_size = sizeof(AVFrame);
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
116 *(AVFrame*)data = l->pic;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2579
diff changeset
117
2577
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
118 return buf_size;
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
119 }
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
120
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
121 static int decode_init(AVCodecContext *avctx){
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
122 WNV1Context * const l = avctx->priv_data;
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
123
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
124 l->avctx = avctx;
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
125 avctx->pix_fmt = PIX_FMT_YUV422P;
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
126
2579
e027765cf410 replace custom bitstream reader with get_bits/vlc()
michael
parents: 2577
diff changeset
127 if(!code_vlc.table){
e027765cf410 replace custom bitstream reader with get_bits/vlc()
michael
parents: 2577
diff changeset
128 init_vlc(&code_vlc, CODE_VLC_BITS, 16,
e027765cf410 replace custom bitstream reader with get_bits/vlc()
michael
parents: 2577
diff changeset
129 &code_tab[0][1], 4, 2,
e027765cf410 replace custom bitstream reader with get_bits/vlc()
michael
parents: 2577
diff changeset
130 &code_tab[0][0], 4, 2, 1);
e027765cf410 replace custom bitstream reader with get_bits/vlc()
michael
parents: 2577
diff changeset
131 }
e027765cf410 replace custom bitstream reader with get_bits/vlc()
michael
parents: 2577
diff changeset
132
2577
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
133 return 0;
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
134 }
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
135
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
136 AVCodec wnv1_decoder = {
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
137 "wnv1",
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
138 CODEC_TYPE_VIDEO,
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
139 CODEC_ID_WNV1,
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
140 sizeof(WNV1Context),
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
141 decode_init,
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
142 NULL,
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
143 NULL,
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
144 decode_frame,
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
145 CODEC_CAP_DR1,
7d9997a69158 Winnov WNV1 video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
146 };