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

libavcodec/truemotion1.c

Go to the documentation of this file.
00001 /*
00002  * Duck TrueMotion 1.0 Decoder
00003  * Copyright (C) 2003 Alex Beregszaszi & Mike Melanson
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg 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  * FFmpeg 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 FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00032 #include <stdio.h>
00033 #include <stdlib.h>
00034 #include <string.h>
00035 
00036 #include "avcodec.h"
00037 #include "dsputil.h"
00038 
00039 #include "truemotion1data.h"
00040 
00041 typedef struct TrueMotion1Context {
00042     AVCodecContext *avctx;
00043     AVFrame frame;
00044 
00045     const uint8_t *buf;
00046     int size;
00047 
00048     const uint8_t *mb_change_bits;
00049     int mb_change_bits_row_size;
00050     const uint8_t *index_stream;
00051     int index_stream_size;
00052 
00053     int flags;
00054     int x, y, w, h;
00055 
00056     uint32_t y_predictor_table[1024];
00057     uint32_t c_predictor_table[1024];
00058     uint32_t fat_y_predictor_table[1024];
00059     uint32_t fat_c_predictor_table[1024];
00060 
00061     int compression;
00062     int block_type;
00063     int block_width;
00064     int block_height;
00065 
00066     int16_t ydt[8];
00067     int16_t cdt[8];
00068     int16_t fat_ydt[8];
00069     int16_t fat_cdt[8];
00070 
00071     int last_deltaset, last_vectable;
00072 
00073     unsigned int *vert_pred;
00074 
00075 } TrueMotion1Context;
00076 
00077 #define FLAG_SPRITE         32
00078 #define FLAG_KEYFRAME       16
00079 #define FLAG_INTERFRAME      8
00080 #define FLAG_INTERPOLATED    4
00081 
00082 struct frame_header {
00083     uint8_t header_size;
00084     uint8_t compression;
00085     uint8_t deltaset;
00086     uint8_t vectable;
00087     uint16_t ysize;
00088     uint16_t xsize;
00089     uint16_t checksum;
00090     uint8_t version;
00091     uint8_t header_type;
00092     uint8_t flags;
00093     uint8_t control;
00094     uint16_t xoffset;
00095     uint16_t yoffset;
00096     uint16_t width;
00097     uint16_t height;
00098 };
00099 
00100 #define ALGO_NOP        0
00101 #define ALGO_RGB16V     1
00102 #define ALGO_RGB16H     2
00103 #define ALGO_RGB24H     3
00104 
00105 /* these are the various block sizes that can occupy a 4x4 block */
00106 #define BLOCK_2x2  0
00107 #define BLOCK_2x4  1
00108 #define BLOCK_4x2  2
00109 #define BLOCK_4x4  3
00110 
00111 typedef struct comp_types {
00112     int algorithm;
00113     int block_width; // vres
00114     int block_height; // hres
00115     int block_type;
00116 } comp_types;
00117 
00118 /* { valid for metatype }, algorithm, num of deltas, vert res, horiz res */
00119 static const comp_types compression_types[17] = {
00120     { ALGO_NOP,    0, 0, 0 },
00121 
00122     { ALGO_RGB16V, 4, 4, BLOCK_4x4 },
00123     { ALGO_RGB16H, 4, 4, BLOCK_4x4 },
00124     { ALGO_RGB16V, 4, 2, BLOCK_4x2 },
00125     { ALGO_RGB16H, 4, 2, BLOCK_4x2 },
00126 
00127     { ALGO_RGB16V, 2, 4, BLOCK_2x4 },
00128     { ALGO_RGB16H, 2, 4, BLOCK_2x4 },
00129     { ALGO_RGB16V, 2, 2, BLOCK_2x2 },
00130     { ALGO_RGB16H, 2, 2, BLOCK_2x2 },
00131 
00132     { ALGO_NOP,    4, 4, BLOCK_4x4 },
00133     { ALGO_RGB24H, 4, 4, BLOCK_4x4 },
00134     { ALGO_NOP,    4, 2, BLOCK_4x2 },
00135     { ALGO_RGB24H, 4, 2, BLOCK_4x2 },
00136 
00137     { ALGO_NOP,    2, 4, BLOCK_2x4 },
00138     { ALGO_RGB24H, 2, 4, BLOCK_2x4 },
00139     { ALGO_NOP,    2, 2, BLOCK_2x2 },
00140     { ALGO_RGB24H, 2, 2, BLOCK_2x2 }
00141 };
00142 
00143 static void select_delta_tables(TrueMotion1Context *s, int delta_table_index)
00144 {
00145     int i;
00146 
00147     if (delta_table_index > 3)
00148         return;
00149 
00150     memcpy(s->ydt, ydts[delta_table_index], 8 * sizeof(int16_t));
00151     memcpy(s->cdt, cdts[delta_table_index], 8 * sizeof(int16_t));
00152     memcpy(s->fat_ydt, fat_ydts[delta_table_index], 8 * sizeof(int16_t));
00153     memcpy(s->fat_cdt, fat_cdts[delta_table_index], 8 * sizeof(int16_t));
00154 
00155     /* Y skinny deltas need to be halved for some reason; maybe the
00156      * skinny Y deltas should be modified */
00157     for (i = 0; i < 8; i++)
00158     {
00159         /* drop the lsb before dividing by 2-- net effect: round down
00160          * when dividing a negative number (e.g., -3/2 = -2, not -1) */
00161         s->ydt[i] &= 0xFFFE;
00162         s->ydt[i] /= 2;
00163     }
00164 }
00165 
00166 #if HAVE_BIGENDIAN
00167 static int make_ydt15_entry(int p2, int p1, int16_t *ydt)
00168 #else
00169 static int make_ydt15_entry(int p1, int p2, int16_t *ydt)
00170 #endif
00171 {
00172     int lo, hi;
00173 
00174     lo = ydt[p1];
00175     lo += (lo << 5) + (lo << 10);
00176     hi = ydt[p2];
00177     hi += (hi << 5) + (hi << 10);
00178     return (lo + (hi << 16)) << 1;
00179 }
00180 
00181 #if HAVE_BIGENDIAN
00182 static int make_cdt15_entry(int p2, int p1, int16_t *cdt)
00183 #else
00184 static int make_cdt15_entry(int p1, int p2, int16_t *cdt)
00185 #endif
00186 {
00187     int r, b, lo;
00188 
00189     b = cdt[p2];
00190     r = cdt[p1] << 10;
00191     lo = b + r;
00192     return (lo + (lo << 16)) << 1;
00193 }
00194 
00195 #if HAVE_BIGENDIAN
00196 static int make_ydt16_entry(int p2, int p1, int16_t *ydt)
00197 #else
00198 static int make_ydt16_entry(int p1, int p2, int16_t *ydt)
00199 #endif
00200 {
00201     int lo, hi;
00202 
00203     lo = ydt[p1];
00204     lo += (lo << 6) + (lo << 11);
00205     hi = ydt[p2];
00206     hi += (hi << 6) + (hi << 11);
00207     return (lo + (hi << 16)) << 1;
00208 }
00209 
00210 #if HAVE_BIGENDIAN
00211 static int make_cdt16_entry(int p2, int p1, int16_t *cdt)
00212 #else
00213 static int make_cdt16_entry(int p1, int p2, int16_t *cdt)
00214 #endif
00215 {
00216     int r, b, lo;
00217 
00218     b = cdt[p2];
00219     r = cdt[p1] << 11;
00220     lo = b + r;
00221     return (lo + (lo << 16)) << 1;
00222 }
00223 
00224 #if HAVE_BIGENDIAN
00225 static int make_ydt24_entry(int p2, int p1, int16_t *ydt)
00226 #else
00227 static int make_ydt24_entry(int p1, int p2, int16_t *ydt)
00228 #endif
00229 {
00230     int lo, hi;
00231 
00232     lo = ydt[p1];
00233     hi = ydt[p2];
00234     return (lo + (hi << 8) + (hi << 16)) << 1;
00235 }
00236 
00237 #if HAVE_BIGENDIAN
00238 static int make_cdt24_entry(int p2, int p1, int16_t *cdt)
00239 #else
00240 static int make_cdt24_entry(int p1, int p2, int16_t *cdt)
00241 #endif
00242 {
00243     int r, b;
00244 
00245     b = cdt[p2];
00246     r = cdt[p1]<<16;
00247     return (b+r) << 1;
00248 }
00249 
00250 static void gen_vector_table15(TrueMotion1Context *s, const uint8_t *sel_vector_table)
00251 {
00252     int len, i, j;
00253     unsigned char delta_pair;
00254 
00255     for (i = 0; i < 1024; i += 4)
00256     {
00257         len = *sel_vector_table++ / 2;
00258         for (j = 0; j < len; j++)
00259         {
00260             delta_pair = *sel_vector_table++;
00261             s->y_predictor_table[i+j] = 0xfffffffe &
00262                 make_ydt15_entry(delta_pair >> 4, delta_pair & 0xf, s->ydt);
00263             s->c_predictor_table[i+j] = 0xfffffffe &
00264                 make_cdt15_entry(delta_pair >> 4, delta_pair & 0xf, s->cdt);
00265         }
00266         s->y_predictor_table[i+(j-1)] |= 1;
00267         s->c_predictor_table[i+(j-1)] |= 1;
00268     }
00269 }
00270 
00271 static void gen_vector_table16(TrueMotion1Context *s, const uint8_t *sel_vector_table)
00272 {
00273     int len, i, j;
00274     unsigned char delta_pair;
00275 
00276     for (i = 0; i < 1024; i += 4)
00277     {
00278         len = *sel_vector_table++ / 2;
00279         for (j = 0; j < len; j++)
00280         {
00281             delta_pair = *sel_vector_table++;
00282             s->y_predictor_table[i+j] = 0xfffffffe &
00283                 make_ydt16_entry(delta_pair >> 4, delta_pair & 0xf, s->ydt);
00284             s->c_predictor_table[i+j] = 0xfffffffe &
00285                 make_cdt16_entry(delta_pair >> 4, delta_pair & 0xf, s->cdt);
00286         }
00287         s->y_predictor_table[i+(j-1)] |= 1;
00288         s->c_predictor_table[i+(j-1)] |= 1;
00289     }
00290 }
00291 
00292 static void gen_vector_table24(TrueMotion1Context *s, const uint8_t *sel_vector_table)
00293 {
00294     int len, i, j;
00295     unsigned char delta_pair;
00296 
00297     for (i = 0; i < 1024; i += 4)
00298     {
00299         len = *sel_vector_table++ / 2;
00300         for (j = 0; j < len; j++)
00301         {
00302             delta_pair = *sel_vector_table++;
00303             s->y_predictor_table[i+j] = 0xfffffffe &
00304                 make_ydt24_entry(delta_pair >> 4, delta_pair & 0xf, s->ydt);
00305             s->c_predictor_table[i+j] = 0xfffffffe &
00306                 make_cdt24_entry(delta_pair >> 4, delta_pair & 0xf, s->cdt);
00307             s->fat_y_predictor_table[i+j] = 0xfffffffe &
00308                 make_ydt24_entry(delta_pair >> 4, delta_pair & 0xf, s->fat_ydt);
00309             s->fat_c_predictor_table[i+j] = 0xfffffffe &
00310                 make_cdt24_entry(delta_pair >> 4, delta_pair & 0xf, s->fat_cdt);
00311         }
00312         s->y_predictor_table[i+(j-1)] |= 1;
00313         s->c_predictor_table[i+(j-1)] |= 1;
00314         s->fat_y_predictor_table[i+(j-1)] |= 1;
00315         s->fat_c_predictor_table[i+(j-1)] |= 1;
00316     }
00317 }
00318 
00319 /* Returns the number of bytes consumed from the bytestream. Returns -1 if
00320  * there was an error while decoding the header */
00321 static int truemotion1_decode_header(TrueMotion1Context *s)
00322 {
00323     int i;
00324     struct frame_header header;
00325     uint8_t header_buffer[128];  /* logical maximum size of the header */
00326     const uint8_t *sel_vector_table;
00327 
00328     /* There is 1 change bit per 4 pixels, so each change byte represents
00329      * 32 pixels; divide width by 4 to obtain the number of change bits and
00330      * then round up to the nearest byte. */
00331     s->mb_change_bits_row_size = ((s->avctx->width >> 2) + 7) >> 3;
00332 
00333     header.header_size = ((s->buf[0] >> 5) | (s->buf[0] << 3)) & 0x7f;
00334     if (s->buf[0] < 0x10)
00335     {
00336         av_log(s->avctx, AV_LOG_ERROR, "invalid header size (%d)\n", s->buf[0]);
00337         return -1;
00338     }
00339 
00340     /* unscramble the header bytes with a XOR operation */
00341     memset(header_buffer, 0, 128);
00342     for (i = 1; i < header.header_size; i++)
00343         header_buffer[i - 1] = s->buf[i] ^ s->buf[i + 1];
00344 
00345     header.compression = header_buffer[0];
00346     header.deltaset = header_buffer[1];
00347     header.vectable = header_buffer[2];
00348     header.ysize = AV_RL16(&header_buffer[3]);
00349     header.xsize = AV_RL16(&header_buffer[5]);
00350     header.checksum = AV_RL16(&header_buffer[7]);
00351     header.version = header_buffer[9];
00352     header.header_type = header_buffer[10];
00353     header.flags = header_buffer[11];
00354     header.control = header_buffer[12];
00355 
00356     /* Version 2 */
00357     if (header.version >= 2)
00358     {
00359         if (header.header_type > 3)
00360         {
00361             av_log(s->avctx, AV_LOG_ERROR, "invalid header type (%d)\n", header.header_type);
00362             return -1;
00363         } else if ((header.header_type == 2) || (header.header_type == 3)) {
00364             s->flags = header.flags;
00365             if (!(s->flags & FLAG_INTERFRAME))
00366                 s->flags |= FLAG_KEYFRAME;
00367         } else
00368             s->flags = FLAG_KEYFRAME;
00369     } else /* Version 1 */
00370         s->flags = FLAG_KEYFRAME;
00371 
00372     if (s->flags & FLAG_SPRITE) {
00373         av_log(s->avctx, AV_LOG_INFO, "SPRITE frame found, please report the sample to the developers\n");
00374         /* FIXME header.width, height, xoffset and yoffset aren't initialized */
00375 #if 0
00376         s->w = header.width;
00377         s->h = header.height;
00378         s->x = header.xoffset;
00379         s->y = header.yoffset;
00380 #else
00381         return -1;
00382 #endif
00383     } else {
00384         s->w = header.xsize;
00385         s->h = header.ysize;
00386         if (header.header_type < 2) {
00387             if ((s->w < 213) && (s->h >= 176))
00388             {
00389                 s->flags |= FLAG_INTERPOLATED;
00390                 av_log(s->avctx, AV_LOG_INFO, "INTERPOLATION selected, please report the sample to the developers\n");
00391             }
00392         }
00393     }
00394 
00395     if (header.compression >= 17) {
00396         av_log(s->avctx, AV_LOG_ERROR, "invalid compression type (%d)\n", header.compression);
00397         return -1;
00398     }
00399 
00400     if ((header.deltaset != s->last_deltaset) ||
00401         (header.vectable != s->last_vectable))
00402         select_delta_tables(s, header.deltaset);
00403 
00404     if ((header.compression & 1) && header.header_type)
00405         sel_vector_table = pc_tbl2;
00406     else {
00407         if (header.vectable < 4)
00408             sel_vector_table = tables[header.vectable - 1];
00409         else {
00410             av_log(s->avctx, AV_LOG_ERROR, "invalid vector table id (%d)\n", header.vectable);
00411             return -1;
00412         }
00413     }
00414 
00415     // FIXME: where to place this ?!?!
00416     if (compression_types[header.compression].algorithm == ALGO_RGB24H)
00417         s->avctx->pix_fmt = PIX_FMT_RGB32;
00418     else
00419         s->avctx->pix_fmt = PIX_FMT_RGB555; // RGB565 is supported as well
00420 
00421     if ((header.deltaset != s->last_deltaset) || (header.vectable != s->last_vectable))
00422     {
00423         if (compression_types[header.compression].algorithm == ALGO_RGB24H)
00424             gen_vector_table24(s, sel_vector_table);
00425         else
00426         if (s->avctx->pix_fmt == PIX_FMT_RGB555)
00427             gen_vector_table15(s, sel_vector_table);
00428         else
00429             gen_vector_table16(s, sel_vector_table);
00430     }
00431 
00432     /* set up pointers to the other key data chunks */
00433     s->mb_change_bits = s->buf + header.header_size;
00434     if (s->flags & FLAG_KEYFRAME) {
00435         /* no change bits specified for a keyframe; only index bytes */
00436         s->index_stream = s->mb_change_bits;
00437     } else {
00438         /* one change bit per 4x4 block */
00439         s->index_stream = s->mb_change_bits +
00440             (s->mb_change_bits_row_size * (s->avctx->height >> 2));
00441     }
00442     s->index_stream_size = s->size - (s->index_stream - s->buf);
00443 
00444     s->last_deltaset = header.deltaset;
00445     s->last_vectable = header.vectable;
00446     s->compression = header.compression;
00447     s->block_width = compression_types[header.compression].block_width;
00448     s->block_height = compression_types[header.compression].block_height;
00449     s->block_type = compression_types[header.compression].block_type;
00450 
00451     if (s->avctx->debug & FF_DEBUG_PICT_INFO)
00452         av_log(s->avctx, AV_LOG_INFO, "tables: %d / %d c:%d %dx%d t:%d %s%s%s%s\n",
00453             s->last_deltaset, s->last_vectable, s->compression, s->block_width,
00454             s->block_height, s->block_type,
00455             s->flags & FLAG_KEYFRAME ? " KEY" : "",
00456             s->flags & FLAG_INTERFRAME ? " INTER" : "",
00457             s->flags & FLAG_SPRITE ? " SPRITE" : "",
00458             s->flags & FLAG_INTERPOLATED ? " INTERPOL" : "");
00459 
00460     return header.header_size;
00461 }
00462 
00463 static av_cold int truemotion1_decode_init(AVCodecContext *avctx)
00464 {
00465     TrueMotion1Context *s = avctx->priv_data;
00466 
00467     s->avctx = avctx;
00468 
00469     // FIXME: it may change ?
00470 //    if (avctx->bits_per_sample == 24)
00471 //        avctx->pix_fmt = PIX_FMT_RGB24;
00472 //    else
00473 //        avctx->pix_fmt = PIX_FMT_RGB555;
00474 
00475     s->frame.data[0] = NULL;
00476 
00477     /* there is a vertical predictor for each pixel in a line; each vertical
00478      * predictor is 0 to start with */
00479     s->vert_pred =
00480         (unsigned int *)av_malloc(s->avctx->width * sizeof(unsigned int));
00481 
00482     return 0;
00483 }
00484 
00485 /*
00486 Block decoding order:
00487 
00488 dxi: Y-Y
00489 dxic: Y-C-Y
00490 dxic2: Y-C-Y-C
00491 
00492 hres,vres,i,i%vres (0 < i < 4)
00493 2x2 0: 0 dxic2
00494 2x2 1: 1 dxi
00495 2x2 2: 0 dxic2
00496 2x2 3: 1 dxi
00497 2x4 0: 0 dxic2
00498 2x4 1: 1 dxi
00499 2x4 2: 2 dxi
00500 2x4 3: 3 dxi
00501 4x2 0: 0 dxic
00502 4x2 1: 1 dxi
00503 4x2 2: 0 dxic
00504 4x2 3: 1 dxi
00505 4x4 0: 0 dxic
00506 4x4 1: 1 dxi
00507 4x4 2: 2 dxi
00508 4x4 3: 3 dxi
00509 */
00510 
00511 #define GET_NEXT_INDEX() \
00512 {\
00513     if (index_stream_index >= s->index_stream_size) { \
00514         av_log(s->avctx, AV_LOG_INFO, " help! truemotion1 decoder went out of bounds\n"); \
00515         return; \
00516     } \
00517     index = s->index_stream[index_stream_index++] * 4; \
00518 }
00519 
00520 #define APPLY_C_PREDICTOR() \
00521     predictor_pair = s->c_predictor_table[index]; \
00522     horiz_pred += (predictor_pair >> 1); \
00523     if (predictor_pair & 1) { \
00524         GET_NEXT_INDEX() \
00525         if (!index) { \
00526             GET_NEXT_INDEX() \
00527             predictor_pair = s->c_predictor_table[index]; \
00528             horiz_pred += ((predictor_pair >> 1) * 5); \
00529             if (predictor_pair & 1) \
00530                 GET_NEXT_INDEX() \
00531             else \
00532                 index++; \
00533         } \
00534     } else \
00535         index++;
00536 
00537 #define APPLY_C_PREDICTOR_24() \
00538     predictor_pair = s->c_predictor_table[index]; \
00539     horiz_pred += (predictor_pair >> 1); \
00540     if (predictor_pair & 1) { \
00541         GET_NEXT_INDEX() \
00542         if (!index) { \
00543             GET_NEXT_INDEX() \
00544             predictor_pair = s->fat_c_predictor_table[index]; \
00545             horiz_pred += (predictor_pair >> 1); \
00546             if (predictor_pair & 1) \
00547                 GET_NEXT_INDEX() \
00548             else \
00549                 index++; \
00550         } \
00551     } else \
00552         index++;
00553 
00554 
00555 #define APPLY_Y_PREDICTOR() \
00556     predictor_pair = s->y_predictor_table[index]; \
00557     horiz_pred += (predictor_pair >> 1); \
00558     if (predictor_pair & 1) { \
00559         GET_NEXT_INDEX() \
00560         if (!index) { \
00561             GET_NEXT_INDEX() \
00562             predictor_pair = s->y_predictor_table[index]; \
00563             horiz_pred += ((predictor_pair >> 1) * 5); \
00564             if (predictor_pair & 1) \
00565                 GET_NEXT_INDEX() \
00566             else \
00567                 index++; \
00568         } \
00569     } else \
00570         index++;
00571 
00572 #define APPLY_Y_PREDICTOR_24() \
00573     predictor_pair = s->y_predictor_table[index]; \
00574     horiz_pred += (predictor_pair >> 1); \
00575     if (predictor_pair & 1) { \
00576         GET_NEXT_INDEX() \
00577         if (!index) { \
00578             GET_NEXT_INDEX() \
00579             predictor_pair = s->fat_y_predictor_table[index]; \
00580             horiz_pred += (predictor_pair >> 1); \
00581             if (predictor_pair & 1) \
00582                 GET_NEXT_INDEX() \
00583             else \
00584                 index++; \
00585         } \
00586     } else \
00587         index++;
00588 
00589 #define OUTPUT_PIXEL_PAIR() \
00590     *current_pixel_pair = *vert_pred + horiz_pred; \
00591     *vert_pred++ = *current_pixel_pair++;
00592 
00593 static void truemotion1_decode_16bit(TrueMotion1Context *s)
00594 {
00595     int y;
00596     int pixels_left;  /* remaining pixels on this line */
00597     unsigned int predictor_pair;
00598     unsigned int horiz_pred;
00599     unsigned int *vert_pred;
00600     unsigned int *current_pixel_pair;
00601     unsigned char *current_line = s->frame.data[0];
00602     int keyframe = s->flags & FLAG_KEYFRAME;
00603 
00604     /* these variables are for managing the stream of macroblock change bits */
00605     const unsigned char *mb_change_bits = s->mb_change_bits;
00606     unsigned char mb_change_byte;
00607     unsigned char mb_change_byte_mask;
00608     int mb_change_index;
00609 
00610     /* these variables are for managing the main index stream */
00611     int index_stream_index = 0;  /* yes, the index into the index stream */
00612     int index;
00613 
00614     /* clean out the line buffer */
00615     memset(s->vert_pred, 0, s->avctx->width * sizeof(unsigned int));
00616 
00617     GET_NEXT_INDEX();
00618 
00619     for (y = 0; y < s->avctx->height; y++) {
00620 
00621         /* re-init variables for the next line iteration */
00622         horiz_pred = 0;
00623         current_pixel_pair = (unsigned int *)current_line;
00624         vert_pred = s->vert_pred;
00625         mb_change_index = 0;
00626         mb_change_byte = mb_change_bits[mb_change_index++];
00627         mb_change_byte_mask = 0x01;
00628         pixels_left = s->avctx->width;
00629 
00630         while (pixels_left > 0) {
00631 
00632             if (keyframe || ((mb_change_byte & mb_change_byte_mask) == 0)) {
00633 
00634                 switch (y & 3) {
00635                 case 0:
00636                     /* if macroblock width is 2, apply C-Y-C-Y; else
00637                      * apply C-Y-Y */
00638                     if (s->block_width == 2) {
00639                         APPLY_C_PREDICTOR();
00640                         APPLY_Y_PREDICTOR();
00641                         OUTPUT_PIXEL_PAIR();
00642                         APPLY_C_PREDICTOR();
00643                         APPLY_Y_PREDICTOR();
00644                         OUTPUT_PIXEL_PAIR();
00645                     } else {
00646                         APPLY_C_PREDICTOR();
00647                         APPLY_Y_PREDICTOR();
00648                         OUTPUT_PIXEL_PAIR();
00649                         APPLY_Y_PREDICTOR();
00650                         OUTPUT_PIXEL_PAIR();
00651                     }
00652                     break;
00653 
00654                 case 1:
00655                 case 3:
00656                     /* always apply 2 Y predictors on these iterations */
00657                     APPLY_Y_PREDICTOR();
00658                     OUTPUT_PIXEL_PAIR();
00659                     APPLY_Y_PREDICTOR();
00660                     OUTPUT_PIXEL_PAIR();
00661                     break;
00662 
00663                 case 2:
00664                     /* this iteration might be C-Y-C-Y, Y-Y, or C-Y-Y
00665                      * depending on the macroblock type */
00666                     if (s->block_type == BLOCK_2x2) {
00667                         APPLY_C_PREDICTOR();
00668                         APPLY_Y_PREDICTOR();
00669                         OUTPUT_PIXEL_PAIR();
00670                         APPLY_C_PREDICTOR();
00671                         APPLY_Y_PREDICTOR();
00672                         OUTPUT_PIXEL_PAIR();
00673                     } else if (s->block_type == BLOCK_4x2) {
00674                         APPLY_C_PREDICTOR();
00675                         APPLY_Y_PREDICTOR();
00676                         OUTPUT_PIXEL_PAIR();
00677                         APPLY_Y_PREDICTOR();
00678                         OUTPUT_PIXEL_PAIR();
00679                     } else {
00680                         APPLY_Y_PREDICTOR();
00681                         OUTPUT_PIXEL_PAIR();
00682                         APPLY_Y_PREDICTOR();
00683                         OUTPUT_PIXEL_PAIR();
00684                     }
00685                     break;
00686                 }
00687 
00688             } else {
00689 
00690                 /* skip (copy) four pixels, but reassign the horizontal
00691                  * predictor */
00692                 *vert_pred++ = *current_pixel_pair++;
00693                 horiz_pred = *current_pixel_pair - *vert_pred;
00694                 *vert_pred++ = *current_pixel_pair++;
00695 
00696             }
00697 
00698             if (!keyframe) {
00699                 mb_change_byte_mask <<= 1;
00700 
00701                 /* next byte */
00702                 if (!mb_change_byte_mask) {
00703                     mb_change_byte = mb_change_bits[mb_change_index++];
00704                     mb_change_byte_mask = 0x01;
00705                 }
00706             }
00707 
00708             pixels_left -= 4;
00709         }
00710 
00711         /* next change row */
00712         if (((y + 1) & 3) == 0)
00713             mb_change_bits += s->mb_change_bits_row_size;
00714 
00715         current_line += s->frame.linesize[0];
00716     }
00717 }
00718 
00719 static void truemotion1_decode_24bit(TrueMotion1Context *s)
00720 {
00721     int y;
00722     int pixels_left;  /* remaining pixels on this line */
00723     unsigned int predictor_pair;
00724     unsigned int horiz_pred;
00725     unsigned int *vert_pred;
00726     unsigned int *current_pixel_pair;
00727     unsigned char *current_line = s->frame.data[0];
00728     int keyframe = s->flags & FLAG_KEYFRAME;
00729 
00730     /* these variables are for managing the stream of macroblock change bits */
00731     const unsigned char *mb_change_bits = s->mb_change_bits;
00732     unsigned char mb_change_byte;
00733     unsigned char mb_change_byte_mask;
00734     int mb_change_index;
00735 
00736     /* these variables are for managing the main index stream */
00737     int index_stream_index = 0;  /* yes, the index into the index stream */
00738     int index;
00739 
00740     /* clean out the line buffer */
00741     memset(s->vert_pred, 0, s->avctx->width * sizeof(unsigned int));
00742 
00743     GET_NEXT_INDEX();
00744 
00745     for (y = 0; y < s->avctx->height; y++) {
00746 
00747         /* re-init variables for the next line iteration */
00748         horiz_pred = 0;
00749         current_pixel_pair = (unsigned int *)current_line;
00750         vert_pred = s->vert_pred;
00751         mb_change_index = 0;
00752         mb_change_byte = mb_change_bits[mb_change_index++];
00753         mb_change_byte_mask = 0x01;
00754         pixels_left = s->avctx->width;
00755 
00756         while (pixels_left > 0) {
00757 
00758             if (keyframe || ((mb_change_byte & mb_change_byte_mask) == 0)) {
00759 
00760                 switch (y & 3) {
00761                 case 0:
00762                     /* if macroblock width is 2, apply C-Y-C-Y; else
00763                      * apply C-Y-Y */
00764                     if (s->block_width == 2) {
00765                         APPLY_C_PREDICTOR_24();
00766                         APPLY_Y_PREDICTOR_24();
00767                         OUTPUT_PIXEL_PAIR();
00768                         APPLY_C_PREDICTOR_24();
00769                         APPLY_Y_PREDICTOR_24();
00770                         OUTPUT_PIXEL_PAIR();
00771                     } else {
00772                         APPLY_C_PREDICTOR_24();
00773                         APPLY_Y_PREDICTOR_24();
00774                         OUTPUT_PIXEL_PAIR();
00775                         APPLY_Y_PREDICTOR_24();
00776                         OUTPUT_PIXEL_PAIR();
00777                     }
00778                     break;
00779 
00780                 case 1:
00781                 case 3:
00782                     /* always apply 2 Y predictors on these iterations */
00783                     APPLY_Y_PREDICTOR_24();
00784                     OUTPUT_PIXEL_PAIR();
00785                     APPLY_Y_PREDICTOR_24();
00786                     OUTPUT_PIXEL_PAIR();
00787                     break;
00788 
00789                 case 2:
00790                     /* this iteration might be C-Y-C-Y, Y-Y, or C-Y-Y
00791                      * depending on the macroblock type */
00792                     if (s->block_type == BLOCK_2x2) {
00793                         APPLY_C_PREDICTOR_24();
00794                         APPLY_Y_PREDICTOR_24();
00795                         OUTPUT_PIXEL_PAIR();
00796                         APPLY_C_PREDICTOR_24();
00797                         APPLY_Y_PREDICTOR_24();
00798                         OUTPUT_PIXEL_PAIR();
00799                     } else if (s->block_type == BLOCK_4x2) {
00800                         APPLY_C_PREDICTOR_24();
00801                         APPLY_Y_PREDICTOR_24();
00802                         OUTPUT_PIXEL_PAIR();
00803                         APPLY_Y_PREDICTOR_24();
00804                         OUTPUT_PIXEL_PAIR();
00805                     } else {
00806                         APPLY_Y_PREDICTOR_24();
00807                         OUTPUT_PIXEL_PAIR();
00808                         APPLY_Y_PREDICTOR_24();
00809                         OUTPUT_PIXEL_PAIR();
00810                     }
00811                     break;
00812                 }
00813 
00814             } else {
00815 
00816                 /* skip (copy) four pixels, but reassign the horizontal
00817                  * predictor */
00818                 *vert_pred++ = *current_pixel_pair++;
00819                 horiz_pred = *current_pixel_pair - *vert_pred;
00820                 *vert_pred++ = *current_pixel_pair++;
00821 
00822             }
00823 
00824             if (!keyframe) {
00825                 mb_change_byte_mask <<= 1;
00826 
00827                 /* next byte */
00828                 if (!mb_change_byte_mask) {
00829                     mb_change_byte = mb_change_bits[mb_change_index++];
00830                     mb_change_byte_mask = 0x01;
00831                 }
00832             }
00833 
00834             pixels_left -= 4;
00835         }
00836 
00837         /* next change row */
00838         if (((y + 1) & 3) == 0)
00839             mb_change_bits += s->mb_change_bits_row_size;
00840 
00841         current_line += s->frame.linesize[0];
00842     }
00843 }
00844 
00845 
00846 static int truemotion1_decode_frame(AVCodecContext *avctx,
00847                                     void *data, int *data_size,
00848                                     AVPacket *avpkt)
00849 {
00850     const uint8_t *buf = avpkt->data;
00851     int buf_size = avpkt->size;
00852     TrueMotion1Context *s = avctx->priv_data;
00853 
00854     s->buf = buf;
00855     s->size = buf_size;
00856 
00857     if (truemotion1_decode_header(s) == -1)
00858         return -1;
00859 
00860     s->frame.reference = 1;
00861     s->frame.buffer_hints = FF_BUFFER_HINTS_VALID |
00862         FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
00863     if (avctx->reget_buffer(avctx, &s->frame) < 0) {
00864         av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00865         return -1;
00866     }
00867 
00868     if (compression_types[s->compression].algorithm == ALGO_RGB24H) {
00869         truemotion1_decode_24bit(s);
00870     } else if (compression_types[s->compression].algorithm != ALGO_NOP) {
00871         truemotion1_decode_16bit(s);
00872     }
00873 
00874     *data_size = sizeof(AVFrame);
00875     *(AVFrame*)data = s->frame;
00876 
00877     /* report that the buffer was completely consumed */
00878     return buf_size;
00879 }
00880 
00881 static av_cold int truemotion1_decode_end(AVCodecContext *avctx)
00882 {
00883     TrueMotion1Context *s = avctx->priv_data;
00884 
00885     if (s->frame.data[0])
00886         avctx->release_buffer(avctx, &s->frame);
00887 
00888     av_free(s->vert_pred);
00889 
00890     return 0;
00891 }
00892 
00893 AVCodec truemotion1_decoder = {
00894     "truemotion1",
00895     AVMEDIA_TYPE_VIDEO,
00896     CODEC_ID_TRUEMOTION1,
00897     sizeof(TrueMotion1Context),
00898     truemotion1_decode_init,
00899     NULL,
00900     truemotion1_decode_end,
00901     truemotion1_decode_frame,
00902     CODEC_CAP_DR1,
00903     .long_name = NULL_IF_CONFIG_SMALL("Duck TrueMotion 1.0"),
00904 };

Generated on Fri Sep 16 2011 17:17:44 for FFmpeg by  doxygen 1.7.1