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
26 #define BITSTREAM_READER_LE
29 #include "libavutil/imgutils.h"
30 #include "libavutil/internal.h"
31 #include "libavutil/intreadwrite.h"
32 #include "libavutil/mem.h"
34 typedef struct TrueMotion2RTContext {
40 } TrueMotion2RTContext;
42 static av_cold int decode_init(AVCodecContext *avctx)
44 avctx->pix_fmt = AV_PIX_FMT_YUV410P;
48 /* Returns the number of bytes consumed from the bytestream. Returns -1 if
49 * there was an error while decoding the header */
50 static int truemotion2rt_decode_header(AVCodecContext *avctx)
52 TrueMotion2RTContext *s = avctx->priv_data;
53 uint8_t header_buffer[128] = { 0 }; /* logical maximum size of the header */
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;
62 if (header_size + 1 > s->size) {
63 av_log(avctx, AV_LOG_ERROR, "Input packet too small.\n");
64 return AVERROR_INVALIDDATA;
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];
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;
76 avctx->height = AV_RL16(header_buffer + 5);
77 avctx->width = AV_RL16(header_buffer + 7);
82 static const int16_t delta_tab4[] = {
83 1, -1, 2, -3, 8, -8, 18, -18, 36, -36, 54, -54, 96, -96, 144, -144
86 static const int16_t delta_tab3[] = {
87 2, -3, 8, -8, 18, -18, 36, -36
90 static const int16_t delta_tab2[] = {
94 static const int16_t *const delta_tabs[] = {
95 delta_tab2, delta_tab3, delta_tab4,
98 static int decode_frame(AVCodecContext *avctx,
99 void *data, int *got_frame,
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;
108 int x, y, delta_mode;
113 if ((ret = truemotion2rt_decode_header(avctx)) < 0)
116 if ((ret = init_get_bits8(gb, buf + ret, buf_size - ret)) < 0)
119 if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
123 delta_mode = s->delta_size - 2;
125 for (y = 0; y < avctx->height; y++) {
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);
131 dst += p->linesize[0];
136 for (y = 0; y < avctx->height; y++) {
137 for (x = 1; x < avctx->width; x += s->hscale) {
140 dst += p->linesize[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];
152 for (y = 0; y < avctx->height >> 2; y++) {
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);
158 dst += p->linesize[1];
163 for (y = 0; y < avctx->height >> 2; y++) {
164 for (x = 1; x < avctx->width >> 2; x += s->hscale) {
167 dst += p->linesize[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];
179 for (y = 0; y < avctx->height >> 2; y++) {
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);
185 dst += p->linesize[2];
190 for (y = 0; y < avctx->height >> 2; y++) {
191 for (x = 1; x < avctx->width >> 2; x += s->hscale) {
194 dst += p->linesize[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];
205 p->pict_type = AV_PICTURE_TYPE_I;
212 AVCodec ff_truemotion2rt_decoder = {
213 .name = "truemotion2rt",
214 .long_name = NULL_IF_CONFIG_SMALL("Duck TrueMotion 2.0 Real Time"),
215 .type = AVMEDIA_TYPE_VIDEO,
216 .id = AV_CODEC_ID_TRUEMOTION2RT,
217 .priv_data_size = sizeof(TrueMotion2RTContext),
219 .decode = decode_frame,
220 .capabilities = AV_CODEC_CAP_DR1,