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

Generated on Sat Feb 16 2013 09:23:14 for ffmpeg by  doxygen 1.7.1