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 | |
Paul B Mahol | c85d042 | 2016-04-06 09:02:39 | [diff] [blame] | 21 | #include "libavutil/internal.h" |
| 22 | #include "libavutil/intreadwrite.h" |
Paul B Mahol | c85d042 | 2016-04-06 09:02:39 | [diff] [blame] | 23 | |
Paul B Mahol | 470cd0c | 2016-06-17 16:30:34 | [diff] [blame] | 24 | #define BITSTREAM_READER_LE |
| 25 | #include "avcodec.h" |
Andreas Rheinhardt | a688f3c | 2022-03-16 17:18:28 | [diff] [blame] | 26 | #include "codec_internal.h" |
Andreas Rheinhardt | 66b691f | 2022-08-24 19:28:16 | [diff] [blame] | 27 | #include "decode.h" |
Paul B Mahol | 470cd0c | 2016-06-17 16:30:34 | [diff] [blame] | 28 | #include "get_bits.h" |
Paul B Mahol | 470cd0c | 2016-06-17 16:30:34 | [diff] [blame] | 29 | |
Paul B Mahol | c85d042 | 2016-04-06 09:02:39 | [diff] [blame] | 30 | typedef struct TrueMotion2RTContext { |
| 31 | GetBitContext gb; |
Paul B Mahol | c85d042 | 2016-04-06 09:02:39 | [diff] [blame] | 32 | int delta_size; |
| 33 | int hscale; |
| 34 | } TrueMotion2RTContext; |
| 35 | |
Paul B Mahol | 470cd0c | 2016-06-17 16:30:34 | [diff] [blame] | 36 | static const int16_t delta_tab2[] = { |
| 37 | 5, -7, 36, -36, |
| 38 | }; |
Paul B Mahol | c85d042 | 2016-04-06 09:02:39 | [diff] [blame] | 39 | |
Paul B Mahol | 470cd0c | 2016-06-17 16:30:34 | [diff] [blame] | 40 | static const int16_t delta_tab3[] = { |
| 41 | 2, -3, 8, -8, 18, -18, 36, -36, |
| 42 | }; |
| 43 | |
| 44 | static const int16_t delta_tab4[] = { |
| 45 | 1, -1, 2, -3, 8, -8, 18, -18, 36, -36, 54, -54, 96, -96, 144, -144, |
| 46 | }; |
| 47 | |
| 48 | static const int16_t *const delta_tabs[] = { |
| 49 | delta_tab2, delta_tab3, delta_tab4, |
| 50 | }; |
| 51 | |
| 52 | /* Returns the number of bytes consumed from the bytestream, or |
| 53 | * AVERROR_INVALIDDATA if there was an error while decoding the header. */ |
Clément Bœsch | bec7145 | 2016-07-14 17:29:43 | [diff] [blame] | 54 | static int truemotion2rt_decode_header(AVCodecContext *avctx, const AVPacket *avpkt) |
Paul B Mahol | c85d042 | 2016-04-06 09:02:39 | [diff] [blame] | 55 | { |
| 56 | TrueMotion2RTContext *s = avctx->priv_data; |
Paul B Mahol | 470cd0c | 2016-06-17 16:30:34 | [diff] [blame] | 57 | int header_size; |
| 58 | uint8_t header_buffer[128] = { 0 }; /* logical maximum header size */ |
| 59 | const uint8_t *buf = avpkt->data; |
| 60 | int size = avpkt->size; |
Vittorio Giovara | 40dd516 | 2016-06-21 13:11:32 | [diff] [blame] | 61 | int width, height; |
| 62 | int ret, i; |
Paul B Mahol | c85d042 | 2016-04-06 09:02:39 | [diff] [blame] | 63 | |
Paul B Mahol | 470cd0c | 2016-06-17 16:30:34 | [diff] [blame] | 64 | if (size < 1) { |
| 65 | av_log(avctx, AV_LOG_ERROR, "input packet too small (%d)\n", size); |
| 66 | return AVERROR_INVALIDDATA; |
| 67 | } |
| 68 | |
| 69 | header_size = ((buf[0] >> 5) | (buf[0] << 3)) & 0x7f; |
Paul B Mahol | c85d042 | 2016-04-06 09:02:39 | [diff] [blame] | 70 | if (header_size < 10) { |
| 71 | av_log(avctx, AV_LOG_ERROR, "invalid header size (%d)\n", header_size); |
| 72 | return AVERROR_INVALIDDATA; |
| 73 | } |
| 74 | |
Paul B Mahol | 470cd0c | 2016-06-17 16:30:34 | [diff] [blame] | 75 | if (header_size + 1 > size) { |
| 76 | 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] | 77 | return AVERROR_INVALIDDATA; |
| 78 | } |
| 79 | |
| 80 | /* unscramble the header bytes with a XOR operation */ |
| 81 | for (i = 1; i < header_size; i++) |
Paul B Mahol | 470cd0c | 2016-06-17 16:30:34 | [diff] [blame] | 82 | header_buffer[i - 1] = buf[i] ^ buf[i + 1]; |
Paul B Mahol | c85d042 | 2016-04-06 09:02:39 | [diff] [blame] | 83 | |
| 84 | s->delta_size = header_buffer[1]; |
| 85 | s->hscale = 1 + !!header_buffer[3]; |
| 86 | if (s->delta_size < 2 || s->delta_size > 4) |
| 87 | return AVERROR_INVALIDDATA; |
| 88 | |
Vittorio Giovara | 40dd516 | 2016-06-21 13:11:32 | [diff] [blame] | 89 | height = AV_RL16(header_buffer + 5); |
| 90 | width = AV_RL16(header_buffer + 7); |
| 91 | |
| 92 | ret = ff_set_dimensions(avctx, width, height); |
| 93 | if (ret < 0) |
| 94 | return ret; |
Paul B Mahol | c85d042 | 2016-04-06 09:02:39 | [diff] [blame] | 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 | |
Andreas Rheinhardt | ce7dbd0 | 2022-03-30 19:33:24 | [diff] [blame] | 100 | static int truemotion2rt_decode_frame(AVCodecContext *avctx, AVFrame *p, |
Paul B Mahol | 470cd0c | 2016-06-17 16:30:34 | [diff] [blame] | 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 | GetBitContext *gb = &s->gb; |
| 105 | uint8_t *dst; |
| 106 | int x, y, delta_mode; |
Paul B Mahol | 470cd0c | 2016-06-17 16:30:34 | [diff] [blame] | 107 | int ret; |
Paul B Mahol | c85d042 | 2016-04-06 09:02:39 | [diff] [blame] | 108 | |
Paul B Mahol | 470cd0c | 2016-06-17 16:30:34 | [diff] [blame] | 109 | ret = truemotion2rt_decode_header(avctx, avpkt); |
| 110 | if (ret < 0) |
Paul B Mahol | c85d042 | 2016-04-06 09:02:39 | [diff] [blame] | 111 | return ret; |
| 112 | |
Michael Niedermayer | 7f22a4e | 2018-11-17 08:24:30 | [diff] [blame] | 113 | if ((avctx->width + s->hscale - 1)/ s->hscale * avctx->height * s->delta_size > avpkt->size * 8LL * 4) |
Michael Niedermayer | 8b5c29b | 2018-02-22 02:04:40 | [diff] [blame] | 114 | return AVERROR_INVALIDDATA; |
| 115 | |
Paul B Mahol | 470cd0c | 2016-06-17 16:30:34 | [diff] [blame] | 116 | ret = init_get_bits8(gb, avpkt->data + ret, avpkt->size - ret); |
| 117 | if (ret < 0) |
Paul B Mahol | c85d042 | 2016-04-06 09:02:39 | [diff] [blame] | 118 | return ret; |
| 119 | |
Paul B Mahol | 470cd0c | 2016-06-17 16:30:34 | [diff] [blame] | 120 | ret = ff_get_buffer(avctx, p, 0); |
| 121 | if (ret < 0) |
Paul B Mahol | c85d042 | 2016-04-06 09:02:39 | [diff] [blame] | 122 | return ret; |
| 123 | |
| 124 | skip_bits(gb, 32); |
| 125 | delta_mode = s->delta_size - 2; |
| 126 | dst = p->data[0]; |
| 127 | for (y = 0; y < avctx->height; y++) { |
| 128 | int diff = 0; |
| 129 | for (x = 0; x < avctx->width; x += s->hscale) { |
| 130 | diff += delta_tabs[delta_mode][get_bits(gb, s->delta_size)]; |
| 131 | dst[x] = av_clip_uint8((y ? dst[x - p->linesize[0]] : 0) + diff); |
| 132 | } |
| 133 | dst += p->linesize[0]; |
| 134 | } |
| 135 | |
| 136 | if (s->hscale > 1) { |
| 137 | dst = p->data[0]; |
| 138 | for (y = 0; y < avctx->height; y++) { |
Paul B Mahol | 470cd0c | 2016-06-17 16:30:34 | [diff] [blame] | 139 | for (x = 1; x < avctx->width; x += s->hscale) |
Paul B Mahol | c85d042 | 2016-04-06 09:02:39 | [diff] [blame] | 140 | dst[x] = dst[x - 1]; |
Paul B Mahol | c85d042 | 2016-04-06 09:02:39 | [diff] [blame] | 141 | dst += p->linesize[0]; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | dst = p->data[0]; |
| 146 | for (y = 0; y < avctx->height; y++) { |
| 147 | for (x = 0; x < avctx->width; x++) |
| 148 | dst[x] = av_clip_uint8(dst[x] + (dst[x] - 128) / 3); |
| 149 | dst += p->linesize[0]; |
| 150 | } |
| 151 | |
| 152 | dst = p->data[1]; |
| 153 | for (y = 0; y < avctx->height >> 2; y++) { |
| 154 | int diff = 0; |
| 155 | for (x = 0; x < avctx->width >> 2; x += s->hscale) { |
| 156 | diff += delta_tabs[delta_mode][get_bits(gb, s->delta_size)]; |
| 157 | dst[x] = av_clip_uint8((y ? dst[x - p->linesize[1]] : 128) + diff); |
| 158 | } |
| 159 | dst += p->linesize[1]; |
| 160 | } |
| 161 | |
| 162 | if (s->hscale > 1) { |
| 163 | dst = p->data[1]; |
| 164 | for (y = 0; y < avctx->height >> 2; y++) { |
Paul B Mahol | 470cd0c | 2016-06-17 16:30:34 | [diff] [blame] | 165 | for (x = 1; x < avctx->width >> 2; x += s->hscale) |
Paul B Mahol | c85d042 | 2016-04-06 09:02:39 | [diff] [blame] | 166 | dst[x] = dst[x - 1]; |
Paul B Mahol | c85d042 | 2016-04-06 09:02:39 | [diff] [blame] | 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++) { |
Paul B Mahol | 470cd0c | 2016-06-17 16:30:34 | [diff] [blame] | 191 | for (x = 1; x < avctx->width >> 2; x += s->hscale) |
Paul B Mahol | c85d042 | 2016-04-06 09:02:39 | [diff] [blame] | 192 | dst[x] = dst[x - 1]; |
Paul B Mahol | c85d042 | 2016-04-06 09:02:39 | [diff] [blame] | 193 | dst += p->linesize[2]; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | dst = p->data[2]; |
| 198 | for (y = 0; y < avctx->height >> 2; y++) { |
| 199 | for (x = 0; x < avctx->width >> 2; x++) |
| 200 | dst[x] += (dst[x] - 128) / 8; |
| 201 | dst += p->linesize[2]; |
| 202 | } |
| 203 | |
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 | |
Andreas Rheinhardt | 20f9727 | 2022-03-16 20:09:54 | [diff] [blame] | 215 | const FFCodec ff_truemotion2rt_decoder = { |
| 216 | .p.name = "truemotion2rt", |
Andreas Rheinhardt | 48286d4 | 2022-08-29 11:38:02 | [diff] [blame] | 217 | CODEC_LONG_NAME("Duck TrueMotion 2.0 Real Time"), |
Andreas Rheinhardt | 20f9727 | 2022-03-16 20:09:54 | [diff] [blame] | 218 | .p.type = AVMEDIA_TYPE_VIDEO, |
| 219 | .p.id = AV_CODEC_ID_TRUEMOTION2RT, |
Paul B Mahol | c85d042 | 2016-04-06 09:02:39 | [diff] [blame] | 220 | .priv_data_size = sizeof(TrueMotion2RTContext), |
Paul B Mahol | 470cd0c | 2016-06-17 16:30:34 | [diff] [blame] | 221 | .init = truemotion2rt_decode_init, |
Andreas Rheinhardt | 4243da4 | 2022-03-30 21:28:24 | [diff] [blame] | 222 | FF_CODEC_DECODE_CB(truemotion2rt_decode_frame), |
Andreas Rheinhardt | 20f9727 | 2022-03-16 20:09:54 | [diff] [blame] | 223 | .p.capabilities = AV_CODEC_CAP_DR1, |
Paul B Mahol | c85d042 | 2016-04-06 09:02:39 | [diff] [blame] | 224 | }; |