• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavcodec/indeo3.c

Go to the documentation of this file.
00001 /*
00002  * Indeo Video v3 compatible decoder
00003  * Copyright (c) 2009 - 2011 Maxim Poliakovski
00004  *
00005  * This file is part of Libav.
00006  *
00007  * Libav is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * Libav is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with Libav; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00032 #include "libavutil/imgutils.h"
00033 #include "libavutil/intreadwrite.h"
00034 #include "avcodec.h"
00035 #include "dsputil.h"
00036 #include "bytestream.h"
00037 #include "get_bits.h"
00038 
00039 #include "indeo3data.h"
00040 
00041 /* RLE opcodes. */
00042 enum {
00043     RLE_ESC_F9    = 249, 
00044     RLE_ESC_FA    = 250, 
00045     RLE_ESC_FB    = 251, 
00046     RLE_ESC_FC    = 252, 
00047     RLE_ESC_FD    = 253, 
00048     RLE_ESC_FE    = 254, 
00049     RLE_ESC_FF    = 255  
00050 };
00051 
00052 
00053 /* Some constants for parsing frame bitstream flags. */
00054 #define BS_8BIT_PEL     (1 << 1) ///< 8bit pixel bitdepth indicator
00055 #define BS_KEYFRAME     (1 << 2) ///< intra frame indicator
00056 #define BS_MV_Y_HALF    (1 << 4) ///< vertical mv halfpel resolution indicator
00057 #define BS_MV_X_HALF    (1 << 5) ///< horizontal mv halfpel resolution indicator
00058 #define BS_NONREF       (1 << 8) ///< nonref (discardable) frame indicator
00059 #define BS_BUFFER        9       ///< indicates which of two frame buffers should be used
00060 
00061 
00062 typedef struct Plane {
00063     uint8_t         *buffers[2];
00064     uint8_t         *pixels[2]; 
00065     uint32_t        width;
00066     uint32_t        height;
00067     uint32_t        pitch;
00068 } Plane;
00069 
00070 #define CELL_STACK_MAX  20
00071 
00072 typedef struct Cell {
00073     int16_t         xpos;       
00074     int16_t         ypos;
00075     int16_t         width;      
00076     int16_t         height;     
00077     uint8_t         tree;       
00078     const int8_t    *mv_ptr;    
00079 } Cell;
00080 
00081 typedef struct Indeo3DecodeContext {
00082     AVCodecContext *avctx;
00083     AVFrame         frame;
00084     DSPContext      dsp;
00085 
00086     GetBitContext   gb;
00087     int             need_resync;
00088     int             skip_bits;
00089     const uint8_t   *next_cell_data;
00090     const uint8_t   *last_byte;
00091     const int8_t    *mc_vectors;
00092     unsigned        num_vectors;    
00093 
00094     int16_t         width, height;
00095     uint32_t        frame_num;      
00096     uint32_t        data_size;      
00097     uint16_t        frame_flags;    
00098     uint8_t         cb_offset;      
00099     uint8_t         buf_sel;        
00100     const uint8_t   *y_data_ptr;
00101     const uint8_t   *v_data_ptr;
00102     const uint8_t   *u_data_ptr;
00103     int32_t         y_data_size;
00104     int32_t         v_data_size;
00105     int32_t         u_data_size;
00106     const uint8_t   *alt_quant;     
00107     Plane           planes[3];
00108 } Indeo3DecodeContext;
00109 
00110 
00111 static uint8_t requant_tab[8][128];
00112 
00113 /*
00114  *  Build the static requantization table.
00115  *  This table is used to remap pixel values according to a specific
00116  *  quant index and thus avoid overflows while adding deltas.
00117  */
00118 static av_cold void build_requant_tab(void)
00119 {
00120     static int8_t offsets[8] = { 1, 1, 2, -3, -3, 3, 4, 4 };
00121     static int8_t deltas [8] = { 0, 1, 0,  4,  4, 1, 0, 1 };
00122 
00123     int i, j, step;
00124 
00125     for (i = 0; i < 8; i++) {
00126         step = i + 2;
00127         for (j = 0; j < 128; j++)
00128                 requant_tab[i][j] = (j + offsets[i]) / step * step + deltas[i];
00129     }
00130 
00131     /* some last elements calculated above will have values >= 128 */
00132     /* pixel values shall never exceed 127 so set them to non-overflowing values */
00133     /* according with the quantization step of the respective section */
00134     requant_tab[0][127] = 126;
00135     requant_tab[1][119] = 118;
00136     requant_tab[1][120] = 118;
00137     requant_tab[2][126] = 124;
00138     requant_tab[2][127] = 124;
00139     requant_tab[6][124] = 120;
00140     requant_tab[6][125] = 120;
00141     requant_tab[6][126] = 120;
00142     requant_tab[6][127] = 120;
00143 
00144     /* Patch for compatibility with the Intel's binary decoders */
00145     requant_tab[1][7] = 10;
00146     requant_tab[4][8] = 10;
00147 }
00148 
00149 
00150 static av_cold int allocate_frame_buffers(Indeo3DecodeContext *ctx,
00151                                           AVCodecContext *avctx)
00152 {
00153     int p, luma_width, luma_height, chroma_width, chroma_height;
00154     int luma_pitch, chroma_pitch, luma_size, chroma_size;
00155 
00156     luma_width  = ctx->width;
00157     luma_height = ctx->height;
00158 
00159     if (luma_width  < 16 || luma_width  > 640 ||
00160         luma_height < 16 || luma_height > 480 ||
00161         luma_width  &  3 || luma_height &   3) {
00162         av_log(avctx, AV_LOG_ERROR, "Invalid picture dimensions: %d x %d!\n",
00163                luma_width, luma_height);
00164         return AVERROR_INVALIDDATA;
00165     }
00166 
00167     chroma_width  = FFALIGN(luma_width  >> 2, 4);
00168     chroma_height = FFALIGN(luma_height >> 2, 4);
00169 
00170     luma_pitch   = FFALIGN(luma_width,   16);
00171     chroma_pitch = FFALIGN(chroma_width, 16);
00172 
00173     /* Calculate size of the luminance plane.  */
00174     /* Add one line more for INTRA prediction. */
00175     luma_size = luma_pitch * (luma_height + 1);
00176 
00177     /* Calculate size of a chrominance planes. */
00178     /* Add one line more for INTRA prediction. */
00179     chroma_size = chroma_pitch * (chroma_height + 1);
00180 
00181     /* allocate frame buffers */
00182     for (p = 0; p < 3; p++) {
00183         ctx->planes[p].pitch  = !p ? luma_pitch  : chroma_pitch;
00184         ctx->planes[p].width  = !p ? luma_width  : chroma_width;
00185         ctx->planes[p].height = !p ? luma_height : chroma_height;
00186 
00187         ctx->planes[p].buffers[0] = av_malloc(!p ? luma_size : chroma_size);
00188         ctx->planes[p].buffers[1] = av_malloc(!p ? luma_size : chroma_size);
00189 
00190         /* fill the INTRA prediction lines with the middle pixel value = 64 */
00191         memset(ctx->planes[p].buffers[0], 0x40, ctx->planes[p].pitch);
00192         memset(ctx->planes[p].buffers[1], 0x40, ctx->planes[p].pitch);
00193 
00194         /* set buffer pointers = buf_ptr + pitch and thus skip the INTRA prediction line */
00195         ctx->planes[p].pixels[0] = ctx->planes[p].buffers[0] + ctx->planes[p].pitch;
00196         ctx->planes[p].pixels[1] = ctx->planes[p].buffers[1] + ctx->planes[p].pitch;
00197     }
00198 
00199     return 0;
00200 }
00201 
00202 
00203 static av_cold void free_frame_buffers(Indeo3DecodeContext *ctx)
00204 {
00205     int p;
00206 
00207     for (p = 0; p < 3; p++) {
00208         av_freep(&ctx->planes[p].buffers[0]);
00209         av_freep(&ctx->planes[p].buffers[1]);
00210     }
00211 }
00212 
00213 
00222 static void copy_cell(Indeo3DecodeContext *ctx, Plane *plane, Cell *cell)
00223 {
00224     int     h, w, mv_x, mv_y, offset, offset_dst;
00225     uint8_t *src, *dst;
00226 
00227     /* setup output and reference pointers */
00228     offset_dst  = (cell->ypos << 2) * plane->pitch + (cell->xpos << 2);
00229     dst         = plane->pixels[ctx->buf_sel] + offset_dst;
00230     mv_y        = cell->mv_ptr[0];
00231     mv_x        = cell->mv_ptr[1];
00232     offset      = offset_dst + mv_y * plane->pitch + mv_x;
00233     src         = plane->pixels[ctx->buf_sel ^ 1] + offset;
00234 
00235     h = cell->height << 2;
00236 
00237     for (w = cell->width; w > 0;) {
00238         /* copy using 16xH blocks */
00239         if (!((cell->xpos << 2) & 15) && w >= 4) {
00240             for (; w >= 4; src += 16, dst += 16, w -= 4)
00241                 ctx->dsp.put_no_rnd_pixels_tab[0][0](dst, src, plane->pitch, h);
00242         }
00243 
00244         /* copy using 8xH blocks */
00245         if (!((cell->xpos << 2) & 7) && w >= 2) {
00246             ctx->dsp.put_no_rnd_pixels_tab[1][0](dst, src, plane->pitch, h);
00247             w -= 2;
00248             src += 8;
00249             dst += 8;
00250         }
00251 
00252         if (w >= 1) {
00253             copy_block4(dst, src, plane->pitch, plane->pitch, h);
00254             w--;
00255             src += 4;
00256             dst += 4;
00257         }
00258     }
00259 }
00260 
00261 
00262 /* Average 4/8 pixels at once without rounding using SWAR */
00263 #define AVG_32(dst, src, ref) \
00264     AV_WN32A(dst, ((AV_RN32A(src) + AV_RN32A(ref)) >> 1) & 0x7F7F7F7FUL)
00265 
00266 #define AVG_64(dst, src, ref) \
00267     AV_WN64A(dst, ((AV_RN64A(src) + AV_RN64A(ref)) >> 1) & 0x7F7F7F7F7F7F7F7FULL)
00268 
00269 
00270 /*
00271  *  Replicate each even pixel as follows:
00272  *  ABCDEFGH -> AACCEEGG
00273  */
00274 static inline uint64_t replicate64(uint64_t a) {
00275 #if HAVE_BIGENDIAN
00276     a &= 0xFF00FF00FF00FF00ULL;
00277     a |= a >> 8;
00278 #else
00279     a &= 0x00FF00FF00FF00FFULL;
00280     a |= a << 8;
00281 #endif
00282     return a;
00283 }
00284 
00285 static inline uint32_t replicate32(uint32_t a) {
00286 #if HAVE_BIGENDIAN
00287     a &= 0xFF00FF00UL;
00288     a |= a >> 8;
00289 #else
00290     a &= 0x00FF00FFUL;
00291     a |= a << 8;
00292 #endif
00293     return a;
00294 }
00295 
00296 
00297 /* Fill n lines with 64bit pixel value pix */
00298 static inline void fill_64(uint8_t *dst, const uint64_t pix, int32_t n,
00299                            int32_t row_offset)
00300 {
00301     for (; n > 0; dst += row_offset, n--)
00302         AV_WN64A(dst, pix);
00303 }
00304 
00305 
00306 /* Error codes for cell decoding. */
00307 enum {
00308     IV3_NOERR       = 0,
00309     IV3_BAD_RLE     = 1,
00310     IV3_BAD_DATA    = 2,
00311     IV3_BAD_COUNTER = 3,
00312     IV3_UNSUPPORTED = 4,
00313     IV3_OUT_OF_DATA = 5
00314 };
00315 
00316 
00317 #define BUFFER_PRECHECK \
00318 if (*data_ptr >= last_ptr) \
00319     return IV3_OUT_OF_DATA; \
00320 
00321 #define RLE_BLOCK_COPY \
00322     if (cell->mv_ptr || !skip_flag) \
00323         copy_block4(dst, ref, row_offset, row_offset, 4 << v_zoom)
00324 
00325 #define RLE_BLOCK_COPY_8 \
00326     pix64 = AV_RN64A(ref);\
00327     if (is_first_row) {/* special prediction case: top line of a cell */\
00328         pix64 = replicate64(pix64);\
00329         fill_64(dst + row_offset, pix64, 7, row_offset);\
00330         AVG_64(dst, ref, dst + row_offset);\
00331     } else \
00332         fill_64(dst, pix64, 8, row_offset)
00333 
00334 #define RLE_LINES_COPY \
00335     copy_block4(dst, ref, row_offset, row_offset, num_lines << v_zoom)
00336 
00337 #define RLE_LINES_COPY_M10 \
00338     pix64 = AV_RN64A(ref);\
00339     if (is_top_of_cell) {\
00340         pix64 = replicate64(pix64);\
00341         fill_64(dst + row_offset, pix64, (num_lines << 1) - 1, row_offset);\
00342         AVG_64(dst, ref, dst + row_offset);\
00343     } else \
00344         fill_64(dst, pix64, num_lines << 1, row_offset)
00345 
00346 #define APPLY_DELTA_4 \
00347     AV_WN16A(dst + line_offset    , AV_RN16A(ref    ) + delta_tab->deltas[dyad1]);\
00348     AV_WN16A(dst + line_offset + 2, AV_RN16A(ref + 2) + delta_tab->deltas[dyad2]);\
00349     if (mode >= 3) {\
00350         if (is_top_of_cell && !cell->ypos) {\
00351             AV_COPY32(dst, dst + row_offset);\
00352         } else {\
00353             AVG_32(dst, ref, dst + row_offset);\
00354         }\
00355     }
00356 
00357 #define APPLY_DELTA_8 \
00358     /* apply two 32-bit VQ deltas to next even line */\
00359     if (is_top_of_cell) { \
00360         AV_WN32A(dst + row_offset    , \
00361                  replicate32(AV_RN32A(ref    )) + delta_tab->deltas_m10[dyad1]);\
00362         AV_WN32A(dst + row_offset + 4, \
00363                  replicate32(AV_RN32A(ref + 4)) + delta_tab->deltas_m10[dyad2]);\
00364     } else { \
00365         AV_WN32A(dst + row_offset    , \
00366                  AV_RN32A(ref    ) + delta_tab->deltas_m10[dyad1]);\
00367         AV_WN32A(dst + row_offset + 4, \
00368                  AV_RN32A(ref + 4) + delta_tab->deltas_m10[dyad2]);\
00369     } \
00370     /* odd lines are not coded but rather interpolated/replicated */\
00371     /* first line of the cell on the top of image? - replicate */\
00372     /* otherwise - interpolate */\
00373     if (is_top_of_cell && !cell->ypos) {\
00374         AV_COPY64(dst, dst + row_offset);\
00375     } else \
00376         AVG_64(dst, ref, dst + row_offset);
00377 
00378 
00379 #define APPLY_DELTA_1011_INTER \
00380     if (mode == 10) { \
00381         AV_WN32A(dst                 , \
00382                  AV_RN32A(dst                 ) + delta_tab->deltas_m10[dyad1]);\
00383         AV_WN32A(dst + 4             , \
00384                  AV_RN32A(dst + 4             ) + delta_tab->deltas_m10[dyad2]);\
00385         AV_WN32A(dst + row_offset    , \
00386                  AV_RN32A(dst + row_offset    ) + delta_tab->deltas_m10[dyad1]);\
00387         AV_WN32A(dst + row_offset + 4, \
00388                  AV_RN32A(dst + row_offset + 4) + delta_tab->deltas_m10[dyad2]);\
00389     } else { \
00390         AV_WN16A(dst                 , \
00391                  AV_RN16A(dst                 ) + delta_tab->deltas[dyad1]);\
00392         AV_WN16A(dst + 2             , \
00393                  AV_RN16A(dst + 2             ) + delta_tab->deltas[dyad2]);\
00394         AV_WN16A(dst + row_offset    , \
00395                  AV_RN16A(dst + row_offset    ) + delta_tab->deltas[dyad1]);\
00396         AV_WN16A(dst + row_offset + 2, \
00397                  AV_RN16A(dst + row_offset + 2) + delta_tab->deltas[dyad2]);\
00398     }
00399 
00400 
00401 static int decode_cell_data(Cell *cell, uint8_t *block, uint8_t *ref_block,
00402                             int pitch, int h_zoom, int v_zoom, int mode,
00403                             const vqEntry *delta[2], int swap_quads[2],
00404                             const uint8_t **data_ptr, const uint8_t *last_ptr)
00405 {
00406     int           x, y, line, num_lines;
00407     int           rle_blocks = 0;
00408     uint8_t       code, *dst, *ref;
00409     const vqEntry *delta_tab;
00410     unsigned int  dyad1, dyad2;
00411     uint64_t      pix64;
00412     int           skip_flag = 0, is_top_of_cell, is_first_row = 1;
00413     int           row_offset, blk_row_offset, line_offset;
00414 
00415     row_offset     =  pitch;
00416     blk_row_offset = (row_offset << (2 + v_zoom)) - (cell->width << 2);
00417     line_offset    = v_zoom ? row_offset : 0;
00418 
00419     for (y = 0; y < cell->height; is_first_row = 0, y += 1 + v_zoom) {
00420         for (x = 0; x < cell->width; x += 1 + h_zoom) {
00421             ref = ref_block;
00422             dst = block;
00423 
00424             if (rle_blocks > 0) {
00425                 if (mode <= 4) {
00426                     RLE_BLOCK_COPY;
00427                 } else if (mode == 10 && !cell->mv_ptr) {
00428                     RLE_BLOCK_COPY_8;
00429                 }
00430                 rle_blocks--;
00431             } else {
00432                 for (line = 0; line < 4;) {
00433                     num_lines = 1;
00434                     is_top_of_cell = is_first_row && !line;
00435 
00436                     /* select primary VQ table for odd, secondary for even lines */
00437                     if (mode <= 4)
00438                         delta_tab = delta[line & 1];
00439                     else
00440                         delta_tab = delta[1];
00441                     BUFFER_PRECHECK;
00442                     code = bytestream_get_byte(data_ptr);
00443                     if (code < 248) {
00444                         if (code < delta_tab->num_dyads) {
00445                             BUFFER_PRECHECK;
00446                             dyad1 = bytestream_get_byte(data_ptr);
00447                             dyad2 = code;
00448                             if (dyad1 >= delta_tab->num_dyads || dyad1 >= 248)
00449                                 return IV3_BAD_DATA;
00450                         } else {
00451                             /* process QUADS */
00452                             code -= delta_tab->num_dyads;
00453                             dyad1 = code / delta_tab->quad_exp;
00454                             dyad2 = code % delta_tab->quad_exp;
00455                             if (swap_quads[line & 1])
00456                                 FFSWAP(unsigned int, dyad1, dyad2);
00457                         }
00458                         if (mode <= 4) {
00459                             APPLY_DELTA_4;
00460                         } else if (mode == 10 && !cell->mv_ptr) {
00461                             APPLY_DELTA_8;
00462                         } else {
00463                             APPLY_DELTA_1011_INTER;
00464                         }
00465                     } else {
00466                         /* process RLE codes */
00467                         switch (code) {
00468                         case RLE_ESC_FC:
00469                             skip_flag  = 0;
00470                             rle_blocks = 1;
00471                             code       = 253;
00472                             /* FALLTHROUGH */
00473                         case RLE_ESC_FF:
00474                         case RLE_ESC_FE:
00475                         case RLE_ESC_FD:
00476                             num_lines = 257 - code - line;
00477                             if (num_lines <= 0)
00478                                 return IV3_BAD_RLE;
00479                             if (mode <= 4) {
00480                                 RLE_LINES_COPY;
00481                             } else if (mode == 10 && !cell->mv_ptr) {
00482                                 RLE_LINES_COPY_M10;
00483                             }
00484                             break;
00485                         case RLE_ESC_FB:
00486                             BUFFER_PRECHECK;
00487                             code = bytestream_get_byte(data_ptr);
00488                             rle_blocks = (code & 0x1F) - 1; /* set block counter */
00489                             if (code >= 64 || rle_blocks < 0)
00490                                 return IV3_BAD_COUNTER;
00491                             skip_flag = code & 0x20;
00492                             num_lines = 4 - line; /* enforce next block processing */
00493                             if (mode >= 10 || (cell->mv_ptr || !skip_flag)) {
00494                                 if (mode <= 4) {
00495                                     RLE_LINES_COPY;
00496                                 } else if (mode == 10 && !cell->mv_ptr) {
00497                                     RLE_LINES_COPY_M10;
00498                                 }
00499                             }
00500                             break;
00501                         case RLE_ESC_F9:
00502                             skip_flag  = 1;
00503                             rle_blocks = 1;
00504                             /* FALLTHROUGH */
00505                         case RLE_ESC_FA:
00506                             if (line)
00507                                 return IV3_BAD_RLE;
00508                             num_lines = 4; /* enforce next block processing */
00509                             if (cell->mv_ptr) {
00510                                 if (mode <= 4) {
00511                                     RLE_LINES_COPY;
00512                                 } else if (mode == 10 && !cell->mv_ptr) {
00513                                     RLE_LINES_COPY_M10;
00514                                 }
00515                             }
00516                             break;
00517                         default:
00518                             return IV3_UNSUPPORTED;
00519                         }
00520                     }
00521 
00522                     line += num_lines;
00523                     ref  += row_offset * (num_lines << v_zoom);
00524                     dst  += row_offset * (num_lines << v_zoom);
00525                 }
00526             }
00527 
00528             /* move to next horizontal block */
00529             block     += 4 << h_zoom;
00530             ref_block += 4 << h_zoom;
00531         }
00532 
00533         /* move to next line of blocks */
00534         ref_block += blk_row_offset;
00535         block     += blk_row_offset;
00536     }
00537     return IV3_NOERR;
00538 }
00539 
00540 
00554 static int decode_cell(Indeo3DecodeContext *ctx, AVCodecContext *avctx,
00555                        Plane *plane, Cell *cell, const uint8_t *data_ptr,
00556                        const uint8_t *last_ptr)
00557 {
00558     int           x, mv_x, mv_y, mode, vq_index, prim_indx, second_indx;
00559     int           zoom_fac;
00560     int           offset, error = 0, swap_quads[2];
00561     uint8_t       code, *block, *ref_block = 0;
00562     const vqEntry *delta[2];
00563     const uint8_t *data_start = data_ptr;
00564 
00565     /* get coding mode and VQ table index from the VQ descriptor byte */
00566     code     = *data_ptr++;
00567     mode     = code >> 4;
00568     vq_index = code & 0xF;
00569 
00570     /* setup output and reference pointers */
00571     offset = (cell->ypos << 2) * plane->pitch + (cell->xpos << 2);
00572     block  =  plane->pixels[ctx->buf_sel] + offset;
00573     if (!cell->mv_ptr) {
00574         /* use previous line as reference for INTRA cells */
00575         ref_block = block - plane->pitch;
00576     } else if (mode >= 10) {
00577         /* for mode 10 and 11 INTER first copy the predicted cell into the current one */
00578         /* so we don't need to do data copying for each RLE code later */
00579         copy_cell(ctx, plane, cell);
00580     } else {
00581         /* set the pointer to the reference pixels for modes 0-4 INTER */
00582         mv_y      = cell->mv_ptr[0];
00583         mv_x      = cell->mv_ptr[1];
00584         offset   += mv_y * plane->pitch + mv_x;
00585         ref_block = plane->pixels[ctx->buf_sel ^ 1] + offset;
00586     }
00587 
00588     /* select VQ tables as follows: */
00589     /* modes 0 and 3 use only the primary table for all lines in a block */
00590     /* while modes 1 and 4 switch between primary and secondary tables on alternate lines */
00591     if (mode == 1 || mode == 4) {
00592         code        = ctx->alt_quant[vq_index];
00593         prim_indx   = (code >> 4)  + ctx->cb_offset;
00594         second_indx = (code & 0xF) + ctx->cb_offset;
00595     } else {
00596         vq_index += ctx->cb_offset;
00597         prim_indx = second_indx = vq_index;
00598     }
00599 
00600     if (prim_indx >= 24 || second_indx >= 24) {
00601         av_log(avctx, AV_LOG_ERROR, "Invalid VQ table indexes! Primary: %d, secondary: %d!\n",
00602                prim_indx, second_indx);
00603         return AVERROR_INVALIDDATA;
00604     }
00605 
00606     delta[0] = &vq_tab[second_indx];
00607     delta[1] = &vq_tab[prim_indx];
00608     swap_quads[0] = second_indx >= 16;
00609     swap_quads[1] = prim_indx   >= 16;
00610 
00611     /* requantize the prediction if VQ index of this cell differs from VQ index */
00612     /* of the predicted cell in order to avoid overflows. */
00613     if (vq_index >= 8 && ref_block) {
00614         for (x = 0; x < cell->width << 2; x++)
00615             ref_block[x] = requant_tab[vq_index & 7][ref_block[x]];
00616     }
00617 
00618     error = IV3_NOERR;
00619 
00620     switch (mode) {
00621     case 0: /*------------------ MODES 0 & 1 (4x4 block processing) --------------------*/
00622     case 1:
00623     case 3: /*------------------ MODES 3 & 4 (4x8 block processing) --------------------*/
00624     case 4:
00625         if (mode >= 3 && cell->mv_ptr) {
00626             av_log(avctx, AV_LOG_ERROR, "Attempt to apply Mode 3/4 to an INTER cell!\n");
00627             return AVERROR_INVALIDDATA;
00628         }
00629 
00630         zoom_fac = mode >= 3;
00631         error = decode_cell_data(cell, block, ref_block, plane->pitch, 0, zoom_fac,
00632                                  mode, delta, swap_quads, &data_ptr, last_ptr);
00633         break;
00634     case 10: /*-------------------- MODE 10 (8x8 block processing) ---------------------*/
00635     case 11: /*----------------- MODE 11 (4x8 INTER block processing) ------------------*/
00636         if (mode == 10 && !cell->mv_ptr) { /* MODE 10 INTRA processing */
00637             error = decode_cell_data(cell, block, ref_block, plane->pitch, 1, 1,
00638                                      mode, delta, swap_quads, &data_ptr, last_ptr);
00639         } else { /* mode 10 and 11 INTER processing */
00640             if (mode == 11 && !cell->mv_ptr) {
00641                av_log(avctx, AV_LOG_ERROR, "Attempt to use Mode 11 for an INTRA cell!\n");
00642                return AVERROR_INVALIDDATA;
00643             }
00644 
00645             zoom_fac = mode == 10;
00646             error = decode_cell_data(cell, block, ref_block, plane->pitch,
00647                                      zoom_fac, 1, mode, delta, swap_quads,
00648                                      &data_ptr, last_ptr);
00649         }
00650         break;
00651     default:
00652         av_log(avctx, AV_LOG_ERROR, "Unsupported coding mode: %d\n", mode);
00653         return AVERROR_INVALIDDATA;
00654     }//switch mode
00655 
00656     switch (error) {
00657     case IV3_BAD_RLE:
00658         av_log(avctx, AV_LOG_ERROR, "Mode %d: RLE code %X is not allowed at the current line\n",
00659                mode, data_ptr[-1]);
00660         return AVERROR_INVALIDDATA;
00661     case IV3_BAD_DATA:
00662         av_log(avctx, AV_LOG_ERROR, "Mode %d: invalid VQ data\n", mode);
00663         return AVERROR_INVALIDDATA;
00664     case IV3_BAD_COUNTER:
00665         av_log(avctx, AV_LOG_ERROR, "Mode %d: RLE-FB invalid counter: %d\n", mode, code);
00666         return AVERROR_INVALIDDATA;
00667     case IV3_UNSUPPORTED:
00668         av_log(avctx, AV_LOG_ERROR, "Mode %d: unsupported RLE code: %X\n", mode, data_ptr[-1]);
00669         return AVERROR_INVALIDDATA;
00670     case IV3_OUT_OF_DATA:
00671         av_log(avctx, AV_LOG_ERROR, "Mode %d: attempt to read past end of buffer\n", mode);
00672         return AVERROR_INVALIDDATA;
00673     }
00674 
00675     return data_ptr - data_start; /* report number of bytes consumed from the input buffer */
00676 }
00677 
00678 
00679 /* Binary tree codes. */
00680 enum {
00681     H_SPLIT    = 0,
00682     V_SPLIT    = 1,
00683     INTRA_NULL = 2,
00684     INTER_DATA = 3
00685 };
00686 
00687 
00688 #define SPLIT_CELL(size, new_size) (new_size) = ((size) > 2) ? ((((size) + 2) >> 2) << 1) : 1
00689 
00690 #define UPDATE_BITPOS(n) \
00691     ctx->skip_bits  += (n); \
00692     ctx->need_resync = 1
00693 
00694 #define RESYNC_BITSTREAM \
00695     if (ctx->need_resync && !(get_bits_count(&ctx->gb) & 7)) { \
00696         skip_bits_long(&ctx->gb, ctx->skip_bits);              \
00697         ctx->skip_bits   = 0;                                  \
00698         ctx->need_resync = 0;                                  \
00699     }
00700 
00701 #define CHECK_CELL \
00702     if (curr_cell.xpos + curr_cell.width > (plane->width >> 2) ||               \
00703         curr_cell.ypos + curr_cell.height > (plane->height >> 2)) {             \
00704         av_log(avctx, AV_LOG_ERROR, "Invalid cell: x=%d, y=%d, w=%d, h=%d\n",   \
00705                curr_cell.xpos, curr_cell.ypos, curr_cell.width, curr_cell.height); \
00706         return AVERROR_INVALIDDATA;                                                              \
00707     }
00708 
00709 
00710 static int parse_bintree(Indeo3DecodeContext *ctx, AVCodecContext *avctx,
00711                          Plane *plane, int code, Cell *ref_cell,
00712                          const int depth, const int strip_width)
00713 {
00714     Cell    curr_cell;
00715     int     bytes_used;
00716 
00717     if (depth <= 0) {
00718         av_log(avctx, AV_LOG_ERROR, "Stack overflow (corrupted binary tree)!\n");
00719         return AVERROR_INVALIDDATA; // unwind recursion
00720     }
00721 
00722     curr_cell = *ref_cell; // clone parent cell
00723     if (code == H_SPLIT) {
00724         SPLIT_CELL(ref_cell->height, curr_cell.height);
00725         ref_cell->ypos   += curr_cell.height;
00726         ref_cell->height -= curr_cell.height;
00727         if (ref_cell->height <= 0 || curr_cell.height <= 0)
00728             return AVERROR_INVALIDDATA;
00729     } else if (code == V_SPLIT) {
00730         if (curr_cell.width > strip_width) {
00731             /* split strip */
00732             curr_cell.width = (curr_cell.width <= (strip_width << 1) ? 1 : 2) * strip_width;
00733         } else
00734             SPLIT_CELL(ref_cell->width, curr_cell.width);
00735         ref_cell->xpos  += curr_cell.width;
00736         ref_cell->width -= curr_cell.width;
00737         if (ref_cell->width <= 0 || curr_cell.width <= 0)
00738             return AVERROR_INVALIDDATA;
00739     }
00740 
00741     while (1) { /* loop until return */
00742         RESYNC_BITSTREAM;
00743         switch (code = get_bits(&ctx->gb, 2)) {
00744         case H_SPLIT:
00745         case V_SPLIT:
00746             if (parse_bintree(ctx, avctx, plane, code, &curr_cell, depth - 1, strip_width))
00747                 return AVERROR_INVALIDDATA;
00748             break;
00749         case INTRA_NULL:
00750             if (!curr_cell.tree) { /* MC tree INTRA code */
00751                 curr_cell.mv_ptr = 0; /* mark the current strip as INTRA */
00752                 curr_cell.tree   = 1; /* enter the VQ tree */
00753             } else { /* VQ tree NULL code */
00754                 RESYNC_BITSTREAM;
00755                 code = get_bits(&ctx->gb, 2);
00756                 if (code >= 2) {
00757                     av_log(avctx, AV_LOG_ERROR, "Invalid VQ_NULL code: %d\n", code);
00758                     return AVERROR_INVALIDDATA;
00759                 }
00760                 if (code == 1)
00761                     av_log(avctx, AV_LOG_ERROR, "SkipCell procedure not implemented yet!\n");
00762 
00763                 CHECK_CELL
00764                 if (!curr_cell.mv_ptr)
00765                     return AVERROR_INVALIDDATA;
00766                 copy_cell(ctx, plane, &curr_cell);
00767                 return 0;
00768             }
00769             break;
00770         case INTER_DATA:
00771             if (!curr_cell.tree) { /* MC tree INTER code */
00772                 unsigned mv_idx;
00773                 /* get motion vector index and setup the pointer to the mv set */
00774                 if (!ctx->need_resync)
00775                     ctx->next_cell_data = &ctx->gb.buffer[(get_bits_count(&ctx->gb) + 7) >> 3];
00776                 mv_idx = *(ctx->next_cell_data++) << 1;
00777                 if (mv_idx >= ctx->num_vectors) {
00778                     av_log(avctx, AV_LOG_ERROR, "motion vector index out of range\n");
00779                     return AVERROR_INVALIDDATA;
00780                 }
00781                 curr_cell.mv_ptr = &ctx->mc_vectors[mv_idx];
00782                 curr_cell.tree   = 1; /* enter the VQ tree */
00783                 UPDATE_BITPOS(8);
00784             } else { /* VQ tree DATA code */
00785                 if (!ctx->need_resync)
00786                     ctx->next_cell_data = &ctx->gb.buffer[(get_bits_count(&ctx->gb) + 7) >> 3];
00787 
00788                 CHECK_CELL
00789                 bytes_used = decode_cell(ctx, avctx, plane, &curr_cell,
00790                                          ctx->next_cell_data, ctx->last_byte);
00791                 if (bytes_used < 0)
00792                     return AVERROR_INVALIDDATA;
00793 
00794                 UPDATE_BITPOS(bytes_used << 3);
00795                 ctx->next_cell_data += bytes_used;
00796                 return 0;
00797             }
00798             break;
00799         }
00800     }//while
00801 
00802     return 0;
00803 }
00804 
00805 
00806 static int decode_plane(Indeo3DecodeContext *ctx, AVCodecContext *avctx,
00807                         Plane *plane, const uint8_t *data, int32_t data_size,
00808                         int32_t strip_width)
00809 {
00810     Cell            curr_cell;
00811     unsigned        num_vectors;
00812 
00813     /* each plane data starts with mc_vector_count field, */
00814     /* an optional array of motion vectors followed by the vq data */
00815     num_vectors = bytestream_get_le32(&data);
00816     if (num_vectors > 256) {
00817         av_log(ctx->avctx, AV_LOG_ERROR,
00818                "Read invalid number of motion vectors %d\n", num_vectors);
00819         return AVERROR_INVALIDDATA;
00820     }
00821     if (num_vectors * 2 >= data_size)
00822         return AVERROR_INVALIDDATA;
00823 
00824     ctx->num_vectors = num_vectors;
00825     ctx->mc_vectors  = num_vectors ? data : 0;
00826 
00827     /* init the bitreader */
00828     init_get_bits(&ctx->gb, &data[num_vectors * 2], (data_size - num_vectors * 2) << 3);
00829     ctx->skip_bits   = 0;
00830     ctx->need_resync = 0;
00831 
00832     ctx->last_byte = data + data_size - 1;
00833 
00834     /* initialize the 1st cell and set its dimensions to whole plane */
00835     curr_cell.xpos   = curr_cell.ypos = 0;
00836     curr_cell.width  = plane->width  >> 2;
00837     curr_cell.height = plane->height >> 2;
00838     curr_cell.tree   = 0; // we are in the MC tree now
00839     curr_cell.mv_ptr = 0; // no motion vector = INTRA cell
00840 
00841     return parse_bintree(ctx, avctx, plane, INTRA_NULL, &curr_cell, CELL_STACK_MAX, strip_width);
00842 }
00843 
00844 
00845 #define OS_HDR_ID   MKBETAG('F', 'R', 'M', 'H')
00846 
00847 static int decode_frame_headers(Indeo3DecodeContext *ctx, AVCodecContext *avctx,
00848                                 const uint8_t *buf, int buf_size)
00849 {
00850     const uint8_t   *buf_ptr = buf, *bs_hdr;
00851     uint32_t        frame_num, word2, check_sum, data_size;
00852     uint32_t        y_offset, u_offset, v_offset, starts[3], ends[3];
00853     uint16_t        height, width;
00854     int             i, j;
00855 
00856     /* parse and check the OS header */
00857     frame_num = bytestream_get_le32(&buf_ptr);
00858     word2     = bytestream_get_le32(&buf_ptr);
00859     check_sum = bytestream_get_le32(&buf_ptr);
00860     data_size = bytestream_get_le32(&buf_ptr);
00861 
00862     if ((frame_num ^ word2 ^ data_size ^ OS_HDR_ID) != check_sum) {
00863         av_log(avctx, AV_LOG_ERROR, "OS header checksum mismatch!\n");
00864         return AVERROR_INVALIDDATA;
00865     }
00866 
00867     /* parse the bitstream header */
00868     bs_hdr = buf_ptr;
00869 
00870     if (bytestream_get_le16(&buf_ptr) != 32) {
00871         av_log(avctx, AV_LOG_ERROR, "Unsupported codec version!\n");
00872         return AVERROR_INVALIDDATA;
00873     }
00874 
00875     ctx->frame_num   =  frame_num;
00876     ctx->frame_flags =  bytestream_get_le16(&buf_ptr);
00877     ctx->data_size   = (bytestream_get_le32(&buf_ptr) + 7) >> 3;
00878     ctx->cb_offset   = *buf_ptr++;
00879 
00880     if (ctx->data_size == 16)
00881         return 4;
00882     if (ctx->data_size > buf_size)
00883         ctx->data_size = buf_size;
00884 
00885     buf_ptr += 3; // skip reserved byte and checksum
00886 
00887     /* check frame dimensions */
00888     height = bytestream_get_le16(&buf_ptr);
00889     width  = bytestream_get_le16(&buf_ptr);
00890     if (av_image_check_size(width, height, 0, avctx))
00891         return AVERROR_INVALIDDATA;
00892 
00893     if (width != ctx->width || height != ctx->height) {
00894         int res;
00895 
00896         av_dlog(avctx, "Frame dimensions changed!\n");
00897 
00898         ctx->width  = width;
00899         ctx->height = height;
00900 
00901         free_frame_buffers(ctx);
00902         if ((res = allocate_frame_buffers(ctx, avctx)) < 0)
00903              return res;
00904         avcodec_set_dimensions(avctx, width, height);
00905     }
00906 
00907     y_offset = bytestream_get_le32(&buf_ptr);
00908     v_offset = bytestream_get_le32(&buf_ptr);
00909     u_offset = bytestream_get_le32(&buf_ptr);
00910 
00911     /* unfortunately there is no common order of planes in the buffer */
00912     /* so we use that sorting algo for determining planes data sizes  */
00913     starts[0] = y_offset;
00914     starts[1] = v_offset;
00915     starts[2] = u_offset;
00916 
00917     for (j = 0; j < 3; j++) {
00918         ends[j] = ctx->data_size;
00919         for (i = 2; i >= 0; i--)
00920             if (starts[i] < ends[j] && starts[i] > starts[j])
00921                 ends[j] = starts[i];
00922     }
00923 
00924     ctx->y_data_size = ends[0] - starts[0];
00925     ctx->v_data_size = ends[1] - starts[1];
00926     ctx->u_data_size = ends[2] - starts[2];
00927     if (FFMAX3(y_offset, v_offset, u_offset) >= ctx->data_size - 16 ||
00928         FFMIN3(ctx->y_data_size, ctx->v_data_size, ctx->u_data_size) <= 0) {
00929         av_log(avctx, AV_LOG_ERROR, "One of the y/u/v offsets is invalid\n");
00930         return AVERROR_INVALIDDATA;
00931     }
00932 
00933     ctx->y_data_ptr = bs_hdr + y_offset;
00934     ctx->v_data_ptr = bs_hdr + v_offset;
00935     ctx->u_data_ptr = bs_hdr + u_offset;
00936     ctx->alt_quant  = buf_ptr + sizeof(uint32_t);
00937 
00938     if (ctx->data_size == 16) {
00939         av_log(avctx, AV_LOG_DEBUG, "Sync frame encountered!\n");
00940         return 16;
00941     }
00942 
00943     if (ctx->frame_flags & BS_8BIT_PEL) {
00944         av_log_ask_for_sample(avctx, "8-bit pixel format\n");
00945         return AVERROR_PATCHWELCOME;
00946     }
00947 
00948     if (ctx->frame_flags & BS_MV_X_HALF || ctx->frame_flags & BS_MV_Y_HALF) {
00949         av_log_ask_for_sample(avctx, "halfpel motion vectors\n");
00950         return AVERROR_PATCHWELCOME;
00951     }
00952 
00953     return 0;
00954 }
00955 
00956 
00966 static void output_plane(const Plane *plane, int buf_sel, uint8_t *dst, int dst_pitch)
00967 {
00968     int             x,y;
00969     const uint8_t   *src  = plane->pixels[buf_sel];
00970     uint32_t        pitch = plane->pitch;
00971 
00972     for (y = 0; y < plane->height; y++) {
00973         /* convert four pixels at once using SWAR */
00974         for (x = 0; x < plane->width >> 2; x++) {
00975             AV_WN32A(dst, (AV_RN32A(src) & 0x7F7F7F7F) << 1);
00976             src += 4;
00977             dst += 4;
00978         }
00979 
00980         for (x <<= 2; x < plane->width; x++)
00981             *dst++ = *src++ << 1;
00982 
00983         src += pitch     - plane->width;
00984         dst += dst_pitch - plane->width;
00985     }
00986 }
00987 
00988 
00989 static av_cold int decode_init(AVCodecContext *avctx)
00990 {
00991     Indeo3DecodeContext *ctx = avctx->priv_data;
00992 
00993     ctx->avctx     = avctx;
00994     ctx->width     = avctx->width;
00995     ctx->height    = avctx->height;
00996     avctx->pix_fmt = PIX_FMT_YUV410P;
00997 
00998     build_requant_tab();
00999 
01000     dsputil_init(&ctx->dsp, avctx);
01001 
01002     allocate_frame_buffers(ctx, avctx);
01003 
01004     return 0;
01005 }
01006 
01007 
01008 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
01009                         AVPacket *avpkt)
01010 {
01011     Indeo3DecodeContext *ctx = avctx->priv_data;
01012     const uint8_t *buf = avpkt->data;
01013     int buf_size       = avpkt->size;
01014     int res;
01015 
01016     res = decode_frame_headers(ctx, avctx, buf, buf_size);
01017     if (res < 0)
01018         return res;
01019 
01020     /* skip sync(null) frames */
01021     if (res) {
01022         // we have processed 16 bytes but no data was decoded
01023         *data_size = 0;
01024         return buf_size;
01025     }
01026 
01027     /* skip droppable INTER frames if requested */
01028     if (ctx->frame_flags & BS_NONREF &&
01029        (avctx->skip_frame >= AVDISCARD_NONREF))
01030         return 0;
01031 
01032     /* skip INTER frames if requested */
01033     if (!(ctx->frame_flags & BS_KEYFRAME) && avctx->skip_frame >= AVDISCARD_NONKEY)
01034         return 0;
01035 
01036     /* use BS_BUFFER flag for buffer switching */
01037     ctx->buf_sel = (ctx->frame_flags >> BS_BUFFER) & 1;
01038 
01039     /* decode luma plane */
01040     if ((res = decode_plane(ctx, avctx, ctx->planes, ctx->y_data_ptr, ctx->y_data_size, 40)))
01041         return res;
01042 
01043     /* decode chroma planes */
01044     if ((res = decode_plane(ctx, avctx, &ctx->planes[1], ctx->u_data_ptr, ctx->u_data_size, 10)))
01045         return res;
01046 
01047     if ((res = decode_plane(ctx, avctx, &ctx->planes[2], ctx->v_data_ptr, ctx->v_data_size, 10)))
01048         return res;
01049 
01050     if (ctx->frame.data[0])
01051         avctx->release_buffer(avctx, &ctx->frame);
01052 
01053     ctx->frame.reference = 0;
01054     if ((res = avctx->get_buffer(avctx, &ctx->frame)) < 0) {
01055         av_log(ctx->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
01056         return res;
01057     }
01058 
01059     output_plane(&ctx->planes[0], ctx->buf_sel, ctx->frame.data[0], ctx->frame.linesize[0]);
01060     output_plane(&ctx->planes[1], ctx->buf_sel, ctx->frame.data[1], ctx->frame.linesize[1]);
01061     output_plane(&ctx->planes[2], ctx->buf_sel, ctx->frame.data[2], ctx->frame.linesize[2]);
01062 
01063     *data_size      = sizeof(AVFrame);
01064     *(AVFrame*)data = ctx->frame;
01065 
01066     return buf_size;
01067 }
01068 
01069 
01070 static av_cold int decode_close(AVCodecContext *avctx)
01071 {
01072     Indeo3DecodeContext *ctx = avctx->priv_data;
01073 
01074     free_frame_buffers(avctx->priv_data);
01075 
01076     if (ctx->frame.data[0])
01077         avctx->release_buffer(avctx, &ctx->frame);
01078 
01079     return 0;
01080 }
01081 
01082 AVCodec ff_indeo3_decoder = {
01083     .name           = "indeo3",
01084     .type           = AVMEDIA_TYPE_VIDEO,
01085     .id             = CODEC_ID_INDEO3,
01086     .priv_data_size = sizeof(Indeo3DecodeContext),
01087     .init           = decode_init,
01088     .close          = decode_close,
01089     .decode         = decode_frame,
01090     .long_name      = NULL_IF_CONFIG_SMALL("Intel Indeo 3"),
01091 };
Generated on Sun Apr 22 2012 21:54:01 for Libav by doxygen 1.7.1