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