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