indeo5.c
Go to the documentation of this file.
1 /*
2  * Indeo Video Interactive v5 compatible decoder
3  * Copyright (c) 2009 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 
30 #define BITSTREAM_READER_LE
31 #include "avcodec.h"
32 #include "get_bits.h"
33 #include "dsputil.h"
34 #include "ivi_dsp.h"
35 #include "ivi_common.h"
36 #include "indeo5data.h"
37 
41 enum {
47 };
48 
49 #define IVI5_PIC_SIZE_ESC 15
50 
61 {
62  int result, i, p, tile_size, pic_size_indx, mb_size, blk_size;
63  int quant_mat, blk_size_changed = 0;
64  IVIBandDesc *band, *band1, *band2;
65  IVIPicConfig pic_conf;
66 
67  ctx->gop_flags = get_bits(&ctx->gb, 8);
68 
69  ctx->gop_hdr_size = (ctx->gop_flags & 1) ? get_bits(&ctx->gb, 16) : 0;
70 
71  if (ctx->gop_flags & IVI5_IS_PROTECTED)
72  ctx->lock_word = get_bits_long(&ctx->gb, 32);
73 
74  tile_size = (ctx->gop_flags & 0x40) ? 64 << get_bits(&ctx->gb, 2) : 0;
75  if (tile_size > 256) {
76  av_log(avctx, AV_LOG_ERROR, "Invalid tile size: %d\n", tile_size);
77  return -1;
78  }
79 
80  /* decode number of wavelet bands */
81  /* num_levels * 3 + 1 */
82  pic_conf.luma_bands = get_bits(&ctx->gb, 2) * 3 + 1;
83  pic_conf.chroma_bands = get_bits1(&ctx->gb) * 3 + 1;
84  ctx->is_scalable = pic_conf.luma_bands != 1 || pic_conf.chroma_bands != 1;
85  if (ctx->is_scalable && (pic_conf.luma_bands != 4 || pic_conf.chroma_bands != 1)) {
86  av_log(avctx, AV_LOG_ERROR, "Scalability: unsupported subdivision! Luma bands: %d, chroma bands: %d\n",
87  pic_conf.luma_bands, pic_conf.chroma_bands);
88  return -1;
89  }
90 
91  pic_size_indx = get_bits(&ctx->gb, 4);
92  if (pic_size_indx == IVI5_PIC_SIZE_ESC) {
93  pic_conf.pic_height = get_bits(&ctx->gb, 13);
94  pic_conf.pic_width = get_bits(&ctx->gb, 13);
95  } else {
96  pic_conf.pic_height = ivi5_common_pic_sizes[pic_size_indx * 2 + 1] << 2;
97  pic_conf.pic_width = ivi5_common_pic_sizes[pic_size_indx * 2 ] << 2;
98  }
99 
100  if (ctx->gop_flags & 2) {
101  av_log(avctx, AV_LOG_ERROR, "YV12 picture format not supported!\n");
102  return -1;
103  }
104 
105  pic_conf.chroma_height = (pic_conf.pic_height + 3) >> 2;
106  pic_conf.chroma_width = (pic_conf.pic_width + 3) >> 2;
107 
108  if (!tile_size) {
109  pic_conf.tile_height = pic_conf.pic_height;
110  pic_conf.tile_width = pic_conf.pic_width;
111  } else {
112  pic_conf.tile_height = pic_conf.tile_width = tile_size;
113  }
114 
115  /* check if picture layout was changed and reallocate buffers */
116  if (ivi_pic_config_cmp(&pic_conf, &ctx->pic_conf)) {
117  result = ff_ivi_init_planes(ctx->planes, &pic_conf);
118  if (result) {
119  av_log(avctx, AV_LOG_ERROR, "Couldn't reallocate color planes!\n");
120  return -1;
121  }
122  ctx->pic_conf = pic_conf;
123  blk_size_changed = 1; /* force reallocation of the internal structures */
124  }
125 
126  for (p = 0; p <= 1; p++) {
127  for (i = 0; i < (!p ? pic_conf.luma_bands : pic_conf.chroma_bands); i++) {
128  band = &ctx->planes[p].bands[i];
129 
130  band->is_halfpel = get_bits1(&ctx->gb);
131 
132  mb_size = get_bits1(&ctx->gb);
133  blk_size = 8 >> get_bits1(&ctx->gb);
134  mb_size = blk_size << !mb_size;
135 
136  blk_size_changed = mb_size != band->mb_size || blk_size != band->blk_size;
137  if (blk_size_changed) {
138  band->mb_size = mb_size;
139  band->blk_size = blk_size;
140  }
141 
142  if (get_bits1(&ctx->gb)) {
143  av_log(avctx, AV_LOG_ERROR, "Extended transform info encountered!\n");
144  return -1;
145  }
146 
147  /* select transform function and scan pattern according to plane and band number */
148  switch ((p << 2) + i) {
149  case 0:
152  band->scan = ff_zigzag_direct;
153  break;
154 
155  case 1:
159  break;
160 
161  case 2:
165  break;
166 
167  case 3:
171  break;
172 
173  case 4:
177  break;
178  }
179 
182 
183  /* select dequant matrix according to plane and band number */
184  if (!p) {
185  quant_mat = (pic_conf.luma_bands > 1) ? i+1 : 0;
186  } else {
187  quant_mat = 5;
188  }
189 
190  if (band->blk_size == 8) {
191  band->intra_base = &ivi5_base_quant_8x8_intra[quant_mat][0];
192  band->inter_base = &ivi5_base_quant_8x8_inter[quant_mat][0];
193  band->intra_scale = &ivi5_scale_quant_8x8_intra[quant_mat][0];
194  band->inter_scale = &ivi5_scale_quant_8x8_inter[quant_mat][0];
195  } else {
200  }
201 
202  if (get_bits(&ctx->gb, 2)) {
203  av_log(avctx, AV_LOG_ERROR, "End marker missing!\n");
204  return -1;
205  }
206  }
207  }
208 
209  /* copy chroma parameters into the 2nd chroma plane */
210  for (i = 0; i < pic_conf.chroma_bands; i++) {
211  band1 = &ctx->planes[1].bands[i];
212  band2 = &ctx->planes[2].bands[i];
213 
214  band2->width = band1->width;
215  band2->height = band1->height;
216  band2->mb_size = band1->mb_size;
217  band2->blk_size = band1->blk_size;
218  band2->is_halfpel = band1->is_halfpel;
219  band2->intra_base = band1->intra_base;
220  band2->inter_base = band1->inter_base;
221  band2->intra_scale = band1->intra_scale;
222  band2->inter_scale = band1->inter_scale;
223  band2->scan = band1->scan;
224  band2->inv_transform = band1->inv_transform;
225  band2->dc_transform = band1->dc_transform;
226  band2->is_2d_trans = band1->is_2d_trans;
227  }
228 
229  /* reallocate internal structures if needed */
230  if (blk_size_changed) {
231  result = ff_ivi_init_tiles(ctx->planes, pic_conf.tile_width,
232  pic_conf.tile_height);
233  if (result) {
234  av_log(avctx, AV_LOG_ERROR,
235  "Couldn't reallocate internal structures!\n");
236  return -1;
237  }
238  }
239 
240  if (ctx->gop_flags & 8) {
241  if (get_bits(&ctx->gb, 3)) {
242  av_log(avctx, AV_LOG_ERROR, "Alignment bits are not zero!\n");
243  return -1;
244  }
245 
246  if (get_bits1(&ctx->gb))
247  skip_bits_long(&ctx->gb, 24); /* skip transparency fill color */
248  }
249 
250  align_get_bits(&ctx->gb);
251 
252  skip_bits(&ctx->gb, 23); /* FIXME: unknown meaning */
253 
254  /* skip GOP extension if any */
255  if (get_bits1(&ctx->gb)) {
256  do {
257  i = get_bits(&ctx->gb, 16);
258  } while (i & 0x8000);
259  }
260 
261  align_get_bits(&ctx->gb);
262 
263  return 0;
264 }
265 
266 
272 static inline void skip_hdr_extension(GetBitContext *gb)
273 {
274  int i, len;
275 
276  do {
277  len = get_bits(gb, 8);
278  for (i = 0; i < len; i++) skip_bits(gb, 8);
279  } while(len);
280 }
281 
282 
291 {
292  if (get_bits(&ctx->gb, 5) != 0x1F) {
293  av_log(avctx, AV_LOG_ERROR, "Invalid picture start code!\n");
294  return -1;
295  }
296 
297  ctx->prev_frame_type = ctx->frame_type;
298  ctx->frame_type = get_bits(&ctx->gb, 3);
299  if (ctx->frame_type >= 5) {
300  av_log(avctx, AV_LOG_ERROR, "Invalid frame type: %d \n", ctx->frame_type);
301  return -1;
302  }
303 
304  ctx->frame_num = get_bits(&ctx->gb, 8);
305 
306  if (ctx->frame_type == FRAMETYPE_INTRA) {
307  ctx->gop_invalid = 1;
308  if (decode_gop_header(ctx, avctx)) {
309  av_log(avctx, AV_LOG_ERROR, "Invalid GOP header, skipping frames.\n");
310  return AVERROR_INVALIDDATA;
311  }
312  ctx->gop_invalid = 0;
313  }
314 
315  if (ctx->frame_type != FRAMETYPE_NULL) {
316  ctx->frame_flags = get_bits(&ctx->gb, 8);
317 
318  ctx->pic_hdr_size = (ctx->frame_flags & 1) ? get_bits_long(&ctx->gb, 24) : 0;
319 
320  ctx->checksum = (ctx->frame_flags & 0x10) ? get_bits(&ctx->gb, 16) : 0;
321 
322  /* skip unknown extension if any */
323  if (ctx->frame_flags & 0x20)
324  skip_hdr_extension(&ctx->gb); /* XXX: untested */
325 
326  /* decode macroblock huffman codebook */
327  if (ff_ivi_dec_huff_desc(&ctx->gb, ctx->frame_flags & 0x40, IVI_MB_HUFF, &ctx->mb_vlc, avctx))
328  return -1;
329 
330  skip_bits(&ctx->gb, 3); /* FIXME: unknown meaning! */
331  }
332 
333  align_get_bits(&ctx->gb);
334 
335  return 0;
336 }
337 
338 
348  AVCodecContext *avctx)
349 {
350  int i;
351  uint8_t band_flags;
352 
353  band_flags = get_bits(&ctx->gb, 8);
354 
355  if (band_flags & 1) {
356  band->is_empty = 1;
357  return 0;
358  }
359 
360  band->data_size = (ctx->frame_flags & 0x80) ? get_bits_long(&ctx->gb, 24) : 0;
361 
362  band->inherit_mv = band_flags & 2;
363  band->inherit_qdelta = band_flags & 8;
364  band->qdelta_present = band_flags & 4;
365  if (!band->qdelta_present) band->inherit_qdelta = 1;
366 
367  /* decode rvmap probability corrections if any */
368  band->num_corr = 0; /* there are no corrections */
369  if (band_flags & 0x10) {
370  band->num_corr = get_bits(&ctx->gb, 8); /* get number of correction pairs */
371  if (band->num_corr > 61) {
372  av_log(avctx, AV_LOG_ERROR, "Too many corrections: %d\n",
373  band->num_corr);
374  return -1;
375  }
376 
377  /* read correction pairs */
378  for (i = 0; i < band->num_corr * 2; i++)
379  band->corr[i] = get_bits(&ctx->gb, 8);
380  }
381 
382  /* select appropriate rvmap table for this band */
383  band->rvmap_sel = (band_flags & 0x40) ? get_bits(&ctx->gb, 3) : 8;
384 
385  /* decode block huffman codebook */
386  if (ff_ivi_dec_huff_desc(&ctx->gb, band_flags & 0x80, IVI_BLK_HUFF, &band->blk_vlc, avctx))
387  return -1;
388 
389  band->checksum_present = get_bits1(&ctx->gb);
390  if (band->checksum_present)
391  band->checksum = get_bits(&ctx->gb, 16);
392 
393  band->glob_quant = get_bits(&ctx->gb, 5);
394 
395  /* skip unknown extension if any */
396  if (band_flags & 0x20) { /* XXX: untested */
397  align_get_bits(&ctx->gb);
398  skip_hdr_extension(&ctx->gb);
399  }
400 
401  align_get_bits(&ctx->gb);
402 
403  return 0;
404 }
405 
406 
418  IVITile *tile, AVCodecContext *avctx)
419 {
420  int x, y, mv_x, mv_y, mv_delta, offs, mb_offset,
421  mv_scale, blks_per_mb;
422  IVIMbInfo *mb, *ref_mb;
423  int row_offset = band->mb_size * band->pitch;
424 
425  mb = tile->mbs;
426  ref_mb = tile->ref_mbs;
427  offs = tile->ypos * band->pitch + tile->xpos;
428 
429  if (!ref_mb &&
430  ((band->qdelta_present && band->inherit_qdelta) || band->inherit_mv))
431  return AVERROR_INVALIDDATA;
432 
433  if (tile->num_MBs != IVI_MBs_PER_TILE(tile->width, tile->height, band->mb_size)) {
434  av_log(avctx, AV_LOG_ERROR, "Allocated tile size %d mismatches parameters %d\n",
435  tile->num_MBs, IVI_MBs_PER_TILE(tile->width, tile->height, band->mb_size));
436  return AVERROR_INVALIDDATA;
437  }
438 
439  /* scale factor for motion vectors */
440  mv_scale = (ctx->planes[0].bands[0].mb_size >> 3) - (band->mb_size >> 3);
441  mv_x = mv_y = 0;
442 
443  for (y = tile->ypos; y < (tile->ypos + tile->height); y += band->mb_size) {
444  mb_offset = offs;
445 
446  for (x = tile->xpos; x < (tile->xpos + tile->width); x += band->mb_size) {
447  mb->xpos = x;
448  mb->ypos = y;
449  mb->buf_offs = mb_offset;
450 
451  if (get_bits1(&ctx->gb)) {
452  if (ctx->frame_type == FRAMETYPE_INTRA) {
453  av_log(avctx, AV_LOG_ERROR, "Empty macroblock in an INTRA picture!\n");
454  return -1;
455  }
456  mb->type = 1; /* empty macroblocks are always INTER */
457  mb->cbp = 0; /* all blocks are empty */
458 
459  mb->q_delta = 0;
460  if (!band->plane && !band->band_num && (ctx->frame_flags & 8)) {
461  mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
462  IVI_VLC_BITS, 1);
463  mb->q_delta = IVI_TOSIGNED(mb->q_delta);
464  }
465 
466  mb->mv_x = mb->mv_y = 0; /* no motion vector coded */
467  if (band->inherit_mv){
468  /* motion vector inheritance */
469  if (mv_scale) {
470  mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale);
471  mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale);
472  } else {
473  mb->mv_x = ref_mb->mv_x;
474  mb->mv_y = ref_mb->mv_y;
475  }
476  }
477  } else {
478  if (band->inherit_mv) {
479  mb->type = ref_mb->type; /* copy mb_type from corresponding reference mb */
480  } else if (ctx->frame_type == FRAMETYPE_INTRA) {
481  mb->type = 0; /* mb_type is always INTRA for intra-frames */
482  } else {
483  mb->type = get_bits1(&ctx->gb);
484  }
485 
486  blks_per_mb = band->mb_size != band->blk_size ? 4 : 1;
487  mb->cbp = get_bits(&ctx->gb, blks_per_mb);
488 
489  mb->q_delta = 0;
490  if (band->qdelta_present) {
491  if (band->inherit_qdelta) {
492  if (ref_mb) mb->q_delta = ref_mb->q_delta;
493  } else if (mb->cbp || (!band->plane && !band->band_num &&
494  (ctx->frame_flags & 8))) {
495  mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
496  IVI_VLC_BITS, 1);
497  mb->q_delta = IVI_TOSIGNED(mb->q_delta);
498  }
499  }
500 
501  if (!mb->type) {
502  mb->mv_x = mb->mv_y = 0; /* there is no motion vector in intra-macroblocks */
503  } else {
504  if (band->inherit_mv){
505  /* motion vector inheritance */
506  if (mv_scale) {
507  mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale);
508  mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale);
509  } else {
510  mb->mv_x = ref_mb->mv_x;
511  mb->mv_y = ref_mb->mv_y;
512  }
513  } else {
514  /* decode motion vector deltas */
515  mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
516  IVI_VLC_BITS, 1);
517  mv_y += IVI_TOSIGNED(mv_delta);
518  mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
519  IVI_VLC_BITS, 1);
520  mv_x += IVI_TOSIGNED(mv_delta);
521  mb->mv_x = mv_x;
522  mb->mv_y = mv_y;
523  }
524  }
525  }
526 
527  mb++;
528  if (ref_mb)
529  ref_mb++;
530  mb_offset += band->mb_size;
531  }
532 
533  offs += row_offset;
534  }
535 
536  align_get_bits(&ctx->gb);
537 
538  return 0;
539 }
540 
541 
548 {
549  switch (ctx->prev_frame_type) {
550  case FRAMETYPE_INTRA:
551  case FRAMETYPE_INTER:
552  ctx->buf_switch ^= 1;
553  ctx->dst_buf = ctx->buf_switch;
554  ctx->ref_buf = ctx->buf_switch ^ 1;
555  break;
557  if (!ctx->inter_scal) {
558  ctx->ref2_buf = 2;
559  ctx->inter_scal = 1;
560  }
561  FFSWAP(int, ctx->dst_buf, ctx->ref2_buf);
562  ctx->ref_buf = ctx->ref2_buf;
563  break;
565  break;
566  }
567 
568  switch (ctx->frame_type) {
569  case FRAMETYPE_INTRA:
570  ctx->buf_switch = 0;
571  /* FALLTHROUGH */
572  case FRAMETYPE_INTER:
573  ctx->inter_scal = 0;
574  ctx->dst_buf = ctx->buf_switch;
575  ctx->ref_buf = ctx->buf_switch ^ 1;
576  break;
579  case FRAMETYPE_NULL:
580  break;
581  }
582 }
583 
584 
586 {
587  return ctx->frame_type != FRAMETYPE_NULL;
588 }
589 
590 
595 {
596  IVI45DecContext *ctx = avctx->priv_data;
597  int result;
598 
600 
601  /* copy rvmap tables in our context so we can apply changes to them */
602  memcpy(ctx->rvmap_tabs, ff_ivi_rvmap_tabs, sizeof(ff_ivi_rvmap_tabs));
603 
604  /* set the initial picture layout according to the basic profile:
605  there is only one band per plane (no scalability), only one tile (no local decoding)
606  and picture format = YVU9 */
607  ctx->pic_conf.pic_width = avctx->width;
608  ctx->pic_conf.pic_height = avctx->height;
609  ctx->pic_conf.chroma_width = (avctx->width + 3) >> 2;
610  ctx->pic_conf.chroma_height = (avctx->height + 3) >> 2;
611  ctx->pic_conf.tile_width = avctx->width;
612  ctx->pic_conf.tile_height = avctx->height;
613  ctx->pic_conf.luma_bands = ctx->pic_conf.chroma_bands = 1;
614 
615  result = ff_ivi_init_planes(ctx->planes, &ctx->pic_conf);
616  if (result) {
617  av_log(avctx, AV_LOG_ERROR, "Couldn't allocate color planes!\n");
618  return -1;
619  }
620 
621  ctx->buf_switch = 0;
622  ctx->inter_scal = 0;
623 
629 
630  avctx->pix_fmt = PIX_FMT_YUV410P;
631 
632  return 0;
633 }
634 
635 
637  .name = "indeo5",
638  .type = AVMEDIA_TYPE_VIDEO,
639  .id = CODEC_ID_INDEO5,
640  .priv_data_size = sizeof(IVI45DecContext),
641  .init = decode_init,
644  .long_name = NULL_IF_CONFIG_SMALL("Intel Indeo Video Interactive 5"),
645 };