FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
truemotion2rt.c
Go to the documentation of this file.
1 /*
2  * Duck TrueMotion 2.0 Real Time Decoder
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 
25 #include "avcodec.h"
26 #define BITSTREAM_READER_LE
27 #include "get_bits.h"
28 #include "internal.h"
29 #include "libavutil/imgutils.h"
30 #include "libavutil/internal.h"
31 #include "libavutil/intreadwrite.h"
32 #include "libavutil/mem.h"
33 
34 typedef struct TrueMotion2RTContext {
36  const uint8_t *buf;
37  int size;
39  int hscale;
41 
43 {
44  avctx->pix_fmt = AV_PIX_FMT_YUV410P;
45  return 0;
46 }
47 
48 /* Returns the number of bytes consumed from the bytestream. Returns -1 if
49  * there was an error while decoding the header */
51 {
53  uint8_t header_buffer[128] = { 0 }; /* logical maximum size of the header */
54  int i, header_size;
55 
56  header_size = ((s->buf[0] >> 5) | (s->buf[0] << 3)) & 0x7f;
57  if (header_size < 10) {
58  av_log(avctx, AV_LOG_ERROR, "invalid header size (%d)\n", header_size);
59  return AVERROR_INVALIDDATA;
60  }
61 
62  if (header_size + 1 > s->size) {
63  av_log(avctx, AV_LOG_ERROR, "Input packet too small.\n");
64  return AVERROR_INVALIDDATA;
65  }
66 
67  /* unscramble the header bytes with a XOR operation */
68  for (i = 1; i < header_size; i++)
69  header_buffer[i - 1] = s->buf[i] ^ s->buf[i + 1];
70 
71  s->delta_size = header_buffer[1];
72  s->hscale = 1 + !!header_buffer[3];
73  if (s->delta_size < 2 || s->delta_size > 4)
74  return AVERROR_INVALIDDATA;
75 
76  avctx->height = AV_RL16(header_buffer + 5);
77  avctx->width = AV_RL16(header_buffer + 7);
78 
79  return header_size;
80 }
81 
82 static const int16_t delta_tab4[] = {
83  1, -1, 2, -3, 8, -8, 18, -18, 36, -36, 54, -54, 96, -96, 144, -144
84 };
85 
86 static const int16_t delta_tab3[] = {
87  2, -3, 8, -8, 18, -18, 36, -36
88 };
89 
90 static const int16_t delta_tab2[] = {
91  5, -7, 36, -36
92 };
93 
94 static const int16_t *const delta_tabs[] = {
96 };
97 
98 static int decode_frame(AVCodecContext *avctx,
99  void *data, int *got_frame,
100  AVPacket *avpkt)
101 {
102  TrueMotion2RTContext *s = avctx->priv_data;
103  const uint8_t *buf = avpkt->data;
104  int ret, buf_size = avpkt->size;
105  AVFrame * const p = data;
106  GetBitContext *gb = &s->gb;
107  uint8_t *dst;
108  int x, y, delta_mode;
109 
110  s->buf = buf;
111  s->size = buf_size;
112 
113  if ((ret = truemotion2rt_decode_header(avctx)) < 0)
114  return ret;
115 
116  if ((ret = init_get_bits8(gb, buf + ret, buf_size - ret)) < 0)
117  return ret;
118 
119  if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
120  return ret;
121 
122  skip_bits(gb, 32);
123  delta_mode = s->delta_size - 2;
124  dst = p->data[0];
125  for (y = 0; y < avctx->height; y++) {
126  int diff = 0;
127  for (x = 0; x < avctx->width; x += s->hscale) {
128  diff += delta_tabs[delta_mode][get_bits(gb, s->delta_size)];
129  dst[x] = av_clip_uint8((y ? dst[x - p->linesize[0]] : 0) + diff);
130  }
131  dst += p->linesize[0];
132  }
133 
134  if (s->hscale > 1) {
135  dst = p->data[0];
136  for (y = 0; y < avctx->height; y++) {
137  for (x = 1; x < avctx->width; x += s->hscale) {
138  dst[x] = dst[x - 1];
139  }
140  dst += p->linesize[0];
141  }
142  }
143 
144  dst = p->data[0];
145  for (y = 0; y < avctx->height; y++) {
146  for (x = 0; x < avctx->width; x++)
147  dst[x] = av_clip_uint8(dst[x] + (dst[x] - 128) / 3);
148  dst += p->linesize[0];
149  }
150 
151  dst = p->data[1];
152  for (y = 0; y < avctx->height >> 2; y++) {
153  int diff = 0;
154  for (x = 0; x < avctx->width >> 2; x += s->hscale) {
155  diff += delta_tabs[delta_mode][get_bits(gb, s->delta_size)];
156  dst[x] = av_clip_uint8((y ? dst[x - p->linesize[1]] : 128) + diff);
157  }
158  dst += p->linesize[1];
159  }
160 
161  if (s->hscale > 1) {
162  dst = p->data[1];
163  for (y = 0; y < avctx->height >> 2; y++) {
164  for (x = 1; x < avctx->width >> 2; x += s->hscale) {
165  dst[x] = dst[x - 1];
166  }
167  dst += p->linesize[1];
168  }
169  }
170 
171  dst = p->data[1];
172  for (y = 0; y < avctx->height >> 2; y++) {
173  for (x = 0; x < avctx->width >> 2; x++)
174  dst[x] += (dst[x] - 128) / 8;
175  dst += p->linesize[1];
176  }
177 
178  dst = p->data[2];
179  for (y = 0; y < avctx->height >> 2; y++) {
180  int diff = 0;
181  for (x = 0; x < avctx->width >> 2; x += s->hscale) {
182  diff += delta_tabs[delta_mode][get_bits(gb, s->delta_size)];
183  dst[x] = av_clip_uint8((y ? dst[x - p->linesize[2]] : 128) + diff);
184  }
185  dst += p->linesize[2];
186  }
187 
188  if (s->hscale > 1) {
189  dst = p->data[2];
190  for (y = 0; y < avctx->height >> 2; y++) {
191  for (x = 1; x < avctx->width >> 2; x += s->hscale) {
192  dst[x] = dst[x - 1];
193  }
194  dst += p->linesize[2];
195  }
196  }
197 
198  dst = p->data[2];
199  for (y = 0; y < avctx->height >> 2; y++) {
200  for (x = 0; x < avctx->width >> 2; x++)
201  dst[x] += (dst[x] - 128) / 8;
202  dst += p->linesize[2];
203  }
204 
206  p->key_frame = 1;
207  *got_frame = 1;
208 
209  return buf_size;
210 }
211 
213  .name = "truemotion2rt",
214  .long_name = NULL_IF_CONFIG_SMALL("Duck TrueMotion 2.0 Real Time"),
215  .type = AVMEDIA_TYPE_VIDEO,
217  .priv_data_size = sizeof(TrueMotion2RTContext),
218  .init = decode_init,
219  .decode = decode_frame,
220  .capabilities = AV_CODEC_CAP_DR1,
221 };
const char * s
Definition: avisynth_c.h:631
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
This structure describes decoded (raw) audio or video data.
Definition: frame.h:184
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
misc image utilities
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:247
memory handling functions
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
int size
Definition: avcodec.h:1581
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1877
AVCodec.
Definition: avcodec.h:3542
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:87
static const int16_t *const delta_tabs[]
Definition: truemotion2rt.c:94
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: truemotion2rt.c:98
uint8_t
GetBitContext gb
Definition: truemotion2rt.c:35
#define av_cold
Definition: attributes.h:82
uint8_t * data
Definition: avcodec.h:1580
bitstream reader API header.
static const int16_t delta_tab2[]
Definition: truemotion2rt.c:90
#define av_log(a,...)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
AVCodec ff_truemotion2rt_decoder
const char * name
Name of the codec implementation.
Definition: avcodec.h:3549
common internal API header
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:258
int width
picture width / height.
Definition: avcodec.h:1836
static av_cold int decode_init(AVCodecContext *avctx)
Definition: truemotion2rt.c:42
const uint8_t * buf
Definition: truemotion2rt.c:36
static const int16_t delta_tab4[]
Definition: truemotion2rt.c:82
static const int16_t delta_tab3[]
Definition: truemotion2rt.c:86
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:215
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:437
main external API structure.
Definition: avcodec.h:1649
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:928
void * buf
Definition: avisynth_c.h:553
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:68
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:292
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:198
static int decode(AVCodecContext *avctx, void *data, int *got_sub, AVPacket *avpkt)
Definition: ccaption_dec.c:722
common internal api header.
if(ret< 0)
Definition: vf_mcdeint.c:282
void * priv_data
Definition: avcodec.h:1691
static av_always_inline int diff(const uint32_t a, const uint32_t b)
static int truemotion2rt_decode_header(AVCodecContext *avctx)
Definition: truemotion2rt.c:50
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:253
This structure stores compressed data.
Definition: avcodec.h:1557
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:956