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.

232 lines
6.1KB

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