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.

293 lines
8.9KB

  1. /*
  2. * MidiVid decoder
  3. * Copyright (c) 2019 Paul B Mahol
  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. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include "libavutil/imgutils.h"
  25. #include "libavutil/internal.h"
  26. #include "libavutil/intreadwrite.h"
  27. #include "libavutil/mem.h"
  28. #define BITSTREAM_READER_LE
  29. #include "avcodec.h"
  30. #include "get_bits.h"
  31. #include "bytestream.h"
  32. #include "internal.h"
  33. typedef struct MidiVidContext {
  34. GetByteContext gb;
  35. uint8_t *uncompressed;
  36. unsigned int uncompressed_size;
  37. uint8_t *skip;
  38. AVFrame *frame;
  39. } MidiVidContext;
  40. static int decode_mvdv(MidiVidContext *s, AVCodecContext *avctx, AVFrame *frame)
  41. {
  42. GetByteContext *gb = &s->gb;
  43. GetBitContext mask;
  44. GetByteContext idx9;
  45. uint16_t nb_vectors, intra_flag;
  46. const uint8_t *vec;
  47. const uint8_t *mask_start;
  48. uint8_t *skip;
  49. uint32_t mask_size;
  50. int idx9bits = 0;
  51. int idx9val = 0;
  52. uint32_t nb_blocks;
  53. nb_vectors = bytestream2_get_le16(gb);
  54. intra_flag = !!bytestream2_get_le16(gb);
  55. if (intra_flag) {
  56. nb_blocks = (avctx->width / 2) * (avctx->height / 2);
  57. } else {
  58. int ret, skip_linesize, padding;
  59. nb_blocks = bytestream2_get_le32(gb);
  60. skip_linesize = avctx->width >> 1;
  61. mask_start = gb->buffer_start + bytestream2_tell(gb);
  62. mask_size = (FFALIGN(avctx->width, 32) >> 2) * (avctx->height >> 2) >> 3;
  63. padding = (FFALIGN(avctx->width, 32) - avctx->width) >> 2;
  64. if (bytestream2_get_bytes_left(gb) < mask_size)
  65. return AVERROR_INVALIDDATA;
  66. ret = init_get_bits8(&mask, mask_start, mask_size);
  67. if (ret < 0)
  68. return ret;
  69. bytestream2_skip(gb, mask_size);
  70. skip = s->skip;
  71. for (int y = 0; y < avctx->height >> 2; y++) {
  72. for (int x = 0; x < avctx->width >> 2; x++) {
  73. int flag = !get_bits1(&mask);
  74. skip[(y*2) *skip_linesize + x*2 ] = flag;
  75. skip[(y*2) *skip_linesize + x*2+1] = flag;
  76. skip[(y*2+1)*skip_linesize + x*2 ] = flag;
  77. skip[(y*2+1)*skip_linesize + x*2+1] = flag;
  78. }
  79. skip_bits_long(&mask, padding);
  80. }
  81. }
  82. vec = gb->buffer_start + bytestream2_tell(gb);
  83. if (bytestream2_get_bytes_left(gb) < nb_vectors * 12)
  84. return AVERROR_INVALIDDATA;
  85. bytestream2_skip(gb, nb_vectors * 12);
  86. if (nb_vectors > 256) {
  87. if (bytestream2_get_bytes_left(gb) < (nb_blocks + 7 * !intra_flag) / 8)
  88. return AVERROR_INVALIDDATA;
  89. bytestream2_init(&idx9, gb->buffer_start + bytestream2_tell(gb), (nb_blocks + 7 * !intra_flag) / 8);
  90. bytestream2_skip(gb, (nb_blocks + 7 * !intra_flag) / 8);
  91. }
  92. skip = s->skip;
  93. for (int y = avctx->height - 2; y >= 0; y -= 2) {
  94. uint8_t *dsty = frame->data[0] + y * frame->linesize[0];
  95. uint8_t *dstu = frame->data[1] + y * frame->linesize[1];
  96. uint8_t *dstv = frame->data[2] + y * frame->linesize[2];
  97. for (int x = 0; x < avctx->width; x += 2) {
  98. int idx;
  99. if (!intra_flag && *skip++)
  100. continue;
  101. if (bytestream2_get_bytes_left(gb) <= 0)
  102. return AVERROR_INVALIDDATA;
  103. if (nb_vectors <= 256) {
  104. idx = bytestream2_get_byte(gb);
  105. } else {
  106. if (idx9bits == 0) {
  107. idx9val = bytestream2_get_byte(&idx9);
  108. idx9bits = 8;
  109. }
  110. idx9bits--;
  111. idx = bytestream2_get_byte(gb) | (((idx9val >> (7 - idx9bits)) & 1) << 8);
  112. }
  113. if (idx >= nb_vectors)
  114. return AVERROR_INVALIDDATA;
  115. dsty[x +frame->linesize[0]] = vec[idx * 12 + 0];
  116. dsty[x+1+frame->linesize[0]] = vec[idx * 12 + 3];
  117. dsty[x] = vec[idx * 12 + 6];
  118. dsty[x+1] = vec[idx * 12 + 9];
  119. dstu[x +frame->linesize[1]] = vec[idx * 12 + 1];
  120. dstu[x+1+frame->linesize[1]] = vec[idx * 12 + 4];
  121. dstu[x] = vec[idx * 12 + 7];
  122. dstu[x+1] = vec[idx * 12 +10];
  123. dstv[x +frame->linesize[2]] = vec[idx * 12 + 2];
  124. dstv[x+1+frame->linesize[2]] = vec[idx * 12 + 5];
  125. dstv[x] = vec[idx * 12 + 8];
  126. dstv[x+1] = vec[idx * 12 +11];
  127. }
  128. }
  129. return intra_flag;
  130. }
  131. static ptrdiff_t lzss_uncompress(MidiVidContext *s, GetByteContext *gb, uint8_t *dst, unsigned int size)
  132. {
  133. uint8_t *dst_start = dst;
  134. uint8_t *dst_end = dst + size;
  135. for (;bytestream2_get_bytes_left(gb) >= 3;) {
  136. int op = bytestream2_get_le16(gb);
  137. for (int i = 0; i < 16; i++) {
  138. if (op & 1) {
  139. int s0 = bytestream2_get_byte(gb);
  140. int s1 = bytestream2_get_byte(gb);
  141. int offset = ((s0 & 0xF0) << 4) | s1;
  142. int length = (s0 & 0xF) + 3;
  143. if (dst + length > dst_end ||
  144. dst - offset < dst_start)
  145. return AVERROR_INVALIDDATA;
  146. if (offset > 0) {
  147. for (int j = 0; j < length; j++) {
  148. dst[j] = dst[j - offset];
  149. }
  150. }
  151. dst += length;
  152. } else {
  153. if (dst >= dst_end)
  154. return AVERROR_INVALIDDATA;
  155. *dst++ = bytestream2_get_byte(gb);
  156. }
  157. op >>= 1;
  158. }
  159. }
  160. return dst - dst_start;
  161. }
  162. static int decode_frame(AVCodecContext *avctx, void *data,
  163. int *got_frame, AVPacket *avpkt)
  164. {
  165. MidiVidContext *s = avctx->priv_data;
  166. GetByteContext *gb = &s->gb;
  167. AVFrame *frame = s->frame;
  168. int ret, key, uncompressed;
  169. if (avpkt->size <= 13)
  170. return AVERROR_INVALIDDATA;
  171. bytestream2_init(gb, avpkt->data, avpkt->size);
  172. bytestream2_skip(gb, 8);
  173. uncompressed = bytestream2_get_le32(gb);
  174. if ((ret = ff_reget_buffer(avctx, s->frame, 0)) < 0)
  175. return ret;
  176. if (uncompressed) {
  177. ret = decode_mvdv(s, avctx, frame);
  178. } else {
  179. av_fast_padded_malloc(&s->uncompressed, &s->uncompressed_size, 16LL * (avpkt->size - 12));
  180. if (!s->uncompressed)
  181. return AVERROR(ENOMEM);
  182. ret = lzss_uncompress(s, gb, s->uncompressed, s->uncompressed_size);
  183. if (ret < 0)
  184. return ret;
  185. bytestream2_init(gb, s->uncompressed, ret);
  186. ret = decode_mvdv(s, avctx, frame);
  187. }
  188. if (ret < 0)
  189. return ret;
  190. key = ret;
  191. if ((ret = av_frame_ref(data, s->frame)) < 0)
  192. return ret;
  193. frame->pict_type = key ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
  194. frame->key_frame = key;
  195. *got_frame = 1;
  196. return avpkt->size;
  197. }
  198. static av_cold int decode_init(AVCodecContext *avctx)
  199. {
  200. MidiVidContext *s = avctx->priv_data;
  201. int ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
  202. if (avctx->width & 3 || avctx->height & 3)
  203. ret = AVERROR_INVALIDDATA;
  204. if (ret < 0) {
  205. av_log(avctx, AV_LOG_ERROR, "Invalid image size %dx%d.\n",
  206. avctx->width, avctx->height);
  207. return ret;
  208. }
  209. avctx->pix_fmt = AV_PIX_FMT_YUV444P;
  210. s->frame = av_frame_alloc();
  211. if (!s->frame)
  212. return AVERROR(ENOMEM);
  213. s->skip = av_calloc(avctx->width >> 1, avctx->height >> 1);
  214. if (!s->skip)
  215. return AVERROR(ENOMEM);
  216. return 0;
  217. }
  218. static void decode_flush(AVCodecContext *avctx)
  219. {
  220. MidiVidContext *s = avctx->priv_data;
  221. av_frame_unref(s->frame);
  222. }
  223. static av_cold int decode_close(AVCodecContext *avctx)
  224. {
  225. MidiVidContext *s = avctx->priv_data;
  226. av_frame_free(&s->frame);
  227. av_freep(&s->uncompressed);
  228. av_freep(&s->skip);
  229. return 0;
  230. }
  231. AVCodec ff_mvdv_decoder = {
  232. .name = "mvdv",
  233. .long_name = NULL_IF_CONFIG_SMALL("MidiVid VQ"),
  234. .type = AVMEDIA_TYPE_VIDEO,
  235. .id = AV_CODEC_ID_MVDV,
  236. .priv_data_size = sizeof(MidiVidContext),
  237. .init = decode_init,
  238. .decode = decode_frame,
  239. .flush = decode_flush,
  240. .close = decode_close,
  241. .capabilities = AV_CODEC_CAP_DR1,
  242. .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
  243. };