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  }
198 
199  return 0;
200 }
201 
202 
204 {
205  int p;
206 
207  for (p = 0; p < 3; p++) {
208  av_freep(&ctx->planes[p].buffers[0]);
209  av_freep(&ctx->planes[p].buffers[1]);
210  ctx->planes[p].pixels[0] = ctx->planes[p].pixels[1] = 0;
211  }
212 }
213 
214 
223 static void copy_cell(Indeo3DecodeContext *ctx, Plane *plane, Cell *cell)
224 {
225  int h, w, mv_x, mv_y, offset, offset_dst;
226  uint8_t *src, *dst;
227 
228  /* setup output and reference pointers */
229  offset_dst = (cell->ypos << 2) * plane->pitch + (cell->xpos << 2);
230  dst = plane->pixels[ctx->buf_sel] + offset_dst;
231  mv_y = cell->mv_ptr[0];
232  mv_x = cell->mv_ptr[1];
233  offset = offset_dst + mv_y * plane->pitch + mv_x;
234  src = plane->pixels[ctx->buf_sel ^ 1] + offset;
235 
236  h = cell->height << 2;
237 
238  for (w = cell->width; w > 0;) {
239  /* copy using 16xH blocks */
240  if (!((cell->xpos << 2) & 15) && w >= 4) {
241  for (; w >= 4; src += 16, dst += 16, w -= 4)
242  ctx->dsp.put_no_rnd_pixels_tab[0][0](dst, src, plane->pitch, h);
243  }
244 
245  /* copy using 8xH blocks */
246  if (!((cell->xpos << 2) & 7) && w >= 2) {
247  ctx->dsp.put_no_rnd_pixels_tab[1][0](dst, src, plane->pitch, h);
248  w -= 2;
249  src += 8;
250  dst += 8;
251  }
252 
253  if (w >= 1) {
254  copy_block4(dst, src, plane->pitch, plane->pitch, h);
255  w--;
256  src += 4;
257  dst += 4;
258  }
259  }
260 }
261 
262 
263 /* Average 4/8 pixels at once without rounding using SWAR */
264 #define AVG_32(dst, src, ref) \
265  AV_WN32A(dst, ((AV_RN32A(src) + AV_RN32A(ref)) >> 1) & 0x7F7F7F7FUL)
266 
267 #define AVG_64(dst, src, ref) \
268  AV_WN64A(dst, ((AV_RN64A(src) + AV_RN64A(ref)) >> 1) & 0x7F7F7F7F7F7F7F7FULL)
269 
270 
271 /*
272  * Replicate each even pixel as follows:
273  * ABCDEFGH -> AACCEEGG
274  */
275 static inline uint64_t replicate64(uint64_t a) {
276 #if HAVE_BIGENDIAN
277  a &= 0xFF00FF00FF00FF00ULL;
278  a |= a >> 8;
279 #else
280  a &= 0x00FF00FF00FF00FFULL;
281  a |= a << 8;
282 #endif
283  return a;
284 }
285 
286 static inline uint32_t replicate32(uint32_t a) {
287 #if HAVE_BIGENDIAN
288  a &= 0xFF00FF00UL;
289  a |= a >> 8;
290 #else
291  a &= 0x00FF00FFUL;
292  a |= a << 8;
293 #endif
294  return a;
295 }
296 
297 
298 /* Fill n lines with 64bit pixel value pix */
299 static inline void fill_64(uint8_t *dst, const uint64_t pix, int32_t n,
300  int32_t row_offset)
301 {
302  for (; n > 0; dst += row_offset, n--)
303  AV_WN64A(dst, pix);
304 }
305 
306 
307 /* Error codes for cell decoding. */
308 enum {
315 };
316 
317 
318 #define BUFFER_PRECHECK \
319 if (*data_ptr >= last_ptr) \
320  return IV3_OUT_OF_DATA; \
321 
322 #define RLE_BLOCK_COPY \
323  if (cell->mv_ptr || !skip_flag) \
324  copy_block4(dst, ref, row_offset, row_offset, 4 << v_zoom)
325 
326 #define RLE_BLOCK_COPY_8 \
327  pix64 = AV_RN64A(ref);\
328  if (is_first_row) {/* special prediction case: top line of a cell */\
329  pix64 = replicate64(pix64);\
330  fill_64(dst + row_offset, pix64, 7, row_offset);\
331  AVG_64(dst, ref, dst + row_offset);\
332  } else \
333  fill_64(dst, pix64, 8, row_offset)
334 
335 #define RLE_LINES_COPY \
336  copy_block4(dst, ref, row_offset, row_offset, num_lines << v_zoom)
337 
338 #define RLE_LINES_COPY_M10 \
339  pix64 = AV_RN64A(ref);\
340  if (is_top_of_cell) {\
341  pix64 = replicate64(pix64);\
342  fill_64(dst + row_offset, pix64, (num_lines << 1) - 1, row_offset);\
343  AVG_64(dst, ref, dst + row_offset);\
344  } else \
345  fill_64(dst, pix64, num_lines << 1, row_offset)
346 
347 #define APPLY_DELTA_4 \
348  AV_WN16A(dst + line_offset ,\
349  (AV_RN16A(ref ) + delta_tab->deltas[dyad1]) & 0x7F7F);\
350  AV_WN16A(dst + line_offset + 2,\
351  (AV_RN16A(ref + 2) + delta_tab->deltas[dyad2]) & 0x7F7F);\
352  if (mode >= 3) {\
353  if (is_top_of_cell && !cell->ypos) {\
354  AV_COPY32(dst, dst + row_offset);\
355  } else {\
356  AVG_32(dst, ref, dst + row_offset);\
357  }\
358  }
359 
360 #define APPLY_DELTA_8 \
361  /* apply two 32-bit VQ deltas to next even line */\
362  if (is_top_of_cell) { \
363  AV_WN32A(dst + row_offset , \
364  (replicate32(AV_RN32A(ref )) + delta_tab->deltas_m10[dyad1]) & 0x7F7F7F7F);\
365  AV_WN32A(dst + row_offset + 4, \
366  (replicate32(AV_RN32A(ref + 4)) + delta_tab->deltas_m10[dyad2]) & 0x7F7F7F7F);\
367  } else { \
368  AV_WN32A(dst + row_offset , \
369  (AV_RN32A(ref ) + delta_tab->deltas_m10[dyad1]) & 0x7F7F7F7F);\
370  AV_WN32A(dst + row_offset + 4, \
371  (AV_RN32A(ref + 4) + delta_tab->deltas_m10[dyad2]) & 0x7F7F7F7F);\
372  } \
373  /* odd lines are not coded but rather interpolated/replicated */\
374  /* first line of the cell on the top of image? - replicate */\
375  /* otherwise - interpolate */\
376  if (is_top_of_cell && !cell->ypos) {\
377  AV_COPY64(dst, dst + row_offset);\
378  } else \
379  AVG_64(dst, ref, dst + row_offset);
380 
381 
382 #define APPLY_DELTA_1011_INTER \
383  if (mode == 10) { \
384  AV_WN32A(dst , \
385  (AV_RN32A(dst ) + delta_tab->deltas_m10[dyad1]) & 0x7F7F7F7F);\
386  AV_WN32A(dst + 4 , \
387  (AV_RN32A(dst + 4 ) + delta_tab->deltas_m10[dyad2]) & 0x7F7F7F7F);\
388  AV_WN32A(dst + row_offset , \
389  (AV_RN32A(dst + row_offset ) + delta_tab->deltas_m10[dyad1]) & 0x7F7F7F7F);\
390  AV_WN32A(dst + row_offset + 4, \
391  (AV_RN32A(dst + row_offset + 4) + delta_tab->deltas_m10[dyad2]) & 0x7F7F7F7F);\
392  } else { \
393  AV_WN16A(dst , \
394  (AV_RN16A(dst ) + delta_tab->deltas[dyad1]) & 0x7F7F);\
395  AV_WN16A(dst + 2 , \
396  (AV_RN16A(dst + 2 ) + delta_tab->deltas[dyad2]) & 0x7F7F);\
397  AV_WN16A(dst + row_offset , \
398  (AV_RN16A(dst + row_offset ) + delta_tab->deltas[dyad1]) & 0x7F7F);\
399  AV_WN16A(dst + row_offset + 2, \
400  (AV_RN16A(dst + row_offset + 2) + delta_tab->deltas[dyad2]) & 0x7F7F);\
401  }
402 
403 
404 static int decode_cell_data(Cell *cell, uint8_t *block, uint8_t *ref_block,
405  int pitch, int h_zoom, int v_zoom, int mode,
406  const vqEntry *delta[2], int swap_quads[2],
407  const uint8_t **data_ptr, const uint8_t *last_ptr)
408 {
409  int x, y, line, num_lines;
410  int rle_blocks = 0;
411  uint8_t code, *dst, *ref;
412  const vqEntry *delta_tab;
413  unsigned int dyad1, dyad2;
414  uint64_t pix64;
415  int skip_flag = 0, is_top_of_cell, is_first_row = 1;
416  int row_offset, blk_row_offset, line_offset;
417 
418  row_offset = pitch;
419  blk_row_offset = (row_offset << (2 + v_zoom)) - (cell->width << 2);
420  line_offset = v_zoom ? row_offset : 0;
421 
422  if (cell->height & v_zoom || cell->width & h_zoom)
423  return IV3_BAD_DATA;
424 
425  for (y = 0; y < cell->height; is_first_row = 0, y += 1 + v_zoom) {
426  for (x = 0; x < cell->width; x += 1 + h_zoom) {
427  ref = ref_block;
428  dst = block;
429 
430  if (rle_blocks > 0) {
431  if (mode <= 4) {
433  } else if (mode == 10 && !cell->mv_ptr) {
435  }
436  rle_blocks--;
437  } else {
438  for (line = 0; line < 4;) {
439  num_lines = 1;
440  is_top_of_cell = is_first_row && !line;
441 
442  /* select primary VQ table for odd, secondary for even lines */
443  if (mode <= 4)
444  delta_tab = delta[line & 1];
445  else
446  delta_tab = delta[1];
448  code = bytestream_get_byte(data_ptr);
449  if (code < 248) {
450  if (code < delta_tab->num_dyads) {
452  dyad1 = bytestream_get_byte(data_ptr);
453  dyad2 = code;
454  if (dyad1 >= delta_tab->num_dyads || dyad1 >= 248)
455  return IV3_BAD_DATA;
456  } else {
457  /* process QUADS */
458  code -= delta_tab->num_dyads;
459  dyad1 = code / delta_tab->quad_exp;
460  dyad2 = code % delta_tab->quad_exp;
461  if (swap_quads[line & 1])
462  FFSWAP(unsigned int, dyad1, dyad2);
463  }
464  if (mode <= 4) {
466  } else if (mode == 10 && !cell->mv_ptr) {
468  } else {
470  }
471  } else {
472  /* process RLE codes */
473  switch (code) {
474  case RLE_ESC_FC:
475  skip_flag = 0;
476  rle_blocks = 1;
477  code = 253;
478  /* FALLTHROUGH */
479  case RLE_ESC_FF:
480  case RLE_ESC_FE:
481  case RLE_ESC_FD:
482  num_lines = 257 - code - line;
483  if (num_lines <= 0)
484  return IV3_BAD_RLE;
485  if (mode <= 4) {
487  } else if (mode == 10 && !cell->mv_ptr) {
489  }
490  break;
491  case RLE_ESC_FB:
493  code = bytestream_get_byte(data_ptr);
494  rle_blocks = (code & 0x1F) - 1; /* set block counter */
495  if (code >= 64 || rle_blocks < 0)
496  return IV3_BAD_COUNTER;
497  skip_flag = code & 0x20;
498  num_lines = 4 - line; /* enforce next block processing */
499  if (mode >= 10 || (cell->mv_ptr || !skip_flag)) {
500  if (mode <= 4) {
502  } else if (mode == 10 && !cell->mv_ptr) {
504  }
505  }
506  break;
507  case RLE_ESC_F9:
508  skip_flag = 1;
509  rle_blocks = 1;
510  /* FALLTHROUGH */
511  case RLE_ESC_FA:
512  if (line)
513  return IV3_BAD_RLE;
514  num_lines = 4; /* enforce next block processing */
515  if (cell->mv_ptr) {
516  if (mode <= 4) {
518  } else if (mode == 10 && !cell->mv_ptr) {
520  }
521  }
522  break;
523  default:
524  return IV3_UNSUPPORTED;
525  }
526  }
527 
528  line += num_lines;
529  ref += row_offset * (num_lines << v_zoom);
530  dst += row_offset * (num_lines << v_zoom);
531  }
532  }
533 
534  /* move to next horizontal block */
535  block += 4 << h_zoom;
536  ref_block += 4 << h_zoom;
537  }
538 
539  /* move to next line of blocks */
540  ref_block += blk_row_offset;
541  block += blk_row_offset;
542  }
543  return IV3_NOERR;
544 }
545 
546 
561  Plane *plane, Cell *cell, const uint8_t *data_ptr,
562  const uint8_t *last_ptr)
563 {
564  int x, mv_x, mv_y, mode, vq_index, prim_indx, second_indx;
565  int zoom_fac;
566  int offset, error = 0, swap_quads[2];
567  uint8_t code, *block, *ref_block = 0;
568  const vqEntry *delta[2];
569  const uint8_t *data_start = data_ptr;
570 
571  /* get coding mode and VQ table index from the VQ descriptor byte */
572  code = *data_ptr++;
573  mode = code >> 4;
574  vq_index = code & 0xF;
575 
576  /* setup output and reference pointers */
577  offset = (cell->ypos << 2) * plane->pitch + (cell->xpos << 2);
578  block = plane->pixels[ctx->buf_sel] + offset;
579  if (!cell->mv_ptr) {
580  /* use previous line as reference for INTRA cells */
581  ref_block = block - plane->pitch;
582  } else if (mode >= 10) {
583  /* for mode 10 and 11 INTER first copy the predicted cell into the current one */
584  /* so we don't need to do data copying for each RLE code later */
585  copy_cell(ctx, plane, cell);
586  } else {
587  /* set the pointer to the reference pixels for modes 0-4 INTER */
588  mv_y = cell->mv_ptr[0];
589  mv_x = cell->mv_ptr[1];
590  offset += mv_y * plane->pitch + mv_x;
591  ref_block = plane->pixels[ctx->buf_sel ^ 1] + offset;
592  }
593 
594  /* select VQ tables as follows: */
595  /* modes 0 and 3 use only the primary table for all lines in a block */
596  /* while modes 1 and 4 switch between primary and secondary tables on alternate lines */
597  if (mode == 1 || mode == 4) {
598  code = ctx->alt_quant[vq_index];
599  prim_indx = (code >> 4) + ctx->cb_offset;
600  second_indx = (code & 0xF) + ctx->cb_offset;
601  } else {
602  vq_index += ctx->cb_offset;
603  prim_indx = second_indx = vq_index;
604  }
605 
606  if (prim_indx >= 24 || second_indx >= 24) {
607  av_log(avctx, AV_LOG_ERROR, "Invalid VQ table indexes! Primary: %d, secondary: %d!\n",
608  prim_indx, second_indx);
609  return AVERROR_INVALIDDATA;
610  }
611 
612  delta[0] = &vq_tab[second_indx];
613  delta[1] = &vq_tab[prim_indx];
614  swap_quads[0] = second_indx >= 16;
615  swap_quads[1] = prim_indx >= 16;
616 
617  /* requantize the prediction if VQ index of this cell differs from VQ index */
618  /* of the predicted cell in order to avoid overflows. */
619  if (vq_index >= 8 && ref_block) {
620  for (x = 0; x < cell->width << 2; x++)
621  ref_block[x] = requant_tab[vq_index & 7][ref_block[x]];
622  }
623 
624  error = IV3_NOERR;
625 
626  switch (mode) {
627  case 0: /*------------------ MODES 0 & 1 (4x4 block processing) --------------------*/
628  case 1:
629  case 3: /*------------------ MODES 3 & 4 (4x8 block processing) --------------------*/
630  case 4:
631  if (mode >= 3 && cell->mv_ptr) {
632  av_log(avctx, AV_LOG_ERROR, "Attempt to apply Mode 3/4 to an INTER cell!\n");
633  return AVERROR_INVALIDDATA;
634  }
635 
636  zoom_fac = mode >= 3;
637  error = decode_cell_data(cell, block, ref_block, plane->pitch, 0, zoom_fac,
638  mode, delta, swap_quads, &data_ptr, last_ptr);
639  break;
640  case 10: /*-------------------- MODE 10 (8x8 block processing) ---------------------*/
641  case 11: /*----------------- MODE 11 (4x8 INTER block processing) ------------------*/
642  if (mode == 10 && !cell->mv_ptr) { /* MODE 10 INTRA processing */
643  error = decode_cell_data(cell, block, ref_block, plane->pitch, 1, 1,
644  mode, delta, swap_quads, &data_ptr, last_ptr);
645  } else { /* mode 10 and 11 INTER processing */
646  if (mode == 11 && !cell->mv_ptr) {
647  av_log(avctx, AV_LOG_ERROR, "Attempt to use Mode 11 for an INTRA cell!\n");
648  return AVERROR_INVALIDDATA;
649  }
650 
651  zoom_fac = mode == 10;
652  error = decode_cell_data(cell, block, ref_block, plane->pitch,
653  zoom_fac, 1, mode, delta, swap_quads,
654  &data_ptr, last_ptr);
655  }
656  break;
657  default:
658  av_log(avctx, AV_LOG_ERROR, "Unsupported coding mode: %d\n", mode);
659  return AVERROR_INVALIDDATA;
660  }//switch mode
661 
662  switch (error) {
663  case IV3_BAD_RLE:
664  av_log(avctx, AV_LOG_ERROR, "Mode %d: RLE code %X is not allowed at the current line\n",
665  mode, data_ptr[-1]);
666  return AVERROR_INVALIDDATA;
667  case IV3_BAD_DATA:
668  av_log(avctx, AV_LOG_ERROR, "Mode %d: invalid VQ data\n", mode);
669  return AVERROR_INVALIDDATA;
670  case IV3_BAD_COUNTER:
671  av_log(avctx, AV_LOG_ERROR, "Mode %d: RLE-FB invalid counter: %d\n", mode, code);
672  return AVERROR_INVALIDDATA;
673  case IV3_UNSUPPORTED:
674  av_log(avctx, AV_LOG_ERROR, "Mode %d: unsupported RLE code: %X\n", mode, data_ptr[-1]);
675  return AVERROR_INVALIDDATA;
676  case IV3_OUT_OF_DATA:
677  av_log(avctx, AV_LOG_ERROR, "Mode %d: attempt to read past end of buffer\n", mode);
678  return AVERROR_INVALIDDATA;
679  }
680 
681  return data_ptr - data_start; /* report number of bytes consumed from the input buffer */
682 }
683 
684 
685 /* Binary tree codes. */
686 enum {
687  H_SPLIT = 0,
688  V_SPLIT = 1,
691 };
692 
693 
694 #define SPLIT_CELL(size, new_size) (new_size) = ((size) > 2) ? ((((size) + 2) >> 2) << 1) : 1
695 
696 #define UPDATE_BITPOS(n) \
697  ctx->skip_bits += (n); \
698  ctx->need_resync = 1
699 
700 #define RESYNC_BITSTREAM \
701  if (ctx->need_resync && !(get_bits_count(&ctx->gb) & 7)) { \
702  skip_bits_long(&ctx->gb, ctx->skip_bits); \
703  ctx->skip_bits = 0; \
704  ctx->need_resync = 0; \
705  }
706 
707 #define CHECK_CELL \
708  if (curr_cell.xpos + curr_cell.width > (plane->width >> 2) || \
709  curr_cell.ypos + curr_cell.height > (plane->height >> 2)) { \
710  av_log(avctx, AV_LOG_ERROR, "Invalid cell: x=%d, y=%d, w=%d, h=%d\n", \
711  curr_cell.xpos, curr_cell.ypos, curr_cell.width, curr_cell.height); \
712  return AVERROR_INVALIDDATA; \
713  }
714 
715 
717  Plane *plane, int code, Cell *ref_cell,
718  const int depth, const int strip_width)
719 {
720  Cell curr_cell;
721  int bytes_used;
722 
723  if (depth <= 0) {
724  av_log(avctx, AV_LOG_ERROR, "Stack overflow (corrupted binary tree)!\n");
725  return AVERROR_INVALIDDATA; // unwind recursion
726  }
727 
728  curr_cell = *ref_cell; // clone parent cell
729  if (code == H_SPLIT) {
730  SPLIT_CELL(ref_cell->height, curr_cell.height);
731  ref_cell->ypos += curr_cell.height;
732  ref_cell->height -= curr_cell.height;
733  if (ref_cell->height <= 0 || curr_cell.height <= 0)
734  return AVERROR_INVALIDDATA;
735  } else if (code == V_SPLIT) {
736  if (curr_cell.width > strip_width) {
737  /* split strip */
738  curr_cell.width = (curr_cell.width <= (strip_width << 1) ? 1 : 2) * strip_width;
739  } else
740  SPLIT_CELL(ref_cell->width, curr_cell.width);
741  ref_cell->xpos += curr_cell.width;
742  ref_cell->width -= curr_cell.width;
743  if (ref_cell->width <= 0 || curr_cell.width <= 0)
744  return AVERROR_INVALIDDATA;
745  }
746 
747  while (1) { /* loop until return */
749  switch (code = get_bits(&ctx->gb, 2)) {
750  case H_SPLIT:
751  case V_SPLIT:
752  if (parse_bintree(ctx, avctx, plane, code, &curr_cell, depth - 1, strip_width))
753  return AVERROR_INVALIDDATA;
754  break;
755  case INTRA_NULL:
756  if (!curr_cell.tree) { /* MC tree INTRA code */
757  curr_cell.mv_ptr = 0; /* mark the current strip as INTRA */
758  curr_cell.tree = 1; /* enter the VQ tree */
759  } else { /* VQ tree NULL code */
761  code = get_bits(&ctx->gb, 2);
762  if (code >= 2) {
763  av_log(avctx, AV_LOG_ERROR, "Invalid VQ_NULL code: %d\n", code);
764  return AVERROR_INVALIDDATA;
765  }
766  if (code == 1)
767  av_log(avctx, AV_LOG_ERROR, "SkipCell procedure not implemented yet!\n");
768 
769  CHECK_CELL
770  if (!curr_cell.mv_ptr)
771  return AVERROR_INVALIDDATA;
772  copy_cell(ctx, plane, &curr_cell);
773  return 0;
774  }
775  break;
776  case INTER_DATA:
777  if (!curr_cell.tree) { /* MC tree INTER code */
778  unsigned mv_idx;
779  /* get motion vector index and setup the pointer to the mv set */
780  if (!ctx->need_resync)
781  ctx->next_cell_data = &ctx->gb.buffer[(get_bits_count(&ctx->gb) + 7) >> 3];
782  mv_idx = *(ctx->next_cell_data++) << 1;
783  if (mv_idx >= ctx->num_vectors) {
784  av_log(avctx, AV_LOG_ERROR, "motion vector index out of range\n");
785  return AVERROR_INVALIDDATA;
786  }
787  curr_cell.mv_ptr = &ctx->mc_vectors[mv_idx];
788  curr_cell.tree = 1; /* enter the VQ tree */
789  UPDATE_BITPOS(8);
790  } else { /* VQ tree DATA code */
791  if (!ctx->need_resync)
792  ctx->next_cell_data = &ctx->gb.buffer[(get_bits_count(&ctx->gb) + 7) >> 3];
793 
794  CHECK_CELL
795  bytes_used = decode_cell(ctx, avctx, plane, &curr_cell,
796  ctx->next_cell_data, ctx->last_byte);
797  if (bytes_used < 0)
798  return AVERROR_INVALIDDATA;
799 
800  UPDATE_BITPOS(bytes_used << 3);
801  ctx->next_cell_data += bytes_used;
802  return 0;
803  }
804  break;
805  }
806  }//while
807 
808  return 0;
809 }
810 
811 
813  Plane *plane, const uint8_t *data, int32_t data_size,
814  int32_t strip_width)
815 {
816  Cell curr_cell;
817  unsigned num_vectors;
818 
819  /* each plane data starts with mc_vector_count field, */
820  /* an optional array of motion vectors followed by the vq data */
821  num_vectors = bytestream_get_le32(&data);
822  if (num_vectors > 256) {
823  av_log(ctx->avctx, AV_LOG_ERROR,
824  "Read invalid number of motion vectors %d\n", num_vectors);
825  return AVERROR_INVALIDDATA;
826  }
827  if (num_vectors * 2 >= data_size)
828  return AVERROR_INVALIDDATA;
829 
830  ctx->num_vectors = num_vectors;
831  ctx->mc_vectors = num_vectors ? data : 0;
832 
833  /* init the bitreader */
834  init_get_bits(&ctx->gb, &data[num_vectors * 2], (data_size - num_vectors * 2) << 3);
835  ctx->skip_bits = 0;
836  ctx->need_resync = 0;
837 
838  ctx->last_byte = data + data_size - 1;
839 
840  /* initialize the 1st cell and set its dimensions to whole plane */
841  curr_cell.xpos = curr_cell.ypos = 0;
842  curr_cell.width = plane->width >> 2;
843  curr_cell.height = plane->height >> 2;
844  curr_cell.tree = 0; // we are in the MC tree now
845  curr_cell.mv_ptr = 0; // no motion vector = INTRA cell
846 
847  return parse_bintree(ctx, avctx, plane, INTRA_NULL, &curr_cell, CELL_STACK_MAX, strip_width);
848 }
849 
850 
851 #define OS_HDR_ID MKBETAG('F', 'R', 'M', 'H')
852 
854  const uint8_t *buf, int buf_size)
855 {
856  const uint8_t *buf_ptr = buf, *bs_hdr;
857  uint32_t frame_num, word2, check_sum, data_size;
858  uint32_t y_offset, u_offset, v_offset, starts[3], ends[3];
859  uint16_t height, width;
860  int i, j;
861 
862  /* parse and check the OS header */
863  frame_num = bytestream_get_le32(&buf_ptr);
864  word2 = bytestream_get_le32(&buf_ptr);
865  check_sum = bytestream_get_le32(&buf_ptr);
866  data_size = bytestream_get_le32(&buf_ptr);
867 
868  if ((frame_num ^ word2 ^ data_size ^ OS_HDR_ID) != check_sum) {
869  av_log(avctx, AV_LOG_ERROR, "OS header checksum mismatch!\n");
870  return AVERROR_INVALIDDATA;
871  }
872 
873  /* parse the bitstream header */
874  bs_hdr = buf_ptr;
875 
876  if (bytestream_get_le16(&buf_ptr) != 32) {
877  av_log(avctx, AV_LOG_ERROR, "Unsupported codec version!\n");
878  return AVERROR_INVALIDDATA;
879  }
880 
881  ctx->frame_num = frame_num;
882  ctx->frame_flags = bytestream_get_le16(&buf_ptr);
883  ctx->data_size = (bytestream_get_le32(&buf_ptr) + 7) >> 3;
884  ctx->cb_offset = *buf_ptr++;
885 
886  if (ctx->data_size == 16)
887  return 4;
888  if (ctx->data_size > buf_size)
889  ctx->data_size = buf_size;
890 
891  buf_ptr += 3; // skip reserved byte and checksum
892 
893  /* check frame dimensions */
894  height = bytestream_get_le16(&buf_ptr);
895  width = bytestream_get_le16(&buf_ptr);
896  if (av_image_check_size(width, height, 0, avctx))
897  return AVERROR_INVALIDDATA;
898 
899  if (width != ctx->width || height != ctx->height) {
900  int res;
901 
902  av_dlog(avctx, "Frame dimensions changed!\n");
903 
904  if (width < 16 || width > 640 ||
905  height < 16 || height > 480 ||
906  width & 3 || height & 3) {
907  av_log(avctx, AV_LOG_ERROR,
908  "Invalid picture dimensions: %d x %d!\n", width, height);
909  return AVERROR_INVALIDDATA;
910  }
911 
912  ctx->width = width;
913  ctx->height = height;
914 
915  free_frame_buffers(ctx);
916  if ((res = allocate_frame_buffers(ctx, avctx)) < 0)
917  return res;
918  avcodec_set_dimensions(avctx, width, height);
919  }
920 
921  y_offset = bytestream_get_le32(&buf_ptr);
922  v_offset = bytestream_get_le32(&buf_ptr);
923  u_offset = bytestream_get_le32(&buf_ptr);
924 
925  /* unfortunately there is no common order of planes in the buffer */
926  /* so we use that sorting algo for determining planes data sizes */
927  starts[0] = y_offset;
928  starts[1] = v_offset;
929  starts[2] = u_offset;
930 
931  for (j = 0; j < 3; j++) {
932  ends[j] = ctx->data_size;
933  for (i = 2; i >= 0; i--)
934  if (starts[i] < ends[j] && starts[i] > starts[j])
935  ends[j] = starts[i];
936  }
937 
938  ctx->y_data_size = ends[0] - starts[0];
939  ctx->v_data_size = ends[1] - starts[1];
940  ctx->u_data_size = ends[2] - starts[2];
941  if (FFMAX3(y_offset, v_offset, u_offset) >= ctx->data_size - 16 ||
942  FFMIN3(ctx->y_data_size, ctx->v_data_size, ctx->u_data_size) <= 0) {
943  av_log(avctx, AV_LOG_ERROR, "One of the y/u/v offsets is invalid\n");
944  return AVERROR_INVALIDDATA;
945  }
946 
947  ctx->y_data_ptr = bs_hdr + y_offset;
948  ctx->v_data_ptr = bs_hdr + v_offset;
949  ctx->u_data_ptr = bs_hdr + u_offset;
950  ctx->alt_quant = buf_ptr + sizeof(uint32_t);
951 
952  if (ctx->data_size == 16) {
953  av_log(avctx, AV_LOG_DEBUG, "Sync frame encountered!\n");
954  return 16;
955  }
956 
957  if (ctx->frame_flags & BS_8BIT_PEL) {
958  av_log_ask_for_sample(avctx, "8-bit pixel format\n");
959  return AVERROR_PATCHWELCOME;
960  }
961 
962  if (ctx->frame_flags & BS_MV_X_HALF || ctx->frame_flags & BS_MV_Y_HALF) {
963  av_log_ask_for_sample(avctx, "halfpel motion vectors\n");
964  return AVERROR_PATCHWELCOME;
965  }
966 
967  return 0;
968 }
969 
970 
980 static void output_plane(const Plane *plane, int buf_sel, uint8_t *dst, int dst_pitch)
981 {
982  int x,y;
983  const uint8_t *src = plane->pixels[buf_sel];
984  uint32_t pitch = plane->pitch;
985 
986  for (y = 0; y < plane->height; y++) {
987  /* convert four pixels at once using SWAR */
988  for (x = 0; x < plane->width >> 2; x++) {
989  AV_WN32A(dst, (AV_RN32A(src) & 0x7F7F7F7F) << 1);
990  src += 4;
991  dst += 4;
992  }
993 
994  for (x <<= 2; x < plane->width; x++)
995  *dst++ = *src++ << 1;
996 
997  src += pitch - plane->width;
998  dst += dst_pitch - plane->width;
999  }
1000 }
1001 
1002 
1004 {
1005  Indeo3DecodeContext *ctx = avctx->priv_data;
1006 
1007  ctx->avctx = avctx;
1008  ctx->width = avctx->width;
1009  ctx->height = avctx->height;
1010  avctx->pix_fmt = PIX_FMT_YUV410P;
1011 
1013 
1014  dsputil_init(&ctx->dsp, avctx);
1015 
1016  allocate_frame_buffers(ctx, avctx);
1017 
1018  return 0;
1019 }
1020 
1021 
1022 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
1023  AVPacket *avpkt)
1024 {
1025  Indeo3DecodeContext *ctx = avctx->priv_data;
1026  const uint8_t *buf = avpkt->data;
1027  int buf_size = avpkt->size;
1028  int res;
1029 
1030  res = decode_frame_headers(ctx, avctx, buf, buf_size);
1031  if (res < 0)
1032  return res;
1033 
1034  /* skip sync(null) frames */
1035  if (res) {
1036  // we have processed 16 bytes but no data was decoded
1037  *data_size = 0;
1038  return buf_size;
1039  }
1040 
1041  /* skip droppable INTER frames if requested */
1042  if (ctx->frame_flags & BS_NONREF &&
1043  (avctx->skip_frame >= AVDISCARD_NONREF))
1044  return 0;
1045 
1046  /* skip INTER frames if requested */
1047  if (!(ctx->frame_flags & BS_KEYFRAME) && avctx->skip_frame >= AVDISCARD_NONKEY)
1048  return 0;
1049 
1050  /* use BS_BUFFER flag for buffer switching */
1051  ctx->buf_sel = (ctx->frame_flags >> BS_BUFFER) & 1;
1052 
1053  /* decode luma plane */
1054  if ((res = decode_plane(ctx, avctx, ctx->planes, ctx->y_data_ptr, ctx->y_data_size, 40)))
1055  return res;
1056 
1057  /* decode chroma planes */
1058  if ((res = decode_plane(ctx, avctx, &ctx->planes[1], ctx->u_data_ptr, ctx->u_data_size, 10)))
1059  return res;
1060 
1061  if ((res = decode_plane(ctx, avctx, &ctx->planes[2], ctx->v_data_ptr, ctx->v_data_size, 10)))
1062  return res;
1063 
1064  if (ctx->frame.data[0])
1065  avctx->release_buffer(avctx, &ctx->frame);
1066 
1067  ctx->frame.reference = 0;
1068  if ((res = avctx->get_buffer(avctx, &ctx->frame)) < 0) {
1069  av_log(ctx->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1070  return res;
1071  }
1072 
1073  output_plane(&ctx->planes[0], ctx->buf_sel, ctx->frame.data[0], ctx->frame.linesize[0]);
1074  output_plane(&ctx->planes[1], ctx->buf_sel, ctx->frame.data[1], ctx->frame.linesize[1]);
1075  output_plane(&ctx->planes[2], ctx->buf_sel, ctx->frame.data[2], ctx->frame.linesize[2]);
1076 
1077  *data_size = sizeof(AVFrame);
1078  *(AVFrame*)data = ctx->frame;
1079 
1080  return buf_size;
1081 }
1082 
1083 
1085 {
1086  Indeo3DecodeContext *ctx = avctx->priv_data;
1087 
1088  free_frame_buffers(avctx->priv_data);
1089 
1090  if (ctx->frame.data[0])
1091  avctx->release_buffer(avctx, &ctx->frame);
1092 
1093  return 0;
1094 }
1095 
1097  .name = "indeo3",
1098  .type = AVMEDIA_TYPE_VIDEO,
1099  .id = CODEC_ID_INDEO3,
1100  .priv_data_size = sizeof(Indeo3DecodeContext),
1101  .init = decode_init,
1102  .close = decode_close,
1103  .decode = decode_frame,
1104  .long_name = NULL_IF_CONFIG_SMALL("Intel Indeo 3"),
1105 };