📄 truemotion1.c
字号:
/* * Duck TrueMotion 1.0 Decoder * Copyright (C) 2003 Alex Beregszaszi & Mike Melanson * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *//** * @file truemotion1.c * Duck TrueMotion v1 Video Decoder by * Alex Beregszaszi (alex@fsn.hu) and * Mike Melanson (melanson@pcisys.net) * * The TrueMotion v1 decoder presently only decodes 16-bit TM1 data and * outputs RGB555 data. 24-bit TM1 data is not supported yet. */#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include "common.h"#include "avcodec.h"#include "dsputil.h"#define printf(...) {} //(f)printf() usage is forbidden in libavcodec, use av_log#define fprintf(...) {} #include "truemotion1data.h"#define LE_16(x) ((((uint8_t*)(x))[1] << 8) | ((uint8_t*)(x))[0])typedef struct TrueMotion1Context { AVCodecContext *avctx; AVFrame frame; AVFrame prev_frame; unsigned char *buf; int size; unsigned char *mb_change_bits; int mb_change_bits_row_size; unsigned char *index_stream; int index_stream_size; int flags; int x, y, w, h; uint32_t y_predictor_table[1024]; uint32_t c_predictor_table[1024]; int compression; int block_type; int block_width; int block_height; int16_t *ydt; int16_t *cdt; int16_t *fat_ydt; int16_t *fat_cdt; int last_deltaset, last_vectable; unsigned int *vert_pred;} TrueMotion1Context;#define FLAG_SPRITE 32#define FLAG_KEYFRAME 16#define FLAG_INTERFRAME 8#define FLAG_INTERPOLATED 4struct frame_header { uint8_t header_size; uint8_t compression; uint8_t deltaset; uint8_t vectable; uint16_t ysize; uint16_t xsize; uint16_t checksum; uint8_t version; uint8_t header_type; uint8_t flags; uint8_t control; uint16_t xoffset; uint16_t yoffset; uint16_t width; uint16_t height;};#define ALGO_NOP 0#define ALGO_RGB16V 1#define ALGO_RGB16H 2#define ALGO_RGB24H 3/* these are the various block sizes that can occupy a 4x4 block */#define BLOCK_2x2 0#define BLOCK_2x4 1#define BLOCK_4x2 2#define BLOCK_4x4 3typedef struct comp_types { int algorithm; int block_width; int block_height; int block_type;} comp_types;/* { valid for metatype }, algorithm, num of deltas, horiz res, vert res */static comp_types compression_types[17] = { { ALGO_NOP, 0, 0, 0 }, { ALGO_RGB16V, 4, 4, BLOCK_4x4 }, { ALGO_RGB16H, 4, 4, BLOCK_4x4 }, { ALGO_RGB16V, 4, 2, BLOCK_4x2 }, { ALGO_RGB16H, 4, 2, BLOCK_4x2 }, { ALGO_RGB16V, 2, 4, BLOCK_2x4 }, { ALGO_RGB16H, 2, 4, BLOCK_2x4 }, { ALGO_RGB16V, 2, 2, BLOCK_2x2 }, { ALGO_RGB16H, 2, 2, BLOCK_2x2 }, { ALGO_NOP, 4, 4, BLOCK_4x4 }, { ALGO_RGB24H, 4, 4, BLOCK_4x4 }, { ALGO_NOP, 4, 2, BLOCK_4x2 }, { ALGO_RGB24H, 4, 2, BLOCK_4x2 }, { ALGO_NOP, 2, 4, BLOCK_2x4 }, { ALGO_RGB24H, 2, 4, BLOCK_2x4 }, { ALGO_NOP, 2, 2, BLOCK_2x2 }, { ALGO_RGB24H, 2, 2, BLOCK_2x2 }};static void select_delta_tables(TrueMotion1Context *s, int delta_table_index){ int i; if (delta_table_index > 3) return; s->ydt = ydts[delta_table_index]; s->cdt = cdts[delta_table_index]; s->fat_ydt = fat_ydts[delta_table_index]; s->fat_cdt = fat_cdts[delta_table_index]; /* Y skinny deltas need to be halved for some reason; maybe the * skinny Y deltas should be modified */ for (i = 0; i < 8; i++) { /* drop the lsb before dividing by 2-- net effect: round down * when dividing a negative number (e.g., -3/2 = -2, not -1) */ s->ydt[i] &= 0xFFFE; s->ydt[i] /= 2; }}#ifdef WORDS_BIGENDIANstatic int make_ydt_entry(int p2, int p1, int16_t *ydt)#elsestatic int make_ydt_entry(int p1, int p2, int16_t *ydt)#endif{ int lo, hi; lo = ydt[p1]; lo += (lo << 5) + (lo << 10); hi = ydt[p2]; hi += (hi << 5) + (hi << 10); return ((lo + (hi << 16)) << 1);}#ifdef WORDS_BIGENDIANstatic int make_cdt_entry(int p2, int p1, int16_t *cdt)#elsestatic int make_cdt_entry(int p1, int p2, int16_t *cdt)#endif{ int r, b, lo; b = cdt[p2]; r = cdt[p1] << 10; lo = b + r; return ((lo + (lo << 16)) << 1);}static void gen_vector_table(TrueMotion1Context *s, uint8_t *sel_vector_table){ int len, i, j; unsigned char delta_pair; for (i = 0; i < 1024; i += 4) { len = *sel_vector_table++ / 2; for (j = 0; j < len; j++) { delta_pair = *sel_vector_table++; s->y_predictor_table[i+j] = 0xfffffffe & make_ydt_entry(delta_pair >> 4, delta_pair & 0xf, s->ydt); s->c_predictor_table[i+j] = 0xfffffffe & make_cdt_entry(delta_pair >> 4, delta_pair & 0xf, s->cdt); } s->y_predictor_table[i+(j-1)] |= 1; s->c_predictor_table[i+(j-1)] |= 1; }}/* Returns the number of bytes consumed from the bytestream. Returns -1 if * there was an error while decoding the header */ static int truemotion1_decode_header(TrueMotion1Context *s){ int i; struct frame_header header; uint8_t header_buffer[128]; /* logical maximum size of the header */ uint8_t *sel_vector_table; /* There is 1 change bit per 4 pixels, so each change byte represents * 32 pixels; divide width by 4 to obtain the number of change bits and * then round up to the nearest byte. */ s->mb_change_bits_row_size = ((s->avctx->width >> 2) + 7) >> 3; header.header_size = ((s->buf[0] >> 5) | (s->buf[0] << 3)) & 0x7f; if (s->buf[0] < 0x10) { printf("invalid header size\n"); return -1; } /* unscramble the header bytes with a XOR operation */ memset(header_buffer, 0, 128); for (i = 1; i < header.header_size; i++) header_buffer[i - 1] = s->buf[i] ^ s->buf[i + 1]; header.compression = header_buffer[0]; header.deltaset = header_buffer[1]; header.vectable = header_buffer[2]; header.ysize = LE_16(&header_buffer[3]); header.xsize = LE_16(&header_buffer[5]); header.checksum = LE_16(&header_buffer[7]); header.version = header_buffer[9]; header.header_type = header_buffer[10]; header.flags = header_buffer[11]; header.control = header_buffer[12]; /* Version 2 */ if (header.version >= 2) { if (header.header_type > 3) { av_log(s->avctx, AV_LOG_ERROR, "truemotion1: invalid header type\n"); return -1; } else if ((header.header_type == 2) || (header.header_type == 3)) { s->flags = header.flags; if (!(s->flags & FLAG_INTERFRAME)) s->flags |= FLAG_KEYFRAME; } else s->flags = FLAG_KEYFRAME; } else /* Version 1 */ s->flags = FLAG_KEYFRAME; if (s->flags & FLAG_SPRITE) { s->w = header.width; s->h = header.height; s->x = header.xoffset; s->y = header.yoffset; } else { s->w = header.xsize; s->h = header.ysize; if (header.header_type < 2) { if ((s->w < 213) && (s->h >= 176)) s->flags |= FLAG_INTERPOLATED; } } if (header.compression > 17) { printf("invalid compression type (%d)\n", header.compression); return -1; } if ((header.deltaset != s->last_deltaset) || (header.vectable != s->last_vectable)) select_delta_tables(s, header.deltaset); if ((header.compression & 1) && header.header_type) sel_vector_table = pc_tbl2; else { if (header.vectable < 4) sel_vector_table = tables[header.vectable - 1]; else { printf("invalid vector table id (%d)\n", header.vectable); return -1; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -