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.

247 lines
6.5KB

  1. /*
  2. * Microsoft Screen 1 (aka Windows Media Video V7 Screen) decoder
  3. * Copyright (c) 2012 Konstantin Shishkov
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * Microsoft Screen 1 (aka Windows Media Video V7 Screen) decoder
  24. */
  25. #include "libavutil/intfloat.h"
  26. #include "libavutil/intreadwrite.h"
  27. #include "avcodec.h"
  28. #include "mss12.h"
  29. typedef struct MSS1Context {
  30. MSS12Context ctx;
  31. AVFrame pic;
  32. } MSS1Context;
  33. static void arith_normalise(ArithCoder *c)
  34. {
  35. for (;;) {
  36. if (c->high >= 0x8000) {
  37. if (c->low < 0x8000) {
  38. if (c->low >= 0x4000 && c->high < 0xC000) {
  39. c->value -= 0x4000;
  40. c->low -= 0x4000;
  41. c->high -= 0x4000;
  42. } else {
  43. return;
  44. }
  45. } else {
  46. c->value -= 0x8000;
  47. c->low -= 0x8000;
  48. c->high -= 0x8000;
  49. }
  50. }
  51. c->value <<= 1;
  52. c->low <<= 1;
  53. c->high <<= 1;
  54. c->high |= 1;
  55. c->value |= get_bits1(c->gb);
  56. }
  57. }
  58. static int arith_get_bit(ArithCoder *c)
  59. {
  60. int range = c->high - c->low + 1;
  61. int bit = (((c->value - c->low) << 1) + 1) / range;
  62. if (bit)
  63. c->low += range >> 1;
  64. else
  65. c->high = c->low + (range >> 1) - 1;
  66. arith_normalise(c);
  67. return bit;
  68. }
  69. static int arith_get_bits(ArithCoder *c, int bits)
  70. {
  71. int range = c->high - c->low + 1;
  72. int val = (((c->value - c->low + 1) << bits) - 1) / range;
  73. int prob = range * val;
  74. c->high = ((prob + range) >> bits) + c->low - 1;
  75. c->low += prob >> bits;
  76. arith_normalise(c);
  77. return val;
  78. }
  79. static int arith_get_number(ArithCoder *c, int mod_val)
  80. {
  81. int range = c->high - c->low + 1;
  82. int val = ((c->value - c->low + 1) * mod_val - 1) / range;
  83. int prob = range * val;
  84. c->high = (prob + range) / mod_val + c->low - 1;
  85. c->low += prob / mod_val;
  86. arith_normalise(c);
  87. return val;
  88. }
  89. static int arith_get_prob(ArithCoder *c, int *probs)
  90. {
  91. int range = c->high - c->low + 1;
  92. int val = ((c->value - c->low + 1) * probs[0] - 1) / range;
  93. int sym = 1;
  94. while (probs[sym] > val)
  95. sym++;
  96. c->high = range * probs[sym - 1] / probs[0] + c->low - 1;
  97. c->low += range * probs[sym] / probs[0];
  98. return sym;
  99. }
  100. static int arith_get_model_sym(ArithCoder *c, Model *m)
  101. {
  102. int idx, val;
  103. idx = arith_get_prob(c, m->cum_prob);
  104. val = m->idx2sym[idx];
  105. ff_mss12_model_update(m, idx);
  106. arith_normalise(c);
  107. return val;
  108. }
  109. static void arith_init(ArithCoder *c, GetBitContext *gb)
  110. {
  111. c->low = 0;
  112. c->high = 0xFFFF;
  113. c->value = get_bits(gb, 16);
  114. c->gb = gb;
  115. c->get_model_sym = arith_get_model_sym;
  116. c->get_number = arith_get_number;
  117. }
  118. static int decode_pal(MSS1Context *ctx, ArithCoder *acoder)
  119. {
  120. int i, ncol, r, g, b;
  121. uint32_t *pal = ctx->ctx.pal + 256 - ctx->ctx.free_colours;
  122. if (!ctx->ctx.free_colours)
  123. return 0;
  124. ncol = arith_get_number(acoder, ctx->ctx.free_colours + 1);
  125. for (i = 0; i < ncol; i++) {
  126. r = arith_get_bits(acoder, 8);
  127. g = arith_get_bits(acoder, 8);
  128. b = arith_get_bits(acoder, 8);
  129. *pal++ = (r << 16) | (g << 8) | b;
  130. }
  131. return !!ncol;
  132. }
  133. static int mss1_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
  134. AVPacket *avpkt)
  135. {
  136. const uint8_t *buf = avpkt->data;
  137. int buf_size = avpkt->size;
  138. MSS1Context *c = avctx->priv_data;
  139. GetBitContext gb;
  140. ArithCoder acoder;
  141. int pal_changed = 0;
  142. int ret;
  143. init_get_bits(&gb, buf, buf_size * 8);
  144. arith_init(&acoder, &gb);
  145. c->pic.reference = 3;
  146. c->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
  147. FF_BUFFER_HINTS_REUSABLE;
  148. if ((ret = avctx->reget_buffer(avctx, &c->pic)) < 0) {
  149. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  150. return ret;
  151. }
  152. c->ctx.pic_start = c->pic.data[0] + c->pic.linesize[0] * (avctx->height - 1);
  153. c->ctx.pic_stride = -c->pic.linesize[0];
  154. c->ctx.keyframe = !arith_get_bit(&acoder);
  155. if (c->ctx.keyframe) {
  156. ff_mss12_codec_reset(&c->ctx);
  157. pal_changed = decode_pal(c, &acoder);
  158. c->pic.key_frame = 1;
  159. c->pic.pict_type = AV_PICTURE_TYPE_I;
  160. } else {
  161. if (c->ctx.corrupted)
  162. return AVERROR_INVALIDDATA;
  163. c->pic.key_frame = 0;
  164. c->pic.pict_type = AV_PICTURE_TYPE_P;
  165. }
  166. c->ctx.corrupted = ff_mss12_decode_rect(&c->ctx, &acoder, 0, 0,
  167. avctx->width, avctx->height);
  168. if (c->ctx.corrupted)
  169. return AVERROR_INVALIDDATA;
  170. memcpy(c->pic.data[1], c->ctx.pal, AVPALETTE_SIZE);
  171. c->pic.palette_has_changed = pal_changed;
  172. *data_size = sizeof(AVFrame);
  173. *(AVFrame*)data = c->pic;
  174. /* always report that the buffer was completely consumed */
  175. return buf_size;
  176. }
  177. static av_cold int mss1_decode_init(AVCodecContext *avctx)
  178. {
  179. MSS1Context * const c = avctx->priv_data;
  180. c->ctx.avctx = avctx;
  181. avctx->coded_frame = &c->pic;
  182. return ff_mss12_decode_init(avctx, 0);
  183. }
  184. static av_cold int mss1_decode_end(AVCodecContext *avctx)
  185. {
  186. MSS1Context * const c = avctx->priv_data;
  187. if (c->pic.data[0])
  188. avctx->release_buffer(avctx, &c->pic);
  189. ff_mss12_decode_end(avctx);
  190. return 0;
  191. }
  192. AVCodec ff_mss1_decoder = {
  193. .name = "mss1",
  194. .type = AVMEDIA_TYPE_VIDEO,
  195. .id = AV_CODEC_ID_MSS1,
  196. .priv_data_size = sizeof(MSS1Context),
  197. .init = mss1_decode_init,
  198. .close = mss1_decode_end,
  199. .decode = mss1_decode_frame,
  200. .capabilities = CODEC_CAP_DR1,
  201. .long_name = NULL_IF_CONFIG_SMALL("MS Screen 1"),
  202. };