2 * Duck TrueMotion 2.0 Real Time decoder
4 * This file is part of FFmpeg.
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.
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.
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
25 #include "libavutil/imgutils.h"
26 #include "libavutil/internal.h"
27 #include "libavutil/intreadwrite.h"
28 #include "libavutil/mem.h"
30 #define BITSTREAM_READER_LE
35 typedef struct TrueMotion2RTContext {
39 } TrueMotion2RTContext;
41 static const int16_t delta_tab2[] = {
45 static const int16_t delta_tab3[] = {
46 2, -3, 8, -8, 18, -18, 36, -36,
49 static const int16_t delta_tab4[] = {
50 1, -1, 2, -3, 8, -8, 18, -18, 36, -36, 54, -54, 96, -96, 144, -144,
53 static const int16_t *const delta_tabs[] = {
54 delta_tab2, delta_tab3, delta_tab4,
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)
61 TrueMotion2RTContext *s = avctx->priv_data;
63 uint8_t header_buffer[128] = { 0 }; /* logical maximum header size */
64 const uint8_t *buf = avpkt->data;
65 int size = avpkt->size;
70 av_log(avctx, AV_LOG_ERROR, "input packet too small (%d)\n", size);
71 return AVERROR_INVALIDDATA;
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;
80 if (header_size + 1 > size) {
81 av_log(avctx, AV_LOG_ERROR, "input packet too small (%d)\n", size);
82 return AVERROR_INVALIDDATA;
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];
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;
94 height = AV_RL16(header_buffer + 5);
95 width = AV_RL16(header_buffer + 7);
97 ret = ff_set_dimensions(avctx, width, height);
101 av_log(avctx, AV_LOG_DEBUG, "Header size: %d\n", header_size);
105 static int truemotion2rt_decode_frame(AVCodecContext *avctx, void *data,
106 int *got_frame, AVPacket *avpkt)
108 TrueMotion2RTContext *s = avctx->priv_data;
109 AVFrame * const p = data;
110 GetBitContext *gb = &s->gb;
112 int x, y, delta_mode;
115 ret = truemotion2rt_decode_header(avctx, avpkt);
119 if ((avctx->width + s->hscale - 1)/ s->hscale * avctx->height * s->delta_size > avpkt->size * 8LL * 4)
120 return AVERROR_INVALIDDATA;
122 ret = init_get_bits8(gb, avpkt->data + ret, avpkt->size - ret);
126 ret = ff_get_buffer(avctx, p, 0);
131 delta_mode = s->delta_size - 2;
133 for (y = 0; y < avctx->height; y++) {
135 for (x = 0; x < avctx->width; x += s->hscale) {
136 diff += delta_tabs[delta_mode][get_bits(gb, s->delta_size)];
137 dst[x] = av_clip_uint8((y ? dst[x - p->linesize[0]] : 0) + diff);
139 dst += p->linesize[0];
144 for (y = 0; y < avctx->height; y++) {
145 for (x = 1; x < avctx->width; x += s->hscale)
147 dst += p->linesize[0];
152 for (y = 0; y < avctx->height; y++) {
153 for (x = 0; x < avctx->width; x++)
154 dst[x] = av_clip_uint8(dst[x] + (dst[x] - 128) / 3);
155 dst += p->linesize[0];
159 for (y = 0; y < avctx->height >> 2; y++) {
161 for (x = 0; x < avctx->width >> 2; x += s->hscale) {
162 diff += delta_tabs[delta_mode][get_bits(gb, s->delta_size)];
163 dst[x] = av_clip_uint8((y ? dst[x - p->linesize[1]] : 128) + diff);
165 dst += p->linesize[1];
170 for (y = 0; y < avctx->height >> 2; y++) {
171 for (x = 1; x < avctx->width >> 2; x += s->hscale)
173 dst += p->linesize[1];
178 for (y = 0; y < avctx->height >> 2; y++) {
179 for (x = 0; x < avctx->width >> 2; x++)
180 dst[x] += (dst[x] - 128) / 8;
181 dst += p->linesize[1];
185 for (y = 0; y < avctx->height >> 2; y++) {
187 for (x = 0; x < avctx->width >> 2; x += s->hscale) {
188 diff += delta_tabs[delta_mode][get_bits(gb, s->delta_size)];
189 dst[x] = av_clip_uint8((y ? dst[x - p->linesize[2]] : 128) + diff);
191 dst += p->linesize[2];
196 for (y = 0; y < avctx->height >> 2; y++) {
197 for (x = 1; x < avctx->width >> 2; x += s->hscale)
199 dst += p->linesize[2];
204 for (y = 0; y < avctx->height >> 2; y++) {
205 for (x = 0; x < avctx->width >> 2; x++)
206 dst[x] += (dst[x] - 128) / 8;
207 dst += p->linesize[2];
210 p->pict_type = AV_PICTURE_TYPE_I;
217 static av_cold int truemotion2rt_decode_init(AVCodecContext *avctx)
219 avctx->pix_fmt = AV_PIX_FMT_YUV410P;
223 AVCodec ff_truemotion2rt_decoder = {
224 .name = "truemotion2rt",
225 .long_name = NULL_IF_CONFIG_SMALL("Duck TrueMotion 2.0 Real Time"),
226 .type = AVMEDIA_TYPE_VIDEO,
227 .id = AV_CODEC_ID_TRUEMOTION2RT,
228 .priv_data_size = sizeof(TrueMotion2RTContext),
229 .init = truemotion2rt_decode_init,
230 .decode = truemotion2rt_decode_frame,
231 .capabilities = AV_CODEC_CAP_DR1,
232 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,