]> git.sesse.net Git - ffmpeg/blob - libavcodec/indeo4.c
Merge commit 'def97856de6021965db86c25a732d78689bd6bb0'
[ffmpeg] / libavcodec / indeo4.c
1 /*
2  * Indeo Video Interactive v4 compatible decoder
3  * Copyright (c) 2009-2011 Maxim Poliakovski
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  * Indeo Video Interactive version 4 decoder
25  *
26  * Indeo 4 data is usually transported within .avi or .mov files.
27  * Known FOURCCs: 'IV41'
28  */
29
30 #define BITSTREAM_READER_LE
31 #include "avcodec.h"
32 #include "get_bits.h"
33 #include "ivi.h"
34 #include "ivi_dsp.h"
35 #include "indeo4data.h"
36 #include "internal.h"
37
38 #define IVI4_PIC_SIZE_ESC   7
39
40
41 static const struct {
42     InvTransformPtr *inv_trans;
43     DCTransformPtr  *dc_trans;
44     int             is_2d_trans;
45 } transforms[18] = {
46     { ff_ivi_inverse_haar_8x8,  ff_ivi_dc_haar_2d,       1 },
47     { ff_ivi_row_haar8,         ff_ivi_dc_haar_2d,       0 },
48     { ff_ivi_col_haar8,         ff_ivi_dc_haar_2d,       0 },
49     { ff_ivi_put_pixels_8x8,    ff_ivi_put_dc_pixel_8x8, 1 },
50     { ff_ivi_inverse_slant_8x8, ff_ivi_dc_slant_2d,      1 },
51     { ff_ivi_row_slant8,        ff_ivi_dc_row_slant,     1 },
52     { ff_ivi_col_slant8,        ff_ivi_dc_col_slant,     1 },
53     { NULL, NULL, 0 }, /* inverse DCT 8x8 */
54     { NULL, NULL, 0 }, /* inverse DCT 8x1 */
55     { NULL, NULL, 0 }, /* inverse DCT 1x8 */
56     { ff_ivi_inverse_haar_4x4,  ff_ivi_dc_haar_2d,       1 },
57     { ff_ivi_inverse_slant_4x4, ff_ivi_dc_slant_2d,      1 },
58     { NULL, NULL, 0 }, /* no transform 4x4 */
59     { ff_ivi_row_haar4,         ff_ivi_dc_haar_2d,       0 },
60     { ff_ivi_col_haar4,         ff_ivi_dc_haar_2d,       0 },
61     { ff_ivi_row_slant4,        ff_ivi_dc_row_slant,     0 },
62     { ff_ivi_col_slant4,        ff_ivi_dc_col_slant,     0 },
63     { NULL, NULL, 0 }, /* inverse DCT 4x4 */
64 };
65
66 /**
67  *  Decode subdivision of a plane.
68  *  This is a simplified version that checks for two supported subdivisions:
69  *  - 1 wavelet band  per plane, size factor 1:1, code pattern: 3
70  *  - 4 wavelet bands per plane, size factor 1:4, code pattern: 2,3,3,3,3
71  *  Anything else is either unsupported or corrupt.
72  *
73  *  @param[in,out] gb    the GetBit context
74  *  @return        number of wavelet bands or 0 on error
75  */
76 static int decode_plane_subdivision(GetBitContext *gb)
77 {
78     int i;
79
80     switch (get_bits(gb, 2)) {
81     case 3:
82         return 1;
83     case 2:
84         for (i = 0; i < 4; i++)
85             if (get_bits(gb, 2) != 3)
86                 return 0;
87         return 4;
88     default:
89         return 0;
90     }
91 }
92
93 static inline int scale_tile_size(int def_size, int size_factor)
94 {
95     return size_factor == 15 ? def_size : (size_factor + 1) << 5;
96 }
97
98 /**
99  *  Decode Indeo 4 picture header.
100  *
101  *  @param[in,out] ctx       pointer to the decoder context
102  *  @param[in]     avctx     pointer to the AVCodecContext
103  *  @return        result code: 0 = OK, negative number = error
104  */
105 static int decode_pic_hdr(IVI45DecContext *ctx, AVCodecContext *avctx)
106 {
107     int             pic_size_indx, i, p;
108     IVIPicConfig    pic_conf;
109
110     if (get_bits(&ctx->gb, 18) != 0x3FFF8) {
111         av_log(avctx, AV_LOG_ERROR, "Invalid picture start code!\n");
112         return AVERROR_INVALIDDATA;
113     }
114
115     ctx->prev_frame_type = ctx->frame_type;
116     ctx->frame_type      = get_bits(&ctx->gb, 3);
117     if (ctx->frame_type == 7) {
118         av_log(avctx, AV_LOG_ERROR, "Invalid frame type: %d\n", ctx->frame_type);
119         return AVERROR_INVALIDDATA;
120     }
121
122 #if IVI4_STREAM_ANALYSER
123     if (ctx->frame_type == IVI4_FRAMETYPE_BIDIR)
124         ctx->has_b_frames = 1;
125 #endif
126
127     ctx->transp_status = get_bits1(&ctx->gb);
128 #if IVI4_STREAM_ANALYSER
129     if (ctx->transp_status) {
130         ctx->has_transp = 1;
131     }
132 #endif
133
134     /* unknown bit: Mac decoder ignores this bit, XANIM returns error */
135     if (get_bits1(&ctx->gb)) {
136         av_log(avctx, AV_LOG_ERROR, "Sync bit is set!\n");
137         return AVERROR_INVALIDDATA;
138     }
139
140     ctx->data_size = get_bits1(&ctx->gb) ? get_bits(&ctx->gb, 24) : 0;
141
142     /* null frames don't contain anything else so we just return */
143     if (ctx->frame_type >= IVI4_FRAMETYPE_NULL_FIRST) {
144         ff_dlog(avctx, "Null frame encountered!\n");
145         return 0;
146     }
147
148     /* Check key lock status. If enabled - ignore lock word.         */
149     /* Usually we have to prompt the user for the password, but      */
150     /* we don't do that because Indeo 4 videos can be decoded anyway */
151     if (get_bits1(&ctx->gb)) {
152         skip_bits_long(&ctx->gb, 32);
153         ff_dlog(avctx, "Password-protected clip!\n");
154     }
155
156     pic_size_indx = get_bits(&ctx->gb, 3);
157     if (pic_size_indx == IVI4_PIC_SIZE_ESC) {
158         pic_conf.pic_height = get_bits(&ctx->gb, 16);
159         pic_conf.pic_width  = get_bits(&ctx->gb, 16);
160     } else {
161         pic_conf.pic_height = ivi4_common_pic_sizes[pic_size_indx * 2 + 1];
162         pic_conf.pic_width  = ivi4_common_pic_sizes[pic_size_indx * 2    ];
163     }
164
165     /* Decode tile dimensions. */
166     if (get_bits1(&ctx->gb)) {
167         pic_conf.tile_height = scale_tile_size(pic_conf.pic_height, get_bits(&ctx->gb, 4));
168         pic_conf.tile_width  = scale_tile_size(pic_conf.pic_width,  get_bits(&ctx->gb, 4));
169 #if IVI4_STREAM_ANALYSER
170         ctx->uses_tiling = 1;
171 #endif
172     } else {
173         pic_conf.tile_height = pic_conf.pic_height;
174         pic_conf.tile_width  = pic_conf.pic_width;
175     }
176
177     /* Decode chroma subsampling. We support only 4:4 aka YVU9. */
178     if (get_bits(&ctx->gb, 2)) {
179         av_log(avctx, AV_LOG_ERROR, "Only YVU9 picture format is supported!\n");
180         return AVERROR_INVALIDDATA;
181     }
182     pic_conf.chroma_height = (pic_conf.pic_height + 3) >> 2;
183     pic_conf.chroma_width  = (pic_conf.pic_width  + 3) >> 2;
184
185     /* decode subdivision of the planes */
186     pic_conf.luma_bands = decode_plane_subdivision(&ctx->gb);
187     pic_conf.chroma_bands = 0;
188     if (pic_conf.luma_bands)
189         pic_conf.chroma_bands = decode_plane_subdivision(&ctx->gb);
190     ctx->is_scalable = pic_conf.luma_bands != 1 || pic_conf.chroma_bands != 1;
191     if (ctx->is_scalable && (pic_conf.luma_bands != 4 || pic_conf.chroma_bands != 1)) {
192         av_log(avctx, AV_LOG_ERROR, "Scalability: unsupported subdivision! Luma bands: %d, chroma bands: %d\n",
193                pic_conf.luma_bands, pic_conf.chroma_bands);
194         return AVERROR_INVALIDDATA;
195     }
196
197     /* check if picture layout was changed and reallocate buffers */
198     if (ivi_pic_config_cmp(&pic_conf, &ctx->pic_conf)) {
199         if (ff_ivi_init_planes(ctx->planes, &pic_conf, 1)) {
200             av_log(avctx, AV_LOG_ERROR, "Couldn't reallocate color planes!\n");
201             ctx->pic_conf.luma_bands = 0;
202             return AVERROR(ENOMEM);
203         }
204
205         ctx->pic_conf = pic_conf;
206
207         /* set default macroblock/block dimensions */
208         for (p = 0; p <= 2; p++) {
209             for (i = 0; i < (!p ? pic_conf.luma_bands : pic_conf.chroma_bands); i++) {
210                 ctx->planes[p].bands[i].mb_size  = !p ? (!ctx->is_scalable ? 16 : 8) : 4;
211                 ctx->planes[p].bands[i].blk_size = !p ? 8 : 4;
212             }
213         }
214
215         if (ff_ivi_init_tiles(ctx->planes, ctx->pic_conf.tile_width,
216                               ctx->pic_conf.tile_height)) {
217             av_log(avctx, AV_LOG_ERROR,
218                    "Couldn't reallocate internal structures!\n");
219             return AVERROR(ENOMEM);
220         }
221     }
222
223     ctx->frame_num = get_bits1(&ctx->gb) ? get_bits(&ctx->gb, 20) : 0;
224
225     /* skip decTimeEst field if present */
226     if (get_bits1(&ctx->gb))
227         skip_bits(&ctx->gb, 8);
228
229     /* decode macroblock and block huffman codebooks */
230     if (ff_ivi_dec_huff_desc(&ctx->gb, get_bits1(&ctx->gb), IVI_MB_HUFF,  &ctx->mb_vlc,  avctx) ||
231         ff_ivi_dec_huff_desc(&ctx->gb, get_bits1(&ctx->gb), IVI_BLK_HUFF, &ctx->blk_vlc, avctx))
232         return AVERROR_INVALIDDATA;
233
234     ctx->rvmap_sel = get_bits1(&ctx->gb) ? get_bits(&ctx->gb, 3) : 8;
235
236     ctx->in_imf = get_bits1(&ctx->gb);
237     ctx->in_q   = get_bits1(&ctx->gb);
238
239     ctx->pic_glob_quant = get_bits(&ctx->gb, 5);
240
241     /* TODO: ignore this parameter if unused */
242     ctx->unknown1 = get_bits1(&ctx->gb) ? get_bits(&ctx->gb, 3) : 0;
243
244     ctx->checksum = get_bits1(&ctx->gb) ? get_bits(&ctx->gb, 16) : 0;
245
246     /* skip picture header extension if any */
247     while (get_bits1(&ctx->gb)) {
248         ff_dlog(avctx, "Pic hdr extension encountered!\n");
249         skip_bits(&ctx->gb, 8);
250     }
251
252     if (get_bits1(&ctx->gb)) {
253         av_log(avctx, AV_LOG_ERROR, "Bad blocks bits encountered!\n");
254     }
255
256     align_get_bits(&ctx->gb);
257
258     return 0;
259 }
260
261
262 /**
263  *  Decode Indeo 4 band header.
264  *
265  *  @param[in,out] ctx       pointer to the decoder context
266  *  @param[in,out] band      pointer to the band descriptor
267  *  @param[in]     avctx     pointer to the AVCodecContext
268  *  @return        result code: 0 = OK, negative number = error
269  */
270 static int decode_band_hdr(IVI45DecContext *ctx, IVIBandDesc *band,
271                            AVCodecContext *avctx)
272 {
273     int plane, band_num, indx, transform_id, scan_indx;
274     int i;
275     int quant_mat;
276
277     plane    = get_bits(&ctx->gb, 2);
278     band_num = get_bits(&ctx->gb, 4);
279     if (band->plane != plane || band->band_num != band_num) {
280         av_log(avctx, AV_LOG_ERROR, "Invalid band header sequence!\n");
281         return AVERROR_INVALIDDATA;
282     }
283
284     band->is_empty = get_bits1(&ctx->gb);
285     if (!band->is_empty) {
286         int old_blk_size = band->blk_size;
287         /* skip header size
288          * If header size is not given, header size is 4 bytes. */
289         if (get_bits1(&ctx->gb))
290             skip_bits(&ctx->gb, 16);
291
292         band->is_halfpel = get_bits(&ctx->gb, 2);
293         if (band->is_halfpel >= 2) {
294             av_log(avctx, AV_LOG_ERROR, "Invalid/unsupported mv resolution: %d!\n",
295                    band->is_halfpel);
296             return AVERROR_INVALIDDATA;
297         }
298 #if IVI4_STREAM_ANALYSER
299         if (!band->is_halfpel)
300             ctx->uses_fullpel = 1;
301 #endif
302
303         band->checksum_present = get_bits1(&ctx->gb);
304         if (band->checksum_present)
305             band->checksum = get_bits(&ctx->gb, 16);
306
307         indx = get_bits(&ctx->gb, 2);
308         if (indx == 3) {
309             av_log(avctx, AV_LOG_ERROR, "Invalid block size!\n");
310             return AVERROR_INVALIDDATA;
311         }
312         band->mb_size  = 16 >> indx;
313         band->blk_size = 8 >> (indx >> 1);
314
315         band->inherit_mv     = get_bits1(&ctx->gb);
316         band->inherit_qdelta = get_bits1(&ctx->gb);
317
318         band->glob_quant = get_bits(&ctx->gb, 5);
319
320         if (!get_bits1(&ctx->gb) || ctx->frame_type == IVI4_FRAMETYPE_INTRA) {
321             transform_id = get_bits(&ctx->gb, 5);
322             if (transform_id >= FF_ARRAY_ELEMS(transforms) ||
323                 !transforms[transform_id].inv_trans) {
324                 avpriv_request_sample(avctx, "Transform %d", transform_id);
325                 return AVERROR_PATCHWELCOME;
326             }
327             if ((transform_id >= 7 && transform_id <= 9) ||
328                  transform_id == 17) {
329                 avpriv_request_sample(avctx, "DCT transform");
330                 return AVERROR_PATCHWELCOME;
331             }
332
333             if (transform_id < 10 && band->blk_size < 8) {
334                 av_log(avctx, AV_LOG_ERROR, "wrong transform size!\n");
335                 return AVERROR_INVALIDDATA;
336             }
337 #if IVI4_STREAM_ANALYSER
338             if ((transform_id >= 0 && transform_id <= 2) || transform_id == 10)
339                 ctx->uses_haar = 1;
340 #endif
341
342             band->inv_transform = transforms[transform_id].inv_trans;
343             band->dc_transform  = transforms[transform_id].dc_trans;
344             band->is_2d_trans   = transforms[transform_id].is_2d_trans;
345
346             if (transform_id < 10)
347                 band->transform_size = 8;
348             else
349                 band->transform_size = 4;
350
351             if (band->blk_size != band->transform_size) {
352                 av_log(avctx, AV_LOG_ERROR, "transform and block size mismatch (%d != %d)\n", band->transform_size, band->blk_size);
353                 return AVERROR_INVALIDDATA;
354             }
355
356             scan_indx = get_bits(&ctx->gb, 4);
357             if (scan_indx == 15) {
358                 av_log(avctx, AV_LOG_ERROR, "Custom scan pattern encountered!\n");
359                 return AVERROR_INVALIDDATA;
360             }
361             if (scan_indx > 4 && scan_indx < 10) {
362                 if (band->blk_size != 4) {
363                     av_log(avctx, AV_LOG_ERROR, "mismatching scan table!\n");
364                     return AVERROR_INVALIDDATA;
365                 }
366             } else if (band->blk_size != 8) {
367                 av_log(avctx, AV_LOG_ERROR, "mismatching scan table!\n");
368                 return AVERROR_INVALIDDATA;
369             }
370
371             band->scan = scan_index_to_tab[scan_indx];
372             band->scan_size = band->blk_size;
373
374             quant_mat = get_bits(&ctx->gb, 5);
375             if (quant_mat == 31) {
376                 av_log(avctx, AV_LOG_ERROR, "Custom quant matrix encountered!\n");
377                 return AVERROR_INVALIDDATA;
378             }
379             if (quant_mat >= FF_ARRAY_ELEMS(quant_index_to_tab)) {
380                 avpriv_request_sample(avctx, "Quantization matrix %d",
381                                       quant_mat);
382                 return AVERROR_INVALIDDATA;
383             }
384             band->quant_mat = quant_mat;
385         } else {
386             if (old_blk_size != band->blk_size) {
387                 av_log(avctx, AV_LOG_ERROR,
388                        "The band block size does not match the configuration "
389                        "inherited\n");
390                 return AVERROR_INVALIDDATA;
391             }
392         }
393         if (quant_index_to_tab[band->quant_mat] > 4 && band->blk_size == 4) {
394             av_log(avctx, AV_LOG_ERROR, "Invalid quant matrix for 4x4 block encountered!\n");
395             band->quant_mat = 0;
396             return AVERROR_INVALIDDATA;
397         }
398         if (band->scan_size != band->blk_size) {
399             av_log(avctx, AV_LOG_ERROR, "mismatching scan table!\n");
400             return AVERROR_INVALIDDATA;
401         }
402         if (band->transform_size == 8 && band->blk_size < 8) {
403             av_log(avctx, AV_LOG_ERROR, "mismatching transform_size!\n");
404             return AVERROR_INVALIDDATA;
405         }
406
407         /* decode block huffman codebook */
408         if (!get_bits1(&ctx->gb))
409             band->blk_vlc.tab = ctx->blk_vlc.tab;
410         else
411             if (ff_ivi_dec_huff_desc(&ctx->gb, 1, IVI_BLK_HUFF,
412                                      &band->blk_vlc, avctx))
413                 return AVERROR_INVALIDDATA;
414
415         /* select appropriate rvmap table for this band */
416         band->rvmap_sel = get_bits1(&ctx->gb) ? get_bits(&ctx->gb, 3) : 8;
417
418         /* decode rvmap probability corrections if any */
419         band->num_corr = 0; /* there is no corrections */
420         if (get_bits1(&ctx->gb)) {
421             band->num_corr = get_bits(&ctx->gb, 8); /* get number of correction pairs */
422             if (band->num_corr > 61) {
423                 av_log(avctx, AV_LOG_ERROR, "Too many corrections: %d\n",
424                        band->num_corr);
425                 return AVERROR_INVALIDDATA;
426             }
427
428             /* read correction pairs */
429             for (i = 0; i < band->num_corr * 2; i++)
430                 band->corr[i] = get_bits(&ctx->gb, 8);
431         }
432     }
433
434     if (band->blk_size == 8) {
435         band->intra_base = &ivi4_quant_8x8_intra[quant_index_to_tab[band->quant_mat]][0];
436         band->inter_base = &ivi4_quant_8x8_inter[quant_index_to_tab[band->quant_mat]][0];
437     } else {
438         band->intra_base = &ivi4_quant_4x4_intra[quant_index_to_tab[band->quant_mat]][0];
439         band->inter_base = &ivi4_quant_4x4_inter[quant_index_to_tab[band->quant_mat]][0];
440     }
441
442     /* Indeo 4 doesn't use scale tables */
443     band->intra_scale = NULL;
444     band->inter_scale = NULL;
445
446     align_get_bits(&ctx->gb);
447
448     if (!band->scan) {
449         av_log(avctx, AV_LOG_ERROR, "band->scan not set\n");
450         return AVERROR_INVALIDDATA;
451     }
452
453     return 0;
454 }
455
456
457 /**
458  *  Decode information (block type, cbp, quant delta, motion vector)
459  *  for all macroblocks in the current tile.
460  *
461  *  @param[in,out] ctx       pointer to the decoder context
462  *  @param[in,out] band      pointer to the band descriptor
463  *  @param[in,out] tile      pointer to the tile descriptor
464  *  @param[in]     avctx     pointer to the AVCodecContext
465  *  @return        result code: 0 = OK, negative number = error
466  */
467 static int decode_mb_info(IVI45DecContext *ctx, IVIBandDesc *band,
468                           IVITile *tile, AVCodecContext *avctx)
469 {
470     int         x, y, mv_x, mv_y, mv_delta, offs, mb_offset, blks_per_mb,
471                 mv_scale, mb_type_bits, s;
472     IVIMbInfo   *mb, *ref_mb;
473     int         row_offset = band->mb_size * band->pitch;
474
475     mb     = tile->mbs;
476     ref_mb = tile->ref_mbs;
477     offs   = tile->ypos * band->pitch + tile->xpos;
478
479     blks_per_mb  = band->mb_size   != band->blk_size  ? 4 : 1;
480     mb_type_bits = ctx->frame_type == IVI4_FRAMETYPE_BIDIR ? 2 : 1;
481
482     /* scale factor for motion vectors */
483     mv_scale = (ctx->planes[0].bands[0].mb_size >> 3) - (band->mb_size >> 3);
484     mv_x = mv_y = 0;
485
486     if (((tile->width + band->mb_size-1)/band->mb_size) * ((tile->height + band->mb_size-1)/band->mb_size) != tile->num_MBs) {
487         av_log(avctx, AV_LOG_ERROR, "num_MBs mismatch %d %d %d %d\n", tile->width, tile->height, band->mb_size, tile->num_MBs);
488         return -1;
489     }
490
491     for (y = tile->ypos; y < tile->ypos + tile->height; y += band->mb_size) {
492         mb_offset = offs;
493
494         for (x = tile->xpos; x < tile->xpos + tile->width; x += band->mb_size) {
495             mb->xpos     = x;
496             mb->ypos     = y;
497             mb->buf_offs = mb_offset;
498             mb->b_mv_x   =
499             mb->b_mv_y   = 0;
500
501             if (get_bits1(&ctx->gb)) {
502                 if (ctx->frame_type == IVI4_FRAMETYPE_INTRA) {
503                     av_log(avctx, AV_LOG_ERROR, "Empty macroblock in an INTRA picture!\n");
504                     return AVERROR_INVALIDDATA;
505                 }
506                 mb->type = 1; /* empty macroblocks are always INTER */
507                 mb->cbp  = 0; /* all blocks are empty */
508
509                 mb->q_delta = 0;
510                 if (!band->plane && !band->band_num && ctx->in_q) {
511                     mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
512                                            IVI_VLC_BITS, 1);
513                     mb->q_delta = IVI_TOSIGNED(mb->q_delta);
514                 }
515
516                 mb->mv_x = mb->mv_y = 0; /* no motion vector coded */
517                 if (band->inherit_mv && ref_mb) {
518                     /* motion vector inheritance */
519                     if (mv_scale) {
520                         mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale);
521                         mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale);
522                     } else {
523                         mb->mv_x = ref_mb->mv_x;
524                         mb->mv_y = ref_mb->mv_y;
525                     }
526                 }
527             } else {
528                 if (band->inherit_mv) {
529                     /* copy mb_type from corresponding reference mb */
530                     if (!ref_mb) {
531                         av_log(avctx, AV_LOG_ERROR, "ref_mb unavailable\n");
532                         return AVERROR_INVALIDDATA;
533                     }
534                     mb->type = ref_mb->type;
535                 } else if (ctx->frame_type == IVI4_FRAMETYPE_INTRA ||
536                            ctx->frame_type == IVI4_FRAMETYPE_INTRA1) {
537                     mb->type = 0; /* mb_type is always INTRA for intra-frames */
538                 } else {
539                     mb->type = get_bits(&ctx->gb, mb_type_bits);
540                 }
541
542                 mb->cbp = get_bits(&ctx->gb, blks_per_mb);
543
544                 mb->q_delta = 0;
545                 if (band->inherit_qdelta) {
546                     if (ref_mb) mb->q_delta = ref_mb->q_delta;
547                 } else if (mb->cbp || (!band->plane && !band->band_num &&
548                            ctx->in_q)) {
549                     mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
550                                            IVI_VLC_BITS, 1);
551                     mb->q_delta = IVI_TOSIGNED(mb->q_delta);
552                 }
553
554                 if (!mb->type) {
555                     mb->mv_x = mb->mv_y = 0; /* there is no motion vector in intra-macroblocks */
556                 } else {
557                     if (band->inherit_mv) {
558                         if (ref_mb)
559                             /* motion vector inheritance */
560                             if (mv_scale) {
561                                 mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale);
562                                 mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale);
563                             } else {
564                                 mb->mv_x = ref_mb->mv_x;
565                                 mb->mv_y = ref_mb->mv_y;
566                             }
567                     } else {
568                         /* decode motion vector deltas */
569                         mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
570                                             IVI_VLC_BITS, 1);
571                         mv_y += IVI_TOSIGNED(mv_delta);
572                         mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
573                                             IVI_VLC_BITS, 1);
574                         mv_x += IVI_TOSIGNED(mv_delta);
575                         mb->mv_x = mv_x;
576                         mb->mv_y = mv_y;
577                         if (mb->type == 3) {
578                             mv_delta = get_vlc2(&ctx->gb,
579                                                 ctx->mb_vlc.tab->table,
580                                                 IVI_VLC_BITS, 1);
581                             mv_y += IVI_TOSIGNED(mv_delta);
582                             mv_delta = get_vlc2(&ctx->gb,
583                                                 ctx->mb_vlc.tab->table,
584                                                 IVI_VLC_BITS, 1);
585                             mv_x += IVI_TOSIGNED(mv_delta);
586                             mb->b_mv_x = -mv_x;
587                             mb->b_mv_y = -mv_y;
588                         }
589                     }
590                     if (mb->type == 2) {
591                         mb->b_mv_x = -mb->mv_x;
592                         mb->b_mv_y = -mb->mv_y;
593                         mb->mv_x = 0;
594                         mb->mv_y = 0;
595                     }
596                 }
597             }
598
599             s= band->is_halfpel;
600             if (mb->type)
601             if ( x +  (mb->mv_x   >>s) +                 (y+               (mb->mv_y   >>s))*band->pitch < 0 ||
602                  x + ((mb->mv_x+s)>>s) + band->mb_size - 1
603                    + (y+band->mb_size - 1 +((mb->mv_y+s)>>s))*band->pitch > band->bufsize -1) {
604                 av_log(avctx, AV_LOG_ERROR, "motion vector %d %d outside reference\n", x*s + mb->mv_x, y*s + mb->mv_y);
605                 return AVERROR_INVALIDDATA;
606             }
607
608             mb++;
609             if (ref_mb)
610                 ref_mb++;
611             mb_offset += band->mb_size;
612         }
613
614         offs += row_offset;
615     }
616
617     align_get_bits(&ctx->gb);
618
619     return 0;
620 }
621
622
623 /**
624  *  Rearrange decoding and reference buffers.
625  *
626  *  @param[in,out] ctx       pointer to the decoder context
627  */
628 static void switch_buffers(IVI45DecContext *ctx)
629 {
630     int is_prev_ref = 0, is_ref = 0;
631
632     switch (ctx->prev_frame_type) {
633     case IVI4_FRAMETYPE_INTRA:
634     case IVI4_FRAMETYPE_INTRA1:
635     case IVI4_FRAMETYPE_INTER:
636         is_prev_ref = 1;
637         break;
638     }
639
640     switch (ctx->frame_type) {
641     case IVI4_FRAMETYPE_INTRA:
642     case IVI4_FRAMETYPE_INTRA1:
643     case IVI4_FRAMETYPE_INTER:
644         is_ref = 1;
645         break;
646     }
647
648     if (is_prev_ref && is_ref) {
649         FFSWAP(int, ctx->dst_buf, ctx->ref_buf);
650     } else if (is_prev_ref) {
651         FFSWAP(int, ctx->ref_buf, ctx->b_ref_buf);
652         FFSWAP(int, ctx->dst_buf, ctx->ref_buf);
653     }
654 }
655
656
657 static int is_nonnull_frame(IVI45DecContext *ctx)
658 {
659     return ctx->frame_type < IVI4_FRAMETYPE_NULL_FIRST;
660 }
661
662
663 static av_cold int decode_init(AVCodecContext *avctx)
664 {
665     IVI45DecContext *ctx = avctx->priv_data;
666
667     ff_ivi_init_static_vlc();
668
669     /* copy rvmap tables in our context so we can apply changes to them */
670     memcpy(ctx->rvmap_tabs, ff_ivi_rvmap_tabs, sizeof(ff_ivi_rvmap_tabs));
671
672     /* Force allocation of the internal buffers */
673     /* during picture header decoding.          */
674     ctx->pic_conf.pic_width  = 0;
675     ctx->pic_conf.pic_height = 0;
676
677     avctx->pix_fmt = AV_PIX_FMT_YUV410P;
678
679     ctx->decode_pic_hdr   = decode_pic_hdr;
680     ctx->decode_band_hdr  = decode_band_hdr;
681     ctx->decode_mb_info   = decode_mb_info;
682     ctx->switch_buffers   = switch_buffers;
683     ctx->is_nonnull_frame = is_nonnull_frame;
684
685     ctx->is_indeo4 = 1;
686
687     ctx->dst_buf   = 0;
688     ctx->ref_buf   = 1;
689     ctx->b_ref_buf = 3; /* buffer 2 is used for scalability mode */
690     ctx->p_frame = av_frame_alloc();
691     if (!ctx->p_frame)
692         return AVERROR(ENOMEM);
693
694     return 0;
695 }
696
697
698 AVCodec ff_indeo4_decoder = {
699     .name           = "indeo4",
700     .long_name      = NULL_IF_CONFIG_SMALL("Intel Indeo Video Interactive 4"),
701     .type           = AVMEDIA_TYPE_VIDEO,
702     .id             = AV_CODEC_ID_INDEO4,
703     .priv_data_size = sizeof(IVI45DecContext),
704     .init           = decode_init,
705     .close          = ff_ivi_decode_close,
706     .decode         = ff_ivi_decode_frame,
707     .capabilities   = AV_CODEC_CAP_DR1,
708 };