]> git.sesse.net Git - ffmpeg/blob - libavcodec/indeo5.c
Merge commit 'f919cc7df6ab844bc12f89fe7bef4fb915a47725'
[ffmpeg] / libavcodec / indeo5.c
1 /*
2  * Indeo Video Interactive v5 compatible decoder
3  * Copyright (c) 2009 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 5 decoder
25  *
26  * Indeo5 data is usually transported within .avi or .mov files.
27  * Known FOURCCs: 'IV50'
28  */
29
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
38 /**
39  *  Indeo5 frame types.
40  */
41 enum {
42     FRAMETYPE_INTRA       = 0,
43     FRAMETYPE_INTER       = 1,  ///< non-droppable P-frame
44     FRAMETYPE_INTER_SCAL  = 2,  ///< droppable P-frame used in the scalability mode
45     FRAMETYPE_INTER_NOREF = 3,  ///< droppable P-frame
46     FRAMETYPE_NULL        = 4   ///< empty frame with no data
47 };
48
49 #define IVI5_PIC_SIZE_ESC       15
50
51 /**
52  *  Decode Indeo5 GOP (Group of pictures) header.
53  *  This header is present in key frames only.
54  *  It defines parameters for all frames in a GOP.
55  *
56  *  @param[in,out] ctx    ptr to the decoder context
57  *  @param[in]     avctx  ptr to the AVCodecContext
58  *  @return         result code: 0 = OK, -1 = error
59  */
60 static int decode_gop_header(IVI45DecContext *ctx, AVCodecContext *avctx)
61 {
62     int             result, i, p, tile_size, pic_size_indx, mb_size, blk_size, is_scalable;
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     is_scalable = pic_conf.luma_bands != 1 || pic_conf.chroma_bands != 1;
85     if (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         ctx->is_scalable = is_scalable;
124         blk_size_changed = 1; /* force reallocation of the internal structures */
125     }
126
127     for (p = 0; p <= 1; p++) {
128         for (i = 0; i < (!p ? pic_conf.luma_bands : pic_conf.chroma_bands); i++) {
129             band = &ctx->planes[p].bands[i];
130
131             band->is_halfpel = get_bits1(&ctx->gb);
132
133             mb_size  = get_bits1(&ctx->gb);
134             blk_size = 8 >> get_bits1(&ctx->gb);
135             mb_size  = blk_size << !mb_size;
136
137             if (p==0 && blk_size==4) {
138                 av_log(avctx, AV_LOG_ERROR, "4x4 luma blocks are unsupported!\n");
139                 return AVERROR_PATCHWELCOME;
140             }
141
142             blk_size_changed = mb_size != band->mb_size || blk_size != band->blk_size;
143             if (blk_size_changed) {
144                 band->mb_size  = mb_size;
145                 band->blk_size = blk_size;
146             }
147
148             if (get_bits1(&ctx->gb)) {
149                 av_log(avctx, AV_LOG_ERROR, "Extended transform info encountered!\n");
150                 return -1;
151             }
152
153             /* select transform function and scan pattern according to plane and band number */
154             switch ((p << 2) + i) {
155             case 0:
156                 band->inv_transform = ff_ivi_inverse_slant_8x8;
157                 band->dc_transform  = ff_ivi_dc_slant_2d;
158                 band->scan          = ff_zigzag_direct;
159                 band->transform_size= 8;
160                 break;
161
162             case 1:
163                 band->inv_transform = ff_ivi_row_slant8;
164                 band->dc_transform  = ff_ivi_dc_row_slant;
165                 band->scan          = ff_ivi_vertical_scan_8x8;
166                 band->transform_size= 8;
167                 break;
168
169             case 2:
170                 band->inv_transform = ff_ivi_col_slant8;
171                 band->dc_transform  = ff_ivi_dc_col_slant;
172                 band->scan          = ff_ivi_horizontal_scan_8x8;
173                 band->transform_size= 8;
174                 break;
175
176             case 3:
177                 band->inv_transform = ff_ivi_put_pixels_8x8;
178                 band->dc_transform  = ff_ivi_put_dc_pixel_8x8;
179                 band->scan          = ff_ivi_horizontal_scan_8x8;
180                 band->transform_size= 8;
181                 break;
182
183             case 4:
184                 band->inv_transform = ff_ivi_inverse_slant_4x4;
185                 band->dc_transform  = ff_ivi_dc_slant_2d;
186                 band->scan          = ff_ivi_direct_scan_4x4;
187                 band->transform_size= 4;
188                 break;
189             }
190
191             band->is_2d_trans = band->inv_transform == ff_ivi_inverse_slant_8x8 ||
192                                 band->inv_transform == ff_ivi_inverse_slant_4x4;
193
194             /* select dequant matrix according to plane and band number */
195             if (!p) {
196                 quant_mat = (pic_conf.luma_bands > 1) ? i+1 : 0;
197             } else {
198                 quant_mat = 5;
199             }
200
201             if (band->blk_size == 8) {
202                 band->intra_base  = &ivi5_base_quant_8x8_intra[quant_mat][0];
203                 band->inter_base  = &ivi5_base_quant_8x8_inter[quant_mat][0];
204                 band->intra_scale = &ivi5_scale_quant_8x8_intra[quant_mat][0];
205                 band->inter_scale = &ivi5_scale_quant_8x8_inter[quant_mat][0];
206             } else {
207                 band->intra_base  = ivi5_base_quant_4x4_intra;
208                 band->inter_base  = ivi5_base_quant_4x4_inter;
209                 band->intra_scale = ivi5_scale_quant_4x4_intra;
210                 band->inter_scale = ivi5_scale_quant_4x4_inter;
211             }
212
213             if (get_bits(&ctx->gb, 2)) {
214                 av_log(avctx, AV_LOG_ERROR, "End marker missing!\n");
215                 return -1;
216             }
217         }
218     }
219
220     /* copy chroma parameters into the 2nd chroma plane */
221     for (i = 0; i < pic_conf.chroma_bands; i++) {
222         band1 = &ctx->planes[1].bands[i];
223         band2 = &ctx->planes[2].bands[i];
224
225         band2->width         = band1->width;
226         band2->height        = band1->height;
227         band2->mb_size       = band1->mb_size;
228         band2->blk_size      = band1->blk_size;
229         band2->is_halfpel    = band1->is_halfpel;
230         band2->intra_base    = band1->intra_base;
231         band2->inter_base    = band1->inter_base;
232         band2->intra_scale   = band1->intra_scale;
233         band2->inter_scale   = band1->inter_scale;
234         band2->scan          = band1->scan;
235         band2->inv_transform = band1->inv_transform;
236         band2->dc_transform  = band1->dc_transform;
237         band2->is_2d_trans   = band1->is_2d_trans;
238         band2->transform_size= band1->transform_size;
239     }
240
241     /* reallocate internal structures if needed */
242     if (blk_size_changed) {
243         result = ff_ivi_init_tiles(ctx->planes, pic_conf.tile_width,
244                                    pic_conf.tile_height);
245         if (result) {
246             av_log(avctx, AV_LOG_ERROR,
247                    "Couldn't reallocate internal structures!\n");
248             return -1;
249         }
250     }
251
252     if (ctx->gop_flags & 8) {
253         if (get_bits(&ctx->gb, 3)) {
254             av_log(avctx, AV_LOG_ERROR, "Alignment bits are not zero!\n");
255             return -1;
256         }
257
258         if (get_bits1(&ctx->gb))
259             skip_bits_long(&ctx->gb, 24); /* skip transparency fill color */
260     }
261
262     align_get_bits(&ctx->gb);
263
264     skip_bits(&ctx->gb, 23); /* FIXME: unknown meaning */
265
266     /* skip GOP extension if any */
267     if (get_bits1(&ctx->gb)) {
268         do {
269             i = get_bits(&ctx->gb, 16);
270         } while (i & 0x8000);
271     }
272
273     align_get_bits(&ctx->gb);
274
275     return 0;
276 }
277
278
279 /**
280  *  Skip a header extension.
281  *
282  *  @param[in,out]  gb  the GetBit context
283  */
284 static inline void skip_hdr_extension(GetBitContext *gb)
285 {
286     int i, len;
287
288     do {
289         len = get_bits(gb, 8);
290         for (i = 0; i < len; i++) skip_bits(gb, 8);
291     } while(len);
292 }
293
294
295 /**
296  *  Decode Indeo5 picture header.
297  *
298  *  @param[in,out]  ctx    ptr to the decoder context
299  *  @param[in]      avctx  ptr to the AVCodecContext
300  *  @return         result code: 0 = OK, -1 = error
301  */
302 static int decode_pic_hdr(IVI45DecContext *ctx, AVCodecContext *avctx)
303 {
304     if (get_bits(&ctx->gb, 5) != 0x1F) {
305         av_log(avctx, AV_LOG_ERROR, "Invalid picture start code!\n");
306         return -1;
307     }
308
309     ctx->prev_frame_type = ctx->frame_type;
310     ctx->frame_type      = get_bits(&ctx->gb, 3);
311     if (ctx->frame_type >= 5) {
312         av_log(avctx, AV_LOG_ERROR, "Invalid frame type: %d \n", ctx->frame_type);
313         return -1;
314     }
315
316     ctx->frame_num = get_bits(&ctx->gb, 8);
317
318     if (ctx->frame_type == FRAMETYPE_INTRA) {
319         ctx->gop_invalid = 1;
320         if (decode_gop_header(ctx, avctx))
321             return -1;
322         ctx->gop_invalid = 0;
323     }
324
325     if (ctx->frame_type == FRAMETYPE_INTER_SCAL && !ctx->is_scalable) {
326         av_log(avctx, AV_LOG_ERROR, "Scalable inter frame in non scaleable stream\n");
327         ctx->frame_type = FRAMETYPE_INTER;
328         return AVERROR_INVALIDDATA;
329     }
330
331     if (ctx->frame_type != FRAMETYPE_NULL) {
332         ctx->frame_flags = get_bits(&ctx->gb, 8);
333
334         ctx->pic_hdr_size = (ctx->frame_flags & 1) ? get_bits_long(&ctx->gb, 24) : 0;
335
336         ctx->checksum = (ctx->frame_flags & 0x10) ? get_bits(&ctx->gb, 16) : 0;
337
338         /* skip unknown extension if any */
339         if (ctx->frame_flags & 0x20)
340             skip_hdr_extension(&ctx->gb); /* XXX: untested */
341
342         /* decode macroblock huffman codebook */
343         if (ff_ivi_dec_huff_desc(&ctx->gb, ctx->frame_flags & 0x40, IVI_MB_HUFF, &ctx->mb_vlc, avctx))
344             return -1;
345
346         skip_bits(&ctx->gb, 3); /* FIXME: unknown meaning! */
347     }
348
349     align_get_bits(&ctx->gb);
350
351     return 0;
352 }
353
354
355 /**
356  *  Decode Indeo5 band header.
357  *
358  *  @param[in,out]  ctx    ptr to the decoder context
359  *  @param[in,out]  band   ptr to the band descriptor
360  *  @param[in]      avctx  ptr to the AVCodecContext
361  *  @return         result code: 0 = OK, -1 = error
362  */
363 static int decode_band_hdr(IVI45DecContext *ctx, IVIBandDesc *band,
364                            AVCodecContext *avctx)
365 {
366     int         i;
367     uint8_t     band_flags;
368
369     band_flags = get_bits(&ctx->gb, 8);
370
371     if (band_flags & 1) {
372         band->is_empty = 1;
373         return 0;
374     }
375
376     band->data_size = (ctx->frame_flags & 0x80) ? get_bits_long(&ctx->gb, 24) : 0;
377
378     band->inherit_mv     = band_flags & 2;
379     band->inherit_qdelta = band_flags & 8;
380     band->qdelta_present = band_flags & 4;
381     if (!band->qdelta_present) band->inherit_qdelta = 1;
382
383     /* decode rvmap probability corrections if any */
384     band->num_corr = 0; /* there are no corrections */
385     if (band_flags & 0x10) {
386         band->num_corr = get_bits(&ctx->gb, 8); /* get number of correction pairs */
387         if (band->num_corr > 61) {
388             av_log(avctx, AV_LOG_ERROR, "Too many corrections: %d\n",
389                    band->num_corr);
390             return -1;
391         }
392
393         /* read correction pairs */
394         for (i = 0; i < band->num_corr * 2; i++)
395             band->corr[i] = get_bits(&ctx->gb, 8);
396     }
397
398     /* select appropriate rvmap table for this band */
399     band->rvmap_sel = (band_flags & 0x40) ? get_bits(&ctx->gb, 3) : 8;
400
401     /* decode block huffman codebook */
402     if (ff_ivi_dec_huff_desc(&ctx->gb, band_flags & 0x80, IVI_BLK_HUFF, &band->blk_vlc, avctx))
403         return -1;
404
405     band->checksum_present = get_bits1(&ctx->gb);
406     if (band->checksum_present)
407         band->checksum = get_bits(&ctx->gb, 16);
408
409     band->glob_quant = get_bits(&ctx->gb, 5);
410
411     /* skip unknown extension if any */
412     if (band_flags & 0x20) { /* XXX: untested */
413         align_get_bits(&ctx->gb);
414         skip_hdr_extension(&ctx->gb);
415     }
416
417     align_get_bits(&ctx->gb);
418
419     return 0;
420 }
421
422
423 /**
424  *  Decode info (block type, cbp, quant delta, motion vector)
425  *  for all macroblocks in the current tile.
426  *
427  *  @param[in,out]  ctx    ptr to the decoder context
428  *  @param[in,out]  band   ptr to the band descriptor
429  *  @param[in,out]  tile   ptr to the tile descriptor
430  *  @param[in]      avctx  ptr to the AVCodecContext
431  *  @return         result code: 0 = OK, -1 = error
432  */
433 static int decode_mb_info(IVI45DecContext *ctx, IVIBandDesc *band,
434                           IVITile *tile, AVCodecContext *avctx)
435 {
436     int         x, y, mv_x, mv_y, mv_delta, offs, mb_offset,
437                 mv_scale, blks_per_mb, s;
438     IVIMbInfo   *mb, *ref_mb;
439     int         row_offset = band->mb_size * band->pitch;
440
441     mb     = tile->mbs;
442     ref_mb = tile->ref_mbs;
443     offs   = tile->ypos * band->pitch + tile->xpos;
444
445     if (!ref_mb &&
446         ((band->qdelta_present && band->inherit_qdelta) || band->inherit_mv))
447         return AVERROR_INVALIDDATA;
448
449     if( tile->num_MBs != IVI_MBs_PER_TILE(tile->width, tile->height, band->mb_size) ){
450         av_log(avctx, AV_LOG_ERROR, "allocated tile size %d mismatches parameters %d\n",
451                tile->num_MBs, IVI_MBs_PER_TILE(tile->width, tile->height, band->mb_size));
452         return AVERROR_INVALIDDATA;
453     }
454
455     /* scale factor for motion vectors */
456     mv_scale = (ctx->planes[0].bands[0].mb_size >> 3) - (band->mb_size >> 3);
457     mv_x = mv_y = 0;
458
459     for (y = tile->ypos; y < (tile->ypos + tile->height); y += band->mb_size) {
460         mb_offset = offs;
461
462         for (x = tile->xpos; x < (tile->xpos + tile->width); x += band->mb_size) {
463             mb->xpos     = x;
464             mb->ypos     = y;
465             mb->buf_offs = mb_offset;
466
467             if (get_bits1(&ctx->gb)) {
468                 if (ctx->frame_type == FRAMETYPE_INTRA) {
469                     av_log(avctx, AV_LOG_ERROR, "Empty macroblock in an INTRA picture!\n");
470                     return -1;
471                 }
472                 mb->type = 1; /* empty macroblocks are always INTER */
473                 mb->cbp  = 0; /* all blocks are empty */
474
475                 mb->q_delta = 0;
476                 if (!band->plane && !band->band_num && (ctx->frame_flags & 8)) {
477                     mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
478                                            IVI_VLC_BITS, 1);
479                     mb->q_delta = IVI_TOSIGNED(mb->q_delta);
480                 }
481
482                 mb->mv_x = mb->mv_y = 0; /* no motion vector coded */
483                 if (band->inherit_mv && ref_mb){
484                     /* motion vector inheritance */
485                     if (mv_scale) {
486                         mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale);
487                         mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale);
488                     } else {
489                         mb->mv_x = ref_mb->mv_x;
490                         mb->mv_y = ref_mb->mv_y;
491                     }
492                 }
493             } else {
494                 if (band->inherit_mv && ref_mb) {
495                     mb->type = ref_mb->type; /* copy mb_type from corresponding reference mb */
496                 } else if (ctx->frame_type == FRAMETYPE_INTRA) {
497                     mb->type = 0; /* mb_type is always INTRA for intra-frames */
498                 } else {
499                     mb->type = get_bits1(&ctx->gb);
500                 }
501
502                 blks_per_mb = band->mb_size != band->blk_size ? 4 : 1;
503                 mb->cbp = get_bits(&ctx->gb, blks_per_mb);
504
505                 mb->q_delta = 0;
506                 if (band->qdelta_present) {
507                     if (band->inherit_qdelta) {
508                         if (ref_mb) mb->q_delta = ref_mb->q_delta;
509                     } else if (mb->cbp || (!band->plane && !band->band_num &&
510                                            (ctx->frame_flags & 8))) {
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
517                 if (!mb->type) {
518                     mb->mv_x = mb->mv_y = 0; /* there is no motion vector in intra-macroblocks */
519                 } else {
520                     if (band->inherit_mv && ref_mb){
521                         /* motion vector inheritance */
522                         if (mv_scale) {
523                             mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale);
524                             mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale);
525                         } else {
526                             mb->mv_x = ref_mb->mv_x;
527                             mb->mv_y = ref_mb->mv_y;
528                         }
529                     } else {
530                         /* decode motion vector deltas */
531                         mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
532                                             IVI_VLC_BITS, 1);
533                         mv_y += IVI_TOSIGNED(mv_delta);
534                         mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
535                                             IVI_VLC_BITS, 1);
536                         mv_x += IVI_TOSIGNED(mv_delta);
537                         mb->mv_x = mv_x;
538                         mb->mv_y = mv_y;
539                     }
540                 }
541             }
542
543             s= band->is_halfpel;
544             if (mb->type)
545             if ( x +  (mb->mv_x   >>s) +                 (y+               (mb->mv_y   >>s))*band->pitch < 0 ||
546                  x + ((mb->mv_x+s)>>s) + band->mb_size - 1
547                    + (y+band->mb_size - 1 +((mb->mv_y+s)>>s))*band->pitch > band->bufsize - 1) {
548                 av_log(avctx, AV_LOG_ERROR, "motion vector %d %d outside reference\n", x*s + mb->mv_x, y*s + mb->mv_y);
549                 return AVERROR_INVALIDDATA;
550             }
551
552             mb++;
553             if (ref_mb)
554                 ref_mb++;
555             mb_offset += band->mb_size;
556         }
557
558         offs += row_offset;
559     }
560
561     align_get_bits(&ctx->gb);
562
563     return 0;
564 }
565
566
567 /**
568  *  Switch buffers.
569  *
570  *  @param[in,out] ctx  ptr to the decoder context
571  */
572 static void switch_buffers(IVI45DecContext *ctx)
573 {
574     switch (ctx->prev_frame_type) {
575     case FRAMETYPE_INTRA:
576     case FRAMETYPE_INTER:
577         ctx->buf_switch ^= 1;
578         ctx->dst_buf = ctx->buf_switch;
579         ctx->ref_buf = ctx->buf_switch ^ 1;
580         break;
581     case FRAMETYPE_INTER_SCAL:
582         if (!ctx->inter_scal) {
583             ctx->ref2_buf   = 2;
584             ctx->inter_scal = 1;
585         }
586         FFSWAP(int, ctx->dst_buf, ctx->ref2_buf);
587         ctx->ref_buf = ctx->ref2_buf;
588         break;
589     case FRAMETYPE_INTER_NOREF:
590         break;
591     }
592
593     switch (ctx->frame_type) {
594     case FRAMETYPE_INTRA:
595         ctx->buf_switch = 0;
596         /* FALLTHROUGH */
597     case FRAMETYPE_INTER:
598         ctx->inter_scal = 0;
599         ctx->dst_buf = ctx->buf_switch;
600         ctx->ref_buf = ctx->buf_switch ^ 1;
601         break;
602     case FRAMETYPE_INTER_SCAL:
603     case FRAMETYPE_INTER_NOREF:
604     case FRAMETYPE_NULL:
605         break;
606     }
607 }
608
609
610 static int is_nonnull_frame(IVI45DecContext *ctx)
611 {
612     return ctx->frame_type != FRAMETYPE_NULL;
613 }
614
615
616 /**
617  *  Initialize Indeo5 decoder.
618  */
619 static av_cold int decode_init(AVCodecContext *avctx)
620 {
621     IVI45DecContext  *ctx = avctx->priv_data;
622     int             result;
623
624     ff_ivi_init_static_vlc();
625
626     /* copy rvmap tables in our context so we can apply changes to them */
627     memcpy(ctx->rvmap_tabs, ff_ivi_rvmap_tabs, sizeof(ff_ivi_rvmap_tabs));
628
629     /* set the initial picture layout according to the basic profile:
630        there is only one band per plane (no scalability), only one tile (no local decoding)
631        and picture format = YVU9 */
632     ctx->pic_conf.pic_width     = avctx->width;
633     ctx->pic_conf.pic_height    = avctx->height;
634     ctx->pic_conf.chroma_width  = (avctx->width  + 3) >> 2;
635     ctx->pic_conf.chroma_height = (avctx->height + 3) >> 2;
636     ctx->pic_conf.tile_width    = avctx->width;
637     ctx->pic_conf.tile_height   = avctx->height;
638     ctx->pic_conf.luma_bands    = ctx->pic_conf.chroma_bands = 1;
639
640     avcodec_get_frame_defaults(&ctx->frame);
641
642     result = ff_ivi_init_planes(ctx->planes, &ctx->pic_conf);
643     if (result) {
644         av_log(avctx, AV_LOG_ERROR, "Couldn't allocate color planes!\n");
645         return -1;
646     }
647
648     ctx->buf_switch = 0;
649     ctx->inter_scal = 0;
650
651     ctx->decode_pic_hdr   = decode_pic_hdr;
652     ctx->decode_band_hdr  = decode_band_hdr;
653     ctx->decode_mb_info   = decode_mb_info;
654     ctx->switch_buffers   = switch_buffers;
655     ctx->is_nonnull_frame = is_nonnull_frame;
656
657     avctx->pix_fmt = PIX_FMT_YUV410P;
658
659     return 0;
660 }
661
662 AVCodec ff_indeo5_decoder = {
663     .name           = "indeo5",
664     .type           = AVMEDIA_TYPE_VIDEO,
665     .id             = CODEC_ID_INDEO5,
666     .priv_data_size = sizeof(IVI45DecContext),
667     .init           = decode_init,
668     .close          = ff_ivi_decode_close,
669     .decode         = ff_ivi_decode_frame,
670     .long_name      = NULL_IF_CONFIG_SMALL("Intel Indeo Video Interactive 5"),
671 };