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.

288 lines
8.6KB

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