37#define TM2_ESCAPE 0x80000000
138 huff->
lens[huff->
num] = length;
147 return FFMAX(ret, ret2);
168 if ((huff.
nodes <= 0) || (huff.
nodes > 0x10000)) {
170 "nodes: %i\n", huff.
nodes);
189 if (res >= 0 && res != huff.
max_bits) {
238#define TM2_OLD_HEADER_MAGIC 0x00000100
239#define TM2_NEW_HEADER_MAGIC 0x00000101
272 for (
i = 0;
i < d;
i++) {
274 if (v & (1 << (
mb - 1)))
275 ctx->deltas[stream_id][
i] = v - (1U <<
mb);
277 ctx->deltas[stream_id][
i] = v;
280 ctx->deltas[stream_id][
i] = 0;
300 len = bytestream2_get_be32(&gb);
311 toks = bytestream2_get_be32(&gb);
313 len = bytestream2_get_be32(&gb);
315 len = bytestream2_get_be32(&gb);
328 len = bytestream2_get_be32(&gb);
345 if ((toks < 0) || (toks > 0xFFFFFF)) {
352 ctx->tok_lens[stream_id] = 0;
355 ctx->tok_lens[stream_id] = toks;
356 len = bytestream2_get_be32(&gb);
364 for (
i = 0;
i < toks;
i++) {
371 if (stream_id <= TM2_MOT && ctx->tokens[stream_id][
i] >=
TM2_DELTAS ||
ctx->tokens[stream_id][
i]<0) {
373 ctx->tokens[stream_id][
i], stream_id,
i);
383 for (
i = 0;
i < toks;
i++) {
385 if (stream_id <= TM2_MOT && ctx->tokens[stream_id][
i] >=
TM2_DELTAS) {
387 ctx->tokens[stream_id][
i], stream_id,
i);
421#define TM2_INIT_POINTERS() \
424 int Ystride, Ustride, Vstride;\
426 Ystride = ctx->y_stride;\
427 Vstride = ctx->uv_stride;\
428 Ustride = ctx->uv_stride;\
429 Y = (ctx->cur?ctx->Y2:ctx->Y1) + by * 4 * Ystride + bx * 4;\
430 V = (ctx->cur?ctx->V2:ctx->V1) + by * 2 * Vstride + bx * 2;\
431 U = (ctx->cur?ctx->U2:ctx->U1) + by * 2 * Ustride + bx * 2;\
432 last = ctx->last + bx * 4;\
433 clast = ctx->clast + bx * 4;
435#define TM2_INIT_POINTERS_2() \
436 unsigned *Yo, *Uo, *Vo;\
437 int oYstride, oUstride, oVstride;\
439 TM2_INIT_POINTERS();\
443 Yo = (ctx->cur?ctx->Y1:ctx->Y2) + by * 4 * oYstride + bx * 4;\
444 Vo = (ctx->cur?ctx->V1:ctx->V2) + by * 2 * oVstride + bx * 2;\
445 Uo = (ctx->cur?ctx->U1:ctx->U2) + by * 2 * oUstride + bx * 2;
448#define TM2_RECALC_BLOCK(CHR, stride, last, CD) {\
449 CD[0] = (unsigned)CHR[ 1] - (unsigned)last[1];\
450 CD[1] = (unsigned)CHR[stride + 1] - (unsigned) CHR[1];\
451 last[0] = (int)CHR[stride + 0];\
452 last[1] = (int)CHR[stride + 1];}
460 for (j = 0; j < 4; j++){
462 for (
i = 0;
i < 4;
i++){
463 d = deltas[
i + j * 4];
476 for (j = 0; j < 2; j++) {
477 for (
i = 0;
i < 2;
i++) {
478 CD[j] += deltas[
i + j * 2];
496 t = (int)(CD[0] + CD[1]) >> 1;
497 l = (int)(prev - CD[0] - CD[1] + clast[1]) >> 1;
498 CD[1] = CD[0] + CD[1] - t;
512 for (
i = 0;
i < 4;
i++) {
520 for (
i = 0;
i < 16;
i++)
534 deltas[1] = deltas[2] = deltas[3] = 0;
538 deltas[1] = deltas[2] = deltas[3] = 0;
542 for (
i = 0;
i < 16;
i++)
557 deltas[1] = deltas[2] = deltas[3] = 0;
561 deltas[1] = deltas[2] = deltas[3] = 0;
565 for (
i = 0;
i < 16;
i++)
574 last[0] = (int)((
unsigned)last[-1] -
ctx->D[0] -
ctx->D[1] -
ctx->D[2] -
ctx->D[3] + last[1]) >> 1;
576 last[0] = (int)((
unsigned)last[1] -
ctx->D[0] -
ctx->D[1] -
ctx->D[2] -
ctx->D[3])>> 1;
577 last[2] = (int)((
unsigned)last[1] + last[3]) >> 1;
579 t1 =
ctx->D[0] + (unsigned)
ctx->D[1];
581 ctx->D[1] = t1 - (t1 >> 1);
582 t2 =
ctx->D[2] + (unsigned)
ctx->D[3];
584 ctx->D[3] = t2 - (t2 >> 1);
593 unsigned left, right;
599 deltas[0] = deltas[1] = deltas[2] = deltas[3] = 0;
602 deltas[0] = deltas[1] = deltas[2] = deltas[3] = 0;
606 for (
i = 0;
i < 16;
i++)
609 ct = (unsigned)
ctx->D[0] +
ctx->D[1] +
ctx->D[2] +
ctx->D[3];
612 left = last[-1] - (unsigned)ct;
620 last[2] = right - (
diff >> 2);
625 ctx->D[0] = (tp + (ct >> 2)) -
left;
627 ctx->D[1] = (tp + (ct >> 1)) -
left;
629 ctx->D[2] = ((tp + ct) - (ct >> 2)) -
left;
642 for (j = 0; j < 2; j++) {
643 for (
i = 0;
i < 2;
i++){
647 U += Ustride;
V += Vstride;
648 Uo += oUstride; Vo += oVstride;
656 ctx->D[0] = Yo[3] - last[3];
657 ctx->D[1] = Yo[3 + oYstride] - Yo[3];
658 ctx->D[2] = Yo[3 + oYstride * 2] - Yo[3 + oYstride];
659 ctx->D[3] = Yo[3 + oYstride * 3] - Yo[3 + oYstride * 2];
661 for (j = 0; j < 4; j++) {
662 for (
i = 0;
i < 4;
i++) {
678 for (j = 0; j < 2; j++) {
679 for (
i = 0;
i < 2;
i++) {
694 ctx->D[0] = Yo[3] - last[3];
695 ctx->D[1] = Yo[3 + oYstride] - Yo[3];
696 ctx->D[2] = Yo[3 + oYstride * 2] - Yo[3 + oYstride];
697 ctx->D[3] = Yo[3 + oYstride * 3] - Yo[3 + oYstride * 2];
699 for (j = 0; j < 4; j++) {
701 for (
i = 0;
i < 4;
i++) {
705 ctx->D[j] = last[3] - d;
727 Yo +=
my * oYstride +
mx;
728 Uo += (
my >> 1) * oUstride + (
mx >> 1);
729 Vo += (
my >> 1) * oVstride + (
mx >> 1);
732 for (j = 0; j < 2; j++) {
733 for (
i = 0;
i < 2;
i++) {
748 for (j = 0; j < 4; j++) {
749 for (
i = 0;
i < 4;
i++) {
757 ctx->D[0] = (unsigned)
Y[3] - last[3];
758 ctx->D[1] = (unsigned)
Y[3 + Ystride] -
Y[3];
759 ctx->D[2] = (unsigned)
Y[3 + Ystride * 2] -
Y[3 + Ystride];
760 ctx->D[3] = (unsigned)
Y[3 + Ystride * 3] -
Y[3 + Ystride * 2];
761 for (
i = 0;
i < 4;
i++)
762 last[
i] =
Y[
i + Ystride * 3];
768 int w =
ctx->avctx->width,
h =
ctx->avctx->height, bw =
w >> 2, bh =
h >> 2, cw =
w >> 1;
775 ctx->tok_ptrs[
i] = 0;
782 memset(
ctx->last, 0, 4 * bw *
sizeof(
int));
783 memset(
ctx->clast, 0, 4 * bw *
sizeof(
int));
785 for (j = 0; j < bh; j++) {
786 memset(
ctx->D, 0, 4 *
sizeof(
int));
787 memset(
ctx->CD, 0, 4 *
sizeof(
int));
788 for (
i = 0;
i < bw;
i++) {
828 for (j = 0; j <
h; j++) {
829 for (
i = 0;
i <
w;
i++) {
830 unsigned y =
Y[
i],
u =
U[
i >> 1], v =
V[
i >> 1];
837 Y[-4] =
Y[-3] =
Y[-2] =
Y[-1] =
Y[0];
838 Y[
w + 3] =
Y[
w + 2] =
Y[
w + 1] =
Y[
w] =
Y[
w - 1];
842 memcpy(
Y - 4 - 1 *
ctx->y_stride,
Y - 4,
ctx->y_stride);
843 memcpy(
Y - 4 - 2 *
ctx->y_stride,
Y - 4,
ctx->y_stride);
844 memcpy(
Y - 4 - 3 *
ctx->y_stride,
Y - 4,
ctx->y_stride);
845 memcpy(
Y - 4 - 4 *
ctx->y_stride,
Y - 4,
ctx->y_stride);
846 }
else if (j ==
h - 1) {
847 memcpy(
Y - 4 + 1 *
ctx->y_stride,
Y - 4,
ctx->y_stride);
848 memcpy(
Y - 4 + 2 *
ctx->y_stride,
Y - 4,
ctx->y_stride);
849 memcpy(
Y - 4 + 3 *
ctx->y_stride,
Y - 4,
ctx->y_stride);
850 memcpy(
Y - 4 + 4 *
ctx->y_stride,
Y - 4,
ctx->y_stride);
856 U[-2] =
U[-1] =
U[0];
857 V[-2] =
V[-1] =
V[0];
858 U[cw + 1] =
U[cw] =
U[cw - 1];
859 V[cw + 1] =
V[cw] =
V[cw - 1];
863 memcpy(
U - 2 - 1 *
ctx->uv_stride,
U - 2,
ctx->uv_stride);
864 memcpy(
V - 2 - 1 *
ctx->uv_stride,
V - 2,
ctx->uv_stride);
865 memcpy(
U - 2 - 2 *
ctx->uv_stride,
U - 2,
ctx->uv_stride);
866 memcpy(
V - 2 - 2 *
ctx->uv_stride,
V - 2,
ctx->uv_stride);
867 }
else if (j ==
h - 1) {
868 memcpy(
U - 2 + 1 *
ctx->uv_stride,
U - 2,
ctx->uv_stride);
869 memcpy(
V - 2 + 1 *
ctx->uv_stride,
V - 2,
ctx->uv_stride);
870 memcpy(
U - 2 + 2 *
ctx->uv_stride,
U - 2,
ctx->uv_stride);
871 memcpy(
V - 2 + 2 *
ctx->uv_stride,
V - 2,
ctx->uv_stride);
877 dst += p->linesize[0];
887#define TM2_HEADER_SIZE 40
893 const uint8_t *buf = avpkt->
data;
894 int buf_size = avpkt->
size & ~3;
945 return (ret < 0) ? ret : buf_size;
1014 .p.name =
"truemotion2",
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t my
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t mx
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
static double val(void *priv, double ch)
const FFCodec ff_truemotion2_decoder
static av_cold void close(AVCodecParserContext *s)
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Libavcodec external API header.
static void BS_FUNC skip(BSCTX *bc, unsigned int n)
Skip n bits in the buffer.
static int BS_FUNC left(const BSCTX *bc)
Return the number of the bits left in a buffer.
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
static av_always_inline int bytestream2_tell(const GetByteContext *g)
#define i(width, name, range_min, range_max)
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Identical in function to ff_get_buffer(), except it reuses the existing buffer if available.
int(* init)(AVBSFContext *ctx)
bitstream reader API header.
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
static av_always_inline int get_vlc2(GetBitContext *s, const VLCElem *table, int bits, int max_depth)
Parse a vlc code.
static int get_bits_left(GetBitContext *gb)
static unsigned int get_bits1(GetBitContext *s)
static int get_bits_count(const GetBitContext *s)
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
@ AV_CODEC_ID_TRUEMOTION2
void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
Same behaviour av_fast_malloc but the buffer has additional AV_INPUT_BUFFER_PADDING_SIZE at the end w...
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
Allocate, reallocate an array through a pointer to a pointer.
@ AV_PICTURE_TYPE_I
Intra.
@ AV_PICTURE_TYPE_P
Predicted.
static av_cold int decode_init(AVCodecContext *avctx)
static av_cold int decode_end(AVCodecContext *avctx)
static int decode_frame(AVCodecContext *avctx, AVFrame *picture, int *got_frame, AVPacket *avpkt)
av_cold void ff_bswapdsp_init(BswapDSPContext *c)
#define u(width, name, range_min, range_max)
void * av_calloc(size_t nmemb, size_t size)
Memory handling functions.
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
main external API structure.
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
int width
picture width / height.
This structure describes decoded (raw) audio or video data.
This structure stores compressed data.
void(* bswap_buf)(uint32_t *dst, const uint32_t *src, int w)
Huffman codes for each of streams.
VLC vlc
table for FFmpeg bitstream reader
int * recode
table for converting from code indexes to values
int * tokens[TM2_NUM_STREAMS]
int deltas[TM2_NUM_STREAMS][TM2_DELTAS]
int tok_ptrs[TM2_NUM_STREAMS]
int tok_lens[TM2_NUM_STREAMS]
structure for gathering Huffman codes information
int nodes
total number of nodes in tree
int val_bits
length of literal
uint8_t * lens
codelengths
int min_bits
minimum length of code
int num
current number filled
int max_bits
maximum length of code
int max_num
total number of codes
#define av_malloc_array(a, b)
#define avpriv_request_sample(...)
static AVFormatContext * ctx
static int tm2_read_deltas(TM2Context *ctx, int stream_id)
static const int tm2_stream_order[TM2_NUM_STREAMS]
#define TM2_INIT_POINTERS_2()
static int tm2_build_huff_table(TM2Context *ctx, TM2Codes *code)
static int decode_frame(AVCodecContext *avctx, AVFrame *rframe, int *got_frame, AVPacket *avpkt)
static void tm2_med_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
#define TM2_INIT_POINTERS()
static int tm2_read_tree(TM2Context *ctx, int length, TM2Huff *huff)
#define TM2_OLD_HEADER_MAGIC
static int tm2_read_header(TM2Context *ctx, const uint8_t *buf)
static int GET_TOK(TM2Context *ctx, int type)
static void tm2_low_chroma(int *data, int stride, int *clast, unsigned *CD, int *deltas, int bx)
static void tm2_still_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
#define TM2_NEW_HEADER_MAGIC
static void tm2_free_codes(TM2Codes *code)
static av_cold int decode_init(AVCodecContext *avctx)
static void tm2_high_chroma(int *data, int stride, int *last, unsigned *CD, int *deltas)
static av_cold int decode_end(AVCodecContext *avctx)
static void tm2_motion_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
static int tm2_read_stream(TM2Context *ctx, const uint8_t *buf, int stream_id, int buf_size)
static void tm2_hi_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
static void tm2_update_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
static int tm2_decode_blocks(TM2Context *ctx, AVFrame *p)
static int tm2_get_token(GetBitContext *gb, TM2Codes *code)
static void tm2_null_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
#define TM2_RECALC_BLOCK(CHR, stride, last, CD)
static void tm2_apply_deltas(TM2Context *ctx, int *Y, int stride, int *deltas, int *last)
static void tm2_low_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
int ff_vlc_init_from_lengths(VLC *vlc, int nb_bits, int nb_codes, const int8_t *lens, int lens_wrap, const void *symbols, int symbols_wrap, int symbols_size, int offset, int flags, void *logctx)
Build VLC decoding tables suitable for use with get_vlc2()
void ff_vlc_free(VLC *vlc)