2 * Cryo Interactive Entertainment HNM4 video decoder
4 * Copyright (c) 2012 David Kment
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "libavutil/internal.h"
26 #include "libavutil/intreadwrite.h"
27 #include "libavutil/mem.h"
29 #include "bytestream.h"
32 #define HNM4_CHUNK_ID_PL 19536
33 #define HNM4_CHUNK_ID_IZ 23113
34 #define HNM4_CHUNK_ID_IU 21833
35 #define HNM4_CHUNK_ID_SD 17491
37 typedef struct Hnm4VideoContext {
46 uint32_t palette[256];
49 static int getbit(GetByteContext *gb, uint32_t *bitbuf, int *bits)
54 *bitbuf = bytestream2_get_le32(gb);
65 static void unpack_intraframe(AVCodecContext *avctx, uint8_t *src,
68 Hnm4VideoContext *hnm = avctx->priv_data;
70 uint32_t bitbuf = 0, writeoffset = 0, count = 0;
75 bytestream2_init(&gb, src, size);
77 while (bytestream2_tell(&gb) < size) {
78 if (getbit(&gb, &bitbuf, &bits)) {
79 if (writeoffset >= hnm->width * hnm->height) {
80 av_log(avctx, AV_LOG_ERROR,
81 "Attempting to write out of bounds");
84 hnm->current[writeoffset++] = bytestream2_get_byte(&gb);
86 if (getbit(&gb, &bitbuf, &bits)) {
87 word = bytestream2_get_le16(&gb);
89 offset = (word >> 3) - 0x2000;
91 count = bytestream2_get_byte(&gb);
95 count = getbit(&gb, &bitbuf, &bits) * 2;
96 count += getbit(&gb, &bitbuf, &bits);
97 offset = bytestream2_get_byte(&gb) - 0x0100;
100 offset += writeoffset;
101 if (offset < 0 || offset + count >= hnm->width * hnm->height) {
102 av_log(avctx, AV_LOG_ERROR, "Attempting to read out of bounds");
104 } else if (writeoffset + count >= hnm->width * hnm->height) {
105 av_log(avctx, AV_LOG_ERROR,
106 "Attempting to write out of bounds");
110 hnm->current[writeoffset++] = hnm->current[offset++];
116 static void postprocess_current_frame(AVCodecContext *avctx)
118 Hnm4VideoContext *hnm = avctx->priv_data;
119 uint32_t x, y, src_x, src_y;
121 for (y = 0; y < hnm->height; y++) {
123 src_x = src_y * hnm->width + (y % 2);
124 for (x = 0; x < hnm->width; x++) {
125 hnm->processed[(y * hnm->width) + x] = hnm->current[src_x];
131 static void copy_processed_frame(AVCodecContext *avctx, AVFrame *frame)
133 Hnm4VideoContext *hnm = avctx->priv_data;
134 uint8_t *src = hnm->processed;
135 uint8_t *dst = frame->data[0];
138 for (y = 0; y < hnm->height; y++) {
139 memcpy(dst, src, hnm->width);
141 dst += frame->linesize[0];
145 static void decode_interframe_v4(AVCodecContext *avctx, uint8_t *src, uint32_t size)
147 Hnm4VideoContext *hnm = avctx->priv_data;
149 uint32_t writeoffset = 0, count, left, offset;
150 uint8_t tag, previous, backline, backward, swap;
152 bytestream2_init(&gb, src, size);
154 while (bytestream2_tell(&gb) < size) {
155 count = bytestream2_peek_byte(&gb) & 0x1F;
157 tag = bytestream2_get_byte(&gb) & 0xE0;
160 hnm->current[writeoffset++] = bytestream2_get_byte(&gb);
161 hnm->current[writeoffset++] = bytestream2_get_byte(&gb);
162 } else if (tag == 1) {
163 writeoffset += bytestream2_get_byte(&gb) * 2;
164 } else if (tag == 2) {
165 count = bytestream2_get_le16(&gb);
167 writeoffset += count;
168 } else if (tag == 3) {
169 count = bytestream2_get_byte(&gb) * 2;
171 hnm->current[writeoffset++] = bytestream2_peek_byte(&gb);
174 bytestream2_skip(&gb, 1);
179 previous = bytestream2_peek_byte(&gb) & 0x20;
180 backline = bytestream2_peek_byte(&gb) & 0x40;
181 backward = bytestream2_peek_byte(&gb) & 0x80;
182 bytestream2_skip(&gb, 1);
183 swap = bytestream2_peek_byte(&gb) & 0x01;
184 offset = bytestream2_get_le16(&gb);
185 offset = (offset >> 1) & 0x7FFF;
186 offset = writeoffset + (offset * 2) - 0x8000;
190 if (!backward && offset + count >= hnm->width * hnm->height) {
191 av_log(avctx, AV_LOG_ERROR, "Attempting to read out of bounds");
193 } else if (backward && offset >= hnm->width * hnm->height) {
194 av_log(avctx, AV_LOG_ERROR, "Attempting to read out of bounds");
196 } else if (writeoffset + count >= hnm->width * hnm->height) {
197 av_log(avctx, AV_LOG_ERROR,
198 "Attempting to write out of bounds");
205 hnm->current[writeoffset++] = hnm->previous[offset - (2 * hnm->width) + 1];
206 hnm->current[writeoffset++] = hnm->previous[offset++];
209 hnm->current[writeoffset++] = hnm->previous[offset++];
210 hnm->current[writeoffset++] = hnm->previous[offset++];
219 hnm->current[writeoffset++] = hnm->current[offset - (2 * hnm->width) + 1];
220 hnm->current[writeoffset++] = hnm->current[offset++];
223 hnm->current[writeoffset++] = hnm->current[offset++];
224 hnm->current[writeoffset++] = hnm->current[offset++];
234 writeoffset -= count * 2;
236 swap = hnm->current[writeoffset];
237 hnm->current[writeoffset] = hnm->current[writeoffset + 1];
238 hnm->current[writeoffset + 1] = swap;
247 static void decode_interframe_v4a(AVCodecContext *avctx, uint8_t *src,
250 Hnm4VideoContext *hnm = avctx->priv_data;
252 uint32_t writeoffset = 0, offset;
253 uint8_t tag, count, previous, delta;
255 bytestream2_init(&gb, src, size);
257 while (bytestream2_tell(&gb) < size) {
258 count = bytestream2_peek_byte(&gb) & 0x3F;
260 tag = bytestream2_get_byte(&gb) & 0xC0;
263 writeoffset += bytestream2_get_byte(&gb);
264 } else if (tag == 1) {
265 hnm->current[writeoffset] = bytestream2_get_byte(&gb);
266 hnm->current[writeoffset + hnm->width] = bytestream2_get_byte(&gb);
268 } else if (tag == 2) {
269 writeoffset += hnm->width;
270 } else if (tag == 3) {
274 delta = bytestream2_peek_byte(&gb) & 0x80;
275 previous = bytestream2_peek_byte(&gb) & 0x40;
276 bytestream2_skip(&gb, 1);
278 offset = writeoffset;
279 offset += bytestream2_get_le16(&gb);
284 if (offset + hnm->width + count >= hnm->width * hnm->height) {
285 av_log(avctx, AV_LOG_ERROR, "Attempting to read out of bounds");
287 } else if (writeoffset + hnm->width + count >= hnm->width * hnm->height) {
288 av_log(avctx, AV_LOG_ERROR, "Attempting to write out of bounds");
294 hnm->current[writeoffset] = hnm->previous[offset];
295 hnm->current[writeoffset + hnm->width] = hnm->previous[offset + hnm->width];
302 hnm->current[writeoffset] = hnm->current[offset];
303 hnm->current[writeoffset + hnm->width] = hnm->current[offset + hnm->width];
313 static void hnm_update_palette(AVCodecContext *avctx, uint8_t *src,
316 Hnm4VideoContext *hnm = avctx->priv_data;
318 uint8_t start, writeoffset;
320 int eight_bit_colors;
322 eight_bit_colors = src[7] & 0x80 && hnm->version == 0x4a;
324 // skip first 8 bytes
325 bytestream2_init(&gb, src + 8, size - 8);
327 while (bytestream2_tell(&gb) < size - 8) {
328 start = bytestream2_get_byte(&gb);
329 count = bytestream2_get_byte(&gb);
330 if (start == 255 && count == 255)
336 hnm->palette[writeoffset] = bytestream2_get_be24(&gb);
337 if (!eight_bit_colors)
338 hnm->palette[writeoffset] <<= 2;
345 static void hnm_flip_buffers(Hnm4VideoContext *hnm)
350 hnm->current = hnm->previous;
351 hnm->previous = temp;
354 static int hnm_decode_frame(AVCodecContext *avctx, void *data,
355 int *got_frame, AVPacket *avpkt)
357 AVFrame *frame = data;
358 Hnm4VideoContext *hnm = avctx->priv_data;
362 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
363 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
367 chunk_id = AV_RL16(avpkt->data + 4);
369 if (chunk_id == HNM4_CHUNK_ID_PL) {
370 hnm_update_palette(avctx, avpkt->data, avpkt->size);
371 frame->palette_has_changed = 1;
372 } else if (chunk_id == HNM4_CHUNK_ID_IZ) {
373 unpack_intraframe(avctx, avpkt->data + 12, avpkt->size - 12);
374 memcpy(hnm->previous, hnm->current, hnm->width * hnm->height);
375 if (hnm->version == 0x4a)
376 memcpy(hnm->processed, hnm->current, hnm->width * hnm->height);
378 postprocess_current_frame(avctx);
379 copy_processed_frame(avctx, frame);
380 frame->pict_type = AV_PICTURE_TYPE_I;
381 frame->key_frame = 1;
382 memcpy(frame->data[1], hnm->palette, 256 * 4);
384 } else if (chunk_id == HNM4_CHUNK_ID_IU) {
385 if (hnm->version == 0x4a) {
386 decode_interframe_v4a(avctx, avpkt->data + 8, avpkt->size - 8);
387 memcpy(hnm->processed, hnm->current, hnm->width * hnm->height);
389 decode_interframe_v4(avctx, avpkt->data + 8, avpkt->size - 8);
390 postprocess_current_frame(avctx);
392 copy_processed_frame(avctx, frame);
393 frame->pict_type = AV_PICTURE_TYPE_P;
394 frame->key_frame = 0;
395 memcpy(frame->data[1], hnm->palette, 256 * 4);
397 hnm_flip_buffers(hnm);
399 av_log(avctx, AV_LOG_ERROR, "invalid chunk id: %d\n", chunk_id);
400 return AVERROR_INVALIDDATA;
406 static av_cold int hnm_decode_init(AVCodecContext *avctx)
408 Hnm4VideoContext *hnm = avctx->priv_data;
410 if (avctx->extradata_size < 1) {
411 av_log(avctx, AV_LOG_ERROR,
412 "Extradata missing, decoder requires version number\n");
413 return AVERROR_INVALIDDATA;
416 hnm->version = avctx->extradata[0];
417 avctx->pix_fmt = AV_PIX_FMT_PAL8;
418 hnm->width = avctx->width;
419 hnm->height = avctx->height;
420 hnm->buffer1 = av_mallocz(avctx->width * avctx->height);
421 hnm->buffer2 = av_mallocz(avctx->width * avctx->height);
422 hnm->processed = av_mallocz(avctx->width * avctx->height);
424 if (!hnm->buffer1 || !hnm->buffer2 || !hnm->processed) {
425 av_log(avctx, AV_LOG_ERROR, "av_mallocz() failed\n");
426 av_freep(&hnm->buffer1);
427 av_freep(&hnm->buffer2);
428 av_freep(&hnm->processed);
429 return AVERROR(ENOMEM);
432 hnm->current = hnm->buffer1;
433 hnm->previous = hnm->buffer2;
438 static av_cold int hnm_decode_end(AVCodecContext *avctx)
440 Hnm4VideoContext *hnm = avctx->priv_data;
442 av_freep(&hnm->buffer1);
443 av_freep(&hnm->buffer2);
444 av_freep(&hnm->processed);
449 AVCodec ff_hnm4_video_decoder = {
451 .long_name = NULL_IF_CONFIG_SMALL("HNM 4 video"),
452 .type = AVMEDIA_TYPE_VIDEO,
453 .id = AV_CODEC_ID_HNM4_VIDEO,
454 .priv_data_size = sizeof(Hnm4VideoContext),
455 .init = hnm_decode_init,
456 .close = hnm_decode_end,
457 .decode = hnm_decode_frame,
458 .capabilities = CODEC_CAP_DR1,