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

Generated on Fri Feb 22 2013 07:24:29 for FFmpeg by  doxygen 1.7.1