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