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.

261 lines
7.4KB

  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. if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
  52. return ret;
  53. if (s->prev_frame->data[0]) {
  54. ret = av_frame_copy(frame, s->prev_frame);
  55. if (ret < 0)
  56. return ret;
  57. } else {
  58. ptrdiff_t linesize[4] = { frame->linesize[0], 0, 0, 0 };
  59. av_image_fill_black(frame->data, linesize, avctx->pix_fmt, 0,
  60. avctx->width, avctx->height);
  61. }
  62. blocks = bytestream2_get_le16(&gb);
  63. if (blocks > 5) {
  64. GetByteContext bgb;
  65. int x = 0, size;
  66. if (blocks * 8 >= 0xFFFF)
  67. size = bytestream2_get_le24(&gb);
  68. else if (blocks * 8 >= 0xFF)
  69. size = bytestream2_get_le16(&gb);
  70. else
  71. size = bytestream2_get_byte(&gb);
  72. skip = bytestream2_tell(&gb);
  73. if (size > avpkt->size - skip)
  74. return AVERROR_INVALIDDATA;
  75. s->zstream.next_in = avpkt->data + skip;
  76. s->zstream.avail_in = size;
  77. s->zstream.next_out = s->block_data;
  78. s->zstream.avail_out = sizeof(s->block_data);
  79. zret = inflate(&s->zstream, Z_FINISH);
  80. if (zret != Z_STREAM_END) {
  81. av_log(avctx, AV_LOG_ERROR,
  82. "Inflate failed with return code: %d.\n", zret);
  83. return AVERROR_INVALIDDATA;
  84. }
  85. ret = inflateReset(&s->zstream);
  86. if (ret != Z_OK) {
  87. av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", ret);
  88. return AVERROR_EXTERNAL;
  89. }
  90. bytestream2_skip(&gb, size);
  91. bytestream2_init(&bgb, s->block_data, blocks * 8);
  92. for (int i = 0; i < blocks; i++) {
  93. int w, h;
  94. bytestream2_skip(&bgb, 4);
  95. w = bytestream2_get_le16(&bgb);
  96. h = bytestream2_get_le16(&bgb);
  97. x += bpp * w * h;
  98. }
  99. if (x >= 0xFFFF)
  100. bytestream2_skip(&gb, 3);
  101. else if (x >= 0xFF)
  102. bytestream2_skip(&gb, 2);
  103. else
  104. bytestream2_skip(&gb, 1);
  105. skip = bytestream2_tell(&gb);
  106. s->zstream.next_in = avpkt->data + skip;
  107. s->zstream.avail_in = avpkt->size - skip;
  108. bytestream2_init(&gb, s->block_data, blocks * 8);
  109. } else if (blocks) {
  110. int x = 0;
  111. bytestream2_seek(&gb, 2, SEEK_SET);
  112. for (int i = 0; i < blocks; i++) {
  113. int w, h;
  114. bytestream2_skip(&gb, 4);
  115. w = bytestream2_get_le16(&gb);
  116. h = bytestream2_get_le16(&gb);
  117. x += bpp * w * h;
  118. }
  119. if (x >= 0xFFFF)
  120. bytestream2_skip(&gb, 3);
  121. else if (x >= 0xFF)
  122. bytestream2_skip(&gb, 2);
  123. else
  124. bytestream2_skip(&gb, 1);
  125. skip = bytestream2_tell(&gb);
  126. s->zstream.next_in = avpkt->data + skip;
  127. s->zstream.avail_in = avpkt->size - skip;
  128. bytestream2_seek(&gb, 2, SEEK_SET);
  129. }
  130. for (int block = 0; block < blocks; block++) {
  131. int x, y, w, h;
  132. x = bytestream2_get_le16(&gb);
  133. y = bytestream2_get_le16(&gb);
  134. w = bytestream2_get_le16(&gb);
  135. h = bytestream2_get_le16(&gb);
  136. if (blocks == 1 && x == 0 && y == 0 && w == avctx->width && h == avctx->height)
  137. intra = 1;
  138. if (x + w > avctx->width || y + h > avctx->height)
  139. return AVERROR_INVALIDDATA;
  140. if (w > avctx->width || h > avctx->height)
  141. return AVERROR_INVALIDDATA;
  142. dst = frame->data[0] + (avctx->height - y - 1) * frame->linesize[0] + x * bpp;
  143. for (int i = 0; i < h; i++) {
  144. s->zstream.next_out = dst;
  145. s->zstream.avail_out = w * bpp;
  146. zret = inflate(&s->zstream, Z_SYNC_FLUSH);
  147. if (zret != Z_OK && zret != Z_STREAM_END) {
  148. av_log(avctx, AV_LOG_ERROR,
  149. "Inflate failed with return code: %d.\n", zret);
  150. return AVERROR_INVALIDDATA;
  151. }
  152. dst -= frame->linesize[0];
  153. }
  154. }
  155. frame->key_frame = intra;
  156. frame->pict_type = intra ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
  157. av_frame_unref(s->prev_frame);
  158. if ((ret = av_frame_ref(s->prev_frame, frame)) < 0)
  159. return ret;
  160. *got_frame = 1;
  161. return avpkt->size;
  162. }
  163. static av_cold int decode_init(AVCodecContext *avctx)
  164. {
  165. WCMVContext *s = avctx->priv_data;
  166. int zret;
  167. switch (avctx->bits_per_coded_sample) {
  168. case 16: avctx->pix_fmt = AV_PIX_FMT_RGB565LE; break;
  169. case 24: avctx->pix_fmt = AV_PIX_FMT_BGR24; break;
  170. case 32: avctx->pix_fmt = AV_PIX_FMT_BGRA; break;
  171. default: av_log(avctx, AV_LOG_ERROR, "Unsupported bits_per_coded_sample: %d\n",
  172. avctx->bits_per_coded_sample);
  173. return AVERROR_PATCHWELCOME;
  174. }
  175. s->bpp = avctx->bits_per_coded_sample >> 3;
  176. s->zstream.zalloc = Z_NULL;
  177. s->zstream.zfree = Z_NULL;
  178. s->zstream.opaque = Z_NULL;
  179. zret = inflateInit(&s->zstream);
  180. if (zret != Z_OK) {
  181. av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
  182. return AVERROR_EXTERNAL;
  183. }
  184. s->prev_frame = av_frame_alloc();
  185. if (!s->prev_frame)
  186. return AVERROR(ENOMEM);
  187. return 0;
  188. }
  189. static av_cold int decode_close(AVCodecContext *avctx)
  190. {
  191. WCMVContext *s = avctx->priv_data;
  192. av_frame_free(&s->prev_frame);
  193. inflateEnd(&s->zstream);
  194. return 0;
  195. }
  196. AVCodec ff_wcmv_decoder = {
  197. .name = "wcmv",
  198. .long_name = NULL_IF_CONFIG_SMALL("WinCAM Motion Video"),
  199. .type = AVMEDIA_TYPE_VIDEO,
  200. .id = AV_CODEC_ID_WCMV,
  201. .priv_data_size = sizeof(WCMVContext),
  202. .init = decode_init,
  203. .close = decode_close,
  204. .decode = decode_frame,
  205. .capabilities = AV_CODEC_CAP_DR1,
  206. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
  207. FF_CODEC_CAP_INIT_CLEANUP,
  208. };