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