FFmpeg
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 
29 #define BITSTREAM_READER_LE
30 #include "avcodec.h"
31 #include "get_bits.h"
32 #include "internal.h"
33 
34 typedef struct TrueMotion2RTContext {
37  int hscale;
39 
40 static const int16_t delta_tab2[] = {
41  5, -7, 36, -36,
42 };
43 
44 static const int16_t delta_tab3[] = {
45  2, -3, 8, -8, 18, -18, 36, -36,
46 };
47 
48 static const int16_t delta_tab4[] = {
49  1, -1, 2, -3, 8, -8, 18, -18, 36, -36, 54, -54, 96, -96, 144, -144,
50 };
51 
52 static const int16_t *const delta_tabs[] = {
54 };
55 
56 /* Returns the number of bytes consumed from the bytestream, or
57  * AVERROR_INVALIDDATA if there was an error while decoding the header. */
58 static int truemotion2rt_decode_header(AVCodecContext *avctx, const AVPacket *avpkt)
59 {
61  int header_size;
62  uint8_t header_buffer[128] = { 0 }; /* logical maximum header size */
63  const uint8_t *buf = avpkt->data;
64  int size = avpkt->size;
65  int width, height;
66  int ret, i;
67 
68  if (size < 1) {
69  av_log(avctx, AV_LOG_ERROR, "input packet too small (%d)\n", size);
70  return AVERROR_INVALIDDATA;
71  }
72 
73  header_size = ((buf[0] >> 5) | (buf[0] << 3)) & 0x7f;
74  if (header_size < 10) {
75  av_log(avctx, AV_LOG_ERROR, "invalid header size (%d)\n", header_size);
76  return AVERROR_INVALIDDATA;
77  }
78 
79  if (header_size + 1 > size) {
80  av_log(avctx, AV_LOG_ERROR, "input packet too small (%d)\n", size);
81  return AVERROR_INVALIDDATA;
82  }
83 
84  /* unscramble the header bytes with a XOR operation */
85  for (i = 1; i < header_size; i++)
86  header_buffer[i - 1] = buf[i] ^ buf[i + 1];
87 
88  s->delta_size = header_buffer[1];
89  s->hscale = 1 + !!header_buffer[3];
90  if (s->delta_size < 2 || s->delta_size > 4)
91  return AVERROR_INVALIDDATA;
92 
93  height = AV_RL16(header_buffer + 5);
94  width = AV_RL16(header_buffer + 7);
95 
96  ret = ff_set_dimensions(avctx, width, height);
97  if (ret < 0)
98  return ret;
99 
100  av_log(avctx, AV_LOG_DEBUG, "Header size: %d\n", header_size);
101  return header_size;
102 }
103 
105  int *got_frame, AVPacket *avpkt)
106 {
107  TrueMotion2RTContext *s = avctx->priv_data;
108  AVFrame * const p = data;
109  GetBitContext *gb = &s->gb;
110  uint8_t *dst;
111  int x, y, delta_mode;
112  int ret;
113 
114  ret = truemotion2rt_decode_header(avctx, avpkt);
115  if (ret < 0)
116  return ret;
117 
118  if ((avctx->width + s->hscale - 1)/ s->hscale * avctx->height * s->delta_size > avpkt->size * 8LL * 4)
119  return AVERROR_INVALIDDATA;
120 
121  ret = init_get_bits8(gb, avpkt->data + ret, avpkt->size - ret);
122  if (ret < 0)
123  return ret;
124 
125  ret = ff_get_buffer(avctx, p, 0);
126  if (ret < 0)
127  return ret;
128 
129  skip_bits(gb, 32);
130  delta_mode = s->delta_size - 2;
131  dst = p->data[0];
132  for (y = 0; y < avctx->height; y++) {
133  int diff = 0;
134  for (x = 0; x < avctx->width; x += s->hscale) {
135  diff += delta_tabs[delta_mode][get_bits(gb, s->delta_size)];
136  dst[x] = av_clip_uint8((y ? dst[x - p->linesize[0]] : 0) + diff);
137  }
138  dst += p->linesize[0];
139  }
140 
141  if (s->hscale > 1) {
142  dst = p->data[0];
143  for (y = 0; y < avctx->height; y++) {
144  for (x = 1; x < avctx->width; x += s->hscale)
145  dst[x] = dst[x - 1];
146  dst += p->linesize[0];
147  }
148  }
149 
150  dst = p->data[0];
151  for (y = 0; y < avctx->height; y++) {
152  for (x = 0; x < avctx->width; x++)
153  dst[x] = av_clip_uint8(dst[x] + (dst[x] - 128) / 3);
154  dst += p->linesize[0];
155  }
156 
157  dst = p->data[1];
158  for (y = 0; y < avctx->height >> 2; y++) {
159  int diff = 0;
160  for (x = 0; x < avctx->width >> 2; x += s->hscale) {
161  diff += delta_tabs[delta_mode][get_bits(gb, s->delta_size)];
162  dst[x] = av_clip_uint8((y ? dst[x - p->linesize[1]] : 128) + diff);
163  }
164  dst += p->linesize[1];
165  }
166 
167  if (s->hscale > 1) {
168  dst = p->data[1];
169  for (y = 0; y < avctx->height >> 2; y++) {
170  for (x = 1; x < avctx->width >> 2; x += s->hscale)
171  dst[x] = dst[x - 1];
172  dst += p->linesize[1];
173  }
174  }
175 
176  dst = p->data[1];
177  for (y = 0; y < avctx->height >> 2; y++) {
178  for (x = 0; x < avctx->width >> 2; x++)
179  dst[x] += (dst[x] - 128) / 8;
180  dst += p->linesize[1];
181  }
182 
183  dst = p->data[2];
184  for (y = 0; y < avctx->height >> 2; y++) {
185  int diff = 0;
186  for (x = 0; x < avctx->width >> 2; x += s->hscale) {
187  diff += delta_tabs[delta_mode][get_bits(gb, s->delta_size)];
188  dst[x] = av_clip_uint8((y ? dst[x - p->linesize[2]] : 128) + diff);
189  }
190  dst += p->linesize[2];
191  }
192 
193  if (s->hscale > 1) {
194  dst = p->data[2];
195  for (y = 0; y < avctx->height >> 2; y++) {
196  for (x = 1; x < avctx->width >> 2; x += s->hscale)
197  dst[x] = dst[x - 1];
198  dst += p->linesize[2];
199  }
200  }
201 
202  dst = p->data[2];
203  for (y = 0; y < avctx->height >> 2; y++) {
204  for (x = 0; x < avctx->width >> 2; x++)
205  dst[x] += (dst[x] - 128) / 8;
206  dst += p->linesize[2];
207  }
208 
210  p->key_frame = 1;
211  *got_frame = 1;
212 
213  return avpkt->size;
214 }
215 
217 {
218  avctx->pix_fmt = AV_PIX_FMT_YUV410P;
219  return 0;
220 }
221 
223  .name = "truemotion2rt",
224  .long_name = NULL_IF_CONFIG_SMALL("Duck TrueMotion 2.0 Real Time"),
225  .type = AVMEDIA_TYPE_VIDEO,
227  .priv_data_size = sizeof(TrueMotion2RTContext),
230  .capabilities = AV_CODEC_CAP_DR1,
231  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
232 };
AV_CODEC_ID_TRUEMOTION2RT
@ AV_CODEC_ID_TRUEMOTION2RT
Definition: codec_id.h:267
AVCodec
AVCodec.
Definition: codec.h:202
FF_CODEC_CAP_INIT_THREADSAFE
#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:42
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:317
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:373
data
const char data[16]
Definition: mxf.c:143
delta_tabs
static const int16_t *const delta_tabs[]
Definition: truemotion2rt.c:52
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:338
ff_truemotion2rt_decoder
const AVCodec ff_truemotion2rt_decoder
Definition: truemotion2rt.c:222
init
static int init
Definition: av_tx.c:47
skip_bits
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:468
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:380
GetBitContext
Definition: get_bits.h:62
delta_tab2
static const int16_t delta_tab2[]
Definition: truemotion2rt.c:40
AVFrame::key_frame
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:409
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
init_get_bits8
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:678
TrueMotion2RTContext::hscale
int hscale
Definition: truemotion2rt.c:37
decode
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:71
width
#define width
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:257
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
get_bits.h
AV_RL16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:94
delta_tab4
static const int16_t delta_tab4[]
Definition: truemotion2rt.c:48
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
TrueMotion2RTContext
Definition: truemotion2rt.c:34
AVFrame::pict_type
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:414
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1652
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:374
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
size
int size
Definition: twinvq_data.h:10344
height
#define height
TrueMotion2RTContext::gb
GetBitContext gb
Definition: truemotion2rt.c:35
delta_tab3
static const int16_t delta_tab3[]
Definition: truemotion2rt.c:44
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
internal.h
truemotion2rt_decode_header
static int truemotion2rt_decode_header(AVCodecContext *avctx, const AVPacket *avpkt)
Definition: truemotion2rt.c:58
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:209
AVCodecContext::height
int height
Definition: avcodec.h:556
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:593
avcodec.h
ret
ret
Definition: filter_design.txt:187
TrueMotion2RTContext::delta_size
int delta_size
Definition: truemotion2rt.c:36
AVCodecContext
main external API structure.
Definition: avcodec.h:383
av_clip_uint8
#define av_clip_uint8
Definition: common.h:102
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
ff_set_dimensions
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:86
diff
static av_always_inline int diff(const uint32_t a, const uint32_t b)
Definition: vf_palettegen.c:139
truemotion2rt_decode_frame
static int truemotion2rt_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: truemotion2rt.c:104
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:410
AVPacket
This structure stores compressed data.
Definition: packet.h:350
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:556
imgutils.h
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition: frame.h:362
AV_PIX_FMT_YUV410P
@ AV_PIX_FMT_YUV410P
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:72
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
truemotion2rt_decode_init
static av_cold int truemotion2rt_decode_init(AVCodecContext *avctx)
Definition: truemotion2rt.c:216