Paul B Mahol | c85d042 | 2016-04-06 09:02:39 | [diff] [blame] | 1 | /* |
Paul B Mahol | 470cd0c | 2016-06-17 16:30:34 | [diff] [blame] | 2 | * Duck TrueMotion 2.0 Real Time decoder |
Paul B Mahol | c85d042 | 2016-04-06 09:02:39 | [diff] [blame] | 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 | |
Paul B Mahol | c85d042 | 2016-04-06 09:02:39 | [diff] [blame] | 25 | #include "libavutil/imgutils.h" |
| 26 | #include "libavutil/internal.h" |
| 27 | #include "libavutil/intreadwrite.h" |
| 28 | #include "libavutil/mem.h" |
| 29 | |
Paul B Mahol | 470cd0c | 2016-06-17 16:30:34 | [diff] [blame] | 30 | #define BITSTREAM_READER_LE |
| 31 | #include "avcodec.h" |
| 32 | #include "get_bits.h" |
| 33 | #include "internal.h" |
| 34 | |
Paul B Mahol | c85d042 | 2016-04-06 09:02:39 | [diff] [blame] | 35 | typedef struct TrueMotion2RTContext { |
| 36 | GetBitContext gb; |
Paul B Mahol | c85d042 | 2016-04-06 09:02:39 | [diff] [blame] | 37 | int delta_size; |
| 38 | int hscale; |
| 39 | } TrueMotion2RTContext; |
| 40 | |
Paul B Mahol | 470cd0c | 2016-06-17 16:30:34 | [diff] [blame] | 41 | static const int16_t delta_tab2[] = { |
| 42 | 5, -7, 36, -36, |
| 43 | }; |
Paul B Mahol | c85d042 | 2016-04-06 09:02:39 | [diff] [blame] | 44 | |
Paul B Mahol | 470cd0c | 2016-06-17 16:30:34 | [diff] [blame] | 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[] = { |
| 54 | delta_tab2, delta_tab3, delta_tab4, |
| 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. */ |
Clément Bœsch | bec7145 | 2016-07-14 17:29:43 | [diff] [blame^] | 59 | static int truemotion2rt_decode_header(AVCodecContext *avctx, const AVPacket *avpkt) |
Paul B Mahol | c85d042 | 2016-04-06 09:02:39 | [diff] [blame] | 60 | { |
| 61 | TrueMotion2RTContext *s = avctx->priv_data; |
Paul B Mahol | 470cd0c | 2016-06-17 16:30:34 | [diff] [blame] | 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 i; |
Paul B Mahol | c85d042 | 2016-04-06 09:02:39 | [diff] [blame] | 67 | |
Paul B Mahol | 470cd0c | 2016-06-17 16:30:34 | [diff] [blame] | 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; |
Paul B Mahol | c85d042 | 2016-04-06 09:02:39 | [diff] [blame] | 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 | |
Paul B Mahol | 470cd0c | 2016-06-17 16:30:34 | [diff] [blame] | 79 | if (header_size + 1 > size) { |
| 80 | av_log(avctx, AV_LOG_ERROR, "input packet too small (%d)\n", size); |
Paul B Mahol | c85d042 | 2016-04-06 09:02:39 | [diff] [blame] | 81 | return AVERROR_INVALIDDATA; |
| 82 | } |
| 83 | |
| 84 | /* unscramble the header bytes with a XOR operation */ |
| 85 | for (i = 1; i < header_size; i++) |
Paul B Mahol | 470cd0c | 2016-06-17 16:30:34 | [diff] [blame] | 86 | header_buffer[i - 1] = buf[i] ^ buf[i + 1]; |
Paul B Mahol | c85d042 | 2016-04-06 09:02:39 | [diff] [blame] | 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 | avctx->height = AV_RL16(header_buffer + 5); |
| 94 | avctx->width = AV_RL16(header_buffer + 7); |
| 95 | |
Paul B Mahol | 470cd0c | 2016-06-17 16:30:34 | [diff] [blame] | 96 | av_log(avctx, AV_LOG_DEBUG, "Header size: %d\n", header_size); |
Paul B Mahol | c85d042 | 2016-04-06 09:02:39 | [diff] [blame] | 97 | return header_size; |
| 98 | } |
| 99 | |
Paul B Mahol | 470cd0c | 2016-06-17 16:30:34 | [diff] [blame] | 100 | static int truemotion2rt_decode_frame(AVCodecContext *avctx, void *data, |
| 101 | int *got_frame, AVPacket *avpkt) |
Paul B Mahol | c85d042 | 2016-04-06 09:02:39 | [diff] [blame] | 102 | { |
| 103 | TrueMotion2RTContext *s = avctx->priv_data; |
Paul B Mahol | c85d042 | 2016-04-06 09:02:39 | [diff] [blame] | 104 | AVFrame * const p = data; |
| 105 | GetBitContext *gb = &s->gb; |
| 106 | uint8_t *dst; |
| 107 | int x, y, delta_mode; |
Paul B Mahol | 470cd0c | 2016-06-17 16:30:34 | [diff] [blame] | 108 | int ret; |
Paul B Mahol | c85d042 | 2016-04-06 09:02:39 | [diff] [blame] | 109 | |
Paul B Mahol | 470cd0c | 2016-06-17 16:30:34 | [diff] [blame] | 110 | ret = truemotion2rt_decode_header(avctx, avpkt); |
| 111 | if (ret < 0) |
Paul B Mahol | c85d042 | 2016-04-06 09:02:39 | [diff] [blame] | 112 | return ret; |
| 113 | |
Paul B Mahol | 470cd0c | 2016-06-17 16:30:34 | [diff] [blame] | 114 | ret = init_get_bits8(gb, avpkt->data + ret, avpkt->size - ret); |
| 115 | if (ret < 0) |
Paul B Mahol | c85d042 | 2016-04-06 09:02:39 | [diff] [blame] | 116 | return ret; |
| 117 | |
Paul B Mahol | 470cd0c | 2016-06-17 16:30:34 | [diff] [blame] | 118 | ret = ff_get_buffer(avctx, p, 0); |
| 119 | if (ret < 0) |
Paul B Mahol | c85d042 | 2016-04-06 09:02:39 | [diff] [blame] | 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++) { |
Paul B Mahol | 470cd0c | 2016-06-17 16:30:34 | [diff] [blame] | 137 | for (x = 1; x < avctx->width; x += s->hscale) |
Paul B Mahol | c85d042 | 2016-04-06 09:02:39 | [diff] [blame] | 138 | dst[x] = dst[x - 1]; |
Paul B Mahol | c85d042 | 2016-04-06 09:02:39 | [diff] [blame] | 139 | dst += p->linesize[0]; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | dst = p->data[0]; |
| 144 | for (y = 0; y < avctx->height; y++) { |
| 145 | for (x = 0; x < avctx->width; x++) |
| 146 | dst[x] = av_clip_uint8(dst[x] + (dst[x] - 128) / 3); |
| 147 | dst += p->linesize[0]; |
| 148 | } |
| 149 | |
| 150 | dst = p->data[1]; |
| 151 | for (y = 0; y < avctx->height >> 2; y++) { |
| 152 | int diff = 0; |
| 153 | for (x = 0; x < avctx->width >> 2; x += s->hscale) { |
| 154 | diff += delta_tabs[delta_mode][get_bits(gb, s->delta_size)]; |
| 155 | dst[x] = av_clip_uint8((y ? dst[x - p->linesize[1]] : 128) + diff); |
| 156 | } |
| 157 | dst += p->linesize[1]; |
| 158 | } |
| 159 | |
| 160 | if (s->hscale > 1) { |
| 161 | dst = p->data[1]; |
| 162 | for (y = 0; y < avctx->height >> 2; y++) { |
Paul B Mahol | 470cd0c | 2016-06-17 16:30:34 | [diff] [blame] | 163 | for (x = 1; x < avctx->width >> 2; x += s->hscale) |
Paul B Mahol | c85d042 | 2016-04-06 09:02:39 | [diff] [blame] | 164 | dst[x] = dst[x - 1]; |
Paul B Mahol | c85d042 | 2016-04-06 09:02:39 | [diff] [blame] | 165 | dst += p->linesize[1]; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | dst = p->data[1]; |
| 170 | for (y = 0; y < avctx->height >> 2; y++) { |
| 171 | for (x = 0; x < avctx->width >> 2; x++) |
| 172 | dst[x] += (dst[x] - 128) / 8; |
| 173 | dst += p->linesize[1]; |
| 174 | } |
| 175 | |
| 176 | dst = p->data[2]; |
| 177 | for (y = 0; y < avctx->height >> 2; y++) { |
| 178 | int diff = 0; |
| 179 | for (x = 0; x < avctx->width >> 2; x += s->hscale) { |
| 180 | diff += delta_tabs[delta_mode][get_bits(gb, s->delta_size)]; |
| 181 | dst[x] = av_clip_uint8((y ? dst[x - p->linesize[2]] : 128) + diff); |
| 182 | } |
| 183 | dst += p->linesize[2]; |
| 184 | } |
| 185 | |
| 186 | if (s->hscale > 1) { |
| 187 | dst = p->data[2]; |
| 188 | for (y = 0; y < avctx->height >> 2; y++) { |
Paul B Mahol | 470cd0c | 2016-06-17 16:30:34 | [diff] [blame] | 189 | for (x = 1; x < avctx->width >> 2; x += s->hscale) |
Paul B Mahol | c85d042 | 2016-04-06 09:02:39 | [diff] [blame] | 190 | dst[x] = dst[x - 1]; |
Paul B Mahol | c85d042 | 2016-04-06 09:02:39 | [diff] [blame] | 191 | dst += p->linesize[2]; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | dst = p->data[2]; |
| 196 | for (y = 0; y < avctx->height >> 2; y++) { |
| 197 | for (x = 0; x < avctx->width >> 2; x++) |
| 198 | dst[x] += (dst[x] - 128) / 8; |
| 199 | dst += p->linesize[2]; |
| 200 | } |
| 201 | |
| 202 | p->pict_type = AV_PICTURE_TYPE_I; |
Paul B Mahol | cd506c2 | 2016-05-22 21:32:38 | [diff] [blame] | 203 | p->key_frame = 1; |
Paul B Mahol | 470cd0c | 2016-06-17 16:30:34 | [diff] [blame] | 204 | *got_frame = 1; |
Paul B Mahol | c85d042 | 2016-04-06 09:02:39 | [diff] [blame] | 205 | |
Paul B Mahol | 470cd0c | 2016-06-17 16:30:34 | [diff] [blame] | 206 | return avpkt->size; |
| 207 | } |
| 208 | |
| 209 | static av_cold int truemotion2rt_decode_init(AVCodecContext *avctx) |
| 210 | { |
| 211 | avctx->pix_fmt = AV_PIX_FMT_YUV410P; |
| 212 | return 0; |
Paul B Mahol | c85d042 | 2016-04-06 09:02:39 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | AVCodec ff_truemotion2rt_decoder = { |
| 216 | .name = "truemotion2rt", |
| 217 | .long_name = NULL_IF_CONFIG_SMALL("Duck TrueMotion 2.0 Real Time"), |
| 218 | .type = AVMEDIA_TYPE_VIDEO, |
| 219 | .id = AV_CODEC_ID_TRUEMOTION2RT, |
| 220 | .priv_data_size = sizeof(TrueMotion2RTContext), |
Paul B Mahol | 470cd0c | 2016-06-17 16:30:34 | [diff] [blame] | 221 | .init = truemotion2rt_decode_init, |
| 222 | .decode = truemotion2rt_decode_frame, |
Paul B Mahol | c85d042 | 2016-04-06 09:02:39 | [diff] [blame] | 223 | .capabilities = AV_CODEC_CAP_DR1, |
Paul B Mahol | 470cd0c | 2016-06-17 16:30:34 | [diff] [blame] | 224 | .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE, |
Paul B Mahol | c85d042 | 2016-04-06 09:02:39 | [diff] [blame] | 225 | }; |