FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
hnm4video.c
Go to the documentation of this file.
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 {
40  int width;
41  int height;
47  uint32_t palette[256];
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 
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;
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 
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 
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 void 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  break;
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  break;
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  break;
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  break;
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  break;
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  break;
215  }
216  if(backward) {
217  if (offset < (!!backline)*(2 * hnm->width - 1) + 2*(left-1)) {
218  av_log(avctx, AV_LOG_ERROR, "Attempting to read out of bounds\n");
219  break;
220  }
221  } else {
222  if (offset < (!!backline)*(2 * hnm->width - 1)) {
223  av_log(avctx, AV_LOG_ERROR, "Attempting to read out of bounds\n");
224  break;
225  }
226  }
227 
228  if (previous) {
229  while (left > 0) {
230  if (backline) {
231  hnm->current[writeoffset++] = hnm->previous[offset - (2 * hnm->width) + 1];
232  hnm->current[writeoffset++] = hnm->previous[offset++];
233  offset++;
234  } else {
235  hnm->current[writeoffset++] = hnm->previous[offset++];
236  hnm->current[writeoffset++] = hnm->previous[offset++];
237  }
238  if (backward)
239  offset -= 4;
240  left--;
241  }
242  } else {
243  while (left > 0) {
244  if (backline) {
245  hnm->current[writeoffset++] = hnm->current[offset - (2 * hnm->width) + 1];
246  hnm->current[writeoffset++] = hnm->current[offset++];
247  offset++;
248  } else {
249  hnm->current[writeoffset++] = hnm->current[offset++];
250  hnm->current[writeoffset++] = hnm->current[offset++];
251  }
252  if (backward)
253  offset -= 4;
254  left--;
255  }
256  }
257 
258  if (swap) {
259  left = count;
260  writeoffset -= count * 2;
261  while (left > 0) {
262  swap = hnm->current[writeoffset];
263  hnm->current[writeoffset] = hnm->current[writeoffset + 1];
264  hnm->current[writeoffset + 1] = swap;
265  left--;
266  writeoffset += 2;
267  }
268  }
269  }
270  }
271 }
272 
274  uint32_t size)
275 {
276  Hnm4VideoContext *hnm = avctx->priv_data;
277  GetByteContext gb;
278  uint32_t writeoffset = 0, offset;
279  uint8_t tag, count, previous, delta;
280 
281  bytestream2_init(&gb, src, size);
282 
283  while (bytestream2_tell(&gb) < size) {
284  count = bytestream2_peek_byte(&gb) & 0x3F;
285  if (count == 0) {
286  tag = bytestream2_get_byte(&gb) & 0xC0;
287  tag = tag >> 6;
288  if (tag == 0) {
289  writeoffset += bytestream2_get_byte(&gb);
290  } else if (tag == 1) {
291  if (writeoffset + hnm->width >= hnm->width * hnm->height) {
292  av_log(avctx, AV_LOG_ERROR, "writeoffset out of bounds\n");
293  break;
294  }
295  hnm->current[writeoffset] = bytestream2_get_byte(&gb);
296  hnm->current[writeoffset + hnm->width] = bytestream2_get_byte(&gb);
297  writeoffset++;
298  } else if (tag == 2) {
299  writeoffset += hnm->width;
300  } else if (tag == 3) {
301  break;
302  }
303  if (writeoffset > hnm->width * hnm->height) {
304  av_log(avctx, AV_LOG_ERROR, "writeoffset out of bounds\n");
305  break;
306  }
307  } else {
308  delta = bytestream2_peek_byte(&gb) & 0x80;
309  previous = bytestream2_peek_byte(&gb) & 0x40;
310  bytestream2_skip(&gb, 1);
311 
312  offset = writeoffset;
313  offset += bytestream2_get_le16(&gb);
314 
315  if (delta) {
316  if (offset < 0x10000) {
317  av_log(avctx, AV_LOG_ERROR, "Attempting to read out of bounds\n");
318  break;
319  }
320  offset -= 0x10000;
321  }
322 
323  if (offset + hnm->width + count >= hnm->width * hnm->height) {
324  av_log(avctx, AV_LOG_ERROR, "Attempting to read out of bounds\n");
325  break;
326  } else if (writeoffset + hnm->width + count >= hnm->width * hnm->height) {
327  av_log(avctx, AV_LOG_ERROR, "Attempting to write out of bounds\n");
328  break;
329  }
330 
331  if (previous) {
332  while (count > 0) {
333  hnm->current[writeoffset] = hnm->previous[offset];
334  hnm->current[writeoffset + hnm->width] = hnm->previous[offset + hnm->width];
335  writeoffset++;
336  offset++;
337  count--;
338  }
339  } else {
340  while (count > 0) {
341  hnm->current[writeoffset] = hnm->current[offset];
342  hnm->current[writeoffset + hnm->width] = hnm->current[offset + hnm->width];
343  writeoffset++;
344  offset++;
345  count--;
346  }
347  }
348  }
349  }
350 }
351 
353  uint32_t size)
354 {
355  Hnm4VideoContext *hnm = avctx->priv_data;
356  GetByteContext gb;
357  uint8_t start, writeoffset;
358  uint16_t count;
359  int eight_bit_colors;
360 
361  eight_bit_colors = src[7] & 0x80 && hnm->version == 0x4a;
362 
363  // skip first 8 bytes
364  bytestream2_init(&gb, src + 8, size - 8);
365 
366  while (bytestream2_tell(&gb) < size - 8) {
367  start = bytestream2_get_byte(&gb);
368  count = bytestream2_get_byte(&gb);
369  if (start == 255 && count == 255)
370  break;
371  if (count == 0)
372  count = 256;
373  writeoffset = start;
374  while (count > 0) {
375  hnm->palette[writeoffset] = bytestream2_get_be24(&gb);
376  if (!eight_bit_colors)
377  hnm->palette[writeoffset] <<= 2;
378  hnm->palette[writeoffset] |= (0xFFU << 24);
379  count--;
380  writeoffset++;
381  }
382  }
383 }
384 
386 {
387  uint8_t *temp;
388 
389  temp = hnm->current;
390  hnm->current = hnm->previous;
391  hnm->previous = temp;
392 }
393 
394 static int hnm_decode_frame(AVCodecContext *avctx, void *data,
395  int *got_frame, AVPacket *avpkt)
396 {
397  AVFrame *frame = data;
398  Hnm4VideoContext *hnm = avctx->priv_data;
399  int ret;
400  uint16_t chunk_id;
401 
402  if (avpkt->size < 8) {
403  av_log(avctx, AV_LOG_ERROR, "packet too small\n");
404  return AVERROR_INVALIDDATA;
405  }
406 
407  chunk_id = AV_RL16(avpkt->data + 4);
408 
409  if (chunk_id == HNM4_CHUNK_ID_PL) {
410  hnm_update_palette(avctx, avpkt->data, avpkt->size);
411  } else if (chunk_id == HNM4_CHUNK_ID_IZ) {
412  if (avpkt->size < 12) {
413  av_log(avctx, AV_LOG_ERROR, "packet too small\n");
414  return AVERROR_INVALIDDATA;
415  }
416  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
417  return ret;
418 
419  unpack_intraframe(avctx, avpkt->data + 12, avpkt->size - 12);
420  memcpy(hnm->previous, hnm->current, hnm->width * hnm->height);
421  if (hnm->version == 0x4a)
422  memcpy(hnm->processed, hnm->current, hnm->width * hnm->height);
423  else
425  copy_processed_frame(avctx, frame);
426  frame->pict_type = AV_PICTURE_TYPE_I;
427  frame->key_frame = 1;
428  memcpy(frame->data[1], hnm->palette, 256 * 4);
429  *got_frame = 1;
430  } else if (chunk_id == HNM4_CHUNK_ID_IU) {
431  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
432  return ret;
433 
434  if (hnm->version == 0x4a) {
435  decode_interframe_v4a(avctx, avpkt->data + 8, avpkt->size - 8);
436  memcpy(hnm->processed, hnm->current, hnm->width * hnm->height);
437  } else {
438  decode_interframe_v4(avctx, avpkt->data + 8, avpkt->size - 8);
440  }
441  copy_processed_frame(avctx, frame);
442  frame->pict_type = AV_PICTURE_TYPE_P;
443  frame->key_frame = 0;
444  memcpy(frame->data[1], hnm->palette, 256 * 4);
445  *got_frame = 1;
446  hnm_flip_buffers(hnm);
447  } else {
448  av_log(avctx, AV_LOG_ERROR, "invalid chunk id: %d\n", chunk_id);
449  return AVERROR_INVALIDDATA;
450  }
451 
452  return avpkt->size;
453 }
454 
456 {
457  Hnm4VideoContext *hnm = avctx->priv_data;
458  int ret;
459 
460  if (avctx->extradata_size < 1) {
461  av_log(avctx, AV_LOG_ERROR,
462  "Extradata missing, decoder requires version number\n");
463  return AVERROR_INVALIDDATA;
464  }
465 
466  ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
467  if (ret < 0)
468  return ret;
469 
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);
477 
478  if ( !hnm->buffer1 || !hnm->buffer2 || !hnm->processed
479  || avctx->width * avctx->height == 0
480  || avctx->height % 2) {
481  av_log(avctx, AV_LOG_ERROR, "av_mallocz() failed\n");
482  av_freep(&hnm->buffer1);
483  av_freep(&hnm->buffer2);
484  av_freep(&hnm->processed);
485  return AVERROR(ENOMEM);
486  }
487 
488  hnm->current = hnm->buffer1;
489  hnm->previous = hnm->buffer2;
490 
491  return 0;
492 }
493 
495 {
496  Hnm4VideoContext *hnm = avctx->priv_data;
497 
498  av_freep(&hnm->buffer1);
499  av_freep(&hnm->buffer2);
500  av_freep(&hnm->processed);
501 
502  return 0;
503 }
504 
506  .name = "hnm4video",
507  .long_name = NULL_IF_CONFIG_SMALL("HNM 4 video"),
508  .type = AVMEDIA_TYPE_VIDEO,
510  .priv_data_size = sizeof(Hnm4VideoContext),
512  .close = hnm_decode_end,
514  .capabilities = AV_CODEC_CAP_DR1,
515 };
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
This structure describes decoded (raw) audio or video data.
Definition: frame.h:226
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
misc image utilities
Memory handling functions.
else temp
Definition: vf_mcdeint.c:256
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
uint8_t * processed
Definition: hnm4video.c:46
int size
Definition: avcodec.h:1446
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1743
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:133
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:236
#define src
Definition: vp8dsp.c:254
AVCodec.
Definition: avcodec.h:3424
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:87
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:42
uint8_t * buffer2
Definition: hnm4video.c:45
#define HNM4_CHUNK_ID_IZ
Definition: hnm4video.c:34
uint8_t version
Definition: hnm4video.c:39
uint8_t
#define av_cold
Definition: attributes.h:82
float delta
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:77
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1634
static int getbit(GetByteContext *gb, uint32_t *bitbuf, int *bits)
Definition: hnm4video.c:50
static AVFrame * frame
uint8_t * data
Definition: avcodec.h:1445
uint32_t tag
Definition: movenc.c:1483
static void unpack_intraframe(AVCodecContext *avctx, uint8_t *src, uint32_t size)
Definition: hnm4video.c:66
ptrdiff_t size
Definition: opengl_enc.c:101
#define av_log(a,...)
static void copy_processed_frame(AVCodecContext *avctx, AVFrame *frame)
Definition: hnm4video.c:132
#define U(x)
Definition: vp56_arith.h:37
static void decode_interframe_v4a(AVCodecContext *avctx, uint8_t *src, uint32_t size)
Definition: hnm4video.c:273
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define AVERROR(e)
Definition: error.h:43
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:164
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
const char * name
Name of the codec implementation.
Definition: avcodec.h:3431
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
GLsizei count
Definition: opengl_enc.c:109
uint8_t * buffer1
Definition: hnm4video.c:44
common internal API header
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
Definition: imgutils.c:282
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:309
uint8_t * current
Definition: hnm4video.c:42
int width
picture width / height.
Definition: avcodec.h:1706
int32_t
uint8_t * previous
Definition: hnm4video.c:43
static av_cold int hnm_decode_end(AVCodecContext *avctx)
Definition: hnm4video.c:494
static av_always_inline int bytestream2_tell(GetByteContext *g)
Definition: bytestream.h:188
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:257
#define HNM4_CHUNK_ID_PL
Definition: hnm4video.c:33
main external API structure.
Definition: avcodec.h:1533
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1918
int extradata_size
Definition: avcodec.h:1635
#define HNM4_CHUNK_ID_IU
Definition: hnm4video.c:35
static void hnm_flip_buffers(Hnm4VideoContext *hnm)
Definition: hnm4video.c:385
static av_cold int hnm_decode_init(AVCodecContext *avctx)
Definition: hnm4video.c:455
static void decode_interframe_v4(AVCodecContext *avctx, uint8_t *src, uint32_t size)
Definition: hnm4video.c:146
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:240
common internal api header.
static int hnm_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: hnm4video.c:394
static void hnm_update_palette(AVCodecContext *avctx, uint8_t *src, uint32_t size)
Definition: hnm4video.c:352
void * priv_data
Definition: avcodec.h:1560
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:304
uint32_t palette[256]
Definition: hnm4video.c:47
static void postprocess_current_frame(AVCodecContext *avctx)
Definition: hnm4video.c:117
#define av_freep(p)
void INT64 start
Definition: avisynth_c.h:690
AVCodec ff_hnm4_video_decoder
Definition: hnm4video.c:505
This structure stores compressed data.
Definition: avcodec.h:1422
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:968
Predicted.
Definition: avutil.h:275