]> git.sesse.net Git - ffmpeg/blob - libavcodec/truemotion2rt.c
Merge commit '74beead9bd596180bcac6108548fc0a86d8eb4ae'
[ffmpeg] / libavcodec / truemotion2rt.c
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 {
35     GetBitContext gb;
36     const uint8_t *buf;
37     int size;
38     int delta_size;
39     int hscale;
40 } TrueMotion2RTContext;
41
42 static av_cold int decode_init(AVCodecContext *avctx)
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 */
50 static int truemotion2rt_decode_header(AVCodecContext *avctx)
51 {
52     TrueMotion2RTContext *s = avctx->priv_data;
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[] = {
95     delta_tab2, delta_tab3, delta_tab4,
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
205     p->pict_type = AV_PICTURE_TYPE_I;
206     p->key_frame = 1;
207     *got_frame   = 1;
208
209     return buf_size;
210 }
211
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),
218     .init           = decode_init,
219     .decode         = decode_frame,
220     .capabilities   = AV_CODEC_CAP_DR1,
221 };