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/imgutils.h"
26 #include "libavutil/internal.h"
27 #include "libavutil/intreadwrite.h"
28 #include "libavutil/mem.h"
30 #include "bytestream.h"
33 #define HNM4_CHUNK_ID_PL 19536
34 #define HNM4_CHUNK_ID_IZ 23113
35 #define HNM4_CHUNK_ID_IU 21833
36 #define HNM4_CHUNK_ID_SD 17491
38 typedef struct Hnm4VideoContext {
47 uint32_t palette[256];
50 static int getbit(GetByteContext *gb, uint32_t *bitbuf, int *bits)
55 *bitbuf = bytestream2_get_le32(gb);
66 static void unpack_intraframe(AVCodecContext *avctx, uint8_t *src,
69 Hnm4VideoContext *hnm = avctx->priv_data;
71 uint32_t bitbuf = 0, writeoffset = 0, count = 0;
76 bytestream2_init(&gb, src, size);
78 while (bytestream2_tell(&gb) < size) {
79 if (getbit(&gb, &bitbuf, &bits)) {
80 if (writeoffset >= hnm->width * hnm->height) {
81 av_log(avctx, AV_LOG_ERROR,
82 "Attempting to write out of bounds\n");
85 hnm->current[writeoffset++] = bytestream2_get_byte(&gb);
87 if (getbit(&gb, &bitbuf, &bits)) {
88 word = bytestream2_get_le16(&gb);
90 offset = (word >> 3) - 0x2000;
92 count = bytestream2_get_byte(&gb);
96 count = getbit(&gb, &bitbuf, &bits) * 2;
97 count += getbit(&gb, &bitbuf, &bits);
98 offset = bytestream2_get_byte(&gb) - 0x0100;
101 offset += writeoffset;
102 if (offset < 0 || offset + count >= hnm->width * hnm->height) {
103 av_log(avctx, AV_LOG_ERROR, "Attempting to read out of bounds\n");
105 } else if (writeoffset + count >= hnm->width * hnm->height) {
106 av_log(avctx, AV_LOG_ERROR,
107 "Attempting to write out of bounds\n");
111 hnm->current[writeoffset++] = hnm->current[offset++];
117 static void postprocess_current_frame(AVCodecContext *avctx)
119 Hnm4VideoContext *hnm = avctx->priv_data;
120 uint32_t x, y, src_y;
121 int width = hnm->width;
123 for (y = 0; y < hnm->height; y++) {
124 uint8_t *dst = hnm->processed + y * width;
125 const uint8_t *src = hnm->current;
127 src += src_y * width + (y % 2);
128 for (x = 0; x < width; x++) {
135 static void copy_processed_frame(AVCodecContext *avctx, AVFrame *frame)
137 Hnm4VideoContext *hnm = avctx->priv_data;
138 uint8_t *src = hnm->processed;
139 uint8_t *dst = frame->data[0];
142 for (y = 0; y < hnm->height; y++) {
143 memcpy(dst, src, hnm->width);
145 dst += frame->linesize[0];
149 static int decode_interframe_v4(AVCodecContext *avctx, uint8_t *src, uint32_t size)
151 Hnm4VideoContext *hnm = avctx->priv_data;
153 uint32_t writeoffset = 0;
154 int count, left, offset;
155 uint8_t tag, previous, backline, backward, swap;
157 bytestream2_init(&gb, src, size);
159 while (bytestream2_tell(&gb) < size) {
160 count = bytestream2_peek_byte(&gb) & 0x1F;
162 tag = bytestream2_get_byte(&gb) & 0xE0;
166 if (writeoffset + 2 > hnm->width * hnm->height) {
167 av_log(avctx, AV_LOG_ERROR, "writeoffset out of bounds\n");
168 return AVERROR_INVALIDDATA;
170 hnm->current[writeoffset++] = bytestream2_get_byte(&gb);
171 hnm->current[writeoffset++] = bytestream2_get_byte(&gb);
172 } else if (tag == 1) {
173 writeoffset += bytestream2_get_byte(&gb) * 2;
174 } else if (tag == 2) {
175 count = bytestream2_get_le16(&gb);
177 writeoffset += count;
178 } else if (tag == 3) {
179 count = bytestream2_get_byte(&gb) * 2;
180 if (writeoffset + count > hnm->width * hnm->height) {
181 av_log(avctx, AV_LOG_ERROR, "writeoffset out of bounds\n");
182 return AVERROR_INVALIDDATA;
185 hnm->current[writeoffset++] = bytestream2_peek_byte(&gb);
188 bytestream2_skip(&gb, 1);
192 if (writeoffset > hnm->width * hnm->height) {
193 av_log(avctx, AV_LOG_ERROR, "writeoffset out of bounds\n");
194 return AVERROR_INVALIDDATA;
197 previous = bytestream2_peek_byte(&gb) & 0x20;
198 backline = bytestream2_peek_byte(&gb) & 0x40;
199 backward = bytestream2_peek_byte(&gb) & 0x80;
200 bytestream2_skip(&gb, 1);
201 swap = bytestream2_peek_byte(&gb) & 0x01;
202 offset = bytestream2_get_le16(&gb);
203 offset = (offset >> 1) & 0x7FFF;
204 offset = writeoffset + (offset * 2) - 0x8000;
208 if (!backward && offset + 2*count > hnm->width * hnm->height) {
209 av_log(avctx, AV_LOG_ERROR, "Attempting to read out of bounds\n");
210 return AVERROR_INVALIDDATA;
211 } else if (backward && offset + 1 >= hnm->width * hnm->height) {
212 av_log(avctx, AV_LOG_ERROR, "Attempting to read out of bounds\n");
213 return AVERROR_INVALIDDATA;
214 } else if (writeoffset + 2*count > hnm->width * hnm->height) {
215 av_log(avctx, AV_LOG_ERROR,
216 "Attempting to write out of bounds\n");
217 return AVERROR_INVALIDDATA;
221 if (offset < (!!backline)*(2 * hnm->width - 1) + 2*(left-1)) {
222 av_log(avctx, AV_LOG_ERROR, "Attempting to read out of bounds\n");
223 return AVERROR_INVALIDDATA;
226 if (offset < (!!backline)*(2 * hnm->width - 1)) {
227 av_log(avctx, AV_LOG_ERROR, "Attempting to read out of bounds\n");
228 return AVERROR_INVALIDDATA;
235 hnm->current[writeoffset++] = hnm->previous[offset - (2 * hnm->width) + 1];
236 hnm->current[writeoffset++] = hnm->previous[offset++];
239 hnm->current[writeoffset++] = hnm->previous[offset++];
240 hnm->current[writeoffset++] = hnm->previous[offset++];
249 hnm->current[writeoffset++] = hnm->current[offset - (2 * hnm->width) + 1];
250 hnm->current[writeoffset++] = hnm->current[offset++];
253 hnm->current[writeoffset++] = hnm->current[offset++];
254 hnm->current[writeoffset++] = hnm->current[offset++];
264 writeoffset -= count * 2;
266 swap = hnm->current[writeoffset];
267 hnm->current[writeoffset] = hnm->current[writeoffset + 1];
268 hnm->current[writeoffset + 1] = swap;
278 static void decode_interframe_v4a(AVCodecContext *avctx, uint8_t *src,
281 Hnm4VideoContext *hnm = avctx->priv_data;
283 uint32_t writeoffset = 0, offset;
284 uint8_t tag, count, previous, delta;
286 bytestream2_init(&gb, src, size);
288 while (bytestream2_tell(&gb) < size) {
289 count = bytestream2_peek_byte(&gb) & 0x3F;
291 tag = bytestream2_get_byte(&gb) & 0xC0;
294 writeoffset += bytestream2_get_byte(&gb);
295 } else if (tag == 1) {
296 if (writeoffset + hnm->width >= hnm->width * hnm->height) {
297 av_log(avctx, AV_LOG_ERROR, "writeoffset out of bounds\n");
300 hnm->current[writeoffset] = bytestream2_get_byte(&gb);
301 hnm->current[writeoffset + hnm->width] = bytestream2_get_byte(&gb);
303 } else if (tag == 2) {
304 writeoffset += hnm->width;
305 } else if (tag == 3) {
308 if (writeoffset > hnm->width * hnm->height) {
309 av_log(avctx, AV_LOG_ERROR, "writeoffset out of bounds\n");
313 delta = bytestream2_peek_byte(&gb) & 0x80;
314 previous = bytestream2_peek_byte(&gb) & 0x40;
315 bytestream2_skip(&gb, 1);
317 offset = writeoffset;
318 offset += bytestream2_get_le16(&gb);
321 if (offset < 0x10000) {
322 av_log(avctx, AV_LOG_ERROR, "Attempting to read out of bounds\n");
328 if (offset + hnm->width + count >= hnm->width * hnm->height) {
329 av_log(avctx, AV_LOG_ERROR, "Attempting to read out of bounds\n");
331 } else if (writeoffset + hnm->width + count >= hnm->width * hnm->height) {
332 av_log(avctx, AV_LOG_ERROR, "Attempting to write out of bounds\n");
338 hnm->current[writeoffset] = hnm->previous[offset];
339 hnm->current[writeoffset + hnm->width] = hnm->previous[offset + hnm->width];
346 hnm->current[writeoffset] = hnm->current[offset];
347 hnm->current[writeoffset + hnm->width] = hnm->current[offset + hnm->width];
357 static void hnm_update_palette(AVCodecContext *avctx, uint8_t *src,
360 Hnm4VideoContext *hnm = avctx->priv_data;
362 uint8_t start, writeoffset;
364 int eight_bit_colors;
366 eight_bit_colors = src[7] & 0x80 && hnm->version == 0x4a;
368 // skip first 8 bytes
369 bytestream2_init(&gb, src + 8, size - 8);
371 while (bytestream2_tell(&gb) < size - 8) {
372 start = bytestream2_get_byte(&gb);
373 count = bytestream2_get_byte(&gb);
374 if (start == 255 && count == 255)
380 hnm->palette[writeoffset] = bytestream2_get_be24(&gb);
381 if (!eight_bit_colors)
382 hnm->palette[writeoffset] <<= 2;
383 hnm->palette[writeoffset] |= (0xFFU << 24);
390 static int hnm_decode_frame(AVCodecContext *avctx, void *data,
391 int *got_frame, AVPacket *avpkt)
393 AVFrame *frame = data;
394 Hnm4VideoContext *hnm = avctx->priv_data;
398 if (avpkt->size < 8) {
399 av_log(avctx, AV_LOG_ERROR, "packet too small\n");
400 return AVERROR_INVALIDDATA;
403 chunk_id = AV_RL16(avpkt->data + 4);
405 if (chunk_id == HNM4_CHUNK_ID_PL) {
406 hnm_update_palette(avctx, avpkt->data, avpkt->size);
407 } else if (chunk_id == HNM4_CHUNK_ID_IZ) {
408 if (avpkt->size < 12) {
409 av_log(avctx, AV_LOG_ERROR, "packet too small\n");
410 return AVERROR_INVALIDDATA;
412 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
415 unpack_intraframe(avctx, avpkt->data + 12, avpkt->size - 12);
416 memcpy(hnm->previous, hnm->current, hnm->width * hnm->height);
417 if (hnm->version == 0x4a)
418 memcpy(hnm->processed, hnm->current, hnm->width * hnm->height);
420 postprocess_current_frame(avctx);
421 copy_processed_frame(avctx, frame);
422 frame->pict_type = AV_PICTURE_TYPE_I;
423 frame->key_frame = 1;
424 memcpy(frame->data[1], hnm->palette, 256 * 4);
426 } else if (chunk_id == HNM4_CHUNK_ID_IU) {
427 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
430 if (hnm->version == 0x4a) {
431 decode_interframe_v4a(avctx, avpkt->data + 8, avpkt->size - 8);
432 memcpy(hnm->processed, hnm->current, hnm->width * hnm->height);
434 int ret = decode_interframe_v4(avctx, avpkt->data + 8, avpkt->size - 8);
437 postprocess_current_frame(avctx);
439 copy_processed_frame(avctx, frame);
440 frame->pict_type = AV_PICTURE_TYPE_P;
441 frame->key_frame = 0;
442 memcpy(frame->data[1], hnm->palette, 256 * 4);
444 FFSWAP(uint8_t *, hnm->current, hnm->previous);
446 av_log(avctx, AV_LOG_ERROR, "invalid chunk id: %d\n", chunk_id);
447 return AVERROR_INVALIDDATA;
453 static av_cold int hnm_decode_init(AVCodecContext *avctx)
455 Hnm4VideoContext *hnm = avctx->priv_data;
458 if (avctx->extradata_size < 1) {
459 av_log(avctx, AV_LOG_ERROR,
460 "Extradata missing, decoder requires version number\n");
461 return AVERROR_INVALIDDATA;
464 ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
467 if (avctx->height & 1)
468 return AVERROR(EINVAL);
470 hnm->version = avctx->extradata[0];
471 avctx->pix_fmt = AV_PIX_FMT_PAL8;
472 hnm->width = avctx->width;
473 hnm->height = avctx->height;
474 hnm->buffer1 = av_mallocz(avctx->width * avctx->height);
475 hnm->buffer2 = av_mallocz(avctx->width * avctx->height);
476 hnm->processed = av_mallocz(avctx->width * avctx->height);
478 if (!hnm->buffer1 || !hnm->buffer2 || !hnm->processed) {
479 av_log(avctx, AV_LOG_ERROR, "av_mallocz() failed\n");
480 return AVERROR(ENOMEM);
483 hnm->current = hnm->buffer1;
484 hnm->previous = hnm->buffer2;
489 static av_cold int hnm_decode_end(AVCodecContext *avctx)
491 Hnm4VideoContext *hnm = avctx->priv_data;
493 av_freep(&hnm->buffer1);
494 av_freep(&hnm->buffer2);
495 av_freep(&hnm->processed);
500 AVCodec ff_hnm4_video_decoder = {
502 .long_name = NULL_IF_CONFIG_SMALL("HNM 4 video"),
503 .type = AVMEDIA_TYPE_VIDEO,
504 .id = AV_CODEC_ID_HNM4_VIDEO,
505 .priv_data_size = sizeof(Hnm4VideoContext),
506 .init = hnm_decode_init,
507 .close = hnm_decode_end,
508 .decode = hnm_decode_frame,
509 .capabilities = AV_CODEC_CAP_DR1,
510 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,