00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
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;
00115 int block_height;
00116 int block_type;
00117 } comp_types;
00118
00119
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
00157
00158 for (i = 0; i < 8; i++)
00159 {
00160
00161
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
00321
00322 static int truemotion1_decode_header(TrueMotion1Context *s)
00323 {
00324 int i;
00325 struct frame_header header;
00326 uint8_t header_buffer[128];
00327 const uint8_t *sel_vector_table;
00328
00329
00330
00331
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
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
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
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
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
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;
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
00434 s->mb_change_bits = s->buf + header.header_size;
00435 if (s->flags & FLAG_KEYFRAME) {
00436
00437 s->index_stream = s->mb_change_bits;
00438 } else {
00439
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
00471
00472
00473
00474
00475
00476 s->frame.data[0] = NULL;
00477
00478
00479
00480 s->vert_pred =
00481 (unsigned int *)av_malloc(s->avctx->width * sizeof(unsigned int));
00482
00483 return 0;
00484 }
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
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 if(index > 1023){\
00523 av_log(s->avctx, AV_LOG_ERROR, " index %d went out of bounds\n", index); \
00524 return; \
00525 }\
00526 predictor_pair = s->c_predictor_table[index]; \
00527 horiz_pred += (predictor_pair >> 1); \
00528 if (predictor_pair & 1) { \
00529 GET_NEXT_INDEX() \
00530 if (!index) { \
00531 GET_NEXT_INDEX() \
00532 predictor_pair = s->c_predictor_table[index]; \
00533 horiz_pred += ((predictor_pair >> 1) * 5); \
00534 if (predictor_pair & 1) \
00535 GET_NEXT_INDEX() \
00536 else \
00537 index++; \
00538 } \
00539 } else \
00540 index++;
00541
00542 #define APPLY_C_PREDICTOR_24() \
00543 if(index > 1023){\
00544 av_log(s->avctx, AV_LOG_ERROR, " index %d went out of bounds\n", index); \
00545 return; \
00546 }\
00547 predictor_pair = s->c_predictor_table[index]; \
00548 horiz_pred += (predictor_pair >> 1); \
00549 if (predictor_pair & 1) { \
00550 GET_NEXT_INDEX() \
00551 if (!index) { \
00552 GET_NEXT_INDEX() \
00553 predictor_pair = s->fat_c_predictor_table[index]; \
00554 horiz_pred += (predictor_pair >> 1); \
00555 if (predictor_pair & 1) \
00556 GET_NEXT_INDEX() \
00557 else \
00558 index++; \
00559 } \
00560 } else \
00561 index++;
00562
00563
00564 #define APPLY_Y_PREDICTOR() \
00565 if(index > 1023){\
00566 av_log(s->avctx, AV_LOG_ERROR, " index %d went out of bounds\n", index); \
00567 return; \
00568 }\
00569 predictor_pair = s->y_predictor_table[index]; \
00570 horiz_pred += (predictor_pair >> 1); \
00571 if (predictor_pair & 1) { \
00572 GET_NEXT_INDEX() \
00573 if (!index) { \
00574 GET_NEXT_INDEX() \
00575 predictor_pair = s->y_predictor_table[index]; \
00576 horiz_pred += ((predictor_pair >> 1) * 5); \
00577 if (predictor_pair & 1) \
00578 GET_NEXT_INDEX() \
00579 else \
00580 index++; \
00581 } \
00582 } else \
00583 index++;
00584
00585 #define APPLY_Y_PREDICTOR_24() \
00586 if(index > 1023){\
00587 av_log(s->avctx, AV_LOG_ERROR, " index %d went out of bounds\n", index); \
00588 return; \
00589 }\
00590 predictor_pair = s->y_predictor_table[index]; \
00591 horiz_pred += (predictor_pair >> 1); \
00592 if (predictor_pair & 1) { \
00593 GET_NEXT_INDEX() \
00594 if (!index) { \
00595 GET_NEXT_INDEX() \
00596 predictor_pair = s->fat_y_predictor_table[index]; \
00597 horiz_pred += (predictor_pair >> 1); \
00598 if (predictor_pair & 1) \
00599 GET_NEXT_INDEX() \
00600 else \
00601 index++; \
00602 } \
00603 } else \
00604 index++;
00605
00606 #define OUTPUT_PIXEL_PAIR() \
00607 *current_pixel_pair = *vert_pred + horiz_pred; \
00608 *vert_pred++ = *current_pixel_pair++;
00609
00610 static void truemotion1_decode_16bit(TrueMotion1Context *s)
00611 {
00612 int y;
00613 int pixels_left;
00614 unsigned int predictor_pair;
00615 unsigned int horiz_pred;
00616 unsigned int *vert_pred;
00617 unsigned int *current_pixel_pair;
00618 unsigned char *current_line = s->frame.data[0];
00619 int keyframe = s->flags & FLAG_KEYFRAME;
00620
00621
00622 const unsigned char *mb_change_bits = s->mb_change_bits;
00623 unsigned char mb_change_byte;
00624 unsigned char mb_change_byte_mask;
00625 int mb_change_index;
00626
00627
00628 int index_stream_index = 0;
00629 int index;
00630
00631
00632 memset(s->vert_pred, 0, s->avctx->width * sizeof(unsigned int));
00633
00634 GET_NEXT_INDEX();
00635
00636 for (y = 0; y < s->avctx->height; y++) {
00637
00638
00639 horiz_pred = 0;
00640 current_pixel_pair = (unsigned int *)current_line;
00641 vert_pred = s->vert_pred;
00642 mb_change_index = 0;
00643 mb_change_byte = mb_change_bits[mb_change_index++];
00644 mb_change_byte_mask = 0x01;
00645 pixels_left = s->avctx->width;
00646
00647 while (pixels_left > 0) {
00648
00649 if (keyframe || ((mb_change_byte & mb_change_byte_mask) == 0)) {
00650
00651 switch (y & 3) {
00652 case 0:
00653
00654
00655 if (s->block_width == 2) {
00656 APPLY_C_PREDICTOR();
00657 APPLY_Y_PREDICTOR();
00658 OUTPUT_PIXEL_PAIR();
00659 APPLY_C_PREDICTOR();
00660 APPLY_Y_PREDICTOR();
00661 OUTPUT_PIXEL_PAIR();
00662 } else {
00663 APPLY_C_PREDICTOR();
00664 APPLY_Y_PREDICTOR();
00665 OUTPUT_PIXEL_PAIR();
00666 APPLY_Y_PREDICTOR();
00667 OUTPUT_PIXEL_PAIR();
00668 }
00669 break;
00670
00671 case 1:
00672 case 3:
00673
00674 APPLY_Y_PREDICTOR();
00675 OUTPUT_PIXEL_PAIR();
00676 APPLY_Y_PREDICTOR();
00677 OUTPUT_PIXEL_PAIR();
00678 break;
00679
00680 case 2:
00681
00682
00683 if (s->block_type == BLOCK_2x2) {
00684 APPLY_C_PREDICTOR();
00685 APPLY_Y_PREDICTOR();
00686 OUTPUT_PIXEL_PAIR();
00687 APPLY_C_PREDICTOR();
00688 APPLY_Y_PREDICTOR();
00689 OUTPUT_PIXEL_PAIR();
00690 } else if (s->block_type == BLOCK_4x2) {
00691 APPLY_C_PREDICTOR();
00692 APPLY_Y_PREDICTOR();
00693 OUTPUT_PIXEL_PAIR();
00694 APPLY_Y_PREDICTOR();
00695 OUTPUT_PIXEL_PAIR();
00696 } else {
00697 APPLY_Y_PREDICTOR();
00698 OUTPUT_PIXEL_PAIR();
00699 APPLY_Y_PREDICTOR();
00700 OUTPUT_PIXEL_PAIR();
00701 }
00702 break;
00703 }
00704
00705 } else {
00706
00707
00708
00709 *vert_pred++ = *current_pixel_pair++;
00710 horiz_pred = *current_pixel_pair - *vert_pred;
00711 *vert_pred++ = *current_pixel_pair++;
00712
00713 }
00714
00715 if (!keyframe) {
00716 mb_change_byte_mask <<= 1;
00717
00718
00719 if (!mb_change_byte_mask) {
00720 mb_change_byte = mb_change_bits[mb_change_index++];
00721 mb_change_byte_mask = 0x01;
00722 }
00723 }
00724
00725 pixels_left -= 4;
00726 }
00727
00728
00729 if (((y + 1) & 3) == 0)
00730 mb_change_bits += s->mb_change_bits_row_size;
00731
00732 current_line += s->frame.linesize[0];
00733 }
00734 }
00735
00736 static void truemotion1_decode_24bit(TrueMotion1Context *s)
00737 {
00738 int y;
00739 int pixels_left;
00740 unsigned int predictor_pair;
00741 unsigned int horiz_pred;
00742 unsigned int *vert_pred;
00743 unsigned int *current_pixel_pair;
00744 unsigned char *current_line = s->frame.data[0];
00745 int keyframe = s->flags & FLAG_KEYFRAME;
00746
00747
00748 const unsigned char *mb_change_bits = s->mb_change_bits;
00749 unsigned char mb_change_byte;
00750 unsigned char mb_change_byte_mask;
00751 int mb_change_index;
00752
00753
00754 int index_stream_index = 0;
00755 int index;
00756
00757
00758 memset(s->vert_pred, 0, s->avctx->width * sizeof(unsigned int));
00759
00760 GET_NEXT_INDEX();
00761
00762 for (y = 0; y < s->avctx->height; y++) {
00763
00764
00765 horiz_pred = 0;
00766 current_pixel_pair = (unsigned int *)current_line;
00767 vert_pred = s->vert_pred;
00768 mb_change_index = 0;
00769 mb_change_byte = mb_change_bits[mb_change_index++];
00770 mb_change_byte_mask = 0x01;
00771 pixels_left = s->avctx->width;
00772
00773 while (pixels_left > 0) {
00774
00775 if (keyframe || ((mb_change_byte & mb_change_byte_mask) == 0)) {
00776
00777 switch (y & 3) {
00778 case 0:
00779
00780
00781 if (s->block_width == 2) {
00782 APPLY_C_PREDICTOR_24();
00783 APPLY_Y_PREDICTOR_24();
00784 OUTPUT_PIXEL_PAIR();
00785 APPLY_C_PREDICTOR_24();
00786 APPLY_Y_PREDICTOR_24();
00787 OUTPUT_PIXEL_PAIR();
00788 } else {
00789 APPLY_C_PREDICTOR_24();
00790 APPLY_Y_PREDICTOR_24();
00791 OUTPUT_PIXEL_PAIR();
00792 APPLY_Y_PREDICTOR_24();
00793 OUTPUT_PIXEL_PAIR();
00794 }
00795 break;
00796
00797 case 1:
00798 case 3:
00799
00800 APPLY_Y_PREDICTOR_24();
00801 OUTPUT_PIXEL_PAIR();
00802 APPLY_Y_PREDICTOR_24();
00803 OUTPUT_PIXEL_PAIR();
00804 break;
00805
00806 case 2:
00807
00808
00809 if (s->block_type == BLOCK_2x2) {
00810 APPLY_C_PREDICTOR_24();
00811 APPLY_Y_PREDICTOR_24();
00812 OUTPUT_PIXEL_PAIR();
00813 APPLY_C_PREDICTOR_24();
00814 APPLY_Y_PREDICTOR_24();
00815 OUTPUT_PIXEL_PAIR();
00816 } else if (s->block_type == BLOCK_4x2) {
00817 APPLY_C_PREDICTOR_24();
00818 APPLY_Y_PREDICTOR_24();
00819 OUTPUT_PIXEL_PAIR();
00820 APPLY_Y_PREDICTOR_24();
00821 OUTPUT_PIXEL_PAIR();
00822 } else {
00823 APPLY_Y_PREDICTOR_24();
00824 OUTPUT_PIXEL_PAIR();
00825 APPLY_Y_PREDICTOR_24();
00826 OUTPUT_PIXEL_PAIR();
00827 }
00828 break;
00829 }
00830
00831 } else {
00832
00833
00834
00835 *vert_pred++ = *current_pixel_pair++;
00836 horiz_pred = *current_pixel_pair - *vert_pred;
00837 *vert_pred++ = *current_pixel_pair++;
00838
00839 }
00840
00841 if (!keyframe) {
00842 mb_change_byte_mask <<= 1;
00843
00844
00845 if (!mb_change_byte_mask) {
00846 mb_change_byte = mb_change_bits[mb_change_index++];
00847 mb_change_byte_mask = 0x01;
00848 }
00849 }
00850
00851 pixels_left -= 4;
00852 }
00853
00854
00855 if (((y + 1) & 3) == 0)
00856 mb_change_bits += s->mb_change_bits_row_size;
00857
00858 current_line += s->frame.linesize[0];
00859 }
00860 }
00861
00862
00863 static int truemotion1_decode_frame(AVCodecContext *avctx,
00864 void *data, int *data_size,
00865 const uint8_t *buf, int buf_size)
00866 {
00867 TrueMotion1Context *s = avctx->priv_data;
00868
00869 s->buf = buf;
00870 s->size = buf_size;
00871
00872 if (truemotion1_decode_header(s) == -1)
00873 return -1;
00874
00875 s->frame.reference = 1;
00876 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID |
00877 FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
00878 if (avctx->reget_buffer(avctx, &s->frame) < 0) {
00879 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00880 return -1;
00881 }
00882
00883 if (compression_types[s->compression].algorithm == ALGO_RGB24H) {
00884 truemotion1_decode_24bit(s);
00885 } else if (compression_types[s->compression].algorithm != ALGO_NOP) {
00886 truemotion1_decode_16bit(s);
00887 }
00888
00889 *data_size = sizeof(AVFrame);
00890 *(AVFrame*)data = s->frame;
00891
00892
00893 return buf_size;
00894 }
00895
00896 static av_cold int truemotion1_decode_end(AVCodecContext *avctx)
00897 {
00898 TrueMotion1Context *s = avctx->priv_data;
00899
00900 if (s->frame.data[0])
00901 avctx->release_buffer(avctx, &s->frame);
00902
00903 av_free(s->vert_pred);
00904
00905 return 0;
00906 }
00907
00908 AVCodec truemotion1_decoder = {
00909 "truemotion1",
00910 CODEC_TYPE_VIDEO,
00911 CODEC_ID_TRUEMOTION1,
00912 sizeof(TrueMotion1Context),
00913 truemotion1_decode_init,
00914 NULL,
00915 truemotion1_decode_end,
00916 truemotion1_decode_frame,
00917 CODEC_CAP_DR1,
00918 .long_name = NULL_IF_CONFIG_SMALL("Duck TrueMotion 1.0"),
00919 };