You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

231 lines
6.7KB

  1. /*
  2. * Duck TrueMotion 2.0 Real Time decoder
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include "libavutil/imgutils.h"
  24. #include "libavutil/internal.h"
  25. #include "libavutil/intreadwrite.h"
  26. #include "libavutil/mem.h"
  27. #define BITSTREAM_READER_LE
  28. #include "avcodec.h"
  29. #include "get_bits.h"
  30. #include "internal.h"
  31. typedef struct TrueMotion2RTContext {
  32. GetBitContext gb;
  33. int delta_size;
  34. int hscale;
  35. } TrueMotion2RTContext;
  36. static const int16_t delta_tab2[] = {
  37. 5, -7, 36, -36,
  38. };
  39. static const int16_t delta_tab3[] = {
  40. 2, -3, 8, -8, 18, -18, 36, -36,
  41. };
  42. static const int16_t delta_tab4[] = {
  43. 1, -1, 2, -3, 8, -8, 18, -18, 36, -36, 54, -54, 96, -96, 144, -144,
  44. };
  45. static const int16_t *const delta_tabs[] = {
  46. delta_tab2, delta_tab3, delta_tab4,
  47. };
  48. /* Returns the number of bytes consumed from the bytestream, or
  49. * AVERROR_INVALIDDATA if there was an error while decoding the header. */
  50. static int truemotion2rt_decode_header(AVCodecContext *avctx, AVPacket *avpkt)
  51. {
  52. TrueMotion2RTContext *s = avctx->priv_data;
  53. int header_size;
  54. uint8_t header_buffer[128] = { 0 }; /* logical maximum header size */
  55. const uint8_t *buf = avpkt->data;
  56. int size = avpkt->size;
  57. int width, height;
  58. int ret, i;
  59. if (size < 1) {
  60. av_log(avctx, AV_LOG_ERROR, "input packet too small (%d)\n", size);
  61. return AVERROR_INVALIDDATA;
  62. }
  63. header_size = ((buf[0] >> 5) | (buf[0] << 3)) & 0x7f;
  64. if (header_size < 10) {
  65. av_log(avctx, AV_LOG_ERROR, "invalid header size (%d)\n", header_size);
  66. return AVERROR_INVALIDDATA;
  67. }
  68. if (header_size + 1 > size) {
  69. av_log(avctx, AV_LOG_ERROR, "input packet too small (%d)\n", size);
  70. return AVERROR_INVALIDDATA;
  71. }
  72. /* unscramble the header bytes with a XOR operation */
  73. for (i = 1; i < header_size; i++)
  74. header_buffer[i - 1] = buf[i] ^ buf[i + 1];
  75. s->delta_size = header_buffer[1];
  76. s->hscale = 1 + !!header_buffer[3];
  77. if (s->delta_size < 2 || s->delta_size > 4)
  78. return AVERROR_INVALIDDATA;
  79. height = AV_RL16(header_buffer + 5);
  80. width = AV_RL16(header_buffer + 7);
  81. ret = ff_set_dimensions(avctx, width, height);
  82. if (ret < 0)
  83. return ret;
  84. av_log(avctx, AV_LOG_DEBUG, "Header size: %d\n", header_size);
  85. return header_size;
  86. }
  87. static int truemotion2rt_decode_frame(AVCodecContext *avctx, void *data,
  88. int *got_frame, AVPacket *avpkt)
  89. {
  90. TrueMotion2RTContext *s = avctx->priv_data;
  91. AVFrame * const p = data;
  92. GetBitContext *gb = &s->gb;
  93. uint8_t *dst;
  94. int x, y, delta_mode;
  95. int ret;
  96. ret = truemotion2rt_decode_header(avctx, avpkt);
  97. if (ret < 0)
  98. return ret;
  99. ret = init_get_bits8(gb, avpkt->data + ret, avpkt->size - ret);
  100. if (ret < 0)
  101. return ret;
  102. ret = ff_get_buffer(avctx, p, 0);
  103. if (ret < 0)
  104. return ret;
  105. skip_bits(gb, 32);
  106. delta_mode = s->delta_size - 2;
  107. dst = p->data[0];
  108. for (y = 0; y < avctx->height; y++) {
  109. int diff = 0;
  110. for (x = 0; x < avctx->width; x += s->hscale) {
  111. diff += delta_tabs[delta_mode][get_bits(gb, s->delta_size)];
  112. dst[x] = av_clip_uint8((y ? dst[x - p->linesize[0]] : 0) + diff);
  113. }
  114. dst += p->linesize[0];
  115. }
  116. if (s->hscale > 1) {
  117. dst = p->data[0];
  118. for (y = 0; y < avctx->height; y++) {
  119. for (x = 1; x < avctx->width; x += s->hscale)
  120. dst[x] = dst[x - 1];
  121. dst += p->linesize[0];
  122. }
  123. }
  124. dst = p->data[0];
  125. for (y = 0; y < avctx->height; y++) {
  126. for (x = 0; x < avctx->width; x++)
  127. dst[x] = av_clip_uint8(dst[x] + (dst[x] - 128) / 3);
  128. dst += p->linesize[0];
  129. }
  130. dst = p->data[1];
  131. for (y = 0; y < avctx->height >> 2; y++) {
  132. int diff = 0;
  133. for (x = 0; x < avctx->width >> 2; x += s->hscale) {
  134. diff += delta_tabs[delta_mode][get_bits(gb, s->delta_size)];
  135. dst[x] = av_clip_uint8((y ? dst[x - p->linesize[1]] : 128) + diff);
  136. }
  137. dst += p->linesize[1];
  138. }
  139. if (s->hscale > 1) {
  140. dst = p->data[1];
  141. for (y = 0; y < avctx->height >> 2; y++) {
  142. for (x = 1; x < avctx->width >> 2; x += s->hscale)
  143. dst[x] = dst[x - 1];
  144. dst += p->linesize[1];
  145. }
  146. }
  147. dst = p->data[1];
  148. for (y = 0; y < avctx->height >> 2; y++) {
  149. for (x = 0; x < avctx->width >> 2; x++)
  150. dst[x] += (dst[x] - 128) / 8;
  151. dst += p->linesize[1];
  152. }
  153. dst = p->data[2];
  154. for (y = 0; y < avctx->height >> 2; y++) {
  155. int diff = 0;
  156. for (x = 0; x < avctx->width >> 2; x += s->hscale) {
  157. diff += delta_tabs[delta_mode][get_bits(gb, s->delta_size)];
  158. dst[x] = av_clip_uint8((y ? dst[x - p->linesize[2]] : 128) + diff);
  159. }
  160. dst += p->linesize[2];
  161. }
  162. if (s->hscale > 1) {
  163. dst = p->data[2];
  164. for (y = 0; y < avctx->height >> 2; y++) {
  165. for (x = 1; x < avctx->width >> 2; x += s->hscale)
  166. dst[x] = dst[x - 1];
  167. dst += p->linesize[2];
  168. }
  169. }
  170. dst = p->data[2];
  171. for (y = 0; y < avctx->height >> 2; y++) {
  172. for (x = 0; x < avctx->width >> 2; x++)
  173. dst[x] += (dst[x] - 128) / 8;
  174. dst += p->linesize[2];
  175. }
  176. p->pict_type = AV_PICTURE_TYPE_I;
  177. p->key_frame = 1;
  178. *got_frame = 1;
  179. return avpkt->size;
  180. }
  181. static av_cold int truemotion2rt_decode_init(AVCodecContext *avctx)
  182. {
  183. avctx->pix_fmt = AV_PIX_FMT_YUV410P;
  184. return 0;
  185. }
  186. AVCodec ff_truemotion2rt_decoder = {
  187. .name = "truemotion2rt",
  188. .long_name = NULL_IF_CONFIG_SMALL("Duck TrueMotion 2.0 Real Time"),
  189. .type = AVMEDIA_TYPE_VIDEO,
  190. .id = AV_CODEC_ID_TRUEMOTION2RT,
  191. .priv_data_size = sizeof(TrueMotion2RTContext),
  192. .init = truemotion2rt_decode_init,
  193. .decode = truemotion2rt_decode_frame,
  194. .capabilities = AV_CODEC_CAP_DR1,
  195. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
  196. };