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.

226 lines
6.6KB

  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 i;
  58. if (size < 1) {
  59. av_log(avctx, AV_LOG_ERROR, "input packet too small (%d)\n", size);
  60. return AVERROR_INVALIDDATA;
  61. }
  62. header_size = ((buf[0] >> 5) | (buf[0] << 3)) & 0x7f;
  63. if (header_size < 10) {
  64. av_log(avctx, AV_LOG_ERROR, "invalid header size (%d)\n", header_size);
  65. return AVERROR_INVALIDDATA;
  66. }
  67. if (header_size + 1 > size) {
  68. av_log(avctx, AV_LOG_ERROR, "input packet too small (%d)\n", size);
  69. return AVERROR_INVALIDDATA;
  70. }
  71. /* unscramble the header bytes with a XOR operation */
  72. for (i = 1; i < header_size; i++)
  73. header_buffer[i - 1] = buf[i] ^ buf[i + 1];
  74. s->delta_size = header_buffer[1];
  75. s->hscale = 1 + !!header_buffer[3];
  76. if (s->delta_size < 2 || s->delta_size > 4)
  77. return AVERROR_INVALIDDATA;
  78. avctx->height = AV_RL16(header_buffer + 5);
  79. avctx->width = AV_RL16(header_buffer + 7);
  80. av_log(avctx, AV_LOG_DEBUG, "Header size: %d\n", header_size);
  81. return header_size;
  82. }
  83. static int truemotion2rt_decode_frame(AVCodecContext *avctx, void *data,
  84. int *got_frame, AVPacket *avpkt)
  85. {
  86. TrueMotion2RTContext *s = avctx->priv_data;
  87. AVFrame * const p = data;
  88. GetBitContext *gb = &s->gb;
  89. uint8_t *dst;
  90. int x, y, delta_mode;
  91. int ret;
  92. ret = truemotion2rt_decode_header(avctx, avpkt);
  93. if (ret < 0)
  94. return ret;
  95. ret = init_get_bits8(gb, avpkt->data + ret, avpkt->size - ret);
  96. if (ret < 0)
  97. return ret;
  98. ret = ff_get_buffer(avctx, p, 0);
  99. if (ret < 0)
  100. return ret;
  101. skip_bits(gb, 32);
  102. delta_mode = s->delta_size - 2;
  103. dst = p->data[0];
  104. for (y = 0; y < avctx->height; y++) {
  105. int diff = 0;
  106. for (x = 0; x < avctx->width; x += s->hscale) {
  107. diff += delta_tabs[delta_mode][get_bits(gb, s->delta_size)];
  108. dst[x] = av_clip_uint8((y ? dst[x - p->linesize[0]] : 0) + diff);
  109. }
  110. dst += p->linesize[0];
  111. }
  112. if (s->hscale > 1) {
  113. dst = p->data[0];
  114. for (y = 0; y < avctx->height; y++) {
  115. for (x = 1; x < avctx->width; x += s->hscale)
  116. dst[x] = dst[x - 1];
  117. dst += p->linesize[0];
  118. }
  119. }
  120. dst = p->data[0];
  121. for (y = 0; y < avctx->height; y++) {
  122. for (x = 0; x < avctx->width; x++)
  123. dst[x] = av_clip_uint8(dst[x] + (dst[x] - 128) / 3);
  124. dst += p->linesize[0];
  125. }
  126. dst = p->data[1];
  127. for (y = 0; y < avctx->height >> 2; y++) {
  128. int diff = 0;
  129. for (x = 0; x < avctx->width >> 2; 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[1]] : 128) + diff);
  132. }
  133. dst += p->linesize[1];
  134. }
  135. if (s->hscale > 1) {
  136. dst = p->data[1];
  137. for (y = 0; y < avctx->height >> 2; y++) {
  138. for (x = 1; x < avctx->width >> 2; x += s->hscale)
  139. dst[x] = dst[x - 1];
  140. dst += p->linesize[1];
  141. }
  142. }
  143. dst = p->data[1];
  144. for (y = 0; y < avctx->height >> 2; y++) {
  145. for (x = 0; x < avctx->width >> 2; x++)
  146. dst[x] += (dst[x] - 128) / 8;
  147. dst += p->linesize[1];
  148. }
  149. dst = p->data[2];
  150. for (y = 0; y < avctx->height >> 2; y++) {
  151. int diff = 0;
  152. for (x = 0; x < avctx->width >> 2; x += s->hscale) {
  153. diff += delta_tabs[delta_mode][get_bits(gb, s->delta_size)];
  154. dst[x] = av_clip_uint8((y ? dst[x - p->linesize[2]] : 128) + diff);
  155. }
  156. dst += p->linesize[2];
  157. }
  158. if (s->hscale > 1) {
  159. dst = p->data[2];
  160. for (y = 0; y < avctx->height >> 2; y++) {
  161. for (x = 1; x < avctx->width >> 2; x += s->hscale)
  162. dst[x] = dst[x - 1];
  163. dst += p->linesize[2];
  164. }
  165. }
  166. dst = p->data[2];
  167. for (y = 0; y < avctx->height >> 2; y++) {
  168. for (x = 0; x < avctx->width >> 2; x++)
  169. dst[x] += (dst[x] - 128) / 8;
  170. dst += p->linesize[2];
  171. }
  172. p->pict_type = AV_PICTURE_TYPE_I;
  173. p->key_frame = 1;
  174. *got_frame = 1;
  175. return avpkt->size;
  176. }
  177. static av_cold int truemotion2rt_decode_init(AVCodecContext *avctx)
  178. {
  179. avctx->pix_fmt = AV_PIX_FMT_YUV410P;
  180. return 0;
  181. }
  182. AVCodec ff_truemotion2rt_decoder = {
  183. .name = "truemotion2rt",
  184. .long_name = NULL_IF_CONFIG_SMALL("Duck TrueMotion 2.0 Real Time"),
  185. .type = AVMEDIA_TYPE_VIDEO,
  186. .id = AV_CODEC_ID_TRUEMOTION2RT,
  187. .priv_data_size = sizeof(TrueMotion2RTContext),
  188. .init = truemotion2rt_decode_init,
  189. .decode = truemotion2rt_decode_frame,
  190. .capabilities = AV_CODEC_CAP_DR1,
  191. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
  192. };