35 #define TM2_ESCAPE 0x80000000
136 huff->
lens[huff->
num] = length;
145 return FFMAX(ret, ret2);
166 if ((huff.
nodes <= 0) || (huff.
nodes > 0x10000)) {
168 "nodes: %i\n", huff.
nodes);
187 if (res >= 0 && res != huff.
max_bits) {
237 #define TM2_OLD_HEADER_MAGIC 0x00000100
238 #define TM2_NEW_HEADER_MAGIC 0x00000101
271 for (
i = 0;
i < d;
i++) {
273 if (v & (1 << (
mb - 1)))
274 ctx->deltas[stream_id][
i] = v - (1U <<
mb);
276 ctx->deltas[stream_id][
i] = v;
279 ctx->deltas[stream_id][
i] = 0;
299 len = bytestream2_get_be32(&gb);
304 if (
len >= INT_MAX / 4 - 1 || len < 0 || len * 4 + 4 > buf_size) {
310 toks = bytestream2_get_be32(&gb);
312 len = bytestream2_get_be32(&gb);
314 len = bytestream2_get_be32(&gb);
327 len = bytestream2_get_be32(&gb);
344 if ((toks < 0) || (toks > 0xFFFFFF)) {
351 ctx->tok_lens[stream_id] = 0;
354 ctx->tok_lens[stream_id] = toks;
355 len = bytestream2_get_be32(&gb);
363 for (
i = 0;
i < toks;
i++) {
370 if (stream_id <= TM2_MOT && ctx->tokens[stream_id][
i] >=
TM2_DELTAS ||
ctx->tokens[stream_id][
i]<0) {
372 ctx->tokens[stream_id][
i], stream_id,
i);
382 for (
i = 0;
i < toks;
i++) {
384 if (stream_id <= TM2_MOT && ctx->tokens[stream_id][
i] >=
TM2_DELTAS) {
386 ctx->tokens[stream_id][
i], stream_id,
i);
420 #define TM2_INIT_POINTERS() \
423 int Ystride, Ustride, Vstride;\
425 Ystride = ctx->y_stride;\
426 Vstride = ctx->uv_stride;\
427 Ustride = ctx->uv_stride;\
428 Y = (ctx->cur?ctx->Y2:ctx->Y1) + by * 4 * Ystride + bx * 4;\
429 V = (ctx->cur?ctx->V2:ctx->V1) + by * 2 * Vstride + bx * 2;\
430 U = (ctx->cur?ctx->U2:ctx->U1) + by * 2 * Ustride + bx * 2;\
431 last = ctx->last + bx * 4;\
432 clast = ctx->clast + bx * 4;
434 #define TM2_INIT_POINTERS_2() \
435 unsigned *Yo, *Uo, *Vo;\
436 int oYstride, oUstride, oVstride;\
438 TM2_INIT_POINTERS();\
442 Yo = (ctx->cur?ctx->Y1:ctx->Y2) + by * 4 * oYstride + bx * 4;\
443 Vo = (ctx->cur?ctx->V1:ctx->V2) + by * 2 * oVstride + bx * 2;\
444 Uo = (ctx->cur?ctx->U1:ctx->U2) + by * 2 * oUstride + bx * 2;
447 #define TM2_RECALC_BLOCK(CHR, stride, last, CD) {\
448 CD[0] = (unsigned)CHR[ 1] - (unsigned)last[1];\
449 CD[1] = (unsigned)CHR[stride + 1] - (unsigned) CHR[1];\
450 last[0] = (int)CHR[stride + 0];\
451 last[1] = (int)CHR[stride + 1];}
459 for (j = 0; j < 4; j++){
461 for (
i = 0;
i < 4;
i++){
462 d = deltas[
i + j * 4];
475 for (j = 0; j < 2; j++) {
476 for (
i = 0;
i < 2;
i++) {
477 CD[j] += deltas[
i + j * 2];
495 t = (
int)(CD[0] + CD[1]) >> 1;
496 l = (
int)(prev - CD[0] - CD[1] + clast[1]) >> 1;
497 CD[1] = CD[0] + CD[1] - t;
511 for (
i = 0;
i < 4;
i++) {
519 for (
i = 0;
i < 16;
i++)
533 deltas[1] = deltas[2] = deltas[3] = 0;
537 deltas[1] = deltas[2] = deltas[3] = 0;
541 for (
i = 0;
i < 16;
i++)
556 deltas[1] = deltas[2] = deltas[3] = 0;
560 deltas[1] = deltas[2] = deltas[3] = 0;
564 for (
i = 0;
i < 16;
i++)
573 last[0] = (
int)((
unsigned)last[-1] -
ctx->D[0] -
ctx->D[1] -
ctx->D[2] -
ctx->D[3] + last[1]) >> 1;
575 last[0] = (
int)((
unsigned)last[1] -
ctx->D[0] -
ctx->D[1] -
ctx->D[2] -
ctx->D[3])>> 1;
576 last[2] = (
int)((
unsigned)last[1] + last[3]) >> 1;
578 t1 =
ctx->D[0] + (unsigned)
ctx->D[1];
581 t2 =
ctx->D[2] + (unsigned)
ctx->D[3];
592 unsigned left, right;
598 deltas[0] = deltas[1] = deltas[2] = deltas[3] = 0;
601 deltas[0] = deltas[1] = deltas[2] = deltas[3] = 0;
605 for (
i = 0;
i < 16;
i++)
608 ct = (unsigned)
ctx->D[0] +
ctx->D[1] +
ctx->D[2] +
ctx->D[3];
611 left = last[-1] - (unsigned)ct;
617 last[0] = left + (
diff >> 2);
618 last[1] = left + (
diff >> 1);
619 last[2] = right - (
diff >> 2);
624 ctx->D[0] = (tp + (ct >> 2)) - left;
626 ctx->D[1] = (tp + (ct >> 1)) - left;
628 ctx->D[2] = ((tp + ct) - (ct >> 2)) - left;
630 ctx->D[3] = (tp + ct) - left;
641 for (j = 0; j < 2; j++) {
642 for (
i = 0;
i < 2;
i++){
646 U += Ustride;
V += Vstride;
647 Uo += oUstride; Vo += oVstride;
655 ctx->D[0] = Yo[3] - last[3];
656 ctx->D[1] = Yo[3 + oYstride] - Yo[3];
657 ctx->D[2] = Yo[3 + oYstride * 2] - Yo[3 + oYstride];
658 ctx->D[3] = Yo[3 + oYstride * 3] - Yo[3 + oYstride * 2];
660 for (j = 0; j < 4; j++) {
661 for (
i = 0;
i < 4;
i++) {
677 for (j = 0; j < 2; j++) {
678 for (
i = 0;
i < 2;
i++) {
693 ctx->D[0] = Yo[3] - last[3];
694 ctx->D[1] = Yo[3 + oYstride] - Yo[3];
695 ctx->D[2] = Yo[3 + oYstride * 2] - Yo[3 + oYstride];
696 ctx->D[3] = Yo[3 + oYstride * 3] - Yo[3 + oYstride * 2];
698 for (j = 0; j < 4; j++) {
700 for (
i = 0;
i < 4;
i++) {
704 ctx->D[j] = last[3] - d;
718 mx =
av_clip(mx, -(bx * 4 + 4),
ctx->avctx->width - bx * 4);
719 my =
av_clip(my, -(by * 4 + 4),
ctx->avctx->height - by * 4);
721 if (4*bx+mx<0 || 4*by+my<0 || 4*bx+mx+4 >
ctx->avctx->width || 4*by+my+4 >
ctx->avctx->height) {
726 Yo += my * oYstride + mx;
727 Uo += (my >> 1) * oUstride + (mx >> 1);
728 Vo += (my >> 1) * oVstride + (mx >> 1);
731 for (j = 0; j < 2; j++) {
732 for (
i = 0;
i < 2;
i++) {
747 for (j = 0; j < 4; j++) {
748 for (
i = 0;
i < 4;
i++) {
756 ctx->D[0] = (unsigned)
Y[3] - last[3];
757 ctx->D[1] = (unsigned)
Y[3 + Ystride] -
Y[3];
758 ctx->D[2] = (unsigned)
Y[3 + Ystride * 2] -
Y[3 + Ystride];
759 ctx->D[3] = (unsigned)
Y[3 + Ystride * 3] -
Y[3 + Ystride * 2];
760 for (
i = 0;
i < 4;
i++)
761 last[
i] =
Y[
i + Ystride * 3];
767 int w =
ctx->avctx->width,
h =
ctx->avctx->height, bw =
w >> 2, bh =
h >> 2, cw =
w >> 1;
774 ctx->tok_ptrs[
i] = 0;
781 memset(
ctx->last, 0, 4 * bw *
sizeof(
int));
782 memset(
ctx->clast, 0, 4 * bw *
sizeof(
int));
784 for (j = 0; j < bh; j++) {
785 memset(
ctx->D, 0, 4 *
sizeof(
int));
786 memset(
ctx->CD, 0, 4 *
sizeof(
int));
787 for (
i = 0;
i < bw;
i++) {
827 for (j = 0; j <
h; j++) {
828 for (
i = 0;
i <
w;
i++) {
829 unsigned y =
Y[
i],
u =
U[
i >> 1], v =
V[
i >> 1];
836 Y[-4] =
Y[-3] =
Y[-2] =
Y[-1] =
Y[0];
837 Y[
w + 3] =
Y[
w + 2] =
Y[
w + 1] =
Y[
w] =
Y[
w - 1];
841 memcpy(
Y - 4 - 1 *
ctx->y_stride,
Y - 4,
ctx->y_stride);
842 memcpy(
Y - 4 - 2 *
ctx->y_stride,
Y - 4,
ctx->y_stride);
843 memcpy(
Y - 4 - 3 *
ctx->y_stride,
Y - 4,
ctx->y_stride);
844 memcpy(
Y - 4 - 4 *
ctx->y_stride,
Y - 4,
ctx->y_stride);
845 }
else if (j ==
h - 1) {
846 memcpy(
Y - 4 + 1 *
ctx->y_stride,
Y - 4,
ctx->y_stride);
847 memcpy(
Y - 4 + 2 *
ctx->y_stride,
Y - 4,
ctx->y_stride);
848 memcpy(
Y - 4 + 3 *
ctx->y_stride,
Y - 4,
ctx->y_stride);
849 memcpy(
Y - 4 + 4 *
ctx->y_stride,
Y - 4,
ctx->y_stride);
855 U[-2] =
U[-1] =
U[0];
856 V[-2] =
V[-1] =
V[0];
857 U[cw + 1] =
U[cw] =
U[cw - 1];
858 V[cw + 1] =
V[cw] =
V[cw - 1];
862 memcpy(
U - 2 - 1 *
ctx->uv_stride,
U - 2,
ctx->uv_stride);
863 memcpy(
V - 2 - 1 *
ctx->uv_stride,
V - 2,
ctx->uv_stride);
864 memcpy(
U - 2 - 2 *
ctx->uv_stride,
U - 2,
ctx->uv_stride);
865 memcpy(
V - 2 - 2 *
ctx->uv_stride,
V - 2,
ctx->uv_stride);
866 }
else if (j ==
h - 1) {
867 memcpy(
U - 2 + 1 *
ctx->uv_stride,
U - 2,
ctx->uv_stride);
868 memcpy(
V - 2 + 1 *
ctx->uv_stride,
V - 2,
ctx->uv_stride);
869 memcpy(
U - 2 + 2 *
ctx->uv_stride,
U - 2,
ctx->uv_stride);
870 memcpy(
V - 2 + 2 *
ctx->uv_stride,
V - 2,
ctx->uv_stride);
886 #define TM2_HEADER_SIZE 40
889 void *
data,
int *got_frame,
894 int buf_size = avpkt->
size & ~3;
943 return (ret < 0) ? ret : buf_size;
1012 .
name =
"truemotion2",
static double val(void *priv, double ch)
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Libavcodec external API header.
static av_cold int init(AVCodecContext *avctx)
void ff_free_vlc(VLC *vlc)
int ff_init_vlc_from_lengths(VLC *vlc_arg, 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()
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(GetByteContext *g)
#define u(width, name, range_min, range_max)
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.
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
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, VLC_TYPE(*table)[2], 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.
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.
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
Allocate, reallocate, or free an array through a pointer to a pointer.
@ AV_PICTURE_TYPE_I
Intra.
@ AV_PICTURE_TYPE_P
Predicted.
av_cold void ff_bswapdsp_init(BswapDSPContext *c)
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
@ 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.
const char * name
Name of the codec implementation.
This structure describes decoded (raw) audio or video data.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
int key_frame
1 -> keyframe, 0-> not
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
enum AVPictureType pict_type
Picture type of the frame.
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 int tm2_read_deltas(TM2Context *ctx, int stream_id)
static const int tm2_stream_order[TM2_NUM_STREAMS]
#define TM2_INIT_POINTERS_2()
AVCodec ff_truemotion2_decoder
static int tm2_build_huff_table(TM2Context *ctx, TM2Codes *code)
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 int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
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 uint32_t a, const uint32_t b)
static const uint8_t offset[127][2]