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.

227 lines
6.2KB

  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 FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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 "avcodec.h"
  26. #include "mss12.h"
  27. typedef struct MSS1Context {
  28. MSS12Context ctx;
  29. AVFrame pic;
  30. SliceContext sc;
  31. } MSS1Context;
  32. static void arith_normalise(ArithCoder *c)
  33. {
  34. for (;;) {
  35. if (c->high >= 0x8000) {
  36. if (c->low < 0x8000) {
  37. if (c->low >= 0x4000 && c->high < 0xC000) {
  38. c->value -= 0x4000;
  39. c->low -= 0x4000;
  40. c->high -= 0x4000;
  41. } else {
  42. return;
  43. }
  44. } else {
  45. c->value -= 0x8000;
  46. c->low -= 0x8000;
  47. c->high -= 0x8000;
  48. }
  49. }
  50. c->value <<= 1;
  51. c->low <<= 1;
  52. c->high <<= 1;
  53. c->high |= 1;
  54. c->value |= get_bits1(c->gbc.gb);
  55. }
  56. }
  57. ARITH_GET_BIT()
  58. static int arith_get_bits(ArithCoder *c, int bits)
  59. {
  60. int range = c->high - c->low + 1;
  61. int val = (((c->value - c->low + 1) << bits) - 1) / range;
  62. int prob = range * val;
  63. c->high = ((prob + range) >> bits) + c->low - 1;
  64. c->low += prob >> bits;
  65. arith_normalise(c);
  66. return val;
  67. }
  68. static int arith_get_number(ArithCoder *c, int mod_val)
  69. {
  70. int range = c->high - c->low + 1;
  71. int val = ((c->value - c->low + 1) * mod_val - 1) / range;
  72. int prob = range * val;
  73. c->high = (prob + range) / mod_val + c->low - 1;
  74. c->low += prob / mod_val;
  75. arith_normalise(c);
  76. return val;
  77. }
  78. static int arith_get_prob(ArithCoder *c, int16_t *probs)
  79. {
  80. int range = c->high - c->low + 1;
  81. int val = ((c->value - c->low + 1) * probs[0] - 1) / range;
  82. int sym = 1;
  83. while (probs[sym] > val)
  84. sym++;
  85. c->high = range * probs[sym - 1] / probs[0] + c->low - 1;
  86. c->low += range * probs[sym] / probs[0];
  87. return sym;
  88. }
  89. ARITH_GET_MODEL_SYM()
  90. static void arith_init(ArithCoder *c, GetBitContext *gb)
  91. {
  92. c->low = 0;
  93. c->high = 0xFFFF;
  94. c->value = get_bits(gb, 16);
  95. c->gbc.gb = gb;
  96. c->get_model_sym = arith_get_model_sym;
  97. c->get_number = arith_get_number;
  98. }
  99. static int decode_pal(MSS12Context *ctx, ArithCoder *acoder)
  100. {
  101. int i, ncol, r, g, b;
  102. uint32_t *pal = ctx->pal + 256 - ctx->free_colours;
  103. if (!ctx->free_colours)
  104. return 0;
  105. ncol = arith_get_number(acoder, ctx->free_colours + 1);
  106. for (i = 0; i < ncol; i++) {
  107. r = arith_get_bits(acoder, 8);
  108. g = arith_get_bits(acoder, 8);
  109. b = arith_get_bits(acoder, 8);
  110. *pal++ = (0xFFU << 24) | (r << 16) | (g << 8) | b;
  111. }
  112. return !!ncol;
  113. }
  114. static int mss1_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  115. AVPacket *avpkt)
  116. {
  117. const uint8_t *buf = avpkt->data;
  118. int buf_size = avpkt->size;
  119. MSS1Context *ctx = avctx->priv_data;
  120. MSS12Context *c = &ctx->ctx;
  121. GetBitContext gb;
  122. ArithCoder acoder;
  123. int pal_changed = 0;
  124. int ret;
  125. init_get_bits(&gb, buf, buf_size * 8);
  126. arith_init(&acoder, &gb);
  127. ctx->pic.reference = 3;
  128. ctx->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_READABLE |
  129. FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
  130. if ((ret = avctx->reget_buffer(avctx, &ctx->pic)) < 0) {
  131. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  132. return ret;
  133. }
  134. c->pal_pic = ctx->pic.data[0] + ctx->pic.linesize[0] * (avctx->height - 1);
  135. c->pal_stride = -ctx->pic.linesize[0];
  136. c->keyframe = !arith_get_bit(&acoder);
  137. if (c->keyframe) {
  138. c->corrupted = 0;
  139. ff_mss12_slicecontext_reset(&ctx->sc);
  140. pal_changed = decode_pal(c, &acoder);
  141. ctx->pic.key_frame = 1;
  142. ctx->pic.pict_type = AV_PICTURE_TYPE_I;
  143. } else {
  144. if (c->corrupted)
  145. return AVERROR_INVALIDDATA;
  146. ctx->pic.key_frame = 0;
  147. ctx->pic.pict_type = AV_PICTURE_TYPE_P;
  148. }
  149. c->corrupted = ff_mss12_decode_rect(&ctx->sc, &acoder, 0, 0,
  150. avctx->width, avctx->height);
  151. if (c->corrupted)
  152. return AVERROR_INVALIDDATA;
  153. memcpy(ctx->pic.data[1], c->pal, AVPALETTE_SIZE);
  154. ctx->pic.palette_has_changed = pal_changed;
  155. *got_frame = 1;
  156. *(AVFrame*)data = ctx->pic;
  157. /* always report that the buffer was completely consumed */
  158. return buf_size;
  159. }
  160. static av_cold int mss1_decode_init(AVCodecContext *avctx)
  161. {
  162. MSS1Context * const c = avctx->priv_data;
  163. int ret;
  164. c->ctx.avctx = avctx;
  165. avctx->coded_frame = &c->pic;
  166. ret = ff_mss12_decode_init(&c->ctx, 0, &c->sc, NULL);
  167. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  168. return ret;
  169. }
  170. static av_cold int mss1_decode_end(AVCodecContext *avctx)
  171. {
  172. MSS1Context * const ctx = avctx->priv_data;
  173. if (ctx->pic.data[0])
  174. avctx->release_buffer(avctx, &ctx->pic);
  175. ff_mss12_decode_end(&ctx->ctx);
  176. return 0;
  177. }
  178. AVCodec ff_mss1_decoder = {
  179. .name = "mss1",
  180. .type = AVMEDIA_TYPE_VIDEO,
  181. .id = AV_CODEC_ID_MSS1,
  182. .priv_data_size = sizeof(MSS1Context),
  183. .init = mss1_decode_init,
  184. .close = mss1_decode_end,
  185. .decode = mss1_decode_frame,
  186. .capabilities = CODEC_CAP_DR1,
  187. .long_name = NULL_IF_CONFIG_SMALL("MS Screen 1"),
  188. };