00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00047 #include <stdio.h>
00048 #include <stdlib.h>
00049 #include <string.h>
00050
00051 #include "avcodec.h"
00052 #include "libavutil/internal.h"
00053
00054 #define HUFFMAN_TABLE_SIZE 64 * 1024
00055 #define HUF_TOKENS 256
00056 #define PALETTE_COUNT 256
00057
00058 typedef struct
00059 {
00060 int count;
00061 unsigned char used;
00062 int children[2];
00063 } hnode;
00064
00065 typedef struct IdcinContext {
00066
00067 AVCodecContext *avctx;
00068 AVFrame frame;
00069
00070 const unsigned char *buf;
00071 int size;
00072
00073 hnode huff_nodes[256][HUF_TOKENS*2];
00074 int num_huff_nodes[256];
00075
00076 uint32_t pal[256];
00077 } IdcinContext;
00078
00079
00080
00081
00082
00083
00084
00085 static int huff_smallest_node(hnode *hnodes, int num_hnodes) {
00086 int i;
00087 int best, best_node;
00088
00089 best = 99999999;
00090 best_node = -1;
00091 for(i = 0; i < num_hnodes; i++) {
00092 if(hnodes[i].used)
00093 continue;
00094 if(!hnodes[i].count)
00095 continue;
00096 if(hnodes[i].count < best) {
00097 best = hnodes[i].count;
00098 best_node = i;
00099 }
00100 }
00101
00102 if(best_node == -1)
00103 return -1;
00104 hnodes[best_node].used = 1;
00105 return best_node;
00106 }
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117 static av_cold void huff_build_tree(IdcinContext *s, int prev) {
00118 hnode *node, *hnodes;
00119 int num_hnodes, i;
00120
00121 num_hnodes = HUF_TOKENS;
00122 hnodes = s->huff_nodes[prev];
00123 for(i = 0; i < HUF_TOKENS * 2; i++)
00124 hnodes[i].used = 0;
00125
00126 while (1) {
00127 node = &hnodes[num_hnodes];
00128
00129
00130 node->children[0] = huff_smallest_node(hnodes, num_hnodes);
00131 if(node->children[0] == -1)
00132 break;
00133
00134 node->children[1] = huff_smallest_node(hnodes, num_hnodes);
00135 if(node->children[1] == -1)
00136 break;
00137
00138
00139 node->count = hnodes[node->children[0]].count +
00140 hnodes[node->children[1]].count;
00141 num_hnodes++;
00142 }
00143
00144 s->num_huff_nodes[prev] = num_hnodes - 1;
00145 }
00146
00147 static av_cold int idcin_decode_init(AVCodecContext *avctx)
00148 {
00149 IdcinContext *s = avctx->priv_data;
00150 int i, j, histogram_index = 0;
00151 unsigned char *histograms;
00152
00153 s->avctx = avctx;
00154 avctx->pix_fmt = PIX_FMT_PAL8;
00155
00156
00157 if (s->avctx->extradata_size != HUFFMAN_TABLE_SIZE) {
00158 av_log(s->avctx, AV_LOG_ERROR, " id CIN video: expected extradata size of %d\n", HUFFMAN_TABLE_SIZE);
00159 return -1;
00160 }
00161
00162
00163 histograms = (unsigned char *)s->avctx->extradata;
00164 for (i = 0; i < 256; i++) {
00165 for(j = 0; j < HUF_TOKENS; j++)
00166 s->huff_nodes[i][j].count = histograms[histogram_index++];
00167 huff_build_tree(s, i);
00168 }
00169
00170 avcodec_get_frame_defaults(&s->frame);
00171 s->frame.data[0] = NULL;
00172
00173 return 0;
00174 }
00175
00176 static void idcin_decode_vlcs(IdcinContext *s)
00177 {
00178 hnode *hnodes;
00179 long x, y;
00180 int prev;
00181 unsigned char v = 0;
00182 int bit_pos, node_num, dat_pos;
00183
00184 prev = bit_pos = dat_pos = 0;
00185 for (y = 0; y < (s->frame.linesize[0] * s->avctx->height);
00186 y += s->frame.linesize[0]) {
00187 for (x = y; x < y + s->avctx->width; x++) {
00188 node_num = s->num_huff_nodes[prev];
00189 hnodes = s->huff_nodes[prev];
00190
00191 while(node_num >= HUF_TOKENS) {
00192 if(!bit_pos) {
00193 if(dat_pos >= s->size) {
00194 av_log(s->avctx, AV_LOG_ERROR, "Huffman decode error.\n");
00195 return;
00196 }
00197 bit_pos = 8;
00198 v = s->buf[dat_pos++];
00199 }
00200
00201 node_num = hnodes[node_num].children[v & 0x01];
00202 v = v >> 1;
00203 bit_pos--;
00204 }
00205
00206 s->frame.data[0][x] = node_num;
00207 prev = node_num;
00208 }
00209 }
00210 }
00211
00212 static int idcin_decode_frame(AVCodecContext *avctx,
00213 void *data, int *data_size,
00214 AVPacket *avpkt)
00215 {
00216 const uint8_t *buf = avpkt->data;
00217 int buf_size = avpkt->size;
00218 IdcinContext *s = avctx->priv_data;
00219 const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
00220
00221 s->buf = buf;
00222 s->size = buf_size;
00223
00224 if (s->frame.data[0])
00225 avctx->release_buffer(avctx, &s->frame);
00226
00227 if (avctx->get_buffer(avctx, &s->frame)) {
00228 av_log(avctx, AV_LOG_ERROR, " id CIN Video: get_buffer() failed\n");
00229 return -1;
00230 }
00231
00232 idcin_decode_vlcs(s);
00233
00234 if (pal) {
00235 s->frame.palette_has_changed = 1;
00236 memcpy(s->pal, pal, AVPALETTE_SIZE);
00237 }
00238
00239 memcpy(s->frame.data[1], s->pal, AVPALETTE_SIZE);
00240
00241 *data_size = sizeof(AVFrame);
00242 *(AVFrame*)data = s->frame;
00243
00244
00245 return buf_size;
00246 }
00247
00248 static av_cold int idcin_decode_end(AVCodecContext *avctx)
00249 {
00250 IdcinContext *s = avctx->priv_data;
00251
00252 if (s->frame.data[0])
00253 avctx->release_buffer(avctx, &s->frame);
00254
00255 return 0;
00256 }
00257
00258 AVCodec ff_idcin_decoder = {
00259 .name = "idcinvideo",
00260 .type = AVMEDIA_TYPE_VIDEO,
00261 .id = AV_CODEC_ID_IDCIN,
00262 .priv_data_size = sizeof(IdcinContext),
00263 .init = idcin_decode_init,
00264 .close = idcin_decode_end,
00265 .decode = idcin_decode_frame,
00266 .capabilities = CODEC_CAP_DR1,
00267 .long_name = NULL_IF_CONFIG_SMALL("id Quake II CIN video"),
00268 };