]> git.sesse.net Git - ffmpeg/blob - libavcodec/hnm4video.c
Merge commit '07fd0a22192805d56c635eb294dc26b0a54ae325'
[ffmpeg] / libavcodec / hnm4video.c
1 /*
2  * Cryo Interactive Entertainment HNM4 video decoder
3  *
4  * Copyright (c) 2012 David Kment
5  *
6  * This file is part of FFmpeg.
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #include <string.h>
24
25 #include "libavutil/internal.h"
26 #include "libavutil/intreadwrite.h"
27 #include "libavutil/mem.h"
28 #include "avcodec.h"
29 #include "bytestream.h"
30 #include "internal.h"
31
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
36
37 typedef struct Hnm4VideoContext {
38     uint8_t version;
39     uint16_t width;
40     uint16_t height;
41     uint8_t *current;
42     uint8_t *previous;
43     uint8_t *buffer1;
44     uint8_t *buffer2;
45     uint8_t *processed;
46     uint32_t palette[256];
47 } Hnm4VideoContext;
48
49 static int getbit(GetByteContext *gb, uint32_t *bitbuf, int *bits)
50 {
51     int ret;
52
53     if (!*bits) {
54         *bitbuf = bytestream2_get_le32(gb);
55         *bits = 32;
56     }
57
58     ret = *bitbuf >> 31;
59     *bitbuf <<= 1;
60     (*bits)--;
61
62     return ret;
63 }
64
65 static void unpack_intraframe(AVCodecContext *avctx, uint8_t *src,
66                               uint32_t size)
67 {
68     Hnm4VideoContext *hnm = avctx->priv_data;
69     GetByteContext gb;
70     uint32_t bitbuf = 0, writeoffset = 0, count = 0;
71     uint16_t word;
72     int32_t offset;
73     int bits = 0;
74
75     bytestream2_init(&gb, src, size);
76
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\n");
82                 break;
83             }
84             hnm->current[writeoffset++] = bytestream2_get_byte(&gb);
85         } else {
86             if (getbit(&gb, &bitbuf, &bits)) {
87                 word   = bytestream2_get_le16(&gb);
88                 count  = word & 0x07;
89                 offset = (word >> 3) - 0x2000;
90                 if (!count)
91                     count = bytestream2_get_byte(&gb);
92                 if (!count)
93                     return;
94             } else {
95                 count  = getbit(&gb, &bitbuf, &bits) * 2;
96                 count += getbit(&gb, &bitbuf, &bits);
97                 offset = bytestream2_get_byte(&gb) - 0x0100;
98             }
99             count  += 2;
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\n");
103                 break;
104             } else if (writeoffset + count >= hnm->width * hnm->height) {
105                 av_log(avctx, AV_LOG_ERROR,
106                        "Attempting to write out of bounds\n");
107                 break;
108             }
109             while (count--) {
110                 hnm->current[writeoffset++] = hnm->current[offset++];
111             }
112         }
113     }
114 }
115
116 static void postprocess_current_frame(AVCodecContext *avctx)
117 {
118     Hnm4VideoContext *hnm = avctx->priv_data;
119     uint32_t x, y, src_x, src_y;
120
121     for (y = 0; y < hnm->height; y++) {
122         src_y = y - (y % 2);
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];
126             src_x += 2;
127         }
128     }
129 }
130
131 static void copy_processed_frame(AVCodecContext *avctx, AVFrame *frame)
132 {
133     Hnm4VideoContext *hnm = avctx->priv_data;
134     uint8_t *src = hnm->processed;
135     uint8_t *dst = frame->data[0];
136     int y;
137
138     for (y = 0; y < hnm->height; y++) {
139         memcpy(dst, src, hnm->width);
140         src += hnm->width;
141         dst += frame->linesize[0];
142     }
143 }
144
145 static void decode_interframe_v4(AVCodecContext *avctx, uint8_t *src, uint32_t size)
146 {
147     Hnm4VideoContext *hnm = avctx->priv_data;
148     GetByteContext gb;
149     uint32_t writeoffset = 0;
150     int count, left, offset;
151     uint8_t tag, previous, backline, backward, swap;
152
153     bytestream2_init(&gb, src, size);
154
155     while (bytestream2_tell(&gb) < size) {
156         count = bytestream2_peek_byte(&gb) & 0x1F;
157         if (count == 0) {
158             tag = bytestream2_get_byte(&gb) & 0xE0;
159             tag = tag >> 5;
160
161             if (tag == 0) {
162                 if (writeoffset + 2 > hnm->width * hnm->height) {
163                     av_log(avctx, AV_LOG_ERROR, "writeoffset out of bounds\n");
164                     break;
165                 }
166                 hnm->current[writeoffset++] = bytestream2_get_byte(&gb);
167                 hnm->current[writeoffset++] = bytestream2_get_byte(&gb);
168             } else if (tag == 1) {
169                 writeoffset += bytestream2_get_byte(&gb) * 2;
170             } else if (tag == 2) {
171                 count = bytestream2_get_le16(&gb);
172                 count *= 2;
173                 writeoffset += count;
174             } else if (tag == 3) {
175                 count = bytestream2_get_byte(&gb) * 2;
176                 if (writeoffset + count > hnm->width * hnm->height) {
177                     av_log(avctx, AV_LOG_ERROR, "writeoffset out of bounds\n");
178                     break;
179                 }
180                 while (count > 0) {
181                     hnm->current[writeoffset++] = bytestream2_peek_byte(&gb);
182                     count--;
183                 }
184                 bytestream2_skip(&gb, 1);
185             } else {
186                 break;
187             }
188             if (writeoffset > hnm->width * hnm->height) {
189                 av_log(avctx, AV_LOG_ERROR, "writeoffset out of bounds\n");
190                 break;
191             }
192         } else {
193             previous = bytestream2_peek_byte(&gb) & 0x20;
194             backline = bytestream2_peek_byte(&gb) & 0x40;
195             backward = bytestream2_peek_byte(&gb) & 0x80;
196             bytestream2_skip(&gb, 1);
197             swap   = bytestream2_peek_byte(&gb) & 0x01;
198             offset = bytestream2_get_le16(&gb);
199             offset = (offset >> 1) & 0x7FFF;
200             offset = writeoffset + (offset * 2) - 0x8000;
201
202             left = count;
203
204             if (!backward && offset + 2*count > hnm->width * hnm->height) {
205                 av_log(avctx, AV_LOG_ERROR, "Attempting to read out of bounds\n");
206                 break;
207             } else if (backward && offset + 1 >= hnm->width * hnm->height) {
208                 av_log(avctx, AV_LOG_ERROR, "Attempting to read out of bounds\n");
209                 break;
210             } else if (writeoffset + 2*count > hnm->width * hnm->height) {
211                 av_log(avctx, AV_LOG_ERROR,
212                        "Attempting to write out of bounds\n");
213                 break;
214             }
215             if(backward) {
216                 if (offset < (!!backline)*(2 * hnm->width - 1) + 2*(left-1)) {
217                     av_log(avctx, AV_LOG_ERROR, "Attempting to read out of bounds\n");
218                     break;
219                 }
220             } else {
221                 if (offset < (!!backline)*(2 * hnm->width - 1)) {
222                     av_log(avctx, AV_LOG_ERROR, "Attempting to read out of bounds\n");
223                     break;
224                 }
225             }
226
227             if (previous) {
228                 while (left > 0) {
229                     if (backline) {
230                         hnm->current[writeoffset++] = hnm->previous[offset - (2 * hnm->width) + 1];
231                         hnm->current[writeoffset++] = hnm->previous[offset++];
232                         offset++;
233                     } else {
234                         hnm->current[writeoffset++] = hnm->previous[offset++];
235                         hnm->current[writeoffset++] = hnm->previous[offset++];
236                     }
237                     if (backward)
238                         offset -= 4;
239                     left--;
240                 }
241             } else {
242                 while (left > 0) {
243                     if (backline) {
244                         hnm->current[writeoffset++] = hnm->current[offset - (2 * hnm->width) + 1];
245                         hnm->current[writeoffset++] = hnm->current[offset++];
246                         offset++;
247                     } else {
248                         hnm->current[writeoffset++] = hnm->current[offset++];
249                         hnm->current[writeoffset++] = hnm->current[offset++];
250                     }
251                     if (backward)
252                         offset -= 4;
253                     left--;
254                 }
255             }
256
257             if (swap) {
258                 left         = count;
259                 writeoffset -= count * 2;
260                 while (left > 0) {
261                     swap = hnm->current[writeoffset];
262                     hnm->current[writeoffset] = hnm->current[writeoffset + 1];
263                     hnm->current[writeoffset + 1] = swap;
264                     left--;
265                     writeoffset += 2;
266                 }
267             }
268         }
269     }
270 }
271
272 static void decode_interframe_v4a(AVCodecContext *avctx, uint8_t *src,
273                                   uint32_t size)
274 {
275     Hnm4VideoContext *hnm = avctx->priv_data;
276     GetByteContext gb;
277     uint32_t writeoffset = 0, offset;
278     uint8_t tag, count, previous, delta;
279
280     bytestream2_init(&gb, src, size);
281
282     while (bytestream2_tell(&gb) < size) {
283         count = bytestream2_peek_byte(&gb) & 0x3F;
284         if (count == 0) {
285             tag = bytestream2_get_byte(&gb) & 0xC0;
286             tag = tag >> 6;
287             if (tag == 0) {
288                 writeoffset += bytestream2_get_byte(&gb);
289             } else if (tag == 1) {
290                 if (writeoffset + hnm->width >= hnm->width * hnm->height) {
291                     av_log(avctx, AV_LOG_ERROR, "writeoffset out of bounds\n");
292                     break;
293                 }
294                 hnm->current[writeoffset]              = bytestream2_get_byte(&gb);
295                 hnm->current[writeoffset + hnm->width] = bytestream2_get_byte(&gb);
296                 writeoffset++;
297             } else if (tag == 2) {
298                 writeoffset += hnm->width;
299             } else if (tag == 3) {
300                 break;
301             }
302             if (writeoffset > hnm->width * hnm->height) {
303                 av_log(avctx, AV_LOG_ERROR, "writeoffset out of bounds\n");
304                 break;
305             }
306         } else {
307             delta    = bytestream2_peek_byte(&gb) & 0x80;
308             previous = bytestream2_peek_byte(&gb) & 0x40;
309             bytestream2_skip(&gb, 1);
310
311             offset  = writeoffset;
312             offset += bytestream2_get_le16(&gb);
313
314             if (delta)
315                 offset -= 0x10000;
316
317             if (offset + hnm->width + count >= hnm->width * hnm->height) {
318                 av_log(avctx, AV_LOG_ERROR, "Attempting to read out of bounds\n");
319                 break;
320             } else if (writeoffset + hnm->width + count >= hnm->width * hnm->height) {
321                 av_log(avctx, AV_LOG_ERROR, "Attempting to write out of bounds\n");
322                 break;
323             }
324
325             if (previous) {
326                 while (count > 0) {
327                     hnm->current[writeoffset]              = hnm->previous[offset];
328                     hnm->current[writeoffset + hnm->width] = hnm->previous[offset + hnm->width];
329                     writeoffset++;
330                     offset++;
331                     count--;
332                 }
333             } else {
334                 while (count > 0) {
335                     hnm->current[writeoffset]              = hnm->current[offset];
336                     hnm->current[writeoffset + hnm->width] = hnm->current[offset + hnm->width];
337                     writeoffset++;
338                     offset++;
339                     count--;
340                 }
341             }
342         }
343     }
344 }
345
346 static void hnm_update_palette(AVCodecContext *avctx, uint8_t *src,
347                                uint32_t size)
348 {
349     Hnm4VideoContext *hnm = avctx->priv_data;
350     GetByteContext gb;
351     uint8_t start, writeoffset;
352     uint16_t count;
353     int eight_bit_colors;
354
355     eight_bit_colors = src[7] & 0x80 && hnm->version == 0x4a;
356
357     // skip first 8 bytes
358     bytestream2_init(&gb, src + 8, size - 8);
359
360     while (bytestream2_tell(&gb) < size - 8) {
361         start = bytestream2_get_byte(&gb);
362         count = bytestream2_get_byte(&gb);
363         if (start == 255 && count == 255)
364             break;
365         if (count == 0)
366             count = 256;
367         writeoffset = start;
368         while (count > 0) {
369             hnm->palette[writeoffset] = bytestream2_get_be24(&gb);
370             if (!eight_bit_colors)
371                 hnm->palette[writeoffset] <<= 2;
372             count--;
373             writeoffset++;
374         }
375     }
376 }
377
378 static void hnm_flip_buffers(Hnm4VideoContext *hnm)
379 {
380     uint8_t *temp;
381
382     temp          = hnm->current;
383     hnm->current  = hnm->previous;
384     hnm->previous = temp;
385 }
386
387 static int hnm_decode_frame(AVCodecContext *avctx, void *data,
388                             int *got_frame, AVPacket *avpkt)
389 {
390     AVFrame *frame = data;
391     Hnm4VideoContext *hnm = avctx->priv_data;
392     int ret;
393     uint16_t chunk_id;
394
395     if (avpkt->size < 8) {
396         av_log(avctx, AV_LOG_ERROR, "packet too small\n");
397         return AVERROR_INVALIDDATA;
398     }
399
400     if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
401         return ret;
402
403     chunk_id = AV_RL16(avpkt->data + 4);
404
405     if (chunk_id == HNM4_CHUNK_ID_PL) {
406         hnm_update_palette(avctx, avpkt->data, avpkt->size);
407         frame->palette_has_changed = 1;
408     } else if (chunk_id == HNM4_CHUNK_ID_IZ) {
409         if (avpkt->size < 12) {
410             av_log(avctx, AV_LOG_ERROR, "packet too small\n");
411             return AVERROR_INVALIDDATA;
412         }
413         unpack_intraframe(avctx, avpkt->data + 12, avpkt->size - 12);
414         memcpy(hnm->previous, hnm->current, hnm->width * hnm->height);
415         if (hnm->version == 0x4a)
416             memcpy(hnm->processed, hnm->current, hnm->width * hnm->height);
417         else
418             postprocess_current_frame(avctx);
419         copy_processed_frame(avctx, frame);
420         frame->pict_type = AV_PICTURE_TYPE_I;
421         frame->key_frame = 1;
422         memcpy(frame->data[1], hnm->palette, 256 * 4);
423         *got_frame = 1;
424     } else if (chunk_id == HNM4_CHUNK_ID_IU) {
425         if (hnm->version == 0x4a) {
426             decode_interframe_v4a(avctx, avpkt->data + 8, avpkt->size - 8);
427             memcpy(hnm->processed, hnm->current, hnm->width * hnm->height);
428         } else {
429             decode_interframe_v4(avctx, avpkt->data + 8, avpkt->size - 8);
430             postprocess_current_frame(avctx);
431         }
432         copy_processed_frame(avctx, frame);
433         frame->pict_type = AV_PICTURE_TYPE_P;
434         frame->key_frame = 0;
435         memcpy(frame->data[1], hnm->palette, 256 * 4);
436         *got_frame = 1;
437         hnm_flip_buffers(hnm);
438     } else {
439         av_log(avctx, AV_LOG_ERROR, "invalid chunk id: %d\n", chunk_id);
440         return AVERROR_INVALIDDATA;
441     }
442
443     return avpkt->size;
444 }
445
446 static av_cold int hnm_decode_init(AVCodecContext *avctx)
447 {
448     Hnm4VideoContext *hnm = avctx->priv_data;
449
450     if (avctx->extradata_size < 1) {
451         av_log(avctx, AV_LOG_ERROR,
452                "Extradata missing, decoder requires version number\n");
453         return AVERROR_INVALIDDATA;
454     }
455
456     hnm->version   = avctx->extradata[0];
457     avctx->pix_fmt = AV_PIX_FMT_PAL8;
458     hnm->width     = avctx->width;
459     hnm->height    = avctx->height;
460     hnm->buffer1   = av_mallocz(avctx->width * avctx->height);
461     hnm->buffer2   = av_mallocz(avctx->width * avctx->height);
462     hnm->processed = av_mallocz(avctx->width * avctx->height);
463
464     if (   !hnm->buffer1 || !hnm->buffer2 || !hnm->processed
465         || avctx->width * avctx->height == 0
466         || avctx->height % 2) {
467         av_log(avctx, AV_LOG_ERROR, "av_mallocz() failed\n");
468         av_freep(&hnm->buffer1);
469         av_freep(&hnm->buffer2);
470         av_freep(&hnm->processed);
471         return AVERROR(ENOMEM);
472     }
473
474     hnm->current  = hnm->buffer1;
475     hnm->previous = hnm->buffer2;
476
477     return 0;
478 }
479
480 static av_cold int hnm_decode_end(AVCodecContext *avctx)
481 {
482     Hnm4VideoContext *hnm = avctx->priv_data;
483
484     av_freep(&hnm->buffer1);
485     av_freep(&hnm->buffer2);
486     av_freep(&hnm->processed);
487
488     return 0;
489 }
490
491 AVCodec ff_hnm4_video_decoder = {
492     .name           = "hnm4video",
493     .long_name      = NULL_IF_CONFIG_SMALL("HNM 4 video"),
494     .type           = AVMEDIA_TYPE_VIDEO,
495     .id             = AV_CODEC_ID_HNM4_VIDEO,
496     .priv_data_size = sizeof(Hnm4VideoContext),
497     .init           = hnm_decode_init,
498     .close          = hnm_decode_end,
499     .decode         = hnm_decode_frame,
500     .capabilities   = CODEC_CAP_DR1,
501 };