vlc: Add header #include when the types are used
[libav.git] / libavcodec / wnv1.c
CommitLineData
a8a15e9d
MM
1/*
2 * Winnov WNV1 codec
3 * Copyright (c) 2005 Konstantin Shishkov
4 *
2912e87a 5 * This file is part of Libav.
b78e7197 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
a8a15e9d
MM
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
b78e7197 10 * version 2.1 of the License, or (at your option) any later version.
a8a15e9d 11 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
a8a15e9d
MM
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
2912e87a 18 * License along with Libav; if not, write to the Free Software
5509bffa 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
a8a15e9d 20 */
115329f1 21
a8a15e9d 22/**
ba87f080 23 * @file
a8a15e9d
MM
24 * Winnov WNV1 codec.
25 */
115329f1 26
a8a15e9d 27#include "avcodec.h"
f9c59f26 28#include "bitstream.h"
594d4d5d 29#include "internal.h"
d5c62122 30#include "mathops.h"
73fc82f3 31#include "vlc.h"
a8a15e9d
MM
32
33
0a9132b8 34typedef struct WNV1Context {
a8a15e9d 35 AVCodecContext *avctx;
a8a15e9d
MM
36
37 int shift;
f9c59f26 38 BitstreamContext bc;
a8a15e9d
MM
39} WNV1Context;
40
0a9132b8
AK
41static const uint16_t code_tab[16][2] = {
42 { 0x1FD, 9 }, { 0xFD, 8 }, { 0x7D, 7 }, { 0x3D, 6 }, { 0x1D, 5 }, { 0x0D, 4 }, { 0x005, 3 },
43 { 0x000, 1 },
44 { 0x004, 3 }, { 0x0C, 4 }, { 0x1C, 5 }, { 0x3C, 6 }, { 0x7C, 7 }, { 0xFC, 8 }, { 0x1FC, 9 }, { 0xFF, 8 }
083b80b5
MN
45};
46
47#define CODE_VLC_BITS 9
48static VLC code_vlc;
49
a8a15e9d
MM
50/* returns modified base_value */
51static inline int wnv1_get_code(WNV1Context *w, int base_value)
52{
f9c59f26 53 int v = bitstream_read_vlc(&w->bc, code_vlc.table, CODE_VLC_BITS, 1);
a8a15e9d 54
0a9132b8 55 if (v == 15)
f9c59f26 56 return ff_reverse[bitstream_read(&w->bc, 8 - w->shift)];
083b80b5 57 else
0a9132b8 58 return base_value + ((v - 7) << w->shift);
a8a15e9d
MM
59}
60
115329f1 61static int decode_frame(AVCodecContext *avctx,
df9b9567 62 void *data, int *got_frame,
7a00bbad 63 AVPacket *avpkt)
a8a15e9d
MM
64{
65 WNV1Context * const l = avctx->priv_data;
0a9132b8
AK
66 const uint8_t *buf = avpkt->data;
67 int buf_size = avpkt->size;
759001c5 68 AVFrame * const p = data;
a8a15e9d 69 unsigned char *Y,*U,*V;
f3fcb1a7 70 int i, j, ret;
a8a15e9d 71 int prev_y = 0, prev_u = 0, prev_v = 0;
b7093975
KS
72 uint8_t *rbuf;
73
91be1103
MS
74 if (buf_size < 8) {
75 av_log(avctx, AV_LOG_ERROR, "Packet is too short\n");
76 return AVERROR_INVALIDDATA;
77 }
78
059a9348 79 rbuf = av_malloc(buf_size + AV_INPUT_BUFFER_PADDING_SIZE);
0a9132b8 80 if (!rbuf) {
b7093975 81 av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer\n");
f3fcb1a7 82 return AVERROR(ENOMEM);
b7093975 83 }
a8a15e9d 84
759001c5 85 if ((ret = ff_get_buffer(avctx, p, 0)) < 0) {
a8a15e9d 86 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
b7093975 87 av_free(rbuf);
f3fcb1a7 88 return ret;
a8a15e9d
MM
89 }
90 p->key_frame = 1;
083b80b5 91
0a9132b8
AK
92 for (i = 8; i < buf_size; i++)
93 rbuf[i] = ff_reverse[buf[i]];
a25dac97 94 bitstream_init8(&l->bc, rbuf + 8, buf_size - 8);
083b80b5 95
a8a15e9d
MM
96 if (buf[2] >> 4 == 6)
97 l->shift = 2;
98 else {
99 l->shift = 8 - (buf[2] >> 4);
100 if (l->shift > 4) {
6d97484d
DB
101 avpriv_request_sample(avctx,
102 "Unknown WNV1 frame header value %i",
d9dee728 103 buf[2] >> 4);
a8a15e9d
MM
104 l->shift = 4;
105 }
106 if (l->shift < 1) {
6d97484d
DB
107 avpriv_request_sample(avctx,
108 "Unknown WNV1 frame header value %i",
d9dee728 109 buf[2] >> 4);
a8a15e9d
MM
110 l->shift = 1;
111 }
112 }
115329f1 113
a8a15e9d
MM
114 Y = p->data[0];
115 U = p->data[1];
116 V = p->data[2];
117 for (j = 0; j < avctx->height; j++) {
118 for (i = 0; i < avctx->width / 2; i++) {
119 Y[i * 2] = wnv1_get_code(l, prev_y);
120 prev_u = U[i] = wnv1_get_code(l, prev_u);
121 prev_y = Y[(i * 2) + 1] = wnv1_get_code(l, Y[i * 2]);
122 prev_v = V[i] = wnv1_get_code(l, prev_v);
123 }
124 Y += p->linesize[0];
125 U += p->linesize[1];
126 V += p->linesize[2];
127 }
128
115329f1 129
df9b9567 130 *got_frame = 1;
b7093975 131 av_free(rbuf);
115329f1 132
a8a15e9d
MM
133 return buf_size;
134}
135
0a9132b8
AK
136static av_cold int decode_init(AVCodecContext *avctx)
137{
a8a15e9d 138 WNV1Context * const l = avctx->priv_data;
bd4110f9 139 static VLC_TYPE code_table[1 << CODE_VLC_BITS][2];
a8a15e9d 140
0a9132b8 141 l->avctx = avctx;
716d413c 142 avctx->pix_fmt = AV_PIX_FMT_YUV422P;
a8a15e9d 143
0a9132b8 144 code_vlc.table = code_table;
bd4110f9 145 code_vlc.table_allocated = 1 << CODE_VLC_BITS;
c994dae1
KS
146 init_vlc(&code_vlc, CODE_VLC_BITS, 16,
147 &code_tab[0][1], 4, 2,
148 &code_tab[0][0], 4, 2, INIT_VLC_USE_NEW_STATIC);
083b80b5 149
a8a15e9d
MM
150 return 0;
151}
152
d36beb3f 153AVCodec ff_wnv1_decoder = {
ec6402b7 154 .name = "wnv1",
b2bed932 155 .long_name = NULL_IF_CONFIG_SMALL("Winnov WNV1"),
ec6402b7 156 .type = AVMEDIA_TYPE_VIDEO,
36ef5369 157 .id = AV_CODEC_ID_WNV1,
ec6402b7
AK
158 .priv_data_size = sizeof(WNV1Context),
159 .init = decode_init,
ec6402b7 160 .decode = decode_frame,
def97856 161 .capabilities = AV_CODEC_CAP_DR1,
a8a15e9d 162};