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 "libavutil/imgutils.h"
26 #include "libavutil/internal.h"
27 #include "libavutil/intreadwrite.h"
28 #include "libavutil/mem.h"
29 
30 #define BITSTREAM_READER_LE
31 #include "avcodec.h"
32 #include "get_bits.h"
33 #include "internal.h"
34 
35 typedef struct TrueMotion2RTContext {
38  int hscale;
40 
41 static const int16_t delta_tab2[] = {
42  5, -7, 36, -36,
43 };
44 
45 static const int16_t delta_tab3[] = {
46  2, -3, 8, -8, 18, -18, 36, -36,
47 };
48 
49 static const int16_t delta_tab4[] = {
50  1, -1, 2, -3, 8, -8, 18, -18, 36, -36, 54, -54, 96, -96, 144, -144,
51 };
52 
53 static const int16_t *const delta_tabs[] = {
55 };
56 
57 /* Returns the number of bytes consumed from the bytestream, or
58  * AVERROR_INVALIDDATA if there was an error while decoding the header. */
59 static int truemotion2rt_decode_header(AVCodecContext *avctx, const AVPacket *avpkt)
60 {
62  int header_size;
63  uint8_t header_buffer[128] = { 0 }; /* logical maximum header size */
64  const uint8_t *buf = avpkt->data;
65  int size = avpkt->size;
66  int width, height;
67  int ret, i;
68 
69  if (size < 1) {
70  av_log(avctx, AV_LOG_ERROR, "input packet too small (%d)\n", size);
71  return AVERROR_INVALIDDATA;
72  }
73 
74  header_size = ((buf[0] >> 5) | (buf[0] << 3)) & 0x7f;
75  if (header_size < 10) {
76  av_log(avctx, AV_LOG_ERROR, "invalid header size (%d)\n", header_size);
77  return AVERROR_INVALIDDATA;
78  }
79 
80  if (header_size + 1 > size) {
81  av_log(avctx, AV_LOG_ERROR, "input packet too small (%d)\n", size);
82  return AVERROR_INVALIDDATA;
83  }
84 
85  /* unscramble the header bytes with a XOR operation */
86  for (i = 1; i < header_size; i++)
87  header_buffer[i - 1] = buf[i] ^ buf[i + 1];
88 
89  s->delta_size = header_buffer[1];
90  s->hscale = 1 + !!header_buffer[3];
91  if (s->delta_size < 2 || s->delta_size > 4)
92  return AVERROR_INVALIDDATA;
93 
94  height = AV_RL16(header_buffer + 5);
95  width = AV_RL16(header_buffer + 7);
96 
97  ret = ff_set_dimensions(avctx, width, height);
98  if (ret < 0)
99  return ret;
100 
101  av_log(avctx, AV_LOG_DEBUG, "Header size: %d\n", header_size);
102  return header_size;
103 }
104 
106  int *got_frame, AVPacket *avpkt)
107 {
108  TrueMotion2RTContext *s = avctx->priv_data;
109  AVFrame * const p = data;
110  GetBitContext *gb = &s->gb;
111  uint8_t *dst;
112  int x, y, delta_mode;
113  int ret;
114 
115  ret = truemotion2rt_decode_header(avctx, avpkt);
116  if (ret < 0)
117  return ret;
118 
119  ret = init_get_bits8(gb, avpkt->data + ret, avpkt->size - ret);
120  if (ret < 0)
121  return ret;
122 
123  ret = ff_get_buffer(avctx, p, 0);
124  if (ret < 0)
125  return ret;
126 
127  skip_bits(gb, 32);
128  delta_mode = s->delta_size - 2;
129  dst = p->data[0];
130  for (y = 0; y < avctx->height; y++) {
131  int diff = 0;
132  for (x = 0; x < avctx->width; x += s->hscale) {
133  diff += delta_tabs[delta_mode][get_bits(gb, s->delta_size)];
134  dst[x] = av_clip_uint8((y ? dst[x - p->linesize[0]] : 0) + diff);
135  }
136  dst += p->linesize[0];
137  }
138 
139  if (s->hscale > 1) {
140  dst = p->data[0];
141  for (y = 0; y < avctx->height; y++) {
142  for (x = 1; x < avctx->width; x += s->hscale)
143  dst[x] = dst[x - 1];
144  dst += p->linesize[0];
145  }
146  }
147 
148  dst = p->data[0];
149  for (y = 0; y < avctx->height; y++) {
150  for (x = 0; x < avctx->width; x++)
151  dst[x] = av_clip_uint8(dst[x] + (dst[x] - 128) / 3);
152  dst += p->linesize[0];
153  }
154 
155  dst = p->data[1];
156  for (y = 0; y < avctx->height >> 2; y++) {
157  int diff = 0;
158  for (x = 0; x < avctx->width >> 2; x += s->hscale) {
159  diff += delta_tabs[delta_mode][get_bits(gb, s->delta_size)];
160  dst[x] = av_clip_uint8((y ? dst[x - p->linesize[1]] : 128) + diff);
161  }
162  dst += p->linesize[1];
163  }
164 
165  if (s->hscale > 1) {
166  dst = p->data[1];
167  for (y = 0; y < avctx->height >> 2; y++) {
168  for (x = 1; x < avctx->width >> 2; x += s->hscale)
169  dst[x] = dst[x - 1];
170  dst += p->linesize[1];
171  }
172  }
173 
174  dst = p->data[1];
175  for (y = 0; y < avctx->height >> 2; y++) {
176  for (x = 0; x < avctx->width >> 2; x++)
177  dst[x] += (dst[x] - 128) / 8;
178  dst += p->linesize[1];
179  }
180 
181  dst = p->data[2];
182  for (y = 0; y < avctx->height >> 2; y++) {
183  int diff = 0;
184  for (x = 0; x < avctx->width >> 2; x += s->hscale) {
185  diff += delta_tabs[delta_mode][get_bits(gb, s->delta_size)];
186  dst[x] = av_clip_uint8((y ? dst[x - p->linesize[2]] : 128) + diff);
187  }
188  dst += p->linesize[2];
189  }
190 
191  if (s->hscale > 1) {
192  dst = p->data[2];
193  for (y = 0; y < avctx->height >> 2; y++) {
194  for (x = 1; x < avctx->width >> 2; x += s->hscale)
195  dst[x] = dst[x - 1];
196  dst += p->linesize[2];
197  }
198  }
199 
200  dst = p->data[2];
201  for (y = 0; y < avctx->height >> 2; y++) {
202  for (x = 0; x < avctx->width >> 2; x++)
203  dst[x] += (dst[x] - 128) / 8;
204  dst += p->linesize[2];
205  }
206 
208  p->key_frame = 1;
209  *got_frame = 1;
210 
211  return avpkt->size;
212 }
213 
215 {
216  avctx->pix_fmt = AV_PIX_FMT_YUV410P;
217  return 0;
218 }
219 
221  .name = "truemotion2rt",
222  .long_name = NULL_IF_CONFIG_SMALL("Duck TrueMotion 2.0 Real Time"),
223  .type = AVMEDIA_TYPE_VIDEO,
225  .priv_data_size = sizeof(TrueMotion2RTContext),
228  .capabilities = AV_CODEC_CAP_DR1,
229  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
230 };
const char * s
Definition: avisynth_c.h:768
#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:201
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:261
Memory handling functions.
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Check that the provided frame dimensions are valid and set them on the codec context.
Definition: utils.c:196
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
int size
Definition: avcodec.h:1401
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1697
AVCodec.
Definition: avcodec.h:3351
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 void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:42
static const int16_t *const delta_tabs[]
Definition: truemotion2rt.c:53
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:40
uint8_t
GetBitContext gb
Definition: truemotion2rt.c:36
#define av_cold
Definition: attributes.h:82
#define height
uint8_t * data
Definition: avcodec.h:1400
bitstream reader API header.
ptrdiff_t size
Definition: opengl_enc.c:101
static const int16_t delta_tab2[]
Definition: truemotion2rt.c:41
#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:186
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
uint16_t width
Definition: gdv.c:47
AVCodec ff_truemotion2rt_decoder
const char * name
Name of the codec implementation.
Definition: avcodec.h:3358
common internal API header
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:284
int width
picture width / height.
Definition: avcodec.h:1660
static const int16_t delta_tab4[]
Definition: truemotion2rt.c:49
static const int16_t delta_tab3[]
Definition: truemotion2rt.c:45
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:232
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:456
main external API structure.
Definition: avcodec.h:1488
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1886
void * buf
Definition: avisynth_c.h:690
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:306
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:215
static int truemotion2rt_decode_header(AVCodecContext *avctx, const AVPacket *avpkt)
Definition: truemotion2rt.c:59
common internal api header.
void * priv_data
Definition: avcodec.h:1515
static av_always_inline int diff(const uint32_t a, const uint32_t b)
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:279
static int truemotion2rt_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
This structure stores compressed data.
Definition: avcodec.h:1377
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:956
static av_cold int truemotion2rt_decode_init(AVCodecContext *avctx)