indeo3.c
Go to the documentation of this file.
1 /*
2  * Indeo Video v3 compatible decoder
3  * Copyright (c) 2009 - 2011 Maxim Poliakovski
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
32 #include "libavutil/imgutils.h"
33 #include "libavutil/intreadwrite.h"
34 #include "avcodec.h"
35 #include "dsputil.h"
36 #include "bytestream.h"
37 #include "get_bits.h"
38 
39 #include "indeo3data.h"
40 
41 /* RLE opcodes. */
42 enum {
43  RLE_ESC_F9 = 249,
44  RLE_ESC_FA = 250,
45  RLE_ESC_FB = 251,
46  RLE_ESC_FC = 252,
47  RLE_ESC_FD = 253,
48  RLE_ESC_FE = 254,
49  RLE_ESC_FF = 255
50 };
51 
52 
53 /* Some constants for parsing frame bitstream flags. */
54 #define BS_8BIT_PEL (1 << 1)
55 #define BS_KEYFRAME (1 << 2)
56 #define BS_MV_Y_HALF (1 << 4)
57 #define BS_MV_X_HALF (1 << 5)
58 #define BS_NONREF (1 << 8)
59 #define BS_BUFFER 9
60 
61 
62 typedef struct Plane {
63  uint8_t *buffers[2];
64  uint8_t *pixels[2];
65  uint32_t width;
66  uint32_t height;
67  uint32_t pitch;
68 } Plane;
69 
70 #define CELL_STACK_MAX 20
71 
72 typedef struct Cell {
73  int16_t xpos;
74  int16_t ypos;
75  int16_t width;
76  int16_t height;
77  uint8_t tree;
78  const int8_t *mv_ptr;
79 } Cell;
80 
81 typedef struct Indeo3DecodeContext {
85 
88  int skip_bits;
89  const uint8_t *next_cell_data;
90  const uint8_t *last_byte;
91  const int8_t *mc_vectors;
92  unsigned num_vectors;
93 
94  int16_t width, height;
95  uint32_t frame_num;
96  uint32_t data_size;
97  uint16_t frame_flags;
98  uint8_t cb_offset;
99  uint8_t buf_sel;
100  const uint8_t *y_data_ptr;
101  const uint8_t *v_data_ptr;
102  const uint8_t *u_data_ptr;
103  int32_t y_data_size;
104  int32_t v_data_size;
105  int32_t u_data_size;
106  const uint8_t *alt_quant;
109 
110 
111 static uint8_t requant_tab[8][128];
112 
113 /*
114  * Build the static requantization table.
115  * This table is used to remap pixel values according to a specific
116  * quant index and thus avoid overflows while adding deltas.
117  */
118 static av_cold void build_requant_tab(void)
119 {
120  static int8_t offsets[8] = { 1, 1, 2, -3, -3, 3, 4, 4 };
121  static int8_t deltas [8] = { 0, 1, 0, 4, 4, 1, 0, 1 };
122 
123  int i, j, step;
124 
125  for (i = 0; i < 8; i++) {
126  step = i + 2;
127  for (j = 0; j < 128; j++)
128  requant_tab[i][j] = (j + offsets[i]) / step * step + deltas[i];
129  }
130 
131  /* some last elements calculated above will have values >= 128 */
132  /* pixel values shall never exceed 127 so set them to non-overflowing values */
133  /* according with the quantization step of the respective section */
134  requant_tab[0][127] = 126;
135  requant_tab[1][119] = 118;
136  requant_tab[1][120] = 118;
137  requant_tab[2][126] = 124;
138  requant_tab[2][127] = 124;
139  requant_tab[6][124] = 120;
140  requant_tab[6][125] = 120;
141  requant_tab[6][126] = 120;
142  requant_tab[6][127] = 120;
143 
144  /* Patch for compatibility with the Intel's binary decoders */
145  requant_tab[1][7] = 10;
146  requant_tab[4][8] = 10;
147 }
148 
149 
151  AVCodecContext *avctx)
152 {
153  int p, luma_width, luma_height, chroma_width, chroma_height;
154  int luma_pitch, chroma_pitch, luma_size, chroma_size;
155 
156  luma_width = ctx->width;
157  luma_height = ctx->height;
158 
159  if (luma_width < 16 || luma_width > 640 ||
160  luma_height < 16 || luma_height > 480 ||
161  luma_width & 3 || luma_height & 3) {
162  av_log(avctx, AV_LOG_ERROR, "Invalid picture dimensions: %d x %d!\n",
163  luma_width, luma_height);
164  return AVERROR_INVALIDDATA;
165  }
166 
167  chroma_width = FFALIGN(luma_width >> 2, 4);
168  chroma_height = FFALIGN(luma_height >> 2, 4);
169 
170  luma_pitch = FFALIGN(luma_width, 16);
171  chroma_pitch = FFALIGN(chroma_width, 16);
172 
173  /* Calculate size of the luminance plane. */
174  /* Add one line more for INTRA prediction. */
175  luma_size = luma_pitch * (luma_height + 1);
176 
177  /* Calculate size of a chrominance planes. */
178  /* Add one line more for INTRA prediction. */
179  chroma_size = chroma_pitch * (chroma_height + 1);
180 
181  /* allocate frame buffers */
182  for (p = 0; p < 3; p++) {
183  ctx->planes[p].pitch = !p ? luma_pitch : chroma_pitch;
184  ctx->planes[p].width = !p ? luma_width : chroma_width;
185  ctx->planes[p].height = !p ? luma_height : chroma_height;
186 
187  ctx->planes[p].buffers[0] = av_malloc(!p ? luma_size : chroma_size);
188  ctx->planes[p].buffers[1] = av_malloc(!p ? luma_size : chroma_size);
189 
190  /* fill the INTRA prediction lines with the middle pixel value = 64 */
191  memset(ctx->planes[p].buffers[0], 0x40, ctx->planes[p].pitch);
192  memset(ctx->planes[p].buffers[1], 0x40, ctx->planes[p].pitch);
193 
194  /* set buffer pointers = buf_ptr + pitch and thus skip the INTRA prediction line */
195  ctx->planes[p].pixels[0] = ctx->planes[p].buffers[0] + ctx->planes[p].pitch;
196  ctx->planes[p].pixels[1] = ctx->planes[p].buffers[1] + ctx->planes[p].pitch;
197  memset(ctx->planes[p].pixels[0], 0, ctx->planes[p].pitch * ctx->planes[p].height);
198  memset(ctx->planes[p].pixels[1], 0, ctx->planes[p].pitch * ctx->planes[p].height);
199  }
200 
201  return 0;
202 }
203 
204 
206 {
207  int p;
208 
209  for (p = 0; p < 3; p++) {
210  av_freep(&ctx->planes[p].buffers[0]);
211  av_freep(&ctx->planes[p].buffers[1]);
212  ctx->planes[p].pixels[0] = ctx->planes[p].pixels[1] = 0;
213  }
214 }
215 
216 
225 static int copy_cell(Indeo3DecodeContext *ctx, Plane *plane, Cell *cell)
226 {
227  int h, w, mv_x, mv_y, offset, offset_dst;
228  uint8_t *src, *dst;
229 
230  /* setup output and reference pointers */
231  offset_dst = (cell->ypos << 2) * plane->pitch + (cell->xpos << 2);
232  dst = plane->pixels[ctx->buf_sel] + offset_dst;
233  mv_y = cell->mv_ptr[0];
234  mv_x = cell->mv_ptr[1];
235 
236  /* -1 because there is an extra line on top for prediction */
237  if ((cell->ypos << 2) + mv_y < -1 || (cell->xpos << 2) + mv_x < 0 ||
238  ((cell->ypos + cell->height) << 2) + mv_y > plane->height ||
239  ((cell->xpos + cell->width) << 2) + mv_x > plane->width) {
240  av_log(ctx->avctx, AV_LOG_ERROR,
241  "Motion vectors point out of the frame.\n");
242  return AVERROR_INVALIDDATA;
243  }
244 
245  offset = offset_dst + mv_y * plane->pitch + mv_x;
246  src = plane->pixels[ctx->buf_sel ^ 1] + offset;
247 
248  h = cell->height << 2;
249 
250  for (w = cell->width; w > 0;) {
251  /* copy using 16xH blocks */
252  if (!((cell->xpos << 2) & 15) && w >= 4) {
253  for (; w >= 4; src += 16, dst += 16, w -= 4)
254  ctx->dsp.put_no_rnd_pixels_tab[0][0](dst, src, plane->pitch, h);
255  }
256 
257  /* copy using 8xH blocks */
258  if (!((cell->xpos << 2) & 7) && w >= 2) {
259  ctx->dsp.put_no_rnd_pixels_tab[1][0](dst, src, plane->pitch, h);
260  w -= 2;
261  src += 8;
262  dst += 8;
263  }
264 
265  if (w >= 1) {
266  copy_block4(dst, src, plane->pitch, plane->pitch, h);
267  w--;
268  src += 4;
269  dst += 4;
270  }
271  }
272 
273  return 0;
274 }
275 
276 
277 /* Average 4/8 pixels at once without rounding using SWAR */
278 #define AVG_32(dst, src, ref) \
279  AV_WN32A(dst, ((AV_RN32A(src) + AV_RN32A(ref)) >> 1) & 0x7F7F7F7FUL)
280 
281 #define AVG_64(dst, src, ref) \
282  AV_WN64A(dst, ((AV_RN64A(src) + AV_RN64A(ref)) >> 1) & 0x7F7F7F7F7F7F7F7FULL)
283 
284 
285 /*
286  * Replicate each even pixel as follows:
287  * ABCDEFGH -> AACCEEGG
288  */
289 static inline uint64_t replicate64(uint64_t a) {
290 #if HAVE_BIGENDIAN
291  a &= 0xFF00FF00FF00FF00ULL;
292  a |= a >> 8;
293 #else
294  a &= 0x00FF00FF00FF00FFULL;
295  a |= a << 8;
296 #endif
297  return a;
298 }
299 
300 static inline uint32_t replicate32(uint32_t a) {
301 #if HAVE_BIGENDIAN
302  a &= 0xFF00FF00UL;
303  a |= a >> 8;
304 #else
305  a &= 0x00FF00FFUL;
306  a |= a << 8;
307 #endif
308  return a;
309 }
310 
311 
312 /* Fill n lines with 64bit pixel value pix */
313 static inline void fill_64(uint8_t *dst, const uint64_t pix, int32_t n,
314  int32_t row_offset)
315 {
316  for (; n > 0; dst += row_offset, n--)
317  AV_WN64A(dst, pix);
318 }
319 
320 
321 /* Error codes for cell decoding. */
322 enum {
329 };
330 
331 
332 #define BUFFER_PRECHECK \
333 if (*data_ptr >= last_ptr) \
334  return IV3_OUT_OF_DATA; \
335 
336 #define RLE_BLOCK_COPY \
337  if (cell->mv_ptr || !skip_flag) \
338  copy_block4(dst, ref, row_offset, row_offset, 4 << v_zoom)
339 
340 #define RLE_BLOCK_COPY_8 \
341  pix64 = AV_RN64A(ref);\
342  if (is_first_row) {/* special prediction case: top line of a cell */\
343  pix64 = replicate64(pix64);\
344  fill_64(dst + row_offset, pix64, 7, row_offset);\
345  AVG_64(dst, ref, dst + row_offset);\
346  } else \
347  fill_64(dst, pix64, 8, row_offset)
348 
349 #define RLE_LINES_COPY \
350  copy_block4(dst, ref, row_offset, row_offset, num_lines << v_zoom)
351 
352 #define RLE_LINES_COPY_M10 \
353  pix64 = AV_RN64A(ref);\
354  if (is_top_of_cell) {\
355  pix64 = replicate64(pix64);\
356  fill_64(dst + row_offset, pix64, (num_lines << 1) - 1, row_offset);\
357  AVG_64(dst, ref, dst + row_offset);\
358  } else \
359  fill_64(dst, pix64, num_lines << 1, row_offset)
360 
361 #define APPLY_DELTA_4 \
362  AV_WN16A(dst + line_offset ,\
363  (AV_RN16A(ref ) + delta_tab->deltas[dyad1]) & 0x7F7F);\
364  AV_WN16A(dst + line_offset + 2,\
365  (AV_RN16A(ref + 2) + delta_tab->deltas[dyad2]) & 0x7F7F);\
366  if (mode >= 3) {\
367  if (is_top_of_cell && !cell->ypos) {\
368  AV_COPY32(dst, dst + row_offset);\
369  } else {\
370  AVG_32(dst, ref, dst + row_offset);\
371  }\
372  }
373 
374 #define APPLY_DELTA_8 \
375  /* apply two 32-bit VQ deltas to next even line */\
376  if (is_top_of_cell) { \
377  AV_WN32A(dst + row_offset , \
378  (replicate32(AV_RN32A(ref )) + delta_tab->deltas_m10[dyad1]) & 0x7F7F7F7F);\
379  AV_WN32A(dst + row_offset + 4, \
380  (replicate32(AV_RN32A(ref + 4)) + delta_tab->deltas_m10[dyad2]) & 0x7F7F7F7F);\
381  } else { \
382  AV_WN32A(dst + row_offset , \
383  (AV_RN32A(ref ) + delta_tab->deltas_m10[dyad1]) & 0x7F7F7F7F);\
384  AV_WN32A(dst + row_offset + 4, \
385  (AV_RN32A(ref + 4) + delta_tab->deltas_m10[dyad2]) & 0x7F7F7F7F);\
386  } \
387  /* odd lines are not coded but rather interpolated/replicated */\
388  /* first line of the cell on the top of image? - replicate */\
389  /* otherwise - interpolate */\
390  if (is_top_of_cell && !cell->ypos) {\
391  AV_COPY64(dst, dst + row_offset);\
392  } else \
393  AVG_64(dst, ref, dst + row_offset);
394 
395 
396 #define APPLY_DELTA_1011_INTER \
397  if (mode == 10) { \
398  AV_WN32A(dst , \
399  (AV_RN32A(dst ) + delta_tab->deltas_m10[dyad1]) & 0x7F7F7F7F);\
400  AV_WN32A(dst + 4 , \
401  (AV_RN32A(dst + 4 ) + delta_tab->deltas_m10[dyad2]) & 0x7F7F7F7F);\
402  AV_WN32A(dst + row_offset , \
403  (AV_RN32A(dst + row_offset ) + delta_tab->deltas_m10[dyad1]) & 0x7F7F7F7F);\
404  AV_WN32A(dst + row_offset + 4, \
405  (AV_RN32A(dst + row_offset + 4) + delta_tab->deltas_m10[dyad2]) & 0x7F7F7F7F);\
406  } else { \
407  AV_WN16A(dst , \
408  (AV_RN16A(dst ) + delta_tab->deltas[dyad1]) & 0x7F7F);\
409  AV_WN16A(dst + 2 , \
410  (AV_RN16A(dst + 2 ) + delta_tab->deltas[dyad2]) & 0x7F7F);\
411  AV_WN16A(dst + row_offset , \
412  (AV_RN16A(dst + row_offset ) + delta_tab->deltas[dyad1]) & 0x7F7F);\
413  AV_WN16A(dst + row_offset + 2, \
414  (AV_RN16A(dst + row_offset + 2) + delta_tab->deltas[dyad2]) & 0x7F7F);\
415  }
416 
417 
418 static int decode_cell_data(Cell *cell, uint8_t *block, uint8_t *ref_block,
419  int pitch, int h_zoom, int v_zoom, int mode,
420  const vqEntry *delta[2], int swap_quads[2],
421  const uint8_t **data_ptr, const uint8_t *last_ptr)
422 {
423  int x, y, line, num_lines;
424  int rle_blocks = 0;
425  uint8_t code, *dst, *ref;
426  const vqEntry *delta_tab;
427  unsigned int dyad1, dyad2;
428  uint64_t pix64;
429  int skip_flag = 0, is_top_of_cell, is_first_row = 1;
430  int row_offset, blk_row_offset, line_offset;
431 
432  row_offset = pitch;
433  blk_row_offset = (row_offset << (2 + v_zoom)) - (cell->width << 2);
434  line_offset = v_zoom ? row_offset : 0;
435 
436  if (cell->height & v_zoom || cell->width & h_zoom)
437  return IV3_BAD_DATA;
438 
439  for (y = 0; y < cell->height; is_first_row = 0, y += 1 + v_zoom) {
440  for (x = 0; x < cell->width; x += 1 + h_zoom) {
441  ref = ref_block;
442  dst = block;
443 
444  if (rle_blocks > 0) {
445  if (mode <= 4) {
447  } else if (mode == 10 && !cell->mv_ptr) {
449  }
450  rle_blocks--;
451  } else {
452  for (line = 0; line < 4;) {
453  num_lines = 1;
454  is_top_of_cell = is_first_row && !line;
455 
456  /* select primary VQ table for odd, secondary for even lines */
457  if (mode <= 4)
458  delta_tab = delta[line & 1];
459  else
460  delta_tab = delta[1];
462  code = bytestream_get_byte(data_ptr);
463  if (code < 248) {
464  if (code < delta_tab->num_dyads) {
466  dyad1 = bytestream_get_byte(data_ptr);
467  dyad2 = code;
468  if (dyad1 >= delta_tab->num_dyads || dyad1 >= 248)
469  return IV3_BAD_DATA;
470  } else {
471  /* process QUADS */
472  code -= delta_tab->num_dyads;
473  dyad1 = code / delta_tab->quad_exp;
474  dyad2 = code % delta_tab->quad_exp;
475  if (swap_quads[line & 1])
476  FFSWAP(unsigned int, dyad1, dyad2);
477  }
478  if (mode <= 4) {
480  } else if (mode == 10 && !cell->mv_ptr) {
482  } else {
484  }
485  } else {
486  /* process RLE codes */
487  switch (code) {
488  case RLE_ESC_FC:
489  skip_flag = 0;
490  rle_blocks = 1;
491  code = 253;
492  /* FALLTHROUGH */
493  case RLE_ESC_FF:
494  case RLE_ESC_FE:
495  case RLE_ESC_FD:
496  num_lines = 257 - code - line;
497  if (num_lines <= 0)
498  return IV3_BAD_RLE;
499  if (mode <= 4) {
501  } else if (mode == 10 && !cell->mv_ptr) {
503  }
504  break;
505  case RLE_ESC_FB:
507  code = bytestream_get_byte(data_ptr);
508  rle_blocks = (code & 0x1F) - 1; /* set block counter */
509  if (code >= 64 || rle_blocks < 0)
510  return IV3_BAD_COUNTER;
511  skip_flag = code & 0x20;
512  num_lines = 4 - line; /* enforce next block processing */
513  if (mode >= 10 || (cell->mv_ptr || !skip_flag)) {
514  if (mode <= 4) {
516  } else if (mode == 10 && !cell->mv_ptr) {
518  }
519  }
520  break;
521  case RLE_ESC_F9:
522  skip_flag = 1;
523  rle_blocks = 1;
524  /* FALLTHROUGH */
525  case RLE_ESC_FA:
526  if (line)
527  return IV3_BAD_RLE;
528  num_lines = 4; /* enforce next block processing */
529  if (cell->mv_ptr) {
530  if (mode <= 4) {
532  } else if (mode == 10 && !cell->mv_ptr) {
534  }
535  }
536  break;
537  default:
538  return IV3_UNSUPPORTED;
539  }
540  }
541 
542  line += num_lines;
543  ref += row_offset * (num_lines << v_zoom);
544  dst += row_offset * (num_lines << v_zoom);
545  }
546  }
547 
548  /* move to next horizontal block */
549  block += 4 << h_zoom;
550  ref_block += 4 << h_zoom;
551  }
552 
553  /* move to next line of blocks */
554  ref_block += blk_row_offset;
555  block += blk_row_offset;
556  }
557  return IV3_NOERR;
558 }
559 
560 
575  Plane *plane, Cell *cell, const uint8_t *data_ptr,
576  const uint8_t *last_ptr)
577 {
578  int x, mv_x, mv_y, mode, vq_index, prim_indx, second_indx;
579  int zoom_fac;
580  int offset, error = 0, swap_quads[2];
581  uint8_t code, *block, *ref_block = 0;
582  const vqEntry *delta[2];
583  const uint8_t *data_start = data_ptr;
584 
585  /* get coding mode and VQ table index from the VQ descriptor byte */
586  code = *data_ptr++;
587  mode = code >> 4;
588  vq_index = code & 0xF;
589 
590  /* setup output and reference pointers */
591  offset = (cell->ypos << 2) * plane->pitch + (cell->xpos << 2);
592  block = plane->pixels[ctx->buf_sel] + offset;
593  if (!cell->mv_ptr) {
594  /* use previous line as reference for INTRA cells */
595  ref_block = block - plane->pitch;
596  } else if (mode >= 10) {
597  /* for mode 10 and 11 INTER first copy the predicted cell into the current one */
598  /* so we don't need to do data copying for each RLE code later */
599  int ret = copy_cell(ctx, plane, cell);
600  if (ret < 0)
601  return ret;
602  } else {
603  /* set the pointer to the reference pixels for modes 0-4 INTER */
604  mv_y = cell->mv_ptr[0];
605  mv_x = cell->mv_ptr[1];
606 
607  /* -1 because there is an extra line on top for prediction */
608  if ((cell->ypos << 2) + mv_y < -1 || (cell->xpos << 2) + mv_x < 0 ||
609  ((cell->ypos + cell->height) << 2) + mv_y > plane->height ||
610  ((cell->xpos + cell->width) << 2) + mv_x > plane->width) {
611  av_log(ctx->avctx, AV_LOG_ERROR,
612  "Motion vectors point out of the frame.\n");
613  return AVERROR_INVALIDDATA;
614  }
615 
616  offset += mv_y * plane->pitch + mv_x;
617  ref_block = plane->pixels[ctx->buf_sel ^ 1] + offset;
618  }
619 
620  /* select VQ tables as follows: */
621  /* modes 0 and 3 use only the primary table for all lines in a block */
622  /* while modes 1 and 4 switch between primary and secondary tables on alternate lines */
623  if (mode == 1 || mode == 4) {
624  code = ctx->alt_quant[vq_index];
625  prim_indx = (code >> 4) + ctx->cb_offset;
626  second_indx = (code & 0xF) + ctx->cb_offset;
627  } else {
628  vq_index += ctx->cb_offset;
629  prim_indx = second_indx = vq_index;
630  }
631 
632  if (prim_indx >= 24 || second_indx >= 24) {
633  av_log(avctx, AV_LOG_ERROR, "Invalid VQ table indexes! Primary: %d, secondary: %d!\n",
634  prim_indx, second_indx);
635  return AVERROR_INVALIDDATA;
636  }
637 
638  delta[0] = &vq_tab[second_indx];
639  delta[1] = &vq_tab[prim_indx];
640  swap_quads[0] = second_indx >= 16;
641  swap_quads[1] = prim_indx >= 16;
642 
643  /* requantize the prediction if VQ index of this cell differs from VQ index */
644  /* of the predicted cell in order to avoid overflows. */
645  if (vq_index >= 8 && ref_block) {
646  for (x = 0; x < cell->width << 2; x++)
647  ref_block[x] = requant_tab[vq_index & 7][ref_block[x]];
648  }
649 
650  error = IV3_NOERR;
651 
652  switch (mode) {
653  case 0: /*------------------ MODES 0 & 1 (4x4 block processing) --------------------*/
654  case 1:
655  case 3: /*------------------ MODES 3 & 4 (4x8 block processing) --------------------*/
656  case 4:
657  if (mode >= 3 && cell->mv_ptr) {
658  av_log(avctx, AV_LOG_ERROR, "Attempt to apply Mode 3/4 to an INTER cell!\n");
659  return AVERROR_INVALIDDATA;
660  }
661 
662  zoom_fac = mode >= 3;
663  error = decode_cell_data(cell, block, ref_block, plane->pitch, 0, zoom_fac,
664  mode, delta, swap_quads, &data_ptr, last_ptr);
665  break;
666  case 10: /*-------------------- MODE 10 (8x8 block processing) ---------------------*/
667  case 11: /*----------------- MODE 11 (4x8 INTER block processing) ------------------*/
668  if (mode == 10 && !cell->mv_ptr) { /* MODE 10 INTRA processing */
669  error = decode_cell_data(cell, block, ref_block, plane->pitch, 1, 1,
670  mode, delta, swap_quads, &data_ptr, last_ptr);
671  } else { /* mode 10 and 11 INTER processing */
672  if (mode == 11 && !cell->mv_ptr) {
673  av_log(avctx, AV_LOG_ERROR, "Attempt to use Mode 11 for an INTRA cell!\n");
674  return AVERROR_INVALIDDATA;
675  }
676 
677  zoom_fac = mode == 10;
678  error = decode_cell_data(cell, block, ref_block, plane->pitch,
679  zoom_fac, 1, mode, delta, swap_quads,
680  &data_ptr, last_ptr);
681  }
682  break;
683  default:
684  av_log(avctx, AV_LOG_ERROR, "Unsupported coding mode: %d\n", mode);
685  return AVERROR_INVALIDDATA;
686  }//switch mode
687 
688  switch (error) {
689  case IV3_BAD_RLE:
690  av_log(avctx, AV_LOG_ERROR, "Mode %d: RLE code %X is not allowed at the current line\n",
691  mode, data_ptr[-1]);
692  return AVERROR_INVALIDDATA;
693  case IV3_BAD_DATA:
694  av_log(avctx, AV_LOG_ERROR, "Mode %d: invalid VQ data\n", mode);
695  return AVERROR_INVALIDDATA;
696  case IV3_BAD_COUNTER:
697  av_log(avctx, AV_LOG_ERROR, "Mode %d: RLE-FB invalid counter: %d\n", mode, code);
698  return AVERROR_INVALIDDATA;
699  case IV3_UNSUPPORTED:
700  av_log(avctx, AV_LOG_ERROR, "Mode %d: unsupported RLE code: %X\n", mode, data_ptr[-1]);
701  return AVERROR_INVALIDDATA;
702  case IV3_OUT_OF_DATA:
703  av_log(avctx, AV_LOG_ERROR, "Mode %d: attempt to read past end of buffer\n", mode);
704  return AVERROR_INVALIDDATA;
705  }
706 
707  return data_ptr - data_start; /* report number of bytes consumed from the input buffer */
708 }
709 
710 
711 /* Binary tree codes. */
712 enum {
713  H_SPLIT = 0,
714  V_SPLIT = 1,
717 };
718 
719 
720 #define SPLIT_CELL(size, new_size) (new_size) = ((size) > 2) ? ((((size) + 2) >> 2) << 1) : 1
721 
722 #define UPDATE_BITPOS(n) \
723  ctx->skip_bits += (n); \
724  ctx->need_resync = 1
725 
726 #define RESYNC_BITSTREAM \
727  if (ctx->need_resync && !(get_bits_count(&ctx->gb) & 7)) { \
728  skip_bits_long(&ctx->gb, ctx->skip_bits); \
729  ctx->skip_bits = 0; \
730  ctx->need_resync = 0; \
731  }
732 
733 #define CHECK_CELL \
734  if (curr_cell.xpos + curr_cell.width > (plane->width >> 2) || \
735  curr_cell.ypos + curr_cell.height > (plane->height >> 2)) { \
736  av_log(avctx, AV_LOG_ERROR, "Invalid cell: x=%d, y=%d, w=%d, h=%d\n", \
737  curr_cell.xpos, curr_cell.ypos, curr_cell.width, curr_cell.height); \
738  return AVERROR_INVALIDDATA; \
739  }
740 
741 
743  Plane *plane, int code, Cell *ref_cell,
744  const int depth, const int strip_width)
745 {
746  Cell curr_cell;
747  int bytes_used, ret;
748 
749  if (depth <= 0) {
750  av_log(avctx, AV_LOG_ERROR, "Stack overflow (corrupted binary tree)!\n");
751  return AVERROR_INVALIDDATA; // unwind recursion
752  }
753 
754  curr_cell = *ref_cell; // clone parent cell
755  if (code == H_SPLIT) {
756  SPLIT_CELL(ref_cell->height, curr_cell.height);
757  ref_cell->ypos += curr_cell.height;
758  ref_cell->height -= curr_cell.height;
759  if (ref_cell->height <= 0 || curr_cell.height <= 0)
760  return AVERROR_INVALIDDATA;
761  } else if (code == V_SPLIT) {
762  if (curr_cell.width > strip_width) {
763  /* split strip */
764  curr_cell.width = (curr_cell.width <= (strip_width << 1) ? 1 : 2) * strip_width;
765  } else
766  SPLIT_CELL(ref_cell->width, curr_cell.width);
767  ref_cell->xpos += curr_cell.width;
768  ref_cell->width -= curr_cell.width;
769  if (ref_cell->width <= 0 || curr_cell.width <= 0)
770  return AVERROR_INVALIDDATA;
771  }
772 
773  while (1) { /* loop until return */
775  switch (code = get_bits(&ctx->gb, 2)) {
776  case H_SPLIT:
777  case V_SPLIT:
778  if (parse_bintree(ctx, avctx, plane, code, &curr_cell, depth - 1, strip_width))
779  return AVERROR_INVALIDDATA;
780  break;
781  case INTRA_NULL:
782  if (!curr_cell.tree) { /* MC tree INTRA code */
783  curr_cell.mv_ptr = 0; /* mark the current strip as INTRA */
784  curr_cell.tree = 1; /* enter the VQ tree */
785  } else { /* VQ tree NULL code */
787  code = get_bits(&ctx->gb, 2);
788  if (code >= 2) {
789  av_log(avctx, AV_LOG_ERROR, "Invalid VQ_NULL code: %d\n", code);
790  return AVERROR_INVALIDDATA;
791  }
792  if (code == 1)
793  av_log(avctx, AV_LOG_ERROR, "SkipCell procedure not implemented yet!\n");
794 
795  CHECK_CELL
796  if (!curr_cell.mv_ptr)
797  return AVERROR_INVALIDDATA;
798  ret = copy_cell(ctx, plane, &curr_cell);
799  return ret;
800  }
801  break;
802  case INTER_DATA:
803  if (!curr_cell.tree) { /* MC tree INTER code */
804  unsigned mv_idx;
805  /* get motion vector index and setup the pointer to the mv set */
806  if (!ctx->need_resync)
807  ctx->next_cell_data = &ctx->gb.buffer[(get_bits_count(&ctx->gb) + 7) >> 3];
808  mv_idx = *(ctx->next_cell_data++) << 1;
809  if (mv_idx >= ctx->num_vectors) {
810  av_log(avctx, AV_LOG_ERROR, "motion vector index out of range\n");
811  return AVERROR_INVALIDDATA;
812  }
813  curr_cell.mv_ptr = &ctx->mc_vectors[mv_idx];
814  curr_cell.tree = 1; /* enter the VQ tree */
815  UPDATE_BITPOS(8);
816  } else { /* VQ tree DATA code */
817  if (!ctx->need_resync)
818  ctx->next_cell_data = &ctx->gb.buffer[(get_bits_count(&ctx->gb) + 7) >> 3];
819 
820  CHECK_CELL
821  bytes_used = decode_cell(ctx, avctx, plane, &curr_cell,
822  ctx->next_cell_data, ctx->last_byte);
823  if (bytes_used < 0)
824  return AVERROR_INVALIDDATA;
825 
826  UPDATE_BITPOS(bytes_used << 3);
827  ctx->next_cell_data += bytes_used;
828  return 0;
829  }
830  break;
831  }
832  }//while
833 
834  return 0;
835 }
836 
837 
839  Plane *plane, const uint8_t *data, int32_t data_size,
840  int32_t strip_width)
841 {
842  Cell curr_cell;
843  unsigned num_vectors;
844 
845  /* each plane data starts with mc_vector_count field, */
846  /* an optional array of motion vectors followed by the vq data */
847  num_vectors = bytestream_get_le32(&data);
848  if (num_vectors > 256) {
849  av_log(ctx->avctx, AV_LOG_ERROR,
850  "Read invalid number of motion vectors %d\n", num_vectors);
851  return AVERROR_INVALIDDATA;
852  }
853  if (num_vectors * 2 >= data_size)
854  return AVERROR_INVALIDDATA;
855 
856  ctx->num_vectors = num_vectors;
857  ctx->mc_vectors = num_vectors ? data : 0;
858 
859  /* init the bitreader */
860  init_get_bits(&ctx->gb, &data[num_vectors * 2], (data_size - num_vectors * 2) << 3);
861  ctx->skip_bits = 0;
862  ctx->need_resync = 0;
863 
864  ctx->last_byte = data + data_size - 1;
865 
866  /* initialize the 1st cell and set its dimensions to whole plane */
867  curr_cell.xpos = curr_cell.ypos = 0;
868  curr_cell.width = plane->width >> 2;
869  curr_cell.height = plane->height >> 2;
870  curr_cell.tree = 0; // we are in the MC tree now
871  curr_cell.mv_ptr = 0; // no motion vector = INTRA cell
872 
873  return parse_bintree(ctx, avctx, plane, INTRA_NULL, &curr_cell, CELL_STACK_MAX, strip_width);
874 }
875 
876 
877 #define OS_HDR_ID MKBETAG('F', 'R', 'M', 'H')
878 
880  const uint8_t *buf, int buf_size)
881 {
882  GetByteContext gb;
883  const uint8_t *bs_hdr;
884  uint32_t frame_num, word2, check_sum, data_size;
885  uint32_t y_offset, u_offset, v_offset, starts[3], ends[3];
886  uint16_t height, width;
887  int i, j;
888 
889  bytestream2_init(&gb, buf, buf_size);
890 
891  /* parse and check the OS header */
892  frame_num = bytestream2_get_le32(&gb);
893  word2 = bytestream2_get_le32(&gb);
894  check_sum = bytestream2_get_le32(&gb);
895  data_size = bytestream2_get_le32(&gb);
896 
897  if ((frame_num ^ word2 ^ data_size ^ OS_HDR_ID) != check_sum) {
898  av_log(avctx, AV_LOG_ERROR, "OS header checksum mismatch!\n");
899  return AVERROR_INVALIDDATA;
900  }
901 
902  /* parse the bitstream header */
903  bs_hdr = gb.buffer;
904 
905  if (bytestream2_get_le16(&gb) != 32) {
906  av_log(avctx, AV_LOG_ERROR, "Unsupported codec version!\n");
907  return AVERROR_INVALIDDATA;
908  }
909 
910  ctx->frame_num = frame_num;
911  ctx->frame_flags = bytestream2_get_le16(&gb);
912  ctx->data_size = (bytestream2_get_le32(&gb) + 7) >> 3;
913  ctx->cb_offset = bytestream2_get_byte(&gb);
914 
915  if (ctx->data_size == 16)
916  return 4;
917  ctx->data_size = FFMIN(ctx->data_size, buf_size - 16);
918 
919  bytestream2_skip(&gb, 3); // skip reserved byte and checksum
920 
921  /* check frame dimensions */
922  height = bytestream2_get_le16(&gb);
923  width = bytestream2_get_le16(&gb);
924  if (av_image_check_size(width, height, 0, avctx))
925  return AVERROR_INVALIDDATA;
926 
927  if (width != ctx->width || height != ctx->height) {
928  int res;
929 
930  av_dlog(avctx, "Frame dimensions changed!\n");
931 
932  if (width < 16 || width > 640 ||
933  height < 16 || height > 480 ||
934  width & 3 || height & 3) {
935  av_log(avctx, AV_LOG_ERROR,
936  "Invalid picture dimensions: %d x %d!\n", width, height);
937  return AVERROR_INVALIDDATA;
938  }
939 
940  ctx->width = width;
941  ctx->height = height;
942 
943  free_frame_buffers(ctx);
944  if ((res = allocate_frame_buffers(ctx, avctx)) < 0)
945  return res;
946  avcodec_set_dimensions(avctx, width, height);
947  }
948 
949  y_offset = bytestream2_get_le32(&gb);
950  v_offset = bytestream2_get_le32(&gb);
951  u_offset = bytestream2_get_le32(&gb);
952  bytestream2_skip(&gb, 4);
953 
954  /* unfortunately there is no common order of planes in the buffer */
955  /* so we use that sorting algo for determining planes data sizes */
956  starts[0] = y_offset;
957  starts[1] = v_offset;
958  starts[2] = u_offset;
959 
960  for (j = 0; j < 3; j++) {
961  ends[j] = ctx->data_size;
962  for (i = 2; i >= 0; i--)
963  if (starts[i] < ends[j] && starts[i] > starts[j])
964  ends[j] = starts[i];
965  }
966 
967  ctx->y_data_size = ends[0] - starts[0];
968  ctx->v_data_size = ends[1] - starts[1];
969  ctx->u_data_size = ends[2] - starts[2];
970  if (FFMAX3(y_offset, v_offset, u_offset) >= ctx->data_size - 16 ||
971  FFMIN3(y_offset, v_offset, u_offset) < gb.buffer - bs_hdr + 16 ||
972  FFMIN3(ctx->y_data_size, ctx->v_data_size, ctx->u_data_size) <= 0) {
973  av_log(avctx, AV_LOG_ERROR, "One of the y/u/v offsets is invalid\n");
974  return AVERROR_INVALIDDATA;
975  }
976 
977  ctx->y_data_ptr = bs_hdr + y_offset;
978  ctx->v_data_ptr = bs_hdr + v_offset;
979  ctx->u_data_ptr = bs_hdr + u_offset;
980  ctx->alt_quant = gb.buffer;
981 
982  if (ctx->data_size == 16) {
983  av_log(avctx, AV_LOG_DEBUG, "Sync frame encountered!\n");
984  return 16;
985  }
986 
987  if (ctx->frame_flags & BS_8BIT_PEL) {
988  av_log_ask_for_sample(avctx, "8-bit pixel format\n");
989  return AVERROR_PATCHWELCOME;
990  }
991 
992  if (ctx->frame_flags & BS_MV_X_HALF || ctx->frame_flags & BS_MV_Y_HALF) {
993  av_log_ask_for_sample(avctx, "halfpel motion vectors\n");
994  return AVERROR_PATCHWELCOME;
995  }
996 
997  return 0;
998 }
999 
1000 
1010 static void output_plane(const Plane *plane, int buf_sel, uint8_t *dst, int dst_pitch)
1011 {
1012  int x,y;
1013  const uint8_t *src = plane->pixels[buf_sel];
1014  uint32_t pitch = plane->pitch;
1015 
1016  for (y = 0; y < plane->height; y++) {
1017  /* convert four pixels at once using SWAR */
1018  for (x = 0; x < plane->width >> 2; x++) {
1019  AV_WN32A(dst, (AV_RN32A(src) & 0x7F7F7F7F) << 1);
1020  src += 4;
1021  dst += 4;
1022  }
1023 
1024  for (x <<= 2; x < plane->width; x++)
1025  *dst++ = *src++ << 1;
1026 
1027  src += pitch - plane->width;
1028  dst += dst_pitch - plane->width;
1029  }
1030 }
1031 
1032 
1034 {
1035  Indeo3DecodeContext *ctx = avctx->priv_data;
1036 
1037  ctx->avctx = avctx;
1038  ctx->width = avctx->width;
1039  ctx->height = avctx->height;
1040  avctx->pix_fmt = PIX_FMT_YUV410P;
1041 
1043 
1044  dsputil_init(&ctx->dsp, avctx);
1045 
1046  allocate_frame_buffers(ctx, avctx);
1047 
1048  return 0;
1049 }
1050 
1051 
1052 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
1053  AVPacket *avpkt)
1054 {
1055  Indeo3DecodeContext *ctx = avctx->priv_data;
1056  const uint8_t *buf = avpkt->data;
1057  int buf_size = avpkt->size;
1058  int res;
1059 
1060  res = decode_frame_headers(ctx, avctx, buf, buf_size);
1061  if (res < 0)
1062  return res;
1063 
1064  /* skip sync(null) frames */
1065  if (res) {
1066  // we have processed 16 bytes but no data was decoded
1067  *data_size = 0;
1068  return buf_size;
1069  }
1070 
1071  /* skip droppable INTER frames if requested */
1072  if (ctx->frame_flags & BS_NONREF &&
1073  (avctx->skip_frame >= AVDISCARD_NONREF))
1074  return 0;
1075 
1076  /* skip INTER frames if requested */
1077  if (!(ctx->frame_flags & BS_KEYFRAME) && avctx->skip_frame >= AVDISCARD_NONKEY)
1078  return 0;
1079 
1080  /* use BS_BUFFER flag for buffer switching */
1081  ctx->buf_sel = (ctx->frame_flags >> BS_BUFFER) & 1;
1082 
1083  /* decode luma plane */
1084  if ((res = decode_plane(ctx, avctx, ctx->planes, ctx->y_data_ptr, ctx->y_data_size, 40)))
1085  return res;
1086 
1087  /* decode chroma planes */
1088  if ((res = decode_plane(ctx, avctx, &ctx->planes[1], ctx->u_data_ptr, ctx->u_data_size, 10)))
1089  return res;
1090 
1091  if ((res = decode_plane(ctx, avctx, &ctx->planes[2], ctx->v_data_ptr, ctx->v_data_size, 10)))
1092  return res;
1093 
1094  if (ctx->frame.data[0])
1095  avctx->release_buffer(avctx, &ctx->frame);
1096 
1097  ctx->frame.reference = 0;
1098  if ((res = avctx->get_buffer(avctx, &ctx->frame)) < 0) {
1099  av_log(ctx->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1100  return res;
1101  }
1102 
1103  output_plane(&ctx->planes[0], ctx->buf_sel, ctx->frame.data[0], ctx->frame.linesize[0]);
1104  output_plane(&ctx->planes[1], ctx->buf_sel, ctx->frame.data[1], ctx->frame.linesize[1]);
1105  output_plane(&ctx->planes[2], ctx->buf_sel, ctx->frame.data[2], ctx->frame.linesize[2]);
1106 
1107  *data_size = sizeof(AVFrame);
1108  *(AVFrame*)data = ctx->frame;
1109 
1110  return buf_size;
1111 }
1112 
1113 
1115 {
1116  Indeo3DecodeContext *ctx = avctx->priv_data;
1117 
1118  free_frame_buffers(avctx->priv_data);
1119 
1120  if (ctx->frame.data[0])
1121  avctx->release_buffer(avctx, &ctx->frame);
1122 
1123  return 0;
1124 }
1125 
1127  .name = "indeo3",
1128  .type = AVMEDIA_TYPE_VIDEO,
1129  .id = CODEC_ID_INDEO3,
1130  .priv_data_size = sizeof(Indeo3DecodeContext),
1131  .init = decode_init,
1132  .close = decode_close,
1133  .decode = decode_frame,
1134  .long_name = NULL_IF_CONFIG_SMALL("Intel Indeo 3"),
1135 };