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.

267 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, 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. return avpkt->size;
  54. if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 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 (s->prev_frame->data[0]) {
  128. ret = av_frame_copy(frame, s->prev_frame);
  129. if (ret < 0)
  130. return ret;
  131. } else {
  132. ptrdiff_t linesize[4] = { frame->linesize[0], 0, 0, 0 };
  133. av_image_fill_black(frame->data, linesize, avctx->pix_fmt, 0,
  134. avctx->width, avctx->height);
  135. }
  136. for (int block = 0; block < blocks; block++) {
  137. int x, y, w, h;
  138. x = bytestream2_get_le16(&gb);
  139. y = bytestream2_get_le16(&gb);
  140. w = bytestream2_get_le16(&gb);
  141. h = bytestream2_get_le16(&gb);
  142. if (blocks == 1 && x == 0 && y == 0 && w == avctx->width && h == avctx->height)
  143. intra = 1;
  144. if (x + w > avctx->width || y + h > avctx->height)
  145. return AVERROR_INVALIDDATA;
  146. if (w > avctx->width || h > avctx->height)
  147. return AVERROR_INVALIDDATA;
  148. dst = frame->data[0] + (avctx->height - y - 1) * frame->linesize[0] + x * bpp;
  149. for (int i = 0; i < h; i++) {
  150. s->zstream.next_out = dst;
  151. s->zstream.avail_out = w * bpp;
  152. zret = inflate(&s->zstream, Z_SYNC_FLUSH);
  153. if (zret != Z_OK && zret != Z_STREAM_END) {
  154. av_log(avctx, AV_LOG_ERROR,
  155. "Inflate failed with return code: %d.\n", zret);
  156. return AVERROR_INVALIDDATA;
  157. }
  158. dst -= frame->linesize[0];
  159. }
  160. }
  161. frame->key_frame = intra;
  162. frame->pict_type = intra ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
  163. av_frame_unref(s->prev_frame);
  164. if ((ret = av_frame_ref(s->prev_frame, frame)) < 0)
  165. return ret;
  166. *got_frame = 1;
  167. return avpkt->size;
  168. }
  169. static av_cold int decode_init(AVCodecContext *avctx)
  170. {
  171. WCMVContext *s = avctx->priv_data;
  172. int zret;
  173. switch (avctx->bits_per_coded_sample) {
  174. case 16: avctx->pix_fmt = AV_PIX_FMT_RGB565LE; break;
  175. case 24: avctx->pix_fmt = AV_PIX_FMT_BGR24; break;
  176. case 32: avctx->pix_fmt = AV_PIX_FMT_BGRA; break;
  177. default: av_log(avctx, AV_LOG_ERROR, "Unsupported bits_per_coded_sample: %d\n",
  178. avctx->bits_per_coded_sample);
  179. return AVERROR_PATCHWELCOME;
  180. }
  181. s->bpp = avctx->bits_per_coded_sample >> 3;
  182. s->zstream.zalloc = Z_NULL;
  183. s->zstream.zfree = Z_NULL;
  184. s->zstream.opaque = Z_NULL;
  185. zret = inflateInit(&s->zstream);
  186. if (zret != Z_OK) {
  187. av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
  188. return AVERROR_EXTERNAL;
  189. }
  190. s->prev_frame = av_frame_alloc();
  191. if (!s->prev_frame)
  192. return AVERROR(ENOMEM);
  193. return 0;
  194. }
  195. static av_cold int decode_close(AVCodecContext *avctx)
  196. {
  197. WCMVContext *s = avctx->priv_data;
  198. av_frame_free(&s->prev_frame);
  199. inflateEnd(&s->zstream);
  200. return 0;
  201. }
  202. AVCodec ff_wcmv_decoder = {
  203. .name = "wcmv",
  204. .long_name = NULL_IF_CONFIG_SMALL("WinCAM Motion Video"),
  205. .type = AVMEDIA_TYPE_VIDEO,
  206. .id = AV_CODEC_ID_WCMV,
  207. .priv_data_size = sizeof(WCMVContext),
  208. .init = decode_init,
  209. .close = decode_close,
  210. .decode = decode_frame,
  211. .capabilities = AV_CODEC_CAP_DR1,
  212. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
  213. FF_CODEC_CAP_INIT_CLEANUP,
  214. };