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.

265 lines
7.7KB

  1. /*
  2. * WinCAM Motion Video decoder
  3. *
  4. * Copyright (c) 2018 Paul B Mahol
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include "libavutil/imgutils.h"
  26. #include "avcodec.h"
  27. #include "bytestream.h"
  28. #include "internal.h"
  29. #include <zlib.h>
  30. typedef struct WCMVContext {
  31. int bpp;
  32. z_stream zstream;
  33. AVFrame *prev_frame;
  34. uint8_t block_data[65536*8];
  35. } WCMVContext;
  36. static int decode_frame(AVCodecContext *avctx,
  37. void *data, int *got_frame,
  38. AVPacket *avpkt)
  39. {
  40. WCMVContext *s = avctx->priv_data;
  41. AVFrame *frame = data;
  42. int skip, blocks, zret, ret, intra = 0, flags = 0, bpp = s->bpp;
  43. GetByteContext gb;
  44. uint8_t *dst;
  45. ret = inflateReset(&s->zstream);
  46. if (ret != Z_OK) {
  47. av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", ret);
  48. return AVERROR_EXTERNAL;
  49. }
  50. bytestream2_init(&gb, avpkt->data, avpkt->size);
  51. blocks = bytestream2_get_le16(&gb);
  52. if (!blocks)
  53. flags |= FF_REGET_BUFFER_FLAG_READONLY;
  54. if ((ret = ff_reget_buffer(avctx, s->prev_frame, flags)) < 0)
  55. return ret;
  56. if (blocks > 5) {
  57. GetByteContext bgb;
  58. int x = 0, size;
  59. if (blocks * 8 >= 0xFFFF)
  60. size = bytestream2_get_le24(&gb);
  61. else if (blocks * 8 >= 0xFF)
  62. size = bytestream2_get_le16(&gb);
  63. else
  64. size = bytestream2_get_byte(&gb);
  65. skip = bytestream2_tell(&gb);
  66. if (size > avpkt->size - skip)
  67. return AVERROR_INVALIDDATA;
  68. s->zstream.next_in = avpkt->data + skip;
  69. s->zstream.avail_in = size;
  70. s->zstream.next_out = s->block_data;
  71. s->zstream.avail_out = sizeof(s->block_data);
  72. zret = inflate(&s->zstream, Z_FINISH);
  73. if (zret != Z_STREAM_END) {
  74. av_log(avctx, AV_LOG_ERROR,
  75. "Inflate failed with return code: %d.\n", zret);
  76. return AVERROR_INVALIDDATA;
  77. }
  78. ret = inflateReset(&s->zstream);
  79. if (ret != Z_OK) {
  80. av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", ret);
  81. return AVERROR_EXTERNAL;
  82. }
  83. bytestream2_skip(&gb, size);
  84. bytestream2_init(&bgb, s->block_data, blocks * 8);
  85. for (int i = 0; i < blocks; i++) {
  86. int w, h;
  87. bytestream2_skip(&bgb, 4);
  88. w = bytestream2_get_le16(&bgb);
  89. h = bytestream2_get_le16(&bgb);
  90. if (x + bpp * (int64_t)w * h > INT_MAX)
  91. return AVERROR_INVALIDDATA;
  92. x += bpp * w * h;
  93. }
  94. if (x >= 0xFFFF)
  95. bytestream2_skip(&gb, 3);
  96. else if (x >= 0xFF)
  97. bytestream2_skip(&gb, 2);
  98. else
  99. bytestream2_skip(&gb, 1);
  100. skip = bytestream2_tell(&gb);
  101. s->zstream.next_in = avpkt->data + skip;
  102. s->zstream.avail_in = avpkt->size - skip;
  103. bytestream2_init(&gb, s->block_data, blocks * 8);
  104. } else if (blocks) {
  105. int x = 0;
  106. bytestream2_seek(&gb, 2, SEEK_SET);
  107. for (int i = 0; i < blocks; i++) {
  108. int w, h;
  109. bytestream2_skip(&gb, 4);
  110. w = bytestream2_get_le16(&gb);
  111. h = bytestream2_get_le16(&gb);
  112. if (x + bpp * (int64_t)w * h > INT_MAX)
  113. return AVERROR_INVALIDDATA;
  114. x += bpp * w * h;
  115. }
  116. if (x >= 0xFFFF)
  117. bytestream2_skip(&gb, 3);
  118. else if (x >= 0xFF)
  119. bytestream2_skip(&gb, 2);
  120. else
  121. bytestream2_skip(&gb, 1);
  122. skip = bytestream2_tell(&gb);
  123. s->zstream.next_in = avpkt->data + skip;
  124. s->zstream.avail_in = avpkt->size - skip;
  125. bytestream2_seek(&gb, 2, SEEK_SET);
  126. }
  127. if (bytestream2_get_bytes_left(&gb) < 8LL * blocks)
  128. return AVERROR_INVALIDDATA;
  129. if (!avctx->frame_number) {
  130. ptrdiff_t linesize[4] = { s->prev_frame->linesize[0], 0, 0, 0 };
  131. av_image_fill_black(s->prev_frame->data, linesize, avctx->pix_fmt, 0,
  132. avctx->width, avctx->height);
  133. }
  134. for (int block = 0; block < blocks; block++) {
  135. int x, y, w, h;
  136. x = bytestream2_get_le16(&gb);
  137. y = bytestream2_get_le16(&gb);
  138. w = bytestream2_get_le16(&gb);
  139. h = bytestream2_get_le16(&gb);
  140. if (blocks == 1 && x == 0 && y == 0 && w == avctx->width && h == avctx->height)
  141. intra = 1;
  142. if (x + w > avctx->width || y + h > avctx->height)
  143. return AVERROR_INVALIDDATA;
  144. if (w > avctx->width || h > avctx->height)
  145. return AVERROR_INVALIDDATA;
  146. dst = s->prev_frame->data[0] + (avctx->height - y - 1) * s->prev_frame->linesize[0] + x * bpp;
  147. for (int i = 0; i < h; i++) {
  148. s->zstream.next_out = dst;
  149. s->zstream.avail_out = w * bpp;
  150. zret = inflate(&s->zstream, Z_SYNC_FLUSH);
  151. if (zret != Z_OK && zret != Z_STREAM_END) {
  152. av_log(avctx, AV_LOG_ERROR,
  153. "Inflate failed with return code: %d.\n", zret);
  154. return AVERROR_INVALIDDATA;
  155. }
  156. dst -= s->prev_frame->linesize[0];
  157. }
  158. }
  159. s->prev_frame->key_frame = intra;
  160. s->prev_frame->pict_type = intra ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
  161. if ((ret = av_frame_ref(frame, s->prev_frame)) < 0)
  162. return ret;
  163. *got_frame = 1;
  164. return avpkt->size;
  165. }
  166. static av_cold int decode_init(AVCodecContext *avctx)
  167. {
  168. WCMVContext *s = avctx->priv_data;
  169. int zret;
  170. switch (avctx->bits_per_coded_sample) {
  171. case 16: avctx->pix_fmt = AV_PIX_FMT_RGB565LE; break;
  172. case 24: avctx->pix_fmt = AV_PIX_FMT_BGR24; break;
  173. case 32: avctx->pix_fmt = AV_PIX_FMT_BGRA; break;
  174. default: av_log(avctx, AV_LOG_ERROR, "Unsupported bits_per_coded_sample: %d\n",
  175. avctx->bits_per_coded_sample);
  176. return AVERROR_PATCHWELCOME;
  177. }
  178. s->bpp = avctx->bits_per_coded_sample >> 3;
  179. s->zstream.zalloc = Z_NULL;
  180. s->zstream.zfree = Z_NULL;
  181. s->zstream.opaque = Z_NULL;
  182. zret = inflateInit(&s->zstream);
  183. if (zret != Z_OK) {
  184. av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
  185. return AVERROR_EXTERNAL;
  186. }
  187. s->prev_frame = av_frame_alloc();
  188. if (!s->prev_frame)
  189. return AVERROR(ENOMEM);
  190. return 0;
  191. }
  192. static av_cold int decode_close(AVCodecContext *avctx)
  193. {
  194. WCMVContext *s = avctx->priv_data;
  195. av_frame_free(&s->prev_frame);
  196. inflateEnd(&s->zstream);
  197. return 0;
  198. }
  199. AVCodec ff_wcmv_decoder = {
  200. .name = "wcmv",
  201. .long_name = NULL_IF_CONFIG_SMALL("WinCAM Motion Video"),
  202. .type = AVMEDIA_TYPE_VIDEO,
  203. .id = AV_CODEC_ID_WCMV,
  204. .priv_data_size = sizeof(WCMVContext),
  205. .init = decode_init,
  206. .close = decode_close,
  207. .decode = decode_frame,
  208. .capabilities = AV_CODEC_CAP_DR1,
  209. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
  210. FF_CODEC_CAP_INIT_CLEANUP,
  211. };