FFmpeg  4.3
smacker.c
Go to the documentation of this file.
1 /*
2  * Smacker decoder
3  * Copyright (c) 2006 Konstantin Shishkov
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * Smacker decoder
25  */
26 
27 /*
28  * Based on http://wiki.multimedia.cx/index.php?title=Smacker
29  */
30 
31 #include <stdio.h>
32 #include <stdlib.h>
33 
35 
36 #define BITSTREAM_READER_LE
37 #include "avcodec.h"
38 #include "bytestream.h"
39 #include "get_bits.h"
40 #include "internal.h"
41 #include "mathops.h"
42 
43 #define SMKTREE_BITS 9
44 #define SMK_NODE 0x80000000
45 
46 #define SMKTREE_DECODE_MAX_RECURSION 32
47 #define SMKTREE_DECODE_BIG_MAX_RECURSION 500
48 
49 typedef struct SmackVContext {
52 
54  int mmap_last[3], mclr_last[3], full_last[3], type_last[3];
56 
57 /**
58  * Context used for code reconstructing
59  */
60 typedef struct HuffContext {
61  int length;
62  int maxlength;
63  int current;
64  uint32_t *bits;
65  int *lengths;
66  int *values;
67 } HuffContext;
68 
69 /* common parameters used for decode_bigtree */
70 typedef struct DBCtx {
71  VLC *v1, *v2;
72  int *recode1, *recode2;
73  int escapes[3];
74  int *last;
75  int lcur;
76 } DBCtx;
77 
78 /* possible runs of blocks */
79 static const int block_runs[64] = {
80  1, 2, 3, 4, 5, 6, 7, 8,
81  9, 10, 11, 12, 13, 14, 15, 16,
82  17, 18, 19, 20, 21, 22, 23, 24,
83  25, 26, 27, 28, 29, 30, 31, 32,
84  33, 34, 35, 36, 37, 38, 39, 40,
85  41, 42, 43, 44, 45, 46, 47, 48,
86  49, 50, 51, 52, 53, 54, 55, 56,
87  57, 58, 59, 128, 256, 512, 1024, 2048 };
88 
93  SMK_BLK_FILL = 3 };
94 
95 /**
96  * Decode local frame tree
97  */
98 static int smacker_decode_tree(GetBitContext *gb, HuffContext *hc, uint32_t prefix, int length)
99 {
100  if (length > SMKTREE_DECODE_MAX_RECURSION || length > 3 * SMKTREE_BITS) {
101  av_log(NULL, AV_LOG_ERROR, "Maximum tree recursion level exceeded.\n");
102  return AVERROR_INVALIDDATA;
103  }
104 
105  if(!get_bits1(gb)){ //Leaf
106  if(hc->current >= hc->length){
107  av_log(NULL, AV_LOG_ERROR, "Tree size exceeded!\n");
108  return AVERROR_INVALIDDATA;
109  }
110  if(length){
111  hc->bits[hc->current] = prefix;
112  hc->lengths[hc->current] = length;
113  } else {
114  hc->bits[hc->current] = 0;
115  hc->lengths[hc->current] = 0;
116  }
117  hc->values[hc->current] = get_bits(gb, 8);
118  hc->current++;
119  if(hc->maxlength < length)
120  hc->maxlength = length;
121  return 0;
122  } else { //Node
123  int r;
124  length++;
125  r = smacker_decode_tree(gb, hc, prefix, length);
126  if(r)
127  return r;
128  return smacker_decode_tree(gb, hc, prefix | (1 << (length - 1)), length);
129  }
130 }
131 
132 /**
133  * Decode header tree
134  */
136  DBCtx *ctx, int length)
137 {
138  // Larger length can cause segmentation faults due to too deep recursion.
139  if (length > SMKTREE_DECODE_BIG_MAX_RECURSION) {
140  av_log(NULL, AV_LOG_ERROR, "Maximum bigtree recursion level exceeded.\n");
141  return AVERROR_INVALIDDATA;
142  }
143 
144  if (hc->current + 1 >= hc->length) {
145  av_log(NULL, AV_LOG_ERROR, "Tree size exceeded!\n");
146  return AVERROR_INVALIDDATA;
147  }
148  if(!get_bits1(gb)){ //Leaf
149  int val, i1, i2;
150  i1 = ctx->v1->table ? get_vlc2(gb, ctx->v1->table, SMKTREE_BITS, 3) : 0;
151  i2 = ctx->v2->table ? get_vlc2(gb, ctx->v2->table, SMKTREE_BITS, 3) : 0;
152  if (i1 < 0 || i2 < 0)
153  return AVERROR_INVALIDDATA;
154  val = ctx->recode1[i1] | (ctx->recode2[i2] << 8);
155  if(val == ctx->escapes[0]) {
156  ctx->last[0] = hc->current;
157  val = 0;
158  } else if(val == ctx->escapes[1]) {
159  ctx->last[1] = hc->current;
160  val = 0;
161  } else if(val == ctx->escapes[2]) {
162  ctx->last[2] = hc->current;
163  val = 0;
164  }
165 
166  hc->values[hc->current++] = val;
167  return 1;
168  } else { //Node
169  int r = 0, r_new, t;
170 
171  t = hc->current++;
172  r = smacker_decode_bigtree(gb, hc, ctx, length + 1);
173  if(r < 0)
174  return r;
175  hc->values[t] = SMK_NODE | r;
176  r++;
177  r_new = smacker_decode_bigtree(gb, hc, ctx, length + 1);
178  if (r_new < 0)
179  return r_new;
180  return r + r_new;
181  }
182 }
183 
184 /**
185  * Store large tree as FFmpeg's vlc codes
186  */
187 static int smacker_decode_header_tree(SmackVContext *smk, GetBitContext *gb, int **recodes, int *last, int size)
188 {
189  int res;
190  HuffContext huff;
191  HuffContext tmp1, tmp2;
192  VLC vlc[2] = { { 0 } };
193  int escapes[3];
194  DBCtx ctx;
195  int err = 0;
196 
197  if(size >= UINT_MAX>>4){ // (((size + 3) >> 2) + 3) << 2 must not overflow
198  av_log(smk->avctx, AV_LOG_ERROR, "size too large\n");
199  return AVERROR_INVALIDDATA;
200  }
201 
202  tmp1.length = 256;
203  tmp1.maxlength = 0;
204  tmp1.current = 0;
205  tmp1.bits = av_mallocz(256 * 4);
206  tmp1.lengths = av_mallocz(256 * sizeof(int));
207  tmp1.values = av_mallocz(256 * sizeof(int));
208 
209  tmp2.length = 256;
210  tmp2.maxlength = 0;
211  tmp2.current = 0;
212  tmp2.bits = av_mallocz(256 * 4);
213  tmp2.lengths = av_mallocz(256 * sizeof(int));
214  tmp2.values = av_mallocz(256 * sizeof(int));
215  if (!tmp1.bits || !tmp1.lengths || !tmp1.values ||
216  !tmp2.bits || !tmp2.lengths || !tmp2.values) {
217  err = AVERROR(ENOMEM);
218  goto error;
219  }
220 
221  if(get_bits1(gb)) {
222  res = smacker_decode_tree(gb, &tmp1, 0, 0);
223  if (res < 0) {
224  err = res;
225  goto error;
226  }
227  skip_bits1(gb);
228  if(tmp1.current > 1) {
229  res = init_vlc(&vlc[0], SMKTREE_BITS, tmp1.length,
230  tmp1.lengths, sizeof(int), sizeof(int),
231  tmp1.bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
232  if(res < 0) {
233  av_log(smk->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
234  err = res;
235  goto error;
236  }
237  }
238  }
239  if (!vlc[0].table) {
240  av_log(smk->avctx, AV_LOG_ERROR, "Skipping low bytes tree\n");
241  }
242  if(get_bits1(gb)){
243  res = smacker_decode_tree(gb, &tmp2, 0, 0);
244  if (res < 0) {
245  err = res;
246  goto error;
247  }
248  skip_bits1(gb);
249  if(tmp2.current > 1) {
250  res = init_vlc(&vlc[1], SMKTREE_BITS, tmp2.length,
251  tmp2.lengths, sizeof(int), sizeof(int),
252  tmp2.bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
253  if(res < 0) {
254  av_log(smk->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
255  err = res;
256  goto error;
257  }
258  }
259  }
260  if (!vlc[1].table) {
261  av_log(smk->avctx, AV_LOG_ERROR, "Skipping high bytes tree\n");
262  }
263 
264  escapes[0] = get_bits(gb, 16);
265  escapes[1] = get_bits(gb, 16);
266  escapes[2] = get_bits(gb, 16);
267 
268  last[0] = last[1] = last[2] = -1;
269 
270  ctx.escapes[0] = escapes[0];
271  ctx.escapes[1] = escapes[1];
272  ctx.escapes[2] = escapes[2];
273  ctx.v1 = &vlc[0];
274  ctx.v2 = &vlc[1];
275  ctx.recode1 = tmp1.values;
276  ctx.recode2 = tmp2.values;
277  ctx.last = last;
278 
279  huff.length = ((size + 3) >> 2) + 4;
280  huff.maxlength = 0;
281  huff.current = 0;
282  huff.values = av_mallocz_array(huff.length, sizeof(int));
283  if (!huff.values) {
284  err = AVERROR(ENOMEM);
285  goto error;
286  }
287 
288  res = smacker_decode_bigtree(gb, &huff, &ctx, 0);
289  if (res < 0)
290  err = res;
291  skip_bits1(gb);
292  if(ctx.last[0] == -1) ctx.last[0] = huff.current++;
293  if(ctx.last[1] == -1) ctx.last[1] = huff.current++;
294  if(ctx.last[2] == -1) ctx.last[2] = huff.current++;
295  if (ctx.last[0] >= huff.length ||
296  ctx.last[1] >= huff.length ||
297  ctx.last[2] >= huff.length) {
298  av_log(smk->avctx, AV_LOG_ERROR, "Huffman codes out of range\n");
299  err = AVERROR_INVALIDDATA;
300  }
301 
302  *recodes = huff.values;
303 
304 error:
305  if(vlc[0].table)
306  ff_free_vlc(&vlc[0]);
307  if(vlc[1].table)
308  ff_free_vlc(&vlc[1]);
309  av_free(tmp1.bits);
310  av_free(tmp1.lengths);
311  av_free(tmp1.values);
312  av_free(tmp2.bits);
313  av_free(tmp2.lengths);
314  av_free(tmp2.values);
315 
316  return err;
317 }
318 
320  GetBitContext gb;
321  int mmap_size, mclr_size, full_size, type_size, ret;
322  int skip = 0;
323 
324  mmap_size = AV_RL32(smk->avctx->extradata);
325  mclr_size = AV_RL32(smk->avctx->extradata + 4);
326  full_size = AV_RL32(smk->avctx->extradata + 8);
327  type_size = AV_RL32(smk->avctx->extradata + 12);
328 
329  ret = init_get_bits8(&gb, smk->avctx->extradata + 16, smk->avctx->extradata_size - 16);
330  if (ret < 0)
331  return ret;
332 
333  if(!get_bits1(&gb)) {
334  skip ++;
335  av_log(smk->avctx, AV_LOG_INFO, "Skipping MMAP tree\n");
336  smk->mmap_tbl = av_malloc(sizeof(int) * 2);
337  if (!smk->mmap_tbl)
338  return AVERROR(ENOMEM);
339  smk->mmap_tbl[0] = 0;
340  smk->mmap_last[0] = smk->mmap_last[1] = smk->mmap_last[2] = 1;
341  } else {
342  ret = smacker_decode_header_tree(smk, &gb, &smk->mmap_tbl, smk->mmap_last, mmap_size);
343  if (ret < 0)
344  return ret;
345  }
346  if(!get_bits1(&gb)) {
347  skip ++;
348  av_log(smk->avctx, AV_LOG_INFO, "Skipping MCLR tree\n");
349  smk->mclr_tbl = av_malloc(sizeof(int) * 2);
350  if (!smk->mclr_tbl)
351  return AVERROR(ENOMEM);
352  smk->mclr_tbl[0] = 0;
353  smk->mclr_last[0] = smk->mclr_last[1] = smk->mclr_last[2] = 1;
354  } else {
355  ret = smacker_decode_header_tree(smk, &gb, &smk->mclr_tbl, smk->mclr_last, mclr_size);
356  if (ret < 0)
357  return ret;
358  }
359  if(!get_bits1(&gb)) {
360  skip ++;
361  av_log(smk->avctx, AV_LOG_INFO, "Skipping FULL tree\n");
362  smk->full_tbl = av_malloc(sizeof(int) * 2);
363  if (!smk->full_tbl)
364  return AVERROR(ENOMEM);
365  smk->full_tbl[0] = 0;
366  smk->full_last[0] = smk->full_last[1] = smk->full_last[2] = 1;
367  } else {
368  ret = smacker_decode_header_tree(smk, &gb, &smk->full_tbl, smk->full_last, full_size);
369  if (ret < 0)
370  return ret;
371  }
372  if(!get_bits1(&gb)) {
373  skip ++;
374  av_log(smk->avctx, AV_LOG_INFO, "Skipping TYPE tree\n");
375  smk->type_tbl = av_malloc(sizeof(int) * 2);
376  if (!smk->type_tbl)
377  return AVERROR(ENOMEM);
378  smk->type_tbl[0] = 0;
379  smk->type_last[0] = smk->type_last[1] = smk->type_last[2] = 1;
380  } else {
381  ret = smacker_decode_header_tree(smk, &gb, &smk->type_tbl, smk->type_last, type_size);
382  if (ret < 0)
383  return ret;
384  }
385  if (skip == 4)
386  return AVERROR_INVALIDDATA;
387 
388  return 0;
389 }
390 
391 static av_always_inline void last_reset(int *recode, int *last) {
392  recode[last[0]] = recode[last[1]] = recode[last[2]] = 0;
393 }
394 
395 /* get code and update history */
396 static av_always_inline int smk_get_code(GetBitContext *gb, int *recode, int *last) {
397  register int *table = recode;
398  int v;
399 
400  while(*table & SMK_NODE) {
401  if (get_bits_left(gb) < 1)
402  return AVERROR_INVALIDDATA;
403  if(get_bits1(gb))
404  table += (*table) & (~SMK_NODE);
405  table++;
406  }
407  v = *table;
408 
409  if(v != recode[last[0]]) {
410  recode[last[2]] = recode[last[1]];
411  recode[last[1]] = recode[last[0]];
412  recode[last[0]] = v;
413  }
414  return v;
415 }
416 
417 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
418  AVPacket *avpkt)
419 {
420  SmackVContext * const smk = avctx->priv_data;
421  uint8_t *out;
422  uint32_t *pal;
423  GetByteContext gb2;
424  GetBitContext gb;
425  int blocks, blk, bw, bh;
426  int i, ret;
427  int stride;
428  int flags;
429 
430  if (avpkt->size <= 769)
431  return AVERROR_INVALIDDATA;
432 
433  if ((ret = ff_reget_buffer(avctx, smk->pic, 0)) < 0)
434  return ret;
435 
436  /* make the palette available on the way out */
437  pal = (uint32_t*)smk->pic->data[1];
438  bytestream2_init(&gb2, avpkt->data, avpkt->size);
439  flags = bytestream2_get_byteu(&gb2);
440  smk->pic->palette_has_changed = flags & 1;
441  smk->pic->key_frame = !!(flags & 2);
442  if (smk->pic->key_frame)
444  else
446 
447  for(i = 0; i < 256; i++)
448  *pal++ = 0xFFU << 24 | bytestream2_get_be24u(&gb2);
449 
450  last_reset(smk->mmap_tbl, smk->mmap_last);
451  last_reset(smk->mclr_tbl, smk->mclr_last);
452  last_reset(smk->full_tbl, smk->full_last);
453  last_reset(smk->type_tbl, smk->type_last);
454  if ((ret = init_get_bits8(&gb, avpkt->data + 769, avpkt->size - 769)) < 0)
455  return ret;
456 
457  blk = 0;
458  bw = avctx->width >> 2;
459  bh = avctx->height >> 2;
460  blocks = bw * bh;
461  stride = smk->pic->linesize[0];
462  while(blk < blocks) {
463  int type, run, mode;
464  uint16_t pix;
465 
466  type = smk_get_code(&gb, smk->type_tbl, smk->type_last);
467  if (type < 0)
468  return type;
469  run = block_runs[(type >> 2) & 0x3F];
470  switch(type & 3){
471  case SMK_BLK_MONO:
472  while(run-- && blk < blocks){
473  int clr, map;
474  int hi, lo;
475  clr = smk_get_code(&gb, smk->mclr_tbl, smk->mclr_last);
476  map = smk_get_code(&gb, smk->mmap_tbl, smk->mmap_last);
477  out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
478  hi = clr >> 8;
479  lo = clr & 0xFF;
480  for(i = 0; i < 4; i++) {
481  if(map & 1) out[0] = hi; else out[0] = lo;
482  if(map & 2) out[1] = hi; else out[1] = lo;
483  if(map & 4) out[2] = hi; else out[2] = lo;
484  if(map & 8) out[3] = hi; else out[3] = lo;
485  map >>= 4;
486  out += stride;
487  }
488  blk++;
489  }
490  break;
491  case SMK_BLK_FULL:
492  mode = 0;
493  if(avctx->codec_tag == MKTAG('S', 'M', 'K', '4')) { // In case of Smacker v4 we have three modes
494  if(get_bits1(&gb)) mode = 1;
495  else if(get_bits1(&gb)) mode = 2;
496  }
497  while(run-- && blk < blocks){
498  out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
499  switch(mode){
500  case 0:
501  for(i = 0; i < 4; i++) {
502  pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
503  AV_WL16(out+2,pix);
504  pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
505  AV_WL16(out,pix);
506  out += stride;
507  }
508  break;
509  case 1:
510  pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
511  out[0] = out[1] = pix & 0xFF;
512  out[2] = out[3] = pix >> 8;
513  out += stride;
514  out[0] = out[1] = pix & 0xFF;
515  out[2] = out[3] = pix >> 8;
516  out += stride;
517  pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
518  out[0] = out[1] = pix & 0xFF;
519  out[2] = out[3] = pix >> 8;
520  out += stride;
521  out[0] = out[1] = pix & 0xFF;
522  out[2] = out[3] = pix >> 8;
523  break;
524  case 2:
525  for(i = 0; i < 2; i++) {
526  uint16_t pix1, pix2;
527  pix2 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
528  pix1 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
529  AV_WL16(out,pix1);
530  AV_WL16(out+2,pix2);
531  out += stride;
532  AV_WL16(out,pix1);
533  AV_WL16(out+2,pix2);
534  out += stride;
535  }
536  break;
537  }
538  blk++;
539  }
540  break;
541  case SMK_BLK_SKIP:
542  while(run-- && blk < blocks)
543  blk++;
544  break;
545  case SMK_BLK_FILL:
546  mode = type >> 8;
547  while(run-- && blk < blocks){
548  uint32_t col;
549  out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
550  col = mode * 0x01010101U;
551  for(i = 0; i < 4; i++) {
552  *((uint32_t*)out) = col;
553  out += stride;
554  }
555  blk++;
556  }
557  break;
558  }
559 
560  }
561 
562  if ((ret = av_frame_ref(data, smk->pic)) < 0)
563  return ret;
564 
565  *got_frame = 1;
566 
567  /* always report that the buffer was completely consumed */
568  return avpkt->size;
569 }
570 
571 
573 {
574  SmackVContext * const smk = avctx->priv_data;
575 
576  av_freep(&smk->mmap_tbl);
577  av_freep(&smk->mclr_tbl);
578  av_freep(&smk->full_tbl);
579  av_freep(&smk->type_tbl);
580 
581  av_frame_free(&smk->pic);
582 
583  return 0;
584 }
585 
586 
588 {
589  SmackVContext * const c = avctx->priv_data;
590  int ret;
591 
592  c->avctx = avctx;
593 
594  avctx->pix_fmt = AV_PIX_FMT_PAL8;
595 
596  c->pic = av_frame_alloc();
597  if (!c->pic)
598  return AVERROR(ENOMEM);
599 
600  /* decode huffman trees from extradata */
601  if(avctx->extradata_size < 16){
602  av_log(avctx, AV_LOG_ERROR, "Extradata missing!\n");
603  decode_end(avctx);
604  return AVERROR(EINVAL);
605  }
606 
608  if (ret < 0) {
609  decode_end(avctx);
610  return ret;
611  }
612 
613  return 0;
614 }
615 
616 
618 {
619  if (avctx->channels < 1 || avctx->channels > 2) {
620  av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
621  return AVERROR_INVALIDDATA;
622  }
625 
626  return 0;
627 }
628 
629 /**
630  * Decode Smacker audio data
631  */
632 static int smka_decode_frame(AVCodecContext *avctx, void *data,
633  int *got_frame_ptr, AVPacket *avpkt)
634 {
635  AVFrame *frame = data;
636  const uint8_t *buf = avpkt->data;
637  int buf_size = avpkt->size;
638  GetBitContext gb;
639  HuffContext h[4] = { { 0 } };
640  VLC vlc[4] = { { 0 } };
641  int16_t *samples;
642  uint8_t *samples8;
643  int val;
644  int i, res, ret;
645  int unp_size;
646  int bits, stereo;
647  int pred[2] = {0, 0};
648 
649  if (buf_size <= 4) {
650  av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
651  return AVERROR_INVALIDDATA;
652  }
653 
654  unp_size = AV_RL32(buf);
655 
656  if (unp_size > (1U<<24)) {
657  av_log(avctx, AV_LOG_ERROR, "packet is too big\n");
658  return AVERROR_INVALIDDATA;
659  }
660 
661  if ((ret = init_get_bits8(&gb, buf + 4, buf_size - 4)) < 0)
662  return ret;
663 
664  if(!get_bits1(&gb)){
665  av_log(avctx, AV_LOG_INFO, "Sound: no data\n");
666  *got_frame_ptr = 0;
667  return 1;
668  }
669  stereo = get_bits1(&gb);
670  bits = get_bits1(&gb);
671  if (stereo ^ (avctx->channels != 1)) {
672  av_log(avctx, AV_LOG_ERROR, "channels mismatch\n");
673  return AVERROR_INVALIDDATA;
674  }
675  if (bits == (avctx->sample_fmt == AV_SAMPLE_FMT_U8)) {
676  av_log(avctx, AV_LOG_ERROR, "sample format mismatch\n");
677  return AVERROR_INVALIDDATA;
678  }
679 
680  /* get output buffer */
681  frame->nb_samples = unp_size / (avctx->channels * (bits + 1));
682  if (unp_size % (avctx->channels * (bits + 1))) {
683  av_log(avctx, AV_LOG_ERROR,
684  "The buffer does not contain an integer number of samples\n");
685  return AVERROR_INVALIDDATA;
686  }
687  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
688  return ret;
689  samples = (int16_t *)frame->data[0];
690  samples8 = frame->data[0];
691 
692  // Initialize
693  for(i = 0; i < (1 << (bits + stereo)); i++) {
694  h[i].length = 256;
695  h[i].maxlength = 0;
696  h[i].current = 0;
697  h[i].bits = av_mallocz(256 * 4);
698  h[i].lengths = av_mallocz(256 * sizeof(int));
699  h[i].values = av_mallocz(256 * sizeof(int));
700  if (!h[i].bits || !h[i].lengths || !h[i].values) {
701  ret = AVERROR(ENOMEM);
702  goto error;
703  }
704  skip_bits1(&gb);
705  if (smacker_decode_tree(&gb, &h[i], 0, 0) < 0) {
707  goto error;
708  }
709  skip_bits1(&gb);
710  if(h[i].current > 1) {
711  res = init_vlc(&vlc[i], SMKTREE_BITS, h[i].length,
712  h[i].lengths, sizeof(int), sizeof(int),
713  h[i].bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
714  if(res < 0) {
715  av_log(avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
717  goto error;
718  }
719  }
720  }
721  /* this codec relies on wraparound instead of clipping audio */
722  if(bits) { //decode 16-bit data
723  for(i = stereo; i >= 0; i--)
724  pred[i] = sign_extend(av_bswap16(get_bits(&gb, 16)), 16);
725  for(i = 0; i <= stereo; i++)
726  *samples++ = pred[i];
727  for(; i < unp_size / 2; i++) {
728  if (get_bits_left(&gb) < 0) {
730  goto error;
731  }
732  if(i & stereo) {
733  if(vlc[2].table)
734  res = get_vlc2(&gb, vlc[2].table, SMKTREE_BITS, 3);
735  else
736  res = 0;
737  if (res < 0) {
738  av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
740  goto error;
741  }
742  val = h[2].values[res];
743  if(vlc[3].table)
744  res = get_vlc2(&gb, vlc[3].table, SMKTREE_BITS, 3);
745  else
746  res = 0;
747  if (res < 0) {
748  av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
750  goto error;
751  }
752  val |= h[3].values[res] << 8;
753  pred[1] += (unsigned)sign_extend(val, 16);
754  *samples++ = pred[1];
755  } else {
756  if(vlc[0].table)
757  res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
758  else
759  res = 0;
760  if (res < 0) {
761  av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
763  goto error;
764  }
765  val = h[0].values[res];
766  if(vlc[1].table)
767  res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
768  else
769  res = 0;
770  if (res < 0) {
771  av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
773  goto error;
774  }
775  val |= h[1].values[res] << 8;
776  pred[0] += (unsigned)sign_extend(val, 16);
777  *samples++ = pred[0];
778  }
779  }
780  } else { //8-bit data
781  for(i = stereo; i >= 0; i--)
782  pred[i] = get_bits(&gb, 8);
783  for(i = 0; i <= stereo; i++)
784  *samples8++ = pred[i];
785  for(; i < unp_size; i++) {
786  if (get_bits_left(&gb) < 0) {
788  goto error;
789  }
790  if(i & stereo){
791  if(vlc[1].table)
792  res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
793  else
794  res = 0;
795  if (res < 0) {
796  av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
798  goto error;
799  }
800  pred[1] += sign_extend(h[1].values[res], 8);
801  *samples8++ = pred[1];
802  } else {
803  if(vlc[0].table)
804  res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
805  else
806  res = 0;
807  if (res < 0) {
808  av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
810  goto error;
811  }
812  pred[0] += sign_extend(h[0].values[res], 8);
813  *samples8++ = pred[0];
814  }
815  }
816  }
817 
818  *got_frame_ptr = 1;
819  ret = buf_size;
820 
821 error:
822  for(i = 0; i < 4; i++) {
823  if(vlc[i].table)
824  ff_free_vlc(&vlc[i]);
825  av_free(h[i].bits);
826  av_free(h[i].lengths);
827  av_free(h[i].values);
828  }
829 
830  return ret;
831 }
832 
834  .name = "smackvid",
835  .long_name = NULL_IF_CONFIG_SMALL("Smacker video"),
836  .type = AVMEDIA_TYPE_VIDEO,
838  .priv_data_size = sizeof(SmackVContext),
839  .init = decode_init,
840  .close = decode_end,
841  .decode = decode_frame,
842  .capabilities = AV_CODEC_CAP_DR1,
843 };
844 
846  .name = "smackaud",
847  .long_name = NULL_IF_CONFIG_SMALL("Smacker audio"),
848  .type = AVMEDIA_TYPE_AUDIO,
850  .init = smka_decode_init,
851  .decode = smka_decode_frame,
852  .capabilities = AV_CODEC_CAP_DR1,
853 };
error
static void error(const char *err)
Definition: target_bsf_fuzzer.c:29
AVCodec
AVCodec.
Definition: codec.h:190
stride
int stride
Definition: mace.c:144
init
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
get_bits_left
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:849
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
SMK_BLK_FULL
@ SMK_BLK_FULL
Definition: smacker.c:91
AVCodecContext::channel_layout
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:1237
HuffContext
Context used for code reconstructing.
Definition: smacker.c:60
HuffContext::maxlength
int maxlength
Definition: smacker.c:62
out
FILE * out
Definition: movenc.c:54
GetByteContext
Definition: bytestream.h:33
SmackVContext::full_last
int full_last[3]
Definition: smacker.c:54
MKTAG
#define MKTAG(a, b, c, d)
Definition: common.h:406
AV_CH_LAYOUT_MONO
#define AV_CH_LAYOUT_MONO
Definition: channel_layout.h:85
SMK_BLK_SKIP
@ SMK_BLK_SKIP
Definition: smacker.c:92
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:203
block_runs
static const int block_runs[64]
Definition: smacker.c:79
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:300
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:355
init_vlc
#define init_vlc(vlc, nb_bits, nb_codes, bits, bits_wrap, bits_size, codes, codes_wrap, codes_size, flags)
Definition: vlc.h:38
table
static const uint16_t table[]
Definition: prosumer.c:206
data
const char data[16]
Definition: mxf.c:91
av_mallocz_array
void * av_mallocz_array(size_t nmemb, size_t size)
Allocate a memory block for an array with av_mallocz().
Definition: mem.c:190
get_vlc2
static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE(*table)[2], int bits, int max_depth)
Parse a vlc code.
Definition: get_bits.h:797
samples
FFmpeg Automated Testing Environment ************************************Introduction Using FATE from your FFmpeg source directory Submitting the results to the FFmpeg result aggregation server Uploading new samples to the fate suite FATE makefile targets and variables Makefile targets Makefile variables Examples Introduction **************FATE is an extended regression suite on the client side and a means for results aggregation and presentation on the server side The first part of this document explains how you can use FATE from your FFmpeg source directory to test your ffmpeg binary The second part describes how you can run FATE to submit the results to FFmpeg’s FATE server In any way you can have a look at the publicly viewable FATE results by visiting this as it can be seen if some test on some platform broke with their recent contribution This usually happens on the platforms the developers could not test on The second part of this document describes how you can run FATE to submit your results to FFmpeg’s FATE server If you want to submit your results be sure to check that your combination of OS and compiler is not already listed on the above mentioned website In the third part you can find a comprehensive listing of FATE makefile targets and variables Using FATE from your FFmpeg source directory **********************************************If you want to run FATE on your machine you need to have the samples in place You can get the samples via the build target fate rsync Use this command from the top level source this will cause FATE to fail NOTE To use a custom wrapper to run the pass ‘ target exec’ to ‘configure’ or set the TARGET_EXEC Make variable Submitting the results to the FFmpeg result aggregation server ****************************************************************To submit your results to the server you should run fate through the shell script ‘tests fate sh’ from the FFmpeg sources This script needs to be invoked with a configuration file as its first argument tests fate sh path to fate_config A configuration file template with comments describing the individual configuration variables can be found at ‘doc fate_config sh template’ Create a configuration that suits your based on the configuration template The ‘slot’ configuration variable can be any string that is not yet but it is suggested that you name it adhering to the following pattern ‘ARCH OS COMPILER COMPILER VERSION’ The configuration file itself will be sourced in a shell therefore all shell features may be used This enables you to setup the environment as you need it for your build For your first test runs the ‘fate_recv’ variable should be empty or commented out This will run everything as normal except that it will omit the submission of the results to the server The following files should be present in $workdir as specified in the configuration it may help to try out the ‘ssh’ command with one or more ‘ v’ options You should get detailed output concerning your SSH configuration and the authentication process The only thing left is to automate the execution of the fate sh script and the synchronisation of the samples directory Uploading new samples to the fate suite *****************************************If you need a sample uploaded send a mail to samples request This is for developers who have an account on the fate suite server If you upload new samples
Definition: fate.txt:139
INIT_VLC_LE
#define INIT_VLC_LE
Definition: vlc.h:54
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:314
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:31
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:379
U
#define U(x)
Definition: vp56_arith.h:37
SmackVContext
Definition: smacker.c:49
GetBitContext
Definition: get_bits.h:61
AV_CODEC_ID_SMACKAUDIO
@ AV_CODEC_ID_SMACKAUDIO
Definition: codec_id.h:433
SMKTREE_DECODE_BIG_MAX_RECURSION
#define SMKTREE_DECODE_BIG_MAX_RECURSION
Definition: smacker.c:47
AVFrame::key_frame
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:378
val
static double val(void *priv, double ch)
Definition: aeval.c:76
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
DBCtx::recode2
int * recode2
Definition: smacker.c:72
AV_CH_LAYOUT_STEREO
#define AV_CH_LAYOUT_STEREO
Definition: channel_layout.h:86
SmackVContext::mclr_last
int mclr_last[3]
Definition: smacker.c:54
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:190
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
av_cold
#define av_cold
Definition: attributes.h:90
init_get_bits8
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:677
decode_init
static av_cold int decode_init(AVCodecContext *avctx)
Definition: smacker.c:587
ff_smacker_decoder
AVCodec ff_smacker_decoder
Definition: smacker.c:833
decode
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:71
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:628
DBCtx::recode1
int * recode1
Definition: smacker.c:72
smka_decode_init
static av_cold int smka_decode_init(AVCodecContext *avctx)
Definition: smacker.c:617
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
bits
uint8_t bits
Definition: vp3data.h:202
ctx
AVFormatContext * ctx
Definition: movenc.c:48
get_bits.h
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:237
smka_decode_frame
static int smka_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
Decode Smacker audio data.
Definition: smacker.c:632
ff_free_vlc
void ff_free_vlc(VLC *vlc)
Definition: bitstream.c:359
blk
#define blk(i)
Definition: sha.c:185
SmackVContext::type_last
int type_last[3]
Definition: smacker.c:54
DBCtx::escapes
int escapes[3]
Definition: smacker.c:73
NULL
#define NULL
Definition: coverity.c:32
smacker_decode_tree
static int smacker_decode_tree(GetBitContext *gb, HuffContext *hc, uint32_t prefix, int length)
Decode local frame tree.
Definition: smacker.c:98
run
uint8_t run
Definition: svq3.c:208
HuffContext::length
int length
Definition: smacker.c:61
SmkBlockTypes
SmkBlockTypes
Definition: smacker.c:89
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:498
mathops.h
ff_smackaud_decoder
AVCodec ff_smackaud_decoder
Definition: smacker.c:845
AV_SAMPLE_FMT_U8
AV_SAMPLE_FMT_U8
Definition: audio_convert.c:194
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
SMKTREE_DECODE_MAX_RECURSION
#define SMKTREE_DECODE_MAX_RECURSION
Definition: smacker.c:46
smacker_decode_header_tree
static int smacker_decode_header_tree(SmackVContext *smk, GetBitContext *gb, int **recodes, int *last, int size)
Store large tree as FFmpeg's vlc codes.
Definition: smacker.c:187
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1854
AVFrame::pict_type
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:383
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:50
HuffContext::values
int * values
Definition: smacker.c:66
AVPacket::size
int size
Definition: packet.h:356
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:186
av_frame_ref
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition: frame.c:444
decode_end
static av_cold int decode_end(AVCodecContext *avctx)
Definition: smacker.c:572
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1194
size
int size
Definition: twinvq_data.h:11134
SMK_BLK_FILL
@ SMK_BLK_FILL
Definition: smacker.c:93
SmackVContext::avctx
AVCodecContext * avctx
Definition: smacker.c:50
DBCtx::v1
VLC * v1
Definition: smacker.c:71
AV_WL16
#define AV_WL16(p, v)
Definition: intreadwrite.h:412
av_bswap16
#define av_bswap16
Definition: bswap.h:31
HuffContext::bits
uint32_t * bits
Definition: smacker.c:64
skip_bits1
static void skip_bits1(GetBitContext *s)
Definition: get_bits.h:538
r
#define r
Definition: input.c:40
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
AVCodecContext::channels
int channels
number of audio channels
Definition: avcodec.h:1187
SmackVContext::mmap_tbl
int * mmap_tbl
Definition: smacker.c:53
AVCodecContext::bits_per_coded_sample
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:1750
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
AVCodecContext::extradata
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:627
av_always_inline
#define av_always_inline
Definition: attributes.h:49
uint8_t
uint8_t
Definition: audio_convert.c:194
AV_SAMPLE_FMT_S16
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:61
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:197
AVCodecContext::height
int height
Definition: avcodec.h:699
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:736
avcodec.h
AV_PIX_FMT_PAL8
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:77
AV_CODEC_ID_SMACKVIDEO
@ AV_CODEC_ID_SMACKVIDEO
Definition: codec_id.h:132
ff_reget_buffer
int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Identical in function to ff_get_buffer(), except it reuses the existing buffer if available.
Definition: decode.c:1960
ret
ret
Definition: filter_design.txt:187
pred
static const float pred[4]
Definition: siprdata.h:259
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
DBCtx::v2
VLC * v2
Definition: smacker.c:71
last_reset
static av_always_inline void last_reset(int *recode, int *last)
Definition: smacker.c:391
SmackVContext::type_tbl
int * type_tbl
Definition: smacker.c:53
AVCodecContext
main external API structure.
Definition: avcodec.h:526
SMKTREE_BITS
#define SMKTREE_BITS
Definition: smacker.c:43
SmackVContext::mclr_tbl
int * mclr_tbl
Definition: smacker.c:53
channel_layout.h
SmackVContext::mmap_last
int mmap_last[3]
Definition: smacker.c:54
AVFrame::palette_has_changed
int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: frame.h:457
AV_RL32
#define AV_RL32
Definition: intreadwrite.h:146
mode
mode
Definition: ebur128.h:83
VLC
Definition: vlc.h:26
SmackVContext::full_tbl
int * full_tbl
Definition: smacker.c:53
HuffContext::current
int current
Definition: smacker.c:63
SmackVContext::pic
AVFrame * pic
Definition: smacker.c:51
sign_extend
static av_const int sign_extend(int val, unsigned bits)
Definition: mathops.h:130
DBCtx::last
int * last
Definition: smacker.c:74
values
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return values
Definition: filter_design.txt:263
DBCtx
Definition: smacker.c:70
SMK_BLK_MONO
@ SMK_BLK_MONO
Definition: smacker.c:90
HuffContext::lengths
int * lengths
Definition: smacker.c:65
AV_PICTURE_TYPE_P
@ AV_PICTURE_TYPE_P
Predicted.
Definition: avutil.h:275
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
SMK_NODE
#define SMK_NODE
Definition: smacker.c:44
decode_frame
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: smacker.c:417
map
const VDPAUPixFmtMap * map
Definition: hwcontext_vdpau.c:85
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
AVCodecContext::codec_tag
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:551
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:553
AVPacket
This structure stores compressed data.
Definition: packet.h:332
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
decode_header_trees
static int decode_header_trees(SmackVContext *smk)
Definition: smacker.c:319
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:699
bytestream.h
bytestream2_init
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:133
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:564
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:331
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
h
h
Definition: vp9dsp_template.c:2038
DBCtx::lcur
int lcur
Definition: smacker.c:75
smacker_decode_bigtree
static int smacker_decode_bigtree(GetBitContext *gb, HuffContext *hc, DBCtx *ctx, int length)
Decode header tree.
Definition: smacker.c:135
smk_get_code
static av_always_inline int smk_get_code(GetBitContext *gb, int *recode, int *last)
Definition: smacker.c:396